xref: /freebsd/lib/libcam/scsi_wrap.c (revision 1d386b48)
1 /*-
2  * Copyright (c) 2021 Netflix, Inc.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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
23  * SUCH DAMAGE.
24  */
25 
26 /*
27  * Wrapper functions to make requests and get answers w/o managing the
28  * details.
29  */
30 
31 #include <sys/cdefs.h>
32 #include <sys/types.h>
33 
34 #include <err.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <strings.h>
38 
39 #include <cam/cam.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>
44 #include "camlib.h"
45 #include "scsi_wrap.h"
46 
47 void *
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)
50 {
51 	uint32_t allocation_length;
52 	union ccb *ccb = NULL;
53 	struct scsi_get_physical_element_hdr *hdr = NULL;
54 	uint32_t dtors;
55 	uint32_t reported;
56 
57 	ccb = cam_getccb(device);
58 	if (ccb == NULL) {
59 		warnx("Can't allocate ccb");
60 		return (NULL);
61 	}
62 
63 	/*
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
68 	 * proper length.
69 	 */
70 	allocation_length = MAX(sizeof(*hdr), 4096);
71 again:
72 	free(hdr);
73 	hdr = calloc(allocation_length, 1);
74 	if (hdr == NULL) {
75 		warnx("Can't allocate memory for physical element list");
76 		return (NULL);
77 	}
78 
79 	scsi_get_physical_element_status(&ccb->csio,
80 	    retry_count,
81 	    NULL,
82 	    task_attr,
83 	    (uint8_t *)hdr,
84 	    allocation_length,
85 	    report_type,
86 	    start_element,
87 	    SSD_FULL_SIZE,
88 	    timeout);
89 
90 	/* Disable freezing the device queue */
91 	ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
92 
93 	if (cam_send_ccb(device, ccb) < 0) {
94 		warn("error sending GET PHYSICAL ELEMENT STATUS command");
95 		goto errout;
96 	}
97 
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);
101 		goto errout;
102 	}
103 
104 	dtors = scsi_4btoul(hdr->num_descriptors);
105 	reported  = scsi_4btoul(hdr->num_returned);
106 	if (dtors != 0 && dtors != reported) {
107 		/*
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
111 		 * have 16.
112 		 */
113 		allocation_length = dtors * sizeof(struct scsi_get_physical_element_descriptor) +
114 		    sizeof(*hdr);
115 		goto again;
116 	}
117 	cam_freeccb(ccb);
118 	return (hdr);
119 errout:
120 	cam_freeccb(ccb);
121 	free(hdr);
122 	return (NULL);
123 }
124 
125 void *
126 scsi_wrap_inquiry(struct cam_device *device, uint32_t page, uint32_t length)
127 {
128 	union ccb *ccb;
129 	uint8_t *buf;
130 
131 	ccb = cam_getccb(device);
132 
133 	if (ccb == NULL)
134 		return (NULL);
135 
136 	buf = malloc(length);
137 
138 	if (buf == NULL) {
139 		cam_freeccb(ccb);
140 		return (NULL);
141 	}
142 
143 	scsi_inquiry(&ccb->csio,
144 		     /*retries*/ 0,
145 		     /*cbfcnp*/ NULL,
146 		     /* tag_action */ MSG_SIMPLE_Q_TAG,
147 		     /* inq_buf */ (uint8_t *)buf,
148 		     /* inq_len */ length,
149 		     /* evpd */ 1,
150 		     /* page_code */ page,
151 		     /* sense_len */ SSD_FULL_SIZE,
152 		     /* timeout */ 5000);
153 
154 	/* Disable freezing the device queue */
155 	ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
156 	// ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
157 
158 	if (cam_send_ccb(device, ccb) < 0) {
159 		warn("error sending INQUIRY command");
160 		cam_freeccb(ccb);
161 		free(buf);
162 		return (NULL);
163 	}
164 
165 	if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
166 		free(buf);
167 		buf = NULL;
168 	}
169 	cam_freeccb(ccb);
170 	return (buf);
171 }
172 
173 struct scsi_vpd_block_device_characteristics *
174 scsi_wrap_vpd_block_device_characteristics(struct cam_device *device)
175 {
176 
177 	return ((struct scsi_vpd_block_device_characteristics *)scsi_wrap_inquiry(
178 	    device, SVPD_BDC, sizeof(struct scsi_vpd_block_device_characteristics)));
179 }
180