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
570
571
572
573
574
575
576
577
578
|
/***************************************************************************
* CVSID: $Id$
*
* hf-scsi.c : SCSI support
*
* Copyright (C) 2006, 2007 Jean-Yves Lefort <jylefort@FreeBSD.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
**************************************************************************/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <cam/cam.h>
#include <cam/cam_ccb.h>
#include <cam/scsi/scsi_all.h>
#include <cam/scsi/scsi_pass.h>
#include "../logger.h"
#include "hf-scsi.h"
#include "hf-ata.h"
#include "hf-block.h"
#include "hf-devtree.h"
#include "hf-storage.h"
#include "hf-util.h"
#define HF_SCSI_DEVICE "/dev/xpt0"
#define N_RESULTS 100
static int hf_scsi_fd;
static gboolean
hf_scsi_is_cdrom (int type)
{
return (type == T_CDROM || type == T_WORM || type == T_OPTICAL);
}
static HalDevice *
hf_scsi_bus_device_new (HalDevice *parent,
const struct bus_match_result *match)
{
HalDevice *device;
g_return_val_if_fail(match != NULL, NULL);
device = hf_device_new(parent);
hal_device_property_set_string(device, "info.subsystem", "scsi_host");
hal_device_property_set_int(device, "scsi_host.host", match->path_id);
hal_device_property_set_string(device, "info.product", "SCSI Host Adapter");
/* scsi_host_compute_udi() in linux2/classdev.c */
hf_device_set_full_udi(device, "%s_scsi_host", hal_device_property_get_string(device, "info.parent"));
return device;
}
static HalDevice *
hf_scsi_scsi_device_new (HalDevice *parent,
const struct device_match_result *match)
{
HalDevice *device;
/* buffer sizes from camcontrol.c */
char vendor[16];
char product[48];
int type;
g_return_val_if_fail(match != NULL, NULL);
device = hf_device_new(parent);
hal_device_property_set_string(device, "info.subsystem", "scsi");
hal_device_property_set_int(device, "scsi.host", match->path_id);
hal_device_property_set_int(device, "scsi.bus", match->path_id);
hal_device_property_set_int(device, "scsi.target", match->target_id);
hal_device_property_set_int(device, "scsi.lun", match->target_lun);
hal_device_property_set_string(device, "info.product", "SCSI Device");
cam_strvis(vendor, match->inq_data.vendor, sizeof(match->inq_data.vendor), sizeof(vendor));
cam_strvis(product, match->inq_data.product, sizeof(match->inq_data.product), sizeof(product));
if (*vendor)
{
hal_device_property_set_string(device, "info.vendor", vendor);
hal_device_property_set_string(device, "scsi.vendor", vendor);
}
if (*product)
hal_device_property_set_string(device, "scsi.model", product);
/* types from cam/scsi/scsi_all.h */
type = SID_TYPE(&match->inq_data);
switch (type)
{
case T_DIRECT:
case T_RBC:
/* From linux2/physdev.c, T_RBC is a Reduced Block Command device
* which is sometimes used by firewire devices */
hal_device_property_set_string(device, "scsi.type", "disk");
break;
case T_SEQUENTIAL:
hal_device_property_set_string(device, "scsi.type", "tape");
break;
case T_PRINTER:
hal_device_property_set_string(device, "scsi.type", "printer");
break;
case T_PROCESSOR:
hal_device_property_set_string(device, "scsi.type", "processor");
break;
case T_WORM:
case T_CDROM:
case T_OPTICAL:
hal_device_property_set_string(device, "scsi.type", "cdrom");
break;
case T_CHANGER:
hal_device_property_set_string(device, "scsi.type", "medium_changer");
break;
case T_SCANNER:
hal_device_property_set_string(device, "scsi.type", "scanner");
break;
case T_STORARRAY:
hal_device_property_set_string(device, "scsi.type", "array");
break;
default:
hal_device_property_set_string(device, "scsi.type", "unknown");
}
/* scsi_compute_udi() in linux2/physdev.c */
hf_device_set_full_udi(device, "%s_scsi_device_lun%i",
hal_device_property_get_string(device, "info.parent"),
hal_device_property_get_int(device, "scsi.lun"));
return device;
}
static HalDevice *
hf_scsi_block_device_new (HalDevice *parent,
const struct device_match_result *match,
const char *devname)
{
HalDevice *device;
int type;
/* buffer size from camcontrol.c */
char revision[16];
g_return_val_if_fail(HAL_IS_DEVICE(parent), NULL);
g_return_val_if_fail(match != NULL, NULL);
g_return_val_if_fail(devname != NULL, NULL);
device = hf_device_new(parent);
hf_devtree_device_set_name(device, devname);
hf_block_device_enable(device, devname);
hf_storage_device_enable(device);
type = SID_TYPE(&match->inq_data);
if (type == T_SEQUENTIAL)
hf_storage_device_enable_tape(device);
else if (hf_scsi_is_cdrom(type))
hf_storage_device_enable_cdrom(device);
if (SID_IS_REMOVABLE(&match->inq_data))
{
hal_device_property_set_bool(device, "storage.removable", TRUE);
hal_device_property_set_bool(device, "storage.media_check_enabled", TRUE);
hal_device_property_set_bool(device, "storage.removable.support_async_notification", FALSE);
}
cam_strvis(revision, match->inq_data.revision, sizeof(match->inq_data.revision), sizeof(revision));
if (hal_device_has_property(parent, "scsi.vendor"))
{
hal_device_copy_property(parent, "scsi.vendor", device, "info.vendor");
hal_device_copy_property(parent, "scsi.vendor", device, "storage.vendor");
}
if (hal_device_has_property(parent, "scsi.model"))
{
hal_device_copy_property(parent, "scsi.model", device, "info.product");
hal_device_copy_property(parent, "scsi.model", device, "storage.model");
}
if (*revision)
hal_device_property_set_string(device, "storage.firmware_revision", revision);
/*
* Walk up the device chain to find the physical device (adapted
* from hotplug_event_begin_add_blockdev() in linux2/blockdev.c).
*/
while (parent)
{
const char *bus;
const char *parent_udi;
bus = hal_device_property_get_string(parent, "info.subsystem");
if (bus)
{
if (! strcmp(bus, "scsi"))
{
hal_device_property_set_string(device, "storage.bus", "scsi");
hal_device_property_set_string(device, "storage.originating_device", hal_device_get_udi(parent));
hal_device_copy_property(parent, "scsi.lun", device, "storage.lun");
/* do not stop here, in case it's an umass device */
}
else if (! strcmp(bus, "usb"))
{
hal_device_property_set_string(device, "storage.bus", "usb");
hal_device_property_set_string(device, "storage.originating_device", hal_device_get_udi(parent));
hal_device_property_set_bool(device, "storage.hotpluggable", TRUE);
break; /* done */
}
}
parent_udi = hal_device_property_get_string(parent, "info.parent");
if (parent_udi)
{
parent = hal_device_store_find(hald_get_gdl(), parent_udi);
g_assert(parent != NULL);
}
else
parent = NULL;
}
return device;
}
static void
hf_scsi_privileged_init (void)
{
hf_scsi_fd = open(HF_SCSI_DEVICE, O_RDWR);
if (hf_scsi_fd < 0)
HAL_INFO(("unable to open %s: %s", HF_SCSI_DEVICE, g_strerror(errno)));
}
static HalDevice *
hf_scsi_get_ata_channel (HalDevice *scsi_bus)
{
HalDevice *parent;
g_return_val_if_fail(HAL_IS_DEVICE(scsi_bus), NULL);
parent = hf_device_store_get_parent(hald_get_gdl(), scsi_bus);
if (parent)
{
const char *driver;
driver = hal_device_property_get_string(parent, "freebsd.driver");
if (! driver || strcmp(driver, "ata"))
parent = NULL; /* parent is not an ATA channel */
}
return parent;
}
static HalDevice *
hf_scsi_get_atapi_device (HalDevice *ata_channel, int target_id)
{
HalDevice *device = NULL;
int ide_host;
GList *l;
g_return_val_if_fail(HAL_IS_DEVICE(ata_channel), NULL);
g_return_val_if_fail(target_id == 0 || target_id == 1, NULL); /* ATA master or slave */
ide_host = hal_device_property_get_int(ata_channel, "ide_host.number");
/*
* If there's an ATAPI device it will be in hf_ata_pending_devices,
* since we haven't called hf_ata_add_pending_devices() yet.
*/
HF_LIST_FOREACH(l, hf_ata_pending_devices)
{
HalDevice *child = HAL_DEVICE(l->data);
HalDevice *parent;
const char *driver;
const char *phys_device;
int host;
int channel;
driver = hal_device_property_get_string(child, "freebsd.driver");
/* ATAPI devices: CD-ROM (acd), tape (ast) or floppy (afd) */
if (! driver || (strcmp(driver, "acd") && strcmp(driver, "ast") && strcmp(driver, "afd")))
continue;
phys_device = hal_device_property_get_string(child, "storage.originating_device");
if (! phys_device)
continue;
parent = hal_device_store_find(hald_get_gdl(), phys_device);
if (! parent)
continue;
host = hal_device_property_get_int(parent, "ide.host");
if (host != ide_host)
continue;
channel = hal_device_property_get_int(parent, "ide.channel");
if (target_id == channel)
{
device = child;
break;
}
}
return device;
}
static void
hf_scsi_handle_pending_device (struct device_match_result **match,
char **devname)
{
g_return_if_fail(match != NULL);
g_return_if_fail(devname != NULL);
if (*match && *devname)
{
HalDevice *parent;
parent = hal_device_store_match_key_value_int(hald_get_gdl(), "scsi_host.host", (*match)->path_id);
if (! parent || ! hal_device_property_get_bool(parent, "info.ignore"))
{
HalDevice *atapi_device = NULL;
HalDevice *scsi_device;
int type;
/*
* If the device's parent (the SCSI bus) is an atapicam(4)
* bus, we shall ignore the corresponding ATAPI device
* unless this device was ignored. This is to ensure that
* the same physical device will only be handled once
* (through ATA or SCSI).
*/
if (parent)
{
HalDevice *ata_channel;
ata_channel = hf_scsi_get_ata_channel(parent);
if (ata_channel)
{
atapi_device = hf_scsi_get_atapi_device(ata_channel, (*match)->target_id);
if (atapi_device)
{
char *cam_devname;
char *scsi_path;
cam_devname = g_strdup_printf("/dev/%s", *devname);
scsi_path = g_strdup_printf("%d,%d,%d", (*match)->path_id, (*match)->target_id, (*match)->target_lun);
hal_device_property_set_string(atapi_device, "block.freebsd.atapi_cam_device", cam_devname);
hal_device_property_set_string(atapi_device, "block.freebsd.cam_path", scsi_path);
g_free(cam_devname);
g_free(scsi_path);
}
}
}
type = SID_TYPE(&(*match)->inq_data);
scsi_device = hf_scsi_scsi_device_new(parent, *match);
if (hf_device_preprobe_and_add(scsi_device) && (type == T_DIRECT ||
type == T_SEQUENTIAL || type == T_RBC || type == T_STORARRAY ||
hf_scsi_is_cdrom(type)))
{
HalDevice *block_device;
char *scsi_path;
block_device = hf_scsi_block_device_new(scsi_device, *match, *devname);
scsi_path = g_strdup_printf("%d,%d,%d", (*match)->path_id, (*match)->target_id, (*match)->target_lun);
hal_device_property_set_string(block_device, "block.freebsd.cam_path", scsi_path);
g_free(scsi_path);
if (hf_device_preprobe(block_device))
{
/*
* The device was not ignored. If there is a
* corresponding ATAPI device, ignore it (see above
* comment).
*/
if (atapi_device)
hal_device_property_set_bool(atapi_device, "info.ignore", TRUE);
hf_runner_run_sync(block_device, 0, "hald-probe-scsi", NULL);
/*
* hald-probe-scsi might have set storage.serial,
* which is used in the UDI computed in
* hf_block_device_complete().
*/
hf_block_device_complete(block_device, block_device, FALSE);
hf_device_add(block_device);
hf_storage_device_probe(block_device, FALSE);
}
}
}
}
g_free(*match);
*match = NULL;
g_free(*devname);
*devname = NULL;
}
/* inspired by getdevtree() in sbin/camcontrol/camcontrol.c */
static void
hf_scsi_probe (void)
{
union ccb ccb;
struct device_match_result *pending_device = NULL;
char *pending_devname = NULL;
if (hf_scsi_fd < 0)
goto end; /* already warned in hf_scsi_init() */
memset(&ccb, 0, sizeof(ccb));
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;
ccb.ccb_h.func_code = XPT_DEV_MATCH;
ccb.cdm.match_buf_len = sizeof(struct dev_match_result) * N_RESULTS;
ccb.cdm.matches = g_new(struct dev_match_result, N_RESULTS);
ccb.cdm.num_matches = 0;
ccb.cdm.num_patterns = 0;
ccb.cdm.pattern_buf_len = 0;
do
{
int i;
if (ioctl(hf_scsi_fd, CAMIOCOMMAND, &ccb) < 0)
{
HAL_WARNING(("unable to get list of SCSI devices: %s", g_strerror(errno)));
break;
}
if (ccb.ccb_h.status != CAM_REQ_CMP
|| (ccb.cdm.status != CAM_DEV_MATCH_LAST
&& ccb.cdm.status != CAM_DEV_MATCH_MORE))
{
HAL_WARNING(("got CAM error %#x, CDM error %d", ccb.ccb_h.status, ccb.cdm.status));
break;
}
for (i = 0; i < (int) ccb.cdm.num_matches; i++)
switch (ccb.cdm.matches[i].type)
{
case DEV_MATCH_BUS:
{
struct bus_match_result *match;
HalDevice *device;
HalDevice *parent = NULL;
match = &ccb.cdm.matches[i].result.bus_result;
if ((int) match->path_id == -1)
break;
hf_scsi_handle_pending_device(&pending_device, &pending_devname);
device = hal_device_store_match_key_value_int(hald_get_gdl(), "scsi_host.host", match->path_id);
if (device)
break; /* device already exists */
if (! strcmp(match->dev_name, "umass-sim"))
{
parent = hf_devtree_find_from_info(hald_get_gdl(), "umass", match->unit_number);
/* make it a child of the mass storage interface */
if (parent)
{
HalDevice *parent_if;
parent_if = hf_device_store_match(hald_get_gdl(),
"info.parent", HAL_PROPERTY_TYPE_STRING, hal_device_get_udi(parent),
"usb.interface.class", HAL_PROPERTY_TYPE_INT32, 0x08,
NULL);
if (parent_if)
parent = parent_if;
}
}
else if (! strcmp(match->dev_name, "ata")) /* ATAPI/CAM */
parent = hf_devtree_find_from_info(hald_get_gdl(), "ata", match->unit_number);
if (! parent || ! hal_device_property_get_bool(parent, "info.ignore"))
{
device = hf_scsi_bus_device_new(parent, match);
hf_device_preprobe_and_add(device);
}
}
break;
case DEV_MATCH_DEVICE:
{
struct device_match_result *match;
HalDevice *device;
match = &ccb.cdm.matches[i].result.device_result;
if ((int) match->path_id == -1)
break;
hf_scsi_handle_pending_device(&pending_device, &pending_devname);
device = hf_device_store_match(hald_get_gdl(),
"scsi.bus", HAL_PROPERTY_TYPE_INT32, match->path_id,
"scsi.target", HAL_PROPERTY_TYPE_INT32, match->target_id,
"scsi.lun", HAL_PROPERTY_TYPE_INT32, match->target_lun,
NULL);
if (device)
break; /* device already exists */
pending_device = g_new(struct device_match_result, 1);
*pending_device = *match;
}
break;
case DEV_MATCH_PERIPH:
{
struct periph_match_result *match;
if (! pending_device)
break;
if (pending_devname)
break; /* only use the first peripheral */
match = &ccb.cdm.matches[i].result.periph_result;
if ((int) match->path_id == -1 || ! strcmp(match->periph_name, "pass") || ! strcmp(match->periph_name, "probe"))
break;
pending_devname = g_strdup_printf("%s%i", match->periph_name, match->unit_number);
}
break;
}
}
while (ccb.ccb_h.status == CAM_REQ_CMP && ccb.cdm.status == CAM_DEV_MATCH_MORE);
hf_scsi_handle_pending_device(&pending_device, &pending_devname);
g_free(ccb.cdm.matches);
end:
/*
* Now that we have probed the SCSI devices and ignored the ATAPI
* devices as necessary, it is time to add the pending ATA block
* devices.
*/
hf_ata_add_pending_devices();
}
HFHandler hf_scsi_handler = {
.privileged_init = hf_scsi_privileged_init,
.probe = hf_scsi_probe
};
|