diff --git a/cddl/lib/libdtrace/io.d b/cddl/lib/libdtrace/io.d index fbce09e2fcf7..043291244a50 100644 --- a/cddl/lib/libdtrace/io.d +++ b/cddl/lib/libdtrace/io.d @@ -1,259 +1,262 @@ /* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License (the "License"). * You may not use this file except in compliance with the License. * * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE * or http://www.opensolaris.org/os/licensing. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at usr/src/OPENSOLARIS.LICENSE. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END * * Portions Copyright 2018 Devin Teske dteske@freebsd.org */ /* * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */ #pragma D depends_on module kernel #pragma D depends_on provider io typedef struct devinfo { int dev_major; /* major number */ int dev_minor; /* minor number */ int dev_instance; /* instance number */ int dev_type; /* type of device */ string dev_name; /* name of device */ string dev_statname; /* name of device + instance/minor */ string dev_pathname; /* pathname of device */ } devinfo_t; #pragma D binding "1.0" translator translator devinfo_t < struct devstat *D > { dev_major = D->device_number; dev_minor = D->unit_number; dev_instance = 0; dev_type = D->device_type; dev_name = stringof(D->device_name); dev_statname = stringof(D->device_name); dev_pathname = stringof(D->device_name); }; typedef struct bufinfo { int b_cmd; /* I/O operation */ int b_flags; /* flags */ long b_bcount; /* number of bytes */ caddr_t b_addr; /* buffer address */ uint64_t b_blkno; /* expanded block # on device */ uint64_t b_lblkno; /* block # on device */ size_t b_resid; /* # of bytes not transferred */ size_t b_bufsize; /* size of allocated buffer */ /* caddr_t b_iodone; I/O completion routine */ int b_error; /* expanded error field */ /* dev_t b_edev; extended device */ } bufinfo_t; #pragma D binding "1.0" translator translator bufinfo_t < struct bio *B > { b_cmd = B->bio_cmd; b_flags = B->bio_flags; b_bcount = B->bio_bcount; b_addr = B->bio_data; b_blkno = 0; b_lblkno = 0; b_resid = B->bio_resid; b_bufsize = 0; /* XXX gnn */ b_error = B->bio_error; }; /* * The following inline constants can be used to examine fi_oflags when using * the fds[] array or a translated fileinfo_t. Note that the various open * flags behave as a bit-field *except* for O_RDONLY, O_WRONLY, and O_RDWR. * To test the open mode, you write code similar to that used with the fcntl(2) * F_GET[X]FL command, such as: if ((fi_oflags & O_ACCMODE) == O_WRONLY). */ inline int O_ACCMODE = 0x0003; #pragma D binding "1.1" O_ACCMODE inline int O_RDONLY = 0x0000; #pragma D binding "1.1" O_RDONLY inline int O_WRONLY = 0x0001; #pragma D binding "1.1" O_WRONLY inline int O_RDWR = 0x0002; #pragma D binding "1.1" O_RDWR inline int O_APPEND = 0x0008; #pragma D binding "1.1" O_APPEND inline int O_CREAT = 0x0200; #pragma D binding "1.1" O_CREAT inline int O_EXCL = 0x0800; #pragma D binding "1.1" O_EXCL inline int O_NOCTTY = 0x8000; #pragma D binding "1.1" O_NOCTTY inline int O_NONBLOCK = 0x0004; #pragma D binding "1.1" O_NONBLOCK inline int O_NDELAY = 0x0004; #pragma D binding "1.1" O_NDELAY inline int O_SYNC = 0x0080; #pragma D binding "1.1" O_SYNC inline int O_TRUNC = 0x0400; #pragma D binding "1.1" O_TRUNC /* * The following inline constants can be used to examine bio_cmd of struct bio * or a translated bufinfo_t. */ inline int BIO_READ = 0x01; #pragma D binding "1.13" BIO_READ inline int BIO_WRITE = 0x02; #pragma D binding "1.13" BIO_WRITE inline int BIO_DELETE = 0x03; #pragma D binding "1.13" BIO_DELETE inline int BIO_GETATTR = 0x04; #pragma D binding "1.13" BIO_GETATTR inline int BIO_FLUSH = 0x05; #pragma D binding "1.13" BIO_FLUSH inline int BIO_CMD0 = 0x06; #pragma D binding "1.13" BIO_CMD0 inline int BIO_CMD1 = 0x07; #pragma D binding "1.13" BIO_CMD1 inline int BIO_CMD2 = 0x08; #pragma D binding "1.13" BIO_CMD2 inline int BIO_ZONE = 0x09; #pragma D binding "1.13" BIO_ZONE /* * The following inline constants can be used to examine bio_flags of struct * bio or a translated bufinfo_t. */ inline int BIO_ERROR = 0x01; #pragma D binding "1.13" BIO_ERROR inline int BIO_DONE = 0x02; #pragma D binding "1.13" BIO_DONE inline int BIO_ONQUEUE = 0x04; #pragma D binding "1.13" BIO_ONQUEUE inline int BIO_ORDERED = 0x08; #pragma D binding "1.13" BIO_ORDERED inline int BIO_UNMAPPED = 0x10; #pragma D binding "1.13" BIO_UNMAPPED inline int BIO_TRANSIENT_MAPPING = 0x20; #pragma D binding "1.13" BIO_TRANSIENT_MAPPING inline int BIO_VLIST = 0x40; #pragma D binding "1.13" BIO_VLIST /* * The following inline constants can be used to examine device_type of struct * devstat or a translated devinfo_t. */ inline int DEVSTAT_TYPE_DIRECT = 0x000; #pragma D binding "1.13" DEVSTAT_TYPE_DIRECT inline int DEVSTAT_TYPE_SEQUENTIAL = 0x001; #pragma D binding "1.13" DEVSTAT_TYPE_SEQUENTIAL inline int DEVSTAT_TYPE_PRINTER = 0x002; #pragma D binding "1.13" DEVSTAT_TYPE_PRINTER inline int DEVSTAT_TYPE_PROCESSOR = 0x003; #pragma D binding "1.13" DEVSTAT_TYPE_PROCESSOR inline int DEVSTAT_TYPE_WORM = 0x004; #pragma D binding "1.13" DEVSTAT_TYPE_WORM inline int DEVSTAT_TYPE_CDROM = 0x005; #pragma D binding "1.13" DEVSTAT_TYPE_CDROM inline int DEVSTAT_TYPE_SCANNER = 0x006; #pragma D binding "1.13" DEVSTAT_TYPE_SCANNER inline int DEVSTAT_TYPE_OPTICAL = 0x007; #pragma D binding "1.13" DEVSTAT_TYPE_OPTICAL inline int DEVSTAT_TYPE_CHANGER = 0x008; #pragma D binding "1.13" DEVSTAT_TYPE_CHANGER inline int DEVSTAT_TYPE_COMM = 0x009; #pragma D binding "1.13" DEVSTAT_TYPE_COMM inline int DEVSTAT_TYPE_ASC0 = 0x00a; #pragma D binding "1.13" DEVSTAT_TYPE_ASC0 inline int DEVSTAT_TYPE_ASC1 = 0x00b; #pragma D binding "1.13" DEVSTAT_TYPE_ASC1 inline int DEVSTAT_TYPE_STORARRAY = 0x00c; #pragma D binding "1.13" DEVSTAT_TYPE_STORARRAY inline int DEVSTAT_TYPE_ENCLOSURE = 0x00d; #pragma D binding "1.13" DEVSTAT_TYPE_ENCLOSURE inline int DEVSTAT_TYPE_FLOPPY = 0x00e; #pragma D binding "1.13" DEVSTAT_TYPE_FLOPPY inline int DEVSTAT_TYPE_MASK = 0x00f; #pragma D binding "1.13" DEVSTAT_TYPE_MASK inline int DEVSTAT_TYPE_IF_SCSI = 0x010; #pragma D binding "1.13" DEVSTAT_TYPE_IF_SCSI inline int DEVSTAT_TYPE_IF_IDE = 0x020; #pragma D binding "1.13" DEVSTAT_TYPE_IF_IDE inline int DEVSTAT_TYPE_IF_OTHER = 0x030; #pragma D binding "1.13" DEVSTAT_TYPE_IF_OTHER +inline int DEVSTAT_TYPE_IF_NVM = 0x040; +#pragma D binding "1.13" DEVSTAT_TYPE_IF_NVME inline int DEVSTAT_TYPE_IF_MASK = 0x0f0; #pragma D binding "1.13" DEVSTAT_TYPE_IF_MASK inline int DEVSTAT_TYPE_PASS = 0x100; #pragma D binding "1.13" DEVSTAT_TYPE_PASS #pragma D binding "1.13" device_type_string inline string device_type_string[int type] = type == DEVSTAT_TYPE_DIRECT ? "DIRECT" : type == DEVSTAT_TYPE_SEQUENTIAL ? "SEQUENTIAL" : type == DEVSTAT_TYPE_PRINTER ? "PRINTER" : type == DEVSTAT_TYPE_PROCESSOR ? "PROCESSOR" : type == DEVSTAT_TYPE_WORM ? "WORM" : type == DEVSTAT_TYPE_CDROM ? "CDROM" : type == DEVSTAT_TYPE_SCANNER ? "SCANNER" : type == DEVSTAT_TYPE_OPTICAL ? "OPTICAL" : type == DEVSTAT_TYPE_CHANGER ? "CHANGER" : type == DEVSTAT_TYPE_COMM ? "COMM" : type == DEVSTAT_TYPE_ASC0 ? "ASC0" : type == DEVSTAT_TYPE_ASC1 ? "ASC1" : type == DEVSTAT_TYPE_STORARRAY ? "STORARRAY" : type == DEVSTAT_TYPE_ENCLOSURE ? "ENCLOSURE" : type == DEVSTAT_TYPE_FLOPPY ? "FLOPPY" : strjoin("UNKNOWN(", strjoin(lltostr(type), ")")); #pragma D binding "1.13" device_type inline string device_type[int type] = device_type_string[type & DEVSTAT_TYPE_MASK]; #pragma D binding "1.13" device_if_string inline string device_if_string[int type] = type == 0 ? "ACCESS" : type == DEVSTAT_TYPE_IF_SCSI ? "SCSI" : type == DEVSTAT_TYPE_IF_IDE ? "IDE" : type == DEVSTAT_TYPE_IF_OTHER ? "OTHER" : + type == DEVSTAT_TYPE_IF_NVME ? "NVME" : strjoin("UNKNOWN(", strjoin(lltostr(type), ")")); #pragma D binding "1.13" device_if inline string device_if[int type] = device_if_string[type & DEVSTAT_TYPE_IF_MASK]; #pragma D binding "1.13" bio_cmd_string inline string bio_cmd_string[int cmd] = cmd == BIO_READ ? "READ" : cmd == BIO_WRITE ? "WRITE" : cmd == BIO_DELETE ? "DELETE" : cmd == BIO_GETATTR ? "GETATTR" : cmd == BIO_FLUSH ? "FLUSH" : cmd == BIO_CMD0 ? "CMD0" : cmd == BIO_CMD1 ? "CMD1" : cmd == BIO_CMD2 ? "CMD2" : cmd == BIO_ZONE ? "ZONE" : strjoin("UNKNOWN(", strjoin(lltostr(cmd), ")")); #pragma D binding "1.13" bio_flag_string inline string bio_flag_string[int flag] = flag == BIO_ERROR ? "ERROR" : flag == BIO_DONE ? "DONE" : flag == BIO_ONQUEUE ? "ONQUEUE" : flag == BIO_ORDERED ? "ORDERED" : flag == BIO_UNMAPPED ? "UNMAPPED" : flag == BIO_TRANSIENT_MAPPING ? "TRANSIENT_MAPPING" : flag == BIO_VLIST ? "VLIST" : ""; diff --git a/lib/libdevstat/devstat.3 b/lib/libdevstat/devstat.3 index 20f5fdd5649c..d0eaff359e15 100644 --- a/lib/libdevstat/devstat.3 +++ b/lib/libdevstat/devstat.3 @@ -1,822 +1,824 @@ .\" .\" Copyright (c) 1998, 1999, 2001 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. .\" .Dd December 15, 2012 .Dt DEVSTAT 3 .Os .Sh NAME .Nm devstat , .Nm devstat_getnumdevs , .Nm devstat_getgeneration , .Nm devstat_getversion , .Nm devstat_checkversion , .Nm devstat_getdevs , .Nm devstat_selectdevs , .Nm devstat_buildmatch , .Nm devstat_compute_statistics , .Nm devstat_compute_etime .Nd device statistics utility library .Sh LIBRARY .Lb libdevstat .Sh SYNOPSIS .In devstat.h .Ft int .Fn devstat_getnumdevs "kvm_t *kd" .Ft long .Fn devstat_getgeneration "kvm_t *kd" .Ft int .Fn devstat_getversion "kvm_t *kd" .Ft int .Fn devstat_checkversion "kvm_t *kd" .Ft int .Fn devstat_getdevs "kvm_t *kd" "struct statinfo *stats" .Ft int .Fo devstat_selectdevs .Fa "struct device_selection **dev_select" .Fa "int *num_selected" .Fa "int *num_selections" .Fa "long *select_generation" .Fa "long current_generation" .Fa "struct devstat *devices" .Fa "int numdevs" .Fa "struct devstat_match *matches" .Fa "int num_matches" .Fa "char **dev_selections" .Fa "int num_dev_selections" .Fa "devstat_select_mode select_mode" .Fa "int maxshowdevs" .Fa "int perf_select" .Fc .Ft int .Fo devstat_buildmatch .Fa "char *match_str" .Fa "struct devstat_match **matches" .Fa "int *num_matches" .Fc .Ft int .Fo devstat_compute_statistics .Fa "struct devstat *current" .Fa "struct devstat *previous" .Fa "long double etime" .Fa "..." .Fc .Ft "long double" .Fo devstat_compute_etime .Fa "struct bintime *cur_time" .Fa "struct bintime *prev_time" .Fc .Sh DESCRIPTION The .Nm library is a library of helper functions for dealing with the kernel .Xr devstat 9 interface, which is accessible to users via .Xr sysctl 3 and .Xr kvm 3 . All functions that take a .Vt "kvm_t *" as first argument can be passed .Dv NULL instead of a kvm handle as this argument, which causes the data to be read via .Xr sysctl 3 . Otherwise, it is read via .Xr kvm 3 using the supplied handle. The .Fn devstat_checkversion function should be called with each kvm handle that is going to be used (or with .Dv NULL if .Xr sysctl 3 is going to be used). .Pp The .Fn devstat_getnumdevs function returns the number of devices registered with the .Nm subsystem in the kernel. .Pp The .Fn devstat_getgeneration function returns the current generation of the .Nm list of devices in the kernel. .Pp The .Fn devstat_getversion function returns the current kernel .Nm version. .Pp The .Fn devstat_checkversion function checks the userland .Nm version against the kernel .Nm version. If the two are identical, it returns zero. Otherwise, it prints an appropriate error in .Va devstat_errbuf and returns \-1. .Pp The .Fn devstat_getdevs function fetches the current list of devices and statistics into the supplied .Vt statinfo structure. The .Vt statinfo structure can be found in .In devstat.h : .Bd -literal -offset indent struct statinfo { long cp_time[CPUSTATES]; long tk_nin; long tk_nout; struct devinfo *dinfo; long double snap_time; }; .Ed .Pp The .Fn devstat_getdevs function expects the .Vt statinfo structure to be allocated, and it also expects the .Va dinfo subelement to be allocated and zeroed prior to the first invocation of .Fn devstat_getdevs . The .Va dinfo subelement is used to store state between calls, and should not be modified after the first call to .Fn devstat_getdevs . The .Va dinfo subelement contains the following elements: .Bd -literal -offset indent struct devinfo { struct devstat *devices; uint8_t *mem_ptr; long generation; int numdevs; }; .Ed .Pp The .Va kern.devstat.all .Xr sysctl 8 variable contains an array of .Nm structures, but at the head of the array is the current .Nm generation. The reason the generation is at the head of the buffer is so that userland software accessing the .Nm statistics information can atomically get both the statistics information and the corresponding generation number. If client software were forced to get the generation number via a separate .Xr sysctl 8 variable (which is available for convenience), the list of devices could change between the time the client gets the generation and the time the client gets the device list. .Pp The .Va mem_ptr subelement of the .Vt devinfo structure is a pointer to memory that is allocated, and resized if necessary, by .Fn devstat_getdevs . The devices subelement of the .Vt devinfo structure is basically a pointer to the beginning of the array of devstat structures from the .Va kern.devstat.all .Xr sysctl 8 variable (or the corresponding values read via .Xr kvm 3 ) . The generation subelement of the .Vt devinfo structure contains the corresponding generation number. The .Va numdevs subelement of the .Vt devinfo structure contains the current number of devices registered with the kernel .Nm subsystem. .Pp The .Fn devstat_selectdevs function selects devices to display based upon a number of criteria: .Bl -tag -width indent .It specified devices Specified devices are the first selection priority. These are generally devices specified by name by the user e.g.\& .Li da0 , da1 , cd0 . .It match patterns These are pattern matching expressions generated by .Fn devstat_buildmatch from user input. .It performance If performance mode is enabled, devices will be sorted based on the .Va bytes field in the .Vt device_selection structure passed in to .Fn devstat_selectdevs . The .Va bytes value currently must be maintained by the user. In the future, this may be done for him in a .Nm library routine. If no devices have been selected by name or by pattern, the performance tracking code will select every device in the system, and sort them by performance. If devices have been selected by name or pattern, the performance tracking code will honor those selections and will only sort among the selected devices. .It order in the devstat list If the selection mode is set to .Dv DS_SELECT_ADD , and if there are still less than .Fa maxshowdevs devices selected, .Fn devstat_selectdevs will automatically select up to .Fa maxshowdevs devices. .El .Pp The .Fn devstat_selectdevs function performs selections in four different modes: .Bl -tag -width ".Dv DS_SELECT_ADDONLY" .It Dv DS_SELECT_ADD In .Dq add mode, .Fn devstat_selectdevs will select any unselected devices specified by name or matching pattern. It will also select more devices, in devstat list order, until the number of selected devices is equal to .Fa maxshowdevs or until all devices are selected. .It Dv DS_SELECT_ONLY In .Dq only mode, .Fn devstat_selectdevs will clear all current selections, and will only select devices specified by name or by matching pattern. .It Dv DS_SELECT_REMOVE In .Dq remove mode, .Fn devstat_selectdevs will remove devices specified by name or by matching pattern. It will not select any additional devices. .It Dv DS_SELECT_ADDONLY In .Dq "add only" mode, .Fn devstat_selectdevs will select any unselected devices specified by name or matching pattern. In this respect it is identical to .Dq add mode. It will not, however, select any devices other than those specified. .El .Pp In all selection modes, .Fn devstat_selectdevs will not select any more than .Fa maxshowdevs devices. One exception to this is when you are in .Dq top mode and no devices have been selected. In this case, .Fn devstat_selectdevs will select every device in the system. Client programs must pay attention to selection order when deciding whether to pay attention to a particular device. This may be the wrong behavior, and probably requires additional thought. .Pp The .Fn devstat_selectdevs function handles allocation and resizing of the .Fa dev_select structure passed in by the client. The .Fn devstat_selectdevs function uses the .Fa numdevs and .Fa current_generation fields to track the current .Nm generation and number of devices. If .Fa num_selections is not the same as .Fa numdevs or if .Fa select_generation is not the same as .Fa current_generation , .Fn devstat_selectdevs will resize the selection list as necessary, and re-initialize the selection array. .Pp The .Fn devstat_buildmatch function takes a comma separated match string and compiles it into a .Vt devstat_match structure that is understood by .Fn devstat_selectdevs . Match strings have the following format: .Pp .D1 Ar device , Ns Ar type , Ns Ar if .Pp The .Fn devstat_buildmatch function takes care of allocating and reallocating the match list as necessary. Currently known match types include: .Bl -tag -width indent .It device type: .Bl -tag -width ".Li enclosure" -compact .It Li da Direct Access devices .It Li sa Sequential Access devices .It Li printer Printers .It Li proc Processor devices .It Li worm Write Once Read Multiple devices .It Li cd CD devices .It Li scanner Scanner devices .It Li optical Optical Memory devices .It Li changer Medium Changer devices .It Li comm Communication devices .It Li array Storage Array devices .It Li enclosure Enclosure Services devices .It Li floppy Floppy devices .El .It interface: .Bl -tag -width ".Li enclosure" -compact .It Li IDE Integrated Drive Electronics devices .It Li SCSI Small Computer System Interface devices +.It Li NVME +NVM Express Interface devices .It Li other Any other device interface .El .It passthrough: .Bl -tag -width ".Li enclosure" -compact .It Li pass Passthrough devices .El .El .Pp The .Fn devstat_compute_statistics function provides complete statistics calculation. There are four arguments for which values .Em must be supplied: .Fa current , .Fa previous , .Fa etime , and the terminating argument for the varargs list, .Dv DSM_NONE . For most applications, the user will want to supply valid .Vt devstat structures for both .Fa current and .Fa previous . In some instances, for instance when calculating statistics since system boot, the user may pass in a .Dv NULL pointer for the .Fa previous argument. In that case, .Fn devstat_compute_statistics will use the total stats in the .Fa current structure to calculate statistics over .Fa etime . For each statistics to be calculated, the user should supply the proper enumerated type (listed below), and a variable of the indicated type. All statistics are either integer values, for which a .Vt uint64_t is used, or floating point, for which a .Vt "long double" is used. The statistics that may be calculated are: .Bl -tag -width ".Dv DSM_TRANSFERS_PER_SECOND_OTHER" .It Dv DSM_NONE type: N/A .Pp This .Em must be the last argument passed to .Fn devstat_compute_statistics . It is an argument list terminator. .It Dv DSM_TOTAL_BYTES type: .Vt "uint64_t *" .Pp The total number of bytes transferred between the acquisition of .Fa previous and .Fa current . .It Dv DSM_TOTAL_BYTES_READ .It Dv DSM_TOTAL_BYTES_WRITE .It Dv DSM_TOTAL_BYTES_FREE type: .Vt "uint64_t *" .Pp The total number of bytes in transactions of the specified type between the acquisition of .Fa previous and .Fa current . .It Dv DSM_TOTAL_TRANSFERS type: .Vt "uint64_t *" .Pp The total number of transfers between the acquisition of .Fa previous and .Fa current . .It Dv DSM_TOTAL_TRANSFERS_OTHER .It Dv DSM_TOTAL_TRANSFERS_READ .It Dv DSM_TOTAL_TRANSFERS_WRITE .It Dv DSM_TOTAL_TRANSFERS_FREE type: .Vt "uint64_t *" .Pp The total number of transactions of the specified type between the acquisition of .Fa previous and .Fa current . .It Dv DSM_TOTAL_DURATION type: .Vt "long double *" .Pp The total duration of transactions, in seconds, between the acquisition of .Fa previous and .Fa current . .It Dv DSM_TOTAL_DURATION_OTHER .It Dv DSM_TOTAL_DURATION_READ .It Dv DSM_TOTAL_DURATION_WRITE .It Dv DSM_TOTAL_DURATION_FREE type: .Vt "long double *" .Pp The total duration of transactions of the specified type between the acquisition of .Fa previous and .Fa current . .It Dv DSM_TOTAL_BUSY_TIME type: .Vt "long double *" .Pp Total time the device had one or more transactions outstanding between the acquisition of .Fa previous and .Fa current . .It Dv DSM_TOTAL_BLOCKS type: .Vt "uint64_t *" .Pp The total number of blocks transferred between the acquisition of .Fa previous and .Fa current . This number is in terms of the blocksize reported by the device. If no blocksize has been reported (i.e., the block size is 0), a default blocksize of 512 bytes will be used in the calculation. .It Dv DSM_TOTAL_BLOCKS_READ .It Dv DSM_TOTAL_BLOCKS_WRITE .It Dv DSM_TOTAL_BLOCKS_FREE type: .Vt "uint64_t *" .Pp The total number of blocks of the specified type between the acquisition of .Fa previous and .Fa current . This number is in terms of the blocksize reported by the device. If no blocksize has been reported (i.e., the block size is 0), a default blocksize of 512 bytes will be used in the calculation. .It Dv DSM_KB_PER_TRANSFER type: .Vt "long double *" .Pp The average number of kilobytes per transfer between the acquisition of .Fa previous and .Fa current . .It Dv DSM_KB_PER_TRANSFER_READ .It Dv DSM_KB_PER_TRANSFER_WRITE .It Dv DSM_KB_PER_TRANSFER_FREE type: .Vt "long double *" .Pp The average number of kilobytes in the specified type transaction between the acquisition of .Fa previous and .Fa current . .It Dv DSM_TRANSFERS_PER_SECOND type: .Vt "long double *" .Pp The average number of transfers per second between the acquisition of .Fa previous and .Fa current . .It Dv DSM_TRANSFERS_PER_SECOND_OTHER .It Dv DSM_TRANSFERS_PER_SECOND_READ .It Dv DSM_TRANSFERS_PER_SECOND_WRITE .It Dv DSM_TRANSFERS_PER_SECOND_FREE type: .Vt "long double *" .Pp The average number of transactions of the specified type per second between the acquisition of .Fa previous and .Fa current . .It Dv DSM_MB_PER_SECOND type: .Vt "long double *" .Pp The average number of megabytes transferred per second between the acquisition of .Fa previous and .Fa current . .It Dv DSM_MB_PER_SECOND_READ .It Dv DSM_MB_PER_SECOND_WRITE .It Dv DSM_MB_PER_SECOND_FREE type: .Vt "long double *" .Pp The average number of megabytes per second in the specified type of transaction between the acquisition of .Fa previous and .Fa current . .It Dv DSM_BLOCKS_PER_SECOND type: .Vt "long double *" .Pp The average number of blocks transferred per second between the acquisition of .Fa previous and .Fa current . This number is in terms of the blocksize reported by the device. If no blocksize has been reported (i.e., the block size is 0), a default blocksize of 512 bytes will be used in the calculation. .It Dv DSM_BLOCKS_PER_SECOND_READ .It Dv DSM_BLOCKS_PER_SECOND_WRITE .It Dv DSM_BLOCKS_PER_SECOND_FREE type: .Vt "long double *" .Pp The average number of blocks per second in the specified type of transaction between the acquisition of .Fa previous and .Fa current . This number is in terms of the blocksize reported by the device. If no blocksize has been reported (i.e., the block size is 0), a default blocksize of 512 bytes will be used in the calculation. .It Dv DSM_MS_PER_TRANSACTION type: .Vt "long double *" .Pp The average duration of transactions between the acquisition of .Fa previous and .Fa current . .It Dv DSM_MS_PER_TRANSACTION_OTHER .It Dv DSM_MS_PER_TRANSACTION_READ .It Dv DSM_MS_PER_TRANSACTION_WRITE .It Dv DSM_MS_PER_TRANSACTION_FREE type: .Vt "long double *" .Pp The average duration of transactions of the specified type between the acquisition of .Fa previous and .Fa current . .It Dv DSM_BUSY_PCT type: .Vt "long double *" .Pp The percentage of time the device had one or more transactions outstanding between the acquisition of .Fa previous and .Fa current . .It Dv DSM_QUEUE_LENGTH type: .Vt "uint64_t *" .Pp The number of not yet completed transactions at the time when .Fa current was acquired. .It Dv DSM_SKIP type: N/A .Pp If you do not need a result from .Fn devstat_compute_statistics , just put .Dv DSM_SKIP as first (type) parameter and .Dv NULL as second parameter. This can be useful in scenarios where the statistics to be calculated are determined at run time. .El .Pp The .Fn devstat_compute_etime function provides an easy way to find the difference in seconds between two .Vt bintime structures. This is most commonly used in conjunction with the time recorded by the .Fn devstat_getdevs function (in .Vt "struct statinfo" ) each time it fetches the current .Nm list. .Sh RETURN VALUES The .Fn devstat_getnumdevs , .Fn devstat_getgeneration , and .Fn devstat_getversion function return the indicated sysctl variable, or \-1 if there is an error fetching the variable. .Pp The .Fn devstat_checkversion function returns 0 if the kernel and userland .Nm versions match. If they do not match, it returns \-1. .Pp The .Fn devstat_getdevs and .Fn devstat_selectdevs functions return \-1 in case of an error, 0 if there is no error, and 1 if the device list or selected devices have changed. A return value of 1 from .Fn devstat_getdevs is usually a hint to re-run .Fn devstat_selectdevs because the device list has changed. .Pp The .Fn devstat_buildmatch function returns \-1 for error, and 0 if there is no error. .Pp The .Fn devstat_compute_etime function returns the computed elapsed time. .Pp The .Fn devstat_compute_statistics function returns \-1 for error, and 0 for success. .Pp If an error is returned from one of the .Nm library functions, the reason for the error is generally printed in the global string .Va devstat_errbuf which is .Dv DEVSTAT_ERRBUF_SIZE characters long. .Sh SEE ALSO .Xr systat 1 , .Xr kvm 3 , .Xr sysctl 3 , .Xr iostat 8 , .Xr rpc.rstatd 8 , .Xr sysctl 8 , .Xr vmstat 8 , .Xr devstat 9 .Sh HISTORY The .Nm statistics system first appeared in .Fx 3.0 . The new interface (the functions prefixed with .Li devstat_ ) first appeared in .Fx 5.0 . .Sh AUTHORS .An Kenneth Merry Aq Mt ken@FreeBSD.org .Sh BUGS There should probably be an interface to de-allocate memory allocated by .Fn devstat_getdevs , .Fn devstat_selectdevs , and .Fn devstat_buildmatch . .Pp The .Fn devstat_selectdevs function should probably not select more than .Fa maxshowdevs devices in .Dq top mode when no devices have been selected previously. .Pp There should probably be functions to perform the statistics buffer swapping that goes on in most of the clients of this library. .Pp The .Vt statinfo and .Vt devinfo structures should probably be cleaned up and thought out a little more. diff --git a/lib/libdevstat/devstat.c b/lib/libdevstat/devstat.c index 7465613da4f1..7aca8c5733d0 100644 --- a/lib/libdevstat/devstat.c +++ b/lib/libdevstat/devstat.c @@ -1,1675 +1,1676 @@ /*- * SPDX-License-Identifier: BSD-3-Clause * * Copyright (c) 1997, 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. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "devstat.h" int compute_stats(struct devstat *current, struct devstat *previous, long double etime, u_int64_t *total_bytes, u_int64_t *total_transfers, u_int64_t *total_blocks, long double *kb_per_transfer, long double *transfers_per_second, long double *mb_per_second, long double *blocks_per_second, long double *ms_per_transaction); typedef enum { DEVSTAT_ARG_NOTYPE, DEVSTAT_ARG_UINT64, DEVSTAT_ARG_LD, DEVSTAT_ARG_SKIP } devstat_arg_type; char devstat_errbuf[DEVSTAT_ERRBUF_SIZE]; /* * Table to match descriptive strings with device types. These are in * order from most common to least common to speed search time. */ struct devstat_match_table match_table[] = { {"da", DEVSTAT_TYPE_DIRECT, DEVSTAT_MATCH_TYPE}, {"cd", DEVSTAT_TYPE_CDROM, DEVSTAT_MATCH_TYPE}, {"scsi", DEVSTAT_TYPE_IF_SCSI, DEVSTAT_MATCH_IF}, {"ide", DEVSTAT_TYPE_IF_IDE, DEVSTAT_MATCH_IF}, {"other", DEVSTAT_TYPE_IF_OTHER, DEVSTAT_MATCH_IF}, + {"nvme", DEVSTAT_TYPE_IF_NVME, DEVSTAT_MATCH_IF}, {"worm", DEVSTAT_TYPE_WORM, DEVSTAT_MATCH_TYPE}, {"sa", DEVSTAT_TYPE_SEQUENTIAL,DEVSTAT_MATCH_TYPE}, {"pass", DEVSTAT_TYPE_PASS, DEVSTAT_MATCH_PASS}, {"optical", DEVSTAT_TYPE_OPTICAL, DEVSTAT_MATCH_TYPE}, {"array", DEVSTAT_TYPE_STORARRAY, DEVSTAT_MATCH_TYPE}, {"changer", DEVSTAT_TYPE_CHANGER, DEVSTAT_MATCH_TYPE}, {"scanner", DEVSTAT_TYPE_SCANNER, DEVSTAT_MATCH_TYPE}, {"printer", DEVSTAT_TYPE_PRINTER, DEVSTAT_MATCH_TYPE}, {"floppy", DEVSTAT_TYPE_FLOPPY, DEVSTAT_MATCH_TYPE}, {"proc", DEVSTAT_TYPE_PROCESSOR, DEVSTAT_MATCH_TYPE}, {"comm", DEVSTAT_TYPE_COMM, DEVSTAT_MATCH_TYPE}, {"enclosure", DEVSTAT_TYPE_ENCLOSURE, DEVSTAT_MATCH_TYPE}, {NULL, 0, 0} }; struct devstat_args { devstat_metric metric; devstat_arg_type argtype; } devstat_arg_list[] = { { DSM_NONE, DEVSTAT_ARG_NOTYPE }, { DSM_TOTAL_BYTES, DEVSTAT_ARG_UINT64 }, { DSM_TOTAL_BYTES_READ, DEVSTAT_ARG_UINT64 }, { DSM_TOTAL_BYTES_WRITE, DEVSTAT_ARG_UINT64 }, { DSM_TOTAL_TRANSFERS, DEVSTAT_ARG_UINT64 }, { DSM_TOTAL_TRANSFERS_READ, DEVSTAT_ARG_UINT64 }, { DSM_TOTAL_TRANSFERS_WRITE, DEVSTAT_ARG_UINT64 }, { DSM_TOTAL_TRANSFERS_OTHER, DEVSTAT_ARG_UINT64 }, { DSM_TOTAL_BLOCKS, DEVSTAT_ARG_UINT64 }, { DSM_TOTAL_BLOCKS_READ, DEVSTAT_ARG_UINT64 }, { DSM_TOTAL_BLOCKS_WRITE, DEVSTAT_ARG_UINT64 }, { DSM_KB_PER_TRANSFER, DEVSTAT_ARG_LD }, { DSM_KB_PER_TRANSFER_READ, DEVSTAT_ARG_LD }, { DSM_KB_PER_TRANSFER_WRITE, DEVSTAT_ARG_LD }, { DSM_TRANSFERS_PER_SECOND, DEVSTAT_ARG_LD }, { DSM_TRANSFERS_PER_SECOND_READ, DEVSTAT_ARG_LD }, { DSM_TRANSFERS_PER_SECOND_WRITE, DEVSTAT_ARG_LD }, { DSM_TRANSFERS_PER_SECOND_OTHER, DEVSTAT_ARG_LD }, { DSM_MB_PER_SECOND, DEVSTAT_ARG_LD }, { DSM_MB_PER_SECOND_READ, DEVSTAT_ARG_LD }, { DSM_MB_PER_SECOND_WRITE, DEVSTAT_ARG_LD }, { DSM_BLOCKS_PER_SECOND, DEVSTAT_ARG_LD }, { DSM_BLOCKS_PER_SECOND_READ, DEVSTAT_ARG_LD }, { DSM_BLOCKS_PER_SECOND_WRITE, DEVSTAT_ARG_LD }, { DSM_MS_PER_TRANSACTION, DEVSTAT_ARG_LD }, { DSM_MS_PER_TRANSACTION_READ, DEVSTAT_ARG_LD }, { DSM_MS_PER_TRANSACTION_WRITE, DEVSTAT_ARG_LD }, { DSM_SKIP, DEVSTAT_ARG_SKIP }, { DSM_TOTAL_BYTES_FREE, DEVSTAT_ARG_UINT64 }, { DSM_TOTAL_TRANSFERS_FREE, DEVSTAT_ARG_UINT64 }, { DSM_TOTAL_BLOCKS_FREE, DEVSTAT_ARG_UINT64 }, { DSM_KB_PER_TRANSFER_FREE, DEVSTAT_ARG_LD }, { DSM_MB_PER_SECOND_FREE, DEVSTAT_ARG_LD }, { DSM_TRANSFERS_PER_SECOND_FREE, DEVSTAT_ARG_LD }, { DSM_BLOCKS_PER_SECOND_FREE, DEVSTAT_ARG_LD }, { DSM_MS_PER_TRANSACTION_OTHER, DEVSTAT_ARG_LD }, { DSM_MS_PER_TRANSACTION_FREE, DEVSTAT_ARG_LD }, { DSM_BUSY_PCT, DEVSTAT_ARG_LD }, { DSM_QUEUE_LENGTH, DEVSTAT_ARG_UINT64 }, { DSM_TOTAL_DURATION, DEVSTAT_ARG_LD }, { DSM_TOTAL_DURATION_READ, DEVSTAT_ARG_LD }, { DSM_TOTAL_DURATION_WRITE, DEVSTAT_ARG_LD }, { DSM_TOTAL_DURATION_FREE, DEVSTAT_ARG_LD }, { DSM_TOTAL_DURATION_OTHER, DEVSTAT_ARG_LD }, { DSM_TOTAL_BUSY_TIME, DEVSTAT_ARG_LD }, }; static const char *namelist[] = { #define X_NUMDEVS 0 "_devstat_num_devs", #define X_GENERATION 1 "_devstat_generation", #define X_VERSION 2 "_devstat_version", #define X_DEVICE_STATQ 3 "_device_statq", #define X_TIME_UPTIME 4 "_time_uptime", #define X_END 5 }; /* * Local function declarations. */ static int compare_select(const void *arg1, const void *arg2); static int readkmem(kvm_t *kd, unsigned long addr, void *buf, size_t nbytes); static int readkmem_nl(kvm_t *kd, const char *name, void *buf, size_t nbytes); static char *get_devstat_kvm(kvm_t *kd); #define KREADNL(kd, var, val) \ readkmem_nl(kd, namelist[var], &val, sizeof(val)) int devstat_getnumdevs(kvm_t *kd) { size_t numdevsize; int numdevs; numdevsize = sizeof(int); /* * Find out how many devices we have in the system. */ if (kd == NULL) { if (sysctlbyname("kern.devstat.numdevs", &numdevs, &numdevsize, NULL, 0) == -1) { snprintf(devstat_errbuf, sizeof(devstat_errbuf), "%s: error getting number of devices\n" "%s: %s", __func__, __func__, strerror(errno)); return(-1); } else return(numdevs); } else { if (KREADNL(kd, X_NUMDEVS, numdevs) == -1) return(-1); else return(numdevs); } } /* * This is an easy way to get the generation number, but the generation is * supplied in a more atmoic manner by the kern.devstat.all sysctl. * Because this generation sysctl is separate from the statistics sysctl, * the device list and the generation could change between the time that * this function is called and the device list is retrieved. */ long devstat_getgeneration(kvm_t *kd) { size_t gensize; long generation; gensize = sizeof(long); /* * Get the current generation number. */ if (kd == NULL) { if (sysctlbyname("kern.devstat.generation", &generation, &gensize, NULL, 0) == -1) { snprintf(devstat_errbuf, sizeof(devstat_errbuf), "%s: error getting devstat generation\n%s: %s", __func__, __func__, strerror(errno)); return(-1); } else return(generation); } else { if (KREADNL(kd, X_GENERATION, generation) == -1) return(-1); else return(generation); } } /* * Get the current devstat version. The return value of this function * should be compared with DEVSTAT_VERSION, which is defined in * sys/devicestat.h. This will enable userland programs to determine * whether they are out of sync with the kernel. */ int devstat_getversion(kvm_t *kd) { size_t versize; int version; versize = sizeof(int); /* * Get the current devstat version. */ if (kd == NULL) { if (sysctlbyname("kern.devstat.version", &version, &versize, NULL, 0) == -1) { snprintf(devstat_errbuf, sizeof(devstat_errbuf), "%s: error getting devstat version\n%s: %s", __func__, __func__, strerror(errno)); return(-1); } else return(version); } else { if (KREADNL(kd, X_VERSION, version) == -1) return(-1); else return(version); } } /* * Check the devstat version we know about against the devstat version the * kernel knows about. If they don't match, print an error into the * devstat error buffer, and return -1. If they match, return 0. */ int devstat_checkversion(kvm_t *kd) { int buflen, res, retval = 0, version; version = devstat_getversion(kd); if (version != DEVSTAT_VERSION) { /* * If getversion() returns an error (i.e. -1), then it * has printed an error message in the buffer. Therefore, * we need to add a \n to the end of that message before we * print our own message in the buffer. */ if (version == -1) buflen = strlen(devstat_errbuf); else buflen = 0; res = snprintf(devstat_errbuf + buflen, DEVSTAT_ERRBUF_SIZE - buflen, "%s%s: userland devstat version %d is not " "the same as the kernel\n%s: devstat " "version %d\n", version == -1 ? "\n" : "", __func__, DEVSTAT_VERSION, __func__, version); if (res < 0) devstat_errbuf[buflen] = '\0'; buflen = strlen(devstat_errbuf); if (version < DEVSTAT_VERSION) res = snprintf(devstat_errbuf + buflen, DEVSTAT_ERRBUF_SIZE - buflen, "%s: libdevstat newer than kernel\n", __func__); else res = snprintf(devstat_errbuf + buflen, DEVSTAT_ERRBUF_SIZE - buflen, "%s: kernel newer than libdevstat\n", __func__); if (res < 0) devstat_errbuf[buflen] = '\0'; retval = -1; } return(retval); } /* * Get the current list of devices and statistics, and the current * generation number. * * Return values: * -1 -- error * 0 -- device list is unchanged * 1 -- device list has changed */ int devstat_getdevs(kvm_t *kd, struct statinfo *stats) { int error; size_t dssize; long oldgeneration; int retval = 0; struct devinfo *dinfo; struct timespec ts; dinfo = stats->dinfo; if (dinfo == NULL) { snprintf(devstat_errbuf, sizeof(devstat_errbuf), "%s: stats->dinfo was NULL", __func__); return(-1); } oldgeneration = dinfo->generation; if (kd == NULL) { clock_gettime(CLOCK_MONOTONIC, &ts); stats->snap_time = ts.tv_sec + ts.tv_nsec * 1e-9; /* If this is our first time through, mem_ptr will be null. */ if (dinfo->mem_ptr == NULL) { /* * Get the number of devices. If it's negative, it's an * error. Don't bother setting the error string, since * getnumdevs() has already done that for us. */ if ((dinfo->numdevs = devstat_getnumdevs(kd)) < 0) return(-1); /* * The kern.devstat.all sysctl returns the current * generation number, as well as all the devices. * So we need four bytes more. */ 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); /* * Request all of the devices. We only really allow for one * ENOMEM failure. It would, of course, be possible to just go * in a loop and keep reallocing the device structure until we * don't get ENOMEM back. I'm not sure it's worth it, though. * If devices are being added to the system that quickly, maybe * the user can just wait until all devices are added. */ for (;;) { error = sysctlbyname("kern.devstat.all", dinfo->mem_ptr, &dssize, NULL, 0); if (error != -1 || errno != EBUSY) break; } if (error == -1) { /* * If we get ENOMEM back, that means that there are * more devices now, so we need to allocate more * space for the device array. */ if (errno == ENOMEM) { /* * No need to set the error string here, * devstat_getnumdevs() will do that if it fails. */ if ((dinfo->numdevs = devstat_getnumdevs(kd)) < 0) return(-1); dssize = (dinfo->numdevs * sizeof(struct devstat)) + sizeof(long); dinfo->mem_ptr = (u_int8_t *) realloc(dinfo->mem_ptr, dssize); if ((error = sysctlbyname("kern.devstat.all", dinfo->mem_ptr, &dssize, NULL, 0)) == -1) { snprintf(devstat_errbuf, sizeof(devstat_errbuf), "%s: error getting device " "stats\n%s: %s", __func__, __func__, strerror(errno)); return(-1); } } else { snprintf(devstat_errbuf, sizeof(devstat_errbuf), "%s: error getting device stats\n" "%s: %s", __func__, __func__, strerror(errno)); return(-1); } } } else { if (KREADNL(kd, X_TIME_UPTIME, ts.tv_sec) == -1) return(-1); else stats->snap_time = ts.tv_sec; /* * This is of course non-atomic, but since we are working * on a core dump, the generation is unlikely to change */ if ((dinfo->numdevs = devstat_getnumdevs(kd)) == -1) return(-1); if ((dinfo->mem_ptr = (u_int8_t *)get_devstat_kvm(kd)) == NULL) return(-1); } /* * The sysctl spits out the generation as the first four bytes, * then all of the device statistics structures. */ dinfo->generation = *(long *)dinfo->mem_ptr; /* * If the generation has changed, and if the current number of * devices is not the same as the number of devices recorded in the * devinfo structure, it is likely that the device list has shrunk. * The reason that it is likely that the device list has shrunk in * this case is that if the device list has grown, the sysctl above * will return an ENOMEM error, and we will reset the number of * devices and reallocate the device array. If the second sysctl * fails, we will return an error and therefore never get to this * point. If the device list has shrunk, the sysctl will not * return an error since we have more space allocated than is * necessary. So, in the shrinkage case, we catch it here and * reallocate the array so that we don't use any more space than is * necessary. */ if (oldgeneration != dinfo->generation) { if (devstat_getnumdevs(kd) != dinfo->numdevs) { if ((dinfo->numdevs = devstat_getnumdevs(kd)) < 0) return(-1); dssize = (dinfo->numdevs * sizeof(struct devstat)) + sizeof(long); dinfo->mem_ptr = (u_int8_t *)realloc(dinfo->mem_ptr, dssize); } retval = 1; } dinfo->devices = (struct devstat *)(dinfo->mem_ptr + sizeof(long)); return(retval); } /* * selectdevs(): * * Devices are selected/deselected based upon the following criteria: * - devices specified by the user on the command line * - devices matching any device type expressions given on the command line * - devices with the highest I/O, if 'top' mode is enabled * - the first n unselected devices in the device list, if maxshowdevs * devices haven't already been selected and if the user has not * specified any devices on the command line and if we're in "add" mode. * * Input parameters: * - device selection list (dev_select) * - current number of devices selected (num_selected) * - total number of devices in the selection list (num_selections) * - devstat generation as of the last time selectdevs() was called * (select_generation) * - current devstat generation (current_generation) * - current list of devices and statistics (devices) * - number of devices in the current device list (numdevs) * - compiled version of the command line device type arguments (matches) * - This is optional. If the number of devices is 0, this will be ignored. * - The matching code pays attention to the current selection mode. So * if you pass in a matching expression, it will be evaluated based * upon the selection mode that is passed in. See below for details. * - number of device type matching expressions (num_matches) * - Set to 0 to disable the matching code. * - list of devices specified on the command line by the user (dev_selections) * - number of devices selected on the command line by the user * (num_dev_selections) * - Our selection mode. There are four different selection modes: * - add mode. (DS_SELECT_ADD) Any devices matching devices explicitly * selected by the user or devices matching a pattern given by the * user will be selected in addition to devices that are already * selected. Additional devices will be selected, up to maxshowdevs * number of devices. * - only mode. (DS_SELECT_ONLY) Only devices matching devices * explicitly given by the user or devices matching a pattern * given by the user will be selected. No other devices will be * selected. * - addonly mode. (DS_SELECT_ADDONLY) This is similar to add and * only. Basically, this will not de-select any devices that are * current selected, as only mode would, but it will also not * gratuitously select up to maxshowdevs devices as add mode would. * - remove mode. (DS_SELECT_REMOVE) Any devices matching devices * explicitly selected by the user or devices matching a pattern * given by the user will be de-selected. * - maximum number of devices we can select (maxshowdevs) * - flag indicating whether or not we're in 'top' mode (perf_select) * * Output data: * - the device selection list may be modified and passed back out * - the number of devices selected and the total number of items in the * device selection list may be changed * - the selection generation may be changed to match the current generation * * Return values: * -1 -- error * 0 -- selected devices are unchanged * 1 -- selected devices changed */ int devstat_selectdevs(struct device_selection **dev_select, int *num_selected, int *num_selections, long *select_generation, long current_generation, struct devstat *devices, int numdevs, struct devstat_match *matches, int num_matches, char **dev_selections, int num_dev_selections, devstat_select_mode select_mode, int maxshowdevs, int perf_select) { int i, j, k; int init_selections = 0, init_selected_var = 0; struct device_selection *old_dev_select = NULL; int old_num_selections = 0, old_num_selected; int selection_number = 0; int changed = 0, found = 0; if ((dev_select == NULL) || (devices == NULL) || (numdevs < 0)) return(-1); /* * We always want to make sure that we have as many dev_select * entries as there are devices. */ /* * In this case, we haven't selected devices before. */ if (*dev_select == NULL) { *dev_select = (struct device_selection *)malloc(numdevs * sizeof(struct device_selection)); *select_generation = current_generation; init_selections = 1; changed = 1; /* * In this case, we have selected devices before, but the device * list has changed since we last selected devices, so we need to * either enlarge or reduce the size of the device selection list. * But delay the resizing until after copying the data to old_dev_select * as to not lose any data in the case of reducing the size. */ } else if (*num_selections != numdevs) { *select_generation = current_generation; init_selections = 1; /* * In this case, we've selected devices before, and the selection * list is the same size as it was the last time, but the device * list has changed. */ } else if (*select_generation < current_generation) { *select_generation = current_generation; 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 * this time through. */ if (select_mode == DS_SELECT_ONLY) init_selected_var = 1; /* * In all cases, we want to back up the number of selected devices. * It is a quick and accurate way to determine whether the selected * devices have changed. */ old_num_selected = *num_selected; /* * We want to make a backup of the current selection list if * the list of devices has changed, or if we're in performance * selection mode. In both cases, we don't want to make a backup * if we already know for sure that the list will be different. * This is certainly the case if this is our first time through the * selection code. */ if (((init_selected_var != 0) || (init_selections != 0) || (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); } if (!changed && *num_selections != numdevs) { *dev_select = (struct device_selection *)reallocf(*dev_select, numdevs * sizeof(struct device_selection)); } if (init_selections != 0) { bzero(*dev_select, sizeof(struct device_selection) * numdevs); for (i = 0; i < numdevs; i++) { (*dev_select)[i].device_number = devices[i].device_number; strncpy((*dev_select)[i].device_name, devices[i].device_name, DEVSTAT_NAME_LEN); (*dev_select)[i].device_name[DEVSTAT_NAME_LEN - 1]='\0'; (*dev_select)[i].unit_number = devices[i].unit_number; (*dev_select)[i].position = i; } *num_selections = numdevs; } else if (init_selected_var != 0) { for (i = 0; i < numdevs; i++) (*dev_select)[i].selected = 0; } /* we haven't gotten around to selecting anything yet.. */ if ((select_mode == DS_SELECT_ONLY) || (init_selections != 0) || (init_selected_var != 0)) *num_selected = 0; /* * Look through any devices the user specified on the command line * and see if they match known devices. If so, select them. */ for (i = 0; (i < *num_selections) && (num_dev_selections > 0); i++) { char tmpstr[80]; snprintf(tmpstr, sizeof(tmpstr), "%s%d", (*dev_select)[i].device_name, (*dev_select)[i].unit_number); for (j = 0; j < num_dev_selections; j++) { if (strcmp(tmpstr, dev_selections[j]) == 0) { /* * Here we do different things based on the * mode we're in. If we're in add or * addonly mode, we only select this device * if it hasn't already been selected. * Otherwise, we would be unnecessarily * changing the selection order and * incrementing the selection count. If * we're in only mode, we unconditionally * select this device, since in only mode * any previous selections are erased and * manually specified devices are the first * ones to be selected. If we're in remove * mode, we de-select the specified device and * decrement the selection count. */ switch(select_mode) { case DS_SELECT_ADD: case DS_SELECT_ADDONLY: if ((*dev_select)[i].selected) break; /* FALLTHROUGH */ case DS_SELECT_ONLY: (*dev_select)[i].selected = ++selection_number; (*num_selected)++; break; case DS_SELECT_REMOVE: (*dev_select)[i].selected = 0; (*num_selected)--; /* * This isn't passed back out, we * just use it to keep track of * how many devices we've removed. */ num_dev_selections--; break; } break; } } } /* * Go through the user's device type expressions and select devices * accordingly. We only do this if the number of devices already * selected is less than the maximum number we can show. */ for (i = 0; (i < num_matches) && (*num_selected < maxshowdevs); i++) { /* We should probably indicate some error here */ if ((matches[i].match_fields == DEVSTAT_MATCH_NONE) || (matches[i].num_match_categories <= 0)) continue; for (j = 0; j < numdevs; j++) { int num_match_categories; num_match_categories = matches[i].num_match_categories; /* * Determine whether or not the current device * matches the given matching expression. This if * statement consists of three components: * - the device type check * - the device interface check * - the passthrough check * If a the matching test is successful, it * decrements the number of matching categories, * and if we've reached the last element that * needed to be matched, the if statement succeeds. * */ if ((((matches[i].match_fields & DEVSTAT_MATCH_TYPE)!=0) && ((devices[j].device_type & DEVSTAT_TYPE_MASK) == (matches[i].device_type & DEVSTAT_TYPE_MASK)) &&(((matches[i].match_fields & DEVSTAT_MATCH_PASS)!=0) || (((matches[i].match_fields & DEVSTAT_MATCH_PASS) == 0) && ((devices[j].device_type & DEVSTAT_TYPE_PASS) == 0))) && (--num_match_categories == 0)) || (((matches[i].match_fields & DEVSTAT_MATCH_IF) != 0) && ((devices[j].device_type & DEVSTAT_TYPE_IF_MASK) == (matches[i].device_type & DEVSTAT_TYPE_IF_MASK)) &&(((matches[i].match_fields & DEVSTAT_MATCH_PASS)!=0) || (((matches[i].match_fields & DEVSTAT_MATCH_PASS) == 0) && ((devices[j].device_type & DEVSTAT_TYPE_PASS) == 0))) && (--num_match_categories == 0)) || (((matches[i].match_fields & DEVSTAT_MATCH_PASS)!=0) && ((devices[j].device_type & DEVSTAT_TYPE_PASS) != 0) && (--num_match_categories == 0))) { /* * This is probably a non-optimal solution * to the problem that the devices in the * device list will not be in the same * order as the devices in the selection * array. */ for (k = 0; k < numdevs; k++) { if ((*dev_select)[k].position == j) { found = 1; break; } } /* * There shouldn't be a case where a device * in the device list is not in the * selection list...but it could happen. */ if (found != 1) { fprintf(stderr, "selectdevs: couldn't" " find %s%d in selection " "list\n", devices[j].device_name, devices[j].unit_number); break; } /* * We do different things based upon the * mode we're in. If we're in add or only * mode, we go ahead and select this device * if it hasn't already been selected. If * it has already been selected, we leave * it alone so we don't mess up the * selection ordering. Manually specified * devices have already been selected, and * they have higher priority than pattern * matched devices. If we're in remove * mode, we de-select the given device and * decrement the selected count. */ switch(select_mode) { case DS_SELECT_ADD: case DS_SELECT_ADDONLY: case DS_SELECT_ONLY: if ((*dev_select)[k].selected != 0) break; (*dev_select)[k].selected = ++selection_number; (*num_selected)++; break; case DS_SELECT_REMOVE: (*dev_select)[k].selected = 0; (*num_selected)--; break; } } } } /* * Here we implement "top" mode. Devices are sorted in the * selection array based on two criteria: whether or not they are * selected (not selection number, just the fact that they are * selected!) and the number of bytes in the "bytes" field of the * selection structure. The bytes field generally must be kept up * by the user. In the future, it may be maintained by library * functions, but for now the user has to do the work. * * At first glance, it may seem wrong that we don't go through and * select every device in the case where the user hasn't specified * any devices or patterns. In fact, though, it won't make any * difference in the device sorting. In that particular case (i.e. * when we're in "add" or "only" mode, and the user hasn't * specified anything) the first time through no devices will be * selected, so the only criterion used to sort them will be their * performance. The second time through, and every time thereafter, * all devices will be selected, so again selection won't matter. */ if (perf_select != 0) { /* Sort the device array by throughput */ qsort(*dev_select, *num_selections, sizeof(struct device_selection), compare_select); if (*num_selected == 0) { /* * Here we select every device in the array, if it * isn't already selected. Because the 'selected' * variable in the selection array entries contains * the selection order, the devstats routine can show * the devices that were selected first. */ for (i = 0; i < *num_selections; i++) { if ((*dev_select)[i].selected == 0) { (*dev_select)[i].selected = ++selection_number; (*num_selected)++; } } } else { selection_number = 0; for (i = 0; i < *num_selections; i++) { if ((*dev_select)[i].selected != 0) { (*dev_select)[i].selected = ++selection_number; } } } } /* * If we're in the "add" selection mode and if we haven't already * selected maxshowdevs number of devices, go through the array and * select any unselected devices. If we're in "only" mode, we * obviously don't want to select anything other than what the user * specifies. If we're in "remove" mode, it probably isn't a good * idea to go through and select any more devices, since we might * end up selecting something that the user wants removed. Through * more complicated logic, we could actually figure this out, but * that would probably require combining this loop with the various * selections loops above. */ if ((select_mode == DS_SELECT_ADD) && (*num_selected < maxshowdevs)) { for (i = 0; i < *num_selections; i++) if ((*dev_select)[i].selected == 0) { (*dev_select)[i].selected = ++selection_number; (*num_selected)++; } } /* * Look at the number of devices that have been selected. If it * has changed, set the changed variable. Otherwise, if we've * made a backup of the selection list, compare it to the current * selection list to see if the selected devices have changed. */ if ((changed == 0) && (old_num_selected != *num_selected)) changed = 1; else if ((changed == 0) && (old_dev_select != NULL)) { /* * Now we go through the selection list and we look at * it three different ways. */ for (i = 0; (i < *num_selections) && (changed == 0) && (i < old_num_selections); i++) { /* * If the device at index i in both the new and old * selection arrays has the same device number and * selection status, it hasn't changed. We * continue on to the next index. */ if (((*dev_select)[i].device_number == old_dev_select[i].device_number) && ((*dev_select)[i].selected == old_dev_select[i].selected)) continue; /* * Now, if we're still going through the if * statement, the above test wasn't true. So we * check here to see if the device at index i in * the current array is the same as the device at * index i in the old array. If it is, that means * that its selection number has changed. Set * changed to 1 and exit the loop. */ else if ((*dev_select)[i].device_number == old_dev_select[i].device_number) { changed = 1; break; } /* * If we get here, then the device at index i in * the current array isn't the same device as the * device at index i in the old array. */ else { found = 0; /* * Search through the old selection array * looking for a device with the same * device number as the device at index i * in the current array. If the selection * status is the same, then we mark it as * found. If the selection status isn't * the same, we break out of the loop. * Since found isn't set, changed will be * set to 1 below. */ for (j = 0; j < old_num_selections; j++) { if (((*dev_select)[i].device_number == old_dev_select[j].device_number) && ((*dev_select)[i].selected == old_dev_select[j].selected)){ found = 1; break; } else if ((*dev_select)[i].device_number == old_dev_select[j].device_number) break; } if (found == 0) changed = 1; } } } if (old_dev_select != NULL) free(old_dev_select); return(changed); } /* * Comparison routine for qsort() above. Note that the comparison here is * backwards -- generally, it should return a value to indicate whether * arg1 is <, =, or > arg2. Instead, it returns the opposite. The reason * it returns the opposite is so that the selection array will be sorted in * order of decreasing performance. We sort on two parameters. The first * sort key is whether or not one or the other of the devices in question * has been selected. If one of them has, and the other one has not, the * selected device is automatically more important than the unselected * device. If neither device is selected, we judge the devices based upon * performance. */ static int compare_select(const void *arg1, const void *arg2) { if ((((const struct device_selection *)arg1)->selected) && (((const struct device_selection *)arg2)->selected == 0)) return(-1); else if ((((const struct device_selection *)arg1)->selected == 0) && (((const struct device_selection *)arg2)->selected)) return(1); else if (((const struct device_selection *)arg2)->bytes < ((const struct device_selection *)arg1)->bytes) return(-1); else if (((const struct device_selection *)arg2)->bytes > ((const struct device_selection *)arg1)->bytes) return(1); else return(0); } /* * Take a string with the general format "arg1,arg2,arg3", and build a * device matching expression from it. */ int devstat_buildmatch(char *match_str, struct devstat_match **matches, int *num_matches) { char *tstr[5]; char **tempstr; int num_args; int i, j; /* We can't do much without a string to parse */ if (match_str == NULL) { snprintf(devstat_errbuf, sizeof(devstat_errbuf), "%s: no match expression", __func__); return(-1); } /* * Break the (comma delimited) input string out into separate strings. */ for (tempstr = tstr, num_args = 0; (*tempstr = strsep(&match_str, ",")) != NULL && (num_args < 5);) if (**tempstr != '\0') { num_args++; if (++tempstr >= &tstr[5]) break; } /* The user gave us too many type arguments */ if (num_args > 3) { snprintf(devstat_errbuf, sizeof(devstat_errbuf), "%s: too many type arguments", __func__); return(-1); } if (*num_matches == 0) *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)); /* * Step through the arguments the user gave us and build a device * matching expression from them. */ for (i = 0; i < num_args; i++) { char *tempstr2, *tempstr3; /* * Get rid of leading white space. */ tempstr2 = tstr[i]; while (isspace(*tempstr2) && (*tempstr2 != '\0')) tempstr2++; /* * Get rid of trailing white space. */ tempstr3 = &tempstr2[strlen(tempstr2) - 1]; while ((*tempstr3 != '\0') && (tempstr3 > tempstr2) && (isspace(*tempstr3))) { *tempstr3 = '\0'; tempstr3--; } /* * Go through the match table comparing the user's * arguments to known device types, interfaces, etc. */ for (j = 0; match_table[j].match_str != NULL; j++) { /* * We do case-insensitive matching, in case someone * wants to enter "SCSI" instead of "scsi" or * something like that. Only compare as many * characters as are in the string in the match * table. This should help if someone tries to use * a super-long match expression. */ if (strncasecmp(tempstr2, match_table[j].match_str, strlen(match_table[j].match_str)) == 0) { /* * Make sure the user hasn't specified two * items of the same type, like "da" and * "cd". One device cannot be both. */ if (((*matches)[*num_matches].match_fields & match_table[j].match_field) != 0) { snprintf(devstat_errbuf, sizeof(devstat_errbuf), "%s: cannot have more than " "one match item in a single " "category", __func__); return(-1); } /* * If we've gotten this far, we have a * winner. Set the appropriate fields in * the match entry. */ (*matches)[*num_matches].match_fields |= match_table[j].match_field; (*matches)[*num_matches].device_type |= match_table[j].type; (*matches)[*num_matches].num_match_categories++; break; } } /* * We should have found a match in the above for loop. If * not, that means the user entered an invalid device type * or interface. */ if ((*matches)[*num_matches].num_match_categories != (i + 1)) { snprintf(devstat_errbuf, sizeof(devstat_errbuf), "%s: unknown match item \"%s\"", __func__, tstr[i]); return(-1); } } (*num_matches)++; return(0); } /* * Compute a number of device statistics. Only one field is mandatory, and * that is "current". Everything else is optional. The caller passes in * pointers to variables to hold the various statistics he desires. If he * doesn't want a particular staistic, he should pass in a NULL pointer. * Return values: * 0 -- success * -1 -- failure */ int compute_stats(struct devstat *current, struct devstat *previous, long double etime, u_int64_t *total_bytes, u_int64_t *total_transfers, u_int64_t *total_blocks, long double *kb_per_transfer, long double *transfers_per_second, long double *mb_per_second, long double *blocks_per_second, long double *ms_per_transaction) { return(devstat_compute_statistics(current, previous, etime, total_bytes ? DSM_TOTAL_BYTES : DSM_SKIP, total_bytes, total_transfers ? DSM_TOTAL_TRANSFERS : DSM_SKIP, total_transfers, total_blocks ? DSM_TOTAL_BLOCKS : DSM_SKIP, total_blocks, kb_per_transfer ? DSM_KB_PER_TRANSFER : DSM_SKIP, kb_per_transfer, transfers_per_second ? DSM_TRANSFERS_PER_SECOND : DSM_SKIP, transfers_per_second, mb_per_second ? DSM_MB_PER_SECOND : DSM_SKIP, mb_per_second, blocks_per_second ? DSM_BLOCKS_PER_SECOND : DSM_SKIP, blocks_per_second, ms_per_transaction ? DSM_MS_PER_TRANSACTION : DSM_SKIP, ms_per_transaction, DSM_NONE)); } /* This is 1/2^64 */ #define BINTIME_SCALE 5.42101086242752217003726400434970855712890625e-20 long double devstat_compute_etime(struct bintime *cur_time, struct bintime *prev_time) { long double etime; etime = cur_time->sec; etime += cur_time->frac * BINTIME_SCALE; if (prev_time != NULL) { etime -= prev_time->sec; etime -= prev_time->frac * BINTIME_SCALE; } return(etime); } #define DELTA(field, index) \ (current->field[(index)] - (previous ? previous->field[(index)] : 0)) #define DELTA_T(field) \ devstat_compute_etime(¤t->field, \ (previous ? &previous->field : NULL)) int devstat_compute_statistics(struct devstat *current, struct devstat *previous, long double etime, ...) { u_int64_t totalbytes, totalbytesread, totalbyteswrite, totalbytesfree; u_int64_t totaltransfers, totaltransfersread, totaltransferswrite; u_int64_t totaltransfersother, totalblocks, totalblocksread; u_int64_t totalblockswrite, totaltransfersfree, totalblocksfree; long double totalduration, totaldurationread, totaldurationwrite; long double totaldurationfree, totaldurationother; va_list ap; devstat_metric metric; u_int64_t *destu64; long double *destld; int retval; retval = 0; /* * current is the only mandatory field. */ if (current == NULL) { snprintf(devstat_errbuf, sizeof(devstat_errbuf), "%s: current stats structure was NULL", __func__); return(-1); } totalbytesread = DELTA(bytes, DEVSTAT_READ); totalbyteswrite = DELTA(bytes, DEVSTAT_WRITE); totalbytesfree = DELTA(bytes, DEVSTAT_FREE); totalbytes = totalbytesread + totalbyteswrite + totalbytesfree; totaltransfersread = DELTA(operations, DEVSTAT_READ); totaltransferswrite = DELTA(operations, DEVSTAT_WRITE); totaltransfersother = DELTA(operations, DEVSTAT_NO_DATA); totaltransfersfree = DELTA(operations, DEVSTAT_FREE); totaltransfers = totaltransfersread + totaltransferswrite + totaltransfersother + totaltransfersfree; totalblocks = totalbytes; totalblocksread = totalbytesread; totalblockswrite = totalbyteswrite; totalblocksfree = totalbytesfree; if (current->block_size > 0) { totalblocks /= current->block_size; totalblocksread /= current->block_size; totalblockswrite /= current->block_size; totalblocksfree /= current->block_size; } else { totalblocks /= 512; totalblocksread /= 512; totalblockswrite /= 512; totalblocksfree /= 512; } totaldurationread = DELTA_T(duration[DEVSTAT_READ]); totaldurationwrite = DELTA_T(duration[DEVSTAT_WRITE]); totaldurationfree = DELTA_T(duration[DEVSTAT_FREE]); totaldurationother = DELTA_T(duration[DEVSTAT_NO_DATA]); totalduration = totaldurationread + totaldurationwrite + totaldurationfree + totaldurationother; va_start(ap, etime); while ((metric = (devstat_metric)va_arg(ap, devstat_metric)) != 0) { if (metric == DSM_NONE) break; if (metric >= DSM_MAX) { snprintf(devstat_errbuf, sizeof(devstat_errbuf), "%s: metric %d is out of range", __func__, metric); retval = -1; goto bailout; } switch (devstat_arg_list[metric].argtype) { case DEVSTAT_ARG_UINT64: destu64 = (u_int64_t *)va_arg(ap, u_int64_t *); break; case DEVSTAT_ARG_LD: destld = (long double *)va_arg(ap, long double *); break; case DEVSTAT_ARG_SKIP: destld = (long double *)va_arg(ap, long double *); break; default: retval = -1; goto bailout; break; /* NOTREACHED */ } if (devstat_arg_list[metric].argtype == DEVSTAT_ARG_SKIP) continue; switch (metric) { case DSM_TOTAL_BYTES: *destu64 = totalbytes; break; case DSM_TOTAL_BYTES_READ: *destu64 = totalbytesread; break; case DSM_TOTAL_BYTES_WRITE: *destu64 = totalbyteswrite; break; case DSM_TOTAL_BYTES_FREE: *destu64 = totalbytesfree; break; case DSM_TOTAL_TRANSFERS: *destu64 = totaltransfers; break; case DSM_TOTAL_TRANSFERS_READ: *destu64 = totaltransfersread; break; case DSM_TOTAL_TRANSFERS_WRITE: *destu64 = totaltransferswrite; break; case DSM_TOTAL_TRANSFERS_FREE: *destu64 = totaltransfersfree; break; case DSM_TOTAL_TRANSFERS_OTHER: *destu64 = totaltransfersother; break; case DSM_TOTAL_BLOCKS: *destu64 = totalblocks; break; case DSM_TOTAL_BLOCKS_READ: *destu64 = totalblocksread; break; case DSM_TOTAL_BLOCKS_WRITE: *destu64 = totalblockswrite; break; case DSM_TOTAL_BLOCKS_FREE: *destu64 = totalblocksfree; break; case DSM_KB_PER_TRANSFER: *destld = totalbytes; *destld /= 1024; if (totaltransfers > 0) *destld /= totaltransfers; else *destld = 0.0; break; case DSM_KB_PER_TRANSFER_READ: *destld = totalbytesread; *destld /= 1024; if (totaltransfersread > 0) *destld /= totaltransfersread; else *destld = 0.0; break; case DSM_KB_PER_TRANSFER_WRITE: *destld = totalbyteswrite; *destld /= 1024; if (totaltransferswrite > 0) *destld /= totaltransferswrite; else *destld = 0.0; break; case DSM_KB_PER_TRANSFER_FREE: *destld = totalbytesfree; *destld /= 1024; if (totaltransfersfree > 0) *destld /= totaltransfersfree; else *destld = 0.0; break; case DSM_TRANSFERS_PER_SECOND: if (etime > 0.0) { *destld = totaltransfers; *destld /= etime; } else *destld = 0.0; break; case DSM_TRANSFERS_PER_SECOND_READ: if (etime > 0.0) { *destld = totaltransfersread; *destld /= etime; } else *destld = 0.0; break; case DSM_TRANSFERS_PER_SECOND_WRITE: if (etime > 0.0) { *destld = totaltransferswrite; *destld /= etime; } else *destld = 0.0; break; case DSM_TRANSFERS_PER_SECOND_FREE: if (etime > 0.0) { *destld = totaltransfersfree; *destld /= etime; } else *destld = 0.0; break; case DSM_TRANSFERS_PER_SECOND_OTHER: if (etime > 0.0) { *destld = totaltransfersother; *destld /= etime; } else *destld = 0.0; break; case DSM_MB_PER_SECOND: *destld = totalbytes; *destld /= 1024 * 1024; if (etime > 0.0) *destld /= etime; else *destld = 0.0; break; case DSM_MB_PER_SECOND_READ: *destld = totalbytesread; *destld /= 1024 * 1024; if (etime > 0.0) *destld /= etime; else *destld = 0.0; break; case DSM_MB_PER_SECOND_WRITE: *destld = totalbyteswrite; *destld /= 1024 * 1024; if (etime > 0.0) *destld /= etime; else *destld = 0.0; break; case DSM_MB_PER_SECOND_FREE: *destld = totalbytesfree; *destld /= 1024 * 1024; if (etime > 0.0) *destld /= etime; else *destld = 0.0; break; case DSM_BLOCKS_PER_SECOND: *destld = totalblocks; if (etime > 0.0) *destld /= etime; else *destld = 0.0; break; case DSM_BLOCKS_PER_SECOND_READ: *destld = totalblocksread; if (etime > 0.0) *destld /= etime; else *destld = 0.0; break; case DSM_BLOCKS_PER_SECOND_WRITE: *destld = totalblockswrite; if (etime > 0.0) *destld /= etime; else *destld = 0.0; break; case DSM_BLOCKS_PER_SECOND_FREE: *destld = totalblocksfree; if (etime > 0.0) *destld /= etime; else *destld = 0.0; break; /* * Some devstat callers update the duration and some don't. * So this will only be accurate if they provide the * duration. */ case DSM_MS_PER_TRANSACTION: if (totaltransfers > 0) { *destld = totalduration; *destld /= totaltransfers; *destld *= 1000; } else *destld = 0.0; break; case DSM_MS_PER_TRANSACTION_READ: if (totaltransfersread > 0) { *destld = totaldurationread; *destld /= totaltransfersread; *destld *= 1000; } else *destld = 0.0; break; case DSM_MS_PER_TRANSACTION_WRITE: if (totaltransferswrite > 0) { *destld = totaldurationwrite; *destld /= totaltransferswrite; *destld *= 1000; } else *destld = 0.0; break; case DSM_MS_PER_TRANSACTION_FREE: if (totaltransfersfree > 0) { *destld = totaldurationfree; *destld /= totaltransfersfree; *destld *= 1000; } else *destld = 0.0; break; case DSM_MS_PER_TRANSACTION_OTHER: if (totaltransfersother > 0) { *destld = totaldurationother; *destld /= totaltransfersother; *destld *= 1000; } else *destld = 0.0; break; case DSM_BUSY_PCT: *destld = DELTA_T(busy_time); if (*destld < 0) *destld = 0; *destld /= etime; *destld *= 100; if (*destld < 0) *destld = 0; break; case DSM_QUEUE_LENGTH: *destu64 = current->start_count - current->end_count; break; case DSM_TOTAL_DURATION: *destld = totalduration; break; case DSM_TOTAL_DURATION_READ: *destld = totaldurationread; break; case DSM_TOTAL_DURATION_WRITE: *destld = totaldurationwrite; break; case DSM_TOTAL_DURATION_FREE: *destld = totaldurationfree; break; case DSM_TOTAL_DURATION_OTHER: *destld = totaldurationother; break; case DSM_TOTAL_BUSY_TIME: *destld = DELTA_T(busy_time); break; /* * XXX: comment out the default block to see if any case's are missing. */ #if 1 default: /* * This shouldn't happen, since we should have * caught any out of range metrics at the top of * the loop. */ snprintf(devstat_errbuf, sizeof(devstat_errbuf), "%s: unknown metric %d", __func__, metric); retval = -1; goto bailout; break; /* NOTREACHED */ #endif } } bailout: va_end(ap); return(retval); } static int readkmem(kvm_t *kd, unsigned long addr, void *buf, size_t nbytes) { if (kvm_read(kd, addr, buf, nbytes) == -1) { snprintf(devstat_errbuf, sizeof(devstat_errbuf), "%s: error reading value (kvm_read): %s", __func__, kvm_geterr(kd)); return(-1); } return(0); } static int readkmem_nl(kvm_t *kd, const char *name, void *buf, size_t nbytes) { struct nlist nl[2]; nl[0].n_name = (char *)name; nl[1].n_name = NULL; if (kvm_nlist(kd, nl) == -1) { snprintf(devstat_errbuf, sizeof(devstat_errbuf), "%s: error getting name list (kvm_nlist): %s", __func__, kvm_geterr(kd)); return(-1); } return(readkmem(kd, nl[0].n_value, buf, nbytes)); } /* * This duplicates the functionality of the kernel sysctl handler for poking * through crash dumps. */ static char * get_devstat_kvm(kvm_t *kd) { int i, wp; long gen; struct devstat *nds; struct devstat ds; struct devstatlist dhead; int num_devs; char *rv = NULL; if ((num_devs = devstat_getnumdevs(kd)) <= 0) return(NULL); if (KREADNL(kd, X_DEVICE_STATQ, dhead) == -1) return(NULL); nds = STAILQ_FIRST(&dhead); if ((rv = malloc(sizeof(gen))) == NULL) { snprintf(devstat_errbuf, sizeof(devstat_errbuf), "%s: out of memory (initial malloc failed)", __func__); return(NULL); } gen = devstat_getgeneration(kd); memcpy(rv, &gen, sizeof(gen)); wp = sizeof(gen); /* * Now push out all the devices. */ for (i = 0; (nds != NULL) && (i < num_devs); nds = STAILQ_NEXT(nds, dev_links), i++) { if (readkmem(kd, (long)nds, &ds, sizeof(ds)) == -1) { free(rv); return(NULL); } nds = &ds; rv = (char *)reallocf(rv, sizeof(gen) + sizeof(ds) * (i + 1)); if (rv == NULL) { snprintf(devstat_errbuf, sizeof(devstat_errbuf), "%s: out of memory (malloc failed)", __func__); return(NULL); } memcpy(rv + wp, &ds, sizeof(ds)); wp += sizeof(ds); } return(rv); } diff --git a/share/man/man9/devstat.9 b/share/man/man9/devstat.9 index e474e3119418..3682ad024eae 100644 --- a/share/man/man9/devstat.9 +++ b/share/man/man9/devstat.9 @@ -1,547 +1,548 @@ .\" .\" Copyright (c) 1998, 1999 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. .\" .Dd July 15, 2020 .Dt DEVSTAT 9 .Os .Sh NAME .Nm devstat , .Nm devstat_end_transaction , .Nm devstat_end_transaction_bio , .Nm devstat_end_transaction_bio_bt , .Nm devstat_new_entry , .Nm devstat_remove_entry , .Nm devstat_start_transaction , .Nm devstat_start_transaction_bio .Nd kernel interface for keeping device statistics .Sh SYNOPSIS .In sys/devicestat.h .Ft struct devstat * .Fo devstat_new_entry .Fa "const void *dev_name" .Fa "int unit_number" .Fa "uint32_t block_size" .Fa "devstat_support_flags flags" .Fa "devstat_type_flags device_type" .Fa "devstat_priority priority" .Fc .Ft void .Fn devstat_remove_entry "struct devstat *ds" .Ft void .Fo devstat_start_transaction .Fa "struct devstat *ds" .Fa "const struct bintime *now" .Fc .Ft void .Fo devstat_start_transaction_bio .Fa "struct devstat *ds" .Fa "struct bio *bp" .Fc .Ft void .Fo devstat_end_transaction .Fa "struct devstat *ds" .Fa "uint32_t bytes" .Fa "devstat_tag_type tag_type" .Fa "devstat_trans_flags flags" .Fa "const struct bintime *now" .Fa "const struct bintime *then" .Fc .Ft void .Fo devstat_end_transaction_bio .Fa "struct devstat *ds" .Fa "const struct bio *bp" .Fc .Ft void .Fo devstat_end_transaction_bio_bt .Fa "struct devstat *ds" .Fa "const struct bio *bp" .Fa "const struct bintime *now" .Fc .Sh DESCRIPTION The devstat subsystem is an interface for recording device statistics, as its name implies. The idea is to keep reasonably detailed statistics while utilizing a minimum amount of CPU time to record them. Thus, no statistical calculations are actually performed in the kernel portion of the .Nm code. Instead, that is left for user programs to handle. .Pp The historical and antiquated .Nm model assumed a single active IO operation per device, which is not accurate for most disk-like drivers in the 2000s and beyond. New consumers of the interface should almost certainly use only the "bio" variants of the start and end transacation routines. .Pp .Fn devstat_new_entry allocates and initializes .Va devstat structure and returns a pointer to it. .Fn devstat_new_entry takes several arguments: .Bl -tag -width device_type .It dev_name The device name, e.g., da, cd, sa. .It unit_number Device unit number. .It block_size Block size of the device, if supported. If the device does not support a block size, or if the blocksize is unknown at the time the device is added to the .Nm list, it should be set to 0. .It flags Flags indicating operations supported or not supported by the device. See below for details. .It device_type The device type. This is broken into three sections: base device type (e.g., direct access, CDROM, sequential access), interface type (IDE, SCSI or other) and a pass-through flag to indicate pas-through devices. See below for a complete list of types. .It priority The device priority. The priority is used to determine how devices are sorted within .Nm devstat Ns 's list of devices. Devices are sorted first by priority (highest to lowest), and then by attach order. See below for a complete list of available priorities. .El .Pp .Fn devstat_remove_entry removes a device from the .Nm subsystem. It takes the devstat structure for the device in question as an argument. The .Nm generation number is incremented and the number of devices is decremented. .Pp .Fn devstat_start_transaction registers the start of a transaction with the .Nm subsystem. Optionally, if the caller already has a .Fn binuptime value available, it may be passed in .Fa *now . Usually the caller can just pass .Dv NULL for .Fa now , and the routine will gather the current .Fn binuptime itself. The busy count is incremented with each transaction start. When a device goes from idle to busy, the system uptime is recorded in the .Va busy_from field of the .Va devstat structure. .Pp .Fn devstat_start_transaction_bio records the .Fn binuptime in the provided bio's .Fa bio_t0 and then invokes .Fn devstat_start_transaction . .Pp .Fn devstat_end_transaction registers the end of a transaction with the .Nm subsystem. It takes six arguments: .Bl -tag -width tag_type .It ds The .Va devstat structure for the device in question. .It bytes The number of bytes transferred in this transaction. .It tag_type Transaction tag type. See below for tag types. .It flags Transaction flags indicating whether the transaction was a read, write, or whether no data was transferred. .It now The .Fn binuptime at the end of the transaction, or .Dv NULL . .It then The .Fn binuptime at the beginning of the transaction, or .Dv NULL . .El .Pp If .Fa now is .Dv NULL , it collects the current time from .Fn binuptime . If .Fa then is .Dv NULL , the operation is not tracked in the .Va devstat .Fa duration table. .Pp .Fn devstat_end_transaction_bio is a thin wrapper for .Fn devstat_end_transaction_bio_bt with a .Dv NULL .Fa now parameter. .Pp .Fn devstat_end_transaction_bio_bt is a wrapper for .Fn devstat_end_transaction which pulls all needed information from a .Va "struct bio" prepared by .Fn devstat_start_transaction_bio . The bio must be ready for .Fn biodone (i.e., .Fa bio_bcount and .Fa bio_resid must be correctly initialized). .Pp The .Va devstat structure is composed of the following fields: .Bl -tag -width dev_creation_time .It sequence0 , .It sequence1 An implementation detail used to gather consistent snapshots of device statistics. .It start_count Number of operations started. .It end_count Number of operations completed. The .Dq busy_count can be calculated by subtracting .Fa end_count from .Fa start_count . .Fa ( sequence0 and .Fa sequence1 are used to get a consistent snapshot.) This is the current number of outstanding transactions for the device. This should never go below zero, and on an idle device it should be zero. If either one of these conditions is not true, it indicates a problem. .Pp There should be one and only one transaction start event and one transaction end event for each transaction. .It dev_links Each .Va devstat structure is placed in a linked list when it is registered. The .Va dev_links field contains a pointer to the next entry in the list of .Va devstat structures. .It device_number The device number is a unique identifier for each device. The device number is incremented for each new device that is registered. The device number is currently only a 32-bit integer, but it could be enlarged if someone has a system with more than four billion device arrival events. .It device_name The device name is a text string given by the registering driver to identify itself. (e.g., .Dq da , .Dq cd , .Dq sa , etc.) .It unit_number The unit number identifies the particular instance of the peripheral driver in question. .It bytes[4] This array contains the number of bytes that have been read (index .Dv DEVSTAT_READ ) , written (index .Dv DEVSTAT_WRITE ) , freed or erased (index .Dv DEVSTAT_FREE ) , or other (index .Dv DEVSTAT_NO_DATA ) . All values are unsigned 64-bit integers. .It operations[4] This array contains the number of operations of a given type that have been performed. The indices are identical to those for .Fa bytes above. .Dv DEVSTAT_NO_DATA or "other" represents the number of transactions to the device which are neither reads, writes, nor frees. For instance, .Tn SCSI drivers often send a test unit ready command to .Tn SCSI devices. The test unit ready command does not read or write any data. It merely causes the device to return its status. .It duration[4] This array contains the total bintime corresponding to completed operations of a given type. The indices are identical to those for .Fa bytes above. (Operations that complete using the historical .Fn devstat_end_transaction API and do not provide a non-NULL .Fa then are not accounted for.) .It busy_time This is the amount of time that the device busy count has been greater than zero. This is only updated when the busy count returns to zero. .It creation_time This is the time, as reported by .Fn getmicrotime that the device was registered. .It block_size This is the block size of the device, if the device has a block size. .It tag_types This is an array of counters to record the number of various tag types that are sent to a device. See below for a list of tag types. .It busy_from If the device is not busy, this was the time that a transaction last completed. If the device is busy, this the most recent of either the time that the device became busy, or the time that the last transaction completed. .It flags These flags indicate which statistics measurements are supported by a particular device. These flags are primarily intended to serve as an aid to userland programs that decipher the statistics. .It device_type This is the device type. It consists of three parts: the device type (e.g., direct access, CDROM, sequential access, etc.), the interface (IDE, SCSI or other) and whether or not the device in question is a pass-through driver. See below for a complete list of device types. .It priority This is the priority. This is the first parameter used to determine where to insert a device in the .Nm list. The second parameter is attach order. See below for a list of available priorities. .It id Identification for GEOM nodes. .El .Pp Each device is given a device type. Pass-through devices have the same underlying device type and interface as the device they provide an interface for, but they also have the pass-through flag set. The base device types are identical to the .Tn SCSI device type numbers, so with .Tn SCSI peripherals, the device type returned from an inquiry is usually ORed with the .Tn SCSI interface type and the pass-through flag if appropriate. The device type flags are as follows: .Bd -literal -offset indent typedef enum { DEVSTAT_TYPE_DIRECT = 0x000, DEVSTAT_TYPE_SEQUENTIAL = 0x001, DEVSTAT_TYPE_PRINTER = 0x002, DEVSTAT_TYPE_PROCESSOR = 0x003, DEVSTAT_TYPE_WORM = 0x004, DEVSTAT_TYPE_CDROM = 0x005, DEVSTAT_TYPE_SCANNER = 0x006, DEVSTAT_TYPE_OPTICAL = 0x007, DEVSTAT_TYPE_CHANGER = 0x008, DEVSTAT_TYPE_COMM = 0x009, DEVSTAT_TYPE_ASC0 = 0x00a, DEVSTAT_TYPE_ASC1 = 0x00b, DEVSTAT_TYPE_STORARRAY = 0x00c, DEVSTAT_TYPE_ENCLOSURE = 0x00d, DEVSTAT_TYPE_FLOPPY = 0x00e, DEVSTAT_TYPE_MASK = 0x00f, DEVSTAT_TYPE_IF_SCSI = 0x010, DEVSTAT_TYPE_IF_IDE = 0x020, DEVSTAT_TYPE_IF_OTHER = 0x030, + DEVSTAT_TYPE_IF_NVME = 0x040, DEVSTAT_TYPE_IF_MASK = 0x0f0, DEVSTAT_TYPE_PASS = 0x100 } devstat_type_flags; .Ed .Pp Devices have a priority associated with them, which controls roughly where they are placed in the .Nm list. The priorities are as follows: .Bd -literal -offset indent typedef enum { DEVSTAT_PRIORITY_MIN = 0x000, DEVSTAT_PRIORITY_OTHER = 0x020, DEVSTAT_PRIORITY_PASS = 0x030, DEVSTAT_PRIORITY_FD = 0x040, DEVSTAT_PRIORITY_WFD = 0x050, DEVSTAT_PRIORITY_TAPE = 0x060, DEVSTAT_PRIORITY_CD = 0x090, DEVSTAT_PRIORITY_DISK = 0x110, DEVSTAT_PRIORITY_ARRAY = 0x120, DEVSTAT_PRIORITY_MAX = 0xfff } devstat_priority; .Ed .Pp Each device has associated with it flags to indicate what operations are supported or not supported. The .Va devstat_support_flags values are as follows: .Bl -tag -width DEVSTAT_NO_ORDERED_TAGS .It DEVSTAT_ALL_SUPPORTED Every statistic type is supported by the device. .It DEVSTAT_NO_BLOCKSIZE This device does not have a blocksize. .It DEVSTAT_NO_ORDERED_TAGS This device does not support ordered tags. .It DEVSTAT_BS_UNAVAILABLE This device supports a blocksize, but it is currently unavailable. This flag is most often used with removable media drives. .El .Pp Transactions to a device fall into one of three categories, which are represented in the .Va flags passed into .Fn devstat_end_transaction . The transaction types are as follows: .Bd -literal -offset indent typedef enum { DEVSTAT_NO_DATA = 0x00, DEVSTAT_READ = 0x01, DEVSTAT_WRITE = 0x02, DEVSTAT_FREE = 0x03 } devstat_trans_flags; #define DEVSTAT_N_TRANS_FLAGS 4 .Ed .Pp DEVSTAT_NO_DATA is a type of transactions to the device which are neither reads or writes. For instance, .Tn SCSI drivers often send a test unit ready command to .Tn SCSI devices. The test unit ready command does not read or write any data. It merely causes the device to return its status. .Pp There are four possible values for the .Va tag_type argument to .Fn devstat_end_transaction : .Bl -tag -width DEVSTAT_TAG_ORDERED .It DEVSTAT_TAG_SIMPLE The transaction had a simple tag. .It DEVSTAT_TAG_HEAD The transaction had a head of queue tag. .It DEVSTAT_TAG_ORDERED The transaction had an ordered tag. .It DEVSTAT_TAG_NONE The device does not support tags. .El .Pp The tag type values correspond to the lower four bits of the .Tn SCSI tag definitions. In CAM, for instance, the .Va tag_action from the CCB is ORed with 0xf to determine the tag type to pass in to .Fn devstat_end_transaction . .Pp There is a macro, .Dv DEVSTAT_VERSION that is defined in .In sys/devicestat.h . This is the current version of the .Nm subsystem, and it should be incremented each time a change is made that would require recompilation of userland programs that access .Nm statistics. Userland programs use this version, via the .Va kern.devstat.version .Nm sysctl variable to determine whether they are in sync with the kernel .Nm structures. .Sh SEE ALSO .Xr systat 1 , .Xr devstat 3 , .Xr iostat 8 , .Xr rpc.rstatd 8 , .Xr vmstat 8 .Sh HISTORY The .Nm statistics system appeared in .Fx 3.0 . .Sh AUTHORS .An Kenneth Merry Aq Mt ken@FreeBSD.org .Sh BUGS There may be a need for .Fn spl protection around some of the .Nm list manipulation code to ensure, for example, that the list of devices is not changed while someone is fetching the .Va kern.devstat.all .Nm sysctl variable. diff --git a/sys/cam/cam_ccb.h b/sys/cam/cam_ccb.h index 9d52213e3952..66b374008aa5 100644 --- a/sys/cam/cam_ccb.h +++ b/sys/cam/cam_ccb.h @@ -1,1555 +1,1556 @@ /*- * Data structures and definitions for CAM Control Blocks (CCBs). * * SPDX-License-Identifier: BSD-2-Clause * * Copyright (c) 1997, 1998 Justin T. Gibbs. * 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. */ #ifndef _CAM_CAM_CCB_H #define _CAM_CAM_CCB_H 1 #include #include #include #ifndef _KERNEL #include #endif #include #include #include #include #include /* General allocation length definitions for CCB structures */ #define IOCDBLEN CAM_MAX_CDBLEN /* Space for CDB bytes/pointer */ #define VUHBALEN 14 /* Vendor Unique HBA length */ #define SIM_IDLEN 16 /* ASCII string len for SIM ID */ #define HBA_IDLEN 16 /* ASCII string len for HBA ID */ #define DEV_IDLEN 16 /* ASCII string len for device names */ #define CCB_PERIPH_PRIV_SIZE 2 /* size of peripheral private area */ #define CCB_SIM_PRIV_SIZE 2 /* size of sim private area */ /* Struct definitions for CAM control blocks */ /* Common CCB header */ /* CCB memory allocation flags */ typedef enum { CAM_CCB_FROM_UMA = 0x00000001,/* CCB from a periph UMA zone */ } ccb_alloc_flags; /* CAM CCB flags */ typedef enum { CAM_CDB_POINTER = 0x00000001,/* The CDB field is a pointer */ CAM_unused1 = 0x00000002, CAM_unused2 = 0x00000004, CAM_NEGOTIATE = 0x00000008,/* * Perform transport negotiation * with this command. */ CAM_DATA_ISPHYS = 0x00000010,/* Data type with physical addrs */ CAM_DIS_AUTOSENSE = 0x00000020,/* Disable autosense feature */ CAM_DIR_BOTH = 0x00000000,/* Data direction (00:IN/OUT) */ CAM_DIR_IN = 0x00000040,/* Data direction (01:DATA IN) */ CAM_DIR_OUT = 0x00000080,/* Data direction (10:DATA OUT) */ CAM_DIR_NONE = 0x000000C0,/* Data direction (11:no data) */ CAM_DIR_MASK = 0x000000C0,/* Data direction Mask */ CAM_DATA_VADDR = 0x00000000,/* Data type (000:Virtual) */ CAM_DATA_PADDR = 0x00000010,/* Data type (001:Physical) */ CAM_DATA_SG = 0x00040000,/* Data type (010:sglist) */ CAM_DATA_SG_PADDR = 0x00040010,/* Data type (011:sglist phys) */ CAM_DATA_BIO = 0x00200000,/* Data type (100:bio) */ CAM_DATA_MASK = 0x00240010,/* Data type mask */ CAM_unused3 = 0x00000100, CAM_unused4 = 0x00000200, CAM_DEV_QFRZDIS = 0x00000400,/* Disable DEV Q freezing */ CAM_DEV_QFREEZE = 0x00000800,/* Freeze DEV Q on execution */ CAM_HIGH_POWER = 0x00001000,/* Command takes a lot of power */ CAM_SENSE_PTR = 0x00002000,/* Sense data is a pointer */ CAM_SENSE_PHYS = 0x00004000,/* Sense pointer is physical addr*/ CAM_TAG_ACTION_VALID = 0x00008000,/* Use the tag action in this ccb*/ CAM_PASS_ERR_RECOVER = 0x00010000,/* Pass driver does err. recovery*/ CAM_DIS_DISCONNECT = 0x00020000,/* Disable disconnect */ CAM_unused5 = 0x00080000, CAM_unused6 = 0x00100000, CAM_CDB_PHYS = 0x00400000,/* CDB poiner is physical */ CAM_unused7 = 0x00800000, /* Phase cognizant mode flags */ CAM_unused8 = 0x01000000, CAM_unused9 = 0x02000000, CAM_unused10 = 0x04000000, CAM_unused11 = 0x08000000, CAM_unused12 = 0x10000000, CAM_unused13 = 0x20000000, CAM_unused14 = 0x40000000, /* Host target Mode flags */ CAM_SEND_SENSE = 0x08000000,/* Send sense data with status */ CAM_unused15 = 0x10000000, CAM_unused16 = 0x20000000, CAM_SEND_STATUS = 0x40000000,/* Send status after data phase */ CAM_UNLOCKED = 0x80000000 /* Call callback without lock. */ } ccb_flags; typedef enum { CAM_USER_DATA_ADDR = 0x00000002,/* Userspace data pointers */ CAM_SG_FORMAT_IOVEC = 0x00000004,/* iovec instead of busdma S/G*/ CAM_UNMAPPED_BUF = 0x00000008 /* use unmapped I/O */ } ccb_xflags; /* XPT Opcodes for xpt_action */ typedef enum { /* Function code flags are bits greater than 0xff */ XPT_FC_QUEUED = 0x100, /* Non-immediate function code */ XPT_FC_USER_CCB = 0x200, XPT_FC_XPT_ONLY = 0x400, /* Only for the transport layer device */ XPT_FC_DEV_QUEUED = 0x800 | XPT_FC_QUEUED, /* Passes through the device queues */ /* Common function commands: 0x00->0x0F */ XPT_NOOP = 0x00, /* Execute Nothing */ XPT_SCSI_IO = 0x01 | XPT_FC_DEV_QUEUED, /* Execute the requested I/O operation */ XPT_GDEV_TYPE = 0x02, /* Get type information for specified device */ XPT_GDEVLIST = 0x03, /* Get a list of peripheral devices */ XPT_PATH_INQ = 0x04, /* Path routing inquiry */ XPT_REL_SIMQ = 0x05, /* Release a frozen device queue */ XPT_SASYNC_CB = 0x06, /* Set Asynchronous Callback Parameters */ XPT_SDEV_TYPE = 0x07, /* Set device type information */ XPT_SCAN_BUS = 0x08 | XPT_FC_QUEUED | XPT_FC_USER_CCB | XPT_FC_XPT_ONLY, /* (Re)Scan the SCSI Bus */ XPT_DEV_MATCH = 0x09 | XPT_FC_XPT_ONLY, /* Get EDT entries matching the given pattern */ XPT_DEBUG = 0x0a, /* Turn on debugging for a bus, target or lun */ XPT_PATH_STATS = 0x0b, /* Path statistics (error counts, etc.) */ XPT_GDEV_STATS = 0x0c, /* Device statistics (error counts, etc.) */ XPT_DEV_ADVINFO = 0x0e, /* Get/Set Device advanced information */ XPT_ASYNC = 0x0f | XPT_FC_QUEUED | XPT_FC_USER_CCB | XPT_FC_XPT_ONLY, /* Asynchronous event */ /* SCSI Control Functions: 0x10->0x1F */ XPT_ABORT = 0x10, /* Abort the specified CCB */ XPT_RESET_BUS = 0x11 | XPT_FC_XPT_ONLY, /* Reset the specified SCSI bus */ XPT_RESET_DEV = 0x12 | XPT_FC_DEV_QUEUED, /* Bus Device Reset the specified SCSI device */ XPT_TERM_IO = 0x13, /* Terminate the I/O process */ XPT_SCAN_LUN = 0x14 | XPT_FC_QUEUED | XPT_FC_USER_CCB | XPT_FC_XPT_ONLY, /* Scan Logical Unit */ XPT_GET_TRAN_SETTINGS = 0x15, /* * Get default/user transfer settings * for the target */ XPT_SET_TRAN_SETTINGS = 0x16, /* * Set transfer rate/width * negotiation settings */ XPT_CALC_GEOMETRY = 0x17, /* * Calculate the geometry parameters for * a device give the sector size and * volume size. */ XPT_ATA_IO = 0x18 | XPT_FC_DEV_QUEUED, /* Execute the requested ATA I/O operation */ XPT_GET_SIM_KNOB_OLD = 0x18, /* Compat only */ XPT_SET_SIM_KNOB = 0x19, /* * Set SIM specific knob values. */ XPT_GET_SIM_KNOB = 0x1a, /* * Get SIM specific knob values. */ XPT_SMP_IO = 0x1b | XPT_FC_DEV_QUEUED, /* Serial Management Protocol */ XPT_NVME_IO = 0x1c | XPT_FC_DEV_QUEUED, /* Execute the requested NVMe I/O operation */ XPT_MMC_IO = 0x1d | XPT_FC_DEV_QUEUED, /* Placeholder for MMC / SD / SDIO I/O stuff */ XPT_SCAN_TGT = 0x1e | XPT_FC_QUEUED | XPT_FC_USER_CCB | XPT_FC_XPT_ONLY, /* Scan Target */ XPT_NVME_ADMIN = 0x1f | XPT_FC_DEV_QUEUED, /* Execute the requested NVMe Admin operation */ /* HBA engine commands 0x20->0x2F */ XPT_ENG_INQ = 0x20 | XPT_FC_XPT_ONLY, /* HBA engine feature inquiry */ XPT_ENG_EXEC = 0x21 | XPT_FC_DEV_QUEUED, /* HBA execute engine request */ /* Target mode commands: 0x30->0x3F */ XPT_EN_LUN = 0x30, /* Enable LUN as a target */ XPT_TARGET_IO = 0x31 | XPT_FC_DEV_QUEUED, /* Execute target I/O request */ XPT_ACCEPT_TARGET_IO = 0x32 | XPT_FC_QUEUED | XPT_FC_USER_CCB, /* Accept Host Target Mode CDB */ XPT_CONT_TARGET_IO = 0x33 | XPT_FC_DEV_QUEUED, /* Continue Host Target I/O Connection */ XPT_IMMED_NOTIFY = 0x34 | XPT_FC_QUEUED | XPT_FC_USER_CCB, /* Notify Host Target driver of event (obsolete) */ XPT_NOTIFY_ACK = 0x35, /* Acknowledgement of event (obsolete) */ XPT_IMMEDIATE_NOTIFY = 0x36 | XPT_FC_QUEUED | XPT_FC_USER_CCB, /* Notify Host Target driver of event */ XPT_NOTIFY_ACKNOWLEDGE = 0x37 | XPT_FC_QUEUED | XPT_FC_USER_CCB, /* Acknowledgement of event */ XPT_REPROBE_LUN = 0x38 | XPT_FC_QUEUED | XPT_FC_USER_CCB, /* Query device capacity and notify GEOM */ XPT_MMC_SET_TRAN_SETTINGS = 0x40 | XPT_FC_DEV_QUEUED, XPT_MMC_GET_TRAN_SETTINGS = 0x41 | XPT_FC_DEV_QUEUED, /* Vendor Unique codes: 0x80->0x8F */ XPT_VUNIQUE = 0x80 } xpt_opcode; #define XPT_FC_GROUP_MASK 0xF0 #define XPT_FC_GROUP(op) ((op) & XPT_FC_GROUP_MASK) #define XPT_FC_GROUP_COMMON 0x00 #define XPT_FC_GROUP_SCSI_CONTROL 0x10 #define XPT_FC_GROUP_HBA_ENGINE 0x20 #define XPT_FC_GROUP_TMODE 0x30 #define XPT_FC_GROUP_VENDOR_UNIQUE 0x80 #define XPT_FC_IS_DEV_QUEUED(ccb) \ (((ccb)->ccb_h.func_code & XPT_FC_DEV_QUEUED) == XPT_FC_DEV_QUEUED) #define XPT_FC_IS_QUEUED(ccb) \ (((ccb)->ccb_h.func_code & XPT_FC_QUEUED) != 0) typedef enum { PROTO_UNKNOWN, PROTO_UNSPECIFIED, PROTO_SCSI, /* Small Computer System Interface */ PROTO_ATA, /* AT Attachment */ PROTO_ATAPI, /* AT Attachment Packetized Interface */ PROTO_SATAPM, /* SATA Port Multiplier */ PROTO_SEMB, /* SATA Enclosure Management Bridge */ PROTO_NVME, /* NVME */ PROTO_MMCSD, /* MMC, SD, SDIO */ } cam_proto; typedef enum { XPORT_UNKNOWN, XPORT_UNSPECIFIED, XPORT_SPI, /* SCSI Parallel Interface */ XPORT_FC, /* Fiber Channel */ XPORT_SSA, /* Serial Storage Architecture */ XPORT_USB, /* Universal Serial Bus */ XPORT_PPB, /* Parallel Port Bus */ XPORT_ATA, /* AT Attachment */ XPORT_SAS, /* Serial Attached SCSI */ XPORT_SATA, /* Serial AT Attachment */ XPORT_ISCSI, /* iSCSI */ XPORT_SRP, /* SCSI RDMA Protocol */ XPORT_NVME, /* NVMe over PCIe */ XPORT_MMCSD, /* MMC, SD, SDIO card */ } cam_xport; #define XPORT_IS_NVME(t) ((t) == XPORT_NVME) #define XPORT_IS_ATA(t) ((t) == XPORT_ATA || (t) == XPORT_SATA) #define XPORT_IS_SCSI(t) ((t) != XPORT_UNKNOWN && \ (t) != XPORT_UNSPECIFIED && \ !XPORT_IS_ATA(t) && !XPORT_IS_NVME(t)) #define XPORT_DEVSTAT_TYPE(t) (XPORT_IS_ATA(t) ? DEVSTAT_TYPE_IF_IDE : \ XPORT_IS_SCSI(t) ? DEVSTAT_TYPE_IF_SCSI : \ + XPORT_IS_NVME(t) ? DEVSTAT_TYPE_IF_NVME : \ DEVSTAT_TYPE_IF_OTHER) #define PROTO_VERSION_UNKNOWN (UINT_MAX - 1) #define PROTO_VERSION_UNSPECIFIED UINT_MAX #define XPORT_VERSION_UNKNOWN (UINT_MAX - 1) #define XPORT_VERSION_UNSPECIFIED UINT_MAX typedef union { LIST_ENTRY(ccb_hdr) le; SLIST_ENTRY(ccb_hdr) sle; TAILQ_ENTRY(ccb_hdr) tqe; STAILQ_ENTRY(ccb_hdr) stqe; } camq_entry; typedef union { void *ptr; u_long field; uint8_t bytes[sizeof(uintptr_t)]; } ccb_priv_entry; typedef union { ccb_priv_entry entries[CCB_PERIPH_PRIV_SIZE]; uint8_t bytes[CCB_PERIPH_PRIV_SIZE * sizeof(ccb_priv_entry)]; } ccb_ppriv_area; typedef union { ccb_priv_entry entries[CCB_SIM_PRIV_SIZE]; uint8_t bytes[CCB_SIM_PRIV_SIZE * sizeof(ccb_priv_entry)]; } ccb_spriv_area; typedef struct { struct timeval *etime; uintptr_t sim_data; uintptr_t periph_data; } ccb_qos_area; struct ccb_hdr { cam_pinfo pinfo; /* Info for priority scheduling */ camq_entry xpt_links; /* For chaining in the XPT layer */ camq_entry sim_links; /* For chaining in the SIM layer */ camq_entry periph_links; /* For chaining in the type driver */ #if BYTE_ORDER == LITTLE_ENDIAN uint16_t retry_count; uint16_t alloc_flags; /* ccb_alloc_flags */ #else uint16_t alloc_flags; /* ccb_alloc_flags */ uint16_t retry_count; #endif void (*cbfcnp)(struct cam_periph *, union ccb *); /* Callback on completion function */ xpt_opcode func_code; /* XPT function code */ uint32_t status; /* Status returned by CAM subsystem */ struct cam_path *path; /* Compiled path for this ccb */ path_id_t path_id; /* Path ID for the request */ target_id_t target_id; /* Target device ID */ lun_id_t target_lun; /* Target LUN number */ uint32_t flags; /* ccb_flags */ uint32_t xflags; /* Extended flags */ ccb_ppriv_area periph_priv; ccb_spriv_area sim_priv; ccb_qos_area qos; uint32_t timeout; /* Hard timeout value in mseconds */ struct timeval softtimeout; /* Soft timeout value in sec + usec */ }; /* Get Device Information CCB */ struct ccb_getdev { struct ccb_hdr ccb_h; cam_proto protocol; struct scsi_inquiry_data inq_data; struct ata_params ident_data; uint8_t serial_num[252]; uint8_t inq_flags; uint8_t serial_num_len; void *padding[2]; }; /* Device Statistics CCB */ struct ccb_getdevstats { struct ccb_hdr ccb_h; int dev_openings; /* Space left for more work on device*/ int dev_active; /* Transactions running on the device */ int allocated; /* CCBs allocated for the device */ int queued; /* CCBs queued to be sent to the device */ int held; /* * CCBs held by peripheral drivers * for this device */ int maxtags; /* * Boundary conditions for number of * tagged operations */ int mintags; struct timeval last_reset; /* Time of last bus reset/loop init */ }; typedef enum { CAM_GDEVLIST_LAST_DEVICE, CAM_GDEVLIST_LIST_CHANGED, CAM_GDEVLIST_MORE_DEVS, CAM_GDEVLIST_ERROR } ccb_getdevlist_status_e; struct ccb_getdevlist { struct ccb_hdr ccb_h; char periph_name[DEV_IDLEN]; uint32_t unit_number; unsigned int generation; uint32_t index; ccb_getdevlist_status_e status; }; typedef enum { PERIPH_MATCH_ANY = 0x000, PERIPH_MATCH_PATH = 0x001, PERIPH_MATCH_TARGET = 0x002, PERIPH_MATCH_LUN = 0x004, PERIPH_MATCH_NAME = 0x008, PERIPH_MATCH_UNIT = 0x010, } periph_pattern_flags; struct periph_match_pattern { char periph_name[DEV_IDLEN]; uint32_t unit_number; path_id_t path_id; target_id_t target_id; lun_id_t target_lun; periph_pattern_flags flags; }; typedef enum { DEV_MATCH_ANY = 0x000, DEV_MATCH_PATH = 0x001, DEV_MATCH_TARGET = 0x002, DEV_MATCH_LUN = 0x004, DEV_MATCH_INQUIRY = 0x008, DEV_MATCH_DEVID = 0x010, } dev_pattern_flags; struct device_id_match_pattern { uint8_t id_len; uint8_t id[256]; }; struct device_match_pattern { path_id_t path_id; target_id_t target_id; lun_id_t target_lun; dev_pattern_flags flags; union { struct scsi_static_inquiry_pattern inq_pat; struct device_id_match_pattern devid_pat; } data; }; typedef enum { BUS_MATCH_ANY = 0x000, BUS_MATCH_PATH = 0x001, BUS_MATCH_NAME = 0x002, BUS_MATCH_UNIT = 0x004, BUS_MATCH_BUS_ID = 0x008, } bus_pattern_flags; struct bus_match_pattern { path_id_t path_id; char dev_name[DEV_IDLEN]; uint32_t unit_number; uint32_t bus_id; bus_pattern_flags flags; }; union match_pattern { struct periph_match_pattern periph_pattern; struct device_match_pattern device_pattern; struct bus_match_pattern bus_pattern; }; typedef enum { DEV_MATCH_PERIPH, DEV_MATCH_DEVICE, DEV_MATCH_BUS } dev_match_type; struct dev_match_pattern { dev_match_type type; union match_pattern pattern; }; struct periph_match_result { char periph_name[DEV_IDLEN]; uint32_t unit_number; path_id_t path_id; target_id_t target_id; lun_id_t target_lun; }; typedef enum { DEV_RESULT_NOFLAG = 0x00, DEV_RESULT_UNCONFIGURED = 0x01 } dev_result_flags; struct device_match_result { path_id_t path_id; target_id_t target_id; lun_id_t target_lun; cam_proto protocol; struct scsi_inquiry_data inq_data; struct ata_params ident_data; dev_result_flags flags; }; struct bus_match_result { path_id_t path_id; char dev_name[DEV_IDLEN]; uint32_t unit_number; uint32_t bus_id; }; union match_result { struct periph_match_result periph_result; struct device_match_result device_result; struct bus_match_result bus_result; }; struct dev_match_result { dev_match_type type; union match_result result; }; typedef enum { CAM_DEV_MATCH_LAST, CAM_DEV_MATCH_MORE, CAM_DEV_MATCH_LIST_CHANGED, CAM_DEV_MATCH_SIZE_ERROR, CAM_DEV_MATCH_ERROR } ccb_dev_match_status; typedef enum { CAM_DEV_POS_NONE = 0x000, CAM_DEV_POS_BUS = 0x001, CAM_DEV_POS_TARGET = 0x002, CAM_DEV_POS_DEVICE = 0x004, CAM_DEV_POS_PERIPH = 0x008, CAM_DEV_POS_PDPTR = 0x010, CAM_DEV_POS_TYPEMASK = 0xf00, CAM_DEV_POS_EDT = 0x100, CAM_DEV_POS_PDRV = 0x200 } dev_pos_type; struct ccb_dm_cookie { void *bus; void *target; void *device; void *periph; void *pdrv; }; struct ccb_dev_position { u_int generations[4]; #define CAM_BUS_GENERATION 0x00 #define CAM_TARGET_GENERATION 0x01 #define CAM_DEV_GENERATION 0x02 #define CAM_PERIPH_GENERATION 0x03 dev_pos_type position_type; struct ccb_dm_cookie cookie; }; struct ccb_dev_match { struct ccb_hdr ccb_h; ccb_dev_match_status status; uint32_t num_patterns; uint32_t pattern_buf_len; struct dev_match_pattern *patterns; uint32_t num_matches; uint32_t match_buf_len; struct dev_match_result *matches; struct ccb_dev_position pos; }; /* * Definitions for the path inquiry CCB fields. */ #define CAM_VERSION 0x1a /* Hex value for current version */ typedef enum { PI_MDP_ABLE = 0x80, /* Supports MDP message */ PI_WIDE_32 = 0x40, /* Supports 32 bit wide SCSI */ PI_WIDE_16 = 0x20, /* Supports 16 bit wide SCSI */ PI_SDTR_ABLE = 0x10, /* Supports SDTR message */ PI_LINKED_CDB = 0x08, /* Supports linked CDBs */ PI_SATAPM = 0x04, /* Supports SATA PM */ PI_TAG_ABLE = 0x02, /* Supports tag queue messages */ PI_SOFT_RST = 0x01 /* Supports soft reset alternative */ } pi_inqflag; typedef enum { PIT_PROCESSOR = 0x80, /* Target mode processor mode */ PIT_PHASE = 0x40, /* Target mode phase cog. mode */ PIT_DISCONNECT = 0x20, /* Disconnects supported in target mode */ PIT_TERM_IO = 0x10, /* Terminate I/O message supported in TM */ PIT_GRP_6 = 0x08, /* Group 6 commands supported */ PIT_GRP_7 = 0x04 /* Group 7 commands supported */ } pi_tmflag; typedef enum { PIM_ATA_EXT = 0x200,/* ATA requests can understand ata_ext requests */ PIM_EXTLUNS = 0x100,/* 64bit extended LUNs supported */ PIM_SCANHILO = 0x80, /* Bus scans from high ID to low ID */ PIM_NOREMOVE = 0x40, /* Removeable devices not included in scan */ PIM_NOINITIATOR = 0x20, /* Initiator role not supported. */ PIM_NOBUSRESET = 0x10, /* User has disabled initial BUS RESET */ PIM_NO_6_BYTE = 0x08, /* Do not send 6-byte commands */ PIM_SEQSCAN = 0x04, /* Do bus scans sequentially, not in parallel */ PIM_UNMAPPED = 0x02, PIM_NOSCAN = 0x01 /* SIM does its own scanning */ } pi_miscflag; /* Path Inquiry CCB */ struct ccb_pathinq_settings_spi { uint8_t ppr_options; }; struct ccb_pathinq_settings_fc { uint64_t wwnn; /* world wide node name */ uint64_t wwpn; /* world wide port name */ uint32_t port; /* 24 bit port id, if known */ uint32_t bitrate; /* Mbps */ }; struct ccb_pathinq_settings_sas { uint32_t bitrate; /* Mbps */ }; #define NVME_DEV_NAME_LEN 52 struct ccb_pathinq_settings_nvme { uint32_t nsid; /* Namespace ID for this path */ uint32_t domain; uint8_t bus; uint8_t slot; uint8_t function; uint8_t extra; char dev_name[NVME_DEV_NAME_LEN]; /* nvme controller dev name for this device */ }; _Static_assert(sizeof(struct ccb_pathinq_settings_nvme) == 64, "ccb_pathinq_settings_nvme too big"); #define PATHINQ_SETTINGS_SIZE 128 struct ccb_pathinq { struct ccb_hdr ccb_h; uint8_t version_num; /* Version number for the SIM/HBA */ uint8_t hba_inquiry; /* Mimic of INQ byte 7 for the HBA */ uint16_t target_sprt; /* Flags for target mode support */ uint32_t hba_misc; /* Misc HBA features */ uint16_t hba_eng_cnt; /* HBA engine count */ /* Vendor Unique capabilities */ uint8_t vuhba_flags[VUHBALEN]; uint32_t max_target; /* Maximum supported Target */ uint32_t max_lun; /* Maximum supported Lun */ uint32_t async_flags; /* Installed Async handlers */ path_id_t hpath_id; /* Highest Path ID in the subsystem */ target_id_t initiator_id; /* ID of the HBA on the SCSI bus */ char sim_vid[SIM_IDLEN]; /* Vendor ID of the SIM */ char hba_vid[HBA_IDLEN]; /* Vendor ID of the HBA */ char dev_name[DEV_IDLEN];/* Device name for SIM */ uint32_t unit_number; /* Unit number for SIM */ uint32_t bus_id; /* Bus ID for SIM */ uint32_t base_transfer_speed;/* Base bus speed in KB/sec */ cam_proto protocol; u_int protocol_version; cam_xport transport; u_int transport_version; union { struct ccb_pathinq_settings_spi spi; struct ccb_pathinq_settings_fc fc; struct ccb_pathinq_settings_sas sas; struct ccb_pathinq_settings_nvme nvme; char ccb_pathinq_settings_opaque[PATHINQ_SETTINGS_SIZE]; } xport_specific; u_int maxio; /* Max supported I/O size, in bytes. */ uint16_t hba_vendor; /* HBA vendor ID */ uint16_t hba_device; /* HBA device ID */ uint16_t hba_subvendor; /* HBA subvendor ID */ uint16_t hba_subdevice; /* HBA subdevice ID */ }; /* Path Statistics CCB */ struct ccb_pathstats { struct ccb_hdr ccb_h; struct timeval last_reset; /* Time of last bus reset/loop init */ }; typedef enum { SMP_FLAG_NONE = 0x00, SMP_FLAG_REQ_SG = 0x01, SMP_FLAG_RSP_SG = 0x02 } ccb_smp_pass_flags; /* * Serial Management Protocol CCB * XXX Currently the semantics for this CCB are that it is executed either * by the addressed device, or that device's parent (i.e. an expander for * any device on an expander) if the addressed device doesn't support SMP. * Later, once we have the ability to probe SMP-only devices and put them * in CAM's topology, the CCB will only be executed by the addressed device * if possible. */ struct ccb_smpio { struct ccb_hdr ccb_h; uint8_t *smp_request; int smp_request_len; uint16_t smp_request_sglist_cnt; uint8_t *smp_response; int smp_response_len; uint16_t smp_response_sglist_cnt; ccb_smp_pass_flags flags; }; typedef union { uint8_t *sense_ptr; /* * Pointer to storage * for sense information */ /* Storage Area for sense information */ struct scsi_sense_data sense_buf; } sense_t; typedef union { uint8_t *cdb_ptr; /* Pointer to the CDB bytes to send */ /* Area for the CDB send */ uint8_t cdb_bytes[IOCDBLEN]; } cdb_t; /* * SCSI I/O Request CCB used for the XPT_SCSI_IO and XPT_CONT_TARGET_IO * function codes. */ struct ccb_scsiio { struct ccb_hdr ccb_h; union ccb *next_ccb; /* Ptr for next CCB for action */ uint8_t *req_map; /* Ptr to mapping info */ uint8_t *data_ptr; /* Ptr to the data buf/SG list */ uint32_t dxfer_len; /* Data transfer length */ /* Autosense storage */ struct scsi_sense_data sense_data; uint8_t sense_len; /* Number of bytes to autosense */ uint8_t cdb_len; /* Number of bytes for the CDB */ uint16_t sglist_cnt; /* Number of SG list entries */ uint8_t scsi_status; /* Returned SCSI status */ uint8_t sense_resid; /* Autosense resid length: 2's comp */ uint32_t resid; /* Transfer residual length: 2's comp */ cdb_t cdb_io; /* Union for CDB bytes/pointer */ uint8_t *msg_ptr; /* Pointer to the message buffer */ uint16_t msg_len; /* Number of bytes for the Message */ uint8_t tag_action; /* What to do for tag queueing */ /* * The tag action should be either the define below (to send a * non-tagged transaction) or one of the defined scsi tag messages * from scsi_message.h. */ #define CAM_TAG_ACTION_NONE 0x00 uint8_t priority; /* Command priority for SIMPLE tag */ u_int tag_id; /* tag id from initator (target mode) */ u_int init_id; /* initiator id of who selected */ #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING) struct bio *bio; /* Associated bio */ #endif }; static __inline uint8_t * scsiio_cdb_ptr(struct ccb_scsiio *ccb) { return ((ccb->ccb_h.flags & CAM_CDB_POINTER) ? ccb->cdb_io.cdb_ptr : ccb->cdb_io.cdb_bytes); } /* * ATA I/O Request CCB used for the XPT_ATA_IO function code. */ struct ccb_ataio { struct ccb_hdr ccb_h; union ccb *next_ccb; /* Ptr for next CCB for action */ struct ata_cmd cmd; /* ATA command register set */ struct ata_res res; /* ATA result register set */ uint8_t *data_ptr; /* Ptr to the data buf/SG list */ uint32_t dxfer_len; /* Data transfer length */ uint32_t resid; /* Transfer residual length: 2's comp */ uint8_t ata_flags; /* Flags for the rest of the buffer */ #define ATA_FLAG_AUX 0x1 #define ATA_FLAG_ICC 0x2 uint8_t icc; /* Isochronous Command Completion */ uint32_t aux; uint32_t unused; }; /* * MMC I/O Request CCB used for the XPT_MMC_IO function code. */ struct ccb_mmcio { struct ccb_hdr ccb_h; union ccb *next_ccb; /* Ptr for next CCB for action */ struct mmc_command cmd; struct mmc_command stop; }; struct ccb_accept_tio { struct ccb_hdr ccb_h; cdb_t cdb_io; /* Union for CDB bytes/pointer */ uint8_t cdb_len; /* Number of bytes for the CDB */ uint8_t tag_action; /* What to do for tag queueing */ uint8_t sense_len; /* Number of bytes of Sense Data */ uint8_t priority; /* Command priority for SIMPLE tag */ u_int tag_id; /* tag id from initator (target mode) */ u_int init_id; /* initiator id of who selected */ struct scsi_sense_data sense_data; }; static __inline uint8_t * atio_cdb_ptr(struct ccb_accept_tio *ccb) { return ((ccb->ccb_h.flags & CAM_CDB_POINTER) ? ccb->cdb_io.cdb_ptr : ccb->cdb_io.cdb_bytes); } /* Release SIM Queue */ struct ccb_relsim { struct ccb_hdr ccb_h; uint32_t release_flags; #define RELSIM_ADJUST_OPENINGS 0x01 #define RELSIM_RELEASE_AFTER_TIMEOUT 0x02 #define RELSIM_RELEASE_AFTER_CMDCMPLT 0x04 #define RELSIM_RELEASE_AFTER_QEMPTY 0x08 uint32_t openings; uint32_t release_timeout; /* Abstract argument. */ uint32_t qfrozen_cnt; }; /* * NVMe I/O Request CCB used for the XPT_NVME_IO and XPT_NVME_ADMIN function codes. */ struct ccb_nvmeio { struct ccb_hdr ccb_h; union ccb *next_ccb; /* Ptr for next CCB for action */ struct nvme_command cmd; /* NVME command, per NVME standard */ struct nvme_completion cpl; /* NVME completion, per NVME standard */ uint8_t *data_ptr; /* Ptr to the data buf/SG list */ uint32_t dxfer_len; /* Data transfer length */ uint16_t sglist_cnt; /* Number of SG list entries */ uint16_t unused; /* padding for removed uint32_t */ }; /* * Definitions for the asynchronous callback CCB fields. */ typedef enum { AC_UNIT_ATTENTION = 0x4000,/* Device reported UNIT ATTENTION */ AC_ADVINFO_CHANGED = 0x2000,/* Advance info might have changes */ AC_CONTRACT = 0x1000,/* A contractual callback */ AC_GETDEV_CHANGED = 0x800,/* Getdev info might have changed */ AC_INQ_CHANGED = 0x400,/* Inquiry info might have changed */ AC_TRANSFER_NEG = 0x200,/* New transfer settings in effect */ AC_LOST_DEVICE = 0x100,/* A device went away */ AC_FOUND_DEVICE = 0x080,/* A new device was found */ AC_PATH_DEREGISTERED = 0x040,/* A path has de-registered */ AC_PATH_REGISTERED = 0x020,/* A new path has been registered */ AC_SENT_BDR = 0x010,/* A BDR message was sent to target */ AC_SCSI_AEN = 0x008,/* A SCSI AEN has been received */ AC_UNSOL_RESEL = 0x002,/* Unsolicited reselection occurred */ AC_BUS_RESET = 0x001 /* A SCSI bus reset occurred */ } ac_code; typedef void ac_callback_t (void *softc, uint32_t code, struct cam_path *path, void *args); /* * Generic Asynchronous callbacks. * * Generic arguments passed bac which are then interpreted between a per-system * contract number. */ #define AC_CONTRACT_DATA_MAX (128 - sizeof (uint64_t)) struct ac_contract { uint64_t contract_number; uint8_t contract_data[AC_CONTRACT_DATA_MAX]; }; #define AC_CONTRACT_DEV_CHG 1 struct ac_device_changed { uint64_t wwpn; uint32_t port; target_id_t target; uint8_t arrived; }; /* Set Asynchronous Callback CCB */ struct ccb_setasync { struct ccb_hdr ccb_h; uint32_t event_enable; /* Async Event enables */ ac_callback_t *callback; void *callback_arg; }; /* Set Device Type CCB */ struct ccb_setdev { struct ccb_hdr ccb_h; uint8_t dev_type; /* Value for dev type field in EDT */ }; /* SCSI Control Functions */ /* Abort XPT request CCB */ struct ccb_abort { struct ccb_hdr ccb_h; union ccb *abort_ccb; /* Pointer to CCB to abort */ }; /* Reset SCSI Bus CCB */ struct ccb_resetbus { struct ccb_hdr ccb_h; }; /* Reset SCSI Device CCB */ struct ccb_resetdev { struct ccb_hdr ccb_h; }; /* Terminate I/O Process Request CCB */ struct ccb_termio { struct ccb_hdr ccb_h; union ccb *termio_ccb; /* Pointer to CCB to terminate */ }; typedef enum { CTS_TYPE_CURRENT_SETTINGS, CTS_TYPE_USER_SETTINGS } cts_type; struct ccb_trans_settings_scsi { u_int valid; /* Which fields to honor */ #define CTS_SCSI_VALID_TQ 0x01 u_int flags; #define CTS_SCSI_FLAGS_TAG_ENB 0x01 }; struct ccb_trans_settings_ata { u_int valid; /* Which fields to honor */ #define CTS_ATA_VALID_TQ 0x01 u_int flags; #define CTS_ATA_FLAGS_TAG_ENB 0x01 }; struct ccb_trans_settings_spi { u_int valid; /* Which fields to honor */ #define CTS_SPI_VALID_SYNC_RATE 0x01 #define CTS_SPI_VALID_SYNC_OFFSET 0x02 #define CTS_SPI_VALID_BUS_WIDTH 0x04 #define CTS_SPI_VALID_DISC 0x08 #define CTS_SPI_VALID_PPR_OPTIONS 0x10 u_int flags; #define CTS_SPI_FLAGS_DISC_ENB 0x01 u_int sync_period; u_int sync_offset; u_int bus_width; u_int ppr_options; }; struct ccb_trans_settings_fc { u_int valid; /* Which fields to honor */ #define CTS_FC_VALID_WWNN 0x8000 #define CTS_FC_VALID_WWPN 0x4000 #define CTS_FC_VALID_PORT 0x2000 #define CTS_FC_VALID_SPEED 0x1000 uint64_t wwnn; /* world wide node name */ uint64_t wwpn; /* world wide port name */ uint32_t port; /* 24 bit port id, if known */ uint32_t bitrate; /* Mbps */ }; struct ccb_trans_settings_sas { u_int valid; /* Which fields to honor */ #define CTS_SAS_VALID_SPEED 0x1000 uint32_t bitrate; /* Mbps */ }; struct ccb_trans_settings_pata { u_int valid; /* Which fields to honor */ #define CTS_ATA_VALID_MODE 0x01 #define CTS_ATA_VALID_BYTECOUNT 0x02 #define CTS_ATA_VALID_ATAPI 0x20 #define CTS_ATA_VALID_CAPS 0x40 int mode; /* Mode */ u_int bytecount; /* Length of PIO transaction */ u_int atapi; /* Length of ATAPI CDB */ u_int caps; /* Device and host SATA caps. */ #define CTS_ATA_CAPS_H 0x0000ffff #define CTS_ATA_CAPS_H_DMA48 0x00000001 /* 48-bit DMA */ #define CTS_ATA_CAPS_D 0xffff0000 }; struct ccb_trans_settings_sata { u_int valid; /* Which fields to honor */ #define CTS_SATA_VALID_MODE 0x01 #define CTS_SATA_VALID_BYTECOUNT 0x02 #define CTS_SATA_VALID_REVISION 0x04 #define CTS_SATA_VALID_PM 0x08 #define CTS_SATA_VALID_TAGS 0x10 #define CTS_SATA_VALID_ATAPI 0x20 #define CTS_SATA_VALID_CAPS 0x40 int mode; /* Legacy PATA mode */ u_int bytecount; /* Length of PIO transaction */ int revision; /* SATA revision */ u_int pm_present; /* PM is present (XPT->SIM) */ u_int tags; /* Number of allowed tags */ u_int atapi; /* Length of ATAPI CDB */ u_int caps; /* Device and host SATA caps. */ #define CTS_SATA_CAPS_H 0x0000ffff #define CTS_SATA_CAPS_H_PMREQ 0x00000001 #define CTS_SATA_CAPS_H_APST 0x00000002 #define CTS_SATA_CAPS_H_DMAAA 0x00000010 /* Auto-activation */ #define CTS_SATA_CAPS_H_AN 0x00000020 /* Async. notification */ #define CTS_SATA_CAPS_D 0xffff0000 #define CTS_SATA_CAPS_D_PMREQ 0x00010000 #define CTS_SATA_CAPS_D_APST 0x00020000 }; struct ccb_trans_settings_nvme { u_int valid; /* Which fields to honor */ #define CTS_NVME_VALID_SPEC 0x01 #define CTS_NVME_VALID_CAPS 0x02 #define CTS_NVME_VALID_LINK 0x04 uint32_t spec; /* NVMe spec implemented -- same as vs register */ uint32_t max_xfer; /* Max transfer size (0 -> unlimited */ uint32_t caps; uint8_t lanes; /* Number of PCIe lanes */ uint8_t speed; /* PCIe generation for each lane */ uint8_t max_lanes; /* Number of PCIe lanes */ uint8_t max_speed; /* PCIe generation for each lane */ }; #include struct ccb_trans_settings_mmc { struct mmc_ios ios; #define MMC_CLK (1 << 1) #define MMC_VDD (1 << 2) #define MMC_CS (1 << 3) #define MMC_BW (1 << 4) #define MMC_PM (1 << 5) #define MMC_BT (1 << 6) #define MMC_BM (1 << 7) #define MMC_VCCQ (1 << 8) uint32_t ios_valid; /* The folowing is used only for GET_TRAN_SETTINGS */ uint32_t host_ocr; int host_f_min; int host_f_max; /* Copied from sys/dev/mmc/bridge.h */ #define MMC_CAP_4_BIT_DATA (1 << 0) /* Can do 4-bit data transfers */ #define MMC_CAP_8_BIT_DATA (1 << 1) /* Can do 8-bit data transfers */ #define MMC_CAP_HSPEED (1 << 2) /* Can do High Speed transfers */ #define MMC_CAP_BOOT_NOACC (1 << 4) /* Cannot access boot partitions */ #define MMC_CAP_WAIT_WHILE_BUSY (1 << 5) /* Host waits for busy responses */ #define MMC_CAP_UHS_SDR12 (1 << 6) /* Can do UHS SDR12 */ #define MMC_CAP_UHS_SDR25 (1 << 7) /* Can do UHS SDR25 */ #define MMC_CAP_UHS_SDR50 (1 << 8) /* Can do UHS SDR50 */ #define MMC_CAP_UHS_SDR104 (1 << 9) /* Can do UHS SDR104 */ #define MMC_CAP_UHS_DDR50 (1 << 10) /* Can do UHS DDR50 */ #define MMC_CAP_MMC_DDR52_120 (1 << 11) /* Can do eMMC DDR52 at 1.2 V */ #define MMC_CAP_MMC_DDR52_180 (1 << 12) /* Can do eMMC DDR52 at 1.8 V */ #define MMC_CAP_MMC_DDR52 (MMC_CAP_MMC_DDR52_120 | MMC_CAP_MMC_DDR52_180) #define MMC_CAP_MMC_HS200_120 (1 << 13) /* Can do eMMC HS200 at 1.2 V */ #define MMC_CAP_MMC_HS200_180 (1 << 14) /* Can do eMMC HS200 at 1.8 V */ #define MMC_CAP_MMC_HS200 (MMC_CAP_MMC_HS200_120| MMC_CAP_MMC_HS200_180) #define MMC_CAP_MMC_HS400_120 (1 << 15) /* Can do eMMC HS400 at 1.2 V */ #define MMC_CAP_MMC_HS400_180 (1 << 16) /* Can do eMMC HS400 at 1.8 V */ #define MMC_CAP_MMC_HS400 (MMC_CAP_MMC_HS400_120 | MMC_CAP_MMC_HS400_180) #define MMC_CAP_MMC_HSX00_120 (MMC_CAP_MMC_HS200_120 | MMC_CAP_MMC_HS400_120) #define MMC_CAP_MMC_ENH_STROBE (1 << 17) /* Can do eMMC Enhanced Strobe */ #define MMC_CAP_SIGNALING_120 (1 << 18) /* Can do signaling at 1.2 V */ #define MMC_CAP_SIGNALING_180 (1 << 19) /* Can do signaling at 1.8 V */ #define MMC_CAP_SIGNALING_330 (1 << 20) /* Can do signaling at 3.3 V */ #define MMC_CAP_DRIVER_TYPE_A (1 << 21) /* Can do Driver Type A */ #define MMC_CAP_DRIVER_TYPE_C (1 << 22) /* Can do Driver Type C */ #define MMC_CAP_DRIVER_TYPE_D (1 << 23) /* Can do Driver Type D */ uint32_t host_caps; uint32_t host_max_data; }; /* Get/Set transfer rate/width/disconnection/tag queueing settings */ struct ccb_trans_settings { struct ccb_hdr ccb_h; cts_type type; /* Current or User settings */ cam_proto protocol; u_int protocol_version; cam_xport transport; u_int transport_version; union { u_int valid; /* Which fields to honor */ struct ccb_trans_settings_ata ata; struct ccb_trans_settings_scsi scsi; struct ccb_trans_settings_nvme nvme; struct ccb_trans_settings_mmc mmc; } proto_specific; union { u_int valid; /* Which fields to honor */ struct ccb_trans_settings_spi spi; struct ccb_trans_settings_fc fc; struct ccb_trans_settings_sas sas; struct ccb_trans_settings_pata ata; struct ccb_trans_settings_sata sata; struct ccb_trans_settings_nvme nvme; } xport_specific; }; /* * Calculate the geometry parameters for a device * give the block size and volume size in blocks. */ struct ccb_calc_geometry { struct ccb_hdr ccb_h; uint32_t block_size; uint64_t volume_size; uint32_t cylinders; uint8_t heads; uint8_t secs_per_track; }; /* * Set or get SIM (and transport) specific knobs */ #define KNOB_VALID_ADDRESS 0x1 #define KNOB_VALID_ROLE 0x2 #define KNOB_ROLE_NONE 0x0 #define KNOB_ROLE_INITIATOR 0x1 #define KNOB_ROLE_TARGET 0x2 #define KNOB_ROLE_BOTH 0x3 struct ccb_sim_knob_settings_spi { u_int valid; u_int initiator_id; u_int role; }; struct ccb_sim_knob_settings_fc { u_int valid; uint64_t wwnn; /* world wide node name */ uint64_t wwpn; /* world wide port name */ u_int role; }; struct ccb_sim_knob_settings_sas { u_int valid; uint64_t wwnn; /* world wide node name */ u_int role; }; #define KNOB_SETTINGS_SIZE 128 struct ccb_sim_knob { struct ccb_hdr ccb_h; union { u_int valid; /* Which fields to honor */ struct ccb_sim_knob_settings_spi spi; struct ccb_sim_knob_settings_fc fc; struct ccb_sim_knob_settings_sas sas; char pad[KNOB_SETTINGS_SIZE]; } xport_specific; }; /* * Rescan the given bus, or bus/target/lun */ struct ccb_rescan { struct ccb_hdr ccb_h; cam_flags flags; }; /* * Turn on debugging for the given bus, bus/target, or bus/target/lun. */ struct ccb_debug { struct ccb_hdr ccb_h; cam_debug_flags flags; }; /* Target mode structures. */ struct ccb_en_lun { struct ccb_hdr ccb_h; uint16_t grp6_len; /* Group 6 VU CDB length */ uint16_t grp7_len; /* Group 7 VU CDB length */ uint8_t enable; }; /* old, barely used immediate notify, binary compatibility */ struct ccb_immed_notify { struct ccb_hdr ccb_h; struct scsi_sense_data sense_data; uint8_t sense_len; /* Number of bytes in sense buffer */ uint8_t initiator_id; /* Id of initiator that selected */ uint8_t message_args[7]; /* Message Arguments */ }; struct ccb_notify_ack { struct ccb_hdr ccb_h; uint16_t seq_id; /* Sequence identifier */ uint8_t event; /* Event flags */ }; struct ccb_immediate_notify { struct ccb_hdr ccb_h; u_int tag_id; /* Tag for immediate notify */ u_int seq_id; /* Tag for target of notify */ u_int initiator_id; /* Initiator Identifier */ u_int arg; /* Function specific */ }; struct ccb_notify_acknowledge { struct ccb_hdr ccb_h; u_int tag_id; /* Tag for immediate notify */ u_int seq_id; /* Tar for target of notify */ u_int initiator_id; /* Initiator Identifier */ u_int arg; /* Response information */ /* * Lower byte of arg is one of RESPONSE CODE values defined below * (subset of response codes from SPL-4 and FCP-4 specifications), * upper 3 bytes is code-specific ADDITIONAL RESPONSE INFORMATION. */ #define CAM_RSP_TMF_COMPLETE 0x00 #define CAM_RSP_TMF_REJECTED 0x04 #define CAM_RSP_TMF_FAILED 0x05 #define CAM_RSP_TMF_SUCCEEDED 0x08 #define CAM_RSP_TMF_INCORRECT_LUN 0x09 }; /* HBA engine structures. */ typedef enum { EIT_BUFFER, /* Engine type: buffer memory */ EIT_LOSSLESS, /* Engine type: lossless compression */ EIT_LOSSY, /* Engine type: lossy compression */ EIT_ENCRYPT /* Engine type: encryption */ } ei_type; typedef enum { EAD_VUNIQUE, /* Engine algorithm ID: vendor unique */ EAD_LZ1V1, /* Engine algorithm ID: LZ1 var.1 */ EAD_LZ2V1, /* Engine algorithm ID: LZ2 var.1 */ EAD_LZ2V2 /* Engine algorithm ID: LZ2 var.2 */ } ei_algo; struct ccb_eng_inq { struct ccb_hdr ccb_h; uint16_t eng_num; /* The engine number for this inquiry */ ei_type eng_type; /* Returned engine type */ ei_algo eng_algo; /* Returned engine algorithm type */ uint32_t eng_memeory; /* Returned engine memory size */ }; struct ccb_eng_exec { /* This structure must match SCSIIO size */ struct ccb_hdr ccb_h; uint8_t *pdrv_ptr; /* Ptr used by the peripheral driver */ uint8_t *req_map; /* Ptr for mapping info on the req. */ uint8_t *data_ptr; /* Pointer to the data buf/SG list */ uint32_t dxfer_len; /* Data transfer length */ uint8_t *engdata_ptr; /* Pointer to the engine buffer data */ uint16_t sglist_cnt; /* Num of scatter gather list entries */ uint32_t dmax_len; /* Destination data maximum length */ uint32_t dest_len; /* Destination data length */ int32_t src_resid; /* Source residual length: 2's comp */ uint32_t timeout; /* Timeout value */ uint16_t eng_num; /* Engine number for this request */ uint16_t vu_flags; /* Vendor Unique flags */ }; /* * Definitions for the timeout field in the SCSI I/O CCB. */ #define CAM_TIME_DEFAULT 0x00000000 /* Use SIM default value */ #define CAM_TIME_INFINITY 0xFFFFFFFF /* Infinite timeout */ #define CAM_SUCCESS 0 /* For signaling general success */ #define XPT_CCB_INVALID -1 /* for signaling a bad CCB to free */ /* * CCB for working with advanced device information. This operates in a fashion * similar to XPT_GDEV_TYPE. Specify the target in ccb_h, the buffer * type requested, and provide a buffer size/buffer to write to. If the * buffer is too small, provsiz will be larger than bufsiz. */ struct ccb_dev_advinfo { struct ccb_hdr ccb_h; uint32_t flags; #define CDAI_FLAG_NONE 0x0 /* No flags set */ #define CDAI_FLAG_STORE 0x1 /* If set, action becomes store */ uint32_t buftype; /* IN: Type of data being requested */ /* NB: buftype is interpreted on a per-transport basis */ #define CDAI_TYPE_SCSI_DEVID 1 #define CDAI_TYPE_SERIAL_NUM 2 #define CDAI_TYPE_PHYS_PATH 3 #define CDAI_TYPE_RCAPLONG 4 #define CDAI_TYPE_EXT_INQ 5 #define CDAI_TYPE_NVME_CNTRL 6 /* NVMe Identify Controller data */ #define CDAI_TYPE_NVME_NS 7 /* NVMe Identify Namespace data */ #define CDAI_TYPE_MMC_PARAMS 8 /* MMC/SD ident */ off_t bufsiz; /* IN: Size of external buffer */ #define CAM_SCSI_DEVID_MAXLEN 65536 /* length in buffer is an uint16_t */ off_t provsiz; /* OUT: Size required/used */ uint8_t *buf; /* IN/OUT: Buffer for requested data */ }; /* * CCB for sending async events */ struct ccb_async { struct ccb_hdr ccb_h; uint32_t async_code; off_t async_arg_size; void *async_arg_ptr; }; /* * Union of all CCB types for kernel space allocation. This union should * never be used for manipulating CCBs - its only use is for the allocation * and deallocation of raw CCB space and is the return type of xpt_ccb_alloc * and the argument to xpt_ccb_free. */ union ccb { struct ccb_hdr ccb_h; /* For convenience */ struct ccb_scsiio csio; struct ccb_getdev cgd; struct ccb_getdevlist cgdl; struct ccb_pathinq cpi; struct ccb_relsim crs; struct ccb_setasync csa; struct ccb_setdev csd; struct ccb_pathstats cpis; struct ccb_getdevstats cgds; struct ccb_dev_match cdm; struct ccb_trans_settings cts; struct ccb_calc_geometry ccg; struct ccb_sim_knob knob; struct ccb_abort cab; struct ccb_resetbus crb; struct ccb_resetdev crd; struct ccb_termio tio; struct ccb_accept_tio atio; struct ccb_scsiio ctio; struct ccb_en_lun cel; struct ccb_immed_notify cin; struct ccb_notify_ack cna; struct ccb_immediate_notify cin1; struct ccb_notify_acknowledge cna2; struct ccb_eng_inq cei; struct ccb_eng_exec cee; struct ccb_smpio smpio; struct ccb_rescan crcn; struct ccb_debug cdbg; struct ccb_ataio ataio; struct ccb_dev_advinfo cdai; struct ccb_async casync; struct ccb_nvmeio nvmeio; struct ccb_mmcio mmcio; }; #define CCB_CLEAR_ALL_EXCEPT_HDR(ccbp) \ bzero((char *)(ccbp) + sizeof((ccbp)->ccb_h), \ sizeof(*(ccbp)) - sizeof((ccbp)->ccb_h)) __BEGIN_DECLS static __inline void cam_fill_csio(struct ccb_scsiio *csio, uint32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb *), uint32_t flags, uint8_t tag_action, uint8_t *data_ptr, uint32_t dxfer_len, uint8_t sense_len, uint8_t cdb_len, uint32_t timeout) { csio->ccb_h.func_code = XPT_SCSI_IO; csio->ccb_h.flags = flags; csio->ccb_h.xflags = 0; csio->ccb_h.retry_count = retries; csio->ccb_h.cbfcnp = cbfcnp; csio->ccb_h.timeout = timeout; csio->data_ptr = data_ptr; csio->dxfer_len = dxfer_len; csio->sense_len = sense_len; csio->cdb_len = cdb_len; csio->tag_action = tag_action; csio->priority = 0; #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING) csio->bio = NULL; #endif } static __inline void cam_fill_ctio(struct ccb_scsiio *csio, uint32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb *), uint32_t flags, u_int tag_action, u_int tag_id, u_int init_id, u_int scsi_status, uint8_t *data_ptr, uint32_t dxfer_len, uint32_t timeout) { csio->ccb_h.func_code = XPT_CONT_TARGET_IO; csio->ccb_h.flags = flags; csio->ccb_h.xflags = 0; csio->ccb_h.retry_count = retries; csio->ccb_h.cbfcnp = cbfcnp; csio->ccb_h.timeout = timeout; csio->data_ptr = data_ptr; csio->dxfer_len = dxfer_len; csio->scsi_status = scsi_status; csio->tag_action = tag_action; csio->priority = 0; csio->tag_id = tag_id; csio->init_id = init_id; } static __inline void cam_fill_ataio(struct ccb_ataio *ataio, uint32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb *), uint32_t flags, u_int tag_action __unused, uint8_t *data_ptr, uint32_t dxfer_len, uint32_t timeout) { ataio->ccb_h.func_code = XPT_ATA_IO; ataio->ccb_h.flags = flags; ataio->ccb_h.retry_count = retries; ataio->ccb_h.cbfcnp = cbfcnp; ataio->ccb_h.timeout = timeout; ataio->data_ptr = data_ptr; ataio->dxfer_len = dxfer_len; ataio->ata_flags = 0; } static __inline void cam_fill_smpio(struct ccb_smpio *smpio, uint32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb *), uint32_t flags, uint8_t *smp_request, int smp_request_len, uint8_t *smp_response, int smp_response_len, uint32_t timeout) { #ifdef _KERNEL KASSERT((flags & CAM_DIR_MASK) == CAM_DIR_BOTH, ("direction != CAM_DIR_BOTH")); KASSERT((smp_request != NULL) && (smp_response != NULL), ("need valid request and response buffers")); KASSERT((smp_request_len != 0) && (smp_response_len != 0), ("need non-zero request and response lengths")); #endif /*_KERNEL*/ smpio->ccb_h.func_code = XPT_SMP_IO; smpio->ccb_h.flags = flags; smpio->ccb_h.retry_count = retries; smpio->ccb_h.cbfcnp = cbfcnp; smpio->ccb_h.timeout = timeout; smpio->smp_request = smp_request; smpio->smp_request_len = smp_request_len; smpio->smp_response = smp_response; smpio->smp_response_len = smp_response_len; } static __inline void cam_fill_mmcio(struct ccb_mmcio *mmcio, uint32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb *), uint32_t flags, uint32_t mmc_opcode, uint32_t mmc_arg, uint32_t mmc_flags, struct mmc_data *mmc_d, uint32_t timeout) { mmcio->ccb_h.func_code = XPT_MMC_IO; mmcio->ccb_h.flags = flags; mmcio->ccb_h.retry_count = retries; mmcio->ccb_h.cbfcnp = cbfcnp; mmcio->ccb_h.timeout = timeout; mmcio->cmd.opcode = mmc_opcode; mmcio->cmd.arg = mmc_arg; mmcio->cmd.flags = mmc_flags; mmcio->stop.opcode = 0; mmcio->stop.arg = 0; mmcio->stop.flags = 0; if (mmc_d != NULL) { mmcio->cmd.data = mmc_d; } else mmcio->cmd.data = NULL; mmcio->cmd.resp[0] = 0; mmcio->cmd.resp[1] = 0; mmcio->cmd.resp[2] = 0; mmcio->cmd.resp[3] = 0; } static __inline void cam_set_ccbstatus(union ccb *ccb, cam_status status) { ccb->ccb_h.status &= ~CAM_STATUS_MASK; ccb->ccb_h.status |= status; } static __inline cam_status cam_ccb_status(union ccb *ccb) { return ((cam_status)(ccb->ccb_h.status & CAM_STATUS_MASK)); } static inline bool cam_ccb_success(union ccb *ccb) { return (cam_ccb_status(ccb) == CAM_REQ_CMP); } void cam_calc_geometry(struct ccb_calc_geometry *ccg, int extended); static __inline void cam_fill_nvmeio(struct ccb_nvmeio *nvmeio, uint32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb *), uint32_t flags, uint8_t *data_ptr, uint32_t dxfer_len, uint32_t timeout) { nvmeio->ccb_h.func_code = XPT_NVME_IO; nvmeio->ccb_h.flags = flags; nvmeio->ccb_h.retry_count = retries; nvmeio->ccb_h.cbfcnp = cbfcnp; nvmeio->ccb_h.timeout = timeout; nvmeio->data_ptr = data_ptr; nvmeio->dxfer_len = dxfer_len; } static __inline void cam_fill_nvmeadmin(struct ccb_nvmeio *nvmeio, uint32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb *), uint32_t flags, uint8_t *data_ptr, uint32_t dxfer_len, uint32_t timeout) { nvmeio->ccb_h.func_code = XPT_NVME_ADMIN; nvmeio->ccb_h.flags = flags; nvmeio->ccb_h.retry_count = retries; nvmeio->ccb_h.cbfcnp = cbfcnp; nvmeio->ccb_h.timeout = timeout; nvmeio->data_ptr = data_ptr; nvmeio->dxfer_len = dxfer_len; } __END_DECLS #endif /* _CAM_CAM_CCB_H */ diff --git a/sys/dev/nvd/nvd.c b/sys/dev/nvd/nvd.c index 6b83757aa938..26bc4ee36d50 100644 --- a/sys/dev/nvd/nvd.c +++ b/sys/dev/nvd/nvd.c @@ -1,525 +1,530 @@ /*- * SPDX-License-Identifier: BSD-2-Clause * * Copyright (C) 2012-2016 Intel Corporation * All rights reserved. * Copyright (C) 2018-2020 Alexander Motin * * 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. */ #include #include #include +#include #include #include #include #include #include #include #include #include #include #include #include #include #include #define NVD_STR "nvd" struct nvd_disk; struct nvd_controller; static disk_ioctl_t nvd_ioctl; static disk_strategy_t nvd_strategy; static dumper_t nvd_dump; static disk_getattr_t nvd_getattr; static void nvd_done(void *arg, const struct nvme_completion *cpl); static void nvd_gone(struct nvd_disk *ndisk); static void *nvd_new_disk(struct nvme_namespace *ns, void *ctrlr); static void *nvd_new_controller(struct nvme_controller *ctrlr); static void nvd_controller_fail(void *ctrlr); static int nvd_load(void); static void nvd_unload(void); MALLOC_DEFINE(M_NVD, "nvd", "nvd(4) allocations"); struct nvme_consumer *consumer_handle; struct nvd_disk { struct nvd_controller *ctrlr; struct bio_queue_head bioq; struct task bioqtask; struct mtx bioqlock; struct disk *disk; struct taskqueue *tq; struct nvme_namespace *ns; uint32_t cur_depth; #define NVD_ODEPTH (1 << 30) uint32_t ordered_in_flight; u_int unit; TAILQ_ENTRY(nvd_disk) global_tailq; TAILQ_ENTRY(nvd_disk) ctrlr_tailq; }; struct nvd_controller { struct nvme_controller *ctrlr; TAILQ_ENTRY(nvd_controller) tailq; TAILQ_HEAD(, nvd_disk) disk_head; }; static struct mtx nvd_lock; static TAILQ_HEAD(, nvd_controller) ctrlr_head; static TAILQ_HEAD(disk_list, nvd_disk) disk_head; static SYSCTL_NODE(_hw, OID_AUTO, nvd, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "nvd driver parameters"); /* * The NVMe specification does not define a maximum or optimal delete size, so * technically max delete size is min(full size of the namespace, 2^32 - 1 * LBAs). A single delete for a multi-TB NVMe namespace though may take much * longer to complete than the nvme(4) I/O timeout period. So choose a sensible * default here that is still suitably large to minimize the number of overall * delete operations. */ static uint64_t nvd_delete_max = (1024 * 1024 * 1024); /* 1GB */ SYSCTL_UQUAD(_hw_nvd, OID_AUTO, delete_max, CTLFLAG_RDTUN, &nvd_delete_max, 0, "nvd maximum BIO_DELETE size in bytes"); static int nvd_modevent(module_t mod, int type, void *arg) { int error = 0; switch (type) { case MOD_LOAD: error = nvd_load(); break; case MOD_UNLOAD: nvd_unload(); break; default: break; } return (error); } moduledata_t nvd_mod = { NVD_STR, (modeventhand_t)nvd_modevent, 0 }; DECLARE_MODULE(nvd, nvd_mod, SI_SUB_DRIVERS, SI_ORDER_ANY); MODULE_VERSION(nvd, 1); MODULE_DEPEND(nvd, nvme, 1, 1, 1); static int nvd_load(void) { if (!nvme_use_nvd) return 0; mtx_init(&nvd_lock, "nvd_lock", NULL, MTX_DEF); TAILQ_INIT(&ctrlr_head); TAILQ_INIT(&disk_head); consumer_handle = nvme_register_consumer(nvd_new_disk, nvd_new_controller, NULL, nvd_controller_fail); return (consumer_handle != NULL ? 0 : -1); } static void nvd_unload(void) { struct nvd_controller *ctrlr; struct nvd_disk *ndisk; if (!nvme_use_nvd) return; mtx_lock(&nvd_lock); while ((ctrlr = TAILQ_FIRST(&ctrlr_head)) != NULL) { TAILQ_REMOVE(&ctrlr_head, ctrlr, tailq); TAILQ_FOREACH(ndisk, &ctrlr->disk_head, ctrlr_tailq) nvd_gone(ndisk); while (!TAILQ_EMPTY(&ctrlr->disk_head)) msleep(&ctrlr->disk_head, &nvd_lock, 0, "nvd_unload",0); free(ctrlr, M_NVD); } mtx_unlock(&nvd_lock); nvme_unregister_consumer(consumer_handle); mtx_destroy(&nvd_lock); } static void nvd_bio_submit(struct nvd_disk *ndisk, struct bio *bp) { int err; bp->bio_driver1 = NULL; if (__predict_false(bp->bio_flags & BIO_ORDERED)) atomic_add_int(&ndisk->cur_depth, NVD_ODEPTH); else atomic_add_int(&ndisk->cur_depth, 1); err = nvme_ns_bio_process(ndisk->ns, bp, nvd_done); if (err) { if (__predict_false(bp->bio_flags & BIO_ORDERED)) { atomic_add_int(&ndisk->cur_depth, -NVD_ODEPTH); atomic_add_int(&ndisk->ordered_in_flight, -1); wakeup(&ndisk->cur_depth); } else { if (atomic_fetchadd_int(&ndisk->cur_depth, -1) == 1 && __predict_false(ndisk->ordered_in_flight != 0)) wakeup(&ndisk->cur_depth); } bp->bio_error = err; bp->bio_flags |= BIO_ERROR; bp->bio_resid = bp->bio_bcount; biodone(bp); } } static void nvd_strategy(struct bio *bp) { struct nvd_disk *ndisk = (struct nvd_disk *)bp->bio_disk->d_drv1; /* * bio with BIO_ORDERED flag must be executed after all previous * bios in the queue, and before any successive bios. */ if (__predict_false(bp->bio_flags & BIO_ORDERED)) { if (atomic_fetchadd_int(&ndisk->ordered_in_flight, 1) == 0 && ndisk->cur_depth == 0 && bioq_first(&ndisk->bioq) == NULL) { nvd_bio_submit(ndisk, bp); return; } } else if (__predict_true(ndisk->ordered_in_flight == 0)) { nvd_bio_submit(ndisk, bp); return; } /* * There are ordered bios in flight, so we need to submit * bios through the task queue to enforce ordering. */ mtx_lock(&ndisk->bioqlock); bioq_insert_tail(&ndisk->bioq, bp); mtx_unlock(&ndisk->bioqlock); taskqueue_enqueue(ndisk->tq, &ndisk->bioqtask); } static void nvd_gone(struct nvd_disk *ndisk) { struct bio *bp; printf(NVD_STR"%u: detached\n", ndisk->unit); mtx_lock(&ndisk->bioqlock); disk_gone(ndisk->disk); while ((bp = bioq_takefirst(&ndisk->bioq)) != NULL) { if (__predict_false(bp->bio_flags & BIO_ORDERED)) atomic_add_int(&ndisk->ordered_in_flight, -1); bp->bio_error = ENXIO; bp->bio_flags |= BIO_ERROR; bp->bio_resid = bp->bio_bcount; biodone(bp); } mtx_unlock(&ndisk->bioqlock); } static void nvd_gonecb(struct disk *dp) { struct nvd_disk *ndisk = (struct nvd_disk *)dp->d_drv1; disk_destroy(ndisk->disk); mtx_lock(&nvd_lock); TAILQ_REMOVE(&disk_head, ndisk, global_tailq); TAILQ_REMOVE(&ndisk->ctrlr->disk_head, ndisk, ctrlr_tailq); if (TAILQ_EMPTY(&ndisk->ctrlr->disk_head)) wakeup(&ndisk->ctrlr->disk_head); mtx_unlock(&nvd_lock); taskqueue_free(ndisk->tq); mtx_destroy(&ndisk->bioqlock); free(ndisk, M_NVD); } static int nvd_ioctl(struct disk *dp, u_long cmd, void *data, int fflag, struct thread *td) { struct nvd_disk *ndisk = dp->d_drv1; return (nvme_ns_ioctl_process(ndisk->ns, cmd, data, fflag, td)); } static int nvd_dump(void *arg, void *virt, off_t offset, size_t len) { struct disk *dp = arg; struct nvd_disk *ndisk = dp->d_drv1; return (nvme_ns_dump(ndisk->ns, virt, offset, len)); } static int nvd_getattr(struct bio *bp) { struct nvd_disk *ndisk = (struct nvd_disk *)bp->bio_disk->d_drv1; const struct nvme_namespace_data *nsdata; u_int i; if (!strcmp("GEOM::lunid", bp->bio_attribute)) { nsdata = nvme_ns_get_data(ndisk->ns); /* Try to return NGUID as lunid. */ for (i = 0; i < sizeof(nsdata->nguid); i++) { if (nsdata->nguid[i] != 0) break; } if (i < sizeof(nsdata->nguid)) { if (bp->bio_length < sizeof(nsdata->nguid) * 2 + 1) return (EFAULT); for (i = 0; i < sizeof(nsdata->nguid); i++) { sprintf(&bp->bio_data[i * 2], "%02x", nsdata->nguid[i]); } bp->bio_completed = bp->bio_length; return (0); } /* Try to return EUI64 as lunid. */ for (i = 0; i < sizeof(nsdata->eui64); i++) { if (nsdata->eui64[i] != 0) break; } if (i < sizeof(nsdata->eui64)) { if (bp->bio_length < sizeof(nsdata->eui64) * 2 + 1) return (EFAULT); for (i = 0; i < sizeof(nsdata->eui64); i++) { sprintf(&bp->bio_data[i * 2], "%02x", nsdata->eui64[i]); } bp->bio_completed = bp->bio_length; return (0); } } return (-1); } static void nvd_done(void *arg, const struct nvme_completion *cpl) { struct bio *bp = (struct bio *)arg; struct nvd_disk *ndisk = bp->bio_disk->d_drv1; if (__predict_false(bp->bio_flags & BIO_ORDERED)) { atomic_add_int(&ndisk->cur_depth, -NVD_ODEPTH); atomic_add_int(&ndisk->ordered_in_flight, -1); wakeup(&ndisk->cur_depth); } else { if (atomic_fetchadd_int(&ndisk->cur_depth, -1) == 1 && __predict_false(ndisk->ordered_in_flight != 0)) wakeup(&ndisk->cur_depth); } biodone(bp); } static void nvd_bioq_process(void *arg, int pending) { struct nvd_disk *ndisk = arg; struct bio *bp; for (;;) { mtx_lock(&ndisk->bioqlock); bp = bioq_takefirst(&ndisk->bioq); mtx_unlock(&ndisk->bioqlock); if (bp == NULL) break; if (__predict_false(bp->bio_flags & BIO_ORDERED)) { /* * bio with BIO_ORDERED flag set must be executed * after all previous bios. */ while (ndisk->cur_depth > 0) tsleep(&ndisk->cur_depth, 0, "nvdorb", 1); } else { /* * bio with BIO_ORDERED flag set must be completed * before proceeding with additional bios. */ while (ndisk->cur_depth >= NVD_ODEPTH) tsleep(&ndisk->cur_depth, 0, "nvdora", 1); } nvd_bio_submit(ndisk, bp); } } static void * nvd_new_controller(struct nvme_controller *ctrlr) { struct nvd_controller *nvd_ctrlr; nvd_ctrlr = malloc(sizeof(struct nvd_controller), M_NVD, M_ZERO | M_WAITOK); nvd_ctrlr->ctrlr = ctrlr; TAILQ_INIT(&nvd_ctrlr->disk_head); mtx_lock(&nvd_lock); TAILQ_INSERT_TAIL(&ctrlr_head, nvd_ctrlr, tailq); mtx_unlock(&nvd_lock); return (nvd_ctrlr); } static void * nvd_new_disk(struct nvme_namespace *ns, void *ctrlr_arg) { uint8_t descr[NVME_MODEL_NUMBER_LENGTH+1]; struct nvd_disk *ndisk, *tnd; struct disk *disk; struct nvd_controller *ctrlr = ctrlr_arg; device_t dev = ctrlr->ctrlr->dev; int unit; ndisk = malloc(sizeof(struct nvd_disk), M_NVD, M_ZERO | M_WAITOK); ndisk->ctrlr = ctrlr; ndisk->ns = ns; ndisk->cur_depth = 0; ndisk->ordered_in_flight = 0; mtx_init(&ndisk->bioqlock, "nvd bioq lock", NULL, MTX_DEF); bioq_init(&ndisk->bioq); TASK_INIT(&ndisk->bioqtask, 0, nvd_bioq_process, ndisk); mtx_lock(&nvd_lock); unit = 0; TAILQ_FOREACH(tnd, &disk_head, global_tailq) { if (tnd->unit > unit) break; unit = tnd->unit + 1; } ndisk->unit = unit; if (tnd != NULL) TAILQ_INSERT_BEFORE(tnd, ndisk, global_tailq); else TAILQ_INSERT_TAIL(&disk_head, ndisk, global_tailq); TAILQ_INSERT_TAIL(&ctrlr->disk_head, ndisk, ctrlr_tailq); mtx_unlock(&nvd_lock); ndisk->tq = taskqueue_create("nvd_taskq", M_WAITOK, taskqueue_thread_enqueue, &ndisk->tq); taskqueue_start_threads(&ndisk->tq, 1, PI_DISK, "nvd taskq"); disk = ndisk->disk = disk_alloc(); disk->d_strategy = nvd_strategy; disk->d_ioctl = nvd_ioctl; disk->d_dump = nvd_dump; disk->d_getattr = nvd_getattr; disk->d_gone = nvd_gonecb; disk->d_name = NVD_STR; disk->d_unit = ndisk->unit; disk->d_drv1 = ndisk; disk->d_sectorsize = nvme_ns_get_sector_size(ns); disk->d_mediasize = (off_t)nvme_ns_get_size(ns); disk->d_maxsize = nvme_ns_get_max_io_xfer_size(ns); disk->d_delmaxsize = (off_t)nvme_ns_get_size(ns); if (disk->d_delmaxsize > nvd_delete_max) disk->d_delmaxsize = nvd_delete_max; disk->d_stripesize = nvme_ns_get_stripesize(ns); disk->d_flags = DISKFLAG_UNMAPPED_BIO | DISKFLAG_DIRECT_COMPLETION; if (nvme_ns_get_flags(ns) & NVME_NS_DEALLOCATE_SUPPORTED) disk->d_flags |= DISKFLAG_CANDELETE; if (nvme_ns_get_flags(ns) & NVME_NS_FLUSH_SUPPORTED) disk->d_flags |= DISKFLAG_CANFLUSHCACHE; + disk->d_devstat = devstat_new_entry(disk->d_name, disk->d_unit, + disk->d_sectorsize, DEVSTAT_ALL_SUPPORTED, + DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_NVME, + DEVSTAT_PRIORITY_DISK); /* * d_ident and d_descr are both far bigger than the length of either * the serial or model number strings. */ nvme_strvis(disk->d_ident, nvme_ns_get_serial_number(ns), sizeof(disk->d_ident), NVME_SERIAL_NUMBER_LENGTH); nvme_strvis(descr, nvme_ns_get_model_number(ns), sizeof(descr), NVME_MODEL_NUMBER_LENGTH); strlcpy(disk->d_descr, descr, sizeof(descr)); /* * For devices that are reported as children of the AHCI controller, * which has no access to the config space for this controller, report * the AHCI controller's data. */ if (ctrlr->ctrlr->quirks & QUIRK_AHCI) dev = device_get_parent(dev); disk->d_hba_vendor = pci_get_vendor(dev); disk->d_hba_device = pci_get_device(dev); disk->d_hba_subvendor = pci_get_subvendor(dev); disk->d_hba_subdevice = pci_get_subdevice(dev); disk->d_rotation_rate = DISK_RR_NON_ROTATING; strlcpy(disk->d_attachment, device_get_nameunit(dev), sizeof(disk->d_attachment)); disk_create(disk, DISK_VERSION); printf(NVD_STR"%u: <%s> NVMe namespace\n", disk->d_unit, descr); printf(NVD_STR"%u: %juMB (%ju %u byte sectors)\n", disk->d_unit, (uintmax_t)disk->d_mediasize / (1024*1024), (uintmax_t)disk->d_mediasize / disk->d_sectorsize, disk->d_sectorsize); return (ndisk); } static void nvd_controller_fail(void *ctrlr_arg) { struct nvd_controller *ctrlr = ctrlr_arg; struct nvd_disk *ndisk; mtx_lock(&nvd_lock); TAILQ_REMOVE(&ctrlr_head, ctrlr, tailq); TAILQ_FOREACH(ndisk, &ctrlr->disk_head, ctrlr_tailq) nvd_gone(ndisk); while (!TAILQ_EMPTY(&ctrlr->disk_head)) msleep(&ctrlr->disk_head, &nvd_lock, 0, "nvd_fail", 0); mtx_unlock(&nvd_lock); free(ctrlr, M_NVD); } diff --git a/sys/sys/devicestat.h b/sys/sys/devicestat.h index 1b8db6f100c9..2583697f7515 100644 --- a/sys/sys/devicestat.h +++ b/sys/sys/devicestat.h @@ -1,208 +1,209 @@ /*- * SPDX-License-Identifier: BSD-3-Clause * * Copyright (c) 1997, 1998, 1999 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. */ #ifndef _DEVICESTAT_H #define _DEVICESTAT_H #include #include /* * XXX: Should really be SPECNAMELEN */ #define DEVSTAT_NAME_LEN 16 /* * device name for the mmap device */ #define DEVSTAT_DEVICE_NAME "devstat" /* * ATTENTION: The devstat version below should be incremented any time a * change is made in struct devstat, or any time a change is made in the * enumerated types that struct devstat uses. (Only if those changes * would require a recompile -- i.e. re-arranging the order of an * enumerated type or something like that.) This version number is used by * userland utilities to determine whether or not they are in sync with the * kernel. */ #define DEVSTAT_VERSION 6 /* * These flags specify which statistics features are supported or not * supported by a particular device. The default is all statistics are * supported. */ typedef enum { DEVSTAT_ALL_SUPPORTED = 0x00, DEVSTAT_NO_BLOCKSIZE = 0x01, DEVSTAT_NO_ORDERED_TAGS = 0x02, DEVSTAT_BS_UNAVAILABLE = 0x04 } devstat_support_flags; typedef enum { DEVSTAT_NO_DATA = 0x00, DEVSTAT_READ = 0x01, DEVSTAT_WRITE = 0x02, DEVSTAT_FREE = 0x03 } devstat_trans_flags; #define DEVSTAT_N_TRANS_FLAGS 4 typedef enum { DEVSTAT_TAG_SIMPLE = 0x00, DEVSTAT_TAG_HEAD = 0x01, DEVSTAT_TAG_ORDERED = 0x02, DEVSTAT_TAG_NONE = 0x03 } devstat_tag_type; typedef enum { DEVSTAT_PRIORITY_MIN = 0x000, DEVSTAT_PRIORITY_OTHER = 0x020, DEVSTAT_PRIORITY_PASS = 0x030, DEVSTAT_PRIORITY_FD = 0x040, DEVSTAT_PRIORITY_WFD = 0x050, DEVSTAT_PRIORITY_TAPE = 0x060, DEVSTAT_PRIORITY_CD = 0x090, DEVSTAT_PRIORITY_DISK = 0x110, DEVSTAT_PRIORITY_ARRAY = 0x120, DEVSTAT_PRIORITY_MAX = 0xfff } devstat_priority; /* * These types are intended to aid statistics gathering/display programs. * The first 13 types (up to the 'target' flag) are identical numerically * to the SCSI device type numbers. The next 3 types designate the device * interface. Currently the choices are IDE, SCSI, and 'other'. The last * flag specifies whether or not the given device is a passthrough device * or not. If it is a passthrough device, the lower 4 bits specify which * type of physical device lies under the passthrough device, and the next * 4 bits specify the interface. */ typedef enum { DEVSTAT_TYPE_DIRECT = 0x000, DEVSTAT_TYPE_SEQUENTIAL = 0x001, DEVSTAT_TYPE_PRINTER = 0x002, DEVSTAT_TYPE_PROCESSOR = 0x003, DEVSTAT_TYPE_WORM = 0x004, DEVSTAT_TYPE_CDROM = 0x005, DEVSTAT_TYPE_SCANNER = 0x006, DEVSTAT_TYPE_OPTICAL = 0x007, DEVSTAT_TYPE_CHANGER = 0x008, DEVSTAT_TYPE_COMM = 0x009, DEVSTAT_TYPE_ASC0 = 0x00a, DEVSTAT_TYPE_ASC1 = 0x00b, DEVSTAT_TYPE_STORARRAY = 0x00c, DEVSTAT_TYPE_ENCLOSURE = 0x00d, DEVSTAT_TYPE_FLOPPY = 0x00e, DEVSTAT_TYPE_MASK = 0x00f, DEVSTAT_TYPE_IF_SCSI = 0x010, DEVSTAT_TYPE_IF_IDE = 0x020, DEVSTAT_TYPE_IF_OTHER = 0x030, + DEVSTAT_TYPE_IF_NVME = 0x040, DEVSTAT_TYPE_IF_MASK = 0x0f0, DEVSTAT_TYPE_PASS = 0x100 } devstat_type_flags; /* * XXX: Next revision should add * off_t offset[DEVSTAT_N_TRANS_FLAGS]; * XXX: which should contain the offset of the last completed transfer. */ struct devstat { /* Internal house-keeping fields */ u_int sequence0; /* Update sequence# */ int allocated; /* Allocated entry */ u_int start_count; /* started ops */ u_int end_count; /* completed ops */ struct bintime busy_from; /* * busy time unaccounted * for since this time */ STAILQ_ENTRY(devstat) dev_links; u_int32_t device_number; /* * Devstat device * number. */ char device_name[DEVSTAT_NAME_LEN]; int unit_number; u_int64_t bytes[DEVSTAT_N_TRANS_FLAGS]; u_int64_t operations[DEVSTAT_N_TRANS_FLAGS]; struct bintime duration[DEVSTAT_N_TRANS_FLAGS]; struct bintime busy_time; struct bintime creation_time; /* * Time the device was * created. */ u_int32_t block_size; /* Block size, bytes */ u_int64_t tag_types[3]; /* * The number of * simple, ordered, * and head of queue * tags sent. */ devstat_support_flags flags; /* * Which statistics * are supported by a * given device. */ devstat_type_flags device_type; /* Device type */ devstat_priority priority; /* Controls list pos. */ const void *id; /* * Identification for * GEOM nodes */ u_int sequence1; /* Update sequence# */ }; STAILQ_HEAD(devstatlist, devstat); #ifdef _KERNEL struct bio; struct devstat *devstat_new_entry(const void *dev_name, int unit_number, u_int32_t block_size, devstat_support_flags flags, devstat_type_flags device_type, devstat_priority priority); void devstat_remove_entry(struct devstat *ds); void devstat_start_transaction(struct devstat *ds, const struct bintime *now); void devstat_start_transaction_bio(struct devstat *ds, struct bio *bp); void devstat_start_transaction_bio_t0(struct devstat *ds, struct bio *bp); void devstat_end_transaction(struct devstat *ds, u_int32_t bytes, devstat_tag_type tag_type, devstat_trans_flags flags, const struct bintime *now, const struct bintime *then); void devstat_end_transaction_bio(struct devstat *ds, const struct bio *bp); void devstat_end_transaction_bio_bt(struct devstat *ds, const struct bio *bp, const struct bintime *now); #endif #endif /* _DEVICESTAT_H */ diff --git a/usr.bin/vmstat/vmstat.8 b/usr.bin/vmstat/vmstat.8 index d1f0d4d60bcc..eead7a7ef9c3 100644 --- a/usr.bin/vmstat/vmstat.8 +++ b/usr.bin/vmstat/vmstat.8 @@ -1,394 +1,396 @@ .\" Copyright (c) 1986, 1993 .\" The Regents of the University of California. 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. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. .\" .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. .\" .\" @(#)vmstat.8 8.1 (Berkeley) 6/6/93 .\" .Dd June 21, 2021 .Dt VMSTAT 8 .Os .Sh NAME .Nm vmstat .Nd report virtual memory statistics .Sh SYNOPSIS .Nm .\" .Op Fl fimst .Op Fl -libxo .Op Fl afHhimoPsz .Op Fl M Ar core Op Fl N Ar system .Op Fl c Ar count .Op Fl n Ar devs .Oo .Fl p .Sm off .Ar type , if , pass .Sm on .Oc .Op Fl w Ar wait .Op Ar disks ... .Op wait Op count .Sh DESCRIPTION The .Nm utility reports certain kernel statistics kept about process, virtual memory, disk, trap and cpu activity. .Pp If the .Fl M option is not specified, information is obtained from the currently running kernel via the .Xr sysctl 3 interface. Otherwise, information is read from the specified core file, using the name list from the specified kernel image (or from the default image). .Pp The options are as follows: .Bl -tag -width indent .It Fl -libxo Generate output via .Xr libxo 3 in a selection of different human and machine readable formats. See .Xr xo_parse_args 3 for details on command line arguments. .It Fl a When used with .Fl i , include statistics about interrupts that have never been generated. .It Fl c Repeat the display .Ar count times. The first display is for the time since a reboot and each subsequent report is for the time period since the last display. If no repeat .Ar count is specified, and .Fl w is specified, the default is infinity, otherwise the default is one. .It Fl f Report on the number .Xr fork 2 , .Xr vfork 2 and .Xr rfork 2 system calls since system startup, and the number of pages of virtual memory involved in each. .It Fl h Changes memory columns into more easily human readable form. The default if standard output is a terminal device. .It Fl H Changes memory columns into straight numbers. The default if standard output is not a terminal device (such as a script). .It Fl i Report on the number of interrupts taken by each device since system startup. .It Fl M Extract values associated with the name list from the specified .Ar core . .It Fl N If .Fl M is also specified, extract the name list from the specified .Ar system instead of the default, which is the kernel image the system has booted from. .It Fl m Report on the usage of kernel dynamic memory allocated using .Xr malloc 9 by type. .It Fl n Change the maximum number of disks to display from the default of 2. .It Fl o Display a list of virtual memory objects in the system and the resident memory used by each object. .It Fl P Report per-cpu system/user/idle cpu statistics. .It Fl p Specify which types of devices to display. There are three different categories of devices: .Pp .Bl -tag -width indent -compact .It device type: .Bl -tag -width 9n -compact .It da Direct Access devices .It sa Sequential Access devices .It printer Printers .It proc Processor devices .It worm Write Once Read Multiple devices .It cd CD devices .It scanner Scanner devices .It optical Optical Memory devices .It changer Medium Changer devices .It comm Communication devices .It array Storage Array devices .It enclosure Enclosure Services devices .It floppy Floppy devices .El .Pp .It interface: .Bl -tag -width 9n -compact .It IDE Integrated Drive Electronics devices .It SCSI Small Computer System Interface devices +.It NVME +NVM Express Interface devices .It other Any other device interface .El .Pp .It passthrough: .Bl -tag -width 9n -compact .It pass Passthrough devices .El .El .Pp The user must specify at least one device type, and may specify at most one device type from each category. Multiple device types in a single device type statement must be separated by commas. .Pp Any number of .Fl p arguments may be specified on the command line. All .Fl p arguments are ORed together to form a matching expression against which all devices in the system are compared. Any device that fully matches any .Fl p argument will be included in the .Nm output, up to two devices, or the maximum number of devices specified by the user. .It Fl s Display the contents of the .Em sum structure, giving the total number of several kinds of paging related events which have occurred since system startup. .It Fl w Pause .Ar wait seconds between each display. If no repeat .Ar wait interval is specified, the default is 1 second. The .Nm command will accept and honor a non-integer number of seconds. .It Fl z Report on memory used by the kernel zone allocator, .Xr uma 9 , by zone. .El .Pp The .Ar wait and .Ar count arguments may be given after their respective flags at any point on the command line before the .Ar disks argument(s), or without their flags, as the final argument(s). The latter form is accepted for backwards compatibility, but it is preferred to use the forms with .Fl w and .Fl c to avoid ambiguity. .Pp By default, .Nm displays the following information: .Bl -tag -width indent .It procs Information about the number of threads in various states: .Pp .Bl -tag -width indent -compact .It r running or in run queue .It b blocked for resources (i/o, paging, etc.) .It w swapped out .El .It memory Information about the usage of virtual and real memory. .Pp Mapped virtual memory is a sum of all of the virtual pages belonging to mapped virtual memory objects. Note that the entire memory object's size is considered mapped even if only a subset of the object's pages are currently mapped. This statistic is not related to the active page queue which is used to track real memory. .Pp .Bl -tag -width indent -compact .It avm mapped virtual memory .Po previously called active in .Nm output .Pc .It fre size of the free list .El .It page Information about page faults and paging activity. These are given in units per second. .Pp .Bl -tag -width indent -compact .It flt total number of page faults .It re pages reactivated (found in laundry or inactive queues) .\" .It at .\" pages attached (found in free list) .It pi pages paged in .It po pages paged out .It fr pages freed .\" .It de .\" anticipated short term memory shortfall .It sr pages scanned by page daemon .El .It disks Disk operations per second (this field is system dependent). Typically paging will be split across the available drives. The header of the field is the first two characters of the disk name and the unit number. If more than two disk drives are configured in the system, .Nm displays only the first two drives, unless the user specifies the .Fl n argument to increase the number of drives displayed. This will probably cause the display to exceed 80 columns, however. To force .Nm to display specific drives, their names may be supplied on the command line. The .Nm utility defaults to show disks first, and then various other random devices in the system to add up to two devices, if there are that many devices in the system. If devices are specified on the command line, or if a device type matching pattern is specified (see above), .Nm will only display the given devices or the devices matching the pattern, and will not randomly select other devices in the system. .It faults Trap/interrupt rates per second. .Pp .Bl -tag -width indent -compact .It in device interrupts (including clock interrupts) .It sy system calls .It cs cpu context switches .El .It cpu Breakdown of percentage usage of CPU time. .Pp .Bl -tag -width indent -compact .It us user time for normal and low priority processes .It sy system and interrupt time .It id cpu idle .El .El .Sh FILES .Bl -tag -width /boot/kernel/kernel -compact .It Pa /boot/kernel/kernel default kernel namelist .It Pa /dev/kmem default memory file .El .Sh EXAMPLES The command: .Dl vmstat -w 5 will print what the system is doing every five seconds. .Pp The command: .Dl vmstat -p da -p cd -w 1 will tell vmstat to select the first two direct access or CDROM devices and display statistics on those devices, as well as other systems statistics every second. .Sh SEE ALSO .Xr fstat 1 , .Xr netstat 1 , .Xr nfsstat 1 , .Xr ps 1 , .Xr systat 1 , .Xr libmemstat 3 , .Xr libxo 3 , .Xr xo_parse_args 3 , .Xr gstat 8 , .Xr iostat 8 , .Xr pstat 8 , .Xr sysctl 8 , .Xr malloc 9 , .Xr uma 9 .Pp The sections starting with ``Interpreting system activity'' in .%T "Installing and Operating 4.3BSD" . .Sh HISTORY The .Nm utility first appeared in .Bx 3 . .Sh BUGS The .Fl c and .Fl w options are only available with the default output. diff --git a/usr.sbin/iostat/iostat.8 b/usr.sbin/iostat/iostat.8 index 863f8ab8466c..a08a62dc694d 100644 --- a/usr.sbin/iostat/iostat.8 +++ b/usr.sbin/iostat/iostat.8 @@ -1,518 +1,520 @@ .\" .\" Copyright (c) 1997 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. .\" .\" Copyright (c) 1985, 1991, 1993 .\" The Regents of the University of California. 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. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. .\" .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. .\" .\" @(#)iostat.8 8.1 (Berkeley) 6/6/93 .\" .Dd August 29, 2023 .Dt IOSTAT 8 .Os .Sh NAME .Nm iostat .Nd report .Tn I/O statistics .Sh SYNOPSIS .Nm .Op Fl CdhIKoTxz .Op Fl c Ar count .Op Fl M Ar core .Op Fl n Ar devs .Op Fl N Ar system .Oo .Fl t .Sm off .Ar type , if , pass .Sm on .Oc .Op Fl w Ar wait .Op Ar drives .Sh DESCRIPTION The .Nm utility displays kernel .Tn I/O statistics on terminal, device and cpu operations. The first statistics that are printed are averaged over the system uptime. To get information about the current activity, a suitable wait time should be specified, so that the subsequent sets of printed statistics will be averaged over that time. .Pp The options are as follows: .Bl -tag -width flag .It Fl C Display CPU statistics. This is on by default, unless .Fl d or .Fl x is specified. .It Fl c Repeat the display .Ar count times. If no repeat .Ar count is specified, the default depends on whether .Fl w is specified. With .Fl w the default repeat count is infinity, otherwise it is 1. .It Fl d Display only device statistics. If this flag is turned on, only device statistics will be displayed, unless .Fl C or .Fl T is also specified to enable the display of CPU or TTY statistics. .It Fl h Put .Nm in .Sq top mode. In this mode, .Nm will show devices in order from highest to lowest bytes per measurement cycle. .It Fl I Display total statistics for a given time period, rather than average statistics for each second during that time period. .It Fl K In the blocks transferred display (-o), display block count in kilobytes rather then the device native block size. .It Fl M Extract values associated with the name list from the specified core instead of the default .Dq Pa /dev/kmem . .It Fl N Extract the name list from the specified system instead of the default .Dq Pa /boot/kernel/kernel . .It Fl n Display up to .Ar devs number of devices. The .Nm utility will display fewer devices if there are not .Ar devs devices present. .It Fl o Display old-style .Nm device statistics. Sectors per second, transfers per second, and milliseconds per seek are displayed. If .Fl I is specified, total blocks/sectors, total transfers, and milliseconds per seek are displayed. .It Fl T Display TTY statistics. This is on by default, unless .Fl d or .Fl x is specified. .It Fl t Specify which types of devices to display. There are three different categories of devices: .Pp .Bl -tag -width indent -compact .It device type: .Bl -tag -width 9n -compact .It da Direct Access devices .It sa Sequential Access devices .It printer Printers .It proc Processor devices .It worm Write Once Read Multiple devices .It cd CD devices .It scanner Scanner devices .It optical Optical Memory devices .It changer Medium Changer devices .It comm Communication devices .It array Storage Array devices .It enclosure Enclosure Services devices .It floppy Floppy devices .El .Pp .It interface: .Bl -tag -width 9n -compact .It IDE Integrated Drive Electronics devices .It SCSI Small Computer System Interface devices +.It NVME +NVM Express Interface devices .It other Any other device interface .El .Pp .It passthrough: .Bl -tag -width 9n -compact .It pass Passthrough devices .El .El .Pp The user must specify at least one device type, and may specify at most one device type from each category. Multiple device types in a single device type statement must be separated by commas. .Pp Any number of .Fl t arguments may be specified on the command line. All .Fl t arguments are ORed together to form a matching expression against which all devices in the system are compared. Any device that fully matches any .Fl t argument will be included in the .Nm output, up to the number of devices that can be displayed in 80 columns, or the maximum number of devices specified by the user. .It Fl w Pause .Ar wait seconds between each display. If no .Ar wait interval is specified, the default is 1 second. .Pp The .Nm command will accept and honor a non-integer number of seconds. Note that the interval only has millisecond granularity. Finer values will be truncated. E.g., .Dq Li -w1.0001 is the same as .Dq Li -w1.000 . The interval will also suffer from modifications to .Va kern.hz so your mileage may vary. .It Fl x Show extended disk statistics. Each disk is displayed on a line of its own with all available statistics. If this flag is turned on, only disk statistics will be displayed, unless .Fl C or .Fl T is also specified to enable the display of CPU or TTY statistics. .It Fl z If .Fl x is specified, omit lines for devices with no activity. .El .Pp The .Nm utility displays its information in the following format: .Bl -tag -width flag .It tty .Bl -tag -width indent -compact .It tin characters read from terminals .It tout characters written to terminals .El .It devices Device operations. The header of the field is the device name and unit number. The .Nm utility will display as many devices as will fit in a standard 80 column screen, or the maximum number of devices in the system, whichever is smaller. If .Fl n is specified on the command line, .Nm will display the smaller of the requested number of devices, and the maximum number of devices in the system. To force .Nm to display specific drives, their names may be supplied on the command line. The .Nm utility will not display more devices than will fit in an 80 column screen, unless the .Fl n argument is given on the command line to specify a maximum number of devices to display. If fewer devices are specified on the command line than will fit in an 80 column screen, .Nm will show only the specified devices. .Pp The standard .Nm device display shows the following statistics: .Pp .Bl -tag -width indent -compact .It KB/t kilobytes per transfer .It tps transfers per second .It MB/s megabytes per second .El .Pp The standard .Nm device display, with the .Fl I flag specified, shows the following statistics: .Pp .Bl -tag -width indent -compact .It KB/t kilobytes per transfer .It xfrs total number of transfers .It MB total number of megabytes transferred .El .Pp The extended .Nm device display, with the .Fl x flag specified, shows the following statistics: .Pp .Bl -tag -width indent -compact .It r/s read operations per second .It w/s write operations per second .It kr/s kilobytes read per second .It kw/s kilobytes write per second .It qlen transactions queue length .It ms/r average duration of read transactions, in milliseconds .It ms/w average duration of write transactions, in milliseconds .It ms/o average duration of all other transactions, in milliseconds .It ms/t average duration of all transactions, in milliseconds .It %b % of time the device had one or more outstanding transactions .El .Pp The extended .Nm device display, with the .Fl x and .Fl I flags specified, shows the following statistics: .Pp .Bl -tag -width indent -compact .It r/i read operations per time period .It w/i write operations per time period .It kr/i kilobytes read per time period .It kw/i kilobytes write per time period .It qlen transactions queue length .It tsvc_t/i total duration of transactions per time period, in seconds .It sb/i total time the device had one or more outstanding transactions per time period, in seconds .El .Pp The old-style .Nm display (using .Fl o ) shows the following statistics: .Pp .Bl -tag -width indent -compact .It sps sectors transferred per second .It tps transfers per second .It msps average milliseconds per transaction .El .Pp The old-style .Nm display, with the .Fl I flag specified, shows the following statistics: .Pp .Bl -tag -width indent -compact .It blk total blocks/sectors transferred .It xfr total transfers .It msps average milliseconds per transaction .El .It cpu .Bl -tag -width indent -compact .It \&us % of cpu time in user mode .It \&ni % of cpu time in user mode running niced processes .It \&sy % of cpu time in system mode .It \&in % of cpu time in interrupt mode .It \&id % of cpu time in idle mode .El .El .Sh FILES .Bl -tag -width /boot/kernel/kernel -compact .It Pa /boot/kernel/kernel Default kernel namelist. .It Pa /dev/kmem Default memory file. .El .Sh EXAMPLES .Dl iostat -w 1 da0 da1 cd0 .Pp Display statistics for the first two Direct Access devices and the first CDROM device every second ad infinitum. .Pp .Dl iostat -c 2 .Pp Display the statistics for the first four devices in the system twice, with a one second display interval. .Pp .Dl iostat -t da -t cd -w 1 .Pp Display statistics for all CDROM and Direct Access devices every second ad infinitum. .Pp .Dl iostat -t da,scsi,pass -t cd,scsi,pass .Pp Display statistics once for all SCSI passthrough devices that provide access to either Direct Access or CDROM devices. .Pp .Dl iostat -h -n 8 -w 1 .Pp Display up to 8 devices with the most I/O every second ad infinitum. .Pp .Dl iostat -dh -t da -w 1 .Pp Omit the TTY and CPU displays, show devices in order of performance and show only Direct Access devices every second ad infinitum. .Pp .Dl iostat -Iw 3 .Pp Display total statistics every three seconds ad infinitum. .Pp .Dl iostat -odICTw 2 -c 9 .Pp Display total statistics using the old-style output format 9 times, with a two second interval between each measurement/display. The .Fl d flag generally disables the TTY and CPU displays, but since the .Fl T and .Fl C flags are given, the TTY and CPU displays will be displayed. .Sh SEE ALSO .Xr fstat 1 , .Xr netstat 1 , .Xr nfsstat 1 , .Xr ps 1 , .Xr systat 1 , .Xr devstat 3 , .Xr ctlstat 8 , .Xr gstat 8 , .Xr pstat 8 , .Xr vmstat 8 .Pp The sections starting with ``Interpreting system activity'' in .%T "Installing and Operating 4.3BSD" . .Sh HISTORY This version of .Nm first appeared in .Fx 3.0 . .Sh AUTHORS .An Kenneth Merry Aq Mt ken@FreeBSD.org .Sh BUGS The use of .Nm as a debugging tool for crash dumps is probably limited because there is currently no way to get statistics that only cover the time immediately before the crash.