summaryrefslogtreecommitdiffstats
path: root/usr.sbin/mptutil/mpt_cam.c
blob: 6a8ff071376c0dfd91753aa8123b62aadc8761a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/*-
 * Copyright (c) 2008 Yahoo!, Inc.
 * All rights reserved.
 * Written by: John Baldwin <jhb@FreeBSD.org>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the author nor the names of any co-contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__RCSID("$FreeBSD$");

#include <sys/param.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <camlib.h>
#include <cam/scsi/scsi_message.h>
#include <cam/scsi/scsi_pass.h>

#include "mptutil.h"

static int xptfd;

static int
xpt_open(void)
{

	if (xptfd == 0)
		xptfd = open(XPT_DEVICE, O_RDWR);
	return (xptfd);
}

/* Fetch the path id of bus 0 for the opened mpt controller. */
static int
fetch_path_id(path_id_t *path_id)
{
	struct bus_match_pattern *b;
	union ccb ccb;
	size_t bufsize;
	int error;

	if (xpt_open() < 0)
		return (ENXIO);

	/* First, find the path id of bus 0 for this mpt controller. */
	bzero(&ccb, sizeof(ccb));

	ccb.ccb_h.func_code = XPT_DEV_MATCH;

	bufsize = sizeof(struct dev_match_result) * 1;
	ccb.cdm.num_matches = 0;
	ccb.cdm.match_buf_len = bufsize;
	ccb.cdm.matches = calloc(1, bufsize);

	bufsize = sizeof(struct dev_match_pattern) * 1;
	ccb.cdm.num_patterns = 1;
	ccb.cdm.pattern_buf_len = bufsize;
	ccb.cdm.patterns = calloc(1, bufsize);

	/* Match mptX bus 0. */
	ccb.cdm.patterns[0].type = DEV_MATCH_BUS;
	b = &ccb.cdm.patterns[0].pattern.bus_pattern;
	snprintf(b->dev_name, sizeof(b->dev_name), "mpt");
	b->unit_number = mpt_unit;
	b->bus_id = 0;
	b->flags = BUS_MATCH_NAME | BUS_MATCH_UNIT | BUS_MATCH_BUS_ID;

	if (ioctl(xptfd, CAMIOCOMMAND, &ccb) < 0) {
		error = errno;
		free(ccb.cdm.matches);
		free(ccb.cdm.patterns);
		return (error);
	}
	free(ccb.cdm.patterns);

	if (((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) ||
	    (ccb.cdm.status != CAM_DEV_MATCH_LAST)) {
		warnx("fetch_path_id got CAM error %#x, CDM error %d\n",
		    ccb.ccb_h.status, ccb.cdm.status);
		free(ccb.cdm.matches);
		return (EIO);
	}

	/* We should have exactly 1 match for the bus. */
	if (ccb.cdm.num_matches != 1 ||
	    ccb.cdm.matches[0].type != DEV_MATCH_BUS) {
		free(ccb.cdm.matches);
		return (ENOENT);
	}
	*path_id = ccb.cdm.matches[0].result.bus_result.path_id;
	free(ccb.cdm.matches);
	return (0);
}

int
mpt_query_disk(U8 VolumeBus, U8 VolumeID, struct mpt_query_disk *qd)
{
	struct periph_match_pattern *p;
	struct periph_match_result *r;
	union ccb ccb;
	path_id_t path_id;
	size_t bufsize;
	int error;

	/* mpt(4) only handles devices on bus 0. */
	if (VolumeBus != 0)
		return (ENXIO);

	if (xpt_open() < 0)
		return (ENXIO);

	/* Find the path ID of bus 0. */
	error = fetch_path_id(&path_id);
	if (error)
		return (error);

	bzero(&ccb, sizeof(ccb));

	ccb.ccb_h.func_code = XPT_DEV_MATCH;
	ccb.ccb_h.path_id = CAM_XPT_PATH_ID;
	ccb.ccb_h.target_id = CAM_TARGET_WILDCARD;
	ccb.ccb_h.target_lun = CAM_LUN_WILDCARD;

	bufsize = sizeof(struct dev_match_result) * 5;
	ccb.cdm.num_matches = 0;
	ccb.cdm.match_buf_len = bufsize;
	ccb.cdm.matches = calloc(1, bufsize);

	bufsize = sizeof(struct dev_match_pattern) * 1;
	ccb.cdm.num_patterns = 1;
	ccb.cdm.pattern_buf_len = bufsize;
	ccb.cdm.patterns = calloc(1, bufsize);

	/* Look for a "da" device at the specified target and lun. */
	ccb.cdm.patterns[0].type = DEV_MATCH_PERIPH;
	p = &ccb.cdm.patterns[0].pattern.periph_pattern;
	p->path_id = path_id;
	snprintf(p->periph_name, sizeof(p->periph_name), "da");
	p->target_id = VolumeID;
	p->flags = PERIPH_MATCH_PATH | PERIPH_MATCH_NAME | PERIPH_MATCH_TARGET;

	if (ioctl(xptfd, CAMIOCOMMAND, &ccb) < 0) {
		error = errno;
		free(ccb.cdm.matches);
		free(ccb.cdm.patterns);
		return (error);
	}
	free(ccb.cdm.patterns);

	if (((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) ||
	    (ccb.cdm.status != CAM_DEV_MATCH_LAST)) {
		warnx("mpt_query_disk got CAM error %#x, CDM error %d\n",
		    ccb.ccb_h.status, ccb.cdm.status);
		free(ccb.cdm.matches);
		return (EIO);
	}

	/*
	 * We should have exactly 1 match for the peripheral.
	 * However, if we don't get a match, don't print an error
	 * message and return ENOENT.
	 */
	if (ccb.cdm.num_matches == 0) {
		free(ccb.cdm.matches);
		return (ENOENT);
	}
	if (ccb.cdm.num_matches != 1) {
		warnx("mpt_query_disk got %d matches, expected 1",
		    ccb.cdm.num_matches);
		free(ccb.cdm.matches);
		return (EIO);
	}
	if (ccb.cdm.matches[0].type != DEV_MATCH_PERIPH) {
		warnx("mpt_query_disk got wrong CAM match");
		free(ccb.cdm.matches);
		return (EIO);
	}

	/* Copy out the data. */
	r = &ccb.cdm.matches[1].result.periph_result;
	snprintf(qd->devname, sizeof(qd->devname), "%s%d", r->periph_name,
	    r->unit_number);
	free(ccb.cdm.matches);

	return (0);
}

static int
periph_is_volume(CONFIG_PAGE_IOC_2 *ioc2, struct periph_match_result *r)
{
	CONFIG_PAGE_IOC_2_RAID_VOL *vol;
	int i;

	if (ioc2 == NULL)
		return (0);
	vol = ioc2->RaidVolume;
	for (i = 0; i < ioc2->NumActiveVolumes; vol++, i++) {
		if (vol->VolumeBus == 0 && vol->VolumeID == r->target_id)
			return (1);
	}
	return (0);
}

/* Much borrowed from scsireadcapacity() in src/sbin/camcontrol/camcontrol.c. */
static int
fetch_scsi_capacity(struct cam_device *dev, struct mpt_standalone_disk *disk)
{
	struct scsi_read_capacity_data rcap;
	struct scsi_read_capacity_data_long rcaplong;
	union ccb *ccb;
	int error;

	ccb = cam_getccb(dev);
	if (ccb == NULL)
		return (ENOMEM);

	/* Zero the rest of the ccb. */
	bzero(&(&ccb->ccb_h)[1], sizeof(struct ccb_scsiio) -
	    sizeof(struct ccb_hdr));

	scsi_read_capacity(&ccb->csio, 1, NULL, MSG_SIMPLE_Q_TAG, &rcap,
	    SSD_FULL_SIZE, 5000);

	/* Disable freezing the device queue */
	ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;

	if (cam_send_ccb(dev, ccb) < 0) {
		error = errno;
		cam_freeccb(ccb);
		return (error);
	}

	if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
		cam_freeccb(ccb);
		return (EIO);
	}
	cam_freeccb(ccb);

	/*
	 * A last block of 2^32-1 means that the true capacity is over 2TB,
	 * and we need to issue the long READ CAPACITY to get the real
	 * capacity.  Otherwise, we're all set.
	 */
	if (scsi_4btoul(rcap.addr) != 0xffffffff) {
		disk->maxlba = scsi_4btoul(rcap.addr);
		return (0);
	}

	/* Zero the rest of the ccb. */
	bzero(&(&ccb->ccb_h)[1], sizeof(struct ccb_scsiio) -
	    sizeof(struct ccb_hdr));

	scsi_read_capacity_16(&ccb->csio, 1, NULL, MSG_SIMPLE_Q_TAG, 0, 0, 0,
	    (uint8_t *)&rcaplong, sizeof(rcaplong), SSD_FULL_SIZE, 5000);

	/* Disable freezing the device queue */
	ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;

	if (cam_send_ccb(dev, ccb) < 0) {
		error = errno;
		cam_freeccb(ccb);
		return (error);
	}

	if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
		cam_freeccb(ccb);
		return (EIO);
	}
	cam_freeccb(ccb);

	disk->maxlba = scsi_8btou64(rcaplong.addr);
	return (0);
}

/* Borrowed heavily from scsi_all.c:scsi_print_inquiry(). */
static void
format_scsi_inquiry(struct mpt_standalone_disk *disk,
    struct scsi_inquiry_data *inq_data)
{
	char vendor[16], product[48], revision[16], rstr[12];

	if (SID_QUAL_IS_VENDOR_UNIQUE(inq_data))
		return;
	if (SID_TYPE(inq_data) != T_DIRECT)
		return;
	if (SID_QUAL(inq_data) != SID_QUAL_LU_CONNECTED)
		return;

	cam_strvis(vendor, inq_data->vendor, sizeof(inq_data->vendor),
	    sizeof(vendor));
	cam_strvis(product, inq_data->product, sizeof(inq_data->product),
	    sizeof(product));
	cam_strvis(revision, inq_data->revision, sizeof(inq_data->revision),
	    sizeof(revision));

	/* Hack for SATA disks, no idea how to tell speed. */
	if (strcmp(vendor, "ATA") == 0) {
		snprintf(disk->inqstring, sizeof(disk->inqstring),
		    "<%s %s> SATA", product, revision);
		return;
	}

	switch (SID_ANSI_REV(inq_data)) {
	case SCSI_REV_CCS:
		strcpy(rstr, "SCSI-CCS");
		break;
	case 5:
		strcpy(rstr, "SAS");
		break;
	default:
		snprintf(rstr, sizeof (rstr), "SCSI-%d",
		    SID_ANSI_REV(inq_data));
		break;
	}
	snprintf(disk->inqstring, sizeof(disk->inqstring), "<%s %s %s> %s",
	    vendor, product, revision, rstr);
}

/* Much borrowed from scsiinquiry() in src/sbin/camcontrol/camcontrol.c. */
static int
fetch_scsi_inquiry(struct cam_device *dev, struct mpt_standalone_disk *disk)
{
	struct scsi_inquiry_data *inq_buf;
	union ccb *ccb;
	int error;

	ccb = cam_getccb(dev);
	if (ccb == NULL)
		return (ENOMEM);

	/* Zero the rest of the ccb. */
	bzero(&(&ccb->ccb_h)[1], sizeof(struct ccb_scsiio) -
	    sizeof(struct ccb_hdr));

	inq_buf = calloc(1, sizeof(*inq_buf));
	if (inq_buf == NULL) {
		cam_freeccb(ccb);
		return (ENOMEM);
	}
	scsi_inquiry(&ccb->csio, 1, NULL, MSG_SIMPLE_Q_TAG, (void *)inq_buf,
	    SHORT_INQUIRY_LENGTH, 0, 0, SSD_FULL_SIZE, 5000);

	/* Disable freezing the device queue */
	ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;

	if (cam_send_ccb(dev, ccb) < 0) {
		error = errno;
		free(inq_buf);
		cam_freeccb(ccb);
		return (error);
	}

	if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
		free(inq_buf);
		cam_freeccb(ccb);
		return (EIO);
	}

	cam_freeccb(ccb);
	format_scsi_inquiry(disk, inq_buf);
	free(inq_buf);
	return (0);
}

int
mpt_fetch_disks(int fd, int *ndisks, struct mpt_standalone_disk **disksp)
{
	CONFIG_PAGE_IOC_2 *ioc2;
	struct mpt_standalone_disk *disks;
	struct periph_match_pattern *p;
	struct periph_match_result *r;
	struct cam_device *dev;
	union ccb ccb;
	path_id_t path_id;
	size_t bufsize;
	int count, error;
	uint32_t i;

	if (xpt_open() < 0)
		return (ENXIO);

	error = fetch_path_id(&path_id);
	if (error)
		return (error);

	for (count = 100;; count+= 100) {
		/* Try to fetch 'count' disks in one go. */
		bzero(&ccb, sizeof(ccb));

		ccb.ccb_h.func_code = XPT_DEV_MATCH;

		bufsize = sizeof(struct dev_match_result) * (count + 1);
		ccb.cdm.num_matches = 0;
		ccb.cdm.match_buf_len = bufsize;
		ccb.cdm.matches = calloc(1, bufsize);

		bufsize = sizeof(struct dev_match_pattern) * 1;
		ccb.cdm.num_patterns = 1;
		ccb.cdm.pattern_buf_len = bufsize;
		ccb.cdm.patterns = calloc(1, bufsize);

		/* Match any "da" peripherals. */
		ccb.cdm.patterns[0].type = DEV_MATCH_PERIPH;
		p = &ccb.cdm.patterns[0].pattern.periph_pattern;
		p->path_id = path_id;
		snprintf(p->periph_name, sizeof(p->periph_name), "da");
		p->flags = PERIPH_MATCH_PATH | PERIPH_MATCH_NAME;

		if (ioctl(xptfd, CAMIOCOMMAND, &ccb) < 0) {
			error = errno;
			free(ccb.cdm.matches);
			free(ccb.cdm.patterns);
			return (error);
		}
		free(ccb.cdm.patterns);

		/* Check for CCB errors. */
		if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
			free(ccb.cdm.matches);
			return (EIO);
		}

		/* If we need a longer list, try again. */
		if (ccb.cdm.status == CAM_DEV_MATCH_MORE) {
			free(ccb.cdm.matches);
			continue;
		}

		/* If we got an error, abort. */
		if (ccb.cdm.status != CAM_DEV_MATCH_LAST) {
			free(ccb.cdm.matches);
			return (EIO);
		}
		break;
	}

	/* Shortcut if we don't have any "da" devices. */
	if (ccb.cdm.num_matches == 0) {
		free(ccb.cdm.matches);
		*ndisks = 0;
		*disksp = NULL;
		return (0);
	}

	/* We should have N matches, 1 for each "da" device. */
	for (i = 0; i < ccb.cdm.num_matches; i++) {
		if (ccb.cdm.matches[i].type != DEV_MATCH_PERIPH) {
			warnx("mpt_fetch_disks got wrong CAM matches");
			free(ccb.cdm.matches);
			return (EIO);
		}
	}

	/*
	 * Some of the "da" peripherals may be for RAID volumes, so
	 * fetch the IOC 2 page (list of RAID volumes) so we can
	 * exclude them from the list.
	 */
	ioc2 = mpt_read_ioc_page(fd, 2, NULL);
	if (ioc2 == NULL)
		return (errno);
	disks = calloc(ccb.cdm.num_matches, sizeof(*disks));
	count = 0;
	for (i = 0; i < ccb.cdm.num_matches; i++) {
		r = &ccb.cdm.matches[i].result.periph_result;
		if (periph_is_volume(ioc2, r))
			continue;
		disks[count].bus = 0;
		disks[count].target = r->target_id;
		snprintf(disks[count].devname, sizeof(disks[count].devname),
		    "%s%d", r->periph_name, r->unit_number);

		dev = cam_open_device(disks[count].devname, O_RDWR);
		if (dev != NULL) {
			fetch_scsi_capacity(dev, &disks[count]);
			fetch_scsi_inquiry(dev, &disks[count]);
			cam_close_device(dev);
		}
		count++;
	}
	free(ccb.cdm.matches);
	free(ioc2);

	*ndisks = count;
	*disksp = disks;
	return (0);
}

/*
 * Instruct the mpt(4) device to rescan its busses to find new devices
 * such as disks whose RAID physdisk page was removed or volumes that
 * were created.  If id is -1, the entire bus is rescanned.
 * Otherwise, only devices at the specified ID are rescanned.  If bus
 * is -1, then all busses are scanned instead of the specified bus.
 * Note that currently, only bus 0 is supported.
 */
int
mpt_rescan_bus(int bus, int id)
{
	union ccb ccb;
	path_id_t path_id;
	int error;

	/* mpt(4) only handles devices on bus 0. */
	if (bus != -1 && bus != 0)
		return (EINVAL);

	if (xpt_open() < 0)
		return (ENXIO);

	error = fetch_path_id(&path_id);
	if (error)
		return (error);

	/* Perform the actual rescan. */
	bzero(&ccb, sizeof(ccb));
	ccb.ccb_h.path_id = path_id;
	if (id == -1) {
		ccb.ccb_h.func_code = XPT_SCAN_BUS;
		ccb.ccb_h.target_id = CAM_TARGET_WILDCARD;
		ccb.ccb_h.target_lun = CAM_LUN_WILDCARD;
		ccb.ccb_h.timeout = 5000;
	} else {
		ccb.ccb_h.func_code = XPT_SCAN_LUN;
		ccb.ccb_h.target_id = id;
		ccb.ccb_h.target_lun = 0;
	}
	ccb.crcn.flags = CAM_FLAG_NONE;

	/* Run this at a low priority. */
	ccb.ccb_h.pinfo.priority = 5;

	if (ioctl(xptfd, CAMIOCOMMAND, &ccb) == -1)
		return (errno);

	if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
		warnx("mpt_rescan_bus rescan got CAM error %#x\n",
		    ccb.ccb_h.status & CAM_STATUS_MASK);
		return (EIO);
	}

	return (0);
}
OpenPOWER on IntegriCloud