xref: /freebsd/usr.sbin/mptutil/mpt_cam.c (revision 4d65a7c6951cea0333f1a0c1b32c38489cdfa6c5)
1fc58801cSScott Long /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni  *
4fc58801cSScott Long  * Copyright (c) 2008 Yahoo!, Inc.
5fc58801cSScott Long  * All rights reserved.
6fc58801cSScott Long  * Written by: John Baldwin <jhb@FreeBSD.org>
7fc58801cSScott Long  *
8fc58801cSScott Long  * Redistribution and use in source and binary forms, with or without
9fc58801cSScott Long  * modification, are permitted provided that the following conditions
10fc58801cSScott Long  * are met:
11fc58801cSScott Long  * 1. Redistributions of source code must retain the above copyright
12fc58801cSScott Long  *    notice, this list of conditions and the following disclaimer.
13fc58801cSScott Long  * 2. Redistributions in binary form must reproduce the above copyright
14fc58801cSScott Long  *    notice, this list of conditions and the following disclaimer in the
15fc58801cSScott Long  *    documentation and/or other materials provided with the distribution.
16fc58801cSScott Long  * 3. Neither the name of the author nor the names of any co-contributors
17fc58801cSScott Long  *    may be used to endorse or promote products derived from this software
18fc58801cSScott Long  *    without specific prior written permission.
19fc58801cSScott Long  *
20fc58801cSScott Long  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21fc58801cSScott Long  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22fc58801cSScott Long  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23fc58801cSScott Long  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24fc58801cSScott Long  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25fc58801cSScott Long  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26fc58801cSScott Long  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27fc58801cSScott Long  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28fc58801cSScott Long  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29fc58801cSScott Long  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30fc58801cSScott Long  * SUCH DAMAGE.
31fc58801cSScott Long  */
32fc58801cSScott Long 
33fc58801cSScott Long #include <sys/param.h>
34fc58801cSScott Long #include <err.h>
35fc58801cSScott Long #include <errno.h>
36fc58801cSScott Long #include <fcntl.h>
37fc58801cSScott Long #include <stdio.h>
38fc58801cSScott Long #include <stdlib.h>
39fc58801cSScott Long #include <string.h>
40fc58801cSScott Long 
41fc58801cSScott Long #include <camlib.h>
42fc58801cSScott Long #include <cam/scsi/scsi_message.h>
43fc58801cSScott Long #include <cam/scsi/scsi_pass.h>
44fc58801cSScott Long 
45fc58801cSScott Long #include "mptutil.h"
46fc58801cSScott Long 
47fc58801cSScott Long static int xptfd;
48fc58801cSScott Long 
49fc58801cSScott Long static int
xpt_open(void)50fc58801cSScott Long xpt_open(void)
51fc58801cSScott Long {
52fc58801cSScott Long 
53fc58801cSScott Long 	if (xptfd == 0)
54fc58801cSScott Long 		xptfd = open(XPT_DEVICE, O_RDWR);
55fc58801cSScott Long 	return (xptfd);
56fc58801cSScott Long }
57fc58801cSScott Long 
58fe2f8087SJohn Baldwin /* Fetch the path id of bus 0 for the opened mpt controller. */
59fe2f8087SJohn Baldwin static int
fetch_path_id(path_id_t * path_id)60fe2f8087SJohn Baldwin fetch_path_id(path_id_t *path_id)
61fe2f8087SJohn Baldwin {
62fe2f8087SJohn Baldwin 	struct bus_match_pattern *b;
63fe2f8087SJohn Baldwin 	union ccb ccb;
64fe2f8087SJohn Baldwin 	size_t bufsize;
65c5f2b79dSJohn Baldwin 	int error;
66fe2f8087SJohn Baldwin 
67fe2f8087SJohn Baldwin 	if (xpt_open() < 0)
68fe2f8087SJohn Baldwin 		return (ENXIO);
69fe2f8087SJohn Baldwin 
70fe2f8087SJohn Baldwin 	/* First, find the path id of bus 0 for this mpt controller. */
71fe2f8087SJohn Baldwin 	bzero(&ccb, sizeof(ccb));
72fe2f8087SJohn Baldwin 
73fe2f8087SJohn Baldwin 	ccb.ccb_h.func_code = XPT_DEV_MATCH;
74fe2f8087SJohn Baldwin 
75fe2f8087SJohn Baldwin 	bufsize = sizeof(struct dev_match_result) * 1;
76fe2f8087SJohn Baldwin 	ccb.cdm.num_matches = 0;
77fe2f8087SJohn Baldwin 	ccb.cdm.match_buf_len = bufsize;
78fe2f8087SJohn Baldwin 	ccb.cdm.matches = calloc(1, bufsize);
79fe2f8087SJohn Baldwin 
80fe2f8087SJohn Baldwin 	bufsize = sizeof(struct dev_match_pattern) * 1;
81fe2f8087SJohn Baldwin 	ccb.cdm.num_patterns = 1;
82fe2f8087SJohn Baldwin 	ccb.cdm.pattern_buf_len = bufsize;
83fe2f8087SJohn Baldwin 	ccb.cdm.patterns = calloc(1, bufsize);
84fe2f8087SJohn Baldwin 
85fe2f8087SJohn Baldwin 	/* Match mptX bus 0. */
86fe2f8087SJohn Baldwin 	ccb.cdm.patterns[0].type = DEV_MATCH_BUS;
87fe2f8087SJohn Baldwin 	b = &ccb.cdm.patterns[0].pattern.bus_pattern;
88fe2f8087SJohn Baldwin 	snprintf(b->dev_name, sizeof(b->dev_name), "mpt");
89fe2f8087SJohn Baldwin 	b->unit_number = mpt_unit;
90fe2f8087SJohn Baldwin 	b->bus_id = 0;
91fe2f8087SJohn Baldwin 	b->flags = BUS_MATCH_NAME | BUS_MATCH_UNIT | BUS_MATCH_BUS_ID;
92fe2f8087SJohn Baldwin 
93fe2f8087SJohn Baldwin 	if (ioctl(xptfd, CAMIOCOMMAND, &ccb) < 0) {
94c5f2b79dSJohn Baldwin 		error = errno;
95fe2f8087SJohn Baldwin 		free(ccb.cdm.matches);
96fe2f8087SJohn Baldwin 		free(ccb.cdm.patterns);
97c5f2b79dSJohn Baldwin 		return (error);
98fe2f8087SJohn Baldwin 	}
99fe2f8087SJohn Baldwin 	free(ccb.cdm.patterns);
100fe2f8087SJohn Baldwin 
101fe2f8087SJohn Baldwin 	if (((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) ||
102fe2f8087SJohn Baldwin 	    (ccb.cdm.status != CAM_DEV_MATCH_LAST)) {
103fe2f8087SJohn Baldwin 		warnx("fetch_path_id got CAM error %#x, CDM error %d\n",
104fe2f8087SJohn Baldwin 		    ccb.ccb_h.status, ccb.cdm.status);
105fe2f8087SJohn Baldwin 		free(ccb.cdm.matches);
106fe2f8087SJohn Baldwin 		return (EIO);
107fe2f8087SJohn Baldwin 	}
108fe2f8087SJohn Baldwin 
109fe2f8087SJohn Baldwin 	/* We should have exactly 1 match for the bus. */
110fe2f8087SJohn Baldwin 	if (ccb.cdm.num_matches != 1 ||
111fe2f8087SJohn Baldwin 	    ccb.cdm.matches[0].type != DEV_MATCH_BUS) {
112fe2f8087SJohn Baldwin 		free(ccb.cdm.matches);
113fe2f8087SJohn Baldwin 		return (ENOENT);
114fe2f8087SJohn Baldwin 	}
115fe2f8087SJohn Baldwin 	*path_id = ccb.cdm.matches[0].result.bus_result.path_id;
116fe2f8087SJohn Baldwin 	free(ccb.cdm.matches);
117fe2f8087SJohn Baldwin 	return (0);
118fe2f8087SJohn Baldwin }
119fe2f8087SJohn Baldwin 
120fc58801cSScott Long int
mpt_query_disk(U8 VolumeBus,U8 VolumeID,struct mpt_query_disk * qd)121fc58801cSScott Long mpt_query_disk(U8 VolumeBus, U8 VolumeID, struct mpt_query_disk *qd)
122fc58801cSScott Long {
123fc58801cSScott Long 	struct periph_match_pattern *p;
124fc58801cSScott Long 	struct periph_match_result *r;
125fc58801cSScott Long 	union ccb ccb;
126fe2f8087SJohn Baldwin 	path_id_t path_id;
127fc58801cSScott Long 	size_t bufsize;
128c5f2b79dSJohn Baldwin 	int error;
129fc58801cSScott Long 
130fc58801cSScott Long 	/* mpt(4) only handles devices on bus 0. */
131fc58801cSScott Long 	if (VolumeBus != 0)
132fc58801cSScott Long 		return (ENXIO);
133fc58801cSScott Long 
134fc58801cSScott Long 	if (xpt_open() < 0)
135fc58801cSScott Long 		return (ENXIO);
136fc58801cSScott Long 
137fe2f8087SJohn Baldwin 	/* Find the path ID of bus 0. */
138fe2f8087SJohn Baldwin 	error = fetch_path_id(&path_id);
139fe2f8087SJohn Baldwin 	if (error)
140fe2f8087SJohn Baldwin 		return (error);
141fe2f8087SJohn Baldwin 
142fc58801cSScott Long 	bzero(&ccb, sizeof(ccb));
143fc58801cSScott Long 
144fc58801cSScott Long 	ccb.ccb_h.func_code = XPT_DEV_MATCH;
145fc58801cSScott Long 	ccb.ccb_h.path_id = CAM_XPT_PATH_ID;
146fc58801cSScott Long 	ccb.ccb_h.target_id = CAM_TARGET_WILDCARD;
147fc58801cSScott Long 	ccb.ccb_h.target_lun = CAM_LUN_WILDCARD;
148fc58801cSScott Long 
149fc58801cSScott Long 	bufsize = sizeof(struct dev_match_result) * 5;
150fc58801cSScott Long 	ccb.cdm.num_matches = 0;
151fc58801cSScott Long 	ccb.cdm.match_buf_len = bufsize;
152fc58801cSScott Long 	ccb.cdm.matches = calloc(1, bufsize);
153fc58801cSScott Long 
154fe2f8087SJohn Baldwin 	bufsize = sizeof(struct dev_match_pattern) * 1;
155fe2f8087SJohn Baldwin 	ccb.cdm.num_patterns = 1;
156fc58801cSScott Long 	ccb.cdm.pattern_buf_len = bufsize;
157fc58801cSScott Long 	ccb.cdm.patterns = calloc(1, bufsize);
158fc58801cSScott Long 
159fc58801cSScott Long 	/* Look for a "da" device at the specified target and lun. */
160fe2f8087SJohn Baldwin 	ccb.cdm.patterns[0].type = DEV_MATCH_PERIPH;
161fe2f8087SJohn Baldwin 	p = &ccb.cdm.patterns[0].pattern.periph_pattern;
162fe2f8087SJohn Baldwin 	p->path_id = path_id;
163fc58801cSScott Long 	snprintf(p->periph_name, sizeof(p->periph_name), "da");
164fc58801cSScott Long 	p->target_id = VolumeID;
165fe2f8087SJohn Baldwin 	p->flags = PERIPH_MATCH_PATH | PERIPH_MATCH_NAME | PERIPH_MATCH_TARGET;
166fc58801cSScott Long 
167fc58801cSScott Long 	if (ioctl(xptfd, CAMIOCOMMAND, &ccb) < 0) {
168c5f2b79dSJohn Baldwin 		error = errno;
169fc58801cSScott Long 		free(ccb.cdm.matches);
170fc58801cSScott Long 		free(ccb.cdm.patterns);
171c5f2b79dSJohn Baldwin 		return (error);
172fc58801cSScott Long 	}
173fc58801cSScott Long 	free(ccb.cdm.patterns);
174fc58801cSScott Long 
175fc58801cSScott Long 	if (((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) ||
176fc58801cSScott Long 	    (ccb.cdm.status != CAM_DEV_MATCH_LAST)) {
177fc58801cSScott Long 		warnx("mpt_query_disk got CAM error %#x, CDM error %d\n",
178fc58801cSScott Long 		    ccb.ccb_h.status, ccb.cdm.status);
179fc58801cSScott Long 		free(ccb.cdm.matches);
180fc58801cSScott Long 		return (EIO);
181fc58801cSScott Long 	}
182fc58801cSScott Long 
183fc58801cSScott Long 	/*
184fe2f8087SJohn Baldwin 	 * We should have exactly 1 match for the peripheral.
185fe2f8087SJohn Baldwin 	 * However, if we don't get a match, don't print an error
186fe2f8087SJohn Baldwin 	 * message and return ENOENT.
187fc58801cSScott Long 	 */
188fe2f8087SJohn Baldwin 	if (ccb.cdm.num_matches == 0) {
189fc58801cSScott Long 		free(ccb.cdm.matches);
190fc58801cSScott Long 		return (ENOENT);
191fc58801cSScott Long 	}
192fe2f8087SJohn Baldwin 	if (ccb.cdm.num_matches != 1) {
193fe2f8087SJohn Baldwin 		warnx("mpt_query_disk got %d matches, expected 1",
194fc58801cSScott Long 		    ccb.cdm.num_matches);
195fc58801cSScott Long 		free(ccb.cdm.matches);
196fc58801cSScott Long 		return (EIO);
197fc58801cSScott Long 	}
198fe2f8087SJohn Baldwin 	if (ccb.cdm.matches[0].type != DEV_MATCH_PERIPH) {
199fe2f8087SJohn Baldwin 		warnx("mpt_query_disk got wrong CAM match");
200fc58801cSScott Long 		free(ccb.cdm.matches);
201fc58801cSScott Long 		return (EIO);
202fc58801cSScott Long 	}
203fc58801cSScott Long 
204fc58801cSScott Long 	/* Copy out the data. */
205fc58801cSScott Long 	r = &ccb.cdm.matches[1].result.periph_result;
206fc58801cSScott Long 	snprintf(qd->devname, sizeof(qd->devname), "%s%d", r->periph_name,
207fc58801cSScott Long 	    r->unit_number);
208fc58801cSScott Long 	free(ccb.cdm.matches);
209fc58801cSScott Long 
210fc58801cSScott Long 	return (0);
211fc58801cSScott Long }
212fc58801cSScott Long 
213fc58801cSScott Long static int
periph_is_volume(CONFIG_PAGE_IOC_2 * ioc2,struct periph_match_result * r)214fc58801cSScott Long periph_is_volume(CONFIG_PAGE_IOC_2 *ioc2, struct periph_match_result *r)
215fc58801cSScott Long {
216fc58801cSScott Long 	CONFIG_PAGE_IOC_2_RAID_VOL *vol;
217fc58801cSScott Long 	int i;
218fc58801cSScott Long 
219fc58801cSScott Long 	if (ioc2 == NULL)
220fc58801cSScott Long 		return (0);
221fc58801cSScott Long 	vol = ioc2->RaidVolume;
222fc58801cSScott Long 	for (i = 0; i < ioc2->NumActiveVolumes; vol++, i++) {
223fc58801cSScott Long 		if (vol->VolumeBus == 0 && vol->VolumeID == r->target_id)
224fc58801cSScott Long 			return (1);
225fc58801cSScott Long 	}
226fc58801cSScott Long 	return (0);
227fc58801cSScott Long }
228fc58801cSScott Long 
229fc58801cSScott Long /* Much borrowed from scsireadcapacity() in src/sbin/camcontrol/camcontrol.c. */
230fc58801cSScott Long static int
fetch_scsi_capacity(struct cam_device * dev,struct mpt_standalone_disk * disk)231fc58801cSScott Long fetch_scsi_capacity(struct cam_device *dev, struct mpt_standalone_disk *disk)
232fc58801cSScott Long {
233fc58801cSScott Long 	struct scsi_read_capacity_data rcap;
234fc58801cSScott Long 	struct scsi_read_capacity_data_long rcaplong;
235fc58801cSScott Long 	union ccb *ccb;
236fc58801cSScott Long 	int error;
237fc58801cSScott Long 
238fc58801cSScott Long 	ccb = cam_getccb(dev);
239fc58801cSScott Long 	if (ccb == NULL)
240fc58801cSScott Long 		return (ENOMEM);
241fc58801cSScott Long 
242fc58801cSScott Long 	/* Zero the rest of the ccb. */
24395320aceSDon Lewis 	CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->csio);
244fc58801cSScott Long 
245fc58801cSScott Long 	scsi_read_capacity(&ccb->csio, 1, NULL, MSG_SIMPLE_Q_TAG, &rcap,
246fc58801cSScott Long 	    SSD_FULL_SIZE, 5000);
247fc58801cSScott Long 
248fc58801cSScott Long 	/* Disable freezing the device queue */
249fc58801cSScott Long 	ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
250fc58801cSScott Long 
251fc58801cSScott Long 	if (cam_send_ccb(dev, ccb) < 0) {
252fc58801cSScott Long 		error = errno;
253fc58801cSScott Long 		cam_freeccb(ccb);
254fc58801cSScott Long 		return (error);
255fc58801cSScott Long 	}
256fc58801cSScott Long 
257fc58801cSScott Long 	if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
258fc58801cSScott Long 		cam_freeccb(ccb);
259fc58801cSScott Long 		return (EIO);
260fc58801cSScott Long 	}
261fc58801cSScott Long 
262fc58801cSScott Long 	/*
263fc58801cSScott Long 	 * A last block of 2^32-1 means that the true capacity is over 2TB,
264fc58801cSScott Long 	 * and we need to issue the long READ CAPACITY to get the real
265fc58801cSScott Long 	 * capacity.  Otherwise, we're all set.
266fc58801cSScott Long 	 */
267fc58801cSScott Long 	if (scsi_4btoul(rcap.addr) != 0xffffffff) {
268fc58801cSScott Long 		disk->maxlba = scsi_4btoul(rcap.addr);
269ae092f75SDon Lewis 		cam_freeccb(ccb);
270fc58801cSScott Long 		return (0);
271fc58801cSScott Long 	}
272fc58801cSScott Long 
273fc58801cSScott Long 	/* Zero the rest of the ccb. */
27495320aceSDon Lewis 	CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->csio);
275fc58801cSScott Long 
276fc58801cSScott Long 	scsi_read_capacity_16(&ccb->csio, 1, NULL, MSG_SIMPLE_Q_TAG, 0, 0, 0,
277e6bd5983SKenneth D. Merry 	    (uint8_t *)&rcaplong, sizeof(rcaplong), SSD_FULL_SIZE, 5000);
278fc58801cSScott Long 
279fc58801cSScott Long 	/* Disable freezing the device queue */
280fc58801cSScott Long 	ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
281fc58801cSScott Long 
282fc58801cSScott Long 	if (cam_send_ccb(dev, ccb) < 0) {
283fc58801cSScott Long 		error = errno;
284fc58801cSScott Long 		cam_freeccb(ccb);
285fc58801cSScott Long 		return (error);
286fc58801cSScott Long 	}
287fc58801cSScott Long 
288fc58801cSScott Long 	if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
289fc58801cSScott Long 		cam_freeccb(ccb);
290fc58801cSScott Long 		return (EIO);
291fc58801cSScott Long 	}
292fc58801cSScott Long 	cam_freeccb(ccb);
293fc58801cSScott Long 
294fc58801cSScott Long 	disk->maxlba = scsi_8btou64(rcaplong.addr);
295fc58801cSScott Long 	return (0);
296fc58801cSScott Long }
297fc58801cSScott Long 
298fc58801cSScott Long /* Borrowed heavily from scsi_all.c:scsi_print_inquiry(). */
299fc58801cSScott Long static void
format_scsi_inquiry(struct mpt_standalone_disk * disk,struct scsi_inquiry_data * inq_data)300fc58801cSScott Long format_scsi_inquiry(struct mpt_standalone_disk *disk,
301fc58801cSScott Long     struct scsi_inquiry_data *inq_data)
302fc58801cSScott Long {
303fc58801cSScott Long 	char vendor[16], product[48], revision[16], rstr[12];
304fc58801cSScott Long 
305fc58801cSScott Long 	if (SID_QUAL_IS_VENDOR_UNIQUE(inq_data))
306fc58801cSScott Long 		return;
307fc58801cSScott Long 	if (SID_TYPE(inq_data) != T_DIRECT)
308fc58801cSScott Long 		return;
309fc58801cSScott Long 	if (SID_QUAL(inq_data) != SID_QUAL_LU_CONNECTED)
310fc58801cSScott Long 		return;
311fc58801cSScott Long 
312fc58801cSScott Long 	cam_strvis(vendor, inq_data->vendor, sizeof(inq_data->vendor),
313fc58801cSScott Long 	    sizeof(vendor));
314fc58801cSScott Long 	cam_strvis(product, inq_data->product, sizeof(inq_data->product),
315fc58801cSScott Long 	    sizeof(product));
316fc58801cSScott Long 	cam_strvis(revision, inq_data->revision, sizeof(inq_data->revision),
317fc58801cSScott Long 	    sizeof(revision));
318fc58801cSScott Long 
319fc58801cSScott Long 	/* Hack for SATA disks, no idea how to tell speed. */
320fc58801cSScott Long 	if (strcmp(vendor, "ATA") == 0) {
321fc58801cSScott Long 		snprintf(disk->inqstring, sizeof(disk->inqstring),
322fc58801cSScott Long 		    "<%s %s> SATA", product, revision);
323fc58801cSScott Long 		return;
324fc58801cSScott Long 	}
325fc58801cSScott Long 
326fc58801cSScott Long 	switch (SID_ANSI_REV(inq_data)) {
327fc58801cSScott Long 	case SCSI_REV_CCS:
328fc58801cSScott Long 		strcpy(rstr, "SCSI-CCS");
329fc58801cSScott Long 		break;
330fc58801cSScott Long 	case 5:
331fc58801cSScott Long 		strcpy(rstr, "SAS");
332fc58801cSScott Long 		break;
333fc58801cSScott Long 	default:
334fc58801cSScott Long 		snprintf(rstr, sizeof (rstr), "SCSI-%d",
335fc58801cSScott Long 		    SID_ANSI_REV(inq_data));
336fc58801cSScott Long 		break;
337fc58801cSScott Long 	}
338fc58801cSScott Long 	snprintf(disk->inqstring, sizeof(disk->inqstring), "<%s %s %s> %s",
339fc58801cSScott Long 	    vendor, product, revision, rstr);
340fc58801cSScott Long }
341fc58801cSScott Long 
342fc58801cSScott Long /* Much borrowed from scsiinquiry() in src/sbin/camcontrol/camcontrol.c. */
343fc58801cSScott Long static int
fetch_scsi_inquiry(struct cam_device * dev,struct mpt_standalone_disk * disk)344fc58801cSScott Long fetch_scsi_inquiry(struct cam_device *dev, struct mpt_standalone_disk *disk)
345fc58801cSScott Long {
346fc58801cSScott Long 	struct scsi_inquiry_data *inq_buf;
347fc58801cSScott Long 	union ccb *ccb;
348fc58801cSScott Long 	int error;
349fc58801cSScott Long 
350fc58801cSScott Long 	ccb = cam_getccb(dev);
351fc58801cSScott Long 	if (ccb == NULL)
352fc58801cSScott Long 		return (ENOMEM);
353fc58801cSScott Long 
354fc58801cSScott Long 	/* Zero the rest of the ccb. */
35595320aceSDon Lewis 	CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->csio);
356fc58801cSScott Long 
357fc58801cSScott Long 	inq_buf = calloc(1, sizeof(*inq_buf));
358fc58801cSScott Long 	if (inq_buf == NULL) {
359fc58801cSScott Long 		cam_freeccb(ccb);
360fc58801cSScott Long 		return (ENOMEM);
361fc58801cSScott Long 	}
362fc58801cSScott Long 	scsi_inquiry(&ccb->csio, 1, NULL, MSG_SIMPLE_Q_TAG, (void *)inq_buf,
363fc58801cSScott Long 	    SHORT_INQUIRY_LENGTH, 0, 0, SSD_FULL_SIZE, 5000);
364fc58801cSScott Long 
365fc58801cSScott Long 	/* Disable freezing the device queue */
366fc58801cSScott Long 	ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
367fc58801cSScott Long 
368fc58801cSScott Long 	if (cam_send_ccb(dev, ccb) < 0) {
369fc58801cSScott Long 		error = errno;
370fc58801cSScott Long 		free(inq_buf);
371fc58801cSScott Long 		cam_freeccb(ccb);
372fc58801cSScott Long 		return (error);
373fc58801cSScott Long 	}
374fc58801cSScott Long 
375fc58801cSScott Long 	if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
376fc58801cSScott Long 		free(inq_buf);
377fc58801cSScott Long 		cam_freeccb(ccb);
378fc58801cSScott Long 		return (EIO);
379fc58801cSScott Long 	}
380fc58801cSScott Long 
381fc58801cSScott Long 	cam_freeccb(ccb);
382fc58801cSScott Long 	format_scsi_inquiry(disk, inq_buf);
383fc58801cSScott Long 	free(inq_buf);
384fc58801cSScott Long 	return (0);
385fc58801cSScott Long }
386fc58801cSScott Long 
387fc58801cSScott Long int
mpt_fetch_disks(int fd,int * ndisks,struct mpt_standalone_disk ** disksp)388fc58801cSScott Long mpt_fetch_disks(int fd, int *ndisks, struct mpt_standalone_disk **disksp)
389fc58801cSScott Long {
390fc58801cSScott Long 	CONFIG_PAGE_IOC_2 *ioc2;
391fc58801cSScott Long 	struct mpt_standalone_disk *disks;
392fc58801cSScott Long 	struct periph_match_pattern *p;
393fc58801cSScott Long 	struct periph_match_result *r;
394fc58801cSScott Long 	struct cam_device *dev;
395fc58801cSScott Long 	union ccb ccb;
396fe2f8087SJohn Baldwin 	path_id_t path_id;
397fc58801cSScott Long 	size_t bufsize;
398fe2f8087SJohn Baldwin 	int count, error;
399c5f2b79dSJohn Baldwin 	uint32_t i;
400fc58801cSScott Long 
401fc58801cSScott Long 	if (xpt_open() < 0)
402fc58801cSScott Long 		return (ENXIO);
403fc58801cSScott Long 
404fe2f8087SJohn Baldwin 	error = fetch_path_id(&path_id);
405fe2f8087SJohn Baldwin 	if (error)
406fe2f8087SJohn Baldwin 		return (error);
407fe2f8087SJohn Baldwin 
408fc58801cSScott Long 	for (count = 100;; count+= 100) {
409fc58801cSScott Long 		/* Try to fetch 'count' disks in one go. */
410fc58801cSScott Long 		bzero(&ccb, sizeof(ccb));
411fc58801cSScott Long 
412fc58801cSScott Long 		ccb.ccb_h.func_code = XPT_DEV_MATCH;
413fc58801cSScott Long 
414fe2f8087SJohn Baldwin 		bufsize = sizeof(struct dev_match_result) * (count + 1);
415fc58801cSScott Long 		ccb.cdm.num_matches = 0;
416fc58801cSScott Long 		ccb.cdm.match_buf_len = bufsize;
417fc58801cSScott Long 		ccb.cdm.matches = calloc(1, bufsize);
418fc58801cSScott Long 
419fe2f8087SJohn Baldwin 		bufsize = sizeof(struct dev_match_pattern) * 1;
420fe2f8087SJohn Baldwin 		ccb.cdm.num_patterns = 1;
421fc58801cSScott Long 		ccb.cdm.pattern_buf_len = bufsize;
422fc58801cSScott Long 		ccb.cdm.patterns = calloc(1, bufsize);
423fc58801cSScott Long 
424fc58801cSScott Long 		/* Match any "da" peripherals. */
425fe2f8087SJohn Baldwin 		ccb.cdm.patterns[0].type = DEV_MATCH_PERIPH;
426fe2f8087SJohn Baldwin 		p = &ccb.cdm.patterns[0].pattern.periph_pattern;
427fe2f8087SJohn Baldwin 		p->path_id = path_id;
428fc58801cSScott Long 		snprintf(p->periph_name, sizeof(p->periph_name), "da");
429fe2f8087SJohn Baldwin 		p->flags = PERIPH_MATCH_PATH | PERIPH_MATCH_NAME;
430fc58801cSScott Long 
431fc58801cSScott Long 		if (ioctl(xptfd, CAMIOCOMMAND, &ccb) < 0) {
432c5f2b79dSJohn Baldwin 			error = errno;
433fc58801cSScott Long 			free(ccb.cdm.matches);
434fc58801cSScott Long 			free(ccb.cdm.patterns);
435c5f2b79dSJohn Baldwin 			return (error);
436fc58801cSScott Long 		}
437fc58801cSScott Long 		free(ccb.cdm.patterns);
438fc58801cSScott Long 
439fc58801cSScott Long 		/* Check for CCB errors. */
440fc58801cSScott Long 		if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
441fc58801cSScott Long 			free(ccb.cdm.matches);
442fc58801cSScott Long 			return (EIO);
443fc58801cSScott Long 		}
444fc58801cSScott Long 
445fc58801cSScott Long 		/* If we need a longer list, try again. */
446fc58801cSScott Long 		if (ccb.cdm.status == CAM_DEV_MATCH_MORE) {
447fc58801cSScott Long 			free(ccb.cdm.matches);
448fc58801cSScott Long 			continue;
449fc58801cSScott Long 		}
450fc58801cSScott Long 
451fc58801cSScott Long 		/* If we got an error, abort. */
452fc58801cSScott Long 		if (ccb.cdm.status != CAM_DEV_MATCH_LAST) {
453fc58801cSScott Long 			free(ccb.cdm.matches);
454fc58801cSScott Long 			return (EIO);
455fc58801cSScott Long 		}
456fc58801cSScott Long 		break;
457fc58801cSScott Long 	}
458fc58801cSScott Long 
459fe2f8087SJohn Baldwin 	/* Shortcut if we don't have any "da" devices. */
460fe2f8087SJohn Baldwin 	if (ccb.cdm.num_matches == 0) {
461fc58801cSScott Long 		free(ccb.cdm.matches);
462fe2f8087SJohn Baldwin 		*ndisks = 0;
463fe2f8087SJohn Baldwin 		*disksp = NULL;
464fe2f8087SJohn Baldwin 		return (0);
465fc58801cSScott Long 	}
466fe2f8087SJohn Baldwin 
467fe2f8087SJohn Baldwin 	/* We should have N matches, 1 for each "da" device. */
468fe2f8087SJohn Baldwin 	for (i = 0; i < ccb.cdm.num_matches; i++) {
469fc58801cSScott Long 		if (ccb.cdm.matches[i].type != DEV_MATCH_PERIPH) {
470fc58801cSScott Long 			warnx("mpt_fetch_disks got wrong CAM matches");
471fc58801cSScott Long 			free(ccb.cdm.matches);
472fc58801cSScott Long 			return (EIO);
473fc58801cSScott Long 		}
474fc58801cSScott Long 	}
475fc58801cSScott Long 
476fc58801cSScott Long 	/*
477fc58801cSScott Long 	 * Some of the "da" peripherals may be for RAID volumes, so
478fc58801cSScott Long 	 * fetch the IOC 2 page (list of RAID volumes) so we can
479fc58801cSScott Long 	 * exclude them from the list.
480fc58801cSScott Long 	 */
481fc58801cSScott Long 	ioc2 = mpt_read_ioc_page(fd, 2, NULL);
482c5f2b79dSJohn Baldwin 	if (ioc2 == NULL)
483c5f2b79dSJohn Baldwin 		return (errno);
484fc58801cSScott Long 	disks = calloc(ccb.cdm.num_matches, sizeof(*disks));
485fc58801cSScott Long 	count = 0;
486fe2f8087SJohn Baldwin 	for (i = 0; i < ccb.cdm.num_matches; i++) {
487fc58801cSScott Long 		r = &ccb.cdm.matches[i].result.periph_result;
488fc58801cSScott Long 		if (periph_is_volume(ioc2, r))
489fc58801cSScott Long 			continue;
490fc58801cSScott Long 		disks[count].bus = 0;
491fc58801cSScott Long 		disks[count].target = r->target_id;
492fc58801cSScott Long 		snprintf(disks[count].devname, sizeof(disks[count].devname),
493fc58801cSScott Long 		    "%s%d", r->periph_name, r->unit_number);
494fc58801cSScott Long 
495fc58801cSScott Long 		dev = cam_open_device(disks[count].devname, O_RDWR);
496fc58801cSScott Long 		if (dev != NULL) {
497fc58801cSScott Long 			fetch_scsi_capacity(dev, &disks[count]);
498fc58801cSScott Long 			fetch_scsi_inquiry(dev, &disks[count]);
499fc58801cSScott Long 			cam_close_device(dev);
500fc58801cSScott Long 		}
501fc58801cSScott Long 		count++;
502fc58801cSScott Long 	}
503fc58801cSScott Long 	free(ccb.cdm.matches);
504fc58801cSScott Long 	free(ioc2);
505fc58801cSScott Long 
506fc58801cSScott Long 	*ndisks = count;
507fc58801cSScott Long 	*disksp = disks;
508fc58801cSScott Long 	return (0);
509fc58801cSScott Long }
510fc58801cSScott Long 
511fc58801cSScott Long /*
512db4fcadfSConrad Meyer  * Instruct the mpt(4) device to rescan its buses to find new devices
513fc58801cSScott Long  * such as disks whose RAID physdisk page was removed or volumes that
514fc58801cSScott Long  * were created.  If id is -1, the entire bus is rescanned.
515fc58801cSScott Long  * Otherwise, only devices at the specified ID are rescanned.  If bus
516db4fcadfSConrad Meyer  * is -1, then all buses are scanned instead of the specified bus.
517fc58801cSScott Long  * Note that currently, only bus 0 is supported.
518fc58801cSScott Long  */
519fc58801cSScott Long int
mpt_rescan_bus(int bus,int id)520fc58801cSScott Long mpt_rescan_bus(int bus, int id)
521fc58801cSScott Long {
522fc58801cSScott Long 	union ccb ccb;
523fc58801cSScott Long 	path_id_t path_id;
524fe2f8087SJohn Baldwin 	int error;
525fc58801cSScott Long 
526fc58801cSScott Long 	/* mpt(4) only handles devices on bus 0. */
527fc58801cSScott Long 	if (bus != -1 && bus != 0)
528fc58801cSScott Long 		return (EINVAL);
529fc58801cSScott Long 
530fc58801cSScott Long 	if (xpt_open() < 0)
531fc58801cSScott Long 		return (ENXIO);
532fc58801cSScott Long 
533fe2f8087SJohn Baldwin 	error = fetch_path_id(&path_id);
534fe2f8087SJohn Baldwin 	if (error)
535fe2f8087SJohn Baldwin 		return (error);
536fe2f8087SJohn Baldwin 
537fe2f8087SJohn Baldwin 	/* Perform the actual rescan. */
538fc58801cSScott Long 	bzero(&ccb, sizeof(ccb));
539fc58801cSScott Long 	ccb.ccb_h.path_id = path_id;
540fc58801cSScott Long 	if (id == -1) {
541fc58801cSScott Long 		ccb.ccb_h.func_code = XPT_SCAN_BUS;
542fc58801cSScott Long 		ccb.ccb_h.target_id = CAM_TARGET_WILDCARD;
543fc58801cSScott Long 		ccb.ccb_h.target_lun = CAM_LUN_WILDCARD;
544fc58801cSScott Long 		ccb.ccb_h.timeout = 5000;
545fc58801cSScott Long 	} else {
546fc58801cSScott Long 		ccb.ccb_h.func_code = XPT_SCAN_LUN;
547fc58801cSScott Long 		ccb.ccb_h.target_id = id;
548fc58801cSScott Long 		ccb.ccb_h.target_lun = 0;
549fc58801cSScott Long 	}
550fc58801cSScott Long 	ccb.crcn.flags = CAM_FLAG_NONE;
551fc58801cSScott Long 
552fc58801cSScott Long 	/* Run this at a low priority. */
553fc58801cSScott Long 	ccb.ccb_h.pinfo.priority = 5;
554fc58801cSScott Long 
555fc58801cSScott Long 	if (ioctl(xptfd, CAMIOCOMMAND, &ccb) == -1)
556fc58801cSScott Long 		return (errno);
557fc58801cSScott Long 
558fc58801cSScott Long 	if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
559fc58801cSScott Long 		warnx("mpt_rescan_bus rescan got CAM error %#x\n",
560fc58801cSScott Long 		    ccb.ccb_h.status & CAM_STATUS_MASK);
561fc58801cSScott Long 		return (EIO);
562fc58801cSScott Long 	}
563fc58801cSScott Long 
564fc58801cSScott Long 	return (0);
565fc58801cSScott Long }
566