[Zrouter-src-freebsd] ZRouter.org: push to FreeBSD HEAD tree

zrouter-src-freebsd at zrouter.org zrouter-src-freebsd at zrouter.org
Fri Jan 13 14:00:52 UTC 2012


details:   http://zrouter.org/hg/FreeBSD/head//rev/1750de5b0399
changeset: 279:1750de5b0399
user:      ray at terran.dlink.ua
date:      Fri Jan 13 15:51:01 2012 +0200
description:
FreeBSD HEAD @svn r230009.

diffstat:

 head/Makefile.inc1                                                |      7 +-
 head/UPDATING                                                     |      9 +-
 head/bin/ps/ps.c                                                  |      4 +-
 head/contrib/gcc/config/freebsd.h                                 |      3 +-
 head/contrib/groff/tmac/doc-syms                                  |      4 +-
 head/contrib/groff/tmac/groff_mdoc.man                            |      4 +-
 head/contrib/pf/pfctl/pfctl.8                                     |      4 +-
 head/etc/mac.conf                                                 |      4 +-
 head/etc/namedb/named.conf                                        |      4 +-
 head/etc/network.subr                                             |      4 +-
 head/etc/rc.d/auditd                                              |      4 +-
 head/etc/rc.d/bgfsck                                              |      4 +-
 head/etc/rc.d/dhclient                                            |     11 +-
 head/etc/rc.d/hostname                                            |      4 +-
 head/etc/rc.d/pfsync                                              |      9 +-
 head/etc/rc.firewall                                              |     12 +-
 head/etc/rc.initdiskless                                          |      8 +-
 head/etc/rc.subr                                                  |      9 +-
 head/etc/services                                                 |     10 +-
 head/gnu/usr.bin/cvs/lib/config.h.proto                           |      4 +-
 head/include/arpa/nameser_compat.h                                |      6 +-
 head/include/complex.h                                            |     10 +-
 head/include/dlfcn.h                                              |      3 +-
 head/include/gssapi/gssapi.h                                      |      6 +-
 head/include/resolv.h                                             |      4 +-
 head/include/rpc/svc.h                                            |      4 +-
 head/include/rpcsvc/mount.x                                       |      6 +-
 head/include/rpcsvc/nfs_prot.x                                    |      6 +-
 head/include/rpcsvc/nis.x                                         |      6 +-
 head/include/stdlib.h                                             |      7 +-
 head/include/tgmath.h                                             |     47 +-
 head/lib/libc/gen/Makefile.inc                                    |      6 +-
 head/lib/libc/gen/Symbol.map                                      |      3 +-
 head/lib/libc/gen/dlfcn.c                                         |     11 +-
 head/lib/libc/gen/dlopen.3                                        |     38 +-
 head/lib/libc/gen/getnetgrent.c                                   |      2 +-
 head/lib/libc/net/getaddrinfo.c                                   |      5 +-
 head/lib/libc/powerpc/SYS.h                                       |     17 +-
 head/lib/libc/powerpc/gen/setjmp.S                                |      5 +-
 head/lib/libc/powerpc64/SYS.h                                     |     17 +-
 head/lib/libc/powerpc64/gen/setjmp.S                              |      7 +-
 head/lib/libc/stdlib/Makefile.inc                                 |      9 +-
 head/lib/libc/stdlib/Symbol.map                                   |      9 +-
 head/lib/libc/stdlib/aligned_alloc.3                              |    126 +
 head/lib/libc/stdlib/malloc.c                                     |     16 +-
 head/lib/libc/stdlib/posix_memalign.3                             |     96 -
 head/lib/libdevstat/devstat.c                                     |     42 +-
 head/lib/libgssapi/gss_display_status.c                           |      6 +-
 head/lib/libgssapi/gss_pseudo_random.c                            |      6 +-
 head/lib/libgssapi/gss_release_oid.c                              |      4 +-
 head/lib/libopie/config.h                                         |      4 +-
 head/lib/libstdthreads/thrd_create.3                              |      6 +-
 head/lib/libutil/Makefile                                         |      5 +-
 head/lib/libutil/flopen.3                                         |      4 +-
 head/lib/libutil/kld.3                                            |      4 +-
 head/lib/libutil/libutil.h                                        |    103 +-
 head/lib/libutil/pidfile.3                                        |     38 +-
 head/lib/libutil/pidfile.c                                        |     18 +-
 head/lib/libvgl/bitmap.c                                          |      4 +-
 head/lib/libvgl/keyboard.c                                        |      4 +-
 head/lib/libvgl/main.c                                            |      4 +-
 head/lib/libvgl/mouse.c                                           |      4 +-
 head/lib/libvgl/simple.c                                          |      4 +-
 head/lib/libvgl/text.c                                            |      4 +-
 head/lib/libvgl/vgl.3                                             |      5 +-
 head/lib/libvgl/vgl.h                                             |      4 +-
 head/lib/msun/src/s_fabsl.c                                       |      4 +-
 head/libexec/bootpd/bootpd.c                                      |      6 +-
 head/libexec/bootpd/bootpgw/bootpgw.c                             |      4 +-
 head/libexec/bootpd/hash.h                                        |      4 +-
 head/libexec/bootpd/tools/bootptest/bootptest.c                   |      4 +-
 head/libexec/bootpd/tools/bootptest/print-bootp.c                 |      4 +-
 head/libexec/ftpd/ftpcmd.y                                        |      4 +-
 head/libexec/getty/init.c                                         |      4 +-
 head/libexec/lukemftpd/nbsd_pidfile.h                             |      4 +-
 head/libexec/rbootd/parseconf.c                                   |      8 +-
 head/libexec/rbootd/rbootd.c                                      |      4 +-
 head/libexec/rbootd/rmpproto.c                                    |      6 +-
 head/libexec/revnetgroup/parse_netgroup.c                         |      6 +-
 head/libexec/rshd/rshd.8                                          |      4 +-
 head/libexec/rtld-aout/shlib.c                                    |      4 +-
 head/libexec/rtld-elf/Symbol.map                                  |      6 +-
 head/libexec/rtld-elf/mips/reloc.c                                |      4 +-
 head/libexec/rtld-elf/rtld.c                                      |    114 +-
 head/libexec/rtld-elf/rtld.h                                      |      4 +-
 head/libexec/rtld-elf/sparc64/reloc.c                             |     12 +-
 head/libexec/tftpd/tftp-io.c                                      |      8 +-
 head/libexec/tftpd/tftp-utils.h                                   |      6 +-
 head/release/doc/en_US.ISO8859-1/hardware/article.sgml            |      5 +-
 head/release/doc/en_US.ISO8859-1/relnotes/article.sgml            |      4 +-
 head/release/doc/share/misc/dev.archlist.txt                      |      3 +-
 head/release/picobsd/tinyware/passwd/local_passwd.c               |      4 +-
 head/release/picobsd/tinyware/passwd/pw_copy.c                    |      4 +-
 head/release/picobsd/tinyware/simple_httpd/README                 |     12 +-
 head/sbin/bsdlabel/bsdlabel.c                                     |     12 +-
 head/sbin/camcontrol/camcontrol.c                                 |      9 +-
 head/sbin/camcontrol/modeedit.c                                   |      4 +-
 head/sbin/devd/devd.cc                                            |      3 +-
 head/sbin/devfs/rule.c                                            |      4 +-
 head/sbin/dhclient/dhclient-script.8                              |      4 +-
 head/sbin/dhclient/options.c                                      |      6 +-
 head/sbin/fsck_ffs/fsck.h                                         |      4 +-
 head/sbin/fsck_ffs/fsutil.c                                       |      4 +-
 head/sbin/fsck_ffs/gjournal.c                                     |      5 +-
 head/sbin/fsirand/fsirand.c                                       |      5 +-
 head/sbin/geom/class/part/geom_part.c                             |      5 +-
 head/sbin/geom/class/part/gpart.8                                 |     25 +-
 head/sbin/geom/class/raid/graid.8                                 |      4 +-
 head/sbin/gvinum/gvinum.c                                         |      9 +-
 head/sbin/hastctl/hastctl.8                                       |      4 +-
 head/sbin/hastctl/hastctl.c                                       |     20 +-
 head/sbin/hastd/activemap.c                                       |      6 +-
 head/sbin/hastd/control.c                                         |     18 +-
 head/sbin/hastd/ebuf.c                                            |      6 +-
 head/sbin/hastd/event.c                                           |     10 +-
 head/sbin/hastd/hast_compression.c                                |      4 +-
 head/sbin/hastd/hast_proto.c                                      |     16 +-
 head/sbin/hastd/hastd.c                                           |     86 +-
 head/sbin/hastd/hooks.c                                           |     12 +-
 head/sbin/hastd/lzf.h                                             |      4 +-
 head/sbin/hastd/metadata.c                                        |     12 +-
 head/sbin/hastd/nv.c                                              |     14 +-
 head/sbin/hastd/primary.c                                         |     82 +-
 head/sbin/hastd/proto.c                                           |     12 +-
 head/sbin/hastd/proto_common.c                                    |      6 +-
 head/sbin/hastd/proto_socketpair.c                                |      4 +-
 head/sbin/hastd/proto_tcp.c                                       |     18 +-
 head/sbin/hastd/proto_uds.c                                       |     14 +-
 head/sbin/hastd/secondary.c                                       |     36 +-
 head/sbin/hastd/subr.c                                            |     12 +-
 head/sbin/hastd/subr.h                                            |      4 +-
 head/sbin/ifconfig/ifieee80211.c                                  |      7 +-
 head/sbin/ipfw/ipfw2.c                                            |     10 +-
 head/sbin/ipfw/main.c                                             |      4 +-
 head/sbin/mdmfs/mdmfs.c                                           |      4 +-
 head/sbin/mount_nfs/mount_nfs.c                                   |      4 +-
 head/sbin/newfs/mkfs.c                                            |      4 +-
 head/sbin/ping6/ping6.c                                           |      7 +-
 head/sbin/reboot/nextboot.8                                       |      4 +-
 head/sbin/recoverdisk/recoverdisk.1                               |     22 +-
 head/sbin/route/route.c                                           |     11 +-
 head/sbin/routed/main.c                                           |      8 +-
 head/sbin/routed/radix.c                                          |      8 +-
 head/sbin/routed/table.c                                          |      6 +-
 head/sbin/tunefs/tunefs.c                                         |      5 +-
 head/share/examples/diskless/README.TEMPLATING                    |      7 +-
 head/share/examples/ppp/ppp.linkdown.sample                       |      4 +-
 head/share/examples/printing/hpvf                                 |      4 +-
 head/share/examples/scsi_target/scsi_cmds.c                       |      4 +-
 head/share/man/man3/ATOMIC_VAR_INIT.3                             |      6 +-
 head/share/man/man3/tgmath.3                                      |      6 +-
 head/share/man/man4/apic.4                                        |      4 +-
 head/share/man/man4/bce.4                                         |      4 +-
 head/share/man/man4/carp.4                                        |      4 +-
 head/share/man/man4/ng_ksocket.4                                  |     12 +-
 head/share/man/man5/ar.5                                          |      6 +-
 head/share/man/man8/rc.subr.8                                     |     20 +-
 head/share/mk/bsd.crunchgen.mk                                    |      7 +-
 head/sys/amd64/conf/GENERIC                                       |      5 +-
 head/sys/arm/conf/KB920X                                          |      5 +-
 head/sys/boot/common/loader.8                                     |     41 +-
 head/sys/boot/forth/loader.4th                                    |     17 +-
 head/sys/cam/cam_periph.c                                         |     81 +-
 head/sys/cam/cam_periph.h                                         |      4 +-
 head/sys/cam/cam_xpt.c                                            |    124 +-
 head/sys/cam/ctl/README.ctl.txt                                   |    449 +
 head/sys/cam/ctl/ctl.c                                            |  13082 ++++++++++
 head/sys/cam/ctl/ctl.h                                            |    216 +
 head/sys/cam/ctl/ctl_backend.c                                    |    177 +
 head/sys/cam/ctl/ctl_backend.h                                    |    288 +
 head/sys/cam/ctl/ctl_backend_block.c                              |   2213 +
 head/sys/cam/ctl/ctl_backend_block.h                              |     72 +
 head/sys/cam/ctl/ctl_backend_ramdisk.c                            |    835 +
 head/sys/cam/ctl/ctl_cmd_table.c                                  |    984 +
 head/sys/cam/ctl/ctl_debug.h                                      |     52 +
 head/sys/cam/ctl/ctl_error.c                                      |    811 +
 head/sys/cam/ctl/ctl_error.h                                      |     92 +
 head/sys/cam/ctl/ctl_frontend.c                                   |    187 +
 head/sys/cam/ctl/ctl_frontend.h                                   |    295 +
 head/sys/cam/ctl/ctl_frontend_cam_sim.c                           |    866 +
 head/sys/cam/ctl/ctl_frontend_internal.c                          |   1782 +
 head/sys/cam/ctl/ctl_frontend_internal.h                          |    154 +
 head/sys/cam/ctl/ctl_ha.h                                         |    270 +
 head/sys/cam/ctl/ctl_io.h                                         |    474 +
 head/sys/cam/ctl/ctl_ioctl.h                                      |    604 +
 head/sys/cam/ctl/ctl_mem_pool.c                                   |    192 +
 head/sys/cam/ctl/ctl_mem_pool.h                                   |     83 +
 head/sys/cam/ctl/ctl_private.h                                    |    493 +
 head/sys/cam/ctl/ctl_scsi_all.c                                   |    227 +
 head/sys/cam/ctl/ctl_scsi_all.h                                   |     52 +
 head/sys/cam/ctl/ctl_ser_table.c                                  |     81 +
 head/sys/cam/ctl/ctl_util.c                                       |    843 +
 head/sys/cam/ctl/ctl_util.h                                       |    119 +
 head/sys/cam/ctl/scsi_ctl.c                                       |   2049 +
 head/sys/cam/scsi/scsi_all.c                                      |     11 +-
 head/sys/cam/scsi/scsi_all.h                                      |     54 +-
 head/sys/cam/scsi/scsi_da.c                                       |     43 +-
 head/sys/cam/scsi/scsi_da.h                                       |     52 +-
 head/sys/cam/scsi/scsi_sg.c                                       |     10 +-
 head/sys/cam/scsi/scsi_targ_bh.c                                  |      4 +-
 head/sys/cddl/contrib/opensolaris/uts/common/fs/zfs/arc.c         |      8 +-
 head/sys/conf/NOTES                                               |      3 +-
 head/sys/conf/files                                               |     52 +-
 head/sys/conf/files.amd64                                         |      3 +-
 head/sys/contrib/dev/acpica/changes.txt                           |     54 +-
 head/sys/contrib/dev/acpica/common/adfile.c                       |      2 +-
 head/sys/contrib/dev/acpica/common/adisasm.c                      |     57 +-
 head/sys/contrib/dev/acpica/common/adwalk.c                       |      2 +-
 head/sys/contrib/dev/acpica/common/dmextern.c                     |      2 +-
 head/sys/contrib/dev/acpica/common/dmrestag.c                     |      2 +-
 head/sys/contrib/dev/acpica/common/dmtable.c                      |      5 +-
 head/sys/contrib/dev/acpica/common/dmtbdump.c                     |      2 +-
 head/sys/contrib/dev/acpica/common/dmtbinfo.c                     |    229 +-
 head/sys/contrib/dev/acpica/common/getopt.c                       |      2 +-
 head/sys/contrib/dev/acpica/compiler/aslanalyze.c                 |      2 +-
 head/sys/contrib/dev/acpica/compiler/aslbtypes.c                  |      2 +-
 head/sys/contrib/dev/acpica/compiler/aslcodegen.c                 |      2 +-
 head/sys/contrib/dev/acpica/compiler/aslcompile.c                 |      2 +-
 head/sys/contrib/dev/acpica/compiler/aslcompiler.h                |      2 +-
 head/sys/contrib/dev/acpica/compiler/aslcompiler.l                |      2 +-
 head/sys/contrib/dev/acpica/compiler/aslcompiler.y                |      2 +-
 head/sys/contrib/dev/acpica/compiler/asldefine.h                  |      2 +-
 head/sys/contrib/dev/acpica/compiler/aslerror.c                   |      2 +-
 head/sys/contrib/dev/acpica/compiler/aslfiles.c                   |      2 +-
 head/sys/contrib/dev/acpica/compiler/aslfold.c                    |      2 +-
 head/sys/contrib/dev/acpica/compiler/aslglobal.h                  |      2 +-
 head/sys/contrib/dev/acpica/compiler/asllength.c                  |      2 +-
 head/sys/contrib/dev/acpica/compiler/asllisting.c                 |      2 +-
 head/sys/contrib/dev/acpica/compiler/aslload.c                    |      2 +-
 head/sys/contrib/dev/acpica/compiler/asllookup.c                  |      2 +-
 head/sys/contrib/dev/acpica/compiler/aslmain.c                    |      6 +-
 head/sys/contrib/dev/acpica/compiler/aslmap.c                     |      2 +-
 head/sys/contrib/dev/acpica/compiler/aslmessages.h                |      2 +-
 head/sys/contrib/dev/acpica/compiler/aslopcodes.c                 |      2 +-
 head/sys/contrib/dev/acpica/compiler/asloperands.c                |      2 +-
 head/sys/contrib/dev/acpica/compiler/aslopt.c                     |      2 +-
 head/sys/contrib/dev/acpica/compiler/aslpredef.c                  |      2 +-
 head/sys/contrib/dev/acpica/compiler/aslresource.c                |      2 +-
 head/sys/contrib/dev/acpica/compiler/aslrestype1.c                |      2 +-
 head/sys/contrib/dev/acpica/compiler/aslrestype1i.c               |      2 +-
 head/sys/contrib/dev/acpica/compiler/aslrestype2.c                |      2 +-
 head/sys/contrib/dev/acpica/compiler/aslrestype2d.c               |      2 +-
 head/sys/contrib/dev/acpica/compiler/aslrestype2e.c               |      2 +-
 head/sys/contrib/dev/acpica/compiler/aslrestype2q.c               |      2 +-
 head/sys/contrib/dev/acpica/compiler/aslrestype2s.c               |      2 +-
 head/sys/contrib/dev/acpica/compiler/aslrestype2w.c               |      2 +-
 head/sys/contrib/dev/acpica/compiler/aslstartup.c                 |      2 +-
 head/sys/contrib/dev/acpica/compiler/aslstubs.c                   |      2 +-
 head/sys/contrib/dev/acpica/compiler/asltransform.c               |      2 +-
 head/sys/contrib/dev/acpica/compiler/asltree.c                    |      2 +-
 head/sys/contrib/dev/acpica/compiler/asltypes.h                   |      2 +-
 head/sys/contrib/dev/acpica/compiler/aslutils.c                   |      2 +-
 head/sys/contrib/dev/acpica/compiler/asluuid.c                    |      2 +-
 head/sys/contrib/dev/acpica/compiler/aslwalks.c                   |      2 +-
 head/sys/contrib/dev/acpica/compiler/dtcompile.c                  |      2 +-
 head/sys/contrib/dev/acpica/compiler/dtcompiler.h                 |      2 +-
 head/sys/contrib/dev/acpica/compiler/dtexpress.c                  |      2 +-
 head/sys/contrib/dev/acpica/compiler/dtfield.c                    |      2 +-
 head/sys/contrib/dev/acpica/compiler/dtio.c                       |      2 +-
 head/sys/contrib/dev/acpica/compiler/dtparser.l                   |      2 +-
 head/sys/contrib/dev/acpica/compiler/dtparser.y                   |      2 +-
 head/sys/contrib/dev/acpica/compiler/dtsubtable.c                 |      2 +-
 head/sys/contrib/dev/acpica/compiler/dttable.c                    |      6 +-
 head/sys/contrib/dev/acpica/compiler/dttemplate.c                 |      2 +-
 head/sys/contrib/dev/acpica/compiler/dttemplate.h                 |     15 +-
 head/sys/contrib/dev/acpica/compiler/dtutils.c                    |      2 +-
 head/sys/contrib/dev/acpica/debugger/dbcmds.c                     |      2 +-
 head/sys/contrib/dev/acpica/debugger/dbdisply.c                   |      2 +-
 head/sys/contrib/dev/acpica/debugger/dbexec.c                     |      2 +-
 head/sys/contrib/dev/acpica/debugger/dbfileio.c                   |      2 +-
 head/sys/contrib/dev/acpica/debugger/dbhistry.c                   |      2 +-
 head/sys/contrib/dev/acpica/debugger/dbinput.c                    |      2 +-
 head/sys/contrib/dev/acpica/debugger/dbmethod.c                   |      2 +-
 head/sys/contrib/dev/acpica/debugger/dbnames.c                    |      2 +-
 head/sys/contrib/dev/acpica/debugger/dbstats.c                    |      2 +-
 head/sys/contrib/dev/acpica/debugger/dbutils.c                    |      2 +-
 head/sys/contrib/dev/acpica/debugger/dbxface.c                    |      2 +-
 head/sys/contrib/dev/acpica/disassembler/dmbuffer.c               |      2 +-
 head/sys/contrib/dev/acpica/disassembler/dmnames.c                |      2 +-
 head/sys/contrib/dev/acpica/disassembler/dmobject.c               |      2 +-
 head/sys/contrib/dev/acpica/disassembler/dmopcode.c               |      2 +-
 head/sys/contrib/dev/acpica/disassembler/dmresrc.c                |      2 +-
 head/sys/contrib/dev/acpica/disassembler/dmresrcl.c               |      2 +-
 head/sys/contrib/dev/acpica/disassembler/dmresrcl2.c              |      2 +-
 head/sys/contrib/dev/acpica/disassembler/dmresrcs.c               |      2 +-
 head/sys/contrib/dev/acpica/disassembler/dmutils.c                |      2 +-
 head/sys/contrib/dev/acpica/disassembler/dmwalk.c                 |      2 +-
 head/sys/contrib/dev/acpica/dispatcher/dsargs.c                   |     10 +-
 head/sys/contrib/dev/acpica/dispatcher/dscontrol.c                |      2 +-
 head/sys/contrib/dev/acpica/dispatcher/dsfield.c                  |      2 +-
 head/sys/contrib/dev/acpica/dispatcher/dsinit.c                   |      2 +-
 head/sys/contrib/dev/acpica/dispatcher/dsmethod.c                 |      2 +-
 head/sys/contrib/dev/acpica/dispatcher/dsmthdat.c                 |      2 +-
 head/sys/contrib/dev/acpica/dispatcher/dsobject.c                 |      2 +-
 head/sys/contrib/dev/acpica/dispatcher/dsopcode.c                 |      2 +-
 head/sys/contrib/dev/acpica/dispatcher/dsutils.c                  |      2 +-
 head/sys/contrib/dev/acpica/dispatcher/dswexec.c                  |      2 +-
 head/sys/contrib/dev/acpica/dispatcher/dswload.c                  |      2 +-
 head/sys/contrib/dev/acpica/dispatcher/dswload2.c                 |      2 +-
 head/sys/contrib/dev/acpica/dispatcher/dswscope.c                 |      2 +-
 head/sys/contrib/dev/acpica/dispatcher/dswstate.c                 |      2 +-
 head/sys/contrib/dev/acpica/events/evevent.c                      |      2 +-
 head/sys/contrib/dev/acpica/events/evglock.c                      |      2 +-
 head/sys/contrib/dev/acpica/events/evgpe.c                        |      2 +-
 head/sys/contrib/dev/acpica/events/evgpeblk.c                     |      2 +-
 head/sys/contrib/dev/acpica/events/evgpeinit.c                    |      2 +-
 head/sys/contrib/dev/acpica/events/evgpeutil.c                    |      2 +-
 head/sys/contrib/dev/acpica/events/evmisc.c                       |      2 +-
 head/sys/contrib/dev/acpica/events/evregion.c                     |      2 +-
 head/sys/contrib/dev/acpica/events/evrgnini.c                     |      2 +-
 head/sys/contrib/dev/acpica/events/evsci.c                        |      2 +-
 head/sys/contrib/dev/acpica/events/evxface.c                      |      2 +-
 head/sys/contrib/dev/acpica/events/evxfevnt.c                     |      2 +-
 head/sys/contrib/dev/acpica/events/evxfgpe.c                      |      2 +-
 head/sys/contrib/dev/acpica/events/evxfregn.c                     |      2 +-
 head/sys/contrib/dev/acpica/executer/exconfig.c                   |      2 +-
 head/sys/contrib/dev/acpica/executer/exconvrt.c                   |      2 +-
 head/sys/contrib/dev/acpica/executer/excreate.c                   |      2 +-
 head/sys/contrib/dev/acpica/executer/exdebug.c                    |      2 +-
 head/sys/contrib/dev/acpica/executer/exdump.c                     |      2 +-
 head/sys/contrib/dev/acpica/executer/exfield.c                    |      2 +-
 head/sys/contrib/dev/acpica/executer/exfldio.c                    |      2 +-
 head/sys/contrib/dev/acpica/executer/exmisc.c                     |      2 +-
 head/sys/contrib/dev/acpica/executer/exmutex.c                    |      2 +-
 head/sys/contrib/dev/acpica/executer/exnames.c                    |      2 +-
 head/sys/contrib/dev/acpica/executer/exoparg1.c                   |      2 +-
 head/sys/contrib/dev/acpica/executer/exoparg2.c                   |      2 +-
 head/sys/contrib/dev/acpica/executer/exoparg3.c                   |      2 +-
 head/sys/contrib/dev/acpica/executer/exoparg6.c                   |      2 +-
 head/sys/contrib/dev/acpica/executer/exprep.c                     |      2 +-
 head/sys/contrib/dev/acpica/executer/exregion.c                   |      2 +-
 head/sys/contrib/dev/acpica/executer/exresnte.c                   |      2 +-
 head/sys/contrib/dev/acpica/executer/exresolv.c                   |      2 +-
 head/sys/contrib/dev/acpica/executer/exresop.c                    |      2 +-
 head/sys/contrib/dev/acpica/executer/exstore.c                    |      2 +-
 head/sys/contrib/dev/acpica/executer/exstoren.c                   |      2 +-
 head/sys/contrib/dev/acpica/executer/exstorob.c                   |      2 +-
 head/sys/contrib/dev/acpica/executer/exsystem.c                   |      2 +-
 head/sys/contrib/dev/acpica/executer/exutils.c                    |      2 +-
 head/sys/contrib/dev/acpica/hardware/hwacpi.c                     |      2 +-
 head/sys/contrib/dev/acpica/hardware/hwgpe.c                      |      2 +-
 head/sys/contrib/dev/acpica/hardware/hwpci.c                      |      2 +-
 head/sys/contrib/dev/acpica/hardware/hwregs.c                     |      2 +-
 head/sys/contrib/dev/acpica/hardware/hwsleep.c                    |      2 +-
 head/sys/contrib/dev/acpica/hardware/hwtimer.c                    |      2 +-
 head/sys/contrib/dev/acpica/hardware/hwvalid.c                    |      2 +-
 head/sys/contrib/dev/acpica/hardware/hwxface.c                    |      2 +-
 head/sys/contrib/dev/acpica/include/acapps.h                      |      4 +-
 head/sys/contrib/dev/acpica/include/accommon.h                    |      2 +-
 head/sys/contrib/dev/acpica/include/acconfig.h                    |      6 +-
 head/sys/contrib/dev/acpica/include/acdebug.h                     |      2 +-
 head/sys/contrib/dev/acpica/include/acdisasm.h                    |      4 +-
 head/sys/contrib/dev/acpica/include/acdispat.h                    |      2 +-
 head/sys/contrib/dev/acpica/include/acevents.h                    |      2 +-
 head/sys/contrib/dev/acpica/include/acexcep.h                     |      2 +-
 head/sys/contrib/dev/acpica/include/acglobal.h                    |      3 +-
 head/sys/contrib/dev/acpica/include/achware.h                     |      2 +-
 head/sys/contrib/dev/acpica/include/acinterp.h                    |      2 +-
 head/sys/contrib/dev/acpica/include/aclocal.h                     |     13 +-
 head/sys/contrib/dev/acpica/include/acmacros.h                    |      2 +-
 head/sys/contrib/dev/acpica/include/acnames.h                     |      2 +-
 head/sys/contrib/dev/acpica/include/acnamesp.h                    |      2 +-
 head/sys/contrib/dev/acpica/include/acobject.h                    |      2 +-
 head/sys/contrib/dev/acpica/include/acopcode.h                    |      2 +-
 head/sys/contrib/dev/acpica/include/acoutput.h                    |      2 +-
 head/sys/contrib/dev/acpica/include/acparser.h                    |      2 +-
 head/sys/contrib/dev/acpica/include/acpi.h                        |      2 +-
 head/sys/contrib/dev/acpica/include/acpiosxf.h                    |      2 +-
 head/sys/contrib/dev/acpica/include/acpixf.h                      |     11 +-
 head/sys/contrib/dev/acpica/include/acpredef.h                    |      2 +-
 head/sys/contrib/dev/acpica/include/acresrc.h                     |      2 +-
 head/sys/contrib/dev/acpica/include/acrestyp.h                    |      2 +-
 head/sys/contrib/dev/acpica/include/acstruct.h                    |      2 +-
 head/sys/contrib/dev/acpica/include/actables.h                    |      2 +-
 head/sys/contrib/dev/acpica/include/actbl.h                       |     16 +-
 head/sys/contrib/dev/acpica/include/actbl1.h                      |      2 +-
 head/sys/contrib/dev/acpica/include/actbl2.h                      |      2 +-
 head/sys/contrib/dev/acpica/include/actbl3.h                      |      2 +-
 head/sys/contrib/dev/acpica/include/actypes.h                     |      2 +-
 head/sys/contrib/dev/acpica/include/acutils.h                     |     33 +-
 head/sys/contrib/dev/acpica/include/amlcode.h                     |      2 +-
 head/sys/contrib/dev/acpica/include/amlresrc.h                    |      2 +-
 head/sys/contrib/dev/acpica/include/platform/acenv.h              |      2 +-
 head/sys/contrib/dev/acpica/include/platform/acfreebsd.h          |      2 +-
 head/sys/contrib/dev/acpica/include/platform/acgcc.h              |      2 +-
 head/sys/contrib/dev/acpica/namespace/nsaccess.c                  |      2 +-
 head/sys/contrib/dev/acpica/namespace/nsalloc.c                   |      2 +-
 head/sys/contrib/dev/acpica/namespace/nsdump.c                    |      2 +-
 head/sys/contrib/dev/acpica/namespace/nsdumpdv.c                  |      2 +-
 head/sys/contrib/dev/acpica/namespace/nseval.c                    |      2 +-
 head/sys/contrib/dev/acpica/namespace/nsinit.c                    |      2 +-
 head/sys/contrib/dev/acpica/namespace/nsload.c                    |      2 +-
 head/sys/contrib/dev/acpica/namespace/nsnames.c                   |      2 +-
 head/sys/contrib/dev/acpica/namespace/nsobject.c                  |      2 +-
 head/sys/contrib/dev/acpica/namespace/nsparse.c                   |      2 +-
 head/sys/contrib/dev/acpica/namespace/nspredef.c                  |      2 +-
 head/sys/contrib/dev/acpica/namespace/nsrepair.c                  |      2 +-
 head/sys/contrib/dev/acpica/namespace/nsrepair2.c                 |      2 +-
 head/sys/contrib/dev/acpica/namespace/nssearch.c                  |      2 +-
 head/sys/contrib/dev/acpica/namespace/nsutils.c                   |      2 +-
 head/sys/contrib/dev/acpica/namespace/nswalk.c                    |      2 +-
 head/sys/contrib/dev/acpica/namespace/nsxfeval.c                  |      2 +-
 head/sys/contrib/dev/acpica/namespace/nsxfname.c                  |      2 +-
 head/sys/contrib/dev/acpica/namespace/nsxfobj.c                   |      2 +-
 head/sys/contrib/dev/acpica/os_specific/service_layers/osunixxf.c |      2 +-
 head/sys/contrib/dev/acpica/parser/psargs.c                       |      2 +-
 head/sys/contrib/dev/acpica/parser/psloop.c                       |      2 +-
 head/sys/contrib/dev/acpica/parser/psopcode.c                     |      2 +-
 head/sys/contrib/dev/acpica/parser/psparse.c                      |      2 +-
 head/sys/contrib/dev/acpica/parser/psscope.c                      |      2 +-
 head/sys/contrib/dev/acpica/parser/pstree.c                       |      2 +-
 head/sys/contrib/dev/acpica/parser/psutils.c                      |      2 +-
 head/sys/contrib/dev/acpica/parser/pswalk.c                       |      2 +-
 head/sys/contrib/dev/acpica/parser/psxface.c                      |      2 +-
 head/sys/contrib/dev/acpica/resources/rsaddr.c                    |      2 +-
 head/sys/contrib/dev/acpica/resources/rscalc.c                    |      2 +-
 head/sys/contrib/dev/acpica/resources/rscreate.c                  |      2 +-
 head/sys/contrib/dev/acpica/resources/rsdump.c                    |      2 +-
 head/sys/contrib/dev/acpica/resources/rsinfo.c                    |      2 +-
 head/sys/contrib/dev/acpica/resources/rsio.c                      |      2 +-
 head/sys/contrib/dev/acpica/resources/rsirq.c                     |      2 +-
 head/sys/contrib/dev/acpica/resources/rslist.c                    |      2 +-
 head/sys/contrib/dev/acpica/resources/rsmemory.c                  |      2 +-
 head/sys/contrib/dev/acpica/resources/rsmisc.c                    |      2 +-
 head/sys/contrib/dev/acpica/resources/rsserial.c                  |      2 +-
 head/sys/contrib/dev/acpica/resources/rsutils.c                   |      2 +-
 head/sys/contrib/dev/acpica/resources/rsxface.c                   |      2 +-
 head/sys/contrib/dev/acpica/tables/tbfadt.c                       |     15 +-
 head/sys/contrib/dev/acpica/tables/tbfind.c                       |      2 +-
 head/sys/contrib/dev/acpica/tables/tbinstal.c                     |      2 +-
 head/sys/contrib/dev/acpica/tables/tbutils.c                      |      2 +-
 head/sys/contrib/dev/acpica/tables/tbxface.c                      |      2 +-
 head/sys/contrib/dev/acpica/tables/tbxfroot.c                     |      2 +-
 head/sys/contrib/dev/acpica/utilities/utaddress.c                 |    322 +
 head/sys/contrib/dev/acpica/utilities/utalloc.c                   |      2 +-
 head/sys/contrib/dev/acpica/utilities/utcache.c                   |      2 +-
 head/sys/contrib/dev/acpica/utilities/utcopy.c                    |      2 +-
 head/sys/contrib/dev/acpica/utilities/utdebug.c                   |      2 +-
 head/sys/contrib/dev/acpica/utilities/utdecode.c                  |      2 +-
 head/sys/contrib/dev/acpica/utilities/utdelete.c                  |     12 +-
 head/sys/contrib/dev/acpica/utilities/uteval.c                    |      2 +-
 head/sys/contrib/dev/acpica/utilities/utglobal.c                  |      9 +-
 head/sys/contrib/dev/acpica/utilities/utids.c                     |      2 +-
 head/sys/contrib/dev/acpica/utilities/utinit.c                    |      3 +-
 head/sys/contrib/dev/acpica/utilities/utlock.c                    |      2 +-
 head/sys/contrib/dev/acpica/utilities/utmath.c                    |      2 +-
 head/sys/contrib/dev/acpica/utilities/utmisc.c                    |     40 +-
 head/sys/contrib/dev/acpica/utilities/utmutex.c                   |      2 +-
 head/sys/contrib/dev/acpica/utilities/utobject.c                  |      2 +-
 head/sys/contrib/dev/acpica/utilities/utosi.c                     |      2 +-
 head/sys/contrib/dev/acpica/utilities/utresrc.c                   |      2 +-
 head/sys/contrib/dev/acpica/utilities/utstate.c                   |      2 +-
 head/sys/contrib/dev/acpica/utilities/uttrack.c                   |      2 +-
 head/sys/contrib/dev/acpica/utilities/utxface.c                   |     47 +-
 head/sys/contrib/dev/acpica/utilities/utxferror.c                 |      2 +-
 head/sys/contrib/dev/acpica/utilities/utxfmutex.c                 |      2 +-
 head/sys/contrib/pf/net/if_pfsync.c                               |    438 +-
 head/sys/contrib/pf/net/pf.c                                      |     20 +-
 head/sys/contrib/pf/net/pf_ioctl.c                                |     97 +-
 head/sys/contrib/pf/net/pfvar.h                                   |     11 +-
 head/sys/contrib/rdma/ib_addr.h                                   |      4 +-
 head/sys/dev/ahci/ahci.c                                          |      3 +-
 head/sys/dev/an/if_an.c                                           |      3 +-
 head/sys/dev/ata/ata-pci.h                                        |      3 +-
 head/sys/dev/ata/atapi-cam.c                                      |      7 +-
 head/sys/dev/ata/chipsets/ata-intel.c                             |      3 +-
 head/sys/dev/ath/ath_hal/ah_desc.h                                |     15 +-
 head/sys/dev/ath/ath_hal/ah_internal.h                            |      6 +-
 head/sys/dev/ath/if_ath.c                                         |      9 +-
 head/sys/dev/ath/if_ath_tx.c                                      |    110 +-
 head/sys/dev/bfe/if_bfe.c                                         |      3 +-
 head/sys/dev/bm/if_bm.c                                           |      3 +-
 head/sys/dev/bxe/if_bxe.c                                         |     11 +-
 head/sys/dev/ciss/ciss.c                                          |      4 +-
 head/sys/dev/e1000/if_em.c                                        |     38 +-
 head/sys/dev/e1000/if_igb.c                                       |     14 +-
 head/sys/dev/e1000/if_lem.c                                       |     24 +-
 head/sys/dev/ep/if_ep.c                                           |      3 +-
 head/sys/dev/et/if_et.c                                           |    116 +-
 head/sys/dev/ex/if_ex.c                                           |      3 +-
 head/sys/dev/firewire/if_fwe.c                                    |      3 +-
 head/sys/dev/ichwd/ichwd.c                                        |     32 +-
 head/sys/dev/ichwd/ichwd.h                                        |      8 +-
 head/sys/dev/ie/if_ie.c                                           |      3 +-
 head/sys/dev/if_ndis/if_ndis.c                                    |      3 +-
 head/sys/dev/ixgb/if_ixgb.c                                       |      3 +-
 head/sys/dev/ixgbe/ixgbe.c                                        |     15 +-
 head/sys/dev/ixgbe/ixv.c                                          |      3 +-
 head/sys/dev/lge/if_lge.c                                         |      3 +-
 head/sys/dev/msk/if_msk.c                                         |      3 +-
 head/sys/dev/my/if_my.c                                           |      3 +-
 head/sys/dev/netmap/if_em_netmap.h                                |     18 +-
 head/sys/dev/netmap/if_igb_netmap.h                               |     18 +-
 head/sys/dev/netmap/if_lem_netmap.h                               |     18 +-
 head/sys/dev/netmap/if_re_netmap.h                                |     31 +-
 head/sys/dev/netmap/ixgbe_netmap.h                                |     20 +-
 head/sys/dev/netmap/netmap.c                                      |     76 +-
 head/sys/dev/netmap/netmap_kern.h                                 |     28 +-
 head/sys/dev/nfe/if_nfe.c                                         |      3 +-
 head/sys/dev/null/null.c                                          |     50 +-
 head/sys/dev/nve/if_nve.c                                         |      3 +-
 head/sys/dev/pst/pst-pci.c                                        |      4 +-
 head/sys/dev/qlxgb/qla_os.c                                       |      7 +-
 head/sys/dev/random/probe.c                                       |      6 +-
 head/sys/dev/rt/if_rt.c                                           |      3 +-
 head/sys/dev/sfxge/sfxge_port.c                                   |     10 +-
 head/sys/dev/sis/if_sis.c                                         |      3 +-
 head/sys/dev/sk/if_sk.c                                           |      5 +-
 head/sys/dev/sn/if_sn.c                                           |      3 +-
 head/sys/dev/snc/dp83932.c                                        |      3 +-
 head/sys/dev/sound/pci/emu10k1.c                                  |      6 +-
 head/sys/dev/sound/pci/emu10kx-midi.c                             |     32 +-
 head/sys/dev/sound/pci/emu10kx-pcm.c                              |    100 +-
 head/sys/dev/sound/pci/emu10kx.c                                  |    467 +-
 head/sys/dev/sound/pci/emu10kx.h                                  |     10 +-
 head/sys/dev/sound/pci/emuxkireg.h                                |     10 +-
 head/sys/dev/stge/if_stge.c                                       |      3 +-
 head/sys/dev/tl/if_tl.c                                           |      3 +-
 head/sys/dev/tsec/if_tsec.c                                       |      3 +-
 head/sys/dev/uart/uart_bus_pci.c                                  |      4 +-
 head/sys/dev/usb/net/usb_ethernet.c                               |      5 +-
 head/sys/dev/vx/if_vx.c                                           |      3 +-
 head/sys/dev/vxge/vxge.c                                          |      3 +-
 head/sys/dev/wb/if_wb.c                                           |      3 +-
 head/sys/dev/wtap/if_medium.c                                     |    120 +
 head/sys/dev/wtap/if_medium.h                                     |     72 +
 head/sys/dev/wtap/if_wtap.c                                       |    904 +
 head/sys/dev/wtap/if_wtap_module.c                                |    186 +
 head/sys/dev/wtap/if_wtapioctl.h                                  |    180 +
 head/sys/dev/wtap/if_wtapvar.h                                    |    163 +
 head/sys/dev/wtap/plugins/visibility.c                            |    241 +
 head/sys/dev/wtap/plugins/visibility.h                            |     56 +
 head/sys/dev/wtap/plugins/visibility_ioctl.h                      |     51 +
 head/sys/dev/wtap/plugins/wtap_plugin.h                           |     48 +
 head/sys/dev/wtap/wtap_hal/hal.c                                  |    213 +
 head/sys/dev/wtap/wtap_hal/hal.h                                  |     55 +
 head/sys/dev/wtap/wtap_hal/handler.h                              |     50 +
 head/sys/dev/xen/netback/netback.c                                |      3 +-
 head/sys/dev/xen/netfront/netfront.c                              |      3 +-
 head/sys/fs/nfs/nfs_commonkrpc.c                                  |      3 +-
 head/sys/fs/nfs/nfs_commonport.c                                  |      3 +-
 head/sys/fs/nfs/nfs_commonsubs.c                                  |      4 +-
 head/sys/fs/nfsclient/nfs_clkrpc.c                                |      3 +-
 head/sys/fs/nfsclient/nfs_clport.c                                |      3 +-
 head/sys/fs/nfsclient/nfs_clrpcops.c                              |      4 +-
 head/sys/fs/nullfs/null_subr.c                                    |      4 +-
 head/sys/fs/pseudofs/pseudofs_vnops.c                             |     26 +-
 head/sys/fs/tmpfs/tmpfs_subr.c                                    |     80 +-
 head/sys/geom/raid/md_intel.c                                     |    173 +-
 head/sys/gnu/dev/sound/pci/emu10k1-alsa.h                         |   1601 -
 head/sys/gnu/dev/sound/pci/p16v-alsa.h                            |    301 -
 head/sys/gnu/dev/sound/pci/p17v-alsa.h                            |    113 -
 head/sys/i386/conf/GENERIC                                        |      5 +-
 head/sys/i386/conf/PAE                                            |      5 +-
 head/sys/i386/conf/XEN                                            |      6 +-
 head/sys/ia64/conf/GENERIC                                        |     14 +-
 head/sys/kern/kern_prot.c                                         |      4 +-
 head/sys/kern/kern_resource.c                                     |      4 +-
 head/sys/kern/kern_shutdown.c                                     |      4 +-
 head/sys/kern/kern_uuid.c                                         |      8 +-
 head/sys/kern/subr_witness.c                                      |      6 +-
 head/sys/kern/uipc_shm.c                                          |    103 +-
 head/sys/kern/vfs_subr.c                                          |      6 +-
 head/sys/kern/vfs_syscalls.c                                      |     19 +-
 head/sys/kern/vnode_if.src                                        |     29 +-
 head/sys/mips/atheros/ar724x_pci.c                                |      4 +-
 head/sys/mips/cavium/cryptocteon/cavium_crypto.c                  |     88 +-
 head/sys/mips/cavium/files.octeon1                                |      4 +-
 head/sys/mips/cavium/octeon_cop2.S                                |    225 +
 head/sys/mips/cavium/octeon_cop2.h                                |    210 +
 head/sys/mips/include/cpuregs.h                                   |      3 +-
 head/sys/mips/include/frame.h                                     |      7 +-
 head/sys/mips/include/octeon_cop2.h                               |    215 +
 head/sys/mips/include/proc.h                                      |     14 +-
 head/sys/mips/mips/db_disasm.c                                    |      3 +-
 head/sys/mips/mips/elf_machdep.c                                  |    101 +-
 head/sys/mips/mips/exception.S                                    |     16 +-
 head/sys/mips/mips/genassym.c                                     |     73 +-
 head/sys/mips/mips/locore.S                                       |      6 +-
 head/sys/mips/mips/octeon_cop2.c                                  |     62 +
 head/sys/mips/mips/octeon_cop2_swtch.S                            |    246 +
 head/sys/mips/mips/pm_machdep.c                                   |      4 +-
 head/sys/mips/mips/swtch.S                                        |     57 +-
 head/sys/mips/mips/trap.c                                         |     85 +-
 head/sys/mips/mips/vm_machdep.c                                   |     51 +-
 head/sys/modules/acpi/acpi/Makefile                               |      9 +-
 head/sys/modules/random/Makefile                                  |      4 +-
 head/sys/modules/sound/driver/emu10kx/Makefile                    |     27 +-
 head/sys/modules/wtap/Makefile                                    |     30 +
 head/sys/net/bpf.c                                                |     57 +-
 head/sys/net/if.c                                                 |    108 +-
 head/sys/net/if_llatbl.c                                          |      5 +-
 head/sys/net/if_media.h                                           |     10 +-
 head/sys/net/if_var.h                                             |     23 +-
 head/sys/net/if_vlan.c                                            |      9 +-
 head/sys/net/rtsock.c                                             |     23 +-
 head/sys/net80211/ieee80211_ioctl.c                               |     13 +-
 head/sys/net80211/ieee80211_output.c                              |      4 +-
 head/sys/net80211/ieee80211_radiotap.c                            |      6 +-
 head/sys/net80211/ieee80211_radiotap.h                            |      4 +-
 head/sys/netatalk/aarp.c                                          |      8 +-
 head/sys/netatalk/at_control.c                                    |     10 +-
 head/sys/netinet/if_ether.c                                       |     39 +-
 head/sys/netinet/if_ether.h                                       |      4 +-
 head/sys/netinet/igmp.c                                           |     26 +-
 head/sys/netinet/in.c                                             |     24 +-
 head/sys/netinet/in_mcast.c                                       |     16 +-
 head/sys/netinet/in_pcb.c                                         |     20 +-
 head/sys/netinet/in_proto.c                                       |     22 +-
 head/sys/netinet/in_var.h                                         |      7 +-
 head/sys/netinet/ip_carp.c                                        |     26 +-
 head/sys/netinet/ip_icmp.c                                        |     22 +-
 head/sys/netinet/ip_input.c                                       |     10 +-
 head/sys/netinet/sctp_bsd_addr.c                                  |      6 +-
 head/sys/netinet/sctp_input.c                                     |    257 +-
 head/sys/netinet/sctp_sysctl.c                                    |     14 +-
 head/sys/netinet/sctp_sysctl.h                                    |     15 +-
 head/sys/netinet/sctputil.c                                       |     15 +-
 head/sys/netinet/tcp_input.c                                      |      5 +-
 head/sys/netinet/tcp_timewait.c                                   |     10 +-
 head/sys/netinet/tcp_usrreq.c                                     |      6 +-
 head/sys/netinet6/icmp6.c                                         |     12 +-
 head/sys/netinet6/in6.c                                           |     42 +-
 head/sys/netinet6/in6_ifattach.c                                  |     34 +-
 head/sys/netinet6/in6_mcast.c                                     |     16 +-
 head/sys/netinet6/in6_var.h                                       |      6 +-
 head/sys/netinet6/ip6_input.c                                     |      6 +-
 head/sys/netinet6/mld6.c                                          |     34 +-
 head/sys/netinet6/nd6.c                                           |     18 +-
 head/sys/netinet6/nd6_rtr.c                                       |     14 +-
 head/sys/netinet6/sctp6_usrreq.c                                  |     11 +-
 head/sys/netipx/ipx.c                                             |     10 +-
 head/sys/pc98/conf/GENERIC                                        |      6 +-
 head/sys/powerpc/conf/GENERIC                                     |      6 +-
 head/sys/powerpc/conf/GENERIC64                                   |      6 +-
 head/sys/powerpc/powerpc/busdma_machdep.c                         |      5 +-
 head/sys/sparc64/conf/GENERIC                                     |      3 +-
 head/sys/ufs/ufs/ufs_quota.c                                      |     23 +-
 head/sys/vm/vm_object.h                                           |      4 +-
 head/tools/bsdbox/Makefile                                        |    115 +
 head/tools/bsdbox/Makefile.base                                   |     55 +
 head/tools/bsdbox/Makefile.fs                                     |     17 +
 head/tools/bsdbox/Makefile.hostapd                                |     15 +
 head/tools/bsdbox/Makefile.kld                                    |      6 +
 head/tools/bsdbox/Makefile.login                                  |      9 +
 head/tools/bsdbox/Makefile.net                                    |     32 +
 head/tools/bsdbox/Makefile.telnetd                                |     14 +
 head/tools/bsdbox/Makefile.textproc                               |     20 +
 head/tools/bsdbox/README                                          |     12 +
 head/tools/regression/bin/sh/builtins/getopts1.0                  |      8 +-
 head/tools/regression/msdosfs/msdosfstest-3.sh                    |      8 +-
 head/tools/regression/msdosfs/msdosfstest-4.sh                    |     10 +-
 head/tools/regression/msdosfs/msdosfstest-5.sh                    |     14 +-
 head/tools/regression/msdosfs/msdosfstest-6.sh                    |     11 +-
 head/tools/tools/nanobsd/pcengines/Files/root/.cshrc              |      4 +-
 head/tools/tools/wtap/wtap/Makefile                               |      9 +
 head/tools/tools/wtap/wtap/wtap.c                                 |     82 +
 head/usr.bin/Makefile                                             |      3 +-
 head/usr.bin/brandelf/brandelf.c                                  |      6 +-
 head/usr.bin/bsdiff/bsdiff/bsdiff.c                               |      4 +-
 head/usr.bin/ctlstat/Makefile                                     |      8 +
 head/usr.bin/ctlstat/ctlstat.8                                    |    122 +
 head/usr.bin/ctlstat/ctlstat.c                                    |    730 +
 head/usr.bin/hexdump/parse.c                                      |     33 +-
 head/usr.bin/locate/locate/util.c                                 |      8 +-
 head/usr.bin/mail/main.c                                          |      4 +-
 head/usr.bin/mail/util.c                                          |      4 +-
 head/usr.bin/ncplist/ncplist.c                                    |      9 +-
 head/usr.bin/netstat/if.c                                         |      4 +-
 head/usr.bin/newgrp/newgrp.c                                      |     12 +-
 head/usr.bin/split/split.c                                        |      5 +-
 head/usr.sbin/Makefile                                            |      3 +-
 head/usr.sbin/acpi/acpidb/Makefile                                |     11 +-
 head/usr.sbin/acpi/iasl/Makefile                                  |     10 +-
 head/usr.sbin/bluetooth/sdpd/server.c                             |      4 +-
 head/usr.sbin/bsnmpd/modules/snmp_wlan/BEGEMOT-WIRELESS-MIB.txt   |      5 +-
 head/usr.sbin/bsnmpd/modules/snmp_wlan/wlan_tree.def              |      6 +-
 head/usr.sbin/bsnmpd/tools/bsnmptools/bsnmpget.1                  |     21 +-
 head/usr.sbin/bsnmpd/tools/bsnmptools/bsnmpget.c                  |     50 +-
 head/usr.sbin/bsnmpd/tools/libbsnmptools/bsnmptools.c             |     19 +-
 head/usr.sbin/bsnmpd/tools/libbsnmptools/bsnmptools.h             |      6 +-
 head/usr.sbin/ctladm/Makefile                                     |     21 +
 head/usr.sbin/ctladm/ctladm.8                                     |    963 +
 head/usr.sbin/ctladm/ctladm.c                                     |   4005 +++
 head/usr.sbin/ctladm/ctladm.h                                     |     50 +
 head/usr.sbin/ctladm/util.c                                       |    156 +
 head/usr.sbin/daemon/daemon.c                                     |      8 +-
 head/usr.sbin/mlxcontrol/interface.c                              |      4 +-
 head/usr.sbin/newsyslog/newsyslog.c                               |     18 +-
 head/usr.sbin/pkg_install/add/main.c                              |      4 +-
 head/usr.sbin/pkg_install/lib/plist.c                             |      4 +-
 head/usr.sbin/tzsetup/tzsetup.c                                   |     36 +-
 692 files changed, 42765 insertions(+), 5275 deletions(-)

diffs (63828 lines):

diff -r ebe87ce4d7dd -r 1750de5b0399 head/Makefile.inc1
--- a/head/Makefile.inc1	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/Makefile.inc1	Fri Jan 13 15:51:01 2012 +0200
@@ -1,5 +1,5 @@
 #
-# $FreeBSD: head/Makefile.inc1 228801 2011-12-22 15:03:48Z dim $
+# $FreeBSD: head/Makefile.inc1 229673 2012-01-06 00:34:27Z adrian $
 #
 # Make command line options:
 #	-DNO_CLEANDIR run ${MAKE} clean, instead of ${MAKE} cleandir
@@ -15,6 +15,8 @@
 #	-DNO_WWWUPDATE do not update www in ${MAKE} update
 #	-DNO_CTF do not run the DTrace CTF conversion tools on built objects
 #	LOCAL_DIRS="list of dirs" to add additional dirs to the SUBDIR list
+#	LOCAL_TOOL_DIRS="list of dirs" to add additional dirs to the build-tools
+#	    list
 #	TARGET="machine" to crossbuild world for a different machine type
 #	TARGET_ARCH= may be required when a TARGET supports multiple endians
 
@@ -105,6 +107,8 @@
 CLEANDIR=	cleandir
 .endif
 
+LOCAL_TOOL_DIRS?=
+
 CVS?=		cvs
 CVSFLAGS?=	-A -P -d -I!
 SVN?=		svn
@@ -1118,6 +1122,7 @@
     bin/csh \
     bin/sh \
     ${_rescue} \
+    ${LOCAL_TOOL_DIRS} \
     lib/ncurses/ncurses \
     lib/ncurses/ncursesw \
     ${_share} \
diff -r ebe87ce4d7dd -r 1750de5b0399 head/UPDATING
--- a/head/UPDATING	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/UPDATING	Fri Jan 13 15:51:01 2012 +0200
@@ -22,6 +22,13 @@
 	machines to maximize performance.  (To disable malloc debugging, run
 	ln -s aj /etc/malloc.conf.)
 
+20120109:
+	panic(9) now stops other CPUs in the SMP systems, disables interrupts
+	on the current CPU and prevents other threads from running.
+	This behavior can be reverted using the kern.stop_scheduler_on_panic
+	tunable/sysctl.
+	The new behavior can be incompatible with kern.sync_on_panic.
+
 20111215:
 	The carp(4) facility has been changed significantly. Configuration
 	of the CARP protocol via ifconfig(8) has changed, as well as format
@@ -1534,4 +1541,4 @@
 Contact Warner Losh if you have any questions about your use of
 this document.
 
-$FreeBSD: head/UPDATING 228571 2011-12-16 12:16:56Z glebius $
+$FreeBSD: head/UPDATING 229854 2012-01-09 12:06:09Z avg $
diff -r ebe87ce4d7dd -r 1750de5b0399 head/bin/ps/ps.c
--- a/head/bin/ps/ps.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/bin/ps/ps.c	Fri Jan 13 15:51:01 2012 +0200
@@ -47,7 +47,7 @@
 #endif
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/bin/ps/ps.c 227840 2011-11-22 21:14:46Z trociny $");
+__FBSDID("$FreeBSD: head/bin/ps/ps.c 229782 2012-01-07 16:10:23Z uqs $");
 
 #include <sys/param.h>
 #include <sys/proc.h>
@@ -625,7 +625,7 @@
 
 			ks = STAILQ_FIRST(&kinfo[i].ki_ks);
 			STAILQ_REMOVE_HEAD(&kinfo[i].ki_ks, ks_next);
-			/* Truncate rightmost column if neccessary.  */
+			/* Truncate rightmost column if necessary.  */
 			if (STAILQ_NEXT(vent, next_ve) == NULL &&
 			   termwidth != UNLIMITED && ks->ks_str != NULL) {
 				left = termwidth - linelen;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/contrib/gcc/config/freebsd.h
--- a/head/contrib/gcc/config/freebsd.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/contrib/gcc/config/freebsd.h	Fri Jan 13 15:51:01 2012 +0200
@@ -26,7 +26,7 @@
    Further work by David O'Brien <obrien at FreeBSD.org> and
    Loren J. Rittle <ljrittle at acm.org>.  */
 
-/* $FreeBSD$ */
+/* $FreeBSD: head/contrib/gcc/config/freebsd.h 229715 2012-01-06 18:37:49Z andreast $ */
 
 /* In case we need to know.  */
 #define USING_CONFIG_FREEBSD 1
@@ -63,6 +63,7 @@
 /* All FreeBSD Architectures support the ELF object file format.  */
 #undef  OBJECT_FORMAT_ELF
 #define OBJECT_FORMAT_ELF	1
+#undef  TARGET_ELF
 #define TARGET_ELF		1
 
 /* Don't assume anything about the header files.  */
diff -r ebe87ce4d7dd -r 1750de5b0399 head/contrib/groff/tmac/doc-syms
--- a/head/contrib/groff/tmac/doc-syms	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/contrib/groff/tmac/doc-syms	Fri Jan 13 15:51:01 2012 +0200
@@ -605,6 +605,8 @@
 .ds doc-str-St--isoC           \*[doc-Tn-font-size]ISO/IEC\*[doc-str-St] 9899:1990
 .as doc-str-St--isoC           " (\*[Lq]\*[doc-Tn-font-size]ISO\~C\^90\*[doc-str-St]\*[Rq])
 .als doc-str-St--isoC-90 doc-str-St--isoC
+.ds doc-str-St--isoC-2011      \*[doc-Tn-font-size]ISO/IEC\*[doc-str-St] 9899:2011
+.as doc-str-St--isoC-2011      " (\*[Lq]\*[doc-Tn-font-size]ISO\~C\^11\*[doc-str-St]\*[Rq])
 .ds doc-str-St--isoC-99        \*[doc-Tn-font-size]ISO/IEC\*[doc-str-St] 9899:1999
 .as doc-str-St--isoC-99        " (\*[Lq]\*[doc-Tn-font-size]ISO\~C\^99\*[doc-str-St]\*[Rq])
 .ds doc-str-St--isoC-amd1       \*[doc-Tn-font-size]ISO/IEC\*[doc-str-St] 9899/AMD1:1995
@@ -613,8 +615,6 @@
 .as doc-str-St--isoC-tcor1      " (\*[Lq]\*[doc-Tn-font-size]ISO\~C\^90\*[doc-str-St], Technical Corrigendum 1\*[Rq])
 .ds doc-str-St--isoC-tcor2      \*[doc-Tn-font-size]ISO/IEC\*[doc-str-St] 9899/TCOR2:1995
 .as doc-str-St--isoC-tcor2      " (\*[Lq]\*[doc-Tn-font-size]ISO\~C\^90\*[doc-str-St], Technical Corrigendum 2\*[Rq])
-.ds doc-str-St--isoC-11        \*[doc-Tn-font-size]ISO/IEC\*[doc-str-St] 9899:2011
-.as doc-str-St--isoC-11        " (\*[Lq]\*[doc-Tn-font-size]ISO\~C\^11\*[doc-str-St]\*[Rq])
 .
 .\" POSIX Part 1: System API
 .ds doc-str-St--p1003.1        \*[doc-Tn-font-size]\%IEEE\*[doc-str-St] Std 1003.1
diff -r ebe87ce4d7dd -r 1750de5b0399 head/contrib/groff/tmac/groff_mdoc.man
--- a/head/contrib/groff/tmac/groff_mdoc.man	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/contrib/groff/tmac/groff_mdoc.man	Fri Jan 13 15:51:01 2012 +0200
@@ -2038,8 +2038,8 @@
 .St -isoC-90
 .It Li \-isoC\-99
 .St -isoC-99
-.It Li \-isoC\-11
-.St -isoC-11
+.It Li \-isoC\-2011
+.St -isoC-2011
 .El
 .Pp
 .
diff -r ebe87ce4d7dd -r 1750de5b0399 head/contrib/pf/pfctl/pfctl.8
--- a/head/contrib/pf/pfctl/pfctl.8	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/contrib/pf/pfctl/pfctl.8	Fri Jan 13 15:51:01 2012 +0200
@@ -24,7 +24,7 @@
 .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 .\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 .\"
-.\" $FreeBSD: head/contrib/pf/pfctl/pfctl.8 223637 2011-06-28 11:57:25Z bz $
+.\" $FreeBSD: head/contrib/pf/pfctl/pfctl.8 229669 2012-01-05 23:11:05Z rwatson $
 .\"
 .Dd June 21, 2011
 .Dt PFCTL 8
@@ -88,7 +88,7 @@
 is set to
 .Dv YES
 in
-.Xr rc.conf.local 8 ,
+.Xr rc.conf 5 ,
 the rule file specified with the variable
 .Va pf_rules
 is loaded automatically by the
diff -r ebe87ce4d7dd -r 1750de5b0399 head/etc/mac.conf
--- a/head/etc/mac.conf	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/etc/mac.conf	Fri Jan 13 15:51:01 2012 +0200
@@ -1,9 +1,9 @@
 #
-# $FreeBSD$
+# $FreeBSD: head/etc/mac.conf 229783 2012-01-07 16:10:32Z uqs $
 #
 # TrustedBSD MAC userland policy configuration file.  Kernel modules
 # export label information, and mac.conf indicates to userland
-# applications what defaults they should use in the absense of any
+# applications what defaults they should use in the absence of any
 # other user-provided information.
 #
 
diff -r ebe87ce4d7dd -r 1750de5b0399 head/etc/namedb/named.conf
--- a/head/etc/namedb/named.conf	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/etc/namedb/named.conf	Fri Jan 13 15:51:01 2012 +0200
@@ -1,4 +1,4 @@
-// $FreeBSD: head/etc/namedb/named.conf 224125 2011-07-17 06:20:47Z dougb $
+// $FreeBSD: head/etc/namedb/named.conf 229783 2012-01-07 16:10:32Z uqs $
 //
 // Refer to the named.conf(5) and named(8) man pages, and the documentation
 // in /usr/share/doc/bind9 for more details.
@@ -94,7 +94,7 @@
 
 	As documented at http://dns.icann.org/services/axfr/ these zones:
 	"." (the root), ARPA, IN-ADDR.ARPA, IP6.ARPA, and ROOT-SERVERS.NET
-	are availble for AXFR from these servers on IPv4 and IPv6:
+	are available for AXFR from these servers on IPv4 and IPv6:
 	xfr.lax.dns.icann.org, xfr.cjr.dns.icann.org
 */
 /*
diff -r ebe87ce4d7dd -r 1750de5b0399 head/etc/network.subr
--- a/head/etc/network.subr	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/etc/network.subr	Fri Jan 13 15:51:01 2012 +0200
@@ -22,7 +22,7 @@
 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 # SUCH DAMAGE.
 #
-# $FreeBSD: head/etc/network.subr 228472 2011-12-13 14:36:04Z glebius $
+# $FreeBSD: head/etc/network.subr 229783 2012-01-07 16:10:32Z uqs $
 #
 
 #
@@ -139,7 +139,7 @@
 			_cfg=0
 		fi
 
-		# backward compatiblity: $ipv6_ifconfig_IF
+		# backward compatibility: $ipv6_ifconfig_IF
 		ifconfig_args=`get_if_var $1 ipv6_ifconfig_IF`
 		if [ -n "${ifconfig_args}" ]; then
 			warn "\$ipv6_ifconfig_$1 is obsolete." \
diff -r ebe87ce4d7dd -r 1750de5b0399 head/etc/rc.d/auditd
--- a/head/etc/rc.d/auditd	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/etc/rc.d/auditd	Fri Jan 13 15:51:01 2012 +0200
@@ -1,6 +1,6 @@
 #!/bin/sh
 #
-# $FreeBSD$
+# $FreeBSD: head/etc/rc.d/auditd 229701 2012-01-06 14:00:31Z pjd $
 #
 # Start up for the Audit daemon.
 #
@@ -8,7 +8,7 @@
 # PROVIDE: auditd
 # REQUIRE: syslogd
 # BEFORE:  DAEMON
-# KEYWORD: shutdown
+# KEYWORD: nojail shutdown
 
 . /etc/rc.subr
 
diff -r ebe87ce4d7dd -r 1750de5b0399 head/etc/rc.d/bgfsck
--- a/head/etc/rc.d/bgfsck	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/etc/rc.d/bgfsck	Fri Jan 13 15:51:01 2012 +0200
@@ -1,6 +1,6 @@
 #!/bin/sh
 #
-# $FreeBSD$
+# $FreeBSD: head/etc/rc.d/bgfsck 229783 2012-01-07 16:10:32Z uqs $
 #
 
 # PROVIDE: bgfsck
@@ -22,7 +22,7 @@
 		background_fsck_delay=0
 	fi
 	if [ ${background_fsck_delay} -lt 0 ]; then
-		echo "Background file system checks delayed indefinitly"
+		echo "Background file system checks delayed indefinitely"
 		return 0
 	fi
 
diff -r ebe87ce4d7dd -r 1750de5b0399 head/etc/rc.d/dhclient
--- a/head/etc/rc.d/dhclient	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/etc/rc.d/dhclient	Fri Jan 13 15:51:01 2012 +0200
@@ -1,6 +1,6 @@
 #!/bin/sh
 #
-# $FreeBSD: head/etc/rc.d/dhclient 226879 2011-10-28 06:03:38Z rea $
+# $FreeBSD: head/etc/rc.d/dhclient 230007 2012-01-12 06:48:11Z rea $
 #
 
 # PROVIDE: dhclient
@@ -22,7 +22,14 @@
 dhclient_pre_check()
 {
 	if [ -z "${rc_force}" ] && ! dhcpif $ifn; then
-		err 1 "'$ifn' is not a DHCP-enabled interface"
+		local msg
+		msg="'$ifn' is not a DHCP-enabled interface"
+		if [ -z "${rc_quiet}" ]; then
+			err 1 "$msg"
+		else
+			debug "$msg"
+			exit 1
+		fi
 	fi
 }
 
diff -r ebe87ce4d7dd -r 1750de5b0399 head/etc/rc.d/hostname
--- a/head/etc/rc.d/hostname	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/etc/rc.d/hostname	Fri Jan 13 15:51:01 2012 +0200
@@ -23,7 +23,7 @@
 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 # SUCH DAMAGE.
 #
-# $FreeBSD$
+# $FreeBSD: head/etc/rc.d/hostname 229822 2012-01-08 20:25:29Z dougb $
 #
 
 # PROVIDE: hostname
@@ -65,7 +65,7 @@
 		# Null hostname is probably OK if DHCP is in use.
 		#
 		if [ -z "`list_net_interfaces dhcp`" ]; then
-			warn "\$hostname is not set -- see ${rcvar_manpage}."
+			warn "\$hostname is not set -- see rc.conf(5)."
 		fi
 		return
 	fi
diff -r ebe87ce4d7dd -r 1750de5b0399 head/etc/rc.d/pfsync
--- a/head/etc/rc.d/pfsync	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/etc/rc.d/pfsync	Fri Jan 13 15:51:01 2012 +0200
@@ -1,6 +1,6 @@
 #!/bin/sh
 #
-# $FreeBSD$
+# $FreeBSD: head/etc/rc.d/pfsync 229850 2012-01-09 08:50:22Z glebius $
 #
 
 # PROVIDE: pfsync
@@ -18,13 +18,6 @@
 
 pfsync_prestart()
 {
-	# XXX Currently pfsync cannot be a module as it must register
-	# a network protocol in a static kernel table.
-	if ! kldstat -q -m pfsync; then
-		warn "pfsync(4) must be statically compiled in the kernel."
-		return 1
-	fi
-
 	case "$pfsync_syncdev" in
 	'')
 		warn "pfsync_syncdev is not set."
diff -r ebe87ce4d7dd -r 1750de5b0399 head/etc/rc.firewall
--- a/head/etc/rc.firewall	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/etc/rc.firewall	Fri Jan 13 15:51:01 2012 +0200
@@ -23,7 +23,7 @@
 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 # SUCH DAMAGE.
 #
-# $FreeBSD$
+# $FreeBSD: head/etc/rc.firewall 229783 2012-01-07 16:10:32Z uqs $
 #
 
 #
@@ -46,7 +46,7 @@
 #   client      - will try to protect just this machine
 #   simple      - will try to protect a whole network
 #   closed      - totally disables IP services except via lo0 interface
-#   workstation - will try to protect just this machine using statefull
+#   workstation - will try to protect just this machine using stateful
 #		  firewalling. See below for rc.conf variables used
 #   UNKNOWN     - disables the loading of firewall rules.
 #   filename    - will load the rules in the given filename (full path required)
@@ -105,7 +105,7 @@
 	${fwcmd} add pass ipv6-icmp from fe80::/10 to fe80::/10
 	${fwcmd} add pass ipv6-icmp from fe80::/10 to ff02::/16
 
-	# Allow ICMPv6 destination unreach
+	# Allow ICMPv6 destination unreachable
 	${fwcmd} add pass ipv6-icmp from any to any icmp6types 1
 
 	# Allow NS/NA/toobig (don't filter it out)
@@ -437,7 +437,7 @@
 	#				 default denied packets should be
 	#				 logged (in /var/log/security).
 	#  firewall_nologports:		List of TCP/UDP ports for which
-	#				 denied incomming packets are not
+	#				 denied incoming packets are not
 	#				 logged.
 
 	# Allow packets for which a state has been built.
@@ -505,7 +505,7 @@
 	  ${fwcmd} add deny { tcp or udp } from any to any $i in
 	done
 
-	# Broadcasts and muticasts
+	# Broadcasts and multicasts
 	${fwcmd} add deny ip  from any to 255.255.255.255
 	${fwcmd} add deny ip  from any to 224.0.0.0/24 in	# XXX
 
@@ -513,7 +513,7 @@
 	${fwcmd} add deny udp from any to any 520 in
 
 	# Noise from webbrowsing.
-	# The statefull filter is a bit agressive, and will cause some
+	# The stateful filter is a bit aggressive, and will cause some
 	#  connection teardowns to be logged.
 	${fwcmd} add deny tcp from any 80,443 to any 1024-65535 in
 
diff -r ebe87ce4d7dd -r 1750de5b0399 head/etc/rc.initdiskless
--- a/head/etc/rc.initdiskless	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/etc/rc.initdiskless	Fri Jan 13 15:51:01 2012 +0200
@@ -24,7 +24,7 @@
 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 # SUCH DAMAGE.
 #
-# $FreeBSD$
+# $FreeBSD: head/etc/rc.initdiskless 229783 2012-01-07 16:10:32Z uqs $
 
 # On entry to this script the entire system consists of a read-only root
 # mounted via NFS. The kernel has run BOOTP and configured an interface
@@ -46,16 +46,16 @@
 #  bcast/${ipba} same as above
 #  ${class}	where ${class} is a list of directories supplied by
 #		bootp/dhcp through the T134 option.
-#		${ipba} and ${class} are typicall used to configure features
+#		${ipba} and ${class} are typically used to configure features
 #		for group of diskless clients, or even individual features;
 #  ${ip}	where ${ip} is the machine's assigned IP address, typically
 #		used to set host-specific features;
 #  ip/${ip}	same as above
 #
 # Template directories are scanned in the order they are listed above,
-# with each sucessive directory overriding (merged into) the previous one;
+# with each successive directory overriding (merged into) the previous one;
 # non-existing directories are ignored.  The subdirectory forms exist to
-# help keep the top level /conf managable in large installations.
+# help keep the top level /conf manageable in large installations.
 #
 # The existence of a directory /conf/T/M causes this script to create a
 # memory filesystem mounted as /M on the client.
diff -r ebe87ce4d7dd -r 1750de5b0399 head/etc/rc.subr
--- a/head/etc/rc.subr	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/etc/rc.subr	Fri Jan 13 15:51:01 2012 +0200
@@ -1,5 +1,5 @@
 # $NetBSD: rc.subr,v 1.67 2006/10/07 11:25:15 elad Exp $
-# $FreeBSD: head/etc/rc.subr 227366 2011-11-08 23:02:32Z jilles $
+# $FreeBSD: head/etc/rc.subr 230007 2012-01-12 06:48:11Z rea $
 #
 # Copyright (c) 1997-2004 The NetBSD Foundation, Inc.
 # All rights reserved.
@@ -32,7 +32,6 @@
 #	functions used by various rc scripts
 #
 
-: ${rcvar_manpage:='rc.conf(5)'}
 : ${RC_PID:=$$}; export RC_PID
 
 #
@@ -152,7 +151,7 @@
 		return 1
 		;;
 	*)
-		warn "\$${1} is not set properly - see ${rcvar_manpage}."
+		warn "\$${1} is not set properly - see rc.conf(5)."
 		return 1
 		;;
 	esac
@@ -1117,7 +1116,7 @@
 #
 rc_usage()
 {
-	echo -n 1>&2 "Usage: $0 [fast|force|one]("
+	echo -n 1>&2 "Usage: $0 [fast|force|one|quiet]("
 
 	_sep=
 	for _elem; do
@@ -1280,7 +1279,7 @@
 #	Make a symbolic link 'link' to src from basedir. If the
 #	directory in which link is to be created does not exist
 #	a warning will be displayed and an error will be returned.
-#	Returns 0 on sucess, 1 otherwise.
+#	Returns 0 on success, 1 otherwise.
 #
 make_symlink()
 {
diff -r ebe87ce4d7dd -r 1750de5b0399 head/etc/services
--- a/head/etc/services	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/etc/services	Fri Jan 13 15:51:01 2012 +0200
@@ -16,7 +16,7 @@
 # Kerberos services are for Kerberos v4, and are unofficial.  Sites running
 # v5 should uncomment v5 entries and comment v4 entries.
 #
-# $FreeBSD$
+# $FreeBSD: head/etc/services 229783 2012-01-07 16:10:32Z uqs $
 #	From: @(#)services	5.8 (Berkeley) 5/9/91
 #
 # WELL KNOWN PORT NUMBERS
@@ -795,7 +795,7 @@
 kpasswd5	464/tcp	   # Kerberos (v5)
 kpasswd5	464/udp	   # Kerberos (v5)
 #PROBLEMS!==============================================================
-# IANA has offically assigned these two ports as ``kpasswd''
+# IANA has officially assigned these two ports as ``kpasswd''
 #kpasswd	464/tcp	   # Kerberos (v5)
 #kpasswd	464/udp	   # Kerberos (v5)
 #PROBLEMS!==============================================================
@@ -1275,8 +1275,8 @@
 hcp-wismar      686/udp    #Hardware Control Protocol Wismar
 asipregistry	687/tcp
 asipregistry	687/udp
-realm-rusd      688/tcp    #ApplianceWare managment protocol
-realm-rusd      688/udp    #ApplianceWare managment protocol
+realm-rusd      688/tcp    #ApplianceWare management protocol
+realm-rusd      688/udp    #ApplianceWare management protocol
 nmap            689/tcp
 nmap            689/udp
 vatp            690/tcp    #Velazquez Application Transfer Protocol
@@ -2392,7 +2392,7 @@
 xdsxdm		6558/udp
 sane-port	6566/tcp   #Scanner Access Now Easy (SANE) Control Port
 sane-port	6566/udp   #Scanner Access Now Easy (SANE) Control Port
-ircd		6667/tcp   #Internet Relay Chat (unoffical)
+ircd		6667/tcp   #Internet Relay Chat (unofficial)
 frc-hp		6704/sctp  #ForCES HP (High Priority) channel
 frc-mp		6705/sctp  #ForCES MP (Medium Priority) channel
 frc-lp		6706/sctp  #ForCES LP (Low priority) channel
diff -r ebe87ce4d7dd -r 1750de5b0399 head/gnu/usr.bin/cvs/lib/config.h.proto
--- a/head/gnu/usr.bin/cvs/lib/config.h.proto	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/gnu/usr.bin/cvs/lib/config.h.proto	Fri Jan 13 15:51:01 2012 +0200
@@ -1,4 +1,4 @@
-/* $FreeBSD$ */
+/* $FreeBSD: head/gnu/usr.bin/cvs/lib/config.h.proto 229809 2012-01-08 12:38:41Z ed $ */
 
 /* config.h.  Generated from config.h.in by configure.  */
 /* config.h.in.  Generated from configure.in by autoheader.  */
@@ -324,7 +324,7 @@
 #define HAVE_SYS_STAT_H 1
 
 /* Define to 1 if you have the <sys/timeb.h> header file. */
-#define HAVE_SYS_TIMEB_H 1
+/* #undef HAVE_SYS_TIMEB_H */
 
 /* Define to 1 if you have the <sys/time.h> header file. */
 #define HAVE_SYS_TIME_H 1
diff -r ebe87ce4d7dd -r 1750de5b0399 head/include/arpa/nameser_compat.h
--- a/head/include/arpa/nameser_compat.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/include/arpa/nameser_compat.h	Fri Jan 13 15:51:01 2012 +0200
@@ -29,7 +29,7 @@
 /*%
  *      from nameser.h	8.1 (Berkeley) 6/2/93
  *	$Id: nameser_compat.h,v 1.5.18.3 2006/05/19 02:36:00 marka Exp $
- * $FreeBSD$
+ * $FreeBSD: head/include/arpa/nameser_compat.h 229781 2012-01-07 16:10:15Z uqs $
  */
 
 #ifndef _ARPA_NAMESER_COMPAT_
@@ -63,7 +63,7 @@
 			/* fields in third byte */
 	unsigned	qr: 1;		/*%< response flag */
 	unsigned	opcode: 4;	/*%< purpose of message */
-	unsigned	aa: 1;		/*%< authoritive answer */
+	unsigned	aa: 1;		/*%< authoritative answer */
 	unsigned	tc: 1;		/*%< truncated message */
 	unsigned	rd: 1;		/*%< recursion desired */
 			/* fields in fourth byte */
@@ -77,7 +77,7 @@
 			/* fields in third byte */
 	unsigned	rd :1;		/*%< recursion desired */
 	unsigned	tc :1;		/*%< truncated message */
-	unsigned	aa :1;		/*%< authoritive answer */
+	unsigned	aa :1;		/*%< authoritative answer */
 	unsigned	opcode :4;	/*%< purpose of message */
 	unsigned	qr :1;		/*%< response flag */
 			/* fields in fourth byte */
diff -r ebe87ce4d7dd -r 1750de5b0399 head/include/complex.h
--- a/head/include/complex.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/include/complex.h	Fri Jan 13 15:51:01 2012 +0200
@@ -23,24 +23,26 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $FreeBSD: head/include/complex.h 226458 2011-10-17 05:41:03Z das $
+ * $FreeBSD: head/include/complex.h 229594 2012-01-05 12:09:39Z ed $
  */
 
 #ifndef _COMPLEX_H
 #define	_COMPLEX_H
 
+#include <sys/cdefs.h>
+
 #ifdef __GNUC__
 #if __STDC_VERSION__ < 199901
 #define	_Complex	__complex__
 #endif
-#define	_Complex_I	1.0fi
+#define	_Complex_I	((float _Complex)1.0i)
+_Static_assert(__generic(_Complex_I, float _Complex, 1, 0),
+    "_Complex_I must be of type float _Complex");
 #endif
 
 #define	complex		_Complex
 #define	I		_Complex_I
 
-#include <sys/cdefs.h>
-
 __BEGIN_DECLS
 
 double		cabs(double complex);
diff -r ebe87ce4d7dd -r 1750de5b0399 head/include/dlfcn.h
--- a/head/include/dlfcn.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/include/dlfcn.h	Fri Jan 13 15:51:01 2012 +0200
@@ -26,7 +26,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $FreeBSD$
+ * $FreeBSD: head/include/dlfcn.h 229768 2012-01-07 10:33:01Z kib $
  */
 
 #ifndef _DLFCN_H_
@@ -118,6 +118,7 @@
 void	*dlsym(void * __restrict, const char * __restrict);
 
 #if __BSD_VISIBLE
+void	*fdlopen(int, int);
 int	 dladdr(const void * __restrict, Dl_info * __restrict);
 dlfunc_t dlfunc(void * __restrict, const char * __restrict);
 int	 dlinfo(void * __restrict, int, void * __restrict);
diff -r ebe87ce4d7dd -r 1750de5b0399 head/include/gssapi/gssapi.h
--- a/head/include/gssapi/gssapi.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/include/gssapi/gssapi.h	Fri Jan 13 15:51:01 2012 +0200
@@ -25,7 +25,7 @@
  * HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF
  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  *
- * $FreeBSD$
+ * $FreeBSD: head/include/gssapi/gssapi.h 229781 2012-01-07 16:10:15Z uqs $
  */
 
 #ifndef _GSSAPI_GSSAPI_H_
@@ -756,11 +756,11 @@
 OM_uint32 gss_decapsulate_token
 	      (const gss_buffer_t,  /* mechanism independent token */
 	       gss_OID,		 /* desired mechanism */
-	       gss_buffer_t	 /* decapsulated mechanism dependant token */
+	       gss_buffer_t	 /* decapsulated mechanism dependent token */
 	      );
 
 OM_uint32 gss_encapsulate_token
-	      (const gss_buffer_t,  /* mechanism dependant token */
+	      (const gss_buffer_t,  /* mechanism dependent token */
 	       gss_OID,		 /* desired mechanism */
 	       gss_buffer_t	 /* encapsulated mechanism independent token */
 	      );
diff -r ebe87ce4d7dd -r 1750de5b0399 head/include/resolv.h
--- a/head/include/resolv.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/include/resolv.h	Fri Jan 13 15:51:01 2012 +0200
@@ -47,7 +47,7 @@
 /*%
  *	@(#)resolv.h	8.1 (Berkeley) 6/2/93
  *	$Id: resolv.h,v 1.19.18.4 2008/04/03 23:15:15 marka Exp $
- * $FreeBSD$
+ * $FreeBSD: head/include/resolv.h 229781 2012-01-07 16:10:15Z uqs $
  */
 
 #ifndef _RESOLV_H_
@@ -184,7 +184,7 @@
 			u_int16_t		nscount;
 			u_int16_t		nstimes[MAXNS];	/*%< ms. */
 			int			nssocks[MAXNS];
-			struct __res_state_ext *ext;	/*%< extention for IPv6 */
+			struct __res_state_ext *ext;	/*%< extension for IPv6 */
 		} _ext;
 	} _u;
 };
diff -r ebe87ce4d7dd -r 1750de5b0399 head/include/rpc/svc.h
--- a/head/include/rpc/svc.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/include/rpc/svc.h	Fri Jan 13 15:51:01 2012 +0200
@@ -30,7 +30,7 @@
  *
  *	from: @(#)svc.h 1.35 88/12/17 SMI
  *	from: @(#)svc.h      1.27    94/04/25 SMI
- * $FreeBSD$
+ * $FreeBSD: head/include/rpc/svc.h 229781 2012-01-07 16:10:15Z uqs $
  */
 
 /*
@@ -314,7 +314,7 @@
  * Somebody has to wait for incoming requests and then call the correct
  * service routine.  The routine svc_run does infinite waiting; i.e.,
  * svc_run never returns.
- * Since another (co-existant) package may wish to selectively wait for
+ * Since another (co-existent) package may wish to selectively wait for
  * incoming calls or other events outside of the rpc architecture, the
  * routine svc_getreq is provided.  It must be passed readfds, the
  * "in-place" results of a select system call (see select, section 2).
diff -r ebe87ce4d7dd -r 1750de5b0399 head/include/rpcsvc/mount.x
--- a/head/include/rpcsvc/mount.x	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/include/rpcsvc/mount.x	Fri Jan 13 15:51:01 2012 +0200
@@ -37,7 +37,7 @@
 %/*static char sccsid[] = "from: @(#)mount.x	2.1 88/08/01 4.0 RPCSRC";*/
 %#endif /* not lint */
 %#include <sys/cdefs.h>
-%__FBSDID("$FreeBSD$");
+%__FBSDID("$FreeBSD: head/include/rpcsvc/mount.x 229781 2012-01-07 16:10:15Z uqs $");
 #endif
 
 const MNTPATHLEN = 1024;	/* maximum bytes in a pathname argument */
@@ -150,7 +150,7 @@
 	version MOUNTVERS {
 		/*
 		 * Does no work. It is made available in all RPC services
-		 * to allow server reponse testing and timing
+		 * to allow server response testing and timing
 		 */
 		void
 		MOUNTPROC_NULL(void) = 0;
@@ -205,7 +205,7 @@
 	version MOUNTVERS3 {
 		/*
 		 * Does no work. It is made available in all RPC services
-		 * to allow server reponse testing and timing
+		 * to allow server response testing and timing
 		 */
 		void
 		MOUNTPROC_NULL(void) = 0;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/include/rpcsvc/nfs_prot.x
--- a/head/include/rpcsvc/nfs_prot.x	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/include/rpcsvc/nfs_prot.x	Fri Jan 13 15:51:01 2012 +0200
@@ -33,7 +33,7 @@
 %/*static char sccsid[] = "from: @(#)nfs_prot.x	2.1 88/08/01 4.0 RPCSRC";*/
 %#endif /* not lint */
 %#include <sys/cdefs.h>
-%__FBSDID("$FreeBSD$");
+%__FBSDID("$FreeBSD: head/include/rpcsvc/nfs_prot.x 229781 2012-01-07 16:10:15Z uqs $");
 #endif
 
 const NFS_PORT          = 2049;
@@ -121,7 +121,7 @@
 	unsigned uid;		/* owner user id */
 	unsigned gid;		/* owner group id */
 	unsigned size;		/* file size in bytes */
-	unsigned blocksize;	/* prefered block size */
+	unsigned blocksize;	/* preferred block size */
 	unsigned rdev;		/* special device # */
 	unsigned blocks;	/* Kb of disk used by file */
 	unsigned fsid;		/* device # */
@@ -402,7 +402,7 @@
 	uid3	uid;		/* owner user id */
 	gid3	gid;		/* owner group id */
 	size3	size;		/* file size in bytes */
-	size3	used;		/* prefered block size */
+	size3	used;		/* preferred block size */
 	specdata3 rdev;		/* special device # */
 	uint64 fsid;		/* device # */
 	fileid3	fileid;		/* inode # */
diff -r ebe87ce4d7dd -r 1750de5b0399 head/include/rpcsvc/nis.x
--- a/head/include/rpcsvc/nis.x	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/include/rpcsvc/nis.x	Fri Jan 13 15:51:01 2012 +0200
@@ -30,7 +30,7 @@
 
 #ifndef RPC_HDR
 %#include <sys/cdefs.h>
-%__FBSDID("$FreeBSD$");
+%__FBSDID("$FreeBSD: head/include/rpcsvc/nis.x 229781 2012-01-07 16:10:15Z uqs $");
 #endif
 
 /* 
@@ -192,8 +192,8 @@
  * note that modifications will appear as two entries, for names, they have
  * an "OLD" entry followed by a "NEW" entry. For entries in tables, there
  * is a remove followed by an add. It is done this way so that we can read
- * the log backwards to back out transactions and forwards to propogate
- * updated.
+ * the log backwards to back out transactions and forwards to propagate
+ * updates.
  */
 enum log_entry_t {
 	LOG_NOP = 0,
diff -r ebe87ce4d7dd -r 1750de5b0399 head/include/stdlib.h
--- a/head/include/stdlib.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/include/stdlib.h	Fri Jan 13 15:51:01 2012 +0200
@@ -27,7 +27,7 @@
  * SUCH DAMAGE.
  *
  *	@(#)stdlib.h	8.5 (Berkeley) 5/19/95
- * $FreeBSD: head/include/stdlib.h 228901 2011-12-26 18:55:37Z ed $
+ * $FreeBSD: head/include/stdlib.h 229848 2012-01-09 06:36:28Z ed $
  */
 
 #ifndef _STDLIB_H_
@@ -149,12 +149,13 @@
 #endif /* __ISO_C_VISIBLE >= 1999 */
 
 /*
- * If we're in a mode greater than C99, expose C1x functions.
+ * If we're in a mode greater than C99, expose C11 functions.
  */
 #if __ISO_C_VISIBLE >= 2011
+void *	aligned_alloc(size_t, size_t);
+int	at_quick_exit(void (*)(void));
 _Noreturn void
 	quick_exit(int);
-int	at_quick_exit(void (*)(void));
 #endif /* __ISO_C_VISIBLE >= 2011 */
 /*
  * Extensions made by POSIX relative to C.  We don't know yet which edition
diff -r ebe87ce4d7dd -r 1750de5b0399 head/include/tgmath.h
--- a/head/include/tgmath.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/include/tgmath.h	Fri Jan 13 15:51:01 2012 +0200
@@ -26,7 +26,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $FreeBSD: head/include/tgmath.h 229575 2012-01-05 10:46:22Z ed $
+ * $FreeBSD: head/include/tgmath.h 229716 2012-01-06 19:04:59Z ed $
  */
 
 #ifndef _TGMATH_H_
@@ -54,21 +54,40 @@
  * because the return type of the whole expression would incorrectly be long
  * double complex regardless of the argument types.
  *
- * The _Complex_I distinction should not be needed, but due to a bug in
- * GCC 4.2, _Complex_I is not of type float _Complex.
+ * The structure of the C11 implementation of these macros can in
+ * principle be reused for non-C11 compilers, but due to an integer
+ * promotion bug for complex types in GCC 4.2, simply let non-C11
+ * compilers use an inefficient yet reliable version.
  */
 
-#ifndef __generic
-#error "<tgmath.h> not implemented for this compiler"
-#endif
-
+#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
+#define	__tg_generic(x, cfnl, cfn, cfnf, fnl, fn, fnf)			\
+	_Generic(x,							\
+		long double _Complex: cfnl,				\
+		double _Complex: cfn,					\
+		float _Complex: cfnf,					\
+		long double: fnl,					\
+		default: fn,						\
+		float: fnf						\
+	)
+#define	__tg_type(x)							\
+	__tg_generic(x, (long double _Complex)0, (double _Complex)0,	\
+	    (float _Complex)0, (long double)0, (double)0, (float)0)
+#define	__tg_impl_simple(x, y, z, fnl, fn, fnf, ...)			\
+	__tg_generic(							\
+	    __tg_type(x) + __tg_type(y) + __tg_type(z),			\
+	    fnl, fn, fnf, fnl, fn, fnf)(__VA_ARGS__)
+#define	__tg_impl_full(x, y, cfnl, cfn, cfnf, fnl, fn, fnf, ...)	\
+	__tg_generic(							\
+	    __tg_type(x) + __tg_type(y),				\
+	    cfnl, cfn, cfnf, fnl, fn, fnf)(__VA_ARGS__)
+#elif defined(__generic)
 #define	__tg_generic_simple(x, fnl, fn, fnf)				\
 	__generic(x, long double _Complex, fnl,				\
 	    __generic(x, double _Complex, fn,				\
 	        __generic(x, float _Complex, fnf,			\
-	            __generic(x, __typeof(_Complex_I), fnf,		\
-	                __generic(x, long double, fnl,			\
-	                    __generic(x, float, fnf, fn))))))
+	            __generic(x, long double, fnl,			\
+	                __generic(x, float, fnf, fn)))))
 #define	__tg_impl_simple(x, y, z, fnl, fn, fnf, ...)			\
 	__tg_generic_simple(x,						\
 	    __tg_generic_simple(y,					\
@@ -87,9 +106,8 @@
 	__generic(x, long double _Complex, cfnl,			\
 	    __generic(x, double _Complex, cfn,				\
 	        __generic(x, float _Complex, cfnf,			\
-	            __generic(x, __typeof(_Complex_I), cfnf,		\
-	                __generic(x, long double, fnl,			\
-	                    __generic(x, float, fnf, fn))))))
+	            __generic(x, long double, fnl,			\
+	                __generic(x, float, fnf, fn)))))
 #define	__tg_impl_full(x, y, cfnl, cfn, cfnf, fnl, fn, fnf, ...)	\
 	__tg_generic_full(x,						\
 	    __tg_generic_full(y, cfnl, cfnl, cfnl, cfnl, cfnl, cfnl),	\
@@ -99,6 +117,9 @@
 	    __tg_generic_full(y, cfnl, cfn , cfn , fnl , fn  , fn  ),	\
 	    __tg_generic_full(y, cfnl, cfn , cfnf, fnl , fn  , fnf ))	\
 	    (__VA_ARGS__)
+#else
+#error "<tgmath.h> not implemented for this compiler"
+#endif
 
 /* Macros to save lots of repetition below */
 #define	__tg_simple(x, fn)						\
diff -r ebe87ce4d7dd -r 1750de5b0399 head/lib/libc/gen/Makefile.inc
--- a/head/lib/libc/gen/Makefile.inc	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/lib/libc/gen/Makefile.inc	Fri Jan 13 15:51:01 2012 +0200
@@ -1,5 +1,5 @@
 #	@(#)Makefile.inc	8.6 (Berkeley) 5/4/95
-# $FreeBSD: head/lib/libc/gen/Makefile.inc 228843 2011-12-23 15:00:37Z cperciva $
+# $FreeBSD: head/lib/libc/gen/Makefile.inc 229768 2012-01-07 10:33:01Z kib $
 
 # machine-independent gen sources
 .PATH: ${.CURDIR}/${LIBC_ARCH}/gen ${.CURDIR}/gen
@@ -95,8 +95,8 @@
 	directory.3 fdopendir.3 \
 	directory.3 readdir.3 directory.3 readdir_r.3 directory.3 rewinddir.3 \
 	directory.3 seekdir.3 directory.3 telldir.3
-MLINKS+=dlopen.3 dlclose.3 dlopen.3 dlerror.3 dlopen.3 dlfunc.3 \
-	dlopen.3 dlsym.3
+MLINKS+=dlopen.3 fdlopen.3 dlopen.3 dlclose.3 dlopen.3 dlerror.3 \
+	dlopen.3 dlfunc.3 dlopen.3 dlsym.3
 MLINKS+=err.3 err_set_exit.3 err.3 err_set_file.3 err.3 errc.3 err.3 errx.3 \
 	err.3 verr.3 err.3 verrc.3 err.3 verrx.3 err.3 vwarn.3 err.3 vwarnc.3 \
 	err.3 vwarnx.3 err.3 warnc.3 err.3 warn.3 err.3 warnx.3
diff -r ebe87ce4d7dd -r 1750de5b0399 head/lib/libc/gen/Symbol.map
--- a/head/lib/libc/gen/Symbol.map	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/lib/libc/gen/Symbol.map	Fri Jan 13 15:51:01 2012 +0200
@@ -1,5 +1,5 @@
 /*
- * $FreeBSD: head/lib/libc/gen/Symbol.map 228843 2011-12-23 15:00:37Z cperciva $
+ * $FreeBSD: head/lib/libc/gen/Symbol.map 229768 2012-01-07 10:33:01Z kib $
  */
 
 FBSD_1.0 {
@@ -382,6 +382,7 @@
 };
 
 FBSD_1.3 {
+	 fdlopen;
 	__FreeBSD_libc_enter_restricted_mode;
 };
 
diff -r ebe87ce4d7dd -r 1750de5b0399 head/lib/libc/gen/dlfcn.c
--- a/head/lib/libc/gen/dlfcn.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/lib/libc/gen/dlfcn.c	Fri Jan 13 15:51:01 2012 +0200
@@ -25,7 +25,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
+__FBSDID("$FreeBSD: head/lib/libc/gen/dlfcn.c 229768 2012-01-07 10:33:01Z kib $");
 
 /*
  * Linkage to services provided by the dynamic linker.
@@ -147,6 +147,15 @@
 	return 0;
 }
 
+#pragma weak fdlopen
+void *
+fdlopen(int fd, int mode)
+{
+
+	_rtld_error(sorry);
+	return NULL;
+}
+
 #pragma weak _rtld_atfork_pre
 void
 _rtld_atfork_pre(int *locks)
diff -r ebe87ce4d7dd -r 1750de5b0399 head/lib/libc/gen/dlopen.3
--- a/head/lib/libc/gen/dlopen.3	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/lib/libc/gen/dlopen.3	Fri Jan 13 15:51:01 2012 +0200
@@ -30,13 +30,14 @@
 .\" Copyright (c) 1991 Sun Microsystems, Inc.
 .\"
 .\" @(#) dlopen.3 1.6 90/01/31 SMI
-.\" $FreeBSD$
+.\" $FreeBSD: head/lib/libc/gen/dlopen.3 229768 2012-01-07 10:33:01Z kib $
 .\"
-.Dd July 7, 2009
+.Dd December 21, 2011
 .Dt DLOPEN 3
 .Os
 .Sh NAME
 .Nm dlopen ,
+.Nm fdlopen ,
 .Nm dlsym ,
 .Nm dlfunc ,
 .Nm dlerror ,
@@ -49,6 +50,8 @@
 .Ft void *
 .Fn dlopen "const char *path" "int mode"
 .Ft void *
+.Fn fdlopen "int fd" "int mode"
+.Ft void *
 .Fn dlsym "void * restrict handle" "const char * restrict symbol"
 .Ft dlfunc_t
 .Fn dlfunc "void * restrict handle" "const char * restrict symbol"
@@ -164,6 +167,36 @@
 .Fn dlerror .
 .Pp
 The
+.Fn fdlopen
+function is similar to
+.Fn dlopen ,
+but it takes the file descriptor argument
+.Fa fd ,
+which is used for the file operations needed to load an object
+into the address space.
+The file descriptor
+.Fa fd
+is not closed by the function regardless a result of execution,
+but a duplicate of the file descriptor is.
+This may be important if a
+.Xr lockf 3
+lock is held on the passed descriptor.
+The
+.Fa fd
+argument -1 is interpreted as a reference to the main
+executable of the process, similar to
+.Va NULL
+value for the
+.Fa name
+argument to
+.Fn dlopen .
+The
+.Fn fdlopen
+function can be used by the code that needs to perform
+additional checks on the loaded objects, to prevent races with
+symlinking or renames.
+.Pp
+The
 .Fn dlsym
 function
 returns the address binding of the symbol described in the null-terminated
@@ -354,6 +387,7 @@
 .Sh ERRORS
 The
 .Fn dlopen ,
+.Fn fdlopen ,
 .Fn dlsym ,
 and
 .Fn dlfunc
diff -r ebe87ce4d7dd -r 1750de5b0399 head/lib/libc/gen/getnetgrent.c
--- a/head/lib/libc/gen/getnetgrent.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/lib/libc/gen/getnetgrent.c	Fri Jan 13 15:51:01 2012 +0200
@@ -34,7 +34,7 @@
 static char sccsid[] = "@(#)getnetgrent.c	8.2 (Berkeley) 4/27/95";
 #endif /* LIBC_SCCS and not lint */
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
+__FBSDID("$FreeBSD: head/lib/libc/gen/getnetgrent.c 229938 2012-01-10 19:54:53Z ghelmer $");
 
 #include <ctype.h>
 #include <stdio.h>
diff -r ebe87ce4d7dd -r 1750de5b0399 head/lib/libc/net/getaddrinfo.c
--- a/head/lib/libc/net/getaddrinfo.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/lib/libc/net/getaddrinfo.c	Fri Jan 13 15:51:01 2012 +0200
@@ -54,7 +54,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
+__FBSDID("$FreeBSD: head/lib/libc/net/getaddrinfo.c 229766 2012-01-07 09:01:19Z ume $");
 
 #include "namespace.h"
 #include <sys/types.h>
@@ -1576,7 +1576,8 @@
 	if (*scope == '\0')
 		return -1;
 
-	if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) {
+	if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6) ||
+	    IN6_IS_ADDR_MC_NODELOCAL(a6)) {
 		/*
 		 * We currently assume a one-to-one mapping between links
 		 * and interfaces, so we simply use interface indices for
diff -r ebe87ce4d7dd -r 1750de5b0399 head/lib/libc/powerpc/SYS.h
--- a/head/lib/libc/powerpc/SYS.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/lib/libc/powerpc/SYS.h	Fri Jan 13 15:51:01 2012 +0200
@@ -27,7 +27,7 @@
  * SUCH DAMAGE.
  *
  *	$NetBSD: SYS.h,v 1.8 2002/01/14 00:55:56 thorpej Exp $
- * $FreeBSD$
+ * $FreeBSD: head/lib/libc/powerpc/SYS.h 229693 2012-01-06 09:21:40Z andreast $
  */
 
 #include <sys/syscall.h>
@@ -44,10 +44,8 @@
 	.align 2;						\
 2:	b	PIC_PLT(CNAME(HIDENAME(cerror)));		\
 ENTRY(__CONCAT(__sys_,x));					\
-	.weak	CNAME(x);					\
-	.set	CNAME(x),CNAME(__CONCAT(__sys_,x));		\
-	.weak	CNAME(__CONCAT(_,x));				\
-	.set	CNAME(__CONCAT(_,x)),CNAME(__CONCAT(__sys_,x));	\
+	WEAK_ALIAS(x,__CONCAT(__sys_,x));			\
+	WEAK_ALIAS(__CONCAT(_,x),__CONCAT(__sys_,x));		\
 	_SYSCALL(x);						\
 	bso	2b
 
@@ -55,8 +53,7 @@
 	.text;							\
 	.align 2;						\
 ENTRY(__CONCAT(__sys_,x));					\
-	.weak	CNAME(__CONCAT(_,x));				\
-	.set	CNAME(__CONCAT(_,x)),CNAME(__CONCAT(__sys_,x));	\
+	WEAK_ALIAS(__CONCAT(_,x),__CONCAT(__sys_,x));		\
 	_SYSCALL(x);						\
 	bnslr;							\
 	b	PIC_PLT(CNAME(HIDENAME(cerror)))
@@ -66,10 +63,8 @@
 	.align 2;						\
 2:	b	PIC_PLT(CNAME(HIDENAME(cerror)));		\
 ENTRY(__CONCAT(__sys_,x));					\
-	.weak	CNAME(x);					\
-	.set	CNAME(x),CNAME(__CONCAT(__sys_,x));		\
-	.weak	CNAME(__CONCAT(_,x));				\
-	.set	CNAME(__CONCAT(_,x)),CNAME(__CONCAT(__sys_,x));	\
+	WEAK_ALIAS(x,__CONCAT(__sys_,x));			\
+	WEAK_ALIAS(__CONCAT(_,x), __CONCAT(__sys_,x));		\
 	_SYSCALL(x);						\
 	bnslr;							\
 	b	PIC_PLT(CNAME(HIDENAME(cerror)))
diff -r ebe87ce4d7dd -r 1750de5b0399 head/lib/libc/powerpc/gen/setjmp.S
--- a/head/lib/libc/powerpc/gen/setjmp.S	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/lib/libc/powerpc/gen/setjmp.S	Fri Jan 13 15:51:01 2012 +0200
@@ -26,7 +26,7 @@
 /*      $NetBSD: setjmp.S,v 1.3 1998/10/03 12:30:38 tsubai Exp $        */
 
 #include <machine/asm.h>
-__FBSDID("$FreeBSD$");
+__FBSDID("$FreeBSD: head/lib/libc/powerpc/gen/setjmp.S 229806 2012-01-08 11:57:38Z andreast $");
 
 #include <sys/syscall.h>
 	
@@ -69,8 +69,7 @@
 	li	%r3,0			/* return (0) */
 	blr
 
-	.weak CNAME(longjmp)
-	.set CNAME(longjmp),CNAME(__longjmp)
+	WEAK_ALIAS(longjmp, __longjmp)
 ENTRY(__longjmp)
 	lmw	%r9,20(%r3)		/* restore regs */
 	mr	%r6,%r4			/* save val param */
diff -r ebe87ce4d7dd -r 1750de5b0399 head/lib/libc/powerpc64/SYS.h
--- a/head/lib/libc/powerpc64/SYS.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/lib/libc/powerpc64/SYS.h	Fri Jan 13 15:51:01 2012 +0200
@@ -27,7 +27,7 @@
  * SUCH DAMAGE.
  *
  *	$NetBSD: SYS.h,v 1.8 2002/01/14 00:55:56 thorpej Exp $
- * $FreeBSD$
+ * $FreeBSD: head/lib/libc/powerpc64/SYS.h 229693 2012-01-06 09:21:40Z andreast $
  */
 
 #include <sys/syscall.h>
@@ -52,10 +52,8 @@
 	mtlr	%r0;						\
 	blr;							\
 ENTRY(__CONCAT(__sys_,x));					\
-	.weak	CNAME(x);					\
-	.set	CNAME(x),CNAME(__CONCAT(__sys_,x));		\
-	.weak	CNAME(__CONCAT(_,x));				\
-	.set	CNAME(__CONCAT(_,x)),CNAME(__CONCAT(__sys_,x));	\
+	WEAK_ALIAS(x,__CONCAT(__sys_,x));			\
+	WEAK_ALIAS(__CONCAT(_,x),__CONCAT(__sys_,x));		\
 	_SYSCALL(x);						\
 	bso	2b
 
@@ -63,8 +61,7 @@
 	.text;							\
 	.align 2;						\
 ENTRY(__CONCAT(__sys_,x));					\
-	.weak	CNAME(__CONCAT(_,x));				\
-	.set	CNAME(__CONCAT(_,x)),CNAME(__CONCAT(__sys_,x));	\
+	WEAK_ALIAS(__CONCAT(_,x),__CONCAT(__sys_,x));		\
 	_SYSCALL(x);						\
 	bnslr;							\
 	mflr	%r0;						\
@@ -81,10 +78,8 @@
 	.text;							\
 	.align 2;						\
 ENTRY(__CONCAT(__sys_,x));					\
-	.weak	CNAME(x);					\
-	.set	CNAME(x),CNAME(__CONCAT(__sys_,x));		\
-	.weak	CNAME(__CONCAT(_,x));				\
-	.set	CNAME(__CONCAT(_,x)),CNAME(__CONCAT(__sys_,x));	\
+	WEAK_ALIAS(x,__CONCAT(__sys_,x));			\
+	WEAK_ALIAS(__CONCAT(_,x), __CONCAT(__sys_,x));		\
 	_SYSCALL(x);						\
 	bnslr;							\
 								\
diff -r ebe87ce4d7dd -r 1750de5b0399 head/lib/libc/powerpc64/gen/setjmp.S
--- a/head/lib/libc/powerpc64/gen/setjmp.S	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/lib/libc/powerpc64/gen/setjmp.S	Fri Jan 13 15:51:01 2012 +0200
@@ -26,7 +26,7 @@
 /*      $NetBSD: setjmp.S,v 1.3 1998/10/03 12:30:38 tsubai Exp $        */
 
 #include <machine/asm.h>
-__FBSDID("$FreeBSD$");
+__FBSDID("$FreeBSD: head/lib/libc/powerpc64/gen/setjmp.S 229806 2012-01-08 11:57:38Z andreast $");
 
 #include <sys/syscall.h>
 	
@@ -93,10 +93,7 @@
 	li	%r3,0			/* return (0) */
 	blr
 
-	.weak CNAME(longjmp)
-	.set CNAME(longjmp),CNAME(__longjmp)
-	.weak CNAME(.longjmp)
-	.set CNAME(.longjmp),CNAME(.__longjmp)
+	WEAK_ALIAS(longjmp, __longjmp)
 ENTRY(__longjmp)
 	ld	%r9,40 + 0*8(%r3)
 	ld	%r10,40 + 1*8(%r3)
diff -r ebe87ce4d7dd -r 1750de5b0399 head/lib/libc/stdlib/Makefile.inc
--- a/head/lib/libc/stdlib/Makefile.inc	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/lib/libc/stdlib/Makefile.inc	Fri Jan 13 15:51:01 2012 +0200
@@ -1,5 +1,5 @@
 #	from @(#)Makefile.inc	8.3 (Berkeley) 2/4/95
-# $FreeBSD: head/lib/libc/stdlib/Makefile.inc 228322 2011-12-07 15:25:48Z theraven $
+# $FreeBSD: head/lib/libc/stdlib/Makefile.inc 229848 2012-01-09 06:36:28Z ed $
 
 # machine-independent stdlib sources
 .PATH: ${.CURDIR}/${LIBC_ARCH}/stdlib ${.CURDIR}/stdlib
@@ -18,17 +18,18 @@
 # machine-dependent stdlib sources
 .sinclude "${.CURDIR}/${LIBC_ARCH}/stdlib/Makefile.inc"
 
-MAN+=	a64l.3 abort.3 abs.3 alloca.3 atexit.3 atof.3 atoi.3 atol.3 \
-	at_quick_exit.3 bsearch.3 \
+MAN+=	a64l.3 abort.3 abs.3 aligned_alloc.3 alloca.3 atexit.3 atof.3 \
+	atoi.3 atol.3 at_quick_exit.3 bsearch.3 \
 	div.3 exit.3 getenv.3 getopt.3 getopt_long.3 getsubopt.3 \
 	hcreate.3 imaxabs.3 imaxdiv.3 insque.3 labs.3 ldiv.3 llabs.3 lldiv.3 \
-	lsearch.3 malloc.3 memory.3 posix_memalign.3 ptsname.3 qsort.3 \
+	lsearch.3 malloc.3 memory.3 ptsname.3 qsort.3 \
 	quick_exit.3 \
 	radixsort.3 rand.3 random.3 \
 	realpath.3 strfmon.3 strtod.3 strtol.3 strtonum.3 strtoul.3 system.3 \
 	tsearch.3
 
 MLINKS+=a64l.3 l64a.3 a64l.3 l64a_r.3
+MLINKS+=aligned_alloc.3 posix_memalign.3
 MLINKS+=atol.3 atoll.3
 MLINKS+=exit.3 _Exit.3
 MLINKS+=getenv.3 putenv.3 getenv.3 setenv.3 getenv.3 unsetenv.3
diff -r ebe87ce4d7dd -r 1750de5b0399 head/lib/libc/stdlib/Symbol.map
--- a/head/lib/libc/stdlib/Symbol.map	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/lib/libc/stdlib/Symbol.map	Fri Jan 13 15:51:01 2012 +0200
@@ -1,5 +1,5 @@
 /*
- * $FreeBSD: head/lib/libc/stdlib/Symbol.map 228322 2011-12-07 15:25:48Z theraven $
+ * $FreeBSD: head/lib/libc/stdlib/Symbol.map 229848 2012-01-09 06:36:28Z ed $
  */
 
 FBSD_1.0 {
@@ -93,18 +93,19 @@
 };
 
 FBSD_1.3 {
+	aligned_alloc;
+	at_quick_exit;
 	atof_l;
 	atoi_l;
 	atol_l;
 	atoll_l;
-	at_quick_exit;
 	quick_exit;
 	strtod_l;
-	strtol_l;
-	strtoll_l;
 	strtof_l;
 	strtoimax_l;
+	strtol_l;
 	strtold_l;
+	strtoll_l;
 	strtoq_l;
 	strtoul_l;
 	strtoull_l;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/lib/libc/stdlib/aligned_alloc.3
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/lib/libc/stdlib/aligned_alloc.3	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,126 @@
+.\" Copyright (C) 2006 Jason Evans <jasone at FreeBSD.org>.
+.\" All rights reserved.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\"    notice(s), this list of conditions and the following disclaimer as
+.\"    the first lines of this file unmodified other than the possible
+.\"    addition of one or more copyright notices.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\"    notice(s), this list of conditions and the following disclaimer in
+.\"    the documentation and/or other materials provided with the
+.\"    distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
+.\" EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
+.\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+.\" BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+.\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+.\" OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+.\" EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+.\"
+.\" $FreeBSD: head/lib/libc/stdlib/aligned_alloc.3 229848 2012-01-09 06:36:28Z ed $
+.\"
+.Dd January 7, 2011
+.Dt ALIGNED_ALLOC 3
+.Os
+.Sh NAME
+.Nm aligned_alloc ,
+.Nm posix_memalign
+.Nd aligned memory allocation
+.Sh LIBRARY
+.Lb libc
+.Sh SYNOPSIS
+.In stdlib.h
+.Ft void *
+.Fn aligned_alloc "size_t alignment" "size_t size"
+.Ft int
+.Fn posix_memalign "void **ptr" "size_t alignment" "size_t size"
+.Sh DESCRIPTION
+The
+.Fn aligned_alloc
+and
+.Fn posix_memalign
+functions allocate
+.Fa size
+bytes of memory such that the allocation's base address is an even multiple of
+.Fa alignment .
+The
+.Fn aligned_alloc
+function returns the allocation, while the
+.Fn posix_memalign
+function stores the allocation in the value pointed to by
+.Fa ptr .
+.Pp
+The requested
+.Fa alignment
+must be a power of 2 at least as large as
+.Fn sizeof "void *" .
+.Pp
+Memory that is allocated via
+.Fn aligned_alloc
+and
+.Fn posix_memalign
+can be used as an argument in subsequent calls to
+.Xr realloc 3 ,
+.Xr reallocf 3 ,
+and
+.Xr free 3 .
+.Sh RETURN VALUES
+The
+.Fn aligned_alloc
+function returns a pointer to the allocation if successful; otherwise a
+NULL pointer is returned and
+.Va errno
+is set to an error value.
+.Pp
+The
+.Fn posix_memalign
+function returns the value 0 if successful; otherwise it returns an error value.
+.Sh ERRORS
+The
+.Fn aligned_alloc
+and
+.Fn posix_memalign
+functions will fail if:
+.Bl -tag -width Er
+.It Bq Er EINVAL
+The
+.Fa alignment
+parameter is not a power of 2 at least as large as
+.Fn sizeof "void *" .
+.It Bq Er ENOMEM
+Memory allocation error.
+.El
+.Sh SEE ALSO
+.Xr free 3 ,
+.Xr malloc 3 ,
+.Xr realloc 3 ,
+.Xr reallocf 3 ,
+.Xr valloc 3
+.Sh STANDARDS
+The
+.Fn aligned_alloc
+function conforms to
+.St -isoC-2011 .
+.Pp
+The
+.Fn posix_memalign
+function conforms to
+.St -p1003.1-2001 .
+.Sh HISTORY
+The
+.Fn posix_memalign
+function first appeared in
+.Fx 7.0 .
+.Pp
+The
+.Fn aligned_alloc
+function first appeared in
+.Fx 10.0 .
diff -r ebe87ce4d7dd -r 1750de5b0399 head/lib/libc/stdlib/malloc.c
--- a/head/lib/libc/stdlib/malloc.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/lib/libc/stdlib/malloc.c	Fri Jan 13 15:51:01 2012 +0200
@@ -158,7 +158,7 @@
 #define	MALLOC_DSS
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/lib/libc/stdlib/malloc.c 228540 2011-12-15 20:40:11Z dim $");
+__FBSDID("$FreeBSD: head/lib/libc/stdlib/malloc.c 229848 2012-01-09 06:36:28Z ed $");
 
 #include "libc_private.h"
 #ifdef MALLOC_DEBUG
@@ -6043,6 +6043,20 @@
 }
 
 void *
+aligned_alloc(size_t alignment, size_t size)
+{
+	void *memptr;
+	int ret;
+
+	ret = posix_memalign(&memptr, alignment, size);
+	if (ret != 0) {
+		errno = ret;
+		return (NULL);
+	}
+	return (memptr);
+}
+
+void *
 calloc(size_t num, size_t size)
 {
 	void *ret;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/lib/libc/stdlib/posix_memalign.3
--- a/head/lib/libc/stdlib/posix_memalign.3	Fri Jan 13 00:58:58 2012 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,96 +0,0 @@
-.\" Copyright (C) 2006 Jason Evans <jasone at FreeBSD.org>.
-.\" All rights reserved.
-.\"
-.\" Redistribution and use in source and binary forms, with or without
-.\" modification, are permitted provided that the following conditions
-.\" are met:
-.\" 1. Redistributions of source code must retain the above copyright
-.\"    notice(s), this list of conditions and the following disclaimer as
-.\"    the first lines of this file unmodified other than the possible
-.\"    addition of one or more copyright notices.
-.\" 2. Redistributions in binary form must reproduce the above copyright
-.\"    notice(s), this list of conditions and the following disclaimer in
-.\"    the documentation and/or other materials provided with the
-.\"    distribution.
-.\"
-.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
-.\" EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
-.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
-.\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
-.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
-.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
-.\" BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
-.\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
-.\" OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
-.\" EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-.\"
-.\" $FreeBSD$
-.\"
-.Dd January 11, 2006
-.Dt POSIX_MEMALIGN 3
-.Os
-.Sh NAME
-.Nm posix_memalign
-.Nd aligned memory allocation
-.Sh LIBRARY
-.Lb libc
-.Sh SYNOPSIS
-.In stdlib.h
-.Ft int
-.Fn posix_memalign "void **ptr" "size_t alignment" "size_t size"
-.Sh DESCRIPTION
-The
-.Fn posix_memalign
-function allocates
-.Fa size
-bytes of memory such that the allocation's base address is an even multiple of
-.Fa alignment ,
-and returns the allocation in the value pointed to by
-.Fa ptr .
-.Pp
-The requested
-.Fa alignment
-must be a power of 2 at least as large as
-.Fn sizeof "void *" .
-.Pp
-Memory that is allocated via
-.Fn posix_memalign
-can be used as an argument in subsequent calls to
-.Xr realloc 3 ,
-.Xr reallocf 3 ,
-and
-.Xr free 3 .
-.Sh RETURN VALUES
-The
-.Fn posix_memalign
-function returns the value 0 if successful; otherwise it returns an error value.
-.Sh ERRORS
-The
-.Fn posix_memalign
-function will fail if:
-.Bl -tag -width Er
-.It Bq Er EINVAL
-The
-.Fa alignment
-parameter is not a power of 2 at least as large as
-.Fn sizeof "void *" .
-.It Bq Er ENOMEM
-Memory allocation error.
-.El
-.Sh SEE ALSO
-.Xr free 3 ,
-.Xr malloc 3 ,
-.Xr realloc 3 ,
-.Xr reallocf 3 ,
-.Xr valloc 3
-.Sh STANDARDS
-The
-.Fn posix_memalign
-function conforms to
-.St -p1003.1-2001 .
-.Sh HISTORY
-The
-.Fn posix_memalign
-function first appeared in
-.Fx 7.0 .
diff -r ebe87ce4d7dd -r 1750de5b0399 head/lib/libdevstat/devstat.c
--- a/head/lib/libdevstat/devstat.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/lib/libdevstat/devstat.c	Fri Jan 13 15:51:01 2012 +0200
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/lib/libdevstat/devstat.c 220319 2011-04-04 09:25:27Z pluknet $");
+__FBSDID("$FreeBSD: head/lib/libdevstat/devstat.c 229735 2012-01-06 21:28:29Z ghelmer $");
 
 #include <sys/types.h>
 #include <sys/sysctl.h>
@@ -365,6 +365,12 @@
 			dssize = (dinfo->numdevs * sizeof(struct devstat)) +
 				 sizeof(long);
 			dinfo->mem_ptr = (u_int8_t *)malloc(dssize);
+			if (dinfo->mem_ptr == NULL) {
+				snprintf(devstat_errbuf, sizeof(devstat_errbuf),
+					 "%s: Cannot allocate memory for mem_ptr element",
+					 __func__);
+				return(-1);
+			}
 		} else
 			dssize = (dinfo->numdevs * sizeof(struct devstat)) +
 				 sizeof(long);
@@ -567,7 +573,7 @@
 	 * either enlarge or reduce the size of the device selection list.
 	 */
 	} else if (*num_selections != numdevs) {
-		*dev_select = (struct device_selection *)realloc(*dev_select,
+		*dev_select = (struct device_selection *)reallocf(*dev_select,
 			numdevs * sizeof(struct device_selection));
 		*select_generation = current_generation;
 		init_selections = 1;
@@ -581,6 +587,13 @@
 		init_selections = 1;
 	}
 
+	if (*dev_select == NULL) {
+		snprintf(devstat_errbuf, sizeof(devstat_errbuf),
+			 "%s: Cannot (re)allocate memory for dev_select argument",
+			 __func__);
+		return(-1);
+	}
+
 	/*
 	 * If we're in "only" mode, we want to clear out the selected
 	 * variable since we're going to select exactly what the user wants
@@ -608,6 +621,12 @@
 	 || (perf_select != 0)) && (changed == 0)){
 		old_dev_select = (struct device_selection *)malloc(
 		    *num_selections * sizeof(struct device_selection));
+		if (old_dev_select == NULL) {
+			snprintf(devstat_errbuf, sizeof(devstat_errbuf),
+				 "%s: Cannot allocate memory for selection list backup",
+				 __func__);
+			return(-1);
+		}
 		old_num_selections = *num_selections;
 		bcopy(*dev_select, old_dev_select, 
 		    sizeof(struct device_selection) * *num_selections);
@@ -1028,16 +1047,17 @@
 		return(-1);
 	}
 
-	/*
-	 * Since you can't realloc a pointer that hasn't been malloced
-	 * first, we malloc first and then realloc.
-	 */
 	if (*num_matches == 0)
-		*matches = (struct devstat_match *)malloc(
-			   sizeof(struct devstat_match));
-	else
-		*matches = (struct devstat_match *)realloc(*matches,
-			  sizeof(struct devstat_match) * (*num_matches + 1));
+		*matches = NULL;
+
+	*matches = (struct devstat_match *)reallocf(*matches,
+		  sizeof(struct devstat_match) * (*num_matches + 1));
+
+	if (*matches == NULL) {
+		snprintf(devstat_errbuf, sizeof(devstat_errbuf),
+			 "%s: Cannot allocate memory for matches list", __func__);
+		return(-1);
+	}
 			  
 	/* Make sure the current entry is clear */
 	bzero(&matches[0][*num_matches], sizeof(struct devstat_match));
diff -r ebe87ce4d7dd -r 1750de5b0399 head/lib/libgssapi/gss_display_status.c
--- a/head/lib/libgssapi/gss_display_status.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/lib/libgssapi/gss_display_status.c	Fri Jan 13 15:51:01 2012 +0200
@@ -23,10 +23,10 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- *	$FreeBSD$
+ *	$FreeBSD: head/lib/libgssapi/gss_display_status.c 229784 2012-01-07 16:13:56Z uqs $
  */
 /*
- * Copyright (c) 1998 - 2005 Kungliga Tekniska Högskolan
+ * Copyright (c) 1998 - 2005 Kungliga Tekniska Högskolan
  * (Royal Institute of Technology, Stockholm, Sweden). 
  * All rights reserved. 
  *
@@ -58,7 +58,7 @@
  * SUCH DAMAGE. 
  */
 /*
- * Copyright (c) 1998 - 2005 Kungliga Tekniska Högskolan
+ * Copyright (c) 1998 - 2005 Kungliga Tekniska Högskolan
  * (Royal Institute of Technology, Stockholm, Sweden). 
  * All rights reserved. 
  *
diff -r ebe87ce4d7dd -r 1750de5b0399 head/lib/libgssapi/gss_pseudo_random.c
--- a/head/lib/libgssapi/gss_pseudo_random.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/lib/libgssapi/gss_pseudo_random.c	Fri Jan 13 15:51:01 2012 +0200
@@ -1,5 +1,5 @@
-/*
- * Copyright (c) 2007 Kungliga Tekniska Högskolan
+/*-
+ * Copyright (c) 2007 Kungliga Tekniska Högskolan
  * (Royal Institute of Technology, Stockholm, Sweden). 
  * All rights reserved. 
  *
@@ -30,7 +30,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
  * SUCH DAMAGE. 
  */
-/* $FreeBSD$ */
+/* $FreeBSD: head/lib/libgssapi/gss_pseudo_random.c 229784 2012-01-07 16:13:56Z uqs $ */
 /* $Id: gss_pseudo_random.c 20053 2007-01-24 01:31:35Z lha $ */
 
 #include <gssapi/gssapi.h>
diff -r ebe87ce4d7dd -r 1750de5b0399 head/lib/libgssapi/gss_release_oid.c
--- a/head/lib/libgssapi/gss_release_oid.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/lib/libgssapi/gss_release_oid.c	Fri Jan 13 15:51:01 2012 +0200
@@ -1,5 +1,5 @@
 /*-
- * Copyright (c) 2006 Kungliga Tekniska Högskolan
+ * Copyright (c) 2006 Kungliga Tekniska Högskolan
  * (Royal Institute of Technology, Stockholm, Sweden). 
  * All rights reserved. 
  *
@@ -30,7 +30,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
  * SUCH DAMAGE. 
  */
-/* $FreeBSD$ */
+/* $FreeBSD: head/lib/libgssapi/gss_release_oid.c 229784 2012-01-07 16:13:56Z uqs $ */
 
 #include <gssapi/gssapi.h>
 #include <stdlib.h>
diff -r ebe87ce4d7dd -r 1750de5b0399 head/lib/libopie/config.h
--- a/head/lib/libopie/config.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/lib/libopie/config.h	Fri Jan 13 15:51:01 2012 +0200
@@ -1,4 +1,4 @@
-/* $FreeBSD$ */
+/* $FreeBSD: head/lib/libopie/config.h 229831 2012-01-09 00:48:19Z eadler $ */
 /* config.h.  Generated automatically by configure.  */
 /* config.h.in.  Generated automatically from configure.in by autoheader.  */
 
@@ -54,7 +54,7 @@
 /* #undef DOANONYMOUS */
 
 /* The default value of the PATH environment variable */
-#define DEFAULT_PATH "/usr/bin:/bin:/usr/sbin:/sbin:/usr/X11R6/bin"
+#define DEFAULT_PATH "/usr/bin:/bin:/usr/sbin:/sbin"
 
 /* Defined if the file /etc/default/login exists 
    (and, presumably, should be looked at by login) */
diff -r ebe87ce4d7dd -r 1750de5b0399 head/lib/libstdthreads/thrd_create.3
--- a/head/lib/libstdthreads/thrd_create.3	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/lib/libstdthreads/thrd_create.3	Fri Jan 13 15:51:01 2012 +0200
@@ -22,7 +22,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.\" $FreeBSD: head/lib/libstdthreads/thrd_create.3 228904 2011-12-26 21:51:53Z ed $
+.\" $FreeBSD: head/lib/libstdthreads/thrd_create.3 229651 2012-01-05 21:36:12Z uqs $
 .\"
 .Dd December 26, 2011
 .Dt THRD_CREATE 3
@@ -110,7 +110,7 @@
 .Fn tss_set "tss_t key" "void *val"
 .Sh DESCRIPTION
 As of
-.St -isoC-11 ,
+.St -isoC-2011 ,
 the C standard includes an API for writing multithreaded applications.
 Since POSIX.1 already includes a threading API that is used by virtually
 any multithreaded application, the interface provided by the C standard
@@ -252,7 +252,7 @@
 .Xr pthread 3
 .Sh STANDARDS
 These functions are expected to conform to
-.St -isoC-11 .
+.St -isoC-2011 .
 .Sh HISTORY
 These functions appeared in
 .Fx 10.0 .
diff -r ebe87ce4d7dd -r 1750de5b0399 head/lib/libutil/Makefile
--- a/head/lib/libutil/Makefile	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/lib/libutil/Makefile	Fri Jan 13 15:51:01 2012 +0200
@@ -1,5 +1,5 @@
 #	@(#)Makefile	8.1 (Berkeley) 6/4/93
-#	$FreeBSD: head/lib/libutil/Makefile 221807 2011-05-12 10:11:39Z stas $
+#	$FreeBSD: head/lib/libutil/Makefile 229943 2012-01-10 22:22:35Z ghelmer $
 
 SHLIBDIR?= /lib
 
@@ -58,7 +58,8 @@
 MLINKS+=pidfile.3 pidfile_open.3 \
 	pidfile.3 pidfile_write.3 \
 	pidfile.3 pidfile_close.3 \
-	pidfile.3 pidfile_remove.3
+	pidfile.3 pidfile_remove.3 \
+	pidfile.3 pidfile_fileno.3
 MLINKS+=quotafile.3 quota_open.3 \
 	quotafile.3 quota_fsname.3 \
 	quotafile.3 quota_qfname.3 \
diff -r ebe87ce4d7dd -r 1750de5b0399 head/lib/libutil/flopen.3
--- a/head/lib/libutil/flopen.3	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/lib/libutil/flopen.3	Fri Jan 13 15:51:01 2012 +0200
@@ -1,5 +1,5 @@
 .\"-
-.\" Copyright (c) 2007 Dag-Erling Coïdan Smørgrav
+.\" Copyright (c) 2007 Dag-Erling Coïdan Smørgrav
 .\" All rights reserved.
 .\"
 .\" Redistribution and use in source and binary forms, with or without
@@ -23,7 +23,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.\" $FreeBSD$
+.\" $FreeBSD: head/lib/libutil/flopen.3 229784 2012-01-07 16:13:56Z uqs $
 .\"
 .Dd June 6, 2009
 .Dt FLOPEN 3
diff -r ebe87ce4d7dd -r 1750de5b0399 head/lib/libutil/kld.3
--- a/head/lib/libutil/kld.3	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/lib/libutil/kld.3	Fri Jan 13 15:51:01 2012 +0200
@@ -1,5 +1,5 @@
 .\"-
-.\" Copyright (c) 2006 Dag-Erling Coïdan Smørgrav
+.\" Copyright (c) 2006 Dag-Erling Coïdan Smørgrav
 .\" All rights reserved.
 .\"
 .\" Redistribution and use in source and binary forms, with or without
@@ -23,7 +23,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.\" $FreeBSD$
+.\" $FreeBSD: head/lib/libutil/kld.3 229784 2012-01-07 16:13:56Z uqs $
 .\"
 .Dd February 18, 2006
 .Dt KLD 3
diff -r ebe87ce4d7dd -r 1750de5b0399 head/lib/libutil/libutil.h
--- a/head/lib/libutil/libutil.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/lib/libutil/libutil.h	Fri Jan 13 15:51:01 2012 +0200
@@ -33,7 +33,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $FreeBSD: head/lib/libutil/libutil.h 229572 2012-01-05 10:40:24Z bapt $
+ * $FreeBSD: head/lib/libutil/libutil.h 229988 2012-01-11 22:45:15Z ghelmer $
  */
 
 #ifndef _LIBUTIL_H_
@@ -84,45 +84,27 @@
 #endif
 
 /* Avoid pulling in all the include files for no need */
-struct termios;
-struct winsize;
 struct in_addr;
 struct kinfo_file;
 struct kinfo_proc;
 struct kinfo_vmentry;
+struct sockaddr;
+struct termios;
+struct winsize;
 
 __BEGIN_DECLS
+char	*auth_getval(const char *_name);
 void	clean_environment(const char * const *_white,
 	    const char * const *_more_white);
+int	expand_number(const char *_buf, uint64_t *_num);
 int	extattr_namespace_to_string(int _attrnamespace, char **_string);
 int	extattr_string_to_namespace(const char *_string, int *_attrnamespace);
 int	flopen(const char *_path, int _flags, ...);
-void	hexdump(const void *ptr, int length, const char *hdr, int flags);
-int	login_tty(int _fd);
-void	trimdomain(char *_fullhost, int _hostsize);
-int	openpty(int *_amaster, int *_aslave, char *_name,
-		     struct termios *_termp, struct winsize *_winp);
 int	forkpty(int *_amaster, char *_name,
-		     struct termios *_termp, struct winsize *_winp);
+	    struct termios *_termp, struct winsize *_winp);
+void	hexdump(const void *_ptr, int _length, const char *_hdr, int _flags);
 int	humanize_number(char *_buf, size_t _len, int64_t _number,
 	    const char *_suffix, int _scale, int _flags);
-int	expand_number(const char *_buf, uint64_t *_num);
-const char *uu_lockerr(int _uu_lockresult);
-int	uu_lock(const char *_ttyname);
-int	uu_unlock(const char *_ttyname);
-int	uu_lock_txfr(const char *_ttyname, pid_t _pid);
-int	_secure_path(const char *_path, uid_t _uid, gid_t _gid);
-properties properties_read(int fd);
-void	properties_free(properties list);
-char	*property_find(properties list, const char *name);
-char	*auth_getval(const char *name);
-int	realhostname(char *host, size_t hsize, const struct in_addr *ip);
-struct sockaddr;
-int	realhostname_sa(char *host, size_t hsize, struct sockaddr *addr,
-			     int addrlen);
-
-int	kld_isloaded(const char *name);
-int	kld_load(const char *name);
 struct kinfo_file *
 	kinfo_getfile(pid_t _pid, int *_cntp);
 struct kinfo_vmentry *
@@ -131,9 +113,29 @@
 	kinfo_getallproc(int *_cntp);
 struct kinfo_proc *
 	kinfo_getproc(pid_t _pid);
+int	kld_isloaded(const char *_name);
+int	kld_load(const char *_name);
+int	login_tty(int _fd);
+int	openpty(int *_amaster, int *_aslave, char *_name,
+	    struct termios *_termp, struct winsize *_winp);
+void	properties_free(properties _list);
+char	*property_find(properties _list, const char *_name);
+properties
+	properties_read(int fd);
+int	realhostname(char *_host, size_t _hsize, const struct in_addr *_ip);
+int	realhostname_sa(char *_host, size_t _hsize, struct sockaddr *_addr,
+	    int _addrlen);
+int	_secure_path(const char *_path, uid_t _uid, gid_t _gid);
+void	trimdomain(char *_fullhost, int _hostsize);
+const char *
+	uu_lockerr(int _uu_lockresult);
+int	uu_lock(const char *_ttyname);
+int	uu_unlock(const char *_ttyname);
+int	uu_lock_txfr(const char *_ttyname, pid_t _pid);
 
 #ifdef _STDIO_H_	/* avoid adding new includes */
-char   *fparseln(FILE *, size_t *, size_t *, const char[3], int);
+char	*fparseln(FILE *_fp, size_t *_len, size_t *_lineno,
+	    const char _delim[3], int _flags);
 #endif
 
 #ifdef _PWD_H_
@@ -154,39 +156,44 @@
 
 #ifdef _GRP_H_
 int 	gr_copy(int __ffd, int _tfd, const struct group *_gr, struct group *_old_gr);
-struct group *gr_dup(const struct group *gr);
-int	gr_equal(const struct group *gr1, const struct group *gr2);
+struct group *gr_dup(const struct group *_gr);
+int	gr_equal(const struct group *_gr1, const struct group *_gr2);
 void	gr_fini(void);
 int	gr_init(const char *_dir, const char *_master);
 int	gr_lock(void);
-char	*gr_make(const struct group *gr);
+char	*gr_make(const struct group *_gr);
 int	gr_mkdb(void);
+struct group *gr_scan(const char *_line);
 int	gr_tmp(int _mdf);
-struct group *gr_scan(const char *line);
 #endif
 
 #ifdef _SYS_PARAM_H_
-struct pidfh *pidfile_open(const char *path, mode_t mode, pid_t *pidptr);
-int pidfile_write(struct pidfh *pfh);
-int pidfile_close(struct pidfh *pfh);
-int pidfile_remove(struct pidfh *pfh);
+int	pidfile_close(struct pidfh *_pfh);
+int	pidfile_fileno(const struct pidfh *_pfh);
+struct pidfh *
+	pidfile_open(const char *_path, mode_t _mode, pid_t *_pidptr);
+int	pidfile_remove(struct pidfh *_pfh);
+int	pidfile_write(struct pidfh *_pfh);
 #endif
 
 #ifdef _UFS_UFS_QUOTA_H_
+struct fstab;
 struct quotafile;
-struct fstab;
-struct quotafile *quota_open(struct fstab *, int, int);
-void quota_close(struct quotafile *);
-int quota_on(struct quotafile *);
-int quota_off(struct quotafile *);
-const char *quota_fsname(const struct quotafile *);
-const char *quota_qfname(const struct quotafile *);
-int quota_maxid(struct quotafile *);
-int quota_check_path(const struct quotafile *, const char *path);
-int quota_read(struct quotafile *, struct dqblk *, int);
-int quota_write_limits(struct quotafile *, struct dqblk *, int);
-int quota_write_usage(struct quotafile *, struct dqblk *, int);
-int quota_convert(struct quotafile *, int);
+int	quota_check_path(const struct quotafile *_qf, const char *_path);
+void	quota_close(struct quotafile *_qf);
+int	quota_convert(struct quotafile *_qf, int _wordsize);
+const char *
+	quota_fsname(const struct quotafile *_qf);
+int	quota_maxid(struct quotafile *_qf);
+int	quota_off(struct quotafile *_qf);
+int	quota_on(struct quotafile *_qf);
+struct quotafile *
+	quota_open(struct fstab *_fs, int _quotatype, int _openflags);
+const char *
+	quota_qfname(const struct quotafile *_qf);
+int	quota_read(struct quotafile *_qf, struct dqblk *_dqb, int _id);
+int	quota_write_limits(struct quotafile *_qf, struct dqblk *_dqb, int _id);
+int	quota_write_usage(struct quotafile *_qf, struct dqblk *_dqb, int _id);
 #endif
 
 __END_DECLS
diff -r ebe87ce4d7dd -r 1750de5b0399 head/lib/libutil/pidfile.3
--- a/head/lib/libutil/pidfile.3	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/lib/libutil/pidfile.3	Fri Jan 13 15:51:01 2012 +0200
@@ -22,7 +22,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.\" $FreeBSD: head/lib/libutil/pidfile.3 226450 2011-10-16 21:30:15Z pjd $
+.\" $FreeBSD: head/lib/libutil/pidfile.3 229942 2012-01-10 21:47:58Z ghelmer $
 .\"
 .Dd October 16, 2011
 .Dt PIDFILE 3
@@ -46,6 +46,8 @@
 .Fn pidfile_close "struct pidfh *pfh"
 .Ft int
 .Fn pidfile_remove "struct pidfh *pfh"
+.Ft int
+.Fn pidfile_fileno "struct pidfh *pfh"
 .Sh DESCRIPTION
 The
 .Nm pidfile
@@ -77,6 +79,9 @@
 .Dv NULL ,
 .Pa /var/run/ Ns Ao Va progname Ac Ns Pa .pid
 file will be used.
+The
+.Fn pidfile_open
+function sets the FD_CLOEXEC close-on-exec flag on the open file descriptor.
 .Pp
 The
 .Fn pidfile_write
@@ -92,6 +97,10 @@
 The
 .Fn pidfile_remove
 function closes and removes a pidfile.
+.Pp
+The
+.Fn pidfile_fileno
+function returns the file descriptor for the open pidfile.
 .Sh RETURN VALUES
 The
 .Fn pidfile_open
@@ -105,15 +114,27 @@
 will be set.
 .Pp
 .Rv -std pidfile_write pidfile_close pidfile_remove
+.Pp
+The
+.Fn pidfile_fileno
+function returns the low-level file descriptor.
+It returns
+.Li -1
+and sets
+.Va errno
+if a NULL
+.Vt pidfh
+is specified, or if the pidfile is no longer open.
 .Sh EXAMPLES
 The following example shows in which order these functions should be used.
 Note that it is safe to pass
 .Dv NULL
 to
 .Fn pidfile_write ,
-.Fn pidfile_remove
+.Fn pidfile_remove ,
+.Fn pidfile_close
 and
-.Fn pidfile_close
+.Fn pidfile_fileno
 functions.
 .Bd -literal
 struct pidfh *pfh;
@@ -184,6 +205,7 @@
 function may also fail and set
 .Va errno
 for any errors specified for the
+.Xr fcntl 2 ,
 .Xr fstat 2 ,
 .Xr open 2 ,
 and
@@ -244,6 +266,16 @@
 system calls and the
 .Xr flopen 3
 library function.
+.Pp
+The
+.Fn pidfile_fileno
+function will fail if:
+.Bl -tag -width Er
+.It Bq Er EDOOFUS
+Improper function use.
+Probably called not from the process which used
+.Fn pidfile_open .
+.El
 .Sh SEE ALSO
 .Xr open 2 ,
 .Xr daemon 3 ,
diff -r ebe87ce4d7dd -r 1750de5b0399 head/lib/libutil/pidfile.c
--- a/head/lib/libutil/pidfile.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/lib/libutil/pidfile.c	Fri Jan 13 15:51:01 2012 +0200
@@ -25,7 +25,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/lib/libutil/pidfile.c 226450 2011-10-16 21:30:15Z pjd $");
+__FBSDID("$FreeBSD: head/lib/libutil/pidfile.c 229966 2012-01-11 16:35:26Z ghelmer $");
 
 #include <sys/param.h>
 #include <sys/file.h>
@@ -44,7 +44,7 @@
 static int _pidfile_remove(struct pidfh *pfh, int freeit);
 
 static int
-pidfile_verify(struct pidfh *pfh)
+pidfile_verify(const struct pidfh *pfh)
 {
 	struct stat sb;
 
@@ -117,7 +117,7 @@
 	 * pidfile_write() can be called multiple times.
 	 */
 	fd = flopen(pfh->pf_path,
-	    O_WRONLY | O_CREAT | O_TRUNC | O_NONBLOCK, mode);
+	    O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NONBLOCK, mode);
 	if (fd == -1) {
 		if (errno == EWOULDBLOCK && pidptr != NULL) {
 			count = 20;
@@ -137,6 +137,7 @@
 		free(pfh);
 		return (NULL);
 	}
+
 	/*
 	 * Remember file information, so in pidfile_write() we are sure we write
 	 * to the proper descriptor.
@@ -252,3 +253,14 @@
 
 	return (_pidfile_remove(pfh, 1));
 }
+
+int
+pidfile_fileno(const struct pidfh *pfh)
+{
+
+	if (pfh == NULL || pfh->pf_fd == -1) {
+		errno = EDOOFUS;
+		return (-1);
+	}
+	return (pfh->pf_fd);
+}
diff -r ebe87ce4d7dd -r 1750de5b0399 head/lib/libvgl/bitmap.c
--- a/head/lib/libvgl/bitmap.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/lib/libvgl/bitmap.c	Fri Jan 13 15:51:01 2012 +0200
@@ -1,5 +1,5 @@
 /*-
- * Copyright (c) 1991-1997 Søren Schmidt
+ * Copyright (c) 1991-1997 Søren Schmidt
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
+__FBSDID("$FreeBSD: head/lib/libvgl/bitmap.c 229784 2012-01-07 16:13:56Z uqs $");
 
 #include <sys/types.h>
 #include <signal.h>
diff -r ebe87ce4d7dd -r 1750de5b0399 head/lib/libvgl/keyboard.c
--- a/head/lib/libvgl/keyboard.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/lib/libvgl/keyboard.c	Fri Jan 13 15:51:01 2012 +0200
@@ -1,5 +1,5 @@
 /*-
- * Copyright (c) 1997 Søren Schmidt
+ * Copyright (c) 1997 Søren Schmidt
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
+__FBSDID("$FreeBSD: head/lib/libvgl/keyboard.c 229784 2012-01-07 16:13:56Z uqs $");
 
 #include <stdio.h>
 #include <sys/types.h>
diff -r ebe87ce4d7dd -r 1750de5b0399 head/lib/libvgl/main.c
--- a/head/lib/libvgl/main.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/lib/libvgl/main.c	Fri Jan 13 15:51:01 2012 +0200
@@ -1,5 +1,5 @@
 /*-
- * Copyright (c) 1991-1997 Søren Schmidt
+ * Copyright (c) 1991-1997 Søren Schmidt
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
+__FBSDID("$FreeBSD: head/lib/libvgl/main.c 229784 2012-01-07 16:13:56Z uqs $");
 
 #include <stdio.h>
 #include <sys/types.h>
diff -r ebe87ce4d7dd -r 1750de5b0399 head/lib/libvgl/mouse.c
--- a/head/lib/libvgl/mouse.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/lib/libvgl/mouse.c	Fri Jan 13 15:51:01 2012 +0200
@@ -1,5 +1,5 @@
 /*-
- * Copyright (c) 1991-1997 Søren Schmidt
+ * Copyright (c) 1991-1997 Søren Schmidt
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
+__FBSDID("$FreeBSD: head/lib/libvgl/mouse.c 229784 2012-01-07 16:13:56Z uqs $");
 
 #include <stdio.h>
 #include <sys/types.h>
diff -r ebe87ce4d7dd -r 1750de5b0399 head/lib/libvgl/simple.c
--- a/head/lib/libvgl/simple.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/lib/libvgl/simple.c	Fri Jan 13 15:51:01 2012 +0200
@@ -1,5 +1,5 @@
 /*-
- * Copyright (c) 1991-1997 Søren Schmidt
+ * Copyright (c) 1991-1997 Søren Schmidt
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/lib/libvgl/simple.c 229516 2012-01-04 20:05:38Z pfg $");
+__FBSDID("$FreeBSD: head/lib/libvgl/simple.c 229784 2012-01-07 16:13:56Z uqs $");
 
 #include <signal.h>
 #include <sys/fbio.h>
diff -r ebe87ce4d7dd -r 1750de5b0399 head/lib/libvgl/text.c
--- a/head/lib/libvgl/text.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/lib/libvgl/text.c	Fri Jan 13 15:51:01 2012 +0200
@@ -1,5 +1,5 @@
 /*-
- * Copyright (c) 1991-1997 Søren Schmidt
+ * Copyright (c) 1991-1997 Søren Schmidt
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
+__FBSDID("$FreeBSD: head/lib/libvgl/text.c 229784 2012-01-07 16:13:56Z uqs $");
 
 #include <stdio.h>
 #include <sys/fbio.h>
diff -r ebe87ce4d7dd -r 1750de5b0399 head/lib/libvgl/vgl.3
--- a/head/lib/libvgl/vgl.3	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/lib/libvgl/vgl.3	Fri Jan 13 15:51:01 2012 +0200
@@ -1,4 +1,5 @@
-.\" Copyright (c) 1997 Søren Schmidt
+.\"-
+.\" Copyright (c) 1997 Søren Schmidt
 .\" All rights reserved.
 .\"
 .\" Redistribution and use in source and binary forms, with or without
@@ -24,7 +25,7 @@
 .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 .\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 .\"
-.\" $FreeBSD$
+.\" $FreeBSD: head/lib/libvgl/vgl.3 229784 2012-01-07 16:13:56Z uqs $
 .Dd November 7, 1999
 .Dt VGL 3
 .Os
diff -r ebe87ce4d7dd -r 1750de5b0399 head/lib/libvgl/vgl.h
--- a/head/lib/libvgl/vgl.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/lib/libvgl/vgl.h	Fri Jan 13 15:51:01 2012 +0200
@@ -1,5 +1,5 @@
 /*-
- * Copyright (c) 1991-1997 Søren Schmidt
+ * Copyright (c) 1991-1997 Søren Schmidt
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -25,7 +25,7 @@
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
- * $FreeBSD$
+ * $FreeBSD: head/lib/libvgl/vgl.h 229784 2012-01-07 16:13:56Z uqs $
  */
 
 #ifndef _VGL_H_
diff -r ebe87ce4d7dd -r 1750de5b0399 head/lib/msun/src/s_fabsl.c
--- a/head/lib/msun/src/s_fabsl.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/lib/msun/src/s_fabsl.c	Fri Jan 13 15:51:01 2012 +0200
@@ -1,5 +1,5 @@
 /*-
- * Copyright (c) 2003 Dag-Erling Coïdan Smørgrav
+ * Copyright (c) 2003 Dag-Erling Coïdan Smørgrav
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -25,7 +25,7 @@
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
- * $FreeBSD$
+ * $FreeBSD: head/lib/msun/src/s_fabsl.c 229784 2012-01-07 16:13:56Z uqs $
  */
 
 #include <math.h>
diff -r ebe87ce4d7dd -r 1750de5b0399 head/libexec/bootpd/bootpd.c
--- a/head/libexec/bootpd/bootpd.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/libexec/bootpd/bootpd.c	Fri Jan 13 15:51:01 2012 +0200
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
+__FBSDID("$FreeBSD: head/libexec/bootpd/bootpd.c 229780 2012-01-07 16:09:54Z uqs $");
 
 #include <sys/types.h>
 #include <sys/param.h>
@@ -663,7 +663,7 @@
 	bp->bp_op = BOOTREPLY;
 	if (bp->bp_ciaddr.s_addr == 0) {
 		/*
-		 * client doesnt know his IP address,
+		 * client doesn't know his IP address,
 		 * search by hardware address.
 		 */
 		if (debug > 1) {
@@ -1119,7 +1119,7 @@
 
 /*
  * This call checks read access to a file.  It returns 0 if the file given
- * by "path" exists and is publically readable.  A value of -1 is returned if
+ * by "path" exists and is publicly readable.  A value of -1 is returned if
  * access is not permitted or an error occurs.  Successful calls also
  * return the file size in bytes using the long pointer "filesize".
  *
diff -r ebe87ce4d7dd -r 1750de5b0399 head/libexec/bootpd/bootpgw/bootpgw.c
--- a/head/libexec/bootpd/bootpgw/bootpgw.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/libexec/bootpd/bootpgw/bootpgw.c	Fri Jan 13 15:51:01 2012 +0200
@@ -31,7 +31,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/libexec/bootpd/bootpgw/bootpgw.c 228581 2011-12-16 15:38:11Z dim $");
+__FBSDID("$FreeBSD: head/libexec/bootpd/bootpgw/bootpgw.c 229780 2012-01-07 16:09:54Z uqs $");
 
 #include <sys/types.h>
 #include <sys/param.h>
@@ -534,7 +534,7 @@
 	/* Has this packet hopped too many times? */
 	hops = bp->bp_hops;
 	if (++hops > maxhops) {
-		report(LOG_NOTICE, "reqest from %s reached hop limit",
+		report(LOG_NOTICE, "request from %s reached hop limit",
 			   inet_ntoa(recv_addr.sin_addr));
 		return;
 	}
diff -r ebe87ce4d7dd -r 1750de5b0399 head/libexec/bootpd/hash.h
--- a/head/libexec/bootpd/hash.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/libexec/bootpd/hash.h	Fri Jan 13 15:51:01 2012 +0200
@@ -25,7 +25,7 @@
 
 /*
  * Generalized hash table ADT
- * $FreeBSD$
+ * $FreeBSD: head/libexec/bootpd/hash.h 229780 2012-01-07 16:09:54Z uqs $
  *
  * Provides multiple, dynamically-allocated, variable-sized hash tables on
  * various data and keys.
@@ -81,7 +81,7 @@
  *
  *	    A generalized hash function, hash_HashFunction(), is included
  *	    with this package to make things a little easier.  It is not
- *	    guarenteed to use the best hash algorithm in existence. . . .
+ *	    guaranteed to use the best hash algorithm in existence. . . .
  */
 
 
diff -r ebe87ce4d7dd -r 1750de5b0399 head/libexec/bootpd/tools/bootptest/bootptest.c
--- a/head/libexec/bootpd/tools/bootptest/bootptest.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/libexec/bootpd/tools/bootptest/bootptest.c	Fri Jan 13 15:51:01 2012 +0200
@@ -34,7 +34,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
+__FBSDID("$FreeBSD: head/libexec/bootpd/tools/bootptest/bootptest.c 229780 2012-01-07 16:09:54Z uqs $");
 
 char *usage = "bootptest [-h] server-name [vendor-data-template-file]";
 
@@ -186,7 +186,7 @@
 			break;
 		switch (argv[0][1]) {
 
-		case 'f':				/* File name to reqest. */
+		case 'f':				/* File name to request. */
 			if (argc < 2)
 				goto error;
 			argc--; argv++;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/libexec/bootpd/tools/bootptest/print-bootp.c
--- a/head/libexec/bootpd/tools/bootptest/print-bootp.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/libexec/bootpd/tools/bootptest/print-bootp.c	Fri Jan 13 15:51:01 2012 +0200
@@ -23,7 +23,7 @@
  * This file was copied from tcpdump-2.1.1 and modified.
  * There is an e-mail list for tcpdump: <tcpdump at ee.lbl.gov>
  *
- * $FreeBSD: head/libexec/bootpd/tools/bootptest/print-bootp.c 228584 2011-12-16 15:55:43Z dim $
+ * $FreeBSD: head/libexec/bootpd/tools/bootptest/print-bootp.c 229780 2012-01-07 16:09:54Z uqs $
  */
 
 #include <stdio.h>
@@ -187,7 +187,7 @@
 
 /*
  * Option description data follows.
- * These are decribed in: RFC-1048, RFC-1395, RFC-1497, RFC-1533
+ * These are described in: RFC-1048, RFC-1395, RFC-1497, RFC-1533
  *
  * The first char of each option string encodes the data format:
  * ?: unknown
diff -r ebe87ce4d7dd -r 1750de5b0399 head/libexec/ftpd/ftpcmd.y
--- a/head/libexec/ftpd/ftpcmd.y	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/libexec/ftpd/ftpcmd.y	Fri Jan 13 15:51:01 2012 +0200
@@ -47,7 +47,7 @@
 #endif /* not lint */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
+__FBSDID("$FreeBSD: head/libexec/ftpd/ftpcmd.y 229780 2012-01-07 16:09:54Z uqs $");
 
 #include <sys/param.h>
 #include <sys/socket.h>
@@ -1718,7 +1718,7 @@
  * Avoid expanding to a pathname including '\r' or '\n' in order to
  * not disrupt the FTP protocol.
  * The expansion found must be unique.
- * Return the result as a malloced string, or NULL if an error occured.
+ * Return the result as a malloced string, or NULL if an error occurred.
  *
  * Problem: this production is used for all pathname
  * processing, but only gives a 550 error reply.
diff -r ebe87ce4d7dd -r 1750de5b0399 head/libexec/getty/init.c
--- a/head/libexec/getty/init.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/libexec/getty/init.c	Fri Jan 13 15:51:01 2012 +0200
@@ -36,7 +36,7 @@
 static char sccsid[] = "@(#)from: init.c	8.1 (Berkeley) 6/4/93";
 #endif
 static const char rcsid[] =
-  "$FreeBSD$";
+  "$FreeBSD: head/libexec/getty/init.c 229780 2012-01-07 16:09:54Z uqs $";
 #endif /* not lint */
 
 /*
@@ -64,7 +64,7 @@
 	{ "et", &omode.c_cc[VEOF] },	/* eof chatacter (eot) */
 	{ "pc", nullstr },		/* pad character */
 	{ "tt" },			/* terminal type */
-	{ "ev" },			/* enviroment */
+	{ "ev" },			/* environment */
 	{ "lo", loginprg },		/* login program */
 	{ "hn", hostname },		/* host name */
 	{ "he" },			/* host name edit */
diff -r ebe87ce4d7dd -r 1750de5b0399 head/libexec/lukemftpd/nbsd_pidfile.h
--- a/head/libexec/lukemftpd/nbsd_pidfile.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/libexec/lukemftpd/nbsd_pidfile.h	Fri Jan 13 15:51:01 2012 +0200
@@ -1,4 +1,4 @@
-/* $FreeBSD$ */
+/* $FreeBSD: head/libexec/lukemftpd/nbsd_pidfile.h 229780 2012-01-07 16:09:54Z uqs $ */
 
 #include <sys/stdint.h>
 #include <sysexits.h>
@@ -10,7 +10,7 @@
 	pid_t otherpid, childpid;
 
 	if (basename != NULL) {
-		errx(EX_USAGE, "Need to impliment NetBSD semantics.");
+		errx(EX_USAGE, "Need to implement NetBSD semantics.");
 	}
 
 	pfh = pidfile_open(basename, 0644, &otherpid);
diff -r ebe87ce4d7dd -r 1750de5b0399 head/libexec/rbootd/parseconf.c
--- a/head/libexec/rbootd/parseconf.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/libexec/rbootd/parseconf.c	Fri Jan 13 15:51:01 2012 +0200
@@ -49,7 +49,7 @@
 static const char sccsid[] = "@(#)parseconf.c	8.1 (Berkeley) 6/4/93";
 #endif
 static const char rcsid[] =
-  "$FreeBSD$";
+  "$FreeBSD: head/libexec/rbootd/parseconf.c 229780 2012-01-07 16:09:54Z uqs $";
 #endif /* not lint */
 
 #include <sys/param.h>
@@ -145,7 +145,7 @@
 		 */
 		if ((addr = ParseAddr(bcp)) == NULL) {
 			syslog(LOG_ERR,
-			       "ParseConfig: line %d: cant parse <%s>",
+			       "ParseConfig: line %d: can't parse <%s>",
 			       linecnt, bcp);
 			continue;
 		}
@@ -193,7 +193,7 @@
 			/*
 			 *  Restricted list of boot files were spec'd,
 			 *  however, none of them were found.  Since we
-			 *  apparently cant let them boot "just anything",
+			 *  apparently can't let them boot "just anything",
 			 *  the entire record is invalidated.
 			 */
 			if (i == 0) {
@@ -355,7 +355,7 @@
 
 	(void) closedir(dfd);			/* close BootDir */
 
-	if (i == 0)				/* cant find any boot files */
+	if (i == 0)				/* can't find any boot files */
 		syslog(LOG_ERR, "GetBootFiles: no boot files (%s)\n", BootDir);
 
 	return(i);
diff -r ebe87ce4d7dd -r 1750de5b0399 head/libexec/rbootd/rbootd.c
--- a/head/libexec/rbootd/rbootd.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/libexec/rbootd/rbootd.c	Fri Jan 13 15:51:01 2012 +0200
@@ -56,7 +56,7 @@
 #endif
 #endif /* not lint */
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
+__FBSDID("$FreeBSD: head/libexec/rbootd/rbootd.c 229780 2012-01-07 16:09:54Z uqs $");
 
 #include <sys/param.h>
 #include <sys/time.h>
@@ -231,7 +231,7 @@
 
 		r = rset;
 
-		if (RmpConns == NULL) {		/* timeout isnt necessary */
+		if (RmpConns == NULL) {		/* timeout isn't necessary */
 			nsel = select(maxfds, &r, NULL, NULL, NULL);
 		} else {
 			timeout.tv_sec = RMP_TIMEOUT;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/libexec/rbootd/rmpproto.c
--- a/head/libexec/rbootd/rmpproto.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/libexec/rbootd/rmpproto.c	Fri Jan 13 15:51:01 2012 +0200
@@ -49,7 +49,7 @@
 static const char sccsid[] = "@(#)rmpproto.c	8.1 (Berkeley) 6/4/93";
 #endif
 static const char rcsid[] =
-  "$FreeBSD$";
+  "$FreeBSD: head/libexec/rbootd/rmpproto.c 229780 2012-01-07 16:09:54Z uqs $";
 #endif /* not lint */
 
 #include <sys/param.h>
@@ -401,7 +401,7 @@
 	int madeconn = 0;
 
 	/*
-	 *  Find the old connection.  If one doesnt exist, create one only
+	 *  Find the old connection.  If one doesn't exist, create one only
 	 *  to return the error code.
 	 */
 	if ((oldconn = FindConn(rconn)) == NULL) {
@@ -515,7 +515,7 @@
 	struct rmp_packet *rpl;
 
 	/*
-	 *  If we cant find the connection, ignore the request.
+	 *  If we can't find the connection, ignore the request.
 	 */
 	if ((oldconn = FindConn(rconn)) == NULL) {
 		syslog(LOG_ERR, "BootDone: no existing connection (%s)",
diff -r ebe87ce4d7dd -r 1750de5b0399 head/libexec/revnetgroup/parse_netgroup.c
--- a/head/libexec/revnetgroup/parse_netgroup.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/libexec/revnetgroup/parse_netgroup.c	Fri Jan 13 15:51:01 2012 +0200
@@ -36,7 +36,7 @@
 
 #ifndef lint
 static const char rcsid[] =
-  "$FreeBSD$";
+  "$FreeBSD: head/libexec/revnetgroup/parse_netgroup.c 229780 2012-01-07 16:09:54Z uqs $";
 #endif /* not lint */
 
 /*
@@ -199,8 +199,8 @@
 	if (lp->l_parsed) {
 #ifdef DEBUG
 		/*
-		 * This error message is largely superflous since the
-		 * code handles the error condition sucessfully, and
+		 * This error message is largely superfluous since the
+		 * code handles the error condition successfully, and
 		 * spewing it out from inside libc can actually hose
 		 * certain programs.
 		 */
diff -r ebe87ce4d7dd -r 1750de5b0399 head/libexec/rshd/rshd.8
--- a/head/libexec/rshd/rshd.8	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/libexec/rshd/rshd.8	Fri Jan 13 15:51:01 2012 +0200
@@ -30,7 +30,7 @@
 .\" SUCH DAMAGE.
 .\"
 .\"     @(#)rshd.8	8.1 (Berkeley) 6/4/93
-.\" $FreeBSD$
+.\" $FreeBSD: head/libexec/rshd/rshd.8 229780 2012-01-07 16:09:54Z uqs $
 .\"
 .Dd June 4, 1993
 .Dt RSHD 8
@@ -148,7 +148,7 @@
 The options are as follows:
 .Bl -tag -width indent
 .It Fl a
-This flag is ignored, and is present for compatability purposes.
+This flag is ignored, and is present for compatibility purposes.
 .It Fl D
 Sets the TCP_NODELAY socket option, which improves the performance
 of small back-to-back writes at the expense of additional network
diff -r ebe87ce4d7dd -r 1750de5b0399 head/libexec/rtld-aout/shlib.c
--- a/head/libexec/rtld-aout/shlib.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/libexec/rtld-aout/shlib.c	Fri Jan 13 15:51:01 2012 +0200
@@ -27,7 +27,7 @@
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
- * $FreeBSD$
+ * $FreeBSD: head/libexec/rtld-aout/shlib.c 229780 2012-01-07 16:09:54Z uqs $
  */
 
 #include <sys/param.h>
@@ -159,7 +159,7 @@
 	if (i == n2)
 		return 1;
 
-	errx(1, "cmpndewey: cant happen");
+	errx(1, "cmpndewey: can't happen");
 	return 0;
 }
 
diff -r ebe87ce4d7dd -r 1750de5b0399 head/libexec/rtld-elf/Symbol.map
--- a/head/libexec/rtld-elf/Symbol.map	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/libexec/rtld-elf/Symbol.map	Fri Jan 13 15:51:01 2012 +0200
@@ -1,5 +1,5 @@
 /*
- * $FreeBSD$
+ * $FreeBSD: head/libexec/rtld-elf/Symbol.map 229768 2012-01-07 10:33:01Z kib $
  */
 
 FBSD_1.0 {
@@ -18,6 +18,10 @@
     __tls_get_addr;
 };
 
+FBSD_1.3 {
+    fdlopen;
+};
+
 FBSDprivate_1.0 {
     _rtld_thread_init;
     _rtld_allocate_tls;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/libexec/rtld-elf/mips/reloc.c
--- a/head/libexec/rtld-elf/mips/reloc.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/libexec/rtld-elf/mips/reloc.c	Fri Jan 13 15:51:01 2012 +0200
@@ -29,7 +29,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/libexec/rtld-elf/mips/reloc.c 228435 2011-12-12 11:03:14Z kib $");
+__FBSDID("$FreeBSD: head/libexec/rtld-elf/mips/reloc.c 229780 2012-01-07 16:09:54Z uqs $");
 
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -358,7 +358,7 @@
 			def = find_symdef(i, obj, &defobj, false, NULL,
 			    lockstate);
 			if (def == NULL) {
-				dbg("Warning4, cant find symbole %d", i);
+				dbg("Warning4, can't find symbole %d", i);
 				return -1;
 			}
 			*got = def->st_value + (Elf_Addr)defobj->relocbase;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/libexec/rtld-elf/rtld.c
--- a/head/libexec/rtld-elf/rtld.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/libexec/rtld-elf/rtld.c	Fri Jan 13 15:51:01 2012 +0200
@@ -24,7 +24,7 @@
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
- * $FreeBSD: head/libexec/rtld-elf/rtld.c 229508 2012-01-04 17:17:11Z kib $
+ * $FreeBSD: head/libexec/rtld-elf/rtld.c 229768 2012-01-07 10:33:01Z kib $
  */
 
 /*
@@ -83,7 +83,7 @@
 static void digest_dynamic(Obj_Entry *, int);
 static Obj_Entry *digest_phdr(const Elf_Phdr *, int, caddr_t, const char *);
 static Obj_Entry *dlcheck(void *);
-static Obj_Entry *dlopen_object(const char *name, Obj_Entry *refobj,
+static Obj_Entry *dlopen_object(const char *name, int fd, Obj_Entry *refobj,
     int lo_flags, int mode);
 static Obj_Entry *do_load_object(int, const char *, char *, struct stat *, int);
 static int do_search_info(const Obj_Entry *obj, int, struct dl_serinfo *);
@@ -103,7 +103,7 @@
 static void unload_filtees(Obj_Entry *);
 static int load_needed_objects(Obj_Entry *, int);
 static int load_preload_objects(void);
-static Obj_Entry *load_object(const char *, const Obj_Entry *, int);
+static Obj_Entry *load_object(const char *, int fd, const Obj_Entry *, int);
 static void map_stacks_exec(RtldLockState *);
 static Obj_Entry *obj_from_addr(const void *);
 static void objlist_call_fini(Objlist *, Obj_Entry *, RtldLockState *);
@@ -120,6 +120,7 @@
     RtldLockState *lockstate);
 static int rtld_dirname(const char *, char *);
 static int rtld_dirname_abs(const char *, char *);
+static void *rtld_dlopen(const char *name, int fd, int mode);
 static void rtld_exit(void);
 static char *search_library_path(const char *, const char *);
 static const void **get_program_var_addr(const char *, RtldLockState *);
@@ -1544,7 +1545,7 @@
 {
 
     for (; needed != NULL; needed = needed->next) {
-	needed->obj = dlopen_object(obj->strtab + needed->name, obj,
+	needed->obj = dlopen_object(obj->strtab + needed->name, -1, obj,
 	  flags, ((ld_loadfltr || obj->z_loadfltr) ? RTLD_NOW : RTLD_LAZY) |
 	  RTLD_LOCAL);
     }
@@ -1568,7 +1569,7 @@
     Obj_Entry *obj1;
 
     for (; needed != NULL; needed = needed->next) {
-	obj1 = needed->obj = load_object(obj->strtab + needed->name, obj,
+	obj1 = needed->obj = load_object(obj->strtab + needed->name, -1, obj,
 	  flags & ~RTLD_LO_NOLOAD);
 	if (obj1 == NULL && !ld_tracing && (flags & RTLD_LO_FILTEES) == 0)
 	    return (-1);
@@ -1615,7 +1616,7 @@
 
 	savech = p[len];
 	p[len] = '\0';
-	if (load_object(p, NULL, 0) == NULL)
+	if (load_object(p, -1, NULL, 0) == NULL)
 	    return -1;	/* XXX - cleanup */
 	p[len] = savech;
 	p += len;
@@ -1625,43 +1626,68 @@
     return 0;
 }
 
+static const char *
+printable_path(const char *path)
+{
+
+	return (path == NULL ? "<unknown>" : path);
+}
+
 /*
- * Load a shared object into memory, if it is not already loaded.
+ * Load a shared object into memory, if it is not already loaded.  The
+ * object may be specified by name or by user-supplied file descriptor
+ * fd_u. In the later case, the fd_u descriptor is not closed, but its
+ * duplicate is.
  *
  * Returns a pointer to the Obj_Entry for the object.  Returns NULL
  * on failure.
  */
 static Obj_Entry *
-load_object(const char *name, const Obj_Entry *refobj, int flags)
+load_object(const char *name, int fd_u, const Obj_Entry *refobj, int flags)
 {
     Obj_Entry *obj;
-    int fd = -1;
+    int fd;
     struct stat sb;
     char *path;
 
-    for (obj = obj_list->next;  obj != NULL;  obj = obj->next)
-	if (object_match_name(obj, name))
-	    return obj;
-
-    path = find_library(name, refobj);
-    if (path == NULL)
-	return NULL;
+    if (name != NULL) {
+	for (obj = obj_list->next;  obj != NULL;  obj = obj->next) {
+	    if (object_match_name(obj, name))
+		return (obj);
+	}
+
+	path = find_library(name, refobj);
+	if (path == NULL)
+	    return (NULL);
+    } else
+	path = NULL;
 
     /*
-     * If we didn't find a match by pathname, open the file and check
-     * again by device and inode.  This avoids false mismatches caused
-     * by multiple links or ".." in pathnames.
+     * If we didn't find a match by pathname, or the name is not
+     * supplied, open the file and check again by device and inode.
+     * This avoids false mismatches caused by multiple links or ".."
+     * in pathnames.
      *
      * To avoid a race, we open the file and use fstat() rather than
      * using stat().
      */
-    if ((fd = open(path, O_RDONLY)) == -1) {
-	_rtld_error("Cannot open \"%s\"", path);
-	free(path);
-	return NULL;
+    fd = -1;
+    if (fd_u == -1) {
+	if ((fd = open(path, O_RDONLY)) == -1) {
+	    _rtld_error("Cannot open \"%s\"", path);
+	    free(path);
+	    return (NULL);
+	}
+    } else {
+	fd = dup(fd_u);
+	if (fd == -1) {
+	    _rtld_error("Cannot dup fd");
+	    free(path);
+	    return (NULL);
+	}
     }
     if (fstat(fd, &sb) == -1) {
-	_rtld_error("Cannot fstat \"%s\"", path);
+	_rtld_error("Cannot fstat \"%s\"", printable_path(path));
 	close(fd);
 	free(path);
 	return NULL;
@@ -1669,7 +1695,7 @@
     for (obj = obj_list->next;  obj != NULL;  obj = obj->next)
 	if (obj->ino == sb.st_ino && obj->dev == sb.st_dev)
 	    break;
-    if (obj != NULL) {
+    if (obj != NULL && name != NULL) {
 	object_add_name(obj, name);
 	free(path);
 	close(fd);
@@ -1703,20 +1729,25 @@
      */
     if (dangerous_ld_env) {
 	if (fstatfs(fd, &fs) != 0) {
-	    _rtld_error("Cannot fstatfs \"%s\"", path);
-		return NULL;
+	    _rtld_error("Cannot fstatfs \"%s\"", printable_path(path));
+	    return NULL;
 	}
 	if (fs.f_flags & MNT_NOEXEC) {
 	    _rtld_error("Cannot execute objects on %s\n", fs.f_mntonname);
 	    return NULL;
 	}
     }
-    dbg("loading \"%s\"", path);
-    obj = map_object(fd, path, sbp);
+    dbg("loading \"%s\"", printable_path(path));
+    obj = map_object(fd, printable_path(path), sbp);
     if (obj == NULL)
         return NULL;
 
-    object_add_name(obj, name);
+    /*
+     * If DT_SONAME is present in the object, digest_dynamic2 already
+     * added it to the object names.
+     */
+    if (name != NULL)
+	object_add_name(obj, name);
     obj->path = path;
     digest_dynamic(obj, 0);
     if (obj->z_noopen && (flags & (RTLD_LO_DLOPEN | RTLD_LO_TRACE)) ==
@@ -2212,6 +2243,20 @@
 void *
 dlopen(const char *name, int mode)
 {
+
+	return (rtld_dlopen(name, -1, mode));
+}
+
+void *
+fdlopen(int fd, int mode)
+{
+
+	return (rtld_dlopen(NULL, fd, mode));
+}
+
+static void *
+rtld_dlopen(const char *name, int fd, int mode)
+{
     RtldLockState lockstate;
     int lo_flags;
 
@@ -2232,7 +2277,7 @@
     if (ld_tracing != NULL)
 	    lo_flags |= RTLD_LO_TRACE;
 
-    return (dlopen_object(name, obj_main, lo_flags,
+    return (dlopen_object(name, fd, obj_main, lo_flags,
       mode & (RTLD_MODEMASK | RTLD_GLOBAL)));
 }
 
@@ -2247,7 +2292,8 @@
 }
 
 static Obj_Entry *
-dlopen_object(const char *name, Obj_Entry *refobj, int lo_flags, int mode)
+dlopen_object(const char *name, int fd, Obj_Entry *refobj, int lo_flags,
+    int mode)
 {
     Obj_Entry **old_obj_tail;
     Obj_Entry *obj;
@@ -2262,11 +2308,11 @@
 
     old_obj_tail = obj_tail;
     obj = NULL;
-    if (name == NULL) {
+    if (name == NULL && fd == -1) {
 	obj = obj_main;
 	obj->refcount++;
     } else {
-	obj = load_object(name, refobj, lo_flags);
+	obj = load_object(name, fd, refobj, lo_flags);
     }
 
     if (obj) {
diff -r ebe87ce4d7dd -r 1750de5b0399 head/libexec/rtld-elf/rtld.h
--- a/head/libexec/rtld-elf/rtld.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/libexec/rtld-elf/rtld.h	Fri Jan 13 15:51:01 2012 +0200
@@ -22,7 +22,7 @@
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
- * $FreeBSD: head/libexec/rtld-elf/rtld.h 228435 2011-12-12 11:03:14Z kib $
+ * $FreeBSD: head/libexec/rtld-elf/rtld.h 229780 2012-01-07 16:09:54Z uqs $
  */
 
 #ifndef RTLD_H /* { */
@@ -238,7 +238,7 @@
     Objlist dagmembers;		/* DAG has these members (%) */
     dev_t dev;			/* Object's filesystem's device */
     ino_t ino;			/* Object's inode number */
-    void *priv;			/* Platform-dependant */
+    void *priv;			/* Platform-dependent */
 } Obj_Entry;
 
 #define RTLD_MAGIC	0xd550b87a
diff -r ebe87ce4d7dd -r 1750de5b0399 head/libexec/rtld-elf/sparc64/reloc.c
--- a/head/libexec/rtld-elf/sparc64/reloc.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/libexec/rtld-elf/sparc64/reloc.c	Fri Jan 13 15:51:01 2012 +0200
@@ -31,7 +31,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/libexec/rtld-elf/sparc64/reloc.c 228435 2011-12-12 11:03:14Z kib $");
+__FBSDID("$FreeBSD: head/libexec/rtld-elf/sparc64/reloc.c 229780 2012-01-07 16:09:54Z uqs $");
 
 #include <sys/param.h>
 #include <sys/mman.h>
@@ -623,7 +623,7 @@
 			flush(where, 4);
 		} else if (target >= 0 && target < (1L<<32)) {
 			/*
-			 * We're withing 32-bits of address zero.
+			 * We're within 32-bits of address zero.
 			 *
 			 * The resulting code in the jump slot is:
 			 *
@@ -643,7 +643,7 @@
 			flush(where, 4);
 		} else if (target <= 0 && target > -(1L<<32)) {
 			/*
-			 * We're withing 32-bits of address -1.
+			 * We're within 32-bits of address -1.
 			 *
 			 * The resulting code in the jump slot is:
 			 *
@@ -665,7 +665,7 @@
 			flush(where, 4);
 		} else if (offset <= (1L<<32) && offset >= -((1L<<32) - 4)) {
 			/*
-			 * We're withing 32-bits -- we can use a direct call
+			 * We're within 32-bits -- we can use a direct call
 			 * insn
 			 *
 			 * The resulting code in the jump slot is:
@@ -688,7 +688,7 @@
 			flush(where, 4);
 		} else if (offset >= 0 && offset < (1L<<44)) {
 			/*
-			 * We're withing 44 bits.  We can generate this
+			 * We're within 44 bits.  We can generate this
 			 * pattern:
 			 *
 			 * The resulting code in the jump slot is:
@@ -713,7 +713,7 @@
 			flush(where, 4);
 		} else if (offset < 0 && offset > -(1L<<44)) {
 			/*
-			 * We're withing 44 bits.  We can generate this
+			 * We're within 44 bits.  We can generate this
 			 * pattern:
 			 *
 			 * The resulting code in the jump slot is:
diff -r ebe87ce4d7dd -r 1750de5b0399 head/libexec/tftpd/tftp-io.c
--- a/head/libexec/tftpd/tftp-io.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/libexec/tftpd/tftp-io.c	Fri Jan 13 15:51:01 2012 +0200
@@ -24,7 +24,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/libexec/tftpd/tftp-io.c 222326 2011-05-26 20:32:33Z rodrigc $");
+__FBSDID("$FreeBSD: head/libexec/tftpd/tftp-io.c 229904 2012-01-10 02:55:35Z eadler $");
 
 #include <sys/stat.h>
 #include <sys/types.h>
@@ -72,13 +72,13 @@
 #define DROPPACKET(s)							\
 	if (packetdroppercentage != 0 &&				\
 	    random()%100 < packetdroppercentage) {			\
-		tftp_log(LOG_DEBUG, "Artifical packet drop in %s", s);	\
+		tftp_log(LOG_DEBUG, "Artificial packet drop in %s", s);	\
 		return;							\
 	}
 #define DROPPACKETn(s,n)						\
 	if (packetdroppercentage != 0 &&				\
 	    random()%100 < packetdroppercentage) {			\
-		tftp_log(LOG_DEBUG, "Artifical packet drop in %s", s);	\
+		tftp_log(LOG_DEBUG, "Artificial packet drop in %s", s);	\
 		return (n);						\
 	}
 
@@ -323,7 +323,6 @@
 {
 	struct tftphdr *tp;
 	int size;
-	char *bp;
 	char buf[MAXPKTSIZE];
 
 	if (debug&DEBUG_PACKETS)
@@ -332,7 +331,6 @@
 	DROPPACKETn("send_ack", 0);
 
 	tp = (struct tftphdr *)buf;
-	bp = buf + 2;
 	size = sizeof(buf) - 2;
 	tp->th_opcode = htons((u_short)ACK);
 	tp->th_block = htons((u_short)block);
diff -r ebe87ce4d7dd -r 1750de5b0399 head/libexec/tftpd/tftp-utils.h
--- a/head/libexec/tftpd/tftp-utils.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/libexec/tftpd/tftp-utils.h	Fri Jan 13 15:51:01 2012 +0200
@@ -24,7 +24,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
+__FBSDID("$FreeBSD: head/libexec/tftpd/tftp-utils.h 229780 2012-01-07 16:09:54Z uqs $");
 
 /*
  */
@@ -36,11 +36,11 @@
 #define	MAXPKTSIZE	(MAXSEGSIZE + 4) /* Maximum size of the packet */
 
 /* For the blksize option */
-#define BLKSIZE_MIN	8		/* Minumum size of the data segment */
+#define BLKSIZE_MIN	8		/* Minimum size of the data segment */
 #define BLKSIZE_MAX	MAXSEGSIZE	/* Maximum size of the data segment */
 
 /* For the timeout option */
-#define TIMEOUT_MIN	0		/* Minumum timeout value */
+#define TIMEOUT_MIN	0		/* Minimum timeout value */
 #define TIMEOUT_MAX	255		/* Maximum timeout value */
 #define MIN_TIMEOUTS	3
 
diff -r ebe87ce4d7dd -r 1750de5b0399 head/release/doc/en_US.ISO8859-1/hardware/article.sgml
--- a/head/release/doc/en_US.ISO8859-1/hardware/article.sgml	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/release/doc/en_US.ISO8859-1/hardware/article.sgml	Fri Jan 13 15:51:01 2012 +0200
@@ -17,7 +17,7 @@
 
     <corpauthor>The &os; Documentation Project</corpauthor>
 
-    <pubdate>$FreeBSD: head/release/doc/en_US.ISO8859-1/hardware/article.sgml 228028 2011-11-27 16:50:13Z marius $</pubdate>
+    <pubdate>$FreeBSD: head/release/doc/en_US.ISO8859-1/hardware/article.sgml 229977 2012-01-11 18:38:52Z brueffer $</pubdate>
 
     <copyright>
       <year>2000</year>
@@ -32,6 +32,7 @@
       <year>2009</year>
       <year>2010</year>
       <year>2011</year>
+      <year>2012</year>
       <holder role="mailto:doc at FreeBSD.org">The &os; Documentation Project</holder>
     </copyright>
 
@@ -773,6 +774,8 @@
 
       &hwlist.esp;
 
+      &hwlist.hpt27xx;
+
       &hwlist.hptiop;
 
       &hwlist.hptmv;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/release/doc/en_US.ISO8859-1/relnotes/article.sgml
--- a/head/release/doc/en_US.ISO8859-1/relnotes/article.sgml	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/release/doc/en_US.ISO8859-1/relnotes/article.sgml	Fri Jan 13 15:51:01 2012 +0200
@@ -17,7 +17,7 @@
 
   <corpauthor>The &os; Project</corpauthor>
 
-  <pubdate>$FreeBSD: head/release/doc/en_US.ISO8859-1/relnotes/article.sgml 228877 2011-12-25 18:15:31Z eadler $</pubdate>
+  <pubdate>$FreeBSD: head/release/doc/en_US.ISO8859-1/relnotes/article.sgml 229779 2012-01-07 16:09:43Z uqs $</pubdate>
 
   <copyright>
     <year>2000</year>
@@ -339,7 +339,7 @@
       with the same semantics as (for example)
       &man.chmod.1;.</para>
 
-    <para>For compatiblity with other implementations, &man.cp.1; now
+    <para>For compatibility with other implementations, &man.cp.1; now
       supports a <option>-a</option> flag, which is equivalent to
       specifying the <option>-RrP</option> flags.</para>
 
diff -r ebe87ce4d7dd -r 1750de5b0399 head/release/doc/share/misc/dev.archlist.txt
--- a/head/release/doc/share/misc/dev.archlist.txt	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/release/doc/share/misc/dev.archlist.txt	Fri Jan 13 15:51:01 2012 +0200
@@ -23,7 +23,7 @@
 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 # SUCH DAMAGE.
 #
-# $FreeBSD: head/release/doc/share/misc/dev.archlist.txt 227666 2011-11-18 16:54:22Z brueffer $
+# $FreeBSD: head/release/doc/share/misc/dev.archlist.txt 229977 2012-01-11 18:38:52Z brueffer $
 #
 
 #
@@ -72,6 +72,7 @@
 fe	i386,pc98,amd64
 fwohci	i386,sparc64,ia64,amd64,powerpc
 hifn	i386,pc98,amd64
+hpt27xx	i386,amd64
 hptiop	i386,amd64
 hptmv	i386,amd64
 hptrr	i386,amd64
diff -r ebe87ce4d7dd -r 1750de5b0399 head/release/picobsd/tinyware/passwd/local_passwd.c
--- a/head/release/picobsd/tinyware/passwd/local_passwd.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/release/picobsd/tinyware/passwd/local_passwd.c	Fri Jan 13 15:51:01 2012 +0200
@@ -36,7 +36,7 @@
 #endif /* not lint */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
+__FBSDID("$FreeBSD: head/release/picobsd/tinyware/passwd/local_passwd.c 229779 2012-01-07 16:09:43Z uqs $");
 
 #include <sys/types.h>
 #include <sys/time.h>
@@ -172,7 +172,7 @@
 	to64(&salt[5], random(), 4);
 	salt[9] = '\0';
 #else
-	/* Make a good size salt for algoritms that can use it. */
+	/* Make a good size salt for algorithms that can use it. */
 	gettimeofday(&tv,0);
 #ifdef LOGIN_CAP
 	if (login_setcryptfmt(lc, "md5", NULL) == NULL)
diff -r ebe87ce4d7dd -r 1750de5b0399 head/release/picobsd/tinyware/passwd/pw_copy.c
--- a/head/release/picobsd/tinyware/passwd/pw_copy.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/release/picobsd/tinyware/passwd/pw_copy.c	Fri Jan 13 15:51:01 2012 +0200
@@ -36,7 +36,7 @@
 #endif /* not lint */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
+__FBSDID("$FreeBSD: head/release/picobsd/tinyware/passwd/pw_copy.c 229779 2012-01-07 16:09:43Z uqs $");
 
 /*
  * This module is used to copy the master password file, replacing a single
@@ -193,7 +193,7 @@
  * by default, warning for IDs greater than USHRT_MAX.
  *
  * If pw_big_ids_warning is anything other than -1 on entry to pw_scan()
- * it will be set based on the existance of PW_SCAN_BIG_IDS in the
+ * it will be set based on the existence of PW_SCAN_BIG_IDS in the
  * environment.
  */
 int     pw_big_ids_warning = -1;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/release/picobsd/tinyware/simple_httpd/README
--- a/head/release/picobsd/tinyware/simple_httpd/README	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/release/picobsd/tinyware/simple_httpd/README	Fri Jan 13 15:51:01 2012 +0200
@@ -21,14 +21,14 @@
 
 The "fetch" mode supports file transfer over httpd.  This 
 is best thought of as mate for fetch(1).  This feature can be
-usefull to transfer a file from one host to another.
+useful to transfer a file from one host to another.
 
 Simple_httpd has the ability to run CGI scripts.  All CGI
 scripts must be located in ${DOCUMENT_ROOT}/cgi-bin.  The
-server currently only sets 3 enviroment variables before calling
+server currently only sets 3 environment variables before calling
 the script.
 
-CGI Enviroment variables are below:
+CGI Environment variables are below:
 
 SERVER_SOFTWARE = FreeBSD/PicoBSD
 REMOTE_HOST = client.canada_lower_taxes.com
@@ -61,7 +61,7 @@
 
 -D
 Do not daemonize.  The server will not run in the background.  It will
-stay attached to the tty.  This is usefull for debugging.  In this
+stay attached to the tty.  This is useful for debugging.  In this
 mode no log file is created.  Logging info is to stdout.
 
 This option is automatically selected if fetch option is selected.
@@ -139,7 +139,7 @@
 To Do
 =====
 
-Simple authentication would be very usefull[understatment].
+Simple authentication would be very useful [understatment].
 /etc/passwd or PAM would be nice.
 
 I think a netmask option would be good. Most internet appliances
@@ -164,4 +164,4 @@
 
 Major rewrite by William Lloyd <wlloyd at slap.net>
 
-$FreeBSD$
+$FreeBSD: head/release/picobsd/tinyware/simple_httpd/README 229779 2012-01-07 16:09:43Z uqs $
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sbin/bsdlabel/bsdlabel.c
--- a/head/sbin/bsdlabel/bsdlabel.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sbin/bsdlabel/bsdlabel.c	Fri Jan 13 15:51:01 2012 +0200
@@ -53,7 +53,7 @@
 #endif /* not lint */
 #endif
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sbin/bsdlabel/bsdlabel.c 229403 2012-01-03 18:51:58Z ed $");
+__FBSDID("$FreeBSD: head/sbin/bsdlabel/bsdlabel.c 229778 2012-01-07 16:09:33Z uqs $");
 
 #include <sys/param.h>
 #include <stdint.h>
@@ -189,8 +189,8 @@
 				break;
 			case 'r':
 				/*
-				 * We accept and ignode -r for compatibility with
-				 * historically disklabel usage.
+				 * We accept and ignore -r for compatibility with
+				 * historical disklabel usage.
 				 */
 				break;
 			case 'w':
@@ -386,7 +386,7 @@
 	struct disklabel *lp = &lab;
 
 	if (disable_write) {
-		warnx("write to disk label supressed - label was as follows:");
+		warnx("write to disk label suppressed - label was as follows:");
 		display(stdout, NULL);
 		return (0);
 	}
@@ -1512,8 +1512,8 @@
 	loclab.d_secperunit = mediasize / secsize;
 
 	/*
-	 * Nobody in these enligthened days uses the CHS geometry for
-	 * anything, but nontheless try to get it right.  If we fail
+	 * Nobody in these enlightened days uses the CHS geometry for
+	 * anything, but nonetheless try to get it right.  If we fail
 	 * to get any good ideas from the device, construct something
 	 * which is IBM-PC friendly.
 	 */
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sbin/camcontrol/camcontrol.c
--- a/head/sbin/camcontrol/camcontrol.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sbin/camcontrol/camcontrol.c	Fri Jan 13 15:51:01 2012 +0200
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sbin/camcontrol/camcontrol.c 228602 2011-12-16 22:35:19Z dim $");
+__FBSDID("$FreeBSD: head/sbin/camcontrol/camcontrol.c 229919 2012-01-10 02:59:58Z eadler $");
 
 #include <sys/ioctl.h>
 #include <sys/stdint.h>
@@ -3412,7 +3412,6 @@
 		}
 		if (spi && syncrate != -1) {
 			int prelim_sync_period;
-			u_int freq;
 
 			if ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0) {
 				warnx("HBA is not capable of changing "
@@ -3437,7 +3436,6 @@
 				prelim_sync_period = 10000000 / syncrate;
 			spi->sync_period =
 				scsi_calc_syncparam(prelim_sync_period);
-			freq = scsi_calc_syncsrate(spi->sync_period);
 			didsettings++;
 		}
 		if (sata && syncrate != -1) {
@@ -4030,13 +4028,12 @@
 					RPL_LUNDATA_LUN_LUN_MASK);
 				break;
 			case RPL_LUNDATA_ATYP_EXTLUN: {
-				int field_len, field_len_code, eam_code;
+				int field_len_code, eam_code;
 
 				eam_code = lundata->luns[i].lundata[j] &
 					RPL_LUNDATA_EXT_EAM_MASK;
 				field_len_code = (lundata->luns[i].lundata[j] &
 					RPL_LUNDATA_EXT_LEN_MASK) >> 4;
-				field_len = field_len_code * 2;
 
 				if ((eam_code == RPL_LUNDATA_EXT_EAM_WK)
 				 && (field_len_code == 0x00)) {
@@ -5732,7 +5729,7 @@
 "defects arguments:\n"
 "-f format         specify defect list format (block, bfi or phys)\n"
 "-G                get the grown defect list\n"
-"-P                get the permanant defect list\n"
+"-P                get the permanent defect list\n"
 "inquiry arguments:\n"
 "-D                get the standard inquiry data\n"
 "-S                get the serial number\n"
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sbin/camcontrol/modeedit.c
--- a/head/sbin/camcontrol/modeedit.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sbin/camcontrol/modeedit.c	Fri Jan 13 15:51:01 2012 +0200
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sbin/camcontrol/modeedit.c 228407 2011-12-11 11:38:50Z ed $");
+__FBSDID("$FreeBSD: head/sbin/camcontrol/modeedit.c 229919 2012-01-10 02:59:58Z eadler $");
 
 #include <sys/queue.h>
 #include <sys/types.h>
@@ -869,7 +869,6 @@
 	  int retry_count, int timeout)
 {
 	u_int8_t data[MAX_COMMAND_SIZE];/* Buffer to hold sense data. */
-	u_int8_t *mode_pars;		/* Pointer to modepage params. */
 	struct scsi_mode_header_6 *mh;	/* Location of mode header. */
 	struct scsi_mode_page_header *mph;
 	struct pagename *nameentry;
@@ -895,7 +894,6 @@
 		/* Locate the next mode page header. */
 		mph = (struct scsi_mode_page_header *)
 		    ((intptr_t)mh + sizeof(*mh) + len);
-		mode_pars = MODE_PAGE_DATA(mph);
 
 		mph->page_code &= SMS_PAGE_CODE;
 		nameentry = nameentry_lookup(mph->page_code);
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sbin/devd/devd.cc
--- a/head/sbin/devd/devd.cc	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sbin/devd/devd.cc	Fri Jan 13 15:51:01 2012 +0200
@@ -63,7 +63,7 @@
 //	  - devd.conf needs more details on the supported statements.
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sbin/devd/devd.cc 226775 2011-10-26 02:11:28Z hrs $");
+__FBSDID("$FreeBSD: head/sbin/devd/devd.cc 229702 2012-01-06 15:01:05Z glebius $");
 
 #include <sys/param.h>
 #include <sys/socket.h>
@@ -298,7 +298,6 @@
 		{ IFM_FDDI,		"FDDI" },
 		{ IFM_IEEE80211,	"802.11" },
 		{ IFM_ATM,		"ATM" },
-		{ IFM_CARP,		"CARP" },
 		{ -1,			"unknown" },
 		{ 0, NULL },
 	};
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sbin/devfs/rule.c
--- a/head/sbin/devfs/rule.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sbin/devfs/rule.c	Fri Jan 13 15:51:01 2012 +0200
@@ -29,7 +29,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sbin/devfs/rule.c 227081 2011-11-04 13:36:02Z ed $");
+__FBSDID("$FreeBSD: head/sbin/devfs/rule.c 229778 2012-01-07 16:09:33Z uqs $");
 
 #include <sys/param.h>
 #include <sys/conf.h>
@@ -47,7 +47,7 @@
 
 #include "extern.h"
 
-static void rulespec_infp(FILE *fp, unsigned long reqest, devfs_rsnum rsnum);
+static void rulespec_infp(FILE *fp, unsigned long request, devfs_rsnum rsnum);
 static void rulespec_instr(struct devfs_rule *dr, const char *str,
     devfs_rsnum rsnum);
 static void rulespec_intok(struct devfs_rule *dr, int ac, char **av,
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sbin/dhclient/dhclient-script.8
--- a/head/sbin/dhclient/dhclient-script.8	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sbin/dhclient/dhclient-script.8	Fri Jan 13 15:51:01 2012 +0200
@@ -36,7 +36,7 @@
 .\" see ``http://www.isc.org/isc''.  To learn more about Vixie
 .\" Enterprises, see ``http://www.vix.com''.
 .\"
-.\" $FreeBSD$
+.\" $FreeBSD: head/sbin/dhclient/dhclient-script.8 229778 2012-01-07 16:09:33Z uqs $
 .\"
 .Dd September 6, 2010
 .Dt DHCLIENT-SCRIPT 8
@@ -232,7 +232,7 @@
 .Pa /etc/dhclient-exit-hooks .
 If found, it will be sourced
 .Pq see Xr sh 1 .
-These hooks scripts can be used to dynamically modify the enviornment at
+These hooks scripts can be used to dynamically modify the environment at
 appropriate times during the DHCP negotiations.
 For example, if the administrator wishes to disable alias IP numbers on
 the DHCP interface, they might want to put the following in
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sbin/dhclient/options.c
--- a/head/sbin/dhclient/options.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sbin/dhclient/options.c	Fri Jan 13 15:51:01 2012 +0200
@@ -41,7 +41,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sbin/dhclient/options.c 229000 2011-12-30 14:33:08Z dumbbell $");
+__FBSDID("$FreeBSD: head/sbin/dhclient/options.c 229778 2012-01-07 16:09:33Z uqs $");
 
 #include <ctype.h>
 
@@ -284,8 +284,8 @@
 			    option->data[i + 1];
 			if (pointer >= *offset) {
 				/*
-				 * The pointer must indicates a prior
-				 * occurance.
+				 * The pointer must indicate a prior
+				 * occurrence.
 				 */
 				warning("Invalid forward pointer in DHCP "
 				    "Domain Search option compression.");
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sbin/fsck_ffs/fsck.h
--- a/head/sbin/fsck_ffs/fsck.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sbin/fsck_ffs/fsck.h	Fri Jan 13 15:51:01 2012 +0200
@@ -57,7 +57,7 @@
  * SUCH DAMAGE.
  *
  *	@(#)fsck.h	8.4 (Berkeley) 5/9/95
- * $FreeBSD: head/sbin/fsck_ffs/fsck.h 228751 2011-12-20 20:39:00Z kib $
+ * $FreeBSD: head/sbin/fsck_ffs/fsck.h 229778 2012-01-07 16:09:33Z uqs $
  */
 
 #ifndef _FSCK_H_
@@ -220,7 +220,7 @@
  * To check if a block has been found as a duplicate it is only
  * necessary to search from duplist through muldup. To find the
  * total number of times that a block has been found as a duplicate
- * the entire list must be searched for occurences of the block
+ * the entire list must be searched for occurrences of the block
  * in question. The following diagram shows a sample list where
  * w (found twice), x (found once), y (found three times), and z
  * (found once) are duplicate block numbers:
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sbin/fsck_ffs/fsutil.c
--- a/head/sbin/fsck_ffs/fsutil.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sbin/fsck_ffs/fsutil.c	Fri Jan 13 15:51:01 2012 +0200
@@ -33,7 +33,7 @@
 #endif /* not lint */
 #endif
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sbin/fsck_ffs/fsutil.c 221233 2011-04-29 23:00:23Z des $");
+__FBSDID("$FreeBSD: head/sbin/fsck_ffs/fsutil.c 229778 2012-01-07 16:09:33Z uqs $");
 
 #include <sys/param.h>
 #include <sys/time.h>
@@ -717,7 +717,7 @@
 #include <stdarg.h>
 
 /*
- * An unexpected inconsistency occured.
+ * An unexpected inconsistency occurred.
  * Die if preening or file system is running with soft dependency protocol,
  * otherwise just print message and continue.
  */
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sbin/fsck_ffs/gjournal.c
--- a/head/sbin/fsck_ffs/gjournal.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sbin/fsck_ffs/gjournal.c	Fri Jan 13 15:51:01 2012 +0200
@@ -52,7 +52,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
+__FBSDID("$FreeBSD: head/sbin/fsck_ffs/gjournal.c 229918 2012-01-10 02:59:50Z eadler $");
 
 #include <sys/param.h>
 #include <sys/disklabel.h>
@@ -399,7 +399,7 @@
 	void *p;
 	struct cgchain *cgc;
 	struct cg *cgp;
-	uint8_t *inosused, *blksfree;
+	uint8_t *inosused;
 	ino_t cino, ino;
 	int cg, mode;
 
@@ -438,7 +438,6 @@
 		/* We don't want it to be freed in the meantime. */
 		busycg(cgc);
 		inosused = cg_inosused(cgp);
-		blksfree = cg_blksfree(cgp);
 		/*
 		 * Now go through the list of all inodes in this cylinder group
 		 * to find unreferenced ones.
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sbin/fsirand/fsirand.c
--- a/head/sbin/fsirand/fsirand.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sbin/fsirand/fsirand.c	Fri Jan 13 15:51:01 2012 +0200
@@ -32,7 +32,7 @@
 
 #ifndef lint
 static const char rcsid[] =
-  "$FreeBSD: head/sbin/fsirand/fsirand.c 227081 2011-11-04 13:36:02Z ed $";
+  "$FreeBSD: head/sbin/fsirand/fsirand.c 229917 2012-01-10 02:59:43Z eadler $";
 #endif /* not lint */
 
 #include <sys/param.h>
@@ -115,7 +115,7 @@
 	caddr_t inodebuf;
 	ssize_t ibufsize;
 	struct fs *sblock;
-	ino_t inumber, maxino;
+	ino_t inumber;
 	ufs2_daddr_t sblockloc, dblk;
 	char sbuf[SBLOCKSIZE], sbuftmp[SBLOCKSIZE];
 	int i, devfd, n, cg;
@@ -165,7 +165,6 @@
 		fprintf(stderr, "Cannot find file system superblock\n");
 		return (1);
 	}
-	maxino = sblock->fs_ncg * sblock->fs_ipg;
 
 	if (sblock->fs_magic == FS_UFS1_MAGIC &&
 	    sblock->fs_old_inodefmt < FS_44INODEFMT) {
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sbin/geom/class/part/geom_part.c
--- a/head/sbin/geom/class/part/geom_part.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sbin/geom/class/part/geom_part.c	Fri Jan 13 15:51:01 2012 +0200
@@ -25,7 +25,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sbin/geom/class/part/geom_part.c 225445 2011-09-08 04:14:16Z ae $");
+__FBSDID("$FreeBSD: head/sbin/geom/class/part/geom_part.c 229916 2012-01-10 02:59:35Z eadler $");
 
 #include <sys/stat.h>
 #include <sys/vtoc.h>
@@ -718,7 +718,7 @@
 	struct ggeom *gp;
 	const char *s, *scheme;
 	off_t sector, end;
-	off_t length, secsz;
+	off_t length;
 	int error, i, windex, wblocks, wtype;
 
 	if (gctl_get_int(req, "nargs") != 1)
@@ -744,7 +744,6 @@
 	if (scheme == NULL)
 		abort();
 	pp = LIST_FIRST(&gp->lg_consumer)->lg_provider;
-	secsz = pp->lg_sectorsize;
 	s = find_geomcfg(gp, "last");
 	wblocks = strlen(s);
 	wtype = 0;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sbin/geom/class/part/gpart.8
--- a/head/sbin/geom/class/part/gpart.8	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sbin/geom/class/part/gpart.8	Fri Jan 13 15:51:01 2012 +0200
@@ -22,9 +22,9 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.\" $FreeBSD: head/sbin/geom/class/part/gpart.8 227800 2011-11-21 18:04:37Z wblock $
+.\" $FreeBSD: head/sbin/geom/class/part/gpart.8 229847 2012-01-09 05:51:33Z wblock $
 .\"
-.Dd November 21, 2011
+.Dd January 8, 2012
 .Dt GPART 8
 .Os
 .Sh NAME
@@ -33,7 +33,7 @@
 .Sh SYNOPSIS
 To add support for the disk partitioning GEOM class,
 place one or more of the following
-lines in your kernel configuration file:
+lines in the kernel configuration file:
 .Bd -ragged -offset indent
 .Cd "options GEOM_PART_APM"
 .Cd "options GEOM_PART_BSD"
@@ -48,7 +48,8 @@
 These options provide support for the various types of partitioning
 schemes supported by the
 .Ns Nm
-utility.  See
+utility.
+See
 .Sx "PARTITIONING SCHEMES"
 below for more details.
 .Pp
@@ -410,13 +411,13 @@
 .El
 .\" ==== RESTORE ====
 .It Cm restore
-Restore the partition table from backup previously created by
+Restore the partition table from a backup previously created by the
 .Cm backup
-action and given from standard input.
-Only partition table may be restored.
-This action does not affect content of partitions.
-This mean that you should copy your data from backup after restoring
-partition table and write bootcode again if it is needed.
+action and read from standard input.
+Only the partition table is restored.
+This action does not affect the content of partitions.
+After restoring the partition table and writing bootcode if needed,
+user data must be restored from backup.
 .Pp
 Additional options include:
 .Bl -tag -width 10n
@@ -502,8 +503,8 @@
 .Po
 This scheme can also be used as the sole partitioning method, without
 an MBR.
-Partition editing tools from other operating systems often do not 
-understand the bare disklabel partition layout, so this is sometimes 
+Partition editing tools from other operating systems often do not
+understand the bare disklabel partition layout, so this is sometimes
 called
 .Dq dangerously dedicated .
 .Pc
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sbin/geom/class/raid/graid.8
--- a/head/sbin/geom/class/raid/graid.8	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sbin/geom/class/raid/graid.8	Fri Jan 13 15:51:01 2012 +0200
@@ -22,7 +22,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.\" $FreeBSD: head/sbin/geom/class/raid/graid.8 226816 2011-10-26 21:50:10Z mav $
+.\" $FreeBSD: head/sbin/geom/class/raid/graid.8 229886 2012-01-09 23:01:42Z jimharris $
 .\"
 .Dd October 26, 2011
 .Dt GRAID 8
@@ -251,7 +251,7 @@
 Do not run GEOM RAID class on migrating volumes under pain of possible data
 corruption!
 .Sh 2TiB BARRIERS
-Intel and Promise metadata formats do not support disks above 2TiB.
+Promise metadata format does not support disks above 2TiB.
 NVIDIA metadata format does not support volumes above 2TiB.
 .Sh EXIT STATUS
 Exit status is 0 on success, and non-zero if the command fails.
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sbin/gvinum/gvinum.c
--- a/head/sbin/gvinum/gvinum.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sbin/gvinum/gvinum.c	Fri Jan 13 15:51:01 2012 +0200
@@ -29,7 +29,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $FreeBSD: head/sbin/gvinum/gvinum.c 227489 2011-11-13 17:06:33Z eadler $
+ * $FreeBSD: head/sbin/gvinum/gvinum.c 229915 2012-01-10 02:59:26Z eadler $
  */
 
 #include <sys/param.h>
@@ -937,14 +937,12 @@
 	struct gctl_req *req;
 	int flags, i;
 	const char *errstr;
-	char *op, *msg;
+	char *op;
 
 	if (rebuild) {
 		op = "rebuildparity";
-		msg = "Rebuilding";
 	} else {
 		op = "checkparity";
-		msg = "Checking";
 	}
 
 	optreset = 1;
@@ -1057,9 +1055,8 @@
 	struct gctl_req *req;
 	int flags, i, j;
 	const char *errstr;
-	char buf[20], *cmd;
+	char buf[20];
 
-	cmd = argv[0];
 	flags = 0;
 	optreset = 1;
 	optind = 1;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sbin/hastctl/hastctl.8
--- a/head/sbin/hastctl/hastctl.8	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sbin/hastctl/hastctl.8	Fri Jan 13 15:51:01 2012 +0200
@@ -25,7 +25,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.\" $FreeBSD: head/sbin/hastctl/hastctl.8 220520 2011-04-10 15:07:54Z trociny $
+.\" $FreeBSD: head/sbin/hastctl/hastctl.8 229778 2012-01-07 16:09:33Z uqs $
 .\"
 .Dd April 10, 2011
 .Dt HASTCTL 8
@@ -97,7 +97,7 @@
 related to dirty map updates, which will degrade performance of the
 given resource.
 If extent size is too large, synchronization, even in case of short
-outage, can take a long time increasing the risk of loosing up-to-date
+outage, can take a long time increasing the risk of losing up-to-date
 node before synchronization process is completed.
 The default extent size is
 .Va 2MB .
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sbin/hastctl/hastctl.c
--- a/head/sbin/hastctl/hastctl.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sbin/hastctl/hastctl.c	Fri Jan 13 15:51:01 2012 +0200
@@ -28,7 +28,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sbin/hastctl/hastctl.c 225785 2011-09-27 08:21:00Z pjd $");
+__FBSDID("$FreeBSD: head/sbin/hastctl/hastctl.c 229945 2012-01-10 22:39:07Z pjd $");
 
 #include <sys/param.h>
 #include <sys/disk.h>
@@ -104,7 +104,7 @@
 	ec = 0;
 	pjdlog_prefix_set("[%s] ", res->hr_name);
 
-	if (provinfo(res, true) < 0) {
+	if (provinfo(res, true) == -1) {
 		ec = EX_NOINPUT;
 		goto end;
 	}
@@ -146,7 +146,7 @@
 
 	res->hr_localoff = METADATA_SIZE + mapsize;
 
-	if (metadata_write(res) < 0) {
+	if (metadata_write(res) == -1) {
 		ec = EX_IOERR;
 		goto end;
 	}
@@ -401,15 +401,15 @@
 			debug++;
 			break;
 		case 'e':
-			if (expand_number(optarg, &extentsize) < 0)
+			if (expand_number(optarg, &extentsize) == -1)
 				errx(EX_USAGE, "Invalid extentsize");
 			break;
 		case 'k':
-			if (expand_number(optarg, &keepdirty) < 0)
+			if (expand_number(optarg, &keepdirty) == -1)
 				errx(EX_USAGE, "Invalid keepdirty");
 			break;
 		case 'm':
-			if (expand_number(optarg, &mediasize) < 0)
+			if (expand_number(optarg, &mediasize) == -1)
 				errx(EX_USAGE, "Invalid mediasize");
 			break;
 		case 'h':
@@ -479,13 +479,13 @@
 	}
 
 	/* Setup control connection... */
-	if (proto_client(NULL, cfg->hc_controladdr, &controlconn) < 0) {
+	if (proto_client(NULL, cfg->hc_controladdr, &controlconn) == -1) {
 		pjdlog_exit(EX_OSERR,
 		    "Unable to setup control connection to %s",
 		    cfg->hc_controladdr);
 	}
 	/* ...and connect to hastd. */
-	if (proto_connect(controlconn, HAST_TIMEOUT) < 0) {
+	if (proto_connect(controlconn, HAST_TIMEOUT) == -1) {
 		pjdlog_exit(EX_OSERR, "Unable to connect to hastd via %s",
 		    cfg->hc_controladdr);
 	}
@@ -494,14 +494,14 @@
 		exit(EX_CONFIG);
 
 	/* Send the command to the server... */
-	if (hast_proto_send(NULL, controlconn, nv, NULL, 0) < 0) {
+	if (hast_proto_send(NULL, controlconn, nv, NULL, 0) == -1) {
 		pjdlog_exit(EX_UNAVAILABLE,
 		    "Unable to send command to hastd via %s",
 		    cfg->hc_controladdr);
 	}
 	nv_free(nv);
 	/* ...and receive reply. */
-	if (hast_proto_recv_hdr(controlconn, &nv) < 0) {
+	if (hast_proto_recv_hdr(controlconn, &nv) == -1) {
 		pjdlog_exit(EX_UNAVAILABLE,
 		    "cannot receive reply from hastd via %s",
 		    cfg->hc_controladdr);
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sbin/hastd/activemap.c
--- a/head/sbin/hastd/activemap.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sbin/hastd/activemap.c	Fri Jan 13 15:51:01 2012 +0200
@@ -28,7 +28,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sbin/hastd/activemap.c 225787 2011-09-27 08:50:37Z pjd $");
+__FBSDID("$FreeBSD: head/sbin/hastd/activemap.c 229778 2012-01-07 16:09:33Z uqs $");
 
 #include <sys/param.h>	/* powerof2() */
 #include <sys/queue.h>
@@ -219,7 +219,7 @@
 	kd = keepdirty_find(amp, extent);
 	if (kd != NULL) {
 		/*
-		 * Only move element at the begining.
+		 * Only move element at the beginning.
 		 */
 		TAILQ_REMOVE(&amp->am_keepdirty, kd, kd_next);
 		TAILQ_INSERT_HEAD(&amp->am_keepdirty, kd, kd_next);
@@ -573,7 +573,7 @@
 		return;
 	}
 	/*
-	 * Mark that we want to start synchronization from the begining.
+	 * Mark that we want to start synchronization from the beginning.
 	 */
 	amp->am_syncoff = -1;
 }
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sbin/hastd/control.c
--- a/head/sbin/hastd/control.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sbin/hastd/control.c	Fri Jan 13 15:51:01 2012 +0200
@@ -28,7 +28,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sbin/hastd/control.c 228542 2011-12-15 22:01:34Z pjd $");
+__FBSDID("$FreeBSD: head/sbin/hastd/control.c 229945 2012-01-10 22:39:07Z pjd $");
 
 #include <sys/types.h>
 #include <sys/wait.h>
@@ -115,7 +115,7 @@
 	 * doing that work.
 	 */
 	if (res->hr_workerpid != 0) {
-		if (kill(res->hr_workerpid, SIGTERM) < 0) {
+		if (kill(res->hr_workerpid, SIGTERM) == -1) {
 			pjdlog_errno(LOG_WARNING,
 			    "Unable to kill worker process %u",
 			    (unsigned int)res->hr_workerpid);
@@ -167,7 +167,7 @@
 		    "Unable to prepare control header");
 		goto end;
 	}
-	if (hast_proto_send(res, res->hr_ctrl, cnvout, NULL, 0) < 0) {
+	if (hast_proto_send(res, res->hr_ctrl, cnvout, NULL, 0) == -1) {
 		error = errno;
 		pjdlog_errno(LOG_ERR, "Unable to send control header");
 		goto end;
@@ -176,7 +176,7 @@
 	/*
 	 * Receive response.
 	 */
-	if (hast_proto_recv_hdr(res->hr_ctrl, &cnvin) < 0) {
+	if (hast_proto_recv_hdr(res->hr_ctrl, &cnvin) == -1) {
 		error = errno;
 		pjdlog_errno(LOG_ERR, "Unable to receive control header");
 		goto end;
@@ -293,7 +293,7 @@
 	uint8_t cmd, role;
 	int error;
 
-	if (proto_accept(cfg->hc_controlconn, &conn) < 0) {
+	if (proto_accept(cfg->hc_controlconn, &conn) == -1) {
 		pjdlog_errno(LOG_ERR, "Unable to accept control connection");
 		return;
 	}
@@ -302,7 +302,7 @@
 	nvin = nvout = NULL;
 	role = HAST_ROLE_UNDEF;
 
-	if (hast_proto_recv_hdr(conn, &nvin) < 0) {
+	if (hast_proto_recv_hdr(conn, &nvin) == -1) {
 		pjdlog_errno(LOG_ERR, "Unable to receive control header");
 		nvin = NULL;
 		goto close;
@@ -395,7 +395,7 @@
 	if (error != 0)
 		nv_add_int16(nvout, error, "error");
 
-	if (hast_proto_send(NULL, conn, nvout, NULL, 0) < 0)
+	if (hast_proto_send(NULL, conn, nvout, NULL, 0) == -1)
 		pjdlog_errno(LOG_ERR, "Unable to send control response");
 close:
 	if (nvin != NULL)
@@ -417,7 +417,7 @@
 	uint8_t cmd;
 
 	for (;;) {
-		if (hast_proto_recv_hdr(res->hr_ctrl, &nvin) < 0) {
+		if (hast_proto_recv_hdr(res->hr_ctrl, &nvin) == -1) {
 			if (sigexit_received)
 				pthread_exit(NULL);
 			pjdlog_errno(LOG_ERR,
@@ -481,7 +481,7 @@
 			nv_free(nvout);
 			continue;
 		}
-		if (hast_proto_send(NULL, res->hr_ctrl, nvout, NULL, 0) < 0) {
+		if (hast_proto_send(NULL, res->hr_ctrl, nvout, NULL, 0) == -1) {
 			pjdlog_errno(LOG_ERR,
 			    "Unable to send reply to control message");
 		}
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sbin/hastd/ebuf.c
--- a/head/sbin/hastd/ebuf.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sbin/hastd/ebuf.c	Fri Jan 13 15:51:01 2012 +0200
@@ -28,7 +28,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sbin/hastd/ebuf.c 225787 2011-09-27 08:50:37Z pjd $");
+__FBSDID("$FreeBSD: head/sbin/hastd/ebuf.c 229945 2012-01-10 22:39:07Z pjd $");
 
 #include <sys/param.h>
 
@@ -116,7 +116,7 @@
 		 * We can't add more entries at the front, so we have to extend
 		 * our buffer.
 		 */
-		if (ebuf_head_extend(eb, size) < 0)
+		if (ebuf_head_extend(eb, size) == -1)
 			return (-1);
 	}
 	PJDLOG_ASSERT(size <= (size_t)(eb->eb_used - eb->eb_start));
@@ -143,7 +143,7 @@
 		 * We can't add more entries at the back, so we have to extend
 		 * our buffer.
 		 */
-		if (ebuf_tail_extend(eb, size) < 0)
+		if (ebuf_tail_extend(eb, size) == -1)
 			return (-1);
 	}
 	PJDLOG_ASSERT(size <=
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sbin/hastd/event.c
--- a/head/sbin/hastd/event.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sbin/hastd/event.c	Fri Jan 13 15:51:01 2012 +0200
@@ -25,7 +25,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sbin/hastd/event.c 225787 2011-09-27 08:50:37Z pjd $");
+__FBSDID("$FreeBSD: head/sbin/hastd/event.c 229945 2012-01-10 22:39:07Z pjd $");
 
 #include <errno.h>
 
@@ -61,11 +61,11 @@
 		    "Unable to prepare event header");
 		goto done;
 	}
-	if (hast_proto_send(res, res->hr_event, nvout, NULL, 0) < 0) {
+	if (hast_proto_send(res, res->hr_event, nvout, NULL, 0) == -1) {
 		pjdlog_errno(LOG_ERR, "Unable to send event header");
 		goto done;
 	}
-	if (hast_proto_recv_hdr(res->hr_event, &nvin) < 0) {
+	if (hast_proto_recv_hdr(res->hr_event, &nvin) == -1) {
 		pjdlog_errno(LOG_ERR, "Unable to receive event header");
 		goto done;
 	}
@@ -92,7 +92,7 @@
 
 	nvin = nvout = NULL;
 
-	if (hast_proto_recv_hdr(res->hr_event, &nvin) < 0) {
+	if (hast_proto_recv_hdr(res->hr_event, &nvin) == -1) {
 		/*
 		 * First error log as debug. This is because worker process
 		 * most likely exited.
@@ -145,7 +145,7 @@
 		    "Unable to prepare event header");
 		goto fail;
 	}
-	if (hast_proto_send(res, res->hr_event, nvout, NULL, 0) < 0) {
+	if (hast_proto_send(res, res->hr_event, nvout, NULL, 0) == -1) {
 		pjdlog_errno(LOG_ERR, "Unable to send event header");
 		goto fail;
 	}
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sbin/hastd/hast_compression.c
--- a/head/sbin/hastd/hast_compression.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sbin/hastd/hast_compression.c	Fri Jan 13 15:51:01 2012 +0200
@@ -25,7 +25,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
+__FBSDID("$FreeBSD: head/sbin/hastd/hast_compression.c 229778 2012-01-07 16:09:33Z uqs $");
 
 #include <sys/endian.h>
 
@@ -55,7 +55,7 @@
 	 * Because inside the loop we don't check at every step, we would
 	 * get an answer only after walking through entire buffer.
 	 * To return early if the buffer doesn't contain all zeros, we probe
-	 * 8 bytes at the begining, in the middle and at the end of the buffer
+	 * 8 bytes at the beginning, in the middle and at the end of the buffer
 	 * first.
 	 */
 
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sbin/hastd/hast_proto.c
--- a/head/sbin/hastd/hast_proto.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sbin/hastd/hast_proto.c	Fri Jan 13 15:51:01 2012 +0200
@@ -29,7 +29,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sbin/hastd/hast_proto.c 225787 2011-09-27 08:50:37Z pjd $");
+__FBSDID("$FreeBSD: head/sbin/hastd/hast_proto.c 229945 2012-01-10 22:39:07Z pjd $");
 
 #include <sys/endian.h>
 
@@ -114,13 +114,13 @@
 
 	hdr.version = HAST_PROTO_VERSION;
 	hdr.size = htole32((uint32_t)ebuf_size(eb));
-	if (ebuf_add_head(eb, &hdr, sizeof(hdr)) < 0)
+	if (ebuf_add_head(eb, &hdr, sizeof(hdr)) == -1)
 		goto end;
 
 	hptr = ebuf_data(eb, &hsize);
-	if (proto_send(conn, hptr, hsize) < 0)
+	if (proto_send(conn, hptr, hsize) == -1)
 		goto end;
-	if (data != NULL && proto_send(conn, dptr, size) < 0)
+	if (data != NULL && proto_send(conn, dptr, size) == -1)
 		goto end;
 
 	ret = 0;
@@ -141,7 +141,7 @@
 	eb = NULL;
 	nv = NULL;
 
-	if (proto_recv(conn, &hdr, sizeof(hdr)) < 0)
+	if (proto_recv(conn, &hdr, sizeof(hdr)) == -1)
 		goto fail;
 
 	if (hdr.version != HAST_PROTO_VERSION) {
@@ -154,11 +154,11 @@
 	eb = ebuf_alloc(hdr.size);
 	if (eb == NULL)
 		goto fail;
-	if (ebuf_add_tail(eb, NULL, hdr.size) < 0)
+	if (ebuf_add_tail(eb, NULL, hdr.size) == -1)
 		goto fail;
 	hptr = ebuf_data(eb, NULL);
 	PJDLOG_ASSERT(hptr != NULL);
-	if (proto_recv(conn, hptr, hdr.size) < 0)
+	if (proto_recv(conn, hptr, hdr.size) == -1)
 		goto fail;
 	nv = nv_ntoh(eb);
 	if (nv == NULL)
@@ -196,7 +196,7 @@
 	} else if (dsize == 0) {
 		(void)nv_set_error(nv, 0);
 	} else {
-		if (proto_recv(conn, data, dsize) < 0)
+		if (proto_recv(conn, data, dsize) == -1)
 			goto end;
 		for (ii = sizeof(pipeline) / sizeof(pipeline[0]); ii > 0;
 		    ii--) {
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sbin/hastd/hastd.c
--- a/head/sbin/hastd/hastd.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sbin/hastd/hastd.c	Fri Jan 13 15:51:01 2012 +0200
@@ -29,7 +29,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sbin/hastd/hastd.c 226463 2011-10-17 12:22:09Z pjd $");
+__FBSDID("$FreeBSD: head/sbin/hastd/hastd.c 229946 2012-01-10 22:41:09Z pjd $");
 
 #include <sys/param.h>
 #include <sys/linker.h>
@@ -68,6 +68,8 @@
 bool sigexit_received = false;
 /* PID file handle. */
 struct pidfh *pfh;
+/* Do we run in foreground? */
+static bool foreground;
 
 /* How often check for hooks running for too long. */
 #define	REPORT_INTERVAL	5
@@ -172,7 +174,7 @@
 	msg[0] = '\0';
 
 	maxfd = sysconf(_SC_OPEN_MAX);
-	if (maxfd < 0) {
+	if (maxfd == -1) {
 		pjdlog_init(pjdlogmode);
 		pjdlog_prefix_set("[%s] (%s) ", res->hr_name,
 		    role2str(res->hr_role));
@@ -450,7 +452,7 @@
 		pjdlog_error("Unable to allocate header for reload message.");
 		return;
 	}
-	if (hast_proto_send(res, res->hr_ctrl, nvout, NULL, 0) < 0) {
+	if (hast_proto_send(res, res->hr_ctrl, nvout, NULL, 0) == -1) {
 		pjdlog_errno(LOG_ERR, "Unable to send reload message");
 		nv_free(nvout);
 		return;
@@ -458,7 +460,7 @@
 	nv_free(nvout);
 
 	/* Receive response. */
-	if (hast_proto_recv_hdr(res->hr_ctrl, &nvin) < 0) {
+	if (hast_proto_recv_hdr(res->hr_ctrl, &nvin) == -1) {
 		pjdlog_errno(LOG_ERR, "Unable to receive reload reply");
 		return;
 	}
@@ -494,7 +496,7 @@
 	 */
 	if (strcmp(cfg->hc_controladdr, newcfg->hc_controladdr) != 0) {
 		if (proto_server(newcfg->hc_controladdr,
-		    &newcfg->hc_controlconn) < 0) {
+		    &newcfg->hc_controlconn) == -1) {
 			pjdlog_errno(LOG_ERR,
 			    "Unable to listen on control address %s",
 			    newcfg->hc_controladdr);
@@ -531,7 +533,7 @@
 	/*
 	 * Check if pidfile's path has changed.
 	 */
-	if (strcmp(cfg->hc_pidfile, newcfg->hc_pidfile) != 0) {
+	if (!foreground && strcmp(cfg->hc_pidfile, newcfg->hc_pidfile) != 0) {
 		newpfh = pidfile_open(newcfg->hc_pidfile, 0600, &otherpid);
 		if (newpfh == NULL) {
 			if (errno == EEXIST) {
@@ -543,7 +545,7 @@
 				    "Unable to open or create pidfile %s",
 				    newcfg->hc_pidfile);
 			}
-		} else if (pidfile_write(newpfh) < 0) {
+		} else if (pidfile_write(newpfh) == -1) {
 			/* Write PID to a file. */
 			pjdlog_errno(LOG_WARNING,
 			    "Unable to write PID to file %s",
@@ -571,10 +573,14 @@
 	/*
 	 * Switch to new pidfile.
 	 */
-	(void)pidfile_remove(pfh);
-	pfh = newpfh;
-	(void)strlcpy(cfg->hc_pidfile, newcfg->hc_pidfile,
-	    sizeof(cfg->hc_pidfile));
+	if (newpfh != NULL) {
+		pjdlog_info("Pidfile changed from %s to %s.", cfg->hc_pidfile,
+		    newcfg->hc_pidfile);
+		(void)pidfile_remove(pfh);
+		pfh = newpfh;
+		(void)strlcpy(cfg->hc_pidfile, newcfg->hc_pidfile,
+		    sizeof(cfg->hc_pidfile));
+	}
 	/*
 	 * Switch to new listen addresses. Close all that were removed.
 	 */
@@ -742,7 +748,7 @@
 	proto_local_address(lst->hl_conn, laddr, sizeof(laddr));
 	pjdlog_debug(1, "Accepting connection to %s.", laddr);
 
-	if (proto_accept(lst->hl_conn, &conn) < 0) {
+	if (proto_accept(lst->hl_conn, &conn) == -1) {
 		pjdlog_errno(LOG_ERR, "Unable to accept connection %s", laddr);
 		return;
 	}
@@ -752,7 +758,7 @@
 	pjdlog_info("Connection from %s to %s.", raddr, laddr);
 
 	/* Error in setting timeout is not critical, but why should it fail? */
-	if (proto_timeout(conn, HAST_TIMEOUT) < 0)
+	if (proto_timeout(conn, HAST_TIMEOUT) == -1)
 		pjdlog_errno(LOG_WARNING, "Unable to set connection timeout");
 
 	nvin = nvout = nverr = NULL;
@@ -771,7 +777,7 @@
 	}
 	/* Ok, remote host can access at least one resource. */
 
-	if (hast_proto_recv_hdr(conn, &nvin) < 0) {
+	if (hast_proto_recv_hdr(conn, &nvin) == -1) {
 		pjdlog_errno(LOG_ERR, "Unable to receive header from %s",
 		    raddr);
 		goto close;
@@ -786,7 +792,7 @@
 	pjdlog_debug(2, "%s: resource=%s", raddr, resname);
 	token = nv_get_uint8_array(nvin, &size, "token");
 	/*
-	 * NULL token means that this is first conection.
+	 * NULL token means that this is first connection.
 	 */
 	if (token != NULL && size != sizeof(res->hr_token)) {
 		pjdlog_error("Received token of invalid size from %s (expected %zu, got %zu).",
@@ -859,7 +865,7 @@
 			    "Worker process exists (pid=%u), stopping it.",
 			    (unsigned int)res->hr_workerpid);
 			/* Stop child process. */
-			if (kill(res->hr_workerpid, SIGINT) < 0) {
+			if (kill(res->hr_workerpid, SIGINT) == -1) {
 				pjdlog_errno(LOG_ERR,
 				    "Unable to stop worker process (pid=%u)",
 				    (unsigned int)res->hr_workerpid);
@@ -909,7 +915,7 @@
 			    strerror(nv_error(nvout)));
 			goto fail;
 		}
-		if (hast_proto_send(NULL, conn, nvout, NULL, 0) < 0) {
+		if (hast_proto_send(NULL, conn, nvout, NULL, 0) == -1) {
 			int error = errno;
 
 			pjdlog_errno(LOG_ERR, "Unable to send response to %s",
@@ -938,7 +944,7 @@
 		    "Unable to prepare error header for %s", raddr);
 		goto close;
 	}
-	if (hast_proto_send(NULL, conn, nverr, NULL, 0) < 0) {
+	if (hast_proto_send(NULL, conn, nverr, NULL, 0) == -1) {
 		pjdlog_errno(LOG_ERR, "Unable to send error to %s", raddr);
 		goto close;
 	}
@@ -963,20 +969,20 @@
 
 	PJDLOG_ASSERT(res->hr_role == HAST_ROLE_PRIMARY);
 
-	if (proto_recv(res->hr_conn, &val, sizeof(val)) < 0) {
+	if (proto_recv(res->hr_conn, &val, sizeof(val)) == -1) {
 		pjdlog_errno(LOG_WARNING,
 		    "Unable to receive connection command");
 		return;
 	}
 	if (proto_client(res->hr_sourceaddr[0] != '\0' ? res->hr_sourceaddr : NULL,
-	    res->hr_remoteaddr, &conn) < 0) {
+	    res->hr_remoteaddr, &conn) == -1) {
 		val = errno;
 		pjdlog_errno(LOG_WARNING,
 		    "Unable to create outgoing connection to %s",
 		    res->hr_remoteaddr);
 		goto out;
 	}
-	if (proto_connect(conn, -1) < 0) {
+	if (proto_connect(conn, -1) == -1) {
 		val = errno;
 		pjdlog_errno(LOG_WARNING, "Unable to connect to %s",
 		    res->hr_remoteaddr);
@@ -985,11 +991,11 @@
 	}
 	val = 0;
 out:
-	if (proto_send(res->hr_conn, &val, sizeof(val)) < 0) {
+	if (proto_send(res->hr_conn, &val, sizeof(val)) == -1) {
 		pjdlog_errno(LOG_WARNING,
 		    "Unable to send reply to connection request");
 	}
-	if (val == 0 && proto_connection_send(res->hr_conn, conn) < 0)
+	if (val == 0 && proto_connection_send(res->hr_conn, conn) == -1)
 		pjdlog_errno(LOG_WARNING, "Unable to send connection");
 
 	pjdlog_prefix_set("%s", "");
@@ -1155,7 +1161,6 @@
 	struct hastd_listen *lst;
 	const char *pidfile;
 	pid_t otherpid;
-	bool foreground;
 	int debuglevel;
 	sigset_t mask;
 
@@ -1220,16 +1225,23 @@
 			pjdlog_exitx(EX_CONFIG, "Pidfile path is too long.");
 		}
 	}
-	pfh = pidfile_open(cfg->hc_pidfile, 0600, &otherpid);
-	if (pfh == NULL) {
-		if (errno == EEXIST) {
-			pjdlog_exitx(EX_TEMPFAIL,
-			    "Another hastd is already running, pidfile: %s, pid: %jd.",
-			    cfg->hc_pidfile, (intmax_t)otherpid);
+
+	if (!foreground) {
+		pfh = pidfile_open(cfg->hc_pidfile, 0600, &otherpid);
+		if (pfh == NULL) {
+			if (errno == EEXIST) {
+				pjdlog_exitx(EX_TEMPFAIL,
+				    "Another hastd is already running, pidfile: %s, pid: %jd.",
+				    cfg->hc_pidfile, (intmax_t)otherpid);
+			}
+			/*
+			 * If we cannot create pidfile for other reasons,
+			 * only warn.
+			 */
+			pjdlog_errno(LOG_WARNING,
+			    "Unable to open or create pidfile %s",
+			    cfg->hc_pidfile);
 		}
-		/* If we cannot create pidfile for other reasons, only warn. */
-		pjdlog_errno(LOG_WARNING, "Unable to open or create pidfile %s",
-		    cfg->hc_pidfile);
 	}
 
 	/*
@@ -1253,14 +1265,14 @@
 	PJDLOG_VERIFY(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
 
 	/* Listen on control address. */
-	if (proto_server(cfg->hc_controladdr, &cfg->hc_controlconn) < 0) {
+	if (proto_server(cfg->hc_controladdr, &cfg->hc_controlconn) == -1) {
 		KEEP_ERRNO((void)pidfile_remove(pfh));
 		pjdlog_exit(EX_OSERR, "Unable to listen on control address %s",
 		    cfg->hc_controladdr);
 	}
 	/* Listen for remote connections. */
 	TAILQ_FOREACH(lst, &cfg->hc_listen, hl_next) {
-		if (proto_server(lst->hl_addr, &lst->hl_conn) < 0) {
+		if (proto_server(lst->hl_addr, &lst->hl_conn) == -1) {
 			KEEP_ERRNO((void)pidfile_remove(pfh));
 			pjdlog_exit(EX_OSERR, "Unable to listen on address %s",
 			    lst->hl_addr);
@@ -1268,7 +1280,7 @@
 	}
 
 	if (!foreground) {
-		if (daemon(0, 0) < 0) {
+		if (daemon(0, 0) == -1) {
 			KEEP_ERRNO((void)pidfile_remove(pfh));
 			pjdlog_exit(EX_OSERR, "Unable to daemonize");
 		}
@@ -1277,7 +1289,7 @@
 		pjdlog_mode_set(PJDLOG_MODE_SYSLOG);
 
 		/* Write PID to a file. */
-		if (pidfile_write(pfh) < 0) {
+		if (pidfile_write(pfh) == -1) {
 			pjdlog_errno(LOG_WARNING,
 			    "Unable to write PID to a file %s",
 			    cfg->hc_pidfile);
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sbin/hastd/hooks.c
--- a/head/sbin/hastd/hooks.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sbin/hastd/hooks.c	Fri Jan 13 15:51:01 2012 +0200
@@ -29,7 +29,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sbin/hastd/hooks.c 225787 2011-09-27 08:50:37Z pjd $");
+__FBSDID("$FreeBSD: head/sbin/hastd/hooks.c 229945 2012-01-10 22:39:07Z pjd $");
 
 #include <sys/types.h>
 #include <sys/sysctl.h>
@@ -105,26 +105,26 @@
 	 * Redirect stdin, stdout and stderr to /dev/null.
 	 */
 	fd = open(_PATH_DEVNULL, O_RDONLY);
-	if (fd < 0) {
+	if (fd == -1) {
 		pjdlog_errno(LOG_WARNING, "Unable to open %s for reading",
 		    _PATH_DEVNULL);
 	} else if (fd != STDIN_FILENO) {
-		if (dup2(fd, STDIN_FILENO) < 0) {
+		if (dup2(fd, STDIN_FILENO) == -1) {
 			pjdlog_errno(LOG_WARNING,
 			    "Unable to duplicate descriptor for stdin");
 		}
 		close(fd);
 	}
 	fd = open(_PATH_DEVNULL, O_WRONLY);
-	if (fd < 0) {
+	if (fd == -1) {
 		pjdlog_errno(LOG_WARNING, "Unable to open %s for writing",
 		    _PATH_DEVNULL);
 	} else {
-		if (fd != STDOUT_FILENO && dup2(fd, STDOUT_FILENO) < 0) {
+		if (fd != STDOUT_FILENO && dup2(fd, STDOUT_FILENO) == -1) {
 			pjdlog_errno(LOG_WARNING,
 			    "Unable to duplicate descriptor for stdout");
 		}
-		if (fd != STDERR_FILENO && dup2(fd, STDERR_FILENO) < 0) {
+		if (fd != STDERR_FILENO && dup2(fd, STDERR_FILENO) == -1) {
 			pjdlog_errno(LOG_WARNING,
 			    "Unable to duplicate descriptor for stderr");
 		}
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sbin/hastd/lzf.h
--- a/head/sbin/hastd/lzf.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sbin/hastd/lzf.h	Fri Jan 13 15:51:01 2012 +0200
@@ -146,7 +146,7 @@
 
 /*
  * Avoid assigning values to errno variable? for some embedding purposes
- * (linux kernel for example), this is neccessary. NOTE: this breaks
+ * (linux kernel for example), this is necessary. NOTE: this breaks
  * the documentation in lzf.h.
  */
 #ifndef AVOID_ERRNO
@@ -167,7 +167,7 @@
  * and return EINVAL if the input stream has been corrupted. This
  * only shields against overflowing the input buffer and will not
  * detect most corrupted streams.
- * This check is not normally noticable on modern hardware
+ * This check is not normally noticeable on modern hardware
  * (<1% slowdown), but might slow down older cpus considerably.
  */
 #ifndef CHECK_INPUT
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sbin/hastd/metadata.c
--- a/head/sbin/hastd/metadata.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sbin/hastd/metadata.c	Fri Jan 13 15:51:01 2012 +0200
@@ -28,7 +28,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sbin/hastd/metadata.c 225787 2011-09-27 08:50:37Z pjd $");
+__FBSDID("$FreeBSD: head/sbin/hastd/metadata.c 229945 2012-01-10 22:39:07Z pjd $");
 
 #include <errno.h>
 #include <fcntl.h>
@@ -61,14 +61,14 @@
 	 * Is this first metadata_read() call for this resource?
 	 */
 	if (res->hr_localfd == -1) {
-		if (provinfo(res, openrw) < 0) {
+		if (provinfo(res, openrw) == -1) {
 			rerrno = errno;
 			goto fail;
 		}
 		opened_here = true;
 		pjdlog_debug(1, "Obtained info about %s.", res->hr_localpath);
 		if (openrw) {
-			if (flock(res->hr_localfd, LOCK_EX | LOCK_NB) < 0) {
+			if (flock(res->hr_localfd, LOCK_EX | LOCK_NB) == -1) {
 				rerrno = errno;
 				if (errno == EOPNOTSUPP) {
 					pjdlog_warning("Unable to lock %s (operation not supported), but continuing.",
@@ -91,7 +91,7 @@
 		    "Unable to allocate memory to read metadata");
 		goto fail;
 	}
-	if (ebuf_add_tail(eb, NULL, METADATA_SIZE) < 0) {
+	if (ebuf_add_tail(eb, NULL, METADATA_SIZE) == -1) {
 		rerrno = errno;
 		pjdlog_errno(LOG_ERR,
 		    "Unable to allocate memory to read metadata");
@@ -101,7 +101,7 @@
 	buf = ebuf_data(eb, NULL);
 	PJDLOG_ASSERT(buf != NULL);
 	done = pread(res->hr_localfd, buf, METADATA_SIZE, 0);
-	if (done < 0 || done != METADATA_SIZE) {
+	if (done == -1 || done != METADATA_SIZE) {
 		rerrno = errno;
 		pjdlog_errno(LOG_ERR, "Unable to read metadata");
 		ebuf_free(eb);
@@ -213,7 +213,7 @@
 	PJDLOG_ASSERT(size < METADATA_SIZE);
 	bcopy(ptr, buf, size);
 	done = pwrite(res->hr_localfd, buf, METADATA_SIZE, 0);
-	if (done < 0 || done != METADATA_SIZE) {
+	if (done == -1 || done != METADATA_SIZE) {
 		pjdlog_errno(LOG_ERR, "Unable to write metadata");
 		goto end;
 	}
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sbin/hastd/nv.c
--- a/head/sbin/hastd/nv.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sbin/hastd/nv.c	Fri Jan 13 15:51:01 2012 +0200
@@ -28,7 +28,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sbin/hastd/nv.c 225787 2011-09-27 08:50:37Z pjd $");
+__FBSDID("$FreeBSD: head/sbin/hastd/nv.c 229945 2012-01-10 22:39:07Z pjd $");
 
 #include <sys/param.h>
 #include <sys/endian.h>
@@ -385,7 +385,7 @@
 	nv->nv_ebuf = eb;
 	nv->nv_magic = NV_MAGIC;
 
-	if (nv_validate(nv, &extra) < 0) {
+	if (nv_validate(nv, &extra) == -1) {
 		rerrno = errno;
 		nv->nv_magic = 0;
 		free(nv);
@@ -480,7 +480,7 @@
 	ssize_t size;
 
 	size = vasprintf(&value, valuefmt, valueap);
-	if (size < 0) {
+	if (size == -1) {
 		if (nv->nv_error == 0)
 			nv->nv_error = ENOMEM;
 		return;
@@ -627,7 +627,7 @@
 	unsigned int ii;
 	bool swap;
 
-	if (nv_validate(nv, NULL) < 0) {
+	if (nv_validate(nv, NULL) == -1) {
 		printf("error: %d\n", errno);
 		return;
 	}
@@ -784,7 +784,7 @@
 	bcopy(name, nvh->nvh_name, namesize);
 
 	/* Add header first. */
-	if (ebuf_add_tail(nv->nv_ebuf, nvh, NVH_HSIZE(nvh)) < 0) {
+	if (ebuf_add_tail(nv->nv_ebuf, nvh, NVH_HSIZE(nvh)) == -1) {
 		PJDLOG_ASSERT(errno != 0);
 		if (nv->nv_error == 0)
 			nv->nv_error = errno;
@@ -793,7 +793,7 @@
 	}
 	free(nvh);
 	/* Add the actual data. */
-	if (ebuf_add_tail(nv->nv_ebuf, value, vsize) < 0) {
+	if (ebuf_add_tail(nv->nv_ebuf, value, vsize) == -1) {
 		PJDLOG_ASSERT(errno != 0);
 		if (nv->nv_error == 0)
 			nv->nv_error = errno;
@@ -804,7 +804,7 @@
 	if (vsize == 0)
 		return;
 	PJDLOG_ASSERT(vsize > 0 && vsize <= sizeof(align));
-	if (ebuf_add_tail(nv->nv_ebuf, align, vsize) < 0) {
+	if (ebuf_add_tail(nv->nv_ebuf, align, vsize) == -1) {
 		PJDLOG_ASSERT(errno != 0);
 		if (nv->nv_error == 0)
 			nv->nv_error = errno;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sbin/hastd/primary.c
--- a/head/sbin/hastd/primary.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sbin/hastd/primary.c	Fri Jan 13 15:51:01 2012 +0200
@@ -29,7 +29,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sbin/hastd/primary.c 226859 2011-10-27 20:32:57Z pjd $");
+__FBSDID("$FreeBSD: head/sbin/hastd/primary.c 229945 2012-01-10 22:39:07Z pjd $");
 
 #include <sys/types.h>
 #include <sys/time.h>
@@ -254,7 +254,7 @@
 		ggiod.gctl_version = G_GATE_VERSION;
 		ggiod.gctl_unit = res->hr_ggateunit;
 		ggiod.gctl_force = 1;
-		if (ioctl(res->hr_ggatefd, G_GATE_CMD_DESTROY, &ggiod) < 0) {
+		if (ioctl(res->hr_ggatefd, G_GATE_CMD_DESTROY, &ggiod) == -1) {
 			pjdlog_errno(LOG_WARNING,
 			    "Unable to destroy hast/%s device",
 			    res->hr_provname);
@@ -451,7 +451,7 @@
 		/* Initialize unique resource identifier. */
 		arc4random_buf(&res->hr_resuid, sizeof(res->hr_resuid));
 		mtx_unlock(&metadata_lock);
-		if (metadata_write(res) < 0)
+		if (metadata_write(res) == -1)
 			exit(EX_NOINPUT);
 		return (true);
 	}
@@ -463,19 +463,19 @@
 	unsigned char *buf;
 	size_t mapsize;
 
-	if (metadata_read(res, true) < 0)
+	if (metadata_read(res, true) == -1)
 		exit(EX_NOINPUT);
 	mtx_init(&res->hr_amp_lock);
 	if (activemap_init(&res->hr_amp, res->hr_datasize, res->hr_extentsize,
-	    res->hr_local_sectorsize, res->hr_keepdirty) < 0) {
+	    res->hr_local_sectorsize, res->hr_keepdirty) == -1) {
 		primary_exit(EX_TEMPFAIL, "Unable to create activemap");
 	}
 	mtx_init(&range_lock);
 	cv_init(&range_regular_cond);
-	if (rangelock_init(&range_regular) < 0)
+	if (rangelock_init(&range_regular) == -1)
 		primary_exit(EX_TEMPFAIL, "Unable to create regular range lock");
 	cv_init(&range_sync_cond);
-	if (rangelock_init(&range_sync) < 0)
+	if (rangelock_init(&range_sync) == -1)
 		primary_exit(EX_TEMPFAIL, "Unable to create sync range lock");
 	mapsize = activemap_ondisk_size(res->hr_amp);
 	buf = calloc(1, mapsize);
@@ -500,7 +500,7 @@
 	 */
 	res->hr_primary_localcnt = 0;
 	res->hr_primary_remotecnt = 0;
-	if (metadata_write(res) < 0)
+	if (metadata_write(res) == -1)
 		exit(EX_NOINPUT);
 }
 
@@ -511,11 +511,11 @@
 	int16_t val;
 
 	val = 1;
-	if (proto_send(res->hr_conn, &val, sizeof(val)) < 0) {
+	if (proto_send(res->hr_conn, &val, sizeof(val)) == -1) {
 		primary_exit(EX_TEMPFAIL,
 		    "Unable to send connection request to parent");
 	}
-	if (proto_recv(res->hr_conn, &val, sizeof(val)) < 0) {
+	if (proto_recv(res->hr_conn, &val, sizeof(val)) == -1) {
 		primary_exit(EX_TEMPFAIL,
 		    "Unable to receive reply to connection request from parent");
 	}
@@ -525,18 +525,18 @@
 		    res->hr_remoteaddr);
 		return (-1);
 	}
-	if (proto_connection_recv(res->hr_conn, true, &conn) < 0) {
+	if (proto_connection_recv(res->hr_conn, true, &conn) == -1) {
 		primary_exit(EX_TEMPFAIL,
 		    "Unable to receive connection from parent");
 	}
-	if (proto_connect_wait(conn, res->hr_timeout) < 0) {
+	if (proto_connect_wait(conn, res->hr_timeout) == -1) {
 		pjdlog_errno(LOG_WARNING, "Unable to connect to %s",
 		    res->hr_remoteaddr);
 		proto_close(conn);
 		return (-1);
 	}
 	/* Error in setting timeout is not critical, but why should it fail? */
-	if (proto_timeout(conn, res->hr_timeout) < 0)
+	if (proto_timeout(conn, res->hr_timeout) == -1)
 		pjdlog_errno(LOG_WARNING, "Unable to set connection timeout");
 
 	*connp = conn;
@@ -583,7 +583,7 @@
 		nv_free(nvout);
 		goto close;
 	}
-	if (hast_proto_send(res, out, nvout, NULL, 0) < 0) {
+	if (hast_proto_send(res, out, nvout, NULL, 0) == -1) {
 		pjdlog_errno(LOG_WARNING,
 		    "Unable to send handshake header to %s",
 		    res->hr_remoteaddr);
@@ -591,7 +591,7 @@
 		goto close;
 	}
 	nv_free(nvout);
-	if (hast_proto_recv_hdr(out, &nvin) < 0) {
+	if (hast_proto_recv_hdr(out, &nvin) == -1) {
 		pjdlog_errno(LOG_WARNING,
 		    "Unable to receive handshake header from %s",
 		    res->hr_remoteaddr);
@@ -655,7 +655,7 @@
 		nv_free(nvout);
 		goto close;
 	}
-	if (hast_proto_send(res, in, nvout, NULL, 0) < 0) {
+	if (hast_proto_send(res, in, nvout, NULL, 0) == -1) {
 		pjdlog_errno(LOG_WARNING,
 		    "Unable to send handshake header to %s",
 		    res->hr_remoteaddr);
@@ -663,7 +663,7 @@
 		goto close;
 	}
 	nv_free(nvout);
-	if (hast_proto_recv_hdr(out, &nvin) < 0) {
+	if (hast_proto_recv_hdr(out, &nvin) == -1) {
 		pjdlog_errno(LOG_WARNING,
 		    "Unable to receive handshake header from %s",
 		    res->hr_remoteaddr);
@@ -726,7 +726,7 @@
 		 * download its activemap.
 		 */
 		if (hast_proto_recv_data(res, out, nvin, map,
-		    mapsize) < 0) {
+		    mapsize) == -1) {
 			pjdlog_errno(LOG_ERR,
 			    "Unable to receive remote activemap");
 			nv_free(nvin);
@@ -801,7 +801,7 @@
 	 * We communicate with ggate via /dev/ggctl. Open it.
 	 */
 	res->hr_ggatefd = open("/dev/" G_GATE_CTL_NAME, O_RDWR);
-	if (res->hr_ggatefd < 0)
+	if (res->hr_ggatefd == -1)
 		primary_exit(EX_OSFILE, "Unable to open /dev/" G_GATE_CTL_NAME);
 	/*
 	 * Create provider before trying to connect, as connection failure
@@ -859,7 +859,7 @@
 	 * Create communication channel for sending control commands from
 	 * parent to child.
 	 */
-	if (proto_client(NULL, "socketpair://", &res->hr_ctrl) < 0) {
+	if (proto_client(NULL, "socketpair://", &res->hr_ctrl) == -1) {
 		/* TODO: There's no need for this to be fatal error. */
 		KEEP_ERRNO((void)pidfile_remove(pfh));
 		pjdlog_exit(EX_OSERR,
@@ -868,7 +868,7 @@
 	/*
 	 * Create communication channel for sending events from child to parent.
 	 */
-	if (proto_client(NULL, "socketpair://", &res->hr_event) < 0) {
+	if (proto_client(NULL, "socketpair://", &res->hr_event) == -1) {
 		/* TODO: There's no need for this to be fatal error. */
 		KEEP_ERRNO((void)pidfile_remove(pfh));
 		pjdlog_exit(EX_OSERR,
@@ -878,7 +878,7 @@
 	 * Create communication channel for sending connection requests from
 	 * child to parent.
 	 */
-	if (proto_client(NULL, "socketpair://", &res->hr_conn) < 0) {
+	if (proto_client(NULL, "socketpair://", &res->hr_conn) == -1) {
 		/* TODO: There's no need for this to be fatal error. */
 		KEEP_ERRNO((void)pidfile_remove(pfh));
 		pjdlog_exit(EX_OSERR,
@@ -886,7 +886,7 @@
 	}
 
 	pid = fork();
-	if (pid < 0) {
+	if (pid == -1) {
 		/* TODO: There's no need for this to be fatal error. */
 		KEEP_ERRNO((void)pidfile_remove(pfh));
 		pjdlog_exit(EX_TEMPFAIL, "Unable to fork");
@@ -933,7 +933,7 @@
 
 	/*
 	 * Create the guard thread first, so we can handle signals from the
-	 * very begining.
+	 * very beginning.
 	 */
 	error = pthread_create(&td, NULL, guard_thread, res);
 	PJDLOG_ASSERT(error == 0);
@@ -1095,7 +1095,7 @@
 		mtx_unlock(&metadata_lock);
 	}
 	rw_unlock(&hio_remote_lock[ncomp]);
-	if (ioctl(res->hr_ggatefd, G_GATE_CMD_DONE, ggio) < 0)
+	if (ioctl(res->hr_ggatefd, G_GATE_CMD_DONE, ggio) == -1)
 		primary_exit(EX_OSERR, "G_GATE_CMD_DONE failed");
 	hio->hio_done = true;
 }
@@ -1133,7 +1133,7 @@
 		pjdlog_debug(2,
 		    "ggate_recv: (%p) Waiting for request from the kernel.",
 		    hio);
-		if (ioctl(res->hr_ggatefd, G_GATE_CMD_START, ggio) < 0) {
+		if (ioctl(res->hr_ggatefd, G_GATE_CMD_START, ggio) == -1) {
 			if (sigexit_received)
 				pthread_exit(NULL);
 			primary_exit(EX_OSERR, "G_GATE_CMD_START failed");
@@ -1225,7 +1225,7 @@
 					continue;
 				}
 				if (rangelock_add(range_regular,
-				    ggio->gctl_offset, ggio->gctl_length) < 0) {
+				    ggio->gctl_offset, ggio->gctl_length) == -1) {
 					mtx_unlock(&range_lock);
 					pjdlog_debug(2,
 					    "regular: Range offset=%jd length=%zu is already locked, waiting.",
@@ -1296,7 +1296,7 @@
 				/*
 				 * If READ failed, try to read from remote node.
 				 */
-				if (ret < 0) {
+				if (ret == -1) {
 					reqlog(LOG_WARNING, 0, ggio,
 					    "Local request failed (%s), trying remote node. ",
 					    strerror(errno));
@@ -1313,7 +1313,7 @@
 			ret = pwrite(res->hr_localfd, ggio->gctl_data,
 			    ggio->gctl_length,
 			    ggio->gctl_offset + res->hr_localoff);
-			if (ret < 0) {
+			if (ret == -1) {
 				hio->hio_errors[ncomp] = errno;
 				reqlog(LOG_WARNING, 0, ggio,
 				    "Local request failed (%s): ",
@@ -1336,7 +1336,7 @@
 			ret = g_delete(res->hr_localfd,
 			    ggio->gctl_offset + res->hr_localoff,
 			    ggio->gctl_length);
-			if (ret < 0) {
+			if (ret == -1) {
 				hio->hio_errors[ncomp] = errno;
 				reqlog(LOG_WARNING, 0, ggio,
 				    "Local request failed (%s): ",
@@ -1352,7 +1352,7 @@
 				break;
 			}
 			ret = g_flush(res->hr_localfd);
-			if (ret < 0) {
+			if (ret == -1) {
 				if (errno == EOPNOTSUPP)
 					res->hr_localflush = false;
 				hio->hio_errors[ncomp] = errno;
@@ -1406,7 +1406,7 @@
 		    "keepalive_send: Unable to prepare header to send.");
 		return;
 	}
-	if (hast_proto_send(res, res->hr_remoteout, nv, NULL, 0) < 0) {
+	if (hast_proto_send(res, res->hr_remoteout, nv, NULL, 0) == -1) {
 		rw_unlock(&hio_remote_lock[ncomp]);
 		pjdlog_common(LOG_DEBUG, 1, errno,
 		    "keepalive_send: Unable to send request");
@@ -1520,7 +1520,7 @@
 		TAILQ_INSERT_TAIL(&hio_recv_list[ncomp], hio, hio_next[ncomp]);
 		mtx_unlock(&hio_recv_list_lock[ncomp]);
 		if (hast_proto_send(res, res->hr_remoteout, nv, data,
-		    data != NULL ? length : 0) < 0) {
+		    data != NULL ? length : 0) == -1) {
 			hio->hio_errors[ncomp] = errno;
 			rw_unlock(&hio_remote_lock[ncomp]);
 			pjdlog_debug(2,
@@ -1617,7 +1617,7 @@
 			mtx_unlock(&hio_recv_list_lock[ncomp]);
 			goto done_queue;
 		}
-		if (hast_proto_recv_hdr(res->hr_remotein, &nv) < 0) {
+		if (hast_proto_recv_hdr(res->hr_remotein, &nv) == -1) {
 			pjdlog_errno(LOG_ERR,
 			    "Unable to receive reply header");
 			rw_unlock(&hio_remote_lock[ncomp]);
@@ -1665,7 +1665,7 @@
 				goto done_queue;
 			}
 			if (hast_proto_recv_data(res, res->hr_remotein, nv,
-			    ggio->gctl_data, ggio->gctl_length) < 0) {
+			    ggio->gctl_data, ggio->gctl_length) == -1) {
 				hio->hio_errors[ncomp] = errno;
 				pjdlog_errno(LOG_ERR,
 				    "Unable to receive reply data");
@@ -1766,7 +1766,7 @@
 			if (!hio->hio_done)
 				write_complete(res, hio);
 		} else {
-			if (ioctl(res->hr_ggatefd, G_GATE_CMD_DONE, ggio) < 0) {
+			if (ioctl(res->hr_ggatefd, G_GATE_CMD_DONE, ggio) == -1) {
 				primary_exit(EX_OSERR,
 				    "G_GATE_CMD_DONE failed");
 			}
@@ -1834,7 +1834,7 @@
 		mtx_unlock(&res->hr_amp_lock);
 		if (dorewind) {
 			dorewind = false;
-			if (offset < 0)
+			if (offset == -1)
 				pjdlog_info("Nodes are in sync.");
 			else {
 				pjdlog_info("Synchronization started. %NB to go.",
@@ -1844,7 +1844,7 @@
 				gettimeofday(&tstart, NULL);
 			}
 		}
-		if (offset < 0) {
+		if (offset == -1) {
 			sync_stop();
 			pjdlog_debug(1, "Nothing to synchronize.");
 			/*
@@ -1903,7 +1903,7 @@
 				mtx_unlock(&range_lock);
 				continue;
 			}
-			if (rangelock_add(range_sync, offset, length) < 0) {
+			if (rangelock_add(range_sync, offset, length) == -1) {
 				mtx_unlock(&range_lock);
 				pjdlog_debug(2,
 				    "sync: Range offset=%jd length=%jd is already locked, waiting.",
@@ -2124,12 +2124,12 @@
 			}
 			rw_unlock(&hio_remote_lock[ii]);
 			if (proto_timeout(gres->hr_remotein,
-			    gres->hr_timeout) < 0) {
+			    gres->hr_timeout) == -1) {
 				pjdlog_errno(LOG_WARNING,
 				    "Unable to set connection timeout");
 			}
 			if (proto_timeout(gres->hr_remoteout,
-			    gres->hr_timeout) < 0) {
+			    gres->hr_timeout) == -1) {
 				pjdlog_errno(LOG_WARNING,
 				    "Unable to set connection timeout");
 			}
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sbin/hastd/proto.c
--- a/head/sbin/hastd/proto.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sbin/hastd/proto.c	Fri Jan 13 15:51:01 2012 +0200
@@ -28,7 +28,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
+__FBSDID("$FreeBSD: head/sbin/hastd/proto.c 229945 2012-01-10 22:39:07Z pjd $");
 
 #include <sys/types.h>
 #include <sys/queue.h>
@@ -131,7 +131,7 @@
 		/*
 		 * ret == 0  - success
 		 * ret == -1 - dstaddr is not for this protocol
-		 * ret > 0   - right protocol, but an error occured
+		 * ret > 0   - right protocol, but an error occurred
 		 */
 		if (ret >= 0)
 			break;
@@ -142,7 +142,7 @@
 		return (-1);
 	}
 	if (ret > 0) {
-		/* An error occured. */
+		/* An error occurred. */
 		errno = ret;
 		return (-1);
 	}
@@ -419,14 +419,14 @@
 	PJDLOG_ASSERT(conn->pc_proto != NULL);
 
 	fd = proto_descriptor(conn);
-	if (fd < 0)
+	if (fd == -1)
 		return (-1);
 
 	tv.tv_sec = timeout;
 	tv.tv_usec = 0;
-	if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) < 0)
+	if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) == -1)
 		return (-1);
-	if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0)
+	if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1)
 		return (-1);
 
 	return (0);
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sbin/hastd/proto_common.c
--- a/head/sbin/hastd/proto_common.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sbin/hastd/proto_common.c	Fri Jan 13 15:51:01 2012 +0200
@@ -29,7 +29,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sbin/hastd/proto_common.c 223143 2011-06-16 08:31:06Z sobomax $");
+__FBSDID("$FreeBSD: head/sbin/hastd/proto_common.c 229945 2012-01-10 22:39:07Z pjd $");
 
 #include <sys/types.h>
 #include <sys/socket.h>
@@ -116,7 +116,7 @@
 		done = send(sock, data, sendsize, MSG_NOSIGNAL);
 		if (done == 0) {
 			return (ENOTCONN);
-		} else if (done < 0) {
+		} else if (done == -1) {
 			if (errno == EINTR)
 				continue;
 			if (errno == ENOBUFS) {
@@ -215,7 +215,7 @@
 	} while (done == -1 && errno == EINTR);
 	if (done == 0) {
 		return (ENOTCONN);
-	} else if (done < 0) {
+	} else if (done == -1) {
 		/*
 		 * If this is blocking socket and we got EAGAIN, this
 		 * means the request timed out. Translate errno to
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sbin/hastd/proto_socketpair.c
--- a/head/sbin/hastd/proto_socketpair.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sbin/hastd/proto_socketpair.c	Fri Jan 13 15:51:01 2012 +0200
@@ -28,7 +28,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
+__FBSDID("$FreeBSD: head/sbin/hastd/proto_socketpair.c 229945 2012-01-10 22:39:07Z pjd $");
 
 #include <sys/types.h>
 #include <sys/socket.h>
@@ -70,7 +70,7 @@
 	if (spctx == NULL)
 		return (errno);
 
-	if (socketpair(PF_UNIX, SOCK_STREAM, 0, spctx->sp_fd) < 0) {
+	if (socketpair(PF_UNIX, SOCK_STREAM, 0, spctx->sp_fd) == -1) {
 		ret = errno;
 		free(spctx);
 		return (ret);
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sbin/hastd/proto_tcp.c
--- a/head/sbin/hastd/proto_tcp.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sbin/hastd/proto_tcp.c	Fri Jan 13 15:51:01 2012 +0200
@@ -29,7 +29,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sbin/hastd/proto_tcp.c 225781 2011-09-27 07:52:39Z pjd $");
+__FBSDID("$FreeBSD: head/sbin/hastd/proto_tcp.c 229945 2012-01-10 22:39:07Z pjd $");
 
 #include <sys/param.h>	/* MAXHOSTNAMELEN */
 #include <sys/socket.h>
@@ -151,7 +151,7 @@
 		/* Port not given, use the default. */
 		port = defport;
 	} else {
-		if (numfromstr(pp + 1, 1, 65535, &port) < 0)
+		if (numfromstr(pp + 1, 1, 65535, &port) == -1)
 			return (errno);
 	}
 	(void)snprintf(portstr, sizeof(portstr), "%jd", (intmax_t)port);
@@ -275,7 +275,7 @@
 		tcp_close(tctx);
 		return (ret);
 	}
-	if (bind(tctx->tc_fd, (struct sockaddr *)&sa, sa.ss_len) < 0) {
+	if (bind(tctx->tc_fd, (struct sockaddr *)&sa, sa.ss_len) == -1) {
 		ret = errno;
 		tcp_close(tctx);
 		return (ret);
@@ -423,12 +423,12 @@
 	PJDLOG_ASSERT(tctx->tc_sa.ss_family != AF_UNSPEC);
 
 	if (bind(tctx->tc_fd, (struct sockaddr *)&tctx->tc_sa,
-	    tctx->tc_sa.ss_len) < 0) {
+	    tctx->tc_sa.ss_len) == -1) {
 		ret = errno;
 		tcp_close(tctx);
 		return (ret);
 	}
-	if (listen(tctx->tc_fd, 8) < 0) {
+	if (listen(tctx->tc_fd, 8) == -1) {
 		ret = errno;
 		tcp_close(tctx);
 		return (ret);
@@ -458,7 +458,7 @@
 	fromlen = tctx->tc_sa.ss_len;
 	newtctx->tc_fd = accept(tctx->tc_fd, (struct sockaddr *)&tctx->tc_sa,
 	    &fromlen);
-	if (newtctx->tc_fd < 0) {
+	if (newtctx->tc_fd == -1) {
 		ret = errno;
 		free(newtctx);
 		return (ret);
@@ -530,7 +530,7 @@
 		return (false);
 
 	salen = sizeof(sa2);
-	if (getpeername(tctx->tc_fd, (struct sockaddr *)&sa2, &salen) < 0)
+	if (getpeername(tctx->tc_fd, (struct sockaddr *)&sa2, &salen) == -1)
 		return (false);
 
 	if (sa1.ss_family != sa2.ss_family || sa1.ss_len != sa2.ss_len)
@@ -573,7 +573,7 @@
 	PJDLOG_ASSERT(tctx->tc_magic == TCP_CTX_MAGIC);
 
 	salen = sizeof(sa);
-	if (getsockname(tctx->tc_fd, (struct sockaddr *)&sa, &salen) < 0) {
+	if (getsockname(tctx->tc_fd, (struct sockaddr *)&sa, &salen) == -1) {
 		PJDLOG_VERIFY(strlcpy(addr, "N/A", size) < size);
 		return;
 	}
@@ -591,7 +591,7 @@
 	PJDLOG_ASSERT(tctx->tc_magic == TCP_CTX_MAGIC);
 
 	salen = sizeof(sa);
-	if (getpeername(tctx->tc_fd, (struct sockaddr *)&sa, &salen) < 0) {
+	if (getpeername(tctx->tc_fd, (struct sockaddr *)&sa, &salen) == -1) {
 		PJDLOG_VERIFY(strlcpy(addr, "N/A", size) < size);
 		return;
 	}
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sbin/hastd/proto_uds.c
--- a/head/sbin/hastd/proto_uds.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sbin/hastd/proto_uds.c	Fri Jan 13 15:51:01 2012 +0200
@@ -28,7 +28,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
+__FBSDID("$FreeBSD: head/sbin/hastd/proto_uds.c 229945 2012-01-10 22:39:07Z pjd $");
 
 /* UDS - UNIX Domain Socket */
 
@@ -144,7 +144,7 @@
 	PJDLOG_ASSERT(timeout >= -1);
 
 	if (connect(uctx->uc_fd, (struct sockaddr *)&uctx->uc_sun,
-	    sizeof(uctx->uc_sun)) < 0) {
+	    sizeof(uctx->uc_sun)) == -1) {
 		return (errno);
 	}
 
@@ -179,13 +179,13 @@
 
 	(void)unlink(uctx->uc_sun.sun_path);
 	if (bind(uctx->uc_fd, (struct sockaddr *)&uctx->uc_sun,
-	    sizeof(uctx->uc_sun)) < 0) {
+	    sizeof(uctx->uc_sun)) == -1) {
 		ret = errno;
 		uds_close(uctx);
 		return (ret);
 	}
 	uctx->uc_owner = getpid();
-	if (listen(uctx->uc_fd, 8) < 0) {
+	if (listen(uctx->uc_fd, 8) == -1) {
 		ret = errno;
 		uds_close(uctx);
 		return (ret);
@@ -214,7 +214,7 @@
 	fromlen = sizeof(newuctx->uc_sun);
 	newuctx->uc_fd = accept(uctx->uc_fd,
 	    (struct sockaddr *)&newuctx->uc_sun, &fromlen);
-	if (newuctx->uc_fd < 0) {
+	if (newuctx->uc_fd == -1) {
 		ret = errno;
 		free(newuctx);
 		return (ret);
@@ -274,7 +274,7 @@
 	PJDLOG_ASSERT(addr != NULL);
 
 	sunlen = sizeof(sun);
-	if (getsockname(uctx->uc_fd, (struct sockaddr *)&sun, &sunlen) < 0) {
+	if (getsockname(uctx->uc_fd, (struct sockaddr *)&sun, &sunlen) == -1) {
 		PJDLOG_VERIFY(strlcpy(addr, "N/A", size) < size);
 		return;
 	}
@@ -298,7 +298,7 @@
 	PJDLOG_ASSERT(addr != NULL);
 
 	sunlen = sizeof(sun);
-	if (getpeername(uctx->uc_fd, (struct sockaddr *)&sun, &sunlen) < 0) {
+	if (getpeername(uctx->uc_fd, (struct sockaddr *)&sun, &sunlen) == -1) {
 		PJDLOG_VERIFY(strlcpy(addr, "N/A", size) < size);
 		return;
 	}
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sbin/hastd/secondary.c
--- a/head/sbin/hastd/secondary.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sbin/hastd/secondary.c	Fri Jan 13 15:51:01 2012 +0200
@@ -29,7 +29,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sbin/hastd/secondary.c 228544 2011-12-15 22:05:23Z pjd $");
+__FBSDID("$FreeBSD: head/sbin/hastd/secondary.c 229945 2012-01-10 22:39:07Z pjd $");
 
 #include <sys/param.h>
 #include <sys/time.h>
@@ -181,7 +181,7 @@
 init_local(struct hast_resource *res)
 {
 
-	if (metadata_read(res, true) < 0)
+	if (metadata_read(res, true) == -1)
 		exit(EX_NOINPUT);
 }
 
@@ -262,7 +262,7 @@
 		 */
 		PJDLOG_ASSERT(res->hr_secondary_localcnt == 0);
 		res->hr_resuid = resuid;
-		if (metadata_write(res) < 0)
+		if (metadata_write(res) == -1)
 			exit(EX_NOINPUT);
 		if (nv_exists(nvin, "virgin")) {
 			free(map);
@@ -282,7 +282,7 @@
 		    (uintmax_t)resuid, (uintmax_t)res->hr_resuid);
 		pjdlog_error("%s", errmsg);
 		nv_add_string(nvout, errmsg, "errmsg");
-		if (hast_proto_send(res, res->hr_remotein, nvout, NULL, 0) < 0) {
+		if (hast_proto_send(res, res->hr_remotein, nvout, NULL, 0) == -1) {
 			pjdlog_exit(EX_TEMPFAIL, "Unable to send response to %s",
 			    res->hr_remoteaddr);
 		}
@@ -327,7 +327,7 @@
 		free(map);
 		pjdlog_error("Split-brain detected, exiting.");
 		nv_add_string(nvout, "Split-brain condition!", "errmsg");
-		if (hast_proto_send(res, res->hr_remotein, nvout, NULL, 0) < 0) {
+		if (hast_proto_send(res, res->hr_remotein, nvout, NULL, 0) == -1) {
 			pjdlog_exit(EX_TEMPFAIL, "Unable to send response to %s",
 			    res->hr_remoteaddr);
 		}
@@ -361,7 +361,7 @@
 		    (uintmax_t)res->hr_secondary_remotecnt);
 	}
 	nv_add_uint32(nvout, (uint32_t)mapsize, "mapsize");
-	if (hast_proto_send(res, res->hr_remotein, nvout, map, mapsize) < 0) {
+	if (hast_proto_send(res, res->hr_remotein, nvout, map, mapsize) == -1) {
 		pjdlog_exit(EX_TEMPFAIL, "Unable to send activemap to %s",
 		    res->hr_remoteaddr);
 	}
@@ -386,7 +386,7 @@
 	/*
 	 * Create communication channel between parent and child.
 	 */
-	if (proto_client(NULL, "socketpair://", &res->hr_ctrl) < 0) {
+	if (proto_client(NULL, "socketpair://", &res->hr_ctrl) == -1) {
 		KEEP_ERRNO((void)pidfile_remove(pfh));
 		pjdlog_exit(EX_OSERR,
 		    "Unable to create control sockets between parent and child");
@@ -394,14 +394,14 @@
 	/*
 	 * Create communication channel between child and parent.
 	 */
-	if (proto_client(NULL, "socketpair://", &res->hr_event) < 0) {
+	if (proto_client(NULL, "socketpair://", &res->hr_event) == -1) {
 		KEEP_ERRNO((void)pidfile_remove(pfh));
 		pjdlog_exit(EX_OSERR,
 		    "Unable to create event sockets between child and parent");
 	}
 
 	pid = fork();
-	if (pid < 0) {
+	if (pid == -1) {
 		KEEP_ERRNO((void)pidfile_remove(pfh));
 		pjdlog_exit(EX_OSERR, "Unable to fork");
 	}
@@ -441,9 +441,9 @@
 	PJDLOG_VERIFY(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
 
 	/* Error in setting timeout is not critical, but why should it fail? */
-	if (proto_timeout(res->hr_remotein, 2 * HAST_KEEPALIVE) < 0)
+	if (proto_timeout(res->hr_remotein, 2 * HAST_KEEPALIVE) == -1)
 		pjdlog_errno(LOG_WARNING, "Unable to set connection timeout");
-	if (proto_timeout(res->hr_remoteout, res->hr_timeout) < 0)
+	if (proto_timeout(res->hr_remoteout, res->hr_timeout) == -1)
 		pjdlog_errno(LOG_WARNING, "Unable to set connection timeout");
 
 	init_local(res);
@@ -623,7 +623,7 @@
 		pjdlog_debug(2, "recv: Taking free request.");
 		QUEUE_TAKE(free, hio);
 		pjdlog_debug(2, "recv: (%p) Got request.", hio);
-		if (hast_proto_recv_hdr(res->hr_remotein, &nv) < 0) {
+		if (hast_proto_recv_hdr(res->hr_remotein, &nv) == -1) {
 			secondary_exit(EX_TEMPFAIL,
 			    "Unable to receive request header");
 		}
@@ -666,7 +666,7 @@
 			continue;
 		} else if (hio->hio_cmd == HIO_WRITE) {
 			if (hast_proto_recv_data(res, res->hr_remotein, nv,
-			    hio->hio_data, MAXPHYS) < 0) {
+			    hio->hio_data, MAXPHYS) == -1) {
 				secondary_exit(EX_TEMPFAIL,
 				    "Unable to receive request data");
 			}
@@ -735,7 +735,7 @@
 			ret = pread(res->hr_localfd, hio->hio_data,
 			    hio->hio_length,
 			    hio->hio_offset + res->hr_localoff);
-			if (ret < 0)
+			if (ret == -1)
 				hio->hio_error = errno;
 			else if (ret != (int64_t)hio->hio_length)
 				hio->hio_error = EIO;
@@ -746,7 +746,7 @@
 			ret = pwrite(res->hr_localfd, hio->hio_data,
 			    hio->hio_length,
 			    hio->hio_offset + res->hr_localoff);
-			if (ret < 0)
+			if (ret == -1)
 				hio->hio_error = errno;
 			else if (ret != (int64_t)hio->hio_length)
 				hio->hio_error = EIO;
@@ -757,7 +757,7 @@
 			ret = g_delete(res->hr_localfd,
 			    hio->hio_offset + res->hr_localoff,
 			    hio->hio_length);
-			if (ret < 0)
+			if (ret == -1)
 				hio->hio_error = errno;
 			else
 				hio->hio_error = 0;
@@ -770,7 +770,7 @@
 				break;
 			}
 			ret = g_flush(res->hr_localfd);
-			if (ret < 0) {
+			if (ret == -1) {
 				if (errno == EOPNOTSUPP)
 					res->hr_localflush = false;
 				hio->hio_error = errno;
@@ -837,7 +837,7 @@
 		if (hio->hio_error != 0)
 			nv_add_int16(nvout, hio->hio_error, "error");
 		if (hast_proto_send(res, res->hr_remoteout, nvout, data,
-		    length) < 0) {
+		    length) == -1) {
 			secondary_exit(EX_TEMPFAIL, "Unable to send reply.");
 		}
 		nv_free(nvout);
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sbin/hastd/subr.c
--- a/head/sbin/hastd/subr.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sbin/hastd/subr.c	Fri Jan 13 15:51:01 2012 +0200
@@ -29,7 +29,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sbin/hastd/subr.c 225781 2011-09-27 07:52:39Z pjd $");
+__FBSDID("$FreeBSD: head/sbin/hastd/subr.c 229945 2012-01-10 22:39:07Z pjd $");
 
 #ifdef HAVE_CAPSICUM
 #include <sys/capability.h>
@@ -86,13 +86,13 @@
 	if (res->hr_localfd == -1) {
 		res->hr_localfd = open(res->hr_localpath,
 		    dowrite ? O_RDWR : O_RDONLY);
-		if (res->hr_localfd < 0) {
+		if (res->hr_localfd == -1) {
 			pjdlog_errno(LOG_ERR, "Unable to open %s",
 			    res->hr_localpath);
 			return (-1);
 		}
 	}
-	if (fstat(res->hr_localfd, &sb) < 0) {
+	if (fstat(res->hr_localfd, &sb) == -1) {
 		pjdlog_errno(LOG_ERR, "Unable to stat %s", res->hr_localpath);
 		return (-1);
 	}
@@ -101,14 +101,14 @@
 		 * If this is character device, it is most likely GEOM provider.
 		 */
 		if (ioctl(res->hr_localfd, DIOCGMEDIASIZE,
-		    &res->hr_local_mediasize) < 0) {
+		    &res->hr_local_mediasize) == -1) {
 			pjdlog_errno(LOG_ERR,
 			    "Unable obtain provider %s mediasize",
 			    res->hr_localpath);
 			return (-1);
 		}
 		if (ioctl(res->hr_localfd, DIOCGSECTORSIZE,
-		    &res->hr_local_sectorsize) < 0) {
+		    &res->hr_local_sectorsize) == -1) {
 			pjdlog_errno(LOG_ERR,
 			    "Unable obtain provider %s sectorsize",
 			    res->hr_localpath);
@@ -149,7 +149,7 @@
 }
 
 int
-drop_privs(struct hast_resource *res)
+drop_privs(const struct hast_resource *res)
 {
 	char jailhost[sizeof(res->hr_name) * 2];
 	struct jail jailst;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sbin/hastd/subr.h
--- a/head/sbin/hastd/subr.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sbin/hastd/subr.h	Fri Jan 13 15:51:01 2012 +0200
@@ -27,7 +27,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $FreeBSD: head/sbin/hastd/subr.h 221899 2011-05-14 17:02:03Z pjd $
+ * $FreeBSD: head/sbin/hastd/subr.h 229699 2012-01-06 12:27:17Z pjd $
  */
 
 #ifndef	_SUBR_H_
@@ -51,6 +51,6 @@
 
 int provinfo(struct hast_resource *res, bool dowrite);
 const char *role2str(int role);
-int drop_privs(struct hast_resource *res);
+int drop_privs(const struct hast_resource *res);
 
 #endif	/* !_SUBR_H_ */
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sbin/ifconfig/ifieee80211.c
--- a/head/sbin/ifconfig/ifieee80211.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sbin/ifconfig/ifieee80211.c	Fri Jan 13 15:51:01 2012 +0200
@@ -24,7 +24,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $FreeBSD: head/sbin/ifconfig/ifieee80211.c 227336 2011-11-08 14:22:40Z adrian $
+ * $FreeBSD: head/sbin/ifconfig/ifieee80211.c 229914 2012-01-10 02:59:18Z eadler $
  */
 
 /*-
@@ -884,7 +884,7 @@
 }
 
 /*
- * This function is purely a NetBSD compatability interface.  The NetBSD
+ * This function is purely a NetBSD compatibility interface.  The NetBSD
  * interface is too inflexible, but it's there so we'll support it since
  * it's not all that hard.
  */
@@ -4383,7 +4383,6 @@
 
 	if (get80211val(s, IEEE80211_IOC_WEP, &wepmode) != -1 &&
 	    wepmode != IEEE80211_WEP_NOSUP) {
-		int firstkey;
 
 		switch (wepmode) {
 		case IEEE80211_WEP_OFF:
@@ -4419,7 +4418,6 @@
 			goto end;
 		}
 
-		firstkey = 1;
 		for (i = 0; i < num; i++) {
 			struct ieee80211req_key ik;
 
@@ -4433,7 +4431,6 @@
 				if (verbose)
 					LINE_BREAK();
 				printkey(&ik);
-				firstkey = 0;
 			}
 		}
 end:
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sbin/ipfw/ipfw2.c
--- a/head/sbin/ipfw/ipfw2.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sbin/ipfw/ipfw2.c	Fri Jan 13 15:51:01 2012 +0200
@@ -17,7 +17,7 @@
  *
  * NEW command line interface for IP firewall facility
  *
- * $FreeBSD: head/sbin/ipfw/ipfw2.c 225044 2011-08-20 17:05:11Z bz $
+ * $FreeBSD: head/sbin/ipfw/ipfw2.c 229778 2012-01-07 16:09:33Z uqs $
  */
 
 #include <sys/types.h>
@@ -454,8 +454,8 @@
  * of the first.  A warning is printed to stderr in the case that the
  * first string does not match the third.
  *
- * This function exists to warn about the bizzare construction
- * strncmp(str, "by", 2) which is used to allow people to use a shotcut
+ * This function exists to warn about the bizarre construction
+ * strncmp(str, "by", 2) which is used to allow people to use a shortcut
  * for "bytes".  The problem is that in addition to accepting "by",
  * "byt", "byte", and "bytes", it also excepts "by_rabid_dogs" and any
  * other string beginning with "by".
@@ -2866,9 +2866,9 @@
 			((struct sockaddr_in*)&result)->sin_addr.s_addr =
 			    INADDR_ANY;
 		} else {
-			/* 
+			/*
 			 * Resolve the host name or address to a family and a
-			 * network representation of the addres.
+			 * network representation of the address.
 			 */
 			if (getaddrinfo(*av, NULL, NULL, &res))
 				errx(EX_DATAERR, NULL);
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sbin/ipfw/main.c
--- a/head/sbin/ipfw/main.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sbin/ipfw/main.c	Fri Jan 13 15:51:01 2012 +0200
@@ -17,7 +17,7 @@
  *
  * Command line interface for IP firewall facility
  *
- * $FreeBSD: head/sbin/ipfw/main.c 229403 2012-01-03 18:51:58Z ed $
+ * $FreeBSD: head/sbin/ipfw/main.c 229778 2012-01-07 16:09:33Z uqs $
  */
 
 #include <sys/wait.h>
@@ -167,7 +167,7 @@
 					i++;
 				bcopy(arg+j, av_p, i-j);
 				av[ac] = av_p;
-				av_p += i-j;	/* the lenght of the string */
+				av_p += i-j;	/* the length of the string */
 				*av_p++ = '\0';
 				ac++;
 				j = i + 1;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sbin/mdmfs/mdmfs.c
--- a/head/sbin/mdmfs/mdmfs.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sbin/mdmfs/mdmfs.c	Fri Jan 13 15:51:01 2012 +0200
@@ -31,7 +31,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sbin/mdmfs/mdmfs.c 225534 2011-09-13 20:16:11Z kib $");
+__FBSDID("$FreeBSD: head/sbin/mdmfs/mdmfs.c 229778 2012-01-07 16:09:33Z uqs $");
 
 #include <sys/param.h>
 #include <sys/mdioctl.h>
@@ -632,7 +632,7 @@
 		 * loop.
 		 */
 		(void)fprintf(stderr, "DEBUG: running:");
-		/* Should be equivilent to 'cmd' (before strsep, of course). */
+		/* Should be equivalent to 'cmd' (before strsep, of course). */
 		for (i = 0; argv[i] != NULL; i++)
 			(void)fprintf(stderr, " %s", argv[i]);
 		(void)fprintf(stderr, "\n");
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sbin/mount_nfs/mount_nfs.c
--- a/head/sbin/mount_nfs/mount_nfs.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sbin/mount_nfs/mount_nfs.c	Fri Jan 13 15:51:01 2012 +0200
@@ -42,7 +42,7 @@
 #endif /* not lint */
 #endif
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sbin/mount_nfs/mount_nfs.c 227507 2011-11-14 18:52:07Z jhb $");
+__FBSDID("$FreeBSD: head/sbin/mount_nfs/mount_nfs.c 229778 2012-01-07 16:09:33Z uqs $");
 
 #include <sys/param.h>
 #include <sys/linker.h>
@@ -788,7 +788,7 @@
 	for (;;) {
 		/*
 		 * Try each entry returned by getaddrinfo(). Note the
-		 * occurence of remote errors by setting `remoteerr'.
+		 * occurrence of remote errors by setting `remoteerr'.
 		 */
 		remoteerr = 0;
 		for (ai = ai_nfs; ai != NULL; ai = ai->ai_next) {
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sbin/newfs/mkfs.c
--- a/head/sbin/newfs/mkfs.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sbin/newfs/mkfs.c	Fri Jan 13 15:51:01 2012 +0200
@@ -42,7 +42,7 @@
 #endif /* not lint */
 #endif
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sbin/newfs/mkfs.c 227081 2011-11-04 13:36:02Z ed $");
+__FBSDID("$FreeBSD: head/sbin/newfs/mkfs.c 229913 2012-01-10 02:59:09Z eadler $");
 
 #include <sys/param.h>
 #include <sys/disklabel.h>
@@ -989,9 +989,7 @@
 iput(union dinode *ip, ino_t ino)
 {
 	ufs2_daddr_t d;
-	int c;
 
-	c = ino_to_cg(&sblock, ino);
 	bread(&disk, part_ofs + fsbtodb(&sblock, cgtod(&sblock, 0)), (char *)&acg,
 	    sblock.fs_cgsize);
 	if (acg.cg_magic != CG_MAGIC) {
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sbin/ping6/ping6.c
--- a/head/sbin/ping6/ping6.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sbin/ping6/ping6.c	Fri Jan 13 15:51:01 2012 +0200
@@ -76,7 +76,7 @@
 #endif /* not lint */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
+__FBSDID("$FreeBSD: head/sbin/ping6/ping6.c 229912 2012-01-10 02:59:01Z eadler $");
 
 /*
  * Using the InterNet Control Message Protocol (ICMP) "ECHO" facility,
@@ -1166,7 +1166,7 @@
 
 			/*
 			 * receive control messages only. Process the
-			 * exceptions (currently the only possiblity is
+			 * exceptions (currently the only possibility is
 			 * a path MTU notification.)
 			 */
 			if ((mtu = get_pathmtu(&m)) > 0) {
@@ -1812,7 +1812,7 @@
 	struct ip6_hbh *ext;
 	int currentlen;
 	u_int8_t type;
-	socklen_t extlen, len, origextlen;
+	socklen_t extlen, len;
 	void *databuf;
 	size_t offset;
 	u_int16_t value2;
@@ -1828,7 +1828,6 @@
 	 *     subtract the size of a cmsg structure from the buffer size.
 	 */
 	if (bufsize < (extlen  + CMSG_SPACE(0))) {
-		origextlen = extlen;
 		extlen = bufsize - CMSG_SPACE(0);
 		warnx("options truncated, showing only %u (total=%u)",
 		    (unsigned int)(extlen / 8 - 1),
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sbin/reboot/nextboot.8
--- a/head/sbin/reboot/nextboot.8	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sbin/reboot/nextboot.8	Fri Jan 13 15:51:01 2012 +0200
@@ -22,7 +22,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.\" $FreeBSD$
+.\" $FreeBSD: head/sbin/reboot/nextboot.8 229778 2012-01-07 16:09:33Z uqs $
 .\"
 .Dd November 4, 2002
 .Dt NEXTBOOT 8
@@ -117,7 +117,7 @@
 .Nm
 code is implemented in the
 .Xr loader 8 .
-It is not the most throughly tested code.
+It is not the most thoroughly tested code.
 It is also my first attempt to write in Forth.
 .Pp
 Finally, it does some evil things like writing to the file system before it
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sbin/recoverdisk/recoverdisk.1
--- a/head/sbin/recoverdisk/recoverdisk.1	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sbin/recoverdisk/recoverdisk.1	Fri Jan 13 15:51:01 2012 +0200
@@ -22,9 +22,9 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.\" $FreeBSD: head/sbin/recoverdisk/recoverdisk.1 221304 2011-05-01 20:14:10Z uqs $
+.\" $FreeBSD: head/sbin/recoverdisk/recoverdisk.1 229697 2012-01-06 11:42:03Z uqs $
 .\"
-.Dd May 1, 2011
+.Dd January 5, 2012
 .Dt RECOVERDISK 1
 .Os
 .Sh NAME
@@ -124,7 +124,11 @@
 
 .Ed
 .Sh SEE ALSO
-.Xr dd 1
+.Xr dd 1 ,
+.Xr ada 4,
+.Xr cam 4,
+.Xr cd 4,
+.Xr da 4
 .Sh HISTORY
 The
 .Nm
@@ -149,6 +153,18 @@
 This is harmless and can be avoided by setting
 .Fl b
 to no more than 128kB.
+.Pp
+.Nm
+needs to know about read errors as fast as possible, i.e. retries by lower
+layers will usually slow down the operation.
+When using
+.Xr cam 4
+attached drives, you may want to set kern.cam.XX.retry_count to zero, e.g.:
+.Bd -literal
+# sysctl kern.cam.ada.retry_count=0
+# sysctl kern.cam.cd.retry_count=0
+# sysctl kern.cam.da.retry_count=0
+.Ed
 .\".Pp
 .\"When reading from optical media, a bug in the GEOM framework will
 .\"prevent it from seeing that the media has been removed.
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sbin/route/route.c
--- a/head/sbin/route/route.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sbin/route/route.c	Fri Jan 13 15:51:01 2012 +0200
@@ -40,7 +40,7 @@
 #endif /* not lint */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
+__FBSDID("$FreeBSD: head/sbin/route/route.c 229766 2012-01-07 09:01:19Z ume $");
 
 #include <sys/param.h>
 #include <sys/file.h>
@@ -375,7 +375,8 @@
 #ifdef __KAME__
 		if (sa->sa_len == sizeof(struct sockaddr_in6) &&
 		    (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr) ||
-		     IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr)) &&
+		     IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr) ||
+		     IN6_IS_ADDR_MC_NODELOCAL(&sin6.sin6_addr)) &&
 		    sin6.sin6_scope_id == 0) {
 			sin6.sin6_scope_id =
 			    ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
@@ -500,7 +501,8 @@
 #ifdef __KAME__
 		if (sa->sa_len == sizeof(struct sockaddr_in6) &&
 		    (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr) ||
-		     IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr)) &&
+		     IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr) ||
+		     IN6_IS_ADDR_MC_NODELOCAL(&sin6.sin6_addr)) &&
 		    sin6.sin6_scope_id == 0) {
 			sin6.sin6_scope_id =
 			    ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
@@ -1002,7 +1004,8 @@
 		memcpy(&su->sin6, res->ai_addr, sizeof(su->sin6));
 #ifdef __KAME__
 		if ((IN6_IS_ADDR_LINKLOCAL(&su->sin6.sin6_addr) ||
-		     IN6_IS_ADDR_MC_LINKLOCAL(&su->sin6.sin6_addr)) &&
+		     IN6_IS_ADDR_MC_LINKLOCAL(&su->sin6.sin6_addr) ||
+		     IN6_IS_ADDR_MC_NODELOCAL(&su->sin6.sin6_addr)) &&
 		    su->sin6.sin6_scope_id) {
 			*(u_int16_t *)&su->sin6.sin6_addr.s6_addr[2] =
 				htons(su->sin6.sin6_scope_id);
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sbin/routed/main.c
--- a/head/sbin/routed/main.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sbin/routed/main.c	Fri Jan 13 15:51:01 2012 +0200
@@ -26,7 +26,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $FreeBSD$
+ * $FreeBSD: head/sbin/routed/main.c 229778 2012-01-07 16:09:33Z uqs $
  */
 
 #include "defs.h"
@@ -45,7 +45,7 @@
 __RCSID("$NetBSD$");
 #include <util.h>
 #elif defined(__FreeBSD__)
-__RCSID("$FreeBSD$");
+__RCSID("$FreeBSD: head/sbin/routed/main.c 229778 2012-01-07 16:09:33Z uqs $");
 #else
 __RCSID("$Revision: 2.31 $");
 #ident "$Revision: 2.31 $"
@@ -414,7 +414,7 @@
 			continue;
 		}
 
-		/* Check the kernel table occassionally for mysteriously
+		/* Check the kernel table occasionally for mysteriously
 		 * evaporated routes
 		 */
 		timevalsub(&t2, &flush_kern_timer, &now);
@@ -446,7 +446,7 @@
 				 * the previous update was finished.
 				 * Even if we just started after discovering
 				 * a 2nd interface or were otherwise delayed,
-				 * pick a 30-second aniversary of the
+				 * pick a 30-second anniversary of the
 				 * original broadcast time.
 				 */
 				n = 1 + (0-t2.tv_sec)/SUPPLY_INTERVAL;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sbin/routed/radix.c
--- a/head/sbin/routed/radix.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sbin/routed/radix.c	Fri Jan 13 15:51:01 2012 +0200
@@ -28,7 +28,7 @@
  *
  *	@(#)radix.c	8.4 (Berkeley) 11/2/94
  *
- * $FreeBSD$
+ * $FreeBSD: head/sbin/routed/radix.c 229778 2012-01-07 16:09:33Z uqs $
  */
 
 /*
@@ -40,7 +40,7 @@
 #ifdef __NetBSD__
 __RCSID("$NetBSD$");
 #elif defined(__FreeBSD__)
-__RCSID("$FreeBSD$");
+__RCSID("$FreeBSD: head/sbin/routed/radix.c 229778 2012-01-07 16:09:33Z uqs $");
 #else
 __RCSID("$Revision: 2.23 $");
 #ident "$Revision: 2.23 $"
@@ -97,7 +97,7 @@
  * node as high in the tree as we can go.
  *
  * The present version of the code makes use of normal routes in short-
- * circuiting an explict mask and compare operation when testing whether
+ * circuiting an explicit mask and compare operation when testing whether
  * a key satisfies a normal route, and also in remembering the unique leaf
  * that governs a subtree.
  */
@@ -247,7 +247,7 @@
 	 *
 	 * In this case, we have a complete match of the key.  Unless
 	 * the node is one of the roots, we are finished.
-	 * If it is the zeros root, then take what we have, prefering
+	 * If it is the zeros root, then take what we have, preferring
 	 * any real data.
 	 * If it is the ones root, then pretend the target key was followed
 	 * by a byte of zeros.
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sbin/routed/table.c
--- a/head/sbin/routed/table.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sbin/routed/table.c	Fri Jan 13 15:51:01 2012 +0200
@@ -26,7 +26,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $FreeBSD$
+ * $FreeBSD: head/sbin/routed/table.c 229778 2012-01-07 16:09:33Z uqs $
  */
 
 #include "defs.h"
@@ -34,7 +34,7 @@
 #ifdef __NetBSD__
 __RCSID("$NetBSD$");
 #elif defined(__FreeBSD__)
-__RCSID("$FreeBSD$");
+__RCSID("$FreeBSD: head/sbin/routed/table.c 229778 2012-01-07 16:09:33Z uqs $");
 #else
 __RCSID("$Revision: 2.27 $");
 #ident "$Revision: 2.27 $"
@@ -2071,7 +2071,7 @@
 	/* try to switch to an alternative */
 	rtswitch(RT, 0);
 
-	/* Delete a dead route after it has been publically mourned. */
+	/* Delete a dead route after it has been publicly mourned. */
 	if (now_garbage > RT->rt_time) {
 		rtdelete(RT);
 		return 0;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sbin/tunefs/tunefs.c
--- a/head/sbin/tunefs/tunefs.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sbin/tunefs/tunefs.c	Fri Jan 13 15:51:01 2012 +0200
@@ -39,7 +39,7 @@
 #endif /* not lint */
 #endif
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sbin/tunefs/tunefs.c 227081 2011-11-04 13:36:02Z ed $");
+__FBSDID("$FreeBSD: head/sbin/tunefs/tunefs.c 229911 2012-01-10 02:58:52Z eadler $");
 
 /*
  * tunefs: change layout parameters to an existing file system.
@@ -90,7 +90,7 @@
 	int Aflag, aflag, eflag, evalue, fflag, fvalue, jflag, Jflag, Lflag;
 	int lflag, mflag, mvalue, Nflag, nflag, oflag, ovalue, pflag, sflag;
 	int tflag;
-	int svalue, Sflag, Svalue;
+	int svalue, Svalue;
 	int ch, found_arg, i;
 	const char *chg[2];
 	struct ufs_args args;
@@ -269,7 +269,6 @@
 			if (Svalue < SUJ_MIN)
 				errx(10, "%s must be >= %d (was %s)",
 				    name, SUJ_MIN, optarg);
-			Sflag = 1;
 			break;
 
 		case 't':
diff -r ebe87ce4d7dd -r 1750de5b0399 head/share/examples/diskless/README.TEMPLATING
--- a/head/share/examples/diskless/README.TEMPLATING	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/share/examples/diskless/README.TEMPLATING	Fri Jan 13 15:51:01 2012 +0200
@@ -11,7 +11,7 @@
 and the /usr/share/examples/diskless/clone_root script which can
 be useful to set up clients and server for diskless boot.
 
---- $FreeBSD: head/share/examples/diskless/README.TEMPLATING 222179 2011-05-22 14:23:48Z uqs $ ---
+--- $FreeBSD: head/share/examples/diskless/README.TEMPLATING 229831 2012-01-09 00:48:19Z eadler $ ---
 ------------------------------------------------------------------------
 
 		      TEMPLATING machine configurations
@@ -94,8 +94,8 @@
 	/usr/ports	( note 5 )
 	/usr/src	( note 5 )
 	/usr/local/crack ( note 5 )
-	/usr/X11R6/lib/X11/xdm/xdm-errors ( note 6 )
-	/usr/X11R6/lib/X11/xdm/xdm-pid 	  ( note 6 )
+	/usr/local/lib/X11/xdm/xdm-errors ( note 6 )
+	/usr/local/lib/X11/xdm/xdm-pid 	  ( note 6 )
 	/usr/local/etc/ssh_host_key	  ( note 6 )
 	/usr/local/etc/ssh_host_key.pub	  ( note 6 )
 	/usr/local/etc/ssh_random_seed	  ( note 6 )
@@ -120,7 +120,7 @@
 		do not want to template such directories.
 
 	note 6: Note that you can solve the problem of xdm and sshd creating
-		files in /usr.  With xdm, edit /usr/X11R6/lib/xdm/xdm-config
+		files in /usr.  With xdm, edit /usr/local/lib/xdm/xdm-config
 		and change the errorLogFile and pidFile config lines.
 
 		With sshd, add 'HostKey' and 'RandomSeed' directives to specify
diff -r ebe87ce4d7dd -r 1750de5b0399 head/share/examples/ppp/ppp.linkdown.sample
--- a/head/share/examples/ppp/ppp.linkdown.sample	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/share/examples/ppp/ppp.linkdown.sample	Fri Jan 13 15:51:01 2012 +0200
@@ -12,7 +12,7 @@
 #  3) If no label has been found, use MYADDR if it exists.
 #
 #
-# $FreeBSD$
+# $FreeBSD: head/share/examples/ppp/ppp.linkdown.sample 229831 2012-01-09 00:48:19Z eadler $
 #
 #########################################################################
 
@@ -23,7 +23,7 @@
 # ``auplay'' (assuming NAS is installed and configured).
 #
 MYADDR:
- !bg /usr/X11R6/bin/auplay /etc/ppp/linkdown.au
+ !bg /usr/local/bin/auplay /etc/ppp/linkdown.au
 
 # If you're running ``ppp -auto -nat dynamic-nat-auto'', and are
 # assigned a dynamic IP number by the peer, this may be worth while
diff -r ebe87ce4d7dd -r 1750de5b0399 head/share/examples/printing/hpvf
--- a/head/share/examples/printing/hpvf	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/share/examples/printing/hpvf	Fri Jan 13 15:51:01 2012 +0200
@@ -1,10 +1,10 @@
 #!/bin/sh
 #
+# $FreeBSD: head/share/examples/printing/hpvf 229831 2012-01-09 00:48:19Z eadler $
+#
 #  hpvf - Convert GIF files into HP/PCL, then print
 #  Installed in /usr/local/libexec/hpvf
 
-PATH=/usr/X11R6/bin:$PATH; export PATH
-
 giftopnm | ppmtopgm | pgmtopbm | pbmtolj -resolution 300 \
     && exit 0 \
     || exit 2
diff -r ebe87ce4d7dd -r 1750de5b0399 head/share/examples/scsi_target/scsi_cmds.c
--- a/head/share/examples/scsi_target/scsi_cmds.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/share/examples/scsi_target/scsi_cmds.c	Fri Jan 13 15:51:01 2012 +0200
@@ -25,7 +25,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $FreeBSD: head/share/examples/scsi_target/scsi_cmds.c 228462 2011-12-13 11:13:28Z mav $
+ * $FreeBSD: head/share/examples/scsi_target/scsi_cmds.c 229997 2012-01-12 00:34:33Z ken $
  */
 
 #include <stdio.h>
@@ -328,7 +328,7 @@
 		bcopy(&inq_data, ctio->data_ptr, sizeof(inq_data));
 		ctio->dxfer_len = inq_data.additional_length + 4;
 		ctio->dxfer_len = min(ctio->dxfer_len,
-				      SCSI_CDB6_LEN(inq->length));
+				      scsi_2btoul(inq->length));
 		ctio->ccb_h.flags |= CAM_DIR_IN | CAM_SEND_STATUS;
 		ctio->scsi_status = SCSI_STATUS_OK;
 	}
diff -r ebe87ce4d7dd -r 1750de5b0399 head/share/man/man3/ATOMIC_VAR_INIT.3
--- a/head/share/man/man3/ATOMIC_VAR_INIT.3	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/share/man/man3/ATOMIC_VAR_INIT.3	Fri Jan 13 15:51:01 2012 +0200
@@ -22,7 +22,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.\" $FreeBSD: head/share/man/man3/ATOMIC_VAR_INIT.3 228920 2011-12-27 22:14:35Z ed $
+.\" $FreeBSD: head/share/man/man3/ATOMIC_VAR_INIT.3 229651 2012-01-05 21:36:12Z uqs $
 .\"
 .Dd December 27, 2011
 .Dt ATOMIC_VAR_INIT 3
@@ -274,7 +274,7 @@
 .Dv memory_order_seq_cst .
 .Pp
 Instead of using the atomic operations provided by this interface,
-.St -isoC-11
+.St -isoC-2011
 allows the atomic variables to be modified directly using built-in
 language operators.
 This behaviour cannot be emulated for older compilers.
@@ -292,7 +292,7 @@
 .Xr atomic 9
 .Sh STANDARDS
 These macros attempt to conform to
-.St -isoC-11 .
+.St -isoC-2011 .
 .Sh HISTORY
 These macros appeared in
 .Fx 10.0 .
diff -r ebe87ce4d7dd -r 1750de5b0399 head/share/man/man3/tgmath.3
--- a/head/share/man/man3/tgmath.3	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/share/man/man3/tgmath.3	Fri Jan 13 15:51:01 2012 +0200
@@ -22,7 +22,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.\" $FreeBSD: head/share/man/man3/tgmath.3 229575 2012-01-05 10:46:22Z ed $
+.\" $FreeBSD: head/share/man/man3/tgmath.3 229651 2012-01-05 21:36:12Z uqs $
 .\"
 .Dd January 4, 2012
 .Dt TGMATH 3
@@ -141,13 +141,13 @@
 .Fx 5.3 .
 .Sh COMPILER SUPPORT
 Before
-.St -isoC-11 ,
+.St -isoC-2011 ,
 the header
 .In tgmath.h
 could not be implemented with strictly conforming C code and needed
 special compiler support.
 As of
-.St -isoC-11 ,
+.St -isoC-2011 ,
 this header file can be implemented using the
 .Fn _Generic
 language keyword.
diff -r ebe87ce4d7dd -r 1750de5b0399 head/share/man/man4/apic.4
--- a/head/share/man/man4/apic.4	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/share/man/man4/apic.4	Fri Jan 13 15:51:01 2012 +0200
@@ -22,7 +22,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.\" $FreeBSD: head/share/man/man4/apic.4 228739 2011-12-20 15:19:33Z mav $
+.\" $FreeBSD: head/share/man/man4/apic.4 229657 2012-01-05 21:43:33Z uqs $
 .\"
 .Dd December 20, 2011
 .Dt APIC 4
@@ -61,7 +61,7 @@
 I/O APICs contain a redirection table, which is used to route the interrupts
 they receive from peripheral buses to one or more local APICs.
 .Pp
-Each local APIC includes one 32-bit programable timer.
+Each local APIC includes one 32-bit programmable timer.
 This driver uses them to supply kernel with one event timer named "LAPIC".
 Event timer provided by the driver supports both one-shot an periodic modes.
 Because of local APIC nature it is per-CPU.
diff -r ebe87ce4d7dd -r 1750de5b0399 head/share/man/man4/bce.4
--- a/head/share/man/man4/bce.4	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/share/man/man4/bce.4	Fri Jan 13 15:51:01 2012 +0200
@@ -26,7 +26,7 @@
 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 .\" THE POSSIBILITY OF SUCH DAMAGE.
 .\"
-.\" $FreeBSD$
+.\" $FreeBSD: head/share/man/man4/bce.4 229585 2012-01-05 11:16:42Z brueffer $
 .\"
 .Dd October 7, 2009
 .Dt BCE 4
@@ -251,7 +251,7 @@
 .It "bce%d: Could not allocate status block DMA memory!"
 The driver could not allocate DMA addressable memory for the controller's
 status block.
-.It "bce_d: Could not map status block DMA memory!"
+.It "bce%d: Could not map status block DMA memory!"
 The driver could not map the status block memory into the controller's DMA
 address space.
 .It "bce%d: Could not allocate statistics block DMA tag!"
diff -r ebe87ce4d7dd -r 1750de5b0399 head/share/man/man4/carp.4
--- a/head/share/man/man4/carp.4	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/share/man/man4/carp.4	Fri Jan 13 15:51:01 2012 +0200
@@ -24,7 +24,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.\" $FreeBSD: head/share/man/man4/carp.4 228736 2011-12-20 13:53:31Z glebius $
+.\" $FreeBSD: head/share/man/man4/carp.4 229657 2012-01-05 21:43:33Z uqs $
 .\"
 .Dd December 20, 2011
 .Dt CARP 4
@@ -193,7 +193,7 @@
 hooks.
 Master/slave events are signalled under system
 .Dv CARP .
-Subsystem specifies vhid and name of interface, where event occured.
+Subsystem specifies vhid and name of interface, where event occurred.
 Type of the message displays new state of vhid.
 Please see
 .Xr devd.conf 5
diff -r ebe87ce4d7dd -r 1750de5b0399 head/share/man/man4/ng_ksocket.4
--- a/head/share/man/man4/ng_ksocket.4	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/share/man/man4/ng_ksocket.4	Fri Jan 13 15:51:01 2012 +0200
@@ -32,9 +32,9 @@
 .\"
 .\" Author: Archie Cobbs <archie at FreeBSD.org>
 .\"
-.\" $FreeBSD$
+.\" $FreeBSD: head/share/man/man4/ng_ksocket.4 229930 2012-01-10 12:45:24Z melifaro $
 .\"
-.Dd October 28, 2005
+.Dd January 09, 2012
 .Dt NG_KSOCKET 4
 .Os
 .Sh NAME
@@ -199,7 +199,7 @@
 the address is the pathname as a doubly quoted string.
 .Pp
 Examples:
-.Bl -tag -width XXXXXXXXXX
+.Bl -tag -width "PF_LOCAL"
 .It Dv PF_LOCAL
 local/"/tmp/foo.socket"
 .It Dv PF_INET
@@ -215,6 +215,12 @@
 form for that structure is used.
 In the future, more
 convenient encoding of the more common socket options may be supported.
+.Pp
+Setting socket options example:
+.Bl -tag -width "PF_LOCAL"
+.It Set FIB 2 for a socket (SOL_SOCKET, SO_SETFIB):
+.Dv "setopt \&{ level=0xffff name=0x1014 data=[ 2 ] \&}"
+.El
 .Sh SHUTDOWN
 This node shuts down upon receipt of a
 .Dv NGM_SHUTDOWN
diff -r ebe87ce4d7dd -r 1750de5b0399 head/share/man/man5/ar.5
--- a/head/share/man/man5/ar.5	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/share/man/man5/ar.5	Fri Jan 13 15:51:01 2012 +0200
@@ -21,7 +21,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.\" $FreeBSD: head/share/man/man5/ar.5 221594 2011-05-07 10:44:08Z kaiw $
+.\" $FreeBSD: head/share/man/man5/ar.5 229657 2012-01-05 21:43:33Z uqs $
 .\"
 .Dd November 28, 2010
 .Os
@@ -126,7 +126,7 @@
 names for members.
 .Bl -tag -width "SVR4/GNU"
 .It "BSD"
-File names that are upto 16 bytes long and which do not contain
+File names that are up to 16 bytes long and which do not contain
 embedded spaces are stored directly in the
 .Ar ar_name
 field of the archive header.
@@ -164,7 +164,7 @@
 .Dq "A BC D"
 .Pc .
 .It "SVR4/GNU"
-File names that are upto 15 characters long are stored directly in the
+File names that are up to 15 characters long are stored directly in the
 .Ar ar_name
 field of the header, terminated by a
 .Dq Li /
diff -r ebe87ce4d7dd -r 1750de5b0399 head/share/man/man8/rc.subr.8
--- a/head/share/man/man8/rc.subr.8	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/share/man/man8/rc.subr.8	Fri Jan 13 15:51:01 2012 +0200
@@ -27,7 +27,7 @@
 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 .\" POSSIBILITY OF SUCH DAMAGE.
 .\"
-.\" $FreeBSD$
+.\" $FreeBSD: head/share/man/man8/rc.subr.8 230007 2012-01-12 06:48:11Z rea $
 .\"
 .Dd May 18, 2007
 .Dt RC.SUBR 8
@@ -339,7 +339,7 @@
 being the list of valid arguments
 prefixed by
 .Sm off
-.Dq Bq Li fast | force | one .
+.Dq Bq Li fast | force | one | quiet .
 .Sm on
 .It Ic reverse_list Ar item ...
 Print the list of
@@ -427,6 +427,22 @@
 being set to
 .Dq Li YES ,
 but performs all the other prerequisite tests.
+.It Li quiet
+Inhibits some verbose diagnostics.
+Currently, this includes messages
+.Qq Starting ${name}
+(as checked by
+.Ic check_startmsgs
+inside
+.Nm )
+and errors about usage of services that are not enabled in
+.Xr rc.conf 5 .
+This prefix also sets
+.Va rc_quiet Ns = Ns Li YES .
+.Em Please, note:
+.Va rc_quiet
+is not intended to completely mask all debug and warning messages,
+but only certain small classes of them.
 .El
 .Pp
 .Ic run_rc_command
diff -r ebe87ce4d7dd -r 1750de5b0399 head/share/mk/bsd.crunchgen.mk
--- a/head/share/mk/bsd.crunchgen.mk	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/share/mk/bsd.crunchgen.mk	Fri Jan 13 15:51:01 2012 +0200
@@ -22,8 +22,10 @@
 # Specific links can be suppressed by setting
 # CRUNCH_SUPPRESS_LINK_$(NAME) to 1.
 #
+# If CRUNCH_GENERATE_LINKS is set to no, no links will be generated.
+#
 
-# $FreeBSD$
+# $FreeBSD: head/share/mk/bsd.crunchgen.mk 229658 2012-01-05 21:49:43Z adrian $
 
 ##################################################################
 #  The following is pretty nearly a generic crunchgen-handling makefile
@@ -39,6 +41,7 @@
 .else
 CANONICALOBJDIR:= /usr/obj${.CURDIR}
 .endif
+CRUNCH_GENERATE_LINKS?=	yes
 
 CLEANFILES+= $(CONF) *.o *.lo *.c *.mk *.cache *.a *.h
 
@@ -51,6 +54,7 @@
 .else
 $(OUTPUTS): $(.CURDIR)/../../$(D)/$(P)/Makefile
 .endif
+.if ${CRUNCH_GENERATE_LINKS} == "yes"
 .ifndef CRUNCH_SUPPRESS_LINK_${P}
 LINKS+= $(BINDIR)/$(PROG) $(BINDIR)/$(P)
 .endif
@@ -59,6 +63,7 @@
 LINKS+= $(BINDIR)/$(PROG) $(BINDIR)/$(A)
 .endif
 .endfor
+.endif
 .endfor
 .endfor
 
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/amd64/conf/GENERIC
--- a/head/sys/amd64/conf/GENERIC	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/amd64/conf/GENERIC	Fri Jan 13 15:51:01 2012 +0200
@@ -16,7 +16,7 @@
 # If you are in doubt as to the purpose or necessity of a line, check first
 # in NOTES.
 #
-# $FreeBSD: head/sys/amd64/conf/GENERIC 228973 2011-12-29 22:48:36Z rwatson $
+# $FreeBSD: head/sys/amd64/conf/GENERIC 229997 2012-01-12 00:34:33Z ken $
 
 cpu		HAMMER
 ident		GENERIC
@@ -131,7 +131,8 @@
 device		sa		# Sequential Access (tape etc)
 device		cd		# CD
 device		pass		# Passthrough device (direct ATA/SCSI access)
-device		ses		# SCSI Environmental Services (and SAF-TE)
+device		ses		# Enclosure Services (SES and SAF-TE)
+device		ctl		# CAM Target Layer
 
 # RAID controllers interfaced to the SCSI subsystem
 device		amr		# AMI MegaRAID
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/arm/conf/KB920X
--- a/head/sys/arm/conf/KB920X	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/arm/conf/KB920X	Fri Jan 13 15:51:01 2012 +0200
@@ -16,7 +16,7 @@
 # If you are in doubt as to the purpose or necessity of a line, check first 
 # in NOTES.
 #
-# $FreeBSD: head/sys/arm/conf/KB920X 225482 2011-09-11 17:39:51Z brueffer $
+# $FreeBSD: head/sys/arm/conf/KB920X 229639 2012-01-05 20:18:01Z adrian $
 
 ident		KB920X
 
@@ -138,3 +138,6 @@
 device		wlan_ccmp	# 802.11 CCMP support
 device		wlan_tkip	# 802.11 TKIP support
 device		wlan_amrr	# AMRR transmit rate control algorithm
+options		IEEE80211_SUPPORT_MESH
+
+options		AH_SUPPORT_AR5416
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/boot/common/loader.8
--- a/head/sys/boot/common/loader.8	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/boot/common/loader.8	Fri Jan 13 15:51:01 2012 +0200
@@ -22,9 +22,9 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.\" $FreeBSD: head/sys/boot/common/loader.8 228916 2011-12-27 15:59:51Z pluknet $
+.\" $FreeBSD: head/sys/boot/common/loader.8 229771 2012-01-07 11:16:23Z kib $
 .\"
-.Dd December 27, 2011
+.Dd January 7, 2012
 .Dt LOADER 8
 .Os
 .Sh NAME
@@ -419,6 +419,43 @@
 Changes to the
 .Va comconsole_speed
 variable take effect immediately.
+.It Va comconsole_port
+Defines the base i/o port used to access console UART
+(i386 and amd64 only).
+If the variable is not set, its assumed value is 0x3F8, which
+corresponds to PC port COM1, unless overriden by
+.Va BOOT_COMCONSOLE_PORT
+variable during the compilation of
+.Nm .
+Setting the
+.Va comconsole_port
+variable automatically set
+.Va hw.uart.console
+environment variable to provide a hint to kernel for location of the console.
+Loader console is changed immediately after variable
+.Va comconsole_port
+is set.
+.It Va comconsole_pcidev
+Defines the location of a PCI device of the 'simple communication'
+class to be used as the serial console UART (i386 and amd64 only).
+The syntax of the variable is
+.Li 'bus:device:function[:bar]' ,
+where all members must be numeric, with possible
+.Li 0x
+prefix to indicate a hexadecimal value.
+The
+.Va bar
+member is optional and assumed to be 0x10 if omitted.
+The bar must decode i/o space.
+Setting the variable
+.Va comconsole_pcidev
+automatically sets the variable
+.Va comconsole_port
+to the base of the selected bar, and hint
+.Va hw.uart.console .
+Loader console is changed immediately after variable
+.Va comconsole_pcidev
+is set.
 .It Va console
 Defines the current console or consoles.
 Multiple consoles may be specified.
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/boot/forth/loader.4th
--- a/head/sys/boot/forth/loader.4th	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/boot/forth/loader.4th	Fri Jan 13 15:51:01 2012 +0200
@@ -22,7 +22,7 @@
 \ OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 \ SUCH DAMAGE.
 \
-\ $FreeBSD: head/sys/boot/forth/loader.4th 228985 2011-12-30 06:24:59Z pluknet $
+\ $FreeBSD: head/sys/boot/forth/loader.4th 229881 2012-01-09 20:25:14Z pluknet $
 
 s" arch-i386" environment? [if] [if]
 	s" loader_version" environment?  [if]
@@ -44,9 +44,20 @@
 only forth also support-functions also builtins definitions
 
 : try-menu-unset
+  \ menu-unset may not be present
+  s" beastie_disable" getenv
+  dup -1 <> if
+    s" YES" compare-insensitive 0= if
+      exit
+    then
+  else
+    drop
+  then
   s" menu-unset"
-  ['] evaluate catch if
-    2drop
+  sfind if
+    execute
+  else
+    drop
   then
 ;
 
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/cam/cam_periph.c
--- a/head/sys/cam/cam_periph.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/cam/cam_periph.c	Fri Jan 13 15:51:01 2012 +0200
@@ -28,7 +28,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/cam/cam_periph.c 227293 2011-11-07 06:44:47Z ed $");
+__FBSDID("$FreeBSD: head/sys/cam/cam_periph.c 230000 2012-01-12 00:41:48Z ken $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -171,14 +171,16 @@
 			return (CAM_REQ_INPROG);
 		} else {
 			printf("cam_periph_alloc: attempt to re-allocate "
-			       "valid device %s%d rejected\n",
-			       periph->periph_name, periph->unit_number);
+			       "valid device %s%d rejected flags %#x "
+			       "refcount %d\n", periph->periph_name,
+			       periph->unit_number, periph->flags,
+			       periph->refcount);
 		}
 		return (CAM_REQ_INVALID);
 	}
 	
 	periph = (struct cam_periph *)malloc(sizeof(*periph), M_CAMPERIPH,
-					     M_NOWAIT);
+					     M_NOWAIT|M_ZERO);
 
 	if (periph == NULL)
 		return (CAM_RESRC_UNAVAIL);
@@ -190,7 +192,6 @@
 	path_id = xpt_path_path_id(path);
 	target_id = xpt_path_target_id(path);
 	lun_id = xpt_path_lun_id(path);
-	bzero(periph, sizeof(*periph));
 	cam_init_pinfo(&periph->pinfo);
 	periph->periph_start = periph_start;
 	periph->periph_dtor = periph_dtor;
@@ -305,17 +306,20 @@
 }
 
 /*
- * Find a peripheral structure with the specified path, target, lun, 
- * and (optionally) type.  If the name is NULL, this function will return
- * the first peripheral driver that matches the specified path.
+ * Find peripheral driver instances attached to the specified path.
  */
 int
 cam_periph_list(struct cam_path *path, struct sbuf *sb)
 {
+	struct sbuf local_sb;
 	struct periph_driver **p_drv;
 	struct cam_periph *periph;
 	int count;
+	int sbuf_alloc_len;
 
+	sbuf_alloc_len = 16;
+retry:
+	sbuf_new(&local_sb, NULL, sbuf_alloc_len, SBUF_FIXEDLEN);
 	count = 0;
 	xpt_lock_buses();
 	for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
@@ -324,30 +328,60 @@
 			if (xpt_path_comp(periph->path, path) != 0)
 				continue;
 
-			if (sbuf_len(sb) != 0)
-				sbuf_cat(sb, ",");
+			if (sbuf_len(&local_sb) != 0)
+				sbuf_cat(&local_sb, ",");
 
-			sbuf_printf(sb, "%s%d", periph->periph_name,
+			sbuf_printf(&local_sb, "%s%d", periph->periph_name,
 				    periph->unit_number);
+
+			if (sbuf_error(&local_sb) == ENOMEM) {
+				sbuf_alloc_len *= 2;
+				xpt_unlock_buses();
+				sbuf_delete(&local_sb);
+				goto retry;
+			}
 			count++;
 		}
 	}
 	xpt_unlock_buses();
+	sbuf_finish(&local_sb);
+	sbuf_cpy(sb, sbuf_data(&local_sb));
+	sbuf_delete(&local_sb);
 	return (count);
 }
 
 cam_status
 cam_periph_acquire(struct cam_periph *periph)
 {
+	cam_status status;
 
+	status = CAM_REQ_CMP_ERR;
 	if (periph == NULL)
-		return(CAM_REQ_CMP_ERR);
+		return (status);
 
 	xpt_lock_buses();
-	periph->refcount++;
+	if ((periph->flags & CAM_PERIPH_INVALID) == 0) {
+		periph->refcount++;
+		status = CAM_REQ_CMP;
+	}
 	xpt_unlock_buses();
 
-	return(CAM_REQ_CMP);
+	return (status);
+}
+
+void
+cam_periph_release_locked_buses(struct cam_periph *periph)
+{
+	if (periph->refcount != 0) {
+		periph->refcount--;
+	} else {
+		panic("%s: release of %p when refcount is zero\n ", __func__,
+		      periph);
+	}
+	if (periph->refcount == 0
+	    && (periph->flags & CAM_PERIPH_INVALID)) {
+		camperiphfree(periph);
+	}
 }
 
 void
@@ -358,15 +392,7 @@
 		return;
 
 	xpt_lock_buses();
-	if (periph->refcount != 0) {
-		periph->refcount--;
-	} else {
-		xpt_print(periph->path, "%s: release %p when refcount is zero\n ", __func__, periph);
-	}
-	if (periph->refcount == 0
-	    && (periph->flags & CAM_PERIPH_INVALID)) {
-		camperiphfree(periph);
-	}
+	cam_periph_release_locked_buses(periph);
 	xpt_unlock_buses();
 }
 
@@ -1812,9 +1838,6 @@
 		error = EIO;
 		break;
 	case CAM_SEL_TIMEOUT:
-	{
-		struct cam_path *newpath;
-
 		if ((camflags & CAM_RETRY_SELTO) != 0) {
 			if (ccb->ccb_h.retry_count > 0 &&
 			    (periph->flags & CAM_PERIPH_INVALID) == 0) {
@@ -1837,6 +1860,11 @@
 			}
 			action_string = "Retries exhausted";
 		}
+		/* FALLTHROUGH */
+	case CAM_DEV_NOT_THERE:
+	{
+		struct cam_path *newpath;
+
 		error = ENXIO;
 		/* Should we do more if we can't create the path?? */
 		if (xpt_create_path(&newpath, periph,
@@ -1855,7 +1883,6 @@
 	}
 	case CAM_REQ_INVALID:
 	case CAM_PATH_INVALID:
-	case CAM_DEV_NOT_THERE:
 	case CAM_NO_HBA:
 	case CAM_PROVIDE_FAIL:
 	case CAM_REQ_TOO_BIG:
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/cam/cam_periph.h
--- a/head/sys/cam/cam_periph.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/cam/cam_periph.h	Fri Jan 13 15:51:01 2012 +0200
@@ -25,7 +25,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $FreeBSD: head/sys/cam/cam_periph.h 223081 2011-06-14 14:53:17Z gibbs $
+ * $FreeBSD: head/sys/cam/cam_periph.h 230000 2012-01-12 00:41:48Z ken $
  */
 
 #ifndef _CAM_CAM_PERIPH_H
@@ -119,6 +119,7 @@
 #define CAM_PERIPH_NEW_DEV_FOUND	0x10
 #define CAM_PERIPH_RECOVERY_INPROG	0x20
 #define CAM_PERIPH_SENSE_INPROG		0x40
+#define CAM_PERIPH_FREE			0x80
 	u_int32_t		 immediate_priority;
 	u_int32_t		 refcount;
 	SLIST_HEAD(, ccb_hdr)	 ccb_list;	/* For "immediate" requests */
@@ -146,6 +147,7 @@
 cam_status	cam_periph_acquire(struct cam_periph *periph);
 void		cam_periph_release(struct cam_periph *periph);
 void		cam_periph_release_locked(struct cam_periph *periph);
+void		cam_periph_release_locked_buses(struct cam_periph *periph);
 int		cam_periph_hold(struct cam_periph *periph, int priority);
 void		cam_periph_unhold(struct cam_periph *periph);
 void		cam_periph_invalidate(struct cam_periph *periph);
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/cam/cam_xpt.c
--- a/head/sys/cam/cam_xpt.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/cam/cam_xpt.c	Fri Jan 13 15:51:01 2012 +0200
@@ -28,7 +28,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/cam/cam_xpt.c 228027 2011-11-27 16:22:19Z marius $");
+__FBSDID("$FreeBSD: head/sys/cam/cam_xpt.c 230000 2012-01-12 00:41:48Z ken $");
 
 #include <sys/param.h>
 #include <sys/bus.h>
@@ -2026,12 +2026,24 @@
 	for (bus = (start_bus ? start_bus : TAILQ_FIRST(&xsoftc.xpt_busses));
 	     bus != NULL;
 	     bus = next_bus) {
-		next_bus = TAILQ_NEXT(bus, links);
-
+
+		bus->refcount++;
+
+		/*
+		 * XXX The locking here is obviously very complex.  We
+		 * should work to simplify it.
+		 */
 		mtx_unlock(&xsoftc.xpt_topo_lock);
 		CAM_SIM_LOCK(bus->sim);
 		retval = tr_func(bus, arg);
 		CAM_SIM_UNLOCK(bus->sim);
+
+		mtx_lock(&xsoftc.xpt_topo_lock);
+		next_bus = TAILQ_NEXT(bus, links);
+		mtx_unlock(&xsoftc.xpt_topo_lock);
+
+		xpt_release_bus(bus);
+
 		if (retval == 0)
 			return(retval);
 		mtx_lock(&xsoftc.xpt_topo_lock);
@@ -2086,9 +2098,13 @@
 		       TAILQ_FIRST(&bus->et_entries));
 	     target != NULL; target = next_target) {
 
+		target->refcount++;
+
+		retval = tr_func(target, arg);
+
 		next_target = TAILQ_NEXT(target, links);
 
-		retval = tr_func(target, arg);
+		xpt_release_target(target);
 
 		if (retval == 0)
 			return(retval);
@@ -2110,9 +2126,21 @@
 	     device != NULL;
 	     device = next_device) {
 
+		/*
+		 * Hold a reference so the current device does not go away
+		 * on us.
+		 */
+		device->refcount++;
+
+		retval = tr_func(device, arg);
+
+		/*
+		 * Grab our next pointer before we release the current
+		 * device.
+		 */
 		next_device = TAILQ_NEXT(device, links);
 
-		retval = tr_func(device, arg);
+		xpt_release_device(device);
 
 		if (retval == 0)
 			return(retval);
@@ -2130,18 +2158,57 @@
 
 	retval = 1;
 
+	xpt_lock_buses();
 	for (periph = (start_periph ? start_periph :
 		       SLIST_FIRST(&device->periphs));
 	     periph != NULL;
 	     periph = next_periph) {
 
+
+		/*
+		 * In this case, we want to show peripherals that have been
+		 * invalidated, but not peripherals that are scheduled to
+		 * be freed.  So instead of calling cam_periph_acquire(),
+		 * which will fail if the periph has been invalidated, we
+		 * just check for the free flag here.  If it is free, we
+		 * skip to the next periph.
+		 */
+		if (periph->flags & CAM_PERIPH_FREE) {
+			next_periph = SLIST_NEXT(periph, periph_links);
+			continue;
+		}
+
+		/*
+		 * Acquire a reference to this periph while we call the
+		 * traversal function, so it can't go away.
+		 */
+		periph->refcount++;
+
+		xpt_unlock_buses();
+
+		retval = tr_func(periph, arg);
+
+		/*
+		 * We need the lock for list traversal.
+		 */
+		xpt_lock_buses();
+
+		/*
+		 * Grab the next peripheral before we release this one, so
+		 * our next pointer is still valid.
+		 */
 		next_periph = SLIST_NEXT(periph, periph_links);
 
-		retval = tr_func(periph, arg);
+		cam_periph_release_locked_buses(periph);
+
 		if (retval == 0)
-			return(retval);
+			goto bailout_done;
 	}
 
+bailout_done:
+
+	xpt_unlock_buses();
+
 	return(retval);
 }
 
@@ -2188,15 +2255,48 @@
 	     TAILQ_FIRST(&(*pdrv)->units)); periph != NULL;
 	     periph = next_periph) {
 
+
+		/*
+		 * In this case, we want to show peripherals that have been
+		 * invalidated, but not peripherals that are scheduled to
+		 * be freed.  So instead of calling cam_periph_acquire(),
+		 * which will fail if the periph has been invalidated, we
+		 * just check for the free flag here.  If it is free, we
+		 * skip to the next periph.
+		 */
+		if (periph->flags & CAM_PERIPH_FREE) {
+			next_periph = TAILQ_NEXT(periph, unit_links);
+			continue;
+		}
+
+		/*
+		 * Acquire a reference to this periph while we call the
+		 * traversal function, so it can't go away.
+		 */
+		periph->refcount++;
+
+		/*
+		 * XXX KDM we have the toplogy lock here, but in
+		 * xptperiphtraverse(), we drop it before calling the
+		 * traversal function.  Which is correct?
+		 */
+		retval = tr_func(periph, arg);
+
+		/*
+		 * Grab the next peripheral before we release this one, so
+		 * our next pointer is still valid.
+		 */
 		next_periph = TAILQ_NEXT(periph, unit_links);
 
-		retval = tr_func(periph, arg);
-		if (retval == 0) {
-			xpt_unlock_buses();
-			return(retval);
-		}
+		cam_periph_release_locked_buses(periph);
+
+		if (retval == 0)
+			goto bailout_done;
 	}
+bailout_done:
+
 	xpt_unlock_buses();
+
 	return(retval);
 }
 
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/cam/ctl/README.ctl.txt
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/sys/cam/ctl/README.ctl.txt	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,449 @@
+/* $FreeBSD: head/sys/cam/ctl/README.ctl.txt 229997 2012-01-12 00:34:33Z ken $ */
+
+CTL - CAM Target Layer Description
+
+Revision 1.4 (December 29th, 2011)
+Ken Merry <ken at FreeBSD.org>
+
+Table of Contents:
+=================
+
+Introduction
+Features
+Configuring and Running CTL
+Revision 1.N Changes
+To Do List
+Code Roadmap
+Userland Commands
+
+Introduction:
+============
+
+CTL is a disk and processor device emulation subsystem originally written
+for Copan Systems under Linux starting in 2003.  It has been shipping in
+Copan (now SGI) products since 2005.
+
+It was ported to FreeBSD in 2008, and thanks to an agreement between SGI
+(who acquired Copan's assets in 2010) and Spectra Logic in 2010, CTL is
+available under a BSD-style license.  The intent behind the agreement was
+that Spectra would work to get CTL into the FreeBSD tree.
+
+Features:
+========
+
+ - Disk and processor device emulation.
+ - Tagged queueing
+ - SCSI task attribute support (ordered, head of queue, simple tags)
+ - SCSI implicit command ordering support.  (e.g. if a read follows a mode
+   select, the read will be blocked until the mode select completes.)
+ - Full task management support (abort, LUN reset, target reset, etc.)
+ - Support for multiple ports
+ - Support for multiple simultaneous initiators
+ - Support for multiple simultaneous backing stores
+ - Persistent reservation support
+ - Mode sense/select support
+ - Error injection support
+ - High Availability support (1)
+ - All I/O handled in-kernel, no userland context switch overhead.
+
+(1) HA Support is just an API stub, and needs much more to be fully
+    functional.  See the to-do list below.
+
+Configuring and Running CTL:
+===========================
+
+ - After applying the CTL patchset to your tree, build world and install it
+   on your target system.
+
+ - Add 'device ctl' to your kernel configuration file.
+
+ - If you're running with a 8Gb or 4Gb Qlogic FC board, add
+   'options ISP_TARGET_MODE' to your kernel config file.  Keep in mind that
+   the isp(4) driver can run in target or initiator mode, but not both on
+   the same machine.  'device ispfw' or loading the ispfw module is also
+   recommended.
+
+ - Rebuild and install a new kernel.
+
+ - Reboot with the new kernel.
+
+ - To add a LUN with the RAM disk backend:
+
+	ctladm create -b ramdisk -s 10485760000000000000
+	ctladm port -o on
+
+ - You should now see the CTL disk LUN through camcontrol devlist:
+
+scbus6 on ctl2cam0 bus 0:
+<FREEBSD CTLDISK 0001>             at scbus6 target 1 lun 0 (da24,pass32)
+<>                                 at scbus6 target -1 lun -1 ()
+
+   This is visible through the CTL CAM SIM.  This allows using CTL without
+   any physical hardware.  You should be able to issue any normal SCSI
+   commands to the device via the pass(4)/da(4) devices.
+
+   If any target-capable HBAs are in the system (e.g. isp(4)), and have
+   target mode enabled, you should now also be able to see the CTL LUNs via
+   that target interface.
+
+   Note that all CTL LUNs are presented to all frontends.  There is no
+   LUN masking, or separate, per-port configuration.
+
+ - Note that the ramdisk backend is a "fake" ramdisk.  That is, it is
+   backed by a small amount of RAM that is used for all I/O requests.  This
+   is useful for performance testing, but not for any data integrity tests.
+
+ - To add a LUN with the block/file backend:
+
+	truncate -s +1T myfile
+	ctladm create -b block -o file=myfile
+	ctladm port -o on
+
+ - You can also see a list of LUNs and their backends like this:
+
+# ctladm devlist
+LUN Backend       Size (Blocks)   BS Serial Number    Device ID       
+  0 block            2147483648  512 MYSERIAL   0     MYDEVID   0     
+  1 block            2147483648  512 MYSERIAL   1     MYDEVID   1     
+  2 block            2147483648  512 MYSERIAL   2     MYDEVID   2     
+  3 block            2147483648  512 MYSERIAL   3     MYDEVID   3     
+  4 block            2147483648  512 MYSERIAL   4     MYDEVID   4     
+  5 block            2147483648  512 MYSERIAL   5     MYDEVID   5     
+  6 block            2147483648  512 MYSERIAL   6     MYDEVID   6     
+  7 block            2147483648  512 MYSERIAL   7     MYDEVID   7     
+  8 block            2147483648  512 MYSERIAL   8     MYDEVID   8     
+  9 block            2147483648  512 MYSERIAL   9     MYDEVID   9     
+ 10 block            2147483648  512 MYSERIAL  10     MYDEVID  10     
+ 11 block            2147483648  512 MYSERIAL  11     MYDEVID  11    
+
+ - You can see the LUN type and backing store for block/file backend LUNs
+   like this:
+
+# ctladm devlist -v
+LUN Backend       Size (Blocks)   BS Serial Number    Device ID       
+  0 block            2147483648  512 MYSERIAL   0     MYDEVID   0     
+      lun_type=0
+      num_threads=14
+      file=testdisk0
+  1 block            2147483648  512 MYSERIAL   1     MYDEVID   1     
+      lun_type=0
+      num_threads=14
+      file=testdisk1
+  2 block            2147483648  512 MYSERIAL   2     MYDEVID   2     
+      lun_type=0
+      num_threads=14
+      file=testdisk2
+  3 block            2147483648  512 MYSERIAL   3     MYDEVID   3     
+      lun_type=0
+      num_threads=14
+      file=testdisk3
+  4 block            2147483648  512 MYSERIAL   4     MYDEVID   4     
+      lun_type=0
+      num_threads=14
+      file=testdisk4
+  5 block            2147483648  512 MYSERIAL   5     MYDEVID   5     
+      lun_type=0
+      num_threads=14
+      file=testdisk5
+  6 block            2147483648  512 MYSERIAL   6     MYDEVID   6     
+      lun_type=0
+      num_threads=14
+      file=testdisk6
+  7 block            2147483648  512 MYSERIAL   7     MYDEVID   7     
+      lun_type=0
+      num_threads=14
+      file=testdisk7
+  8 block            2147483648  512 MYSERIAL   8     MYDEVID   8     
+      lun_type=0
+      num_threads=14
+      file=testdisk8
+  9 block            2147483648  512 MYSERIAL   9     MYDEVID   9     
+      lun_type=0
+      num_threads=14
+      file=testdisk9
+ 10 ramdisk                   0    0 MYSERIAL   0     MYDEVID   0     
+      lun_type=3
+ 11 ramdisk     204800000000000  512 MYSERIAL   1     MYDEVID   1     
+      lun_type=0
+
+
+Revision 1.4 Changes
+====================
+ - Added in the second HA mode (where CTL does the data transfers instead
+   of having data transfers done below CTL), and abstracted out the Copan
+   HA API.
+
+ - Fixed the phantom device problem in the CTL CAM SIM and improved the
+   CAM SIM to automatically trigger a rescan when the port is enabled and
+   disabled.
+ 
+ - Made the number of threads in the block backend configurable via sysctl,
+   loader tunable and the ctladm command line.  (You can now specify
+   -o num_threads=4 when creating a LUN with ctladm create.)
+
+ - Fixed some LUN selection issues in ctlstat(8) and allowed for selection
+   of LUN numbers up to 1023.
+
+ - General cleanup.
+
+ - This version intended for public release.
+
+Revision 1.3 Changes
+====================
+ - Added descriptor sense support to CTL.  It can be enabled through the
+   control mode page (10), but is disabled by default.
+
+ - Improved error injection support.  The number of errors that can be
+   injected with 'ctladm inject' has been increased, and any arbitrary
+   sense data may now be injected as well.
+
+ - The port infrastructure has been revamped.  Individual ports and types
+   of ports may now be enabled and disabled from the command line.  ctladm
+   now has the ability to set the WWNN and WWPN for each port.
+
+ - The block backend can now send multiple I/Os to backing files.  Multiple
+   writes are only allowed for ZFS, but multiple readers are allowed for
+   any filesystem.
+
+ - The block and ramdisk backends now support setting the LUN blocksize.
+   There are some restrictions when the backing device is a block device,
+   but otherwise the blocksize may be set to anything.
+
+Revision 1.2 Changes
+====================
+
+ - CTL initialization process has been revamped.  Instead of using an
+   ad-hoc method, it is now sequenced through SYSINIT() calls.
+
+ - A block/file backend has been added.  This allows using arbitrary files
+   or block devices as a backing store.
+
+ - The userland LUN configuration interface has been completely rewritten.
+   Configuration is now done out of band.
+
+ - The ctladm(8) command line interface has been revamped, and is now
+   similar to camcontrol(8).
+
+To Do List:
+==========
+
+ - Make CTL buildable as a module.  Work needs to be done on initialization,
+   and on freeing resources and LUNs when it is built as a module.
+
+ - Use devstat(9) for CTL's statistics collection.  CTL uses a home-grown
+   statistics collection system that is similar to devstat(9).  ctlstat
+   should be retired in favor of iostat, etc., once aggregation modes are
+   available in iostat to match the behavior of ctlstat -t and dump modes
+   are available to match the behavior of ctlstat -d/ctlstat -J.
+
+ - ZFS ARC backend for CTL.  Since ZFS copies all I/O into the ARC
+   (Adaptive Replacement Cache), running the block/file backend on top of a
+   ZFS-backed zdev or file will involve an extra set of copies.  The
+   optimal solution for backing targets served by CTL with ZFS would be to
+   allocate buffers out of the ARC directly, and DMA to/from them directly.
+   That would eliminate an extra data buffer allocation and copy.
+
+ - Switch CTL over to using CAM CCBs instead of its own union ctl_io.  This
+   will likely require a significant amount of work, but will eliminate
+   another data structure in the stack, more memory allocations, etc.  This
+   will also require changes to the CAM CCB structure to support CTL.
+
+ - Full-featured High Availability support.  The HA API that is in ctl_ha.h
+   is essentially a renamed version of Copan's HA API.  There is no
+   substance to it, but it remains in CTL to show what needs to be done to
+   implement active/active HA from a CTL standpoint.  The things that would
+   need to be done include:
+	- A kernel level software API for message passing as well as DMA
+	  between at least two nodes.
+	- Hardware support and drivers for inter-node communication.  This
+	  could be as simples as ethernet hardware and drivers.
+	- A "supervisor", or startup framework to control and coordinate
+	  HA startup, failover (going from active/active to single mode),
+	  and failback (going from single mode to active/active).
+	- HA support in other components of the stack.  The goal behind HA
+	  is that one node can fail and another node can seamlessly take
+	  over handling I/O requests.  This requires support from pretty
+	  much every component in the storage stack, from top to bottom.
+	  CTL is one piece of it, but you also need support in the RAID
+	  stack/filesystem/backing store.  You also need full configuration
+	  mirroring, and all peer nodes need to be able to talk to the
+	  underlying storage hardware.
+
+Code Roadmap:
+============
+
+CTL has the concept of pluggable frontend ports and backends.  All
+frontends and backends can be active at the same time.  You can have a
+ramdisk-backed LUN present along side a file backed LUN.
+
+ctl.c:
+-----
+
+This is the core of CTL, where all of the command handlers and a lot of
+other things live.  Yes, it is large.  It started off small and grew to its
+current size over time.  Perhaps it can be split into more files at some
+point.
+
+Here is a roadmap of some of the primary functions in ctl.c.  Starting here
+and following the various leaf functions will show the command flow.
+
+ctl_queue() 		This is where commands from the frontend ports come
+			in.
+
+ctl_queue_sense()	This is only used for non-packetized SCSI.  i.e.
+			parallel SCSI prior to U320 and perhaps U160.
+
+ctl_work_thread() 	This is the primary work thread, and everything gets
+			executed from there.
+
+ctl_scsiio_precheck() 	This where all of the initial checks are done, and I/O
+			is either queued for execution or blocked.
+
+ctl_scsiio() 		This is where the command handler is actually
+			executed.  (See ctl_cmd_table.c for the mapping of
+			SCSI opcode to command handler function.)
+
+ctl_done()		This is the routine called (or ctl_done_lock()) to
+			initiate the command completion process.
+
+ctl_process_done()	This is where command completion actually happens.
+
+ctl.h:
+-----
+
+Basic function declarations and data structures.
+
+ctl_backend.c,
+ctl_backend.h:
+-------------
+
+These files define the basic CTL backend API.  The comments in the header
+explain the API.
+
+ctl_backend_block.c
+ctl_backend_block.h:
+-------------------
+
+The block and file backend.  This allows for using a disk or a file as the
+backing store for a LUN.  Multiple threads are started to do I/O to the
+backing device, primarily because the VFS API requires that to get any
+concurrency.
+
+ctl_backend_ramdisk.c:
+---------------------
+
+A "fake" ramdisk backend.  It only allocates a small amount of memory to
+act as a source and sink for reads and writes from an initiator.  Therefore
+it cannot be used for any real data, but it can be used to test for
+throughput.  It can also be used to test initiators' support for extremely
+large LUNs.
+
+ctl_cmd_table.c:
+---------------
+
+This is a table with all 256 possible SCSI opcodes, and command handler
+functions defined for supported opcodes.  It is included in ctl.c.
+
+ctl_debug.h:
+-----------
+
+Simplistic debugging support.
+
+ctl_error.c,
+ctl_error.h:
+-----------
+
+CTL-specific wrappers around the CAM sense building functions.
+
+ctl_frontend.c,
+ctl_frontend.h:
+--------------
+
+These files define the basic CTL frontend port API.  The comments in the
+header explain the API.
+
+ctl_frontend_cam_sim.c:
+----------------------
+
+This is a CTL frontend port that is also a CAM SIM.  The idea is that this
+frontend allows for using CTL without any target-capable hardware.  So any
+LUNs you create in CTL are visible via this port.
+
+
+ctl_frontend_internal.c
+ctl_frontend_internal.h:
+-----------------------
+
+This is a frontend port written for Copan to do some system-specific tasks
+that required sending commands into CTL from inside the kernel.  This isn't
+entirely relevant to FreeBSD in general, but can perhaps be repurposed or
+removed later.
+
+ctl_ha.h:
+--------
+
+This is a stubbed-out High Availability API.  See the comments in the
+header and the description of what is needed as far as HA support above.
+
+ctl_io.h:
+--------
+
+This defines most of the core CTL I/O structures.  union ctl_io is
+conceptually very similar to CAM's union ccb.  
+
+ctl_ioctl.h:
+-----------
+
+This defines all ioctls available through the CTL character device, and
+the data structures needed for those ioctls.
+
+ctl_mem_pool.c
+ctl_mem_pool.h:
+--------------
+
+Generic memory pool implementation.  This is currently only used by the
+internal frontend.  The internal frontend can probably be rewritten to use
+UMA zones and this can be removed.
+
+ctl_private.h:
+-------------
+
+Private data structres (e.g. CTL softc) and function prototypes.  This also
+includes the SCSI vendor and product names used by CTL.
+
+ctl_scsi_all.c
+ctl_scsi_all.h:
+--------------
+
+CTL wrappers around CAM sense printing functions.
+
+ctl_ser_table.c:
+---------------
+
+Command serialization table.  This defines what happens when one type of
+command is followed by another type of command.  e.g., what do you do when
+you have a mode select followed by a write?  You block the write until the
+mode select is complete.  That is defined in this table.
+
+ctl_util.c
+ctl_util.h:
+----------
+
+CTL utility functions, primarily designed to be used from userland.  See
+ctladm for the primary consumer of these functions.  These include CDB
+building functions.
+
+scsi_ctl.c:
+----------
+
+CAM target peripheral driver and CTL frontend port.  This is the path into
+CTL for commands from target-capable hardware/SIMs.
+
+Userland Commands:
+=================
+
+ctladm(8) fills a role similar to camcontrol(8).  It allow configuring LUNs,
+issuing commands, injecting errors and various other control functions.
+
+ctlstat(8) fills a role similar to iostat(8).  It reports I/O statistics
+for CTL.
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/cam/ctl/ctl.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/sys/cam/ctl/ctl.c	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,13082 @@
+/*-
+ * Copyright (c) 2003-2009 Silicon Graphics International Corp.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions, and the following disclaimer,
+ *    without modification.
+ * 2. Redistributions in binary form must reproduce at minimum a disclaimer
+ *    substantially similar to the "NO WARRANTY" disclaimer below
+ *    ("Disclaimer") and any redistribution must be conditioned upon
+ *    including a substantially similar Disclaimer requirement for further
+ *    binary redistribution.
+ *
+ * NO WARRANTY
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGES.
+ *
+ * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl.c#8 $
+ */
+/*
+ * CAM Target Layer, a SCSI device emulation subsystem.
+ *
+ * Author: Ken Merry <ken at FreeBSD.org>
+ */
+
+#define _CTL_C
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD: head/sys/cam/ctl/ctl.c 229997 2012-01-12 00:34:33Z ken $");
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/kernel.h>
+#include <sys/types.h>
+#include <sys/kthread.h>
+#include <sys/bio.h>
+#include <sys/fcntl.h>
+#include <sys/lock.h>
+#include <sys/mutex.h>
+#include <sys/condvar.h>
+#include <sys/malloc.h>
+#include <sys/conf.h>
+#include <sys/ioccom.h>
+#include <sys/queue.h>
+#include <sys/sbuf.h>
+#include <sys/endian.h>
+#include <sys/sysctl.h>
+
+#include <cam/cam.h>
+#include <cam/scsi/scsi_all.h>
+#include <cam/scsi/scsi_da.h>
+#include <cam/ctl/ctl_io.h>
+#include <cam/ctl/ctl.h>
+#include <cam/ctl/ctl_frontend.h>
+#include <cam/ctl/ctl_frontend_internal.h>
+#include <cam/ctl/ctl_util.h>
+#include <cam/ctl/ctl_backend.h>
+#include <cam/ctl/ctl_ioctl.h>
+#include <cam/ctl/ctl_ha.h>
+#include <cam/ctl/ctl_private.h>
+#include <cam/ctl/ctl_debug.h>
+#include <cam/ctl/ctl_scsi_all.h>
+#include <cam/ctl/ctl_error.h>
+
+struct ctl_softc *control_softc = NULL;
+
+/*
+ * The default is to run with CTL_DONE_THREAD turned on.  Completed
+ * transactions are queued for processing by the CTL work thread.  When
+ * CTL_DONE_THREAD is not defined, completed transactions are processed in
+ * the caller's context.
+ */
+#define CTL_DONE_THREAD
+
+/*
+ *  * Use the serial number and device ID provided by the backend, rather than
+ *   * making up our own.
+ *    */
+#define CTL_USE_BACKEND_SN
+
+/*
+ * Size and alignment macros needed for Copan-specific HA hardware.  These
+ * can go away when the HA code is re-written, and uses busdma for any
+ * hardware.
+ */
+#define	CTL_ALIGN_8B(target, source, type)				\
+	if (((uint32_t)source & 0x7) != 0)				\
+		target = (type)(source + (0x8 - ((uint32_t)source & 0x7)));\
+	else								\
+		target = (type)source;
+
+#define	CTL_SIZE_8B(target, size)					\
+	if ((size & 0x7) != 0)						\
+		target = size + (0x8 - (size & 0x7));			\
+	else								\
+		target = size;
+
+#define CTL_ALIGN_8B_MARGIN	16
+
+/*
+ * Template mode pages.
+ */
+
+/*
+ * Note that these are default values only.  The actual values will be
+ * filled in when the user does a mode sense.
+ */
+static struct copan_power_subpage power_page_default = {
+	/*page_code*/ PWR_PAGE_CODE | SMPH_SPF,
+	/*subpage*/ PWR_SUBPAGE_CODE,
+	/*page_length*/ {(sizeof(struct copan_power_subpage) - 4) & 0xff00,
+			 (sizeof(struct copan_power_subpage) - 4) & 0x00ff},
+	/*page_version*/ PWR_VERSION,
+	/* total_luns */ 26,
+	/* max_active_luns*/ PWR_DFLT_MAX_LUNS,
+	/*reserved*/ {0, 0, 0, 0, 0, 0, 0, 0, 0,
+		      0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+		      0, 0, 0, 0, 0, 0}
+};
+
+static struct copan_power_subpage power_page_changeable = {
+	/*page_code*/ PWR_PAGE_CODE | SMPH_SPF,
+	/*subpage*/ PWR_SUBPAGE_CODE,
+	/*page_length*/ {(sizeof(struct copan_power_subpage) - 4) & 0xff00,
+			 (sizeof(struct copan_power_subpage) - 4) & 0x00ff},
+	/*page_version*/ 0,
+	/* total_luns */ 0,
+	/* max_active_luns*/ 0,
+	/*reserved*/ {0, 0, 0, 0, 0, 0, 0, 0, 0,
+		      0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+		      0, 0, 0, 0, 0, 0}
+};
+
+static struct copan_aps_subpage aps_page_default = {
+	APS_PAGE_CODE | SMPH_SPF, //page_code
+	APS_SUBPAGE_CODE, //subpage
+	{(sizeof(struct copan_aps_subpage) - 4) & 0xff00,
+	 (sizeof(struct copan_aps_subpage) - 4) & 0x00ff}, //page_length
+	APS_VERSION, //page_version
+	0, //lock_active
+	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+	0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+	0, 0, 0, 0, 0} //reserved
+};
+
+static struct copan_aps_subpage aps_page_changeable = {
+	APS_PAGE_CODE | SMPH_SPF, //page_code
+	APS_SUBPAGE_CODE, //subpage
+	{(sizeof(struct copan_aps_subpage) - 4) & 0xff00,
+	 (sizeof(struct copan_aps_subpage) - 4) & 0x00ff}, //page_length
+	0, //page_version
+	0, //lock_active
+	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+	0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+	0, 0, 0, 0, 0} //reserved
+};
+
+static struct copan_debugconf_subpage debugconf_page_default = {
+	DBGCNF_PAGE_CODE | SMPH_SPF,	/* page_code */
+	DBGCNF_SUBPAGE_CODE,		/* subpage */
+	{(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
+	 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
+	DBGCNF_VERSION,			/* page_version */
+	{CTL_TIME_IO_DEFAULT_SECS>>8,
+	 CTL_TIME_IO_DEFAULT_SECS>>0},	/* ctl_time_io_secs */
+};
+
+static struct copan_debugconf_subpage debugconf_page_changeable = {
+	DBGCNF_PAGE_CODE | SMPH_SPF,	/* page_code */
+	DBGCNF_SUBPAGE_CODE,		/* subpage */
+	{(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
+	 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
+	0,				/* page_version */
+	{0xff,0xff},			/* ctl_time_io_secs */
+};
+
+static struct scsi_format_page format_page_default = {
+	/*page_code*/SMS_FORMAT_DEVICE_PAGE,
+	/*page_length*/sizeof(struct scsi_format_page) - 2,
+	/*tracks_per_zone*/ {0, 0},
+	/*alt_sectors_per_zone*/ {0, 0},
+	/*alt_tracks_per_zone*/ {0, 0},
+	/*alt_tracks_per_lun*/ {0, 0},
+	/*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff,
+			        CTL_DEFAULT_SECTORS_PER_TRACK & 0xff},
+	/*bytes_per_sector*/ {0, 0},
+	/*interleave*/ {0, 0},
+	/*track_skew*/ {0, 0},
+	/*cylinder_skew*/ {0, 0},
+	/*flags*/ SFP_HSEC,
+	/*reserved*/ {0, 0, 0}
+};
+
+static struct scsi_format_page format_page_changeable = {
+	/*page_code*/SMS_FORMAT_DEVICE_PAGE,
+	/*page_length*/sizeof(struct scsi_format_page) - 2,
+	/*tracks_per_zone*/ {0, 0},
+	/*alt_sectors_per_zone*/ {0, 0},
+	/*alt_tracks_per_zone*/ {0, 0},
+	/*alt_tracks_per_lun*/ {0, 0},
+	/*sectors_per_track*/ {0, 0},
+	/*bytes_per_sector*/ {0, 0},
+	/*interleave*/ {0, 0},
+	/*track_skew*/ {0, 0},
+	/*cylinder_skew*/ {0, 0},
+	/*flags*/ 0,
+	/*reserved*/ {0, 0, 0}
+};
+
+static struct scsi_rigid_disk_page rigid_disk_page_default = {
+	/*page_code*/SMS_RIGID_DISK_PAGE,
+	/*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
+	/*cylinders*/ {0, 0, 0},
+	/*heads*/ CTL_DEFAULT_HEADS,
+	/*start_write_precomp*/ {0, 0, 0},
+	/*start_reduced_current*/ {0, 0, 0},
+	/*step_rate*/ {0, 0},
+	/*landing_zone_cylinder*/ {0, 0, 0},
+	/*rpl*/ SRDP_RPL_DISABLED,
+	/*rotational_offset*/ 0,
+	/*reserved1*/ 0,
+	/*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff,
+			   CTL_DEFAULT_ROTATION_RATE & 0xff},
+	/*reserved2*/ {0, 0}
+};
+
+static struct scsi_rigid_disk_page rigid_disk_page_changeable = {
+	/*page_code*/SMS_RIGID_DISK_PAGE,
+	/*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
+	/*cylinders*/ {0, 0, 0},
+	/*heads*/ 0,
+	/*start_write_precomp*/ {0, 0, 0},
+	/*start_reduced_current*/ {0, 0, 0},
+	/*step_rate*/ {0, 0},
+	/*landing_zone_cylinder*/ {0, 0, 0},
+	/*rpl*/ 0,
+	/*rotational_offset*/ 0,
+	/*reserved1*/ 0,
+	/*rotation_rate*/ {0, 0},
+	/*reserved2*/ {0, 0}
+};
+
+static struct scsi_caching_page caching_page_default = {
+	/*page_code*/SMS_CACHING_PAGE,
+	/*page_length*/sizeof(struct scsi_caching_page) - 2,
+	/*flags1*/ SCP_DISC | SCP_WCE,
+	/*ret_priority*/ 0,
+	/*disable_pf_transfer_len*/ {0xff, 0xff},
+	/*min_prefetch*/ {0, 0},
+	/*max_prefetch*/ {0xff, 0xff},
+	/*max_pf_ceiling*/ {0xff, 0xff},
+	/*flags2*/ 0,
+	/*cache_segments*/ 0,
+	/*cache_seg_size*/ {0, 0},
+	/*reserved*/ 0,
+	/*non_cache_seg_size*/ {0, 0, 0}
+};
+
+static struct scsi_caching_page caching_page_changeable = {
+	/*page_code*/SMS_CACHING_PAGE,
+	/*page_length*/sizeof(struct scsi_caching_page) - 2,
+	/*flags1*/ 0,
+	/*ret_priority*/ 0,
+	/*disable_pf_transfer_len*/ {0, 0},
+	/*min_prefetch*/ {0, 0},
+	/*max_prefetch*/ {0, 0},
+	/*max_pf_ceiling*/ {0, 0},
+	/*flags2*/ 0,
+	/*cache_segments*/ 0,
+	/*cache_seg_size*/ {0, 0},
+	/*reserved*/ 0,
+	/*non_cache_seg_size*/ {0, 0, 0}
+};
+
+static struct scsi_control_page control_page_default = {
+	/*page_code*/SMS_CONTROL_MODE_PAGE,
+	/*page_length*/sizeof(struct scsi_control_page) - 2,
+	/*rlec*/0,
+	/*queue_flags*/0,
+	/*eca_and_aen*/0,
+	/*reserved*/0,
+	/*aen_holdoff_period*/{0, 0}
+};
+
+static struct scsi_control_page control_page_changeable = {
+	/*page_code*/SMS_CONTROL_MODE_PAGE,
+	/*page_length*/sizeof(struct scsi_control_page) - 2,
+	/*rlec*/SCP_DSENSE,
+	/*queue_flags*/0,
+	/*eca_and_aen*/0,
+	/*reserved*/0,
+	/*aen_holdoff_period*/{0, 0}
+};
+
+SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer");
+
+/*
+ * XXX KDM move these into the softc.
+ */
+static int rcv_sync_msg;
+static int persis_offset;
+static uint8_t ctl_pause_rtr;
+static int     ctl_is_single;
+static int     index_to_aps_page;
+
+
+/*
+ * Serial number (0x80), device id (0x83), and supported pages (0x00)
+ */
+#define SCSI_EVPD_NUM_SUPPORTED_PAGES	3
+
+static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
+				  int param);
+static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
+static void ctl_init(void);
+void ctl_shutdown(void);
+static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
+static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
+static void ctl_ioctl_online(void *arg);
+static void ctl_ioctl_offline(void *arg);
+static int ctl_ioctl_targ_enable(void *arg, struct ctl_id targ_id);
+static int ctl_ioctl_targ_disable(void *arg, struct ctl_id targ_id);
+static int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id);
+static int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id);
+static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio);
+static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio, int have_lock);
+static int ctl_ioctl_submit_wait(union ctl_io *io);
+static void ctl_ioctl_datamove(union ctl_io *io);
+static void ctl_ioctl_done(union ctl_io *io);
+static void ctl_ioctl_hard_startstop_callback(void *arg,
+					      struct cfi_metatask *metatask);
+static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask);
+static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
+			      struct ctl_ooa *ooa_hdr);
+static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
+		     struct thread *td);
+uint32_t ctl_get_resindex(struct ctl_nexus *nexus);
+uint32_t ctl_port_idx(int port_num);
+#ifdef unused
+static union ctl_io *ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port,
+				   uint32_t targ_target, uint32_t targ_lun,
+				   int can_wait);
+static void ctl_kfree_io(union ctl_io *io);
+#endif /* unused */
+static void ctl_free_io_internal(union ctl_io *io, int have_lock);
+static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
+			 struct ctl_be_lun *be_lun, struct ctl_id target_id);
+static int ctl_free_lun(struct ctl_lun *lun);
+static void ctl_create_lun(struct ctl_be_lun *be_lun);
+/**
+static void ctl_failover_change_pages(struct ctl_softc *softc,
+				      struct ctl_scsiio *ctsio, int master);
+**/
+
+static int ctl_do_mode_select(union ctl_io *io);
+static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
+			   uint64_t res_key, uint64_t sa_res_key,
+			   uint8_t type, uint32_t residx,
+			   struct ctl_scsiio *ctsio,
+			   struct scsi_per_res_out *cdb,
+			   struct scsi_per_res_out_parms* param);
+static void ctl_pro_preempt_other(struct ctl_lun *lun,
+				  union ctl_ha_msg *msg);
+static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg);
+static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
+static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
+static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
+static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
+static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
+static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len);
+static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2);
+static ctl_action ctl_check_for_blockage(union ctl_io *pending_io,
+					 union ctl_io *ooa_io);
+static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
+				union ctl_io *starting_io);
+static int ctl_check_blocked(struct ctl_lun *lun);
+static int ctl_scsiio_lun_check(struct ctl_softc *ctl_softc,
+				struct ctl_lun *lun,
+				struct ctl_cmd_entry *entry,
+				struct ctl_scsiio *ctsio);
+//static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc);
+static void ctl_failover(void);
+static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
+			       struct ctl_scsiio *ctsio);
+static int ctl_scsiio(struct ctl_scsiio *ctsio);
+
+static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
+static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
+			    ctl_ua_type ua_type);
+static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io,
+			 ctl_ua_type ua_type);
+static int ctl_abort_task(union ctl_io *io);
+static void ctl_run_task_queue(struct ctl_softc *ctl_softc);
+#ifdef CTL_IO_DELAY
+static void ctl_datamove_timer_wakeup(void *arg);
+static void ctl_done_timer_wakeup(void *arg);
+#endif /* CTL_IO_DELAY */
+
+static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
+static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
+static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
+static void ctl_datamove_remote_write(union ctl_io *io);
+static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
+static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
+static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
+static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
+				    ctl_ha_dt_cb callback);
+static void ctl_datamove_remote_read(union ctl_io *io);
+static void ctl_datamove_remote(union ctl_io *io);
+static int ctl_process_done(union ctl_io *io, int have_lock);
+static void ctl_work_thread(void *arg);
+
+/*
+ * Load the serialization table.  This isn't very pretty, but is probably
+ * the easiest way to do it.
+ */
+#include "ctl_ser_table.c"
+
+/*
+ * We only need to define open, close and ioctl routines for this driver.
+ */
+static struct cdevsw ctl_cdevsw = {
+	.d_version =	D_VERSION,
+	.d_flags =	0,
+	.d_open =	ctl_open,
+	.d_close =	ctl_close,
+	.d_ioctl =	ctl_ioctl,
+	.d_name =	"ctl",
+};
+
+
+MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
+
+/*
+ * If we have the CAM SIM, we may or may not have another SIM that will
+ * cause CTL to get initialized.  If not, we need to initialize it.
+ */
+SYSINIT(ctl_init, SI_SUB_CONFIGURE, SI_ORDER_THIRD, ctl_init, NULL);
+
+static void
+ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
+			    union ctl_ha_msg *msg_info)
+{
+	struct ctl_scsiio *ctsio;
+
+	if (msg_info->hdr.original_sc == NULL) {
+		printf("%s: original_sc == NULL!\n", __func__);
+		/* XXX KDM now what? */
+		return;
+	}
+
+	ctsio = &msg_info->hdr.original_sc->scsiio;
+	ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
+	ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
+	ctsio->io_hdr.status = msg_info->hdr.status;
+	ctsio->scsi_status = msg_info->scsi.scsi_status;
+	ctsio->sense_len = msg_info->scsi.sense_len;
+	ctsio->sense_residual = msg_info->scsi.sense_residual;
+	ctsio->residual = msg_info->scsi.residual;
+	memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
+	       sizeof(ctsio->sense_data));
+	memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
+	       &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen));;
+	STAILQ_INSERT_TAIL(&ctl_softc->isc_queue, &ctsio->io_hdr, links);
+	ctl_wakeup_thread();
+}
+
+static void
+ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
+				union ctl_ha_msg *msg_info)
+{
+	struct ctl_scsiio *ctsio;
+
+	if (msg_info->hdr.serializing_sc == NULL) {
+		printf("%s: serializing_sc == NULL!\n", __func__);
+		/* XXX KDM now what? */
+		return;
+	}
+
+	ctsio = &msg_info->hdr.serializing_sc->scsiio;
+#if 0
+	/*
+	 * Attempt to catch the situation where an I/O has
+	 * been freed, and we're using it again.
+	 */
+	if (ctsio->io_hdr.io_type == 0xff) {
+		union ctl_io *tmp_io;
+		tmp_io = (union ctl_io *)ctsio;
+		printf("%s: %p use after free!\n", __func__,
+		       ctsio);
+		printf("%s: type %d msg %d cdb %x iptl: "
+		       "%d:%d:%d:%d tag 0x%04x "
+		       "flag %#x status %x\n",
+			__func__,
+			tmp_io->io_hdr.io_type,
+			tmp_io->io_hdr.msg_type,
+			tmp_io->scsiio.cdb[0],
+			tmp_io->io_hdr.nexus.initid.id,
+			tmp_io->io_hdr.nexus.targ_port,
+			tmp_io->io_hdr.nexus.targ_target.id,
+			tmp_io->io_hdr.nexus.targ_lun,
+			(tmp_io->io_hdr.io_type ==
+			CTL_IO_TASK) ?
+			tmp_io->taskio.tag_num :
+			tmp_io->scsiio.tag_num,
+		        tmp_io->io_hdr.flags,
+			tmp_io->io_hdr.status);
+	}
+#endif
+	ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
+	STAILQ_INSERT_TAIL(&ctl_softc->isc_queue, &ctsio->io_hdr, links);
+	ctl_wakeup_thread();
+}
+
+/*
+ * ISC (Inter Shelf Communication) event handler.  Events from the HA
+ * subsystem come in here.
+ */
+static void
+ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
+{
+	struct ctl_softc *ctl_softc;
+	union ctl_io *io;
+	struct ctl_prio *presio;
+	ctl_ha_status isc_status;
+
+	ctl_softc = control_softc;
+	io = NULL;
+
+
+#if 0
+	printf("CTL: Isc Msg event %d\n", event);
+#endif
+	if (event == CTL_HA_EVT_MSG_RECV) {
+		union ctl_ha_msg msg_info;
+
+		isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
+					     sizeof(msg_info), /*wait*/ 0);
+#if 0
+		printf("CTL: msg_type %d\n", msg_info.msg_type);
+#endif
+		if (isc_status != 0) {
+			printf("Error receiving message, status = %d\n",
+			       isc_status);
+			return;
+		}
+		mtx_lock(&ctl_softc->ctl_lock);
+
+		switch (msg_info.hdr.msg_type) {
+		case CTL_MSG_SERIALIZE:
+#if 0
+			printf("Serialize\n");
+#endif
+			io = ctl_alloc_io((void *)ctl_softc->othersc_pool);
+			if (io == NULL) {
+				printf("ctl_isc_event_handler: can't allocate "
+				       "ctl_io!\n");
+				/* Bad Juju */
+				/* Need to set busy and send msg back */
+				mtx_unlock(&ctl_softc->ctl_lock);
+				msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
+				msg_info.hdr.status = CTL_SCSI_ERROR;
+				msg_info.scsi.scsi_status = SCSI_STATUS_BUSY;
+				msg_info.scsi.sense_len = 0;
+			        if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
+				    sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){
+				}
+				goto bailout;
+			}
+			ctl_zero_io(io);
+			// populate ctsio from msg_info
+			io->io_hdr.io_type = CTL_IO_SCSI;
+			io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
+			io->io_hdr.original_sc = msg_info.hdr.original_sc;
+#if 0
+			printf("pOrig %x\n", (int)msg_info.original_sc);
+#endif
+			io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
+					    CTL_FLAG_IO_ACTIVE;
+			/*
+			 * If we're in serialization-only mode, we don't
+			 * want to go through full done processing.  Thus
+			 * the COPY flag.
+			 *
+			 * XXX KDM add another flag that is more specific.
+			 */
+			if (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)
+				io->io_hdr.flags |= CTL_FLAG_INT_COPY;
+			io->io_hdr.nexus = msg_info.hdr.nexus;
+#if 0
+			printf("targ %d, port %d, iid %d, lun %d\n",
+			       io->io_hdr.nexus.targ_target.id,
+			       io->io_hdr.nexus.targ_port,
+			       io->io_hdr.nexus.initid.id,
+			       io->io_hdr.nexus.targ_lun);
+#endif
+			io->scsiio.tag_num = msg_info.scsi.tag_num;
+			io->scsiio.tag_type = msg_info.scsi.tag_type;
+			memcpy(io->scsiio.cdb, msg_info.scsi.cdb,
+			       CTL_MAX_CDBLEN);
+			if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
+				struct ctl_cmd_entry *entry;
+				uint8_t opcode;
+
+				opcode = io->scsiio.cdb[0];
+				entry = &ctl_cmd_table[opcode];
+				io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
+				io->io_hdr.flags |=
+					entry->flags & CTL_FLAG_DATA_MASK;
+			}
+			STAILQ_INSERT_TAIL(&ctl_softc->isc_queue,
+					   &io->io_hdr, links);
+			ctl_wakeup_thread();
+			break;
+
+		/* Performed on the Originating SC, XFER mode only */
+		case CTL_MSG_DATAMOVE: {
+			struct ctl_sg_entry *sgl;
+			int i, j;
+
+			io = msg_info.hdr.original_sc;
+			if (io == NULL) {
+				printf("%s: original_sc == NULL!\n", __func__);
+				/* XXX KDM do something here */
+				break;
+			}
+			io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
+			io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
+			/*
+			 * Keep track of this, we need to send it back over
+			 * when the datamove is complete.
+			 */
+			io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
+
+			if (msg_info.dt.sg_sequence == 0) {
+				/*
+				 * XXX KDM we use the preallocated S/G list
+				 * here, but we'll need to change this to
+				 * dynamic allocation if we need larger S/G
+				 * lists.
+				 */
+				if (msg_info.dt.kern_sg_entries >
+				    sizeof(io->io_hdr.remote_sglist) /
+				    sizeof(io->io_hdr.remote_sglist[0])) {
+					printf("%s: number of S/G entries "
+					    "needed %u > allocated num %zd\n",
+					    __func__,
+					    msg_info.dt.kern_sg_entries,
+					    sizeof(io->io_hdr.remote_sglist)/
+					    sizeof(io->io_hdr.remote_sglist[0]));
+				
+					/*
+					 * XXX KDM send a message back to
+					 * the other side to shut down the
+					 * DMA.  The error will come back
+					 * through via the normal channel.
+					 */
+					break;
+				}
+				sgl = io->io_hdr.remote_sglist;
+				memset(sgl, 0,
+				       sizeof(io->io_hdr.remote_sglist));
+
+				io->scsiio.kern_data_ptr = (uint8_t *)sgl;
+
+				io->scsiio.kern_sg_entries =
+					msg_info.dt.kern_sg_entries;
+				io->scsiio.rem_sg_entries =
+					msg_info.dt.kern_sg_entries;
+				io->scsiio.kern_data_len =
+					msg_info.dt.kern_data_len;
+				io->scsiio.kern_total_len =
+					msg_info.dt.kern_total_len;
+				io->scsiio.kern_data_resid =
+					msg_info.dt.kern_data_resid;
+				io->scsiio.kern_rel_offset =
+					msg_info.dt.kern_rel_offset;
+				/*
+				 * Clear out per-DMA flags.
+				 */
+				io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK;
+				/*
+				 * Add per-DMA flags that are set for this
+				 * particular DMA request.
+				 */
+				io->io_hdr.flags |= msg_info.dt.flags &
+						    CTL_FLAG_RDMA_MASK;
+			} else
+				sgl = (struct ctl_sg_entry *)
+					io->scsiio.kern_data_ptr;
+
+			for (i = msg_info.dt.sent_sg_entries, j = 0;
+			     i < (msg_info.dt.sent_sg_entries +
+			     msg_info.dt.cur_sg_entries); i++, j++) {
+				sgl[i].addr = msg_info.dt.sg_list[j].addr;
+				sgl[i].len = msg_info.dt.sg_list[j].len;
+
+#if 0
+				printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n",
+				       __func__,
+				       msg_info.dt.sg_list[j].addr,
+				       msg_info.dt.sg_list[j].len,
+				       sgl[i].addr, sgl[i].len, j, i);
+#endif
+			}
+#if 0
+			memcpy(&sgl[msg_info.dt.sent_sg_entries],
+			       msg_info.dt.sg_list,
+			       sizeof(*sgl) * msg_info.dt.cur_sg_entries);
+#endif
+
+			/*
+			 * If this is the last piece of the I/O, we've got
+			 * the full S/G list.  Queue processing in the thread.
+			 * Otherwise wait for the next piece.
+			 */
+			if (msg_info.dt.sg_last != 0) {
+				STAILQ_INSERT_TAIL(&ctl_softc->isc_queue,
+						   &io->io_hdr, links);
+				ctl_wakeup_thread();
+			}
+			break;
+		}
+		/* Performed on the Serializing (primary) SC, XFER mode only */
+		case CTL_MSG_DATAMOVE_DONE: {
+			if (msg_info.hdr.serializing_sc == NULL) {
+				printf("%s: serializing_sc == NULL!\n",
+				       __func__);
+				/* XXX KDM now what? */
+				break;
+			}
+			/*
+			 * We grab the sense information here in case
+			 * there was a failure, so we can return status
+			 * back to the initiator.
+			 */
+			io = msg_info.hdr.serializing_sc;
+			io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
+			io->io_hdr.status = msg_info.hdr.status;
+			io->scsiio.scsi_status = msg_info.scsi.scsi_status;
+			io->scsiio.sense_len = msg_info.scsi.sense_len;
+			io->scsiio.sense_residual =msg_info.scsi.sense_residual;
+			io->io_hdr.port_status = msg_info.scsi.fetd_status;
+			io->scsiio.residual = msg_info.scsi.residual;
+			memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data,
+			       sizeof(io->scsiio.sense_data));
+
+			STAILQ_INSERT_TAIL(&ctl_softc->isc_queue,
+					   &io->io_hdr, links);
+			ctl_wakeup_thread();
+			break;
+		}
+
+		/* Preformed on Originating SC, SER_ONLY mode */
+		case CTL_MSG_R2R:
+			io = msg_info.hdr.original_sc;
+			if (io == NULL) {
+				printf("%s: Major Bummer\n", __func__);
+				mtx_unlock(&ctl_softc->ctl_lock);
+				return;
+			} else {
+#if 0
+				printf("pOrig %x\n",(int) ctsio);
+#endif
+			}
+			io->io_hdr.msg_type = CTL_MSG_R2R;
+			io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
+			STAILQ_INSERT_TAIL(&ctl_softc->isc_queue,
+					   &io->io_hdr, links);
+			ctl_wakeup_thread();
+			break;
+
+		/*
+		 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
+		 * mode.
+		 * Performed on the Originating (i.e. secondary) SC in XFER
+		 * mode
+		 */
+		case CTL_MSG_FINISH_IO:
+			if (ctl_softc->ha_mode == CTL_HA_MODE_XFER)
+				ctl_isc_handler_finish_xfer(ctl_softc,
+							    &msg_info);
+			else
+				ctl_isc_handler_finish_ser_only(ctl_softc,
+								&msg_info);
+			break;
+
+		/* Preformed on Originating SC */
+		case CTL_MSG_BAD_JUJU:
+			io = msg_info.hdr.original_sc;
+			if (io == NULL) {
+				printf("%s: Bad JUJU!, original_sc is NULL!\n",
+				       __func__);
+				break;
+			}
+			ctl_copy_sense_data(&msg_info, io);
+			/*
+			 * IO should have already been cleaned up on other
+			 * SC so clear this flag so we won't send a message
+			 * back to finish the IO there.
+			 */
+			io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
+			io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
+
+			/* io = msg_info.hdr.serializing_sc; */
+			io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
+		        STAILQ_INSERT_TAIL(&ctl_softc->isc_queue,
+					   &io->io_hdr, links);
+			ctl_wakeup_thread();
+			break;
+
+		/* Handle resets sent from the other side */
+		case CTL_MSG_MANAGE_TASKS: {
+			struct ctl_taskio *taskio;
+			taskio = (struct ctl_taskio *)ctl_alloc_io(
+				(void *)ctl_softc->othersc_pool);
+			if (taskio == NULL) {
+				printf("ctl_isc_event_handler: can't allocate "
+				       "ctl_io!\n");
+				/* Bad Juju */
+				/* should I just call the proper reset func
+				   here??? */
+				mtx_unlock(&ctl_softc->ctl_lock);
+				goto bailout;
+			}
+			ctl_zero_io((union ctl_io *)taskio);
+			taskio->io_hdr.io_type = CTL_IO_TASK;
+			taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
+			taskio->io_hdr.nexus = msg_info.hdr.nexus;
+			taskio->task_action = msg_info.task.task_action;
+			taskio->tag_num = msg_info.task.tag_num;
+			taskio->tag_type = msg_info.task.tag_type;
+#ifdef CTL_TIME_IO
+			taskio->io_hdr.start_time = time_uptime;
+			getbintime(&taskio->io_hdr.start_bt);
+#if 0
+			cs_prof_gettime(&taskio->io_hdr.start_ticks);
+#endif
+#endif /* CTL_TIME_IO */
+		        STAILQ_INSERT_TAIL(&ctl_softc->task_queue,
+					   &taskio->io_hdr, links);
+			ctl_softc->flags |= CTL_FLAG_TASK_PENDING;
+			ctl_wakeup_thread();
+			break;
+		}
+		/* Persistent Reserve action which needs attention */
+		case CTL_MSG_PERS_ACTION:
+			presio = (struct ctl_prio *)ctl_alloc_io(
+				(void *)ctl_softc->othersc_pool);
+			if (presio == NULL) {
+				printf("ctl_isc_event_handler: can't allocate "
+				       "ctl_io!\n");
+				/* Bad Juju */
+				/* Need to set busy and send msg back */
+				mtx_unlock(&ctl_softc->ctl_lock);
+				goto bailout;
+			}
+			ctl_zero_io((union ctl_io *)presio);
+			presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
+			presio->pr_msg = msg_info.pr;
+		        STAILQ_INSERT_TAIL(&ctl_softc->isc_queue,
+					   &presio->io_hdr, links);
+			ctl_wakeup_thread();
+			break;
+		case CTL_MSG_SYNC_FE:
+			rcv_sync_msg = 1;
+			break;
+		case CTL_MSG_APS_LOCK: {
+			// It's quicker to execute this then to
+			// queue it.
+			struct ctl_lun *lun;
+			struct ctl_page_index *page_index;
+			struct copan_aps_subpage *current_sp;
+
+			lun = ctl_softc->ctl_luns[msg_info.hdr.nexus.targ_lun];
+			page_index = &lun->mode_pages.index[index_to_aps_page];
+			current_sp = (struct copan_aps_subpage *)
+				     (page_index->page_data +
+				     (page_index->page_len * CTL_PAGE_CURRENT));
+
+			current_sp->lock_active = msg_info.aps.lock_flag;
+		        break;
+		}
+		default:
+		        printf("How did I get here?\n");
+		}
+		mtx_unlock(&ctl_softc->ctl_lock);
+	} else if (event == CTL_HA_EVT_MSG_SENT) {
+		if (param != CTL_HA_STATUS_SUCCESS) {
+			printf("Bad status from ctl_ha_msg_send status %d\n",
+			       param);
+		}
+		return;
+	} else if (event == CTL_HA_EVT_DISCONNECT) {
+		printf("CTL: Got a disconnect from Isc\n");
+		return;
+	} else {
+		printf("ctl_isc_event_handler: Unknown event %d\n", event);
+		return;
+	}
+
+bailout:
+	return;
+}
+
+static void
+ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
+{
+	struct scsi_sense_data *sense;
+
+	sense = &dest->scsiio.sense_data;
+	bcopy(&src->scsi.sense_data, sense, sizeof(*sense));
+	dest->scsiio.scsi_status = src->scsi.scsi_status;
+	dest->scsiio.sense_len = src->scsi.sense_len;
+	dest->io_hdr.status = src->hdr.status;
+}
+
+static void
+ctl_init(void)
+{
+	struct ctl_softc *softc;
+	struct ctl_io_pool *internal_pool, *emergency_pool, *other_pool;
+	struct ctl_frontend *fe;
+	struct ctl_lun *lun;
+        uint8_t sc_id =0;
+#if 0
+	int i;
+#endif
+	int retval;
+	//int isc_retval;
+
+	retval = 0;
+	ctl_pause_rtr = 0;
+        rcv_sync_msg = 0;
+
+	control_softc = malloc(sizeof(*control_softc), M_DEVBUF, M_WAITOK);
+	softc = control_softc;
+
+	memset(softc, 0, sizeof(*softc));
+
+	softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600,
+			      "cam/ctl");
+
+	softc->dev->si_drv1 = softc;
+
+	mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
+	softc->open_count = 0;
+
+	/*
+	 * Default to actually sending a SYNCHRONIZE CACHE command down to
+	 * the drive.
+	 */
+	softc->flags = CTL_FLAG_REAL_SYNC;
+
+	/*
+	 * In Copan's HA scheme, the "master" and "slave" roles are
+	 * figured out through the slot the controller is in.  Although it
+	 * is an active/active system, someone has to be in charge.
+ 	 */
+#ifdef NEEDTOPORT
+        scmicro_rw(SCMICRO_GET_SHELF_ID, &sc_id);
+#endif
+
+        if (sc_id == 0) {
+		softc->flags |= CTL_FLAG_MASTER_SHELF;
+		persis_offset = 0;
+	} else
+		persis_offset = CTL_MAX_INITIATORS;
+
+	/*
+	 * XXX KDM need to figure out where we want to get our target ID
+	 * and WWID.  Is it different on each port?
+	 */
+	softc->target.id = 0;
+	softc->target.wwid[0] = 0x12345678;
+	softc->target.wwid[1] = 0x87654321;
+	STAILQ_INIT(&softc->lun_list);
+	STAILQ_INIT(&softc->pending_lun_queue);
+	STAILQ_INIT(&softc->task_queue);
+	STAILQ_INIT(&softc->incoming_queue);
+	STAILQ_INIT(&softc->rtr_queue);
+	STAILQ_INIT(&softc->done_queue);
+	STAILQ_INIT(&softc->isc_queue);
+	STAILQ_INIT(&softc->fe_list);
+	STAILQ_INIT(&softc->be_list);
+	STAILQ_INIT(&softc->io_pools);
+
+	lun = &softc->lun;
+
+	/*
+	 * We don't bother calling these with ctl_lock held here, because,
+	 * in theory, no one else can try to do anything while we're in our
+	 * module init routine.
+	 */
+	if (ctl_pool_create(softc, CTL_POOL_INTERNAL, CTL_POOL_ENTRIES_INTERNAL,
+			    &internal_pool)!= 0){
+		printf("ctl: can't allocate %d entry internal pool, "
+		       "exiting\n", CTL_POOL_ENTRIES_INTERNAL);
+		return;
+	}
+
+	if (ctl_pool_create(softc, CTL_POOL_EMERGENCY,
+			    CTL_POOL_ENTRIES_EMERGENCY, &emergency_pool) != 0) {
+		printf("ctl: can't allocate %d entry emergency pool, "
+		       "exiting\n", CTL_POOL_ENTRIES_EMERGENCY);
+		ctl_pool_free(softc, internal_pool);
+		return;
+	}
+
+	if (ctl_pool_create(softc, CTL_POOL_4OTHERSC, CTL_POOL_ENTRIES_OTHER_SC,
+	                    &other_pool) != 0)
+	{
+		printf("ctl: can't allocate %d entry other SC pool, "
+		       "exiting\n", CTL_POOL_ENTRIES_OTHER_SC);
+		ctl_pool_free(softc, internal_pool);
+		ctl_pool_free(softc, emergency_pool);
+		return;
+	}
+
+	softc->internal_pool = internal_pool;
+	softc->emergency_pool = emergency_pool;
+	softc->othersc_pool = other_pool;
+
+	ctl_pool_acquire(internal_pool);
+	ctl_pool_acquire(emergency_pool);
+	ctl_pool_acquire(other_pool);
+
+	/*
+	 * We used to allocate a processor LUN here.  The new scheme is to
+	 * just let the user allocate LUNs as he sees fit.
+	 */
+#if 0
+	mtx_lock(&softc->ctl_lock);
+	ctl_alloc_lun(softc, lun, /*be_lun*/NULL, /*target*/softc->target);
+	mtx_unlock(&softc->ctl_lock);
+#endif
+
+	if (kproc_create(ctl_work_thread, softc, &softc->work_thread, 0, 0,
+			 "ctl_thrd") != 0) {
+		printf("error creating CTL work thread!\n");
+		ctl_free_lun(lun);
+		ctl_pool_free(softc, internal_pool);
+		ctl_pool_free(softc, emergency_pool);
+		ctl_pool_free(softc, other_pool);
+		return;
+	}
+	printf("ctl: CAM Target Layer loaded\n");
+
+	/*
+	 * Initialize the initiator and portname mappings
+	 */
+	memset(softc->wwpn_iid, 0, sizeof(softc->wwpn_iid));
+
+	/*
+	 * Initialize the ioctl front end.
+	 */
+	fe = &softc->ioctl_info.fe;
+	sprintf(softc->ioctl_info.port_name, "CTL ioctl");
+	fe->port_type = CTL_PORT_IOCTL;
+	fe->num_requested_ctl_io = 100;
+	fe->port_name = softc->ioctl_info.port_name;
+	fe->port_online = ctl_ioctl_online;
+	fe->port_offline = ctl_ioctl_offline;
+	fe->onoff_arg = &softc->ioctl_info;
+	fe->targ_enable = ctl_ioctl_targ_enable;
+	fe->targ_disable = ctl_ioctl_targ_disable;
+	fe->lun_enable = ctl_ioctl_lun_enable;
+	fe->lun_disable = ctl_ioctl_lun_disable;
+	fe->targ_lun_arg = &softc->ioctl_info;
+	fe->fe_datamove = ctl_ioctl_datamove;
+	fe->fe_done = ctl_ioctl_done;
+	fe->max_targets = 15;
+	fe->max_target_id = 15;
+
+	if (ctl_frontend_register(&softc->ioctl_info.fe,
+	                  (softc->flags & CTL_FLAG_MASTER_SHELF)) != 0) {
+		printf("ctl: ioctl front end registration failed, will "
+		       "continue anyway\n");
+	}
+
+#ifdef CTL_IO_DELAY
+	if (sizeof(struct callout) > CTL_TIMER_BYTES) {
+		printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n",
+		       sizeof(struct callout), CTL_TIMER_BYTES);
+		return;
+	}
+#endif /* CTL_IO_DELAY */
+
+}
+
+void
+ctl_shutdown(void)
+{
+	struct ctl_softc *softc;
+	struct ctl_lun *lun, *next_lun;
+	struct ctl_io_pool *pool, *next_pool;
+
+	softc = (struct ctl_softc *)control_softc;
+
+	if (ctl_frontend_deregister(&softc->ioctl_info.fe) != 0)
+		printf("ctl: ioctl front end deregistration failed\n");
+
+	mtx_lock(&softc->ctl_lock);
+
+	/*
+	 * Free up each LUN.
+	 */
+	for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){
+		next_lun = STAILQ_NEXT(lun, links);
+		ctl_free_lun(lun);
+	}
+
+	/*
+	 * This will rip the rug out from under any FETDs or anyone else
+	 * that has a pool allocated.  Since we increment our module
+	 * refcount any time someone outside the main CTL module allocates
+	 * a pool, we shouldn't have any problems here.  The user won't be
+	 * able to unload the CTL module until client modules have
+	 * successfully unloaded.
+	 */
+	for (pool = STAILQ_FIRST(&softc->io_pools); pool != NULL;
+	     pool = next_pool) {
+		next_pool = STAILQ_NEXT(pool, links);
+		ctl_pool_free(softc, pool);
+	}
+
+	mtx_unlock(&softc->ctl_lock);
+
+#if 0
+	ctl_shutdown_thread(softc->work_thread);
+#endif
+
+	mtx_destroy(&softc->ctl_lock);
+
+	destroy_dev(softc->dev);
+
+	printf("ctl: CAM Target Layer unloaded\n");
+}
+
+/*
+ * XXX KDM should we do some access checks here?  Bump a reference count to
+ * prevent a CTL module from being unloaded while someone has it open?
+ */
+static int
+ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
+{
+	return (0);
+}
+
+static int
+ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
+{
+	return (0);
+}
+
+int
+ctl_port_enable(ctl_port_type port_type)
+{
+	struct ctl_softc *softc;
+	struct ctl_frontend *fe;
+
+	if (ctl_is_single == 0) {
+		union ctl_ha_msg msg_info;
+		int isc_retval;
+
+#if 0
+		printf("%s: HA mode, synchronizing frontend enable\n",
+		        __func__);
+#endif
+		msg_info.hdr.msg_type = CTL_MSG_SYNC_FE;
+	        if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
+		        sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) {
+			printf("Sync msg send error retval %d\n", isc_retval);
+		}
+		if (!rcv_sync_msg) {
+			isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
+			        sizeof(msg_info), 1);
+		}
+#if 0
+        	printf("CTL:Frontend Enable\n");
+	} else {
+		printf("%s: single mode, skipping frontend synchronization\n",
+		        __func__);
+#endif
+	}
+
+	softc = control_softc;
+
+	STAILQ_FOREACH(fe, &softc->fe_list, links) {
+		if (port_type & fe->port_type)
+		{
+#if 0
+			printf("port %d\n", fe->targ_port);
+#endif
+			ctl_frontend_online(fe);
+		}
+	}
+
+	return (0);
+}
+
+int
+ctl_port_disable(ctl_port_type port_type)
+{
+	struct ctl_softc *softc;
+	struct ctl_frontend *fe;
+
+	softc = control_softc;
+
+	STAILQ_FOREACH(fe, &softc->fe_list, links) {
+		if (port_type & fe->port_type)
+			ctl_frontend_offline(fe);
+	}
+
+	return (0);
+}
+
+/*
+ * Returns 0 for success, 1 for failure.
+ * Currently the only failure mode is if there aren't enough entries
+ * allocated.  So, in case of a failure, look at num_entries_dropped,
+ * reallocate and try again.
+ */
+int
+ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced,
+	      int *num_entries_filled, int *num_entries_dropped,
+	      ctl_port_type port_type, int no_virtual)
+{
+	struct ctl_softc *softc;
+	struct ctl_frontend *fe;
+	int entries_dropped, entries_filled;
+	int retval;
+	int i;
+
+	softc = control_softc;
+
+	retval = 0;
+	entries_filled = 0;
+	entries_dropped = 0;
+
+	i = 0;
+	mtx_lock(&softc->ctl_lock);
+	STAILQ_FOREACH(fe, &softc->fe_list, links) {
+		struct ctl_port_entry *entry;
+
+		if ((fe->port_type & port_type) == 0)
+			continue;
+
+		if ((no_virtual != 0)
+		 && (fe->virtual_port != 0))
+			continue;
+
+		if (entries_filled >= num_entries_alloced) {
+			entries_dropped++;
+			continue;
+		}
+		entry = &entries[i];
+
+		entry->port_type = fe->port_type;
+		strlcpy(entry->port_name, fe->port_name,
+			sizeof(entry->port_name));
+		entry->physical_port = fe->physical_port;
+		entry->virtual_port = fe->virtual_port;
+		entry->wwnn = fe->wwnn;
+		entry->wwpn = fe->wwpn;
+
+		i++;
+		entries_filled++;
+	}
+
+	mtx_unlock(&softc->ctl_lock);
+
+	if (entries_dropped > 0)
+		retval = 1;
+
+	*num_entries_dropped = entries_dropped;
+	*num_entries_filled = entries_filled;
+
+	return (retval);
+}
+
+static void
+ctl_ioctl_online(void *arg)
+{
+	struct ctl_ioctl_info *ioctl_info;
+
+	ioctl_info = (struct ctl_ioctl_info *)arg;
+
+	ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED;
+}
+
+static void
+ctl_ioctl_offline(void *arg)
+{
+	struct ctl_ioctl_info *ioctl_info;
+
+	ioctl_info = (struct ctl_ioctl_info *)arg;
+
+	ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED;
+}
+
+/*
+ * Remove an initiator by port number and initiator ID.
+ * Returns 0 for success, 1 for failure.
+ * Assumes the caller does NOT hold the CTL lock.
+ */
+int
+ctl_remove_initiator(int32_t targ_port, uint32_t iid)
+{
+	struct ctl_softc *softc;
+
+	softc = control_softc;
+
+	if ((targ_port < 0)
+	 || (targ_port > CTL_MAX_PORTS)) {
+		printf("%s: invalid port number %d\n", __func__, targ_port);
+		return (1);
+	}
+	if (iid > CTL_MAX_INIT_PER_PORT) {
+		printf("%s: initiator ID %u > maximun %u!\n",
+		       __func__, iid, CTL_MAX_INIT_PER_PORT);
+		return (1);
+	}
+
+	mtx_lock(&softc->ctl_lock);
+
+	softc->wwpn_iid[targ_port][iid].in_use = 0;
+
+	mtx_unlock(&softc->ctl_lock);
+
+	return (0);
+}
+
+/*
+ * Add an initiator to the initiator map.
+ * Returns 0 for success, 1 for failure.
+ * Assumes the caller does NOT hold the CTL lock.
+ */
+int
+ctl_add_initiator(uint64_t wwpn, int32_t targ_port, uint32_t iid)
+{
+	struct ctl_softc *softc;
+	int retval;
+
+	softc = control_softc;
+
+	retval = 0;
+
+	if ((targ_port < 0)
+	 || (targ_port > CTL_MAX_PORTS)) {
+		printf("%s: invalid port number %d\n", __func__, targ_port);
+		return (1);
+	}
+	if (iid > CTL_MAX_INIT_PER_PORT) {
+		printf("%s: WWPN %#jx initiator ID %u > maximun %u!\n",
+		       __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
+		return (1);
+	}
+
+	mtx_lock(&softc->ctl_lock);
+
+	if (softc->wwpn_iid[targ_port][iid].in_use != 0) {
+		/*
+		 * We don't treat this as an error.
+		 */
+		if (softc->wwpn_iid[targ_port][iid].wwpn == wwpn) {
+			printf("%s: port %d iid %u WWPN %#jx arrived again?\n",
+			       __func__, targ_port, iid, (uintmax_t)wwpn);
+			goto bailout;
+		}
+
+		/*
+		 * This is an error, but what do we do about it?  The
+		 * driver is telling us we have a new WWPN for this
+		 * initiator ID, so we pretty much need to use it.
+		 */
+		printf("%s: port %d iid %u WWPN %#jx arrived, WWPN %#jx is "
+		       "still at that address\n", __func__, targ_port, iid,
+		       (uintmax_t)wwpn,
+		       (uintmax_t)softc->wwpn_iid[targ_port][iid].wwpn);
+
+		/*
+		 * XXX KDM clear have_ca and ua_pending on each LUN for
+		 * this initiator.
+		 */
+	}
+	softc->wwpn_iid[targ_port][iid].in_use = 1;
+	softc->wwpn_iid[targ_port][iid].iid = iid;
+	softc->wwpn_iid[targ_port][iid].wwpn = wwpn;
+	softc->wwpn_iid[targ_port][iid].port = targ_port;
+
+bailout:
+
+	mtx_unlock(&softc->ctl_lock);
+
+	return (retval);
+}
+
+/*
+ * XXX KDM should we pretend to do something in the target/lun
+ * enable/disable functions?
+ */
+static int
+ctl_ioctl_targ_enable(void *arg, struct ctl_id targ_id)
+{
+	return (0);
+}
+
+static int
+ctl_ioctl_targ_disable(void *arg, struct ctl_id targ_id)
+{
+	return (0);
+}
+
+static int
+ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id)
+{
+	return (0);
+}
+
+static int
+ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id)
+{
+	return (0);
+}
+
+/*
+ * Data movement routine for the CTL ioctl frontend port.
+ */
+static int
+ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio)
+{
+	struct ctl_sg_entry *ext_sglist, *kern_sglist;
+	struct ctl_sg_entry ext_entry, kern_entry;
+	int ext_sglen, ext_sg_entries, kern_sg_entries;
+	int ext_sg_start, ext_offset;
+	int len_to_copy, len_copied;
+	int kern_watermark, ext_watermark;
+	int ext_sglist_malloced;
+	int i, j;
+
+	ext_sglist_malloced = 0;
+	ext_sg_start = 0;
+	ext_offset = 0;
+
+	CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n"));
+
+	/*
+	 * If this flag is set, fake the data transfer.
+	 */
+	if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) {
+		ctsio->ext_data_filled = ctsio->ext_data_len;
+		goto bailout;
+	}
+
+	/*
+	 * To simplify things here, if we have a single buffer, stick it in
+	 * a S/G entry and just make it a single entry S/G list.
+	 */
+	if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) {
+		int len_seen;
+
+		ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist);
+
+		ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL,
+							   M_WAITOK);
+		if (ext_sglist == NULL) {
+			ctl_set_internal_failure(ctsio,
+						 /*sks_valid*/ 0,
+						 /*retry_count*/ 0);
+			return (CTL_RETVAL_COMPLETE);
+		}
+		ext_sglist_malloced = 1;
+		if (copyin(ctsio->ext_data_ptr, ext_sglist,
+				   ext_sglen) != 0) {
+			ctl_set_internal_failure(ctsio,
+						 /*sks_valid*/ 0,
+						 /*retry_count*/ 0);
+			goto bailout;
+		}
+		ext_sg_entries = ctsio->ext_sg_entries;
+		len_seen = 0;
+		for (i = 0; i < ext_sg_entries; i++) {
+			if ((len_seen + ext_sglist[i].len) >=
+			     ctsio->ext_data_filled) {
+				ext_sg_start = i;
+				ext_offset = ctsio->ext_data_filled - len_seen;
+				break;
+			}
+			len_seen += ext_sglist[i].len;
+		}
+	} else {
+		ext_sglist = &ext_entry;
+		ext_sglist->addr = ctsio->ext_data_ptr;
+		ext_sglist->len = ctsio->ext_data_len;
+		ext_sg_entries = 1;
+		ext_sg_start = 0;
+		ext_offset = ctsio->ext_data_filled;
+	}
+
+	if (ctsio->kern_sg_entries > 0) {
+		kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr;
+		kern_sg_entries = ctsio->kern_sg_entries;
+	} else {
+		kern_sglist = &kern_entry;
+		kern_sglist->addr = ctsio->kern_data_ptr;
+		kern_sglist->len = ctsio->kern_data_len;
+		kern_sg_entries = 1;
+	}
+
+
+	kern_watermark = 0;
+	ext_watermark = ext_offset;
+	len_copied = 0;
+	for (i = ext_sg_start, j = 0;
+	     i < ext_sg_entries && j < kern_sg_entries;) {
+		uint8_t *ext_ptr, *kern_ptr;
+
+		len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark,
+				      kern_sglist[j].len - kern_watermark);
+
+		ext_ptr = (uint8_t *)ext_sglist[i].addr;
+		ext_ptr = ext_ptr + ext_watermark;
+		if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
+			/*
+			 * XXX KDM fix this!
+			 */
+			panic("need to implement bus address support");
+#if 0
+			kern_ptr = bus_to_virt(kern_sglist[j].addr);
+#endif
+		} else
+			kern_ptr = (uint8_t *)kern_sglist[j].addr;
+		kern_ptr = kern_ptr + kern_watermark;
+
+		kern_watermark += len_to_copy;
+		ext_watermark += len_to_copy;
+
+		if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
+		     CTL_FLAG_DATA_IN) {
+			CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
+					 "bytes to user\n", len_to_copy));
+			CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
+					 "to %p\n", kern_ptr, ext_ptr));
+			if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) {
+				ctl_set_internal_failure(ctsio,
+							 /*sks_valid*/ 0,
+							 /*retry_count*/ 0);
+				goto bailout;
+			}
+		} else {
+			CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
+					 "bytes from user\n", len_to_copy));
+			CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
+					 "to %p\n", ext_ptr, kern_ptr));
+			if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){
+				ctl_set_internal_failure(ctsio,
+							 /*sks_valid*/ 0,
+							 /*retry_count*/0);
+				goto bailout;
+			}
+		}
+
+		len_copied += len_to_copy;
+
+		if (ext_sglist[i].len == ext_watermark) {
+			i++;
+			ext_watermark = 0;
+		}
+
+		if (kern_sglist[j].len == kern_watermark) {
+			j++;
+			kern_watermark = 0;
+		}
+	}
+
+	ctsio->ext_data_filled += len_copied;
+
+	CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, "
+			 "kern_sg_entries: %d\n", ext_sg_entries,
+			 kern_sg_entries));
+	CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, "
+			 "kern_data_len = %d\n", ctsio->ext_data_len,
+			 ctsio->kern_data_len));
+
+
+	/* XXX KDM set residual?? */
+bailout:
+
+	if (ext_sglist_malloced != 0)
+		free(ext_sglist, M_CTL);
+
+	return (CTL_RETVAL_COMPLETE);
+}
+
+/*
+ * Serialize a command that went down the "wrong" side, and so was sent to
+ * this controller for execution.  The logic is a little different than the
+ * standard case in ctl_scsiio_precheck().  Errors in this case need to get
+ * sent back to the other side, but in the success case, we execute the
+ * command on this side (XFER mode) or tell the other side to execute it
+ * (SER_ONLY mode).
+ */
+static int
+ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio, int have_lock)
+{
+	struct ctl_softc *ctl_softc;
+	union ctl_ha_msg msg_info;
+	struct ctl_lun *lun;
+	int retval = 0;
+
+	ctl_softc = control_softc;
+	if (have_lock == 0)
+		mtx_lock(&ctl_softc->ctl_lock);
+
+	lun = ctl_softc->ctl_luns[ctsio->io_hdr.nexus.targ_lun];
+	if (lun==NULL)
+	{
+		/*
+		 * Why isn't LUN defined? The other side wouldn't
+		 * send a cmd if the LUN is undefined.
+		 */
+		printf("%s: Bad JUJU!, LUN is NULL!\n", __func__);
+
+		/* "Logical unit not supported" */
+		ctl_set_sense_data(&msg_info.scsi.sense_data,
+				   lun,
+				   /*sense_format*/SSD_TYPE_NONE,
+				   /*current_error*/ 1,
+				   /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
+				   /*asc*/ 0x25,
+				   /*ascq*/ 0x00,
+				   SSD_ELEM_NONE);
+
+		msg_info.scsi.sense_len = SSD_FULL_SIZE;
+		msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
+		msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
+		msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
+		msg_info.hdr.serializing_sc = NULL;
+		msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
+	        if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
+				sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
+		}
+		if (have_lock == 0)
+			mtx_unlock(&ctl_softc->ctl_lock);
+		return(1);
+
+	}
+
+    	TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
+
+	switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
+		(union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
+		 ooa_links))) {
+	case CTL_ACTION_BLOCK:
+		ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
+		TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
+				  blocked_links);
+		break;
+	case CTL_ACTION_PASS:
+	case CTL_ACTION_SKIP:
+		if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
+			ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
+			STAILQ_INSERT_TAIL(&ctl_softc->rtr_queue,
+					   &ctsio->io_hdr, links);
+		} else {
+
+			/* send msg back to other side */
+			msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
+			msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
+			msg_info.hdr.msg_type = CTL_MSG_R2R;
+#if 0
+			printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc);
+#endif
+		        if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
+			    sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
+			}
+		}
+		break;
+	case CTL_ACTION_OVERLAP:
+		/* OVERLAPPED COMMANDS ATTEMPTED */
+		ctl_set_sense_data(&msg_info.scsi.sense_data,
+				   lun,
+				   /*sense_format*/SSD_TYPE_NONE,
+				   /*current_error*/ 1,
+				   /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
+				   /*asc*/ 0x4E,
+				   /*ascq*/ 0x00,
+				   SSD_ELEM_NONE);
+
+		msg_info.scsi.sense_len = SSD_FULL_SIZE;
+		msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
+		msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
+		msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
+		msg_info.hdr.serializing_sc = NULL;
+		msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
+#if 0
+		printf("BAD JUJU:Major Bummer Overlap\n");
+#endif
+		TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
+		retval = 1;
+		if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
+		    sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
+		}
+		break;
+	case CTL_ACTION_OVERLAP_TAG:
+		/* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */
+		ctl_set_sense_data(&msg_info.scsi.sense_data,
+				   lun,
+				   /*sense_format*/SSD_TYPE_NONE,
+				   /*current_error*/ 1,
+				   /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
+				   /*asc*/ 0x4D,
+				   /*ascq*/ ctsio->tag_num & 0xff,
+				   SSD_ELEM_NONE);
+
+		msg_info.scsi.sense_len = SSD_FULL_SIZE;
+		msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
+		msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
+		msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
+		msg_info.hdr.serializing_sc = NULL;
+		msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
+#if 0
+		printf("BAD JUJU:Major Bummer Overlap Tag\n");
+#endif
+		TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
+		retval = 1;
+		if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
+		    sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
+		}
+		break;
+	case CTL_ACTION_ERROR:
+	default:
+		/* "Internal target failure" */
+		ctl_set_sense_data(&msg_info.scsi.sense_data,
+				   lun,
+				   /*sense_format*/SSD_TYPE_NONE,
+				   /*current_error*/ 1,
+				   /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
+				   /*asc*/ 0x44,
+				   /*ascq*/ 0x00,
+				   SSD_ELEM_NONE);
+
+		msg_info.scsi.sense_len = SSD_FULL_SIZE;
+		msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
+		msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
+		msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
+		msg_info.hdr.serializing_sc = NULL;
+		msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
+#if 0
+		printf("BAD JUJU:Major Bummer HW Error\n");
+#endif
+		TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
+		retval = 1;
+		if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
+		    sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
+		}
+		break;
+	}
+	if (have_lock == 0)
+		mtx_unlock(&ctl_softc->ctl_lock);
+	return (retval);
+}
+
+static int
+ctl_ioctl_submit_wait(union ctl_io *io)
+{
+	struct ctl_fe_ioctl_params params;
+	ctl_fe_ioctl_state last_state;
+	int done, retval;
+
+	retval = 0;
+
+	bzero(&params, sizeof(params));
+
+	mtx_init(&params.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF);
+	cv_init(&params.sem, "ctlioccv");
+	params.state = CTL_IOCTL_INPROG;
+	last_state = params.state;
+
+	io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms;
+
+	CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n"));
+
+	/* This shouldn't happen */
+	if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE)
+		return (retval);
+
+	done = 0;
+
+	do {
+		mtx_lock(&params.ioctl_mtx);
+		/*
+		 * Check the state here, and don't sleep if the state has
+		 * already changed (i.e. wakeup has already occured, but we
+		 * weren't waiting yet).
+		 */
+		if (params.state == last_state) {
+			/* XXX KDM cv_wait_sig instead? */
+			cv_wait(&params.sem, &params.ioctl_mtx);
+		}
+		last_state = params.state;
+
+		switch (params.state) {
+		case CTL_IOCTL_INPROG:
+			/* Why did we wake up? */
+			/* XXX KDM error here? */
+			mtx_unlock(&params.ioctl_mtx);
+			break;
+		case CTL_IOCTL_DATAMOVE:
+			CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n"));
+
+			/*
+			 * change last_state back to INPROG to avoid
+			 * deadlock on subsequent data moves.
+			 */
+			params.state = last_state = CTL_IOCTL_INPROG;
+
+			mtx_unlock(&params.ioctl_mtx);
+			ctl_ioctl_do_datamove(&io->scsiio);
+			/*
+			 * Note that in some cases, most notably writes,
+			 * this will queue the I/O and call us back later.
+			 * In other cases, generally reads, this routine
+			 * will immediately call back and wake us up,
+			 * probably using our own context.
+			 */
+			io->scsiio.be_move_done(io);
+			break;
+		case CTL_IOCTL_DONE:
+			mtx_unlock(&params.ioctl_mtx);
+			CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n"));
+			done = 1;
+			break;
+		default:
+			mtx_unlock(&params.ioctl_mtx);
+			/* XXX KDM error here? */
+			break;
+		}
+	} while (done == 0);
+
+	mtx_destroy(&params.ioctl_mtx);
+	cv_destroy(&params.sem);
+
+	return (CTL_RETVAL_COMPLETE);
+}
+
+static void
+ctl_ioctl_datamove(union ctl_io *io)
+{
+	struct ctl_fe_ioctl_params *params;
+
+	params = (struct ctl_fe_ioctl_params *)
+		io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
+
+	mtx_lock(&params->ioctl_mtx);
+	params->state = CTL_IOCTL_DATAMOVE;
+	cv_broadcast(&params->sem);
+	mtx_unlock(&params->ioctl_mtx);
+}
+
+static void
+ctl_ioctl_done(union ctl_io *io)
+{
+	struct ctl_fe_ioctl_params *params;
+
+	params = (struct ctl_fe_ioctl_params *)
+		io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
+
+	mtx_lock(&params->ioctl_mtx);
+	params->state = CTL_IOCTL_DONE;
+	cv_broadcast(&params->sem);
+	mtx_unlock(&params->ioctl_mtx);
+}
+
+static void
+ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask)
+{
+	struct ctl_fe_ioctl_startstop_info *sd_info;
+
+	sd_info = (struct ctl_fe_ioctl_startstop_info *)arg;
+
+	sd_info->hs_info.status = metatask->status;
+	sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns;
+	sd_info->hs_info.luns_complete =
+		metatask->taskinfo.startstop.luns_complete;
+	sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed;
+
+	cv_broadcast(&sd_info->sem);
+}
+
+static void
+ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask)
+{
+	struct ctl_fe_ioctl_bbrread_info *fe_bbr_info;
+
+	fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg;
+
+	mtx_lock(fe_bbr_info->lock);
+	fe_bbr_info->bbr_info->status = metatask->status;
+	fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
+	fe_bbr_info->wakeup_done = 1;
+	mtx_unlock(fe_bbr_info->lock);
+
+	cv_broadcast(&fe_bbr_info->sem);
+}
+
+/*
+ * Must be called with the ctl_lock held.
+ * Returns 0 for success, errno for failure.
+ */
+static int
+ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
+		   struct ctl_ooa *ooa_hdr)
+{
+	union ctl_io *io;
+	int retval;
+
+	retval = 0;
+
+	for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
+	     (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
+	     ooa_links)) {
+		struct ctl_ooa_entry *cur_entry, entry;
+
+		/*
+		 * If we've got more than we can fit, just count the
+		 * remaining entries.
+		 */
+		if (*cur_fill_num >= ooa_hdr->alloc_num)
+			continue;
+
+		cur_entry = &ooa_hdr->entries[*cur_fill_num];
+
+		bzero(&entry, sizeof(entry));
+
+		entry.tag_num = io->scsiio.tag_num;
+		entry.lun_num = lun->lun;
+#ifdef CTL_TIME_IO
+		entry.start_bt = io->io_hdr.start_bt;
+#endif
+		bcopy(io->scsiio.cdb, entry.cdb, io->scsiio.cdb_len);
+		entry.cdb_len = io->scsiio.cdb_len;
+		if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
+			entry.cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
+
+		if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
+			entry.cmd_flags |= CTL_OOACMD_FLAG_DMA;
+
+		if (io->io_hdr.flags & CTL_FLAG_ABORT)
+			entry.cmd_flags |= CTL_OOACMD_FLAG_ABORT;
+
+		if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
+			entry.cmd_flags |= CTL_OOACMD_FLAG_RTR;
+
+		if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
+			entry.cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
+
+		retval = copyout(&entry, cur_entry, sizeof(entry));
+
+		if (retval != 0)
+			break;
+	}
+
+	return (retval);
+}
+
+static void *
+ctl_copyin_alloc(void *user_addr, int len, char *error_str,
+		 size_t error_str_len)
+{
+	void *kptr;
+
+	kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
+	if (kptr == NULL) {
+		snprintf(error_str, error_str_len, "Cannot allocate %d bytes",
+			 len);
+		return (NULL);
+	}
+
+	if (copyin(user_addr, kptr, len) != 0) {
+		snprintf(error_str, error_str_len, "Error copying %d bytes "
+			 "from user address %p to kernel address %p", len,
+			 user_addr, kptr);
+		free(kptr, M_CTL);
+		return (NULL);
+	}
+
+	return (kptr);
+}
+
+static void
+ctl_free_args(int num_be_args, struct ctl_be_arg *be_args)
+{
+	int i;
+
+	if (be_args == NULL)
+		return;
+
+	for (i = 0; i < num_be_args; i++) {
+		free(be_args[i].kname, M_CTL);
+		free(be_args[i].kvalue, M_CTL);
+	}
+
+	free(be_args, M_CTL);
+}
+
+static struct ctl_be_arg *
+ctl_copyin_args(int num_be_args, struct ctl_be_arg *be_args,
+		char *error_str, size_t error_str_len)
+{
+	struct ctl_be_arg *args;
+	int i;
+
+	args = ctl_copyin_alloc(be_args, num_be_args * sizeof(*be_args),
+				error_str, error_str_len);
+
+	if (args == NULL)
+		goto bailout;
+
+	for (i = 0; i < num_be_args; i++) {
+		uint8_t *tmpptr;
+
+		args[i].kname = ctl_copyin_alloc(args[i].name,
+			args[i].namelen, error_str, error_str_len);
+		if (args[i].kname == NULL)
+			goto bailout;
+
+		if (args[i].kname[args[i].namelen - 1] != '\0') {
+			snprintf(error_str, error_str_len, "Argument %d "
+				 "name is not NUL-terminated", i);
+			goto bailout;
+		}
+
+		args[i].kvalue = NULL;
+
+		tmpptr = ctl_copyin_alloc(args[i].value,
+			args[i].vallen, error_str, error_str_len);
+		if (tmpptr == NULL)
+			goto bailout;
+
+		args[i].kvalue = tmpptr;
+
+		if ((args[i].flags & CTL_BEARG_ASCII)
+		 && (tmpptr[args[i].vallen - 1] != '\0')) {
+			snprintf(error_str, error_str_len, "Argument %d "
+				 "value is not NUL-terminated", i);
+			goto bailout;
+		}
+	}
+
+	return (args);
+bailout:
+
+	ctl_free_args(num_be_args, args);
+
+	return (NULL);
+}
+
+/*
+ * Escape characters that are illegal or not recommended in XML.
+ */
+int
+ctl_sbuf_printf_esc(struct sbuf *sb, char *str)
+{
+	int retval;
+
+	retval = 0;
+
+	for (; *str; str++) {
+		switch (*str) {
+		case '&':
+			retval = sbuf_printf(sb, "&");
+			break;
+		case '>':
+			retval = sbuf_printf(sb, ">");
+			break;
+		case '<':
+			retval = sbuf_printf(sb, "<");
+			break;
+		default:
+			retval = sbuf_putc(sb, *str);
+			break;
+		}
+
+		if (retval != 0)
+			break;
+
+	}
+
+	return (retval);
+}
+
+static int
+ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
+	  struct thread *td)
+{
+	struct ctl_softc *softc;
+	int retval;
+
+	softc = control_softc;
+
+	retval = 0;
+
+	switch (cmd) {
+	case CTL_IO: {
+		union ctl_io *io;
+		void *pool_tmp;
+
+		/*
+		 * If we haven't been "enabled", don't allow any SCSI I/O
+		 * to this FETD.
+		 */
+		if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) {
+			retval = -EPERM;
+			break;
+		}
+
+		io = ctl_alloc_io(softc->ioctl_info.fe.ctl_pool_ref);
+		if (io == NULL) {
+			printf("ctl_ioctl: can't allocate ctl_io!\n");
+			retval = -ENOSPC;
+			break;
+		}
+
+		/*
+		 * Need to save the pool reference so it doesn't get
+		 * spammed by the user's ctl_io.
+		 */
+		pool_tmp = io->io_hdr.pool;
+
+		memcpy(io, (void *)addr, sizeof(*io));
+
+		io->io_hdr.pool = pool_tmp;
+		/*
+		 * No status yet, so make sure the status is set properly.
+		 */
+		io->io_hdr.status = CTL_STATUS_NONE;
+
+		/*
+		 * The user sets the initiator ID, target and LUN IDs.
+		 */
+		io->io_hdr.nexus.targ_port = softc->ioctl_info.fe.targ_port;
+		io->io_hdr.flags |= CTL_FLAG_USER_REQ;
+		if ((io->io_hdr.io_type == CTL_IO_SCSI)
+		 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED))
+			io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++;
+
+		retval = ctl_ioctl_submit_wait(io);
+
+		if (retval != 0) {
+			ctl_free_io(io);
+			break;
+		}
+
+		memcpy((void *)addr, io, sizeof(*io));
+
+		/* return this to our pool */
+		ctl_free_io(io);
+
+		break;
+	}
+	case CTL_ENABLE_PORT:
+	case CTL_DISABLE_PORT:
+	case CTL_SET_PORT_WWNS: {
+		struct ctl_frontend *fe;
+		struct ctl_port_entry *entry;
+
+		entry = (struct ctl_port_entry *)addr;
+		
+		mtx_lock(&softc->ctl_lock);
+		STAILQ_FOREACH(fe, &softc->fe_list, links) {
+			int action, done;
+
+			action = 0;
+			done = 0;
+
+			if ((entry->port_type == CTL_PORT_NONE)
+			 && (entry->targ_port == fe->targ_port)) {
+				/*
+				 * If the user only wants to enable or
+				 * disable or set WWNs on a specific port,
+				 * do the operation and we're done.
+				 */
+				action = 1;
+				done = 1;
+			} else if (entry->port_type & fe->port_type) {
+				/*
+				 * Compare the user's type mask with the
+				 * particular frontend type to see if we
+				 * have a match.
+				 */
+				action = 1;
+				done = 0;
+
+				/*
+				 * Make sure the user isn't trying to set
+				 * WWNs on multiple ports at the same time.
+				 */
+				if (cmd == CTL_SET_PORT_WWNS) {
+					printf("%s: Can't set WWNs on "
+					       "multiple ports\n", __func__);
+					retval = EINVAL;
+					break;
+				}
+			}
+			if (action != 0) {
+				/*
+				 * XXX KDM we have to drop the lock here,
+				 * because the online/offline operations
+				 * can potentially block.  We need to
+				 * reference count the frontends so they
+				 * can't go away,
+				 */
+				mtx_unlock(&softc->ctl_lock);
+
+				if (cmd == CTL_ENABLE_PORT)
+					ctl_frontend_online(fe);
+				else if (cmd == CTL_DISABLE_PORT)
+					ctl_frontend_offline(fe);
+
+				mtx_lock(&softc->ctl_lock);
+
+				if (cmd == CTL_SET_PORT_WWNS)
+					ctl_frontend_set_wwns(fe,
+					    (entry->flags & CTL_PORT_WWNN_VALID) ?
+					    1 : 0, entry->wwnn,
+					    (entry->flags & CTL_PORT_WWPN_VALID) ?
+					    1 : 0, entry->wwpn);
+			}
+			if (done != 0)
+				break;
+		}
+		mtx_unlock(&softc->ctl_lock);
+		break;
+	}
+	case CTL_GET_PORT_LIST: {
+		struct ctl_frontend *fe;
+		struct ctl_port_list *list;
+		int i;
+
+		list = (struct ctl_port_list *)addr;
+
+		if (list->alloc_len != (list->alloc_num *
+		    sizeof(struct ctl_port_entry))) {
+			printf("%s: CTL_GET_PORT_LIST: alloc_len %u != "
+			       "alloc_num %u * sizeof(struct ctl_port_entry) "
+			       "%zu\n", __func__, list->alloc_len,
+			       list->alloc_num, sizeof(struct ctl_port_entry));
+			retval = EINVAL;
+			break;
+		}
+		list->fill_len = 0;
+		list->fill_num = 0;
+		list->dropped_num = 0;
+		i = 0;
+		mtx_lock(&softc->ctl_lock);
+		STAILQ_FOREACH(fe, &softc->fe_list, links) {
+			struct ctl_port_entry entry, *list_entry;
+
+			if (list->fill_num >= list->alloc_num) {
+				list->dropped_num++;
+				continue;
+			}
+
+			entry.port_type = fe->port_type;
+			strlcpy(entry.port_name, fe->port_name,
+				sizeof(entry.port_name));
+			entry.targ_port = fe->targ_port;
+			entry.physical_port = fe->physical_port;
+			entry.virtual_port = fe->virtual_port;
+			entry.wwnn = fe->wwnn;
+			entry.wwpn = fe->wwpn;
+			if (fe->status & CTL_PORT_STATUS_ONLINE)
+				entry.online = 1;
+			else
+				entry.online = 0;
+
+			list_entry = &list->entries[i];
+
+			retval = copyout(&entry, list_entry, sizeof(entry));
+			if (retval != 0) {
+				printf("%s: CTL_GET_PORT_LIST: copyout "
+				       "returned %d\n", __func__, retval);
+				break;
+			}
+			i++;
+			list->fill_num++;
+			list->fill_len += sizeof(entry);
+		}
+		mtx_unlock(&softc->ctl_lock);
+
+		/*
+		 * If this is non-zero, we had a copyout fault, so there's
+		 * probably no point in attempting to set the status inside
+		 * the structure.
+		 */
+		if (retval != 0)
+			break;
+
+		if (list->dropped_num > 0)
+			list->status = CTL_PORT_LIST_NEED_MORE_SPACE;
+		else
+			list->status = CTL_PORT_LIST_OK;
+		break;
+	}
+	case CTL_DUMP_OOA: {
+		struct ctl_lun *lun;
+		union ctl_io *io;
+		char printbuf[128];
+		struct sbuf sb;
+
+		mtx_lock(&softc->ctl_lock);
+		printf("Dumping OOA queues:\n");
+		STAILQ_FOREACH(lun, &softc->lun_list, links) {
+			for (io = (union ctl_io *)TAILQ_FIRST(
+			     &lun->ooa_queue); io != NULL;
+			     io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
+			     ooa_links)) {
+				sbuf_new(&sb, printbuf, sizeof(printbuf),
+					 SBUF_FIXEDLEN);
+				sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ",
+					    (intmax_t)lun->lun,
+					    io->scsiio.tag_num,
+					    (io->io_hdr.flags &
+					    CTL_FLAG_BLOCKED) ? "" : " BLOCKED",
+					    (io->io_hdr.flags &
+					    CTL_FLAG_DMA_INPROG) ? " DMA" : "",
+					    (io->io_hdr.flags &
+					    CTL_FLAG_ABORT) ? " ABORT" : "",
+			                    (io->io_hdr.flags &
+		                        CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : "");
+				ctl_scsi_command_string(&io->scsiio, NULL, &sb);
+				sbuf_finish(&sb);
+				printf("%s\n", sbuf_data(&sb));
+			}
+		}
+		printf("OOA queues dump done\n");
+		mtx_unlock(&softc->ctl_lock);
+		break;
+	}
+	case CTL_GET_OOA: {
+		struct ctl_lun *lun;
+		struct ctl_ooa *ooa_hdr;
+		uint32_t cur_fill_num;
+
+		ooa_hdr = (struct ctl_ooa *)addr;
+
+		if ((ooa_hdr->alloc_len == 0)
+		 || (ooa_hdr->alloc_num == 0)) {
+			printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
+			       "must be non-zero\n", __func__,
+			       ooa_hdr->alloc_len, ooa_hdr->alloc_num);
+			retval = EINVAL;
+			break;
+		}
+
+		if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
+		    sizeof(struct ctl_ooa_entry))) {
+			printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
+			       "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
+			       __func__, ooa_hdr->alloc_len,
+			       ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
+			retval = EINVAL;
+			break;
+		}
+
+		mtx_lock(&softc->ctl_lock);
+		if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0)
+		 && ((ooa_hdr->lun_num > CTL_MAX_LUNS)
+		  || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) {
+			mtx_unlock(&softc->ctl_lock);
+			printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
+			       __func__, (uintmax_t)ooa_hdr->lun_num);
+			retval = EINVAL;
+			break;
+		}
+
+		cur_fill_num = 0;
+
+		if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
+			STAILQ_FOREACH(lun, &softc->lun_list, links) {
+				retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,
+					ooa_hdr);
+				if (retval != 0)
+					break;
+			}
+			if (retval != 0) {
+				mtx_unlock(&softc->ctl_lock);
+				break;
+			}
+		} else {
+			lun = softc->ctl_luns[ooa_hdr->lun_num];
+
+			retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr);
+		}
+		mtx_unlock(&softc->ctl_lock);
+
+		ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
+		ooa_hdr->fill_len = ooa_hdr->fill_num *
+			sizeof(struct ctl_ooa_entry);
+
+		getbintime(&ooa_hdr->cur_bt);
+
+		if (cur_fill_num > ooa_hdr->alloc_num) {
+			ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
+			ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
+		} else {
+			ooa_hdr->dropped_num = 0;
+			ooa_hdr->status = CTL_OOA_OK;
+		}
+		break;
+	}
+	case CTL_CHECK_OOA: {
+		union ctl_io *io;
+		struct ctl_lun *lun;
+		struct ctl_ooa_info *ooa_info;
+
+
+		ooa_info = (struct ctl_ooa_info *)addr;
+
+		if (ooa_info->lun_id >= CTL_MAX_LUNS) {
+			ooa_info->status = CTL_OOA_INVALID_LUN;
+			break;
+		}
+		mtx_lock(&softc->ctl_lock);
+		lun = softc->ctl_luns[ooa_info->lun_id];
+		if (lun == NULL) {
+			mtx_unlock(&softc->ctl_lock);
+			ooa_info->status = CTL_OOA_INVALID_LUN;
+			break;
+		}
+
+		ooa_info->num_entries = 0;
+		for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
+		     io != NULL; io = (union ctl_io *)TAILQ_NEXT(
+		     &io->io_hdr, ooa_links)) {
+			ooa_info->num_entries++;
+		}
+
+		mtx_unlock(&softc->ctl_lock);
+		ooa_info->status = CTL_OOA_SUCCESS;
+
+		break;
+	}
+	case CTL_HARD_START:
+	case CTL_HARD_STOP: {
+		struct ctl_fe_ioctl_startstop_info ss_info;
+		struct cfi_metatask *metatask;
+		struct mtx hs_mtx;
+
+		mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF);
+
+		cv_init(&ss_info.sem, "hard start/stop cv" );
+
+		metatask = cfi_alloc_metatask(/*can_wait*/ 1);
+		if (metatask == NULL) {
+			retval = ENOMEM;
+			mtx_destroy(&hs_mtx);
+			break;
+		}
+
+		if (cmd == CTL_HARD_START)
+			metatask->tasktype = CFI_TASK_STARTUP;
+		else
+			metatask->tasktype = CFI_TASK_SHUTDOWN;
+
+		metatask->callback = ctl_ioctl_hard_startstop_callback;
+		metatask->callback_arg = &ss_info;
+
+		cfi_action(metatask);
+
+		/* Wait for the callback */
+		mtx_lock(&hs_mtx);
+		cv_wait_sig(&ss_info.sem, &hs_mtx);
+		mtx_unlock(&hs_mtx);
+
+		/*
+		 * All information has been copied from the metatask by the
+		 * time cv_broadcast() is called, so we free the metatask here.
+		 */
+		cfi_free_metatask(metatask);
+
+		memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info));
+
+		mtx_destroy(&hs_mtx);
+		break;
+	}
+	case CTL_BBRREAD: {
+		struct ctl_bbrread_info *bbr_info;
+		struct ctl_fe_ioctl_bbrread_info fe_bbr_info;
+		struct mtx bbr_mtx;
+		struct cfi_metatask *metatask;
+
+		bbr_info = (struct ctl_bbrread_info *)addr;
+
+		bzero(&fe_bbr_info, sizeof(fe_bbr_info));
+
+		bzero(&bbr_mtx, sizeof(bbr_mtx));
+		mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF);
+
+		fe_bbr_info.bbr_info = bbr_info;
+		fe_bbr_info.lock = &bbr_mtx;
+
+		cv_init(&fe_bbr_info.sem, "BBR read cv");
+		metatask = cfi_alloc_metatask(/*can_wait*/ 1);
+
+		if (metatask == NULL) {
+			mtx_destroy(&bbr_mtx);
+			cv_destroy(&fe_bbr_info.sem);
+			retval = ENOMEM;
+			break;
+		}
+		metatask->tasktype = CFI_TASK_BBRREAD;
+		metatask->callback = ctl_ioctl_bbrread_callback;
+		metatask->callback_arg = &fe_bbr_info;
+		metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num;
+		metatask->taskinfo.bbrread.lba = bbr_info->lba;
+		metatask->taskinfo.bbrread.len = bbr_info->len;
+
+		cfi_action(metatask);
+
+		mtx_lock(&bbr_mtx);
+		while (fe_bbr_info.wakeup_done == 0)
+			cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx);
+		mtx_unlock(&bbr_mtx);
+
+		bbr_info->status = metatask->status;
+		bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
+		bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status;
+		memcpy(&bbr_info->sense_data,
+		       &metatask->taskinfo.bbrread.sense_data,
+		       ctl_min(sizeof(bbr_info->sense_data),
+			       sizeof(metatask->taskinfo.bbrread.sense_data)));
+
+		cfi_free_metatask(metatask);
+
+		mtx_destroy(&bbr_mtx);
+		cv_destroy(&fe_bbr_info.sem);
+
+		break;
+	}
+	case CTL_DELAY_IO: {
+		struct ctl_io_delay_info *delay_info;
+#ifdef CTL_IO_DELAY
+		struct ctl_lun *lun;
+#endif /* CTL_IO_DELAY */
+
+		delay_info = (struct ctl_io_delay_info *)addr;
+
+#ifdef CTL_IO_DELAY
+		mtx_lock(&softc->ctl_lock);
+
+		if ((delay_info->lun_id > CTL_MAX_LUNS)
+		 || (softc->ctl_luns[delay_info->lun_id] == NULL)) {
+			delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
+		} else {
+			lun = softc->ctl_luns[delay_info->lun_id];
+
+			delay_info->status = CTL_DELAY_STATUS_OK;
+
+			switch (delay_info->delay_type) {
+			case CTL_DELAY_TYPE_CONT:
+				break;
+			case CTL_DELAY_TYPE_ONESHOT:
+				break;
+			default:
+				delay_info->status =
+					CTL_DELAY_STATUS_INVALID_TYPE;
+				break;
+			}
+
+			switch (delay_info->delay_loc) {
+			case CTL_DELAY_LOC_DATAMOVE:
+				lun->delay_info.datamove_type =
+					delay_info->delay_type;
+				lun->delay_info.datamove_delay =
+					delay_info->delay_secs;
+				break;
+			case CTL_DELAY_LOC_DONE:
+				lun->delay_info.done_type =
+					delay_info->delay_type;
+				lun->delay_info.done_delay =
+					delay_info->delay_secs;
+				break;
+			default:
+				delay_info->status =
+					CTL_DELAY_STATUS_INVALID_LOC;
+				break;
+			}
+		}
+
+		mtx_unlock(&softc->ctl_lock);
+#else
+		delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
+#endif /* CTL_IO_DELAY */
+		break;
+	}
+	case CTL_REALSYNC_SET: {
+		int *syncstate;
+
+		syncstate = (int *)addr;
+
+		mtx_lock(&softc->ctl_lock);
+		switch (*syncstate) {
+		case 0:
+			softc->flags &= ~CTL_FLAG_REAL_SYNC;
+			break;
+		case 1:
+			softc->flags |= CTL_FLAG_REAL_SYNC;
+			break;
+		default:
+			retval = -EINVAL;
+			break;
+		}
+		mtx_unlock(&softc->ctl_lock);
+		break;
+	}
+	case CTL_REALSYNC_GET: {
+		int *syncstate;
+
+		syncstate = (int*)addr;
+
+		mtx_lock(&softc->ctl_lock);
+		if (softc->flags & CTL_FLAG_REAL_SYNC)
+			*syncstate = 1;
+		else
+			*syncstate = 0;
+		mtx_unlock(&softc->ctl_lock);
+
+		break;
+	}
+	case CTL_SETSYNC:
+	case CTL_GETSYNC: {
+		struct ctl_sync_info *sync_info;
+		struct ctl_lun *lun;
+
+		sync_info = (struct ctl_sync_info *)addr;
+
+		mtx_lock(&softc->ctl_lock);
+		lun = softc->ctl_luns[sync_info->lun_id];
+		if (lun == NULL) {
+			mtx_unlock(&softc->ctl_lock);
+			sync_info->status = CTL_GS_SYNC_NO_LUN;
+		}
+		/*
+		 * Get or set the sync interval.  We're not bounds checking
+		 * in the set case, hopefully the user won't do something
+		 * silly.
+		 */
+		if (cmd == CTL_GETSYNC)
+			sync_info->sync_interval = lun->sync_interval;
+		else
+			lun->sync_interval = sync_info->sync_interval;
+
+		mtx_unlock(&softc->ctl_lock);
+
+		sync_info->status = CTL_GS_SYNC_OK;
+
+		break;
+	}
+	case CTL_GETSTATS: {
+		struct ctl_stats *stats;
+		struct ctl_lun *lun;
+		int i;
+
+		stats = (struct ctl_stats *)addr;
+
+		if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) >
+		     stats->alloc_len) {
+			stats->status = CTL_SS_NEED_MORE_SPACE;
+			stats->num_luns = softc->num_luns;
+			break;
+		}
+		/*
+		 * XXX KDM no locking here.  If the LUN list changes,
+		 * things can blow up.
+		 */
+		for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL;
+		     i++, lun = STAILQ_NEXT(lun, links)) {
+			retval = copyout(&lun->stats, &stats->lun_stats[i],
+					 sizeof(lun->stats));
+			if (retval != 0)
+				break;
+		}
+		stats->num_luns = softc->num_luns;
+		stats->fill_len = sizeof(struct ctl_lun_io_stats) *
+				 softc->num_luns;
+		stats->status = CTL_SS_OK;
+#ifdef CTL_TIME_IO
+		stats->flags = CTL_STATS_FLAG_TIME_VALID;
+#else
+		stats->flags = CTL_STATS_FLAG_NONE;
+#endif
+		getnanouptime(&stats->timestamp);
+		break;
+	}
+	case CTL_ERROR_INJECT: {
+		struct ctl_error_desc *err_desc, *new_err_desc;
+		struct ctl_lun *lun;
+
+		err_desc = (struct ctl_error_desc *)addr;
+
+		new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
+				      M_WAITOK | M_ZERO);
+		if (new_err_desc == NULL) {
+			printf("%s: CTL_ERROR_INJECT: error allocating %zu "
+			       "bytes\n", __func__, sizeof(*new_err_desc));
+			retval = ENOMEM;
+			break;
+		}
+		bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
+
+		mtx_lock(&softc->ctl_lock);
+		lun = softc->ctl_luns[err_desc->lun_id];
+		if (lun == NULL) {
+			mtx_unlock(&softc->ctl_lock);
+			printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
+			       __func__, (uintmax_t)err_desc->lun_id);
+			retval = EINVAL;
+			break;
+		}
+
+		/*
+		 * We could do some checking here to verify the validity
+		 * of the request, but given the complexity of error
+		 * injection requests, the checking logic would be fairly
+		 * complex.
+		 *
+		 * For now, if the request is invalid, it just won't get
+		 * executed and might get deleted.
+		 */
+		STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
+
+		/*
+		 * XXX KDM check to make sure the serial number is unique,
+		 * in case we somehow manage to wrap.  That shouldn't
+		 * happen for a very long time, but it's the right thing to
+		 * do.
+		 */
+		new_err_desc->serial = lun->error_serial;
+		err_desc->serial = lun->error_serial;
+		lun->error_serial++;
+
+		mtx_unlock(&softc->ctl_lock);
+		break;
+	}
+	case CTL_ERROR_INJECT_DELETE: {
+		struct ctl_error_desc *delete_desc, *desc, *desc2;
+		struct ctl_lun *lun;
+		int delete_done;
+
+		delete_desc = (struct ctl_error_desc *)addr;
+		delete_done = 0;
+
+		mtx_lock(&softc->ctl_lock);
+		lun = softc->ctl_luns[delete_desc->lun_id];
+		if (lun == NULL) {
+			mtx_unlock(&softc->ctl_lock);
+			printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
+			       __func__, (uintmax_t)delete_desc->lun_id);
+			retval = EINVAL;
+			break;
+		}
+		STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
+			if (desc->serial != delete_desc->serial)
+				continue;
+
+			STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
+				      links);
+			free(desc, M_CTL);
+			delete_done = 1;
+		}
+		mtx_unlock(&softc->ctl_lock);
+		if (delete_done == 0) {
+			printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
+			       "error serial %ju on LUN %u\n", __func__, 
+			       delete_desc->serial, delete_desc->lun_id);
+			retval = EINVAL;
+			break;
+		}
+		break;
+	}
+	case CTL_DUMP_STRUCTS: {
+		int i, j, k;
+		struct ctl_frontend *fe;
+
+		printf("CTL IID to WWPN map start:\n");
+		for (i = 0; i < CTL_MAX_PORTS; i++) {
+			for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
+				if (softc->wwpn_iid[i][j].in_use == 0)
+					continue;
+
+				printf("port %d iid %u WWPN %#jx\n",
+				       softc->wwpn_iid[i][j].port,
+				       softc->wwpn_iid[i][j].iid, 
+				       (uintmax_t)softc->wwpn_iid[i][j].wwpn);
+			}
+		}
+		printf("CTL IID to WWPN map end\n");
+		printf("CTL Persistent Reservation information start:\n");
+		for (i = 0; i < CTL_MAX_LUNS; i++) {
+			struct ctl_lun *lun;
+
+			lun = softc->ctl_luns[i];
+
+			if ((lun == NULL)
+			 || ((lun->flags & CTL_LUN_DISABLED) != 0))
+				continue;
+
+			for (j = 0; j < (CTL_MAX_PORTS * 2); j++) {
+				for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
+					if (lun->per_res[j+k].registered == 0)
+						continue;
+					printf("LUN %d port %d iid %d key "
+					       "%#jx\n", i, j, k,
+					       (uintmax_t)scsi_8btou64(
+					       lun->per_res[j+k].res_key.key));
+				}
+			}
+		}
+		printf("CTL Persistent Reservation information end\n");
+		printf("CTL Frontends:\n");
+		/*
+		 * XXX KDM calling this without a lock.  We'd likely want
+		 * to drop the lock before calling the frontend's dump
+		 * routine anyway.
+		 */
+		STAILQ_FOREACH(fe, &softc->fe_list, links) {
+			printf("Frontend %s Type %u pport %d vport %d WWNN "
+			       "%#jx WWPN %#jx\n", fe->port_name, fe->port_type,
+			       fe->physical_port, fe->virtual_port,
+			       (uintmax_t)fe->wwnn, (uintmax_t)fe->wwpn);
+
+			/*
+			 * Frontends are not required to support the dump
+			 * routine.
+			 */
+			if (fe->fe_dump == NULL)
+				continue;
+
+			fe->fe_dump();
+		}
+		printf("CTL Frontend information end\n");
+		break;
+	}
+	case CTL_LUN_REQ: {
+		struct ctl_lun_req *lun_req;
+		struct ctl_backend_driver *backend;
+
+		lun_req = (struct ctl_lun_req *)addr;
+
+		backend = ctl_backend_find(lun_req->backend);
+		if (backend == NULL) {
+			lun_req->status = CTL_LUN_ERROR;
+			snprintf(lun_req->error_str,
+				 sizeof(lun_req->error_str),
+				 "Backend \"%s\" not found.",
+				 lun_req->backend);
+			break;
+		}
+		if (lun_req->num_be_args > 0) {
+			lun_req->kern_be_args = ctl_copyin_args(
+				lun_req->num_be_args,
+				lun_req->be_args,
+				lun_req->error_str,
+				sizeof(lun_req->error_str));
+			if (lun_req->kern_be_args == NULL) {
+				lun_req->status = CTL_LUN_ERROR;
+				break;
+			}
+		}
+
+		retval = backend->ioctl(dev, cmd, addr, flag, td);
+
+		if (lun_req->num_be_args > 0) {
+			ctl_free_args(lun_req->num_be_args,
+				      lun_req->kern_be_args);
+		}
+		break;
+	}
+	case CTL_LUN_LIST: {
+		struct sbuf *sb;
+		struct ctl_lun *lun;
+		struct ctl_lun_list *list;
+
+		list = (struct ctl_lun_list *)addr;
+
+		/*
+		 * Allocate a fixed length sbuf here, based on the length
+		 * of the user's buffer.  We could allocate an auto-extending
+		 * buffer, and then tell the user how much larger our
+		 * amount of data is than his buffer, but that presents
+		 * some problems:
+		 *
+		 * 1.  The sbuf(9) routines use a blocking malloc, and so
+		 *     we can't hold a lock while calling them with an
+		 *     auto-extending buffer.
+ 		 *
+		 * 2.  There is not currently a LUN reference counting
+		 *     mechanism, outside of outstanding transactions on
+		 *     the LUN's OOA queue.  So a LUN could go away on us
+		 *     while we're getting the LUN number, backend-specific
+		 *     information, etc.  Thus, given the way things
+		 *     currently work, we need to hold the CTL lock while
+		 *     grabbing LUN information.
+		 *
+		 * So, from the user's standpoint, the best thing to do is
+		 * allocate what he thinks is a reasonable buffer length,
+		 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
+		 * double the buffer length and try again.  (And repeat
+		 * that until he succeeds.)
+		 */
+		sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
+		if (sb == NULL) {
+			list->status = CTL_LUN_LIST_ERROR;
+			snprintf(list->error_str, sizeof(list->error_str),
+				 "Unable to allocate %d bytes for LUN list",
+				 list->alloc_len);
+			break;
+		}
+
+		sbuf_printf(sb, "<ctllunlist>\n");
+
+		mtx_lock(&softc->ctl_lock);
+
+		STAILQ_FOREACH(lun, &softc->lun_list, links) {
+			retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
+					     (uintmax_t)lun->lun);
+
+			/*
+			 * Bail out as soon as we see that we've overfilled
+			 * the buffer.
+			 */
+			if (retval != 0)
+				break;
+
+			retval = sbuf_printf(sb, "<backend_type>%s"
+					     "</backend_type>\n",
+					     (lun->backend == NULL) ?  "none" :
+					     lun->backend->name);
+
+			if (retval != 0)
+				break;
+
+			retval = sbuf_printf(sb, "<lun_type>%d</lun_type>\n",
+					     lun->be_lun->lun_type);
+
+			if (retval != 0)
+				break;
+
+			if (lun->backend == NULL) {
+				retval = sbuf_printf(sb, "</lun>\n");
+				if (retval != 0)
+					break;
+				continue;
+			}
+
+			retval = sbuf_printf(sb, "<size>%ju</size>\n",
+					     (lun->be_lun->maxlba > 0) ?
+					     lun->be_lun->maxlba + 1 : 0);
+
+			if (retval != 0)
+				break;
+
+			retval = sbuf_printf(sb, "<blocksize>%u</blocksize>\n",
+					     lun->be_lun->blocksize);
+
+			if (retval != 0)
+				break;
+
+			retval = sbuf_printf(sb, "<serial_number>");
+
+			if (retval != 0)
+				break;
+
+			retval = ctl_sbuf_printf_esc(sb,
+						     lun->be_lun->serial_num);
+
+			if (retval != 0)
+				break;
+
+			retval = sbuf_printf(sb, "</serial_number>\n");
+		
+			if (retval != 0)
+				break;
+
+			retval = sbuf_printf(sb, "<device_id>");
+
+			if (retval != 0)
+				break;
+
+			retval = ctl_sbuf_printf_esc(sb,lun->be_lun->device_id);
+
+			if (retval != 0)
+				break;
+
+			retval = sbuf_printf(sb, "</device_id>\n");
+
+			if (retval != 0)
+				break;
+
+			if (lun->backend->lun_info == NULL) {
+				retval = sbuf_printf(sb, "</lun>\n");
+				if (retval != 0)
+					break;
+				continue;
+			}
+
+			retval =lun->backend->lun_info(lun->be_lun->be_lun, sb);
+
+			if (retval != 0)
+				break;
+
+			retval = sbuf_printf(sb, "</lun>\n");
+
+			if (retval != 0)
+				break;
+		}
+		mtx_unlock(&softc->ctl_lock);
+
+		if ((retval != 0)
+		 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
+			retval = 0;
+			sbuf_delete(sb);
+			list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
+			snprintf(list->error_str, sizeof(list->error_str),
+				 "Out of space, %d bytes is too small",
+				 list->alloc_len);
+			break;
+		}
+
+		sbuf_finish(sb);
+
+		retval = copyout(sbuf_data(sb), list->lun_xml,
+				 sbuf_len(sb) + 1);
+
+		list->fill_len = sbuf_len(sb) + 1;
+		list->status = CTL_LUN_LIST_OK;
+		sbuf_delete(sb);
+		break;
+	}
+	default: {
+		/* XXX KDM should we fix this? */
+#if 0
+		struct ctl_backend_driver *backend;
+		unsigned int type;
+		int found;
+
+		found = 0;
+
+		/*
+		 * We encode the backend type as the ioctl type for backend
+		 * ioctls.  So parse it out here, and then search for a
+		 * backend of this type.
+		 */
+		type = _IOC_TYPE(cmd);
+
+		STAILQ_FOREACH(backend, &softc->be_list, links) {
+			if (backend->type == type) {
+				found = 1;
+				break;
+			}
+		}
+		if (found == 0) {
+			printf("ctl: unknown ioctl command %#lx or backend "
+			       "%d\n", cmd, type);
+			retval = -EINVAL;
+			break;
+		}
+		retval = backend->ioctl(dev, cmd, addr, flag, td);
+#endif
+		retval = ENOTTY;
+		break;
+	}
+	}
+	return (retval);
+}
+
+uint32_t
+ctl_get_initindex(struct ctl_nexus *nexus)
+{
+	if (nexus->targ_port < CTL_MAX_PORTS)
+		return (nexus->initid.id +
+			(nexus->targ_port * CTL_MAX_INIT_PER_PORT));
+	else
+		return (nexus->initid.id +
+		       ((nexus->targ_port - CTL_MAX_PORTS) *
+			CTL_MAX_INIT_PER_PORT));
+}
+
+uint32_t
+ctl_get_resindex(struct ctl_nexus *nexus)
+{
+	return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
+}
+
+uint32_t
+ctl_port_idx(int port_num)
+{
+	if (port_num < CTL_MAX_PORTS)
+		return(port_num);
+	else
+		return(port_num - CTL_MAX_PORTS);
+}
+
+/*
+ * Note:  This only works for bitmask sizes that are at least 32 bits, and
+ * that are a power of 2.
+ */
+int
+ctl_ffz(uint32_t *mask, uint32_t size)
+{
+	uint32_t num_chunks, num_pieces;
+	int i, j;
+
+	num_chunks = (size >> 5);
+	if (num_chunks == 0)
+		num_chunks++;
+	num_pieces = ctl_min((sizeof(uint32_t) * 8), size);
+
+	for (i = 0; i < num_chunks; i++) {
+		for (j = 0; j < num_pieces; j++) {
+			if ((mask[i] & (1 << j)) == 0)
+				return ((i << 5) + j);
+		}
+	}
+
+	return (-1);
+}
+
+int
+ctl_set_mask(uint32_t *mask, uint32_t bit)
+{
+	uint32_t chunk, piece;
+
+	chunk = bit >> 5;
+	piece = bit % (sizeof(uint32_t) * 8);
+
+	if ((mask[chunk] & (1 << piece)) != 0)
+		return (-1);
+	else
+		mask[chunk] |= (1 << piece);
+
+	return (0);
+}
+
+int
+ctl_clear_mask(uint32_t *mask, uint32_t bit)
+{
+	uint32_t chunk, piece;
+
+	chunk = bit >> 5;
+	piece = bit % (sizeof(uint32_t) * 8);
+
+	if ((mask[chunk] & (1 << piece)) == 0)
+		return (-1);
+	else
+		mask[chunk] &= ~(1 << piece);
+
+	return (0);
+}
+
+int
+ctl_is_set(uint32_t *mask, uint32_t bit)
+{
+	uint32_t chunk, piece;
+
+	chunk = bit >> 5;
+	piece = bit % (sizeof(uint32_t) * 8);
+
+	if ((mask[chunk] & (1 << piece)) == 0)
+		return (0);
+	else
+		return (1);
+}
+
+#ifdef unused
+/*
+ * The bus, target and lun are optional, they can be filled in later.
+ * can_wait is used to determine whether we can wait on the malloc or not.
+ */
+union ctl_io*
+ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, uint32_t targ_target,
+	      uint32_t targ_lun, int can_wait)
+{
+	union ctl_io *io;
+
+	if (can_wait)
+		io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_WAITOK);
+	else
+		io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT);
+
+	if (io != NULL) {
+		io->io_hdr.io_type = io_type;
+		io->io_hdr.targ_port = targ_port;
+		/*
+		 * XXX KDM this needs to change/go away.  We need to move
+		 * to a preallocated pool of ctl_scsiio structures.
+		 */
+		io->io_hdr.nexus.targ_target.id = targ_target;
+		io->io_hdr.nexus.targ_lun = targ_lun;
+	}
+
+	return (io);
+}
+
+void
+ctl_kfree_io(union ctl_io *io)
+{
+	free(io, M_CTL);
+}
+#endif /* unused */
+
+/*
+ * ctl_softc, pool_type, total_ctl_io are passed in.
+ * npool is passed out.
+ */
+int
+ctl_pool_create(struct ctl_softc *ctl_softc, ctl_pool_type pool_type,
+		uint32_t total_ctl_io, struct ctl_io_pool **npool)
+{
+	uint32_t i;
+	union ctl_io *cur_io, *next_io;
+	struct ctl_io_pool *pool;
+	int retval;
+
+	retval = 0;
+
+	pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, M_NOWAIT);
+	if (pool == NULL) {
+		retval = -ENOMEM;
+		goto bailout;
+	}
+
+	memset(pool, 0, sizeof(*pool));
+
+	pool->type = pool_type;
+	pool->ctl_softc = ctl_softc;
+
+	mtx_lock(&ctl_softc->ctl_lock);
+	pool->id = ctl_softc->cur_pool_id++;
+	mtx_unlock(&ctl_softc->ctl_lock);
+
+	pool->flags = CTL_POOL_FLAG_NONE;
+	STAILQ_INIT(&pool->free_queue);
+
+	/*
+	 * XXX KDM other options here:
+	 * - allocate a page at a time
+	 * - allocate one big chunk of memory.
+	 * Page allocation might work well, but would take a little more
+	 * tracking.
+	 */
+	for (i = 0; i < total_ctl_io; i++) {
+		cur_io = (union ctl_io *)malloc(sizeof(*cur_io), M_CTL,
+						M_NOWAIT);
+		if (cur_io == NULL) {
+			retval = ENOMEM;
+			break;
+		}
+		cur_io->io_hdr.pool = pool;
+		STAILQ_INSERT_TAIL(&pool->free_queue, &cur_io->io_hdr, links);
+		pool->total_ctl_io++;
+		pool->free_ctl_io++;
+	}
+
+	if (retval != 0) {
+		for (cur_io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue);
+		     cur_io != NULL; cur_io = next_io) {
+			next_io = (union ctl_io *)STAILQ_NEXT(&cur_io->io_hdr,
+							      links);
+			STAILQ_REMOVE(&pool->free_queue, &cur_io->io_hdr,
+				      ctl_io_hdr, links);
+			free(cur_io, M_CTL);
+		}
+
+		free(pool, M_CTL);
+		goto bailout;
+	}
+	mtx_lock(&ctl_softc->ctl_lock);
+	ctl_softc->num_pools++;
+	STAILQ_INSERT_TAIL(&ctl_softc->io_pools, pool, links);
+	/*
+	 * Increment our usage count if this is an external consumer, so we
+	 * can't get unloaded until the external consumer (most likely a
+	 * FETD) unloads and frees his pool.
+	 *
+	 * XXX KDM will this increment the caller's module use count, or
+	 * mine?
+	 */
+#if 0
+	if ((pool_type != CTL_POOL_EMERGENCY)
+	 && (pool_type != CTL_POOL_INTERNAL)
+	 && (pool_type != CTL_POOL_IOCTL)
+	 && (pool_type != CTL_POOL_4OTHERSC))
+		MOD_INC_USE_COUNT;
+#endif
+
+	mtx_unlock(&ctl_softc->ctl_lock);
+
+	*npool = pool;
+
+bailout:
+
+	return (retval);
+}
+
+/*
+ * Caller must hold ctl_softc->ctl_lock.
+ */
+int
+ctl_pool_acquire(struct ctl_io_pool *pool)
+{
+	if (pool == NULL)
+		return (-EINVAL);
+
+	if (pool->flags & CTL_POOL_FLAG_INVALID)
+		return (-EINVAL);
+
+	pool->refcount++;
+
+	return (0);
+}
+
+/*
+ * Caller must hold ctl_softc->ctl_lock.
+ */
+int
+ctl_pool_invalidate(struct ctl_io_pool *pool)
+{
+	if (pool == NULL)
+		return (-EINVAL);
+
+	pool->flags |= CTL_POOL_FLAG_INVALID;
+
+	return (0);
+}
+
+/*
+ * Caller must hold ctl_softc->ctl_lock.
+ */
+int
+ctl_pool_release(struct ctl_io_pool *pool)
+{
+	if (pool == NULL)
+		return (-EINVAL);
+
+	if ((--pool->refcount == 0)
+	 && (pool->flags & CTL_POOL_FLAG_INVALID)) {
+		ctl_pool_free(pool->ctl_softc, pool);
+	}
+
+	return (0);
+}
+
+/*
+ * Must be called with ctl_softc->ctl_lock held.
+ */
+void
+ctl_pool_free(struct ctl_softc *ctl_softc, struct ctl_io_pool *pool)
+{
+	union ctl_io *cur_io, *next_io;
+
+	for (cur_io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue);
+	     cur_io != NULL; cur_io = next_io) {
+		next_io = (union ctl_io *)STAILQ_NEXT(&cur_io->io_hdr,
+						      links);
+		STAILQ_REMOVE(&pool->free_queue, &cur_io->io_hdr, ctl_io_hdr,
+			      links);
+		free(cur_io, M_CTL);
+	}
+
+	STAILQ_REMOVE(&ctl_softc->io_pools, pool, ctl_io_pool, links);
+	ctl_softc->num_pools--;
+
+	/*
+	 * XXX KDM will this decrement the caller's usage count or mine?
+	 */
+#if 0
+	if ((pool->type != CTL_POOL_EMERGENCY)
+	 && (pool->type != CTL_POOL_INTERNAL)
+	 && (pool->type != CTL_POOL_IOCTL))
+		MOD_DEC_USE_COUNT;
+#endif
+
+	free(pool, M_CTL);
+}
+
+/*
+ * This routine does not block (except for spinlocks of course).
+ * It tries to allocate a ctl_io union from the caller's pool as quickly as
+ * possible.
+ */
+union ctl_io *
+ctl_alloc_io(void *pool_ref)
+{
+	union ctl_io *io;
+	struct ctl_softc *ctl_softc;
+	struct ctl_io_pool *pool, *npool;
+	struct ctl_io_pool *emergency_pool;
+
+	pool = (struct ctl_io_pool *)pool_ref;
+
+	if (pool == NULL) {
+		printf("%s: pool is NULL\n", __func__);
+		return (NULL);
+	}
+
+	emergency_pool = NULL;
+
+	ctl_softc = pool->ctl_softc;
+
+	mtx_lock(&ctl_softc->ctl_lock);
+	/*
+	 * First, try to get the io structure from the user's pool.
+	 */
+	if (ctl_pool_acquire(pool) == 0) {
+		io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue);
+		if (io != NULL) {
+			STAILQ_REMOVE_HEAD(&pool->free_queue, links);
+			pool->total_allocated++;
+			pool->free_ctl_io--;
+			mtx_unlock(&ctl_softc->ctl_lock);
+			return (io);
+		} else
+			ctl_pool_release(pool);
+	}
+	/*
+	 * If he doesn't have any io structures left, search for an
+	 * emergency pool and grab one from there.
+	 */
+	STAILQ_FOREACH(npool, &ctl_softc->io_pools, links) {
+		if (npool->type != CTL_POOL_EMERGENCY)
+			continue;
+
+		if (ctl_pool_acquire(npool) != 0)
+			continue;
+
+		emergency_pool = npool;
+
+		io = (union ctl_io *)STAILQ_FIRST(&npool->free_queue);
+		if (io != NULL) {
+			STAILQ_REMOVE_HEAD(&npool->free_queue, links);
+			npool->total_allocated++;
+			npool->free_ctl_io--;
+			mtx_unlock(&ctl_softc->ctl_lock);
+			return (io);
+		} else
+			ctl_pool_release(npool);
+	}
+
+	/* Drop the spinlock before we malloc */
+	mtx_unlock(&ctl_softc->ctl_lock);
+
+	/*
+	 * The emergency pool (if it exists) didn't have one, so try an
+	 * atomic (i.e. nonblocking) malloc and see if we get lucky.
+	 */
+	io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT);
+	if (io != NULL) {
+		/*
+		 * If the emergency pool exists but is empty, add this
+		 * ctl_io to its list when it gets freed.
+		 */
+		if (emergency_pool != NULL) {
+			mtx_lock(&ctl_softc->ctl_lock);
+			if (ctl_pool_acquire(emergency_pool) == 0) {
+				io->io_hdr.pool = emergency_pool;
+				emergency_pool->total_ctl_io++;
+				/*
+				 * Need to bump this, otherwise
+				 * total_allocated and total_freed won't
+				 * match when we no longer have anything
+				 * outstanding.
+				 */
+				emergency_pool->total_allocated++;
+			}
+			mtx_unlock(&ctl_softc->ctl_lock);
+		} else
+			io->io_hdr.pool = NULL;
+	}
+
+	return (io);
+}
+
+static void
+ctl_free_io_internal(union ctl_io *io, int have_lock)
+{
+	if (io == NULL)
+		return;
+
+	/*
+	 * If this ctl_io has a pool, return it to that pool.
+	 */
+	if (io->io_hdr.pool != NULL) {
+		struct ctl_io_pool *pool;
+#if 0
+		struct ctl_softc *ctl_softc;
+		union ctl_io *tmp_io;
+		unsigned long xflags;
+		int i;
+
+		ctl_softc = control_softc;
+#endif
+
+		pool = (struct ctl_io_pool *)io->io_hdr.pool;
+
+		if (have_lock == 0)
+			mtx_lock(&pool->ctl_softc->ctl_lock);
+#if 0
+		save_flags(xflags);
+
+		for (i = 0, tmp_io = (union ctl_io *)STAILQ_FIRST(
+		     &ctl_softc->task_queue); tmp_io != NULL; i++,
+		     tmp_io = (union ctl_io *)STAILQ_NEXT(&tmp_io->io_hdr,
+		     links)) {
+			if (tmp_io == io) {
+				printf("%s: %p is still on the task queue!\n",
+				       __func__, tmp_io);
+				printf("%s: (%d): type %d "
+				       "msg %d cdb %x iptl: "
+				       "%d:%d:%d:%d tag 0x%04x "
+				       "flg %#lx\n",
+					__func__, i,
+					tmp_io->io_hdr.io_type,
+					tmp_io->io_hdr.msg_type,
+					tmp_io->scsiio.cdb[0],
+					tmp_io->io_hdr.nexus.initid.id,
+					tmp_io->io_hdr.nexus.targ_port,
+					tmp_io->io_hdr.nexus.targ_target.id,
+					tmp_io->io_hdr.nexus.targ_lun,
+					(tmp_io->io_hdr.io_type ==
+					CTL_IO_TASK) ?
+					tmp_io->taskio.tag_num :
+					tmp_io->scsiio.tag_num,
+					xflags);
+				panic("I/O still on the task queue!");
+			}
+		}
+#endif
+		io->io_hdr.io_type = 0xff;
+		STAILQ_INSERT_TAIL(&pool->free_queue, &io->io_hdr, links);
+		pool->total_freed++;
+		pool->free_ctl_io++;
+		ctl_pool_release(pool);
+		if (have_lock == 0)
+			mtx_unlock(&pool->ctl_softc->ctl_lock);
+	} else {
+		/*
+		 * Otherwise, just free it.  We probably malloced it and
+		 * the emergency pool wasn't available.
+		 */
+		free(io, M_CTL);
+	}
+
+}
+
+void
+ctl_free_io(union ctl_io *io)
+{
+	ctl_free_io_internal(io, /*have_lock*/ 0);
+}
+
+void
+ctl_zero_io(union ctl_io *io)
+{
+	void *pool_ref;
+
+	if (io == NULL)
+		return;
+
+	/*
+	 * May need to preserve linked list pointers at some point too.
+	 */
+	pool_ref = io->io_hdr.pool;
+
+	memset(io, 0, sizeof(*io));
+
+	io->io_hdr.pool = pool_ref;
+}
+
+/*
+ * This routine is currently used for internal copies of ctl_ios that need
+ * to persist for some reason after we've already returned status to the
+ * FETD.  (Thus the flag set.)
+ *
+ * XXX XXX
+ * Note that this makes a blind copy of all fields in the ctl_io, except
+ * for the pool reference.  This includes any memory that has been
+ * allocated!  That memory will no longer be valid after done has been
+ * called, so this would be VERY DANGEROUS for command that actually does
+ * any reads or writes.  Right now (11/7/2005), this is only used for immediate
+ * start and stop commands, which don't transfer any data, so this is not a
+ * problem.  If it is used for anything else, the caller would also need to
+ * allocate data buffer space and this routine would need to be modified to
+ * copy the data buffer(s) as well.
+ */
+void
+ctl_copy_io(union ctl_io *src, union ctl_io *dest)
+{
+	void *pool_ref;
+
+	if ((src == NULL)
+	 || (dest == NULL))
+		return;
+
+	/*
+	 * May need to preserve linked list pointers at some point too.
+	 */
+	pool_ref = dest->io_hdr.pool;
+
+	memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest)));
+
+	dest->io_hdr.pool = pool_ref;
+	/*
+	 * We need to know that this is an internal copy, and doesn't need
+	 * to get passed back to the FETD that allocated it.
+	 */
+	dest->io_hdr.flags |= CTL_FLAG_INT_COPY;
+}
+
+#ifdef NEEDTOPORT
+static void
+ctl_update_power_subpage(struct copan_power_subpage *page)
+{
+	int num_luns, num_partitions, config_type;
+	struct ctl_softc *softc;
+	cs_BOOL_t aor_present, shelf_50pct_power;
+	cs_raidset_personality_t rs_type;
+	int max_active_luns;
+
+	softc = control_softc;
+
+	/* subtract out the processor LUN */
+	num_luns = softc->num_luns - 1;
+	/*
+	 * Default to 7 LUNs active, which was the only number we allowed
+	 * in the past.
+	 */
+	max_active_luns = 7;
+
+	num_partitions = config_GetRsPartitionInfo();
+	config_type = config_GetConfigType();
+	shelf_50pct_power = config_GetShelfPowerMode();
+	aor_present = config_IsAorRsPresent();
+
+	rs_type = ddb_GetRsRaidType(1);
+	if ((rs_type != CS_RAIDSET_PERSONALITY_RAID5)
+	 && (rs_type != CS_RAIDSET_PERSONALITY_RAID1)) {
+		EPRINT(0, "Unsupported RS type %d!", rs_type);
+	}
+
+
+	page->total_luns = num_luns;
+
+	switch (config_type) {
+	case 40:
+		/*
+		 * In a 40 drive configuration, it doesn't matter what DC
+		 * cards we have, whether we have AOR enabled or not,
+		 * partitioning or not, or what type of RAIDset we have.
+		 * In that scenario, we can power up every LUN we present
+		 * to the user.
+		 */
+		max_active_luns = num_luns;
+
+		break;
+	case 64:
+		if (shelf_50pct_power == CS_FALSE) {
+			/* 25% power */
+			if (aor_present == CS_TRUE) {
+				if (rs_type ==
+				     CS_RAIDSET_PERSONALITY_RAID5) {
+					max_active_luns = 7;
+				} else if (rs_type ==
+					 CS_RAIDSET_PERSONALITY_RAID1){
+					max_active_luns = 14;
+				} else {
+					/* XXX KDM now what?? */
+				}
+			} else {
+				if (rs_type ==
+				     CS_RAIDSET_PERSONALITY_RAID5) {
+					max_active_luns = 8;
+				} else if (rs_type ==
+					 CS_RAIDSET_PERSONALITY_RAID1){
+					max_active_luns = 16;
+				} else {
+					/* XXX KDM now what?? */
+				}
+			}
+		} else {
+			/* 50% power */
+			/*
+			 * With 50% power in a 64 drive configuration, we
+			 * can power all LUNs we present.
+			 */
+			max_active_luns = num_luns;
+		}
+		break;
+	case 112:
+		if (shelf_50pct_power == CS_FALSE) {
+			/* 25% power */
+			if (aor_present == CS_TRUE) {
+				if (rs_type ==
+				     CS_RAIDSET_PERSONALITY_RAID5) {
+					max_active_luns = 7;
+				} else if (rs_type ==
+					 CS_RAIDSET_PERSONALITY_RAID1){
+					max_active_luns = 14;
+				} else {
+					/* XXX KDM now what?? */
+				}
+			} else {
+				if (rs_type ==
+				     CS_RAIDSET_PERSONALITY_RAID5) {
+					max_active_luns = 8;
+				} else if (rs_type ==
+					 CS_RAIDSET_PERSONALITY_RAID1){
+					max_active_luns = 16;
+				} else {
+					/* XXX KDM now what?? */
+				}
+			}
+		} else {
+			/* 50% power */
+			if (aor_present == CS_TRUE) {
+				if (rs_type ==
+				     CS_RAIDSET_PERSONALITY_RAID5) {
+					max_active_luns = 14;
+				} else if (rs_type ==
+					 CS_RAIDSET_PERSONALITY_RAID1){
+					/*
+					 * We're assuming here that disk
+					 * caching is enabled, and so we're
+					 * able to power up half of each
+					 * LUN, and cache all writes.
+					 */
+					max_active_luns = num_luns;
+				} else {
+					/* XXX KDM now what?? */
+				}
+			} else {
+				if (rs_type ==
+				     CS_RAIDSET_PERSONALITY_RAID5) {
+					max_active_luns = 15;
+				} else if (rs_type ==
+					 CS_RAIDSET_PERSONALITY_RAID1){
+					max_active_luns = 30;
+				} else {
+					/* XXX KDM now what?? */
+				}
+			}
+		}
+		break;
+	default:
+		/*
+		 * In this case, we have an unknown configuration, so we
+		 * just use the default from above.
+		 */
+		break;
+	}
+
+	page->max_active_luns = max_active_luns;
+#if 0
+	printk("%s: total_luns = %d, max_active_luns = %d\n", __func__,
+	       page->total_luns, page->max_active_luns);
+#endif
+}
+#endif /* NEEDTOPORT */
+
+/*
+ * This routine could be used in the future to load default and/or saved
+ * mode page parameters for a particuar lun.
+ */
+static int
+ctl_init_page_index(struct ctl_lun *lun)
+{
+	int i;
+	struct ctl_page_index *page_index;
+	struct ctl_softc *softc;
+
+	memcpy(&lun->mode_pages.index, page_index_template,
+	       sizeof(page_index_template));
+
+	softc = lun->ctl_softc;
+
+	for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
+
+		page_index = &lun->mode_pages.index[i];
+		/*
+		 * If this is a disk-only mode page, there's no point in
+		 * setting it up.  For some pages, we have to have some
+		 * basic information about the disk in order to calculate the
+		 * mode page data.
+		 */
+		if ((lun->be_lun->lun_type != T_DIRECT)
+		 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
+			continue;
+
+		switch (page_index->page_code & SMPH_PC_MASK) {
+		case SMS_FORMAT_DEVICE_PAGE: {
+			struct scsi_format_page *format_page;
+
+			if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
+				panic("subpage is incorrect!");
+
+			/*
+			 * Sectors per track are set above.  Bytes per
+			 * sector need to be set here on a per-LUN basis.
+			 */
+			memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
+			       &format_page_default,
+			       sizeof(format_page_default));
+			memcpy(&lun->mode_pages.format_page[
+			       CTL_PAGE_CHANGEABLE], &format_page_changeable,
+			       sizeof(format_page_changeable));
+			memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
+			       &format_page_default,
+			       sizeof(format_page_default));
+			memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
+			       &format_page_default,
+			       sizeof(format_page_default));
+
+			format_page = &lun->mode_pages.format_page[
+				CTL_PAGE_CURRENT];
+			scsi_ulto2b(lun->be_lun->blocksize,
+				    format_page->bytes_per_sector);
+
+			format_page = &lun->mode_pages.format_page[
+				CTL_PAGE_DEFAULT];
+			scsi_ulto2b(lun->be_lun->blocksize,
+				    format_page->bytes_per_sector);
+
+			format_page = &lun->mode_pages.format_page[
+				CTL_PAGE_SAVED];
+			scsi_ulto2b(lun->be_lun->blocksize,
+				    format_page->bytes_per_sector);
+
+			page_index->page_data =
+				(uint8_t *)lun->mode_pages.format_page;
+			break;
+		}
+		case SMS_RIGID_DISK_PAGE: {
+			struct scsi_rigid_disk_page *rigid_disk_page;
+			uint32_t sectors_per_cylinder;
+			uint64_t cylinders;
+#ifndef	__XSCALE__
+			int shift;
+#endif /* !__XSCALE__ */
+
+			if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
+				panic("invalid subpage value %d",
+				      page_index->subpage);
+
+			/*
+			 * Rotation rate and sectors per track are set
+			 * above.  We calculate the cylinders here based on
+			 * capacity.  Due to the number of heads and
+			 * sectors per track we're using, smaller arrays
+			 * may turn out to have 0 cylinders.  Linux and
+			 * FreeBSD don't pay attention to these mode pages
+			 * to figure out capacity, but Solaris does.  It
+			 * seems to deal with 0 cylinders just fine, and
+			 * works out a fake geometry based on the capacity.
+			 */
+			memcpy(&lun->mode_pages.rigid_disk_page[
+			       CTL_PAGE_CURRENT], &rigid_disk_page_default,
+			       sizeof(rigid_disk_page_default));
+			memcpy(&lun->mode_pages.rigid_disk_page[
+			       CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
+			       sizeof(rigid_disk_page_changeable));
+			memcpy(&lun->mode_pages.rigid_disk_page[
+			       CTL_PAGE_DEFAULT], &rigid_disk_page_default,
+			       sizeof(rigid_disk_page_default));
+			memcpy(&lun->mode_pages.rigid_disk_page[
+			       CTL_PAGE_SAVED], &rigid_disk_page_default,
+			       sizeof(rigid_disk_page_default));
+
+			sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
+				CTL_DEFAULT_HEADS;
+
+			/*
+			 * The divide method here will be more accurate,
+			 * probably, but results in floating point being
+			 * used in the kernel on i386 (__udivdi3()).  On the
+			 * XScale, though, __udivdi3() is implemented in
+			 * software.
+			 *
+			 * The shift method for cylinder calculation is
+			 * accurate if sectors_per_cylinder is a power of
+			 * 2.  Otherwise it might be slightly off -- you
+			 * might have a bit of a truncation problem.
+			 */
+#ifdef	__XSCALE__
+			cylinders = (lun->be_lun->maxlba + 1) /
+				sectors_per_cylinder;
+#else
+			for (shift = 31; shift > 0; shift--) {
+				if (sectors_per_cylinder & (1 << shift))
+					break;
+			}
+			cylinders = (lun->be_lun->maxlba + 1) >> shift;
+#endif
+
+			/*
+			 * We've basically got 3 bytes, or 24 bits for the
+			 * cylinder size in the mode page.  If we're over,
+			 * just round down to 2^24.
+			 */
+			if (cylinders > 0xffffff)
+				cylinders = 0xffffff;
+
+			rigid_disk_page = &lun->mode_pages.rigid_disk_page[
+				CTL_PAGE_CURRENT];
+			scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
+
+			rigid_disk_page = &lun->mode_pages.rigid_disk_page[
+				CTL_PAGE_DEFAULT];
+			scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
+
+			rigid_disk_page = &lun->mode_pages.rigid_disk_page[
+				CTL_PAGE_SAVED];
+			scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
+
+			page_index->page_data =
+				(uint8_t *)lun->mode_pages.rigid_disk_page;
+			break;
+		}
+		case SMS_CACHING_PAGE: {
+
+			if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
+				panic("invalid subpage value %d",
+				      page_index->subpage);
+			/*
+			 * Defaults should be okay here, no calculations
+			 * needed.
+			 */
+			memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
+			       &caching_page_default,
+			       sizeof(caching_page_default));
+			memcpy(&lun->mode_pages.caching_page[
+			       CTL_PAGE_CHANGEABLE], &caching_page_changeable,
+			       sizeof(caching_page_changeable));
+			memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
+			       &caching_page_default,
+			       sizeof(caching_page_default));
+			memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
+			       &caching_page_default,
+			       sizeof(caching_page_default));
+			page_index->page_data =
+				(uint8_t *)lun->mode_pages.caching_page;
+			break;
+		}
+		case SMS_CONTROL_MODE_PAGE: {
+
+			if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
+				panic("invalid subpage value %d",
+				      page_index->subpage);
+
+			/*
+			 * Defaults should be okay here, no calculations
+			 * needed.
+			 */
+			memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT],
+			       &control_page_default,
+			       sizeof(control_page_default));
+			memcpy(&lun->mode_pages.control_page[
+			       CTL_PAGE_CHANGEABLE], &control_page_changeable,
+			       sizeof(control_page_changeable));
+			memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT],
+			       &control_page_default,
+			       sizeof(control_page_default));
+			memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED],
+			       &control_page_default,
+			       sizeof(control_page_default));
+			page_index->page_data =
+				(uint8_t *)lun->mode_pages.control_page;
+			break;
+
+		}
+		case SMS_VENDOR_SPECIFIC_PAGE:{
+			switch (page_index->subpage) {
+			case PWR_SUBPAGE_CODE: {
+				struct copan_power_subpage *current_page,
+							   *saved_page;
+
+				memcpy(&lun->mode_pages.power_subpage[
+				       CTL_PAGE_CURRENT],
+				       &power_page_default,
+				       sizeof(power_page_default));
+				memcpy(&lun->mode_pages.power_subpage[
+				       CTL_PAGE_CHANGEABLE],
+				       &power_page_changeable,
+				       sizeof(power_page_changeable));
+				memcpy(&lun->mode_pages.power_subpage[
+				       CTL_PAGE_DEFAULT],
+				       &power_page_default,
+				       sizeof(power_page_default));
+				memcpy(&lun->mode_pages.power_subpage[
+				       CTL_PAGE_SAVED],
+				       &power_page_default,
+				       sizeof(power_page_default));
+				page_index->page_data =
+				    (uint8_t *)lun->mode_pages.power_subpage;
+
+				current_page = (struct copan_power_subpage *)
+					(page_index->page_data +
+					 (page_index->page_len *
+					  CTL_PAGE_CURRENT));
+			        saved_page = (struct copan_power_subpage *)
+				        (page_index->page_data +
+					 (page_index->page_len *
+					  CTL_PAGE_SAVED));
+				break;
+			}
+			case APS_SUBPAGE_CODE: {
+				struct copan_aps_subpage *current_page,
+							 *saved_page;
+
+				// This gets set multiple times but
+				// it should always be the same. It's
+				// only done during init so who cares.
+				index_to_aps_page = i;
+
+				memcpy(&lun->mode_pages.aps_subpage[
+				       CTL_PAGE_CURRENT],
+				       &aps_page_default,
+				       sizeof(aps_page_default));
+				memcpy(&lun->mode_pages.aps_subpage[
+				       CTL_PAGE_CHANGEABLE],
+				       &aps_page_changeable,
+				       sizeof(aps_page_changeable));
+				memcpy(&lun->mode_pages.aps_subpage[
+				       CTL_PAGE_DEFAULT],
+				       &aps_page_default,
+				       sizeof(aps_page_default));
+				memcpy(&lun->mode_pages.aps_subpage[
+				       CTL_PAGE_SAVED],
+				       &aps_page_default,
+				       sizeof(aps_page_default));
+				page_index->page_data =
+					(uint8_t *)lun->mode_pages.aps_subpage;
+
+				current_page = (struct copan_aps_subpage *)
+					(page_index->page_data +
+					 (page_index->page_len *
+					  CTL_PAGE_CURRENT));
+				saved_page = (struct copan_aps_subpage *)
+					(page_index->page_data +
+					 (page_index->page_len *
+					  CTL_PAGE_SAVED));
+				break;
+			}
+			case DBGCNF_SUBPAGE_CODE: {
+				struct copan_debugconf_subpage *current_page,
+							       *saved_page;
+
+				memcpy(&lun->mode_pages.debugconf_subpage[
+				       CTL_PAGE_CURRENT],
+				       &debugconf_page_default,
+				       sizeof(debugconf_page_default));
+				memcpy(&lun->mode_pages.debugconf_subpage[
+				       CTL_PAGE_CHANGEABLE],
+				       &debugconf_page_changeable,
+				       sizeof(debugconf_page_changeable));
+				memcpy(&lun->mode_pages.debugconf_subpage[
+				       CTL_PAGE_DEFAULT],
+				       &debugconf_page_default,
+				       sizeof(debugconf_page_default));
+				memcpy(&lun->mode_pages.debugconf_subpage[
+				       CTL_PAGE_SAVED],
+				       &debugconf_page_default,
+				       sizeof(debugconf_page_default));
+				page_index->page_data =
+					(uint8_t *)lun->mode_pages.debugconf_subpage;
+
+				current_page = (struct copan_debugconf_subpage *)
+					(page_index->page_data +
+					 (page_index->page_len *
+					  CTL_PAGE_CURRENT));
+				saved_page = (struct copan_debugconf_subpage *)
+					(page_index->page_data +
+					 (page_index->page_len *
+					  CTL_PAGE_SAVED));
+				break;
+			}
+			default:
+				panic("invalid subpage value %d",
+				      page_index->subpage);
+				break;
+			}
+   			break;
+		}
+		default:
+			panic("invalid page value %d",
+			      page_index->page_code & SMPH_PC_MASK);
+			break;
+    	}
+	}
+
+	return (CTL_RETVAL_COMPLETE);
+}
+
+/*
+ * LUN allocation.
+ *
+ * Requirements:
+ * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
+ *   wants us to allocate the LUN and he can block.
+ * - ctl_softc is always set
+ * - be_lun is set if the LUN has a backend (needed for disk LUNs)
+ *
+ * Returns 0 for success, non-zero (errno) for failure.
+ */
+static int
+ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
+	      struct ctl_be_lun *const be_lun, struct ctl_id target_id)
+{
+	struct ctl_lun *nlun, *lun;
+	struct ctl_frontend *fe;
+	int lun_number, i;
+
+	if (be_lun == NULL)
+		return (EINVAL);
+
+	/*
+	 * We currently only support Direct Access or Processor LUN types.
+	 */
+	switch (be_lun->lun_type) {
+	case T_DIRECT:
+		break;
+	case T_PROCESSOR:
+		break;
+	case T_SEQUENTIAL:
+	case T_CHANGER:
+	default:
+		be_lun->lun_config_status(be_lun->be_lun,
+					  CTL_LUN_CONFIG_FAILURE);
+		break;
+	}
+	if (ctl_lun == NULL) {
+		lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
+		if (lun == NULL) {
+			be_lun->lun_config_status(lun->be_lun->be_lun,
+						  CTL_LUN_CONFIG_FAILURE);
+			return (-ENOMEM);
+		}
+		lun->flags = CTL_LUN_MALLOCED;
+	} else
+		lun = ctl_lun;
+
+	memset(lun, 0, sizeof(*lun));
+
+	mtx_lock(&ctl_softc->ctl_lock);
+	/*
+	 * See if the caller requested a particular LUN number.  If so, see
+	 * if it is available.  Otherwise, allocate the first available LUN.
+	 */
+	if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
+		if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
+		 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
+			mtx_unlock(&ctl_softc->ctl_lock);
+			if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
+				printf("ctl: requested LUN ID %d is higher "
+				       "than CTL_MAX_LUNS - 1 (%d)\n",
+				       be_lun->req_lun_id, CTL_MAX_LUNS - 1);
+			} else {
+				/*
+				 * XXX KDM return an error, or just assign
+				 * another LUN ID in this case??
+				 */
+				printf("ctl: requested LUN ID %d is already "
+				       "in use\n", be_lun->req_lun_id);
+			}
+			if (lun->flags & CTL_LUN_MALLOCED)
+				free(lun, M_CTL);
+			be_lun->lun_config_status(be_lun->be_lun,
+						  CTL_LUN_CONFIG_FAILURE);
+			return (ENOSPC);
+		}
+		lun_number = be_lun->req_lun_id;
+	} else {
+		lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS);
+		if (lun_number == -1) {
+			mtx_unlock(&ctl_softc->ctl_lock);
+			printf("ctl: can't allocate LUN on target %ju, out of "
+			       "LUNs\n", (uintmax_t)target_id.id);
+			if (lun->flags & CTL_LUN_MALLOCED)
+				free(lun, M_CTL);
+			be_lun->lun_config_status(be_lun->be_lun,
+						  CTL_LUN_CONFIG_FAILURE);
+			return (ENOSPC);
+		}
+	}
+	ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
+
+	lun->target = target_id;
+	lun->lun = lun_number;
+	lun->be_lun = be_lun;
+	/*
+	 * The processor LUN is always enabled.  Disk LUNs come on line
+	 * disabled, and must be enabled by the backend.
+	 */
+	lun->flags = CTL_LUN_DISABLED;
+	lun->backend = be_lun->be;
+	be_lun->ctl_lun = lun;
+	be_lun->lun_id = lun_number;
+	atomic_add_int(&be_lun->be->num_luns, 1);
+	if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF)
+		lun->flags |= CTL_LUN_STOPPED;
+
+	if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE)
+		lun->flags |= CTL_LUN_INOPERABLE;
+
+	if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
+		lun->flags |= CTL_LUN_PRIMARY_SC;
+
+	lun->ctl_softc = ctl_softc;
+	TAILQ_INIT(&lun->ooa_queue);
+	TAILQ_INIT(&lun->blocked_queue);
+	STAILQ_INIT(&lun->error_list);
+
+	/*
+	 * Initialize the mode page index.
+	 */
+	ctl_init_page_index(lun);
+
+	/*
+	 * Set the poweron UA for all initiators on this LUN only.
+	 */
+	for (i = 0; i < CTL_MAX_INITIATORS; i++)
+		lun->pending_sense[i].ua_pending = CTL_UA_POWERON;
+
+	/*
+	 * Now, before we insert this lun on the lun list, set the lun
+	 * inventory changed UA for all other luns.
+	 */
+	STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
+		for (i = 0; i < CTL_MAX_INITIATORS; i++) {
+			nlun->pending_sense[i].ua_pending |= CTL_UA_LUN_CHANGE;
+		}
+	}
+
+	STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
+
+	ctl_softc->ctl_luns[lun_number] = lun;
+
+	ctl_softc->num_luns++;
+
+	/* Setup statistics gathering */
+	lun->stats.device_type = be_lun->lun_type;
+	lun->stats.lun_number = lun_number;
+	if (lun->stats.device_type == T_DIRECT)
+		lun->stats.blocksize = be_lun->blocksize;
+	else
+		lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
+	for (i = 0;i < CTL_MAX_PORTS;i++)
+		lun->stats.ports[i].targ_port = i;
+
+	mtx_unlock(&ctl_softc->ctl_lock);
+
+	lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
+
+	/*
+	 * Run through each registered FETD and bring it online if it isn't
+	 * already.  Enable the target ID if it hasn't been enabled, and
+	 * enable this particular LUN.
+	 */
+	STAILQ_FOREACH(fe, &ctl_softc->fe_list, links) {
+		int retval;
+
+		/*
+		 * XXX KDM this only works for ONE TARGET ID.  We'll need
+		 * to do things differently if we go to a multiple target
+		 * ID scheme.
+		 */
+		if ((fe->status & CTL_PORT_STATUS_TARG_ONLINE) == 0) {
+
+			retval = fe->targ_enable(fe->targ_lun_arg, target_id);
+			if (retval != 0) {
+				printf("ctl_alloc_lun: FETD %s port %d "
+				       "returned error %d for targ_enable on "
+				       "target %ju\n", fe->port_name,
+				       fe->targ_port, retval,
+				       (uintmax_t)target_id.id);
+			} else
+				fe->status |= CTL_PORT_STATUS_TARG_ONLINE;
+		}
+
+		retval = fe->lun_enable(fe->targ_lun_arg, target_id,lun_number);
+		if (retval != 0) {
+			printf("ctl_alloc_lun: FETD %s port %d returned error "
+			       "%d for lun_enable on target %ju lun %d\n",
+			       fe->port_name, fe->targ_port, retval,
+			       (uintmax_t)target_id.id, lun_number);
+		} else
+			fe->status |= CTL_PORT_STATUS_LUN_ONLINE;
+	}
+	return (0);
+}
+
+/*
+ * Delete a LUN.
+ * Assumptions:
+ * - caller holds ctl_softc->ctl_lock.
+ * - LUN has already been marked invalid and any pending I/O has been taken
+ *   care of.
+ */
+static int
+ctl_free_lun(struct ctl_lun *lun)
+{
+	struct ctl_softc *softc;
+#if 0
+	struct ctl_frontend *fe;
+#endif
+	struct ctl_lun *nlun;
+	union ctl_io *io, *next_io;
+	int i;
+
+	softc = lun->ctl_softc;
+
+	STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
+
+	ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
+
+	softc->ctl_luns[lun->lun] = NULL;
+
+	if (TAILQ_FIRST(&lun->ooa_queue) != NULL) {
+		printf("ctl_free_lun: aieee!! freeing a LUN with "
+		       "outstanding I/O!!\n");
+	}
+
+	/*
+	 * If we have anything pending on the RtR queue, remove it.
+	 */
+	for (io = (union ctl_io *)STAILQ_FIRST(&softc->rtr_queue); io != NULL;
+	     io = next_io) {
+		next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links);
+		if ((io->io_hdr.nexus.targ_target.id == lun->target.id)
+		 && (io->io_hdr.nexus.targ_lun == lun->lun))
+			STAILQ_REMOVE(&softc->rtr_queue, &io->io_hdr,
+				      ctl_io_hdr, links);
+	}
+
+	/*
+	 * Then remove everything from the blocked queue.
+	 */
+	for (io = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); io != NULL;
+	     io = next_io) {
+		next_io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,blocked_links);
+		TAILQ_REMOVE(&lun->blocked_queue, &io->io_hdr, blocked_links);
+		io->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
+	}
+
+	/*
+	 * Now clear out the OOA queue, and free all the I/O.
+	 * XXX KDM should we notify the FETD here?  We probably need to
+	 * quiesce the LUN before deleting it.
+	 */
+	for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); io != NULL;
+	     io = next_io) {
+		next_io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, ooa_links);
+		TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
+		ctl_free_io_internal(io, /*have_lock*/ 1);
+	}
+
+	softc->num_luns--;
+
+	/*
+	 * XXX KDM this scheme only works for a single target/multiple LUN
+	 * setup.  It needs to be revamped for a multiple target scheme.
+	 *
+	 * XXX KDM this results in fe->lun_disable() getting called twice,
+	 * once when ctl_disable_lun() is called, and a second time here.
+	 * We really need to re-think the LUN disable semantics.  There
+	 * should probably be several steps/levels to LUN removal:
+	 *  - disable
+	 *  - invalidate
+	 *  - free
+ 	 *
+	 * Right now we only have a disable method when communicating to
+	 * the front end ports, at least for individual LUNs.
+	 */
+#if 0
+	STAILQ_FOREACH(fe, &softc->fe_list, links) {
+		int retval;
+
+		retval = fe->lun_disable(fe->targ_lun_arg, lun->target,
+					 lun->lun);
+		if (retval != 0) {
+			printf("ctl_free_lun: FETD %s port %d returned error "
+			       "%d for lun_disable on target %ju lun %jd\n",
+			       fe->port_name, fe->targ_port, retval,
+			       (uintmax_t)lun->target.id, (intmax_t)lun->lun);
+		}
+
+		if (STAILQ_FIRST(&softc->lun_list) == NULL) {
+			fe->status &= ~CTL_PORT_STATUS_LUN_ONLINE;
+
+			retval = fe->targ_disable(fe->targ_lun_arg,lun->target);
+			if (retval != 0) {
+				printf("ctl_free_lun: FETD %s port %d "
+				       "returned error %d for targ_disable on "
+				       "target %ju\n", fe->port_name,
+				       fe->targ_port, retval,
+				       (uintmax_t)lun->target.id);
+			} else
+				fe->status &= ~CTL_PORT_STATUS_TARG_ONLINE;
+
+			if ((fe->status & CTL_PORT_STATUS_TARG_ONLINE) != 0)
+				continue;
+
+#if 0
+			fe->port_offline(fe->onoff_arg);
+			fe->status &= ~CTL_PORT_STATUS_ONLINE;
+#endif
+		}
+	}
+#endif
+
+	/*
+	 * Tell the backend to free resources, if this LUN has a backend.
+	 */
+	atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
+	lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
+
+	if (lun->flags & CTL_LUN_MALLOCED)
+		free(lun, M_CTL);
+
+	STAILQ_FOREACH(nlun, &softc->lun_list, links) {
+		for (i = 0; i < CTL_MAX_INITIATORS; i++) {
+			nlun->pending_sense[i].ua_pending |= CTL_UA_LUN_CHANGE;
+		}
+	}
+
+	return (0);
+}
+
+static void
+ctl_create_lun(struct ctl_be_lun *be_lun)
+{
+	struct ctl_softc *ctl_softc;
+
+	ctl_softc = control_softc;
+
+	/*
+	 * ctl_alloc_lun() should handle all potential failure cases.
+	 */
+	ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target);
+}
+
+int
+ctl_add_lun(struct ctl_be_lun *be_lun)
+{
+	struct ctl_softc *ctl_softc;
+
+	ctl_softc = control_softc;
+
+	mtx_lock(&ctl_softc->ctl_lock);
+	STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links);
+	mtx_unlock(&ctl_softc->ctl_lock);
+
+	ctl_wakeup_thread();
+
+	return (0);
+}
+
+int
+ctl_enable_lun(struct ctl_be_lun *be_lun)
+{
+	struct ctl_softc *ctl_softc;
+	struct ctl_frontend *fe, *nfe;
+	struct ctl_lun *lun;
+	int retval;
+
+	ctl_softc = control_softc;
+
+	lun = (struct ctl_lun *)be_lun->ctl_lun;
+
+	mtx_lock(&ctl_softc->ctl_lock);
+	if ((lun->flags & CTL_LUN_DISABLED) == 0) {
+		/*
+		 * eh?  Why did we get called if the LUN is already
+		 * enabled?
+		 */
+		mtx_unlock(&ctl_softc->ctl_lock);
+		return (0);
+	}
+	lun->flags &= ~CTL_LUN_DISABLED;
+
+	for (fe = STAILQ_FIRST(&ctl_softc->fe_list); fe != NULL; fe = nfe) {
+		nfe = STAILQ_NEXT(fe, links);
+
+		/*
+		 * Drop the lock while we call the FETD's enable routine.
+		 * This can lead to a callback into CTL (at least in the
+		 * case of the internal initiator frontend.
+		 */
+		mtx_unlock(&ctl_softc->ctl_lock);
+		retval = fe->lun_enable(fe->targ_lun_arg, lun->target,lun->lun);
+		mtx_lock(&ctl_softc->ctl_lock);
+		if (retval != 0) {
+			printf("%s: FETD %s port %d returned error "
+			       "%d for lun_enable on target %ju lun %jd\n",
+			       __func__, fe->port_name, fe->targ_port, retval,
+			       (uintmax_t)lun->target.id, (intmax_t)lun->lun);
+		}
+#if 0
+		 else {
+            /* NOTE:  TODO:  why does lun enable affect port status? */
+			fe->status |= CTL_PORT_STATUS_LUN_ONLINE;
+		}
+#endif
+	}
+
+	mtx_unlock(&ctl_softc->ctl_lock);
+
+	return (0);
+}
+
+int
+ctl_disable_lun(struct ctl_be_lun *be_lun)
+{
+	struct ctl_softc *ctl_softc;
+	struct ctl_frontend *fe;
+	struct ctl_lun *lun;
+	int retval;
+
+	ctl_softc = control_softc;
+
+	lun = (struct ctl_lun *)be_lun->ctl_lun;
+
+	mtx_lock(&ctl_softc->ctl_lock);
+
+	if (lun->flags & CTL_LUN_DISABLED) {
+		mtx_unlock(&ctl_softc->ctl_lock);
+		return (0);
+	}
+	lun->flags |= CTL_LUN_DISABLED;
+
+	STAILQ_FOREACH(fe, &ctl_softc->fe_list, links) {
+		mtx_unlock(&ctl_softc->ctl_lock);
+		/*
+		 * Drop the lock before we call the frontend's disable
+		 * routine, to avoid lock order reversals.
+		 *
+		 * XXX KDM what happens if the frontend list changes while
+		 * we're traversing it?  It's unlikely, but should be handled.
+		 */
+		retval = fe->lun_disable(fe->targ_lun_arg, lun->target,
+					 lun->lun);
+		mtx_lock(&ctl_softc->ctl_lock);
+		if (retval != 0) {
+			printf("ctl_alloc_lun: FETD %s port %d returned error "
+			       "%d for lun_disable on target %ju lun %jd\n",
+			       fe->port_name, fe->targ_port, retval,
+			       (uintmax_t)lun->target.id, (intmax_t)lun->lun);
+		}
+	}
+
+	mtx_unlock(&ctl_softc->ctl_lock);
+
+	return (0);
+}
+
+int
+ctl_start_lun(struct ctl_be_lun *be_lun)
+{
+	struct ctl_softc *ctl_softc;
+	struct ctl_lun *lun;
+
+	ctl_softc = control_softc;
+
+	lun = (struct ctl_lun *)be_lun->ctl_lun;
+
+	mtx_lock(&ctl_softc->ctl_lock);
+	lun->flags &= ~CTL_LUN_STOPPED;
+	mtx_unlock(&ctl_softc->ctl_lock);
+
+	return (0);
+}
+
+int
+ctl_stop_lun(struct ctl_be_lun *be_lun)
+{
+	struct ctl_softc *ctl_softc;
+	struct ctl_lun *lun;
+
+	ctl_softc = control_softc;
+
+	lun = (struct ctl_lun *)be_lun->ctl_lun;
+
+	mtx_lock(&ctl_softc->ctl_lock);
+	lun->flags |= CTL_LUN_STOPPED;
+	mtx_unlock(&ctl_softc->ctl_lock);
+
+	return (0);
+}
+
+int
+ctl_lun_offline(struct ctl_be_lun *be_lun)
+{
+	struct ctl_softc *ctl_softc;
+	struct ctl_lun *lun;
+
+	ctl_softc = control_softc;
+
+	lun = (struct ctl_lun *)be_lun->ctl_lun;
+
+	mtx_lock(&ctl_softc->ctl_lock);
+	lun->flags |= CTL_LUN_OFFLINE;
+	mtx_unlock(&ctl_softc->ctl_lock);
+
+	return (0);
+}
+
+int
+ctl_lun_online(struct ctl_be_lun *be_lun)
+{
+	struct ctl_softc *ctl_softc;
+	struct ctl_lun *lun;
+
+	ctl_softc = control_softc;
+
+	lun = (struct ctl_lun *)be_lun->ctl_lun;
+
+	mtx_lock(&ctl_softc->ctl_lock);
+	lun->flags &= ~CTL_LUN_OFFLINE;
+	mtx_unlock(&ctl_softc->ctl_lock);
+
+	return (0);
+}
+
+int
+ctl_invalidate_lun(struct ctl_be_lun *be_lun)
+{
+	struct ctl_softc *ctl_softc;
+	struct ctl_lun *lun;
+
+	ctl_softc = control_softc;
+
+	lun = (struct ctl_lun *)be_lun->ctl_lun;
+
+	mtx_lock(&ctl_softc->ctl_lock);
+
+	/*
+	 * The LUN needs to be disabled before it can be marked invalid.
+	 */
+	if ((lun->flags & CTL_LUN_DISABLED) == 0) {
+		mtx_unlock(&ctl_softc->ctl_lock);
+		return (-1);
+	}
+	/*
+	 * Mark the LUN invalid.
+	 */
+	lun->flags |= CTL_LUN_INVALID;
+
+	/*
+	 * If there is nothing in the OOA queue, go ahead and free the LUN.
+	 * If we have something in the OOA queue, we'll free it when the
+	 * last I/O completes.
+	 */
+	if (TAILQ_FIRST(&lun->ooa_queue) == NULL)
+		ctl_free_lun(lun);
+	mtx_unlock(&ctl_softc->ctl_lock);
+
+	return (0);
+}
+
+int
+ctl_lun_inoperable(struct ctl_be_lun *be_lun)
+{
+	struct ctl_softc *ctl_softc;
+	struct ctl_lun *lun;
+
+	ctl_softc = control_softc;
+	lun = (struct ctl_lun *)be_lun->ctl_lun;
+
+	mtx_lock(&ctl_softc->ctl_lock);
+	lun->flags |= CTL_LUN_INOPERABLE;
+	mtx_unlock(&ctl_softc->ctl_lock);
+
+	return (0);
+}
+
+int
+ctl_lun_operable(struct ctl_be_lun *be_lun)
+{
+	struct ctl_softc *ctl_softc;
+	struct ctl_lun *lun;
+
+	ctl_softc = control_softc;
+	lun = (struct ctl_lun *)be_lun->ctl_lun;
+
+	mtx_lock(&ctl_softc->ctl_lock);
+	lun->flags &= ~CTL_LUN_INOPERABLE;
+	mtx_unlock(&ctl_softc->ctl_lock);
+
+	return (0);
+}
+
+int
+ctl_lun_power_lock(struct ctl_be_lun *be_lun, struct ctl_nexus *nexus,
+		   int lock)
+{
+	struct ctl_softc *softc;
+	struct ctl_lun *lun;
+	struct copan_aps_subpage *current_sp;
+	struct ctl_page_index *page_index;
+	int i;
+
+	softc = control_softc;
+
+	mtx_lock(&softc->ctl_lock);
+
+	lun = (struct ctl_lun *)be_lun->ctl_lun;
+
+	page_index = NULL;
+	for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
+		if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
+		     APS_PAGE_CODE)
+			continue;
+
+		if (lun->mode_pages.index[i].subpage != APS_SUBPAGE_CODE)
+			continue;
+		page_index = &lun->mode_pages.index[i];
+	}
+
+	if (page_index == NULL) {
+		mtx_unlock(&softc->ctl_lock);
+		printf("%s: APS subpage not found for lun %ju!\n", __func__,
+		       (uintmax_t)lun->lun);
+		return (1);
+	}
+#if 0
+	if ((softc->aps_locked_lun != 0)
+	 && (softc->aps_locked_lun != lun->lun)) {
+		printf("%s: attempt to lock LUN %llu when %llu is already "
+		       "locked\n");
+		mtx_unlock(&softc->ctl_lock);
+		return (1);
+	}
+#endif
+
+	current_sp = (struct copan_aps_subpage *)(page_index->page_data +
+		(page_index->page_len * CTL_PAGE_CURRENT));
+
+	if (lock != 0) {
+		current_sp->lock_active = APS_LOCK_ACTIVE;
+		softc->aps_locked_lun = lun->lun;
+	} else {
+		current_sp->lock_active = 0;
+		softc->aps_locked_lun = 0;
+	}
+
+
+	/*
+	 * If we're in HA mode, try to send the lock message to the other
+	 * side.
+	 */
+	if (ctl_is_single == 0) {
+		int isc_retval;
+		union ctl_ha_msg lock_msg;
+
+		lock_msg.hdr.nexus = *nexus;
+		lock_msg.hdr.msg_type = CTL_MSG_APS_LOCK;
+		if (lock != 0)
+			lock_msg.aps.lock_flag = 1;
+		else
+			lock_msg.aps.lock_flag = 0;
+		isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &lock_msg,
+					 sizeof(lock_msg), 0);
+		if (isc_retval > CTL_HA_STATUS_SUCCESS) {
+			printf("%s: APS (lock=%d) error returned from "
+			       "ctl_ha_msg_send: %d\n", __func__, lock, isc_retval);
+			mtx_unlock(&softc->ctl_lock);
+			return (1);
+		}
+	}
+
+	mtx_unlock(&softc->ctl_lock);
+
+	return (0);
+}
+
+/*
+ * Backend "memory move is complete" callback for requests that never
+ * make it down to say RAIDCore's configuration code.
+ */
+int
+ctl_config_move_done(union ctl_io *io)
+{
+	int retval;
+
+	retval = CTL_RETVAL_COMPLETE;
+
+
+	CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
+	/*
+	 * XXX KDM this shouldn't happen, but what if it does?
+	 */
+	if (io->io_hdr.io_type != CTL_IO_SCSI)
+		panic("I/O type isn't CTL_IO_SCSI!");
+
+	if ((io->io_hdr.port_status == 0)
+	 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
+	 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE))
+		io->io_hdr.status = CTL_SUCCESS;
+	else if ((io->io_hdr.port_status != 0)
+	      && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
+	      && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)){
+		/*
+		 * For hardware error sense keys, the sense key
+		 * specific value is defined to be a retry count,
+		 * but we use it to pass back an internal FETD
+		 * error code.  XXX KDM  Hopefully the FETD is only
+		 * using 16 bits for an error code, since that's
+		 * all the space we have in the sks field.
+		 */
+		ctl_set_internal_failure(&io->scsiio,
+					 /*sks_valid*/ 1,
+					 /*retry_count*/
+					 io->io_hdr.port_status);
+		free(io->scsiio.kern_data_ptr, M_CTL);
+		ctl_done(io);
+		goto bailout;
+	}
+
+	if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
+	 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
+	 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
+		/*
+		 * XXX KDM just assuming a single pointer here, and not a
+		 * S/G list.  If we start using S/G lists for config data,
+		 * we'll need to know how to clean them up here as well.
+		 */
+		free(io->scsiio.kern_data_ptr, M_CTL);
+		/* Hopefully the user has already set the status... */
+		ctl_done(io);
+	} else {
+		/*
+		 * XXX KDM now we need to continue data movement.  Some
+		 * options:
+		 * - call ctl_scsiio() again?  We don't do this for data
+		 *   writes, because for those at least we know ahead of
+		 *   time where the write will go and how long it is.  For
+		 *   config writes, though, that information is largely
+		 *   contained within the write itself, thus we need to
+		 *   parse out the data again.
+		 *
+		 * - Call some other function once the data is in?
+		 */
+
+		/*
+		 * XXX KDM call ctl_scsiio() again for now, and check flag
+		 * bits to see whether we're allocated or not.
+		 */
+		retval = ctl_scsiio(&io->scsiio);
+	}
+bailout:
+	return (retval);
+}
+
+/*
+ * This gets called by a backend driver when it is done with a
+ * configuration write.
+ */
+void
+ctl_config_write_done(union ctl_io *io)
+{
+	/*
+	 * If the IO_CONT flag is set, we need to call the supplied
+	 * function to continue processing the I/O, instead of completing
+	 * the I/O just yet.
+	 *
+	 * If there is an error, though, we don't want to keep processing.
+	 * Instead, just send status back to the initiator.
+	 */
+	if ((io->io_hdr.flags & CTL_FLAG_IO_CONT)
+	 && (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)
+	  || ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))) {
+		io->scsiio.io_cont(io);
+		return;
+	}
+	/*
+	 * Since a configuration write can be done for commands that actually
+	 * have data allocated, like write buffer, and commands that have
+	 * no data, like start/stop unit, we need to check here.
+	 */
+	if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
+		free(io->scsiio.kern_data_ptr, M_CTL);
+	ctl_done(io);
+}
+
+/*
+ * SCSI release command.
+ */
+int
+ctl_scsi_release(struct ctl_scsiio *ctsio)
+{
+	int length, longid, thirdparty_id, resv_id;
+	struct ctl_softc *ctl_softc;
+	struct ctl_lun *lun;
+
+	length = 0;
+	resv_id = 0;
+
+	CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
+
+	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
+	ctl_softc = control_softc;
+
+	switch (ctsio->cdb[0]) {
+	case RELEASE: {
+		struct scsi_release *cdb;
+
+		cdb = (struct scsi_release *)ctsio->cdb;
+		if ((cdb->byte2 & 0x1f) != 0) {
+			ctl_set_invalid_field(ctsio,
+					      /*sks_valid*/ 1,
+					      /*command*/ 1,
+					      /*field*/ 1,
+					      /*bit_valid*/ 0,
+					      /*bit*/ 0);
+			ctl_done((union ctl_io *)ctsio);
+			return (CTL_RETVAL_COMPLETE);
+		}
+		break;
+	}
+	case RELEASE_10: {
+		struct scsi_release_10 *cdb;
+
+		cdb = (struct scsi_release_10 *)ctsio->cdb;
+
+		if ((cdb->byte2 & SR10_EXTENT) != 0) {
+			ctl_set_invalid_field(ctsio,
+					      /*sks_valid*/ 1,
+					      /*command*/ 1,
+					      /*field*/ 1,
+					      /*bit_valid*/ 1,
+					      /*bit*/ 0);
+			ctl_done((union ctl_io *)ctsio);
+			return (CTL_RETVAL_COMPLETE);
+
+		}
+
+		if ((cdb->byte2 & SR10_3RDPTY) != 0) {
+			ctl_set_invalid_field(ctsio,
+					      /*sks_valid*/ 1,
+					      /*command*/ 1,
+					      /*field*/ 1,
+					      /*bit_valid*/ 1,
+					      /*bit*/ 4);
+			ctl_done((union ctl_io *)ctsio);
+			return (CTL_RETVAL_COMPLETE);
+		}
+
+		if (cdb->byte2 & SR10_LONGID)
+			longid = 1;
+		else
+			thirdparty_id = cdb->thirdparty_id;
+
+		resv_id = cdb->resv_id;
+		length = scsi_2btoul(cdb->length);
+		break;
+	}
+	}
+
+
+	/*
+	 * XXX KDM right now, we only support LUN reservation.  We don't
+	 * support 3rd party reservations, or extent reservations, which
+	 * might actually need the parameter list.  If we've gotten this
+	 * far, we've got a LUN reservation.  Anything else got kicked out
+	 * above.  So, according to SPC, ignore the length.
+	 */
+	length = 0;
+
+	if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
+	 && (length > 0)) {
+		ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
+		if (ctsio->kern_data_ptr == NULL) {
+			ctsio->io_hdr.status = CTL_SCSI_ERROR;
+			ctsio->io_hdr.status = SCSI_STATUS_BUSY;
+			ctl_done((union ctl_io *)ctsio);
+			return (CTL_RETVAL_COMPLETE);
+		}
+		ctsio->kern_data_len = length;
+		ctsio->kern_total_len = length;
+		ctsio->kern_data_resid = 0;
+		ctsio->kern_rel_offset = 0;
+		ctsio->kern_sg_entries = 0;
+		ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
+		ctsio->be_move_done = ctl_config_move_done;
+		ctl_datamove((union ctl_io *)ctsio);
+
+		return (CTL_RETVAL_COMPLETE);
+	}
+
+	if (length > 0)
+		thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
+
+	mtx_lock(&ctl_softc->ctl_lock);
+
+	/*
+	 * According to SPC, it is not an error for an intiator to attempt
+	 * to release a reservation on a LUN that isn't reserved, or that
+	 * is reserved by another initiator.  The reservation can only be
+	 * released, though, by the initiator who made it or by one of
+	 * several reset type events.
+	 */
+	if (lun->flags & CTL_LUN_RESERVED) {
+		if ((ctsio->io_hdr.nexus.initid.id == lun->rsv_nexus.initid.id)
+		 && (ctsio->io_hdr.nexus.targ_port == lun->rsv_nexus.targ_port)
+		 && (ctsio->io_hdr.nexus.targ_target.id ==
+		     lun->rsv_nexus.targ_target.id)) {
+			lun->flags &= ~CTL_LUN_RESERVED;
+		}
+	}
+
+	ctsio->scsi_status = SCSI_STATUS_OK;
+	ctsio->io_hdr.status = CTL_SUCCESS;
+
+	if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
+		free(ctsio->kern_data_ptr, M_CTL);
+		ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
+	}
+
+	mtx_unlock(&ctl_softc->ctl_lock);
+
+	ctl_done((union ctl_io *)ctsio);
+	return (CTL_RETVAL_COMPLETE);
+}
+
+int
+ctl_scsi_reserve(struct ctl_scsiio *ctsio)
+{
+	int extent, thirdparty, longid;
+	int resv_id, length;
+	uint64_t thirdparty_id;
+	struct ctl_softc *ctl_softc;
+	struct ctl_lun *lun;
+
+	extent = 0;
+	thirdparty = 0;
+	longid = 0;
+	resv_id = 0;
+	length = 0;
+	thirdparty_id = 0;
+
+	CTL_DEBUG_PRINT(("ctl_reserve\n"));
+
+	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
+	ctl_softc = control_softc;
+
+	switch (ctsio->cdb[0]) {
+	case RESERVE: {
+		struct scsi_reserve *cdb;
+
+		cdb = (struct scsi_reserve *)ctsio->cdb;
+		if ((cdb->byte2 & 0x1f) != 0) {
+			ctl_set_invalid_field(ctsio,
+					      /*sks_valid*/ 1,
+					      /*command*/ 1,
+					      /*field*/ 1,
+					      /*bit_valid*/ 0,
+					      /*bit*/ 0);
+			ctl_done((union ctl_io *)ctsio);
+			return (CTL_RETVAL_COMPLETE);
+		}
+		resv_id = cdb->resv_id;
+		length = scsi_2btoul(cdb->length);
+		break;
+	}
+	case RESERVE_10: {
+		struct scsi_reserve_10 *cdb;
+
+		cdb = (struct scsi_reserve_10 *)ctsio->cdb;
+
+		if ((cdb->byte2 & SR10_EXTENT) != 0) {
+			ctl_set_invalid_field(ctsio,
+					      /*sks_valid*/ 1,
+					      /*command*/ 1,
+					      /*field*/ 1,
+					      /*bit_valid*/ 1,
+					      /*bit*/ 0);
+			ctl_done((union ctl_io *)ctsio);
+			return (CTL_RETVAL_COMPLETE);
+		}
+		if ((cdb->byte2 & SR10_3RDPTY) != 0) {
+			ctl_set_invalid_field(ctsio,
+					      /*sks_valid*/ 1,
+					      /*command*/ 1,
+					      /*field*/ 1,
+					      /*bit_valid*/ 1,
+					      /*bit*/ 4);
+			ctl_done((union ctl_io *)ctsio);
+			return (CTL_RETVAL_COMPLETE);
+		}
+		if (cdb->byte2 & SR10_LONGID)
+			longid = 1;
+		else
+			thirdparty_id = cdb->thirdparty_id;
+
+		resv_id = cdb->resv_id;
+		length = scsi_2btoul(cdb->length);
+		break;
+	}
+	}
+
+	/*
+	 * XXX KDM right now, we only support LUN reservation.  We don't
+	 * support 3rd party reservations, or extent reservations, which
+	 * might actually need the parameter list.  If we've gotten this
+	 * far, we've got a LUN reservation.  Anything else got kicked out
+	 * above.  So, according to SPC, ignore the length.
+	 */
+	length = 0;
+
+	if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
+	 && (length > 0)) {
+		ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
+		if (ctsio->kern_data_ptr == NULL) {
+			ctsio->io_hdr.status = CTL_SCSI_ERROR;
+			ctsio->io_hdr.status = SCSI_STATUS_BUSY;
+			ctl_done((union ctl_io *)ctsio);
+			return (CTL_RETVAL_COMPLETE);
+		}
+		ctsio->kern_data_len = length;
+		ctsio->kern_total_len = length;
+		ctsio->kern_data_resid = 0;
+		ctsio->kern_rel_offset = 0;
+		ctsio->kern_sg_entries = 0;
+		ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
+		ctsio->be_move_done = ctl_config_move_done;
+		ctl_datamove((union ctl_io *)ctsio);
+
+		return (CTL_RETVAL_COMPLETE);
+	}
+
+	if (length > 0)
+		thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
+
+	mtx_lock(&ctl_softc->ctl_lock);
+	if (lun->flags & CTL_LUN_RESERVED) {
+		if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id)
+		 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port)
+		 || (ctsio->io_hdr.nexus.targ_target.id !=
+		     lun->rsv_nexus.targ_target.id)) {
+			ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
+			ctsio->io_hdr.status = CTL_SCSI_ERROR;
+			goto bailout;
+		}
+	}
+
+	lun->flags |= CTL_LUN_RESERVED;
+	lun->rsv_nexus = ctsio->io_hdr.nexus;
+
+	ctsio->scsi_status = SCSI_STATUS_OK;
+	ctsio->io_hdr.status = CTL_SUCCESS;
+
+bailout:
+	if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
+		free(ctsio->kern_data_ptr, M_CTL);
+		ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
+	}
+
+	mtx_unlock(&ctl_softc->ctl_lock);
+
+	ctl_done((union ctl_io *)ctsio);
+	return (CTL_RETVAL_COMPLETE);
+}
+
+int
+ctl_start_stop(struct ctl_scsiio *ctsio)
+{
+	struct scsi_start_stop_unit *cdb;
+	struct ctl_lun *lun;
+	struct ctl_softc *ctl_softc;
+	int retval;
+
+	CTL_DEBUG_PRINT(("ctl_start_stop\n"));
+
+	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
+	ctl_softc = control_softc;
+	retval = 0;
+
+	cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
+
+	/*
+	 * XXX KDM
+	 * We don't support the immediate bit on a stop unit.  In order to
+	 * do that, we would need to code up a way to know that a stop is
+	 * pending, and hold off any new commands until it completes, one
+	 * way or another.  Then we could accept or reject those commands
+	 * depending on its status.  We would almost need to do the reverse
+	 * of what we do below for an immediate start -- return the copy of
+	 * the ctl_io to the FETD with status to send to the host (and to
+	 * free the copy!) and then free the original I/O once the stop
+	 * actually completes.  That way, the OOA queue mechanism can work
+	 * to block commands that shouldn't proceed.  Another alternative
+	 * would be to put the copy in the queue in place of the original,
+	 * and return the original back to the caller.  That could be
+	 * slightly safer..
+	 */
+	if ((cdb->byte2 & SSS_IMMED)
+	 && ((cdb->how & SSS_START) == 0)) {
+		ctl_set_invalid_field(ctsio,
+				      /*sks_valid*/ 1,
+				      /*command*/ 1,
+				      /*field*/ 1,
+				      /*bit_valid*/ 1,
+				      /*bit*/ 0);
+		ctl_done((union ctl_io *)ctsio);
+		return (CTL_RETVAL_COMPLETE);
+	}
+
+	/*
+	 * We don't support the power conditions field.  We need to check
+	 * this prior to checking the load/eject and start/stop bits.
+	 */
+	if ((cdb->how & SSS_PC_MASK) != SSS_PC_START_VALID) {
+		ctl_set_invalid_field(ctsio,
+				      /*sks_valid*/ 1,
+				      /*command*/ 1,
+				      /*field*/ 4,
+				      /*bit_valid*/ 1,
+				      /*bit*/ 4);
+		ctl_done((union ctl_io *)ctsio);
+		return (CTL_RETVAL_COMPLETE);
+	}
+
+	/*
+	 * Media isn't removable, so we can't load or eject it.
+	 */
+	if ((cdb->how & SSS_LOEJ) != 0) {
+		ctl_set_invalid_field(ctsio,
+				      /*sks_valid*/ 1,
+				      /*command*/ 1,
+				      /*field*/ 4,
+				      /*bit_valid*/ 1,
+				      /*bit*/ 1);
+		ctl_done((union ctl_io *)ctsio);
+		return (CTL_RETVAL_COMPLETE);
+	}
+
+	if ((lun->flags & CTL_LUN_PR_RESERVED)
+	 && ((cdb->how & SSS_START)==0)) {
+		uint32_t residx;
+
+		residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
+		if (!lun->per_res[residx].registered
+		 || (lun->pr_res_idx!=residx && lun->res_type < 4)) {
+
+			ctl_set_reservation_conflict(ctsio);
+			ctl_done((union ctl_io *)ctsio);
+			return (CTL_RETVAL_COMPLETE);
+		}
+	}
+
+	/*
+	 * If there is no backend on this device, we can't start or stop
+	 * it.  In theory we shouldn't get any start/stop commands in the
+	 * first place at this level if the LUN doesn't have a backend.
+	 * That should get stopped by the command decode code.
+	 */
+	if (lun->backend == NULL) {
+		ctl_set_invalid_opcode(ctsio);
+		ctl_done((union ctl_io *)ctsio);
+		return (CTL_RETVAL_COMPLETE);
+	}
+
+	/*
+	 * XXX KDM Copan-specific offline behavior.
+	 * Figure out a reasonable way to port this?
+	 */
+#ifdef NEEDTOPORT
+	mtx_lock(&ctl_softc->ctl_lock);
+
+	if (((cdb->byte2 & SSS_ONOFFLINE) == 0)
+	 && (lun->flags & CTL_LUN_OFFLINE)) {
+		/*
+		 * If the LUN is offline, and the on/offline bit isn't set,
+		 * reject the start or stop.  Otherwise, let it through.
+		 */
+		mtx_unlock(&ctl_softc->ctl_lock);
+		ctl_set_lun_not_ready(ctsio);
+		ctl_done((union ctl_io *)ctsio);
+	} else {
+		mtx_unlock(&ctl_softc->ctl_lock);
+#endif /* NEEDTOPORT */
+		/*
+		 * This could be a start or a stop when we're online,
+		 * or a stop/offline or start/online.  A start or stop when
+		 * we're offline is covered in the case above.
+		 */
+		/*
+		 * In the non-immediate case, we send the request to
+		 * the backend and return status to the user when
+		 * it is done.
+		 *
+		 * In the immediate case, we allocate a new ctl_io
+		 * to hold a copy of the request, and send that to
+		 * the backend.  We then set good status on the
+		 * user's request and return it immediately.
+		 */
+		if (cdb->byte2 & SSS_IMMED) {
+			union ctl_io *new_io;
+
+			new_io = ctl_alloc_io(ctsio->io_hdr.pool);
+			if (new_io == NULL) {
+				ctl_set_busy(ctsio);
+				ctl_done((union ctl_io *)ctsio);
+			} else {
+				ctl_copy_io((union ctl_io *)ctsio,
+					    new_io);
+				retval = lun->backend->config_write(new_io);
+				ctl_set_success(ctsio);
+				ctl_done((union ctl_io *)ctsio);
+			}
+		} else {
+			retval = lun->backend->config_write(
+				(union ctl_io *)ctsio);
+		}
+#ifdef NEEDTOPORT
+	}
+#endif
+	return (retval);
+}
+
+/*
+ * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
+ * we don't really do anything with the LBA and length fields if the user
+ * passes them in.  Instead we'll just flush out the cache for the entire
+ * LUN.
+ */
+int
+ctl_sync_cache(struct ctl_scsiio *ctsio)
+{
+	struct ctl_lun *lun;
+	struct ctl_softc *ctl_softc;
+	uint64_t starting_lba;
+	uint32_t block_count;
+	int reladr, immed;
+	int retval;
+
+	CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
+
+	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
+	ctl_softc = control_softc;
+	retval = 0;
+	reladr = 0;
+	immed = 0;
+
+	switch (ctsio->cdb[0]) {
+	case SYNCHRONIZE_CACHE: {
+		struct scsi_sync_cache *cdb;
+		cdb = (struct scsi_sync_cache *)ctsio->cdb;
+
+		if (cdb->byte2 & SSC_RELADR)
+			reladr = 1;
+
+		if (cdb->byte2 & SSC_IMMED)
+			immed = 1;
+
+		starting_lba = scsi_4btoul(cdb->begin_lba);
+		block_count = scsi_2btoul(cdb->lb_count);
+		break;
+	}
+	case SYNCHRONIZE_CACHE_16: {
+		struct scsi_sync_cache_16 *cdb;
+		cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
+
+		if (cdb->byte2 & SSC_RELADR)
+			reladr = 1;
+
+		if (cdb->byte2 & SSC_IMMED)
+			immed = 1;
+
+		starting_lba = scsi_8btou64(cdb->begin_lba);
+		block_count = scsi_4btoul(cdb->lb_count);
+		break;
+	}
+	default:
+		ctl_set_invalid_opcode(ctsio);
+		ctl_done((union ctl_io *)ctsio);
+		goto bailout;
+		break; /* NOTREACHED */
+	}
+
+	if (immed) {
+		/*
+		 * We don't support the immediate bit.  Since it's in the
+		 * same place for the 10 and 16 byte SYNCHRONIZE CACHE
+		 * commands, we can just return the same error in either
+		 * case.
+		 */
+		ctl_set_invalid_field(ctsio,
+				      /*sks_valid*/ 1,
+				      /*command*/ 1,
+				      /*field*/ 1,
+				      /*bit_valid*/ 1,
+				      /*bit*/ 1);
+		ctl_done((union ctl_io *)ctsio);
+		goto bailout;
+	}
+
+	if (reladr) {
+		/*
+		 * We don't support the reladr bit either.  It can only be
+		 * used with linked commands, and we don't support linked
+		 * commands.  Since the bit is in the same place for the
+		 * 10 and 16 byte SYNCHRONIZE CACHE * commands, we can
+		 * just return the same error in either case.
+		 */
+		ctl_set_invalid_field(ctsio,
+				      /*sks_valid*/ 1,
+				      /*command*/ 1,
+				      /*field*/ 1,
+				      /*bit_valid*/ 1,
+				      /*bit*/ 0);
+		ctl_done((union ctl_io *)ctsio);
+		goto bailout;
+	}
+
+	/*
+	 * We check the LBA and length, but don't do anything with them.
+	 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
+	 * get flushed.  This check will just help satisfy anyone who wants
+	 * to see an error for an out of range LBA.
+	 */
+	if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
+		ctl_set_lba_out_of_range(ctsio);
+		ctl_done((union ctl_io *)ctsio);
+		goto bailout;
+	}
+
+	/*
+	 * If this LUN has no backend, we can't flush the cache anyway.
+	 */
+	if (lun->backend == NULL) {
+		ctl_set_invalid_opcode(ctsio);
+		ctl_done((union ctl_io *)ctsio);
+		goto bailout;
+	}
+
+	/*
+	 * Check to see whether we're configured to send the SYNCHRONIZE
+	 * CACHE command directly to the back end.
+	 */
+	mtx_lock(&ctl_softc->ctl_lock);
+	if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC)
+	 && (++(lun->sync_count) >= lun->sync_interval)) {
+		lun->sync_count = 0;
+		mtx_unlock(&ctl_softc->ctl_lock);
+		retval = lun->backend->config_write((union ctl_io *)ctsio);
+	} else {
+		mtx_unlock(&ctl_softc->ctl_lock);
+		ctl_set_success(ctsio);
+		ctl_done((union ctl_io *)ctsio);
+	}
+
+bailout:
+
+	return (retval);
+}
+
+int
+ctl_format(struct ctl_scsiio *ctsio)
+{
+	struct scsi_format *cdb;
+	struct ctl_lun *lun;
+	struct ctl_softc *ctl_softc;
+	int length, defect_list_len;
+
+	CTL_DEBUG_PRINT(("ctl_format\n"));
+
+	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
+	ctl_softc = control_softc;
+
+	cdb = (struct scsi_format *)ctsio->cdb;
+
+	length = 0;
+	if (cdb->byte2 & SF_FMTDATA) {
+		if (cdb->byte2 & SF_LONGLIST)
+			length = sizeof(struct scsi_format_header_long);
+		else
+			length = sizeof(struct scsi_format_header_short);
+	}
+
+	if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
+	 && (length > 0)) {
+		ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
+		if (ctsio->kern_data_ptr == NULL) {
+			ctsio->io_hdr.status = CTL_SCSI_ERROR;
+			ctsio->io_hdr.status = SCSI_STATUS_BUSY;
+			ctl_done((union ctl_io *)ctsio);
+			return (CTL_RETVAL_COMPLETE);
+		}
+		ctsio->kern_data_len = length;
+		ctsio->kern_total_len = length;
+		ctsio->kern_data_resid = 0;
+		ctsio->kern_rel_offset = 0;
+		ctsio->kern_sg_entries = 0;
+		ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
+		ctsio->be_move_done = ctl_config_move_done;
+		ctl_datamove((union ctl_io *)ctsio);
+
+		return (CTL_RETVAL_COMPLETE);
+	}
+
+	defect_list_len = 0;
+
+	if (cdb->byte2 & SF_FMTDATA) {
+		if (cdb->byte2 & SF_LONGLIST) {
+			struct scsi_format_header_long *header;
+
+			header = (struct scsi_format_header_long *)
+				ctsio->kern_data_ptr;
+
+			defect_list_len = scsi_4btoul(header->defect_list_len);
+			if (defect_list_len != 0) {
+				ctl_set_invalid_field(ctsio,
+						      /*sks_valid*/ 1,
+						      /*command*/ 0,
+						      /*field*/ 2,
+						      /*bit_valid*/ 0,
+						      /*bit*/ 0);
+				goto bailout;
+			}
+		} else {
+			struct scsi_format_header_short *header;
+
+			header = (struct scsi_format_header_short *)
+				ctsio->kern_data_ptr;
+
+			defect_list_len = scsi_2btoul(header->defect_list_len);
+			if (defect_list_len != 0) {
+				ctl_set_invalid_field(ctsio,
+						      /*sks_valid*/ 1,
+						      /*command*/ 0,
+						      /*field*/ 2,
+						      /*bit_valid*/ 0,
+						      /*bit*/ 0);
+				goto bailout;
+			}
+		}
+	}
+
+	/*
+	 * The format command will clear out the "Medium format corrupted"
+	 * status if set by the configuration code.  That status is really
+	 * just a way to notify the host that we have lost the media, and
+	 * get them to issue a command that will basically make them think
+	 * they're blowing away the media.
+	 */
+	mtx_lock(&ctl_softc->ctl_lock);
+	lun->flags &= ~CTL_LUN_INOPERABLE;
+	mtx_unlock(&ctl_softc->ctl_lock);
+
+	ctsio->scsi_status = SCSI_STATUS_OK;
+	ctsio->io_hdr.status = CTL_SUCCESS;
+bailout:
+
+	if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
+		free(ctsio->kern_data_ptr, M_CTL);
+		ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
+	}
+
+	ctl_done((union ctl_io *)ctsio);
+	return (CTL_RETVAL_COMPLETE);
+}
+
+int
+ctl_write_buffer(struct ctl_scsiio *ctsio)
+{
+	struct scsi_write_buffer *cdb;
+	struct copan_page_header *header;
+	struct ctl_lun *lun;
+	struct ctl_softc *ctl_softc;
+	int buffer_offset, len;
+	int retval;
+
+	header = NULL;
+
+	retval = CTL_RETVAL_COMPLETE;
+
+	CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
+
+	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
+	ctl_softc = control_softc;
+	cdb = (struct scsi_write_buffer *)ctsio->cdb;
+
+	if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) {
+		ctl_set_invalid_field(ctsio,
+				      /*sks_valid*/ 1,
+				      /*command*/ 1,
+				      /*field*/ 1,
+				      /*bit_valid*/ 1,
+				      /*bit*/ 4);
+		ctl_done((union ctl_io *)ctsio);
+		return (CTL_RETVAL_COMPLETE);
+	}
+	if (cdb->buffer_id != 0) {
+		ctl_set_invalid_field(ctsio,
+				      /*sks_valid*/ 1,
+				      /*command*/ 1,
+				      /*field*/ 2,
+				      /*bit_valid*/ 0,
+				      /*bit*/ 0);
+		ctl_done((union ctl_io *)ctsio);
+		return (CTL_RETVAL_COMPLETE);
+	}
+
+	len = scsi_3btoul(cdb->length);
+	buffer_offset = scsi_3btoul(cdb->offset);
+
+	if (len > sizeof(lun->write_buffer)) {
+		ctl_set_invalid_field(ctsio,
+				      /*sks_valid*/ 1,
+				      /*command*/ 1,
+				      /*field*/ 6,
+				      /*bit_valid*/ 0,
+				      /*bit*/ 0);
+		ctl_done((union ctl_io *)ctsio);
+		return (CTL_RETVAL_COMPLETE);
+	}
+
+	if (buffer_offset != 0) {
+		ctl_set_invalid_field(ctsio,
+				      /*sks_valid*/ 1,
+				      /*command*/ 1,
+				      /*field*/ 3,
+				      /*bit_valid*/ 0,
+				      /*bit*/ 0);
+		ctl_done((union ctl_io *)ctsio);
+		return (CTL_RETVAL_COMPLETE);
+	}
+
+	/*
+	 * If we've got a kernel request that hasn't been malloced yet,
+	 * malloc it and tell the caller the data buffer is here.
+	 */
+	if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
+		ctsio->kern_data_ptr = lun->write_buffer;
+		ctsio->kern_data_len = len;
+		ctsio->kern_total_len = len;
+		ctsio->kern_data_resid = 0;
+		ctsio->kern_rel_offset = 0;
+		ctsio->kern_sg_entries = 0;
+		ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
+		ctsio->be_move_done = ctl_config_move_done;
+		ctl_datamove((union ctl_io *)ctsio);
+
+		return (CTL_RETVAL_COMPLETE);
+	}
+
+	ctl_done((union ctl_io *)ctsio);
+
+	return (CTL_RETVAL_COMPLETE);
+}
+
+/*
+ * Note that this function currently doesn't actually do anything inside
+ * CTL to enforce things if the DQue bit is turned on.
+ *
+ * Also note that this function can't be used in the default case, because
+ * the DQue bit isn't set in the changeable mask for the control mode page
+ * anyway.  This is just here as an example for how to implement a page
+ * handler, and a placeholder in case we want to allow the user to turn
+ * tagged queueing on and off.
+ *
+ * The D_SENSE bit handling is functional, however, and will turn
+ * descriptor sense on and off for a given LUN.
+ */
+int
+ctl_control_page_handler(struct ctl_scsiio *ctsio,
+			 struct ctl_page_index *page_index, uint8_t *page_ptr)
+{
+	struct scsi_control_page *current_cp, *saved_cp, *user_cp;
+	struct ctl_lun *lun;
+	struct ctl_softc *softc;
+	int set_ua;
+	uint32_t initidx;
+
+	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
+	initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
+	set_ua = 0;
+
+	user_cp = (struct scsi_control_page *)page_ptr;
+	current_cp = (struct scsi_control_page *)
+		(page_index->page_data + (page_index->page_len *
+		CTL_PAGE_CURRENT));
+	saved_cp = (struct scsi_control_page *)
+		(page_index->page_data + (page_index->page_len *
+		CTL_PAGE_SAVED));
+
+	softc = control_softc;
+
+	mtx_lock(&softc->ctl_lock);
+	if (((current_cp->rlec & SCP_DSENSE) == 0)
+	 && ((user_cp->rlec & SCP_DSENSE) != 0)) {
+		/*
+		 * Descriptor sense is currently turned off and the user
+		 * wants to turn it on.
+		 */
+		current_cp->rlec |= SCP_DSENSE;
+		saved_cp->rlec |= SCP_DSENSE;
+		lun->flags |= CTL_LUN_SENSE_DESC;
+		set_ua = 1;
+	} else if (((current_cp->rlec & SCP_DSENSE) != 0)
+		&& ((user_cp->rlec & SCP_DSENSE) == 0)) {
+		/*
+		 * Descriptor sense is currently turned on, and the user
+		 * wants to turn it off.
+		 */
+		current_cp->rlec &= ~SCP_DSENSE;
+		saved_cp->rlec &= ~SCP_DSENSE;
+		lun->flags &= ~CTL_LUN_SENSE_DESC;
+		set_ua = 1;
+	}
+	if (current_cp->queue_flags & SCP_QUEUE_DQUE) {
+		if (user_cp->queue_flags & SCP_QUEUE_DQUE) {
+#ifdef NEEDTOPORT
+			csevent_log(CSC_CTL | CSC_SHELF_SW |
+				    CTL_UNTAG_TO_UNTAG,
+				    csevent_LogType_Trace,
+				    csevent_Severity_Information,
+				    csevent_AlertLevel_Green,
+				    csevent_FRU_Firmware,
+				    csevent_FRU_Unknown,
+				    "Received untagged to untagged transition");
+#endif /* NEEDTOPORT */
+		} else {
+#ifdef NEEDTOPORT
+			csevent_log(CSC_CTL | CSC_SHELF_SW |
+				    CTL_UNTAG_TO_TAG,
+				    csevent_LogType_ConfigChange,
+				    csevent_Severity_Information,
+				    csevent_AlertLevel_Green,
+				    csevent_FRU_Firmware,
+				    csevent_FRU_Unknown,
+				    "Received untagged to tagged "
+				    "queueing transition");
+#endif /* NEEDTOPORT */
+
+			current_cp->queue_flags &= ~SCP_QUEUE_DQUE;
+			saved_cp->queue_flags &= ~SCP_QUEUE_DQUE;
+			set_ua = 1;
+		}
+	} else {
+		if (user_cp->queue_flags & SCP_QUEUE_DQUE) {
+#ifdef NEEDTOPORT
+			csevent_log(CSC_CTL | CSC_SHELF_SW |
+				    CTL_TAG_TO_UNTAG,
+				    csevent_LogType_ConfigChange,
+				    csevent_Severity_Warning,
+				    csevent_AlertLevel_Yellow,
+				    csevent_FRU_Firmware,
+				    csevent_FRU_Unknown,
+				    "Received tagged queueing to untagged "
+				    "transition");
+#endif /* NEEDTOPORT */
+
+			current_cp->queue_flags |= SCP_QUEUE_DQUE;
+			saved_cp->queue_flags |= SCP_QUEUE_DQUE;
+			set_ua = 1;
+		} else {
+#ifdef NEEDTOPORT
+			csevent_log(CSC_CTL | CSC_SHELF_SW |
+				    CTL_TAG_TO_TAG,
+				    csevent_LogType_Trace,
+				    csevent_Severity_Information,
+				    csevent_AlertLevel_Green,
+				    csevent_FRU_Firmware,
+				    csevent_FRU_Unknown,
+				    "Received tagged queueing to tagged "
+				    "queueing transition");
+#endif /* NEEDTOPORT */
+		}
+	}
+	if (set_ua != 0) {
+		int i;
+		/*
+		 * Let other initiators know that the mode
+		 * parameters for this LUN have changed.
+		 */
+		for (i = 0; i < CTL_MAX_INITIATORS; i++) {
+			if (i == initidx)
+				continue;
+
+			lun->pending_sense[i].ua_pending |=
+				CTL_UA_MODE_CHANGE;
+		}
+	}
+	mtx_unlock(&softc->ctl_lock);
+
+	return (0);
+}
+
+int
+ctl_power_sp_handler(struct ctl_scsiio *ctsio,
+		     struct ctl_page_index *page_index, uint8_t *page_ptr)
+{
+	return (0);
+}
+
+int
+ctl_power_sp_sense_handler(struct ctl_scsiio *ctsio,
+			   struct ctl_page_index *page_index, int pc)
+{
+	struct copan_power_subpage *page;
+
+	page = (struct copan_power_subpage *)page_index->page_data +
+		(page_index->page_len * pc);
+
+	switch (pc) {
+	case SMS_PAGE_CTRL_CHANGEABLE >> 6:
+		/*
+		 * We don't update the changable bits for this page.
+		 */
+		break;
+	case SMS_PAGE_CTRL_CURRENT >> 6:
+	case SMS_PAGE_CTRL_DEFAULT >> 6:
+	case SMS_PAGE_CTRL_SAVED >> 6:
+#ifdef NEEDTOPORT
+		ctl_update_power_subpage(page);
+#endif
+		break;
+	default:
+#ifdef NEEDTOPORT
+		EPRINT(0, "Invalid PC %d!!", pc);
+#endif
+		break;
+	}
+	return (0);
+}
+
+
+int
+ctl_aps_sp_handler(struct ctl_scsiio *ctsio,
+		   struct ctl_page_index *page_index, uint8_t *page_ptr)
+{
+	struct copan_aps_subpage *user_sp;
+	struct copan_aps_subpage *current_sp;
+	union ctl_modepage_info *modepage_info;
+	struct ctl_softc *softc;
+	struct ctl_lun *lun;
+	int retval;
+
+	retval = CTL_RETVAL_COMPLETE;
+	current_sp = (struct copan_aps_subpage *)(page_index->page_data +
+		     (page_index->page_len * CTL_PAGE_CURRENT));
+	softc = control_softc;
+	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
+
+	user_sp = (struct copan_aps_subpage *)page_ptr;
+
+	modepage_info = (union ctl_modepage_info *)
+		ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
+
+	modepage_info->header.page_code = page_index->page_code & SMPH_PC_MASK;
+	modepage_info->header.subpage = page_index->subpage;
+	modepage_info->aps.lock_active = user_sp->lock_active;
+
+	mtx_lock(&softc->ctl_lock);
+
+	/*
+	 * If there is a request to lock the LUN and another LUN is locked
+	 * this is an error. If the requested LUN is already locked ignore
+	 * the request. If no LUN is locked attempt to lock it.
+	 * if there is a request to unlock the LUN and the LUN is currently
+	 * locked attempt to unlock it. Otherwise ignore the request. i.e.
+	 * if another LUN is locked or no LUN is locked.
+	 */
+	if (user_sp->lock_active & APS_LOCK_ACTIVE) {
+		if (softc->aps_locked_lun == lun->lun) {
+			/*
+			 * This LUN is already locked, so we're done.
+			 */
+			retval = CTL_RETVAL_COMPLETE;
+		} else if (softc->aps_locked_lun == 0) {
+			/*
+			 * No one has the lock, pass the request to the
+			 * backend.
+			 */
+			retval = lun->backend->config_write(
+				(union ctl_io *)ctsio);
+		} else {
+			/*
+			 * Someone else has the lock, throw out the request.
+			 */
+			ctl_set_already_locked(ctsio);
+			free(ctsio->kern_data_ptr, M_CTL);
+			ctl_done((union ctl_io *)ctsio);
+
+			/*
+			 * Set the return value so that ctl_do_mode_select()
+			 * won't try to complete the command.  We already
+			 * completed it here.
+			 */
+			retval = CTL_RETVAL_ERROR;
+		}
+	} else if (softc->aps_locked_lun == lun->lun) {
+		/*
+		 * This LUN is locked, so pass the unlock request to the
+		 * backend.
+		 */
+		retval = lun->backend->config_write((union ctl_io *)ctsio);
+	}
+	mtx_unlock(&softc->ctl_lock);
+
+	return (retval);
+}
+
+int
+ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio,
+				struct ctl_page_index *page_index,
+				uint8_t *page_ptr)
+{
+	uint8_t *c;
+	int i;
+
+	c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs;
+	ctl_time_io_secs =
+		(c[0] << 8) |
+		(c[1] << 0) |
+		0;
+	CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs));
+	printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs);
+	printf("page data:");
+	for (i=0; i<8; i++)
+		printf(" %.2x",page_ptr[i]);
+	printf("\n");
+	return (0);
+}
+
+int
+ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio,
+			       struct ctl_page_index *page_index,
+			       int pc)
+{
+	struct copan_debugconf_subpage *page;
+
+	page = (struct copan_debugconf_subpage *)page_index->page_data +
+		(page_index->page_len * pc);
+
+	switch (pc) {
+	case SMS_PAGE_CTRL_CHANGEABLE >> 6:
+	case SMS_PAGE_CTRL_DEFAULT >> 6:
+	case SMS_PAGE_CTRL_SAVED >> 6:
+		/*
+		 * We don't update the changable or default bits for this page.
+		 */
+		break;
+	case SMS_PAGE_CTRL_CURRENT >> 6:
+		page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8;
+		page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0;
+		break;
+	default:
+#ifdef NEEDTOPORT
+		EPRINT(0, "Invalid PC %d!!", pc);
+#endif /* NEEDTOPORT */
+		break;
+	}
+	return (0);
+}
+
+
+static int
+ctl_do_mode_select(union ctl_io *io)
+{
+	struct scsi_mode_page_header *page_header;
+	struct ctl_page_index *page_index;
+	struct ctl_scsiio *ctsio;
+	int control_dev, page_len;
+	int page_len_offset, page_len_size;
+	union ctl_modepage_info *modepage_info;
+	struct ctl_lun *lun;
+	int *len_left, *len_used;
+	int retval, i;
+
+	ctsio = &io->scsiio;
+	page_index = NULL;
+	page_len = 0;
+	retval = CTL_RETVAL_COMPLETE;
+
+	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
+
+	if (lun->be_lun->lun_type != T_DIRECT)
+		control_dev = 1;
+	else
+		control_dev = 0;
+
+	modepage_info = (union ctl_modepage_info *)
+		ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
+	len_left = &modepage_info->header.len_left;
+	len_used = &modepage_info->header.len_used;
+
+do_next_page:
+
+	page_header = (struct scsi_mode_page_header *)
+		(ctsio->kern_data_ptr + *len_used);
+
+	if (*len_left == 0) {
+		free(ctsio->kern_data_ptr, M_CTL);
+		ctl_set_success(ctsio);
+		ctl_done((union ctl_io *)ctsio);
+		return (CTL_RETVAL_COMPLETE);
+	} else if (*len_left < sizeof(struct scsi_mode_page_header)) {
+
+		free(ctsio->kern_data_ptr, M_CTL);
+		ctl_set_param_len_error(ctsio);
+		ctl_done((union ctl_io *)ctsio);
+		return (CTL_RETVAL_COMPLETE);
+
+	} else if ((page_header->page_code & SMPH_SPF)
+		&& (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
+
+		free(ctsio->kern_data_ptr, M_CTL);
+		ctl_set_param_len_error(ctsio);
+		ctl_done((union ctl_io *)ctsio);
+		return (CTL_RETVAL_COMPLETE);
+	}
+
+
+	/*
+	 * XXX KDM should we do something with the block descriptor?
+	 */
+	for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
+
+		if ((control_dev != 0)
+		 && (lun->mode_pages.index[i].page_flags &
+		     CTL_PAGE_FLAG_DISK_ONLY))
+			continue;
+
+		if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
+		    (page_header->page_code & SMPH_PC_MASK))
+			continue;
+
+		/*
+		 * If neither page has a subpage code, then we've got a
+		 * match.
+		 */
+		if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0)
+		 && ((page_header->page_code & SMPH_SPF) == 0)) {
+			page_index = &lun->mode_pages.index[i];
+			page_len = page_header->page_length;
+			break;
+		}
+
+		/*
+		 * If both pages have subpages, then the subpage numbers
+		 * have to match.
+		 */
+		if ((lun->mode_pages.index[i].page_code & SMPH_SPF)
+		  && (page_header->page_code & SMPH_SPF)) {
+			struct scsi_mode_page_header_sp *sph;
+
+			sph = (struct scsi_mode_page_header_sp *)page_header;
+
+			if (lun->mode_pages.index[i].subpage ==
+			    sph->subpage) {
+				page_index = &lun->mode_pages.index[i];
+				page_len = scsi_2btoul(sph->page_length);
+				break;
+			}
+		}
+	}
+
+	/*
+	 * If we couldn't find the page, or if we don't have a mode select
+	 * handler for it, send back an error to the user.
+	 */
+	if ((page_index == NULL)
+	 || (page_index->select_handler == NULL)) {
+		ctl_set_invalid_field(ctsio,
+				      /*sks_valid*/ 1,
+				      /*command*/ 0,
+				      /*field*/ *len_used,
+				      /*bit_valid*/ 0,
+				      /*bit*/ 0);
+		free(ctsio->kern_data_ptr, M_CTL);
+		ctl_done((union ctl_io *)ctsio);
+		return (CTL_RETVAL_COMPLETE);
+	}
+
+	if (page_index->page_code & SMPH_SPF) {
+		page_len_offset = 2;
+		page_len_size = 2;
+	} else {
+		page_len_size = 1;
+		page_len_offset = 1;
+	}
+
+	/*
+	 * If the length the initiator gives us isn't the one we specify in
+	 * the mode page header, or if they didn't specify enough data in
+	 * the CDB to avoid truncating this page, kick out the request.
+	 */
+	if ((page_len != (page_index->page_len - page_len_offset -
+			  page_len_size))
+	 || (*len_left < page_index->page_len)) {
+
+
+		ctl_set_invalid_field(ctsio,
+				      /*sks_valid*/ 1,
+				      /*command*/ 0,
+				      /*field*/ *len_used + page_len_offset,
+				      /*bit_valid*/ 0,
+				      /*bit*/ 0);
+		free(ctsio->kern_data_ptr, M_CTL);
+		ctl_done((union ctl_io *)ctsio);
+		return (CTL_RETVAL_COMPLETE);
+	}
+
+	/*
+	 * Run through the mode page, checking to make sure that the bits
+	 * the user changed are actually legal for him to change.
+	 */
+	for (i = 0; i < page_index->page_len; i++) {
+		uint8_t *user_byte, *change_mask, *current_byte;
+		int bad_bit;
+		int j;
+
+		user_byte = (uint8_t *)page_header + i;
+		change_mask = page_index->page_data +
+			      (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
+		current_byte = page_index->page_data +
+			       (page_index->page_len * CTL_PAGE_CURRENT) + i;
+
+		/*
+		 * Check to see whether the user set any bits in this byte
+		 * that he is not allowed to set.
+		 */
+		if ((*user_byte & ~(*change_mask)) ==
+		    (*current_byte & ~(*change_mask)))
+			continue;
+
+		/*
+		 * Go through bit by bit to determine which one is illegal.
+		 */
+		bad_bit = 0;
+		for (j = 7; j >= 0; j--) {
+			if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
+			    (((1 << i) & ~(*change_mask)) & *current_byte)) {
+				bad_bit = i;
+				break;
+			}
+		}
+		ctl_set_invalid_field(ctsio,
+				      /*sks_valid*/ 1,
+				      /*command*/ 0,
+				      /*field*/ *len_used + i,
+				      /*bit_valid*/ 1,
+				      /*bit*/ bad_bit);
+		free(ctsio->kern_data_ptr, M_CTL);
+		ctl_done((union ctl_io *)ctsio);
+		return (CTL_RETVAL_COMPLETE);
+	}
+
+	/*
+	 * Decrement these before we call the page handler, since we may
+	 * end up getting called back one way or another before the handler
+	 * returns to this context.
+	 */
+	*len_left -= page_index->page_len;
+	*len_used += page_index->page_len;
+
+	retval = page_index->select_handler(ctsio, page_index,
+					    (uint8_t *)page_header);
+
+	/*
+	 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
+	 * wait until this queued command completes to finish processing
+	 * the mode page.  If it returns anything other than
+	 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
+	 * already set the sense information, freed the data pointer, and
+	 * completed the io for us.
+	 */
+	if (retval != CTL_RETVAL_COMPLETE)
+		goto bailout_no_done;
+
+	/*
+	 * If the initiator sent us more than one page, parse the next one.
+	 */
+	if (*len_left > 0)
+		goto do_next_page;
+
+	ctl_set_success(ctsio);
+	free(ctsio->kern_data_ptr, M_CTL);
+	ctl_done((union ctl_io *)ctsio);
+
+bailout_no_done:
+
+	return (CTL_RETVAL_COMPLETE);
+
+}
+
+int
+ctl_mode_select(struct ctl_scsiio *ctsio)
+{
+	int param_len, pf, sp;
+	int header_size, bd_len;
+	int len_left, len_used;
+	struct ctl_page_index *page_index;
+	struct ctl_lun *lun;
+	int control_dev, page_len;
+	union ctl_modepage_info *modepage_info;
+	int retval;
+
+	pf = 0;
+	sp = 0;
+	page_len = 0;
+	len_used = 0;
+	len_left = 0;
+	retval = 0;
+	bd_len = 0;
+	page_index = NULL;
+
+	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
+
+	if (lun->be_lun->lun_type != T_DIRECT)
+		control_dev = 1;
+	else
+		control_dev = 0;
+
+	switch (ctsio->cdb[0]) {
+	case MODE_SELECT_6: {
+		struct scsi_mode_select_6 *cdb;
+
+		cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
+
+		pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
+		sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
+
+		param_len = cdb->length;
+		header_size = sizeof(struct scsi_mode_header_6);
+		break;
+	}
+	case MODE_SELECT_10: {
+		struct scsi_mode_select_10 *cdb;
+
+		cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
+
+		pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
+		sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
+
+		param_len = scsi_2btoul(cdb->length);
+		header_size = sizeof(struct scsi_mode_header_10);
+		break;
+	}
+	default:
+		ctl_set_invalid_opcode(ctsio);
+		ctl_done((union ctl_io *)ctsio);
+		return (CTL_RETVAL_COMPLETE);
+		break; /* NOTREACHED */
+	}
+
+	/*
+	 * From SPC-3:
+	 * "A parameter list length of zero indicates that the Data-Out Buffer
+	 * shall be empty. This condition shall not be considered as an error."
+	 */
+	if (param_len == 0) {
+		ctl_set_success(ctsio);
+		ctl_done((union ctl_io *)ctsio);
+		return (CTL_RETVAL_COMPLETE);
+	}
+
+	/*
+	 * Since we'll hit this the first time through, prior to
+	 * allocation, we don't need to free a data buffer here.
+	 */
+	if (param_len < header_size) {
+		ctl_set_param_len_error(ctsio);
+		ctl_done((union ctl_io *)ctsio);
+		return (CTL_RETVAL_COMPLETE);
+	}
+
+	/*
+	 * Allocate the data buffer and grab the user's data.  In theory,
+	 * we shouldn't have to sanity check the parameter list length here
+	 * because the maximum size is 64K.  We should be able to malloc
+	 * that much without too many problems.
+	 */
+	if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
+		ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
+		if (ctsio->kern_data_ptr == NULL) {
+			ctl_set_busy(ctsio);
+			ctl_done((union ctl_io *)ctsio);
+			return (CTL_RETVAL_COMPLETE);
+		}
+		ctsio->kern_data_len = param_len;
+		ctsio->kern_total_len = param_len;
+		ctsio->kern_data_resid = 0;
+		ctsio->kern_rel_offset = 0;
+		ctsio->kern_sg_entries = 0;
+		ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
+		ctsio->be_move_done = ctl_config_move_done;
+		ctl_datamove((union ctl_io *)ctsio);
+
+		return (CTL_RETVAL_COMPLETE);
+	}
+
+	switch (ctsio->cdb[0]) {
+	case MODE_SELECT_6: {
+		struct scsi_mode_header_6 *mh6;
+
+		mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
+		bd_len = mh6->blk_desc_len;
+		break;
+	}
+	case MODE_SELECT_10: {
+		struct scsi_mode_header_10 *mh10;
+
+		mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
+		bd_len = scsi_2btoul(mh10->blk_desc_len);
+		break;
+	}
+	default:
+		panic("Invalid CDB type %#x", ctsio->cdb[0]);
+		break;
+	}
+
+	if (param_len < (header_size + bd_len)) {
+		free(ctsio->kern_data_ptr, M_CTL);
+		ctl_set_param_len_error(ctsio);
+		ctl_done((union ctl_io *)ctsio);
+		return (CTL_RETVAL_COMPLETE);
+	}
+
+	/*
+	 * Set the IO_CONT flag, so that if this I/O gets passed to
+	 * ctl_config_write_done(), it'll get passed back to
+	 * ctl_do_mode_select() for further processing, or completion if
+	 * we're all done.
+	 */
+	ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
+	ctsio->io_cont = ctl_do_mode_select;
+
+	modepage_info = (union ctl_modepage_info *)
+		ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
+
+	memset(modepage_info, 0, sizeof(*modepage_info));
+
+	len_left = param_len - header_size - bd_len;
+	len_used = header_size + bd_len;
+
+	modepage_info->header.len_left = len_left;
+	modepage_info->header.len_used = len_used;
+
+	return (ctl_do_mode_select((union ctl_io *)ctsio));
+}
+
+int
+ctl_mode_sense(struct ctl_scsiio *ctsio)
+{
+	struct ctl_lun *lun;
+	int pc, page_code, dbd, llba, subpage;
+	int alloc_len, page_len, header_len, total_len;
+	struct scsi_mode_block_descr *block_desc;
+	struct ctl_page_index *page_index;
+	int control_dev;
+
+	dbd = 0;
+	llba = 0;
+	block_desc = NULL;
+	page_index = NULL;
+
+	CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
+
+	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
+
+	if (lun->be_lun->lun_type != T_DIRECT)
+		control_dev = 1;
+	else
+		control_dev = 0;
+
+	switch (ctsio->cdb[0]) {
+	case MODE_SENSE_6: {
+		struct scsi_mode_sense_6 *cdb;
+
+		cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
+
+		header_len = sizeof(struct scsi_mode_hdr_6);
+		if (cdb->byte2 & SMS_DBD)
+			dbd = 1;
+		else
+			header_len += sizeof(struct scsi_mode_block_descr);
+
+		pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
+		page_code = cdb->page & SMS_PAGE_CODE;
+		subpage = cdb->subpage;
+		alloc_len = cdb->length;
+		break;
+	}
+	case MODE_SENSE_10: {
+		struct scsi_mode_sense_10 *cdb;
+
+		cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
+
+		header_len = sizeof(struct scsi_mode_hdr_10);
+
+		if (cdb->byte2 & SMS_DBD)
+			dbd = 1;
+		else
+			header_len += sizeof(struct scsi_mode_block_descr);
+		if (cdb->byte2 & SMS10_LLBAA)
+			llba = 1;
+		pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
+		page_code = cdb->page & SMS_PAGE_CODE;
+		subpage = cdb->subpage;
+		alloc_len = scsi_2btoul(cdb->length);
+		break;
+	}
+	default:
+		ctl_set_invalid_opcode(ctsio);
+		ctl_done((union ctl_io *)ctsio);
+		return (CTL_RETVAL_COMPLETE);
+		break; /* NOTREACHED */
+	}
+
+	/*
+	 * We have to make a first pass through to calculate the size of
+	 * the pages that match the user's query.  Then we allocate enough
+	 * memory to hold it, and actually copy the data into the buffer.
+	 */
+	switch (page_code) {
+	case SMS_ALL_PAGES_PAGE: {
+		int i;
+
+		page_len = 0;
+
+		/*
+		 * At the moment, values other than 0 and 0xff here are
+		 * reserved according to SPC-3.
+		 */
+		if ((subpage != SMS_SUBPAGE_PAGE_0)
+		 && (subpage != SMS_SUBPAGE_ALL)) {
+			ctl_set_invalid_field(ctsio,
+					      /*sks_valid*/ 1,
+					      /*command*/ 1,
+					      /*field*/ 3,
+					      /*bit_valid*/ 0,
+					      /*bit*/ 0);
+			ctl_done((union ctl_io *)ctsio);
+			return (CTL_RETVAL_COMPLETE);
+		}
+
+		for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
+			if ((control_dev != 0)
+			 && (lun->mode_pages.index[i].page_flags &
+			     CTL_PAGE_FLAG_DISK_ONLY))
+				continue;
+
+			/*
+			 * We don't use this subpage if the user didn't
+			 * request all subpages.
+			 */
+			if ((lun->mode_pages.index[i].subpage != 0)
+			 && (subpage == SMS_SUBPAGE_PAGE_0))
+				continue;
+
+#if 0
+			printf("found page %#x len %d\n",
+			       lun->mode_pages.index[i].page_code &
+			       SMPH_PC_MASK,
+			       lun->mode_pages.index[i].page_len);
+#endif
+			page_len += lun->mode_pages.index[i].page_len;
+		}
+		break;
+	}
+	default: {
+		int i;
+
+		page_len = 0;
+
+		for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
+			/* Look for the right page code */
+			if ((lun->mode_pages.index[i].page_code &
+			     SMPH_PC_MASK) != page_code)
+				continue;
+
+			/* Look for the right subpage or the subpage wildcard*/
+			if ((lun->mode_pages.index[i].subpage != subpage)
+			 && (subpage != SMS_SUBPAGE_ALL))
+				continue;
+
+			/* Make sure the page is supported for this dev type */
+			if ((control_dev != 0)
+			 && (lun->mode_pages.index[i].page_flags &
+			     CTL_PAGE_FLAG_DISK_ONLY))
+				continue;
+
+#if 0
+			printf("found page %#x len %d\n",
+			       lun->mode_pages.index[i].page_code &
+			       SMPH_PC_MASK,
+			       lun->mode_pages.index[i].page_len);
+#endif
+
+			page_len += lun->mode_pages.index[i].page_len;
+		}
+
+		if (page_len == 0) {
+			ctl_set_invalid_field(ctsio,
+					      /*sks_valid*/ 1,
+					      /*command*/ 1,
+					      /*field*/ 2,
+					      /*bit_valid*/ 1,
+					      /*bit*/ 5);
+			ctl_done((union ctl_io *)ctsio);
+			return (CTL_RETVAL_COMPLETE);
+		}
+		break;
+	}
+	}
+
+	total_len = header_len + page_len;
+#if 0
+	printf("header_len = %d, page_len = %d, total_len = %d\n",
+	       header_len, page_len, total_len);
+#endif
+
+	ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK);
+	if (ctsio->kern_data_ptr == NULL) {
+		ctsio->io_hdr.status = CTL_SCSI_ERROR;
+		ctsio->scsi_status = SCSI_STATUS_BUSY;
+		ctl_done((union ctl_io *)ctsio);
+		return (CTL_RETVAL_COMPLETE);
+	}
+	ctsio->kern_sg_entries = 0;
+	ctsio->kern_data_resid = 0;
+	ctsio->kern_rel_offset = 0;
+	if (total_len < alloc_len) {
+		ctsio->residual = alloc_len - total_len;
+		ctsio->kern_data_len = total_len;
+		ctsio->kern_total_len = total_len;
+	} else {
+		ctsio->residual = 0;
+		ctsio->kern_data_len = alloc_len;
+		ctsio->kern_total_len = alloc_len;
+	}
+	memset(ctsio->kern_data_ptr, 0, total_len);
+
+	switch (ctsio->cdb[0]) {
+	case MODE_SENSE_6: {
+		struct scsi_mode_hdr_6 *header;
+
+		header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
+
+		header->datalen = ctl_min(total_len - 1, 254);
+
+		if (dbd)
+			header->block_descr_len = 0;
+		else
+			header->block_descr_len =
+				sizeof(struct scsi_mode_block_descr);
+		block_desc = (struct scsi_mode_block_descr *)&header[1];
+		break;
+	}
+	case MODE_SENSE_10: {
+		struct scsi_mode_hdr_10 *header;
+		int datalen;
+
+		header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
+
+		datalen = ctl_min(total_len - 2, 65533);
+		scsi_ulto2b(datalen, header->datalen);
+		if (dbd)
+			scsi_ulto2b(0, header->block_descr_len);
+		else
+			scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
+				    header->block_descr_len);
+		block_desc = (struct scsi_mode_block_descr *)&header[1];
+		break;
+	}
+	default:
+		panic("invalid CDB type %#x", ctsio->cdb[0]);
+		break; /* NOTREACHED */
+	}
+
+	/*
+	 * If we've got a disk, use its blocksize in the block
+	 * descriptor.  Otherwise, just set it to 0.
+	 */
+	if (dbd == 0) {
+		if (control_dev != 0)
+			scsi_ulto3b(lun->be_lun->blocksize,
+				    block_desc->block_len);
+		else
+			scsi_ulto3b(0, block_desc->block_len);
+	}
+
+	switch (page_code) {
+	case SMS_ALL_PAGES_PAGE: {
+		int i, data_used;
+
+		data_used = header_len;
+		for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
+			struct ctl_page_index *page_index;
+
+			page_index = &lun->mode_pages.index[i];
+
+			if ((control_dev != 0)
+			 && (page_index->page_flags &
+			    CTL_PAGE_FLAG_DISK_ONLY))
+				continue;
+
+			/*
+			 * We don't use this subpage if the user didn't
+			 * request all subpages.  We already checked (above)
+			 * to make sure the user only specified a subpage
+			 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
+			 */
+			if ((page_index->subpage != 0)
+			 && (subpage == SMS_SUBPAGE_PAGE_0))
+				continue;
+
+			/*
+			 * Call the handler, if it exists, to update the
+			 * page to the latest values.
+			 */
+			if (page_index->sense_handler != NULL)
+				page_index->sense_handler(ctsio, page_index,pc);
+
+			memcpy(ctsio->kern_data_ptr + data_used,
+			       page_index->page_data +
+			       (page_index->page_len * pc),
+			       page_index->page_len);
+			data_used += page_index->page_len;
+		}
+		break;
+	}
+	default: {
+		int i, data_used;
+
+		data_used = header_len;
+
+		for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
+			struct ctl_page_index *page_index;
+
+			page_index = &lun->mode_pages.index[i];
+
+			/* Look for the right page code */
+			if ((page_index->page_code & SMPH_PC_MASK) != page_code)
+				continue;
+
+			/* Look for the right subpage or the subpage wildcard*/
+			if ((page_index->subpage != subpage)
+			 && (subpage != SMS_SUBPAGE_ALL))
+				continue;
+
+			/* Make sure the page is supported for this dev type */
+			if ((control_dev != 0)
+			 && (page_index->page_flags &
+			     CTL_PAGE_FLAG_DISK_ONLY))
+				continue;
+
+			/*
+			 * Call the handler, if it exists, to update the
+			 * page to the latest values.
+			 */
+			if (page_index->sense_handler != NULL)
+				page_index->sense_handler(ctsio, page_index,pc);
+
+			memcpy(ctsio->kern_data_ptr + data_used,
+			       page_index->page_data +
+			       (page_index->page_len * pc),
+			       page_index->page_len);
+			data_used += page_index->page_len;
+		}
+		break;
+	}
+	}
+
+	ctsio->scsi_status = SCSI_STATUS_OK;
+
+	ctsio->be_move_done = ctl_config_move_done;
+	ctl_datamove((union ctl_io *)ctsio);
+
+	return (CTL_RETVAL_COMPLETE);
+}
+
+int
+ctl_read_capacity(struct ctl_scsiio *ctsio)
+{
+	struct scsi_read_capacity *cdb;
+	struct scsi_read_capacity_data *data;
+	struct ctl_lun *lun;
+	uint32_t lba;
+
+	CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
+
+	cdb = (struct scsi_read_capacity *)ctsio->cdb;
+
+	lba = scsi_4btoul(cdb->addr);
+	if (((cdb->pmi & SRC_PMI) == 0)
+	 && (lba != 0)) {
+		ctl_set_invalid_field(/*ctsio*/ ctsio,
+				      /*sks_valid*/ 1,
+				      /*command*/ 1,
+				      /*field*/ 2,
+				      /*bit_valid*/ 0,
+				      /*bit*/ 0);
+		ctl_done((union ctl_io *)ctsio);
+		return (CTL_RETVAL_COMPLETE);
+	}
+
+	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
+
+	ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK);
+	if (ctsio->kern_data_ptr == NULL) {
+		ctsio->io_hdr.status = CTL_SCSI_ERROR;
+		ctsio->scsi_status = SCSI_STATUS_BUSY;
+		ctl_done((union ctl_io *)ctsio);
+		return (CTL_RETVAL_COMPLETE);
+	}
+	data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
+	ctsio->residual = 0;
+	ctsio->kern_data_len = sizeof(*data);
+	ctsio->kern_total_len = sizeof(*data);
+	ctsio->kern_data_resid = 0;
+	ctsio->kern_rel_offset = 0;
+	ctsio->kern_sg_entries = 0;
+
+	memset(data, 0, sizeof(*data));
+
+	/*
+	 * If the maximum LBA is greater than 0xfffffffe, the user must
+	 * issue a SERVICE ACTION IN (16) command, with the read capacity
+	 * serivce action set.
+	 */
+	if (lun->be_lun->maxlba > 0xfffffffe)
+		scsi_ulto4b(0xffffffff, data->addr);
+	else
+		scsi_ulto4b(lun->be_lun->maxlba, data->addr);
+
+	/*
+	 * XXX KDM this may not be 512 bytes...
+	 */
+	scsi_ulto4b(lun->be_lun->blocksize, data->length);
+
+	ctsio->scsi_status = SCSI_STATUS_OK;
+
+	ctsio->be_move_done = ctl_config_move_done;
+	ctl_datamove((union ctl_io *)ctsio);
+
+	return (CTL_RETVAL_COMPLETE);
+}
+
+static int
+ctl_read_capacity_16(struct ctl_scsiio *ctsio)
+{
+	struct scsi_read_capacity_16 *cdb;
+	struct scsi_read_capacity_data_long *data;
+	struct ctl_lun *lun;
+	uint64_t lba;
+	uint32_t alloc_len;
+
+	CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
+
+	cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
+
+	alloc_len = scsi_4btoul(cdb->alloc_len);
+	lba = scsi_8btou64(cdb->addr);
+
+	if ((cdb->reladr & SRC16_PMI)
+	 && (lba != 0)) {
+		ctl_set_invalid_field(/*ctsio*/ ctsio,
+				      /*sks_valid*/ 1,
+				      /*command*/ 1,
+				      /*field*/ 2,
+				      /*bit_valid*/ 0,
+				      /*bit*/ 0);
+		ctl_done((union ctl_io *)ctsio);
+		return (CTL_RETVAL_COMPLETE);
+	}
+
+	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
+
+	ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK);
+	if (ctsio->kern_data_ptr == NULL) {
+		ctsio->io_hdr.status = CTL_SCSI_ERROR;
+		ctsio->scsi_status = SCSI_STATUS_BUSY;
+		ctl_done((union ctl_io *)ctsio);
+		return (CTL_RETVAL_COMPLETE);
+	}
+	data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
+
+	if (sizeof(*data) < alloc_len) {
+		ctsio->residual = alloc_len - sizeof(*data);
+		ctsio->kern_data_len = sizeof(*data);
+		ctsio->kern_total_len = sizeof(*data);
+	} else {
+		ctsio->residual = 0;
+		ctsio->kern_data_len = alloc_len;
+		ctsio->kern_total_len = alloc_len;
+	}
+	ctsio->kern_data_resid = 0;
+	ctsio->kern_rel_offset = 0;
+	ctsio->kern_sg_entries = 0;
+
+	memset(data, 0, sizeof(*data));
+
+	scsi_u64to8b(lun->be_lun->maxlba, data->addr);
+	/* XXX KDM this may not be 512 bytes... */
+	scsi_ulto4b(lun->be_lun->blocksize, data->length);
+
+	ctsio->scsi_status = SCSI_STATUS_OK;
+
+	ctsio->be_move_done = ctl_config_move_done;
+	ctl_datamove((union ctl_io *)ctsio);
+
+	return (CTL_RETVAL_COMPLETE);
+}
+
+int
+ctl_service_action_in(struct ctl_scsiio *ctsio)
+{
+	struct scsi_service_action_in *cdb;
+	int retval;
+
+	CTL_DEBUG_PRINT(("ctl_service_action_in\n"));
+
+	cdb = (struct scsi_service_action_in *)ctsio->cdb;
+
+	retval = CTL_RETVAL_COMPLETE;
+
+	switch (cdb->service_action) {
+	case SRC16_SERVICE_ACTION:
+		retval = ctl_read_capacity_16(ctsio);
+		break;
+	default:
+		ctl_set_invalid_field(/*ctsio*/ ctsio,
+				      /*sks_valid*/ 1,
+				      /*command*/ 1,
+				      /*field*/ 1,
+				      /*bit_valid*/ 1,
+				      /*bit*/ 4);
+		ctl_done((union ctl_io *)ctsio);
+		break;
+	}
+
+	return (retval);
+}
+
+int
+ctl_maintenance_in(struct ctl_scsiio *ctsio)
+{
+	struct scsi_maintenance_in *cdb;
+	int retval;
+	int alloc_len, total_len = 0;
+	int num_target_port_groups;
+	struct ctl_lun *lun;
+	struct ctl_softc *softc;
+	struct scsi_target_group_data *rtg_ptr;
+	struct scsi_target_port_group_descriptor *tpg_desc_ptr1, *tpg_desc_ptr2;
+	struct scsi_target_port_descriptor  *tp_desc_ptr1_1, *tp_desc_ptr1_2,
+	                                    *tp_desc_ptr2_1, *tp_desc_ptr2_2;
+
+	CTL_DEBUG_PRINT(("ctl_maintenance_in\n"));
+
+	cdb = (struct scsi_maintenance_in *)ctsio->cdb;
+	softc = control_softc;
+	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
+
+	retval = CTL_RETVAL_COMPLETE;
+	mtx_lock(&softc->ctl_lock);
+
+	if ((cdb->byte2 & SERVICE_ACTION_MASK) != SA_RPRT_TRGT_GRP) {
+		ctl_set_invalid_field(/*ctsio*/ ctsio,
+				      /*sks_valid*/ 1,
+				      /*command*/ 1,
+				      /*field*/ 1,
+				      /*bit_valid*/ 1,
+				      /*bit*/ 4);
+		ctl_done((union ctl_io *)ctsio);
+		return(retval);
+	}
+
+	if (ctl_is_single)
+        	num_target_port_groups = NUM_TARGET_PORT_GROUPS - 1;
+	else
+        	num_target_port_groups = NUM_TARGET_PORT_GROUPS;
+
+	total_len = sizeof(struct scsi_target_group_data) +
+		sizeof(struct scsi_target_port_group_descriptor) *
+		num_target_port_groups +
+		sizeof(struct scsi_target_port_descriptor) *
+		NUM_PORTS_PER_GRP * num_target_port_groups;
+
+	alloc_len = scsi_4btoul(cdb->length);
+
+	ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK);
+	if (ctsio->kern_data_ptr == NULL) {
+		ctsio->io_hdr.status = CTL_SCSI_ERROR;
+		ctsio->scsi_status = SCSI_STATUS_BUSY;
+		ctl_done((union ctl_io *)ctsio);
+		return (CTL_RETVAL_COMPLETE);
+	}
+	memset(ctsio->kern_data_ptr, 0, total_len);
+
+	ctsio->kern_sg_entries = 0;
+
+	if (total_len < alloc_len) {
+		ctsio->residual = alloc_len - total_len;
+		ctsio->kern_data_len = total_len;
+		ctsio->kern_total_len = total_len;
+	} else {
+		ctsio->residual = 0;
+		ctsio->kern_data_len = alloc_len;
+		ctsio->kern_total_len = alloc_len;
+	}
+	ctsio->kern_data_resid = 0;
+	ctsio->kern_rel_offset = 0;
+
+	rtg_ptr = (struct scsi_target_group_data *)ctsio->kern_data_ptr;
+
+	tpg_desc_ptr1 = &rtg_ptr->groups[0];
+	tp_desc_ptr1_1 = &tpg_desc_ptr1->descriptors[0];
+	tp_desc_ptr1_2 = (struct scsi_target_port_descriptor *)
+	        &tp_desc_ptr1_1->desc_list[0];
+
+
+
+	if (ctl_is_single == 0) {
+		tpg_desc_ptr2 = (struct scsi_target_port_group_descriptor *)
+	                &tp_desc_ptr1_2->desc_list[0];
+		tp_desc_ptr2_1 = &tpg_desc_ptr2->descriptors[0];
+		tp_desc_ptr2_2 = (struct scsi_target_port_descriptor *)
+	        	&tp_desc_ptr2_1->desc_list[0];
+        } else {
+		tpg_desc_ptr2 = NULL;
+		tp_desc_ptr2_1 = NULL;
+		tp_desc_ptr2_2 = NULL;
+	}
+
+	scsi_ulto4b(total_len - 4, rtg_ptr->length);
+	if (ctl_is_single == 0) {
+        	if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS) {
+			if (lun->flags & CTL_LUN_PRIMARY_SC) {
+				tpg_desc_ptr1->pref_state = TPG_PRIMARY;
+				tpg_desc_ptr2->pref_state =
+					TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
+			} else {
+				tpg_desc_ptr1->pref_state =
+					TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
+				tpg_desc_ptr2->pref_state = TPG_PRIMARY;
+			}
+		} else {
+			if (lun->flags & CTL_LUN_PRIMARY_SC) {
+				tpg_desc_ptr1->pref_state =
+					TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
+				tpg_desc_ptr2->pref_state = TPG_PRIMARY;
+			} else {
+				tpg_desc_ptr1->pref_state = TPG_PRIMARY;
+				tpg_desc_ptr2->pref_state =
+					TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
+			}
+		}
+	} else {
+		tpg_desc_ptr1->pref_state = TPG_PRIMARY;
+	}
+	tpg_desc_ptr1->support = 0;
+	tpg_desc_ptr1->target_port_group[1] = 1;
+	tpg_desc_ptr1->status = TPG_IMPLICIT;
+	tpg_desc_ptr1->target_port_count= NUM_PORTS_PER_GRP;
+
+	if (ctl_is_single == 0) {
+		tpg_desc_ptr2->support = 0;
+		tpg_desc_ptr2->target_port_group[1] = 2;
+		tpg_desc_ptr2->status = TPG_IMPLICIT;
+		tpg_desc_ptr2->target_port_count = NUM_PORTS_PER_GRP;
+
+		tp_desc_ptr1_1->relative_target_port_identifier[1] = 1;
+		tp_desc_ptr1_2->relative_target_port_identifier[1] = 2;
+
+		tp_desc_ptr2_1->relative_target_port_identifier[1] = 9;
+		tp_desc_ptr2_2->relative_target_port_identifier[1] = 10;
+	} else {
+        	if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS) {
+			tp_desc_ptr1_1->relative_target_port_identifier[1] = 1;
+			tp_desc_ptr1_2->relative_target_port_identifier[1] = 2;
+		} else {
+			tp_desc_ptr1_1->relative_target_port_identifier[1] = 9;
+			tp_desc_ptr1_2->relative_target_port_identifier[1] = 10;
+		}
+	}
+
+	mtx_unlock(&softc->ctl_lock);
+
+	ctsio->be_move_done = ctl_config_move_done;
+
+	CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n",
+			 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1],
+			 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3],
+			 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5],
+			 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7]));
+
+	ctl_datamove((union ctl_io *)ctsio);
+	return(retval);
+}
+
+int
+ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
+{
+	struct scsi_per_res_in *cdb;
+	int alloc_len, total_len = 0;
+	/* struct scsi_per_res_in_rsrv in_data; */
+	struct ctl_lun *lun;
+	struct ctl_softc *softc;
+
+	CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
+
+	softc = control_softc;
+
+	cdb = (struct scsi_per_res_in *)ctsio->cdb;
+
+	alloc_len = scsi_2btoul(cdb->length);
+
+	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
+
+retry:
+	mtx_lock(&softc->ctl_lock);
+	switch (cdb->action) {
+	case SPRI_RK: /* read keys */
+		total_len = sizeof(struct scsi_per_res_in_keys) +
+			lun->pr_key_count *
+			sizeof(struct scsi_per_res_key);
+		break;
+	case SPRI_RR: /* read reservation */
+		if (lun->flags & CTL_LUN_PR_RESERVED)
+			total_len = sizeof(struct scsi_per_res_in_rsrv);
+		else
+			total_len = sizeof(struct scsi_per_res_in_header);
+		break;
+	case SPRI_RC: /* report capabilities */
+		total_len = sizeof(struct scsi_per_res_cap);
+		break;
+	case SPRI_RS: /* read full status */
+	default:
+		mtx_unlock(&softc->ctl_lock);
+		ctl_set_invalid_field(ctsio,
+				      /*sks_valid*/ 1,
+				      /*command*/ 1,
+				      /*field*/ 1,
+				      /*bit_valid*/ 1,
+				      /*bit*/ 0);
+		ctl_done((union ctl_io *)ctsio);
+		return (CTL_RETVAL_COMPLETE);
+		break; /* NOTREACHED */
+	}
+	mtx_unlock(&softc->ctl_lock);
+
+	ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK);
+	if (ctsio->kern_data_ptr == NULL) {
+		ctsio->io_hdr.status = CTL_SCSI_ERROR;
+		ctsio->scsi_status = SCSI_STATUS_BUSY;
+		ctl_done((union ctl_io *)ctsio);
+		return (CTL_RETVAL_COMPLETE);
+	}
+
+	if (total_len < alloc_len) {
+		ctsio->residual = alloc_len - total_len;
+		ctsio->kern_data_len = total_len;
+		ctsio->kern_total_len = total_len;
+	} else {
+		ctsio->residual = 0;
+		ctsio->kern_data_len = alloc_len;
+		ctsio->kern_total_len = alloc_len;
+	}
+
+	ctsio->kern_data_resid = 0;
+	ctsio->kern_rel_offset = 0;
+	ctsio->kern_sg_entries = 0;
+
+	memset(ctsio->kern_data_ptr, 0, total_len);
+
+	mtx_lock(&softc->ctl_lock);
+	switch (cdb->action) {
+	case SPRI_RK: { // read keys
+        struct scsi_per_res_in_keys *res_keys;
+		int i, key_count;
+
+		res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
+
+		/*
+		 * We had to drop the lock to allocate our buffer, which
+		 * leaves time for someone to come in with another
+		 * persistent reservation.  (That is unlikely, though,
+		 * since this should be the only persistent reservation
+		 * command active right now.)
+		 */
+		if (total_len != (sizeof(struct scsi_per_res_in_keys) +
+		    (lun->pr_key_count *
+		     sizeof(struct scsi_per_res_key)))){
+			mtx_unlock(&softc->ctl_lock);
+			free(ctsio->kern_data_ptr, M_CTL);
+			printf("%s: reservation length changed, retrying\n",
+			       __func__);
+			goto retry;
+		}
+
+		scsi_ulto4b(lun->PRGeneration, res_keys->header.generation);
+
+		scsi_ulto4b(sizeof(struct scsi_per_res_key) *
+			     lun->pr_key_count, res_keys->header.length);
+
+		for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) {
+			if (!lun->per_res[i].registered)
+				continue;
+
+			/*
+			 * We used lun->pr_key_count to calculate the
+			 * size to allocate.  If it turns out the number of
+			 * initiators with the registered flag set is
+			 * larger than that (i.e. they haven't been kept in
+			 * sync), we've got a problem.
+			 */
+			if (key_count >= lun->pr_key_count) {
+#ifdef NEEDTOPORT
+				csevent_log(CSC_CTL | CSC_SHELF_SW |
+					    CTL_PR_ERROR,
+					    csevent_LogType_Fault,
+					    csevent_AlertLevel_Yellow,
+					    csevent_FRU_ShelfController,
+					    csevent_FRU_Firmware,
+				        csevent_FRU_Unknown,
+					    "registered keys %d >= key "
+					    "count %d", key_count,
+					    lun->pr_key_count);
+#endif
+				key_count++;
+				continue;
+			}
+			memcpy(res_keys->keys[key_count].key,
+			       lun->per_res[i].res_key.key,
+			       ctl_min(sizeof(res_keys->keys[key_count].key),
+			       sizeof(lun->per_res[i].res_key)));
+			key_count++;
+		}
+		break;
+	}
+	case SPRI_RR: { // read reservation
+		struct scsi_per_res_in_rsrv *res;
+		int tmp_len, header_only;
+
+		res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
+
+		scsi_ulto4b(lun->PRGeneration, res->header.generation);
+
+		if (lun->flags & CTL_LUN_PR_RESERVED)
+		{
+			tmp_len = sizeof(struct scsi_per_res_in_rsrv);
+			scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
+				    res->header.length);
+			header_only = 0;
+		} else {
+			tmp_len = sizeof(struct scsi_per_res_in_header);
+			scsi_ulto4b(0, res->header.length);
+			header_only = 1;
+		}
+
+		/*
+		 * We had to drop the lock to allocate our buffer, which
+		 * leaves time for someone to come in with another
+		 * persistent reservation.  (That is unlikely, though,
+		 * since this should be the only persistent reservation
+		 * command active right now.)
+		 */
+		if (tmp_len != total_len) {
+			mtx_unlock(&softc->ctl_lock);
+			free(ctsio->kern_data_ptr, M_CTL);
+			printf("%s: reservation status changed, retrying\n",
+			       __func__);
+			goto retry;
+		}
+
+		/*
+		 * No reservation held, so we're done.
+		 */
+		if (header_only != 0)
+			break;
+
+		/*
+		 * If the registration is an All Registrants type, the key
+		 * is 0, since it doesn't really matter.
+		 */
+		if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
+			memcpy(res->data.reservation,
+			       &lun->per_res[lun->pr_res_idx].res_key,
+			       sizeof(struct scsi_per_res_key));
+		}
+		res->data.scopetype = lun->res_type;
+		break;
+	}
+	case SPRI_RC:     //report capabilities
+	{
+		struct scsi_per_res_cap *res_cap;
+		uint16_t type_mask;
+
+		res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
+		scsi_ulto2b(sizeof(*res_cap), res_cap->length);
+		res_cap->flags2 |= SPRI_TMV;
+		type_mask = SPRI_TM_WR_EX_AR |
+			    SPRI_TM_EX_AC_RO |
+			    SPRI_TM_WR_EX_RO |
+			    SPRI_TM_EX_AC |
+			    SPRI_TM_WR_EX |
+			    SPRI_TM_EX_AC_AR;
+		scsi_ulto2b(type_mask, res_cap->type_mask);
+		break;
+	}
+	case SPRI_RS: //read full status
+	default:
+		/*
+		 * This is a bug, because we just checked for this above,
+		 * and should have returned an error.
+		 */
+		panic("Invalid PR type %x", cdb->action);
+		break; /* NOTREACHED */
+	}
+	mtx_unlock(&softc->ctl_lock);
+
+	ctsio->be_move_done = ctl_config_move_done;
+
+	CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n",
+			 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1],
+			 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3],
+			 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5],
+			 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7]));
+
+	ctl_datamove((union ctl_io *)ctsio);
+
+	return (CTL_RETVAL_COMPLETE);
+}
+
+/*
+ * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
+ * it should return.
+ */
+static int
+ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
+		uint64_t sa_res_key, uint8_t type, uint32_t residx,
+		struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
+		struct scsi_per_res_out_parms* param)
+{
+	union ctl_ha_msg persis_io;
+	int retval, i;
+	int isc_retval;
+
+	retval = 0;
+
+	if (sa_res_key == 0) {
+		mtx_lock(&softc->ctl_lock);
+		if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
+			/* validate scope and type */
+			if ((cdb->scope_type & SPR_SCOPE_MASK) !=
+			     SPR_LU_SCOPE) {
+				mtx_unlock(&softc->ctl_lock);
+				ctl_set_invalid_field(/*ctsio*/ ctsio,
+						      /*sks_valid*/ 1,
+						      /*command*/ 1,
+						      /*field*/ 2,
+						      /*bit_valid*/ 1,
+						      /*bit*/ 4);
+				ctl_done((union ctl_io *)ctsio);
+				return (1);
+			}
+
+		        if (type>8 || type==2 || type==4 || type==0) {
+				mtx_unlock(&softc->ctl_lock);
+				ctl_set_invalid_field(/*ctsio*/ ctsio,
+       	           				      /*sks_valid*/ 1,
+						      /*command*/ 1,
+						      /*field*/ 2,
+						      /*bit_valid*/ 1,
+						      /*bit*/ 0);
+				ctl_done((union ctl_io *)ctsio);
+				return (1);
+		        }
+
+			/* temporarily unregister this nexus */
+			lun->per_res[residx].registered = 0;
+
+			/*
+			 * Unregister everybody else and build UA for
+			 * them
+			 */
+			for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
+				if (lun->per_res[i].registered == 0)
+					continue;
+
+				if (!persis_offset
+				 && i <CTL_MAX_INITIATORS)
+					lun->pending_sense[i].ua_pending |=
+						CTL_UA_REG_PREEMPT;
+				else if (persis_offset
+				      && i >= persis_offset)
+					lun->pending_sense[i-persis_offset
+						].ua_pending |=
+						CTL_UA_REG_PREEMPT;
+				lun->per_res[i].registered = 0;
+				memset(&lun->per_res[i].res_key, 0,
+				       sizeof(struct scsi_per_res_key));
+			}
+			lun->per_res[residx].registered = 1;
+			lun->pr_key_count = 1;
+			lun->res_type = type;
+			if (lun->res_type != SPR_TYPE_WR_EX_AR
+			 && lun->res_type != SPR_TYPE_EX_AC_AR)
+				lun->pr_res_idx = residx;
+
+			mtx_unlock(&softc->ctl_lock);
+			/* send msg to other side */
+			persis_io.hdr.nexus = ctsio->io_hdr.nexus;
+			persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
+			persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
+			persis_io.pr.pr_info.residx = lun->pr_res_idx;
+			persis_io.pr.pr_info.res_type = type;
+			memcpy(persis_io.pr.pr_info.sa_res_key,
+			       param->serv_act_res_key,
+			       sizeof(param->serv_act_res_key));
+			if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
+			     &persis_io, sizeof(persis_io), 0)) >
+			     CTL_HA_STATUS_SUCCESS) {
+				printf("CTL:Persis Out error returned "
+				       "from ctl_ha_msg_send %d\n",
+				       isc_retval);
+			}
+		} else {
+			/* not all registrants */
+			mtx_unlock(&softc->ctl_lock);
+			free(ctsio->kern_data_ptr, M_CTL);
+			ctl_set_invalid_field(ctsio,
+					      /*sks_valid*/ 1,
+					      /*command*/ 0,
+					      /*field*/ 8,
+					      /*bit_valid*/ 0,
+					      /*bit*/ 0);
+			ctl_done((union ctl_io *)ctsio);
+			return (1);
+		}
+	} else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
+		|| !(lun->flags & CTL_LUN_PR_RESERVED)) {
+		int found = 0;
+
+		mtx_lock(&softc->ctl_lock);
+		if (res_key == sa_res_key) {
+			/* special case */
+			/*
+			 * The spec implies this is not good but doesn't
+			 * say what to do. There are two choices either
+			 * generate a res conflict or check condition
+			 * with illegal field in parameter data. Since
+			 * that is what is done when the sa_res_key is
+			 * zero I'll take that approach since this has
+			 * to do with the sa_res_key.
+			 */
+			mtx_unlock(&softc->ctl_lock);
+			free(ctsio->kern_data_ptr, M_CTL);
+			ctl_set_invalid_field(ctsio,
+					      /*sks_valid*/ 1,
+					      /*command*/ 0,
+					      /*field*/ 8,
+					      /*bit_valid*/ 0,
+					      /*bit*/ 0);
+			ctl_done((union ctl_io *)ctsio);
+			return (1);
+		}
+
+		for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
+			if (lun->per_res[i].registered
+			 && memcmp(param->serv_act_res_key,
+			    lun->per_res[i].res_key.key,
+			    sizeof(struct scsi_per_res_key)) != 0)
+				continue;
+
+			found = 1;
+			lun->per_res[i].registered = 0;
+			memset(&lun->per_res[i].res_key, 0,
+			       sizeof(struct scsi_per_res_key));
+			lun->pr_key_count--;
+
+			if (!persis_offset
+			 && i < CTL_MAX_INITIATORS)
+				lun->pending_sense[i].ua_pending |=
+					CTL_UA_REG_PREEMPT;
+			else if (persis_offset
+			      && i >= persis_offset)
+				lun->pending_sense[i-persis_offset].ua_pending|=
+					CTL_UA_REG_PREEMPT;
+		}
+		mtx_unlock(&softc->ctl_lock);
+		if (!found) {
+			free(ctsio->kern_data_ptr, M_CTL);
+			ctl_set_reservation_conflict(ctsio);
+			ctl_done((union ctl_io *)ctsio);
+			return (CTL_RETVAL_COMPLETE);
+		}
+		/* send msg to other side */
+		persis_io.hdr.nexus = ctsio->io_hdr.nexus;
+		persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
+		persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
+		persis_io.pr.pr_info.residx = lun->pr_res_idx;
+		persis_io.pr.pr_info.res_type = type;
+		memcpy(persis_io.pr.pr_info.sa_res_key,
+		       param->serv_act_res_key,
+		       sizeof(param->serv_act_res_key));
+		if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
+		     &persis_io, sizeof(persis_io), 0)) >
+		     CTL_HA_STATUS_SUCCESS) {
+			printf("CTL:Persis Out error returned from "
+			       "ctl_ha_msg_send %d\n", isc_retval);
+		}
+	} else {
+		/* Reserved but not all registrants */
+		/* sa_res_key is res holder */
+		if (memcmp(param->serv_act_res_key,
+                   lun->per_res[lun->pr_res_idx].res_key.key,
+                   sizeof(struct scsi_per_res_key)) == 0) {
+			/* validate scope and type */
+			if ((cdb->scope_type & SPR_SCOPE_MASK) !=
+			     SPR_LU_SCOPE) {
+				ctl_set_invalid_field(/*ctsio*/ ctsio,
+						      /*sks_valid*/ 1,
+						      /*command*/ 1,
+						      /*field*/ 2,
+						      /*bit_valid*/ 1,
+						      /*bit*/ 4);
+				ctl_done((union ctl_io *)ctsio);
+				return (1);
+			}
+
+			if (type>8 || type==2 || type==4 || type==0) {
+				ctl_set_invalid_field(/*ctsio*/ ctsio,
+						      /*sks_valid*/ 1,
+						      /*command*/ 1,
+						      /*field*/ 2,
+						      /*bit_valid*/ 1,
+						      /*bit*/ 0);
+				ctl_done((union ctl_io *)ctsio);
+				return (1);
+			}
+
+			/*
+			 * Do the following:
+			 * if sa_res_key != res_key remove all
+			 * registrants w/sa_res_key and generate UA
+			 * for these registrants(Registrations
+			 * Preempted) if it wasn't an exclusive
+			 * reservation generate UA(Reservations
+			 * Preempted) for all other registered nexuses
+			 * if the type has changed. Establish the new
+			 * reservation and holder. If res_key and
+			 * sa_res_key are the same do the above
+			 * except don't unregister the res holder.
+			 */
+
+			/*
+			 * Temporarily unregister so it won't get
+			 * removed or UA generated
+			 */
+			lun->per_res[residx].registered = 0;
+			for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
+				if (lun->per_res[i].registered == 0)
+					continue;
+
+				if (memcmp(param->serv_act_res_key,
+				    lun->per_res[i].res_key.key,
+				    sizeof(struct scsi_per_res_key)) == 0) {
+					lun->per_res[i].registered = 0;
+					memset(&lun->per_res[i].res_key,
+					       0,
+					       sizeof(struct scsi_per_res_key));
+					lun->pr_key_count--;
+
+					if (!persis_offset
+					 && i < CTL_MAX_INITIATORS)
+						lun->pending_sense[i
+							].ua_pending |=
+							CTL_UA_REG_PREEMPT;
+					else if (persis_offset
+					      && i >= persis_offset)
+						lun->pending_sense[
+						  i-persis_offset].ua_pending |=
+						  CTL_UA_REG_PREEMPT;
+				} else if (type != lun->res_type
+					&& (lun->res_type == SPR_TYPE_WR_EX_RO
+					 || lun->res_type ==SPR_TYPE_EX_AC_RO)){
+						if (!persis_offset
+						 && i < CTL_MAX_INITIATORS)
+							lun->pending_sense[i
+							].ua_pending |=
+							CTL_UA_RES_RELEASE;
+						else if (persis_offset
+						      && i >= persis_offset)
+							lun->pending_sense[
+							i-persis_offset
+							].ua_pending |=
+							CTL_UA_RES_RELEASE;
+				}
+			}
+			lun->per_res[residx].registered = 1;
+			lun->res_type = type;
+			if (lun->res_type != SPR_TYPE_WR_EX_AR
+			 && lun->res_type != SPR_TYPE_EX_AC_AR)
+				lun->pr_res_idx = residx;
+			else
+				lun->pr_res_idx =
+					CTL_PR_ALL_REGISTRANTS;
+
+			persis_io.hdr.nexus = ctsio->io_hdr.nexus;
+			persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
+			persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
+			persis_io.pr.pr_info.residx = lun->pr_res_idx;
+			persis_io.pr.pr_info.res_type = type;
+			memcpy(persis_io.pr.pr_info.sa_res_key,
+			       param->serv_act_res_key,
+			       sizeof(param->serv_act_res_key));
+			if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
+			     &persis_io, sizeof(persis_io), 0)) >
+			     CTL_HA_STATUS_SUCCESS) {
+				printf("CTL:Persis Out error returned "
+				       "from ctl_ha_msg_send %d\n",
+				       isc_retval);
+			}
+		} else {
+			/*
+			 * sa_res_key is not the res holder just
+			 * remove registrants
+			 */
+			int found=0;
+			mtx_lock(&softc->ctl_lock);
+
+			for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
+				if (memcmp(param->serv_act_res_key,
+				    lun->per_res[i].res_key.key,
+				    sizeof(struct scsi_per_res_key)) != 0)
+					continue;
+
+				found = 1;
+				lun->per_res[i].registered = 0;
+				memset(&lun->per_res[i].res_key, 0,
+				       sizeof(struct scsi_per_res_key));
+				lun->pr_key_count--;
+
+				if (!persis_offset
+				 && i < CTL_MAX_INITIATORS)
+					lun->pending_sense[i].ua_pending |=
+						CTL_UA_REG_PREEMPT;
+				else if (persis_offset
+				      && i >= persis_offset)
+					lun->pending_sense[
+						i-persis_offset].ua_pending |=
+						CTL_UA_REG_PREEMPT;
+			}
+
+			if (!found) {
+				mtx_unlock(&softc->ctl_lock);
+				free(ctsio->kern_data_ptr, M_CTL);
+				ctl_set_reservation_conflict(ctsio);
+				ctl_done((union ctl_io *)ctsio);
+		        	return (1);
+			}
+			mtx_unlock(&softc->ctl_lock);
+			persis_io.hdr.nexus = ctsio->io_hdr.nexus;
+			persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
+			persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
+			persis_io.pr.pr_info.residx = lun->pr_res_idx;
+			persis_io.pr.pr_info.res_type = type;
+			memcpy(persis_io.pr.pr_info.sa_res_key,
+			       param->serv_act_res_key,
+			       sizeof(param->serv_act_res_key));
+			if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
+			     &persis_io, sizeof(persis_io), 0)) >
+			     CTL_HA_STATUS_SUCCESS) {
+				printf("CTL:Persis Out error returned "
+				       "from ctl_ha_msg_send %d\n",
+				isc_retval);
+			}
+		}
+	}
+
+	lun->PRGeneration++;
+
+	return (retval);
+}
+
+static void
+ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
+{
+	int i;
+
+	if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
+	 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
+	 || memcmp(&lun->per_res[lun->pr_res_idx].res_key,
+		   msg->pr.pr_info.sa_res_key,
+		   sizeof(struct scsi_per_res_key)) != 0) {
+		uint64_t sa_res_key;
+		sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
+
+		if (sa_res_key == 0) {
+			/* temporarily unregister this nexus */
+			lun->per_res[msg->pr.pr_info.residx].registered = 0;
+
+			/*
+			 * Unregister everybody else and build UA for
+			 * them
+			 */
+			for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
+				if (lun->per_res[i].registered == 0)
+					continue;
+
+				if (!persis_offset
+				 && i < CTL_MAX_INITIATORS)
+					lun->pending_sense[i].ua_pending |=
+						CTL_UA_REG_PREEMPT;
+				else if (persis_offset && i >= persis_offset)
+					lun->pending_sense[i -
+						persis_offset].ua_pending |=
+						CTL_UA_REG_PREEMPT;
+				lun->per_res[i].registered = 0;
+				memset(&lun->per_res[i].res_key, 0,
+				       sizeof(struct scsi_per_res_key));
+			}
+
+			lun->per_res[msg->pr.pr_info.residx].registered = 1;
+			lun->pr_key_count = 1;
+			lun->res_type = msg->pr.pr_info.res_type;
+			if (lun->res_type != SPR_TYPE_WR_EX_AR
+			 && lun->res_type != SPR_TYPE_EX_AC_AR)
+				lun->pr_res_idx = msg->pr.pr_info.residx;
+		} else {
+		        for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
+				if (memcmp(msg->pr.pr_info.sa_res_key,
+		                   lun->per_res[i].res_key.key,
+		                   sizeof(struct scsi_per_res_key)) != 0)
+					continue;
+
+				lun->per_res[i].registered = 0;
+				memset(&lun->per_res[i].res_key, 0,
+				       sizeof(struct scsi_per_res_key));
+				lun->pr_key_count--;
+
+				if (!persis_offset
+				 && i < persis_offset)
+					lun->pending_sense[i].ua_pending |=
+						CTL_UA_REG_PREEMPT;
+				else if (persis_offset
+				      && i >= persis_offset)
+					lun->pending_sense[i -
+						persis_offset].ua_pending |=
+						CTL_UA_REG_PREEMPT;
+			}
+		}
+	} else {
+		/*
+		 * Temporarily unregister so it won't get removed
+		 * or UA generated
+		 */
+		lun->per_res[msg->pr.pr_info.residx].registered = 0;
+		for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
+			if (lun->per_res[i].registered == 0)
+				continue;
+
+			if (memcmp(msg->pr.pr_info.sa_res_key,
+	                   lun->per_res[i].res_key.key,
+	                   sizeof(struct scsi_per_res_key)) == 0) {
+				lun->per_res[i].registered = 0;
+				memset(&lun->per_res[i].res_key, 0,
+				       sizeof(struct scsi_per_res_key));
+				lun->pr_key_count--;
+				if (!persis_offset
+				 && i < CTL_MAX_INITIATORS)
+					lun->pending_sense[i].ua_pending |=
+						CTL_UA_REG_PREEMPT;
+				else if (persis_offset
+				      && i >= persis_offset)
+					lun->pending_sense[i -
+						persis_offset].ua_pending |=
+						CTL_UA_REG_PREEMPT;
+			} else if (msg->pr.pr_info.res_type != lun->res_type
+				&& (lun->res_type == SPR_TYPE_WR_EX_RO
+				 || lun->res_type == SPR_TYPE_EX_AC_RO)) {
+					if (!persis_offset
+					 && i < persis_offset)
+						lun->pending_sense[i
+							].ua_pending |=
+							CTL_UA_RES_RELEASE;
+					else if (persis_offset
+					      && i >= persis_offset)
+					lun->pending_sense[i -
+						persis_offset].ua_pending |=
+						CTL_UA_RES_RELEASE;
+			}
+		}
+		lun->per_res[msg->pr.pr_info.residx].registered = 1;
+		lun->res_type = msg->pr.pr_info.res_type;
+		if (lun->res_type != SPR_TYPE_WR_EX_AR
+		 && lun->res_type != SPR_TYPE_EX_AC_AR)
+			lun->pr_res_idx = msg->pr.pr_info.residx;
+		else
+			lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
+	}
+	lun->PRGeneration++;
+
+}
+
+
+int
+ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
+{
+	int retval;
+	int isc_retval;
+	u_int32_t param_len;
+	struct scsi_per_res_out *cdb;
+	struct ctl_lun *lun;
+	struct scsi_per_res_out_parms* param;
+	struct ctl_softc *softc;
+	uint32_t residx;
+	uint64_t res_key, sa_res_key;
+	uint8_t type;
+	union ctl_ha_msg persis_io;
+	int    i;
+
+	CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
+
+	retval = CTL_RETVAL_COMPLETE;
+
+	softc = control_softc;
+
+	cdb = (struct scsi_per_res_out *)ctsio->cdb;
+	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
+
+	/*
+	 * We only support whole-LUN scope.  The scope & type are ignored for
+	 * register, register and ignore existing key and clear.
+	 * We sometimes ignore scope and type on preempts too!!
+	 * Verify reservation type here as well.
+	 */
+	type = cdb->scope_type & SPR_TYPE_MASK;
+	if ((cdb->action == SPRO_RESERVE)
+	 || (cdb->action == SPRO_RELEASE)) {
+		if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
+			ctl_set_invalid_field(/*ctsio*/ ctsio,
+					      /*sks_valid*/ 1,
+					      /*command*/ 1,
+					      /*field*/ 2,
+					      /*bit_valid*/ 1,
+					      /*bit*/ 4);
+			ctl_done((union ctl_io *)ctsio);
+			return (CTL_RETVAL_COMPLETE);
+		}
+
+		if (type>8 || type==2 || type==4 || type==0) {
+			ctl_set_invalid_field(/*ctsio*/ ctsio,
+					      /*sks_valid*/ 1,
+					      /*command*/ 1,
+					      /*field*/ 2,
+					      /*bit_valid*/ 1,
+					      /*bit*/ 0);
+			ctl_done((union ctl_io *)ctsio);
+			return (CTL_RETVAL_COMPLETE);
+		}
+	}
+
+	switch (cdb->action & SPRO_ACTION_MASK) {
+	case SPRO_REGISTER:
+	case SPRO_RESERVE:
+	case SPRO_RELEASE:
+	case SPRO_CLEAR:
+	case SPRO_PREEMPT:
+	case SPRO_REG_IGNO:
+		break;
+	case SPRO_REG_MOVE:
+	case SPRO_PRE_ABO:
+	default:
+		ctl_set_invalid_field(/*ctsio*/ ctsio,
+				      /*sks_valid*/ 1,
+				      /*command*/ 1,
+				      /*field*/ 1,
+				      /*bit_valid*/ 1,
+				      /*bit*/ 0);
+		ctl_done((union ctl_io *)ctsio);
+		return (CTL_RETVAL_COMPLETE);
+		break; /* NOTREACHED */
+	}
+
+	param_len = scsi_4btoul(cdb->length);
+
+	if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
+		ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
+		if (ctsio->kern_data_ptr == NULL) {
+			ctl_set_busy(ctsio);
+			ctl_done((union ctl_io *)ctsio);
+			return (CTL_RETVAL_COMPLETE);
+		}
+		ctsio->kern_data_len = param_len;
+		ctsio->kern_total_len = param_len;
+		ctsio->kern_data_resid = 0;
+		ctsio->kern_rel_offset = 0;
+		ctsio->kern_sg_entries = 0;
+		ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
+		ctsio->be_move_done = ctl_config_move_done;
+		ctl_datamove((union ctl_io *)ctsio);
+
+		return (CTL_RETVAL_COMPLETE);
+	}
+
+	param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
+
+	residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
+	res_key = scsi_8btou64(param->res_key.key);
+	sa_res_key = scsi_8btou64(param->serv_act_res_key);
+
+	/*
+	 * Validate the reservation key here except for SPRO_REG_IGNO
+	 * This must be done for all other service actions
+	 */
+	if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
+		mtx_lock(&softc->ctl_lock);
+		if (lun->per_res[residx].registered) {
+		    if (memcmp(param->res_key.key,
+			       lun->per_res[residx].res_key.key,
+			       ctl_min(sizeof(param->res_key),
+			       sizeof(lun->per_res[residx].res_key))) != 0) {
+				/*
+				 * The current key passed in doesn't match
+				 * the one the initiator previously
+				 * registered.
+				 */
+				mtx_unlock(&softc->ctl_lock);
+				free(ctsio->kern_data_ptr, M_CTL);
+				ctl_set_reservation_conflict(ctsio);
+				ctl_done((union ctl_io *)ctsio);
+				return (CTL_RETVAL_COMPLETE);
+			}
+		} else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
+		    /*
+			 * We are not registered
+			 */
+			mtx_unlock(&softc->ctl_lock);
+			free(ctsio->kern_data_ptr, M_CTL);
+			ctl_set_reservation_conflict(ctsio);
+			ctl_done((union ctl_io *)ctsio);
+			return (CTL_RETVAL_COMPLETE);
+		} else if (res_key != 0) {
+			/*
+			 * We are not registered and trying to register but
+			 * the register key isn't zero.
+			 */
+			mtx_unlock(&softc->ctl_lock);
+			free(ctsio->kern_data_ptr, M_CTL);
+			ctl_set_reservation_conflict(ctsio);
+			ctl_done((union ctl_io *)ctsio);
+			return (CTL_RETVAL_COMPLETE);
+		}
+		mtx_unlock(&softc->ctl_lock);
+	}
+
+	switch (cdb->action & SPRO_ACTION_MASK) {
+	case SPRO_REGISTER:
+	case SPRO_REG_IGNO: {
+
+#if 0
+		printf("Registration received\n");
+#endif
+
+		/*
+		 * We don't support any of these options, as we report in
+		 * the read capabilities request (see
+		 * ctl_persistent_reserve_in(), above).
+		 */
+		if ((param->flags & SPR_SPEC_I_PT)
+		 || (param->flags & SPR_ALL_TG_PT)
+		 || (param->flags & SPR_APTPL)) {
+			int bit_ptr;
+
+			if (param->flags & SPR_APTPL)
+				bit_ptr = 0;
+			else if (param->flags & SPR_ALL_TG_PT)
+				bit_ptr = 2;
+			else /* SPR_SPEC_I_PT */
+				bit_ptr = 3;
+
+			free(ctsio->kern_data_ptr, M_CTL);
+			ctl_set_invalid_field(ctsio,
+					      /*sks_valid*/ 1,
+					      /*command*/ 0,
+					      /*field*/ 20,
+					      /*bit_valid*/ 1,
+					      /*bit*/ bit_ptr);
+			ctl_done((union ctl_io *)ctsio);
+			return (CTL_RETVAL_COMPLETE);
+		}
+
+		mtx_lock(&softc->ctl_lock);
+
+		/*
+		 * The initiator wants to clear the
+		 * key/unregister.
+		 */
+		if (sa_res_key == 0) {
+			if ((res_key == 0
+			  && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
+			 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
+			  && !lun->per_res[residx].registered)) {
+				mtx_unlock(&softc->ctl_lock);
+				goto done;
+			}
+
+			lun->per_res[residx].registered = 0;
+			memset(&lun->per_res[residx].res_key,
+			       0, sizeof(lun->per_res[residx].res_key));
+			lun->pr_key_count--;
+
+			if (residx == lun->pr_res_idx) {
+				lun->flags &= ~CTL_LUN_PR_RESERVED;
+				lun->pr_res_idx = CTL_PR_NO_RESERVATION;
+
+				if ((lun->res_type == SPR_TYPE_WR_EX_RO
+				  || lun->res_type == SPR_TYPE_EX_AC_RO)
+				 && lun->pr_key_count) {
+					/*
+					 * If the reservation is a registrants
+					 * only type we need to generate a UA
+					 * for other registered inits.  The
+					 * sense code should be RESERVATIONS
+					 * RELEASED
+					 */
+
+					for (i = 0; i < CTL_MAX_INITIATORS;i++){
+						if (lun->per_res[
+						    i+persis_offset].registered
+						    == 0)
+							continue;
+						lun->pending_sense[i
+							].ua_pending |=
+							CTL_UA_RES_RELEASE;
+					}
+				}
+				lun->res_type = 0;
+			} else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
+				if (lun->pr_key_count==0) {
+					lun->flags &= ~CTL_LUN_PR_RESERVED;
+					lun->res_type = 0;
+					lun->pr_res_idx = CTL_PR_NO_RESERVATION;
+				}
+			}
+			persis_io.hdr.nexus = ctsio->io_hdr.nexus;
+			persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
+			persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
+			persis_io.pr.pr_info.residx = residx;
+			if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL,
+			     &persis_io, sizeof(persis_io), 0 )) >
+			     CTL_HA_STATUS_SUCCESS) {
+				printf("CTL:Persis Out error returned from "
+				       "ctl_ha_msg_send %d\n", isc_retval);
+			}
+			mtx_unlock(&softc->ctl_lock);
+		} else /* sa_res_key != 0 */ {
+
+			/*
+			 * If we aren't registered currently then increment
+			 * the key count and set the registered flag.
+			 */
+			if (!lun->per_res[residx].registered) {
+				lun->pr_key_count++;
+				lun->per_res[residx].registered = 1;
+			}
+
+			memcpy(&lun->per_res[residx].res_key,
+			       param->serv_act_res_key,
+			       ctl_min(sizeof(param->serv_act_res_key),
+			       sizeof(lun->per_res[residx].res_key)));
+
+			persis_io.hdr.nexus = ctsio->io_hdr.nexus;
+			persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
+			persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
+			persis_io.pr.pr_info.residx = residx;
+			memcpy(persis_io.pr.pr_info.sa_res_key,
+			       param->serv_act_res_key,
+			       sizeof(param->serv_act_res_key));
+			mtx_unlock(&softc->ctl_lock);
+			if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
+			     &persis_io, sizeof(persis_io), 0)) >
+			     CTL_HA_STATUS_SUCCESS) {
+				printf("CTL:Persis Out error returned from "
+				       "ctl_ha_msg_send %d\n", isc_retval);
+			}
+		}
+		lun->PRGeneration++;
+
+		break;
+	}
+	case SPRO_RESERVE:
+#if 0
+                printf("Reserve executed type %d\n", type);
+#endif
+		mtx_lock(&softc->ctl_lock);
+		if (lun->flags & CTL_LUN_PR_RESERVED) {
+			/*
+			 * if this isn't the reservation holder and it's
+			 * not a "all registrants" type or if the type is
+			 * different then we have a conflict
+			 */
+			if ((lun->pr_res_idx != residx
+			  && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
+			 || lun->res_type != type) {
+				mtx_unlock(&softc->ctl_lock);
+				free(ctsio->kern_data_ptr, M_CTL);
+				ctl_set_reservation_conflict(ctsio);
+				ctl_done((union ctl_io *)ctsio);
+				return (CTL_RETVAL_COMPLETE);
+			}
+		} else /* create a reservation */ {
+			/*
+			 * If it's not an "all registrants" type record
+			 * reservation holder
+			 */
+			if (type != SPR_TYPE_WR_EX_AR
+			 && type != SPR_TYPE_EX_AC_AR)
+				lun->pr_res_idx = residx; /* Res holder */
+			else
+				lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
+
+			lun->flags |= CTL_LUN_PR_RESERVED;
+			lun->res_type = type;
+
+			mtx_unlock(&softc->ctl_lock);
+
+			/* send msg to other side */
+			persis_io.hdr.nexus = ctsio->io_hdr.nexus;
+			persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
+			persis_io.pr.pr_info.action = CTL_PR_RESERVE;
+			persis_io.pr.pr_info.residx = lun->pr_res_idx;
+			persis_io.pr.pr_info.res_type = type;
+			if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
+			     &persis_io, sizeof(persis_io), 0)) >
+			     CTL_HA_STATUS_SUCCESS) {
+				printf("CTL:Persis Out error returned from "
+				       "ctl_ha_msg_send %d\n", isc_retval);
+			}
+		}
+		break;
+
+	case SPRO_RELEASE:
+		mtx_lock(&softc->ctl_lock);
+		if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
+			/* No reservation exists return good status */
+			mtx_unlock(&softc->ctl_lock);
+			goto done;
+		}
+		/*
+		 * Is this nexus a reservation holder?
+		 */
+		if (lun->pr_res_idx != residx
+		 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
+			/*
+			 * not a res holder return good status but
+			 * do nothing
+			 */
+			mtx_unlock(&softc->ctl_lock);
+			goto done;
+		}
+
+		if (lun->res_type != type) {
+			mtx_unlock(&softc->ctl_lock);
+			free(ctsio->kern_data_ptr, M_CTL);
+			ctl_set_illegal_pr_release(ctsio);
+			ctl_done((union ctl_io *)ctsio);
+			return (CTL_RETVAL_COMPLETE);
+		}
+
+		/* okay to release */
+		lun->flags &= ~CTL_LUN_PR_RESERVED;
+		lun->pr_res_idx = CTL_PR_NO_RESERVATION;
+		lun->res_type = 0;
+
+		/*
+		 * if this isn't an exclusive access
+		 * res generate UA for all other
+		 * registrants.
+		 */
+		if (type != SPR_TYPE_EX_AC
+		 && type != SPR_TYPE_WR_EX) {
+			/*
+			 * temporarily unregister so we don't generate UA
+			 */
+			lun->per_res[residx].registered = 0;
+
+			for (i = 0; i < CTL_MAX_INITIATORS; i++) {
+				if (lun->per_res[i+persis_offset].registered
+				    == 0)
+					continue;
+				lun->pending_sense[i].ua_pending |=
+					CTL_UA_RES_RELEASE;
+			}
+
+			lun->per_res[residx].registered = 1;
+		}
+		mtx_unlock(&softc->ctl_lock);
+		/* Send msg to other side */
+		persis_io.hdr.nexus = ctsio->io_hdr.nexus;
+		persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
+		persis_io.pr.pr_info.action = CTL_PR_RELEASE;
+		if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io,
+		     sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
+			printf("CTL:Persis Out error returned from "
+			       "ctl_ha_msg_send %d\n", isc_retval);
+		}
+		break;
+
+	case SPRO_CLEAR:
+		/* send msg to other side */
+
+		mtx_lock(&softc->ctl_lock);
+		lun->flags &= ~CTL_LUN_PR_RESERVED;
+		lun->res_type = 0;
+		lun->pr_key_count = 0;
+		lun->pr_res_idx = CTL_PR_NO_RESERVATION;
+
+
+		memset(&lun->per_res[residx].res_key,
+		       0, sizeof(lun->per_res[residx].res_key));
+		lun->per_res[residx].registered = 0;
+
+		for (i=0; i < 2*CTL_MAX_INITIATORS; i++)
+			if (lun->per_res[i].registered) {
+				if (!persis_offset && i < CTL_MAX_INITIATORS)
+					lun->pending_sense[i].ua_pending |=
+						CTL_UA_RES_PREEMPT;
+				else if (persis_offset && i >= persis_offset)
+					lun->pending_sense[i-persis_offset
+					    ].ua_pending |= CTL_UA_RES_PREEMPT;
+
+				memset(&lun->per_res[i].res_key,
+				       0, sizeof(struct scsi_per_res_key));
+				lun->per_res[i].registered = 0;
+			}
+		lun->PRGeneration++;
+		mtx_unlock(&softc->ctl_lock);
+		persis_io.hdr.nexus = ctsio->io_hdr.nexus;
+		persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
+		persis_io.pr.pr_info.action = CTL_PR_CLEAR;
+		if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
+		     sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
+			printf("CTL:Persis Out error returned from "
+			       "ctl_ha_msg_send %d\n", isc_retval);
+		}
+		break;
+
+	case SPRO_PREEMPT: {
+		int nretval;
+
+		nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
+					  residx, ctsio, cdb, param);
+		if (nretval != 0)
+			return (CTL_RETVAL_COMPLETE);
+		break;
+	}
+	case SPRO_REG_MOVE:
+	case SPRO_PRE_ABO:
+	default:
+		free(ctsio->kern_data_ptr, M_CTL);
+		ctl_set_invalid_field(/*ctsio*/ ctsio,
+				      /*sks_valid*/ 1,
+				      /*command*/ 1,
+				      /*field*/ 1,
+				      /*bit_valid*/ 1,
+				      /*bit*/ 0);
+		ctl_done((union ctl_io *)ctsio);
+		return (CTL_RETVAL_COMPLETE);
+		break; /* NOTREACHED */
+	}
+
+done:
+	free(ctsio->kern_data_ptr, M_CTL);
+	ctl_set_success(ctsio);
+	ctl_done((union ctl_io *)ctsio);
+
+	return (retval);
+}
+
+/*
+ * This routine is for handling a message from the other SC pertaining to
+ * persistent reserve out. All the error checking will have been done
+ * so only perorming the action need be done here to keep the two
+ * in sync.
+ */
+static void
+ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg)
+{
+	struct ctl_lun *lun;
+	struct ctl_softc *softc;
+	int i;
+
+	softc = control_softc;
+
+	mtx_lock(&softc->ctl_lock);
+
+	lun = softc->ctl_luns[msg->hdr.nexus.targ_lun];
+	switch(msg->pr.pr_info.action) {
+	case CTL_PR_REG_KEY:
+		if (!lun->per_res[msg->pr.pr_info.residx].registered) {
+			lun->per_res[msg->pr.pr_info.residx].registered = 1;
+			lun->pr_key_count++;
+		}
+		lun->PRGeneration++;
+		memcpy(&lun->per_res[msg->pr.pr_info.residx].res_key,
+		       msg->pr.pr_info.sa_res_key,
+		       sizeof(struct scsi_per_res_key));
+		break;
+
+	case CTL_PR_UNREG_KEY:
+		lun->per_res[msg->pr.pr_info.residx].registered = 0;
+		memset(&lun->per_res[msg->pr.pr_info.residx].res_key,
+		       0, sizeof(struct scsi_per_res_key));
+		lun->pr_key_count--;
+
+		/* XXX Need to see if the reservation has been released */
+		/* if so do we need to generate UA? */
+		if (msg->pr.pr_info.residx == lun->pr_res_idx) {
+			lun->flags &= ~CTL_LUN_PR_RESERVED;
+			lun->pr_res_idx = CTL_PR_NO_RESERVATION;
+
+			if ((lun->res_type == SPR_TYPE_WR_EX_RO
+			  || lun->res_type == SPR_TYPE_EX_AC_RO)
+			 && lun->pr_key_count) {
+				/*
+				 * If the reservation is a registrants
+				 * only type we need to generate a UA
+				 * for other registered inits.  The
+				 * sense code should be RESERVATIONS
+				 * RELEASED
+				 */
+
+				for (i = 0; i < CTL_MAX_INITIATORS; i++) {
+					if (lun->per_res[i+
+					    persis_offset].registered == 0)
+						continue;
+
+					lun->pending_sense[i
+						].ua_pending |=
+						CTL_UA_RES_RELEASE;
+				}
+			}
+			lun->res_type = 0;
+		} else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
+			if (lun->pr_key_count==0) {
+				lun->flags &= ~CTL_LUN_PR_RESERVED;
+				lun->res_type = 0;
+				lun->pr_res_idx = CTL_PR_NO_RESERVATION;
+			}
+		}
+		lun->PRGeneration++;
+		break;
+
+	case CTL_PR_RESERVE:
+		lun->flags |= CTL_LUN_PR_RESERVED;
+		lun->res_type = msg->pr.pr_info.res_type;
+		lun->pr_res_idx = msg->pr.pr_info.residx;
+
+		break;
+
+	case CTL_PR_RELEASE:
+		/*
+		 * if this isn't an exclusive access res generate UA for all
+		 * other registrants.
+		 */
+		if (lun->res_type != SPR_TYPE_EX_AC
+		 && lun->res_type != SPR_TYPE_WR_EX) {
+			for (i = 0; i < CTL_MAX_INITIATORS; i++)
+				if (lun->per_res[i+persis_offset].registered)
+					lun->pending_sense[i].ua_pending |=
+						CTL_UA_RES_RELEASE;
+		}
+
+		lun->flags &= ~CTL_LUN_PR_RESERVED;
+		lun->pr_res_idx = CTL_PR_NO_RESERVATION;
+		lun->res_type = 0;
+		break;
+
+	case CTL_PR_PREEMPT:
+		ctl_pro_preempt_other(lun, msg);
+		break;
+	case CTL_PR_CLEAR:
+		lun->flags &= ~CTL_LUN_PR_RESERVED;
+		lun->res_type = 0;
+		lun->pr_key_count = 0;
+		lun->pr_res_idx = CTL_PR_NO_RESERVATION;
+
+		for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
+			if (lun->per_res[i].registered == 0)
+				continue;
+			if (!persis_offset
+			 && i < CTL_MAX_INITIATORS)
+				lun->pending_sense[i].ua_pending |=
+					CTL_UA_RES_PREEMPT;
+			else if (persis_offset
+			      && i >= persis_offset)
+   				lun->pending_sense[i-persis_offset].ua_pending|=
+					CTL_UA_RES_PREEMPT;
+			memset(&lun->per_res[i].res_key, 0,
+			       sizeof(struct scsi_per_res_key));
+			lun->per_res[i].registered = 0;
+		}
+		lun->PRGeneration++;
+		break;
+	}
+
+	mtx_unlock(&softc->ctl_lock);
+}
+
+int
+ctl_read_write(struct ctl_scsiio *ctsio)
+{
+	struct ctl_lun *lun;
+	struct ctl_lba_len lbalen;
+	uint64_t lba;
+	uint32_t num_blocks;
+	int reladdr, fua, dpo, ebp;
+	int retval;
+	int isread;
+
+	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
+
+	CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
+
+	reladdr = 0;
+	fua = 0;
+	dpo = 0;
+	ebp = 0;
+
+	retval = CTL_RETVAL_COMPLETE;
+
+	isread = ctsio->cdb[0] == READ_6  || ctsio->cdb[0] == READ_10
+	      || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
+	if (lun->flags & CTL_LUN_PR_RESERVED && isread) {
+		uint32_t residx;
+
+		/*
+		 * XXX KDM need a lock here.
+		 */
+		residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
+		if ((lun->res_type == SPR_TYPE_EX_AC
+		  && residx != lun->pr_res_idx)
+		 || ((lun->res_type == SPR_TYPE_EX_AC_RO
+		   || lun->res_type == SPR_TYPE_EX_AC_AR)
+		  && !lun->per_res[residx].registered)) {
+			ctl_set_reservation_conflict(ctsio);
+			ctl_done((union ctl_io *)ctsio);
+			return (CTL_RETVAL_COMPLETE);
+	        }
+	}
+
+	switch (ctsio->cdb[0]) {
+	case READ_6:
+	case WRITE_6: {
+		struct scsi_rw_6 *cdb;
+
+		cdb = (struct scsi_rw_6 *)ctsio->cdb;
+
+		lba = scsi_3btoul(cdb->addr);
+		/* only 5 bits are valid in the most significant address byte */
+		lba &= 0x1fffff;
+		num_blocks = cdb->length;
+		/*
+		 * This is correct according to SBC-2.
+		 */
+		if (num_blocks == 0)
+			num_blocks = 256;
+		break;
+	}
+	case READ_10:
+	case WRITE_10: {
+		struct scsi_rw_10 *cdb;
+
+		cdb = (struct scsi_rw_10 *)ctsio->cdb;
+
+		if (cdb->byte2 & SRW10_RELADDR)
+			reladdr = 1;
+		if (cdb->byte2 & SRW10_FUA)
+			fua = 1;
+		if (cdb->byte2 & SRW10_DPO)
+			dpo = 1;
+
+		if ((cdb->opcode == WRITE_10)
+		 && (cdb->byte2 & SRW10_EBP))
+			ebp = 1;
+
+		lba = scsi_4btoul(cdb->addr);
+		num_blocks = scsi_2btoul(cdb->length);
+		break;
+	}
+	case WRITE_VERIFY_10: {
+		struct scsi_write_verify_10 *cdb;
+
+		cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
+
+		/*
+		 * XXX KDM we should do actual write verify support at some
+		 * point.  This is obviously fake, we're just translating
+		 * things to a write.  So we don't even bother checking the
+		 * BYTCHK field, since we don't do any verification.  If
+		 * the user asks for it, we'll just pretend we did it.
+		 */
+		if (cdb->byte2 & SWV_DPO)
+			dpo = 1;
+
+		lba = scsi_4btoul(cdb->addr);
+		num_blocks = scsi_2btoul(cdb->length);
+		break;
+	}
+	case READ_12:
+	case WRITE_12: {
+		struct scsi_rw_12 *cdb;
+
+		cdb = (struct scsi_rw_12 *)ctsio->cdb;
+
+		if (cdb->byte2 & SRW12_RELADDR)
+			reladdr = 1;
+		if (cdb->byte2 & SRW12_FUA)
+			fua = 1;
+		if (cdb->byte2 & SRW12_DPO)
+			dpo = 1;
+		lba = scsi_4btoul(cdb->addr);
+		num_blocks = scsi_4btoul(cdb->length);
+		break;
+	}
+	case WRITE_VERIFY_12: {
+		struct scsi_write_verify_12 *cdb;
+
+		cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
+
+		if (cdb->byte2 & SWV_DPO)
+			dpo = 1;
+		
+		lba = scsi_4btoul(cdb->addr);
+		num_blocks = scsi_4btoul(cdb->length);
+
+		break;
+	}
+	case READ_16:
+	case WRITE_16: {
+		struct scsi_rw_16 *cdb;
+
+		cdb = (struct scsi_rw_16 *)ctsio->cdb;
+
+		if (cdb->byte2 & SRW12_RELADDR)
+			reladdr = 1;
+		if (cdb->byte2 & SRW12_FUA)
+			fua = 1;
+		if (cdb->byte2 & SRW12_DPO)
+			dpo = 1;
+
+		lba = scsi_8btou64(cdb->addr);
+		num_blocks = scsi_4btoul(cdb->length);
+		break;
+	}
+	case WRITE_VERIFY_16: {
+		struct scsi_write_verify_16 *cdb;
+
+		cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
+
+		if (cdb->byte2 & SWV_DPO)
+			dpo = 1;
+
+		lba = scsi_8btou64(cdb->addr);
+		num_blocks = scsi_4btoul(cdb->length);
+		break;
+	}
+	default:
+		/*
+		 * We got a command we don't support.  This shouldn't
+		 * happen, commands should be filtered out above us.
+		 */
+		ctl_set_invalid_opcode(ctsio);
+		ctl_done((union ctl_io *)ctsio);
+
+		return (CTL_RETVAL_COMPLETE);
+		break; /* NOTREACHED */
+	}
+
+	/*
+	 * XXX KDM what do we do with the DPO and FUA bits?  FUA might be
+	 * interesting for us, but if RAIDCore is in write-back mode,
+	 * getting it to do write-through for a particular transaction may
+	 * not be possible.
+	 */
+	/*
+	 * We don't support relative addressing.  That also requires
+	 * supporting linked commands, which we don't do.
+	 */
+	if (reladdr != 0) {
+		ctl_set_invalid_field(ctsio,
+				      /*sks_valid*/ 1,
+				      /*command*/ 1,
+				      /*field*/ 1,
+				      /*bit_valid*/ 1,
+				      /*bit*/ 0);
+		ctl_done((union ctl_io *)ctsio);
+		return (CTL_RETVAL_COMPLETE);
+	}
+
+	/*
+	 * The first check is to make sure we're in bounds, the second
+	 * check is to catch wrap-around problems.  If the lba + num blocks
+	 * is less than the lba, then we've wrapped around and the block
+	 * range is invalid anyway.
+	 */
+	if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
+	 || ((lba + num_blocks) < lba)) {
+		ctl_set_lba_out_of_range(ctsio);
+		ctl_done((union ctl_io *)ctsio);
+		return (CTL_RETVAL_COMPLETE);
+	}
+
+	/*
+	 * According to SBC-3, a transfer length of 0 is not an error.
+	 * Note that this cannot happen with WRITE(6) or READ(6), since 0
+	 * translates to 256 blocks for those commands.
+	 */
+	if (num_blocks == 0) {
+		ctl_set_success(ctsio);
+		ctl_done((union ctl_io *)ctsio);
+		return (CTL_RETVAL_COMPLETE);
+	}
+
+	lbalen.lba = lba;
+	lbalen.len = num_blocks;
+	memcpy(ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, &lbalen,
+	       sizeof(lbalen));
+
+	CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
+
+	retval = lun->backend->data_submit((union ctl_io *)ctsio);
+
+	return (retval);
+}
+
+int
+ctl_report_luns(struct ctl_scsiio *ctsio)
+{
+	struct scsi_report_luns *cdb;
+	struct scsi_report_luns_data *lun_data;
+	struct ctl_lun *lun, *request_lun;
+	int num_luns, retval;
+	uint32_t alloc_len, lun_datalen;
+	int num_filled, well_known;
+	uint32_t initidx;
+
+	retval = CTL_RETVAL_COMPLETE;
+	well_known = 0;
+
+	cdb = (struct scsi_report_luns *)ctsio->cdb;
+
+	CTL_DEBUG_PRINT(("ctl_report_luns\n"));
+
+	mtx_lock(&control_softc->ctl_lock);
+	num_luns = control_softc->num_luns;
+	mtx_unlock(&control_softc->ctl_lock);
+
+	switch (cdb->select_report) {
+	case RPL_REPORT_DEFAULT:
+	case RPL_REPORT_ALL:
+		break;
+	case RPL_REPORT_WELLKNOWN:
+		well_known = 1;
+		num_luns = 0;
+		break;
+	default:
+		ctl_set_invalid_field(ctsio,
+				      /*sks_valid*/ 1,
+				      /*command*/ 1,
+				      /*field*/ 2,
+				      /*bit_valid*/ 0,
+				      /*bit*/ 0);
+		ctl_done((union ctl_io *)ctsio);
+		return (retval);
+		break; /* NOTREACHED */
+	}
+
+	alloc_len = scsi_4btoul(cdb->length);
+	/*
+	 * The initiator has to allocate at least 16 bytes for this request,
+	 * so he can at least get the header and the first LUN.  Otherwise
+	 * we reject the request (per SPC-3 rev 14, section 6.21).
+	 */
+	if (alloc_len < (sizeof(struct scsi_report_luns_data) +
+	    sizeof(struct scsi_report_luns_lundata))) {
+		ctl_set_invalid_field(ctsio,
+				      /*sks_valid*/ 1,
+				      /*command*/ 1,
+				      /*field*/ 6,
+				      /*bit_valid*/ 0,
+				      /*bit*/ 0);
+		ctl_done((union ctl_io *)ctsio);
+		return (retval);
+	}
+
+	request_lun = (struct ctl_lun *)
+		ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
+
+	lun_datalen = sizeof(*lun_data) +
+		(num_luns * sizeof(struct scsi_report_luns_lundata));
+
+	ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK);
+	if (ctsio->kern_data_ptr == NULL) {
+		ctsio->io_hdr.status = CTL_SCSI_ERROR;
+		ctsio->scsi_status = SCSI_STATUS_BUSY;
+		ctl_done((union ctl_io *)ctsio);
+		return (CTL_RETVAL_COMPLETE);
+	}
+
+	lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
+	ctsio->kern_sg_entries = 0;
+
+	if (lun_datalen < alloc_len) {
+		ctsio->residual = alloc_len - lun_datalen;
+		ctsio->kern_data_len = lun_datalen;
+		ctsio->kern_total_len = lun_datalen;
+	} else {
+		ctsio->residual = 0;
+		ctsio->kern_data_len = alloc_len;
+		ctsio->kern_total_len = alloc_len;
+	}
+	ctsio->kern_data_resid = 0;
+	ctsio->kern_rel_offset = 0;
+	ctsio->kern_sg_entries = 0;
+
+	initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
+
+	memset(lun_data, 0, lun_datalen);
+
+	/*
+	 * We set this to the actual data length, regardless of how much
+	 * space we actually have to return results.  If the user looks at
+	 * this value, he'll know whether or not he allocated enough space
+	 * and reissue the command if necessary.  We don't support well
+	 * known logical units, so if the user asks for that, return none.
+	 */
+	scsi_ulto4b(lun_datalen - 8, lun_data->length);
+
+	mtx_lock(&control_softc->ctl_lock);
+	for (num_filled = 0, lun = STAILQ_FIRST(&control_softc->lun_list);
+	     (lun != NULL) && (num_filled < num_luns);
+	     lun = STAILQ_NEXT(lun, links)) {
+
+		if (lun->lun <= 0xff) {
+			/*
+			 * Peripheral addressing method, bus number 0.
+			 */
+			lun_data->luns[num_filled].lundata[0] =
+				RPL_LUNDATA_ATYP_PERIPH;
+			lun_data->luns[num_filled].lundata[1] = lun->lun;
+			num_filled++;
+		} else if (lun->lun <= 0x3fff) {
+			/*
+			 * Flat addressing method.
+			 */
+			lun_data->luns[num_filled].lundata[0] =
+				RPL_LUNDATA_ATYP_FLAT |
+				(lun->lun & RPL_LUNDATA_FLAT_LUN_MASK);
+#ifdef OLDCTLHEADERS
+				(SRLD_ADDR_FLAT << SRLD_ADDR_SHIFT) |
+				(lun->lun & SRLD_BUS_LUN_MASK);
+#endif
+			lun_data->luns[num_filled].lundata[1] =
+#ifdef OLDCTLHEADERS
+				lun->lun >> SRLD_BUS_LUN_BITS;
+#endif
+				lun->lun >> RPL_LUNDATA_FLAT_LUN_BITS;
+			num_filled++;
+		} else {
+			printf("ctl_report_luns: bogus LUN number %jd, "
+			       "skipping\n", (intmax_t)lun->lun);
+		}
+		/*
+		 * According to SPC-3, rev 14 section 6.21:
+		 *
+		 * "The execution of a REPORT LUNS command to any valid and
+		 * installed logical unit shall clear the REPORTED LUNS DATA
+		 * HAS CHANGED unit attention condition for all logical
+		 * units of that target with respect to the requesting
+		 * initiator. A valid and installed logical unit is one
+		 * having a PERIPHERAL QUALIFIER of 000b in the standard
+		 * INQUIRY data (see 6.4.2)."
+		 *
+		 * If request_lun is NULL, the LUN this report luns command
+		 * was issued to is either disabled or doesn't exist. In that
+		 * case, we shouldn't clear any pending lun change unit
+		 * attention.
+		 */
+		if (request_lun != NULL)
+			lun->pending_sense[initidx].ua_pending &=
+				~CTL_UA_LUN_CHANGE;
+	}
+	mtx_unlock(&control_softc->ctl_lock);
+
+	/*
+	 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
+	 * this request.
+	 */
+	ctsio->scsi_status = SCSI_STATUS_OK;
+
+	ctsio->be_move_done = ctl_config_move_done;
+	ctl_datamove((union ctl_io *)ctsio);
+
+	return (retval);
+}
+
+int
+ctl_request_sense(struct ctl_scsiio *ctsio)
+{
+	struct scsi_request_sense *cdb;
+	struct scsi_sense_data *sense_ptr;
+	struct ctl_lun *lun;
+	uint32_t initidx;
+	int have_error;
+	ctl_sense_format sense_format;
+
+	cdb = (struct scsi_request_sense *)ctsio->cdb;
+
+	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
+
+	CTL_DEBUG_PRINT(("ctl_request_sense\n"));
+
+	/*
+	 * Determine which sense format the user wants.
+	 */
+	if (cdb->byte2 & SRS_DESC)
+		sense_format = CTL_SENSE_DESCRIPTOR;
+	else
+		sense_format = CTL_SENSE_FIXED;
+
+	ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
+	if (ctsio->kern_data_ptr == NULL) {
+		ctsio->io_hdr.status = CTL_SCSI_ERROR;
+		ctsio->scsi_status = SCSI_STATUS_BUSY;
+		ctl_done((union ctl_io *)ctsio);
+		return (CTL_RETVAL_COMPLETE);
+	}
+	sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
+	ctsio->kern_sg_entries = 0;
+
+	/*
+	 * struct scsi_sense_data, which is currently set to 256 bytes, is
+	 * larger than the largest allowed value for the length field in the
+	 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
+	 */
+	ctsio->residual = 0;
+	ctsio->kern_data_len = cdb->length;
+	ctsio->kern_total_len = cdb->length;
+
+	ctsio->kern_data_resid = 0;
+	ctsio->kern_rel_offset = 0;
+	ctsio->kern_sg_entries = 0;
+
+	/*
+	 * If we don't have a LUN, we don't have any pending sense.
+	 */
+	if (lun == NULL)
+		goto no_sense;
+
+	have_error = 0;
+	initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
+	/*
+	 * Check for pending sense, and then for pending unit attentions.
+	 * Pending sense gets returned first, then pending unit attentions.
+	 */
+	mtx_lock(&lun->ctl_softc->ctl_lock);
+	if (ctl_is_set(lun->have_ca, initidx)) {
+		ctl_sense_format stored_format;
+
+		/*
+		 * Check to see which sense format was used for the stored
+		 * sense data.
+		 */
+		stored_format = ctl_get_sense_format(
+		    &lun->pending_sense[initidx].sense);
+
+		/*
+		 * If the user requested a different sense format than the
+		 * one we stored, then we need to convert it to the other
+		 * format.  If we're going from descriptor to fixed format
+		 * sense data, we may lose things in translation, depending
+		 * on what options were used.
+		 */
+		if ((stored_format == CTL_SENSE_FIXED)
+		 && (sense_format == CTL_SENSE_DESCRIPTOR))
+			ctl_sense_to_desc((struct scsi_sense_data_fixed *)
+			    &lun->pending_sense[initidx].sense,
+			    (struct scsi_sense_data_desc *)sense_ptr);
+		else if ((stored_format == CTL_SENSE_DESCRIPTOR)
+		      && (sense_format == CTL_SENSE_FIXED))
+			ctl_sense_to_fixed((struct scsi_sense_data_desc *)
+			    &lun->pending_sense[initidx].sense,
+			    (struct scsi_sense_data_fixed *)sense_ptr);
+		else
+			memcpy(sense_ptr, &lun->pending_sense[initidx].sense,
+			       ctl_min(sizeof(*sense_ptr),
+			       sizeof(lun->pending_sense[initidx].sense)));
+
+		ctl_clear_mask(lun->have_ca, initidx);
+		have_error = 1;
+	} else if (lun->pending_sense[initidx].ua_pending != CTL_UA_NONE) {
+		ctl_ua_type ua_type;
+
+		ua_type = ctl_build_ua(lun->pending_sense[initidx].ua_pending,
+				       sense_ptr, sense_format);
+		if (ua_type != CTL_UA_NONE) {
+			have_error = 1;
+			/* We're reporting this UA, so clear it */
+			lun->pending_sense[initidx].ua_pending &= ~ua_type;
+		}
+	}
+	mtx_unlock(&lun->ctl_softc->ctl_lock);
+
+	/*
+	 * We already have a pending error, return it.
+	 */
+	if (have_error != 0) {
+		/*
+		 * We report the SCSI status as OK, since the status of the
+		 * request sense command itself is OK.
+		 */
+		ctsio->scsi_status = SCSI_STATUS_OK;
+
+		/*
+		 * We report 0 for the sense length, because we aren't doing
+		 * autosense in this case.  We're reporting sense as
+		 * parameter data.
+		 */
+		ctsio->sense_len = 0;
+
+		ctsio->be_move_done = ctl_config_move_done;
+		ctl_datamove((union ctl_io *)ctsio);
+
+		return (CTL_RETVAL_COMPLETE);
+	}
+
+no_sense:
+
+	/*
+	 * No sense information to report, so we report that everything is
+	 * okay.
+	 */
+	ctl_set_sense_data(sense_ptr,
+			   lun,
+			   sense_format,
+			   /*current_error*/ 1,
+			   /*sense_key*/ SSD_KEY_NO_SENSE,
+			   /*asc*/ 0x00,
+			   /*ascq*/ 0x00,
+			   SSD_ELEM_NONE);
+
+	ctsio->scsi_status = SCSI_STATUS_OK;
+
+	/*
+	 * We report 0 for the sense length, because we aren't doing
+	 * autosense in this case.  We're reporting sense as parameter data.
+	 */
+	ctsio->sense_len = 0;
+	ctsio->be_move_done = ctl_config_move_done;
+	ctl_datamove((union ctl_io *)ctsio);
+
+	return (CTL_RETVAL_COMPLETE);
+}
+
+int
+ctl_tur(struct ctl_scsiio *ctsio)
+{
+	struct ctl_lun *lun;
+
+	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
+
+	CTL_DEBUG_PRINT(("ctl_tur\n"));
+
+	if (lun == NULL)
+		return (-EINVAL);
+
+	ctsio->scsi_status = SCSI_STATUS_OK;
+	ctsio->io_hdr.status = CTL_SUCCESS;
+
+	ctl_done((union ctl_io *)ctsio);
+
+	return (CTL_RETVAL_COMPLETE);
+}
+
+#ifdef notyet
+static int
+ctl_cmddt_inquiry(struct ctl_scsiio *ctsio)
+{
+
+}
+#endif
+
+static int
+ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
+{
+	struct scsi_vpd_supported_pages *pages;
+	int sup_page_size;
+	struct ctl_lun *lun;
+
+	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
+
+	sup_page_size = sizeof(struct scsi_vpd_supported_pages) +
+		SCSI_EVPD_NUM_SUPPORTED_PAGES;
+	/*
+	 * XXX KDM GFP_???  We probably don't want to wait here,
+	 * unless we end up having a process/thread context.
+	 */
+	ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK);
+	if (ctsio->kern_data_ptr == NULL) {
+		ctsio->io_hdr.status = CTL_SCSI_ERROR;
+		ctsio->scsi_status = SCSI_STATUS_BUSY;
+		ctl_done((union ctl_io *)ctsio);
+		return (CTL_RETVAL_COMPLETE);
+	}
+	pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
+	ctsio->kern_sg_entries = 0;
+
+	if (sup_page_size < alloc_len) {
+		ctsio->residual = alloc_len - sup_page_size;
+		ctsio->kern_data_len = sup_page_size;
+		ctsio->kern_total_len = sup_page_size;
+	} else {
+		ctsio->residual = 0;
+		ctsio->kern_data_len = alloc_len;
+		ctsio->kern_total_len = alloc_len;
+	}
+	ctsio->kern_data_resid = 0;
+	ctsio->kern_rel_offset = 0;
+	ctsio->kern_sg_entries = 0;
+
+	memset(pages, 0, sup_page_size);
+
+	/*
+	 * The control device is always connected.  The disk device, on the
+	 * other hand, may not be online all the time.  Need to change this
+	 * to figure out whether the disk device is actually online or not.
+	 */
+	if (lun != NULL)
+		pages->device = (SID_QUAL_LU_CONNECTED << 5) |
+				lun->be_lun->lun_type;
+	else
+		pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
+
+	pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES;
+	/* Supported VPD pages */
+	pages->page_list[0] = SVPD_SUPPORTED_PAGES;
+	/* Serial Number */
+	pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER;
+	/* Device Identification */
+	pages->page_list[2] = SVPD_DEVICE_ID;
+
+	ctsio->scsi_status = SCSI_STATUS_OK;
+
+	ctsio->be_move_done = ctl_config_move_done;
+	ctl_datamove((union ctl_io *)ctsio);
+
+	return (CTL_RETVAL_COMPLETE);
+}
+
+static int
+ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
+{
+	struct scsi_vpd_unit_serial_number *sn_ptr;
+	struct ctl_lun *lun;
+#ifndef CTL_USE_BACKEND_SN
+	char tmpstr[32];
+#endif
+
+	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
+
+	/* XXX KDM which malloc flags here?? */
+	ctsio->kern_data_ptr = malloc(sizeof(*sn_ptr), M_CTL, M_WAITOK);
+	if (ctsio->kern_data_ptr == NULL) {
+		ctsio->io_hdr.status = CTL_SCSI_ERROR;
+		ctsio->scsi_status = SCSI_STATUS_BUSY;
+		ctl_done((union ctl_io *)ctsio);
+		return (CTL_RETVAL_COMPLETE);
+	}
+	sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
+	ctsio->kern_sg_entries = 0;
+
+	if (sizeof(*sn_ptr) < alloc_len) {
+		ctsio->residual = alloc_len - sizeof(*sn_ptr);
+		ctsio->kern_data_len = sizeof(*sn_ptr);
+		ctsio->kern_total_len = sizeof(*sn_ptr);
+	} else {
+		ctsio->residual = 0;
+		ctsio->kern_data_len = alloc_len;
+		ctsio->kern_total_len = alloc_len;
+	}
+	ctsio->kern_data_resid = 0;
+	ctsio->kern_rel_offset = 0;
+	ctsio->kern_sg_entries = 0;
+
+	memset(sn_ptr, 0, sizeof(*sn_ptr));
+
+	/*
+	 * The control device is always connected.  The disk device, on the
+	 * other hand, may not be online all the time.  Need to change this
+	 * to figure out whether the disk device is actually online or not.
+	 */
+	if (lun != NULL)
+		sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
+				  lun->be_lun->lun_type;
+	else
+		sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
+
+	sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
+	sn_ptr->length = ctl_min(sizeof(*sn_ptr) - 4, CTL_SN_LEN);
+#ifdef CTL_USE_BACKEND_SN
+	/*
+	 * If we don't have a LUN, we just leave the serial number as
+	 * all spaces.
+	 */
+	memset(sn_ptr->serial_num, 0x20, sizeof(sn_ptr->serial_num));
+	if (lun != NULL) {
+		strncpy((char *)sn_ptr->serial_num,
+			(char *)lun->be_lun->serial_num, CTL_SN_LEN);
+	}
+#else
+	/*
+	 * Note that we're using a non-unique serial number here,
+	 */
+	snprintf(tmpstr, sizeof(tmpstr), "MYSERIALNUMIS000");
+	memset(sn_ptr->serial_num, 0x20, sizeof(sn_ptr->serial_num));
+	strncpy(sn_ptr->serial_num, tmpstr, ctl_min(CTL_SN_LEN,
+		ctl_min(sizeof(tmpstr), sizeof(*sn_ptr) - 4)));
+#endif
+	ctsio->scsi_status = SCSI_STATUS_OK;
+
+	ctsio->be_move_done = ctl_config_move_done;
+	ctl_datamove((union ctl_io *)ctsio);
+
+	return (CTL_RETVAL_COMPLETE);
+}
+
+
+static int
+ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
+{
+	struct scsi_vpd_device_id *devid_ptr;
+	struct scsi_vpd_id_descriptor *desc, *desc1;
+	struct scsi_vpd_id_descriptor *desc2, *desc3; /* for types 4h and 5h */
+	struct scsi_vpd_id_t10 *t10id;
+	struct ctl_softc *ctl_softc;
+	struct ctl_lun *lun;
+	struct ctl_frontend *fe;
+#ifndef CTL_USE_BACKEND_SN
+	char tmpstr[32];
+#endif /* CTL_USE_BACKEND_SN */
+	int devid_len;
+
+	ctl_softc = control_softc;
+	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
+
+	devid_len = sizeof(struct scsi_vpd_device_id) +
+		sizeof(struct scsi_vpd_id_descriptor) +
+		sizeof(struct scsi_vpd_id_t10) + CTL_DEVID_LEN +
+		sizeof(struct scsi_vpd_id_descriptor) + CTL_WWPN_LEN +
+		sizeof(struct scsi_vpd_id_descriptor) +
+		sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
+		sizeof(struct scsi_vpd_id_descriptor) +
+		sizeof(struct scsi_vpd_id_trgt_port_grp_id);
+
+	/* XXX KDM which malloc flags here ?? */
+	ctsio->kern_data_ptr = malloc(devid_len, M_CTL, M_WAITOK);
+	if (ctsio->kern_data_ptr == NULL) {
+		ctsio->io_hdr.status = CTL_SCSI_ERROR;
+		ctsio->scsi_status = SCSI_STATUS_BUSY;
+		ctl_done((union ctl_io *)ctsio);
+		return (CTL_RETVAL_COMPLETE);
+	}
+	devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
+	ctsio->kern_sg_entries = 0;
+
+	if (devid_len < alloc_len) {
+		ctsio->residual = alloc_len - devid_len;
+		ctsio->kern_data_len = devid_len;
+		ctsio->kern_total_len = devid_len;
+	} else {
+		ctsio->residual = 0;
+		ctsio->kern_data_len = alloc_len;
+		ctsio->kern_total_len = alloc_len;
+	}
+	ctsio->kern_data_resid = 0;
+	ctsio->kern_rel_offset = 0;
+	ctsio->kern_sg_entries = 0;
+
+	desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
+	t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
+	desc1 = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
+		sizeof(struct scsi_vpd_id_t10) + CTL_DEVID_LEN);
+	desc2 = (struct scsi_vpd_id_descriptor *)(&desc1->identifier[0] +
+	          CTL_WWPN_LEN);
+	desc3 = (struct scsi_vpd_id_descriptor *)(&desc2->identifier[0] +
+	         sizeof(struct scsi_vpd_id_rel_trgt_port_id));
+	memset(devid_ptr, 0, devid_len);
+
+	/*
+	 * The control device is always connected.  The disk device, on the
+	 * other hand, may not be online all the time.
+	 */
+	if (lun != NULL)
+		devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
+				     lun->be_lun->lun_type;
+	else
+		devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
+
+	devid_ptr->page_code = SVPD_DEVICE_ID;
+
+	scsi_ulto2b(devid_len - 4, devid_ptr->length);
+
+	mtx_lock(&ctl_softc->ctl_lock);
+
+	fe = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)];
+
+	/*
+	 * For Fibre channel,
+	 */
+	if (fe->port_type == CTL_PORT_FC)
+	{
+		desc->proto_codeset = (SCSI_PROTO_FC << 4) |
+				      SVPD_ID_CODESET_ASCII;
+        	desc1->proto_codeset = (SCSI_PROTO_FC << 4) |
+		              SVPD_ID_CODESET_BINARY;
+	}
+	else
+	{
+		desc->proto_codeset = (SCSI_PROTO_SPI << 4) |
+				      SVPD_ID_CODESET_ASCII;
+        	desc1->proto_codeset = (SCSI_PROTO_SPI << 4) |
+		              SVPD_ID_CODESET_BINARY;
+	}
+	desc2->proto_codeset = desc3->proto_codeset = desc1->proto_codeset;
+	mtx_unlock(&ctl_softc->ctl_lock);
+
+	/*
+	 * We're using a LUN association here.  i.e., this device ID is a
+	 * per-LUN identifier.
+	 */
+	desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
+	desc->length = sizeof(*t10id) + CTL_DEVID_LEN;
+	strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
+
+	/*
+	 * desc1 is for the WWPN which is a port asscociation.
+	 */
+	desc1->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | SVPD_ID_TYPE_NAA;
+	desc1->length = CTL_WWPN_LEN;
+	/* XXX Call Reggie's get_WWNN func here then add port # to the end */
+	/* For testing just create the WWPN */
+#if 0
+	ddb_GetWWNN((char *)desc1->identifier);
+
+	/* NOTE: if the port is 0 or 8 we don't want to subtract 1 */
+	/* This is so Copancontrol will return something sane */
+	if (ctsio->io_hdr.nexus.targ_port!=0 &&
+	    ctsio->io_hdr.nexus.targ_port!=8)
+		desc1->identifier[7] += ctsio->io_hdr.nexus.targ_port-1;
+	else
+		desc1->identifier[7] += ctsio->io_hdr.nexus.targ_port;
+#endif
+
+	be64enc(desc1->identifier, fe->wwpn);
+
+	/*
+	 * desc2 is for the Relative Target Port(type 4h) identifier
+	 */
+	desc2->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT
+	                 | SVPD_ID_TYPE_RELTARG;
+	desc2->length = 4;
+//#if 0
+	/* NOTE: if the port is 0 or 8 we don't want to subtract 1 */
+	/* This is so Copancontrol will return something sane */
+	if (ctsio->io_hdr.nexus.targ_port!=0 &&
+	    ctsio->io_hdr.nexus.targ_port!=8)
+		desc2->identifier[3] = ctsio->io_hdr.nexus.targ_port - 1;
+	else
+	        desc2->identifier[3] = ctsio->io_hdr.nexus.targ_port;
+//#endif
+
+	/*
+	 * desc3 is for the Target Port Group(type 5h) identifier
+	 */
+	desc3->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT
+	                 | SVPD_ID_TYPE_TPORTGRP;
+	desc3->length = 4;
+	if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS || ctl_is_single)
+		desc3->identifier[3] = 1;
+	else
+		desc3->identifier[3] = 2;
+
+#ifdef CTL_USE_BACKEND_SN
+	/*
+	 * If we've actually got a backend, copy the device id from the
+	 * per-LUN data.  Otherwise, set it to all spaces.
+	 */
+	if (lun != NULL) {
+		/*
+		 * Copy the backend's LUN ID.
+		 */
+		strncpy((char *)t10id->vendor_spec_id,
+			(char *)lun->be_lun->device_id, CTL_DEVID_LEN);
+	} else {
+		/*
+		 * No backend, set this to spaces.
+		 */
+		memset(t10id->vendor_spec_id, 0x20, CTL_DEVID_LEN);
+	}
+#else
+	snprintf(tmpstr, sizeof(tmpstr), "MYDEVICEIDIS%4d",
+		 (lun != NULL) ?  (int)lun->lun : 0);
+	strncpy(t10id->vendor_spec_id, tmpstr, ctl_min(CTL_DEVID_LEN,
+		sizeof(tmpstr)));
+#endif
+
+	ctsio->scsi_status = SCSI_STATUS_OK;
+
+	ctsio->be_move_done = ctl_config_move_done;
+	ctl_datamove((union ctl_io *)ctsio);
+
+	return (CTL_RETVAL_COMPLETE);
+}
+
+static int
+ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
+{
+	struct scsi_inquiry *cdb;
+	int alloc_len, retval;
+
+	cdb = (struct scsi_inquiry *)ctsio->cdb;
+
+	retval = CTL_RETVAL_COMPLETE;
+
+	alloc_len = scsi_2btoul(cdb->length);
+
+	switch (cdb->page_code) {
+	case SVPD_SUPPORTED_PAGES:
+		retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
+		break;
+	case SVPD_UNIT_SERIAL_NUMBER:
+		retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
+		break;
+	case SVPD_DEVICE_ID:
+		retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
+		break;
+	default:
+		ctl_set_invalid_field(ctsio,
+				      /*sks_valid*/ 1,
+				      /*command*/ 1,
+				      /*field*/ 2,
+				      /*bit_valid*/ 0,
+				      /*bit*/ 0);
+		ctl_done((union ctl_io *)ctsio);
+		retval = CTL_RETVAL_COMPLETE;
+		break;
+	}
+
+	return (retval);
+}
+
+static int
+ctl_inquiry_std(struct ctl_scsiio *ctsio)
+{
+	struct scsi_inquiry_data *inq_ptr;
+	struct scsi_inquiry *cdb;
+	struct ctl_softc *ctl_softc;
+	struct ctl_lun *lun;
+	uint32_t alloc_len;
+	int is_fc;
+
+	ctl_softc = control_softc;
+
+	/*
+	 * Figure out whether we're talking to a Fibre Channel port or not.
+	 * We treat the ioctl front end, and any SCSI adapters, as packetized
+	 * SCSI front ends.
+	 */
+	mtx_lock(&ctl_softc->ctl_lock);
+	if (ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type !=
+	    CTL_PORT_FC)
+		is_fc = 0;
+	else
+		is_fc = 1;
+	mtx_unlock(&ctl_softc->ctl_lock);
+
+	lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
+	cdb = (struct scsi_inquiry *)ctsio->cdb;
+	alloc_len = scsi_2btoul(cdb->length);
+
+	/*
+	 * We malloc the full inquiry data size here and fill it
+	 * in.  If the user only asks for less, we'll give him
+	 * that much.
+	 */
+	/* XXX KDM what malloc flags should we use here?? */
+	ctsio->kern_data_ptr = malloc(sizeof(*inq_ptr), M_CTL, M_WAITOK);
+	if (ctsio->kern_data_ptr == NULL) {
+		ctsio->io_hdr.status = CTL_SCSI_ERROR;
+		ctsio->scsi_status = SCSI_STATUS_BUSY;
+		ctl_done((union ctl_io *)ctsio);
+		return (CTL_RETVAL_COMPLETE);
+	}
+	inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
+	ctsio->kern_sg_entries = 0;
+	ctsio->kern_data_resid = 0;
+	ctsio->kern_rel_offset = 0;
+
+	if (sizeof(*inq_ptr) < alloc_len) {
+		ctsio->residual = alloc_len - sizeof(*inq_ptr);
+		ctsio->kern_data_len = sizeof(*inq_ptr);
+		ctsio->kern_total_len = sizeof(*inq_ptr);
+	} else {
+		ctsio->residual = 0;
+		ctsio->kern_data_len = alloc_len;
+		ctsio->kern_total_len = alloc_len;
+	}
+
+	memset(inq_ptr, 0, sizeof(*inq_ptr));
+
+	/*
+	 * The control device is always connected.  The disk device, on the
+	 * other hand, may not be online all the time.  If we don't have a
+	 * LUN mapping, we'll just say it's offline.
+	 */
+	if (lun != NULL)
+		inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
+				  lun->be_lun->lun_type;
+	else
+		inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
+
+	/* RMB in byte 2 is 0 */
+	inq_ptr->version = SCSI_REV_SPC3;
+
+	/*
+	 * According to SAM-3, even if a device only supports a single
+	 * level of LUN addressing, it should still set the HISUP bit:
+	 *
+	 * 4.9.1 Logical unit numbers overview
+	 *
+	 * All logical unit number formats described in this standard are
+	 * hierarchical in structure even when only a single level in that
+	 * hierarchy is used. The HISUP bit shall be set to one in the
+	 * standard INQUIRY data (see SPC-2) when any logical unit number
+	 * format described in this standard is used.  Non-hierarchical
+	 * formats are outside the scope of this standard.
+	 *
+	 * Therefore we set the HiSup bit here.
+	 *
+	 * The reponse format is 2, per SPC-3.
+	 */
+	inq_ptr->response_format = SID_HiSup | 2;
+
+	inq_ptr->additional_length = sizeof(*inq_ptr) - 4;
+	CTL_DEBUG_PRINT(("additional_length = %d\n",
+			 inq_ptr->additional_length));
+
+	inq_ptr->spc3_flags = SPC3_SID_TPGS_IMPLICIT;
+	/* 16 bit addressing */
+	if (is_fc == 0)
+		inq_ptr->spc2_flags = SPC2_SID_ADDR16;
+	/* XXX set the SID_MultiP bit here if we're actually going to
+	   respond on multiple ports */
+	inq_ptr->spc2_flags |= SPC2_SID_MultiP;
+
+	/* 16 bit data bus, synchronous transfers */
+	/* XXX these flags don't apply for FC */
+	if (is_fc == 0)
+		inq_ptr->flags = SID_WBus16 | SID_Sync;
+	/*
+	 * XXX KDM do we want to support tagged queueing on the control
+	 * device at all?
+	 */
+	if ((lun == NULL)
+	 || (lun->be_lun->lun_type != T_PROCESSOR))
+		inq_ptr->flags |= SID_CmdQue;
+	/*
+	 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
+	 * We have 8 bytes for the vendor name, and 16 bytes for the device
+	 * name and 4 bytes for the revision.
+	 */
+	strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
+	if (lun == NULL) {
+		strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT);
+	} else {
+		switch (lun->be_lun->lun_type) {
+		case T_DIRECT:
+			strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT);
+			break;
+		case T_PROCESSOR:
+			strcpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT);
+			break;
+		default:
+			strcpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT);
+			break;
+		}
+	}
+
+	/*
+	 * XXX make this a macro somewhere so it automatically gets
+	 * incremented when we make changes.
+	 */
+	strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
+
+	/*
+	 * For parallel SCSI, we support double transition and single
+	 * transition clocking.  We also support QAS (Quick Arbitration
+	 * and Selection) and Information Unit transfers on both the
+	 * control and array devices.
+	 */
+	if (is_fc == 0)
+		inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
+				    SID_SPI_IUS;
+
+	/* SAM-3 */
+	scsi_ulto2b(0x0060, inq_ptr->version1);
+	/* SPC-3 (no version claimed) XXX should we claim a version? */
+	scsi_ulto2b(0x0300, inq_ptr->version2);
+	if (is_fc) {
+		/* FCP-2 ANSI INCITS.350:2003 */
+		scsi_ulto2b(0x0917, inq_ptr->version3);
+	} else {
+		/* SPI-4 ANSI INCITS.362:200x */
+		scsi_ulto2b(0x0B56, inq_ptr->version3);
+	}
+
+	if (lun == NULL) {
+		/* SBC-2 (no version claimed) XXX should we claim a version? */
+		scsi_ulto2b(0x0320, inq_ptr->version4);
+	} else {
+		switch (lun->be_lun->lun_type) {
+		case T_DIRECT:
+			/*
+			 * SBC-2 (no version claimed) XXX should we claim a
+			 * version?
+			 */
+			scsi_ulto2b(0x0320, inq_ptr->version4);
+			break;
+		case T_PROCESSOR:
+		default:
+			break;
+		}
+	}
+	sprintf((char *)inq_ptr->vendor_specific1, "Copyright (C) 2004, COPAN "
+		"Systems, Inc.  All Rights Reserved.");
+
+	ctsio->scsi_status = SCSI_STATUS_OK;
+	if (ctsio->kern_data_len > 0) {
+		ctsio->be_move_done = ctl_config_move_done;
+		ctl_datamove((union ctl_io *)ctsio);
+	} else {
+		ctsio->io_hdr.status = CTL_SUCCESS;
+		ctl_done((union ctl_io *)ctsio);
+	}
+
+	return (CTL_RETVAL_COMPLETE);
+}
+
+int
+ctl_inquiry(struct ctl_scsiio *ctsio)
+{
+	struct scsi_inquiry *cdb;
+	int retval;
+
+	cdb = (struct scsi_inquiry *)ctsio->cdb;
+
+	retval = 0;
+
+	CTL_DEBUG_PRINT(("ctl_inquiry\n"));
+
+	/*
+	 * Right now, we don't support the CmdDt inquiry information.
+	 * This would be nice to support in the future.  When we do
+	 * support it, we should change this test so that it checks to make
+	 * sure SI_EVPD and SI_CMDDT aren't both set at the same time.
+	 */
+#ifdef notyet
+	if (((cdb->byte2 & SI_EVPD)
+	 && (cdb->byte2 & SI_CMDDT)))
+#endif
+	if (cdb->byte2 & SI_CMDDT) {
+		/*
+		 * Point to the SI_CMDDT bit.  We might change this
+		 * when we support SI_CMDDT, but since both bits would be
+		 * "wrong", this should probably just stay as-is then.
+		 */
+		ctl_set_invalid_field(ctsio,
+				      /*sks_valid*/ 1,
+				      /*command*/ 1,
+				      /*field*/ 1,
+				      /*bit_valid*/ 1,
+				      /*bit*/ 1);
+		ctl_done((union ctl_io *)ctsio);
+		return (CTL_RETVAL_COMPLETE);
+	}
+	if (cdb->byte2 & SI_EVPD)
+		retval = ctl_inquiry_evpd(ctsio);
+#ifdef notyet
+	else if (cdb->byte2 & SI_CMDDT)
+		retval = ctl_inquiry_cmddt(ctsio);
+#endif
+	else
+		retval = ctl_inquiry_std(ctsio);
+
+	return (retval);
+}
+
+/*
+ * For known CDB types, parse the LBA and length.
+ */
+static int
+ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len)
+{
+	if (io->io_hdr.io_type != CTL_IO_SCSI)
+		return (1);
+
+	switch (io->scsiio.cdb[0]) {
+	case READ_6:
+	case WRITE_6: {
+		struct scsi_rw_6 *cdb;
+
+		cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
+
+		*lba = scsi_3btoul(cdb->addr);
+		/* only 5 bits are valid in the most significant address byte */
+		*lba &= 0x1fffff;
+		*len = cdb->length;
+		break;
+	}
+	case READ_10:
+	case WRITE_10: {
+		struct scsi_rw_10 *cdb;
+
+		cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
+
+		*lba = scsi_4btoul(cdb->addr);
+		*len = scsi_2btoul(cdb->length);
+		break;
+	}
+	case WRITE_VERIFY_10: {
+		struct scsi_write_verify_10 *cdb;
+
+		cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
+
+		*lba = scsi_4btoul(cdb->addr);
+		*len = scsi_2btoul(cdb->length);
+		break;
+	}
+	case READ_12:
+	case WRITE_12: {
+		struct scsi_rw_12 *cdb;
+
+		cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
+
+		*lba = scsi_4btoul(cdb->addr);
+		*len = scsi_4btoul(cdb->length);
+		break;
+	}
+	case WRITE_VERIFY_12: {
+		struct scsi_write_verify_12 *cdb;
+
+		cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
+
+		*lba = scsi_4btoul(cdb->addr);
+		*len = scsi_4btoul(cdb->length);
+		break;
+	}
+	case READ_16:
+	case WRITE_16: {
+		struct scsi_rw_16 *cdb;
+
+		cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
+
+		*lba = scsi_8btou64(cdb->addr);
+		*len = scsi_4btoul(cdb->length);
+		break;
+	}
+	case WRITE_VERIFY_16: {
+		struct scsi_write_verify_16 *cdb;
+
+		cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
+
+		
+		*lba = scsi_8btou64(cdb->addr);
+		*len = scsi_4btoul(cdb->length);
+		break;
+	}
+	default:
+		return (1);
+		break; /* NOTREACHED */
+	}
+
+	return (0);
+}
+
+static ctl_action
+ctl_extent_check_lba(uint64_t lba1, uint32_t len1, uint64_t lba2, uint32_t len2)
+{
+	uint64_t endlba1, endlba2;
+
+	endlba1 = lba1 + len1 - 1;
+	endlba2 = lba2 + len2 - 1;
+
+	if ((endlba1 < lba2)
+	 || (endlba2 < lba1))
+		return (CTL_ACTION_PASS);
+	else
+		return (CTL_ACTION_BLOCK);
+}
+
+static ctl_action
+ctl_extent_check(union ctl_io *io1, union ctl_io *io2)
+{
+	uint64_t lba1, lba2;
+	uint32_t len1, len2;
+	int retval;
+
+	retval = ctl_get_lba_len(io1, &lba1, &len1);
+	if (retval != 0)
+		return (CTL_ACTION_ERROR);
+
+	retval = ctl_get_lba_len(io2, &lba2, &len2);
+	if (retval != 0)
+		return (CTL_ACTION_ERROR);
+
+	return (ctl_extent_check_lba(lba1, len1, lba2, len2));
+}
+
+static ctl_action
+ctl_check_for_blockage(union ctl_io *pending_io, union ctl_io *ooa_io)
+{
+	struct ctl_cmd_entry *pending_entry, *ooa_entry;
+	ctl_serialize_action *serialize_row;
+
+	/*
+	 * The initiator attempted multiple untagged commands at the same
+	 * time.  Can't do that.
+	 */
+	if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
+	 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
+	 && ((pending_io->io_hdr.nexus.targ_port ==
+	      ooa_io->io_hdr.nexus.targ_port)
+	  && (pending_io->io_hdr.nexus.initid.id ==
+	      ooa_io->io_hdr.nexus.initid.id))
+	 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
+		return (CTL_ACTION_OVERLAP);
+
+	/*
+	 * The initiator attempted to send multiple tagged commands with
+	 * the same ID.  (It's fine if different initiators have the same
+	 * tag ID.)
+	 *
+	 * Even if all of those conditions are true, we don't kill the I/O
+	 * if the command ahead of us has been aborted.  We won't end up
+	 * sending it to the FETD, and it's perfectly legal to resend a
+	 * command with the same tag number as long as the previous
+	 * instance of this tag number has been aborted somehow.
+	 */
+	if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
+	 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
+	 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
+	 && ((pending_io->io_hdr.nexus.targ_port ==
+	      ooa_io->io_hdr.nexus.targ_port)
+	  && (pending_io->io_hdr.nexus.initid.id ==
+	      ooa_io->io_hdr.nexus.initid.id))
+	 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
+		return (CTL_ACTION_OVERLAP_TAG);
+
+	/*
+	 * If we get a head of queue tag, SAM-3 says that we should
+	 * immediately execute it.
+	 *
+	 * What happens if this command would normally block for some other
+	 * reason?  e.g. a request sense with a head of queue tag
+	 * immediately after a write.  Normally that would block, but this
+	 * will result in its getting executed immediately...
+	 *
+	 * We currently return "pass" instead of "skip", so we'll end up
+	 * going through the rest of the queue to check for overlapped tags.
+	 *
+	 * XXX KDM check for other types of blockage first??
+	 */
+	if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
+		return (CTL_ACTION_PASS);
+
+	/*
+	 * Ordered tags have to block until all items ahead of them
+	 * have completed.  If we get called with an ordered tag, we always
+	 * block, if something else is ahead of us in the queue.
+	 */
+	if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
+		return (CTL_ACTION_BLOCK);
+
+	/*
+	 * Simple tags get blocked until all head of queue and ordered tags
+	 * ahead of them have completed.  I'm lumping untagged commands in
+	 * with simple tags here.  XXX KDM is that the right thing to do?
+	 */
+	if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
+	  || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
+	 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
+	  || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
+		return (CTL_ACTION_BLOCK);
+
+	pending_entry = &ctl_cmd_table[pending_io->scsiio.cdb[0]];
+	ooa_entry = &ctl_cmd_table[ooa_io->scsiio.cdb[0]];
+
+	serialize_row = ctl_serialize_table[ooa_entry->seridx];
+
+	switch (serialize_row[pending_entry->seridx]) {
+	case CTL_SER_BLOCK:
+		return (CTL_ACTION_BLOCK);
+		break; /* NOTREACHED */
+	case CTL_SER_EXTENT:
+		return (ctl_extent_check(pending_io, ooa_io));
+		break; /* NOTREACHED */
+	case CTL_SER_PASS:
+		return (CTL_ACTION_PASS);
+		break; /* NOTREACHED */
+	case CTL_SER_SKIP:
+		return (CTL_ACTION_SKIP);
+		break;
+	default:
+		panic("invalid serialization value %d",
+		      serialize_row[pending_entry->seridx]);
+		break; /* NOTREACHED */
+	}
+
+	return (CTL_ACTION_ERROR);
+}
+
+/*
+ * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
+ * Assumptions:
+ * - caller holds ctl_lock
+ * - pending_io is generally either incoming, or on the blocked queue
+ * - starting I/O is the I/O we want to start the check with.
+ */
+static ctl_action
+ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
+	      union ctl_io *starting_io)
+{
+	union ctl_io *ooa_io;
+	ctl_action action;
+
+	/*
+	 * Run back along the OOA queue, starting with the current
+	 * blocked I/O and going through every I/O before it on the
+	 * queue.  If starting_io is NULL, we'll just end up returning
+	 * CTL_ACTION_PASS.
+	 */
+	for (ooa_io = starting_io; ooa_io != NULL;
+	     ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
+	     ooa_links)){
+
+		/*
+		 * This routine just checks to see whether
+		 * cur_blocked is blocked by ooa_io, which is ahead
+		 * of it in the queue.  It doesn't queue/dequeue
+		 * cur_blocked.
+		 */
+		action = ctl_check_for_blockage(pending_io, ooa_io);
+		switch (action) {
+		case CTL_ACTION_BLOCK:
+		case CTL_ACTION_OVERLAP:
+		case CTL_ACTION_OVERLAP_TAG:
+		case CTL_ACTION_SKIP:
+		case CTL_ACTION_ERROR:
+			return (action);
+			break; /* NOTREACHED */
+		case CTL_ACTION_PASS:
+			break;
+		default:
+			panic("invalid action %d", action);
+			break;  /* NOTREACHED */
+		}
+	}
+
+	return (CTL_ACTION_PASS);
+}
+
+/*
+ * Assumptions:
+ * - An I/O has just completed, and has been removed from the per-LUN OOA
+ *   queue, so some items on the blocked queue may now be unblocked.
+ * - The caller holds ctl_softc->ctl_lock
+ */
+static int
+ctl_check_blocked(struct ctl_lun *lun)
+{
+	union ctl_io *cur_blocked, *next_blocked;
+
+	/*
+	 * Run forward from the head of the blocked queue, checking each
+	 * entry against the I/Os prior to it on the OOA queue to see if
+	 * there is still any blockage.
+	 *
+	 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
+	 * with our removing a variable on it while it is traversing the
+	 * list.
+	 */
+	for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
+	     cur_blocked != NULL; cur_blocked = next_blocked) {
+		union ctl_io *prev_ooa;
+		ctl_action action;
+
+		next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
+							  blocked_links);
+
+		prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
+						      ctl_ooaq, ooa_links);
+
+		/*
+		 * If cur_blocked happens to be the first item in the OOA
+		 * queue now, prev_ooa will be NULL, and the action
+		 * returned will just be CTL_ACTION_PASS.
+		 */
+		action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
+
+		switch (action) {
+		case CTL_ACTION_BLOCK:
+			/* Nothing to do here, still blocked */
+			break;
+		case CTL_ACTION_OVERLAP:
+		case CTL_ACTION_OVERLAP_TAG:
+			/*
+			 * This shouldn't happen!  In theory we've already
+			 * checked this command for overlap...
+			 */
+			break;
+		case CTL_ACTION_PASS:
+		case CTL_ACTION_SKIP: {
+			struct ctl_softc *softc;
+			struct ctl_cmd_entry *entry;
+			uint32_t initidx;
+			uint8_t opcode;
+			int isc_retval;
+
+			/*
+			 * The skip case shouldn't happen, this transaction
+			 * should have never made it onto the blocked queue.
+			 */
+			/*
+			 * This I/O is no longer blocked, we can remove it
+			 * from the blocked queue.  Since this is a TAILQ
+			 * (doubly linked list), we can do O(1) removals
+			 * from any place on the list.
+			 */
+			TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
+				     blocked_links);
+			cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
+
+			if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){
+				/*
+				 * Need to send IO back to original side to
+				 * run
+				 */
+				union ctl_ha_msg msg_info;
+
+				msg_info.hdr.original_sc =
+					cur_blocked->io_hdr.original_sc;
+				msg_info.hdr.serializing_sc = cur_blocked;
+				msg_info.hdr.msg_type = CTL_MSG_R2R;
+				if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
+				     &msg_info, sizeof(msg_info), 0)) >
+				     CTL_HA_STATUS_SUCCESS) {
+					printf("CTL:Check Blocked error from "
+					       "ctl_ha_msg_send %d\n",
+					       isc_retval);
+				}
+				break;
+			}
+			opcode = cur_blocked->scsiio.cdb[0];
+			entry = &ctl_cmd_table[opcode];
+			softc = control_softc;
+
+			initidx = ctl_get_initindex(&cur_blocked->io_hdr.nexus);
+
+			/*
+			 * Check this I/O for LUN state changes that may
+			 * have happened while this command was blocked.
+			 * The LUN state may have been changed by a command
+			 * ahead of us in the queue, so we need to re-check
+			 * for any states that can be caused by SCSI
+			 * commands.
+			 */
+			if (ctl_scsiio_lun_check(softc, lun, entry,
+						 &cur_blocked->scsiio) == 0) {
+				cur_blocked->io_hdr.flags |=
+				                      CTL_FLAG_IS_WAS_ON_RTR;
+				STAILQ_INSERT_TAIL(&lun->ctl_softc->rtr_queue,
+						   &cur_blocked->io_hdr, links);
+				/*
+				 * In the non CTL_DONE_THREAD case, we need
+				 * to wake up the work thread here.  When
+				 * we're processing completed requests from
+				 * the work thread context, we'll pop back
+				 * around and end up pulling things off the
+				 * RtR queue.  When we aren't processing
+				 * things from the work thread context,
+				 * though, we won't ever check the RtR queue.
+				 * So we need to wake up the thread to clear
+				 * things off the queue.  Otherwise this
+				 * transaction will just sit on the RtR queue
+				 * until a new I/O comes in.  (Which may or
+				 * may not happen...)
+				 */
+#ifndef CTL_DONE_THREAD
+				ctl_wakeup_thread();
+#endif
+			} else
+				ctl_done_lock(cur_blocked, /*have_lock*/ 1);
+			break;
+		}
+		default:
+			/*
+			 * This probably shouldn't happen -- we shouldn't
+			 * get CTL_ACTION_ERROR, or anything else.
+			 */
+			break;
+		}
+	}
+
+	return (CTL_RETVAL_COMPLETE);
+}
+
+/*
+ * This routine (with one exception) checks LUN flags that can be set by
+ * commands ahead of us in the OOA queue.  These flags have to be checked
+ * when a command initially comes in, and when we pull a command off the
+ * blocked queue and are preparing to execute it.  The reason we have to
+ * check these flags for commands on the blocked queue is that the LUN
+ * state may have been changed by a command ahead of us while we're on the
+ * blocked queue.
+ *
+ * Ordering is somewhat important with these checks, so please pay
+ * careful attention to the placement of any new checks.
+ */
+static int
+ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
+		     struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
+{
+	int retval;
+
+	retval = 0;
+
+	/*
+	 * If this shelf is a secondary shelf controller, we have to reject
+	 * any media access commands.
+	 */
+#if 0
+	/* No longer needed for HA */
+	if (((ctl_softc->flags & CTL_FLAG_MASTER_SHELF) == 0)
+	 && ((entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0)) {
+		ctl_set_lun_standby(ctsio);
+		retval = 1;
+		goto bailout;
+	}
+#endif
+
+	/*
+	 * Check for a reservation conflict.  If this command isn't allowed
+	 * even on reserved LUNs, and if this initiator isn't the one who
+	 * reserved us, reject the command with a reservation conflict.
+	 */
+	if ((lun->flags & CTL_LUN_RESERVED)
+	 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
+		if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id)
+		 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port)
+		 || (ctsio->io_hdr.nexus.targ_target.id !=
+		     lun->rsv_nexus.targ_target.id)) {
+			ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
+			ctsio->io_hdr.status = CTL_SCSI_ERROR;
+			retval = 1;
+			goto bailout;
+		}
+	}
+
+	if ( (lun->flags & CTL_LUN_PR_RESERVED)
+	 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV) == 0)) {
+		uint32_t residx;
+
+		residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
+		/*
+		 * if we aren't registered or it's a res holder type
+		 * reservation and this isn't the res holder then set a
+		 * conflict.
+		 * NOTE: Commands which might be allowed on write exclusive
+		 * type reservations are checked in the particular command
+		 * for a conflict. Read and SSU are the only ones.
+		 */
+		if (!lun->per_res[residx].registered
+		 || (residx != lun->pr_res_idx && lun->res_type < 4)) {
+			ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
+			ctsio->io_hdr.status = CTL_SCSI_ERROR;
+			retval = 1;
+			goto bailout;
+		}
+
+	}
+
+	if ((lun->flags & CTL_LUN_OFFLINE)
+	 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) {
+		ctl_set_lun_not_ready(ctsio);
+		retval = 1;
+		goto bailout;
+	}
+
+	/*
+	 * If the LUN is stopped, see if this particular command is allowed
+	 * for a stopped lun.  Otherwise, reject it with 0x04,0x02.
+	 */
+	if ((lun->flags & CTL_LUN_STOPPED)
+	 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) {
+		/* "Logical unit not ready, initializing cmd. required" */
+		ctl_set_lun_stopped(ctsio);
+		retval = 1;
+		goto bailout;
+	}
+
+	if ((lun->flags & CTL_LUN_INOPERABLE)
+	 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) {
+		/* "Medium format corrupted" */
+		ctl_set_medium_format_corrupted(ctsio);
+		retval = 1;
+		goto bailout;
+	}
+
+bailout:
+	return (retval);
+
+}
+
+static void
+ctl_failover_io(union ctl_io *io, int have_lock)
+{
+	ctl_set_busy(&io->scsiio);
+	ctl_done_lock(io, have_lock);
+}
+
+static void
+ctl_failover(void)
+{
+	struct ctl_lun *lun;
+	struct ctl_softc *ctl_softc;
+	union ctl_io *next_io, *pending_io;
+	union ctl_io *io;
+	int lun_idx;
+	int i;
+
+	ctl_softc = control_softc;
+
+	mtx_lock(&ctl_softc->ctl_lock);
+	/*
+	 * Remove any cmds from the other SC from the rtr queue.  These
+	 * will obviously only be for LUNs for which we're the primary.
+	 * We can't send status or get/send data for these commands.
+	 * Since they haven't been executed yet, we can just remove them.
+	 * We'll either abort them or delete them below, depending on
+	 * which HA mode we're in.
+	 */
+	for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue);
+	     io != NULL; io = next_io) {
+		next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links);
+		if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
+			STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr,
+				      ctl_io_hdr, links);
+	}
+
+	for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) {
+		lun = ctl_softc->ctl_luns[lun_idx];
+		if (lun==NULL)
+			continue;
+
+		/*
+		 * Processor LUNs are primary on both sides.
+		 * XXX will this always be true?
+		 */
+		if (lun->be_lun->lun_type == T_PROCESSOR)
+			continue;
+
+		if ((lun->flags & CTL_LUN_PRIMARY_SC)
+		 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
+			printf("FAILOVER: primary lun %d\n", lun_idx);
+		        /*
+			 * Remove all commands from the other SC. First from the
+			 * blocked queue then from the ooa queue. Once we have
+			 * removed them. Call ctl_check_blocked to see if there
+			 * is anything that can run.
+			 */
+			for (io = (union ctl_io *)TAILQ_FIRST(
+			     &lun->blocked_queue); io != NULL; io = next_io) {
+
+		        	next_io = (union ctl_io *)TAILQ_NEXT(
+				    &io->io_hdr, blocked_links);
+
+				if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
+					TAILQ_REMOVE(&lun->blocked_queue,
+						     &io->io_hdr,blocked_links);
+					io->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
+					TAILQ_REMOVE(&lun->ooa_queue,
+						     &io->io_hdr, ooa_links);
+
+					ctl_free_io_internal(io, 1);
+				}
+			}
+
+			for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
+	     		     io != NULL; io = next_io) {
+
+		        	next_io = (union ctl_io *)TAILQ_NEXT(
+				    &io->io_hdr, ooa_links);
+
+				if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
+
+					TAILQ_REMOVE(&lun->ooa_queue,
+						&io->io_hdr,
+					     	ooa_links);
+
+					ctl_free_io_internal(io, 1);
+				}
+			}
+			ctl_check_blocked(lun);
+		} else if ((lun->flags & CTL_LUN_PRIMARY_SC)
+			&& (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
+
+			printf("FAILOVER: primary lun %d\n", lun_idx);
+			/*
+			 * Abort all commands from the other SC.  We can't
+			 * send status back for them now.  These should get
+			 * cleaned up when they are completed or come out
+			 * for a datamove operation.
+			 */
+			for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
+	     		     io != NULL; io = next_io) {
+		        	next_io = (union ctl_io *)TAILQ_NEXT(
+					&io->io_hdr, ooa_links);
+
+				if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
+					io->io_hdr.flags |= CTL_FLAG_ABORT;
+			}
+		} else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
+			&& (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
+
+			printf("FAILOVER: secondary lun %d\n", lun_idx);
+
+			lun->flags |= CTL_LUN_PRIMARY_SC;
+
+			/*
+			 * We send all I/O that was sent to this controller
+			 * and redirected to the other side back with
+			 * busy status, and have the initiator retry it.
+			 * Figuring out how much data has been transferred,
+			 * etc. and picking up where we left off would be 
+			 * very tricky.
+			 *
+			 * XXX KDM need to remove I/O from the blocked
+			 * queue as well!
+			 */
+			for (pending_io = (union ctl_io *)TAILQ_FIRST(
+			     &lun->ooa_queue); pending_io != NULL;
+			     pending_io = next_io) {
+
+				next_io =  (union ctl_io *)TAILQ_NEXT(
+					&pending_io->io_hdr, ooa_links);
+
+				pending_io->io_hdr.flags &=
+					~CTL_FLAG_SENT_2OTHER_SC;
+
+				if (pending_io->io_hdr.flags &
+				    CTL_FLAG_IO_ACTIVE) {
+					pending_io->io_hdr.flags |=
+						CTL_FLAG_FAILOVER;
+				} else {
+					ctl_set_busy(&pending_io->scsiio);
+					ctl_done_lock(pending_io,
+						      /*have_lock*/1);
+				}
+			}
+
+			/*
+			 * Build Unit Attention
+			 */
+			for (i = 0; i < CTL_MAX_INITIATORS; i++) {
+				lun->pending_sense[i].ua_pending |=
+				                     CTL_UA_ASYM_ACC_CHANGE;
+			}
+		} else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
+			&& (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
+			printf("FAILOVER: secondary lun %d\n", lun_idx);
+			/*
+			 * if the first io on the OOA is not on the RtR queue
+			 * add it.
+			 */
+			lun->flags |= CTL_LUN_PRIMARY_SC;
+
+			pending_io = (union ctl_io *)TAILQ_FIRST(
+			    &lun->ooa_queue);
+			if (pending_io==NULL) {
+				printf("Nothing on OOA queue\n");
+				continue;
+			}
+
+			pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
+			if ((pending_io->io_hdr.flags &
+			     CTL_FLAG_IS_WAS_ON_RTR) == 0) {
+				pending_io->io_hdr.flags |=
+				    CTL_FLAG_IS_WAS_ON_RTR;
+				STAILQ_INSERT_TAIL(&ctl_softc->rtr_queue,
+						   &pending_io->io_hdr, links);
+			}
+#if 0
+			else
+			{
+				printf("Tag 0x%04x is running\n",
+				      pending_io->scsiio.tag_num);
+			}
+#endif
+
+			next_io = (union ctl_io *)TAILQ_NEXT(
+			    &pending_io->io_hdr, ooa_links);
+			for (pending_io=next_io; pending_io != NULL;
+			     pending_io = next_io) {
+				pending_io->io_hdr.flags &=
+				    ~CTL_FLAG_SENT_2OTHER_SC;
+				next_io = (union ctl_io *)TAILQ_NEXT(
+					&pending_io->io_hdr, ooa_links);
+				if (pending_io->io_hdr.flags &
+				    CTL_FLAG_IS_WAS_ON_RTR) {
+#if 0
+				        printf("Tag 0x%04x is running\n",
+				      		pending_io->scsiio.tag_num);
+#endif
+					continue;
+				}
+
+				switch (ctl_check_ooa(lun, pending_io,
+			            (union ctl_io *)TAILQ_PREV(
+				    &pending_io->io_hdr, ctl_ooaq,
+				    ooa_links))) {
+
+				case CTL_ACTION_BLOCK:
+					TAILQ_INSERT_TAIL(&lun->blocked_queue,
+							  &pending_io->io_hdr,
+							  blocked_links);
+					pending_io->io_hdr.flags |=
+					    CTL_FLAG_BLOCKED;
+					break;
+				case CTL_ACTION_PASS:
+				case CTL_ACTION_SKIP:
+					pending_io->io_hdr.flags |=
+					    CTL_FLAG_IS_WAS_ON_RTR;
+					STAILQ_INSERT_TAIL(
+					    &ctl_softc->rtr_queue,
+					    &pending_io->io_hdr, links);
+					break;
+				case CTL_ACTION_OVERLAP:
+					ctl_set_overlapped_cmd(
+					    (struct ctl_scsiio *)pending_io);
+					ctl_done_lock(pending_io,
+						      /*have_lock*/ 1);
+					break;
+				case CTL_ACTION_OVERLAP_TAG:
+					ctl_set_overlapped_tag(
+					    (struct ctl_scsiio *)pending_io,
+					    pending_io->scsiio.tag_num & 0xff);
+					ctl_done_lock(pending_io,
+						      /*have_lock*/ 1);
+					break;
+				case CTL_ACTION_ERROR:
+				default:
+					ctl_set_internal_failure(
+						(struct ctl_scsiio *)pending_io,
+						0,  // sks_valid
+						0); //retry count
+					ctl_done_lock(pending_io,
+						      /*have_lock*/ 1);
+					break;
+				}
+			}
+
+			/*
+			 * Build Unit Attention
+			 */
+			for (i = 0; i < CTL_MAX_INITIATORS; i++) {
+				lun->pending_sense[i].ua_pending |=
+				                     CTL_UA_ASYM_ACC_CHANGE;
+			}
+		} else {
+			panic("Unhandled HA mode failover, LUN flags = %#x, "
+			      "ha_mode = #%x", lun->flags, ctl_softc->ha_mode);
+		}
+	}
+	ctl_pause_rtr = 0;
+	mtx_unlock(&ctl_softc->ctl_lock);
+}
+
+static int
+ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio)
+{
+	struct ctl_lun *lun;
+	struct ctl_cmd_entry *entry;
+	uint8_t opcode;
+	uint32_t initidx;
+	int retval;
+
+	retval = 0;
+
+	lun = NULL;
+
+	opcode = ctsio->cdb[0];
+
+	mtx_lock(&ctl_softc->ctl_lock);
+
+	if ((ctsio->io_hdr.nexus.targ_lun < CTL_MAX_LUNS)
+	 && (ctl_softc->ctl_luns[ctsio->io_hdr.nexus.targ_lun] != NULL)) {
+		lun = ctl_softc->ctl_luns[ctsio->io_hdr.nexus.targ_lun];
+		/*
+		 * If the LUN is invalid, pretend that it doesn't exist.
+		 * It will go away as soon as all pending I/O has been
+		 * completed.
+		 */
+		if (lun->flags & CTL_LUN_DISABLED) {
+			lun = NULL;
+		} else {
+			ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
+			ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr =
+				lun->be_lun;
+			if (lun->be_lun->lun_type == T_PROCESSOR) {
+				ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV;
+			}
+		}
+	} else {
+		ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
+		ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
+	}
+
+	entry = &ctl_cmd_table[opcode];
+
+	ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
+	ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
+
+	/*
+	 * Check to see whether we can send this command to LUNs that don't
+	 * exist.  This should pretty much only be the case for inquiry
+	 * and request sense.  Further checks, below, really require having
+	 * a LUN, so we can't really check the command anymore.  Just put
+	 * it on the rtr queue.
+	 */
+	if (lun == NULL) {
+		if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS)
+			goto queue_rtr;
+
+		ctl_set_unsupported_lun(ctsio);
+		mtx_unlock(&ctl_softc->ctl_lock);
+		ctl_done((union ctl_io *)ctsio);
+		goto bailout;
+	} else {
+		/*
+		 * Every I/O goes into the OOA queue for a particular LUN, and
+		 * stays there until completion.
+		 */
+		TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
+
+		/*
+		 * Make sure we support this particular command on this LUN.
+		 * e.g., we don't support writes to the control LUN.
+		 */
+		switch (lun->be_lun->lun_type) {
+		case T_PROCESSOR:
+		 	if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0)
+			 && ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS)
+			      == 0)) {
+				ctl_set_invalid_opcode(ctsio);
+				mtx_unlock(&ctl_softc->ctl_lock);
+				ctl_done((union ctl_io *)ctsio);
+				goto bailout;
+			}
+			break;
+		case T_DIRECT:
+			if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0)
+			 && ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS)
+			      == 0)){
+				ctl_set_invalid_opcode(ctsio);
+				mtx_unlock(&ctl_softc->ctl_lock);
+				ctl_done((union ctl_io *)ctsio);
+				goto bailout;
+			}
+			break;
+		default:
+			printf("Unsupported CTL LUN type %d\n",
+			       lun->be_lun->lun_type);
+			panic("Unsupported CTL LUN type %d\n",
+			      lun->be_lun->lun_type);
+			break; /* NOTREACHED */
+		}
+	}
+
+	initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
+
+	/*
+	 * If we've got a request sense, it'll clear the contingent
+	 * allegiance condition.  Otherwise, if we have a CA condition for
+	 * this initiator, clear it, because it sent down a command other
+	 * than request sense.
+	 */
+	if ((opcode != REQUEST_SENSE)
+	 && (ctl_is_set(lun->have_ca, initidx)))
+		ctl_clear_mask(lun->have_ca, initidx);
+
+	/*
+	 * If the command has this flag set, it handles its own unit
+	 * attention reporting, we shouldn't do anything.  Otherwise we
+	 * check for any pending unit attentions, and send them back to the
+	 * initiator.  We only do this when a command initially comes in,
+	 * not when we pull it off the blocked queue.
+	 *
+	 * According to SAM-3, section 5.3.2, the order that things get
+	 * presented back to the host is basically unit attentions caused
+	 * by some sort of reset event, busy status, reservation conflicts
+	 * or task set full, and finally any other status.
+	 *
+	 * One issue here is that some of the unit attentions we report
+	 * don't fall into the "reset" category (e.g. "reported luns data
+	 * has changed").  So reporting it here, before the reservation
+	 * check, may be technically wrong.  I guess the only thing to do
+	 * would be to check for and report the reset events here, and then
+	 * check for the other unit attention types after we check for a
+	 * reservation conflict.
+	 *
+	 * XXX KDM need to fix this
+	 */
+	if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
+		ctl_ua_type ua_type;
+
+		ua_type = lun->pending_sense[initidx].ua_pending;
+		if (ua_type != CTL_UA_NONE) {
+			ctl_sense_format sense_format;
+
+			if (lun != NULL)
+				sense_format = (lun->flags &
+				    CTL_LUN_SENSE_DESC) ? CTL_SENSE_DESCRIPTOR :
+				    CTL_SENSE_FIXED;
+			else
+				sense_format = CTL_SENSE_FIXED;
+
+			ua_type = ctl_build_ua(ua_type, &ctsio->sense_data,
+					       sense_format);
+			if (ua_type != CTL_UA_NONE) {
+				ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
+				ctsio->io_hdr.status = CTL_SCSI_ERROR |
+						       CTL_AUTOSENSE;
+				ctsio->sense_len = SSD_FULL_SIZE;
+				lun->pending_sense[initidx].ua_pending &=
+					~ua_type;
+				mtx_unlock(&ctl_softc->ctl_lock);
+				ctl_done((union ctl_io *)ctsio);
+				goto bailout;
+			}
+		}
+	}
+
+
+	if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) {
+		mtx_unlock(&ctl_softc->ctl_lock);
+		ctl_done((union ctl_io *)ctsio);
+		goto bailout;
+	}
+
+	/*
+	 * XXX CHD this is where we want to send IO to other side if
+	 * this LUN is secondary on this SC. We will need to make a copy
+	 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
+	 * the copy we send as FROM_OTHER.
+	 * We also need to stuff the address of the original IO so we can
+	 * find it easily. Something similar will need be done on the other
+	 * side so when we are done we can find the copy.
+	 */
+	if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
+		union ctl_ha_msg msg_info;
+		int isc_retval;
+
+		ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
+
+		msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
+		msg_info.hdr.original_sc = (union ctl_io *)ctsio;
+#if 0
+		printf("1. ctsio %p\n", ctsio);
+#endif
+		msg_info.hdr.serializing_sc = NULL;
+		msg_info.hdr.nexus = ctsio->io_hdr.nexus;
+		msg_info.scsi.tag_num = ctsio->tag_num;
+		msg_info.scsi.tag_type = ctsio->tag_type;
+		memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
+
+		ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
+
+		if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
+		    (void *)&msg_info, sizeof(msg_info), 0)) >
+		    CTL_HA_STATUS_SUCCESS) {
+			printf("CTL:precheck, ctl_ha_msg_send returned %d\n",
+			       isc_retval);
+			printf("CTL:opcode is %x\n",opcode);
+		} else {
+#if 0
+			printf("CTL:Precheck sent msg, opcode is %x\n",opcode);
+#endif
+		}
+
+		/*
+		 * XXX KDM this I/O is off the incoming queue, but hasn't
+		 * been inserted on any other queue.  We may need to come
+		 * up with a holding queue while we wait for serialization
+		 * so that we have an idea of what we're waiting for from
+		 * the other side.
+		 */
+		goto bailout_unlock;
+	}
+
+	switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
+			      (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
+			      ctl_ooaq, ooa_links))) {
+	case CTL_ACTION_BLOCK:
+		ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
+		TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
+				  blocked_links);
+		goto bailout_unlock;
+		break; /* NOTREACHED */
+	case CTL_ACTION_PASS:
+	case CTL_ACTION_SKIP:
+		goto queue_rtr;
+		break; /* NOTREACHED */
+	case CTL_ACTION_OVERLAP:
+		ctl_set_overlapped_cmd(ctsio);
+		mtx_unlock(&ctl_softc->ctl_lock);
+		ctl_done((union ctl_io *)ctsio);
+		goto bailout;
+		break; /* NOTREACHED */
+	case CTL_ACTION_OVERLAP_TAG:
+		ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
+		mtx_unlock(&ctl_softc->ctl_lock);
+		ctl_done((union ctl_io *)ctsio);
+		goto bailout;
+		break; /* NOTREACHED */
+	case CTL_ACTION_ERROR:
+	default:
+		ctl_set_internal_failure(ctsio,
+					 /*sks_valid*/ 0,
+					 /*retry_count*/ 0);
+		mtx_unlock(&ctl_softc->ctl_lock);
+		ctl_done((union ctl_io *)ctsio);
+		goto bailout;
+		break; /* NOTREACHED */
+	}
+
+	goto bailout_unlock;
+
+queue_rtr:
+	ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
+	STAILQ_INSERT_TAIL(&ctl_softc->rtr_queue, &ctsio->io_hdr, links);
+
+bailout_unlock:
+	mtx_unlock(&ctl_softc->ctl_lock);
+
+bailout:
+	return (retval);
+}
+
+static int
+ctl_scsiio(struct ctl_scsiio *ctsio)
+{
+	int retval;
+	struct ctl_cmd_entry *entry;
+
+	retval = CTL_RETVAL_COMPLETE;
+
+	CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
+
+	entry = &ctl_cmd_table[ctsio->cdb[0]];
+
+	/*
+	 * If this I/O has been aborted, just send it straight to
+	 * ctl_done() without executing it.
+	 */
+	if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
+		ctl_done((union ctl_io *)ctsio);
+		goto bailout;
+	}
+
+	/*
+	 * All the checks should have been handled by ctl_scsiio_precheck().
+	 * We should be clear now to just execute the I/O.
+	 */
+	retval = entry->execute(ctsio);
+
+bailout:
+	return (retval);
+}
+
+/*
+ * Since we only implement one target right now, a bus reset simply resets
+ * our single target.
+ */
+static int
+ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io)
+{
+	return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET));
+}
+
+static int
+ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
+		 ctl_ua_type ua_type)
+{
+	struct ctl_lun *lun;
+	int retval;
+
+	if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
+		union ctl_ha_msg msg_info;
+
+		io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
+		msg_info.hdr.nexus = io->io_hdr.nexus;
+		if (ua_type==CTL_UA_TARG_RESET)
+			msg_info.task.task_action = CTL_TASK_TARGET_RESET;
+		else
+			msg_info.task.task_action = CTL_TASK_BUS_RESET;
+		msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
+		msg_info.hdr.original_sc = NULL;
+		msg_info.hdr.serializing_sc = NULL;
+		if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL,
+		    (void *)&msg_info, sizeof(msg_info), 0)) {
+		}
+	}
+	retval = 0;
+
+	STAILQ_FOREACH(lun, &ctl_softc->lun_list, links)
+		retval += ctl_lun_reset(lun, io, ua_type);
+
+	return (retval);
+}
+
+/*
+ * The LUN should always be set.  The I/O is optional, and is used to
+ * distinguish between I/Os sent by this initiator, and by other
+ * initiators.  We set unit attention for initiators other than this one.
+ * SAM-3 is vague on this point.  It does say that a unit attention should
+ * be established for other initiators when a LUN is reset (see section
+ * 5.7.3), but it doesn't specifically say that the unit attention should
+ * be established for this particular initiator when a LUN is reset.  Here
+ * is the relevant text, from SAM-3 rev 8:
+ *
+ * 5.7.2 When a SCSI initiator port aborts its own tasks
+ *
+ * When a SCSI initiator port causes its own task(s) to be aborted, no
+ * notification that the task(s) have been aborted shall be returned to
+ * the SCSI initiator port other than the completion response for the
+ * command or task management function action that caused the task(s) to
+ * be aborted and notification(s) associated with related effects of the
+ * action (e.g., a reset unit attention condition).
+ *
+ * XXX KDM for now, we're setting unit attention for all initiators.
+ */
+static int
+ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
+{
+	union ctl_io *xio;
+#if 0
+	uint32_t initindex;
+#endif
+	int i;
+
+	/*
+	 * Run through the OOA queue and abort each I/O.
+	 */
+#if 0
+	TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
+#endif
+	for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
+	     xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
+		xio->io_hdr.flags |= CTL_FLAG_ABORT;
+	}
+
+	/*
+	 * This version sets unit attention for every
+	 */
+#if 0
+	initindex = ctl_get_initindex(&io->io_hdr.nexus);
+	for (i = 0; i < CTL_MAX_INITIATORS; i++) {
+		if (initindex == i)
+			continue;
+		lun->pending_sense[i].ua_pending |= ua_type;
+	}
+#endif
+
+	/*
+	 * A reset (any kind, really) clears reservations established with
+	 * RESERVE/RELEASE.  It does not clear reservations established
+	 * with PERSISTENT RESERVE OUT, but we don't support that at the
+	 * moment anyway.  See SPC-2, section 5.6.  SPC-3 doesn't address
+	 * reservations made with the RESERVE/RELEASE commands, because
+	 * those commands are obsolete in SPC-3.
+	 */
+	lun->flags &= ~CTL_LUN_RESERVED;
+
+	for (i = 0; i < CTL_MAX_INITIATORS; i++) {
+		ctl_clear_mask(lun->have_ca, i);
+		lun->pending_sense[i].ua_pending |= ua_type;
+	}
+
+	return (0);
+}
+
+static int
+ctl_abort_task(union ctl_io *io)
+{
+	union ctl_io *xio;
+	struct ctl_lun *lun;
+	struct ctl_softc *ctl_softc;
+#if 0
+	struct sbuf sb;
+	char printbuf[128];
+#endif
+	int found;
+
+	ctl_softc = control_softc;
+	found = 0;
+
+	/*
+	 * Look up the LUN.
+	 */
+	if ((io->io_hdr.nexus.targ_lun < CTL_MAX_LUNS)
+	 && (ctl_softc->ctl_luns[io->io_hdr.nexus.targ_lun] != NULL))
+		lun = ctl_softc->ctl_luns[io->io_hdr.nexus.targ_lun];
+	else
+		goto bailout;
+
+#if 0
+	printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
+	       lun->lun, io->taskio.tag_num, io->taskio.tag_type);
+#endif
+
+	/*
+	 * Run through the OOA queue and attempt to find the given I/O.
+	 * The target port, initiator ID, tag type and tag number have to
+	 * match the values that we got from the initiator.  If we have an
+	 * untagged command to abort, simply abort the first untagged command
+	 * we come to.  We only allow one untagged command at a time of course.
+	 */
+#if 0
+	TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
+#endif
+	for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
+	     xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
+#if 0
+		sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
+
+		sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
+			    lun->lun, xio->scsiio.tag_num,
+			    xio->scsiio.tag_type,
+			    (xio->io_hdr.blocked_links.tqe_prev
+			    == NULL) ? "" : " BLOCKED",
+			    (xio->io_hdr.flags &
+			    CTL_FLAG_DMA_INPROG) ? " DMA" : "",
+			    (xio->io_hdr.flags &
+			    CTL_FLAG_ABORT) ? " ABORT" : ""),
+			    (xio->io_hdr.flags &
+			    CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "");
+		ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
+		sbuf_finish(&sb);
+		printf("%s\n", sbuf_data(&sb));
+#endif
+
+		if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port)
+		 && (xio->io_hdr.nexus.initid.id ==
+		     io->io_hdr.nexus.initid.id)) {
+			/*
+			 * If the abort says that the task is untagged, the
+			 * task in the queue must be untagged.  Otherwise,
+			 * we just check to see whether the tag numbers
+			 * match.  This is because the QLogic firmware
+			 * doesn't pass back the tag type in an abort
+			 * request.
+			 */
+#if 0
+			if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
+			  && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
+			 || (xio->scsiio.tag_num == io->taskio.tag_num)) {
+#endif
+			/*
+			 * XXX KDM we've got problems with FC, because it
+			 * doesn't send down a tag type with aborts.  So we
+			 * can only really go by the tag number...
+			 * This may cause problems with parallel SCSI.
+			 * Need to figure that out!!
+			 */
+			if (xio->scsiio.tag_num == io->taskio.tag_num) {
+				xio->io_hdr.flags |= CTL_FLAG_ABORT;
+				found = 1;
+				if ((io->io_hdr.flags &
+				     CTL_FLAG_FROM_OTHER_SC) == 0 &&
+				    !(lun->flags & CTL_LUN_PRIMARY_SC)) {
+					union ctl_ha_msg msg_info;
+
+					io->io_hdr.flags |=
+					                CTL_FLAG_SENT_2OTHER_SC;
+					msg_info.hdr.nexus = io->io_hdr.nexus;
+					msg_info.task.task_action =
+						CTL_TASK_ABORT_TASK;
+					msg_info.task.tag_num =
+						io->taskio.tag_num;
+					msg_info.task.tag_type =
+						io->taskio.tag_type;
+					msg_info.hdr.msg_type =
+						CTL_MSG_MANAGE_TASKS;
+					msg_info.hdr.original_sc = NULL;
+					msg_info.hdr.serializing_sc = NULL;
+#if 0
+					printf("Sent Abort to other side\n");
+#endif
+					if (CTL_HA_STATUS_SUCCESS !=
+					        ctl_ha_msg_send(CTL_HA_CHAN_CTL,
+		    				(void *)&msg_info,
+						sizeof(msg_info), 0)) {
+					}
+				}
+#if 0
+				printf("ctl_abort_task: found I/O to abort\n");
+#endif
+				break;
+			}
+		}
+	}
+
+bailout:
+
+	if (found == 0) {
+		/*
+		 * This isn't really an error.  It's entirely possible for
+		 * the abort and command completion to cross on the wire.
+		 * This is more of an informative/diagnostic error.
+		 */
+#if 0
+		printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
+		       "%d:%d:%d:%d tag %d type %d\n",
+		       io->io_hdr.nexus.initid.id,
+		       io->io_hdr.nexus.targ_port,
+		       io->io_hdr.nexus.targ_target.id,
+		       io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
+		       io->taskio.tag_type);
+#endif
+		return (1);
+	} else
+		return (0);
+}
+
+/*
+ * Assumptions:  caller holds ctl_softc->ctl_lock
+ *
+ * This routine cannot block!  It must be callable from an interrupt
+ * handler as well as from the work thread.
+ */
+static void
+ctl_run_task_queue(struct ctl_softc *ctl_softc)
+{
+	union ctl_io *io, *next_io;
+
+	CTL_DEBUG_PRINT(("ctl_run_task_queue\n"));
+
+	for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->task_queue);
+	     io != NULL; io = next_io) {
+		int retval;
+		const char *task_desc;
+
+		next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links);
+
+		retval = 0;
+
+		switch (io->io_hdr.io_type) {
+		case CTL_IO_TASK: {
+			task_desc = ctl_scsi_task_string(&io->taskio);
+			if (task_desc != NULL) {
+#ifdef NEEDTOPORT
+				csevent_log(CSC_CTL | CSC_SHELF_SW |
+					    CTL_TASK_REPORT,
+					    csevent_LogType_Trace,
+					    csevent_Severity_Information,
+					    csevent_AlertLevel_Green,
+					    csevent_FRU_Firmware,
+					    csevent_FRU_Unknown,
+					    "CTL: received task: %s",task_desc);
+#endif
+			} else {
+#ifdef NEEDTOPORT
+				csevent_log(CSC_CTL | CSC_SHELF_SW |
+					    CTL_TASK_REPORT,
+					    csevent_LogType_Trace,
+					    csevent_Severity_Information,
+					    csevent_AlertLevel_Green,
+					    csevent_FRU_Firmware,
+					    csevent_FRU_Unknown,
+					    "CTL: received unknown task "
+					    "type: %d (%#x)",
+					    io->taskio.task_action,
+					    io->taskio.task_action);
+#endif
+			}
+			switch (io->taskio.task_action) {
+			case CTL_TASK_ABORT_TASK:
+				retval = ctl_abort_task(io);
+				break;
+			case CTL_TASK_ABORT_TASK_SET:
+				break;
+			case CTL_TASK_CLEAR_ACA:
+				break;
+			case CTL_TASK_CLEAR_TASK_SET:
+				break;
+			case CTL_TASK_LUN_RESET: {
+				struct ctl_lun *lun;
+				uint32_t targ_lun;
+				int retval;
+
+				targ_lun = io->io_hdr.nexus.targ_lun;
+
+				if ((targ_lun < CTL_MAX_LUNS)
+				 && (ctl_softc->ctl_luns[targ_lun] != NULL))
+					lun = ctl_softc->ctl_luns[targ_lun];
+				else {
+					retval = 1;
+					break;
+				}
+
+				if (!(io->io_hdr.flags &
+				    CTL_FLAG_FROM_OTHER_SC)) {
+					union ctl_ha_msg msg_info;
+
+					io->io_hdr.flags |=
+						CTL_FLAG_SENT_2OTHER_SC;
+					msg_info.hdr.msg_type =
+						CTL_MSG_MANAGE_TASKS;
+					msg_info.hdr.nexus = io->io_hdr.nexus;
+					msg_info.task.task_action =
+						CTL_TASK_LUN_RESET;
+					msg_info.hdr.original_sc = NULL;
+					msg_info.hdr.serializing_sc = NULL;
+					if (CTL_HA_STATUS_SUCCESS !=
+					    ctl_ha_msg_send(CTL_HA_CHAN_CTL,
+					    (void *)&msg_info,
+					    sizeof(msg_info), 0)) {
+					}
+				}
+
+				retval = ctl_lun_reset(lun, io,
+						       CTL_UA_LUN_RESET);
+				break;
+			}
+			case CTL_TASK_TARGET_RESET:
+				retval = ctl_target_reset(ctl_softc, io,
+							  CTL_UA_TARG_RESET);
+				break;
+			case CTL_TASK_BUS_RESET:
+				retval = ctl_bus_reset(ctl_softc, io);
+				break;
+			case CTL_TASK_PORT_LOGIN:
+				break;
+			case CTL_TASK_PORT_LOGOUT:
+				break;
+			default:
+				printf("ctl_run_task_queue: got unknown task "
+				       "management event %d\n",
+				       io->taskio.task_action);
+				break;
+			}
+			if (retval == 0)
+				io->io_hdr.status = CTL_SUCCESS;
+			else
+				io->io_hdr.status = CTL_ERROR;
+
+			STAILQ_REMOVE(&ctl_softc->task_queue, &io->io_hdr,
+				      ctl_io_hdr, links);
+			/*
+			 * This will queue this I/O to the done queue, but the
+			 * work thread won't be able to process it until we
+			 * return and the lock is released.
+			 */
+			ctl_done_lock(io, /*have_lock*/ 1);
+			break;
+		}
+		default: {
+
+			printf("%s: invalid I/O type %d msg %d cdb %x"
+			       " iptl: %ju:%d:%ju:%d tag 0x%04x\n",
+			       __func__, io->io_hdr.io_type,
+			       io->io_hdr.msg_type, io->scsiio.cdb[0],
+			       (uintmax_t)io->io_hdr.nexus.initid.id,
+			       io->io_hdr.nexus.targ_port,
+			       (uintmax_t)io->io_hdr.nexus.targ_target.id,
+			       io->io_hdr.nexus.targ_lun,
+			       (io->io_hdr.io_type == CTL_IO_TASK) ?
+			       io->taskio.tag_num : io->scsiio.tag_num);
+			STAILQ_REMOVE(&ctl_softc->task_queue, &io->io_hdr,
+				      ctl_io_hdr, links);
+			ctl_free_io_internal(io, 1);
+			break;
+		}
+		}
+	}
+
+	ctl_softc->flags &= ~CTL_FLAG_TASK_PENDING;
+}
+
+/*
+ * For HA operation.  Handle commands that come in from the other
+ * controller.
+ */
+static void
+ctl_handle_isc(union ctl_io *io)
+{
+	int free_io;
+	struct ctl_lun *lun;
+	struct ctl_softc *ctl_softc;
+
+	ctl_softc = control_softc;
+
+	lun = ctl_softc->ctl_luns[io->io_hdr.nexus.targ_lun];
+
+	switch (io->io_hdr.msg_type) {
+	case CTL_MSG_SERIALIZE:
+		free_io = ctl_serialize_other_sc_cmd(&io->scsiio,
+						     /*have_lock*/ 0);
+		break;
+	case CTL_MSG_R2R: {
+		uint8_t opcode;
+		struct ctl_cmd_entry *entry;
+
+		/*
+		 * This is only used in SER_ONLY mode.
+		 */
+		free_io = 0;
+		opcode = io->scsiio.cdb[0];
+		entry = &ctl_cmd_table[opcode];
+		mtx_lock(&ctl_softc->ctl_lock);
+		if (ctl_scsiio_lun_check(ctl_softc, lun,
+		    entry, (struct ctl_scsiio *)io) != 0) {
+			ctl_done_lock(io, /*have_lock*/ 1);
+			mtx_unlock(&ctl_softc->ctl_lock);
+			break;
+		}
+		io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
+		STAILQ_INSERT_TAIL(&ctl_softc->rtr_queue,
+				   &io->io_hdr, links);
+		mtx_unlock(&ctl_softc->ctl_lock);
+		break;
+	}
+	case CTL_MSG_FINISH_IO:
+		if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
+			free_io = 0;
+			ctl_done_lock(io, /*have_lock*/ 0);
+		} else {
+			free_io = 1;
+			mtx_lock(&ctl_softc->ctl_lock);
+			TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr,
+				     ooa_links);
+			STAILQ_REMOVE(&ctl_softc->task_queue,
+				      &io->io_hdr, ctl_io_hdr, links);
+			ctl_check_blocked(lun);
+			mtx_unlock(&ctl_softc->ctl_lock);
+		}
+		break;
+	case CTL_MSG_PERS_ACTION:
+		ctl_hndl_per_res_out_on_other_sc(
+			(union ctl_ha_msg *)&io->presio.pr_msg);
+		free_io = 1;
+		break;
+	case CTL_MSG_BAD_JUJU:
+		free_io = 0;
+		ctl_done_lock(io, /*have_lock*/ 0);
+		break;
+	case CTL_MSG_DATAMOVE:
+		/* Only used in XFER mode */
+		free_io = 0;
+		ctl_datamove_remote(io);
+		break;
+	case CTL_MSG_DATAMOVE_DONE:
+		/* Only used in XFER mode */
+		free_io = 0;
+		io->scsiio.be_move_done(io);
+		break;
+	default:
+		free_io = 1;
+		printf("%s: Invalid message type %d\n",
+		       __func__, io->io_hdr.msg_type);
+		break;
+	}
+	if (free_io)
+		ctl_free_io_internal(io, 0);
+
+}
+
+
+/*
+ * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
+ * there is no match.
+ */
+static ctl_lun_error_pattern
+ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
+{
+	struct ctl_cmd_entry *entry;
+	ctl_lun_error_pattern filtered_pattern, pattern;
+	uint8_t opcode;
+
+	pattern = desc->error_pattern;
+
+	/*
+	 * XXX KDM we need more data passed into this function to match a
+	 * custom pattern, and we actually need to implement custom pattern
+	 * matching.
+	 */
+	if (pattern & CTL_LUN_PAT_CMD)
+		return (CTL_LUN_PAT_CMD);
+
+	if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
+		return (CTL_LUN_PAT_ANY);
+
+	opcode = ctsio->cdb[0];
+	entry = &ctl_cmd_table[opcode];
+
+	filtered_pattern = entry->pattern & pattern;
+
+	/*
+	 * If the user requested specific flags in the pattern (e.g.
+	 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
+	 * flags.
+	 *
+	 * If the user did not specify any flags, it doesn't matter whether
+	 * or not the command supports the flags.
+	 */
+	if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
+	     (pattern & ~CTL_LUN_PAT_MASK))
+		return (CTL_LUN_PAT_NONE);
+
+	/*
+	 * If the user asked for a range check, see if the requested LBA
+	 * range overlaps with this command's LBA range.
+	 */
+	if (filtered_pattern & CTL_LUN_PAT_RANGE) {
+		uint64_t lba1;
+		uint32_t len1;
+		ctl_action action;
+		int retval;
+
+		retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
+		if (retval != 0)
+			return (CTL_LUN_PAT_NONE);
+
+		action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
+					      desc->lba_range.len);
+		/*
+		 * A "pass" means that the LBA ranges don't overlap, so
+		 * this doesn't match the user's range criteria.
+		 */
+		if (action == CTL_ACTION_PASS)
+			return (CTL_LUN_PAT_NONE);
+	}
+
+	return (filtered_pattern);
+}
+
+/*
+ * Called with the CTL lock held.
+ */
+static void
+ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
+{
+	struct ctl_error_desc *desc, *desc2;
+
+	STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
+		ctl_lun_error_pattern pattern;
+		/*
+		 * Check to see whether this particular command matches
+		 * the pattern in the descriptor.
+		 */
+		pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
+		if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
+			continue;
+
+		switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
+		case CTL_LUN_INJ_ABORTED:
+			ctl_set_aborted(&io->scsiio);
+			break;
+		case CTL_LUN_INJ_MEDIUM_ERR:
+			ctl_set_medium_error(&io->scsiio);
+			break;
+		case CTL_LUN_INJ_UA:
+			/* 29h/00h  POWER ON, RESET, OR BUS DEVICE RESET
+			 * OCCURRED */
+			ctl_set_ua(&io->scsiio, 0x29, 0x00);
+			break;
+		case CTL_LUN_INJ_CUSTOM:
+			/*
+			 * We're assuming the user knows what he is doing.
+			 * Just copy the sense information without doing
+			 * checks.
+			 */
+			bcopy(&desc->custom_sense, &io->scsiio.sense_data,
+			      ctl_min(sizeof(desc->custom_sense),
+				      sizeof(io->scsiio.sense_data)));
+			io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
+			io->scsiio.sense_len = SSD_FULL_SIZE;
+			io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
+			break;
+		case CTL_LUN_INJ_NONE:
+		default:
+			/*
+			 * If this is an error injection type we don't know
+			 * about, clear the continuous flag (if it is set)
+			 * so it will get deleted below.
+			 */
+			desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
+			break;
+		}
+		/*
+		 * By default, each error injection action is a one-shot
+		 */
+		if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
+			continue;
+
+		STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
+
+		free(desc, M_CTL);
+	}
+}
+
+#ifdef CTL_IO_DELAY
+static void
+ctl_datamove_timer_wakeup(void *arg)
+{
+	union ctl_io *io;
+
+	io = (union ctl_io *)arg;
+
+	ctl_datamove(io);
+}
+#endif /* CTL_IO_DELAY */
+
+/*
+ * Assumption:  caller does NOT hold ctl_lock
+ */
+void
+ctl_datamove(union ctl_io *io)
+{
+	void (*fe_datamove)(union ctl_io *io);
+
+	CTL_DEBUG_PRINT(("ctl_datamove\n"));
+
+#ifdef CTL_TIME_IO
+	if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
+		char str[256];
+		char path_str[64];
+		struct sbuf sb;
+
+		ctl_scsi_path_string(io, path_str, sizeof(path_str));
+		sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
+
+		sbuf_cat(&sb, path_str);
+		switch (io->io_hdr.io_type) {
+		case CTL_IO_SCSI:
+			ctl_scsi_command_string(&io->scsiio, NULL, &sb);
+			sbuf_printf(&sb, "\n");
+			sbuf_cat(&sb, path_str);
+			sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
+				    io->scsiio.tag_num, io->scsiio.tag_type);
+			break;
+		case CTL_IO_TASK:
+			sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
+				    "Tag Type: %d\n", io->taskio.task_action,
+				    io->taskio.tag_num, io->taskio.tag_type);
+			break;
+		default:
+			printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
+			panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
+			break;
+		}
+		sbuf_cat(&sb, path_str);
+		sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
+			    (intmax_t)time_uptime - io->io_hdr.start_time);
+		sbuf_finish(&sb);
+		printf("%s", sbuf_data(&sb));
+	}
+#endif /* CTL_TIME_IO */
+
+	mtx_lock(&control_softc->ctl_lock);
+#ifdef CTL_IO_DELAY
+	if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
+		struct ctl_lun *lun;
+
+		lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
+
+		io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
+	} else {
+		struct ctl_lun *lun;
+
+		lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
+		if ((lun != NULL)
+		 && (lun->delay_info.datamove_delay > 0)) {
+			struct callout *callout;
+
+			callout = (struct callout *)&io->io_hdr.timer_bytes;
+			callout_init(callout, /*mpsafe*/ 1);
+			io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
+			callout_reset(callout,
+				      lun->delay_info.datamove_delay * hz,
+				      ctl_datamove_timer_wakeup, io);
+			if (lun->delay_info.datamove_type ==
+			    CTL_DELAY_TYPE_ONESHOT)
+				lun->delay_info.datamove_delay = 0;
+			mtx_unlock(&control_softc->ctl_lock);
+			return;
+		}
+	}
+#endif
+	/*
+	 * If we have any pending task management commands, process them
+	 * first.  This is necessary to eliminate a race condition with the
+	 * FETD:
+	 *
+	 * - FETD submits a task management command, like an abort.
+	 * - Back end calls fe_datamove() to move the data for the aborted
+	 *   command.  The FETD can't really accept it, but if it did, it
+	 *   would end up transmitting data for a command that the initiator
+	 *   told us to abort.
+	 *
+	 * We close the race by processing all pending task management
+	 * commands here (we can't block!), and then check this I/O to see
+	 * if it has been aborted.  If so, return it to the back end with
+	 * bad status, so the back end can say return an error to the back end
+	 * and then when the back end returns an error, we can return the
+	 * aborted command to the FETD, so it can clean up its resources.
+	 */
+	if (control_softc->flags & CTL_FLAG_TASK_PENDING)
+		ctl_run_task_queue(control_softc);
+
+	/*
+	 * This command has been aborted.  Set the port status, so we fail
+	 * the data move.
+	 */
+	if (io->io_hdr.flags & CTL_FLAG_ABORT) {
+		printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n",
+		       io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id,
+		       io->io_hdr.nexus.targ_port,
+		       (uintmax_t)io->io_hdr.nexus.targ_target.id,
+		       io->io_hdr.nexus.targ_lun);
+		io->io_hdr.status = CTL_CMD_ABORTED;
+		io->io_hdr.port_status = 31337;
+		mtx_unlock(&control_softc->ctl_lock);
+		/*
+		 * Note that the backend, in this case, will get the
+		 * callback in its context.  In other cases it may get
+		 * called in the frontend's interrupt thread context.
+		 */
+		io->scsiio.be_move_done(io);
+		return;
+	}
+
+	/*
+	 * If we're in XFER mode and this I/O is from the other shelf
+	 * controller, we need to send the DMA to the other side to
+	 * actually transfer the data to/from the host.  In serialize only
+	 * mode the transfer happens below CTL and ctl_datamove() is only
+	 * called on the machine that originally received the I/O.
+	 */
+	if ((control_softc->ha_mode == CTL_HA_MODE_XFER)
+	 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
+		union ctl_ha_msg msg;
+		uint32_t sg_entries_sent;
+		int do_sg_copy;
+		int i;
+
+		memset(&msg, 0, sizeof(msg));
+		msg.hdr.msg_type = CTL_MSG_DATAMOVE;
+		msg.hdr.original_sc = io->io_hdr.original_sc;
+		msg.hdr.serializing_sc = io;
+		msg.hdr.nexus = io->io_hdr.nexus;
+		msg.dt.flags = io->io_hdr.flags;
+		/*
+		 * We convert everything into a S/G list here.  We can't
+		 * pass by reference, only by value between controllers.
+		 * So we can't pass a pointer to the S/G list, only as many
+		 * S/G entries as we can fit in here.  If it's possible for
+		 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
+		 * then we need to break this up into multiple transfers.
+		 */
+		if (io->scsiio.kern_sg_entries == 0) {
+			msg.dt.kern_sg_entries = 1;
+			/*
+			 * If this is in cached memory, flush the cache
+			 * before we send the DMA request to the other
+			 * controller.  We want to do this in either the
+			 * read or the write case.  The read case is
+			 * straightforward.  In the write case, we want to
+			 * make sure nothing is in the local cache that
+			 * could overwrite the DMAed data.
+			 */
+			if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
+				/*
+				 * XXX KDM use bus_dmamap_sync() here.
+				 */
+			}
+
+			/*
+			 * Convert to a physical address if this is a
+			 * virtual address.
+			 */
+			if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
+				msg.dt.sg_list[0].addr =
+					io->scsiio.kern_data_ptr;
+			} else {
+				/*
+				 * XXX KDM use busdma here!
+				 */
+#if 0
+				msg.dt.sg_list[0].addr = (void *)
+					vtophys(io->scsiio.kern_data_ptr);
+#endif
+			}
+
+			msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
+			do_sg_copy = 0;
+		} else {
+			struct ctl_sg_entry *sgl;
+
+			do_sg_copy = 1;
+			msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
+			sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
+			if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
+				/*
+				 * XXX KDM use bus_dmamap_sync() here.
+				 */
+			}
+		}
+
+		msg.dt.kern_data_len = io->scsiio.kern_data_len;
+		msg.dt.kern_total_len = io->scsiio.kern_total_len;
+		msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
+		msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
+		msg.dt.sg_sequence = 0;
+
+		/*
+		 * Loop until we've sent all of the S/G entries.  On the
+		 * other end, we'll recompose these S/G entries into one
+		 * contiguous list before passing it to the
+		 */
+		for (sg_entries_sent = 0; sg_entries_sent <
+		     msg.dt.kern_sg_entries; msg.dt.sg_sequence++) {
+			msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/
+				sizeof(msg.dt.sg_list[0])),
+				msg.dt.kern_sg_entries - sg_entries_sent);
+
+			if (do_sg_copy != 0) {
+				struct ctl_sg_entry *sgl;
+				int j;
+
+				sgl = (struct ctl_sg_entry *)
+					io->scsiio.kern_data_ptr;
+				/*
+				 * If this is in cached memory, flush the cache
+				 * before we send the DMA request to the other
+				 * controller.  We want to do this in either
+				 * the * read or the write case.  The read
+				 * case is straightforward.  In the write
+				 * case, we want to make sure nothing is
+				 * in the local cache that could overwrite
+				 * the DMAed data.
+				 */
+
+				for (i = sg_entries_sent, j = 0;
+				     i < msg.dt.cur_sg_entries; i++, j++) {
+					if ((io->io_hdr.flags &
+					     CTL_FLAG_NO_DATASYNC) == 0) {
+						/*
+						 * XXX KDM use bus_dmamap_sync()
+						 */
+					}
+					if ((io->io_hdr.flags &
+					     CTL_FLAG_BUS_ADDR) == 0) {
+						/*
+						 * XXX KDM use busdma.
+						 */
+#if 0
+						msg.dt.sg_list[j].addr =(void *)
+						       vtophys(sgl[i].addr);
+#endif
+					} else {
+						msg.dt.sg_list[j].addr =
+							sgl[i].addr;
+					}
+					msg.dt.sg_list[j].len = sgl[i].len;
+				}
+			}
+
+			sg_entries_sent += msg.dt.cur_sg_entries;
+			if (sg_entries_sent >= msg.dt.kern_sg_entries)
+				msg.dt.sg_last = 1;
+			else
+				msg.dt.sg_last = 0;
+
+			/*
+			 * XXX KDM drop and reacquire the lock here?
+			 */
+			if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
+			    sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
+				/*
+				 * XXX do something here.
+				 */
+			}
+
+			msg.dt.sent_sg_entries = sg_entries_sent;
+		}
+		io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
+		if (io->io_hdr.flags & CTL_FLAG_FAILOVER)
+			ctl_failover_io(io, /*have_lock*/ 1);
+
+	} else {
+
+		/*
+		 * Lookup the fe_datamove() function for this particular
+		 * front end.
+		 */
+		fe_datamove =
+		    control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
+		mtx_unlock(&control_softc->ctl_lock);
+
+		fe_datamove(io);
+	}
+}
+
+static void
+ctl_send_datamove_done(union ctl_io *io, int have_lock)
+{
+	union ctl_ha_msg msg;
+	int isc_status;
+
+	memset(&msg, 0, sizeof(msg));
+
+	msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
+	msg.hdr.original_sc = io;
+	msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
+	msg.hdr.nexus = io->io_hdr.nexus;
+	msg.hdr.status = io->io_hdr.status;
+	msg.scsi.tag_num = io->scsiio.tag_num;
+	msg.scsi.tag_type = io->scsiio.tag_type;
+	msg.scsi.scsi_status = io->scsiio.scsi_status;
+	memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
+	       sizeof(io->scsiio.sense_data));
+	msg.scsi.sense_len = io->scsiio.sense_len;
+	msg.scsi.sense_residual = io->scsiio.sense_residual;
+	msg.scsi.fetd_status = io->io_hdr.port_status;
+	msg.scsi.residual = io->scsiio.residual;
+	io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
+
+	if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
+		ctl_failover_io(io, /*have_lock*/ have_lock);
+		return;
+	}
+
+	isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0);
+	if (isc_status > CTL_HA_STATUS_SUCCESS) {
+		/* XXX do something if this fails */
+	}
+
+}
+
+/*
+ * The DMA to the remote side is done, now we need to tell the other side
+ * we're done so it can continue with its data movement.
+ */
+static void
+ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
+{
+	union ctl_io *io;
+
+	io = rq->context;
+
+	if (rq->ret != CTL_HA_STATUS_SUCCESS) {
+		printf("%s: ISC DMA write failed with error %d", __func__,
+		       rq->ret);
+		ctl_set_internal_failure(&io->scsiio,
+					 /*sks_valid*/ 1,
+					 /*retry_count*/ rq->ret);
+	}
+
+	ctl_dt_req_free(rq);
+
+	/*
+	 * In this case, we had to malloc the memory locally.  Free it.
+	 */
+	if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
+		int i;
+		for (i = 0; i < io->scsiio.kern_sg_entries; i++)
+			free(io->io_hdr.local_sglist[i].addr, M_CTL);
+	}
+	/*
+	 * The data is in local and remote memory, so now we need to send
+	 * status (good or back) back to the other side.
+	 */
+	ctl_send_datamove_done(io, /*have_lock*/ 0);
+}
+
+/*
+ * We've moved the data from the host/controller into local memory.  Now we
+ * need to push it over to the remote controller's memory.
+ */
+static int
+ctl_datamove_remote_dm_write_cb(union ctl_io *io)
+{
+	int retval;
+
+	retval = 0;
+
+	retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
+					  ctl_datamove_remote_write_cb);
+
+	return (retval);
+}
+
+static void
+ctl_datamove_remote_write(union ctl_io *io)
+{
+	int retval;
+	void (*fe_datamove)(union ctl_io *io);
+
+	/*
+	 * - Get the data from the host/HBA into local memory.
+	 * - DMA memory from the local controller to the remote controller.
+	 * - Send status back to the remote controller.
+	 */
+
+	retval = ctl_datamove_remote_sgl_setup(io);
+	if (retval != 0)
+		return;
+
+	/* Switch the pointer over so the FETD knows what to do */
+	io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
+
+	/*
+	 * Use a custom move done callback, since we need to send completion
+	 * back to the other controller, not to the backend on this side.
+	 */
+	io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
+
+	fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
+
+	fe_datamove(io);
+
+	return;
+
+}
+
+static int
+ctl_datamove_remote_dm_read_cb(union ctl_io *io)
+{
+#if 0
+	char str[256];
+	char path_str[64];
+	struct sbuf sb;
+#endif
+
+	/*
+	 * In this case, we had to malloc the memory locally.  Free it.
+	 */
+	if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
+		int i;
+		for (i = 0; i < io->scsiio.kern_sg_entries; i++)
+			free(io->io_hdr.local_sglist[i].addr, M_CTL);
+	}
+
+#if 0
+	scsi_path_string(io, path_str, sizeof(path_str));
+	sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
+	sbuf_cat(&sb, path_str);
+	scsi_command_string(&io->scsiio, NULL, &sb);
+	sbuf_printf(&sb, "\n");
+	sbuf_cat(&sb, path_str);
+	sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
+		    io->scsiio.tag_num, io->scsiio.tag_type);
+	sbuf_cat(&sb, path_str);
+	sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
+		    io->io_hdr.flags, io->io_hdr.status);
+	sbuf_finish(&sb);
+	printk("%s", sbuf_data(&sb));
+#endif
+
+
+	/*
+	 * The read is done, now we need to send status (good or bad) back
+	 * to the other side.
+	 */
+	ctl_send_datamove_done(io, /*have_lock*/ 0);
+
+	return (0);
+}
+
+static void
+ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
+{
+	union ctl_io *io;
+	void (*fe_datamove)(union ctl_io *io);
+
+	io = rq->context;
+
+	if (rq->ret != CTL_HA_STATUS_SUCCESS) {
+		printf("%s: ISC DMA read failed with error %d", __func__,
+		       rq->ret);
+		ctl_set_internal_failure(&io->scsiio,
+					 /*sks_valid*/ 1,
+					 /*retry_count*/ rq->ret);
+	}
+
+	ctl_dt_req_free(rq);
+
+	/* Switch the pointer over so the FETD knows what to do */
+	io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
+
+	/*
+	 * Use a custom move done callback, since we need to send completion
+	 * back to the other controller, not to the backend on this side.
+	 */
+	io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
+
+	/* XXX KDM add checks like the ones in ctl_datamove? */
+
+	fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
+
+	fe_datamove(io);
+}
+
+static int
+ctl_datamove_remote_sgl_setup(union ctl_io *io)
+{
+	struct ctl_sg_entry *local_sglist, *remote_sglist;
+	struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist;
+	struct ctl_softc *softc;
+	int retval;
+	int i;
+
+	retval = 0;
+	softc = control_softc;
+
+	local_sglist = io->io_hdr.local_sglist;
+	local_dma_sglist = io->io_hdr.local_dma_sglist;
+	remote_sglist = io->io_hdr.remote_sglist;
+	remote_dma_sglist = io->io_hdr.remote_dma_sglist;
+
+	if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) {
+		for (i = 0; i < io->scsiio.kern_sg_entries; i++) {
+			local_sglist[i].len = remote_sglist[i].len;
+
+			/*
+			 * XXX Detect the situation where the RS-level I/O
+			 * redirector on the other side has already read the
+			 * data off of the AOR RS on this side, and
+			 * transferred it to remote (mirror) memory on the
+			 * other side.  Since we already have the data in
+			 * memory here, we just need to use it.
+			 *
+			 * XXX KDM this can probably be removed once we
+			 * get the cache device code in and take the
+			 * current AOR implementation out.
+			 */
+#ifdef NEEDTOPORT
+			if ((remote_sglist[i].addr >=
+			     (void *)vtophys(softc->mirr->addr))
+			 && (remote_sglist[i].addr <
+			     ((void *)vtophys(softc->mirr->addr) +
+			     CacheMirrorOffset))) {
+				local_sglist[i].addr = remote_sglist[i].addr -
+					CacheMirrorOffset;
+				if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
+				     CTL_FLAG_DATA_IN)
+					io->io_hdr.flags |= CTL_FLAG_REDIR_DONE;
+			} else {
+				local_sglist[i].addr = remote_sglist[i].addr +
+					CacheMirrorOffset;
+			}
+#endif
+#if 0
+			printf("%s: local %p, remote %p, len %d\n",
+			       __func__, local_sglist[i].addr,
+			       remote_sglist[i].addr, local_sglist[i].len);
+#endif
+		}
+	} else {
+		uint32_t len_to_go;
+
+		/*
+		 * In this case, we don't have automatically allocated
+		 * memory for this I/O on this controller.  This typically
+		 * happens with internal CTL I/O -- e.g. inquiry, mode
+		 * sense, etc.  Anything coming from RAIDCore will have
+		 * a mirror area available.
+		 */
+		len_to_go = io->scsiio.kern_data_len;
+
+		/*
+		 * Clear the no datasync flag, we have to use malloced
+		 * buffers.
+		 */
+		io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC;
+
+		/*
+		 * The difficult thing here is that the size of the various
+		 * S/G segments may be different than the size from the
+		 * remote controller.  That'll make it harder when DMAing
+		 * the data back to the other side.
+		 */
+		for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) /
+		     sizeof(io->io_hdr.remote_sglist[0])) &&
+		     (len_to_go > 0); i++) {
+			local_sglist[i].len = ctl_min(len_to_go, 131072);
+			CTL_SIZE_8B(local_dma_sglist[i].len,
+				    local_sglist[i].len);
+			local_sglist[i].addr =
+				malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK);
+
+			local_dma_sglist[i].addr = local_sglist[i].addr;
+
+			if (local_sglist[i].addr == NULL) {
+				int j;
+
+				printf("malloc failed for %zd bytes!",
+				       local_dma_sglist[i].len);
+				for (j = 0; j < i; j++) {
+					free(local_sglist[j].addr, M_CTL);
+				}
+				ctl_set_internal_failure(&io->scsiio,
+							 /*sks_valid*/ 1,
+							 /*retry_count*/ 4857);
+				retval = 1;
+				goto bailout_error;
+				
+			}
+			/* XXX KDM do we need a sync here? */
+
+			len_to_go -= local_sglist[i].len;
+		}
+		/*
+		 * Reset the number of S/G entries accordingly.  The
+		 * original number of S/G entries is available in
+		 * rem_sg_entries.
+		 */
+		io->scsiio.kern_sg_entries = i;
+
+#if 0
+		printf("%s: kern_sg_entries = %d\n", __func__,
+		       io->scsiio.kern_sg_entries);
+		for (i = 0; i < io->scsiio.kern_sg_entries; i++)
+			printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i,
+			       local_sglist[i].addr, local_sglist[i].len,
+			       local_dma_sglist[i].len);
+#endif
+	}
+
+
+	return (retval);
+
+bailout_error:
+
+	ctl_send_datamove_done(io, /*have_lock*/ 0);
+
+	return (retval);
+}
+
+static int
+ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
+			 ctl_ha_dt_cb callback)
+{
+	struct ctl_ha_dt_req *rq;
+	struct ctl_sg_entry *remote_sglist, *local_sglist;
+	struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist;
+	uint32_t local_used, remote_used, total_used;
+	int retval;
+	int i, j;
+
+	retval = 0;
+
+	rq = ctl_dt_req_alloc();
+
+	/*
+	 * If we failed to allocate the request, and if the DMA didn't fail
+	 * anyway, set busy status.  This is just a resource allocation
+	 * failure.
+	 */
+	if ((rq == NULL)
+	 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE))
+		ctl_set_busy(&io->scsiio);
+
+	if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) {
+
+		if (rq != NULL)
+			ctl_dt_req_free(rq);
+
+		/*
+		 * The data move failed.  We need to return status back
+		 * to the other controller.  No point in trying to DMA
+		 * data to the remote controller.
+		 */
+
+		ctl_send_datamove_done(io, /*have_lock*/ 0);
+
+		retval = 1;
+
+		goto bailout;
+	}
+
+	local_sglist = io->io_hdr.local_sglist;
+	local_dma_sglist = io->io_hdr.local_dma_sglist;
+	remote_sglist = io->io_hdr.remote_sglist;
+	remote_dma_sglist = io->io_hdr.remote_dma_sglist;
+	local_used = 0;
+	remote_used = 0;
+	total_used = 0;
+
+	if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) {
+		rq->ret = CTL_HA_STATUS_SUCCESS;
+		rq->context = io;
+		callback(rq);
+		goto bailout;
+	}
+
+	/*
+	 * Pull/push the data over the wire from/to the other controller.
+	 * This takes into account the possibility that the local and
+	 * remote sglists may not be identical in terms of the size of
+	 * the elements and the number of elements.
+	 *
+	 * One fundamental assumption here is that the length allocated for
+	 * both the local and remote sglists is identical.  Otherwise, we've
+	 * essentially got a coding error of some sort.
+	 */
+	for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
+		int isc_ret;
+		uint32_t cur_len, dma_length;
+		uint8_t *tmp_ptr;
+
+		rq->id = CTL_HA_DATA_CTL;
+		rq->command = command;
+		rq->context = io;
+
+		/*
+		 * Both pointers should be aligned.  But it is possible
+		 * that the allocation length is not.  They should both
+		 * also have enough slack left over at the end, though,
+		 * to round up to the next 8 byte boundary.
+		 */
+		cur_len = ctl_min(local_sglist[i].len - local_used,
+				  remote_sglist[j].len - remote_used);
+
+		/*
+		 * In this case, we have a size issue and need to decrease
+		 * the size, except in the case where we actually have less
+		 * than 8 bytes left.  In that case, we need to increase
+		 * the DMA length to get the last bit.
+		 */
+		if ((cur_len & 0x7) != 0) {
+			if (cur_len > 0x7) {
+				cur_len = cur_len - (cur_len & 0x7);
+				dma_length = cur_len;
+			} else {
+				CTL_SIZE_8B(dma_length, cur_len);
+			}
+
+		} else
+			dma_length = cur_len;
+
+		/*
+		 * If we had to allocate memory for this I/O, instead of using
+		 * the non-cached mirror memory, we'll need to flush the cache
+		 * before trying to DMA to the other controller.
+		 *
+		 * We could end up doing this multiple times for the same
+		 * segment if we have a larger local segment than remote
+		 * segment.  That shouldn't be an issue.
+		 */
+		if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
+			/*
+			 * XXX KDM use bus_dmamap_sync() here.
+			 */
+		}
+
+		rq->size = dma_length;
+
+		tmp_ptr = (uint8_t *)local_sglist[i].addr;
+		tmp_ptr += local_used;
+
+		/* Use physical addresses when talking to ISC hardware */
+		if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
+			/* XXX KDM use busdma */
+#if 0
+			rq->local = vtophys(tmp_ptr);
+#endif
+		} else
+			rq->local = tmp_ptr;
+
+		tmp_ptr = (uint8_t *)remote_sglist[j].addr;
+		tmp_ptr += remote_used;
+		rq->remote = tmp_ptr;
+
+		rq->callback = NULL;
+
+		local_used += cur_len;
+		if (local_used >= local_sglist[i].len) {
+			i++;
+			local_used = 0;
+		}
+
+		remote_used += cur_len;
+		if (remote_used >= remote_sglist[j].len) {
+			j++;
+			remote_used = 0;
+		}
+		total_used += cur_len;
+
+		if (total_used >= io->scsiio.kern_data_len)
+			rq->callback = callback;
+
+		if ((rq->size & 0x7) != 0) {
+			printf("%s: warning: size %d is not on 8b boundary\n",
+			       __func__, rq->size);
+		}
+		if (((uintptr_t)rq->local & 0x7) != 0) {
+			printf("%s: warning: local %p not on 8b boundary\n",
+			       __func__, rq->local);
+		}
+		if (((uintptr_t)rq->remote & 0x7) != 0) {
+			printf("%s: warning: remote %p not on 8b boundary\n",
+			       __func__, rq->local);
+		}
+#if 0
+		printf("%s: %s: local %#x remote %#x size %d\n", __func__,
+		       (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
+		       rq->local, rq->remote, rq->size);
+#endif
+
+		isc_ret = ctl_dt_single(rq);
+		if (isc_ret == CTL_HA_STATUS_WAIT)
+			continue;
+
+		if (isc_ret == CTL_HA_STATUS_DISCONNECT) {
+			rq->ret = CTL_HA_STATUS_SUCCESS;
+		} else {
+			rq->ret = isc_ret;
+		}
+		callback(rq);
+		goto bailout;
+	}
+
+bailout:
+	return (retval);
+
+}
+
+static void
+ctl_datamove_remote_read(union ctl_io *io)
+{
+	int retval;
+	int i;
+
+	/*
+	 * This will send an error to the other controller in the case of a
+	 * failure.
+	 */
+	retval = ctl_datamove_remote_sgl_setup(io);
+	if (retval != 0)
+		return;
+
+	retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
+					  ctl_datamove_remote_read_cb);
+	if ((retval != 0)
+	 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) {
+		/*
+		 * Make sure we free memory if there was an error..  The
+		 * ctl_datamove_remote_xfer() function will send the
+		 * datamove done message, or call the callback with an
+		 * error if there is a problem.
+		 */
+		for (i = 0; i < io->scsiio.kern_sg_entries; i++)
+			free(io->io_hdr.local_sglist[i].addr, M_CTL);
+	}
+
+	return;
+}
+
+/*
+ * Process a datamove request from the other controller.  This is used for
+ * XFER mode only, not SER_ONLY mode.  For writes, we DMA into local memory
+ * first.  Once that is complete, the data gets DMAed into the remote
+ * controller's memory.  For reads, we DMA from the remote controller's
+ * memory into our memory first, and then move it out to the FETD.
+ *
+ * Should be called without the ctl_lock held.
+ */
+static void
+ctl_datamove_remote(union ctl_io *io)
+{
+	struct ctl_softc *softc;
+
+	softc = control_softc;
+
+	/*
+	 * Note that we look for an aborted I/O here, but don't do some of
+	 * the other checks that ctl_datamove() normally does.  We don't
+	 * need to run the task queue, because this I/O is on the ISC
+	 * queue, which is executed by the work thread after the task queue.
+	 * We don't need to run the datamove delay code, since that should
+	 * have been done if need be on the other controller.
+	 */
+	mtx_lock(&softc->ctl_lock);
+
+	if (io->io_hdr.flags & CTL_FLAG_ABORT) {
+
+		printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__,
+		       io->scsiio.tag_num, io->io_hdr.nexus.initid.id,
+		       io->io_hdr.nexus.targ_port,
+		       io->io_hdr.nexus.targ_target.id,
+		       io->io_hdr.nexus.targ_lun);
+		io->io_hdr.status = CTL_CMD_ABORTED;
+		io->io_hdr.port_status = 31338;
+
+		mtx_unlock(&softc->ctl_lock);
+
+		ctl_send_datamove_done(io, /*have_lock*/ 0);
+
+		return;
+	}
+
+	if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) {
+		mtx_unlock(&softc->ctl_lock);
+		ctl_datamove_remote_write(io);
+	} else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){
+		mtx_unlock(&softc->ctl_lock);
+		ctl_datamove_remote_read(io);
+	} else {
+		union ctl_ha_msg msg;
+		struct scsi_sense_data *sense;
+		uint8_t sks[3];
+		int retry_count;
+
+		memset(&msg, 0, sizeof(msg));
+
+		msg.hdr.msg_type = CTL_MSG_BAD_JUJU;
+		msg.hdr.status = CTL_SCSI_ERROR;
+		msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
+
+		retry_count = 4243;
+
+		sense = &msg.scsi.sense_data;
+		sks[0] = SSD_SCS_VALID;
+		sks[1] = (retry_count >> 8) & 0xff;
+		sks[2] = retry_count & 0xff;
+
+		/* "Internal target failure" */
+		scsi_set_sense_data(sense,
+				    /*sense_format*/ SSD_TYPE_NONE,
+				    /*current_error*/ 1,
+				    /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
+				    /*asc*/ 0x44,
+				    /*ascq*/ 0x00,
+				    /*type*/ SSD_ELEM_SKS,
+				    /*size*/ sizeof(sks),
+				    /*data*/ sks,
+				    SSD_ELEM_NONE);
+
+		io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
+		if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
+			ctl_failover_io(io, /*have_lock*/ 1);
+			mtx_unlock(&softc->ctl_lock);
+			return;
+		}
+
+		mtx_unlock(&softc->ctl_lock);
+
+		if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) >
+		    CTL_HA_STATUS_SUCCESS) {
+			/* XXX KDM what to do if this fails? */
+		}
+		return;
+	}
+	
+}
+
+static int
+ctl_process_done(union ctl_io *io, int have_lock)
+{
+	struct ctl_lun *lun;
+	struct ctl_softc *ctl_softc;
+	void (*fe_done)(union ctl_io *io);
+	uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port);
+
+	CTL_DEBUG_PRINT(("ctl_process_done\n"));
+
+	fe_done =
+	    control_softc->ctl_ports[targ_port]->fe_done;
+
+#ifdef CTL_TIME_IO
+	if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
+		char str[256];
+		char path_str[64];
+		struct sbuf sb;
+
+		ctl_scsi_path_string(io, path_str, sizeof(path_str));
+		sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
+
+		sbuf_cat(&sb, path_str);
+		switch (io->io_hdr.io_type) {
+		case CTL_IO_SCSI:
+			ctl_scsi_command_string(&io->scsiio, NULL, &sb);
+			sbuf_printf(&sb, "\n");
+			sbuf_cat(&sb, path_str);
+			sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
+				    io->scsiio.tag_num, io->scsiio.tag_type);
+			break;
+		case CTL_IO_TASK:
+			sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
+				    "Tag Type: %d\n", io->taskio.task_action,
+				    io->taskio.tag_num, io->taskio.tag_type);
+			break;
+		default:
+			printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
+			panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
+			break;
+		}
+		sbuf_cat(&sb, path_str);
+		sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
+			    (intmax_t)time_uptime - io->io_hdr.start_time);
+		sbuf_finish(&sb);
+		printf("%s", sbuf_data(&sb));
+	}
+#endif /* CTL_TIME_IO */
+
+	switch (io->io_hdr.io_type) {
+	case CTL_IO_SCSI:
+		break;
+	case CTL_IO_TASK:
+		ctl_io_error_print(io, NULL);
+		if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
+			ctl_free_io_internal(io, /*have_lock*/ 0);
+		else
+			fe_done(io);
+		return (CTL_RETVAL_COMPLETE);
+		break;
+	default:
+		printf("ctl_process_done: invalid io type %d\n",
+		       io->io_hdr.io_type);
+		panic("ctl_process_done: invalid io type %d\n",
+		      io->io_hdr.io_type);
+		break; /* NOTREACHED */
+	}
+
+	lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
+	if (lun == NULL) {
+		CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
+				 io->io_hdr.nexus.targ_lun));
+		fe_done(io);
+		goto bailout;
+	}
+	ctl_softc = lun->ctl_softc;
+
+	/*
+	 * Remove this from the OOA queue.
+	 */
+	if (have_lock == 0)
+		mtx_lock(&ctl_softc->ctl_lock);
+
+	/*
+	 * Check to see if we have any errors to inject here.  We only
+	 * inject errors for commands that don't already have errors set.
+	 */
+	if ((STAILQ_FIRST(&lun->error_list) != NULL)
+	 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))
+		ctl_inject_error(lun, io);
+
+	/*
+	 * XXX KDM how do we treat commands that aren't completed
+	 * successfully?
+	 *
+	 * XXX KDM should we also track I/O latency?
+	 */
+	if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
+		uint32_t blocksize;
+#ifdef CTL_TIME_IO
+		struct bintime cur_bt;
+#endif
+
+		if ((lun->be_lun != NULL)
+		 && (lun->be_lun->blocksize != 0))
+			blocksize = lun->be_lun->blocksize;
+		else
+			blocksize = 512;
+
+		switch (io->io_hdr.io_type) {
+		case CTL_IO_SCSI: {
+			int isread;
+			struct ctl_lba_len lbalen;
+
+			isread = 0;
+			switch (io->scsiio.cdb[0]) {
+			case READ_6:
+			case READ_10:
+			case READ_12:
+			case READ_16:
+				isread = 1;
+				/* FALLTHROUGH */
+			case WRITE_6:
+			case WRITE_10:
+			case WRITE_12:
+			case WRITE_16:
+			case WRITE_VERIFY_10:
+			case WRITE_VERIFY_12:
+			case WRITE_VERIFY_16:
+				memcpy(&lbalen, io->io_hdr.ctl_private[
+				       CTL_PRIV_LBA_LEN].bytes, sizeof(lbalen));
+
+				if (isread) {
+					lun->stats.ports[targ_port].bytes[CTL_STATS_READ] +=
+						lbalen.len * blocksize;
+					lun->stats.ports[targ_port].operations[CTL_STATS_READ]++;
+
+#ifdef CTL_TIME_IO
+					bintime_add(
+					   &lun->stats.ports[targ_port].dma_time[CTL_STATS_READ],
+					   &io->io_hdr.dma_bt);
+					lun->stats.ports[targ_port].num_dmas[CTL_STATS_READ] +=
+						io->io_hdr.num_dmas;
+					getbintime(&cur_bt);
+					bintime_sub(&cur_bt,
+						    &io->io_hdr.start_bt);
+
+					bintime_add(
+					    &lun->stats.ports[targ_port].time[CTL_STATS_READ],
+					    &cur_bt);
+
+#if 0
+					cs_prof_gettime(&cur_ticks);
+					lun->stats.time[CTL_STATS_READ] +=
+						cur_ticks -
+						io->io_hdr.start_ticks;
+#endif
+#if 0
+					lun->stats.time[CTL_STATS_READ] +=
+						jiffies - io->io_hdr.start_time;
+#endif
+#endif /* CTL_TIME_IO */
+				} else {
+					lun->stats.ports[targ_port].bytes[CTL_STATS_WRITE] +=
+						lbalen.len * blocksize;
+					lun->stats.ports[targ_port].operations[
+						CTL_STATS_WRITE]++;
+
+#ifdef CTL_TIME_IO
+					bintime_add(
+					  &lun->stats.ports[targ_port].dma_time[CTL_STATS_WRITE],
+					  &io->io_hdr.dma_bt);
+					lun->stats.ports[targ_port].num_dmas[CTL_STATS_WRITE] +=
+						io->io_hdr.num_dmas;
+					getbintime(&cur_bt);
+					bintime_sub(&cur_bt,
+						    &io->io_hdr.start_bt);
+
+					bintime_add(
+					    &lun->stats.ports[targ_port].time[CTL_STATS_WRITE],
+					    &cur_bt);
+#if 0
+					cs_prof_gettime(&cur_ticks);
+					lun->stats.ports[targ_port].time[CTL_STATS_WRITE] +=
+						cur_ticks -
+						io->io_hdr.start_ticks;
+					lun->stats.ports[targ_port].time[CTL_STATS_WRITE] +=
+						jiffies - io->io_hdr.start_time;
+#endif
+#endif /* CTL_TIME_IO */
+				}
+				break;
+			default:
+				lun->stats.ports[targ_port].operations[CTL_STATS_NO_IO]++;
+
+#ifdef CTL_TIME_IO
+				bintime_add(
+				  &lun->stats.ports[targ_port].dma_time[CTL_STATS_NO_IO],
+				  &io->io_hdr.dma_bt);
+				lun->stats.ports[targ_port].num_dmas[CTL_STATS_NO_IO] +=
+					io->io_hdr.num_dmas;
+				getbintime(&cur_bt);
+				bintime_sub(&cur_bt, &io->io_hdr.start_bt);
+
+				bintime_add(&lun->stats.ports[targ_port].time[CTL_STATS_NO_IO],
+					    &cur_bt);
+
+#if 0
+				cs_prof_gettime(&cur_ticks);
+				lun->stats.ports[targ_port].time[CTL_STATS_NO_IO] +=
+					cur_ticks -
+					io->io_hdr.start_ticks;
+				lun->stats.ports[targ_port].time[CTL_STATS_NO_IO] +=
+					jiffies - io->io_hdr.start_time;
+#endif
+#endif /* CTL_TIME_IO */
+				break;
+			}
+			break;
+		}
+		default:
+			break;
+		}
+	}
+
+	TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
+
+	/*
+	 * Run through the blocked queue on this LUN and see if anything
+	 * has become unblocked, now that this transaction is done.
+	 */
+	ctl_check_blocked(lun);
+
+	/*
+	 * If the LUN has been invalidated, free it if there is nothing
+	 * left on its OOA queue.
+	 */
+	if ((lun->flags & CTL_LUN_INVALID)
+	 && (TAILQ_FIRST(&lun->ooa_queue) == NULL))
+		ctl_free_lun(lun);
+
+	/*
+	 * If this command has been aborted, make sure we set the status
+	 * properly.  The FETD is responsible for freeing the I/O and doing
+	 * whatever it needs to do to clean up its state.
+	 */
+	if (io->io_hdr.flags & CTL_FLAG_ABORT)
+		io->io_hdr.status = CTL_CMD_ABORTED;
+
+	/*
+	 * We print out status for every task management command.  For SCSI
+	 * commands, we filter out any unit attention errors; they happen
+	 * on every boot, and would clutter up the log.  Note:  task
+	 * management commands aren't printed here, they are printed above,
+	 * since they should never even make it down here.
+	 */
+	switch (io->io_hdr.io_type) {
+	case CTL_IO_SCSI: {
+		int error_code, sense_key, asc, ascq;
+
+		sense_key = 0;
+
+		if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR)
+		 && (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) {
+			/*
+			 * Since this is just for printing, no need to
+			 * show errors here.
+			 */
+			scsi_extract_sense_len(&io->scsiio.sense_data,
+					       io->scsiio.sense_len,
+					       &error_code,
+					       &sense_key,
+					       &asc,
+					       &ascq,
+					       /*show_errors*/ 0);
+		}
+
+		if (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
+		 && (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SCSI_ERROR)
+		  || (io->scsiio.scsi_status != SCSI_STATUS_CHECK_COND)
+		  || (sense_key != SSD_KEY_UNIT_ATTENTION))) {
+
+			if ((time_uptime - ctl_softc->last_print_jiffies) <= 0){
+				ctl_softc->skipped_prints++;
+				if (have_lock == 0)
+					mtx_unlock(&ctl_softc->ctl_lock);
+			} else {
+				uint32_t skipped_prints;
+
+				skipped_prints = ctl_softc->skipped_prints;
+
+				ctl_softc->skipped_prints = 0;
+				ctl_softc->last_print_jiffies = time_uptime;
+
+				if (have_lock == 0)
+					mtx_unlock(&ctl_softc->ctl_lock);
+				if (skipped_prints > 0) {
+#ifdef NEEDTOPORT
+					csevent_log(CSC_CTL | CSC_SHELF_SW |
+					    CTL_ERROR_REPORT,
+					    csevent_LogType_Trace,
+					    csevent_Severity_Information,
+					    csevent_AlertLevel_Green,
+					    csevent_FRU_Firmware,
+					    csevent_FRU_Unknown,
+					    "High CTL error volume, %d prints "
+					    "skipped", skipped_prints);
+#endif
+				}
+				ctl_io_error_print(io, NULL);
+			}
+		} else {
+			if (have_lock == 0)
+				mtx_unlock(&ctl_softc->ctl_lock);
+		}
+		break;
+	}
+	case CTL_IO_TASK:
+		if (have_lock == 0)
+			mtx_unlock(&ctl_softc->ctl_lock);
+		ctl_io_error_print(io, NULL);
+		break;
+	default:
+		if (have_lock == 0)
+			mtx_unlock(&ctl_softc->ctl_lock);
+		break;
+	}
+
+	/*
+	 * Tell the FETD or the other shelf controller we're done with this
+	 * command.  Note that only SCSI commands get to this point.  Task
+	 * management commands are completed above.
+	 *
+	 * We only send status to the other controller if we're in XFER
+	 * mode.  In SER_ONLY mode, the I/O is done on the controller that
+	 * received the I/O (from CTL's perspective), and so the status is
+	 * generated there.
+	 * 
+	 * XXX KDM if we hold the lock here, we could cause a deadlock
+	 * if the frontend comes back in in this context to queue
+	 * something.
+	 */
+	if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER)
+	 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
+		union ctl_ha_msg msg;
+
+		memset(&msg, 0, sizeof(msg));
+		msg.hdr.msg_type = CTL_MSG_FINISH_IO;
+		msg.hdr.original_sc = io->io_hdr.original_sc;
+		msg.hdr.nexus = io->io_hdr.nexus;
+		msg.hdr.status = io->io_hdr.status;
+		msg.scsi.scsi_status = io->scsiio.scsi_status;
+		msg.scsi.tag_num = io->scsiio.tag_num;
+		msg.scsi.tag_type = io->scsiio.tag_type;
+		msg.scsi.sense_len = io->scsiio.sense_len;
+		msg.scsi.sense_residual = io->scsiio.sense_residual;
+		msg.scsi.residual = io->scsiio.residual;
+		memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
+		       sizeof(io->scsiio.sense_data));
+		/*
+		 * We copy this whether or not this is an I/O-related
+		 * command.  Otherwise, we'd have to go and check to see
+		 * whether it's a read/write command, and it really isn't
+		 * worth it.
+		 */
+		memcpy(&msg.scsi.lbalen,
+		       &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
+		       sizeof(msg.scsi.lbalen));;
+
+		if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
+				sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
+			/* XXX do something here */
+		}
+
+		ctl_free_io_internal(io, /*have_lock*/ 0);
+	} else 
+		fe_done(io);
+
+bailout:
+
+	return (CTL_RETVAL_COMPLETE);
+}
+
+/*
+ * Front end should call this if it doesn't do autosense.  When the request
+ * sense comes back in from the initiator, we'll dequeue this and send it.
+ */
+int
+ctl_queue_sense(union ctl_io *io)
+{
+	struct ctl_lun *lun;
+	struct ctl_softc *ctl_softc;
+	uint32_t initidx;
+
+	ctl_softc = control_softc;
+
+	CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
+
+	/*
+	 * LUN lookup will likely move to the ctl_work_thread() once we
+	 * have our new queueing infrastructure (that doesn't put things on
+	 * a per-LUN queue initially).  That is so that we can handle
+	 * things like an INQUIRY to a LUN that we don't have enabled.  We
+	 * can't deal with that right now.
+	 */
+	mtx_lock(&ctl_softc->ctl_lock);
+
+	/*
+	 * If we don't have a LUN for this, just toss the sense
+	 * information.
+	 */
+	if ((io->io_hdr.nexus.targ_lun < CTL_MAX_LUNS)
+	 && (ctl_softc->ctl_luns[io->io_hdr.nexus.targ_lun] != NULL))
+		lun = ctl_softc->ctl_luns[io->io_hdr.nexus.targ_lun];
+	else
+		goto bailout;
+
+	initidx = ctl_get_initindex(&io->io_hdr.nexus);
+
+	/*
+	 * Already have CA set for this LUN...toss the sense information.
+	 */
+	if (ctl_is_set(lun->have_ca, initidx))
+		goto bailout;
+
+	memcpy(&lun->pending_sense[initidx].sense, &io->scsiio.sense_data,
+	       ctl_min(sizeof(lun->pending_sense[initidx].sense),
+	       sizeof(io->scsiio.sense_data)));
+	ctl_set_mask(lun->have_ca, initidx);
+
+bailout:
+	mtx_unlock(&ctl_softc->ctl_lock);
+
+	ctl_free_io(io);
+
+	return (CTL_RETVAL_COMPLETE);
+}
+
+/*
+ * Primary command inlet from frontend ports.  All SCSI and task I/O
+ * requests must go through this function.
+ */
+int
+ctl_queue(union ctl_io *io)
+{
+	struct ctl_softc *ctl_softc;
+
+	CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
+
+	ctl_softc = control_softc;
+
+#ifdef CTL_TIME_IO
+	io->io_hdr.start_time = time_uptime;
+	getbintime(&io->io_hdr.start_bt);
+#endif /* CTL_TIME_IO */
+
+	mtx_lock(&ctl_softc->ctl_lock);
+
+	switch (io->io_hdr.io_type) {
+	case CTL_IO_SCSI:
+		STAILQ_INSERT_TAIL(&ctl_softc->incoming_queue, &io->io_hdr,
+				   links);
+		break;
+	case CTL_IO_TASK:
+		STAILQ_INSERT_TAIL(&ctl_softc->task_queue, &io->io_hdr, links);
+		/*
+		 * Set the task pending flag.  This is necessary to close a
+		 * race condition with the FETD:
+		 *
+		 * - FETD submits a task management command, like an abort.
+		 * - Back end calls fe_datamove() to move the data for the
+		 *   aborted command.  The FETD can't really accept it, but
+		 *   if it did, it would end up transmitting data for a
+		 *   command that the initiator told us to abort.
+		 *
+		 * We close the race condition by setting the flag here,
+		 * and checking it in ctl_datamove(), before calling the
+		 * FETD's fe_datamove routine.  If we've got a task
+		 * pending, we run the task queue and then check to see
+		 * whether our particular I/O has been aborted.
+		 */
+		ctl_softc->flags |= CTL_FLAG_TASK_PENDING;
+		break;
+	default:
+		mtx_unlock(&ctl_softc->ctl_lock);
+		printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
+		return (-EINVAL);
+		break; /* NOTREACHED */
+	}
+	mtx_unlock(&ctl_softc->ctl_lock);
+
+	ctl_wakeup_thread();
+
+	return (CTL_RETVAL_COMPLETE);
+}
+
+#ifdef CTL_IO_DELAY
+static void
+ctl_done_timer_wakeup(void *arg)
+{
+	union ctl_io *io;
+
+	io = (union ctl_io *)arg;
+	ctl_done_lock(io, /*have_lock*/ 0);
+}
+#endif /* CTL_IO_DELAY */
+
+void
+ctl_done_lock(union ctl_io *io, int have_lock)
+{
+	struct ctl_softc *ctl_softc;
+#ifndef CTL_DONE_THREAD
+	union ctl_io *xio;
+#endif /* !CTL_DONE_THREAD */
+
+	ctl_softc = control_softc;
+
+	if (have_lock == 0)
+		mtx_lock(&ctl_softc->ctl_lock);
+
+	/*
+	 * Enable this to catch duplicate completion issues.
+	 */
+#if 0
+	if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
+		printf("%s: type %d msg %d cdb %x iptl: "
+		       "%d:%d:%d:%d tag 0x%04x "
+		       "flag %#x status %x\n",
+			__func__,
+			io->io_hdr.io_type,
+			io->io_hdr.msg_type,
+			io->scsiio.cdb[0],
+			io->io_hdr.nexus.initid.id,
+			io->io_hdr.nexus.targ_port,
+			io->io_hdr.nexus.targ_target.id,
+			io->io_hdr.nexus.targ_lun,
+			(io->io_hdr.io_type ==
+			CTL_IO_TASK) ?
+			io->taskio.tag_num :
+			io->scsiio.tag_num,
+		        io->io_hdr.flags,
+			io->io_hdr.status);
+	} else
+		io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
+#endif
+
+	/*
+	 * This is an internal copy of an I/O, and should not go through
+	 * the normal done processing logic.
+	 */
+	if (io->io_hdr.flags & CTL_FLAG_INT_COPY) {
+		if (have_lock == 0)
+			mtx_unlock(&ctl_softc->ctl_lock);
+		return;
+	}
+
+	/*
+	 * We need to send a msg to the serializing shelf to finish the IO
+	 * as well.  We don't send a finish message to the other shelf if
+	 * this is a task management command.  Task management commands
+	 * aren't serialized in the OOA queue, but rather just executed on
+	 * both shelf controllers for commands that originated on that
+	 * controller.
+	 */
+	if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)
+	 && (io->io_hdr.io_type != CTL_IO_TASK)) {
+		union ctl_ha_msg msg_io;
+
+		msg_io.hdr.msg_type = CTL_MSG_FINISH_IO;
+		msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc;
+		if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io,
+		    sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) {
+		}
+		/* continue on to finish IO */
+	}
+#ifdef CTL_IO_DELAY
+	if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
+		struct ctl_lun *lun;
+
+		lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
+
+		io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
+	} else {
+		struct ctl_lun *lun;
+
+		lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
+
+		if ((lun != NULL)
+		 && (lun->delay_info.done_delay > 0)) {
+			struct callout *callout;
+
+			callout = (struct callout *)&io->io_hdr.timer_bytes;
+			callout_init(callout, /*mpsafe*/ 1);
+			io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
+			callout_reset(callout,
+				      lun->delay_info.done_delay * hz,
+				      ctl_done_timer_wakeup, io);
+			if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
+				lun->delay_info.done_delay = 0;
+			if (have_lock == 0)
+				mtx_unlock(&ctl_softc->ctl_lock);
+			return;
+		}
+	}
+#endif /* CTL_IO_DELAY */
+
+	STAILQ_INSERT_TAIL(&ctl_softc->done_queue, &io->io_hdr, links);
+
+#ifdef CTL_DONE_THREAD
+	if (have_lock == 0)
+		mtx_unlock(&ctl_softc->ctl_lock);
+
+	ctl_wakeup_thread();
+#else /* CTL_DONE_THREAD */
+	for (xio = (union ctl_io *)STAILQ_FIRST(&ctl_softc->done_queue);
+	     xio != NULL;
+	     xio =(union ctl_io *)STAILQ_FIRST(&ctl_softc->done_queue)) {
+
+		STAILQ_REMOVE_HEAD(&ctl_softc->done_queue, links);
+
+		ctl_process_done(xio, /*have_lock*/ 1);
+	}
+	if (have_lock == 0)
+		mtx_unlock(&ctl_softc->ctl_lock);
+#endif /* CTL_DONE_THREAD */
+}
+
+void
+ctl_done(union ctl_io *io)
+{
+	ctl_done_lock(io, /*have_lock*/ 0);
+}
+
+int
+ctl_isc(struct ctl_scsiio *ctsio)
+{
+	struct ctl_lun *lun;
+	int retval;
+
+	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
+
+	CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0]));
+
+	CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n"));
+
+	retval = lun->backend->data_submit((union ctl_io *)ctsio);
+
+	return (retval);
+}
+
+
+static void
+ctl_work_thread(void *arg)
+{
+	struct ctl_softc *softc;
+	union ctl_io *io;
+	struct ctl_be_lun *be_lun;
+	int retval;
+
+	CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
+
+	softc = (struct ctl_softc *)arg;
+	if (softc == NULL)
+		return;
+
+	mtx_lock(&softc->ctl_lock);
+	for (;;) {
+		retval = 0;
+
+		/*
+		 * We handle the queues in this order:
+		 * - task management
+		 * - ISC
+		 * - done queue (to free up resources, unblock other commands)
+		 * - RtR queue
+		 * - incoming queue
+		 *
+		 * If those queues are empty, we break out of the loop and
+		 * go to sleep.
+		 */
+		io = (union ctl_io *)STAILQ_FIRST(&softc->task_queue);
+		if (io != NULL) {
+			ctl_run_task_queue(softc);
+			continue;
+		}
+		io = (union ctl_io *)STAILQ_FIRST(&softc->isc_queue);
+		if (io != NULL) {
+			STAILQ_REMOVE_HEAD(&softc->isc_queue, links);
+			ctl_handle_isc(io);
+			continue;
+		}
+		io = (union ctl_io *)STAILQ_FIRST(&softc->done_queue);
+		if (io != NULL) {
+			STAILQ_REMOVE_HEAD(&softc->done_queue, links);
+			/* clear any blocked commands, call fe_done */
+			mtx_unlock(&softc->ctl_lock);
+			/*
+			 * XXX KDM
+			 * Call this without a lock for now.  This will
+			 * depend on whether there is any way the FETD can
+			 * sleep or deadlock if called with the CTL lock
+			 * held.
+			 */
+			retval = ctl_process_done(io, /*have_lock*/ 0);
+			mtx_lock(&softc->ctl_lock);
+			continue;
+		}
+		if (!ctl_pause_rtr) {
+			io = (union ctl_io *)STAILQ_FIRST(&softc->rtr_queue);
+			if (io != NULL) {
+				STAILQ_REMOVE_HEAD(&softc->rtr_queue, links);
+				mtx_unlock(&softc->ctl_lock);
+				goto execute;
+			}
+		}
+		io = (union ctl_io *)STAILQ_FIRST(&softc->incoming_queue);
+		if (io != NULL) {
+			STAILQ_REMOVE_HEAD(&softc->incoming_queue, links);
+			mtx_unlock(&softc->ctl_lock);
+			ctl_scsiio_precheck(softc, &io->scsiio);
+			mtx_lock(&softc->ctl_lock);
+			continue;
+		}
+		/*
+		 * We might want to move this to a separate thread, so that
+		 * configuration requests (in this case LUN creations)
+		 * won't impact the I/O path.
+		 */
+		be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
+		if (be_lun != NULL) {
+			STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
+			mtx_unlock(&softc->ctl_lock);
+			ctl_create_lun(be_lun);
+			mtx_lock(&softc->ctl_lock);
+			continue;
+		}
+
+		/* XXX KDM use the PDROP flag?? */
+		/* Sleep until we have something to do. */
+		mtx_sleep(softc, &softc->ctl_lock, PRIBIO, "ctl_work", 0);
+
+		/* Back to the top of the loop to see what woke us up. */
+		continue;
+
+execute:
+		retval = ctl_scsiio(&io->scsiio);
+		switch (retval) {
+		case CTL_RETVAL_COMPLETE:
+			break;
+		default:
+			/*
+			 * Probably need to make sure this doesn't happen.
+			 */
+			break;
+		}
+		mtx_lock(&softc->ctl_lock);
+	}
+}
+
+void
+ctl_wakeup_thread()
+{
+	struct ctl_softc *softc;
+
+	softc = control_softc;
+
+	wakeup(softc);
+}
+
+/* Initialization and failover */
+
+void
+ctl_init_isc_msg(void)
+{
+	printf("CTL: Still calling this thing\n");
+}
+
+/*
+ * Init component
+ * 	Initializes component into configuration defined by bootMode
+ *	(see hasc-sv.c)
+ *  	returns hasc_Status:
+ * 		OK
+ *		ERROR - fatal error
+ */
+static ctl_ha_comp_status
+ctl_isc_init(struct ctl_ha_component *c)
+{
+	ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
+
+	c->status = ret;
+	return ret;
+}
+
+/* Start component
+ * 	Starts component in state requested. If component starts successfully,
+ *	it must set its own state to the requestrd state
+ *	When requested state is HASC_STATE_HA, the component may refine it
+ * 	by adding _SLAVE or _MASTER flags.
+ *	Currently allowed state transitions are:
+ *	UNKNOWN->HA		- initial startup
+ *	UNKNOWN->SINGLE - initial startup when no parter detected
+ *	HA->SINGLE		- failover
+ * returns ctl_ha_comp_status:
+ * 		OK	- component successfully started in requested state
+ *		FAILED  - could not start the requested state, failover may
+ * 			  be possible
+ *		ERROR	- fatal error detected, no future startup possible
+ */
+static ctl_ha_comp_status
+ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state)
+{
+	ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
+
+	// UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap)
+	if (c->state == CTL_HA_STATE_UNKNOWN ) {
+		ctl_is_single = 0;
+		if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
+		    != CTL_HA_STATUS_SUCCESS) {
+			printf("ctl_isc_start: ctl_ha_msg_create failed.\n");
+			ret = CTL_HA_COMP_STATUS_ERROR;
+		}
+	} else if (CTL_HA_STATE_IS_HA(c->state)
+		&& CTL_HA_STATE_IS_SINGLE(state)){
+		// HA->SINGLE transition
+	        ctl_failover();
+		ctl_is_single = 1;
+	} else {
+		printf("ctl_isc_start:Invalid state transition %X->%X\n",
+		       c->state, state);
+		ret = CTL_HA_COMP_STATUS_ERROR;
+	}
+	if (CTL_HA_STATE_IS_SINGLE(state))
+		ctl_is_single = 1;
+
+	c->state = state;
+	c->status = ret;
+	return ret;
+}
+
+/*
+ * Quiesce component
+ * The component must clear any error conditions (set status to OK) and
+ * prepare itself to another Start call
+ * returns ctl_ha_comp_status:
+ * 	OK
+ *	ERROR
+ */
+static ctl_ha_comp_status
+ctl_isc_quiesce(struct ctl_ha_component *c)
+{
+	int ret = CTL_HA_COMP_STATUS_OK;
+
+	ctl_pause_rtr = 1;
+	c->status = ret;
+	return ret;
+}
+
+struct ctl_ha_component ctl_ha_component_ctlisc =
+{
+	.name = "CTL ISC",
+	.state = CTL_HA_STATE_UNKNOWN,
+	.init = ctl_isc_init,
+	.start = ctl_isc_start,
+	.quiesce = ctl_isc_quiesce
+};
+
+/*
+ *  vim: ts=8
+ */
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/cam/ctl/ctl.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/sys/cam/ctl/ctl.h	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,216 @@
+/*-
+ * Copyright (c) 2003 Silicon Graphics International Corp.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions, and the following disclaimer,
+ *    without modification.
+ * 2. Redistributions in binary form must reproduce at minimum a disclaimer
+ *    substantially similar to the "NO WARRANTY" disclaimer below
+ *    ("Disclaimer") and any redistribution must be conditioned upon
+ *    including a substantially similar Disclaimer requirement for further
+ *    binary redistribution.
+ *
+ * NO WARRANTY
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGES.
+ *
+ * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl.h#5 $
+ * $FreeBSD: head/sys/cam/ctl/ctl.h 229997 2012-01-12 00:34:33Z ken $
+ */
+/*
+ * Function definitions used both within CTL and potentially in various CTL
+ * clients.
+ *
+ * Author: Ken Merry <ken at FreeBSD.org>
+ */
+
+#ifndef	_CTL_H_
+#define	_CTL_H_
+
+#define	ctl_min(x,y)	(((x) < (y)) ? (x) : (y))
+#define	CTL_RETVAL_COMPLETE	0
+#define	CTL_RETVAL_QUEUED	1
+#define	CTL_RETVAL_ALLOCATED	2
+#define	CTL_RETVAL_ERROR	3
+
+typedef enum {
+	CTL_PORT_NONE		= 0x00,
+	CTL_PORT_FC		= 0x01,
+	CTL_PORT_SCSI		= 0x02,
+	CTL_PORT_IOCTL		= 0x04,
+	CTL_PORT_INTERNAL	= 0x08,
+	CTL_PORT_ALL		= 0xff,
+	CTL_PORT_ISC		= 0x100 // FC port for inter-shelf communication
+} ctl_port_type;
+
+struct ctl_port_entry {
+	ctl_port_type		port_type;
+	char			port_name[64];
+	int32_t			targ_port;
+	int			physical_port;
+	int			virtual_port;
+	u_int			flags;
+#define	CTL_PORT_WWNN_VALID	0x01
+#define	CTL_PORT_WWPN_VALID	0x02
+	uint64_t		wwnn;
+	uint64_t		wwpn;
+	int			online;
+};
+
+struct ctl_modepage_header {
+	uint8_t page_code;
+	uint8_t subpage;
+	int32_t len_used;
+	int32_t len_left;
+};
+
+struct ctl_modepage_aps {
+	struct ctl_modepage_header header;
+	uint8_t lock_active;
+};
+
+union ctl_modepage_info {
+	struct ctl_modepage_header header;
+	struct ctl_modepage_aps aps;
+};
+
+/*
+ * Serial number length, for VPD page 0x80.
+ */
+#define	CTL_SN_LEN	16
+
+/*
+ * Device ID length, for VPD page 0x83.
+ */
+#define	CTL_DEVID_LEN	16
+/*
+ * WWPN length, for VPD page 0x83.
+ */
+#define CTL_WWPN_LEN   8
+
+/*
+ * Unit attention types. ASC/ASCQ values for these should be placed in
+ * ctl_build_ua.  These are also listed in order of reporting priority.
+ * i.e. a poweron UA is reported first, bus reset second, etc.
+ */
+typedef enum {
+	CTL_UA_NONE		= 0x0000,
+	CTL_UA_POWERON		= 0x0001,
+	CTL_UA_BUS_RESET	= 0x0002,
+	CTL_UA_TARG_RESET	= 0x0004,
+	CTL_UA_LUN_RESET	= 0x0008,
+	CTL_UA_LUN_CHANGE	= 0x0010,
+	CTL_UA_MODE_CHANGE	= 0x0020,
+	CTL_UA_LOG_CHANGE	= 0x0040,
+	CTL_UA_LVD		= 0x0080,
+	CTL_UA_SE		= 0x0100,
+	CTL_UA_RES_PREEMPT	= 0x0200,
+	CTL_UA_RES_RELEASE	= 0x0400,
+	CTL_UA_REG_PREEMPT  	= 0x0800,
+	CTL_UA_ASYM_ACC_CHANGE  = 0x1000
+} ctl_ua_type;
+
+#ifdef	_KERNEL
+
+MALLOC_DECLARE(M_CTL);
+
+typedef enum {
+	CTL_THREAD_NONE		= 0x00,
+	CTL_THREAD_WAKEUP	= 0x01
+} ctl_thread_flags;
+
+struct ctl_thread {
+	void			(*thread_func)(void *arg);
+	void			*arg;
+	struct cv		wait_queue;
+	const char		*thread_name;
+	ctl_thread_flags	thread_flags;
+	struct completion	*thread_event;
+	struct task_struct	*task;
+};
+
+struct ctl_page_index;
+
+#ifdef SYSCTL_DECL	/* from sysctl.h */
+SYSCTL_DECL(_kern_cam_ctl);
+#endif
+
+/*
+ * Call these routines to enable or disable front end ports.
+ */
+int ctl_port_enable(ctl_port_type port_type);
+int ctl_port_disable(ctl_port_type port_type);
+/*
+ * This routine grabs a list of frontend ports.
+ */
+int ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced,
+		  int *num_entries_filled, int *num_entries_dropped,
+		  ctl_port_type port_type, int no_virtual);
+
+/*
+ * Put a string into an sbuf, escaping characters that are illegal or not
+ * recommended in XML.  Note this doesn't escape everything, just > < and &.
+ */
+int ctl_sbuf_printf_esc(struct sbuf *sb, char *str);
+
+int ctl_ffz(uint32_t *mask, uint32_t size);
+int ctl_set_mask(uint32_t *mask, uint32_t bit);
+int ctl_clear_mask(uint32_t *mask, uint32_t bit);
+int ctl_is_set(uint32_t *mask, uint32_t bit);
+int ctl_control_page_handler(struct ctl_scsiio *ctsio,
+			     struct ctl_page_index *page_index,
+			     uint8_t *page_ptr);
+/**
+int ctl_failover_sp_handler(struct ctl_scsiio *ctsio,
+			    struct ctl_page_index *page_index,
+			    uint8_t *page_ptr);
+**/
+int ctl_power_sp_handler(struct ctl_scsiio *ctsio,
+			 struct ctl_page_index *page_index, uint8_t *page_ptr);
+int ctl_power_sp_sense_handler(struct ctl_scsiio *ctsio,
+			       struct ctl_page_index *page_index, int pc);
+int ctl_aps_sp_handler(struct ctl_scsiio *ctsio,
+		       struct ctl_page_index *page_index, uint8_t *page_ptr);
+int ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio,
+				   struct ctl_page_index *page_index,
+				   int pc);
+int ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio,
+				    struct ctl_page_index *page_index,
+				    uint8_t *page_ptr);
+int ctl_config_move_done(union ctl_io *io);
+void ctl_datamove(union ctl_io *io);
+void ctl_done(union ctl_io *io);
+void ctl_config_write_done(union ctl_io *io);
+#if 0
+int ctl_thread(void *arg);
+#endif
+void ctl_wakeup_thread(void);
+#if 0
+struct ctl_thread *ctl_create_thread(void (*thread_func)
+	(void *thread_arg), void *thread_arg, const char *thread_name);
+void ctl_signal_thread(struct ctl_thread *thread);
+void ctl_shutdown_thread(struct ctl_thread *thread);
+#endif
+void ctl_portDB_changed(int portnum);
+void ctl_init_isc_msg(void);
+
+#endif	/* _KERNEL */
+
+#endif	/* _CTL_H_ */
+
+/*
+ * vim: ts=8
+ */
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/cam/ctl/ctl_backend.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/sys/cam/ctl/ctl_backend.c	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,177 @@
+/*-
+ * Copyright (c) 2003 Silicon Graphics International Corp.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions, and the following disclaimer,
+ *    without modification.
+ * 2. Redistributions in binary form must reproduce at minimum a disclaimer
+ *    substantially similar to the "NO WARRANTY" disclaimer below
+ *    ("Disclaimer") and any redistribution must be conditioned upon
+ *    including a substantially similar Disclaimer requirement for further
+ *    binary redistribution.
+ *
+ * NO WARRANTY
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGES.
+ *
+ * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_backend.c#3 $
+ */
+/*
+ * CTL backend driver registration routines
+ *
+ * Author: Ken Merry <ken at FreeBSD.org>
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD: head/sys/cam/ctl/ctl_backend.c 229997 2012-01-12 00:34:33Z ken $");
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/kernel.h>
+#include <sys/types.h>
+#include <sys/malloc.h>
+#include <sys/lock.h>
+#include <sys/mutex.h>
+#include <sys/condvar.h>
+#include <sys/queue.h>
+
+#include <cam/scsi/scsi_all.h>
+#include <cam/scsi/scsi_da.h>
+#include <cam/ctl/ctl_io.h>
+#include <cam/ctl/ctl.h>
+#include <cam/ctl/ctl_frontend.h>
+#include <cam/ctl/ctl_backend.h>
+#include <cam/ctl/ctl_frontend_internal.h>
+#include <cam/ctl/ctl_ioctl.h>
+#include <cam/ctl/ctl_ha.h>
+#include <cam/ctl/ctl_private.h>
+#include <cam/ctl/ctl_debug.h>
+
+extern struct ctl_softc *control_softc;
+
+int
+ctl_backend_register(struct ctl_backend_driver *be)
+{
+	struct ctl_softc *ctl_softc;
+	struct ctl_backend_driver *be_tmp;
+
+	ctl_softc = control_softc;
+
+	mtx_lock(&ctl_softc->ctl_lock);
+	/*
+	 * Sanity check, make sure this isn't a duplicate registration.
+	 */
+	STAILQ_FOREACH(be_tmp, &ctl_softc->be_list, links) {
+		if (strcmp(be_tmp->name, be->name) == 0) {
+			mtx_unlock(&ctl_softc->ctl_lock);
+			return (-1);
+		}
+	}
+	mtx_unlock(&ctl_softc->ctl_lock);
+
+	/*
+	 * Call the backend's initialization routine.
+	 */
+	be->init();
+
+	mtx_lock(&ctl_softc->ctl_lock);
+	
+	STAILQ_INSERT_TAIL(&ctl_softc->be_list, be, links);
+
+	ctl_softc->num_backends++;
+
+	/*
+	 * Don't want to increment the usage count for internal consumers,
+	 * we won't be able to unload otherwise.
+	 */
+	/* XXX KDM find a substitute for this? */
+#if 0
+	if ((be->flags & CTL_BE_FLAG_INTERNAL) == 0)
+		MOD_INC_USE_COUNT;
+#endif
+
+#ifdef CS_BE_CONFIG_MOVE_DONE_IS_NOT_USED
+	be->config_move_done = ctl_config_move_done;
+#endif
+	/* XXX KDM fix this! */
+	be->num_luns = 0;
+#if 0
+	atomic_set(&be->num_luns, 0);
+#endif
+
+	mtx_unlock(&ctl_softc->ctl_lock);
+
+	return (0);
+}
+
+int
+ctl_backend_deregister(struct ctl_backend_driver *be)
+{
+	struct ctl_softc *ctl_softc;
+
+	ctl_softc = control_softc;
+
+	mtx_lock(&ctl_softc->ctl_lock);
+
+#if 0
+	if (atomic_read(&be->num_luns) != 0) {
+#endif
+	/* XXX KDM fix this! */
+	if (be->num_luns != 0) {
+		mtx_unlock(&ctl_softc->ctl_lock);
+		return (-1);
+	}
+
+	STAILQ_REMOVE(&ctl_softc->be_list, be, ctl_backend_driver, links);
+
+	ctl_softc->num_backends--;
+
+	/* XXX KDM find a substitute for this? */
+#if 0
+	if ((be->flags & CTL_BE_FLAG_INTERNAL) == 0)
+		MOD_DEC_USE_COUNT;
+#endif
+
+	mtx_unlock(&ctl_softc->ctl_lock);
+
+	return (0);
+}
+
+struct ctl_backend_driver *
+ctl_backend_find(char *backend_name)
+{
+	struct ctl_softc *ctl_softc;
+	struct ctl_backend_driver *be_tmp;
+
+	ctl_softc = control_softc;
+
+	mtx_lock(&ctl_softc->ctl_lock);
+
+	STAILQ_FOREACH(be_tmp, &ctl_softc->be_list, links) {
+		if (strcmp(be_tmp->name, backend_name) == 0) {
+			mtx_unlock(&ctl_softc->ctl_lock);
+			return (be_tmp);
+		}
+	}
+
+	mtx_unlock(&ctl_softc->ctl_lock);
+
+	return (NULL);
+}
+
+/*
+ * vim: ts=8
+ */
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/cam/ctl/ctl_backend.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/sys/cam/ctl/ctl_backend.h	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,288 @@
+/*-
+ * Copyright (c) 2003 Silicon Graphics International Corp.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions, and the following disclaimer,
+ *    without modification.
+ * 2. Redistributions in binary form must reproduce at minimum a disclaimer
+ *    substantially similar to the "NO WARRANTY" disclaimer below
+ *    ("Disclaimer") and any redistribution must be conditioned upon
+ *    including a substantially similar Disclaimer requirement for further
+ *    binary redistribution.
+ *
+ * NO WARRANTY
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGES.
+ *
+ * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_backend.h#2 $
+ * $FreeBSD: head/sys/cam/ctl/ctl_backend.h 229997 2012-01-12 00:34:33Z ken $
+ */
+/*
+ * CTL backend driver definitions
+ *
+ * Author: Ken Merry <ken at FreeBSD.org>
+ */
+
+#ifndef	_CTL_BACKEND_H_
+#define	_CTL_BACKEND_H_
+
+/*
+ * XXX KDM move this to another header file?
+ */
+#define	CTL_BE_NAME_LEN		32
+
+/*
+ * The ID_REQ flag is used to say that the caller has requested a
+ * particular LUN ID in the req_lun_id field.  If we cannot allocate that
+ * LUN ID, the ctl_add_lun() call will fail.
+ *
+ * The POWERED_OFF flag tells us that the LUN should default to the powered
+ * off state.  It will return 0x04,0x02 until it is powered up.  ("Logical
+ * unit not ready, initializing command required.")
+ *
+ * The INOPERABLE flag tells us that this LUN is not operable for whatever
+ * reason.  This means that user data may have been (or has been?) lost.
+ * We will return 0x31,0x00 ("Medium format corrupted") until the host
+ * issues a FORMAT UNIT command to clear the error.
+ *
+ * The PRIMARY flag tells us that this LUN is registered as a Primary LUN
+ * which is accessible via the Master shelf controller in an HA. This flag
+ * being set indicates a Primary LUN. This flag being reset represents a
+ * Secondary LUN controlled by the Secondary controller in an HA
+ * configuration. Flag is applicable at this time to T_DIRECT types. 
+ *
+ * The SERIAL_NUM flag tells us that the serial_num field is filled in and
+ * valid for use in SCSI INQUIRY VPD page 0x80.
+ *
+ * The DEVID flag tells us that the device_id field is filled in and
+ * valid for use in SCSI INQUIRY VPD page 0x83.
+ *
+ * The DEV_TYPE flag tells us that the device_type field is filled in.
+ */
+typedef enum {
+	CTL_LUN_FLAG_ID_REQ		= 0x01,
+	CTL_LUN_FLAG_POWERED_OFF	= 0x02,
+	CTL_LUN_FLAG_INOPERABLE		= 0x04,
+	CTL_LUN_FLAG_PRIMARY		= 0x08,
+	CTL_LUN_FLAG_SERIAL_NUM		= 0x10,
+	CTL_LUN_FLAG_DEVID		= 0x20,
+	CTL_LUN_FLAG_DEV_TYPE		= 0x40
+} ctl_backend_lun_flags;
+
+#ifdef _KERNEL
+
+#define CTL_BACKEND_DECLARE(name, driver) \
+	static int name ## _modevent(module_t mod, int type, void *data) \
+	{ \
+		switch (type) { \
+		case MOD_LOAD: \
+			ctl_backend_register( \
+				(struct ctl_backend_driver *)data); \
+			break; \
+		case MOD_UNLOAD: \
+			printf(#name " module unload - not possible for this module type\n"); \
+			return EINVAL; \
+		default: \
+			return EOPNOTSUPP; \
+		} \
+		return 0; \
+	} \
+	static moduledata_t name ## _mod = { \
+		#name, \
+		name ## _modevent, \
+		(void *)&driver \
+	}; \
+	DECLARE_MODULE(name, name ## _mod, SI_SUB_CONFIGURE, SI_ORDER_FOURTH); \
+	MODULE_DEPEND(name, ctl, 1, 1, 1); \
+	MODULE_DEPEND(name, cam, 1, 1, 1)
+
+
+typedef enum {
+	CTL_LUN_CONFIG_OK,
+	CTL_LUN_CONFIG_FAILURE
+} ctl_lun_config_status;
+
+typedef void (*be_callback_t)(void *be_lun);
+typedef void (*be_lun_config_t)(void *be_lun,
+				ctl_lun_config_status status);
+
+/*
+ * The lun_type field is the SCSI device type of this particular LUN.  In
+ * general, this should be T_DIRECT, although backends will want to create
+ * a processor LUN, typically at LUN 0.  See scsi_all.h for the defines for
+ * the various SCSI device types.
+ *
+ * The flags are described above.
+ *
+ * The be_lun field is the backend driver's own context that will get
+ * passsed back so that it can tell which LUN CTL is referencing.
+ *
+ * maxlba is the maximum accessible LBA on the LUN.  Note that this is
+ * different from the capacity of the array.  capacity = maxlba + 1
+ *
+ * blocksize is the size, in bytes, of each LBA on the LUN.  In general
+ * this should be 512.  In theory CTL should be able to handle other block
+ * sizes.  Host application software may not deal with it very well, though.
+ *
+ * req_lun_id is the requested LUN ID.  CTL only pays attention to this
+ * field if the CTL_LUN_FLAG_ID_REQ flag is set.  If the requested LUN ID is
+ * not available, the LUN addition will fail.  If a particular LUN ID isn't
+ * requested, the first available LUN ID will be allocated.
+ *
+ * serial_num is the device serial number returned in the SCSI INQUIRY VPD
+ * page 0x80.  This should be a unique, per-shelf value.  The data inside
+ * this field should be ASCII only, left aligned, and any unused space
+ * should be padded out with ASCII spaces.  This field should NOT be NULL
+ * terminated.
+ *
+ * device_id is the T10 device identifier returned in the SCSI INQUIRY VPD
+ * page 0x83.  This should be a unique, per-LUN value.  The data inside
+ * this field should be ASCII only, left aligned, and any unused space
+ * should be padded with ASCII spaces.  This field should NOT be NULL
+ * terminated.
+ *
+ * The lun_shutdown() method is the callback for the ctl_invalidate_lun()
+ * call.  It is called when all outstanding I/O for that LUN has been
+ * completed and CTL has deleted the resources for that LUN.  When the CTL
+ * backend gets this call, it can safely free its per-LUN resources.
+ *
+ * The lun_config_status() method is the callback for the ctl_add_lun()
+ * call.  It is called when the LUN is successfully added, or when LUN
+ * addition fails.  If the LUN is successfully added, the backend may call
+ * the ctl_enable_lun() method to enable the LUN.
+ *
+ * The be field is a pointer to the ctl_backend_driver structure, which
+ * contains the backend methods to be called by CTL.
+ *
+ * The ctl_lun field is for CTL internal use only, and should not be used
+ * by the backend.
+ *
+ * The links field is for CTL internal use only, and should not be used by
+ * the backend.
+ */
+struct ctl_be_lun {
+	uint8_t			lun_type;	/* passed to CTL */
+	ctl_backend_lun_flags	flags;		/* passed to CTL */
+	void			*be_lun;	/* passed to CTL */
+	uint64_t		maxlba;		/* passed to CTL */
+	uint32_t		blocksize;	/* passed to CTL */
+	uint32_t		req_lun_id;	/* passed to CTL */
+	uint32_t		lun_id;		/* returned from CTL */
+	uint8_t			serial_num[CTL_SN_LEN];	 /* passed to CTL */
+	uint8_t			device_id[CTL_DEVID_LEN];/* passed to CTL */
+	be_callback_t		lun_shutdown;	/* passed to CTL */
+	be_lun_config_t		lun_config_status; /* passed to CTL */
+	struct ctl_backend_driver *be;		/* passed to CTL */
+	void			*ctl_lun;	/* used by CTL */
+	STAILQ_ENTRY(ctl_be_lun) links;		/* used by CTL */
+};
+
+typedef enum {
+	CTL_BE_FLAG_NONE	= 0x00,	/* no flags */
+	CTL_BE_FLAG_HAS_CONFIG	= 0x01,	/* can do config reads, writes */
+	CTL_BE_FLAG_INTERNAL	= 0x02	/* don't inc mod refcount */
+} ctl_backend_flags;
+
+typedef int (*be_init_t)(void);
+typedef int (*be_func_t)(union ctl_io *io);
+typedef void (*be_vfunc_t)(union ctl_io *io);
+typedef int (*be_ioctl_t)(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
+			  struct thread *td);
+typedef int (*be_luninfo_t)(void *be_lun, struct sbuf *sb);
+
+struct ctl_backend_driver {
+	char		  name[CTL_BE_NAME_LEN]; /* passed to CTL */
+	ctl_backend_flags flags;	         /* passed to CTL */
+	be_init_t	  init;			 /* passed to CTL */
+	be_func_t	  data_submit;		 /* passed to CTL */
+	be_func_t	  data_move_done;	 /* passed to CTL */
+	be_func_t	  config_read;		 /* passed to CTL */
+	be_func_t	  config_write;		 /* passed to CTL */
+	be_ioctl_t	  ioctl;		 /* passed to CTL */
+	be_luninfo_t	  lun_info;		 /* passed to CTL */
+#ifdef CS_BE_CONFIG_MOVE_DONE_IS_NOT_USED
+	be_func_t	  config_move_done;	 /* passed to backend */
+#endif
+#if 0
+	be_vfunc_t	  config_write_done;	 /* passed to backend */
+#endif
+	u_int		  num_luns;		 /* used by CTL */
+	STAILQ_ENTRY(ctl_backend_driver) links;	 /* used by CTL */
+};
+
+int ctl_backend_register(struct ctl_backend_driver *be);
+int ctl_backend_deregister(struct ctl_backend_driver *be);
+struct ctl_backend_driver *ctl_backend_find(char *backend_name);
+
+/*
+ * To add a LUN, first call ctl_add_lun().  You will get the lun_config_status()
+ * callback when the LUN addition has either succeeded or failed.
+ *
+ * Once you get that callback, you can then call ctl_enable_lun() to enable
+ * the LUN.
+ */
+int ctl_add_lun(struct ctl_be_lun *be_lun);
+int ctl_enable_lun(struct ctl_be_lun *be_lun);
+
+/*
+ * To delete a LUN, first call ctl_disable_lun(), then
+ * ctl_invalidate_lun().  You will get the lun_shutdown() callback when all
+ * I/O to the LUN has completed and the LUN has been deleted.
+ */
+int ctl_disable_lun(struct ctl_be_lun *be_lun);
+int ctl_invalidate_lun(struct ctl_be_lun *be_lun);
+
+/*
+ * To start a LUN (transition from powered off to powered on state) call
+ * ctl_start_lun().  To stop a LUN (transition from powered on to powered
+ * off state) call ctl_stop_lun().
+ */
+int ctl_start_lun(struct ctl_be_lun *be_lun);
+int ctl_stop_lun(struct ctl_be_lun *be_lun);
+
+/*
+ * If a LUN is inoperable, call ctl_lun_inoperable().  Generally the LUN
+ * will become operable once again when the user issues the SCSI FORMAT UNIT
+ * command.  (CTL will automatically clear the inoperable flag.)  If we
+ * need to re-enable the LUN, we can call ctl_lun_operable() to enable it
+ * without a SCSI command.
+ */
+int ctl_lun_inoperable(struct ctl_be_lun *be_lun);
+int ctl_lun_operable(struct ctl_be_lun *be_lun);
+
+/*
+ * If a LUN is locked on or unlocked from a power/APS standpoint, call
+ * ctl_lun_power_lock() to update the current status in CTL's APS subpage.
+ * Set the lock flag to 1 to lock the LUN, set it to 0 to unlock the LUN.
+ */
+int ctl_lun_power_lock(struct ctl_be_lun *be_lun, struct ctl_nexus *nexus,
+		       int lock);
+
+/*
+ * To take a LUN offline, call ctl_lun_offline().  Generally the LUN will
+ * be online again once the user sends a SCSI START STOP UNIT command with
+ * the start and on/offline bits set.  The backend can bring the LUN back
+ * online via the ctl_lun_online() function, if necessary.
+ */
+int ctl_lun_offline(struct ctl_be_lun *be_lun);
+int ctl_lun_online(struct ctl_be_lun *be_lun);
+
+#endif /* _KERNEL */
+#endif /* _CTL_BACKEND_H_ */
+
+/*
+ * vim: ts=8
+ */
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/cam/ctl/ctl_backend_block.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/sys/cam/ctl/ctl_backend_block.c	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,2213 @@
+/*-
+ * Copyright (c) 2003 Silicon Graphics International Corp.
+ * Copyright (c) 2009-2011 Spectra Logic Corporation
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions, and the following disclaimer,
+ *    without modification.
+ * 2. Redistributions in binary form must reproduce at minimum a disclaimer
+ *    substantially similar to the "NO WARRANTY" disclaimer below
+ *    ("Disclaimer") and any redistribution must be conditioned upon
+ *    including a substantially similar Disclaimer requirement for further
+ *    binary redistribution.
+ *
+ * NO WARRANTY
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGES.
+ *
+ * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_backend_block.c#5 $
+ */
+/*
+ * CAM Target Layer driver backend for block devices.
+ *
+ * Author: Ken Merry <ken at FreeBSD.org>
+ */
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD: head/sys/cam/ctl/ctl_backend_block.c 229997 2012-01-12 00:34:33Z ken $");
+
+#include <opt_kdtrace.h>
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/kernel.h>
+#include <sys/types.h>
+#include <sys/kthread.h>
+#include <sys/bio.h>
+#include <sys/fcntl.h>
+#include <sys/lock.h>
+#include <sys/mutex.h>
+#include <sys/condvar.h>
+#include <sys/malloc.h>
+#include <sys/conf.h>
+#include <sys/ioccom.h>
+#include <sys/queue.h>
+#include <sys/sbuf.h>
+#include <sys/endian.h>
+#include <sys/uio.h>
+#include <sys/buf.h>
+#include <sys/taskqueue.h>
+#include <sys/vnode.h>
+#include <sys/namei.h>
+#include <sys/mount.h>
+#include <sys/disk.h>
+#include <sys/fcntl.h>
+#include <sys/filedesc.h>
+#include <sys/proc.h>
+#include <sys/pcpu.h>
+#include <sys/module.h>
+#include <sys/sdt.h>
+#include <sys/devicestat.h>
+#include <sys/sysctl.h>
+
+#include <geom/geom.h>
+
+#include <cam/cam.h>
+#include <cam/scsi/scsi_all.h>
+#include <cam/scsi/scsi_da.h>
+#include <cam/ctl/ctl_io.h>
+#include <cam/ctl/ctl.h>
+#include <cam/ctl/ctl_backend.h>
+#include <cam/ctl/ctl_frontend_internal.h>
+#include <cam/ctl/ctl_ioctl.h>
+#include <cam/ctl/ctl_scsi_all.h>
+#include <cam/ctl/ctl_error.h>
+
+/*
+ * The idea here is that we'll allocate enough S/G space to hold a 16MB
+ * I/O.  If we get an I/O larger than that, we'll reject it.
+ */
+#define	CTLBLK_MAX_IO_SIZE	(16 * 1024 * 1024)
+#define	CTLBLK_MAX_SEGS		(CTLBLK_MAX_IO_SIZE / MAXPHYS) + 1
+
+#ifdef CTLBLK_DEBUG
+#define DPRINTF(fmt, args...) \
+    printf("cbb(%s:%d): " fmt, __FUNCTION__, __LINE__, ##args)
+#else
+#define DPRINTF(fmt, args...) do {} while(0)
+#endif
+
+SDT_PROVIDER_DEFINE(cbb);
+
+typedef enum {
+	CTL_BE_BLOCK_LUN_UNCONFIGURED	= 0x01,
+	CTL_BE_BLOCK_LUN_CONFIG_ERR	= 0x02,
+	CTL_BE_BLOCK_LUN_WAITING	= 0x04,
+	CTL_BE_BLOCK_LUN_MULTI_THREAD	= 0x08
+} ctl_be_block_lun_flags;
+
+typedef enum {
+	CTL_BE_BLOCK_NONE,
+	CTL_BE_BLOCK_DEV,
+	CTL_BE_BLOCK_FILE
+} ctl_be_block_type;
+
+struct ctl_be_block_devdata {
+	struct cdev *cdev;
+	struct cdevsw *csw;
+	int dev_ref;
+};
+
+struct ctl_be_block_filedata {
+	struct ucred *cred;
+};
+
+union ctl_be_block_bedata {
+	struct ctl_be_block_devdata dev;
+	struct ctl_be_block_filedata file;
+};
+
+struct ctl_be_block_io;
+struct ctl_be_block_lun;
+
+typedef void (*cbb_dispatch_t)(struct ctl_be_block_lun *be_lun,
+			       struct ctl_be_block_io *beio);
+
+/*
+ * Backend LUN structure.  There is a 1:1 mapping between a block device
+ * and a backend block LUN, and between a backend block LUN and a CTL LUN.
+ */
+struct ctl_be_block_lun {
+	struct ctl_block_disk *disk;
+	char lunname[32];
+	char *dev_path;
+	ctl_be_block_type dev_type;
+	struct vnode *vn;
+	union ctl_be_block_bedata backend;
+	cbb_dispatch_t dispatch;
+	cbb_dispatch_t lun_flush;
+	struct mtx lock;
+	uma_zone_t lun_zone;
+	uint64_t size_blocks;
+	uint64_t size_bytes;
+	uint32_t blocksize;
+	int blocksize_shift;
+	struct ctl_be_block_softc *softc;
+	struct devstat *disk_stats;
+	ctl_be_block_lun_flags flags;
+	STAILQ_ENTRY(ctl_be_block_lun) links;
+	struct ctl_be_lun ctl_be_lun;
+	struct taskqueue *io_taskqueue;
+	struct task io_task;
+	int num_threads;
+	STAILQ_HEAD(, ctl_io_hdr) input_queue;
+	STAILQ_HEAD(, ctl_io_hdr) config_write_queue;
+	STAILQ_HEAD(, ctl_io_hdr) datamove_queue;
+};
+
+/*
+ * Overall softc structure for the block backend module.
+ */
+struct ctl_be_block_softc {
+	STAILQ_HEAD(, ctl_be_block_io)   beio_free_queue;
+	struct mtx			 lock;
+	int				 prealloc_beio;
+	int				 num_disks;
+	STAILQ_HEAD(, ctl_block_disk)	 disk_list;
+	int				 num_luns;
+	STAILQ_HEAD(, ctl_be_block_lun)	 lun_list;
+};
+
+static struct ctl_be_block_softc backend_block_softc;
+
+/*
+ * Per-I/O information.
+ */
+struct ctl_be_block_io {
+	union ctl_io			*io;
+	struct ctl_sg_entry		sg_segs[CTLBLK_MAX_SEGS];
+	struct iovec			xiovecs[CTLBLK_MAX_SEGS];
+	int				bio_cmd;
+	int				bio_flags;
+	int				num_segs;
+	int				num_bios_sent;
+	int				num_bios_done;
+	int				send_complete;
+	int				num_errors;
+	struct bintime			ds_t0;
+	devstat_tag_type		ds_tag_type;
+	devstat_trans_flags		ds_trans_type;
+	uint64_t			io_len;
+	uint64_t			io_offset;
+	struct ctl_be_block_softc	*softc;
+	struct ctl_be_block_lun		*lun;
+	STAILQ_ENTRY(ctl_be_block_io)	links;
+};
+
+static int cbb_num_threads = 14;
+TUNABLE_INT("kern.cam.ctl.block.num_threads", &cbb_num_threads);
+SYSCTL_NODE(_kern_cam_ctl, OID_AUTO, block, CTLFLAG_RD, 0,
+	    "CAM Target Layer Block Backend");
+SYSCTL_INT(_kern_cam_ctl_block, OID_AUTO, num_threads, CTLFLAG_RW,
+           &cbb_num_threads, 0, "Number of threads per backing file");
+
+static struct ctl_be_block_io *ctl_alloc_beio(struct ctl_be_block_softc *softc);
+static void ctl_free_beio(struct ctl_be_block_io *beio);
+static int ctl_grow_beio(struct ctl_be_block_softc *softc, int count);
+#if 0
+static void ctl_shrink_beio(struct ctl_be_block_softc *softc);
+#endif
+static void ctl_complete_beio(struct ctl_be_block_io *beio);
+static int ctl_be_block_move_done(union ctl_io *io);
+static void ctl_be_block_biodone(struct bio *bio);
+static void ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun,
+				    struct ctl_be_block_io *beio);
+static void ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun,
+				       struct ctl_be_block_io *beio);
+static void ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun,
+				   struct ctl_be_block_io *beio);
+static void ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun,
+				      struct ctl_be_block_io *beio);
+static void ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun,
+				    union ctl_io *io);
+static void ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun,
+				  union ctl_io *io);
+static void ctl_be_block_worker(void *context, int pending);
+static int ctl_be_block_submit(union ctl_io *io);
+static int ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
+				   int flag, struct thread *td);
+static int ctl_be_block_open_file(struct ctl_be_block_lun *be_lun,
+				  struct ctl_lun_req *req);
+static int ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun,
+				 struct ctl_lun_req *req);
+static int ctl_be_block_close(struct ctl_be_block_lun *be_lun);
+static int ctl_be_block_open(struct ctl_be_block_softc *softc,
+			     struct ctl_be_block_lun *be_lun,
+			     struct ctl_lun_req *req);
+static int ctl_be_block_create(struct ctl_be_block_softc *softc,
+			       struct ctl_lun_req *req);
+static int ctl_be_block_rm(struct ctl_be_block_softc *softc,
+			   struct ctl_lun_req *req);
+static void ctl_be_block_lun_shutdown(void *be_lun);
+static void ctl_be_block_lun_config_status(void *be_lun,
+					   ctl_lun_config_status status);
+static int ctl_be_block_config_write(union ctl_io *io);
+static int ctl_be_block_config_read(union ctl_io *io);
+static int ctl_be_block_lun_info(void *be_lun, struct sbuf *sb);
+int ctl_be_block_init(void);
+
+static struct ctl_backend_driver ctl_be_block_driver = 
+{
+	name: "block",
+	flags: CTL_BE_FLAG_HAS_CONFIG,
+	init: ctl_be_block_init,
+	data_submit: ctl_be_block_submit,
+	data_move_done: ctl_be_block_move_done,
+	config_read: ctl_be_block_config_read,
+	config_write: ctl_be_block_config_write,
+	ioctl: ctl_be_block_ioctl,
+	lun_info: ctl_be_block_lun_info
+};
+
+MALLOC_DEFINE(M_CTLBLK, "ctlblk", "Memory used for CTL block backend");
+CTL_BACKEND_DECLARE(cbb, ctl_be_block_driver);
+
+static struct ctl_be_block_io *
+ctl_alloc_beio(struct ctl_be_block_softc *softc)
+{
+	struct ctl_be_block_io *beio;
+	int count;
+
+	mtx_lock(&softc->lock);
+
+	beio = STAILQ_FIRST(&softc->beio_free_queue);
+	if (beio != NULL) {
+		STAILQ_REMOVE(&softc->beio_free_queue, beio,
+			      ctl_be_block_io, links);
+	}
+	mtx_unlock(&softc->lock);
+
+	if (beio != NULL) {
+		bzero(beio, sizeof(*beio));
+		beio->softc = softc;
+		return (beio);
+	}
+
+	for (;;) {
+
+		count = ctl_grow_beio(softc, /*count*/ 10);
+
+		/*
+		 * This shouldn't be possible, since ctl_grow_beio() uses a
+		 * blocking malloc.
+		 */
+		if (count == 0)
+			return (NULL);
+
+		/*
+		 * Since we have to drop the lock when we're allocating beio
+		 * structures, it's possible someone else can come along and
+		 * allocate the beio's we've just allocated.
+		 */
+		mtx_lock(&softc->lock);
+		beio = STAILQ_FIRST(&softc->beio_free_queue);
+		if (beio != NULL) {
+			STAILQ_REMOVE(&softc->beio_free_queue, beio,
+				      ctl_be_block_io, links);
+		}
+		mtx_unlock(&softc->lock);
+
+		if (beio != NULL) {
+			bzero(beio, sizeof(*beio));
+			beio->softc = softc;
+			break;
+		}
+	}
+	return (beio);
+}
+
+static void
+ctl_free_beio(struct ctl_be_block_io *beio)
+{
+	struct ctl_be_block_softc *softc;
+	int duplicate_free;
+	int i;
+
+	softc = beio->softc;
+	duplicate_free = 0;
+
+	for (i = 0; i < beio->num_segs; i++) {
+		if (beio->sg_segs[i].addr == NULL)
+			duplicate_free++;
+
+		uma_zfree(beio->lun->lun_zone, beio->sg_segs[i].addr);
+		beio->sg_segs[i].addr = NULL;
+	}
+
+	if (duplicate_free > 0) {
+		printf("%s: %d duplicate frees out of %d segments\n", __func__,
+		       duplicate_free, beio->num_segs);
+	}
+	mtx_lock(&softc->lock);
+	STAILQ_INSERT_TAIL(&softc->beio_free_queue, beio, links);
+	mtx_unlock(&softc->lock);
+}
+
+static int
+ctl_grow_beio(struct ctl_be_block_softc *softc, int count)
+{
+	int i;
+
+	for (i = 0; i < count; i++) {
+		struct ctl_be_block_io *beio;
+
+		beio = (struct ctl_be_block_io *)malloc(sizeof(*beio),
+							   M_CTLBLK,
+							   M_WAITOK | M_ZERO);
+		if (beio == NULL)
+			break;
+
+		bzero(beio, sizeof(*beio));
+		beio->softc = softc;
+		mtx_lock(&softc->lock);
+		STAILQ_INSERT_TAIL(&softc->beio_free_queue, beio, links);
+		mtx_unlock(&softc->lock);
+	}
+
+	return (i);
+}
+
+#if 0
+static void
+ctl_shrink_beio(struct ctl_be_block_softc *softc)
+{
+	struct ctl_be_block_io *beio, *beio_tmp;
+
+	mtx_lock(&softc->lock);
+	STAILQ_FOREACH_SAFE(beio, &softc->beio_free_queue, links, beio_tmp) {
+		STAILQ_REMOVE(&softc->beio_free_queue, beio,
+			      ctl_be_block_io, links);
+		free(beio, M_CTLBLK);
+	}
+	mtx_unlock(&softc->lock);
+}
+#endif
+
+static void
+ctl_complete_beio(struct ctl_be_block_io *beio)
+{
+	union ctl_io *io;
+	int io_len;
+
+	io = beio->io;
+
+	if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)
+		io_len = beio->io_len;
+	else
+		io_len = 0;
+
+	devstat_end_transaction(beio->lun->disk_stats,
+				/*bytes*/ io_len,
+				beio->ds_tag_type,
+				beio->ds_trans_type,
+				/*now*/ NULL,
+				/*then*/&beio->ds_t0);
+
+	ctl_free_beio(beio);
+	ctl_done(io);
+}
+
+static int
+ctl_be_block_move_done(union ctl_io *io)
+{
+	struct ctl_be_block_io *beio;
+	struct ctl_be_block_lun *be_lun;
+#ifdef CTL_TIME_IO
+	struct bintime cur_bt;
+#endif  
+
+	beio = (struct ctl_be_block_io *)
+		io->io_hdr.ctl_private[CTL_PRIV_BACKEND].ptr;
+
+	be_lun = beio->lun;
+
+	DPRINTF("entered\n");
+
+#ifdef CTL_TIME_IO
+	getbintime(&cur_bt);
+	bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
+	bintime_add(&io->io_hdr.dma_bt, &cur_bt);
+	io->io_hdr.num_dmas++;
+#endif  
+
+	/*
+	 * We set status at this point for read commands, and write
+	 * commands with errors.
+	 */
+	if ((beio->bio_cmd == BIO_READ)
+	 && (io->io_hdr.port_status == 0)
+	 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
+	 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE))
+		ctl_set_success(&io->scsiio);
+	else if ((io->io_hdr.port_status != 0)
+	      && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
+	      && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) {
+		/*
+		 * For hardware error sense keys, the sense key
+		 * specific value is defined to be a retry count,
+		 * but we use it to pass back an internal FETD
+		 * error code.  XXX KDM  Hopefully the FETD is only
+		 * using 16 bits for an error code, since that's
+		 * all the space we have in the sks field.
+		 */
+		ctl_set_internal_failure(&io->scsiio,
+					 /*sks_valid*/ 1,
+					 /*retry_count*/
+					 io->io_hdr.port_status);
+	}
+
+	/*
+	 * If this is a read, or a write with errors, it is done.
+	 */
+	if ((beio->bio_cmd == BIO_READ)
+	 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)
+	 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) {
+		ctl_complete_beio(beio);
+		return (0);
+	}
+
+	/*
+	 * At this point, we have a write and the DMA completed
+	 * successfully.  We now have to queue it to the task queue to
+	 * execute the backend I/O.  That is because we do blocking
+	 * memory allocations, and in the file backing case, blocking I/O.
+	 * This move done routine is generally called in the SIM's
+	 * interrupt context, and therefore we cannot block.
+	 */
+	mtx_lock(&be_lun->lock);
+	/*
+	 * XXX KDM make sure that links is okay to use at this point.
+	 * Otherwise, we either need to add another field to ctl_io_hdr,
+	 * or deal with resource allocation here.
+	 */
+	STAILQ_INSERT_TAIL(&be_lun->datamove_queue, &io->io_hdr, links);
+	mtx_unlock(&be_lun->lock);
+
+	taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
+
+	return (0);
+}
+
+static void
+ctl_be_block_biodone(struct bio *bio)
+{
+	struct ctl_be_block_io *beio;
+	struct ctl_be_block_lun *be_lun;
+	union ctl_io *io;
+
+	beio = bio->bio_caller1;
+	be_lun = beio->lun;
+	io = beio->io;
+
+	DPRINTF("entered\n");
+
+	mtx_lock(&be_lun->lock);
+	if (bio->bio_error != 0)
+		beio->num_errors++;
+
+	beio->num_bios_done++;
+
+	/*
+	 * XXX KDM will this cause WITNESS to complain?  Holding a lock
+	 * during the free might cause it to complain.
+	 */
+	g_destroy_bio(bio);
+
+	/*
+	 * If the send complete bit isn't set, or we aren't the last I/O to
+	 * complete, then we're done.
+	 */
+	if ((beio->send_complete == 0)
+	 || (beio->num_bios_done < beio->num_bios_sent)) {
+		mtx_unlock(&be_lun->lock);
+		return;
+	}
+
+	/*
+	 * At this point, we've verified that we are the last I/O to
+	 * complete, so it's safe to drop the lock.
+	 */
+	mtx_unlock(&be_lun->lock);
+
+	/*
+	 * If there are any errors from the backing device, we fail the
+	 * entire I/O with a medium error.
+	 */
+	if (beio->num_errors > 0) {
+		if (beio->bio_cmd == BIO_FLUSH) {
+			/* XXX KDM is there is a better error here? */
+			ctl_set_internal_failure(&io->scsiio,
+						 /*sks_valid*/ 1,
+						 /*retry_count*/ 0xbad2);
+		} else
+			ctl_set_medium_error(&io->scsiio);
+		ctl_complete_beio(beio);
+		return;
+	}
+
+	/*
+	 * If this is a write or a flush, we're all done.
+	 * If this is a read, we can now send the data to the user.
+	 */
+	if ((beio->bio_cmd == BIO_WRITE)
+	 || (beio->bio_cmd == BIO_FLUSH)) {
+		ctl_set_success(&io->scsiio);
+		ctl_complete_beio(beio);
+	} else {
+		io->scsiio.be_move_done = ctl_be_block_move_done;
+		io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs;
+		io->scsiio.kern_data_len = beio->io_len;
+		io->scsiio.kern_total_len = beio->io_len;
+		io->scsiio.kern_rel_offset = 0;
+		io->scsiio.kern_data_resid = 0;
+		io->scsiio.kern_sg_entries = beio->num_segs;
+		io->io_hdr.flags |= CTL_FLAG_ALLOCATED | CTL_FLAG_KDPTR_SGLIST;
+#ifdef CTL_TIME_IO
+        	getbintime(&io->io_hdr.dma_start_bt);
+#endif  
+		ctl_datamove(io);
+	}
+}
+
+static void
+ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun,
+			struct ctl_be_block_io *beio)
+{
+	union ctl_io *io;
+	struct mount *mountpoint;
+	int vfs_is_locked, error, lock_flags;
+
+	DPRINTF("entered\n");
+
+	io = beio->io;
+
+	vfs_is_locked = VFS_LOCK_GIANT(be_lun->vn->v_mount);
+
+       	(void) vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
+
+	if (MNT_SHARED_WRITES(mountpoint)
+	 || ((mountpoint == NULL)
+	  && MNT_SHARED_WRITES(be_lun->vn->v_mount)))
+		lock_flags = LK_SHARED;
+	else
+		lock_flags = LK_EXCLUSIVE;
+
+	vn_lock(be_lun->vn, lock_flags | LK_RETRY);
+
+	binuptime(&beio->ds_t0);
+	devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
+
+	error = VOP_FSYNC(be_lun->vn, MNT_WAIT, curthread);
+	VOP_UNLOCK(be_lun->vn, 0);
+
+	vn_finished_write(mountpoint);
+
+	VFS_UNLOCK_GIANT(vfs_is_locked);
+
+	if (error == 0)
+		ctl_set_success(&io->scsiio);
+	else {
+		/* XXX KDM is there is a better error here? */
+		ctl_set_internal_failure(&io->scsiio,
+					 /*sks_valid*/ 1,
+					 /*retry_count*/ 0xbad1);
+	}
+
+	ctl_complete_beio(beio);
+}
+
+SDT_PROBE_DEFINE1(cbb, kernel, read, file_start, file_start, "uint64_t");
+SDT_PROBE_DEFINE1(cbb, kernel, write, file_start, file_start, "uint64_t");
+SDT_PROBE_DEFINE1(cbb, kernel, read, file_done, file_done,"uint64_t");
+SDT_PROBE_DEFINE1(cbb, kernel, write, file_done, file_done, "uint64_t");
+
+static void
+ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun,
+			   struct ctl_be_block_io *beio)
+{
+	struct ctl_be_block_filedata *file_data;
+	union ctl_io *io;
+	struct uio xuio;
+	struct iovec *xiovec;
+	int vfs_is_locked, flags;
+	int error, i;
+
+	DPRINTF("entered\n");
+
+	file_data = &be_lun->backend.file;
+	io = beio->io;
+	flags = beio->bio_flags;
+
+	if (beio->bio_cmd == BIO_READ) {
+		SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0);
+	} else {
+		SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0);
+	}
+
+	bzero(&xuio, sizeof(xuio));
+	if (beio->bio_cmd == BIO_READ)
+		xuio.uio_rw = UIO_READ;
+	else
+		xuio.uio_rw = UIO_WRITE;
+
+	xuio.uio_offset = beio->io_offset;
+	xuio.uio_resid = beio->io_len;
+	xuio.uio_segflg = UIO_SYSSPACE;
+	xuio.uio_iov = beio->xiovecs;
+	xuio.uio_iovcnt = beio->num_segs;
+	xuio.uio_td = curthread;
+
+	for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) {
+		xiovec->iov_base = beio->sg_segs[i].addr;
+		xiovec->iov_len = beio->sg_segs[i].len;
+	}
+
+	vfs_is_locked = VFS_LOCK_GIANT(be_lun->vn->v_mount);
+	if (beio->bio_cmd == BIO_READ) {
+		vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
+
+		binuptime(&beio->ds_t0);
+		devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
+
+		/*
+		 * UFS pays attention to IO_DIRECT for reads.  If the
+		 * DIRECTIO option is configured into the kernel, it calls
+		 * ffs_rawread().  But that only works for single-segment
+		 * uios with user space addresses.  In our case, with a
+		 * kernel uio, it still reads into the buffer cache, but it
+		 * will just try to release the buffer from the cache later
+		 * on in ffs_read().
+		 *
+		 * ZFS does not pay attention to IO_DIRECT for reads.
+		 *
+		 * UFS does not pay attention to IO_SYNC for reads.
+		 *
+		 * ZFS pays attention to IO_SYNC (which translates into the
+		 * Solaris define FRSYNC for zfs_read()) for reads.  It
+		 * attempts to sync the file before reading.
+		 *
+		 * So, to attempt to provide some barrier semantics in the
+		 * BIO_ORDERED case, set both IO_DIRECT and IO_SYNC.
+		 */
+		error = VOP_READ(be_lun->vn, &xuio, (flags & BIO_ORDERED) ?
+				 (IO_DIRECT|IO_SYNC) : 0, file_data->cred);
+
+		VOP_UNLOCK(be_lun->vn, 0);
+	} else {
+		struct mount *mountpoint;
+		int lock_flags;
+
+		(void)vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
+
+		if (MNT_SHARED_WRITES(mountpoint)
+		 || ((mountpoint == NULL)
+		  && MNT_SHARED_WRITES(be_lun->vn->v_mount)))
+			lock_flags = LK_SHARED;
+		else
+			lock_flags = LK_EXCLUSIVE;
+
+		vn_lock(be_lun->vn, lock_flags | LK_RETRY);
+
+		binuptime(&beio->ds_t0);
+		devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
+
+		/*
+		 * UFS pays attention to IO_DIRECT for writes.  The write
+		 * is done asynchronously.  (Normally the write would just
+		 * get put into cache.
+		 *
+		 * UFS pays attention to IO_SYNC for writes.  It will
+		 * attempt to write the buffer out synchronously if that
+		 * flag is set.
+		 *
+		 * ZFS does not pay attention to IO_DIRECT for writes.
+		 *
+		 * ZFS pays attention to IO_SYNC (a.k.a. FSYNC or FRSYNC)
+		 * for writes.  It will flush the transaction from the
+		 * cache before returning.
+		 *
+		 * So if we've got the BIO_ORDERED flag set, we want
+		 * IO_SYNC in either the UFS or ZFS case.
+		 */
+		error = VOP_WRITE(be_lun->vn, &xuio, (flags & BIO_ORDERED) ?
+				  IO_SYNC : 0, file_data->cred);
+		VOP_UNLOCK(be_lun->vn, 0);
+
+		vn_finished_write(mountpoint);
+        }
+        VFS_UNLOCK_GIANT(vfs_is_locked);
+
+	/*
+	 * If we got an error, set the sense data to "MEDIUM ERROR" and
+	 * return the I/O to the user.
+	 */
+	if (error != 0) {
+		char path_str[32];
+
+		ctl_scsi_path_string(io, path_str, sizeof(path_str));
+		/*
+		 * XXX KDM ZFS returns ENOSPC when the underlying
+		 * filesystem fills up.  What kind of SCSI error should we
+		 * return for that?
+		 */
+		printf("%s%s command returned errno %d\n", path_str,
+		       (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", error);
+		ctl_set_medium_error(&io->scsiio);
+		ctl_complete_beio(beio);
+		return;
+	}
+
+	/*
+	 * If this is a write, we're all done.
+	 * If this is a read, we can now send the data to the user.
+	 */
+	if (beio->bio_cmd == BIO_WRITE) {
+		ctl_set_success(&io->scsiio);
+		SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0);
+		ctl_complete_beio(beio);
+	} else {
+		SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0);
+		io->scsiio.be_move_done = ctl_be_block_move_done;
+		io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs;
+		io->scsiio.kern_data_len = beio->io_len;
+		io->scsiio.kern_total_len = beio->io_len;
+		io->scsiio.kern_rel_offset = 0;
+		io->scsiio.kern_data_resid = 0;
+		io->scsiio.kern_sg_entries = beio->num_segs;
+		io->io_hdr.flags |= CTL_FLAG_ALLOCATED | CTL_FLAG_KDPTR_SGLIST;
+#ifdef CTL_TIME_IO
+        	getbintime(&io->io_hdr.dma_start_bt);
+#endif  
+		ctl_datamove(io);
+	}
+}
+
+static void
+ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun,
+		       struct ctl_be_block_io *beio)
+{
+	struct bio *bio;
+	union ctl_io *io;
+	struct ctl_be_block_devdata *dev_data;
+
+	dev_data = &be_lun->backend.dev;
+	io = beio->io;
+
+	DPRINTF("entered\n");
+
+	/* This can't fail, it's a blocking allocation. */
+	bio = g_alloc_bio();
+
+	bio->bio_cmd	    = BIO_FLUSH;
+	bio->bio_flags	   |= BIO_ORDERED;
+	bio->bio_dev	    = dev_data->cdev;
+	bio->bio_offset	    = 0;
+	bio->bio_data	    = 0;
+	bio->bio_done	    = ctl_be_block_biodone;
+	bio->bio_caller1    = beio;
+	bio->bio_pblkno	    = 0;
+
+	/*
+	 * We don't need to acquire the LUN lock here, because we are only
+	 * sending one bio, and so there is no other context to synchronize
+	 * with.
+	 */
+	beio->num_bios_sent = 1;
+	beio->send_complete = 1;
+
+	binuptime(&beio->ds_t0);
+	devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
+
+	(*dev_data->csw->d_strategy)(bio);
+}
+
+static void
+ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun,
+			  struct ctl_be_block_io *beio)
+{
+	int i;
+	struct bio *bio;
+	struct ctl_be_block_devdata *dev_data;
+	off_t cur_offset;
+	int max_iosize;
+
+	DPRINTF("entered\n");
+
+	dev_data = &be_lun->backend.dev;
+
+	/*
+	 * We have to limit our I/O size to the maximum supported by the
+	 * backend device.  Hopefully it is MAXPHYS.  If the driver doesn't
+	 * set it properly, use DFLTPHYS.
+	 */
+	max_iosize = dev_data->cdev->si_iosize_max;
+	if (max_iosize < PAGE_SIZE)
+		max_iosize = DFLTPHYS;
+
+	cur_offset = beio->io_offset;
+
+	/*
+	 * XXX KDM need to accurately reflect the number of I/Os outstanding
+	 * to a device.
+	 */
+	binuptime(&beio->ds_t0);
+	devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
+
+	for (i = 0; i < beio->num_segs; i++) {
+		size_t cur_size;
+		uint8_t *cur_ptr;
+
+		cur_size = beio->sg_segs[i].len;
+		cur_ptr = beio->sg_segs[i].addr;
+
+		while (cur_size > 0) {
+			/* This can't fail, it's a blocking allocation. */
+			bio = g_alloc_bio();
+
+			KASSERT(bio != NULL, ("g_alloc_bio() failed!\n"));
+
+			bio->bio_cmd = beio->bio_cmd;
+			bio->bio_flags |= beio->bio_flags;
+			bio->bio_dev = dev_data->cdev;
+			bio->bio_caller1 = beio;
+			bio->bio_length = min(cur_size, max_iosize);
+			bio->bio_offset = cur_offset;
+			bio->bio_data = cur_ptr;
+			bio->bio_done = ctl_be_block_biodone;
+			bio->bio_pblkno = cur_offset / be_lun->blocksize;
+
+			cur_offset += bio->bio_length;
+			cur_ptr += bio->bio_length;
+			cur_size -= bio->bio_length;
+
+			/*
+			 * Make sure we set the complete bit just before we
+			 * issue the last bio so we don't wind up with a
+			 * race.
+			 *
+			 * Use the LUN mutex here instead of a combination
+			 * of atomic variables for simplicity.
+			 *
+			 * XXX KDM we could have a per-IO lock, but that
+			 * would cause additional per-IO setup and teardown
+			 * overhead.  Hopefully there won't be too much
+			 * contention on the LUN lock.
+			 */
+			mtx_lock(&be_lun->lock);
+
+			beio->num_bios_sent++;
+
+			if ((i == beio->num_segs - 1)
+			 && (cur_size == 0))
+				beio->send_complete = 1;
+
+			mtx_unlock(&be_lun->lock);
+
+			(*dev_data->csw->d_strategy)(bio);
+		}
+	}
+}
+
+static void
+ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun,
+			 union ctl_io *io)
+{
+	struct ctl_be_block_io *beio;
+	struct ctl_be_block_softc *softc;
+
+	DPRINTF("entered\n");
+
+	softc = be_lun->softc;
+	beio = ctl_alloc_beio(softc);
+	if (beio == NULL) {
+		/*
+		 * This should not happen.  ctl_alloc_beio() will call
+		 * ctl_grow_beio() with a blocking malloc as needed.
+		 * A malloc with M_WAITOK should not fail.
+		 */
+		ctl_set_busy(&io->scsiio);
+		ctl_done(io);
+		return;
+	}
+
+	beio->io = io;
+	beio->softc = softc;
+	beio->lun = be_lun;
+	io->io_hdr.ctl_private[CTL_PRIV_BACKEND].ptr = beio;
+
+	switch (io->scsiio.cdb[0]) {
+	case SYNCHRONIZE_CACHE:
+	case SYNCHRONIZE_CACHE_16:
+		beio->ds_trans_type = DEVSTAT_NO_DATA;
+		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
+		beio->io_len = 0;
+		be_lun->lun_flush(be_lun, beio);
+		break;
+	default:
+		panic("Unhandled CDB type %#x", io->scsiio.cdb[0]);
+		break;
+	}
+}
+
+SDT_PROBE_DEFINE1(cbb, kernel, read, start, start, "uint64_t");
+SDT_PROBE_DEFINE1(cbb, kernel, write, start, start, "uint64_t");
+SDT_PROBE_DEFINE1(cbb, kernel, read, alloc_done, alloc_done, "uint64_t");
+SDT_PROBE_DEFINE1(cbb, kernel, write, alloc_done, alloc_done, "uint64_t");
+
+static void
+ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun,
+			   union ctl_io *io)
+{
+	struct ctl_be_block_io *beio;
+	struct ctl_be_block_softc *softc;
+	struct ctl_lba_len lbalen;
+	uint64_t len_left, io_size_bytes;
+	int i;
+
+	softc = be_lun->softc;
+
+	DPRINTF("entered\n");
+
+	if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) {
+		SDT_PROBE(cbb, kernel, read, start, 0, 0, 0, 0, 0);
+	} else {
+		SDT_PROBE(cbb, kernel, write, start, 0, 0, 0, 0, 0);
+	}
+
+	memcpy(&lbalen, io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
+	       sizeof(lbalen));
+
+	io_size_bytes = lbalen.len * be_lun->blocksize;
+
+	/*
+	 * XXX KDM this is temporary, until we implement chaining of beio
+	 * structures and multiple datamove calls to move all the data in
+	 * or out.
+	 */
+	if (io_size_bytes > CTLBLK_MAX_IO_SIZE) {
+		printf("%s: IO length %ju > max io size %u\n", __func__,
+		       io_size_bytes, CTLBLK_MAX_IO_SIZE);
+		ctl_set_invalid_field(&io->scsiio,
+				      /*sks_valid*/ 0,
+				      /*command*/ 1,
+				      /*field*/ 0,
+				      /*bit_valid*/ 0,
+				      /*bit*/ 0);
+		ctl_done(io);
+		return;
+	}
+
+	beio = ctl_alloc_beio(softc);
+	if (beio == NULL) {
+		/*
+		 * This should not happen.  ctl_alloc_beio() will call
+		 * ctl_grow_beio() with a blocking malloc as needed.
+		 * A malloc with M_WAITOK should not fail.
+		 */
+		ctl_set_busy(&io->scsiio);
+		ctl_done(io);
+		return;
+	}
+
+	beio->io = io;
+	beio->softc = softc;
+	beio->lun = be_lun;
+	io->io_hdr.ctl_private[CTL_PRIV_BACKEND].ptr = beio;
+
+	/*
+	 * If the I/O came down with an ordered or head of queue tag, set
+	 * the BIO_ORDERED attribute.  For head of queue tags, that's
+	 * pretty much the best we can do.
+	 *
+	 * XXX KDM we don't have a great way to easily know about the FUA
+	 * bit right now (it is decoded in ctl_read_write(), but we don't
+	 * pass that knowledge to the backend), and in any case we would
+	 * need to determine how to handle it.  
+	 */
+	if ((io->scsiio.tag_type == CTL_TAG_ORDERED)
+	 || (io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE))
+		beio->bio_flags = BIO_ORDERED;
+
+	switch (io->scsiio.tag_type) {
+	case CTL_TAG_ORDERED:
+		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
+		break;
+	case CTL_TAG_HEAD_OF_QUEUE:
+		beio->ds_tag_type = DEVSTAT_TAG_HEAD;
+		break;
+	case CTL_TAG_UNTAGGED:
+	case CTL_TAG_SIMPLE:
+	case CTL_TAG_ACA:
+	default:
+		beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
+		break;
+	}
+
+	/*
+	 * This path handles read and write only.  The config write path
+	 * handles flush operations.
+	 */
+	if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) {
+		beio->bio_cmd = BIO_READ;
+		beio->ds_trans_type = DEVSTAT_READ;
+	} else {
+		beio->bio_cmd = BIO_WRITE;
+		beio->ds_trans_type = DEVSTAT_WRITE;
+	}
+
+	beio->io_len = lbalen.len * be_lun->blocksize;
+	beio->io_offset = lbalen.lba * be_lun->blocksize;
+
+	DPRINTF("%s at LBA %jx len %u\n",
+	       (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE",
+	       (uintmax_t)lbalen.lba, lbalen.len);
+
+	for (i = 0, len_left = io_size_bytes; i < CTLBLK_MAX_SEGS &&
+	     len_left > 0; i++) {
+
+		/*
+		 * Setup the S/G entry for this chunk.
+		 */
+		beio->sg_segs[i].len = min(MAXPHYS, len_left);
+		beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK);
+		/*
+		 * uma_zalloc() can in theory return NULL even with M_WAITOK
+		 * if it can't pull more memory into the zone.
+		 */
+		if (beio->sg_segs[i].addr == NULL) {
+			ctl_set_busy(&io->scsiio);
+			ctl_complete_beio(beio);
+			return;
+		}
+
+		DPRINTF("segment %d addr %p len %zd\n", i,
+			beio->sg_segs[i].addr, beio->sg_segs[i].len);
+
+		beio->num_segs++;
+		len_left -= beio->sg_segs[i].len;
+	}
+
+	/*
+	 * For the read case, we need to read the data into our buffers and
+	 * then we can send it back to the user.  For the write case, we
+	 * need to get the data from the user first.
+	 */
+	if (beio->bio_cmd == BIO_READ) {
+		SDT_PROBE(cbb, kernel, read, alloc_done, 0, 0, 0, 0, 0);
+		be_lun->dispatch(be_lun, beio);
+	} else {
+		SDT_PROBE(cbb, kernel, write, alloc_done, 0, 0, 0, 0, 0);
+		io->scsiio.be_move_done = ctl_be_block_move_done;
+		io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs;
+		io->scsiio.kern_data_len = beio->io_len;
+		io->scsiio.kern_total_len = beio->io_len;
+		io->scsiio.kern_rel_offset = 0;
+		io->scsiio.kern_data_resid = 0;
+		io->scsiio.kern_sg_entries = beio->num_segs;
+		io->io_hdr.flags |= CTL_FLAG_ALLOCATED | CTL_FLAG_KDPTR_SGLIST;
+#ifdef CTL_TIME_IO
+        	getbintime(&io->io_hdr.dma_start_bt);
+#endif  
+		ctl_datamove(io);
+	}
+}
+
+static void
+ctl_be_block_worker(void *context, int pending)
+{
+	struct ctl_be_block_lun *be_lun;
+	struct ctl_be_block_softc *softc;
+	union ctl_io *io;
+
+	be_lun = (struct ctl_be_block_lun *)context;
+	softc = be_lun->softc;
+
+	DPRINTF("entered\n");
+
+	mtx_lock(&be_lun->lock);
+	for (;;) {
+		io = (union ctl_io *)STAILQ_FIRST(&be_lun->datamove_queue);
+		if (io != NULL) {
+			struct ctl_be_block_io *beio;
+
+			DPRINTF("datamove queue\n");
+
+			STAILQ_REMOVE(&be_lun->datamove_queue, &io->io_hdr,
+				      ctl_io_hdr, links);
+
+			mtx_unlock(&be_lun->lock);
+
+			beio = (struct ctl_be_block_io *)
+			    io->io_hdr.ctl_private[CTL_PRIV_BACKEND].ptr;
+
+			be_lun->dispatch(be_lun, beio);
+
+			mtx_lock(&be_lun->lock);
+			continue;
+		}
+		io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_write_queue);
+		if (io != NULL) {
+
+			DPRINTF("config write queue\n");
+
+			STAILQ_REMOVE(&be_lun->config_write_queue, &io->io_hdr,
+				      ctl_io_hdr, links);
+
+			mtx_unlock(&be_lun->lock);
+
+			ctl_be_block_cw_dispatch(be_lun, io);
+
+			mtx_lock(&be_lun->lock);
+			continue;
+		}
+		io = (union ctl_io *)STAILQ_FIRST(&be_lun->input_queue);
+		if (io != NULL) {
+			DPRINTF("input queue\n");
+
+			STAILQ_REMOVE(&be_lun->input_queue, &io->io_hdr,
+				      ctl_io_hdr, links);
+			mtx_unlock(&be_lun->lock);
+
+			/*
+			 * We must drop the lock, since this routine and
+			 * its children may sleep.
+			 */
+			ctl_be_block_dispatch(be_lun, io);
+
+			mtx_lock(&be_lun->lock);
+			continue;
+		}
+
+		/*
+		 * If we get here, there is no work left in the queues, so
+		 * just break out and let the task queue go to sleep.
+		 */
+		break;
+	}
+	mtx_unlock(&be_lun->lock);
+}
+
+/*
+ * Entry point from CTL to the backend for I/O.  We queue everything to a
+ * work thread, so this just puts the I/O on a queue and wakes up the
+ * thread.
+ */
+static int
+ctl_be_block_submit(union ctl_io *io)
+{
+	struct ctl_be_block_lun *be_lun;
+	struct ctl_be_lun *ctl_be_lun;
+	int retval;
+
+	DPRINTF("entered\n");
+
+	retval = CTL_RETVAL_COMPLETE;
+
+	ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
+		CTL_PRIV_BACKEND_LUN].ptr;
+	be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun;
+
+	/*
+	 * Make sure we only get SCSI I/O.
+	 */
+	KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, ("Non-SCSI I/O (type "
+		"%#x) encountered", io->io_hdr.io_type));
+
+	mtx_lock(&be_lun->lock);
+	/*
+	 * XXX KDM make sure that links is okay to use at this point.
+	 * Otherwise, we either need to add another field to ctl_io_hdr,
+	 * or deal with resource allocation here.
+	 */
+	STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links);
+	mtx_unlock(&be_lun->lock);
+
+	taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
+
+	return (retval);
+}
+
+static int
+ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
+			int flag, struct thread *td)
+{
+	struct ctl_be_block_softc *softc;
+	int error;
+
+	softc = &backend_block_softc;
+
+	error = 0;
+
+	switch (cmd) {
+	case CTL_LUN_REQ: {
+		struct ctl_lun_req *lun_req;
+
+		lun_req = (struct ctl_lun_req *)addr;
+
+		switch (lun_req->reqtype) {
+		case CTL_LUNREQ_CREATE:
+			error = ctl_be_block_create(softc, lun_req);
+			break;
+		case CTL_LUNREQ_RM:
+			error = ctl_be_block_rm(softc, lun_req);
+			break;
+		default:
+			lun_req->status = CTL_LUN_ERROR;
+			snprintf(lun_req->error_str, sizeof(lun_req->error_str),
+				 "%s: invalid LUN request type %d", __func__,
+				 lun_req->reqtype);
+			break;
+		}
+		break;
+	}
+	default:
+		error = ENOTTY;
+		break;
+	}
+
+	return (error);
+}
+
+static int
+ctl_be_block_open_file(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
+{
+	struct ctl_be_block_filedata *file_data;
+	struct ctl_lun_create_params *params;
+	struct vattr		      vattr;
+	int			      error;
+
+	error = 0;
+	file_data = &be_lun->backend.file;
+	params = &req->reqdata.create;
+
+	be_lun->dev_type = CTL_BE_BLOCK_FILE;
+	be_lun->dispatch = ctl_be_block_dispatch_file;
+	be_lun->lun_flush = ctl_be_block_flush_file;
+
+	error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
+	if (error != 0) {
+		snprintf(req->error_str, sizeof(req->error_str),
+			 "error calling VOP_GETATTR() for file %s",
+			 be_lun->dev_path);
+		return (error);
+	}
+
+	/*
+	 * Verify that we have the ability to upgrade to exclusive
+	 * access on this file so we can trap errors at open instead
+	 * of reporting them during first access.
+	 */
+	if (VOP_ISLOCKED(be_lun->vn) != LK_EXCLUSIVE) {
+		vn_lock(be_lun->vn, LK_UPGRADE | LK_RETRY);
+		if (be_lun->vn->v_iflag & VI_DOOMED) {
+			error = EBADF;
+			snprintf(req->error_str, sizeof(req->error_str),
+				 "error locking file %s", be_lun->dev_path);
+			return (error);
+		}
+	}
+
+
+	file_data->cred = crhold(curthread->td_ucred);
+	be_lun->size_bytes = vattr.va_size;
+	/*
+	 * We set the multi thread flag for file operations because all
+	 * filesystems (in theory) are capable of allowing multiple readers
+	 * of a file at once.  So we want to get the maximum possible
+	 * concurrency.
+	 */
+	be_lun->flags |= CTL_BE_BLOCK_LUN_MULTI_THREAD;
+
+	/*
+	 * XXX KDM vattr.va_blocksize may be larger than 512 bytes here.
+	 * With ZFS, it is 131072 bytes.  Block sizes that large don't work
+	 * with disklabel and UFS on FreeBSD at least.  Large block sizes
+	 * may not work with other OSes as well.  So just export a sector
+	 * size of 512 bytes, which should work with any OS or
+	 * application.  Since our backing is a file, any block size will
+	 * work fine for the backing store.
+	 */
+#if 0
+	be_lun->blocksize= vattr.va_blocksize;
+#endif
+	if (params->blocksize_bytes != 0)
+		be_lun->blocksize = params->blocksize_bytes;
+	else
+		be_lun->blocksize = 512;
+
+	/*
+	 * Sanity check.  The media size has to be at least one
+	 * sector long.
+	 */
+	if (be_lun->size_bytes < be_lun->blocksize) {
+		error = EINVAL;
+		snprintf(req->error_str, sizeof(req->error_str),
+			 "file %s size %ju < block size %u", be_lun->dev_path,
+			 (uintmax_t)be_lun->size_bytes, be_lun->blocksize);
+	}
+	return (error);
+}
+
+static int
+ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
+{
+	struct ctl_lun_create_params *params;
+	struct vattr		      vattr;
+	struct cdev		     *dev;
+	struct cdevsw		     *devsw;
+	int			      error;
+
+	params = &req->reqdata.create;
+
+	be_lun->dev_type = CTL_BE_BLOCK_DEV;
+	be_lun->dispatch = ctl_be_block_dispatch_dev;
+	be_lun->lun_flush = ctl_be_block_flush_dev;
+	be_lun->backend.dev.cdev = be_lun->vn->v_rdev;
+	be_lun->backend.dev.csw = dev_refthread(be_lun->backend.dev.cdev,
+					     &be_lun->backend.dev.dev_ref);
+	if (be_lun->backend.dev.csw == NULL)
+		panic("Unable to retrieve device switch");
+
+	error = VOP_GETATTR(be_lun->vn, &vattr, NOCRED);
+	if (error) {
+		snprintf(req->error_str, sizeof(req->error_str),
+			 "%s: error getting vnode attributes for device %s",
+			 __func__, be_lun->dev_path);
+		return (error);
+	}
+
+	dev = be_lun->vn->v_rdev;
+	devsw = dev->si_devsw;
+	if (!devsw->d_ioctl) {
+		snprintf(req->error_str, sizeof(req->error_str),
+			 "%s: no d_ioctl for device %s!", __func__,
+			 be_lun->dev_path);
+		return (ENODEV);
+	}
+
+	error = devsw->d_ioctl(dev, DIOCGSECTORSIZE,
+			       (caddr_t)&be_lun->blocksize, FREAD,
+			       curthread);
+	if (error) {
+		snprintf(req->error_str, sizeof(req->error_str),
+			 "%s: error %d returned for DIOCGSECTORSIZE ioctl "
+			 "on %s!", __func__, error, be_lun->dev_path);
+		return (error);
+	}
+
+	/*
+	 * If the user has asked for a blocksize that is greater than the
+	 * backing device's blocksize, we can do it only if the blocksize
+	 * the user is asking for is an even multiple of the underlying 
+	 * device's blocksize.
+	 */
+	if ((params->blocksize_bytes != 0)
+	 && (params->blocksize_bytes > be_lun->blocksize)) {
+		uint32_t bs_multiple, tmp_blocksize;
+
+		bs_multiple = params->blocksize_bytes / be_lun->blocksize;
+
+		tmp_blocksize = bs_multiple * be_lun->blocksize;
+
+		if (tmp_blocksize == params->blocksize_bytes) {
+			be_lun->blocksize = params->blocksize_bytes;
+		} else {
+			snprintf(req->error_str, sizeof(req->error_str),
+				 "%s: requested blocksize %u is not an even "
+				 "multiple of backing device blocksize %u",
+				 __func__, params->blocksize_bytes,
+				 be_lun->blocksize);
+			return (EINVAL);
+			
+		}
+	} else if ((params->blocksize_bytes != 0)
+		&& (params->blocksize_bytes != be_lun->blocksize)) {
+		snprintf(req->error_str, sizeof(req->error_str),
+			 "%s: requested blocksize %u < backing device "
+			 "blocksize %u", __func__, params->blocksize_bytes,
+			 be_lun->blocksize);
+		return (EINVAL);
+	}
+
+	error = devsw->d_ioctl(dev, DIOCGMEDIASIZE,
+			       (caddr_t)&be_lun->size_bytes, FREAD,
+			       curthread);
+	if (error) {
+		snprintf(req->error_str, sizeof(req->error_str),
+			 "%s: error %d returned for DIOCGMEDIASIZE ioctl "
+			 "on %s!", __func__, error, be_lun->dev_path);
+		return (error);
+	}
+
+	return (0);
+
+}
+
+
+static int
+ctl_be_block_close(struct ctl_be_block_lun *be_lun)
+{
+	DROP_GIANT();
+	if (be_lun->vn) {
+		int flags = FREAD | FWRITE;
+		int vfs_is_locked = 0;
+
+		switch (be_lun->dev_type) {
+		case CTL_BE_BLOCK_DEV:
+			if (be_lun->backend.dev.csw) {
+				dev_relthread(be_lun->backend.dev.cdev,
+					      be_lun->backend.dev.dev_ref);
+				be_lun->backend.dev.csw  = NULL;
+				be_lun->backend.dev.cdev = NULL;
+			}
+			break;
+		case CTL_BE_BLOCK_FILE:
+			vfs_is_locked = VFS_LOCK_GIANT(be_lun->vn->v_mount);
+			break;
+		case CTL_BE_BLOCK_NONE:
+		default:
+			panic("Unexpected backend type.");
+			break;
+		}
+
+		(void)vn_close(be_lun->vn, flags, NOCRED, curthread);
+		be_lun->vn = NULL;
+
+		switch (be_lun->dev_type) {
+		case CTL_BE_BLOCK_DEV:
+			break;
+		case CTL_BE_BLOCK_FILE:
+			VFS_UNLOCK_GIANT(vfs_is_locked);
+			if (be_lun->backend.file.cred != NULL) {
+				crfree(be_lun->backend.file.cred);
+				be_lun->backend.file.cred = NULL;
+			}
+			break;
+		case CTL_BE_BLOCK_NONE:
+		default:
+			panic("Unexpected backend type.");
+			break;
+		}
+	}
+	PICKUP_GIANT();
+
+	return (0);
+}
+
+static int
+ctl_be_block_open(struct ctl_be_block_softc *softc,
+		       struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
+{
+	struct nameidata nd;
+	int		 flags;
+	int		 error;
+	int		 vfs_is_locked;
+
+	/*
+	 * XXX KDM allow a read-only option?
+	 */
+	flags = FREAD | FWRITE;
+	error = 0;
+
+	if (rootvnode == NULL) {
+		snprintf(req->error_str, sizeof(req->error_str),
+			 "%s: Root filesystem is not mounted", __func__);
+		return (1);
+	}
+
+	if (!curthread->td_proc->p_fd->fd_cdir) {
+		curthread->td_proc->p_fd->fd_cdir = rootvnode;
+		VREF(rootvnode);
+	}
+	if (!curthread->td_proc->p_fd->fd_rdir) {
+		curthread->td_proc->p_fd->fd_rdir = rootvnode;
+		VREF(rootvnode);
+	}
+	if (!curthread->td_proc->p_fd->fd_jdir) {
+		curthread->td_proc->p_fd->fd_jdir = rootvnode;
+		VREF(rootvnode);
+	}
+
+ again:
+	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path, curthread);
+	error = vn_open(&nd, &flags, 0, NULL);
+	if (error) {
+		/*
+		 * This is the only reasonable guess we can make as far as
+		 * path if the user doesn't give us a fully qualified path.
+		 * If they want to specify a file, they need to specify the
+		 * full path.
+		 */
+		if (be_lun->dev_path[0] != '/') {
+			char *dev_path = "/dev/";
+			char *dev_name;
+
+			/* Try adding device path at beginning of name */
+			dev_name = malloc(strlen(be_lun->dev_path)
+					+ strlen(dev_path) + 1,
+					  M_CTLBLK, M_WAITOK);
+			if (dev_name) {
+				sprintf(dev_name, "%s%s", dev_path,
+					be_lun->dev_path);
+				free(be_lun->dev_path, M_CTLBLK);
+				be_lun->dev_path = dev_name;
+				goto again;
+			}
+		}
+		snprintf(req->error_str, sizeof(req->error_str),
+			 "%s: error opening %s", __func__, be_lun->dev_path);
+		return (error);
+	}
+
+	vfs_is_locked = NDHASGIANT(&nd);
+
+	NDFREE(&nd, NDF_ONLY_PNBUF);
+		
+	be_lun->vn = nd.ni_vp;
+
+	/* We only support disks and files. */
+	if (vn_isdisk(be_lun->vn, &error)) {
+		error = ctl_be_block_open_dev(be_lun, req);
+	} else if (be_lun->vn->v_type == VREG) {
+		error = ctl_be_block_open_file(be_lun, req);
+	} else {
+		error = EINVAL;
+		snprintf(req->error_str, sizeof(req->error_str),
+			 "%s is not a disk or file", be_lun->dev_path);
+	}
+	VOP_UNLOCK(be_lun->vn, 0);
+	VFS_UNLOCK_GIANT(vfs_is_locked);
+
+	if (error != 0) {
+		ctl_be_block_close(be_lun);
+		return (error);
+	}
+
+	be_lun->blocksize_shift = fls(be_lun->blocksize) - 1;
+	be_lun->size_blocks = be_lun->size_bytes >> be_lun->blocksize_shift;
+
+	return (0);
+
+}
+
+static int
+ctl_be_block_mem_ctor(void *mem, int size, void *arg, int flags)
+{
+	return (0);
+}
+
+static void
+ctl_be_block_mem_dtor(void *mem, int size, void *arg)
+{
+	bzero(mem, size);
+}
+
+static int
+ctl_be_block_create(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
+{
+	struct ctl_be_block_lun *be_lun;
+	struct ctl_lun_create_params *params;
+	struct ctl_be_arg *file_arg;
+	char tmpstr[32];
+	int retval, num_threads;
+	int i;
+
+	params = &req->reqdata.create;
+	retval = 0;
+
+	num_threads = cbb_num_threads;
+
+	file_arg = NULL;
+
+	be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK);
+
+	if (be_lun == NULL) {
+		snprintf(req->error_str, sizeof(req->error_str),
+			 "%s: error allocating %zd bytes", __func__,
+			 sizeof(*be_lun));
+		goto bailout_error;
+	}
+
+	be_lun->softc = softc;
+	STAILQ_INIT(&be_lun->input_queue);
+	STAILQ_INIT(&be_lun->config_write_queue);
+	STAILQ_INIT(&be_lun->datamove_queue);
+	sprintf(be_lun->lunname, "cblk%d", softc->num_luns);
+	mtx_init(&be_lun->lock, be_lun->lunname, NULL, MTX_DEF);
+
+	be_lun->lun_zone = uma_zcreate(be_lun->lunname, MAXPHYS, 
+	    ctl_be_block_mem_ctor, ctl_be_block_mem_dtor, NULL, NULL,
+	    /*align*/ 0, /*flags*/0);
+
+	if (be_lun->lun_zone == NULL) {
+		snprintf(req->error_str, sizeof(req->error_str),
+			 "%s: error allocating UMA zone", __func__);
+		goto bailout_error;
+	}
+
+	if (params->flags & CTL_LUN_FLAG_DEV_TYPE)
+		be_lun->ctl_be_lun.lun_type = params->device_type;
+	else
+		be_lun->ctl_be_lun.lun_type = T_DIRECT;
+
+	if (be_lun->ctl_be_lun.lun_type == T_DIRECT) {
+		for (i = 0; i < req->num_be_args; i++) {
+			if (strcmp(req->kern_be_args[i].name, "file") == 0) {
+				file_arg = &req->kern_be_args[i];
+				break;
+			}
+		}
+
+		if (file_arg == NULL) {
+			snprintf(req->error_str, sizeof(req->error_str),
+				 "%s: no file argument specified", __func__);
+			goto bailout_error;
+		}
+
+		be_lun->dev_path = malloc(file_arg->vallen, M_CTLBLK,
+					  M_WAITOK | M_ZERO);
+		if (be_lun->dev_path == NULL) {
+			snprintf(req->error_str, sizeof(req->error_str),
+				 "%s: error allocating %d bytes", __func__,
+				 file_arg->vallen);
+			goto bailout_error;
+		}
+
+		strlcpy(be_lun->dev_path, (char *)file_arg->value,
+			file_arg->vallen);
+
+		retval = ctl_be_block_open(softc, be_lun, req);
+		if (retval != 0) {
+			retval = 0;
+			goto bailout_error;
+		}
+
+		/*
+		 * Tell the user the size of the file/device.
+		 */
+		params->lun_size_bytes = be_lun->size_bytes;
+
+		/*
+		 * The maximum LBA is the size - 1.
+		 */
+		be_lun->ctl_be_lun.maxlba = be_lun->size_blocks - 1;
+	} else {
+		/*
+		 * For processor devices, we don't have any size.
+		 */
+		be_lun->blocksize = 0;
+		be_lun->size_blocks = 0;
+		be_lun->size_bytes = 0;
+		be_lun->ctl_be_lun.maxlba = 0;
+		params->lun_size_bytes = 0;
+
+		/*
+		 * Default to just 1 thread for processor devices.
+		 */
+		num_threads = 1;
+	}
+
+	/*
+	 * XXX This searching loop might be refactored to be combined with
+	 * the loop above,
+	 */
+	for (i = 0; i < req->num_be_args; i++) {
+		if (strcmp(req->kern_be_args[i].name, "num_threads") == 0) {
+			struct ctl_be_arg *thread_arg;
+			char num_thread_str[16];
+			int tmp_num_threads;
+
+
+			thread_arg = &req->kern_be_args[i];
+
+			strlcpy(num_thread_str, (char *)thread_arg->value,
+				min(thread_arg->vallen,
+				sizeof(num_thread_str)));
+
+			tmp_num_threads = strtol(num_thread_str, NULL, 0);
+
+			/*
+			 * We don't let the user specify less than one
+			 * thread, but hope he's clueful enough not to
+			 * specify 1000 threads.
+			 */
+			if (tmp_num_threads < 1) {
+				snprintf(req->error_str, sizeof(req->error_str),
+					 "%s: invalid number of threads %s",
+				         __func__, num_thread_str);
+				goto bailout_error;
+			}
+
+			num_threads = tmp_num_threads;
+		}
+	}
+
+	be_lun->flags = CTL_BE_BLOCK_LUN_UNCONFIGURED;
+	be_lun->ctl_be_lun.flags = CTL_LUN_FLAG_PRIMARY;
+	be_lun->ctl_be_lun.be_lun = be_lun;
+	be_lun->ctl_be_lun.blocksize = be_lun->blocksize;
+	/* Tell the user the blocksize we ended up using */
+	params->blocksize_bytes = be_lun->blocksize;
+	if (params->flags & CTL_LUN_FLAG_ID_REQ) {
+		be_lun->ctl_be_lun.req_lun_id = params->req_lun_id;
+		be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_ID_REQ;
+	} else
+		be_lun->ctl_be_lun.req_lun_id = 0;
+
+	be_lun->ctl_be_lun.lun_shutdown = ctl_be_block_lun_shutdown;
+	be_lun->ctl_be_lun.lun_config_status =
+		ctl_be_block_lun_config_status;
+	be_lun->ctl_be_lun.be = &ctl_be_block_driver;
+
+	if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) {
+		snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d",
+			 softc->num_luns);
+		strncpy((char *)be_lun->ctl_be_lun.serial_num, tmpstr,
+			ctl_min(sizeof(be_lun->ctl_be_lun.serial_num),
+			sizeof(tmpstr)));
+
+		/* Tell the user what we used for a serial number */
+		strncpy((char *)params->serial_num, tmpstr,
+			ctl_min(sizeof(params->serial_num), sizeof(tmpstr)));
+	} else { 
+		strncpy((char *)be_lun->ctl_be_lun.serial_num,
+			params->serial_num,
+			ctl_min(sizeof(be_lun->ctl_be_lun.serial_num),
+			sizeof(params->serial_num)));
+	}
+	if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) {
+		snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns);
+		strncpy((char *)be_lun->ctl_be_lun.device_id, tmpstr,
+			ctl_min(sizeof(be_lun->ctl_be_lun.device_id),
+			sizeof(tmpstr)));
+
+		/* Tell the user what we used for a device ID */
+		strncpy((char *)params->device_id, tmpstr,
+			ctl_min(sizeof(params->device_id), sizeof(tmpstr)));
+	} else {
+		strncpy((char *)be_lun->ctl_be_lun.device_id,
+			params->device_id,
+			ctl_min(sizeof(be_lun->ctl_be_lun.device_id),
+				sizeof(params->device_id)));
+	}
+
+	TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun);
+
+	be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK,
+	    taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue);
+
+	if (be_lun->io_taskqueue == NULL) {
+		snprintf(req->error_str, sizeof(req->error_str),
+			 "%s: Unable to create taskqueue", __func__);
+		goto bailout_error;
+	}
+
+	/*
+	 * Note that we start the same number of threads by default for
+	 * both the file case and the block device case.  For the file
+	 * case, we need multiple threads to allow concurrency, because the
+	 * vnode interface is designed to be a blocking interface.  For the
+	 * block device case, ZFS zvols at least will block the caller's
+	 * context in many instances, and so we need multiple threads to
+	 * overcome that problem.  Other block devices don't need as many
+	 * threads, but they shouldn't cause too many problems.
+	 *
+	 * If the user wants to just have a single thread for a block
+	 * device, he can specify that when the LUN is created, or change
+	 * the tunable/sysctl to alter the default number of threads.
+	 */
+	retval = taskqueue_start_threads(&be_lun->io_taskqueue,
+					 /*num threads*/num_threads,
+					 /*priority*/PWAIT,
+					 /*thread name*/
+					 "%s taskq", be_lun->lunname);
+
+	if (retval != 0)
+		goto bailout_error;
+
+	be_lun->num_threads = num_threads;
+
+	mtx_lock(&softc->lock);
+	softc->num_luns++;
+	STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links);
+
+	mtx_unlock(&softc->lock);
+
+	retval = ctl_add_lun(&be_lun->ctl_be_lun);
+	if (retval != 0) {
+		mtx_lock(&softc->lock);
+		STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun,
+			      links);
+		softc->num_luns--;
+		mtx_unlock(&softc->lock);
+		snprintf(req->error_str, sizeof(req->error_str),
+			 "%s: ctl_add_lun() returned error %d, see dmesg for "
+			"details", __func__, retval);
+		retval = 0;
+		goto bailout_error;
+	}
+
+	mtx_lock(&softc->lock);
+
+	/*
+	 * Tell the config_status routine that we're waiting so it won't
+	 * clean up the LUN in the event of an error.
+	 */
+	be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING;
+
+	while (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) {
+		retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0);
+		if (retval == EINTR)
+			break;
+	}
+	be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
+
+	if (be_lun->flags & CTL_BE_BLOCK_LUN_CONFIG_ERR) {
+		snprintf(req->error_str, sizeof(req->error_str),
+			 "%s: LUN configuration error, see dmesg for details",
+			 __func__);
+		STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun,
+			      links);
+		softc->num_luns--;
+		mtx_unlock(&softc->lock);
+		goto bailout_error;
+	} else {
+		params->req_lun_id = be_lun->ctl_be_lun.lun_id;
+	}
+
+	mtx_unlock(&softc->lock);
+
+	be_lun->disk_stats = devstat_new_entry("cbb", params->req_lun_id,
+					       be_lun->blocksize,
+					       DEVSTAT_ALL_SUPPORTED,
+					       be_lun->ctl_be_lun.lun_type
+					       | DEVSTAT_TYPE_IF_OTHER,
+					       DEVSTAT_PRIORITY_OTHER);
+
+
+	req->status = CTL_LUN_OK;
+
+	return (retval);
+
+bailout_error:
+	req->status = CTL_LUN_ERROR;
+
+	ctl_be_block_close(be_lun);
+
+	free(be_lun->dev_path, M_CTLBLK);
+	free(be_lun, M_CTLBLK);
+
+	return (retval);
+}
+
+static int
+ctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
+{
+	struct ctl_lun_rm_params *params;
+	struct ctl_be_block_lun *be_lun;
+	int retval;
+
+	params = &req->reqdata.rm;
+
+	mtx_lock(&softc->lock);
+
+	be_lun = NULL;
+
+	STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
+		if (be_lun->ctl_be_lun.lun_id == params->lun_id)
+			break;
+	}
+	mtx_unlock(&softc->lock);
+
+	if (be_lun == NULL) {
+		snprintf(req->error_str, sizeof(req->error_str),
+			 "%s: LUN %u is not managed by the block backend",
+			 __func__, params->lun_id);
+		goto bailout_error;
+	}
+
+	retval = ctl_disable_lun(&be_lun->ctl_be_lun);
+
+	if (retval != 0) {
+		snprintf(req->error_str, sizeof(req->error_str),
+			 "%s: error %d returned from ctl_disable_lun() for "
+			 "LUN %d", __func__, retval, params->lun_id);
+		goto bailout_error;
+
+	}
+
+	retval = ctl_invalidate_lun(&be_lun->ctl_be_lun);
+	if (retval != 0) {
+		snprintf(req->error_str, sizeof(req->error_str),
+			 "%s: error %d returned from ctl_invalidate_lun() for "
+			 "LUN %d", __func__, retval, params->lun_id);
+		goto bailout_error;
+	}
+
+	mtx_lock(&softc->lock);
+
+	be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING;
+
+	while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) {
+                retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0);
+                if (retval == EINTR)
+                        break;
+        }
+
+	be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
+
+	if ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) {
+		snprintf(req->error_str, sizeof(req->error_str),
+			 "%s: interrupted waiting for LUN to be freed", 
+			 __func__);
+		mtx_unlock(&softc->lock);
+		goto bailout_error;
+	}
+
+	STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, links);
+
+	softc->num_luns--;
+	mtx_unlock(&softc->lock);
+
+	taskqueue_drain(be_lun->io_taskqueue, &be_lun->io_task);
+
+	taskqueue_free(be_lun->io_taskqueue);
+
+	ctl_be_block_close(be_lun);
+
+	if (be_lun->disk_stats != NULL)
+		devstat_remove_entry(be_lun->disk_stats);
+
+	uma_zdestroy(be_lun->lun_zone);
+
+	free(be_lun->dev_path, M_CTLBLK);
+
+	free(be_lun, M_CTLBLK);
+
+	req->status = CTL_LUN_OK;
+
+	return (0);
+
+bailout_error:
+
+	req->status = CTL_LUN_ERROR;
+
+	return (0);
+}
+
+static void
+ctl_be_block_lun_shutdown(void *be_lun)
+{
+	struct ctl_be_block_lun *lun;
+	struct ctl_be_block_softc *softc;
+
+	lun = (struct ctl_be_block_lun *)be_lun;
+
+	softc = lun->softc;
+
+	mtx_lock(&softc->lock);
+	lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED;
+	if (lun->flags & CTL_BE_BLOCK_LUN_WAITING)
+		wakeup(lun);
+	mtx_unlock(&softc->lock);
+
+}
+
+static void
+ctl_be_block_lun_config_status(void *be_lun, ctl_lun_config_status status)
+{
+	struct ctl_be_block_lun *lun;
+	struct ctl_be_block_softc *softc;
+
+	lun = (struct ctl_be_block_lun *)be_lun;
+	softc = lun->softc;
+
+	if (status == CTL_LUN_CONFIG_OK) {
+		mtx_lock(&softc->lock);
+		lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED;
+		if (lun->flags & CTL_BE_BLOCK_LUN_WAITING)
+			wakeup(lun);
+		mtx_unlock(&softc->lock);
+
+		/*
+		 * We successfully added the LUN, attempt to enable it.
+		 */
+		if (ctl_enable_lun(&lun->ctl_be_lun) != 0) {
+			printf("%s: ctl_enable_lun() failed!\n", __func__);
+			if (ctl_invalidate_lun(&lun->ctl_be_lun) != 0) {
+				printf("%s: ctl_invalidate_lun() failed!\n",
+				       __func__);
+			}
+		}
+
+		return;
+	}
+
+
+	mtx_lock(&softc->lock);
+	lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED;
+	lun->flags |= CTL_BE_BLOCK_LUN_CONFIG_ERR;
+	wakeup(lun);
+	mtx_unlock(&softc->lock);
+}
+
+
+static int
+ctl_be_block_config_write(union ctl_io *io)
+{
+	struct ctl_be_block_lun *be_lun;
+	struct ctl_be_lun *ctl_be_lun;
+	int retval;
+
+	retval = 0;
+
+	DPRINTF("entered\n");
+
+	ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
+		CTL_PRIV_BACKEND_LUN].ptr;
+	be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun;
+
+	switch (io->scsiio.cdb[0]) {
+	case SYNCHRONIZE_CACHE:
+	case SYNCHRONIZE_CACHE_16:
+		/*
+		 * The upper level CTL code will filter out any CDBs with
+		 * the immediate bit set and return the proper error.
+		 *
+		 * We don't really need to worry about what LBA range the
+		 * user asked to be synced out.  When they issue a sync
+		 * cache command, we'll sync out the whole thing.
+		 */
+		mtx_lock(&be_lun->lock);
+		STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr,
+				   links);
+		mtx_unlock(&be_lun->lock);
+		taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
+		break;
+	case START_STOP_UNIT: {
+		struct scsi_start_stop_unit *cdb;
+
+		cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb;
+
+		if (cdb->how & SSS_START)
+			retval = ctl_start_lun(ctl_be_lun);
+		else {
+			retval = ctl_stop_lun(ctl_be_lun);
+			/*
+			 * XXX KDM Copan-specific offline behavior.
+			 * Figure out a reasonable way to port this?
+			 */
+#ifdef NEEDTOPORT
+			if ((retval == 0)
+			 && (cdb->byte2 & SSS_ONOFFLINE))
+				retval = ctl_lun_offline(ctl_be_lun);
+#endif
+		}
+
+		/*
+		 * In general, the above routines should not fail.  They
+		 * just set state for the LUN.  So we've got something
+		 * pretty wrong here if we can't start or stop the LUN.
+		 */
+		if (retval != 0) {
+			ctl_set_internal_failure(&io->scsiio,
+						 /*sks_valid*/ 1,
+						 /*retry_count*/ 0xf051);
+			retval = CTL_RETVAL_COMPLETE;
+		} else {
+			ctl_set_success(&io->scsiio);
+		}
+		ctl_config_write_done(io);
+		break;
+	}
+	default:
+		ctl_set_invalid_opcode(&io->scsiio);
+		ctl_config_write_done(io);
+		retval = CTL_RETVAL_COMPLETE;
+		break;
+	}
+
+	return (retval);
+
+}
+
+static int
+ctl_be_block_config_read(union ctl_io *io)
+{
+	return (0);
+}
+
+static int
+ctl_be_block_lun_info(void *be_lun, struct sbuf *sb)
+{
+	struct ctl_be_block_lun *lun;
+	int retval;
+
+	lun = (struct ctl_be_block_lun *)be_lun;
+	retval = 0;
+
+	retval = sbuf_printf(sb, "<num_threads>");
+
+	if (retval != 0)
+		goto bailout;
+
+	retval = sbuf_printf(sb, "%d", lun->num_threads);
+
+	if (retval != 0)
+		goto bailout;
+
+	retval = sbuf_printf(sb, "</num_threads>");
+
+	/*
+	 * For processor devices, we don't have a path variable.
+	 */
+	if ((retval != 0)
+	 || (lun->dev_path == NULL))
+		goto bailout;
+
+	retval = sbuf_printf(sb, "<file>");
+
+	if (retval != 0)
+		goto bailout;
+
+	retval = ctl_sbuf_printf_esc(sb, lun->dev_path);
+
+	if (retval != 0)
+		goto bailout;
+
+	retval = sbuf_printf(sb, "</file>\n");
+
+bailout:
+
+	return (retval);
+}
+
+int
+ctl_be_block_init(void)
+{
+	struct ctl_be_block_softc *softc;
+	int retval;
+
+	softc = &backend_block_softc;
+	retval = 0;
+
+	mtx_init(&softc->lock, "ctlblk", NULL, MTX_DEF);
+	STAILQ_INIT(&softc->beio_free_queue);
+	STAILQ_INIT(&softc->disk_list);
+	STAILQ_INIT(&softc->lun_list);
+	ctl_grow_beio(softc, 200);
+
+	return (retval);
+}
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/cam/ctl/ctl_backend_block.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/sys/cam/ctl/ctl_backend_block.h	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,72 @@
+/*-
+ * Copyright (c) 2003 Silicon Graphics International Corp.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions, and the following disclaimer,
+ *    without modification.
+ * 2. Redistributions in binary form must reproduce at minimum a disclaimer
+ *    substantially similar to the "NO WARRANTY" disclaimer below
+ *    ("Disclaimer") and any redistribution must be conditioned upon
+ *    including a substantially similar Disclaimer requirement for further
+ *    binary redistribution.
+ *
+ * NO WARRANTY
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGES.
+ *
+ * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_backend_block.h#1 $
+ * $FreeBSD: head/sys/cam/ctl/ctl_backend_block.h 229997 2012-01-12 00:34:33Z ken $
+ */
+/*
+ * CAM Target Layer driver backend interface for block devices.
+ *
+ * Author: Ken Merry <ken at FreeBSD.org>
+ */
+
+#ifndef	_CTL_BACKEND_BLOCK_H_
+#define	_CTL_BACKEND_BLOCK_H_
+
+struct ctl_block_disk {
+	uint32_t   version;	/* interface version */
+	uint32_t   disknum;	/* returned device number */
+	STAILQ_ENTRY(ctl_block_disk) links;  /* linked list pointer */
+	char	   disk_name[MAXPATHLEN]; /* name of this device */
+	int        allocated;	/* disk is allocated to a LUN */
+	uint64_t   size_blocks; /* disk size in blocks */
+	uint64_t   size_bytes;  /* disk size in bytes */
+};
+
+typedef enum {
+	CTL_BLOCK_DEVLIST_MORE,
+	CTL_BLOCK_DEVLIST_DONE
+} ctl_block_devlist_status;
+
+struct ctl_block_devlist {
+	uint32_t		version;	/* interface version */
+	uint32_t		buf_len;	/* passed in, buffer length */
+	uint32_t		ctl_disk_size;	/* size of adddev, passed in */
+	struct ctl_block_disk	*devbuf;	/* buffer passed in/filled out*/
+	uint32_t		num_bufs;	/* number passed out */
+	uint32_t		buf_used;	/* bytes passed out */
+	uint32_t		total_disks;	/* number of disks in system */
+	ctl_block_devlist_status status;	/* did we get the whole list? */
+};
+
+#define	CTL_BLOCK_ADDDEV	_IOWR(COPAN_ARRAY_BE_BLOCK, 0x00, struct ctl_block_disk)
+#define	CTL_BLOCK_DEVLIST	_IOWR(COPAN_ARRAY_BE_BLOCK, 0x01, struct ctl_block_devlist)
+#define	CTL_BLOCK_RMDEV		_IOW(COPAN_ARRAY_BE_BLOCK, 0x02, struct ctl_block_disk)
+
+#endif	/* _CTL_BACKEND_BLOCK_H_ */
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/cam/ctl/ctl_backend_ramdisk.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/sys/cam/ctl/ctl_backend_ramdisk.c	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,835 @@
+/*-
+ * Copyright (c) 2003, 2008 Silicon Graphics International Corp.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions, and the following disclaimer,
+ *    without modification.
+ * 2. Redistributions in binary form must reproduce at minimum a disclaimer
+ *    substantially similar to the "NO WARRANTY" disclaimer below
+ *    ("Disclaimer") and any redistribution must be conditioned upon
+ *    including a substantially similar Disclaimer requirement for further
+ *    binary redistribution.
+ *
+ * NO WARRANTY
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGES.
+ *
+ * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_backend_ramdisk.c#3 $
+ */
+/*
+ * CAM Target Layer backend for a "fake" ramdisk.
+ *
+ * Author: Ken Merry <ken at FreeBSD.org>
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD: head/sys/cam/ctl/ctl_backend_ramdisk.c 229997 2012-01-12 00:34:33Z ken $");
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/kernel.h>
+#include <sys/condvar.h>
+#include <sys/types.h>
+#include <sys/lock.h>
+#include <sys/mutex.h>
+#include <sys/malloc.h>
+#include <sys/time.h>
+#include <sys/queue.h>
+#include <sys/conf.h>
+#include <sys/ioccom.h>
+#include <sys/module.h>
+
+#include <cam/scsi/scsi_all.h>
+#include <cam/ctl/ctl_io.h>
+#include <cam/ctl/ctl.h>
+#include <cam/ctl/ctl_util.h>
+#include <cam/ctl/ctl_backend.h>
+#include <cam/ctl/ctl_frontend_internal.h>
+#include <cam/ctl/ctl_debug.h>
+#include <cam/ctl/ctl_ioctl.h>
+#include <cam/ctl/ctl_error.h>
+
+typedef enum {
+	CTL_BE_RAMDISK_LUN_UNCONFIGURED	= 0x01,
+	CTL_BE_RAMDISK_LUN_CONFIG_ERR	= 0x02,
+	CTL_BE_RAMDISK_LUN_WAITING	= 0x04
+} ctl_be_ramdisk_lun_flags;
+
+struct ctl_be_ramdisk_lun {
+	uint64_t size_bytes;
+	uint64_t size_blocks;
+	struct ctl_be_ramdisk_softc *softc;
+	ctl_be_ramdisk_lun_flags flags;
+	STAILQ_ENTRY(ctl_be_ramdisk_lun) links;
+	struct ctl_be_lun ctl_be_lun;
+};
+
+struct ctl_be_ramdisk_softc {
+	struct mtx lock;
+	int rd_size;
+#ifdef CTL_RAMDISK_PAGES
+	uint8_t **ramdisk_pages;
+	int num_pages;
+#else
+	uint8_t *ramdisk_buffer;
+#endif
+	int num_luns;
+	STAILQ_HEAD(, ctl_be_ramdisk_lun) lun_list;
+};
+
+static struct ctl_be_ramdisk_softc rd_softc;
+
+int ctl_backend_ramdisk_init(void);
+void ctl_backend_ramdisk_shutdown(void);
+static int ctl_backend_ramdisk_move_done(union ctl_io *io);
+static int ctl_backend_ramdisk_submit(union ctl_io *io);
+static int ctl_backend_ramdisk_ioctl(struct cdev *dev, u_long cmd,
+				     caddr_t addr, int flag, struct thread *td);
+static int ctl_backend_ramdisk_rm(struct ctl_be_ramdisk_softc *softc,
+				  struct ctl_lun_req *req);
+static int ctl_backend_ramdisk_create(struct ctl_be_ramdisk_softc *softc,
+				      struct ctl_lun_req *req, int do_wait);
+static void ctl_backend_ramdisk_lun_shutdown(void *be_lun);
+static void ctl_backend_ramdisk_lun_config_status(void *be_lun,
+						  ctl_lun_config_status status);
+static int ctl_backend_ramdisk_config_write(union ctl_io *io);
+static int ctl_backend_ramdisk_config_read(union ctl_io *io);
+
+static struct ctl_backend_driver ctl_be_ramdisk_driver = 
+{
+	name: "ramdisk",
+	flags: CTL_BE_FLAG_HAS_CONFIG,
+	init: ctl_backend_ramdisk_init,
+	data_submit: ctl_backend_ramdisk_submit,
+	data_move_done: ctl_backend_ramdisk_move_done,
+	config_read: ctl_backend_ramdisk_config_read,
+	config_write: ctl_backend_ramdisk_config_write,
+	ioctl: ctl_backend_ramdisk_ioctl
+};
+
+MALLOC_DEFINE(M_RAMDISK, "ramdisk", "Memory used for CTL RAMdisk");
+CTL_BACKEND_DECLARE(cbr, ctl_be_ramdisk_driver);
+
+int
+ctl_backend_ramdisk_init(void)
+{
+	struct ctl_be_ramdisk_softc *softc;
+#ifdef CTL_RAMDISK_PAGES
+	int i, j;
+#endif
+
+
+	softc = &rd_softc;
+
+	memset(softc, 0, sizeof(*softc));
+
+	mtx_init(&softc->lock, "ramdisk", NULL, MTX_DEF);
+
+	STAILQ_INIT(&softc->lun_list);
+	softc->rd_size = 4 * 1024 * 1024;
+#ifdef CTL_RAMDISK_PAGES
+	softc->num_pages = softc->rd_size / PAGE_SIZE;
+	softc->ramdisk_pages = (uint8_t **)malloc(sizeof(uint8_t *) *
+						  softc->num_pages, M_RAMDISK,
+						  M_WAITOK);
+	for (i = 0; i < softc->num_pages; i++) {
+		softc->ramdisk_pages[i] = malloc(PAGE_SIZE, M_RAMDISK,M_WAITOK);
+		if (softc->ramdisk_pages[i] == NULL) {
+			for (j = 0; j < i; j++) {
+				free(softc->ramdisk_pages[j], M_RAMDISK);
+			}
+			free(softc->ramdisk_pages, M_RAMDISK);
+			panic("RAMDisk initialization failed\n");
+			return (1); /* NOTREACHED */
+		}
+	}
+#else
+	softc->ramdisk_buffer = (uint8_t *)malloc(softc->rd_size, M_RAMDISK,
+						  M_WAITOK);
+#endif
+
+	return (0);
+}
+
+void
+ctl_backend_ramdisk_shutdown(void)
+{
+	struct ctl_be_ramdisk_softc *softc;
+	struct ctl_be_ramdisk_lun *lun, *next_lun;
+#ifdef CTL_RAMDISK_PAGES
+	int i;
+#endif
+
+	softc = &rd_softc;
+
+	mtx_lock(&softc->lock);
+	for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){
+		/*
+		 * Grab the next LUN.  The current LUN may get removed by
+		 * ctl_invalidate_lun(), which will call our LUN shutdown
+		 * routine, if there is no outstanding I/O for this LUN.
+		 */
+		next_lun = STAILQ_NEXT(lun, links);
+
+		/*
+		 * Drop our lock here.  Since ctl_invalidate_lun() can call
+		 * back into us, this could potentially lead to a recursive
+		 * lock of the same mutex, which would cause a hang.
+		 */
+		mtx_unlock(&softc->lock);
+		ctl_disable_lun(&lun->ctl_be_lun);
+		ctl_invalidate_lun(&lun->ctl_be_lun);
+		mtx_lock(&softc->lock);
+	}
+	mtx_unlock(&softc->lock);
+	
+#ifdef CTL_RAMDISK_PAGES
+	for (i = 0; i < softc->num_pages; i++)
+		free(softc->ramdisk_pages[i], M_RAMDISK);
+
+	free(softc->ramdisk_pages, M_RAMDISK);
+#else
+	free(softc->ramdisk_buffer, M_RAMDISK);
+#endif
+
+	if (ctl_backend_deregister(&ctl_be_ramdisk_driver) != 0) {
+		printf("ctl_backend_ramdisk_shutdown: "
+		       "ctl_backend_deregister() failed!\n");
+	}
+}
+
+static int
+ctl_backend_ramdisk_move_done(union ctl_io *io)
+{
+#ifdef CTL_TIME_IO
+	struct bintime cur_bt;
+#endif
+
+	CTL_DEBUG_PRINT(("ctl_backend_ramdisk_move_done\n"));
+	if ((io->io_hdr.port_status == 0)
+	 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
+	 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE))
+		io->io_hdr.status = CTL_SUCCESS;
+	else if ((io->io_hdr.port_status != 0)
+	      && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
+	      && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)){
+		/*
+		 * For hardware error sense keys, the sense key
+		 * specific value is defined to be a retry count,
+		 * but we use it to pass back an internal FETD
+		 * error code.  XXX KDM  Hopefully the FETD is only
+		 * using 16 bits for an error code, since that's
+		 * all the space we have in the sks field.
+		 */
+		ctl_set_internal_failure(&io->scsiio,
+					 /*sks_valid*/ 1,
+					 /*retry_count*/
+					 io->io_hdr.port_status);
+	}
+#ifdef CTL_TIME_IO
+	getbintime(&cur_bt);
+	bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
+	bintime_add(&io->io_hdr.dma_bt, &cur_bt);
+	io->io_hdr.num_dmas++;
+#endif
+
+	if (io->scsiio.kern_sg_entries > 0)
+		free(io->scsiio.kern_data_ptr, M_RAMDISK);
+	ctl_done(io);
+	return(0);
+}
+
+static int
+ctl_backend_ramdisk_submit(union ctl_io *io)
+{
+	struct ctl_lba_len lbalen;
+#ifdef CTL_RAMDISK_PAGES
+	struct ctl_sg_entry *sg_entries;
+	int len_filled;
+	int i;
+#endif
+	int num_sg_entries, len;
+	struct ctl_be_ramdisk_softc *softc;
+	struct ctl_be_lun *ctl_be_lun;
+	struct ctl_be_ramdisk_lun *be_lun;
+
+	softc = &rd_softc;
+	
+	ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
+		CTL_PRIV_BACKEND_LUN].ptr;
+	be_lun = (struct ctl_be_ramdisk_lun *)ctl_be_lun->be_lun;
+
+	memcpy(&lbalen, io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
+	       sizeof(lbalen));
+
+	len = lbalen.len * ctl_be_lun->blocksize;
+
+	/*
+	 * Kick out the request if it's bigger than we can handle.
+	 */
+	if (len > softc->rd_size) {
+		ctl_set_internal_failure(&io->scsiio,
+					 /*sks_valid*/ 0,
+					 /*retry_count*/ 0);
+		ctl_done(io);
+		return (CTL_RETVAL_COMPLETE);
+	}
+
+	/*
+	 * Kick out the request if it's larger than the device size that
+	 * the user requested.
+	 */
+	if (((lbalen.lba * ctl_be_lun->blocksize) + len) > be_lun->size_bytes) {
+		ctl_set_lba_out_of_range(&io->scsiio);
+		ctl_done(io);
+		return (CTL_RETVAL_COMPLETE);
+	}
+
+#ifdef CTL_RAMDISK_PAGES
+	num_sg_entries = len >> PAGE_SHIFT;
+	if ((len & (PAGE_SIZE - 1)) != 0)
+		num_sg_entries++;
+
+	if (num_sg_entries > 1) {
+		io->scsiio.kern_data_ptr = malloc(sizeof(struct ctl_sg_entry) *
+						  num_sg_entries, M_RAMDISK,
+						  M_WAITOK);
+		if (io->scsiio.kern_data_ptr == NULL) {
+			ctl_set_internal_failure(&io->scsiio,
+						 /*sks_valid*/ 0,
+						 /*retry_count*/ 0);
+			ctl_done(io);
+			return (CTL_RETVAL_COMPLETE);
+		}
+		sg_entries = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
+		for (i = 0, len_filled = 0; i < num_sg_entries;
+		     i++, len_filled += PAGE_SIZE) {
+			sg_entries[i].addr = softc->ramdisk_pages[i];
+			sg_entries[i].len = ctl_min(PAGE_SIZE,
+						    len - len_filled);
+		}
+	} else {
+#endif /* CTL_RAMDISK_PAGES */
+		/*
+		 * If this is less than 1 page, don't bother allocating a
+		 * scatter/gather list for it.  This saves time/overhead.
+		 */
+		num_sg_entries = 0;
+#ifdef CTL_RAMDISK_PAGES
+		io->scsiio.kern_data_ptr = softc->ramdisk_pages[0];
+#else
+		io->scsiio.kern_data_ptr = softc->ramdisk_buffer;
+#endif
+#ifdef CTL_RAMDISK_PAGES
+	}
+#endif
+
+	io->scsiio.be_move_done = ctl_backend_ramdisk_move_done;
+	io->scsiio.kern_data_len = len;
+	io->scsiio.kern_total_len = len;
+	io->scsiio.kern_rel_offset = 0;
+	io->scsiio.kern_data_resid = 0;
+	io->scsiio.kern_sg_entries = num_sg_entries;
+	io->io_hdr.flags |= CTL_FLAG_ALLOCATED | CTL_FLAG_KDPTR_SGLIST;
+#ifdef CTL_TIME_IO
+	getbintime(&io->io_hdr.dma_start_bt);
+#endif
+	ctl_datamove(io);
+
+	return (CTL_RETVAL_COMPLETE);
+}
+
+static int
+ctl_backend_ramdisk_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
+			  int flag, struct thread *td)
+{
+	struct ctl_be_ramdisk_softc *softc;
+	int retval;
+
+	retval = 0;
+	softc = &rd_softc;
+
+	switch (cmd) {
+	case CTL_LUN_REQ: {
+		struct ctl_lun_req *lun_req;
+
+		lun_req = (struct ctl_lun_req *)addr;
+
+		switch (lun_req->reqtype) {
+		case CTL_LUNREQ_CREATE:
+			retval = ctl_backend_ramdisk_create(softc, lun_req,
+							    /*do_wait*/ 1);
+			break;
+		case CTL_LUNREQ_RM:
+			retval = ctl_backend_ramdisk_rm(softc, lun_req);
+			break;
+		default:
+			lun_req->status = CTL_LUN_ERROR;
+			snprintf(lun_req->error_str, sizeof(lun_req->error_str),
+				 "%s: invalid LUN request type %d", __func__,
+				 lun_req->reqtype);
+			break;
+		}
+		break;
+	}
+	default:
+		retval = ENOTTY;
+		break;
+	}
+
+	return (retval);
+}
+
+static int
+ctl_backend_ramdisk_rm(struct ctl_be_ramdisk_softc *softc,
+		       struct ctl_lun_req *req)
+{
+	struct ctl_be_ramdisk_lun *be_lun;
+	struct ctl_lun_rm_params *params;
+	int retval;
+
+
+	retval = 0;
+	params = &req->reqdata.rm;
+
+	be_lun = NULL;
+
+	mtx_lock(&softc->lock);
+
+	STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
+		if (be_lun->ctl_be_lun.lun_id == params->lun_id)
+			break;
+	}
+	mtx_unlock(&softc->lock);
+
+	if (be_lun == NULL) {
+		snprintf(req->error_str, sizeof(req->error_str),
+			 "%s: LUN %u is not managed by the ramdisk backend",
+			 __func__, params->lun_id);
+		goto bailout_error;
+	}
+
+	retval = ctl_disable_lun(&be_lun->ctl_be_lun);
+
+	if (retval != 0) {
+		snprintf(req->error_str, sizeof(req->error_str),
+			 "%s: error %d returned from ctl_disable_lun() for "
+			 "LUN %d", __func__, retval, params->lun_id);
+		goto bailout_error;
+	}
+
+	/*
+	 * Set the waiting flag before we invalidate the LUN.  Our shutdown
+	 * routine can be called any time after we invalidate the LUN,
+	 * and can be called from our context.
+	 *
+	 * This tells the shutdown routine that we're waiting, or we're
+	 * going to wait for the shutdown to happen.
+	 */
+	mtx_lock(&softc->lock);
+	be_lun->flags |= CTL_BE_RAMDISK_LUN_WAITING;
+	mtx_unlock(&softc->lock);
+
+	retval = ctl_invalidate_lun(&be_lun->ctl_be_lun);
+	if (retval != 0) {
+		snprintf(req->error_str, sizeof(req->error_str),
+			 "%s: error %d returned from ctl_invalidate_lun() for "
+			 "LUN %d", __func__, retval, params->lun_id);
+		goto bailout_error;
+	}
+
+	mtx_lock(&softc->lock);
+
+	while ((be_lun->flags & CTL_BE_RAMDISK_LUN_UNCONFIGURED) == 0) {
+		retval = msleep(be_lun, &softc->lock, PCATCH, "ctlram", 0);
+ 		if (retval == EINTR)   
+			break;
+	}
+	be_lun->flags &= ~CTL_BE_RAMDISK_LUN_WAITING;
+
+	/*
+	 * We only remove this LUN from the list and free it (below) if
+	 * retval == 0.  If the user interrupted the wait, we just bail out
+	 * without actually freeing the LUN.  We let the shutdown routine
+	 * free the LUN if that happens.
+	 */
+	if (retval == 0) {
+		STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_ramdisk_lun,
+			      links);
+		softc->num_luns--;
+	}
+
+	mtx_unlock(&softc->lock);
+
+	if (retval == 0)
+		free(be_lun, M_RAMDISK);
+
+	req->status = CTL_LUN_OK;
+
+	return (retval);
+
+bailout_error:
+
+	/*
+	 * Don't leave the waiting flag set.
+	 */
+	mtx_lock(&softc->lock);
+	be_lun->flags &= ~CTL_BE_RAMDISK_LUN_WAITING;
+	mtx_unlock(&softc->lock);
+
+	req->status = CTL_LUN_ERROR;
+
+	return (0);
+}
+
+static int
+ctl_backend_ramdisk_create(struct ctl_be_ramdisk_softc *softc,
+			   struct ctl_lun_req *req, int do_wait)
+{
+	struct ctl_be_ramdisk_lun *be_lun;
+	struct ctl_lun_create_params *params;
+	uint32_t blocksize;
+	char tmpstr[32];
+	int retval;
+
+	retval = 0;
+	params = &req->reqdata.create;
+	if (params->blocksize_bytes != 0)
+		blocksize = params->blocksize_bytes;
+	else
+		blocksize = 512;
+
+	be_lun = malloc(sizeof(*be_lun), M_RAMDISK, M_ZERO | (do_wait ?
+			M_WAITOK : M_NOWAIT));
+
+	if (be_lun == NULL) {
+		snprintf(req->error_str, sizeof(req->error_str),
+			 "%s: error allocating %zd bytes", __func__,
+			 sizeof(*be_lun));
+		goto bailout_error;
+	}
+
+	if (params->flags & CTL_LUN_FLAG_DEV_TYPE)
+		be_lun->ctl_be_lun.lun_type = params->device_type;
+	else
+		be_lun->ctl_be_lun.lun_type = T_DIRECT;
+
+	if (be_lun->ctl_be_lun.lun_type == T_DIRECT) {
+
+		if (params->lun_size_bytes < blocksize) {
+			snprintf(req->error_str, sizeof(req->error_str),
+				 "%s: LUN size %ju < blocksize %u", __func__,
+				 params->lun_size_bytes, blocksize);
+			goto bailout_error;
+		}
+
+		be_lun->size_blocks = params->lun_size_bytes / blocksize;
+		be_lun->size_bytes = be_lun->size_blocks * blocksize;
+
+		be_lun->ctl_be_lun.maxlba = be_lun->size_blocks - 1;
+	} else {
+		be_lun->ctl_be_lun.maxlba = 0;
+		blocksize = 0;
+		be_lun->size_bytes = 0;
+		be_lun->size_blocks = 0;
+	}
+
+	be_lun->ctl_be_lun.blocksize = blocksize;
+
+	/* Tell the user the blocksize we ended up using */
+	params->blocksize_bytes = blocksize;
+
+	/* Tell the user the exact size we ended up using */
+	params->lun_size_bytes = be_lun->size_bytes;
+
+	be_lun->softc = softc;
+
+	be_lun->flags = CTL_BE_RAMDISK_LUN_UNCONFIGURED;
+	be_lun->ctl_be_lun.flags = CTL_LUN_FLAG_PRIMARY;
+	be_lun->ctl_be_lun.be_lun = be_lun;
+
+	if (params->flags & CTL_LUN_FLAG_ID_REQ) {
+		be_lun->ctl_be_lun.req_lun_id = params->req_lun_id;
+		be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_ID_REQ;
+	} else
+		be_lun->ctl_be_lun.req_lun_id = 0;
+
+	be_lun->ctl_be_lun.lun_shutdown = ctl_backend_ramdisk_lun_shutdown;
+	be_lun->ctl_be_lun.lun_config_status =
+		ctl_backend_ramdisk_lun_config_status;
+	be_lun->ctl_be_lun.be = &ctl_be_ramdisk_driver;
+	if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) {
+		snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d",
+			 softc->num_luns);
+		strncpy((char *)be_lun->ctl_be_lun.serial_num, tmpstr,
+			ctl_min(sizeof(be_lun->ctl_be_lun.serial_num),
+			sizeof(tmpstr)));
+
+		/* Tell the user what we used for a serial number */
+		strncpy((char *)params->serial_num, tmpstr,
+			ctl_min(sizeof(params->serial_num), sizeof(tmpstr)));
+	} else { 
+		strncpy((char *)be_lun->ctl_be_lun.serial_num,
+			params->serial_num,
+			ctl_min(sizeof(be_lun->ctl_be_lun.serial_num),
+			sizeof(params->serial_num)));
+	}
+	if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) {
+		snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns);
+		strncpy((char *)be_lun->ctl_be_lun.device_id, tmpstr,
+			ctl_min(sizeof(be_lun->ctl_be_lun.device_id),
+			sizeof(tmpstr)));
+
+		/* Tell the user what we used for a device ID */
+		strncpy((char *)params->device_id, tmpstr,
+			ctl_min(sizeof(params->device_id), sizeof(tmpstr)));
+	} else {
+		strncpy((char *)be_lun->ctl_be_lun.device_id,
+			params->device_id,
+			ctl_min(sizeof(be_lun->ctl_be_lun.device_id),
+				sizeof(params->device_id)));
+	}
+
+	mtx_lock(&softc->lock);
+	softc->num_luns++;
+	STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links);
+
+	mtx_unlock(&softc->lock);
+
+	retval = ctl_add_lun(&be_lun->ctl_be_lun);
+	if (retval != 0) {
+		mtx_lock(&softc->lock);
+		STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_ramdisk_lun,
+			      links);
+		softc->num_luns--;
+		mtx_unlock(&softc->lock);
+		snprintf(req->error_str, sizeof(req->error_str),
+			 "%s: ctl_add_lun() returned error %d, see dmesg for "
+			"details", __func__, retval);
+		retval = 0;
+		goto bailout_error;
+	}
+
+	if (do_wait == 0)
+		return (retval);
+
+	mtx_lock(&softc->lock);
+
+	/*
+	 * Tell the config_status routine that we're waiting so it won't
+	 * clean up the LUN in the event of an error.
+	 */
+	be_lun->flags |= CTL_BE_RAMDISK_LUN_WAITING;
+
+	while (be_lun->flags & CTL_BE_RAMDISK_LUN_UNCONFIGURED) {
+		retval = msleep(be_lun, &softc->lock, PCATCH, "ctlram", 0);
+		if (retval == EINTR)
+			break;
+	}
+	be_lun->flags &= ~CTL_BE_RAMDISK_LUN_WAITING;
+
+	if (be_lun->flags & CTL_BE_RAMDISK_LUN_CONFIG_ERR) {
+		snprintf(req->error_str, sizeof(req->error_str),
+			 "%s: LUN configuration error, see dmesg for details",
+			 __func__);
+		STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_ramdisk_lun,
+			      links);
+		softc->num_luns--;
+		mtx_unlock(&softc->lock);
+		goto bailout_error;
+	} else {
+		params->req_lun_id = be_lun->ctl_be_lun.lun_id;
+	}
+	mtx_unlock(&softc->lock);
+
+	req->status = CTL_LUN_OK;
+
+	return (retval);
+
+bailout_error:
+	req->status = CTL_LUN_ERROR;
+	free(be_lun, M_RAMDISK);
+
+	return (retval);
+}
+
+static void
+ctl_backend_ramdisk_lun_shutdown(void *be_lun)
+{
+	struct ctl_be_ramdisk_lun *lun;
+	struct ctl_be_ramdisk_softc *softc;
+	int do_free;
+
+	lun = (struct ctl_be_ramdisk_lun *)be_lun;
+	softc = lun->softc;
+	do_free = 0;
+
+	mtx_lock(&softc->lock);
+
+	lun->flags |= CTL_BE_RAMDISK_LUN_UNCONFIGURED;
+
+	if (lun->flags & CTL_BE_RAMDISK_LUN_WAITING) {
+		wakeup(lun);
+	} else {
+		STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_ramdisk_lun,
+			      links);
+		softc->num_luns--;
+		do_free = 1;
+	}
+
+	mtx_unlock(&softc->lock);
+
+	if (do_free != 0)
+		free(be_lun, M_RAMDISK);
+}
+
+static void
+ctl_backend_ramdisk_lun_config_status(void *be_lun,
+				      ctl_lun_config_status status)
+{
+	struct ctl_be_ramdisk_lun *lun;
+	struct ctl_be_ramdisk_softc *softc;
+
+	lun = (struct ctl_be_ramdisk_lun *)be_lun;
+	softc = lun->softc;
+
+	if (status == CTL_LUN_CONFIG_OK) {
+		mtx_lock(&softc->lock);
+		lun->flags &= ~CTL_BE_RAMDISK_LUN_UNCONFIGURED;
+		if (lun->flags & CTL_BE_RAMDISK_LUN_WAITING)
+			wakeup(lun);
+		mtx_unlock(&softc->lock);
+
+		/*
+		 * We successfully added the LUN, attempt to enable it.
+		 */
+		if (ctl_enable_lun(&lun->ctl_be_lun) != 0) {
+			printf("%s: ctl_enable_lun() failed!\n", __func__);
+			if (ctl_invalidate_lun(&lun->ctl_be_lun) != 0) {
+				printf("%s: ctl_invalidate_lun() failed!\n",
+				       __func__);
+			}
+		}
+
+		return;
+	}
+
+
+	mtx_lock(&softc->lock);
+	lun->flags &= ~CTL_BE_RAMDISK_LUN_UNCONFIGURED;
+
+	/*
+	 * If we have a user waiting, let him handle the cleanup.  If not,
+	 * clean things up here.
+	 */
+	if (lun->flags & CTL_BE_RAMDISK_LUN_WAITING) {
+		lun->flags |= CTL_BE_RAMDISK_LUN_CONFIG_ERR;
+		wakeup(lun);
+	} else {
+		STAILQ_REMOVE(&softc->lun_list, lun, ctl_be_ramdisk_lun,
+			      links);
+		softc->num_luns--;
+		free(lun, M_RAMDISK);
+	}
+	mtx_unlock(&softc->lock);
+}
+
+static int
+ctl_backend_ramdisk_config_write(union ctl_io *io)
+{
+	struct ctl_be_ramdisk_softc *softc;
+	int retval;
+
+	retval = 0;
+	softc = &rd_softc;
+
+	switch (io->scsiio.cdb[0]) {
+	case SYNCHRONIZE_CACHE:
+	case SYNCHRONIZE_CACHE_16:
+		/*
+		 * The upper level CTL code will filter out any CDBs with
+		 * the immediate bit set and return the proper error.  It
+		 * will also not allow a sync cache command to go to a LUN
+		 * that is powered down.
+		 *
+		 * We don't really need to worry about what LBA range the
+		 * user asked to be synced out.  When they issue a sync
+		 * cache command, we'll sync out the whole thing.
+		 *
+		 * This is obviously just a stubbed out implementation.
+		 * The real implementation will be in the RAIDCore/CTL
+		 * interface, and can only really happen when RAIDCore
+		 * implements a per-array cache sync.
+		 */
+		ctl_set_success(&io->scsiio);
+		ctl_config_write_done(io);
+		break;
+	case START_STOP_UNIT: {
+		struct scsi_start_stop_unit *cdb;
+		struct ctl_be_lun *ctl_be_lun;
+		struct ctl_be_ramdisk_lun *be_lun;
+
+		cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb;
+
+		ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
+			CTL_PRIV_BACKEND_LUN].ptr;
+		be_lun = (struct ctl_be_ramdisk_lun *)ctl_be_lun->be_lun;
+
+		if (cdb->how & SSS_START)
+			retval = ctl_start_lun(ctl_be_lun);
+		else {
+			retval = ctl_stop_lun(ctl_be_lun);
+#ifdef NEEDTOPORT
+			if ((retval == 0)
+			 && (cdb->byte2 & SSS_ONOFFLINE))
+				retval = ctl_lun_offline(ctl_be_lun);
+#endif
+		}
+
+		/*
+		 * In general, the above routines should not fail.  They
+		 * just set state for the LUN.  So we've got something
+		 * pretty wrong here if we can't start or stop the LUN.
+		 */
+		if (retval != 0) {
+			ctl_set_internal_failure(&io->scsiio,
+						 /*sks_valid*/ 1,
+						 /*retry_count*/ 0xf051);
+			retval = CTL_RETVAL_COMPLETE;
+		} else {
+			ctl_set_success(&io->scsiio);
+		}
+		ctl_config_write_done(io);
+		break;
+	}
+	default:
+		ctl_set_invalid_opcode(&io->scsiio);
+		ctl_config_write_done(io);
+		retval = CTL_RETVAL_COMPLETE;
+		break;
+	}
+
+	return (retval);
+}
+
+static int
+ctl_backend_ramdisk_config_read(union ctl_io *io)
+{
+	/*
+	 * XXX KDM need to implement!!
+	 */
+	return (0);
+}
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/cam/ctl/ctl_cmd_table.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/sys/cam/ctl/ctl_cmd_table.c	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,984 @@
+/*-
+ * Copyright (c) 2003, 2004, 2005, 2009 Silicon Graphics International Corp.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions, and the following disclaimer,
+ *    without modification.
+ * 2. Redistributions in binary form must reproduce at minimum a disclaimer
+ *    substantially similar to the "NO WARRANTY" disclaimer below
+ *    ("Disclaimer") and any redistribution must be conditioned upon
+ *    including a substantially similar Disclaimer requirement for further
+ *    binary redistribution.
+ *
+ * NO WARRANTY
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGES.
+ *
+ * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_cmd_table.c#4 $
+ * $FreeBSD: head/sys/cam/ctl/ctl_cmd_table.c 229997 2012-01-12 00:34:33Z ken $
+ */
+/*
+ * CAM Target Layer command table.
+ *
+ * Author: Ken Merry <ken at FreeBSD.org>, Kim Le
+ */
+
+#include <sys/cdefs.h>
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/kernel.h>
+#include <sys/types.h>
+#include <sys/malloc.h>
+#include <sys/condvar.h>
+#include <sys/queue.h>
+
+#include <cam/scsi/scsi_all.h>
+#include <cam/scsi/scsi_da.h>
+#include <cam/ctl/ctl_io.h>
+#include <cam/ctl/ctl.h>
+#include <cam/ctl/ctl_frontend.h>
+#include <cam/ctl/ctl_backend.h>
+#include <cam/ctl/ctl_frontend_internal.h>
+#include <cam/ctl/ctl_ioctl.h>
+#include <cam/ctl/ctl_ha.h>
+#include <cam/ctl/ctl_private.h>
+
+/*
+ * Whenever support for a new command is added, it should be added to this
+ * table.
+ */
+struct ctl_cmd_entry ctl_cmd_table[] =
+{
+/* 00 TEST UNIT READY */
+{ctl_tur, CTL_SERIDX_TUR, CTL_CMD_FLAG_OK_ON_BOTH |
+			  CTL_FLAG_DATA_NONE |
+			  CTL_CMD_FLAG_ALLOW_ON_PR_RESV,
+ CTL_LUN_PAT_TUR},
+
+/* 01 REWIND */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 02 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 03 REQUEST SENSE */
+{ctl_request_sense, CTL_SERIDX_RQ_SNS, CTL_FLAG_DATA_IN |
+				       CTL_CMD_FLAG_OK_ON_ALL_LUNS |
+				       CTL_CMD_FLAG_ALLOW_ON_RESV |
+				       CTL_CMD_FLAG_NO_SENSE |
+				       CTL_CMD_FLAG_OK_ON_STOPPED |
+				       CTL_CMD_FLAG_OK_ON_INOPERABLE |
+				       CTL_CMD_FLAG_OK_ON_OFFLINE |
+				       CTL_CMD_FLAG_OK_ON_SECONDARY |
+				       CTL_CMD_FLAG_ALLOW_ON_PR_RESV,
+ CTL_LUN_PAT_NONE},
+
+/* 04 FORMAT UNIT */
+{ctl_format, CTL_SERIDX_FORMAT, CTL_CMD_FLAG_OK_ON_SLUN |
+				CTL_CMD_FLAG_OK_ON_INOPERABLE |
+				CTL_FLAG_DATA_OUT,
+ CTL_LUN_PAT_NONE},
+
+/* 05 READ BLOCK LIMITS */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 06 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 07 REASSIGN BLOCKS */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 08 READ(6) */
+{ctl_read_write, CTL_SERIDX_READ, CTL_CMD_FLAG_OK_ON_SLUN |
+				  CTL_FLAG_DATA_IN |
+				  CTL_CMD_FLAG_ALLOW_ON_PR_RESV,
+ CTL_LUN_PAT_READ | CTL_LUN_PAT_RANGE},
+
+/* 09 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 0A WRITE(6) */
+{ctl_read_write, CTL_SERIDX_WRITE, CTL_CMD_FLAG_OK_ON_SLUN |
+				   CTL_FLAG_DATA_OUT,
+ CTL_LUN_PAT_WRITE | CTL_LUN_PAT_RANGE},
+
+/* 0B SEEK(6) */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 0C */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 0D */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 0E */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 0F READ REVERSE(6) */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 10 WRITE FILEMARKS(6) */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 11 SPACE(6) */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 12 INQUIRY */
+{ctl_inquiry, CTL_SERIDX_INQ, CTL_CMD_FLAG_OK_ON_ALL_LUNS |
+			      CTL_CMD_FLAG_ALLOW_ON_RESV |
+			      CTL_CMD_FLAG_NO_SENSE |
+			      CTL_CMD_FLAG_OK_ON_STOPPED |
+			      CTL_CMD_FLAG_OK_ON_INOPERABLE |
+			      CTL_CMD_FLAG_OK_ON_OFFLINE |
+			      CTL_CMD_FLAG_OK_ON_SECONDARY |
+			      CTL_FLAG_DATA_IN |
+			      CTL_CMD_FLAG_ALLOW_ON_PR_RESV,
+ CTL_LUN_PAT_NONE},
+
+/* 13 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 14 RECOVER BUFFERED DATA */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 15 MODE SELECT(6) */
+{ctl_mode_select, CTL_SERIDX_MD_SEL, CTL_CMD_FLAG_OK_ON_BOTH |
+				     CTL_CMD_FLAG_OK_ON_STOPPED |
+				     CTL_CMD_FLAG_OK_ON_INOPERABLE |
+				     CTL_CMD_FLAG_OK_ON_SECONDARY |
+				     CTL_FLAG_DATA_OUT,
+ CTL_LUN_PAT_NONE},
+
+/* 16 RESERVE(6) */
+{ctl_scsi_reserve, CTL_SERIDX_RESV, CTL_CMD_FLAG_ALLOW_ON_RESV |
+				    CTL_CMD_FLAG_OK_ON_BOTH |
+				    CTL_CMD_FLAG_OK_ON_STOPPED |
+				    CTL_CMD_FLAG_OK_ON_INOPERABLE |
+				    CTL_CMD_FLAG_OK_ON_SECONDARY |
+				    CTL_FLAG_DATA_OUT,
+ CTL_LUN_PAT_NONE},
+
+/* 17 RELEASE(6) */
+{ctl_scsi_release, CTL_SERIDX_REL, CTL_CMD_FLAG_ALLOW_ON_RESV |
+				   CTL_CMD_FLAG_OK_ON_BOTH |
+				   CTL_CMD_FLAG_OK_ON_STOPPED |
+				   CTL_CMD_FLAG_OK_ON_INOPERABLE |
+				   CTL_CMD_FLAG_OK_ON_SECONDARY |
+				   CTL_FLAG_DATA_NONE,
+ CTL_LUN_PAT_NONE},
+
+/* 18 COPY */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 19 ERASE(6) */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 1A MODE SENSE(6) */
+{ctl_mode_sense, CTL_SERIDX_MD_SNS, CTL_CMD_FLAG_OK_ON_BOTH |
+				    CTL_CMD_FLAG_OK_ON_STOPPED |
+				    CTL_CMD_FLAG_OK_ON_INOPERABLE |
+				    CTL_CMD_FLAG_OK_ON_SECONDARY |
+				    CTL_FLAG_DATA_IN,
+ CTL_LUN_PAT_NONE},
+
+/* 1B START STOP UNIT */
+{ctl_start_stop, CTL_SERIDX_START, CTL_CMD_FLAG_OK_ON_SLUN |
+				   CTL_CMD_FLAG_OK_ON_STOPPED |
+				   CTL_CMD_FLAG_OK_ON_INOPERABLE |
+				   CTL_CMD_FLAG_OK_ON_OFFLINE |
+				   CTL_FLAG_DATA_NONE |
+				   CTL_CMD_FLAG_ALLOW_ON_PR_RESV,
+ CTL_LUN_PAT_NONE},
+
+/* 1C RECEIVE DIAGNOSTIC RESULTS */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 1D SEND DIAGNOSTIC */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 1E PREVENT ALLOW MEDIUM REMOVAL */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 1F */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 20 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 21 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 22 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 23 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 24 SET WINDOW */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 25 READ CAPACITY(10) */
+{ctl_read_capacity, CTL_SERIDX_RD_CAP, CTL_CMD_FLAG_OK_ON_SLUN|
+				       CTL_CMD_FLAG_OK_ON_STOPPED |
+				       CTL_CMD_FLAG_OK_ON_INOPERABLE |
+				       CTL_CMD_FLAG_OK_ON_SECONDARY |
+				       CTL_FLAG_DATA_IN |
+				       CTL_CMD_FLAG_ALLOW_ON_PR_RESV,
+ CTL_LUN_PAT_READCAP},
+
+/* 26 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 27 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 28 READ(10) */
+{ctl_read_write, CTL_SERIDX_READ, CTL_CMD_FLAG_OK_ON_SLUN |
+				  CTL_FLAG_DATA_IN |
+				  CTL_CMD_FLAG_ALLOW_ON_PR_RESV,
+ CTL_LUN_PAT_READ | CTL_LUN_PAT_RANGE},
+
+/* 29 READ GENERATION */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 2A WRITE(10) */
+{ctl_read_write, CTL_SERIDX_WRITE, CTL_CMD_FLAG_OK_ON_SLUN| CTL_FLAG_DATA_OUT,
+ CTL_LUN_PAT_WRITE | CTL_LUN_PAT_RANGE},
+
+/* 2B SEEK(10) */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 2C ERASE(10) */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 2D READ UPDATED BLOCK */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 2E WRITE AND VERIFY(10) */
+{ctl_read_write, CTL_SERIDX_WRITE, CTL_CMD_FLAG_OK_ON_SLUN| CTL_FLAG_DATA_OUT,
+ CTL_LUN_PAT_WRITE | CTL_LUN_PAT_RANGE},
+
+/* 2F VERIFY(10) */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 30 SEARCH DATA HIGH(10) */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 31 SEARCH DATA EQUAL(10) */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 32 SEARCH DATA LOW(10) */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 33 SET LIMITS(10) */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 34 PRE-FETCH(10) */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 35 SYNCHRONIZE CACHE(10) */
+{ctl_sync_cache, CTL_SERIDX_START, CTL_CMD_FLAG_OK_ON_SLUN |
+				   CTL_FLAG_DATA_NONE,
+ CTL_LUN_PAT_NONE},
+
+/* 36 LOCK UNLOCK CACHE(10) */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 37 READ DEFECT DATA(10) */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 38 MEDIUM SCAN */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 39 COMPARE */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 3A COPY AND VERIFY */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 3B WRITE BUFFER */
+{ctl_write_buffer, CTL_SERIDX_WRITE, CTL_CMD_FLAG_OK_ON_PROC |
+				     CTL_FLAG_DATA_OUT,
+ CTL_LUN_PAT_NONE},
+
+/* 3C READ BUFFER */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 3D UPDATE BLOCK */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 3E READ LONG */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 3F WRITE LONG */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 40 CHANGE DEFINITION */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 41 WRITE SAME(10) */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 42 READ SUB-CHANNEL */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 43 READ TOC/PMA/ATIP */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 44 REPORT DENSITY SUPPORT */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 45 PLAY AUDIO(10) */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 46 GET CONFIGURATION */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 47 PLAY AUDIO MSF */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 48 PLAY AUDIO TRACK INDEX */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 49 PLAY TRACK RELATIVE(10) */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 4A GET EVENT STATUS NOTIFICATION */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 4B PAUSE/RESUME */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 4C LOG SELECT */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 4D LOG SENSE */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 4E STOP PLAY/SCAN */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 4F */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 50 XDWRITE(10) */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 51 XPWRITE(10) */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 52 XDREAD(10) */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 53 RESERVE TRACK */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 54 SEND OPC INFORMATION */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 55 MODE SELECT(10) */
+{ctl_mode_select, CTL_SERIDX_MD_SEL, CTL_CMD_FLAG_OK_ON_BOTH |
+				     CTL_CMD_FLAG_OK_ON_STOPPED |
+				     CTL_CMD_FLAG_OK_ON_INOPERABLE |
+				     CTL_CMD_FLAG_OK_ON_SECONDARY |
+				     CTL_FLAG_DATA_OUT,
+ CTL_LUN_PAT_NONE},
+
+/* 56 RESERVE(10) */
+{ctl_scsi_reserve, CTL_SERIDX_RESV, CTL_CMD_FLAG_ALLOW_ON_RESV |
+				    CTL_CMD_FLAG_OK_ON_BOTH |
+				    CTL_CMD_FLAG_OK_ON_STOPPED |
+				    CTL_CMD_FLAG_OK_ON_INOPERABLE |
+				    CTL_CMD_FLAG_OK_ON_SECONDARY |
+				    CTL_FLAG_DATA_OUT,
+ CTL_LUN_PAT_NONE},
+
+/* 57 RELEASE(10) */
+{ctl_scsi_release, CTL_SERIDX_REL, CTL_CMD_FLAG_ALLOW_ON_RESV |
+				   CTL_CMD_FLAG_OK_ON_BOTH |
+				   CTL_CMD_FLAG_OK_ON_STOPPED |
+				   CTL_CMD_FLAG_OK_ON_INOPERABLE |
+				   CTL_CMD_FLAG_OK_ON_SECONDARY |
+				   CTL_FLAG_DATA_OUT,
+ CTL_LUN_PAT_NONE},
+
+/* 58 REPAIR TRACK */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 59 READ MASTER CUE */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 5A MODE SENSE(10) */
+{ctl_mode_sense, CTL_SERIDX_MD_SNS, CTL_CMD_FLAG_OK_ON_BOTH |
+				    CTL_CMD_FLAG_OK_ON_STOPPED |
+				    CTL_CMD_FLAG_OK_ON_INOPERABLE |
+				    CTL_CMD_FLAG_OK_ON_SECONDARY |
+				    CTL_FLAG_DATA_IN,
+ CTL_LUN_PAT_NONE},
+
+/* 5B CLOSE TRACK/SESSION */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 5C READ BUFFER CAPACITY */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 5D SEND CUE SHEET */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 5E PERSISTENT RESERVE IN */
+{ctl_persistent_reserve_in, CTL_SERIDX_PRES_IN, CTL_CMD_FLAG_ALLOW_ON_RESV |
+						CTL_CMD_FLAG_OK_ON_BOTH |
+						CTL_CMD_FLAG_OK_ON_STOPPED |
+						CTL_CMD_FLAG_OK_ON_INOPERABLE |
+						CTL_CMD_FLAG_OK_ON_SECONDARY |
+						CTL_FLAG_DATA_IN |
+						CTL_CMD_FLAG_ALLOW_ON_PR_RESV,
+ CTL_LUN_PAT_NONE},
+
+//{ctl_persistent_reserve_in, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE},
+
+/* 5F PERSISTENT RESERVE OUT */
+{ctl_persistent_reserve_out, CTL_SERIDX_PRES_OUT, CTL_CMD_FLAG_ALLOW_ON_RESV |
+						  CTL_CMD_FLAG_OK_ON_BOTH |
+						  CTL_CMD_FLAG_OK_ON_STOPPED |
+						  CTL_CMD_FLAG_OK_ON_INOPERABLE|
+						  CTL_CMD_FLAG_OK_ON_SECONDARY |
+						  CTL_FLAG_DATA_OUT |
+						  CTL_CMD_FLAG_ALLOW_ON_PR_RESV,
+ CTL_LUN_PAT_NONE},
+
+//{ctl_persistent_reserve_out, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE},
+
+/* 60 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 61 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 62 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 63 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 64 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 65 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 66 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 67 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 68 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 69 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 6A */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 6B */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 6C */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 6D */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 6E */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 6F */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 70 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 71 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 72 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 73 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 74 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 75 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 76 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 77 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 78 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 79 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 7A */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 7B */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 7C */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 7D */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 7E */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 7F */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 80 XDWRITE EXTENDED(16) */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 81 REBUILD(16) */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 82 REGENERATE(16) */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 83 EXTENDED COPY */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 84 RECEIVE COPY RESULTS */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 85 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 86 ACCESS CONTROL IN */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 87 ACCESS CONTROL OUT */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 88 READ(16) */
+{ctl_read_write, CTL_SERIDX_READ, CTL_CMD_FLAG_OK_ON_SLUN | CTL_FLAG_DATA_IN |
+                                  CTL_CMD_FLAG_ALLOW_ON_PR_RESV,
+ CTL_LUN_PAT_READ | CTL_LUN_PAT_RANGE},
+
+/* 89 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 8A WRITE(16) */
+{ctl_read_write, CTL_SERIDX_WRITE, CTL_CMD_FLAG_OK_ON_SLUN| CTL_FLAG_DATA_OUT,
+ CTL_LUN_PAT_WRITE | CTL_LUN_PAT_RANGE},
+
+/* 8B */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 8C READ ATTRIBUTE */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 8D WRITE ATTRIBUTE */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 8E WRITE AND VERIFY(16) */
+{ctl_read_write, CTL_SERIDX_WRITE, CTL_CMD_FLAG_OK_ON_SLUN| CTL_FLAG_DATA_OUT,
+ CTL_LUN_PAT_WRITE | CTL_LUN_PAT_RANGE},
+
+/* 8F VERIFY(16) */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 90 PRE-FETCH(16) */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 91 SYNCHRONIZE CACHE(16) */
+{ctl_sync_cache, CTL_SERIDX_START, CTL_CMD_FLAG_OK_ON_SLUN |
+				   CTL_FLAG_DATA_NONE,
+ CTL_LUN_PAT_NONE},
+
+/* 92 LOCK UNLOCK CACHE(16) */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 93 WRITE SAME(16) */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 94 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 95 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 96 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 97 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 98 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 99 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 9A */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 9B */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 9C */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 9D */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* 9E SERVICE ACTION IN(16) */
+/* XXX KDM not all service actions will be read capacity!! */
+{ctl_service_action_in, CTL_SERIDX_RD_CAP, CTL_CMD_FLAG_OK_ON_SLUN |
+					   CTL_CMD_FLAG_OK_ON_STOPPED |
+					   CTL_CMD_FLAG_OK_ON_INOPERABLE |
+					   CTL_CMD_FLAG_OK_ON_SECONDARY |
+					   CTL_FLAG_DATA_IN |
+					   CTL_CMD_FLAG_ALLOW_ON_PR_RESV,
+ CTL_LUN_PAT_READCAP},
+
+/* 9F SERVICE ACTION OUT(16) */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* A0 REPORT LUNS */
+{ctl_report_luns, CTL_SERIDX_INQ, CTL_CMD_FLAG_OK_ON_ALL_LUNS |
+				  CTL_CMD_FLAG_ALLOW_ON_RESV |
+				  CTL_CMD_FLAG_NO_SENSE |
+				  CTL_CMD_FLAG_OK_ON_STOPPED |
+				  CTL_CMD_FLAG_OK_ON_INOPERABLE |
+				  CTL_CMD_FLAG_OK_ON_OFFLINE |
+				  CTL_CMD_FLAG_OK_ON_SECONDARY |
+				  CTL_FLAG_DATA_IN |
+				  CTL_CMD_FLAG_ALLOW_ON_PR_RESV,
+ CTL_LUN_PAT_NONE},
+
+/* A1 BLANK */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* A2 SEND EVENT */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* A3 MAINTENANCE (IN) Service Action - (0A) REPORT TARGET PORT GROUP */
+{ctl_maintenance_in, CTL_SERIDX_MAIN_IN, CTL_CMD_FLAG_OK_ON_BOTH |
+				         CTL_CMD_FLAG_OK_ON_STOPPED |
+				         CTL_CMD_FLAG_OK_ON_INOPERABLE |
+				         CTL_CMD_FLAG_OK_ON_SECONDARY |
+				         CTL_FLAG_DATA_IN,
+ CTL_LUN_PAT_NONE},
+
+/* A4 MAINTENANCE (OUT) */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* A5 MOVE MEDIUM */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* A6 EXCHANGE MEDIUM */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* A7 MOVE MEDIUM ATTACHED */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* A8 READ(12) */
+{ctl_read_write, CTL_SERIDX_READ, CTL_CMD_FLAG_OK_ON_SLUN | CTL_FLAG_DATA_IN |
+                                  CTL_CMD_FLAG_ALLOW_ON_PR_RESV,
+ CTL_LUN_PAT_READ | CTL_LUN_PAT_RANGE},
+
+/* A9 PLAY TRACK RELATIVE(12) */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* AA WRITE(12) */
+{ctl_read_write, CTL_SERIDX_WRITE, CTL_CMD_FLAG_OK_ON_SLUN| CTL_FLAG_DATA_OUT,
+ CTL_LUN_PAT_WRITE | CTL_LUN_PAT_RANGE},
+
+/* AB SERVICE ACTION IN(12) */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* AC ERASE(12) */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* AD READ DVD STRUCTURE */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* AE WRITE AND VERIFY(12) */
+{ctl_read_write, CTL_SERIDX_WRITE, CTL_CMD_FLAG_OK_ON_SLUN| CTL_FLAG_DATA_OUT,
+ CTL_LUN_PAT_WRITE | CTL_LUN_PAT_RANGE},
+
+/* AF VERIFY(12) */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* B0 SEARCH DATA HIGH(12) */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* B1 SEARCH DATA EQUAL(12) */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* B2 SEARCH DATA LOW(12) */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* B3 SET LIMITS(12) */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* B4 READ ELEMENT STATUS ATTACHED */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* B5 REQUEST VOLUME ELEMENT ADDRESS */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* B6 SEND VOLUME TAG */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* B7 READ DEFECT DATA(12) */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* B8 READ ELEMENT STATUS */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* B9 READ CD MSF */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* BA REDUNDANCY GROUP (IN) */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* BB REDUNDANCY GROUP (OUT) */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* BC SPARE (IN) */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* BD SPARE (OUT) */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* BE VOLUME SET (IN) */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* BF VOLUME SET (OUT) */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* C0 - ISC_SEND_MSG_SHORT */
+//{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE},
+{ctl_isc, CTL_SERIDX_READ, CTL_CMD_FLAG_OK_ON_PROC | CTL_FLAG_DATA_NONE,
+ CTL_LUN_PAT_NONE},
+
+/* C1 - ISC_SEND_MSG */
+//{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE},
+{ctl_isc, CTL_SERIDX_READ, CTL_CMD_FLAG_OK_ON_PROC | CTL_FLAG_DATA_OUT,
+ CTL_LUN_PAT_NONE},
+
+/* C2 - ISC_WRITE */
+//{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE},
+{ctl_isc, CTL_SERIDX_READ, CTL_CMD_FLAG_OK_ON_PROC | CTL_FLAG_DATA_OUT,
+ CTL_LUN_PAT_NONE},
+
+/* C3 - ISC_READ */
+//{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE},
+{ctl_isc, CTL_SERIDX_READ, CTL_CMD_FLAG_OK_ON_PROC | CTL_FLAG_DATA_IN,
+ CTL_LUN_PAT_NONE},
+
+/* C4 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* C5 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* C6 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* C7 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* C8 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* C9 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* CA */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* CB */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* CC */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* CD */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* CE */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* CF */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* D0 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* D1 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* D2 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* D3 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* D4 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* D5 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* D6 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* D7 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* D8 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* D9 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* DA */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* DB */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* DC */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* DD */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* DE */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* DF */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* E0 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* E1 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* E2 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* E3 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* E4 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* E5 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* E6 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* E7 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* E8 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* E9 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* EA */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* EB */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* EC */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* ED */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* EE */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* EF */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* F0 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* F1 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* F2 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* F3 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* F4 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* F5 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* F6 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* F7 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* F8 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* F9 */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* FA */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* FB */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* FC */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* FD */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* FE */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE},
+
+/* FF */
+{NULL, CTL_SERIDX_INVLD, CTL_CMD_FLAG_NONE, CTL_LUN_PAT_NONE}
+
+};
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/cam/ctl/ctl_debug.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/sys/cam/ctl/ctl_debug.h	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,52 @@
+/*-
+ * Copyright (c) 2003 Silicon Graphics International Corp.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions, and the following disclaimer,
+ *    without modification.
+ * 2. Redistributions in binary form must reproduce at minimum a disclaimer
+ *    substantially similar to the "NO WARRANTY" disclaimer below
+ *    ("Disclaimer") and any redistribution must be conditioned upon
+ *    including a substantially similar Disclaimer requirement for further
+ *    binary redistribution.
+ *
+ * NO WARRANTY
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGES.
+ *
+ * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_debug.h#2 $
+ * $FreeBSD: head/sys/cam/ctl/ctl_debug.h 229997 2012-01-12 00:34:33Z ken $
+ */
+/*
+ * CAM Target Layer debugging interface.
+ *
+ * Author: Ken Merry <ken at FreeBSD.org>
+ */
+
+#ifndef	_CTL_DEBUG_H_
+#define	_CTL_DEBUG_H_
+
+#ifdef	CAM_CTL_DEBUG
+#define	CTL_DEBUG_PRINT(X)		\
+	do {				\
+		printf("ctl_debug: ");	\
+		printf X;		\
+	} while (0)
+#else /* CAM_CTL_DEBUG */
+#define	CTL_DEBUG_PRINT(X)
+#endif /* CAM_CTL_DEBUG */
+
+#endif	/* _CTL_DEBUG_H_ */
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/cam/ctl/ctl_error.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/sys/cam/ctl/ctl_error.c	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,811 @@
+/*-
+ * Copyright (c) 2003-2009 Silicon Graphics International Corp.
+ * Copyright (c) 2011 Spectra Logic Corporation
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions, and the following disclaimer,
+ *    without modification.
+ * 2. Redistributions in binary form must reproduce at minimum a disclaimer
+ *    substantially similar to the "NO WARRANTY" disclaimer below
+ *    ("Disclaimer") and any redistribution must be conditioned upon
+ *    including a substantially similar Disclaimer requirement for further
+ *    binary redistribution.
+ *
+ * NO WARRANTY
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGES.
+ *
+ * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_error.c#2 $
+ */
+/*
+ * CAM Target Layer error reporting routines.
+ *
+ * Author: Ken Merry <ken at FreeBSD.org>
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD: head/sys/cam/ctl/ctl_error.c 229997 2012-01-12 00:34:33Z ken $");
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/kernel.h>
+#include <sys/types.h>
+#include <sys/malloc.h>
+#include <sys/lock.h>
+#include <sys/mutex.h>
+#include <sys/condvar.h>
+#include <sys/stddef.h>
+#include <sys/ctype.h>
+#include <machine/stdarg.h>
+
+#include <cam/scsi/scsi_all.h>
+#include <cam/scsi/scsi_da.h>
+#include <cam/ctl/ctl_io.h>
+#include <cam/ctl/ctl.h>
+#include <cam/ctl/ctl_frontend.h>
+#include <cam/ctl/ctl_frontend_internal.h>
+#include <cam/ctl/ctl_backend.h>
+#include <cam/ctl/ctl_ioctl.h>
+#include <cam/ctl/ctl_error.h>
+#include <cam/ctl/ctl_ha.h>
+#include <cam/ctl/ctl_private.h>
+
+void
+ctl_set_sense_data_va(struct scsi_sense_data *sense_data, void *lunptr,
+		      scsi_sense_data_type sense_format, int current_error,
+		      int sense_key, int asc, int ascq, va_list ap) 
+{
+	struct ctl_lun *lun;
+
+	lun = (struct ctl_lun *)lunptr;
+
+	/*
+	 * Determine whether to return fixed or descriptor format sense
+	 * data.
+	 */
+	if (sense_format == SSD_TYPE_NONE) {
+		/*
+		 * If the format isn't specified, we only return descriptor
+		 * sense if the LUN exists and descriptor sense is turned
+		 * on for that LUN.
+		 */
+		if ((lun != NULL)
+		 && (lun->flags & CTL_LUN_SENSE_DESC))
+			sense_format = SSD_TYPE_DESC;
+		else
+			sense_format = SSD_TYPE_FIXED;
+	}
+
+	scsi_set_sense_data_va(sense_data, sense_format, current_error,
+			       sense_key, asc, ascq, ap);
+}
+
+void
+ctl_set_sense_data(struct scsi_sense_data *sense_data, void *lunptr,
+		   scsi_sense_data_type sense_format, int current_error,
+		   int sense_key, int asc, int ascq, ...) 
+{
+	va_list ap;
+
+	va_start(ap, ascq);
+	ctl_set_sense_data_va(sense_data, lunptr, sense_format, current_error,
+			      sense_key, asc, ascq, ap);
+	va_end(ap);
+}
+
+void
+ctl_set_sense(struct ctl_scsiio *ctsio, int current_error, int sense_key,
+	      int asc, int ascq, ...)
+{
+	va_list ap;
+	struct ctl_lun *lun;
+
+	/*
+	 * The LUN can't go away until all of the commands have been
+	 * completed.  Therefore we can safely access the LUN structure and
+	 * flags without the lock.
+	 */
+	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
+
+	va_start(ap, ascq);
+	ctl_set_sense_data_va(&ctsio->sense_data,
+			      lun,
+			      SSD_TYPE_NONE,
+			      current_error,
+			      sense_key,
+			      asc,
+			      ascq,
+			      ap);
+	va_end(ap);
+
+	ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
+	ctsio->sense_len = SSD_FULL_SIZE;
+	ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
+}
+
+/*
+ * Transform fixed sense data into descriptor sense data.
+ * 
+ * For simplicity's sake, we assume that both sense structures are
+ * SSD_FULL_SIZE.  Otherwise, the logic gets more complicated.
+ */
+void
+ctl_sense_to_desc(struct scsi_sense_data_fixed *sense_src,
+		  struct scsi_sense_data_desc *sense_dest)
+{
+	struct scsi_sense_stream stream_sense;
+	int current_error;
+	uint8_t stream_bits;
+
+	bzero(sense_dest, sizeof(*sense_dest));
+
+	if ((sense_src->error_code & SSD_ERRCODE) == SSD_DEFERRED_ERROR)
+		current_error = 0;
+	else
+		current_error = 1;
+
+	bzero(&stream_sense, sizeof(stream_sense));
+
+	/*
+	 * Check to see whether any of the tape-specific bits are set.  If
+	 * so, we'll need a stream sense descriptor.
+	 */
+	if (sense_src->flags & (SSD_ILI|SSD_EOM|SSD_FILEMARK))
+		stream_bits = sense_src->flags & ~SSD_KEY;
+	else
+		stream_bits = 0;
+
+	/*
+	 * Utilize our sense setting routine to do the transform.  If a
+	 * value is set in the fixed sense data, set it in the descriptor
+	 * data.  Otherwise, skip it.
+	 */
+	ctl_set_sense_data((struct scsi_sense_data *)sense_dest,
+			   /*lun*/ NULL,
+			   /*sense_format*/ SSD_TYPE_DESC,
+			   current_error,
+			   /*sense_key*/ sense_src->flags & SSD_KEY,
+			   /*asc*/ sense_src->add_sense_code,
+			   /*ascq*/ sense_src->add_sense_code_qual,
+
+			   /* Information Bytes */ 
+			   (scsi_4btoul(sense_src->info) != 0) ?
+			   SSD_ELEM_INFO : SSD_ELEM_SKIP,
+			   sizeof(sense_src->info),
+			   sense_src->info,
+
+			   /* Command specific bytes */
+			   (scsi_4btoul(sense_src->cmd_spec_info) != 0) ?
+			   SSD_ELEM_COMMAND : SSD_ELEM_SKIP,
+			   sizeof(sense_src->cmd_spec_info),
+			   sense_src->cmd_spec_info,
+
+			   /* FRU */
+			   (sense_src->fru != 0) ?
+			   SSD_ELEM_FRU : SSD_ELEM_SKIP,
+			   sizeof(sense_src->fru),
+			   &sense_src->fru,
+
+			   /* Sense Key Specific */
+			   (sense_src->sense_key_spec[0] & SSD_SCS_VALID) ?
+			   SSD_ELEM_SKS : SSD_ELEM_SKIP,
+			   sizeof(sense_src->sense_key_spec),
+			   sense_src->sense_key_spec,
+
+			   /* Tape bits */
+			   (stream_bits != 0) ?
+			   SSD_ELEM_STREAM : SSD_ELEM_SKIP,
+			   sizeof(stream_bits),
+			   &stream_bits,
+
+			   SSD_ELEM_NONE);
+}
+
+/*
+ * Transform descriptor format sense data into fixed sense data.
+ *
+ * Some data may be lost in translation, because there are descriptors
+ * thant can't be represented as fixed sense data.
+ *
+ * For simplicity's sake, we assume that both sense structures are
+ * SSD_FULL_SIZE.  Otherwise, the logic gets more complicated.
+ */
+void
+ctl_sense_to_fixed(struct scsi_sense_data_desc *sense_src,
+		   struct scsi_sense_data_fixed *sense_dest)
+{
+	int current_error;
+	uint8_t *info_ptr = NULL, *cmd_ptr = NULL, *fru_ptr = NULL;
+	uint8_t *sks_ptr = NULL, *stream_ptr = NULL;
+	int info_size = 0, cmd_size = 0, fru_size = 0;
+	int sks_size = 0, stream_size = 0;
+	int pos;
+
+	if ((sense_src->error_code & SSD_ERRCODE) == SSD_DESC_CURRENT_ERROR)
+		current_error = 1;
+	else
+		current_error = 0;
+
+	for (pos = 0; pos < (int)(sense_src->extra_len - 1);) {
+		struct scsi_sense_desc_header *header;
+
+		header = (struct scsi_sense_desc_header *)
+		    &sense_src->sense_desc[pos];
+
+		/*
+		 * See if this record goes past the end of the sense data.
+		 * It shouldn't, but check just in case.
+		 */
+		if ((pos + header->length + sizeof(*header)) >
+		     sense_src->extra_len)
+			break;
+
+		switch (sense_src->sense_desc[pos]) {
+		case SSD_DESC_INFO: {
+			struct scsi_sense_info *info;
+
+			info = (struct scsi_sense_info *)header;
+
+			info_ptr = info->info;
+			info_size = sizeof(info->info);
+
+			pos += info->length +
+			    sizeof(struct scsi_sense_desc_header);
+			break;
+		}
+		case SSD_DESC_COMMAND: {
+			struct scsi_sense_command *cmd;
+
+			cmd = (struct scsi_sense_command *)header;
+			cmd_ptr = cmd->command_info;
+			cmd_size = sizeof(cmd->command_info);
+
+			pos += cmd->length + 
+			    sizeof(struct scsi_sense_desc_header);
+			break;
+		}
+		case SSD_DESC_FRU: {
+			struct scsi_sense_fru *fru;
+
+			fru = (struct scsi_sense_fru *)header;
+			fru_ptr = &fru->fru;
+			fru_size = sizeof(fru->fru);
+			pos += fru->length +
+			    sizeof(struct scsi_sense_desc_header);
+			break;
+		}
+		case SSD_DESC_SKS: {
+			struct scsi_sense_sks *sks;
+
+			sks = (struct scsi_sense_sks *)header;
+			sks_ptr = sks->sense_key_spec;
+			sks_size = sizeof(sks->sense_key_spec);
+
+			pos = sks->length +
+			    sizeof(struct scsi_sense_desc_header);
+			break;
+		}
+		case SSD_DESC_STREAM: {
+			struct scsi_sense_stream *stream_sense;
+
+			stream_sense = (struct scsi_sense_stream *)header;
+			stream_ptr = &stream_sense->byte3;
+			stream_size = sizeof(stream_sense->byte3);
+			pos = stream_sense->length +
+			    sizeof(struct scsi_sense_desc_header);
+			break;
+		}
+		default:
+			/*
+			 * We don't recognize this particular sense
+			 * descriptor type, so just skip it.
+			 */
+			pos += sizeof(*header) + header->length;
+			break;
+		}
+	}
+
+	ctl_set_sense_data((struct scsi_sense_data *)sense_dest,
+			   /*lun*/ NULL,
+			   /*sense_format*/ SSD_TYPE_FIXED,
+			   current_error,
+			   /*sense_key*/ sense_src->sense_key & SSD_KEY,
+			   /*asc*/ sense_src->add_sense_code,
+			   /*ascq*/ sense_src->add_sense_code_qual,
+
+			   /* Information Bytes */ 
+			   (info_ptr != NULL) ? SSD_ELEM_INFO : SSD_ELEM_SKIP,
+			   info_size,
+			   info_ptr,
+
+			   /* Command specific bytes */
+			   (cmd_ptr != NULL) ? SSD_ELEM_COMMAND : SSD_ELEM_SKIP,
+			   cmd_size,
+			   cmd_ptr,
+
+			   /* FRU */
+			   (fru_ptr != NULL) ? SSD_ELEM_FRU : SSD_ELEM_SKIP,
+			   fru_size,
+			   fru_ptr,
+
+			   /* Sense Key Specific */
+			   (sks_ptr != NULL) ? SSD_ELEM_SKS : SSD_ELEM_SKIP,
+			   sks_size,
+			   sks_ptr,
+
+			   /* Tape bits */
+			   (stream_ptr != NULL) ? SSD_ELEM_STREAM : SSD_ELEM_SKIP,
+			   stream_size,
+			   stream_ptr,
+
+			   SSD_ELEM_NONE);
+}
+
+ctl_sense_format
+ctl_get_sense_format(struct scsi_sense_data *sense_data)
+{
+	switch (sense_data->error_code & SSD_ERRCODE) {
+	case SSD_DESC_CURRENT_ERROR:
+	case SSD_DESC_DEFERRED_ERROR:
+		return (SSD_TYPE_DESC);
+	case SSD_CURRENT_ERROR:
+	case SSD_DEFERRED_ERROR:
+	default:
+		return (SSD_TYPE_FIXED);
+		break;
+	}
+}
+
+void
+ctl_set_ua(struct ctl_scsiio *ctsio, int asc, int ascq)
+{
+	ctl_set_sense(ctsio,
+		      /*current_error*/ 1,
+		      /*sense_key*/ SSD_KEY_UNIT_ATTENTION,
+		      asc,
+		      ascq,
+		      SSD_ELEM_NONE);
+}
+
+ctl_ua_type
+ctl_build_ua(ctl_ua_type ua_type, struct scsi_sense_data *sense,
+	     ctl_sense_format sense_format)
+{
+	ctl_ua_type ua_to_build;
+	int i, asc, ascq;
+
+	if (ua_type == CTL_UA_NONE)
+		return (ua_type);
+
+	ua_to_build = CTL_UA_NONE;
+
+	for (i = 0; i < (sizeof(ua_type) * 8); i++) {
+		if (ua_type & (1 << i)) {
+			ua_to_build = 1 << i;
+			break;
+		}
+	}
+
+	switch (ua_to_build) {
+	case CTL_UA_POWERON:
+		/* 29h/01h  POWER ON OCCURRED */
+		asc = 0x29;
+		ascq = 0x01;
+		break;
+	case CTL_UA_BUS_RESET:
+		/* 29h/02h  SCSI BUS RESET OCCURRED */
+		asc = 0x29;
+		ascq = 0x02;
+		break;
+	case CTL_UA_TARG_RESET:
+		/* 29h/03h  BUS DEVICE RESET FUNCTION OCCURRED*/
+		asc = 0x29;
+		ascq = 0x03;
+		break;
+	case CTL_UA_LUN_RESET:
+		/* 29h/00h  POWER ON, RESET, OR BUS DEVICE RESET OCCURRED */
+		/*
+		 * Since we don't have a specific ASC/ASCQ pair for a LUN
+		 * reset, just return the generic reset code.
+		 */
+		asc = 0x29;
+		ascq = 0x00;
+		break;
+	case CTL_UA_LUN_CHANGE:
+		/* 3Fh/0Eh  REPORTED LUNS DATA HAS CHANGED */
+		asc = 0x3F;
+		ascq = 0x0E;
+		break;
+	case CTL_UA_MODE_CHANGE:
+		/* 2Ah/01h  MODE PARAMETERS CHANGED */
+		asc = 0x2A;
+		ascq = 0x01;
+		break;
+	case CTL_UA_LOG_CHANGE:
+		/* 2Ah/02h  LOG PARAMETERS CHANGED */
+		asc = 0x2A;
+		ascq = 0x02;
+		break;
+	case CTL_UA_LVD:
+		/* 29h/06h  TRANSCEIVER MODE CHANGED TO LVD */
+		asc = 0x29;
+		ascq = 0x06;
+		break;
+	case CTL_UA_SE:
+		/* 29h/05h  TRANSCEIVER MODE CHANGED TO SINGLE-ENDED */
+		asc = 0x29;
+		ascq = 0x05;
+		break;
+	case CTL_UA_RES_PREEMPT:
+		/* 2Ah/03h  RESERVATIONS PREEMPTED */
+		asc = 0x2A;
+		ascq = 0x03;
+		break;
+	case CTL_UA_RES_RELEASE:
+		/* 2Ah/04h  RESERVATIONS RELEASED */
+		asc = 0x2A;
+		ascq = 0x04;
+		break;
+	case CTL_UA_REG_PREEMPT:
+		/* 2Ah/05h  REGISTRATIONS PREEMPTED */
+		asc = 0x2A;
+		ascq = 0x05;
+		break;
+	case CTL_UA_ASYM_ACC_CHANGE:
+	        /* 2Ah/06n  ASYMMETRIC ACCESS STATE CHANGED */
+		asc = 0x2A;
+		ascq = 0x06;
+		break;
+	default:
+		ua_to_build = CTL_UA_NONE;
+		return (ua_to_build);
+		break; /* NOTREACHED */
+	}
+
+	ctl_set_sense_data(sense,
+			   /*lun*/ NULL,
+			   sense_format,
+			   /*current_error*/ 1,
+			   /*sense_key*/ SSD_KEY_UNIT_ATTENTION,
+			   asc,
+			   ascq,
+			   SSD_ELEM_NONE);
+
+	return (ua_to_build);
+}
+
+void
+ctl_set_overlapped_cmd(struct ctl_scsiio *ctsio)
+{
+	/* OVERLAPPED COMMANDS ATTEMPTED */
+	ctl_set_sense(ctsio,
+		      /*current_error*/ 1,
+		      /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
+		      /*asc*/ 0x4E,
+		      /*ascq*/ 0x00,
+		      SSD_ELEM_NONE);
+}
+
+void
+ctl_set_overlapped_tag(struct ctl_scsiio *ctsio, uint8_t tag)
+{
+	/* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */
+	ctl_set_sense(ctsio,
+		      /*current_error*/ 1,
+		      /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
+		      /*asc*/ 0x4D,
+		      /*ascq*/ tag,
+		      SSD_ELEM_NONE);
+}
+
+/*
+ * Tell the user that there was a problem with the command or data he sent.
+ */
+void
+ctl_set_invalid_field(struct ctl_scsiio *ctsio, int sks_valid, int command,
+		      int field, int bit_valid, int bit)
+{
+	uint8_t sks[3];
+	int asc;
+
+	if (command != 0) {
+		/* "Invalid field in CDB" */
+		asc = 0x24;
+	} else {
+		/* "Invalid field in parameter list" */
+		asc = 0x26;
+	}
+
+	if (sks_valid) {
+		sks[0] = SSD_SCS_VALID;
+		if (command)
+			sks[0] |= SSD_FIELDPTR_CMD;
+		scsi_ulto2b(field, &sks[1]);
+
+		if (bit_valid)
+			sks[0] |= SSD_BITPTR_VALID | bit;
+	}
+
+	ctl_set_sense(ctsio,
+		      /*current_error*/ 1,
+		      /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
+		      asc,
+		      /*ascq*/ 0x00,
+		      /*type*/ (sks_valid != 0) ? SSD_ELEM_SKS : SSD_ELEM_SKIP,
+		      /*size*/ sizeof(sks),
+		      /*data*/ sks,
+		      SSD_ELEM_NONE);
+}
+
+void
+ctl_set_invalid_opcode(struct ctl_scsiio *ctsio)
+{
+	struct scsi_sense_data *sense;
+	uint8_t sks[3];
+
+	sense = &ctsio->sense_data;
+
+	sks[0] = SSD_SCS_VALID | SSD_FIELDPTR_CMD;
+	scsi_ulto2b(0, &sks[1]);
+
+	/* "Invalid command operation code" */
+	ctl_set_sense(ctsio,
+		      /*current_error*/ 1,
+		      /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
+		      /*asc*/ 0x20,
+		      /*ascq*/ 0x00,
+		      /*type*/ SSD_ELEM_SKS,
+		      /*size*/ sizeof(sks),
+		      /*data*/ sks,
+		      SSD_ELEM_NONE);
+}
+
+void
+ctl_set_param_len_error(struct ctl_scsiio *ctsio)
+{
+	/* "Parameter list length error" */
+	ctl_set_sense(ctsio,
+		      /*current_error*/ 1,
+		      /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
+		      /*asc*/ 0x1a,
+		      /*ascq*/ 0x00,
+		      SSD_ELEM_NONE);
+}
+
+void
+ctl_set_already_locked(struct ctl_scsiio *ctsio)
+{
+	/* Vendor unique "Somebody already is locked" */
+	ctl_set_sense(ctsio,
+		      /*current_error*/ 1,
+		      /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
+		      /*asc*/ 0x81,
+		      /*ascq*/ 0x00,
+		      SSD_ELEM_NONE);
+}
+
+void
+ctl_set_unsupported_lun(struct ctl_scsiio *ctsio)
+{
+	/* "Logical unit not supported" */
+	ctl_set_sense(ctsio,
+		      /*current_error*/ 1,
+		      /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
+		      /*asc*/ 0x25,
+		      /*ascq*/ 0x00,
+		      SSD_ELEM_NONE);
+}
+
+void
+ctl_set_internal_failure(struct ctl_scsiio *ctsio, int sks_valid,
+			 uint16_t retry_count)
+{
+	uint8_t sks[3];
+
+	if (sks_valid) {
+		sks[0] = SSD_SCS_VALID;
+		sks[1] = (retry_count >> 8) & 0xff;
+		sks[2] = retry_count & 0xff;
+	}
+
+	/* "Internal target failure" */
+	ctl_set_sense(ctsio,
+		      /*current_error*/ 1,
+		      /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
+		      /*asc*/ 0x44,
+		      /*ascq*/ 0x00,
+		      /*type*/ (sks_valid != 0) ? SSD_ELEM_SKS : SSD_ELEM_SKIP,
+		      /*size*/ sizeof(sks),
+		      /*data*/ sks,
+		      SSD_ELEM_NONE);
+}
+
+void
+ctl_set_medium_error(struct ctl_scsiio *ctsio)
+{
+	if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) {
+		/* "Unrecovered read error" */
+		ctl_set_sense(ctsio,
+			      /*current_error*/ 1,
+			      /*sense_key*/ SSD_KEY_MEDIUM_ERROR,
+			      /*asc*/ 0x11,
+			      /*ascq*/ 0x00,
+			      SSD_ELEM_NONE);
+	} else {
+		/* "Write error - auto reallocation failed" */
+		ctl_set_sense(ctsio,
+			      /*current_error*/ 1,
+			      /*sense_key*/ SSD_KEY_MEDIUM_ERROR,
+			      /*asc*/ 0x0C,
+			      /*ascq*/ 0x02,
+			      SSD_ELEM_NONE);
+	}
+}
+
+void
+ctl_set_aborted(struct ctl_scsiio *ctsio)
+{
+	ctl_set_sense(ctsio,
+		      /*current_error*/ 1,
+		      /*sense_key*/ SSD_KEY_ABORTED_COMMAND,
+		      /*asc*/ 0x45,
+		      /*ascq*/ 0x00,
+		      SSD_ELEM_NONE);
+}
+
+void
+ctl_set_lba_out_of_range(struct ctl_scsiio *ctsio)
+{
+	/* "Logical block address out of range" */
+	ctl_set_sense(ctsio,
+		      /*current_error*/ 1,
+		      /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
+		      /*asc*/ 0x21,
+		      /*ascq*/ 0x00,
+		      SSD_ELEM_NONE);
+}
+
+void
+ctl_set_lun_stopped(struct ctl_scsiio *ctsio)
+{
+	/* "Logical unit not ready, initializing cmd. required" */
+	ctl_set_sense(ctsio,
+		      /*current_error*/ 1,
+		      /*sense_key*/ SSD_KEY_NOT_READY,
+		      /*asc*/ 0x04,
+		      /*ascq*/ 0x02,
+		      SSD_ELEM_NONE);
+}
+
+void
+ctl_set_lun_not_ready(struct ctl_scsiio *ctsio)
+{
+	/* "Logical unit not ready, manual intervention required" */
+	ctl_set_sense(ctsio,
+		      /*current_error*/ 1,
+		      /*sense_key*/ SSD_KEY_NOT_READY,
+		      /*asc*/ 0x04,
+		      /*ascq*/ 0x05,
+		      SSD_ELEM_NONE);
+}
+
+void
+ctl_set_illegal_pr_release(struct ctl_scsiio *ctsio)
+{
+	/* "Invalid release of persistent reservation" */
+	ctl_set_sense(ctsio,
+		      /*current_error*/ 1,
+		      /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
+		      /*asc*/ 0x26,
+		      /*ascq*/ 0x04,
+		      SSD_ELEM_NONE);
+}
+
+void
+ctl_set_lun_standby(struct ctl_scsiio *ctsio)
+{
+	/* "Logical unit not ready, target port in standby state" */
+	ctl_set_sense(ctsio,
+		      /*current_error*/ 1,
+		      /*sense_key*/ SSD_KEY_NOT_READY,
+		      /*asc*/ 0x04,
+		      /*ascq*/ 0x0b,
+		      SSD_ELEM_NONE);
+}
+
+void
+ctl_set_medium_format_corrupted(struct ctl_scsiio *ctsio)
+{
+	/* "Medium format corrupted" */
+	ctl_set_sense(ctsio,
+		      /*current_error*/ 1,
+		      /*sense_key*/ SSD_KEY_MEDIUM_ERROR,
+		      /*asc*/ 0x31,
+		      /*ascq*/ 0x00,
+		      SSD_ELEM_NONE);
+}
+
+void
+ctl_set_medium_magazine_inaccessible(struct ctl_scsiio *ctsio)
+{
+	/* "Medium magazine not accessible" */
+	ctl_set_sense(ctsio,
+		      /*current_error*/ 1,
+		      /*sense_key*/ SSD_KEY_NOT_READY,
+		      /*asc*/ 0x3b,
+		      /*ascq*/ 0x11,
+		      SSD_ELEM_NONE);
+}
+
+void
+ctl_set_data_phase_error(struct ctl_scsiio *ctsio)
+{
+	/* "Data phase error" */
+	ctl_set_sense(ctsio,
+		      /*current_error*/ 1,
+		      /*sense_key*/ SSD_KEY_NOT_READY,
+		      /*asc*/ 0x4b,
+		      /*ascq*/ 0x00,
+		      SSD_ELEM_NONE);
+}
+
+void
+ctl_set_reservation_conflict(struct ctl_scsiio *ctsio)
+{
+	struct scsi_sense_data *sense;
+
+	sense = &ctsio->sense_data;
+	memset(sense, 0, sizeof(*sense));
+	ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
+	ctsio->sense_len = 0;
+	ctsio->io_hdr.status = CTL_SCSI_ERROR;
+}
+
+void
+ctl_set_queue_full(struct ctl_scsiio *ctsio)
+{
+	struct scsi_sense_data *sense;
+
+	sense = &ctsio->sense_data;
+	memset(sense, 0, sizeof(*sense));
+	ctsio->scsi_status = SCSI_STATUS_QUEUE_FULL;
+	ctsio->sense_len = 0;
+	ctsio->io_hdr.status = CTL_SCSI_ERROR;
+}
+
+void
+ctl_set_busy(struct ctl_scsiio *ctsio)
+{
+	struct scsi_sense_data *sense;
+
+	sense = &ctsio->sense_data;
+	memset(sense, 0, sizeof(*sense));
+	ctsio->scsi_status = SCSI_STATUS_BUSY;
+	ctsio->sense_len = 0;
+	ctsio->io_hdr.status = CTL_SCSI_ERROR;
+}
+
+void
+ctl_set_success(struct ctl_scsiio *ctsio)
+{
+	struct scsi_sense_data *sense;
+
+	sense = &ctsio->sense_data;
+	memset(sense, 0, sizeof(*sense));
+	ctsio->scsi_status = SCSI_STATUS_OK;
+	ctsio->sense_len = 0;
+	ctsio->io_hdr.status = CTL_SUCCESS;
+}
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/cam/ctl/ctl_error.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/sys/cam/ctl/ctl_error.h	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,92 @@
+/*-
+ * Copyright (c) 2003 Silicon Graphics International Corp.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions, and the following disclaimer,
+ *    without modification.
+ * 2. Redistributions in binary form must reproduce at minimum a disclaimer
+ *    substantially similar to the "NO WARRANTY" disclaimer below
+ *    ("Disclaimer") and any redistribution must be conditioned upon
+ *    including a substantially similar Disclaimer requirement for further
+ *    binary redistribution.
+ *
+ * NO WARRANTY
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGES.
+ *
+ * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_error.h#1 $
+ * $FreeBSD: head/sys/cam/ctl/ctl_error.h 229997 2012-01-12 00:34:33Z ken $
+ */
+/*
+ * Function definitions for various error reporting routines used both
+ * within CTL and various CTL clients.
+ *
+ * Author: Ken Merry <ken at FreeBSD.org>
+ */
+
+#include <machine/stdarg.h>
+
+#ifndef	_CTL_ERROR_H_
+#define	_CTL_ERROR_H_
+
+typedef enum {
+	CTL_SENSE_NOT_SPECIFIED,
+	CTL_SENSE_FIXED,
+	CTL_SENSE_DESCRIPTOR
+} ctl_sense_format;
+
+void ctl_set_sense_data_va(struct scsi_sense_data *sense_data, void *lun,
+			   scsi_sense_data_type sense_format, int current_error,
+			   int sense_key, int asc, int ascq, va_list ap); 
+void ctl_set_sense_data(struct scsi_sense_data *sense_data, void *lun,
+			scsi_sense_data_type sense_format, int current_error,
+			int sense_key, int asc, int ascq, ...); 
+void ctl_set_sense(struct ctl_scsiio *ctsio, int current_error, int sense_key,
+		   int asc, int ascq, ...);
+void ctl_sense_to_desc(struct scsi_sense_data_fixed *sense_src,
+		      struct scsi_sense_data_desc *sense_dest);
+void ctl_sense_to_fixed(struct scsi_sense_data_desc *sense_src,
+			struct scsi_sense_data_fixed *sense_dest);
+ctl_sense_format ctl_get_sense_format(struct scsi_sense_data *sense_data);
+void ctl_set_ua(struct ctl_scsiio *ctsio, int asc, int ascq);
+ctl_ua_type ctl_build_ua(ctl_ua_type ua_type, struct scsi_sense_data *sense,
+			 ctl_sense_format sense_format);
+void ctl_set_overlapped_cmd(struct ctl_scsiio *ctsio);
+void ctl_set_overlapped_tag(struct ctl_scsiio *ctsio, uint8_t tag);
+void ctl_set_invalid_field(struct ctl_scsiio *ctsio, int sks_valid, int command,
+			   int field, int bit_valid, int bit);
+void ctl_set_invalid_opcode(struct ctl_scsiio *ctsio);
+void ctl_set_param_len_error(struct ctl_scsiio *ctsio);
+void ctl_set_already_locked(struct ctl_scsiio *ctsio);
+void ctl_set_unsupported_lun(struct ctl_scsiio *ctsio);
+void ctl_set_lun_standby(struct ctl_scsiio *ctsio);
+void ctl_set_internal_failure(struct ctl_scsiio *ctsio, int sks_valid,
+			      uint16_t retry_count);
+void ctl_set_medium_error(struct ctl_scsiio *ctsio);
+void ctl_set_aborted(struct ctl_scsiio *ctsio);
+void ctl_set_lba_out_of_range(struct ctl_scsiio *ctsio);
+void ctl_set_lun_stopped(struct ctl_scsiio *ctsio);
+void ctl_set_lun_not_ready(struct ctl_scsiio *ctsio);
+void ctl_set_illegal_pr_release(struct ctl_scsiio *ctsio);
+void ctl_set_medium_format_corrupted(struct ctl_scsiio *ctsio);
+void ctl_set_medium_magazine_inaccessible(struct ctl_scsiio *ctsio);
+void ctl_set_data_phase_error(struct ctl_scsiio *ctsio);
+void ctl_set_reservation_conflict(struct ctl_scsiio *ctsio);
+void ctl_set_queue_full(struct ctl_scsiio *ctsio);
+void ctl_set_busy(struct ctl_scsiio *ctsio);
+void ctl_set_success(struct ctl_scsiio *ctsio);
+
+#endif	/* _CTL_ERROR_H_ */
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/cam/ctl/ctl_frontend.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/sys/cam/ctl/ctl_frontend.c	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,187 @@
+/*-
+ * Copyright (c) 2003 Silicon Graphics International Corp.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions, and the following disclaimer,
+ *    without modification.
+ * 2. Redistributions in binary form must reproduce at minimum a disclaimer
+ *    substantially similar to the "NO WARRANTY" disclaimer below
+ *    ("Disclaimer") and any redistribution must be conditioned upon
+ *    including a substantially similar Disclaimer requirement for further
+ *    binary redistribution.
+ *
+ * NO WARRANTY
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGES.
+ *
+ * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_frontend.c#4 $
+ */
+/*
+ * CAM Target Layer front end interface code
+ *
+ * Author: Ken Merry <ken at FreeBSD.org>
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD: head/sys/cam/ctl/ctl_frontend.c 229997 2012-01-12 00:34:33Z ken $");
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/kernel.h>
+#include <sys/types.h>
+#include <sys/malloc.h>
+#include <sys/lock.h>
+#include <sys/mutex.h>
+#include <sys/condvar.h>
+#include <sys/endian.h>
+#include <sys/queue.h>
+
+#include <cam/scsi/scsi_all.h>
+#include <cam/scsi/scsi_da.h>
+#include <cam/ctl/ctl_io.h>
+#include <cam/ctl/ctl.h>
+#include <cam/ctl/ctl_frontend.h>
+#include <cam/ctl/ctl_frontend_internal.h>
+#include <cam/ctl/ctl_backend.h>
+/* XXX KDM move defines from ctl_ioctl.h to somewhere else */
+#include <cam/ctl/ctl_ioctl.h>
+#include <cam/ctl/ctl_ha.h>
+#include <cam/ctl/ctl_private.h>
+#include <cam/ctl/ctl_debug.h>
+
+extern struct ctl_softc *control_softc;
+
+int
+ctl_frontend_register(struct ctl_frontend *fe, int master_shelf)
+{
+	struct ctl_io_pool *pool;
+	int port_num;
+	int retval;
+
+	retval = 0;
+
+	KASSERT(control_softc != NULL, ("CTL is not initialized"));
+
+	mtx_lock(&control_softc->ctl_lock);
+	port_num = ctl_ffz(&control_softc->ctl_port_mask, CTL_MAX_PORTS);
+	if ((port_num == -1)
+	 || (ctl_set_mask(&control_softc->ctl_port_mask, port_num) == -1)) {
+		fe->targ_port = -1;
+		mtx_unlock(&control_softc->ctl_lock);
+		return (1);
+	}
+	control_softc->num_frontends++;
+
+	mtx_unlock(&control_softc->ctl_lock);
+	/*
+	 * We add 20 to whatever the caller requests, so he doesn't get
+	 * burned by queueing things back to the pending sense queue.  In
+	 * theory, there should probably only be one outstanding item, at
+	 * most, on the pending sense queue for a LUN.  We'll clear the
+	 * pending sense queue on the next command, whether or not it is
+	 * a REQUEST SENSE.
+	 */
+	retval = ctl_pool_create(control_softc,
+				 (fe->port_type != CTL_PORT_IOCTL) ?
+				 CTL_POOL_FETD : CTL_POOL_IOCTL,
+				 fe->num_requested_ctl_io + 20, &pool);
+	if (retval != 0) {
+		fe->targ_port = -1;
+		mtx_lock(&control_softc->ctl_lock);
+		ctl_clear_mask(&control_softc->ctl_port_mask, port_num);
+		mtx_unlock(&control_softc->ctl_lock);
+		return (retval);
+	}
+
+	mtx_lock(&control_softc->ctl_lock);
+
+	/* For now assume master shelf */
+	//fe->targ_port = port_num;
+	fe->targ_port = port_num + (master_shelf!=0 ? 0 : CTL_MAX_PORTS);
+	fe->max_initiators = CTL_MAX_INIT_PER_PORT;
+	STAILQ_INSERT_TAIL(&control_softc->fe_list, fe, links);
+	ctl_pool_acquire(pool);
+	control_softc->ctl_ports[port_num] = fe;
+
+	mtx_unlock(&control_softc->ctl_lock);
+
+	fe->ctl_pool_ref = pool;
+
+	return (retval);
+}
+
+int
+ctl_frontend_deregister(struct ctl_frontend *fe)
+{
+	struct ctl_io_pool *pool;
+	int port_num;
+	int retval;
+
+	retval = 0;
+
+	pool = (struct ctl_io_pool *)fe->ctl_pool_ref;
+
+	if (fe->targ_port == -1) {
+		retval = 1;
+		goto bailout;
+	}
+
+	mtx_lock(&control_softc->ctl_lock);
+
+	ctl_pool_invalidate(pool);
+	ctl_pool_release(pool);
+
+	STAILQ_REMOVE(&control_softc->fe_list, fe, ctl_frontend, links);
+	control_softc->num_frontends--;
+	port_num = (fe->targ_port < CTL_MAX_PORTS) ? fe->targ_port :
+	                                             fe->targ_port - CTL_MAX_PORTS;
+	ctl_clear_mask(&control_softc->ctl_port_mask, port_num);
+	control_softc->ctl_ports[port_num] = NULL;
+	mtx_unlock(&control_softc->ctl_lock);
+bailout:
+	return (retval);
+}
+
+void
+ctl_frontend_set_wwns(struct ctl_frontend *fe, int wwnn_valid, uint64_t wwnn,
+		      int wwpn_valid, uint64_t wwpn)
+{
+	if (wwnn_valid)
+		fe->wwnn = wwnn;
+
+	if (wwpn_valid)
+		fe->wwpn = wwpn;
+}
+
+void
+ctl_frontend_online(struct ctl_frontend *fe)
+{
+	fe->port_online(fe->onoff_arg);
+	/* XXX KDM need a lock here? */
+	fe->status |= CTL_PORT_STATUS_ONLINE;
+}
+
+void
+ctl_frontend_offline(struct ctl_frontend *fe)
+{
+	fe->port_offline(fe->onoff_arg);
+	/* XXX KDM need a lock here? */
+	fe->status &= ~CTL_PORT_STATUS_ONLINE;
+}
+
+/*
+ * vim: ts=8
+ */
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/cam/ctl/ctl_frontend.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/sys/cam/ctl/ctl_frontend.h	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,295 @@
+/*-
+ * Copyright (c) 2003 Silicon Graphics International Corp.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions, and the following disclaimer,
+ *    without modification.
+ * 2. Redistributions in binary form must reproduce at minimum a disclaimer
+ *    substantially similar to the "NO WARRANTY" disclaimer below
+ *    ("Disclaimer") and any redistribution must be conditioned upon
+ *    including a substantially similar Disclaimer requirement for further
+ *    binary redistribution.
+ *
+ * NO WARRANTY
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGES.
+ *
+ * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_frontend.h#2 $
+ * $FreeBSD: head/sys/cam/ctl/ctl_frontend.h 229997 2012-01-12 00:34:33Z ken $
+ */
+/*
+ * CAM Target Layer front end registration hooks
+ *
+ * Author: Ken Merry <ken at FreeBSD.org>
+ */
+
+#ifndef	_CTL_FRONTEND_H_
+#define	_CTL_FRONTEND_H_
+
+typedef enum {
+	CTL_PORT_STATUS_NONE		= 0x00,
+	CTL_PORT_STATUS_ONLINE		= 0x01,
+	CTL_PORT_STATUS_TARG_ONLINE	= 0x02,
+	CTL_PORT_STATUS_LUN_ONLINE	= 0x04
+} ctl_port_status;
+
+typedef void (*port_func_t)(void *onoff_arg);
+typedef int (*targ_func_t)(void *arg, struct ctl_id targ_id);
+typedef	int (*lun_func_t)(void *arg, struct ctl_id targ_id, int lun_id);
+
+/*
+ * The ctl_frontend structure is the registration mechanism between a FETD
+ * (Front End Target Driver) and the CTL layer.  Here is a description of
+ * the fields:
+ *
+ * port_type:		  This field tells CTL what kind of front end it is
+ *                        dealing with.  This field serves two purposes.
+ *                        The first is to let CTL know whether the frontend
+ *                        in question is inside the main CTL module (i.e.
+ *                        the ioctl front end), and therefore its module
+ *                        reference count shouldn't be incremented.  The
+ *                        CTL ioctl front end should continue to use the
+ *                        CTL_PORT_IOCTL argument as long as it is part of
+ *                        the main CTL module.  The second is to let CTL
+ *                        know what kind of front end it is dealing with, so
+ *                        it can return the proper inquiry data for that
+ *                        particular port.
+ *
+ * num_requested_ctl_io:  This is the number of ctl_io structures that the
+ *			  front end needs for its pool.  This should
+ * 			  generally be the maximum number of outstanding
+ *			  transactions that the FETD can handle.  The CTL
+ *			  layer will add a few to this to account for
+ *			  ctl_io buffers queued for pending sense data.
+ *			  (Pending sense only gets queued if the FETD
+ * 			  doesn't support autosense.  e.g. non-packetized
+ * 			  parallel SCSI doesn't support autosense.)
+ *
+ * port_name:		  A string describing the FETD.  e.g. "LSI 1030T U320"
+ *			  or whatever you want to use to describe the driver.
+ *
+ *
+ * physical_port:	  This is the physical port number of this
+ * 			  particular port within the driver/hardware.  This
+ * 			  number is hardware/driver specific.
+ * virtual_port:	  This is the virtual port number of this
+ * 			  particular port.  This is for things like NP-IV.
+ * 
+ * port_online():	  This function is called, with onoff_arg as its
+ *			  argument, by the CTL layer when it wants the FETD
+ *			  to start responding to selections on the specified
+ * 			  target ID.  (targ_target)
+ *
+ * port_offline():	  This function is called, with onoff_arg as its
+ *			  argument, by the CTL layer when it wants the FETD
+ * 			  to stop responding to selection on the specified
+ * 			  target ID.  (targ_target)
+ *
+ * onoff_arg:		  This is supplied as an argument to port_online()
+ *			  and port_offline().  This is specified by the
+ *			  FETD.
+ *
+ * targ_enable():	  This function is called, with targ_lun_arg and a
+ * 			  target ID as its arguments, by CTL when it wants
+ *			  the FETD to enable a particular target.  targ_enable()
+ *			  will always be called for a particular target ID
+ * 			  before any LUN is enabled for that target.  If the
+ *			  FETD does not support enabling targets, but rather
+ *			  LUNs, it should ignore this call and return 0.  If
+ *			  the FETD does support enabling targets, it should
+ *			  return 0 for success and non-zero if it cannot
+ *			  enable the given target.
+ *
+ *			  TODO:  Add the ability to specify a WWID here.
+ *
+ * targ_disable():	  This function is called, with targ_lun_arg and a
+ *			  target ID as its arguments, by CTL when it wants
+ *			  the FETD to disable a particular target.
+ *			  targ_disable() will always be called for a
+ *			  particular target ID after all LUNs are disabled
+ *			  on that particular target.  If the FETD does not
+ *			  support enabling targets, it should ignore this
+ *			  call and return 0.  If the FETD does support
+ *			  enabling targets, it should return 0 for success,
+ *			  and non-zero if it cannot disable the given target.
+ *
+ * lun_enable():	  This function is called, with targ_lun_arg, a target
+ *			  ID and a LUN ID as its arguments, by CTL when it
+ *			  wants the FETD to enable a particular LUN.  If the
+ *			  FETD doesn't really know about LUNs, it should
+ *			  just ignore this call and return 0.  If the FETD
+ *			  cannot enable the requested LUN for some reason, the
+ *			  FETD should return non-zero status.
+ *
+ * lun_disable():	  This function is called, with targ_lun_arg, a target
+ *			  ID and LUN ID as its arguments, by CTL when it
+ *			  wants the FETD to disable a particular LUN.  If the
+ *			  FETD doesn't really know about LUNs, it should just
+ *			  ignore this call and return 0.  If the FETD cannot
+ *			  disable the requested LUN for some reason, the
+ *			  FETD should return non-zero status.
+ *
+ * targ_lun_arg:	  This is supplied as an argument to the targ/lun
+ *			  enable/disable() functions.  This is specified by
+ *			  the FETD.
+ *
+ * fe_datamove():	  This function is called one or more times per I/O
+ *			  by the CTL layer to tell the FETD to initiate a
+ *			  DMA to or from the data buffer(s) specified by
+ * 			  the passed-in ctl_io structure.
+ *
+ * fe_done():	  	  This function is called by the CTL layer when a
+ *			  particular SCSI I/O or task management command has
+ * 			  completed.  For SCSI I/O requests (CTL_IO_SCSI),
+ *			  sense data is always supplied if the status is
+ *			  CTL_SCSI_ERROR and the SCSI status byte is
+ *			  SCSI_STATUS_CHECK_COND.  If the FETD doesn't
+ *			  support autosense, the sense should be queued
+ *			  back to the CTL layer via ctl_queue_sense().
+ *
+ * fe_dump():		  This function, if it exists, is called by CTL
+ *			  to request a dump of any debugging information or
+ *			  state to the console.
+ *
+ * max_targets:		  The maximum number of targets that we can create
+ *			  per-port.
+ *
+ * max_target_id:	  The highest target ID that we can use.
+ *
+ * targ_port:		  The CTL layer assigns a "port number" to every
+ *			  FETD.  This port number should be passed back in
+ *			  in the header of every ctl_io that is queued to
+ *			  the CTL layer.  This enables us to determine
+ *			  which bus the command came in on.
+ *
+ * ctl_pool_ref:	  Memory pool reference used by the FETD in calls to
+ * 			  ctl_alloc_io().
+ *
+ * max_initiators:	  Maximum number of initiators that the FETD is
+ *			  allowed to have.  Initiators should be numbered
+ *			  from 0 to max_initiators - 1.  This value will
+ * 			  typically be 16, and thus not a problem for
+ * 			  parallel SCSI.  This may present issues for Fibre
+ *			  Channel.
+ *
+ * wwnn			  World Wide Node Name to be used by the FETD.
+ *			  Note that this is set *after* registration.  It
+ * 			  will be set prior to the online function getting
+ * 			  called.
+ *
+ * wwpn			  World Wide Port Name to be used by the FETD.
+ *			  Note that this is set *after* registration.  It
+ * 			  will be set prior to the online function getting
+ * 			  called.
+ *
+ * status:		  Used by CTL to keep track of per-FETD state.
+ *
+ * links:		  Linked list pointers, used by CTL.  The FETD
+ *			  shouldn't touch this field.
+ */
+struct ctl_frontend {
+	ctl_port_type	port_type;		/* passed to CTL */
+	int		num_requested_ctl_io;	/* passed to CTL */
+	char		*port_name;		/* passed to CTL */
+	int		physical_port;		/* passed to CTL */
+	int		virtual_port;		/* passed to CTL */
+	port_func_t	port_online;		/* passed to CTL */
+	port_func_t	port_offline;		/* passed to CTL */
+	void		*onoff_arg;		/* passed to CTL */
+	targ_func_t	targ_enable;		/* passed to CTL */
+	targ_func_t	targ_disable;		/* passed to CTL */
+	lun_func_t	lun_enable;		/* passed to CTL */
+	lun_func_t	lun_disable;		/* passed to CTL */
+	void		*targ_lun_arg;		/* passed to CTL */
+	void		(*fe_datamove)(union ctl_io *io); /* passed to CTL */
+	void		(*fe_done)(union ctl_io *io); /* passed to CTL */
+	void		(*fe_dump)(void);	/* passed to CTL */
+	int		max_targets;		/* passed to CTL */
+	int		max_target_id;		/* passed to CTL */
+	int32_t		targ_port;		/* passed back to FETD */
+	void		*ctl_pool_ref;		/* passed back to FETD */
+	uint32_t	max_initiators;		/* passed back to FETD */
+	uint64_t	wwnn;			/* set by CTL before online */
+	uint64_t	wwpn;			/* set by CTL before online */
+	ctl_port_status	status;			/* used by CTL */
+	STAILQ_ENTRY(ctl_frontend) links;	/* used by CTL */
+};
+
+/*
+ * This may block until resources are allocated.  Called at FETD module load
+ * time. Returns 0 for success, non-zero for failure.
+ */
+int ctl_frontend_register(struct ctl_frontend *fe, int master_SC);
+
+/*
+ * Called at FETD module unload time.
+ * Returns 0 for success, non-zero for failure.
+ */
+int ctl_frontend_deregister(struct ctl_frontend *fe);
+
+/*
+ * Called to set the WWNN and WWPN for a particular frontend.
+ */
+void ctl_frontend_set_wwns(struct ctl_frontend *fe, int wwnn_valid,
+			   uint64_t wwnn, int wwpn_valid, uint64_t wwpn);
+
+/*
+ * Called to bring a particular frontend online.
+ */
+void ctl_frontend_online(struct ctl_frontend *fe);
+
+/*
+ * Called to take a particular frontend offline.
+ */
+void ctl_frontend_offline(struct ctl_frontend *fe);
+
+/*
+ * This routine queues I/O and task management requests from the FETD to the
+ * CTL layer.  Returns immediately.  Returns 0 for success, non-zero for
+ * failure.
+ */
+int ctl_queue(union ctl_io *io);
+
+/*
+ * This routine is used if the front end interface doesn't support
+ * autosense (e.g. non-packetized parallel SCSI).  This will queue the
+ * scsiio structure back to a per-lun pending sense queue.  This MUST be
+ * called BEFORE any request sense can get queued to the CTL layer -- I
+ * need it in the queue in order to service the request.  The scsiio
+ * structure passed in here will be freed by the CTL layer when sense is
+ * retrieved by the initiator.  Returns 0 for success, non-zero for failure.
+ */
+int ctl_queue_sense(union ctl_io *io);
+
+/*
+ * This routine adds an initiator to CTL's port database.  The WWPN should
+ * be the FC WWPN, if available.  The targ_port field should be the same as
+ * the targ_port passed back from CTL in the ctl_frontend structure above.
+ * The iid field should be the same as the iid passed in the nexus of each
+ * ctl_io from this initiator.
+ */
+int ctl_add_initiator(uint64_t wwpn, int32_t targ_port, uint32_t iid);
+
+/*
+ * This routine will remove an initiator from CTL's port database.  The
+ * targ_port field should be the same as the targ_port passed back in the
+ * ctl_frontend structure above.  The iid field should be the same as the
+ * iid passed in the nexus of each ctl_io from this initiator.
+ */
+int
+ctl_remove_initiator(int32_t targ_port, uint32_t iid);
+
+#endif	/* _CTL_FRONTEND_H_ */
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/cam/ctl/ctl_frontend_cam_sim.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/sys/cam/ctl/ctl_frontend_cam_sim.c	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,866 @@
+/*-
+ * Copyright (c) 2009 Silicon Graphics International Corp.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions, and the following disclaimer,
+ *    without modification.
+ * 2. Redistributions in binary form must reproduce at minimum a disclaimer
+ *    substantially similar to the "NO WARRANTY" disclaimer below
+ *    ("Disclaimer") and any redistribution must be conditioned upon
+ *    including a substantially similar Disclaimer requirement for further
+ *    binary redistribution.
+ *
+ * NO WARRANTY
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGES.
+ *
+ * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_frontend_cam_sim.c#4 $
+ */
+/*
+ * CTL frontend to CAM SIM interface.  This allows access to CTL LUNs via
+ * the da(4) and pass(4) drivers from inside the system.
+ *
+ * Author: Ken Merry <ken at FreeBSD.org>
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD: head/sys/cam/ctl/ctl_frontend_cam_sim.c 229997 2012-01-12 00:34:33Z ken $");
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/kernel.h>
+#include <sys/types.h>
+#include <sys/malloc.h>
+#include <sys/lock.h>
+#include <sys/mutex.h>
+#include <sys/condvar.h>
+#include <sys/queue.h>
+#include <sys/bus.h>
+#include <sys/sysctl.h>
+#include <machine/bus.h>
+#include <sys/sbuf.h>
+
+#include <cam/cam.h>
+#include <cam/cam_ccb.h>
+#include <cam/cam_sim.h>
+#include <cam/cam_xpt_sim.h>
+#include <cam/cam_xpt.h>
+#include <cam/cam_periph.h>
+#include <cam/scsi/scsi_all.h>
+#include <cam/scsi/scsi_message.h>
+#include <cam/ctl/ctl_io.h>
+#include <cam/ctl/ctl.h>
+#include <cam/ctl/ctl_frontend.h>
+#include <cam/ctl/ctl_frontend_internal.h>
+#include <cam/ctl/ctl_mem_pool.h>
+#include <cam/ctl/ctl_debug.h>
+
+#define	io_ptr		spriv_ptr1
+
+struct cfcs_io {
+	union ccb *ccb;
+};
+
+struct cfcs_softc {
+	struct ctl_frontend fe;
+	char port_name[32];
+	struct cam_sim *sim;
+	struct cam_devq *devq;
+	struct cam_path *path;
+	struct mtx lock;
+	char lock_desc[32];
+	uint64_t wwnn;
+	uint64_t wwpn;
+	uint32_t cur_tag_num;
+	int online;
+};
+
+/*
+ * We can't handle CCBs with these flags.  For the most part, we just don't
+ * handle physical addresses yet.  That would require mapping things in
+ * order to do the copy.
+ */
+#define	CFCS_BAD_CCB_FLAGS (CAM_DATA_PHYS | CAM_SG_LIST_PHYS | \
+	CAM_MSG_BUF_PHYS | CAM_SNS_BUF_PHYS | CAM_CDB_PHYS | CAM_SENSE_PTR |\
+	CAM_SENSE_PHYS)
+
+int cfcs_init(void);
+void cfcs_shutdown(void);
+static void cfcs_poll(struct cam_sim *sim);
+static void cfcs_online(void *arg);
+static void cfcs_offline(void *arg);
+static int cfcs_targ_enable(void *arg, struct ctl_id targ_id);
+static int cfcs_targ_disable(void *arg, struct ctl_id targ_id);
+static int cfcs_lun_enable(void *arg, struct ctl_id target_id, int lun_id);
+static int cfcs_lun_disable(void *arg, struct ctl_id target_id, int lun_id);
+static void cfcs_datamove(union ctl_io *io);
+static void cfcs_done(union ctl_io *io);
+void cfcs_action(struct cam_sim *sim, union ccb *ccb);
+static void cfcs_async(void *callback_arg, uint32_t code,
+		       struct cam_path *path, void *arg);
+
+struct cfcs_softc cfcs_softc;
+/*
+ * This is primarly intended to allow for error injection to test the CAM
+ * sense data and sense residual handling code.  This sets the maximum
+ * amount of SCSI sense data that we will report to CAM.
+ */
+static int cfcs_max_sense = sizeof(struct scsi_sense_data);
+
+SYSINIT(cfcs_init, SI_SUB_CONFIGURE, SI_ORDER_FOURTH, cfcs_init, NULL);
+SYSCTL_NODE(_kern_cam, OID_AUTO, ctl2cam, CTLFLAG_RD, 0,
+	    "CAM Target Layer SIM frontend");
+SYSCTL_INT(_kern_cam_ctl2cam, OID_AUTO, max_sense, CTLFLAG_RW,
+           &cfcs_max_sense, 0, "Maximum sense data size");
+
+
+int
+cfcs_init(void)
+{
+	struct cfcs_softc *softc;
+	struct ccb_setasync csa;
+	struct ctl_frontend *fe;
+#ifdef NEEDTOPORT
+	char wwnn[8];
+#endif
+	int retval;
+
+	softc = &cfcs_softc;
+	retval = 0;
+	bzero(softc, sizeof(*softc));
+	sprintf(softc->lock_desc, "ctl2cam");
+	mtx_init(&softc->lock, softc->lock_desc, NULL, MTX_DEF);
+	fe = &softc->fe;
+
+	fe->port_type = CTL_PORT_INTERNAL;
+	/* XXX KDM what should the real number be here? */
+	fe->num_requested_ctl_io = 4096;
+	snprintf(softc->port_name, sizeof(softc->port_name), "ctl2cam");
+	fe->port_name = softc->port_name;
+	fe->port_online = cfcs_online;
+	fe->port_offline = cfcs_offline;
+	fe->onoff_arg = softc;
+	fe->targ_enable = cfcs_targ_enable;
+	fe->targ_disable = cfcs_targ_disable;
+	fe->lun_enable = cfcs_lun_enable;
+	fe->lun_disable = cfcs_lun_disable;
+	fe->targ_lun_arg = softc;
+	fe->fe_datamove = cfcs_datamove;
+	fe->fe_done = cfcs_done;
+
+	/* XXX KDM what should we report here? */
+	/* XXX These should probably be fetched from CTL. */
+	fe->max_targets = 1;
+	fe->max_target_id = 15;
+
+	retval = ctl_frontend_register(fe, /*master_SC*/ 1);
+	if (retval != 0) {
+		printf("%s: ctl_frontend_register() failed with error %d!\n",
+		       __func__, retval);
+		retval = 1;
+		goto bailout;
+	}
+
+	/*
+	 * Get the WWNN out of the database, and create a WWPN as well.
+	 */
+#ifdef NEEDTOPORT
+	ddb_GetWWNN((char *)wwnn);
+	softc->wwnn = be64dec(wwnn);
+	softc->wwpn = softc->wwnn + (softc->fe.targ_port & 0xff);
+#endif
+
+	/*
+	 * If the CTL frontend didn't tell us what our WWNN/WWPN is, go
+	 * ahead and set something random.
+	 */
+	if (fe->wwnn == 0) {
+		uint64_t random_bits;
+
+		arc4rand(&random_bits, sizeof(random_bits), 0);
+		softc->wwnn = (random_bits & 0x0000000fffffff00ULL) |
+			/* Company ID */ 0x5000000000000000ULL |
+			/* NL-Port */    0x0300;
+		softc->wwpn = softc->wwnn + fe->targ_port + 1;
+		fe->wwnn = softc->wwnn;
+		fe->wwpn = softc->wwpn;
+	} else {
+		softc->wwnn = fe->wwnn;
+		softc->wwpn = fe->wwpn;
+	}
+
+
+	softc->devq = cam_simq_alloc(fe->num_requested_ctl_io);
+	if (softc->devq == NULL) {
+		printf("%s: error allocating devq\n", __func__);
+		retval = ENOMEM;
+		goto bailout;
+	}
+
+	softc->sim = cam_sim_alloc(cfcs_action, cfcs_poll, softc->port_name,
+				   softc, /*unit*/ 0, &softc->lock, 1,
+				   fe->num_requested_ctl_io, softc->devq);
+	if (softc->sim == NULL) {
+		printf("%s: error allocating SIM\n", __func__);
+		retval = ENOMEM;
+		goto bailout;
+	}
+
+	mtx_lock(&softc->lock);
+	if (xpt_bus_register(softc->sim, NULL, 0) != CAM_SUCCESS) {
+		printf("%s: error registering SIM\n", __func__);
+		retval = ENOMEM;
+		goto bailout;
+	}
+
+	if (xpt_create_path(&softc->path, /*periph*/NULL,
+			    cam_sim_path(softc->sim),
+			    CAM_TARGET_WILDCARD,
+			    CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
+		printf("%s: error creating path\n", __func__);
+		xpt_bus_deregister(cam_sim_path(softc->sim));
+		retval = 1;
+		goto bailout;
+	}
+
+	mtx_unlock(&softc->lock);
+
+	xpt_setup_ccb(&csa.ccb_h, softc->path, /*priority*/ 5);
+	csa.ccb_h.func_code = XPT_SASYNC_CB;
+	csa.event_enable = AC_LOST_DEVICE;
+	csa.callback = cfcs_async;
+        csa.callback_arg = softc->sim;
+        xpt_action((union ccb *)&csa);
+
+	return (retval);
+
+bailout:
+	if (softc->sim)
+		cam_sim_free(softc->sim, /*free_devq*/ TRUE);
+	else if (softc->devq)
+		cam_simq_free(softc->devq);
+
+	mtx_unlock(&softc->lock);
+
+	return (retval);
+}
+
+static void
+cfcs_poll(struct cam_sim *sim)
+{
+
+}
+
+void
+cfcs_shutdown(void)
+{
+
+}
+
+static void
+cfcs_online(void *arg)
+{
+	struct cfcs_softc *softc;
+	union ccb *ccb;
+
+	softc = (struct cfcs_softc *)arg;
+
+	mtx_lock(&softc->lock);
+	softc->online = 1;
+	mtx_unlock(&softc->lock);
+
+	ccb = xpt_alloc_ccb_nowait();
+	if (ccb == NULL) {
+		printf("%s: unable to allocate CCB for rescan\n", __func__);
+		return;
+	}
+
+	if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
+			    cam_sim_path(softc->sim), CAM_TARGET_WILDCARD,
+			    CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
+		printf("%s: can't allocate path for rescan\n", __func__);
+		xpt_free_ccb(ccb);
+		return;
+	}
+	xpt_rescan(ccb);
+}
+
+static void
+cfcs_offline(void *arg)
+{
+	struct cfcs_softc *softc;
+	union ccb *ccb;
+
+	softc = (struct cfcs_softc *)arg;
+
+	mtx_lock(&softc->lock);
+	softc->online = 0;
+	mtx_unlock(&softc->lock);
+
+	ccb = xpt_alloc_ccb_nowait();
+	if (ccb == NULL) {
+		printf("%s: unable to allocate CCB for rescan\n", __func__);
+		return;
+	}
+
+	if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
+			    cam_sim_path(softc->sim), CAM_TARGET_WILDCARD,
+			    CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
+		printf("%s: can't allocate path for rescan\n", __func__);
+		xpt_free_ccb(ccb);
+		return;
+	}
+	xpt_rescan(ccb);
+}
+
+static int
+cfcs_targ_enable(void *arg, struct ctl_id targ_id)
+{
+	return (0);
+}
+
+static int
+cfcs_targ_disable(void *arg, struct ctl_id targ_id)
+{
+	return (0);
+}
+
+static int
+cfcs_lun_enable(void *arg, struct ctl_id target_id, int lun_id)
+{
+	return (0);
+}
+static int
+cfcs_lun_disable(void *arg, struct ctl_id target_id, int lun_id)
+{
+	return (0);
+}
+
+/*
+ * This function is very similar to ctl_ioctl_do_datamove().  Is there a
+ * way to combine the functionality?
+ *
+ * XXX KDM may need to move this into a thread.  We're doing a bcopy in the
+ * caller's context, which will usually be the backend.  That may not be a
+ * good thing.
+ */
+static void
+cfcs_datamove(union ctl_io *io)
+{
+	union ccb *ccb;
+	bus_dma_segment_t cam_sg_entry, *cam_sglist;
+	struct ctl_sg_entry ctl_sg_entry, *ctl_sglist;
+	int cam_sg_count, ctl_sg_count, cam_sg_start;
+	int cam_sg_offset;
+	int len_to_copy, len_copied;
+	int ctl_watermark, cam_watermark;
+	int i, j;
+
+
+	cam_sg_offset = 0;
+	cam_sg_start = 0;
+
+	ccb = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
+
+	/*
+	 * Note that we have a check in cfcs_action() to make sure that any
+	 * CCBs with "bad" flags are returned with CAM_REQ_INVALID.  This
+	 * is just to make sure no one removes that check without updating
+	 * this code to provide the additional functionality necessary to
+	 * support those modes of operation.
+	 */
+	KASSERT(((ccb->ccb_h.flags & CFCS_BAD_CCB_FLAGS) == 0), ("invalid "
+		  "CAM flags %#x", (ccb->ccb_h.flags & CFCS_BAD_CCB_FLAGS)));
+
+	/*
+	 * Simplify things on both sides by putting single buffers into a
+	 * single entry S/G list.
+	 */
+	if (ccb->ccb_h.flags & CAM_SCATTER_VALID) {
+		if (ccb->ccb_h.flags & CAM_SG_LIST_PHYS) {
+			/* We should filter this out on entry */
+			panic("%s: physical S/G list, should not get here",
+			      __func__);
+		} else {
+			int len_seen;
+
+			cam_sglist = (bus_dma_segment_t *)ccb->csio.data_ptr;
+			cam_sg_count = ccb->csio.sglist_cnt;
+
+			for (i = 0, len_seen = 0; i < cam_sg_count; i++) {
+				if ((len_seen + cam_sglist[i].ds_len) >=
+				     io->scsiio.kern_rel_offset) {
+					cam_sg_start = i;
+					cam_sg_offset =
+						io->scsiio.kern_rel_offset -
+						len_seen;
+					break;
+				}
+				len_seen += cam_sglist[i].ds_len;
+			}
+		}
+	} else {
+		cam_sglist = &cam_sg_entry;
+		cam_sglist[0].ds_len = ccb->csio.dxfer_len;
+		cam_sglist[0].ds_addr = (bus_addr_t)ccb->csio.data_ptr;
+		cam_sg_count = 1;
+		cam_sg_start = 0;
+		cam_sg_offset = io->scsiio.kern_rel_offset;
+	}
+
+	if (io->scsiio.kern_sg_entries > 0) {
+		ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
+		ctl_sg_count = io->scsiio.kern_sg_entries;
+	} else {
+		ctl_sglist = &ctl_sg_entry;
+		ctl_sglist->addr = io->scsiio.kern_data_ptr;
+		ctl_sglist->len = io->scsiio.kern_data_len;
+		ctl_sg_count = 1;
+	}
+
+	ctl_watermark = 0;
+	cam_watermark = cam_sg_offset;
+	len_copied = 0;
+	for (i = cam_sg_start, j = 0;
+	     i < cam_sg_count && j < ctl_sg_count;) {
+		uint8_t *cam_ptr, *ctl_ptr;
+
+		len_to_copy = ctl_min(cam_sglist[i].ds_len - cam_watermark,
+				      ctl_sglist[j].len - ctl_watermark);
+
+		cam_ptr = (uint8_t *)cam_sglist[i].ds_addr;
+		cam_ptr = cam_ptr + cam_watermark;
+		if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
+			/*
+			 * XXX KDM fix this!
+			 */
+			panic("need to implement bus address support");
+#if 0
+			kern_ptr = bus_to_virt(kern_sglist[j].addr);
+#endif
+		} else
+			ctl_ptr = (uint8_t *)ctl_sglist[j].addr;
+		ctl_ptr = ctl_ptr + ctl_watermark;
+
+		ctl_watermark += len_to_copy;
+		cam_watermark += len_to_copy;
+
+		if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
+		     CTL_FLAG_DATA_IN) {
+			CTL_DEBUG_PRINT(("%s: copying %d bytes to CAM\n",
+					 __func__, len_to_copy));
+			CTL_DEBUG_PRINT(("%s: from %p to %p\n", ctl_ptr,
+					 __func__, cam_ptr));
+			bcopy(ctl_ptr, cam_ptr, len_to_copy);
+		} else {
+			CTL_DEBUG_PRINT(("%s: copying %d bytes from CAM\n",
+					 __func__, len_to_copy));
+			CTL_DEBUG_PRINT(("%s: from %p to %p\n", cam_ptr,
+					 __func__, ctl_ptr));
+			bcopy(cam_ptr, ctl_ptr, len_to_copy);
+		}
+
+		len_copied += len_to_copy;
+
+		if (cam_sglist[i].ds_len == cam_watermark) {
+			i++;
+			cam_watermark = 0;
+		}
+
+		if (ctl_sglist[j].len == ctl_watermark) {
+			j++;
+			ctl_watermark = 0;
+		}
+	}
+
+	io->scsiio.ext_data_filled += len_copied;
+
+	io->scsiio.be_move_done(io);
+}
+
+static void
+cfcs_done(union ctl_io *io)
+{
+	union ccb *ccb;
+	struct cfcs_softc *softc;
+	struct cam_sim *sim;
+
+	ccb = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
+
+	sim = xpt_path_sim(ccb->ccb_h.path);
+	softc = (struct cfcs_softc *)cam_sim_softc(sim);
+
+	/*
+	 * At this point we should have status.  If we don't, that's a bug.
+	 */
+	KASSERT(((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE),
+		("invalid CTL status %#x", io->io_hdr.status));
+
+	/*
+	 * Translate CTL status to CAM status.
+	 */
+	switch (io->io_hdr.status & CTL_STATUS_MASK) {
+	case CTL_SUCCESS:
+		ccb->ccb_h.status = CAM_REQ_CMP;
+		break;
+	case CTL_SCSI_ERROR:
+		ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
+		ccb->csio.scsi_status = io->scsiio.scsi_status;
+		bcopy(&io->scsiio.sense_data, &ccb->csio.sense_data,
+		      min(io->scsiio.sense_len, ccb->csio.sense_len));
+		if (ccb->csio.sense_len > io->scsiio.sense_len)
+			ccb->csio.sense_resid = ccb->csio.sense_len -
+						io->scsiio.sense_len;
+		else
+			ccb->csio.sense_resid = 0;
+		if ((ccb->csio.sense_len - ccb->csio.sense_resid) >
+		     cfcs_max_sense) {
+			ccb->csio.sense_resid = ccb->csio.sense_len -
+						cfcs_max_sense;
+		}
+		break;
+	case CTL_CMD_ABORTED:
+		ccb->ccb_h.status = CAM_REQ_ABORTED;
+		break;
+	case CTL_ERROR:
+	default:
+		ccb->ccb_h.status = CAM_REQ_CMP_ERR;
+		break;
+	}
+
+	mtx_lock(sim->mtx);
+	xpt_done(ccb);
+	mtx_unlock(sim->mtx);
+
+	ctl_free_io(io);
+}
+
+void
+cfcs_action(struct cam_sim *sim, union ccb *ccb)
+{
+	struct cfcs_softc *softc;
+	int err;
+
+	softc = (struct cfcs_softc *)cam_sim_softc(sim);
+	mtx_assert(&softc->lock, MA_OWNED);
+
+	switch (ccb->ccb_h.func_code) {
+	case XPT_SCSI_IO: {
+		union ctl_io *io;
+		struct ccb_scsiio *csio;
+
+		csio = &ccb->csio;
+
+		/*
+		 * Catch CCB flags, like physical address flags, that
+	 	 * indicate situations we currently can't handle.
+		 */
+		if (ccb->ccb_h.flags & CFCS_BAD_CCB_FLAGS) {
+			ccb->ccb_h.status = CAM_REQ_INVALID;
+			printf("%s: bad CCB flags %#x (all flags %#x)\n",
+			       __func__, ccb->ccb_h.flags & CFCS_BAD_CCB_FLAGS,
+			       ccb->ccb_h.flags);
+			xpt_done(ccb);
+			return;
+		}
+
+		/*
+		 * If we aren't online, there are no devices to see.
+		 */
+		if (softc->online == 0) {
+			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
+			xpt_done(ccb);
+			return;
+		}
+
+		io = ctl_alloc_io(softc->fe.ctl_pool_ref);
+		if (io == NULL) {
+			printf("%s: can't allocate ctl_io\n", __func__);
+			ccb->ccb_h.status = CAM_BUSY | CAM_DEV_QFRZN;
+			xpt_freeze_devq(ccb->ccb_h.path, 1);
+			xpt_done(ccb);
+			return;
+		}
+		ctl_zero_io(io);
+		/* Save pointers on both sides */
+		io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ccb;
+		ccb->ccb_h.io_ptr = io;
+
+		/*
+		 * Only SCSI I/O comes down this path, resets, etc. come
+		 * down via the XPT_RESET_BUS/LUN CCBs below.
+		 */
+		io->io_hdr.io_type = CTL_IO_SCSI;
+		io->io_hdr.nexus.initid.id = 1;
+		io->io_hdr.nexus.targ_port = softc->fe.targ_port;
+		/*
+		 * XXX KDM how do we handle target IDs?
+		 */
+		io->io_hdr.nexus.targ_target.id = ccb->ccb_h.target_id;
+		io->io_hdr.nexus.targ_lun = ccb->ccb_h.target_lun;
+		/*
+		 * This tag scheme isn't the best, since we could in theory
+		 * have a very long-lived I/O and tag collision, especially
+		 * in a high I/O environment.  But it should work well
+		 * enough for now.  Since we're using unsigned ints,
+		 * they'll just wrap around.
+		 */
+		io->scsiio.tag_num = softc->cur_tag_num++;
+		csio->tag_id = io->scsiio.tag_num;
+		switch (csio->tag_action) {
+		case CAM_TAG_ACTION_NONE:
+			io->scsiio.tag_type = CTL_TAG_UNTAGGED;
+			break;
+		case MSG_SIMPLE_TASK:
+			io->scsiio.tag_type = CTL_TAG_SIMPLE;
+			break;
+		case MSG_HEAD_OF_QUEUE_TASK:
+        		io->scsiio.tag_type = CTL_TAG_HEAD_OF_QUEUE;
+			break;
+		case MSG_ORDERED_TASK:
+        		io->scsiio.tag_type = CTL_TAG_ORDERED;
+			break;
+		case MSG_ACA_TASK:
+			io->scsiio.tag_type = CTL_TAG_ACA;
+			break;
+		default:
+			io->scsiio.tag_type = CTL_TAG_UNTAGGED;
+			printf("%s: unhandled tag type %#x!!\n", __func__,
+			       csio->tag_action);
+			break;
+		}
+		if (csio->cdb_len > sizeof(io->scsiio.cdb)) {
+			printf("%s: WARNING: CDB len %d > ctl_io space %zd\n",
+			       __func__, csio->cdb_len, sizeof(io->scsiio.cdb));
+		}
+		io->scsiio.cdb_len = min(csio->cdb_len, sizeof(io->scsiio.cdb));
+		bcopy(csio->cdb_io.cdb_bytes, io->scsiio.cdb,
+		      io->scsiio.cdb_len);
+
+		err = ctl_queue(io);
+		if (err != CTL_RETVAL_COMPLETE) {
+			printf("%s: func %d: error %d returned by "
+			       "ctl_queue()!\n", __func__,
+			       ccb->ccb_h.func_code, err);
+			ctl_free_io(io);
+		} else {
+			ccb->ccb_h.status |= CAM_SIM_QUEUED;
+		}
+		break;
+	}
+	case XPT_ABORT: {
+		union ctl_io *io;
+		union ccb *abort_ccb;
+
+		abort_ccb = ccb->cab.abort_ccb;
+
+		if (abort_ccb->ccb_h.func_code != XPT_SCSI_IO) {
+			ccb->ccb_h.status = CAM_REQ_INVALID;
+			xpt_done(ccb);
+		}
+
+		/*
+		 * If we aren't online, there are no devices to talk to.
+		 */
+		if (softc->online == 0) {
+			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
+			xpt_done(ccb);
+			return;
+		}
+
+		io = ctl_alloc_io(softc->fe.ctl_pool_ref);
+		if (io == NULL) {
+			ccb->ccb_h.status = CAM_BUSY | CAM_DEV_QFRZN;
+			xpt_freeze_devq(ccb->ccb_h.path, 1);
+			xpt_done(ccb);
+			return;
+		}
+
+		ctl_zero_io(io);
+		/* Save pointers on both sides */
+		io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ccb;
+		ccb->ccb_h.io_ptr = io;
+
+		io->io_hdr.io_type = CTL_IO_TASK;
+		io->io_hdr.nexus.initid.id = 1;
+		io->io_hdr.nexus.targ_port = softc->fe.targ_port;
+		io->io_hdr.nexus.targ_target.id = ccb->ccb_h.target_id;
+		io->io_hdr.nexus.targ_lun = ccb->ccb_h.target_lun;
+		io->taskio.task_action = CTL_TASK_ABORT_TASK;
+		io->taskio.tag_num = abort_ccb->csio.tag_id;
+		switch (abort_ccb->csio.tag_action) {
+		case CAM_TAG_ACTION_NONE:
+			io->taskio.tag_type = CTL_TAG_UNTAGGED;
+			break;
+		case MSG_SIMPLE_TASK:
+			io->taskio.tag_type = CTL_TAG_SIMPLE;
+			break;
+		case MSG_HEAD_OF_QUEUE_TASK:
+        		io->taskio.tag_type = CTL_TAG_HEAD_OF_QUEUE;
+			break;
+		case MSG_ORDERED_TASK:
+        		io->taskio.tag_type = CTL_TAG_ORDERED;
+			break;
+		case MSG_ACA_TASK:
+			io->taskio.tag_type = CTL_TAG_ACA;
+			break;
+		default:
+			io->taskio.tag_type = CTL_TAG_UNTAGGED;
+			printf("%s: unhandled tag type %#x!!\n", __func__,
+			       abort_ccb->csio.tag_action);
+			break;
+		}
+		err = ctl_queue(io);
+		if (err != CTL_RETVAL_COMPLETE) {
+			printf("%s func %d: error %d returned by "
+			       "ctl_queue()!\n", __func__,
+			       ccb->ccb_h.func_code, err);
+			ctl_free_io(io);
+		}
+		break;
+	}
+	case XPT_GET_TRAN_SETTINGS: {
+		struct ccb_trans_settings *cts;
+		struct ccb_trans_settings_scsi *scsi;
+		struct ccb_trans_settings_fc *fc;
+
+		cts = &ccb->cts;
+		scsi = &cts->proto_specific.scsi;
+		fc = &cts->xport_specific.fc;
+
+		
+		cts->protocol = PROTO_SCSI;
+		cts->protocol_version = SCSI_REV_SPC2;
+		cts->transport = XPORT_FC;
+		cts->transport_version = 0;
+
+		scsi->valid = CTS_SCSI_VALID_TQ;
+		scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
+		fc->valid = CTS_FC_VALID_SPEED;
+		fc->bitrate = 800000;
+		fc->wwnn = softc->wwnn;
+		fc->wwpn = softc->wwpn;
+       		fc->port = softc->fe.targ_port;
+		fc->valid |= CTS_FC_VALID_WWNN | CTS_FC_VALID_WWPN |
+			CTS_FC_VALID_PORT; 
+		ccb->ccb_h.status = CAM_REQ_CMP;
+		break;
+	}
+	case XPT_SET_TRAN_SETTINGS:
+		/* XXX KDM should we actually do something here? */
+		ccb->ccb_h.status = CAM_REQ_CMP;
+		break;
+	case XPT_RESET_BUS:
+	case XPT_RESET_DEV: {
+		union ctl_io *io;
+
+		/*
+		 * If we aren't online, there are no devices to talk to.
+		 */
+		if (softc->online == 0) {
+			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
+			xpt_done(ccb);
+			return;
+		}
+
+		io = ctl_alloc_io(softc->fe.ctl_pool_ref);
+		if (io == NULL) {
+			ccb->ccb_h.status = CAM_BUSY | CAM_DEV_QFRZN;
+			xpt_freeze_devq(ccb->ccb_h.path, 1);
+			xpt_done(ccb);
+			return;
+		}
+
+		ctl_zero_io(io);
+		/* Save pointers on both sides */
+		io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ccb;
+		ccb->ccb_h.io_ptr = io;
+
+		io->io_hdr.io_type = CTL_IO_TASK;
+		io->io_hdr.nexus.initid.id = 0;
+		io->io_hdr.nexus.targ_port = softc->fe.targ_port;
+		io->io_hdr.nexus.targ_target.id = ccb->ccb_h.target_id;
+		io->io_hdr.nexus.targ_lun = ccb->ccb_h.target_lun;
+		if (ccb->ccb_h.func_code == XPT_RESET_BUS)
+			io->taskio.task_action = CTL_TASK_BUS_RESET;
+		else
+			io->taskio.task_action = CTL_TASK_LUN_RESET;
+
+		err = ctl_queue(io);
+		if (err != CTL_RETVAL_COMPLETE) {
+			printf("%s func %d: error %d returned by "
+			      "ctl_queue()!\n", __func__,
+			      ccb->ccb_h.func_code, err);
+			ctl_free_io(io);
+		}
+		break;
+	}
+	case XPT_CALC_GEOMETRY:
+		cam_calc_geometry(&ccb->ccg, 1);
+		xpt_done(ccb);
+		break;
+	case XPT_PATH_INQ: {
+		struct ccb_pathinq *cpi;
+
+		cpi = &ccb->cpi;
+
+		cpi->version_num = 0;
+		cpi->hba_inquiry = PI_TAG_ABLE;
+		cpi->target_sprt = 0;
+		cpi->hba_misc = 0;
+		cpi->hba_eng_cnt = 0;
+		cpi->max_target = 1;
+		cpi->max_lun = 1024;
+		/* Do we really have a limit? */
+		cpi->maxio = 1024 * 1024;
+		cpi->async_flags = 0;
+		cpi->hpath_id = 0;
+		cpi->initiator_id = 0;
+
+		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
+		strncpy(cpi->hba_vid, "FreeBSD", HBA_IDLEN);
+		strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
+		cpi->unit_number = 0;
+		cpi->bus_id = 0;
+		cpi->base_transfer_speed = 800000;
+		cpi->protocol = PROTO_SCSI;
+		cpi->protocol_version = SCSI_REV_SPC2;
+		/*
+		 * Pretend to be Fibre Channel.
+		 */
+		cpi->transport = XPORT_FC;
+		cpi->transport_version = 0;
+		cpi->xport_specific.fc.wwnn = softc->wwnn;
+		cpi->xport_specific.fc.wwpn = softc->wwpn;
+		cpi->xport_specific.fc.port = softc->fe.targ_port;
+		cpi->xport_specific.fc.bitrate = 8 * 1000 * 1000;
+		cpi->ccb_h.status = CAM_REQ_CMP;
+		break;
+	}
+	default:
+		ccb->ccb_h.status = CAM_PROVIDE_FAIL;
+		printf("%s: unsupported CCB type %#x\n", __func__,
+		       ccb->ccb_h.func_code);
+		xpt_done(ccb);
+		break;
+	}
+}
+
+static void
+cfcs_async(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
+{
+
+}
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/cam/ctl/ctl_frontend_internal.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/sys/cam/ctl/ctl_frontend_internal.c	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,1782 @@
+/*-
+ * Copyright (c) 2004, 2005 Silicon Graphics International Corp.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions, and the following disclaimer,
+ *    without modification.
+ * 2. Redistributions in binary form must reproduce at minimum a disclaimer
+ *    substantially similar to the "NO WARRANTY" disclaimer below
+ *    ("Disclaimer") and any redistribution must be conditioned upon
+ *    including a substantially similar Disclaimer requirement for further
+ *    binary redistribution.
+ *
+ * NO WARRANTY
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGES.
+ *
+ * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_frontend_internal.c#5 $
+ */
+/*
+ * CTL kernel internal frontend target driver.  This allows kernel-level
+ * clients to send commands into CTL.
+ *
+ * This has elements of a FETD (e.g. it has to set tag numbers, initiator,
+ * port, target, and LUN) and elements of an initiator (LUN discovery and
+ * probing, error recovery, command initiation).  Even though this has some
+ * initiator type elements, this is not intended to be a full fledged
+ * initiator layer.  It is only intended to send a limited number of
+ * commands to a well known target layer.
+ *
+ * To be able to fulfill the role of a full initiator layer, it would need
+ * a whole lot more functionality.
+ *
+ * Author: Ken Merry <ken at FreeBSD.org>
+ *
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD: head/sys/cam/ctl/ctl_frontend_internal.c 229997 2012-01-12 00:34:33Z ken $");
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/kernel.h>
+#include <sys/types.h>
+#include <sys/malloc.h>
+#include <sys/lock.h>
+#include <sys/mutex.h>
+#include <sys/condvar.h>
+#include <sys/queue.h>
+#include <sys/sbuf.h>
+#include <cam/scsi/scsi_all.h>
+#include <cam/scsi/scsi_da.h>
+#include <cam/ctl/ctl_io.h>
+#include <cam/ctl/ctl.h>
+#include <cam/ctl/ctl_frontend.h>
+#include <cam/ctl/ctl_frontend_internal.h>
+#include <cam/ctl/ctl_backend.h>
+#include <cam/ctl/ctl_ioctl.h>
+#include <cam/ctl/ctl_util.h>
+#include <cam/ctl/ctl_ha.h>
+#include <cam/ctl/ctl_private.h>
+#include <cam/ctl/ctl_mem_pool.h>
+#include <cam/ctl/ctl_debug.h>
+#include <cam/ctl/ctl_scsi_all.h>
+#include <cam/ctl/ctl_error.h>
+
+/*
+ * Task structure:
+ *  - overall metatask, different potential metatask types (e.g. forced
+ *    shutdown, gentle shutdown)
+ *  - forced shutdown metatask:
+ *     - states:  report luns, pending, done?
+ *     - list of luns pending, with the relevant I/O for that lun attached.
+ *       This would allow moving ahead on LUNs with no errors, and going
+ *       into error recovery on LUNs with problems.  Per-LUN states might
+ *       include inquiry, stop/offline, done.
+ *
+ * Use LUN enable for LUN list instead of getting it manually?  We'd still
+ * need inquiry data for each LUN.
+ *
+ * How to handle processor LUN w.r.t. found/stopped counts?
+ */
+#ifdef oldapi
+typedef enum {
+	CFI_TASK_NONE,
+	CFI_TASK_SHUTDOWN,
+	CFI_TASK_STARTUP
+} cfi_tasktype;
+
+struct cfi_task_startstop {
+	int total_luns;
+	int luns_complete;
+	int luns_failed;
+	cfi_cb_t callback;
+	void *callback_arg;
+	/* XXX KDM add more fields here */
+};
+
+union cfi_taskinfo {
+	struct cfi_task_startstop startstop;
+};
+
+struct cfi_metatask {
+	cfi_tasktype		tasktype;
+	cfi_mt_status		status;
+	union cfi_taskinfo	taskinfo;
+	struct ctl_mem_element	*element;
+	void			*cfi_context;
+	STAILQ_ENTRY(cfi_metatask) links;
+};
+#endif
+
+typedef enum {
+	CFI_ERR_RETRY		= 0x000,
+	CFI_ERR_FAIL		= 0x001,
+	CFI_ERR_LUN_RESET	= 0x002,
+	CFI_ERR_MASK		= 0x0ff,
+	CFI_ERR_NO_DECREMENT	= 0x100
+} cfi_error_action;
+
+typedef enum {
+	CFI_ERR_SOFT,
+	CFI_ERR_HARD
+} cfi_error_policy;
+
+typedef enum {
+	CFI_LUN_INQUIRY,
+	CFI_LUN_READCAPACITY,
+	CFI_LUN_READCAPACITY_16,
+	CFI_LUN_READY
+} cfi_lun_state;
+
+struct cfi_lun {
+	struct ctl_id target_id;
+	int lun_id;
+	struct scsi_inquiry_data inq_data;
+	uint64_t num_blocks;
+	uint32_t blocksize;
+	int blocksize_powerof2;
+	uint32_t cur_tag_num;
+	cfi_lun_state state;
+	struct ctl_mem_element *element;
+	struct cfi_softc *softc;
+	STAILQ_HEAD(, cfi_lun_io) io_list;
+	STAILQ_ENTRY(cfi_lun) links;
+};
+
+struct cfi_lun_io {
+	struct cfi_lun *lun;
+	struct cfi_metatask *metatask;
+	cfi_error_policy policy;
+	void (*done_function)(union ctl_io *io);
+	union ctl_io *ctl_io;
+	struct cfi_lun_io *orig_lun_io;
+	STAILQ_ENTRY(cfi_lun_io) links;
+};
+
+typedef enum {
+	CFI_NONE	= 0x00,
+	CFI_ONLINE	= 0x01,
+} cfi_flags;
+
+struct cfi_softc {
+	struct ctl_frontend fe;
+	char fe_name[40];
+	struct mtx lock;
+	cfi_flags flags;
+	STAILQ_HEAD(, cfi_lun) lun_list;
+	STAILQ_HEAD(, cfi_metatask) metatask_list;
+	struct ctl_mem_pool lun_pool;
+	struct ctl_mem_pool metatask_pool;
+};
+
+MALLOC_DEFINE(M_CTL_CFI, "ctlcfi", "CTL CFI");
+
+static struct cfi_softc fetd_internal_softc;
+
+void cfi_init(void);
+void cfi_shutdown(void) __unused;
+static void cfi_online(void *arg);
+static void cfi_offline(void *arg);
+static int cfi_targ_enable(void *arg, struct ctl_id targ_id);
+static int cfi_targ_disable(void *arg, struct ctl_id targ_id);
+static int cfi_lun_enable(void *arg, struct ctl_id target_id, int lun_id);
+static int cfi_lun_disable(void *arg, struct ctl_id target_id, int lun_id);
+static void cfi_datamove(union ctl_io *io);
+static cfi_error_action cfi_checkcond_parse(union ctl_io *io,
+					    struct cfi_lun_io *lun_io);
+static cfi_error_action cfi_error_parse(union ctl_io *io,
+					struct cfi_lun_io *lun_io);
+static void cfi_init_io(union ctl_io *io, struct cfi_lun *lun,
+			struct cfi_metatask *metatask, cfi_error_policy policy,
+			int retries, struct cfi_lun_io *orig_lun_io,
+			void (*done_function)(union ctl_io *io));
+static void cfi_done(union ctl_io *io);
+static void cfi_lun_probe_done(union ctl_io *io);
+static void cfi_lun_probe(struct cfi_lun *lun, int have_lock);
+static void cfi_metatask_done(struct cfi_softc *softc,
+			      struct cfi_metatask *metatask);
+static void cfi_metatask_bbr_errorparse(struct cfi_metatask *metatask,
+					union ctl_io *io);
+static void cfi_metatask_io_done(union ctl_io *io);
+static void cfi_err_recovery_done(union ctl_io *io);
+static void cfi_lun_io_done(union ctl_io *io);
+
+SYSINIT(cfi_init, SI_SUB_CONFIGURE, SI_ORDER_FOURTH, cfi_init, NULL);
+
+void
+cfi_init(void)
+{
+	struct cfi_softc *softc;
+	struct ctl_frontend *fe;
+	int retval;
+
+	softc = &fetd_internal_softc;
+
+	fe = &softc->fe;
+
+	retval = 0;
+
+	if (sizeof(struct cfi_lun_io) > CTL_PORT_PRIV_SIZE) {
+		printf("%s: size of struct cfi_lun_io %zd > "
+		       "CTL_PORT_PRIV_SIZE %d\n", __func__,
+		       sizeof(struct cfi_lun_io),
+		       CTL_PORT_PRIV_SIZE);
+	}
+	memset(softc, 0, sizeof(softc));
+
+	mtx_init(&softc->lock, "CTL frontend mutex", NULL, MTX_DEF);
+	softc->flags |= CTL_FLAG_MASTER_SHELF;
+
+	STAILQ_INIT(&softc->lun_list);
+	STAILQ_INIT(&softc->metatask_list);
+	sprintf(softc->fe_name, "CTL internal");
+	fe->port_type = CTL_PORT_INTERNAL;
+	fe->num_requested_ctl_io = 100;
+	fe->port_name = softc->fe_name;
+	fe->port_online = cfi_online;
+	fe->port_offline = cfi_offline;
+	fe->onoff_arg = softc;
+	fe->targ_enable = cfi_targ_enable;
+	fe->targ_disable = cfi_targ_disable;
+	fe->lun_enable = cfi_lun_enable;
+	fe->lun_disable = cfi_lun_disable;
+	fe->targ_lun_arg = softc;
+	fe->fe_datamove = cfi_datamove;
+	fe->fe_done = cfi_done;
+	fe->max_targets = 15;
+	fe->max_target_id = 15;
+
+	if (ctl_frontend_register(fe, (softc->flags & CTL_FLAG_MASTER_SHELF)) != 0) 
+	{
+		printf("%s: internal frontend registration failed\n", __func__);
+		retval = 1;
+		goto bailout;
+	}
+
+	if (ctl_init_mem_pool(&softc->lun_pool,
+			      sizeof(struct cfi_lun),
+			      CTL_MEM_POOL_PERM_GROW, /*grow_inc*/ 3,
+			      /* initial_pool_size */ CTL_MAX_LUNS) != 0) {
+		printf("%s: can't initialize LUN memory pool\n", __func__);
+		retval = 1;
+		goto bailout_error;
+	}
+
+	if (ctl_init_mem_pool(&softc->metatask_pool,
+			      sizeof(struct cfi_metatask),
+			      CTL_MEM_POOL_PERM_GROW, /*grow_inc*/ 3,
+			      /*initial_pool_size*/ 10) != 0) {
+		printf("%s: can't initialize metatask memory pool\n", __func__);
+		retval = 2;
+		goto bailout_error;
+	}
+bailout:
+
+	return;
+
+bailout_error:
+
+	switch (retval) {
+	case 3:
+		ctl_shrink_mem_pool(&softc->metatask_pool);
+		/* FALLTHROUGH */
+	case 2:
+		ctl_shrink_mem_pool(&softc->lun_pool);
+		/* FALLTHROUGH */
+	case 1:
+		ctl_frontend_deregister(fe);
+		break;
+	default:
+		break;
+	}
+}
+
+void
+cfi_shutdown(void)
+{
+	struct cfi_softc *softc;
+
+	softc = &fetd_internal_softc;
+
+	/*
+	 * XXX KDM need to clear out any I/O pending on each LUN.
+	 */
+	if (ctl_frontend_deregister(&softc->fe) != 0)
+		printf("%s: ctl_frontend_deregister() failed\n", __func__);
+
+	if (ctl_shrink_mem_pool(&softc->lun_pool) != 0)
+		printf("%s: error shrinking LUN pool\n", __func__);
+
+	if (ctl_shrink_mem_pool(&softc->metatask_pool) != 0)
+		printf("%s: error shrinking LUN pool\n", __func__);
+}
+
+static void
+cfi_online(void *arg)
+{
+	struct cfi_softc *softc;
+	struct cfi_lun *lun;
+
+	softc = (struct cfi_softc *)arg;
+
+	softc->flags |= CFI_ONLINE;
+
+	/*
+	 * Go through and kick off the probe for each lun.  Should we check
+	 * the LUN flags here to determine whether or not to probe it?
+	 */
+	mtx_lock(&softc->lock);
+	STAILQ_FOREACH(lun, &softc->lun_list, links)
+		cfi_lun_probe(lun, /*have_lock*/ 1);
+	mtx_unlock(&softc->lock);
+}
+
+static void
+cfi_offline(void *arg)
+{
+	struct cfi_softc *softc;
+
+	softc = (struct cfi_softc *)arg;
+
+	softc->flags &= ~CFI_ONLINE;
+}
+
+static int
+cfi_targ_enable(void *arg, struct ctl_id targ_id)
+{
+	return (0);
+}
+
+static int
+cfi_targ_disable(void *arg, struct ctl_id targ_id)
+{
+	return (0);
+}
+
+static int
+cfi_lun_enable(void *arg, struct ctl_id target_id, int lun_id)
+{
+	struct ctl_mem_element *element;
+	struct cfi_softc *softc;
+	struct cfi_lun *lun;
+	int found;
+
+	softc = (struct cfi_softc *)arg;
+
+	found = 0;
+	mtx_lock(&softc->lock);
+	STAILQ_FOREACH(lun, &softc->lun_list, links) {
+		if ((lun->target_id.id == target_id.id)
+		 && (lun->lun_id == lun_id)) {
+			found = 1;
+			break;
+		}
+	}
+	mtx_unlock(&softc->lock);
+
+	/*
+	 * If we already have this target/LUN, there is no reason to add
+	 * it to our lists again.
+	 */
+	if (found != 0)
+		return (0);
+
+	element = ctl_alloc_mem_element(&softc->lun_pool, /*can_wait*/ 0);
+
+	if (element == NULL) {
+		printf("%s: unable to allocate LUN structure\n", __func__);
+		return (1);
+	}
+
+	lun = (struct cfi_lun *)element->bytes;
+
+	lun->element = element;
+	lun->target_id = target_id;
+	lun->lun_id = lun_id;
+	lun->cur_tag_num = 0;
+	lun->state = CFI_LUN_INQUIRY;
+	lun->softc = softc;
+	STAILQ_INIT(&lun->io_list);
+
+	mtx_lock(&softc->lock);
+	STAILQ_INSERT_TAIL(&softc->lun_list, lun, links);
+	mtx_unlock(&softc->lock);
+
+	cfi_lun_probe(lun, /*have_lock*/ 0);
+
+	return (0);
+}
+
+static int
+cfi_lun_disable(void *arg, struct ctl_id target_id, int lun_id)
+{
+	struct cfi_softc *softc;
+	struct cfi_lun *lun;
+	int found;
+
+	softc = (struct cfi_softc *)arg;
+
+	found = 0;
+
+	/*
+	 * XXX KDM need to do an invalidate and then a free when any
+	 * pending I/O has completed.  Or do we?  CTL won't free a LUN
+	 * while any I/O is pending.  So we won't get this notification
+	 * unless any I/O we have pending on a LUN has completed.
+	 */
+	mtx_lock(&softc->lock);
+	STAILQ_FOREACH(lun, &softc->lun_list, links) {
+		if ((lun->target_id.id == target_id.id)
+		 && (lun->lun_id == lun_id)) {
+			found = 1;
+			break;
+		}
+	}
+	if (found != 0)
+		STAILQ_REMOVE(&softc->lun_list, lun, cfi_lun, links);
+
+	mtx_unlock(&softc->lock);
+
+	if (found == 0) {
+		printf("%s: can't find target %ju lun %d\n", __func__,
+		       (uintmax_t)target_id.id, lun_id);
+		return (1);
+	}
+
+	ctl_free_mem_element(lun->element);
+
+	return (0);
+}
+
+/*
+ * XXX KDM run this inside a thread, or inside the caller's context?
+ */
+static void
+cfi_datamove(union ctl_io *io)
+{
+	struct ctl_sg_entry *ext_sglist, *kern_sglist;
+	struct ctl_sg_entry ext_entry, kern_entry;
+	int ext_sglen, ext_sg_entries, kern_sg_entries;
+	int ext_sg_start, ext_offset;
+	int len_to_copy, len_copied;
+	int kern_watermark, ext_watermark;
+	int ext_sglist_malloced;
+	struct ctl_scsiio *ctsio;
+	int i, j;
+
+	ext_sglist_malloced = 0;
+	ext_sg_start = 0;
+	ext_offset = 0;
+	ext_sglist = NULL;
+
+	CTL_DEBUG_PRINT(("%s\n", __func__));
+
+	ctsio = &io->scsiio;
+
+	/*
+	 * If this is the case, we're probably doing a BBR read and don't
+	 * actually need to transfer the data.  This will effectively
+	 * bit-bucket the data.
+	 */
+	if (ctsio->ext_data_ptr == NULL)
+		goto bailout;
+
+	/*
+	 * To simplify things here, if we have a single buffer, stick it in
+	 * a S/G entry and just make it a single entry S/G list.
+	 */
+	if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) {
+		int len_seen;
+
+		ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist);
+
+		/*
+		 * XXX KDM GFP_KERNEL, don't know what the caller's context
+		 * is.  Need to figure that out.
+		 */
+		ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL_CFI,
+							   M_WAITOK);
+		if (ext_sglist == NULL) {
+			ctl_set_internal_failure(ctsio,
+						 /*sks_valid*/ 0,
+						 /*retry_count*/ 0);
+			return;
+		}
+		ext_sglist_malloced = 1;
+		if (memcpy(ext_sglist, ctsio->ext_data_ptr, ext_sglen) != 0) {
+			ctl_set_internal_failure(ctsio,
+						 /*sks_valid*/ 0,
+						 /*retry_count*/ 0);
+			goto bailout;
+		}
+		ext_sg_entries = ctsio->ext_sg_entries;
+		len_seen = 0;
+		for (i = 0; i < ext_sg_entries; i++) {
+			if ((len_seen + ext_sglist[i].len) >=
+			     ctsio->ext_data_filled) {
+				ext_sg_start = i;
+				ext_offset = ctsio->ext_data_filled - len_seen;
+				break;
+			}
+			len_seen += ext_sglist[i].len;
+		}
+	} else {
+		ext_sglist = &ext_entry;
+		ext_sglist->addr = ctsio->ext_data_ptr;
+		ext_sglist->len = ctsio->ext_data_len;
+		ext_sg_entries = 1;
+		ext_sg_start = 0;
+		ext_offset = ctsio->ext_data_filled;
+	}
+
+	if (ctsio->kern_sg_entries > 0) {
+		kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr;
+		kern_sg_entries = ctsio->kern_sg_entries;
+	} else {
+		kern_sglist = &kern_entry;
+		kern_sglist->addr = ctsio->kern_data_ptr;
+		kern_sglist->len = ctsio->kern_data_len;
+		kern_sg_entries = 1;
+	}
+
+
+	kern_watermark = 0;
+	ext_watermark = ext_offset;
+	len_copied = 0;
+	for (i = ext_sg_start, j = 0;
+	     i < ext_sg_entries && j < kern_sg_entries;) {
+		uint8_t *ext_ptr, *kern_ptr;
+
+		len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark,
+				      kern_sglist[j].len - kern_watermark);
+
+		ext_ptr = (uint8_t *)ext_sglist[i].addr;
+		ext_ptr = ext_ptr + ext_watermark;
+		if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
+			/*
+			 * XXX KDM fix this!
+			 */
+			panic("need to implement bus address support");
+#if 0
+			kern_ptr = bus_to_virt(kern_sglist[j].addr);
+#endif
+		} else
+			kern_ptr = (uint8_t *)kern_sglist[j].addr;
+		kern_ptr = kern_ptr + kern_watermark;
+
+		kern_watermark += len_to_copy;
+		ext_watermark += len_to_copy;
+		
+		if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
+		     CTL_FLAG_DATA_IN) {
+			CTL_DEBUG_PRINT(("%s: copying %d bytes to user\n",
+					 __func__, len_to_copy));
+			CTL_DEBUG_PRINT(("%s: from %p to %p\n", __func__,
+					 kern_ptr, ext_ptr));
+			memcpy(ext_ptr, kern_ptr, len_to_copy);
+		} else {
+			CTL_DEBUG_PRINT(("%s: copying %d bytes from user\n",
+					 __func__, len_to_copy));
+			CTL_DEBUG_PRINT(("%s: from %p to %p\n", __func__,
+					 ext_ptr, kern_ptr));
+			memcpy(kern_ptr, ext_ptr, len_to_copy);
+		}
+
+		len_copied += len_to_copy;
+
+		if (ext_sglist[i].len == ext_watermark) {
+			i++;
+			ext_watermark = 0;
+		}
+
+		if (kern_sglist[j].len == kern_watermark) {
+			j++;
+			kern_watermark = 0;
+		}
+	}
+
+	ctsio->ext_data_filled += len_copied;
+
+	CTL_DEBUG_PRINT(("%s: ext_sg_entries: %d, kern_sg_entries: %d\n",
+			 __func__, ext_sg_entries, kern_sg_entries));
+	CTL_DEBUG_PRINT(("%s: ext_data_len = %d, kern_data_len = %d\n",
+			 __func__, ctsio->ext_data_len, ctsio->kern_data_len));
+	
+
+	/* XXX KDM set residual?? */
+bailout:
+
+	if (ext_sglist_malloced != 0)
+		free(ext_sglist, M_CTL_CFI);
+
+	io->scsiio.be_move_done(io);
+
+	return;
+}
+
+/*
+ * For any sort of check condition, busy, etc., we just retry.  We do not
+ * decrement the retry count for unit attention type errors.  These are
+ * normal, and we want to save the retry count for "real" errors.  Otherwise,
+ * we could end up with situations where a command will succeed in some
+ * situations and fail in others, depending on whether a unit attention is
+ * pending.  Also, some of our error recovery actions, most notably the
+ * LUN reset action, will cause a unit attention.
+ *
+ * We can add more detail here later if necessary.
+ */
+static cfi_error_action
+cfi_checkcond_parse(union ctl_io *io, struct cfi_lun_io *lun_io)
+{
+	cfi_error_action error_action;
+	int error_code, sense_key, asc, ascq;
+
+	/*
+	 * Default to retrying the command.
+	 */
+	error_action = CFI_ERR_RETRY;
+
+	scsi_extract_sense_len(&io->scsiio.sense_data,
+			       io->scsiio.sense_len,
+			       &error_code,
+			       &sense_key,
+			       &asc,
+			       &ascq,
+			       /*show_errors*/ 1);
+
+	switch (error_code) {
+	case SSD_DEFERRED_ERROR:
+	case SSD_DESC_DEFERRED_ERROR:
+		error_action |= CFI_ERR_NO_DECREMENT;
+		break;
+	case SSD_CURRENT_ERROR:
+	case SSD_DESC_CURRENT_ERROR:
+	default: {
+		switch (sense_key) {
+		case SSD_KEY_UNIT_ATTENTION:
+			error_action |= CFI_ERR_NO_DECREMENT;
+			break;
+		case SSD_KEY_HARDWARE_ERROR:
+			/*
+			 * This is our generic "something bad happened"
+			 * error code.  It often isn't recoverable.
+			 */
+			if ((asc == 0x44) && (ascq == 0x00))
+				error_action = CFI_ERR_FAIL;
+			break;
+		case SSD_KEY_NOT_READY:
+			/*
+			 * If the LUN is powered down, there likely isn't
+			 * much point in retrying right now.
+			 */
+			if ((asc == 0x04) && (ascq == 0x02))
+				error_action = CFI_ERR_FAIL;
+			/*
+			 * If the LUN is offline, there probably isn't much
+			 * point in retrying, either.
+			 */
+			if ((asc == 0x04) && (ascq == 0x03))
+				error_action = CFI_ERR_FAIL;
+			break;
+		}
+	}
+	}
+
+	return (error_action);
+}
+
+static cfi_error_action
+cfi_error_parse(union ctl_io *io, struct cfi_lun_io *lun_io)
+{
+	cfi_error_action error_action;
+
+	error_action = CFI_ERR_RETRY;
+
+	switch (io->io_hdr.io_type) {
+	case CTL_IO_SCSI:
+		switch (io->io_hdr.status & CTL_STATUS_MASK) {
+		case CTL_SCSI_ERROR:
+			switch (io->scsiio.scsi_status) {
+			case SCSI_STATUS_RESERV_CONFLICT:
+				/*
+				 * For a reservation conflict, we'll usually
+				 * want the hard error recovery policy, so
+				 * we'll reset the LUN.
+				 */
+				if (lun_io->policy == CFI_ERR_HARD)
+					error_action =
+						CFI_ERR_LUN_RESET;
+				else
+					error_action = 
+						CFI_ERR_RETRY;
+				break;
+			case SCSI_STATUS_CHECK_COND:
+			default:
+				error_action = cfi_checkcond_parse(io, lun_io);
+				break;
+			}
+			break;
+		default:
+			error_action = CFI_ERR_RETRY;
+			break;
+		}
+		break;
+	case CTL_IO_TASK:
+		/*
+		 * In theory task management commands shouldn't fail...
+		 */
+		error_action = CFI_ERR_RETRY;
+		break;
+	default:
+		printf("%s: invalid ctl_io type %d\n", __func__,
+		       io->io_hdr.io_type);
+		panic("%s: invalid ctl_io type %d\n", __func__,
+		      io->io_hdr.io_type);
+		break;
+	}
+
+	return (error_action);
+}
+
+static void
+cfi_init_io(union ctl_io *io, struct cfi_lun *lun,
+	    struct cfi_metatask *metatask, cfi_error_policy policy, int retries,
+	    struct cfi_lun_io *orig_lun_io,
+	    void (*done_function)(union ctl_io *io))
+{
+	struct cfi_lun_io *lun_io;
+
+	io->io_hdr.nexus.initid.id = 7;
+	io->io_hdr.nexus.targ_port = lun->softc->fe.targ_port;
+	io->io_hdr.nexus.targ_target.id = lun->target_id.id;
+	io->io_hdr.nexus.targ_lun = lun->lun_id;
+	io->io_hdr.retries = retries;
+	lun_io = (struct cfi_lun_io *)io->io_hdr.port_priv;
+	io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = lun_io;
+	lun_io->lun = lun;
+	lun_io->metatask = metatask;
+	lun_io->ctl_io = io;
+	lun_io->policy = policy;
+	lun_io->orig_lun_io = orig_lun_io;
+	lun_io->done_function = done_function;
+	/*
+	 * We only set the tag number for SCSI I/Os.  For task management
+	 * commands, the tag number is only really needed for aborts, so
+	 * the caller can set it if necessary.
+	 */
+	switch (io->io_hdr.io_type) {
+	case CTL_IO_SCSI:
+		io->scsiio.tag_num = lun->cur_tag_num++;
+		break;
+	case CTL_IO_TASK:
+	default:
+		break;
+	}
+}
+
+static void
+cfi_done(union ctl_io *io)
+{
+	struct cfi_lun_io *lun_io;
+	struct cfi_softc *softc;
+	struct cfi_lun *lun;
+
+	lun_io = (struct cfi_lun_io *)
+		io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
+
+	lun = lun_io->lun;
+	softc = lun->softc;
+
+	/*
+	 * Very minimal retry logic.  We basically retry if we got an error
+	 * back, and the retry count is greater than 0.  If we ever want
+	 * more sophisticated initiator type behavior, the CAM error
+	 * recovery code in ../common might be helpful.
+	 */
+	if (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
+	 && (io->io_hdr.retries > 0)) {
+		ctl_io_status old_status;
+		cfi_error_action error_action;
+
+		error_action = cfi_error_parse(io, lun_io);
+
+		switch (error_action & CFI_ERR_MASK) {
+		case CFI_ERR_FAIL:
+			goto done;
+			break; /* NOTREACHED */
+		case CFI_ERR_LUN_RESET: {
+			union ctl_io *new_io;
+			struct cfi_lun_io *new_lun_io;
+
+			new_io = ctl_alloc_io(softc->fe.ctl_pool_ref);
+			if (new_io == NULL) {
+				printf("%s: unable to allocate ctl_io for "
+				       "error recovery\n", __func__);
+				goto done;
+			}
+			ctl_zero_io(new_io);
+
+			new_io->io_hdr.io_type = CTL_IO_TASK;
+			new_io->taskio.task_action = CTL_TASK_LUN_RESET;
+
+			cfi_init_io(new_io,
+				    /*lun*/ lun_io->lun,
+				    /*metatask*/ NULL,
+				    /*policy*/ CFI_ERR_SOFT,
+				    /*retries*/ 0,
+				    /*orig_lun_io*/lun_io,
+				    /*done_function*/ cfi_err_recovery_done);
+			
+
+			new_lun_io = (struct cfi_lun_io *)
+				new_io->io_hdr.port_priv;
+
+			mtx_lock(&lun->softc->lock);
+			STAILQ_INSERT_TAIL(&lun->io_list, new_lun_io, links);
+			mtx_unlock(&lun->softc->lock);
+
+			io = new_io;
+			break;
+		}
+		case CFI_ERR_RETRY:
+		default:
+			if ((error_action & CFI_ERR_NO_DECREMENT) == 0)
+				io->io_hdr.retries--;
+			break;
+		}
+
+		old_status = io->io_hdr.status;
+		io->io_hdr.status = CTL_STATUS_NONE;
+#if 0
+		io->io_hdr.flags &= ~CTL_FLAG_ALREADY_DONE;
+#endif
+		io->io_hdr.flags &= ~CTL_FLAG_ABORT;
+		io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
+
+		if (ctl_queue(io) != CTL_RETVAL_COMPLETE) {
+			printf("%s: error returned from ctl_queue()!\n",
+			       __func__);
+			io->io_hdr.status = old_status;
+		} else
+			return;
+	}
+done:
+	lun_io->done_function(io);
+}
+
+static void
+cfi_lun_probe_done(union ctl_io *io)
+{
+	struct cfi_lun *lun;
+	struct cfi_lun_io *lun_io;
+
+	lun_io = (struct cfi_lun_io *)
+		io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
+	lun = lun_io->lun;
+
+	switch (lun->state) {
+	case CFI_LUN_INQUIRY: {
+		if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
+			/* print out something here?? */
+			printf("%s: LUN %d probe failed because inquiry "
+			       "failed\n", __func__, lun->lun_id);
+			ctl_io_error_print(io, NULL);
+		} else {
+
+			if (SID_TYPE(&lun->inq_data) != T_DIRECT) {
+				char path_str[40];
+
+				lun->state = CFI_LUN_READY;
+				ctl_scsi_path_string(io, path_str,
+						     sizeof(path_str));
+				printf("%s", path_str);
+				scsi_print_inquiry(&lun->inq_data);
+			} else {
+				lun->state = CFI_LUN_READCAPACITY;
+				cfi_lun_probe(lun, /*have_lock*/ 0);
+			}
+		}
+		mtx_lock(&lun->softc->lock);
+		STAILQ_REMOVE(&lun->io_list, lun_io, cfi_lun_io, links);
+		mtx_unlock(&lun->softc->lock);
+		ctl_free_io(io);
+		break;
+	}
+	case CFI_LUN_READCAPACITY:
+	case CFI_LUN_READCAPACITY_16: {
+		uint64_t maxlba;
+		uint32_t blocksize;
+
+		maxlba = 0;
+		blocksize = 0;
+
+		if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
+			printf("%s: LUN %d probe failed because READ CAPACITY "
+			       "failed\n", __func__, lun->lun_id);
+			ctl_io_error_print(io, NULL);
+		} else {
+
+			if (lun->state == CFI_LUN_READCAPACITY) {
+				struct scsi_read_capacity_data *rdcap;
+
+				rdcap = (struct scsi_read_capacity_data *)
+					io->scsiio.ext_data_ptr;
+
+				maxlba = scsi_4btoul(rdcap->addr);
+				blocksize = scsi_4btoul(rdcap->length);
+				if (blocksize == 0) {
+					printf("%s: LUN %d has invalid "
+					       "blocksize 0, probe aborted\n",
+					       __func__, lun->lun_id);
+				} else if (maxlba == 0xffffffff) {
+					lun->state = CFI_LUN_READCAPACITY_16;
+					cfi_lun_probe(lun, /*have_lock*/ 0);
+				} else
+					lun->state = CFI_LUN_READY;
+			} else {
+				struct scsi_read_capacity_data_long *rdcap_long;
+
+				rdcap_long = (struct
+					scsi_read_capacity_data_long *)
+					io->scsiio.ext_data_ptr;
+				maxlba = scsi_8btou64(rdcap_long->addr);
+				blocksize = scsi_4btoul(rdcap_long->length);
+
+				if (blocksize == 0) {
+					printf("%s: LUN %d has invalid "
+					       "blocksize 0, probe aborted\n",
+					       __func__, lun->lun_id);
+				} else
+					lun->state = CFI_LUN_READY;
+			}
+		}
+
+		if (lun->state == CFI_LUN_READY) {
+			char path_str[40];
+
+			lun->num_blocks = maxlba + 1;
+			lun->blocksize = blocksize;
+
+			/*
+			 * If this is true, the blocksize is a power of 2.
+			 * We already checked for 0 above.
+			 */
+			if (((blocksize - 1) & blocksize) == 0) {
+				int i;
+
+				for (i = 0; i < 32; i++) {
+					if ((blocksize & (1 << i)) != 0) {
+						lun->blocksize_powerof2 = i;
+						break;
+					}
+				}
+			}
+			ctl_scsi_path_string(io, path_str,sizeof(path_str));
+			printf("%s", path_str);
+			scsi_print_inquiry(&lun->inq_data);
+			printf("%s %ju blocks, blocksize %d\n", path_str,
+			       (uintmax_t)maxlba + 1, blocksize);
+		}
+		mtx_lock(&lun->softc->lock);
+		STAILQ_REMOVE(&lun->io_list, lun_io, cfi_lun_io, links);
+		mtx_unlock(&lun->softc->lock);
+		free(io->scsiio.ext_data_ptr, M_CTL_CFI);
+		ctl_free_io(io);
+		break;
+	}
+	case CFI_LUN_READY:
+	default:
+		mtx_lock(&lun->softc->lock);
+		/* How did we get here?? */
+		STAILQ_REMOVE(&lun->io_list, lun_io, cfi_lun_io, links);
+		mtx_unlock(&lun->softc->lock);
+		ctl_free_io(io);
+		break;
+	}
+}
+
+static void
+cfi_lun_probe(struct cfi_lun *lun, int have_lock)
+{
+
+	if (have_lock == 0)
+		mtx_lock(&lun->softc->lock);
+	if ((lun->softc->flags & CFI_ONLINE) == 0) {
+		if (have_lock == 0)
+			mtx_unlock(&lun->softc->lock);
+		return;
+	}
+	if (have_lock == 0)
+		mtx_unlock(&lun->softc->lock);
+
+	switch (lun->state) {
+	case CFI_LUN_INQUIRY: {
+		struct cfi_lun_io *lun_io;
+		union ctl_io *io;
+
+		io = ctl_alloc_io(lun->softc->fe.ctl_pool_ref);
+		if (io == NULL) {
+			printf("%s: unable to alloc ctl_io for target %ju "
+			       "lun %d probe\n", __func__,
+			       (uintmax_t)lun->target_id.id, lun->lun_id);
+			return;
+		}
+		ctl_scsi_inquiry(io,
+				 /*data_ptr*/(uint8_t *)&lun->inq_data,
+				 /*data_len*/ sizeof(lun->inq_data),
+				 /*byte2*/ 0,
+				 /*page_code*/ 0,
+				 /*tag_type*/ CTL_TAG_SIMPLE,
+				 /*control*/ 0);
+
+		cfi_init_io(io,
+			    /*lun*/ lun,
+			    /*metatask*/ NULL,
+			    /*policy*/ CFI_ERR_SOFT,
+			    /*retries*/ 5,
+			    /*orig_lun_io*/ NULL,
+			    /*done_function*/
+			    cfi_lun_probe_done);
+
+		lun_io = (struct cfi_lun_io *)io->io_hdr.port_priv;
+
+		if (have_lock == 0)
+			mtx_lock(&lun->softc->lock);
+		STAILQ_INSERT_TAIL(&lun->io_list, lun_io, links);
+		if (have_lock == 0)
+			mtx_unlock(&lun->softc->lock);
+
+		if (ctl_queue(io) != CTL_RETVAL_COMPLETE) {
+			printf("%s: error returned from ctl_queue()!\n",
+			       __func__);
+			STAILQ_REMOVE(&lun->io_list, lun_io,
+				      cfi_lun_io, links);
+			ctl_free_io(io);
+		}
+		break;
+	}
+	case CFI_LUN_READCAPACITY:
+	case CFI_LUN_READCAPACITY_16: {
+		struct cfi_lun_io *lun_io;
+		uint8_t *dataptr;
+		union ctl_io *io;
+
+		io = ctl_alloc_io(lun->softc->fe.ctl_pool_ref);
+		if (io == NULL) {
+			printf("%s: unable to alloc ctl_io for target %ju "
+			       "lun %d probe\n", __func__,
+			       (uintmax_t)lun->target_id.id, lun->lun_id);
+			return;
+		}
+
+		dataptr = malloc(sizeof(struct scsi_read_capacity_data_long),
+				 M_CTL_CFI, M_NOWAIT);
+		if (dataptr == NULL) {
+			printf("%s: unable to allocate SCSI read capacity "
+			       "buffer for target %ju lun %d\n", __func__,
+			       (uintmax_t)lun->target_id.id, lun->lun_id);
+			return;
+		}
+		if (lun->state == CFI_LUN_READCAPACITY) {
+			ctl_scsi_read_capacity(io,
+				/*data_ptr*/ dataptr,
+				/*data_len*/
+				sizeof(struct scsi_read_capacity_data_long),
+				/*addr*/ 0,
+				/*reladr*/ 0,
+				/*pmi*/ 0,
+				/*tag_type*/ CTL_TAG_SIMPLE,
+				/*control*/ 0);
+		} else {
+			ctl_scsi_read_capacity_16(io,
+				/*data_ptr*/ dataptr,
+				/*data_len*/
+				sizeof(struct scsi_read_capacity_data_long),
+				/*addr*/ 0,
+				/*reladr*/ 0,
+				/*pmi*/ 0,
+				/*tag_type*/ CTL_TAG_SIMPLE,
+				/*control*/ 0);
+		}
+		cfi_init_io(io,
+			    /*lun*/ lun,
+			    /*metatask*/ NULL,
+			    /*policy*/ CFI_ERR_SOFT,
+			    /*retries*/ 7,
+			    /*orig_lun_io*/ NULL,
+			    /*done_function*/ cfi_lun_probe_done);
+
+		lun_io = (struct cfi_lun_io *)io->io_hdr.port_priv;
+
+		if (have_lock == 0)
+			mtx_lock(&lun->softc->lock);
+		STAILQ_INSERT_TAIL(&lun->io_list, lun_io, links);
+		if (have_lock == 0)
+			mtx_unlock(&lun->softc->lock);
+
+		if (ctl_queue(io) != CTL_RETVAL_COMPLETE) {
+			printf("%s: error returned from ctl_queue()!\n",
+			       __func__);
+			STAILQ_REMOVE(&lun->io_list, lun_io,
+				      cfi_lun_io, links);
+			free(dataptr, M_CTL_CFI);
+			ctl_free_io(io);
+		}
+		break;
+	}
+	case CFI_LUN_READY:
+	default:
+		/* Why were we called? */
+		break;
+	}
+}
+
+static void
+cfi_metatask_done(struct cfi_softc *softc, struct cfi_metatask *metatask)
+{
+	mtx_lock(&softc->lock);
+	STAILQ_REMOVE(&softc->metatask_list, metatask, cfi_metatask, links);
+	mtx_unlock(&softc->lock);
+
+	/*
+	 * Return status to the caller.  Caller allocated storage, and is
+	 * responsible for calling cfi_free_metatask to release it once
+	 * they've seen the status.
+	 */
+	metatask->callback(metatask->callback_arg, metatask);
+}
+
+static void
+cfi_metatask_bbr_errorparse(struct cfi_metatask *metatask, union ctl_io *io)
+{
+	int error_code, sense_key, asc, ascq;
+
+	if (metatask->tasktype != CFI_TASK_BBRREAD)
+		return;
+
+	if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
+		metatask->status = CFI_MT_SUCCESS;
+		metatask->taskinfo.bbrread.status = CFI_BBR_SUCCESS;
+		return;
+	}
+
+	if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SCSI_ERROR) {
+		metatask->status = CFI_MT_ERROR;
+		metatask->taskinfo.bbrread.status = CFI_BBR_ERROR;
+		return;
+	}
+
+	metatask->taskinfo.bbrread.scsi_status = io->scsiio.scsi_status;
+	memcpy(&metatask->taskinfo.bbrread.sense_data, &io->scsiio.sense_data,
+	       ctl_min(sizeof(metatask->taskinfo.bbrread.sense_data),
+		       sizeof(io->scsiio.sense_data)));
+
+	if (io->scsiio.scsi_status == SCSI_STATUS_RESERV_CONFLICT) {
+		metatask->status = CFI_MT_ERROR;
+		metatask->taskinfo.bbrread.status = CFI_BBR_RESERV_CONFLICT;
+		return;
+	}
+
+	if (io->scsiio.scsi_status != SCSI_STATUS_CHECK_COND) {
+		metatask->status = CFI_MT_ERROR;
+		metatask->taskinfo.bbrread.status = CFI_BBR_SCSI_ERROR;
+		return;
+	}
+
+	scsi_extract_sense_len(&io->scsiio.sense_data,
+			       io->scsiio.sense_len,
+			       &error_code,
+			       &sense_key,
+			       &asc,
+			       &ascq,
+			       /*show_errors*/ 1);
+
+	switch (error_code) {
+	case SSD_DEFERRED_ERROR:
+	case SSD_DESC_DEFERRED_ERROR:
+		metatask->status = CFI_MT_ERROR;
+		metatask->taskinfo.bbrread.status = CFI_BBR_SCSI_ERROR;
+		break;
+	case SSD_CURRENT_ERROR:
+	case SSD_DESC_CURRENT_ERROR:
+	default: {
+		struct scsi_sense_data *sense;
+
+		sense = &io->scsiio.sense_data;
+
+		if ((asc == 0x04) && (ascq == 0x02)) {
+			metatask->status = CFI_MT_ERROR;
+			metatask->taskinfo.bbrread.status = CFI_BBR_LUN_STOPPED;
+		} else if ((asc == 0x04) && (ascq == 0x03)) {
+			metatask->status = CFI_MT_ERROR;
+			metatask->taskinfo.bbrread.status =
+				CFI_BBR_LUN_OFFLINE_CTL;
+		} else if ((asc == 0x44) && (ascq == 0x00)) {
+#ifdef NEEDTOPORT
+			if (sense->sense_key_spec[0] & SSD_SCS_VALID) {
+				uint16_t retry_count;
+
+				retry_count = sense->sense_key_spec[1] << 8 |
+					      sense->sense_key_spec[2];
+				if (((retry_count & 0xf000) == CSC_RAIDCORE)
+				 && ((retry_count & 0x0f00) == CSC_SHELF_SW)
+				 && ((retry_count & 0xff) ==
+				      RC_STS_DEVICE_OFFLINE)) {
+					metatask->status = CFI_MT_ERROR;
+					metatask->taskinfo.bbrread.status =
+						CFI_BBR_LUN_OFFLINE_RC;
+				} else {
+					metatask->status = CFI_MT_ERROR;
+					metatask->taskinfo.bbrread.status =
+						CFI_BBR_SCSI_ERROR;
+				}
+			} else {
+#endif /* NEEDTOPORT */
+				metatask->status = CFI_MT_ERROR;
+				metatask->taskinfo.bbrread.status =
+					CFI_BBR_SCSI_ERROR;
+#ifdef NEEDTOPORT
+			}
+#endif
+		} else {
+			metatask->status = CFI_MT_ERROR;
+			metatask->taskinfo.bbrread.status = CFI_BBR_SCSI_ERROR;
+		}
+		break;
+	}
+	}
+}
+
+static void
+cfi_metatask_io_done(union ctl_io *io)
+{
+	struct cfi_lun_io *lun_io;
+	struct cfi_metatask *metatask;
+	struct cfi_softc *softc;
+	struct cfi_lun *lun;
+
+	lun_io = (struct cfi_lun_io *)
+		io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
+
+	lun = lun_io->lun;
+	softc = lun->softc;
+
+	metatask = lun_io->metatask;
+
+	switch (metatask->tasktype) {
+	case CFI_TASK_STARTUP:
+	case CFI_TASK_SHUTDOWN: {
+		int failed, done, is_start;
+
+		failed = 0;
+		done = 0;
+		if (metatask->tasktype == CFI_TASK_STARTUP)
+			is_start = 1;
+		else
+			is_start = 0;
+
+		mtx_lock(&softc->lock);
+		if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)
+			metatask->taskinfo.startstop.luns_complete++;
+		else {
+			metatask->taskinfo.startstop.luns_failed++;
+			failed = 1;
+		}
+		if ((metatask->taskinfo.startstop.luns_complete +
+		     metatask->taskinfo.startstop.luns_failed) >=
+		     metatask->taskinfo.startstop.total_luns)
+			done = 1;
+
+		mtx_unlock(&softc->lock);
+
+		if (failed != 0) {
+			printf("%s: LUN %d %s request failed\n", __func__,
+			       lun_io->lun->lun_id, (is_start == 1) ? "start" :
+			       "stop");
+			ctl_io_error_print(io, &lun_io->lun->inq_data);
+		}
+		if (done != 0) {
+			if (metatask->taskinfo.startstop.luns_failed > 0)
+				metatask->status = CFI_MT_ERROR;
+			else
+				metatask->status = CFI_MT_SUCCESS;
+			cfi_metatask_done(softc, metatask);
+		}
+		mtx_lock(&softc->lock);
+		STAILQ_REMOVE(&lun->io_list, lun_io, cfi_lun_io, links);
+		mtx_unlock(&softc->lock);
+
+		ctl_free_io(io);
+		break;
+	}
+	case CFI_TASK_BBRREAD: {
+		/*
+		 * Translate the SCSI error into an enumeration.
+		 */
+		cfi_metatask_bbr_errorparse(metatask, io);
+
+		mtx_lock(&softc->lock);
+		STAILQ_REMOVE(&lun->io_list, lun_io, cfi_lun_io, links);
+		mtx_unlock(&softc->lock);
+
+		ctl_free_io(io);
+
+		cfi_metatask_done(softc, metatask);
+		break;
+	}
+	default:
+		/*
+		 * This shouldn't happen.
+		 */
+		mtx_lock(&softc->lock);
+		STAILQ_REMOVE(&lun->io_list, lun_io, cfi_lun_io, links);
+		mtx_unlock(&softc->lock);
+
+		ctl_free_io(io);
+		break;
+	}
+}
+
+static void
+cfi_err_recovery_done(union ctl_io *io)
+{
+	struct cfi_lun_io *lun_io, *orig_lun_io;
+	struct cfi_lun *lun;
+	union ctl_io *orig_io;
+
+	lun_io = (struct cfi_lun_io *)io->io_hdr.port_priv;
+	orig_lun_io = lun_io->orig_lun_io;
+	orig_io = orig_lun_io->ctl_io;
+	lun = lun_io->lun;
+
+	if (io->io_hdr.status != CTL_SUCCESS) {
+		printf("%s: error recovery action failed.  Original "
+		       "error:\n", __func__);
+
+		ctl_io_error_print(orig_lun_io->ctl_io, &lun->inq_data);
+
+		printf("%s: error from error recovery action:\n", __func__);
+
+		ctl_io_error_print(io, &lun->inq_data);
+
+		printf("%s: trying original command again...\n", __func__);
+	}
+
+	mtx_lock(&lun->softc->lock);
+	STAILQ_REMOVE(&lun->io_list, lun_io, cfi_lun_io, links);
+	mtx_unlock(&lun->softc->lock);
+	ctl_free_io(io);
+
+	orig_io->io_hdr.retries--;
+	orig_io->io_hdr.status = CTL_STATUS_NONE;
+
+	if (ctl_queue(orig_io) != CTL_RETVAL_COMPLETE) {
+		printf("%s: error returned from ctl_queue()!\n", __func__);
+		STAILQ_REMOVE(&lun->io_list, orig_lun_io,
+			      cfi_lun_io, links);
+		ctl_free_io(orig_io);
+	}
+}
+
+static void
+cfi_lun_io_done(union ctl_io *io)
+{
+	struct cfi_lun *lun;
+	struct cfi_lun_io *lun_io;
+
+	lun_io = (struct cfi_lun_io *)
+		io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
+	lun = lun_io->lun;
+
+	if (lun_io->metatask == NULL) {
+		printf("%s: I/O has no metatask pointer, discarding\n",
+		       __func__);
+		STAILQ_REMOVE(&lun->io_list, lun_io, cfi_lun_io, links);
+		ctl_free_io(io);
+		return;
+	}
+	cfi_metatask_io_done(io);
+}
+
+void
+cfi_action(struct cfi_metatask *metatask)
+{
+	struct cfi_softc *softc;
+
+	softc = &fetd_internal_softc;
+
+	mtx_lock(&softc->lock);
+
+	STAILQ_INSERT_TAIL(&softc->metatask_list, metatask, links);
+
+	if ((softc->flags & CFI_ONLINE) == 0) {
+		mtx_unlock(&softc->lock);
+		metatask->status = CFI_MT_PORT_OFFLINE;
+		cfi_metatask_done(softc, metatask);
+		return;
+	} else
+		mtx_unlock(&softc->lock);
+
+	switch (metatask->tasktype) {
+	case CFI_TASK_STARTUP:
+	case CFI_TASK_SHUTDOWN: {
+		union ctl_io *io;
+		int da_luns, ios_allocated, do_start;
+		struct cfi_lun *lun;
+		STAILQ_HEAD(, ctl_io_hdr) tmp_io_list;
+
+		da_luns = 0;
+		ios_allocated = 0;
+		STAILQ_INIT(&tmp_io_list);
+
+		if (metatask->tasktype == CFI_TASK_STARTUP)
+			do_start = 1;
+		else
+			do_start = 0;
+
+		mtx_lock(&softc->lock);
+		STAILQ_FOREACH(lun, &softc->lun_list, links) {
+			if (lun->state != CFI_LUN_READY)
+				continue;
+
+			if (SID_TYPE(&lun->inq_data) != T_DIRECT)
+				continue;
+			da_luns++;
+			io = ctl_alloc_io(softc->fe.ctl_pool_ref);
+			if (io != NULL) {
+				ios_allocated++;
+				STAILQ_INSERT_TAIL(&tmp_io_list, &io->io_hdr,
+						   links);
+			}
+		}
+
+		if (ios_allocated < da_luns) {
+			printf("%s: error allocating ctl_io for %s\n",
+			       __func__, (do_start == 1) ? "startup" :
+			       "shutdown");
+			da_luns = ios_allocated;
+		}
+
+		metatask->taskinfo.startstop.total_luns = da_luns;
+
+		STAILQ_FOREACH(lun, &softc->lun_list, links) {
+			struct cfi_lun_io *lun_io;
+
+			if (lun->state != CFI_LUN_READY)
+				continue;
+
+			if (SID_TYPE(&lun->inq_data) != T_DIRECT)
+				continue;
+
+			io = (union ctl_io *)STAILQ_FIRST(&tmp_io_list);
+			if (io == NULL)
+				break;
+
+			STAILQ_REMOVE(&tmp_io_list, &io->io_hdr, ctl_io_hdr,
+				      links);
+
+			ctl_scsi_start_stop(io,
+					    /*start*/ do_start,
+					    /*load_eject*/ 0,
+					    /*immediate*/ 0,
+					    /*power_conditions*/
+					    SSS_PC_START_VALID,
+					    /*onoffline*/ 1,
+					    /*ctl_tag_type*/ CTL_TAG_ORDERED,
+					    /*control*/ 0);
+
+			cfi_init_io(io,
+				    /*lun*/ lun,
+				    /*metatask*/ metatask,
+				    /*policy*/ CFI_ERR_HARD,
+				    /*retries*/ 3,
+				    /*orig_lun_io*/ NULL,
+				    /*done_function*/ cfi_lun_io_done);
+
+			lun_io = (struct cfi_lun_io *) io->io_hdr.port_priv;
+
+			STAILQ_INSERT_TAIL(&lun->io_list, lun_io, links);
+
+			if (ctl_queue(io) != CTL_RETVAL_COMPLETE) {
+				printf("%s: error returned from ctl_queue()!\n",
+				       __func__);
+				STAILQ_REMOVE(&lun->io_list, lun_io,
+					      cfi_lun_io, links);
+				ctl_free_io(io);
+				metatask->taskinfo.startstop.total_luns--;
+			}
+		}
+
+		if (STAILQ_FIRST(&tmp_io_list) != NULL) {
+			printf("%s: error: tmp_io_list != NULL\n", __func__);
+			for (io = (union ctl_io *)STAILQ_FIRST(&tmp_io_list);
+			     io != NULL;
+			     io = (union ctl_io *)STAILQ_FIRST(&tmp_io_list)) {
+				STAILQ_REMOVE(&tmp_io_list, &io->io_hdr,
+					      ctl_io_hdr, links);
+				ctl_free_io(io);
+			}
+		}
+		mtx_unlock(&softc->lock);
+
+		break;
+	}
+	case CFI_TASK_BBRREAD: {
+		union ctl_io *io;
+		struct cfi_lun *lun;
+		struct cfi_lun_io *lun_io;
+		cfi_bbrread_status status;
+		int req_lun_num;
+		uint32_t num_blocks;
+
+		status = CFI_BBR_SUCCESS;
+
+		req_lun_num = metatask->taskinfo.bbrread.lun_num;
+
+		mtx_lock(&softc->lock);
+		STAILQ_FOREACH(lun, &softc->lun_list, links) {
+			if (lun->lun_id != req_lun_num)
+				continue;
+			if (lun->state != CFI_LUN_READY) {
+				status = CFI_BBR_LUN_UNCONFIG;
+				break;
+			} else
+				break;
+		}
+
+		if (lun == NULL)
+			status = CFI_BBR_NO_LUN;
+
+		if (status != CFI_BBR_SUCCESS) {
+			metatask->status = CFI_MT_ERROR;
+			metatask->taskinfo.bbrread.status = status;
+			mtx_unlock(&softc->lock);
+			cfi_metatask_done(softc, metatask);
+			break;
+		}
+
+		/*
+		 * Convert the number of bytes given into blocks and check
+		 * that the number of bytes is a multiple of the blocksize.
+		 * CTL will verify that the LBA is okay.
+		 */
+		if (lun->blocksize_powerof2 != 0) {
+			if ((metatask->taskinfo.bbrread.len &
+			    (lun->blocksize - 1)) != 0) {
+				metatask->status = CFI_MT_ERROR;
+				metatask->taskinfo.bbrread.status =
+					CFI_BBR_BAD_LEN;
+				cfi_metatask_done(softc, metatask);
+				break;
+			}
+
+			num_blocks = metatask->taskinfo.bbrread.len >>
+				lun->blocksize_powerof2;
+		} else {
+			/*
+			 * XXX KDM this could result in floating point
+			 * division, which isn't supported in the kernel on
+			 * x86 at least.
+			 */
+			if ((metatask->taskinfo.bbrread.len %
+			     lun->blocksize) != 0) {
+				metatask->status = CFI_MT_ERROR;
+				metatask->taskinfo.bbrread.status =
+					CFI_BBR_BAD_LEN;
+				cfi_metatask_done(softc, metatask);
+				break;
+			}
+
+			/*
+			 * XXX KDM this could result in floating point
+			 * division in some cases.
+			 */
+			num_blocks = metatask->taskinfo.bbrread.len /
+				lun->blocksize;
+
+		}
+
+		io = ctl_alloc_io(softc->fe.ctl_pool_ref);
+		if (io == NULL) {
+			metatask->status = CFI_MT_ERROR;
+			metatask->taskinfo.bbrread.status = CFI_BBR_NO_MEM;
+			mtx_unlock(&softc->lock);
+			cfi_metatask_done(softc, metatask);
+			break;
+		}
+
+		/*
+		 * XXX KDM need to do a read capacity to get the blocksize
+		 * for this device.
+		 */
+		ctl_scsi_read_write(io,
+				    /*data_ptr*/ NULL,
+				    /*data_len*/ metatask->taskinfo.bbrread.len,
+				    /*read_op*/ 1,
+				    /*byte2*/ 0,
+				    /*minimum_cdb_size*/ 0,
+				    /*lba*/ metatask->taskinfo.bbrread.lba,
+				    /*num_blocks*/ num_blocks,
+				    /*tag_type*/ CTL_TAG_SIMPLE,
+				    /*control*/ 0);
+
+		cfi_init_io(io,
+			    /*lun*/ lun,
+			    /*metatask*/ metatask,
+			    /*policy*/ CFI_ERR_SOFT,
+			    /*retries*/ 3,
+			    /*orig_lun_io*/ NULL,
+			    /*done_function*/ cfi_lun_io_done);
+
+		lun_io = (struct cfi_lun_io *)io->io_hdr.port_priv;
+
+		STAILQ_INSERT_TAIL(&lun->io_list, lun_io, links);
+
+		if (ctl_queue(io) != CTL_RETVAL_COMPLETE) {
+			printf("%s: error returned from ctl_queue()!\n",
+			       __func__);
+			STAILQ_REMOVE(&lun->io_list, lun_io, cfi_lun_io, links);
+			ctl_free_io(io);
+			metatask->status = CFI_MT_ERROR;
+			metatask->taskinfo.bbrread.status = CFI_BBR_ERROR;
+			mtx_unlock(&softc->lock);
+			cfi_metatask_done(softc, metatask);
+			break;
+		}
+
+		mtx_unlock(&softc->lock);
+		break;
+	}
+	default:
+		panic("invalid metatask type %d", metatask->tasktype);
+		break; /* NOTREACHED */
+	}
+}
+
+#ifdef oldapi
+void
+cfi_shutdown_shelf(cfi_cb_t callback, void *callback_arg)
+{
+	struct ctl_mem_element *element;
+	struct cfi_softc *softc;
+	struct cfi_metatask *metatask;
+
+	softc = &fetd_internal_softc;
+
+	element = ctl_alloc_mem_element(&softc->metatask_pool, /*can_wait*/ 0);
+	if (element == NULL) {
+		callback(callback_arg,
+			 /*status*/ CFI_MT_ERROR,
+			 /*sluns_found*/ 0,
+			 /*sluns_complete*/ 0,
+			 /*sluns_failed*/ 0);
+		return;
+	}
+
+	metatask = (struct cfi_metatask *)element->bytes;
+
+	memset(metatask, 0, sizeof(*metatask));
+	metatask->tasktype = CFI_TASK_SHUTDOWN;
+	metatask->status = CFI_MT_NONE;
+	metatask->taskinfo.startstop.callback = callback;
+	metatask->taskinfo.startstop.callback_arg = callback_arg;
+	metatask->element = element;
+
+	cfi_action(softc, metatask);
+
+	/*
+	 * - send a report luns to lun 0, get LUN list.
+	 * - send an inquiry to each lun
+	 * - send a stop/offline to each direct access LUN
+	 *    - if we get a reservation conflict, reset the LUN and then
+	 *      retry sending the stop/offline
+	 * - return status back to the caller
+	 */
+}
+
+void
+cfi_start_shelf(cfi_cb_t callback, void *callback_arg)
+{
+	struct ctl_mem_element *element;
+	struct cfi_softc *softc;
+	struct cfi_metatask *metatask;
+
+	softc = &fetd_internal_softc;
+
+	element = ctl_alloc_mem_element(&softc->metatask_pool, /*can_wait*/ 0);
+	if (element == NULL) {
+		callback(callback_arg,
+			 /*status*/ CFI_MT_ERROR,
+			 /*sluns_found*/ 0,
+			 /*sluns_complete*/ 0,
+			 /*sluns_failed*/ 0);
+		return;
+	}
+
+	metatask = (struct cfi_metatask *)element->bytes;
+
+	memset(metatask, 0, sizeof(*metatask));
+	metatask->tasktype = CFI_TASK_STARTUP;
+	metatask->status = CFI_MT_NONE;
+	metatask->taskinfo.startstop.callback = callback;
+	metatask->taskinfo.startstop.callback_arg = callback_arg;
+	metatask->element = element;
+
+	cfi_action(softc, metatask);
+
+	/*
+	 * - send a report luns to lun 0, get LUN list.
+	 * - send an inquiry to each lun
+	 * - send a stop/offline to each direct access LUN
+	 *    - if we get a reservation conflict, reset the LUN and then
+	 *      retry sending the stop/offline
+	 * - return status back to the caller
+	 */
+}
+
+#endif
+
+struct cfi_metatask *
+cfi_alloc_metatask(int can_wait)
+{
+	struct ctl_mem_element *element;
+	struct cfi_metatask *metatask;
+	struct cfi_softc *softc;
+
+	softc = &fetd_internal_softc;
+
+	element = ctl_alloc_mem_element(&softc->metatask_pool, can_wait);
+	if (element == NULL)
+		return (NULL);
+
+	metatask = (struct cfi_metatask *)element->bytes;
+	memset(metatask, 0, sizeof(*metatask));
+	metatask->status = CFI_MT_NONE;
+	metatask->element = element;
+
+	return (metatask);
+}
+
+void
+cfi_free_metatask(struct cfi_metatask *metatask)
+{
+	ctl_free_mem_element(metatask->element);
+}
+
+/*
+ * vim: ts=8
+ */
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/cam/ctl/ctl_frontend_internal.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/sys/cam/ctl/ctl_frontend_internal.h	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,154 @@
+/*-
+ * Copyright (c) 2004 Silicon Graphics International Corp.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions, and the following disclaimer,
+ *    without modification.
+ * 2. Redistributions in binary form must reproduce at minimum a disclaimer
+ *    substantially similar to the "NO WARRANTY" disclaimer below
+ *    ("Disclaimer") and any redistribution must be conditioned upon
+ *    including a substantially similar Disclaimer requirement for further
+ *    binary redistribution.
+ *
+ * NO WARRANTY
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGES.
+ *
+ * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_frontend_internal.h#1 $
+ * $FreeBSD: head/sys/cam/ctl/ctl_frontend_internal.h 229997 2012-01-12 00:34:33Z ken $
+ */
+/*
+ * CTL kernel internal frontend target driver.  This allows kernel-level
+ * clients to send commands into CTL.
+ *
+ * Author: Ken Merry <ken at FreeBSD.org>
+ */
+
+#ifndef	_CTL_FRONTEND_INTERNAL_H_
+#define	_CTL_FRONTEND_INTERNAL_H_
+
+/*
+ * These are general metatask error codes.  If the error code is CFI_MT_ERROR, 
+ * check any metatask-specific status codes for more detail on the problem.
+ */
+typedef enum {
+	CFI_MT_NONE,
+	CFI_MT_PORT_OFFLINE,
+	CFI_MT_ERROR,
+	CFI_MT_SUCCESS
+} cfi_mt_status;
+
+typedef enum {
+	CFI_TASK_NONE,
+	CFI_TASK_SHUTDOWN,
+	CFI_TASK_STARTUP,
+	CFI_TASK_BBRREAD
+} cfi_tasktype;
+
+struct cfi_task_startstop {
+	int total_luns;
+	int luns_complete;
+	int luns_failed;
+};
+
+/*
+ * Error code description:
+ * CFI_BBR_SUCCESS          - the read was successful
+ * CFI_BBR_LUN_UNCONFIG     - CFI probe for this lun hasn't completed
+ * CFI_BBR_NO_LUN           - this lun doesn't exist, as far as CFI knows
+ * CFI_BBR_NO_MEM           - memory allocation error
+ * CFI_BBR_BAD_LEN          - data length isn't a multiple of the blocksize
+ * CFI_BBR_RESERV_CONFLICT  - another initiator has this lun reserved, so
+ *                            we can't issue I/O at all.
+ * CFI_BBR_LUN_STOPPED      - the lun is powered off.
+ * CFI_BBR_LUN_OFFLINE_CTL  - the lun is offline from a CTL standpoint
+ * CFI_BBR_LUN_OFFLINE_RC   - the lun is offline from a RAIDCore standpoint.
+ *                            This is bad, because it basically means we've
+ *                            had a double failure on the LUN.
+ * CFI_BBR_SCSI_ERROR       - generic SCSI error, see status byte and sense
+ *                            data for more resolution if you want it.
+ * CFI_BBR_ERROR            - the catch-all error code.
+ */
+typedef enum {
+	CFI_BBR_SUCCESS,
+	CFI_BBR_LUN_UNCONFIG,
+	CFI_BBR_NO_LUN,
+	CFI_BBR_NO_MEM,
+	CFI_BBR_BAD_LEN,
+	CFI_BBR_RESERV_CONFLICT,
+	CFI_BBR_LUN_STOPPED,
+	CFI_BBR_LUN_OFFLINE_CTL,
+	CFI_BBR_LUN_OFFLINE_RC,
+	CFI_BBR_SCSI_ERROR,
+	CFI_BBR_ERROR,
+} cfi_bbrread_status;
+
+struct cfi_task_bbrread {
+	int			lun_num;      /* lun number */
+	uint64_t		lba;          /* logical block address */
+	int			len;          /* length in bytes */
+	cfi_bbrread_status	status;       /* BBR status */
+	uint8_t			scsi_status;  /* SCSI status */
+	struct scsi_sense_data	sense_data;   /* SCSI sense data */
+};
+
+union cfi_taskinfo {
+	struct cfi_task_startstop startstop;
+	struct cfi_task_bbrread bbrread;
+};
+
+struct cfi_metatask;
+
+typedef void (*cfi_cb_t)(void *arg, struct cfi_metatask *metatask);
+
+struct cfi_metatask {
+	cfi_tasktype		tasktype;	/* passed to CFI */
+	cfi_mt_status		status;		/* returned from CFI */
+	union cfi_taskinfo	taskinfo;	/* returned from CFI */
+	struct ctl_mem_element	*element;	/* used by CFI, don't touch*/
+	cfi_cb_t		callback;	/* passed to CFI */
+	void			*callback_arg;	/* passed to CFI */
+	STAILQ_ENTRY(cfi_metatask) links;	/* used by CFI, don't touch*/
+};
+
+#ifdef _KERNEL
+
+MALLOC_DECLARE(M_CTL_CFI);
+
+/*
+ * This is the API for sending meta commands (commands that are sent to more
+ * than one LUN) to the internal frontend:
+ *  - Allocate a metatask using cfi_alloc_metatask().  can_wait == 0 means
+ *    that you're calling from an interrupt context.  can_wait == 1 means
+ *    that you're calling from a thread context and don't mind waiting to
+ *    allocate memory.
+ *  - Setup the task type, callback and callback argument.
+ *  - Call cfi_action().
+ *  - When the callback comes, note the status and any per-command status
+ *    (see the taskinfo union) and then free the metatask with
+ *    cfi_free_metatask().
+ */
+struct cfi_metatask *cfi_alloc_metatask(int can_wait);
+void cfi_free_metatask(struct cfi_metatask *metatask);
+void cfi_action(struct cfi_metatask *metatask);
+
+#endif /* _KERNEL */
+
+#endif	/* _CTL_FRONTEND_INTERNAL_H_ */
+
+/*
+ * vim: ts=8
+ */
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/cam/ctl/ctl_ha.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/sys/cam/ctl/ctl_ha.h	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,270 @@
+/*-
+ * Copyright (c) 2003-2009 Silicon Graphics International Corp.
+ * Copyright (c) 2011 Spectra Logic Corporation
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions, and the following disclaimer,
+ *    without modification.
+ * 2. Redistributions in binary form must reproduce at minimum a disclaimer
+ *    substantially similar to the "NO WARRANTY" disclaimer below
+ *    ("Disclaimer") and any redistribution must be conditioned upon
+ *    including a substantially similar Disclaimer requirement for further
+ *    binary redistribution.
+ *
+ * NO WARRANTY
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGES.
+ *
+ * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_ha.h#1 $
+ * $FreeBSD: head/sys/cam/ctl/ctl_ha.h 229997 2012-01-12 00:34:33Z ken $
+ */
+
+#ifndef _CTL_HA_H_
+#define	_CTL_HA_H_
+
+/*
+ * CTL High Availability Modes:
+ *
+ * CTL_HA_MODE_SER_ONLY:  Commands are serialized to the other side.  Write
+ *			  mirroring and read re-direction are assumed to
+ * 			  happen in the back end.
+ * CTL_HA_MODE_XFER:	  Commands are serialized and data is transferred
+ *			  for write mirroring and read re-direction.
+ */
+
+typedef enum {
+	CTL_HA_MODE_SER_ONLY,
+	CTL_HA_MODE_XFER
+} ctl_ha_mode;
+
+
+/*
+ * This is a stubbed out High Availability interface.  It assumes two nodes
+ * staying in sync.
+ *
+ * The reason this interface is here, and stubbed out, is that CTL was
+ * originally written with support for Copan's (now SGI) high availability
+ * framework.  That framework was not released by SGI, and would not have
+ * been generally applicable to FreeBSD anyway.
+ *
+ * The idea here is to show the kind of API that would need to be in place
+ * in a HA framework to work with CTL's HA hooks.  This API is very close
+ * to the Copan/SGI API, so that the code using it could stay in place
+ * as-is.
+ *
+ * So, in summary, this is a shell without real substance, and much more
+ * work would be needed to actually make HA work.  The implementation
+ * inside CTL will also need to change to fit the eventual implementation.
+ * The additional pieces we would need are:
+ *
+ *  - HA "Supervisor" framework that can startup the components of the
+ *    system, and initiate failover (i.e. active/active to single mode)
+ *    and failback (single to active/active mode) state transitions.
+ *    This framework would be able to recognize when an event happens
+ *    that requires it to initiate state transitions in the components it
+ *    manages.
+ *
+ *  - HA communication framework.  This framework should have the following
+ *    features:
+ *	- Separate channels for separate system components.  The CTL
+ *	  instance on one node should communicate with the CTL instance
+ *	  on another node.
+ *	- Short message passing.  These messages would be fixed length, so
+ *	  they could be preallocated and easily passed between the nodes.
+ *	  i.e. conceptually like an ethernet packet.
+ *	- DMA/large buffer capability.  This would require some negotiation
+ *	  with the other node to define the destination.  It could
+ *	  allow for "push" (i.e. initiated by the requesting node) DMA or
+ * 	  "pull" (i.e. initiated by the target controller) DMA or both.
+ *	- Communication channel status change notification.
+ *  - HA capability in other portions of the storage stack.  Having two CTL
+ *    instances communicate is just one part of an overall HA solution.
+ *    State needs to be synchronized at multiple levels of the system in
+ *    order for failover to actually work.  For instance, if CTL is using a
+ *    file on a ZFS filesystem as its backing store, the ZFS array state
+ *    should be synchronized with the other node, so that the other node
+ *    can immediately take over if the node that is primary for a particular
+ *    array fails.
+ */
+
+/*
+ * Communication channel IDs for various system components.  This is to
+ * make sure one CTL instance talks with another, one ZFS instance talks
+ * with another, etc.
+ */
+typedef enum {
+	CTL_HA_CHAN_NONE,
+	CTL_HA_CHAN_CTL,
+	CTL_HA_CHAN_ZFS,
+	CTL_HA_CHAN_MAX
+} ctl_ha_channel;
+
+/*
+ * HA communication event notification.  These are events generated by the
+ * HA communication subsystem.
+ *
+ * CTL_HA_EVT_MSG_RECV:		Message received by the other node.
+ * CTL_HA_EVT_MSG_SENT:		Message sent to the other node.
+ * CTL_HA_EVT_DISCONNECT:	Communication channel disconnected.
+ * CTL_HA_EVT_DMA_SENT:		DMA successfully sent to other node (push).
+ * CTL_HA_EVT_DMA_RECEIVED:	DMA successfully received by other node (pull).
+ */
+typedef enum {
+	CTL_HA_EVT_NONE,
+	CTL_HA_EVT_MSG_RECV,
+	CTL_HA_EVT_MSG_SENT,
+	CTL_HA_EVT_DISCONNECT,
+	CTL_HA_EVT_DMA_SENT,
+	CTL_HA_EVT_DMA_RECEIVED,
+	CTL_HA_EVT_MAX
+} ctl_ha_event;
+
+typedef enum {
+	CTL_HA_STATUS_WAIT,
+	CTL_HA_STATUS_SUCCESS,
+	CTL_HA_STATUS_ERROR,
+	CTL_HA_STATUS_INVALID,
+	CTL_HA_STATUS_DISCONNECT,
+	CTL_HA_STATUS_BUSY,
+	CTL_HA_STATUS_MAX
+} ctl_ha_status;
+
+typedef enum {
+	CTL_HA_DATA_CTL,
+	CTL_HA_DATA_ZFS,
+	CTL_HA_DATA_MAX
+} ctl_ha_dtid;
+
+typedef enum {
+	CTL_HA_DT_CMD_READ,
+	CTL_HA_DT_CMD_WRITE,
+} ctl_ha_dt_cmd;
+
+struct ctl_ha_dt_req;
+
+typedef void (*ctl_ha_dt_cb)(struct ctl_ha_dt_req *);
+
+struct ctl_ha_dt_req {
+	ctl_ha_dt_cmd	command;
+	void		*context;
+	ctl_ha_dt_cb	callback;
+	ctl_ha_dtid	id;
+	int		ret;
+	uint32_t	size;
+	uint8_t		*local;
+	uint8_t		*remote;
+};
+
+typedef void (*ctl_evt_handler)(ctl_ha_channel channel, ctl_ha_event event,
+				int param);
+void ctl_ha_register_evthandler(ctl_ha_channel channel,
+				ctl_evt_handler handler);
+
+static inline ctl_ha_status
+ctl_ha_msg_create(ctl_ha_channel channel, ctl_evt_handler handler)
+{
+	return (CTL_HA_STATUS_SUCCESS);
+}
+
+/*
+ * Receive a message of the specified size.
+ */
+static inline ctl_ha_status
+ctl_ha_msg_recv(ctl_ha_channel channel, void *buffer, unsigned int size,
+		int wait)
+{
+	return (CTL_HA_STATUS_SUCCESS);
+}
+
+/*
+ * Send a message of the specified size.
+ */
+static inline ctl_ha_status
+ctl_ha_msg_send(ctl_ha_channel channel, void *buffer, unsigned int size,
+		int wait)
+{
+	return (CTL_HA_STATUS_SUCCESS);
+}
+
+/*
+ * Allocate a data transfer request structure.
+ */
+static inline struct ctl_ha_dt_req *
+ctl_dt_req_alloc(void)
+{
+	return (NULL);
+}
+
+/*
+ * Free a data transfer request structure.
+ */
+static inline void
+ctl_dt_req_free(struct ctl_ha_dt_req *req)
+{
+	return;
+}
+
+/*
+ * Issue a DMA request for a single buffer.
+ */
+static inline ctl_ha_status
+ctl_dt_single(struct ctl_ha_dt_req *req)
+{
+	return (CTL_HA_STATUS_WAIT);
+}
+
+/*
+ * SINGLE:	   One node
+ * HA:		   Two nodes (Active/Active implied)
+ * SLAVE/MASTER:   The component can set these flags to indicate which side
+ *		   is in control.  It has no effect on the HA framework.
+ */
+typedef enum {
+	CTL_HA_STATE_UNKNOWN	= 0x00,
+	CTL_HA_STATE_SINGLE	= 0x01,
+	CTL_HA_STATE_HA		= 0x02,
+	CTL_HA_STATE_MASK	= 0x0F,
+	CTL_HA_STATE_SLAVE	= 0x10,
+	CTL_HA_STATE_MASTER	= 0x20
+} ctl_ha_state;
+
+typedef enum {
+	CTL_HA_COMP_STATUS_OK,
+	CTL_HA_COMP_STATUS_FAILED,
+	CTL_HA_COMP_STATUS_ERROR
+} ctl_ha_comp_status;
+
+struct ctl_ha_component;
+
+typedef ctl_ha_comp_status (*ctl_hacmp_init_t)(struct ctl_ha_component *);
+typedef ctl_ha_comp_status (*ctl_hacmp_start_t)(struct ctl_ha_component *,
+						ctl_ha_state);
+
+struct ctl_ha_component {
+	char			*name;
+	ctl_ha_state 		state;
+	ctl_ha_comp_status	status;
+	ctl_hacmp_init_t	init;
+	ctl_hacmp_start_t	start;
+	ctl_hacmp_init_t	quiesce;
+};
+
+#define	CTL_HA_STATE_IS_SINGLE(state)	((state & CTL_HA_STATE_MASK) == \
+					  CTL_HA_STATE_SINGLE)
+#define	CTL_HA_STATE_IS_HA(state)	((state & CTL_HA_STATE_MASK) == \
+					  CTL_HA_STATE_HA)
+
+#endif /* _CTL_HA_H_ */
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/cam/ctl/ctl_io.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/sys/cam/ctl/ctl_io.h	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,474 @@
+/*-
+ * Copyright (c) 2003 Silicon Graphics International Corp.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions, and the following disclaimer,
+ *    without modification.
+ * 2. Redistributions in binary form must reproduce at minimum a disclaimer
+ *    substantially similar to the "NO WARRANTY" disclaimer below
+ *    ("Disclaimer") and any redistribution must be conditioned upon
+ *    including a substantially similar Disclaimer requirement for further
+ *    binary redistribution.
+ *
+ * NO WARRANTY
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGES.
+ *
+ * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_io.h#5 $
+ * $FreeBSD: head/sys/cam/ctl/ctl_io.h 229997 2012-01-12 00:34:33Z ken $
+ */
+/*
+ * CAM Target Layer data movement structures/interface.
+ *
+ * Author: Ken Merry <ken at FreeBSD.org>
+ */
+
+#ifndef	_CTL_IO_H_
+#define	_CTL_IO_H_
+
+#ifdef _CTL_C
+#define EXTERN(__var,__val) __var = __val
+#else
+#define EXTERN(__var,__val) extern __var
+#endif
+
+#define	CTL_MAX_CDBLEN	32
+/*
+ * Uncomment this next line to enable printing out times for I/Os
+ * that take longer than CTL_TIME_IO_SECS seconds to get to the datamove
+ * and/or done stage.
+ */
+#define	CTL_TIME_IO
+#ifdef  CTL_TIME_IO
+#define	CTL_TIME_IO_DEFAULT_SECS	90
+EXTERN(int ctl_time_io_secs, CTL_TIME_IO_DEFAULT_SECS);
+#endif
+
+/*
+ * Uncomment these next two lines to enable the CTL I/O delay feature.  You
+ * can delay I/O at two different points -- datamove and done.  This is
+ * useful for diagnosing abort conditions (for hosts that send an abort on a
+ * timeout), and for determining how long a host's timeout is.
+ */
+#define	CTL_IO_DELAY
+#define	CTL_TIMER_BYTES		sizeof(struct callout)
+
+typedef enum {
+	CTL_STATUS_NONE,	/* No status */
+	CTL_SUCCESS,		/* Transaction completed successfully */
+	CTL_CMD_TIMEOUT,	/* Command timed out, shouldn't happen here */
+	CTL_SEL_TIMEOUT,	/* Selection timeout, shouldn't happen here */
+	CTL_ERROR,		/* General CTL error XXX expand on this? */
+	CTL_SCSI_ERROR,		/* SCSI error, look at status byte/sense data */
+	CTL_CMD_ABORTED,	/* Command aborted, don't return status */
+	CTL_STATUS_MASK = 0xfff,/* Mask off any status flags */
+	CTL_AUTOSENSE = 0x1000	/* Autosense performed */
+} ctl_io_status;
+
+/*
+ * WARNING:  Keep the data in/out/none flags where they are.  They're used
+ * in conjuction with ctl_cmd_flags.  See comment above ctl_cmd_flags
+ * definition in ctl_private.h.
+ */
+typedef enum {
+	CTL_FLAG_NONE		= 0x00000000,	/* no flags */
+	CTL_FLAG_DATA_IN	= 0x00000001,	/* DATA IN */
+	CTL_FLAG_DATA_OUT	= 0x00000002,	/* DATA OUT */
+	CTL_FLAG_DATA_NONE	= 0x00000003,	/* no data */
+	CTL_FLAG_DATA_MASK	= 0x00000003,
+	CTL_FLAG_KDPTR_SGLIST	= 0x00000008, 	/* kern_data_ptr is S/G list*/
+	CTL_FLAG_EDPTR_SGLIST	= 0x00000010,	/* ext_data_ptr is S/G list */
+	CTL_FLAG_DO_AUTOSENSE	= 0x00000020,	/* grab sense info */
+	CTL_FLAG_USER_REQ	= 0x00000040,	/* request came from userland */
+	CTL_FLAG_CONTROL_DEV	= 0x00000080,	/* processor device */
+	CTL_FLAG_ALLOCATED	= 0x00000100,	/* data space allocated */
+	CTL_FLAG_BLOCKED	= 0x00000200,	/* on the blocked queue */
+	CTL_FLAG_ABORT		= 0x00000800,	/* this I/O should be aborted */
+	CTL_FLAG_DMA_INPROG	= 0x00001000,	/* DMA in progress */
+	CTL_FLAG_NO_DATASYNC	= 0x00002000,	/* don't cache flush data */
+	CTL_FLAG_DELAY_DONE	= 0x00004000,	/* delay injection done */
+	CTL_FLAG_INT_COPY	= 0x00008000,	/* internal copy, no done call*/
+	CTL_FLAG_SENT_2OTHER_SC	= 0x00010000,
+	CTL_FLAG_FROM_OTHER_SC	= 0x00020000,
+	CTL_FLAG_IS_WAS_ON_RTR  = 0x00040000,	/* Don't rerun cmd on failover*/
+	CTL_FLAG_BUS_ADDR	= 0x00080000,	/* ctl_sglist contains BUS
+						   addresses, not virtual ones*/
+	CTL_FLAG_IO_CONT	= 0x00100000,	/* Continue I/O instead of
+						   completing */
+	CTL_FLAG_AUTO_MIRROR	= 0x00200000,	/* Automatically use memory
+						   from the RC cache mirrored
+						   address area. */
+#if 0
+	CTL_FLAG_ALREADY_DONE	= 0x00200000	/* I/O already completed */
+#endif
+	CTL_FLAG_NO_DATAMOVE	= 0x00400000,
+	CTL_FLAG_DMA_QUEUED	= 0x00800000,	/* DMA queued but not started*/
+	CTL_FLAG_STATUS_QUEUED	= 0x01000000,	/* Status queued but not sent*/
+
+	CTL_FLAG_REDIR_DONE	= 0x02000000,	/* Redirection has already
+						   been done. */
+	CTL_FLAG_FAILOVER	= 0x04000000,	/* Killed by a failover */
+	CTL_FLAG_IO_ACTIVE	= 0x08000000,	/* I/O active on this SC */
+	CTL_FLAG_RDMA_MASK	= CTL_FLAG_NO_DATASYNC | CTL_FLAG_BUS_ADDR |
+				  CTL_FLAG_AUTO_MIRROR | CTL_FLAG_REDIR_DONE
+						/* Flags we care about for
+						   remote DMA */
+} ctl_io_flags;
+
+
+struct ctl_lba_len {
+	uint64_t lba;
+	uint32_t len;
+};
+
+union ctl_priv {
+	uint8_t		bytes[sizeof(uint64_t) * 2];
+	uint64_t	integer;
+	void		*ptr;
+};
+
+/*
+ * Number of CTL private areas.
+ */
+#define	CTL_NUM_PRIV	6
+
+/*
+ * Which private area are we using for a particular piece of data?
+ */
+#define	CTL_PRIV_LUN		0	/* CTL LUN pointer goes here */
+#define	CTL_PRIV_LBA_LEN	1	/* Decoded LBA/len for read/write*/
+#define	CTL_PRIV_MODEPAGE	1	/* Modepage info for config write */
+#define	CTL_PRIV_BACKEND	2	/* Reserved for block, RAIDCore */
+#define	CTL_PRIV_BACKEND_LUN	3	/* Backend LUN pointer */
+#define	CTL_PRIV_FRONTEND	4	/* LSI driver, ioctl front end */
+#define	CTL_PRIV_USER		5	/* Userland use */
+
+#define CTL_INVALID_PORTNAME 0xFF
+#define CTL_UNMAPPED_IID     0xFF
+/*
+ * XXX KDM this size is for the port_priv variable in struct ctl_io_hdr
+ * below.  This should be defined in terms of the size of struct
+ * ctlfe_lun_cmd_info at the moment:
+ * struct ctlfe_lun_cmd_info {
+ *	int cur_transfer_index;
+ * 	ctlfe_cmd_flags flags;
+ * 	bus_dma_segment_t cam_sglist[32];
+ * };
+ *
+ * This isn't really the way I'd prefer to do it, but it does make some
+ * sense, AS LONG AS we can guarantee that there will always only be one
+ * outstanding DMA request per ctl_io.  If that assumption isn't valid,
+ * then we've got problems.
+ *
+ * At some point it may be nice switch CTL over to using CCBs for
+ * everything.  At that point we can probably use the ATIO/CTIO model, so
+ * that multiple simultaneous DMAs per command will just work.
+ *
+ * Also note that the current size, 600, is appropriate for 64-bit
+ * architectures, but is overkill for 32-bit architectures.  Need a way to
+ * figure out the size at compile time, or just get rid of this altogether.
+ */
+#define	CTL_PORT_PRIV_SIZE	600
+
+struct ctl_sg_entry {
+	void	*addr;
+	size_t	len;
+};
+
+struct ctl_id {
+	uint32_t		id;
+	uint64_t		wwid[2];
+};
+
+typedef enum {
+	CTL_IO_NONE,
+	CTL_IO_SCSI,
+	CTL_IO_TASK,
+} ctl_io_type;
+
+struct ctl_nexus {
+	struct ctl_id initid;		/* Initiator ID */
+	uint32_t targ_port;		/* Target port, filled in by PORT */
+	struct ctl_id targ_target;	/* Destination target */
+	uint32_t targ_lun;		/* Destination lun */
+};
+
+typedef enum {
+	CTL_MSG_SERIALIZE,
+	CTL_MSG_R2R,
+	CTL_MSG_FINISH_IO,
+	CTL_MSG_BAD_JUJU,
+	CTL_MSG_MANAGE_TASKS,
+	CTL_MSG_PERS_ACTION,
+	CTL_MSG_SYNC_FE,
+	CTL_MSG_APS_LOCK,
+	CTL_MSG_DATAMOVE,
+	CTL_MSG_DATAMOVE_DONE
+} ctl_msg_type;
+
+struct ctl_scsiio;
+
+#define	CTL_NUM_SG_ENTRIES	9
+
+struct ctl_io_hdr {
+	uint32_t	  version;	/* interface version XXX */
+	ctl_io_type	  io_type;	/* task I/O, SCSI I/O, etc. */
+	ctl_msg_type	  msg_type;
+	struct ctl_nexus  nexus;	/* Initiator, port, target, lun */
+	uint32_t	  iid_indx;	/* the index into the iid mapping */
+	uint32_t	  flags;	/* transaction flags */
+	uint32_t	  status;	/* transaction status */
+	uint32_t	  port_status;	/* trans status, set by PORT, 0 = good*/
+	uint32_t	  timeout;	/* timeout in ms */
+	uint32_t	  retries;	/* retry count */
+#ifdef CTL_IO_DELAY
+	uint8_t		  timer_bytes[CTL_TIMER_BYTES]; /* timer kludge */
+#endif /* CTL_IO_DELAY */
+#ifdef CTL_TIME_IO
+	time_t		  start_time;	/* I/O start time */
+	struct bintime	  start_bt;	/* Timer start ticks */
+	struct bintime	  dma_start_bt;	/* DMA start ticks */
+	struct bintime	  dma_bt;	/* DMA total ticks */
+	uint32_t	  num_dmas;	/* Number of DMAs */
+#endif /* CTL_TIME_IO */
+	union ctl_io	  *original_sc;
+	union ctl_io	  *serializing_sc;
+	void		  *pool;	/* I/O pool */
+	union ctl_priv	  ctl_private[CTL_NUM_PRIV];/* CTL private area */
+	uint8_t		  port_priv[CTL_PORT_PRIV_SIZE];/* PORT private area*/
+	struct ctl_sg_entry remote_sglist[CTL_NUM_SG_ENTRIES];
+	struct ctl_sg_entry remote_dma_sglist[CTL_NUM_SG_ENTRIES];
+	struct ctl_sg_entry local_sglist[CTL_NUM_SG_ENTRIES];
+	struct ctl_sg_entry local_dma_sglist[CTL_NUM_SG_ENTRIES];
+	STAILQ_ENTRY(ctl_io_hdr) links;	/* linked list pointer */
+	TAILQ_ENTRY(ctl_io_hdr) ooa_links;
+	TAILQ_ENTRY(ctl_io_hdr) blocked_links;
+};
+
+typedef enum {
+	CTL_TAG_UNTAGGED,
+	CTL_TAG_SIMPLE,
+	CTL_TAG_ORDERED,
+	CTL_TAG_HEAD_OF_QUEUE,
+	CTL_TAG_ACA
+} ctl_tag_type;
+
+union ctl_io;
+
+/*
+ * SCSI passthrough I/O structure for the CAM Target Layer.  Note
+ * that some of these fields are here for completeness, but they aren't
+ * used in the CTL implementation.  e.g., timeout and retries won't be
+ * used.
+ *
+ * Note:  Make sure the io_hdr is *always* the first element in this
+ * structure.
+ */
+struct ctl_scsiio {
+	struct ctl_io_hdr io_hdr;	/* common to all I/O types */
+	uint32_t   ext_sg_entries;	/* 0 = no S/G list, > 0 = num entries */
+	uint8_t	   *ext_data_ptr;	/* data buffer or S/G list */
+	uint32_t   ext_data_len;	/* Data transfer length */
+	uint32_t   ext_data_filled;	/* Amount of data filled so far */
+	uint32_t   kern_sg_entries;	/* 0 = no S/G list, > 0 = num entries */
+	uint32_t   rem_sg_entries;	/* 0 = no S/G list, > 0 = num entries */
+	uint8_t    *kern_data_ptr;	/* data buffer or S/G list */
+	uint32_t   kern_data_len;	/* Length of this S/G list/buffer */
+	uint32_t   kern_total_len;	/* Total length of this transaction */
+	uint32_t   kern_data_resid;	/* Length left to transfer after this*/
+	uint32_t   kern_rel_offset;	/* Byte Offset of this transfer */
+	struct     scsi_sense_data sense_data;	/* sense data */
+	uint8_t	   sense_len;		/* Returned sense length */
+	uint8_t	   scsi_status;		/* SCSI status byte */
+	uint8_t	   sense_residual;	/* sense residual length */
+	uint32_t   residual;		/* data residual length */
+	uint32_t   tag_num;		/* tag number */
+	ctl_tag_type tag_type;		/* simple, ordered, head of queue,etc.*/
+	uint8_t    cdb_len;		/* CDB length */
+	uint8_t	   cdb[CTL_MAX_CDBLEN];	/* CDB */
+	int	   (*be_move_done)(union ctl_io *io); /* called by fe */
+	int        (*io_cont)(union ctl_io *io); /* to continue processing */
+};
+
+typedef enum {
+	CTL_TASK_ABORT_TASK,
+	CTL_TASK_ABORT_TASK_SET,
+	CTL_TASK_CLEAR_ACA,
+	CTL_TASK_CLEAR_TASK_SET,
+	CTL_TASK_LUN_RESET,
+	CTL_TASK_TARGET_RESET,
+	CTL_TASK_BUS_RESET,
+	CTL_TASK_PORT_LOGIN,
+	CTL_TASK_PORT_LOGOUT
+} ctl_task_type;
+
+/*
+ * Task management I/O structure.  Aborts, bus resets, etc., are sent using
+ * this structure.
+ *
+ * Note:  Make sure the io_hdr is *always* the first element in this
+ * structure.
+ */
+struct ctl_taskio {
+	struct ctl_io_hdr	io_hdr;      /* common to all I/O types */
+	ctl_task_type		task_action; /* Target Reset, Abort, etc.  */
+	uint32_t		tag_num;     /* tag number */
+	ctl_tag_type		tag_type;    /* simple, ordered, etc. */
+};
+
+typedef enum {
+	CTL_PR_REG_KEY,
+	CTL_PR_UNREG_KEY,
+	CTL_PR_PREEMPT,
+	CTL_PR_CLEAR,
+	CTL_PR_RESERVE,
+	CTL_PR_RELEASE
+} ctl_pr_action;
+
+/*
+ * The PR info is specifically for sending Persistent Reserve actions
+ * to the other SC which it must also act on.
+ *
+ * Note:  Make sure the io_hdr is *always* the first element in this
+ * structure.
+ */
+struct ctl_pr_info {
+	ctl_pr_action        action;
+	uint8_t              sa_res_key[8];
+	uint8_t              res_type;
+	uint16_t             residx;
+};
+
+struct ctl_ha_msg_hdr {
+	ctl_msg_type		msg_type;
+	union ctl_io		*original_sc;
+	union ctl_io		*serializing_sc;
+	struct ctl_nexus	nexus;	     /* Initiator, port, target, lun */
+	uint32_t		status;	     /* transaction status */
+	TAILQ_ENTRY(ctl_ha_msg_hdr) links;
+};
+
+#define	CTL_HA_MAX_SG_ENTRIES	16
+
+/*
+ * Used for CTL_MSG_APS_LOCK.
+ */
+struct ctl_ha_msg_aps {
+	struct ctl_ha_msg_hdr	hdr;
+	uint8_t			lock_flag;
+};
+
+/*
+ * Used for CTL_MSG_PERS_ACTION.
+ */
+struct ctl_ha_msg_pr {
+	struct ctl_ha_msg_hdr	hdr;
+	struct ctl_pr_info	pr_info;
+};
+
+/*
+ * The S/G handling here is a little different than the standard ctl_scsiio
+ * structure, because we can't pass data by reference in between controllers.
+ * The S/G list in the ctl_scsiio struct is normally passed in the
+ * kern_data_ptr field.  So kern_sg_entries here will always be non-zero,
+ * even if there is only one entry.
+ *
+ * Used for CTL_MSG_DATAMOVE.
+ */
+struct ctl_ha_msg_dt {
+	struct ctl_ha_msg_hdr	hdr;
+	ctl_io_flags		flags;  /* Only I/O flags are used here */
+	uint32_t		sg_sequence;     /* S/G portion number  */
+	uint8_t			sg_last;         /* last S/G batch = 1 */
+	uint32_t		sent_sg_entries; /* previous S/G count */
+	uint32_t		cur_sg_entries;  /* current S/G entries */
+	uint32_t		kern_sg_entries; /* total S/G entries */
+	uint32_t		kern_data_len;   /* Length of this S/G list */
+	uint32_t		kern_total_len;  /* Total length of this
+						    transaction */
+	uint32_t		kern_data_resid; /* Length left to transfer
+						    after this*/
+	uint32_t		kern_rel_offset; /* Byte Offset of this
+						    transfer */
+	struct ctl_sg_entry	sg_list[CTL_HA_MAX_SG_ENTRIES];
+};
+
+/*
+ * Used for CTL_MSG_SERIALIZE, CTL_MSG_FINISH_IO, CTL_MSG_BAD_JUJU.
+ */
+struct ctl_ha_msg_scsi {
+	struct ctl_ha_msg_hdr	hdr;
+	uint8_t			cdb[CTL_MAX_CDBLEN];	/* CDB */
+	uint32_t		tag_num;     /* tag number */
+	ctl_tag_type		tag_type;    /* simple, ordered, etc. */
+	uint8_t			scsi_status; /* SCSI status byte */
+	struct scsi_sense_data	sense_data;  /* sense data */
+	uint8_t			sense_len;   /* Returned sense length */
+	uint8_t			sense_residual;	/* sense residual length */
+	uint32_t		residual;    /* data residual length */
+	uint32_t		fetd_status; /* trans status, set by FETD,
+						0 = good*/
+	struct ctl_lba_len	lbalen;      /* used for stats */
+};
+
+/* 
+ * Used for CTL_MSG_MANAGE_TASKS.
+ */
+struct ctl_ha_msg_task {
+	struct ctl_ha_msg_hdr	hdr;
+	ctl_task_type		task_action; /* Target Reset, Abort, etc.  */
+	uint32_t		tag_num;     /* tag number */
+	ctl_tag_type		tag_type;    /* simple, ordered, etc. */
+};
+
+union ctl_ha_msg {
+	struct ctl_ha_msg_hdr	hdr;
+	struct ctl_ha_msg_task	task;
+	struct ctl_ha_msg_scsi	scsi;
+	struct ctl_ha_msg_dt	dt;
+	struct ctl_ha_msg_pr	pr;
+	struct ctl_ha_msg_aps	aps;
+};
+
+
+struct ctl_prio {
+	struct ctl_io_hdr  io_hdr;
+	struct ctl_ha_msg_pr pr_msg;
+};
+
+
+
+union ctl_io {
+	struct ctl_io_hdr io_hdr;	/* common to all I/O types */
+	struct ctl_scsiio scsiio;	/* Normal SCSI commands */
+	struct ctl_taskio taskio;	/* SCSI task management/reset */
+	struct ctl_prio   presio;	/* update per. res info on other SC */
+};
+
+#ifdef _KERNEL
+
+union ctl_io *ctl_alloc_io(void *pool_ref);
+void ctl_free_io(union ctl_io *io);
+void ctl_zero_io(union ctl_io *io);
+void ctl_copy_io(union ctl_io *src, union ctl_io *dest);
+
+#endif /* _KERNEL */
+
+#endif	/* _CTL_IO_H_ */
+
+/*
+ * vim: ts=8
+ */
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/cam/ctl/ctl_ioctl.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/sys/cam/ctl/ctl_ioctl.h	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,604 @@
+/*-
+ * Copyright (c) 2003 Silicon Graphics International Corp.
+ * Copyright (c) 2011 Spectra Logic Corporation
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions, and the following disclaimer,
+ *    without modification.
+ * 2. Redistributions in binary form must reproduce at minimum a disclaimer
+ *    substantially similar to the "NO WARRANTY" disclaimer below
+ *    ("Disclaimer") and any redistribution must be conditioned upon
+ *    including a substantially similar Disclaimer requirement for further
+ *    binary redistribution.
+ *
+ * NO WARRANTY
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGES.
+ *
+ * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_ioctl.h#4 $
+ * $FreeBSD: head/sys/cam/ctl/ctl_ioctl.h 229997 2012-01-12 00:34:33Z ken $
+ */
+/*
+ * CAM Target Layer ioctl interface.
+ *
+ * Author: Ken Merry <ken at FreeBSD.org>
+ */
+
+#ifndef	_CTL_IOCTL_H_
+#define	_CTL_IOCTL_H_
+
+#define	CTL_DEFAULT_DEV		"/dev/cam/ctl"
+/*
+ * Maximum number of targets we support.
+ */
+#define	CTL_MAX_TARGETS		1
+
+/*
+ * Maximum target ID we support.
+ */
+#define	CTL_MAX_TARGID		15
+
+/*
+ * Maximum number of LUNs we support at the moment.  MUST be a power of 2.
+ */
+#define	CTL_MAX_LUNS		256
+
+/*
+ * Maximum number of initiators per port.
+ */
+#define	CTL_MAX_INIT_PER_PORT	2048 // Was 16
+
+/*
+ * Maximum number of ports registered at one time.
+ */
+#define	CTL_MAX_PORTS		32
+
+/*
+ * Maximum number of initiators we support.
+ */
+#define	CTL_MAX_INITIATORS	(CTL_MAX_INIT_PER_PORT * CTL_MAX_PORTS)
+
+/* Hopefully this won't conflict with new misc devices that pop up */
+#define	CTL_MINOR	225
+
+typedef enum {
+	CTL_OOA_INVALID_LUN,
+	CTL_OOA_SUCCESS
+} ctl_ooa_status;
+
+struct ctl_ooa_info {
+	uint32_t target_id;	/* Passed in to CTL */
+	uint32_t lun_id;	/* Passed in to CTL */
+	uint32_t num_entries;	/* Returned from CTL */
+	ctl_ooa_status status;	/* Returned from CTL */
+};
+
+struct ctl_hard_startstop_info {
+	cfi_mt_status status;
+	int total_luns;
+	int luns_complete;
+	int luns_failed;
+};
+
+struct ctl_bbrread_info {
+	int			lun_num;	/* Passed in to CTL */
+	uint64_t		lba;		/* Passed in to CTL */
+	int			len;		/* Passed in to CTL */
+	cfi_mt_status		status;		/* Returned from CTL */
+	cfi_bbrread_status	bbr_status;	/* Returned from CTL */
+	uint8_t			scsi_status;	/* Returned from CTL */
+	struct scsi_sense_data	sense_data;	/* Returned from CTL */
+};
+
+typedef enum {
+	CTL_DELAY_TYPE_NONE,
+	CTL_DELAY_TYPE_CONT,
+	CTL_DELAY_TYPE_ONESHOT
+} ctl_delay_type;
+
+typedef enum {
+	CTL_DELAY_LOC_NONE,
+	CTL_DELAY_LOC_DATAMOVE,
+	CTL_DELAY_LOC_DONE,
+} ctl_delay_location;
+
+typedef enum {
+	CTL_DELAY_STATUS_NONE,
+	CTL_DELAY_STATUS_OK,
+	CTL_DELAY_STATUS_INVALID_LUN,
+	CTL_DELAY_STATUS_INVALID_TYPE,
+	CTL_DELAY_STATUS_INVALID_LOC,
+	CTL_DELAY_STATUS_NOT_IMPLEMENTED
+} ctl_delay_status;
+
+struct ctl_io_delay_info {
+	uint32_t		target_id;
+	uint32_t		lun_id;
+	ctl_delay_type		delay_type;
+	ctl_delay_location	delay_loc;
+	uint32_t		delay_secs;
+	ctl_delay_status	status;
+};
+
+typedef enum {
+	CTL_GS_SYNC_NONE,
+	CTL_GS_SYNC_OK,
+	CTL_GS_SYNC_NO_LUN
+} ctl_gs_sync_status;
+
+/*
+ * The target and LUN id specify which device to modify.  The sync interval
+ * means that we will let through every N SYNCHRONIZE CACHE commands.
+ */
+struct ctl_sync_info {
+	uint32_t		target_id;	/* passed to kernel */
+	uint32_t		lun_id;		/* passed to kernel */
+	int			sync_interval;	/* depends on whether get/set */
+	ctl_gs_sync_status	status;		/* passed from kernel */
+};
+
+typedef enum {
+	CTL_STATS_NO_IO,
+	CTL_STATS_READ,
+	CTL_STATS_WRITE
+} ctl_stat_types;
+#define	CTL_STATS_NUM_TYPES	3
+
+typedef enum {
+	CTL_LUN_STATS_NO_BLOCKSIZE	= 0x01
+} ctl_lun_stats_flags;
+
+struct ctl_lun_io_port_stats {
+	uint32_t			targ_port;
+	uint64_t			bytes[CTL_STATS_NUM_TYPES];
+	uint64_t			operations[CTL_STATS_NUM_TYPES];
+	struct bintime			time[CTL_STATS_NUM_TYPES];
+	uint64_t			num_dmas[CTL_STATS_NUM_TYPES];
+	struct bintime			dma_time[CTL_STATS_NUM_TYPES];
+};
+
+struct ctl_lun_io_stats {
+	uint8_t				device_type;
+	uint64_t			lun_number;
+	uint32_t			blocksize;
+	ctl_lun_stats_flags		flags;
+	struct ctl_lun_io_port_stats	ports[CTL_MAX_PORTS];
+};
+
+typedef enum {
+	CTL_SS_OK,
+	CTL_SS_NEED_MORE_SPACE,
+	CTL_SS_ERROR
+} ctl_stats_status;
+
+typedef enum {
+	CTL_STATS_FLAG_NONE		= 0x00,
+	CTL_STATS_FLAG_TIME_VALID	= 0x01
+} ctl_stats_flags;
+
+struct ctl_stats {
+	int			alloc_len;	/* passed to kernel */
+	struct ctl_lun_io_stats	*lun_stats;	/* passed to/from kernel */
+	int			fill_len;	/* passed to userland */
+	int			num_luns;	/* passed to userland */
+	ctl_stats_status	status;		/* passed to userland */
+	ctl_stats_flags		flags;		/* passed to userland */
+	struct timespec		timestamp;	/* passed to userland */
+};
+
+/*
+ * The types of errors that can be injected:
+ *
+ * NONE:	No error specified.
+ * ABORTED:	SSD_KEY_ABORTED_COMMAND, 0x45, 0x00
+ * MEDIUM_ERR:	Medium error, different asc/ascq depending on read/write.
+ * UA:		Unit attention.
+ * CUSTOM:	User specifies the sense data.
+ * TYPE:	Mask to use with error types.
+ *
+ * Flags that affect injection behavior:
+ * CONTINUOUS:	This error will stay around until explicitly cleared.
+ * DESCRIPTOR:	Use descriptor sense instead of fixed sense.
+ */
+typedef enum {
+	CTL_LUN_INJ_NONE		= 0x000,
+	CTL_LUN_INJ_ABORTED		= 0x001,
+	CTL_LUN_INJ_MEDIUM_ERR		= 0x002,
+	CTL_LUN_INJ_UA			= 0x003,
+	CTL_LUN_INJ_CUSTOM		= 0x004,
+	CTL_LUN_INJ_TYPE		= 0x0ff,
+	CTL_LUN_INJ_CONTINUOUS		= 0x100,
+	CTL_LUN_INJ_DESCRIPTOR		= 0x200
+} ctl_lun_error;
+
+/*
+ * Flags to specify what type of command the given error pattern will
+ * execute on.  The first group of types can be ORed together.
+ *
+ * READ:	Any read command.
+ * WRITE:	Any write command.
+ * READWRITE:	Any read or write command.
+ * READCAP:	Any read capacity command.
+ * TUR:		Test Unit Ready.
+ * ANY:		Any command.
+ * MASK:	Mask for basic command patterns.
+ *
+ * Special types:
+ *
+ * CMD:		The CDB to act on is specified in struct ctl_error_desc_cmd.
+ * RANGE:	For read/write commands, act when the LBA is in the
+ *		specified range.
+ */
+typedef enum {
+	CTL_LUN_PAT_NONE	= 0x000,
+	CTL_LUN_PAT_READ	= 0x001,
+	CTL_LUN_PAT_WRITE	= 0x002,
+	CTL_LUN_PAT_READWRITE	= CTL_LUN_PAT_READ | CTL_LUN_PAT_WRITE,
+	CTL_LUN_PAT_READCAP	= 0x004,
+	CTL_LUN_PAT_TUR		= 0x008,
+	CTL_LUN_PAT_ANY		= 0x0ff,
+	CTL_LUN_PAT_MASK	= 0x0ff,
+	CTL_LUN_PAT_CMD		= 0x100,
+	CTL_LUN_PAT_RANGE	= 0x200
+} ctl_lun_error_pattern;
+
+/*
+ * This structure allows the user to specify a particular CDB pattern to
+ * look for.
+ *
+ * cdb_pattern:		Fill in the relevant bytes to look for in the CDB.
+ * cdb_valid_bytes:	Bitmask specifying valid bytes in the cdb_pattern.
+ * flags:		Specify any command flags (see ctl_io_flags) that
+ *			should be set.
+ */
+struct ctl_error_desc_cmd {
+	uint8_t		cdb_pattern[CTL_MAX_CDBLEN];
+	uint32_t	cdb_valid_bytes;
+	uint32_t	flags;
+};
+
+/*
+ * Error injection descriptor.
+ *
+ * target_id:	   Target ID to act on.
+ * lun_id	   LUN to act on.
+ * lun_error:	   The type of error to inject.  See above for descriptions.
+ * error_pattern:  What kind of command to act on.  See above.
+ * cmd_desc:	   For CTL_LUN_PAT_CMD only.
+ * lba_range:	   For CTL_LUN_PAT_RANGE only.
+ * custom_sense:   Specify sense.  For CTL_LUN_INJ_CUSTOM only.
+ * serial:	   Serial number returned by the kernel.  Use for deletion.
+ * links:	   Kernel use only.
+ */
+struct ctl_error_desc {
+	uint32_t			target_id;	/* To kernel */
+	uint32_t			lun_id;		/* To kernel */
+	ctl_lun_error			lun_error;	/* To kernel */
+	ctl_lun_error_pattern		error_pattern;	/* To kernel */
+	struct ctl_error_desc_cmd	cmd_desc;	/* To kernel */
+	struct ctl_lba_len		lba_range;	/* To kernel */
+	struct scsi_sense_data		custom_sense;	/* To kernel */
+	uint64_t			serial;		/* From kernel */
+	STAILQ_ENTRY(ctl_error_desc)	links;		/* Kernel use only */
+};
+
+typedef enum {
+	CTL_OOA_FLAG_NONE	= 0x00,
+	CTL_OOA_FLAG_ALL_LUNS	= 0x01
+} ctl_ooa_flags;
+
+typedef enum {
+	CTL_OOA_OK,
+	CTL_OOA_NEED_MORE_SPACE,
+	CTL_OOA_ERROR
+} ctl_get_ooa_status;
+
+typedef enum {
+	CTL_OOACMD_FLAG_NONE		= 0x00,
+	CTL_OOACMD_FLAG_DMA		= 0x01,
+	CTL_OOACMD_FLAG_BLOCKED		= 0x02,
+	CTL_OOACMD_FLAG_ABORT		= 0x04,
+	CTL_OOACMD_FLAG_RTR		= 0x08,
+	CTL_OOACMD_FLAG_DMA_QUEUED	= 0x10
+} ctl_ooa_cmd_flags;
+
+struct ctl_ooa_entry {
+	ctl_ooa_cmd_flags	cmd_flags;
+	uint8_t			cdb[CTL_MAX_CDBLEN];
+	uint8_t			cdb_len;
+	uint32_t		tag_num;
+	uint32_t		lun_num;
+	struct bintime		start_bt;
+};
+
+struct ctl_ooa {
+	ctl_ooa_flags		flags;		/* passed to kernel */
+	uint64_t		lun_num;	/* passed to kernel */
+	uint32_t		alloc_len;	/* passed to kernel */
+	uint32_t		alloc_num;	/* passed to kernel */
+	struct ctl_ooa_entry	*entries;	/* filled in kernel */
+	uint32_t		fill_len;	/* passed to userland */
+	uint32_t		fill_num;	/* passed to userland */
+	uint32_t		dropped_num;	/* passed to userland */
+	struct bintime		cur_bt;		/* passed to userland */
+	ctl_get_ooa_status	status;		/* passed to userland */
+};
+
+typedef enum {
+	CTL_PORT_LIST_NONE,
+	CTL_PORT_LIST_OK,
+	CTL_PORT_LIST_NEED_MORE_SPACE,
+	CTL_PORT_LIST_ERROR
+} ctl_port_list_status;
+
+struct ctl_port_list {
+	uint32_t		alloc_len;	/* passed to kernel */
+	uint32_t		alloc_num;	/* passed to kernel */
+	struct ctl_port_entry   *entries;	/* filled in kernel */
+	uint32_t		fill_len;	/* passed to userland */
+	uint32_t		fill_num;	/* passed to userland */
+	uint32_t		dropped_num;	/* passed to userland */
+	ctl_port_list_status	status;		/* passed to userland */
+};
+
+typedef enum {
+	CTL_LUN_NOSTATUS,
+	CTL_LUN_OK,
+	CTL_LUN_ERROR
+} ctl_lun_status;
+
+#define	CTL_ERROR_STR_LEN	160
+
+#define	CTL_BEARG_RD		0x01
+#define	CTL_BEARG_WR		0x02
+#define	CTL_BEARG_RW		(CTL_BEARG_RD|CTL_BEARG_WR)
+#define	CTL_BEARG_ASCII		0x04
+
+/*
+ * Backend Argument:
+ *
+ * namelen:	Length of the name field, including the terminating NUL.
+ *
+ * name:	Name of the paramter.  This must be NUL-terminated.
+ *
+ * flags:	Flags for the parameter, see above for values.
+ *
+ * vallen:	Length of the value in bytes.
+ *
+ * value:	Value to be set/fetched.
+ *
+ * kname:	For kernel use only.
+ *
+ * kvalue:	For kernel use only.
+ */
+struct ctl_be_arg {
+	int	namelen;
+	char	*name;
+	int	flags;
+	int	vallen;
+	void	*value;
+
+	char	*kname;
+	void	*kvalue;
+};
+
+typedef enum {
+	CTL_LUNREQ_CREATE,
+	CTL_LUNREQ_RM
+} ctl_lunreq_type;
+
+
+/*
+ * LUN creation parameters:
+ *
+ * flags:		Various LUN flags, see ctl_backend.h for a
+ *			description of the flag values and meanings.
+ *
+ * device_type:		The SCSI device type.  e.g. 0 for Direct Access,
+ *			3 for Processor, etc.  Only certain backends may
+ *			support setting this field.  The CTL_LUN_FLAG_DEV_TYPE
+ *			flag should be set in the flags field if the device
+ *			type is set.
+ *
+ * lun_size_bytes:	The size of the LUN in bytes.  For some backends
+ *			this is relevant (e.g. ramdisk), for others, it may
+ *			be ignored in favor of using the properties of the
+ *			backing store.  If specified, this should be a
+ *			multiple of the blocksize.
+ *
+ *			The actual size of the LUN is returned in this
+ *			field.
+ *
+ * blocksize_bytes:	The LUN blocksize in bytes.  For some backends this
+ *			is relevant, for others it may be ignored in
+ *			favor of using the properties of the backing store. 
+ *
+ *			The actual blocksize of the LUN is returned in this
+ *			field.
+ *
+ * req_lun_id:		The requested LUN ID.  The CTL_LUN_FLAG_ID_REQ flag
+ *			should be set if this is set.  The request will be
+ *			granted if the LUN number is available, otherwise
+ * 			the LUN addition request will fail.
+ *
+ *			The allocated LUN number is returned in this field.
+ *
+ * serial_num:		This is the value returned in SCSI INQUIRY VPD page
+ *			0x80.  If it is specified, the CTL_LUN_FLAG_SERIAL_NUM
+ *			flag should be set.
+ *
+ *			The serial number value used is returned in this
+ *			field.
+ *
+ * device_id:		This is the value returned in the T10 vendor ID
+ *			based DESIGNATOR field in the SCSI INQUIRY VPD page
+ *			0x83 data.  If it is specified, the CTL_LUN_FLAG_DEVID
+ *			flag should be set.
+ *
+ *			The device id value used is returned in this field.
+ */
+struct ctl_lun_create_params {
+	ctl_backend_lun_flags	flags;
+	uint8_t			device_type;
+	uint64_t		lun_size_bytes;
+	uint32_t		blocksize_bytes;
+	uint32_t		req_lun_id;
+	uint8_t			serial_num[CTL_SN_LEN];
+	uint8_t			device_id[CTL_DEVID_LEN];
+};
+
+/*
+ * LUN removal parameters:
+ *
+ * lun_id:		The number of the LUN to delete.  This must be set.
+ *			The LUN must be backed by the given backend.
+ */
+struct ctl_lun_rm_params {
+	uint32_t		lun_id;
+};
+
+/*
+ * Union of request type data.  Fill in the appropriate union member for
+ * the request type.
+ */
+union ctl_lunreq_data {
+	struct ctl_lun_create_params	create;
+	struct ctl_lun_rm_params	rm;
+};
+
+/*
+ * LUN request interface:
+ *
+ * backend:		This is required, and is NUL-terminated a string
+ *			that is the name of the backend, like "ramdisk" or
+ *			"block".
+ *
+ * reqtype:		The type of request, CTL_LUNREQ_CREATE to create a
+ *			LUN, CTL_LUNREQ_RM to delete a LUN.
+ *
+ * reqdata:		Request type-specific information.  See the
+ *			description of individual the union members above
+ *			for more information.
+ *
+ * num_be_args:		This is the number of backend-specific arguments
+ *			in the be_args array.
+ *
+ * be_args:		This is an array of backend-specific arguments.
+ *			See above for a description of the fields in this
+ *			structure.
+ *
+ * status:		Status of the LUN request.
+ *
+ * error_str:		If the status is CTL_LUN_ERROR, this will
+ *			contain a string describing the error.
+ *
+ * kern_be_args:	For kernel use only.
+ */
+struct ctl_lun_req {
+	char			backend[CTL_BE_NAME_LEN];
+	ctl_lunreq_type		reqtype;
+	union ctl_lunreq_data	reqdata;
+	int			num_be_args;
+	struct ctl_be_arg	*be_args;
+	ctl_lun_status		status;
+	char			error_str[CTL_ERROR_STR_LEN];
+	struct ctl_be_arg	*kern_be_args;
+};
+
+/*
+ * LUN list status:
+ *
+ * NONE:		No status.
+ *
+ * OK:			Request completed successfully.
+ *
+ * NEED_MORE_SPACE:	The allocated length of the entries field is too
+ * 			small for the available data.
+ *
+ * ERROR:		An error occured, look at the error string for a
+ *			description of the error.
+ */
+typedef enum {
+	CTL_LUN_LIST_NONE,
+	CTL_LUN_LIST_OK,
+	CTL_LUN_LIST_NEED_MORE_SPACE,
+	CTL_LUN_LIST_ERROR
+} ctl_lun_list_status;
+
+/*
+ * LUN list interface
+ *
+ * backend_name:	This is a NUL-terminated string.  If the string
+ *			length is 0, then all LUNs on all backends will
+ *			be enumerated.  Otherwise this is the name of the
+ *			backend to be enumerated, like "ramdisk" or "block".
+ *
+ * alloc_len:		The length of the data buffer allocated for entries.
+ *			In order to properly size the buffer, make one call
+ *			with alloc_len set to 0, and then use the returned
+ *			dropped_len as the buffer length to allocate and
+ *			pass in on a subsequent call.
+ *
+ * lun_xml:		XML-formatted information on the requested LUNs.
+ *
+ * fill_len:		The amount of data filled in the storage for entries.
+ *
+ * status:		The status of the request.  See above for the 
+ *			description of the values of this field.
+ *
+ * error_str:		If the status indicates an error, this string will
+ *			be filled in to describe the error.
+ */
+struct ctl_lun_list {
+	char			backend[CTL_BE_NAME_LEN]; /* passed to kernel*/
+	uint32_t		alloc_len;	/* passed to kernel */
+	char                   *lun_xml;	/* filled in kernel */
+	uint32_t		fill_len;	/* passed to userland */
+	ctl_lun_list_status	status;		/* passed to userland */
+	char			error_str[CTL_ERROR_STR_LEN];
+						/* passed to userland */
+};
+
+#define	CTL_IO			_IOWR(CTL_MINOR, 0x00, union ctl_io)
+#define	CTL_ENABLE_PORT		_IOW(CTL_MINOR, 0x04, struct ctl_port_entry)
+#define	CTL_DISABLE_PORT	_IOW(CTL_MINOR, 0x05, struct ctl_port_entry)
+#define	CTL_DUMP_OOA		_IO(CTL_MINOR, 0x06)
+#define	CTL_CHECK_OOA		_IOWR(CTL_MINOR, 0x07, struct ctl_ooa_info)
+#define	CTL_HARD_STOP		_IOR(CTL_MINOR, 0x08, \
+				     struct ctl_hard_startstop_info)
+#define	CTL_HARD_START		_IOR(CTL_MINOR, 0x09, \
+				     struct ctl_hard_startstop_info)
+#define	CTL_DELAY_IO		_IOWR(CTL_MINOR, 0x10, struct ctl_io_delay_info)
+#define	CTL_REALSYNC_GET	_IOR(CTL_MINOR, 0x11, int)
+#define	CTL_REALSYNC_SET	_IOW(CTL_MINOR, 0x12, int)
+#define	CTL_SETSYNC		_IOWR(CTL_MINOR, 0x13, struct ctl_sync_info)
+#define	CTL_GETSYNC		_IOWR(CTL_MINOR, 0x14, struct ctl_sync_info)
+#define	CTL_GETSTATS		_IOWR(CTL_MINOR, 0x15, struct ctl_stats)
+#define	CTL_ERROR_INJECT	_IOWR(CTL_MINOR, 0x16, struct ctl_error_desc)
+#define	CTL_BBRREAD		_IOWR(CTL_MINOR, 0x17, struct ctl_bbrread_info)
+#define	CTL_GET_OOA		_IOWR(CTL_MINOR, 0x18, struct ctl_ooa)
+#define	CTL_DUMP_STRUCTS	_IO(CTL_MINOR, 0x19)
+#define	CTL_GET_PORT_LIST	_IOWR(CTL_MINOR, 0x20, struct ctl_port_list)
+#define	CTL_LUN_REQ		_IOWR(CTL_MINOR, 0x21, struct ctl_lun_req)
+#define	CTL_LUN_LIST		_IOWR(CTL_MINOR, 0x22, struct ctl_lun_list)
+#define	CTL_ERROR_INJECT_DELETE	_IOW(CTL_MINOR, 0x23, struct ctl_error_desc)
+#define	CTL_SET_PORT_WWNS	_IOW(CTL_MINOR, 0x24, struct ctl_port_entry)
+
+#endif /* _CTL_IOCTL_H_ */
+
+/*
+ * vim: ts=8
+ */
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/cam/ctl/ctl_mem_pool.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/sys/cam/ctl/ctl_mem_pool.c	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,192 @@
+/*-
+ * Copyright (c) 2003, 2004 Silicon Graphics International Corp.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions, and the following disclaimer,
+ *    without modification.
+ * 2. Redistributions in binary form must reproduce at minimum a disclaimer
+ *    substantially similar to the "NO WARRANTY" disclaimer below
+ *    ("Disclaimer") and any redistribution must be conditioned upon
+ *    including a substantially similar Disclaimer requirement for further
+ *    binary redistribution.
+ *
+ * NO WARRANTY
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGES.
+ *
+ * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_mem_pool.c#1 $
+ */
+/*
+ * CAM Target Layer memory pool code.
+ *
+ * Author: Ken Merry <ken at FreeBSD.org>
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD: head/sys/cam/ctl/ctl_mem_pool.c 229997 2012-01-12 00:34:33Z ken $");
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/kernel.h>
+#include <sys/types.h>
+#include <sys/malloc.h>
+#include <sys/lock.h>
+#include <sys/mutex.h>
+#include <sys/condvar.h>
+#include <sys/queue.h>
+
+#include <cam/ctl/ctl_mem_pool.h>
+
+MALLOC_DEFINE(M_CTL_POOL, "ctlpool", "CTL memory pool");
+
+int
+ctl_init_mem_pool(struct ctl_mem_pool *pool, int chunk_size,
+		  ctl_mem_pool_flags flags, int grow_inc,
+		  int initial_pool_size)
+{
+	pool->flags = flags;
+	pool->chunk_size = chunk_size;
+	pool->grow_inc = grow_inc;
+	mtx_init(&pool->lock, "Pool mutex", NULL, MTX_DEF);
+	STAILQ_INIT(&pool->free_mem_list);
+	cv_init(&pool->wait_mem, "CTL mem pool");
+
+	if (ctl_grow_mem_pool(pool, initial_pool_size, /*can_wait*/ 1) !=
+	    initial_pool_size)
+		return (1);
+	else
+		return (0);
+}
+
+struct ctl_mem_element *
+ctl_alloc_mem_element(struct ctl_mem_pool *pool, int can_wait)
+{
+	struct ctl_mem_element *mem;
+
+	for (;;) {
+		mtx_lock(&pool->lock);
+
+		mem = STAILQ_FIRST(&pool->free_mem_list);
+		if (mem != NULL) {
+			STAILQ_REMOVE(&pool->free_mem_list, mem,
+				      ctl_mem_element, links);
+			mem->flags = CTL_MEM_ELEMENT_PREALLOC;
+		}
+		mtx_unlock(&pool->lock);
+
+		if (mem != NULL)
+			return (mem);
+
+		/*
+		 * Grow the pool permanantly by the requested increment
+		 * instead of temporarily.  This has the effect that
+		 * whatever the high water mark of transactions is for
+		 * this pool, we'll keep that much memory around.
+		 */
+		if (pool->flags & CTL_MEM_POOL_PERM_GROW) {
+			if (ctl_grow_mem_pool(pool, pool->grow_inc,
+					      can_wait) != 0)
+				continue;
+		}
+		mem = (struct ctl_mem_element *)malloc(sizeof(*mem),
+			M_CTL_POOL, can_wait ? M_WAITOK : M_NOWAIT);
+
+		if (mem != NULL) {
+			mem->flags = CTL_MEM_ELEMENT_NONE;
+			mem->pool = pool;
+
+			mem->bytes = malloc(pool->chunk_size, M_CTL_POOL,
+				can_wait ?  M_WAITOK : M_NOWAIT);
+			if (mem->bytes == NULL) {
+				free(mem, M_CTL_POOL);
+				mem = NULL;
+			} else {
+				return (mem);
+			}
+		}
+
+		if (can_wait == 0)
+			return (NULL);
+
+		cv_wait_unlock(&pool->wait_mem, &pool->lock);
+	}
+}
+
+void
+ctl_free_mem_element(struct ctl_mem_element *mem)
+{
+	struct ctl_mem_pool *pool;
+
+	pool = mem->pool;
+
+	if (mem->flags & CTL_MEM_ELEMENT_PREALLOC) {
+		mtx_lock(&pool->lock);
+		STAILQ_INSERT_TAIL(&pool->free_mem_list, mem, links);
+		mtx_unlock(&pool->lock);
+		cv_broadcast(&pool->wait_mem);
+	} else
+		free(mem, M_CTL_POOL);
+}
+
+int
+ctl_grow_mem_pool(struct ctl_mem_pool *pool, int count, int can_wait)
+{
+	int i;
+
+	for (i = 0; i < count; i++) {
+		struct ctl_mem_element *mem;
+
+		mem = (struct ctl_mem_element *)malloc(sizeof(*mem),
+			M_CTL_POOL, can_wait ? M_WAITOK : M_NOWAIT);
+
+		if (mem == NULL)
+			break;
+
+		mem->bytes = malloc(pool->chunk_size, M_CTL_POOL, can_wait ?
+				     M_WAITOK : M_NOWAIT);
+		if (mem->bytes == NULL) {
+			free(mem, M_CTL_POOL);
+			break;
+		}
+		mem->flags = CTL_MEM_ELEMENT_PREALLOC;
+		mem->pool = pool;
+		mtx_lock(&pool->lock);
+		STAILQ_INSERT_TAIL(&pool->free_mem_list, mem, links);
+		mtx_unlock(&pool->lock);
+	}
+
+	return (i);
+}
+
+int
+ctl_shrink_mem_pool(struct ctl_mem_pool *pool)
+{
+	struct ctl_mem_element *mem, *mem_next;
+
+	mtx_lock(&pool->lock);
+	for (mem = STAILQ_FIRST(&pool->free_mem_list); mem != NULL;
+	     mem = mem_next) {
+		mem_next = STAILQ_NEXT(mem, links);
+
+		STAILQ_REMOVE(&pool->free_mem_list, mem, ctl_mem_element,
+			      links);
+		free(mem->bytes, M_CTL_POOL);
+		free(mem, M_CTL_POOL);
+	}
+	mtx_unlock(&pool->lock);
+
+	return (0);
+}
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/cam/ctl/ctl_mem_pool.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/sys/cam/ctl/ctl_mem_pool.h	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,83 @@
+/*-
+ * Copyright (c) 2003, 2004 Silicon Graphics International Corp.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions, and the following disclaimer,
+ *    without modification.
+ * 2. Redistributions in binary form must reproduce at minimum a disclaimer
+ *    substantially similar to the "NO WARRANTY" disclaimer below
+ *    ("Disclaimer") and any redistribution must be conditioned upon
+ *    including a substantially similar Disclaimer requirement for further
+ *    binary redistribution.
+ *
+ * NO WARRANTY
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGES.
+ *
+ * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_mem_pool.h#1 $
+ * $FreeBSD: head/sys/cam/ctl/ctl_mem_pool.h 229997 2012-01-12 00:34:33Z ken $
+ */
+/*
+ * CAM Target Layer memory pool code.
+ *
+ * Author: Ken Merry <ken at FreeBSD.org>
+ */
+
+#ifndef	_CTL_MEMPOOL_H_
+#define	_CTL_MEMPOOL_H_
+
+typedef enum {
+	CTL_MEM_POOL_NONE,
+	CTL_MEM_POOL_PERM_GROW
+} ctl_mem_pool_flags;
+
+struct ctl_mem_pool {
+	ctl_mem_pool_flags flags;
+	int chunk_size;
+	int grow_inc;
+	struct mtx lock;
+        struct cv wait_mem;
+	STAILQ_HEAD(, ctl_mem_element) free_mem_list;
+};
+
+typedef enum {
+	CTL_MEM_ELEMENT_NONE,
+	CTL_MEM_ELEMENT_PREALLOC
+} ctl_mem_element_flags;
+
+struct ctl_mem_element {
+	ctl_mem_element_flags flags;
+	struct ctl_mem_pool *pool;
+	uint8_t *bytes;
+	STAILQ_ENTRY(ctl_mem_element) links;
+};
+
+#ifdef	_KERNEL
+
+MALLOC_DECLARE(M_CTL_POOL);
+
+int ctl_init_mem_pool(struct ctl_mem_pool *pool, int chunk_size,
+		      ctl_mem_pool_flags flags, int grow_inc,
+		      int initial_pool_size);
+struct ctl_mem_element *ctl_alloc_mem_element(struct ctl_mem_pool *pool,
+					      int can_wait);
+void ctl_free_mem_element(struct ctl_mem_element *mem);
+int ctl_grow_mem_pool(struct ctl_mem_pool *pool, int count,
+		      int can_wait);
+int ctl_shrink_mem_pool(struct ctl_mem_pool *pool);
+#endif	/* _KERNEL */
+
+#endif	/* _CTL_MEMPOOL_H_ */
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/cam/ctl/ctl_private.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/sys/cam/ctl/ctl_private.h	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,493 @@
+/*-
+ * Copyright (c) 2003, 2004, 2005, 2008 Silicon Graphics International Corp.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions, and the following disclaimer,
+ *    without modification.
+ * 2. Redistributions in binary form must reproduce at minimum a disclaimer
+ *    substantially similar to the "NO WARRANTY" disclaimer below
+ *    ("Disclaimer") and any redistribution must be conditioned upon
+ *    including a substantially similar Disclaimer requirement for further
+ *    binary redistribution.
+ *
+ * NO WARRANTY
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGES.
+ *
+ * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_private.h#7 $
+ * $FreeBSD: head/sys/cam/ctl/ctl_private.h 229997 2012-01-12 00:34:33Z ken $
+ */
+/*
+ * CAM Target Layer driver private data structures/definitions.
+ *
+ * Author: Ken Merry <ken at FreeBSD.org>
+ */
+
+#ifndef	_CTL_PRIVATE_H_
+#define	_CTL_PRIVATE_H_
+
+/*
+ * SCSI vendor and product names.
+ */
+#define	CTL_VENDOR		"FREEBSD "
+#define	CTL_DIRECT_PRODUCT	"CTLDISK         "
+#define	CTL_PROCESSOR_PRODUCT	"CTLPROCESSOR    "
+#define	CTL_UNKNOWN_PRODUCT	"CTLDEVICE       "
+
+struct ctl_fe_ioctl_startstop_info {
+	struct cv			sem;
+	struct ctl_hard_startstop_info	hs_info;
+};
+
+struct ctl_fe_ioctl_bbrread_info {
+	struct cv			sem;
+	struct ctl_bbrread_info		*bbr_info;
+	int				wakeup_done;
+	struct mtx			*lock;
+};
+
+typedef enum {
+	CTL_IOCTL_INPROG,
+	CTL_IOCTL_DATAMOVE,
+	CTL_IOCTL_DONE
+} ctl_fe_ioctl_state;
+
+struct ctl_fe_ioctl_params {
+	struct cv		sem;
+	struct mtx		ioctl_mtx;
+	ctl_fe_ioctl_state	state;
+};
+
+#define	CTL_POOL_ENTRIES_INTERNAL	200
+#define	CTL_POOL_ENTRIES_EMERGENCY	300
+#define CTL_POOL_ENTRIES_OTHER_SC   200
+
+typedef enum {
+	CTL_POOL_INTERNAL,
+	CTL_POOL_FETD,
+	CTL_POOL_EMERGENCY,
+	CTL_POOL_IOCTL,
+	CTL_POOL_4OTHERSC
+} ctl_pool_type;
+
+typedef enum {
+	CTL_POOL_FLAG_NONE	= 0x00,
+	CTL_POOL_FLAG_INVALID	= 0x01
+} ctl_pool_flags;
+
+struct ctl_io_pool {
+	ctl_pool_type			type;
+	ctl_pool_flags			flags;
+	uint32_t			id;
+	struct ctl_softc		*ctl_softc;
+	uint32_t			refcount;
+	uint64_t			total_allocated;
+	uint64_t			total_freed;
+	int32_t				total_ctl_io;
+	int32_t				free_ctl_io;
+	STAILQ_HEAD(, ctl_io_hdr)	free_queue;
+	STAILQ_ENTRY(ctl_io_pool)	links;
+};
+
+typedef enum {
+	CTL_IOCTL_FLAG_NONE	= 0x00,
+	CTL_IOCTL_FLAG_ENABLED	= 0x01
+} ctl_ioctl_flags;
+
+struct ctl_ioctl_info {
+	ctl_ioctl_flags		flags;
+	uint32_t		cur_tag_num;
+	struct ctl_frontend	fe;
+	char			port_name[24];
+};
+
+typedef enum {
+	CTL_SER_BLOCK,
+	CTL_SER_EXTENT,
+	CTL_SER_PASS,
+	CTL_SER_SKIP
+} ctl_serialize_action;
+
+typedef enum {
+	CTL_ACTION_BLOCK,
+	CTL_ACTION_OVERLAP,
+	CTL_ACTION_OVERLAP_TAG,
+	CTL_ACTION_PASS,
+	CTL_ACTION_SKIP,
+	CTL_ACTION_ERROR
+} ctl_action;
+
+/*
+ * WARNING:  Keep the bottom nibble here free, we OR in the data direction
+ * flags for each command.
+ *
+ * Note:  "OK_ON_ALL_LUNS" == we don't have to have a lun configured
+ *        "OK_ON_BOTH"     == we have to have a lun configured
+ */
+typedef enum {
+	CTL_CMD_FLAG_NONE		= 0x0000,
+	CTL_CMD_FLAG_NO_SENSE		= 0x0010,
+	CTL_CMD_FLAG_OK_ON_ALL_LUNS	= 0x0020,
+	CTL_CMD_FLAG_ALLOW_ON_RESV	= 0x0040,
+	CTL_CMD_FLAG_OK_ON_PROC		= 0x0100,
+	CTL_CMD_FLAG_OK_ON_SLUN		= 0x0200,
+	CTL_CMD_FLAG_OK_ON_BOTH		= 0x0300,
+	CTL_CMD_FLAG_OK_ON_STOPPED	= 0x0400,
+	CTL_CMD_FLAG_OK_ON_INOPERABLE	= 0x0800,
+	CTL_CMD_FLAG_OK_ON_OFFLINE	= 0x1000,
+	CTL_CMD_FLAG_OK_ON_SECONDARY	= 0x2000,
+	CTL_CMD_FLAG_ALLOW_ON_PR_RESV   = 0x4000
+} ctl_cmd_flags;
+
+typedef enum {
+	CTL_SERIDX_TUR	= 0,
+	CTL_SERIDX_READ,
+	CTL_SERIDX_WRITE,
+	CTL_SERIDX_MD_SNS,
+	CTL_SERIDX_MD_SEL,
+	CTL_SERIDX_RQ_SNS,
+	CTL_SERIDX_INQ,
+	CTL_SERIDX_RD_CAP,
+	CTL_SERIDX_RESV,
+	CTL_SERIDX_REL,
+	CTL_SERIDX_LOG_SNS,
+	CTL_SERIDX_FORMAT,
+	CTL_SERIDX_START,
+	CTL_SERIDX_PRES_IN,
+	CTL_SERIDX_PRES_OUT,
+	CTL_SERIDX_MAIN_IN,
+	/* TBD: others to be filled in as needed */
+	CTL_SERIDX_COUNT, /* LAST, not a normal code, provides # codes */
+	CTL_SERIDX_INVLD = CTL_SERIDX_COUNT
+} ctl_seridx;
+
+typedef int	ctl_opfunc(struct ctl_scsiio *ctsio);
+
+struct ctl_cmd_entry {
+	ctl_opfunc		*execute;
+	ctl_seridx		seridx;
+	ctl_cmd_flags		flags;
+	ctl_lun_error_pattern	pattern;
+};
+
+typedef enum {
+	CTL_LUN_NONE		= 0x000,
+	CTL_LUN_CONTROL		= 0x001,
+	CTL_LUN_RESERVED	= 0x002,
+	CTL_LUN_INVALID		= 0x004,
+	CTL_LUN_DISABLED	= 0x008,
+	CTL_LUN_MALLOCED	= 0x010,
+	CTL_LUN_STOPPED		= 0x020,
+	CTL_LUN_INOPERABLE	= 0x040,
+	CTL_LUN_OFFLINE		= 0x080,
+	CTL_LUN_PR_RESERVED	= 0x100,
+	CTL_LUN_PRIMARY_SC	= 0x200,
+	CTL_LUN_SENSE_DESC	= 0x400
+} ctl_lun_flags;
+
+typedef enum {
+	CTLBLOCK_FLAG_NONE	= 0x00,
+	CTLBLOCK_FLAG_INVALID	= 0x01
+} ctlblock_flags;
+
+union ctl_softcs {
+	struct ctl_softc	*ctl_softc;
+	struct ctlblock_softc	*ctlblock_softc;
+};
+
+/*
+ * Mode page defaults.
+ */
+#if 0
+/*
+ * These values make Solaris trim off some of the capacity.
+ */
+#define	CTL_DEFAULT_SECTORS_PER_TRACK	63
+#define	CTL_DEFAULT_HEADS		255
+/*
+ * These values seem to work okay.
+ */
+#define	CTL_DEFAULT_SECTORS_PER_TRACK	63
+#define	CTL_DEFAULT_HEADS		16
+/*
+ * These values work reasonably well.
+ */
+#define	CTL_DEFAULT_SECTORS_PER_TRACK	512
+#define	CTL_DEFAULT_HEADS		64
+#endif
+
+/*
+ * Solaris is somewhat picky about how many heads and sectors per track you
+ * have defined in mode pages 3 and 4.  These values seem to cause Solaris
+ * to get the capacity more or less right when you run the format tool.
+ * They still have problems when dealing with devices larger than 1TB,
+ * but there isn't anything we can do about that.
+ *
+ * For smaller LUN sizes, this ends up causing the number of cylinders to
+ * work out to 0.  Solaris actually recognizes that and comes up with its
+ * own bogus geometry to fit the actual capacity of the drive.  They really
+ * should just give up on geometry and stick to the read capacity
+ * information alone for modern disk drives.
+ *
+ * One thing worth mentioning about Solaris' mkfs command is that it
+ * doesn't like sectors per track values larger than 256.  512 seems to
+ * work okay for format, but causes problems when you try to make a
+ * filesystem.
+ *
+ * Another caveat about these values:  the product of these two values
+ * really should be a power of 2.  This is because of the simplistic
+ * shift-based calculation that we have to use on the i386 platform to
+ * calculate the number of cylinders here.  (If you use a divide, you end
+ * up calling __udivdi3(), which is a hardware FP call on the PC.  On the
+ * XScale, it is done in software, so you can do that from inside the
+ * kernel.)
+ *
+ * So for the current values (256 S/T, 128 H), we get 32768, which works
+ * very nicely for calculating cylinders.
+ *
+ * If you want to change these values so that their product is no longer a
+ * power of 2, re-visit the calculation in ctl_init_page_index().  You may
+ * need to make it a bit more complicated to get the number of cylinders
+ * right.
+ */
+#define	CTL_DEFAULT_SECTORS_PER_TRACK	256
+#define	CTL_DEFAULT_HEADS		128
+
+#define	CTL_DEFAULT_ROTATION_RATE	10000
+
+struct ctl_page_index;
+
+typedef int	ctl_modesen_handler(struct ctl_scsiio *ctsio,
+				    struct ctl_page_index *page_index,
+				    int pc);
+typedef int	ctl_modesel_handler(struct ctl_scsiio *ctsio,
+				    struct ctl_page_index *page_index,
+				    uint8_t *page_ptr);
+
+typedef enum {
+	CTL_PAGE_FLAG_NONE	 = 0x00,
+	CTL_PAGE_FLAG_DISK_ONLY	 = 0x01
+} ctl_page_flags;
+
+struct ctl_page_index {
+	uint8_t			page_code;
+	uint8_t			subpage;
+	uint16_t		page_len;
+	uint8_t			*page_data;
+	ctl_page_flags		page_flags;
+	ctl_modesen_handler	*sense_handler;
+	ctl_modesel_handler	*select_handler;
+};
+
+#define	CTL_PAGE_CURRENT	0x00
+#define	CTL_PAGE_CHANGEABLE	0x01
+#define	CTL_PAGE_DEFAULT	0x02
+#define	CTL_PAGE_SAVED		0x03
+
+static struct ctl_page_index page_index_template[] = {
+	{SMS_FORMAT_DEVICE_PAGE, 0, sizeof(struct scsi_format_page), NULL,
+	 CTL_PAGE_FLAG_DISK_ONLY, NULL, NULL},
+	{SMS_RIGID_DISK_PAGE, 0, sizeof(struct scsi_rigid_disk_page), NULL,
+	 CTL_PAGE_FLAG_DISK_ONLY, NULL, NULL},
+	{SMS_CACHING_PAGE, 0, sizeof(struct scsi_caching_page), NULL,
+	 CTL_PAGE_FLAG_DISK_ONLY, NULL, NULL},
+	{SMS_CONTROL_MODE_PAGE, 0, sizeof(struct scsi_control_page), NULL,
+	 CTL_PAGE_FLAG_NONE, NULL, ctl_control_page_handler},
+   	{SMS_VENDOR_SPECIFIC_PAGE | SMPH_SPF, PWR_SUBPAGE_CODE,
+	 sizeof(struct copan_power_subpage), NULL, CTL_PAGE_FLAG_NONE,
+	 ctl_power_sp_sense_handler, ctl_power_sp_handler},
+	{SMS_VENDOR_SPECIFIC_PAGE | SMPH_SPF, APS_SUBPAGE_CODE,
+	 sizeof(struct copan_aps_subpage), NULL, CTL_PAGE_FLAG_NONE,
+	 NULL, ctl_aps_sp_handler},
+	{SMS_VENDOR_SPECIFIC_PAGE | SMPH_SPF, DBGCNF_SUBPAGE_CODE,
+	 sizeof(struct copan_debugconf_subpage), NULL, CTL_PAGE_FLAG_NONE,
+	 ctl_debugconf_sp_sense_handler, ctl_debugconf_sp_select_handler},
+};
+
+#define	CTL_NUM_MODE_PAGES sizeof(page_index_template)/   \
+			   sizeof(page_index_template[0])
+
+struct ctl_mode_pages {
+	struct scsi_format_page		format_page[4];
+	struct scsi_rigid_disk_page	rigid_disk_page[4];
+	struct scsi_caching_page	caching_page[4];
+	struct scsi_control_page	control_page[4];
+	struct copan_power_subpage	power_subpage[4];
+	struct copan_aps_subpage	aps_subpage[4];
+	struct copan_debugconf_subpage	debugconf_subpage[4];
+	struct ctl_page_index		index[CTL_NUM_MODE_PAGES];
+};
+
+struct ctl_pending_sense {
+	ctl_ua_type		ua_pending;
+	struct scsi_sense_data	sense;
+};
+
+struct ctl_lun_delay_info {
+	ctl_delay_type		datamove_type;
+	uint32_t		datamove_delay;
+	ctl_delay_type		done_type;
+	uint32_t		done_delay;
+};
+
+typedef enum {
+	CTL_ERR_INJ_NONE	= 0x00,
+	CTL_ERR_INJ_ABORTED	= 0x01
+} ctl_err_inject_flags;
+
+typedef enum {
+	CTL_PR_FLAG_NONE	= 0x00,
+	CTL_PR_FLAG_REGISTERED	= 0x01,
+	CTL_PR_FLAG_ACTIVE_RES	= 0x02
+} ctl_per_res_flags;
+
+struct ctl_per_res_info {
+	struct scsi_per_res_key res_key;
+	uint8_t  registered;
+};
+
+#define CTL_PR_ALL_REGISTRANTS  0xFFFF
+#define CTL_PR_NO_RESERVATION   0xFFF0
+
+/*
+ * For report target port groups.
+ */
+#define NUM_TARGET_PORT_GROUPS	2
+#define NUM_PORTS_PER_GRP	2
+
+struct ctl_lun {
+	struct mtx			lun_lock;
+	struct ctl_id			target;
+	uint64_t			lun;
+	ctl_lun_flags			flags;
+	STAILQ_HEAD(,ctl_error_desc)	error_list;
+	uint64_t			error_serial;
+	struct ctl_softc		*ctl_softc;
+	struct ctl_be_lun		*be_lun;
+	struct ctl_backend_driver	*backend;
+	int				io_count;
+	struct ctl_lun_delay_info	delay_info;
+	int				sync_interval;
+	int				sync_count;
+	TAILQ_HEAD(ctl_ooaq, ctl_io_hdr)  ooa_queue;
+	TAILQ_HEAD(ctl_blockq,ctl_io_hdr) blocked_queue;
+	STAILQ_ENTRY(ctl_lun)		links;
+	STAILQ_ENTRY(ctl_lun)		run_links;
+	struct ctl_nexus		rsv_nexus;
+	uint32_t			have_ca[CTL_MAX_INITIATORS >> 5];
+	struct ctl_pending_sense	pending_sense[CTL_MAX_INITIATORS];
+	struct ctl_mode_pages		mode_pages;
+	struct ctl_lun_io_stats		stats;
+	struct ctl_per_res_info		per_res[2*CTL_MAX_INITIATORS];
+	unsigned int			PRGeneration;
+	int				pr_key_count;
+	uint16_t        		pr_res_idx;
+	uint8_t				res_type;
+	uint8_t				write_buffer[524288];
+};
+
+typedef enum {
+	CTL_FLAG_TASK_PENDING	= 0x01,
+	CTL_FLAG_REAL_SYNC	= 0x02,
+	CTL_FLAG_MASTER_SHELF	= 0x04
+} ctl_gen_flags;
+
+struct ctl_wwpn_iid {
+	int in_use;
+	uint64_t wwpn;
+	uint32_t iid;
+	int32_t port;
+};
+
+struct ctl_softc {
+	struct mtx ctl_lock;
+	struct cdev *dev;
+	int open_count;
+	struct ctl_id target;
+	int num_disks;
+	int num_luns;
+	ctl_gen_flags flags;
+	ctl_ha_mode ha_mode;
+	struct ctl_ioctl_info ioctl_info;
+	struct ctl_lun lun;
+	struct ctl_io_pool *internal_pool;
+	struct ctl_io_pool *emergency_pool;
+	struct ctl_io_pool *othersc_pool;
+	struct proc *work_thread;
+	int targ_online;
+	uint32_t ctl_lun_mask[CTL_MAX_LUNS >> 5];
+	struct ctl_lun *ctl_luns[CTL_MAX_LUNS];
+	struct ctl_wwpn_iid wwpn_iid[CTL_MAX_PORTS][CTL_MAX_INIT_PER_PORT];
+	uint32_t ctl_port_mask;
+	uint64_t aps_locked_lun;
+	STAILQ_HEAD(, ctl_lun) lun_list;
+	STAILQ_HEAD(, ctl_be_lun) pending_lun_queue;
+	STAILQ_HEAD(, ctl_io_hdr) task_queue;
+	STAILQ_HEAD(, ctl_io_hdr) incoming_queue;
+	STAILQ_HEAD(, ctl_io_hdr) rtr_queue;
+	STAILQ_HEAD(, ctl_io_hdr) done_queue;
+	STAILQ_HEAD(, ctl_io_hdr) isc_queue;
+	uint32_t num_frontends;
+	STAILQ_HEAD(, ctl_frontend) fe_list;
+	struct ctl_frontend *ctl_ports[CTL_MAX_PORTS];
+	uint32_t num_backends;
+	STAILQ_HEAD(, ctl_backend_driver) be_list;
+	uint32_t num_pools;
+	uint32_t cur_pool_id;
+	STAILQ_HEAD(, ctl_io_pool) io_pools;
+	time_t last_print_jiffies;
+	uint32_t skipped_prints;
+};
+
+#ifdef _KERNEL
+
+extern struct ctl_cmd_entry ctl_cmd_table[];
+
+uint32_t ctl_get_initindex(struct ctl_nexus *nexus);
+int ctl_pool_create(struct ctl_softc *ctl_softc, ctl_pool_type pool_type,
+		    uint32_t total_ctl_io, struct ctl_io_pool **npool);
+int ctl_pool_acquire(struct ctl_io_pool *pool);
+int ctl_pool_invalidate(struct ctl_io_pool *pool);
+int ctl_pool_release(struct ctl_io_pool *pool);
+void ctl_pool_free(struct ctl_softc *ctl_softc, struct ctl_io_pool *pool);
+int ctl_scsi_release(struct ctl_scsiio *ctsio);
+int ctl_scsi_reserve(struct ctl_scsiio *ctsio);
+int ctl_start_stop(struct ctl_scsiio *ctsio);
+int ctl_sync_cache(struct ctl_scsiio *ctsio);
+int ctl_format(struct ctl_scsiio *ctsio);
+int ctl_write_buffer(struct ctl_scsiio *ctsio);
+int ctl_mode_select(struct ctl_scsiio *ctsio);
+int ctl_mode_sense(struct ctl_scsiio *ctsio);
+int ctl_read_capacity(struct ctl_scsiio *ctsio);
+int ctl_service_action_in(struct ctl_scsiio *ctsio);
+int ctl_read_write(struct ctl_scsiio *ctsio);
+int ctl_report_luns(struct ctl_scsiio *ctsio);
+int ctl_request_sense(struct ctl_scsiio *ctsio);
+int ctl_tur(struct ctl_scsiio *ctsio);
+int ctl_inquiry(struct ctl_scsiio *ctsio);
+int ctl_persistent_reserve_in(struct ctl_scsiio *ctsio);
+int ctl_persistent_reserve_out(struct ctl_scsiio *ctsio);
+int ctl_maintenance_in(struct ctl_scsiio *ctsio);
+void ctl_done_lock(union ctl_io *io, int have_lock);
+int ctl_isc(struct ctl_scsiio *ctsio);
+
+#endif	/* _KERNEL */
+
+#endif	/* _CTL_PRIVATE_H_ */
+
+/*
+ * vim: ts=8
+ */
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/cam/ctl/ctl_scsi_all.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/sys/cam/ctl/ctl_scsi_all.c	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,227 @@
+/*-
+ * Implementation of Utility functions for all SCSI device types.
+ *
+ * Copyright (c) 1997, 1998, 1999 Justin T. Gibbs.
+ * Copyright (c) 1997, 1998, 2003 Kenneth D. Merry.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions, and the following disclaimer,
+ *    without modification, immediately at the beginning of the file.
+ * 2. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_scsi_all.c#2 $
+ */
+
+#include <sys/param.h>
+
+__FBSDID("$FreeBSD: head/sys/cam/ctl/ctl_scsi_all.c 229997 2012-01-12 00:34:33Z ken $");
+
+#include <sys/types.h>
+#ifdef _KERNEL
+#include <sys/systm.h>
+#include <sys/libkern.h>
+#include <sys/kernel.h>
+#include <sys/sysctl.h>
+#else
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <inttypes.h>
+#endif
+
+#include <cam/cam.h>
+#include <cam/cam_ccb.h>
+#include <cam/cam_queue.h>
+#include <cam/cam_xpt.h>
+#include <cam/scsi/scsi_all.h>
+
+#include <cam/ctl/ctl_io.h>
+#include <cam/ctl/ctl_scsi_all.h>
+#include <sys/sbuf.h>
+#ifndef _KERNEL
+#include <camlib.h>
+#endif
+
+const char *
+ctl_scsi_status_string(struct ctl_scsiio *ctsio)
+{
+	switch(ctsio->scsi_status) {
+	case SCSI_STATUS_OK:
+		return("OK");
+	case SCSI_STATUS_CHECK_COND:
+		return("Check Condition");
+	case SCSI_STATUS_BUSY:
+		return("Busy");
+	case SCSI_STATUS_INTERMED:
+		return("Intermediate");
+	case SCSI_STATUS_INTERMED_COND_MET:
+		return("Intermediate-Condition Met");
+	case SCSI_STATUS_RESERV_CONFLICT:
+		return("Reservation Conflict");
+	case SCSI_STATUS_CMD_TERMINATED:
+		return("Command Terminated");
+	case SCSI_STATUS_QUEUE_FULL:
+		return("Queue Full");
+	case SCSI_STATUS_ACA_ACTIVE:
+		return("ACA Active");
+	case SCSI_STATUS_TASK_ABORTED:
+		return("Task Aborted");
+	default: {
+		static char unkstr[64];
+		snprintf(unkstr, sizeof(unkstr), "Unknown %#x",
+			 ctsio->scsi_status);
+		return(unkstr);
+	}
+	}
+}
+
+/*
+ * scsi_command_string() returns 0 for success and -1 for failure.
+ */
+int
+ctl_scsi_command_string(struct ctl_scsiio *ctsio,
+			struct scsi_inquiry_data *inq_data, struct sbuf *sb)
+{
+	char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
+
+	sbuf_printf(sb, "%s. CDB: %s",
+		    scsi_op_desc(ctsio->cdb[0], inq_data),
+		    scsi_cdb_string(ctsio->cdb, cdb_str, sizeof(cdb_str)));
+
+	return(0);
+}
+
+void
+ctl_scsi_path_string(union ctl_io *io, char *path_str, int len)
+{
+	if (io->io_hdr.nexus.targ_target.wwid[0] == 0) {
+		snprintf(path_str, len, "(%ju:%d:%ju:%d): ",
+			 (uintmax_t)io->io_hdr.nexus.initid.id,
+			 io->io_hdr.nexus.targ_port,
+			 (uintmax_t)io->io_hdr.nexus.targ_target.id,
+			 io->io_hdr.nexus.targ_lun);
+	} else {
+		/*
+		 * XXX KDM find a better way to display FC WWIDs.
+		 */
+#ifdef _KERNEL
+		snprintf(path_str, len, "(%ju:%d:%#jx,%#jx:%d): ",
+			 (uintmax_t)io->io_hdr.nexus.initid.id,
+			 io->io_hdr.nexus.targ_port,
+			 (intmax_t)io->io_hdr.nexus.targ_target.wwid[0],
+			 (intmax_t)io->io_hdr.nexus.targ_target.wwid[1],
+			 io->io_hdr.nexus.targ_lun);
+#else /* _KERNEL */
+		snprintf(path_str, len, "(%ju:%d:%#jx,%#jx:%d): ",
+			 (uintmax_t)io->io_hdr.nexus.initid.id,
+			 io->io_hdr.nexus.targ_port,
+			 (intmax_t)io->io_hdr.nexus.targ_target.wwid[0],
+			 (intmax_t)io->io_hdr.nexus.targ_target.wwid[1],
+			 io->io_hdr.nexus.targ_lun);
+#endif /* _KERNEL */
+	}
+}
+
+/*
+ * ctl_scsi_sense_sbuf() returns 0 for success and -1 for failure.
+ */
+int
+ctl_scsi_sense_sbuf(struct ctl_scsiio *ctsio,
+		    struct scsi_inquiry_data *inq_data, struct sbuf *sb,
+		    scsi_sense_string_flags flags)
+{
+	char	  path_str[64];
+
+	if ((ctsio == NULL) || (sb == NULL))
+		return(-1);
+
+	ctl_scsi_path_string((union ctl_io *)ctsio, path_str, sizeof(path_str));
+
+	if (flags & SSS_FLAG_PRINT_COMMAND) {
+
+		sbuf_cat(sb, path_str);
+
+		ctl_scsi_command_string(ctsio, inq_data, sb);
+
+		sbuf_printf(sb, "\n");
+	}
+
+	scsi_sense_only_sbuf(&ctsio->sense_data, ctsio->sense_len, sb,
+			     path_str, inq_data, ctsio->cdb, ctsio->cdb_len);
+
+	return(0);
+}
+
+char *
+ctl_scsi_sense_string(struct ctl_scsiio *ctsio,
+		      struct scsi_inquiry_data *inq_data, char *str,
+		      int str_len)
+{
+	struct sbuf sb;
+
+	sbuf_new(&sb, str, str_len, 0);
+
+	ctl_scsi_sense_sbuf(ctsio, inq_data, &sb, SSS_FLAG_PRINT_COMMAND);
+
+	sbuf_finish(&sb);
+
+	return(sbuf_data(&sb));
+}
+
+#ifdef _KERNEL
+void 
+ctl_scsi_sense_print(struct ctl_scsiio *ctsio,
+		     struct scsi_inquiry_data *inq_data)
+{
+	struct sbuf sb;
+	char str[512];
+
+	sbuf_new(&sb, str, sizeof(str), 0);
+
+	ctl_scsi_sense_sbuf(ctsio, inq_data, &sb, SSS_FLAG_PRINT_COMMAND);
+
+	sbuf_finish(&sb);
+
+	printf("%s", sbuf_data(&sb));
+}
+
+#else /* _KERNEL */
+void
+ctl_scsi_sense_print(struct ctl_scsiio *ctsio,
+		     struct scsi_inquiry_data *inq_data, FILE *ofile)
+{
+	struct sbuf sb;
+	char str[512];
+
+	if ((ctsio == NULL) || (ofile == NULL))
+		return;
+
+	sbuf_new(&sb, str, sizeof(str), 0);
+
+	ctl_scsi_sense_sbuf(ctsio, inq_data, &sb, SSS_FLAG_PRINT_COMMAND);
+
+	sbuf_finish(&sb);
+
+	fprintf(ofile, "%s", sbuf_data(&sb));
+}
+
+#endif /* _KERNEL */
+
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/cam/ctl/ctl_scsi_all.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/sys/cam/ctl/ctl_scsi_all.h	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,52 @@
+/*-
+ * Copyright (c) 1997, 1998, 1999 Justin T. Gibbs.
+ * Copyright (c) 1997, 1998, 2003 Kenneth D. Merry.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions, and the following disclaimer,
+ *    without modification, immediately at the beginning of the file.
+ * 2. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_scsi_all.h#2 $
+ */
+
+__FBSDID("$FreeBSD: head/sys/cam/ctl/ctl_scsi_all.h 229997 2012-01-12 00:34:33Z ken $");
+
+__BEGIN_DECLS
+const char *	ctl_scsi_status_string(struct ctl_scsiio *ctsio);
+#ifdef _KERNEL
+void		ctl_scsi_sense_print(struct ctl_scsiio *ctsio,
+				     struct scsi_inquiry_data *inq_data);
+#else /* _KERNEL */
+void		ctl_scsi_sense_print(struct ctl_scsiio *ctsio,
+				     struct scsi_inquiry_data *inq_data,
+				     FILE *ofile);
+#endif /* _KERNEL */
+int ctl_scsi_command_string(struct ctl_scsiio *ctsio,
+			    struct scsi_inquiry_data *inq_data,struct sbuf *sb);
+int ctl_scsi_sense_sbuf(struct ctl_scsiio *ctsio,
+			struct scsi_inquiry_data *inq_data, struct sbuf *sb,
+			scsi_sense_string_flags flags);
+void ctl_scsi_path_string(union ctl_io *io, char *path_str, int strlen);
+char *ctl_scsi_sense_string(struct ctl_scsiio *ctsio,
+			    struct scsi_inquiry_data *inq_data, char *str,
+			    int str_len);
+
+__END_DECLS
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/cam/ctl/ctl_ser_table.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/sys/cam/ctl/ctl_ser_table.c	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,81 @@
+/*-
+ * Copyright (c) 2003 Silicon Graphics International Corp.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions, and the following disclaimer,
+ *    without modification.
+ * 2. Redistributions in binary form must reproduce at minimum a disclaimer
+ *    substantially similar to the "NO WARRANTY" disclaimer below
+ *    ("Disclaimer") and any redistribution must be conditioned upon
+ *    including a substantially similar Disclaimer requirement for further
+ *    binary redistribution.
+ *
+ * NO WARRANTY
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGES.
+ *
+ * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_ser_table.c#1 $
+ * $FreeBSD: head/sys/cam/ctl/ctl_ser_table.c 229997 2012-01-12 00:34:33Z ken $
+ */
+
+/*
+ * CAM Target Layer command serialization table.
+ *
+ * Author: Kim Le
+ */
+
+/****************************************************************************/
+/* TABLE       ctlSerTbl                                                    */
+/*                                                                          */
+/*  The matrix which drives the serialization algorithm. The major index    */
+/*  (the first) into this table is the command being checked and the minor  */
+/*  index is the command against which the first command is being checked.  */
+/*  i.e., the major index (row) command is ahead of the minor index command */
+/*  (column) in the queue.  This allows the code to optimize by capturing   */
+/*  the result of the first indexing operation into a pointer.              */
+/*                                                                          */
+/*  Whenever a new value is added to the IDX_T type, this matrix must be    */
+/*  expanded by one row AND one column -- Because of this, some effort      */
+/*  should be made to re-use the indexes whenever possible.                 */
+/*                                                                          */
+/****************************************************************************/
+
+#define	sK	CTL_SER_SKIP		/* Skip */
+#define	pS	CTL_SER_PASS		/* pS */
+#define	bK	CTL_SER_BLOCK		/* Blocked */
+#define	xT	CTL_SER_EXTENT		/* Extent check */
+
+static ctl_serialize_action
+ctl_serialize_table[CTL_SERIDX_COUNT][CTL_SERIDX_COUNT] = {
+/**>IDX_ :: 2nd:TUR RD  WRT  MDSN MDSL RQSN INQ  RDCP RES  REL LSNS FMT STR PRIN PROT MAININ*/
+/*TUR     */{   pS, pS, pS,  bK,  bK,  bK,  pS,  pS,  bK,  bK, pS,  bK, bK, bK,  bK,  bK},
+/*READ    */{   pS, pS, xT,  bK,  bK,  bK,  pS,  pS,  bK,  bK, pS,  bK, bK, bK,  bK,  bK},
+/*WRITE   */{   pS, xT, xT,  bK,  bK,  bK,  pS,  pS,  bK,  bK, pS,  bK, bK, bK,  bK,  bK},
+/*MD_SNS  */{   bK, bK, bK,  pS,  bK,  bK,  pS,  pS,  bK,  bK, pS,  bK, bK, bK,  bK,  bK},
+/*MD_SEL  */{   bK, bK, bK,  bK,  bK,  bK,  pS,  pS,  bK,  bK, pS,  bK, bK, bK,  bK,  bK},
+/*RQ_SNS  */{   pS, pS, pS,  pS,  pS,  bK,  pS,  pS,  bK,  bK, pS,  bK, bK, bK,  bK,  bK},
+/*INQ     */{   pS, pS, pS,  pS,  pS,  bK,  pS,  pS,  bK,  bK, pS,  bK, bK, bK,  bK,  bK},
+/*RD_CAP  */{   pS, pS, pS,  pS,  pS,  bK,  pS,  pS,  bK,  bK, pS,  bK, bK, bK,  bK,  bK},
+/*RESV    */{   bK, bK, bK,  bK,  bK,  bK,  bK,  bK,  bK,  bK, bK,  bK, bK, bK,  bK,  bK},
+/*REL     */{   bK, bK, bK,  bK,  bK,  bK,  bK,  bK,  bK,  bK, bK,  bK, bK, bK,  bK,  bK},
+/*LOG_SNS */{   pS, pS, pS,  pS,  bK,  bK,  pS,  pS,  bK,  bK, pS,  bK, bK, bK,  bK,  bK},
+/*FORMAT  */{   pS, bK, bK,  bK,  bK,  pS,  pS,  bK,  bK,  bK, bK,  bK, bK, bK,  bK,  bK},
+/*START   */{   bK, bK, bK,  bK,  bK,  bK,  pS,  bK,  bK,  bK, bK,  bK, bK, bK,  bK,  bK},
+/*PRES_IN */{   bK, bK, bK,  bK,  bK,  bK,  bK,  bK,  bK,  bK, bK,  bK, bK, bK,  bK,  bK},
+/*PRES_OUT*/{   bK, bK, bK,  bK,  bK,  bK,  bK,  bK,  bK,  bK, bK,  bK, bK, bK,  bK,  bK},
+/*MAIN_IN */{   bK, bK, bK,  bK,  bK,  bK,  pS,  bK,  bK,  bK, bK,  bK, bK, bK,  bK,  pS}
+};
+
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/cam/ctl/ctl_util.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/sys/cam/ctl/ctl_util.c	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,843 @@
+/*-
+ * Copyright (c) 2003 Silicon Graphics International Corp.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions, and the following disclaimer,
+ *    without modification.
+ * 2. Redistributions in binary form must reproduce at minimum a disclaimer
+ *    substantially similar to the "NO WARRANTY" disclaimer below
+ *    ("Disclaimer") and any redistribution must be conditioned upon
+ *    including a substantially similar Disclaimer requirement for further
+ *    binary redistribution.
+ *
+ * NO WARRANTY
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGES.
+ *
+ * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_util.c#2 $
+ */
+/*
+ * CAM Target Layer SCSI library
+ *
+ * Author: Ken Merry <ken at FreeBSD.org>
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD: head/sys/cam/ctl/ctl_util.c 229997 2012-01-12 00:34:33Z ken $");
+
+#ifdef _KERNEL
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/kernel.h>
+#include <sys/types.h>
+#include <sys/malloc.h>
+#else /* __KERNEL__ */
+#include <sys/types.h>
+#include <sys/time.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#endif /* __KERNEL__ */
+#include <sys/sbuf.h>
+#include <sys/queue.h>
+#include <sys/callout.h>
+#include <cam/scsi/scsi_all.h>
+#include <cam/ctl/ctl_io.h>
+#include <cam/ctl/ctl_scsi_all.h>
+#include <cam/ctl/ctl_util.h>
+
+struct ctl_status_desc {
+	ctl_io_status status;
+	const char *description;
+};
+
+struct ctl_task_desc {
+	ctl_task_type	task_action;
+	const char	*description;
+};
+static struct ctl_status_desc ctl_status_table[] = {
+	{CTL_STATUS_NONE, "No Status"},
+	{CTL_SUCCESS, "Command Completed Successfully"},
+	{CTL_CMD_TIMEOUT, "Command Timed Out"},
+	{CTL_SEL_TIMEOUT, "Selection Timeout"},
+	{CTL_ERROR, "Command Failed"},
+	{CTL_SCSI_ERROR, "SCSI Error"},
+	{CTL_CMD_ABORTED, "Command Aborted"},
+};
+
+static struct ctl_task_desc ctl_task_table[] = {
+	{CTL_TASK_ABORT_TASK, "Abort Task"},
+	{CTL_TASK_ABORT_TASK_SET, "Abort Task Set"},
+	{CTL_TASK_CLEAR_ACA, "Clear ACA"},
+	{CTL_TASK_CLEAR_TASK_SET, "Clear Task Set"},
+	{CTL_TASK_LUN_RESET, "LUN Reset"},
+	{CTL_TASK_TARGET_RESET, "Target Reset"},
+	{CTL_TASK_BUS_RESET, "Bus Reset"},
+	{CTL_TASK_PORT_LOGIN, "Port Login"},
+	{CTL_TASK_PORT_LOGOUT, "Port Logout"}
+};
+
+void
+ctl_scsi_tur(union ctl_io *io, ctl_tag_type tag_type, uint8_t control)
+{
+	struct ctl_scsiio *ctsio;
+	struct scsi_test_unit_ready *cdb;
+
+	ctl_scsi_zero_io(io);
+
+	io->io_hdr.io_type = CTL_IO_SCSI;
+	ctsio = &io->scsiio;
+	cdb = (struct scsi_test_unit_ready *)ctsio->cdb;
+
+	cdb->opcode = TEST_UNIT_READY;
+	cdb->control = control;
+	io->io_hdr.flags = CTL_FLAG_DATA_NONE;
+	ctsio->tag_type = tag_type;
+	ctsio->cdb_len = sizeof(*cdb);
+	ctsio->ext_data_len = 0;
+	ctsio->ext_data_ptr = NULL;
+	ctsio->ext_sg_entries = 0;
+	ctsio->ext_data_filled = 0;
+	ctsio->sense_len = SSD_FULL_SIZE;
+}
+
+void
+ctl_scsi_inquiry(union ctl_io *io, uint8_t *data_ptr, int32_t data_len,
+		 uint8_t byte2, uint8_t page_code, ctl_tag_type tag_type,
+		 uint8_t control)
+{
+	struct ctl_scsiio *ctsio;
+	struct scsi_inquiry *cdb;
+
+	ctl_scsi_zero_io(io);
+
+	io->io_hdr.io_type = CTL_IO_SCSI;
+	ctsio = &io->scsiio;
+	cdb = (struct scsi_inquiry *)ctsio->cdb;
+
+	cdb->opcode = INQUIRY;
+	cdb->byte2 = byte2;
+	cdb->page_code = page_code;
+	cdb->control = control;
+	scsi_ulto2b(data_len, cdb->length);
+	io->io_hdr.io_type = CTL_IO_SCSI;
+	io->io_hdr.flags = CTL_FLAG_DATA_IN;
+	ctsio->tag_type = tag_type;
+	ctsio->cdb_len = sizeof(*cdb);
+	ctsio->ext_data_len = data_len;
+	ctsio->ext_data_ptr = data_ptr;
+	ctsio->ext_sg_entries = 0;
+	ctsio->ext_data_filled = 0;
+	ctsio->sense_len = SSD_FULL_SIZE;
+}
+
+void
+ctl_scsi_request_sense(union ctl_io *io, uint8_t *data_ptr,
+		       int32_t data_len, uint8_t byte2, ctl_tag_type tag_type,
+		       uint8_t control)
+{
+	struct ctl_scsiio *ctsio;
+	struct scsi_request_sense *cdb;
+
+	ctl_scsi_zero_io(io);
+
+	io->io_hdr.io_type = CTL_IO_SCSI;
+	ctsio = &io->scsiio;
+	cdb = (struct scsi_request_sense *)ctsio->cdb;
+
+	cdb->opcode = REQUEST_SENSE;
+	cdb->byte2 = byte2;
+	cdb->control = control;
+	cdb->length = data_len;
+	io->io_hdr.io_type = CTL_IO_SCSI;
+	io->io_hdr.flags = CTL_FLAG_DATA_IN;
+	ctsio->tag_type = tag_type;
+	ctsio->cdb_len = sizeof(*cdb);
+	ctsio->ext_data_ptr = data_ptr;
+	ctsio->ext_data_len = data_len;
+	ctsio->ext_sg_entries = 0;
+	ctsio->ext_data_filled = 0;
+	ctsio->sense_len = SSD_FULL_SIZE;
+}
+
+void
+ctl_scsi_report_luns(union ctl_io *io, uint8_t *data_ptr, uint32_t data_len,
+		     uint8_t select_report, ctl_tag_type tag_type,
+		     uint8_t control)
+{
+	struct ctl_scsiio *ctsio;
+	struct scsi_report_luns *cdb;
+
+	ctl_scsi_zero_io(io);
+
+	io->io_hdr.io_type = CTL_IO_SCSI;
+	ctsio = &io->scsiio;
+	cdb = (struct scsi_report_luns *)ctsio->cdb;
+
+	cdb->opcode = REPORT_LUNS;
+	cdb->select_report = select_report;
+	scsi_ulto4b(data_len, cdb->length);
+	cdb->control = control;
+	io->io_hdr.io_type = CTL_IO_SCSI;
+	io->io_hdr.flags = CTL_FLAG_DATA_IN;
+	ctsio->tag_type = tag_type;
+	ctsio->cdb_len = sizeof(*cdb);
+	ctsio->ext_data_ptr = data_ptr;
+	ctsio->ext_data_len = data_len;
+	ctsio->ext_sg_entries = 0;
+	ctsio->ext_data_filled = 0;
+	ctsio->sense_len = SSD_FULL_SIZE;
+}
+
+void
+ctl_scsi_read_write_buffer(union ctl_io *io, uint8_t *data_ptr,
+			   uint32_t data_len, int read_buffer, uint8_t mode,
+			   uint8_t buffer_id, uint32_t buffer_offset,
+			   ctl_tag_type tag_type, uint8_t control)
+{
+	struct ctl_scsiio *ctsio;
+	struct scsi_write_buffer *cdb;
+
+	ctl_scsi_zero_io(io);
+
+	io->io_hdr.io_type = CTL_IO_SCSI;
+	ctsio = &io->scsiio;
+	cdb = (struct scsi_write_buffer *)ctsio->cdb;
+
+	if (read_buffer != 0)
+		cdb->opcode = READ_BUFFER;
+	else
+		cdb->opcode = WRITE_BUFFER;
+
+	cdb->byte2 = mode & RWB_MODE;
+	cdb->buffer_id = buffer_id;
+	scsi_ulto3b(buffer_offset, cdb->offset);
+	scsi_ulto3b(data_len, cdb->length);
+	cdb->control = control;
+	io->io_hdr.io_type = CTL_IO_SCSI;
+	if (read_buffer != 0)
+		io->io_hdr.flags = CTL_FLAG_DATA_IN;
+	else
+		io->io_hdr.flags = CTL_FLAG_DATA_OUT;
+	ctsio->tag_type = tag_type;
+	ctsio->cdb_len = sizeof(*cdb);
+	ctsio->ext_data_ptr = data_ptr;
+	ctsio->ext_data_len = data_len;
+	ctsio->ext_sg_entries = 0;
+	ctsio->ext_data_filled = 0;
+	ctsio->sense_len = SSD_FULL_SIZE;
+}
+
+void
+ctl_scsi_read_write(union ctl_io *io, uint8_t *data_ptr, uint32_t data_len,
+		    int read_op, uint8_t byte2, int minimum_cdb_size,
+		    uint64_t lba, uint32_t num_blocks, ctl_tag_type tag_type,
+		    uint8_t control)
+{
+	struct ctl_scsiio *ctsio;
+
+	ctl_scsi_zero_io(io);
+
+	io->io_hdr.io_type = CTL_IO_SCSI;
+	ctsio = &io->scsiio;
+
+	/*
+	 * Pick out the smallest CDB that will hold the user's request.
+	 * minimum_cdb_size allows cranking the CDB size up, even for
+	 * requests that would not normally need a large CDB.  This can be
+	 * useful for testing (e.g. to make sure READ_16 support works without
+	 * having an array larger than 2TB) and for compatibility -- e.g.
+	 * if your device doesn't support READ_6.  (ATAPI drives don't.)
+	 */
+	if ((minimum_cdb_size < 10)
+	 && ((lba & 0x1fffff) == lba)
+	 && ((num_blocks & 0xff) == num_blocks)
+	 && (byte2 == 0)) {
+		struct scsi_rw_6 *cdb;
+
+		/*
+		 * Note that according to SBC-2, the target should return 256
+		 * blocks if the transfer length in a READ(6) or WRITE(6) CDB
+		 * is set to 0.  Since it's possible that some targets
+		 * won't do the right thing, we only send a READ(6) or
+		 * WRITE(6) for transfer sizes up to and including 255 blocks.
+		 */
+		cdb = (struct scsi_rw_6 *)ctsio->cdb;
+
+		cdb->opcode = (read_op) ? READ_6 : WRITE_6;
+		scsi_ulto3b(lba, cdb->addr);
+		cdb->length = num_blocks & 0xff;
+		cdb->control = control;
+
+		ctsio->cdb_len = sizeof(*cdb);
+
+	} else if ((minimum_cdb_size < 12)
+		&& ((num_blocks & 0xffff) == num_blocks)
+		&& ((lba & 0xffffffff) == lba)) {
+		struct scsi_rw_10 *cdb;
+
+		cdb = (struct scsi_rw_10 *)ctsio->cdb;
+
+		cdb->opcode = (read_op) ? READ_10 : WRITE_10;
+		cdb->byte2 = byte2;
+		scsi_ulto4b(lba, cdb->addr);
+		cdb->reserved = 0;
+		scsi_ulto2b(num_blocks, cdb->length);
+		cdb->control = control;
+
+		ctsio->cdb_len = sizeof(*cdb);
+	} else if ((minimum_cdb_size < 16)
+		&& ((num_blocks & 0xffffffff) == num_blocks)
+		&& ((lba & 0xffffffff) == lba)) {
+		struct scsi_rw_12 *cdb;
+
+		cdb = (struct scsi_rw_12 *)ctsio->cdb;
+
+		cdb->opcode = (read_op) ? READ_12 : WRITE_12;
+		cdb->byte2 = byte2;
+		scsi_ulto4b(lba, cdb->addr);
+		scsi_ulto4b(num_blocks, cdb->length);
+		cdb->reserved = 0;
+		cdb->control = control;
+
+		ctsio->cdb_len = sizeof(*cdb);
+	} else {
+		struct scsi_rw_16 *cdb;
+
+		cdb = (struct scsi_rw_16 *)ctsio->cdb;
+
+		cdb->opcode = (read_op) ? READ_16 : WRITE_16;
+		cdb->byte2 = byte2;
+		scsi_u64to8b(lba, cdb->addr);
+		scsi_ulto4b(num_blocks, cdb->length);
+		cdb->reserved = 0;
+		cdb->control = control;
+
+		ctsio->cdb_len = sizeof(*cdb);
+	}
+
+	io->io_hdr.io_type = CTL_IO_SCSI;
+	if (read_op != 0)
+		io->io_hdr.flags = CTL_FLAG_DATA_IN;
+	else
+		io->io_hdr.flags = CTL_FLAG_DATA_OUT;
+	ctsio->tag_type = tag_type;
+	ctsio->ext_data_ptr = data_ptr;
+	ctsio->ext_data_len = data_len;
+	ctsio->ext_sg_entries = 0;
+	ctsio->ext_data_filled = 0;
+	ctsio->sense_len = SSD_FULL_SIZE;
+}
+
+void
+ctl_scsi_read_capacity(union ctl_io *io, uint8_t *data_ptr, uint32_t data_len,
+		       uint32_t addr, int reladr, int pmi,
+		       ctl_tag_type tag_type, uint8_t control)
+{
+	struct scsi_read_capacity *cdb;
+
+	ctl_scsi_zero_io(io);
+
+	io->io_hdr.io_type = CTL_IO_SCSI;
+	cdb = (struct scsi_read_capacity *)io->scsiio.cdb;
+
+	cdb->opcode = READ_CAPACITY;
+	if (reladr)
+		cdb->byte2 = SRC_RELADR;
+	if (pmi)
+		cdb->pmi = SRC_PMI;
+	scsi_ulto4b(addr, cdb->addr);
+	cdb->control = control;
+	io->io_hdr.io_type = CTL_IO_SCSI;
+	io->io_hdr.flags = CTL_FLAG_DATA_IN;
+	io->scsiio.tag_type = tag_type;
+	io->scsiio.ext_data_ptr = data_ptr;
+	io->scsiio.ext_data_len = data_len;
+	io->scsiio.ext_sg_entries = 0;
+	io->scsiio.ext_data_filled = 0;
+	io->scsiio.sense_len = SSD_FULL_SIZE;
+}
+
+void
+ctl_scsi_read_capacity_16(union ctl_io *io, uint8_t *data_ptr,
+			  uint32_t data_len, uint64_t addr, int reladr,
+			  int pmi, ctl_tag_type tag_type, uint8_t control)
+{
+	struct scsi_read_capacity_16 *cdb;
+
+	ctl_scsi_zero_io(io);
+
+	io->io_hdr.io_type = CTL_IO_SCSI;
+	cdb = (struct scsi_read_capacity_16 *)io->scsiio.cdb;
+
+	cdb->opcode = SERVICE_ACTION_IN;
+	cdb->service_action = SRC16_SERVICE_ACTION;
+	if (reladr)
+		cdb->reladr |= SRC16_RELADR;
+	if (pmi)
+		cdb->reladr |= SRC16_PMI;
+	scsi_u64to8b(addr, cdb->addr);
+	scsi_ulto4b(data_len, cdb->alloc_len);
+	cdb->control = control;
+
+	io->io_hdr.io_type = CTL_IO_SCSI;
+	io->io_hdr.flags = CTL_FLAG_DATA_IN;
+	io->scsiio.tag_type = tag_type;
+	io->scsiio.ext_data_ptr = data_ptr;
+	io->scsiio.ext_data_len = data_len;
+	io->scsiio.ext_sg_entries = 0;
+	io->scsiio.ext_data_filled = 0;
+	io->scsiio.sense_len = SSD_FULL_SIZE;
+}
+
+void
+ctl_scsi_mode_sense(union ctl_io *io, uint8_t *data_ptr, uint32_t data_len, 
+		    int dbd, int llbaa, uint8_t page_code, uint8_t pc,
+		    uint8_t subpage, int minimum_cdb_size,
+		    ctl_tag_type tag_type, uint8_t control)
+{
+	ctl_scsi_zero_io(io);
+
+	if ((minimum_cdb_size < 10)
+	 && (llbaa == 0)
+	 && (data_len < 256)) {
+		struct scsi_mode_sense_6 *cdb;
+
+		cdb = (struct scsi_mode_sense_6 *)io->scsiio.cdb;
+
+		cdb->opcode = MODE_SENSE_6;
+		if (dbd)
+			cdb->byte2 |= SMS_DBD;
+		cdb->page = page_code | pc;
+		cdb->subpage = subpage;
+		cdb->length = data_len;
+		cdb->control = control;
+	} else {
+		struct scsi_mode_sense_10 *cdb;
+
+		cdb = (struct scsi_mode_sense_10 *)io->scsiio.cdb;
+
+		cdb->opcode = MODE_SENSE_10;
+		if (dbd)
+			cdb->byte2 |= SMS_DBD;
+		if (llbaa)
+			cdb->byte2 |= SMS10_LLBAA;
+		cdb->page = page_code | pc;
+		cdb->subpage = subpage;
+		scsi_ulto2b(data_len, cdb->length);
+		cdb->control = control;
+	}
+
+	io->io_hdr.io_type = CTL_IO_SCSI;
+	io->io_hdr.flags = CTL_FLAG_DATA_IN;
+	io->scsiio.tag_type = tag_type;
+	io->scsiio.ext_data_ptr = data_ptr;
+	io->scsiio.ext_data_len = data_len;
+	io->scsiio.ext_sg_entries = 0;
+	io->scsiio.ext_data_filled = 0;
+	io->scsiio.sense_len = SSD_FULL_SIZE;
+}
+
+void
+ctl_scsi_start_stop(union ctl_io *io, int start, int load_eject, int immediate,
+		    int power_conditions, int onoffline __unused,
+		    ctl_tag_type tag_type, uint8_t control)
+{
+	struct scsi_start_stop_unit *cdb;
+
+	cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb;
+
+	ctl_scsi_zero_io(io);
+
+	cdb->opcode = START_STOP_UNIT;
+	if (immediate)
+		cdb->byte2 |= SSS_IMMED;
+#ifdef NEEDTOPORT
+	if (onoffline)
+		cdb->byte2 |= SSS_ONOFFLINE;
+#endif
+	cdb->how = power_conditions;
+	if (load_eject)
+		cdb->how |= SSS_LOEJ;
+	if (start)
+		cdb->how |= SSS_START;
+	cdb->control = control;
+	io->io_hdr.io_type = CTL_IO_SCSI;
+	io->io_hdr.flags = CTL_FLAG_DATA_NONE;
+	io->scsiio.tag_type = tag_type;
+	io->scsiio.ext_data_ptr = NULL;
+	io->scsiio.ext_data_len = 0;
+	io->scsiio.ext_sg_entries = 0;
+	io->scsiio.ext_data_filled = 0;
+	io->scsiio.sense_len = SSD_FULL_SIZE;
+}
+
+void
+ctl_scsi_sync_cache(union ctl_io *io, int immed, int reladr,
+		    int minimum_cdb_size, uint64_t starting_lba,
+		    uint32_t block_count, ctl_tag_type tag_type,
+		    uint8_t control)
+{
+	ctl_scsi_zero_io(io);
+
+	if ((minimum_cdb_size < 16)
+	 && ((block_count & 0xffff) == block_count)
+	 && ((starting_lba & 0xffffffff) == starting_lba)) {
+		struct scsi_sync_cache *cdb;
+
+		cdb = (struct scsi_sync_cache *)io->scsiio.cdb;
+
+		cdb->opcode = SYNCHRONIZE_CACHE;
+		if (reladr)
+			cdb->byte2 |= SSC_RELADR;
+
+		if (immed)
+			cdb->byte2 |= SSC_IMMED;
+
+		scsi_ulto4b(starting_lba, cdb->begin_lba);
+		scsi_ulto2b(block_count, cdb->lb_count);
+		cdb->control = control;
+	} else {
+		struct scsi_sync_cache_16 *cdb;
+
+		cdb = (struct scsi_sync_cache_16 *)io->scsiio.cdb;
+
+		cdb->opcode = SYNCHRONIZE_CACHE_16;
+		if (reladr)
+			cdb->byte2 |= SSC_RELADR;
+
+		if (immed)
+			cdb->byte2 |= SSC_IMMED;
+
+		scsi_u64to8b(starting_lba, cdb->begin_lba);
+		scsi_ulto4b(block_count, cdb->lb_count);
+		cdb->control = control;
+	}
+	io->io_hdr.io_type = CTL_IO_SCSI;
+	io->io_hdr.flags = CTL_FLAG_DATA_NONE;
+	io->scsiio.tag_type = tag_type;
+	io->scsiio.ext_data_ptr = NULL;
+	io->scsiio.ext_data_len = 0;
+	io->scsiio.ext_sg_entries = 0;
+	io->scsiio.ext_data_filled = 0;
+	io->scsiio.sense_len = SSD_FULL_SIZE;
+}
+
+void
+ctl_scsi_persistent_res_in(union ctl_io *io, uint8_t *data_ptr,
+			   uint32_t data_len, int action,
+			   ctl_tag_type tag_type, uint8_t control)
+{
+
+	struct scsi_per_res_in *cdb;
+
+	ctl_scsi_zero_io(io);
+
+	cdb = (struct scsi_per_res_in *)io->scsiio.cdb;
+	cdb->opcode = PERSISTENT_RES_IN;
+	cdb->action = action;
+	scsi_ulto2b(data_len, cdb->length);
+	cdb->control = control;
+
+	io->io_hdr.io_type = CTL_IO_SCSI;
+	io->io_hdr.flags = CTL_FLAG_DATA_IN;
+	io->scsiio.tag_type = tag_type;
+	io->scsiio.ext_data_ptr = data_ptr;
+	io->scsiio.ext_data_len = data_len;
+	io->scsiio.ext_sg_entries = 0;
+	io->scsiio.ext_data_filled = 0;
+	io->scsiio.sense_len = SSD_FULL_SIZE;
+}
+
+void
+ctl_scsi_persistent_res_out(union ctl_io *io, uint8_t *data_ptr,
+			    uint32_t data_len, int action, int type,
+			    uint64_t key, uint64_t sa_key,
+			    ctl_tag_type tag_type, uint8_t control)
+{
+
+	struct scsi_per_res_out *cdb;
+	struct scsi_per_res_out_parms *params;
+
+	ctl_scsi_zero_io(io);
+
+	cdb = (struct scsi_per_res_out *)io->scsiio.cdb;
+	params = (struct scsi_per_res_out_parms *)data_ptr;
+
+	cdb->opcode = PERSISTENT_RES_OUT;
+	if (action == 5)
+	    cdb->action = 6;
+	else
+	    cdb->action = action;
+	switch(type)
+	{
+	    case 0:
+		    cdb->scope_type = 1;
+			break;
+	    case 1:
+		    cdb->scope_type = 3;
+			break;
+	    case 2:
+		    cdb->scope_type = 5;
+			break;
+	    case 3:
+		    cdb->scope_type = 6;
+			break;
+	    case 4:
+		    cdb->scope_type = 7;
+			break;
+	    case 5:
+		    cdb->scope_type = 8;
+			break;
+	}
+	scsi_ulto4b(data_len, cdb->length);
+	cdb->control = control;
+
+	scsi_u64to8b(key, params->res_key.key);
+	scsi_u64to8b(sa_key, params->serv_act_res_key);
+
+	io->io_hdr.io_type = CTL_IO_SCSI;
+	io->io_hdr.flags = CTL_FLAG_DATA_OUT;
+	io->scsiio.tag_type = tag_type;
+	io->scsiio.ext_data_ptr = data_ptr;
+	io->scsiio.ext_data_len = data_len;
+	io->scsiio.ext_sg_entries = 0;
+	io->scsiio.ext_data_filled = 0;
+	io->scsiio.sense_len = SSD_FULL_SIZE;
+
+}
+
+void
+ctl_scsi_maintenance_in(union ctl_io *io, uint8_t *data_ptr, uint32_t data_len, 
+			uint8_t action, ctl_tag_type tag_type, uint8_t control)
+{
+	struct scsi_maintenance_in *cdb;
+
+	ctl_scsi_zero_io(io);
+
+	cdb = (struct scsi_maintenance_in *)io->scsiio.cdb;
+	cdb->opcode = MAINTENANCE_IN;
+	cdb->byte2 = action;
+	scsi_ulto4b(data_len, cdb->length);
+	cdb->control = control;
+
+	io->io_hdr.io_type = CTL_IO_SCSI;
+	io->io_hdr.flags = CTL_FLAG_DATA_IN;
+	io->scsiio.tag_type = tag_type;
+	io->scsiio.ext_data_ptr = data_ptr;
+	io->scsiio.ext_data_len = data_len;
+	io->scsiio.ext_sg_entries = 0;
+	io->scsiio.ext_data_filled = 0;
+	io->scsiio.sense_len = SSD_FULL_SIZE;
+}
+
+#ifndef _KERNEL
+union ctl_io *
+ctl_scsi_alloc_io(struct ctl_id initid)
+{
+	union ctl_io *io;
+
+	io = (union ctl_io *)malloc(sizeof(*io));
+	if (io == NULL)
+		goto bailout;
+
+	io->io_hdr.nexus.initid = initid;
+
+bailout:
+	return (io);
+}
+
+void
+ctl_scsi_free_io(union ctl_io *io)
+{
+	free(io);
+}
+
+#endif /* !_KERNEL */
+void
+ctl_scsi_zero_io(union ctl_io *io)
+{
+	void *pool_ref;
+
+	if (io == NULL)
+		return;
+
+	pool_ref = io->io_hdr.pool;
+
+	memset(io, 0, sizeof(*io));
+
+	io->io_hdr.pool = pool_ref;
+}
+
+const char *
+ctl_scsi_task_string(struct ctl_taskio *taskio)
+{
+	unsigned int i;
+
+	for (i = 0; i < (sizeof(ctl_task_table)/sizeof(ctl_task_table[0]));
+	     i++) {
+		if (taskio->task_action == ctl_task_table[i].task_action) {
+			return (ctl_task_table[i].description);
+		}
+	}
+
+	return (NULL);
+}
+
+void
+ctl_io_error_sbuf(union ctl_io *io, struct scsi_inquiry_data *inq_data,
+		  struct sbuf *sb)
+{
+	struct ctl_status_desc *status_desc;
+	char path_str[64];
+	unsigned int i;
+
+	status_desc = NULL;
+
+	for (i = 0; i < (sizeof(ctl_status_table)/sizeof(ctl_status_table[0]));
+	     i++) {
+		if ((io->io_hdr.status & CTL_STATUS_MASK) ==
+		     ctl_status_table[i].status) {
+			status_desc = &ctl_status_table[i];
+			break;
+		}
+	}
+
+	ctl_scsi_path_string(io, path_str, sizeof(path_str));
+
+	switch (io->io_hdr.io_type) {
+	case CTL_IO_SCSI:
+		sbuf_cat(sb, path_str);
+
+		ctl_scsi_command_string(&io->scsiio, NULL, sb);
+
+		sbuf_printf(sb, "\n");
+
+		sbuf_printf(sb, "%sTag: 0x%04x, Type: %d\n", path_str,
+			    io->scsiio.tag_num, io->scsiio.tag_type);
+		break;
+	case CTL_IO_TASK: {
+		const char *task_desc;
+
+		sbuf_cat(sb, path_str);
+
+		task_desc = ctl_scsi_task_string(&io->taskio);
+
+		if (task_desc == NULL)
+			sbuf_printf(sb, "Unknown Task Action %d (%#x)",
+				    io->taskio.task_action,
+				    io->taskio.task_action);
+		else
+			sbuf_printf(sb, "Task Action: %s", task_desc);
+
+		sbuf_printf(sb, "\n");
+
+		switch (io->taskio.task_action) {
+		case CTL_TASK_ABORT_TASK:
+		case CTL_TASK_ABORT_TASK_SET:
+		case CTL_TASK_CLEAR_TASK_SET:
+			sbuf_printf(sb, "%sTag: 0x%04x, Type: %d\n", path_str,
+				    io->taskio.tag_num,
+				    io->taskio.tag_type);
+			break;
+		default:
+			break;
+		}
+		break;
+	}
+	default:
+		break;
+	}
+
+	sbuf_cat(sb, path_str);
+	if (status_desc == NULL)
+		sbuf_printf(sb, "CTL Status: Unknown status %#x\n",
+			    io->io_hdr.status);
+	else
+		sbuf_printf(sb, "CTL Status: %s\n", status_desc->description);
+
+	if ((io->io_hdr.io_type == CTL_IO_SCSI)
+	 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR)) {
+		sbuf_cat(sb, path_str);
+		sbuf_printf(sb, "SCSI Status: %s\n",
+			    ctl_scsi_status_string(&io->scsiio));
+
+		if (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)
+			ctl_scsi_sense_sbuf(&io->scsiio, inq_data,
+					    sb, SSS_FLAG_NONE);
+	}
+}
+
+char *
+ctl_io_error_string(union ctl_io *io, struct scsi_inquiry_data *inq_data,
+		    char *str, int str_len)
+{
+	struct sbuf sb;
+
+	sbuf_new(&sb, str, str_len, SBUF_FIXEDLEN);
+
+	ctl_io_error_sbuf(io, inq_data, &sb);
+
+	sbuf_finish(&sb);
+
+	return (sbuf_data(&sb));
+}
+
+#ifdef _KERNEL
+
+void
+ctl_io_error_print(union ctl_io *io, struct scsi_inquiry_data *inq_data)
+{
+	char str[512];
+#ifdef NEEDTOPORT
+	char *message;
+	char *line;
+
+	message = io_error_string(io, inq_data, str, sizeof(str));
+
+	for (line = strsep(&message, "\n"); line != NULL;
+	     line = strsep(&message, "\n")) {
+		csevent_log(CSC_CTL | CSC_SHELF_SW | CTL_ERROR_REPORT,
+                            csevent_LogType_Trace,
+                            csevent_Severity_Information,
+                            csevent_AlertLevel_Green,
+                            csevent_FRU_Firmware,
+                            csevent_FRU_Unknown, "%s", line);
+	}
+#else
+	printf("%s", ctl_io_error_string(io, inq_data, str, sizeof(str)));
+#endif
+
+}
+
+#else /* _KERNEL */
+
+void
+ctl_io_error_print(union ctl_io *io, struct scsi_inquiry_data *inq_data,
+		   FILE *ofile)
+{
+	char str[512];
+
+	fprintf(ofile, "%s", ctl_io_error_string(io, inq_data, str,
+		sizeof(str)));
+}
+
+#endif /* _KERNEL */
+
+/*
+ * vim: ts=8
+ */
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/cam/ctl/ctl_util.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/sys/cam/ctl/ctl_util.h	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,119 @@
+/*-
+ * Copyright (c) 2003 Silicon Graphics International Corp.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions, and the following disclaimer,
+ *    without modification.
+ * 2. Redistributions in binary form must reproduce at minimum a disclaimer
+ *    substantially similar to the "NO WARRANTY" disclaimer below
+ *    ("Disclaimer") and any redistribution must be conditioned upon
+ *    including a substantially similar Disclaimer requirement for further
+ *    binary redistribution.
+ *
+ * NO WARRANTY
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGES.
+ *
+ * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_util.h#2 $
+ * $FreeBSD: head/sys/cam/ctl/ctl_util.h 229997 2012-01-12 00:34:33Z ken $
+ */
+/*
+ * CAM Target Layer SCSI library interface
+ *
+ * Author: Ken Merry <ken at FreeBSD.org>
+ */
+
+#ifndef	_CTL_UTIL_H
+#define	_CTL_UTIL_H 1
+
+__BEGIN_DECLS
+
+void ctl_scsi_tur(union ctl_io *io, ctl_tag_type tag_type, uint8_t control);
+void ctl_scsi_inquiry(union ctl_io *io, uint8_t *data_ptr, int32_t data_len,
+		      uint8_t byte2, uint8_t page_code, ctl_tag_type tag_type,
+		      uint8_t control);
+void ctl_scsi_request_sense(union ctl_io *io, uint8_t *data_ptr,
+			    int32_t data_len, uint8_t byte2,
+			    ctl_tag_type tag_type, uint8_t control);
+void ctl_scsi_report_luns(union ctl_io *io, uint8_t *data_ptr,
+			  uint32_t data_len, uint8_t select_report,
+			  ctl_tag_type tag_type, uint8_t control);
+void ctl_scsi_read_write_buffer(union ctl_io *io, uint8_t *data_ptr,
+				uint32_t data_len, int read_buffer,
+				uint8_t mode, uint8_t buffer_id,
+				uint32_t buffer_offset, ctl_tag_type tag_type,
+				uint8_t control);
+void ctl_scsi_read_write(union ctl_io *io, uint8_t *data_ptr,
+			 uint32_t data_len, int read_op, uint8_t byte2,
+			 int minimum_cdb_size, uint64_t lba,
+			 uint32_t num_blocks, ctl_tag_type tag_type,
+			 uint8_t control);
+void ctl_scsi_read_capacity(union ctl_io *io, uint8_t *data_ptr,
+			    uint32_t data_len, uint32_t addr, int reladr,
+			    int pmi, ctl_tag_type tag_type, uint8_t control);
+void ctl_scsi_read_capacity_16(union ctl_io *io, uint8_t *data_ptr,
+			       uint32_t data_len, uint64_t addr, int reladr,
+			       int pmi, ctl_tag_type tag_type, uint8_t control);
+void ctl_scsi_mode_sense(union ctl_io *io, uint8_t *data_ptr,
+			 uint32_t data_len, int dbd, int llbaa,
+			 uint8_t page_code, uint8_t pc, uint8_t subpage,
+			 int minimum_cdb_size, ctl_tag_type tag_type,
+			 uint8_t control);
+void ctl_scsi_start_stop(union ctl_io *io, int start, int load_eject,
+			 int immediate, int power_conditions, int onoffline,
+			 ctl_tag_type tag_type, uint8_t control);
+void ctl_scsi_sync_cache(union ctl_io *io, int immed, int reladr,
+			 int minimum_cdb_size, uint64_t starting_lba,
+			 uint32_t block_count, ctl_tag_type tag_type,
+			 uint8_t control);
+void ctl_scsi_persistent_res_in(union ctl_io *io, uint8_t *data_ptr, 
+				uint32_t data_len, int action,
+				ctl_tag_type tag_type, uint8_t control);
+void ctl_scsi_persistent_res_out(union ctl_io *io, uint8_t *data_ptr, 
+				 uint32_t data_len, int action, int type,
+				 uint64_t key, uint64_t sa_key,
+				 ctl_tag_type tag_type, uint8_t control);
+void ctl_scsi_maintenance_in(union ctl_io *io, uint8_t *data_ptr, 
+			     uint32_t data_len, uint8_t action, 
+			     ctl_tag_type tag_type, uint8_t control);
+#ifndef _KERNEL
+union ctl_io *ctl_scsi_alloc_io(struct ctl_id initid);
+void ctl_scsi_free_io(union ctl_io *io);
+#endif /* !_KERNEL */
+void ctl_scsi_zero_io(union ctl_io *io);
+const char *ctl_scsi_task_string(struct ctl_taskio *taskio);
+void ctl_io_error_sbuf(union ctl_io *io,
+		       struct scsi_inquiry_data *inq_data, struct sbuf *sb);
+char *ctl_io_error_string(union ctl_io *io,
+			  struct scsi_inquiry_data *inq_data, char *str,
+			  int str_len);
+#ifdef _KERNEL
+
+void ctl_io_error_print(union ctl_io *io, struct scsi_inquiry_data *inq_data);
+#else /* _KERNEL */
+void
+ctl_io_error_print(union ctl_io *io, struct scsi_inquiry_data *inq_data,
+		   FILE *ofile);
+
+#endif /* _KERNEL */
+
+__END_DECLS
+
+#endif	/* _CTL_UTIL_H */
+
+/*
+ * vim: ts=8
+ */
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/cam/ctl/scsi_ctl.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/sys/cam/ctl/scsi_ctl.c	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,2049 @@
+/*-
+ * Copyright (c) 2008, 2009 Silicon Graphics International Corp.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions, and the following disclaimer,
+ *    without modification.
+ * 2. Redistributions in binary form must reproduce at minimum a disclaimer
+ *    substantially similar to the "NO WARRANTY" disclaimer below
+ *    ("Disclaimer") and any redistribution must be conditioned upon
+ *    including a substantially similar Disclaimer requirement for further
+ *    binary redistribution.
+ *
+ * NO WARRANTY
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGES.
+ *
+ * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/scsi_ctl.c#4 $
+ */
+/*
+ * Peripheral driver interface between CAM and CTL (CAM Target Layer).
+ *
+ * Author: Ken Merry <ken at FreeBSD.org>
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD: head/sys/cam/ctl/scsi_ctl.c 229997 2012-01-12 00:34:33Z ken $");
+
+#include <sys/param.h>
+#include <sys/queue.h>
+#include <sys/systm.h>
+#include <sys/kernel.h>
+#include <sys/lock.h>
+#include <sys/mutex.h>
+#include <sys/condvar.h>
+#include <sys/malloc.h>
+#include <sys/bus.h>
+#include <sys/endian.h>
+#include <sys/sbuf.h>
+#include <sys/sysctl.h>
+#include <sys/types.h>
+#include <sys/systm.h>
+#include <machine/bus.h>
+
+#include <cam/cam.h>
+#include <cam/cam_ccb.h>
+#include <cam/cam_periph.h>
+#include <cam/cam_queue.h>
+#include <cam/cam_xpt_periph.h>
+#include <cam/cam_debug.h>
+#include <cam/cam_sim.h>
+#include <cam/cam_xpt.h>
+
+#include <cam/scsi/scsi_all.h>
+#include <cam/scsi/scsi_message.h>
+
+#include <cam/ctl/ctl_io.h>
+#include <cam/ctl/ctl.h>
+#include <cam/ctl/ctl_frontend.h>
+#include <cam/ctl/ctl_util.h>
+#include <cam/ctl/ctl_error.h>
+
+typedef enum {
+	CTLFE_CCB_WAITING 	= 0x01
+} ctlfe_ccb_types;
+
+struct ctlfe_softc {
+	struct ctl_frontend fe;
+	path_id_t path_id;
+	struct cam_sim *sim;
+	char port_name[DEV_IDLEN];
+	STAILQ_HEAD(, ctlfe_lun_softc) lun_softc_list;
+	STAILQ_ENTRY(ctlfe_softc) links;
+};
+
+STAILQ_HEAD(, ctlfe_softc) ctlfe_softc_list;
+struct mtx ctlfe_list_mtx;
+static char ctlfe_mtx_desc[] = "ctlfelist";
+static int ctlfe_dma_enabled = 1;
+#ifdef CTLFE_INIT_ENABLE
+static int ctlfe_max_targets = 1;
+static int ctlfe_num_targets = 0;
+#endif
+
+typedef enum {
+	CTLFE_LUN_NONE		= 0x00,
+	CTLFE_LUN_WILDCARD	= 0x01
+} ctlfe_lun_flags;
+
+struct ctlfe_lun_softc {
+	struct ctlfe_softc *parent_softc;
+	struct cam_periph *periph;
+	ctlfe_lun_flags flags;
+	struct callout dma_callout;
+	uint64_t ccbs_alloced;
+	uint64_t ccbs_freed;
+	uint64_t ctios_sent;
+	uint64_t ctios_returned;
+	uint64_t atios_sent;
+	uint64_t atios_returned;
+	uint64_t inots_sent;
+	uint64_t inots_returned;
+	/* bus_dma_tag_t dma_tag; */
+	TAILQ_HEAD(, ccb_hdr) work_queue;
+	STAILQ_ENTRY(ctlfe_lun_softc) links;
+};
+
+typedef enum {
+	CTLFE_CMD_NONE		= 0x00,
+	CTLFE_CMD_PIECEWISE	= 0x01
+} ctlfe_cmd_flags;
+
+/*
+ * The size limit of this structure is CTL_PORT_PRIV_SIZE, from ctl_io.h.
+ * Currently that is 600 bytes.
+ */
+struct ctlfe_lun_cmd_info {
+	int cur_transfer_index;
+	ctlfe_cmd_flags flags;
+	/*
+	 * XXX KDM struct bus_dma_segment is 8 bytes on i386, and 16
+	 * bytes on amd64.  So with 32 elements, this is 256 bytes on
+	 * i386 and 512 bytes on amd64.
+	 */
+	bus_dma_segment_t cam_sglist[32];
+};
+
+/*
+ * When we register the adapter/bus, request that this many ctl_ios be
+ * allocated.  This should be the maximum supported by the adapter, but we
+ * currently don't have a way to get that back from the path inquiry.
+ * XXX KDM add that to the path inquiry.
+ */
+#define	CTLFE_REQ_CTL_IO	4096
+/*
+ * Number of Accept Target I/O CCBs to allocate and queue down to the
+ * adapter per LUN.
+ * XXX KDM should this be controlled by CTL?
+ */
+#define	CTLFE_ATIO_PER_LUN	1024
+/*
+ * Number of Immediate Notify CCBs (used for aborts, resets, etc.) to
+ * allocate and queue down to the adapter per LUN.
+ * XXX KDM should this be controlled by CTL?
+ */
+#define	CTLFE_IN_PER_LUN	1024
+
+/*
+ * Timeout (in seconds) on CTIO CCB allocation for doing a DMA or sending
+ * status to the initiator.  The SIM is expected to have its own timeouts,
+ * so we're not putting this timeout around the CCB execution time.  The
+ * SIM should timeout and let us know if it has an issue.
+ */
+#define	CTLFE_DMA_TIMEOUT	60
+
+/*
+ * Turn this on to enable extra debugging prints.
+ */
+#if 0
+#define	CTLFE_DEBUG
+#endif
+
+/*
+ * Use randomly assigned WWNN/WWPN values.  This is to work around an issue
+ * in the FreeBSD initiator that makes it unable to rescan the target if
+ * the target gets rebooted and the WWNN/WWPN stay the same.
+ */
+#if 0
+#define	RANDOM_WWNN
+#endif
+
+SYSCTL_INT(_kern_cam_ctl, OID_AUTO, dma_enabled, CTLFLAG_RW,
+	   &ctlfe_dma_enabled, 0, "DMA enabled");
+MALLOC_DEFINE(M_CTLFE, "CAM CTL FE", "CAM CTL FE interface");
+
+#define	ccb_type	ppriv_field0
+/* This is only used in the ATIO */
+#define	io_ptr		ppriv_ptr1
+
+/* This is only used in the CTIO */
+#define	ccb_atio	ppriv_ptr1
+
+int			ctlfeinitialize(void);
+void			ctlfeshutdown(void);
+static periph_init_t	ctlfeinit;
+static void		ctlfeasync(void *callback_arg, uint32_t code,
+				   struct cam_path *path, void *arg);
+static periph_ctor_t	ctlferegister;
+static periph_oninv_t	ctlfeoninvalidate;
+static periph_dtor_t	ctlfecleanup;
+static periph_start_t	ctlfestart;
+static void		ctlfedone(struct cam_periph *periph,
+				  union ccb *done_ccb);
+
+static void 		ctlfe_onoffline(void *arg, int online);
+static void 		ctlfe_online(void *arg);
+static void 		ctlfe_offline(void *arg);
+static int 		ctlfe_targ_enable(void *arg, struct ctl_id targ_id);
+static int 		ctlfe_targ_disable(void *arg, struct ctl_id targ_id);
+static int 		ctlfe_lun_enable(void *arg, struct ctl_id targ_id,
+					 int lun_id);
+static int 		ctlfe_lun_disable(void *arg, struct ctl_id targ_id,
+					  int lun_id);
+static void		ctlfe_dump_sim(struct cam_sim *sim);
+static void		ctlfe_dump_queue(struct ctlfe_lun_softc *softc);
+static void		ctlfe_dma_timeout(void *arg);
+static void 		ctlfe_datamove_done(union ctl_io *io);
+static void 		ctlfe_dump(void);
+
+static struct periph_driver ctlfe_driver =
+{
+	ctlfeinit, "ctl",
+	TAILQ_HEAD_INITIALIZER(ctlfe_driver.units), /*generation*/ 0
+};
+PERIPHDRIVER_DECLARE(ctl, ctlfe_driver);
+
+extern struct ctl_softc *control_softc;
+
+int
+ctlfeinitialize(void)
+{
+	cam_status status;
+
+	STAILQ_INIT(&ctlfe_softc_list);
+
+	mtx_init(&ctlfe_list_mtx, ctlfe_mtx_desc, NULL, MTX_DEF);
+
+	xpt_lock_buses();
+	periphdriver_register(&ctlfe_driver);
+	xpt_unlock_buses();
+
+	status = xpt_register_async(AC_PATH_REGISTERED | AC_PATH_DEREGISTERED | 
+				    AC_CONTRACT, ctlfeasync, NULL, NULL);
+
+	if (status != CAM_REQ_CMP) {
+		printf("ctl: Failed to attach async callback due to CAM "
+		       "status 0x%x!\n", status);
+	}
+
+	return (0);
+}
+
+void
+ctlfeshutdown(void)
+{
+	return;
+}
+
+void
+ctlfeinit(void)
+{
+	cam_status status;
+
+	STAILQ_INIT(&ctlfe_softc_list);
+
+	mtx_init(&ctlfe_list_mtx, ctlfe_mtx_desc, NULL, MTX_DEF);
+
+	KASSERT(control_softc != NULL, ("CTL is not initialized!"));
+
+	status = xpt_register_async(AC_PATH_REGISTERED | AC_PATH_DEREGISTERED |
+				    AC_CONTRACT, ctlfeasync, NULL, NULL);
+
+	if (status != CAM_REQ_CMP) {
+		printf("ctl: Failed to attach async callback due to CAM "
+		       "status 0x%x!\n", status);
+	}
+}
+
+static void
+ctlfeasync(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
+{
+
+#ifdef CTLFEDEBUG
+	printf("%s: entered\n", __func__);
+#endif
+
+	/*
+	 * When a new path gets registered, and it is capable of target
+	 * mode, go ahead and attach.  Later on, we may need to be more
+	 * selective, but for now this will be sufficient.
+ 	 */
+	switch (code) {
+	case AC_PATH_REGISTERED: {
+		struct ctl_frontend *fe;
+		struct ctlfe_softc *bus_softc;
+		struct ctlfe_lun_softc *lun_softc;
+		struct cam_path *path;
+		struct ccb_pathinq *cpi;
+		cam_status status;
+		int retval;
+
+		cpi = (struct ccb_pathinq *)arg;
+
+		/* Don't attach if it doesn't support target mode */
+		if ((cpi->target_sprt & PIT_PROCESSOR) == 0) {
+			printf("%s: SIM %s%d doesn't support target mode\n",
+			       __func__, cpi->dev_name, cpi->unit_number);
+			break;
+		}
+
+#ifdef CTLFE_INIT_ENABLE
+		if (ctlfe_num_targets >= ctlfe_max_targets) {
+			union ccb *ccb;
+			struct cam_sim *sim;
+
+			ccb = (union ccb *)malloc(sizeof(*ccb), M_TEMP,
+						  M_NOWAIT | M_ZERO);
+			if (ccb == NULL) {
+				printf("%s: unable to malloc CCB!\n", __func__);
+				xpt_free_path(path);
+				return;
+			}
+			xpt_setup_ccb(&ccb->ccb_h, cpi->ccb_h.path,
+				      /*priority*/ 1);
+
+			sim = xpt_path_sim(cpi->ccb_h.path);
+
+			ccb->ccb_h.func_code = XPT_SET_SIM_KNOB;
+			ccb->knob.xport_specific.valid = KNOB_VALID_ROLE;
+			ccb->knob.xport_specific.fc.role = KNOB_ROLE_INITIATOR;
+
+			/* We should hold the SIM lock here */
+			mtx_assert(sim->mtx, MA_OWNED);
+
+			xpt_action(ccb);
+
+			if ((ccb->ccb_h.status & CAM_STATUS_MASK) !=
+			     CAM_REQ_CMP) {
+				printf("%s: SIM %s%d (path id %d) initiator "
+				       "enable failed with status %#x\n",
+				       __func__, cpi->dev_name,
+				       cpi->unit_number, cpi->ccb_h.path_id,
+				       ccb->ccb_h.status);
+			} else {
+				printf("%s: SIM %s%d (path id %d) initiator "
+				       "enable succeeded\n",
+				       __func__, cpi->dev_name,
+				       cpi->unit_number, cpi->ccb_h.path_id);
+			}
+
+			free(ccb, M_TEMP);
+
+			break;
+		} else {
+			ctlfe_num_targets++;
+		}
+
+		printf("%s: ctlfe_num_targets = %d\n", __func__,
+		       ctlfe_num_targets);
+#endif /* CTLFE_INIT_ENABLE */
+
+		/*
+		 * We're in an interrupt context here, so we have to
+		 * use M_NOWAIT.  Of course this means trouble if we
+		 * can't allocate memory.
+		 */
+		bus_softc = malloc(sizeof(*bus_softc), M_CTLFE,
+				   M_NOWAIT | M_ZERO);
+		if (bus_softc == NULL) {
+			printf("%s: unable to malloc %zd bytes for softc\n",
+			       __func__, sizeof(*bus_softc));
+			return;
+		}
+
+		bus_softc->path_id = cpi->ccb_h.path_id;
+		bus_softc->sim = xpt_path_sim(cpi->ccb_h.path);
+		STAILQ_INIT(&bus_softc->lun_softc_list);
+
+		fe = &bus_softc->fe;
+
+		/*
+		 * XXX KDM should we be more accurate here ?
+		 */
+		if (cpi->transport == XPORT_FC)
+			fe->port_type = CTL_PORT_FC;
+		else
+			fe->port_type = CTL_PORT_SCSI;
+
+		/* XXX KDM what should the real number be here? */
+		fe->num_requested_ctl_io = 4096;
+		snprintf(bus_softc->port_name, sizeof(bus_softc->port_name),
+			 "%s%d", cpi->dev_name, cpi->unit_number);
+		/*
+		 * XXX KDM it would be nice to allocate storage in the
+		 * frontend structure itself.
+	 	 */
+		fe->port_name = bus_softc->port_name;
+		fe->physical_port = cpi->unit_number;
+		fe->virtual_port = cpi->bus_id;
+		fe->port_online = ctlfe_online;
+		fe->port_offline = ctlfe_offline;
+		fe->onoff_arg = bus_softc;
+		fe->targ_enable = ctlfe_targ_enable;
+		fe->targ_disable = ctlfe_targ_disable;
+		fe->lun_enable = ctlfe_lun_enable;
+		fe->lun_disable = ctlfe_lun_disable;
+		fe->targ_lun_arg = bus_softc;
+		fe->fe_datamove = ctlfe_datamove_done;
+		fe->fe_done = ctlfe_datamove_done;
+		fe->fe_dump = ctlfe_dump;
+		/*
+		 * XXX KDM the path inquiry doesn't give us the maximum
+		 * number of targets supported.
+		 */
+		fe->max_targets = cpi->max_target;
+		fe->max_target_id = cpi->max_target;
+		
+		/*
+		 * XXX KDM need to figure out whether we're the master or
+		 * slave.
+		 */
+		printf("%s: calling ctl_frontend_register() for %s%d\n",
+		       __func__, cpi->dev_name, cpi->unit_number);
+		retval = ctl_frontend_register(fe, /*master_SC*/ 1);
+		if (retval != 0) {
+			printf("%s: ctl_frontend_register() failed with "
+			       "error %d!\n", __func__, retval);
+			free(bus_softc, M_CTLFE);
+			break;
+		} else {
+			mtx_lock(&ctlfe_list_mtx);
+			STAILQ_INSERT_TAIL(&ctlfe_softc_list, bus_softc, links);
+			mtx_unlock(&ctlfe_list_mtx);
+		}
+
+		status = xpt_create_path(&path, /*periph*/ NULL,
+					 bus_softc->path_id,CAM_TARGET_WILDCARD,
+					 CAM_LUN_WILDCARD);
+		if (status != CAM_REQ_CMP) {
+			printf("%s: unable to create path for wildcard "
+			       "periph\n", __func__);
+			break;
+		}
+		lun_softc = malloc(sizeof(*lun_softc), M_CTLFE,
+				   M_NOWAIT | M_ZERO);
+		if (lun_softc == NULL) {
+			xpt_print(path, "%s: unable to allocate softc for "
+				  "wildcard periph\n", __func__);
+			xpt_free_path(path);
+			break;
+		}
+
+		lun_softc->parent_softc = bus_softc;
+		lun_softc->flags |= CTLFE_LUN_WILDCARD;
+
+		status = cam_periph_alloc(ctlferegister,
+					  ctlfeoninvalidate,
+					  ctlfecleanup,
+					  ctlfestart,
+					  "ctl",
+					  CAM_PERIPH_BIO,
+					  path,
+					  ctlfeasync,
+					  0,
+					  lun_softc);
+
+		xpt_free_path(path);
+
+		break;
+	}
+	case AC_PATH_DEREGISTERED:
+		/* ctl_frontend_deregister() */
+		break;
+	case AC_CONTRACT: {
+		struct ac_contract *ac;
+
+		ac = (struct ac_contract *)arg;
+
+		switch (ac->contract_number) {
+		case AC_CONTRACT_DEV_CHG: {
+			struct ac_device_changed *dev_chg;
+			struct ctlfe_softc *softc;
+			int retval, found;
+
+			dev_chg = (struct ac_device_changed *)ac->contract_data;
+
+			printf("%s: WWPN %#jx port %u path %u target %u %s\n",
+			       __func__, dev_chg->wwpn, dev_chg->port,
+			       xpt_path_path_id(path), dev_chg->target,
+			       (dev_chg->arrived == 0) ?  "left" : "arrived");
+
+			found = 0;
+
+			mtx_lock(&ctlfe_list_mtx);
+			STAILQ_FOREACH(softc, &ctlfe_softc_list, links) {
+				if (softc->path_id == xpt_path_path_id(path)) {
+					found = 1;
+					break;
+				}
+			}
+			mtx_unlock(&ctlfe_list_mtx);
+
+			if (found == 0) {
+				printf("%s: CTL port for CAM path %u not "
+				       "found!\n", __func__,
+				       xpt_path_path_id(path));
+				break;
+			}
+			if (dev_chg->arrived != 0) {
+				retval = ctl_add_initiator(dev_chg->wwpn,
+					softc->fe.targ_port, dev_chg->target);
+			} else {
+				retval = ctl_remove_initiator(
+					softc->fe.targ_port, dev_chg->target);
+			}
+
+			if (retval != 0) {
+				printf("%s: could not %s port %d iid %u "
+				       "WWPN %#jx!\n", __func__,
+				       (dev_chg->arrived != 0) ? "add" :
+				       "remove", softc->fe.targ_port,
+				       dev_chg->target,
+				       (uintmax_t)dev_chg->wwpn);
+			}
+			break;
+		}
+		default:
+			printf("%s: unsupported contract number %ju\n",
+			       __func__, (uintmax_t)ac->contract_number);
+			break;
+		}
+		break;
+	}
+	default:
+		break;
+	}
+}
+
+static cam_status
+ctlferegister(struct cam_periph *periph, void *arg)
+{
+	struct ctlfe_softc *bus_softc;
+	struct ctlfe_lun_softc *softc;
+	struct cam_sim *sim;
+	union ccb en_lun_ccb;
+	cam_status status;
+	int i;
+
+	softc = (struct ctlfe_lun_softc *)arg;
+	bus_softc = softc->parent_softc;
+	sim = xpt_path_sim(periph->path);
+	
+	TAILQ_INIT(&softc->work_queue);
+	softc->periph = periph;
+	softc->parent_softc = bus_softc;
+
+	callout_init_mtx(&softc->dma_callout, sim->mtx, /*flags*/ 0);
+	periph->softc = softc;
+
+	xpt_setup_ccb(&en_lun_ccb.ccb_h, periph->path, /*priority*/ 1);
+	en_lun_ccb.ccb_h.func_code = XPT_EN_LUN;
+	en_lun_ccb.cel.grp6_len = 0;
+	en_lun_ccb.cel.grp7_len = 0;
+	en_lun_ccb.cel.enable = 1;
+	xpt_action(&en_lun_ccb);
+	status = (en_lun_ccb.ccb_h.status & CAM_STATUS_MASK);
+	if (status != CAM_REQ_CMP) {
+		xpt_print(periph->path, "%s: Enable LUN failed, status 0x%x\n", 
+			  __func__, en_lun_ccb.ccb_h.status);
+		return (status);
+	}
+
+	status = CAM_REQ_CMP;
+
+	for (i = 0; i < CTLFE_ATIO_PER_LUN; i++) {
+		union ccb *new_ccb;
+
+		new_ccb = (union ccb *)malloc(sizeof(*new_ccb), M_CTLFE,
+					      M_NOWAIT);
+		if (new_ccb == NULL) {
+			status = CAM_RESRC_UNAVAIL;
+			break;
+		}
+		xpt_setup_ccb(&new_ccb->ccb_h, periph->path, /*priority*/ 1);
+		new_ccb->ccb_h.func_code = XPT_ACCEPT_TARGET_IO;
+		new_ccb->ccb_h.cbfcnp = ctlfedone;
+		xpt_action(new_ccb);
+		softc->atios_sent++;
+		status = new_ccb->ccb_h.status;
+		if ((status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
+			free(new_ccb, M_CTLFE);
+			break;
+		}
+	}
+
+	status = cam_periph_acquire(periph);
+	if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
+		xpt_print(periph->path, "%s: could not acquire reference "
+			  "count, status = %#x\n", __func__, status);
+		return (status);
+	}
+
+	if (i == 0) {
+		xpt_print(periph->path, "%s: could not allocate ATIO CCBs, "
+			  "status 0x%x\n", __func__, status);
+		return (CAM_REQ_CMP_ERR);
+	}
+
+	for (i = 0; i < CTLFE_IN_PER_LUN; i++) {
+		union ccb *new_ccb;
+
+		new_ccb = (union ccb *)malloc(sizeof(*new_ccb), M_CTLFE,
+					      M_NOWAIT);
+		if (new_ccb == NULL) {
+			status = CAM_RESRC_UNAVAIL;
+			break;
+		}
+
+		xpt_setup_ccb(&new_ccb->ccb_h, periph->path, /*priority*/ 1);
+		new_ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
+		new_ccb->ccb_h.cbfcnp = ctlfedone;
+		xpt_action(new_ccb);
+		softc->inots_sent++;
+		status = new_ccb->ccb_h.status;
+		if (status != CAM_REQ_INPROG) {
+			free(new_ccb, M_CTLFE);
+			break;
+		}
+	}
+	if (i == 0) {
+		xpt_print(periph->path, "%s: could not allocate immediate "
+			  "notify CCBs, status 0x%x\n", __func__, status);
+		return (CAM_REQ_CMP_ERR);
+	}
+	return (CAM_REQ_CMP);
+}
+
+static void
+ctlfeoninvalidate(struct cam_periph *periph)
+{
+	union ccb en_lun_ccb;
+	cam_status status;
+	struct ctlfe_lun_softc *softc;
+
+	softc = (struct ctlfe_lun_softc *)periph->softc;
+
+	xpt_setup_ccb(&en_lun_ccb.ccb_h, periph->path, /*priority*/ 1);
+	en_lun_ccb.ccb_h.func_code = XPT_EN_LUN;
+	en_lun_ccb.cel.grp6_len = 0;
+	en_lun_ccb.cel.grp7_len = 0;
+	en_lun_ccb.cel.enable = 0;
+	xpt_action(&en_lun_ccb);
+	status = (en_lun_ccb.ccb_h.status & CAM_STATUS_MASK);
+	if (status != CAM_REQ_CMP) {
+		xpt_print(periph->path, "%s: Disable LUN failed, status 0x%x\n",
+			  __func__, en_lun_ccb.ccb_h.status);
+		/*
+		 * XXX KDM what do we do now?
+		 */
+	}
+	xpt_print(periph->path, "LUN removed, %ju ATIOs outstanding, %ju "
+		  "INOTs outstanding, %d refs\n", softc->atios_sent -
+		  softc->atios_returned, softc->inots_sent -
+		  softc->inots_returned, periph->refcount);
+}
+
+static void
+ctlfecleanup(struct cam_periph *periph)
+{
+	struct ctlfe_lun_softc *softc;
+	struct ctlfe_softc *bus_softc;
+
+	xpt_print(periph->path, "%s: Called\n", __func__);
+
+	softc = (struct ctlfe_lun_softc *)periph->softc;
+	bus_softc = softc->parent_softc;
+
+	STAILQ_REMOVE(&bus_softc->lun_softc_list, softc, ctlfe_lun_softc,links);
+	
+	/*
+	 * XXX KDM is there anything else that needs to be done here?
+	 */
+	free(softc, M_CTLFE);
+}
+
+static void
+ctlfestart(struct cam_periph *periph, union ccb *start_ccb)
+{
+	struct ctlfe_lun_softc *softc;
+	struct ccb_hdr *ccb_h;
+
+	softc = (struct ctlfe_lun_softc *)periph->softc;
+
+	softc->ccbs_alloced++;
+
+	ccb_h = TAILQ_FIRST(&softc->work_queue);
+	if (periph->immediate_priority <= periph->pinfo.priority) {
+		panic("shouldn't get to the CCB waiting case!");
+		start_ccb->ccb_h.ccb_type = CTLFE_CCB_WAITING;
+		SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
+				  periph_links.sle);
+		periph->immediate_priority = CAM_PRIORITY_NONE;
+		wakeup(&periph->ccb_list);
+	} else if (ccb_h == NULL) {
+		softc->ccbs_freed++;
+		xpt_release_ccb(start_ccb);
+	} else {
+		struct ccb_accept_tio *atio;
+		struct ccb_scsiio *csio;
+		uint8_t *data_ptr;
+		uint32_t dxfer_len;
+		ccb_flags flags;
+		union ctl_io *io;
+		uint8_t scsi_status;
+
+		/* Take the ATIO off the work queue */
+		TAILQ_REMOVE(&softc->work_queue, ccb_h, periph_links.tqe);
+		atio = (struct ccb_accept_tio *)ccb_h;
+		io = (union ctl_io *)ccb_h->io_ptr;
+		csio = &start_ccb->csio;
+
+		flags = atio->ccb_h.flags &
+			(CAM_DIS_DISCONNECT|CAM_TAG_ACTION_VALID|CAM_DIR_MASK);
+
+		if ((io == NULL)
+		 || (io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) {
+			/*
+			 * We're done, send status back.
+			 */
+			flags |= CAM_SEND_STATUS;
+			if (io == NULL) {
+				scsi_status = SCSI_STATUS_BUSY;
+				csio->sense_len = 0;
+			} else if ((io->io_hdr.status & CTL_STATUS_MASK) ==
+				   CTL_CMD_ABORTED) {
+				io->io_hdr.flags &= ~CTL_FLAG_STATUS_QUEUED;
+
+				/*
+				 * If this command was aborted, we don't
+				 * need to send status back to the SIM.
+				 * Just free the CTIO and ctl_io, and
+				 * recycle the ATIO back to the SIM.
+				 */
+				xpt_print(periph->path, "%s: aborted "
+					  "command 0x%04x discarded\n",
+					  __func__, io->scsiio.tag_num);
+				ctl_free_io(io);
+				/*
+				 * For a wildcard attachment, commands can
+				 * come in with a specific target/lun.  Reset
+				 * the target and LUN fields back to the
+				 * wildcard values before we send them back
+				 * down to the SIM.  The SIM has a wildcard
+				 * LUN enabled, not whatever target/lun 
+				 * these happened to be.
+				 */
+				if (softc->flags & CTLFE_LUN_WILDCARD) {
+					atio->ccb_h.target_id =
+						CAM_TARGET_WILDCARD;
+					atio->ccb_h.target_lun =
+						CAM_LUN_WILDCARD;
+				}
+
+				if ((atio->ccb_h.status & CAM_DEV_QFRZN) != 0) {
+					cam_release_devq(periph->path,
+							 /*relsim_flags*/0,
+							 /*reduction*/0,
+ 							 /*timeout*/0,
+							 /*getcount_only*/0);
+					atio->ccb_h.status &= ~CAM_DEV_QFRZN;
+				}
+
+				ccb_h = TAILQ_FIRST(&softc->work_queue);
+
+				if (atio->ccb_h.func_code != 
+				    XPT_ACCEPT_TARGET_IO) {
+					xpt_print(periph->path, "%s: func_code "
+						  "is %#x\n", __func__,
+						  atio->ccb_h.func_code);
+				}
+				start_ccb->ccb_h.func_code = XPT_ABORT;
+				start_ccb->cab.abort_ccb = (union ccb *)atio;
+				start_ccb->ccb_h.cbfcnp = ctlfedone;
+
+				/* Tell the SIM that we've aborted this ATIO */
+				xpt_action(start_ccb);
+				softc->ccbs_freed++;
+				xpt_release_ccb(start_ccb);
+
+				/*
+				 * Send the ATIO back down to the SIM.
+				 */
+				xpt_action((union ccb *)atio);
+				softc->atios_sent++;
+
+				/*
+				 * If we still have work to do, ask for
+				 * another CCB.  Otherwise, deactivate our
+				 * callout.
+				 */
+				if (ccb_h != NULL)
+					xpt_schedule(periph, /*priority*/ 1);
+				else
+					callout_stop(&softc->dma_callout);
+
+				return;
+			} else {
+				io->io_hdr.flags &= ~CTL_FLAG_STATUS_QUEUED;
+				scsi_status = io->scsiio.scsi_status;
+				csio->sense_len = io->scsiio.sense_len;
+			}
+			data_ptr = NULL;
+			dxfer_len = 0;
+			if (io == NULL) {
+				printf("%s: tag %04x io is NULL\n", __func__,
+				       atio->tag_id);
+			} else {
+#ifdef CTLFEDEBUG
+				printf("%s: tag %04x status %x\n", __func__,
+				       atio->tag_id, io->io_hdr.status);
+#endif
+			}
+			csio->sglist_cnt = 0;
+			if (csio->sense_len != 0) {
+				csio->sense_data = io->scsiio.sense_data;
+				flags |= CAM_SEND_SENSE;
+			} else if (scsi_status == SCSI_STATUS_CHECK_COND) {
+				xpt_print(periph->path, "%s: check condition "
+					  "with no sense\n", __func__);
+			}
+		} else {
+			struct ctlfe_lun_cmd_info *cmd_info;
+
+			/*
+			 * Datamove call, we need to setup the S/G list. 
+			 * If we pass in a S/G list, the isp(4) driver at
+			 * least expects physical/bus addresses.
+			 */
+
+			cmd_info = (struct ctlfe_lun_cmd_info *)
+				io->io_hdr.port_priv;
+
+			KASSERT(sizeof(*cmd_info) < CTL_PORT_PRIV_SIZE,
+				("%s: sizeof(struct ctlfe_lun_cmd_info) %zd < "
+				"CTL_PORT_PRIV_SIZE %d", __func__,
+				sizeof(*cmd_info), CTL_PORT_PRIV_SIZE));
+			io->io_hdr.flags &= ~CTL_FLAG_DMA_QUEUED;
+
+			/*
+			 * Need to zero this, in case it has been used for
+			 * a previous datamove for this particular I/O.
+			 */
+			bzero(cmd_info, sizeof(*cmd_info));
+			scsi_status = 0;
+
+			/*
+			 * Set the direction, relative to the initiator.
+			 */
+			flags &= ~CAM_DIR_MASK;
+			if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
+			     CTL_FLAG_DATA_IN)
+				flags |= CAM_DIR_IN;
+			else
+				flags |= CAM_DIR_OUT;
+			
+			csio->cdb_len = atio->cdb_len;
+
+			if (io->scsiio.kern_sg_entries == 0) {
+				/* No S/G list */
+				data_ptr = io->scsiio.kern_data_ptr;
+				dxfer_len = io->scsiio.kern_data_len;
+				csio->sglist_cnt = 0;
+
+				if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR)
+					flags |= CAM_DATA_PHYS;
+			} else if (io->scsiio.kern_sg_entries <=
+				   (sizeof(cmd_info->cam_sglist)/
+				   sizeof(cmd_info->cam_sglist[0]))) {
+				/*
+				 * S/G list with physical or virtual pointers.
+				 * Just populate the CAM S/G list with the
+				 * pointers.
+				 */
+				int i;
+				struct ctl_sg_entry *ctl_sglist;
+				bus_dma_segment_t *cam_sglist;
+
+				ctl_sglist = (struct ctl_sg_entry *)
+					io->scsiio.kern_data_ptr;
+				cam_sglist = cmd_info->cam_sglist;
+
+				for (i = 0; i < io->scsiio.kern_sg_entries;i++){
+					cam_sglist[i].ds_addr =
+						(bus_addr_t)ctl_sglist[i].addr;
+					cam_sglist[i].ds_len =
+						ctl_sglist[i].len;
+				}
+				csio->sglist_cnt = io->scsiio.kern_sg_entries;
+				flags |= CAM_SCATTER_VALID;
+				if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR)
+					flags |= CAM_SG_LIST_PHYS;
+				else
+					flags &= ~CAM_SG_LIST_PHYS;
+				data_ptr = (uint8_t *)cam_sglist;
+				dxfer_len = io->scsiio.kern_data_len;
+			} else {
+				/* S/G list with virtual pointers */
+				struct ctl_sg_entry *sglist;
+				int *ti;
+
+				/*
+				 * XXX KDM this is a temporary hack.  The
+				 * isp(4) driver can't deal with S/G lists
+				 * with virtual pointers, so we need to
+				 * go through and send down one virtual
+				 * pointer at a time.
+				 */
+				sglist = (struct ctl_sg_entry *)
+					io->scsiio.kern_data_ptr;
+				ti = &cmd_info->cur_transfer_index;
+				data_ptr = sglist[*ti].addr;
+				dxfer_len = sglist[*ti].len;
+				csio->sglist_cnt = 0;
+				cmd_info->flags |= CTLFE_CMD_PIECEWISE;
+				(*ti)++;
+			}
+
+			io->scsiio.ext_data_filled += dxfer_len;
+
+			if (io->scsiio.ext_data_filled >
+			    io->scsiio.kern_total_len) {
+				xpt_print(periph->path, "%s: tag 0x%04x "
+					  "fill len %u > total %u\n",
+					  __func__, io->scsiio.tag_num,
+					  io->scsiio.ext_data_filled,
+					  io->scsiio.kern_total_len);
+			}
+		}
+
+#ifdef CTLFEDEBUG
+		printf("%s: %s: tag %04x flags %x ptr %p len %u\n", __func__,
+		       (flags & CAM_SEND_STATUS) ? "done" : "datamove",
+		       atio->tag_id, flags, data_ptr, dxfer_len);
+#endif
+
+		/*
+		 * Valid combinations:
+		 *  - CAM_SEND_STATUS, SCATTER_VALID = 0, dxfer_len = 0,
+		 *    sglist_cnt = 0
+		 *  - CAM_SEND_STATUS = 0, SCATTER_VALID = 0, dxfer_len != 0,
+		 *    sglist_cnt = 0 
+		 *  - CAM_SEND_STATUS = 0, SCATTER_VALID, dxfer_len != 0,
+		 *    sglist_cnt != 0
+		 */
+#ifdef CTLFEDEBUG
+		if (((flags & CAM_SEND_STATUS)
+		  && (((flags & CAM_SCATTER_VALID) != 0)
+		   || (dxfer_len != 0)
+		   || (csio->sglist_cnt != 0)))
+		 || (((flags & CAM_SEND_STATUS) == 0)
+		  && (dxfer_len == 0))
+		 || ((flags & CAM_SCATTER_VALID)
+		  && (csio->sglist_cnt == 0))
+		 || (((flags & CAM_SCATTER_VALID) == 0)
+		  && (csio->sglist_cnt != 0))) {
+			printf("%s: tag %04x cdb %02x flags %#x dxfer_len "
+			       "%d sg %u\n", __func__, atio->tag_id,
+			       atio->cdb_io.cdb_bytes[0], flags, dxfer_len,
+			       csio->sglist_cnt);
+			if (io != NULL) {
+				printf("%s: tag %04x io status %#x\n", __func__,
+				       atio->tag_id, io->io_hdr.status);
+			} else {
+				printf("%s: tag %04x no associated io\n",
+				       __func__, atio->tag_id);
+			}
+		}
+#endif
+		cam_fill_ctio(csio,
+			      /*retries*/ 2,
+			      ctlfedone,
+			      flags,
+			      (flags & CAM_TAG_ACTION_VALID) ?
+			       MSG_SIMPLE_Q_TAG : 0,
+			      atio->tag_id,
+			      atio->init_id,
+			      scsi_status,
+			      /*data_ptr*/ data_ptr,
+			      /*dxfer_len*/ dxfer_len,
+			      /*timeout*/ 5 * 1000);
+		start_ccb->ccb_h.ccb_atio = atio;
+		if (((flags & CAM_SEND_STATUS) == 0)
+		 && (io != NULL))
+			io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
+
+		softc->ctios_sent++;
+
+		xpt_action(start_ccb);
+
+		if ((atio->ccb_h.status & CAM_DEV_QFRZN) != 0) {
+			cam_release_devq(periph->path,
+					 /*relsim_flags*/0,
+					 /*reduction*/0,
+ 					 /*timeout*/0,
+					 /*getcount_only*/0);
+			atio->ccb_h.status &= ~CAM_DEV_QFRZN;
+		}
+
+		ccb_h = TAILQ_FIRST(&softc->work_queue);
+	}
+	/*
+	 * If we still have work to do, ask for another CCB.  Otherwise,
+	 * deactivate our callout.
+	 */
+	if (ccb_h != NULL)
+		xpt_schedule(periph, /*priority*/ 1);
+	else
+		callout_stop(&softc->dma_callout);
+}
+
+static void
+ctlfe_free_ccb(struct cam_periph *periph, union ccb *ccb)
+{
+	struct ctlfe_lun_softc *softc;
+
+	softc = (struct ctlfe_lun_softc *)periph->softc;
+
+	switch (ccb->ccb_h.func_code) {
+	case XPT_ACCEPT_TARGET_IO:
+		softc->atios_returned++;
+		break;
+	case XPT_IMMEDIATE_NOTIFY:
+	case XPT_NOTIFY_ACKNOWLEDGE:
+		softc->inots_returned++;
+		break;
+	default:
+		break;
+	}
+
+	free(ccb, M_CTLFE);
+
+	KASSERT(softc->atios_returned <= softc->atios_sent, ("%s: "
+		"atios_returned %ju > atios_sent %ju", __func__,
+		softc->atios_returned, softc->atios_sent));
+	KASSERT(softc->inots_returned <= softc->inots_sent, ("%s: "
+		"inots_returned %ju > inots_sent %ju", __func__,
+		softc->inots_returned, softc->inots_sent));
+
+	/*
+	 * If we have received all of our CCBs, we can release our
+	 * reference on the peripheral driver.  It will probably go away
+	 * now.
+	 */
+	if ((softc->atios_returned == softc->atios_sent)
+	 && (softc->inots_returned == softc->inots_sent)) {
+		cam_periph_release_locked(periph);
+	}
+}
+
+static void
+ctlfedone(struct cam_periph *periph, union ccb *done_ccb)
+{
+	struct ctlfe_lun_softc *softc;
+	struct ctlfe_softc *bus_softc;
+
+#ifdef CTLFE_DEBUG
+	printf("%s: entered, func_code = %#x, type = %#lx\n", __func__,
+	       done_ccb->ccb_h.func_code, done_ccb->ccb_h.ccb_type);
+#endif
+
+	softc = (struct ctlfe_lun_softc *)periph->softc;
+	bus_softc = softc->parent_softc;
+
+	if (done_ccb->ccb_h.ccb_type == CTLFE_CCB_WAITING) {
+		panic("shouldn't get to the CCB waiting case!");
+		wakeup(&done_ccb->ccb_h.cbfcnp);
+		return;
+	}
+
+	/*
+	 * If the peripheral is invalid, ATIOs and immediate notify CCBs
+	 * need to be freed.  Most of the ATIOs and INOTs that come back
+	 * will be CCBs that are being returned from the SIM as a result of
+	 * our disabling the LUN.
+	 *
+	 * Other CCB types are handled in their respective cases below.
+	 */
+	if (periph->flags & CAM_PERIPH_INVALID) {
+		switch (done_ccb->ccb_h.func_code) {
+		case XPT_ACCEPT_TARGET_IO:
+		case XPT_IMMEDIATE_NOTIFY:
+		case XPT_NOTIFY_ACKNOWLEDGE:
+			ctlfe_free_ccb(periph, done_ccb);
+			return;
+		default:
+			break;
+		}
+
+	}
+	switch (done_ccb->ccb_h.func_code) {
+	case XPT_ACCEPT_TARGET_IO: {
+		union ctl_io *io;
+		struct ccb_accept_tio *atio;
+
+		atio = &done_ccb->atio;
+
+		softc->atios_returned++;
+
+		/*
+		 * Allocate a ctl_io, pass it to CTL, and wait for the
+		 * datamove or done.
+		 */
+		io = ctl_alloc_io(bus_softc->fe.ctl_pool_ref);
+		if (io == NULL) {
+			atio->ccb_h.flags &= ~CAM_DIR_MASK;
+			atio->ccb_h.flags |= CAM_DIR_NONE;
+
+			printf("%s: ctl_alloc_io failed!\n", __func__);
+
+			/*
+			 * XXX KDM need to set SCSI_STATUS_BUSY, but there
+			 * is no field in the ATIO structure to do that,
+			 * and we aren't able to allocate a ctl_io here.
+			 * What to do?
+			 */
+			atio->sense_len = 0;
+			done_ccb->ccb_h.io_ptr = NULL;
+			TAILQ_INSERT_TAIL(&softc->work_queue, &atio->ccb_h,
+					  periph_links.tqe);
+			xpt_schedule(periph, /*priority*/ 1);
+			break;
+		}
+		ctl_zero_io(io);
+
+		/* Save pointers on both sides */
+		io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = done_ccb;
+		done_ccb->ccb_h.io_ptr = io;
+
+		/*
+		 * Only SCSI I/O comes down this path, resets, etc. come
+		 * down the immediate notify path below.
+		 */
+		io->io_hdr.io_type = CTL_IO_SCSI;
+		io->io_hdr.nexus.initid.id = atio->init_id;
+		io->io_hdr.nexus.targ_port = bus_softc->fe.targ_port;
+		io->io_hdr.nexus.targ_target.id = atio->ccb_h.target_id;
+		io->io_hdr.nexus.targ_lun = atio->ccb_h.target_lun;
+		io->scsiio.tag_num = atio->tag_id;
+		switch (atio->tag_action) {
+		case CAM_TAG_ACTION_NONE:
+			io->scsiio.tag_type = CTL_TAG_UNTAGGED;
+			break;
+		case MSG_SIMPLE_TASK:
+			io->scsiio.tag_type = CTL_TAG_SIMPLE;
+			break;
+		case MSG_HEAD_OF_QUEUE_TASK:
+        		io->scsiio.tag_type = CTL_TAG_HEAD_OF_QUEUE;
+			break;
+		case MSG_ORDERED_TASK:
+        		io->scsiio.tag_type = CTL_TAG_ORDERED;
+			break;
+		case MSG_ACA_TASK:
+			io->scsiio.tag_type = CTL_TAG_ACA;
+			break;
+		default:
+			io->scsiio.tag_type = CTL_TAG_UNTAGGED;
+			printf("%s: unhandled tag type %#x!!\n", __func__,
+			       atio->tag_action);
+			break;
+		}
+		if (atio->cdb_len > sizeof(io->scsiio.cdb)) {
+			printf("%s: WARNING: CDB len %d > ctl_io space %zd\n",
+			       __func__, atio->cdb_len, sizeof(io->scsiio.cdb));
+		}
+		io->scsiio.cdb_len = min(atio->cdb_len, sizeof(io->scsiio.cdb));
+		bcopy(atio->cdb_io.cdb_bytes, io->scsiio.cdb,
+		      io->scsiio.cdb_len);
+
+#ifdef CTLFEDEBUG
+		printf("%s: %ju:%d:%ju:%d: tag %04x CDB %02x\n", __func__,
+		        (uintmax_t)io->io_hdr.nexus.initid.id,
+		        io->io_hdr.nexus.targ_port,
+		        (uintmax_t)io->io_hdr.nexus.targ_target.id,
+		        io->io_hdr.nexus.targ_lun,
+			io->scsiio.tag_num, io->scsiio.cdb[0]);
+#endif
+
+		ctl_queue(io);
+		break;
+	}
+	case XPT_CONT_TARGET_IO: {
+		struct ccb_accept_tio *atio;
+		union ctl_io *io;
+
+		atio = (struct ccb_accept_tio *)done_ccb->ccb_h.ccb_atio;
+		io = (union ctl_io *)atio->ccb_h.io_ptr;
+
+		softc->ctios_returned++;
+#ifdef CTLFEDEBUG
+		printf("%s: got XPT_CONT_TARGET_IO tag %#x flags %#x\n",
+		       __func__, atio->tag_id, done_ccb->ccb_h.flags);
+#endif
+		/*
+		 * If we were sending status back to the initiator, free up
+		 * resources.  If we were doing a datamove, call the
+		 * datamove done routine.
+		 */
+		if (done_ccb->ccb_h.flags & CAM_SEND_STATUS) {
+			softc->ccbs_freed++;
+			xpt_release_ccb(done_ccb);
+			ctl_free_io(io);
+			/*
+			 * For a wildcard attachment, commands can come in
+			 * with a specific target/lun.  Reset the target
+			 * and LUN fields back to the wildcard values before
+			 * we send them back down to the SIM.  The SIM has
+			 * a wildcard LUN enabled, not whatever target/lun
+			 * these happened to be.
+			 */
+			if (softc->flags & CTLFE_LUN_WILDCARD) {
+				atio->ccb_h.target_id = CAM_TARGET_WILDCARD;
+				atio->ccb_h.target_lun = CAM_LUN_WILDCARD;
+			}
+			if (periph->flags & CAM_PERIPH_INVALID) {
+				ctlfe_free_ccb(periph, (union ccb *)atio);
+				return;
+			} else {
+				xpt_action((union ccb *)atio);
+				softc->atios_sent++;
+			}
+		} else {
+			struct ctlfe_lun_cmd_info *cmd_info;
+			struct ccb_scsiio *csio;
+
+			csio = &done_ccb->csio;
+			cmd_info = (struct ctlfe_lun_cmd_info *)
+				io->io_hdr.port_priv;
+
+			io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
+
+			io->scsiio.ext_data_len += csio->dxfer_len;
+			if (io->scsiio.ext_data_len >
+			    io->scsiio.kern_total_len) {
+				xpt_print(periph->path, "%s: tag 0x%04x "
+					  "done len %u > total %u sent %u\n",
+					  __func__, io->scsiio.tag_num,
+					  io->scsiio.ext_data_len,
+					  io->scsiio.kern_total_len,
+					  io->scsiio.ext_data_filled);
+			}
+			/*
+			 * Translate CAM status to CTL status.  Success
+			 * does not change the overall, ctl_io status.  In
+			 * that case we just set port_status to 0.  If we
+			 * have a failure, though, set a data phase error
+			 * for the overall ctl_io.
+			 */
+			switch (done_ccb->ccb_h.status & CAM_STATUS_MASK) {
+			case CAM_REQ_CMP:
+				io->io_hdr.port_status = 0;
+				break;
+			default:
+				/*
+				 * XXX KDM the isp(4) driver doesn't really
+				 * seem to send errors back for data
+				 * transfers that I can tell.  There is one
+				 * case where it'll send CAM_REQ_CMP_ERR,
+				 * but probably not that many more cases.
+				 * So set a generic data phase error here,
+				 * like the SXP driver sets.
+				 */
+				io->io_hdr.port_status = 0xbad1;
+				ctl_set_data_phase_error(&io->scsiio);
+				/*
+				 * XXX KDM figure out residual.
+				 */
+				break;
+			}
+			/*
+			 * If we had to break this S/G list into multiple
+			 * pieces, figure out where we are in the list, and
+			 * continue sending pieces if necessary.
+			 */
+			if ((cmd_info->flags & CTLFE_CMD_PIECEWISE)
+			 && (io->io_hdr.port_status == 0)
+			 && (cmd_info->cur_transfer_index <
+			     io->scsiio.kern_sg_entries)) {
+				struct ctl_sg_entry *sglist;
+				ccb_flags flags;
+				uint8_t scsi_status;
+				uint8_t *data_ptr;
+				uint32_t dxfer_len;
+				int *ti;
+
+				sglist = (struct ctl_sg_entry *)
+					io->scsiio.kern_data_ptr;
+				ti = &cmd_info->cur_transfer_index;
+				flags = atio->ccb_h.flags &
+					(CAM_DIS_DISCONNECT|
+					 CAM_TAG_ACTION_VALID|
+					 CAM_DIR_MASK);
+				
+				/*
+				 * Set the direction, relative to the initiator.
+				 */
+				flags &= ~CAM_DIR_MASK;
+				if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
+				     CTL_FLAG_DATA_IN)
+					flags |= CAM_DIR_IN;
+				else
+					flags |= CAM_DIR_OUT;
+
+				data_ptr = sglist[*ti].addr;
+				dxfer_len = sglist[*ti].len;
+				(*ti)++;
+
+				scsi_status = 0;
+
+				if (((flags & CAM_SEND_STATUS) == 0)
+				 && (dxfer_len == 0)) {
+					printf("%s: tag %04x no status or "
+					       "len cdb = %02x\n", __func__,
+					       atio->tag_id,
+					atio->cdb_io.cdb_bytes[0]);
+					printf("%s: tag %04x io status %#x\n",
+					       __func__, atio->tag_id,
+					       io->io_hdr.status);
+				}
+
+				cam_fill_ctio(csio,
+					      /*retries*/ 2,
+					      ctlfedone,
+					      flags,
+					      (flags & CAM_TAG_ACTION_VALID) ?
+					       MSG_SIMPLE_Q_TAG : 0,
+					      atio->tag_id,
+					      atio->init_id,
+					      scsi_status,
+					      /*data_ptr*/ data_ptr,
+					      /*dxfer_len*/ dxfer_len,
+					      /*timeout*/ 5 * 1000);
+
+				csio->resid = 0;
+				csio->ccb_h.ccb_atio = atio;
+				io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
+				softc->ctios_sent++;
+				xpt_action((union ccb *)csio);
+			} else {
+				/*
+				 * Release the CTIO.  The ATIO will be sent back
+				 * down to the SIM once we send status.
+				 */
+				softc->ccbs_freed++;
+				xpt_release_ccb(done_ccb);
+
+				/* Call the backend move done callback */
+				io->scsiio.be_move_done(io);
+			}
+		}
+		break;
+	}
+	case XPT_IMMEDIATE_NOTIFY: {
+		union ctl_io *io;
+		struct ccb_immediate_notify *inot;
+		cam_status status;
+		int frozen;
+
+		inot = &done_ccb->cin1;
+
+		softc->inots_returned++;
+
+		frozen = (done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
+
+		printf("%s: got XPT_IMMEDIATE_NOTIFY status %#x tag %#x "
+		       "seq %#x\n", __func__, inot->ccb_h.status,
+		       inot->tag_id, inot->seq_id);
+
+		io = ctl_alloc_io(bus_softc->fe.ctl_pool_ref);
+		if (io != NULL) {
+			int send_ctl_io;
+
+			send_ctl_io = 1;
+
+			ctl_zero_io(io);		
+			io->io_hdr.io_type = CTL_IO_TASK;
+			io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr =done_ccb;
+			inot->ccb_h.io_ptr = io;
+			io->io_hdr.nexus.initid.id = inot->initiator_id;
+			io->io_hdr.nexus.targ_port = bus_softc->fe.targ_port;
+			io->io_hdr.nexus.targ_target.id = inot->ccb_h.target_id;
+			io->io_hdr.nexus.targ_lun = inot->ccb_h.target_lun;
+			/* XXX KDM should this be the tag_id? */
+			io->taskio.tag_num = inot->seq_id;
+
+			status = inot->ccb_h.status & CAM_STATUS_MASK;
+			switch (status) {
+			case CAM_SCSI_BUS_RESET:
+				io->taskio.task_action = CTL_TASK_BUS_RESET;
+				break;
+			case CAM_BDR_SENT:
+				io->taskio.task_action = CTL_TASK_TARGET_RESET;
+				break;
+			case CAM_MESSAGE_RECV:
+				switch (inot->arg) {
+				case MSG_ABORT_TASK_SET:
+					/*
+					 * XXX KDM this isn't currently
+					 * supported by CTL.  It ends up
+					 * being a no-op.
+					 */
+					io->taskio.task_action =
+						CTL_TASK_ABORT_TASK_SET;
+					break;
+				case MSG_TARGET_RESET:
+					io->taskio.task_action =
+						CTL_TASK_TARGET_RESET;
+					break;
+				case MSG_ABORT_TASK:
+					io->taskio.task_action =
+						CTL_TASK_ABORT_TASK;
+					break;
+				case MSG_LOGICAL_UNIT_RESET:
+					io->taskio.task_action =
+						CTL_TASK_LUN_RESET;
+					break;
+				case MSG_CLEAR_TASK_SET:
+					/*
+					 * XXX KDM this isn't currently
+					 * supported by CTL.  It ends up
+					 * being a no-op.
+					 */
+					io->taskio.task_action =
+						CTL_TASK_CLEAR_TASK_SET;
+					break;
+				case MSG_CLEAR_ACA:
+					io->taskio.task_action = 
+						CTL_TASK_CLEAR_ACA;
+					break;
+				case MSG_NOOP:
+					send_ctl_io = 0;
+					break;
+				default:
+					xpt_print(periph->path, "%s: "
+						  "unsupported message 0x%x\n", 
+						  __func__, inot->arg);
+					send_ctl_io = 0;
+					break;
+				}
+				break;
+			case CAM_REQ_ABORTED:
+				/*
+				 * This request was sent back by the driver.
+				 * XXX KDM what do we do here?
+				 */
+				send_ctl_io = 0;
+				break;
+			default:
+				xpt_print(periph->path, "%s: "
+					  "unsupported CAM status 0x%x\n", 
+					  __func__, status);
+				send_ctl_io = 0;
+				break;
+			}
+			if (send_ctl_io != 0) {
+				ctl_queue(io);
+			} else {
+				ctl_free_io(io);
+				done_ccb->ccb_h.status = CAM_REQ_INPROG;
+				done_ccb->ccb_h.func_code =
+					XPT_NOTIFY_ACKNOWLEDGE;
+				xpt_action(done_ccb);
+			}
+		} else {
+			xpt_print(periph->path, "%s: could not allocate "
+				  "ctl_io for immediate notify!\n", __func__);
+			/* requeue this to the adapter */
+			done_ccb->ccb_h.status = CAM_REQ_INPROG;
+			done_ccb->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE;
+			xpt_action(done_ccb);
+		}
+
+		if (frozen != 0) {
+			cam_release_devq(periph->path,
+					 /*relsim_flags*/ 0,
+					 /*opening reduction*/ 0,
+					 /*timeout*/ 0,
+					 /*getcount_only*/ 0);
+		}
+		break;
+	}
+	case XPT_NOTIFY_ACKNOWLEDGE:
+		/*
+		 * Queue this back down to the SIM as an immediate notify.
+		 */
+		done_ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
+		xpt_action(done_ccb);
+		softc->inots_sent++;
+		break;
+	case XPT_ABORT:
+		/*
+		 * XPT_ABORT is an immediate CCB, we shouldn't get here.
+		 */
+		panic("%s: XPT_ABORT CCB returned!", __func__);
+		break;
+	case XPT_SET_SIM_KNOB:
+	case XPT_GET_SIM_KNOB:
+		break;
+	default:
+		panic("%s: unexpected CCB type %#x", __func__,
+		      done_ccb->ccb_h.func_code);
+		break;
+	}
+}
+
+static void
+ctlfe_onoffline(void *arg, int online)
+{
+	struct ctlfe_softc *bus_softc;
+	union ccb *ccb;
+	cam_status status;
+	struct cam_path *path;
+	struct cam_sim *sim;
+	int set_wwnn;
+
+	bus_softc = (struct ctlfe_softc *)arg;
+
+	set_wwnn = 0;
+
+	status = xpt_create_path(&path, /*periph*/ NULL, bus_softc->path_id,
+		CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
+	if (status != CAM_REQ_CMP) {
+		printf("%s: unable to create path!\n", __func__);
+		return;
+	}
+	ccb = (union ccb *)malloc(sizeof(*ccb), M_TEMP, M_WAITOK | M_ZERO);
+	if (ccb == NULL) {
+		printf("%s: unable to malloc CCB!\n", __func__);
+		xpt_free_path(path);
+		return;
+	}
+	xpt_setup_ccb(&ccb->ccb_h, path, /*priority*/ 1);
+
+	sim = xpt_path_sim(path);
+
+	/*
+	 * Copan WWN format:
+	 *
+	 * Bits 63-60:	0x5		NAA, IEEE registered name
+	 * Bits 59-36:	0x000ED5	IEEE Company name assigned to Copan
+	 * Bits 35-12:			Copan SSN (Sequential Serial Number)
+	 * Bits 11-8:			Type of port:
+	 *					1 == N-Port
+	 *					2 == F-Port
+	 *					3 == NL-Port
+	 * Bits 7-0:			0 == Node Name, >0 == Port Number
+	 */
+
+	if (online != 0) {
+
+		ccb->ccb_h.func_code = XPT_GET_SIM_KNOB;
+
+		CAM_SIM_LOCK(sim);
+
+		xpt_action(ccb);
+
+		CAM_SIM_UNLOCK(sim);
+
+		if ((ccb->knob.xport_specific.valid & KNOB_VALID_ADDRESS) != 0){
+#ifdef RANDOM_WWNN
+			uint64_t random_bits;
+#endif
+
+			printf("%s: %s current WWNN %#jx\n", __func__,
+			       bus_softc->port_name,
+			       ccb->knob.xport_specific.fc.wwnn);
+			printf("%s: %s current WWPN %#jx\n", __func__,
+			       bus_softc->port_name,
+			       ccb->knob.xport_specific.fc.wwpn);
+
+#ifdef RANDOM_WWNN
+			arc4rand(&random_bits, sizeof(random_bits), 0);
+#endif
+
+			/*
+			 * XXX KDM this is a bit of a kludge for now.  We
+			 * take the current WWNN/WWPN from the card, and
+			 * replace the company identifier and the NL-Port
+			 * indicator and the port number (for the WWPN).
+			 * This should be replaced later with ddb_GetWWNN,
+			 * or possibly a more centralized scheme.  (It
+			 * would be nice to have the WWNN/WWPN for each
+			 * port stored in the ctl_frontend structure.)
+			 */
+#ifdef RANDOM_WWNN
+			ccb->knob.xport_specific.fc.wwnn = 
+				(random_bits &
+				0x0000000fffffff00ULL) |
+				/* Company ID */ 0x5000ED5000000000ULL |
+				/* NL-Port */    0x0300;
+			ccb->knob.xport_specific.fc.wwpn = 
+				(random_bits &
+				0x0000000fffffff00ULL) |
+				/* Company ID */ 0x5000ED5000000000ULL |
+				/* NL-Port */    0x3000 |
+				/* Port Num */ (bus_softc->fe.targ_port & 0xff);
+
+			/*
+			 * This is a bit of an API break/reversal, but if
+			 * we're doing the random WWNN that's a little
+			 * different anyway.  So record what we're actually
+			 * using with the frontend code so it's reported
+			 * accurately.
+			 */
+			bus_softc->fe.wwnn = 
+				ccb->knob.xport_specific.fc.wwnn;
+			bus_softc->fe.wwpn = 
+				ccb->knob.xport_specific.fc.wwpn;
+			set_wwnn = 1;
+#else /* RANDOM_WWNN */
+			/*
+			 * If the user has specified a WWNN/WWPN, send them
+			 * down to the SIM.  Otherwise, record what the SIM
+			 * has reported.
+			 */
+			if ((bus_softc->fe.wwnn != 0)
+			 && (bus_softc->fe.wwpn != 0)) {
+				ccb->knob.xport_specific.fc.wwnn =
+					bus_softc->fe.wwnn;
+				ccb->knob.xport_specific.fc.wwpn =
+					bus_softc->fe.wwpn;
+				set_wwnn = 1;
+			} else {
+				bus_softc->fe.wwnn =
+					ccb->knob.xport_specific.fc.wwnn;
+				bus_softc->fe.wwpn =
+					ccb->knob.xport_specific.fc.wwpn;
+			}
+#endif /* RANDOM_WWNN */
+
+
+			if (set_wwnn != 0) {
+				printf("%s: %s new WWNN %#jx\n", __func__,
+				       bus_softc->port_name,
+				ccb->knob.xport_specific.fc.wwnn);
+				printf("%s: %s new WWPN %#jx\n", __func__,
+				       bus_softc->port_name,
+				       ccb->knob.xport_specific.fc.wwpn);
+			}
+		} else {
+			printf("%s: %s has no valid WWNN/WWPN\n", __func__,
+			       bus_softc->port_name);
+		}
+	}
+	ccb->ccb_h.func_code = XPT_SET_SIM_KNOB;
+	ccb->knob.xport_specific.valid = KNOB_VALID_ROLE;
+	if (set_wwnn != 0)
+		ccb->knob.xport_specific.valid |= KNOB_VALID_ADDRESS;
+
+	if (online != 0)
+		ccb->knob.xport_specific.fc.role = KNOB_ROLE_TARGET;
+	else
+		ccb->knob.xport_specific.fc.role = KNOB_ROLE_NONE;
+
+
+	CAM_SIM_LOCK(sim);
+
+	xpt_action(ccb);
+
+	CAM_SIM_UNLOCK(sim);
+
+	if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
+		printf("%s: SIM %s (path id %d) target %s failed with "
+		       "status %#x\n",
+		       __func__, bus_softc->port_name, bus_softc->path_id,
+		       (online != 0) ? "enable" : "disable",
+		       ccb->ccb_h.status);
+	} else {
+		printf("%s: SIM %s (path id %d) target %s succeeded\n",
+		       __func__, bus_softc->port_name, bus_softc->path_id,
+		       (online != 0) ? "enable" : "disable");
+	}
+
+	free(ccb, M_TEMP);
+	xpt_free_path(path);
+
+	return;
+}
+
+static void
+ctlfe_online(void *arg)
+{
+	ctlfe_onoffline(arg, /*online*/ 1);
+}
+
+static void
+ctlfe_offline(void *arg)
+{
+	ctlfe_onoffline(arg, /*online*/ 0);
+}
+
+static int
+ctlfe_targ_enable(void *arg, struct ctl_id targ_id)
+{
+	return (0);
+}
+
+static int
+ctlfe_targ_disable(void *arg, struct ctl_id targ_id)
+{
+	return (0);
+}
+
+/*
+ * This will get called to enable a LUN on every bus that is attached to
+ * CTL.  So we only need to create a path/periph for this particular bus.
+ */
+static int
+ctlfe_lun_enable(void *arg, struct ctl_id targ_id, int lun_id)
+{
+	struct ctlfe_softc *bus_softc;
+	struct ctlfe_lun_softc *softc;
+	struct cam_path *path;
+	struct cam_periph *periph;
+	struct cam_sim *sim;
+	cam_status status;
+
+	
+	bus_softc = (struct ctlfe_softc *)arg;
+
+	status = xpt_create_path_unlocked(&path, /*periph*/ NULL,
+					  bus_softc->path_id,
+					  targ_id.id,
+					  lun_id);
+	/* XXX KDM need some way to return status to CTL here? */
+	if (status != CAM_REQ_CMP) {
+		printf("%s: could not create path, status %#x\n", __func__,
+		       status);
+		return (1);
+	}
+
+	softc = malloc(sizeof(*softc), M_CTLFE, M_WAITOK | M_ZERO);
+	if (softc == NULL) {
+		printf("%s: could not allocate %zd bytes for softc\n",
+		       __func__, sizeof(*softc));
+		xpt_free_path(path);
+		return (1);
+	}
+	sim = xpt_path_sim(path);
+	mtx_lock(sim->mtx);
+	periph = cam_periph_find(path, "ctl");
+	if (periph != NULL) {
+		/* We've already got a periph, no need to alloc a new one. */
+		xpt_free_path(path);
+		free(softc, M_CTLFE);
+		mtx_unlock(sim->mtx);
+		return (0);
+	}
+
+	softc->parent_softc = bus_softc;
+	STAILQ_INSERT_TAIL(&bus_softc->lun_softc_list, softc, links);
+
+	status = cam_periph_alloc(ctlferegister,
+				  ctlfeoninvalidate,
+				  ctlfecleanup,
+				  ctlfestart,
+				  "ctl",
+				  CAM_PERIPH_BIO,
+				  path,
+				  ctlfeasync,
+				  0,
+				  softc);
+
+	mtx_unlock(sim->mtx);
+
+	xpt_free_path(path);
+
+	return (0);
+}
+
+/*
+ * XXX KDM we disable LUN removal here.  The problem is that the isp(4)
+ * driver doesn't currently handle LUN removal properly.  We need to keep 
+ * enough state here at the peripheral level even after LUNs have been
+ * removed inside CTL.
+ *
+ * Once the isp(4) driver is fixed, this can be re-enabled.
+ */
+static int
+ctlfe_lun_disable(void *arg, struct ctl_id targ_id, int lun_id)
+{
+#ifdef NOTYET
+	struct ctlfe_softc *softc;
+	struct ctlfe_lun_softc *lun_softc;
+
+	softc = (struct ctlfe_softc *)arg;
+
+	mtx_lock(softc->sim->mtx);
+	STAILQ_FOREACH(lun_softc, &softc->lun_softc_list, links) {
+		struct cam_path *path;
+
+		path = lun_softc->periph->path;
+
+		if ((xpt_path_target_id(path) == targ_id.id)
+		 && (xpt_path_lun_id(path) == lun_id)) {
+			break;
+		}
+	}
+	if (lun_softc == NULL) {
+		mtx_unlock(softc->sim->mtx);
+		printf("%s: can't find target %d lun %d\n", __func__,
+		       targ_id.id, lun_id);
+		return (1);
+	}
+
+	cam_periph_invalidate(lun_softc->periph);
+
+	mtx_unlock(softc->sim->mtx);
+#endif
+
+	return (0);
+}
+
+static void
+ctlfe_dump_sim(struct cam_sim *sim)
+{
+	int i;
+
+	printf("%s%d: max tagged openings: %d, max dev openings: %d\n",
+	       sim->sim_name, sim->unit_number,
+	       sim->max_tagged_dev_openings, sim->max_dev_openings);
+	printf("%s%d: max_ccbs: %u, ccb_count: %u\n", 
+	       sim->sim_name, sim->unit_number,
+	       sim->max_ccbs, sim->ccb_count);
+	printf("%s%d: ccb_freeq is %sempty\n",
+	       sim->sim_name, sim->unit_number,
+	       (SLIST_FIRST(&sim->ccb_freeq) == NULL) ? "" : "NOT ");
+	printf("%s%d: alloc_queue.entries %d, alloc_openings %d\n",
+	       sim->sim_name, sim->unit_number,
+	       sim->devq->alloc_queue.entries, sim->devq->alloc_openings);
+	printf("%s%d: qfrozen_cnt:", sim->sim_name, sim->unit_number);
+	for (i = 0; i < CAM_RL_VALUES; i++) {
+		printf("%s%u", (i != 0) ? ":" : "",
+		sim->devq->alloc_queue.qfrozen_cnt[i]);
+	}
+	printf("\n");
+}
+
+/*
+ * Assumes that the SIM lock is held.
+ */
+static void
+ctlfe_dump_queue(struct ctlfe_lun_softc *softc)
+{
+	struct ccb_hdr *hdr;
+	struct cam_periph *periph;
+	int num_items;
+
+	periph = softc->periph;
+	num_items = 0;
+
+	TAILQ_FOREACH(hdr, &softc->work_queue, periph_links.tqe) {
+		union ctl_io *io;
+
+		io = hdr->io_ptr;
+
+		num_items++;
+
+		/*
+		 * This can happen when we get an ATIO but can't allocate
+		 * a ctl_io.  See the XPT_ACCEPT_TARGET_IO case in ctlfedone().
+		 */
+		if (io == NULL) {
+			struct ccb_scsiio *csio;
+
+			csio = (struct ccb_scsiio *)hdr;
+
+			xpt_print(periph->path, "CCB %#x ctl_io allocation "
+				  "failed\n", csio->tag_id);
+			continue;
+		}
+
+		/*
+		 * Only regular SCSI I/O is put on the work
+		 * queue, so we can print sense here.  There may be no
+		 * sense if it's no the queue for a DMA, but this serves to
+		 * print out the CCB as well.
+		 *
+		 * XXX KDM switch this over to scsi_sense_print() when
+		 * CTL is merged in with CAM.
+		 */
+		ctl_io_error_print(io, NULL);
+
+		/*
+		 * We're sending status back to the
+		 * initiator, so we're on the queue waiting
+		 * for a CTIO to do that.
+		 */
+		if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)
+			continue;
+
+		/*
+		 * Otherwise, we're on the queue waiting to
+		 * do a data transfer.
+		 */
+		xpt_print(periph->path, "Total %u, Current %u, Resid %u\n",
+			  io->scsiio.kern_total_len, io->scsiio.kern_data_len,
+			  io->scsiio.kern_data_resid);
+	}
+
+	xpt_print(periph->path, "%d requests total waiting for CCBs\n",
+		  num_items);
+	xpt_print(periph->path, "%ju CCBs oustanding (%ju allocated, %ju "
+		  "freed)\n", (uintmax_t)(softc->ccbs_alloced -
+		  softc->ccbs_freed), (uintmax_t)softc->ccbs_alloced,
+		  (uintmax_t)softc->ccbs_freed);
+	xpt_print(periph->path, "%ju CTIOs outstanding (%ju sent, %ju "
+		  "returned\n", (uintmax_t)(softc->ctios_sent -
+		  softc->ctios_returned), softc->ctios_sent,
+		  softc->ctios_returned);
+}
+
+/*
+ * This function is called when we fail to get a CCB for a DMA or status return
+ * to the initiator within the specified time period.
+ *
+ * The callout code should insure that we hold the sim mutex here.
+ */
+static void
+ctlfe_dma_timeout(void *arg)
+{
+	struct ctlfe_lun_softc *softc;
+	struct cam_periph *periph;
+	struct cam_sim *sim;
+	int num_queued;
+
+	softc = (struct ctlfe_lun_softc *)arg;
+	periph = softc->periph;
+	sim = xpt_path_sim(periph->path);
+	num_queued = 0;
+
+	/*
+	 * Nothing to do...
+	 */
+	if (TAILQ_FIRST(&softc->work_queue) == NULL) {
+		xpt_print(periph->path, "TIMEOUT triggered after %d "
+			  "seconds, but nothing on work queue??\n",
+			  CTLFE_DMA_TIMEOUT);
+		return;
+	}
+
+	xpt_print(periph->path, "TIMEOUT (%d seconds) waiting for DMA to "
+		  "start\n", CTLFE_DMA_TIMEOUT);
+
+	ctlfe_dump_queue(softc);
+
+	ctlfe_dump_sim(sim);
+
+	xpt_print(periph->path, "calling xpt_schedule() to attempt to "
+		  "unstick our queue\n");
+
+	xpt_schedule(periph, /*priority*/ 1);
+
+	xpt_print(periph->path, "xpt_schedule() call complete\n");
+}
+
+/*
+ * Datamove/done routine called by CTL.  Put ourselves on the queue to
+ * receive a CCB from CAM so we can queue the continue I/O request down
+ * to the adapter.
+ */
+static void
+ctlfe_datamove_done(union ctl_io *io)
+{
+	union ccb *ccb;
+	struct cam_sim *sim;
+	struct cam_periph *periph;
+	struct ctlfe_lun_softc *softc;
+
+	ccb = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
+
+	sim = xpt_path_sim(ccb->ccb_h.path);
+
+	mtx_lock(sim->mtx);
+
+	periph = xpt_path_periph(ccb->ccb_h.path);
+
+	softc = (struct ctlfe_lun_softc *)periph->softc;
+
+	if (io->io_hdr.io_type == CTL_IO_TASK) {
+		/*
+		 * Task management commands don't require any further
+		 * communication back to the adapter.  Requeue the CCB
+		 * to the adapter, and free the CTL I/O.
+		 */
+		xpt_print(ccb->ccb_h.path, "%s: returning task I/O "
+			  "tag %#x seq %#x\n", __func__,
+			  ccb->cin1.tag_id, ccb->cin1.seq_id);
+		/*
+		 * Send the notify acknowledge down to the SIM, to let it
+		 * know we processed the task management command.
+		 */
+		ccb->ccb_h.status = CAM_REQ_INPROG;
+		ccb->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE;
+		xpt_action(ccb);
+		ctl_free_io(io);
+	} else {
+		if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)
+			io->io_hdr.flags |= CTL_FLAG_STATUS_QUEUED;
+		else
+			io->io_hdr.flags |= CTL_FLAG_DMA_QUEUED;
+
+		TAILQ_INSERT_TAIL(&softc->work_queue, &ccb->ccb_h,
+				  periph_links.tqe);
+
+		/*
+		 * Reset the timeout for our latest active DMA.
+		 */
+		callout_reset(&softc->dma_callout,
+			      CTLFE_DMA_TIMEOUT * hz,
+			      ctlfe_dma_timeout, softc);
+		/*
+		 * Ask for the CAM transport layer to send us a CCB to do
+		 * the DMA or send status, unless ctlfe_dma_enabled is set
+		 * to 0.
+		 */
+		if (ctlfe_dma_enabled != 0)
+			xpt_schedule(periph, /*priority*/ 1);
+	}
+
+	mtx_unlock(sim->mtx);
+}
+
+static void
+ctlfe_dump(void)
+{
+	struct ctlfe_softc *bus_softc;
+
+	STAILQ_FOREACH(bus_softc, &ctlfe_softc_list, links) {
+		struct ctlfe_lun_softc *lun_softc;
+
+		ctlfe_dump_sim(bus_softc->sim);
+
+		STAILQ_FOREACH(lun_softc, &bus_softc->lun_softc_list, links) {
+			ctlfe_dump_queue(lun_softc);
+		}
+	}
+}
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/cam/scsi/scsi_all.c
--- a/head/sys/cam/scsi/scsi_all.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/cam/scsi/scsi_all.c	Fri Jan 13 15:51:01 2012 +0200
@@ -28,7 +28,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/cam/scsi/scsi_all.c 225950 2011-10-03 20:32:55Z ken $");
+__FBSDID("$FreeBSD: head/sys/cam/scsi/scsi_all.c 229997 2012-01-12 00:34:33Z ken $");
 
 #include <sys/param.h>
 #include <sys/types.h>
@@ -5057,14 +5057,7 @@
 		scsi_cmd->byte2 |= SI_EVPD;
 		scsi_cmd->page_code = page_code;		
 	}
-	/*
-	 * A 'transfer units' count of 256 is coded as
-	 * zero for all commands with a single byte count
-	 * field. 
-	 */
-	if (inq_len == 256)
-		inq_len = 0;
-	scsi_cmd->length = inq_len;
+	scsi_ulto2b(inq_len, scsi_cmd->length);
 }
 
 void
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/cam/scsi/scsi_all.h
--- a/head/sys/cam/scsi/scsi_all.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/cam/scsi/scsi_all.h	Fri Jan 13 15:51:01 2012 +0200
@@ -14,7 +14,7 @@
  *
  * Ported to run under 386BSD by Julian Elischer (julian at tfs.com) Sept 1992
  *
- * $FreeBSD: head/sys/cam/scsi/scsi_all.h 228846 2011-12-23 19:12:02Z mav $
+ * $FreeBSD: head/sys/cam/scsi/scsi_all.h 229997 2012-01-12 00:34:33Z ken $
  */
 
 /*
@@ -175,8 +175,7 @@
 #define	SI_EVPD 	0x01
 #define	SI_CMDDT	0x02
 	u_int8_t page_code;
-	u_int8_t reserved;
-	u_int8_t length;
+	u_int8_t length[2];
 	u_int8_t control;
 };
 
@@ -532,6 +531,55 @@
 	uint8_t non_cache_seg_size[3];
 };
 
+/*
+ * XXX KDM move this off to a vendor shim.
+ */
+struct copan_power_subpage {
+	uint8_t page_code;
+#define	PWR_PAGE_CODE		0x00
+	uint8_t subpage;
+#define	PWR_SUBPAGE_CODE	0x02
+	uint8_t page_length[2];
+	uint8_t page_version;
+#define	PWR_VERSION		    0x01
+	uint8_t total_luns;
+	uint8_t max_active_luns;
+#define	PWR_DFLT_MAX_LUNS	    0x07
+	uint8_t reserved[25];
+};
+
+/*
+ * XXX KDM move this off to a vendor shim.
+ */
+struct copan_aps_subpage {
+	uint8_t page_code;
+#define	APS_PAGE_CODE		0x00
+	uint8_t subpage;
+#define	APS_SUBPAGE_CODE	0x03
+	uint8_t page_length[2];
+	uint8_t page_version;
+#define	APS_VERSION		    0x00
+	uint8_t lock_active;
+#define	APS_LOCK_ACTIVE	    0x01
+#define	APS_LOCK_INACTIVE	0x00
+	uint8_t reserved[26];
+};
+
+/*
+ * XXX KDM move this off to a vendor shim.
+ */
+struct copan_debugconf_subpage {
+	uint8_t page_code;
+#define DBGCNF_PAGE_CODE		0x00
+	uint8_t subpage;
+#define DBGCNF_SUBPAGE_CODE	0xF0
+	uint8_t page_length[2];
+	uint8_t page_version;
+#define DBGCNF_VERSION			0x00
+	uint8_t ctl_time_io_secs[2];
+};
+
+
 struct scsi_info_exceptions_page {
 	u_int8_t page_code;
 #define	SIEP_PAGE_SAVABLE		0x80	/* Page is savable */
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/cam/scsi/scsi_da.c
--- a/head/sys/cam/scsi/scsi_da.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/cam/scsi/scsi_da.c	Fri Jan 13 15:51:01 2012 +0200
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/cam/scsi/scsi_da.c 229288 2012-01-02 17:02:45Z mav $");
+__FBSDID("$FreeBSD: head/sys/cam/scsi/scsi_da.c 230000 2012-01-12 00:41:48Z ken $");
 
 #include <sys/param.h>
 
@@ -127,6 +127,7 @@
 	da_flags flags;	
 	da_quirks quirks;
 	int	 minimum_cmd_size;
+	int	 error_inject;
 	int	 ordered_tag_count;
 	int	 outstanding_cmds;
 	struct	 disk_params params;
@@ -879,7 +880,7 @@
 	}
 
 	if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
-		return(ENXIO);
+		return (ENXIO);
 	}
 
 	cam_periph_lock(periph);
@@ -940,13 +941,13 @@
 
 	periph = (struct cam_periph *)dp->d_drv1;
 	if (periph == NULL)
-		return (ENXIO);	
+		return (0);	
 
 	cam_periph_lock(periph);
 	if ((error = cam_periph_hold(periph, PRIBIO)) != 0) {
 		cam_periph_unlock(periph);
 		cam_periph_release(periph);
-		return (error);
+		return (0);
 	}
 
 	softc = (struct da_softc *)periph->softc;
@@ -1214,8 +1215,8 @@
 	bioq_flush(&softc->bio_queue, NULL, ENXIO);
 
 	disk_gone(softc->disk);
-	xpt_print(periph->path, "lost device - %d outstanding\n",
-		  softc->outstanding_cmds);
+	xpt_print(periph->path, "lost device - %d outstanding, %d refs\n",
+		  softc->outstanding_cmds, periph->refcount);
 }
 
 static void
@@ -1361,6 +1362,16 @@
 		&softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
 		"Minimum CDB size");
 
+	SYSCTL_ADD_INT(&softc->sysctl_ctx,
+		       SYSCTL_CHILDREN(softc->sysctl_tree),
+		       OID_AUTO,
+		       "error_inject",
+		       CTLFLAG_RW,
+		       &softc->error_inject,
+		       0,
+		       "error_inject leaf");
+
+
 	/*
 	 * Add some addressing info.
 	 */
@@ -1887,6 +1898,13 @@
 			bp->bio_resid = csio->resid;
 			if (csio->resid > 0)
 				bp->bio_flags |= BIO_ERROR;
+			if (softc->error_inject != 0) {
+				bp->bio_error = softc->error_inject;
+				bp->bio_resid = bp->bio_bcount;
+				bp->bio_flags |= BIO_ERROR;
+				softc->error_inject = 0;
+			}
+
 		}
 
 		/*
@@ -2103,13 +2121,20 @@
 		}
 		free(csio->data_ptr, M_SCSIDA);
 		if (announce_buf[0] != '\0') {
-			xpt_announce_periph(periph, announce_buf);
 			/*
 			 * Create our sysctl variables, now that we know
 			 * we have successfully attached.
 			 */
-			(void) cam_periph_acquire(periph);	/* increase the refcount */
-			taskqueue_enqueue(taskqueue_thread,&softc->sysctl_task);
+			/* increase the refcount */
+			if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
+				taskqueue_enqueue(taskqueue_thread,
+						  &softc->sysctl_task);
+				xpt_announce_periph(periph, announce_buf);
+			} else {
+				xpt_print(periph->path, "fatal error, "
+				    "could not acquire reference count\n");
+			}
+				
 		}
 		softc->state = DA_STATE_NORMAL;	
 		/*
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/cam/scsi/scsi_da.h
--- a/head/sys/cam/scsi/scsi_da.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/cam/scsi/scsi_da.h	Fri Jan 13 15:51:01 2012 +0200
@@ -46,7 +46,7 @@
  *
  * Ported to run under 386BSD by Julian Elischer (julian at tfs.com) Sept 1992
  *
- * $FreeBSD$
+ * $FreeBSD: head/sys/cam/scsi/scsi_da.h 229997 2012-01-12 00:34:33Z ken $
  */
 
 #ifndef	_SCSI_SCSI_DA_H
@@ -421,6 +421,56 @@
     	} flexible_disk;	
 };
 
+/*
+ * XXX KDM
+ * Here for CTL compatibility, reconcile this.
+ */
+struct scsi_format_page {
+	uint8_t page_code;
+	uint8_t page_length;
+	uint8_t tracks_per_zone[2];
+	uint8_t alt_sectors_per_zone[2];
+	uint8_t alt_tracks_per_zone[2];
+	uint8_t alt_tracks_per_lun[2];
+	uint8_t sectors_per_track[2];
+	uint8_t bytes_per_sector[2];
+	uint8_t interleave[2];
+	uint8_t track_skew[2];
+	uint8_t cylinder_skew[2];
+	uint8_t flags;
+#define	SFP_SSEC	0x80
+#define	SFP_HSEC	0x40
+#define	SFP_RMB		0x20
+#define	SFP_SURF	0x10
+	uint8_t reserved[3];
+};
+
+/*
+ * XXX KDM
+ * Here for CTL compatibility, reconcile this.
+ */
+struct scsi_rigid_disk_page {
+	uint8_t page_code;
+#define	SMS_RIGID_DISK_PAGE		0x04
+	uint8_t page_length;
+	uint8_t cylinders[3];
+	uint8_t heads;
+	uint8_t start_write_precomp[3];
+	uint8_t start_reduced_current[3];
+	uint8_t step_rate[2];
+	uint8_t landing_zone_cylinder[3];
+	uint8_t rpl;
+#define	SRDP_RPL_DISABLED	0x00
+#define	SRDP_RPL_SLAVE		0x01
+#define	SRDP_RPL_MASTER		0x02
+#define	SRDP_RPL_MASTER_CONTROL	0x03
+	uint8_t rotational_offset;
+	uint8_t reserved1;
+	uint8_t rotation_rate[2];
+	uint8_t reserved2[2];
+};
+
+
 struct scsi_da_rw_recovery_page {
 	u_int8_t page_code;
 #define SMS_RW_ERROR_RECOVERY_PAGE	0x01
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/cam/scsi/scsi_sg.c
--- a/head/sys/cam/scsi/scsi_sg.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/cam/scsi/scsi_sg.c	Fri Jan 13 15:51:01 2012 +0200
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/cam/scsi/scsi_sg.c 220644 2011-04-14 21:25:32Z mav $");
+__FBSDID("$FreeBSD: head/sys/cam/scsi/scsi_sg.c 230000 2012-01-12 00:41:48Z ken $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -399,18 +399,24 @@
 	if (periph == NULL)
 		return (ENXIO);
 
+	if (cam_periph_acquire(periph) != CAM_REQ_CMP)
+		return (ENXIO);
+
 	/*
 	 * Don't allow access when we're running at a high securelevel.
 	 */
 	error = securelevel_gt(td->td_ucred, 1);
-	if (error)
+	if (error) {
+		cam_periph_release(periph);
 		return (error);
+	}
 
 	cam_periph_lock(periph);
 
 	softc = (struct sg_softc *)periph->softc;
 	if (softc->flags & SG_FLAG_INVALID) {
 		cam_periph_unlock(periph);
+		cam_periph_release(periph);
 		return (ENXIO);
 	}
 
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/cam/scsi/scsi_targ_bh.c
--- a/head/sys/cam/scsi/scsi_targ_bh.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/cam/scsi/scsi_targ_bh.c	Fri Jan 13 15:51:01 2012 +0200
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/cam/scsi/scsi_targ_bh.c 227293 2011-11-07 06:44:47Z ed $");
+__FBSDID("$FreeBSD: head/sys/cam/scsi/scsi_targ_bh.c 229997 2012-01-12 00:34:33Z ken $");
 
 #include <sys/param.h>
 #include <sys/queue.h>
@@ -604,7 +604,7 @@
 			atio->ccb_h.flags |= CAM_DIR_IN;
 			descr->data = &no_lun_inq_data;
 			descr->data_resid = MIN(sizeof(no_lun_inq_data),
-						SCSI_CDB6_LEN(inq->length));
+						scsi_2btoul(inq->length));
 			descr->data_increment = descr->data_resid;
 			descr->timeout = 5 * 1000;
 			descr->status = SCSI_STATUS_OK;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/cddl/contrib/opensolaris/uts/common/fs/zfs/arc.c
--- a/head/sys/cddl/contrib/opensolaris/uts/common/fs/zfs/arc.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/cddl/contrib/opensolaris/uts/common/fs/zfs/arc.c	Fri Jan 13 15:51:01 2012 +0200
@@ -464,10 +464,10 @@
 static uint64_t		arc_meta_used;
 static uint64_t		arc_meta_limit;
 static uint64_t		arc_meta_max = 0;
-SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, arc_meta_used, CTLFLAG_RDTUN,
-    &arc_meta_used, 0, "ARC metadata used");
-SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, arc_meta_limit, CTLFLAG_RDTUN,
-    &arc_meta_limit, 0, "ARC metadata limit");
+SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, arc_meta_used, CTLFLAG_RD, &arc_meta_used, 0,
+    "ARC metadata used");
+SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, arc_meta_limit, CTLFLAG_RW, &arc_meta_limit, 0,
+    "ARC metadata limit");
 
 typedef struct l2arc_buf_hdr l2arc_buf_hdr_t;
 
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/conf/NOTES
--- a/head/sys/conf/NOTES	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/conf/NOTES	Fri Jan 13 15:51:01 2012 +0200
@@ -1,4 +1,4 @@
-# $FreeBSD: head/sys/conf/NOTES 228563 2011-12-16 08:27:04Z glebius $
+# $FreeBSD: head/sys/conf/NOTES 229997 2012-01-12 00:34:33Z ken $
 #
 # NOTES -- Lines that can be cut/pasted into kernel and hints configs.
 #
@@ -1297,6 +1297,7 @@
 device		targbh		#SCSI Target Mode Blackhole Device
 device		pass		#CAM passthrough driver
 device		sg		#Linux SCSI passthrough
+device		ctl		#CAM Target Layer
 
 # CAM OPTIONS:
 # debugging options:
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/conf/files
--- a/head/sys/conf/files	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/conf/files	Fri Jan 13 15:51:01 2012 +0200
@@ -1,4 +1,4 @@
-# $FreeBSD: head/sys/conf/files 229537 2012-01-04 23:39:11Z ray $
+# $FreeBSD: head/sys/conf/files 229997 2012-01-12 00:34:33Z ken $
 #
 # The long compile-with and dependency lines are required because of
 # limitations in config: backslash-newline doesn't work in strings, and
@@ -50,11 +50,6 @@
 aic79xx_reg_print.o		optional ahd pci ahd_reg_pretty_print	   \
 	compile-with	"${NORMAL_C}"					   \
 	no-implicit-rule local
-emu10k1-alsa%diked.h		optional snd_emu10kx	   \
-	dependency	"$S/tools/sound/emu10k1-mkalsa.sh $S/gnu/dev/sound/pci/emu10k1-alsa.h" \
-	compile-with	"CC='${CC}' AWK=${AWK} sh $S/tools/sound/emu10k1-mkalsa.sh $S/gnu/dev/sound/pci/emu10k1-alsa.h emu10k1-alsa%diked.h" \
-	no-obj no-implicit-rule before-depend				   \
-	clean		"emu10k1-alsa%diked.h"
 #
 # The 'fdt_dtb_file' target covers an actual DTB file name, which is derived
 # from the specified source (DTS) file: <platform>.dts -> <platform>.dtb
@@ -67,16 +62,6 @@
 	compile-with "sh $S/tools/fdt/make_dtbh.sh ${FDT_DTS_FILE} ." \
 	no-obj no-implicit-rule before-depend \
 	clean		"fdt_static_dtb.h"
-p16v-alsa%diked.h		optional snd_emu10kx pci		   \
-	dependency	"$S/tools/sound/emu10k1-mkalsa.sh $S/gnu/dev/sound/pci/p16v-alsa.h" \
-	compile-with	"CC='${CC}' AWK=${AWK} sh $S/tools/sound/emu10k1-mkalsa.sh $S/gnu/dev/sound/pci/p16v-alsa.h p16v-alsa%diked.h" \
-	no-obj no-implicit-rule before-depend				   \
-	clean		"p16v-alsa%diked.h"
-p17v-alsa%diked.h		optional snd_emu10kx pci		   \
-	dependency	"$S/tools/sound/emu10k1-mkalsa.sh $S/gnu/dev/sound/pci/p17v-alsa.h" \
-	compile-with	"CC='${CC}' AWK=${AWK} sh $S/tools/sound/emu10k1-mkalsa.sh $S/gnu/dev/sound/pci/p17v-alsa.h p17v-alsa%diked.h" \
-	no-obj no-implicit-rule before-depend				   \
-	clean		"p17v-alsa%diked.h"
 feeder_eq_gen.h			optional sound				   \
 	dependency	"$S/tools/sound/feeder_eq_mkfilter.awk"		   \
 	compile-with	"${AWK} -f $S/tools/sound/feeder_eq_mkfilter.awk -- ${FEEDER_EQ_PRESETS} > feeder_eq_gen.h" \
@@ -130,6 +115,19 @@
 cam/scsi/scsi_cd.c		optional cd
 cam/scsi/scsi_ch.c		optional ch
 cam/ata/ata_da.c		optional ada | da
+cam/ctl/ctl.c			optional ctl
+cam/ctl/ctl_backend.c		optional ctl
+cam/ctl/ctl_backend_block.c	optional ctl
+cam/ctl/ctl_backend_ramdisk.c	optional ctl
+cam/ctl/ctl_cmd_table.c		optional ctl
+cam/ctl/ctl_frontend.c		optional ctl
+cam/ctl/ctl_frontend_cam_sim.c	optional ctl
+cam/ctl/ctl_frontend_internal.c	optional ctl
+cam/ctl/ctl_mem_pool.c		optional ctl
+cam/ctl/ctl_scsi_all.c		optional ctl
+cam/ctl/ctl_error.c		optional ctl
+cam/ctl/ctl_util.c		optional ctl
+cam/ctl/scsi_ctl.c		optional ctl
 cam/scsi/scsi_da.c		optional da
 cam/scsi/scsi_low.c		optional ct | ncv | nsp | stg
 cam/scsi/scsi_low_pisa.c	optional ct | ncv | nsp | stg
@@ -282,6 +280,7 @@
 contrib/dev/acpica/tables/tbutils.c		optional acpi
 contrib/dev/acpica/tables/tbxface.c		optional acpi
 contrib/dev/acpica/tables/tbxfroot.c		optional acpi
+contrib/dev/acpica/utilities/utaddress.c	optional acpi
 contrib/dev/acpica/utilities/utalloc.c		optional acpi
 contrib/dev/acpica/utilities/utcache.c		optional acpi
 contrib/dev/acpica/utilities/utcopy.c		optional acpi
@@ -1744,19 +1743,9 @@
 dev/sound/pci/csapcm.c		optional snd_csa pci
 dev/sound/pci/ds1.c		optional snd_ds1 pci
 dev/sound/pci/emu10k1.c		optional snd_emu10k1 pci
-dev/sound/pci/emu10kx.c		optional snd_emu10kx pci \
-	dependency "emu10k1-alsa%diked.h" \
-	dependency "p16v-alsa%diked.h" \
-	dependency "p17v-alsa%diked.h" \
-	warning "kernel contains GPL contaminated emu10kx headers"
-dev/sound/pci/emu10kx-pcm.c	optional snd_emu10kx pci \
-	dependency "emu10k1-alsa%diked.h" \
-	dependency "p16v-alsa%diked.h" \
-	dependency "p17v-alsa%diked.h" \
-	warning "kernel contains GPL contaminated emu10kx headers"
-dev/sound/pci/emu10kx-midi.c	optional snd_emu10kx pci \
-	dependency "emu10k1-alsa%diked.h" \
-	warning "kernel contains GPL contaminated emu10kx headers"
+dev/sound/pci/emu10kx.c		optional snd_emu10kx pci
+dev/sound/pci/emu10kx-pcm.c	optional snd_emu10kx pci
+dev/sound/pci/emu10kx-midi.c	optional snd_emu10kx pci
 dev/sound/pci/envy24.c		optional snd_envy24 pci
 dev/sound/pci/envy24ht.c	optional snd_envy24ht pci
 dev/sound/pci/es137x.c		optional snd_es137x pci
@@ -2436,8 +2425,9 @@
 kern/serdev_if.m		standard
 kern/stack_protector.c		standard \
 	compile-with "${NORMAL_C:N-fstack-protector*}"
-kern/subr_acl_nfs4.c		standard
-kern/subr_acl_posix1e.c		standard
+# XXX subr_acl_nfs4.c is also used by ZFS
+kern/subr_acl_nfs4.c		optional ufs_acl
+kern/subr_acl_posix1e.c		optional ufs_acl
 kern/subr_autoconf.c		standard
 kern/subr_blist.c		standard
 kern/subr_bus.c			standard
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/conf/files.amd64
--- a/head/sys/conf/files.amd64	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/conf/files.amd64	Fri Jan 13 15:51:01 2012 +0200
@@ -1,7 +1,7 @@
 # This file tells config what files go into building a kernel,
 # files marked standard are always included.
 #
-# $FreeBSD: head/sys/conf/files.amd64 228940 2011-12-28 23:26:58Z delphij $
+# $FreeBSD: head/sys/conf/files.amd64 229887 2012-01-09 23:20:30Z jkim $
 #
 # The long compile-with and dependency lines are required because of
 # limitations in config: backslash-newline doesn't work in strings, and
@@ -215,6 +215,7 @@
 dev/nfe/if_nfe.c		optional	nfe pci
 dev/nve/if_nve.c		optional	nve pci
 dev/nvram/nvram.c		optional	nvram isa
+dev/random/nehemiah.c		optional	random
 dev/qlxgb/qla_dbg.c		optional	qlxgb pci
 dev/qlxgb/qla_hw.c		optional	qlxgb pci
 dev/qlxgb/qla_ioctl.c		optional	qlxgb pci
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/changes.txt
--- a/head/sys/contrib/dev/acpica/changes.txt	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/changes.txt	Fri Jan 13 15:51:01 2012 +0200
@@ -1,9 +1,57 @@
+----------------------------------------
+11 January 2012. Summary of changes for version 20120111:
+
+This release is available at www.acpica.org/downloads.
+The ACPI 5.0 specification is available at www.acpi.info.
+
+1) ACPICA Core Subsystem:
+
+Implemented a new mechanism to allow host device drivers to check for address 
+range conflicts with ACPI Operation Regions. Both SystemMemory and SystemIO 
+address spaces are supported. A new external interface, AcpiCheckAddressRange, 
+allows drivers to check an address range against the ACPI namespace. See the 
+ACPICA reference for additional details. Adds one new file, 
+utilities/utaddress.c. Lin Ming, Bob Moore.
+
+Fixed several issues with the ACPI 5.0 FADT support: Add the sleep Control and 
+Status registers, update the ACPI 5.0 flags, and update internal data 
+structures to handle an FADT larger than 256 bytes. The size of the ACPI 5.0 
+FADT is 268 bytes.
+
+Updated all ACPICA copyrights and signons to 2012. Added the 2012 copyright to 
+all module headers and signons, including the standard Linux header. This 
+affects virtually every file in the ACPICA core subsystem, iASL compiler, and 
+all ACPICA utilities.
+
+Example Code and Data Size: These are the sizes for the OS-independent 
+acpica.lib produced by the Microsoft Visual C++ 9.0 32-bit compiler. The debug 
+version of the code includes the debug output trace mechanism and has a much 
+larger code and data size.
+
+  Previous Release:
+    Non-Debug Version:  92.3K Code, 24.9K Data, 117.2K Total
+    Debug Version:     170.8K Code, 72.6K Data, 243.4K Total
+  Current Release:
+    Non-Debug Version:  92.8K Code, 24.9K Data, 117.7K Total
+    Debug Version:     171.7K Code, 72.9K Data, 244.5K Total
+
+
+2) iASL Compiler/Disassembler and Tools:
+
+Disassembler: fixed a problem with the automatic resource tag generation 
+support. Fixes a problem where the resource tags are inadvertently not 
+constructed if the table being disassembled contains external references to 
+control methods. Moved the actual construction of the tags to after the final 
+namespace is constructed (after 2nd parse is invoked due to external control 
+method references.) ACPICA BZ 941.
+
+Table Compiler: Make all "generic" operators caseless. These are the operators 
+like UINT8, String, etc. Making these caseless improves ease-of-use. ACPICA BZ 
+934.
+
 ----------------------------------------
 23 November 2011. Summary of changes for version 20111123:
 
-This release is available at www.acpica.org/downloads
-The ACPI 5.0 specification is available at www.acpi.info
-
 0) ACPI 5.0 Support:
 
 This release contains full support for the ACPI 5.0 specification, as 
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/common/adfile.c
--- a/head/sys/contrib/dev/acpica/common/adfile.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/common/adfile.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/common/adisasm.c
--- a/head/sys/contrib/dev/acpica/common/adisasm.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/common/adisasm.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -376,7 +376,8 @@
         AdDisassemblerHeader (Filename);
         AcpiOsPrintf (" * ACPI Data Table [%4.4s]\n *\n",
             Table->Signature);
-        AcpiOsPrintf (" * Format: [HexOffset DecimalOffset ByteLength]  FieldName : FieldValue\n */\n\n");
+        AcpiOsPrintf (" * Format: [HexOffset DecimalOffset ByteLength]  "
+            "FieldName : FieldValue\n */\n\n");
 
         AcpiDmDumpDataTable (Table);
         fprintf (stderr, "Acpi Data Table [%4.4s] decoded, written to \"%s\"\n",
@@ -403,15 +404,17 @@
             AcpiOsPrintf ("*****/\n");
         }
 
-        /*
-         * Load namespace from names created within control methods
-         */
-        AcpiDmFinishNamespaceLoad (AcpiGbl_ParseOpRoot, AcpiGbl_RootNode, OwnerId);
+        /* Load namespace from names created within control methods */
+
+        AcpiDmFinishNamespaceLoad (AcpiGbl_ParseOpRoot,
+            AcpiGbl_RootNode, OwnerId);
 
         /*
-         * Cross reference the namespace here, in order to generate External() statements
+         * Cross reference the namespace here, in order to
+         * generate External() statements
          */
-        AcpiDmCrossReferenceNamespace (AcpiGbl_ParseOpRoot, AcpiGbl_RootNode, OwnerId);
+        AcpiDmCrossReferenceNamespace (AcpiGbl_ParseOpRoot,
+            AcpiGbl_RootNode, OwnerId);
 
         if (AslCompilerdebug)
         {
@@ -422,24 +425,20 @@
 
         AcpiDmFindOrphanMethods (AcpiGbl_ParseOpRoot);
 
-        /* Convert fixed-offset references to resource descriptors to symbolic references */
-
-        AcpiDmConvertResourceIndexes (AcpiGbl_ParseOpRoot, AcpiGbl_RootNode);
-
         /*
-         * If we found any external control methods, we must reparse the entire
-         * tree with the new information (namely, the number of arguments per
-         * method)
+         * If we found any external control methods, we must reparse
+         * the entire tree with the new information (namely, the
+         * number of arguments per method)
          */
         if (AcpiDmGetExternalMethodCount ())
         {
             fprintf (stderr,
-                "\nFound %u external control methods, reparsing with new information\n",
+                "\nFound %u external control methods, "
+                "reparsing with new information\n",
                 AcpiDmGetExternalMethodCount ());
 
-            /*
-             * Reparse, rebuild namespace. no need to xref namespace
-             */
+            /* Reparse, rebuild namespace. no need to xref namespace */
+
             AcpiPsDeleteParseTree (AcpiGbl_ParseOpRoot);
             AcpiNsDeleteNamespaceSubtree (AcpiGbl_RootNode);
 
@@ -456,7 +455,7 @@
             Status = AcpiNsRootInitialize ();
             AcpiDmAddExternalsToNamespace ();
 
-            /* Parse table. No need to reload it, however (FALSE) */
+            /* Parse the table again. No need to reload it, however */
 
             Status = AdParseTable (Table, NULL, FALSE, FALSE);
             if (ACPI_FAILURE (Status))
@@ -477,6 +476,15 @@
             }
         }
 
+        /*
+         * Now that the namespace is finalized, we can perform namespace
+         * transforms.
+         *
+         * 1) Convert fixed-offset references to resource descriptors
+         *    to symbolic references (Note: modifies namespace)
+         */
+        AcpiDmConvertResourceIndexes (AcpiGbl_ParseOpRoot, AcpiGbl_RootNode);
+
         /* Optional displays */
 
         if (AcpiGbl_DbOpt_disasm)
@@ -502,11 +510,12 @@
 
     if (OutToFile && File)
     {
+        if (AslCompilerdebug) /* Display final namespace, with transforms */
+        {
+            LsSetupNsList (File);
+            LsDisplayNamespace ();
+        }
 
-#ifdef ASL_DISASM_DEBUG
-        LsSetupNsList (File);
-        LsDisplayNamespace ();
-#endif
         fclose (File);
         AcpiOsRedirectOutput (stdout);
     }
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/common/adwalk.c
--- a/head/sys/contrib/dev/acpica/common/adwalk.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/common/adwalk.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/common/dmextern.c
--- a/head/sys/contrib/dev/acpica/common/dmextern.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/common/dmextern.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/common/dmrestag.c
--- a/head/sys/contrib/dev/acpica/common/dmrestag.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/common/dmrestag.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/common/dmtable.c
--- a/head/sys/contrib/dev/acpica/common/dmtable.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/common/dmtable.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -232,7 +232,7 @@
 };
 
 
-#define ACPI_FADT_PM_RESERVED       8
+#define ACPI_FADT_PM_RESERVED       9
 
 static const char           *AcpiDmFadtProfiles[] =
 {
@@ -244,6 +244,7 @@
     "SOHO Server",
     "Appliance PC",
     "Performance Server",
+    "Tablet",
     "Unknown Profile Type"
 };
 
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/common/dmtbdump.c
--- a/head/sys/contrib/dev/acpica/common/dmtbdump.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/common/dmtbdump.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/common/dmtbinfo.c
--- a/head/sys/contrib/dev/acpica/common/dmtbinfo.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/common/dmtbinfo.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -78,123 +78,123 @@
 /*
  * Macros used to generate offsets to specific table fields
  */
-#define ACPI_FACS_OFFSET(f)             (UINT8) ACPI_OFFSET (ACPI_TABLE_FACS,f)
-#define ACPI_GAS_OFFSET(f)              (UINT8) ACPI_OFFSET (ACPI_GENERIC_ADDRESS,f)
-#define ACPI_HDR_OFFSET(f)              (UINT8) ACPI_OFFSET (ACPI_TABLE_HEADER,f)
-#define ACPI_RSDP_OFFSET(f)             (UINT8) ACPI_OFFSET (ACPI_TABLE_RSDP,f)
-#define ACPI_BERT_OFFSET(f)             (UINT8) ACPI_OFFSET (ACPI_TABLE_BERT,f)
-#define ACPI_BGRT_OFFSET(f)             (UINT8) ACPI_OFFSET (ACPI_TABLE_BGRT,f)
-#define ACPI_BOOT_OFFSET(f)             (UINT8) ACPI_OFFSET (ACPI_TABLE_BOOT,f)
-#define ACPI_CPEP_OFFSET(f)             (UINT8) ACPI_OFFSET (ACPI_TABLE_CPEP,f)
-#define ACPI_DBGP_OFFSET(f)             (UINT8) ACPI_OFFSET (ACPI_TABLE_DBGP,f)
-#define ACPI_DMAR_OFFSET(f)             (UINT8) ACPI_OFFSET (ACPI_TABLE_DMAR,f)
-#define ACPI_DRTM_OFFSET(f)             (UINT8) ACPI_OFFSET (ACPI_TABLE_DRTM,f)
-#define ACPI_ECDT_OFFSET(f)             (UINT8) ACPI_OFFSET (ACPI_TABLE_ECDT,f)
-#define ACPI_EINJ_OFFSET(f)             (UINT8) ACPI_OFFSET (ACPI_TABLE_EINJ,f)
-#define ACPI_ERST_OFFSET(f)             (UINT8) ACPI_OFFSET (ACPI_TABLE_ERST,f)
-#define ACPI_GTDT_OFFSET(f)             (UINT8) ACPI_OFFSET (ACPI_TABLE_GTDT,f)
-#define ACPI_HEST_OFFSET(f)             (UINT8) ACPI_OFFSET (ACPI_TABLE_HEST,f)
-#define ACPI_HPET_OFFSET(f)             (UINT8) ACPI_OFFSET (ACPI_TABLE_HPET,f)
-#define ACPI_IVRS_OFFSET(f)             (UINT8) ACPI_OFFSET (ACPI_TABLE_IVRS,f)
-#define ACPI_MADT_OFFSET(f)             (UINT8) ACPI_OFFSET (ACPI_TABLE_MADT,f)
-#define ACPI_MCFG_OFFSET(f)             (UINT8) ACPI_OFFSET (ACPI_TABLE_MCFG,f)
-#define ACPI_MCHI_OFFSET(f)             (UINT8) ACPI_OFFSET (ACPI_TABLE_MCHI,f)
-#define ACPI_MPST_OFFSET(f)             (UINT8) ACPI_OFFSET (ACPI_TABLE_MPST,f)
-#define ACPI_MSCT_OFFSET(f)             (UINT8) ACPI_OFFSET (ACPI_TABLE_MSCT,f)
-#define ACPI_PCCT_OFFSET(f)             (UINT8) ACPI_OFFSET (ACPI_TABLE_PCCT,f)
-#define ACPI_PMTT_OFFSET(f)             (UINT8) ACPI_OFFSET (ACPI_TABLE_PMTT,f)
-#define ACPI_S3PT_OFFSET(f)             (UINT8) ACPI_OFFSET (ACPI_TABLE_S3PT,f)
-#define ACPI_SBST_OFFSET(f)             (UINT8) ACPI_OFFSET (ACPI_TABLE_SBST,f)
-#define ACPI_SLIT_OFFSET(f)             (UINT8) ACPI_OFFSET (ACPI_TABLE_SLIT,f)
-#define ACPI_SPCR_OFFSET(f)             (UINT8) ACPI_OFFSET (ACPI_TABLE_SPCR,f)
-#define ACPI_SPMI_OFFSET(f)             (UINT8) ACPI_OFFSET (ACPI_TABLE_SPMI,f)
-#define ACPI_SRAT_OFFSET(f)             (UINT8) ACPI_OFFSET (ACPI_TABLE_SRAT,f)
-#define ACPI_TCPA_OFFSET(f)             (UINT8) ACPI_OFFSET (ACPI_TABLE_TCPA,f)
-#define ACPI_UEFI_OFFSET(f)             (UINT8) ACPI_OFFSET (ACPI_TABLE_UEFI,f)
-#define ACPI_WAET_OFFSET(f)             (UINT8) ACPI_OFFSET (ACPI_TABLE_WAET,f)
-#define ACPI_WDAT_OFFSET(f)             (UINT8) ACPI_OFFSET (ACPI_TABLE_WDAT,f)
-#define ACPI_WDDT_OFFSET(f)             (UINT8) ACPI_OFFSET (ACPI_TABLE_WDDT,f)
-#define ACPI_WDRT_OFFSET(f)             (UINT8) ACPI_OFFSET (ACPI_TABLE_WDRT,f)
+#define ACPI_FACS_OFFSET(f)             (UINT16) ACPI_OFFSET (ACPI_TABLE_FACS,f)
+#define ACPI_GAS_OFFSET(f)              (UINT16) ACPI_OFFSET (ACPI_GENERIC_ADDRESS,f)
+#define ACPI_HDR_OFFSET(f)              (UINT16) ACPI_OFFSET (ACPI_TABLE_HEADER,f)
+#define ACPI_RSDP_OFFSET(f)             (UINT16) ACPI_OFFSET (ACPI_TABLE_RSDP,f)
+#define ACPI_BERT_OFFSET(f)             (UINT16) ACPI_OFFSET (ACPI_TABLE_BERT,f)
+#define ACPI_BGRT_OFFSET(f)             (UINT16) ACPI_OFFSET (ACPI_TABLE_BGRT,f)
+#define ACPI_BOOT_OFFSET(f)             (UINT16) ACPI_OFFSET (ACPI_TABLE_BOOT,f)
+#define ACPI_CPEP_OFFSET(f)             (UINT16) ACPI_OFFSET (ACPI_TABLE_CPEP,f)
+#define ACPI_DBGP_OFFSET(f)             (UINT16) ACPI_OFFSET (ACPI_TABLE_DBGP,f)
+#define ACPI_DMAR_OFFSET(f)             (UINT16) ACPI_OFFSET (ACPI_TABLE_DMAR,f)
+#define ACPI_DRTM_OFFSET(f)             (UINT16) ACPI_OFFSET (ACPI_TABLE_DRTM,f)
+#define ACPI_ECDT_OFFSET(f)             (UINT16) ACPI_OFFSET (ACPI_TABLE_ECDT,f)
+#define ACPI_EINJ_OFFSET(f)             (UINT16) ACPI_OFFSET (ACPI_TABLE_EINJ,f)
+#define ACPI_ERST_OFFSET(f)             (UINT16) ACPI_OFFSET (ACPI_TABLE_ERST,f)
+#define ACPI_GTDT_OFFSET(f)             (UINT16) ACPI_OFFSET (ACPI_TABLE_GTDT,f)
+#define ACPI_HEST_OFFSET(f)             (UINT16) ACPI_OFFSET (ACPI_TABLE_HEST,f)
+#define ACPI_HPET_OFFSET(f)             (UINT16) ACPI_OFFSET (ACPI_TABLE_HPET,f)
+#define ACPI_IVRS_OFFSET(f)             (UINT16) ACPI_OFFSET (ACPI_TABLE_IVRS,f)
+#define ACPI_MADT_OFFSET(f)             (UINT16) ACPI_OFFSET (ACPI_TABLE_MADT,f)
+#define ACPI_MCFG_OFFSET(f)             (UINT16) ACPI_OFFSET (ACPI_TABLE_MCFG,f)
+#define ACPI_MCHI_OFFSET(f)             (UINT16) ACPI_OFFSET (ACPI_TABLE_MCHI,f)
+#define ACPI_MPST_OFFSET(f)             (UINT16) ACPI_OFFSET (ACPI_TABLE_MPST,f)
+#define ACPI_MSCT_OFFSET(f)             (UINT16) ACPI_OFFSET (ACPI_TABLE_MSCT,f)
+#define ACPI_PCCT_OFFSET(f)             (UINT16) ACPI_OFFSET (ACPI_TABLE_PCCT,f)
+#define ACPI_PMTT_OFFSET(f)             (UINT16) ACPI_OFFSET (ACPI_TABLE_PMTT,f)
+#define ACPI_S3PT_OFFSET(f)             (UINT16) ACPI_OFFSET (ACPI_TABLE_S3PT,f)
+#define ACPI_SBST_OFFSET(f)             (UINT16) ACPI_OFFSET (ACPI_TABLE_SBST,f)
+#define ACPI_SLIT_OFFSET(f)             (UINT16) ACPI_OFFSET (ACPI_TABLE_SLIT,f)
+#define ACPI_SPCR_OFFSET(f)             (UINT16) ACPI_OFFSET (ACPI_TABLE_SPCR,f)
+#define ACPI_SPMI_OFFSET(f)             (UINT16) ACPI_OFFSET (ACPI_TABLE_SPMI,f)
+#define ACPI_SRAT_OFFSET(f)             (UINT16) ACPI_OFFSET (ACPI_TABLE_SRAT,f)
+#define ACPI_TCPA_OFFSET(f)             (UINT16) ACPI_OFFSET (ACPI_TABLE_TCPA,f)
+#define ACPI_UEFI_OFFSET(f)             (UINT16) ACPI_OFFSET (ACPI_TABLE_UEFI,f)
+#define ACPI_WAET_OFFSET(f)             (UINT16) ACPI_OFFSET (ACPI_TABLE_WAET,f)
+#define ACPI_WDAT_OFFSET(f)             (UINT16) ACPI_OFFSET (ACPI_TABLE_WDAT,f)
+#define ACPI_WDDT_OFFSET(f)             (UINT16) ACPI_OFFSET (ACPI_TABLE_WDDT,f)
+#define ACPI_WDRT_OFFSET(f)             (UINT16) ACPI_OFFSET (ACPI_TABLE_WDRT,f)
 
 /* Subtables */
 
-#define ACPI_ASF0_OFFSET(f)             (UINT8) ACPI_OFFSET (ACPI_ASF_INFO,f)
-#define ACPI_ASF1_OFFSET(f)             (UINT8) ACPI_OFFSET (ACPI_ASF_ALERT,f)
-#define ACPI_ASF1a_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_ASF_ALERT_DATA,f)
-#define ACPI_ASF2_OFFSET(f)             (UINT8) ACPI_OFFSET (ACPI_ASF_REMOTE,f)
-#define ACPI_ASF2a_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_ASF_CONTROL_DATA,f)
-#define ACPI_ASF3_OFFSET(f)             (UINT8) ACPI_OFFSET (ACPI_ASF_RMCP,f)
-#define ACPI_ASF4_OFFSET(f)             (UINT8) ACPI_OFFSET (ACPI_ASF_ADDRESS,f)
-#define ACPI_CPEP0_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_CPEP_POLLING,f)
-#define ACPI_DMARS_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_DMAR_DEVICE_SCOPE,f)
-#define ACPI_DMAR0_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_DMAR_HARDWARE_UNIT,f)
-#define ACPI_DMAR1_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_DMAR_RESERVED_MEMORY,f)
-#define ACPI_DMAR2_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_DMAR_ATSR,f)
-#define ACPI_DMAR3_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_DMAR_RHSA,f)
-#define ACPI_EINJ0_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_WHEA_HEADER,f)
-#define ACPI_ERST0_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_WHEA_HEADER,f)
-#define ACPI_FPDTH_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_FPDT_HEADER,f)
-#define ACPI_FPDT0_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_FPDT_BOOT,f)
-#define ACPI_FPDT1_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_FPDT_S3PT_PTR,f)
-#define ACPI_HEST0_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_HEST_IA_MACHINE_CHECK,f)
-#define ACPI_HEST1_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_HEST_IA_CORRECTED,f)
-#define ACPI_HEST2_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_HEST_IA_NMI,f)
-#define ACPI_HEST6_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_HEST_AER_ROOT,f)
-#define ACPI_HEST7_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_HEST_AER,f)
-#define ACPI_HEST8_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_HEST_AER_BRIDGE,f)
-#define ACPI_HEST9_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_HEST_GENERIC,f)
-#define ACPI_HESTN_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_HEST_NOTIFY,f)
-#define ACPI_HESTB_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_HEST_IA_ERROR_BANK,f)
-#define ACPI_IVRSH_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_IVRS_HEADER,f)
-#define ACPI_IVRS0_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_IVRS_HARDWARE,f)
-#define ACPI_IVRS1_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_IVRS_MEMORY,f)
-#define ACPI_IVRSD_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_IVRS_DE_HEADER,f)
-#define ACPI_IVRS8A_OFFSET(f)           (UINT8) ACPI_OFFSET (ACPI_IVRS_DEVICE8A,f)
-#define ACPI_IVRS8B_OFFSET(f)           (UINT8) ACPI_OFFSET (ACPI_IVRS_DEVICE8B,f)
-#define ACPI_IVRS8C_OFFSET(f)           (UINT8) ACPI_OFFSET (ACPI_IVRS_DEVICE8C,f)
-#define ACPI_MADT0_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_MADT_LOCAL_APIC,f)
-#define ACPI_MADT1_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_MADT_IO_APIC,f)
-#define ACPI_MADT2_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_MADT_INTERRUPT_OVERRIDE,f)
-#define ACPI_MADT3_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_MADT_NMI_SOURCE,f)
-#define ACPI_MADT4_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_MADT_LOCAL_APIC_NMI,f)
-#define ACPI_MADT5_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_MADT_LOCAL_APIC_OVERRIDE,f)
-#define ACPI_MADT6_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_MADT_IO_SAPIC,f)
-#define ACPI_MADT7_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_MADT_LOCAL_SAPIC,f)
-#define ACPI_MADT8_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_MADT_INTERRUPT_SOURCE,f)
-#define ACPI_MADT9_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_MADT_LOCAL_X2APIC,f)
-#define ACPI_MADT10_OFFSET(f)           (UINT8) ACPI_OFFSET (ACPI_MADT_LOCAL_X2APIC_NMI,f)
-#define ACPI_MADT11_OFFSET(f)           (UINT8) ACPI_OFFSET (ACPI_MADT_GENERIC_INTERRUPT,f)
-#define ACPI_MADT12_OFFSET(f)           (UINT8) ACPI_OFFSET (ACPI_MADT_GENERIC_DISTRIBUTOR,f)
-#define ACPI_MADTH_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_SUBTABLE_HEADER,f)
-#define ACPI_MCFG0_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_MCFG_ALLOCATION,f)
-#define ACPI_MPST0_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_MPST_POWER_NODE,f)
-#define ACPI_MPST0A_OFFSET(f)           (UINT8) ACPI_OFFSET (ACPI_MPST_POWER_STATE,f)
-#define ACPI_MPST0B_OFFSET(f)           (UINT8) ACPI_OFFSET (ACPI_MPST_COMPONENT,f)
-#define ACPI_MPST1_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_MPST_DATA_HDR,f)
-#define ACPI_MPST2_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_MPST_POWER_DATA,f)
-#define ACPI_MSCT0_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_MSCT_PROXIMITY,f)
-#define ACPI_PCCT0_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_PCCT_SUBSPACE,f)
-#define ACPI_PMTT0_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_PMTT_SOCKET,f)
-#define ACPI_PMTT1_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_PMTT_CONTROLLER,f)
-#define ACPI_PMTT1A_OFFSET(f)           (UINT8) ACPI_OFFSET (ACPI_PMTT_DOMAIN,f)
-#define ACPI_PMTT2_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_PMTT_PHYSICAL_COMPONENT,f)
-#define ACPI_PMTTH_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_PMTT_HEADER,f)
-#define ACPI_S3PTH_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_S3PT_HEADER,f)
-#define ACPI_S3PT0_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_S3PT_RESUME,f)
-#define ACPI_S3PT1_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_S3PT_SUSPEND,f)
-#define ACPI_SLICH_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_SLIC_HEADER,f)
-#define ACPI_SLIC0_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_SLIC_KEY,f)
-#define ACPI_SLIC1_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_SLIC_MARKER,f)
-#define ACPI_SRATH_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_SUBTABLE_HEADER,f)
-#define ACPI_SRAT0_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_SRAT_CPU_AFFINITY,f)
-#define ACPI_SRAT1_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_SRAT_MEM_AFFINITY,f)
-#define ACPI_SRAT2_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_SRAT_X2APIC_CPU_AFFINITY,f)
-#define ACPI_WDAT0_OFFSET(f)            (UINT8) ACPI_OFFSET (ACPI_WDAT_ENTRY,f)
+#define ACPI_ASF0_OFFSET(f)             (UINT16) ACPI_OFFSET (ACPI_ASF_INFO,f)
+#define ACPI_ASF1_OFFSET(f)             (UINT16) ACPI_OFFSET (ACPI_ASF_ALERT,f)
+#define ACPI_ASF1a_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_ASF_ALERT_DATA,f)
+#define ACPI_ASF2_OFFSET(f)             (UINT16) ACPI_OFFSET (ACPI_ASF_REMOTE,f)
+#define ACPI_ASF2a_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_ASF_CONTROL_DATA,f)
+#define ACPI_ASF3_OFFSET(f)             (UINT16) ACPI_OFFSET (ACPI_ASF_RMCP,f)
+#define ACPI_ASF4_OFFSET(f)             (UINT16) ACPI_OFFSET (ACPI_ASF_ADDRESS,f)
+#define ACPI_CPEP0_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_CPEP_POLLING,f)
+#define ACPI_DMARS_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_DMAR_DEVICE_SCOPE,f)
+#define ACPI_DMAR0_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_DMAR_HARDWARE_UNIT,f)
+#define ACPI_DMAR1_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_DMAR_RESERVED_MEMORY,f)
+#define ACPI_DMAR2_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_DMAR_ATSR,f)
+#define ACPI_DMAR3_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_DMAR_RHSA,f)
+#define ACPI_EINJ0_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_WHEA_HEADER,f)
+#define ACPI_ERST0_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_WHEA_HEADER,f)
+#define ACPI_FPDTH_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_FPDT_HEADER,f)
+#define ACPI_FPDT0_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_FPDT_BOOT,f)
+#define ACPI_FPDT1_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_FPDT_S3PT_PTR,f)
+#define ACPI_HEST0_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_HEST_IA_MACHINE_CHECK,f)
+#define ACPI_HEST1_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_HEST_IA_CORRECTED,f)
+#define ACPI_HEST2_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_HEST_IA_NMI,f)
+#define ACPI_HEST6_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_HEST_AER_ROOT,f)
+#define ACPI_HEST7_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_HEST_AER,f)
+#define ACPI_HEST8_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_HEST_AER_BRIDGE,f)
+#define ACPI_HEST9_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_HEST_GENERIC,f)
+#define ACPI_HESTN_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_HEST_NOTIFY,f)
+#define ACPI_HESTB_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_HEST_IA_ERROR_BANK,f)
+#define ACPI_IVRSH_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_IVRS_HEADER,f)
+#define ACPI_IVRS0_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_IVRS_HARDWARE,f)
+#define ACPI_IVRS1_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_IVRS_MEMORY,f)
+#define ACPI_IVRSD_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_IVRS_DE_HEADER,f)
+#define ACPI_IVRS8A_OFFSET(f)           (UINT16) ACPI_OFFSET (ACPI_IVRS_DEVICE8A,f)
+#define ACPI_IVRS8B_OFFSET(f)           (UINT16) ACPI_OFFSET (ACPI_IVRS_DEVICE8B,f)
+#define ACPI_IVRS8C_OFFSET(f)           (UINT16) ACPI_OFFSET (ACPI_IVRS_DEVICE8C,f)
+#define ACPI_MADT0_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_MADT_LOCAL_APIC,f)
+#define ACPI_MADT1_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_MADT_IO_APIC,f)
+#define ACPI_MADT2_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_MADT_INTERRUPT_OVERRIDE,f)
+#define ACPI_MADT3_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_MADT_NMI_SOURCE,f)
+#define ACPI_MADT4_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_MADT_LOCAL_APIC_NMI,f)
+#define ACPI_MADT5_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_MADT_LOCAL_APIC_OVERRIDE,f)
+#define ACPI_MADT6_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_MADT_IO_SAPIC,f)
+#define ACPI_MADT7_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_MADT_LOCAL_SAPIC,f)
+#define ACPI_MADT8_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_MADT_INTERRUPT_SOURCE,f)
+#define ACPI_MADT9_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_MADT_LOCAL_X2APIC,f)
+#define ACPI_MADT10_OFFSET(f)           (UINT16) ACPI_OFFSET (ACPI_MADT_LOCAL_X2APIC_NMI,f)
+#define ACPI_MADT11_OFFSET(f)           (UINT16) ACPI_OFFSET (ACPI_MADT_GENERIC_INTERRUPT,f)
+#define ACPI_MADT12_OFFSET(f)           (UINT16) ACPI_OFFSET (ACPI_MADT_GENERIC_DISTRIBUTOR,f)
+#define ACPI_MADTH_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_SUBTABLE_HEADER,f)
+#define ACPI_MCFG0_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_MCFG_ALLOCATION,f)
+#define ACPI_MPST0_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_MPST_POWER_NODE,f)
+#define ACPI_MPST0A_OFFSET(f)           (UINT16) ACPI_OFFSET (ACPI_MPST_POWER_STATE,f)
+#define ACPI_MPST0B_OFFSET(f)           (UINT16) ACPI_OFFSET (ACPI_MPST_COMPONENT,f)
+#define ACPI_MPST1_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_MPST_DATA_HDR,f)
+#define ACPI_MPST2_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_MPST_POWER_DATA,f)
+#define ACPI_MSCT0_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_MSCT_PROXIMITY,f)
+#define ACPI_PCCT0_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_PCCT_SUBSPACE,f)
+#define ACPI_PMTT0_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_PMTT_SOCKET,f)
+#define ACPI_PMTT1_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_PMTT_CONTROLLER,f)
+#define ACPI_PMTT1A_OFFSET(f)           (UINT16) ACPI_OFFSET (ACPI_PMTT_DOMAIN,f)
+#define ACPI_PMTT2_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_PMTT_PHYSICAL_COMPONENT,f)
+#define ACPI_PMTTH_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_PMTT_HEADER,f)
+#define ACPI_S3PTH_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_S3PT_HEADER,f)
+#define ACPI_S3PT0_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_S3PT_RESUME,f)
+#define ACPI_S3PT1_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_S3PT_SUSPEND,f)
+#define ACPI_SLICH_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_SLIC_HEADER,f)
+#define ACPI_SLIC0_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_SLIC_KEY,f)
+#define ACPI_SLIC1_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_SLIC_MARKER,f)
+#define ACPI_SRATH_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_SUBTABLE_HEADER,f)
+#define ACPI_SRAT0_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_SRAT_CPU_AFFINITY,f)
+#define ACPI_SRAT1_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_SRAT_MEM_AFFINITY,f)
+#define ACPI_SRAT2_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_SRAT_X2APIC_CPU_AFFINITY,f)
+#define ACPI_WDAT0_OFFSET(f)            (UINT16) ACPI_OFFSET (ACPI_WDAT_ENTRY,f)
 
 /*
  * Simplify access to flag fields by breaking them up into bytes
  */
-#define ACPI_FLAG_OFFSET(d,f,o)         (UINT8) (ACPI_OFFSET (d,f) + o)
+#define ACPI_FLAG_OFFSET(d,f,o)         (UINT16) (ACPI_OFFSET (d,f) + o)
 
 /* Flags */
 
@@ -384,6 +384,7 @@
     {ACPI_DMT_FLAG2,    ACPI_FADT_FLAG_OFFSET (BootFlags,0),        "VGA Not Present (V4)", 0},
     {ACPI_DMT_FLAG3,    ACPI_FADT_FLAG_OFFSET (BootFlags,0),        "MSI Not Supported (V4)", 0},
     {ACPI_DMT_FLAG4,    ACPI_FADT_FLAG_OFFSET (BootFlags,0),        "PCIe ASPM Not Supported (V4)", 0},
+    {ACPI_DMT_FLAG5,    ACPI_FADT_FLAG_OFFSET (BootFlags,0),        "CMOS RTC Not Present (V5)", 0},
 
     {ACPI_DMT_UINT8,    ACPI_FADT_OFFSET (Reserved),                "Reserved", 0},
     {ACPI_DMT_UINT32,   ACPI_FADT_OFFSET (Flags),                   "Flags (decoded below)", DT_FLAG},
@@ -417,8 +418,7 @@
     {ACPI_DMT_FLAG2,    ACPI_FADT_FLAG_OFFSET (Flags,2),            "Use APIC Cluster Model (V4)", 0},
     {ACPI_DMT_FLAG3,    ACPI_FADT_FLAG_OFFSET (Flags,2),            "Use APIC Physical Destination Mode (V4)", 0},
     {ACPI_DMT_FLAG4,    ACPI_FADT_FLAG_OFFSET (Flags,2),            "Hardware Reduced (V5)", 0},
-    {ACPI_DMT_FLAG5,    ACPI_FADT_FLAG_OFFSET (Flags,2),            "Prefer S0 Idle (V5)", 0},
-    {ACPI_DMT_FLAG6,    ACPI_FADT_FLAG_OFFSET (Flags,2),            "Use Sleep Register (V5)", 0},
+    {ACPI_DMT_FLAG5,    ACPI_FADT_FLAG_OFFSET (Flags,2),            "Low Power S0 Idle (V5)", 0},
     ACPI_DMT_TERMINATOR
 };
 
@@ -456,7 +456,8 @@
 
 ACPI_DMTABLE_INFO           AcpiDmTableInfoFadt5[] =
 {
-    {ACPI_DMT_GAS,      ACPI_FADT_OFFSET (SleepRegister),           "Sleep Register", 0},
+    {ACPI_DMT_GAS,      ACPI_FADT_OFFSET (SleepControl),            "Sleep Control Register", 0},
+    {ACPI_DMT_GAS,      ACPI_FADT_OFFSET (SleepStatus),             "Sleep Status Register", 0},
     ACPI_DMT_TERMINATOR
 };
 
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/common/getopt.c
--- a/head/sys/contrib/dev/acpica/common/getopt.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/common/getopt.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/compiler/aslanalyze.c
--- a/head/sys/contrib/dev/acpica/compiler/aslanalyze.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/compiler/aslanalyze.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/compiler/aslbtypes.c
--- a/head/sys/contrib/dev/acpica/compiler/aslbtypes.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/compiler/aslbtypes.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/compiler/aslcodegen.c
--- a/head/sys/contrib/dev/acpica/compiler/aslcodegen.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/compiler/aslcodegen.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/compiler/aslcompile.c
--- a/head/sys/contrib/dev/acpica/compiler/aslcompile.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/compiler/aslcompile.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/compiler/aslcompiler.h
--- a/head/sys/contrib/dev/acpica/compiler/aslcompiler.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/compiler/aslcompiler.h	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/compiler/aslcompiler.l
--- a/head/sys/contrib/dev/acpica/compiler/aslcompiler.l	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/compiler/aslcompiler.l	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/compiler/aslcompiler.y
--- a/head/sys/contrib/dev/acpica/compiler/aslcompiler.y	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/compiler/aslcompiler.y	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/compiler/asldefine.h
--- a/head/sys/contrib/dev/acpica/compiler/asldefine.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/compiler/asldefine.h	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/compiler/aslerror.c
--- a/head/sys/contrib/dev/acpica/compiler/aslerror.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/compiler/aslerror.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/compiler/aslfiles.c
--- a/head/sys/contrib/dev/acpica/compiler/aslfiles.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/compiler/aslfiles.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/compiler/aslfold.c
--- a/head/sys/contrib/dev/acpica/compiler/aslfold.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/compiler/aslfold.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/compiler/aslglobal.h
--- a/head/sys/contrib/dev/acpica/compiler/aslglobal.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/compiler/aslglobal.h	Fri Jan 13 15:51:01 2012 +0200
@@ -7,7 +7,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/compiler/asllength.c
--- a/head/sys/contrib/dev/acpica/compiler/asllength.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/compiler/asllength.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/compiler/asllisting.c
--- a/head/sys/contrib/dev/acpica/compiler/asllisting.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/compiler/asllisting.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/compiler/aslload.c
--- a/head/sys/contrib/dev/acpica/compiler/aslload.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/compiler/aslload.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/compiler/asllookup.c
--- a/head/sys/contrib/dev/acpica/compiler/asllookup.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/compiler/asllookup.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/compiler/aslmain.c
--- a/head/sys/contrib/dev/acpica/compiler/aslmain.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/compiler/aslmain.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -879,6 +879,10 @@
     if (Gbl_DoSignon)
     {
         printf (ACPI_COMMON_SIGNON (ASL_COMPILER_NAME));
+        if (Gbl_IgnoreErrors)
+        {
+            printf ("Ignoring all errors, forcing AML file generation\n\n");
+        }
     }
 
     /* Abort if anything went wrong on the command line */
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/compiler/aslmap.c
--- a/head/sys/contrib/dev/acpica/compiler/aslmap.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/compiler/aslmap.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/compiler/aslmessages.h
--- a/head/sys/contrib/dev/acpica/compiler/aslmessages.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/compiler/aslmessages.h	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/compiler/aslopcodes.c
--- a/head/sys/contrib/dev/acpica/compiler/aslopcodes.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/compiler/aslopcodes.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/compiler/asloperands.c
--- a/head/sys/contrib/dev/acpica/compiler/asloperands.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/compiler/asloperands.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/compiler/aslopt.c
--- a/head/sys/contrib/dev/acpica/compiler/aslopt.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/compiler/aslopt.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/compiler/aslpredef.c
--- a/head/sys/contrib/dev/acpica/compiler/aslpredef.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/compiler/aslpredef.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/compiler/aslresource.c
--- a/head/sys/contrib/dev/acpica/compiler/aslresource.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/compiler/aslresource.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/compiler/aslrestype1.c
--- a/head/sys/contrib/dev/acpica/compiler/aslrestype1.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/compiler/aslrestype1.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/compiler/aslrestype1i.c
--- a/head/sys/contrib/dev/acpica/compiler/aslrestype1i.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/compiler/aslrestype1i.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/compiler/aslrestype2.c
--- a/head/sys/contrib/dev/acpica/compiler/aslrestype2.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/compiler/aslrestype2.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/compiler/aslrestype2d.c
--- a/head/sys/contrib/dev/acpica/compiler/aslrestype2d.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/compiler/aslrestype2d.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/compiler/aslrestype2e.c
--- a/head/sys/contrib/dev/acpica/compiler/aslrestype2e.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/compiler/aslrestype2e.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/compiler/aslrestype2q.c
--- a/head/sys/contrib/dev/acpica/compiler/aslrestype2q.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/compiler/aslrestype2q.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/compiler/aslrestype2s.c
--- a/head/sys/contrib/dev/acpica/compiler/aslrestype2s.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/compiler/aslrestype2s.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/compiler/aslrestype2w.c
--- a/head/sys/contrib/dev/acpica/compiler/aslrestype2w.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/compiler/aslrestype2w.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/compiler/aslstartup.c
--- a/head/sys/contrib/dev/acpica/compiler/aslstartup.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/compiler/aslstartup.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/compiler/aslstubs.c
--- a/head/sys/contrib/dev/acpica/compiler/aslstubs.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/compiler/aslstubs.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/compiler/asltransform.c
--- a/head/sys/contrib/dev/acpica/compiler/asltransform.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/compiler/asltransform.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/compiler/asltree.c
--- a/head/sys/contrib/dev/acpica/compiler/asltree.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/compiler/asltree.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/compiler/asltypes.h
--- a/head/sys/contrib/dev/acpica/compiler/asltypes.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/compiler/asltypes.h	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/compiler/aslutils.c
--- a/head/sys/contrib/dev/acpica/compiler/aslutils.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/compiler/aslutils.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/compiler/asluuid.c
--- a/head/sys/contrib/dev/acpica/compiler/asluuid.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/compiler/asluuid.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/compiler/aslwalks.c
--- a/head/sys/contrib/dev/acpica/compiler/aslwalks.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/compiler/aslwalks.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/compiler/dtcompile.c
--- a/head/sys/contrib/dev/acpica/compiler/dtcompile.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/compiler/dtcompile.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/compiler/dtcompiler.h
--- a/head/sys/contrib/dev/acpica/compiler/dtcompiler.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/compiler/dtcompiler.h	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/compiler/dtexpress.c
--- a/head/sys/contrib/dev/acpica/compiler/dtexpress.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/compiler/dtexpress.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/compiler/dtfield.c
--- a/head/sys/contrib/dev/acpica/compiler/dtfield.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/compiler/dtfield.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/compiler/dtio.c
--- a/head/sys/contrib/dev/acpica/compiler/dtio.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/compiler/dtio.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/compiler/dtparser.l
--- a/head/sys/contrib/dev/acpica/compiler/dtparser.l	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/compiler/dtparser.l	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/compiler/dtparser.y
--- a/head/sys/contrib/dev/acpica/compiler/dtparser.y	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/compiler/dtparser.y	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/compiler/dtsubtable.c
--- a/head/sys/contrib/dev/acpica/compiler/dtsubtable.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/compiler/dtsubtable.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/compiler/dttable.c
--- a/head/sys/contrib/dev/acpica/compiler/dttable.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/compiler/dttable.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -1822,7 +1822,9 @@
             break;
         }
 
-        if (!ACPI_STRCMP (Name, Info->Name))
+        /* Use caseless compare for generic keywords */
+
+        if (!AcpiUtStricmp (Name, Info->Name))
         {
             break;
         }
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/compiler/dttemplate.c
--- a/head/sys/contrib/dev/acpica/compiler/dttemplate.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/compiler/dttemplate.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/compiler/dttemplate.h
--- a/head/sys/contrib/dev/acpica/compiler/dttemplate.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/compiler/dttemplate.h	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -302,13 +302,15 @@
     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00   /* 00000038    "........" */
 };
 
+/* Version 5 FADT */
+
 const unsigned char TemplateFadt[] =
 {
-    0x46,0x41,0x43,0x50,0xF4,0x00,0x00,0x00,  /* 00000000    "FACP...." */
-    0x04,0x4E,0x49,0x4E,0x54,0x45,0x4C,0x20,  /* 00000008    ".NINTEL " */
+    0x46,0x41,0x43,0x50,0x0C,0x01,0x00,0x00,  /* 00000000    "FACP...." */
+    0x05,0x18,0x49,0x4E,0x54,0x45,0x4C,0x20,  /* 00000008    "..INTEL " */
     0x54,0x45,0x4D,0x50,0x4C,0x41,0x54,0x45,  /* 00000010    "TEMPLATE" */
     0x00,0x00,0x00,0x00,0x49,0x4E,0x54,0x4C,  /* 00000018    "....INTL" */
-    0x28,0x05,0x10,0x20,0x01,0x00,0x00,0x00,  /* 00000020    "(.. ...." */
+    0x23,0x11,0x11,0x20,0x01,0x00,0x00,0x00,  /* 00000020    "#.. ...." */
     0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  /* 00000028    "........" */
     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  /* 00000030    "........" */
     0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  /* 00000038    "........" */
@@ -334,7 +336,10 @@
     0x00,0x00,0x00,0x00,0x01,0x80,0x00,0x01,  /* 000000D8    "........" */
     0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  /* 000000E0    "........" */
     0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  /* 000000E8    "........" */
-    0x00,0x00,0x00,0x00                       /* 000000F0    "...."     */
+    0x00,0x00,0x00,0x00,0x01,0x08,0x00,0x01,  /* 000000F0    "........" */
+    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  /* 000000F8    "........" */
+    0x01,0x08,0x00,0x01,0x00,0x00,0x00,0x00,  /* 00000100    "........" */
+    0x00,0x00,0x00,0x00                       /* 00000108    "...."     */
 };
 
 const unsigned char TemplateFpdt[] =
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/compiler/dtutils.c
--- a/head/sys/contrib/dev/acpica/compiler/dtutils.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/compiler/dtutils.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/debugger/dbcmds.c
--- a/head/sys/contrib/dev/acpica/debugger/dbcmds.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/debugger/dbcmds.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/debugger/dbdisply.c
--- a/head/sys/contrib/dev/acpica/debugger/dbdisply.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/debugger/dbdisply.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/debugger/dbexec.c
--- a/head/sys/contrib/dev/acpica/debugger/dbexec.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/debugger/dbexec.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/debugger/dbfileio.c
--- a/head/sys/contrib/dev/acpica/debugger/dbfileio.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/debugger/dbfileio.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/debugger/dbhistry.c
--- a/head/sys/contrib/dev/acpica/debugger/dbhistry.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/debugger/dbhistry.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/debugger/dbinput.c
--- a/head/sys/contrib/dev/acpica/debugger/dbinput.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/debugger/dbinput.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/debugger/dbmethod.c
--- a/head/sys/contrib/dev/acpica/debugger/dbmethod.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/debugger/dbmethod.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/debugger/dbnames.c
--- a/head/sys/contrib/dev/acpica/debugger/dbnames.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/debugger/dbnames.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/debugger/dbstats.c
--- a/head/sys/contrib/dev/acpica/debugger/dbstats.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/debugger/dbstats.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/debugger/dbutils.c
--- a/head/sys/contrib/dev/acpica/debugger/dbutils.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/debugger/dbutils.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/debugger/dbxface.c
--- a/head/sys/contrib/dev/acpica/debugger/dbxface.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/debugger/dbxface.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/disassembler/dmbuffer.c
--- a/head/sys/contrib/dev/acpica/disassembler/dmbuffer.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/disassembler/dmbuffer.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/disassembler/dmnames.c
--- a/head/sys/contrib/dev/acpica/disassembler/dmnames.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/disassembler/dmnames.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/disassembler/dmobject.c
--- a/head/sys/contrib/dev/acpica/disassembler/dmobject.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/disassembler/dmobject.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/disassembler/dmopcode.c
--- a/head/sys/contrib/dev/acpica/disassembler/dmopcode.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/disassembler/dmopcode.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/disassembler/dmresrc.c
--- a/head/sys/contrib/dev/acpica/disassembler/dmresrc.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/disassembler/dmresrc.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/disassembler/dmresrcl.c
--- a/head/sys/contrib/dev/acpica/disassembler/dmresrcl.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/disassembler/dmresrcl.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/disassembler/dmresrcl2.c
--- a/head/sys/contrib/dev/acpica/disassembler/dmresrcl2.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/disassembler/dmresrcl2.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/disassembler/dmresrcs.c
--- a/head/sys/contrib/dev/acpica/disassembler/dmresrcs.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/disassembler/dmresrcs.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/disassembler/dmutils.c
--- a/head/sys/contrib/dev/acpica/disassembler/dmutils.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/disassembler/dmutils.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/disassembler/dmwalk.c
--- a/head/sys/contrib/dev/acpica/disassembler/dmwalk.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/disassembler/dmwalk.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/dispatcher/dsargs.c
--- a/head/sys/contrib/dev/acpica/dispatcher/dsargs.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/dispatcher/dsargs.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -426,5 +426,13 @@
 
     Status = AcpiDsExecuteArguments (Node, ExtraDesc->Extra.ScopeNode,
                 ExtraDesc->Extra.AmlLength, ExtraDesc->Extra.AmlStart);
+    if (ACPI_FAILURE (Status))
+    {
+        return_ACPI_STATUS (Status);
+    }
+
+    Status = AcpiUtAddAddressRange (ObjDesc->Region.SpaceId,
+                 ObjDesc->Region.Address, ObjDesc->Region.Length,
+                 Node);
     return_ACPI_STATUS (Status);
 }
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/dispatcher/dscontrol.c
--- a/head/sys/contrib/dev/acpica/dispatcher/dscontrol.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/dispatcher/dscontrol.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/dispatcher/dsfield.c
--- a/head/sys/contrib/dev/acpica/dispatcher/dsfield.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/dispatcher/dsfield.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/dispatcher/dsinit.c
--- a/head/sys/contrib/dev/acpica/dispatcher/dsinit.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/dispatcher/dsinit.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/dispatcher/dsmethod.c
--- a/head/sys/contrib/dev/acpica/dispatcher/dsmethod.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/dispatcher/dsmethod.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/dispatcher/dsmthdat.c
--- a/head/sys/contrib/dev/acpica/dispatcher/dsmthdat.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/dispatcher/dsmthdat.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/dispatcher/dsobject.c
--- a/head/sys/contrib/dev/acpica/dispatcher/dsobject.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/dispatcher/dsobject.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/dispatcher/dsopcode.c
--- a/head/sys/contrib/dev/acpica/dispatcher/dsopcode.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/dispatcher/dsopcode.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/dispatcher/dsutils.c
--- a/head/sys/contrib/dev/acpica/dispatcher/dsutils.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/dispatcher/dsutils.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/dispatcher/dswexec.c
--- a/head/sys/contrib/dev/acpica/dispatcher/dswexec.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/dispatcher/dswexec.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/dispatcher/dswload.c
--- a/head/sys/contrib/dev/acpica/dispatcher/dswload.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/dispatcher/dswload.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/dispatcher/dswload2.c
--- a/head/sys/contrib/dev/acpica/dispatcher/dswload2.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/dispatcher/dswload2.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/dispatcher/dswscope.c
--- a/head/sys/contrib/dev/acpica/dispatcher/dswscope.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/dispatcher/dswscope.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/dispatcher/dswstate.c
--- a/head/sys/contrib/dev/acpica/dispatcher/dswstate.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/dispatcher/dswstate.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/events/evevent.c
--- a/head/sys/contrib/dev/acpica/events/evevent.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/events/evevent.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/events/evglock.c
--- a/head/sys/contrib/dev/acpica/events/evglock.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/events/evglock.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/events/evgpe.c
--- a/head/sys/contrib/dev/acpica/events/evgpe.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/events/evgpe.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/events/evgpeblk.c
--- a/head/sys/contrib/dev/acpica/events/evgpeblk.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/events/evgpeblk.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/events/evgpeinit.c
--- a/head/sys/contrib/dev/acpica/events/evgpeinit.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/events/evgpeinit.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/events/evgpeutil.c
--- a/head/sys/contrib/dev/acpica/events/evgpeutil.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/events/evgpeutil.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/events/evmisc.c
--- a/head/sys/contrib/dev/acpica/events/evmisc.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/events/evmisc.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/events/evregion.c
--- a/head/sys/contrib/dev/acpica/events/evregion.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/events/evregion.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/events/evrgnini.c
--- a/head/sys/contrib/dev/acpica/events/evrgnini.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/events/evrgnini.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/events/evsci.c
--- a/head/sys/contrib/dev/acpica/events/evsci.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/events/evsci.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/events/evxface.c
--- a/head/sys/contrib/dev/acpica/events/evxface.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/events/evxface.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/events/evxfevnt.c
--- a/head/sys/contrib/dev/acpica/events/evxfevnt.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/events/evxfevnt.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/events/evxfgpe.c
--- a/head/sys/contrib/dev/acpica/events/evxfgpe.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/events/evxfgpe.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/events/evxfregn.c
--- a/head/sys/contrib/dev/acpica/events/evxfregn.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/events/evxfregn.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/executer/exconfig.c
--- a/head/sys/contrib/dev/acpica/executer/exconfig.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/executer/exconfig.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/executer/exconvrt.c
--- a/head/sys/contrib/dev/acpica/executer/exconvrt.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/executer/exconvrt.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/executer/excreate.c
--- a/head/sys/contrib/dev/acpica/executer/excreate.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/executer/excreate.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/executer/exdebug.c
--- a/head/sys/contrib/dev/acpica/executer/exdebug.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/executer/exdebug.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/executer/exdump.c
--- a/head/sys/contrib/dev/acpica/executer/exdump.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/executer/exdump.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/executer/exfield.c
--- a/head/sys/contrib/dev/acpica/executer/exfield.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/executer/exfield.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/executer/exfldio.c
--- a/head/sys/contrib/dev/acpica/executer/exfldio.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/executer/exfldio.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/executer/exmisc.c
--- a/head/sys/contrib/dev/acpica/executer/exmisc.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/executer/exmisc.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/executer/exmutex.c
--- a/head/sys/contrib/dev/acpica/executer/exmutex.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/executer/exmutex.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/executer/exnames.c
--- a/head/sys/contrib/dev/acpica/executer/exnames.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/executer/exnames.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/executer/exoparg1.c
--- a/head/sys/contrib/dev/acpica/executer/exoparg1.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/executer/exoparg1.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/executer/exoparg2.c
--- a/head/sys/contrib/dev/acpica/executer/exoparg2.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/executer/exoparg2.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/executer/exoparg3.c
--- a/head/sys/contrib/dev/acpica/executer/exoparg3.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/executer/exoparg3.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/executer/exoparg6.c
--- a/head/sys/contrib/dev/acpica/executer/exoparg6.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/executer/exoparg6.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/executer/exprep.c
--- a/head/sys/contrib/dev/acpica/executer/exprep.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/executer/exprep.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/executer/exregion.c
--- a/head/sys/contrib/dev/acpica/executer/exregion.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/executer/exregion.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/executer/exresnte.c
--- a/head/sys/contrib/dev/acpica/executer/exresnte.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/executer/exresnte.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/executer/exresolv.c
--- a/head/sys/contrib/dev/acpica/executer/exresolv.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/executer/exresolv.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/executer/exresop.c
--- a/head/sys/contrib/dev/acpica/executer/exresop.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/executer/exresop.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/executer/exstore.c
--- a/head/sys/contrib/dev/acpica/executer/exstore.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/executer/exstore.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/executer/exstoren.c
--- a/head/sys/contrib/dev/acpica/executer/exstoren.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/executer/exstoren.c	Fri Jan 13 15:51:01 2012 +0200
@@ -7,7 +7,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/executer/exstorob.c
--- a/head/sys/contrib/dev/acpica/executer/exstorob.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/executer/exstorob.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/executer/exsystem.c
--- a/head/sys/contrib/dev/acpica/executer/exsystem.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/executer/exsystem.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/executer/exutils.c
--- a/head/sys/contrib/dev/acpica/executer/exutils.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/executer/exutils.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/hardware/hwacpi.c
--- a/head/sys/contrib/dev/acpica/hardware/hwacpi.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/hardware/hwacpi.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/hardware/hwgpe.c
--- a/head/sys/contrib/dev/acpica/hardware/hwgpe.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/hardware/hwgpe.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/hardware/hwpci.c
--- a/head/sys/contrib/dev/acpica/hardware/hwpci.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/hardware/hwpci.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/hardware/hwregs.c
--- a/head/sys/contrib/dev/acpica/hardware/hwregs.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/hardware/hwregs.c	Fri Jan 13 15:51:01 2012 +0200
@@ -7,7 +7,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/hardware/hwsleep.c
--- a/head/sys/contrib/dev/acpica/hardware/hwsleep.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/hardware/hwsleep.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/hardware/hwtimer.c
--- a/head/sys/contrib/dev/acpica/hardware/hwtimer.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/hardware/hwtimer.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/hardware/hwvalid.c
--- a/head/sys/contrib/dev/acpica/hardware/hwvalid.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/hardware/hwvalid.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/hardware/hwxface.c
--- a/head/sys/contrib/dev/acpica/hardware/hwxface.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/hardware/hwxface.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/include/acapps.h
--- a/head/sys/contrib/dev/acpica/include/acapps.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/include/acapps.h	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -52,7 +52,7 @@
 /* Common info for tool signons */
 
 #define ACPICA_NAME                 "Intel ACPI Component Architecture"
-#define ACPICA_COPYRIGHT            "Copyright (c) 2000 - 2011 Intel Corporation"
+#define ACPICA_COPYRIGHT            "Copyright (c) 2000 - 2012 Intel Corporation"
 
 #if ACPI_MACHINE_WIDTH == 64
 #define ACPI_WIDTH          "-64"
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/include/accommon.h
--- a/head/sys/contrib/dev/acpica/include/accommon.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/include/accommon.h	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/include/acconfig.h
--- a/head/sys/contrib/dev/acpica/include/acconfig.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/include/acconfig.h	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -125,6 +125,10 @@
 
 #define ACPI_MAX_SLEEP                  2000    /* 2000 millisec == two seconds */
 
+/* Address Range lists are per-SpaceId (Memory and I/O only) */
+
+#define ACPI_ADDRESS_RANGE_MAX          2
+
 
 /******************************************************************************
  *
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/include/acdebug.h
--- a/head/sys/contrib/dev/acpica/include/acdebug.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/include/acdebug.h	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/include/acdisasm.h
--- a/head/sys/contrib/dev/acpica/include/acdisasm.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/include/acdisasm.h	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -63,7 +63,7 @@
 typedef const struct acpi_dmtable_info
 {
     UINT8                       Opcode;
-    UINT8                       Offset;
+    UINT16                      Offset;
     char                        *Name;
     UINT8                       Flags;
 
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/include/acdispat.h
--- a/head/sys/contrib/dev/acpica/include/acdispat.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/include/acdispat.h	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/include/acevents.h
--- a/head/sys/contrib/dev/acpica/include/acevents.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/include/acevents.h	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/include/acexcep.h
--- a/head/sys/contrib/dev/acpica/include/acexcep.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/include/acexcep.h	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/include/acglobal.h
--- a/head/sys/contrib/dev/acpica/include/acglobal.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/include/acglobal.h	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -283,6 +283,7 @@
 ACPI_EXTERN BOOLEAN                     AcpiGbl_EventsInitialized;
 ACPI_EXTERN UINT8                       AcpiGbl_OsiData;
 ACPI_EXTERN ACPI_INTERFACE_INFO        *AcpiGbl_SupportedInterfaces;
+ACPI_EXTERN ACPI_ADDRESS_RANGE         *AcpiGbl_AddressRangeList[ACPI_ADDRESS_RANGE_MAX];
 
 
 #ifndef DEFINE_ACPI_GLOBALS
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/include/achware.h
--- a/head/sys/contrib/dev/acpica/include/achware.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/include/achware.h	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/include/acinterp.h
--- a/head/sys/contrib/dev/acpica/include/acinterp.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/include/acinterp.h	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/include/aclocal.h
--- a/head/sys/contrib/dev/acpica/include/aclocal.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/include/aclocal.h	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -755,6 +755,17 @@
 ACPI_STATUS (*ACPI_EXECUTE_OP) (
     struct acpi_walk_state          *WalkState);
 
+/* Address Range info block */
+
+typedef struct acpi_address_range
+{
+    struct acpi_address_range   *Next;
+    ACPI_NAMESPACE_NODE         *RegionNode;
+    ACPI_PHYSICAL_ADDRESS       StartAddress;
+    ACPI_PHYSICAL_ADDRESS       EndAddress;
+
+} ACPI_ADDRESS_RANGE;
+
 
 /*****************************************************************************
  *
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/include/acmacros.h
--- a/head/sys/contrib/dev/acpica/include/acmacros.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/include/acmacros.h	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/include/acnames.h
--- a/head/sys/contrib/dev/acpica/include/acnames.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/include/acnames.h	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/include/acnamesp.h
--- a/head/sys/contrib/dev/acpica/include/acnamesp.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/include/acnamesp.h	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/include/acobject.h
--- a/head/sys/contrib/dev/acpica/include/acobject.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/include/acobject.h	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/include/acopcode.h
--- a/head/sys/contrib/dev/acpica/include/acopcode.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/include/acopcode.h	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/include/acoutput.h
--- a/head/sys/contrib/dev/acpica/include/acoutput.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/include/acoutput.h	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/include/acparser.h
--- a/head/sys/contrib/dev/acpica/include/acparser.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/include/acparser.h	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/include/acpi.h
--- a/head/sys/contrib/dev/acpica/include/acpi.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/include/acpi.h	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/include/acpiosxf.h
--- a/head/sys/contrib/dev/acpica/include/acpiosxf.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/include/acpiosxf.h	Fri Jan 13 15:51:01 2012 +0200
@@ -9,7 +9,7 @@
 
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/include/acpixf.h
--- a/head/sys/contrib/dev/acpica/include/acpixf.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/include/acpixf.h	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -48,7 +48,7 @@
 
 /* Current ACPICA subsystem version in YYYYMMDD format */
 
-#define ACPI_CA_VERSION                 0x20111123
+#define ACPI_CA_VERSION                 0x20120111
 
 #include <contrib/dev/acpica/include/actypes.h>
 #include <contrib/dev/acpica/include/actbl.h>
@@ -145,6 +145,13 @@
 AcpiRemoveInterface (
     ACPI_STRING             InterfaceName);
 
+UINT32
+AcpiCheckAddressRange (
+    ACPI_ADR_SPACE_TYPE     SpaceId,
+    ACPI_PHYSICAL_ADDRESS   Address,
+    ACPI_SIZE               Length,
+    BOOLEAN                 Warn);
+
 
 /*
  * ACPI Memory management
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/include/acpredef.h
--- a/head/sys/contrib/dev/acpica/include/acpredef.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/include/acpredef.h	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/include/acresrc.h
--- a/head/sys/contrib/dev/acpica/include/acresrc.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/include/acresrc.h	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/include/acrestyp.h
--- a/head/sys/contrib/dev/acpica/include/acrestyp.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/include/acrestyp.h	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/include/acstruct.h
--- a/head/sys/contrib/dev/acpica/include/acstruct.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/include/acstruct.h	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/include/actables.h
--- a/head/sys/contrib/dev/acpica/include/actables.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/include/actables.h	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/include/actbl.h
--- a/head/sys/contrib/dev/acpica/include/actbl.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/include/actbl.h	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -298,7 +298,8 @@
     ACPI_GENERIC_ADDRESS    XPmTimerBlock;      /* 64-bit Extended Power Mgt Timer Ctrl Reg Blk address */
     ACPI_GENERIC_ADDRESS    XGpe0Block;         /* 64-bit Extended General Purpose Event 0 Reg Blk address */
     ACPI_GENERIC_ADDRESS    XGpe1Block;         /* 64-bit Extended General Purpose Event 1 Reg Blk address */
-    ACPI_GENERIC_ADDRESS    SleepRegister;      /* 64-bit address of the Sleep register */
+    ACPI_GENERIC_ADDRESS    SleepControl;       /* 64-bit Sleep Control register */
+    ACPI_GENERIC_ADDRESS    SleepStatus;        /* 64-bit Sleep Status register */
 
 } ACPI_TABLE_FADT;
 
@@ -335,8 +336,7 @@
 #define ACPI_FADT_APIC_CLUSTER      (1<<18)     /* 18: [V4] All local APICs must use cluster model (ACPI 3.0) */
 #define ACPI_FADT_APIC_PHYSICAL     (1<<19)     /* 19: [V4] All local xAPICs must use physical dest mode (ACPI 3.0) */
 #define ACPI_FADT_HW_REDUCED        (1<<20)     /* 20: [V5] ACPI hardware is not implemented (ACPI 5.0) */
-#define ACPI_FADT_PREFER_S0_IDLE    (1<<21)     /* 21: [V5] Use advanced idle capabilities (ACPI 5.0) */
-#define ACPI_FADT_USE_SLEEP_REG     (1<<22)     /* 22: [V5] Use the sleep register for sleep (ACPI 5.0) */
+#define ACPI_FADT_LOW_POWER_S0      (1<<21)     /* 21: [V5] S0 power savings are equal or better than S3 (ACPI 5.0) */
 
 
 /* Values for PreferredProfile (Prefered Power Management Profiles) */
@@ -351,7 +351,7 @@
     PM_SOHO_SERVER          = 5,
     PM_APPLIANCE_PC         = 6,
     PM_PERFORMANCE_SERVER   = 7,
-    PM_SLATE                = 8
+    PM_TABLET               = 8
 };
 
 
@@ -403,7 +403,7 @@
 
 /* Macros used to generate offsets to specific table fields */
 
-#define ACPI_FADT_OFFSET(f)             (UINT8) ACPI_OFFSET (ACPI_TABLE_FADT, f)
+#define ACPI_FADT_OFFSET(f)             (UINT16) ACPI_OFFSET (ACPI_TABLE_FADT, f)
 
 /*
  * Sizes of the various flavors of FADT. We need to look closely
@@ -417,11 +417,11 @@
  *     FADT V2  size: 0x084
  *     FADT V3  size: 0x0F4
  *     FADT V4  size: 0x0F4
- *     FADT V5  size: 0x100
+ *     FADT V5  size: 0x10C
  */
 #define ACPI_FADT_V1_SIZE       (UINT32) (ACPI_FADT_OFFSET (Flags) + 4)
 #define ACPI_FADT_V2_SIZE       (UINT32) (ACPI_FADT_OFFSET (Reserved4[0]) + 3)
-#define ACPI_FADT_V3_SIZE       (UINT32) (ACPI_FADT_OFFSET (SleepRegister))
+#define ACPI_FADT_V3_SIZE       (UINT32) (ACPI_FADT_OFFSET (SleepControl))
 #define ACPI_FADT_V5_SIZE       (UINT32) (sizeof (ACPI_TABLE_FADT))
 
 #endif /* __ACTBL_H__ */
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/include/actbl1.h
--- a/head/sys/contrib/dev/acpica/include/actbl1.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/include/actbl1.h	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/include/actbl2.h
--- a/head/sys/contrib/dev/acpica/include/actbl2.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/include/actbl2.h	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/include/actbl3.h
--- a/head/sys/contrib/dev/acpica/include/actbl3.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/include/actbl3.h	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/include/actypes.h
--- a/head/sys/contrib/dev/acpica/include/actypes.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/include/actypes.h	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/include/acutils.h
--- a/head/sys/contrib/dev/acpica/include/acutils.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/include/acutils.h	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -130,7 +130,6 @@
 #define DB_DWORD_DISPLAY    4
 #define DB_QWORD_DISPLAY    8
 
-
 /*
  * utglobal - Global data structures and procedures
  */
@@ -739,6 +738,11 @@
 AcpiUtStrlwr (
     char                    *SrcString);
 
+int
+AcpiUtStricmp (
+    char                    *String1,
+    char                    *String2);
+
 void
 AcpiUtPrintString (
     char                    *String,
@@ -914,6 +918,31 @@
 
 #endif /* ACPI_DBG_TRACK_ALLOCATIONS */
 
+/*
+ * utaddress - address range check
+ */
+ACPI_STATUS
+AcpiUtAddAddressRange (
+    ACPI_ADR_SPACE_TYPE     SpaceId,
+    ACPI_PHYSICAL_ADDRESS   Address,
+    UINT32                  Length,
+    ACPI_NAMESPACE_NODE     *RegionNode);
+
+void
+AcpiUtRemoveAddressRange (
+    ACPI_ADR_SPACE_TYPE     SpaceId,
+    ACPI_NAMESPACE_NODE     *RegionNode);
+
+UINT32
+AcpiUtCheckAddressRange (
+    ACPI_ADR_SPACE_TYPE     SpaceId,
+    ACPI_PHYSICAL_ADDRESS   Address,
+    UINT32                  Length,
+    BOOLEAN                 Warn);
+
+void
+AcpiUtDeleteAddressLists (
+    void);
 
 /*
  * utxferror - various error/warning output functions
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/include/amlcode.h
--- a/head/sys/contrib/dev/acpica/include/amlcode.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/include/amlcode.h	Fri Jan 13 15:51:01 2012 +0200
@@ -7,7 +7,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/include/amlresrc.h
--- a/head/sys/contrib/dev/acpica/include/amlresrc.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/include/amlresrc.h	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/include/platform/acenv.h
--- a/head/sys/contrib/dev/acpica/include/platform/acenv.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/include/platform/acenv.h	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/include/platform/acfreebsd.h
--- a/head/sys/contrib/dev/acpica/include/platform/acfreebsd.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/include/platform/acfreebsd.h	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/include/platform/acgcc.h
--- a/head/sys/contrib/dev/acpica/include/platform/acgcc.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/include/platform/acgcc.h	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/namespace/nsaccess.c
--- a/head/sys/contrib/dev/acpica/namespace/nsaccess.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/namespace/nsaccess.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/namespace/nsalloc.c
--- a/head/sys/contrib/dev/acpica/namespace/nsalloc.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/namespace/nsalloc.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/namespace/nsdump.c
--- a/head/sys/contrib/dev/acpica/namespace/nsdump.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/namespace/nsdump.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/namespace/nsdumpdv.c
--- a/head/sys/contrib/dev/acpica/namespace/nsdumpdv.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/namespace/nsdumpdv.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/namespace/nseval.c
--- a/head/sys/contrib/dev/acpica/namespace/nseval.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/namespace/nseval.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/namespace/nsinit.c
--- a/head/sys/contrib/dev/acpica/namespace/nsinit.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/namespace/nsinit.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/namespace/nsload.c
--- a/head/sys/contrib/dev/acpica/namespace/nsload.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/namespace/nsload.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/namespace/nsnames.c
--- a/head/sys/contrib/dev/acpica/namespace/nsnames.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/namespace/nsnames.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/namespace/nsobject.c
--- a/head/sys/contrib/dev/acpica/namespace/nsobject.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/namespace/nsobject.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/namespace/nsparse.c
--- a/head/sys/contrib/dev/acpica/namespace/nsparse.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/namespace/nsparse.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/namespace/nspredef.c
--- a/head/sys/contrib/dev/acpica/namespace/nspredef.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/namespace/nspredef.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/namespace/nsrepair.c
--- a/head/sys/contrib/dev/acpica/namespace/nsrepair.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/namespace/nsrepair.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/namespace/nsrepair2.c
--- a/head/sys/contrib/dev/acpica/namespace/nsrepair2.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/namespace/nsrepair2.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/namespace/nssearch.c
--- a/head/sys/contrib/dev/acpica/namespace/nssearch.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/namespace/nssearch.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/namespace/nsutils.c
--- a/head/sys/contrib/dev/acpica/namespace/nsutils.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/namespace/nsutils.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/namespace/nswalk.c
--- a/head/sys/contrib/dev/acpica/namespace/nswalk.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/namespace/nswalk.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/namespace/nsxfeval.c
--- a/head/sys/contrib/dev/acpica/namespace/nsxfeval.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/namespace/nsxfeval.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/namespace/nsxfname.c
--- a/head/sys/contrib/dev/acpica/namespace/nsxfname.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/namespace/nsxfname.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/namespace/nsxfobj.c
--- a/head/sys/contrib/dev/acpica/namespace/nsxfobj.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/namespace/nsxfobj.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/os_specific/service_layers/osunixxf.c
--- a/head/sys/contrib/dev/acpica/os_specific/service_layers/osunixxf.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/os_specific/service_layers/osunixxf.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/parser/psargs.c
--- a/head/sys/contrib/dev/acpica/parser/psargs.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/parser/psargs.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/parser/psloop.c
--- a/head/sys/contrib/dev/acpica/parser/psloop.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/parser/psloop.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/parser/psopcode.c
--- a/head/sys/contrib/dev/acpica/parser/psopcode.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/parser/psopcode.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/parser/psparse.c
--- a/head/sys/contrib/dev/acpica/parser/psparse.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/parser/psparse.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/parser/psscope.c
--- a/head/sys/contrib/dev/acpica/parser/psscope.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/parser/psscope.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/parser/pstree.c
--- a/head/sys/contrib/dev/acpica/parser/pstree.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/parser/pstree.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/parser/psutils.c
--- a/head/sys/contrib/dev/acpica/parser/psutils.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/parser/psutils.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/parser/pswalk.c
--- a/head/sys/contrib/dev/acpica/parser/pswalk.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/parser/pswalk.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/parser/psxface.c
--- a/head/sys/contrib/dev/acpica/parser/psxface.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/parser/psxface.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/resources/rsaddr.c
--- a/head/sys/contrib/dev/acpica/resources/rsaddr.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/resources/rsaddr.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/resources/rscalc.c
--- a/head/sys/contrib/dev/acpica/resources/rscalc.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/resources/rscalc.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/resources/rscreate.c
--- a/head/sys/contrib/dev/acpica/resources/rscreate.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/resources/rscreate.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/resources/rsdump.c
--- a/head/sys/contrib/dev/acpica/resources/rsdump.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/resources/rsdump.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/resources/rsinfo.c
--- a/head/sys/contrib/dev/acpica/resources/rsinfo.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/resources/rsinfo.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/resources/rsio.c
--- a/head/sys/contrib/dev/acpica/resources/rsio.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/resources/rsio.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/resources/rsirq.c
--- a/head/sys/contrib/dev/acpica/resources/rsirq.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/resources/rsirq.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/resources/rslist.c
--- a/head/sys/contrib/dev/acpica/resources/rslist.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/resources/rslist.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/resources/rsmemory.c
--- a/head/sys/contrib/dev/acpica/resources/rsmemory.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/resources/rsmemory.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/resources/rsmisc.c
--- a/head/sys/contrib/dev/acpica/resources/rsmisc.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/resources/rsmisc.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/resources/rsserial.c
--- a/head/sys/contrib/dev/acpica/resources/rsserial.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/resources/rsserial.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/resources/rsutils.c
--- a/head/sys/contrib/dev/acpica/resources/rsutils.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/resources/rsutils.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/resources/rsxface.c
--- a/head/sys/contrib/dev/acpica/resources/rsxface.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/resources/rsxface.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/tables/tbfadt.c
--- a/head/sys/contrib/dev/acpica/tables/tbfadt.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/tables/tbfadt.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -77,14 +77,15 @@
 typedef struct acpi_fadt_info
 {
     char                    *Name;
-    UINT8                   Address64;
-    UINT8                   Address32;
-    UINT8                   Length;
+    UINT16                  Address64;
+    UINT16                  Address32;
+    UINT16                  Length;
     UINT8                   DefaultLength;
     UINT8                   Type;
 
 } ACPI_FADT_INFO;
 
+#define ACPI_FADT_OPTIONAL          0
 #define ACPI_FADT_REQUIRED          1
 #define ACPI_FADT_SEPARATE_LENGTH   2
 
@@ -102,7 +103,7 @@
         ACPI_FADT_OFFSET (Pm1bEventBlock),
         ACPI_FADT_OFFSET (Pm1EventLength),
         ACPI_PM1_REGISTER_WIDTH * 2,        /* Enable + Status register */
-        0},
+        ACPI_FADT_OPTIONAL},
 
     {"Pm1aControlBlock",
         ACPI_FADT_OFFSET (XPm1aControlBlock),
@@ -116,7 +117,7 @@
         ACPI_FADT_OFFSET (Pm1bControlBlock),
         ACPI_FADT_OFFSET (Pm1ControlLength),
         ACPI_PM1_REGISTER_WIDTH,
-        0},
+        ACPI_FADT_OPTIONAL},
 
     {"Pm2ControlBlock",
         ACPI_FADT_OFFSET (XPm2ControlBlock),
@@ -156,7 +157,7 @@
 typedef struct acpi_fadt_pm_info
 {
     ACPI_GENERIC_ADDRESS    *Target;
-    UINT8                   Source;
+    UINT16                  Source;
     UINT8                   RegisterNum;
 
 } ACPI_FADT_PM_INFO;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/tables/tbfind.c
--- a/head/sys/contrib/dev/acpica/tables/tbfind.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/tables/tbfind.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/tables/tbinstal.c
--- a/head/sys/contrib/dev/acpica/tables/tbinstal.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/tables/tbinstal.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/tables/tbutils.c
--- a/head/sys/contrib/dev/acpica/tables/tbutils.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/tables/tbutils.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/tables/tbxface.c
--- a/head/sys/contrib/dev/acpica/tables/tbxface.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/tables/tbxface.c	Fri Jan 13 15:51:01 2012 +0200
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/tables/tbxfroot.c
--- a/head/sys/contrib/dev/acpica/tables/tbxfroot.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/tables/tbxfroot.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/utilities/utaddress.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/sys/contrib/dev/acpica/utilities/utaddress.c	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,322 @@
+/******************************************************************************
+ *
+ * Module Name: utaddress - OpRegion address range check
+ *
+ *****************************************************************************/
+
+/*
+ * Copyright (C) 2000 - 2012, Intel Corp.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions, and the following disclaimer,
+ *    without modification.
+ * 2. Redistributions in binary form must reproduce at minimum a disclaimer
+ *    substantially similar to the "NO WARRANTY" disclaimer below
+ *    ("Disclaimer") and any redistribution must be conditioned upon
+ *    including a substantially similar Disclaimer requirement for further
+ *    binary redistribution.
+ * 3. Neither the names of the above-listed copyright holders nor the names
+ *    of any contributors may be used to endorse or promote products derived
+ *    from this software without specific prior written permission.
+ *
+ * Alternatively, this software may be distributed under the terms of the
+ * GNU General Public License ("GPL") version 2 as published by the Free
+ * Software Foundation.
+ *
+ * NO WARRANTY
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGES.
+ */
+
+#define __UTADDRESS_C__
+
+#include <contrib/dev/acpica/include/acpi.h>
+#include <contrib/dev/acpica/include/accommon.h>
+#include <contrib/dev/acpica/include/acnamesp.h>
+
+
+#define _COMPONENT          ACPI_UTILITIES
+        ACPI_MODULE_NAME    ("utaddress")
+
+
+/*******************************************************************************
+ *
+ * FUNCTION:    AcpiUtAddAddressRange
+ *
+ * PARAMETERS:  SpaceId             - Address space ID
+ *              Address             - OpRegion start address
+ *              Length              - OpRegion length
+ *              RegionNode          - OpRegion namespace node
+ *
+ * RETURN:      Status
+ *
+ * DESCRIPTION: Add the Operation Region address range to the global list.
+ *              The only supported Space IDs are Memory and I/O. Called when
+ *              the OpRegion address/length operands are fully evaluated.
+ *
+ * MUTEX:       Locks the namespace
+ *
+ * NOTE: Because this interface is only called when an OpRegion argument
+ * list is evaluated, there cannot be any duplicate RegionNodes.
+ * Duplicate Address/Length values are allowed, however, so that multiple
+ * address conflicts can be detected.
+ *
+ ******************************************************************************/
+
+ACPI_STATUS
+AcpiUtAddAddressRange (
+    ACPI_ADR_SPACE_TYPE     SpaceId,
+    ACPI_PHYSICAL_ADDRESS   Address,
+    UINT32                  Length,
+    ACPI_NAMESPACE_NODE     *RegionNode)
+{
+    ACPI_ADDRESS_RANGE      *RangeInfo;
+    ACPI_STATUS             Status;
+
+
+    ACPI_FUNCTION_TRACE (UtAddAddressRange);
+
+
+    if ((SpaceId != ACPI_ADR_SPACE_SYSTEM_MEMORY) &&
+        (SpaceId != ACPI_ADR_SPACE_SYSTEM_IO))
+    {
+        return_ACPI_STATUS (AE_OK);
+    }
+
+    /* Allocate/init a new info block, add it to the appropriate list */
+
+    RangeInfo = ACPI_ALLOCATE (sizeof (ACPI_ADDRESS_RANGE));
+    if (!RangeInfo)
+    {
+        return_ACPI_STATUS (AE_NO_MEMORY);
+    }
+
+    RangeInfo->StartAddress = Address;
+    RangeInfo->EndAddress = (Address + Length - 1);
+    RangeInfo->RegionNode = RegionNode;
+
+    Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
+    if (ACPI_FAILURE (Status))
+    {
+        ACPI_FREE (RangeInfo);
+        return_ACPI_STATUS (Status);
+    }
+
+    RangeInfo->Next = AcpiGbl_AddressRangeList[SpaceId];
+    AcpiGbl_AddressRangeList[SpaceId] = RangeInfo;
+
+    ACPI_DEBUG_PRINT ((ACPI_DB_NAMES,
+        "\nAdded [%4.4s] address range: 0x%p-0x%p\n",
+        AcpiUtGetNodeName (RangeInfo->RegionNode),
+        ACPI_CAST_PTR (void, Address),
+        ACPI_CAST_PTR (void, RangeInfo->EndAddress)));
+
+    (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
+    return_ACPI_STATUS (AE_OK);
+}
+
+
+/*******************************************************************************
+ *
+ * FUNCTION:    AcpiUtRemoveAddressRange
+ *
+ * PARAMETERS:  SpaceId             - Address space ID
+ *              RegionNode          - OpRegion namespace node
+ *
+ * RETURN:      None
+ *
+ * DESCRIPTION: Remove the Operation Region from the global list. The only
+ *              supported Space IDs are Memory and I/O. Called when an
+ *              OpRegion is deleted.
+ *
+ * MUTEX:       Assumes the namespace is locked
+ *
+ ******************************************************************************/
+
+void
+AcpiUtRemoveAddressRange (
+    ACPI_ADR_SPACE_TYPE     SpaceId,
+    ACPI_NAMESPACE_NODE     *RegionNode)
+{
+    ACPI_ADDRESS_RANGE      *RangeInfo;
+    ACPI_ADDRESS_RANGE      *Prev;
+
+
+    ACPI_FUNCTION_TRACE (UtRemoveAddressRange);
+
+
+    if ((SpaceId != ACPI_ADR_SPACE_SYSTEM_MEMORY) &&
+        (SpaceId != ACPI_ADR_SPACE_SYSTEM_IO))
+    {
+        return_VOID;
+    }
+
+    /* Get the appropriate list head and check the list */
+
+    RangeInfo = Prev = AcpiGbl_AddressRangeList[SpaceId];
+    while (RangeInfo)
+    {
+        if (RangeInfo->RegionNode == RegionNode)
+        {
+            if (RangeInfo == Prev) /* Found at list head */
+            {
+                AcpiGbl_AddressRangeList[SpaceId] = RangeInfo->Next;
+            }
+            else
+            {
+                Prev->Next = RangeInfo->Next;
+            }
+
+            ACPI_DEBUG_PRINT ((ACPI_DB_NAMES,
+                "\nRemoved [%4.4s] address range: 0x%p-0x%p\n",
+                AcpiUtGetNodeName (RangeInfo->RegionNode),
+                ACPI_CAST_PTR (void, RangeInfo->StartAddress),
+                ACPI_CAST_PTR (void, RangeInfo->EndAddress)));
+
+            ACPI_FREE (RangeInfo);
+            return_VOID;
+        }
+
+        Prev = RangeInfo;
+        RangeInfo = RangeInfo->Next;
+    }
+
+    return_VOID;
+}
+
+
+/*******************************************************************************
+ *
+ * FUNCTION:    AcpiUtCheckAddressRange
+ *
+ * PARAMETERS:  SpaceId             - Address space ID
+ *              Address             - Start address
+ *              Length              - Length of address range
+ *              Warn                - TRUE if warning on overlap desired
+ *
+ * RETURN:      Count of the number of conflicts detected. Zero is always
+ *              returned for Space IDs other than Memory or I/O.
+ *
+ * DESCRIPTION: Check if the input address range overlaps any of the
+ *              ASL operation region address ranges. The only supported
+ *              Space IDs are Memory and I/O.
+ *
+ * MUTEX:       Assumes the namespace is locked.
+ *
+ ******************************************************************************/
+
+UINT32
+AcpiUtCheckAddressRange (
+    ACPI_ADR_SPACE_TYPE     SpaceId,
+    ACPI_PHYSICAL_ADDRESS   Address,
+    UINT32                  Length,
+    BOOLEAN                 Warn)
+{
+    ACPI_ADDRESS_RANGE      *RangeInfo;
+    ACPI_PHYSICAL_ADDRESS   EndAddress;
+    char                    *Pathname;
+    UINT32                  OverlapCount = 0;
+
+
+    ACPI_FUNCTION_TRACE (UtCheckAddressRange);
+
+
+    if ((SpaceId != ACPI_ADR_SPACE_SYSTEM_MEMORY) &&
+        (SpaceId != ACPI_ADR_SPACE_SYSTEM_IO))
+    {
+        return_UINT32 (0);
+    }
+
+    RangeInfo = AcpiGbl_AddressRangeList[SpaceId];
+    EndAddress = Address + Length - 1;
+
+    /* Check entire list for all possible conflicts */
+
+    while (RangeInfo)
+    {
+        /*
+         * Check if the requested Address/Length overlaps this AddressRange.
+         * Four cases to consider:
+         *
+         * 1) Input address/length is contained completely in the address range
+         * 2) Input address/length overlaps range at the range start
+         * 3) Input address/length overlaps range at the range end
+         * 4) Input address/length completely encompasses the range
+         */
+        if ((Address <= RangeInfo->EndAddress) &&
+            (EndAddress >= RangeInfo->StartAddress))
+        {
+            /* Found an address range overlap */
+
+            OverlapCount++;
+            if (Warn)   /* Optional warning message */
+            {
+                Pathname = AcpiNsGetExternalPathname (RangeInfo->RegionNode);
+
+                ACPI_WARNING ((AE_INFO,
+                    "0x%p-0x%p %s conflicts with Region %s %d",
+                    ACPI_CAST_PTR (void, Address),
+                    ACPI_CAST_PTR (void, EndAddress),
+                    AcpiUtGetRegionName (SpaceId), Pathname, OverlapCount));
+                ACPI_FREE (Pathname);
+            }
+        }
+
+        RangeInfo = RangeInfo->Next;
+    }
+
+    return_UINT32 (OverlapCount);
+}
+
+
+/*******************************************************************************
+ *
+ * FUNCTION:    AcpiUtDeleteAddressLists
+ *
+ * PARAMETERS:  None
+ *
+ * RETURN:      None
+ *
+ * DESCRIPTION: Delete all global address range lists (called during
+ *              subsystem shutdown).
+ *
+ ******************************************************************************/
+
+void
+AcpiUtDeleteAddressLists (
+    void)
+{
+    ACPI_ADDRESS_RANGE      *Next;
+    ACPI_ADDRESS_RANGE      *RangeInfo;
+    int                     i;
+
+
+    /* Delete all elements in all address range lists */
+
+    for (i = 0; i < ACPI_ADDRESS_RANGE_MAX; i++)
+    {
+        Next = AcpiGbl_AddressRangeList[i];
+
+        while (Next)
+        {
+            RangeInfo = Next;
+            Next = RangeInfo->Next;
+            ACPI_FREE (RangeInfo);
+        }
+
+        AcpiGbl_AddressRangeList[i] = NULL;
+    }
+}
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/utilities/utalloc.c
--- a/head/sys/contrib/dev/acpica/utilities/utalloc.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/utilities/utalloc.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/utilities/utcache.c
--- a/head/sys/contrib/dev/acpica/utilities/utcache.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/utilities/utcache.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/utilities/utcopy.c
--- a/head/sys/contrib/dev/acpica/utilities/utcopy.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/utilities/utcopy.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/utilities/utdebug.c
--- a/head/sys/contrib/dev/acpica/utilities/utdebug.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/utilities/utdebug.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/utilities/utdecode.c
--- a/head/sys/contrib/dev/acpica/utilities/utdecode.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/utilities/utdecode.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/utilities/utdelete.c
--- a/head/sys/contrib/dev/acpica/utilities/utdelete.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/utilities/utdelete.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -235,6 +235,16 @@
         ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
             "***** Region %p\n", Object));
 
+        /*
+         * Update AddressRange list. However, only permanent regions
+         * are installed in this list. (Not created within a method)
+         */
+        if (!(Object->Region.Node->Flags & ANOBJ_TEMPORARY))
+        {
+            AcpiUtRemoveAddressRange (Object->Region.SpaceId,
+                Object->Region.Node);
+        }
+
         SecondDesc = AcpiNsGetSecondaryObject (Object);
         if (SecondDesc)
         {
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/utilities/uteval.c
--- a/head/sys/contrib/dev/acpica/utilities/uteval.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/utilities/uteval.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/utilities/utglobal.c
--- a/head/sys/contrib/dev/acpica/utilities/utglobal.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/utilities/utglobal.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -233,6 +233,13 @@
         return_ACPI_STATUS (Status);
     }
 
+    /* Address Range lists */
+
+    for (i = 0; i < ACPI_ADDRESS_RANGE_MAX; i++)
+    {
+        AcpiGbl_AddressRangeList[i] = NULL;
+    }
+
     /* Mutex locked flags */
 
     for (i = 0; i < ACPI_NUM_MUTEX; i++)
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/utilities/utids.c
--- a/head/sys/contrib/dev/acpica/utilities/utids.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/utilities/utids.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/utilities/utinit.c
--- a/head/sys/contrib/dev/acpica/utilities/utinit.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/utilities/utinit.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -104,6 +104,7 @@
         GpeXruptInfo = NextGpeXruptInfo;
     }
 
+    AcpiUtDeleteAddressLists ();
     return_VOID;
 }
 
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/utilities/utlock.c
--- a/head/sys/contrib/dev/acpica/utilities/utlock.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/utilities/utlock.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/utilities/utmath.c
--- a/head/sys/contrib/dev/acpica/utilities/utmath.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/utilities/utmath.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/utilities/utmisc.c
--- a/head/sys/contrib/dev/acpica/utilities/utmisc.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/utilities/utmisc.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -466,6 +466,44 @@
 
     return;
 }
+
+
+/******************************************************************************
+ *
+ * FUNCTION:    AcpiUtStricmp
+ *
+ * PARAMETERS:  String1             - first string to compare
+ *              String2             - second string to compare
+ *
+ * RETURN:      int that signifies string relationship. Zero means strings
+ *              are equal.
+ *
+ * DESCRIPTION: Implementation of the non-ANSI stricmp function (compare
+ *              strings with no case sensitivity)
+ *
+ ******************************************************************************/
+
+int
+AcpiUtStricmp (
+    char                    *String1,
+    char                    *String2)
+{
+    int                     c1;
+    int                     c2;
+
+
+    do
+    {
+        c1 = tolower ((int) *String1);
+        c2 = tolower ((int) *String2);
+
+        String1++;
+        String2++;
+    }
+    while ((c1 == c2) && (c1));
+
+    return (c1 - c2);
+}
 #endif
 
 
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/utilities/utmutex.c
--- a/head/sys/contrib/dev/acpica/utilities/utmutex.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/utilities/utmutex.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/utilities/utobject.c
--- a/head/sys/contrib/dev/acpica/utilities/utobject.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/utilities/utobject.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/utilities/utosi.c
--- a/head/sys/contrib/dev/acpica/utilities/utosi.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/utilities/utosi.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/utilities/utresrc.c
--- a/head/sys/contrib/dev/acpica/utilities/utresrc.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/utilities/utresrc.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/utilities/utstate.c
--- a/head/sys/contrib/dev/acpica/utilities/utstate.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/utilities/utstate.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/utilities/uttrack.c
--- a/head/sys/contrib/dev/acpica/utilities/uttrack.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/utilities/uttrack.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/utilities/utxface.c
--- a/head/sys/contrib/dev/acpica/utilities/utxface.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/utilities/utxface.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -50,6 +50,7 @@
 #include <contrib/dev/acpica/include/acnamesp.h>
 #include <contrib/dev/acpica/include/acdebug.h>
 #include <contrib/dev/acpica/include/actables.h>
+#include <contrib/dev/acpica/include/acinterp.h>
 
 #define _COMPONENT          ACPI_UTILITIES
         ACPI_MODULE_NAME    ("utxface")
@@ -785,5 +786,47 @@
 
 ACPI_EXPORT_SYMBOL (AcpiInstallInterfaceHandler)
 
+
+/*****************************************************************************
+ *
+ * FUNCTION:    AcpiCheckAddressRange
+ *
+ * PARAMETERS:  SpaceId             - Address space ID
+ *              Address             - Start address
+ *              Length              - Length
+ *              Warn                - TRUE if warning on overlap desired
+ *
+ * RETURN:      Count of the number of conflicts detected.
+ *
+ * DESCRIPTION: Check if the input address range overlaps any of the
+ *              ASL operation region address ranges.
+ *
+ ****************************************************************************/
+
+UINT32
+AcpiCheckAddressRange (
+    ACPI_ADR_SPACE_TYPE     SpaceId,
+    ACPI_PHYSICAL_ADDRESS   Address,
+    ACPI_SIZE               Length,
+    BOOLEAN                 Warn)
+{
+    UINT32                  Overlaps;
+    ACPI_STATUS             Status;
+
+
+    Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
+    if (ACPI_FAILURE (Status))
+    {
+        return (0);
+    }
+
+    Overlaps = AcpiUtCheckAddressRange (SpaceId, Address,
+        (UINT32) Length, Warn);
+
+    (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
+    return (Overlaps);
+}
+
+ACPI_EXPORT_SYMBOL (AcpiCheckAddressRange)
+
 #endif /* !ACPI_ASL_COMPILER */
-
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/utilities/utxferror.c
--- a/head/sys/contrib/dev/acpica/utilities/utxferror.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/utilities/utxferror.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/dev/acpica/utilities/utxfmutex.c
--- a/head/sys/contrib/dev/acpica/utilities/utxfmutex.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/dev/acpica/utilities/utxfmutex.c	Fri Jan 13 15:51:01 2012 +0200
@@ -5,7 +5,7 @@
  ******************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2011, Intel Corp.
+ * Copyright (C) 2000 - 2012, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/pf/net/if_pfsync.c
--- a/head/sys/contrib/pf/net/if_pfsync.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/pf/net/if_pfsync.c	Fri Jan 13 15:51:01 2012 +0200
@@ -47,6 +47,8 @@
  * 1.118, 1.124, 1.148, 1.149, 1.151, 1.171 - fixes to bulk updates
  * 1.120, 1.175 - use monotonic time_uptime
  * 1.122 - reduce number of updates for non-TCP sessions
+ * 1.128 - cleanups
+ * 1.170 - SIOCSIFMTU checks
  */
 
 #ifdef __FreeBSD__
@@ -55,15 +57,9 @@
 #include "opt_pf.h"
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/contrib/pf/net/if_pfsync.c 228855 2011-12-24 00:23:27Z pluknet $");
+__FBSDID("$FreeBSD: head/sys/contrib/pf/net/if_pfsync.c 229976 2012-01-11 18:34:57Z glebius $");
 
 #define	NBPFILTER	1
-
-#ifdef DEV_PFSYNC
-#define	NPFSYNC		DEV_PFSYNC
-#else
-#define	NPFSYNC		0
-#endif
 #endif /* __FreeBSD__ */
 
 #include <sys/param.h>
@@ -86,6 +82,7 @@
 #include <sys/taskqueue.h>
 #include <sys/lock.h>
 #include <sys/mutex.h>
+#include <sys/protosw.h>
 #else
 #include <sys/ioctl.h>
 #include <sys/timeout.h>
@@ -294,21 +291,28 @@
 #else
 	struct timeout		 sc_tmo;
 #endif
+};
+
 #ifdef __FreeBSD__
-	eventhandler_tag	 sc_detachtag;
-#endif
-
-};
-
-#ifdef __FreeBSD__
+static MALLOC_DEFINE(M_PFSYNC, "pfsync", "pfsync data");
 static VNET_DEFINE(struct pfsync_softc	*, pfsyncif) = NULL;
 #define	V_pfsyncif		VNET(pfsyncif)
-
+static VNET_DEFINE(void *, pfsync_swi_cookie) = NULL;
+#define	V_pfsync_swi_cookie	VNET(pfsync_swi_cookie)
 static VNET_DEFINE(struct pfsyncstats, pfsyncstats);
 #define	V_pfsyncstats		VNET(pfsyncstats)
 static VNET_DEFINE(int, pfsync_carp_adj) = CARP_MAXSKEW;
 #define	V_pfsync_carp_adj	VNET(pfsync_carp_adj)
 
+static void	pfsyncintr(void *);
+static int	pfsync_multicast_setup(struct pfsync_softc *);
+static void	pfsync_multicast_cleanup(struct pfsync_softc *);
+static int	pfsync_init(void);
+static void	pfsync_uninit(void);
+static void	pfsync_sendout1(int);
+
+#define	schednetisr(NETISR_PFSYNC)	swi_sched(V_pfsync_swi_cookie, 0)
+
 SYSCTL_NODE(_net, OID_AUTO, pfsync, CTLFLAG_RW, 0, "PFSYNC");
 SYSCTL_VNET_STRUCT(_net_pfsync, OID_AUTO, stats, CTLFLAG_RW,
     &VNET_NAME(pfsyncstats), pfsyncstats,
@@ -321,16 +325,6 @@
 #define	V_pfsyncstats	 pfsyncstats
 #endif
 
-#ifdef __FreeBSD__
-static void	pfsyncintr(void *);
-struct pfsync_swi {
-	void *	pfsync_swi_cookie;
-};
-static struct pfsync_swi	 pfsync_swi;
-#define	schednetisr(p)	swi_sched(pfsync_swi.pfsync_swi_cookie, 0)
-#define	NETISR_PFSYNC
-#endif
-
 void	pfsyncattach(int);
 #ifdef __FreeBSD__
 int	pfsync_clone_create(struct if_clone *, int, caddr_t);
@@ -352,7 +346,6 @@
 void	pfsyncstart(struct ifnet *);
 
 struct mbuf *pfsync_if_dequeue(struct ifnet *);
-struct mbuf *pfsync_get_mbuf(struct pfsync_softc *);
 
 void	pfsync_deferred(struct pf_state *, int);
 void	pfsync_undefer(struct pfsync_deferral *, int);
@@ -364,11 +357,8 @@
 void	pfsync_drop(struct pfsync_softc *);
 void	pfsync_sendout(void);
 void	pfsync_send_plus(void *, size_t);
-int	pfsync_tdb_sendout(struct pfsync_softc *);
-int	pfsync_sendout_mbuf(struct pfsync_softc *, struct mbuf *);
 void	pfsync_timeout(void *);
 void	pfsync_tdb_timeout(void *);
-void	pfsync_send_bus(struct pfsync_softc *, u_int8_t);
 
 void	pfsync_bulk_start(void);
 void	pfsync_bulk_status(u_int8_t);
@@ -376,8 +366,6 @@
 void	pfsync_bulk_fail(void *);
 
 #ifdef __FreeBSD__
-void	pfsync_ifdetach(void *, struct ifnet *);
-
 /* XXX: ugly */
 #define	betoh64		(unsigned long long)be64toh
 #define	timeout_del	callout_stop
@@ -389,6 +377,10 @@
 #endif
 
 #ifdef __FreeBSD__
+VNET_DEFINE(struct ifc_simple_data, pfsync_cloner_data);
+VNET_DEFINE(struct if_clone, pfsync_cloner);
+#define	V_pfsync_cloner_data	VNET(pfsync_cloner_data)
+#define	V_pfsync_cloner		VNET(pfsync_cloner)
 IFC_SIMPLE_DECLARE(pfsync, 1);
 #else
 struct if_clone	pfsync_cloner =
@@ -414,25 +406,20 @@
 	if (unit != 0)
 		return (EINVAL);
 
-#ifndef __FreeBSD__
+#ifdef __FreeBSD__
+	sc = malloc(sizeof(struct pfsync_softc), M_PFSYNC, M_WAITOK | M_ZERO);
+	sc->pfsync_sync_ok = 1;
+#else
 	pfsync_sync_ok = 1;
+	sc = malloc(sizeof(*pfsyncif), M_DEVBUF, M_NOWAIT | M_ZERO);
 #endif
 
-	sc = malloc(sizeof(struct pfsync_softc), M_DEVBUF, M_NOWAIT | M_ZERO);
-	if (sc == NULL)
-		return (ENOMEM);
-
 	for (q = 0; q < PFSYNC_S_COUNT; q++)
 		TAILQ_INIT(&sc->sc_qs[q]);
 
 #ifdef __FreeBSD__
-	sc->pfsync_sync_ok = 1;
-	sc->sc_pool = uma_zcreate("pfsync", PFSYNC_PLSIZE,
-			NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
-	if (sc->sc_pool == NULL) {
-		free(sc, M_DEVBUF);
-		return (ENOMEM);
-	}
+	sc->sc_pool = uma_zcreate("pfsync", PFSYNC_PLSIZE, NULL, NULL, NULL,
+	    NULL, UMA_ALIGN_PTR, 0);
 #else
 	pool_init(&sc->sc_pool, PFSYNC_PLSIZE, 0, 0, 0, "pfsync", NULL);
 #endif
@@ -445,13 +432,7 @@
 	sc->sc_len = PFSYNC_MINPKT;
 	sc->sc_maxupdates = 128;
 
-#ifdef __FreeBSD__
-	sc->sc_imo.imo_membership = (struct in_multi **)malloc(
-	    (sizeof(struct in_multi *) * IP_MIN_MEMBERSHIPS), M_DEVBUF,
-	    M_NOWAIT | M_ZERO);
-	sc->sc_imo.imo_max_memberships = IP_MIN_MEMBERSHIPS;
-	sc->sc_imo.imo_multicast_vif = -1;
-#else
+#ifndef __FreeBSD__
 	sc->sc_imo.imo_membership = (struct in_multi **)malloc(
 	    (sizeof(struct in_multi *) * IP_MIN_MEMBERSHIPS), M_IPMOPTS,
 	    M_WAITOK | M_ZERO);
@@ -461,26 +442,11 @@
 #ifdef __FreeBSD__
 	ifp = sc->sc_ifp = if_alloc(IFT_PFSYNC);
 	if (ifp == NULL) {
-		free(sc->sc_imo.imo_membership, M_DEVBUF);
 		uma_zdestroy(sc->sc_pool);
-		free(sc, M_DEVBUF);
+		free(sc, M_PFSYNC);
 		return (ENOSPC);
 	}
 	if_initname(ifp, ifc->ifc_name, unit);
-
-	sc->sc_detachtag = EVENTHANDLER_REGISTER(ifnet_departure_event,
-#ifdef __FreeBSD__
-	    pfsync_ifdetach, V_pfsyncif, EVENTHANDLER_PRI_ANY);
-#else
-	    pfsync_ifdetach, pfsyncif, EVENTHANDLER_PRI_ANY);
-#endif
-	if (sc->sc_detachtag == NULL) {
-		if_free(ifp);
-		free(sc->sc_imo.imo_membership, M_DEVBUF);
-		uma_zdestroy(sc->sc_pool);
-		free(sc, M_DEVBUF);
-		return (ENOSPC);
-	}
 #else
 	ifp = &sc->sc_if;
 	snprintf(ifp->if_xname, sizeof ifp->if_xname, "pfsync%d", unit);
@@ -492,13 +458,12 @@
 	ifp->if_type = IFT_PFSYNC;
 	ifp->if_snd.ifq_maxlen = ifqmaxlen;
 	ifp->if_hdrlen = sizeof(struct pfsync_header);
-	ifp->if_mtu = 1500; /* XXX */
+	ifp->if_mtu = ETHERMTU;
 #ifdef __FreeBSD__
 	callout_init(&sc->sc_tmo, CALLOUT_MPSAFE);
 	callout_init_mtx(&sc->sc_bulk_tmo, &pf_task_mtx, 0);
 	callout_init(&sc->sc_bulkfail_tmo, CALLOUT_MPSAFE);
 #else
-	ifp->if_hardmtu = MCLBYTES; /* XXX */
 	timeout_set(&sc->sc_tmo, pfsync_timeout, sc);
 	timeout_set(&sc->sc_bulk_tmo, pfsync_bulk_update, sc);
 	timeout_set(&sc->sc_bulkfail_tmo, pfsync_bulk_fail, sc);
@@ -540,7 +505,6 @@
 	struct pfsync_softc *sc = ifp->if_softc;
 
 #ifdef __FreeBSD__
-	EVENTHANDLER_DEREGISTER(ifnet_departure_event, sc->sc_detachtag);
 	PF_LOCK();
 #endif
 	timeout_del(&sc->sc_bulkfail_tmo);
@@ -573,11 +537,13 @@
 #endif
 #ifdef __FreeBSD__
 	if_free(ifp);
-	free(sc->sc_imo.imo_membership, M_DEVBUF);
+	if (sc->sc_imo.imo_membership)
+		pfsync_multicast_cleanup(sc);
+	free(sc, M_PFSYNC);
 #else
 	free(sc->sc_imo.imo_membership, M_IPMOPTS);
+	free(sc, M_DEVBUF);
 #endif
-	free(sc, M_DEVBUF);
 
 #ifdef __FreeBSD__
 	V_pfsyncif = NULL;
@@ -718,9 +684,9 @@
 	int pool_flags;
 	int error;
 
+#ifdef __FreeBSD__
 	PF_LOCK_ASSERT();
 
-#ifdef __FreeBSD__
 	if (sp->creatorid == 0 && V_pf_status.debug >= PF_DEBUG_MISC) {
 #else
 	if (sp->creatorid == 0 && pf_status.debug >= PF_DEBUG_MISC) {
@@ -860,11 +826,7 @@
 		CLR(st->state_flags, PFSTATE_NOSYNC);
 		if (ISSET(st->state_flags, PFSTATE_ACK)) {
 			pfsync_q_ins(st, PFSYNC_S_IACK);
-#ifdef __FreeBSD__
-			pfsync_sendout();
-#else
 			schednetisr(NETISR_PFSYNC);
-#endif
 		}
 	}
 	CLR(st->state_flags, PFSTATE_ACK);
@@ -1320,11 +1282,7 @@
 			V_pfsyncstats.pfsyncs_stale++;
 
 			pfsync_update_state(st);
-#ifdef __FreeBSD__
-			pfsync_sendout();
-#else
 			schednetisr(NETISR_PFSYNC);
-#endif
 			continue;
 		}
 		pfsync_alloc_scrub_memory(&sp->dst, &st->dst);
@@ -1430,11 +1388,7 @@
 			V_pfsyncstats.pfsyncs_stale++;
 
 			pfsync_update_state(st);
-#ifdef __FreeBSD__
-			pfsync_sendout();
-#else
 			schednetisr(NETISR_PFSYNC);
-#endif
 			continue;
 		}
 		pfsync_alloc_scrub_memory(&up->dst, &st->dst);
@@ -1470,6 +1424,9 @@
 	}
 	ura = (struct pfsync_upd_req *)(mp->m_data + offp);
 
+#ifdef __FreeBSD__
+	PF_LOCK();
+#endif
 	for (i = 0; i < count; i++) {
 		ur = &ura[i];
 
@@ -1487,11 +1444,12 @@
 			if (ISSET(st->state_flags, PFSTATE_NOSYNC))
 				continue;
 
-			PF_LOCK();
 			pfsync_update_state_req(st);
-			PF_UNLOCK();
 		}
 	}
+#ifdef __FreeBSD__
+	PF_UNLOCK();
+#endif
 
 	return (len);
 }
@@ -1614,7 +1572,7 @@
 #ifdef __FreeBSD__
 		callout_reset(&sc->sc_bulkfail_tmo, 4 * hz +
 		    V_pf_pool_limits[PF_LIMIT_STATES].limit /
-		    ((sc->sc_sync_if->if_mtu - PFSYNC_MINPKT) /
+		    ((sc->sc_ifp->if_mtu - PFSYNC_MINPKT) /
 		    sizeof(struct pfsync_state)),
 		    pfsync_bulk_fail, V_pfsyncif);
 #else
@@ -1821,10 +1779,10 @@
 #endif
 		break;
 	case SIOCSIFMTU:
-		if (ifr->ifr_mtu <= PFSYNC_MINPKT)
+		if (!sc->sc_sync_if ||
+		    ifr->ifr_mtu <= PFSYNC_MINPKT ||
+		    ifr->ifr_mtu > sc->sc_sync_if->if_mtu)
 			return (EINVAL);
-		if (ifr->ifr_mtu > MCLBYTES) /* XXX could be bigger */
-			ifr->ifr_mtu = MCLBYTES;
 		if (ifr->ifr_mtu < ifp->if_mtu) {
 			s = splnet();
 #ifdef __FreeBSD__
@@ -1886,12 +1844,15 @@
 			sc->sc_sync_if = NULL;
 #ifdef __FreeBSD__
 			PF_UNLOCK();
-#endif
+			if (imo->imo_membership)
+				pfsync_multicast_cleanup(sc);
+#else
 			if (imo->imo_num_memberships > 0) {
 				in_delmulti(imo->imo_membership[
 				    --imo->imo_num_memberships]);
 				imo->imo_multicast_ifp = NULL;
 			}
+#endif
 			break;
 		}
 
@@ -1916,57 +1877,53 @@
 			pfsync_sendout();
 		sc->sc_sync_if = sifp;
 
+#ifdef __FreeBSD__
+		if (imo->imo_membership) {
+			PF_UNLOCK();
+			pfsync_multicast_cleanup(sc);
+			PF_LOCK();
+		}
+#else
 		if (imo->imo_num_memberships > 0) {
-#ifdef __FreeBSD__
-			PF_UNLOCK();
-#endif
 			in_delmulti(imo->imo_membership[--imo->imo_num_memberships]);
-#ifdef __FreeBSD__
-			PF_LOCK();
-#endif
 			imo->imo_multicast_ifp = NULL;
 		}
-
+#endif
+
+#ifdef __FreeBSD__
 		if (sc->sc_sync_if &&
-#ifdef __FreeBSD__
 		    sc->sc_sync_peer.s_addr == htonl(INADDR_PFSYNC_GROUP)) {
+			PF_UNLOCK();
+			error = pfsync_multicast_setup(sc);
+			if (error)
+				return (error);
+			PF_LOCK();
+		}
 #else
+		if (sc->sc_sync_if &&
 		    sc->sc_sync_peer.s_addr == INADDR_PFSYNC_GROUP) {
-#endif
 			struct in_addr addr;
 
 			if (!(sc->sc_sync_if->if_flags & IFF_MULTICAST)) {
 				sc->sc_sync_if = NULL;
-#ifdef __FreeBSD__
-				PF_UNLOCK();
-#endif
 				splx(s);
 				return (EADDRNOTAVAIL);
 			}
 
-#ifdef __FreeBSD__
-			addr.s_addr = htonl(INADDR_PFSYNC_GROUP);
-#else
 			addr.s_addr = INADDR_PFSYNC_GROUP;
-#endif
-
-#ifdef __FreeBSD__
-			PF_UNLOCK();
-#endif
+
 			if ((imo->imo_membership[0] =
 			    in_addmulti(&addr, sc->sc_sync_if)) == NULL) {
 				sc->sc_sync_if = NULL;
 				splx(s);
 				return (ENOBUFS);
 			}
-#ifdef __FreeBSD__
-			PF_LOCK();
-#endif
 			imo->imo_num_memberships++;
 			imo->imo_multicast_ifp = sc->sc_sync_if;
 			imo->imo_multicast_ttl = PFSYNC_DFLTTL;
 			imo->imo_multicast_loop = 0;
 		}
+#endif	/* !__FreeBSD__ */
 
 		ip = &sc->sc_template;
 		bzero(ip, sizeof(*ip));
@@ -2102,7 +2059,7 @@
 #ifdef PFSYNC_DEBUG
 #ifdef __FreeBSD__
 			KASSERT(st->sync_state == q,
-				("%s: st->sync_state == q", 
+				("%s: st->sync_state == q",
 					__FUNCTION__));
 #else
 			KASSERT(st->sync_state == q);
@@ -2132,12 +2089,20 @@
 	sc->sc_len = PFSYNC_MINPKT;
 }
 
+#ifdef __FreeBSD__
+void pfsync_sendout()
+{
+	pfsync_sendout1(1);
+}
+
+static void
+pfsync_sendout1(int schedswi)
+{
+	struct pfsync_softc *sc = V_pfsyncif;
+#else
 void
 pfsync_sendout(void)
 {
-#ifdef __FreeBSD__
-	struct pfsync_softc *sc = V_pfsyncif;
-#else
 	struct pfsync_softc *sc = pfsyncif;
 #endif
 #if NBPFILTER > 0
@@ -2158,7 +2123,6 @@
 #endif
 #ifdef __FreeBSD__
 	size_t pktlen;
-	int dummy_error;
 #endif
 	int offset;
 	int q, count = 0;
@@ -2198,8 +2162,7 @@
 	if (pktlen > MHLEN) {
 		/* Find the right pool to allocate from. */
 		/* XXX: This is ugly. */
-		m_cljget(m, M_DONTWAIT, pktlen <= MSIZE ? MSIZE :
-			pktlen <= MCLBYTES ? MCLBYTES :
+		m_cljget(m, M_DONTWAIT, pktlen <= MCLBYTES ? MCLBYTES :
 #if MJUMPAGESIZE != MCLBYTES
 			pktlen <= MJUMPAGESIZE ? MJUMPAGESIZE :
 #endif
@@ -2364,8 +2327,14 @@
 	sc->sc_ifp->if_obytes += m->m_pkthdr.len;
 	sc->sc_len = PFSYNC_MINPKT;
 
-	IFQ_ENQUEUE(&sc->sc_ifp->if_snd, m, dummy_error);
-	schednetisr(NETISR_PFSYNC);
+	if (!_IF_QFULL(&sc->sc_ifp->if_snd))
+		_IF_ENQUEUE(&sc->sc_ifp->if_snd, m);
+	else {
+		m_freem(m);
+                sc->sc_ifp->if_snd.ifq_drops++;
+	}
+	if (schedswi)
+		swi_sched(V_pfsync_swi_cookie, 0);
 #else
 	sc->sc_if.if_opackets++;
 	sc->sc_if.if_obytes += m->m_pkthdr.len;
@@ -2424,11 +2393,7 @@
 	pfsync_q_ins(st, PFSYNC_S_INS);
 
 	if (ISSET(st->state_flags, PFSTATE_ACK))
-#ifdef __FreeBSD__
-		pfsync_sendout();
-#else
 		schednetisr(NETISR_PFSYNC);
-#endif
 	else
 		st->sync_updates = 0;
 }
@@ -2627,11 +2592,7 @@
 
 	if (sync || (time_uptime - st->pfsync_time) < 2) {
 		pfsync_upds++;
-#ifdef __FreeBSD__
-		pfsync_sendout();
-#else
 		schednetisr(NETISR_PFSYNC);
-#endif
 	}
 }
 
@@ -2667,7 +2628,7 @@
 		nlen += sizeof(struct pfsync_subheader);
 
 #ifdef __FreeBSD__
-	if (sc->sc_len + nlen > sc->sc_sync_if->if_mtu) {
+	if (sc->sc_len + nlen > sc->sc_ifp->if_mtu) {
 #else
 	if (sc->sc_len + nlen > sc->sc_if.if_mtu) {
 #endif
@@ -2682,11 +2643,7 @@
 	TAILQ_INSERT_TAIL(&sc->sc_upd_req_list, item, ur_entry);
 	sc->sc_len += nlen;
 
-#ifdef __FreeBSD__
-	pfsync_sendout();
-#else
 	schednetisr(NETISR_PFSYNC);
-#endif
 }
 
 void
@@ -2715,11 +2672,7 @@
 		pfsync_q_del(st);
 	case PFSYNC_S_NONE:
 		pfsync_q_ins(st, PFSYNC_S_UPD);
-#ifdef __FreeBSD__
-		pfsync_sendout();
-#else
 		schednetisr(NETISR_PFSYNC);
-#endif
 		return;
 
 	case PFSYNC_S_INS:
@@ -2883,7 +2836,7 @@
 	int q = st->sync_state;
 
 #ifdef __FreeBSD__
-	KASSERT(st->sync_state != PFSYNC_S_NONE, 
+	KASSERT(st->sync_state != PFSYNC_S_NONE,
 		("%s: st->sync_state != PFSYNC_S_NONE", __FUNCTION__));
 #else
 	KASSERT(st->sync_state != PFSYNC_S_NONE);
@@ -3014,7 +2967,7 @@
 		printf("pfsync: received bulk update request\n");
 
 #ifdef __FreeBSD__
-	PF_LOCK();
+	PF_LOCK_ASSERT();
 	if (TAILQ_EMPTY(&V_state_list))
 #else
 	if (TAILQ_EMPTY(&state_list))
@@ -3028,15 +2981,11 @@
 #else
 			sc->sc_bulk_next = TAILQ_FIRST(&state_list);
 #endif
-			sc->sc_bulk_last = sc->sc_bulk_next;
-
-			pfsync_bulk_status(PFSYNC_BUS_START);
-			callout_reset(&sc->sc_bulk_tmo, 1,
-			    pfsync_bulk_update, sc);
+		sc->sc_bulk_last = sc->sc_bulk_next;
+
+		pfsync_bulk_status(PFSYNC_BUS_START);
+		callout_reset(&sc->sc_bulk_tmo, 1, pfsync_bulk_update, sc);
 	}
-#ifdef __FreeBSD__
-	PF_UNLOCK();
-#endif
 }
 
 void
@@ -3294,7 +3243,11 @@
 	CURVNET_SET(sc->sc_ifp->if_vnet);
 	pfsync_ints++;
 
-	IF_DEQUEUE_ALL(&sc->sc_ifp->if_snd, m);
+	PF_LOCK();
+	if (sc->sc_len > PFSYNC_MINPKT)
+		pfsync_sendout1(0);
+	_IF_DEQUEUE_ALL(&sc->sc_ifp->if_snd, m);
+	PF_UNLOCK();
 
 	for (; m != NULL; m = n) {
 
@@ -3343,54 +3296,91 @@
 }
 
 #ifdef __FreeBSD__
-void
-pfsync_ifdetach(void *arg, struct ifnet *ifp)
+static int
+pfsync_multicast_setup(struct pfsync_softc *sc)
 {
-	struct pfsync_softc *sc = (struct pfsync_softc *)arg;
-	struct ip_moptions *imo;
-
-	if (sc == NULL || sc->sc_sync_if != ifp)
-		return;         /* not for us; unlocked read */
-
-	CURVNET_SET(sc->sc_ifp->if_vnet);
-
-	PF_LOCK();
-
-	/* Deal with a member interface going away from under us. */
-	sc->sc_sync_if = NULL;
-	imo = &sc->sc_imo;
-	if (imo->imo_num_memberships > 0) {
-		KASSERT(imo->imo_num_memberships == 1,
-		    ("%s: imo_num_memberships != 1", __func__));
-		/*
-		 * Our event handler is always called after protocol
-		 * domains have been detached from the underlying ifnet.
-		 * Do not call in_delmulti(); we held a single reference
-		 * which the protocol domain has purged in in_purgemaddrs().
-		 */
-		PF_UNLOCK();
-		imo->imo_membership[--imo->imo_num_memberships] = NULL;
-		PF_LOCK();
-		imo->imo_multicast_ifp = NULL;
+	struct ip_moptions *imo = &sc->sc_imo;
+	int error;
+
+	if (!(sc->sc_sync_if->if_flags & IFF_MULTICAST)) {
+		sc->sc_sync_if = NULL;
+		return (EADDRNOTAVAIL);
 	}
 
-	PF_UNLOCK();
-	
-	CURVNET_RESTORE();
+	imo->imo_membership = (struct in_multi **)malloc(
+	    (sizeof(struct in_multi *) * IP_MIN_MEMBERSHIPS), M_PFSYNC,
+	    M_WAITOK | M_ZERO);
+	imo->imo_max_memberships = IP_MIN_MEMBERSHIPS;
+	imo->imo_multicast_vif = -1;
+
+	if ((error = in_joingroup(sc->sc_sync_if, &sc->sc_sync_peer, NULL,
+	    &imo->imo_membership[0])) != 0) {
+		free(imo->imo_membership, M_PFSYNC);
+		return (error);
+	}
+	imo->imo_num_memberships++;
+	imo->imo_multicast_ifp = sc->sc_sync_if;
+	imo->imo_multicast_ttl = PFSYNC_DFLTTL;
+	imo->imo_multicast_loop = 0;
+
+	return (0);
 }
 
+static void
+pfsync_multicast_cleanup(struct pfsync_softc *sc)
+{
+	struct ip_moptions *imo = &sc->sc_imo;
+
+	in_leavegroup(imo->imo_membership[0], NULL);
+	free(imo->imo_membership, M_PFSYNC);
+	imo->imo_membership = NULL;
+	imo->imo_multicast_ifp = NULL;
+}
+
+#ifdef INET
+extern  struct domain inetdomain;
+static struct protosw in_pfsync_protosw = {
+	.pr_type =		SOCK_RAW,
+	.pr_domain =		&inetdomain,
+	.pr_protocol =		IPPROTO_PFSYNC,
+	.pr_flags =		PR_ATOMIC|PR_ADDR,
+	.pr_input =		pfsync_input,
+	.pr_output =		(pr_output_t *)rip_output,
+	.pr_ctloutput =		rip_ctloutput,
+	.pr_usrreqs =		&rip_usrreqs
+};
+#endif
+
 static int
-vnet_pfsync_init(const void *unused)
+pfsync_init()
 {
+	VNET_ITERATOR_DECL(vnet_iter);
 	int error = 0;
 
-	pfsyncattach(0);
-
-	error = swi_add(NULL, "pfsync", pfsyncintr, V_pfsyncif,
-		SWI_NET, INTR_MPSAFE, &pfsync_swi.pfsync_swi_cookie);
+	VNET_LIST_RLOCK();
+	VNET_FOREACH(vnet_iter) {
+		CURVNET_SET(vnet_iter);
+		V_pfsync_cloner = pfsync_cloner;
+		V_pfsync_cloner_data = pfsync_cloner_data;
+		V_pfsync_cloner.ifc_data = &V_pfsync_cloner_data;
+		if_clone_attach(&V_pfsync_cloner);
+		error = swi_add(NULL, "pfsync", pfsyncintr, V_pfsyncif,
+		    SWI_NET, INTR_MPSAFE, &V_pfsync_swi_cookie);
+		CURVNET_RESTORE();
+		if (error)
+			goto fail_locked;
+	}
+	VNET_LIST_RUNLOCK();
+#ifdef INET
+	error = pf_proto_register(PF_INET, &in_pfsync_protosw);
 	if (error)
-		panic("%s: swi_add %d", __func__, error);
-
+		goto fail;
+	error = ipproto_register(IPPROTO_PFSYNC);
+	if (error) {
+		pf_proto_unregister(PF_INET, IPPROTO_PFSYNC, SOCK_RAW);
+		goto fail;
+	}
+#endif
 	PF_LOCK();
 	pfsync_state_import_ptr = pfsync_state_import;
 	pfsync_up_ptr = pfsync_up;
@@ -3403,13 +3393,27 @@
 	PF_UNLOCK();
 
 	return (0);
+
+fail:
+	VNET_LIST_RLOCK();
+fail_locked:
+	VNET_FOREACH(vnet_iter) {
+		CURVNET_SET(vnet_iter);
+		if (V_pfsync_swi_cookie) {
+			swi_remove(V_pfsync_swi_cookie);
+			if_clone_detach(&V_pfsync_cloner);
+		}
+		CURVNET_RESTORE();
+	}
+	VNET_LIST_RUNLOCK();
+
+	return (error);
 }
 
-static int
-vnet_pfsync_uninit(const void *unused)
+static void
+pfsync_uninit()
 {
-
-	swi_remove(pfsync_swi.pfsync_swi_cookie);
+	VNET_ITERATOR_DECL(vnet_iter);
 
 	PF_LOCK();
 	pfsync_state_import_ptr = NULL;
@@ -3422,30 +3426,18 @@
 	pfsync_defer_ptr = NULL;
 	PF_UNLOCK();
 
-	if_clone_detach(&pfsync_cloner);
-
-	return (0);
+	ipproto_unregister(IPPROTO_PFSYNC);
+	pf_proto_unregister(PF_INET, IPPROTO_PFSYNC, SOCK_RAW);
+	VNET_LIST_RLOCK();
+	VNET_FOREACH(vnet_iter) {
+		CURVNET_SET(vnet_iter);
+		swi_remove(V_pfsync_swi_cookie);
+		if_clone_detach(&V_pfsync_cloner);
+		CURVNET_RESTORE();
+	}
+	VNET_LIST_RUNLOCK();
 }
 
-/* Define startup order. */
-#define	PFSYNC_SYSINIT_ORDER	SI_SUB_PROTO_IF
-#define	PFSYNC_MODEVENT_ORDER	(SI_ORDER_FIRST) /* On boot slot in here. */
-#define	PFSYNC_VNET_ORDER	(PFSYNC_MODEVENT_ORDER + 2) /* Later still. */
-
-/*
- * Starting up.
- * VNET_SYSINIT is called for each existing vnet and each new vnet.
- */
-VNET_SYSINIT(vnet_pfsync_init, PFSYNC_SYSINIT_ORDER, PFSYNC_VNET_ORDER,
-    vnet_pfsync_init, NULL);
-
-/*
- * Closing up shop. These are done in REVERSE ORDER,
- * Not called on reboot.
- * VNET_SYSUNINIT is called for each exiting vnet as it exits.
- */
-VNET_SYSUNINIT(vnet_pfsync_uninit, PFSYNC_SYSINIT_ORDER, PFSYNC_VNET_ORDER,
-    vnet_pfsync_uninit, NULL);
 static int
 pfsync_modevent(module_t mod, int type, void *data)
 {
@@ -3453,21 +3445,23 @@
 
 	switch (type) {
 	case MOD_LOAD:
-#ifndef __FreeBSD__
-		pfsyncattach(0);
-#endif
+		error = pfsync_init();
+		break;
+	case MOD_QUIESCE:
+		/*
+		 * Module should not be unloaded due to race conditions.
+		 */
+		error = EPERM;
 		break;
 	case MOD_UNLOAD:
-#ifndef __FreeBSD__
-		if_clone_detach(&pfsync_cloner);
-#endif
+		pfsync_uninit();
 		break;
 	default:
 		error = EINVAL;
 		break;
 	}
 
-	return error;
+	return (error);
 }
 
 static moduledata_t pfsync_mod = {
@@ -3478,7 +3472,7 @@
 
 #define PFSYNC_MODVER 1
 
-DECLARE_MODULE(pfsync, pfsync_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
+DECLARE_MODULE(pfsync, pfsync_mod, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
 MODULE_VERSION(pfsync, PFSYNC_MODVER);
 MODULE_DEPEND(pfsync, pf, PF_MODVER, PF_MODVER, PF_MODVER);
 #endif /* __FreeBSD__ */
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/pf/net/pf.c
--- a/head/sys/contrib/pf/net/pf.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/pf/net/pf.c	Fri Jan 13 15:51:01 2012 +0200
@@ -40,30 +40,14 @@
 #include "opt_inet6.h"
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/contrib/pf/net/pf.c 228150 2011-11-30 08:47:17Z glebius $");
+__FBSDID("$FreeBSD: head/sys/contrib/pf/net/pf.c 229851 2012-01-09 08:55:23Z glebius $");
 #endif
 
 #ifdef __FreeBSD__
 #include "opt_bpf.h"
 #include "opt_pf.h"
 
-#ifdef DEV_BPF
-#define	NBPFILTER	DEV_BPF
-#else
-#define	NBPFILTER	0
-#endif
-
-#ifdef DEV_PFLOG
-#define	NPFLOG		DEV_PFLOG
-#else
-#define	NPFLOG		0
-#endif
-
-#ifdef DEV_PFSYNC
-#define	NPFSYNC		DEV_PFSYNC
-#else
-#define	NPFSYNC		0
-#endif
+#define	NPFSYNC		1
 
 #ifdef DEV_PFLOW
 #define	NPFLOW		DEV_PFLOW
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/pf/net/pf_ioctl.c
--- a/head/sys/contrib/pf/net/pf_ioctl.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/pf/net/pf_ioctl.c	Fri Jan 13 15:51:01 2012 +0200
@@ -37,18 +37,14 @@
 
 #ifdef __FreeBSD__
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/contrib/pf/net/pf_ioctl.c 226536 2011-10-19 11:04:49Z bz $");
+__FBSDID("$FreeBSD: head/sys/contrib/pf/net/pf_ioctl.c 229852 2012-01-09 09:19:00Z glebius $");
 
 #include "opt_inet.h"
 #include "opt_inet6.h"
 #include "opt_bpf.h"
 #include "opt_pf.h"
 
-#ifdef DEV_BPF
-#define		NBPFILTER	DEV_BPF
-#else
-#define		NBPFILTER	0
-#endif
+#define		NPFSYNC		1
 
 #ifdef DEV_PFLOG
 #define		NPFLOG		DEV_PFLOG
@@ -56,16 +52,10 @@
 #define		NPFLOG		0
 #endif
 
-#ifdef DEV_PFSYNC
-#define		NPFSYNC		DEV_PFSYNC
-#else
-#define		NPFSYNC		0
-#endif
-
-#else
+#else /* !__FreeBSD__ */
 #include "pfsync.h"
 #include "pflog.h"
-#endif
+#endif /* __FreeBSD__ */
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -4328,57 +4318,25 @@
 	return (0);
 }
 
-/* Vnet accessors */
-static int
-vnet_pf_init(const void *unused) 
-{
-
-	V_pf_pfil_hooked = 0;
-	V_pf_end_threads = 0;
-
-	V_debug_pfugidhack = 0;
-
-	TAILQ_INIT(&V_pf_tags);
-	TAILQ_INIT(&V_pf_qids);
-
-	pf_load();
-
-	return (0);
-}
-
-static int
-vnet_pf_uninit(const void *unused)
-{
-
-	pf_unload();
-
-	return (0);
-}
-
-/* Define startup order. */
-#define	PF_SYSINIT_ORDER	SI_SUB_PROTO_BEGIN
-#define	PF_MODEVENT_ORDER	(SI_ORDER_FIRST) /* On boot slot in here. */
-#define	PF_VNET_ORDER		(PF_MODEVENT_ORDER + 2) /* Later still. */
-
-/*
- * Starting up.
- * VNET_SYSINIT is called for each existing vnet and each new vnet.
- */
-VNET_SYSINIT(vnet_pf_init, PF_SYSINIT_ORDER, PF_VNET_ORDER,
-    vnet_pf_init, NULL);
-
-/*
- * Closing up shop. These are done in REVERSE ORDER,
- * Not called on reboot.
- * VNET_SYSUNINIT is called for each exiting vnet as it exits.
- */
-VNET_SYSUNINIT(vnet_pf_uninit, PF_SYSINIT_ORDER, PF_VNET_ORDER,
-    vnet_pf_uninit, NULL);
-
 static int
 pf_load(void)
 {
-
+	VNET_ITERATOR_DECL(vnet_iter);
+
+	VNET_LIST_RLOCK();
+	VNET_FOREACH(vnet_iter) {
+		CURVNET_SET(vnet_iter);
+		V_pf_pfil_hooked = 0;
+		V_pf_end_threads = 0;
+		V_debug_pfugidhack = 0;
+		TAILQ_INIT(&V_pf_tags);
+		TAILQ_INIT(&V_pf_qids);
+		CURVNET_RESTORE();
+	}
+	VNET_LIST_RUNLOCK();
+
+	init_pf_mutex();
+	pf_dev = make_dev(&pf_cdevsw, 0, 0, 0, 0600, PF_NAME);
 	init_zone_var();
 	sx_init(&V_pf_consistency_lock, "pf_statetbl_lock");
 	if (pfattach() < 0)
@@ -4395,6 +4353,7 @@
 	PF_LOCK();
 	V_pf_status.running = 0;
 	PF_UNLOCK();
+	m_addr_chg_pf_p = NULL;
 	error = dehook_pf();
 	if (error) {
 		/*
@@ -4417,6 +4376,8 @@
 	pf_osfp_cleanup();
 	cleanup_pf_zone();
 	PF_UNLOCK();
+	destroy_dev(pf_dev);
+	destroy_pf_mutex();
 	sx_destroy(&V_pf_consistency_lock);
 	return error;
 }
@@ -4428,12 +4389,16 @@
 
 	switch(type) {
 	case MOD_LOAD:
-		init_pf_mutex();
-		pf_dev = make_dev(&pf_cdevsw, 0, 0, 0, 0600, PF_NAME);
+		error = pf_load();
+		break;
+	case MOD_QUIESCE:
+		/*
+		 * Module should not be unloaded due to race conditions.
+		 */
+		error = EPERM;
 		break;
 	case MOD_UNLOAD:
-		destroy_dev(pf_dev);
-		destroy_pf_mutex();
+		error = pf_unload();
 		break;
 	default:
 		error = EINVAL;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/pf/net/pfvar.h
--- a/head/sys/contrib/pf/net/pfvar.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/pf/net/pfvar.h	Fri Jan 13 15:51:01 2012 +0200
@@ -241,15 +241,8 @@
 
 #define	PF_LOCK_ASSERT()	mtx_assert(&pf_task_mtx, MA_OWNED)
 #define	PF_UNLOCK_ASSERT()	mtx_assert(&pf_task_mtx, MA_NOTOWNED)
-
-#define	PF_LOCK()	do {				\
-	PF_UNLOCK_ASSERT();				\
-	mtx_lock(&pf_task_mtx);				\
-} while(0)
-#define	PF_UNLOCK()	do {				\
-	PF_LOCK_ASSERT();				\
-	mtx_unlock(&pf_task_mtx);			\
-} while(0)
+#define	PF_LOCK()		mtx_lock(&pf_task_mtx)
+#define	PF_UNLOCK()		mtx_unlock(&pf_task_mtx)
 #else
 #define	PF_LOCK_ASSERT()
 #define	PF_UNLOCK_ASSERT()
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/contrib/rdma/ib_addr.h
--- a/head/sys/contrib/rdma/ib_addr.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/contrib/rdma/ib_addr.h	Fri Jan 13 15:51:01 2012 +0200
@@ -25,7 +25,7 @@
  * notice, one of the license notices in the documentation
  * and/or other materials provided with the distribution.
  *
- * $FreeBSD$
+ * $FreeBSD: head/sys/contrib/rdma/ib_addr.h 229753 2012-01-07 00:47:27Z dim $
  *
  */
 
@@ -42,7 +42,7 @@
 #include <contrib/rdma/ib_verbs.h>
 
 
-#define MAX_ADDR_LEN ETHER_ADDR_LEN	/* XXX doesn't support IB! */
+#define MAX_ADDR_LEN	20
 
 struct rdma_addr_client {
 	int refcount;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/ahci/ahci.c
--- a/head/sys/dev/ahci/ahci.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/ahci/ahci.c	Fri Jan 13 15:51:01 2012 +0200
@@ -25,7 +25,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/dev/ahci/ahci.c 228200 2011-12-02 12:52:33Z mav $");
+__FBSDID("$FreeBSD: head/sys/dev/ahci/ahci.c 229671 2012-01-06 00:22:55Z jimharris $");
 
 #include <sys/param.h>
 #include <sys/module.h>
@@ -172,6 +172,7 @@
 	{0x1d028086, 0x00, "Intel Patsburg",	0},
 	{0x1d048086, 0x00, "Intel Patsburg",	0},
 	{0x1d068086, 0x00, "Intel Patsburg",	0},
+	{0x28268086, 0x00, "Intel Patsburg (RAID)",	0},
 	{0x1e028086, 0x00, "Intel Panther Point",	0},
 	{0x1e038086, 0x00, "Intel Panther Point",	0},
 	{0x1e048086, 0x00, "Intel Panther Point",	0},
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/an/if_an.c
--- a/head/sys/dev/an/if_an.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/an/if_an.c	Fri Jan 13 15:51:01 2012 +0200
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/dev/an/if_an.c 228621 2011-12-17 10:23:17Z bschmidt $");
+__FBSDID("$FreeBSD: head/sys/dev/an/if_an.c 229767 2012-01-07 09:41:57Z kevlo $");
 
 /*
  * The Aironet 4500/4800 series cards come in PCMCIA, ISA and PCI form.
@@ -761,7 +761,6 @@
 	ifp->if_softc = sc;
 	if_initname(ifp, device_get_name(sc->an_dev),
 	    device_get_unit(sc->an_dev));
-	ifp->if_mtu = ETHERMTU;
 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
 	ifp->if_ioctl = an_ioctl;
 	ifp->if_start = an_start;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/ata/ata-pci.h
--- a/head/sys/dev/ata/ata-pci.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/ata/ata-pci.h	Fri Jan 13 15:51:01 2012 +0200
@@ -23,7 +23,7 @@
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
- * $FreeBSD: head/sys/dev/ata/ata-pci.h 228497 2011-12-14 13:12:55Z mav $
+ * $FreeBSD: head/sys/dev/ata/ata-pci.h 229671 2012-01-06 00:22:55Z jimharris $
  */
 
 /* structure holding chipset config info */
@@ -237,6 +237,7 @@
 #define ATA_PBG_AH1		0x1d028086
 #define ATA_PBG_R1		0x1d048086
 #define ATA_PBG_R2		0x1d068086
+#define ATA_PBG_R3		0x28268086
 #define ATA_PBG_S2		0x1d088086
 
 #define ATA_PPT_S1		0x1e008086
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/ata/atapi-cam.c
--- a/head/sys/dev/ata/atapi-cam.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/ata/atapi-cam.c	Fri Jan 13 15:51:01 2012 +0200
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/dev/ata/atapi-cam.c 208822 2010-06-05 08:58:03Z mav $");
+__FBSDID("$FreeBSD: head/sys/dev/ata/atapi-cam.c 229997 2012-01-12 00:34:33Z ken $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -576,9 +576,10 @@
 	    struct scsi_inquiry *inq = (struct scsi_inquiry *) &request->u.atapi.ccb[0];
 
 	    if (inq->byte2 == 0 && inq->page_code == 0 &&
-		inq->length > SHORT_INQUIRY_LENGTH) {
+		scsi_2btoul(inq->length) > SHORT_INQUIRY_LENGTH) {
 		bzero(buf, len);
-		len = inq->length = SHORT_INQUIRY_LENGTH;
+		len = SHORT_INQUIRY_LENGTH;
+		scsi_ulto2b(len, inq->length);
 	    }
 	    break;
 	}
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/ata/chipsets/ata-intel.c
--- a/head/sys/dev/ata/chipsets/ata-intel.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/ata/chipsets/ata-intel.c	Fri Jan 13 15:51:01 2012 +0200
@@ -25,7 +25,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/dev/ata/chipsets/ata-intel.c 228497 2011-12-14 13:12:55Z mav $");
+__FBSDID("$FreeBSD: head/sys/dev/ata/chipsets/ata-intel.c 229671 2012-01-06 00:22:55Z jimharris $");
 
 #include "opt_ata.h"
 #include <sys/param.h>
@@ -197,6 +197,7 @@
      { ATA_PBG_AH1,      0, INTEL_AHCI, 0, ATA_SA300, "Patsburg" },
      { ATA_PBG_R1,       0, INTEL_AHCI, 0, ATA_SA300, "Patsburg" },
      { ATA_PBG_R2,       0, INTEL_AHCI, 0, ATA_SA300, "Patsburg" },
+     { ATA_PBG_R3,       0, INTEL_AHCI, 0, ATA_SA300, "Patsburg" },
      { ATA_PBG_S2,       0, INTEL_6CH2, 0, ATA_SA300, "Patsburg" },
      { ATA_PPT_S1,       0, INTEL_6CH,  0, ATA_SA300, "Panther Point" },
      { ATA_PPT_S2,       0, INTEL_6CH,  0, ATA_SA300, "Panther Point" },
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/ath/ath_hal/ah_desc.h
--- a/head/sys/dev/ath/ath_hal/ah_desc.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/ath/ath_hal/ah_desc.h	Fri Jan 13 15:51:01 2012 +0200
@@ -14,7 +14,7 @@
  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  *
- * $FreeBSD: head/sys/dev/ath/ath_hal/ah_desc.h 226759 2011-10-25 23:09:07Z adrian $
+ * $FreeBSD: head/sys/dev/ath/ath_hal/ah_desc.h 229791 2012-01-07 20:23:05Z adrian $
  */
 
 #ifndef _DEV_ATH_DESC_H
@@ -23,6 +23,19 @@
 #include "opt_ah.h"		/* NB: required for AH_SUPPORT_AR5416 */
 
 /*
+ * For now, define this for the structure definitions.
+ * Because of how the HAL / driver module currently builds,
+ * it's not very feasible to build the module without
+ * this defined.  The rest of the code (eg in the driver
+ * body) can work fine with these fields being uninitialised;
+ * they'll be initialised to 0 anyway.
+ */
+
+#ifndef	AH_SUPPORT_AR5416
+#define	AH_SUPPORT_AR5416	1
+#endif
+
+/*
  * Transmit descriptor status.  This structure is filled
  * in only after the tx descriptor process method finds a
  * ``done'' descriptor; at which point it returns something
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/ath/ath_hal/ah_internal.h
--- a/head/sys/dev/ath/ath_hal/ah_internal.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/ath/ath_hal/ah_internal.h	Fri Jan 13 15:51:01 2012 +0200
@@ -14,7 +14,7 @@
  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  *
- * $FreeBSD: head/sys/dev/ath/ath_hal/ah_internal.h 227410 2011-11-09 22:39:44Z adrian $
+ * $FreeBSD: head/sys/dev/ath/ath_hal/ah_internal.h 229791 2012-01-07 20:23:05Z adrian $
  */
 #ifndef _ATH_AH_INTERAL_H_
 #define _ATH_AH_INTERAL_H_
@@ -30,6 +30,10 @@
 #include <net80211/_ieee80211.h>
 #include "opt_ah.h"			/* needed for AH_SUPPORT_AR5416 */
 
+#ifndef	AH_SUPPORT_AR5416
+#define	AH_SUPPORT_AR5416	1
+#endif
+
 #ifndef NBBY
 #define	NBBY	8			/* number of bits/byte */
 #endif
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/ath/if_ath.c
--- a/head/sys/dev/ath/if_ath.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/ath/if_ath.c	Fri Jan 13 15:51:01 2012 +0200
@@ -28,7 +28,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/dev/ath/if_ath.c 228891 2011-12-26 07:47:05Z adrian $");
+__FBSDID("$FreeBSD: head/sys/dev/ath/if_ath.c 229950 2012-01-11 00:18:33Z adrian $");
 
 /*
  * Driver for the Atheros Wireless LAN controller.
@@ -2486,6 +2486,13 @@
 	if (IEEE80211_IS_CHAN_HT(ic->ic_curchan))
 		rfilt |= HAL_RX_FILTER_COMPBAR;
 
+	/*
+	 * Enable radar PHY errors if requested by the
+	 * DFS module.
+	 */
+	if (sc->sc_dodfs)
+		rfilt |= HAL_RX_FILTER_PHYRADAR;
+
 	DPRINTF(sc, ATH_DEBUG_MODE, "%s: RX filter 0x%x, %s if_flags 0x%x\n",
 	    __func__, rfilt, ieee80211_opmode_name[ic->ic_opmode], ifp->if_flags);
 	return rfilt;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/ath/if_ath_tx.c
--- a/head/sys/dev/ath/if_ath_tx.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/ath/if_ath_tx.c	Fri Jan 13 15:51:01 2012 +0200
@@ -28,7 +28,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/dev/ath/if_ath_tx.c 229165 2012-01-01 01:08:51Z adrian $");
+__FBSDID("$FreeBSD: head/sys/dev/ath/if_ath_tx.c 229949 2012-01-11 00:16:44Z adrian $");
 
 /*
  * Driver for the Atheros Wireless LAN controller.
@@ -435,7 +435,8 @@
 	 * Setup the last descriptor in the list.
 	 * bf_prev points to the last; bf is NULL here.
 	 */
-	ath_hal_setuplasttxdesc(sc->sc_ah, bf_prev->bf_desc, bf_first->bf_desc);
+	ath_hal_setuplasttxdesc(sc->sc_ah, bf_prev->bf_desc,
+	    bf_first->bf_desc);
 
 	/*
 	 * Set the first descriptor bf_lastds field to point to
@@ -481,7 +482,8 @@
  * Hand-off packet to a hardware queue.
  */
 static void
-ath_tx_handoff_hw(struct ath_softc *sc, struct ath_txq *txq, struct ath_buf *bf)
+ath_tx_handoff_hw(struct ath_softc *sc, struct ath_txq *txq,
+    struct ath_buf *bf)
 {
 	struct ath_hal *ah = sc->sc_ah;
 
@@ -548,7 +550,8 @@
 			 * frame at SWBA.
 			 */
 			if (!qbusy) {
-				ath_hal_puttxbuf(ah, txq->axq_qnum, bf->bf_daddr);
+				ath_hal_puttxbuf(ah, txq->axq_qnum,
+				    bf->bf_daddr);
 				txq->axq_flags &= ~ATH_TXQ_PUTPENDING;
 				DPRINTF(sc, ATH_DEBUG_XMIT,
 				    "%s: TXDP[%u] = %p (%p) depth %d\n",
@@ -566,7 +569,8 @@
 			DPRINTF(sc, ATH_DEBUG_XMIT,
 			    "%s: link[%u](%p)=%p (%p) depth %d\n", __func__,
 			    txq->axq_qnum, txq->axq_link,
-			    (caddr_t)bf->bf_daddr, bf->bf_desc, txq->axq_depth);
+			    (caddr_t)bf->bf_daddr, bf->bf_desc,
+			    txq->axq_depth);
 			if ((txq->axq_flags & ATH_TXQ_PUTPENDING) && !qbusy) {
 				/*
 				 * The q was busy when we previously tried
@@ -599,7 +603,8 @@
 			DPRINTF(sc, ATH_DEBUG_XMIT,
 			    "%s: link[%u](%p)=%p (%p) depth %d\n", __func__,
 			    txq->axq_qnum, txq->axq_link,
-			    (caddr_t)bf->bf_daddr, bf->bf_desc, txq->axq_depth);
+			    (caddr_t)bf->bf_daddr, bf->bf_desc,
+			    txq->axq_depth);
 		}
 #endif /* IEEE80211_SUPPORT_TDMA */
 		if (bf->bf_state.bfs_aggr)
@@ -652,7 +657,8 @@
 
 static int
 ath_tx_tag_crypto(struct ath_softc *sc, struct ieee80211_node *ni,
-    struct mbuf *m0, int iswep, int isfrag, int *hdrlen, int *pktlen, int *keyix)
+    struct mbuf *m0, int iswep, int isfrag, int *hdrlen, int *pktlen,
+    int *keyix)
 {
 	if (iswep) {
 		const struct ieee80211_cipher *cip;
@@ -671,7 +677,7 @@
 			 * 802.11 layer counts failures and provides
 			 * debugging/diagnostics.
 			 */
-			return 0;
+			return (0);
 		}
 		/*
 		 * Adjust the packet + header lengths for the crypto
@@ -698,7 +704,7 @@
 	} else
 		(*keyix) = HAL_TXKEYIX_INVALID;
 
-	return 1;
+	return (1);
 }
 
 static uint8_t
@@ -720,7 +726,7 @@
 	if (shortPreamble)
 		ctsrate |= rt->info[cix].shortPreamble;
 
-	return ctsrate;
+	return (ctsrate);
 }
 
 /*
@@ -737,7 +743,7 @@
 	if (rt->info[cix].phy == IEEE80211_T_HT) {
 		printf("%s: HT rate where it shouldn't be (0x%x)\n",
 		    __func__, rt->info[cix].rateCode);
-		return -1;
+		return (-1);
 	}
 
 	/*
@@ -765,7 +771,7 @@
 			ctsduration += rt->info[rix].lpAckDuration;
 	}
 
-	return ctsduration;
+	return (ctsduration);
 }
 
 /*
@@ -1906,9 +1912,11 @@
 
 	if (bf->bf_state.bfs_addedbaw)
 		device_printf(sc->sc_dev,
-		    "%s: re-added? tid=%d, seqno %d; window %d:%d; baw head=%d tail=%d\n",
+		    "%s: re-added? tid=%d, seqno %d; window %d:%d; "
+		    "baw head=%d tail=%d\n",
 		    __func__, tid->tid, SEQNO(bf->bf_state.bfs_seqno),
-		    tap->txa_start, tap->txa_wnd, tid->baw_head, tid->baw_tail);
+		    tap->txa_start, tap->txa_wnd, tid->baw_head,
+		    tid->baw_tail);
 
 	/*
 	 * ni->ni_txseqs[] is the currently allocated seqno.
@@ -1917,9 +1925,11 @@
 	index  = ATH_BA_INDEX(tap->txa_start, SEQNO(bf->bf_state.bfs_seqno));
 	cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
-	    "%s: tid=%d, seqno %d; window %d:%d; index=%d cindex=%d baw head=%d tail=%d\n",
+	    "%s: tid=%d, seqno %d; window %d:%d; index=%d cindex=%d "
+	    "baw head=%d tail=%d\n",
 	    __func__, tid->tid, SEQNO(bf->bf_state.bfs_seqno),
-	    tap->txa_start, tap->txa_wnd, index, cindex, tid->baw_head, tid->baw_tail);
+	    tap->txa_start, tap->txa_wnd, index, cindex, tid->baw_head,
+	    tid->baw_tail);
 
 
 #if 0
@@ -1941,7 +1951,8 @@
 	}
 	tid->tx_buf[cindex] = bf;
 
-	if (index >= ((tid->baw_tail - tid->baw_head) & (ATH_TID_MAX_BUFS - 1))) {
+	if (index >= ((tid->baw_tail - tid->baw_head) &
+	    (ATH_TID_MAX_BUFS - 1))) {
 		tid->baw_tail = cindex;
 		INCR(tid->baw_tail, ATH_TID_MAX_BUFS);
 	}
@@ -2019,7 +2030,8 @@
 	cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
 
 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
-	    "%s: tid=%d, baw=%d:%d, seqno=%d, index=%d, cindex=%d, baw head=%d, tail=%d\n",
+	    "%s: tid=%d, baw=%d:%d, seqno=%d, index=%d, cindex=%d, "
+	    "baw head=%d, tail=%d\n",
 	    __func__, tid->tid, tap->txa_start, tap->txa_wnd, seqno, index,
 	    cindex, tid->baw_head, tid->baw_tail);
 
@@ -2044,11 +2056,13 @@
 
 	tid->tx_buf[cindex] = NULL;
 
-	while (tid->baw_head != tid->baw_tail && !tid->tx_buf[tid->baw_head]) {
+	while (tid->baw_head != tid->baw_tail &&
+	    !tid->tx_buf[tid->baw_head]) {
 		INCR(tap->txa_start, IEEE80211_SEQ_RANGE);
 		INCR(tid->baw_head, ATH_TID_MAX_BUFS);
 	}
-	DPRINTF(sc, ATH_DEBUG_SW_TX_BAW, "%s: baw is now %d:%d, baw head=%d\n",
+	DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
+	    "%s: baw is now %d:%d, baw head=%d\n",
 	    __func__, tap->txa_start, tap->txa_wnd, tid->baw_head);
 }
 
@@ -2248,24 +2262,34 @@
 	ATH_TXQ_LOCK(txq);
 	if (atid->paused) {
 		/* TID is paused, queue */
+		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: paused\n", __func__);
 		ATH_TXQ_INSERT_TAIL(atid, bf, bf_list);
 	} else if (ath_tx_ampdu_pending(sc, an, tid)) {
 		/* AMPDU pending; queue */
+		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: pending\n", __func__);
 		ATH_TXQ_INSERT_TAIL(atid, bf, bf_list);
 		/* XXX sched? */
 	} else if (ath_tx_ampdu_running(sc, an, tid)) {
 		/* AMPDU running, attempt direct dispatch if possible */
-		if (txq->axq_depth < sc->sc_hwq_limit)
+		if (txq->axq_depth < sc->sc_hwq_limit) {
 			ath_tx_xmit_aggr(sc, an, bf);
-		else {
+			DPRINTF(sc, ATH_DEBUG_SW_TX,
+			    "%s: xmit_aggr\n",
+			    __func__);
+		} else {
+			DPRINTF(sc, ATH_DEBUG_SW_TX,
+			    "%s: ampdu; swq'ing\n",
+			    __func__);
 			ATH_TXQ_INSERT_TAIL(atid, bf, bf_list);
 			ath_tx_tid_sched(sc, atid);
 		}
 	} else if (txq->axq_depth < sc->sc_hwq_limit) {
 		/* AMPDU not running, attempt direct dispatch */
+		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: xmit_normal\n", __func__);
 		ath_tx_xmit_normal(sc, txq, bf);
 	} else {
 		/* Busy; queue */
+		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: swq'ing\n", __func__);
 		ATH_TXQ_INSERT_TAIL(atid, bf, bf_list);
 		ath_tx_tid_sched(sc, atid);
 	}
@@ -2374,8 +2398,8 @@
  * forward.
  */
 static void
-ath_tx_tid_drain(struct ath_softc *sc, struct ath_node *an, struct ath_tid *tid,
-    ath_bufhead *bf_cq)
+ath_tx_tid_drain(struct ath_softc *sc, struct ath_node *an,
+    struct ath_tid *tid, ath_bufhead *bf_cq)
 {
 	struct ath_buf *bf;
 	struct ieee80211_tx_ampdu *tap;
@@ -2398,8 +2422,8 @@
 			device_printf(sc->sc_dev,
 			    "%s: node %p: tid %d: txq_depth=%d, "
 			    "txq_aggr_depth=%d, sched=%d, paused=%d, "
-			    "hwq_depth=%d, incomp=%d, baw_head=%d, baw_tail=%d "
-			    "txa_start=%d, ni_txseqs=%d\n",
+			    "hwq_depth=%d, incomp=%d, baw_head=%d, "
+			    "baw_tail=%d txa_start=%d, ni_txseqs=%d\n",
 			     __func__, ni, tid->tid, txq->axq_depth,
 			     txq->axq_aggr_depth, tid->sched, tid->paused,
 			     tid->hwq_depth, tid->incomp, tid->baw_head,
@@ -2644,7 +2668,8 @@
 				if (! bf->bf_state.bfs_addedbaw)
 					device_printf(sc->sc_dev,
 					    "%s: wasn't added: seqno %d\n",
-					    __func__, SEQNO(bf->bf_state.bfs_seqno));
+					    __func__,
+					    SEQNO(bf->bf_state.bfs_seqno));
 			}
 			bf->bf_state.bfs_dobaw = 0;
 			/*
@@ -3061,7 +3086,8 @@
  * not the last descriptor in the first frame.
  */
 static void
-ath_tx_aggr_comp_aggr(struct ath_softc *sc, struct ath_buf *bf_first, int fail)
+ath_tx_aggr_comp_aggr(struct ath_softc *sc, struct ath_buf *bf_first,
+    int fail)
 {
 	//struct ath_desc *ds = bf->bf_lastds;
 	struct ieee80211_node *ni = bf_first->bf_node;
@@ -3148,7 +3174,8 @@
 	memcpy(rc, bf_first->bf_state.bfs_rc, sizeof(rc));
 
 	DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
-	    "%s: txa_start=%d, tx_ok=%d, status=%.8x, flags=%.8x, isaggr=%d, seq_st=%d, hasba=%d, ba=%.8x, %.8x\n",
+	    "%s: txa_start=%d, tx_ok=%d, status=%.8x, flags=%.8x, "
+	    "isaggr=%d, seq_st=%d, hasba=%d, ba=%.8x, %.8x\n",
 	    __func__, tap->txa_start, tx_ok, ts.ts_status, ts.ts_flags,
 	    isaggr, seq_st, hasba, ba[0], ba[1]);
 
@@ -3162,7 +3189,8 @@
 	/* AR5416 BA bug; this requires an interface reset */
 	if (isaggr && tx_ok && (! hasba)) {
 		device_printf(sc->sc_dev,
-		    "%s: AR5416 bug: hasba=%d; txok=%d, isaggr=%d, seq_st=%d\n",
+		    "%s: AR5416 bug: hasba=%d; txok=%d, isaggr=%d, "
+		    "seq_st=%d\n",
 		    __func__, hasba, tx_ok, isaggr, seq_st);
 		/* XXX TODO: schedule an interface reset */
 	}
@@ -3193,7 +3221,8 @@
 	 */
 	while (bf) {
 		nframes++;
-		ba_index = ATH_BA_INDEX(seq_st, SEQNO(bf->bf_state.bfs_seqno));
+		ba_index = ATH_BA_INDEX(seq_st,
+		    SEQNO(bf->bf_state.bfs_seqno));
 		bf_next = bf->bf_next;
 		bf->bf_next = NULL;	/* Remove it from the aggr list */
 
@@ -3245,7 +3274,8 @@
 	 * control code.
 	 */
 	if (fail == 0)
-		ath_tx_update_ratectrl(sc, ni, rc, &ts, pktlen, nframes, nbad);
+		ath_tx_update_ratectrl(sc, ni, rc, &ts, pktlen, nframes,
+		    nbad);
 
 	/*
 	 * send bar if we dropped any frames
@@ -3313,8 +3343,10 @@
 	if (tid == IEEE80211_NONQOS_TID)
 		device_printf(sc->sc_dev, "%s: TID=16!\n", __func__);
 
-	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: bf=%p: tid=%d, hwq_depth=%d\n",
-	    __func__, bf, bf->bf_state.bfs_tid, atid->hwq_depth);
+	DPRINTF(sc, ATH_DEBUG_SW_TX,
+	    "%s: bf=%p: tid=%d, hwq_depth=%d, seqno=%d\n",
+	    __func__, bf, bf->bf_state.bfs_tid, atid->hwq_depth,
+	    SEQNO(bf->bf_state.bfs_seqno));
 
 	atid->hwq_depth--;
 	if (atid->hwq_depth < 0)
@@ -3329,6 +3361,8 @@
 	 */
 	if (atid->cleanup_inprogress) {
 		ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
+		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: cleanup_unaggr\n",
+		    __func__);
 		ath_tx_comp_cleanup_unaggr(sc, bf);
 		return;
 	}
@@ -3339,6 +3373,8 @@
 	 */
 	if (fail == 0 && ts->ts_status & HAL_TXERR_XRETRY) {
 		ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
+		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: retry_unaggr\n",
+		    __func__);
 		ath_tx_aggr_retry_unaggr(sc, bf);
 		return;
 	}
@@ -3419,7 +3455,8 @@
 		 * data frame), schedule it directly; continue.
 		 */
 		if (! bf->bf_state.bfs_dobaw) {
-			DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR, "%s: non-baw packet\n",
+			DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
+			    "%s: non-baw packet\n",
 			    __func__);
 			ATH_TXQ_REMOVE(tid, bf, bf_list);
 			bf->bf_state.bfs_aggr = 0;
@@ -3489,7 +3526,8 @@
 				sc->sc_aggr_stats.aggr_single_pkt++;
 		} else {
 			DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
-			    "%s: multi-frame aggregate: %d frames, length %d\n",
+			    "%s: multi-frame aggregate: %d frames, "
+			    "length %d\n",
 			     __func__, bf->bf_state.bfs_nframes,
 			    bf->bf_state.bfs_al);
 			bf->bf_state.bfs_aggr = 1;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/bfe/if_bfe.c
--- a/head/sys/dev/bfe/if_bfe.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/bfe/if_bfe.c	Fri Jan 13 15:51:01 2012 +0200
@@ -27,7 +27,7 @@
 
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/dev/bfe/if_bfe.c 227843 2011-11-22 21:28:20Z marius $");
+__FBSDID("$FreeBSD: head/sys/dev/bfe/if_bfe.c 229767 2012-01-07 09:41:57Z kevlo $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -337,7 +337,6 @@
 	ifp->if_ioctl = bfe_ioctl;
 	ifp->if_start = bfe_start;
 	ifp->if_init = bfe_init;
-	ifp->if_mtu = ETHERMTU;
 	IFQ_SET_MAXLEN(&ifp->if_snd, BFE_TX_QLEN);
 	ifp->if_snd.ifq_drv_maxlen = BFE_TX_QLEN;
 	IFQ_SET_READY(&ifp->if_snd);
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/bm/if_bm.c
--- a/head/sys/dev/bm/if_bm.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/bm/if_bm.c	Fri Jan 13 15:51:01 2012 +0200
@@ -36,7 +36,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/dev/bm/if_bm.c 227843 2011-11-22 21:28:20Z marius $");
+__FBSDID("$FreeBSD: head/sys/dev/bm/if_bm.c 229767 2012-01-07 09:41:57Z kevlo $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -468,7 +468,6 @@
 
 	if_initname(ifp, device_get_name(sc->sc_dev),
 	    device_get_unit(sc->sc_dev));
-	ifp->if_mtu = ETHERMTU;
 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
 	ifp->if_start = bm_start;
 	ifp->if_ioctl = bm_ioctl;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/bxe/if_bxe.c
--- a/head/sys/dev/bxe/if_bxe.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/bxe/if_bxe.c	Fri Jan 13 15:51:01 2012 +0200
@@ -31,7 +31,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/dev/bxe/if_bxe.c 227843 2011-11-22 21:28:20Z marius $");
+__FBSDID("$FreeBSD: head/sys/dev/bxe/if_bxe.c 229767 2012-01-07 09:41:57Z kevlo $");
 
 /*
  * The following controllers are supported by this driver:
@@ -2136,7 +2136,6 @@
 #endif
 
 	ifp->if_init = bxe_init;
-	ifp->if_mtu = ETHERMTU;
 	ifp->if_hwassist = BXE_IF_HWASSIST;
 	ifp->if_capabilities = BXE_IF_CAPABILITIES;
 	/* TPA not enabled by default. */
@@ -14115,7 +14114,7 @@
 			i = 0;
 			config = BXE_SP(sc, mcast_config);
 
-			IF_ADDR_LOCK(ifp);
+			if_maddr_rlock(ifp);
 
 			TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
 				if (ifma->ifma_addr->sa_family != AF_LINK)
@@ -14144,7 +14143,7 @@
 				    config_table->cam_entry.lsb_mac_addr);
 			}
 
-			IF_ADDR_UNLOCK(ifp);
+			if_maddr_runlock(ifp);
 
 			old = config->hdr.length;
 
@@ -14172,7 +14171,7 @@
 			/* Accept one or more multicasts */
 			memset(mc_filter, 0, 4 * MC_HASH_SIZE);
 
-			IF_ADDR_LOCK(ifp);
+			if_maddr_rlock(ifp);
 
 			TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
 				if (ifma->ifma_addr->sa_family != AF_LINK)
@@ -14184,7 +14183,7 @@
 				bit &= 0x1f;
 				mc_filter[regidx] |= (1 << bit);
 			}
-			IF_ADDR_UNLOCK(ifp);
+			if_maddr_runlock(ifp);
 
 			for (i = 0; i < MC_HASH_SIZE; i++)
 				REG_WR(sc, MC_HASH_OFFSET(sc, i), mc_filter[i]);
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/ciss/ciss.c
--- a/head/sys/dev/ciss/ciss.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/ciss/ciss.c	Fri Jan 13 15:51:01 2012 +0200
@@ -24,7 +24,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- *	$FreeBSD: head/sys/dev/ciss/ciss.c 227293 2011-11-07 06:44:47Z ed $
+ *	$FreeBSD: head/sys/dev/ciss/ciss.c 229997 2012-01-12 00:34:33Z ken $
  */
 
 /*
@@ -1614,7 +1614,7 @@
     inq->opcode = INQUIRY;
     inq->byte2 = SI_EVPD;
     inq->page_code = CISS_VPD_LOGICAL_DRIVE_GEOMETRY;
-    inq->length = sizeof(ld->cl_geometry);
+    scsi_ulto2b(sizeof(ld->cl_geometry), inq->length);
 
     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
 	ciss_printf(sc, "error getting geometry (%d)\n", error);
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/e1000/if_em.c
--- a/head/sys/dev/e1000/if_em.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/e1000/if_em.c	Fri Jan 13 15:51:01 2012 +0200
@@ -30,7 +30,7 @@
   POSSIBILITY OF SUCH DAMAGE.
 
 ******************************************************************************/
-/*$FreeBSD: head/sys/dev/e1000/if_em.c 228415 2011-12-11 18:46:14Z jfv $*/
+/*$FreeBSD: head/sys/dev/e1000/if_em.c 229939 2012-01-10 19:57:23Z luigi $*/
 
 #ifdef HAVE_KERNEL_OPTION_HEADERS
 #include "opt_device_polling.h"
@@ -2898,7 +2898,6 @@
 		return (-1);
 	}
 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
-	ifp->if_mtu = ETHERMTU;
 	ifp->if_init =  em_init;
 	ifp->if_softc = adapter;
 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
@@ -3298,16 +3297,15 @@
 #ifdef DEV_NETMAP
 		if (slot) {
 			int si = i + na->tx_rings[txr->me].nkr_hwofs;
+			uint64_t paddr;
 			void *addr;
 
 			if (si >= na->num_tx_desc)
 				si -= na->num_tx_desc;
-			addr = NMB(slot + si);
-			txr->tx_base[i].buffer_addr =
-			    htole64(vtophys(addr));
+			addr = PNMB(slot + si, &paddr);
+			txr->tx_base[i].buffer_addr = htole64(paddr);
 			/* reload the map for netmap mode */
-			netmap_load_map(txr->txtag,
-			    txbuf->map, addr, na->buff_size);
+			netmap_load_map(txr->txtag, txbuf->map, addr);
 		}
 #endif /* DEV_NETMAP */
 
@@ -4105,9 +4103,6 @@
 		sj += adapter->num_rx_desc;
 
 	for (int j = 0; j != adapter->num_rx_desc; j++, sj++) {
-		void *addr;
-		int sz;
-
 		rxbuf = &rxr->rx_buffers[j];
 		/* no mbuf and regular mode -> skip this entry */
 		if (rxbuf->m_head == NULL && !slot)
@@ -4116,12 +4111,20 @@
 		if (sj >= adapter->num_rx_desc)
 			sj -= adapter->num_rx_desc;
 		/* see comment, set slot addr and map */
-		addr = slot ? NMB(slot + sj) : rxbuf->m_head->m_data;
-		sz = slot ? na->buff_size : adapter->rx_mbuf_sz;
-		// XXX load or reload ?
-		netmap_load_map(rxr->rxtag, rxbuf->map, addr, sz);
-		/* Update descriptor */
-		rxr->rx_base[j].buffer_addr = htole64(vtophys(addr));
+		if (slot) {
+			uint64_t paddr;
+			void *addr = PNMB(slot + sj, &paddr);
+			netmap_load_map(rxr->rxtag, rxbuf->map, addr);
+			/* Update descriptor */
+			rxr->rx_base[j].buffer_addr = htole64(paddr);
+		} else {
+			/* Get the memory mapping */
+			bus_dmamap_load_mbuf_sg(rxr->rxtag,
+			    rxbuf->map, rxbuf->m_head, seg,
+			    &nsegs, BUS_DMA_NOWAIT);
+			/* Update descriptor */
+			rxr->rx_base[j].buffer_addr = htole64(seg[0].ds_addr);
+		}
 		bus_dmamap_sync(rxr->rxtag, rxbuf->map, BUS_DMASYNC_PREREAD);
 	}
     }
@@ -4480,8 +4483,7 @@
 #endif
 			if (status & E1000_RXD_STAT_VP) {
 				sendmp->m_pkthdr.ether_vtag =
-				    (le16toh(cur->special) &
-				    E1000_RXD_SPC_VLAN_MASK);
+				    le16toh(cur->special);
 				sendmp->m_flags |= M_VLANTAG;
 			}
 #ifndef __NO_STRICT_ALIGNMENT
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/e1000/if_igb.c
--- a/head/sys/dev/e1000/if_igb.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/e1000/if_igb.c	Fri Jan 13 15:51:01 2012 +0200
@@ -30,7 +30,7 @@
   POSSIBILITY OF SUCH DAMAGE.
 
 ******************************************************************************/
-/*$FreeBSD: head/sys/dev/e1000/if_igb.c 228803 2011-12-22 15:33:41Z luigi $*/
+/*$FreeBSD: head/sys/dev/e1000/if_igb.c 229939 2012-01-10 19:57:23Z luigi $*/
 
 
 #ifdef HAVE_KERNEL_OPTION_HEADERS
@@ -2899,7 +2899,6 @@
 		return (-1);
 	}
 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
-	ifp->if_mtu = ETHERMTU;
 	ifp->if_init =  igb_init;
 	ifp->if_softc = adapter;
 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
@@ -3321,8 +3320,7 @@
 
 			if (si < 0)
 				si += na->num_tx_desc;
-			netmap_load_map(txr->txtag, txbuf->map,
-				NMB(slot + si), na->buff_size);
+			netmap_load_map(txr->txtag, txbuf->map, NMB(slot + si));
 		}
 #endif /* DEV_NETMAP */
 		/* clear the watch index */
@@ -4063,15 +4061,15 @@
 		if (slot) {
 			/* slot sj is mapped to the i-th NIC-ring entry */
 			int sj = j + na->rx_rings[rxr->me].nkr_hwofs;
+			uint64_t paddr;
 			void *addr;
 
 			if (sj < 0)
 				sj += na->num_rx_desc;
-			addr = NMB(slot + sj);
-			netmap_load_map(rxr->ptag,
-			    rxbuf->pmap, addr, na->buff_size);
+			addr = PNMB(slot + sj, &paddr);
+			netmap_load_map(rxr->ptag, rxbuf->pmap, addr);
 			/* Update descriptor */
-			rxr->rx_base[j].read.pkt_addr = htole64(vtophys(addr));
+			rxr->rx_base[j].read.pkt_addr = htole64(paddr);
 			continue;
 		}
 #endif /* DEV_NETMAP */
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/e1000/if_lem.c
--- a/head/sys/dev/e1000/if_lem.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/e1000/if_lem.c	Fri Jan 13 15:51:01 2012 +0200
@@ -30,7 +30,7 @@
   POSSIBILITY OF SUCH DAMAGE.
 
 ******************************************************************************/
-/*$FreeBSD: head/sys/dev/e1000/if_lem.c 228387 2011-12-10 07:08:52Z jfv $*/
+/*$FreeBSD: head/sys/dev/e1000/if_lem.c 229939 2012-01-10 19:57:23Z luigi $*/
 
 #ifdef HAVE_KERNEL_OPTION_HEADERS
 #include "opt_device_polling.h"
@@ -2354,7 +2354,6 @@
 		return (-1);
 	}
 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
-	ifp->if_mtu = ETHERMTU;
 	ifp->if_init =  lem_init;
 	ifp->if_softc = adapter;
 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
@@ -2671,16 +2670,15 @@
 		if (slot) {
 			/* slot si is mapped to the i-th NIC-ring entry */
 			int si = i + na->tx_rings[0].nkr_hwofs;
+			uint64_t paddr;
 			void *addr;
 
 			if (si > na->num_tx_desc)
 				si -= na->num_tx_desc;
-			addr = NMB(slot + si);
-			adapter->tx_desc_base[si].buffer_addr =
-			    htole64(vtophys(addr));
+			addr = PNMB(slot + si, &paddr);
+			adapter->tx_desc_base[si].buffer_addr = htole64(paddr);
 			/* reload the map for netmap mode */
-			netmap_load_map(adapter->txtag,
-			    tx_buffer->map, addr, na->buff_size);
+			netmap_load_map(adapter->txtag, tx_buffer->map, addr);
 		}
 #endif /* DEV_NETMAP */
 		tx_buffer->next_eop = -1;
@@ -3248,16 +3246,15 @@
 		if (slot) {
 			/* slot si is mapped to the i-th NIC-ring entry */
 			int si = i + na->rx_rings[0].nkr_hwofs;
+			uint64_t paddr;
 			void *addr;
 
 			if (si > na->num_rx_desc)
 				si -= na->num_rx_desc;
-			addr = NMB(slot + si);
-			netmap_load_map(adapter->rxtag,
-			    rx_buffer->map, addr, na->buff_size);
+			addr = PNMB(slot + si, &paddr);
+			netmap_load_map(adapter->rxtag, rx_buffer->map, addr);
 			/* Update descriptor */
-			adapter->rx_desc_base[i].buffer_addr =
-			    htole64(vtophys(addr));
+			adapter->rx_desc_base[i].buffer_addr = htole64(paddr);
 			continue;
 		}
 #endif /* DEV_NETMAP */
@@ -3591,8 +3588,7 @@
 #endif
 				if (status & E1000_RXD_STAT_VP) {
 					adapter->fmp->m_pkthdr.ether_vtag =
-					    (le16toh(current_desc->special) &
-					    E1000_RXD_SPC_VLAN_MASK);
+					    le16toh(current_desc->special);
 					adapter->fmp->m_flags |= M_VLANTAG;
 				}
 #ifndef __NO_STRICT_ALIGNMENT
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/ep/if_ep.c
--- a/head/sys/dev/ep/if_ep.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/ep/if_ep.c	Fri Jan 13 15:51:01 2012 +0200
@@ -29,7 +29,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
+__FBSDID("$FreeBSD: head/sys/dev/ep/if_ep.c 229767 2012-01-07 09:41:57Z kevlo $");
 
 /*
  *	Modified from the FreeBSD 1.1.5.1 version by:
@@ -301,7 +301,6 @@
 
 	ifp->if_softc = sc;
 	if_initname(ifp, device_get_name(sc->dev), device_get_unit(sc->dev));
-	ifp->if_mtu = ETHERMTU;
 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
 	ifp->if_start = epstart;
 	ifp->if_ioctl = epioctl;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/et/if_et.c
--- a/head/sys/dev/et/if_et.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/et/if_et.c	Fri Jan 13 15:51:01 2012 +0200
@@ -35,7 +35,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/dev/et/if_et.c 228369 2011-12-09 19:10:38Z yongari $");
+__FBSDID("$FreeBSD: head/sys/dev/et/if_et.c 229940 2012-01-10 20:52:02Z yongari $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -363,8 +363,9 @@
 static int
 et_detach(device_t dev)
 {
-	struct et_softc *sc = device_get_softc(dev);
-
+	struct et_softc *sc;
+
+	sc = device_get_softc(dev);
 	if (device_is_attached(dev)) {
 		ether_ifdetach(sc->ifp);
 		ET_LOCK(sc);
@@ -401,8 +402,9 @@
 static int
 et_shutdown(device_t dev)
 {
-	struct et_softc *sc = device_get_softc(dev);
-
+	struct et_softc *sc;
+
+	sc = device_get_softc(dev);
 	ET_LOCK(sc);
 	et_stop(sc);
 	ET_UNLOCK(sc);
@@ -412,10 +414,11 @@
 static int
 et_miibus_readreg(device_t dev, int phy, int reg)
 {
-	struct et_softc *sc = device_get_softc(dev);
+	struct et_softc *sc;
 	uint32_t val;
 	int i, ret;
 
+	sc = device_get_softc(dev);
 	/* Stop any pending operations */
 	CSR_WRITE_4(sc, ET_MII_CMD, 0);
 
@@ -455,10 +458,11 @@
 static int
 et_miibus_writereg(device_t dev, int phy, int reg, int val0)
 {
-	struct et_softc *sc = device_get_softc(dev);
+	struct et_softc *sc;
 	uint32_t val;
 	int i;
 
+	sc = device_get_softc(dev);
 	/* Stop any pending operations */
 	CSR_WRITE_4(sc, ET_MII_CMD, 0);
 
@@ -593,10 +597,12 @@
 static int
 et_ifmedia_upd_locked(struct ifnet *ifp)
 {
-	struct et_softc *sc = ifp->if_softc;
-	struct mii_data *mii = device_get_softc(sc->sc_miibus);
+	struct et_softc *sc;
+	struct mii_data *mii;
 	struct mii_softc *miisc;
 
+	sc = ifp->if_softc;
+	mii = device_get_softc(sc->sc_miibus);
 	LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
 		PHY_RESET(miisc);
 	return (mii_mediachg(mii));
@@ -605,9 +611,10 @@
 static int
 et_ifmedia_upd(struct ifnet *ifp)
 {
-	struct et_softc *sc = ifp->if_softc;
+	struct et_softc *sc;
 	int res;
 
+	sc = ifp->if_softc;
 	ET_LOCK(sc);
 	res = et_ifmedia_upd_locked(ifp);
 	ET_UNLOCK(sc);
@@ -638,10 +645,11 @@
 static void
 et_stop(struct et_softc *sc)
 {
-	struct ifnet *ifp = sc->ifp;
+	struct ifnet *ifp;
 
 	ET_LOCK_ASSERT(sc);
 
+	ifp = sc->ifp;
 	callout_stop(&sc->sc_tick);
 	/* Disable interrupts. */
 	CSR_WRITE_4(sc, ET_INTR_MASK, 0xffffffff);
@@ -757,6 +765,7 @@
 static void
 et_reset(struct et_softc *sc)
 {
+
 	CSR_WRITE_4(sc, ET_MAC_CFG1,
 		    ET_MAC_CFG1_RST_TXFUNC | ET_MAC_CFG1_RST_RXFUNC |
 		    ET_MAC_CFG1_RST_TXMC | ET_MAC_CFG1_RST_RXMC |
@@ -1163,10 +1172,11 @@
 static void
 et_intr(void *xsc)
 {
-	struct et_softc *sc = xsc;
+	struct et_softc *sc;
 	struct ifnet *ifp;
 	uint32_t status;
 
+	sc = xsc;
 	ET_LOCK(sc);
 	ifp = sc->ifp;
 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
@@ -1268,10 +1278,14 @@
 static int
 et_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
 {
-	struct et_softc *sc = ifp->if_softc;
-	struct mii_data *mii = device_get_softc(sc->sc_miibus);
-	struct ifreq *ifr = (struct ifreq *)data;
-	int error = 0, mask, max_framelen;
+	struct et_softc *sc;
+	struct mii_data *mii;
+	struct ifreq *ifr;
+	int error, mask, max_framelen;
+
+	sc = ifp->if_softc;
+	ifr = (struct ifreq *)data;
+	error = 0;
 
 /* XXX LOCKSUSED */
 	switch (cmd) {
@@ -1295,6 +1309,7 @@
 
 	case SIOCSIFMEDIA:
 	case SIOCGIFMEDIA:
+		mii = device_get_softc(sc->sc_miibus);
 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
 		break;
 
@@ -1424,8 +1439,9 @@
 static void
 et_start(struct ifnet *ifp)
 {
-	struct et_softc *sc = ifp->if_softc;
-
+	struct et_softc *sc;
+
+	sc = ifp->if_softc;
 	ET_LOCK(sc);
 	et_start_locked(ifp);
 	ET_UNLOCK(sc);
@@ -1456,6 +1472,7 @@
 static int
 et_stop_rxdma(struct et_softc *sc)
 {
+
 	CSR_WRITE_4(sc, ET_RXDMA_CTRL,
 		    ET_RXDMA_CTRL_HALT | ET_RXDMA_CTRL_RING1_ENABLE);
 
@@ -1470,6 +1487,7 @@
 static int
 et_stop_txdma(struct et_softc *sc)
 {
+
 	CSR_WRITE_4(sc, ET_TXDMA_CTRL,
 		    ET_TXDMA_CTRL_HALT | ET_TXDMA_CTRL_SINGLE_EPKT);
 	return (0);
@@ -1598,10 +1616,11 @@
 static int
 et_chip_init(struct et_softc *sc)
 {
-	struct ifnet *ifp = sc->ifp;
+	struct ifnet *ifp;
 	uint32_t rxq_end;
 	int error, frame_len, rxmem_size;
 
+	ifp = sc->ifp;
 	/*
 	 * Split 16Kbytes internal memory between TX and RX
 	 * according to frame length.
@@ -1716,8 +1735,8 @@
 static int
 et_init_rxdma(struct et_softc *sc)
 {
-	struct et_rxstatus_data *rxsd = &sc->sc_rx_status;
-	struct et_rxstat_ring *rxst_ring = &sc->sc_rxstat_ring;
+	struct et_rxstatus_data *rxsd;
+	struct et_rxstat_ring *rxst_ring;
 	struct et_rxdesc_ring *rx_ring;
 	int error;
 
@@ -1730,12 +1749,14 @@
 	/*
 	 * Install RX status
 	 */
+	rxsd = &sc->sc_rx_status;
 	CSR_WRITE_4(sc, ET_RX_STATUS_HI, ET_ADDR_HI(rxsd->rxsd_paddr));
 	CSR_WRITE_4(sc, ET_RX_STATUS_LO, ET_ADDR_LO(rxsd->rxsd_paddr));
 
 	/*
 	 * Install RX stat ring
 	 */
+	rxst_ring = &sc->sc_rxstat_ring;
 	CSR_WRITE_4(sc, ET_RXSTAT_HI, ET_ADDR_HI(rxst_ring->rsr_paddr));
 	CSR_WRITE_4(sc, ET_RXSTAT_LO, ET_ADDR_LO(rxst_ring->rsr_paddr));
 	CSR_WRITE_4(sc, ET_RXSTAT_CNT, ET_RX_NSTAT - 1);
@@ -1786,8 +1807,8 @@
 static int
 et_init_txdma(struct et_softc *sc)
 {
-	struct et_txdesc_ring *tx_ring = &sc->sc_tx_ring;
-	struct et_txstatus_data *txsd = &sc->sc_tx_status;
+	struct et_txdesc_ring *tx_ring;
+	struct et_txstatus_data *txsd;
 	int error;
 
 	error = et_stop_txdma(sc);
@@ -1799,6 +1820,7 @@
 	/*
 	 * Install TX descriptor ring
 	 */
+	tx_ring = &sc->sc_tx_ring;
 	CSR_WRITE_4(sc, ET_TX_RING_HI, ET_ADDR_HI(tx_ring->tr_paddr));
 	CSR_WRITE_4(sc, ET_TX_RING_LO, ET_ADDR_LO(tx_ring->tr_paddr));
 	CSR_WRITE_4(sc, ET_TX_RING_CNT, ET_TX_NDESC - 1);
@@ -1806,6 +1828,7 @@
 	/*
 	 * Install TX status
 	 */
+	txsd = &sc->sc_tx_status;
 	CSR_WRITE_4(sc, ET_TX_STATUS_HI, ET_ADDR_HI(txsd->txsd_paddr));
 	CSR_WRITE_4(sc, ET_TX_STATUS_LO, ET_ADDR_LO(txsd->txsd_paddr));
 
@@ -1821,8 +1844,8 @@
 static void
 et_init_mac(struct et_softc *sc)
 {
-	struct ifnet *ifp = sc->ifp;
-	const uint8_t *eaddr = IF_LLADDR(ifp);
+	struct ifnet *ifp;
+	const uint8_t *eaddr;
 	uint32_t val;
 
 	/* Reset MAC */
@@ -1858,6 +1881,8 @@
 	/*
 	 * Set MAC address
 	 */
+	ifp = sc->ifp;
+	eaddr = IF_LLADDR(ifp);
 	val = eaddr[2] | (eaddr[3] << 8) | (eaddr[4] << 16) | (eaddr[5] << 24);
 	CSR_WRITE_4(sc, ET_MAC_ADDR1, val);
 	val = (eaddr[0] << 16) | (eaddr[1] << 24);
@@ -1873,8 +1898,8 @@
 static void
 et_init_rxmac(struct et_softc *sc)
 {
-	struct ifnet *ifp = sc->ifp;
-	const uint8_t *eaddr = IF_LLADDR(ifp);
+	struct ifnet *ifp;
+	const uint8_t *eaddr;
 	uint32_t val;
 	int i;
 
@@ -1892,6 +1917,8 @@
 	/*
 	 * Set WOL source address.  XXX is this necessary?
 	 */
+	ifp = sc->ifp;
+	eaddr = IF_LLADDR(ifp);
 	val = (eaddr[2] << 24) | (eaddr[3] << 16) | (eaddr[4] << 8) | eaddr[5];
 	CSR_WRITE_4(sc, ET_WOL_SA_LO, val);
 	val = (eaddr[0] << 8) | eaddr[1];
@@ -1958,6 +1985,7 @@
 static void
 et_init_txmac(struct et_softc *sc)
 {
+
 	/* Disable TX MAC and FC(?) */
 	CSR_WRITE_4(sc, ET_TXMAC_CTRL, ET_TXMAC_CTRL_FC_DISABLE);
 
@@ -1976,12 +2004,12 @@
 static int
 et_start_rxdma(struct et_softc *sc)
 {
-	uint32_t val = 0;
-
-	val |= (sc->sc_rx_data[0].rbd_bufsize & ET_RXDMA_CTRL_RING0_SIZE_MASK) |
-	       ET_RXDMA_CTRL_RING0_ENABLE;
+	uint32_t val;
+
+	val = (sc->sc_rx_data[0].rbd_bufsize & ET_RXDMA_CTRL_RING0_SIZE_MASK) |
+	    ET_RXDMA_CTRL_RING0_ENABLE;
 	val |= (sc->sc_rx_data[1].rbd_bufsize & ET_RXDMA_CTRL_RING1_SIZE_MASK) |
-	       ET_RXDMA_CTRL_RING1_ENABLE;
+	    ET_RXDMA_CTRL_RING1_ENABLE;
 
 	CSR_WRITE_4(sc, ET_RXDMA_CTRL, val);
 
@@ -1997,6 +2025,7 @@
 static int
 et_start_txdma(struct et_softc *sc)
 {
+
 	CSR_WRITE_4(sc, ET_TXDMA_CTRL, ET_TXDMA_CTRL_SINGLE_EPKT);
 	return (0);
 }
@@ -2274,10 +2303,11 @@
 static void
 et_tick(void *xsc)
 {
-	struct et_softc *sc = xsc;
+	struct et_softc *sc;
 	struct ifnet *ifp;
 	struct mii_data *mii;
 
+	sc = xsc;
 	ET_LOCK_ASSERT(sc);
 	ifp = sc->ifp;
 	mii = device_get_softc(sc->sc_miibus);
@@ -2539,10 +2569,12 @@
 static int
 et_sysctl_rx_intr_npkts(SYSCTL_HANDLER_ARGS)
 {
-	struct et_softc *sc = arg1;
-	struct ifnet *ifp = sc->ifp;
-	int error = 0, v;
-
+	struct et_softc *sc;
+	struct ifnet *ifp;
+	int error, v;
+
+	sc = arg1;
+	ifp = sc->ifp;
 	v = sc->sc_rx_intr_npkts;
 	error = sysctl_handle_int(oidp, &v, 0, req);
 	if (error || req->newptr == NULL)
@@ -2564,10 +2596,12 @@
 static int
 et_sysctl_rx_intr_delay(SYSCTL_HANDLER_ARGS)
 {
-	struct et_softc *sc = arg1;
-	struct ifnet *ifp = sc->ifp;
-	int error = 0, v;
-
+	struct et_softc *sc;
+	struct ifnet *ifp;
+	int error, v;
+
+	sc = arg1;
+	ifp = sc->ifp;
 	v = sc->sc_rx_intr_delay;
 	error = sysctl_handle_int(oidp, &v, 0, req);
 	if (error || req->newptr == NULL)
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/ex/if_ex.c
--- a/head/sys/dev/ex/if_ex.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/ex/if_ex.c	Fri Jan 13 15:51:01 2012 +0200
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
+__FBSDID("$FreeBSD: head/sys/dev/ex/if_ex.c 229767 2012-01-07 09:41:57Z kevlo $");
 
 /*
  * Intel EtherExpress Pro/10, Pro/10+ Ethernet driver
@@ -232,7 +232,6 @@
 	 */
 	ifp->if_softc = sc;
 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
-	ifp->if_mtu = ETHERMTU;
 	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
 	ifp->if_start = ex_start;
 	ifp->if_ioctl = ex_ioctl;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/firewire/if_fwe.c
--- a/head/sys/dev/firewire/if_fwe.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/firewire/if_fwe.c	Fri Jan 13 15:51:01 2012 +0200
@@ -31,7 +31,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  * 
- * $FreeBSD: head/sys/dev/firewire/if_fwe.c 227309 2011-11-07 15:43:11Z ed $
+ * $FreeBSD: head/sys/dev/firewire/if_fwe.c 229767 2012-01-07 09:41:57Z kevlo $
  */
 
 #ifdef HAVE_KERNEL_OPTION_HEADERS
@@ -214,7 +214,6 @@
 #endif
 	ifp->if_start = fwe_start;
 	ifp->if_ioctl = fwe_ioctl;
-	ifp->if_mtu = ETHERMTU;
 	ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST);
 	ifp->if_snd.ifq_maxlen = TX_MAX_QUEUE;
 
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/ichwd/ichwd.c
--- a/head/sys/dev/ichwd/ichwd.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/ichwd/ichwd.c	Fri Jan 13 15:51:01 2012 +0200
@@ -56,7 +56,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/dev/ichwd/ichwd.c 225340 2011-09-02 17:06:50Z delphij $");
+__FBSDID("$FreeBSD: head/sys/dev/ichwd/ichwd.c 229598 2012-01-05 16:27:32Z jhb $");
 
 #include <sys/param.h>
 #include <sys/kernel.h>
@@ -198,26 +198,26 @@
 static devclass_t ichwd_devclass;
 
 #define ichwd_read_tco_1(sc, off) \
-	bus_space_read_1((sc)->tco_bst, (sc)->tco_bsh, (off))
+	bus_read_1((sc)->tco_res, (off))
 #define ichwd_read_tco_2(sc, off) \
-	bus_space_read_2((sc)->tco_bst, (sc)->tco_bsh, (off))
+	bus_read_2((sc)->tco_res, (off))
 #define ichwd_read_tco_4(sc, off) \
-	bus_space_read_4((sc)->tco_bst, (sc)->tco_bsh, (off))
+	bus_read_4((sc)->tco_res, (off))
 #define ichwd_read_smi_4(sc, off) \
-	bus_space_read_4((sc)->smi_bst, (sc)->smi_bsh, (off))
+	bus_read_4((sc)->smi_res, (off))
 #define ichwd_read_gcs_4(sc, off) \
-	bus_space_read_4((sc)->gcs_bst, (sc)->gcs_bsh, (off))
+	bus_read_4((sc)->gcs_res, (off))
 
 #define ichwd_write_tco_1(sc, off, val) \
-	bus_space_write_1((sc)->tco_bst, (sc)->tco_bsh, (off), (val))
+	bus_write_1((sc)->tco_res, (off), (val))
 #define ichwd_write_tco_2(sc, off, val) \
-	bus_space_write_2((sc)->tco_bst, (sc)->tco_bsh, (off), (val))
+	bus_write_2((sc)->tco_res, (off), (val))
 #define ichwd_write_tco_4(sc, off, val) \
-	bus_space_write_4((sc)->tco_bst, (sc)->tco_bsh, (off), (val))
+	bus_write_4((sc)->tco_res, (off), (val))
 #define ichwd_write_smi_4(sc, off, val) \
-	bus_space_write_4((sc)->smi_bst, (sc)->smi_bsh, (off), (val))
+	bus_write_4((sc)->smi_res, (off), (val))
 #define ichwd_write_gcs_4(sc, off, val) \
-	bus_space_write_4((sc)->gcs_bst, (sc)->gcs_bsh, (off), (val))
+	bus_write_4((sc)->gcs_res, (off), (val))
 
 #define ichwd_verbose_printf(dev, ...) \
 	do {						\
@@ -519,8 +519,6 @@
 		device_printf(dev, "unable to reserve SMI registers\n");
 		goto fail;
 	}
-	sc->smi_bst = rman_get_bustag(sc->smi_res);
-	sc->smi_bsh = rman_get_bushandle(sc->smi_res);
 
 	sc->tco_rid = 1;
 	sc->tco_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->tco_rid,
@@ -530,8 +528,6 @@
 		device_printf(dev, "unable to reserve TCO registers\n");
 		goto fail;
 	}
-	sc->tco_bst = rman_get_bustag(sc->tco_res);
-	sc->tco_bsh = rman_get_bushandle(sc->tco_res);
 
 	sc->gcs_rid = 0;
 	if (sc->ich_version >= 6) {
@@ -541,12 +537,6 @@
 			device_printf(dev, "unable to reserve GCS registers\n");
 			goto fail;
 		}
-		sc->gcs_bst = rman_get_bustag(sc->gcs_res);
-		sc->gcs_bsh = rman_get_bushandle(sc->gcs_res);
-	} else {
-		sc->gcs_res = 0;
-		sc->gcs_bst = 0;
-		sc->gcs_bsh = 0;
 	}
 
 	if (ichwd_clear_noreboot(sc) != 0)
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/ichwd/ichwd.h
--- a/head/sys/dev/ichwd/ichwd.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/ichwd/ichwd.h	Fri Jan 13 15:51:01 2012 +0200
@@ -25,7 +25,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $FreeBSD: head/sys/dev/ichwd/ichwd.h 221789 2011-05-11 20:31:27Z jfv $
+ * $FreeBSD: head/sys/dev/ichwd/ichwd.h 229598 2012-01-05 16:27:32Z jhb $
  */
 
 #ifndef _ICHWD_H_
@@ -48,18 +48,12 @@
 	int			 smi_enabled;
 	int			 smi_rid;
 	struct resource		*smi_res;
-	bus_space_tag_t		 smi_bst;
-	bus_space_handle_t	 smi_bsh;
 
 	int			 tco_rid;
 	struct resource		*tco_res;
-	bus_space_tag_t		 tco_bst;
-	bus_space_handle_t	 tco_bsh;
 
 	int			 gcs_rid;
 	struct resource		*gcs_res;
-	bus_space_tag_t		 gcs_bst;
-	bus_space_handle_t	 gcs_bsh;
 
 	eventhandler_tag	 ev_tag;
 };
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/ie/if_ie.c
--- a/head/sys/dev/ie/if_ie.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/ie/if_ie.c	Fri Jan 13 15:51:01 2012 +0200
@@ -51,7 +51,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
+__FBSDID("$FreeBSD: head/sys/dev/ie/if_ie.c 229767 2012-01-07 09:41:57Z kevlo $");
 
 /*
  * Intel 82586 Ethernet chip
@@ -313,7 +313,6 @@
 
 	ifp->if_softc = sc;
 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
-	ifp->if_mtu = ETHERMTU;
 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
 	ifp->if_start = iestart;
 	ifp->if_ioctl = ieioctl;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/if_ndis/if_ndis.c
--- a/head/sys/dev/if_ndis/if_ndis.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/if_ndis/if_ndis.c	Fri Jan 13 15:51:01 2012 +0200
@@ -34,7 +34,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/dev/if_ndis/if_ndis.c 228621 2011-12-17 10:23:17Z bschmidt $");
+__FBSDID("$FreeBSD: head/sys/dev/if_ndis/if_ndis.c 229767 2012-01-07 09:41:57Z kevlo $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -714,7 +714,6 @@
 	ndis_probe_offload(sc);
 
 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
-	ifp->if_mtu = ETHERMTU;
 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
 	ifp->if_ioctl = ndis_ioctl;
 	ifp->if_start = ndis_start;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/ixgb/if_ixgb.c
--- a/head/sys/dev/ixgb/if_ixgb.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/ixgb/if_ixgb.c	Fri Jan 13 15:51:01 2012 +0200
@@ -31,7 +31,7 @@
 
 ***************************************************************************/
 
-/*$FreeBSD$*/
+/*$FreeBSD: head/sys/dev/ixgb/if_ixgb.c 229767 2012-01-07 09:41:57Z kevlo $*/
 
 #ifdef HAVE_KERNEL_OPTION_HEADERS
 #include "opt_device_polling.h"
@@ -1354,7 +1354,6 @@
 	ifp->if_unit = device_get_unit(dev);
 	ifp->if_name = "ixgb";
 #endif
-	ifp->if_mtu = ETHERMTU;
 	ifp->if_baudrate = 1000000000;
 	ifp->if_init = ixgb_init;
 	ifp->if_softc = adapter;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/ixgbe/ixgbe.c
--- a/head/sys/dev/ixgbe/ixgbe.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/ixgbe/ixgbe.c	Fri Jan 13 15:51:01 2012 +0200
@@ -30,7 +30,7 @@
   POSSIBILITY OF SUCH DAMAGE.
 
 ******************************************************************************/
-/*$FreeBSD: head/sys/dev/ixgbe/ixgbe.c 228440 2011-12-12 18:27:28Z mdf $*/
+/*$FreeBSD: head/sys/dev/ixgbe/ixgbe.c 229939 2012-01-10 19:57:23Z luigi $*/
 
 #ifdef HAVE_KERNEL_OPTION_HEADERS
 #include "opt_inet.h"
@@ -2431,7 +2431,6 @@
 		return (-1);
 	}
 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
-	ifp->if_mtu = ETHERMTU;
 	ifp->if_baudrate = 1000000000;
 	ifp->if_init = ixgbe_init;
 	ifp->if_softc = adapter;
@@ -2877,8 +2876,7 @@
 
 			if (si >= na->num_tx_desc)
 				si -= na->num_tx_desc;
-			netmap_load_map(txr->txtag, txbuf->map,
-			    NMB(slot + si), na->buff_size);
+			netmap_load_map(txr->txtag, txbuf->map, NMB(slot + si));
 		}
 #endif /* DEV_NETMAP */
 		/* Clear the EOP index */
@@ -3811,16 +3809,15 @@
 		 */
 		if (slot) {
 			int sj = j + na->rx_rings[rxr->me].nkr_hwofs;
+			uint64_t paddr;
 			void *addr;
 
 			if (sj >= na->num_rx_desc)
 				sj -= na->num_rx_desc;
-			addr = NMB(slot + sj);
-			netmap_load_map(rxr->ptag,
-			    rxbuf->pmap, addr, na->buff_size);
+			addr = PNMB(slot + sj, &paddr);
+			netmap_load_map(rxr->ptag, rxbuf->pmap, addr);
 			/* Update descriptor */
-			rxr->rx_base[j].read.pkt_addr =
-			    htole64(vtophys(addr));
+			rxr->rx_base[j].read.pkt_addr = htole64(paddr);
 			continue;
 		}
 #endif /* DEV_NETMAP */
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/ixgbe/ixv.c
--- a/head/sys/dev/ixgbe/ixv.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/ixgbe/ixv.c	Fri Jan 13 15:51:01 2012 +0200
@@ -30,7 +30,7 @@
   POSSIBILITY OF SUCH DAMAGE.
 
 ******************************************************************************/
-/*$FreeBSD: head/sys/dev/ixgbe/ixv.c 228440 2011-12-12 18:27:28Z mdf $*/
+/*$FreeBSD: head/sys/dev/ixgbe/ixv.c 229767 2012-01-07 09:41:57Z kevlo $*/
 
 #ifdef HAVE_KERNEL_OPTION_HEADERS
 #include "opt_inet.h"
@@ -1829,7 +1829,6 @@
 	if (ifp == NULL)
 		panic("%s: can not if_alloc()\n", device_get_nameunit(dev));
 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
-	ifp->if_mtu = ETHERMTU;
 	ifp->if_baudrate = 1000000000;
 	ifp->if_init = ixv_init;
 	ifp->if_softc = adapter;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/lge/if_lge.c
--- a/head/sys/dev/lge/if_lge.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/lge/if_lge.c	Fri Jan 13 15:51:01 2012 +0200
@@ -32,7 +32,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/dev/lge/if_lge.c 227843 2011-11-22 21:28:20Z marius $");
+__FBSDID("$FreeBSD: head/sys/dev/lge/if_lge.c 229767 2012-01-07 09:41:57Z kevlo $");
 
 /*
  * Level 1 LXT1001 gigabit ethernet driver for FreeBSD. Public
@@ -536,7 +536,6 @@
 	}
 	ifp->if_softc = sc;
 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
-	ifp->if_mtu = ETHERMTU;
 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
 	ifp->if_ioctl = lge_ioctl;
 	ifp->if_start = lge_start;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/msk/if_msk.c
--- a/head/sys/dev/msk/if_msk.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/msk/if_msk.c	Fri Jan 13 15:51:01 2012 +0200
@@ -99,7 +99,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/dev/msk/if_msk.c 228717 2011-12-19 19:02:36Z yongari $");
+__FBSDID("$FreeBSD: head/sys/dev/msk/if_msk.c 229767 2012-01-07 09:41:57Z kevlo $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -1636,7 +1636,6 @@
 	}
 	ifp->if_softc = sc_if;
 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
-	ifp->if_mtu = ETHERMTU;
 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
 	ifp->if_capabilities = IFCAP_TXCSUM | IFCAP_TSO4;
 	/*
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/my/if_my.c
--- a/head/sys/dev/my/if_my.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/my/if_my.c	Fri Jan 13 15:51:01 2012 +0200
@@ -28,7 +28,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/dev/my/if_my.c 223624 2011-06-28 08:36:48Z kevlo $");
+__FBSDID("$FreeBSD: head/sys/dev/my/if_my.c 229767 2012-01-07 09:41:57Z kevlo $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -895,7 +895,6 @@
 	}
 	ifp->if_softc = sc;
 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
-	ifp->if_mtu = ETHERMTU;
 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
 	ifp->if_ioctl = my_ioctl;
 	ifp->if_start = my_start;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/netmap/if_em_netmap.h
--- a/head/sys/dev/netmap/if_em_netmap.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/netmap/if_em_netmap.h	Fri Jan 13 15:51:01 2012 +0200
@@ -24,7 +24,7 @@
  */
 
 /*
- * $FreeBSD: head/sys/dev/netmap/if_em_netmap.h 228844 2011-12-23 16:02:14Z luigi $
+ * $FreeBSD: head/sys/dev/netmap/if_em_netmap.h 229939 2012-01-10 19:57:23Z luigi $
  * $Id: if_em_netmap.h 9802 2011-12-02 18:42:37Z luigi $
  *
  * netmap changes for if_em.
@@ -222,7 +222,8 @@
 			int flags = ((slot->flags & NS_REPORT) ||
 				j == 0 || j == report_frequency) ?
 					E1000_TXD_CMD_RS : 0;
-			void *addr = NMB(slot);
+			uint64_t paddr;
+			void *addr = PNMB(slot, &paddr);
 			int len = slot->len;
 			if (addr == netmap_buffer_base || len > NETMAP_BUF_SIZE) {
 				if (do_lock)
@@ -236,10 +237,9 @@
 			    htole32(adapter->txd_cmd | len |
 				(E1000_TXD_CMD_EOP | flags) );
 			if (slot->flags & NS_BUF_CHANGED) {
-				curr->buffer_addr = htole64(vtophys(addr));
+				curr->buffer_addr = htole64(paddr);
 				/* buffer has changed, reload map */
-				netmap_reload_map(txr->txtag, txbuf->map,
-				    addr, na->buff_size);
+				netmap_reload_map(txr->txtag, txbuf->map, addr);
 				slot->flags &= ~NS_BUF_CHANGED;
 			}
 
@@ -355,7 +355,8 @@
 			struct netmap_slot *slot = &ring->slot[j];
 			struct e1000_rx_desc *curr = &rxr->rx_base[l];
 			struct em_buffer *rxbuf = &rxr->rx_buffers[l];
-			void *addr = NMB(slot);
+			uint64_t paddr;
+			void *addr = PNMB(slot, &paddr);
 
 			if (addr == netmap_buffer_base) { /* bad buf */
 				if (do_lock)
@@ -365,10 +366,9 @@
 
 			curr->status = 0;
 			if (slot->flags & NS_BUF_CHANGED) {
-				curr->buffer_addr = htole64(vtophys(addr));
+				curr->buffer_addr = htole64(paddr);
 				/* buffer has changed, reload map */
-				netmap_reload_map(rxr->rxtag, rxbuf->map,
-				    addr, na->buff_size);
+				netmap_reload_map(rxr->rxtag, rxbuf->map, addr);
 				slot->flags &= ~NS_BUF_CHANGED;
 			}
 
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/netmap/if_igb_netmap.h
--- a/head/sys/dev/netmap/if_igb_netmap.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/netmap/if_igb_netmap.h	Fri Jan 13 15:51:01 2012 +0200
@@ -24,7 +24,7 @@
  */
 
 /*
- * $FreeBSD: head/sys/dev/netmap/if_igb_netmap.h 228881 2011-12-25 21:00:56Z luigi $
+ * $FreeBSD: head/sys/dev/netmap/if_igb_netmap.h 229939 2012-01-10 19:57:23Z luigi $
  * $Id: if_igb_netmap.h 9802 2011-12-02 18:42:37Z luigi $
  *
  * netmap modifications for igb
@@ -181,7 +181,8 @@
 			struct igb_tx_buffer *txbuf = &txr->tx_buffers[l];
 			union e1000_adv_tx_desc *curr =
 			    (union e1000_adv_tx_desc *)&txr->tx_base[l];
-			void *addr = NMB(slot);
+			uint64_t paddr;
+			void *addr = PNMB(slot, &paddr);
 			int flags = ((slot->flags & NS_REPORT) ||
 				j == 0 || j == report_frequency) ?
 					E1000_ADVTXD_DCMD_RS : 0;
@@ -195,7 +196,7 @@
 
 			slot->flags &= ~NS_REPORT;
 			// XXX do we need to set the address ?
-			curr->read.buffer_addr = htole64(vtophys(addr));
+			curr->read.buffer_addr = htole64(paddr);
 			curr->read.olinfo_status =
 			    htole32(olinfo_status |
 				(len<< E1000_ADVTXD_PAYLEN_SHIFT));
@@ -206,8 +207,7 @@
 				    E1000_ADVTXD_DCMD_EOP | flags);
 			if (slot->flags & NS_BUF_CHANGED) {
 				/* buffer has changed, reload map */
-				netmap_reload_map(txr->txtag, txbuf->map,
-					addr, na->buff_size);
+				netmap_reload_map(txr->txtag, txbuf->map, addr);
 				slot->flags &= ~NS_BUF_CHANGED;
 			}
 
@@ -317,7 +317,8 @@
 			struct netmap_slot *slot = ring->slot + j;
 			union e1000_adv_rx_desc *curr = &rxr->rx_base[l];
 			struct igb_rx_buf *rxbuf = rxr->rx_buffers + l;
-			void *addr = NMB(slot);
+			uint64_t paddr;
+			void *addr = PNMB(slot, &paddr);
 
 			if (addr == netmap_buffer_base) { /* bad buf */
 				if (do_lock)
@@ -326,10 +327,9 @@
 			}
 
 			curr->wb.upper.status_error = 0;
-			curr->read.pkt_addr = htole64(vtophys(addr));
+			curr->read.pkt_addr = htole64(paddr);
 			if (slot->flags & NS_BUF_CHANGED) {
-				netmap_reload_map(rxr->ptag, rxbuf->pmap,
-					addr, na->buff_size);
+				netmap_reload_map(rxr->ptag, rxbuf->pmap, addr);
 				slot->flags &= ~NS_BUF_CHANGED;
 			}
 
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/netmap/if_lem_netmap.h
--- a/head/sys/dev/netmap/if_lem_netmap.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/netmap/if_lem_netmap.h	Fri Jan 13 15:51:01 2012 +0200
@@ -24,7 +24,7 @@
  */
 
 /*
- * $FreeBSD: head/sys/dev/netmap/if_lem_netmap.h 228276 2011-12-05 12:06:53Z luigi $
+ * $FreeBSD: head/sys/dev/netmap/if_lem_netmap.h 229939 2012-01-10 19:57:23Z luigi $
  * $Id: if_lem_netmap.h 9802 2011-12-02 18:42:37Z luigi $
  *
  * netmap support for if_lem.c
@@ -186,7 +186,8 @@
 			struct netmap_slot *slot = &ring->slot[j];
 			struct e1000_tx_desc *curr = &adapter->tx_desc_base[l];
 			struct em_buffer *txbuf = &adapter->tx_buffer_area[l];
-			void *addr = NMB(slot);
+			uint64_t paddr;
+			void *addr = PNMB(slot, &paddr);
 			int flags = ((slot->flags & NS_REPORT) ||
 				j == 0 || j == report_frequency) ?
 					E1000_TXD_CMD_RS : 0;
@@ -204,10 +205,9 @@
 			    htole32( adapter->txd_cmd | len |
 				(E1000_TXD_CMD_EOP | flags) );
 			if (slot->flags & NS_BUF_CHANGED) {
-				curr->buffer_addr = htole64(vtophys(addr));
+				curr->buffer_addr = htole64(paddr);
 				/* buffer has changed, reload map */
-				netmap_reload_map(adapter->txtag, txbuf->map,
-				    addr, na->buff_size);
+				netmap_reload_map(adapter->txtag, txbuf->map, addr);
 				slot->flags &= ~NS_BUF_CHANGED;
 			}
 
@@ -314,7 +314,8 @@
 			struct netmap_slot *slot = &ring->slot[j];
 			struct e1000_rx_desc *curr = &adapter->rx_desc_base[l];
 			struct em_buffer *rxbuf = &adapter->rx_buffer_area[l];
-			void *addr = NMB(slot);
+			uint64_t paddr;
+			void *addr = PNMB(slot, &paddr);
 
 			if (addr == netmap_buffer_base) { /* bad buf */
 				if (do_lock)
@@ -323,10 +324,9 @@
 			}
 			curr->status = 0;
 			if (slot->flags & NS_BUF_CHANGED) {
-				curr->buffer_addr = htole64(vtophys(addr));
+				curr->buffer_addr = htole64(paddr);
 				/* buffer has changed, and reload map */
-				netmap_reload_map(adapter->rxtag, rxbuf->map,
-				    addr, na->buff_size);
+				netmap_reload_map(adapter->rxtag, rxbuf->map, addr);
 				slot->flags &= ~NS_BUF_CHANGED;
 			}
 
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/netmap/if_re_netmap.h
--- a/head/sys/dev/netmap/if_re_netmap.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/netmap/if_re_netmap.h	Fri Jan 13 15:51:01 2012 +0200
@@ -24,7 +24,7 @@
  */
 
 /*
- * $FreeBSD: head/sys/dev/netmap/if_re_netmap.h 228276 2011-12-05 12:06:53Z luigi $
+ * $FreeBSD: head/sys/dev/netmap/if_re_netmap.h 229939 2012-01-10 19:57:23Z luigi $
  * $Id: if_re_netmap.h 9802 2011-12-02 18:42:37Z luigi $
  *
  * netmap support for if_re
@@ -183,7 +183,8 @@
 			struct rl_desc *desc = &sc->rl_ldata.rl_tx_list[l];
 			int cmd = slot->len | RL_TDESC_CMD_EOF |
 				RL_TDESC_CMD_OWN | RL_TDESC_CMD_SOF ;
-			void *addr = NMB(slot);
+			uint64_t paddr;
+			void *addr = PNMB(slot, &paddr);
 			int len = slot->len;
 
 			if (addr == netmap_buffer_base || len > NETMAP_BUF_SIZE) {
@@ -197,12 +198,11 @@
 				cmd |= RL_TDESC_CMD_EOR;
 
 			if (slot->flags & NS_BUF_CHANGED) {
-				uint64_t paddr = vtophys(addr);
 				desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(paddr));
 				desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(paddr));
 				/* buffer has changed, unload and reload map */
 				netmap_reload_map(sc->rl_ldata.rl_tx_mtag,
-					txd[l].tx_dmamap, addr, na->buff_size);
+					txd[l].tx_dmamap, addr);
 				slot->flags &= ~NS_BUF_CHANGED;
 			}
 			slot->flags &= ~NS_REPORT;
@@ -304,7 +304,8 @@
 			struct netmap_slot *slot = ring->slot + j;
 			struct rl_desc *desc = &sc->rl_ldata.rl_rx_list[l];
 			int cmd = na->buff_size | RL_RDESC_CMD_OWN;
-			void *addr = NMB(slot);
+			uint64_t paddr;
+			void *addr = PNMB(slot, &paddr);
 
 			if (addr == netmap_buffer_base) { /* bad buf */
 				if (do_lock)
@@ -318,11 +319,10 @@
 			desc->rl_cmdstat = htole32(cmd);
 			slot->flags &= ~NS_REPORT;
 			if (slot->flags & NS_BUF_CHANGED) {
-				uint64_t paddr = vtophys(addr);
 				desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(paddr));
 				desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(paddr));
 				netmap_reload_map(sc->rl_ldata.rl_rx_mtag,
-					rxd[l].rx_dmamap, addr, na->buff_size);
+					rxd[l].rx_dmamap, addr);
 				slot->flags &= ~NS_BUF_CHANGED;
 			}
 			bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag,
@@ -377,12 +377,11 @@
 		if (l >= n)
 			l -= n;
 
-		addr = NMB(slot + l);
-		paddr = vtophys(addr);
+		addr = PNMB(slot + l, &paddr);
 		desc[i].rl_bufaddr_lo = htole32(RL_ADDR_LO(paddr));
 		desc[i].rl_bufaddr_hi = htole32(RL_ADDR_HI(paddr));
 		netmap_load_map(sc->rl_ldata.rl_tx_mtag,
-			txd[i].tx_dmamap, addr, na->buff_size);
+			txd[i].tx_dmamap, addr);
 	}
 }
 
@@ -407,8 +406,12 @@
 		if (l >= n)
 			l -= n;
 
-		addr = NMB(slot + l);
-		paddr = vtophys(addr);
+		addr = PNMB(slot + l, &paddr);
+
+		netmap_reload_map(sc->rl_ldata.rl_rx_mtag,
+			sc->rl_ldata.rl_rx_desc[i].rx_dmamap, addr);
+		bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag,
+		    sc->rl_ldata.rl_rx_desc[i].rx_dmamap, BUS_DMASYNC_PREREAD);
 		desc[i].rl_bufaddr_lo = htole32(RL_ADDR_LO(paddr));
 		desc[i].rl_bufaddr_hi = htole32(RL_ADDR_HI(paddr));
 		cmdstat = na->buff_size;
@@ -422,9 +425,5 @@
 		if (i < n - 1 - kring->nr_hwavail) // XXX + 1 ?
 			cmdstat |= RL_RDESC_CMD_OWN;
 		desc[i].rl_cmdstat = htole32(cmdstat);
-
-		netmap_reload_map(sc->rl_ldata.rl_rx_mtag,
-			sc->rl_ldata.rl_rx_desc[i].rx_dmamap,
-			addr, na->buff_size);
 	}
 }
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/netmap/ixgbe_netmap.h
--- a/head/sys/dev/netmap/ixgbe_netmap.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/netmap/ixgbe_netmap.h	Fri Jan 13 15:51:01 2012 +0200
@@ -24,7 +24,7 @@
  */
 
 /*
- * $FreeBSD: head/sys/dev/netmap/ixgbe_netmap.h 228844 2011-12-23 16:02:14Z luigi $
+ * $FreeBSD: head/sys/dev/netmap/ixgbe_netmap.h 229939 2012-01-10 19:57:23Z luigi $
  * $Id: ixgbe_netmap.h 9802 2011-12-02 18:42:37Z luigi $
  *
  * netmap modifications for ixgbe
@@ -253,7 +253,7 @@
 			 * Note that txbuf and curr are indexed by l.
 			 *
 			 * In this driver we collect the buffer address
-			 * (using the NMB() macro) because we always
+			 * (using the PNMB() macro) because we always
 			 * need to rewrite it into the NIC ring.
 			 * Many other drivers preserve the address, so
 			 * we only need to access it if NS_BUF_CHANGED
@@ -262,7 +262,8 @@
 			struct netmap_slot *slot = &ring->slot[j];
 			struct ixgbe_tx_buf *txbuf = &txr->tx_buffers[l];
 			union ixgbe_adv_tx_desc *curr = &txr->tx_base[l];
-			void *addr = NMB(slot);
+			uint64_t paddr;
+			void *addr = PNMB(slot, &paddr);
 			// XXX type for flags and len ?
 			int flags = ((slot->flags & NS_REPORT) ||
 				j == 0 || j == report_frequency) ?
@@ -290,7 +291,7 @@
 			 * address in the NIC ring. Other drivers do not
 			 * need this.
 			 */
-			curr->read.buffer_addr = htole64(vtophys(addr));
+			curr->read.buffer_addr = htole64(paddr);
 			curr->read.olinfo_status = 0;
 			curr->read.cmd_type_len =
 			    htole32(txr->txd_cmd | len |
@@ -303,8 +304,7 @@
 			 */
 			if (slot->flags & NS_BUF_CHANGED) {
 				/* buffer has changed, unload and reload map */
-				netmap_reload_map(txr->txtag, txbuf->map,
-					addr, na->buff_size);
+				netmap_reload_map(txr->txtag, txbuf->map, addr);
 				slot->flags &= ~NS_BUF_CHANGED;
 			}
 
@@ -479,16 +479,16 @@
 			struct netmap_slot *slot = &ring->slot[j];
 			union ixgbe_adv_rx_desc *curr = &rxr->rx_base[l];
 			struct ixgbe_rx_buf *rxbuf = &rxr->rx_buffers[l];
-			void *addr = NMB(slot);
+			uint64_t paddr;
+			void *addr = PNMB(slot, &paddr);
 
 			if (addr == netmap_buffer_base) /* bad buf */
 				goto ring_reset;
 
 			curr->wb.upper.status_error = 0;
-			curr->read.pkt_addr = htole64(vtophys(addr));
+			curr->read.pkt_addr = htole64(paddr);
 			if (slot->flags & NS_BUF_CHANGED) {
-				netmap_reload_map(rxr->ptag, rxbuf->pmap,
-				    addr, na->buff_size);
+				netmap_reload_map(rxr->ptag, rxbuf->pmap, addr);
 				slot->flags &= ~NS_BUF_CHANGED;
 			}
 
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/netmap/netmap.c
--- a/head/sys/dev/netmap/netmap.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/netmap/netmap.c	Fri Jan 13 15:51:01 2012 +0200
@@ -24,7 +24,7 @@
  */
 
 /*
- * $FreeBSD: head/sys/dev/netmap/netmap.c 228845 2011-12-23 16:03:57Z luigi $
+ * $FreeBSD: head/sys/dev/netmap/netmap.c 229947 2012-01-10 23:02:01Z luigi $
  * $Id: netmap.c 9795 2011-12-02 11:39:08Z luigi $
  *
  * This module supports memory mapped access to network devices,
@@ -56,7 +56,7 @@
  */
 
 #include <sys/cdefs.h> /* prerequisite */
-__FBSDID("$FreeBSD: head/sys/dev/netmap/netmap.c 228845 2011-12-23 16:03:57Z luigi $");
+__FBSDID("$FreeBSD: head/sys/dev/netmap/netmap.c 229947 2012-01-10 23:02:01Z luigi $");
 
 #include <sys/types.h>
 #include <sys/module.h>
@@ -104,6 +104,13 @@
 static void * netmap_malloc(size_t size, const char *msg);
 static void netmap_free(void *addr, const char *msg);
 
+#define netmap_if_malloc(len)   netmap_malloc(len, "nifp")
+#define netmap_if_free(v)	netmap_free((v), "nifp")
+
+#define netmap_ring_malloc(len) netmap_malloc(len, "ring")
+#define netmap_free_rings(na)		\
+	netmap_free((na)->tx_rings[0].ring, "shadow rings");
+
 /*
  * Allocator for a pool of packet buffers. For each buffer we have
  * one entry in the bitmap to signal the state. Allocation scans
@@ -123,7 +130,7 @@
 struct netmap_buf_pool nm_buf_pool;
 /* XXX move these two vars back into netmap_buf_pool */
 u_int netmap_total_buffers;
-char *netmap_buffer_base;
+char *netmap_buffer_base;	/* address of an invalid buffer */
 
 /* user-controlled variables */
 int netmap_verbose;
@@ -145,8 +152,10 @@
  * Buffer 0 is the 'junk' buffer.
  */
 static void
-netmap_new_bufs(struct netmap_buf_pool *p, struct netmap_slot *slot, u_int n)
+netmap_new_bufs(struct netmap_if *nifp __unused,
+		struct netmap_slot *slot, u_int n)
 {
+	struct netmap_buf_pool *p = &nm_buf_pool;
 	uint32_t bi = 0;		/* index in the bitmap */
 	uint32_t mask, j, i = 0;	/* slot counter */
 
@@ -175,8 +184,10 @@
 
 
 static void
-netmap_free_buf(struct netmap_buf_pool *p, uint32_t i)
+netmap_free_buf(struct netmap_if *nifp __unused, uint32_t i)
 {
+	struct netmap_buf_pool *p = &nm_buf_pool;
+
 	uint32_t pos, mask;
 	if (i >= p->total_buffers) {
 		D("invalid free index %d", i);
@@ -233,6 +244,12 @@
 	uint16_t	np_txpoll;
 };
 
+/* Shorthand to compute a netmap interface offset. */
+#define netmap_if_offset(v)                                     \
+    ((char *) (v) - (char *) netmap_mem_d->nm_buffer)
+/* .. and get a physical address given a memory offset */
+#define netmap_ofstophys(o)                                     \
+    (vtophys(netmap_mem_d->nm_buffer) + (o))
 
 static struct cdev *netmap_dev; /* /dev/netmap character device. */
 static struct netmap_mem_d *netmap_mem_d; /* Our memory allocator. */
@@ -386,21 +403,19 @@
 			ring = na->tx_rings[i].ring;
 			lim = na->tx_rings[i].nkr_num_slots;
 			for (j = 0; j < lim; j++)
-				netmap_free_buf(&nm_buf_pool,
-					ring->slot[j].buf_idx);
+				netmap_free_buf(nifp, ring->slot[j].buf_idx);
 
 			ND("rx queue %d", i);
 			ring = na->rx_rings[i].ring;
 			lim = na->rx_rings[i].nkr_num_slots;
 			for (j = 0; j < lim; j++)
-				netmap_free_buf(&nm_buf_pool,
-					ring->slot[j].buf_idx);
+				netmap_free_buf(nifp, ring->slot[j].buf_idx);
 		}
 		NMA_UNLOCK();
-		netmap_free(na->tx_rings[0].ring, "shadow rings");
+		netmap_free_rings(na);
 		wakeup(na);
 	}
-	netmap_free(nifp, "nifp");
+	netmap_if_free(nifp);
 
 	na->nm_lock(ifp->if_softc, NETMAP_CORE_UNLOCK, 0); 
 
@@ -432,7 +447,7 @@
 	 * to the tx and rx rings in the shared memory region.
 	 */
 	len = sizeof(struct netmap_if) + 2 * n * sizeof(ssize_t);
-	nifp = netmap_malloc(len, "nifp");
+	nifp = netmap_if_malloc(len);
 	if (nifp == NULL)
 		return (NULL);
 
@@ -455,13 +470,13 @@
 	len = n * (2 * sizeof(struct netmap_ring) +
 		  (na->num_tx_desc + na->num_rx_desc) *
 		   sizeof(struct netmap_slot) );
-	buff = netmap_malloc(len, "shadow rings");
+	buff = netmap_ring_malloc(len);
 	if (buff == NULL) {
 		D("failed to allocate %d bytes for %s shadow ring",
 			len, ifname);
 error:
 		(na->refcount)--;
-		netmap_free(nifp, "nifp, rings failed");
+		netmap_if_free(nifp);
 		return (NULL);
 	}
 	/* do we have the bufers ? we are in need of num_tx_desc buffers for
@@ -503,7 +518,7 @@
 		 */
 		ring->avail = kring->nr_hwavail = numdesc - 1;
 		ring->cur = kring->nr_hwcur = 0;
-		netmap_new_bufs(&nm_buf_pool, ring->slot, numdesc);
+		netmap_new_bufs(nifp, ring->slot, numdesc);
 
 		ofs += sizeof(struct netmap_ring) +
 			numdesc * sizeof(struct netmap_slot);
@@ -522,7 +537,7 @@
 			kring->nkr_num_slots = numdesc;
 		ring->cur = kring->nr_hwcur = 0;
 		ring->avail = kring->nr_hwavail = 0; /* empty */
-		netmap_new_bufs(&nm_buf_pool, ring->slot, numdesc);
+		netmap_new_bufs(nifp, ring->slot, numdesc);
 		ofs += sizeof(struct netmap_ring) +
 			numdesc * sizeof(struct netmap_slot);
 	}
@@ -567,8 +582,9 @@
 {
 	if (nprot & PROT_EXEC)
 		return (-1);	// XXX -1 or EINVAL ?
+
 	ND("request for offset 0x%x", (uint32_t)offset);
-	*paddr = vtophys(netmap_mem_d->nm_buffer) + offset;
+	*paddr = netmap_ofstophys(offset);
 
 	return (0);
 }
@@ -907,11 +923,12 @@
 				/*
 				 * do something similar to netmap_dtor().
 				 */
-				netmap_free(na->tx_rings[0].ring, "rings, reg.failed");
-				free(na->tx_rings, M_DEVBUF);
+				netmap_free_rings(na);
+				// XXX tx_rings is inline, must not be freed.
+				// free(na->tx_rings, M_DEVBUF); // XXX wrong ?
 				na->tx_rings = na->rx_rings = NULL;
 				na->refcount--;
-				netmap_free(nifp, "nifp, rings failed");
+				netmap_if_free(nifp);
 				nifp = NULL;
 			}
 		}
@@ -939,8 +956,7 @@
 		nmr->nr_numrings = na->num_queues;
 		nmr->nr_numslots = na->num_tx_desc;
 		nmr->nr_memsize = netmap_mem_d->nm_totalsize;
-		nmr->nr_offset =
-			((char *) nifp - (char *) netmap_mem_d->nm_buffer);
+		nmr->nr_offset = netmap_if_offset(nifp);
 		break;
 
 	case NIOCUNREGIF:
@@ -1388,22 +1404,18 @@
  * XXX buflen is probably not needed, buffers have constant size.
  */
 void
-netmap_reload_map(bus_dma_tag_t tag, bus_dmamap_t map,
-	void *buf, bus_size_t buflen)
+netmap_reload_map(bus_dma_tag_t tag, bus_dmamap_t map, void *buf)
 {
-	bus_addr_t paddr;
 	bus_dmamap_unload(tag, map);
-	bus_dmamap_load(tag, map, buf, buflen, ns_dmamap_cb, &paddr,
-				BUS_DMA_NOWAIT);
+	bus_dmamap_load(tag, map, buf, NETMAP_BUF_SIZE, ns_dmamap_cb,
+		NULL, BUS_DMA_NOWAIT);
 }
 
 void
-netmap_load_map(bus_dma_tag_t tag, bus_dmamap_t map,
-	void *buf, bus_size_t buflen)
+netmap_load_map(bus_dma_tag_t tag, bus_dmamap_t map, void *buf)
 {
-	bus_addr_t paddr;
-	bus_dmamap_load(tag, map, buf, buflen, ns_dmamap_cb, &paddr,
-				BUS_DMA_NOWAIT);
+	bus_dmamap_load(tag, map, buf, NETMAP_BUF_SIZE, ns_dmamap_cb,
+		NULL, BUS_DMA_NOWAIT);
 }
 
 /*------ netmap memory allocator -------*/
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/netmap/netmap_kern.h
--- a/head/sys/dev/netmap/netmap_kern.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/netmap/netmap_kern.h	Fri Jan 13 15:51:01 2012 +0200
@@ -24,7 +24,7 @@
  */
 
 /*
- * $FreeBSD: head/sys/dev/netmap/netmap_kern.h 228845 2011-12-23 16:03:57Z luigi $
+ * $FreeBSD: head/sys/dev/netmap/netmap_kern.h 229939 2012-01-10 19:57:23Z luigi $
  * $Id: netmap_kern.h 9795 2011-12-02 11:39:08Z luigi $
  *
  * The header contains the definitions of constants and function
@@ -169,10 +169,8 @@
 enum txrx { NR_RX = 0, NR_TX = 1 };
 struct netmap_slot *netmap_reset(struct netmap_adapter *na,
 	enum txrx tx, int n, u_int new_cur);
-void netmap_load_map(bus_dma_tag_t tag, bus_dmamap_t map,
-        void *buf, bus_size_t buflen);
-void netmap_reload_map(bus_dma_tag_t tag, bus_dmamap_t map,
-        void *buf, bus_size_t buflen);
+void netmap_load_map(bus_dma_tag_t tag, bus_dmamap_t map, void *buf);
+void netmap_reload_map(bus_dma_tag_t tag, bus_dmamap_t map, void *buf);
 int netmap_ring_reinit(struct netmap_kring *);
 
 /*
@@ -206,11 +204,11 @@
 
 
 /*
- * return the address of a buffer.
+ * NMB return the virtual address of a buffer (buffer 0 on bad index)
+ * PNMB also fills the physical address
  * XXX this is a special version with hardwired 2k bufs
- * On error return netmap_buffer_base which is detected as a bad pointer.
  */
-static inline char *
+static inline void *
 NMB(struct netmap_slot *slot)
 {
 	uint32_t i = slot->buf_idx;
@@ -222,4 +220,18 @@
 #endif
 }
 
+static inline void *
+PNMB(struct netmap_slot *slot, uint64_t *pp)
+{
+	uint32_t i = slot->buf_idx;
+	void *ret = (i >= netmap_total_buffers) ? netmap_buffer_base :
+#if NETMAP_BUF_SIZE == 2048
+		netmap_buffer_base + (i << 11);
+#else
+		netmap_buffer_base + (i *NETMAP_BUF_SIZE);
+#endif
+	*pp = vtophys(ret);
+	return ret;
+}
+
 #endif /* _NET_NETMAP_KERN_H_ */
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/nfe/if_nfe.c
--- a/head/sys/dev/nfe/if_nfe.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/nfe/if_nfe.c	Fri Jan 13 15:51:01 2012 +0200
@@ -21,7 +21,7 @@
 /* Driver for NVIDIA nForce MCP Fast Ethernet and Gigabit Ethernet */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/dev/nfe/if_nfe.c 227843 2011-11-22 21:28:20Z marius $");
+__FBSDID("$FreeBSD: head/sys/dev/nfe/if_nfe.c 229767 2012-01-07 09:41:57Z kevlo $");
 
 #ifdef HAVE_KERNEL_OPTION_HEADERS
 #include "opt_device_polling.h"
@@ -565,7 +565,6 @@
 
 	ifp->if_softc = sc;
 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
-	ifp->if_mtu = ETHERMTU;
 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
 	ifp->if_ioctl = nfe_ioctl;
 	ifp->if_start = nfe_start;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/null/null.c
--- a/head/sys/dev/null/null.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/null/null.c	Fri Jan 13 15:51:01 2012 +0200
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/dev/null/null.c 221855 2011-05-13 19:35:01Z mdf $");
+__FBSDID("$FreeBSD: head/sys/dev/null/null.c 229965 2012-01-11 15:00:16Z gnn $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -39,6 +39,7 @@
 #include <sys/priv.h>
 #include <sys/disk.h>
 #include <sys/bus.h>
+#include <sys/filio.h>
 
 #include <machine/bus.h>
 #include <machine/vmparam.h>
@@ -49,6 +50,7 @@
 
 static d_write_t null_write;
 static d_ioctl_t null_ioctl;
+static d_ioctl_t zero_ioctl;
 static d_read_t zero_read;
 
 static struct cdevsw null_cdevsw = {
@@ -63,6 +65,7 @@
 	.d_version =	D_VERSION,
 	.d_read =	zero_read,
 	.d_write =	null_write,
+	.d_ioctl =	zero_ioctl,
 	.d_name =	"zero",
 	.d_flags =	D_MMAP_ANON,
 };
@@ -82,17 +85,50 @@
     int flags __unused, struct thread *td)
 {
 	int error;
+	error = 0;
 
-	if (cmd != DIOCSKERNELDUMP)
-		return (ENOIOCTL);
-	error = priv_check(td, PRIV_SETDUMPER);
-	if (error)
-		return (error);
-	return (set_dumper(NULL));
+	switch (cmd) {
+	case DIOCSKERNELDUMP:
+		error = priv_check(td, PRIV_SETDUMPER);
+		if (error == 0)
+			error = set_dumper(NULL);
+		break;
+	case FIONBIO:
+		break;
+	case FIOASYNC:
+		if (*(int *)data != 0)
+			error = EINVAL;
+		break;
+	default:
+		error = ENOIOCTL;
+	}
+	return (error);
 }
 
 /* ARGSUSED */
 static int
+zero_ioctl(struct cdev *dev __unused, u_long cmd, caddr_t data __unused,
+	   int flags __unused, struct thread *td)
+{
+	int error;
+	error = 0;
+
+	switch (cmd) {
+	case FIONBIO:
+		break;
+	case FIOASYNC:
+		if (*(int *)data != 0)
+			error = EINVAL;
+		break;
+	default:
+		error = ENOIOCTL;
+	}
+	return (error);
+}
+
+
+/* ARGSUSED */
+static int
 zero_read(struct cdev *dev __unused, struct uio *uio, int flags __unused)
 {
 	void *zbuf;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/nve/if_nve.c
--- a/head/sys/dev/nve/if_nve.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/nve/if_nve.c	Fri Jan 13 15:51:01 2012 +0200
@@ -72,7 +72,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/dev/nve/if_nve.c 227843 2011-11-22 21:28:20Z marius $");
+__FBSDID("$FreeBSD: head/sys/dev/nve/if_nve.c 229767 2012-01-07 09:41:57Z kevlo $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -529,7 +529,6 @@
 	ifp->if_ioctl = nve_ioctl;
 	ifp->if_start = nve_ifstart;
 	ifp->if_init = nve_init;
-	ifp->if_mtu = ETHERMTU;
 	ifp->if_baudrate = IF_Mbps(100);
 	IFQ_SET_MAXLEN(&ifp->if_snd, TX_RING_SIZE - 1);
 	ifp->if_snd.ifq_drv_maxlen = TX_RING_SIZE - 1;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/pst/pst-pci.c
--- a/head/sys/dev/pst/pst-pci.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/pst/pst-pci.c	Fri Jan 13 15:51:01 2012 +0200
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
+__FBSDID("$FreeBSD: head/sys/dev/pst/pst-pci.c 229984 2012-01-11 21:38:05Z truckman $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -95,7 +95,7 @@
     sc->ibase = rman_get_virtual(sc->r_mem);
     sc->reg = (struct i2o_registers *)sc->ibase;
     sc->dev = dev;
-    mtx_init(&sc->mtx, "pst lock", MTX_DEF, 0);
+    mtx_init(&sc->mtx, "pst lock", NULL, MTX_DEF);
 
     if (!iop_init(sc))
 	return 0;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/qlxgb/qla_os.c
--- a/head/sys/dev/qlxgb/qla_os.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/qlxgb/qla_os.c	Fri Jan 13 15:51:01 2012 +0200
@@ -31,7 +31,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/dev/qlxgb/qla_os.c 227064 2011-11-03 21:20:22Z bz $");
+__FBSDID("$FreeBSD: head/sys/dev/qlxgb/qla_os.c 229767 2012-01-07 09:41:57Z kevlo $");
 
 #include "qla_os.h"
 #include "qla_reg.h"
@@ -660,7 +660,6 @@
 
 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
 
-	ifp->if_mtu = ETHERMTU;
 	ifp->if_baudrate = (1 * 1000 * 1000 *1000);
 	ifp->if_init = qla_init;
 	ifp->if_softc = ha;
@@ -760,7 +759,7 @@
 	int mcnt = 0;
 	struct ifnet *ifp = ha->ifp;
 
-	IF_ADDR_LOCK(ifp);
+	if_maddr_rlock(ifp);
 
 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
 
@@ -776,7 +775,7 @@
 		mcnt++;
 	}
 
-	IF_ADDR_UNLOCK(ifp);
+	if_maddr_runlock(ifp);
 
 	qla_hw_set_multi(ha, mta, mcnt, add_multi);
 
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/random/probe.c
--- a/head/sys/dev/random/probe.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/random/probe.c	Fri Jan 13 15:51:01 2012 +0200
@@ -26,7 +26,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
+__FBSDID("$FreeBSD: head/sys/dev/random/probe.c 229887 2012-01-09 23:20:30Z jkim $");
 
 #include <sys/types.h>
 #include <sys/param.h>
@@ -36,7 +36,7 @@
 #include <sys/stdint.h>
 #include <sys/sysctl.h>
 
-#if defined(__i386__) && !defined(PC98)
+#if defined(__amd64__) || (defined(__i386__) && !defined(PC98))
 #include <machine/cpufunc.h>
 #include <machine/cputypes.h>
 #include <machine/md_var.h>
@@ -55,7 +55,7 @@
 	*systat = random_yarrow;
 
 	/* Then go looking for hardware */
-#if defined(__i386__) && !defined(PC98)
+#if defined(__amd64__) || (defined(__i386__) && !defined(PC98))
 	if (via_feature_rng & VIA_HAS_RNG) {
 		*systat = random_nehemiah;
 	}
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/rt/if_rt.c
--- a/head/sys/dev/rt/if_rt.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/rt/if_rt.c	Fri Jan 13 15:51:01 2012 +0200
@@ -29,7 +29,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/dev/rt/if_rt.c 227843 2011-11-22 21:28:20Z marius $");
+__FBSDID("$FreeBSD: head/sys/dev/rt/if_rt.c 229767 2012-01-07 09:41:57Z kevlo $");
 
 #include "if_rtvar.h"
 #include "if_rtreg.h"
@@ -383,7 +383,6 @@
 	ifp->if_init = rt_init;
 	ifp->if_ioctl = rt_ioctl;
 	ifp->if_start = rt_start;
-	ifp->if_mtu = ETHERMTU;
 #define	RT_TX_QLEN	256
 
 	IFQ_SET_MAXLEN(&ifp->if_snd, RT_TX_QLEN);
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/sfxge/sfxge_port.c
--- a/head/sys/dev/sfxge/sfxge_port.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/sfxge/sfxge_port.c	Fri Jan 13 15:51:01 2012 +0200
@@ -28,7 +28,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/dev/sfxge/sfxge_port.c 227699 2011-11-19 09:13:58Z philip $");
+__FBSDID("$FreeBSD: head/sys/dev/sfxge/sfxge_port.c 229613 2012-01-05 18:32:37Z jhb $");
 
 #include <sys/types.h>
 #include <sys/limits.h>
@@ -301,7 +301,7 @@
 		 * 0xff. */
 		bucket[0xff] = 1;
 
-		IF_ADDR_LOCK(ifp);
+		if_maddr_rlock(ifp);
 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
 			if (ifma->ifma_addr->sa_family == AF_LINK) {
 				sa = (struct sockaddr_dl *)ifma->ifma_addr;
@@ -309,7 +309,7 @@
 				bucket[index] = 1;
 			}
 		}
-		IF_ADDR_UNLOCK(ifp);
+		if_maddr_runlock(ifp);
 	}
 	return efx_mac_hash_set(enp, bucket);
 }
@@ -391,10 +391,10 @@
 		goto fail2;
 
 	/* Set the unicast address */
-	IF_ADDR_LOCK(ifp);
+	if_addr_rlock(ifp);
 	bcopy(LLADDR((struct sockaddr_dl *)ifp->if_addr->ifa_addr),
 	      mac_addr, sizeof(mac_addr));
-	IF_ADDR_UNLOCK(ifp);
+	if_addr_runlock(ifp);
 	if ((rc = efx_mac_addr_set(enp, mac_addr)) != 0)
 		goto fail;
 
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/sis/if_sis.c
--- a/head/sys/dev/sis/if_sis.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/sis/if_sis.c	Fri Jan 13 15:51:01 2012 +0200
@@ -32,7 +32,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/dev/sis/if_sis.c 227843 2011-11-22 21:28:20Z marius $");
+__FBSDID("$FreeBSD: head/sys/dev/sis/if_sis.c 229767 2012-01-07 09:41:57Z kevlo $");
 
 /*
  * SiS 900/SiS 7016 fast ethernet PCI NIC driver. Datasheets are
@@ -1054,7 +1054,6 @@
 	}
 	ifp->if_softc = sc;
 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
-	ifp->if_mtu = ETHERMTU;
 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
 	ifp->if_ioctl = sis_ioctl;
 	ifp->if_start = sis_start;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/sk/if_sk.c
--- a/head/sys/dev/sk/if_sk.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/sk/if_sk.c	Fri Jan 13 15:51:01 2012 +0200
@@ -48,7 +48,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/dev/sk/if_sk.c 227843 2011-11-22 21:28:20Z marius $");
+__FBSDID("$FreeBSD: head/sys/dev/sk/if_sk.c 229767 2012-01-07 09:41:57Z kevlo $");
 
 /*
  * SysKonnect SK-NET gigabit ethernet driver for FreeBSD. Supports
@@ -140,7 +140,7 @@
 
 #ifndef lint
 static const char rcsid[] =
-  "$FreeBSD: head/sys/dev/sk/if_sk.c 227843 2011-11-22 21:28:20Z marius $";
+  "$FreeBSD: head/sys/dev/sk/if_sk.c 229767 2012-01-07 09:41:57Z kevlo $";
 #endif
 
 static struct sk_type sk_devs[] = {
@@ -1350,7 +1350,6 @@
 	}
 	ifp->if_softc = sc_if;
 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
-	ifp->if_mtu = ETHERMTU;
 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
 	/*
 	 * SK_GENESIS has a bug in checksum offload - From linux.
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/sn/if_sn.c
--- a/head/sys/dev/sn/if_sn.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/sn/if_sn.c	Fri Jan 13 15:51:01 2012 +0200
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
+__FBSDID("$FreeBSD: head/sys/dev/sn/if_sn.c 229767 2012-01-07 09:41:57Z kevlo $");
 
 /*
  * This is a driver for SMC's 9000 series of Ethernet adapters.
@@ -201,7 +201,6 @@
 	}
 	ifp->if_softc = sc;
 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
-	ifp->if_mtu = ETHERMTU;
 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
 	ifp->if_start = snstart;
 	ifp->if_ioctl = snioctl;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/snc/dp83932.c
--- a/head/sys/dev/snc/dp83932.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/snc/dp83932.c	Fri Jan 13 15:51:01 2012 +0200
@@ -1,4 +1,4 @@
-/*	$FreeBSD$	*/
+/*	$FreeBSD: head/sys/dev/snc/dp83932.c 229767 2012-01-07 09:41:57Z kevlo $	*/
 /*	$NecBSD: dp83932.c,v 1.5 1999/07/29 05:08:44 kmatsuda Exp $	*/
 /*	$NetBSD: if_snc.c,v 1.18 1998/04/25 21:27:40 scottr Exp $	*/
 
@@ -176,7 +176,6 @@
 	ifp->if_start = sncstart;
 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
         ifp->if_init = sncinit;
-        ifp->if_mtu = ETHERMTU;
 	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
 
 	/* Initialize media goo. */
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/sound/pci/emu10k1.c
--- a/head/sys/dev/sound/pci/emu10k1.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/sound/pci/emu10k1.c	Fri Jan 13 15:51:01 2012 +0200
@@ -41,7 +41,7 @@
 #include <dev/sound/midi/mpu401.h>
 #include "mpufoi_if.h"
 
-SND_DECLARE_FILE("$FreeBSD: head/sys/dev/sound/pci/emu10k1.c 229430 2012-01-03 21:04:54Z pfg $");
+SND_DECLARE_FILE("$FreeBSD: head/sys/dev/sound/pci/emu10k1.c 229980 2012-01-11 21:03:54Z pfg $");
 
 /* -------------------------------------------------------------------- */
 
@@ -1079,9 +1079,9 @@
 		emu_wrptr(sc, 0, ch->sizereg, sz);
 		if (ch->num == 0) {
 			if (sc->audigy) {
-				val = EMU_ADCCR_LCHANENABLE;
+				val = EMU_A_ADCCR_LCHANENABLE;
 				if (AFMT_CHANNEL(ch->fmt) > 1)
-					val |= EMU_ADCCR_RCHANENABLE;
+					val |= EMU_A_ADCCR_RCHANENABLE;
 				val |= audigy_recval(ch->spd);
 			} else {
 				val = EMU_ADCCR_LCHANENABLE;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/sound/pci/emu10kx-midi.c
--- a/head/sys/dev/sound/pci/emu10kx-midi.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/sound/pci/emu10kx-midi.c	Fri Jan 13 15:51:01 2012 +0200
@@ -25,7 +25,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $FreeBSD$
+ * $FreeBSD: head/sys/dev/sound/pci/emu10kx-midi.c 229981 2012-01-11 21:17:14Z pfg $
  */
 
 #include <sys/param.h>
@@ -50,8 +50,8 @@
 #include <dev/sound/midi/mpu401.h>
 #include "mpufoi_if.h"
 
+#include <dev/sound/pci/emuxkireg.h>
 #include <dev/sound/pci/emu10kx.h>
-#include "emu10k1-alsa%diked.h"
 
 struct emu_midi_softc {
 	struct mtx	mtx;
@@ -176,25 +176,25 @@
 	if (scp->is_emu10k1) {
 		/* SB Live! - only one MIDI device here */
 		inte_val = 0;
-		/* inte_val |= INTE_MIDITXENABLE;*/
-		inte_val |= INTE_MIDIRXENABLE;
-		ipr_val = IPR_MIDITRANSBUFEMPTY;
-		ipr_val |= IPR_MIDIRECVBUFEMPTY;
+		/* inte_val |= EMU_INTE_MIDITXENABLE;*/
+		inte_val |= EMU_INTE_MIDIRXENABLE;
+		ipr_val = EMU_IPR_MIDITRANSBUFE;
+		ipr_val |= EMU_IPR_MIDIRECVBUFE;
 	} else {
-		if (scp->port == A_MUDATA1) {
+		if (scp->port == EMU_A_MUDATA1) {
 			/* EXTERNAL MIDI (AudigyDrive) */
 			inte_val = 0;
-			/* inte_val |= A_INTE_MIDITXENABLE1;*/
-			inte_val |= INTE_MIDIRXENABLE;
-			ipr_val = IPR_MIDITRANSBUFEMPTY;
-			ipr_val |= IPR_MIDIRECVBUFEMPTY;
+			/* inte_val |= A_EMU_INTE_MIDITXENABLE1;*/
+			inte_val |= EMU_INTE_MIDIRXENABLE;
+			ipr_val = EMU_IPR_MIDITRANSBUFE;
+			ipr_val |= EMU_IPR_MIDIRECVBUFE;
 		} else {
 			/* MIDI hw config port 2 */
 			inte_val = 0;
-			/* inte_val |= A_INTE_MIDITXENABLE2;*/
-			inte_val |= INTE_A_MIDIRXENABLE2;
-			ipr_val = IPR_A_MIDITRANSBUFEMPTY2;
-			ipr_val |= IPR_A_MIDIRECVBUFEMPTY2;
+			/* inte_val |= A_EMU_INTE_MIDITXENABLE2;*/
+			inte_val |= EMU_INTE_A_MIDIRXENABLE2;
+			ipr_val = EMU_IPR_A_MIDITRANSBUFE2;
+			ipr_val |= EMU_IPR_A_MIDIRECBUFE2;
 		}
 	}
 
@@ -214,7 +214,7 @@
 	if (scp->is_emu10k1)
 		emu_enable_ir(scp->card);
 	else {
-		if (scp->port == A_MUDATA1)
+		if (scp->port == EMU_A_MUDATA1)
 			emu_enable_ir(scp->card);
 	}
 
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/sound/pci/emu10kx-pcm.c
--- a/head/sys/dev/sound/pci/emu10kx-pcm.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/sound/pci/emu10kx-pcm.c	Fri Jan 13 15:51:01 2012 +0200
@@ -24,7 +24,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $FreeBSD$
+ * $FreeBSD: head/sys/dev/sound/pci/emu10kx-pcm.c 229981 2012-01-11 21:17:14Z pfg $
  */
 
 #include <sys/param.h>
@@ -49,8 +49,8 @@
 
 #include "mixer_if.h"
 
+#include <dev/sound/pci/emuxkireg.h>
 #include <dev/sound/pci/emu10kx.h>
-#include "emu10k1-alsa%diked.h"
 
 struct emu_pcm_pchinfo {
 	int		spd;
@@ -555,8 +555,8 @@
 		break;
 	}
 
-	emu_wr(sc->card, AC97ADDRESS, regno, 1);
-	tmp = emu_rd(sc->card, AC97DATA, 2);
+	emu_wr(sc->card, EMU_AC97ADDR, regno, 1);
+	tmp = emu_rd(sc->card, EMU_AC97DATA, 2);
 
 	if (use_ac97)
 		emulated = tmp;
@@ -621,8 +621,8 @@
 		break;
 	}
 	if (write_ac97) {
-		emu_wr(sc->card, AC97ADDRESS, regno, 1);
-		emu_wr(sc->card, AC97DATA, data, 2);
+		emu_wr(sc->card, EMU_AC97ADDR, regno, 1);
+		emu_wr(sc->card, EMU_AC97DATA, data, 2);
 	}
 }
 
@@ -658,8 +658,8 @@
 	struct emu_pcm_info *sc = (struct emu_pcm_info *)devinfo;
 
 	KASSERT(sc->card != NULL, ("emu_rdcd: no soundcard"));
-	emu_wr(sc->card, AC97ADDRESS, regno, 1);
-	rd = emu_rd(sc->card, AC97DATA, 2);
+	emu_wr(sc->card, EMU_AC97ADDR, regno, 1);
+	rd = emu_rd(sc->card, EMU_AC97DATA, 2);
 	return (rd);
 }
 
@@ -669,8 +669,8 @@
 	struct emu_pcm_info *sc = (struct emu_pcm_info *)devinfo;
 
 	KASSERT(sc->card != NULL, ("emu_wrcd: no soundcard"));
-	emu_wr(sc->card, AC97ADDRESS, regno, 1);
-	emu_wr(sc->card, AC97DATA, data, 2);
+	emu_wr(sc->card, EMU_AC97ADDR, regno, 1);
+	emu_wr(sc->card, EMU_AC97DATA, data, 2);
 	return (0);
 }
 
@@ -870,12 +870,12 @@
 	ch->blksz = sc->bufsz / 2; /* We rise interrupt for half-full buffer */
 	ch->fmt = SND_FORMAT(AFMT_U8, 1, 0);
 	ch->spd = 8000;
-	ch->idxreg = sc->is_emu10k1 ? ADCIDX : A_ADCIDX;
-	ch->basereg = ADCBA;
-	ch->sizereg = ADCBS;
-	ch->setupreg = ADCCR;
-	ch->irqmask = INTE_ADCBUFENABLE;
-	ch->iprmask = IPR_ADCBUFFULL | IPR_ADCBUFHALFFULL;
+	ch->idxreg = sc->is_emu10k1 ? EMU_ADCIDX : EMU_A_ADCIDX;
+	ch->basereg = EMU_ADCBA;
+	ch->sizereg = EMU_ADCBS;
+	ch->setupreg = EMU_ADCCR;
+	ch->irqmask = EMU_INTE_ADCBUFENABLE;
+	ch->iprmask = EMU_IPR_ADCBUFFULL | EMU_IPR_ADCBUFHALFFULL;
 
 	if (sndbuf_alloc(ch->buffer, emu_gettag(sc->card), 0, sc->bufsz) != 0)
 		return (NULL);
@@ -953,22 +953,22 @@
 
 	switch (sc->bufsz) {
 	case 4096:
-		sz = ADCBS_BUFSIZE_4096;
+		sz = EMU_RECBS_BUFSIZE_4096;
 		break;
 	case 8192:
-		sz = ADCBS_BUFSIZE_8192;
+		sz = EMU_RECBS_BUFSIZE_8192;
 		break;
 	case 16384:
-		sz = ADCBS_BUFSIZE_16384;
+		sz = EMU_RECBS_BUFSIZE_16384;
 		break;
 	case 32768:
-		sz = ADCBS_BUFSIZE_32768;
+		sz = EMU_RECBS_BUFSIZE_32768;
 		break;
 	case 65536:
-		sz = ADCBS_BUFSIZE_65536;
+		sz = EMU_RECBS_BUFSIZE_65536;
 		break;
 	default:
-		sz = ADCBS_BUFSIZE_4096;
+		sz = EMU_RECBS_BUFSIZE_4096;
 	}
 
 	snd_mtxlock(sc->lock);
@@ -976,9 +976,9 @@
 	case PCMTRIG_START:
 		ch->run = 1;
 		emu_wrptr(sc->card, 0, ch->sizereg, sz);
-		val = sc->is_emu10k1 ? ADCCR_LCHANENABLE : A_ADCCR_LCHANENABLE;
+		val = sc->is_emu10k1 ? EMU_ADCCR_LCHANENABLE : EMU_A_ADCCR_LCHANENABLE;
 		if (AFMT_CHANNEL(ch->fmt) > 1)
-			val |= sc->is_emu10k1 ? ADCCR_RCHANENABLE : A_ADCCR_RCHANENABLE;
+			val |= sc->is_emu10k1 ? EMU_ADCCR_RCHANENABLE : EMU_A_ADCCR_RCHANENABLE;
 		val |= sc->is_emu10k1 ? emu_k1_recval(ch->spd) : emu_k2_recval(ch->spd);
 		emu_wrptr(sc->card, 0, ch->setupreg, 0);
 		emu_wrptr(sc->card, 0, ch->setupreg, val);
@@ -1049,11 +1049,11 @@
 	ch = &(sc->rch_efx);
 	ch->fmt = SND_FORMAT(AFMT_S16_LE, 1, 0);
 	ch->spd = sc->is_emu10k1 ? 48000*32 : 48000 * 64;
-	ch->idxreg = FXIDX;
-	ch->basereg = FXBA;
-	ch->sizereg = FXBS;
-	ch->irqmask = INTE_EFXBUFENABLE;
-	ch->iprmask = IPR_EFXBUFFULL | IPR_EFXBUFHALFFULL;
+	ch->idxreg = EMU_FXIDX;
+	ch->basereg = EMU_FXBA;
+	ch->sizereg = EMU_FXBS;
+	ch->irqmask = EMU_INTE_EFXBUFENABLE;
+	ch->iprmask = EMU_IPR_EFXBUFFULL | EMU_IPR_EFXBUFHALFFULL;
 	ch->buffer = b;
 	ch->pcm = sc;
 	ch->channel = c;
@@ -1113,22 +1113,22 @@
 
 	switch (sc->bufsz) {
 	case 4096:
-		sz = ADCBS_BUFSIZE_4096;
+		sz = EMU_RECBS_BUFSIZE_4096;
 		break;
 	case 8192:
-		sz = ADCBS_BUFSIZE_8192;
+		sz = EMU_RECBS_BUFSIZE_8192;
 		break;
 	case 16384:
-		sz = ADCBS_BUFSIZE_16384;
+		sz = EMU_RECBS_BUFSIZE_16384;
 		break;
 	case 32768:
-		sz = ADCBS_BUFSIZE_32768;
+		sz = EMU_RECBS_BUFSIZE_32768;
 		break;
 	case 65536:
-		sz = ADCBS_BUFSIZE_65536;
+		sz = EMU_RECBS_BUFSIZE_65536;
 		break;
 	default:
-		sz = ADCBS_BUFSIZE_4096;
+		sz = EMU_RECBS_BUFSIZE_4096;
 	}
 
 	snd_mtxlock(sc->lock);
@@ -1140,14 +1140,14 @@
 		/*
 		 * SB Live! is limited to 32 mono channels. Audigy
 		 * has 64 mono channels. Channels are enabled
-		 * by setting a bit in A_FXWC[1|2] registers.
+		 * by setting a bit in EMU_A_FXWC[1|2] registers.
 		 */
 		/* XXX there is no way to demultiplex this streams for now */
 		if (sc->is_emu10k1) {
-			emu_wrptr(sc->card, 0, FXWC, 0xffffffff);
+			emu_wrptr(sc->card, 0, EMU_FXWC, 0xffffffff);
 		} else {
-			emu_wrptr(sc->card, 0, A_FXWC1, 0xffffffff);
-			emu_wrptr(sc->card, 0, A_FXWC2, 0xffffffff);
+			emu_wrptr(sc->card, 0, EMU_A_FXWC1, 0xffffffff);
+			emu_wrptr(sc->card, 0, EMU_A_FXWC2, 0xffffffff);
 		}
 		break;
 	case PCMTRIG_STOP:
@@ -1155,10 +1155,10 @@
 	case PCMTRIG_ABORT:
 		ch->run = 0;
 		if (sc->is_emu10k1) {
-			emu_wrptr(sc->card, 0, FXWC, 0x0);
+			emu_wrptr(sc->card, 0, EMU_FXWC, 0x0);
 		} else {
-			emu_wrptr(sc->card, 0, A_FXWC1, 0x0);
-			emu_wrptr(sc->card, 0, A_FXWC2, 0x0);
+			emu_wrptr(sc->card, 0, EMU_A_FXWC1, 0x0);
+			emu_wrptr(sc->card, 0, EMU_A_FXWC2, 0x0);
 		}
 		emu_wrptr(sc->card, 0, ch->sizereg, 0);
 		(void)emu_intr_unregister(sc->card, ch->ihandle);
@@ -1238,8 +1238,8 @@
 
 	snd_mtxlock(sc->lock);
 	
-	if (stat & IPR_INTERVALTIMER) {
-		ack |= IPR_INTERVALTIMER;
+	if (stat & EMU_IPR_INTERVALTIMER) {
+		ack |= EMU_IPR_INTERVALTIMER;
 		for (i = 0; i < MAX_CHANNELS; i++)
 			if (sc->pch[i].channel) {
 				if (sc->pch[i].run == 1) {
@@ -1262,8 +1262,8 @@
 	}
 
 
-	if (stat & (IPR_ADCBUFFULL | IPR_ADCBUFHALFFULL)) {
-		ack |= stat & (IPR_ADCBUFFULL | IPR_ADCBUFHALFFULL);
+	if (stat & (EMU_IPR_ADCBUFFULL | EMU_IPR_ADCBUFHALFFULL)) {
+		ack |= stat & (EMU_IPR_ADCBUFFULL | EMU_IPR_ADCBUFHALFFULL);
 		if (sc->rch_adc.channel) {
 			snd_mtxunlock(sc->lock);
 			chn_intr(sc->rch_adc.channel);
@@ -1271,8 +1271,8 @@
 		}
 	}
 
-	if (stat & (IPR_EFXBUFFULL | IPR_EFXBUFHALFFULL)) {
-		ack |= stat & (IPR_EFXBUFFULL | IPR_EFXBUFHALFFULL);
+	if (stat & (EMU_IPR_EFXBUFFULL | EMU_IPR_EFXBUFHALFFULL)) {
+		ack |= stat & (EMU_IPR_EFXBUFFULL | EMU_IPR_EFXBUFHALFFULL);
 		if (sc->rch_efx.channel) {
 			snd_mtxunlock(sc->lock);
 			chn_intr(sc->rch_efx.channel);
@@ -1450,8 +1450,8 @@
 		goto bad;
 	}
 
-	inte = INTE_INTERVALTIMERENB;
-	ipr = IPR_INTERVALTIMER; /* Used by playback & ADC */
+	inte = EMU_INTE_INTERTIMERENB;
+	ipr = EMU_IPR_INTERVALTIMER; /* Used by playback & ADC */
 	sc->ihandle = emu_intr_register(sc->card, inte, ipr, &emu_pcm_intr, sc);
 
 	if (emu_pcm_init(sc) == -1) {
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/sound/pci/emu10kx.c
--- a/head/sys/dev/sound/pci/emu10kx.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/sound/pci/emu10kx.c	Fri Jan 13 15:51:01 2012 +0200
@@ -24,7 +24,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $FreeBSD$
+ * $FreeBSD: head/sys/dev/sound/pci/emu10kx.c 229981 2012-01-11 21:17:14Z pfg $
  */
 
 #include <sys/param.h>
@@ -53,6 +53,7 @@
 #include <dev/sound/pcm/sound.h>
 #include <dev/sound/pcm/ac97.h>
 
+#include <dev/sound/pci/emuxkireg.h>
 #include <dev/sound/pci/emu10kx.h>
 
 /* hw flags */
@@ -181,7 +182,7 @@
 #define	A_IN_AUX2_R	0x0d
 #define	A_IN_AUX2	A_IN_AUX2_L
 
-/* Audigiy Outputs */
+/* Audigy Outputs */
 #define	A_OUT_D_FRONT_L	0x00
 #define	A_OUT_D_FRONT_R	0x01
 #define	A_OUT_D_FRONT	A_OUT_D_FRONT_L
@@ -217,9 +218,19 @@
 #define	A_OUT_ADC_REC_R	0x17
 #define	A_OUT_ADC_REC	A_OUT_ADC_REC_L
 
-#include "emu10k1-alsa%diked.h"
-#include "p16v-alsa%diked.h"
-#include "p17v-alsa%diked.h"
+#define EMU_DATA2	0x24
+#define EMU_IPR2	0x28
+#define EMU_INTE2	0x2c
+#define EMU_IPR3	0x38
+#define EMU_INTE3	0x3c
+
+#define EMU_A2_SRCSel		0x60
+#define EMU_A2_SRCMULTI_ENABLE	0x6e
+
+#define EMU_A_I2S_CAPTURE_96000	0x00000400
+
+#define EMU_A2_MIXER_I2S_ENABLE           0x7B
+#define EMU_A2_MIXER_SPDIF_ENABLE         0x7A
 
 #define	C_FRONT_L	0
 #define	C_FRONT_R	1
@@ -630,7 +641,7 @@
 	}
 }
 /*
- * PTR / DATA interface. Access to EMU10Kx is made
+ * EMU_PTR / EMU_DATA interface. Access to EMU10Kx is made
  * via (channel, register) pair. Some registers are channel-specific,
  * some not.
  */
@@ -639,11 +650,11 @@
 {
 	uint32_t ptr, val, mask, size, offset;
 
-	ptr = ((reg << 16) & sc->address_mask) | (chn & PTR_CHANNELNUM_MASK);
+	ptr = ((reg << 16) & sc->address_mask) | (chn & EMU_PTR_CHNO_MASK);
 
 	EMU_RWLOCK();
-	emu_wr_nolock(sc, PTR, ptr, 4);
-	val = emu_rd_nolock(sc, DATA, 4);
+	emu_wr_nolock(sc, EMU_PTR, ptr, 4);
+	val = emu_rd_nolock(sc, EMU_DATA, 4);
 	EMU_RWUNLOCK();
 
 	/*
@@ -666,10 +677,10 @@
 {
 	uint32_t ptr, mask, size, offset;
 
-	ptr = ((reg << 16) & sc->address_mask) | (chn & PTR_CHANNELNUM_MASK);
+	ptr = ((reg << 16) & sc->address_mask) | (chn & EMU_PTR_CHNO_MASK);
 
 	EMU_RWLOCK();
-	emu_wr_nolock(sc, PTR, ptr, 4);
+	emu_wr_nolock(sc, EMU_PTR, ptr, 4);
 	/*
 	 * XXX Another kind of magic encoding in register number. This can
 	 * give you side effect - it will read previous data from register
@@ -681,13 +692,13 @@
 		mask = ((1 << size) - 1) << offset;
 		data <<= offset;
 		data &= mask;
-		data |= emu_rd_nolock(sc, DATA, 4) & ~mask;
+		data |= emu_rd_nolock(sc, EMU_DATA, 4) & ~mask;
 	}
-	emu_wr_nolock(sc, DATA, data, 4);
+	emu_wr_nolock(sc, EMU_DATA, data, 4);
 	EMU_RWUNLOCK();
 }
 /*
- * PTR2 / DATA2 interface. Access to P16v is made
+ * EMU_A2_PTR / EMU_DATA2 interface. Access to P16v is made
  * via (channel, register) pair. Some registers are channel-specific,
  * some not. This interface is supported by CA0102 and CA0108 chips only.
  */
@@ -698,8 +709,8 @@
 
 	/* XXX separate lock? */
 	EMU_RWLOCK();
-	emu_wr_nolock(sc, PTR2, (reg << 16) | chn, 4);
-	val = emu_rd_nolock(sc, DATA2, 4);
+	emu_wr_nolock(sc, EMU_A2_PTR, (reg << 16) | chn, 4);
+	val = emu_rd_nolock(sc, EMU_DATA2, 4);
 
 	EMU_RWUNLOCK();
 
@@ -711,8 +722,8 @@
 {
 
 	EMU_RWLOCK();
-	emu_wr_nolock(sc, PTR2, (reg << 16) | chn, 4);
-	emu_wr_nolock(sc, DATA2, data, 4);
+	emu_wr_nolock(sc, EMU_A2_PTR, (reg << 16) | chn, 4);
+	emu_wr_nolock(sc, EMU_DATA2, data, 4);
 	EMU_RWUNLOCK();
 }
 /*
@@ -737,13 +748,13 @@
 
 /*
  * Direct hardware register access
- * Assume that it is never used to access PTR-based registers and can run unlocked.
+ * Assume that it is never used to access EMU_PTR-based registers and can run unlocked.
  */
 void
 emu_wr(struct emu_sc_info *sc, unsigned int regno, uint32_t data, unsigned int size)
 {
-	KASSERT(regno != PTR, ("emu_wr: attempt to write to PTR"));
-	KASSERT(regno != PTR2, ("emu_wr: attempt to write to PTR2"));
+	KASSERT(regno != EMU_PTR, ("emu_wr: attempt to write to EMU_PTR"));
+	KASSERT(regno != EMU_A2_PTR, ("emu_wr: attempt to write to EMU_A2_PTR"));
 
 	emu_wr_nolock(sc, regno, data, size);
 }
@@ -753,8 +764,8 @@
 {
 	uint32_t rd;
 
-	KASSERT(regno != DATA, ("emu_rd: attempt to read DATA"));
-	KASSERT(regno != DATA2, ("emu_rd: attempt to read DATA2"));
+	KASSERT(regno != EMU_DATA, ("emu_rd: attempt to read DATA"));
+	KASSERT(regno != EMU_DATA2, ("emu_rd: attempt to read DATA2"));
 
 	rd = emu_rd_nolock(sc, regno, size);
 	return (rd);
@@ -770,24 +781,24 @@
 	uint32_t iocfg;
 
 	if (sc->is_emu10k2 || sc->is_ca0102) {
-		iocfg = emu_rd_nolock(sc, A_IOCFG, 2);
-		emu_wr_nolock(sc, A_IOCFG, iocfg | A_IOCFG_GPOUT2, 2);
+		iocfg = emu_rd_nolock(sc, EMU_A_IOCFG, 2);
+		emu_wr_nolock(sc, EMU_A_IOCFG, iocfg | EMU_A_IOCFG_GPOUT2, 2);
 		DELAY(500);
-		emu_wr_nolock(sc, A_IOCFG, iocfg | A_IOCFG_GPOUT1 | A_IOCFG_GPOUT2, 2);
+		emu_wr_nolock(sc, EMU_A_IOCFG, iocfg | EMU_A_IOCFG_GPOUT1 | EMU_A_IOCFG_GPOUT2, 2);
 		DELAY(500);
-		emu_wr_nolock(sc, A_IOCFG, iocfg | A_IOCFG_GPOUT1, 2);
+		emu_wr_nolock(sc, EMU_A_IOCFG, iocfg | EMU_A_IOCFG_GPOUT1, 2);
 		DELAY(100);
-		emu_wr_nolock(sc, A_IOCFG, iocfg, 2);
+		emu_wr_nolock(sc, EMU_A_IOCFG, iocfg, 2);
 		device_printf(sc->dev, "Audigy IR MIDI events enabled.\n");
 		sc->enable_ir = 1;
 	}
 	if (sc->is_emu10k1) {
-		iocfg = emu_rd_nolock(sc, HCFG, 4);
-		emu_wr_nolock(sc, HCFG, iocfg | HCFG_GPOUT2, 4);
+		iocfg = emu_rd_nolock(sc, EMU_HCFG, 4);
+		emu_wr_nolock(sc, EMU_HCFG, iocfg | EMU_HCFG_GPOUT2, 4);
 		DELAY(500);
-		emu_wr_nolock(sc, HCFG, iocfg | HCFG_GPOUT1 | HCFG_GPOUT2, 4);
+		emu_wr_nolock(sc, EMU_HCFG, iocfg | EMU_HCFG_GPOUT1 | EMU_HCFG_GPOUT2, 4);
 		DELAY(100);
-		emu_wr_nolock(sc, HCFG, iocfg, 4);
+		emu_wr_nolock(sc, EMU_HCFG, iocfg, 4);
 		device_printf(sc->dev, "SB Live! IR MIDI events enabled.\n");
 		sc->enable_ir = 1;
 	}
@@ -835,7 +846,7 @@
 			sc->timerinterval = sc->timer[i];
 
 	/* XXX */
-	emu_wr(sc, TIMER, sc->timerinterval & 0x03ff, 2);
+	emu_wr(sc, EMU_TIMER, sc->timerinterval & 0x03ff, 2);
 	mtx_unlock(&sc->lock);
 
 	return (timer);
@@ -868,16 +879,16 @@
 			ena_int = 1;
 	}
 
-	emu_wr(sc, TIMER, sc->timerinterval & 0x03ff, 2);
+	emu_wr(sc, EMU_TIMER, sc->timerinterval & 0x03ff, 2);
 
 	if (ena_int == 1) {
-		x = emu_rd(sc, INTE, 4);
-		x |= INTE_INTERVALTIMERENB;
-		emu_wr(sc, INTE, x, 4);
+		x = emu_rd(sc, EMU_INTE, 4);
+		x |= EMU_INTE_INTERTIMERENB;
+		emu_wr(sc, EMU_INTE, x, 4);
 	} else {
-		x = emu_rd(sc, INTE, 4);
-		x &= ~INTE_INTERVALTIMERENB;
-		emu_wr(sc, INTE, x, 4);
+		x = emu_rd(sc, EMU_INTE, 4);
+		x &= ~EMU_INTE_INTERTIMERENB;
+		emu_wr(sc, EMU_INTE, x, 4);
 	}
 	mtx_unlock(&sc->lock);
 	return (0);
@@ -917,9 +928,9 @@
 			sc->ihandler[i].intr_mask = intr_mask;
 			sc->ihandler[i].softc = isc;
 			sc->ihandler[i].irq_func = func;
-			x = emu_rd(sc, INTE, 4);
+			x = emu_rd(sc, EMU_INTE, 4);
 			x |= inte_mask;
-			emu_wr(sc, INTE, x, 4);
+			emu_wr(sc, EMU_INTE, x, 4);
 			mtx_unlock(&sc->lock);
 			if (sc->dbg_level > 1)
 				device_printf(sc->dev, "ihandle %d registered\n", i);
@@ -946,7 +957,7 @@
 		return (-1);
 	}
 
-	x = emu_rd(sc, INTE, 4);
+	x = emu_rd(sc, EMU_INTE, 4);
 	x &= ~sc->ihandler[hnumber].inte_mask;
 
 	sc->ihandler[hnumber].inte_mask = 0;
@@ -954,12 +965,12 @@
 	sc->ihandler[hnumber].softc = NULL;
 	sc->ihandler[hnumber].irq_func = NULL;
 
-	/* other interrupt handlers may use this INTE value */
+	/* other interrupt handlers may use this EMU_INTE value */
 	for (i = 0; i < EMU_MAX_IRQ_CONSUMERS; i++)
 		if (sc->ihandler[i].inte_mask != 0)
 			x |= sc->ihandler[i].inte_mask;
 
-	emu_wr(sc, INTE, x, 4);
+	emu_wr(sc, EMU_INTE, x, 4);
 
 	mtx_unlock(&sc->lock);
 	return (hnumber);
@@ -973,11 +984,11 @@
 	int i;
 
 	for (;;) {
-		stat = emu_rd(sc, IPR, 4);
+		stat = emu_rd(sc, EMU_IPR, 4);
 		ack = 0;
 		if (stat == 0)
 			break;
-		emu_wr(sc, IPR, stat, 4);
+		emu_wr(sc, EMU_IPR, stat, 4);
 		for (i = 0; i < EMU_MAX_IRQ_CONSUMERS; i++) {
 			if ((((sc->ihandler[i].intr_mask) & stat) != 0) &&
 			    (((void *)sc->ihandler[i].irq_func) != NULL)) {
@@ -993,13 +1004,13 @@
 
 	if ((sc->is_ca0102) || (sc->is_ca0108))
 		for (;;) {
-			stat = emu_rd(sc, IPR2, 4);
+			stat = emu_rd(sc, EMU_IPR2, 4);
 			ack = 0;
 			if (stat == 0)
 				break;
-			emu_wr(sc, IPR2, stat, 4);
+			emu_wr(sc, EMU_IPR2, stat, 4);
 			if (sc->dbg_level > 1)
-				device_printf(sc->dev, "IPR2: %08x\n", stat);
+				device_printf(sc->dev, "EMU_IPR2: %08x\n", stat);
 
 			break;	/* to avoid infinite loop. shoud be removed
 				 * after completion of P16V interface. */
@@ -1007,13 +1018,13 @@
 
 	if (sc->is_ca0102)
 		for (;;) {
-			stat = emu_rd(sc, IPR3, 4);
+			stat = emu_rd(sc, EMU_IPR3, 4);
 			ack = 0;
 			if (stat == 0)
 				break;
-			emu_wr(sc, IPR3, stat, 4);
+			emu_wr(sc, EMU_IPR3, stat, 4);
 			if (sc->dbg_level > 1)
-				device_printf(sc->dev, "IPR3: %08x\n", stat);
+				device_printf(sc->dev, "EMU_IPR3: %08x\n", stat);
 
 			break;	/* to avoid infinite loop. should be removed
 				 * after completion of S/PDIF interface */
@@ -1374,61 +1385,61 @@
 
 
 	if (v->stereo) {
-		emu_wrptr(sc, v->vnum, CPF, CPF_STEREO_MASK);
+		emu_wrptr(sc, v->vnum, EMU_CHAN_CPF, EMU_CHAN_CPF_STEREO_MASK);
 	} else {
-		emu_wrptr(sc, v->vnum, CPF, 0);
+		emu_wrptr(sc, v->vnum, EMU_CHAN_CPF, 0);
 	}
 	val = v->stereo ? 28 : 30;
 	val *= v->b16 ? 1 : 2;
 	start = v->sa + val;
 
 	if (sc->is_emu10k1) {
-		emu_wrptr(sc, v->vnum, FXRT, ((v->routing[3] << 12) |
+		emu_wrptr(sc, v->vnum, EMU_CHAN_FXRT, ((v->routing[3] << 12) |
 		    (v->routing[2] << 8) |
 		    (v->routing[1] << 4) |
 		    (v->routing[0] << 0)) << 16);
 	} else {
-		emu_wrptr(sc, v->vnum, A_FXRT1, (v->routing[3] << 24) |
+		emu_wrptr(sc, v->vnum, EMU_A_CHAN_FXRT1, (v->routing[3] << 24) |
 		    (v->routing[2] << 16) |
 		    (v->routing[1] << 8) |
 		    (v->routing[0] << 0));
-		emu_wrptr(sc, v->vnum, A_FXRT2, (v->routing[7] << 24) |
+		emu_wrptr(sc, v->vnum, EMU_A_CHAN_FXRT2, (v->routing[7] << 24) |
 		    (v->routing[6] << 16) |
 		    (v->routing[5] << 8) |
 		    (v->routing[4] << 0));
-		emu_wrptr(sc, v->vnum, A_SENDAMOUNTS, (v->amounts[7] << 24) |
+		emu_wrptr(sc, v->vnum, EMU_A_CHAN_SENDAMOUNTS, (v->amounts[7] << 24) |
 		    (v->amounts[6] << 26) |
 		    (v->amounts[5] << 8) |
 		    (v->amounts[4] << 0));
 	}
-	emu_wrptr(sc, v->vnum, PTRX, (v->amounts[0] << 8) | (v->amounts[1] << 0));
-	emu_wrptr(sc, v->vnum, DSL, v->ea | (v->amounts[3] << 24));
-	emu_wrptr(sc, v->vnum, PSST, v->sa | (v->amounts[2] << 24));
-
-	emu_wrptr(sc, v->vnum, CCCA, start | (v->b16 ? 0 : CCCA_8BITSELECT));
-	emu_wrptr(sc, v->vnum, Z1, 0);
-	emu_wrptr(sc, v->vnum, Z2, 0);
-
-	silent_page = ((uint32_t) (sc->mem.silent_page_addr) << 1) | MAP_PTI_MASK;
-	emu_wrptr(sc, v->vnum, MAPA, silent_page);
-	emu_wrptr(sc, v->vnum, MAPB, silent_page);
-
-	emu_wrptr(sc, v->vnum, CVCF, CVCF_CURRENTFILTER_MASK);
-	emu_wrptr(sc, v->vnum, VTFT, VTFT_FILTERTARGET_MASK);
-	emu_wrptr(sc, v->vnum, ATKHLDM, 0);
-	emu_wrptr(sc, v->vnum, DCYSUSM, DCYSUSM_DECAYTIME_MASK);
-	emu_wrptr(sc, v->vnum, LFOVAL1, 0x8000);
-	emu_wrptr(sc, v->vnum, LFOVAL2, 0x8000);
-	emu_wrptr(sc, v->vnum, FMMOD, 0);
-	emu_wrptr(sc, v->vnum, TREMFRQ, 0);
-	emu_wrptr(sc, v->vnum, FM2FRQ2, 0);
-	emu_wrptr(sc, v->vnum, ENVVAL, 0x8000);
-
-	emu_wrptr(sc, v->vnum, ATKHLDV, ATKHLDV_HOLDTIME_MASK | ATKHLDV_ATTACKTIME_MASK);
-	emu_wrptr(sc, v->vnum, ENVVOL, 0x8000);
-
-	emu_wrptr(sc, v->vnum, PEFE_FILTERAMOUNT, 0x7f);
-	emu_wrptr(sc, v->vnum, PEFE_PITCHAMOUNT, 0);
+	emu_wrptr(sc, v->vnum, EMU_CHAN_PTRX, (v->amounts[0] << 8) | (v->amounts[1] << 0));
+	emu_wrptr(sc, v->vnum, EMU_CHAN_DSL, v->ea | (v->amounts[3] << 24));
+	emu_wrptr(sc, v->vnum, EMU_CHAN_PSST, v->sa | (v->amounts[2] << 24));
+
+	emu_wrptr(sc, v->vnum, EMU_CHAN_CCCA, start | (v->b16 ? 0 : EMU_CHAN_CCCA_8BITSELECT));
+	emu_wrptr(sc, v->vnum, EMU_CHAN_Z1, 0);
+	emu_wrptr(sc, v->vnum, EMU_CHAN_Z2, 0);
+
+	silent_page = ((uint32_t) (sc->mem.silent_page_addr) << 1) | EMU_CHAN_MAP_PTI_MASK;
+	emu_wrptr(sc, v->vnum, EMU_CHAN_MAPA, silent_page);
+	emu_wrptr(sc, v->vnum, EMU_CHAN_MAPB, silent_page);
+
+	emu_wrptr(sc, v->vnum, EMU_CHAN_CVCF, EMU_CHAN_CVCF_CURRFILTER_MASK);
+	emu_wrptr(sc, v->vnum, EMU_CHAN_VTFT, EMU_CHAN_VTFT_FILTERTARGET_MASK);
+	emu_wrptr(sc, v->vnum, EMU_CHAN_ATKHLDM, 0);
+	emu_wrptr(sc, v->vnum, EMU_CHAN_DCYSUSM, EMU_CHAN_DCYSUSM_DECAYTIME_MASK);
+	emu_wrptr(sc, v->vnum, EMU_CHAN_LFOVAL1, 0x8000);
+	emu_wrptr(sc, v->vnum, EMU_CHAN_LFOVAL2, 0x8000);
+	emu_wrptr(sc, v->vnum, EMU_CHAN_FMMOD, 0);
+	emu_wrptr(sc, v->vnum, EMU_CHAN_TREMFRQ, 0);
+	emu_wrptr(sc, v->vnum, EMU_CHAN_FM2FRQ2, 0);
+	emu_wrptr(sc, v->vnum, EMU_CHAN_ENVVAL, 0x8000);
+
+	emu_wrptr(sc, v->vnum, EMU_CHAN_ATKHLDV, EMU_CHAN_ATKHLDV_HOLDTIME_MASK | EMU_CHAN_ATKHLDV_ATTACKTIME_MASK);
+	emu_wrptr(sc, v->vnum, EMU_CHAN_ENVVOL, 0x8000);
+
+	emu_wrptr(sc, v->vnum, EMU_CHAN_PEFE_FILTERAMOUNT, 0x7f);
+	emu_wrptr(sc, v->vnum, EMU_CHAN_PEFE_PITCHAMOUNT, 0);
 	if ((v->stereo) && (v->slave != NULL))
 		emu_vwrite(sc, v->slave);
 }
@@ -1438,7 +1449,7 @@
 {
 	int reg;
 
-	reg = (channel & 0x20) ? SOLEH : SOLEL;
+	reg = (channel & 0x20) ? EMU_SOLEH : EMU_SOLEL;
 	channel &= 0x1f;
 	reg |= 1 << 24;
 	reg |= channel << 16;
@@ -1459,29 +1470,29 @@
 		ccis *= v->b16 ? 1 : 2;
 		sample = v->b16 ? 0x00000000 : 0x80808080;
 		for (i = 0; i < cs; i++)
-			emu_wrptr(sc, v->vnum, CD0 + i, sample);
-		emu_wrptr(sc, v->vnum, CCR_CACHEINVALIDSIZE, 0);
-		emu_wrptr(sc, v->vnum, CCR_READADDRESS, cra);
-		emu_wrptr(sc, v->vnum, CCR_CACHEINVALIDSIZE, ccis);
-
-		emu_wrptr(sc, v->vnum, IFATN, 0xff00);
-		emu_wrptr(sc, v->vnum, VTFT, 0xffffffff);
-		emu_wrptr(sc, v->vnum, CVCF, 0xffffffff);
-		emu_wrptr(sc, v->vnum, DCYSUSV, 0x00007f7f);
+			emu_wrptr(sc, v->vnum, EMU_CHAN_CD0 + i, sample);
+		emu_wrptr(sc, v->vnum, EMU_CHAN_CCR_CACHEINVALIDSIZE, 0);
+		emu_wrptr(sc, v->vnum, EMU_CHAN_CCR_READADDRESS, cra);
+		emu_wrptr(sc, v->vnum, EMU_CHAN_CCR_CACHEINVALIDSIZE, ccis);
+
+		emu_wrptr(sc, v->vnum, EMU_CHAN_IFATN, 0xff00);
+		emu_wrptr(sc, v->vnum, EMU_CHAN_VTFT, 0xffffffff);
+		emu_wrptr(sc, v->vnum, EMU_CHAN_CVCF, 0xffffffff);
+		emu_wrptr(sc, v->vnum, EMU_CHAN_DCYSUSV, 0x00007f7f);
 		emu_vstop(sc, v->vnum, 0);
 
 		pitch_target = emu_rate_to_linearpitch(v->speed);
 		initial_pitch = emu_rate_to_pitch(v->speed) >> 8;
-		emu_wrptr(sc, v->vnum, PTRX_PITCHTARGET, pitch_target);
-		emu_wrptr(sc, v->vnum, CPF_CURRENTPITCH, pitch_target);
-		emu_wrptr(sc, v->vnum, IP, initial_pitch);
+		emu_wrptr(sc, v->vnum, EMU_CHAN_PTRX_PITCHTARGET, pitch_target);
+		emu_wrptr(sc, v->vnum, EMU_CHAN_CPF_PITCH, pitch_target);
+		emu_wrptr(sc, v->vnum, EMU_CHAN_IP, initial_pitch);
 	} else {
-		emu_wrptr(sc, v->vnum, PTRX_PITCHTARGET, 0);
-		emu_wrptr(sc, v->vnum, CPF_CURRENTPITCH, 0);
-		emu_wrptr(sc, v->vnum, IFATN, 0xffff);
-		emu_wrptr(sc, v->vnum, VTFT, 0x0000ffff);
-		emu_wrptr(sc, v->vnum, CVCF, 0x0000ffff);
-		emu_wrptr(sc, v->vnum, IP, 0);
+		emu_wrptr(sc, v->vnum, EMU_CHAN_PTRX_PITCHTARGET, 0);
+		emu_wrptr(sc, v->vnum, EMU_CHAN_CPF_PITCH, 0);
+		emu_wrptr(sc, v->vnum, EMU_CHAN_IFATN, 0xffff);
+		emu_wrptr(sc, v->vnum, EMU_CHAN_VTFT, 0x0000ffff);
+		emu_wrptr(sc, v->vnum, EMU_CHAN_CVCF, 0x0000ffff);
+		emu_wrptr(sc, v->vnum, EMU_CHAN_IP, 0);
 		emu_vstop(sc, v->vnum, 1);
 	}
 	if ((v->stereo) && (v->slave != NULL))
@@ -1494,7 +1505,7 @@
 	int s, ptr;
 
 	s = (v->b16 ? 1 : 0) + (v->stereo ? 1 : 0);
-	ptr = (emu_rdptr(sc, v->vnum, CCCA_CURRADDR) - (v->start >> s)) << s;
+	ptr = (emu_rdptr(sc, v->vnum, EMU_CHAN_CCCA_CURRADDR) - (v->start >> s)) << s;
 	return (ptr & ~0x0000001f);
 }
 
@@ -1694,9 +1705,9 @@
 
 	/* stop DSP */
 	if (sc->is_emu10k1) {
-		emu_wrptr(sc, 0, DBG, EMU10K1_DBG_SINGLE_STEP);
+		emu_wrptr(sc, 0, EMU_DBG, EMU_DBG_SINGLE_STEP);
 	} else {
-		emu_wrptr(sc, 0, A_DBG, A_DBG_SINGLE_STEP);
+		emu_wrptr(sc, 0, EMU_A_DBG, EMU_A_DBG_SINGLE_STEP);
 	}
 
 	/* code size is in instructions */
@@ -2162,9 +2173,9 @@
 
 	/* start DSP */
 	if (sc->is_emu10k1) {
-		emu_wrptr(sc, 0, DBG, 0);
+		emu_wrptr(sc, 0, EMU_DBG, 0);
 	} else {
-		emu_wrptr(sc, 0, A_DBG, 0);
+		emu_wrptr(sc, 0, EMU_A_DBG, 0);
 	}
 }
 
@@ -2450,24 +2461,24 @@
 		return;
 	}
 
-	hcfg = HCFG_AUDIOENABLE | HCFG_AUTOMUTE;
+	hcfg = EMU_HCFG_AUDIOENABLE | EMU_HCFG_AUTOMUTE;
 	a_iocfg = 0;
 
 	if (sc->rev >= 6)
-		hcfg |= HCFG_JOYENABLE;
+		hcfg |= EMU_HCFG_JOYENABLE;
 
 	if (sc->is_emu10k1)
-		hcfg |= HCFG_LOCKTANKCACHE_MASK;
+		hcfg |= EMU_HCFG_LOCKTANKCACHE_MASK;
 	else
-		hcfg |= HCFG_CODECFORMAT_I2S | HCFG_JOYENABLE;
+		hcfg |= EMU_HCFG_CODECFMT_I2S | EMU_HCFG_JOYENABLE;
 
 
 	if (mode == MODE_DIGITAL) {
 		if (sc->broken_digital) {
 			device_printf(sc->dev, "Digital mode is reported as broken on this card.\n");
 		}
-		a_iocfg |= A_IOCFG_ENABLE_DIGITAL;
-		hcfg |= HCFG_GPOUT0;
+		a_iocfg |= EMU_A_IOCFG_GPOUT1;
+		hcfg |= EMU_HCFG_GPOUT0;
 	}
 
 	if (mode == MODE_ANALOG)
@@ -2478,12 +2489,12 @@
 
 	if ((sc->is_ca0102) || (sc->is_ca0108))
 		/*
-		 * Setting A_IOCFG_DISABLE_ANALOG will do opposite things
+		 * Setting EMU_A_IOCFG_DISABLE_ANALOG will do opposite things
 		 * on diffrerent cards.
 		 * "don't disable analog outs" on Audigy 2 (ca0102/ca0108)
 		 * "disable analog outs" on Audigy (emu10k2)
 		 */
-		a_iocfg |= A_IOCFG_DISABLE_ANALOG;
+		a_iocfg |= EMU_A_IOCFG_DISABLE_ANALOG;
 
 	if (sc->is_ca0108)
 		a_iocfg |= 0x20; /* XXX */
@@ -2492,12 +2503,12 @@
 	if (mode == MODE_DIGITAL)
 		emumix_set_gpr(sc, sc->mute_gpr[ANALOGMUTE], 1);
 
-	emu_wr(sc, HCFG, hcfg, 4);
+	emu_wr(sc, EMU_HCFG, hcfg, 4);
 
 	if ((sc->is_emu10k2) || (sc->is_ca0102) || (sc->is_ca0108)) {
-		tmp = emu_rd(sc, A_IOCFG, 2);
+		tmp = emu_rd(sc, EMU_A_IOCFG, 2);
 		tmp = a_iocfg;
-		emu_wr(sc, A_IOCFG, tmp, 2);
+		emu_wr(sc, EMU_A_IOCFG, tmp, 2);
 	}
 
 	/* Unmute if we have changed mode to analog. */
@@ -2523,16 +2534,16 @@
 		return;
 	}
 
-	spcs = SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
-	    SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
-	    SPCS_GENERATIONSTATUS | 0x00001200 | 0x00000000 |
-	    SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT;
+	spcs = EMU_SPCS_CLKACCY_1000PPM | EMU_SPCS_SAMPLERATE_48 |
+	    EMU_SPCS_CHANNELNUM_LEFT | EMU_SPCS_SOURCENUM_UNSPEC |
+	    EMU_SPCS_GENERATIONSTATUS | 0x00001200 | 0x00000000 |
+	    EMU_SPCS_EMPHASIS_NONE | EMU_SPCS_COPYRIGHT;
 
 	mode = SPDIF_MODE_PCM;
 
-	emu_wrptr(sc, 0, SPCS0, spcs);
-	emu_wrptr(sc, 0, SPCS1, spcs);
-	emu_wrptr(sc, 0, SPCS2, spcs);
+	emu_wrptr(sc, 0, EMU_SPCS0, spcs);
+	emu_wrptr(sc, 0, EMU_SPCS1, spcs);
+	emu_wrptr(sc, 0, EMU_SPCS2, spcs);
 }
 
 #define	L2L_POINTS	10
@@ -2635,8 +2646,8 @@
 {
 
 	/*
-	 * XXX May not need this if we have IPR3 handler.
-	 * Is it a real init calls, or IPR3 interrupt acknowledgments?
+	 * XXX May not need this if we have EMU_IPR3 handler.
+	 * Is it a real init calls, or EMU_IPR3 interrupt acknowledgments?
 	 * Looks much like "(data << 16) | register".
 	 */
 	emu_wr_cbptr(sc, (0x00d0 << 16) | 0x0000);
@@ -2660,42 +2671,42 @@
 	int i;
 
 	/* disable audio and lock cache */
-	emu_wr(sc, HCFG, HCFG_LOCKSOUNDCACHE | HCFG_LOCKTANKCACHE_MASK | HCFG_MUTEBUTTONENABLE, 4);
+	emu_wr(sc, EMU_HCFG, EMU_HCFG_LOCKSOUNDCACHE | EMU_HCFG_LOCKTANKCACHE_MASK | EMU_HCFG_MUTEBUTTONENABLE, 4);
 
 	/* reset recording buffers */
-	emu_wrptr(sc, 0, MICBS, ADCBS_BUFSIZE_NONE);
-	emu_wrptr(sc, 0, MICBA, 0);
-	emu_wrptr(sc, 0, FXBS, ADCBS_BUFSIZE_NONE);
-	emu_wrptr(sc, 0, FXBA, 0);
-	emu_wrptr(sc, 0, ADCBS, ADCBS_BUFSIZE_NONE);
-	emu_wrptr(sc, 0, ADCBA, 0);
+	emu_wrptr(sc, 0, EMU_MICBS, EMU_RECBS_BUFSIZE_NONE);
+	emu_wrptr(sc, 0, EMU_MICBA, 0);
+	emu_wrptr(sc, 0, EMU_FXBS, EMU_RECBS_BUFSIZE_NONE);
+	emu_wrptr(sc, 0, EMU_FXBA, 0);
+	emu_wrptr(sc, 0, EMU_ADCBS, EMU_RECBS_BUFSIZE_NONE);
+	emu_wrptr(sc, 0, EMU_ADCBA, 0);
 
 	/* disable channel interrupt */
-	emu_wr(sc, INTE, INTE_INTERVALTIMERENB | INTE_SAMPLERATETRACKER | INTE_PCIERRORENABLE, 4);
-	emu_wrptr(sc, 0, CLIEL, 0);
-	emu_wrptr(sc, 0, CLIEH, 0);
-	emu_wrptr(sc, 0, SOLEL, 0);
-	emu_wrptr(sc, 0, SOLEH, 0);
+	emu_wr(sc, EMU_INTE, EMU_INTE_INTERTIMERENB | EMU_INTE_SAMPLERATER | EMU_INTE_PCIERRENABLE, 4);
+	emu_wrptr(sc, 0, EMU_CLIEL, 0);
+	emu_wrptr(sc, 0, EMU_CLIEH, 0);
+	emu_wrptr(sc, 0, EMU_SOLEL, 0);
+	emu_wrptr(sc, 0, EMU_SOLEH, 0);
 
 	/* disable P16V and S/PDIF interrupts */
 	if ((sc->is_ca0102) || (sc->is_ca0108))
-		emu_wr(sc, INTE2, 0, 4);
+		emu_wr(sc, EMU_INTE2, 0, 4);
 
 	if (sc->is_ca0102)
-		emu_wr(sc, INTE3, 0, 4);
+		emu_wr(sc, EMU_INTE3, 0, 4);
 
 	/* init phys inputs and outputs */
 	ac97slot = 0;
 	if (sc->has_51)
-		ac97slot = AC97SLOT_CNTR | AC97SLOT_LFE;
+		ac97slot = EMU_AC97SLOT_CENTER | EMU_AC97SLOT_LFE;
 	if (sc->has_71)
-		ac97slot = AC97SLOT_CNTR | AC97SLOT_LFE | AC97SLOT_REAR_LEFT | AC97SLOT_REAR_RIGHT;
+		ac97slot = EMU_AC97SLOT_CENTER | EMU_AC97SLOT_LFE | EMU_AC97SLOT_REAR_LEFT | EMU_AC97SLOT_REAR_RIGHT;
 	if (sc->is_emu10k2)
 		ac97slot |= 0x40;
-	emu_wrptr(sc, 0, AC97SLOT, ac97slot);
+	emu_wrptr(sc, 0, EMU_AC97SLOT, ac97slot);
 
 	if (sc->is_emu10k2)	/* XXX for later cards? */
-		emu_wrptr(sc, 0, SPBYPASS, 0xf00);	/* What will happen if
+		emu_wrptr(sc, 0, EMU_SPBYPASS, 0xf00);	/* What will happen if
 							 * we write 1 here? */
 
 	if (bus_dma_tag_create( /* parent */ bus_get_dma_tag(sc->dev),
@@ -2729,61 +2740,61 @@
 		sc->mem.ptb_pages[i] = tmp | i;
 
 	for (ch = 0; ch < NUM_G; ch++) {
-		emu_wrptr(sc, ch, MAPA, tmp | MAP_PTI_MASK);
-		emu_wrptr(sc, ch, MAPB, tmp | MAP_PTI_MASK);
+		emu_wrptr(sc, ch, EMU_CHAN_MAPA, tmp | EMU_CHAN_MAP_PTI_MASK);
+		emu_wrptr(sc, ch, EMU_CHAN_MAPB, tmp | EMU_CHAN_MAP_PTI_MASK);
 	}
-	emu_wrptr(sc, 0, PTB, (sc->mem.ptb_pages_addr));
-	emu_wrptr(sc, 0, TCB, 0);	/* taken from original driver */
-	emu_wrptr(sc, 0, TCBS, 0);	/* taken from original driver */
+	emu_wrptr(sc, 0, EMU_PTB, (sc->mem.ptb_pages_addr));
+	emu_wrptr(sc, 0, EMU_TCB, 0);	/* taken from original driver */
+	emu_wrptr(sc, 0, EMU_TCBS, 0);	/* taken from original driver */
 
 	/* init envelope engine */
 	for (ch = 0; ch < NUM_G; ch++) {
-		emu_wrptr(sc, ch, DCYSUSV, 0);
-		emu_wrptr(sc, ch, IP, 0);
-		emu_wrptr(sc, ch, VTFT, 0xffff);
-		emu_wrptr(sc, ch, CVCF, 0xffff);
-		emu_wrptr(sc, ch, PTRX, 0);
-		emu_wrptr(sc, ch, CPF, 0);
-		emu_wrptr(sc, ch, CCR, 0);
-
-		emu_wrptr(sc, ch, PSST, 0);
-		emu_wrptr(sc, ch, DSL, 0x10);
-		emu_wrptr(sc, ch, CCCA, 0);
-		emu_wrptr(sc, ch, Z1, 0);
-		emu_wrptr(sc, ch, Z2, 0);
-		emu_wrptr(sc, ch, FXRT, 0xd01c0000);
-
-		emu_wrptr(sc, ch, ATKHLDM, 0);
-		emu_wrptr(sc, ch, DCYSUSM, 0);
-		emu_wrptr(sc, ch, IFATN, 0xffff);
-		emu_wrptr(sc, ch, PEFE, 0);
-		emu_wrptr(sc, ch, FMMOD, 0);
-		emu_wrptr(sc, ch, TREMFRQ, 24);	/* 1 Hz */
-		emu_wrptr(sc, ch, FM2FRQ2, 24);	/* 1 Hz */
-		emu_wrptr(sc, ch, TEMPENV, 0);
+		emu_wrptr(sc, ch, EMU_CHAN_DCYSUSV, 0);
+		emu_wrptr(sc, ch, EMU_CHAN_IP, 0);
+		emu_wrptr(sc, ch, EMU_CHAN_VTFT, 0xffff);
+		emu_wrptr(sc, ch, EMU_CHAN_CVCF, 0xffff);
+		emu_wrptr(sc, ch, EMU_CHAN_PTRX, 0);
+		emu_wrptr(sc, ch, EMU_CHAN_CPF, 0);
+		emu_wrptr(sc, ch, EMU_CHAN_CCR, 0);
+
+		emu_wrptr(sc, ch, EMU_CHAN_PSST, 0);
+		emu_wrptr(sc, ch, EMU_CHAN_DSL, 0x10);
+		emu_wrptr(sc, ch, EMU_CHAN_CCCA, 0);
+		emu_wrptr(sc, ch, EMU_CHAN_Z1, 0);
+		emu_wrptr(sc, ch, EMU_CHAN_Z2, 0);
+		emu_wrptr(sc, ch, EMU_CHAN_FXRT, 0xd01c0000);
+
+		emu_wrptr(sc, ch, EMU_CHAN_ATKHLDM, 0);
+		emu_wrptr(sc, ch, EMU_CHAN_DCYSUSM, 0);
+		emu_wrptr(sc, ch, EMU_CHAN_IFATN, 0xffff);
+		emu_wrptr(sc, ch, EMU_CHAN_PEFE, 0);
+		emu_wrptr(sc, ch, EMU_CHAN_FMMOD, 0);
+		emu_wrptr(sc, ch, EMU_CHAN_TREMFRQ, 24);	/* 1 Hz */
+		emu_wrptr(sc, ch, EMU_CHAN_FM2FRQ2, 24);	/* 1 Hz */
+		emu_wrptr(sc, ch, EMU_CHAN_TEMPENV, 0);
 
 		/*** these are last so OFF prevents writing ***/
-		emu_wrptr(sc, ch, LFOVAL2, 0);
-		emu_wrptr(sc, ch, LFOVAL1, 0);
-		emu_wrptr(sc, ch, ATKHLDV, 0);
-		emu_wrptr(sc, ch, ENVVOL, 0);
-		emu_wrptr(sc, ch, ENVVAL, 0);
+		emu_wrptr(sc, ch, EMU_CHAN_LFOVAL2, 0);
+		emu_wrptr(sc, ch, EMU_CHAN_LFOVAL1, 0);
+		emu_wrptr(sc, ch, EMU_CHAN_ATKHLDV, 0);
+		emu_wrptr(sc, ch, EMU_CHAN_ENVVOL, 0);
+		emu_wrptr(sc, ch, EMU_CHAN_ENVVAL, 0);
 
 		if ((sc->is_emu10k2) || (sc->is_ca0102) || (sc->is_ca0108)) {
 			emu_wrptr(sc, ch, 0x4c, 0x0);
 			emu_wrptr(sc, ch, 0x4d, 0x0);
 			emu_wrptr(sc, ch, 0x4e, 0x0);
 			emu_wrptr(sc, ch, 0x4f, 0x0);
-			emu_wrptr(sc, ch, A_FXRT1, 0x3f3f3f3f);
-			emu_wrptr(sc, ch, A_FXRT2, 0x3f3f3f3f);
-			emu_wrptr(sc, ch, A_SENDAMOUNTS, 0x0);
+			emu_wrptr(sc, ch, EMU_A_CHAN_FXRT1, 0x3f3f3f3f);
+			emu_wrptr(sc, ch, EMU_A_CHAN_FXRT2, 0x3f3f3f3f);
+			emu_wrptr(sc, ch, EMU_A_CHAN_SENDAMOUNTS, 0x0);
 		}
 	}
 
 	emumix_set_spdif_mode(sc, SPDIF_MODE_PCM);
 
 	if ((sc->is_emu10k2) || (sc->is_ca0102) || (sc->is_ca0108))
-		emu_wrptr(sc, 0, A_SPDIF_SAMPLERATE, A_SPDIF_48000);
+		emu_wrptr(sc, 0, EMU_A_SPDIF_SAMPLERATE, EMU_A_SPDIF_48000);
 
 	/*
 	 * CAxxxx cards needs additional setup:
@@ -2793,23 +2804,23 @@
 	 */
 	if ((sc->is_ca0102) || (sc->is_ca0108)) {
 
-		spdif_sr = emu_rdptr(sc, 0, A_SPDIF_SAMPLERATE);
+		spdif_sr = emu_rdptr(sc, 0, EMU_A_SPDIF_SAMPLERATE);
 		spdif_sr &= 0xfffff1ff;
-		spdif_sr |= A_I2S_CAPTURE_96000;
-		emu_wrptr(sc, 0, A_SPDIF_SAMPLERATE, spdif_sr);
+		spdif_sr |= EMU_A_I2S_CAPTURE_96000;
+		emu_wrptr(sc, 0, EMU_A_SPDIF_SAMPLERATE, spdif_sr);
 
 		/* Disable P16v processing */
-		emu_wr_p16vptr(sc, 0, SRCSel, 0x14);
+		emu_wr_p16vptr(sc, 0, EMU_A2_SRCSel, 0x14);
 
 		/* Setup P16v/P17v sound routing */
 		if (sc->is_ca0102)
-			emu_wr_p16vptr(sc, 0, SRCMULTI_ENABLE, 0xFF00FF00);
+			emu_wr_p16vptr(sc, 0, EMU_A2_SRCMULTI_ENABLE, 0xFF00FF00);
 		else {
-			emu_wr_p16vptr(sc, 0, P17V_MIXER_I2S_ENABLE, 0xFF000000);
-			emu_wr_p16vptr(sc, 0, P17V_MIXER_SPDIF_ENABLE, 0xFF000000);
-
-			tmp = emu_rd(sc, A_IOCFG, 2);
-			emu_wr(sc, A_IOCFG, tmp & ~0x8, 2);
+			emu_wr_p16vptr(sc, 0, EMU_A2_MIXER_I2S_ENABLE, 0xFF000000);
+			emu_wr_p16vptr(sc, 0, EMU_A2_MIXER_SPDIF_ENABLE, 0xFF000000);
+
+			tmp = emu_rd(sc, EMU_A_IOCFG, 2);
+			emu_wr(sc, EMU_A_IOCFG, tmp & ~0x8, 2);
 		}
 	}
 	emu_initefx(sc);
@@ -2824,7 +2835,7 @@
 	emumix_set_mode(sc, def_mode);
 
 	if (bootverbose) {
-		tmp = emu_rd(sc, HCFG, 4);
+		tmp = emu_rd(sc, EMU_HCFG, 4);
 		device_printf(sc->dev, "Card Configuration (   0x%08x )\n", tmp);
 		device_printf(sc->dev, "Card Configuration ( & 0xff000000 ) : %s%s%s%s%s%s%s%s\n",
 		    (tmp & 0x80000000 ? "[Legacy MPIC] " : ""),
@@ -2864,7 +2875,7 @@
 		    (tmp & 0x00000001 ? "[AUDIOENABLE]" : " "));
 
 		if ((sc->is_emu10k2) || (sc->is_ca0102) || (sc->is_ca0108)) {
-			tmp = emu_rd(sc, A_IOCFG, 2);
+			tmp = emu_rd(sc, EMU_A_IOCFG, 2);
 			device_printf(sc->dev, "Audigy Card Configuration (    0x%04x )\n", tmp);
 			device_printf(sc->dev, "Audigy Card Configuration (  & 0xff00 )");
 			printf(" : %s%s%s%s%s%s%s%s\n",
@@ -2897,36 +2908,36 @@
 	uint32_t ch;
 	struct emu_memblk *blk;
 
-	emu_wr(sc, INTE, 0, 4);
+	emu_wr(sc, EMU_INTE, 0, 4);
 	for (ch = 0; ch < NUM_G; ch++)
-		emu_wrptr(sc, ch, DCYSUSV, 0);
+		emu_wrptr(sc, ch, EMU_CHAN_DCYSUSV, 0);
 	for (ch = 0; ch < NUM_G; ch++) {
-		emu_wrptr(sc, ch, VTFT, 0);
-		emu_wrptr(sc, ch, CVCF, 0);
-		emu_wrptr(sc, ch, PTRX, 0);
-		emu_wrptr(sc, ch, CPF, 0);
+		emu_wrptr(sc, ch, EMU_CHAN_VTFT, 0);
+		emu_wrptr(sc, ch, EMU_CHAN_CVCF, 0);
+		emu_wrptr(sc, ch, EMU_CHAN_PTRX, 0);
+		emu_wrptr(sc, ch, EMU_CHAN_CPF, 0);
 	}
 
 	/* disable audio and lock cache */
-	emu_wr(sc, HCFG, HCFG_LOCKSOUNDCACHE | HCFG_LOCKTANKCACHE_MASK | HCFG_MUTEBUTTONENABLE, 4);
-
-	emu_wrptr(sc, 0, PTB, 0);
+	emu_wr(sc, EMU_HCFG, EMU_HCFG_LOCKSOUNDCACHE | EMU_HCFG_LOCKTANKCACHE_MASK | EMU_HCFG_MUTEBUTTONENABLE, 4);
+
+	emu_wrptr(sc, 0, EMU_PTB, 0);
 	/* reset recording buffers */
-	emu_wrptr(sc, 0, MICBS, ADCBS_BUFSIZE_NONE);
-	emu_wrptr(sc, 0, MICBA, 0);
-	emu_wrptr(sc, 0, FXBS, ADCBS_BUFSIZE_NONE);
-	emu_wrptr(sc, 0, FXBA, 0);
-	emu_wrptr(sc, 0, FXWC, 0);
-	emu_wrptr(sc, 0, ADCBS, ADCBS_BUFSIZE_NONE);
-	emu_wrptr(sc, 0, ADCBA, 0);
-	emu_wrptr(sc, 0, TCB, 0);
-	emu_wrptr(sc, 0, TCBS, 0);
+	emu_wrptr(sc, 0, EMU_MICBS, EMU_RECBS_BUFSIZE_NONE);
+	emu_wrptr(sc, 0, EMU_MICBA, 0);
+	emu_wrptr(sc, 0, EMU_FXBS, EMU_RECBS_BUFSIZE_NONE);
+	emu_wrptr(sc, 0, EMU_FXBA, 0);
+	emu_wrptr(sc, 0, EMU_FXWC, 0);
+	emu_wrptr(sc, 0, EMU_ADCBS, EMU_RECBS_BUFSIZE_NONE);
+	emu_wrptr(sc, 0, EMU_ADCBA, 0);
+	emu_wrptr(sc, 0, EMU_TCB, 0);
+	emu_wrptr(sc, 0, EMU_TCBS, 0);
 
 	/* disable channel interrupt */
-	emu_wrptr(sc, 0, CLIEL, 0);
-	emu_wrptr(sc, 0, CLIEH, 0);
-	emu_wrptr(sc, 0, SOLEL, 0);
-	emu_wrptr(sc, 0, SOLEH, 0);
+	emu_wrptr(sc, 0, EMU_CLIEL, 0);
+	emu_wrptr(sc, 0, EMU_CLIEH, 0);
+	emu_wrptr(sc, 0, EMU_SOLEL, 0);
+	emu_wrptr(sc, 0, EMU_SOLEH, 0);
 
 	if (!SLIST_EMPTY(&sc->mem.blocks))
 		device_printf(sc->dev, "warning: memblock list not empty\n");
@@ -3137,9 +3148,9 @@
 	/*	0xe0...0x100 are unknown	*/
 	/*	sc->tram_base = 0x200		*/
 	/*	sc->tram_addr_base = 0x300	*/
-		sc->gpr_base = A_FXGPREGBASE;
+		sc->gpr_base = EMU_A_FXGPREGBASE;
 		sc->num_gprs = 0x200;
-		sc->code_base = A_MICROCODEBASE;
+		sc->code_base = EMU_A_MICROCODEBASE;
 		sc->code_size = 0x800 / 2;	/* 0x600-0xdff,  2048 words,
 						 * 1024 instructions */
 
@@ -3147,16 +3158,16 @@
 		sc->num_fxbuses = 16;
 		sc->num_inputs = 8;
 		sc->num_outputs = 16;
-		sc->address_mask = A_PTR_ADDRESS_MASK;
+		sc->address_mask = EMU_A_PTR_ADDR_MASK;
 	}
 	if (sc->is_emu10k1) {
 		sc->has_51 = 0;	/* We don't support 5.1 sound on SB Live! 5.1 */
 		sc->opcode_shift = 20;
 		sc->high_operand_shift = 10;
-		sc->code_base = MICROCODEBASE;
+		sc->code_base = EMU_MICROCODEBASE;
 		sc->code_size = 0x400 / 2;	/* 0x400-0x7ff,  1024 words,
 						 * 512 instructions */
-		sc->gpr_base = FXGPREGBASE;
+		sc->gpr_base = EMU_FXGPREGBASE;
 		sc->num_gprs = 0x100;
 		sc->input_base = 0x10;
 		sc->output_base = 0x20;
@@ -3171,7 +3182,7 @@
 		sc->num_fxbuses = 8;
 		sc->num_inputs = 8;
 		sc->num_outputs = 16;
-		sc->address_mask = PTR_ADDRESS_MASK;
+		sc->address_mask = EMU_PTR_ADDR_MASK;
 	}
 	if (sc->opcode_shift == 0)
 		goto bad;
@@ -3379,7 +3390,7 @@
 		}
 		midiinfo->card = sc;
 		if (sc->is_emu10k2 || (sc->is_ca0102)) {
-			midiinfo->port = A_MUDATA1;
+			midiinfo->port = EMU_A_MUDATA1;
 			midiinfo->portnr = 1;
 		}
 		if (sc->is_emu10k1) {
@@ -3405,7 +3416,7 @@
 		}
 		midiinfo->card = sc;
 
-		midiinfo->port = A_MUDATA2;
+		midiinfo->port = EMU_A_MUDATA2;
 		midiinfo->portnr = 2;
 
 		func->func = SCF_MIDI;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/sound/pci/emu10kx.h
--- a/head/sys/dev/sound/pci/emu10kx.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/sound/pci/emu10kx.h	Fri Jan 13 15:51:01 2012 +0200
@@ -24,7 +24,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $FreeBSD$
+ * $FreeBSD: head/sys/dev/sound/pci/emu10kx.h 229981 2012-01-11 21:17:14Z pfg $
  */
 
 #ifndef EMU10KX_H
@@ -52,6 +52,14 @@
 #define	EMU_VAR_MCH_DISABLED 	3
 #define	EMU_VAR_MCH_REC 	4
 
+#define EMU_A_IOCFG_DISABLE_ANALOG	0x0040	/* = 'enable' for Audigy2 */
+#define EMU_A_IOCFG_GPOUT2	0x0001
+#define EMU_AC97SLOT_REAR_RIGHT	0x01
+#define EMU_AC97SLOT_REAR_LEFT	0x02
+#define EMU_HCFG_GPOUT0		0x00001000
+#define EMU_HCFG_GPOUT1		0x00000800
+#define EMU_HCFG_GPOUT2		0x00000400
+
 #define	RT_FRONT		0
 #define	RT_REAR			1
 #define	RT_CENTER		2
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/sound/pci/emuxkireg.h
--- a/head/sys/dev/sound/pci/emuxkireg.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/sound/pci/emuxkireg.h	Fri Jan 13 15:51:01 2012 +0200
@@ -1,4 +1,4 @@
-/* $FreeBSD: head/sys/dev/sound/pci/emuxkireg.h 229430 2012-01-03 21:04:54Z pfg $ */
+/* $FreeBSD: head/sys/dev/sound/pci/emuxkireg.h 229981 2012-01-11 21:17:14Z pfg $ */
 /*	$NetBSD: emuxkireg.h,v 1.8 2008/04/28 20:23:54 martin Exp $	*/
 
 /*-
@@ -168,8 +168,8 @@
 #define EMU_A_IOCFG			0x18
 #define EMU_A_GPINPUT_MASK		0xff00
 #define EMU_A_GPOUTPUT_MASK		0x00ff
-#define EMU_A_IOCFG_GPOUT0		0x0040
-#define EMU_A_IOCFG_GPOUT1		0x0004
+#define EMU_A_IOCFG_GPOUT0		0x0044
+#define EMU_A_IOCFG_GPOUT1		0x0002
 
 #define EMU_TIMER	0x1a
 #define  EMU_TIMER_RATE_MASK	0x000003ff
@@ -532,8 +532,8 @@
 #define EMU_A_FXWC1		0x74
 #define EMU_A_FXWC2		0x75
 #define EMU_A_SPDIF_SAMPLERATE	0x76
-#define EMU_A_SPDIF_48000	0x00000080
-#define EMU_A_SPDIF_44100	0x00000000
+#define EMU_A_SPDIF_44100	0x00000080
+#define EMU_A_SPDIF_48000	0x00000000
 #define EMU_A_SPDIF_96000	0x00000040
 #define EMU_A2_SPDIF_SAMPLERATE	EMU_MKSUBREG(3, 9, EMU_A_SPDIF_SAMPLERATE)
 #define EMU_A2_SPDIF_MASK	0x00000e00
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/stge/if_stge.c
--- a/head/sys/dev/stge/if_stge.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/stge/if_stge.c	Fri Jan 13 15:51:01 2012 +0200
@@ -35,7 +35,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/dev/stge/if_stge.c 227848 2011-11-22 21:55:40Z marius $");
+__FBSDID("$FreeBSD: head/sys/dev/stge/if_stge.c 229767 2012-01-07 09:41:57Z kevlo $");
 
 #ifdef HAVE_KERNEL_OPTION_HEADERS
 #include "opt_device_polling.h"
@@ -573,7 +573,6 @@
 	ifp->if_ioctl = stge_ioctl;
 	ifp->if_start = stge_start;
 	ifp->if_init = stge_init;
-	ifp->if_mtu = ETHERMTU;
 	ifp->if_snd.ifq_drv_maxlen = STGE_TX_RING_CNT - 1;
 	IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen);
 	IFQ_SET_READY(&ifp->if_snd);
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/tl/if_tl.c
--- a/head/sys/dev/tl/if_tl.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/tl/if_tl.c	Fri Jan 13 15:51:01 2012 +0200
@@ -31,7 +31,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/dev/tl/if_tl.c 227843 2011-11-22 21:28:20Z marius $");
+__FBSDID("$FreeBSD: head/sys/dev/tl/if_tl.c 229767 2012-01-07 09:41:57Z kevlo $");
 
 /*
  * Texas Instruments ThunderLAN driver for FreeBSD 2.2.6 and 3.x.
@@ -1175,7 +1175,6 @@
 	ifp->if_ioctl = tl_ioctl;
 	ifp->if_start = tl_start;
 	ifp->if_init = tl_init;
-	ifp->if_mtu = ETHERMTU;
 	ifp->if_snd.ifq_maxlen = TL_TX_LIST_CNT - 1;
 	ifp->if_capabilities |= IFCAP_VLAN_MTU;
 	ifp->if_capenable |= IFCAP_VLAN_MTU;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/tsec/if_tsec.c
--- a/head/sys/dev/tsec/if_tsec.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/tsec/if_tsec.c	Fri Jan 13 15:51:01 2012 +0200
@@ -28,7 +28,7 @@
  * Freescale integrated Three-Speed Ethernet Controller (TSEC) driver.
  */
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
+__FBSDID("$FreeBSD: head/sys/dev/tsec/if_tsec.c 229767 2012-01-07 09:41:57Z kevlo $");
 
 #ifdef HAVE_KERNEL_OPTION_HEADERS
 #include "opt_device_polling.h"
@@ -247,7 +247,6 @@
 
 	ifp->if_softc = sc;
 	if_initname(ifp, device_get_name(sc->dev), device_get_unit(sc->dev));
-	ifp->if_mtu = ETHERMTU;
 	ifp->if_flags = IFF_SIMPLEX | IFF_MULTICAST | IFF_BROADCAST;
 	ifp->if_init = tsec_init;
 	ifp->if_start = tsec_start;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/uart/uart_bus_pci.c
--- a/head/sys/dev/uart/uart_bus_pci.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/uart/uart_bus_pci.c	Fri Jan 13 15:51:01 2012 +0200
@@ -25,7 +25,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/dev/uart/uart_bus_pci.c 229379 2012-01-03 10:01:12Z kevlo $");
+__FBSDID("$FreeBSD: head/sys/dev/uart/uart_bus_pci.c 229971 2012-01-11 17:46:08Z kib $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -112,6 +112,8 @@
 	0x10, 16384000 },
 { 0x151f, 0x0000, 0xffff, 0, "TOPIC Semiconductor TP560 56k modem", 0x10 },
 { 0x8086, 0x1c3d, 0xffff, 0, "Intel AMT - KT Controller", 0x10 },
+{ 0x8086, 0x3b67, 0xffff, 0, "5 Series/3400 Series Chipset KT Controller",
+	0x10 },
 { 0x8086, 0x8811, 0xffff, 0, "Intel EG20T Serial Port 0", 0x10 },
 { 0x8086, 0x8812, 0xffff, 0, "Intel EG20T Serial Port 1", 0x10 },
 { 0x8086, 0x8813, 0xffff, 0, "Intel EG20T Serial Port 2", 0x10 },
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/usb/net/usb_ethernet.c
--- a/head/sys/dev/usb/net/usb_ethernet.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/usb/net/usb_ethernet.c	Fri Jan 13 15:51:01 2012 +0200
@@ -1,4 +1,4 @@
-/* $FreeBSD: head/sys/dev/usb/net/usb_ethernet.c 227309 2011-11-07 15:43:11Z ed $ */
+/* $FreeBSD: head/sys/dev/usb/net/usb_ethernet.c 229767 2012-01-07 09:41:57Z kevlo $ */
 /*-
  * Copyright (c) 2009 Andrew Thompson (thompsa at FreeBSD.org)
  *
@@ -25,7 +25,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/dev/usb/net/usb_ethernet.c 227309 2011-11-07 15:43:11Z ed $");
+__FBSDID("$FreeBSD: head/sys/dev/usb/net/usb_ethernet.c 229767 2012-01-07 09:41:57Z kevlo $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -219,7 +219,6 @@
 		ue->ue_ifp = ifp;
 		error = ue->ue_methods->ue_attach_post_sub(ue);
 	} else {
-		ifp->if_mtu = ETHERMTU;
 		ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
 		if (ue->ue_methods->ue_ioctl != NULL)
 			ifp->if_ioctl = ue->ue_methods->ue_ioctl;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/vx/if_vx.c
--- a/head/sys/dev/vx/if_vx.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/vx/if_vx.c	Fri Jan 13 15:51:01 2012 +0200
@@ -31,7 +31,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
+__FBSDID("$FreeBSD: head/sys/dev/vx/if_vx.c 229767 2012-01-07 09:41:57Z kevlo $");
 
 /*
  * Created from if_ep.c driver by Fred Gray (fgray at rice.edu) to support
@@ -188,7 +188,6 @@
 		eaddr[(i << 1) + 1] = x;
 	}
 
-	ifp->if_mtu = ETHERMTU;
 	ifp->if_snd.ifq_maxlen = ifqmaxlen;
 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
 	ifp->if_start = vx_start;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/vxge/vxge.c
--- a/head/sys/dev/vxge/vxge.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/vxge/vxge.c	Fri Jan 13 15:51:01 2012 +0200
@@ -28,7 +28,7 @@
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  * POSSIBILITY OF SUCH DAMAGE.
  */
-/*$FreeBSD: head/sys/dev/vxge/vxge.c 221389 2011-05-03 15:58:24Z gnn $*/
+/*$FreeBSD: head/sys/dev/vxge/vxge.c 229767 2012-01-07 09:41:57Z kevlo $*/
 
 #include <dev/vxge/vxge.h>
 
@@ -1378,7 +1378,6 @@
 	/* Initialize interface ifnet structure */
 	if_initname(ifp, device_get_name(ndev), device_get_unit(ndev));
 
-	ifp->if_mtu = ETHERMTU;
 	ifp->if_baudrate = VXGE_BAUDRATE;
 	ifp->if_init = vxge_init;
 	ifp->if_softc = vdev;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/wb/if_wb.c
--- a/head/sys/dev/wb/if_wb.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/wb/if_wb.c	Fri Jan 13 15:51:01 2012 +0200
@@ -31,7 +31,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/dev/wb/if_wb.c 227843 2011-11-22 21:28:20Z marius $");
+__FBSDID("$FreeBSD: head/sys/dev/wb/if_wb.c 229767 2012-01-07 09:41:57Z kevlo $");
 
 /*
  * Winbond fast ethernet PCI NIC driver
@@ -660,7 +660,6 @@
 	}
 	ifp->if_softc = sc;
 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
-	ifp->if_mtu = ETHERMTU;
 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
 	ifp->if_ioctl = wb_ioctl;
 	ifp->if_start = wb_start;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/wtap/if_medium.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/sys/dev/wtap/if_medium.c	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,120 @@
+/*-
+ * Copyright (c) 2010-2011 Monthadar Al Jaberi, TerraNet AB
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer,
+ *    without modification.
+ * 2. Redistributions in binary form must reproduce at minimum a disclaimer
+ *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
+ *    redistribution must be conditioned upon including a substantially
+ *    similar Disclaimer requirement for further binary redistribution.
+ *
+ * NO WARRANTY
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
+ * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
+ * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
+ * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGES.
+ *
+ * $FreeBSD: head/sys/dev/wtap/if_medium.c 229970 2012-01-11 17:41:14Z adrian $
+ */
+#include "if_wtapvar.h"
+#include "if_medium.h"
+
+void
+init_medium(struct wtap_medium *md)
+{
+
+	DWTAP_PRINTF("%s\n", __func__);
+	STAILQ_INIT(&md->md_pktbuf);
+	mtx_init(&md->md_mtx, "wtap_medium mtx", NULL, MTX_DEF | MTX_RECURSE);
+
+	/* Event handler for sending packets between wtaps */
+	struct eventhandler *eh = (struct eventhandler *)
+	    malloc(sizeof(struct eventhandler), M_WTAP, M_NOWAIT | M_ZERO);
+	eh->tq = taskqueue_create("wtap_tx_taskq",  M_NOWAIT | M_ZERO,
+	    taskqueue_thread_enqueue, &eh->tq);
+	taskqueue_start_threads(&eh->tq, 1, PI_NET, "%s taskq", "wtap_medium");
+	md->tx_handler = eh;
+	/* Mark medium closed by default */
+	md->open = 0;
+}
+
+void
+deinit_medium(struct wtap_medium *md)
+{
+
+	DWTAP_PRINTF("%s\n", __func__);
+	taskqueue_free(md->tx_handler->tq);
+	free(md->tx_handler, M_WTAP);
+}
+
+int
+medium_transmit(struct wtap_medium *md, int id, struct mbuf*m)
+{
+
+	mtx_lock(&md->md_mtx);
+	if (md->open == 0){
+		DWTAP_PRINTF("[%d] dropping m=%p\n", id, m);
+		m_free(m);
+		mtx_unlock(&md->md_mtx);
+		return 0;
+	}
+
+	DWTAP_PRINTF("[%d] transmiting m=%p\n", id, m);
+	struct packet *p = (struct packet *)malloc(sizeof(struct packet),
+	    M_WTAP_PACKET, M_ZERO | M_NOWAIT);
+	p->id = id;
+	p->m = m;
+
+	STAILQ_INSERT_TAIL(&md->md_pktbuf, p, pf_list);
+	taskqueue_enqueue(md->tx_handler->tq, &md->tx_handler->proc);
+	mtx_unlock(&md->md_mtx);
+
+      return 0;
+}
+
+struct packet *
+medium_get_next_packet(struct wtap_medium *md)
+{
+	struct packet *p;
+
+	mtx_lock(&md->md_mtx);
+	p = STAILQ_FIRST(&md->md_pktbuf);
+	if (p == NULL){
+		mtx_unlock(&md->md_mtx);
+		return NULL;
+	}
+
+	STAILQ_REMOVE_HEAD(&md->md_pktbuf, pf_list);
+	mtx_unlock(&md->md_mtx);
+	return p;
+}
+
+void
+medium_open(struct wtap_medium *md)
+{
+
+	mtx_lock(&md->md_mtx);
+	md->open = 1;
+	mtx_unlock(&md->md_mtx);
+}
+
+void
+medium_close(struct wtap_medium *md)
+{
+
+	mtx_lock(&md->md_mtx);
+	md->open = 0;
+	mtx_unlock(&md->md_mtx);
+}
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/wtap/if_medium.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/sys/dev/wtap/if_medium.h	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,72 @@
+/*-
+ * Copyright (c) 2010-2011 Monthadar Al Jaberi, TerraNet AB
+ * All rights reserved.
+ *
+ * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer,
+ *    without modification.
+ * 2. Redistributions in binary form must reproduce at minimum a disclaimer
+ *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
+ *    redistribution must be conditioned upon including a substantially
+ *    similar Disclaimer requirement for further binary redistribution.
+ *
+ * NO WARRANTY
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
+ * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
+ * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
+ * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGES.
+ *
+ * $FreeBSD: head/sys/dev/wtap/if_medium.h 229970 2012-01-11 17:41:14Z adrian $
+ */
+
+#ifndef	__DEV_WTAP_MEDIUM_H__
+#define	__DEV_WTAP_MEDIUM_H__
+
+#include "if_wtapvar.h"
+#include "wtap_hal/handler.h"
+
+struct packet {
+	STAILQ_ENTRY(packet)	pf_list;
+	struct mbuf *		m;
+	int			id;
+};
+typedef STAILQ_HEAD(, packet) md_pkthead;
+
+struct wtap_medium {
+	struct mtx 			md_mtx;
+#if 0
+	int				visibility[MAX_NBR_WTAP];
+	struct stailhead 		*headp;
+	packet_head			pktbuf;
+	STAILQ_HEAD(stailhead, packet) pktbuf;
+	STAILQ_HEAD(stailhead, packet) pktbuf;
+	/* = STAILQ_HEAD_INITIALIZER(head); */
+#endif
+	/* 0 means we drop packets, 1 we queue them */
+	int				open;
+	md_pkthead			md_pktbuf;	/* master queue */
+	struct eventhandler		*tx_handler;
+	struct timehandler		*bc_handler;
+};
+
+extern	void init_medium(struct wtap_medium *);
+extern	void deinit_medium(struct wtap_medium *);
+extern	void medium_open(struct wtap_medium *);
+extern	void medium_close(struct wtap_medium *);
+extern	int medium_transmit(struct wtap_medium *, int id, struct mbuf*);
+extern	struct packet *medium_get_next_packet(struct wtap_medium *);
+
+#endif	/* __DEV_WTAP_MEDIUM_H__ */
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/wtap/if_wtap.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/sys/dev/wtap/if_wtap.c	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,904 @@
+/*-
+ * Copyright (c) 2010-2011 Monthadar Al Jaberi, TerraNet AB
+ * All rights reserved.
+ *
+ * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer,
+ *    without modification.
+ * 2. Redistributions in binary form must reproduce at minimum a disclaimer
+ *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
+ *    redistribution must be conditioned upon including a substantially
+ *    similar Disclaimer requirement for further binary redistribution.
+ *
+ * NO WARRANTY
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
+ * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
+ * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
+ * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGES.
+ *
+ * $FreeBSD: head/sys/dev/wtap/if_wtap.c 229970 2012-01-11 17:41:14Z adrian $
+ */
+#include "if_wtapvar.h"
+#include <sys/uio.h>    /* uio struct */
+#include <sys/jail.h>
+#include <net/if_var.h>
+#include <net/vnet.h>
+
+#include <net80211/ieee80211_ratectl.h>
+#include "if_medium.h"
+
+/* device for IOCTL and read/write for debuggin purposes */
+/* Function prototypes */
+static	d_open_t	wtap_node_open;
+static	d_close_t	wtap_node_close;
+static	d_write_t	wtap_node_write;
+static	d_ioctl_t	wtap_node_ioctl;
+
+static struct cdevsw wtap_cdevsw = {
+	.d_version =	D_VERSION,
+	.d_flags =	0,
+	.d_open = 	wtap_node_open,
+	.d_close = 	wtap_node_close,
+	.d_write = 	wtap_node_write,
+	.d_ioctl =	wtap_node_ioctl,
+	.d_name =	"wtapnode",
+};
+
+static int
+wtap_node_open(struct cdev *dev, int oflags, int devtype, struct thread *p)
+{
+
+	int err = 0;
+	uprintf("Opened device \"echo\" successfully.\n");
+	return(err);
+}
+
+static int
+wtap_node_close(struct cdev *dev, int fflag, int devtype, struct thread *p)
+{
+
+	uprintf("Closing device \"echo.\"\n");
+	return(0);
+}
+
+static int
+wtap_node_write(struct cdev *dev, struct uio *uio, int ioflag)
+{
+	int err = 0;
+	struct mbuf *m;
+	struct ifnet *ifp;
+	struct wtap_softc *sc;
+	uint8_t buf[1024];
+	int buf_len;
+
+	uprintf("write device %s \"echo.\"\n", dev->si_name);
+	buf_len = MIN(uio->uio_iov->iov_len, 1024);
+	err = copyin(uio->uio_iov->iov_base, buf, buf_len);
+
+	if (err != 0) {
+		uprintf("Write failed: bad address!\n");
+		return (err);
+	}
+
+	MGETHDR(m, M_DONTWAIT, MT_DATA);
+	m_copyback(m, 0, buf_len, buf);
+
+	CURVNET_SET(TD_TO_VNET(curthread));
+	IFNET_RLOCK_NOSLEEP();
+
+	TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
+		printf("ifp->if_xname = %s\n", ifp->if_xname);
+		if(strcmp(dev->si_name, ifp->if_xname) == 0){
+			printf("found match, correspoding wtap = %s\n",
+			    ifp->if_xname);
+			sc = (struct wtap_softc *)ifp->if_softc;
+			printf("wtap id = %d\n", sc->id);
+			wtap_inject(sc, m);
+		}
+	}
+
+	IFNET_RUNLOCK_NOSLEEP();
+	CURVNET_RESTORE();
+
+	return(err);
+}
+
+int
+wtap_node_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
+    int fflag, struct thread *td)
+{
+	int error = 0;
+
+	switch(cmd) {
+	default:
+		DWTAP_PRINTF("Unkown WTAP IOCTL\n");
+		error = EINVAL;
+	}
+	return error;
+}
+
+static int wtap_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
+	const struct ieee80211_bpf_params *params);
+
+static int
+wtap_medium_enqueue(struct wtap_vap *avp, struct mbuf *m)
+{
+
+	return medium_transmit(avp->av_md, avp->id, m);
+}
+
+static int
+wtap_media_change(struct ifnet *ifp)
+{
+
+	DWTAP_PRINTF("%s\n", __func__);
+	int error = ieee80211_media_change(ifp);
+	/* NB: only the fixed rate can change and that doesn't need a reset */
+	return (error == ENETRESET ? 0 : error);
+}
+
+/*
+ * Intercept management frames to collect beacon rssi data
+ * and to do ibss merges.
+ */
+static void
+wtap_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m,
+    int subtype, int rssi, int nf)
+{
+	struct ieee80211vap *vap = ni->ni_vap;
+#if 0
+	DWTAP_PRINTF("[%d] %s\n", myath_id(ni), __func__);
+#endif
+	WTAP_VAP(vap)->av_recv_mgmt(ni, m, subtype, rssi, nf);
+}
+
+static int
+wtap_reset_vap(struct ieee80211vap *vap, u_long cmd)
+{
+
+	DWTAP_PRINTF("%s\n", __func__);
+	return 0;
+}
+
+static void
+wtap_beacon_update(struct ieee80211vap *vap, int item)
+{
+	struct ieee80211_beacon_offsets *bo = &WTAP_VAP(vap)->av_boff;
+
+	DWTAP_PRINTF("%s\n", __func__);
+	setbit(bo->bo_flags, item);
+}
+
+/*
+ * Allocate and setup an initial beacon frame.
+ */
+static int
+wtap_beacon_alloc(struct wtap_softc *sc, struct ieee80211_node *ni)
+{
+	struct ieee80211vap *vap = ni->ni_vap;
+	struct wtap_vap *avp = WTAP_VAP(vap);
+
+	DWTAP_PRINTF("[%s] %s\n", ether_sprintf(ni->ni_macaddr), __func__);
+
+	/*
+	 * NB: the beacon data buffer must be 32-bit aligned;
+	 * we assume the mbuf routines will return us something
+	 * with this alignment (perhaps should assert).
+	 */
+	avp->beacon = ieee80211_beacon_alloc(ni, &avp->av_boff);
+	if (avp->beacon == NULL) {
+		printf("%s: cannot get mbuf\n", __func__);
+		return ENOMEM;
+	}
+	callout_init(&avp->av_swba, 0);
+	avp->bf_node = ieee80211_ref_node(ni);
+
+	return 0;
+}
+
+static void
+wtap_beacon_config(struct wtap_softc *sc, struct ieee80211vap *vap)
+{
+
+	DWTAP_PRINTF("%s\n", __func__);
+}
+
+static void
+wtap_beacon_intrp(void *arg)
+{
+	struct wtap_vap *avp = arg;
+	struct ieee80211vap *vap = arg;
+	struct mbuf *m;
+
+	KASSERT(vap->iv_state >= IEEE80211_S_RUN,
+	    ("not running, state %d", vap->iv_state));
+	DWTAP_PRINTF("[%d] beacon intrp\n", avp->id);	//burst mode
+	/*
+	 * Update dynamic beacon contents.  If this returns
+	 * non-zero then we need to remap the memory because
+	 * the beacon frame changed size (probably because
+	 * of the TIM bitmap).
+	 */
+	m = m_dup(avp->beacon, M_DONTWAIT);
+	if (ieee80211_beacon_update(avp->bf_node, &avp->av_boff, m, 0)) {
+		printf("%s, need to remap the memory because the beacon frame"
+		    " changed size.\n",__func__);
+	}
+
+	if (ieee80211_radiotap_active_vap(vap))
+	    ieee80211_radiotap_tx(vap, m);
+
+#if 0
+	medium_transmit(avp->av_md, avp->id, m);
+#endif
+	wtap_medium_enqueue(avp, m);
+	callout_schedule(&avp->av_swba, avp->av_bcinterval);
+}
+
+static int
+wtap_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
+{
+	struct ieee80211com *ic = vap->iv_ic;
+	struct wtap_softc *sc = ic->ic_ifp->if_softc;
+	struct wtap_vap *avp = WTAP_VAP(vap);
+	struct ieee80211_node *ni = NULL;
+	int error;
+
+	DWTAP_PRINTF("%s\n", __func__);
+
+	ni = vap->iv_bss;
+	/*
+	 * Invoke the parent method to do net80211 work.
+	 */
+	error = avp->av_newstate(vap, nstate, arg);
+	if (error != 0)
+		goto bad;
+
+	if (nstate == IEEE80211_S_RUN) {
+		/* NB: collect bss node again, it may have changed */
+		ni = vap->iv_bss;
+		switch (vap->iv_opmode) {
+		case IEEE80211_M_MBSS:
+			error = wtap_beacon_alloc(sc, ni);
+			if (error != 0)
+				goto bad;
+			wtap_beacon_config(sc, vap);
+			callout_reset(&avp->av_swba, avp->av_bcinterval,
+			    wtap_beacon_intrp, vap);
+			break;
+		default:
+			goto bad;
+		}
+	}
+	return 0;
+bad:
+	printf("%s: bad\n", __func__);
+	return error;
+}
+
+static void
+wtap_bmiss(struct ieee80211vap *vap)
+{
+	struct wtap_vap *avp = (struct wtap_vap *)vap;
+
+	DWTAP_PRINTF("%s\n", __func__);
+	avp->av_bmiss(vap);
+}
+
+static struct ieee80211vap *
+wtap_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ],
+    int unit, enum ieee80211_opmode opmode, int flags,
+    const uint8_t bssid[IEEE80211_ADDR_LEN],
+    const uint8_t mac[IEEE80211_ADDR_LEN])
+{
+	 struct wtap_softc *sc = ic->ic_ifp->if_softc;
+	 struct ieee80211vap *vap;
+	 struct wtap_vap *avp;
+	 int error;
+
+	 DWTAP_PRINTF("%s\n", __func__);
+
+	avp = (struct wtap_vap *) malloc(sizeof(struct wtap_vap),
+	    M_80211_VAP, M_NOWAIT | M_ZERO);
+	avp->id = sc->id;
+	avp->av_md = sc->sc_md;
+	avp->av_bcinterval = BEACON_INTRERVAL + 100*sc->id;
+	vap = (struct ieee80211vap *) avp;
+	error = ieee80211_vap_setup(ic, vap, name, unit, IEEE80211_M_MBSS,
+	    flags | IEEE80211_CLONE_NOBEACONS, bssid, mac);
+
+	/* override various methods */
+	avp->av_recv_mgmt = vap->iv_recv_mgmt;
+	vap->iv_recv_mgmt = wtap_recv_mgmt;
+	vap->iv_reset = wtap_reset_vap;
+	vap->iv_update_beacon = wtap_beacon_update;
+	avp->av_newstate = vap->iv_newstate;
+	vap->iv_newstate = wtap_newstate;
+	avp->av_bmiss = vap->iv_bmiss;
+	vap->iv_bmiss = wtap_bmiss;
+
+	/* complete setup */
+	ieee80211_vap_attach(vap, wtap_media_change, ieee80211_media_status);
+	avp->av_dev = make_dev(&wtap_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
+	    (const char *)ic->ic_ifp->if_xname);
+
+	/* TODO this is a hack to force it to choose the rate we want */
+	vap->iv_bss->ni_txrate = 130;
+	return vap;
+}
+
+static void
+wtap_vap_delete(struct ieee80211vap *vap)
+{
+	struct wtap_vap *avp = WTAP_VAP(vap);
+
+	DWTAP_PRINTF("%s\n", __func__);
+	destroy_dev(avp->av_dev);
+	callout_stop(&avp->av_swba);
+	ieee80211_vap_detach(vap);
+	free((struct wtap_vap*) vap, M_80211_VAP);
+}
+
+/* NB: This function is not used.
+ * I had the problem of the queue
+ * being empty all the time.
+ * Maybe I am setting the queue wrong?
+ */
+static void
+wtap_start(struct ifnet *ifp)
+{
+	struct ieee80211com *ic = ifp->if_l2com;
+	struct ifnet *icifp = ic->ic_ifp;
+	struct wtap_softc *sc = icifp->if_softc;
+	struct ieee80211_node *ni;
+	struct mbuf *m;
+
+	DWTAP_PRINTF("my_start, with id=%u\n", sc->id);
+
+	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || sc->up == 0)
+		return;
+	for (;;) {
+		if(IFQ_IS_EMPTY(&ifp->if_snd)){
+		    printf("queue empty, just trying to see "
+		        "if the other queue is empty\n");
+#if 0
+		    printf("queue for id=1, %u\n",
+		        IFQ_IS_EMPTY(&global_mscs[1]->ifp->if_snd));
+		    printf("queue for id=0, %u\n",
+		        IFQ_IS_EMPTY(&global_mscs[0]->ifp->if_snd));
+#endif
+		    break;
+		}
+		IFQ_DEQUEUE(&ifp->if_snd, m);
+		if (m == NULL) {
+			printf("error dequeueing from ifp->snd\n");
+			break;
+		}
+		ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
+		/*
+		 * Check for fragmentation.  If this frame
+		 * has been broken up verify we have enough
+		 * buffers to send all the fragments so all
+		 * go out or none...
+		 */
+#if 0
+		STAILQ_INIT(&frags);
+#endif
+		if ((m->m_flags & M_FRAG)){
+			printf("dont support frags\n");
+			ifp->if_oerrors++;
+			return;
+		}
+		ifp->if_opackets++;
+		if(wtap_raw_xmit(ni, m, NULL) < 0){
+			printf("error raw_xmiting\n");
+			ifp->if_oerrors++;
+			return;
+		}
+	}
+}
+
+static int
+wtap_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
+{
+#if 0
+	DWTAP_PRINTF("%s\n", __func__);
+	uprintf("%s, command %lu\n", __func__, cmd);
+#endif
+#define	IS_RUNNING(ifp) \
+	((ifp->if_flags & IFF_UP) && (ifp->if_drv_flags & IFF_DRV_RUNNING))
+	struct ieee80211com *ic = ifp->if_l2com;
+	struct wtap_softc *sc = ifp->if_softc;
+	struct ifreq *ifr = (struct ifreq *)data;
+	int error = 0;
+
+	switch (cmd) {
+	case SIOCSIFFLAGS:
+		//printf("%s: %s\n", __func__, "SIOCSIFFLAGS");
+		if (IS_RUNNING(ifp)) {
+			DWTAP_PRINTF("running\n");
+#if 0
+			/*
+			 * To avoid rescanning another access point,
+			 * do not call ath_init() here.  Instead,
+			 * only reflect promisc mode settings.
+			 */
+			//ath_mode_init(sc);
+#endif
+			} else if (ifp->if_flags & IFF_UP) {
+			DWTAP_PRINTF("up\n");
+			sc->up = 1;
+#if 0
+			/*
+			 * Beware of being called during attach/detach
+			 * to reset promiscuous mode.  In that case we
+			 * will still be marked UP but not RUNNING.
+			 * However trying to re-init the interface
+			 * is the wrong thing to do as we've already
+			 * torn down much of our state.  There's
+			 * probably a better way to deal with this.
+			 */
+			//if (!sc->sc_invalid)
+			//	ath_init(sc);	/* XXX lose error */
+#endif
+			ifp->if_drv_flags |= IFF_DRV_RUNNING;
+			ieee80211_start_all(ic);
+		} else {
+			DWTAP_PRINTF("stoping\n");
+#if 0
+			ath_stop_locked(ifp);
+#ifdef notyet
+			/* XXX must wakeup in places like ath_vap_delete */
+			if (!sc->sc_invalid)
+				ath_hal_setpower(sc->sc_ah, HAL_PM_FULL_SLEEP);
+#endif
+#endif
+		}
+		break;
+	case SIOCGIFMEDIA:
+	case SIOCSIFMEDIA:
+#if 0
+		DWTAP_PRINTF("%s: %s\n", __func__, "SIOCGIFMEDIA|SIOCSIFMEDIA");
+#endif
+		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
+		break;
+	case SIOCGIFADDR:
+#if 0
+		DWTAP_PRINTF("%s: %s\n", __func__, "SIOCGIFADDR");
+#endif
+		error = ether_ioctl(ifp, cmd, data);
+		break;
+	default:
+		DWTAP_PRINTF("%s: %s [%lu]\n", __func__, "EINVAL", cmd);
+		error = EINVAL;
+		break;
+	}
+	return error;
+#undef IS_RUNNING
+}
+
+static void
+wtap_init(void *arg){
+
+	DWTAP_PRINTF("%s\n", __func__);
+}
+
+static void
+wtap_scan_start(struct ieee80211com *ic)
+{
+
+#if 0
+	DWTAP_PRINTF("%s\n", __func__);
+#endif
+}
+
+static void
+wtap_scan_end(struct ieee80211com *ic)
+{
+
+#if 0
+	DWTAP_PRINTF("%s\n", __func__);
+#endif
+}
+
+static void
+wtap_set_channel(struct ieee80211com *ic)
+{
+
+#if 0
+	DWTAP_PRINTF("%s\n", __func__);
+#endif
+}
+
+static int
+wtap_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
+	const struct ieee80211_bpf_params *params)
+{
+#if 0
+	DWTAP_PRINTF("%s, %p\n", __func__, m);
+#endif
+	struct ieee80211vap	*vap = ni->ni_vap;
+	struct wtap_vap 	*avp = WTAP_VAP(vap);
+
+	if (ieee80211_radiotap_active_vap(vap)) {
+		ieee80211_radiotap_tx(vap, m);
+	}
+	if (m->m_flags & M_TXCB)
+		ieee80211_process_callback(ni, m, 0);
+	ieee80211_free_node(ni);
+	return wtap_medium_enqueue(avp, m);
+}
+
+void
+wtap_inject(struct wtap_softc *sc, struct mbuf *m)
+{
+      struct wtap_buf *bf = (struct wtap_buf *)malloc(sizeof(struct wtap_buf),
+          M_WTAP_RXBUF, M_NOWAIT | M_ZERO);
+      KASSERT(bf != NULL, ("could not allocated a new wtap_buf\n"));
+      bf->m = m;
+
+      mtx_lock(&sc->sc_mtx);
+      STAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
+      taskqueue_enqueue(sc->sc_tq, &sc->sc_rxtask);
+      mtx_unlock(&sc->sc_mtx);
+}
+
+void
+wtap_rx_deliver(struct wtap_softc *sc, struct mbuf *m)
+{
+	struct ifnet *ifp = sc->sc_ifp;
+	struct ieee80211com *ic = ifp->if_l2com;
+	struct ieee80211_node *ni;
+	int type;
+#if 0
+	DWTAP_PRINTF("%s\n", __func__);
+#endif
+
+	DWTAP_PRINTF("[%d] receiving m=%p\n", sc->id, m);
+	if (m == NULL) {		/* NB: shouldn't happen */
+		if_printf(ifp, "%s: no mbuf!\n", __func__);
+	}
+
+	ifp->if_ipackets++;
+
+	ieee80211_dump_pkt(ic, mtod(m, caddr_t), 0,0,0);
+
+	/*
+	  * Locate the node for sender, track state, and then
+	  * pass the (referenced) node up to the 802.11 layer
+	  * for its use.
+	  */
+	ni = ieee80211_find_rxnode_withkey(ic,
+	    mtod(m, const struct ieee80211_frame_min *),IEEE80211_KEYIX_NONE);
+	if (ni != NULL) {
+		/*
+		 * Sending station is known, dispatch directly.
+		 */
+		type = ieee80211_input(ni, m, 1<<7, 10);
+		ieee80211_free_node(ni);
+	} else {
+		type = ieee80211_input_all(ic, m, 1<<7, 10);
+	}
+}
+
+static void
+wtap_rx_proc(void *arg, int npending)
+{
+	struct wtap_softc *sc = (struct wtap_softc *)arg;
+	struct ifnet *ifp = sc->sc_ifp;
+	struct ieee80211com *ic = ifp->if_l2com;
+	struct mbuf *m;
+	struct ieee80211_node *ni;
+	int type;
+	struct wtap_buf *bf;
+
+#if 0
+	DWTAP_PRINTF("%s\n", __func__);
+#endif
+
+	for(;;) {
+		mtx_lock(&sc->sc_mtx);
+		bf = STAILQ_FIRST(&sc->sc_rxbuf);
+		if (bf == NULL) {
+			mtx_unlock(&sc->sc_mtx);
+			return;
+		}
+		STAILQ_REMOVE_HEAD(&sc->sc_rxbuf, bf_list);
+		mtx_unlock(&sc->sc_mtx);
+		KASSERT(bf != NULL, ("wtap_buf is NULL\n"));
+		m = bf->m;
+		DWTAP_PRINTF("[%d] receiving m=%p\n", sc->id, bf->m);
+		if (m == NULL) {		/* NB: shouldn't happen */
+			if_printf(ifp, "%s: no mbuf!\n", __func__);
+			free(bf, M_WTAP_RXBUF);
+			return;
+		}
+
+		ifp->if_ipackets++;
+#if 0
+		ieee80211_dump_pkt(ic, mtod(m, caddr_t), 0,0,0);
+#endif
+
+		/*
+		 * Locate the node for sender, track state, and then
+		 * pass the (referenced) node up to the 802.11 layer
+		 * for its use.
+		 */
+		ni = ieee80211_find_rxnode_withkey(ic,
+		    mtod(m, const struct ieee80211_frame_min *),
+		    IEEE80211_KEYIX_NONE);
+		if (ni != NULL) {
+			/*
+			 * Sending station is known, dispatch directly.
+			 */
+#if 0
+			ieee80211_radiotap_rx(ni->ni_vap, m);
+#endif
+			type = ieee80211_input(ni, m, 1<<7, 10);
+			ieee80211_free_node(ni);
+		} else {
+#if 0
+			ieee80211_radiotap_rx_all(ic, m);
+#endif
+			type = ieee80211_input_all(ic, m, 1<<7, 10);
+		}
+		
+		/* The mbufs are freed by the Net80211 stack */
+		free(bf, M_WTAP_RXBUF);
+	}
+}
+
+static void
+wtap_newassoc(struct ieee80211_node *ni, int isnew)
+{
+
+	DWTAP_PRINTF("%s\n", __func__);
+}
+
+/*
+ * Callback from the 802.11 layer to update WME parameters.
+ */
+static int
+wtap_wme_update(struct ieee80211com *ic)
+{
+
+	DWTAP_PRINTF("%s\n", __func__);
+	return 0;
+}
+
+static void
+wtap_update_mcast(struct ifnet *ifp)
+{
+
+	DWTAP_PRINTF("%s\n", __func__);
+}
+
+static void
+wtap_update_promisc(struct ifnet *ifp)
+{
+
+	DWTAP_PRINTF("%s\n", __func__);
+}
+
+static int
+wtap_if_transmit(struct ifnet *ifp, struct mbuf *m)
+{
+	struct ieee80211_node *ni =
+	    (struct ieee80211_node *) m->m_pkthdr.rcvif;
+	struct ieee80211vap *vap = ni->ni_vap;
+	struct wtap_vap *avp = WTAP_VAP(vap);
+
+	if(ni == NULL){
+		printf("m->m_pkthdr.rcvif is NULL we cant radiotap_tx\n");
+	}else{
+		if (ieee80211_radiotap_active_vap(vap))
+			ieee80211_radiotap_tx(vap, m);
+	}
+	if (m->m_flags & M_TXCB)
+		ieee80211_process_callback(ni, m, 0);
+	ieee80211_free_node(ni);
+	return wtap_medium_enqueue(avp, m);
+}
+
+static struct ieee80211_node *
+wtap_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
+{
+	struct ieee80211_node *ni;
+
+	DWTAP_PRINTF("%s\n", __func__);
+
+	ni = malloc(sizeof(struct ieee80211_node), M_80211_NODE,
+	    M_NOWAIT|M_ZERO);
+
+	ni->ni_txrate = 130;
+	return ni;
+}
+
+static void
+wtap_node_free(struct ieee80211_node *ni)
+{
+	struct ieee80211com *ic = ni->ni_ic;
+	struct wtap_softc *sc = ic->ic_ifp->if_softc;
+
+	DWTAP_PRINTF("%s\n", __func__);
+	sc->sc_node_free(ni);
+}
+
+int32_t
+wtap_attach(struct wtap_softc *sc, const uint8_t *macaddr)
+{
+	struct ifnet *ifp;
+	struct ieee80211com *ic;
+	char wtap_name[] = {'w','T','a','p',sc->id,
+	    '_','t','a','s','k','q','\0'};
+
+	DWTAP_PRINTF("%s\n", __func__);
+
+	ifp = if_alloc(IFT_IEEE80211);
+	if (ifp == NULL) {
+		printf("can not if_alloc()\n");
+		return -1;
+	}
+	ic = ifp->if_l2com;
+	if_initname(ifp, "wtap", sc->id);
+
+	sc->sc_ifp = ifp;
+	sc->up = 0;
+
+	STAILQ_INIT(&sc->sc_rxbuf);
+	sc->sc_tq = taskqueue_create(wtap_name, M_NOWAIT | M_ZERO,
+	    taskqueue_thread_enqueue, &sc->sc_tq);
+	taskqueue_start_threads(&sc->sc_tq, 1, PI_SOFT, "%s taskQ",
+	    ifp->if_xname);
+	TASK_INIT(&sc->sc_rxtask, 0, wtap_rx_proc, sc);
+
+	ifp->if_softc = sc;
+	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
+	ifp->if_start = wtap_start;
+	ifp->if_ioctl = wtap_ioctl;
+	ifp->if_init = wtap_init;
+	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
+	ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
+	IFQ_SET_READY(&ifp->if_snd);
+
+	ic->ic_ifp = ifp;
+	ic->ic_phytype = IEEE80211_T_DS;
+	ic->ic_opmode = IEEE80211_M_MBSS;
+	ic->ic_caps = IEEE80211_C_MBSS;
+
+	ic->ic_max_keyix = 128; /* A value read from Atheros ATH_KEYMAX */
+
+	ic->ic_regdomain.regdomain = SKU_ETSI;
+	ic->ic_regdomain.country = CTRY_SWEDEN;
+	ic->ic_regdomain.location = 1; /* Indoors */
+	ic->ic_regdomain.isocc[0] = 'S';
+	ic->ic_regdomain.isocc[1] = 'E';
+	/*
+	 * Indicate we need the 802.11 header padded to a
+	 * 32-bit boundary for 4-address and QoS frames.
+	 */
+	ic->ic_flags |= IEEE80211_F_DATAPAD;
+	ic->ic_nchans = 1;
+	ic->ic_channels[0].ic_flags = IEEE80211_CHAN_B;
+	ic->ic_channels[0].ic_freq = 2412;
+
+	ieee80211_ifattach(ic, macaddr);
+
+#if 0
+	/* new prototype hook-ups */
+	msc->if_input = ifp->if_input;
+	ifp->if_input = myath_if_input;
+	msc->if_output = ifp->if_output;
+	ifp->if_output = myath_if_output;
+#endif
+	sc->if_transmit = ifp->if_transmit;
+	ifp->if_transmit = wtap_if_transmit;
+
+	/* override default methods */
+	ic->ic_newassoc = wtap_newassoc;
+#if 0
+	ic->ic_updateslot = myath_updateslot;
+#endif
+	ic->ic_wme.wme_update = wtap_wme_update;
+	ic->ic_vap_create = wtap_vap_create;
+	ic->ic_vap_delete = wtap_vap_delete;
+	ic->ic_raw_xmit = wtap_raw_xmit;
+	ic->ic_update_mcast = wtap_update_mcast;
+	ic->ic_update_promisc = wtap_update_promisc;
+
+	sc->sc_node_alloc = ic->ic_node_alloc;
+	ic->ic_node_alloc = wtap_node_alloc;
+	sc->sc_node_free = ic->ic_node_free;
+	ic->ic_node_free = wtap_node_free;
+
+#if 0
+	ic->ic_node_getsignal = myath_node_getsignal;
+#endif
+	ic->ic_scan_start = wtap_scan_start;
+	ic->ic_scan_end = wtap_scan_end;
+	ic->ic_set_channel = wtap_set_channel;
+
+	ieee80211_radiotap_attach(ic,
+	    &sc->sc_tx_th.wt_ihdr, sizeof(sc->sc_tx_th),
+	    WTAP_TX_RADIOTAP_PRESENT,
+	    &sc->sc_rx_th.wr_ihdr, sizeof(sc->sc_rx_th),
+	    WTAP_RX_RADIOTAP_PRESENT);
+
+	/* Work here, we must find a way to populate the rate table */
+#if 0
+	if(ic->ic_rt == NULL){
+		printf("no table for ic_curchan\n");
+		ic->ic_rt = ieee80211_get_ratetable(&ic->ic_channels[0]);
+	}
+	printf("ic->ic_rt =%p\n", ic->ic_rt);
+	printf("rate count %d\n", ic->ic_rt->rateCount);
+
+	uint8_t code = ic->ic_rt->info[0].dot11Rate;
+	uint8_t cix = ic->ic_rt->info[0].ctlRateIndex;
+	uint8_t ctl_rate = ic->ic_rt->info[cix].dot11Rate;
+	printf("code=%d, cix=%d, ctl_rate=%d\n", code, cix, ctl_rate);
+
+	uint8_t rix0 = ic->ic_rt->rateCodeToIndex[130];
+	uint8_t rix1 = ic->ic_rt->rateCodeToIndex[132];
+	uint8_t rix2 = ic->ic_rt->rateCodeToIndex[139];
+	uint8_t rix3 = ic->ic_rt->rateCodeToIndex[150];
+	printf("rix0 %u,rix1 %u,rix2 %u,rix3 %u\n", rix0,rix1,rix2,rix3);
+	printf("lpAckDuration=%u\n", ic->ic_rt->info[0].lpAckDuration);
+	printf("rate=%d\n", ic->ic_rt->info[0].rateKbps);
+#endif
+	return 0;
+}
+
+int32_t
+wtap_detach(struct wtap_softc *sc)
+{
+	struct ifnet *ifp = sc->sc_ifp;
+	struct ieee80211com *ic = ifp->if_l2com;
+
+	DWTAP_PRINTF("%s\n", __func__);
+	ieee80211_ageq_drain(&ic->ic_stageq);
+	ieee80211_ifdetach(ic);
+	if_free(ifp);
+	return 0;
+}
+
+void
+wtap_resume(struct wtap_softc *sc)
+{
+
+	DWTAP_PRINTF("%s\n", __func__);
+}
+
+void
+wtap_suspend(struct wtap_softc *sc)
+{
+
+	DWTAP_PRINTF("%s\n", __func__);
+}
+
+void
+wtap_shutdown(struct wtap_softc *sc)
+{
+
+	DWTAP_PRINTF("%s\n", __func__);
+}
+
+void
+wtap_intr(struct wtap_softc *sc)
+{
+
+	DWTAP_PRINTF("%s\n", __func__);
+}
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/wtap/if_wtap_module.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/sys/dev/wtap/if_wtap_module.c	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,186 @@
+/*-
+ * Copyright (c) 2010-2011 Monthadar Al Jaberi, TerraNet AB
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer,
+ *    without modification.
+ * 2. Redistributions in binary form must reproduce at minimum a disclaimer
+ *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
+ *    redistribution must be conditioned upon including a substantially
+ *    similar Disclaimer requirement for further binary redistribution.
+ *
+ * NO WARRANTY
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
+ * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
+ * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
+ * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGES.
+ *
+ * $FreeBSD: head/sys/dev/wtap/if_wtap_module.c 229970 2012-01-11 17:41:14Z adrian $
+ */
+#include <sys/param.h>
+#include <sys/module.h>
+#include <sys/kernel.h>
+#include <sys/systm.h>
+#include <sys/sysctl.h>
+#include <sys/mbuf.h>
+#include <sys/malloc.h>
+#include <sys/lock.h>
+#include <sys/mutex.h>
+#include <sys/proc.h>
+#include <sys/ucred.h>
+#include <sys/jail.h>
+
+#include <sys/types.h>
+#include <sys/sockio.h>
+#include <sys/socket.h>
+#include <sys/socketvar.h>
+#include <sys/errno.h>
+#include <sys/callout.h>
+#include <sys/endian.h>
+#include <sys/kthread.h>
+#include <sys/taskqueue.h>
+#include <sys/priv.h>
+#include <sys/sysctl.h>
+
+#include <machine/bus.h>
+
+#include <net/if.h>
+#include <net/if_dl.h>
+#include <net/if_media.h>
+#include <net/if_types.h>
+#include <net/if_arp.h>
+#include <net/ethernet.h>
+#include <net/if_llc.h>
+#include <net/vnet.h>
+
+#include <net80211/ieee80211_var.h>
+#include <net80211/ieee80211_regdomain.h>
+
+#include <net/bpf.h>
+
+
+#include <sys/errno.h>
+#include <sys/conf.h>   /* cdevsw struct */
+#include <sys/uio.h>    /* uio struct */
+
+
+#include <netinet/in.h>
+#include <netinet/if_ether.h>
+
+#include "if_wtapvar.h"
+#include "if_wtapioctl.h"
+#include "if_medium.h"
+#include "wtap_hal/hal.h"
+
+/* WTAP PLUGINS */
+#include "plugins/visibility.h"
+
+MALLOC_DEFINE(M_WTAP, "wtap", "wtap wireless simulator");
+MALLOC_DEFINE(M_WTAP_PACKET, "wtap packet", "wtap wireless simulator packet");
+MALLOC_DEFINE(M_WTAP_RXBUF, "wtap rxbuf",
+    "wtap wireless simulator recieve buffer");
+MALLOC_DEFINE(M_WTAP_PLUGIN, "wtap plugin", "wtap wireless simulator plugin");
+
+static struct wtap_hal		*hal;
+
+/* Function prototypes */
+static d_ioctl_t	wtap_ioctl;
+
+static struct cdev *sdev;
+static struct cdevsw wtap_cdevsw = {
+	.d_version =	D_VERSION,
+	.d_flags =	0,
+	.d_ioctl =	wtap_ioctl,
+	.d_name =	"wtapctl",
+};
+
+int
+wtap_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
+    int fflag, struct thread *td)
+{
+	int error = 0;
+
+	CURVNET_SET(CRED_TO_VNET(curthread->td_ucred));
+
+	switch(cmd) {
+	case WTAPIOCTLCRT:
+		if(new_wtap(hal, *(int *)data))
+			error = EINVAL;
+		break;
+	case WTAPIOCTLDEL:
+		if(free_wtap(hal, *(int *)data))
+			error = EINVAL;
+		break;
+	default:
+		DWTAP_PRINTF("Unkown WTAP IOCTL\n");
+		error = EINVAL;
+	}
+
+	CURVNET_RESTORE();
+	return error;
+}
+
+
+/* The function called at load/unload. */
+static int
+event_handler(module_t module, int event, void *arg) 
+{
+	struct visibility_plugin *plugin;
+	int e = 0; /* Error, 0 for normal return status */
+
+	switch (event) {
+	case MOD_LOAD:
+		sdev = make_dev(&wtap_cdevsw,0,UID_ROOT,
+		    GID_WHEEL,0600,(const char *)"wtapctl");
+		hal = (struct wtap_hal *)malloc(sizeof(struct wtap_hal),
+		    M_WTAP, M_NOWAIT | M_ZERO);
+		bzero(hal, sizeof(struct wtap_hal));
+
+		init_hal(hal);
+
+		/* Setting up a simple plugin */
+		plugin = (struct visibility_plugin *)malloc
+		    (sizeof(struct visibility_plugin), M_WTAP_PLUGIN,
+		    M_NOWAIT | M_ZERO);
+		plugin->base.wp_hal  = hal;
+		plugin->base.init = visibility_init;
+		plugin->base.deinit = visibility_deinit;
+		plugin->base.work = visibility_work;
+		register_plugin(hal, (struct wtap_plugin *)plugin);
+
+                printf("Loaded wtap wireless simulator\n");
+                break;
+	case MOD_UNLOAD:
+		destroy_dev(sdev);
+		deregister_plugin(hal);
+		deinit_hal(hal);
+		free(hal, M_WTAP);
+		printf("Unloading wtap wireless simulator\n");
+		break;
+	default:
+		e = EOPNOTSUPP; /* Error, Operation Not Supported */
+		break;
+	}
+
+	return(e);
+}
+
+/* The second argument of DECLARE_MODULE. */
+static moduledata_t wtap_conf = {
+	"wtap",		/* module name */
+	event_handler,	/* event handler */
+	NULL		/* extra data */
+};
+
+DECLARE_MODULE(wtap, wtap_conf, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/wtap/if_wtapioctl.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/sys/dev/wtap/if_wtapioctl.h	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,180 @@
+/*-
+ * Copyright (c) 2010-2011 Monthadar Al Jaberi, TerraNet AB
+ * All rights reserved.
+ *
+ * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer,
+ *    without modification.
+ * 2. Redistributions in binary form must reproduce at minimum a disclaimer
+ *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
+ *    redistribution must be conditioned upon including a substantially
+ *    similar Disclaimer requirement for further binary redistribution.
+ *
+ * NO WARRANTY
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
+ * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
+ * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
+ * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGES.
+ *
+ * $FreeBSD: head/sys/dev/wtap/if_wtapioctl.h 229970 2012-01-11 17:41:14Z adrian $
+ */
+
+/*
+ * Ioctl-related defintions for the Wireless TAP
+ * based on Atheros Wireless LAN controller driver.
+ */
+
+#ifndef _DEV_WTAP_WTAPIOCTL_H
+#define _DEV_WTAP_WTAPIOCTL_H
+
+#include <sys/param.h>
+#include <net80211/ieee80211_radiotap.h>
+
+#define	SIOCGATHSTATS	_IOWR('i', 137, struct ifreq)
+#define	SIOCZATHSTATS	_IOWR('i', 139, struct ifreq)
+
+#define WTAPIOCTLCRT	_IOW('W', 1, int)
+#define WTAPIOCTLDEL	_IOW('W', 2, int)
+
+struct wtap_stats {
+	u_int32_t	ast_watchdog;	/* device reset by watchdog */
+	u_int32_t	ast_hardware;	/* fatal hardware error interrupts */
+	u_int32_t	ast_bmiss;	/* beacon miss interrupts */
+	u_int32_t	ast_bmiss_phantom;/* beacon miss interrupts */
+	u_int32_t	ast_bstuck;	/* beacon stuck interrupts */
+	u_int32_t	ast_rxorn;	/* rx overrun interrupts */
+	u_int32_t	ast_rxeol;	/* rx eol interrupts */
+	u_int32_t	ast_txurn;	/* tx underrun interrupts */
+	u_int32_t	ast_mib;	/* mib interrupts */
+	u_int32_t	ast_intrcoal;	/* interrupts coalesced */
+	u_int32_t	ast_tx_packets;	/* packet sent on the interface */
+	u_int32_t	ast_tx_mgmt;	/* management frames transmitted */
+	u_int32_t	ast_tx_discard;	/* frames discarded prior to assoc */
+	u_int32_t	ast_tx_qstop;	/* output stopped 'cuz no buffer */
+	u_int32_t	ast_tx_encap;	/* tx encapsulation failed */
+	u_int32_t	ast_tx_nonode;	/* tx failed 'cuz no node */
+	u_int32_t	ast_tx_nombuf;	/* tx failed 'cuz no mbuf */
+	u_int32_t	ast_tx_nomcl;	/* tx failed 'cuz no cluster */
+	u_int32_t	ast_tx_linear;	/* tx linearized to cluster */
+	u_int32_t	ast_tx_nodata;	/* tx discarded empty frame */
+	u_int32_t	ast_tx_busdma;	/* tx failed for dma resrcs */
+	u_int32_t	ast_tx_xretries;/* tx failed 'cuz too many retries */
+	u_int32_t	ast_tx_fifoerr;	/* tx failed 'cuz FIFO underrun */
+	u_int32_t	ast_tx_filtered;/* tx failed 'cuz xmit filtered */
+	u_int32_t	ast_tx_shortretry;/* tx on-chip retries (short) */
+	u_int32_t	ast_tx_longretry;/* tx on-chip retries (long) */
+	u_int32_t	ast_tx_badrate;	/* tx failed 'cuz bogus xmit rate */
+	u_int32_t	ast_tx_noack;	/* tx frames with no ack marked */
+	u_int32_t	ast_tx_rts;	/* tx frames with rts enabled */
+	u_int32_t	ast_tx_cts;	/* tx frames with cts enabled */
+	u_int32_t	ast_tx_shortpre;/* tx frames with short preamble */
+	u_int32_t	ast_tx_altrate;	/* tx frames with alternate rate */
+	u_int32_t	ast_tx_protect;	/* tx frames with protection */
+	u_int32_t	ast_tx_ctsburst;/* tx frames with cts and bursting */
+	u_int32_t	ast_tx_ctsext;	/* tx frames with cts extension */
+	u_int32_t	ast_rx_nombuf;	/* rx setup failed 'cuz no mbuf */
+	u_int32_t	ast_rx_busdma;	/* rx setup failed for dma resrcs */
+	u_int32_t	ast_rx_orn;	/* rx failed 'cuz of desc overrun */
+	u_int32_t	ast_rx_crcerr;	/* rx failed 'cuz of bad CRC */
+	u_int32_t	ast_rx_fifoerr;	/* rx failed 'cuz of FIFO overrun */
+	u_int32_t	ast_rx_badcrypt;/* rx failed 'cuz decryption */
+	u_int32_t	ast_rx_badmic;	/* rx failed 'cuz MIC failure */
+	u_int32_t	ast_rx_phyerr;	/* rx failed 'cuz of PHY err */
+	u_int32_t	ast_rx_phy[32];	/* rx PHY error per-code counts */
+	u_int32_t	ast_rx_tooshort;/* rx discarded 'cuz frame too short */
+	u_int32_t	ast_rx_toobig;	/* rx discarded 'cuz frame too large */
+	u_int32_t	ast_rx_packets;	/* packet recv on the interface */
+	u_int32_t	ast_rx_mgt;	/* management frames received */
+	u_int32_t	ast_rx_ctl;	/* rx discarded 'cuz ctl frame */
+	int8_t		ast_tx_rssi;	/* tx rssi of last ack */
+	int8_t		ast_rx_rssi;	/* rx rssi from histogram */
+	u_int8_t	ast_tx_rate;	/* IEEE rate of last unicast tx */
+	u_int32_t	ast_be_xmit;	/* beacons transmitted */
+	u_int32_t	ast_be_nombuf;	/* beacon setup failed 'cuz no mbuf */
+	u_int32_t	ast_per_cal;	/* periodic calibration calls */
+	u_int32_t	ast_per_calfail;/* periodic calibration failed */
+	u_int32_t	ast_per_rfgain;	/* periodic calibration rfgain reset */
+	u_int32_t	ast_rate_calls;	/* rate control checks */
+	u_int32_t	ast_rate_raise;	/* rate control raised xmit rate */
+	u_int32_t	ast_rate_drop;	/* rate control dropped xmit rate */
+	u_int32_t	ast_ant_defswitch;/* rx/default antenna switches */
+	u_int32_t	ast_ant_txswitch;/* tx antenna switches */
+	u_int32_t	ast_ant_rx[8];	/* rx frames with antenna */
+	u_int32_t	ast_ant_tx[8];	/* tx frames with antenna */
+	u_int32_t	ast_cabq_xmit;	/* cabq frames transmitted */
+	u_int32_t	ast_cabq_busy;	/* cabq found busy */
+	u_int32_t	ast_tx_raw;	/* tx frames through raw api */
+	u_int32_t	ast_ff_txok;	/* fast frames tx'd successfully */
+	u_int32_t	ast_ff_txerr;	/* fast frames tx'd w/ error */
+	u_int32_t	ast_ff_rx;	/* fast frames rx'd */
+	u_int32_t	ast_ff_flush;	/* fast frames flushed from staging q */
+	u_int32_t	ast_tx_qfull;	/* tx dropped 'cuz of queue limit */
+	int8_t		ast_rx_noise;	/* rx noise floor */
+	u_int32_t	ast_tx_nobuf;	/* tx dropped 'cuz no ath buffer */
+	u_int32_t	ast_tdma_update;/* TDMA slot timing updates */
+	u_int32_t	ast_tdma_timers;/* TDMA slot update set beacon timers */
+	u_int32_t	ast_tdma_tsf;	/* TDMA slot update set TSF */
+	u_int16_t	ast_tdma_tsfadjp;/* TDMA slot adjust+ (usec, smoothed)*/
+	u_int16_t	ast_tdma_tsfadjm;/* TDMA slot adjust- (usec, smoothed)*/
+	u_int32_t	ast_tdma_ack;	/* TDMA tx failed 'cuz ACK required */
+	u_int32_t	ast_tx_raw_fail;/* raw tx failed 'cuz h/w down */
+	u_int32_t	ast_tx_nofrag;	/* tx dropped 'cuz no ath frag buffer */
+	u_int32_t	ast_be_missed;	/* missed beacons */
+	u_int32_t	ast_pad[13];
+};
+
+/*
+ * Radio capture format.
+ */
+#define WTAP_RX_RADIOTAP_PRESENT (		\
+	0)
+
+struct wtap_rx_radiotap_header {
+	struct ieee80211_radiotap_header wr_ihdr;
+#if 0
+	u_int64_t	wr_tsf;
+	u_int8_t	wr_flags;
+	u_int8_t	wr_rate;
+	int8_t		wr_antsignal;
+	int8_t		wr_antnoise;
+	u_int8_t	wr_antenna;
+	u_int8_t	wr_pad[3];
+	u_int32_t	wr_chan_flags;
+	u_int16_t	wr_chan_freq;
+	u_int8_t	wr_chan_ieee;
+	int8_t		wr_chan_maxpow;
+#endif
+} __packed;
+
+#define WTAP_TX_RADIOTAP_PRESENT (		\
+	0)
+
+struct wtap_tx_radiotap_header {
+	struct ieee80211_radiotap_header wt_ihdr;
+#if 0
+	u_int64_t	wt_tsf;
+	u_int8_t	wt_flags;
+	u_int8_t	wt_rate;
+	u_int8_t	wt_txpower;
+	u_int8_t	wt_antenna;
+	u_int32_t	wt_chan_flags;
+	u_int16_t	wt_chan_freq;
+	u_int8_t	wt_chan_ieee;
+	int8_t		wt_chan_maxpow;
+#endif
+} __packed;
+
+#endif
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/wtap/if_wtapvar.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/sys/dev/wtap/if_wtapvar.h	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,163 @@
+/*-
+ * Copyright (c) 2010-2011 Monthadar Al Jaberi, TerraNet AB
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer,
+ *    without modification.
+ * 2. Redistributions in binary form must reproduce at minimum a disclaimer
+ *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
+ *    redistribution must be conditioned upon including a substantially
+ *    similar Disclaimer requirement for further binary redistribution.
+ *
+ * NO WARRANTY
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
+ * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
+ * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
+ * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGES.
+ *
+ * $FreeBSD: head/sys/dev/wtap/if_wtapvar.h 229970 2012-01-11 17:41:14Z adrian $
+ */
+
+#ifndef _DEV_WTAP_WTAPVAR_H
+#define _DEV_WTAP_WTAPVAR_H
+
+#include <sys/cdefs.h>
+#include <sys/param.h>
+#include <sys/conf.h>
+#include <sys/module.h>
+#include <sys/kernel.h>
+#include <sys/systm.h>
+#include <sys/sysctl.h>
+#include <sys/mbuf.h>
+#include <sys/malloc.h>
+#include <sys/lock.h>
+#include <sys/mutex.h>
+
+#include <sys/types.h>
+#include <sys/sockio.h>
+#include <sys/socket.h>
+#include <sys/socketvar.h>
+#include <sys/errno.h>
+#include <sys/callout.h>
+#include <sys/endian.h>
+#include <sys/kthread.h>
+#include <sys/taskqueue.h>
+#include <sys/priv.h>
+#include <sys/sysctl.h>
+
+#include <machine/bus.h>
+
+#include <net/if.h>
+#include <net/if_dl.h>
+#include <net/if_media.h>
+#include <net/if_types.h>
+#include <net/if_arp.h>
+#include <net/ethernet.h>
+#include <net/if_llc.h>
+
+#include <net80211/ieee80211_var.h>
+#include <net80211/ieee80211_regdomain.h>
+
+#include <net/bpf.h>
+
+#include <net/vnet.h>
+
+#include <netinet/in.h>
+#include <netinet/if_ether.h>
+
+#if 0
+#define DWTAP_PRINTF(...) printf(__VA_ARGS__)
+#else
+#define DWTAP_PRINTF(...)
+#endif
+
+#include "if_wtapioctl.h"
+
+#define MAX_NBR_WTAP (64)
+#define BEACON_INTRERVAL (1000)
+
+MALLOC_DECLARE(M_WTAP);
+MALLOC_DECLARE(M_WTAP_PACKET);
+MALLOC_DECLARE(M_WTAP_BEACON);
+MALLOC_DECLARE(M_WTAP_RXBUF);
+MALLOC_DECLARE(M_WTAP_PLUGIN);
+
+/* driver-specific node state */
+struct wtap_node {
+	struct ieee80211_node an_node;	/* base class */
+	/* future addons */
+};
+#define	WTAP_NODE(ni)	((struct ath_node *)(ni))
+#define	WTAP_NODE_CONST(ni)	((const struct ath_node *)(ni))
+
+struct wtap_buf {
+	STAILQ_ENTRY(wtap_buf)	bf_list;
+	struct mbuf		*m;	/* mbuf for buf */
+};
+typedef STAILQ_HEAD(, wtap_buf) wtap_bufhead;
+
+#define	WTAP_BUF_BUSY 0x00000002	/* (tx) desc owned by h/w */
+
+struct wtap_vap {
+	struct ieee80211vap av_vap;		/* base class */
+	int32_t			id;		/* wtap id */
+	struct cdev 		*av_dev;	/* userspace injecting frames */
+	struct wtap_medium	*av_md;		/* back pointer */
+	struct mbuf *beacon;			/* beacon */
+	struct ieee80211_node	*bf_node;	/* pointer to the node */
+	struct ieee80211_beacon_offsets av_boff;/* dynamic update state */
+	struct callout		av_swba;	/* software beacon alert */
+	uint32_t		av_bcinterval;	/* beacon interval */
+	void (*av_recv_mgmt)(struct ieee80211_node *,
+	    struct mbuf *, int, int, int);
+	int (*av_newstate)(struct ieee80211vap *,
+	    enum ieee80211_state, int);
+	void (*av_bmiss)(struct ieee80211vap *);
+};
+#define	WTAP_VAP(vap)	((struct wtap_vap *)(vap))
+
+struct taskqueue;
+
+struct wtap_softc {
+	int32_t			id;
+	int32_t			up;
+	struct ifnet		*sc_ifp;	/* interface common */
+	struct wtap_medium	*sc_md;		/* interface medium */
+	struct ieee80211_node*	(* sc_node_alloc)
+	    (struct ieee80211vap *, const uint8_t [IEEE80211_ADDR_LEN]);
+	void (*sc_node_free)(struct ieee80211_node *);
+	int (*if_output)			/* output routine (enqueue) */
+	    (struct ifnet *, struct mbuf *, struct sockaddr *, struct route *);
+	void (*if_input) (struct ifnet *, struct mbuf *);/* from h/w driver */
+	int (*if_transmit)(struct ifnet *, struct mbuf *);/* output routine */
+	struct mtx		sc_mtx;		/* master lock (recursive) */
+	struct taskqueue	*sc_tq;		/* private task queue */
+	wtap_bufhead		sc_rxbuf;	/* receive buffer */
+	struct task		sc_rxtask;	/* rx int processing */
+	struct wtap_tx_radiotap_header sc_tx_th;
+	int			sc_tx_th_len;
+	struct wtap_rx_radiotap_header sc_rx_th;
+	int			sc_rx_th_len;
+};
+
+int32_t	wtap_attach(struct wtap_softc *, const uint8_t *macaddr);
+int32_t	wtap_detach(struct wtap_softc *);
+void	wtap_resume(struct wtap_softc *);
+void	wtap_suspend(struct wtap_softc *);
+void	wtap_shutdown(struct wtap_softc *);
+void	wtap_intr(struct wtap_softc *);
+void	wtap_inject(struct wtap_softc *, struct mbuf *);
+void	wtap_rx_deliver(struct wtap_softc *, struct mbuf *);
+
+#endif
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/wtap/plugins/visibility.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/sys/dev/wtap/plugins/visibility.c	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,241 @@
+/*-
+ * Copyright (c) 2010-2011 Monthadar Al Jaberi, TerraNet AB
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer,
+ *    without modification.
+ * 2. Redistributions in binary form must reproduce at minimum a disclaimer
+ *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
+ *    redistribution must be conditioned upon including a substantially
+ *    similar Disclaimer requirement for further binary redistribution.
+ *
+ * NO WARRANTY
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
+ * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
+ * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
+ * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGES.
+ *
+ * $FreeBSD: head/sys/dev/wtap/plugins/visibility.c 229970 2012-01-11 17:41:14Z adrian $
+ */
+#include <sys/param.h>
+#include <sys/module.h>
+#include <sys/kernel.h>
+#include <sys/systm.h>
+#include <sys/sysctl.h>
+#include <sys/mbuf.h>
+#include <sys/malloc.h>
+#include <sys/lock.h>
+#include <sys/mutex.h>
+#include <sys/proc.h>
+#include <sys/ucred.h>
+#include <sys/jail.h>
+
+#include <sys/types.h>
+#include <sys/sockio.h>
+#include <sys/socket.h>
+#include <sys/socketvar.h>
+#include <sys/errno.h>
+#include <sys/callout.h>
+#include <sys/endian.h>
+#include <sys/kthread.h>
+#include <sys/taskqueue.h>
+#include <sys/priv.h>
+#include <sys/sysctl.h>
+
+#include <machine/bus.h>
+
+#include <net/if.h>
+#include <net/if_dl.h>
+#include <net/if_media.h>
+#include <net/if_types.h>
+#include <net/if_arp.h>
+#include <net/ethernet.h>
+#include <net/if_llc.h>
+#include <net/vnet.h>
+
+#include <net80211/ieee80211_var.h>
+#include <net80211/ieee80211_regdomain.h>
+
+#include <net/bpf.h>
+
+
+#include <sys/errno.h>
+#include <sys/conf.h>   /* cdevsw struct */
+#include <sys/uio.h>    /* uio struct */
+
+#include <netinet/in.h>
+#include <netinet/if_ether.h>
+
+#include "visibility.h"
+
+/* Function prototypes */
+static d_ioctl_t	vis_ioctl;
+
+static struct cdevsw vis_cdevsw = {
+	.d_version =	D_VERSION,
+	.d_flags =	0,
+	.d_ioctl =	vis_ioctl,
+	.d_name =	"visctl",
+};
+
+void
+visibility_init(struct wtap_plugin *plugin)
+{
+	struct visibility_plugin *vis_plugin;
+
+	vis_plugin = (struct visibility_plugin *) plugin;
+	plugin->wp_sdev = make_dev(&vis_cdevsw,0,UID_ROOT,GID_WHEEL,0600,
+	    (const char *)"visctl");
+	plugin->wp_sdev->si_drv1 = vis_plugin;
+	mtx_init(&vis_plugin->pl_mtx, "visibility_plugin mtx",
+	    NULL, MTX_DEF | MTX_RECURSE);
+	printf("Using visibility wtap plugin...\n");
+}
+
+void
+visibility_deinit(struct wtap_plugin *plugin)
+{
+	struct visibility_plugin *vis_plugin;
+
+	vis_plugin = (struct visibility_plugin *) plugin;
+	destroy_dev(plugin->wp_sdev);
+	mtx_destroy(&vis_plugin->pl_mtx);
+	free(vis_plugin, M_WTAP_PLUGIN);
+	printf("Removing visibility wtap plugin...\n");
+}
+
+/* We need to use a mutex lock when we read out a visibility map
+ * and when we change visibility map from user space through IOCTL
+ */
+void
+visibility_work(struct wtap_plugin *plugin, struct packet *p)
+{
+	struct visibility_plugin *vis_plugin =
+	    (struct visibility_plugin *) plugin;
+	struct wtap_hal *hal = (struct wtap_hal *)vis_plugin->base.wp_hal;
+	struct vis_map *map;
+
+	KASSERT(mtod(p->m, const char *) != (const char *) 0xdeadc0de ||
+	    mtod(p->m, const char *) != NULL,
+	    ("[%s] got a corrupt packet from master queue, p->m=%p, p->id=%d\n",
+	    __func__, p->m, p->id));
+	DWTAP_PRINTF("[%d] BROADCASTING m=%p\n", p->id, p->m);
+	mtx_lock(&vis_plugin->pl_mtx);
+	map = &vis_plugin->pl_node[p->id];
+	mtx_unlock(&vis_plugin->pl_mtx);
+
+	/* This is O(n*n) which is not optimal for large
+	 * number of nodes. Another way of doing it is
+	 * creating groups of nodes that hear each other.
+	 * Atleast for this simple static node plugin.
+	 */
+	for(int i=0; i<ARRAY_SIZE; ++i){
+		uint32_t index = map->map[i];
+		for(int j=0; j<32; ++j){
+			int vis = index & 0x01;
+			if(vis){
+				int k = i*ARRAY_SIZE + j;
+				if(hal->hal_devs[k] != NULL
+				    && hal->hal_devs[k]->up == 1){
+					struct wtap_softc *sc =
+					    hal->hal_devs[k];
+					struct mbuf *m =
+					    m_dup(p->m, M_DONTWAIT);
+					DWTAP_PRINTF("[%d] duplicated old_m=%p"
+					    "to new_m=%p\n", p->id, p->m, m);
+#if 0
+					printf("[%d] sending to %d\n",
+					    p->id, k);
+#endif
+					wtap_inject(sc, m);
+				}
+			}
+			index = index >> 1;
+		}
+	}
+}
+
+static void
+add_link(struct visibility_plugin *vis_plugin, struct link *l)
+{
+
+	mtx_lock(&vis_plugin->pl_mtx);
+	struct vis_map *map = &vis_plugin->pl_node[l->id1];
+	int index = l->id2/ARRAY_SIZE;
+	int bit = l->id2 % ARRAY_SIZE;
+	uint32_t value = 1 << bit;
+	map->map[index] = map->map[index] | value;
+	mtx_unlock(&vis_plugin->pl_mtx);
+#if 0
+	printf("l->id1=%d, l->id2=%d, map->map[%d] = %u, bit=%d\n",
+	    l->id1, l->id2, index, map->map[index], bit);
+#endif
+}
+
+static void
+del_link(struct visibility_plugin *vis_plugin, struct link *l)
+{
+
+	mtx_lock(&vis_plugin->pl_mtx);
+	struct vis_map *map = &vis_plugin->pl_node[l->id1];
+	int index = l->id2/ARRAY_SIZE;
+	int bit = l->id2 % ARRAY_SIZE;
+	uint32_t value = 1 << bit;
+	map->map[index] = map->map[index] & ~value;
+	mtx_unlock(&vis_plugin->pl_mtx);
+#if 0
+	printf("map->map[index] = %u\n", map->map[index]);
+#endif
+}
+
+
+int
+vis_ioctl(struct cdev *sdev, u_long cmd, caddr_t data,
+    int fflag, struct thread *td)
+{
+	struct visibility_plugin *vis_plugin =
+	    (struct visibility_plugin *) sdev->si_drv1;
+	struct wtap_hal *hal = vis_plugin->base.wp_hal;
+	struct link l;
+	int op;
+	int error = 0;
+
+	CURVNET_SET(CRED_TO_VNET(curthread->td_ucred));
+	switch(cmd) {
+	case VISIOCTLOPEN:
+		op =  *(int *)data; 
+		if(op == 0)
+			medium_close(hal->hal_md);
+		else
+			medium_open(hal->hal_md);
+		break;
+	case VISIOCTLLINK:
+		l = *(struct link *)data;
+		if(l.op == 0)
+			del_link(vis_plugin, &l);
+		else
+			add_link(vis_plugin, &l);
+#if 0
+		printf("op=%d, id1=%d, id2=%d\n", l.op, l.id1, l.id2);
+#endif
+		break;
+	default:
+		DWTAP_PRINTF("Unkown WTAP IOCTL\n");
+		error = EINVAL;
+	}
+
+	CURVNET_RESTORE();
+	return error;
+}
+
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/wtap/plugins/visibility.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/sys/dev/wtap/plugins/visibility.h	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,56 @@
+/*-
+ * Copyright (c) 2011 Monthadar Al Jaberi, TerraNet AB
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer,
+ *    without modification.
+ * 2. Redistributions in binary form must reproduce at minimum a disclaimer
+ *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
+ *    redistribution must be conditioned upon including a substantially
+ *    similar Disclaimer requirement for further binary redistribution.
+ *
+ * NO WARRANTY
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
+ * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
+ * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
+ * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGES.
+ *
+ * $FreeBSD: head/sys/dev/wtap/plugins/visibility.h 229970 2012-01-11 17:41:14Z adrian $
+ */
+#ifndef _VISIBILITY_H_
+#define _VISIBILITY_H_
+
+#include "wtap_plugin.h"
+#include "visibility_ioctl.h"
+
+#define CDEV_GET_SOFTC(x) (x)->si_drv1
+
+#define ARRAY_SIZE (32) //We support a maximum of 1024 nodes for now
+
+struct vis_map {
+	uint32_t map[ARRAY_SIZE]; 
+};
+
+struct visibility_plugin {
+	struct wtap_plugin	base;
+	struct mtx		pl_mtx;
+	struct vis_map pl_node[MAX_NBR_WTAP];
+};
+
+void visibility_init(struct wtap_plugin *);
+void visibility_deinit(struct wtap_plugin *);
+void visibility_work(struct wtap_plugin *, struct packet *);
+
+#endif
+
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/wtap/plugins/visibility_ioctl.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/sys/dev/wtap/plugins/visibility_ioctl.h	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,51 @@
+/*-
+ * Copyright (c) 2011 Monthadar Al Jaberi, TerraNet AB
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer,
+ *    without modification.
+ * 2. Redistributions in binary form must reproduce at minimum a disclaimer
+ *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
+ *    redistribution must be conditioned upon including a substantially
+ *    similar Disclaimer requirement for further binary redistribution.
+ *
+ * NO WARRANTY
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
+ * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
+ * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
+ * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGES.
+ *
+ * $FreeBSD: head/sys/dev/wtap/plugins/visibility_ioctl.h 229970 2012-01-11 17:41:14Z adrian $
+ */
+
+/*
+ * Ioctl-related defintions for the Wireless TAP plugins.
+ */
+
+#ifndef _VISIBILITY_IOCTL_H
+#define _VISIBILITY_IOCTL_H
+
+#include <sys/param.h>
+
+struct link {
+      int	op; //0 remove, 1 link
+      int 	id1;
+      int	id2;
+};
+
+#define VISIOCTLOPEN _IOW('W', 3, int) // 0 close, 1 open
+#define VISIOCTLLINK _IOW('W', 4, struct link) //
+
+#endif
+
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/wtap/plugins/wtap_plugin.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/sys/dev/wtap/plugins/wtap_plugin.h	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,48 @@
+/*-
+ * Copyright (c) 2011 Monthadar Al Jaberi, TerraNet AB
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer,
+ *    without modification.
+ * 2. Redistributions in binary form must reproduce at minimum a disclaimer
+ *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
+ *    redistribution must be conditioned upon including a substantially
+ *    similar Disclaimer requirement for further binary redistribution.
+ *
+ * NO WARRANTY
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
+ * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
+ * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
+ * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGES.
+ *
+ * $FreeBSD: head/sys/dev/wtap/plugins/wtap_plugin.h 229970 2012-01-11 17:41:14Z adrian $
+ */
+#ifndef _WTAP_PLUGIN_H_
+#define _WTAP_PLUGIN_H_
+
+#include "../if_wtapvar.h"
+#include "../if_medium.h"
+#include "../wtap_hal/hal.h"
+
+
+struct wtap_plugin {
+	struct cdev	*wp_sdev;
+	struct wtap_hal	*wp_hal;
+	void (*init)(struct wtap_plugin *);
+	void (*deinit)(struct wtap_plugin *);
+	void (*work)(struct wtap_plugin *, struct packet *p);
+};
+
+#endif
+
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/wtap/wtap_hal/hal.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/sys/dev/wtap/wtap_hal/hal.c	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,213 @@
+/*-
+ * Copyright (c) 2010-2011 Monthadar Al Jaberi, TerraNet AB
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer,
+ *    without modification.
+ * 2. Redistributions in binary form must reproduce at minimum a disclaimer
+ *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
+ *    redistribution must be conditioned upon including a substantially
+ *    similar Disclaimer requirement for further binary redistribution.
+ *
+ * NO WARRANTY
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
+ * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
+ * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
+ * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGES.
+ *
+ * $FreeBSD: head/sys/dev/wtap/wtap_hal/hal.c 229970 2012-01-11 17:41:14Z adrian $
+ */
+#include "hal.h"
+#include "../if_medium.h"
+#include "handler.h"
+
+static void
+hal_tx_proc(void *arg, int npending)
+{
+	struct wtap_hal *hal = (struct wtap_hal *)arg;
+	struct packet *p;
+
+#if 0
+	DWTAP_PRINTF("%s\n", __func__);
+#endif
+
+	hal = (struct wtap_hal *)arg;
+	for(;;){
+		p = medium_get_next_packet(hal->hal_md);
+		if(p == NULL)
+		return;
+
+		hal->plugin->work(hal->plugin, p);
+
+#if 0
+		DWTAP_PRINTF("[%d] freeing m=%p\n", p->id, p->m);
+#endif
+		m_free(p->m);
+		free(p, M_WTAP_PACKET);
+	}
+}
+
+void
+init_hal(struct wtap_hal *hal)
+{
+
+	DWTAP_PRINTF("%s\n", __func__);
+	mtx_init(&hal->hal_mtx, "wtap_hal mtx", NULL, MTX_DEF | MTX_RECURSE);
+
+	hal->hal_md = (struct wtap_medium *)malloc(sizeof(struct wtap_medium),
+	    M_WTAP, M_NOWAIT | M_ZERO);
+	bzero(hal->hal_md, sizeof(struct wtap_medium));
+
+	init_medium(hal->hal_md);
+	/* register event handler for packets */
+	TASK_INIT(&hal->hal_md->tx_handler->proc, 0, hal_tx_proc, hal);
+}
+
+void
+register_plugin(struct wtap_hal *hal, struct wtap_plugin *plugin)
+{
+
+	plugin->init(plugin);
+	hal->plugin = plugin;
+}
+
+void
+deregister_plugin(struct wtap_hal *hal)
+{
+
+	hal->plugin->deinit(hal->plugin);
+	hal->plugin = NULL; /* catch illegal usages */
+}
+
+void
+deinit_hal(struct wtap_hal *hal)
+{
+
+	DWTAP_PRINTF("%s\n", __func__);
+	deinit_medium(hal->hal_md);
+	free(hal->hal_md, M_WTAP);
+	mtx_destroy(&hal->hal_mtx);
+}
+
+int32_t
+new_wtap(struct wtap_hal *hal, int32_t id)
+{
+	static const uint8_t mac_pool[64][IEEE80211_ADDR_LEN] = {
+	    {0,152,154,152,150,151},
+	    {0,152,154,152,150,152},
+	    {0,152,154,152,150,153},
+	    {0,152,154,152,150,154},
+	    {0,152,154,152,150,155},
+	    {0,152,154,152,150,156},
+	    {0,152,154,152,150,157},
+	    {0,152,154,152,150,158},
+	    {0,152,154,152,151,151},
+	    {0,152,154,152,151,152},
+	    {0,152,154,152,151,153},
+	    {0,152,154,152,151,154},
+	    {0,152,154,152,151,155},
+	    {0,152,154,152,151,156},
+	    {0,152,154,152,151,157},
+	    {0,152,154,152,151,158},
+	    {0,152,154,152,152,151},
+	    {0,152,154,152,152,152},
+	    {0,152,154,152,152,153},
+	    {0,152,154,152,152,154},
+	    {0,152,154,152,152,155},
+	    {0,152,154,152,152,156},
+	    {0,152,154,152,152,157},
+	    {0,152,154,152,152,158},
+	    {0,152,154,152,153,151},
+	    {0,152,154,152,153,152},
+	    {0,152,154,152,153,153},
+	    {0,152,154,152,153,154},
+	    {0,152,154,152,153,155},
+	    {0,152,154,152,153,156},
+	    {0,152,154,152,153,157},
+	    {0,152,154,152,153,158},
+	    {0,152,154,152,154,151},
+	    {0,152,154,152,154,152},
+	    {0,152,154,152,154,153},
+	    {0,152,154,152,154,154},
+	    {0,152,154,152,154,155},
+	    {0,152,154,152,154,156},
+	    {0,152,154,152,154,157},
+	    {0,152,154,152,154,158},
+	    {0,152,154,152,155,151},
+	    {0,152,154,152,155,152},
+	    {0,152,154,152,155,153},
+	    {0,152,154,152,155,154},
+	    {0,152,154,152,155,155},
+	    {0,152,154,152,155,156},
+	    {0,152,154,152,155,157},
+	    {0,152,154,152,155,158},
+	    {0,152,154,152,156,151},
+	    {0,152,154,152,156,152},
+	    {0,152,154,152,156,153},
+	    {0,152,154,152,156,154},
+	    {0,152,154,152,156,155},
+	    {0,152,154,152,156,156},
+	    {0,152,154,152,156,157},
+	    {0,152,154,152,156,158},
+	    {0,152,154,152,157,151},
+	    {0,152,154,152,157,152},
+	    {0,152,154,152,157,153},
+	    {0,152,154,152,157,154},
+	    {0,152,154,152,157,155},
+	    {0,152,154,152,157,156},
+	    {0,152,154,152,157,157},
+	    {0,152,154,152,157,158}
+	    };
+
+	DWTAP_PRINTF("%s\n", __func__);
+	uint8_t const *macaddr = mac_pool[id];
+	if(hal->hal_devs[id] != NULL){
+		printf("error, wtap_id=%d already created\n", id);
+		return -1;
+	}
+
+	hal->hal_devs[id] = (struct wtap_softc *)malloc(
+	    sizeof(struct wtap_softc), M_WTAP, M_NOWAIT | M_ZERO);
+	bzero(hal->hal_devs[id], sizeof(struct wtap_softc));
+	hal->hal_devs[id]->sc_md = hal->hal_md;
+	hal->hal_devs[id]->id = id;
+	mtx_init(&hal->hal_devs[id]->sc_mtx, "wtap_softc mtx", NULL,
+	    MTX_DEF | MTX_RECURSE);
+
+	if(wtap_attach(hal->hal_devs[id], macaddr)){
+		printf("%s, cant alloc new wtap\n", __func__);
+		return -1;
+	}
+
+	return 0;
+}
+
+int32_t
+free_wtap(struct wtap_hal *hal, int32_t id)
+{
+
+	DWTAP_PRINTF("%s\n", __func__);
+	if(hal->hal_devs[id] == NULL){
+		printf("error, wtap_id=%d never created\n", id);
+		return -1;
+	}
+
+	if(wtap_detach(hal->hal_devs[id]))
+		printf("%s, cant alloc new wtap\n", __func__);
+	mtx_destroy(&hal->hal_devs[id]->sc_mtx);
+	free(hal->hal_devs[id], M_WTAP);
+	hal->hal_devs[id] = NULL;
+	return 0;
+}
+
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/wtap/wtap_hal/hal.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/sys/dev/wtap/wtap_hal/hal.h	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,55 @@
+/*-
+ * Copyright (c) 2010-2011 Monthadar Al Jaberi, TerraNet AB
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer,
+ *    without modification.
+ * 2. Redistributions in binary form must reproduce at minimum a disclaimer
+ *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
+ *    redistribution must be conditioned upon including a substantially
+ *    similar Disclaimer requirement for further binary redistribution.
+ *
+ * NO WARRANTY
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
+ * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
+ * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
+ * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGES.
+ *
+ * $FreeBSD: head/sys/dev/wtap/wtap_hal/hal.h 229970 2012-01-11 17:41:14Z adrian $
+ */
+
+#ifndef _WTAP_HAL_H_
+#define _WTAP_HAL_H_
+
+#include "../if_wtapvar.h"
+#include "../plugins/wtap_plugin.h"
+#include "handler.h"
+
+struct wtap_hal {
+	struct wtap_medium	*hal_md;
+	struct mtx		hal_mtx;
+	struct wtap_plugin	*plugin;
+	struct wtap_softc 	*hal_devs[MAX_NBR_WTAP];
+};
+
+void init_hal(struct wtap_hal *);
+void deinit_hal(struct wtap_hal *);
+void register_plugin(struct wtap_hal *, struct wtap_plugin *);
+void deregister_plugin(struct wtap_hal *);
+int32_t new_wtap(struct wtap_hal *, int32_t id);
+int32_t free_wtap(struct wtap_hal *, int32_t id);
+
+
+#endif
+
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/wtap/wtap_hal/handler.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/sys/dev/wtap/wtap_hal/handler.h	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,50 @@
+/*-
+ * Copyright (c) 2010-2011 Monthadar Al Jaberi, TerraNet AB
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer,
+ *    without modification.
+ * 2. Redistributions in binary form must reproduce at minimum a disclaimer
+ *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
+ *    redistribution must be conditioned upon including a substantially
+ *    similar Disclaimer requirement for further binary redistribution.
+ *
+ * NO WARRANTY
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
+ * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
+ * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
+ * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGES.
+ *
+ * $FreeBSD: head/sys/dev/wtap/wtap_hal/handler.h 229970 2012-01-11 17:41:14Z adrian $
+ */
+
+#ifndef _WTAP_HANDLER_H_
+#define _WTAP_HANDLER_H_
+
+#include "../if_wtapvar.h"
+
+struct timehandler {
+	struct callout		cb;
+	void (*proc)(void *);
+};
+
+struct taskqueue;
+
+struct eventhandler {
+	struct taskqueue		*tq;
+	struct task		proc;
+};
+
+#endif
+
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/xen/netback/netback.c
--- a/head/sys/dev/xen/netback/netback.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/xen/netback/netback.c	Fri Jan 13 15:51:01 2012 +0200
@@ -29,7 +29,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/dev/xen/netback/netback.c 228471 2011-12-13 14:06:01Z ed $");
+__FBSDID("$FreeBSD: head/sys/dev/xen/netback/netback.c 229767 2012-01-07 09:41:57Z kevlo $");
 #include "opt_sctp.h"
 
 #include <sys/param.h>
@@ -375,7 +375,6 @@
 	ifp->if_output = ether_output;
 	ifp->if_start = netback_start;
 	ifp->if_ioctl = netback_ioctl;
-	ifp->if_mtu = ETHERMTU;
 	ifp->if_snd.ifq_maxlen = NET_TX_RING_SIZE - 1;
 	
 	DPRINTF("Created %s for domid=%d handle=%d\n", IFNAME(netif), netif->domid, netif->handle);
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/dev/xen/netfront/netfront.c
--- a/head/sys/dev/xen/netfront/netfront.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/dev/xen/netfront/netfront.c	Fri Jan 13 15:51:01 2012 +0200
@@ -26,7 +26,7 @@
 
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/dev/xen/netfront/netfront.c 225709 2011-09-21 00:15:29Z gibbs $");
+__FBSDID("$FreeBSD: head/sys/dev/xen/netfront/netfront.c 229767 2012-01-07 09:41:57Z kevlo $");
 
 #include "opt_inet.h"
 
@@ -2145,7 +2145,6 @@
     	ifp->if_watchdog = xn_watchdog;
 #endif
     	ifp->if_init = xn_ifinit;
-    	ifp->if_mtu = ETHERMTU;
     	ifp->if_snd.ifq_maxlen = NET_TX_RING_SIZE - 1;
 	
     	ifp->if_hwassist = XN_CSUM_FEATURES;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/fs/nfs/nfs_commonkrpc.c
--- a/head/sys/fs/nfs/nfs_commonkrpc.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/fs/nfs/nfs_commonkrpc.c	Fri Jan 13 15:51:01 2012 +0200
@@ -32,13 +32,12 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/fs/nfs/nfs_commonkrpc.c 228757 2011-12-21 02:45:51Z rmacklem $");
+__FBSDID("$FreeBSD: head/sys/fs/nfs/nfs_commonkrpc.c 229802 2012-01-08 01:54:46Z rmacklem $");
 
 /*
  * Socket operations for use by nfs
  */
 
-#include "opt_inet6.h"
 #include "opt_kdtrace.h"
 #include "opt_kgssapi.h"
 #include "opt_nfs.h"
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/fs/nfs/nfs_commonport.c
--- a/head/sys/fs/nfs/nfs_commonport.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/fs/nfs/nfs_commonport.c	Fri Jan 13 15:51:01 2012 +0200
@@ -32,7 +32,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/fs/nfs/nfs_commonport.c 224121 2011-07-17 03:44:05Z zack $");
+__FBSDID("$FreeBSD: head/sys/fs/nfs/nfs_commonport.c 229956 2012-01-11 02:46:42Z rmacklem $");
 
 /*
  * Functions that need to be different for different versions of BSD
@@ -483,7 +483,6 @@
 				newnfsstats.srvcache_nonidemdonehits = 0;
 				newnfsstats.srvcache_misses = 0;
 				newnfsstats.srvcache_tcppeak = 0;
-				newnfsstats.srvcache_size = 0;
 				newnfsstats.srvclients = 0;
 				newnfsstats.srvopenowners = 0;
 				newnfsstats.srvopens = 0;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/fs/nfs/nfs_commonsubs.c
--- a/head/sys/fs/nfs/nfs_commonsubs.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/fs/nfs/nfs_commonsubs.c	Fri Jan 13 15:51:01 2012 +0200
@@ -32,7 +32,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/fs/nfs/nfs_commonsubs.c 224121 2011-07-17 03:44:05Z zack $");
+__FBSDID("$FreeBSD: head/sys/fs/nfs/nfs_commonsubs.c 229802 2012-01-08 01:54:46Z rmacklem $");
 
 /*
  * These functions support the macros and help fiddle mbuf chains for
@@ -40,6 +40,8 @@
  * copy data between mbuf chains and uio lists.
  */
 #ifndef APPLEKEXT
+#include "opt_inet6.h"
+
 #include <fs/nfs/nfsport.h>
 
 /*
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/fs/nfsclient/nfs_clkrpc.c
--- a/head/sys/fs/nfsclient/nfs_clkrpc.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/fs/nfsclient/nfs_clkrpc.c	Fri Jan 13 15:51:01 2012 +0200
@@ -32,9 +32,8 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/fs/nfsclient/nfs_clkrpc.c 223309 2011-06-19 22:08:55Z rmacklem $");
+__FBSDID("$FreeBSD: head/sys/fs/nfsclient/nfs_clkrpc.c 229802 2012-01-08 01:54:46Z rmacklem $");
 
-#include "opt_inet6.h"
 #include "opt_kgssapi.h"
 
 #include <fs/nfs/nfsport.h>
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/fs/nfsclient/nfs_clport.c
--- a/head/sys/fs/nfsclient/nfs_clport.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/fs/nfsclient/nfs_clport.c	Fri Jan 13 15:51:01 2012 +0200
@@ -32,8 +32,9 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/fs/nfsclient/nfs_clport.c 224778 2011-08-11 12:30:23Z rwatson $");
+__FBSDID("$FreeBSD: head/sys/fs/nfsclient/nfs_clport.c 229802 2012-01-08 01:54:46Z rmacklem $");
 
+#include "opt_inet6.h"
 #include "opt_kdtrace.h"
 
 #include <sys/capability.h>
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/fs/nfsclient/nfs_clrpcops.c
--- a/head/sys/fs/nfsclient/nfs_clrpcops.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/fs/nfsclient/nfs_clrpcops.c	Fri Jan 13 15:51:01 2012 +0200
@@ -32,7 +32,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/fs/nfsclient/nfs_clrpcops.c 228827 2011-12-23 02:04:35Z rmacklem $");
+__FBSDID("$FreeBSD: head/sys/fs/nfsclient/nfs_clrpcops.c 229802 2012-01-08 01:54:46Z rmacklem $");
 
 /*
  * Rpc op calls, generally called from the vnode op calls or through the
@@ -43,6 +43,8 @@
  */
 
 #ifndef APPLEKEXT
+#include "opt_inet6.h"
+
 #include <fs/nfs/nfsport.h>
 
 /*
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/fs/nullfs/null_subr.c
--- a/head/sys/fs/nullfs/null_subr.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/fs/nullfs/null_subr.c	Fri Jan 13 15:51:01 2012 +0200
@@ -31,7 +31,7 @@
  *
  *	@(#)null_subr.c	8.7 (Berkeley) 5/14/95
  *
- * $FreeBSD: head/sys/fs/nullfs/null_subr.c 229431 2012-01-03 21:09:07Z kib $
+ * $FreeBSD: head/sys/fs/nullfs/null_subr.c 229600 2012-01-05 17:06:04Z dim $
  */
 
 #include <sys/param.h>
@@ -201,7 +201,7 @@
 	int error;
 
 	ASSERT_VOP_LOCKED(lowervp, "lowervp");
-	KASSERT(lowervp->v_usecount >= 1, ("Unreferenced vnode %p\n", vp));
+	KASSERT(lowervp->v_usecount >= 1, ("Unreferenced vnode %p\n", lowervp));
 
 	/* Lookup the hash firstly */
 	*vpp = null_hashget(mp, lowervp);
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/fs/pseudofs/pseudofs_vnops.c
--- a/head/sys/fs/pseudofs/pseudofs_vnops.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/fs/pseudofs/pseudofs_vnops.c	Fri Jan 13 15:51:01 2012 +0200
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/fs/pseudofs/pseudofs_vnops.c 227697 2011-11-19 07:50:49Z kib $");
+__FBSDID("$FreeBSD: head/sys/fs/pseudofs/pseudofs_vnops.c 229694 2012-01-06 10:12:59Z jh $");
 
 #include "opt_pseudofs.h"
 
@@ -630,14 +630,14 @@
 	if (uio->uio_offset < 0 || uio->uio_resid < 0 ||
 	    (offset = uio->uio_offset) != uio->uio_offset ||
 	    (resid = uio->uio_resid) != uio->uio_resid ||
-	    (buflen = offset + resid + 1) < offset || buflen > INT_MAX) {
+	    (buflen = offset + resid) < offset || buflen >= INT_MAX) {
 		error = EINVAL;
 		goto ret;
 	}
-	if (buflen > MAXPHYS + 1)
-		buflen = MAXPHYS + 1;
+	if (buflen > MAXPHYS)
+		buflen = MAXPHYS;
 
-	sb = sbuf_new(sb, NULL, buflen, 0);
+	sb = sbuf_new(sb, NULL, buflen + 1, 0);
 	if (sb == NULL) {
 		error = EIO;
 		goto ret;
@@ -650,8 +650,14 @@
 		goto ret;
 	}
 
-	sbuf_finish(sb);
-	error = uiomove_frombuf(sbuf_data(sb), sbuf_len(sb), uio);
+	/*
+	 * XXX: If the buffer overflowed, sbuf_len() will not return
+	 * the data length. Then just use the full length because an
+	 * overflowed sbuf must be full.
+	 */
+	if (sbuf_finish(sb) == 0)
+		buflen = sbuf_len(sb);
+	error = uiomove_frombuf(sbuf_data(sb), buflen, uio);
 	sbuf_delete(sb);
 ret:
 	vn_lock(vn, locked | LK_RETRY);
@@ -891,7 +897,11 @@
 		PFS_RETURN (error);
 	}
 
-	sbuf_finish(&sb);
+	if (sbuf_finish(&sb) != 0) {
+		sbuf_delete(&sb);
+		PFS_RETURN (ENAMETOOLONG);
+	}
+
 	error = uiomove_frombuf(sbuf_data(&sb), sbuf_len(&sb), uio);
 	sbuf_delete(&sb);
 	PFS_RETURN (error);
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/fs/tmpfs/tmpfs_subr.c
--- a/head/sys/fs/tmpfs/tmpfs_subr.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/fs/tmpfs/tmpfs_subr.c	Fri Jan 13 15:51:01 2012 +0200
@@ -34,7 +34,7 @@
  * Efficient memory file system supporting functions.
  */
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/fs/tmpfs/tmpfs_subr.c 223677 2011-06-29 16:40:41Z alc $");
+__FBSDID("$FreeBSD: head/sys/fs/tmpfs/tmpfs_subr.c 229821 2012-01-08 20:09:26Z alc $");
 
 #include <sys/param.h>
 #include <sys/namei.h>
@@ -48,6 +48,7 @@
 #include <vm/vm.h>
 #include <vm/vm_object.h>
 #include <vm/vm_page.h>
+#include <vm/vm_pageout.h>
 #include <vm/vm_pager.h>
 #include <vm/vm_extern.h>
 
@@ -886,10 +887,10 @@
 	struct tmpfs_mount *tmp;
 	struct tmpfs_node *node;
 	vm_object_t uobj;
-	vm_page_t m;
-	vm_pindex_t newpages, oldpages;
+	vm_page_t m, ma[1];
+	vm_pindex_t idx, newpages, oldpages;
 	off_t oldsize;
-	size_t zerolen;
+	int base, rv;
 
 	MPASS(vp->v_type == VREG);
 	MPASS(newsize >= 0);
@@ -912,15 +913,57 @@
 	    newpages - oldpages > TMPFS_PAGES_AVAIL(tmp))
 		return (ENOSPC);
 
-	TMPFS_LOCK(tmp);
-	tmp->tm_pages_used += (newpages - oldpages);
-	TMPFS_UNLOCK(tmp);
-
-	node->tn_size = newsize;
-	vnode_pager_setsize(vp, newsize);
 	VM_OBJECT_LOCK(uobj);
 	if (newsize < oldsize) {
 		/*
+		 * Zero the truncated part of the last page.
+		 */
+		base = newsize & PAGE_MASK;
+		if (base != 0) {
+			idx = OFF_TO_IDX(newsize);
+retry:
+			m = vm_page_lookup(uobj, idx);
+			if (m != NULL) {
+				if ((m->oflags & VPO_BUSY) != 0 ||
+				    m->busy != 0) {
+					vm_page_sleep(m, "tmfssz");
+					goto retry;
+				}
+			} else if (vm_pager_has_page(uobj, idx, NULL, NULL)) {
+				m = vm_page_alloc(uobj, idx, VM_ALLOC_NORMAL);
+				if (m == NULL) {
+					VM_OBJECT_UNLOCK(uobj);
+					VM_WAIT;
+					VM_OBJECT_LOCK(uobj);
+					goto retry;
+				} else if (m->valid != VM_PAGE_BITS_ALL) {
+					ma[0] = m;
+					rv = vm_pager_get_pages(uobj, ma, 1, 0);
+					m = vm_page_lookup(uobj, idx);
+				} else
+					/* A cached page was reactivated. */
+					rv = VM_PAGER_OK;
+				vm_page_lock(m);
+				if (rv == VM_PAGER_OK) {
+					vm_page_deactivate(m);
+					vm_page_unlock(m);
+					vm_page_wakeup(m);
+				} else {
+					vm_page_free(m);
+					vm_page_unlock(m);
+					VM_OBJECT_UNLOCK(uobj);
+					return (EIO);
+				}
+			}
+			if (m != NULL) {
+				pmap_zero_page_area(m, base, PAGE_SIZE - base);
+				MPASS(m->valid == VM_PAGE_BITS_ALL);
+				vm_page_dirty(m);
+				vm_pager_page_unswapped(m);
+			}
+		}
+
+		/*
 		 * Release any swap space and free any whole pages.
 		 */
 		if (newpages < oldpages) {
@@ -928,19 +971,16 @@
 			    newpages);
 			vm_object_page_remove(uobj, newpages, 0, 0);
 		}
-
-		/*
-		 * Zero the truncated part of the last page.
-		 */
-		zerolen = round_page(newsize) - newsize;
-		if (zerolen > 0) {
-			m = vm_page_grab(uobj, OFF_TO_IDX(newsize),
-			    VM_ALLOC_NOBUSY | VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
-			pmap_zero_page_area(m, PAGE_SIZE - zerolen, zerolen);
-		}
 	}
 	uobj->size = newpages;
 	VM_OBJECT_UNLOCK(uobj);
+
+	TMPFS_LOCK(tmp);
+	tmp->tm_pages_used += (newpages - oldpages);
+	TMPFS_UNLOCK(tmp);
+
+	node->tn_size = newsize;
+	vnode_pager_setsize(vp, newsize);
 	return (0);
 }
 
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/geom/raid/md_intel.c
--- a/head/sys/geom/raid/md_intel.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/geom/raid/md_intel.c	Fri Jan 13 15:51:01 2012 +0200
@@ -25,7 +25,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/geom/raid/md_intel.c 226816 2011-10-26 21:50:10Z mav $");
+__FBSDID("$FreeBSD: head/sys/geom/raid/md_intel.c 229886 2012-01-09 23:01:42Z jimharris $");
 
 #include <sys/param.h>
 #include <sys/bio.h>
@@ -64,7 +64,10 @@
 	uint8_t		total_domains;
 	uint8_t		failed_disk_num;
 	uint8_t		ddf;
-	uint32_t	filler_2[7];
+	uint32_t	offset_hi;
+	uint32_t	disk_sectors_hi;
+	uint32_t	stripe_count_hi;
+	uint32_t	filler_2[4];
 	uint32_t	disk_idx[1];	/* total_disks entries. */
 #define INTEL_DI_IDX	0x00ffffff
 #define INTEL_DI_RBLD	0x01000000
@@ -111,7 +114,8 @@
 	uint8_t		fs_state;
 	uint16_t	verify_errors;
 	uint16_t	bad_blocks;
-	uint32_t	filler_1[4];
+	uint32_t	curr_migr_unit_hi;
+	uint32_t	filler_1[3];
 	struct intel_raid_map map[1];	/* 2 entries if migr_state != 0. */
 } __packed;
 
@@ -125,8 +129,9 @@
 #define INTEL_F_ASSIGNED	0x02
 #define INTEL_F_FAILED		0x04
 #define INTEL_F_ONLINE		0x08
-
-	uint32_t	filler[5];
+	uint32_t	owner_cfg_num;
+	uint32_t	sectors_hi;
+	uint32_t	filler[3];
 } __packed;
 
 struct intel_raid_conf {
@@ -254,6 +259,82 @@
 	return (mvol);
 }
 
+static off_t
+intel_get_map_offset(struct intel_raid_map *mmap)
+{
+	off_t offset = (off_t)mmap->offset_hi << 32;
+
+	offset += mmap->offset;
+	return (offset);
+}
+
+static void
+intel_set_map_offset(struct intel_raid_map *mmap, off_t offset)
+{
+
+	mmap->offset = offset & 0xffffffff;
+	mmap->offset_hi = offset >> 32;
+}
+
+static off_t
+intel_get_map_disk_sectors(struct intel_raid_map *mmap)
+{
+	off_t disk_sectors = (off_t)mmap->disk_sectors_hi << 32;
+
+	disk_sectors += mmap->disk_sectors;
+	return (disk_sectors);
+}
+
+static void
+intel_set_map_disk_sectors(struct intel_raid_map *mmap, off_t disk_sectors)
+{
+
+	mmap->disk_sectors = disk_sectors & 0xffffffff;
+	mmap->disk_sectors_hi = disk_sectors >> 32;
+}
+
+static void
+intel_set_map_stripe_count(struct intel_raid_map *mmap, off_t stripe_count)
+{
+
+	mmap->stripe_count = stripe_count & 0xffffffff;
+	mmap->stripe_count_hi = stripe_count >> 32;
+}
+
+static off_t
+intel_get_disk_sectors(struct intel_raid_disk *disk)
+{
+	off_t sectors = (off_t)disk->sectors_hi << 32;
+
+	sectors += disk->sectors;
+	return (sectors);
+}
+
+static void
+intel_set_disk_sectors(struct intel_raid_disk *disk, off_t sectors)
+{
+
+	disk->sectors = sectors & 0xffffffff;
+	disk->sectors_hi = sectors >> 32;
+}
+
+static off_t
+intel_get_vol_curr_migr_unit(struct intel_raid_vol *vol)
+{
+	off_t curr_migr_unit = (off_t)vol->curr_migr_unit_hi << 32;
+
+	curr_migr_unit += vol->curr_migr_unit;
+	return (curr_migr_unit);
+}
+
+static void
+intel_set_vol_curr_migr_unit(struct intel_raid_vol *vol, off_t curr_migr_unit)
+{
+
+	vol->curr_migr_unit = curr_migr_unit & 0xffffffff;
+	vol->curr_migr_unit_hi = curr_migr_unit >> 32;
+}
+
 static void
 g_raid_md_intel_print(struct intel_raid_conf *meta)
 {
@@ -274,10 +355,11 @@
 	printf("attributes          0x%08x\n", meta->attributes);
 	printf("total_disks         %u\n", meta->total_disks);
 	printf("total_volumes       %u\n", meta->total_volumes);
-	printf("DISK#   serial disk_sectors disk_id flags\n");
+	printf("DISK#   serial disk_sectors disk_sectors_hi disk_id flags\n");
 	for (i = 0; i < meta->total_disks; i++ ) {
-		printf("    %d   <%.16s> %u 0x%08x 0x%08x\n", i,
+		printf("    %d   <%.16s> %u %u 0x%08x 0x%08x\n", i,
 		    meta->disk[i].serial, meta->disk[i].sectors,
+		    meta->disk[i].sectors_hi,
 		    meta->disk[i].id, meta->disk[i].flags);
 	}
 	for (i = 0; i < meta->total_volumes; i++) {
@@ -288,6 +370,7 @@
 		printf(" state              %u\n", mvol->state);
 		printf(" reserved           %u\n", mvol->reserved);
 		printf(" curr_migr_unit     %u\n", mvol->curr_migr_unit);
+		printf(" curr_migr_unit_hi  %u\n", mvol->curr_migr_unit_hi);
 		printf(" checkpoint_id      %u\n", mvol->checkpoint_id);
 		printf(" migr_state         %u\n", mvol->migr_state);
 		printf(" migr_type          %u\n", mvol->migr_type);
@@ -297,8 +380,11 @@
 			printf("  *** Map %d ***\n", j);
 			mmap = intel_get_map(mvol, j);
 			printf("  offset            %u\n", mmap->offset);
+			printf("  offset_hi         %u\n", mmap->offset_hi);
 			printf("  disk_sectors      %u\n", mmap->disk_sectors);
+			printf("  disk_sectors_hi   %u\n", mmap->disk_sectors_hi);
 			printf("  stripe_count      %u\n", mmap->stripe_count);
+			printf("  stripe_count_hi   %u\n", mmap->stripe_count_hi);
 			printf("  strip_sectors     %u\n", mmap->strip_sectors);
 			printf("  status            %u\n", mmap->status);
 			printf("  type              %u\n", mmap->type);
@@ -660,12 +746,15 @@
 				continue;
 			/* Make sure this disk is big enough. */
 			TAILQ_FOREACH(sd, &tmpdisk->d_subdisks, sd_next) {
+				off_t disk_sectors = 
+				    intel_get_disk_sectors(&pd->pd_disk_meta);
+
 				if (sd->sd_offset + sd->sd_size + 4096 >
-				    (off_t)pd->pd_disk_meta.sectors * 512) {
+				    disk_sectors * 512) {
 					G_RAID_DEBUG1(1, sc,
 					    "Disk too small (%llu < %llu)",
-					    ((unsigned long long)
-					    pd->pd_disk_meta.sectors) * 512,
+					    (unsigned long long)
+					    disk_sectors * 512,
 					    (unsigned long long)
 					    sd->sd_offset + sd->sd_size + 4096);
 					break;
@@ -788,7 +877,7 @@
 					sd->sd_rebuild_pos = 0;
 				} else {
 					sd->sd_rebuild_pos =
-					    (off_t)mvol->curr_migr_unit *
+					    intel_get_vol_curr_migr_unit(mvol) *
 					    sd->sd_volume->v_strip_size *
 					    mmap0->total_domains;
 				}
@@ -815,7 +904,7 @@
 					sd->sd_rebuild_pos = 0;
 				} else {
 					sd->sd_rebuild_pos =
-					    (off_t)mvol->curr_migr_unit *
+					    intel_get_vol_curr_migr_unit(mvol) *
 					    sd->sd_volume->v_strip_size *
 					    mmap0->total_domains;
 				}
@@ -967,8 +1056,8 @@
 		vol->v_sectorsize = 512; //ZZZ
 		for (j = 0; j < vol->v_disks_count; j++) {
 			sd = &vol->v_subdisks[j];
-			sd->sd_offset = (off_t)mmap->offset * 512; //ZZZ
-			sd->sd_size = (off_t)mmap->disk_sectors * 512; //ZZZ
+			sd->sd_offset = intel_get_map_offset(mmap) * 512; //ZZZ
+			sd->sd_size = intel_get_map_disk_sectors(mmap) * 512; //ZZZ
 		}
 		g_raid_start_volume(vol);
 	}
@@ -1176,9 +1265,6 @@
 				G_RAID_DEBUG(1,
 				    "Intel vendor mismatch 0x%04x != 0x8086",
 				    vendor);
-			} else if (pp->mediasize / pp->sectorsize > UINT32_MAX) {
-				G_RAID_DEBUG(1,
-				    "Intel disk '%s' is too big.", pp->name);
 			} else {
 				G_RAID_DEBUG(1,
 				    "No Intel metadata, forcing spare.");
@@ -1195,10 +1281,10 @@
 		G_RAID_DEBUG(1, "Intel serial '%s' not found", serial);
 		goto fail1;
 	}
-	if (meta->disk[disk_pos].sectors !=
+	if (intel_get_disk_sectors(&meta->disk[disk_pos]) !=
 	    (pp->mediasize / pp->sectorsize)) {
 		G_RAID_DEBUG(1, "Intel size mismatch %ju != %ju",
-		    (off_t)meta->disk[disk_pos].sectors,
+		    intel_get_disk_sectors(&meta->disk[disk_pos]),
 		    (off_t)(pp->mediasize / pp->sectorsize));
 		goto fail1;
 	}
@@ -1266,7 +1352,8 @@
 	pd->pd_disk_pos = -1;
 	if (spare == 2) {
 		memcpy(&pd->pd_disk_meta.serial[0], serial, INTEL_SERIAL_LEN);
-		pd->pd_disk_meta.sectors = pp->mediasize / pp->sectorsize;
+		intel_set_disk_sectors(&pd->pd_disk_meta, 
+		    pp->mediasize / pp->sectorsize);
 		pd->pd_disk_meta.id = 0;
 		pd->pd_disk_meta.flags = INTEL_F_SPARE;
 	} else {
@@ -1372,7 +1459,7 @@
 	const char *verb, *volname, *levelname, *diskname;
 	char *tmp;
 	int *nargs, *force;
-	off_t off, size, sectorsize, strip;
+	off_t off, size, sectorsize, strip, disk_sectors;
 	intmax_t *sizearg, *striparg;
 	int numdisks, i, len, level, qual, update;
 	int error;
@@ -1452,13 +1539,6 @@
 			cp->private = disk;
 			g_topology_unlock();
 
-			if (pp->mediasize / pp->sectorsize > UINT32_MAX) {
-				gctl_error(req,
-				    "Disk '%s' is too big.", diskname);
-				error = -8;
-				break;
-			}
-
 			error = g_raid_md_get_label(cp,
 			    &pd->pd_disk_meta.serial[0], INTEL_SERIAL_LEN);
 			if (error != 0) {
@@ -1479,7 +1559,8 @@
 				    "Dumping not supported by %s.",
 				    cp->provider->name);
 
-			pd->pd_disk_meta.sectors = pp->mediasize / pp->sectorsize;
+			intel_set_disk_sectors(&pd->pd_disk_meta,
+			    pp->mediasize / pp->sectorsize);
 			if (size > pp->mediasize)
 				size = pp->mediasize;
 			if (sectorsize < pp->sectorsize)
@@ -1544,10 +1625,6 @@
 			gctl_error(req, "Size too small.");
 			return (-13);
 		}
-		if (size > 0xffffffffllu * sectorsize) {
-			gctl_error(req, "Size too big.");
-			return (-14);
-		}
 
 		/* We have all we need, create things: volume, ... */
 		mdi->mdio_started = 1;
@@ -1655,8 +1732,11 @@
 			disk = vol1->v_subdisks[i].sd_disk;
 			pd = (struct g_raid_md_intel_perdisk *)
 			    disk->d_md_data;
-			if ((off_t)pd->pd_disk_meta.sectors * 512 < size)
-				size = (off_t)pd->pd_disk_meta.sectors * 512;
+			disk_sectors = 
+			    intel_get_disk_sectors(&pd->pd_disk_meta);
+
+			if (disk_sectors * 512 < size)
+				size = disk_sectors * 512;
 			if (disk->d_consumer != NULL &&
 			    disk->d_consumer->provider != NULL &&
 			    disk->d_consumer->provider->sectorsize >
@@ -1950,14 +2030,6 @@
 			pp = cp->provider;
 			g_topology_unlock();
 
-			if (pp->mediasize / pp->sectorsize > UINT32_MAX) {
-				gctl_error(req,
-				    "Disk '%s' is too big.", diskname);
-				g_raid_kill_consumer(sc, cp);
-				error = -8;
-				break;
-			}
-
 			/* Read disk serial. */
 			error = g_raid_md_get_label(cp,
 			    &serial[0], INTEL_SERIAL_LEN);
@@ -1990,7 +2062,8 @@
 
 			memcpy(&pd->pd_disk_meta.serial[0], &serial[0],
 			    INTEL_SERIAL_LEN);
-			pd->pd_disk_meta.sectors = pp->mediasize / pp->sectorsize;
+			intel_set_disk_sectors(&pd->pd_disk_meta,
+			    pp->mediasize / pp->sectorsize);
 			pd->pd_disk_meta.id = 0;
 			pd->pd_disk_meta.flags = INTEL_F_SPARE;
 
@@ -2165,8 +2238,8 @@
 		mmap0 = intel_get_map(mvol, 0);
 
 		/* Write map / common part of two maps. */
-		mmap0->offset = sd->sd_offset / sectorsize;
-		mmap0->disk_sectors = sd->sd_size / sectorsize;
+		intel_set_map_offset(mmap0, sd->sd_offset / sectorsize);
+		intel_set_map_disk_sectors(mmap0, sd->sd_size / sectorsize);
 		mmap0->strip_sectors = vol->v_strip_size / sectorsize;
 		if (vol->v_state == G_RAID_VOLUME_S_BROKEN)
 			mmap0->status = INTEL_S_FAILURE;
@@ -2188,15 +2261,15 @@
 			mmap0->total_domains = 2;
 		else
 			mmap0->total_domains = 1;
-		mmap0->stripe_count = sd->sd_size / vol->v_strip_size /
-		    mmap0->total_domains;
+		intel_set_map_stripe_count(mmap0,
+		    sd->sd_size / vol->v_strip_size / mmap0->total_domains);
 		mmap0->failed_disk_num = 0xff;
 		mmap0->ddf = 1;
 
 		/* If there are two maps - copy common and update. */
 		if (mvol->migr_state) {
-			mvol->curr_migr_unit = pos /
-			    vol->v_strip_size / mmap0->total_domains;
+			intel_set_vol_curr_migr_unit(mvol,
+			    pos / vol->v_strip_size / mmap0->total_domains);
 			mmap1 = intel_get_map(mvol, 1);
 			memcpy(mmap1, mmap0, sizeof(struct intel_raid_map));
 			mmap0->status = INTEL_S_READY;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/gnu/dev/sound/pci/emu10k1-alsa.h
--- a/head/sys/gnu/dev/sound/pci/emu10k1-alsa.h	Fri Jan 13 00:58:58 2012 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1601 +0,0 @@
-#ifndef __SOUND_EMU10K1_H
-#define __SOUND_EMU10K1_H
-
-/*-
- *  Copyright (c) by Jaroslav Kysela <perex at suse.cz>,
- *		     Creative Labs, Inc.
- *  Definitions for EMU10K1 (SB Live!) chips
- *
- *
- *   This program is free software; you can redistribute it and/or modify
- *   it under the terms of the GNU General Public License as published by
- *   the Free Software Foundation; either version 2 of the License, or
- *   (at your option) any later version.
- *
- *   This program is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *   GNU General Public License for more details.
- *
- *   You should have received a copy of the GNU General Public License
- *   along with this program; if not, write to the Free Software
- *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
- *
- */
-
-/* $FreeBSD$ */
-
-#ifdef __KERNEL__
-
-#include <sound/pcm.h>
-#include <sound/rawmidi.h>
-#include <sound/hwdep.h>
-#include <sound/ac97_codec.h>
-#include <sound/util_mem.h>
-#include <sound/pcm-indirect.h>
-#include <sound/timer.h>
-#include <linux/interrupt.h>
-#include <linux/mutex.h>
-#include <asm/io.h>
-
-/* ------------------- DEFINES -------------------- */
-
-#define EMUPAGESIZE     4096
-#define MAXREQVOICES    8
-#define MAXPAGES        8192
-#define RESERVED        0
-#define NUM_MIDI        16
-#define NUM_G           64              /* use all channels */
-#define NUM_FXSENDS     4
-#define NUM_EFX_PLAYBACK    16
-
-/* FIXME? - according to the OSS driver the EMU10K1 needs a 29 bit DMA mask */
-#define EMU10K1_DMA_MASK	0x7fffffffUL	/* 31bit */
-#define AUDIGY_DMA_MASK		0x7fffffffUL	/* 31bit FIXME - 32 should work? */
-						/* See ALSA bug #1276 - rlrevell */
-
-#define TMEMSIZE        256*1024
-#define TMEMSIZEREG     4
-
-#define IP_TO_CP(ip) ((ip == 0) ? 0 : (((0x00001000uL | (ip & 0x00000FFFL)) << (((ip >> 12) & 0x000FL) + 4)) & 0xFFFF0000uL))
-
-// Audigy specify registers are prefixed with 'A_'
-
-/************************************************************************************************/
-/* PCI function 0 registers, address = <val> + PCIBASE0						*/
-/************************************************************************************************/
-
-#define PTR			0x00		/* Indexed register set pointer register	*/
-						/* NOTE: The CHANNELNUM and ADDRESS words can	*/
-						/* be modified independently of each other.	*/
-#define PTR_CHANNELNUM_MASK	0x0000003f	/* For each per-channel register, indicates the	*/
-						/* channel number of the register to be		*/
-						/* accessed.  For non per-channel registers the	*/
-						/* value should be set to zero.			*/
-#define PTR_ADDRESS_MASK	0x07ff0000	/* Register index				*/
-#define A_PTR_ADDRESS_MASK	0x0fff0000
-
-#define DATA			0x04		/* Indexed register set data register		*/
-
-#define IPR			0x08		/* Global interrupt pending register		*/
-						/* Clear pending interrupts by writing a 1 to	*/
-						/* the relevant bits and zero to the other bits	*/
-#define IPR_P16V		0x80000000	/* Bit set when the CA0151 P16V chip wishes
-						   to interrupt */
-#define IPR_GPIOMSG		0x20000000	/* GPIO message interrupt (RE'd, still not sure 
-						   which INTE bits enable it)			*/
-
-/* The next two interrupts are for the midi port on the Audigy Drive (A_MPU1)			*/
-#define IPR_A_MIDITRANSBUFEMPTY2 0x10000000	/* MIDI UART transmit buffer empty		*/
-#define IPR_A_MIDIRECVBUFEMPTY2	0x08000000	/* MIDI UART receive buffer empty		*/
-
-#define IPR_SPDIFBUFFULL	0x04000000	/* SPDIF capture related, 10k2 only? (RE)	*/
-#define IPR_SPDIFBUFHALFFULL	0x02000000	/* SPDIF capture related? (RE)			*/
-
-#define IPR_SAMPLERATETRACKER	0x01000000	/* Sample rate tracker lock status change	*/
-#define IPR_FXDSP		0x00800000	/* Enable FX DSP interrupts			*/
-#define IPR_FORCEINT		0x00400000	/* Force Sound Blaster interrupt		*/
-#define IPR_PCIERROR		0x00200000	/* PCI bus error				*/
-#define IPR_VOLINCR		0x00100000	/* Volume increment button pressed		*/
-#define IPR_VOLDECR		0x00080000	/* Volume decrement button pressed		*/
-#define IPR_MUTE		0x00040000	/* Mute button pressed				*/
-#define IPR_MICBUFFULL		0x00020000	/* Microphone buffer full			*/
-#define IPR_MICBUFHALFFULL	0x00010000	/* Microphone buffer half full			*/
-#define IPR_ADCBUFFULL		0x00008000	/* ADC buffer full				*/
-#define IPR_ADCBUFHALFFULL	0x00004000	/* ADC buffer half full				*/
-#define IPR_EFXBUFFULL		0x00002000	/* Effects buffer full				*/
-#define IPR_EFXBUFHALFFULL	0x00001000	/* Effects buffer half full			*/
-#define IPR_GPSPDIFSTATUSCHANGE	0x00000800	/* GPSPDIF channel status change		*/
-#define IPR_CDROMSTATUSCHANGE	0x00000400	/* CD-ROM channel status change			*/
-#define IPR_INTERVALTIMER	0x00000200	/* Interval timer terminal count		*/
-#define IPR_MIDITRANSBUFEMPTY	0x00000100	/* MIDI UART transmit buffer empty		*/
-#define IPR_MIDIRECVBUFEMPTY	0x00000080	/* MIDI UART receive buffer empty		*/
-#define IPR_CHANNELLOOP		0x00000040	/* Channel (half) loop interrupt(s) pending	*/
-#define IPR_CHANNELNUMBERMASK	0x0000003f	/* When IPR_CHANNELLOOP is set, indicates the	*/
-						/* highest set channel in CLIPL, CLIPH, HLIPL,  */
-						/* or HLIPH.  When IP is written with CL set,	*/
-						/* the bit in H/CLIPL or H/CLIPH corresponding	*/
-						/* to the CIN value written will be cleared.	*/
-
-#define INTE			0x0c		/* Interrupt enable register			*/
-#define INTE_VIRTUALSB_MASK	0xc0000000	/* Virtual Soundblaster I/O port capture	*/
-#define INTE_VIRTUALSB_220	0x00000000	/* Capture at I/O base address 0x220-0x22f	*/
-#define INTE_VIRTUALSB_240	0x40000000	/* Capture at I/O base address 0x240		*/
-#define INTE_VIRTUALSB_260	0x80000000	/* Capture at I/O base address 0x260		*/
-#define INTE_VIRTUALSB_280	0xc0000000	/* Capture at I/O base address 0x280		*/
-#define INTE_VIRTUALMPU_MASK	0x30000000	/* Virtual MPU I/O port capture			*/
-#define INTE_VIRTUALMPU_300	0x00000000	/* Capture at I/O base address 0x300-0x301	*/
-#define INTE_VIRTUALMPU_310	0x10000000	/* Capture at I/O base address 0x310		*/
-#define INTE_VIRTUALMPU_320	0x20000000	/* Capture at I/O base address 0x320		*/
-#define INTE_VIRTUALMPU_330	0x30000000	/* Capture at I/O base address 0x330		*/
-#define INTE_MASTERDMAENABLE	0x08000000	/* Master DMA emulation at 0x000-0x00f		*/
-#define INTE_SLAVEDMAENABLE	0x04000000	/* Slave DMA emulation at 0x0c0-0x0df		*/
-#define INTE_MASTERPICENABLE	0x02000000	/* Master PIC emulation at 0x020-0x021		*/
-#define INTE_SLAVEPICENABLE	0x01000000	/* Slave PIC emulation at 0x0a0-0x0a1		*/
-#define INTE_VSBENABLE		0x00800000	/* Enable virtual Soundblaster			*/
-#define INTE_ADLIBENABLE	0x00400000	/* Enable AdLib emulation at 0x388-0x38b	*/
-#define INTE_MPUENABLE		0x00200000	/* Enable virtual MPU				*/
-#define INTE_FORCEINT		0x00100000	/* Continuously assert INTAN			*/
-
-#define INTE_MRHANDENABLE	0x00080000	/* Enable the "Mr. Hand" logic			*/
-						/* NOTE: There is no reason to use this under	*/
-						/* Linux, and it will cause odd hardware 	*/
-						/* behavior and possibly random segfaults and	*/
-						/* lockups if enabled.				*/
-
-/* The next two interrupts are for the midi port on the Audigy Drive (A_MPU1)			*/
-#define INTE_A_MIDITXENABLE2	0x00020000	/* Enable MIDI transmit-buffer-empty interrupts	*/
-#define INTE_A_MIDIRXENABLE2	0x00010000	/* Enable MIDI receive-buffer-empty interrupts	*/
-
-
-#define INTE_SAMPLERATETRACKER	0x00002000	/* Enable sample rate tracker interrupts	*/
-						/* NOTE: This bit must always be enabled       	*/
-#define INTE_FXDSPENABLE	0x00001000	/* Enable FX DSP interrupts			*/
-#define INTE_PCIERRORENABLE	0x00000800	/* Enable PCI bus error interrupts		*/
-#define INTE_VOLINCRENABLE	0x00000400	/* Enable volume increment button interrupts	*/
-#define INTE_VOLDECRENABLE	0x00000200	/* Enable volume decrement button interrupts	*/
-#define INTE_MUTEENABLE		0x00000100	/* Enable mute button interrupts		*/
-#define INTE_MICBUFENABLE	0x00000080	/* Enable microphone buffer interrupts		*/
-#define INTE_ADCBUFENABLE	0x00000040	/* Enable ADC buffer interrupts			*/
-#define INTE_EFXBUFENABLE	0x00000020	/* Enable Effects buffer interrupts		*/
-#define INTE_GPSPDIFENABLE	0x00000010	/* Enable GPSPDIF status interrupts		*/
-#define INTE_CDSPDIFENABLE	0x00000008	/* Enable CDSPDIF status interrupts		*/
-#define INTE_INTERVALTIMERENB	0x00000004	/* Enable interval timer interrupts		*/
-#define INTE_MIDITXENABLE	0x00000002	/* Enable MIDI transmit-buffer-empty interrupts	*/
-#define INTE_MIDIRXENABLE	0x00000001	/* Enable MIDI receive-buffer-empty interrupts	*/
-
-#define WC			0x10		/* Wall Clock register				*/
-#define WC_SAMPLECOUNTER_MASK	0x03FFFFC0	/* Sample periods elapsed since reset		*/
-#define WC_SAMPLECOUNTER	0x14060010
-#define WC_CURRENTCHANNEL	0x0000003F	/* Channel [0..63] currently being serviced	*/
-						/* NOTE: Each channel takes 1/64th of a sample	*/
-						/* period to be serviced.			*/
-
-#define HCFG			0x14		/* Hardware config register			*/
-						/* NOTE: There is no reason to use the legacy	*/
-						/* SoundBlaster emulation stuff described below	*/
-						/* under Linux, and all kinds of weird hardware	*/
-						/* behavior can result if you try.  Don't.	*/
-#define HCFG_LEGACYFUNC_MASK	0xe0000000	/* Legacy function number 			*/
-#define HCFG_LEGACYFUNC_MPU	0x00000000	/* Legacy MPU	 				*/
-#define HCFG_LEGACYFUNC_SB	0x40000000	/* Legacy SB					*/
-#define HCFG_LEGACYFUNC_AD	0x60000000	/* Legacy AD					*/
-#define HCFG_LEGACYFUNC_MPIC	0x80000000	/* Legacy MPIC					*/
-#define HCFG_LEGACYFUNC_MDMA	0xa0000000	/* Legacy MDMA					*/
-#define HCFG_LEGACYFUNC_SPCI	0xc0000000	/* Legacy SPCI					*/
-#define HCFG_LEGACYFUNC_SDMA	0xe0000000	/* Legacy SDMA					*/
-#define HCFG_IOCAPTUREADDR	0x1f000000	/* The 4 LSBs of the captured I/O address.	*/
-#define HCFG_LEGACYWRITE	0x00800000	/* 1 = write, 0 = read 				*/
-#define HCFG_LEGACYWORD		0x00400000	/* 1 = word, 0 = byte 				*/
-#define HCFG_LEGACYINT		0x00200000	/* 1 = legacy event captured. Write 1 to clear.	*/
-						/* NOTE: The rest of the bits in this register	*/
-						/* _are_ relevant under Linux.			*/
-#define HCFG_CODECFORMAT_MASK	0x00070000	/* CODEC format					*/
-#define HCFG_CODECFORMAT_AC97	0x00000000	/* AC97 CODEC format -- Primary Output		*/
-#define HCFG_CODECFORMAT_I2S	0x00010000	/* I2S CODEC format -- Secondary (Rear) Output	*/
-#define HCFG_GPINPUT0		0x00004000	/* External pin112				*/
-#define HCFG_GPINPUT1		0x00002000	/* External pin110				*/
-#define HCFG_GPOUTPUT_MASK	0x00001c00	/* External pins which may be controlled	*/
-#define HCFG_GPOUT0		0x00001000	/* External pin? (spdif enable on 5.1)		*/
-#define HCFG_GPOUT1		0x00000800	/* External pin? (IR)				*/
-#define HCFG_GPOUT2		0x00000400	/* External pin? (IR)				*/
-#define HCFG_JOYENABLE      	0x00000200	/* Internal joystick enable    			*/
-#define HCFG_PHASETRACKENABLE	0x00000100	/* Phase tracking enable			*/
-						/* 1 = Force all 3 async digital inputs to use	*/
-						/* the same async sample rate tracker (ZVIDEO)	*/
-#define HCFG_AC3ENABLE_MASK	0x000000e0	/* AC3 async input control - Not implemented	*/
-#define HCFG_AC3ENABLE_ZVIDEO	0x00000080	/* Channels 0 and 1 replace ZVIDEO		*/
-#define HCFG_AC3ENABLE_CDSPDIF	0x00000040	/* Channels 0 and 1 replace CDSPDIF		*/
-#define HCFG_AC3ENABLE_GPSPDIF  0x00000020      /* Channels 0 and 1 replace GPSPDIF             */
-#define HCFG_AUTOMUTE		0x00000010	/* When set, the async sample rate convertors	*/
-						/* will automatically mute their output when	*/
-						/* they are not rate-locked to the external	*/
-						/* async audio source  				*/
-#define HCFG_LOCKSOUNDCACHE	0x00000008	/* 1 = Cancel bustmaster accesses to soundcache */
-						/* NOTE: This should generally never be used.  	*/
-#define HCFG_LOCKTANKCACHE_MASK	0x00000004	/* 1 = Cancel bustmaster accesses to tankcache	*/
-						/* NOTE: This should generally never be used.  	*/
-#define HCFG_LOCKTANKCACHE	0x01020014
-#define HCFG_MUTEBUTTONENABLE	0x00000002	/* 1 = Master mute button sets AUDIOENABLE = 0.	*/
-						/* NOTE: This is a 'cheap' way to implement a	*/
-						/* master mute function on the mute button, and	*/
-						/* in general should not be used unless a more	*/
-						/* sophisticated master mute function has not	*/
-						/* been written.       				*/
-#define HCFG_AUDIOENABLE	0x00000001	/* 0 = CODECs transmit zero-valued samples	*/
-						/* Should be set to 1 when the EMU10K1 is	*/
-						/* completely initialized.			*/
-
-//For Audigy, MPU port move to 0x70-0x74 ptr register
-
-#define MUDATA			0x18		/* MPU401 data register (8 bits)       		*/
-
-#define MUCMD			0x19		/* MPU401 command register (8 bits)    		*/
-#define MUCMD_RESET		0xff		/* RESET command				*/
-#define MUCMD_ENTERUARTMODE	0x3f		/* Enter_UART_mode command			*/
-						/* NOTE: All other commands are ignored		*/
-
-#define MUSTAT			MUCMD		/* MPU401 status register (8 bits)     		*/
-#define MUSTAT_IRDYN		0x80		/* 0 = MIDI data or command ACK			*/
-#define MUSTAT_ORDYN		0x40		/* 0 = MUDATA can accept a command or data	*/
-
-#define A_IOCFG			0x18		/* GPIO on Audigy card (16bits)			*/
-#define A_GPINPUT_MASK		0xff00
-#define A_GPOUTPUT_MASK		0x00ff
-
-// Audigy output/GPIO stuff taken from the kX drivers
-#define A_IOCFG_GPOUT0		0x0044		/* analog/digital				*/
-#define A_IOCFG_DISABLE_ANALOG	0x0040		/* = 'enable' for Audigy2 (chiprev=4)		*/
-#define A_IOCFG_ENABLE_DIGITAL	0x0004
-#define A_IOCFG_ENABLE_DIGITAL_AUDIGY4	0x0080
-#define A_IOCFG_UNKNOWN_20      0x0020
-#define A_IOCFG_DISABLE_AC97_FRONT      0x0080  /* turn off ac97 front -> front (10k2.1)	*/
-#define A_IOCFG_GPOUT1		0x0002		/* IR? drive's internal bypass (?)		*/
-#define A_IOCFG_GPOUT2		0x0001		/* IR */
-#define A_IOCFG_MULTIPURPOSE_JACK	0x2000  /* center+lfe+rear_center (a2/a2ex)		*/
-                                                /* + digital for generic 10k2			*/
-#define A_IOCFG_DIGITAL_JACK    0x1000          /* digital for a2 platinum			*/
-#define A_IOCFG_FRONT_JACK      0x4000
-#define A_IOCFG_REAR_JACK       0x8000
-#define A_IOCFG_PHONES_JACK     0x0100          /* LiveDrive					*/
-
-/* outputs:
- *	for audigy2 platinum:	0xa00
- *	for a2 platinum ex:	0x1c00
- *	for a1 platinum:	0x0
- */
-
-#define TIMER			0x1a		/* Timer terminal count register		*/
-						/* NOTE: After the rate is changed, a maximum	*/
-						/* of 1024 sample periods should be allowed	*/
-						/* before the new rate is guaranteed accurate.	*/
-#define TIMER_RATE_MASK		0x000003ff	/* Timer interrupt rate in sample periods	*/
-						/* 0 == 1024 periods, [1..4] are not useful	*/
-#define TIMER_RATE		0x0a00001a
-
-#define AC97DATA		0x1c		/* AC97 register set data register (16 bit)	*/
-
-#define AC97ADDRESS		0x1e		/* AC97 register set address register (8 bit)	*/
-#define AC97ADDRESS_READY	0x80		/* Read-only bit, reflects CODEC READY signal	*/
-#define AC97ADDRESS_ADDRESS	0x7f		/* Address of indexed AC97 register		*/
-
-/* Available on the Audigy 2 and Audigy 4 only. This is the P16V chip. */
-#define PTR2			0x20		/* Indexed register set pointer register	*/
-#define DATA2			0x24		/* Indexed register set data register		*/
-#define IPR2			0x28		/* P16V interrupt pending register		*/
-#define IPR2_PLAYBACK_CH_0_LOOP      0x00001000 /* Playback Channel 0 loop                               */
-#define IPR2_PLAYBACK_CH_0_HALF_LOOP 0x00000100 /* Playback Channel 0 half loop                          */
-#define IPR2_CAPTURE_CH_0_LOOP       0x00100000 /* Capture Channel 0 loop                               */
-#define IPR2_CAPTURE_CH_0_HALF_LOOP  0x00010000 /* Capture Channel 0 half loop                          */
-						/* 0x00000100 Playback. Only in once per period.
-						 * 0x00110000 Capture. Int on half buffer.
-						 */
-#define INTE2			0x2c		/* P16V Interrupt enable register. 	*/
-#define INTE2_PLAYBACK_CH_0_LOOP      0x00001000 /* Playback Channel 0 loop                               */
-#define INTE2_PLAYBACK_CH_0_HALF_LOOP 0x00000100 /* Playback Channel 0 half loop                          */
-#define INTE2_PLAYBACK_CH_1_LOOP      0x00002000 /* Playback Channel 1 loop                               */
-#define INTE2_PLAYBACK_CH_1_HALF_LOOP 0x00000200 /* Playback Channel 1 half loop                          */
-#define INTE2_PLAYBACK_CH_2_LOOP      0x00004000 /* Playback Channel 2 loop                               */
-#define INTE2_PLAYBACK_CH_2_HALF_LOOP 0x00000400 /* Playback Channel 2 half loop                          */
-#define INTE2_PLAYBACK_CH_3_LOOP      0x00008000 /* Playback Channel 3 loop                               */
-#define INTE2_PLAYBACK_CH_3_HALF_LOOP 0x00000800 /* Playback Channel 3 half loop                          */
-#define INTE2_CAPTURE_CH_0_LOOP       0x00100000 /* Capture Channel 0 loop                               */
-#define INTE2_CAPTURE_CH_0_HALF_LOOP  0x00010000 /* Caputre Channel 0 half loop                          */
-#define HCFG2			0x34		/* Defaults: 0, win2000 sets it to 00004201 */
-						/* 0x00000000 2-channel output. */
-						/* 0x00000200 8-channel output. */
-						/* 0x00000004 pauses stream/irq fail. */
-						/* Rest of bits no nothing to sound output */
-						/* bit 0: Enable P16V audio.
-						 * bit 1: Lock P16V record memory cache.
-						 * bit 2: Lock P16V playback memory cache.
-						 * bit 3: Dummy record insert zero samples.
-						 * bit 8: Record 8-channel in phase.
-						 * bit 9: Playback 8-channel in phase.
-						 * bit 11-12: Playback mixer attenuation: 0=0dB, 1=-6dB, 2=-12dB, 3=Mute.
-						 * bit 13: Playback mixer enable.
-						 * bit 14: Route SRC48 mixer output to fx engine.
-						 * bit 15: Enable IEEE 1394 chip.
-						 */
-#define IPR3			0x38		/* Cdif interrupt pending register		*/
-#define INTE3			0x3c		/* Cdif interrupt enable register. 	*/
-/************************************************************************************************/
-/* PCI function 1 registers, address = <val> + PCIBASE1						*/
-/************************************************************************************************/
-
-#define JOYSTICK1		0x00		/* Analog joystick port register		*/
-#define JOYSTICK2		0x01		/* Analog joystick port register		*/
-#define JOYSTICK3		0x02		/* Analog joystick port register		*/
-#define JOYSTICK4		0x03		/* Analog joystick port register		*/
-#define JOYSTICK5		0x04		/* Analog joystick port register		*/
-#define JOYSTICK6		0x05		/* Analog joystick port register		*/
-#define JOYSTICK7		0x06		/* Analog joystick port register		*/
-#define JOYSTICK8		0x07		/* Analog joystick port register		*/
-
-/* When writing, any write causes JOYSTICK_COMPARATOR output enable to be pulsed on write.	*/
-/* When reading, use these bitfields: */
-#define JOYSTICK_BUTTONS	0x0f		/* Joystick button data				*/
-#define JOYSTICK_COMPARATOR	0xf0		/* Joystick comparator data			*/
-
-
-/********************************************************************************************************/
-/* Emu10k1 pointer-offset register set, accessed through the PTR and DATA registers			*/
-/********************************************************************************************************/
-
-#define CPF			0x00		/* Current pitch and fraction register			*/
-#define CPF_CURRENTPITCH_MASK	0xffff0000	/* Current pitch (linear, 0x4000 == unity pitch shift) 	*/
-#define CPF_CURRENTPITCH	0x10100000
-#define CPF_STEREO_MASK		0x00008000	/* 1 = Even channel interleave, odd channel locked	*/
-#define CPF_STOP_MASK		0x00004000	/* 1 = Current pitch forced to 0			*/
-#define CPF_FRACADDRESS_MASK	0x00003fff	/* Linear fractional address of the current channel	*/
-
-#define PTRX			0x01		/* Pitch target and send A/B amounts register		*/
-#define PTRX_PITCHTARGET_MASK	0xffff0000	/* Pitch target of specified channel			*/
-#define PTRX_PITCHTARGET	0x10100001
-#define PTRX_FXSENDAMOUNT_A_MASK 0x0000ff00	/* Linear level of channel output sent to FX send bus A	*/
-#define PTRX_FXSENDAMOUNT_A	0x08080001
-#define PTRX_FXSENDAMOUNT_B_MASK 0x000000ff	/* Linear level of channel output sent to FX send bus B	*/
-#define PTRX_FXSENDAMOUNT_B	0x08000001
-
-#define CVCF			0x02		/* Current volume and filter cutoff register		*/
-#define CVCF_CURRENTVOL_MASK	0xffff0000	/* Current linear volume of specified channel		*/
-#define CVCF_CURRENTVOL		0x10100002
-#define CVCF_CURRENTFILTER_MASK	0x0000ffff	/* Current filter cutoff frequency of specified channel	*/
-#define CVCF_CURRENTFILTER	0x10000002
-
-#define VTFT			0x03		/* Volume target and filter cutoff target register	*/
-#define VTFT_VOLUMETARGET_MASK	0xffff0000	/* Volume target of specified channel			*/
-#define VTFT_VOLUMETARGET	0x10100003
-#define VTFT_FILTERTARGET_MASK	0x0000ffff	/* Filter cutoff target of specified channel		*/
-#define VTFT_FILTERTARGET	0x10000003
-
-#define Z1			0x05		/* Filter delay memory 1 register			*/
-
-#define Z2			0x04		/* Filter delay memory 2 register			*/
-
-#define PSST			0x06		/* Send C amount and loop start address register	*/
-#define PSST_FXSENDAMOUNT_C_MASK 0xff000000	/* Linear level of channel output sent to FX send bus C	*/
-
-#define PSST_FXSENDAMOUNT_C	0x08180006
-
-#define PSST_LOOPSTARTADDR_MASK	0x00ffffff	/* Loop start address of the specified channel		*/
-#define PSST_LOOPSTARTADDR	0x18000006
-
-#define DSL			0x07		/* Send D amount and loop start address register	*/
-#define DSL_FXSENDAMOUNT_D_MASK	0xff000000	/* Linear level of channel output sent to FX send bus D	*/
-
-#define DSL_FXSENDAMOUNT_D	0x08180007
-
-#define DSL_LOOPENDADDR_MASK	0x00ffffff	/* Loop end address of the specified channel		*/
-#define DSL_LOOPENDADDR		0x18000007
-
-#define CCCA			0x08		/* Filter Q, interp. ROM, byte size, cur. addr register */
-#define CCCA_RESONANCE		0xf0000000	/* Lowpass filter resonance (Q) height			*/
-#define CCCA_INTERPROMMASK	0x0e000000	/* Selects passband of interpolation ROM		*/
-						/* 1 == full band, 7 == lowpass				*/
-						/* ROM 0 is used when pitch shifting downward or less	*/
-						/* then 3 semitones upward.  Increasingly higher ROM	*/
-						/* numbers are used, typically in steps of 3 semitones,	*/
-						/* as upward pitch shifting is performed.		*/
-#define CCCA_INTERPROM_0	0x00000000	/* Select interpolation ROM 0				*/
-#define CCCA_INTERPROM_1	0x02000000	/* Select interpolation ROM 1				*/
-#define CCCA_INTERPROM_2	0x04000000	/* Select interpolation ROM 2				*/
-#define CCCA_INTERPROM_3	0x06000000	/* Select interpolation ROM 3				*/
-#define CCCA_INTERPROM_4	0x08000000	/* Select interpolation ROM 4				*/
-#define CCCA_INTERPROM_5	0x0a000000	/* Select interpolation ROM 5				*/
-#define CCCA_INTERPROM_6	0x0c000000	/* Select interpolation ROM 6				*/
-#define CCCA_INTERPROM_7	0x0e000000	/* Select interpolation ROM 7				*/
-#define CCCA_8BITSELECT		0x01000000	/* 1 = Sound memory for this channel uses 8-bit samples	*/
-#define CCCA_CURRADDR_MASK	0x00ffffff	/* Current address of the selected channel		*/
-#define CCCA_CURRADDR		0x18000008
-
-#define CCR			0x09		/* Cache control register				*/
-#define CCR_CACHEINVALIDSIZE	0x07190009
-#define CCR_CACHEINVALIDSIZE_MASK	0xfe000000	/* Number of invalid samples cache for this channel    	*/
-#define CCR_CACHELOOPFLAG	0x01000000	/* 1 = Cache has a loop service pending			*/
-#define CCR_INTERLEAVEDSAMPLES	0x00800000	/* 1 = A cache service will fetch interleaved samples	*/
-#define CCR_WORDSIZEDSAMPLES	0x00400000	/* 1 = A cache service will fetch word sized samples	*/
-#define CCR_READADDRESS		0x06100009
-#define CCR_READADDRESS_MASK	0x003f0000	/* Location of cache just beyond current cache service	*/
-#define CCR_LOOPINVALSIZE	0x0000fe00	/* Number of invalid samples in cache prior to loop	*/
-						/* NOTE: This is valid only if CACHELOOPFLAG is set	*/
-#define CCR_LOOPFLAG		0x00000100	/* Set for a single sample period when a loop occurs	*/
-#define CCR_CACHELOOPADDRHI	0x000000ff	/* DSL_LOOPSTARTADDR's hi byte if CACHELOOPFLAG is set	*/
-
-#define CLP			0x0a		/* Cache loop register (valid if CCR_CACHELOOPFLAG = 1) */
-						/* NOTE: This register is normally not used		*/
-#define CLP_CACHELOOPADDR	0x0000ffff	/* Cache loop address (DSL_LOOPSTARTADDR [0..15])	*/
-
-#define FXRT			0x0b		/* Effects send routing register			*/
-						/* NOTE: It is illegal to assign the same routing to	*/
-						/* two effects sends.					*/
-#define FXRT_CHANNELA		0x000f0000	/* Effects send bus number for channel's effects send A	*/
-#define FXRT_CHANNELB		0x00f00000	/* Effects send bus number for channel's effects send B	*/
-#define FXRT_CHANNELC		0x0f000000	/* Effects send bus number for channel's effects send C	*/
-#define FXRT_CHANNELD		0xf0000000	/* Effects send bus number for channel's effects send D	*/
-
-#define MAPA			0x0c		/* Cache map A						*/
-
-#define MAPB			0x0d		/* Cache map B						*/
-
-#define MAP_PTE_MASK		0xffffe000	/* The 19 MSBs of the PTE indexed by the PTI		*/
-#define MAP_PTI_MASK		0x00001fff	/* The 13 bit index to one of the 8192 PTE dwords      	*/
-
-#define ENVVOL			0x10		/* Volume envelope register				*/
-#define ENVVOL_MASK		0x0000ffff	/* Current value of volume envelope state variable	*/  
-						/* 0x8000-n == 666*n usec delay	       			*/
-
-#define ATKHLDV 		0x11		/* Volume envelope hold and attack register		*/
-#define ATKHLDV_PHASE0		0x00008000	/* 0 = Begin attack phase				*/
-#define ATKHLDV_HOLDTIME_MASK	0x00007f00	/* Envelope hold time (127-n == n*88.2msec)		*/
-#define ATKHLDV_ATTACKTIME_MASK	0x0000007f	/* Envelope attack time, log encoded			*/
-						/* 0 = infinite, 1 = 10.9msec, ... 0x7f = 5.5msec	*/
-
-#define DCYSUSV 		0x12		/* Volume envelope sustain and decay register		*/
-#define DCYSUSV_PHASE1_MASK	0x00008000	/* 0 = Begin attack phase, 1 = begin release phase	*/
-#define DCYSUSV_SUSTAINLEVEL_MASK 0x00007f00	/* 127 = full, 0 = off, 0.75dB increments		*/
-#define DCYSUSV_CHANNELENABLE_MASK 0x00000080	/* 1 = Inhibit envelope engine from writing values in	*/
-						/* this channel and from writing to pitch, filter and	*/
-						/* volume targets.					*/
-#define DCYSUSV_DECAYTIME_MASK	0x0000007f	/* Volume envelope decay time, log encoded     		*/
-						/* 0 = 43.7msec, 1 = 21.8msec, 0x7f = 22msec		*/
-
-#define LFOVAL1 		0x13		/* Modulation LFO value					*/
-#define LFOVAL_MASK		0x0000ffff	/* Current value of modulation LFO state variable	*/
-						/* 0x8000-n == 666*n usec delay				*/
-
-#define ENVVAL			0x14		/* Modulation envelope register				*/
-#define ENVVAL_MASK		0x0000ffff	/* Current value of modulation envelope state variable 	*/
-						/* 0x8000-n == 666*n usec delay				*/
-
-#define ATKHLDM			0x15		/* Modulation envelope hold and attack register		*/
-#define ATKHLDM_PHASE0		0x00008000	/* 0 = Begin attack phase				*/
-#define ATKHLDM_HOLDTIME	0x00007f00	/* Envelope hold time (127-n == n*42msec)		*/
-#define ATKHLDM_ATTACKTIME	0x0000007f	/* Envelope attack time, log encoded			*/
-						/* 0 = infinite, 1 = 11msec, ... 0x7f = 5.5msec		*/
-
-#define DCYSUSM			0x16		/* Modulation envelope decay and sustain register	*/
-#define DCYSUSM_PHASE1_MASK	0x00008000	/* 0 = Begin attack phase, 1 = begin release phase	*/
-#define DCYSUSM_SUSTAINLEVEL_MASK 0x00007f00	/* 127 = full, 0 = off, 0.75dB increments		*/
-#define DCYSUSM_DECAYTIME_MASK	0x0000007f	/* Envelope decay time, log encoded			*/
-						/* 0 = 43.7msec, 1 = 21.8msec, 0x7f = 22msec		*/
-
-#define LFOVAL2 		0x17		/* Vibrato LFO register					*/
-#define LFOVAL2_MASK		0x0000ffff	/* Current value of vibrato LFO state variable 		*/
-						/* 0x8000-n == 666*n usec delay				*/
-
-#define IP			0x18		/* Initial pitch register				*/
-#define IP_MASK			0x0000ffff	/* Exponential initial pitch shift			*/
-						/* 4 bits of octave, 12 bits of fractional octave	*/
-#define IP_UNITY		0x0000e000	/* Unity pitch shift					*/
-
-#define IFATN			0x19		/* Initial filter cutoff and attenuation register	*/
-#define IFATN_FILTERCUTOFF_MASK	0x0000ff00	/* Initial filter cutoff frequency in exponential units	*/
-						/* 6 most significant bits are semitones		*/
-						/* 2 least significant bits are fractions		*/
-#define IFATN_FILTERCUTOFF	0x08080019
-#define IFATN_ATTENUATION_MASK	0x000000ff	/* Initial attenuation in 0.375dB steps			*/
-#define IFATN_ATTENUATION	0x08000019
-
-
-#define PEFE			0x1a		/* Pitch envelope and filter envelope amount register	*/
-#define PEFE_PITCHAMOUNT_MASK	0x0000ff00	/* Pitch envlope amount					*/
-						/* Signed 2's complement, +/- one octave peak extremes	*/
-#define PEFE_PITCHAMOUNT	0x0808001a
-#define PEFE_FILTERAMOUNT_MASK	0x000000ff	/* Filter envlope amount				*/
-						/* Signed 2's complement, +/- six octaves peak extremes */
-#define PEFE_FILTERAMOUNT	0x0800001a
-#define FMMOD			0x1b		/* Vibrato/filter modulation from LFO register		*/
-#define FMMOD_MODVIBRATO	0x0000ff00	/* Vibrato LFO modulation depth				*/
-						/* Signed 2's complement, +/- one octave extremes	*/
-#define FMMOD_MOFILTER		0x000000ff	/* Filter LFO modulation depth				*/
-						/* Signed 2's complement, +/- three octave extremes	*/
-
-
-#define TREMFRQ 		0x1c		/* Tremolo amount and modulation LFO frequency register	*/
-#define TREMFRQ_DEPTH		0x0000ff00	/* Tremolo depth					*/
-						/* Signed 2's complement, with +/- 12dB extremes	*/
-
-#define TREMFRQ_FREQUENCY	0x000000ff	/* Tremolo LFO frequency				*/
-						/* ??Hz steps, maximum of ?? Hz.			*/
-#define FM2FRQ2 		0x1d		/* Vibrato amount and vibrato LFO frequency register	*/
-#define FM2FRQ2_DEPTH		0x0000ff00	/* Vibrato LFO vibrato depth				*/
-						/* Signed 2's complement, +/- one octave extremes	*/
-#define FM2FRQ2_FREQUENCY	0x000000ff	/* Vibrato LFO frequency				*/
-						/* 0.039Hz steps, maximum of 9.85 Hz.			*/
-
-#define TEMPENV 		0x1e		/* Tempory envelope register				*/
-#define TEMPENV_MASK		0x0000ffff	/* 16-bit value						*/
-						/* NOTE: All channels contain internal variables; do	*/
-						/* not write to these locations.			*/
-
-/* 1f something */
-
-#define CD0			0x20		/* Cache data 0 register				*/
-#define CD1			0x21		/* Cache data 1 register				*/
-#define CD2			0x22		/* Cache data 2 register				*/
-#define CD3			0x23		/* Cache data 3 register				*/
-#define CD4			0x24		/* Cache data 4 register				*/
-#define CD5			0x25		/* Cache data 5 register				*/
-#define CD6			0x26		/* Cache data 6 register				*/
-#define CD7			0x27		/* Cache data 7 register				*/
-#define CD8			0x28		/* Cache data 8 register				*/
-#define CD9			0x29		/* Cache data 9 register				*/
-#define CDA			0x2a		/* Cache data A register				*/
-#define CDB			0x2b		/* Cache data B register				*/
-#define CDC			0x2c		/* Cache data C register				*/
-#define CDD			0x2d		/* Cache data D register				*/
-#define CDE			0x2e		/* Cache data E register				*/
-#define CDF			0x2f		/* Cache data F register				*/
-
-/* 0x30-3f seem to be the same as 0x20-2f */
-
-#define PTB			0x40		/* Page table base register				*/
-#define PTB_MASK		0xfffff000	/* Physical address of the page table in host memory	*/
-
-#define TCB			0x41		/* Tank cache base register    				*/
-#define TCB_MASK		0xfffff000	/* Physical address of the bottom of host based TRAM	*/
-
-#define ADCCR			0x42		/* ADC sample rate/stereo control register		*/
-#define ADCCR_RCHANENABLE	0x00000010	/* Enables right channel for writing to the host       	*/
-#define ADCCR_LCHANENABLE	0x00000008	/* Enables left channel for writing to the host		*/
-						/* NOTE: To guarantee phase coherency, both channels	*/
-						/* must be disabled prior to enabling both channels.	*/
-#define A_ADCCR_RCHANENABLE	0x00000020
-#define A_ADCCR_LCHANENABLE	0x00000010
-
-#define A_ADCCR_SAMPLERATE_MASK 0x0000000F      /* Audigy sample rate convertor output rate		*/
-#define ADCCR_SAMPLERATE_MASK	0x00000007	/* Sample rate convertor output rate			*/
-#define ADCCR_SAMPLERATE_48	0x00000000	/* 48kHz sample rate					*/
-#define ADCCR_SAMPLERATE_44	0x00000001	/* 44.1kHz sample rate					*/
-#define ADCCR_SAMPLERATE_32	0x00000002	/* 32kHz sample rate					*/
-#define ADCCR_SAMPLERATE_24	0x00000003	/* 24kHz sample rate					*/
-#define ADCCR_SAMPLERATE_22	0x00000004	/* 22.05kHz sample rate					*/
-#define ADCCR_SAMPLERATE_16	0x00000005	/* 16kHz sample rate					*/
-#define ADCCR_SAMPLERATE_11	0x00000006	/* 11.025kHz sample rate				*/
-#define ADCCR_SAMPLERATE_8	0x00000007	/* 8kHz sample rate					*/
-#define A_ADCCR_SAMPLERATE_12	0x00000006	/* 12kHz sample rate					*/
-#define A_ADCCR_SAMPLERATE_11	0x00000007	/* 11.025kHz sample rate				*/
-#define A_ADCCR_SAMPLERATE_8	0x00000008	/* 8kHz sample rate					*/
-
-#define FXWC			0x43		/* FX output write channels register			*/
-						/* When set, each bit enables the writing of the	*/
-						/* corresponding FX output channel (internal registers  */
-						/* 0x20-0x3f) to host memory.  This mode of recording   */
-						/* is 16bit, 48KHz only. All 32 channels can be enabled */
-						/* simultaneously.					*/
-
-#define FXWC_DEFAULTROUTE_C     (1<<0)		/* left emu out? */
-#define FXWC_DEFAULTROUTE_B     (1<<1)		/* right emu out? */
-#define FXWC_DEFAULTROUTE_A     (1<<12)
-#define FXWC_DEFAULTROUTE_D     (1<<13)
-#define FXWC_ADCLEFT            (1<<18)
-#define FXWC_CDROMSPDIFLEFT     (1<<18)
-#define FXWC_ADCRIGHT           (1<<19)
-#define FXWC_CDROMSPDIFRIGHT    (1<<19)
-#define FXWC_MIC                (1<<20)
-#define FXWC_ZOOMLEFT           (1<<20)
-#define FXWC_ZOOMRIGHT          (1<<21)
-#define FXWC_SPDIFLEFT          (1<<22)		/* 0x00400000 */
-#define FXWC_SPDIFRIGHT         (1<<23)		/* 0x00800000 */
-
-#define TCBS			0x44		/* Tank cache buffer size register			*/
-#define TCBS_MASK		0x00000007	/* Tank cache buffer size field				*/
-#define TCBS_BUFFSIZE_16K	0x00000000
-#define TCBS_BUFFSIZE_32K	0x00000001
-#define TCBS_BUFFSIZE_64K	0x00000002
-#define TCBS_BUFFSIZE_128K	0x00000003
-#define TCBS_BUFFSIZE_256K	0x00000004
-#define TCBS_BUFFSIZE_512K	0x00000005
-#define TCBS_BUFFSIZE_1024K	0x00000006
-#define TCBS_BUFFSIZE_2048K	0x00000007
-
-#define MICBA			0x45		/* AC97 microphone buffer address register		*/
-#define MICBA_MASK		0xfffff000	/* 20 bit base address					*/
-
-#define ADCBA			0x46		/* ADC buffer address register				*/
-#define ADCBA_MASK		0xfffff000	/* 20 bit base address					*/
-
-#define FXBA			0x47		/* FX Buffer Address */
-#define FXBA_MASK		0xfffff000	/* 20 bit base address					*/
-
-/* 0x48 something - word access, defaults to 3f */
-
-#define MICBS			0x49		/* Microphone buffer size register			*/
-
-#define ADCBS			0x4a		/* ADC buffer size register				*/
-
-#define FXBS			0x4b		/* FX buffer size register				*/
-
-/* register: 0x4c..4f: ffff-ffff current amounts, per-channel */
-
-/* The following mask values define the size of the ADC, MIX and FX buffers in bytes */
-#define ADCBS_BUFSIZE_NONE	0x00000000
-#define ADCBS_BUFSIZE_384	0x00000001
-#define ADCBS_BUFSIZE_448	0x00000002
-#define ADCBS_BUFSIZE_512	0x00000003
-#define ADCBS_BUFSIZE_640	0x00000004
-#define ADCBS_BUFSIZE_768	0x00000005
-#define ADCBS_BUFSIZE_896	0x00000006
-#define ADCBS_BUFSIZE_1024	0x00000007
-#define ADCBS_BUFSIZE_1280	0x00000008
-#define ADCBS_BUFSIZE_1536	0x00000009
-#define ADCBS_BUFSIZE_1792	0x0000000a
-#define ADCBS_BUFSIZE_2048	0x0000000b
-#define ADCBS_BUFSIZE_2560	0x0000000c
-#define ADCBS_BUFSIZE_3072	0x0000000d
-#define ADCBS_BUFSIZE_3584	0x0000000e
-#define ADCBS_BUFSIZE_4096	0x0000000f
-#define ADCBS_BUFSIZE_5120	0x00000010
-#define ADCBS_BUFSIZE_6144	0x00000011
-#define ADCBS_BUFSIZE_7168	0x00000012
-#define ADCBS_BUFSIZE_8192	0x00000013
-#define ADCBS_BUFSIZE_10240	0x00000014
-#define ADCBS_BUFSIZE_12288	0x00000015
-#define ADCBS_BUFSIZE_14366	0x00000016
-#define ADCBS_BUFSIZE_16384	0x00000017
-#define ADCBS_BUFSIZE_20480	0x00000018
-#define ADCBS_BUFSIZE_24576	0x00000019
-#define ADCBS_BUFSIZE_28672	0x0000001a
-#define ADCBS_BUFSIZE_32768	0x0000001b
-#define ADCBS_BUFSIZE_40960	0x0000001c
-#define ADCBS_BUFSIZE_49152	0x0000001d
-#define ADCBS_BUFSIZE_57344	0x0000001e
-#define ADCBS_BUFSIZE_65536	0x0000001f
-
-
-#define CDCS			0x50		/* CD-ROM digital channel status register	*/
-
-#define GPSCS			0x51		/* General Purpose SPDIF channel status register*/
-
-#define DBG			0x52		/* DO NOT PROGRAM THIS REGISTER!!! MAY DESTROY CHIP */
-
-#define REG53			0x53		/* DO NOT PROGRAM THIS REGISTER!!! MAY DESTROY CHIP */
-
-#define A_DBG			 0x53
-#define A_DBG_SINGLE_STEP	 0x00020000	/* Set to zero to start dsp */
-#define A_DBG_ZC		 0x40000000	/* zero tram counter */
-#define A_DBG_STEP_ADDR		 0x000003ff
-#define A_DBG_SATURATION_OCCURED 0x20000000
-#define A_DBG_SATURATION_ADDR	 0x0ffc0000
-
-// NOTE: 0x54,55,56: 64-bit
-#define SPCS0			0x54		/* SPDIF output Channel Status 0 register	*/
-
-#define SPCS1			0x55		/* SPDIF output Channel Status 1 register	*/
-
-#define SPCS2			0x56		/* SPDIF output Channel Status 2 register	*/
-
-#define SPCS_CLKACCYMASK	0x30000000	/* Clock accuracy				*/
-#define SPCS_CLKACCY_1000PPM	0x00000000	/* 1000 parts per million			*/
-#define SPCS_CLKACCY_50PPM	0x10000000	/* 50 parts per million				*/
-#define SPCS_CLKACCY_VARIABLE	0x20000000	/* Variable accuracy				*/
-#define SPCS_SAMPLERATEMASK	0x0f000000	/* Sample rate					*/
-#define SPCS_SAMPLERATE_44	0x00000000	/* 44.1kHz sample rate				*/
-#define SPCS_SAMPLERATE_48	0x02000000	/* 48kHz sample rate				*/
-#define SPCS_SAMPLERATE_32	0x03000000	/* 32kHz sample rate				*/
-#define SPCS_CHANNELNUMMASK	0x00f00000	/* Channel number				*/
-#define SPCS_CHANNELNUM_UNSPEC	0x00000000	/* Unspecified channel number			*/
-#define SPCS_CHANNELNUM_LEFT	0x00100000	/* Left channel					*/
-#define SPCS_CHANNELNUM_RIGHT	0x00200000	/* Right channel				*/
-#define SPCS_SOURCENUMMASK	0x000f0000	/* Source number				*/
-#define SPCS_SOURCENUM_UNSPEC	0x00000000	/* Unspecified source number			*/
-#define SPCS_GENERATIONSTATUS	0x00008000	/* Originality flag (see IEC-958 spec)		*/
-#define SPCS_CATEGORYCODEMASK	0x00007f00	/* Category code (see IEC-958 spec)		*/
-#define SPCS_MODEMASK		0x000000c0	/* Mode (see IEC-958 spec)			*/
-#define SPCS_EMPHASISMASK	0x00000038	/* Emphasis					*/
-#define SPCS_EMPHASIS_NONE	0x00000000	/* No emphasis					*/
-#define SPCS_EMPHASIS_50_15	0x00000008	/* 50/15 usec 2 channel				*/
-#define SPCS_COPYRIGHT		0x00000004	/* Copyright asserted flag -- do not modify	*/
-#define SPCS_NOTAUDIODATA	0x00000002	/* 0 = Digital audio, 1 = not audio		*/
-#define SPCS_PROFESSIONAL	0x00000001	/* 0 = Consumer (IEC-958), 1 = pro (AES3-1992)	*/
-
-/* The 32-bit CLIx and SOLx registers all have one bit per channel control/status      		*/
-#define CLIEL			0x58		/* Channel loop interrupt enable low register	*/
-
-#define CLIEH			0x59		/* Channel loop interrupt enable high register	*/
-
-#define CLIPL			0x5a		/* Channel loop interrupt pending low register	*/
-
-#define CLIPH			0x5b		/* Channel loop interrupt pending high register	*/
-
-#define SOLEL			0x5c		/* Stop on loop enable low register		*/
-
-#define SOLEH			0x5d		/* Stop on loop enable high register		*/
-
-#define SPBYPASS		0x5e		/* SPDIF BYPASS mode register			*/
-#define SPBYPASS_SPDIF0_MASK	0x00000003	/* SPDIF 0 bypass mode				*/
-#define SPBYPASS_SPDIF1_MASK	0x0000000c	/* SPDIF 1 bypass mode				*/
-/* bypass mode: 0 - DSP; 1 - SPDIF A, 2 - SPDIF B, 3 - SPDIF C					*/
-#define SPBYPASS_FORMAT		0x00000f00      /* If 1, SPDIF XX uses 24 bit, if 0 - 20 bit	*/
-
-#define AC97SLOT		0x5f            /* additional AC97 slots enable bits		*/
-#define AC97SLOT_REAR_RIGHT	0x01		/* Rear left */
-#define AC97SLOT_REAR_LEFT	0x02		/* Rear right */
-#define AC97SLOT_CNTR		0x10            /* Center enable */
-#define AC97SLOT_LFE		0x20            /* LFE enable */
-
-// NOTE: 0x60,61,62: 64-bit
-#define CDSRCS			0x60		/* CD-ROM Sample Rate Converter status register	*/
-
-#define GPSRCS			0x61		/* General Purpose SPDIF sample rate cvt status */
-
-#define ZVSRCS			0x62		/* ZVideo sample rate converter status		*/
-						/* NOTE: This one has no SPDIFLOCKED field	*/
-						/* Assumes sample lock				*/
-
-/* These three bitfields apply to CDSRCS, GPSRCS, and (except as noted) ZVSRCS.			*/
-#define SRCS_SPDIFVALID		0x04000000	/* SPDIF stream valid				*/
-#define SRCS_SPDIFLOCKED	0x02000000	/* SPDIF stream locked				*/
-#define SRCS_RATELOCKED		0x01000000	/* Sample rate locked				*/
-#define SRCS_ESTSAMPLERATE	0x0007ffff	/* Do not modify this field.			*/
-
-/* Note that these values can vary +/- by a small amount                                        */
-#define SRCS_SPDIFRATE_44	0x0003acd9
-#define SRCS_SPDIFRATE_48	0x00040000
-#define SRCS_SPDIFRATE_96	0x00080000
-
-#define MICIDX                  0x63            /* Microphone recording buffer index register   */
-#define MICIDX_MASK             0x0000ffff      /* 16-bit value                                 */
-#define MICIDX_IDX		0x10000063
-
-#define ADCIDX			0x64		/* ADC recording buffer index register		*/
-#define ADCIDX_MASK		0x0000ffff	/* 16 bit index field				*/
-#define ADCIDX_IDX		0x10000064
-
-#define A_ADCIDX		0x63
-#define A_ADCIDX_IDX		0x10000063
-
-#define A_MICIDX		0x64
-#define A_MICIDX_IDX		0x10000064
-
-#define FXIDX			0x65		/* FX recording buffer index register		*/
-#define FXIDX_MASK		0x0000ffff	/* 16-bit value					*/
-#define FXIDX_IDX		0x10000065
-
-/* The 32-bit HLIx and HLIPx registers all have one bit per channel control/status      		*/
-#define HLIEL			0x66		/* Channel half loop interrupt enable low register	*/
-
-#define HLIEH			0x67		/* Channel half loop interrupt enable high register	*/
-
-#define HLIPL			0x68		/* Channel half loop interrupt pending low register	*/
-
-#define HLIPH			0x69		/* Channel half loop interrupt pending high register	*/
-
-// 0x6a,6b,6c used for some recording
-// 0x6d unused
-// 0x6e,6f - tanktable base / offset
-
-/* This is the MPU port on the card (via the game port)						*/
-#define A_MUDATA1		0x70
-#define A_MUCMD1		0x71
-#define A_MUSTAT1		A_MUCMD1
-
-/* This is the MPU port on the Audigy Drive 							*/
-#define A_MUDATA2		0x72
-#define A_MUCMD2		0x73
-#define A_MUSTAT2		A_MUCMD2	
-
-/* The next two are the Audigy equivalent of FXWC						*/
-/* the Audigy can record any output (16bit, 48kHz, up to 64 channel simultaneously) 		*/
-/* Each bit selects a channel for recording */
-#define A_FXWC1			0x74            /* Selects 0x7f-0x60 for FX recording           */
-#define A_FXWC2			0x75		/* Selects 0x9f-0x80 for FX recording           */
-
-#define A_SPDIF_SAMPLERATE	0x76		/* Set the sample rate of SPDIF output		*/
-#define A_SAMPLE_RATE		0x76		/* Various sample rate settings. */
-#define A_SAMPLE_RATE_NOT_USED  0x0ffc111e	/* Bits that are not used and cannot be set. 	*/
-#define A_SAMPLE_RATE_UNKNOWN	0xf0030001	/* Bits that can be set, but have unknown use. 	*/
-#define A_SPDIF_RATE_MASK	0x000000e0	/* Any other values for rates, just use 48000	*/
-#define A_SPDIF_48000		0x00000000
-#define A_SPDIF_192000		0x00000020
-#define A_SPDIF_96000		0x00000040
-#define A_SPDIF_44100		0x00000080
-
-#define A_I2S_CAPTURE_RATE_MASK	0x00000e00	/* This sets the capture PCM rate, but it is    */
-#define A_I2S_CAPTURE_48000	0x00000000	/* unclear if this sets the ADC rate as well.	*/
-#define A_I2S_CAPTURE_192000	0x00000200
-#define A_I2S_CAPTURE_96000	0x00000400
-#define A_I2S_CAPTURE_44100	0x00000800
-
-#define A_PCM_RATE_MASK		0x0000e000	/* This sets the playback PCM rate on the P16V	*/
-#define A_PCM_48000		0x00000000
-#define A_PCM_192000		0x00002000
-#define A_PCM_96000		0x00004000
-#define A_PCM_44100		0x00008000
-
-/* 0x77,0x78,0x79 "something i2s-related" - default to 0x01080000 on my audigy 2 ZS --rlrevell	*/
-/* 0x7a, 0x7b - lookup tables */
-
-#define A_FXRT2			0x7c
-#define A_FXRT_CHANNELE		0x0000003f	/* Effects send bus number for channel's effects send E	*/
-#define A_FXRT_CHANNELF		0x00003f00	/* Effects send bus number for channel's effects send F	*/
-#define A_FXRT_CHANNELG		0x003f0000	/* Effects send bus number for channel's effects send G	*/
-#define A_FXRT_CHANNELH		0x3f000000	/* Effects send bus number for channel's effects send H	*/
-
-#define A_SENDAMOUNTS		0x7d
-#define A_FXSENDAMOUNT_E_MASK	0xFF000000
-#define A_FXSENDAMOUNT_F_MASK	0x00FF0000
-#define A_FXSENDAMOUNT_G_MASK	0x0000FF00
-#define A_FXSENDAMOUNT_H_MASK	0x000000FF
-/* 0x7c, 0x7e "high bit is used for filtering" */
- 
-/* The send amounts for this one are the same as used with the emu10k1 */
-#define A_FXRT1			0x7e
-#define A_FXRT_CHANNELA		0x0000003f
-#define A_FXRT_CHANNELB		0x00003f00
-#define A_FXRT_CHANNELC		0x003f0000
-#define A_FXRT_CHANNELD		0x3f000000
-
-
-/* Each FX general purpose register is 32 bits in length, all bits are used			*/
-#define FXGPREGBASE		0x100		/* FX general purpose registers base       	*/
-#define A_FXGPREGBASE		0x400		/* Audigy GPRs, 0x400 to 0x5ff			*/
-
-#define A_TANKMEMCTLREGBASE	0x100		/* Tank memory control registers base - only for Audigy */
-#define A_TANKMEMCTLREG_MASK	0x1f		/* only 5 bits used - only for Audigy */
-
-/* Tank audio data is logarithmically compressed down to 16 bits before writing to TRAM and is	*/
-/* decompressed back to 20 bits on a read.  There are a total of 160 locations, the last 32	*/
-/* locations are for external TRAM. 								*/
-#define TANKMEMDATAREGBASE	0x200		/* Tank memory data registers base     		*/
-#define TANKMEMDATAREG_MASK	0x000fffff	/* 20 bit tank audio data field			*/
-
-/* Combined address field and memory opcode or flag field.  160 locations, last 32 are external	*/
-#define TANKMEMADDRREGBASE	0x300		/* Tank memory address registers base		*/
-#define TANKMEMADDRREG_ADDR_MASK 0x000fffff	/* 20 bit tank address field			*/
-#define TANKMEMADDRREG_CLEAR	0x00800000	/* Clear tank memory				*/
-#define TANKMEMADDRREG_ALIGN	0x00400000	/* Align read or write relative to tank access	*/
-#define TANKMEMADDRREG_WRITE	0x00200000	/* Write to tank memory				*/
-#define TANKMEMADDRREG_READ	0x00100000	/* Read from tank memory			*/
-
-#define MICROCODEBASE		0x400		/* Microcode data base address			*/
-
-/* Each DSP microcode instruction is mapped into 2 doublewords 					*/
-/* NOTE: When writing, always write the LO doubleword first.  Reads can be in either order.	*/
-#define LOWORD_OPX_MASK		0x000ffc00	/* Instruction operand X			*/
-#define LOWORD_OPY_MASK		0x000003ff	/* Instruction operand Y			*/
-#define HIWORD_OPCODE_MASK	0x00f00000	/* Instruction opcode				*/
-#define HIWORD_RESULT_MASK	0x000ffc00	/* Instruction result				*/
-#define HIWORD_OPA_MASK		0x000003ff	/* Instruction operand A			*/
-
-
-/* Audigy Soundcard have a different instruction format */
-#define A_MICROCODEBASE		0x600
-#define A_LOWORD_OPY_MASK	0x000007ff
-#define A_LOWORD_OPX_MASK	0x007ff000
-#define A_HIWORD_OPCODE_MASK	0x0f000000
-#define A_HIWORD_RESULT_MASK	0x007ff000
-#define A_HIWORD_OPA_MASK	0x000007ff
-
-
-/* ------------------- STRUCTURES -------------------- */
-
-enum {
-	EMU10K1_EFX,
-	EMU10K1_PCM,
-	EMU10K1_SYNTH,
-	EMU10K1_MIDI
-};
-
-struct snd_emu10k1;
-
-struct snd_emu10k1_voice {
-	struct snd_emu10k1 *emu;
-	int number;
-	unsigned int use: 1,
-	    pcm: 1,
-	    efx: 1,
-	    synth: 1,
-	    midi: 1;
-	void (*interrupt)(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *pvoice);
-
-	struct snd_emu10k1_pcm *epcm;
-};
-
-enum {
-	PLAYBACK_EMUVOICE,
-	PLAYBACK_EFX,
-	CAPTURE_AC97ADC,
-	CAPTURE_AC97MIC,
-	CAPTURE_EFX
-};
-
-struct snd_emu10k1_pcm {
-	struct snd_emu10k1 *emu;
-	int type;
-	struct snd_pcm_substream *substream;
-	struct snd_emu10k1_voice *voices[NUM_EFX_PLAYBACK];
-	struct snd_emu10k1_voice *extra;
-	unsigned short running;
-	unsigned short first_ptr;
-	struct snd_util_memblk *memblk;
-	unsigned int start_addr;
-	unsigned int ccca_start_addr;
-	unsigned int capture_ipr;	/* interrupt acknowledge mask */
-	unsigned int capture_inte;	/* interrupt enable mask */
-	unsigned int capture_ba_reg;	/* buffer address register */
-	unsigned int capture_bs_reg;	/* buffer size register */
-	unsigned int capture_idx_reg;	/* buffer index register */
-	unsigned int capture_cr_val;	/* control value */
-	unsigned int capture_cr_val2;	/* control value2 (for audigy) */
-	unsigned int capture_bs_val;	/* buffer size value */
-	unsigned int capture_bufsize;	/* buffer size in bytes */
-};
-
-struct snd_emu10k1_pcm_mixer {
-	/* mono, left, right x 8 sends (4 on emu10k1) */
-	unsigned char send_routing[3][8];
-	unsigned char send_volume[3][8];
-	unsigned short attn[3];
-	struct snd_emu10k1_pcm *epcm;
-};
-
-#define snd_emu10k1_compose_send_routing(route) \
-((route[0] | (route[1] << 4) | (route[2] << 8) | (route[3] << 12)) << 16)
-
-#define snd_emu10k1_compose_audigy_fxrt1(route) \
-((unsigned int)route[0] | ((unsigned int)route[1] << 8) | ((unsigned int)route[2] << 16) | ((unsigned int)route[3] << 24))
-
-#define snd_emu10k1_compose_audigy_fxrt2(route) \
-((unsigned int)route[4] | ((unsigned int)route[5] << 8) | ((unsigned int)route[6] << 16) | ((unsigned int)route[7] << 24))
-
-struct snd_emu10k1_memblk {
-	struct snd_util_memblk mem;
-	/* private part */
-	int first_page, last_page, pages, mapped_page;
-	unsigned int map_locked;
-	struct list_head mapped_link;
-	struct list_head mapped_order_link;
-};
-
-#define snd_emu10k1_memblk_offset(blk)	(((blk)->mapped_page << PAGE_SHIFT) | ((blk)->mem.offset & (PAGE_SIZE - 1)))
-
-#define EMU10K1_MAX_TRAM_BLOCKS_PER_CODE	16
-
-struct snd_emu10k1_fx8010_ctl {
-	struct list_head list;		/* list link container */
-	unsigned int vcount;
-	unsigned int count;		/* count of GPR (1..16) */
-	unsigned short gpr[32];		/* GPR number(s) */
-	unsigned int value[32];
-	unsigned int min;		/* minimum range */
-	unsigned int max;		/* maximum range */
-	unsigned int translation;	/* translation type (EMU10K1_GPR_TRANSLATION*) */
-	struct snd_kcontrol *kcontrol;
-};
-
-typedef void (snd_fx8010_irq_handler_t)(struct snd_emu10k1 *emu, void *private_data);
-
-struct snd_emu10k1_fx8010_irq {
-	struct snd_emu10k1_fx8010_irq *next;
-	snd_fx8010_irq_handler_t *handler;
-	unsigned short gpr_running;
-	void *private_data;
-};
-
-struct snd_emu10k1_fx8010_pcm {
-	unsigned int valid: 1,
-		     opened: 1,
-		     active: 1;
-	unsigned int channels;		/* 16-bit channels count */
-	unsigned int tram_start;	/* initial ring buffer position in TRAM (in samples) */
-	unsigned int buffer_size;	/* count of buffered samples */
-	unsigned short gpr_size;		/* GPR containing size of ring buffer in samples (host) */
-	unsigned short gpr_ptr;		/* GPR containing current pointer in the ring buffer (host = reset, FX8010) */
-	unsigned short gpr_count;	/* GPR containing count of samples between two interrupts (host) */
-	unsigned short gpr_tmpcount;	/* GPR containing current count of samples to interrupt (host = set, FX8010) */
-	unsigned short gpr_trigger;	/* GPR containing trigger (activate) information (host) */
-	unsigned short gpr_running;	/* GPR containing info if PCM is running (FX8010) */
-	unsigned char etram[32];	/* external TRAM address & data */
-	struct snd_pcm_indirect pcm_rec;
-	unsigned int tram_pos;
-	unsigned int tram_shift;
-	struct snd_emu10k1_fx8010_irq *irq;
-};
-
-struct snd_emu10k1_fx8010 {
-	unsigned short fxbus_mask;	/* used FX buses (bitmask) */
-	unsigned short extin_mask;	/* used external inputs (bitmask) */
-	unsigned short extout_mask;	/* used external outputs (bitmask) */
-	unsigned short pad1;
-	unsigned int itram_size;	/* internal TRAM size in samples */
-	struct snd_dma_buffer etram_pages; /* external TRAM pages and size */
-	unsigned int dbg;		/* FX debugger register */
-	unsigned char name[128];
-	int gpr_size;			/* size of allocated GPR controls */
-	int gpr_count;			/* count of used kcontrols */
-	struct list_head gpr_ctl;	/* GPR controls */
-	struct mutex lock;
-	struct snd_emu10k1_fx8010_pcm pcm[8];
-	spinlock_t irq_lock;
-	struct snd_emu10k1_fx8010_irq *irq_handlers;
-};
-
-#define emu10k1_gpr_ctl(n) list_entry(n, struct snd_emu10k1_fx8010_ctl, list)
-
-struct snd_emu10k1_midi {
-	struct snd_emu10k1 *emu;
-	struct snd_rawmidi *rmidi;
-	struct snd_rawmidi_substream *substream_input;
-	struct snd_rawmidi_substream *substream_output;
-	unsigned int midi_mode;
-	spinlock_t input_lock;
-	spinlock_t output_lock;
-	spinlock_t open_lock;
-	int tx_enable, rx_enable;
-	int port;
-	int ipr_tx, ipr_rx;
-	void (*interrupt)(struct snd_emu10k1 *emu, unsigned int status);
-};
-
-struct snd_emu_chip_details {
-	u32 vendor;
-	u32 device;
-	u32 subsystem;
-	unsigned char revision;
-	unsigned char emu10k1_chip; /* Original SB Live. Not SB Live 24bit. */
-	unsigned char emu10k2_chip; /* Audigy 1 or Audigy 2. */
-	unsigned char ca0102_chip;  /* Audigy 1 or Audigy 2. Not SB Audigy 2 Value. */
-	unsigned char ca0108_chip;  /* Audigy 2 Value */
-	unsigned char ca_cardbus_chip; /* Audigy 2 ZS Notebook */
-	unsigned char ca0151_chip;  /* P16V */
-	unsigned char spk71;        /* Has 7.1 speakers */
-	unsigned char sblive51;	    /* SBLive! 5.1 - extout 0x11 -> center, 0x12 -> lfe */
-	unsigned char spdif_bug;    /* Has Spdif phasing bug */
-	unsigned char ac97_chip;    /* Has an AC97 chip: 1 = mandatory, 2 = optional */
-	unsigned char ecard;        /* APS EEPROM */
-	unsigned char emu1212m;     /* EMU 1212m card */
-	unsigned char spi_dac;      /* SPI interface for DAC */
-	unsigned char i2c_adc;      /* I2C interface for ADC */
-	unsigned char adc_1361t;    /* Use Philips 1361T ADC */
-	const char *driver;
-	const char *name;
-	const char *id;		/* for backward compatibility - can be NULL if not needed */
-};
-
-struct snd_emu10k1 {
-	int irq;
-
-	unsigned long port;			/* I/O port number */
-	unsigned int tos_link: 1,		/* tos link detected */
-		rear_ac97: 1,			/* rear channels are on AC'97 */
-		enable_ir: 1;
-	/* Contains profile of card capabilities */
-	const struct snd_emu_chip_details *card_capabilities;
-	unsigned int audigy;			/* is Audigy? */
-	unsigned int revision;			/* chip revision */
-	unsigned int serial;			/* serial number */
-	unsigned short model;			/* subsystem id */
-	unsigned int card_type;			/* EMU10K1_CARD_* */
-	unsigned int ecard_ctrl;		/* ecard control bits */
-	unsigned long dma_mask;			/* PCI DMA mask */
-	int max_cache_pages;			/* max memory size / PAGE_SIZE */
-	struct snd_dma_buffer silent_page;	/* silent page */
-	struct snd_dma_buffer ptb_pages;	/* page table pages */
-	struct snd_dma_device p16v_dma_dev;
-	struct snd_dma_buffer p16v_buffer;
-
-	struct snd_util_memhdr *memhdr;		/* page allocation list */
-	struct snd_emu10k1_memblk *reserved_page;	/* reserved page */
-
-	struct list_head mapped_link_head;
-	struct list_head mapped_order_link_head;
-	void **page_ptr_table;
-	unsigned long *page_addr_table;
-	spinlock_t memblk_lock;
-
-	unsigned int spdif_bits[3];		/* s/pdif out setup */
-
-	struct snd_emu10k1_fx8010 fx8010;		/* FX8010 info */
-	int gpr_base;
-	
-	struct snd_ac97 *ac97;
-
-	struct pci_dev *pci;
-	struct snd_card *card;
-	struct snd_pcm *pcm;
-	struct snd_pcm *pcm_mic;
-	struct snd_pcm *pcm_efx;
-	struct snd_pcm *pcm_multi;
-	struct snd_pcm *pcm_p16v;
-
-	spinlock_t synth_lock;
-	void *synth;
-	int (*get_synth_voice)(struct snd_emu10k1 *emu);
-
-	spinlock_t reg_lock;
-	spinlock_t emu_lock;
-	spinlock_t voice_lock;
-
-	struct snd_emu10k1_voice voices[NUM_G];
-	struct snd_emu10k1_voice p16v_voices[4];
-	struct snd_emu10k1_voice p16v_capture_voice;
-	int p16v_device_offset;
-	u32 p16v_capture_source;
-	u32 p16v_capture_channel;
-	struct snd_emu10k1_pcm_mixer pcm_mixer[32];
-	struct snd_emu10k1_pcm_mixer efx_pcm_mixer[NUM_EFX_PLAYBACK];
-	struct snd_kcontrol *ctl_send_routing;
-	struct snd_kcontrol *ctl_send_volume;
-	struct snd_kcontrol *ctl_attn;
-	struct snd_kcontrol *ctl_efx_send_routing;
-	struct snd_kcontrol *ctl_efx_send_volume;
-	struct snd_kcontrol *ctl_efx_attn;
-
-	void (*hwvol_interrupt)(struct snd_emu10k1 *emu, unsigned int status);
-	void (*capture_interrupt)(struct snd_emu10k1 *emu, unsigned int status);
-	void (*capture_mic_interrupt)(struct snd_emu10k1 *emu, unsigned int status);
-	void (*capture_efx_interrupt)(struct snd_emu10k1 *emu, unsigned int status);
-	void (*spdif_interrupt)(struct snd_emu10k1 *emu, unsigned int status);
-	void (*dsp_interrupt)(struct snd_emu10k1 *emu);
-
-	struct snd_pcm_substream *pcm_capture_substream;
-	struct snd_pcm_substream *pcm_capture_mic_substream;
-	struct snd_pcm_substream *pcm_capture_efx_substream;
-	struct snd_pcm_substream *pcm_playback_efx_substream;
-
-	struct snd_timer *timer;
-
-	struct snd_emu10k1_midi midi;
-	struct snd_emu10k1_midi midi2; /* for audigy */
-
-	unsigned int efx_voices_mask[2];
-	unsigned int next_free_voice;
-
-#ifdef CONFIG_PM
-	unsigned int *saved_ptr;
-	unsigned int *saved_gpr;
-	unsigned int *tram_val_saved;
-	unsigned int *tram_addr_saved;
-	unsigned int *saved_icode;
-	unsigned int *p16v_saved;
-	unsigned int saved_a_iocfg, saved_hcfg;
-#endif
-
-};
-
-int snd_emu10k1_create(struct snd_card *card,
-		       struct pci_dev *pci,
-		       unsigned short extin_mask,
-		       unsigned short extout_mask,
-		       long max_cache_bytes,
-		       int enable_ir,
-		       uint subsystem,
-		       struct snd_emu10k1 ** remu);
-
-int snd_emu10k1_pcm(struct snd_emu10k1 * emu, int device, struct snd_pcm ** rpcm);
-int snd_emu10k1_pcm_mic(struct snd_emu10k1 * emu, int device, struct snd_pcm ** rpcm);
-int snd_emu10k1_pcm_efx(struct snd_emu10k1 * emu, int device, struct snd_pcm ** rpcm);
-int snd_p16v_pcm(struct snd_emu10k1 * emu, int device, struct snd_pcm ** rpcm);
-int snd_p16v_free(struct snd_emu10k1 * emu);
-int snd_p16v_mixer(struct snd_emu10k1 * emu);
-int snd_emu10k1_pcm_multi(struct snd_emu10k1 * emu, int device, struct snd_pcm ** rpcm);
-int snd_emu10k1_fx8010_pcm(struct snd_emu10k1 * emu, int device, struct snd_pcm ** rpcm);
-int snd_emu10k1_mixer(struct snd_emu10k1 * emu, int pcm_device, int multi_device);
-int snd_emu10k1_timer(struct snd_emu10k1 * emu, int device);
-int snd_emu10k1_fx8010_new(struct snd_emu10k1 *emu, int device, struct snd_hwdep ** rhwdep);
-
-irqreturn_t snd_emu10k1_interrupt(int irq, void *dev_id, struct pt_regs *regs);
-
-void snd_emu10k1_voice_init(struct snd_emu10k1 * emu, int voice);
-int snd_emu10k1_init_efx(struct snd_emu10k1 *emu);
-void snd_emu10k1_free_efx(struct snd_emu10k1 *emu);
-int snd_emu10k1_fx8010_tram_setup(struct snd_emu10k1 *emu, u32 size);
-int snd_emu10k1_done(struct snd_emu10k1 * emu);
-
-/* I/O functions */
-unsigned int snd_emu10k1_ptr_read(struct snd_emu10k1 * emu, unsigned int reg, unsigned int chn);
-void snd_emu10k1_ptr_write(struct snd_emu10k1 *emu, unsigned int reg, unsigned int chn, unsigned int data);
-unsigned int snd_emu10k1_ptr20_read(struct snd_emu10k1 * emu, unsigned int reg, unsigned int chn);
-void snd_emu10k1_ptr20_write(struct snd_emu10k1 *emu, unsigned int reg, unsigned int chn, unsigned int data);
-int snd_emu10k1_spi_write(struct snd_emu10k1 * emu, unsigned int data);
-unsigned int snd_emu10k1_efx_read(struct snd_emu10k1 *emu, unsigned int pc);
-void snd_emu10k1_intr_enable(struct snd_emu10k1 *emu, unsigned int intrenb);
-void snd_emu10k1_intr_disable(struct snd_emu10k1 *emu, unsigned int intrenb);
-void snd_emu10k1_voice_intr_enable(struct snd_emu10k1 *emu, unsigned int voicenum);
-void snd_emu10k1_voice_intr_disable(struct snd_emu10k1 *emu, unsigned int voicenum);
-void snd_emu10k1_voice_intr_ack(struct snd_emu10k1 *emu, unsigned int voicenum);
-void snd_emu10k1_voice_half_loop_intr_enable(struct snd_emu10k1 *emu, unsigned int voicenum);
-void snd_emu10k1_voice_half_loop_intr_disable(struct snd_emu10k1 *emu, unsigned int voicenum);
-void snd_emu10k1_voice_half_loop_intr_ack(struct snd_emu10k1 *emu, unsigned int voicenum);
-void snd_emu10k1_voice_set_loop_stop(struct snd_emu10k1 *emu, unsigned int voicenum);
-void snd_emu10k1_voice_clear_loop_stop(struct snd_emu10k1 *emu, unsigned int voicenum);
-void snd_emu10k1_wait(struct snd_emu10k1 *emu, unsigned int wait);
-static inline unsigned int snd_emu10k1_wc(struct snd_emu10k1 *emu) { return (inl(emu->port + WC) >> 6) & 0xfffff; }
-unsigned short snd_emu10k1_ac97_read(struct snd_ac97 *ac97, unsigned short reg);
-void snd_emu10k1_ac97_write(struct snd_ac97 *ac97, unsigned short reg, unsigned short data);
-unsigned int snd_emu10k1_rate_to_pitch(unsigned int rate);
-
-#ifdef CONFIG_PM
-void snd_emu10k1_suspend_regs(struct snd_emu10k1 *emu);
-void snd_emu10k1_resume_init(struct snd_emu10k1 *emu);
-void snd_emu10k1_resume_regs(struct snd_emu10k1 *emu);
-int snd_emu10k1_efx_alloc_pm_buffer(struct snd_emu10k1 *emu);
-void snd_emu10k1_efx_free_pm_buffer(struct snd_emu10k1 *emu);
-void snd_emu10k1_efx_suspend(struct snd_emu10k1 *emu);
-void snd_emu10k1_efx_resume(struct snd_emu10k1 *emu);
-int snd_p16v_alloc_pm_buffer(struct snd_emu10k1 *emu);
-void snd_p16v_free_pm_buffer(struct snd_emu10k1 *emu);
-void snd_p16v_suspend(struct snd_emu10k1 *emu);
-void snd_p16v_resume(struct snd_emu10k1 *emu);
-#endif
-
-/* memory allocation */
-struct snd_util_memblk *snd_emu10k1_alloc_pages(struct snd_emu10k1 *emu, struct snd_pcm_substream *substream);
-int snd_emu10k1_free_pages(struct snd_emu10k1 *emu, struct snd_util_memblk *blk);
-struct snd_util_memblk *snd_emu10k1_synth_alloc(struct snd_emu10k1 *emu, unsigned int size);
-int snd_emu10k1_synth_free(struct snd_emu10k1 *emu, struct snd_util_memblk *blk);
-int snd_emu10k1_synth_bzero(struct snd_emu10k1 *emu, struct snd_util_memblk *blk, int offset, int size);
-int snd_emu10k1_synth_copy_from_user(struct snd_emu10k1 *emu, struct snd_util_memblk *blk, int offset, const char __user *data, int size);
-int snd_emu10k1_memblk_map(struct snd_emu10k1 *emu, struct snd_emu10k1_memblk *blk);
-
-/* voice allocation */
-int snd_emu10k1_voice_alloc(struct snd_emu10k1 *emu, int type, int pair, struct snd_emu10k1_voice **rvoice);
-int snd_emu10k1_voice_free(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *pvoice);
-
-/* MIDI uart */
-int snd_emu10k1_midi(struct snd_emu10k1 * emu);
-int snd_emu10k1_audigy_midi(struct snd_emu10k1 * emu);
-
-/* proc interface */
-int snd_emu10k1_proc_init(struct snd_emu10k1 * emu);
-
-/* fx8010 irq handler */
-int snd_emu10k1_fx8010_register_irq_handler(struct snd_emu10k1 *emu,
-					    snd_fx8010_irq_handler_t *handler,
-					    unsigned char gpr_running,
-					    void *private_data,
-					    struct snd_emu10k1_fx8010_irq **r_irq);
-int snd_emu10k1_fx8010_unregister_irq_handler(struct snd_emu10k1 *emu,
-					      struct snd_emu10k1_fx8010_irq *irq);
-
-#endif /* __KERNEL__ */
-
-/*
- * ---- FX8010 ----
- */
-
-#define EMU10K1_CARD_CREATIVE			0x00000000
-#define EMU10K1_CARD_EMUAPS			0x00000001
-
-#define EMU10K1_FX8010_PCM_COUNT		8
-
-/* instruction set */
-#define iMAC0	 0x00	/* R = A + (X * Y >> 31)   ; saturation */
-#define iMAC1	 0x01	/* R = A + (-X * Y >> 31)  ; saturation */
-#define iMAC2	 0x02	/* R = A + (X * Y >> 31)   ; wraparound */
-#define iMAC3	 0x03	/* R = A + (-X * Y >> 31)  ; wraparound */
-#define iMACINT0 0x04	/* R = A + X * Y	   ; saturation */
-#define iMACINT1 0x05	/* R = A + X * Y	   ; wraparound (31-bit) */
-#define iACC3	 0x06	/* R = A + X + Y	   ; saturation */
-#define iMACMV   0x07	/* R = A, acc += X * Y >> 31 */
-#define iANDXOR  0x08	/* R = (A & X) ^ Y */
-#define iTSTNEG  0x09	/* R = (A >= Y) ? X : ~X */
-#define iLIMITGE 0x0a	/* R = (A >= Y) ? X : Y */
-#define iLIMITLT 0x0b	/* R = (A < Y) ? X : Y */
-#define iLOG	 0x0c	/* R = linear_data, A (log_data), X (max_exp), Y (format_word) */
-#define iEXP	 0x0d	/* R = log_data, A (linear_data), X (max_exp), Y (format_word) */
-#define iINTERP  0x0e	/* R = A + (X * (Y - A) >> 31)  ; saturation */
-#define iSKIP    0x0f	/* R = A (cc_reg), X (count), Y (cc_test) */
-
-/* GPRs */
-#define FXBUS(x)	(0x00 + (x))	/* x = 0x00 - 0x0f */
-#define EXTIN(x)	(0x10 + (x))	/* x = 0x00 - 0x0f */
-#define EXTOUT(x)	(0x20 + (x))	/* x = 0x00 - 0x0f physical outs -> FXWC low 16 bits */
-#define FXBUS2(x)	(0x30 + (x))	/* x = 0x00 - 0x0f copies of fx buses for capture -> FXWC high 16 bits */
-					/* NB: 0x31 and 0x32 are shared with Center/LFE on SB live 5.1 */
-
-#define C_00000000	0x40
-#define C_00000001	0x41
-#define C_00000002	0x42
-#define C_00000003	0x43
-#define C_00000004	0x44
-#define C_00000008	0x45
-#define C_00000010	0x46
-#define C_00000020	0x47
-#define C_00000100	0x48
-#define C_00010000	0x49
-#define C_00080000	0x4a
-#define C_10000000	0x4b
-#define C_20000000	0x4c
-#define C_40000000	0x4d
-#define C_80000000	0x4e
-#define C_7fffffff	0x4f
-#define C_ffffffff	0x50
-#define C_fffffffe	0x51
-#define C_c0000000	0x52
-#define C_4f1bbcdc	0x53
-#define C_5a7ef9db	0x54
-#define C_00100000	0x55		/* ?? */
-#define GPR_ACCU	0x56		/* ACCUM, accumulator */
-#define GPR_COND	0x57		/* CCR, condition register */
-#define GPR_NOISE0	0x58		/* noise source */
-#define GPR_NOISE1	0x59		/* noise source */
-#define GPR_IRQ		0x5a		/* IRQ register */
-#define GPR_DBAC	0x5b		/* TRAM Delay Base Address Counter */
-#define GPR(x)		(FXGPREGBASE + (x)) /* free GPRs: x = 0x00 - 0xff */
-#define ITRAM_DATA(x)	(TANKMEMDATAREGBASE + 0x00 + (x)) /* x = 0x00 - 0x7f */
-#define ETRAM_DATA(x)	(TANKMEMDATAREGBASE + 0x80 + (x)) /* x = 0x00 - 0x1f */
-#define ITRAM_ADDR(x)	(TANKMEMADDRREGBASE + 0x00 + (x)) /* x = 0x00 - 0x7f */
-#define ETRAM_ADDR(x)	(TANKMEMADDRREGBASE + 0x80 + (x)) /* x = 0x00 - 0x1f */
-
-#define A_ITRAM_DATA(x)	(TANKMEMDATAREGBASE + 0x00 + (x)) /* x = 0x00 - 0xbf */
-#define A_ETRAM_DATA(x)	(TANKMEMDATAREGBASE + 0xc0 + (x)) /* x = 0x00 - 0x3f */
-#define A_ITRAM_ADDR(x)	(TANKMEMADDRREGBASE + 0x00 + (x)) /* x = 0x00 - 0xbf */
-#define A_ETRAM_ADDR(x)	(TANKMEMADDRREGBASE + 0xc0 + (x)) /* x = 0x00 - 0x3f */
-#define A_ITRAM_CTL(x)	(A_TANKMEMCTLREGBASE + 0x00 + (x)) /* x = 0x00 - 0xbf */
-#define A_ETRAM_CTL(x)	(A_TANKMEMCTLREGBASE + 0xc0 + (x)) /* x = 0x00 - 0x3f */
-
-#define A_FXBUS(x)	(0x00 + (x))	/* x = 0x00 - 0x3f FX buses */
-#define A_EXTIN(x)	(0x40 + (x))	/* x = 0x00 - 0x0f physical ins */
-#define A_P16VIN(x)	(0x50 + (x))	/* x = 0x00 - 0x0f p16v ins (A2 only) "EMU32 inputs" */
-#define A_EXTOUT(x)	(0x60 + (x))	/* x = 0x00 - 0x1f physical outs -> A_FXWC1 0x79-7f unknown   */
-#define A_FXBUS2(x)	(0x80 + (x))	/* x = 0x00 - 0x1f extra outs used for EFX capture -> A_FXWC2 */
-#define A_EMU32OUTH(x)	(0xa0 + (x))	/* x = 0x00 - 0x0f "EMU32_OUT_10 - _1F" - ??? */
-#define A_EMU32OUTL(x)	(0xb0 + (x))	/* x = 0x00 - 0x0f "EMU32_OUT_1 - _F" - ??? */
-#define A_GPR(x)	(A_FXGPREGBASE + (x))
-
-/* cc_reg constants */
-#define CC_REG_NORMALIZED C_00000001
-#define CC_REG_BORROW	C_00000002
-#define CC_REG_MINUS	C_00000004
-#define CC_REG_ZERO	C_00000008
-#define CC_REG_SATURATE	C_00000010
-#define CC_REG_NONZERO	C_00000100
-
-/* FX buses */
-#define FXBUS_PCM_LEFT		0x00
-#define FXBUS_PCM_RIGHT		0x01
-#define FXBUS_PCM_LEFT_REAR	0x02
-#define FXBUS_PCM_RIGHT_REAR	0x03
-#define FXBUS_MIDI_LEFT		0x04
-#define FXBUS_MIDI_RIGHT	0x05
-#define FXBUS_PCM_CENTER	0x06
-#define FXBUS_PCM_LFE		0x07
-#define FXBUS_PCM_LEFT_FRONT	0x08
-#define FXBUS_PCM_RIGHT_FRONT	0x09
-#define FXBUS_MIDI_REVERB	0x0c
-#define FXBUS_MIDI_CHORUS	0x0d
-#define FXBUS_PCM_LEFT_SIDE	0x0e
-#define FXBUS_PCM_RIGHT_SIDE	0x0f
-#define FXBUS_PT_LEFT		0x14
-#define FXBUS_PT_RIGHT		0x15
-
-/* Inputs */
-#define EXTIN_AC97_L	   0x00	/* AC'97 capture channel - left */
-#define EXTIN_AC97_R	   0x01	/* AC'97 capture channel - right */
-#define EXTIN_SPDIF_CD_L   0x02	/* internal S/PDIF CD - onboard - left */
-#define EXTIN_SPDIF_CD_R   0x03	/* internal S/PDIF CD - onboard - right */
-#define EXTIN_ZOOM_L	   0x04	/* Zoom Video I2S - left */
-#define EXTIN_ZOOM_R	   0x05	/* Zoom Video I2S - right */
-#define EXTIN_TOSLINK_L	   0x06	/* LiveDrive - TOSLink Optical - left */
-#define EXTIN_TOSLINK_R    0x07	/* LiveDrive - TOSLink Optical - right */
-#define EXTIN_LINE1_L	   0x08	/* LiveDrive - Line/Mic 1 - left */
-#define EXTIN_LINE1_R	   0x09	/* LiveDrive - Line/Mic 1 - right */
-#define EXTIN_COAX_SPDIF_L 0x0a	/* LiveDrive - Coaxial S/PDIF - left */
-#define EXTIN_COAX_SPDIF_R 0x0b /* LiveDrive - Coaxial S/PDIF - right */
-#define EXTIN_LINE2_L	   0x0c	/* LiveDrive - Line/Mic 2 - left */
-#define EXTIN_LINE2_R	   0x0d	/* LiveDrive - Line/Mic 2 - right */
-
-/* Outputs */
-#define EXTOUT_AC97_L	   0x00	/* AC'97 playback channel - left */
-#define EXTOUT_AC97_R	   0x01	/* AC'97 playback channel - right */
-#define EXTOUT_TOSLINK_L   0x02	/* LiveDrive - TOSLink Optical - left */
-#define EXTOUT_TOSLINK_R   0x03	/* LiveDrive - TOSLink Optical - right */
-#define EXTOUT_AC97_CENTER 0x04	/* SB Live 5.1 - center */
-#define EXTOUT_AC97_LFE	   0x05 /* SB Live 5.1 - LFE */
-#define EXTOUT_HEADPHONE_L 0x06	/* LiveDrive - Headphone - left */
-#define EXTOUT_HEADPHONE_R 0x07	/* LiveDrive - Headphone - right */
-#define EXTOUT_REAR_L	   0x08	/* Rear channel - left */
-#define EXTOUT_REAR_R	   0x09	/* Rear channel - right */
-#define EXTOUT_ADC_CAP_L   0x0a	/* ADC Capture buffer - left */
-#define EXTOUT_ADC_CAP_R   0x0b	/* ADC Capture buffer - right */
-#define EXTOUT_MIC_CAP	   0x0c	/* MIC Capture buffer */
-#define EXTOUT_AC97_REAR_L 0x0d	/* SB Live 5.1 (c) 2003 - Rear Left */
-#define EXTOUT_AC97_REAR_R 0x0e	/* SB Live 5.1 (c) 2003 - Rear Right */
-#define EXTOUT_ACENTER	   0x11 /* Analog Center */
-#define EXTOUT_ALFE	   0x12 /* Analog LFE */
-
-/* Audigy Inputs */
-#define A_EXTIN_AC97_L		0x00	/* AC'97 capture channel - left */
-#define A_EXTIN_AC97_R		0x01	/* AC'97 capture channel - right */
-#define A_EXTIN_SPDIF_CD_L	0x02	/* digital CD left */
-#define A_EXTIN_SPDIF_CD_R	0x03	/* digital CD left */
-#define A_EXTIN_OPT_SPDIF_L     0x04    /* audigy drive Optical SPDIF - left */
-#define A_EXTIN_OPT_SPDIF_R     0x05    /*                              right */ 
-#define A_EXTIN_LINE2_L		0x08	/* audigy drive line2/mic2 - left */
-#define A_EXTIN_LINE2_R		0x09	/*                           right */
-#define A_EXTIN_ADC_L		0x0a    /* Philips ADC - left */
-#define A_EXTIN_ADC_R		0x0b    /*               right */
-#define A_EXTIN_AUX2_L		0x0c	/* audigy drive aux2 - left */
-#define A_EXTIN_AUX2_R		0x0d	/*                   - right */
-
-/* Audigiy Outputs */
-#define A_EXTOUT_FRONT_L	0x00	/* digital front left */
-#define A_EXTOUT_FRONT_R	0x01	/*               right */
-#define A_EXTOUT_CENTER		0x02	/* digital front center */
-#define A_EXTOUT_LFE		0x03	/* digital front lfe */
-#define A_EXTOUT_HEADPHONE_L	0x04	/* headphone audigy drive left */
-#define A_EXTOUT_HEADPHONE_R	0x05	/*                        right */
-#define A_EXTOUT_REAR_L		0x06	/* digital rear left */
-#define A_EXTOUT_REAR_R		0x07	/*              right */
-#define A_EXTOUT_AFRONT_L	0x08	/* analog front left */
-#define A_EXTOUT_AFRONT_R	0x09	/*              right */
-#define A_EXTOUT_ACENTER	0x0a	/* analog center */
-#define A_EXTOUT_ALFE		0x0b	/* analog LFE */
-#define A_EXTOUT_ASIDE_L	0x0c	/* analog side left  - Audigy 2 ZS */
-#define A_EXTOUT_ASIDE_R	0x0d	/*             right - Audigy 2 ZS */
-#define A_EXTOUT_AREAR_L	0x0e	/* analog rear left */
-#define A_EXTOUT_AREAR_R	0x0f	/*             right */
-#define A_EXTOUT_AC97_L		0x10	/* AC97 left (front) */
-#define A_EXTOUT_AC97_R		0x11	/*      right */
-#define A_EXTOUT_ADC_CAP_L	0x16	/* ADC capture buffer left */
-#define A_EXTOUT_ADC_CAP_R	0x17	/*                    right */
-#define A_EXTOUT_MIC_CAP	0x18	/* Mic capture buffer */
-
-/* Audigy constants */
-#define A_C_00000000	0xc0
-#define A_C_00000001	0xc1
-#define A_C_00000002	0xc2
-#define A_C_00000003	0xc3
-#define A_C_00000004	0xc4
-#define A_C_00000008	0xc5
-#define A_C_00000010	0xc6
-#define A_C_00000020	0xc7
-#define A_C_00000100	0xc8
-#define A_C_00010000	0xc9
-#define A_C_00000800	0xca
-#define A_C_10000000	0xcb
-#define A_C_20000000	0xcc
-#define A_C_40000000	0xcd
-#define A_C_80000000	0xce
-#define A_C_7fffffff	0xcf
-#define A_C_ffffffff	0xd0
-#define A_C_fffffffe	0xd1
-#define A_C_c0000000	0xd2
-#define A_C_4f1bbcdc	0xd3
-#define A_C_5a7ef9db	0xd4
-#define A_C_00100000	0xd5
-#define A_GPR_ACCU	0xd6		/* ACCUM, accumulator */
-#define A_GPR_COND	0xd7		/* CCR, condition register */
-#define A_GPR_NOISE0	0xd8		/* noise source */
-#define A_GPR_NOISE1	0xd9		/* noise source */
-#define A_GPR_IRQ	0xda		/* IRQ register */
-#define A_GPR_DBAC	0xdb		/* TRAM Delay Base Address Counter - internal */
-#define A_GPR_DBACE	0xde		/* TRAM Delay Base Address Counter - external */
-
-/* definitions for debug register */
-#define EMU10K1_DBG_ZC			0x80000000	/* zero tram counter */
-#define EMU10K1_DBG_SATURATION_OCCURED	0x02000000	/* saturation control */
-#define EMU10K1_DBG_SATURATION_ADDR	0x01ff0000	/* saturation address */
-#define EMU10K1_DBG_SINGLE_STEP		0x00008000	/* single step mode */
-#define EMU10K1_DBG_STEP		0x00004000	/* start single step */
-#define EMU10K1_DBG_CONDITION_CODE	0x00003e00	/* condition code */
-#define EMU10K1_DBG_SINGLE_STEP_ADDR	0x000001ff	/* single step address */
-
-/* tank memory address line */
-#ifndef __KERNEL__
-#define TANKMEMADDRREG_ADDR_MASK 0x000fffff	/* 20 bit tank address field			*/
-#define TANKMEMADDRREG_CLEAR	 0x00800000	/* Clear tank memory				*/
-#define TANKMEMADDRREG_ALIGN	 0x00400000	/* Align read or write relative to tank access	*/
-#define TANKMEMADDRREG_WRITE	 0x00200000	/* Write to tank memory				*/
-#define TANKMEMADDRREG_READ	 0x00100000	/* Read from tank memory			*/
-#endif
-
-struct snd_emu10k1_fx8010_info {
-	unsigned int internal_tram_size;	/* in samples */
-	unsigned int external_tram_size;	/* in samples */
-	char fxbus_names[16][32];		/* names of FXBUSes */
-	char extin_names[16][32];		/* names of external inputs */
-	char extout_names[32][32];		/* names of external outputs */
-	unsigned int gpr_controls;		/* count of GPR controls */
-};
-
-#define EMU10K1_GPR_TRANSLATION_NONE		0
-#define EMU10K1_GPR_TRANSLATION_TABLE100	1
-#define EMU10K1_GPR_TRANSLATION_BASS		2
-#define EMU10K1_GPR_TRANSLATION_TREBLE		3
-#define EMU10K1_GPR_TRANSLATION_ONOFF		4
-
-struct snd_emu10k1_fx8010_control_gpr {
-	struct snd_ctl_elem_id id;		/* full control ID definition */
-	unsigned int vcount;		/* visible count */
-	unsigned int count;		/* count of GPR (1..16) */
-	unsigned short gpr[32];		/* GPR number(s) */
-	unsigned int value[32];		/* initial values */
-	unsigned int min;		/* minimum range */
-	unsigned int max;		/* maximum range */
-	unsigned int translation;	/* translation type (EMU10K1_GPR_TRANSLATION*) */
-};
-
-struct snd_emu10k1_fx8010_code {
-	char name[128];
-
-	DECLARE_BITMAP(gpr_valid, 0x200); /* bitmask of valid initializers */
-	u_int32_t __user *gpr_map;	  /* initializers */
-
-	unsigned int gpr_add_control_count; /* count of GPR controls to add/replace */
-	struct snd_emu10k1_fx8010_control_gpr __user *gpr_add_controls; /* GPR controls to add/replace */
-
-	unsigned int gpr_del_control_count; /* count of GPR controls to remove */
-	struct snd_ctl_elem_id __user *gpr_del_controls; /* IDs of GPR controls to remove */
-
-	unsigned int gpr_list_control_count; /* count of GPR controls to list */
-	unsigned int gpr_list_control_total; /* total count of GPR controls */
-	struct snd_emu10k1_fx8010_control_gpr __user *gpr_list_controls; /* listed GPR controls */
-
-	DECLARE_BITMAP(tram_valid, 0x100); /* bitmask of valid initializers */
-	u_int32_t __user *tram_data_map;  /* data initializers */
-	u_int32_t __user *tram_addr_map;  /* map initializers */
-
-	DECLARE_BITMAP(code_valid, 1024); /* bitmask of valid instructions */
-	u_int32_t __user *code;		  /* one instruction - 64 bits */
-};
-
-struct snd_emu10k1_fx8010_tram {
-	unsigned int address;		/* 31.bit == 1 -> external TRAM */
-	unsigned int size;		/* size in samples (4 bytes) */
-	unsigned int *samples;		/* pointer to samples (20-bit) */
-					/* NULL->clear memory */
-};
-
-struct snd_emu10k1_fx8010_pcm_rec {
-	unsigned int substream;		/* substream number */
-	unsigned int res1;		/* reserved */
-	unsigned int channels;		/* 16-bit channels count, zero = remove this substream */
-	unsigned int tram_start;	/* ring buffer position in TRAM (in samples) */
-	unsigned int buffer_size;	/* count of buffered samples */
-	unsigned short gpr_size;		/* GPR containing size of ringbuffer in samples (host) */
-	unsigned short gpr_ptr;		/* GPR containing current pointer in the ring buffer (host = reset, FX8010) */
-	unsigned short gpr_count;	/* GPR containing count of samples between two interrupts (host) */
-	unsigned short gpr_tmpcount;	/* GPR containing current count of samples to interrupt (host = set, FX8010) */
-	unsigned short gpr_trigger;	/* GPR containing trigger (activate) information (host) */
-	unsigned short gpr_running;	/* GPR containing info if PCM is running (FX8010) */
-	unsigned char pad;		/* reserved */
-	unsigned char etram[32];	/* external TRAM address & data (one per channel) */
-	unsigned int res2;		/* reserved */
-};
-
-#define SNDRV_EMU10K1_IOCTL_INFO	_IOR ('H', 0x10, struct snd_emu10k1_fx8010_info)
-#define SNDRV_EMU10K1_IOCTL_CODE_POKE	_IOW ('H', 0x11, struct snd_emu10k1_fx8010_code)
-#define SNDRV_EMU10K1_IOCTL_CODE_PEEK	_IOWR('H', 0x12, struct snd_emu10k1_fx8010_code)
-#define SNDRV_EMU10K1_IOCTL_TRAM_SETUP	_IOW ('H', 0x20, int)
-#define SNDRV_EMU10K1_IOCTL_TRAM_POKE	_IOW ('H', 0x21, struct snd_emu10k1_fx8010_tram)
-#define SNDRV_EMU10K1_IOCTL_TRAM_PEEK	_IOWR('H', 0x22, struct snd_emu10k1_fx8010_tram)
-#define SNDRV_EMU10K1_IOCTL_PCM_POKE	_IOW ('H', 0x30, struct snd_emu10k1_fx8010_pcm_rec)
-#define SNDRV_EMU10K1_IOCTL_PCM_PEEK	_IOWR('H', 0x31, struct snd_emu10k1_fx8010_pcm_rec)
-#define SNDRV_EMU10K1_IOCTL_STOP	_IO  ('H', 0x80)
-#define SNDRV_EMU10K1_IOCTL_CONTINUE	_IO  ('H', 0x81)
-#define SNDRV_EMU10K1_IOCTL_ZERO_TRAM_COUNTER _IO ('H', 0x82)
-#define SNDRV_EMU10K1_IOCTL_SINGLE_STEP	_IOW ('H', 0x83, int)
-#define SNDRV_EMU10K1_IOCTL_DBG_READ	_IOR ('H', 0x84, int)
-
-/* typedefs for compatibility to user-space */
-typedef struct snd_emu10k1_fx8010_info emu10k1_fx8010_info_t;
-typedef struct snd_emu10k1_fx8010_control_gpr emu10k1_fx8010_control_gpr_t;
-typedef struct snd_emu10k1_fx8010_code emu10k1_fx8010_code_t;
-typedef struct snd_emu10k1_fx8010_tram emu10k1_fx8010_tram_t;
-typedef struct snd_emu10k1_fx8010_pcm_rec emu10k1_fx8010_pcm_t;
-
-#endif	/* __SOUND_EMU10K1_H */
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/gnu/dev/sound/pci/p16v-alsa.h
--- a/head/sys/gnu/dev/sound/pci/p16v-alsa.h	Fri Jan 13 00:58:58 2012 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,301 +0,0 @@
-/*-
- *  Copyright (c) by James Courtier-Dutton <James at superbug.demon.co.uk>
- *  Driver p16v chips
- *  Version: 0.21
- *
- *  FEATURES currently supported:
- *    Output fixed at S32_LE, 2 channel to hw:0,0
- *    Rates: 44.1, 48, 96, 192.
- *
- *  Changelog:
- *  0.8
- *    Use separate card based buffer for periods table.
- *  0.9
- *    Use 2 channel output streams instead of 8 channel.
- *       (8 channel output streams might be good for ASIO type output)
- *    Corrected speaker output, so Front -> Front etc.
- *  0.10
- *    Fixed missed interrupts.
- *  0.11
- *    Add Sound card model number and names.
- *    Add Analog volume controls.
- *  0.12
- *    Corrected playback interrupts. Now interrupt per period, instead of half period.
- *  0.13
- *    Use single trigger for multichannel.
- *  0.14
- *    Mic capture now works at fixed: S32_LE, 96000Hz, Stereo.
- *  0.15
- *    Force buffer_size / period_size == INTEGER.
- *  0.16
- *    Update p16v.c to work with changed alsa api.
- *  0.17
- *    Update p16v.c to work with changed alsa api. Removed boot_devs.
- *  0.18
- *    Merging with snd-emu10k1 driver.
- *  0.19
- *    One stereo channel at 24bit now works.
- *  0.20
- *    Added better register defines.
- *  0.21
- *    Split from p16v.c
- *
- *
- *  BUGS:
- *    Some stability problems when unloading the snd-p16v kernel module.
- *    --
- *
- *  TODO:
- *    SPDIF out.
- *    Find out how to change capture sample rates. E.g. To record SPDIF at 48000Hz.
- *    Currently capture fixed at 48000Hz.
- *
- *    --
- *  GENERAL INFO:
- *    Model: SB0240
- *    P16V Chip: CA0151-DBS
- *    Audigy 2 Chip: CA0102-IAT
- *    AC97 Codec: STAC 9721
- *    ADC: Philips 1361T (Stereo 24bit)
- *    DAC: CS4382-K (8-channel, 24bit, 192Khz)
- *
- *  This code was initally based on code from ALSA's emu10k1x.c which is:
- *  Copyright (c) by Francisco Moraes <fmoraes at nc.rr.com>
- *
- *   This program is free software; you can redistribute it and/or modify
- *   it under the terms of the GNU General Public License as published by
- *   the Free Software Foundation; either version 2 of the License, or
- *   (at your option) any later version.
- *
- *   This program is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *   GNU General Public License for more details.
- *
- *   You should have received a copy of the GNU General Public License
- *   along with this program; if not, write to the Free Software
- *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
- *
- */
-
-/* $FreeBSD$ */
-
-/********************************************************************************************************/
-/* Audigy2 P16V pointer-offset register set, accessed through the PTR2 and DATA2 registers                     */
-/********************************************************************************************************/
-                                                                                                                           
-/* The sample rate of the SPDIF outputs is set by modifying a register in the EMU10K2 PTR register A_SPDIF_SAMPLERATE.
- * The sample rate is also controlled by the same registers that control the rate of the EMU10K2 sample rate converters.
- */
-
-/* Initally all registers from 0x00 to 0x3f have zero contents. */
-#define PLAYBACK_LIST_ADDR	0x00		/* Base DMA address of a list of pointers to each period/size */
-						/* One list entry: 4 bytes for DMA address, 
-						 * 4 bytes for period_size << 16.
-						 * One list entry is 8 bytes long.
-						 * One list entry for each period in the buffer.
-						 */
-#define PLAYBACK_LIST_SIZE	0x01		/* Size of list in bytes << 16. E.g. 8 periods -> 0x00380000  */
-#define PLAYBACK_LIST_PTR	0x02		/* Pointer to the current period being played */
-#define PLAYBACK_UNKNOWN3	0x03		/* Not used */
-#define PLAYBACK_DMA_ADDR	0x04		/* Playback DMA addresss */
-#define PLAYBACK_PERIOD_SIZE	0x05		/* Playback period size. win2000 uses 0x04000000 */
-#define PLAYBACK_POINTER	0x06		/* Playback period pointer. Used with PLAYBACK_LIST_PTR to determine buffer position currently in DAC */
-#define PLAYBACK_FIFO_END_ADDRESS	0x07		/* Playback FIFO end address */
-#define PLAYBACK_FIFO_POINTER	0x08		/* Playback FIFO pointer and number of valid sound samples in cache */
-#define PLAYBACK_UNKNOWN9	0x09		/* Not used */
-#define CAPTURE_DMA_ADDR	0x10		/* Capture DMA address */
-#define CAPTURE_BUFFER_SIZE	0x11		/* Capture buffer size */
-#define CAPTURE_POINTER		0x12		/* Capture buffer pointer. Sample currently in ADC */
-#define CAPTURE_FIFO_POINTER	0x13		/* Capture FIFO pointer and number of valid sound samples in cache */
-#define CAPTURE_P16V_VOLUME1	0x14		/* Low: Capture volume 0xXXXX3030 */
-#define CAPTURE_P16V_VOLUME2	0x15		/* High:Has no effect on capture volume */
-#define CAPTURE_P16V_SOURCE     0x16            /* P16V source select. Set to 0x0700E4E5 for AC97 CAPTURE */
-						/* [0:1] Capture input 0 channel select. 0 = Capture output 0.
-						 *                                       1 = Capture output 1.
-						 *                                       2 = Capture output 2.
-						 *                                       3 = Capture output 3.
-						 * [3:2] Capture input 1 channel select. 0 = Capture output 0.
-						 *                                       1 = Capture output 1.
-						 *                                       2 = Capture output 2.
-						 *                                       3 = Capture output 3.
-						 * [5:4] Capture input 2 channel select. 0 = Capture output 0.
-						 *                                       1 = Capture output 1.
-						 *                                       2 = Capture output 2.
-						 *                                       3 = Capture output 3.
-						 * [7:6] Capture input 3 channel select. 0 = Capture output 0.
-						 *                                       1 = Capture output 1.
-						 *                                       2 = Capture output 2.
-						 *                                       3 = Capture output 3.
-						 * [9:8] Playback input 0 channel select. 0 = Play output 0.
-						 *                                        1 = Play output 1.
-						 *                                        2 = Play output 2.
-						 *                                        3 = Play output 3.
-						 * [11:10] Playback input 1 channel select. 0 = Play output 0.
-						 *                                          1 = Play output 1.
-						 *                                          2 = Play output 2.
-						 *                                          3 = Play output 3.
-						 * [13:12] Playback input 2 channel select. 0 = Play output 0.
-						 *                                          1 = Play output 1.
-						 *                                          2 = Play output 2.
-						 *                                          3 = Play output 3.
-						 * [15:14] Playback input 3 channel select. 0 = Play output 0.
-						 *                                          1 = Play output 1.
-						 *                                          2 = Play output 2.
-						 *                                          3 = Play output 3.
-						 * [19:16] Playback mixer output enable. 1 bit per channel.
-						 * [23:20] Capture mixer output enable. 1 bit per channel.
-						 * [26:24] FX engine channel capture 0 = 0x60-0x67.
-						 *                                   1 = 0x68-0x6f.
-						 *                                   2 = 0x70-0x77.
-						 *                                   3 = 0x78-0x7f.
-						 *                                   4 = 0x80-0x87.
-						 *                                   5 = 0x88-0x8f.
-						 *                                   6 = 0x90-0x97.
-						 *                                   7 = 0x98-0x9f.
-						 * [31:27] Not used.
-						 */
-
-						/* 0x1 = capture on.
-						 * 0x100 = capture off.
-						 * 0x200 = capture off.
-						 * 0x1000 = capture off.
-						 */
-#define CAPTURE_RATE_STATUS		0x17		/* Capture sample rate. Read only */
-						/* [15:0] Not used.
-						 * [18:16] Channel 0 Detected sample rate. 0 - 44.1khz
-						 *                               1 - 48 khz
-						 *                               2 - 96 khz
-						 *                               3 - 192 khz
-						 *                               7 - undefined rate.
-						 * [19] Channel 0. 1 - Valid, 0 - Not Valid.
-						 * [22:20] Channel 1 Detected sample rate. 
-						 * [23] Channel 1. 1 - Valid, 0 - Not Valid.
-						 * [26:24] Channel 2 Detected sample rate. 
-						 * [27] Channel 2. 1 - Valid, 0 - Not Valid.
-						 * [30:28] Channel 3 Detected sample rate. 
-						 * [31] Channel 3. 1 - Valid, 0 - Not Valid.
-						 */
-/* 0x18 - 0x1f unused */
-#define PLAYBACK_LAST_SAMPLE    0x20		/* The sample currently being played. Read only */
-/* 0x21 - 0x3f unused */
-#define BASIC_INTERRUPT         0x40		/* Used by both playback and capture interrupt handler */
-						/* Playback (0x1<<channel_id) Don't touch high 16bits. */
-						/* Capture  (0x100<<channel_id). not tested */
-						/* Start Playback [3:0] (one bit per channel)
-						 * Start Capture [11:8] (one bit per channel)
-						 * Record source select for channel 0 [18:16]
-						 * Record source select for channel 1 [22:20]
-						 * Record source select for channel 2 [26:24]
-						 * Record source select for channel 3 [30:28]
-						 * 0 - SPDIF channel.
-						 * 1 - I2S channel.
-						 * 2 - SRC48 channel.
-						 * 3 - SRCMulti_SPDIF channel.
-						 * 4 - SRCMulti_I2S channel.
-						 * 5 - SPDIF channel.
-						 * 6 - fxengine capture.
-						 * 7 - AC97 capture.
-						 */
-						/* Default 41110000.
-						 * Writing 0xffffffff hangs the PC.
-						 * Writing 0xffff0000 -> 77770000 so it must be some sort of route.
-						 * bit 0x1 starts DMA playback on channel_id 0
-						 */
-/* 0x41,42 take values from 0 - 0xffffffff, but have no effect on playback */
-/* 0x43,0x48 do not remember settings */
-/* 0x41-45 unused */
-#define WATERMARK            0x46		/* Test bit to indicate cache level usage */
-						/* Values it can have while playing on channel 0. 
-						 * 0000f000, 0000f004, 0000f008, 0000f00c.
-						 * Readonly.
-						 */
-/* 0x47-0x4f unused */
-/* 0x50-0x5f Capture cache data */
-#define SRCSel			0x60            /* SRCSel. Default 0x4. Bypass P16V 0x14 */
-						/* [0] 0 = 10K2 audio, 1 = SRC48 mixer output.
-						 * [2] 0 = 10K2 audio, 1 = SRCMulti SPDIF mixer output.
-						 * [4] 0 = 10K2 audio, 1 = SRCMulti I2S mixer output.
-						 */
-						/* SRC48 converts samples rates 44.1, 48, 96, 192 to 48 khz. */
-						/* SRCMulti converts 48khz samples rates to 44.1, 48, 96, 192 to 48. */
-						/* SRC48 and SRCMULTI sample rate select and output select. */
-						/* 0xffffffff -> 0xC0000015
-						 * 0xXXXXXXX4 = Enable Front Left/Right
-						 * Enable PCMs
-						 */
-
-/* 0x61 -> 0x6c are Volume controls */
-#define PLAYBACK_VOLUME_MIXER1  0x61		/* SRC48 Low to mixer input volume control. */
-#define PLAYBACK_VOLUME_MIXER2  0x62		/* SRC48 High to mixer input volume control. */
-#define PLAYBACK_VOLUME_MIXER3  0x63		/* SRCMULTI SPDIF Low to mixer input volume control. */
-#define PLAYBACK_VOLUME_MIXER4  0x64		/* SRCMULTI SPDIF High to mixer input volume control. */
-#define PLAYBACK_VOLUME_MIXER5  0x65		/* SRCMULTI I2S Low to mixer input volume control. */
-#define PLAYBACK_VOLUME_MIXER6  0x66		/* SRCMULTI I2S High to mixer input volume control. */
-#define PLAYBACK_VOLUME_MIXER7  0x67		/* P16V Low to SRCMULTI SPDIF mixer input volume control. */
-#define PLAYBACK_VOLUME_MIXER8  0x68		/* P16V High to SRCMULTI SPDIF mixer input volume control. */
-#define PLAYBACK_VOLUME_MIXER9  0x69		/* P16V Low to SRCMULTI I2S mixer input volume control. */
-						/* 0xXXXX3030 = PCM0 Volume (Front).
-						 * 0x3030XXXX = PCM1 Volume (Center)
-						 */
-#define PLAYBACK_VOLUME_MIXER10  0x6a		/* P16V High to SRCMULTI I2S mixer input volume control. */
-						/* 0x3030XXXX = PCM3 Volume (Rear). */
-#define PLAYBACK_VOLUME_MIXER11  0x6b		/* E10K2 Low to SRC48 mixer input volume control. */
-#define PLAYBACK_VOLUME_MIXER12 0x6c		/* E10K2 High to SRC48 mixer input volume control. */
-
-#define SRC48_ENABLE            0x6d            /* SRC48 input audio enable */
-						/* SRC48 converts samples rates 44.1, 48, 96, 192 to 48 khz. */
-						/* [23:16] The corresponding P16V channel to SRC48 enabled if == 1.
-						 * [31:24] The corresponding E10K2 channel to SRC48 enabled.
-						 */
-#define SRCMULTI_ENABLE         0x6e            /* SRCMulti input audio enable. Default 0xffffffff */
-						/* SRCMulti converts 48khz samples rates to 44.1, 48, 96, 192 to 48. */
-						/* [7:0] The corresponding P16V channel to SRCMulti_I2S enabled if == 1.
-						 * [15:8] The corresponding E10K2 channel to SRCMulti I2S enabled.
-						 * [23:16] The corresponding P16V channel to SRCMulti SPDIF enabled.
-						 * [31:24] The corresponding E10K2 channel to SRCMulti SPDIF enabled.
-						 */
-						/* Bypass P16V 0xff00ff00 
-						 * Bitmap. 0 = Off, 1 = On.
-						 * P16V playback outputs:
-						 * 0xXXXXXXX1 = PCM0 Left. (Front)
-						 * 0xXXXXXXX2 = PCM0 Right.
-						 * 0xXXXXXXX4 = PCM1 Left. (Center/LFE)
-						 * 0xXXXXXXX8 = PCM1 Right.
-						 * 0xXXXXXX1X = PCM2 Left. (Unknown)
-						 * 0xXXXXXX2X = PCM2 Right.
-						 * 0xXXXXXX4X = PCM3 Left. (Rear)
-						 * 0xXXXXXX8X = PCM3 Right.
-						 */
-#define AUDIO_OUT_ENABLE        0x6f            /* Default: 000100FF */
-						/* [3:0] Does something, but not documented. Probably capture enable.
-						 * [7:4] Playback channels enable. not documented.
-						 * [16] AC97 output enable if == 1
-						 * [30] 0 = SRCMulti_I2S input from fxengine 0x68-0x6f.
-						 *      1 = SRCMulti_I2S input from SRC48 output.
-						 * [31] 0 = SRCMulti_SPDIF input from fxengine 0x60-0x67.
-						 *      1 = SRCMulti_SPDIF input from SRC48 output.
-						 */
-						/* 0xffffffff -> C00100FF */
-						/* 0 -> Not playback sound, irq still running */
-						/* 0xXXXXXX10 = PCM0 Left/Right On. (Front)
-						 * 0xXXXXXX20 = PCM1 Left/Right On. (Center/LFE)
-						 * 0xXXXXXX40 = PCM2 Left/Right On. (Unknown)
-						 * 0xXXXXXX80 = PCM3 Left/Right On. (Rear)
-						 */
-#define PLAYBACK_SPDIF_SELECT     0x70          /* Default: 12030F00 */
-						/* 0xffffffff -> 3FF30FFF */
-						/* 0x00000001 pauses stream/irq fail. */
-						/* All other bits do not effect playback */
-#define PLAYBACK_SPDIF_SRC_SELECT 0x71          /* Default: 0000E4E4 */
-						/* 0xffffffff -> F33FFFFF */
-						/* All bits do not effect playback */
-#define PLAYBACK_SPDIF_USER_DATA0 0x72		/* SPDIF out user data 0 */
-#define PLAYBACK_SPDIF_USER_DATA1 0x73		/* SPDIF out user data 1 */
-/* 0x74-0x75 unknown */
-#define CAPTURE_SPDIF_CONTROL	0x76		/* SPDIF in control setting */
-#define CAPTURE_SPDIF_STATUS	0x77		/* SPDIF in status */
-#define CAPURE_SPDIF_USER_DATA0 0x78		/* SPDIF in user data 0 */
-#define CAPURE_SPDIF_USER_DATA1 0x79		/* SPDIF in user data 1 */
-#define CAPURE_SPDIF_USER_DATA2 0x7a		/* SPDIF in user data 2 */
-
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/gnu/dev/sound/pci/p17v-alsa.h
--- a/head/sys/gnu/dev/sound/pci/p17v-alsa.h	Fri Jan 13 00:58:58 2012 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,113 +0,0 @@
-/*-
- *  Copyright (c) by James Courtier-Dutton <James at superbug.demon.co.uk>
- *  Driver p17v chips
- *  Version: 0.01
- *
- *   This program is free software; you can redistribute it and/or modify
- *   it under the terms of the GNU General Public License as published by
- *   the Free Software Foundation; either version 2 of the License, or
- *   (at your option) any later version.
- *
- *   This program is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *   GNU General Public License for more details.
- *
- *   You should have received a copy of the GNU General Public License
- *   along with this program; if not, write to the Free Software
- *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
- *
- */
-
-/* $FreeBSD$ */
-
-/******************************************************************************/
-/* Audigy2Value Tina (P17V) pointer-offset register set,
- * accessed through the PTR20 and DATA24 registers  */
-/******************************************************************************/
-
-/* 00 - 07: Not used */
-#define P17V_PLAYBACK_FIFO_PTR	0x08	/* Current playback fifo pointer
-					 * and number of sound samples in cache.
-					 */  
-/* 09 - 12: Not used */
-#define P17V_CAPTURE_FIFO_PTR	0x13	/* Current capture fifo pointer
-					 * and number of sound samples in cache.
-					 */  
-/* 14 - 17: Not used */
-#define P17V_PB_CHN_SEL		0x18	/* P17v playback channel select */
-#define P17V_SE_SLOT_SEL_L	0x19	/* Sound Engine slot select low */
-#define P17V_SE_SLOT_SEL_H	0x1a	/* Sound Engine slot select high */
-/* 1b - 1f: Not used */
-/* 20 - 2f: Not used */
-/* 30 - 3b: Not used */
-#define P17V_SPI		0x3c	/* SPI interface register */
-#define P17V_I2C_ADDR		0x3d	/* I2C Address */
-#define P17V_I2C_0		0x3e	/* I2C Data */
-#define P17V_I2C_1		0x3f	/* I2C Data */
-
-#define P17V_START_AUDIO	0x40	/* Start Audio bit */
-/* 41 - 47: Reserved */
-#define P17V_START_CAPTURE	0x48	/* Start Capture bit */
-#define P17V_CAPTURE_FIFO_BASE	0x49	/* Record FIFO base address */
-#define P17V_CAPTURE_FIFO_SIZE	0x4a	/* Record FIFO buffer size */
-#define P17V_CAPTURE_FIFO_INDEX	0x4b	/* Record FIFO capture index */
-#define P17V_CAPTURE_VOL_H	0x4c	/* P17v capture volume control */
-#define P17V_CAPTURE_VOL_L	0x4d	/* P17v capture volume control */
-/* 4e - 4f: Not used */
-/* 50 - 5f: Not used */
-#define P17V_SRCSel		0x60	/* SRC48 and SRCMulti sample rate select
-					 * and output select
-					 */
-#define P17V_MIXER_AC97_10K1_VOL_L	0x61	/* 10K to Mixer_AC97 input volume control */
-#define P17V_MIXER_AC97_10K1_VOL_H	0x62	/* 10K to Mixer_AC97 input volume control */
-#define P17V_MIXER_AC97_P17V_VOL_L	0x63	/* P17V to Mixer_AC97 input volume control */
-#define P17V_MIXER_AC97_P17V_VOL_H	0x64	/* P17V to Mixer_AC97 input volume control */
-#define P17V_MIXER_AC97_SRP_REC_VOL_L	0x65	/* SRP Record to Mixer_AC97 input volume control */
-#define P17V_MIXER_AC97_SRP_REC_VOL_H	0x66	/* SRP Record to Mixer_AC97 input volume control */
-/* 67 - 68: Reserved */
-#define P17V_MIXER_Spdif_10K1_VOL_L	0x69	/* 10K to Mixer_Spdif input volume control */
-#define P17V_MIXER_Spdif_10K1_VOL_H	0x6A	/* 10K to Mixer_Spdif input volume control */
-#define P17V_MIXER_Spdif_P17V_VOL_L	0x6B	/* P17V to Mixer_Spdif input volume control */
-#define P17V_MIXER_Spdif_P17V_VOL_H	0x6C	/* P17V to Mixer_Spdif input volume control */
-#define P17V_MIXER_Spdif_SRP_REC_VOL_L	0x6D	/* SRP Record to Mixer_Spdif input volume control */
-#define P17V_MIXER_Spdif_SRP_REC_VOL_H	0x6E	/* SRP Record to Mixer_Spdif input volume control */
-/* 6f - 70: Reserved */
-#define P17V_MIXER_I2S_10K1_VOL_L	0x71	/* 10K to Mixer_I2S input volume control */
-#define P17V_MIXER_I2S_10K1_VOL_H	0x72	/* 10K to Mixer_I2S input volume control */
-#define P17V_MIXER_I2S_P17V_VOL_L	0x73	/* P17V to Mixer_I2S input volume control */
-#define P17V_MIXER_I2S_P17V_VOL_H	0x74	/* P17V to Mixer_I2S input volume control */
-#define P17V_MIXER_I2S_SRP_REC_VOL_L	0x75	/* SRP Record to Mixer_I2S input volume control */
-#define P17V_MIXER_I2S_SRP_REC_VOL_H	0x76	/* SRP Record to Mixer_I2S input volume control */
-/* 77 - 78: Reserved */
-#define P17V_MIXER_AC97_ENABLE		0x79	/* Mixer AC97 input audio enable */
-#define P17V_MIXER_SPDIF_ENABLE		0x7A	/* Mixer SPDIF input audio enable */
-#define P17V_MIXER_I2S_ENABLE		0x7B	/* Mixer I2S input audio enable */
-#define P17V_AUDIO_OUT_ENABLE		0x7C	/* Audio out enable */
-#define P17V_MIXER_ATT			0x7D	/* SRP Mixer Attenuation Select */
-#define P17V_SRP_RECORD_SRR		0x7E	/* SRP Record channel source Select */
-#define P17V_SOFT_RESET_SRP_MIXER	0x7F	/* SRP and mixer soft reset */
-
-#define P17V_AC97_OUT_MASTER_VOL_L	0x80	/* AC97 Output master volume control */
-#define P17V_AC97_OUT_MASTER_VOL_H	0x81	/* AC97 Output master volume control */
-#define P17V_SPDIF_OUT_MASTER_VOL_L	0x82	/* SPDIF Output master volume control */
-#define P17V_SPDIF_OUT_MASTER_VOL_H	0x83	/* SPDIF Output master volume control */
-#define P17V_I2S_OUT_MASTER_VOL_L	0x84	/* I2S Output master volume control */
-#define P17V_I2S_OUT_MASTER_VOL_H	0x85	/* I2S Output master volume control */
-/* 86 - 87: Not used */
-#define P17V_I2S_CHANNEL_SWAP_PHASE_INVERSE	0x88	/* I2S out mono channel swap
-							 * and phase inverse */
-#define P17V_SPDIF_CHANNEL_SWAP_PHASE_INVERSE	0x89	/* SPDIF out mono channel swap
-							 * and phase inverse */
-/* 8A: Not used */
-#define P17V_SRP_P17V_ESR		0x8B	/* SRP_P17V estimated sample rate and rate lock */
-#define P17V_SRP_REC_ESR		0x8C	/* SRP_REC estimated sample rate and rate lock */
-#define P17V_SRP_BYPASS			0x8D	/* srps channel bypass and srps bypass */
-/* 8E - 92: Not used */
-#define P17V_I2S_SRC_SEL		0x93	/* I2SIN mode sel */
-
-
-
-
-
-
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/i386/conf/GENERIC
--- a/head/sys/i386/conf/GENERIC	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/i386/conf/GENERIC	Fri Jan 13 15:51:01 2012 +0200
@@ -16,7 +16,7 @@
 # If you are in doubt as to the purpose or necessity of a line, check first
 # in NOTES.
 #
-# $FreeBSD: head/sys/i386/conf/GENERIC 228973 2011-12-29 22:48:36Z rwatson $
+# $FreeBSD: head/sys/i386/conf/GENERIC 229997 2012-01-12 00:34:33Z ken $
 
 cpu		I486_CPU
 cpu		I586_CPU
@@ -138,7 +138,8 @@
 device		sa		# Sequential Access (tape etc)
 device		cd		# CD
 device		pass		# Passthrough device (direct ATA/SCSI access)
-device		ses		# SCSI Environmental Services (and SAF-TE)
+device		ses		# Enclosure Services (SES and SAF-TE)
+device		ctl		# CAM Target Layer
 
 # RAID controllers interfaced to the SCSI subsystem
 device		amr		# AMI MegaRAID
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/i386/conf/PAE
--- a/head/sys/i386/conf/PAE	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/i386/conf/PAE	Fri Jan 13 15:51:01 2012 +0200
@@ -1,7 +1,7 @@
 #
 # PAE -- Generic kernel configuration file for FreeBSD/i386 PAE
 #
-# $FreeBSD: head/sys/i386/conf/PAE 228003 2011-11-26 18:02:39Z marius $
+# $FreeBSD: head/sys/i386/conf/PAE 229997 2012-01-12 00:34:33Z ken $
 
 include GENERIC
 
@@ -23,6 +23,7 @@
 # address properly may cause data corruption when used in a machine with more
 # than 4 gigabytes of memory.
 
+
 nodevice	ahb
 nodevice	sym
 nodevice	trm
@@ -37,6 +38,8 @@
 nodevice	nsp
 nodevice	stg
 
+nodevice	ctl
+
 nodevice	asr
 nodevice	dpt
 nodevice	mly
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/i386/conf/XEN
--- a/head/sys/i386/conf/XEN	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/i386/conf/XEN	Fri Jan 13 15:51:01 2012 +0200
@@ -1,7 +1,7 @@
 #
 # XEN -- Kernel configuration for i386 XEN DomU
 #
-# $FreeBSD: head/sys/i386/conf/XEN 224699 2011-08-07 20:16:46Z rmacklem $
+# $FreeBSD: head/sys/i386/conf/XEN 229789 2012-01-07 19:38:26Z adrian $
 
 cpu		I686_CPU
 ident		XEN
@@ -83,6 +83,10 @@
 device		gif		# IPv6 and IPv4 tunneling
 device		faith		# IPv6-to-IPv4 relaying (translation)
 
+# Wireless cards
+options		IEEE80211_SUPPORT_MESH
+options		AH_SUPPORT_AR5416
+
 # The `bpf' device enables the Berkeley Packet Filter.
 # Be aware of the administrative consequences of enabling this!
 # Note that 'bpf' is required for DHCP.
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/ia64/conf/GENERIC
--- a/head/sys/ia64/conf/GENERIC	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/ia64/conf/GENERIC	Fri Jan 13 15:51:01 2012 +0200
@@ -18,7 +18,7 @@
 #
 # For hardware specific information check HARDWARE.TXT
 #
-# $FreeBSD: head/sys/ia64/conf/GENERIC 228973 2011-12-29 22:48:36Z rwatson $
+# $FreeBSD: head/sys/ia64/conf/GENERIC 229997 2012-01-12 00:34:33Z ken $
 
 cpu		ITANIUM2
 ident		GENERIC
@@ -113,7 +113,8 @@
 device		da		# Direct Access (ie disk)
 device		pass		# Passthrough (direct ATA/SCSI access)
 device		sa		# Sequential Access (ie tape)
-device		ses		# Environmental Services (and SAF-TE)
+device		ses		# Enclosure Services (SES and SAF-TE)
+device		ctl		# CAM Target Layer
 
 # RAID controllers
 device		aac		# Adaptec FSA RAID
@@ -180,6 +181,15 @@
 device		uvisor		# Visor and Palm devices
 device		uvscom		# USB serial support for DDI pocket's PHS
 
+# Wireless NIC cards.
+# The wlan(4) module assumes this, so just define it so it
+# at least correctly loads.
+options		IEEE80211_SUPPORT_MESH
+
+# The ath(4) and ath_hal(4) code requires this.  The module currently
+# builds everything including AR5416 (and later 11n NIC) support.
+options		AH_SUPPORT_AR5416
+
 # FireWire support
 device		fwip		# IP over FireWire (RFC 2734,3146)
 # sbp(4) works for some systems but causes boot failure on others
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/kern/kern_prot.c
--- a/head/sys/kern/kern_prot.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/kern/kern_prot.c	Fri Jan 13 15:51:01 2012 +0200
@@ -42,7 +42,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/kern/kern_prot.c 225617 2011-09-16 13:58:51Z kmacy $");
+__FBSDID("$FreeBSD: head/sys/kern/kern_prot.c 229818 2012-01-08 18:48:36Z hrs $");
 
 #include "opt_compat.h"
 #include "opt_inet.h"
@@ -73,7 +73,7 @@
 
 #ifdef REGRESSION
 FEATURE(regression,
-    "Kernel support for interfaces nessesary for regression testing (SECURITY RISK!)");
+    "Kernel support for interfaces necessary for regression testing (SECURITY RISK!)");
 #endif
 
 #if defined(INET) || defined(INET6)
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/kern/kern_resource.c
--- a/head/sys/kern/kern_resource.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/kern/kern_resource.c	Fri Jan 13 15:51:01 2012 +0200
@@ -35,7 +35,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/kern/kern_resource.c 228470 2011-12-13 14:00:27Z eadler $");
+__FBSDID("$FreeBSD: head/sys/kern/kern_resource.c 229622 2012-01-05 19:02:52Z jhb $");
 
 #include "opt_compat.h"
 
@@ -497,7 +497,7 @@
 	oldpri = td->td_user_pri;
 	sched_user_prio(td, newpri);
 	if (td->td_user_pri != oldpri && (td == curthread ||
-	    td->td_priority == oldpri || td->td_user_pri >= PRI_MAX_REALTIME))
+	    td->td_priority == oldpri || td->td_user_pri <= PRI_MAX_REALTIME))
 		sched_prio(td, td->td_user_pri);
 	if (TD_ON_UPILOCK(td) && oldpri != newpri) {
 		critical_enter();
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/kern/kern_shutdown.c
--- a/head/sys/kern/kern_shutdown.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/kern/kern_shutdown.c	Fri Jan 13 15:51:01 2012 +0200
@@ -35,7 +35,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/kern/kern_shutdown.c 228632 2011-12-17 15:11:22Z avg $");
+__FBSDID("$FreeBSD: head/sys/kern/kern_shutdown.c 229854 2012-01-09 12:06:09Z avg $");
 
 #include "opt_ddb.h"
 #include "opt_kdb.h"
@@ -123,7 +123,7 @@
 	&sync_on_panic, 0, "Do a sync before rebooting from a panic");
 TUNABLE_INT("kern.sync_on_panic", &sync_on_panic);
 
-static int stop_scheduler_on_panic = 0;
+static int stop_scheduler_on_panic = 1;
 SYSCTL_INT(_kern, OID_AUTO, stop_scheduler_on_panic, CTLFLAG_RW | CTLFLAG_TUN,
     &stop_scheduler_on_panic, 0, "stop scheduler upon entering panic");
 TUNABLE_INT("kern.stop_scheduler_on_panic", &stop_scheduler_on_panic);
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/kern/kern_uuid.c
--- a/head/sys/kern/kern_uuid.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/kern/kern_uuid.c	Fri Jan 13 15:51:01 2012 +0200
@@ -25,7 +25,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/kern/kern_uuid.c 225617 2011-09-16 13:58:51Z kmacy $");
+__FBSDID("$FreeBSD: head/sys/kern/kern_uuid.c 229621 2012-01-05 19:00:36Z jhb $");
 
 #include <sys/param.h>
 #include <sys/endian.h>
@@ -98,20 +98,20 @@
 	IFNET_RLOCK_NOSLEEP();
 	TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
 		/* Walk the address list */
-		IF_ADDR_LOCK(ifp);
+		IF_ADDR_RLOCK(ifp);
 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 			sdl = (struct sockaddr_dl*)ifa->ifa_addr;
 			if (sdl != NULL && sdl->sdl_family == AF_LINK &&
 			    sdl->sdl_type == IFT_ETHER) {
 				/* Got a MAC address. */
 				bcopy(LLADDR(sdl), node, UUID_NODE_LEN);
-				IF_ADDR_UNLOCK(ifp);
+				IF_ADDR_RUNLOCK(ifp);
 				IFNET_RUNLOCK_NOSLEEP();
 				CURVNET_RESTORE();
 				return;
 			}
 		}
-		IF_ADDR_UNLOCK(ifp);
+		IF_ADDR_RUNLOCK(ifp);
 	}
 	IFNET_RUNLOCK_NOSLEEP();
 
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/kern/subr_witness.c
--- a/head/sys/kern/subr_witness.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/kern/subr_witness.c	Fri Jan 13 15:51:01 2012 +0200
@@ -85,7 +85,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/kern/subr_witness.c 228424 2011-12-11 21:02:01Z avg $");
+__FBSDID("$FreeBSD: head/sys/kern/subr_witness.c 229873 2012-01-09 19:34:12Z jhb $");
 
 #include "opt_ddb.h"
 #include "opt_hwpmc_hooks.h"
@@ -520,7 +520,7 @@
 	{ "udpinp", &lock_class_rw },
 	{ "in_multi_mtx", &lock_class_mtx_sleep },
 	{ "igmp_mtx", &lock_class_mtx_sleep },
-	{ "if_addr_mtx", &lock_class_mtx_sleep },
+	{ "if_addr_lock", &lock_class_rw },
 	{ NULL, NULL },
 	/*
 	 * IPv6 multicast:
@@ -529,7 +529,7 @@
 	{ "udpinp", &lock_class_rw },
 	{ "in6_multi_mtx", &lock_class_mtx_sleep },
 	{ "mld_mtx", &lock_class_mtx_sleep },
-	{ "if_addr_mtx", &lock_class_mtx_sleep },
+	{ "if_addr_lock", &lock_class_rw },
 	{ NULL, NULL },
 	/*
 	 * UNIX Domain Sockets
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/kern/uipc_shm.c
--- a/head/sys/kern/uipc_shm.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/kern/uipc_shm.c	Fri Jan 13 15:51:01 2012 +0200
@@ -39,17 +39,10 @@
  *
  * (3) Resource limits?  Does this need its own resource limits or are the
  *     existing limits in mmap(2) sufficient?
- *
- * (4) Partial page truncation.  vnode_pager_setsize() will zero any parts
- *     of a partially mapped page as a result of ftruncate(2)/truncate(2).
- *     We can do the same (with the same pmap evil), but do we need to
- *     worry about the bits on disk if the page is swapped out or will the
- *     swapper zero the parts of a page that are invalid if the page is
- *     swapped back in for us?
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/kern/uipc_shm.c 228533 2011-12-15 15:17:19Z jhb $");
+__FBSDID("$FreeBSD: head/sys/kern/uipc_shm.c 229821 2012-01-08 20:09:26Z alc $");
 
 #include "opt_capsicum.h"
 
@@ -86,6 +79,7 @@
 #include <vm/vm_kern.h>
 #include <vm/vm_object.h>
 #include <vm/vm_page.h>
+#include <vm/vm_pageout.h>
 #include <vm/vm_pager.h>
 #include <vm/swap_pager.h>
 
@@ -253,9 +247,10 @@
 shm_dotruncate(struct shmfd *shmfd, off_t length)
 {
 	vm_object_t object;
-	vm_page_t m;
-	vm_pindex_t nobjsize;
+	vm_page_t m, ma[1];
+	vm_pindex_t idx, nobjsize;
 	vm_ooffset_t delta;
+	int base, rv;
 
 	object = shmfd->shm_object;
 	VM_OBJECT_LOCK(object);
@@ -275,6 +270,56 @@
 			VM_OBJECT_UNLOCK(object);
 			return (EBUSY);
 		}
+
+		/*
+		 * Zero the truncated part of the last page.
+		 */
+		base = length & PAGE_MASK;
+		if (base != 0) {
+			idx = OFF_TO_IDX(length);
+retry:
+			m = vm_page_lookup(object, idx);
+			if (m != NULL) {
+				if ((m->oflags & VPO_BUSY) != 0 ||
+				    m->busy != 0) {
+					vm_page_sleep(m, "shmtrc");
+					goto retry;
+				}
+			} else if (vm_pager_has_page(object, idx, NULL, NULL)) {
+				m = vm_page_alloc(object, idx, VM_ALLOC_NORMAL);
+				if (m == NULL) {
+					VM_OBJECT_UNLOCK(object);
+					VM_WAIT;
+					VM_OBJECT_LOCK(object);
+					goto retry;
+				} else if (m->valid != VM_PAGE_BITS_ALL) {
+					ma[0] = m;
+					rv = vm_pager_get_pages(object, ma, 1,
+					    0);
+					m = vm_page_lookup(object, idx);
+				} else
+					/* A cached page was reactivated. */
+					rv = VM_PAGER_OK;
+				vm_page_lock(m);
+				if (rv == VM_PAGER_OK) {
+					vm_page_deactivate(m);
+					vm_page_unlock(m);
+					vm_page_wakeup(m);
+				} else {
+					vm_page_free(m);
+					vm_page_unlock(m);
+					VM_OBJECT_UNLOCK(object);
+					return (EIO);
+				}
+			}
+			if (m != NULL) {
+				pmap_zero_page_area(m, base, PAGE_SIZE - base);
+				KASSERT(m->valid == VM_PAGE_BITS_ALL,
+				    ("shm_dotruncate: page %p is invalid", m));
+				vm_page_dirty(m);
+				vm_pager_page_unswapped(m);
+			}
+		}
 		delta = ptoa(object->size - nobjsize);
 
 		/* Toss in memory pages. */
@@ -289,45 +334,7 @@
 		/* Free the swap accounted for shm */
 		swap_release_by_cred(delta, object->cred);
 		object->charge -= delta;
-
-		/*
-		 * If the last page is partially mapped, then zero out
-		 * the garbage at the end of the page.  See comments
-		 * in vnode_pager_setsize() for more details.
-		 *
-		 * XXXJHB: This handles in memory pages, but what about
-		 * a page swapped out to disk?
-		 */
-		if ((length & PAGE_MASK) &&
-		    (m = vm_page_lookup(object, OFF_TO_IDX(length))) != NULL &&
-		    m->valid != 0) {
-			int base = (int)length & PAGE_MASK;
-			int size = PAGE_SIZE - base;
-
-			pmap_zero_page_area(m, base, size);
-
-			/*
-			 * Update the valid bits to reflect the blocks that
-			 * have been zeroed.  Some of these valid bits may
-			 * have already been set.
-			 */
-			vm_page_set_valid_range(m, base, size);
-
-			/*
-			 * Round "base" to the next block boundary so that the
-			 * dirty bit for a partially zeroed block is not
-			 * cleared.
-			 */
-			base = roundup2(base, DEV_BSIZE);
-
-			vm_page_clear_dirty(m, base, PAGE_SIZE - base);
-		} else if ((length & PAGE_MASK) &&
-		    __predict_false(object->cache != NULL)) {
-			vm_page_cache_free(object, OFF_TO_IDX(length),
-			    nobjsize);
-		}
 	} else {
-
 		/* Attempt to reserve the swap */
 		delta = ptoa(nobjsize - object->size);
 		if (!swap_reserve_by_cred(delta, object->cred)) {
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/kern/vfs_subr.c
--- a/head/sys/kern/vfs_subr.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/kern/vfs_subr.c	Fri Jan 13 15:51:01 2012 +0200
@@ -39,7 +39,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/kern/vfs_subr.c 228849 2011-12-23 20:11:37Z jhb $");
+__FBSDID("$FreeBSD: head/sys/kern/vfs_subr.c 229727 2012-01-06 20:05:48Z jhb $");
 
 #include "opt_ddb.h"
 #include "opt_watchdog.h"
@@ -4035,7 +4035,7 @@
 void
 vop_deleteextattr_post(void *ap, int rc)
 {
-	struct vop_setattr_args *a = ap;
+	struct vop_deleteextattr_args *a = ap;
 
 	if (!rc)
 		VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
@@ -4125,7 +4125,7 @@
 void
 vop_setextattr_post(void *ap, int rc)
 {
-	struct vop_setattr_args *a = ap;
+	struct vop_setextattr_args *a = ap;
 
 	if (!rc)
 		VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/kern/vfs_syscalls.c
--- a/head/sys/kern/vfs_syscalls.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/kern/vfs_syscalls.c	Fri Jan 13 15:51:01 2012 +0200
@@ -35,7 +35,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/kern/vfs_syscalls.c 228593 2011-12-16 20:10:00Z jhb $");
+__FBSDID("$FreeBSD: head/sys/kern/vfs_syscalls.c 229828 2012-01-08 23:06:53Z kib $");
 
 #include "opt_capsicum.h"
 #include "opt_compat.h"
@@ -86,6 +86,8 @@
 #include <vm/vm_page.h>
 #include <vm/uma.h>
 
+#include <ufs/ufs/quota.h>
+
 static MALLOC_DEFINE(M_FADVISE, "fadvise", "posix_fadvise(2) information");
 
 SDT_PROVIDER_DEFINE(vfs);
@@ -212,7 +214,20 @@
 		return (error);
 	}
 	error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg);
-	vfs_unbusy(mp);
+
+	/*
+	 * Since quota on operation typically needs to open quota
+	 * file, the Q_QUOTAON handler needs to unbusy the mount point
+	 * before calling into namei.  Otherwise, unmount might be
+	 * started between two vfs_busy() invocations (first is our,
+	 * second is from mount point cross-walk code in lookup()),
+	 * causing deadlock.
+	 *
+	 * Require that Q_QUOTAON handles the vfs_busy() reference on
+	 * its own, always returning with ubusied mount point.
+	 */
+	if ((uap->cmd >> SUBCMDSHIFT) != Q_QUOTAON)
+		vfs_unbusy(mp);
 	VFS_UNLOCK_GIANT(vfslocked);
 	return (error);
 }
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/kern/vnode_if.src
--- a/head/sys/kern/vnode_if.src	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/kern/vnode_if.src	Fri Jan 13 15:51:01 2012 +0200
@@ -27,7 +27,7 @@
 # SUCH DAMAGE.
 #
 #	@(#)vnode_if.src	8.12 (Berkeley) 5/14/95
-# $FreeBSD: head/sys/kern/vnode_if.src 228849 2011-12-23 20:11:37Z jhb $
+# $FreeBSD: head/sys/kern/vnode_if.src 229728 2012-01-06 20:06:45Z jhb $
 #
 
 #
@@ -50,7 +50,7 @@
 # X: locked if not nil.
 #
 # The paramater named "vpp" is assumed to be always used with double
-# indirection (**vpp) and that name is hard-codeed in vnode_if.awk !
+# indirection (**vpp) and that name is hard-coded in vnode_if.awk !
 #
 # Lines starting with %! specify a pre or post-condition function
 # to call before/after the vop call.
@@ -639,3 +639,28 @@
 	IN off_t end;
 	IN int advice;
 };
+
+# The VOPs below are spares at the end of the table to allow new VOPs to be
+# added in stable branches without breaking the KBI.  New VOPs in HEAD should
+# be added above these spares.  When merging a new VOP to a stable branch,
+# the new VOP should replace one of the spares.
+
+vop_spare1 {
+	IN struct vnode *vp;
+};
+
+vop_spare2 {
+	IN struct vnode *vp;
+};
+
+vop_spare3 {
+	IN struct vnode *vp;
+};
+
+vop_spare4 {
+	IN struct vnode *vp;
+};
+
+vop_spare5 {
+	IN struct vnode *vp;
+};
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/mips/atheros/ar724x_pci.c
--- a/head/sys/mips/atheros/ar724x_pci.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/mips/atheros/ar724x_pci.c	Fri Jan 13 15:51:01 2012 +0200
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/mips/atheros/ar724x_pci.c 227843 2011-11-22 21:28:20Z marius $");
+__FBSDID("$FreeBSD: head/sys/mips/atheros/ar724x_pci.c 229765 2012-01-07 04:13:25Z adrian $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -93,7 +93,7 @@
 	uint32_t val, mask, shift;
 
 	/* Register access is 32-bit aligned */
-	shift = 8 * (offset & (bytes % 4));
+	shift = (offset & 3) * 8;
 	if (bytes % 4)
 		mask = (1 << (bytes * 8)) - 1;
 	else
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/mips/cavium/cryptocteon/cavium_crypto.c
--- a/head/sys/mips/cavium/cryptocteon/cavium_crypto.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/mips/cavium/cryptocteon/cavium_crypto.c	Fri Jan 13 15:51:01 2012 +0200
@@ -42,7 +42,7 @@
 /****************************************************************************/
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/mips/cavium/cryptocteon/cavium_crypto.c 217620 2011-01-20 05:44:36Z gonzo $");
+__FBSDID("$FreeBSD: head/sys/mips/cavium/cryptocteon/cavium_crypto.c 229677 2012-01-06 01:23:26Z gonzo $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -87,22 +87,6 @@
 	    }								\
 	} while (0)
 
-static inline unsigned long octeon_crypto_enable(void)
-{
-    register_t s;
-    
-    s = intr_disable();
-    mips_wr_status(mips_rd_status() | MIPS_SR_COP_2_BIT);
-
-    return (s);
-}
-
-static inline void octeon_crypto_disable(register_t s)
-{
-    mips_wr_status(mips_rd_status() & ~MIPS_SR_COP_2_BIT);
-    intr_restore(s);
-}
-
 #define ESP_HEADER_LENGTH     8
 #define DES_CBC_IV_LENGTH     8
 #define AES_CBC_IV_LENGTH     16
@@ -252,14 +236,12 @@
     uint64_t *key1;
     register uint64_t xor1 = 0x3636363636363636ULL;
     register uint64_t xor2 = 0x5c5c5c5c5c5c5c5cULL;
-    register_t s;
 
     dprintf("%s()\n", __func__);
 
     memset(hash_key, 0, sizeof(hash_key));
     memcpy(hash_key, (uint8_t *) key, (auth ? 20 : 16));
     key1 = (uint64_t *) hash_key;
-    s = octeon_crypto_enable();
     if (auth) {
        CVMX_MT_HSH_IV(0x67452301EFCDAB89ULL, 0);
        CVMX_MT_HSH_IV(0x98BADCFE10325476ULL, 1);
@@ -332,7 +314,6 @@
       outer[2] = 0;
       CVMX_MF_HSH_IV(outer[2], 2);
     }
-    octeon_crypto_disable(s);
     return;
 }
 
@@ -349,7 +330,6 @@
 {
     uint64_t *data;
     int data_i, data_l;
-    register_t s;
 
     dprintf("%s()\n", __func__);
 
@@ -367,7 +347,6 @@
     CVMX_PREFETCH0(ivp);
     CVMX_PREFETCH0(od->octo_enckey);
 
-    s = octeon_crypto_enable();
 
     /* load 3DES Key */
     CVMX_MT_3DES_KEY(((uint64_t *) od->octo_enckey)[0], 0);
@@ -378,7 +357,6 @@
 	CVMX_MT_3DES_KEY(((uint64_t *) od->octo_enckey)[0], 1);
 	CVMX_MT_3DES_KEY(((uint64_t *) od->octo_enckey)[0], 2);
     } else {
-	octeon_crypto_disable(s);
 	dprintf("%s: Bad key length %d\n", __func__, od->octo_encklen);
 	return -EINVAL;
     }
@@ -397,7 +375,6 @@
 	crypt_len -= 8;
     }
 
-    octeon_crypto_disable(s);
     return 0;
 }
 
@@ -412,7 +389,6 @@
 {
     uint64_t *data;
     int data_i, data_l;
-    register_t s;
 
     dprintf("%s()\n", __func__);
 
@@ -430,8 +406,6 @@
     CVMX_PREFETCH0(ivp);
     CVMX_PREFETCH0(od->octo_enckey);
 
-    s = octeon_crypto_enable();
-
     /* load 3DES Key */
     CVMX_MT_3DES_KEY(((uint64_t *) od->octo_enckey)[0], 0);
     if (od->octo_encklen == 24) {
@@ -441,7 +415,6 @@
 	CVMX_MT_3DES_KEY(((uint64_t *) od->octo_enckey)[0], 1);
 	CVMX_MT_3DES_KEY(((uint64_t *) od->octo_enckey)[0], 2);
     } else {
-	octeon_crypto_disable(s);
 	dprintf("%s: Bad key length %d\n", __func__, od->octo_encklen);
 	return -EINVAL;
     }
@@ -460,7 +433,6 @@
 	crypt_len -= 8;
     }
 
-    octeon_crypto_disable(s);
     return 0;
 }
 
@@ -477,7 +449,6 @@
 {
     uint64_t *data, *pdata;
     int data_i, data_l;
-    register_t s;
 
     dprintf("%s()\n", __func__);
 
@@ -495,8 +466,6 @@
     CVMX_PREFETCH0(ivp);
     CVMX_PREFETCH0(od->octo_enckey);
 
-    s = octeon_crypto_enable();
-
     /* load AES Key */
     CVMX_MT_AES_KEY(((uint64_t *) od->octo_enckey)[0], 0);
     CVMX_MT_AES_KEY(((uint64_t *) od->octo_enckey)[1], 1);
@@ -511,7 +480,6 @@
 	CVMX_MT_AES_KEY(((uint64_t *) od->octo_enckey)[2], 2);
 	CVMX_MT_AES_KEY(((uint64_t *) od->octo_enckey)[3], 3);
     } else {
-	octeon_crypto_disable(s);
 	dprintf("%s: Bad key length %d\n", __func__, od->octo_encklen);
 	return -EINVAL;
     }
@@ -536,7 +504,6 @@
 	crypt_len -= 16;
     }
 
-    octeon_crypto_disable(s);
     return 0;
 }
 
@@ -551,7 +518,6 @@
 {
     uint64_t *data, *pdata;
     int data_i, data_l;
-    register_t s;
 
     dprintf("%s()\n", __func__);
 
@@ -569,8 +535,6 @@
     CVMX_PREFETCH0(ivp);
     CVMX_PREFETCH0(od->octo_enckey);
 
-    s = octeon_crypto_enable();
-
     /* load AES Key */
     CVMX_MT_AES_KEY(((uint64_t *) od->octo_enckey)[0], 0);
     CVMX_MT_AES_KEY(((uint64_t *) od->octo_enckey)[1], 1);
@@ -585,7 +549,6 @@
 	CVMX_MT_AES_KEY(((uint64_t *) od->octo_enckey)[2], 2);
 	CVMX_MT_AES_KEY(((uint64_t *) od->octo_enckey)[3], 3);
     } else {
-	octeon_crypto_disable(s);
 	dprintf("%s: Bad key length %d\n", __func__, od->octo_encklen);
 	return -EINVAL;
     }
@@ -610,7 +573,6 @@
 	crypt_len -= 16;
     }
 
-    octeon_crypto_disable(s);
     return 0;
 }
 
@@ -629,7 +591,6 @@
     uint64_t *data;
     uint64_t tmp1, tmp2;
     int data_i, data_l, alen = auth_len;
-    register_t s;
 
     dprintf("%s()\n", __func__);
 
@@ -644,8 +605,6 @@
 
     IOV_INIT(iov, data, data_i, data_l);
 
-    s = octeon_crypto_enable();
-
     /* Load MD5 IV */
     CVMX_MT_HSH_IV(od->octo_hminner[0], 0);
     CVMX_MT_HSH_IV(od->octo_hminner[1], 1);
@@ -716,7 +675,6 @@
     CVMX_MF_HSH_IV(tmp1, 1);
     *(uint32_t *)data = (uint32_t) (tmp1 >> 32);
 
-    octeon_crypto_disable(s);
     return 0;
 }
 
@@ -735,7 +693,6 @@
     uint64_t *data;
     uint64_t tmp1, tmp2, tmp3;
     int data_i, data_l, alen = auth_len;
-    register_t s;
 
     dprintf("%s()\n", __func__);
 
@@ -750,8 +707,6 @@
 
     IOV_INIT(iov, data, data_i, data_l);
 
-    s = octeon_crypto_enable();
-
     /* Load SHA1 IV */
     CVMX_MT_HSH_IV(od->octo_hminner[0], 0);
     CVMX_MT_HSH_IV(od->octo_hminner[1], 1);
@@ -825,7 +780,6 @@
     CVMX_MF_HSH_IV(tmp1, 1);
     *(uint32_t *)data = (uint32_t) (tmp1 >> 32);
 
-    octeon_crypto_disable(s);
     return 0;
 }
 
@@ -849,7 +803,6 @@
     uint32_t *data32;
     uint64_t tmp1, tmp2;
     int data_i, data_l, alen = auth_len;
-    register_t s;
 
     dprintf("%s()\n", __func__);
 
@@ -870,8 +823,6 @@
     CVMX_PREFETCH0(ivp);
     CVMX_PREFETCH0(od->octo_enckey);
 
-    s = octeon_crypto_enable();
-
     /* load 3DES Key */
     CVMX_MT_3DES_KEY(((uint64_t *) od->octo_enckey)[0], 0);
     if (od->octo_encklen == 24) {
@@ -881,7 +832,6 @@
 	CVMX_MT_3DES_KEY(((uint64_t *) od->octo_enckey)[0], 1);
 	CVMX_MT_3DES_KEY(((uint64_t *) od->octo_enckey)[0], 2);
     } else {
-	octeon_crypto_disable(s);
 	dprintf("%s: Bad key length %d\n", __func__, od->octo_encklen);
 	return -EINVAL;
     }
@@ -981,7 +931,6 @@
     CVMX_MF_HSH_IV(tmp1, 1);
     *data32 = (uint32_t) (tmp1 >> 32);
 
-    octeon_crypto_disable(s);
     return 0;
 }
 
@@ -1002,7 +951,6 @@
     uint32_t *data32;
     uint64_t tmp1, tmp2;
     int data_i, data_l, alen = auth_len;
-    register_t s;
 
     dprintf("%s()\n", __func__);
 
@@ -1023,8 +971,6 @@
     CVMX_PREFETCH0(ivp);
     CVMX_PREFETCH0(od->octo_enckey);
 
-    s = octeon_crypto_enable();
-
     /* load 3DES Key */
     CVMX_MT_3DES_KEY(((uint64_t *) od->octo_enckey)[0], 0);
     if (od->octo_encklen == 24) {
@@ -1034,7 +980,6 @@
 	CVMX_MT_3DES_KEY(((uint64_t *) od->octo_enckey)[0], 1);
 	CVMX_MT_3DES_KEY(((uint64_t *) od->octo_enckey)[0], 2);
     } else {
-	octeon_crypto_disable(s);
 	dprintf("%s: Bad key length %d\n", __func__, od->octo_encklen);
 	return -EINVAL;
     }
@@ -1134,7 +1079,6 @@
     CVMX_MF_HSH_IV(tmp1, 1);
     *data32 = (uint32_t) (tmp1 >> 32);
 
-    octeon_crypto_disable(s);
     return 0;
 }
 
@@ -1158,7 +1102,6 @@
     uint32_t *data32;
     uint64_t tmp1, tmp2, tmp3;
     int data_i, data_l, alen = auth_len;
-    register_t s;
 
     dprintf("%s()\n", __func__);
 
@@ -1179,8 +1122,6 @@
     CVMX_PREFETCH0(ivp);
     CVMX_PREFETCH0(od->octo_enckey);
 
-    s = octeon_crypto_enable();
-
     /* load 3DES Key */
     CVMX_MT_3DES_KEY(((uint64_t *) od->octo_enckey)[0], 0);
     if (od->octo_encklen == 24) {
@@ -1190,7 +1131,6 @@
 	CVMX_MT_3DES_KEY(((uint64_t *) od->octo_enckey)[0], 1);
 	CVMX_MT_3DES_KEY(((uint64_t *) od->octo_enckey)[0], 2);
     } else {
-	octeon_crypto_disable(s);
 	dprintf("%s: Bad key length %d\n", __func__, od->octo_encklen);
 	return -EINVAL;
     }
@@ -1293,7 +1233,6 @@
     CVMX_MF_HSH_IV(tmp1, 1);
     *data32 = (uint32_t) (tmp1 >> 32);
 
-    octeon_crypto_disable(s);
     return 0;
 }
 
@@ -1314,7 +1253,6 @@
     uint32_t *data32;
     uint64_t tmp1, tmp2, tmp3;
     int data_i, data_l, alen = auth_len;
-    register_t s;
 
     dprintf("%s()\n", __func__);
 
@@ -1335,8 +1273,6 @@
     CVMX_PREFETCH0(ivp);
     CVMX_PREFETCH0(od->octo_enckey);
 
-    s = octeon_crypto_enable();
-
     /* load 3DES Key */
     CVMX_MT_3DES_KEY(((uint64_t *) od->octo_enckey)[0], 0);
     if (od->octo_encklen == 24) {
@@ -1346,7 +1282,6 @@
 	CVMX_MT_3DES_KEY(((uint64_t *) od->octo_enckey)[0], 1);
 	CVMX_MT_3DES_KEY(((uint64_t *) od->octo_enckey)[0], 2);
     } else {
-	octeon_crypto_disable(s);
 	dprintf("%s: Bad key length %d\n", __func__, od->octo_encklen);
 	return -EINVAL;
     }
@@ -1448,7 +1383,6 @@
     CVMX_MF_HSH_IV(tmp1, 1);
     *data32 = (uint32_t) (tmp1 >> 32);
 
-    octeon_crypto_disable(s);
     return 0;
 }
 
@@ -1473,7 +1407,6 @@
     uint32_t *data32;
     uint64_t tmp1, tmp2;
     int data_i, data_l, alen = auth_len;
-    register_t s;
 
     dprintf("%s()\n", __func__);
 
@@ -1494,8 +1427,6 @@
     CVMX_PREFETCH0(ivp);
     CVMX_PREFETCH0(od->octo_enckey);
 
-    s = octeon_crypto_enable();
-
     /* load AES Key */
     CVMX_MT_AES_KEY(((uint64_t *) od->octo_enckey)[0], 0);
     CVMX_MT_AES_KEY(((uint64_t *) od->octo_enckey)[1], 1);
@@ -1510,7 +1441,6 @@
 	CVMX_MT_AES_KEY(((uint64_t *) od->octo_enckey)[2], 2);
 	CVMX_MT_AES_KEY(((uint64_t *) od->octo_enckey)[3], 3);
     } else {
-	octeon_crypto_disable(s);
 	dprintf("%s: Bad key length %d\n", __func__, od->octo_encklen);
 	return -EINVAL;
     }
@@ -1633,7 +1563,6 @@
     CVMX_MF_HSH_IV(tmp1, 1);
     *data32 = (uint32_t) (tmp1 >> 32);
 
-    octeon_crypto_disable(s);
     return 0;
 }
 
@@ -1655,7 +1584,6 @@
     uint32_t *data32;
     uint64_t tmp1, tmp2;
     int data_i, data_l, alen = auth_len;
-    register_t s;
 
     dprintf("%s()\n", __func__);
 
@@ -1676,8 +1604,6 @@
     CVMX_PREFETCH0(ivp);
     CVMX_PREFETCH0(od->octo_enckey);
 
-    s = octeon_crypto_enable();
-
     /* load AES Key */
     CVMX_MT_AES_KEY(((uint64_t *) od->octo_enckey)[0], 0);
     CVMX_MT_AES_KEY(((uint64_t *) od->octo_enckey)[1], 1);
@@ -1692,7 +1618,6 @@
 	CVMX_MT_AES_KEY(((uint64_t *) od->octo_enckey)[2], 2);
 	CVMX_MT_AES_KEY(((uint64_t *) od->octo_enckey)[3], 3);
     } else {
-	octeon_crypto_disable(s);
 	dprintf("%s: Bad key length %d\n", __func__, od->octo_encklen);
 	return -EINVAL;
     }
@@ -1811,7 +1736,6 @@
     CVMX_MF_HSH_IV(tmp1, 1);
     *data32 = (uint32_t) (tmp1 >> 32);
 
-    octeon_crypto_disable(s);
     return 0;
 }
 
@@ -1836,7 +1760,6 @@
     uint32_t *data32;
     uint64_t tmp1, tmp2, tmp3;
     int data_i, data_l, alen = auth_len;
-    register_t s;
 
     dprintf("%s()\n", __func__);
 
@@ -1857,8 +1780,6 @@
     CVMX_PREFETCH0(ivp);
     CVMX_PREFETCH0(od->octo_enckey);
 
-    s = octeon_crypto_enable();
-
     /* load AES Key */
     CVMX_MT_AES_KEY(((uint64_t *) od->octo_enckey)[0], 0);
     CVMX_MT_AES_KEY(((uint64_t *) od->octo_enckey)[1], 1);
@@ -1873,7 +1794,6 @@
 	CVMX_MT_AES_KEY(((uint64_t *) od->octo_enckey)[2], 2);
 	CVMX_MT_AES_KEY(((uint64_t *) od->octo_enckey)[3], 3);
     } else {
-	octeon_crypto_disable(s);
 	dprintf("%s: Bad key length %d\n", __func__, od->octo_encklen);
 	return -EINVAL;
     }
@@ -2015,7 +1935,6 @@
     CVMX_MF_HSH_IV(tmp1, 1);
     *data32 = (uint32_t) (tmp1 >> 32);
 
-    octeon_crypto_disable(s);
     return 0;
 }
 
@@ -2037,7 +1956,6 @@
     uint32_t *data32;
     uint64_t tmp1, tmp2, tmp3;
     int data_i, data_l, alen = auth_len;
-    register_t s;
 
     dprintf("%s()\n", __func__);
 
@@ -2058,8 +1976,6 @@
     CVMX_PREFETCH0(ivp);
     CVMX_PREFETCH0(od->octo_enckey);
 
-    s = octeon_crypto_enable();
-
     /* load AES Key */
     CVMX_MT_AES_KEY(((uint64_t *) od->octo_enckey)[0], 0);
     CVMX_MT_AES_KEY(((uint64_t *) od->octo_enckey)[1], 1);
@@ -2074,7 +1990,6 @@
 	CVMX_MT_AES_KEY(((uint64_t *) od->octo_enckey)[2], 2);
 	CVMX_MT_AES_KEY(((uint64_t *) od->octo_enckey)[3], 3);
     } else {
-	octeon_crypto_disable(s);
 	dprintf("%s: Bad key length %d\n", __func__, od->octo_encklen);
 	return -EINVAL;
     }
@@ -2215,7 +2130,6 @@
     CVMX_MF_HSH_IV(tmp1, 1);
     *data32 = (uint32_t) (tmp1 >> 32);
 
-    octeon_crypto_disable(s);
     return 0;
 }
 
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/mips/cavium/files.octeon1
--- a/head/sys/mips/cavium/files.octeon1	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/mips/cavium/files.octeon1	Fri Jan 13 15:51:01 2012 +0200
@@ -1,4 +1,4 @@
-# $FreeBSD: head/sys/mips/cavium/files.octeon1 228925 2011-12-28 05:57:03Z gonzo $
+# $FreeBSD: head/sys/mips/cavium/files.octeon1 229677 2012-01-06 01:23:26Z gonzo $
 # Octeon Support Files
 #
 mips/cavium/asm_octeon.S			optional smp
@@ -21,6 +21,8 @@
 
 mips/cavium/cryptocteon/cavium_crypto.c		optional cryptocteon
 mips/cavium/cryptocteon/cryptocteon.c		optional cryptocteon
+mips/mips/octeon_cop2_swtch.S			standard
+mips/mips/octeon_cop2.c				standard
 
 mips/cavium/octe/ethernet.c			optional octe
 mips/cavium/octe/ethernet-mv88e61xx.c		optional octe octeon_vendor_lanner
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/mips/cavium/octeon_cop2.S
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/sys/mips/cavium/octeon_cop2.S	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,225 @@
+/*-
+ * Copyright (c) 2011 Oleksandr Tymoshenko
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD: head/sys/mips/cavium/octeon_cop2.S 229677 2012-01-06 01:23:26Z gonzo $
+ */
+
+#include <machine/asm.h>
+#include <mips/cavium/octeon_cop2.h>
+
+#include "assym.s"
+
+.set noreorder
+
+#define SAVE_COP2_REGISTER(reg)				\
+	dmfc2	t1, reg; sd	t1, reg##_OFFSET(a0)
+
+
+#define RESTORE_COP2_REGISTER(reg)			\
+	ld	t1, reg##_OFFSET(a0); dmtc2	t1, reg##_SET
+ 
+LEAF(octeon_cop2_save)
+	/* Get CvmCtl register */
+	dmfc0	t0, $9, 7
+
+	/* CRC state */
+	SAVE_COP2_REGISTER(COP2_CRC_IV)
+	SAVE_COP2_REGISTER(COP2_CRC_LENGTH)
+	SAVE_COP2_REGISTER(COP2_CRC_POLY)
+
+	/* if CvmCtl[NODFA_CP2] -> save_nodfa */
+	bbit1	t0, 28, save_nodfa
+	nop
+
+	/* LLM state */
+	SAVE_COP2_REGISTER(COP2_LLM_DAT0)
+	SAVE_COP2_REGISTER(COP2_LLM_DAT1)
+
+save_nodfa:
+	/* crypto stuff is irrelevant if CvmCtl[NOCRYPTO]  */
+	bbit1	t0, 26, save_done
+	nop
+
+	SAVE_COP2_REGISTER(COP2_3DES_IV)
+	SAVE_COP2_REGISTER(COP2_3DES_KEY0)
+	SAVE_COP2_REGISTER(COP2_3DES_KEY1)
+	SAVE_COP2_REGISTER(COP2_3DES_KEY2)
+	SAVE_COP2_REGISTER(COP2_3DES_RESULT)
+
+	SAVE_COP2_REGISTER(COP2_AES_INP0)
+	SAVE_COP2_REGISTER(COP2_AES_IV0)
+	SAVE_COP2_REGISTER(COP2_AES_IV1)
+	SAVE_COP2_REGISTER(COP2_AES_KEY0)
+	SAVE_COP2_REGISTER(COP2_AES_KEY1)
+	SAVE_COP2_REGISTER(COP2_AES_KEY2)
+	SAVE_COP2_REGISTER(COP2_AES_KEY3)
+	SAVE_COP2_REGISTER(COP2_AES_KEYLEN)
+	SAVE_COP2_REGISTER(COP2_AES_RESULT0)
+	SAVE_COP2_REGISTER(COP2_AES_RESULT1)
+
+	dmfc0	t0, $15
+	li	t1, 0x000d0000 /* Octeon Pass1 */
+	beq	t0, t1, save_pass1
+	nop
+
+	SAVE_COP2_REGISTER(COP2_HSH_DATW0)
+	SAVE_COP2_REGISTER(COP2_HSH_DATW1)
+	SAVE_COP2_REGISTER(COP2_HSH_DATW2)
+	SAVE_COP2_REGISTER(COP2_HSH_DATW3)
+	SAVE_COP2_REGISTER(COP2_HSH_DATW4)
+	SAVE_COP2_REGISTER(COP2_HSH_DATW5)
+	SAVE_COP2_REGISTER(COP2_HSH_DATW6)
+	SAVE_COP2_REGISTER(COP2_HSH_DATW7)
+	SAVE_COP2_REGISTER(COP2_HSH_DATW8)
+	SAVE_COP2_REGISTER(COP2_HSH_DATW9)
+	SAVE_COP2_REGISTER(COP2_HSH_DATW10)
+	SAVE_COP2_REGISTER(COP2_HSH_DATW11)
+	SAVE_COP2_REGISTER(COP2_HSH_DATW12)
+	SAVE_COP2_REGISTER(COP2_HSH_DATW13)
+	SAVE_COP2_REGISTER(COP2_HSH_DATW14)
+	SAVE_COP2_REGISTER(COP2_HSH_IVW0)
+	SAVE_COP2_REGISTER(COP2_HSH_IVW1)
+	SAVE_COP2_REGISTER(COP2_HSH_IVW2)
+	SAVE_COP2_REGISTER(COP2_HSH_IVW3)
+	SAVE_COP2_REGISTER(COP2_HSH_IVW4)
+	SAVE_COP2_REGISTER(COP2_HSH_IVW5)
+	SAVE_COP2_REGISTER(COP2_HSH_IVW6)
+	SAVE_COP2_REGISTER(COP2_HSH_IVW7)
+	SAVE_COP2_REGISTER(COP2_GFM_MULT0)
+	SAVE_COP2_REGISTER(COP2_GFM_MULT1)
+	SAVE_COP2_REGISTER(COP2_GFM_POLY)
+	SAVE_COP2_REGISTER(COP2_GFM_RESULT0)
+	SAVE_COP2_REGISTER(COP2_GFM_RESULT1)
+	jr ra
+	nop
+
+save_pass1:
+	SAVE_COP2_REGISTER(COP2_HSH_DATW0_PASS1)
+	SAVE_COP2_REGISTER(COP2_HSH_DATW1_PASS1)
+	SAVE_COP2_REGISTER(COP2_HSH_DATW2_PASS1)
+	SAVE_COP2_REGISTER(COP2_HSH_DATW3_PASS1)
+	SAVE_COP2_REGISTER(COP2_HSH_DATW4_PASS1)
+	SAVE_COP2_REGISTER(COP2_HSH_DATW5_PASS1)
+	SAVE_COP2_REGISTER(COP2_HSH_DATW6_PASS1)
+	SAVE_COP2_REGISTER(COP2_HSH_IVW0_PASS1)
+	SAVE_COP2_REGISTER(COP2_HSH_IVW1_PASS1)
+	SAVE_COP2_REGISTER(COP2_HSH_IVW2_PASS1)
+
+save_done:	
+	jr ra
+	nop
+END(octeon_cop2_save)
+
+LEAF(octeon_cop2_restore)
+	/* Get CvmCtl register */
+	dmfc0	t0, $9, 7
+
+	/* CRC state */
+	RESTORE_COP2_REGISTER(COP2_CRC_IV)
+	RESTORE_COP2_REGISTER(COP2_CRC_LENGTH)
+	RESTORE_COP2_REGISTER(COP2_CRC_POLY)
+
+	/* if CvmCtl[NODFA_CP2] -> save_nodfa */
+	bbit1	t0, 28, restore_nodfa
+	nop
+
+	/* LLM state */
+	RESTORE_COP2_REGISTER(COP2_LLM_DAT0)
+	RESTORE_COP2_REGISTER(COP2_LLM_DAT1)
+
+restore_nodfa:
+	/* crypto stuff is irrelevant if CvmCtl[NOCRYPTO]  */
+	bbit1	t0, 26, restore_done
+	nop
+
+	RESTORE_COP2_REGISTER(COP2_3DES_IV)
+	RESTORE_COP2_REGISTER(COP2_3DES_KEY0)
+	RESTORE_COP2_REGISTER(COP2_3DES_KEY1)
+	RESTORE_COP2_REGISTER(COP2_3DES_KEY2)
+	RESTORE_COP2_REGISTER(COP2_3DES_RESULT)
+
+	RESTORE_COP2_REGISTER(COP2_AES_INP0)
+	RESTORE_COP2_REGISTER(COP2_AES_IV0)
+	RESTORE_COP2_REGISTER(COP2_AES_IV1)
+	RESTORE_COP2_REGISTER(COP2_AES_KEY0)
+	RESTORE_COP2_REGISTER(COP2_AES_KEY1)
+	RESTORE_COP2_REGISTER(COP2_AES_KEY2)
+	RESTORE_COP2_REGISTER(COP2_AES_KEY3)
+	RESTORE_COP2_REGISTER(COP2_AES_KEYLEN)
+	RESTORE_COP2_REGISTER(COP2_AES_RESULT0)
+	RESTORE_COP2_REGISTER(COP2_AES_RESULT1)
+
+	dmfc0	t0, $15
+	li	t1, 0x000d0000 /* Octeon Pass1 */
+	beq	t0, t1, restore_pass1
+	nop
+
+	RESTORE_COP2_REGISTER(COP2_HSH_DATW0)
+	RESTORE_COP2_REGISTER(COP2_HSH_DATW1)
+	RESTORE_COP2_REGISTER(COP2_HSH_DATW2)
+	RESTORE_COP2_REGISTER(COP2_HSH_DATW3)
+	RESTORE_COP2_REGISTER(COP2_HSH_DATW4)
+	RESTORE_COP2_REGISTER(COP2_HSH_DATW5)
+	RESTORE_COP2_REGISTER(COP2_HSH_DATW6)
+	RESTORE_COP2_REGISTER(COP2_HSH_DATW7)
+	RESTORE_COP2_REGISTER(COP2_HSH_DATW8)
+	RESTORE_COP2_REGISTER(COP2_HSH_DATW9)
+	RESTORE_COP2_REGISTER(COP2_HSH_DATW10)
+	RESTORE_COP2_REGISTER(COP2_HSH_DATW11)
+	RESTORE_COP2_REGISTER(COP2_HSH_DATW12)
+	RESTORE_COP2_REGISTER(COP2_HSH_DATW13)
+	RESTORE_COP2_REGISTER(COP2_HSH_DATW14)
+	RESTORE_COP2_REGISTER(COP2_HSH_IVW0)
+	RESTORE_COP2_REGISTER(COP2_HSH_IVW1)
+	RESTORE_COP2_REGISTER(COP2_HSH_IVW2)
+	RESTORE_COP2_REGISTER(COP2_HSH_IVW3)
+	RESTORE_COP2_REGISTER(COP2_HSH_IVW4)
+	RESTORE_COP2_REGISTER(COP2_HSH_IVW5)
+	RESTORE_COP2_REGISTER(COP2_HSH_IVW6)
+	RESTORE_COP2_REGISTER(COP2_HSH_IVW7)
+	RESTORE_COP2_REGISTER(COP2_GFM_MULT0)
+	RESTORE_COP2_REGISTER(COP2_GFM_MULT1)
+	RESTORE_COP2_REGISTER(COP2_GFM_POLY)
+	RESTORE_COP2_REGISTER(COP2_GFM_RESULT0)
+	RESTORE_COP2_REGISTER(COP2_GFM_RESULT1)
+	jr ra
+	nop
+
+restore_pass1:
+	RESTORE_COP2_REGISTER(COP2_HSH_DATW0_PASS1)
+	RESTORE_COP2_REGISTER(COP2_HSH_DATW1_PASS1)
+	RESTORE_COP2_REGISTER(COP2_HSH_DATW2_PASS1)
+	RESTORE_COP2_REGISTER(COP2_HSH_DATW3_PASS1)
+	RESTORE_COP2_REGISTER(COP2_HSH_DATW4_PASS1)
+	RESTORE_COP2_REGISTER(COP2_HSH_DATW5_PASS1)
+	RESTORE_COP2_REGISTER(COP2_HSH_DATW6_PASS1)
+	RESTORE_COP2_REGISTER(COP2_HSH_IVW0_PASS1)
+	RESTORE_COP2_REGISTER(COP2_HSH_IVW1_PASS1)
+	RESTORE_COP2_REGISTER(COP2_HSH_IVW2_PASS1)
+
+restore_done:	
+	jr ra
+	nop
+END(octeon_cop2_restore)
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/mips/cavium/octeon_cop2.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/sys/mips/cavium/octeon_cop2.h	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,210 @@
+/*-
+ * Copyright (c) 2011, Oleksandr Tymoshenko <gonzo at FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice unmodified, this list of conditions, and the following
+ *    disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD: head/sys/mips/cavium/octeon_cop2.h 229677 2012-01-06 01:23:26Z gonzo $
+ *
+ */
+
+#ifndef __OCTEON_COP2_H__
+#define __OCTEON_COP2_H__
+
+/*
+ * COP2 registers of interest
+ */
+#define	COP2_CRC_IV		0x201
+#define	COP2_CRC_IV_SET		COP2_CRC_IV
+#define	COP2_CRC_LENGTH		0x202
+#define	COP2_CRC_LENGTH_SET	0x1202
+#define	COP2_CRC_POLY		0x200
+#define	COP2_CRC_POLY_SET	0x4200
+#define	COP2_LLM_DAT0		0x402
+#define	COP2_LLM_DAT0_SET	COP2_LLM_DAT0
+#define	COP2_LLM_DAT1		0x40A
+#define	COP2_LLM_DAT1_SET	COP2_LLM_DAT1
+#define	COP2_3DES_IV		0x084
+#define	COP2_3DES_IV_SET	COP2_3DES_IV
+#define	COP2_3DES_KEY0		0x080
+#define	COP2_3DES_KEY0_SET	COP2_3DES_KEY0
+#define	COP2_3DES_KEY1		0x081
+#define	COP2_3DES_KEY1_SET	COP2_3DES_KEY1
+#define	COP2_3DES_KEY2		0x082
+#define	COP2_3DES_KEY2_SET	COP2_3DES_KEY2
+#define	COP2_3DES_RESULT	0x088
+#define	COP2_3DES_RESULT_SET	0x098
+#define	COP2_AES_INP0		0x111
+#define	COP2_AES_INP0_SET	COP2_AES_INP0
+#define	COP2_AES_IV0		0x102
+#define	COP2_AES_IV0_SET	COP2_AES_IV0
+#define	COP2_AES_IV1		0x103
+#define	COP2_AES_IV1_SET	COP2_AES_IV1
+#define	COP2_AES_KEY0		0x104
+#define	COP2_AES_KEY0_SET	COP2_AES_KEY0
+#define	COP2_AES_KEY1		0x105
+#define	COP2_AES_KEY1_SET	COP2_AES_KEY1
+#define	COP2_AES_KEY2		0x106
+#define	COP2_AES_KEY2_SET	COP2_AES_KEY2
+#define	COP2_AES_KEY3		0x107
+#define	COP2_AES_KEY3_SET	COP2_AES_KEY3
+#define	COP2_AES_KEYLEN		0x110
+#define	COP2_AES_KEYLEN_SET	COP2_AES_KEYLEN
+#define	COP2_AES_RESULT0	0x100
+#define	COP2_AES_RESULT0_SET	COP2_AES_RESULT0
+#define	COP2_AES_RESULT1	0x101
+#define	COP2_AES_RESULT1_SET	COP2_AES_RESULT1
+#define	COP2_HSH_DATW0		0x240
+#define	COP2_HSH_DATW0_SET	COP2_HSH_DATW0
+#define	COP2_HSH_DATW1		0x241
+#define	COP2_HSH_DATW1_SET	COP2_HSH_DATW1
+#define	COP2_HSH_DATW2		0x242
+#define	COP2_HSH_DATW2_SET	COP2_HSH_DATW2
+#define	COP2_HSH_DATW3		0x243
+#define	COP2_HSH_DATW3_SET	COP2_HSH_DATW3
+#define	COP2_HSH_DATW4		0x244
+#define	COP2_HSH_DATW4_SET	COP2_HSH_DATW4
+#define	COP2_HSH_DATW5		0x245
+#define	COP2_HSH_DATW5_SET	COP2_HSH_DATW5
+#define	COP2_HSH_DATW6		0x246
+#define	COP2_HSH_DATW6_SET	COP2_HSH_DATW6
+#define	COP2_HSH_DATW7		0x247
+#define	COP2_HSH_DATW7_SET	COP2_HSH_DATW7
+#define	COP2_HSH_DATW8		0x248
+#define	COP2_HSH_DATW8_SET	COP2_HSH_DATW8
+#define	COP2_HSH_DATW9		0x249
+#define	COP2_HSH_DATW9_SET	COP2_HSH_DATW9
+#define	COP2_HSH_DATW10		0x24A
+#define	COP2_HSH_DATW10_SET	COP2_HSH_DATW10
+#define	COP2_HSH_DATW11		0x24B
+#define	COP2_HSH_DATW11_SET	COP2_HSH_DATW11
+#define	COP2_HSH_DATW12		0x24C
+#define	COP2_HSH_DATW12_SET	COP2_HSH_DATW12
+#define	COP2_HSH_DATW13		0x24D
+#define	COP2_HSH_DATW13_SET	COP2_HSH_DATW13
+#define	COP2_HSH_DATW14		0x24E
+#define	COP2_HSH_DATW14_SET	COP2_HSH_DATW14
+#define	COP2_HSH_IVW0		0x250
+#define	COP2_HSH_IVW0_SET	COP2_HSH_IVW0
+#define	COP2_HSH_IVW1		0x251
+#define	COP2_HSH_IVW1_SET	COP2_HSH_IVW1
+#define	COP2_HSH_IVW2		0x252
+#define	COP2_HSH_IVW2_SET	COP2_HSH_IVW2
+#define	COP2_HSH_IVW3		0x253
+#define	COP2_HSH_IVW3_SET	COP2_HSH_IVW3
+#define	COP2_HSH_IVW4		0x254
+#define	COP2_HSH_IVW4_SET	COP2_HSH_IVW4
+#define	COP2_HSH_IVW5		0x255
+#define	COP2_HSH_IVW5_SET	COP2_HSH_IVW5
+#define	COP2_HSH_IVW6		0x256
+#define	COP2_HSH_IVW6_SET	COP2_HSH_IVW6
+#define	COP2_HSH_IVW7		0x257
+#define	COP2_HSH_IVW7_SET	COP2_HSH_IVW7
+#define	COP2_GFM_MULT0		0x258
+#define	COP2_GFM_MULT0_SET	COP2_GFM_MULT0
+#define	COP2_GFM_MULT1		0x259
+#define	COP2_GFM_MULT1_SET	COP2_GFM_MULT1
+#define	COP2_GFM_POLY		0x25E
+#define	COP2_GFM_POLY_SET	COP2_GFM_POLY
+#define	COP2_GFM_RESULT0	0x25A
+#define	COP2_GFM_RESULT0_SET	COP2_GFM_RESULT0
+#define	COP2_GFM_RESULT1	0x25B
+#define	COP2_GFM_RESULT1_SET	COP2_GFM_RESULT1
+#define	COP2_HSH_DATW0_PASS1	0x040
+#define	COP2_HSH_DATW0_PASS1_SET	COP2_HSH_DATW0_PASS1
+#define	COP2_HSH_DATW1_PASS1	0x041
+#define	COP2_HSH_DATW1_PASS1_SET	COP2_HSH_DATW1_PASS1
+#define	COP2_HSH_DATW2_PASS1	0x042
+#define	COP2_HSH_DATW2_PASS1_SET	COP2_HSH_DATW2_PASS1
+#define	COP2_HSH_DATW3_PASS1	0x043
+#define	COP2_HSH_DATW3_PASS1_SET	COP2_HSH_DATW3_PASS1
+#define	COP2_HSH_DATW4_PASS1	0x044
+#define	COP2_HSH_DATW4_PASS1_SET	COP2_HSH_DATW4_PASS1
+#define	COP2_HSH_DATW5_PASS1	0x045
+#define	COP2_HSH_DATW5_PASS1_SET	COP2_HSH_DATW5_PASS1
+#define	COP2_HSH_DATW6_PASS1	0x046
+#define	COP2_HSH_DATW6_PASS1_SET	COP2_HSH_DATW6_PASS1
+#define	COP2_HSH_IVW0_PASS1	0x048
+#define	COP2_HSH_IVW0_PASS1_SET	COP2_HSH_IVW0_PASS1
+#define	COP2_HSH_IVW1_PASS1	0x049
+#define	COP2_HSH_IVW1_PASS1_SET	COP2_HSH_IVW1_PASS1
+#define	COP2_HSH_IVW2_PASS1	0x04A
+#define	COP2_HSH_IVW2_PASS1_SET	COP2_HSH_IVW2_PASS1
+
+#ifndef LOCORE
+
+struct octeon_cop2_state {
+	/* 3DES */
+        /* 0x0084 */
+        unsigned long   _3des_iv;
+        /* 0x0080..0x0082 */
+        unsigned long   _3des_key[3];
+        /* 0x0088, set: 0x0098 */
+        unsigned long   _3des_result;
+
+	/* AES */
+        /* 0x0111 */
+        unsigned long   aes_inp0;
+        /* 0x0102..0x0103 */
+        unsigned long   aes_iv[2];
+        /* 0x0104..0x0107 */
+        unsigned long   aes_key[4];
+        /* 0x0110 */
+        unsigned long   aes_keylen;
+        /* 0x0100..0x0101 */
+        unsigned long   aes_result[2];
+
+	/* CRC */
+        /*  0x0201 */
+        unsigned long   crc_iv;
+        /* 0x0202, set: 0x1202 */
+        unsigned long   crc_length;
+        /* 0x0200, set: 0x4200 */
+        unsigned long   crc_poly;
+
+	/* Low-latency memory stuff */
+        /* 0x0402, 0x040A */
+        unsigned long   llm_dat[2];
+
+	/* SHA & MD5 */
+        /* 0x0240..0x024E */
+        unsigned long   hsh_datw[15];
+        /* 0x0250..0x0257 */
+        unsigned long   hsh_ivw[8];
+
+	/* GFM */
+        /*  0x0258..0x0259 */
+        unsigned long   gfm_mult[2];
+        /* 0x025E */
+        unsigned long   gfm_poly;
+        /* 0x025A..0x025B */
+        unsigned long   gfm_result[2];
+};
+
+/* Prototypes */
+
+void octeon_cop2_save(struct octeon_cop2_state *);
+void octeon_cop2_restore(struct octeon_cop2_state *);
+
+#endif /* LOCORE */
+#endif /* __OCTEON_COP2_H__ */
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/mips/include/cpuregs.h
--- a/head/sys/mips/include/cpuregs.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/mips/include/cpuregs.h	Fri Jan 13 15:51:01 2012 +0200
@@ -52,7 +52,7 @@
  * from: Header: /sprite/src/kernel/vm/ds3100.md/RCS/vmPmaxConst.h,
  *	v 9.1 89/09/18 17:33:00 shirriff Exp  SPRITE (DECWRL)
  *
- * $FreeBSD: head/sys/mips/include/cpuregs.h 227658 2011-11-18 09:30:24Z jchandra $
+ * $FreeBSD: head/sys/mips/include/cpuregs.h 229677 2012-01-06 01:23:26Z gonzo $
  */
 
 #ifndef _MIPS_CPUREGS_H_
@@ -233,6 +233,7 @@
 #define	MIPS3_CR_EXC_CODE	0x0000007C	/* five bits */
 #define	MIPS_CR_IP		0x0000FF00
 #define	MIPS_CR_EXC_CODE_SHIFT	2
+#define	MIPS_CR_COP_ERR_SHIFT	28
 
 /*
  * The bits in the status register.  All bits are active when set to 1.
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/mips/include/frame.h
--- a/head/sys/mips/include/frame.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/mips/include/frame.h	Fri Jan 13 15:51:01 2012 +0200
@@ -31,7 +31,7 @@
  * SUCH DAMAGE.
  *
  *	JNPR: frame.h,v 1.6.2.1 2007/09/10 08:14:57 girish
- * $FreeBSD$
+ * $FreeBSD: head/sys/mips/include/frame.h 229677 2012-01-06 01:23:26Z gonzo $
  *
  */
 #ifndef _MACHINE_FRAME_H_
@@ -122,11 +122,6 @@
 	f_register_t	f31;
 	register_t	fsr;
         register_t   fdummy;
-	/*
-	 * COP2 registers may need to be saved here based on the CPU, and those
-	 * might need to be per process, or even for the kernel, so we need
-	 * some thought here.
-	 */
 };
 
 /* REVISIT */
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/mips/include/octeon_cop2.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/sys/mips/include/octeon_cop2.h	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,215 @@
+/*-
+ * Copyright (c) 2011, Oleksandr Tymoshenko <gonzo at FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice unmodified, this list of conditions, and the following
+ *    disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD: head/sys/mips/include/octeon_cop2.h 229677 2012-01-06 01:23:26Z gonzo $
+ *
+ */
+
+#ifndef __OCTEON_COP2_H__
+#define __OCTEON_COP2_H__
+
+/*
+ * COP2 registers of interest
+ */
+#define	COP2_CRC_IV		0x201
+#define	COP2_CRC_IV_SET		COP2_CRC_IV
+#define	COP2_CRC_LENGTH		0x202
+#define	COP2_CRC_LENGTH_SET	0x1202
+#define	COP2_CRC_POLY		0x200
+#define	COP2_CRC_POLY_SET	0x4200
+#define	COP2_LLM_DAT0		0x402
+#define	COP2_LLM_DAT0_SET	COP2_LLM_DAT0
+#define	COP2_LLM_DAT1		0x40A
+#define	COP2_LLM_DAT1_SET	COP2_LLM_DAT1
+#define	COP2_3DES_IV		0x084
+#define	COP2_3DES_IV_SET	COP2_3DES_IV
+#define	COP2_3DES_KEY0		0x080
+#define	COP2_3DES_KEY0_SET	COP2_3DES_KEY0
+#define	COP2_3DES_KEY1		0x081
+#define	COP2_3DES_KEY1_SET	COP2_3DES_KEY1
+#define	COP2_3DES_KEY2		0x082
+#define	COP2_3DES_KEY2_SET	COP2_3DES_KEY2
+#define	COP2_3DES_RESULT	0x088
+#define	COP2_3DES_RESULT_SET	0x098
+#define	COP2_AES_INP0		0x111
+#define	COP2_AES_INP0_SET	COP2_AES_INP0
+#define	COP2_AES_IV0		0x102
+#define	COP2_AES_IV0_SET	COP2_AES_IV0
+#define	COP2_AES_IV1		0x103
+#define	COP2_AES_IV1_SET	COP2_AES_IV1
+#define	COP2_AES_KEY0		0x104
+#define	COP2_AES_KEY0_SET	COP2_AES_KEY0
+#define	COP2_AES_KEY1		0x105
+#define	COP2_AES_KEY1_SET	COP2_AES_KEY1
+#define	COP2_AES_KEY2		0x106
+#define	COP2_AES_KEY2_SET	COP2_AES_KEY2
+#define	COP2_AES_KEY3		0x107
+#define	COP2_AES_KEY3_SET	COP2_AES_KEY3
+#define	COP2_AES_KEYLEN		0x110
+#define	COP2_AES_KEYLEN_SET	COP2_AES_KEYLEN
+#define	COP2_AES_RESULT0	0x100
+#define	COP2_AES_RESULT0_SET	COP2_AES_RESULT0
+#define	COP2_AES_RESULT1	0x101
+#define	COP2_AES_RESULT1_SET	COP2_AES_RESULT1
+#define	COP2_HSH_DATW0		0x240
+#define	COP2_HSH_DATW0_SET	COP2_HSH_DATW0
+#define	COP2_HSH_DATW1		0x241
+#define	COP2_HSH_DATW1_SET	COP2_HSH_DATW1
+#define	COP2_HSH_DATW2		0x242
+#define	COP2_HSH_DATW2_SET	COP2_HSH_DATW2
+#define	COP2_HSH_DATW3		0x243
+#define	COP2_HSH_DATW3_SET	COP2_HSH_DATW3
+#define	COP2_HSH_DATW4		0x244
+#define	COP2_HSH_DATW4_SET	COP2_HSH_DATW4
+#define	COP2_HSH_DATW5		0x245
+#define	COP2_HSH_DATW5_SET	COP2_HSH_DATW5
+#define	COP2_HSH_DATW6		0x246
+#define	COP2_HSH_DATW6_SET	COP2_HSH_DATW6
+#define	COP2_HSH_DATW7		0x247
+#define	COP2_HSH_DATW7_SET	COP2_HSH_DATW7
+#define	COP2_HSH_DATW8		0x248
+#define	COP2_HSH_DATW8_SET	COP2_HSH_DATW8
+#define	COP2_HSH_DATW9		0x249
+#define	COP2_HSH_DATW9_SET	COP2_HSH_DATW9
+#define	COP2_HSH_DATW10		0x24A
+#define	COP2_HSH_DATW10_SET	COP2_HSH_DATW10
+#define	COP2_HSH_DATW11		0x24B
+#define	COP2_HSH_DATW11_SET	COP2_HSH_DATW11
+#define	COP2_HSH_DATW12		0x24C
+#define	COP2_HSH_DATW12_SET	COP2_HSH_DATW12
+#define	COP2_HSH_DATW13		0x24D
+#define	COP2_HSH_DATW13_SET	COP2_HSH_DATW13
+#define	COP2_HSH_DATW14		0x24E
+#define	COP2_HSH_DATW14_SET	COP2_HSH_DATW14
+#define	COP2_HSH_IVW0		0x250
+#define	COP2_HSH_IVW0_SET	COP2_HSH_IVW0
+#define	COP2_HSH_IVW1		0x251
+#define	COP2_HSH_IVW1_SET	COP2_HSH_IVW1
+#define	COP2_HSH_IVW2		0x252
+#define	COP2_HSH_IVW2_SET	COP2_HSH_IVW2
+#define	COP2_HSH_IVW3		0x253
+#define	COP2_HSH_IVW3_SET	COP2_HSH_IVW3
+#define	COP2_HSH_IVW4		0x254
+#define	COP2_HSH_IVW4_SET	COP2_HSH_IVW4
+#define	COP2_HSH_IVW5		0x255
+#define	COP2_HSH_IVW5_SET	COP2_HSH_IVW5
+#define	COP2_HSH_IVW6		0x256
+#define	COP2_HSH_IVW6_SET	COP2_HSH_IVW6
+#define	COP2_HSH_IVW7		0x257
+#define	COP2_HSH_IVW7_SET	COP2_HSH_IVW7
+#define	COP2_GFM_MULT0		0x258
+#define	COP2_GFM_MULT0_SET	COP2_GFM_MULT0
+#define	COP2_GFM_MULT1		0x259
+#define	COP2_GFM_MULT1_SET	COP2_GFM_MULT1
+#define	COP2_GFM_POLY		0x25E
+#define	COP2_GFM_POLY_SET	COP2_GFM_POLY
+#define	COP2_GFM_RESULT0	0x25A
+#define	COP2_GFM_RESULT0_SET	COP2_GFM_RESULT0
+#define	COP2_GFM_RESULT1	0x25B
+#define	COP2_GFM_RESULT1_SET	COP2_GFM_RESULT1
+#define	COP2_HSH_DATW0_PASS1	0x040
+#define	COP2_HSH_DATW0_PASS1_SET	COP2_HSH_DATW0_PASS1
+#define	COP2_HSH_DATW1_PASS1	0x041
+#define	COP2_HSH_DATW1_PASS1_SET	COP2_HSH_DATW1_PASS1
+#define	COP2_HSH_DATW2_PASS1	0x042
+#define	COP2_HSH_DATW2_PASS1_SET	COP2_HSH_DATW2_PASS1
+#define	COP2_HSH_DATW3_PASS1	0x043
+#define	COP2_HSH_DATW3_PASS1_SET	COP2_HSH_DATW3_PASS1
+#define	COP2_HSH_DATW4_PASS1	0x044
+#define	COP2_HSH_DATW4_PASS1_SET	COP2_HSH_DATW4_PASS1
+#define	COP2_HSH_DATW5_PASS1	0x045
+#define	COP2_HSH_DATW5_PASS1_SET	COP2_HSH_DATW5_PASS1
+#define	COP2_HSH_DATW6_PASS1	0x046
+#define	COP2_HSH_DATW6_PASS1_SET	COP2_HSH_DATW6_PASS1
+#define	COP2_HSH_IVW0_PASS1	0x048
+#define	COP2_HSH_IVW0_PASS1_SET	COP2_HSH_IVW0_PASS1
+#define	COP2_HSH_IVW1_PASS1	0x049
+#define	COP2_HSH_IVW1_PASS1_SET	COP2_HSH_IVW1_PASS1
+#define	COP2_HSH_IVW2_PASS1	0x04A
+#define	COP2_HSH_IVW2_PASS1_SET	COP2_HSH_IVW2_PASS1
+
+#ifndef LOCORE
+
+struct octeon_cop2_state {
+	/* 3DES */
+	/* 0x0084 */
+	unsigned long   _3des_iv;
+	/* 0x0080..0x0082 */
+	unsigned long   _3des_key[3];
+	/* 0x0088, set: 0x0098 */
+	unsigned long   _3des_result;
+
+	/* AES */
+	/* 0x0111 */
+	unsigned long   aes_inp0;
+	/* 0x0102..0x0103 */
+	unsigned long   aes_iv[2];
+	/* 0x0104..0x0107 */
+	unsigned long   aes_key[4];
+	/* 0x0110 */
+	unsigned long   aes_keylen;
+	/* 0x0100..0x0101 */
+	unsigned long   aes_result[2];
+
+	/* CRC */
+	/*  0x0201 */
+	unsigned long   crc_iv;
+	/* 0x0202, set: 0x1202 */
+	unsigned long   crc_length;
+	/* 0x0200, set: 0x4200 */
+	unsigned long   crc_poly;
+
+	/* Low-latency memory stuff */
+	/* 0x0402, 0x040A */
+	unsigned long   llm_dat[2];
+
+	/* SHA & MD5 */
+	/* 0x0240..0x024E */
+	unsigned long   hsh_datw[15];
+	/* 0x0250..0x0257 */
+	unsigned long   hsh_ivw[8];
+
+	/* GFM */
+	/*  0x0258..0x0259 */
+	unsigned long   gfm_mult[2];
+	/* 0x025E */
+	unsigned long   gfm_poly;
+	/* 0x025A..0x025B */
+	unsigned long   gfm_result[2];
+};
+
+/* Prototypes */
+
+struct octeon_cop2_state* octeon_cop2_alloc_ctx(void);
+void octeon_cop2_free_ctx(struct octeon_cop2_state *);
+/*
+ * Save/restore part
+ */
+void octeon_cop2_save(struct octeon_cop2_state *);
+void octeon_cop2_restore(struct octeon_cop2_state *);
+
+#endif /* LOCORE */
+#endif /* __OCTEON_COP2_H__ */
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/mips/include/proc.h
--- a/head/sys/mips/include/proc.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/mips/include/proc.h	Fri Jan 13 15:51:01 2012 +0200
@@ -33,12 +33,16 @@
  *
  *	@(#)proc.h	8.1 (Berkeley) 6/10/93
  *	JNPR: proc.h,v 1.7.2.1 2007/09/10 06:25:24 girish
- * $FreeBSD: head/sys/mips/include/proc.h 226112 2011-10-07 16:09:44Z kib $
+ * $FreeBSD: head/sys/mips/include/proc.h 229677 2012-01-06 01:23:26Z gonzo $
  */
 
 #ifndef _MACHINE_PROC_H_
 #define	_MACHINE_PROC_H_
 
+#ifdef	CPU_CNMIPS
+#include <machine/octeon_cop2.h>
+#endif
+
 /*
  * Machine-dependent part of the proc structure.
  */
@@ -58,10 +62,18 @@
 	int		md_pc_count;	/* performance counter */
 	int		md_pc_spill;	/* performance counter spill */
 	void		*md_tls;
+#ifdef	CPU_CNMIPS
+	struct octeon_cop2_state	*md_cop2; /* kernel context */
+	struct octeon_cop2_state	*md_ucop2; /* userland context */
+#define	COP2_OWNER_USERLAND	0x0000		/* Userland owns COP2 */
+#define	COP2_OWNER_KERNEL	0x0001		/* Kernel owns COP2 */
+	int		md_cop2owner;
+#endif
 };
 
 /* md_flags */
 #define	MDTD_FPUSED	0x0001		/* Process used the FPU */
+#define	MDTD_COP2USED	0x0002		/* Process used the COP2 */
 
 struct mdproc {
 	/* empty */
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/mips/mips/db_disasm.c
--- a/head/sys/mips/mips/db_disasm.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/mips/mips/db_disasm.c	Fri Jan 13 15:51:01 2012 +0200
@@ -40,7 +40,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
+__FBSDID("$FreeBSD: head/sys/mips/mips/db_disasm.c 229798 2012-01-08 00:34:39Z gonzo $");
 
 #include <sys/param.h>
 #include <vm/vm_param.h>
@@ -388,5 +388,6 @@
 		    reg_name[i.IType.rt], reg_name[i.IType.rs],
 		    (short)i.IType.imm);
 	}
+	db_printf("\n");
 	return (delay);
 }
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/mips/mips/elf_machdep.c
--- a/head/sys/mips/mips/elf_machdep.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/mips/mips/elf_machdep.c	Fri Jan 13 15:51:01 2012 +0200
@@ -26,7 +26,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/mips/mips/elf_machdep.c 226065 2011-10-06 17:34:43Z kib $");
+__FBSDID("$FreeBSD: head/sys/mips/mips/elf_machdep.c 229803 2012-01-08 05:44:19Z gonzo $");
 
 #include <sys/param.h>
 #include <sys/kernel.h>
@@ -168,25 +168,41 @@
 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
     int type, int local, elf_lookup_fn lookup)
 {
-	Elf_Addr *where = (Elf_Addr *)NULL;
+	Elf32_Addr *where = (Elf32_Addr *)NULL;
 	Elf_Addr addr;
 	Elf_Addr addend = (Elf_Addr)0;
 	Elf_Word rtype = (Elf_Word)0, symidx;
-	const Elf_Rel *rel;
+	const Elf_Rel *rel = NULL;
+	const Elf_Rela *rela = NULL;
 
 	/*
 	 * Stash R_MIPS_HI16 info so we can use it when processing R_MIPS_LO16
 	 */
 	static Elf_Addr ahl;
-	static Elf_Addr *where_hi16;
+	static Elf32_Addr *where_hi16;
 
 	switch (type) {
 	case ELF_RELOC_REL:
 		rel = (const Elf_Rel *)data;
-		where = (Elf_Addr *) (relocbase + rel->r_offset);
-		addend = *where;
+		where = (Elf32_Addr *) (relocbase + rel->r_offset);
 		rtype = ELF_R_TYPE(rel->r_info);
 		symidx = ELF_R_SYM(rel->r_info);
+		switch (rtype) {
+		case R_MIPS_64:
+			addend = *(Elf64_Addr *)where;
+			break;
+		default:
+			addend = *where;
+			break;
+		}
+
+		break;
+	case ELF_RELOC_RELA:
+		rela = (const Elf_Rela *)data;
+		where = (Elf32_Addr *) (relocbase + rela->r_offset);
+		addend = rela->r_addend;
+		rtype = ELF_R_TYPE(rela->r_info);
+		symidx = ELF_R_SYM(rela->r_info);
 		break;
 	default:
 		panic("unknown reloc type %d\n", type);
@@ -202,7 +218,7 @@
 			return (-1);
 		addr += addend;
 		if (*where != addr)
-			*where = addr;
+			*where = (Elf32_Addr)addr;
 		break;
 
 	case R_MIPS_26:		/* ((A << 2) | (P & 0xf0000000) + S) >> 2 */
@@ -220,25 +236,73 @@
 		*where |= addr & 0x03ffffff;
 		break;
 
+	case R_MIPS_64:		/* S + A */
+		addr = lookup(lf, symidx, 1);
+		if (addr == 0)
+			return (-1);
+		addr += addend;
+		if (*(Elf64_Addr*)where != addr)
+			*(Elf64_Addr*)where = addr;
+		break;
+
 	case R_MIPS_HI16:	/* ((AHL + S) - ((short)(AHL + S)) >> 16 */
-		ahl = addend << 16;
-		where_hi16 = where;
+		if (rela != NULL) {
+			addr = lookup(lf, symidx, 1);
+			if (addr == 0)
+				return (-1);
+			addr += addend;
+			*where &= 0xffff0000;
+			*where |= ((((long long) addr + 0x8000LL) >> 16) & 0xffff);
+		}
+		else {
+			ahl = addend << 16;
+			where_hi16 = where;
+		}
 		break;
 
 	case R_MIPS_LO16:	/* AHL + S */
-		ahl += (int16_t)addend;
+		if (rela != NULL) {
+			addr = lookup(lf, symidx, 1);
+			if (addr == 0)
+				return (-1);
+			addr += addend;
+			*where &= 0xffff0000;
+			*where |= addr & 0xffff;
+		}
+		else {
+			ahl += (int16_t)addend;
+			addr = lookup(lf, symidx, 1);
+			if (addr == 0)
+				return (-1);
+
+			addend &= 0xffff0000;
+			addend |= (uint16_t)(ahl + addr);
+			*where = addend;
+
+			addend = *where_hi16;
+			addend &= 0xffff0000;
+			addend |= ((ahl + addr) - (int16_t)(ahl + addr)) >> 16;
+			*where_hi16 = addend;
+		}
+
+		break;
+
+	case R_MIPS_HIGHER:	/* %higher(A+S) */
 		addr = lookup(lf, symidx, 1);
 		if (addr == 0)
 			return (-1);
+		addr += addend;
+		*where &= 0xffff0000;
+		*where |= (((long long)addr + 0x80008000LL) >> 32) & 0xffff;
+		break;
 
-		addend &= 0xffff0000;
-		addend |= (uint16_t)(ahl + addr);
-		*where = addend;
-
-		addend = *where_hi16;
-		addend &= 0xffff0000;
-		addend |= ((ahl + addr) - (int16_t)(ahl + addr)) >> 16;
-		*where_hi16 = addend;
+	case R_MIPS_HIGHEST:	/* %highest(A+S) */
+		addr = lookup(lf, symidx, 1);
+		if (addr == 0)
+			return (-1);
+		addr += addend;
+		*where &= 0xffff0000;
+		*where |= (((long long)addr + 0x800080008000LL) >> 48) & 0xffff;
 		break;
 
 	default:
@@ -246,6 +310,7 @@
 			rtype);
 		return (-1);
 	}
+
 	return(0);
 }
 
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/mips/mips/exception.S
--- a/head/sys/mips/mips/exception.S	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/mips/mips/exception.S	Fri Jan 13 15:51:01 2012 +0200
@@ -46,7 +46,7 @@
  *	v 1.1 89/07/10 14:27:41 nelson Exp  SPRITE (DECWRL)
  *	from: @(#)locore.s	8.5 (Berkeley) 1/4/94
  *	JNPR: exception.S,v 1.5 2007/01/08 04:58:37 katta
- * $FreeBSD: head/sys/mips/mips/exception.S 226517 2011-10-18 16:37:28Z jchandra $
+ * $FreeBSD: head/sys/mips/mips/exception.S 229677 2012-01-06 01:23:26Z gonzo $
  */
 
 /*
@@ -375,15 +375,15 @@
 	REG_S	a3, CALLFRAME_RA + KERN_REG_SIZE(sp)		# for debugging
 
 	/*
-	 * Update interrupt mask in saved status register
+	 * Update interrupt and CPU mask in saved status register
 	 * Some of interrupts could be disabled by
 	 * intr filters if interrupts are enabled later
 	 * in trap handler
 	 */
 	mfc0	a0, MIPS_COP_0_STATUS
-	and	a0, a0, MIPS_SR_INT_MASK
+	and	a0, a0, (MIPS_SR_INT_MASK|MIPS_SR_COP_USABILITY)
 	RESTORE_REG(a1, SR, sp)
-	and	a1, a1, ~MIPS_SR_INT_MASK
+	and	a1, a1, ~(MIPS_SR_INT_MASK|MIPS_SR_COP_USABILITY)
 	or	a1, a1, a0
 	SAVE_REG(a1, SR, sp)
 	RESTORE_CPU			# v0 contains the return address.
@@ -469,6 +469,7 @@
 # Turn off fpu and enter kernel mode
 	and	t0, a0, ~(MIPS_SR_COP_1_BIT | MIPS_SR_EXL | MIPS3_SR_KSU_MASK | MIPS_SR_INT_IE)
 #if defined(CPU_CNMIPS)
+	and	t0, t0, ~(MIPS_SR_COP_2_BIT)
 	or      t0, t0, (MIPS_SR_KX | MIPS_SR_SX | MIPS_SR_UX | MIPS_SR_PX)
 #elif defined(CPU_RMI)	|| defined(CPU_NLM)
 	or      t0, t0, (MIPS_SR_KX | MIPS_SR_UX | MIPS_SR_COP_2_BIT)
@@ -631,15 +632,15 @@
 	REG_S	a3, CALLFRAME_RA + KERN_REG_SIZE(sp)		# for debugging
 
 	/*
-	 * Update interrupt mask in saved status register
+	 * Update interrupt and CPU mask in saved status register
 	 * Some of interrupts could be disabled by
 	 * intr filters if interrupts are enabled later
 	 * in trap handler
 	 */
 	mfc0	a0, MIPS_COP_0_STATUS
-	and	a0, a0, MIPS_SR_INT_MASK
+	and	a0, a0, (MIPS_SR_INT_MASK|MIPS_SR_COP_USABILITY)
 	RESTORE_REG(a1, SR, sp)
-	and	a1, a1, ~MIPS_SR_INT_MASK
+	and	a1, a1, ~(MIPS_SR_INT_MASK|MIPS_SR_COP_USABILITY)
 	or	a1, a1, a0
 	SAVE_REG(a1, SR, sp)
 	REG_L	v0, CALLFRAME_RA + KERN_REG_SIZE(sp)
@@ -727,6 +728,7 @@
 # Turn off fpu, disable interrupts, set kernel mode kernel mode, clear exception level.
 	and	t0, a0, ~(MIPS_SR_COP_1_BIT | MIPS_SR_EXL | MIPS_SR_INT_IE | MIPS3_SR_KSU_MASK)
 #ifdef CPU_CNMIPS
+	and	t0, t0, ~(MIPS_SR_COP_2_BIT)
 	or      t0, t0, (MIPS_SR_KX | MIPS_SR_SX | MIPS_SR_UX | MIPS_SR_PX)
 #elif defined(CPU_RMI)	|| defined(CPU_NLM)
 	or      t0, t0, (MIPS_SR_KX | MIPS_SR_UX | MIPS_SR_COP_2_BIT)
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/mips/mips/genassym.c
--- a/head/sys/mips/mips/genassym.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/mips/mips/genassym.c	Fri Jan 13 15:51:01 2012 +0200
@@ -35,7 +35,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/mips/mips/genassym.c 223562 2011-06-26 10:07:48Z kevlo $");
+__FBSDID("$FreeBSD: head/sys/mips/mips/genassym.c 229677 2012-01-06 01:23:26Z gonzo $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -58,11 +58,14 @@
 #include <machine/sigframe.h>
 #include <machine/proc.h>
 
+#ifdef	CPU_CNMIPS
+#include <machine/octeon_cop2.h>
+#endif
+
 #ifndef offsetof
 #define	offsetof(t,m) (int)((&((t *)0L)->m))
 #endif
 
-
 ASSYM(TD_PCB, offsetof(struct thread, td_pcb));
 ASSYM(TD_UPTE, offsetof(struct thread, td_md.md_upte));
 ASSYM(TD_KSTACK, offsetof(struct thread, td_kstack));
@@ -70,6 +73,7 @@
 ASSYM(TD_LOCK, offsetof(struct thread, td_lock));
 ASSYM(TD_FRAME, offsetof(struct thread, td_frame));
 ASSYM(TD_TLS, offsetof(struct thread, td_md.md_tls));
+ASSYM(TD_MDFLAGS, offsetof(struct thread, td_md.md_flags));
 
 ASSYM(TF_REG_SR, offsetof(struct trapframe, sr));
 
@@ -99,8 +103,73 @@
 ASSYM(TDF_ASTPENDING, TDF_ASTPENDING);
 ASSYM(PCPU_SIZE, sizeof(struct pcpu));
 ASSYM(MAXCOMLEN, MAXCOMLEN);
+ASSYM(MDTD_COP2USED, MDTD_COP2USED);
 
 ASSYM(MIPS_KSEG0_START, MIPS_KSEG0_START);
 ASSYM(MIPS_KSEG1_START, MIPS_KSEG1_START);
 ASSYM(MIPS_KSEG2_START, MIPS_KSEG2_START);
 ASSYM(MIPS_XKSEG_START, MIPS_XKSEG_START);
+
+#ifdef	CPU_CNMIPS
+ASSYM(TD_COP2OWNER, offsetof(struct thread, td_md.md_cop2owner));
+ASSYM(TD_COP2, offsetof(struct thread, td_md.md_cop2));
+ASSYM(TD_UCOP2, offsetof(struct thread, td_md.md_ucop2));
+ASSYM(COP2_CRC_IV_OFFSET, offsetof(struct octeon_cop2_state, crc_iv));
+ASSYM(COP2_CRC_LENGTH_OFFSET, offsetof(struct octeon_cop2_state, crc_length));
+ASSYM(COP2_CRC_POLY_OFFSET, offsetof(struct octeon_cop2_state, crc_poly));
+ASSYM(COP2_LLM_DAT0_OFFSET, offsetof(struct octeon_cop2_state, llm_dat));
+ASSYM(COP2_LLM_DAT1_OFFSET, offsetof(struct octeon_cop2_state, llm_dat) + 8);
+ASSYM(COP2_3DES_IV_OFFSET, offsetof(struct octeon_cop2_state, _3des_iv));
+ASSYM(COP2_3DES_KEY0_OFFSET, offsetof(struct octeon_cop2_state, _3des_key));
+ASSYM(COP2_3DES_KEY1_OFFSET, offsetof(struct octeon_cop2_state, _3des_key) + 8);
+ASSYM(COP2_3DES_KEY2_OFFSET, offsetof(struct octeon_cop2_state, _3des_key) + 16);
+ASSYM(COP2_3DES_RESULT_OFFSET, offsetof(struct octeon_cop2_state, _3des_result));
+ASSYM(COP2_AES_INP0_OFFSET, offsetof(struct octeon_cop2_state, aes_inp0));
+ASSYM(COP2_AES_IV0_OFFSET, offsetof(struct octeon_cop2_state, aes_iv));
+ASSYM(COP2_AES_IV1_OFFSET, offsetof(struct octeon_cop2_state, aes_iv) + 8);
+ASSYM(COP2_AES_KEY0_OFFSET, offsetof(struct octeon_cop2_state, aes_key));
+ASSYM(COP2_AES_KEY1_OFFSET, offsetof(struct octeon_cop2_state, aes_key) + 8);
+ASSYM(COP2_AES_KEY2_OFFSET, offsetof(struct octeon_cop2_state, aes_key) + 16);
+ASSYM(COP2_AES_KEY3_OFFSET, offsetof(struct octeon_cop2_state, aes_key) + 24);
+ASSYM(COP2_AES_KEYLEN_OFFSET, offsetof(struct octeon_cop2_state, aes_keylen));
+ASSYM(COP2_AES_RESULT0_OFFSET, offsetof(struct octeon_cop2_state, aes_result));
+ASSYM(COP2_AES_RESULT1_OFFSET, offsetof(struct octeon_cop2_state, aes_result) + 8);
+ASSYM(COP2_HSH_DATW0_OFFSET, offsetof(struct octeon_cop2_state, hsh_datw));
+ASSYM(COP2_HSH_DATW1_OFFSET, offsetof(struct octeon_cop2_state, hsh_datw) + 8);
+ASSYM(COP2_HSH_DATW2_OFFSET, offsetof(struct octeon_cop2_state, hsh_datw) + 16);
+ASSYM(COP2_HSH_DATW3_OFFSET, offsetof(struct octeon_cop2_state, hsh_datw) + 24);
+ASSYM(COP2_HSH_DATW4_OFFSET, offsetof(struct octeon_cop2_state, hsh_datw) + 32);
+ASSYM(COP2_HSH_DATW5_OFFSET, offsetof(struct octeon_cop2_state, hsh_datw) + 40);
+ASSYM(COP2_HSH_DATW6_OFFSET, offsetof(struct octeon_cop2_state, hsh_datw) + 48);
+ASSYM(COP2_HSH_DATW7_OFFSET, offsetof(struct octeon_cop2_state, hsh_datw) + 56);
+ASSYM(COP2_HSH_DATW8_OFFSET, offsetof(struct octeon_cop2_state, hsh_datw) + 64);
+ASSYM(COP2_HSH_DATW9_OFFSET, offsetof(struct octeon_cop2_state, hsh_datw) + 72);
+ASSYM(COP2_HSH_DATW10_OFFSET, offsetof(struct octeon_cop2_state, hsh_datw) + 80);
+ASSYM(COP2_HSH_DATW11_OFFSET, offsetof(struct octeon_cop2_state, hsh_datw) + 88);
+ASSYM(COP2_HSH_DATW12_OFFSET, offsetof(struct octeon_cop2_state, hsh_datw) + 96);
+ASSYM(COP2_HSH_DATW13_OFFSET, offsetof(struct octeon_cop2_state, hsh_datw) + 104);
+ASSYM(COP2_HSH_DATW14_OFFSET, offsetof(struct octeon_cop2_state, hsh_datw) + 112);
+ASSYM(COP2_HSH_IVW0_OFFSET, offsetof(struct octeon_cop2_state, hsh_ivw));
+ASSYM(COP2_HSH_IVW1_OFFSET, offsetof(struct octeon_cop2_state, hsh_ivw) + 8);
+ASSYM(COP2_HSH_IVW2_OFFSET, offsetof(struct octeon_cop2_state, hsh_ivw) + 16);
+ASSYM(COP2_HSH_IVW3_OFFSET, offsetof(struct octeon_cop2_state, hsh_ivw) + 24);
+ASSYM(COP2_HSH_IVW4_OFFSET, offsetof(struct octeon_cop2_state, hsh_ivw) + 32);
+ASSYM(COP2_HSH_IVW5_OFFSET, offsetof(struct octeon_cop2_state, hsh_ivw) + 40);
+ASSYM(COP2_HSH_IVW6_OFFSET, offsetof(struct octeon_cop2_state, hsh_ivw) + 48);
+ASSYM(COP2_HSH_IVW7_OFFSET, offsetof(struct octeon_cop2_state, hsh_ivw) + 56);
+ASSYM(COP2_GFM_MULT0_OFFSET, offsetof(struct octeon_cop2_state, gfm_mult));
+ASSYM(COP2_GFM_MULT1_OFFSET, offsetof(struct octeon_cop2_state, gfm_mult) + 8);
+ASSYM(COP2_GFM_POLY_OFFSET, offsetof(struct octeon_cop2_state, gfm_poly));
+ASSYM(COP2_GFM_RESULT0_OFFSET, offsetof(struct octeon_cop2_state, gfm_result));
+ASSYM(COP2_GFM_RESULT1_OFFSET, offsetof(struct octeon_cop2_state, gfm_result) + 8);
+ASSYM(COP2_HSH_DATW0_PASS1_OFFSET, offsetof(struct octeon_cop2_state, hsh_datw));
+ASSYM(COP2_HSH_DATW1_PASS1_OFFSET, offsetof(struct octeon_cop2_state, hsh_datw) + 8);
+ASSYM(COP2_HSH_DATW2_PASS1_OFFSET, offsetof(struct octeon_cop2_state, hsh_datw) + 16);
+ASSYM(COP2_HSH_DATW3_PASS1_OFFSET, offsetof(struct octeon_cop2_state, hsh_datw) + 24);
+ASSYM(COP2_HSH_DATW4_PASS1_OFFSET, offsetof(struct octeon_cop2_state, hsh_datw) + 32);
+ASSYM(COP2_HSH_DATW5_PASS1_OFFSET, offsetof(struct octeon_cop2_state, hsh_datw) + 40);
+ASSYM(COP2_HSH_DATW6_PASS1_OFFSET, offsetof(struct octeon_cop2_state, hsh_datw) + 48);
+ASSYM(COP2_HSH_IVW0_PASS1_OFFSET, offsetof(struct octeon_cop2_state, hsh_ivw));
+ASSYM(COP2_HSH_IVW1_PASS1_OFFSET, offsetof(struct octeon_cop2_state, hsh_ivw) + 8);
+ASSYM(COP2_HSH_IVW2_PASS1_OFFSET, offsetof(struct octeon_cop2_state, hsh_ivw) + 16);
+#endif
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/mips/mips/locore.S
--- a/head/sys/mips/mips/locore.S	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/mips/mips/locore.S	Fri Jan 13 15:51:01 2012 +0200
@@ -47,7 +47,7 @@
  *
  *	from: @(#)locore.s	8.5 (Berkeley) 1/4/94
  *	JNPR: locore.S,v 1.6.2.1 2007/08/29 12:24:49 girish
- * $FreeBSD: head/sys/mips/mips/locore.S 224115 2011-07-16 20:31:29Z jchandra $
+ * $FreeBSD: head/sys/mips/mips/locore.S 229677 2012-01-06 01:23:26Z gonzo $
  */
 
 /*
@@ -95,10 +95,10 @@
 	 */
 
 	/* Set these bits */
-        li	t1, (MIPS_SR_COP_2_BIT | MIPS_SR_COP_0_BIT | MIPS_SR_PX | MIPS_SR_KX | MIPS_SR_UX | MIPS_SR_SX | MIPS_SR_BEV)
+        li	t1, (MIPS_SR_COP_0_BIT | MIPS_SR_PX | MIPS_SR_KX | MIPS_SR_UX | MIPS_SR_SX | MIPS_SR_BEV)
 
 	/* Reset these bits */
-        li	t0, ~(MIPS_SR_DE | MIPS_SR_SOFT_RESET | MIPS_SR_ERL | MIPS_SR_EXL | MIPS_SR_INT_IE)
+        li	t0, ~(MIPS_SR_DE | MIPS_SR_SOFT_RESET | MIPS_SR_ERL | MIPS_SR_EXL | MIPS_SR_INT_IE | MIPS_SR_COP_2_BIT)
 #elif defined (CPU_RMI) || defined (CPU_NLM) 
 	/* Set these bits */
         li	t1, (MIPS_SR_COP_2_BIT | MIPS_SR_COP_0_BIT | MIPS_SR_KX | MIPS_SR_UX)
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/mips/mips/octeon_cop2.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/sys/mips/mips/octeon_cop2.c	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,62 @@
+/*-
+ * Copyright (c) 2011, Oleksandr Tymoshenko <gonzo at FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice unmodified, this list of conditions, and the following
+ *    disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD: head/sys/mips/mips/octeon_cop2.c 229677 2012-01-06 01:23:26Z gonzo $");
+
+#include <sys/types.h>
+#include <sys/systm.h>
+#include <sys/param.h>
+#include <sys/kernel.h>
+#include <vm/uma.h>
+
+#include <machine/octeon_cop2.h>
+
+static uma_zone_t ctxzone;
+
+static void 
+octeon_cop2_init(void* dummy)
+{
+	printf("Create COP2 context zone\n");
+	ctxzone = uma_zcreate("COP2 context",
+	                        sizeof(struct octeon_cop2_state), 
+				NULL, NULL, NULL, NULL, 8, 0);
+}
+
+struct octeon_cop2_state *
+octeon_cop2_alloc_ctx()
+{
+	return uma_zalloc(ctxzone, M_NOWAIT);
+}
+
+void
+octeon_cop2_free_ctx(struct octeon_cop2_state *ctx)
+{
+	uma_zfree(ctxzone, ctx);
+}
+
+SYSINIT(octeon_cop2, SI_SUB_CPU, SI_ORDER_FIRST, octeon_cop2_init, NULL);
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/mips/mips/octeon_cop2_swtch.S
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/sys/mips/mips/octeon_cop2_swtch.S	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,246 @@
+/*-
+ * Copyright (c) 2011 Oleksandr Tymoshenko
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD: head/sys/mips/mips/octeon_cop2_swtch.S 229677 2012-01-06 01:23:26Z gonzo $
+ */
+
+#include <machine/asm.h>
+#include <machine/cpuregs.h>
+#include <machine/octeon_cop2.h>
+
+#include "assym.s"
+
+.set noreorder
+
+#define SAVE_COP2_REGISTER(reg)				\
+	dmfc2	t1, reg; sd	t1, reg##_OFFSET(a0)
+
+
+#define RESTORE_COP2_REGISTER(reg)			\
+	ld	t1, reg##_OFFSET(a0); dmtc2	t1, reg##_SET
+ 
+LEAF(octeon_cop2_save)
+
+	/* save original cop2 status in t2*/
+	mfc0	t2, MIPS_COP_0_STATUS
+	or	t0, t2, MIPS_SR_COP_2_BIT
+	and	t0, t0, ~MIPS_SR_INT_IE
+	mtc0	t0, MIPS_COP_0_STATUS
+
+	/* Get CvmCtl register */
+	dmfc0	t0, $9, 7
+
+	/* CRC state */
+	SAVE_COP2_REGISTER(COP2_CRC_IV)
+	SAVE_COP2_REGISTER(COP2_CRC_LENGTH)
+	SAVE_COP2_REGISTER(COP2_CRC_POLY)
+
+	/* if CvmCtl[NODFA_CP2] -> save_nodfa */
+	bbit1	t0, 28, save_nodfa
+	nop
+
+	/* LLM state */
+	SAVE_COP2_REGISTER(COP2_LLM_DAT0)
+	SAVE_COP2_REGISTER(COP2_LLM_DAT1)
+
+save_nodfa:
+	/* crypto stuff is irrelevant if CvmCtl[NOCRYPTO]  */
+	bbit1	t0, 26, save_done
+	nop
+
+	SAVE_COP2_REGISTER(COP2_3DES_IV)
+	SAVE_COP2_REGISTER(COP2_3DES_KEY0)
+	SAVE_COP2_REGISTER(COP2_3DES_KEY1)
+	SAVE_COP2_REGISTER(COP2_3DES_KEY2)
+	SAVE_COP2_REGISTER(COP2_3DES_RESULT)
+
+	SAVE_COP2_REGISTER(COP2_AES_INP0)
+	SAVE_COP2_REGISTER(COP2_AES_IV0)
+	SAVE_COP2_REGISTER(COP2_AES_IV1)
+	SAVE_COP2_REGISTER(COP2_AES_KEY0)
+	SAVE_COP2_REGISTER(COP2_AES_KEY1)
+	SAVE_COP2_REGISTER(COP2_AES_KEY2)
+	SAVE_COP2_REGISTER(COP2_AES_KEY3)
+	SAVE_COP2_REGISTER(COP2_AES_KEYLEN)
+	SAVE_COP2_REGISTER(COP2_AES_RESULT0)
+	SAVE_COP2_REGISTER(COP2_AES_RESULT1)
+
+	dmfc0	t0, $15
+	li	t1, 0x000d0000 /* Octeon Pass1 */
+	beq	t0, t1, save_pass1
+	nop
+
+	SAVE_COP2_REGISTER(COP2_HSH_DATW0)
+	SAVE_COP2_REGISTER(COP2_HSH_DATW1)
+	SAVE_COP2_REGISTER(COP2_HSH_DATW2)
+	SAVE_COP2_REGISTER(COP2_HSH_DATW3)
+	SAVE_COP2_REGISTER(COP2_HSH_DATW4)
+	SAVE_COP2_REGISTER(COP2_HSH_DATW5)
+	SAVE_COP2_REGISTER(COP2_HSH_DATW6)
+	SAVE_COP2_REGISTER(COP2_HSH_DATW7)
+	SAVE_COP2_REGISTER(COP2_HSH_DATW8)
+	SAVE_COP2_REGISTER(COP2_HSH_DATW9)
+	SAVE_COP2_REGISTER(COP2_HSH_DATW10)
+	SAVE_COP2_REGISTER(COP2_HSH_DATW11)
+	SAVE_COP2_REGISTER(COP2_HSH_DATW12)
+	SAVE_COP2_REGISTER(COP2_HSH_DATW13)
+	SAVE_COP2_REGISTER(COP2_HSH_DATW14)
+	SAVE_COP2_REGISTER(COP2_HSH_IVW0)
+	SAVE_COP2_REGISTER(COP2_HSH_IVW1)
+	SAVE_COP2_REGISTER(COP2_HSH_IVW2)
+	SAVE_COP2_REGISTER(COP2_HSH_IVW3)
+	SAVE_COP2_REGISTER(COP2_HSH_IVW4)
+	SAVE_COP2_REGISTER(COP2_HSH_IVW5)
+	SAVE_COP2_REGISTER(COP2_HSH_IVW6)
+	SAVE_COP2_REGISTER(COP2_HSH_IVW7)
+	SAVE_COP2_REGISTER(COP2_GFM_MULT0)
+	SAVE_COP2_REGISTER(COP2_GFM_MULT1)
+	SAVE_COP2_REGISTER(COP2_GFM_POLY)
+	SAVE_COP2_REGISTER(COP2_GFM_RESULT0)
+	SAVE_COP2_REGISTER(COP2_GFM_RESULT1)
+	/* restore saved COP2 status */
+	mtc0	t2, MIPS_COP_0_STATUS
+	jr ra
+	nop
+
+save_pass1:
+	SAVE_COP2_REGISTER(COP2_HSH_DATW0_PASS1)
+	SAVE_COP2_REGISTER(COP2_HSH_DATW1_PASS1)
+	SAVE_COP2_REGISTER(COP2_HSH_DATW2_PASS1)
+	SAVE_COP2_REGISTER(COP2_HSH_DATW3_PASS1)
+	SAVE_COP2_REGISTER(COP2_HSH_DATW4_PASS1)
+	SAVE_COP2_REGISTER(COP2_HSH_DATW5_PASS1)
+	SAVE_COP2_REGISTER(COP2_HSH_DATW6_PASS1)
+	SAVE_COP2_REGISTER(COP2_HSH_IVW0_PASS1)
+	SAVE_COP2_REGISTER(COP2_HSH_IVW1_PASS1)
+	SAVE_COP2_REGISTER(COP2_HSH_IVW2_PASS1)
+
+save_done:	
+	/* restore saved COP2 status */
+	mtc0	t2, MIPS_COP_0_STATUS
+	jr ra
+	nop
+END(octeon_cop2_save)
+
+LEAF(octeon_cop2_restore)
+	/* save original cop2 status in t2*/
+	mfc0	t2, MIPS_COP_0_STATUS
+	or	t0, t2, MIPS_SR_COP_2_BIT
+	and	t0, t0, ~MIPS_SR_INT_IE
+	mtc0	t0, MIPS_COP_0_STATUS
+	/* Get CvmCtl register */
+	dmfc0	t0, $9, 7
+
+	/* CRC state */
+	RESTORE_COP2_REGISTER(COP2_CRC_IV)
+	RESTORE_COP2_REGISTER(COP2_CRC_LENGTH)
+	RESTORE_COP2_REGISTER(COP2_CRC_POLY)
+
+	/* if CvmCtl[NODFA_CP2] -> save_nodfa */
+	bbit1	t0, 28, restore_nodfa
+	nop
+
+	/* LLM state */
+	RESTORE_COP2_REGISTER(COP2_LLM_DAT0)
+	RESTORE_COP2_REGISTER(COP2_LLM_DAT1)
+
+restore_nodfa:
+	/* crypto stuff is irrelevant if CvmCtl[NOCRYPTO]  */
+	bbit1	t0, 26, restore_done
+	nop
+
+	RESTORE_COP2_REGISTER(COP2_3DES_IV)
+	RESTORE_COP2_REGISTER(COP2_3DES_KEY0)
+	RESTORE_COP2_REGISTER(COP2_3DES_KEY1)
+	RESTORE_COP2_REGISTER(COP2_3DES_KEY2)
+	RESTORE_COP2_REGISTER(COP2_3DES_RESULT)
+
+	RESTORE_COP2_REGISTER(COP2_AES_INP0)
+	RESTORE_COP2_REGISTER(COP2_AES_IV0)
+	RESTORE_COP2_REGISTER(COP2_AES_IV1)
+	RESTORE_COP2_REGISTER(COP2_AES_KEY0)
+	RESTORE_COP2_REGISTER(COP2_AES_KEY1)
+	RESTORE_COP2_REGISTER(COP2_AES_KEY2)
+	RESTORE_COP2_REGISTER(COP2_AES_KEY3)
+	RESTORE_COP2_REGISTER(COP2_AES_KEYLEN)
+	RESTORE_COP2_REGISTER(COP2_AES_RESULT0)
+	RESTORE_COP2_REGISTER(COP2_AES_RESULT1)
+
+	dmfc0	t0, $15
+	li	t1, 0x000d0000 /* Octeon Pass1 */
+	beq	t0, t1, restore_pass1
+	nop
+
+	RESTORE_COP2_REGISTER(COP2_HSH_DATW0)
+	RESTORE_COP2_REGISTER(COP2_HSH_DATW1)
+	RESTORE_COP2_REGISTER(COP2_HSH_DATW2)
+	RESTORE_COP2_REGISTER(COP2_HSH_DATW3)
+	RESTORE_COP2_REGISTER(COP2_HSH_DATW4)
+	RESTORE_COP2_REGISTER(COP2_HSH_DATW5)
+	RESTORE_COP2_REGISTER(COP2_HSH_DATW6)
+	RESTORE_COP2_REGISTER(COP2_HSH_DATW7)
+	RESTORE_COP2_REGISTER(COP2_HSH_DATW8)
+	RESTORE_COP2_REGISTER(COP2_HSH_DATW9)
+	RESTORE_COP2_REGISTER(COP2_HSH_DATW10)
+	RESTORE_COP2_REGISTER(COP2_HSH_DATW11)
+	RESTORE_COP2_REGISTER(COP2_HSH_DATW12)
+	RESTORE_COP2_REGISTER(COP2_HSH_DATW13)
+	RESTORE_COP2_REGISTER(COP2_HSH_DATW14)
+	RESTORE_COP2_REGISTER(COP2_HSH_IVW0)
+	RESTORE_COP2_REGISTER(COP2_HSH_IVW1)
+	RESTORE_COP2_REGISTER(COP2_HSH_IVW2)
+	RESTORE_COP2_REGISTER(COP2_HSH_IVW3)
+	RESTORE_COP2_REGISTER(COP2_HSH_IVW4)
+	RESTORE_COP2_REGISTER(COP2_HSH_IVW5)
+	RESTORE_COP2_REGISTER(COP2_HSH_IVW6)
+	RESTORE_COP2_REGISTER(COP2_HSH_IVW7)
+	RESTORE_COP2_REGISTER(COP2_GFM_MULT0)
+	RESTORE_COP2_REGISTER(COP2_GFM_MULT1)
+	RESTORE_COP2_REGISTER(COP2_GFM_POLY)
+	RESTORE_COP2_REGISTER(COP2_GFM_RESULT0)
+	RESTORE_COP2_REGISTER(COP2_GFM_RESULT1)
+	/* restore saved COP2 status */
+	mtc0	t2, MIPS_COP_0_STATUS
+	jr ra
+	nop
+
+restore_pass1:
+	RESTORE_COP2_REGISTER(COP2_HSH_DATW0_PASS1)
+	RESTORE_COP2_REGISTER(COP2_HSH_DATW1_PASS1)
+	RESTORE_COP2_REGISTER(COP2_HSH_DATW2_PASS1)
+	RESTORE_COP2_REGISTER(COP2_HSH_DATW3_PASS1)
+	RESTORE_COP2_REGISTER(COP2_HSH_DATW4_PASS1)
+	RESTORE_COP2_REGISTER(COP2_HSH_DATW5_PASS1)
+	RESTORE_COP2_REGISTER(COP2_HSH_DATW6_PASS1)
+	RESTORE_COP2_REGISTER(COP2_HSH_IVW0_PASS1)
+	RESTORE_COP2_REGISTER(COP2_HSH_IVW1_PASS1)
+	RESTORE_COP2_REGISTER(COP2_HSH_IVW2_PASS1)
+
+restore_done:	
+	/* restore saved COP2 status */
+	mtc0	t2, MIPS_COP_0_STATUS
+	jr ra
+	nop
+END(octeon_cop2_restore)
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/mips/mips/pm_machdep.c
--- a/head/sys/mips/mips/pm_machdep.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/mips/mips/pm_machdep.c	Fri Jan 13 15:51:01 2012 +0200
@@ -36,7 +36,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/mips/mips/pm_machdep.c 225617 2011-09-16 13:58:51Z kmacy $");
+__FBSDID("$FreeBSD: head/sys/mips/mips/pm_machdep.c 229677 2012-01-06 01:23:26Z gonzo $");
 
 #include "opt_compat.h"
 #include "opt_cputype.h"
@@ -520,7 +520,7 @@
 	td->td_frame->sr |= MIPS_SR_PX | MIPS_SR_UX | MIPS_SR_KX;
 #endif
 #ifdef CPU_CNMIPS
-	td->td_frame->sr |= MIPS_SR_COP_2_BIT | MIPS_SR_PX | MIPS_SR_UX |
+	td->td_frame->sr |= MIPS_SR_PX | MIPS_SR_UX |
 	    MIPS_SR_KX | MIPS_SR_SX;
 #endif
 	/*
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/mips/mips/swtch.S
--- a/head/sys/mips/mips/swtch.S	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/mips/mips/swtch.S	Fri Jan 13 15:51:01 2012 +0200
@@ -47,7 +47,7 @@
  *
  *	from: @(#)locore.s	8.5 (Berkeley) 1/4/94
  *	JNPR: swtch.S,v 1.6.2.1 2007/09/10 10:36:50 girish
- * $FreeBSD$
+ * $FreeBSD: head/sys/mips/mips/swtch.S 229677 2012-01-06 01:23:26Z gonzo $
  */
 
 /*
@@ -250,6 +250,61 @@
 getpc:
 	SAVE_U_PCB_CONTEXT(ra, PREG_PC, a0)		# save return address
 
+#ifdef CPU_CNMIPS
+
+	lw	t2, TD_MDFLAGS(a3)		# get md_flags
+	and	t1, t2, MDTD_COP2USED
+	beqz	t1, cop2_untouched
+	nop
+
+	/* Clear cop2used flag */
+	and	t2, t2, ~MDTD_COP2USED
+	sw	t2, TD_MDFLAGS(a3)
+
+	and	t2, t0, ~MIPS_SR_COP_2_BIT	# clear COP_2 enable bit
+	SAVE_U_PCB_CONTEXT(t2, PREG_SR, a0)	# save status register
+
+	RESTORE_U_PCB_REG(t0, PS, a0)		# get CPU status register
+	and	t2, t0, ~MIPS_SR_COP_2_BIT	# clear COP_2 enable bit
+	SAVE_U_PCB_REG(t2, PS, a0)		# save stratus register
+
+	/* preserve a0..a3 */
+	move	s0, a0
+	move	s1, a1
+	move	s2, a2
+	move	s3, a3
+
+	/* does kernel own COP2 context? */
+	lw	t1, TD_COP2OWNER(a3)		# get md_cop2owner
+	beqz	t1, userland_cop2		# 0 - it's userland context
+	nop
+
+	PTR_L	a0, TD_COP2(a3)
+	beqz	a0, no_cop2_context
+	nop
+
+	j	do_cop2_save
+	nop
+
+userland_cop2:
+
+	PTR_L	a0, TD_UCOP2(a3)
+	beqz	a0, no_cop2_context
+	nop
+
+do_cop2_save:
+	jal	octeon_cop2_save
+	nop
+
+no_cop2_context:
+	move	a3, s3
+	move	a2, s2
+	move	a1, s1
+	move	a0, s0
+
+cop2_untouched:
+#endif
+
 	PTR_S	a2, TD_LOCK(a3)			# Switchout td_lock 
 
 mips_sw1:
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/mips/mips/trap.c
--- a/head/sys/mips/mips/trap.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/mips/mips/trap.c	Fri Jan 13 15:51:01 2012 +0200
@@ -39,7 +39,7 @@
  *	JNPR: trap.c,v 1.13.2.2 2007/08/29 10:03:49 girish
  */
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/mips/mips/trap.c 226065 2011-10-06 17:34:43Z kib $");
+__FBSDID("$FreeBSD: head/sys/mips/mips/trap.c 229677 2012-01-06 01:23:26Z gonzo $");
 
 #include "opt_ddb.h"
 #include "opt_global.h"
@@ -251,6 +251,9 @@
 	"Store Doubleword"
 };
 
+#ifdef	CPU_CNMIPS
+#include <machine/octeon_cop2.h>
+#endif
 
 static int allow_unaligned_acc = 1;
 
@@ -410,6 +413,7 @@
 	char *msg = NULL;
 	intptr_t addr = 0;
 	register_t pc;
+	int cop;
 
 	trapdebug_enter(trapframe, 0);
 	
@@ -767,28 +771,91 @@
 		goto err;
 		break;
 	case T_COP_UNUSABLE:
+#ifdef	CPU_CNMIPS
+		cop = (trapframe->cause & MIPS_CR_COP_ERR) >> MIPS_CR_COP_ERR_SHIFT;
+		/* Handle only COP2 exception */
+		if (cop != 2)
+			goto err;
+
+		addr = trapframe->pc;
+		/* save userland cop2 context if it has been touched */
+		if ((td->td_md.md_flags & MDTD_COP2USED) &&
+		    (td->td_md.md_cop2owner == COP2_OWNER_USERLAND)) {
+			if (td->td_md.md_ucop2)
+				octeon_cop2_save(td->td_md.md_ucop2);
+			else
+				panic("COP2 was used in user mode but md_ucop2 is NULL");
+		}
+
+		if (td->td_md.md_cop2 == NULL) {
+			td->td_md.md_cop2 = octeon_cop2_alloc_ctx();
+			if (td->td_md.md_cop2 == NULL)
+				panic("Failed to allocate COP2 context");
+			memset(td->td_md.md_cop2, 0, sizeof(*td->td_md.md_cop2));
+		}
+
+		octeon_cop2_restore(td->td_md.md_cop2);
+		
+		/* Make userland re-request its context */
+		td->td_frame->sr &= ~MIPS_SR_COP_2_BIT;
+		td->td_md.md_flags |= MDTD_COP2USED;
+		td->td_md.md_cop2owner = COP2_OWNER_KERNEL;
+		/* Enable COP2, it will be disabled in cpu_switch */
+		mips_wr_status(mips_rd_status() | MIPS_SR_COP_2_BIT);
+		return (trapframe->pc);
+#else
 		goto err;
 		break;
+#endif
+
 	case T_COP_UNUSABLE + T_USER:
+		cop = (trapframe->cause & MIPS_CR_COP_ERR) >> MIPS_CR_COP_ERR_SHIFT;
+		if (cop == 1) {
 #if !defined(CPU_HAVEFPU)
 		/* FP (COP1) instruction */
-		if ((trapframe->cause & MIPS_CR_COP_ERR) == 0x10000000) {
 			log_illegal_instruction("COP1_UNUSABLE", trapframe);
 			i = SIGILL;
 			break;
+#else
+			addr = trapframe->pc;
+			MipsSwitchFPState(PCPU_GET(fpcurthread), td->td_frame);
+			PCPU_SET(fpcurthread, td);
+			td->td_frame->sr |= MIPS_SR_COP_1_BIT;
+			td->td_md.md_flags |= MDTD_FPUSED;
+			goto out;
+#endif
+		}
+#ifdef	CPU_CNMIPS
+		else  if (cop == 2) {
+			addr = trapframe->pc;
+			if ((td->td_md.md_flags & MDTD_COP2USED) &&
+			    (td->td_md.md_cop2owner == COP2_OWNER_KERNEL)) {
+				if (td->td_md.md_cop2)
+					octeon_cop2_save(td->td_md.md_cop2);
+				else
+					panic("COP2 was used in kernel mode but md_cop2 is NULL");
+			}
+
+			if (td->td_md.md_ucop2 == NULL) {
+				td->td_md.md_ucop2 = octeon_cop2_alloc_ctx();
+				if (td->td_md.md_ucop2 == NULL)
+					panic("Failed to allocate userland COP2 context");
+				memset(td->td_md.md_ucop2, 0, sizeof(*td->td_md.md_ucop2));
+			}
+
+			octeon_cop2_restore(td->td_md.md_ucop2);
+
+			td->td_frame->sr |= MIPS_SR_COP_2_BIT;
+			td->td_md.md_flags |= MDTD_COP2USED;
+			td->td_md.md_cop2owner = COP2_OWNER_USERLAND;
+			goto out;
 		}
 #endif
-		if ((trapframe->cause & MIPS_CR_COP_ERR) != 0x10000000) {
+		else {
 			log_illegal_instruction("COPn_UNUSABLE", trapframe);
 			i = SIGILL;	/* only FPU instructions allowed */
 			break;
 		}
-		addr = trapframe->pc;
-		MipsSwitchFPState(PCPU_GET(fpcurthread), td->td_frame);
-		PCPU_SET(fpcurthread, td);
-		td->td_frame->sr |= MIPS_SR_COP_1_BIT;
-		td->td_md.md_flags |= MDTD_FPUSED;
-		goto out;
 
 	case T_FPE:
 #if !defined(SMP) && (defined(DDB) || defined(DEBUG))
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/mips/mips/vm_machdep.c
--- a/head/sys/mips/mips/vm_machdep.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/mips/mips/vm_machdep.c	Fri Jan 13 15:51:01 2012 +0200
@@ -39,7 +39,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
+__FBSDID("$FreeBSD: head/sys/mips/mips/vm_machdep.c 229677 2012-01-06 01:23:26Z gonzo $");
 
 #include "opt_cputype.h"
 #include "opt_ddb.h"
@@ -163,7 +163,36 @@
 	td2->td_md.md_saved_intr = MIPS_SR_INT_IE;
 	td2->td_md.md_spinlock_count = 1;
 #ifdef CPU_CNMIPS
-	pcb2->pcb_context[PCB_REG_SR] |= MIPS_SR_COP_2_BIT | MIPS_SR_PX | MIPS_SR_UX | MIPS_SR_KX | MIPS_SR_SX;
+	if (td1->td_md.md_flags & MDTD_COP2USED) {
+		if (td1->td_md.md_cop2owner == COP2_OWNER_USERLAND) {
+			if (td1->td_md.md_ucop2)
+				octeon_cop2_save(td1->td_md.md_ucop2);
+			else
+				panic("cpu_fork: ucop2 is NULL but COP2 is enabled");
+		}
+		else {
+			if (td1->td_md.md_cop2)
+				octeon_cop2_save(td1->td_md.md_cop2);
+			else
+				panic("cpu_fork: cop2 is NULL but COP2 is enabled");
+		}
+	}
+
+	if (td1->td_md.md_cop2) {
+		td2->td_md.md_cop2 = octeon_cop2_alloc_ctx();
+		memcpy(td2->td_md.md_cop2, td1->td_md.md_cop2, 
+			sizeof(*td1->td_md.md_cop2));
+	}
+	if (td1->td_md.md_ucop2) {
+		td2->td_md.md_ucop2 = octeon_cop2_alloc_ctx();
+		memcpy(td2->td_md.md_ucop2, td1->td_md.md_ucop2, 
+			sizeof(*td1->td_md.md_ucop2));
+	}
+	td2->td_md.md_cop2owner = td1->td_md.md_cop2owner;
+	pcb2->pcb_context[PCB_REG_SR] |= MIPS_SR_PX | MIPS_SR_UX | MIPS_SR_KX | MIPS_SR_SX;
+	/* Clear COP2 bits for userland & kernel */
+	td2->td_frame->sr &= ~MIPS_SR_COP_2_BIT;
+	pcb2->pcb_context[PCB_REG_SR] &= ~MIPS_SR_COP_2_BIT;
 #endif
 }
 
@@ -195,11 +224,27 @@
 
 	if (PCPU_GET(fpcurthread) == td)
 		PCPU_GET(fpcurthread) = (struct thread *)0;
+#ifdef  CPU_CNMIPS
+	if (td->td_md.md_cop2)
+		memset(td->td_md.md_cop2, 0,
+			sizeof(*td->td_md.md_cop2));
+	if (td->td_md.md_ucop2)
+		memset(td->td_md.md_ucop2, 0,
+			sizeof(*td->td_md.md_ucop2));
+#endif
 }
 
 void
 cpu_thread_free(struct thread *td)
 {
+#ifdef  CPU_CNMIPS
+	if (td->td_md.md_cop2)
+		octeon_cop2_free_ctx(td->td_md.md_cop2);
+	if (td->td_md.md_ucop2)
+		octeon_cop2_free_ctx(td->td_md.md_ucop2);
+	td->td_md.md_cop2 = NULL;
+	td->td_md.md_ucop2 = NULL;
+#endif
 }
 
 void
@@ -357,7 +402,7 @@
 	    (MIPS_SR_KX | MIPS_SR_UX | MIPS_SR_INT_MASK);
 
 #ifdef CPU_CNMIPS
-	pcb2->pcb_context[PCB_REG_SR] |= MIPS_SR_COP_2_BIT | MIPS_SR_COP_0_BIT |
+	pcb2->pcb_context[PCB_REG_SR] |= MIPS_SR_COP_0_BIT |
 	  MIPS_SR_PX | MIPS_SR_UX | MIPS_SR_KX | MIPS_SR_SX;
 #endif
 
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/modules/acpi/acpi/Makefile
--- a/head/sys/modules/acpi/acpi/Makefile	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/modules/acpi/acpi/Makefile	Fri Jan 13 15:51:01 2012 +0200
@@ -1,4 +1,4 @@
-# $FreeBSD: head/sys/modules/acpi/acpi/Makefile 228110 2011-11-28 23:36:48Z jkim $
+# $FreeBSD: head/sys/modules/acpi/acpi/Makefile 229989 2012-01-11 23:06:37Z jkim $
 
 .if !defined(FORCE_BUILD)
 .error "The ACPI module is deprecated, set FORCE_BUILD to force it"
@@ -55,9 +55,10 @@
 SRCS+=	rsaddr.c rscalc.c rscreate.c rsdump.c rsinfo.c rsio.c rsirq.c rslist.c
 SRCS+=	rsmemory.c rsmisc.c rsserial.c rsutils.c rsxface.c
 SRCS+=	tbfadt.c tbfind.c tbinstal.c tbutils.c tbxface.c tbxfroot.c
-SRCS+=	utalloc.c utcache.c utcopy.c utdebug.c utdecode.c utdelete.c uteval.c
-SRCS+=	utglobal.c utids.c utinit.c utlock.c utmath.c utmisc.c utmutex.c
-SRCS+=	utobject.c utosi.c utresrc.c utstate.c utxface.c utxferror.c
+SRCS+=	utaddress.c utalloc.c utcache.c utcopy.c utdebug.c utdecode.c
+SRCS+=	utdelete.c uteval.c utglobal.c utids.c utinit.c utlock.c utmath.c
+SRCS+=	utmisc.c utmutex.c utobject.c utosi.c utresrc.c utstate.c utxface.c
+SRCS+=	utxferror.c
 #SRCS+=	utxfmutex.c
 
 # OSPM layer and core hardware drivers
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/modules/random/Makefile
--- a/head/sys/modules/random/Makefile	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/modules/random/Makefile	Fri Jan 13 15:51:01 2012 +0200
@@ -1,4 +1,4 @@
-# $FreeBSD$
+# $FreeBSD: head/sys/modules/random/Makefile 229887 2012-01-09 23:20:30Z jkim $
 
 .PATH: ${.CURDIR}/../../dev/random
 .PATH: ${.CURDIR}/../../crypto/rijndael
@@ -6,7 +6,7 @@
 
 KMOD=	random
 SRCS=	randomdev.c probe.c
-.if ${MACHINE} == "i386"
+.if ${MACHINE} == "amd64" || ${MACHINE} == "i386"
 SRCS+=	nehemiah.c
 .endif
 SRCS+=	randomdev_soft.c yarrow.c hash.c
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/modules/sound/driver/emu10kx/Makefile
--- a/head/sys/modules/sound/driver/emu10kx/Makefile	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/modules/sound/driver/emu10kx/Makefile	Fri Jan 13 15:51:01 2012 +0200
@@ -1,4 +1,4 @@
-# $FreeBSD$
+# $FreeBSD: head/sys/modules/sound/driver/emu10kx/Makefile 229981 2012-01-11 21:17:14Z pfg $
 .PATH: ${.CURDIR}/../../../../dev/sound/pci \
        ${.CURDIR}/../../../../gnu/dev/sound/pci
 
@@ -11,29 +11,6 @@
 SRCS+=	emu10kx.c
 SRCS+=	emu10kx-pcm.c
 SRCS+=	emu10kx-midi.c
-# de-GPLed Makefiles
-SRCS+=	emu10k1-alsa%diked.h
-SRCS+=	p16v-alsa%diked.h
-SRCS+=	p17v-alsa%diked.h
-
-emu10k1-alsa%diked.h: emu10k1-alsa.h
-	grep -v '#include' ${.OODATE} | $(CC) -E -D__KERNEL__ -dM - \
-	    | awk -F"[ 	(]" '/define/ \
-	    { print "#ifndef " $$2 ; print ; print "#endif" }' \
-	    >${.TARGET}
-p16v-alsa%diked.h: p16v-alsa.h
-	grep -v '#include' ${.OODATE} | $(CC) -E -D__KERNEL__ -dM - \
-	    | awk -F"[ 	(]" '/define/ \
-	    { print "#ifndef " $$2 ; print ; print "#endif" }' \
-	    >${.TARGET}
-p17v-alsa%diked.h: p17v-alsa.h
-	grep -v '#include' ${.OODATE} | $(CC) -E -D__KERNEL__ -dM - \
-	    | awk -F"[ 	(]" '/define/ \
-	    { print "#ifndef " $$2 ; print ; print "#endif" }' \
-	    >${.TARGET}
-
-CLEANFILES+=	emu10k1-alsa%diked.h
-CLEANFILES+=	p16v-alsa%diked.h
-CLEANFILES+=	p17v-alsa%diked.h
+SRCS+=	emuxkireg.h
 
 .include <bsd.kmod.mk>
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/modules/wtap/Makefile
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/sys/modules/wtap/Makefile	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,30 @@
+# $FreeBSD: head/sys/modules/wtap/Makefile 229970 2012-01-11 17:41:14Z adrian $
+
+.PATH:	${.CURDIR}/../../dev/wtap
+
+# Declare Name of kernel module
+KMOD	=  wtap
+
+# Enumerate Source files for kernel module
+SRCS	=  if_wtap_module.c if_wtap.c if_medium.c
+
+.PATH:	${.CURDIR}/../../dev/wtap/wtap_hal
+SRCS	+= hal.c
+
+.PATH:	${.CURDIR}/../../dev/wtap/plugins
+SRCS	+= visibility.c
+
+SRCS	+= opt_global.h
+
+.if defined(KERNBUILDDIR)
+MKDEP=          -include ${KERNBUILDDIR}/opt_global.h
+.else
+CFLAGS+=        -include opt_global.h
+MKDEP=          -include opt_global.h
+
+opt_global.h:
+	echo "#define VIMAGE 1" > ${.TARGET}
+.endif
+
+# Include kernel module makefile
+.include <bsd.kmod.mk>
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/net/bpf.c
--- a/head/sys/net/bpf.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/net/bpf.c	Fri Jan 13 15:51:01 2012 +0200
@@ -35,7 +35,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/net/bpf.c 229073 2011-12-31 07:21:28Z lstewart $");
+__FBSDID("$FreeBSD: head/sys/net/bpf.c 229898 2012-01-10 00:48:29Z lstewart $");
 
 #include "opt_bpf.h"
 #include "opt_compat.h"
@@ -2253,33 +2253,42 @@
 {
 	struct bpf_if	*bp;
 	struct bpf_d	*d;
+#ifdef INVARIANTS
+	int ndetached;
 
-	/* Locate BPF interface information */
-	mtx_lock(&bpf_mtx);
-	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
-		if (ifp == bp->bif_ifp)
-			break;
-	}
+	ndetached = 0;
+#endif
 
-	/* Interface wasn't attached */
-	if ((bp == NULL) || (bp->bif_ifp == NULL)) {
+	/* Find all bpf_if struct's which reference ifp and detach them. */
+	do {
+		mtx_lock(&bpf_mtx);
+		LIST_FOREACH(bp, &bpf_iflist, bif_next) {
+			if (ifp == bp->bif_ifp)
+				break;
+		}
+		if (bp != NULL)
+			LIST_REMOVE(bp, bif_next);
 		mtx_unlock(&bpf_mtx);
+
+		if (bp != NULL) {
+#ifdef INVARIANTS
+			ndetached++;
+#endif
+			while ((d = LIST_FIRST(&bp->bif_dlist)) != NULL) {
+				bpf_detachd(d);
+				BPFD_LOCK(d);
+				bpf_wakeup(d);
+				BPFD_UNLOCK(d);
+			}
+			mtx_destroy(&bp->bif_mtx);
+			free(bp, M_BPF);
+		}
+	} while (bp != NULL);
+
+#ifdef INVARIANTS
+	if (ndetached == 0)
 		printf("bpfdetach: %s was not attached\n", ifp->if_xname);
-		return;
-	}
-
-	LIST_REMOVE(bp, bif_next);
-	mtx_unlock(&bpf_mtx);
-
-	while ((d = LIST_FIRST(&bp->bif_dlist)) != NULL) {
-		bpf_detachd(d);
-		BPFD_LOCK(d);
-		bpf_wakeup(d);
-		BPFD_UNLOCK(d);
-	}
-
-	mtx_destroy(&bp->bif_mtx);
-	free(bp, M_BPF);
+#endif
 }
 
 /*
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/net/if.c
--- a/head/sys/net/if.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/net/if.c	Fri Jan 13 15:51:01 2012 +0200
@@ -27,7 +27,7 @@
  * SUCH DAMAGE.
  *
  *	@(#)if.c	8.5 (Berkeley) 1/9/95
- * $FreeBSD: head/sys/net/if.c 228736 2011-12-20 13:53:31Z glebius $
+ * $FreeBSD: head/sys/net/if.c 229621 2012-01-05 19:00:36Z jhb $
  */
 
 #include "opt_compat.h"
@@ -786,10 +786,10 @@
 	struct ifmultiaddr *ifma;
 	struct ifmultiaddr *next;
 
-	IF_ADDR_LOCK(ifp);
+	IF_ADDR_WLOCK(ifp);
 	TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
 		if_delmulti_locked(ifp, ifma, 1);
-	IF_ADDR_UNLOCK(ifp);
+	IF_ADDR_WUNLOCK(ifp);
 }
 
 /*
@@ -1133,10 +1133,10 @@
 	ifgl->ifgl_group = ifg;
 	ifgm->ifgm_ifp = ifp;
 
-	IF_ADDR_LOCK(ifp);
+	IF_ADDR_WLOCK(ifp);
 	TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
 	TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
-	IF_ADDR_UNLOCK(ifp);
+	IF_ADDR_WUNLOCK(ifp);
 
 	IFNET_WUNLOCK();
 
@@ -1163,9 +1163,9 @@
 		return (ENOENT);
 	}
 
-	IF_ADDR_LOCK(ifp);
+	IF_ADDR_WLOCK(ifp);
 	TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
-	IF_ADDR_UNLOCK(ifp);
+	IF_ADDR_WUNLOCK(ifp);
 
 	TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
 		if (ifgm->ifgm_ifp == ifp)
@@ -1206,9 +1206,9 @@
 
 		strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
 
-		IF_ADDR_LOCK(ifp);
+		IF_ADDR_WLOCK(ifp);
 		TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
-		IF_ADDR_UNLOCK(ifp);
+		IF_ADDR_WUNLOCK(ifp);
 
 		TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
 			if (ifgm->ifgm_ifp == ifp)
@@ -1250,33 +1250,33 @@
 	struct ifgroupreq	*ifgr = data;
 
 	if (ifgr->ifgr_len == 0) {
-		IF_ADDR_LOCK(ifp);
+		IF_ADDR_RLOCK(ifp);
 		TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
 			ifgr->ifgr_len += sizeof(struct ifg_req);
-		IF_ADDR_UNLOCK(ifp);
+		IF_ADDR_RUNLOCK(ifp);
 		return (0);
 	}
 
 	len = ifgr->ifgr_len;
 	ifgp = ifgr->ifgr_groups;
 	/* XXX: wire */
-	IF_ADDR_LOCK(ifp);
+	IF_ADDR_RLOCK(ifp);
 	TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
 		if (len < sizeof(ifgrq)) {
-			IF_ADDR_UNLOCK(ifp);
+			IF_ADDR_RUNLOCK(ifp);
 			return (EINVAL);
 		}
 		bzero(&ifgrq, sizeof ifgrq);
 		strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
 		    sizeof(ifgrq.ifgrq_group));
 		if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
-		    	IF_ADDR_UNLOCK(ifp);
+		    	IF_ADDR_RUNLOCK(ifp);
 			return (error);
 		}
 		len -= sizeof(ifgrq);
 		ifgp++;
 	}
-	IF_ADDR_UNLOCK(ifp);
+	IF_ADDR_RUNLOCK(ifp);
 
 	return (0);
 }
@@ -1383,28 +1383,28 @@
 if_addr_rlock(struct ifnet *ifp)
 {
 
-	IF_ADDR_LOCK(ifp);
+	IF_ADDR_RLOCK(ifp);
 }
 
 void
 if_addr_runlock(struct ifnet *ifp)
 {
 
-	IF_ADDR_UNLOCK(ifp);
+	IF_ADDR_RUNLOCK(ifp);
 }
 
 void
 if_maddr_rlock(struct ifnet *ifp)
 {
 
-	IF_ADDR_LOCK(ifp);
+	IF_ADDR_RLOCK(ifp);
 }
 
 void
 if_maddr_runlock(struct ifnet *ifp)
 {
 
-	IF_ADDR_UNLOCK(ifp);
+	IF_ADDR_RUNLOCK(ifp);
 }
 
 /*
@@ -1516,14 +1516,14 @@
 
 	IFNET_RLOCK_NOSLEEP();
 	TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
-		IF_ADDR_LOCK(ifp);
+		IF_ADDR_RLOCK(ifp);
 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 			if (ifa->ifa_addr->sa_family != addr->sa_family)
 				continue;
 			if (sa_equal(addr, ifa->ifa_addr)) {
 				if (getref)
 					ifa_ref(ifa);
-				IF_ADDR_UNLOCK(ifp);
+				IF_ADDR_RUNLOCK(ifp);
 				goto done;
 			}
 			/* IP6 doesn't have broadcast */
@@ -1533,11 +1533,11 @@
 			    sa_equal(ifa->ifa_broadaddr, addr)) {
 				if (getref)
 					ifa_ref(ifa);
-				IF_ADDR_UNLOCK(ifp);
+				IF_ADDR_RUNLOCK(ifp);
 				goto done;
 			}
 		}
-		IF_ADDR_UNLOCK(ifp);
+		IF_ADDR_RUNLOCK(ifp);
 	}
 	ifa = NULL;
 done:
@@ -1571,7 +1571,7 @@
 
 	IFNET_RLOCK_NOSLEEP();
 	TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
-		IF_ADDR_LOCK(ifp);
+		IF_ADDR_RLOCK(ifp);
 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 			if (ifa->ifa_addr->sa_family != addr->sa_family)
 				continue;
@@ -1580,11 +1580,11 @@
 			    ifa->ifa_broadaddr->sa_len != 0 &&
 			    sa_equal(ifa->ifa_broadaddr, addr)) {
 				ifa_ref(ifa);
-				IF_ADDR_UNLOCK(ifp);
+				IF_ADDR_RUNLOCK(ifp);
 				goto done;
 			}
 		}
-		IF_ADDR_UNLOCK(ifp);
+		IF_ADDR_RUNLOCK(ifp);
 	}
 	ifa = NULL;
 done:
@@ -1606,18 +1606,18 @@
 	TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
 			continue;
-		IF_ADDR_LOCK(ifp);
+		IF_ADDR_RLOCK(ifp);
 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 			if (ifa->ifa_addr->sa_family != addr->sa_family)
 				continue;
 			if (ifa->ifa_dstaddr != NULL &&
 			    sa_equal(addr, ifa->ifa_dstaddr)) {
 				ifa_ref(ifa);
-				IF_ADDR_UNLOCK(ifp);
+				IF_ADDR_RUNLOCK(ifp);
 				goto done;
 			}
 		}
-		IF_ADDR_UNLOCK(ifp);
+		IF_ADDR_RUNLOCK(ifp);
 	}
 	ifa = NULL;
 done:
@@ -1651,12 +1651,12 @@
 	/*
 	 * Scan though each interface, looking for ones that have addresses
 	 * in this address family.  Maintain a reference on ifa_maybe once
-	 * we find one, as we release the IF_ADDR_LOCK() that kept it stable
+	 * we find one, as we release the IF_ADDR_RLOCK() that kept it stable
 	 * when we move onto the next interface.
 	 */
 	IFNET_RLOCK_NOSLEEP();
 	TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
-		IF_ADDR_LOCK(ifp);
+		IF_ADDR_RLOCK(ifp);
 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 			char *cp, *cp2, *cp3;
 
@@ -1675,7 +1675,7 @@
 				if (ifa->ifa_dstaddr != NULL &&
 				    sa_equal(addr, ifa->ifa_dstaddr)) {
 					ifa_ref(ifa);
-					IF_ADDR_UNLOCK(ifp);
+					IF_ADDR_RUNLOCK(ifp);
 					goto done;
 				}
 			} else {
@@ -1686,7 +1686,7 @@
 				if (ifa->ifa_claim_addr) {
 					if ((*ifa->ifa_claim_addr)(ifa, addr)) {
 						ifa_ref(ifa);
-						IF_ADDR_UNLOCK(ifp);
+						IF_ADDR_RUNLOCK(ifp);
 						goto done;
 					}
 					continue;
@@ -1726,7 +1726,7 @@
 				}
 			}
 		}
-		IF_ADDR_UNLOCK(ifp);
+		IF_ADDR_RUNLOCK(ifp);
 	}
 	ifa = ifa_maybe;
 	ifa_maybe = NULL;
@@ -1752,7 +1752,7 @@
 
 	if (af >= AF_MAX)
 		return (NULL);
-	IF_ADDR_LOCK(ifp);
+	IF_ADDR_RLOCK(ifp);
 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 		if (ifa->ifa_addr->sa_family != af)
 			continue;
@@ -1784,7 +1784,7 @@
 done:
 	if (ifa != NULL)
 		ifa_ref(ifa);
-	IF_ADDR_UNLOCK(ifp);
+	IF_ADDR_RUNLOCK(ifp);
 	return (ifa);
 }
 
@@ -2334,9 +2334,9 @@
 			 * lose a race while we check if the membership
 			 * already exists.
 			 */
-			IF_ADDR_LOCK(ifp);
+			IF_ADDR_RLOCK(ifp);
 			ifma = if_findmulti(ifp, &ifr->ifr_addr);
-			IF_ADDR_UNLOCK(ifp);
+			IF_ADDR_RUNLOCK(ifp);
 			if (ifma != NULL)
 				error = EADDRINUSE;
 			else
@@ -2762,7 +2762,7 @@
 		}
 
 		addrs = 0;
-		IF_ADDR_LOCK(ifp);
+		IF_ADDR_RLOCK(ifp);
 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 			struct sockaddr *sa = ifa->ifa_addr;
 
@@ -2794,7 +2794,7 @@
 			if (sbuf_error(sb) == 0)
 				valid_len = sbuf_len(sb);
 		}
-		IF_ADDR_UNLOCK(ifp);
+		IF_ADDR_RUNLOCK(ifp);
 		if (addrs == 0) {
 			bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
 			sbuf_bcat(sb, &ifr, sizeof(ifr));
@@ -2952,13 +2952,13 @@
 	 * If the address is already present, return a new reference to it;
 	 * otherwise, allocate storage and set up a new address.
 	 */
-	IF_ADDR_LOCK(ifp);
+	IF_ADDR_WLOCK(ifp);
 	ifma = if_findmulti(ifp, sa);
 	if (ifma != NULL) {
 		ifma->ifma_refcount++;
 		if (retifma != NULL)
 			*retifma = ifma;
-		IF_ADDR_UNLOCK(ifp);
+		IF_ADDR_WUNLOCK(ifp);
 		return (0);
 	}
 
@@ -3024,7 +3024,7 @@
 	 * pointer is still valid.
 	 */
 	rt_newmaddrmsg(RTM_NEWMADDR, ifma);
-	IF_ADDR_UNLOCK(ifp);
+	IF_ADDR_WUNLOCK(ifp);
 
 	/*
 	 * We are certain we have added something, so call down to the
@@ -3044,7 +3044,7 @@
 		free(llsa, M_IFMADDR);
 
 unlock_out:
-	IF_ADDR_UNLOCK(ifp);
+	IF_ADDR_WUNLOCK(ifp);
 	return (error);
 }
 
@@ -3078,12 +3078,12 @@
 	if (ifp == NULL)
 		return (ENOENT);
 
-	IF_ADDR_LOCK(ifp);
+	IF_ADDR_WLOCK(ifp);
 	lastref = 0;
 	ifma = if_findmulti(ifp, sa);
 	if (ifma != NULL)
 		lastref = if_delmulti_locked(ifp, ifma, 0);
-	IF_ADDR_UNLOCK(ifp);
+	IF_ADDR_WUNLOCK(ifp);
 
 	if (ifma == NULL)
 		return (ENOENT);
@@ -3105,10 +3105,10 @@
 	struct ifmultiaddr *ifma;
 	struct ifmultiaddr *next;
 
-	IF_ADDR_LOCK(ifp);
+	IF_ADDR_WLOCK(ifp);
 	TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
 		if_delmulti_locked(ifp, ifma, 0);
-	IF_ADDR_UNLOCK(ifp);
+	IF_ADDR_WUNLOCK(ifp);
 }
 
 /*
@@ -3145,7 +3145,7 @@
 	 * If and only if the ifnet instance exists: Acquire the address lock.
 	 */
 	if (ifp != NULL)
-		IF_ADDR_LOCK(ifp);
+		IF_ADDR_WLOCK(ifp);
 
 	lastref = if_delmulti_locked(ifp, ifma, 0);
 
@@ -3155,7 +3155,7 @@
 		 *  Release the address lock.
 		 *  If the group was left: update the hardware hash filter.
 		 */
-		IF_ADDR_UNLOCK(ifp);
+		IF_ADDR_WUNLOCK(ifp);
 		if (lastref && ifp->if_ioctl != NULL) {
 			(void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
 		}
@@ -3177,7 +3177,7 @@
 	if (ifp != NULL && ifma->ifma_ifp != NULL) {
 		KASSERT(ifma->ifma_ifp == ifp,
 		    ("%s: inconsistent ifp %p", __func__, ifp));
-		IF_ADDR_LOCK_ASSERT(ifp);
+		IF_ADDR_WLOCK_ASSERT(ifp);
 	}
 
 	ifp = ifma->ifma_ifp;
@@ -3250,14 +3250,14 @@
 	struct ifaddr *ifa;
 	struct ifreq ifr;
 
-	IF_ADDR_LOCK(ifp);
+	IF_ADDR_RLOCK(ifp);
 	ifa = ifp->if_addr;
 	if (ifa == NULL) {
-		IF_ADDR_UNLOCK(ifp);
+		IF_ADDR_RUNLOCK(ifp);
 		return (EINVAL);
 	}
 	ifa_ref(ifa);
-	IF_ADDR_UNLOCK(ifp);
+	IF_ADDR_RUNLOCK(ifp);
 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
 	if (sdl == NULL) {
 		ifa_free(ifa);
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/net/if_llatbl.c
--- a/head/sys/net/if_llatbl.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/net/if_llatbl.c	Fri Jan 13 15:51:01 2012 +0200
@@ -25,7 +25,7 @@
  * SUCH DAMAGE.
  */
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/net/if_llatbl.c 222143 2011-05-20 19:12:20Z qingli $");
+__FBSDID("$FreeBSD: head/sys/net/if_llatbl.c 229810 2012-01-08 13:34:00Z glebius $");
 
 #include "opt_ddb.h"
 #include "opt_inet.h"
@@ -65,9 +65,6 @@
 static VNET_DEFINE(SLIST_HEAD(, lltable), lltables);
 #define	V_lltables	VNET(lltables)
 
-extern void arprequest(struct ifnet *, struct in_addr *, struct in_addr *,
-	u_char *);
-
 static void vnet_lltable_init(void);
 
 struct rwlock lltable_rwlock;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/net/if_media.h
--- a/head/sys/net/if_media.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/net/if_media.h	Fri Jan 13 15:51:01 2012 +0200
@@ -1,5 +1,5 @@
 /*	$NetBSD: if_media.h,v 1.3 1997/03/26 01:19:27 thorpej Exp $	*/
-/* $FreeBSD: head/sys/net/if_media.h 221955 2011-05-15 12:58:29Z marius $ */
+/* $FreeBSD: head/sys/net/if_media.h 229698 2012-01-06 12:05:43Z glebius $ */
 
 /*-
  * Copyright (c) 1997
@@ -247,11 +247,6 @@
 #define	IFM_ATM_UNASSIGNED	0x00000400	/* unassigned cells */
 
 /*
- * CARP Common Address Redundancy Protocol
- */
-#define	IFM_CARP	0x000000c0
-
-/*
  * Shared media sub-types
  */
 #define	IFM_AUTO	0		/* Autoselect best media */
@@ -337,7 +332,6 @@
 	{ IFM_FDDI,		"FDDI" },				\
 	{ IFM_IEEE80211,	"IEEE 802.11 Wireless Ethernet" },	\
 	{ IFM_ATM,		"ATM" },				\
-	{ IFM_CARP,		"Common Address Redundancy Protocol" }, \
 	{ 0, NULL },							\
 }
 
@@ -718,8 +712,6 @@
 	    { "no network", "active" } },				\
 	{ IFM_ATM,		IFM_AVALID,	IFM_ACTIVE,		\
 	    { "no network", "active" } },				\
-	{ IFM_CARP,		IFM_AVALID,	IFM_ACTIVE,		\
-	    { "backup", "master" } },					\
 	{ 0,			0,		0,			\
 	    { NULL, NULL } }						\
 }
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/net/if_var.h
--- a/head/sys/net/if_var.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/net/if_var.h	Fri Jan 13 15:51:01 2012 +0200
@@ -27,7 +27,7 @@
  * SUCH DAMAGE.
  *
  *	From: @(#)if.h	8.1 (Berkeley) 6/10/93
- * $FreeBSD: head/sys/net/if_var.h 228571 2011-12-16 12:16:56Z glebius $
+ * $FreeBSD: head/sys/net/if_var.h 229873 2012-01-09 19:34:12Z jhb $
  */
 
 #ifndef	_NET_IF_VAR_H_
@@ -189,11 +189,11 @@
 	int	if_afdata_initialized;
 	struct	rwlock if_afdata_lock;
 	struct	task if_linktask;	/* task for link change events */
-	struct	mtx if_addr_mtx;	/* mutex to protect address lists */
+	struct	rwlock if_addr_lock;	/* lock to protect address lists */
 
 	LIST_ENTRY(ifnet) if_clones;	/* interfaces of a cloner */
 	TAILQ_HEAD(, ifg_list) if_groups; /* linked list of groups per if */
-					/* protected by if_addr_mtx */
+					/* protected by if_addr_lock */
 	void	*if_pf_kif;
 	void	*if_lagg;		/* lagg glue */
 	char	*if_description;	/* interface description */
@@ -246,12 +246,17 @@
 /*
  * Locks for address lists on the network interface.
  */
-#define	IF_ADDR_LOCK_INIT(if)	mtx_init(&(if)->if_addr_mtx,		\
-				    "if_addr_mtx", NULL, MTX_DEF)
-#define	IF_ADDR_LOCK_DESTROY(if)	mtx_destroy(&(if)->if_addr_mtx)
-#define	IF_ADDR_LOCK(if)	mtx_lock(&(if)->if_addr_mtx)
-#define	IF_ADDR_UNLOCK(if)	mtx_unlock(&(if)->if_addr_mtx)
-#define	IF_ADDR_LOCK_ASSERT(if)	mtx_assert(&(if)->if_addr_mtx, MA_OWNED)
+#define	IF_ADDR_LOCK_INIT(if)	rw_init(&(if)->if_addr_lock, "if_addr_lock")
+#define	IF_ADDR_LOCK_DESTROY(if)	rw_destroy(&(if)->if_addr_lock)
+#define	IF_ADDR_WLOCK(if)	rw_wlock(&(if)->if_addr_lock)
+#define	IF_ADDR_WUNLOCK(if)	rw_wunlock(&(if)->if_addr_lock)
+#define	IF_ADDR_RLOCK(if)	rw_rlock(&(if)->if_addr_lock)
+#define	IF_ADDR_RUNLOCK(if)	rw_runlock(&(if)->if_addr_lock)
+#define	IF_ADDR_LOCK_ASSERT(if)	rw_assert(&(if)->if_addr_lock, RA_LOCKED)
+#define	IF_ADDR_WLOCK_ASSERT(if) rw_assert(&(if)->if_addr_lock, RA_WLOCKED)
+/* XXX: Compat. */
+#define	IF_ADDR_LOCK(if)	IF_ADDR_WLOCK(if)
+#define	IF_ADDR_UNLOCK(if)	IF_ADDR_WUNLOCK(if)
 
 /*
  * Function variations on locking macros intended to be used by loadable
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/net/if_vlan.c
--- a/head/sys/net/if_vlan.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/net/if_vlan.c	Fri Jan 13 15:51:01 2012 +0200
@@ -39,7 +39,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/net/if_vlan.c 228967 2011-12-29 18:40:58Z yongari $");
+__FBSDID("$FreeBSD: head/sys/net/if_vlan.c 229587 2012-01-05 11:42:34Z rwatson $");
 
 #include "opt_vlan.h"
 
@@ -1597,6 +1597,13 @@
 
 	case SIOCSETVLAN:
 #ifdef VIMAGE
+		/*
+		 * XXXRW/XXXBZ: The goal in these checks is to allow a VLAN
+		 * interface to be delegated to a jail without allowing the
+		 * jail to change what underlying interface/VID it is
+		 * associated with.  We are not entirely convinced that this
+		 * is the right way to accomplish that policy goal.
+		 */
 		if (ifp->if_vnet != ifp->if_home_vnet) {
 			error = EPERM;
 			break;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/net/rtsock.c
--- a/head/sys/net/rtsock.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/net/rtsock.c	Fri Jan 13 15:51:01 2012 +0200
@@ -27,7 +27,7 @@
  * SUCH DAMAGE.
  *
  *	@(#)rtsock.c	8.7 (Berkeley) 10/12/95
- * $FreeBSD: head/sys/net/rtsock.c 228571 2011-12-16 12:16:56Z glebius $
+ * $FreeBSD: head/sys/net/rtsock.c 229814 2012-01-08 17:11:53Z glebius $
  */
 #include "opt_compat.h"
 #include "opt_sctp.h"
@@ -456,7 +456,7 @@
 		 * Try to find an address on the given outgoing interface
 		 * that belongs to the jail.
 		 */
-		IF_ADDR_LOCK(ifp);
+		IF_ADDR_RLOCK(ifp);
 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 			struct sockaddr *sa;
 			sa = ifa->ifa_addr;
@@ -468,7 +468,7 @@
 				break;
 			}
 		}
-		IF_ADDR_UNLOCK(ifp);
+		IF_ADDR_RUNLOCK(ifp);
 		if (!found) {
 			/*
 			 * As a last resort return the 'default' jail address.
@@ -498,7 +498,7 @@
 		 * Try to find an address on the given outgoing interface
 		 * that belongs to the jail.
 		 */
-		IF_ADDR_LOCK(ifp);
+		IF_ADDR_RLOCK(ifp);
 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 			struct sockaddr *sa;
 			sa = ifa->ifa_addr;
@@ -511,7 +511,7 @@
 				break;
 			}
 		}
-		IF_ADDR_UNLOCK(ifp);
+		IF_ADDR_RUNLOCK(ifp);
 		if (!found) {
 			/*
 			 * As a last resort return the 'default' jail address.
@@ -1547,7 +1547,7 @@
 	TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
 		if (w->w_arg && w->w_arg != ifp->if_index)
 			continue;
-		IF_ADDR_LOCK(ifp);
+		IF_ADDR_RLOCK(ifp);
 		ifa = ifp->if_addr;
 		info.rti_info[RTAX_IFP] = ifa->ifa_addr;
 		len = rt_msg2(RTM_IFINFO, &info, NULL, w);
@@ -1606,6 +1606,7 @@
 				ifam->ifam_flags = ifa->ifa_flags;
 				ifam->ifam_metric = ifa->ifa_metric;
 				ifam->ifam_addrs = info.rti_addrs;
+				ifam->ifam_data = ifa->if_data;
 				if (carp_get_vhid_p != NULL)
 					ifam->ifam_data.ifi_vhid =
 					    (*carp_get_vhid_p)(ifa);
@@ -1614,13 +1615,13 @@
 					goto done;
 			}
 		}
-		IF_ADDR_UNLOCK(ifp);
+		IF_ADDR_RUNLOCK(ifp);
 		info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
 			info.rti_info[RTAX_BRD] = NULL;
 	}
 done:
 	if (ifp != NULL)
-		IF_ADDR_UNLOCK(ifp);
+		IF_ADDR_RUNLOCK(ifp);
 	IFNET_RUNLOCK();
 	return (error);
 }
@@ -1641,7 +1642,7 @@
 			continue;
 		ifa = ifp->if_addr;
 		info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
-		IF_ADDR_LOCK(ifp);
+		IF_ADDR_RLOCK(ifp);
 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
 			if (af && af != ifma->ifma_addr->sa_family)
 				continue;
@@ -1662,12 +1663,12 @@
 				ifmam->ifmam_addrs = info.rti_addrs;
 				error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
 				if (error) {
-					IF_ADDR_UNLOCK(ifp);
+					IF_ADDR_RUNLOCK(ifp);
 					goto done;
 				}
 			}
 		}
-		IF_ADDR_UNLOCK(ifp);
+		IF_ADDR_RUNLOCK(ifp);
 	}
 done:
 	IFNET_RUNLOCK();
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/net80211/ieee80211_ioctl.c
--- a/head/sys/net80211/ieee80211_ioctl.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/net80211/ieee80211_ioctl.c	Fri Jan 13 15:51:01 2012 +0200
@@ -25,7 +25,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_ioctl.c 228621 2011-12-17 10:23:17Z bschmidt $");
+__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_ioctl.c 229968 2012-01-11 17:10:27Z adrian $");
 
 /*
  * IEEE 802.11 ioctl support (FreeBSD-specific)
@@ -1397,6 +1397,17 @@
 			    IEEE80211_FC0_SUBTYPE_DEAUTH, reason);
 			ieee80211_free_node(ni);
 			break;
+		case IEEE80211_M_MBSS:
+			IEEE80211_NODE_LOCK(nt);
+			ni = ieee80211_find_node_locked(nt, mac);
+			if (ni != NULL) {
+				ieee80211_node_leave(ni);
+				ieee80211_free_node(ni);
+			} else {
+				error = ENOENT;
+			}
+			IEEE80211_NODE_UNLOCK(nt);
+			break;
 		default:
 			error = EINVAL;
 			break;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/net80211/ieee80211_output.c
--- a/head/sys/net80211/ieee80211_output.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/net80211/ieee80211_output.c	Fri Jan 13 15:51:01 2012 +0200
@@ -25,7 +25,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_output.c 227331 2011-11-08 04:00:24Z adrian $");
+__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_output.c 229795 2012-01-07 23:41:38Z bz $");
 
 #include "opt_inet.h"
 #include "opt_inet6.h"
@@ -834,7 +834,7 @@
 		uint32_t flow;
 		uint8_t tos;
 		/*
-		 * IPv6 frame, map the DSCP bits from the TOS field.
+		 * IPv6 frame, map the DSCP bits from the traffic class field.
 		 */
 		m_copydata(m, sizeof(struct ether_header) +
 		    offsetof(struct ip6_hdr, ip6_flow), sizeof(flow),
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/net80211/ieee80211_radiotap.c
--- a/head/sys/net80211/ieee80211_radiotap.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/net80211/ieee80211_radiotap.c	Fri Jan 13 15:51:01 2012 +0200
@@ -24,7 +24,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_radiotap.c 193761 2009-06-08 21:16:06Z sam $");
+__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_radiotap.c 229952 2012-01-11 01:09:08Z adrian $");
 
 /*
  * IEEE 802.11 radiotap support.
@@ -325,6 +325,10 @@
 		    .align	= sizeof(uint32_t),
 		    .width	= 2*sizeof(uint32_t),
 		},
+		[IEEE80211_RADIOTAP_MCS] = {
+		    .align	= sizeof(uint8_t),
+		    .width	= 3*sizeof(uint8_t),
+		},
 	};
 	uint32_t present = le32toh(rh->it_present);
 	int off, i;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/net80211/ieee80211_radiotap.h
--- a/head/sys/net80211/ieee80211_radiotap.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/net80211/ieee80211_radiotap.h	Fri Jan 13 15:51:01 2012 +0200
@@ -1,4 +1,4 @@
-/* $FreeBSD: head/sys/net80211/ieee80211_radiotap.h 174568 2007-12-13 01:23:40Z sam $ */
+/* $FreeBSD: head/sys/net80211/ieee80211_radiotap.h 229952 2012-01-11 01:09:08Z adrian $ */
 /* $NetBSD: ieee80211_radiotap.h,v 1.16 2007/01/06 05:51:15 dyoung Exp $ */
 
 /*-
@@ -190,6 +190,8 @@
 	IEEE80211_RADIOTAP_DB_ANTNOISE = 13,
 	/* NB: gap for netbsd definitions */
 	IEEE80211_RADIOTAP_XCHANNEL = 18,
+	IEEE80211_RADIOTAP_MCS = 19,
+	IEEE80211_RADIOTAP_VENDOREXT = 30,
 	IEEE80211_RADIOTAP_EXT = 31,
 };
 
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/netatalk/aarp.c
--- a/head/sys/netatalk/aarp.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/netatalk/aarp.c	Fri Jan 13 15:51:01 2012 +0200
@@ -47,7 +47,7 @@
  *	+1-313-764-2278
  *	netatalk at umich.edu
  *
- * $FreeBSD$
+ * $FreeBSD: head/sys/netatalk/aarp.c 229621 2012-01-05 19:00:36Z jhb $
  */
 
 #include "opt_atalk.h"
@@ -406,7 +406,7 @@
 		 * Since we don't know the net, we just look for the first
 		 * phase 1 address on the interface.
 		 */
-		IF_ADDR_LOCK(ifp);
+		IF_ADDR_RLOCK(ifp);
 		for (aa = (struct at_ifaddr *)TAILQ_FIRST(&ifp->if_addrhead);
 		    aa;
 		    aa = (struct at_ifaddr *)aa->aa_ifa.ifa_link.tqe_next) {
@@ -416,12 +416,12 @@
 			}
 		}
 		if (aa == NULL) {
-			IF_ADDR_UNLOCK(ifp);
+			IF_ADDR_RUNLOCK(ifp);
 			m_freem(m);
 			return;
 		}
 		ifa_ref(&aa->aa_ifa);
-		IF_ADDR_UNLOCK(ifp);
+		IF_ADDR_RUNLOCK(ifp);
 		tpa.s_net = spa.s_net = AA_SAT(aa)->sat_addr.s_net;
 	}
 
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/netatalk/at_control.c
--- a/head/sys/netatalk/at_control.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/netatalk/at_control.c	Fri Jan 13 15:51:01 2012 +0200
@@ -26,7 +26,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
+__FBSDID("$FreeBSD: head/sys/netatalk/at_control.c 229621 2012-01-05 19:00:36Z jhb $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -254,9 +254,9 @@
 			 */
 			aa->aa_ifp = ifp;
 			ifa_ref(&aa->aa_ifa);		/* if_addrhead */
-			IF_ADDR_LOCK(ifp);
+			IF_ADDR_WLOCK(ifp);
 			TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
-			IF_ADDR_UNLOCK(ifp);
+			IF_ADDR_WUNLOCK(ifp);
 		} else {
 			/*
 			 * If we DID find one then we clobber any routes
@@ -357,9 +357,9 @@
 		 * remove the ifaddr from the interface
 		 */
 		ifa = (struct ifaddr *)aa;
-		IF_ADDR_LOCK(ifp);
+		IF_ADDR_WLOCK(ifp);
 		TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
-		IF_ADDR_UNLOCK(ifp);
+		IF_ADDR_WUNLOCK(ifp);
 		ifa_free(ifa);				/* if_addrhead */
 
 		/*
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/netinet/if_ether.c
--- a/head/sys/netinet/if_ether.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/netinet/if_ether.c	Fri Jan 13 15:51:01 2012 +0200
@@ -36,7 +36,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/netinet/if_ether.c 228959 2011-12-29 15:59:14Z glebius $");
+__FBSDID("$FreeBSD: head/sys/netinet/if_ether.c 229816 2012-01-08 17:25:15Z glebius $");
 
 #include "opt_inet.h"
 
@@ -123,8 +123,6 @@
 	"Number of packets to hold per ARP entry");
 
 static void	arp_init(void);
-void		arprequest(struct ifnet *,
-			struct in_addr *, struct in_addr *, u_char *);
 static void	arpintr(struct mbuf *);
 static void	arptimer(void *);
 #ifdef INET
@@ -213,29 +211,41 @@
 	struct mbuf *m;
 	struct arphdr *ah;
 	struct sockaddr sa;
+	u_char *carpaddr = NULL;
 
 	if (sip == NULL) {
-		/* XXX don't believe this can happen (or explain why) */
 		/*
 		 * The caller did not supply a source address, try to find
 		 * a compatible one among those assigned to this interface.
 		 */
 		struct ifaddr *ifa;
 
+		IF_ADDR_RLOCK(ifp);
 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
-			if (!ifa->ifa_addr ||
-			    ifa->ifa_addr->sa_family != AF_INET)
+			if (ifa->ifa_addr->sa_family != AF_INET)
 				continue;
-			sip = &SIN(ifa->ifa_addr)->sin_addr;
+
+			if (ifa->ifa_carp) {
+				if ((*carp_iamatch_p)(ifa, &carpaddr) == 0)
+					continue;
+				sip = &IA_SIN(ifa)->sin_addr;
+			} else {
+				carpaddr = NULL;
+				sip = &IA_SIN(ifa)->sin_addr;
+			}
+
 			if (0 == ((sip->s_addr ^ tip->s_addr) &
-			    SIN(ifa->ifa_netmask)->sin_addr.s_addr) )
+			    IA_MASKSIN(ifa)->sin_addr.s_addr))
 				break;  /* found it. */
 		}
+		IF_ADDR_RUNLOCK(ifp);
 		if (sip == NULL) {  
 			printf("%s: cannot find matching address\n", __func__);
 			return;
 		}
 	}
+	if (enaddr == NULL)
+		enaddr = carpaddr ? carpaddr : (u_char *)IF_LLADDR(ifp);
 
 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
 		return;
@@ -330,9 +340,7 @@
 		 */
 		if (!(la->la_flags & LLE_STATIC) &&
 		    time_uptime + la->la_preempt > la->la_expire) {
-			arprequest(ifp, NULL,
-			    &SIN(dst)->sin_addr, IF_LLADDR(ifp));
-
+			arprequest(ifp, NULL, &SIN(dst)->sin_addr, NULL);
 			la->la_preempt--;
 		}
 		
@@ -408,8 +416,7 @@
 			LLE_REMREF(la);
 		la->la_asked++;
 		LLE_WUNLOCK(la);
-		arprequest(ifp, NULL, &SIN(dst)->sin_addr,
-		    IF_LLADDR(ifp));
+		arprequest(ifp, NULL, &SIN(dst)->sin_addr, NULL);
 		return (error);
 	}
 done:
@@ -608,17 +615,17 @@
 	 * No match, use the first inet address on the receive interface
 	 * as a dummy address for the rest of the function.
 	 */
-	IF_ADDR_LOCK(ifp);
+	IF_ADDR_RLOCK(ifp);
 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
 		if (ifa->ifa_addr->sa_family == AF_INET &&
 		    (ifa->ifa_carp == NULL ||
 		    (*carp_iamatch_p)(ifa, &enaddr))) {
 			ia = ifatoia(ifa);
 			ifa_ref(ifa);
-			IF_ADDR_UNLOCK(ifp);
+			IF_ADDR_RUNLOCK(ifp);
 			goto match;
 		}
-	IF_ADDR_UNLOCK(ifp);
+	IF_ADDR_RUNLOCK(ifp);
 
 	/*
 	 * If bridging, fall back to using any inet address.
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/netinet/if_ether.h
--- a/head/sys/netinet/if_ether.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/netinet/if_ether.h	Fri Jan 13 15:51:01 2012 +0200
@@ -27,7 +27,7 @@
  * SUCH DAMAGE.
  *
  *	@(#)if_ether.h	8.3 (Berkeley) 5/2/95
- * $FreeBSD: head/sys/netinet/if_ether.h 228571 2011-12-16 12:16:56Z glebius $
+ * $FreeBSD: head/sys/netinet/if_ether.h 229810 2012-01-08 13:34:00Z glebius $
  */
 
 #ifndef _NETINET_IF_ETHER_H_
@@ -115,6 +115,8 @@
 int	arpresolve(struct ifnet *ifp, struct rtentry *rt,
 		    struct mbuf *m, struct sockaddr *dst, u_char *desten,
 		    struct llentry **lle);
+void	arprequest(struct ifnet *, struct in_addr *, struct in_addr *,
+		    u_char *);
 void	arp_ifinit(struct ifnet *, struct ifaddr *);
 void	arp_ifinit2(struct ifnet *, struct ifaddr *, u_char *);
 void	arp_ifscrub(struct ifnet *, uint32_t);
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/netinet/igmp.c
--- a/head/sys/netinet/igmp.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/netinet/igmp.c	Fri Jan 13 15:51:01 2012 +0200
@@ -48,7 +48,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/netinet/igmp.c 229420 2012-01-03 20:34:52Z jhb $");
+__FBSDID("$FreeBSD: head/sys/netinet/igmp.c 229621 2012-01-05 19:00:36Z jhb $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -618,7 +618,7 @@
 
 	igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
 	if (igi->igi_version == IGMP_VERSION_3) {
-		IF_ADDR_LOCK(ifp);
+		IF_ADDR_RLOCK(ifp);
 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
 			if (ifma->ifma_addr->sa_family != AF_INET ||
 			    ifma->ifma_protospec == NULL)
@@ -634,7 +634,7 @@
 			}
 			inm_clear_recorded(inm);
 		}
-		IF_ADDR_UNLOCK(ifp);
+		IF_ADDR_RUNLOCK(ifp);
 		/*
 		 * Free the in_multi reference(s) for this IGMP lifecycle.
 		 */
@@ -751,7 +751,7 @@
 	 * for the interface on which the query arrived,
 	 * except those which are already running.
 	 */
-	IF_ADDR_LOCK(ifp);
+	IF_ADDR_RLOCK(ifp);
 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
 		if (ifma->ifma_addr->sa_family != AF_INET ||
 		    ifma->ifma_protospec == NULL)
@@ -779,7 +779,7 @@
 			break;
 		}
 	}
-	IF_ADDR_UNLOCK(ifp);
+	IF_ADDR_RUNLOCK(ifp);
 
 out_locked:
 	IGMP_UNLOCK();
@@ -852,7 +852,7 @@
 		 */
 		CTR2(KTR_IGMPV3, "process v2 general query on ifp %p(%s)",
 		    ifp, ifp->if_xname);
-		IF_ADDR_LOCK(ifp);
+		IF_ADDR_RLOCK(ifp);
 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
 			if (ifma->ifma_addr->sa_family != AF_INET ||
 			    ifma->ifma_protospec == NULL)
@@ -860,7 +860,7 @@
 			inm = (struct in_multi *)ifma->ifma_protospec;
 			igmp_v2_update_group(inm, timer);
 		}
-		IF_ADDR_UNLOCK(ifp);
+		IF_ADDR_RUNLOCK(ifp);
 	} else {
 		/*
 		 * Group-specific IGMPv2 query, we need only
@@ -1708,7 +1708,7 @@
 			IFQ_SET_MAXLEN(&scq, IGMP_MAX_STATE_CHANGE_PACKETS);
 		}
 
-		IF_ADDR_LOCK(ifp);
+		IF_ADDR_RLOCK(ifp);
 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
 			if (ifma->ifma_addr->sa_family != AF_INET ||
 			    ifma->ifma_protospec == NULL)
@@ -1726,7 +1726,7 @@
 				break;
 			}
 		}
-		IF_ADDR_UNLOCK(ifp);
+		IF_ADDR_RUNLOCK(ifp);
 
 		if (igi->igi_version == IGMP_VERSION_3) {
 			struct in_multi		*tinm;
@@ -2023,7 +2023,7 @@
 	 * for all memberships scoped to this link.
 	 */
 	ifp = igi->igi_ifp;
-	IF_ADDR_LOCK(ifp);
+	IF_ADDR_RLOCK(ifp);
 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
 		if (ifma->ifma_addr->sa_family != AF_INET ||
 		    ifma->ifma_protospec == NULL)
@@ -2068,7 +2068,7 @@
 		inm->inm_timer = 0;
 		_IF_DRAIN(&inm->inm_scq);
 	}
-	IF_ADDR_UNLOCK(ifp);
+	IF_ADDR_RUNLOCK(ifp);
 	SLIST_FOREACH_SAFE(inm, &igi->igi_relinmhead, inm_nrele, tinm) {
 		SLIST_REMOVE_HEAD(&igi->igi_relinmhead, inm_nrele);
 		inm_release_locked(inm);
@@ -3331,7 +3331,7 @@
 
 	ifp = igi->igi_ifp;
 
-	IF_ADDR_LOCK(ifp);
+	IF_ADDR_RLOCK(ifp);
 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
 		if (ifma->ifma_addr->sa_family != AF_INET ||
 		    ifma->ifma_protospec == NULL)
@@ -3362,7 +3362,7 @@
 			break;
 		}
 	}
-	IF_ADDR_UNLOCK(ifp);
+	IF_ADDR_RUNLOCK(ifp);
 
 	loop = (igi->igi_flags & IGIF_LOOPBACK) ? 1 : 0;
 	igmp_dispatch_queue(&igi->igi_gq, IGMP_MAX_RESPONSE_BURST, loop);
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/netinet/in.c
--- a/head/sys/netinet/in.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/netinet/in.c	Fri Jan 13 15:51:01 2012 +0200
@@ -31,7 +31,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/netinet/in.c 229478 2012-01-04 13:29:26Z jhb $");
+__FBSDID("$FreeBSD: head/sys/netinet/in.c 229621 2012-01-05 19:00:36Z jhb $");
 
 #include "opt_mpath.h"
 
@@ -363,7 +363,7 @@
 		ifa_ref(&ia->ia_ifa);
 	IN_IFADDR_RUNLOCK();
 	if (ia == NULL) {
-		IF_ADDR_LOCK(ifp);
+		IF_ADDR_RLOCK(ifp);
 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 			iap = ifatoia(ifa);
 			if (iap->ia_addr.sin_family == AF_INET) {
@@ -377,7 +377,7 @@
 		}
 		if (ia != NULL)
 			ifa_ref(&ia->ia_ifa);
-		IF_ADDR_UNLOCK(ifp);
+		IF_ADDR_RUNLOCK(ifp);
 	}
 	if (ia == NULL)
 		iaIsFirst = 1;
@@ -441,9 +441,9 @@
 			ia->ia_ifp = ifp;
 
 			ifa_ref(ifa);			/* if_addrhead */
-			IF_ADDR_LOCK(ifp);
+			IF_ADDR_WLOCK(ifp);
 			TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
-			IF_ADDR_UNLOCK(ifp);
+			IF_ADDR_WUNLOCK(ifp);
 			ifa_ref(ifa);			/* in_ifaddrhead */
 			IN_IFADDR_WLOCK();
 			TAILQ_INSERT_TAIL(&V_in_ifaddrhead, ia, ia_link);
@@ -622,7 +622,7 @@
 	if (ia->ia_ifa.ifa_carp)
 		(*carp_detach_p)(&ia->ia_ifa);
 
-	IF_ADDR_LOCK(ifp);
+	IF_ADDR_WLOCK(ifp);
 	/* Re-check that ia is still part of the list. */
 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 		if (ifa == &ia->ia_ifa)
@@ -634,12 +634,12 @@
 		 * try it again for the next loop as there is no other exit
 		 * path between here and out.
 		 */
-		IF_ADDR_UNLOCK(ifp);
+		IF_ADDR_WUNLOCK(ifp);
 		error = EADDRNOTAVAIL;
 		goto out;
 	}
 	TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
-	IF_ADDR_UNLOCK(ifp);
+	IF_ADDR_WUNLOCK(ifp);
 	ifa_free(&ia->ia_ifa);				/* if_addrhead */
 
 	IN_IFADDR_WLOCK();
@@ -784,7 +784,7 @@
 			}
 		}
 
-		IF_ADDR_LOCK(ifp);
+		IF_ADDR_RLOCK(ifp);
 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)	{
 			if (ifa->ifa_addr->sa_family != AF_INET)
 				continue;
@@ -798,7 +798,7 @@
 		}
 		if (ifa != NULL)
 			ifa_ref(ifa);
-		IF_ADDR_UNLOCK(ifp);
+		IF_ADDR_RUNLOCK(ifp);
 		if (ifa == NULL)
 			return (EADDRNOTAVAIL);
 		ia = (struct in_ifaddr *)ifa;
@@ -1302,7 +1302,7 @@
 	 * We need to do this as IF_ADDR_LOCK() may be re-acquired
 	 * by code further down.
 	 */
-	IF_ADDR_LOCK(ifp);
+	IF_ADDR_RLOCK(ifp);
 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
 		if (ifma->ifma_addr->sa_family != AF_INET ||
 		    ifma->ifma_protospec == NULL)
@@ -1314,7 +1314,7 @@
 		inm = (struct in_multi *)ifma->ifma_protospec;
 		LIST_INSERT_HEAD(&purgeinms, inm, inm_link);
 	}
-	IF_ADDR_UNLOCK(ifp);
+	IF_ADDR_RUNLOCK(ifp);
 
 	LIST_FOREACH_SAFE(inm, &purgeinms, inm_link, tinm) {
 		LIST_REMOVE(inm, inm_link);
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/netinet/in_mcast.c
--- a/head/sys/netinet/in_mcast.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/netinet/in_mcast.c	Fri Jan 13 15:51:01 2012 +0200
@@ -33,7 +33,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/netinet/in_mcast.c 228969 2011-12-29 20:41:16Z jhb $");
+__FBSDID("$FreeBSD: head/sys/netinet/in_mcast.c 229621 2012-01-05 19:00:36Z jhb $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -429,7 +429,7 @@
 		return (error);
 
 	/* XXX ifma_protospec must be covered by IF_ADDR_LOCK */
-	IF_ADDR_LOCK(ifp);
+	IF_ADDR_WLOCK(ifp);
 
 	/*
 	 * If something other than netinet is occupying the link-layer
@@ -453,11 +453,11 @@
 #endif
 		++inm->inm_refcount;
 		*pinm = inm;
-		IF_ADDR_UNLOCK(ifp);
+		IF_ADDR_WUNLOCK(ifp);
 		return (0);
 	}
 
-	IF_ADDR_LOCK_ASSERT(ifp);
+	IF_ADDR_WLOCK_ASSERT(ifp);
 
 	/*
 	 * A new in_multi record is needed; allocate and initialize it.
@@ -469,7 +469,7 @@
 	inm = malloc(sizeof(*inm), M_IPMADDR, M_NOWAIT | M_ZERO);
 	if (inm == NULL) {
 		if_delmulti_ifma(ifma);
-		IF_ADDR_UNLOCK(ifp);
+		IF_ADDR_WUNLOCK(ifp);
 		return (ENOMEM);
 	}
 	inm->inm_addr = *group;
@@ -492,7 +492,7 @@
 
 	*pinm = inm;
 
-	IF_ADDR_UNLOCK(ifp);
+	IF_ADDR_WUNLOCK(ifp);
 	return (0);
 }
 
@@ -2832,7 +2832,7 @@
 
 	IN_MULTI_LOCK();
 
-	IF_ADDR_LOCK(ifp);
+	IF_ADDR_RLOCK(ifp);
 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
 		if (ifma->ifma_addr->sa_family != AF_INET ||
 		    ifma->ifma_protospec == NULL)
@@ -2865,7 +2865,7 @@
 				break;
 		}
 	}
-	IF_ADDR_UNLOCK(ifp);
+	IF_ADDR_RUNLOCK(ifp);
 
 	IN_MULTI_UNLOCK();
 
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/netinet/in_pcb.c
--- a/head/sys/netinet/in_pcb.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/netinet/in_pcb.c	Fri Jan 13 15:51:01 2012 +0200
@@ -36,7 +36,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/netinet/in_pcb.c 227309 2011-11-07 15:43:11Z ed $");
+__FBSDID("$FreeBSD: head/sys/netinet/in_pcb.c 229621 2012-01-05 19:00:36Z jhb $");
 
 #include "opt_ddb.h"
 #include "opt_ipsec.h"
@@ -745,7 +745,7 @@
 		ifp = ia->ia_ifp;
 		ifa_free(&ia->ia_ifa);
 		ia = NULL;
-		IF_ADDR_LOCK(ifp);
+		IF_ADDR_RLOCK(ifp);
 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 
 			sa = ifa->ifa_addr;
@@ -759,10 +759,10 @@
 		}
 		if (ia != NULL) {
 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
-			IF_ADDR_UNLOCK(ifp);
+			IF_ADDR_RUNLOCK(ifp);
 			goto done;
 		}
-		IF_ADDR_UNLOCK(ifp);
+		IF_ADDR_RUNLOCK(ifp);
 
 		/* 3. As a last resort return the 'default' jail address. */
 		error = prison_get_ip4(cred, laddr);
@@ -804,7 +804,7 @@
 		 */
 		ia = NULL;
 		ifp = sro.ro_rt->rt_ifp;
-		IF_ADDR_LOCK(ifp);
+		IF_ADDR_RLOCK(ifp);
 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 			sa = ifa->ifa_addr;
 			if (sa->sa_family != AF_INET)
@@ -817,10 +817,10 @@
 		}
 		if (ia != NULL) {
 			laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
-			IF_ADDR_UNLOCK(ifp);
+			IF_ADDR_RUNLOCK(ifp);
 			goto done;
 		}
-		IF_ADDR_UNLOCK(ifp);
+		IF_ADDR_RUNLOCK(ifp);
 
 		/* 3. As a last resort return the 'default' jail address. */
 		error = prison_get_ip4(cred, laddr);
@@ -868,7 +868,7 @@
 			ifp = ia->ia_ifp;
 			ifa_free(&ia->ia_ifa);
 			ia = NULL;
-			IF_ADDR_LOCK(ifp);
+			IF_ADDR_RLOCK(ifp);
 			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 
 				sa = ifa->ifa_addr;
@@ -883,10 +883,10 @@
 			}
 			if (ia != NULL) {
 				laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
-				IF_ADDR_UNLOCK(ifp);
+				IF_ADDR_RUNLOCK(ifp);
 				goto done;
 			}
-			IF_ADDR_UNLOCK(ifp);
+			IF_ADDR_RUNLOCK(ifp);
 		}
 
 		/* 3. As a last resort return the 'default' jail address. */
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/netinet/in_proto.c
--- a/head/sys/netinet/in_proto.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/netinet/in_proto.c	Fri Jan 13 15:51:01 2012 +0200
@@ -30,14 +30,13 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/netinet/in_proto.c 223963 2011-07-12 19:29:29Z tuexen $");
+__FBSDID("$FreeBSD: head/sys/netinet/in_proto.c 229850 2012-01-09 08:50:22Z glebius $");
 
 #include "opt_ipx.h"
 #include "opt_mrouting.h"
 #include "opt_ipsec.h"
 #include "opt_inet.h"
 #include "opt_inet6.h"
-#include "opt_pf.h"
 #include "opt_sctp.h"
 #include "opt_mpath.h"
 
@@ -101,11 +100,6 @@
 #include <netinet/sctp_var.h>
 #endif /* SCTP */
 
-#ifdef DEV_PFSYNC
-#include <net/pfvar.h>
-#include <net/if_pfsync.h>
-#endif
-
 FEATURE(inet, "Internet Protocol version 4");
 
 extern	struct domain inetdomain;
@@ -317,17 +311,6 @@
 	.pr_ctloutput =		rip_ctloutput,
 	.pr_usrreqs =		&rip_usrreqs
 },
-#ifdef DEV_PFSYNC
-{
-	.pr_type =		SOCK_RAW,
-	.pr_domain =		&inetdomain,
-	.pr_protocol =		IPPROTO_PFSYNC,
-	.pr_flags =		PR_ATOMIC|PR_ADDR,
-	.pr_input =		pfsync_input,
-	.pr_ctloutput =		rip_ctloutput,
-	.pr_usrreqs =		&rip_usrreqs
-},
-#endif	/* DEV_PFSYNC */
 /* Spacer n-times for loadable protocols. */
 IPPROTOSPACER,
 IPPROTOSPACER,
@@ -397,6 +380,3 @@
 SYSCTL_NODE(_net_inet, IPPROTO_IPIP,	ipip,	CTLFLAG_RW, 0,	"IPIP");
 #endif /* IPSEC */
 SYSCTL_NODE(_net_inet, IPPROTO_RAW,	raw,	CTLFLAG_RW, 0,	"RAW");
-#ifdef DEV_PFSYNC
-SYSCTL_NODE(_net_inet, IPPROTO_PFSYNC,	pfsync,	CTLFLAG_RW, 0,	"PFSYNC");
-#endif
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/netinet/in_var.h
--- a/head/sys/netinet/in_var.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/netinet/in_var.h	Fri Jan 13 15:51:01 2012 +0200
@@ -27,7 +27,7 @@
  * SUCH DAMAGE.
  *
  *	@(#)in_var.h	8.2 (Berkeley) 1/9/95
- * $FreeBSD: head/sys/netinet/in_var.h 228571 2011-12-16 12:16:56Z glebius $
+ * $FreeBSD: head/sys/netinet/in_var.h 229815 2012-01-08 17:20:29Z glebius $
  */
 
 #ifndef _NETINET_IN_VAR_H_
@@ -85,6 +85,7 @@
  */
 #define IA_SIN(ia)    (&(((struct in_ifaddr *)(ia))->ia_addr))
 #define IA_DSTSIN(ia) (&(((struct in_ifaddr *)(ia))->ia_dstaddr))
+#define IA_MASKSIN(ia) (&(((struct in_ifaddr *)(ia))->ia_sockmask))
 
 #define IN_LNAOF(in, ifa) \
 	((ntohl((in).s_addr) & ~((struct in_ifaddr *)(ifa)->ia_subnetmask))
@@ -394,9 +395,9 @@
 	struct in_multi *inm;
 
 	IN_MULTI_LOCK_ASSERT();
-	IF_ADDR_LOCK(ifp);
+	IF_ADDR_RLOCK(ifp);
 	inm = inm_lookup_locked(ifp, ina);
-	IF_ADDR_UNLOCK(ifp);
+	IF_ADDR_RUNLOCK(ifp);
 
 	return (inm);
 }
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/netinet/ip_carp.c
--- a/head/sys/netinet/ip_carp.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/netinet/ip_carp.c	Fri Jan 13 15:51:01 2012 +0200
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/netinet/ip_carp.c 228812 2011-12-22 18:48:21Z glebius $");
+__FBSDID("$FreeBSD: head/sys/netinet/ip_carp.c 229621 2012-01-05 19:00:36Z jhb $");
 
 #include "opt_bpf.h"
 #include "opt_inet.h"
@@ -549,14 +549,14 @@
 	struct timeval sc_tv, ch_tv;
 
 	/* verify that the VHID is valid on the receiving interface */
-	IF_ADDR_LOCK(ifp);
+	IF_ADDR_RLOCK(ifp);
 	IFNET_FOREACH_IFA(ifp, ifa)
 		if (ifa->ifa_addr->sa_family == af &&
 		    ifa->ifa_carp->sc_vhid == ch->carp_vhid) {
 			ifa_ref(ifa);
 			break;
 		}
-	IF_ADDR_UNLOCK(ifp);
+	IF_ADDR_RUNLOCK(ifp);
 
 	if (ifa == NULL) {
 		CARPSTATS_INC(carps_badvhid);
@@ -1003,16 +1003,16 @@
 {
 	struct ifaddr *ifa;
 
-	IF_ADDR_LOCK(ifp);
+	IF_ADDR_RLOCK(ifp);
 	IFNET_FOREACH_IFA(ifp, ifa)
 		if (ifa->ifa_addr->sa_family == AF_INET6 &&
 		    ifa->ifa_carp->sc_state == MASTER &&
 		    IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa))) {
 			ifa_ref(ifa);
-			IF_ADDR_UNLOCK(ifp);
+			IF_ADDR_RUNLOCK(ifp);
 			return (ifa);
 		}
-	IF_ADDR_UNLOCK(ifp);
+	IF_ADDR_RUNLOCK(ifp);
 
 	return (NULL);
 }
@@ -1022,14 +1022,14 @@
 {
 	struct ifaddr *ifa;
 
-	IF_ADDR_LOCK(ifp);
+	IF_ADDR_RLOCK(ifp);
 	IFNET_FOREACH_IFA(ifp, ifa)
 		if (ifa->ifa_addr->sa_family == AF_INET6 &&
 		    IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa))) {
 			struct carp_softc *sc = ifa->ifa_carp;
 			struct m_tag *mtag;
 
-			IF_ADDR_UNLOCK(ifp);
+			IF_ADDR_RUNLOCK(ifp);
 
 			mtag = m_tag_get(PACKET_TAG_CARP,
 			    sizeof(struct ifnet *), M_NOWAIT);
@@ -1043,7 +1043,7 @@
 
 			return (LLADDR(&sc->sc_addr));
 		}
-	IF_ADDR_UNLOCK(ifp);
+	IF_ADDR_RUNLOCK(ifp);
 
 	return (NULL);
 }
@@ -1512,10 +1512,10 @@
 	cif->cif_ifp = ifp;
 	TAILQ_INIT(&cif->cif_vrs);
 
-	IF_ADDR_LOCK(ifp);
+	IF_ADDR_WLOCK(ifp);
 	ifp->if_carp = cif;
 	if_ref(ifp);
-	IF_ADDR_UNLOCK(ifp);
+	IF_ADDR_WUNLOCK(ifp);
 
 	return (cif);
 
@@ -1534,10 +1534,10 @@
 	KASSERT(TAILQ_EMPTY(&cif->cif_vrs), ("%s: softc list not empty",
 	    __func__));
 
-	IF_ADDR_LOCK(ifp);
+	IF_ADDR_WLOCK(ifp);
 	ifp->if_carp = NULL;
 	if_rele(ifp);
-	IF_ADDR_UNLOCK(ifp);
+	IF_ADDR_WUNLOCK(ifp);
 
 	CIF_LOCK_DESTROY(cif);
 
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/netinet/ip_icmp.c
--- a/head/sys/netinet/ip_icmp.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/netinet/ip_icmp.c	Fri Jan 13 15:51:01 2012 +0200
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/netinet/ip_icmp.c 223637 2011-06-28 11:57:25Z bz $");
+__FBSDID("$FreeBSD: head/sys/netinet/ip_icmp.c 229749 2012-01-07 00:11:36Z eadler $");
 
 #include "opt_inet.h"
 #include "opt_ipsec.h"
@@ -89,7 +89,7 @@
 #define	V_icmplim_output		VNET(icmplim_output)
 SYSCTL_VNET_INT(_net_inet_icmp, OID_AUTO, icmplim_output, CTLFLAG_RW,
 	&VNET_NAME(icmplim_output), 0,
-	"Enable rate limiting of ICMP responses");
+	"Enable logging of ICMP response rate limiting");
 
 #ifdef INET
 VNET_DEFINE(struct icmpstat, icmpstat);
@@ -703,7 +703,7 @@
 	 */
 	ifp = m->m_pkthdr.rcvif;
 	if (ifp != NULL && ifp->if_flags & IFF_BROADCAST) {
-		IF_ADDR_LOCK(ifp);
+		IF_ADDR_RLOCK(ifp);
 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 			if (ifa->ifa_addr->sa_family != AF_INET)
 				continue;
@@ -711,11 +711,11 @@
 			if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
 			    t.s_addr) {
 				t = IA_SIN(ia)->sin_addr;
-				IF_ADDR_UNLOCK(ifp);
+				IF_ADDR_RUNLOCK(ifp);
 				goto match;
 			}
 		}
-		IF_ADDR_UNLOCK(ifp);
+		IF_ADDR_RUNLOCK(ifp);
 	}
 	/*
 	 * If the packet was transiting through us, use the address of
@@ -724,16 +724,16 @@
 	 * criteria apply.
 	 */
 	if (V_icmp_rfi && ifp != NULL) {
-		IF_ADDR_LOCK(ifp);
+		IF_ADDR_RLOCK(ifp);
 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 			if (ifa->ifa_addr->sa_family != AF_INET)
 				continue;
 			ia = ifatoia(ifa);
 			t = IA_SIN(ia)->sin_addr;
-			IF_ADDR_UNLOCK(ifp);
+			IF_ADDR_RUNLOCK(ifp);
 			goto match;
 		}
-		IF_ADDR_UNLOCK(ifp);
+		IF_ADDR_RUNLOCK(ifp);
 	}
 	/*
 	 * If the incoming packet was not addressed directly to us, use
@@ -742,16 +742,16 @@
 	 * with normal source selection.
 	 */
 	if (V_reply_src[0] != '\0' && (ifp = ifunit(V_reply_src))) {
-		IF_ADDR_LOCK(ifp);
+		IF_ADDR_RLOCK(ifp);
 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 			if (ifa->ifa_addr->sa_family != AF_INET)
 				continue;
 			ia = ifatoia(ifa);
 			t = IA_SIN(ia)->sin_addr;
-			IF_ADDR_UNLOCK(ifp);
+			IF_ADDR_RUNLOCK(ifp);
 			goto match;
 		}
-		IF_ADDR_UNLOCK(ifp);
+		IF_ADDR_RUNLOCK(ifp);
 	}
 	/*
 	 * If the packet was transiting through us, use the address of
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/netinet/ip_input.c
--- a/head/sys/netinet/ip_input.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/netinet/ip_input.c	Fri Jan 13 15:51:01 2012 +0200
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/netinet/ip_input.c 226401 2011-10-15 16:28:06Z glebius $");
+__FBSDID("$FreeBSD: head/sys/netinet/ip_input.c 229621 2012-01-05 19:00:36Z jhb $");
 
 #include "opt_bootp.h"
 #include "opt_ipfw.h"
@@ -611,7 +611,7 @@
 	 * into the stack for SIMPLEX interfaces handled by ether_output().
 	 */
 	if (ifp != NULL && ifp->if_flags & IFF_BROADCAST) {
-		IF_ADDR_LOCK(ifp);
+		IF_ADDR_RLOCK(ifp);
 	        TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 			if (ifa->ifa_addr->sa_family != AF_INET)
 				continue;
@@ -619,18 +619,18 @@
 			if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
 			    ip->ip_dst.s_addr) {
 				ifa_ref(ifa);
-				IF_ADDR_UNLOCK(ifp);
+				IF_ADDR_RUNLOCK(ifp);
 				goto ours;
 			}
 #ifdef BOOTP_COMPAT
 			if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY) {
 				ifa_ref(ifa);
-				IF_ADDR_UNLOCK(ifp);
+				IF_ADDR_RUNLOCK(ifp);
 				goto ours;
 			}
 #endif
 		}
-		IF_ADDR_UNLOCK(ifp);
+		IF_ADDR_RUNLOCK(ifp);
 		ia = NULL;
 	}
 	/* RFC 3927 2.7: Do not forward datagrams for 169.254.0.0/16. */
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/netinet/sctp_bsd_addr.c
--- a/head/sys/netinet/sctp_bsd_addr.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/netinet/sctp_bsd_addr.c	Fri Jan 13 15:51:01 2012 +0200
@@ -33,7 +33,7 @@
 /* $KAME: sctp_output.c,v 1.46 2005/03/06 16:04:17 itojun Exp $	 */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/netinet/sctp_bsd_addr.c 228653 2011-12-17 19:21:40Z tuexen $");
+__FBSDID("$FreeBSD: head/sys/netinet/sctp_bsd_addr.c 229621 2012-01-05 19:00:36Z jhb $");
 
 #include <netinet/sctp_os.h>
 #include <netinet/sctp_var.h>
@@ -216,7 +216,7 @@
 
 	IFNET_RLOCK();
 	TAILQ_FOREACH(ifn, &MODULE_GLOBAL(ifnet), if_list) {
-		IF_ADDR_LOCK(ifn);
+		IF_ADDR_RLOCK(ifn);
 		TAILQ_FOREACH(ifa, &ifn->if_addrlist, ifa_list) {
 			if (ifa->ifa_addr == NULL) {
 				continue;
@@ -273,7 +273,7 @@
 				sctp_ifa->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
 			}
 		}
-		IF_ADDR_UNLOCK(ifn);
+		IF_ADDR_RUNLOCK(ifn);
 	}
 	IFNET_RUNLOCK();
 }
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/netinet/sctp_input.c
--- a/head/sys/netinet/sctp_input.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/netinet/sctp_input.c	Fri Jan 13 15:51:01 2012 +0200
@@ -33,7 +33,7 @@
 /* $KAME: sctp_input.c,v 1.27 2005/03/06 16:04:17 itojun Exp $	 */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/netinet/sctp_input.c 228907 2011-12-27 10:16:24Z tuexen $");
+__FBSDID("$FreeBSD: head/sys/netinet/sctp_input.c 229805 2012-01-08 09:56:24Z tuexen $");
 
 #include <netinet/sctp_os.h>
 #include <netinet/sctp_var.h>
@@ -88,23 +88,76 @@
 {
 	struct sctp_init *init;
 	struct mbuf *op_err;
-	uint32_t init_limit;
 
 	SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_init: handling INIT tcb:%p\n",
 	    stcb);
 	if (stcb == NULL) {
 		SCTP_INP_RLOCK(inp);
-		if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
-			goto outnow;
-		}
-	}
-	op_err = NULL;
+	}
+	/* validate length */
+	if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_chunk)) {
+		op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
+		sctp_abort_association(inp, stcb, m, iphlen, sh, op_err,
+		    vrf_id, port);
+		if (stcb)
+			*abort_no_unlock = 1;
+		goto outnow;
+	}
+	/* validate parameters */
 	init = &cp->init;
-	/* First are we accepting? */
-	if ((inp->sctp_socket->so_qlimit == 0) && (stcb == NULL)) {
-		SCTPDBG(SCTP_DEBUG_INPUT2,
-		    "sctp_handle_init: Abort, so_qlimit:%d\n",
-		    inp->sctp_socket->so_qlimit);
+	if (init->initiate_tag == 0) {
+		/* protocol error... send abort */
+		op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
+		sctp_abort_association(inp, stcb, m, iphlen, sh, op_err,
+		    vrf_id, port);
+		if (stcb)
+			*abort_no_unlock = 1;
+		goto outnow;
+	}
+	if (ntohl(init->a_rwnd) < SCTP_MIN_RWND) {
+		/* invalid parameter... send abort */
+		op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
+		sctp_abort_association(inp, stcb, m, iphlen, sh, op_err,
+		    vrf_id, port);
+		if (stcb)
+			*abort_no_unlock = 1;
+		goto outnow;
+	}
+	if (init->num_inbound_streams == 0) {
+		/* protocol error... send abort */
+		op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
+		sctp_abort_association(inp, stcb, m, iphlen, sh, op_err,
+		    vrf_id, port);
+		if (stcb)
+			*abort_no_unlock = 1;
+		goto outnow;
+	}
+	if (init->num_outbound_streams == 0) {
+		/* protocol error... send abort */
+		op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
+		sctp_abort_association(inp, stcb, m, iphlen, sh, op_err,
+		    vrf_id, port);
+		if (stcb)
+			*abort_no_unlock = 1;
+		goto outnow;
+	}
+	if (sctp_validate_init_auth_params(m, offset + sizeof(*cp),
+	    offset + ntohs(cp->ch.chunk_length))) {
+		/* auth parameter(s) error... send abort */
+		sctp_abort_association(inp, stcb, m, iphlen, sh, NULL, vrf_id, port);
+		if (stcb)
+			*abort_no_unlock = 1;
+		goto outnow;
+	}
+	/*
+	 * We are only accepting if we have a socket with positive
+	 * so_qlimit.
+	 */
+	if ((stcb == NULL) &&
+	    ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
+	    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
+	    (inp->sctp_socket == NULL) ||
+	    (inp->sctp_socket->so_qlimit == 0))) {
 		/*
 		 * FIX ME ?? What about TCP model and we have a
 		 * match/restart case? Actually no fix is needed. the lookup
@@ -117,71 +170,21 @@
 		 * accepts(). The App just looses and should NOT be in this
 		 * state :-)
 		 */
-		sctp_abort_association(inp, stcb, m, iphlen, sh, op_err,
-		    vrf_id, port);
-		if (stcb)
-			*abort_no_unlock = 1;
+		if (SCTP_BASE_SYSCTL(sctp_blackhole) == 0) {
+			sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id, port);
+		}
 		goto outnow;
 	}
-	if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_chunk)) {
-		/* Invalid length */
-		op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
-		sctp_abort_association(inp, stcb, m, iphlen, sh, op_err,
-		    vrf_id, port);
-		if (stcb)
-			*abort_no_unlock = 1;
-		goto outnow;
-	}
-	/* validate parameters */
-	if (init->initiate_tag == 0) {
-		/* protocol error... send abort */
-		op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
-		sctp_abort_association(inp, stcb, m, iphlen, sh, op_err,
-		    vrf_id, port);
-		if (stcb)
-			*abort_no_unlock = 1;
-		goto outnow;
-	}
-	if (ntohl(init->a_rwnd) < SCTP_MIN_RWND) {
-		/* invalid parameter... send abort */
-		op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
-		sctp_abort_association(inp, stcb, m, iphlen, sh, op_err,
-		    vrf_id, port);
-		if (stcb)
-			*abort_no_unlock = 1;
-		goto outnow;
-	}
-	if (init->num_inbound_streams == 0) {
-		/* protocol error... send abort */
-		op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
-		sctp_abort_association(inp, stcb, m, iphlen, sh, op_err,
-		    vrf_id, port);
-		if (stcb)
-			*abort_no_unlock = 1;
-		goto outnow;
-	}
-	if (init->num_outbound_streams == 0) {
-		/* protocol error... send abort */
-		op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
-		sctp_abort_association(inp, stcb, m, iphlen, sh, op_err,
-		    vrf_id, port);
-		if (stcb)
-			*abort_no_unlock = 1;
-		goto outnow;
-	}
-	init_limit = offset + ntohs(cp->ch.chunk_length);
-	if (sctp_validate_init_auth_params(m, offset + sizeof(*cp),
-	    init_limit)) {
-		/* auth parameter(s) error... send abort */
-		sctp_abort_association(inp, stcb, m, iphlen, sh, NULL, vrf_id, port);
-		if (stcb)
-			*abort_no_unlock = 1;
-		goto outnow;
-	}
-	/* send an INIT-ACK w/cookie */
-	SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending INIT-ACK\n");
-	sctp_send_initiate_ack(inp, stcb, m, iphlen, offset, sh, cp, vrf_id, port,
-	    ((stcb == NULL) ? SCTP_HOLDS_LOCK : SCTP_NOT_LOCKED));
+	if ((stcb != NULL) &&
+	    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT)) {
+		SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending SHUTDOWN-ACK\n");
+		sctp_send_shutdown_ack(stcb, NULL);
+		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
+	} else {
+		SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending INIT-ACK\n");
+		sctp_send_initiate_ack(inp, stcb, m, iphlen, offset, sh, cp, vrf_id, port,
+		    ((stcb == NULL) ? SCTP_HOLDS_LOCK : SCTP_NOT_LOCKED));
+	}
 outnow:
 	if (stcb == NULL) {
 		SCTP_INP_RUNLOCK(inp);
@@ -1025,7 +1028,7 @@
 		if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
 		    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
 			/* Set the connected flag to disconnected */
-			stcb->sctp_ep->sctp_socket->so_snd.sb_cc = 0;
+			stcb->sctp_socket->so_snd.sb_cc = 0;
 		}
 	}
 	SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
@@ -4499,7 +4502,6 @@
 	 * process all control chunks...
 	 */
 	if (((ch->chunk_type == SCTP_SELECTIVE_ACK) ||
-	/* EY */
 	    (ch->chunk_type == SCTP_NR_SELECTIVE_ACK) ||
 	    (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) &&
 	    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
@@ -4613,54 +4615,30 @@
 		}
 		switch (ch->chunk_type) {
 		case SCTP_INITIATION:
-			/* must be first and only chunk */
 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n");
-			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
-				/* We are not interested anymore? */
-				if ((stcb) && (stcb->asoc.total_output_queue_size)) {
-					/*
-					 * collision case where we are
-					 * sending to them too
-					 */
-					;
-				} else {
-					if (locked_tcb) {
-						SCTP_TCB_UNLOCK(locked_tcb);
-					}
-					*offset = length;
-					return (NULL);
-				}
-			}
-			if ((chk_length > SCTP_LARGEST_INIT_ACCEPTED) ||
-			    (num_chunks > 1) ||
-			    (SCTP_BASE_SYSCTL(sctp_strict_init) && (length - *offset > (int)SCTP_SIZE32(chk_length)))) {
+			/* The INIT chunk must be the only chunk. */
+			if ((num_chunks > 1) ||
+			    (length - *offset > (int)SCTP_SIZE32(chk_length))) {
+				sctp_abort_association(inp, stcb, m,
+				    iphlen, sh, NULL, vrf_id, port);
 				*offset = length;
-				if (locked_tcb) {
-					SCTP_TCB_UNLOCK(locked_tcb);
-				}
 				return (NULL);
 			}
-			if ((stcb != NULL) &&
-			    (SCTP_GET_STATE(&stcb->asoc) ==
-			    SCTP_STATE_SHUTDOWN_ACK_SENT)) {
-				sctp_send_shutdown_ack(stcb, NULL);
+			/* Honor our resource limit. */
+			if (chk_length > SCTP_LARGEST_INIT_ACCEPTED) {
+				struct mbuf *op_err;
+
+				op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
+				sctp_abort_association(inp, stcb, m,
+				    iphlen, sh, op_err, vrf_id, port);
 				*offset = length;
-				sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
-				if (locked_tcb) {
-					SCTP_TCB_UNLOCK(locked_tcb);
-				}
 				return (NULL);
 			}
-			if (netp) {
-				sctp_handle_init(m, iphlen, *offset, sh,
-				    (struct sctp_init_chunk *)ch, inp,
-				    stcb, &abort_no_unlock, vrf_id, port);
-			}
-			if (abort_no_unlock)
-				return (NULL);
-
+			sctp_handle_init(m, iphlen, *offset, sh,
+			    (struct sctp_init_chunk *)ch, inp,
+			    stcb, &abort_no_unlock, vrf_id, port);
 			*offset = length;
-			if (locked_tcb) {
+			if ((!abort_no_unlock) && (locked_tcb)) {
 				SCTP_TCB_UNLOCK(locked_tcb);
 			}
 			return (NULL);
@@ -4668,7 +4646,6 @@
 		case SCTP_PAD_CHUNK:
 			break;
 		case SCTP_INITIATION_ACK:
-			/* must be first and only chunk */
 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT-ACK\n");
 			if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
 				/* We are not interested anymore */
@@ -4697,8 +4674,9 @@
 					return (NULL);
 				}
 			}
+			/* The INIT-ACK chunk must be the only chunk. */
 			if ((num_chunks > 1) ||
-			    (SCTP_BASE_SYSCTL(sctp_strict_init) && (length - *offset > (int)SCTP_SIZE32(chk_length)))) {
+			    (length - *offset > (int)SCTP_SIZE32(chk_length))) {
 				*offset = length;
 				if (locked_tcb) {
 					SCTP_TCB_UNLOCK(locked_tcb);
@@ -4711,16 +4689,17 @@
 			} else {
 				ret = -1;
 			}
+			*offset = length;
+			if (abort_no_unlock) {
+				return (NULL);
+			}
 			/*
 			 * Special case, I must call the output routine to
 			 * get the cookie echoed
 			 */
-			if (abort_no_unlock)
-				return (NULL);
-
-			if ((stcb) && ret == 0)
+			if ((stcb != NULL) && (ret == 0)) {
 				sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
-			*offset = length;
+			}
 			if (locked_tcb) {
 				SCTP_TCB_UNLOCK(locked_tcb);
 			}
@@ -4977,7 +4956,6 @@
 		case SCTP_OPERATION_ERROR:
 			SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP-ERR\n");
 			if ((stcb) && netp && *netp && sctp_handle_error(ch, stcb, *netp) < 0) {
-
 				*offset = length;
 				return (NULL);
 			}
@@ -5009,23 +4987,11 @@
 			if ((stcb == NULL) && (inp->sctp_socket->so_qlen >= inp->sctp_socket->so_qlimit)) {
 				if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
 				    (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) {
-					struct mbuf *oper;
-					struct sctp_paramhdr *phdr;
-
-					oper = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
-					    0, M_DONTWAIT, 1, MT_DATA);
-					if (oper) {
-						SCTP_BUF_LEN(oper) =
-						    sizeof(struct sctp_paramhdr);
-						phdr = mtod(oper,
-						    struct sctp_paramhdr *);
-						phdr->param_type =
-						    htons(SCTP_CAUSE_OUT_OF_RESC);
-						phdr->param_length =
-						    htons(sizeof(struct sctp_paramhdr));
-					}
+					struct mbuf *op_err;
+
+					op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
 					sctp_abort_association(inp, stcb, m,
-					    iphlen, sh, oper, vrf_id, port);
+					    iphlen, sh, op_err, vrf_id, port);
 				}
 				*offset = length;
 				return (NULL);
@@ -5962,8 +5928,13 @@
 		if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
 			goto bad;
 		}
-		if (ch->chunk_type != SCTP_ABORT_ASSOCIATION)
-			sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id, port);
+		if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) {
+			if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
+			    ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
+			    (ch->chunk_type != SCTP_INIT))) {
+				sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id, port);
+			}
+		}
 		goto bad;
 	} else if (stcb == NULL) {
 		refcount_up = 1;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/netinet/sctp_sysctl.c
--- a/head/sys/netinet/sctp_sysctl.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/netinet/sctp_sysctl.c	Fri Jan 13 15:51:01 2012 +0200
@@ -31,7 +31,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/netinet/sctp_sysctl.c 228907 2011-12-27 10:16:24Z tuexen $");
+__FBSDID("$FreeBSD: head/sys/netinet/sctp_sysctl.c 229805 2012-01-08 09:56:24Z tuexen $");
 
 #include <netinet/sctp_os.h>
 #include <netinet/sctp.h>
@@ -58,7 +58,6 @@
 #if !defined(SCTP_WITH_NO_CSUM)
 	SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) = SCTPCTL_LOOPBACK_NOCSUM_DEFAULT;
 #endif
-	SCTP_BASE_SYSCTL(sctp_strict_init) = SCTPCTL_STRICT_INIT_DEFAULT;
 	SCTP_BASE_SYSCTL(sctp_peer_chunk_oh) = SCTPCTL_PEER_CHKOH_DEFAULT;
 	SCTP_BASE_SYSCTL(sctp_max_burst_default) = SCTPCTL_MAXBURST_DEFAULT;
 	SCTP_BASE_SYSCTL(sctp_fr_max_burst_default) = SCTPCTL_FRMAXBURST_DEFAULT;
@@ -118,6 +117,7 @@
 	SCTP_BASE_SYSCTL(sctp_rttvar_eqret) = SCTPCTL_RTTVAR_EQRET_DEFAULT;
 	SCTP_BASE_SYSCTL(sctp_steady_step) = SCTPCTL_RTTVAR_STEADYS_DEFAULT;
 	SCTP_BASE_SYSCTL(sctp_use_dccc_ecn) = SCTPCTL_RTTVAR_DCCCECN_DEFAULT;
+	SCTP_BASE_SYSCTL(sctp_blackhole) = SCTPCTL_BLACKHOLE_DEFAULT;
 
 #if defined(SCTP_LOCAL_TRACE_BUF)
 	memset(&SCTP_BASE_SYSCTL(sctp_log), 0, sizeof(struct sctp_log));
@@ -607,7 +607,6 @@
 #if !defined(SCTP_WITH_NO_CSUM)
 		RANGECHK(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback), SCTPCTL_LOOPBACK_NOCSUM_MIN, SCTPCTL_LOOPBACK_NOCSUM_MAX);
 #endif
-		RANGECHK(SCTP_BASE_SYSCTL(sctp_strict_init), SCTPCTL_STRICT_INIT_MIN, SCTPCTL_STRICT_INIT_MAX);
 		RANGECHK(SCTP_BASE_SYSCTL(sctp_peer_chunk_oh), SCTPCTL_PEER_CHKOH_MIN, SCTPCTL_PEER_CHKOH_MAX);
 		RANGECHK(SCTP_BASE_SYSCTL(sctp_max_burst_default), SCTPCTL_MAXBURST_MIN, SCTPCTL_MAXBURST_MAX);
 		RANGECHK(SCTP_BASE_SYSCTL(sctp_fr_max_burst_default), SCTPCTL_FRMAXBURST_MIN, SCTPCTL_FRMAXBURST_MAX);
@@ -667,6 +666,7 @@
 		RANGECHK(SCTP_BASE_SYSCTL(sctp_mobility_fasthandoff), SCTPCTL_MOBILITY_FASTHANDOFF_MIN, SCTPCTL_MOBILITY_FASTHANDOFF_MAX);
 		RANGECHK(SCTP_BASE_SYSCTL(sctp_enable_sack_immediately), SCTPCTL_SACK_IMMEDIATELY_ENABLE_MIN, SCTPCTL_SACK_IMMEDIATELY_ENABLE_MAX);
 		RANGECHK(SCTP_BASE_SYSCTL(sctp_inits_include_nat_friendly), SCTPCTL_NAT_FRIENDLY_INITS_MIN, SCTPCTL_NAT_FRIENDLY_INITS_MAX);
+		RANGECHK(SCTP_BASE_SYSCTL(sctp_blackhole), SCTPCTL_BLACKHOLE_MIN, SCTPCTL_BLACKHOLE_MAX);
 
 #ifdef SCTP_DEBUG
 		RANGECHK(SCTP_BASE_SYSCTL(sctp_debug_on), SCTPCTL_DEBUG_MIN, SCTPCTL_DEBUG_MAX);
@@ -876,10 +876,6 @@
     SCTPCTL_LOOPBACK_NOCSUM_DESC);
 #endif
 
-SYSCTL_VNET_PROC(_net_inet_sctp, OID_AUTO, strict_init, CTLTYPE_UINT | CTLFLAG_RW,
-    &SCTP_BASE_SYSCTL(sctp_strict_init), 0, sysctl_sctp_check, "IU",
-    SCTPCTL_STRICT_INIT_DESC);
-
 SYSCTL_VNET_PROC(_net_inet_sctp, OID_AUTO, peer_chkoh, CTLTYPE_UINT | CTLFLAG_RW,
     &SCTP_BASE_SYSCTL(sctp_peer_chunk_oh), 0, sysctl_sctp_check, "IU",
     SCTPCTL_PEER_CHKOH_DESC);
@@ -1129,6 +1125,10 @@
     &SCTP_BASE_SYSCTL(sctp_use_dccc_ecn), 0, sysctl_sctp_check, "IU",
     SCTPCTL_RTTVAR_DCCCECN_DESC);
 
+SYSCTL_VNET_PROC(_net_inet_sctp, OID_AUTO, blackhole, CTLTYPE_UINT | CTLFLAG_RW,
+    &SCTP_BASE_SYSCTL(sctp_blackhole), 0, sysctl_sctp_check, "IU",
+    SCTPCTL_BLACKHOLE_DESC);
+
 #ifdef SCTP_DEBUG
 SYSCTL_VNET_PROC(_net_inet_sctp, OID_AUTO, debug, CTLTYPE_UINT | CTLFLAG_RW,
     &SCTP_BASE_SYSCTL(sctp_debug_on), 0, sysctl_sctp_check, "IU",
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/netinet/sctp_sysctl.h
--- a/head/sys/netinet/sctp_sysctl.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/netinet/sctp_sysctl.h	Fri Jan 13 15:51:01 2012 +0200
@@ -31,7 +31,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/netinet/sctp_sysctl.h 228653 2011-12-17 19:21:40Z tuexen $");
+__FBSDID("$FreeBSD: head/sys/netinet/sctp_sysctl.h 229805 2012-01-08 09:56:24Z tuexen $");
 
 #ifndef __sctp_sysctl_h__
 #define __sctp_sysctl_h__
@@ -50,7 +50,6 @@
 #if !defined(SCTP_WITH_NO_CSUM)
 	uint32_t sctp_no_csum_on_loopback;
 #endif
-	uint32_t sctp_strict_init;
 	uint32_t sctp_peer_chunk_oh;
 	uint32_t sctp_max_burst_default;
 	uint32_t sctp_max_chunks_on_queue;
@@ -115,6 +114,7 @@
 	uint32_t sctp_vtag_time_wait;
 	uint32_t sctp_buffer_splitting;
 	uint32_t sctp_initial_cwnd;
+	uint32_t sctp_blackhole;
 #if defined(SCTP_DEBUG)
 	uint32_t sctp_debug_on;
 #endif
@@ -168,12 +168,6 @@
 #define SCTPCTL_LOOPBACK_NOCSUM_MAX	1
 #define SCTPCTL_LOOPBACK_NOCSUM_DEFAULT	1
 
-/* strict_init: Enable strict INIT/INIT-ACK singleton enforcement */
-#define SCTPCTL_STRICT_INIT_DESC	"Enable strict INIT/INIT-ACK singleton enforcement"
-#define SCTPCTL_STRICT_INIT_MIN		0
-#define SCTPCTL_STRICT_INIT_MAX		1
-#define SCTPCTL_STRICT_INIT_DEFAULT	1
-
 /* peer_chkoh: Amount to debit peers rwnd per chunk sent */
 #define SCTPCTL_PEER_CHKOH_DESC		"Amount to debit peers rwnd per chunk sent"
 #define SCTPCTL_PEER_CHKOH_MIN		0
@@ -526,6 +520,11 @@
 #define SCTPCTL_RTTVAR_DCCCECN_MAX	1
 #define SCTPCTL_RTTVAR_DCCCECN_DEFAULT	1	/* 0 means disable feature */
 
+#define SCTPCTL_BLACKHOLE_DESC		"Enable SCTP blackholing"
+#define SCTPCTL_BLACKHOLE_MIN		0
+#define SCTPCTL_BLACKHOLE_MAX		2
+#define SCTPCTL_BLACKHOLE_DEFAULT	SCTPCTL_BLACKHOLE_MIN
+
 #if defined(SCTP_DEBUG)
 /* debug: Configure debug output */
 #define SCTPCTL_DEBUG_DESC	"Configure debug output"
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/netinet/sctputil.c
--- a/head/sys/netinet/sctputil.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/netinet/sctputil.c	Fri Jan 13 15:51:01 2012 +0200
@@ -33,7 +33,7 @@
 /* $KAME: sctputil.c,v 1.37 2005/03/07 23:26:09 itojun Exp $	 */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/netinet/sctputil.c 228907 2011-12-27 10:16:24Z tuexen $");
+__FBSDID("$FreeBSD: head/sys/netinet/sctputil.c 229805 2012-01-08 09:56:24Z tuexen $");
 
 #include <netinet/sctp_os.h>
 #include <netinet/sctp_pcb.h>
@@ -1460,7 +1460,7 @@
 	type = tmr->type;
 	if (inp) {
 		SCTP_INP_INCR_REF(inp);
-		if ((inp->sctp_socket == 0) &&
+		if ((inp->sctp_socket == NULL) &&
 		    ((tmr->type != SCTP_TIMER_TYPE_INPKILL) &&
 		    (tmr->type != SCTP_TIMER_TYPE_INIT) &&
 		    (tmr->type != SCTP_TIMER_TYPE_SEND) &&
@@ -3801,6 +3801,7 @@
 {
 	struct sctp_chunkhdr *ch, chunk_buf;
 	unsigned int chk_length;
+	int contains_init_chunk;
 
 	SCTP_STAT_INCR_COUNTER32(sctps_outoftheblue);
 	/* Generate a TO address for future reference */
@@ -3810,6 +3811,7 @@
 			    SCTP_CALLED_DIRECTLY_NOCMPSET);
 		}
 	}
+	contains_init_chunk = 0;
 	ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
 	    sizeof(*ch), (uint8_t *) & chunk_buf);
 	while (ch != NULL) {
@@ -3819,6 +3821,9 @@
 			break;
 		}
 		switch (ch->chunk_type) {
+		case SCTP_INIT:
+			contains_init_chunk = 1;
+			break;
 		case SCTP_COOKIE_ECHO:
 			/* We hit here only if the assoc is being freed */
 			return;
@@ -3844,7 +3849,11 @@
 		ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
 		    sizeof(*ch), (uint8_t *) & chunk_buf);
 	}
-	sctp_send_abort(m, iphlen, sh, 0, op_err, vrf_id, port);
+	if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
+	    ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
+	    (contains_init_chunk == 0))) {
+		sctp_send_abort(m, iphlen, sh, 0, op_err, vrf_id, port);
+	}
 }
 
 /*
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/netinet/tcp_input.c
--- a/head/sys/netinet/tcp_input.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/netinet/tcp_input.c	Fri Jan 13 15:51:01 2012 +0200
@@ -48,7 +48,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/netinet/tcp_input.c 227309 2011-11-07 15:43:11Z ed $");
+__FBSDID("$FreeBSD: head/sys/netinet/tcp_input.c 229665 2012-01-05 22:29:11Z jhb $");
 
 #include "opt_ipfw.h"		/* for ipfw_fwd	*/
 #include "opt_inet.h"
@@ -1795,9 +1795,6 @@
 	win = sbspace(&so->so_rcv);
 	if (win < 0)
 		win = 0;
-	KASSERT(SEQ_GEQ(tp->rcv_adv, tp->rcv_nxt),
-	    ("tcp_input negative window: tp %p rcv_nxt %u rcv_adv %u", tp,
-	    tp->rcv_nxt, tp->rcv_adv));
 	tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
 
 	/* Reset receive buffer auto scaling when not in bulk receive mode. */
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/netinet/tcp_timewait.c
--- a/head/sys/netinet/tcp_timewait.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/netinet/tcp_timewait.c	Fri Jan 13 15:51:01 2012 +0200
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/netinet/tcp_timewait.c 221891 2011-05-14 14:41:40Z jhb $");
+__FBSDID("$FreeBSD: head/sys/netinet/tcp_timewait.c 229700 2012-01-06 12:49:01Z jhb $");
 
 #include "opt_inet.h"
 #include "opt_inet6.h"
@@ -242,10 +242,10 @@
 	/*
 	 * Recover last window size sent.
 	 */
-	KASSERT(SEQ_GEQ(tp->rcv_adv, tp->rcv_nxt),
-	    ("tcp_twstart negative window: tp %p rcv_nxt %u rcv_adv %u", tp,
-	    tp->rcv_nxt, tp->rcv_adv));
-	tw->last_win = (tp->rcv_adv - tp->rcv_nxt) >> tp->rcv_scale;
+	if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt))
+		tw->last_win = (tp->rcv_adv - tp->rcv_nxt) >> tp->rcv_scale;
+	else
+		tw->last_win = 0;
 
 	/*
 	 * Set t_recent if timestamps are used on the connection.
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/netinet/tcp_usrreq.c
--- a/head/sys/netinet/tcp_usrreq.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/netinet/tcp_usrreq.c	Fri Jan 13 15:51:01 2012 +0200
@@ -36,7 +36,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/netinet/tcp_usrreq.c 226448 2011-10-16 20:18:39Z andre $");
+__FBSDID("$FreeBSD: head/sys/netinet/tcp_usrreq.c 229714 2012-01-06 18:29:40Z np $");
 
 #include "opt_ddb.h"
 #include "opt_inet.h"
@@ -204,8 +204,10 @@
 			tcp_discardcb(tp);
 			in_pcbdetach(inp);
 			in_pcbfree(inp);
-		} else
+		} else {
 			in_pcbdetach(inp);
+			INP_WUNLOCK(inp);
+		}
 	}
 }
 
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/netinet6/icmp6.c
--- a/head/sys/netinet6/icmp6.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/netinet6/icmp6.c	Fri Jan 13 15:51:01 2012 +0200
@@ -61,7 +61,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/netinet6/icmp6.c 228966 2011-12-29 18:25:18Z jhb $");
+__FBSDID("$FreeBSD: head/sys/netinet6/icmp6.c 229621 2012-01-05 19:00:36Z jhb $");
 
 #include "opt_inet.h"
 #include "opt_inet6.h"
@@ -1782,7 +1782,7 @@
 	IFNET_RLOCK_NOSLEEP();
 	TAILQ_FOREACH(ifp, &V_ifnet, if_list) {
 		addrsofif = 0;
-		IF_ADDR_LOCK(ifp);
+		IF_ADDR_RLOCK(ifp);
 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 			if (ifa->ifa_addr->sa_family != AF_INET6)
 				continue;
@@ -1833,7 +1833,7 @@
 			}
 			addrsofif++; /* count the address */
 		}
-		IF_ADDR_UNLOCK(ifp);
+		IF_ADDR_RUNLOCK(ifp);
 		if (iffound) {
 			*ifpp = ifp;
 			IFNET_RUNLOCK_NOSLEEP();
@@ -1868,7 +1868,7 @@
   again:
 
 	for (; ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
-		IF_ADDR_LOCK(ifp);
+		IF_ADDR_RLOCK(ifp);
 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 			if (ifa->ifa_addr->sa_family != AF_INET6)
 				continue;
@@ -1923,7 +1923,7 @@
 			/* now we can copy the address */
 			if (resid < sizeof(struct in6_addr) +
 			    sizeof(u_int32_t)) {
-				IF_ADDR_UNLOCK(ifp);
+				IF_ADDR_RUNLOCK(ifp);
 				/*
 				 * We give up much more copy.
 				 * Set the truncate flag and return.
@@ -1970,7 +1970,7 @@
 			resid -= (sizeof(struct in6_addr) + sizeof(u_int32_t));
 			copied += (sizeof(struct in6_addr) + sizeof(u_int32_t));
 		}
-		IF_ADDR_UNLOCK(ifp);
+		IF_ADDR_RUNLOCK(ifp);
 		if (ifp0)	/* we need search only on the specified IF */
 			break;
 	}
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/netinet6/in6.c
--- a/head/sys/netinet6/in6.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/netinet6/in6.c	Fri Jan 13 15:51:01 2012 +0200
@@ -61,7 +61,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/netinet6/in6.c 229465 2012-01-04 07:01:23Z glebius $");
+__FBSDID("$FreeBSD: head/sys/netinet6/in6.c 229621 2012-01-05 19:00:36Z jhb $");
 
 #include "opt_compat.h"
 #include "opt_inet.h"
@@ -978,9 +978,9 @@
 		ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask;
 		ia->ia_ifp = ifp;
 		ifa_ref(&ia->ia_ifa);			/* if_addrhead */
-		IF_ADDR_LOCK(ifp);
+		IF_ADDR_WLOCK(ifp);
 		TAILQ_INSERT_TAIL(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
-		IF_ADDR_UNLOCK(ifp);
+		IF_ADDR_WUNLOCK(ifp);
 
 		ifa_ref(&ia->ia_ifa);			/* in6_ifaddrhead */
 		IN6_IFADDR_WLOCK();
@@ -1335,7 +1335,7 @@
 	 * link-local and node-local all-nodes multicast
 	 * address routes
 	 */
-	IF_ADDR_LOCK(ifp);
+	IF_ADDR_RLOCK(ifp);
 	TAILQ_FOREACH(ifa0, &ifp->if_addrhead, ifa_link) {
 		if ((ifa0->ifa_addr->sa_family != AF_INET6) ||
 		    memcmp(&satosin6(ifa0->ifa_addr)->sin6_addr,
@@ -1347,7 +1347,7 @@
 	}
 	if (ifa0 != NULL)
 		ifa_ref(ifa0);
-	IF_ADDR_UNLOCK(ifp);
+	IF_ADDR_RUNLOCK(ifp);
 
 	/*
 	 * Remove the loopback route to the interface address.
@@ -1517,9 +1517,9 @@
 {
 	int	s = splnet();
 
-	IF_ADDR_LOCK(ifp);
+	IF_ADDR_WLOCK(ifp);
 	TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
-	IF_ADDR_UNLOCK(ifp);
+	IF_ADDR_WUNLOCK(ifp);
 	ifa_free(&ia->ia_ifa);			/* if_addrhead */
 
 	/*
@@ -1746,7 +1746,7 @@
 			}
 		}
 
-		IF_ADDR_LOCK(ifp);
+		IF_ADDR_RLOCK(ifp);
 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 			if (ifa->ifa_addr->sa_family != AF_INET6)
 				continue;
@@ -1769,7 +1769,7 @@
 		}
 		if (ifa != NULL)
 			ifa_ref(ifa);
-		IF_ADDR_UNLOCK(ifp);
+		IF_ADDR_RUNLOCK(ifp);
 		if (!ifa)
 			return EADDRNOTAVAIL;
 		ia = ifa2ia6(ifa);
@@ -1853,13 +1853,13 @@
 	 * if this is its first address,
 	 * and to validate the address if necessary.
 	 */
-	IF_ADDR_LOCK(ifp);
+	IF_ADDR_RLOCK(ifp);
 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 		if (ifa->ifa_addr->sa_family != AF_INET6)
 			continue;
 		ifacount++;
 	}
-	IF_ADDR_UNLOCK(ifp);
+	IF_ADDR_RUNLOCK(ifp);
 
 	ia->ia_addr = *sin6;
 
@@ -1930,7 +1930,7 @@
 {
 	struct ifaddr *ifa;
 
-	IF_ADDR_LOCK(ifp);
+	IF_ADDR_RLOCK(ifp);
 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 		if (ifa->ifa_addr->sa_family != AF_INET6)
 			continue;
@@ -1942,7 +1942,7 @@
 			break;
 		}
 	}
-	IF_ADDR_UNLOCK(ifp);
+	IF_ADDR_RUNLOCK(ifp);
 
 	return ((struct in6_ifaddr *)ifa);
 }
@@ -1957,7 +1957,7 @@
 {
 	struct ifaddr *ifa;
 
-	IF_ADDR_LOCK(ifp);
+	IF_ADDR_RLOCK(ifp);
 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 		if (ifa->ifa_addr->sa_family != AF_INET6)
 			continue;
@@ -1966,7 +1966,7 @@
 			break;
 		}
 	}
-	IF_ADDR_UNLOCK(ifp);
+	IF_ADDR_RUNLOCK(ifp);
 
 	return ((struct in6_ifaddr *)ifa);
 }
@@ -2207,7 +2207,7 @@
 	 * If two or more, return one which matches the dst longest.
 	 * If none, return one of global addresses assigned other ifs.
 	 */
-	IF_ADDR_LOCK(ifp);
+	IF_ADDR_RLOCK(ifp);
 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 		if (ifa->ifa_addr->sa_family != AF_INET6)
 			continue;
@@ -2241,7 +2241,7 @@
 	}
 	if (besta) {
 		ifa_ref(&besta->ia_ifa);
-		IF_ADDR_UNLOCK(ifp);
+		IF_ADDR_RUNLOCK(ifp);
 		return (besta);
 	}
 
@@ -2262,10 +2262,10 @@
 
 		if (ifa != NULL)
 			ifa_ref(ifa);
-		IF_ADDR_UNLOCK(ifp);
+		IF_ADDR_RUNLOCK(ifp);
 		return (struct in6_ifaddr *)ifa;
 	}
-	IF_ADDR_UNLOCK(ifp);
+	IF_ADDR_RUNLOCK(ifp);
 
 	/* use the last-resort values, that are, deprecated addresses */
 	if (dep[0])
@@ -2285,7 +2285,7 @@
 	struct ifaddr *ifa;
 	struct in6_ifaddr *ia;
 
-	IF_ADDR_LOCK(ifp);
+	IF_ADDR_RLOCK(ifp);
 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 		if (ifa->ifa_addr->sa_family != AF_INET6)
 			continue;
@@ -2301,7 +2301,7 @@
 			    arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz));
 		}
 	}
-	IF_ADDR_UNLOCK(ifp);
+	IF_ADDR_RUNLOCK(ifp);
 
 	/*
 	 * special cases, like 6to4, are handled in in6_ifattach
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/netinet6/in6_ifattach.c
--- a/head/sys/netinet6/in6_ifattach.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/netinet6/in6_ifattach.c	Fri Jan 13 15:51:01 2012 +0200
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/netinet6/in6_ifattach.c 229546 2012-01-05 01:13:25Z bz $");
+__FBSDID("$FreeBSD: head/sys/netinet6/in6_ifattach.c 229621 2012-01-05 19:00:36Z jhb $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -243,7 +243,7 @@
 	static u_int8_t allone[8] =
 		{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
 
-	IF_ADDR_LOCK(ifp);
+	IF_ADDR_RLOCK(ifp);
 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 		if (ifa->ifa_addr->sa_family != AF_LINK)
 			continue;
@@ -255,7 +255,7 @@
 
 		goto found;
 	}
-	IF_ADDR_UNLOCK(ifp);
+	IF_ADDR_RUNLOCK(ifp);
 
 	return -1;
 
@@ -282,7 +282,7 @@
 
 		/* look at IEEE802/EUI64 only */
 		if (addrlen != 8 && addrlen != 6) {
-			IF_ADDR_UNLOCK(ifp);
+			IF_ADDR_RUNLOCK(ifp);
 			return -1;
 		}
 
@@ -292,11 +292,11 @@
 		 * card insertion.
 		 */
 		if (bcmp(addr, allzero, addrlen) == 0) {
-			IF_ADDR_UNLOCK(ifp);
+			IF_ADDR_RUNLOCK(ifp);
 			return -1;
 		}
 		if (bcmp(addr, allone, addrlen) == 0) {
-			IF_ADDR_UNLOCK(ifp);
+			IF_ADDR_RUNLOCK(ifp);
 			return -1;
 		}
 
@@ -317,11 +317,11 @@
 
 	case IFT_ARCNET:
 		if (addrlen != 1) {
-			IF_ADDR_UNLOCK(ifp);
+			IF_ADDR_RUNLOCK(ifp);
 			return -1;
 		}
 		if (!addr[0]) {
-			IF_ADDR_UNLOCK(ifp);
+			IF_ADDR_RUNLOCK(ifp);
 			return -1;
 		}
 
@@ -345,17 +345,17 @@
 		 * identifier source (can be renumbered).
 		 * we don't do this.
 		 */
-		IF_ADDR_UNLOCK(ifp);
+		IF_ADDR_RUNLOCK(ifp);
 		return -1;
 
 	default:
-		IF_ADDR_UNLOCK(ifp);
+		IF_ADDR_RUNLOCK(ifp);
 		return -1;
 	}
 
 	/* sanity check: g bit must not indicate "group" */
 	if (EUI64_GROUP(in6)) {
-		IF_ADDR_UNLOCK(ifp);
+		IF_ADDR_RUNLOCK(ifp);
 		return -1;
 	}
 
@@ -368,11 +368,11 @@
 	 */
 	if ((in6->s6_addr[8] & ~(EUI64_GBIT | EUI64_UBIT)) == 0x00 &&
 	    bcmp(&in6->s6_addr[9], allzero, 7) == 0) {
-		IF_ADDR_UNLOCK(ifp);
+		IF_ADDR_RUNLOCK(ifp);
 		return -1;
 	}
 
-	IF_ADDR_UNLOCK(ifp);
+	IF_ADDR_RUNLOCK(ifp);
 	return 0;
 }
 
@@ -833,9 +833,9 @@
 		}
 
 		/* remove from the linked list */
-		IF_ADDR_LOCK(ifp);
+		IF_ADDR_WLOCK(ifp);
 		TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
-		IF_ADDR_UNLOCK(ifp);
+		IF_ADDR_WUNLOCK(ifp);
 		ifa_free(ifa);				/* if_addrhead */
 
 		IN6_IFADDR_WLOCK();
@@ -950,7 +950,7 @@
 	 * We need to do this as IF_ADDR_LOCK() may be re-acquired
 	 * by code further down.
 	 */
-	IF_ADDR_LOCK(ifp);
+	IF_ADDR_RLOCK(ifp);
 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
 		if (ifma->ifma_addr->sa_family != AF_INET6 ||
 		    ifma->ifma_protospec == NULL)
@@ -958,7 +958,7 @@
 		inm = (struct in6_multi *)ifma->ifma_protospec;
 		LIST_INSERT_HEAD(&purgeinms, inm, in6m_entry);
 	}
-	IF_ADDR_UNLOCK(ifp);
+	IF_ADDR_RUNLOCK(ifp);
 
 	LIST_FOREACH_SAFE(inm, &purgeinms, in6m_entry, tinm) {
 		LIST_REMOVE(inm, in6m_entry);
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/netinet6/in6_mcast.c
--- a/head/sys/netinet6/in6_mcast.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/netinet6/in6_mcast.c	Fri Jan 13 15:51:01 2012 +0200
@@ -33,7 +33,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/netinet6/in6_mcast.c 227481 2011-11-13 02:32:10Z bz $");
+__FBSDID("$FreeBSD: head/sys/netinet6/in6_mcast.c 229621 2012-01-05 19:00:36Z jhb $");
 
 #include "opt_inet6.h"
 
@@ -400,7 +400,7 @@
 	 * re-acquire around the call.
 	 */
 	IN6_MULTI_LOCK_ASSERT();
-	IF_ADDR_LOCK(ifp);
+	IF_ADDR_WLOCK(ifp);
 
 	inm = in6m_lookup_locked(ifp, group);
 	if (inm != NULL) {
@@ -424,11 +424,11 @@
 	 * Check if a link-layer group is already associated
 	 * with this network-layer group on the given ifnet.
 	 */
-	IF_ADDR_UNLOCK(ifp);
+	IF_ADDR_WUNLOCK(ifp);
 	error = if_addmulti(ifp, (struct sockaddr *)&gsin6, &ifma);
 	if (error != 0)
 		return (error);
-	IF_ADDR_LOCK(ifp);
+	IF_ADDR_WLOCK(ifp);
 
 	/*
 	 * If something other than netinet6 is occupying the link-layer
@@ -455,7 +455,7 @@
 		goto out_locked;
 	}
 
-	IF_ADDR_LOCK_ASSERT(ifp);
+	IF_ADDR_WLOCK_ASSERT(ifp);
 
 	/*
 	 * A new in6_multi record is needed; allocate and initialize it.
@@ -487,7 +487,7 @@
 	*pinm = inm;
 
 out_locked:
-	IF_ADDR_UNLOCK(ifp);
+	IF_ADDR_WUNLOCK(ifp);
 	return (error);
 }
 
@@ -2719,7 +2719,7 @@
 
 	IN6_MULTI_LOCK();
 
-	IF_ADDR_LOCK(ifp);
+	IF_ADDR_RLOCK(ifp);
 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
 		if (ifma->ifma_addr->sa_family != AF_INET6 ||
 		    ifma->ifma_protospec == NULL)
@@ -2748,7 +2748,7 @@
 				break;
 		}
 	}
-	IF_ADDR_UNLOCK(ifp);
+	IF_ADDR_RUNLOCK(ifp);
 
 	IN6_MULTI_UNLOCK();
 
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/netinet6/in6_var.h
--- a/head/sys/netinet6/in6_var.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/netinet6/in6_var.h	Fri Jan 13 15:51:01 2012 +0200
@@ -58,7 +58,7 @@
  * SUCH DAMAGE.
  *
  *	@(#)in_var.h	8.1 (Berkeley) 6/10/93
- * $FreeBSD: head/sys/netinet6/in6_var.h 229276 2012-01-02 13:03:13Z bz $
+ * $FreeBSD: head/sys/netinet6/in6_var.h 229621 2012-01-05 19:00:36Z jhb $
  */
 
 #ifndef _NETINET6_IN6_VAR_H_
@@ -716,9 +716,9 @@
 	struct in6_multi *inm;
 
 	IN6_MULTI_LOCK();
-	IF_ADDR_LOCK(ifp);
+	IF_ADDR_RLOCK(ifp);
 	inm = in6m_lookup_locked(ifp, mcaddr);
-	IF_ADDR_UNLOCK(ifp);
+	IF_ADDR_RUNLOCK(ifp);
 	IN6_MULTI_UNLOCK();
 
 	return (inm);
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/netinet6/ip6_input.c
--- a/head/sys/netinet6/ip6_input.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/netinet6/ip6_input.c	Fri Jan 13 15:51:01 2012 +0200
@@ -61,7 +61,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/netinet6/ip6_input.c 225044 2011-08-20 17:05:11Z bz $");
+__FBSDID("$FreeBSD: head/sys/netinet6/ip6_input.c 229621 2012-01-05 19:00:36Z jhb $");
 
 #include "opt_inet.h"
 #include "opt_inet6.h"
@@ -617,7 +617,7 @@
 		bad = 1;
 #define	sa_equal(a1, a2)						\
 	(bcmp((a1), (a2), ((a1))->sin6_len) == 0)
-		IF_ADDR_LOCK(ifp);
+		IF_ADDR_RLOCK(ifp);
 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 			if (ifa->ifa_addr->sa_family != dst6.sin6_family)
 				continue;
@@ -649,7 +649,7 @@
 			    ip6_sprintf(ip6bufs, &ip6->ip6_src),
 			    ip6_sprintf(ip6bufd, &ip6->ip6_dst)));
 		}
-		IF_ADDR_UNLOCK(ifp);
+		IF_ADDR_RUNLOCK(ifp);
 		LLE_RUNLOCK(lle);
 		if (bad)
 			goto bad;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/netinet6/mld6.c
--- a/head/sys/netinet6/mld6.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/netinet6/mld6.c	Fri Jan 13 15:51:01 2012 +0200
@@ -64,7 +64,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/netinet6/mld6.c 229479 2012-01-04 13:35:20Z jhb $");
+__FBSDID("$FreeBSD: head/sys/netinet6/mld6.c 229621 2012-01-05 19:00:36Z jhb $");
 
 #include "opt_inet.h"
 #include "opt_inet6.h"
@@ -543,7 +543,7 @@
 
 	mli = MLD_IFINFO(ifp);
 	if (mli->mli_version == MLD_VERSION_2) {
-		IF_ADDR_LOCK(ifp);
+		IF_ADDR_RLOCK(ifp);
 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
 			if (ifma->ifma_addr->sa_family != AF_INET6 ||
 			    ifma->ifma_protospec == NULL)
@@ -555,7 +555,7 @@
 			}
 			in6m_clear_recorded(inm);
 		}
-		IF_ADDR_UNLOCK(ifp);
+		IF_ADDR_RUNLOCK(ifp);
 		SLIST_FOREACH_SAFE(inm, &mli->mli_relinmhead, in6m_nrele,
 		    tinm) {
 			SLIST_REMOVE_HEAD(&mli->mli_relinmhead, in6m_nrele);
@@ -694,7 +694,7 @@
 	if (timer == 0)
 		timer = 1;
 
-	IF_ADDR_LOCK(ifp);
+	IF_ADDR_RLOCK(ifp);
 	if (is_general_query) {
 		/*
 		 * For each reporting group joined on this
@@ -726,7 +726,7 @@
 		in6_clearscope(&mld->mld_addr);
 	}
 
-	IF_ADDR_UNLOCK(ifp);
+	IF_ADDR_RUNLOCK(ifp);
 	MLD_UNLOCK();
 	IN6_MULTI_UNLOCK();
 
@@ -937,10 +937,10 @@
 		 * Queries for groups we are not a member of on this
 		 * link are simply ignored.
 		 */
-		IF_ADDR_LOCK(ifp);
+		IF_ADDR_RLOCK(ifp);
 		inm = in6m_lookup_locked(ifp, &mld->mld_addr);
 		if (inm == NULL) {
-			IF_ADDR_UNLOCK(ifp);
+			IF_ADDR_RUNLOCK(ifp);
 			goto out_locked;
 		}
 		if (nsrc > 0) {
@@ -948,7 +948,7 @@
 			    &V_mld_gsrdelay)) {
 				CTR1(KTR_MLD, "%s: GS query throttled.",
 				    __func__);
-				IF_ADDR_UNLOCK(ifp);
+				IF_ADDR_RUNLOCK(ifp);
 				goto out_locked;
 			}
 		}
@@ -966,7 +966,7 @@
 
 		/* XXX Clear embedded scope ID as userland won't expect it. */
 		in6_clearscope(&mld->mld_addr);
-		IF_ADDR_UNLOCK(ifp);
+		IF_ADDR_RUNLOCK(ifp);
 	}
 
 out_locked:
@@ -1176,7 +1176,7 @@
 
 	IN6_MULTI_LOCK();
 	MLD_LOCK();
-	IF_ADDR_LOCK(ifp);
+	IF_ADDR_RLOCK(ifp);
 
 	/*
 	 * MLDv1 report suppression.
@@ -1224,8 +1224,8 @@
 	}
 
 out_locked:
+	IF_ADDR_RUNLOCK(ifp);
 	MLD_UNLOCK();
-	IF_ADDR_UNLOCK(ifp);
 	IN6_MULTI_UNLOCK();
 
 	/* XXX Clear embedded scope ID as userland won't expect it. */
@@ -1401,7 +1401,7 @@
 			IFQ_SET_MAXLEN(&scq, MLD_MAX_STATE_CHANGE_PACKETS);
 		}
 
-		IF_ADDR_LOCK(ifp);
+		IF_ADDR_RLOCK(ifp);
 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
 			if (ifma->ifma_addr->sa_family != AF_INET6 ||
 			    ifma->ifma_protospec == NULL)
@@ -1417,7 +1417,7 @@
 				break;
 			}
 		}
-		IF_ADDR_UNLOCK(ifp);
+		IF_ADDR_RUNLOCK(ifp);
 
 		switch (mli->mli_version) {
 		case MLD_VERSION_1:
@@ -1685,7 +1685,7 @@
 
 	ifp = mli->mli_ifp;
 
-	IF_ADDR_LOCK(ifp);
+	IF_ADDR_RLOCK(ifp);
 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
 		if (ifma->ifma_addr->sa_family != AF_INET6)
 			continue;
@@ -1722,7 +1722,7 @@
 			break;
 		}
 	}
-	IF_ADDR_UNLOCK(ifp);
+	IF_ADDR_RUNLOCK(ifp);
 	SLIST_FOREACH_SAFE(inm, &mli->mli_relinmhead, in6m_nrele, tinm) {
 		SLIST_REMOVE_HEAD(&mli->mli_relinmhead, in6m_nrele);
 		in6m_release_locked(inm);
@@ -2996,7 +2996,7 @@
 
 	ifp = mli->mli_ifp;
 
-	IF_ADDR_LOCK(ifp);
+	IF_ADDR_RLOCK(ifp);
 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
 		if (ifma->ifma_addr->sa_family != AF_INET6 ||
 		    ifma->ifma_protospec == NULL)
@@ -3027,7 +3027,7 @@
 			break;
 		}
 	}
-	IF_ADDR_UNLOCK(ifp);
+	IF_ADDR_RUNLOCK(ifp);
 
 	mld_dispatch_queue(&mli->mli_gq, MLD_MAX_RESPONSE_BURST);
 
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/netinet6/nd6.c
--- a/head/sys/netinet6/nd6.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/netinet6/nd6.c	Fri Jan 13 15:51:01 2012 +0200
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/netinet6/nd6.c 228966 2011-12-29 18:25:18Z jhb $");
+__FBSDID("$FreeBSD: head/sys/netinet6/nd6.c 229621 2012-01-05 19:00:36Z jhb $");
 
 #include "opt_inet.h"
 #include "opt_inet6.h"
@@ -692,7 +692,7 @@
 	struct in6_ifaddr *public_ifa6 = NULL;
 
 	ifp = ia6->ia_ifa.ifa_ifp;
-	IF_ADDR_LOCK(ifp);
+	IF_ADDR_RLOCK(ifp);
 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 		struct in6_ifaddr *it6;
 
@@ -734,7 +734,7 @@
 		if (public_ifa6 != NULL)
 			ifa_ref(&public_ifa6->ia_ifa);
 	}
-	IF_ADDR_UNLOCK(ifp);
+	IF_ADDR_RUNLOCK(ifp);
 
 	if (public_ifa6 != NULL) {
 		int e;
@@ -1366,7 +1366,7 @@
 			 */
 			int duplicated_linklocal = 0;
 
-			IF_ADDR_LOCK(ifp);
+			IF_ADDR_RLOCK(ifp);
 			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 				if (ifa->ifa_addr->sa_family != AF_INET6)
 					continue;
@@ -1377,7 +1377,7 @@
 					break;
 				}
 			}
-			IF_ADDR_UNLOCK(ifp);
+			IF_ADDR_RUNLOCK(ifp);
 
 			if (duplicated_linklocal) {
 				ND.flags |= ND6_IFF_IFDISABLED;
@@ -1395,14 +1395,14 @@
 			/* Mark all IPv6 address as tentative. */
 
 			ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED;
-			IF_ADDR_LOCK(ifp);
+			IF_ADDR_RLOCK(ifp);
 			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 				if (ifa->ifa_addr->sa_family != AF_INET6)
 					continue;
 				ia = (struct in6_ifaddr *)ifa;
 				ia->ia6_flags |= IN6_IFF_TENTATIVE;
 			}
-			IF_ADDR_UNLOCK(ifp);
+			IF_ADDR_RUNLOCK(ifp);
 		}
 
 		if (ND.flags & ND6_IFF_AUTO_LINKLOCAL) {
@@ -1422,7 +1422,7 @@
 				 */
 				int haslinklocal = 0;
 			
-				IF_ADDR_LOCK(ifp);
+				IF_ADDR_RLOCK(ifp);
 				TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 					if (ifa->ifa_addr->sa_family != AF_INET6)
 						continue;
@@ -1432,7 +1432,7 @@
 						break;
 					}
 				}
-				IF_ADDR_UNLOCK(ifp);
+				IF_ADDR_RUNLOCK(ifp);
 				if (!haslinklocal)
 					in6_ifattach(ifp, NULL);
 			}
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/netinet6/nd6_rtr.c
--- a/head/sys/netinet6/nd6_rtr.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/netinet6/nd6_rtr.c	Fri Jan 13 15:51:01 2012 +0200
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/netinet6/nd6_rtr.c 229547 2012-01-05 01:14:35Z bz $");
+__FBSDID("$FreeBSD: head/sys/netinet6/nd6_rtr.c 229621 2012-01-05 19:00:36Z jhb $");
 
 #include "opt_inet.h"
 #include "opt_inet6.h"
@@ -454,11 +454,11 @@
 	info.rti_info[RTAX_NETMASK] = rt_mask(rt);
 	ifp = rt->rt_ifp;
 	if (ifp != NULL) {
-		IF_ADDR_LOCK(ifp);
+		IF_ADDR_RLOCK(ifp);
 		ifa = TAILQ_FIRST(&ifp->if_addrhead);
 		info.rti_info[RTAX_IFP] = ifa->ifa_addr;
 		ifa_ref(ifa);
-		IF_ADDR_UNLOCK(ifp);
+		IF_ADDR_RUNLOCK(ifp);
 		info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
 	} else
 		ifa = NULL;
@@ -1110,7 +1110,7 @@
 	 * consider autoconfigured addresses while RFC2462 simply said
 	 * "address".
 	 */
-	IF_ADDR_LOCK(ifp);
+	IF_ADDR_RLOCK(ifp);
 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 		struct in6_ifaddr *ifa6;
 		u_int32_t remaininglifetime;
@@ -1233,7 +1233,7 @@
 		ifa6->ia6_lifetime = lt6_tmp;
 		ifa6->ia6_updatetime = time_second;
 	}
-	IF_ADDR_UNLOCK(ifp);
+	IF_ADDR_RUNLOCK(ifp);
 	if (ia6_match == NULL && new->ndpr_vltime) {
 		int ifidlen;
 
@@ -1591,14 +1591,14 @@
 	    IN6_IFF_NOTREADY | IN6_IFF_ANYCAST);
 	if (ifa == NULL) {
 		/* XXX: freebsd does not have ifa_ifwithaf */
-		IF_ADDR_LOCK(ifp);
+		IF_ADDR_RLOCK(ifp);
 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 			if (ifa->ifa_addr->sa_family == AF_INET6)
 				break;
 		}
 		if (ifa != NULL)
 			ifa_ref(ifa);
-		IF_ADDR_UNLOCK(ifp);
+		IF_ADDR_RUNLOCK(ifp);
 		/* should we care about ia6_flags? */
 	}
 	if (ifa == NULL) {
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/netinet6/sctp6_usrreq.c
--- a/head/sys/netinet6/sctp6_usrreq.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/netinet6/sctp6_usrreq.c	Fri Jan 13 15:51:01 2012 +0200
@@ -32,7 +32,7 @@
 /*	$KAME: sctp6_usrreq.c,v 1.38 2005/08/24 08:08:56 suz Exp $	*/
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/netinet6/sctp6_usrreq.c 228907 2011-12-27 10:16:24Z tuexen $");
+__FBSDID("$FreeBSD: head/sys/netinet6/sctp6_usrreq.c 229805 2012-01-08 09:56:24Z tuexen $");
 
 #include <netinet/sctp_os.h>
 #include <sys/proc.h>
@@ -236,8 +236,13 @@
 		if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
 			goto bad;
 		}
-		if (ch->chunk_type != SCTP_ABORT_ASSOCIATION)
-			sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id, port);
+		if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) {
+			if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
+			    ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
+			    (ch->chunk_type != SCTP_INIT))) {
+				sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id, port);
+			}
+		}
 		goto bad;
 	} else if (stcb == NULL) {
 		refcount_up = 1;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/netipx/ipx.c
--- a/head/sys/netipx/ipx.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/netipx/ipx.c	Fri Jan 13 15:51:01 2012 +0200
@@ -62,7 +62,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
+__FBSDID("$FreeBSD: head/sys/netipx/ipx.c 229621 2012-01-05 19:00:36Z jhb $");
 
 #include <sys/param.h>
 #include <sys/kernel.h>
@@ -214,9 +214,9 @@
 			IPX_IFADDR_WUNLOCK();
 
 			ifa_ref(&ia->ia_ifa);		/* if_addrhead */
-			IF_ADDR_LOCK(ifp);
+			IF_ADDR_WLOCK(ifp);
 			TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
-			IF_ADDR_UNLOCK(ifp);
+			IF_ADDR_WUNLOCK(ifp);
 		}
 		break;
 
@@ -253,9 +253,9 @@
 		ipx_ifscrub(ifp, ia);
 		ifa = (struct ifaddr *)ia;
 
-		IF_ADDR_LOCK(ifp);
+		IF_ADDR_WLOCK(ifp);
 		TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
-		IF_ADDR_UNLOCK(ifp);
+		IF_ADDR_WUNLOCK(ifp);
 		ifa_free(ifa);				/* if_addrhead */
 
 		IPX_IFADDR_WLOCK();
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/pc98/conf/GENERIC
--- a/head/sys/pc98/conf/GENERIC	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/pc98/conf/GENERIC	Fri Jan 13 15:51:01 2012 +0200
@@ -16,7 +16,7 @@
 # If you are in doubt as to the purpose or necessity of a line, check first
 # in NOTES.
 #
-# $FreeBSD: head/sys/pc98/conf/GENERIC 228973 2011-12-29 22:48:36Z rwatson $
+# $FreeBSD: head/sys/pc98/conf/GENERIC 229691 2012-01-06 08:51:20Z adrian $
 
 cpu		I486_CPU
 cpu		I586_CPU
@@ -214,7 +214,7 @@
 #device		wlan		# 802.11 support
 #options 	IEEE80211_DEBUG	# enable debug msgs
 #options 	IEEE80211_AMPDU_AGE	# age frames in AMPDU reorder q's
-#options 	IEEE80211_SUPPORT_MESH	# enable 802.11s draft support
+options 	IEEE80211_SUPPORT_MESH	# enable 802.11s draft support
 #device		wlan_wep	# 802.11 WEP support
 #device		wlan_ccmp	# 802.11 CCMP support
 #device		wlan_tkip	# 802.11 TKIP support
@@ -223,7 +223,7 @@
 #device		ath		# Atheros NIC's
 #device		ath_pci		# Atheros pci/cardbus glue
 #device		ath_hal		# pci/cardbus chip support
-#options 	AH_SUPPORT_AR5416	# enable AR5416 tx/rx descriptors
+options 	AH_SUPPORT_AR5416	# enable AR5416 tx/rx descriptors
 #device		ath_rate_sample	# SampleRate tx rate control for ath
 #device		ral		# Ralink Technology RT2500 wireless NICs.
 #device		wi		# WaveLAN/Intersil/Symbol 802.11 wireless NICs.
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/powerpc/conf/GENERIC
--- a/head/sys/powerpc/conf/GENERIC	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/powerpc/conf/GENERIC	Fri Jan 13 15:51:01 2012 +0200
@@ -16,7 +16,7 @@
 # If you are in doubt as to the purpose or necessity of a line, check first 
 # in NOTES.
 #
-# $FreeBSD: head/sys/powerpc/conf/GENERIC 228973 2011-12-29 22:48:36Z rwatson $
+# $FreeBSD: head/sys/powerpc/conf/GENERIC 229640 2012-01-05 20:19:33Z adrian $
 
 cpu		AIM
 ident		GENERIC
@@ -174,6 +174,10 @@
 device		cue		# CATC USB Ethernet
 device		kue		# Kawasaki LSI USB Ethernet
 
+# Wireless NIC cards
+options		IEEE80211_SUPPORT_MESH
+options		AH_SUPPORT_AR5416
+
 # FireWire support
 device		firewire	# FireWire bus code
 # sbp(4) works for some systems but causes boot failure on others
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/powerpc/conf/GENERIC64
--- a/head/sys/powerpc/conf/GENERIC64	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/powerpc/conf/GENERIC64	Fri Jan 13 15:51:01 2012 +0200
@@ -16,7 +16,7 @@
 # If you are in doubt as to the purpose or necessity of a line, check first 
 # in NOTES.
 #
-# $FreeBSD: head/sys/powerpc/conf/GENERIC64 224019 2011-07-14 15:35:43Z nwhitehorn $
+# $FreeBSD: head/sys/powerpc/conf/GENERIC64 229660 2012-01-05 22:06:01Z andreast $
 
 cpu		AIM
 ident		GENERIC
@@ -170,6 +170,10 @@
 device		cue		# CATC USB Ethernet
 device		kue		# Kawasaki LSI USB Ethernet
 
+# Wireless NIC cards
+options         IEEE80211_SUPPORT_MESH
+options         AH_SUPPORT_AR5416
+
 # FireWire support
 device		firewire	# FireWire bus code
 device		sbp		# SCSI over FireWire (Requires scbus and da)
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/powerpc/powerpc/busdma_machdep.c
--- a/head/sys/powerpc/powerpc/busdma_machdep.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/powerpc/powerpc/busdma_machdep.c	Fri Jan 13 15:51:01 2012 +0200
@@ -29,7 +29,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/powerpc/powerpc/busdma_machdep.c 227309 2011-11-07 15:43:11Z ed $");
+__FBSDID("$FreeBSD: head/sys/powerpc/powerpc/busdma_machdep.c 229967 2012-01-11 16:53:51Z nwhitehorn $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -51,6 +51,7 @@
 
 #include <machine/atomic.h>
 #include <machine/bus.h>
+#include <machine/cpufunc.h>
 #include <machine/md_var.h>
 
 #include "iommu_if.h"
@@ -979,6 +980,8 @@
 			dmat->bounce_zone->total_bounced++;
 		}
 	}
+
+	powerpc_sync();
 }
 
 static void
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/sparc64/conf/GENERIC
--- a/head/sys/sparc64/conf/GENERIC	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/sparc64/conf/GENERIC	Fri Jan 13 15:51:01 2012 +0200
@@ -16,7 +16,7 @@
 # If you are in doubt as to the purpose or necessity of a line, check first
 # in NOTES.
 #
-# $FreeBSD: head/sys/sparc64/conf/GENERIC 228973 2011-12-29 22:48:36Z rwatson $
+# $FreeBSD: head/sys/sparc64/conf/GENERIC 229997 2012-01-12 00:34:33Z ken $
 
 cpu		SUN4U
 ident		GENERIC
@@ -119,6 +119,7 @@
 device		cd		# CD
 device		pass		# Passthrough device (direct ATA/SCSI access)
 device		ses		# SCSI Environmental Services (and SAF-TE)
+device		ctl		# CAM Target Layer
 
 # RAID controllers
 #device		amr		# AMI MegaRAID
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/ufs/ufs/ufs_quota.c
--- a/head/sys/ufs/ufs/ufs_quota.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/ufs/ufs/ufs_quota.c	Fri Jan 13 15:51:01 2012 +0200
@@ -33,7 +33,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/sys/ufs/ufs/ufs_quota.c 222955 2011-06-10 22:19:44Z jeff $");
+__FBSDID("$FreeBSD: head/sys/ufs/ufs/ufs_quota.c 229828 2012-01-08 23:06:53Z kib $");
 
 #include "opt_ffs.h"
 
@@ -512,17 +512,29 @@
 
 	NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_USERSPACE, fname, td);
 	flags = FREAD | FWRITE;
+	vfs_ref(mp);
+	vfs_unbusy(mp);
 	error = vn_open(&nd, &flags, 0, NULL);
-	if (error)
+	if (error != 0) {
+		vfs_rel(mp);
 		return (error);
+	}
 	vfslocked = NDHASGIANT(&nd);
 	NDFREE(&nd, NDF_ONLY_PNBUF);
 	vp = nd.ni_vp;
-	if (vp->v_type != VREG) {
+	error = vfs_busy(mp, MBF_NOWAIT);
+	vfs_rel(mp);
+	if (error == 0) {
+		if (vp->v_type != VREG) {
+			error = EACCES;
+			vfs_unbusy(mp);
+		}
+	}
+	if (error != 0) {
 		VOP_UNLOCK(vp, 0);
 		(void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
 		VFS_UNLOCK_GIANT(vfslocked);
-		return (EACCES);
+		return (error);
 	}
 
 	UFS_LOCK(ump);
@@ -531,6 +543,7 @@
 		VOP_UNLOCK(vp, 0);
 		(void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
 		VFS_UNLOCK_GIANT(vfslocked);
+		vfs_unbusy(mp);
 		return (EALREADY);
 	}
 	ump->um_qflags[type] |= QTF_OPENING|QTF_CLOSING;
@@ -542,6 +555,7 @@
 		UFS_UNLOCK(ump);
 		(void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
 		VFS_UNLOCK_GIANT(vfslocked);
+		vfs_unbusy(mp);
 		return (error);
 	}
 	VOP_UNLOCK(vp, 0);
@@ -619,6 +633,7 @@
 		("quotaon: leaking flags"));
 	UFS_UNLOCK(ump);
 
+	vfs_unbusy(mp);
 	return (error);
 }
 
diff -r ebe87ce4d7dd -r 1750de5b0399 head/sys/vm/vm_object.h
--- a/head/sys/vm/vm_object.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/sys/vm/vm_object.h	Fri Jan 13 15:51:01 2012 +0200
@@ -57,7 +57,7 @@
  * any improvements or extensions that they make and grant Carnegie the
  * rights to redistribute these changes.
  *
- * $FreeBSD: head/sys/vm/vm_object.h 227530 2011-11-15 14:40:00Z kib $
+ * $FreeBSD: head/sys/vm/vm_object.h 229934 2012-01-10 18:05:44Z kib $
  */
 
 /*
@@ -96,7 +96,7 @@
 	objtype_t type;			/* type of pager */
 	u_short flags;			/* see below */
 	u_short pg_color;		/* (c) color of first page in obj */
-	u_short paging_in_progress;	/* Paging (in or out) so don't collapse or destroy */
+	u_int paging_in_progress;	/* Paging (in or out) so don't collapse or destroy */
 	int resident_page_count;	/* number of resident pages */
 	struct vm_object *backing_object; /* object that I'm a shadow of */
 	vm_ooffset_t backing_object_offset;/* Offset in backing object */
diff -r ebe87ce4d7dd -r 1750de5b0399 head/tools/bsdbox/Makefile
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/tools/bsdbox/Makefile	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,115 @@
+#$FreeBSD: head/tools/bsdbox/Makefile 229675 2012-01-06 00:56:31Z adrian $
+#	@(#)Makefile	8.1 (Berkeley) 6/2/93
+
+NO_MAN=
+
+.include <bsd.own.mk>
+MK_SSP=	no
+
+PROG=	bsdbox
+BINDIR?=/sbin
+
+#################################################################
+#
+# General notes:
+#
+# A number of Make variables are used to generate the crunchgen config file.
+#
+#  CRUNCH_SRCDIRS: lists directories to search for included programs
+#  CRUNCH_PROGS:  lists programs to be included
+#  CRUNCH_LIBS:  libraries to statically link with
+#  CRUNCH_SHLIBS:  libraries to dynamically link with
+#  CRUNCH_BUILDOPTS: generic build options to be added to every program
+#  CRUNCH_BUILDTOOLS: lists programs that need build tools built in the
+#	local architecture.
+#
+# Special options can be specified for individual programs
+#  CRUNCH_SRCDIR_$(P): base source directory for program $(P)
+#  CRUNCH_BUILDOPTS_$(P): additional build options for $(P)
+#  CRUNCH_ALIAS_$(P): additional names to be used for $(P)
+#
+# By default, any name appearing in CRUNCH_PROGS or CRUNCH_ALIAS_${P}
+# will be used to generate a hard link to the resulting binary.
+# Specific links can be suppressed by setting
+# CRUNCH_SUPPRESS_LINK_$(NAME) to 1.
+#
+
+# Define Makefile variable RESCUE
+CRUNCH_BUILDOPTS+= -DRESCUE
+
+# Don't do symlinks as part of the install
+CRUNCH_GENERATE_LINKS= no
+
+# Which sources have local-arch build tools?
+# Define as blank; othrs need to override
+CRUNCH_BUILDTOOLS=
+
+###################################################################
+# Programs from stock /bin
+#
+# WARNING: Changing this list may require adjusting
+# /usr/include/paths.h as well!  You were warned!
+#
+CRUNCH_SRCDIRS+= bin
+# These are required to be shared so login and su can run as
+# setuid binaries - they use these libraries. PAM needs to be
+# built dynamically or it tries to build _all_ of the modules
+# statically - and that ends very badly.
+CRUNCH_SHLIBS+=	-lc -lutil
+CRUNCH_LIBS+= -lkvm -lmemstat -lnetgraph
+CRUNCH_LIBS+= -lcrypt -ledit -ll -ltermcap
+
+###################################################################
+# Programs from standard /sbin
+#
+# WARNING: Changing this list may require adjusting
+# /usr/include/paths.h as well!  You were warned!
+#
+# Note that mdmfs have their own private 'pathnames.h'
+# headers in addition to the standard 'paths.h' header.
+#
+CRUNCH_SRCDIRS+= sbin
+
+CRUNCH_LIBS+= -lalias -lcam -lcurses -ldevstat -lipsec
+# Don't forget this - ifconfig, etc -adrian
+.if ${MK_IPX} != "no"
+CRUNCH_LIBS+= -lipx
+.endif
+CRUNCH_LIBS+= -lgeom -lbsdxml -ljail -lkiconv -lmd -lsbuf -lufs
+
+##################################################################
+# Programs from stock /usr/bin
+#
+CRUNCH_SRCDIRS+= usr.bin
+# grep
+CRUNCH_LIBS+=	-lbz2
+
+##################################################################
+# Programs from stock /usr/sbin
+#
+CRUNCH_SRCDIRS+= usr.sbin
+
+##################################################################
+
+CRUNCH_SRCDIRS+= libexec
+
+CRUNCH_LIBS+= -lm
+
+.include "Makefile.base"
+.include "Makefile.net"
+.include "Makefile.hostapd"
+.include "Makefile.textproc"
+.include "Makefile.login"
+.include "Makefile.kld"
+# telnet/telnetd are too broken to include as a crunchgen'ed binary,
+# thanks to some of the horrible layering violations going on.
+# .include "Makefile.telnetd"
+.include "Makefile.fs"
+
+CRUNCH_LIBS+= -lcrypto -lssl -lz
+
+# the crunchgen build environment
+.include <bsd.crunchgen.mk>
+
+# and since it creates a program..
+.include <bsd.prog.mk>
diff -r ebe87ce4d7dd -r 1750de5b0399 head/tools/bsdbox/Makefile.base
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/tools/bsdbox/Makefile.base	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,55 @@
+#
+# This builds a variety of "base" tools, useful for an embedded
+# system.
+#
+# $FreeBSD: head/tools/bsdbox/Makefile.base 229675 2012-01-06 00:56:31Z adrian $
+#
+CRUNCH_PROGS_sbin+=	dmesg sysctl init reboot
+CRUNCH_PROGS_bin+=	ls cat dd df cp hostname kill mkdir sleep ps ln rm hostname
+CRUNCH_PROGS_usr.bin+=	true false hexdump tail nc w head uname tset
+CRUNCH_PROGS_usr.sbin+=	gpioctl
+CRUNCH_ALIAS_w=		uptime
+CRUNCH_ALIAS_tset=	reset
+
+CRUNCH_PROGS_usr.bin+=	vmstat systat
+CRUNCH_LIBS+=		-ldevstat -lncursesw -lncurses -lmemstat -lkvm
+
+CRUNCH_PROGS_usr.bin+=	tar cpio
+# XXX SSL ?
+CRUNCH_LIBS+=		-larchive -lbz2 -lz -llzma -lbsdxml -lssl -lcrypto
+
+# Clear requires tput, and it's a shell script so it won't be crunched
+CRUNCH_PROGS_usr.bin+=	tput
+
+# sh
+CRUNCH_PROGS_bin+=	sh
+CRUNCH_ALIAS_sh= -sh
+CRUNCH_SUPPRESS_LINK_-sh=	1
+CRUNCH_BUILDTOOLS+=	bin/sh
+
+# chown
+CRUNCH_PROGS_usr.sbin+=	chown
+CRUNCH_ALIAS_chown=	chgrp
+
+# Basic filesystem stuff
+CRUNCH_PROGS_sbin+=	mount umount
+
+# grep
+# grep doesn't yet work -adrian
+CRUNCH_PROGS_usr.bin+=	grep 
+
+# less/more
+CRUNCH_PROGS_usr.bin+=	less
+CRUNCH_ALIAS_less=	more
+
+# passwd
+CRUNCH_PROGS_usr.bin+=	passwd
+# These need to be shared, or PAM wants to include _all_ of the libraries
+# at runtime.
+CRUNCH_SHLIBS+= -lpam -lbsm
+
+# gzip/gunzip
+CRUNCH_PROGS_usr.bin+=	gzip
+CRUNCH_ALIAS_gunzip=	gzip
+CRUNCH_ALIAS_gzcat=	gzip
+CRUNCH_LIBS+=		-lz -llzma -lbz2
diff -r ebe87ce4d7dd -r 1750de5b0399 head/tools/bsdbox/Makefile.fs
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/tools/bsdbox/Makefile.fs	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,17 @@
+#
+# Filesystem related tools
+#
+# $FreeBSD: head/tools/bsdbox/Makefile.fs 229675 2012-01-06 00:56:31Z adrian $
+
+# mfs
+CRUNCH_PROGS_sbin+=	mdmfs mdconfig newfs
+CRUNCH_ALIAS_mdmfs=	mount_mfs
+
+# UFS
+CRUNCH_PROGS_sbin+=	fsck_ffs
+CRUNCH_LIBS+= -lgeom
+CRUNCH_LIBS+= -lufs
+
+# msdos
+# CRUNCH_PROGS_sbin+=	mount_msdosfs
+# CRUNCH_LIBS+= -lkiconv
diff -r ebe87ce4d7dd -r 1750de5b0399 head/tools/bsdbox/Makefile.hostapd
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/tools/bsdbox/Makefile.hostapd	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,15 @@
+#
+# Build hostap/wpa_supplicant and supporting utilities.
+#
+# $FreeBSD: head/tools/bsdbox/Makefile.hostapd 229675 2012-01-06 00:56:31Z adrian $
+#
+CRUNCH_PROGS_usr.sbin+=	hostapd hostapd_cli
+CRUNCH_SRCDIR_hostapd=	$(.CURDIR)/../../usr.sbin/wpa/hostapd
+CRUNCH_SRCDIR_hostapd_cli=	$(.CURDIR)/../../usr.sbin/wpa/hostapd_cli
+
+CRUNCH_PROGS_usr.sbin+=	wpa_supplicant wpa_cli
+CRUNCH_SRCDIR_wpa_supplicant=	$(.CURDIR)/../../usr.sbin/wpa/wpa_supplicant
+CRUNCH_SRCDIR_wpa_cli=	$(.CURDIR)/../../usr.sbin/wpa/wpa_cli
+
+CRUNCH_LIBS+=	-lpcap
+
diff -r ebe87ce4d7dd -r 1750de5b0399 head/tools/bsdbox/Makefile.kld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/tools/bsdbox/Makefile.kld	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,6 @@
+#
+# This builds the kld related programs.
+#
+# $FreeBSD: head/tools/bsdbox/Makefile.kld 229675 2012-01-06 00:56:31Z adrian $
+#
+CRUNCH_PROGS_sbin+=	kldload kldunload kldstat
diff -r ebe87ce4d7dd -r 1750de5b0399 head/tools/bsdbox/Makefile.login
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/tools/bsdbox/Makefile.login	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,9 @@
+#
+# This builds login and friends.
+#
+# $FreeBSD: head/tools/bsdbox/Makefile.login 229675 2012-01-06 00:56:31Z adrian $
+#
+
+CRUNCH_PROGS_libexec+=	getty
+CRUNCH_PROGS_usr.bin+=	cap_mkdb
+CRUNCH_PROGS_usr.sbin+=	pwd_mkdb
diff -r ebe87ce4d7dd -r 1750de5b0399 head/tools/bsdbox/Makefile.net
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/tools/bsdbox/Makefile.net	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,32 @@
+#
+# This builds network tools.
+#
+# $FreeBSD: head/tools/bsdbox/Makefile.net 229675 2012-01-06 00:56:31Z adrian $
+#
+
+CRUNCH_PROGS_sbin+=	route ping
+CRUNCH_PROGS_usr.sbin+=	arp
+
+# inetd
+CRUNCH_PROGS_usr.sbin+=	inetd
+CRUNCH_LIBS+=	-lwrap
+
+#.if ${MK_INET6_SUPPORT} != "no"
+#CRUNCH_PROGS_sbin+= ping6
+#.endif
+
+# netstat
+CRUNCH_PROGS_usr.bin+=	netstat
+CRUNCH_LIBS+=	-lmemstat -lnetgraph
+CRUNCH_BUILDOPTS_netstat=-DMK_IPX_SUPPORT=no
+
+# ifconfig
+CRUNCH_PROGS_sbin+=	ifconfig
+CRUNCH_BUILDOPTS_ifconfig=-DMK_IPX_SUPPORT=no
+
+# wlan stuff
+CRUNCH_PROGS_usr.sbin+=	wlandebug
+
+# tcpdump
+CRUNCH_PROGS_usr.sbin+=	tcpdump
+# CRUNCH_LIBS+=	-lpcap -lcrypto
diff -r ebe87ce4d7dd -r 1750de5b0399 head/tools/bsdbox/Makefile.telnetd
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/tools/bsdbox/Makefile.telnetd	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,14 @@
+# Build telnetd
+# Question - why is telnetds objects ending up in the srcdir? -adrian
+
+# This won't work yet - because telnetd relies on libtelnet.a which includes
+# kerberos support by default; building telnetd without kerberos support
+# requires the cross-build world to be built the same.
+#   -adrian
+
+# $FreeBSD: head/tools/bsdbox/Makefile.telnetd 229675 2012-01-06 00:56:31Z adrian $
+
+CRUNCH_PROGS_libexec+=	telnetd
+CRUNCH_PROGS_usr.bin+=	telnet
+CRUNCH_LIBS+=		-lkrb5 -lhx509 -lasn1 -lcom_err -lroken -ltelnetd
+# CRUNCH_BUILDOPTS_telnetd=	WITHOUT_KERBEROS_SUPPORT=yes
diff -r ebe87ce4d7dd -r 1750de5b0399 head/tools/bsdbox/Makefile.textproc
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/tools/bsdbox/Makefile.textproc	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,20 @@
+#
+# This builds a variety of text processing tools that
+# may be useful on an embedded device.
+#
+# $FreeBSD: head/tools/bsdbox/Makefile.textproc 229675 2012-01-06 00:56:31Z adrian $
+#
+
+# Sed
+CRUNCH_PROGS_usr.bin+=	sed
+
+# Awk
+# Disable - it's big! -adrian
+#CRUNCH_PROGS_usr.bin+=	awk
+#CRUNCH_BUILDTOOLS+=	usr.bin/awk
+
+# vi
+# Disable - it's big! -adrian
+#CRUNCH_PROGS_usr.bin+=	vi
+
+CRUNCH_PROGS_usr.bin+=	ee
diff -r ebe87ce4d7dd -r 1750de5b0399 head/tools/bsdbox/README
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/tools/bsdbox/README	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,12 @@
+$FreeBSD: head/tools/bsdbox/README 229675 2012-01-06 00:56:31Z adrian $
+
+This is a very cut down implementation of a "busybox" style binary
+for FreeBSD.
+
+It's based on the rescue build system, which uses the crunchgen functionality
+to build binaries.
+
+It's a work in progress.
+
+  -- adrian
+
diff -r ebe87ce4d7dd -r 1750de5b0399 head/tools/regression/bin/sh/builtins/getopts1.0
--- a/head/tools/regression/bin/sh/builtins/getopts1.0	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/tools/regression/bin/sh/builtins/getopts1.0	Fri Jan 13 15:51:01 2012 +0200
@@ -1,6 +1,6 @@
-# $FreeBSD$
+# $FreeBSD: head/tools/regression/bin/sh/builtins/getopts1.0 229742 2012-01-06 23:20:33Z jilles $
 
-echo '-1-'
+printf -- '-1-\n'
 set -- -abc
 getopts "ab:" OPTION
 echo ${OPTION}
@@ -11,7 +11,7 @@
 # ksh93 20090505; pdksh 5.2.14p2; mksh R39c; bash 4.1 PL7; and zsh 4.3.10.
 # all recognize that "b" is missing its argument on the *first* iteration
 # of 'getopts' and do not produce the "a" in $OPTION.
-echo '-2-'
+printf -- '-2-\n'
 set -- -ab
 getopts "ab:" OPTION
 echo ${OPTION}
@@ -19,7 +19,7 @@
 echo ${OPTION}
 
 # The 'shift' is aimed at causing an error.
-echo '-3-'
+printf -- '-3-\n'
 shift 1
 getopts "ab:" OPTION
 echo ${OPTION}
diff -r ebe87ce4d7dd -r 1750de5b0399 head/tools/regression/msdosfs/msdosfstest-3.sh
--- a/head/tools/regression/msdosfs/msdosfstest-3.sh	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/tools/regression/msdosfs/msdosfstest-3.sh	Fri Jan 13 15:51:01 2012 +0200
@@ -1,5 +1,5 @@
 #!/bin/sh
-# $FreeBSD$
+# $FreeBSD: head/tools/regression/msdosfs/msdosfstest-3.sh 229656 2012-01-05 21:36:53Z uqs $
 # A really simple script to create a swap-backed msdosfs filesystem, then
 # test to make sure the mbnambuf optimisation(msdosfs_conv.c rev 1.40)
 # doesn't break multi-byte characters.
@@ -9,8 +9,10 @@
 bsdlabel -w md10 auto
 newfs_msdos -F 16 -b 8192 /dev/md10a
 mount_msdosfs -L zh_TW.Big5 -D CP950 /dev/md10a /tmp/msdosfstest/
-mkdir /tmp/msdosfstest/012345678_¨¸´c¤§¬ü
-cd /tmp/msdosfstest/012345678_¨¸´c¤§¬ü
+# The comment is UTF-8, the actual command uses the Big5 representation.
+# mkdir /tmp/msdosfstest/012345678_邪惡之美
+mkdir /tmp/msdosfstest/012345678_$'\250\270\264\143\244\247\254\374'
+cd /tmp/msdosfstest/012345678_$'\250\270\264\143\244\247\254\374'
 if [ $? -eq 0 ]; then
 	echo "ok 3";
 else
diff -r ebe87ce4d7dd -r 1750de5b0399 head/tools/regression/msdosfs/msdosfstest-4.sh
--- a/head/tools/regression/msdosfs/msdosfstest-4.sh	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/tools/regression/msdosfs/msdosfstest-4.sh	Fri Jan 13 15:51:01 2012 +0200
@@ -1,5 +1,5 @@
 #!/bin/sh
-# $FreeBSD$
+# $FreeBSD: head/tools/regression/msdosfs/msdosfstest-4.sh 229656 2012-01-05 21:36:53Z uqs $
 # A really simple script to create a swap-backed msdosfs filesystem, then
 # test to see if msdosfs_conv.c rev 1.45[1] works properly.
 
@@ -8,12 +8,12 @@
 bsdlabel -w md10 auto
 newfs_msdos -F 16 -b 8192 /dev/md10a
 mount_msdosfs -L uk_UA.KOI8-U -D CP866 -l /dev/md10a /tmp/msdosfstest
-# mkdir /tmp/msdosfstest/U+0456 (CYRILLIC SMALL LETTER BYELORUSSIAN-UKRAINIAN I)
-mkdir /tmp/msdosfstest/¦
+# The comment is UTF-8, the actual command uses the KOI8-U representation.
+# mkdir /tmp/msdosfstest/Ñ– (CYRILLIC SMALL LETTER BYELORUSSIAN-UKRAINIAN I)
+mkdir /tmp/msdosfstest/$'\246'
 if [ $? -eq 0 ]; then
 	echo "ok 4 (pass stage 1/3)"
-	# cd /tmp/msdosfstest/U+0456 (CYRILLIC SMALL LETTER BYELORUSSIAN-UKRAINIAN I)
-	cd /tmp/msdosfstest/¦
+	cd /tmp/msdosfstest/$'\246'
 	if [ $? -eq 0 ]; then
 		echo "ok 4 (pass stage 2/3)"
 		cd /tmp
diff -r ebe87ce4d7dd -r 1750de5b0399 head/tools/regression/msdosfs/msdosfstest-5.sh
--- a/head/tools/regression/msdosfs/msdosfstest-5.sh	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/tools/regression/msdosfs/msdosfstest-5.sh	Fri Jan 13 15:51:01 2012 +0200
@@ -1,5 +1,5 @@
 #!/bin/sh
-# $FreeBSD$
+# $FreeBSD: head/tools/regression/msdosfs/msdosfstest-5.sh 229656 2012-01-05 21:36:53Z uqs $
 # A really simple script to create a swap-backed msdosfs filesystem, then
 # test to see if msdosfs_conv.c rev 1.45[2] works properly.
 # Note that this is a requisite condition but far away from sufficient condition.
@@ -10,18 +10,20 @@
 bsdlabel -w md10 auto
 newfs_msdos -F 16 -b 8192 /dev/md10a
 mount_msdosfs -L ja_JP.eucJP -D CP932 -l /dev/md10a /tmp/msdosfstest
-# touch /tmp/msdosfstest/U+FF71 (HALFWIDTH KATAKANA LETTER A)
-touch /tmp/msdosfstest/Ž±
+# The comment is UTF-8, the actual command uses the eucJP representation.
+# touch /tmp/msdosfstest/ï½± (HALFWIDTH KATAKANA LETTER A)
+touch /tmp/msdosfstest/$'\216\261'
 if [ $? -eq 0 ]; then
 	umount /tmp/msdosfstest
 	mount_msdosfs -L ja_JP.eucJP -D CP932 -s /dev/md10a /tmp/msdosfstest
-	ls /tmp/msdosfstest/Ž±
+	ls /tmp/msdosfstest/$'\216\261'
 	if [ $? -eq 0 ]; then
 		echo "ok 5 (pass stage 1/2)"
 		umount /tmp/msdosfstest
 		mount_msdosfs -L uk_UA.KOI8-U -D CP866 -l /dev/md10a /tmp/msdosfstest
-		# ls /tmp/msdosfstest/0xb1 (MEDIUM SHADE)
-		ls /tmp/msdosfstest/‘
+		# The comment is UTF-8, the actual command uses the KOI8-U representation.
+		# ls /tmp/msdosfstest/â–’ (MEDIUM SHADE)
+		ls /tmp/msdosfstest/$'\221'
 		if [ $? -ne 0 ]; then
 			# assume that U+FF71 was recorded with long name
 			echo "ok 5 (pass stage 2/2)"
diff -r ebe87ce4d7dd -r 1750de5b0399 head/tools/regression/msdosfs/msdosfstest-6.sh
--- a/head/tools/regression/msdosfs/msdosfstest-6.sh	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/tools/regression/msdosfs/msdosfstest-6.sh	Fri Jan 13 15:51:01 2012 +0200
@@ -1,5 +1,5 @@
 #!/bin/sh
-# $FreeBSD$
+# $FreeBSD: head/tools/regression/msdosfs/msdosfstest-6.sh 229656 2012-01-05 21:36:53Z uqs $
 # A really simple script to create a swap-backed msdosfs filesystem, then
 # test to make sure the nmount conversion(mount_msdosfs.c rev 1.37)
 # doesn't break multi-byte characters.
@@ -9,8 +9,13 @@
 bsdlabel -w md10 auto
 newfs_msdos -F 32 -b 8192 /dev/md10a
 mount_msdosfs -L zh_TW.Big5 -D CP950 /dev/md10a /tmp/msdosfstest/
-mkdir '/tmp/msdosfstest/¬O§_¬Ý¹L§{¶¡±`¨£ªº³\¯øªå²\®ü¼y¥\®b¦Y»\¶º²Ä¥|¶°'
-cd '/tmp/msdosfstest/¬O§_¬Ý¹L§{¶¡±`¨£ªº³\¯øªå²\®ü¼y¥\®b¦Y»\¶º²Ä¥|¶°'
+# The comment is UTF-8, the actual command uses the Big5 representation.
+# mkdir /tmp/msdosfstest/是否看過坊間常見的許茹芸淚海æ
¶åŠŸå®´åƒè“‹é£¯ç¬¬å››é›†
+subdir=$'\254\117\247\137\254\335\271\114\247\173\266\241\261\140\250\243'\
+$'\252\272\263\134\257\370\252\345\262\134\256\374\274\171\245\134'\
+$'\256\142\246\131\273\134\266\272\262\304\245\174\266\260'
+mkdir /tmp/msdosfstest/$subdir
+cd /tmp/msdosfstest/$subdir
 if [ $? -eq 0 ]; then
 	echo "ok 6";
 else
diff -r ebe87ce4d7dd -r 1750de5b0399 head/tools/tools/nanobsd/pcengines/Files/root/.cshrc
--- a/head/tools/tools/nanobsd/pcengines/Files/root/.cshrc	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/tools/tools/nanobsd/pcengines/Files/root/.cshrc	Fri Jan 13 15:51:01 2012 +0200
@@ -1,4 +1,4 @@
-# $FreeBSD$
+# $FreeBSD: head/tools/tools/nanobsd/pcengines/Files/root/.cshrc 229831 2012-01-09 00:48:19Z eadler $
 #
 # .cshrc - csh resource script, read at beginning of execution by each shell
 #
@@ -14,7 +14,7 @@
 # A righteous umask
 umask 22
 
-set path = (/sbin /bin /usr/sbin /usr/bin /usr/games /usr/local/sbin /usr/local/bin /usr/X11R6/bin $HOME/bin)
+set path = (/sbin /bin /usr/sbin /usr/bin /usr/games /usr/local/sbin /usr/local/bin $HOME/bin)
 
 setenv	EDITOR	vi
 setenv	PAGER	more
diff -r ebe87ce4d7dd -r 1750de5b0399 head/tools/tools/wtap/wtap/Makefile
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/tools/tools/wtap/wtap/Makefile	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,9 @@
+# $FreeBSD: head/tools/tools/wtap/wtap/Makefile 229972 2012-01-11 17:51:56Z adrian $
+
+PROG=	wtap
+SRC=	wtap.c
+NO_MAN=	1
+
+CFLAGS+=	-I${.CURDIR}/../../../../sys/dev/wtap/
+
+.include <bsd.prog.mk>
diff -r ebe87ce4d7dd -r 1750de5b0399 head/tools/tools/wtap/wtap/wtap.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/tools/tools/wtap/wtap/wtap.c	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,82 @@
+/*-
+ * Copyright (c) 2010-2011 Monthadar Al Jaberi, TerraNet AB
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer,
+ *    without modification.
+ * 2. Redistributions in binary form must reproduce at minimum a disclaimer
+ *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
+ *    redistribution must be conditioned upon including a substantially
+ *    similar Disclaimer requirement for further binary redistribution.
+ *
+ * NO WARRANTY
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
+ * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
+ * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
+ * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGES.
+ *
+ * $FreeBSD: head/tools/tools/wtap/wtap/wtap.c 229972 2012-01-11 17:51:56Z adrian $
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+
+#include "if_wtapioctl.h"
+
+static int dev = -1;
+
+static void create(int id)
+{
+    if(ioctl(dev, WTAPIOCTLCRT, &id) < 0){
+	printf("error creating wtap with id=%d\n", id);
+    }
+}
+
+static void delete(int id)
+{
+    if(ioctl(dev, WTAPIOCTLDEL, &id) < 0){
+	printf("error deleting wtap with id=%d\n", id);
+    }
+}
+
+int main( int argc, const char* argv[])
+{
+    if(argc != 3){
+      printf("usage: %s [c | d] wtap_id\n", argv[0]);
+      return -1;
+    }
+    int id = atoi(argv[2]);
+    if(!(id >= 0 && id < 64)){
+	printf("wtap_id must be between 0 and 7\n");
+	return -1;
+    }
+    dev = open("/dev/wtapctl", O_RDONLY);
+    if(dev < 0){
+      printf("error opening wtapctl cdev\n");
+      return -1;
+    }
+    switch((char)*argv[1]){
+      case 'c':
+	create(id);
+	break;
+      case 'd':
+	delete(id);
+	break;
+      default:
+	printf("wtap ioctl: unkown command '%c'\n", *argv[1]);
+	return -1;
+    }
+    return 0;
+}
diff -r ebe87ce4d7dd -r 1750de5b0399 head/usr.bin/Makefile
--- a/head/usr.bin/Makefile	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/usr.bin/Makefile	Fri Jan 13 15:51:01 2012 +0200
@@ -1,5 +1,5 @@
 #	From: @(#)Makefile	8.3 (Berkeley) 1/7/94
-# $FreeBSD: head/usr.bin/Makefile 228857 2011-12-24 12:16:38Z marius $
+# $FreeBSD: head/usr.bin/Makefile 229997 2012-01-12 00:34:33Z ken $
 
 .include <bsd.own.mk>
 
@@ -33,6 +33,7 @@
 	compress \
 	cpuset \
 	csplit \
+	ctlstat \
 	cut \
 	dirname \
 	du \
diff -r ebe87ce4d7dd -r 1750de5b0399 head/usr.bin/brandelf/brandelf.c
--- a/head/usr.bin/brandelf/brandelf.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/usr.bin/brandelf/brandelf.c	Fri Jan 13 15:51:01 2012 +0200
@@ -28,7 +28,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/usr.bin/brandelf/brandelf.c 228991 2011-12-30 10:59:15Z uqs $");
+__FBSDID("$FreeBSD: head/usr.bin/brandelf/brandelf.c 229908 2012-01-10 02:58:29Z eadler $");
 
 #include <sys/types.h>
 #include <sys/elf_common.h>
@@ -64,7 +64,7 @@
 	const char *strtype = "FreeBSD";
 	int type = ELFOSABI_FREEBSD;
 	int retval = 0;
-	int ch, change = 0, verbose = 0, force = 0, listed = 0;
+	int ch, change = 0, force = 0, listed = 0;
 
 	while ((ch = getopt(argc, argv, "f:lt:v")) != -1)
 		switch (ch) {
@@ -84,7 +84,7 @@
 			listed = 1;
 			break;
 		case 'v':
-			verbose = 1;
+			/* does nothing */
 			break;
 		case 't':
 			if (force)
diff -r ebe87ce4d7dd -r 1750de5b0399 head/usr.bin/bsdiff/bsdiff/bsdiff.c
--- a/head/usr.bin/bsdiff/bsdiff/bsdiff.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/usr.bin/bsdiff/bsdiff/bsdiff.c	Fri Jan 13 15:51:01 2012 +0200
@@ -25,7 +25,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
+__FBSDID("$FreeBSD: head/usr.bin/bsdiff/bsdiff/bsdiff.c 229910 2012-01-10 02:58:44Z eadler $");
 
 #include <sys/types.h>
 
@@ -272,7 +272,7 @@
 	/* Compute the differences, writing ctrl as we go */
 	if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL)
 		errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err);
-	scan=0;len=0;
+	scan=0;len=0;pos=0;
 	lastscan=0;lastpos=0;lastoffset=0;
 	while(scan<newsize) {
 		oldscore=0;
diff -r ebe87ce4d7dd -r 1750de5b0399 head/usr.bin/ctlstat/Makefile
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/usr.bin/ctlstat/Makefile	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,8 @@
+# $FreeBSD: head/usr.bin/ctlstat/Makefile 229997 2012-01-12 00:34:33Z ken $
+
+PROG=		ctlstat
+MAN=		ctlstat.8
+SDIR=		${.CURDIR}/../../sys
+CFLAGS+=	-I${SDIR}
+
+.include <bsd.prog.mk>
diff -r ebe87ce4d7dd -r 1750de5b0399 head/usr.bin/ctlstat/ctlstat.8
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/usr.bin/ctlstat/ctlstat.8	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,122 @@
+.\" 
+.\" Copyright (c) 2010 Silicon Graphics International Corp.
+.\" All rights reserved.
+.\" 
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\"    notice, this list of conditions, and the following disclaimer,
+.\"    without modification.
+.\" 2. Redistributions in binary form must reproduce at minimum a disclaimer
+.\"    substantially similar to the "NO WARRANTY" disclaimer below
+.\"    ("Disclaimer") and any redistribution must be conditioned upon
+.\"    including a substantially similar Disclaimer requirement for further
+.\"    binary redistribution.
+.\" 
+.\" NO WARRANTY
+.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+.\" "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+.\" LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
+.\" A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+.\" HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+.\" IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+.\" POSSIBILITY OF SUCH DAMAGES.
+.\" 
+.\" ctlstat utility man page.
+.\"
+.\" Author: Ken Merry <ken at FreeBSD.org>
+.\"
+.\" $Id: //depot/users/kenm/FreeBSD-test2/usr.bin/ctlstat/ctlstat.8#2 $
+.\" $FreeBSD: head/usr.bin/ctlstat/ctlstat.8 229997 2012-01-12 00:34:33Z ken $
+.\"
+.Dd June 4, 2010
+.Dt CTLSTAT 8
+.Os
+.Sh NAME
+.Nm ctlstat
+.Nd CAM Target Layer statistics utility
+.Sh SYNOPSIS
+.Nm
+.Op Fl t
+.Op Fl c Ar count
+.Op Fl C
+.Op Fl d
+.Op Fl D
+.Op Fl j
+.Op Fl l Ar lun
+.Op Fl n Ar numdevs
+.Op Fl w Ar wait
+.Sh DESCRIPTION
+The
+.Nm
+utility provides statistics information for the CAM Target Layer.
+The first display (except for dump and JSON modes) shows average statistics
+since system startup.
+Subsequent displays show average statistics during the measurement
+interval.
+.Pp
+The options are as follows:
+.Bl -tag -width 10n
+.It Fl t
+Total mode.
+This displays separate columns with the total CTL read and write output,
+and a combined total column that also includes non I/O operations.
+.It Fl c Ar count
+Display statistics this many times.
+.It Fl C
+Disable display of CPU statistics.
+.It Fl d
+Display DMA operation time (latency) instead of overall I/O time (latency).
+.It Fl D
+Text dump mode.
+Dump all available statistics every 30 seconds in a text format suitable
+for parsing.
+No statistics are computed in this mode, only raw numbers are displayed.
+.It Fl h
+Suppress display of the header.
+.It Fl j
+JSON dump mode.
+Dump all available statistics every 30 seconds in JavaScript Object
+Notation (JSON) format.
+No statistics are computed in this mode, only raw numbers are displayed.
+.It Fl l Ar lun
+Request statistics for the specified LUN.
+This option is incompatible with total (
+.Fl t )
+mode.
+.It Fl n Ar numdevs
+Display statistics for this many devices.
+.It Fl w Ar wait
+Wait this many seconds in between displays.
+If this option is not specified,
+.Nm
+defaults to a 1 second interval.
+.El
+.Sh EXAMPLES
+.Dl ctlstat -t
+.Pp
+Display total statistics for the system with a one second interval.
+.Pp
+.Dl ctlstat -d -l 5 -C
+.Pp
+Display average DMA time for LUN 5 and omit CPU utilization.
+.Pp
+.Dl ctlstat -n 7 -w 10
+.Pp
+Display statistics for the first 7 LUNs, and display average statistics
+every 10 seconds.
+.Sh SEE ALSO
+.Xr cam 3 ,
+.Xr cam 4 ,
+.Xr xpt 4 ,
+.Xr camcontrol 8 ,
+.Xr ctladm 8 ,
+.Xr iostat 8
+.Sh AUTHORS
+.An Ken Merry Aq ken at FreeBSD.org
+.An Will Andrews Aq will at FreeBSD.org
diff -r ebe87ce4d7dd -r 1750de5b0399 head/usr.bin/ctlstat/ctlstat.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/usr.bin/ctlstat/ctlstat.c	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,730 @@
+/*-
+ * Copyright (c) 2004, 2008, 2009 Silicon Graphics International Corp.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions, and the following disclaimer,
+ *    without modification.
+ * 2. Redistributions in binary form must reproduce at minimum a disclaimer
+ *    substantially similar to the "NO WARRANTY" disclaimer below
+ *    ("Disclaimer") and any redistribution must be conditioned upon
+ *    including a substantially similar Disclaimer requirement for further
+ *    binary redistribution.
+ *
+ * NO WARRANTY
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGES.
+ *
+ * $Id: //depot/users/kenm/FreeBSD-test2/usr.bin/ctlstat/ctlstat.c#4 $
+ */
+/*
+ * CAM Target Layer statistics program
+ *
+ * Authors: Ken Merry <ken at FreeBSD.org>, Will Andrews <will at FreeBSD.org>
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD: head/usr.bin/ctlstat/ctlstat.c 229997 2012-01-12 00:34:33Z ken $");
+
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/param.h>
+#include <sys/time.h>
+#include <sys/sysctl.h>
+#include <sys/resource.h>
+#include <sys/queue.h>
+#include <sys/callout.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <getopt.h>
+#include <string.h>
+#include <errno.h>
+#include <err.h>
+#include <ctype.h>
+#include <bitstring.h>
+#include <cam/scsi/scsi_all.h>
+#include <cam/ctl/ctl.h>
+#include <cam/ctl/ctl_io.h>
+#include <cam/ctl/ctl_scsi_all.h>
+#include <cam/ctl/ctl_util.h>
+#include <cam/ctl/ctl_frontend_internal.h>
+#include <cam/ctl/ctl_backend.h>
+#include <cam/ctl/ctl_ioctl.h>
+
+/*
+ * The default amount of space we allocate for LUN storage space.  We
+ * dynamically allocate more if needed.
+ */
+#define	CTL_STAT_NUM_LUNS	30
+
+/*
+ * The default number of LUN selection bits we allocate.  This is large
+ * because we don't currently increase it if the user specifies a LUN
+ * number of 1024 or larger.
+ */
+#define	CTL_STAT_LUN_BITS	1024L
+
+static const char *ctlstat_opts = "Cc:Ddhjl:n:tw:";
+static const char *ctlstat_usage = "Usage:  ctlstat [-CDdjht] [-l lunnum]"
+				   "[-c count] [-n numdevs] [-w wait]\n";
+
+struct ctl_cpu_stats {
+	uint64_t user;
+	uint64_t nice;
+	uint64_t system;
+	uint64_t intr;
+	uint64_t idle;
+};
+
+typedef enum {
+	CTLSTAT_MODE_STANDARD,
+	CTLSTAT_MODE_DUMP,
+	CTLSTAT_MODE_JSON,
+} ctlstat_mode_types;
+
+#define	CTLSTAT_FLAG_CPU		(1 << 0)
+#define	CTLSTAT_FLAG_HEADER		(1 << 1)
+#define	CTLSTAT_FLAG_FIRST_RUN		(1 << 2)
+#define	CTLSTAT_FLAG_TOTALS		(1 << 3)
+#define	CTLSTAT_FLAG_DMA_TIME		(1 << 4)
+#define	CTLSTAT_FLAG_LUN_TIME_VALID	(1 << 5)
+#define	F_CPU(ctx) ((ctx)->flags & CTLSTAT_FLAG_CPU)
+#define	F_HDR(ctx) ((ctx)->flags & CTLSTAT_FLAG_HEADER)
+#define	F_FIRST(ctx) ((ctx)->flags & CTLSTAT_FLAG_FIRST_RUN)
+#define	F_TOTALS(ctx) ((ctx)->flags & CTLSTAT_FLAG_TOTALS)
+#define	F_DMA(ctx) ((ctx)->flags & CTLSTAT_FLAG_DMA_TIME)
+#define	F_LUNVAL(ctx) ((ctx)->flags & CTLSTAT_FLAG_LUN_TIME_VALID)
+
+struct ctlstat_context {
+	ctlstat_mode_types mode;
+	int flags;
+	struct ctl_lun_io_stats *cur_lun_stats, *prev_lun_stats,
+		*tmp_lun_stats;
+	struct ctl_lun_io_stats cur_total_stats[3], prev_total_stats[3];
+	struct timespec cur_time, prev_time;
+	struct ctl_cpu_stats cur_cpu, prev_cpu;
+	uint64_t cur_total_jiffies, prev_total_jiffies;
+	uint64_t cur_idle, prev_idle;
+	bitstr_t bit_decl(lun_mask, CTL_STAT_LUN_BITS);
+	int num_luns;
+	int numdevs;
+	int header_interval;
+};
+
+#ifndef min
+#define	min(x,y)	(((x) < (y)) ? (x) : (y))
+#endif
+
+static void usage(int error);
+static int getstats(int fd, int *num_luns, struct ctl_lun_io_stats **xlun_stats,
+		    struct timespec *cur_time, int *lun_time_valid);
+static int getcpu(struct ctl_cpu_stats *cpu_stats);
+static void compute_stats(struct ctl_lun_io_stats *cur_stats,
+			  struct ctl_lun_io_stats *prev_stats,
+			  long double etime, long double *mbsec,
+			  long double *kb_per_transfer,
+			  long double *transfers_per_second,
+			  long double *ms_per_transfer,
+			  long double *ms_per_dma,
+			  long double *dmas_per_second);
+
+static void
+usage(int error)
+{
+	fprintf(error ? stderr : stdout, ctlstat_usage);
+}
+
+static int
+getstats(int fd, int *num_luns, struct ctl_lun_io_stats **xlun_stats,
+	 struct timespec *cur_time, int *flags)
+{
+	struct ctl_lun_io_stats *lun_stats;
+	struct ctl_stats stats;
+	int more_space_count;
+
+	more_space_count = 0;
+
+	if (*num_luns == 0)
+		*num_luns = CTL_STAT_NUM_LUNS;
+
+	lun_stats = *xlun_stats;
+retry:
+
+	if (lun_stats == NULL) {
+		lun_stats = (struct ctl_lun_io_stats *)malloc(
+			sizeof(*lun_stats) * *num_luns);
+	}
+
+	memset(&stats, 0, sizeof(stats));
+	stats.alloc_len = *num_luns * sizeof(*lun_stats);
+	memset(lun_stats, 0, stats.alloc_len);
+	stats.lun_stats = lun_stats;
+
+	if (ioctl(fd, CTL_GETSTATS, &stats) == -1)
+		err(1, "error returned from CTL_GETSTATS ioctl");
+
+	switch (stats.status) {
+	case CTL_SS_OK:
+		break;
+	case CTL_SS_ERROR:
+		err(1, "CTL_SS_ERROR returned from CTL_GETSTATS ioctl");
+		break;
+	case CTL_SS_NEED_MORE_SPACE:
+		if (more_space_count > 0) {
+			errx(1, "CTL_GETSTATS returned NEED_MORE_SPACE again");
+		}
+		*num_luns = stats.num_luns;
+		free(lun_stats);
+		lun_stats = NULL;
+		more_space_count++;
+		goto retry;
+		break; /* NOTREACHED */
+	default:
+		errx(1, "unknown status %d returned from CTL_GETSTATS ioctl",
+		     stats.status);
+		break;
+	}
+
+	*xlun_stats = lun_stats;
+	*num_luns = stats.num_luns;
+	cur_time->tv_sec = stats.timestamp.tv_sec;
+	cur_time->tv_nsec = stats.timestamp.tv_nsec;
+	if (stats.flags & CTL_STATS_FLAG_TIME_VALID)
+		*flags |= CTLSTAT_FLAG_LUN_TIME_VALID;
+	else
+		*flags &= ~CTLSTAT_FLAG_LUN_TIME_VALID;
+
+	return (0);
+}
+
+static int
+getcpu(struct ctl_cpu_stats *cpu_stats)
+{
+	long cp_time[CPUSTATES];
+	size_t cplen;
+
+	cplen = sizeof(cp_time);
+
+	if (sysctlbyname("kern.cp_time", &cp_time, &cplen, NULL, 0) == -1) {
+		warn("sysctlbyname(kern.cp_time...) failed");
+		return (1);
+	}
+
+	cpu_stats->user = cp_time[CP_USER];
+	cpu_stats->nice = cp_time[CP_NICE];
+	cpu_stats->system = cp_time[CP_SYS];
+	cpu_stats->intr = cp_time[CP_INTR];
+	cpu_stats->idle = cp_time[CP_IDLE];
+
+	return (0);
+}
+
+static void
+compute_stats(struct ctl_lun_io_stats *cur_stats,
+	      struct ctl_lun_io_stats *prev_stats, long double etime,
+	      long double *mbsec, long double *kb_per_transfer,
+	      long double *transfers_per_second, long double *ms_per_transfer,
+	      long double *ms_per_dma, long double *dmas_per_second)
+{
+	uint64_t total_bytes = 0, total_operations = 0, total_dmas = 0;
+	uint32_t port;
+	struct bintime total_time_bt, total_dma_bt;
+	struct timespec total_time_ts, total_dma_ts;
+	int i;
+
+	bzero(&total_time_bt, sizeof(total_time_bt));
+	bzero(&total_dma_bt, sizeof(total_dma_bt));
+	bzero(&total_time_ts, sizeof(total_time_ts));
+	bzero(&total_dma_ts, sizeof(total_dma_ts));
+	for (port = 0; port < CTL_MAX_PORTS; port++) {
+		for (i = 0; i < CTL_STATS_NUM_TYPES; i++) {
+			total_bytes += cur_stats->ports[port].bytes[i];
+			total_operations +=
+			    cur_stats->ports[port].operations[i];
+			total_dmas += cur_stats->ports[port].num_dmas[i];
+			bintime_add(&total_time_bt,
+			    &cur_stats->ports[port].time[i]);
+			bintime_add(&total_dma_bt,
+			    &cur_stats->ports[port].dma_time[i]);
+			if (prev_stats != NULL) {
+				total_bytes -=
+				    prev_stats->ports[port].bytes[i];
+				total_operations -=
+				    prev_stats->ports[port].operations[i];
+				total_dmas -=
+				    prev_stats->ports[port].num_dmas[i];
+				bintime_sub(&total_time_bt,
+				    &prev_stats->ports[port].time[i]);
+				bintime_sub(&total_dma_bt,
+				    &prev_stats->ports[port].dma_time[i]);
+			}
+		}
+	}
+
+	*mbsec = total_bytes;
+	*mbsec /= 1024 * 1024;
+	if (etime > 0.0)
+		*mbsec /= etime;
+	else
+		*mbsec = 0;
+	*kb_per_transfer = total_bytes;
+	*kb_per_transfer /= 1024;
+	if (total_operations > 0)
+		*kb_per_transfer /= total_operations;
+	else
+		*kb_per_transfer = 0;
+	*transfers_per_second = total_operations;
+	*dmas_per_second = total_dmas;
+	if (etime > 0.0) {
+		*transfers_per_second /= etime;
+		*dmas_per_second /= etime;
+	} else {
+		*transfers_per_second = 0;
+		*dmas_per_second = 0;
+	}
+
+	bintime2timespec(&total_time_bt, &total_time_ts);
+	bintime2timespec(&total_dma_bt, &total_dma_ts);
+	if (total_operations > 0) {
+		/*
+		 * Convert the timespec to milliseconds.
+		 */
+		*ms_per_transfer = total_time_ts.tv_sec * 1000;
+		*ms_per_transfer += total_time_ts.tv_nsec / 1000000;
+		*ms_per_transfer /= total_operations;
+	} else
+		*ms_per_transfer = 0;
+
+	if (total_dmas > 0) {
+		/*
+		 * Convert the timespec to milliseconds.
+		 */
+		*ms_per_dma = total_dma_ts.tv_sec * 1000;
+		*ms_per_dma += total_dma_ts.tv_nsec / 1000000;
+		*ms_per_dma /= total_dmas;
+	} else
+		*ms_per_dma = 0;
+}
+
+/* The dump_stats() and json_stats() functions perform essentially the same
+ * purpose, but dump the statistics in different formats.  JSON is more
+ * conducive to programming, however.
+ */
+
+#define	PRINT_BINTIME(prefix, bt) \
+	printf("%s %jd s %ju frac\n", prefix, (intmax_t)(bt).sec, \
+	       (uintmax_t)(bt).frac)
+const char *iotypes[] = {"NO IO", "READ", "WRITE"};
+
+static void
+ctlstat_dump(struct ctlstat_context *ctx) {
+	int iotype, lun, port;
+	struct ctl_lun_io_stats *stats = ctx->cur_lun_stats;
+
+	for (lun = 0; lun < ctx->num_luns;lun++) {
+		printf("lun %d\n", lun);
+		for (port = 0; port < CTL_MAX_PORTS; port++) {
+			printf(" port %d\n",
+			    stats[lun].ports[port].targ_port);
+			for (iotype = 0; iotype < CTL_STATS_NUM_TYPES;
+			    iotype++) {
+				printf("  io type %d (%s)\n", iotype,
+				    iotypes[iotype]);
+				printf("   bytes %ju\n", (uintmax_t)
+				    stats[lun].ports[port].bytes[iotype]);
+				printf("   operations %ju\n", (uintmax_t)
+				    stats[lun].ports[port].operations[iotype]);
+				PRINT_BINTIME("   io time",
+				    stats[lun].ports[port].time[iotype]);
+				printf("   num dmas %ju\n", (uintmax_t)
+				    stats[lun].ports[port].num_dmas[iotype]);
+				PRINT_BINTIME("   dma time",
+				    stats[lun].ports[port].dma_time[iotype]);
+			}
+		}
+	}
+}
+
+#define	JSON_BINTIME(prefix, bt) \
+	printf("\"%s\":{\"sec\":%jd,\"frac\":%ju},", \
+	    prefix, (intmax_t)(bt).sec, (uintmax_t)(bt).frac)
+
+static void
+ctlstat_json(struct ctlstat_context *ctx) {
+	int iotype, lun, port;
+	struct ctl_lun_io_stats *stats = ctx->cur_lun_stats;
+
+	printf("{\"luns\":[");
+	for (lun = 0; lun < ctx->num_luns; lun++) {
+		printf("{\"ports\":[");
+		for (port = 0; port < CTL_MAX_PORTS;port++) {
+			printf("{\"num\":%d,\"io\":[",
+			    stats[lun].ports[port].targ_port);
+			for (iotype = 0; iotype < CTL_STATS_NUM_TYPES;
+			    iotype++) {
+				printf("{\"type\":\"%s\",", iotypes[iotype]);
+				printf("\"bytes\":%ju,", (uintmax_t)stats[
+				       lun].ports[port].bytes[iotype]);
+				printf("\"operations\":%ju,", (uintmax_t)stats[
+				       lun].ports[port].operations[iotype]);
+				JSON_BINTIME("io time",
+				    stats[lun].ports[port].time[iotype]);
+				JSON_BINTIME("dma time",
+				    stats[lun].ports[port].dma_time[iotype]);
+				printf("\"num dmas\":%ju}", (uintmax_t)
+				    stats[lun].ports[port].num_dmas[iotype]);
+				if (iotype < (CTL_STATS_NUM_TYPES - 1))
+					printf(","); /* continue io array */
+			}
+			printf("]}"); /* close port */
+			if (port < (CTL_MAX_PORTS - 1))
+				printf(","); /* continue port array */
+		}
+		printf("]}"); /* close lun */
+		if (lun < (ctx->num_luns - 1))
+			printf(","); /* continue lun array */
+	}
+	printf("]}"); /* close luns and toplevel */
+}
+
+static void
+ctlstat_standard(struct ctlstat_context *ctx) {
+	long double cur_secs, prev_secs, etime;
+	uint64_t delta_jiffies, delta_idle;
+	uint32_t port;
+	long double cpu_percentage;
+	int i;
+	int j;
+
+	cpu_percentage = 0;
+
+	if (F_CPU(ctx) && (getcpu(&ctx->cur_cpu) != 0))
+		errx(1, "error returned from getcpu()");
+
+	cur_secs = ctx->cur_time.tv_sec + (ctx->cur_time.tv_nsec / 1000000000);
+	prev_secs = ctx->prev_time.tv_sec +
+	     (ctx->prev_time.tv_nsec / 1000000000);
+
+	etime = cur_secs - prev_secs;
+
+	if (F_CPU(ctx)) {
+		ctx->prev_total_jiffies = ctx->cur_total_jiffies;
+		ctx->cur_total_jiffies = ctx->cur_cpu.user +
+		    ctx->cur_cpu.nice + ctx->cur_cpu.system +
+		    ctx->cur_cpu.intr + ctx->cur_cpu.idle;
+		delta_jiffies = ctx->cur_total_jiffies;
+		if (F_FIRST(ctx) == 0)
+			delta_jiffies -= ctx->prev_total_jiffies;
+		ctx->prev_idle = ctx->cur_idle;
+		ctx->cur_idle = ctx->cur_cpu.idle;
+		delta_idle = ctx->cur_idle - ctx->prev_idle;
+
+		cpu_percentage = delta_jiffies - delta_idle;
+		cpu_percentage /= delta_jiffies;
+		cpu_percentage *= 100;
+	}
+
+	if (F_HDR(ctx)) {
+		ctx->header_interval--;
+		if (ctx->header_interval <= 0) {
+			int hdr_devs;
+
+			hdr_devs = 0;
+
+			if (F_TOTALS(ctx)) {
+				fprintf(stdout, "%s     System Read     %s"
+					"System Write     %sSystem Total%s\n",
+					(F_LUNVAL(ctx) != 0) ? "     " : "",
+					(F_LUNVAL(ctx) != 0) ? "     " : "",
+					(F_LUNVAL(ctx) != 0) ? "     " : "",
+					(F_CPU(ctx) == 0)   ? "    CPU" : "");
+				hdr_devs = 3;
+			} else {
+				if (F_CPU(ctx))
+					fprintf(stdout, "  CPU  ");
+				for (i = 0; i < min(CTL_STAT_LUN_BITS,
+				     ctx->num_luns); i++) {
+					int lun;
+
+					/*
+					 * Obviously this won't work with
+					 * LUN numbers greater than a signed
+					 * integer.
+					 */
+					lun = (int)ctx->cur_lun_stats[i
+						].lun_number;
+
+					if (bit_test(ctx->lun_mask, lun) == 0)
+						continue;
+					fprintf(stdout, "%15.6s%d ",
+						"lun", lun);
+					hdr_devs++;
+				}
+				fprintf(stdout, "\n");
+			}
+			for (i = 0; i < hdr_devs; i++)
+				fprintf(stdout, "%s  %sKB/t %s  MB/s ",
+					((F_CPU(ctx) != 0) && (i == 0) &&
+					(F_TOTALS(ctx) == 0)) ?  "       " : "",
+					(F_LUNVAL(ctx) != 0) ? " ms  " : "",
+					(F_DMA(ctx) == 0) ? "tps" : "dps");
+			fprintf(stdout, "\n");
+			ctx->header_interval = 20;
+		}
+	}
+
+	if (F_TOTALS(ctx) != 0) {
+		long double mbsec[3];
+		long double kb_per_transfer[3];
+		long double transfers_per_sec[3];
+		long double ms_per_transfer[3];
+		long double ms_per_dma[3];
+		long double dmas_per_sec[3];
+
+		for (i = 0; i < 3; i++) 
+			ctx->prev_total_stats[i] = ctx->cur_total_stats[i];
+
+		memset(&ctx->cur_total_stats, 0, sizeof(ctx->cur_total_stats));
+
+		/* Use macros to make the next loop more readable. */
+#define	ADD_STATS_BYTES(st, p, i, j) \
+	ctx->cur_total_stats[st].ports[p].bytes[j] += \
+	    ctx->cur_lun_stats[i].ports[p].bytes[j]
+#define	ADD_STATS_OPERATIONS(st, p, i, j) \
+	ctx->cur_total_stats[st].ports[p].operations[j] += \
+	    ctx->cur_lun_stats[i].ports[p].operations[j]
+#define	ADD_STATS_NUM_DMAS(st, p, i, j) \
+	ctx->cur_total_stats[st].ports[p].num_dmas[j] += \
+	    ctx->cur_lun_stats[i].ports[p].num_dmas[j]
+#define	ADD_STATS_TIME(st, p, i, j) \
+	bintime_add(&ctx->cur_total_stats[st].ports[p].time[j], \
+	    &ctx->cur_lun_stats[i].ports[p].time[j])
+#define	ADD_STATS_DMA_TIME(st, p, i, j) \
+	bintime_add(&ctx->cur_total_stats[st].ports[p].dma_time[j], \
+	    &ctx->cur_lun_stats[i].ports[p].dma_time[j])
+
+		for (i = 0; i < ctx->num_luns; i++) {
+			for (port = 0; port < CTL_MAX_PORTS; port++) {
+				for (j = 0; j < CTL_STATS_NUM_TYPES; j++) {
+					ADD_STATS_BYTES(2, port, i, j);
+					ADD_STATS_OPERATIONS(2, port, i, j);
+					ADD_STATS_NUM_DMAS(2, port, i, j);
+					ADD_STATS_TIME(2, port, i, j);
+					ADD_STATS_DMA_TIME(2, port, i, j);
+				}
+				ADD_STATS_BYTES(0, port, i, CTL_STATS_READ);
+				ADD_STATS_OPERATIONS(0, port, i,
+				    CTL_STATS_READ);
+				ADD_STATS_NUM_DMAS(0, port, i, CTL_STATS_READ);
+				ADD_STATS_TIME(0, port, i, CTL_STATS_READ);
+				ADD_STATS_DMA_TIME(0, port, i, CTL_STATS_READ);
+
+				ADD_STATS_BYTES(1, port, i, CTL_STATS_WRITE);
+				ADD_STATS_OPERATIONS(1, port, i,
+				    CTL_STATS_WRITE);
+				ADD_STATS_NUM_DMAS(1, port, i, CTL_STATS_WRITE);
+				ADD_STATS_TIME(1, port, i, CTL_STATS_WRITE);
+				ADD_STATS_DMA_TIME(1, port, i, CTL_STATS_WRITE);
+			}
+		}
+
+		for (i = 0; i < 3; i++) {
+			compute_stats(&ctx->cur_total_stats[i],
+				F_FIRST(ctx) ? NULL : &ctx->prev_total_stats[i],
+				etime, &mbsec[i], &kb_per_transfer[i],
+				&transfers_per_sec[i],
+				&ms_per_transfer[i], &ms_per_dma[i],
+				&dmas_per_sec[i]);
+			if (F_DMA(ctx) != 0)
+				fprintf(stdout, " %2.2Lf",
+					ms_per_dma[i]);
+			else if (F_LUNVAL(ctx) != 0)
+				fprintf(stdout, " %2.2Lf",
+					ms_per_transfer[i]);
+			fprintf(stdout, " %5.2Lf %3.0Lf %5.2Lf ",
+				kb_per_transfer[i],
+				(F_DMA(ctx) == 0) ? transfers_per_sec[i] :
+				dmas_per_sec[i], mbsec[i]);
+		}
+		if (F_CPU(ctx))
+			fprintf(stdout, " %5.1Lf%%", cpu_percentage);
+	} else {
+		if (F_CPU(ctx))
+			fprintf(stdout, "%5.1Lf%% ", cpu_percentage);
+
+		for (i = 0; i < min(CTL_STAT_LUN_BITS, ctx->num_luns); i++) {
+			long double mbsec, kb_per_transfer;
+			long double transfers_per_sec;
+			long double ms_per_transfer;
+			long double ms_per_dma;
+			long double dmas_per_sec;
+
+			if (bit_test(ctx->lun_mask,
+			    (int)ctx->cur_lun_stats[i].lun_number) == 0)
+				continue;
+			compute_stats(&ctx->cur_lun_stats[i], F_FIRST(ctx) ?
+				NULL : &ctx->prev_lun_stats[i], etime,
+				&mbsec, &kb_per_transfer,
+				&transfers_per_sec, &ms_per_transfer,
+				&ms_per_dma, &dmas_per_sec);
+			if (F_DMA(ctx))
+				fprintf(stdout, " %2.2Lf",
+					ms_per_dma);
+			else if (F_LUNVAL(ctx) != 0)
+				fprintf(stdout, " %2.2Lf",
+					ms_per_transfer);
+			fprintf(stdout, " %5.2Lf %3.0Lf %5.2Lf ",
+				kb_per_transfer, (F_DMA(ctx) == 0) ?
+				transfers_per_sec : dmas_per_sec, mbsec);
+		}
+	}
+}
+
+int
+main(int argc, char **argv)
+{
+	int c;
+	int count, waittime;
+	int set_lun;
+	int fd, retval;
+	struct ctlstat_context ctx;
+
+	/* default values */
+	retval = 0;
+	waittime = 1;
+	count = -1;
+	memset(&ctx, 0, sizeof(ctx));
+	ctx.numdevs = 3;
+	ctx.mode = CTLSTAT_MODE_STANDARD;
+	ctx.flags |= CTLSTAT_FLAG_CPU;
+	ctx.flags |= CTLSTAT_FLAG_FIRST_RUN;
+	ctx.flags |= CTLSTAT_FLAG_HEADER;
+
+	while ((c = getopt(argc, argv, ctlstat_opts)) != -1) {
+		switch (c) {
+		case 'C':
+			ctx.flags &= ~CTLSTAT_FLAG_CPU;
+			break;
+		case 'c':
+			count = atoi(optarg);
+			break;
+		case 'd':
+			ctx.flags |= CTLSTAT_FLAG_DMA_TIME;
+			break;
+		case 'D':
+			ctx.mode = CTLSTAT_MODE_DUMP;
+			waittime = 30;
+			break;
+		case 'h':
+			ctx.flags &= ~CTLSTAT_FLAG_HEADER;
+			break;
+		case 'j':
+			ctx.mode = CTLSTAT_MODE_JSON;
+			waittime = 30;
+			break;
+		case 'l': {
+			int cur_lun;
+
+			cur_lun = atoi(optarg);
+			if (cur_lun > CTL_STAT_LUN_BITS)
+				errx(1, "Invalid LUN number %d", cur_lun);
+
+			bit_ffs(ctx.lun_mask, CTL_STAT_LUN_BITS, &set_lun);
+			if (set_lun == -1)
+				ctx.numdevs = 1;
+			else
+				ctx.numdevs++;
+			bit_set(ctx.lun_mask, cur_lun);
+			break;
+		}
+		case 'n':
+			ctx.numdevs = atoi(optarg);
+			break;
+		case 't':
+			ctx.flags |= CTLSTAT_FLAG_TOTALS;
+			ctx.numdevs = 3;
+			break;
+		case 'w':
+			waittime = atoi(optarg);
+			break;
+		default:
+			retval = 1;
+			usage(retval);
+			exit(retval);
+			break;
+		}
+	}
+
+	bit_ffs(ctx.lun_mask, CTL_STAT_LUN_BITS, &set_lun);
+
+	if ((F_TOTALS(&ctx))
+	 && (set_lun != -1)) {
+		errx(1, "Total Mode (-t) is incompatible with individual "
+		     "LUN mode (-l)");
+	} else if (set_lun == -1) {
+		/*
+		 * Note that this just selects the first N LUNs to display,
+		 * but at this point we have no knoweledge of which LUN
+		 * numbers actually exist.  So we may select LUNs that
+		 * aren't there.
+		 */
+		bit_nset(ctx.lun_mask, 0, min(ctx.numdevs - 1,
+			 CTL_STAT_LUN_BITS - 1));
+	}
+
+	if ((fd = open(CTL_DEFAULT_DEV, O_RDWR)) == -1)
+		err(1, "cannot open %s", CTL_DEFAULT_DEV);
+
+	for (;count != 0;) {
+		ctx.tmp_lun_stats = ctx.prev_lun_stats;
+		ctx.prev_lun_stats = ctx.cur_lun_stats;
+		ctx.cur_lun_stats = ctx.tmp_lun_stats;
+		ctx.prev_time = ctx.cur_time;
+		ctx.prev_cpu = ctx.cur_cpu;
+		if (getstats(fd, &ctx.num_luns, &ctx.cur_lun_stats,
+			     &ctx.cur_time, &ctx.flags) != 0)
+			errx(1, "error returned from getstats()");
+
+		switch(ctx.mode) {
+		case CTLSTAT_MODE_STANDARD:
+			ctlstat_standard(&ctx);
+			break;
+		case CTLSTAT_MODE_DUMP:
+			ctlstat_dump(&ctx);
+			break;
+		case CTLSTAT_MODE_JSON:
+			ctlstat_json(&ctx);
+			break;
+		default:
+			break;
+		}
+
+		fprintf(stdout, "\n");
+		ctx.flags &= ~CTLSTAT_FLAG_FIRST_RUN;
+		if (count != 1)
+			sleep(waittime);
+		if (count > 0)
+			count--;
+	}
+
+	exit (retval);
+}
+
+/*
+ * vim: ts=8
+ */
diff -r ebe87ce4d7dd -r 1750de5b0399 head/usr.bin/hexdump/parse.c
--- a/head/usr.bin/hexdump/parse.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/usr.bin/hexdump/parse.c	Fri Jan 13 15:51:01 2012 +0200
@@ -33,7 +33,7 @@
 #endif
 #endif /* not lint */
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/usr.bin/hexdump/parse.c 229403 2012-01-03 18:51:58Z ed $");
+__FBSDID("$FreeBSD: head/usr.bin/hexdump/parse.c 229794 2012-01-07 23:15:21Z eadler $");
 
 #include <sys/types.h>
 
@@ -255,7 +255,9 @@
 					sokay = NOTOKAY;
 			}
 
-			p2 = p1 + 1;		/* Set end pointer. */
+			p2 = *p1 ? p1 + 1 : p1;	/* Set end pointer -- make sure
+						 * that it's non-NUL/-NULL first
+						 * though. */
 			cs[0] = *p1;		/* Set conversion string. */
 			cs[1] = '\0';
 
@@ -449,13 +451,21 @@
 	char *p2;
 
 	/* alphabetic escape sequences have to be done in place */
-	for (p2 = p1;; ++p1, ++p2) {
-		if (!*p1) {
-			*p2 = *p1;
-			break;
-		}
-		if (*p1 == '\\')
-			switch(*++p1) {
+	for (p2 = p1; *p1; p1++, p2++) {
+		/* 
+		 * Let's take a peak at the next item and see whether or not
+		 * we need to escape the value...
+		 */
+		if (*p1 == '\\') {
+
+			p1++;
+
+			switch(*p1) {
+			/* A standalone `\' */
+			case '\0':
+				*p2 = '\\';
+				*++p2 = '\0';
+				break;
 			case 'a':
 			     /* *p2 = '\a'; */
 				*p2 = '\007';
@@ -482,7 +492,12 @@
 				*p2 = *p1;
 				break;
 			}
+
+		} else
+			*p2 = *p1;
+
 	}
+
 }
 
 void
diff -r ebe87ce4d7dd -r 1750de5b0399 head/usr.bin/locate/locate/util.c
--- a/head/usr.bin/locate/locate/util.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/usr.bin/locate/locate/util.c	Fri Jan 13 15:51:01 2012 +0200
@@ -34,7 +34,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $FreeBSD: head/usr.bin/locate/locate/util.c 229403 2012-01-03 18:51:58Z ed $
+ * $FreeBSD: head/usr.bin/locate/locate/util.c 229655 2012-01-05 21:36:45Z uqs $
  */
 
 
@@ -219,15 +219,15 @@
 
 /*
  * Read integer from mmap pointer.
- * Essential a simple  ``return *(int *)p'' but avoid sigbus
+ * Essentially a simple ``return *(int *)p'' but avoids sigbus
  * for integer alignment (SunOS 4.x, 5.x).
  *
  * Convert network byte order to host byte order if necessary.
- * So we can read on FreeBSD/i386 (little endian) a locate database
+ * So we can read a locate database on FreeBSD/i386 (little endian)
  * which was built on SunOS/sparc (big endian).
  */
 
-int 
+int
 getwm(p)
 	caddr_t p;
 {
diff -r ebe87ce4d7dd -r 1750de5b0399 head/usr.bin/mail/main.c
--- a/head/usr.bin/mail/main.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/usr.bin/mail/main.c	Fri Jan 13 15:51:01 2012 +0200
@@ -39,7 +39,7 @@
 #endif
 #endif /* not lint */
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/usr.bin/mail/main.c 228992 2011-12-30 11:02:40Z uqs $");
+__FBSDID("$FreeBSD: head/usr.bin/mail/main.c 229655 2012-01-05 21:36:45Z uqs $");
 
 #include "rcv.h"
 #include <fcntl.h>
@@ -259,7 +259,7 @@
 		if (ef == NULL)
 			ef = "%";
 		if (setfile(ef) <= 0)
-			/* Either an error has occurted, or no mail */
+			/* Either an error has occurred, or no mail */
 			exit(1);
 		else
 			exit(0);
diff -r ebe87ce4d7dd -r 1750de5b0399 head/usr.bin/mail/util.c
--- a/head/usr.bin/mail/util.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/usr.bin/mail/util.c	Fri Jan 13 15:51:01 2012 +0200
@@ -33,7 +33,7 @@
 #endif
 #endif /* not lint */
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/usr.bin/mail/util.c 228992 2011-12-30 11:02:40Z uqs $");
+__FBSDID("$FreeBSD: head/usr.bin/mail/util.c 229655 2012-01-05 21:36:45Z uqs $");
 
 #include <sys/time.h>
 
@@ -550,7 +550,7 @@
 }
 
 /*
- * Count the occurrances of c in str
+ * Count the occurrences of c in str
  */
 int
 charcount(char *str, int c)
diff -r ebe87ce4d7dd -r 1750de5b0399 head/usr.bin/ncplist/ncplist.c
--- a/head/usr.bin/ncplist/ncplist.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/usr.bin/ncplist/ncplist.c	Fri Jan 13 15:51:01 2012 +0200
@@ -32,7 +32,7 @@
 
 #include <sys/cdefs.h>
 
-__FBSDID("$FreeBSD: head/usr.bin/ncplist/ncplist.c 226429 2011-10-16 10:00:28Z ed $");
+__FBSDID("$FreeBSD: head/usr.bin/ncplist/ncplist.c 229907 2012-01-10 02:58:20Z eadler $");
 
 #include <sys/param.h>
 #include <sys/time.h>
@@ -375,7 +375,7 @@
 int
 main(int argc, char *argv[])
 {
-	int opt, wdone = 0, nargs = 0, i;
+	int opt, nargs = 0, i;
 	enum listop what;
 	char *args[MAX_ARGS];
 
@@ -438,23 +438,18 @@
 	switch(what) {
 	    case LO_SERVERS:
 		show_serverlist(args[0]);
-		wdone = 1;
 		break;
 	    case LO_USERS:
 		show_userlist(args[0]);
-		wdone = 1;
 		break;
 	    case LO_QUEUES:
 		show_queuelist(args[0], args[1]);
-		wdone = 1;
 		break;
 	    case LO_VOLUMES:
 		list_volumes(args[0]);
-		wdone = 1;
 		break;
 	    case LO_BINDERY:
 		list_bindery(args[0], args[1], args[2]);
-		wdone = 1;
 		break;
 	    default:
 		help();
diff -r ebe87ce4d7dd -r 1750de5b0399 head/usr.bin/netstat/if.c
--- a/head/usr.bin/netstat/if.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/usr.bin/netstat/if.c	Fri Jan 13 15:51:01 2012 +0200
@@ -34,7 +34,7 @@
 #endif
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/usr.bin/netstat/if.c 229403 2012-01-03 18:51:58Z ed $");
+__FBSDID("$FreeBSD: head/usr.bin/netstat/if.c 229909 2012-01-10 02:58:36Z eadler $");
 
 #include <sys/types.h>
 #include <sys/protosw.h>
@@ -188,7 +188,6 @@
 	} ifaddr;
 	u_long ifaddraddr;
 	u_long ifaddrfound;
-	u_long ifnetfound;
 	u_long opackets;
 	u_long ipackets;
 	u_long obytes;
@@ -249,7 +248,6 @@
 		link_layer = 0;
 
 		if (ifaddraddr == 0) {
-			ifnetfound = ifnetaddr;
 			if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet) != 0)
 				return;
 			strlcpy(name, ifnet.if_xname, sizeof(name));
diff -r ebe87ce4d7dd -r 1750de5b0399 head/usr.bin/newgrp/newgrp.c
--- a/head/usr.bin/newgrp/newgrp.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/usr.bin/newgrp/newgrp.c	Fri Jan 13 15:51:01 2012 +0200
@@ -29,7 +29,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/usr.bin/newgrp/newgrp.c 226416 2011-10-16 07:05:43Z delphij $");
+__FBSDID("$FreeBSD: head/usr.bin/newgrp/newgrp.c 229668 2012-01-05 23:08:11Z ghelmer $");
 
 #include <sys/types.h>
 
@@ -190,7 +190,7 @@
 		err(1, "malloc");
 	if ((ngrps = getgroups(ngrps_max, (gid_t *)grps)) < 0) {
 		warn("getgroups");
-		return;
+		goto end;
 	}
 
 	/* Remove requested gid from supp. list if it exists. */
@@ -204,7 +204,7 @@
 		if (setgroups(ngrps, (const gid_t *)grps) < 0) {
 			PRIV_END;
 			warn("setgroups");
-			return;
+			goto end;
 		}
 		PRIV_END;
 	}
@@ -213,7 +213,7 @@
 	if (setgid(grp->gr_gid)) {
 		PRIV_END;
 		warn("setgid");
-		return;
+		goto end;
 	}
 	PRIV_END;
 	grps[0] = grp->gr_gid;
@@ -228,12 +228,12 @@
 			if (setgroups(ngrps, (const gid_t *)grps)) {
 				PRIV_END;
 				warn("setgroups");
-				return;
+				goto end;
 			}
 			PRIV_END;
 		}
 	}
-
+end:
 	free(grps);
 }
 
diff -r ebe87ce4d7dd -r 1750de5b0399 head/usr.bin/split/split.c
--- a/head/usr.bin/split/split.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/usr.bin/split/split.c	Fri Jan 13 15:51:01 2012 +0200
@@ -28,7 +28,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/usr.bin/split/split.c 227183 2011-11-06 08:17:11Z ed $");
+__FBSDID("$FreeBSD: head/usr.bin/split/split.c 229905 2012-01-10 02:55:43Z eadler $");
 
 #ifndef lint
 static const char copyright[] =
@@ -347,17 +347,14 @@
 {
 	long i, maxfiles, tfnum;
 	static long fnum;
-	static int defname;
 	static char *fpnt;
 
 	if (ofd == -1) {
 		if (fname[0] == '\0') {
 			fname[0] = 'x';
 			fpnt = fname + 1;
-			defname = 1;
 		} else {
 			fpnt = fname + strlen(fname);
-			defname = 0;
 		}
 		ofd = fileno(stdout);
 	}
diff -r ebe87ce4d7dd -r 1750de5b0399 head/usr.sbin/Makefile
--- a/head/usr.sbin/Makefile	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/usr.sbin/Makefile	Fri Jan 13 15:51:01 2012 +0200
@@ -1,5 +1,5 @@
 #	From: @(#)Makefile	5.20 (Berkeley) 6/12/93
-# $FreeBSD: head/usr.sbin/Makefile 225937 2011-10-03 15:13:09Z nwhitehorn $
+# $FreeBSD: head/usr.sbin/Makefile 229997 2012-01-12 00:34:33Z ken $
 
 .include <bsd.own.mk>
 
@@ -16,6 +16,7 @@
 	clear_locks \
 	crashinfo \
 	cron \
+	ctladm \
 	daemon \
 	dconschat \
 	devinfo \
diff -r ebe87ce4d7dd -r 1750de5b0399 head/usr.sbin/acpi/acpidb/Makefile
--- a/head/usr.sbin/acpi/acpidb/Makefile	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/usr.sbin/acpi/acpidb/Makefile	Fri Jan 13 15:51:01 2012 +0200
@@ -1,4 +1,4 @@
-# $FreeBSD: head/usr.sbin/acpi/acpidb/Makefile 228110 2011-11-28 23:36:48Z jkim $
+# $FreeBSD: head/usr.sbin/acpi/acpidb/Makefile 229989 2012-01-11 23:06:37Z jkim $
 
 PROG=	acpidb
 SRCS=	acpidb.c
@@ -56,10 +56,11 @@
 	tbxfroot.c
 
 # utilities
-SRCS+=	utalloc.c utcache.c utcopy.c utdebug.c utdecode.c	\
-	utdelete.c uteval.c utglobal.c utids.c utinit.c		\
-	utlock.c utmath.c utmisc.c utmutex.c utobject.c utosi.c	\
-	utresrc.c utstate.c uttrack.c utxface.c utxferror.c
+SRCS+=	utaddress.c utalloc.c utcache.c utcopy.c utdebug.c	\
+	utdecode.c utdelete.c uteval.c utglobal.c utids.c	\
+	utinit.c utlock.c utmath.c utmisc.c utmutex.c		\
+	utobject.c utosi.c utresrc.c utstate.c uttrack.c	\
+	utxface.c utxferror.c
 
 MAN=	acpidb.8
 WARNS?=	2
diff -r ebe87ce4d7dd -r 1750de5b0399 head/usr.sbin/acpi/iasl/Makefile
--- a/head/usr.sbin/acpi/iasl/Makefile	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/usr.sbin/acpi/iasl/Makefile	Fri Jan 13 15:51:01 2012 +0200
@@ -1,4 +1,4 @@
-# $FreeBSD: head/usr.sbin/acpi/iasl/Makefile 228110 2011-11-28 23:36:48Z jkim $
+# $FreeBSD: head/usr.sbin/acpi/iasl/Makefile 229989 2012-01-11 23:06:37Z jkim $
 
 PROG=	iasl
 SRCS=	adfile.c adisasm.c adwalk.c
@@ -55,10 +55,10 @@
 SRCS+=	tbfadt.c tbinstal.c tbutils.c tbxface.c
 
 # utilities
-SRCS+=	utalloc.c utcache.c utcopy.c utdebug.c utdecode.c	\
-	utdelete.c utglobal.c utinit.c utlock.c utmath.c	\
-	utmisc.c utmutex.c utobject.c utosi.c utresrc.c		\
-	utstate.c utxface.c utxferror.c
+SRCS+=	utaddress.c utalloc.c utcache.c utcopy.c utdebug.c	\
+	utdecode.c utdelete.c utglobal.c utinit.c utlock.c	\
+	utmath.c utmisc.c utmutex.c utobject.c utosi.c		\
+	utresrc.c utstate.c utxface.c utxferror.c
 
 WARNS?=	2
 
diff -r ebe87ce4d7dd -r 1750de5b0399 head/usr.sbin/bluetooth/sdpd/server.c
--- a/head/usr.sbin/bluetooth/sdpd/server.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/usr.sbin/bluetooth/sdpd/server.c	Fri Jan 13 15:51:01 2012 +0200
@@ -26,7 +26,7 @@
  * SUCH DAMAGE.
  *
  * $Id: server.c,v 1.6 2004/01/13 01:54:39 max Exp $
- * $FreeBSD: head/usr.sbin/bluetooth/sdpd/server.c 228990 2011-12-30 10:58:14Z uqs $
+ * $FreeBSD: head/usr.sbin/bluetooth/sdpd/server.c 229655 2012-01-05 21:36:45Z uqs $
  */
 
 #include <sys/param.h>
@@ -334,7 +334,7 @@
 		 * The minimum L2CAP MTU is 43 bytes. That means we need
 		 * 65536 / 43 = ~1524 chunks to transfer maximum packet
 		 * size with minimum MTU. The "rsp_cs" field in fd_idx_t
-		 * is 11 bit wide that gives us up to 2048 chunks.
+		 * is 11 bits wide, which gives us up to 2048 chunks.
 		 */
 
 		if (omtu < NG_L2CAP_MTU_MINIMUM) {
diff -r ebe87ce4d7dd -r 1750de5b0399 head/usr.sbin/bsnmpd/modules/snmp_wlan/BEGEMOT-WIRELESS-MIB.txt
--- a/head/usr.sbin/bsnmpd/modules/snmp_wlan/BEGEMOT-WIRELESS-MIB.txt	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/usr.sbin/bsnmpd/modules/snmp_wlan/BEGEMOT-WIRELESS-MIB.txt	Fri Jan 13 15:51:01 2012 +0200
@@ -26,7 +26,7 @@
 -- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 -- SUCH DAMAGE.
 --
--- $FreeBSD: head/usr.sbin/bsnmpd/modules/snmp_wlan/BEGEMOT-WIRELESS-MIB.txt 228990 2011-12-30 10:58:14Z uqs $
+-- $FreeBSD: head/usr.sbin/bsnmpd/modules/snmp_wlan/BEGEMOT-WIRELESS-MIB.txt 229653 2012-01-05 21:36:28Z uqs $
 --
 
 BEGEMOT-WIRELESS-MIB DEFINITIONS ::= BEGIN
@@ -82,8 +82,8 @@
 		    associationLeave(8),
 		    associationNotAuthenticated(9),
 -- XXX: TODO - FIXME
-		    disassocPwrcapBad(10),
-		    disassocSuperchanBad(11),
+		    dissassocPwrcapBad(10),
+		    dissassocSuperchanBad(11),
 		    ieInvalid(13),
 		    micFailure(14),
 		    fourWayHandshakeTimeout(15),
diff -r ebe87ce4d7dd -r 1750de5b0399 head/usr.sbin/bsnmpd/modules/snmp_wlan/wlan_tree.def
--- a/head/usr.sbin/bsnmpd/modules/snmp_wlan/wlan_tree.def	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/usr.sbin/bsnmpd/modules/snmp_wlan/wlan_tree.def	Fri Jan 13 15:51:01 2012 +0200
@@ -26,7 +26,7 @@
 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 # SUCH DAMAGE.
 #
-# $FreeBSD: head/usr.sbin/bsnmpd/modules/snmp_wlan/wlan_tree.def 228990 2011-12-30 10:58:14Z uqs $
+# $FreeBSD: head/usr.sbin/bsnmpd/modules/snmp_wlan/wlan_tree.def 229653 2012-01-05 21:36:28Z uqs $
 #
 
 #include "tc.def"
@@ -75,8 +75,8 @@
 	7 notAssociated
 	8 associationLeave
 	9 associationNotAuthenticated
-	10 disassocPwrcapBad
-	11 disassocSuperchanBad
+	10 dissassocPwrcapBad
+	11 dissassocSuperchanBad
 	13 ieInvalid
 	14 micFailure
 	15 fourWayHandshakeTimeout
diff -r ebe87ce4d7dd -r 1750de5b0399 head/usr.sbin/bsnmpd/tools/bsnmptools/bsnmpget.1
--- a/head/usr.sbin/bsnmpd/tools/bsnmptools/bsnmpget.1	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/usr.sbin/bsnmpd/tools/bsnmptools/bsnmpget.1	Fri Jan 13 15:51:01 2012 +0200
@@ -31,9 +31,9 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.\" $FreeBSD: head/usr.sbin/bsnmpd/tools/bsnmptools/bsnmpget.1 228990 2011-12-30 10:58:14Z uqs $
+.\" $FreeBSD: head/usr.sbin/bsnmpd/tools/bsnmptools/bsnmpget.1 229933 2012-01-10 15:29:03Z syrinx $
 .\"
-.Dd September 17, 2007
+.Dd January 10, 2012
 .Dt BSNMPGET 1
 .Os
 .Sh NAME
@@ -112,7 +112,7 @@
 received successfully.
 .Pp
 .Nm Bsnmpwalk
-queries an agent with SMNP GetNextRequest packets,
+queries an agent with ether SMNP GetNextRequest or GetBulkRequest packets,
 asking for values of OID instances that are a part of the object subtree
 rooted at the provided OIDs.
 .Pp
@@ -220,7 +220,7 @@
 transport is used.
 .It Fl M Ar max-repetitions
 The value for the max-repetitions field in a GetBulk PDU.
-Default is 1.
+Default is 10.
 .It Fl N Ar non-repeaters
 The value for the non-repeaters field in a GetBulk PDU.
 Default is 0.
@@ -251,8 +251,17 @@
 By default plain text SNMPv3 PDUs are sent.
 .It Fl p Ar [get|getnext|getbulk]
 The PDU type to send by
-.Nm bsmpget .
-Default is get.
+.Nm bsmpget
+and
+.Nm bsnmpwalk . 
+Default is get
+for
+.Nm bsmpget
+and getnext for
+.Nm bsnmpwalk .
+Getbulk allows executing the so called SNMP "bulkwalks" allowing the values of
+multiple columns to be retrived in a single PDU by
+.Nm bsnmpwalk .
 .It Fl r Ar retries
 Number of resends of request packets before giving up if the agent does
 not respond after the first try.
diff -r ebe87ce4d7dd -r 1750de5b0399 head/usr.sbin/bsnmpd/tools/bsnmptools/bsnmpget.c
--- a/head/usr.sbin/bsnmpd/tools/bsnmptools/bsnmpget.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/usr.sbin/bsnmpd/tools/bsnmptools/bsnmpget.c	Fri Jan 13 15:51:01 2012 +0200
@@ -29,7 +29,7 @@
  * Bsnmpget and bsnmpwalk are simple tools for querying SNMP agents,
  * bsnmpset can be used to set MIB objects in an agent.
  *
- * $FreeBSD: head/usr.sbin/bsnmpd/tools/bsnmptools/bsnmpget.c 228990 2011-12-30 10:58:14Z uqs $
+ * $FreeBSD: head/usr.sbin/bsnmpd/tools/bsnmptools/bsnmpget.c 229933 2012-01-10 15:29:03Z syrinx $
  */
 
 #include <sys/queue.h>
@@ -76,8 +76,9 @@
 	    (program == BSNMPWALK) ? "[-dhnK]" :
 	    (program == BSNMPSET) ? "[-adehnK]" :
 	    "",
-	(program == BSNMPGET) ? " [-M max-repetitions] [-N non-repeaters]" : "",
-	(program == BSNMPGET) ? "[-p pdu] " : "",
+	(program == BSNMPGET || program == BSNMPWALK) ?
+	" [-M max-repetitions] [-N non-repeaters]" : "",
+	(program == BSNMPGET || program == BSNMPWALK) ? "[-p pdu] " : "",
 	(program == BSNMPGET) ? " OID [OID ...]" :
 	    (program == BSNMPWALK || program == BSNMPSET) ? " [OID ...]" :
 	    ""
@@ -150,7 +151,7 @@
 
 	switch (program) {
 		case BSNMPWALK:
-			opts = "dhnKA:b:C:I:i:l:o:P:r:s:t:U:v:";
+			opts = "dhnKA:b:C:I:i:l:M:N:o:P:p:r:s:t:U:v:";
 			break;
 		case BSNMPGET:
 			opts = "aDdehnKA:b:C:I:i:l:M:N:o:P:p:r:s:t:U:v:";
@@ -398,7 +399,7 @@
 		}
 
 		if (snmp_parse_resp(&resp, &req) >= 0) {
-			snmp_output_resp(snmptoolctx, &resp);
+			snmp_output_resp(snmptoolctx, &resp, NULL);
 			break;
 		}
 
@@ -460,8 +461,14 @@
 	struct snmp_pdu req, resp;
 	struct asn_oid root;	/* Keep the initial oid. */
 	int32_t outputs, rc;
+	uint32_t op;
 
-	snmp_pdu_create(&req, SNMP_PDU_GETNEXT);
+	if (GET_PDUTYPE(snmptoolctx) == SNMP_PDU_GETBULK)
+		op = SNMP_PDU_GETBULK;
+	else
+		op = SNMP_PDU_GETNEXT;
+
+	snmp_pdu_create(&req, op);
 
 	while ((rc = snmp_pdu_add_bindings(snmptoolctx, NULL,
 	    snmptool_add_vbind, &req, 1)) > 0) {
@@ -470,6 +477,10 @@
 		memset(&root, 0, sizeof(struct asn_oid));
 		asn_append_oid(&root, &(req.bindings[0].var));
 
+		if (op == SNMP_PDU_GETBULK)
+			snmpget_fix_getbulk(&req, GET_MAXREP(snmptoolctx),
+			    GET_NONREP(snmptoolctx));
+
 		outputs = 0;
 		while (snmp_dialog(&req, &resp) >= 0) {
 			if ((snmp_parse_resp(&resp, &req)) < 0) {
@@ -479,21 +490,24 @@
 				break;
 			}
 
-			if (!(asn_is_suboid(&root, &(resp.bindings[0].var)))) {
-				snmp_pdu_free(&resp);
-				break;
-			}
-
-			if (snmp_output_resp(snmptoolctx, &resp)!= 0) {
+			rc = snmp_output_resp(snmptoolctx, &resp, &root);
+			if (rc < 0) {
 				snmp_pdu_free(&resp);
 				outputs = -1;
 				break;
 			}
-			outputs++;
+
+			outputs += rc;
 			snmp_pdu_free(&resp);
 
-			snmpwalk_nextpdu_create(SNMP_PDU_GETNEXT,
-			    &(resp.bindings[0].var), &req);
+			if (rc < resp.nbindings)
+				break;
+
+			snmpwalk_nextpdu_create(op,
+			    &(resp.bindings[resp.nbindings - 1].var), &req);
+			if (op == SNMP_PDU_GETBULK)
+				snmpget_fix_getbulk(&req, GET_MAXREP(snmptoolctx),
+				    GET_NONREP(snmptoolctx));
 		}
 
 		/* Just in case our root was a leaf. */
@@ -503,7 +517,7 @@
 				if (snmp_parse_resp(&resp,&req) < 0)
 					snmp_output_err_resp(snmptoolctx, &resp);
 				else
-					snmp_output_resp(snmptoolctx, &(resp));
+					snmp_output_resp(snmptoolctx, &(resp), NULL);
 
 				snmp_pdu_free(&resp);
 			} else
@@ -515,7 +529,7 @@
 			break;
 		}
 
-		snmp_pdu_create(&req, SNMP_PDU_GETNEXT);
+		snmp_pdu_create(&req, op);
 	}
 
 	if (rc == 0)
@@ -1076,7 +1090,7 @@
 
 		if (snmp_pdu_check(&req, &resp) > 0) {
 			if (GET_OUTPUT(snmptoolctx) != OUTPUT_QUIET)
-				snmp_output_resp(snmptoolctx, &resp);
+				snmp_output_resp(snmptoolctx, &resp, NULL);
 			break;
 		}
 
diff -r ebe87ce4d7dd -r 1750de5b0399 head/usr.sbin/bsnmpd/tools/libbsnmptools/bsnmptools.c
--- a/head/usr.sbin/bsnmpd/tools/libbsnmptools/bsnmptools.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/usr.sbin/bsnmpd/tools/libbsnmptools/bsnmptools.c	Fri Jan 13 15:51:01 2012 +0200
@@ -28,7 +28,7 @@
  *
  * Helper functions for snmp client tools
  *
- * $FreeBSD: head/usr.sbin/bsnmpd/tools/libbsnmptools/bsnmptools.c 228990 2011-12-30 10:58:14Z uqs $
+ * $FreeBSD: head/usr.sbin/bsnmpd/tools/libbsnmptools/bsnmptools.c 229933 2012-01-10 15:29:03Z syrinx $
  */
 
 #include <sys/param.h> 
@@ -132,6 +132,7 @@
 	snmptoolctx->flags = SNMP_PDU_GET;	/* XXX */
 	SLIST_INIT(&snmptoolctx->filelist);
 	snmp_client_init(&snmp_client);
+	SET_MAXREP(snmptoolctx, SNMP_MAX_REPETITIONS);
 
 	if (add_filename(snmptoolctx, bsnmpd_defs, &IsoOrgDod_OID, 0) < 0)
 		warnx("Error adding file %s to list", bsnmpd_defs);
@@ -2039,14 +2040,20 @@
 }
 
 int32_t
-snmp_output_resp(struct snmp_toolinfo *snmptoolctx, struct snmp_pdu *pdu)
+snmp_output_resp(struct snmp_toolinfo *snmptoolctx, struct snmp_pdu *pdu,
+    struct asn_oid *root)
 {
 	int32_t error;
 	char p[ASN_OIDSTRLEN];
 	uint32_t i;
 	struct snmp_object object;
 
-	for (i = 0, error = 0; i < pdu->nbindings; i++) {
+	i = error = 0;
+	while (i < pdu->nbindings) {
+		if (root != NULL && !(asn_is_suboid(root,
+		    &(pdu->bindings[i].var))))
+			break;
+
 		if (GET_OUTPUT(snmptoolctx) != OUTPUT_QUIET) {
 			if (!ISSET_NUMERIC(snmptoolctx) &&
 			    (snmp_fill_object(snmptoolctx, &object,
@@ -2058,9 +2065,13 @@
 			}
 		}
 		error |= snmp_output_numval(snmptoolctx, &(pdu->bindings[i]), object.info);
+		i++;
 	}
 
-	return (error);
+	if (error)
+		return (-1);
+
+	return (i);
 }
 
 void
diff -r ebe87ce4d7dd -r 1750de5b0399 head/usr.sbin/bsnmpd/tools/libbsnmptools/bsnmptools.h
--- a/head/usr.sbin/bsnmpd/tools/libbsnmptools/bsnmptools.h	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/usr.sbin/bsnmpd/tools/libbsnmptools/bsnmptools.h	Fri Jan 13 15:51:01 2012 +0200
@@ -28,7 +28,7 @@
  *
  * Helper functions common for all tools.
  *
- * $FreeBSD: head/usr.sbin/bsnmpd/tools/libbsnmptools/bsnmptools.h 228990 2011-12-30 10:58:14Z uqs $
+ * $FreeBSD: head/usr.sbin/bsnmpd/tools/libbsnmptools/bsnmptools.h 229933 2012-01-10 15:29:03Z syrinx $
  */
 
 #ifndef	_BSNMP_TOOLS_H_
@@ -47,6 +47,8 @@
 #define	SNMP_DEFS_DIR		"/usr/share/snmp/defs/"
 #define	SNMP_DEFAULT_LOCAL	"/var/run/snmpd.sock"
 
+#define	SNMP_MAX_REPETITIONS	10
+
 enum snmp_access {
 	SNMP_ACCESS_NONE = 0,
 	SNMP_ACCESS_GET,
@@ -323,7 +325,7 @@
 int32_t snmp_output_numval(struct snmp_toolinfo *, struct snmp_value *,
     struct snmp_oid2str *);
 void snmp_output_val(struct snmp_value *);
-int32_t snmp_output_resp(struct snmp_toolinfo *, struct snmp_pdu *);
+int32_t snmp_output_resp(struct snmp_toolinfo *, struct snmp_pdu *, struct asn_oid *);
 void snmp_output_err_resp(struct snmp_toolinfo *, struct snmp_pdu *);
 void snmp_output_engine(void);
 void snmp_output_keys(void);
diff -r ebe87ce4d7dd -r 1750de5b0399 head/usr.sbin/ctladm/Makefile
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/usr.sbin/ctladm/Makefile	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,21 @@
+# $FreeBSD: head/usr.sbin/ctladm/Makefile 229997 2012-01-12 00:34:33Z ken $
+
+PROG=		ctladm
+SRCS=		ctladm.c util.c ctl_util.c ctl_scsi_all.c
+.PATH:		${.CURDIR}/../../sys/cam/ctl 
+SDIR=		${.CURDIR}/../../sys
+CFLAGS+=	-I${SDIR}
+# This is necessary because of these warnings:
+# warning: cast increases required alignment of target type
+# The solution is to either upgrade the compiler (preferred), or do void 
+# pointer gymnastics to get around the warning.  For now, disable the
+# warning instead of doing the void pointer workaround.
+.if ${MACHINE_CPUARCH} == "arm"
+WARNS?= 3
+.endif
+
+DPADD=		${LIBCAM} ${LIBSBUF}
+LDADD=		-lcam -lsbuf -lbsdxml
+MAN=		ctladm.8
+
+.include <bsd.prog.mk>
diff -r ebe87ce4d7dd -r 1750de5b0399 head/usr.sbin/ctladm/ctladm.8
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/usr.sbin/ctladm/ctladm.8	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,963 @@
+.\" 
+.\" Copyright (c) 2003 Silicon Graphics International Corp.
+.\" All rights reserved.
+.\" 
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\"    notice, this list of conditions, and the following disclaimer,
+.\"    without modification.
+.\" 2. Redistributions in binary form must reproduce at minimum a disclaimer
+.\"    substantially similar to the "NO WARRANTY" disclaimer below
+.\"    ("Disclaimer") and any redistribution must be conditioned upon
+.\"    including a substantially similar Disclaimer requirement for further
+.\"    binary redistribution.
+.\" 
+.\" NO WARRANTY
+.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+.\" "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+.\" LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
+.\" A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+.\" HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+.\" IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+.\" POSSIBILITY OF SUCH DAMAGES.
+.\" 
+.\" ctladm utility man page.
+.\"
+.\" Author: Ken Merry <ken at FreeBSD.org>
+.\"
+.\" $Id: //depot/users/kenm/FreeBSD-test2/usr.sbin/ctladm/ctladm.8#3 $
+.\" $FreeBSD: head/usr.sbin/ctladm/ctladm.8 229997 2012-01-12 00:34:33Z ken $
+.\"
+.Dd July 8, 2011
+.Dt CTLADM 8
+.Os
+.Sh NAME
+.Nm ctladm
+.Nd CAM Target Layer control utility
+.Sh SYNOPSIS
+.Nm
+.Aq Ar command
+.Op target:lun
+.Op generic args
+.Op command args
+.Nm
+.Ic tur
+.Aq target:lun
+.Op general options
+.Nm
+.Ic inquiry
+.Aq target:lun
+.Op general options
+.Nm
+.Ic reqsense
+.Aq target:lun
+.Op general options
+.Nm
+.Ic reportluns
+.Aq target:lun
+.Op general options
+.Nm
+.Ic read
+.Aq target:lun
+.Op general options
+.Aq Fl l Ar lba
+.Aq Fl d Ar datalen
+.Aq Fl f Ar file|-
+.Aq Fl b Ar blocksize_bytes
+.Op Fl c Ar cdbsize
+.Op Fl N
+.Nm
+.Ic write
+.Aq target:lun
+.Op general options
+.Aq Fl l Ar lba
+.Aq Fl d Ar datalen
+.Aq Fl f Ar file|-
+.Aq Fl b Ar blocksize_bytes
+.Op Fl c Ar cdbsize
+.Op Fl N
+.Nm
+.Ic bbrread
+.Aq target:lun
+.Op general options
+.Aq Fl -l Ar lba
+.Aq Fl -d Ar datalen
+.Nm
+.Ic readcap
+.Aq target:lun
+.Op general options
+.Op Fl c Ar cdbsize
+.Nm
+.Ic modesense
+.Aq target:lun
+.Aq Fl m Ar page | Fl l
+.Op Fl P Ar pc
+.Op Fl d
+.Op Fl S Ar subpage
+.Op Fl c Ar size
+.Nm
+.Ic start
+.Aq target:lun
+.Op general options
+.Op Fl i
+.Op Fl o
+.Nm
+.Ic stop
+.Aq target:lun
+.Op general options
+.Op Fl i
+.Op Fl o
+.Nm
+.Ic synccache
+.Aq target:lun
+.Op general options
+.Op Fl l Ar lba
+.Op Fl b Ar blockcount
+.Op Fl r
+.Op Fl i
+.Op Fl c Ar cdbsize
+.Nm
+.Ic shutdown
+.Op general options
+.Nm
+.Ic startup
+.Op general options
+.Nm
+.Ic hardstop
+.Nm
+.Ic hardstart
+.Nm
+.Ic lunlist
+.Nm
+.Ic delay
+.Aq target:lun
+.Aq Fl l Ar datamove|done
+.Aq Fl t Ar secs
+.Op Fl T Ar oneshot|cont
+.Nm
+.Ic realsync Aq on|off|query
+.Nm
+.Ic setsync interval
+.Aq target:lun
+.Aq Fl i Ar interval
+.Nm
+.Ic getsync
+.Aq target:lun
+.Nm
+.Ic inject
+.Aq Fl i Ar action
+.Aq Fl p Ar pattern
+.Op Fl r Ar lba,len
+.Op Fl s Ar len fmt Op Ar args
+.Op Fl c
+.Op Fl d Ar delete_id
+.Nm
+.Ic create
+.Aq Fl b Ar backend
+.Op Fl B Ar blocksize
+.Op Fl d Ar device_id
+.Op Fl l Ar lun_id
+.Op Fl o Ar name=value
+.Op Fl s Ar size_bytes
+.Op Fl S Ar serial_num
+.Op Fl t Ar device_type
+.Nm
+.Ic remove
+.Aq Fl b Ar backend
+.Aq Fl l Ar lun_id
+.Op Fl o Ar name=value
+.Nm
+.Ic devlist
+.Op Fl b Ar backend
+.Op Fl v
+.Op Fl x
+.Nm
+.Ic port
+.Op Fl l
+.Op Fl o Ar on|off
+.Op Fl w Ar wwpn
+.Op Fl W Ar wwnn
+.Op Fl p Ar targ_port
+.Op Fl t Ar fe_type
+.Op Fl q
+.Op Fl x
+.Nm
+.Ic dumpooa
+.Nm
+.Ic dumpstructs
+.Nm
+.Ic help
+.Sh DESCRIPTION
+The
+.Nm
+utility is designed to provide a way to access and control the CAM Target
+Layer (CTL).
+It provides a way to send
+.Tn SCSI
+commands to the CTL layer, and also provides
+some meta-commands that utilize
+.Tn SCSI
+commands.
+(For instance, the
+.Ic lunlist
+command is implemented using the
+.Tn SCSI
+REPORT LUNS and INQUIRY commands.)
+.Pp
+The
+.Nm
+utility has a number of primary functions, many of which require a device
+identifier.
+The device identifier takes the following form:
+.Bl -tag -width 14n
+.It target:lun
+Specify the target (almost always 0) and LUN number to operate on.
+.El
+Many of the primary functions of the
+.Nm
+utility take the following optional arguments:
+.Pp
+.Bl -tag -width 10n
+.It Fl C Ar retries
+Specify the number of times to retry a command in the event of failure.
+.It Fl D Ar device
+Specify the device to open.  This allows opening a device other than the
+default device,
+.Pa /dev/cam/ctl ,
+to be opened for sending commands.
+.It Fl I Ar id
+Specify the initiator number to use.
+By default,
+.Nm
+will use 7 as the initiator number.
+.El
+.Pp
+Primary commands:
+.Bl -tag -width 11n
+.It Ic tur
+Send the
+.Tn SCSI
+TEST UNIT READY command to the device and report whether or not it is
+ready.
+.It Ic inquiry
+Send the
+.Tn SCSI
+INQUIRY command to the device and display some of the returned inquiry
+data.
+.It Ic reqsense
+Send the
+.Tn SCSI
+REQUEST SENSE command to the device and display the returned sense
+information.
+.It Ic reportluns
+Send the
+.Tn SCSI
+REPORT LUNS command to the device and display supported LUNs.
+.It Ic read
+Send a
+.Tn SCSI
+READ command to the device, and write the requested data to a file or
+stdout.
+.Bl -tag -width 12n
+.It Fl l Ar lba
+Specify the starting Logical Block Address for the READ.  This can be
+specified in decimal, octal (starting with 0), hexadecimal (starting with
+0x) or any other base supported by
+.Xr strtoull 3 .
+.It Fl d Ar datalen
+Specify the length, in 512 byte blocks, of the READ request.
+.It Fl f Ar file
+Specify the destination for the data read by the READ command.  Either a
+filename or
+.Sq -
+for stdout may be specified.
+.It Fl c Ar cdbsize
+Specify the minimum
+.Tn SCSI
+CDB (Command Data Block) size to be used for the READ request.  Allowable
+values are 6, 10, 12 and 16.  Depending upon the LBA and amount of data
+requested, a larger CDB size may be used to satisfy the request.  (e.g.,
+for LBAs above 0xffffffff, READ(16) must be used to satisfy the request.)
+.It Fl b Ar blocksize
+Specify the blocksize of the underlying
+.Tn SCSI
+device, so the transfer length
+can be calculated accurately.  The blocksize can be obtained via the
+.Tn SCSI
+READ CAPACITY command.
+.It Fl N 
+Do not copy data to
+.Nm
+from the kernel when doing a read, just execute the command without copying
+data.
+This is to be used for performance testing.
+.El
+.It Ic write
+Read data from a file or stdin, and write the data to the device using the
+.Tn SCSI
+WRITE command.
+.Bl -tag -width 12n
+.It Fl l Ar lba
+Specify the starting Logical Block Address for the WRITE.  This can be
+specified in decimal, octal (starting with 0), hexadecimal (starting with
+0x) or any other base supported by
+.Xr strtoull 3 .
+.It Fl d Ar atalen
+Specify the length, in 512 byte blocks, of the WRITE request.
+.It Fl f Ar file
+Specify the source for the data to be written by the WRITE command.  Either a
+filename or
+.Sq -
+for stdin may be specified.
+.It Fl c Ar cdbsize
+Specify the minimum
+.Tn SCSI
+CDB (Command Data Block) size to be used for the READ request.  Allowable
+values are 6, 10, 12 and 16.  Depending upon the LBA and amount of data
+requested, a larger CDB size may be used to satisfy the request.  (e.g.,
+for LBAs above 0xffffffff, READ(16) must be used to satisfy the request.)
+.It Fl b Ar blocksize
+Specify the blocksize of the underlying
+.Tn SCSI
+device, so the transfer length
+can be calculated accurately.  The blocksize can be obtained via the
+.Tn SCSI
+READ CAPACITY command.
+.It Fl N
+Do not copy data to
+.Nm
+to the kernel when doing a write, just execute the command without copying
+data.
+This is to be used for performance testing.
+.El
+.It Ic bbrread
+Issue a SCSI READ command to the logical device to potentially force a bad
+block on a disk in the RAID set to be reconstructed from the other disks in
+the array.  This command should only be used on an array that is in the
+normal state.  If used on a critical array, it could cause the array to go
+offline if the bad block to be remapped is on one of the disks that is
+still active in the array.
+.Pp
+The data for this particular command will be discarded, and not returned to
+the user.
+.Pp
+In order to determine which LUN to read from, the user should first
+determine which LUN the disk with a bad block belongs to.  Then he should
+map the bad disk block back to the logical block address for the array in
+order to determine which LBA to pass in to the
+.Ic bbrread
+command.
+.Pp
+This command is primarily intended for testing.  In practice, bad block
+remapping will generally be triggered by the in-kernel Disk Aerobics and
+Disk Scrubbing code.
+.Bl -tag -width 10n
+.It Fl l Ar lba
+Specify the starting Logical Block Address.
+.It Fl d Ar datalen
+Specify the amount of data in bytes to read from the LUN.  This must be a
+multiple of the LUN blocksize.
+.El
+.It Ic readcap
+Send the
+.Tn SCSI
+READ CAPACITY command to the device and display the device size and device
+block size.  By default, READ CAPACITY(10) is
+used.  If the device returns a maximum LBA of 0xffffffff, however,
+.Nm
+will automatically issue a READ CAPACITY(16), which is implemented as a
+service action of the SERVICE ACTION IN(16) opcode.  The user can specify
+the minimum CDB size with the
+.Fl c
+argument.  Valid values for the
+.Fl c
+option are 10 and 16.  If a 10 byte CDB is specified, the request will be
+automatically reissued with a 16 byte CDB if the maximum LBA returned is
+0xffffffff.
+.It Ic modesense
+Send a
+.Tn SCSI
+MODE SENSE command to the device, and display the requested mode page(s) or
+page list.
+.Bl -tag -width 10n
+.It Fl m Ar page
+Specify the mode page to display.  This option and the
+.Fl l
+option are mutually exclusive.  One of the two must be specified, though.
+Mode page numbers may be specified in decimal or hexadecimal.
+.It Fl l
+Request that the list of mode pages supported by the device be returned.
+This option and the
+.Fl m
+option are mutually exclusive.  One of the two must be specified, though.
+.It Fl P Ar pc
+Specify the mode page page control value.  Possible values are:
+.Bl -tag -width 2n -compact
+.It 0
+Current values.
+.It 1
+Changeable value bitmask.
+.It 2
+Default values.
+.It 3
+Saved values.
+.El
+.It Fl d
+Disable block descriptors when sending the mode sense request.
+.It Fl S Ar subpage
+Specify the subpage used with the mode sense request.
+.It Fl c Ar cdbsize
+Specify the CDB size used for the mode sense request.  Supported values are
+6 and 10.
+.El
+.It Ic start
+Send the
+.Tn SCSI
+START STOP UNIT command to the specified LUN with the start
+bit set.
+.Bl -tag -width 4n
+.It Fl i
+Set the immediate bit in the CDB.  Note that CTL does not support the
+immediate bit, so this is primarily useful for making sure that CTL returns
+the proper error.
+.It Fl o
+Set the Copan proprietary on/offline bit in the CDB.  When this flag is
+used, the LUN will be marked online again (see the description of the
+.Ic shutdown
+and
+.Ic startup
+commands).  When this flag is used with a
+start command, the LUN will NOT be spun up.  You need to use a start
+command without the
+.Fl o
+flag to spin up the disks in the LUN.
+.El
+.It Ic stop
+Send the
+.Tn SCSI
+START STOP UNIT command to the specified LUN with the start
+bit cleared.  We use an ordered tag to stop the LUN, so we can guarantee
+that all pending I/O executes before it is stopped.  (CTL guarantees this
+anyway, but
+.Nm
+sends an ordered tag for completeness.)
+.Bl -tag -width 4n
+.It Fl i
+Set the immediate bit in the CDB.  Note that CTL does not support the
+immediate bit, so this is primarily useful for making sure that CTL returns
+the proper error.
+.It Fl o
+Set the Copan proprietary on/offline bit in the CDB.  When this flag is
+used, the LUN will be spun down and taken offline ("Logical unit not ready,
+manual intervention required").  See the description of the 
+.Ic shutdown
+and
+.Ic startup
+options.
+.El
+.It Ic synccache
+Send the
+.Tn SCSI 
+SYNCHRONIZE CACHE command to the device.  By default, SYNCHRONIZE
+CACHE(10) is used.  If the specified starting LBA is greater than
+0xffffffff or the length is greater than 0xffff, though,
+SYNCHRONIZE CACHE(16) will be used.  The 16 byte command will also be used
+if the user specifies a 16 byte CDB with the
+.Fl c
+argument.
+.Bl -tag -width 14n
+.It Fl l Ar lba
+Specify the starting LBA of the cache region to synchronize.  This option is a
+no-op for CTL.  If you send a SYNCHRONIZE CACHE command, it will sync the
+cache for the entire LUN.
+.It Fl b Ar blockcount
+Specify the length of the cache region to synchronize.  This option is a
+no-op for CTL.  If you send a SYNCHRONIZE CACHE command, it will sync the
+cache for the entire LUN.
+.It Fl r
+Specify relative addressing for the starting LBA.  CTL does not support
+relative addressing, since it only works for linked commands, and CTL
+doesn't support linked commands.
+.It Fl i
+Tell the target to return status immediately after issuing the SYHCHRONIZE CACHE
+command rather than waiting for the cache to finish syncing.  CTL does not
+support this bit.
+.It Fl c Ar cdbsize
+Specify the minimum CDB size.  Valid values are 10 and 16 bytes.
+.El
+.It Ic shutdown
+Issue a
+.Tn SCSI
+START STOP UNIT command with the start bit cleared and the on/offline bit
+set to all direct access LUNs.  This will spin down all direct access LUNs,
+and mark them offline ("Logical unit not ready, manual intervention
+required").  Once marked offline, the state can only be cleared by sending
+a START STOP UNIT command with the start bit set and the on/offline bit
+set.  The
+.Nm
+commands
+.Ic startup
+and
+.Ic start
+will accomplish this.  Note that the
+on/offline bit is a non-standard Copan extension to the
+.Tn SCSI
+START STOP UNIT command, so merely sending a normal start command from an
+initiator will not clear the condition.  (This is by design.)
+.It Ic startup
+Issue a
+.Tn SCSI
+START STOP UNIT command with the start bit set and the on/offline bit set
+to all direct access LUNs.  This will mark all direct access LUNs "online"
+again.  It will not cause any LUNs to start up.  A separate start command
+without the on/offline bit set is necessary for that.
+.It Ic hardstop
+Use the kernel facility for stopping all direct access LUNs and setting the
+offline bit.  Unlike the
+.Ic shutdown
+command above, this command allows shutting down LUNs with I/O active.  It
+will also issue a LUN reset to any reserved LUNs to break the reservation
+so that the LUN can be stopped.
+.Ic shutdown
+command instead.
+.It Ic hardstart
+This command is functionally identical to the
+.Ic startup
+command described above.  The primary difference is that the LUNs are
+enumerated and commands sent by the in-kernel Front End Target Driver
+instead of by
+.Nm .
+.It Ic lunlist
+List all LUNs registered with CTL.
+Because this command uses the ioctl port, it will only work when the FETDs
+(Front End Target Drivers) are enabled.
+This command is the equivalent of doing a REPORT LUNS on one LUN and then
+and then an INQUIRY on each LUN in the system.
+.It Ic delay
+Delay commands at the given location.  There are two places where commands
+may be delayed currently: before data is transferred
+.Pq Dq datamove
+and just prior to sending status to the host
+.Pq Dq done .
+One of the two must be supplied as an argument to the
+.Fl l 
+option.  The
+.Fl t
+option must also be specified.
+.Bl -tag -width 12n
+.It Fl l Ar delayloc
+Delay command(s) at the specified location.
+This can either be at the data movement stage (datamove) or prior to
+command completion (done).
+.It Fl t Ar delaytime
+Delay command(s) for the specified number of seconds.  This must be
+specified.  If set to 0, it will clear out any previously set delay for
+this particular location (datamove or done).
+.It Fl T Ar delaytype
+Specify the delay type.
+By default, the
+.Ic delay
+option will delay the next command sent to the given LUN.
+With the
+.Fl T Ar cont
+option, every command will be delayed by the specified period of time.
+With the
+.Fl T Ar oneshot
+the next command sent to the given LUN will be delayed and all subsequent
+commands will be completed normally.
+This is the default.
+.El
+.It Ic realsync
+Query and control CTL's SYNCHRONIZE CACHE behavior.  The
+.Sq query
+argument
+will show whether SYNCHRONIZE CACHE commands are being sent to the backend
+or not.
+The default is to send SYNCHRONIZE CACHE commands to the backend.
+The 
+.Sq on
+argument will cause all SYNCHRONIZE CACHE commands sent to all LUNs to be
+sent to the backend.
+The
+.Sq off
+argument will cause all SYNCHRONIZE CACHE commands sent to all LUNs to be
+immediately returned to the initiator with successful status.
+.It Ic setsync
+For a given lun, only actually service every Nth SYNCHRONIZE CACHE command
+that is sent.  This can be used for debugging the optimal time period for
+sending SYNCHRONIZE cache commands.  An interval of 0 means that the cache
+will be flushed for this LUN every time a SYNCHRONIZE CACHE command is
+received.
+.Pp
+You must specify the target and LUN you want to modify.
+.It Ic getsync
+Get the interval at which we actually service the SYNCHRONIZE CACHE
+command, as set by the
+.Ic setsync
+command above.
+The reported number means that we will actually flush the cache on every
+Nth SYNCHRONIZE CACHE command.  A value of 0 means that we will flush the
+cache every time.
+.Pp
+You must specify the target and LUN you want to query.
+.It Ic inject
+Inject the specified type of error for the LUN specified, when a command
+that matches the given pattern is seen.
+The sense data returned is in either fixed or descriptor format, depending
+upon the status of the D_SENSE bit in the control mode page (page 0xa) for
+the LUN.
+.Pp
+Errors are only injected for commands that have not already failed for
+other reasons.
+By default, only the first command matching the pattern specified is
+returned with the supplied error.
+.Pp
+If the
+.Fl c
+flag is specified, all commands matching the pattern will be returned with
+the specified error until the error injection command is deleted with
+.Fl d 
+flag.
+.Bl -tag -width 17n
+.It Fl i Ar action
+Specify the error to return:
+.Bl -tag -width 10n
+.It aborted
+Return the next matching command on the specified LUN with the sense key
+ABORTED COMMAND (0x0b), and the ASC/ASCQ 0x45,0x00 ("Select or reselect
+failure").
+.It mediumerr
+Return the next matching command on the specified LUN with the sense key
+MEDIUM ERROR (0x03) and the ASC/ASCQ 0x11,0x00 ("Unrecovered read error") for
+reads, or ASC/ASCQ 0x0c,0x02 ("Write error - auto reallocation failed")
+for write errors.
+.It ua
+Return the next matching command on the specified LUN with the sense key
+UNIT ATTENTION (0x06) and the ASC/ASCQ 0x29,0x00 ("POWER ON, RESET, OR BUS
+DEVICE RESET OCCURRED").
+.It custom
+Return the next matching command on the specified LUN with the supplied
+sense data.
+The
+.Fl s
+argument must be specified.
+.El
+.It Fl p Ar pattern
+Specify which commands should be returned with the given error.
+.Bl -tag -width 10n
+.It read
+The error should apply to READ(6), READ(10), READ(12), READ(16), etc.
+.It write
+The error should apply to WRITE(6), WRITE(10), WRITE(12), WRITE(16), WRITE
+AND VERIFY(10), etc.
+.It rw
+The error should apply to both read and write type commands.
+.It readcap
+The error should apply to READ CAPACITY(10) and READ CAPACITY(16) commands.
+.It tur
+The error should apply to TEST UNIT READY commands.
+.It any
+The error should apply to any command.
+.El
+.It Fl r Ar lba,len
+Specify the starting lba and length of the range of LBAs which should
+trigger an error.
+This option is only applies when read and/or write patterns are specified.
+If used with other command types, the error will never be triggered.
+.It Fl s Ar len fmt Op Ar args
+Specify the sense data that is to be returned for custom actions.
+If the format is
+.Sq - ,
+len bytes of sense data will be read from standard input and written to the
+sense buffer.
+If len is longer than 252 bytes (the maximum allowable
+.Tn SCSI
+sense data length), it will be truncated to that length.
+The sense data format is described in
+.Xr cam_cdparse 3 .
+.It Fl c
+The error injection should be persistent, instead of happening once.
+Persistent errors must be deleted with the
+.Fl d
+argument.
+.It Fl d Ar delete_id
+Delete the specified error injection serial number.
+The serial number is returned when the error is injected.
+.El
+.It Ic port
+Perform one of several CTL frontend port operations.
+Either get a list of frontend ports
+.Pq Fl l ,
+turn one or more frontends on
+or off
+.Pq Fl o Ar on|off ,
+or set the World Wide Node Name
+.Pq Fl w Ar wwnn
+or World Wide Port Name
+.Pq Fl W Ar wwpn
+for a given port.
+One of
+.Fl l ,
+.Fl o ,
+or 
+.Fl w
+or
+.Fl W
+must be specified.
+The WWNN and WWPN may both be specified at the same time, but cannot be
+combined with enabling/disabling or listing ports.
+.Bl -tag -width 12n
+.It Fl l
+List all CTL frontend ports or a specific port type or number.
+.It Fl o Ar on|off
+Turn the specified CTL frontend ports off or on.
+If no port number or port type is specified, all ports are turned on or
+off.
+.It Fl p Ar targ_port 
+Specify the frontend port number.
+The port numbers can be found in the frontend port list.
+.It Fl q
+Omit the header in the port list output.
+.It Fl t Ar fe_type
+Specify the frontend type.
+Currently defined port types are
+.Dq fc
+(Fibre Channel),
+.Dq scsi
+(Parallel SCSI),
+.Dq ioctl
+(CTL ioctl interface),
+and
+.Dq internal
+(CTL CAM SIM).
+.It Fl w Ar wwnn
+Set the World Wide Node Name for the given port.
+The
+.Fl n 
+argument must be specified, since this is only possible to implement on a
+single port.
+As a general rule, the WWNN should be the same across all ports on the
+system.
+.It Fl W Ar wwpn
+Set the World Wide Node Name for the given port.
+The
+.Fl n
+argument must be specified, since this is only possible to implement on a
+single port.
+As a general rule, the WWPN must be different for every port in the system.
+.It Fl x
+Output the port list in XML format.
+.El
+.It Ic dumpooa
+Dump the OOA (Order Of Arrival) queue for each LUN registered with CTL.
+.It Ic dumpstructs
+Dump the CTL structures to the console.
+.It Ic create
+Create a new LUN.
+The backend must be specified, and depending upon the backend requested,
+some of the other options may be required.
+If the LUN is created successfully, the LUN configuration will be
+displayed.
+If LUN creation fails, a message will be displayed describing the failure.
+.Bl -tag -width 14n
+.It Fl b Ar backend
+The
+.Fl b
+flag is required.
+This specifies the name backend to use when creating the LUN.
+Examples are
+.Dq ramdisk
+and
+.Dq block .
+.It Fl B Ar blocksize
+Specify the blocksize of the backend in bytes.
+.It Fl d Ar device_id
+Specify the LUN-associated string to use in the
+.Tn SCSI
+INQUIRY VPD page 0x83 data.
+.It Fl l Ar lun_id
+Request that a particular LUN number be assigned.
+If the requested LUN number is not available, the request will fail.
+.It Fl o Ar name=value
+Specify a backend-specific name/value pair.
+Multiple
+.Fl o
+arguments may be specified.
+Refer to the backend documentation for arguments that may be used.
+.It Fl s Ar size_bytes
+Specify the size of the LUN in bytes.
+Some backends may allow setting the size (e.g. the ramdisk backend) and for
+others the size may be implicit (e.g. the block backend).
+.It Fl S Ar serial_num
+Specify the serial number to be used in the
+.Tn SCSI
+INQUIRY VPD page 0x80 data.
+.It Fl t Ar device_type
+Specify the numeric SCSI device type to use when creating the LUN.
+For example, the Direct Access type is 0.
+If this flag is not used, the type of LUN created is backend-specific.
+Not all LUN types are supported.
+Currently CTL only supports Direct Access (type 0) and Processor (type 3)
+LUNs.
+The backend requested may or may not support all of the LUN types that CTL
+supports.
+.El
+.It Ic remove
+Remove a LUN.
+The backend must be specified, and the LUN number must also be specified.
+Backend-specific options may also be specified with the
+.Fl o
+flag.
+.Bl -tag -width 14n
+.It Fl b Ar backend
+Specify the backend that owns the LUN to be removed.
+Examples are
+.Dq ramdisk
+and
+.Dq block .
+.It Fl l Ar lun_id
+Specify the LUN number to remove.
+.It Fl o Ar name=value
+Specify a backend-specific name/value pair.
+Multiple
+.Fl o
+arguments may be specified.
+Refer to the backend documentation for arguments that may be used.
+.El
+.It Ic devlist
+Get a list of all configured LUNs.
+This also includes the LUN size and blocksize, serial number and device ID.
+.Bl -tag -width 11n
+.It Fl b Ar backend
+Specify the backend.
+This restricts the LUN list to the named backend.
+Examples are
+.Dq ramdisk
+and
+.Dq block .
+.It Fl v
+Be verbose.
+This will also display any backend-specific LUN attributes in addition to
+the standard per-LUN information.
+.It Fl x
+Dump the raw XML.
+The LUN list information from the kernel comes in XML format, and this
+option allows the display of the raw XML data.
+This option and the
+.Fl v
+and
+.Fl b
+options are mutually exclusive.
+If you specify
+.Fl x ,
+the entire LUN database is displayed in XML format.
+.El
+.It Ic help
+Display
+.Nm
+usage information.
+.El
+.Sh EXAMPLES
+.Dl ctladm tur 0:1
+.Pp
+Send a
+.Tn SCSI
+TEST UNIT READY command to LUN 1.
+.Pp
+.Dl ctladm modesense 0:1 -l
+.Pp
+Display the list of mode pages supported by LUN 1.
+.Pp
+.Dl ctladm modesense 0:0 -m 10 -P 3 -d -c 10
+.Pp
+Display the saved version of the Control mode page (page 10) on LUN 0.
+Disable fetching block descriptors, and use a 10 byte MODE SENSE command
+instead of the default 6 byte command.
+.Pp
+.Bd -literal
+ctladm read 0:2 -l 0 -d 1 -b 512 -f - > foo
+.Ed
+.Pp
+Read the first 512 byte block from LUN 2 and dump it to the file
+.Pa foo .
+.Bd -literal
+ctladm write 0:3 -l 0xff432140 -d 20 -b 512 -f /tmp/bar
+.Ed
+.Pp
+Read 10240 bytes from the file
+.Pa /tmp/bar
+and write it to target 0, LUN 3.
+starting at LBA 0xff432140.
+.Pp
+.Dl ctladm create -b ramdisk -s 10485760000000000
+.Pp
+Create a LUN with the
+.Dq fake
+ramdisk as a backing store.
+The LUN will claim to have a size of approximately 10 terabytes.
+.Pp
+.Dl ctladm create -b block -o file=src/usr.sbin/ctladm/ctladm.8
+.Pp
+Create a LUN using the block backend, and specify the file
+.Pa src/usr.sbin/ctladm/ctladm.8
+as the backing store.
+The size of the LUN will be derived from the size of the file.
+.Pp
+.Dl ctladm create -b block -o file=src/usr.sbin/ctladm/ctladm.8 -S MYSERIAL321 -d MYDEVID123
+.Pp
+Create a LUN using the block backend, specify the file
+.Pa src/usr.sbin/ctladm/ctladm.8
+as the backing store, and specify the
+.Tn SCSI
+VPD page 0x80 and 0x83 serial number (
+.Fl S)
+and device ID (
+.Fl d).
+.Pp
+.Dl ctladm remove -b block -l 12
+.Pp
+Remove LUN 12, which is handled by the block backend, from the system.
+.Pp
+.Dl ctladm devlist
+.Pp
+List configured LUNs in the system, along with their backend and serial
+number.
+This works when the Front End Target Drivers are enabled or disabled.
+.Pp
+.Dl ctladm lunlist
+.Pp
+List all LUNs in the system, along with their inquiry data and device type.
+This only works when the FETDs are enabled, since the commands go through the 
+ioctl port.
+.Pp
+.Dl ctladm inject 0:6 -i mediumerr -p read -r 0,512 -c
+.Pp
+Inject a medium error on LUN 6 for every read that covers the first 512
+blocks of the LUN.
+.Pp
+.Bd -literal -offset indent
+ctladm inject 0:6 -i custom -p tur -s 18 "f0 0 02 s12 04 02"
+.Ed
+.Pp
+Inject a custom error on LUN 6 for the next TEST UNIT READY command only.
+This will result in a sense key of NOT READY (0x02), and an ASC/ASCQ of
+0x04,0x02 ("Logical unit not ready, initializing command required").
+.Sh SEE ALSO
+.Xr cam 3 ,
+.Xr cam_cdbparse 3 ,
+.Xr cam 4 ,
+.Xr xpt 4 ,
+.Xr camcontrol 8
+.Sh HISTORY
+The
+.Nm
+utility was originally written during the Winter/Spring of 2003 as an
+interface to CTL.
+.Sh AUTHORS
+.An Ken Merry Aq ken at FreeBSD.org
diff -r ebe87ce4d7dd -r 1750de5b0399 head/usr.sbin/ctladm/ctladm.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/usr.sbin/ctladm/ctladm.c	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,4005 @@
+/*-
+ * Copyright (c) 2003, 2004 Silicon Graphics International Corp.
+ * Copyright (c) 1997-2007 Kenneth D. Merry
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions, and the following disclaimer,
+ *    without modification.
+ * 2. Redistributions in binary form must reproduce at minimum a disclaimer
+ *    substantially similar to the "NO WARRANTY" disclaimer below
+ *    ("Disclaimer") and any redistribution must be conditioned upon
+ *    including a substantially similar Disclaimer requirement for further
+ *    binary redistribution.
+ *
+ * NO WARRANTY
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGES.
+ *
+ * $Id: //depot/users/kenm/FreeBSD-test2/usr.sbin/ctladm/ctladm.c#4 $
+ */
+/*
+ * CAM Target Layer exercise program.
+ *
+ * Author: Ken Merry <ken at FreeBSD.org>
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD: head/usr.sbin/ctladm/ctladm.c 229997 2012-01-12 00:34:33Z ken $");
+
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/param.h>
+#include <sys/queue.h>
+#include <sys/callout.h>
+#include <sys/sbuf.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <getopt.h>
+#include <string.h>
+#include <errno.h>
+#include <err.h>
+#include <ctype.h>
+#include <bsdxml.h>
+#include <cam/scsi/scsi_all.h>
+#include <cam/scsi/scsi_message.h>
+#include <cam/ctl/ctl.h>
+#include <cam/ctl/ctl_io.h>
+#include <cam/ctl/ctl_frontend_internal.h>
+#include <cam/ctl/ctl_backend.h>
+#include <cam/ctl/ctl_ioctl.h>
+#include <cam/ctl/ctl_backend_block.h>
+#include <cam/ctl/ctl_util.h>
+#include <cam/ctl/ctl_scsi_all.h>
+#include <camlib.h>
+#include "ctladm.h"
+
+#ifdef min
+#undef min
+#endif
+#define min(x,y) (x < y) ? x : y
+
+typedef enum {
+	CTLADM_CMD_TUR,
+	CTLADM_CMD_INQUIRY,
+	CTLADM_CMD_REQ_SENSE,
+	CTLADM_CMD_ARRAYLIST,
+	CTLADM_CMD_REPORT_LUNS,
+	CTLADM_CMD_HELP,
+	CTLADM_CMD_DEVLIST,
+	CTLADM_CMD_ADDDEV,
+	CTLADM_CMD_RM,
+	CTLADM_CMD_CREATE,
+	CTLADM_CMD_READ,
+	CTLADM_CMD_WRITE,
+	CTLADM_CMD_PORT,
+	CTLADM_CMD_READCAPACITY,
+	CTLADM_CMD_MODESENSE,
+	CTLADM_CMD_DUMPOOA,
+	CTLADM_CMD_DUMPSTRUCTS,
+	CTLADM_CMD_START,
+	CTLADM_CMD_STOP,
+	CTLADM_CMD_SYNC_CACHE,
+	CTLADM_CMD_SHUTDOWN,
+	CTLADM_CMD_STARTUP,
+	CTLADM_CMD_LUNLIST,
+	CTLADM_CMD_HARDSTOP,
+	CTLADM_CMD_HARDSTART,
+	CTLADM_CMD_DELAY,
+	CTLADM_CMD_REALSYNC,
+	CTLADM_CMD_SETSYNC,
+	CTLADM_CMD_GETSYNC,
+	CTLADM_CMD_ERR_INJECT,
+	CTLADM_CMD_BBRREAD,
+	CTLADM_CMD_PRES_IN,
+	CTLADM_CMD_PRES_OUT,
+	CTLADM_CMD_INQ_VPD_DEVID,
+	CTLADM_CMD_RTPG
+} ctladm_cmdfunction;
+
+typedef enum {
+	CTLADM_ARG_NONE		= 0x0000000,
+	CTLADM_ARG_AUTOSENSE	= 0x0000001,
+	CTLADM_ARG_DEVICE	= 0x0000002,
+	CTLADM_ARG_ARRAYSIZE	= 0x0000004,
+	CTLADM_ARG_BACKEND	= 0x0000008,
+	CTLADM_ARG_CDBSIZE	= 0x0000010,
+	CTLADM_ARG_DATALEN	= 0x0000020,
+	CTLADM_ARG_FILENAME	= 0x0000040,
+	CTLADM_ARG_LBA		= 0x0000080,
+	CTLADM_ARG_PC		= 0x0000100,
+	CTLADM_ARG_PAGE_CODE	= 0x0000200,
+	CTLADM_ARG_PAGE_LIST	= 0x0000400,
+	CTLADM_ARG_SUBPAGE	= 0x0000800,
+	CTLADM_ARG_PAGELIST	= 0x0001000,
+	CTLADM_ARG_DBD		= 0x0002000,
+	CTLADM_ARG_TARG_LUN	= 0x0004000,
+	CTLADM_ARG_BLOCKSIZE	= 0x0008000,
+	CTLADM_ARG_IMMED	= 0x0010000,
+	CTLADM_ARG_RELADR	= 0x0020000,
+	CTLADM_ARG_RETRIES	= 0x0040000,
+	CTLADM_ARG_ONOFFLINE	= 0x0080000,
+	CTLADM_ARG_ONESHOT	= 0x0100000,
+	CTLADM_ARG_TIMEOUT	= 0x0200000,
+	CTLADM_ARG_INITIATOR 	= 0x0400000,
+	CTLADM_ARG_NOCOPY	= 0x0800000,
+	CTLADM_ARG_NEED_TL	= 0x1000000
+} ctladm_cmdargs;
+
+struct ctladm_opts {
+	const char	*optname;
+	uint32_t	cmdnum;
+	ctladm_cmdargs	argnum;
+	const char	*subopt;
+};
+
+typedef enum {
+	CC_OR_NOT_FOUND,
+	CC_OR_AMBIGUOUS,
+	CC_OR_FOUND
+} ctladm_optret;
+
+static const char rw_opts[] = "Nb:c:d:f:l:";
+static const char startstop_opts[] = "io";
+
+struct ctladm_opts option_table[] = {
+	{"adddev", CTLADM_CMD_ADDDEV, CTLADM_ARG_NONE, NULL},
+	{"bbrread", CTLADM_CMD_BBRREAD, CTLADM_ARG_NEED_TL, "d:l:"},
+	{"create", CTLADM_CMD_CREATE, CTLADM_ARG_NONE, "b:B:d:l:o:s:S:t:"},
+	{"delay", CTLADM_CMD_DELAY, CTLADM_ARG_NEED_TL, "T:l:t:"},
+	{"devid", CTLADM_CMD_INQ_VPD_DEVID, CTLADM_ARG_NEED_TL, NULL},
+	{"devlist", CTLADM_CMD_DEVLIST, CTLADM_ARG_NONE, "b:vx"},
+	{"dumpooa", CTLADM_CMD_DUMPOOA, CTLADM_ARG_NONE, NULL},
+	{"dumpstructs", CTLADM_CMD_DUMPSTRUCTS, CTLADM_ARG_NONE, NULL},
+	{"getsync", CTLADM_CMD_GETSYNC, CTLADM_ARG_NEED_TL, NULL},
+	{"hardstart", CTLADM_CMD_HARDSTART, CTLADM_ARG_NONE, NULL},
+	{"hardstop", CTLADM_CMD_HARDSTOP, CTLADM_ARG_NONE, NULL},
+	{"help", CTLADM_CMD_HELP, CTLADM_ARG_NONE, NULL},
+	{"inject", CTLADM_CMD_ERR_INJECT, CTLADM_ARG_NEED_TL, "cd:i:p:r:s:"},
+	{"inquiry", CTLADM_CMD_INQUIRY, CTLADM_ARG_NEED_TL, NULL},
+	{"lunlist", CTLADM_CMD_LUNLIST, CTLADM_ARG_NONE, NULL},
+	{"modesense", CTLADM_CMD_MODESENSE, CTLADM_ARG_NEED_TL, "P:S:dlm:c:"},
+	{"port", CTLADM_CMD_PORT, CTLADM_ARG_NONE, "lo:p:qt:w:W:x"},
+	{"prin", CTLADM_CMD_PRES_IN, CTLADM_ARG_NEED_TL, "a:"},
+	{"prout", CTLADM_CMD_PRES_OUT, CTLADM_ARG_NEED_TL, "a:k:r:s:"},
+	{"read", CTLADM_CMD_READ, CTLADM_ARG_NEED_TL, rw_opts},
+	{"readcapacity", CTLADM_CMD_READCAPACITY, CTLADM_ARG_NEED_TL, "c:"},
+	{"realsync", CTLADM_CMD_REALSYNC, CTLADM_ARG_NONE, NULL},
+	{"remove", CTLADM_CMD_RM, CTLADM_ARG_NONE, "b:l:o:"},
+	{"reportluns", CTLADM_CMD_REPORT_LUNS, CTLADM_ARG_NEED_TL, NULL},
+	{"reqsense", CTLADM_CMD_REQ_SENSE, CTLADM_ARG_NEED_TL, NULL},
+	{"rtpg", CTLADM_CMD_RTPG, CTLADM_ARG_NEED_TL, NULL},
+	{"setsync", CTLADM_CMD_SETSYNC, CTLADM_ARG_NEED_TL, "i:"},
+	{"shutdown", CTLADM_CMD_SHUTDOWN, CTLADM_ARG_NONE, NULL},
+	{"start", CTLADM_CMD_START, CTLADM_ARG_NEED_TL, startstop_opts},
+	{"startup", CTLADM_CMD_STARTUP, CTLADM_ARG_NONE, NULL},
+	{"stop", CTLADM_CMD_STOP, CTLADM_ARG_NEED_TL, startstop_opts},
+	{"synccache", CTLADM_CMD_SYNC_CACHE, CTLADM_ARG_NEED_TL, "b:c:il:r"},
+	{"tur", CTLADM_CMD_TUR, CTLADM_ARG_NEED_TL, NULL},
+	{"write", CTLADM_CMD_WRITE, CTLADM_ARG_NEED_TL, rw_opts},
+	{"-?", CTLADM_CMD_HELP, CTLADM_ARG_NONE, NULL},
+	{"-h", CTLADM_CMD_HELP, CTLADM_ARG_NONE, NULL},
+	{NULL, 0, 0, NULL}
+};
+
+
+ctladm_optret getoption(struct ctladm_opts *table, char *arg, uint32_t *cmdnum,
+			ctladm_cmdargs *argnum, const char **subopt);
+static int cctl_parse_tl(char *str, int *target, int *lun);
+static int cctl_dump_ooa(int fd, int argc, char **argv);
+static int cctl_port_dump(int fd, int quiet, int xml, int32_t fe_num,
+			  ctl_port_type port_type);
+static int cctl_port(int fd, int argc, char **argv, char *combinedopt);
+static int cctl_do_io(int fd, int retries, union ctl_io *io, const char *func);
+static int cctl_delay(int fd, int target, int lun, int argc, char **argv,
+		      char *combinedopt);
+static int cctl_lunlist(int fd);
+static void cctl_cfi_mt_statusstr(cfi_mt_status status, char *str, int str_len);
+static void cctl_cfi_bbr_statusstr(cfi_bbrread_status, char *str, int str_len);
+static int cctl_hardstopstart(int fd, ctladm_cmdfunction command);
+static int cctl_bbrread(int fd, int target, int lun, int iid, int argc,
+			char **argv, char *combinedopt);
+static int cctl_startup_shutdown(int fd, int target, int lun, int iid,
+				 ctladm_cmdfunction command);
+static int cctl_sync_cache(int fd, int target, int lun, int iid, int retries,
+			   int argc, char **argv, char *combinedopt);
+static int cctl_start_stop(int fd, int target, int lun, int iid, int retries,
+			   int start, int argc, char **argv, char *combinedopt);
+static int cctl_mode_sense(int fd, int target, int lun, int iid, int retries, 
+			   int argc, char **argv, char *combinedopt);
+static int cctl_read_capacity(int fd, int target, int lun, int iid,
+			      int retries, int argc, char **argv,
+			      char *combinedopt);
+static int cctl_read_write(int fd, int target, int lun, int iid, int retries,
+			   int argc, char **argv, char *combinedopt,
+			   ctladm_cmdfunction command);
+static int cctl_get_luns(int fd, int target, int lun, int iid, int retries,
+			 struct scsi_report_luns_data **lun_data,
+			 uint32_t *num_luns);
+static int cctl_report_luns(int fd, int target, int lun, int iid, int retries);
+static int cctl_tur(int fd, int target, int lun, int iid, int retries);
+static int cctl_get_inquiry(int fd, int target, int lun, int iid, int retries,
+			    char *path_str, int path_len,
+			    struct scsi_inquiry_data *inq_data);
+static int cctl_inquiry(int fd, int target, int lun, int iid, int retries);
+static int cctl_req_sense(int fd, int target, int lun, int iid, int retries);
+static int cctl_persistent_reserve_in(int fd, int target, int lun,
+				      int initiator, int argc, char **argv,
+				      char *combinedopt, int retry_count);
+static int cctl_persistent_reserve_out(int fd, int target, int lun, 
+				       int initiator, int argc, char **argv,
+				       char *combinedopt, int retry_count);
+static int cctl_create_lun(int fd, int argc, char **argv, char *combinedopt);
+static int cctl_inquiry_vpd_devid(int fd, int target, int lun, int initiator);
+static int cctl_report_target_port_group(int fd, int target, int lun,
+					 int initiator);
+
+ctladm_optret
+getoption(struct ctladm_opts *table, char *arg, uint32_t *cmdnum,
+	  ctladm_cmdargs *argnum, const char **subopt)
+{
+	struct ctladm_opts *opts;
+	int num_matches = 0;
+
+	for (opts = table; (opts != NULL) && (opts->optname != NULL);
+	     opts++) {
+		if (strncmp(opts->optname, arg, strlen(arg)) == 0) {
+			*cmdnum = opts->cmdnum;
+			*argnum = opts->argnum;
+			*subopt = opts->subopt;
+
+			if (strcmp(opts->optname, arg) == 0)
+				return (CC_OR_FOUND);
+
+			if (++num_matches > 1)
+				return(CC_OR_AMBIGUOUS);
+		}
+	}
+
+	if (num_matches > 0)
+		return(CC_OR_FOUND);
+	else
+		return(CC_OR_NOT_FOUND);
+}
+
+
+static int
+cctl_parse_tl(char *str, int *target, int *lun)
+{
+	char *tmpstr;
+	int retval;
+
+	retval = 0;
+
+	while (isspace(*str) && (*str != '\0'))
+		str++;
+
+	tmpstr = (char *)strtok(str, ":");
+	if ((tmpstr != NULL) && (*tmpstr != '\0')) {
+		*target = strtol(tmpstr, NULL, 0);
+		tmpstr = (char *)strtok(NULL, ":");
+		if ((tmpstr != NULL) && (*tmpstr != '\0')) {
+			*lun = strtol(tmpstr, NULL, 0);
+		} else
+			retval = -1;
+	} else
+		retval = -1;
+
+	return (retval);
+}
+
+static int
+cctl_dump_ooa(int fd, int argc, char **argv)
+{
+	struct ctl_ooa ooa;
+	long double cmd_latency;
+	int num_entries, len;
+	int target = -1, lun = -1;
+	int retval;
+	unsigned int i;
+
+	num_entries = 104;
+
+	if ((argc > 2)
+	 && (isdigit(argv[2][0]))) {
+		retval = cctl_parse_tl(argv[2], &target, &lun);
+		if (retval != 0)
+			warnx("invalid target:lun argument %s", argv[2]);
+	}
+retry:
+
+	len = num_entries * sizeof(struct ctl_ooa_entry);
+
+	bzero(&ooa, sizeof(ooa));
+
+	ooa.entries = malloc(len);
+
+	if (ooa.entries == NULL) {
+		warn("%s: error mallocing %d bytes", __func__, len);
+		return (1);
+	}
+
+	if (argc > 2) {
+		ooa.lun_num = lun;
+	} else
+		ooa.flags |= CTL_OOA_FLAG_ALL_LUNS;
+
+	ooa.alloc_len = len;
+	ooa.alloc_num = num_entries;
+	if (ioctl(fd, CTL_GET_OOA, &ooa) == -1) {
+		warn("%s: CTL_GET_OOA ioctl failed", __func__);
+		retval = 1;
+		goto bailout;
+	}
+
+	if (ooa.status == CTL_OOA_NEED_MORE_SPACE) {
+		num_entries = num_entries * 2;
+		free(ooa.entries);
+		ooa.entries = NULL;
+		goto retry;
+	}
+
+	if (ooa.status != CTL_OOA_OK) {
+		warnx("%s: CTL_GET_OOA ioctl returned error %d", __func__,
+		      ooa.status);
+		retval = 1;
+		goto bailout;
+	}
+
+	fprintf(stdout, "Dumping OOA queues\n");
+	for (i = 0; i < ooa.fill_num; i++) {
+		struct ctl_ooa_entry *entry;
+		char cdb_str[(SCSI_MAX_CDBLEN * 3) +1];
+		struct bintime delta_bt;
+		struct timespec ts;
+
+		entry = &ooa.entries[i];
+
+		delta_bt = ooa.cur_bt;
+		bintime_sub(&delta_bt, &entry->start_bt);
+		bintime2timespec(&delta_bt, &ts);
+		cmd_latency = ts.tv_sec * 1000;
+		if (ts.tv_nsec > 0)
+			cmd_latency += ts.tv_nsec / 1000000;
+		
+		fprintf(stdout, "LUN %jd tag 0x%04x%s%s%s%s%s: %s. CDB: %s "
+			"(%0.0Lf ms)\n",
+			(intmax_t)entry->lun_num, entry->tag_num,
+			(entry->cmd_flags & CTL_OOACMD_FLAG_BLOCKED) ?
+			 " BLOCKED" : "",
+			(entry->cmd_flags & CTL_OOACMD_FLAG_DMA) ? " DMA" : "",
+			(entry->cmd_flags & CTL_OOACMD_FLAG_DMA_QUEUED) ?
+			 " DMAQUEUED" : "",
+			(entry->cmd_flags & CTL_OOACMD_FLAG_ABORT) ?
+			 " ABORT" : "",
+			(entry->cmd_flags & CTL_OOACMD_FLAG_RTR) ? " RTR" :"",
+			scsi_op_desc(entry->cdb[0], NULL),
+			scsi_cdb_string(entry->cdb, cdb_str, sizeof(cdb_str)),
+			cmd_latency);
+	}
+	fprintf(stdout, "OOA queues dump done\n");
+#if 0
+	if (ioctl(fd, CTL_DUMP_OOA) == -1) {
+		warn("%s: CTL_DUMP_OOA ioctl failed", __func__);
+		return (1);
+	}
+#endif
+
+bailout:
+	free(ooa.entries);
+
+	return (0);
+}
+
+static int
+cctl_dump_structs(int fd, ctladm_cmdargs cmdargs __unused)
+{
+	if (ioctl(fd, CTL_DUMP_STRUCTS) == -1) {
+		warn(__func__);
+		return (1);
+	}
+	return (0);
+}
+
+static int
+cctl_port_dump(int fd, int quiet, int xml, int32_t targ_port,
+	       ctl_port_type port_type)
+{
+	struct ctl_port_list port_list;
+	struct ctl_port_entry *entries;
+	struct sbuf *sb = NULL;
+	int num_entries;
+	int did_print = 0;
+	unsigned int i;
+
+	num_entries = 16;
+
+retry:
+
+	entries = malloc(sizeof(*entries) * num_entries);
+	bzero(&port_list, sizeof(port_list));
+	port_list.entries = entries;
+	port_list.alloc_num = num_entries;
+	port_list.alloc_len = num_entries * sizeof(*entries);
+	if (ioctl(fd, CTL_GET_PORT_LIST, &port_list) != 0) {
+		warn("%s: CTL_GET_PORT_LIST ioctl failed", __func__);
+		return (1);
+	}
+	if (port_list.status == CTL_PORT_LIST_NEED_MORE_SPACE) {
+		printf("%s: allocated %d, need %d, retrying\n", __func__,
+		       num_entries, port_list.fill_num + port_list.dropped_num);
+		free(entries);
+		num_entries = port_list.fill_num + port_list.dropped_num;
+		goto retry;
+	}
+
+	if ((quiet == 0)
+	 && (xml == 0))
+		printf("Port Online Type     Name         pp vp %-18s %-18s\n",
+		       "WWNN", "WWPN");
+
+	if (xml != 0) {
+		sb = sbuf_new_auto();
+		sbuf_printf(sb, "<ctlfelist>\n");
+	}
+	for (i = 0; i < port_list.fill_num; i++) {
+		struct ctl_port_entry *entry;
+		const char *type;
+
+		entry = &entries[i];
+
+		switch (entry->port_type) {
+		case CTL_PORT_FC:
+			type = "FC";
+			break;
+		case CTL_PORT_SCSI:
+			type = "SCSI";
+			break;
+		case CTL_PORT_IOCTL:
+			type = "IOCTL";
+			break;
+		case CTL_PORT_INTERNAL:
+			type = "INTERNAL";
+			break;
+		case CTL_PORT_ISC:
+			type = "ISC";
+			break;
+		default:
+			type = "UNKNOWN";
+			break;
+		}
+
+		/*
+		 * If the user specified a frontend number or a particular
+		 * frontend type, only print out that particular frontend
+		 * or frontend type.
+		 */
+		if ((targ_port != -1)
+		 && (targ_port != entry->targ_port))
+			continue;
+		else if ((port_type != CTL_PORT_NONE)
+		      && ((port_type & entry->port_type) == 0))
+			continue;
+
+		did_print = 1;
+
+#if 0
+		printf("Num: %ju Type: %s (%#x) Name: %s Physical Port: %d "
+		       "Virtual Port: %d\n", (uintmax_t)entry->fe_num, type,
+		       entry->port_type, entry->fe_name, entry->physical_port,
+		       entry->virtual_port);
+		printf("WWNN %#jx WWPN %#jx Online: %s\n",
+		       (uintmax_t)entry->wwnn, (uintmax_t)entry->wwpn,
+		       (entry->online) ? "YES" : "NO" );
+#endif
+		if (xml == 0) {
+			printf("%-4d %-6s %-8s %-12s %-2d %-2d %#-18jx "
+			       "%#-18jx\n",
+			       entry->targ_port, (entry->online) ? "YES" : "NO",
+			       type, entry->port_name, entry->physical_port,
+			       entry->virtual_port, (uintmax_t)entry->wwnn,
+			       (uintmax_t)entry->wwpn);
+		} else {
+			sbuf_printf(sb, "<targ_port id=\"%d\">\n",
+				    entry->targ_port);
+			sbuf_printf(sb, "<online>%s</online>\n",
+				    (entry->online) ? "YES" : "NO");
+			sbuf_printf(sb, "<port_type>%s</port_type>\n", type);
+			sbuf_printf(sb, "<port_name>%s</port_name>\n",
+				    entry->port_name);
+			sbuf_printf(sb, "<physical_port>%d</physical_port>\n",
+				    entry->physical_port);
+			sbuf_printf(sb, "<virtual_port>%d</virtual_port>\n",
+				    entry->virtual_port);
+			sbuf_printf(sb, "<wwnn>%#jx</wwnn>\n",
+				    (uintmax_t)entry->wwnn);
+			sbuf_printf(sb, "<wwpn>%#jx</wwpn>\n",
+				    (uintmax_t)entry->wwpn);
+			sbuf_printf(sb, "</targ_port>\n");
+		}
+
+	}
+	if (xml != 0) {
+		sbuf_printf(sb, "</ctlfelist>\n");
+		sbuf_finish(sb);
+		printf("%s", sbuf_data(sb));
+		sbuf_delete(sb);
+	}
+
+	/*
+	 * Give some indication that we didn't find the frontend or
+	 * frontend type requested by the user.  We could print something
+	 * out, but it would probably be better to hide that behind a
+	 * verbose flag.
+	 */
+	if ((did_print == 0)
+	 && ((targ_port != -1)
+	  || (port_type != CTL_PORT_NONE)))
+		return (1);
+	else
+		return (0);
+}
+
+typedef enum {
+	CCTL_PORT_MODE_NONE,
+	CCTL_PORT_MODE_LIST,
+	CCTL_PORT_MODE_SET,
+	CCTL_PORT_MODE_ON,
+	CCTL_PORT_MODE_OFF
+} cctl_port_mode;
+
+struct ctladm_opts cctl_fe_table[] = {
+	{"fc", CTL_PORT_FC, CTLADM_ARG_NONE, NULL},
+	{"scsi", CTL_PORT_SCSI, CTLADM_ARG_NONE, NULL},
+	{"internal", CTL_PORT_INTERNAL, CTLADM_ARG_NONE, NULL},
+	{"all", CTL_PORT_ALL, CTLADM_ARG_NONE, NULL},
+	{NULL, 0, 0, NULL}
+};
+
+static int
+cctl_port(int fd, int argc, char **argv, char *combinedopt)
+{
+	int c;
+	int32_t targ_port = -1;
+	int retval = 0;
+	int wwnn_set = 0, wwpn_set = 0;
+	uint64_t wwnn = 0, wwpn = 0;
+	cctl_port_mode port_mode = CCTL_PORT_MODE_NONE;
+	struct ctl_port_entry entry;
+	ctl_port_type port_type = CTL_PORT_NONE;
+	int quiet = 0, xml = 0;
+
+	while ((c = getopt(argc, argv, combinedopt)) != -1) {
+		switch (c) {
+		case 'l':
+			if (port_mode != CCTL_PORT_MODE_NONE)
+				goto bailout_badarg;
+
+			port_mode = CCTL_PORT_MODE_LIST;
+			break;
+		case 'o':
+			if (port_mode != CCTL_PORT_MODE_NONE)
+				goto bailout_badarg;
+			
+			if (strcasecmp(optarg, "on") == 0)
+				port_mode = CCTL_PORT_MODE_ON;
+			else if (strcasecmp(optarg, "off") == 0)
+				port_mode = CCTL_PORT_MODE_OFF;
+			else {
+				warnx("Invalid -o argument %s, \"on\" or "
+				      "\"off\" are the only valid args",
+				      optarg);
+				retval = 1;
+				goto bailout;
+			}
+			break;
+		case 'p':
+			targ_port = strtol(optarg, NULL, 0);
+			break;
+		case 'q':
+			quiet = 1;
+			break;
+		case 't': {
+			ctladm_optret optret;
+			ctladm_cmdargs argnum;
+			const char *subopt;
+			ctl_port_type tmp_port_type;
+
+			optret = getoption(cctl_fe_table, optarg, &tmp_port_type,
+					   &argnum, &subopt);
+			if (optret == CC_OR_AMBIGUOUS) {
+				warnx("%s: ambiguous frontend type %s",
+				      __func__, optarg);
+				retval = 1;
+				goto bailout;
+			} else if (optret == CC_OR_NOT_FOUND) {
+				warnx("%s: invalid frontend type %s",
+				      __func__, optarg);
+				retval = 1;
+				goto bailout;
+			}
+
+			port_type |= tmp_port_type;
+			break;
+		}
+		case 'w':
+			if ((port_mode != CCTL_PORT_MODE_NONE)
+			 && (port_mode != CCTL_PORT_MODE_SET))
+				goto bailout_badarg;
+
+			port_mode = CCTL_PORT_MODE_SET;
+
+			wwnn = strtoull(optarg, NULL, 0);
+			wwnn_set = 1;
+			break;
+		case 'W':
+			if ((port_mode != CCTL_PORT_MODE_NONE)
+			 && (port_mode != CCTL_PORT_MODE_SET))
+				goto bailout_badarg;
+
+			port_mode = CCTL_PORT_MODE_SET;
+
+			wwpn = strtoull(optarg, NULL, 0);
+			wwpn_set = 1;
+			break;
+		case 'x':
+			xml = 1;
+			break;
+		}
+	}
+
+	/*
+	 * The user can specify either one or more frontend types (-t), or
+	 * a specific frontend, but not both.
+	 *
+	 * If the user didn't specify a frontend type or number, set it to
+	 * all.  This is primarily needed for the enable/disable ioctls.
+	 * This will be a no-op for the listing code.  For the set ioctl,
+	 * we'll throw an error, since that only works on one port at a time.
+	 */
+	if ((port_type != CTL_PORT_NONE) && (targ_port != -1)) {
+		warnx("%s: can only specify one of -t or -n", __func__);
+		retval = 1;
+		goto bailout;
+	} else if ((targ_port == -1) && (port_type == CTL_PORT_NONE))
+		port_type = CTL_PORT_ALL;
+
+	bzero(&entry, sizeof(&entry));
+
+	/*
+	 * These are needed for all but list/dump mode.
+	 */
+	entry.port_type = port_type;
+	entry.targ_port = targ_port;
+
+	switch (port_mode) {
+	case CCTL_PORT_MODE_LIST:
+		cctl_port_dump(fd, quiet, xml, targ_port, port_type);
+		break;
+	case CCTL_PORT_MODE_SET:
+		if (targ_port == -1) {
+			warnx("%s: -w and -W require -n", __func__);
+			retval = 1;
+			goto bailout;
+		}
+
+		if (wwnn_set) {
+			entry.flags |= CTL_PORT_WWNN_VALID;
+			entry.wwnn = wwnn;
+		}
+		if (wwpn_set) {
+			entry.flags |= CTL_PORT_WWPN_VALID;
+			entry.wwpn = wwpn;
+		}
+
+		if (ioctl(fd, CTL_SET_PORT_WWNS, &entry) == -1) {
+			warn("%s: CTL_SET_PORT_WWNS ioctl failed", __func__);
+			retval = 1;
+			goto bailout;
+		}
+		break;
+	case CCTL_PORT_MODE_ON:
+		if (ioctl(fd, CTL_ENABLE_PORT, &entry) == -1) {
+			warn("%s: CTL_ENABLE_PORT ioctl failed", __func__);
+			retval = 1;
+			goto bailout;
+		}
+		fprintf(stdout, "Front End Ports enabled\n");
+		break;
+	case CCTL_PORT_MODE_OFF:
+		if (ioctl(fd, CTL_DISABLE_PORT, &entry) == -1) {
+			warn("%s: CTL_DISABLE_PORT ioctl failed", __func__);
+			retval = 1;
+			goto bailout;
+		}
+		fprintf(stdout, "Front End Ports disabled\n");
+		break;
+	default:
+		warnx("%s: one of -l, -o or -w/-W must be specified", __func__);
+		retval = 1;
+		goto bailout;
+		break;
+	}
+
+bailout:
+
+	return (retval);
+
+bailout_badarg:
+	warnx("%s: only one of -l, -o or -w/-W may be specified", __func__);
+	return (1);
+}
+
+static int
+cctl_do_io(int fd, int retries, union ctl_io *io, const char *func)
+{
+	do {
+		if (ioctl(fd, CTL_IO, io) == -1) {
+			warn("%s: error sending CTL_IO ioctl", func);
+			return (-1);
+		}
+	} while (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
+	      && (retries-- > 0));
+
+	return (0);
+}
+
+static int
+cctl_delay(int fd, int target, int lun, int argc, char **argv,
+	   char *combinedopt)
+{
+	int datamove_delay;
+	struct ctl_io_delay_info delay_info;
+	char *delayloc = NULL;
+	char *delaytype = NULL;
+	int delaytime = -1;
+	int retval;
+	int c;
+
+	retval = 0;
+	datamove_delay = 0;
+
+	memset(&delay_info, 0, sizeof(delay_info));
+
+	while ((c = getopt(argc, argv, combinedopt)) != -1) {
+		switch (c) {
+		case 'T':
+			delaytype = strdup(optarg);
+			break;
+		case 'l':
+			delayloc = strdup(optarg);
+			break;
+		case 't':
+			delaytime = strtoul(optarg, NULL, 0);
+			break;
+		}
+	}
+
+	if (delaytime == -1) {
+		warnx("%s: you must specify the delaytime with -t", __func__);
+		retval = 1;
+		goto bailout;
+	}
+
+	if (strcasecmp(delayloc, "datamove") == 0)
+		delay_info.delay_loc = CTL_DELAY_LOC_DATAMOVE;
+	else if (strcasecmp(delayloc, "done") == 0)
+		delay_info.delay_loc = CTL_DELAY_LOC_DONE;
+	else {
+		warnx("%s: invalid delay location %s", __func__, delayloc);
+		retval = 1;
+		goto bailout;
+	}
+
+	if ((delaytype == NULL)
+	 || (strcmp(delaytype, "oneshot") == 0))
+		delay_info.delay_type = CTL_DELAY_TYPE_ONESHOT;
+	else if (strcmp(delaytype, "cont") == 0)
+		delay_info.delay_type = CTL_DELAY_TYPE_CONT;
+	else {
+		warnx("%s: invalid delay type %s", __func__, delaytype);
+		retval = 1;
+		goto bailout;
+	}
+
+	delay_info.target_id = target;
+	delay_info.lun_id = lun;
+	delay_info.delay_secs = delaytime;
+
+	if (ioctl(fd, CTL_DELAY_IO, &delay_info) == -1) {
+		warn("%s: CTL_DELAY_IO ioctl failed", __func__);
+		retval = 1;
+		goto bailout;
+	}
+	switch (delay_info.status) {
+	case CTL_DELAY_STATUS_NONE:
+		warnx("%s: no delay status??", __func__);
+		retval = 1;
+		break;
+	case CTL_DELAY_STATUS_OK:
+		break;
+	case CTL_DELAY_STATUS_INVALID_LUN:
+		warnx("%s: invalid lun %d", __func__, lun);
+		retval = 1;
+		break;
+	case CTL_DELAY_STATUS_INVALID_TYPE:
+		warnx("%s: invalid delay type %d", __func__,
+		      delay_info.delay_type);
+		retval = 1;
+		break;
+	case CTL_DELAY_STATUS_INVALID_LOC:
+		warnx("%s: delay location %s not implemented?", __func__,
+		      delayloc);
+		retval = 1;
+		break;
+	case CTL_DELAY_STATUS_NOT_IMPLEMENTED:
+		warnx("%s: delay not implemented in the kernel", __func__);
+		warnx("%s: recompile with the CTL_IO_DELAY flag set", __func__);
+		retval = 1;
+		break;
+	default:
+		warnx("%s: unknown delay return status %d", __func__,
+		      delay_info.status);
+		retval = 1;
+		break;
+	}
+bailout:
+
+	/* delayloc should never be NULL, but just in case...*/
+	if (delayloc != NULL)
+		free(delayloc);
+
+	return (retval);
+}
+
+static int
+cctl_realsync(int fd, int argc, char **argv)
+{
+	int syncstate;
+	int retval;
+	char *syncarg;
+
+	retval = 0;
+
+	if (argc != 3) {
+		warnx("%s %s takes exactly one argument", argv[0], argv[1]);
+		retval = 1;
+		goto bailout;
+	}
+
+	syncarg = argv[2];
+
+	if (strncasecmp(syncarg, "query", min(strlen(syncarg),
+			strlen("query"))) == 0) {
+		if (ioctl(fd, CTL_REALSYNC_GET, &syncstate) == -1) {
+			warn("%s: CTL_REALSYNC_GET ioctl failed", __func__);
+			retval = 1;
+			goto bailout;
+		}
+		fprintf(stdout, "SYNCHRONIZE CACHE support is: ");
+		switch (syncstate) {
+		case 0:
+			fprintf(stdout, "OFF\n");
+			break;
+		case 1:
+			fprintf(stdout, "ON\n");
+			break;
+		default:
+			fprintf(stdout, "unknown (%d)\n", syncstate);
+			break;
+		}
+		goto bailout;
+	} else if (strcasecmp(syncarg, "on") == 0) {
+		syncstate = 1;
+	} else if (strcasecmp(syncarg, "off") == 0) {
+		syncstate = 0;
+	} else {
+		warnx("%s: invalid realsync argument %s", __func__, syncarg);
+		retval = 1;
+		goto bailout;
+	}
+
+	if (ioctl(fd, CTL_REALSYNC_SET, &syncstate) == -1) {
+		warn("%s: CTL_REALSYNC_SET ioctl failed", __func__);
+		retval = 1;
+		goto bailout;
+	}
+bailout:
+	return (retval);
+}
+
+static int
+cctl_getsetsync(int fd, int target, int lun, ctladm_cmdfunction command,
+		int argc, char **argv, char *combinedopt)
+{
+	struct ctl_sync_info sync_info;
+	uint32_t ioctl_cmd;
+	int sync_interval = -1;
+	int retval;
+	int c;
+
+	retval = 0;
+
+	memset(&sync_info, 0, sizeof(sync_info));
+	sync_info.target_id = target;
+	sync_info.lun_id = lun;
+
+	while ((c = getopt(argc, argv, combinedopt)) != -1) {
+		switch (c) {
+		case 'i':
+			sync_interval = strtoul(optarg, NULL, 0);
+			break;
+		default:
+			break;
+		}
+	}
+
+	if (command == CTLADM_CMD_SETSYNC) {
+		if (sync_interval == -1) {
+			warnx("%s: you must specify the sync interval with -i",
+			      __func__);
+			retval = 1;
+			goto bailout;
+		}
+		sync_info.sync_interval = sync_interval;
+		ioctl_cmd = CTL_SETSYNC;
+	} else {
+		ioctl_cmd = CTL_GETSYNC;
+	}
+
+	if (ioctl(fd, ioctl_cmd, &sync_info) == -1) {
+		warn("%s: CTL_%sSYNC ioctl failed", __func__,
+		     (command == CTLADM_CMD_SETSYNC) ? "SET" : "GET");
+		retval = 1;
+		goto bailout;
+	}
+
+	switch (sync_info.status) {
+	case CTL_GS_SYNC_OK:
+		if (command == CTLADM_CMD_GETSYNC) {
+			fprintf(stdout, "%d:%d: sync interval: %d\n",
+				target, lun, sync_info.sync_interval);
+		}
+		break;
+	case CTL_GS_SYNC_NO_LUN:
+		warnx("%s: unknown target:LUN %d:%d", __func__, target, lun);
+		retval = 1;
+		break;
+	case CTL_GS_SYNC_NONE:
+	default:
+		warnx("%s: unknown CTL_%sSYNC status %d", __func__,
+		      (command == CTLADM_CMD_SETSYNC) ? "SET" : "GET",
+		      sync_info.status);
+		retval = 1;
+		break;
+	}
+bailout:
+	return (retval);
+}
+
+struct ctladm_opts cctl_err_types[] = {
+	{"aborted", CTL_LUN_INJ_ABORTED, CTLADM_ARG_NONE, NULL},
+	{"mediumerr", CTL_LUN_INJ_MEDIUM_ERR, CTLADM_ARG_NONE, NULL},
+	{"ua", CTL_LUN_INJ_UA, CTLADM_ARG_NONE, NULL},
+	{"custom", CTL_LUN_INJ_CUSTOM, CTLADM_ARG_NONE, NULL},
+	{NULL, 0, 0, NULL}
+
+};
+
+struct ctladm_opts cctl_err_patterns[] = {
+	{"read", CTL_LUN_PAT_READ, CTLADM_ARG_NONE, NULL},
+	{"write", CTL_LUN_PAT_WRITE, CTLADM_ARG_NONE, NULL},
+	{"rw", CTL_LUN_PAT_READWRITE, CTLADM_ARG_NONE, NULL},
+	{"readwrite", CTL_LUN_PAT_READWRITE, CTLADM_ARG_NONE, NULL},
+	{"readcap", CTL_LUN_PAT_READCAP, CTLADM_ARG_NONE, NULL},
+	{"tur", CTL_LUN_PAT_TUR, CTLADM_ARG_NONE, NULL},
+	{"any", CTL_LUN_PAT_ANY, CTLADM_ARG_NONE, NULL},
+#if 0
+	{"cmd", CTL_LUN_PAT_CMD,  CTLADM_ARG_NONE, NULL},
+#endif
+	{NULL, 0, 0, NULL}
+};
+
+static int
+cctl_error_inject(int fd, uint32_t target, uint32_t lun, int argc, char **argv, 
+		  char *combinedopt)
+{
+	int retval;
+	struct ctl_error_desc err_desc;
+	uint64_t lba = 0;
+	uint32_t len = 0;
+	uint64_t delete_id = 0;
+	int delete_id_set = 0;
+	int continuous = 0;
+	int sense_len = 0;
+	int fd_sense = 0;
+	int c;
+
+	bzero(&err_desc, sizeof(err_desc));
+	err_desc.target_id = target;
+	err_desc.lun_id = lun;
+
+	while ((c = getopt(argc, argv, combinedopt)) != -1) {
+		switch (c) {
+		case 'c':
+			continuous = 1;
+			break;
+		case 'd':
+			delete_id = strtoull(optarg, NULL, 0);
+			delete_id_set = 1;
+			break;
+		case 'i':
+		case 'p': {
+			ctladm_optret optret;
+			ctladm_cmdargs argnum;
+			const char *subopt;
+
+			if (c == 'i') {
+				ctl_lun_error err_type;
+
+				if (err_desc.lun_error != CTL_LUN_INJ_NONE) {
+					warnx("%s: can't specify multiple -i "
+					      "arguments", __func__);
+					retval = 1;
+					goto bailout;
+				}
+				optret = getoption(cctl_err_types, optarg,
+						   &err_type, &argnum, &subopt);
+				err_desc.lun_error = err_type;
+			} else {
+				ctl_lun_error_pattern pattern;
+
+				optret = getoption(cctl_err_patterns, optarg,
+						   &pattern, &argnum, &subopt);
+				err_desc.error_pattern |= pattern;
+			}
+
+			if (optret == CC_OR_AMBIGUOUS) {
+				warnx("%s: ambiguous argument %s", __func__,
+				      optarg);
+				retval = 1;
+				goto bailout;
+			} else if (optret == CC_OR_NOT_FOUND) {
+				warnx("%s: argument %s not found", __func__,
+				      optarg);
+				retval = 1;
+				goto bailout;
+			}
+			break;
+		}
+		case 'r': {
+			char *tmpstr, *tmpstr2;
+
+			tmpstr = strdup(optarg);
+			if (tmpstr == NULL) {
+				warn("%s: error duplicating string %s",
+				     __func__, optarg);
+				retval = 1;
+				goto bailout;
+			}
+
+			tmpstr2 = strsep(&tmpstr, ",");
+			if (tmpstr2 == NULL) {
+				warnx("%s: invalid -r argument %s", __func__,
+				      optarg);
+				retval = 1;
+				free(tmpstr);
+				goto bailout;
+			}
+			lba = strtoull(tmpstr2, NULL, 0);
+			tmpstr2 = strsep(&tmpstr, ",");
+			if (tmpstr2 == NULL) {
+				warnx("%s: no len argument for -r lba,len, got"
+				      " %s", __func__, optarg);
+				retval = 1;
+				free(tmpstr);
+				goto bailout;
+			}
+			len = strtoul(tmpstr2, NULL, 0);
+			free(tmpstr);
+			break;
+		}
+		case 's': {
+			struct get_hook hook;
+			char *sensestr;
+
+			sense_len = strtol(optarg, NULL, 0);
+			if (sense_len <= 0) {
+				warnx("invalid number of sense bytes %d",
+				      sense_len);
+				retval = 1;
+				goto bailout;
+			}
+
+			sense_len = MIN(sense_len, SSD_FULL_SIZE);
+
+			hook.argc = argc - optind;
+			hook.argv = argv + optind;
+			hook.got = 0;
+
+			sensestr = cget(&hook, NULL);
+			if ((sensestr != NULL)
+			 && (sensestr[0] == '-')) {
+				fd_sense = 1;
+			} else {
+				buff_encode_visit(
+				    (uint8_t *)&err_desc.custom_sense,
+				    sense_len, sensestr, iget, &hook);
+			}
+			optind += hook.got;
+			break;
+		}
+		default:
+			break;
+		}
+	}
+
+	if (delete_id_set != 0) {
+		err_desc.serial = delete_id;
+		if (ioctl(fd, CTL_ERROR_INJECT_DELETE, &err_desc) == -1) {
+			warn("%s: error issuing CTL_ERROR_INJECT_DELETE ioctl",
+			     __func__);
+			retval = 1;
+		}
+		goto bailout;
+	}
+
+	if (err_desc.lun_error == CTL_LUN_INJ_NONE) {
+		warnx("%s: error injection command (-i) needed",
+		      __func__);
+		retval = 1;
+		goto bailout;
+	} else if ((err_desc.lun_error == CTL_LUN_INJ_CUSTOM)
+		&& (sense_len == 0)) {
+		warnx("%s: custom error requires -s", __func__);
+		retval = 1;
+		goto bailout;
+	}
+
+	if (continuous != 0)
+		err_desc.lun_error |= CTL_LUN_INJ_CONTINUOUS;
+
+	/*
+	 * If fd_sense is set, we need to read the sense data the user
+	 * wants returned from stdin.
+	 */
+        if (fd_sense == 1) {
+		ssize_t amt_read;
+		int amt_to_read = sense_len;
+ 		u_int8_t *buf_ptr = (uint8_t *)&err_desc.custom_sense;
+
+		for (amt_read = 0; amt_to_read > 0;
+		     amt_read = read(STDIN_FILENO, buf_ptr, amt_to_read)) {
+			if (amt_read == -1) {
+         			warn("error reading sense data from stdin");
+				retval = 1;
+				goto bailout;
+			}
+			amt_to_read -= amt_read;
+			buf_ptr += amt_read;
+		}
+	}
+
+	if (err_desc.error_pattern == CTL_LUN_PAT_NONE) {
+		warnx("%s: command pattern (-p) needed", __func__);
+		retval = 1;
+		goto bailout;
+	}
+
+	if (len != 0) {
+		err_desc.error_pattern |= CTL_LUN_PAT_RANGE;
+		/*
+		 * We could check here to see whether it's a read/write
+		 * command, but that will be pointless once we allow
+		 * custom patterns.  At that point, the user could specify
+		 * a READ(6) CDB type, and we wouldn't have an easy way here
+		 * to verify whether range checking is possible there.  The
+		 * user will just figure it out when his error never gets
+		 * executed.
+		 */
+#if 0
+		if ((err_desc.pattern & CTL_LUN_PAT_READWRITE) == 0) {
+			warnx("%s: need read and/or write pattern if range "
+			      "is specified", __func__);
+			retval = 1;
+			goto bailout;
+		}
+#endif
+		err_desc.lba_range.lba = lba;
+		err_desc.lba_range.len = len;
+	}
+
+	if (ioctl(fd, CTL_ERROR_INJECT, &err_desc) == -1) {
+		warn("%s: error issuing CTL_ERROR_INJECT ioctl", __func__);
+		retval = 1;
+	} else {
+		printf("Error injection succeeded, serial number is %ju\n",
+		       (uintmax_t)err_desc.serial);
+	}
+bailout:
+
+	return (retval);
+}
+
+static int
+cctl_lunlist(int fd)
+{
+	struct scsi_report_luns_data *lun_data;
+	struct scsi_inquiry_data *inq_data;
+	uint32_t num_luns;
+	int target;
+	int initid;
+	unsigned int i;
+	int retval;
+
+	retval = 0;
+	inq_data = NULL;
+
+	target = 6;
+	initid = 7;
+
+	/*
+	 * XXX KDM assuming LUN 0 is fine, but we may need to change this
+	 * if we ever acquire the ability to have multiple targets.
+	 */
+	if ((retval = cctl_get_luns(fd, target, /*lun*/ 0, initid,
+				    /*retries*/ 2, &lun_data, &num_luns)) != 0)
+		goto bailout;
+
+	inq_data = malloc(sizeof(*inq_data));
+	if (inq_data == NULL) {
+		warn("%s: couldn't allocate memory for inquiry data\n",
+		     __func__);
+		retval = 1;
+		goto bailout;
+	}
+	for (i = 0; i < num_luns; i++) {
+		char scsi_path[40];
+		int lun_val;
+
+		switch (lun_data->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) {
+		case RPL_LUNDATA_ATYP_PERIPH:
+			lun_val = lun_data->luns[i].lundata[1];
+			break;
+		case RPL_LUNDATA_ATYP_FLAT:
+			lun_val = (lun_data->luns[i].lundata[0] &
+				RPL_LUNDATA_FLAT_LUN_MASK) |
+				(lun_data->luns[i].lundata[1] <<
+				RPL_LUNDATA_FLAT_LUN_BITS);
+			break;
+		case RPL_LUNDATA_ATYP_LUN:
+		case RPL_LUNDATA_ATYP_EXTLUN:
+		default:
+			fprintf(stdout, "Unsupported LUN format %d\n",
+				lun_data->luns[i].lundata[0] &
+				RPL_LUNDATA_ATYP_MASK);
+			lun_val = -1;
+			break;
+		}
+		if (lun_val == -1)
+			continue;
+
+		if ((retval = cctl_get_inquiry(fd, target, lun_val, initid,
+					       /*retries*/ 2, scsi_path,
+					       sizeof(scsi_path),
+					       inq_data)) != 0) {
+			goto bailout;
+		}
+		printf("%s", scsi_path);
+		scsi_print_inquiry(inq_data);
+	}
+bailout:
+
+	if (lun_data != NULL)
+		free(lun_data);
+
+	if (inq_data != NULL)
+		free(inq_data);
+
+	return (retval);
+}
+
+static void
+cctl_cfi_mt_statusstr(cfi_mt_status status, char *str, int str_len)
+{
+	switch (status) {
+	case CFI_MT_PORT_OFFLINE:
+		snprintf(str, str_len, "Port Offline");
+		break;
+	case CFI_MT_ERROR:
+		snprintf(str, str_len, "Error");
+		break;
+	case CFI_MT_SUCCESS:
+		snprintf(str, str_len, "Success");
+		break;
+	case CFI_MT_NONE:
+		snprintf(str, str_len, "None??");
+		break;
+	default:
+		snprintf(str, str_len, "Unknown status: %d", status);
+		break;
+	}
+}
+
+static void
+cctl_cfi_bbr_statusstr(cfi_bbrread_status status, char *str, int str_len)
+{
+	switch (status) {
+	case CFI_BBR_SUCCESS:
+		snprintf(str, str_len, "Success");
+		break;
+	case CFI_BBR_LUN_UNCONFIG:
+		snprintf(str, str_len, "LUN not configured");
+		break;
+	case CFI_BBR_NO_LUN:
+		snprintf(str, str_len, "LUN does not exist");
+		break;
+	case CFI_BBR_NO_MEM:
+		snprintf(str, str_len, "Memory allocation error");
+		break;
+	case CFI_BBR_BAD_LEN:
+		snprintf(str, str_len, "Length is not a multiple of blocksize");
+		break;
+	case CFI_BBR_RESERV_CONFLICT:
+		snprintf(str, str_len, "Reservation conflict");
+		break;
+	case CFI_BBR_LUN_STOPPED:
+		snprintf(str, str_len, "LUN is powered off");
+		break;
+	case CFI_BBR_LUN_OFFLINE_CTL:
+		snprintf(str, str_len, "LUN is offline");
+		break;
+	case CFI_BBR_LUN_OFFLINE_RC:
+		snprintf(str, str_len, "RAIDCore array is offline (double "
+			 "failure?)");
+		break;
+	case CFI_BBR_SCSI_ERROR:
+		snprintf(str, str_len, "SCSI Error");
+		break;
+	case CFI_BBR_ERROR:
+		snprintf(str, str_len, "Error");
+		break;
+	default:
+		snprintf(str, str_len, "Unknown status: %d", status);
+		break;
+	}
+}
+
+static int
+cctl_hardstopstart(int fd, ctladm_cmdfunction command)
+{
+	struct ctl_hard_startstop_info hs_info;
+	char error_str[256];
+	int do_start;
+	int retval;
+
+	retval = 0;
+
+	if (command == CTLADM_CMD_HARDSTART)
+		do_start = 1;
+	else
+		do_start = 0;
+
+	if (ioctl(fd, (do_start == 1) ? CTL_HARD_START : CTL_HARD_STOP,
+		  &hs_info) == -1) {
+		warn("%s: CTL_HARD_%s ioctl failed", __func__,
+		     (do_start == 1) ? "START" : "STOP");
+		retval = 1;
+		goto bailout;
+	}
+
+	fprintf(stdout, "Hard %s Status: ", (command == CTLADM_CMD_HARDSTOP) ?
+		"Stop" : "Start");
+	cctl_cfi_mt_statusstr(hs_info.status, error_str, sizeof(error_str));
+	fprintf(stdout, "%s\n", error_str);
+	fprintf(stdout, "Total LUNs: %d\n", hs_info.total_luns);
+	fprintf(stdout, "LUNs complete: %d\n", hs_info.luns_complete);
+	fprintf(stdout, "LUNs failed: %d\n", hs_info.luns_failed);
+
+bailout:
+	return (retval);
+}
+
+static int
+cctl_bbrread(int fd, int target __unused, int lun, int iid __unused,
+	     int argc, char **argv, char *combinedopt)
+{
+	struct ctl_bbrread_info bbr_info;
+	char error_str[256];
+	int datalen = -1;
+	uint64_t lba = 0;
+	int lba_set = 0;
+	int retval;
+	int c;
+
+	retval = 0;
+
+	while ((c = getopt(argc, argv, combinedopt)) != -1) {
+		switch (c) {
+		case 'd':
+			datalen = strtoul(optarg, NULL, 0);
+			break;
+		case 'l':
+			lba = strtoull(optarg, NULL, 0);
+			lba_set = 1;
+			break;
+		default:
+			break;
+		}
+	}
+
+	if (lba_set == 0) {
+		warnx("%s: you must specify an LBA with -l", __func__);
+		retval = 1;
+		goto bailout;
+	}
+
+	if (datalen == -1) {
+		warnx("%s: you must specify a length with -d", __func__);
+		retval = 1;
+		goto bailout;
+	}
+
+	bbr_info.lun_num = lun;
+	bbr_info.lba = lba;
+	/*
+	 * XXX KDM get the blocksize first??
+	 */
+	if ((datalen % 512) != 0) {
+		warnx("%s: data length %d is not a multiple of 512 bytes",
+		     __func__, datalen);
+		retval = 1;
+		goto bailout;
+	}
+	bbr_info.len = datalen;
+
+	if (ioctl(fd, CTL_BBRREAD, &bbr_info) == -1) {
+		warn("%s: CTL_BBRREAD ioctl failed", __func__);
+		retval = 1;
+		goto bailout;
+	}
+	cctl_cfi_mt_statusstr(bbr_info.status, error_str, sizeof(error_str));
+	fprintf(stdout, "BBR Read Overall Status: %s\n", error_str);
+	cctl_cfi_bbr_statusstr(bbr_info.bbr_status, error_str,
+			       sizeof(error_str));
+	fprintf(stdout, "BBR Read Status: %s\n", error_str);
+	/*
+	 * XXX KDM should we bother printing out SCSI status if we get
+	 * CFI_BBR_SCSI_ERROR back?
+	 *
+	 * Return non-zero if this fails?
+	 */
+bailout:
+	return (retval);
+}
+
+static int
+cctl_startup_shutdown(int fd, int target, int lun, int iid,
+		      ctladm_cmdfunction command)
+{
+	union ctl_io *io;
+	struct ctl_id id;
+	struct scsi_report_luns_data *lun_data;
+	struct scsi_inquiry_data *inq_data;
+	uint32_t num_luns;
+	unsigned int i;
+	int retval;
+
+	retval = 0;
+	inq_data = NULL;
+
+	/*
+	 * - report luns
+	 * - step through each lun, do an inquiry
+	 * - check OOA queue on direct access luns
+	 * - send stop with offline bit to each direct access device with a
+	 *   clear OOA queue
+	 *   - if we get a reservation conflict, reset the LUN to clear it
+	 *     and reissue the stop with the offline bit set
+	 */
+
+	id.id = iid;
+
+	io = ctl_scsi_alloc_io(id);
+	if (io == NULL) {
+		warnx("%s: can't allocate memory", __func__);
+		return (1);
+	}
+
+	if ((retval = cctl_get_luns(fd, target, lun, iid, /*retries*/ 2,
+				    &lun_data, &num_luns)) != 0)
+		goto bailout;
+
+	inq_data = malloc(sizeof(*inq_data));
+	if (inq_data == NULL) {
+		warn("%s: couldn't allocate memory for inquiry data\n",
+		     __func__);
+		retval = 1;
+		goto bailout;
+	}
+	for (i = 0; i < num_luns; i++) {
+		char scsi_path[40];
+		int lun_val;
+
+		/*
+		 * XXX KDM figure out a way to share this code with
+		 * cctl_lunlist()?
+		 */
+		switch (lun_data->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) {
+		case RPL_LUNDATA_ATYP_PERIPH:
+			lun_val = lun_data->luns[i].lundata[1];
+			break;
+		case RPL_LUNDATA_ATYP_FLAT:
+			lun_val = (lun_data->luns[i].lundata[0] &
+				RPL_LUNDATA_FLAT_LUN_MASK) |
+				(lun_data->luns[i].lundata[1] <<
+				RPL_LUNDATA_FLAT_LUN_BITS);
+			break;
+		case RPL_LUNDATA_ATYP_LUN:
+		case RPL_LUNDATA_ATYP_EXTLUN:
+		default:
+			fprintf(stdout, "Unsupported LUN format %d\n",
+				lun_data->luns[i].lundata[0] &
+				RPL_LUNDATA_ATYP_MASK);
+			lun_val = -1;
+			break;
+		}
+		if (lun_val == -1)
+			continue;
+
+		if ((retval = cctl_get_inquiry(fd, target, lun_val, iid,
+					       /*retries*/ 2, scsi_path,
+					       sizeof(scsi_path),
+					       inq_data)) != 0) {
+			goto bailout;
+		}
+		printf("%s", scsi_path);
+		scsi_print_inquiry(inq_data);
+		/*
+		 * We only want to shutdown direct access devices.
+		 */
+		if (SID_TYPE(inq_data) != T_DIRECT) {
+			printf("%s LUN is not direct access, skipped\n",
+			       scsi_path);
+			continue;
+		}
+
+		if (command == CTLADM_CMD_SHUTDOWN) {
+			struct ctl_ooa_info ooa_info;
+
+			ooa_info.target_id = target;
+			ooa_info.lun_id = lun_val;
+
+			if (ioctl(fd, CTL_CHECK_OOA, &ooa_info) == -1) {
+				printf("%s CTL_CHECK_OOA ioctl failed\n",
+				       scsi_path);
+				continue;
+			}
+
+			if (ooa_info.status != CTL_OOA_SUCCESS) {
+				printf("%s CTL_CHECK_OOA returned status %d\n", 
+				       scsi_path, ooa_info.status);
+				continue;
+			}
+			if (ooa_info.num_entries != 0) {
+				printf("%s %d entr%s in the OOA queue, "
+				       "skipping shutdown\n", scsi_path,
+				       ooa_info.num_entries,
+				       (ooa_info.num_entries > 1)?"ies" : "y" );
+				continue;
+			}
+		}
+		
+		ctl_scsi_start_stop(/*io*/ io,
+				    /*start*/(command == CTLADM_CMD_STARTUP) ?
+					      1 : 0,
+				    /*load_eject*/ 0,
+				    /*immediate*/ 0,
+				    /*power_conditions*/ SSS_PC_START_VALID,
+				    /*onoffline*/ 1,
+				    /*ctl_tag_type*/
+				    (command == CTLADM_CMD_STARTUP) ?
+				    CTL_TAG_SIMPLE :CTL_TAG_ORDERED,
+				    /*control*/ 0);
+
+		io->io_hdr.nexus.targ_target.id = target;
+		io->io_hdr.nexus.targ_lun = lun_val;
+		io->io_hdr.nexus.initid = id;
+
+		if (cctl_do_io(fd, /*retries*/ 3, io, __func__) != 0) {
+			retval = 1;
+			goto bailout;
+		}
+
+		if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
+			ctl_io_error_print(io, inq_data, stderr);
+		else {
+			printf("%s LUN is now %s\n", scsi_path,
+			       (command == CTLADM_CMD_STARTUP) ? "online" :
+			       "offline");
+		}
+	}
+bailout:
+	if (lun_data != NULL)
+		free(lun_data);
+
+	if (inq_data != NULL)
+		free(inq_data);
+
+	if (io != NULL)
+		ctl_scsi_free_io(io);
+
+	return (retval);
+}
+
+static int
+cctl_sync_cache(int fd, int target, int lun, int iid, int retries,
+		int argc, char **argv, char *combinedopt)
+{
+	union ctl_io *io;
+	struct ctl_id id;
+	int cdb_size = -1;
+	int retval;
+	uint64_t our_lba = 0;
+	uint32_t our_block_count = 0;
+	int reladr = 0, immed = 0; 
+	int c;
+
+	id.id = iid;
+	retval = 0;
+
+	io = ctl_scsi_alloc_io(id);
+	if (io == NULL) {
+		warnx("%s: can't allocate memory", __func__);
+		return (1);
+	}
+
+	while ((c = getopt(argc, argv, combinedopt)) != -1) {
+		switch (c) {
+		case 'b':
+			our_block_count = strtoul(optarg, NULL, 0);
+			break;
+		case 'c':
+			cdb_size = strtol(optarg, NULL, 0);
+			break;
+		case 'i':
+			immed = 1;
+			break;
+		case 'l':
+			our_lba = strtoull(optarg, NULL, 0);
+			break;
+		case 'r':
+			reladr = 1;
+			break;
+		default:
+			break;
+		}
+	}
+
+	if (cdb_size != -1) {
+		switch (cdb_size) {
+		case 10:
+		case 16:
+			break;
+		default:
+			warnx("%s: invalid cdbsize %d, valid sizes are 10 "
+			      "and 16", __func__, cdb_size);
+			retval = 1;
+			goto bailout;
+			break; /* NOTREACHED */
+		}
+	} else
+		cdb_size = 10;
+
+	ctl_scsi_sync_cache(/*io*/ io,
+			    /*immed*/ immed,
+			    /*reladr*/ reladr,
+			    /*minimum_cdb_size*/ cdb_size,
+			    /*starting_lba*/ our_lba,
+			    /*block_count*/ our_block_count,
+			    /*tag_type*/ CTL_TAG_SIMPLE,
+			    /*control*/ 0);
+
+	io->io_hdr.nexus.targ_target.id = target;
+	io->io_hdr.nexus.targ_lun = lun;
+	io->io_hdr.nexus.initid = id;
+
+	if (cctl_do_io(fd, retries, io, __func__) != 0) {
+		retval = 1;
+		goto bailout;
+	}
+
+	if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
+		fprintf(stdout, "Cache synchronized successfully\n");
+	} else
+		ctl_io_error_print(io, NULL, stderr);
+bailout:
+	ctl_scsi_free_io(io);
+
+	return (retval);
+}
+
+static int
+cctl_start_stop(int fd, int target, int lun, int iid, int retries, int start,
+		int argc, char **argv, char *combinedopt)
+{
+	union ctl_io *io;
+	struct ctl_id id;
+	char scsi_path[40];
+	int immed = 0, onoffline = 0;
+	int retval, c;
+
+	id.id = iid;
+	retval = 0;
+
+	io = ctl_scsi_alloc_io(id);
+	if (io == NULL) {
+		warnx("%s: can't allocate memory", __func__);
+		return (1);
+	}
+
+	while ((c = getopt(argc, argv, combinedopt)) != -1) {
+		switch (c) {
+		case 'i':
+			immed = 1;
+			break;
+		case 'o':
+			onoffline = 1;
+			break;
+		default:
+			break;
+		}
+	}
+	/*
+	 * Use an ordered tag for the stop command, to guarantee that any
+	 * pending I/O will finish before the stop command executes.  This
+	 * would normally be the case anyway, since CTL will basically
+	 * treat the start/stop command as an ordered command with respect
+	 * to any other command except an INQUIRY.  (See ctl_ser_table.c.)
+	 */
+	ctl_scsi_start_stop(/*io*/ io,
+			    /*start*/ start,
+			    /*load_eject*/ 0,
+			    /*immediate*/ immed,
+			    /*power_conditions*/ SSS_PC_START_VALID,
+			    /*onoffline*/ onoffline,
+			    /*ctl_tag_type*/ start ? CTL_TAG_SIMPLE :
+						     CTL_TAG_ORDERED,
+			    /*control*/ 0);
+
+	io->io_hdr.nexus.targ_target.id = target;
+	io->io_hdr.nexus.targ_lun = lun;
+	io->io_hdr.nexus.initid = id;
+
+	if (cctl_do_io(fd, retries, io, __func__) != 0) {
+		retval = 1;
+		goto bailout;
+	}
+
+	ctl_scsi_path_string(io, scsi_path, sizeof(scsi_path));
+	if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
+		fprintf(stdout, "%s LUN %s successfully\n", scsi_path,
+			(start) ?  "started" : "stopped");
+	} else
+		ctl_io_error_print(io, NULL, stderr);
+
+bailout:
+	ctl_scsi_free_io(io);
+
+	return (retval);
+}
+
+static int
+cctl_mode_sense(int fd, int target, int lun, int iid, int retries, 
+		int argc, char **argv, char *combinedopt)
+{
+	union ctl_io *io;
+	struct ctl_id id;
+	uint32_t datalen;
+	uint8_t *dataptr;
+	int pc = -1, cdbsize, retval, dbd = 0, subpage = -1;
+	int list = 0;
+	int page_code = -1;
+	int c;
+
+	id.id = iid;
+	cdbsize = 0;
+	retval = 0;
+	dataptr = NULL;
+
+	io = ctl_scsi_alloc_io(id);
+	if (io == NULL) {
+		warn("%s: can't allocate memory", __func__);
+		return (1);
+	}
+
+	while ((c = getopt(argc, argv, combinedopt)) != -1) {
+		switch (c) {
+		case 'P':
+			pc = strtoul(optarg, NULL, 0);
+			break;
+		case 'S':
+			subpage = strtoul(optarg, NULL, 0);
+			break;
+		case 'd':
+			dbd = 1;
+			break;
+		case 'l':
+			list = 1;
+			break;
+		case 'm':
+			page_code = strtoul(optarg, NULL, 0);
+			break;
+		case 'c':
+			cdbsize = strtol(optarg, NULL, 0);
+			break;
+		default:
+			break;
+		}
+	}
+
+	if (((list == 0) && (page_code == -1))
+	 || ((list != 0) && (page_code != -1))) {
+		warnx("%s: you must specify either a page code (-m) or -l",
+		      __func__);
+		retval = 1;
+		goto bailout;
+	}
+
+	if ((page_code != -1)
+	 && ((page_code > SMS_ALL_PAGES_PAGE)
+	  || (page_code < 0))) {
+		warnx("%s: page code %d is out of range", __func__,
+		      page_code);
+		retval = 1;
+		goto bailout;
+	}
+
+	if (list == 1) {
+		page_code = SMS_ALL_PAGES_PAGE;
+		if (pc != -1) {
+			warnx("%s: arg -P makes no sense with -l",
+			      __func__);
+			retval = 1;
+			goto bailout;
+		}
+		if (subpage != -1) {
+			warnx("%s: arg -S makes no sense with -l", __func__);
+			retval = 1;
+			goto bailout;
+		}
+	}
+
+	if (pc == -1)
+		pc = SMS_PAGE_CTRL_CURRENT;
+	else {
+		if ((pc > 3)
+		 || (pc < 0)) {
+			warnx("%s: page control value %d is out of range: 0-3",
+			      __func__, pc);
+			retval = 1;
+			goto bailout;
+		}
+	}
+
+
+	if ((subpage != -1)
+	 && ((subpage > 255)
+	  || (subpage < 0))) {
+		warnx("%s: subpage code %d is out of range: 0-255", __func__,
+		      subpage);
+		retval = 1;
+		goto bailout;
+	}
+	if (cdbsize != 0) {
+		switch (cdbsize) {
+		case 6:
+		case 10:
+			break;
+		default:
+			warnx("%s: invalid cdbsize %d, valid sizes are 6 "
+			      "and 10", __func__, cdbsize);
+			retval = 1;
+			goto bailout;
+			break;
+		}
+	} else
+		cdbsize = 6;
+
+	if (subpage == -1)
+		subpage = 0;
+
+	if (cdbsize == 6)
+		datalen = 255;
+	else
+		datalen = 65535;
+
+	dataptr = (uint8_t *)malloc(datalen);
+	if (dataptr == NULL) {
+		warn("%s: can't allocate %d bytes", __func__, datalen);
+		retval = 1;
+		goto bailout;
+	}
+
+	memset(dataptr, 0, datalen);
+
+	ctl_scsi_mode_sense(io,
+			    /*data_ptr*/ dataptr,
+			    /*data_len*/ datalen,
+			    /*dbd*/ dbd,
+			    /*llbaa*/ 0,
+			    /*page_code*/ page_code,
+			    /*pc*/ pc << 6,
+			    /*subpage*/ subpage,
+			    /*minimum_cdb_size*/ cdbsize,
+			    /*tag_type*/ CTL_TAG_SIMPLE,
+			    /*control*/ 0);
+
+	io->io_hdr.nexus.targ_target.id = target;
+	io->io_hdr.nexus.targ_lun = lun;
+	io->io_hdr.nexus.initid = id;
+
+	if (cctl_do_io(fd, retries, io, __func__) != 0) {
+		retval = 1;
+		goto bailout;
+	}
+
+	if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
+		int pages_len, used_len;
+		uint32_t returned_len;
+		uint8_t *ndataptr;
+
+		if (io->scsiio.cdb[0] == MODE_SENSE_6) {
+			struct scsi_mode_hdr_6 *hdr6;
+			int bdlen;
+
+			hdr6 = (struct scsi_mode_hdr_6 *)dataptr;
+
+			returned_len = hdr6->datalen + 1;
+			bdlen = hdr6->block_descr_len;
+
+			ndataptr = (uint8_t *)((uint8_t *)&hdr6[1] + bdlen);
+		} else {
+			struct scsi_mode_hdr_10 *hdr10;
+			int bdlen;
+
+			hdr10 = (struct scsi_mode_hdr_10 *)dataptr;
+
+			returned_len = scsi_2btoul(hdr10->datalen) + 2;
+			bdlen = scsi_2btoul(hdr10->block_descr_len);
+
+			ndataptr = (uint8_t *)((uint8_t *)&hdr10[1] + bdlen);
+		}
+		/* just in case they can give us more than we allocated for */
+		returned_len = min(returned_len, datalen);
+		pages_len = returned_len - (ndataptr - dataptr);
+#if 0
+		fprintf(stdout, "returned_len = %d, pages_len = %d\n",
+			returned_len, pages_len);
+#endif
+		if (list == 1) {
+			fprintf(stdout, "Supported mode pages:\n");
+			for (used_len = 0; used_len < pages_len;) {
+				struct scsi_mode_page_header *header;
+
+				header = (struct scsi_mode_page_header *)
+					&ndataptr[used_len];
+				fprintf(stdout, "%d\n", header->page_code);
+				used_len += header->page_length + 2;
+			}
+		} else {
+			for (used_len = 0; used_len < pages_len; used_len++) {
+				fprintf(stdout, "0x%x ", ndataptr[used_len]);
+				if (((used_len+1) % 16) == 0)
+					fprintf(stdout, "\n");
+			}
+			fprintf(stdout, "\n");
+		}
+	} else
+		ctl_io_error_print(io, NULL, stderr);
+bailout:
+
+	ctl_scsi_free_io(io);
+
+	if (dataptr != NULL)
+		free(dataptr);
+
+	return (retval);
+}
+
+static int
+cctl_read_capacity(int fd, int target, int lun, int iid, int retries, 
+           	   int argc, char **argv, char *combinedopt)
+{
+	union ctl_io *io;
+	struct ctl_id id;
+	struct scsi_read_capacity_data *data;
+	struct scsi_read_capacity_data_long *longdata;
+	int cdbsize = -1, retval;
+	uint8_t *dataptr;
+	int c;
+
+	cdbsize = 10;
+	dataptr = NULL;
+	retval = 0;
+	id.id = iid;
+
+	io = ctl_scsi_alloc_io(id);
+	if (io == NULL) {
+		warn("%s: can't allocate memory\n", __func__);
+		return (1);
+	}
+
+	while ((c = getopt(argc, argv, combinedopt)) != -1) {
+		switch (c) {
+		case 'c':
+			cdbsize = strtol(optarg, NULL, 0);
+			break;
+		default:
+			break;
+		}
+	}
+	if (cdbsize != -1) {
+		switch (cdbsize) {
+		case 10:
+		case 16:
+			break;
+		default:
+			warnx("%s: invalid cdbsize %d, valid sizes are 10 "
+			      "and 16", __func__, cdbsize);
+			retval = 1;
+			goto bailout;
+			break; /* NOTREACHED */
+		}
+	} else
+		cdbsize = 10;
+
+	dataptr = (uint8_t *)malloc(sizeof(*longdata));
+	if (dataptr == NULL) {
+		warn("%s: can't allocate %zd bytes\n", __func__,
+		     sizeof(*longdata));
+		retval = 1;
+		goto bailout;
+	}
+	memset(dataptr, 0, sizeof(*longdata));
+
+retry:
+
+	switch (cdbsize) {
+	case 10:
+		ctl_scsi_read_capacity(io,
+				       /*data_ptr*/ dataptr,
+				       /*data_len*/ sizeof(*longdata),
+				       /*addr*/ 0,
+				       /*reladr*/ 0,
+				       /*pmi*/ 0,
+				       /*tag_type*/ CTL_TAG_SIMPLE,
+				       /*control*/ 0);
+		break;
+	case 16:
+		ctl_scsi_read_capacity_16(io,
+					  /*data_ptr*/ dataptr,
+					  /*data_len*/ sizeof(*longdata),
+					  /*addr*/ 0,
+					  /*reladr*/ 0,
+					  /*pmi*/ 0,
+					  /*tag_type*/ CTL_TAG_SIMPLE,
+					  /*control*/ 0);
+		break;
+	}
+
+	io->io_hdr.nexus.initid = id;
+	io->io_hdr.nexus.targ_target.id = target;
+	io->io_hdr.nexus.targ_lun = lun;
+
+	if (cctl_do_io(fd, retries, io, __func__) != 0) {
+		retval = 1;
+		goto bailout;
+	}
+
+	if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
+		uint64_t maxlba;
+		uint32_t blocksize;
+
+		if (cdbsize == 10) {
+
+			data = (struct scsi_read_capacity_data *)dataptr;
+
+			maxlba = scsi_4btoul(data->addr);
+			blocksize = scsi_4btoul(data->length);
+
+			if (maxlba == 0xffffffff) {
+				cdbsize = 16;
+				goto retry;
+			}
+		} else {
+			longdata=(struct scsi_read_capacity_data_long *)dataptr;
+
+			maxlba = scsi_8btou64(longdata->addr);
+			blocksize = scsi_4btoul(longdata->length);
+		}
+
+		fprintf(stdout, "Disk Capacity: %ju, Blocksize: %d\n",
+			(uintmax_t)maxlba, blocksize);
+	} else {
+		ctl_io_error_print(io, NULL, stderr);
+	}
+bailout:
+	ctl_scsi_free_io(io);
+
+	if (dataptr != NULL)
+		free(dataptr);
+
+	return (retval);
+}
+
+static int
+cctl_read_write(int fd, int target, int lun, int iid, int retries,
+		int argc, char **argv, char *combinedopt,
+		ctladm_cmdfunction command)
+{
+	union ctl_io *io;
+	struct ctl_id id;
+	int file_fd, do_stdio;
+	int cdbsize = -1, databytes;
+	uint8_t *dataptr;
+	char *filename = NULL;
+	int datalen = -1, blocksize = -1;
+	uint64_t lba = 0;
+	int lba_set = 0;
+	int retval;
+	int c;
+
+	retval = 0;
+	do_stdio = 0;
+	dataptr = NULL;
+	file_fd = -1;
+	id.id = iid;
+
+	io = ctl_scsi_alloc_io(id);
+	if (io == NULL) {
+		warn("%s: can't allocate memory\n", __func__);
+		return (1);
+	}
+
+	while ((c = getopt(argc, argv, combinedopt)) != -1) {
+		switch (c) {
+		case 'N':
+			io->io_hdr.flags |= CTL_FLAG_NO_DATAMOVE;
+			break;
+		case 'b':
+			blocksize = strtoul(optarg, NULL, 0);
+			break;
+		case 'c':
+			cdbsize = strtoul(optarg, NULL, 0);
+			break;
+		case 'd':
+			datalen = strtoul(optarg, NULL, 0);
+			break;
+		case 'f':
+			filename = strdup(optarg);
+			break;
+		case 'l':
+			lba = strtoull(optarg, NULL, 0);
+			lba_set = 1;
+			break;
+		default:
+			break;
+		}
+	}
+	if (filename == NULL) {
+		warnx("%s: you must supply a filename using -f", __func__);
+		retval = 1;
+		goto bailout;
+	}
+
+	if (datalen == -1) {
+		warnx("%s: you must specify the data length with -d", __func__);
+		retval = 1;
+		goto bailout;
+	}
+
+	if (lba_set == 0) {
+		warnx("%s: you must specify the LBA with -l", __func__);
+		retval = 1;
+		goto bailout;
+	}
+
+	if (blocksize == -1) {
+		warnx("%s: you must specify the blocksize with -b", __func__);
+		retval = 1;
+		goto bailout;
+	}
+
+	if (cdbsize != -1) {
+		switch (cdbsize) {
+		case 6:
+		case 10:
+		case 12:
+		case 16:
+			break;
+		default:
+			warnx("%s: invalid cdbsize %d, valid sizes are 6, "
+			      "10, 12 or 16", __func__, cdbsize);
+			retval = 1;
+			goto bailout;
+			break; /* NOTREACHED */
+		}
+	} else
+		cdbsize = 6;
+
+	databytes = datalen * blocksize;
+	dataptr = (uint8_t *)malloc(databytes);
+
+	if (dataptr == NULL) {
+		warn("%s: can't allocate %d bytes\n", __func__, databytes);
+		retval = 1;
+		goto bailout;
+	}
+	if (strcmp(filename, "-") == 0) {
+		if (command == CTLADM_CMD_READ)
+			file_fd = STDOUT_FILENO;
+		else
+			file_fd = STDIN_FILENO;
+		do_stdio = 1;
+	} else {
+		file_fd = open(filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
+		if (file_fd == -1) {
+			warn("%s: can't open file %s", __func__, filename);
+			retval = 1;
+			goto bailout;
+		}
+	}
+
+	memset(dataptr, 0, databytes);
+
+	if (command == CTLADM_CMD_WRITE) {
+		int bytes_read;
+
+		bytes_read = read(file_fd, dataptr, databytes);
+		if (bytes_read == -1) {
+			warn("%s: error reading file %s", __func__, filename);
+			retval = 1;
+			goto bailout;
+		}
+		if (bytes_read != databytes) {
+			warnx("%s: only read %d bytes from file %s",
+			      __func__, bytes_read, filename);
+			retval = 1;
+			goto bailout;
+		}
+	}
+	ctl_scsi_read_write(io,
+			    /*data_ptr*/ dataptr,
+			    /*data_len*/ databytes,
+			    /*read_op*/ (command == CTLADM_CMD_READ) ? 1 : 0,
+			    /*byte2*/ 0,
+			    /*minimum_cdb_size*/ cdbsize,
+			    /*lba*/ lba,
+			    /*num_blocks*/ datalen,
+			    /*tag_type*/ CTL_TAG_SIMPLE,
+			    /*control*/ 0);
+
+	io->io_hdr.nexus.targ_target.id = target;
+	io->io_hdr.nexus.targ_lun = lun;
+	io->io_hdr.nexus.initid = id;
+
+	if (cctl_do_io(fd, retries, io, __func__) != 0) {
+		retval = 1;
+		goto bailout;
+	}
+
+	if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)
+	 && (command == CTLADM_CMD_READ)) {
+		int bytes_written;
+
+		bytes_written = write(file_fd, dataptr, databytes);
+		if (bytes_written == -1) {
+			warn("%s: can't write to %s", __func__, filename);
+			goto bailout;
+		}
+	} else if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
+		ctl_io_error_print(io, NULL, stderr);
+
+
+bailout:
+
+	ctl_scsi_free_io(io);
+
+	if (dataptr != NULL)
+		free(dataptr);
+
+	if ((do_stdio == 0)
+	 && (file_fd != -1))
+		close(file_fd);
+
+	return (retval);
+}
+
+static int
+cctl_get_luns(int fd, int target, int lun, int iid, int retries, struct
+	      scsi_report_luns_data **lun_data, uint32_t *num_luns)
+{
+	union ctl_io *io;
+	struct ctl_id id;
+	uint32_t nluns;
+	int lun_datalen;
+	int retval;
+
+	retval = 0;
+	id.id = iid;
+
+	io = ctl_scsi_alloc_io(id);
+	if (io == NULL) {
+		warnx("%s: can't allocate memory", __func__);
+		return (1);
+	}
+
+	/*
+	 * lun_data includes space for 1 lun, allocate space for 4 initially.
+	 * If that isn't enough, we'll allocate more.
+	 */
+	nluns = 4;
+retry:
+	lun_datalen = sizeof(*lun_data) +
+		(nluns * sizeof(struct scsi_report_luns_lundata));
+	*lun_data = malloc(lun_datalen);
+
+	if (*lun_data == NULL) {
+		warnx("%s: can't allocate memory", __func__);
+		ctl_scsi_free_io(io);
+		return (1);
+	}
+
+	ctl_scsi_report_luns(io,
+			     /*data_ptr*/ (uint8_t *)*lun_data,
+			     /*data_len*/ lun_datalen,
+			     /*select_report*/ RPL_REPORT_ALL,
+			     /*tag_type*/ CTL_TAG_SIMPLE,
+			     /*control*/ 0);
+
+	io->io_hdr.nexus.initid = id;
+	io->io_hdr.nexus.targ_target.id = target;
+	io->io_hdr.nexus.targ_lun = lun;
+
+	if (cctl_do_io(fd, retries, io, __func__) != 0) {
+		retval = 1;
+		goto bailout;
+	}
+
+	if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
+		uint32_t returned_len, returned_luns;
+
+		returned_len = scsi_4btoul((*lun_data)->length);
+		returned_luns = returned_len / 8;
+		if (returned_luns > nluns) {
+			nluns = returned_luns;
+			free(*lun_data);
+			goto retry;
+		}
+		/* These should be the same */
+		*num_luns = MIN(returned_luns, nluns);
+	} else {
+		ctl_io_error_print(io, NULL, stderr);
+		retval = 1;
+	}
+bailout:
+	ctl_scsi_free_io(io);
+
+	return (retval);
+}
+
+static int
+cctl_report_luns(int fd, int target, int lun, int iid, int retries)
+{
+	struct scsi_report_luns_data *lun_data;
+	uint32_t num_luns, i;
+	int retval;
+
+	lun_data = NULL;
+
+	if ((retval = cctl_get_luns(fd, target, lun, iid, retries, &lun_data,
+				   &num_luns)) != 0)
+		goto bailout;
+
+	fprintf(stdout, "%u LUNs returned\n", num_luns);
+	for (i = 0; i < num_luns; i++) {
+		int lun_val;
+
+		/*
+		 * XXX KDM figure out a way to share this code with
+		 * cctl_lunlist()?
+		 */
+		switch (lun_data->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) {
+		case RPL_LUNDATA_ATYP_PERIPH:
+			lun_val = lun_data->luns[i].lundata[1];
+			break;
+		case RPL_LUNDATA_ATYP_FLAT:
+			lun_val = (lun_data->luns[i].lundata[0] &
+				RPL_LUNDATA_FLAT_LUN_MASK) |
+				(lun_data->luns[i].lundata[1] <<
+				RPL_LUNDATA_FLAT_LUN_BITS);
+			break;
+		case RPL_LUNDATA_ATYP_LUN:
+		case RPL_LUNDATA_ATYP_EXTLUN:
+		default:
+			fprintf(stdout, "Unsupported LUN format %d\n",
+				lun_data->luns[i].lundata[0] &
+				RPL_LUNDATA_ATYP_MASK);
+			lun_val = -1;
+			break;
+		}
+		if (lun_val == -1)
+			continue;
+
+		fprintf(stdout, "%d\n", lun_val);
+	}
+
+bailout:
+	if (lun_data != NULL)
+		free(lun_data);
+
+	return (retval);
+}
+
+static int
+cctl_tur(int fd, int target, int lun, int iid, int retries)
+{
+	union ctl_io *io;
+	struct ctl_id id;
+
+	id.id = iid;
+
+	io = ctl_scsi_alloc_io(id);
+	if (io == NULL) {
+		fprintf(stderr, "can't allocate memory\n");
+		return (1);
+	}
+
+	ctl_scsi_tur(io,
+		     /* tag_type */ CTL_TAG_SIMPLE,
+		     /* control */ 0);
+
+	io->io_hdr.nexus.targ_target.id = target;
+	io->io_hdr.nexus.targ_lun = lun;
+	io->io_hdr.nexus.initid = id;
+
+	if (cctl_do_io(fd, retries, io, __func__) != 0) {
+		ctl_scsi_free_io(io);
+		return (1);
+	}
+
+	if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)
+		fprintf(stdout, "Unit is ready\n");
+	else
+		ctl_io_error_print(io, NULL, stderr);
+
+	return (0);
+}
+
+static int
+cctl_get_inquiry(int fd, int target, int lun, int iid, int retries, 
+		 char *path_str, int path_len,
+		 struct scsi_inquiry_data *inq_data)
+{
+	union ctl_io *io;
+	struct ctl_id id;
+	int retval;
+
+	retval = 0;
+
+	id.id = iid;
+
+	io = ctl_scsi_alloc_io(id);
+	if (io == NULL) {
+		warnx("cctl_inquiry: can't allocate memory\n");
+		return (1);
+	}
+
+	ctl_scsi_inquiry(/*io*/ io,
+			 /*data_ptr*/ (uint8_t *)inq_data,
+			 /*data_len*/ sizeof(*inq_data),
+			 /*byte2*/ 0,
+			 /*page_code*/ 0,
+			 /*tag_type*/ CTL_TAG_SIMPLE,
+			 /*control*/ 0);
+
+	io->io_hdr.nexus.targ_target.id = target;
+	io->io_hdr.nexus.targ_lun = lun;
+	io->io_hdr.nexus.initid = id;
+
+	if (cctl_do_io(fd, retries, io, __func__) != 0) {
+		retval = 1;
+		goto bailout;
+	}
+
+	if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
+		retval = 1;
+		ctl_io_error_print(io, NULL, stderr);
+	} else if (path_str != NULL)
+		ctl_scsi_path_string(io, path_str, path_len);
+
+bailout:
+	ctl_scsi_free_io(io);
+
+	return (retval);
+}
+
+static int
+cctl_inquiry(int fd, int target, int lun, int iid, int retries)
+{
+	struct scsi_inquiry_data *inq_data;
+	char scsi_path[40];
+	int retval;
+
+	retval = 0;
+
+	inq_data = malloc(sizeof(*inq_data));
+	if (inq_data == NULL) {
+		warnx("%s: can't allocate inquiry data", __func__);
+		retval = 1;
+		goto bailout;
+	}
+
+	if ((retval = cctl_get_inquiry(fd, target, lun, iid, retries, scsi_path,
+				       sizeof(scsi_path), inq_data)) != 0)
+		goto bailout;
+
+	printf("%s", scsi_path);
+	scsi_print_inquiry(inq_data);
+
+bailout:
+	if (inq_data != NULL)
+		free(inq_data);
+
+	return (retval);
+}
+
+static int
+cctl_req_sense(int fd, int target, int lun, int iid, int retries)
+{
+	union ctl_io *io;
+	struct scsi_sense_data *sense_data;
+	struct ctl_id id;
+	int retval;
+
+	retval = 0;
+
+	id.id = iid;
+
+	io = ctl_scsi_alloc_io(id);
+	if (io == NULL) {
+		warnx("cctl_req_sense: can't allocate memory\n");
+		return (1);
+	}
+	sense_data = malloc(sizeof(*sense_data));
+	memset(sense_data, 0, sizeof(*sense_data));
+
+	ctl_scsi_request_sense(/*io*/ io,
+			       /*data_ptr*/ (uint8_t *)sense_data,
+			       /*data_len*/ sizeof(*sense_data),
+			       /*byte2*/ 0,
+			       /*tag_type*/ CTL_TAG_SIMPLE,
+			       /*control*/ 0);
+
+	io->io_hdr.nexus.targ_target.id = target;
+	io->io_hdr.nexus.targ_lun = lun;
+	io->io_hdr.nexus.initid = id;
+
+	if (cctl_do_io(fd, retries, io, __func__) != 0) {
+		retval = 1;
+		goto bailout;
+	}
+
+	if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
+		bcopy(sense_data, &io->scsiio.sense_data, sizeof(*sense_data));
+		io->scsiio.sense_len = sizeof(*sense_data);
+		ctl_scsi_sense_print(&io->scsiio, NULL, stdout);
+	} else
+		ctl_io_error_print(io, NULL, stderr);
+
+bailout:
+
+	ctl_scsi_free_io(io);
+	free(sense_data);
+
+	return (retval);
+}
+
+static int
+cctl_report_target_port_group(int fd, int target, int lun, int initiator)
+{
+	union ctl_io *io;
+	struct ctl_id id;
+	uint32_t datalen;
+	uint8_t *dataptr;
+	int retval;
+
+	id.id = initiator;
+	dataptr = NULL;
+	retval = 0;
+
+	io = ctl_scsi_alloc_io(id);
+	if (io == NULL) {
+		warn("%s: can't allocate memory", __func__);
+		return (1);
+	}
+
+	datalen = 64;
+	dataptr = (uint8_t *)malloc(datalen);
+	if (dataptr == NULL) {
+		warn("%s: can't allocate %d bytes", __func__, datalen);
+	    	retval = 1;
+		goto bailout;
+	}
+
+	memset(dataptr, 0, datalen);
+
+	ctl_scsi_maintenance_in(/*io*/ io,
+				/*data_ptr*/ dataptr,
+				/*data_len*/ datalen,
+				/*action*/ SA_RPRT_TRGT_GRP,
+				/*tag_type*/ CTL_TAG_SIMPLE,
+				/*control*/ 0);
+
+	io->io_hdr.nexus.targ_target.id = target;
+	io->io_hdr.nexus.targ_lun = lun;
+	io->io_hdr.nexus.initid = id;
+
+	if (cctl_do_io(fd, 0, io, __func__) != 0) {
+		retval = 1;
+		goto bailout;
+	}
+
+	if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
+		int returned_len, used_len;
+
+		returned_len = scsi_4btoul(&dataptr[0]) + 4;
+
+		for (used_len = 0; used_len < returned_len; used_len++) {
+			fprintf(stdout, "0x%02x ", dataptr[used_len]);
+			if (((used_len+1) % 8) == 0)
+				fprintf(stdout, "\n");
+		}
+		fprintf(stdout, "\n");
+	} else
+		ctl_io_error_print(io, NULL, stderr);
+
+bailout:
+	ctl_scsi_free_io(io);
+
+	if (dataptr != NULL)
+		free(dataptr);
+
+	return (retval);
+}
+
+static int
+cctl_inquiry_vpd_devid(int fd, int target, int lun, int initiator)
+{
+	union ctl_io *io;
+	struct ctl_id id;
+	uint32_t datalen;
+	uint8_t *dataptr;
+	int retval;
+
+	id.id = initiator;
+	retval = 0;
+	dataptr = NULL;
+
+	io = ctl_scsi_alloc_io(id);
+	if (io == NULL) {
+		warn("%s: can't allocate memory", __func__);
+		return (1);
+	}
+
+	datalen = 256;
+	dataptr = (uint8_t *)malloc(datalen);
+	if (dataptr == NULL) {
+		warn("%s: can't allocate %d bytes", __func__, datalen);
+	    	retval = 1;
+		goto bailout;
+	}
+
+	memset(dataptr, 0, datalen);
+
+	ctl_scsi_inquiry(/*io*/        io,
+			 /*data_ptr*/  dataptr,
+			 /*data_len*/  datalen,
+			 /*byte2*/     SI_EVPD,
+			 /*page_code*/ SVPD_DEVICE_ID,
+			 /*tag_type*/  CTL_TAG_SIMPLE,
+			 /*control*/   0);
+
+	io->io_hdr.nexus.targ_target.id = target;
+	io->io_hdr.nexus.targ_lun = lun;
+	io->io_hdr.nexus.initid = id;
+
+	if (cctl_do_io(fd, 0, io, __func__) != 0) {
+		retval = 1;
+		goto bailout;
+	}
+
+	if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
+		int returned_len, used_len;
+
+		returned_len = scsi_2btoul(&dataptr[2]) + 4;
+
+		for (used_len = 0; used_len < returned_len; used_len++) {
+			fprintf(stdout, "0x%02x ", dataptr[used_len]);
+			if (((used_len+1) % 8) == 0)
+				fprintf(stdout, "\n");
+		}
+		fprintf(stdout, "\n");
+	} else
+		ctl_io_error_print(io, NULL, stderr);
+
+bailout:
+	ctl_scsi_free_io(io);
+
+	if (dataptr != NULL)
+		free(dataptr);
+
+	return (retval);
+}
+
+static int
+cctl_persistent_reserve_in(int fd, int target, int lun, int initiator, 
+                           int argc, char **argv, char *combinedopt,
+			   int retry_count)
+{
+	union ctl_io *io;
+	struct ctl_id id;
+	uint32_t datalen;
+	uint8_t *dataptr;
+	int action = -1;
+	int retval;
+	int c;
+
+	id.id = initiator;
+	retval = 0;
+	dataptr = NULL;
+
+	io = ctl_scsi_alloc_io(id);
+	if (io == NULL) {
+		warn("%s: can't allocate memory", __func__);
+		return (1);
+	}
+
+	while ((c = getopt(argc, argv, combinedopt)) != -1) {
+		switch (c) {
+		case 'a':
+			action = strtol(optarg, NULL, 0);
+			break;
+		default:
+			break;
+		}
+	}
+
+	if (action < 0 || action > 2) {
+		warn("action must be specified and in the range: 0-2");
+		retval = 1;
+		goto bailout;
+	}
+
+
+	datalen = 256;
+	dataptr = (uint8_t *)malloc(datalen);
+	if (dataptr == NULL) {
+		warn("%s: can't allocate %d bytes", __func__, datalen);
+	    	retval = 1;
+		goto bailout;
+	}
+
+	memset(dataptr, 0, datalen);
+
+	ctl_scsi_persistent_res_in(io,
+				   /*data_ptr*/ dataptr,
+				   /*data_len*/ datalen,
+				   /*action*/   action,
+				   /*tag_type*/ CTL_TAG_SIMPLE,
+				   /*control*/  0);
+
+	io->io_hdr.nexus.targ_target.id = target;
+	io->io_hdr.nexus.targ_lun = lun;
+	io->io_hdr.nexus.initid = id;
+
+	if (cctl_do_io(fd, retry_count, io, __func__) != 0) {
+		retval = 1;
+		goto bailout;
+	}
+
+	if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
+		int returned_len, used_len;
+
+		returned_len = 0;
+
+		switch (action) {
+		case 0:
+			returned_len = scsi_4btoul(&dataptr[4]) + 8;
+			returned_len = min(returned_len, 256);
+			break;
+		case 1:
+			returned_len = scsi_4btoul(&dataptr[4]) + 8;
+			break;
+		case 2:
+			returned_len = 8;
+			break;
+		default:
+			warnx("%s: invalid action %d", __func__, action);
+			goto bailout;
+			break; /* NOTREACHED */
+		}
+
+		for (used_len = 0; used_len < returned_len; used_len++) {
+			fprintf(stdout, "0x%02x ", dataptr[used_len]);
+			if (((used_len+1) % 8) == 0)
+				fprintf(stdout, "\n");
+		}
+		fprintf(stdout, "\n");
+	} else
+		ctl_io_error_print(io, NULL, stderr);
+
+bailout:
+	ctl_scsi_free_io(io);
+
+	if (dataptr != NULL)
+		free(dataptr);
+
+	return (retval);
+}
+
+static int
+cctl_persistent_reserve_out(int fd, int target, int lun, int initiator, 
+			    int argc, char **argv, char *combinedopt, 
+			    int retry_count)
+{
+	union ctl_io *io;
+	struct ctl_id id;
+	uint32_t datalen;
+	uint64_t key = 0, sa_key = 0;
+	int action = -1, restype = -1;
+	uint8_t *dataptr;
+	int retval;
+	int c;
+
+	id.id = initiator;
+	retval = 0;
+	dataptr = NULL;
+
+	io = ctl_scsi_alloc_io(id);
+	if (io == NULL) {
+		warn("%s: can't allocate memory", __func__);
+		return (1);
+	}
+
+	while ((c = getopt(argc, argv, combinedopt)) != -1) {
+		switch (c) {
+		case 'a':
+			action = strtol(optarg, NULL, 0);
+			break;
+		case 'k':
+			key = strtoull(optarg, NULL, 0);
+			break;
+		case 'r':
+			restype = strtol(optarg, NULL, 0);
+			break;
+		case 's':
+			sa_key = strtoull(optarg, NULL, 0);
+			break;
+		default:
+			break;
+		}
+	}
+	if (action < 0 || action > 5) {
+		warn("action must be specified and in the range: 0-5");
+		retval = 1;
+		goto bailout;
+	}
+
+	if (restype < 0 || restype > 5) {
+		if (action != 0 && action != 5 && action != 3) {
+			warn("'restype' must specified and in the range: 0-5");
+			retval = 1;
+			goto bailout;
+		}
+	}
+
+	datalen = 24;
+	dataptr = (uint8_t *)malloc(datalen);
+	if (dataptr == NULL) {
+		warn("%s: can't allocate %d bytes", __func__, datalen);
+		retval = 1;
+		goto bailout;
+	}
+
+	memset(dataptr, 0, datalen);
+
+	ctl_scsi_persistent_res_out(io,
+				    /*data_ptr*/ dataptr,
+				    /*data_len*/ datalen,
+				    /*action*/   action,
+				    /*type*/     restype,
+				    /*key*/      key,
+				    /*sa key*/   sa_key,
+				    /*tag_type*/ CTL_TAG_SIMPLE,
+				    /*control*/  0);
+
+	io->io_hdr.nexus.targ_target.id = target;
+	io->io_hdr.nexus.targ_lun = lun;
+	io->io_hdr.nexus.initid = id;
+
+	if (cctl_do_io(fd, retry_count, io, __func__) != 0) {
+		retval = 1;
+		goto bailout;
+	}
+	if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
+		char scsi_path[40];
+		ctl_scsi_path_string(io, scsi_path, sizeof(scsi_path));
+		fprintf( stdout, "%sPERSISTENT RESERVE OUT executed "
+			"successfully\n", scsi_path);
+	} else
+		ctl_io_error_print(io, NULL, stderr);
+
+bailout:
+	ctl_scsi_free_io(io);
+
+	if (dataptr != NULL)
+		free(dataptr);
+
+	return (retval);
+}
+
+struct cctl_req_option {
+	char			     *name;
+	int			      namelen;
+	char			     *value;
+	int			      vallen;
+	STAILQ_ENTRY(cctl_req_option) links;
+};
+
+static int
+cctl_create_lun(int fd, int argc, char **argv, char *combinedopt)
+{
+	struct ctl_lun_req req;
+	int device_type = -1;
+	uint64_t lun_size = 0;
+	uint32_t blocksize = 0, req_lun_id = 0;
+	char *serial_num = NULL;
+	char *device_id = NULL;
+	int lun_size_set = 0, blocksize_set = 0, lun_id_set = 0;
+	char *backend_name = NULL;
+	STAILQ_HEAD(, cctl_req_option) option_list;
+	int num_options = 0;
+	int retval = 0, c;
+
+	STAILQ_INIT(&option_list);
+
+	while ((c = getopt(argc, argv, combinedopt)) != -1) {
+		switch (c) {
+		case 'b':
+			backend_name = strdup(optarg);
+			break;
+		case 'B':
+			blocksize = strtoul(optarg, NULL, 0);
+			blocksize_set = 1;
+			break;
+		case 'd':
+			device_id = strdup(optarg);
+			break;
+		case 'l':
+			req_lun_id = strtoul(optarg, NULL, 0);
+			lun_id_set = 1;
+			break;
+		case 'o': {
+			struct cctl_req_option *option;
+			char *tmpstr;
+			char *name, *value;
+
+			tmpstr = strdup(optarg);
+			name = strsep(&tmpstr, "=");
+			if (name == NULL) {
+				warnx("%s: option -o takes \"name=value\""
+				      "argument", __func__);
+				retval = 1;
+				goto bailout;
+			}
+			value = strsep(&tmpstr, "=");
+			if (value == NULL) {
+				warnx("%s: option -o takes \"name=value\""
+				      "argument", __func__);
+				retval = 1;
+				goto bailout;
+			}
+			option = malloc(sizeof(*option));
+			if (option == NULL) {
+				warn("%s: error allocating %zd bytes",
+				     __func__, sizeof(*option));
+				retval = 1;
+				goto bailout;
+			}
+			option->name = strdup(name);
+			option->namelen = strlen(name) + 1;
+			option->value = strdup(value);
+			option->vallen = strlen(value) + 1;
+			free(tmpstr);
+
+			STAILQ_INSERT_TAIL(&option_list, option, links);
+			num_options++;
+			break;
+		}
+		case 's':
+			lun_size = strtoull(optarg, NULL, 0);
+			lun_size_set = 1;
+			break;
+		case 'S':
+			serial_num = strdup(optarg);
+			break;
+		case 't':
+			device_type = strtoul(optarg, NULL, 0);
+			break;
+		default:
+			break;
+		}
+	}
+
+	if (backend_name == NULL) {
+		warnx("%s: backend name (-b) must be specified", __func__);
+		retval = 1;
+		goto bailout;
+	}
+
+	bzero(&req, sizeof(req));
+
+	strlcpy(req.backend, backend_name, sizeof(req.backend));
+	req.reqtype = CTL_LUNREQ_CREATE;
+
+	if (blocksize_set != 0)
+		req.reqdata.create.blocksize_bytes = blocksize;
+
+	if (lun_size_set != 0)
+		req.reqdata.create.lun_size_bytes = lun_size;
+
+	if (lun_id_set != 0) {
+		req.reqdata.create.flags |= CTL_LUN_FLAG_ID_REQ;
+		req.reqdata.create.req_lun_id = req_lun_id;
+	}
+
+	req.reqdata.create.flags |= CTL_LUN_FLAG_DEV_TYPE;
+
+	if (device_type != -1)
+		req.reqdata.create.device_type = device_type;
+	else
+		req.reqdata.create.device_type = T_DIRECT;
+
+	if (serial_num != NULL) {
+		strlcpy(req.reqdata.create.serial_num, serial_num,
+			sizeof(req.reqdata.create.serial_num));
+		req.reqdata.create.flags |= CTL_LUN_FLAG_SERIAL_NUM;
+	}
+
+	if (device_id != NULL) {
+		strlcpy(req.reqdata.create.device_id, device_id,
+			sizeof(req.reqdata.create.device_id));
+		req.reqdata.create.flags |= CTL_LUN_FLAG_DEVID;
+	}
+
+	req.num_be_args = num_options;
+	if (num_options > 0) {
+		struct cctl_req_option *option, *next_option;
+		int i;
+
+		req.be_args = malloc(num_options * sizeof(*req.be_args));
+		if (req.be_args == NULL) {
+			warn("%s: error allocating %zd bytes", __func__,
+			     num_options * sizeof(*req.be_args));
+			retval = 1;
+			goto bailout;
+		}
+
+		for (i = 0, option = STAILQ_FIRST(&option_list);
+		     i < num_options; i++, option = next_option) {
+			next_option = STAILQ_NEXT(option, links);
+
+			req.be_args[i].namelen = option->namelen;
+			req.be_args[i].name = strdup(option->name);
+			req.be_args[i].vallen = option->vallen;
+			req.be_args[i].value = strdup(option->value);
+			/*
+			 * XXX KDM do we want a way to specify a writeable
+			 * flag of some sort?  Do we want a way to specify
+			 * binary data?
+			 */
+			req.be_args[i].flags = CTL_BEARG_ASCII | CTL_BEARG_RD;
+
+			STAILQ_REMOVE(&option_list, option, cctl_req_option,
+				      links);
+			free(option->name);
+			free(option->value);
+			free(option);
+		}
+	}
+
+	if (ioctl(fd, CTL_LUN_REQ, &req) == -1) {
+		warn("%s: error issuing CTL_LUN_REQ ioctl", __func__);
+		retval = 1;
+		goto bailout;
+	}
+
+	if (req.status == CTL_LUN_ERROR) {
+		warnx("%s: error returned from LUN creation request:\n%s",
+		      __func__, req.error_str);
+		retval = 1;
+		goto bailout;
+	} else if (req.status != CTL_LUN_OK) {
+		warnx("%s: unknown LUN creation request status %d",
+		      __func__, req.status);
+		retval = 1;
+		goto bailout;
+	}
+
+	fprintf(stdout, "LUN created successfully\n");
+	fprintf(stdout, "backend:       %s\n", req.backend);
+	fprintf(stdout, "device type:   %d\n",req.reqdata.create.device_type);
+	fprintf(stdout, "LUN size:      %ju bytes\n",
+		(uintmax_t)req.reqdata.create.lun_size_bytes);
+	fprintf(stdout, "blocksize      %u bytes\n",
+		req.reqdata.create.blocksize_bytes);
+	fprintf(stdout, "LUN ID:        %d\n", req.reqdata.create.req_lun_id);
+	fprintf(stdout, "Serial Number: %s\n", req.reqdata.create.serial_num);
+	fprintf(stdout, "Device ID;     %s\n", req.reqdata.create.device_id);
+
+bailout:
+	return (retval);
+}
+
+static int
+cctl_rm_lun(int fd, int argc, char **argv, char *combinedopt)
+{
+	struct ctl_lun_req req;
+	uint32_t lun_id = 0;
+	int lun_id_set = 0;
+	char *backend_name = NULL;
+	STAILQ_HEAD(, cctl_req_option) option_list;
+	int num_options = 0;
+	int retval = 0, c;
+
+	STAILQ_INIT(&option_list);
+
+	while ((c = getopt(argc, argv, combinedopt)) != -1) {
+		switch (c) {
+		case 'b':
+			backend_name = strdup(optarg);
+			break;
+		case 'l':
+			lun_id = strtoul(optarg, NULL, 0);
+			lun_id_set = 1;
+			break;
+		case 'o': {
+			struct cctl_req_option *option;
+			char *tmpstr;
+			char *name, *value;
+
+			tmpstr = strdup(optarg);
+			name = strsep(&tmpstr, "=");
+			if (name == NULL) {
+				warnx("%s: option -o takes \"name=value\""
+				      "argument", __func__);
+				retval = 1;
+				goto bailout;
+			}
+			value = strsep(&tmpstr, "=");
+			if (value == NULL) {
+				warnx("%s: option -o takes \"name=value\""
+				      "argument", __func__);
+				retval = 1;
+				goto bailout;
+			}
+			option = malloc(sizeof(*option));
+			if (option == NULL) {
+				warn("%s: error allocating %zd bytes",
+				     __func__, sizeof(*option));
+				retval = 1;
+				goto bailout;
+			}
+			option->name = strdup(name);
+			option->namelen = strlen(name) + 1;
+			option->value = strdup(value);
+			option->vallen = strlen(value) + 1;
+			free(tmpstr);
+
+			STAILQ_INSERT_TAIL(&option_list, option, links);
+			num_options++;
+			break;
+		}
+		default:
+			break;
+		}
+	}
+
+	if (backend_name == NULL)
+		errx(1, "%s: backend name (-b) must be specified", __func__);
+
+	if (lun_id_set == 0)
+		errx(1, "%s: LUN id (-l) must be specified", __func__);
+
+	bzero(&req, sizeof(req));
+
+	strlcpy(req.backend, backend_name, sizeof(req.backend));
+	req.reqtype = CTL_LUNREQ_RM;
+
+	req.reqdata.rm.lun_id = lun_id;
+
+	req.num_be_args = num_options;
+	if (num_options > 0) {
+		struct cctl_req_option *option, *next_option;
+		int i;
+
+		req.be_args = malloc(num_options * sizeof(*req.be_args));
+		if (req.be_args == NULL) {
+			warn("%s: error allocating %zd bytes", __func__,
+			     num_options * sizeof(*req.be_args));
+			retval = 1;
+			goto bailout;
+		}
+
+		for (i = 0, option = STAILQ_FIRST(&option_list);
+		     i < num_options; i++, option = next_option) {
+			next_option = STAILQ_NEXT(option, links);
+
+			req.be_args[i].namelen = option->namelen;
+			req.be_args[i].name = strdup(option->name);
+			req.be_args[i].vallen = option->vallen;
+			req.be_args[i].value = strdup(option->value);
+			/*
+			 * XXX KDM do we want a way to specify a writeable
+			 * flag of some sort?  Do we want a way to specify
+			 * binary data?
+			 */
+			req.be_args[i].flags = CTL_BEARG_ASCII | CTL_BEARG_RD;
+
+			STAILQ_REMOVE(&option_list, option, cctl_req_option,
+				      links);
+			free(option->name);
+			free(option->value);
+			free(option);
+		}
+	}
+
+	if (ioctl(fd, CTL_LUN_REQ, &req) == -1) {
+		warn("%s: error issuing CTL_LUN_REQ ioctl", __func__);
+		retval = 1;
+		goto bailout;
+	}
+
+	if (req.status == CTL_LUN_ERROR) {
+		warnx("%s: error returned from LUN creation request:\n%s",
+		      __func__, req.error_str);
+		retval = 1;
+		goto bailout;
+	} else if (req.status != CTL_LUN_OK) {
+		warnx("%s: unknown LUN creation request status %d",
+		      __func__, req.status);
+		retval = 1;
+		goto bailout;
+	}
+
+	printf("LUN %d deleted successfully\n", lun_id);
+
+bailout:
+	return (retval);
+}
+
+/*
+ * Name/value pair used for per-LUN attributes.
+ */
+struct cctl_lun_nv {
+	char *name;
+	char *value;
+	STAILQ_ENTRY(cctl_lun_nv) links;
+};
+
+/*
+ * Backend LUN information.  
+ */
+struct cctl_lun {
+	uint64_t lun_id;
+	char *backend_type;
+	uint64_t size_blocks;
+	uint32_t blocksize;
+	char *serial_number;
+	char *device_id;
+	STAILQ_HEAD(,cctl_lun_nv) attr_list;
+	STAILQ_ENTRY(cctl_lun) links;
+};
+
+struct cctl_devlist_data {
+	int num_luns;
+	STAILQ_HEAD(,cctl_lun) lun_list;
+	struct cctl_lun *cur_lun;
+	int level;
+	struct sbuf *cur_sb[32];
+};
+
+static void
+cctl_start_element(void *user_data, const char *name, const char **attr)
+{
+	int i;
+	struct cctl_devlist_data *devlist;
+	struct cctl_lun *cur_lun;
+
+	devlist = (struct cctl_devlist_data *)user_data;
+	cur_lun = devlist->cur_lun;
+	devlist->level++;
+	if ((u_int)devlist->level > (sizeof(devlist->cur_sb) /
+	    sizeof(devlist->cur_sb[0])))
+		errx(1, "%s: too many nesting levels, %zd max", __func__,
+		     sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0]));
+
+	devlist->cur_sb[devlist->level] = sbuf_new_auto();
+	if (devlist->cur_sb[devlist->level] == NULL)
+		err(1, "%s: Unable to allocate sbuf", __func__);
+
+	if (strcmp(name, "lun") == 0) {
+		if (cur_lun != NULL)
+			errx(1, "%s: improper lun element nesting", __func__);
+
+		cur_lun = calloc(1, sizeof(*cur_lun));
+		if (cur_lun == NULL)
+			err(1, "%s: cannot allocate %zd bytes", __func__,
+			    sizeof(*cur_lun));
+
+		devlist->num_luns++;
+		devlist->cur_lun = cur_lun;
+
+		STAILQ_INIT(&cur_lun->attr_list);
+		STAILQ_INSERT_TAIL(&devlist->lun_list, cur_lun, links);
+
+		for (i = 0; attr[i] != NULL; i += 2) {
+			if (strcmp(attr[i], "id") == 0) {
+				cur_lun->lun_id = strtoull(attr[i+1], NULL, 0);
+			} else {
+				errx(1, "%s: invalid LUN attribute %s = %s",
+				     __func__, attr[i], attr[i+1]);
+			}
+		}
+	}
+}
+
+static void
+cctl_end_element(void *user_data, const char *name)
+{
+	struct cctl_devlist_data *devlist;
+	struct cctl_lun *cur_lun;
+	char *str;
+
+	devlist = (struct cctl_devlist_data *)user_data;
+	cur_lun = devlist->cur_lun;
+
+	if ((cur_lun == NULL)
+	 && (strcmp(name, "ctllunlist") != 0))
+		errx(1, "%s: cur_lun == NULL! (name = %s)", __func__, name);
+
+	if (devlist->cur_sb[devlist->level] == NULL)
+		errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
+		     devlist->level, name);
+
+	sbuf_finish(devlist->cur_sb[devlist->level]);
+	str = strdup(sbuf_data(devlist->cur_sb[devlist->level]));
+	if (str == NULL)
+		err(1, "%s can't allocate %zd bytes for string", __func__,
+		    sbuf_len(devlist->cur_sb[devlist->level]));
+
+	if (strlen(str) == 0) {
+		free(str);
+		str = NULL;
+	}
+
+	sbuf_delete(devlist->cur_sb[devlist->level]);
+	devlist->cur_sb[devlist->level] = NULL;
+	devlist->level--;
+
+	if (strcmp(name, "backend_type") == 0) {
+		cur_lun->backend_type = str;
+		str = NULL;
+	} else if (strcmp(name, "size") == 0) {
+		cur_lun->size_blocks = strtoull(str, NULL, 0);
+	} else if (strcmp(name, "blocksize") == 0) {
+		cur_lun->blocksize = strtoul(str, NULL, 0);
+	} else if (strcmp(name, "serial_number") == 0) {
+		cur_lun->serial_number = str;
+		str = NULL;
+	} else if (strcmp(name, "device_id") == 0) {
+		cur_lun->device_id = str;
+		str = NULL;
+	} else if (strcmp(name, "lun") == 0) {
+		devlist->cur_lun = NULL;
+	} else if (strcmp(name, "ctllunlist") == 0) {
+		
+	} else {
+		struct cctl_lun_nv *nv;
+
+		nv = calloc(1, sizeof(*nv));
+		if (nv == NULL)
+			err(1, "%s: can't allocate %zd bytes for nv pair",
+			    __func__, sizeof(*nv));
+
+		nv->name = strdup(name);
+		if (nv->name == NULL)
+			err(1, "%s: can't allocated %zd bytes for string",
+			    __func__, strlen(name));
+
+		nv->value = str;
+		str = NULL;
+		STAILQ_INSERT_TAIL(&cur_lun->attr_list, nv, links);
+	}
+
+	free(str);
+}
+
+static void
+cctl_char_handler(void *user_data, const XML_Char *str, int len)
+{
+	struct cctl_devlist_data *devlist;
+
+	devlist = (struct cctl_devlist_data *)user_data;
+
+	sbuf_bcat(devlist->cur_sb[devlist->level], str, len);
+}
+
+static int
+cctl_devlist(int fd, int argc, char **argv, char *combinedopt)
+{
+	struct ctl_lun_list list;
+	struct cctl_devlist_data devlist;
+	struct cctl_lun *lun;
+	XML_Parser parser;
+	char *lun_str;
+	int lun_len;
+	int dump_xml = 0;
+	int retval, c;
+	char *backend = NULL;
+	int verbose = 0;
+
+	retval = 0;
+	lun_len = 4096;
+
+	bzero(&devlist, sizeof(devlist));
+	STAILQ_INIT(&devlist.lun_list);
+
+	while ((c = getopt(argc, argv, combinedopt)) != -1) {
+		switch (c) {
+		case 'b':
+			backend = strdup(optarg);
+			break;
+		case 'v':
+			verbose++;
+			break;
+		case 'x':
+			dump_xml = 1;
+			break;
+		default:
+			break;
+		}
+	}
+
+retry:
+	lun_str = malloc(lun_len);
+
+	bzero(&list, sizeof(list));
+	list.alloc_len = lun_len;
+	list.status = CTL_LUN_LIST_NONE;
+	list.lun_xml = lun_str;
+
+	if (ioctl(fd, CTL_LUN_LIST, &list) == -1) {
+		warn("%s: error issuing CTL_LUN_LIST ioctl", __func__);
+		retval = 1;
+		goto bailout;
+	}
+
+	if (list.status == CTL_LUN_LIST_ERROR) {
+		warnx("%s: error returned from CTL_LUN_LIST ioctl:\n%s",
+		      __func__, list.error_str);
+	} else if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
+		lun_len = lun_len << 1;
+		goto retry;
+	}
+
+	if (dump_xml != 0) {
+		printf("%s", lun_str);
+		goto bailout;
+	}
+
+	parser = XML_ParserCreate(NULL);
+	if (parser == NULL) {
+		warn("%s: Unable to create XML parser", __func__);
+		retval = 1;
+		goto bailout;
+	}
+
+	XML_SetUserData(parser, &devlist);
+	XML_SetElementHandler(parser, cctl_start_element, cctl_end_element);
+	XML_SetCharacterDataHandler(parser, cctl_char_handler);
+
+	retval = XML_Parse(parser, lun_str, strlen(lun_str), 1);
+	XML_ParserFree(parser);
+	if (retval != 1) {
+		retval = 1;
+		goto bailout;
+	}
+
+	printf("LUN Backend  %18s %4s %-16s %-16s\n", "Size (Blocks)", "BS",
+	       "Serial Number", "Device ID");
+	STAILQ_FOREACH(lun, &devlist.lun_list, links) {
+		struct cctl_lun_nv *nv;
+
+		if ((backend != NULL)
+		 && (strcmp(lun->backend_type, backend) != 0))
+			continue;
+
+		printf("%3ju %-8s %18ju %4u %-16s %-16s\n",
+		       (uintmax_t)lun->lun_id,
+		       lun->backend_type, (uintmax_t)lun->size_blocks,
+		       lun->blocksize, lun->serial_number, lun->device_id);
+
+		if (verbose == 0)
+			continue;
+
+		STAILQ_FOREACH(nv, &lun->attr_list, links) {
+			printf("      %s=%s\n", nv->name, nv->value);
+		}
+	}
+bailout:
+	free(lun_str);
+
+	return (retval);
+}
+
+void
+usage(int error)
+{
+	fprintf(error ? stderr : stdout,
+"Usage:\n"
+"Primary commands:\n"
+"         ctladm tur         [dev_id][general options]\n"
+"         ctladm inquiry     [dev_id][general options]\n"
+"         ctladm devid       [dev_id][general options]\n"
+"         ctladm reqsense    [dev_id][general options]\n"
+"         ctladm reportluns  [dev_id][general options]\n"
+"         ctladm read        [dev_id][general options] <-l lba> <-d len>\n"
+"                            <-f file|-> <-b blocksize> [-c cdbsize][-N]\n"
+"         ctladm write       [dev_id][general options] <-l lba> <-d len>\n"
+"                            <-f file|-> <-b blocksize> [-c cdbsize][-N]\n"
+"         ctladm readcap     [dev_id][general options] [-c cdbsize]\n"
+"         ctladm modesense   [dev_id][general options] <-m page|-l> [-P pc]\n"
+"                            [-d] [-S subpage] [-c cdbsize]\n"
+"         ctladm prin        [dev_id][general options] <-a action>\n"
+"         ctladm prout       [dev_id][general options] <-a action>\n"
+"                            <-r restype] [-k key] [-s sa_key]\n"
+"         ctladm rtpg        [dev_id][general options]\n"
+"         ctladm start       [dev_id][general options] [-i] [-o]\n"
+"         ctladm stop        [dev_id][general options] [-i] [-o]\n"
+"         ctladm synccache   [dev_id][general options] [-l lba]\n"
+"                            [-b blockcount] [-r] [-i] [-c cdbsize]\n"
+"         ctladm create      <-b backend> [-B blocksize] [-d device_id]\n"
+"                            [-l lun_id] [-o name=value] [-s size_bytes]\n"
+"                            [-S serial_num] [-t dev_type]\n"
+"         ctladm remove      <-b backend> <-l lun_id> [-o name=value]\n"
+"         ctladm devlist     [-b][-v][-x]\n"
+"         ctladm shutdown\n"
+"         ctladm startup\n"
+"         ctladm hardstop\n"
+"         ctladm hardstart\n"
+"         ctladm lunlist\n"
+"         ctladm bbrread     [dev_id] <-l lba> <-d datalen>\n"
+"         ctladm delay       [dev_id] <-l datamove|done> [-T oneshot|cont]\n"
+"                            [-t secs]\n"
+"         ctladm realsync    <on|off|query>\n"
+"         ctladm setsync     [dev_id] <-i interval>\n"
+"         ctladm getsync     [dev_id]\n"
+"         ctladm inject      [dev_id] <-i action> <-p pattern> [-r lba,len]\n"
+"                            [-s len fmt [args]] [-c] [-d delete_id]\n"
+"         ctladm port        <-l | -o <on|off> | [-w wwnn][-W wwpn]>\n"
+"                            [-p targ_port] [-t port_type] [-q] [-x]\n"
+"         ctladm dumpooa\n"
+"         ctladm dumpstructs\n"
+"         ctladm help\n"
+"General Options:\n"
+"-I intiator_id           : defaults to 7, used to change the initiator id\n"
+"-C retries               : specify the number of times to retry this command\n"
+"-D devicename            : specify the device to operate on\n"
+"                         : (default is %s)\n"
+"read/write options:\n"
+"-l lba                   : logical block address\n"
+"-d len                   : read/write length, in blocks\n"
+"-f file|-                : write/read data to/from file or stdout/stdin\n"
+"-b blocksize             : block size, in bytes\n"
+"-c cdbsize               : specify minimum cdb size: 6, 10, 12 or 16\n"
+"-N                       : do not copy data to/from userland\n"
+"readcapacity options:\n"
+"-c cdbsize               : specify minimum cdb size: 10 or 16\n"
+"modesense options:\n"
+"-m page                  : specify the mode page to view\n"
+"-l                       : request a list of supported pages\n"
+"-P pc                    : specify the page control value: 0-3 (current,\n"
+"                           changeable, default, saved, respectively)\n"
+"-d                       : disable block descriptors for mode sense\n"
+"-S subpage               : specify a subpage\n"
+"-c cdbsize               : specify minimum cdb size: 6 or 10\n"
+"persistent reserve in options:\n"
+"-a action                : specify the action value: 0-2 (read key, read\n"
+"                           reservation, read capabilities, respectively)\n"
+"persistent reserve out options:\n"
+"-a action                : specify the action value: 0-5 (register, reserve,\n"
+"                           release, clear, preempt, register and ignore)\n"
+"-k key                   : key value\n"
+"-s sa_key                : service action value\n"
+"-r restype               : specify the reservation type: 0-5(wr ex, ex ac,\n"
+"                           wr ex ro, ex ac ro, wr ex ar, ex ac ar)\n"
+"start/stop options:\n"
+"-i                       : set the immediate bit (CTL does not support this)\n"
+"-o                       : set the on/offline bit\n"
+"synccache options:\n"
+"-l lba                   : set the starting LBA\n"
+"-b blockcount            : set the length to sync in blocks\n"
+"-r                       : set the relative addressing bit\n"
+"-i                       : set the immediate bit\n"
+"-c cdbsize               : specify minimum cdb size: 10 or 16\n"
+"create options:\n"
+"-b backend               : backend name (\"block\", \"ramdisk\", etc.)\n"
+"-B blocksize             : LUN blocksize in bytes (some backends)\n"
+"-d device_id             : SCSI VPD page 0x83 ID\n"
+"-l lun_id                : requested LUN number\n"
+"-o name=value            : backend-specific options, multiple allowed\n"
+"-s size_bytes            : LUN size in bytes (some backends)\n"
+"-S serial_num            : SCSI VPD page 0x80 serial number\n"
+"-t dev_type              : SCSI device type (0=disk, 3=processor)\n"
+"remove options:\n"
+"-b backend               : backend name (\"block\", \"ramdisk\", etc.)\n"
+"-l lun_id                : LUN number to delete\n"
+"-o name=value            : backend-specific options, multiple allowed\n"
+"devlist options:\n"
+"-b backend               : list devices from specified backend only\n"
+"-v                       : be verbose, show backend attributes\n"
+"-x                       : dump raw XML\n"
+"delay options:\n"
+"-l datamove|done         : delay command at datamove or done phase\n"
+"-T oneshot               : delay one command, then resume normal completion\n"
+"-T cont                  : delay all commands\n"
+"-t secs                  : number of seconds to delay\n"
+"inject options:\n"
+"-i error_action          : action to perform\n"
+"-p pattern               : command pattern to look for\n"
+"-r lba,len               : LBA range for pattern\n"
+"-s len fmt [args]        : sense data for custom sense action\n"
+"-c                       : continuous operation\n"
+"-d delete_id             : error id to delete\n"
+"port options:\n"
+"-l                       : list frontend ports\n"
+"-o on|off                : turn frontend ports on or off\n"
+"-w wwnn                  : set WWNN for one frontend\n"
+"-W wwpn                  : set WWPN for one frontend\n"
+"-t port_type             : specify fc, scsi, ioctl, internal frontend type\n"
+"-p targ_port             : specify target port number\n"
+"-q                       : omit header in list output\n"
+"-x                       : output port list in XML format\n"
+"bbrread options:\n"
+"-l lba                   : starting LBA\n"
+"-d datalen               : length, in bytes, to read\n",
+CTL_DEFAULT_DEV);
+}
+
+int
+main(int argc, char **argv)
+{
+	int option_index, c;
+	ctladm_cmdfunction command;
+	ctladm_cmdargs cmdargs;
+	ctladm_optret optreturn;
+	char *device;
+	const char *mainopt = "C:D:I:";
+	const char *subopt = NULL;
+	char combinedopt[256];
+	int target, lun;
+	int optstart = 2;
+	int retval, fd;
+	int retries, timeout;
+	int initid;
+
+	option_index = 0;
+	retval = 0;
+	cmdargs = CTLADM_ARG_NONE;
+	command = CTLADM_CMD_HELP;
+	device = NULL;
+	fd = -1;
+	retries = 0;
+	target = 0;
+	lun = 0;
+	timeout = 0;
+	initid = 7;
+
+	if (argc < 2) {
+		usage(1);
+		retval = 1;
+		goto bailout;
+	}
+
+	/*
+	 * Get the base option.
+	 */
+	optreturn = getoption(option_table,argv[1], &command, &cmdargs,&subopt);
+
+	if (optreturn == CC_OR_AMBIGUOUS) {
+		warnx("ambiguous option %s", argv[1]);
+		usage(0);
+		exit(1);
+	} else if (optreturn == CC_OR_NOT_FOUND) {
+		warnx("option %s not found", argv[1]);
+		usage(0);
+		exit(1);
+	}
+
+	if (cmdargs & CTLADM_ARG_NEED_TL) {
+	 	if ((argc < 3)
+		 || (!isdigit(argv[2][0]))) {
+			warnx("option %s requires a target:lun argument",
+			      argv[1]);
+			usage(0);
+			exit(1);
+		}
+		retval = cctl_parse_tl(argv[2], &target, &lun);
+		if (retval != 0)
+			errx(1, "invalid target:lun argument %s", argv[2]);
+
+		cmdargs |= CTLADM_ARG_TARG_LUN;
+		optstart++;
+	}
+
+	/*
+	 * Ahh, getopt(3) is a pain.
+	 *
+	 * This is a gross hack.  There really aren't many other good
+	 * options (excuse the pun) for parsing options in a situation like
+	 * this.  getopt is kinda braindead, so you end up having to run
+	 * through the options twice, and give each invocation of getopt
+	 * the option string for the other invocation.
+	 *
+	 * You would think that you could just have two groups of options.
+	 * The first group would get parsed by the first invocation of
+	 * getopt, and the second group would get parsed by the second
+	 * invocation of getopt.  It doesn't quite work out that way.  When
+	 * the first invocation of getopt finishes, it leaves optind pointing
+	 * to the argument _after_ the first argument in the second group.
+	 * So when the second invocation of getopt comes around, it doesn't
+	 * recognize the first argument it gets and then bails out.
+	 *
+	 * A nice alternative would be to have a flag for getopt that says
+	 * "just keep parsing arguments even when you encounter an unknown
+	 * argument", but there isn't one.  So there's no real clean way to
+	 * easily parse two sets of arguments without having one invocation
+	 * of getopt know about the other.
+	 *
+	 * Without this hack, the first invocation of getopt would work as
+	 * long as the generic arguments are first, but the second invocation
+	 * (in the subfunction) would fail in one of two ways.  In the case
+	 * where you don't set optreset, it would fail because optind may be
+	 * pointing to the argument after the one it should be pointing at.
+	 * In the case where you do set optreset, and reset optind, it would
+	 * fail because getopt would run into the first set of options, which
+	 * it doesn't understand.
+	 *
+	 * All of this would "sort of" work if you could somehow figure out
+	 * whether optind had been incremented one option too far.  The
+	 * mechanics of that, however, are more daunting than just giving
+	 * both invocations all of the expect options for either invocation.
+	 *
+	 * Needless to say, I wouldn't mind if someone invented a better
+	 * (non-GPL!) command line parsing interface than getopt.  I
+	 * wouldn't mind if someone added more knobs to getopt to make it
+	 * work better.  Who knows, I may talk myself into doing it someday,
+	 * if the standards weenies let me.  As it is, it just leads to
+	 * hackery like this and causes people to avoid it in some cases.
+	 *
+	 * KDM, September 8th, 1998
+	 */
+	if (subopt != NULL)
+		sprintf(combinedopt, "%s%s", mainopt, subopt);
+	else
+		sprintf(combinedopt, "%s", mainopt);
+
+	/*
+	 * Start getopt processing at argv[2/3], since we've already
+	 * accepted argv[1..2] as the command name, and as a possible
+	 * device name.
+	 */
+	optind = optstart;
+
+	/*
+	 * Now we run through the argument list looking for generic
+	 * options, and ignoring options that possibly belong to
+	 * subfunctions.
+	 */
+	while ((c = getopt(argc, argv, combinedopt))!= -1){
+		switch (c) {
+		case 'C':
+			cmdargs |= CTLADM_ARG_RETRIES;
+			retries = strtol(optarg, NULL, 0);
+			break;
+		case 'D':
+			device = strdup(optarg);
+			cmdargs |= CTLADM_ARG_DEVICE;
+			break;
+		case 'I':
+			cmdargs |= CTLADM_ARG_INITIATOR;
+			initid = strtol(optarg, NULL, 0);
+			break;
+		default:
+			break;
+		}
+	}
+
+	if ((cmdargs & CTLADM_ARG_INITIATOR) == 0)
+		initid = 7;
+
+	optind = optstart;
+	optreset = 1;
+
+	/*
+	 * Default to opening the CTL device for now.
+	 */
+	if (((cmdargs & CTLADM_ARG_DEVICE) == 0)
+	 && (command != CTLADM_CMD_HELP)) {
+		device = strdup(CTL_DEFAULT_DEV);
+		cmdargs |= CTLADM_ARG_DEVICE;
+	}
+
+	if ((cmdargs & CTLADM_ARG_DEVICE)
+	 && (command != CTLADM_CMD_HELP)) {
+		fd = open(device, O_RDWR);
+		if (fd == -1) {
+			fprintf(stderr, "%s: error opening %s: %s\n",
+				argv[0], device, strerror(errno));
+			retval = 1;
+			goto bailout;
+		}
+	} else if ((command != CTLADM_CMD_HELP)
+		&& ((cmdargs & CTLADM_ARG_DEVICE) == 0)) {
+		fprintf(stderr, "%s: you must specify a device with the "
+			"--device argument for this command\n", argv[0]);
+		command = CTLADM_CMD_HELP;
+		retval = 1;
+	}
+
+	switch (command) {
+	case CTLADM_CMD_TUR:
+		retval = cctl_tur(fd, target, lun, initid, retries);
+		break;
+	case CTLADM_CMD_INQUIRY:
+		retval = cctl_inquiry(fd, target, lun, initid, retries);
+		break;
+	case CTLADM_CMD_REQ_SENSE:
+		retval = cctl_req_sense(fd, target, lun, initid, retries);
+		break;
+	case CTLADM_CMD_REPORT_LUNS:
+		retval = cctl_report_luns(fd, target, lun, initid, retries);
+		break;
+	case CTLADM_CMD_CREATE:
+		retval = cctl_create_lun(fd, argc, argv, combinedopt);
+		break;
+	case CTLADM_CMD_RM:
+		retval = cctl_rm_lun(fd, argc, argv, combinedopt);
+		break;
+	case CTLADM_CMD_DEVLIST:
+		retval = cctl_devlist(fd, argc, argv, combinedopt);
+		break;
+	case CTLADM_CMD_READ:
+	case CTLADM_CMD_WRITE:
+		retval = cctl_read_write(fd, target, lun, initid, retries,
+					 argc, argv, combinedopt, command);
+		break;
+	case CTLADM_CMD_PORT:
+		retval = cctl_port(fd, argc, argv, combinedopt);
+		break;
+	case CTLADM_CMD_READCAPACITY:
+		retval = cctl_read_capacity(fd, target, lun, initid, retries,
+					    argc, argv, combinedopt);
+		break;
+	case CTLADM_CMD_MODESENSE:
+		retval = cctl_mode_sense(fd, target, lun, initid, retries,
+					 argc, argv, combinedopt);
+		break;
+	case CTLADM_CMD_START:
+	case CTLADM_CMD_STOP:
+		retval = cctl_start_stop(fd, target, lun, initid, retries,
+					 (command == CTLADM_CMD_START) ? 1 : 0,
+					 argc, argv, combinedopt);
+		break;
+	case CTLADM_CMD_SYNC_CACHE:
+		retval = cctl_sync_cache(fd, target, lun, initid, retries,
+					 argc, argv, combinedopt);
+		break;
+	case CTLADM_CMD_SHUTDOWN:
+	case CTLADM_CMD_STARTUP:
+		retval = cctl_startup_shutdown(fd, target, lun, initid,
+					       command);
+		break;
+	case CTLADM_CMD_HARDSTOP:
+	case CTLADM_CMD_HARDSTART:
+		retval = cctl_hardstopstart(fd, command);
+		break;
+	case CTLADM_CMD_BBRREAD:
+		retval = cctl_bbrread(fd, target, lun, initid, argc, argv,
+				      combinedopt);
+		break;
+	case CTLADM_CMD_LUNLIST:
+		retval = cctl_lunlist(fd);
+		break;
+	case CTLADM_CMD_DELAY:
+		retval = cctl_delay(fd, target, lun, argc, argv, combinedopt);
+		break;
+	case CTLADM_CMD_REALSYNC:
+		retval = cctl_realsync(fd, argc, argv);
+		break;
+	case CTLADM_CMD_SETSYNC:
+	case CTLADM_CMD_GETSYNC:
+		retval = cctl_getsetsync(fd, target, lun, command,
+					 argc, argv, combinedopt);
+		break;
+	case CTLADM_CMD_ERR_INJECT:
+		retval = cctl_error_inject(fd, target, lun, argc, argv,
+					   combinedopt);
+		break;
+	case CTLADM_CMD_DUMPOOA:
+		retval = cctl_dump_ooa(fd, argc, argv);
+		break;
+	case CTLADM_CMD_DUMPSTRUCTS:
+		retval = cctl_dump_structs(fd, cmdargs);
+		break;
+	case CTLADM_CMD_PRES_IN:
+		retval = cctl_persistent_reserve_in(fd, target, lun, initid, 
+		                                    argc, argv, combinedopt,
+						    retries);
+		break;
+	case CTLADM_CMD_PRES_OUT:
+		retval = cctl_persistent_reserve_out(fd, target, lun, initid, 
+						     argc, argv, combinedopt,
+						     retries);
+		break;
+	case CTLADM_CMD_INQ_VPD_DEVID:
+	        retval = cctl_inquiry_vpd_devid(fd, target, lun, initid);
+		break;
+	case CTLADM_CMD_RTPG:
+	        retval = cctl_report_target_port_group(fd, target, lun, initid);
+		break;
+	case CTLADM_CMD_HELP:
+	default:
+		usage(retval);
+		break;
+	}
+bailout:
+
+	if (fd != -1)
+		close(fd);
+
+	exit (retval);
+}
+
+/*
+ * vim: ts=8
+ */
diff -r ebe87ce4d7dd -r 1750de5b0399 head/usr.sbin/ctladm/ctladm.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/usr.sbin/ctladm/ctladm.h	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 1998 Kenneth D. Merry.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD: head/usr.sbin/ctladm/ctladm.h 229997 2012-01-12 00:34:33Z ken $
+ * $Id: //depot/users/kenm/FreeBSD-test2/usr.sbin/ctladm/ctladm.h#1 $
+ */
+
+#ifndef _CTLADM_H
+#define _CTLADM_H
+
+/*
+ * get_hook: Structure for evaluating args in a callback.
+ */
+struct get_hook
+{
+	int argc;
+	char **argv;
+	int got;
+};
+
+char *cget(void *hook, char *name);
+int iget(void *hook, char *name);
+void arg_put(void *hook, int letter, void *arg, int count, char *name);
+void usage(int error);
+
+#endif /* _CTLADM_H */
diff -r ebe87ce4d7dd -r 1750de5b0399 head/usr.sbin/ctladm/util.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/head/usr.sbin/ctladm/util.c	Fri Jan 13 15:51:01 2012 +0200
@@ -0,0 +1,156 @@
+/*
+ * Written By Julian ELischer
+ * Copyright julian Elischer 1993.
+ * Permission is granted to use or redistribute this file in any way as long
+ * as this notice remains. Julian Elischer does not guarantee that this file
+ * is totally correct for any given task and users of this file must
+ * accept responsibility for any damage that occurs from the application of this
+ * file.
+ *
+ * (julian at tfs.com julian at dialix.oz.au)
+ *
+ * User SCSI hooks added by Peter Dufault:
+ *
+ * Copyright (c) 1994 HD Associates
+ * (contact: dufault at hda.com)
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. The name of HD Associates
+ *    may not be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY HD ASSOCIATES ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL HD ASSOCIATES BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+/*
+ * Taken from the original scsi(8) program.
+ * from: scsi.c,v 1.17 1998/01/12 07:57:57 charnier Exp $";
+ */
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD: head/usr.sbin/ctladm/util.c 229997 2012-01-12 00:34:33Z ken $");
+
+#include <sys/stdint.h>
+#include <sys/types.h>
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include <camlib.h>
+#include "ctladm.h"
+
+int verbose;
+
+/* iget: Integer argument callback
+ */
+int
+iget(void *hook, char *name)
+{
+	struct get_hook *h = (struct get_hook *)hook;
+	int arg;
+
+	if (h->got >= h->argc)
+	{
+		fprintf(stderr, "Expecting an integer argument.\n");
+		usage(0);
+		exit(1);
+	}
+	arg = strtol(h->argv[h->got], 0, 0);
+	h->got++;
+
+	if (verbose && name && *name)
+		printf("%s: %d\n", name, arg);
+
+	return arg;
+}
+
+/* cget: char * argument callback
+ */
+char *
+cget(void *hook, char *name)
+{
+	struct get_hook *h = (struct get_hook *)hook;
+	char *arg;
+
+	if (h->got >= h->argc)
+	{
+		fprintf(stderr, "Expecting a character pointer argument.\n");
+		usage(0);
+		exit(1);
+	}
+	arg = h->argv[h->got];
+	h->got++;
+
+	if (verbose && name)
+		printf("cget: %s: %s", name, arg);
+
+	return arg;
+}
+
+/* arg_put: "put argument" callback
+ */
+void
+arg_put(void *hook __unused, int letter, void *arg, int count, char *name)
+{
+	if (verbose && name && *name)
+		printf("%s:  ", name);
+
+	switch(letter)
+	{
+		case 'i':
+		case 'b':
+		printf("%jd ", (intmax_t)(intptr_t)arg);
+		break;
+
+		case 'c':
+		case 'z':
+		{
+			char *p;
+
+			p = malloc(count + 1);
+			if (p == NULL) {
+				fprintf(stderr, "can't malloc memory for p\n");
+				exit(1);
+			}
+
+			bzero(p, count +1);
+			strncpy(p, (char *)arg, count);
+			if (letter == 'z')
+			{
+				int i;
+				for (i = count - 1; i >= 0; i--)
+					if (p[i] == ' ')
+						p[i] = 0;
+					else
+						break;
+			}
+			printf("%s ", p);
+
+			free(p);
+		}
+
+		break;
+
+		default:
+		printf("Unknown format letter: '%c'\n", letter);
+	}
+	if (verbose)
+		putchar('\n');
+}
diff -r ebe87ce4d7dd -r 1750de5b0399 head/usr.sbin/daemon/daemon.c
--- a/head/usr.sbin/daemon/daemon.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/usr.sbin/daemon/daemon.c	Fri Jan 13 15:51:01 2012 +0200
@@ -29,7 +29,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
+__FBSDID("$FreeBSD: head/usr.sbin/daemon/daemon.c 229667 2012-01-05 22:48:36Z ghelmer $");
 
 #include <sys/param.h>
 
@@ -79,9 +79,6 @@
 	if (argc == 0)
 		usage();
 
-	if (user != NULL)
-		restrict_process(user);
-
 	/*
 	 * Try to open the pidfile before calling daemon(3),
 	 * to be able to report the error intelligently
@@ -97,6 +94,9 @@
 		}
 	}
 
+   if (user != NULL)
+             restrict_process(user);
+
 	if (daemon(nochdir, noclose) == -1)
 		err(1, NULL);
 
diff -r ebe87ce4d7dd -r 1750de5b0399 head/usr.sbin/mlxcontrol/interface.c
--- a/head/usr.sbin/mlxcontrol/interface.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/usr.sbin/mlxcontrol/interface.c	Fri Jan 13 15:51:01 2012 +0200
@@ -23,7 +23,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- *	$FreeBSD$
+ *	$FreeBSD: head/usr.sbin/mlxcontrol/interface.c 229997 2012-01-12 00:34:33Z ken $
  */
 
 #include <fcntl.h>
@@ -253,7 +253,7 @@
 
     /* build the cdb */
     inq_cmd->opcode = INQUIRY;
-    inq_cmd->length = SHORT_INQUIRY_LENGTH;
+    scsi_ulto2b(SHORT_INQUIRY_LENGTH, inq_cmd->length);
     
     /* hand it off for processing */
     mlx_perform(unit, mlx_command, &cmd);
diff -r ebe87ce4d7dd -r 1750de5b0399 head/usr.sbin/newsyslog/newsyslog.c
--- a/head/usr.sbin/newsyslog/newsyslog.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/usr.sbin/newsyslog/newsyslog.c	Fri Jan 13 15:51:01 2012 +0200
@@ -54,7 +54,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/usr.sbin/newsyslog/newsyslog.c 229403 2012-01-03 18:51:58Z ed $");
+__FBSDID("$FreeBSD: head/usr.sbin/newsyslog/newsyslog.c 229654 2012-01-05 21:36:36Z uqs $");
 
 #define	OSF
 
@@ -1600,7 +1600,7 @@
  * Generate a log filename, when using classic filenames.
  */
 static void
-gen_clasiclog_fname(char *fname, size_t fname_sz, const char *archive_dir,
+gen_classiclog_fname(char *fname, size_t fname_sz, const char *archive_dir,
     const char *namepart, int numlogs_c)
 {
 
@@ -1612,15 +1612,15 @@
 }
 
 /*
- * Delete a rotated logfiles, when using classic filenames.
+ * Delete a rotated logfile, when using classic filenames.
  */
 static void
-delete_clasiclog(const char *archive_dir, const char *namepart, int numlog_c)
+delete_classiclog(const char *archive_dir, const char *namepart, int numlog_c)
 {
 	char file1[MAXPATHLEN], zfile1[MAXPATHLEN];
 	int c;
 
-	gen_clasiclog_fname(file1, sizeof(file1), archive_dir, namepart,
+	gen_classiclog_fname(file1, sizeof(file1), archive_dir, namepart,
 	    numlog_c);
 
 	for (c = 0; c < COMPRESS_TYPES; c++) {
@@ -1744,10 +1744,10 @@
 		 * kept ent->numlogs + 1 files.  This code can go away
 		 * at some point in the future.
 		 */
-		delete_clasiclog(dirpart, namepart, ent->numlogs);
+		delete_classiclog(dirpart, namepart, ent->numlogs);
 
 		if (ent->numlogs > 0)
-			delete_clasiclog(dirpart, namepart, ent->numlogs - 1);
+			delete_classiclog(dirpart, namepart, ent->numlogs - 1);
 
 	}
 
@@ -1768,7 +1768,7 @@
 		/* Don't run the code to move down logs */
 		numlogs_c = -1;
 	} else {
-		gen_clasiclog_fname(file1, sizeof(file1), dirpart, namepart,
+		gen_classiclog_fname(file1, sizeof(file1), dirpart, namepart,
 		    ent->numlogs - 1);
 		numlogs_c = ent->numlogs - 2;		/* copy for countdown */
 	}
@@ -1777,7 +1777,7 @@
 	for (; numlogs_c >= 0; numlogs_c--) {
 		(void) strlcpy(file2, file1, sizeof(file2));
 
-		gen_clasiclog_fname(file1, sizeof(file1), dirpart, namepart,
+		gen_classiclog_fname(file1, sizeof(file1), dirpart, namepart,
 		    numlogs_c);
 
 		logfile_suffix = get_logfile_suffix(file1);
diff -r ebe87ce4d7dd -r 1750de5b0399 head/usr.sbin/pkg_install/add/main.c
--- a/head/usr.sbin/pkg_install/add/main.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/usr.sbin/pkg_install/add/main.c	Fri Jan 13 15:51:01 2012 +0200
@@ -19,7 +19,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/usr.sbin/pkg_install/add/main.c 228990 2011-12-30 10:58:14Z uqs $");
+__FBSDID("$FreeBSD: head/usr.sbin/pkg_install/add/main.c 229831 2012-01-09 00:48:19Z eadler $");
 
 #include <sys/param.h>
 #include <sys/sysctl.h>
@@ -285,7 +285,7 @@
     }
     /* Make sure the sub-execs we invoke get found */
     setenv("PATH", 
-	   "/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/X11R6/bin",
+	   "/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin",
 	   1);
 
     /* Set a reasonable umask */
diff -r ebe87ce4d7dd -r 1750de5b0399 head/usr.sbin/pkg_install/lib/plist.c
--- a/head/usr.sbin/pkg_install/lib/plist.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/usr.sbin/pkg_install/lib/plist.c	Fri Jan 13 15:51:01 2012 +0200
@@ -19,7 +19,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/usr.sbin/pkg_install/lib/plist.c 228990 2011-12-30 10:58:14Z uqs $");
+__FBSDID("$FreeBSD: head/usr.sbin/pkg_install/lib/plist.c 229655 2012-01-05 21:36:45Z uqs $");
 
 #include "lib.h"
 #include <err.h>
@@ -123,7 +123,7 @@
 
 /*
  * Delete plist item 'type' in the list (if 'name' is non-null, match it
- * too.)  If 'all' is set, delete all items, not just the first occurrance.
+ * too.)  If 'all' is set, delete all items, not just the first occurrence.
  */
 void
 delete_plist(Package *pkg, Boolean all, plist_t type, const char *name)
diff -r ebe87ce4d7dd -r 1750de5b0399 head/usr.sbin/tzsetup/tzsetup.c
--- a/head/usr.sbin/tzsetup/tzsetup.c	Fri Jan 13 00:58:58 2012 +0200
+++ b/head/usr.sbin/tzsetup/tzsetup.c	Fri Jan 13 15:51:01 2012 +0200
@@ -33,7 +33,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/usr.sbin/tzsetup/tzsetup.c 228176 2011-12-01 09:02:57Z fjoe $");
+__FBSDID("$FreeBSD: head/usr.sbin/tzsetup/tzsetup.c 230005 2012-01-12 05:50:32Z wollman $");
 
 #include <err.h>
 #include <errno.h>
@@ -57,6 +57,13 @@
 #define	_PATH_DB		"/var/db/zoneinfo"
 #define	_PATH_WALL_CMOS_CLOCK	"/etc/wall_cmos_clock"
 
+#ifdef PATH_MAX
+#define	SILLY_BUFFER_SIZE	2*PATH_MAX
+#else
+#warning "Somebody needs to fix this to dynamically size this buffer."
+#define	SILLY_BUFFER_SIZE	2048
+#endif
+
 /* special return codes for `fire' actions */
 #define DITEM_FAILURE           1
 
@@ -638,7 +645,7 @@
 install_zoneinfo_file(const char *zoneinfo_file)
 {
 	char		buf[1024];
-	char		title[64], prompt[64];
+	char		title[64], prompt[SILLY_BUFFER_SIZE];
 	struct stat	sb;
 	ssize_t		len;
 	int		fd1, fd2, copymode;
@@ -709,7 +716,18 @@
 				return (DITEM_FAILURE | DITEM_RECREATE);
 			}
 
-			unlink(path_localtime);
+			if (unlink(path_localtime) < 0) {
+				snprintf(prompt, sizeof(prompt),
+				    "Could not unlink %s: %s",
+				    path_localtime, strerror(errno));
+				if (usedialog) {
+					snprintf(title, sizeof(title), "Error");
+					dialog_msgbox(title, prompt, 8, 72, 1);
+				} else
+					fprintf(stderr, "%s\n", prompt);
+				return (DITEM_FAILURE | DITEM_RECREATE);
+			}
+
 			fd2 = open(path_localtime, O_CREAT | O_EXCL | O_WRONLY,
 			    S_IRUSR | S_IRGRP | S_IROTH);
 			if (fd2 < 0) {
@@ -755,7 +773,17 @@
 					fprintf(stderr, "%s\n", prompt);
 				return (DITEM_FAILURE | DITEM_RECREATE);
 			}
-			unlink(path_localtime);
+			if (unlink(path_localtime) < 0) {
+				snprintf(prompt, sizeof(prompt),
+				    "Could not unlink %s: %s",
+				    path_localtime, strerror(errno));
+				if (usedialog) {
+					snprintf(title, sizeof(title), "Error");
+					dialog_msgbox(title, prompt, 8, 72, 1);
+				} else
+					fprintf(stderr, "%s\n", prompt);
+				return (DITEM_FAILURE | DITEM_RECREATE);
+			}
 			if (symlink(zoneinfo_file, path_localtime) < 0) {
 				snprintf(title, sizeof(title), "Error");
 				snprintf(prompt, sizeof(prompt),


More information about the Zrouter-src-freebsd mailing list