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