07.06.2013 Views

Scsi - Index of

Scsi - Index of

Scsi - Index of

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

through to the most significant bit (MSb) last. In the following diagrams, packets are<br />

displayed such that both individual bits and fields are represented (in a left to right reading<br />

order) as they would move across the bus. Multiple byte fields in standard descriptors,<br />

requests, and responses are interpreted as and moved over the bus in little-endian order,<br />

i.e.,<br />

LSB to MSB.)<br />

而与此同时,在灯火阑珊处,我们也依稀记得,在那份名为SCSI Primary Commands-4 的规范,即那份<br />

在江湖上有着SCSI 葵花宝典之美誉的SPC-4 中,第三章,3.5, Bit and byte ordering 那一段,是这般描<br />

述的: If a field consists <strong>of</strong> more than one byte and contains a single value, the byte containing<br />

the MSB is stored at the lowest address and the byte containing the LSB is stored at the<br />

highest address (i.e., big-endian byte ordering).所以request_buffer 回来的数据是采用be 的字<br />

节序,因此我们这里的c 要通过be32_to_cpu()转换才能变成cpu 使用的结果.反过来,当我们再次copy<br />

回request_buffer 中的时候,要再使用cpu_to_be32()给转回去.<br />

最后如果你还要问,为什么要采用两种字节序?多麻烦啊?那我没什么可说的,你问上帝问真主问释迦牟尼<br />

去吧,也许他们能告诉你,不管白老鼠黑老鼠,只要不给猫逮住的就是好老鼠.<br />

有多少爱可以胡来?(一)<br />

上帝给了每个人一支书写人生的铅笔,却未曾给我们橡皮擦.但计算机的世界却并非如此,电脑用着用着觉<br />

得不正常了,按一下reset 键就一切ok 了. (当然你要是中了熊猫烧香啊中了冲击波啥的病毒那就另当别<br />

论<br />

183<br />

了,喂,别打岔行不行,我们讲正事呢.)如果人生也可以这样,那么星爷的那段经典的妇孺皆知的”人世间最悲<br />

哀的……假如……”的对白恐怕就没有意义了.<br />

在驱动程序中,一个非常非常重要的概念就是错误处理.生活不是林黛玉,不会因为忧伤而风情万种,写代码<br />

不是写小说,不会因为作者的构思完美而天衣无缝.所以我们来看看在usb-storage 中,我们是如何来进行<br />

错误处理的.<br />

一切都得从那个结构体变量struct scsi_host_template usb_stor_host_template 开始说起.其实这<br />

个结构体正是我们和scsi 核心层最最关键的接口. 我们知道这个结构体有很多内容, 我们为<br />

usb_stor_host_template 的许多成员赋了值,但是显然很多我们都没有讲.那么现在是时候去讲了.这其<br />

中,我们不妨把与错误处理相关的三个成员给揪出来.这些函数都是我们自己定义的,提供给scsi core 那边<br />

去调用,就好像queuecommand()一样.<br />

430 /* error and abort handlers */<br />

431 .eh_abort_handler = command_abort,<br />

432 .eh_device_reset_handler = device_reset,<br />

433 .eh_bus_reset_handler = bus_reset,<br />

好,让我们一个一个来看.先看两个与reset 相关的函数,网友”要挑熟女”问我为什么有两个reset 函数,很<br />

简单,当你的电脑有点小毛病了之后,你可以有两种选择,一种是注销就可以了,一种是重起才可<br />

以.device_reset 在这里对应的就是注销,bus_reset 对应的就是重起.当然这样说并不严谨,只能说,一般<br />

来说轻微一点,device_reset 就够了,如果严重一点,眼看着设备病入膏肓了,那么可能就要bus_reset()<br />

了.<br />

Ok,我们让代码来告诉你.<br />

首先看到的是device_reset().来自drivers/usb/storage/scsiglue.c,<br />

239 /* This invokes the transport reset mechanism to reset the state <strong>of</strong> the

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!