2 * Copyright (c) 2021 Netflix, Inc.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * Wrapper functions to make requests and get answers w/o managing the
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/types.h>
42 #include <cam/cam_ccb.h>
43 #include <cam/scsi/scsi_all.h>
44 #include <cam/scsi/scsi_pass.h>
45 #include <cam/scsi/scsi_message.h>
47 #include "scsi_wrap.h"
50 scsi_wrap_get_physical_element_status(struct cam_device *device, int task_attr, int retry_count,
51 int timeout, uint8_t report_type, uint32_t start_element)
53 uint32_t allocation_length;
54 union ccb *ccb = NULL;
55 struct scsi_get_physical_element_hdr *hdr = NULL;
59 ccb = cam_getccb(device);
61 warnx("Can't allocate ccb");
66 * Do the request up to twice. Once to get the length and once to get
67 * the data. We'll guess that 4096 is enough to almost always long
68 * enough since that's 127 entries and most drives have < 20 heads. If
69 * by chance it's not, then we'll loop once as we'll then know the
72 allocation_length = MAX(sizeof(*hdr), 4096);
75 hdr = calloc(allocation_length, 1);
77 warnx("Can't allocate memory for physical element list");
81 scsi_get_physical_element_status(&ccb->csio,
92 /* Disable freezing the device queue */
93 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
95 if (cam_send_ccb(device, ccb) < 0) {
96 warn("error sending GET PHYSICAL ELEMENT STATUS command");
100 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
101 cam_error_print(device, ccb, CAM_ESF_ALL,
102 CAM_EPF_ALL, stderr);
106 dtors = scsi_4btoul(hdr->num_descriptors);
107 reported = scsi_4btoul(hdr->num_returned);
108 if (dtors != 0 && dtors != reported) {
110 * Get all the data... in the future we may need to step through
111 * a long list, but so far all drives I've found fit into one
112 * response. A 4k transfer can do 128 heads and current designs
115 allocation_length = dtors * sizeof(struct scsi_get_physical_element_descriptor) +
128 scsi_wrap_inquiry(struct cam_device *device, uint32_t page, uint32_t length)
133 ccb = cam_getccb(device);
138 buf = malloc(length);
145 scsi_inquiry(&ccb->csio,
148 /* tag_action */ MSG_SIMPLE_Q_TAG,
149 /* inq_buf */ (uint8_t *)buf,
150 /* inq_len */ length,
152 /* page_code */ page,
153 /* sense_len */ SSD_FULL_SIZE,
156 /* Disable freezing the device queue */
157 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
158 // ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
160 if (cam_send_ccb(device, ccb) < 0) {
161 warn("error sending INQUIRY command");
167 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
175 struct scsi_vpd_block_device_characteristics *
176 scsi_wrap_vpd_block_device_characteristics(struct cam_device *device)
179 return ((struct scsi_vpd_block_device_characteristics *)scsi_wrap_inquiry(
180 device, SVPD_BDC, sizeof(struct scsi_vpd_block_device_characteristics)));