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