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