Chinaunix首页 | 论坛 | 博客
  • 博客访问: 404414
  • 博文数量: 120
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 741
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-27 18:15
文章分类

全部博文(120)

文章存档

2016年(13)

2015年(41)

2014年(66)

我的朋友

分类: LINUX

2016-01-21 13:46:45

一、支持硬盘驱动的内核;
有些U盘,使用了SSD级别的主控(或者量产时指定为固定磁盘),接入到电脑上,识别成本地硬盘,例如sandisk的Cuzer Blade某些型号的U盘,文中常规U盘是指识别成可移动磁盘的U盘,本地硬盘U盘是指识别成本地硬盘的U盘;
1、先常规U盘,后本地硬盘U盘
usb 1-1: new high-speed USB device number 2 using hiusb-ehci
scsi1 : usb-storage 1-1:1.0
usbdev11 -> /dev/usbdev1.2
scsi 1:0:0:0: Direct-Access     TOSHIBA  TransMemory      1.00 PQ: 0 ANSI: 4
sd 1:0:0:0: [sda] 30297216 512-byte logical blocks: (15.5 GB/14.4 GiB)
sd 1:0:0:0: [sda] Write Protect is off
sd 1:0:0:0: [sda] Write cache: disabled, read cache: enabled, doesn't support DPO or FUA
 sda: sda1
sd 1:0:0:0: [sda] Attached SCSI removable disk
udisk1110 -> /dev/sda
udisk1110p1 -> /dev/sda1


usb 1-3: new high-speed USB device number 3 using hiusb-ehci
scsi2 : usb-storage 1-3:1.0
usbdev13 -> /dev/usbdev1.3
scsi 2:0:0:0: Direct-Access     SanDisk  Cruzer Blade     2.01 PQ: 0 ANSI: 6
sd 2:0:0:0: [sdb] 7821312 512-byte logical blocks: (4.00 GB/3.72 GiB)
sd 2:0:0:0: [sdb] Write Protect is off
sd 2:0:0:0: [sdb] Write cache: disabled, read cache: enabled, doesn't support DPO or FUA
 sdb: sdb1
sd 2:0:0:0: [sdb] Attached SCSI disk
udisk1310 -> /dev/sdb
udisk1310p1 -> /dev/sdb1


============================================================================================================


2、先本地硬盘U盘,后常规U盘
usb 1-1: new high-speed USB device number 2 using hiusb-ehci
scsi1 : usb-storage 1-1:1.0
usbdev11 -> /dev/usbdev1.2
scsi 1:0:0:0: Direct-Access     SanDisk  Cruzer Blade     2.01 PQ: 0 ANSI: 6
sd 1:0:0:0: [sda] 7821312 512-byte logical blocks: (4.00 GB/3.72 GiB)
sd 1:0:0:0: [sda] Write Protect is off
sd 1:0:0:0: [sda] Write cache: disabled, read cache: enabled, doesn't support DPO or FUA
 sda: sda1
sd 1:0:0:0: [sda] Attached SCSI disk
udisk1110 -> /dev/sda
udisk1110p1 -> /dev/sda1


usb 1-3: new high-speed USB device number 3 using hiusb-ehci
scsi2 : usb-storage 1-3:1.0
usbdev13 -> /dev/usbdev1.3
scsi 2:0:0:0: Direct-Access     TOSHIBA  TransMemory      1.00 PQ: 0 ANSI: 4
sd 2:0:0:0: [sdb] 30297216 512-byte logical blocks: (15.5 GB/14.4 GiB)
sd 2:0:0:0: [sdb] Write Protect is off
sd 2:0:0:0: [sdb] Write cache: disabled, read cache: enabled, doesn't support DPO or FUA
 sdb: sdb1
sd 2:0:0:0: [sdb] Attached SCSI removable disk
udisk1310 -> /dev/sdb
udisk1310p1 -> /dev/sdb1


============================================================================================================


3、均常规U盘
usb 1-1: new high-speed USB device number 2 using hiusb-ehci
scsi1 : usb-storage 1-1:1.0
usbdev11 -> /dev/usbdev1.2
scsi 1:0:0:0: Direct-Access     Kingston DataTraveler 3.0 PMAP PQ: 0 ANSI: 6
sd 1:0:0:0: [sda] 30720000 512-byte logical blocks: (15.7 GB/14.6 GiB)
sd 1:0:0:0: [sda] Write Protect is off
sd 1:0:0:0: [sda] No Caching mode page found
sd 1:0:0:0: [sda] Assuming drive cache: write through
sd 1:0:0:0: [sda] No Caching mode page found
sd 1:0:0:0: [sda] Assuming drive cache: write through
 sda: sda1
sd 1:0:0:0: [sda] No Caching mode page found
sd 1:0:0:0: [sda] Assuming drive cache: write through
sd 1:0:0:0: [sda] Attached SCSI removable disk
udisk1110 -> /dev/sda
udisk1110p1 -> /dev/sda1


usb 1-3: new high-speed USB device number 3 using hiusb-ehci
scsi2 : usb-storage 1-3:1.0
usbdev13 -> /dev/usbdev1.3
scsi 2:0:0:0: Direct-Access     TOSHIBA  TransMemory      1.00 PQ: 0 ANSI: 4
sd 2:0:0:0: [sdb] 30297216 512-byte logical blocks: (15.5 GB/14.4 GiB)
sd 2:0:0:0: [sdb] Write Protect is off
sd 2:0:0:0: [sdb] Write cache: disabled, read cache: enabled, doesn't support DPO or FUA
 sdb: sdb1
sd 2:0:0:0: [sdb] Attached SCSI removable disk
udisk1310 -> /dev/sdb
udisk1310p1 -> /dev/sdb1


4、前面三种情况下,插硬盘
ata1: exception Emask 0x10 SAct 0x0 SErr 0x4040000 action 0xe frozen
ata1: irq_stat 0x00000040, connection status changed
ata1: SError: { CommWake DevExch }
ata1: hard resetting link
ata1: SATA link up 3.0 Gbps (SStatus 123 SControl 300)
ata1.00: ATA-8: WDC WD5000AAKX-221CA1, 17.01H17, max UDMA/133
ata1.00: 976773168 sectors, multi 0: LBA48 NCQ (depth 0/32)
ata1.00: configured for UDMA/133
ata1: EH complete
scsi 0:0:0:0: Direct-Access     ATA      WDC WD5000AAKX-2 17.0 PQ: 0 ANSI: 5
sd 0:0:0:0: [sdb] 976773168 512-byte logical blocks: (500 GB/465 GiB)
sd 0:0:0:0: [sdb] Write Protect is off
sd 0:0:0:0: [sdb] Write cache: enabled, read cache: enabled, doesn't support DPO or FUA
 sdb: sdb1 sdb2 < sdb5 sdb6 >
sd 0:0:0:0: [sdb] Attached SCSI disk


二、不支持硬盘驱动的内核;
1、先常规U盘,后本地硬盘U盘
usb 1-1: new high-speed USB device number 2 using hiusb-ehci
scsi0 : usb-storage 1-1:1.0----(hosts.c->scsi_add_host_with_dma)
usbdev11 -> /dev/usbdev1.2
scsi 0:0:0:0: Direct-Access     TOSHIBA  TransMemory      1.00 PQ: 0 ANSI: 4
sd 0:0:0:0: [sda] 30297216 512-byte logical blocks: (15.5 GB/14.4 GiB)
sd 0:0:0:0: [sda] Write Protect is off
sd 0:0:0:0: [sda] Write cache: disabled, read cache: enabled, doesn't support DPO or FUA
 sda: sda1
sd 0:0:0:0: [sda] Attached SCSI removable disk
udisk1110 -> /dev/sda
udisk1110p1 -> /dev/sda1


usb 1-3: new high-speed USB device number 3 using hiusb-ehci
scsi1 : usb-storage 1-3:1.0
usbdev13 -> /dev/usbdev1.3
scsi 1:0:0:0: Direct-Access     SanDisk  Cruzer Blade     2.01 PQ: 0 ANSI: 6
sd 1:0:0:0: [sdb] 7821312 512-byte logical blocks: (4.00 GB/3.72 GiB)
sd 1:0:0:0: [sdb] Write Protect is off
sd 1:0:0:0: [sdb] Write cache: disabled, read cache: enabled, doesn't support DPO or FUA
 sdb: sdb1
sd 1:0:0:0: [sdb] Attached SCSI disk
udisk1310 -> /dev/sdb
udisk1310p1 -> /dev/sdb1


============================================================================================================


2、先本地硬盘U盘,后常规U盘
usb 1-1: new high-speed USB device number 2 using hiusb-ehci
scsi0 : usb-storage 1-1:1.0
usbdev11 -> /dev/usbdev1.2
scsi 0:0:0:0: Direct-Access     SanDisk  Cruzer Blade     2.01 PQ: 0 ANSI: 6
sd 0:0:0:0: [sda] 7821312 512-byte logical blocks: (4.00 GB/3.72 GiB)
sd 0:0:0:0: [sda] Write Protect is off
sd 0:0:0:0: [sda] Write cache: disabled, read cache: enabled, doesn't support DPO or FUA
 sda: sda1
sd 0:0:0:0: [sda] Attached SCSI disk
udisk1110 -> /dev/sda
udisk1110p1 -> /dev/sda1


usb 1-3: new high-speed USB device number 3 using hiusb-ehci
scsi1 : usb-storage 1-3:1.0
usbdev13 -> /dev/usbdev1.3
scsi 1:0:0:0: Direct-Access     TOSHIBA  TransMemory      1.00 PQ: 0 ANSI: 4
sd 1:0:0:0: [sdb] 30297216 512-byte logical blocks: (15.5 GB/14.4 GiB)
sd 1:0:0:0: [sdb] Write Protect is off
sd 1:0:0:0: [sdb] Write cache: disabled, read cache: enabled, doesn't support DPO or FUA
 sdb: sdb1
sd 1:0:0:0: [sdb] Attached SCSI removable disk
udisk1310 -> /dev/sdb
udisk1310p1 -> /dev/sdb1


3、均常规U盘
usb 1-1: new high-speed USB device number 2 using hiusb-ehci
scsi0 : usb-storage 1-1:1.0----(hosts.c->scsi_add_host_with_dma)
usbdev11 -> /dev/usbdev1.2
scsi 0:0:0:0: Direct-Access     Kingston DataTraveler 3.0 PMAP PQ: 0 ANSI: 6
sd 0:0:0:0: [sda] 30720000 512-byte logical blocks: (15.7 GB/14.6 GiB)
sd 0:0:0:0: [sda] Write Protect is off
sd 0:0:0:0: [sda] No Caching mode page found
sd 0:0:0:0: [sda] Assuming drive cache: write through
sd 0:0:0:0: [sda] No Caching mode page found
sd 0:0:0:0: [sda] Assuming drive cache: write through
 sda: sda1
sd 0:0:0:0: [sda] No Caching mode page found
sd 0:0:0:0: [sda] Assuming drive cache: write through
sd 0:0:0:0: [sda] Attached SCSI removable disk
udisk1110 -> /dev/sda
udisk1110p1 -> /dev/sda1


usb 1-3: new high-speed USB device number 3 using hiusb-ehci
scsi1 : usb-storage 1-3:1.0
usbdev13 -> /dev/usbdev1.3
scsi 1:0:0:0: Direct-Access     TOSHIBA  TransMemory      1.00 PQ: 0 ANSI: 4
sd 1:0:0:0: [sdb] 30297216 512-byte logical blocks: (15.5 GB/14.4 GiB)
sd 1:0:0:0: [sdb] Write Protect is off
sd 1:0:0:0: [sdb] Write cache: disabled, read cache: enabled, doesn't support DPO or FUA
 sdb: sdb1
sd 1:0:0:0: [sdb] Attached SCSI removable disk
udisk1310 -> /dev/sdb
udisk1310p1 -> /dev/sdb1


硬盘驱动
ata/ahci_platform.c:
ahci_init->
ahci_probe->
ata_host_activate(host, irq, ahci_interrupt, IRQF_SHARED, &ahci_platform_sht);插拔硬盘,触发中断,调用ahci_interrupt
ahci_interrupt->


插硬盘时的打印信息分析:
ata1: exception Emask 0x10 SAct 0x0 SErr 0x4040000 action 0xe frozen----ata_eh_link_report()
ata1: irq_stat 0x00000040, connection status changed----ahci_error_intr()
ata1: SError: { CommWake DevExch }
ata1: hard resetting link----libata-eh.c(ata_do_eh->ata_eh_recover->ata_eh_reset)
....等待一会儿
ata1: SATA link up 3.0 Gbps (SStatus 123 SControl 300)
ata1.00: ATA-8: WDC WD5000AAKX-221CA1, 17.01H17, max UDMA/133
ata1.00: 976773168 sectors, multi 0: LBA48 NCQ (depth 0/32)
ata1.00: configured for UDMA/133
ata1: EH complete
scsi 0:0:0:0: Direct-Access     ATA      WDC WD5000AAKX-2 17.0 PQ: 0 ANSI: 5
sd 0:0:0:0: [sdb] 976773168 512-byte logical blocks: (500 GB/465 GiB)
sd 0:0:0:0: [sdb] Write Protect is off
sd 0:0:0:0: [sdb] Write cache: enabled, read cache: enabled, doesn't support DPO or FUA
 sdb: sdb1 sdb2 < sdb5 sdb6 >
sd 0:0:0:0: [sdb] Attached SCSI disk


拔硬盘:
ata1: exception Emask 0x10 SAct 0x0 SErr 0x90100 action 0xe frozen
ata1: irq_stat 0x00400000, PHY RDY changed
ata1: SError: { UnrecovData PHYRdyChg 10B8B }
ata1: hard resetting link----libata-eh.c(ata_do_eh->ata_eh_recover->ata_eh_reset)
ata1: SATA link down (SStatus 0 SControl 300)
ata1: hard resetting link----libata-eh.c(ata_do_eh->ata_eh_recover->ata_eh_reset)
ata1: SATA link down (SStatus 0 SControl 300)
ata1: limiting SATA link speed to 1.5 Gbps----(ata_eh_reset->libata-core.c->sata_down_spd_limit)
ata1: hard resetting link----libata-eh.c(ata_do_eh->ata_eh_recover->ata_eh_reset)
ata1: SATA link down (SStatus 0 SControl 310)
ata1.00: disabled
ata1: EH complete
ata1.00: detaching (SCSI 0:0:0:0)----(libata-scsi.c->ata_scsi_hotplug->ata_scsi_handle_link_detach->ata_scsi_remove_dev)
sd 0:0:0:0: [sda] Synchronizing SCSI cache
sd 0:0:0:0: [sda]  Result: hostbyte=0x04 driverbyte=0x00
sd 0:0:0:0: [sda] Stopping disk
sd 0:0:0:0: [sda] START_STOP FAILED
sd 0:0:0:0: [sda]  Result: hostbyte=0x04 driverbyte=0x00


三、分析硬盘和usb驱动的加载过程
1.为什么硬盘的scsi id始终为0?
内核中,硬盘、usb口都使用了scsi总线,也就是说硬盘和U盘接入之后,在scsi总线上都有一个id;
内核编译后,从System.map可以看出,__initcall_ahci_init6在__initcall_usb_stor_init6之前,也就是说先挂载硬盘驱动,后挂载usb驱动
硬盘驱动挂载时,占用了scsi 0,并且不释放;此时usb就从1开始了;如果内核不支持硬盘,那么usb的scsi就从0开始;
内核支持硬盘的配置如下:
CONFIG_HI_SATA=y
CONFIG_HI_SATA_IOBASE=0xF9900000
CONFIG_HI_SATA_IOSIZE=0x00010000
CONFIG_HI_SATA_IRQNUM=102
CONFIG_ATA=y
# CONFIG_ATA_NONSTANDARD is not set
CONFIG_ATA_VERBOSE_ERROR=y
CONFIG_SATA_PMP=y


#
# Controllers with non-SFF native interface
#
CONFIG_SATA_AHCI_PLATFORM=y


2、为什么插拔硬盘,硬盘的scsi始终为0,而usb的随着插拔,不断增加?
硬盘驱动先于U盘驱动挂载,所以为0,而插拔硬盘并不会重新执行scsi_add_host_with_dma,也不会移除增加的host,所以一直为0,只是通过中断来更新硬盘的状态;
U盘插入时,调用scsi_add_host_with_dma增加设备,拔掉时会执行scsi_remove_host删除host,所以会一直递增;


3、硬盘驱动挂载简析
(ahci_platform.c)ahci_init->
ahci_probe->
ata_host_activate(host, irq, ahci_interrupt, IRQF_SHARED,&ahci_platform_sht);


ata_host_activate->
ata_host_register->
ata_scsi_add_hosts->
scsi_host_alloc->"shost->host_no = atomic_inc_return(&scsi_host_next_hn) - 1;"//分配scsi id
scsi_add_host_with_dma

4、U盘驱动挂载简析
(storage/usb.c)usb_stor_init->
storage_probe->
usb_stor_probe1->
scsi_host_alloc->"shost->host_no = atomic_inc_return(&scsi_host_next_hn) - 1;"//分配scsi id
usb_stor_probe2->
scsi_add_host->
scsi_add_host_with_dma
拔掉U盘:
(storage/usb.c)usb_stor_disconnect->
quiesce_and_remove_host->
scsi_remove_host(host);

四、区分U盘和硬盘
1、硬盘通过sata口连接到系统时,此时linux识别为SCSI disk;
2、U盘连接到系统时:
如果U盘是采用的ssd的主控,或者量产时指定为固定硬盘,那么将会被识别成SCSI disk;如sandisk的Cruzer Blade某些型号的U盘;
一般情况下,识别成SCSI removable disk;
不管U盘识别成什么类型的disk,总是生成/proc/scsi/usb-storage/目录,该目录下面即为U盘的描述信息
3、查看scsi总线的设备;cat /proc/scsi/scsi;ex:
Attached devices:
Host: scsi1 Channel: 00 Id: 00 Lun: 00
  Vendor: TOSHIBA  Model: TransMemory      Rev: 1.00
  Type:   Direct-Access                    ANSI  SCSI revision: 04
Host: scsi2 Channel: 00 Id: 00 Lun: 00
  Vendor: SanDisk  Model: Cruzer Blade     Rev: 2.01
  Type:   Direct-Access                    ANSI  SCSI revision: 06
Host: scsi0 Channel: 00 Id: 00 Lun: 00
  Vendor: ATA      Model: WDC WD5000AAKX-2 Rev: 17.0
  Type:   Direct-Access                    ANSI  SCSI revision: 05


4、查看scsi上连接的U盘
# cat /proc/scsi/usb-storage/
/proc/scsi/usb-storage/1  /proc/scsi/usb-storage/2
# cat /proc/scsi/usb-storage/1 
   Host scsi1: usb-storage
       Vendor: TOSHIBA 
      Product: TransMemory     
Serial Number: 2602E3127D03CD6057F0AAB4
     Protocol: Transparent SCSI
    Transport: Bulk
       Quirks:
# cat /proc/scsi/usb-storage/2 
   Host scsi2: usb-storage
       Vendor: SanDisk
      Product: Cruzer Blade
Serial Number: 2005445681189CD274A3
     Protocol: Transparent SCSI
    Transport: Bulk
       Quirks: SANE_SENSE


5、查看scsi总线上连接的设备的block,设备名字由(%d:%d:%d:%d)host_no:channel:id:lun组成
# ls /sys/bus/scsi/devices/0\:0\:0\:0/block/
sdc
# ls /sys/bus/scsi/devices/1\:0\:0\:0/block/
sda
# ls /sys/bus/scsi/devices/2\:0\:0\:0/block/
sdb
       
6、查看scsi总线上连接的设备是否可移动
# cat /sys/block/sdc/removable 
0(sata连接的硬盘不可移动)
# cat /sys/block/sda/removable 
1(常规U盘可移动)
# cat /sys/block/sdb/removable 
0(识别成本地磁盘的U盘,不可移动)


7、区分U盘和硬盘的逻辑判断
(1)根据cat /proc/scsi/scsi的结果,获取到host_no:channel:id:lun;并按照(%d:%d:%d:%d)格式,组成相应的设备名
(2)获取cat /proc/scsi/usb-storage/设备号,该目录下的设备,为可移动磁盘
(3)第2步结果减去第1步结果,即为硬盘
(4)通过执行ls /sys/bus/scsi/devices/设备名/block/,得到各自的分区
(5)然后就可以根据需求,挂载不同的分区,并执行相应的动作
       
阅读(5135) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~