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 #include <sys/types.h>
40 #include <cam/cam_ccb.h>
41 #include <cam/scsi/scsi_all.h>
42 #include <cam/scsi/scsi_pass.h>
43 #include <cam/scsi/scsi_message.h>
45 #include "scsi_wrap.h"
48 scsi_wrap_get_physical_element_status(struct cam_device *device, int task_attr, int retry_count,
49 int timeout, uint8_t report_type, uint32_t start_element)
51 uint32_t allocation_length;
52 union ccb *ccb = NULL;
53 struct scsi_get_physical_element_hdr *hdr = NULL;
57 ccb = cam_getccb(device);
59 warnx("Can't allocate ccb");
64 * Do the request up to twice. Once to get the length and once to get
65 * the data. We'll guess that 4096 is enough to almost always long
66 * enough since that's 127 entries and most drives have < 20 heads. If
67 * by chance it's not, then we'll loop once as we'll then know the
70 allocation_length = MAX(sizeof(*hdr), 4096);
73 hdr = calloc(allocation_length, 1);
75 warnx("Can't allocate memory for physical element list");
79 scsi_get_physical_element_status(&ccb->csio,
90 /* Disable freezing the device queue */
91 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
93 if (cam_send_ccb(device, ccb) < 0) {
94 warn("error sending GET PHYSICAL ELEMENT STATUS command");
98 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
99 cam_error_print(device, ccb, CAM_ESF_ALL,
100 CAM_EPF_ALL, stderr);
104 dtors = scsi_4btoul(hdr->num_descriptors);
105 reported = scsi_4btoul(hdr->num_returned);
106 if (dtors != 0 && dtors != reported) {
108 * Get all the data... in the future we may need to step through
109 * a long list, but so far all drives I've found fit into one
110 * response. A 4k transfer can do 128 heads and current designs
113 allocation_length = dtors * sizeof(struct scsi_get_physical_element_descriptor) +
126 scsi_wrap_inquiry(struct cam_device *device, uint32_t page, uint32_t length)
131 ccb = cam_getccb(device);
136 buf = malloc(length);
143 scsi_inquiry(&ccb->csio,
146 /* tag_action */ MSG_SIMPLE_Q_TAG,
147 /* inq_buf */ (uint8_t *)buf,
148 /* inq_len */ length,
150 /* page_code */ page,
151 /* sense_len */ SSD_FULL_SIZE,
154 /* Disable freezing the device queue */
155 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
156 // ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
158 if (cam_send_ccb(device, ccb) < 0) {
159 warn("error sending INQUIRY command");
165 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
173 struct scsi_vpd_block_device_characteristics *
174 scsi_wrap_vpd_block_device_characteristics(struct cam_device *device)
177 return ((struct scsi_vpd_block_device_characteristics *)scsi_wrap_inquiry(
178 device, SVPD_BDC, sizeof(struct scsi_vpd_block_device_characteristics)));