1da739265SWarner Losh /*-
2da739265SWarner Losh * Copyright (c) 2021 Netflix, Inc.
3da739265SWarner Losh *
4da739265SWarner Losh * Redistribution and use in source and binary forms, with or without
5da739265SWarner Losh * modification, are permitted provided that the following conditions
6da739265SWarner Losh * are met:
7da739265SWarner Losh * 1. Redistributions of source code must retain the above copyright
8da739265SWarner Losh * notice, this list of conditions and the following disclaimer.
9da739265SWarner Losh * 2. Redistributions in binary form must reproduce the above copyright
10da739265SWarner Losh * notice, this list of conditions and the following disclaimer in the
11da739265SWarner Losh * documentation and/or other materials provided with the distribution.
12da739265SWarner Losh *
13da739265SWarner Losh * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14da739265SWarner Losh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15da739265SWarner Losh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16da739265SWarner Losh * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17da739265SWarner Losh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18da739265SWarner Losh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19da739265SWarner Losh * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20da739265SWarner Losh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21da739265SWarner Losh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22da739265SWarner Losh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23da739265SWarner Losh * SUCH DAMAGE.
24da739265SWarner Losh */
25da739265SWarner Losh
26da739265SWarner Losh /*
27da739265SWarner Losh * Wrapper functions to make requests and get answers w/o managing the
28da739265SWarner Losh * details.
29da739265SWarner Losh */
30da739265SWarner Losh
31da739265SWarner Losh #include <sys/types.h>
32da739265SWarner Losh
33da739265SWarner Losh #include <err.h>
34da739265SWarner Losh #include <stdio.h>
35da739265SWarner Losh #include <stdlib.h>
36da739265SWarner Losh #include <strings.h>
37da739265SWarner Losh
38da739265SWarner Losh #include <cam/cam.h>
39da739265SWarner Losh #include <cam/cam_ccb.h>
40da739265SWarner Losh #include <cam/scsi/scsi_all.h>
41da739265SWarner Losh #include <cam/scsi/scsi_pass.h>
42da739265SWarner Losh #include <cam/scsi/scsi_message.h>
43da739265SWarner Losh #include "camlib.h"
44da739265SWarner Losh #include "scsi_wrap.h"
45da739265SWarner Losh
46da739265SWarner Losh void *
scsi_wrap_get_physical_element_status(struct cam_device * device,int task_attr,int retry_count,int timeout,uint8_t report_type,uint32_t start_element)47da739265SWarner Losh scsi_wrap_get_physical_element_status(struct cam_device *device, int task_attr, int retry_count,
48da739265SWarner Losh int timeout, uint8_t report_type, uint32_t start_element)
49da739265SWarner Losh {
50da739265SWarner Losh uint32_t allocation_length;
51da739265SWarner Losh union ccb *ccb = NULL;
52da739265SWarner Losh struct scsi_get_physical_element_hdr *hdr = NULL;
53da739265SWarner Losh uint32_t dtors;
54da739265SWarner Losh uint32_t reported;
55da739265SWarner Losh
56da739265SWarner Losh ccb = cam_getccb(device);
57da739265SWarner Losh if (ccb == NULL) {
58da739265SWarner Losh warnx("Can't allocate ccb");
59da739265SWarner Losh return (NULL);
60da739265SWarner Losh }
61da739265SWarner Losh
62da739265SWarner Losh /*
63da739265SWarner Losh * Do the request up to twice. Once to get the length and once to get
64da739265SWarner Losh * the data. We'll guess that 4096 is enough to almost always long
65da739265SWarner Losh * enough since that's 127 entries and most drives have < 20 heads. If
66da739265SWarner Losh * by chance it's not, then we'll loop once as we'll then know the
67da739265SWarner Losh * proper length.
68da739265SWarner Losh */
69da739265SWarner Losh allocation_length = MAX(sizeof(*hdr), 4096);
70da739265SWarner Losh again:
71da739265SWarner Losh free(hdr);
72da739265SWarner Losh hdr = calloc(allocation_length, 1);
73da739265SWarner Losh if (hdr == NULL) {
74da739265SWarner Losh warnx("Can't allocate memory for physical element list");
75da739265SWarner Losh return (NULL);
76da739265SWarner Losh }
77da739265SWarner Losh
78da739265SWarner Losh scsi_get_physical_element_status(&ccb->csio,
79da739265SWarner Losh retry_count,
80da739265SWarner Losh NULL,
81da739265SWarner Losh task_attr,
82da739265SWarner Losh (uint8_t *)hdr,
83da739265SWarner Losh allocation_length,
84da739265SWarner Losh report_type,
85da739265SWarner Losh start_element,
86da739265SWarner Losh SSD_FULL_SIZE,
87da739265SWarner Losh timeout);
88da739265SWarner Losh
89da739265SWarner Losh /* Disable freezing the device queue */
90da739265SWarner Losh ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
91da739265SWarner Losh
92da739265SWarner Losh if (cam_send_ccb(device, ccb) < 0) {
93da739265SWarner Losh warn("error sending GET PHYSICAL ELEMENT STATUS command");
94da739265SWarner Losh goto errout;
95da739265SWarner Losh }
96da739265SWarner Losh
97da739265SWarner Losh if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
98da739265SWarner Losh cam_error_print(device, ccb, CAM_ESF_ALL,
99da739265SWarner Losh CAM_EPF_ALL, stderr);
100da739265SWarner Losh goto errout;
101da739265SWarner Losh }
102da739265SWarner Losh
103da739265SWarner Losh dtors = scsi_4btoul(hdr->num_descriptors);
104da739265SWarner Losh reported = scsi_4btoul(hdr->num_returned);
105da739265SWarner Losh if (dtors != 0 && dtors != reported) {
106da739265SWarner Losh /*
107da739265SWarner Losh * Get all the data... in the future we may need to step through
108da739265SWarner Losh * a long list, but so far all drives I've found fit into one
109da739265SWarner Losh * response. A 4k transfer can do 128 heads and current designs
110da739265SWarner Losh * have 16.
111da739265SWarner Losh */
112da739265SWarner Losh allocation_length = dtors * sizeof(struct scsi_get_physical_element_descriptor) +
113da739265SWarner Losh sizeof(*hdr);
114da739265SWarner Losh goto again;
115da739265SWarner Losh }
116da739265SWarner Losh cam_freeccb(ccb);
117da739265SWarner Losh return (hdr);
118da739265SWarner Losh errout:
119da739265SWarner Losh cam_freeccb(ccb);
120da739265SWarner Losh free(hdr);
121da739265SWarner Losh return (NULL);
122da739265SWarner Losh }
123da739265SWarner Losh
124da739265SWarner Losh void *
scsi_wrap_inquiry(struct cam_device * device,uint32_t page,uint32_t length)125da739265SWarner Losh scsi_wrap_inquiry(struct cam_device *device, uint32_t page, uint32_t length)
126da739265SWarner Losh {
127da739265SWarner Losh union ccb *ccb;
128da739265SWarner Losh uint8_t *buf;
129da739265SWarner Losh
130da739265SWarner Losh ccb = cam_getccb(device);
131da739265SWarner Losh
132da739265SWarner Losh if (ccb == NULL)
133da739265SWarner Losh return (NULL);
134da739265SWarner Losh
135da739265SWarner Losh buf = malloc(length);
136da739265SWarner Losh
137da739265SWarner Losh if (buf == NULL) {
138da739265SWarner Losh cam_freeccb(ccb);
139da739265SWarner Losh return (NULL);
140da739265SWarner Losh }
141da739265SWarner Losh
142da739265SWarner Losh scsi_inquiry(&ccb->csio,
143da739265SWarner Losh /*retries*/ 0,
144da739265SWarner Losh /*cbfcnp*/ NULL,
145da739265SWarner Losh /* tag_action */ MSG_SIMPLE_Q_TAG,
146*f9ffa1efSWarner Losh /* inq_buf */ (uint8_t *)buf,
147da739265SWarner Losh /* inq_len */ length,
148da739265SWarner Losh /* evpd */ 1,
149da739265SWarner Losh /* page_code */ page,
150da739265SWarner Losh /* sense_len */ SSD_FULL_SIZE,
151da739265SWarner Losh /* timeout */ 5000);
152da739265SWarner Losh
153da739265SWarner Losh /* Disable freezing the device queue */
154da739265SWarner Losh ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
155da739265SWarner Losh // ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
156da739265SWarner Losh
157da739265SWarner Losh if (cam_send_ccb(device, ccb) < 0) {
158da739265SWarner Losh warn("error sending INQUIRY command");
159da739265SWarner Losh cam_freeccb(ccb);
160da739265SWarner Losh free(buf);
161da739265SWarner Losh return (NULL);
162da739265SWarner Losh }
163da739265SWarner Losh
164da739265SWarner Losh if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
165da739265SWarner Losh free(buf);
166da739265SWarner Losh buf = NULL;
167da739265SWarner Losh }
168da739265SWarner Losh cam_freeccb(ccb);
169da739265SWarner Losh return (buf);
170da739265SWarner Losh }
171da739265SWarner Losh
172da739265SWarner Losh struct scsi_vpd_block_device_characteristics *
scsi_wrap_vpd_block_device_characteristics(struct cam_device * device)173da739265SWarner Losh scsi_wrap_vpd_block_device_characteristics(struct cam_device *device)
174da739265SWarner Losh {
175da739265SWarner Losh
176da739265SWarner Losh return ((struct scsi_vpd_block_device_characteristics *)scsi_wrap_inquiry(
177da739265SWarner Losh device, SVPD_BDC, sizeof(struct scsi_vpd_block_device_characteristics)));
178da739265SWarner Losh }
179