xref: /freebsd/lib/libcam/scsi_wrap.c (revision f9ffa1ef25cff69f3aa090752430b2b16d1dc7a6)
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/cdefs.h>
32da739265SWarner Losh __FBSDID("$FreeBSD$");
33da739265SWarner Losh 
34da739265SWarner Losh #include <sys/types.h>
35da739265SWarner Losh 
36da739265SWarner Losh #include <err.h>
37da739265SWarner Losh #include <stdio.h>
38da739265SWarner Losh #include <stdlib.h>
39da739265SWarner Losh #include <strings.h>
40da739265SWarner Losh 
41da739265SWarner Losh #include <cam/cam.h>
42da739265SWarner Losh #include <cam/cam_ccb.h>
43da739265SWarner Losh #include <cam/scsi/scsi_all.h>
44da739265SWarner Losh #include <cam/scsi/scsi_pass.h>
45da739265SWarner Losh #include <cam/scsi/scsi_message.h>
46da739265SWarner Losh #include "camlib.h"
47da739265SWarner Losh #include "scsi_wrap.h"
48da739265SWarner Losh 
49da739265SWarner Losh void *
50da739265SWarner Losh scsi_wrap_get_physical_element_status(struct cam_device *device, int task_attr, int retry_count,
51da739265SWarner Losh     int timeout, uint8_t report_type, uint32_t start_element)
52da739265SWarner Losh {
53da739265SWarner Losh 	uint32_t allocation_length;
54da739265SWarner Losh 	union ccb *ccb = NULL;
55da739265SWarner Losh 	struct scsi_get_physical_element_hdr *hdr = NULL;
56da739265SWarner Losh 	uint32_t dtors;
57da739265SWarner Losh 	uint32_t reported;
58da739265SWarner Losh 
59da739265SWarner Losh 	ccb = cam_getccb(device);
60da739265SWarner Losh 	if (ccb == NULL) {
61da739265SWarner Losh 		warnx("Can't allocate ccb");
62da739265SWarner Losh 		return (NULL);
63da739265SWarner Losh 	}
64da739265SWarner Losh 
65da739265SWarner Losh 	/*
66da739265SWarner Losh 	 * Do the request up to twice. Once to get the length and once to get
67da739265SWarner Losh 	 * the data. We'll guess that 4096 is enough to almost always long
68da739265SWarner Losh 	 * enough since that's 127 entries and most drives have < 20 heads.  If
69da739265SWarner Losh 	 * by chance it's not, then we'll loop once as we'll then know the
70da739265SWarner Losh 	 * proper length.
71da739265SWarner Losh 	 */
72da739265SWarner Losh 	allocation_length = MAX(sizeof(*hdr), 4096);
73da739265SWarner Losh again:
74da739265SWarner Losh 	free(hdr);
75da739265SWarner Losh 	hdr = calloc(allocation_length, 1);
76da739265SWarner Losh 	if (hdr == NULL) {
77da739265SWarner Losh 		warnx("Can't allocate memory for physical element list");
78da739265SWarner Losh 		return (NULL);
79da739265SWarner Losh 	}
80da739265SWarner Losh 
81da739265SWarner Losh 	scsi_get_physical_element_status(&ccb->csio,
82da739265SWarner Losh 	    retry_count,
83da739265SWarner Losh 	    NULL,
84da739265SWarner Losh 	    task_attr,
85da739265SWarner Losh 	    (uint8_t *)hdr,
86da739265SWarner Losh 	    allocation_length,
87da739265SWarner Losh 	    report_type,
88da739265SWarner Losh 	    start_element,
89da739265SWarner Losh 	    SSD_FULL_SIZE,
90da739265SWarner Losh 	    timeout);
91da739265SWarner Losh 
92da739265SWarner Losh 	/* Disable freezing the device queue */
93da739265SWarner Losh 	ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
94da739265SWarner Losh 
95da739265SWarner Losh 	if (cam_send_ccb(device, ccb) < 0) {
96da739265SWarner Losh 		warn("error sending GET PHYSICAL ELEMENT STATUS command");
97da739265SWarner Losh 		goto errout;
98da739265SWarner Losh 	}
99da739265SWarner Losh 
100da739265SWarner Losh 	if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
101da739265SWarner Losh 		cam_error_print(device, ccb, CAM_ESF_ALL,
102da739265SWarner Losh 				CAM_EPF_ALL, stderr);
103da739265SWarner Losh 		goto errout;
104da739265SWarner Losh 	}
105da739265SWarner Losh 
106da739265SWarner Losh 	dtors = scsi_4btoul(hdr->num_descriptors);
107da739265SWarner Losh 	reported  = scsi_4btoul(hdr->num_returned);
108da739265SWarner Losh 	if (dtors != 0 && dtors != reported) {
109da739265SWarner Losh 		/*
110da739265SWarner Losh 		 * Get all the data... in the future we may need to step through
111da739265SWarner Losh 		 * a long list, but so far all drives I've found fit into one
112da739265SWarner Losh 		 * response. A 4k transfer can do 128 heads and current designs
113da739265SWarner Losh 		 * have 16.
114da739265SWarner Losh 		 */
115da739265SWarner Losh 		allocation_length = dtors * sizeof(struct scsi_get_physical_element_descriptor) +
116da739265SWarner Losh 		    sizeof(*hdr);
117da739265SWarner Losh 		goto again;
118da739265SWarner Losh 	}
119da739265SWarner Losh 	cam_freeccb(ccb);
120da739265SWarner Losh 	return (hdr);
121da739265SWarner Losh errout:
122da739265SWarner Losh 	cam_freeccb(ccb);
123da739265SWarner Losh 	free(hdr);
124da739265SWarner Losh 	return (NULL);
125da739265SWarner Losh }
126da739265SWarner Losh 
127da739265SWarner Losh void *
128da739265SWarner Losh scsi_wrap_inquiry(struct cam_device *device, uint32_t page, uint32_t length)
129da739265SWarner Losh {
130da739265SWarner Losh 	union ccb *ccb;
131da739265SWarner Losh 	uint8_t *buf;
132da739265SWarner Losh 
133da739265SWarner Losh 	ccb = cam_getccb(device);
134da739265SWarner Losh 
135da739265SWarner Losh 	if (ccb == NULL)
136da739265SWarner Losh 		return (NULL);
137da739265SWarner Losh 
138da739265SWarner Losh 	buf = malloc(length);
139da739265SWarner Losh 
140da739265SWarner Losh 	if (buf == NULL) {
141da739265SWarner Losh 		cam_freeccb(ccb);
142da739265SWarner Losh 		return (NULL);
143da739265SWarner Losh 	}
144da739265SWarner Losh 
145da739265SWarner Losh 	scsi_inquiry(&ccb->csio,
146da739265SWarner Losh 		     /*retries*/ 0,
147da739265SWarner Losh 		     /*cbfcnp*/ NULL,
148da739265SWarner Losh 		     /* tag_action */ MSG_SIMPLE_Q_TAG,
149*f9ffa1efSWarner Losh 		     /* inq_buf */ (uint8_t *)buf,
150da739265SWarner Losh 		     /* inq_len */ length,
151da739265SWarner Losh 		     /* evpd */ 1,
152da739265SWarner Losh 		     /* page_code */ page,
153da739265SWarner Losh 		     /* sense_len */ SSD_FULL_SIZE,
154da739265SWarner Losh 		     /* timeout */ 5000);
155da739265SWarner Losh 
156da739265SWarner Losh 	/* Disable freezing the device queue */
157da739265SWarner Losh 	ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
158da739265SWarner Losh 	// ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
159da739265SWarner Losh 
160da739265SWarner Losh 	if (cam_send_ccb(device, ccb) < 0) {
161da739265SWarner Losh 		warn("error sending INQUIRY command");
162da739265SWarner Losh 		cam_freeccb(ccb);
163da739265SWarner Losh 		free(buf);
164da739265SWarner Losh 		return (NULL);
165da739265SWarner Losh 	}
166da739265SWarner Losh 
167da739265SWarner Losh 	if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
168da739265SWarner Losh 		free(buf);
169da739265SWarner Losh 		buf = NULL;
170da739265SWarner Losh 	}
171da739265SWarner Losh 	cam_freeccb(ccb);
172da739265SWarner Losh 	return (buf);
173da739265SWarner Losh }
174da739265SWarner Losh 
175da739265SWarner Losh struct scsi_vpd_block_device_characteristics *
176da739265SWarner Losh scsi_wrap_vpd_block_device_characteristics(struct cam_device *device)
177da739265SWarner Losh {
178da739265SWarner Losh 
179da739265SWarner Losh 	return ((struct scsi_vpd_block_device_characteristics *)scsi_wrap_inquiry(
180da739265SWarner Losh 	    device, SVPD_BDC, sizeof(struct scsi_vpd_block_device_characteristics)));
181da739265SWarner Losh }
182