Giuseppe Corbelli wrote:
> 
> On Thu, 3 Aug 2000, Douglas Gilbert wrote:
> > >
> > > is there a way in user-space to map from the dev_t ( retrive from
> > > stat(2) ) to the scsi target & logical ids?
> > > if no can i do it in kernel-space ?
> >
> > With an open file descriptor to a SCSI device the
> > SCSI_IOCTL_GET_IDLUN ioctl() can be used.
> > The struct associated with this ioctl() is
> > described in:
> > http://www.torque.net/scsi/linux_scsi_24
> >
> > You may also be able to deduce it from looking at
> > 'cat /proc/scsi/scsi' [but that is a bit hard to
> > parse].
> I'd like to "extend the original question" :-)
> How can I translate between BUS,ID,LUN <-> sg <-> sr (hd)?
> Of course I should take a look at cdrecord's source but it's too hard for
> me without documentation. Forgive me :-)

Giuseppe,
Here is a more long-winded reply that may help others ...

The answer depends on what size of "hammer" you wish to 
use on this problem. 

At one extreme is the device pseudo file system 
(devfs) which is included in 2.4 kernels (but not 
usually the default). It makes both the 
host/channel/target/lun hierarchy and (when
devfsd is used) the linux traditional "sda"-like
device names available. To get your mapping may
require a fair amount of directory scanning.


Next there is scsidev ( see
http://www.garloff.de/kurt/linux/scsidev ). Once this
utility is run it adds a /dev/scsi directory that
looks like this on my system:

$ ls -l /dev/scsi
brw-------    1 root     root       8,   0 Aug 12 11:04 sdh1-0c0i0l0
crw-------    1 root     root      21,   0 Aug 12 10:48 sgh1-0c0i0l0
crw-------    1 root     root      21,   1 Aug 12 10:48 sgh2-0c0i2l0
crw-------    1 root     root      21,   3 Aug 12 11:04 sgh2-0c0i5l0
crw-------    1 root     root      21,   2 Aug 12 10:48 sgh2-0c0i6l0
br--------    1 root     root      11,   0 Aug 12 10:48 srh2-0c0i2l0
br--------    1 root     root      11,   1 Aug 12 10:48 srh2-0c0i6l0

This time a relatively simple directory scan should
answer your question. [Even though devfs introduces
a /dev/scsi directory, it and scsidev do seem to
happily co-exist.]


Then there is the SCSI_IOCTL_GET_IDLUN ioctl() that
I mentioned in the first reply. It does some awkward
bit stuffing and be careful with the host number
(because in lk 2.2 and earlier you will need to call
the additional SCSI_IOCTL_GET_BUS_NUMBER ioctl() to
get that number). It can applied to all SCSI device
names.

 
Finally the sg driver tries to make this a bit simpler
with the SG_GET_SCSI_ID ioctl(). See sg's documentation.
It should be simple enough to see what it does from
/usr/src/linux/include/scsi/sg.h :
typedef struct sg_scsi_id {
    int host_no;        /* as in "scsi<n>" where 'n' is one of 0, 1, 2 etc */
    int channel;
    int scsi_id;        /* scsi id of target device */
    int lun;
    int scsi_type;      /* TYPE_... defined in scsi/scsi.h */
    /* ...... */
} Sg_scsi_id;


For examples of this see the sg web site and the
sg utilities. The best example is a program called
sg_map (another is sg_scan). The output of sg_map
for my system looks like:

$ sg_map 
/dev/sg0  /dev/sda
/dev/sg1  /dev/sr0
/dev/sg2  /dev/sr1
/dev/sg3

Note that /dev/sg3 doesn't "map" because it is a scanner.
With this extra "x" option this becomes:

$ sg_map -x
/dev/sg0  1 0 0 0  0  /dev/sda
/dev/sg1  2 0 2 0  5  /dev/sr0
/dev/sg2  2 0 6 0  5  /dev/sr1
/dev/sg3  2 0 5 0  6

The numbers are host_number, channel, id, lun and
scsi device type (where "6" is a scanner).
With the sg driver in lk 2.4 similar information is
provided via the "proc" file system:

$ cd /proc/scsi/sg ;  cat device_hdr devices
host    chan    id      lun     type    bopens  qdepth  busy
1       0       0       0       0       4       63      3
2       0       2       0       5       0       4       0
2       0       6       0       5       0       4       0
2       0       5       0       6       0       4       0


Hope this helps.

Doug Gilbert

-
To unsubscribe from this list: send the line "unsubscribe linux-scsi" in
the body of a message to [EMAIL PROTECTED]

Reply via email to