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