sd.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791
  1. /*
  2. * sd.c Copyright (C) 1992 Drew Eckhardt
  3. * Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale
  4. *
  5. * Linux scsi disk driver
  6. * Initial versions: Drew Eckhardt
  7. * Subsequent revisions: Eric Youngdale
  8. * Modification history:
  9. * - Drew Eckhardt <drew@colorado.edu> original
  10. * - Eric Youngdale <eric@andante.org> add scatter-gather, multiple
  11. * outstanding request, and other enhancements.
  12. * Support loadable low-level scsi drivers.
  13. * - Jirka Hanika <geo@ff.cuni.cz> support more scsi disks using
  14. * eight major numbers.
  15. * - Richard Gooch <rgooch@atnf.csiro.au> support devfs.
  16. * - Torben Mathiasen <tmm@image.dk> Resource allocation fixes in
  17. * sd_init and cleanups.
  18. * - Alex Davis <letmein@erols.com> Fix problem where partition info
  19. * not being read in sd_open. Fix problem where removable media
  20. * could be ejected after sd_open.
  21. * - Douglas Gilbert <dgilbert@interlog.com> cleanup for lk 2.5.x
  22. * - Badari Pulavarty <pbadari@us.ibm.com>, Matthew Wilcox
  23. * <willy@debian.org>, Kurt Garloff <garloff@suse.de>:
  24. * Support 32k/1M disks.
  25. *
  26. * Logging policy (needs CONFIG_SCSI_LOGGING defined):
  27. * - setting up transfer: SCSI_LOG_HLQUEUE levels 1 and 2
  28. * - end of transfer (bh + scsi_lib): SCSI_LOG_HLCOMPLETE level 1
  29. * - entering sd_ioctl: SCSI_LOG_IOCTL level 1
  30. * - entering other commands: SCSI_LOG_HLQUEUE level 3
  31. * Note: when the logging level is set by the user, it must be greater
  32. * than the level indicated above to trigger output.
  33. */
  34. #include <linux/module.h>
  35. #include <linux/fs.h>
  36. #include <linux/kernel.h>
  37. #include <linux/mm.h>
  38. #include <linux/bio.h>
  39. #include <linux/genhd.h>
  40. #include <linux/hdreg.h>
  41. #include <linux/errno.h>
  42. #include <linux/idr.h>
  43. #include <linux/interrupt.h>
  44. #include <linux/init.h>
  45. #include <linux/blkdev.h>
  46. #include <linux/blkpg.h>
  47. #include <linux/delay.h>
  48. #include <linux/mutex.h>
  49. #include <asm/uaccess.h>
  50. #include <scsi/scsi.h>
  51. #include <scsi/scsi_cmnd.h>
  52. #include <scsi/scsi_dbg.h>
  53. #include <scsi/scsi_device.h>
  54. #include <scsi/scsi_driver.h>
  55. #include <scsi/scsi_eh.h>
  56. #include <scsi/scsi_host.h>
  57. #include <scsi/scsi_ioctl.h>
  58. #include <scsi/scsicam.h>
  59. #include <scsi/sd.h>
  60. #include "scsi_logging.h"
  61. MODULE_AUTHOR("Eric Youngdale");
  62. MODULE_DESCRIPTION("SCSI disk (sd) driver");
  63. MODULE_LICENSE("GPL");
  64. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK0_MAJOR);
  65. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK1_MAJOR);
  66. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK2_MAJOR);
  67. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK3_MAJOR);
  68. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK4_MAJOR);
  69. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK5_MAJOR);
  70. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK6_MAJOR);
  71. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK7_MAJOR);
  72. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK8_MAJOR);
  73. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK9_MAJOR);
  74. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK10_MAJOR);
  75. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK11_MAJOR);
  76. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK12_MAJOR);
  77. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK13_MAJOR);
  78. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK14_MAJOR);
  79. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK15_MAJOR);
  80. static DEFINE_IDR(sd_index_idr);
  81. static DEFINE_SPINLOCK(sd_index_lock);
  82. /* This semaphore is used to mediate the 0->1 reference get in the
  83. * face of object destruction (i.e. we can't allow a get on an
  84. * object after last put) */
  85. static DEFINE_MUTEX(sd_ref_mutex);
  86. static const char *sd_cache_types[] = {
  87. "write through", "none", "write back",
  88. "write back, no read (daft)"
  89. };
  90. static ssize_t sd_store_cache_type(struct class_device *cdev, const char *buf,
  91. size_t count)
  92. {
  93. int i, ct = -1, rcd, wce, sp;
  94. struct scsi_disk *sdkp = to_scsi_disk(cdev);
  95. struct scsi_device *sdp = sdkp->device;
  96. char buffer[64];
  97. char *buffer_data;
  98. struct scsi_mode_data data;
  99. struct scsi_sense_hdr sshdr;
  100. int len;
  101. if (sdp->type != TYPE_DISK)
  102. /* no cache control on RBC devices; theoretically they
  103. * can do it, but there's probably so many exceptions
  104. * it's not worth the risk */
  105. return -EINVAL;
  106. for (i = 0; i < ARRAY_SIZE(sd_cache_types); i++) {
  107. const int len = strlen(sd_cache_types[i]);
  108. if (strncmp(sd_cache_types[i], buf, len) == 0 &&
  109. buf[len] == '\n') {
  110. ct = i;
  111. break;
  112. }
  113. }
  114. if (ct < 0)
  115. return -EINVAL;
  116. rcd = ct & 0x01 ? 1 : 0;
  117. wce = ct & 0x02 ? 1 : 0;
  118. if (scsi_mode_sense(sdp, 0x08, 8, buffer, sizeof(buffer), SD_TIMEOUT,
  119. SD_MAX_RETRIES, &data, NULL))
  120. return -EINVAL;
  121. len = min_t(size_t, sizeof(buffer), data.length - data.header_length -
  122. data.block_descriptor_length);
  123. buffer_data = buffer + data.header_length +
  124. data.block_descriptor_length;
  125. buffer_data[2] &= ~0x05;
  126. buffer_data[2] |= wce << 2 | rcd;
  127. sp = buffer_data[0] & 0x80 ? 1 : 0;
  128. if (scsi_mode_select(sdp, 1, sp, 8, buffer_data, len, SD_TIMEOUT,
  129. SD_MAX_RETRIES, &data, &sshdr)) {
  130. if (scsi_sense_valid(&sshdr))
  131. sd_print_sense_hdr(sdkp, &sshdr);
  132. return -EINVAL;
  133. }
  134. sd_revalidate_disk(sdkp->disk);
  135. return count;
  136. }
  137. static ssize_t sd_store_allow_restart(struct class_device *cdev, const char *buf,
  138. size_t count)
  139. {
  140. struct scsi_disk *sdkp = to_scsi_disk(cdev);
  141. struct scsi_device *sdp = sdkp->device;
  142. if (!capable(CAP_SYS_ADMIN))
  143. return -EACCES;
  144. if (sdp->type != TYPE_DISK)
  145. return -EINVAL;
  146. sdp->allow_restart = simple_strtoul(buf, NULL, 10);
  147. return count;
  148. }
  149. static ssize_t sd_show_cache_type(struct class_device *cdev, char *buf)
  150. {
  151. struct scsi_disk *sdkp = to_scsi_disk(cdev);
  152. int ct = sdkp->RCD + 2*sdkp->WCE;
  153. return snprintf(buf, 40, "%s\n", sd_cache_types[ct]);
  154. }
  155. static ssize_t sd_show_fua(struct class_device *cdev, char *buf)
  156. {
  157. struct scsi_disk *sdkp = to_scsi_disk(cdev);
  158. return snprintf(buf, 20, "%u\n", sdkp->DPOFUA);
  159. }
  160. static ssize_t sd_show_allow_restart(struct class_device *cdev, char *buf)
  161. {
  162. struct scsi_disk *sdkp = to_scsi_disk(cdev);
  163. return snprintf(buf, 40, "%d\n", sdkp->device->allow_restart);
  164. }
  165. static struct class_device_attribute sd_disk_attrs[] = {
  166. __ATTR(cache_type, S_IRUGO|S_IWUSR, sd_show_cache_type,
  167. sd_store_cache_type),
  168. __ATTR(FUA, S_IRUGO, sd_show_fua, NULL),
  169. __ATTR(allow_restart, S_IRUGO|S_IWUSR, sd_show_allow_restart,
  170. sd_store_allow_restart),
  171. __ATTR_NULL,
  172. };
  173. static struct class sd_disk_class = {
  174. .name = "scsi_disk",
  175. .owner = THIS_MODULE,
  176. .release = scsi_disk_release,
  177. .class_dev_attrs = sd_disk_attrs,
  178. };
  179. static struct scsi_driver sd_template = {
  180. .owner = THIS_MODULE,
  181. .gendrv = {
  182. .name = "sd",
  183. .probe = sd_probe,
  184. .remove = sd_remove,
  185. .shutdown = sd_shutdown,
  186. },
  187. .rescan = sd_rescan,
  188. .init_command = sd_init_command,
  189. .issue_flush = sd_issue_flush,
  190. };
  191. /*
  192. * Device no to disk mapping:
  193. *
  194. * major disc2 disc p1
  195. * |............|.............|....|....| <- dev_t
  196. * 31 20 19 8 7 4 3 0
  197. *
  198. * Inside a major, we have 16k disks, however mapped non-
  199. * contiguously. The first 16 disks are for major0, the next
  200. * ones with major1, ... Disk 256 is for major0 again, disk 272
  201. * for major1, ...
  202. * As we stay compatible with our numbering scheme, we can reuse
  203. * the well-know SCSI majors 8, 65--71, 136--143.
  204. */
  205. static int sd_major(int major_idx)
  206. {
  207. switch (major_idx) {
  208. case 0:
  209. return SCSI_DISK0_MAJOR;
  210. case 1 ... 7:
  211. return SCSI_DISK1_MAJOR + major_idx - 1;
  212. case 8 ... 15:
  213. return SCSI_DISK8_MAJOR + major_idx - 8;
  214. default:
  215. BUG();
  216. return 0; /* shut up gcc */
  217. }
  218. }
  219. static inline struct scsi_disk *scsi_disk(struct gendisk *disk)
  220. {
  221. return container_of(disk->private_data, struct scsi_disk, driver);
  222. }
  223. static struct scsi_disk *__scsi_disk_get(struct gendisk *disk)
  224. {
  225. struct scsi_disk *sdkp = NULL;
  226. if (disk->private_data) {
  227. sdkp = scsi_disk(disk);
  228. if (scsi_device_get(sdkp->device) == 0)
  229. class_device_get(&sdkp->cdev);
  230. else
  231. sdkp = NULL;
  232. }
  233. return sdkp;
  234. }
  235. static struct scsi_disk *scsi_disk_get(struct gendisk *disk)
  236. {
  237. struct scsi_disk *sdkp;
  238. mutex_lock(&sd_ref_mutex);
  239. sdkp = __scsi_disk_get(disk);
  240. mutex_unlock(&sd_ref_mutex);
  241. return sdkp;
  242. }
  243. static struct scsi_disk *scsi_disk_get_from_dev(struct device *dev)
  244. {
  245. struct scsi_disk *sdkp;
  246. mutex_lock(&sd_ref_mutex);
  247. sdkp = dev_get_drvdata(dev);
  248. if (sdkp)
  249. sdkp = __scsi_disk_get(sdkp->disk);
  250. mutex_unlock(&sd_ref_mutex);
  251. return sdkp;
  252. }
  253. static void scsi_disk_put(struct scsi_disk *sdkp)
  254. {
  255. struct scsi_device *sdev = sdkp->device;
  256. mutex_lock(&sd_ref_mutex);
  257. class_device_put(&sdkp->cdev);
  258. scsi_device_put(sdev);
  259. mutex_unlock(&sd_ref_mutex);
  260. }
  261. /**
  262. * sd_init_command - build a scsi (read or write) command from
  263. * information in the request structure.
  264. * @SCpnt: pointer to mid-level's per scsi command structure that
  265. * contains request and into which the scsi command is written
  266. *
  267. * Returns 1 if successful and 0 if error (or cannot be done now).
  268. **/
  269. static int sd_init_command(struct scsi_cmnd * SCpnt)
  270. {
  271. struct scsi_device *sdp = SCpnt->device;
  272. struct request *rq = SCpnt->request;
  273. struct gendisk *disk = rq->rq_disk;
  274. sector_t block = rq->sector;
  275. unsigned int this_count = SCpnt->request_bufflen >> 9;
  276. unsigned int timeout = sdp->timeout;
  277. SCSI_LOG_HLQUEUE(1, printk("sd_init_command: disk=%s, block=%llu, "
  278. "count=%d\n", disk->disk_name,
  279. (unsigned long long)block, this_count));
  280. if (!sdp || !scsi_device_online(sdp) ||
  281. block + rq->nr_sectors > get_capacity(disk)) {
  282. SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n",
  283. rq->nr_sectors));
  284. SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
  285. return 0;
  286. }
  287. if (sdp->changed) {
  288. /*
  289. * quietly refuse to do anything to a changed disc until
  290. * the changed bit has been reset
  291. */
  292. /* printk("SCSI disk has been changed. Prohibiting further I/O.\n"); */
  293. return 0;
  294. }
  295. SCSI_LOG_HLQUEUE(2, printk("%s : block=%llu\n",
  296. disk->disk_name, (unsigned long long)block));
  297. /*
  298. * If we have a 1K hardware sectorsize, prevent access to single
  299. * 512 byte sectors. In theory we could handle this - in fact
  300. * the scsi cdrom driver must be able to handle this because
  301. * we typically use 1K blocksizes, and cdroms typically have
  302. * 2K hardware sectorsizes. Of course, things are simpler
  303. * with the cdrom, since it is read-only. For performance
  304. * reasons, the filesystems should be able to handle this
  305. * and not force the scsi disk driver to use bounce buffers
  306. * for this.
  307. */
  308. if (sdp->sector_size == 1024) {
  309. if ((block & 1) || (rq->nr_sectors & 1)) {
  310. scmd_printk(KERN_ERR, SCpnt,
  311. "Bad block number requested\n");
  312. return 0;
  313. } else {
  314. block = block >> 1;
  315. this_count = this_count >> 1;
  316. }
  317. }
  318. if (sdp->sector_size == 2048) {
  319. if ((block & 3) || (rq->nr_sectors & 3)) {
  320. scmd_printk(KERN_ERR, SCpnt,
  321. "Bad block number requested\n");
  322. return 0;
  323. } else {
  324. block = block >> 2;
  325. this_count = this_count >> 2;
  326. }
  327. }
  328. if (sdp->sector_size == 4096) {
  329. if ((block & 7) || (rq->nr_sectors & 7)) {
  330. scmd_printk(KERN_ERR, SCpnt,
  331. "Bad block number requested\n");
  332. return 0;
  333. } else {
  334. block = block >> 3;
  335. this_count = this_count >> 3;
  336. }
  337. }
  338. if (rq_data_dir(rq) == WRITE) {
  339. if (!sdp->writeable) {
  340. return 0;
  341. }
  342. SCpnt->cmnd[0] = WRITE_6;
  343. SCpnt->sc_data_direction = DMA_TO_DEVICE;
  344. } else if (rq_data_dir(rq) == READ) {
  345. SCpnt->cmnd[0] = READ_6;
  346. SCpnt->sc_data_direction = DMA_FROM_DEVICE;
  347. } else {
  348. scmd_printk(KERN_ERR, SCpnt, "Unknown command %x\n", rq->cmd_flags);
  349. return 0;
  350. }
  351. SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%ld 512 byte blocks.\n",
  352. disk->disk_name, (rq_data_dir(rq) == WRITE) ?
  353. "writing" : "reading", this_count, rq->nr_sectors));
  354. SCpnt->cmnd[1] = 0;
  355. if (block > 0xffffffff) {
  356. SCpnt->cmnd[0] += READ_16 - READ_6;
  357. SCpnt->cmnd[1] |= blk_fua_rq(rq) ? 0x8 : 0;
  358. SCpnt->cmnd[2] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0;
  359. SCpnt->cmnd[3] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0;
  360. SCpnt->cmnd[4] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0;
  361. SCpnt->cmnd[5] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0;
  362. SCpnt->cmnd[6] = (unsigned char) (block >> 24) & 0xff;
  363. SCpnt->cmnd[7] = (unsigned char) (block >> 16) & 0xff;
  364. SCpnt->cmnd[8] = (unsigned char) (block >> 8) & 0xff;
  365. SCpnt->cmnd[9] = (unsigned char) block & 0xff;
  366. SCpnt->cmnd[10] = (unsigned char) (this_count >> 24) & 0xff;
  367. SCpnt->cmnd[11] = (unsigned char) (this_count >> 16) & 0xff;
  368. SCpnt->cmnd[12] = (unsigned char) (this_count >> 8) & 0xff;
  369. SCpnt->cmnd[13] = (unsigned char) this_count & 0xff;
  370. SCpnt->cmnd[14] = SCpnt->cmnd[15] = 0;
  371. } else if ((this_count > 0xff) || (block > 0x1fffff) ||
  372. SCpnt->device->use_10_for_rw) {
  373. if (this_count > 0xffff)
  374. this_count = 0xffff;
  375. SCpnt->cmnd[0] += READ_10 - READ_6;
  376. SCpnt->cmnd[1] |= blk_fua_rq(rq) ? 0x8 : 0;
  377. SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
  378. SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
  379. SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
  380. SCpnt->cmnd[5] = (unsigned char) block & 0xff;
  381. SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
  382. SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
  383. SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
  384. } else {
  385. if (unlikely(blk_fua_rq(rq))) {
  386. /*
  387. * This happens only if this drive failed
  388. * 10byte rw command with ILLEGAL_REQUEST
  389. * during operation and thus turned off
  390. * use_10_for_rw.
  391. */
  392. scmd_printk(KERN_ERR, SCpnt,
  393. "FUA write on READ/WRITE(6) drive\n");
  394. return 0;
  395. }
  396. SCpnt->cmnd[1] |= (unsigned char) ((block >> 16) & 0x1f);
  397. SCpnt->cmnd[2] = (unsigned char) ((block >> 8) & 0xff);
  398. SCpnt->cmnd[3] = (unsigned char) block & 0xff;
  399. SCpnt->cmnd[4] = (unsigned char) this_count;
  400. SCpnt->cmnd[5] = 0;
  401. }
  402. SCpnt->request_bufflen = this_count * sdp->sector_size;
  403. /*
  404. * We shouldn't disconnect in the middle of a sector, so with a dumb
  405. * host adapter, it's safe to assume that we can at least transfer
  406. * this many bytes between each connect / disconnect.
  407. */
  408. SCpnt->transfersize = sdp->sector_size;
  409. SCpnt->underflow = this_count << 9;
  410. SCpnt->allowed = SD_MAX_RETRIES;
  411. SCpnt->timeout_per_command = timeout;
  412. /*
  413. * This is the completion routine we use. This is matched in terms
  414. * of capability to this function.
  415. */
  416. SCpnt->done = sd_rw_intr;
  417. /*
  418. * This indicates that the command is ready from our end to be
  419. * queued.
  420. */
  421. return 1;
  422. }
  423. /**
  424. * sd_open - open a scsi disk device
  425. * @inode: only i_rdev member may be used
  426. * @filp: only f_mode and f_flags may be used
  427. *
  428. * Returns 0 if successful. Returns a negated errno value in case
  429. * of error.
  430. *
  431. * Note: This can be called from a user context (e.g. fsck(1) )
  432. * or from within the kernel (e.g. as a result of a mount(1) ).
  433. * In the latter case @inode and @filp carry an abridged amount
  434. * of information as noted above.
  435. **/
  436. static int sd_open(struct inode *inode, struct file *filp)
  437. {
  438. struct gendisk *disk = inode->i_bdev->bd_disk;
  439. struct scsi_disk *sdkp;
  440. struct scsi_device *sdev;
  441. int retval;
  442. if (!(sdkp = scsi_disk_get(disk)))
  443. return -ENXIO;
  444. SCSI_LOG_HLQUEUE(3, printk("sd_open: disk=%s\n", disk->disk_name));
  445. sdev = sdkp->device;
  446. /*
  447. * If the device is in error recovery, wait until it is done.
  448. * If the device is offline, then disallow any access to it.
  449. */
  450. retval = -ENXIO;
  451. if (!scsi_block_when_processing_errors(sdev))
  452. goto error_out;
  453. if (sdev->removable || sdkp->write_prot)
  454. check_disk_change(inode->i_bdev);
  455. /*
  456. * If the drive is empty, just let the open fail.
  457. */
  458. retval = -ENOMEDIUM;
  459. if (sdev->removable && !sdkp->media_present &&
  460. !(filp->f_flags & O_NDELAY))
  461. goto error_out;
  462. /*
  463. * If the device has the write protect tab set, have the open fail
  464. * if the user expects to be able to write to the thing.
  465. */
  466. retval = -EROFS;
  467. if (sdkp->write_prot && (filp->f_mode & FMODE_WRITE))
  468. goto error_out;
  469. /*
  470. * It is possible that the disk changing stuff resulted in
  471. * the device being taken offline. If this is the case,
  472. * report this to the user, and don't pretend that the
  473. * open actually succeeded.
  474. */
  475. retval = -ENXIO;
  476. if (!scsi_device_online(sdev))
  477. goto error_out;
  478. if (!sdkp->openers++ && sdev->removable) {
  479. if (scsi_block_when_processing_errors(sdev))
  480. scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT);
  481. }
  482. return 0;
  483. error_out:
  484. scsi_disk_put(sdkp);
  485. return retval;
  486. }
  487. /**
  488. * sd_release - invoked when the (last) close(2) is called on this
  489. * scsi disk.
  490. * @inode: only i_rdev member may be used
  491. * @filp: only f_mode and f_flags may be used
  492. *
  493. * Returns 0.
  494. *
  495. * Note: may block (uninterruptible) if error recovery is underway
  496. * on this disk.
  497. **/
  498. static int sd_release(struct inode *inode, struct file *filp)
  499. {
  500. struct gendisk *disk = inode->i_bdev->bd_disk;
  501. struct scsi_disk *sdkp = scsi_disk(disk);
  502. struct scsi_device *sdev = sdkp->device;
  503. SCSI_LOG_HLQUEUE(3, printk("sd_release: disk=%s\n", disk->disk_name));
  504. if (!--sdkp->openers && sdev->removable) {
  505. if (scsi_block_when_processing_errors(sdev))
  506. scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW);
  507. }
  508. /*
  509. * XXX and what if there are packets in flight and this close()
  510. * XXX is followed by a "rmmod sd_mod"?
  511. */
  512. scsi_disk_put(sdkp);
  513. return 0;
  514. }
  515. static int sd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  516. {
  517. struct scsi_disk *sdkp = scsi_disk(bdev->bd_disk);
  518. struct scsi_device *sdp = sdkp->device;
  519. struct Scsi_Host *host = sdp->host;
  520. int diskinfo[4];
  521. /* default to most commonly used values */
  522. diskinfo[0] = 0x40; /* 1 << 6 */
  523. diskinfo[1] = 0x20; /* 1 << 5 */
  524. diskinfo[2] = sdkp->capacity >> 11;
  525. /* override with calculated, extended default, or driver values */
  526. if (host->hostt->bios_param)
  527. host->hostt->bios_param(sdp, bdev, sdkp->capacity, diskinfo);
  528. else
  529. scsicam_bios_param(bdev, sdkp->capacity, diskinfo);
  530. geo->heads = diskinfo[0];
  531. geo->sectors = diskinfo[1];
  532. geo->cylinders = diskinfo[2];
  533. return 0;
  534. }
  535. /**
  536. * sd_ioctl - process an ioctl
  537. * @inode: only i_rdev/i_bdev members may be used
  538. * @filp: only f_mode and f_flags may be used
  539. * @cmd: ioctl command number
  540. * @arg: this is third argument given to ioctl(2) system call.
  541. * Often contains a pointer.
  542. *
  543. * Returns 0 if successful (some ioctls return postive numbers on
  544. * success as well). Returns a negated errno value in case of error.
  545. *
  546. * Note: most ioctls are forward onto the block subsystem or further
  547. * down in the scsi subsytem.
  548. **/
  549. static int sd_ioctl(struct inode * inode, struct file * filp,
  550. unsigned int cmd, unsigned long arg)
  551. {
  552. struct block_device *bdev = inode->i_bdev;
  553. struct gendisk *disk = bdev->bd_disk;
  554. struct scsi_device *sdp = scsi_disk(disk)->device;
  555. void __user *p = (void __user *)arg;
  556. int error;
  557. SCSI_LOG_IOCTL(1, printk("sd_ioctl: disk=%s, cmd=0x%x\n",
  558. disk->disk_name, cmd));
  559. /*
  560. * If we are in the middle of error recovery, don't let anyone
  561. * else try and use this device. Also, if error recovery fails, it
  562. * may try and take the device offline, in which case all further
  563. * access to the device is prohibited.
  564. */
  565. error = scsi_nonblockable_ioctl(sdp, cmd, p, filp);
  566. if (!scsi_block_when_processing_errors(sdp) || !error)
  567. return error;
  568. /*
  569. * Send SCSI addressing ioctls directly to mid level, send other
  570. * ioctls to block level and then onto mid level if they can't be
  571. * resolved.
  572. */
  573. switch (cmd) {
  574. case SCSI_IOCTL_GET_IDLUN:
  575. case SCSI_IOCTL_GET_BUS_NUMBER:
  576. return scsi_ioctl(sdp, cmd, p);
  577. default:
  578. error = scsi_cmd_ioctl(filp, disk, cmd, p);
  579. if (error != -ENOTTY)
  580. return error;
  581. }
  582. return scsi_ioctl(sdp, cmd, p);
  583. }
  584. static void set_media_not_present(struct scsi_disk *sdkp)
  585. {
  586. sdkp->media_present = 0;
  587. sdkp->capacity = 0;
  588. sdkp->device->changed = 1;
  589. }
  590. /**
  591. * sd_media_changed - check if our medium changed
  592. * @disk: kernel device descriptor
  593. *
  594. * Returns 0 if not applicable or no change; 1 if change
  595. *
  596. * Note: this function is invoked from the block subsystem.
  597. **/
  598. static int sd_media_changed(struct gendisk *disk)
  599. {
  600. struct scsi_disk *sdkp = scsi_disk(disk);
  601. struct scsi_device *sdp = sdkp->device;
  602. int retval;
  603. SCSI_LOG_HLQUEUE(3, printk("sd_media_changed: disk=%s\n",
  604. disk->disk_name));
  605. if (!sdp->removable)
  606. return 0;
  607. /*
  608. * If the device is offline, don't send any commands - just pretend as
  609. * if the command failed. If the device ever comes back online, we
  610. * can deal with it then. It is only because of unrecoverable errors
  611. * that we would ever take a device offline in the first place.
  612. */
  613. if (!scsi_device_online(sdp))
  614. goto not_present;
  615. /*
  616. * Using TEST_UNIT_READY enables differentiation between drive with
  617. * no cartridge loaded - NOT READY, drive with changed cartridge -
  618. * UNIT ATTENTION, or with same cartridge - GOOD STATUS.
  619. *
  620. * Drives that auto spin down. eg iomega jaz 1G, will be started
  621. * by sd_spinup_disk() from sd_revalidate_disk(), which happens whenever
  622. * sd_revalidate() is called.
  623. */
  624. retval = -ENODEV;
  625. if (scsi_block_when_processing_errors(sdp))
  626. retval = scsi_test_unit_ready(sdp, SD_TIMEOUT, SD_MAX_RETRIES);
  627. /*
  628. * Unable to test, unit probably not ready. This usually
  629. * means there is no disc in the drive. Mark as changed,
  630. * and we will figure it out later once the drive is
  631. * available again.
  632. */
  633. if (retval)
  634. goto not_present;
  635. /*
  636. * For removable scsi disk we have to recognise the presence
  637. * of a disk in the drive. This is kept in the struct scsi_disk
  638. * struct and tested at open ! Daniel Roche (dan@lectra.fr)
  639. */
  640. sdkp->media_present = 1;
  641. retval = sdp->changed;
  642. sdp->changed = 0;
  643. return retval;
  644. not_present:
  645. set_media_not_present(sdkp);
  646. return 1;
  647. }
  648. static int sd_sync_cache(struct scsi_disk *sdkp)
  649. {
  650. int retries, res;
  651. struct scsi_device *sdp = sdkp->device;
  652. struct scsi_sense_hdr sshdr;
  653. if (!scsi_device_online(sdp))
  654. return -ENODEV;
  655. for (retries = 3; retries > 0; --retries) {
  656. unsigned char cmd[10] = { 0 };
  657. cmd[0] = SYNCHRONIZE_CACHE;
  658. /*
  659. * Leave the rest of the command zero to indicate
  660. * flush everything.
  661. */
  662. res = scsi_execute_req(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
  663. SD_TIMEOUT, SD_MAX_RETRIES);
  664. if (res == 0)
  665. break;
  666. }
  667. if (res) {
  668. sd_print_result(sdkp, res);
  669. if (driver_byte(res) & DRIVER_SENSE)
  670. sd_print_sense_hdr(sdkp, &sshdr);
  671. }
  672. return res;
  673. }
  674. static int sd_issue_flush(struct device *dev, sector_t *error_sector)
  675. {
  676. int ret = 0;
  677. struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
  678. if (!sdkp)
  679. return -ENODEV;
  680. if (sdkp->WCE)
  681. ret = sd_sync_cache(sdkp);
  682. scsi_disk_put(sdkp);
  683. return ret;
  684. }
  685. static void sd_prepare_flush(request_queue_t *q, struct request *rq)
  686. {
  687. memset(rq->cmd, 0, sizeof(rq->cmd));
  688. rq->cmd_type = REQ_TYPE_BLOCK_PC;
  689. rq->timeout = SD_TIMEOUT;
  690. rq->cmd[0] = SYNCHRONIZE_CACHE;
  691. rq->cmd_len = 10;
  692. }
  693. static void sd_rescan(struct device *dev)
  694. {
  695. struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
  696. if (sdkp) {
  697. sd_revalidate_disk(sdkp->disk);
  698. scsi_disk_put(sdkp);
  699. }
  700. }
  701. #ifdef CONFIG_COMPAT
  702. /*
  703. * This gets directly called from VFS. When the ioctl
  704. * is not recognized we go back to the other translation paths.
  705. */
  706. static long sd_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  707. {
  708. struct block_device *bdev = file->f_path.dentry->d_inode->i_bdev;
  709. struct gendisk *disk = bdev->bd_disk;
  710. struct scsi_device *sdev = scsi_disk(disk)->device;
  711. /*
  712. * If we are in the middle of error recovery, don't let anyone
  713. * else try and use this device. Also, if error recovery fails, it
  714. * may try and take the device offline, in which case all further
  715. * access to the device is prohibited.
  716. */
  717. if (!scsi_block_when_processing_errors(sdev))
  718. return -ENODEV;
  719. if (sdev->host->hostt->compat_ioctl) {
  720. int ret;
  721. ret = sdev->host->hostt->compat_ioctl(sdev, cmd, (void __user *)arg);
  722. return ret;
  723. }
  724. /*
  725. * Let the static ioctl translation table take care of it.
  726. */
  727. return -ENOIOCTLCMD;
  728. }
  729. #endif
  730. static struct block_device_operations sd_fops = {
  731. .owner = THIS_MODULE,
  732. .open = sd_open,
  733. .release = sd_release,
  734. .ioctl = sd_ioctl,
  735. .getgeo = sd_getgeo,
  736. #ifdef CONFIG_COMPAT
  737. .compat_ioctl = sd_compat_ioctl,
  738. #endif
  739. .media_changed = sd_media_changed,
  740. .revalidate_disk = sd_revalidate_disk,
  741. };
  742. /**
  743. * sd_rw_intr - bottom half handler: called when the lower level
  744. * driver has completed (successfully or otherwise) a scsi command.
  745. * @SCpnt: mid-level's per command structure.
  746. *
  747. * Note: potentially run from within an ISR. Must not block.
  748. **/
  749. static void sd_rw_intr(struct scsi_cmnd * SCpnt)
  750. {
  751. int result = SCpnt->result;
  752. unsigned int xfer_size = SCpnt->request_bufflen;
  753. unsigned int good_bytes = result ? 0 : xfer_size;
  754. u64 start_lba = SCpnt->request->sector;
  755. u64 bad_lba;
  756. struct scsi_sense_hdr sshdr;
  757. int sense_valid = 0;
  758. int sense_deferred = 0;
  759. int info_valid;
  760. if (result) {
  761. sense_valid = scsi_command_normalize_sense(SCpnt, &sshdr);
  762. if (sense_valid)
  763. sense_deferred = scsi_sense_is_deferred(&sshdr);
  764. }
  765. #ifdef CONFIG_SCSI_LOGGING
  766. SCSI_LOG_HLCOMPLETE(1, printk("sd_rw_intr: %s: res=0x%x\n",
  767. SCpnt->request->rq_disk->disk_name, result));
  768. if (sense_valid) {
  769. SCSI_LOG_HLCOMPLETE(1, printk("sd_rw_intr: sb[respc,sk,asc,"
  770. "ascq]=%x,%x,%x,%x\n", sshdr.response_code,
  771. sshdr.sense_key, sshdr.asc, sshdr.ascq));
  772. }
  773. #endif
  774. if (driver_byte(result) != DRIVER_SENSE &&
  775. (!sense_valid || sense_deferred))
  776. goto out;
  777. switch (sshdr.sense_key) {
  778. case HARDWARE_ERROR:
  779. case MEDIUM_ERROR:
  780. if (!blk_fs_request(SCpnt->request))
  781. goto out;
  782. info_valid = scsi_get_sense_info_fld(SCpnt->sense_buffer,
  783. SCSI_SENSE_BUFFERSIZE,
  784. &bad_lba);
  785. if (!info_valid)
  786. goto out;
  787. if (xfer_size <= SCpnt->device->sector_size)
  788. goto out;
  789. switch (SCpnt->device->sector_size) {
  790. case 256:
  791. start_lba <<= 1;
  792. break;
  793. case 512:
  794. break;
  795. case 1024:
  796. start_lba >>= 1;
  797. break;
  798. case 2048:
  799. start_lba >>= 2;
  800. break;
  801. case 4096:
  802. start_lba >>= 3;
  803. break;
  804. default:
  805. /* Print something here with limiting frequency. */
  806. goto out;
  807. break;
  808. }
  809. /* This computation should always be done in terms of
  810. * the resolution of the device's medium.
  811. */
  812. good_bytes = (bad_lba - start_lba)*SCpnt->device->sector_size;
  813. break;
  814. case RECOVERED_ERROR:
  815. case NO_SENSE:
  816. /* Inform the user, but make sure that it's not treated
  817. * as a hard error.
  818. */
  819. scsi_print_sense("sd", SCpnt);
  820. SCpnt->result = 0;
  821. memset(SCpnt->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
  822. good_bytes = xfer_size;
  823. break;
  824. case ILLEGAL_REQUEST:
  825. if (SCpnt->device->use_10_for_rw &&
  826. (SCpnt->cmnd[0] == READ_10 ||
  827. SCpnt->cmnd[0] == WRITE_10))
  828. SCpnt->device->use_10_for_rw = 0;
  829. if (SCpnt->device->use_10_for_ms &&
  830. (SCpnt->cmnd[0] == MODE_SENSE_10 ||
  831. SCpnt->cmnd[0] == MODE_SELECT_10))
  832. SCpnt->device->use_10_for_ms = 0;
  833. break;
  834. default:
  835. break;
  836. }
  837. out:
  838. scsi_io_completion(SCpnt, good_bytes);
  839. }
  840. static int media_not_present(struct scsi_disk *sdkp,
  841. struct scsi_sense_hdr *sshdr)
  842. {
  843. if (!scsi_sense_valid(sshdr))
  844. return 0;
  845. /* not invoked for commands that could return deferred errors */
  846. if (sshdr->sense_key != NOT_READY &&
  847. sshdr->sense_key != UNIT_ATTENTION)
  848. return 0;
  849. if (sshdr->asc != 0x3A) /* medium not present */
  850. return 0;
  851. set_media_not_present(sdkp);
  852. return 1;
  853. }
  854. /*
  855. * spinup disk - called only in sd_revalidate_disk()
  856. */
  857. static void
  858. sd_spinup_disk(struct scsi_disk *sdkp)
  859. {
  860. unsigned char cmd[10];
  861. unsigned long spintime_expire = 0;
  862. int retries, spintime;
  863. unsigned int the_result;
  864. struct scsi_sense_hdr sshdr;
  865. int sense_valid = 0;
  866. spintime = 0;
  867. /* Spin up drives, as required. Only do this at boot time */
  868. /* Spinup needs to be done for module loads too. */
  869. do {
  870. retries = 0;
  871. do {
  872. cmd[0] = TEST_UNIT_READY;
  873. memset((void *) &cmd[1], 0, 9);
  874. the_result = scsi_execute_req(sdkp->device, cmd,
  875. DMA_NONE, NULL, 0,
  876. &sshdr, SD_TIMEOUT,
  877. SD_MAX_RETRIES);
  878. /*
  879. * If the drive has indicated to us that it
  880. * doesn't have any media in it, don't bother
  881. * with any more polling.
  882. */
  883. if (media_not_present(sdkp, &sshdr))
  884. return;
  885. if (the_result)
  886. sense_valid = scsi_sense_valid(&sshdr);
  887. retries++;
  888. } while (retries < 3 &&
  889. (!scsi_status_is_good(the_result) ||
  890. ((driver_byte(the_result) & DRIVER_SENSE) &&
  891. sense_valid && sshdr.sense_key == UNIT_ATTENTION)));
  892. if ((driver_byte(the_result) & DRIVER_SENSE) == 0) {
  893. /* no sense, TUR either succeeded or failed
  894. * with a status error */
  895. if(!spintime && !scsi_status_is_good(the_result)) {
  896. sd_printk(KERN_NOTICE, sdkp, "Unit Not Ready\n");
  897. sd_print_result(sdkp, the_result);
  898. }
  899. break;
  900. }
  901. /*
  902. * The device does not want the automatic start to be issued.
  903. */
  904. if (sdkp->device->no_start_on_add) {
  905. break;
  906. }
  907. /*
  908. * If manual intervention is required, or this is an
  909. * absent USB storage device, a spinup is meaningless.
  910. */
  911. if (sense_valid &&
  912. sshdr.sense_key == NOT_READY &&
  913. sshdr.asc == 4 && sshdr.ascq == 3) {
  914. break; /* manual intervention required */
  915. /*
  916. * Issue command to spin up drive when not ready
  917. */
  918. } else if (sense_valid && sshdr.sense_key == NOT_READY) {
  919. if (!spintime) {
  920. sd_printk(KERN_NOTICE, sdkp, "Spinning up disk...");
  921. cmd[0] = START_STOP;
  922. cmd[1] = 1; /* Return immediately */
  923. memset((void *) &cmd[2], 0, 8);
  924. cmd[4] = 1; /* Start spin cycle */
  925. scsi_execute_req(sdkp->device, cmd, DMA_NONE,
  926. NULL, 0, &sshdr,
  927. SD_TIMEOUT, SD_MAX_RETRIES);
  928. spintime_expire = jiffies + 100 * HZ;
  929. spintime = 1;
  930. }
  931. /* Wait 1 second for next try */
  932. msleep(1000);
  933. printk(".");
  934. /*
  935. * Wait for USB flash devices with slow firmware.
  936. * Yes, this sense key/ASC combination shouldn't
  937. * occur here. It's characteristic of these devices.
  938. */
  939. } else if (sense_valid &&
  940. sshdr.sense_key == UNIT_ATTENTION &&
  941. sshdr.asc == 0x28) {
  942. if (!spintime) {
  943. spintime_expire = jiffies + 5 * HZ;
  944. spintime = 1;
  945. }
  946. /* Wait 1 second for next try */
  947. msleep(1000);
  948. } else {
  949. /* we don't understand the sense code, so it's
  950. * probably pointless to loop */
  951. if(!spintime) {
  952. sd_printk(KERN_NOTICE, sdkp, "Unit Not Ready\n");
  953. sd_print_sense_hdr(sdkp, &sshdr);
  954. }
  955. break;
  956. }
  957. } while (spintime && time_before_eq(jiffies, spintime_expire));
  958. if (spintime) {
  959. if (scsi_status_is_good(the_result))
  960. printk("ready\n");
  961. else
  962. printk("not responding...\n");
  963. }
  964. }
  965. /*
  966. * read disk capacity
  967. */
  968. static void
  969. sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer)
  970. {
  971. unsigned char cmd[16];
  972. int the_result, retries;
  973. int sector_size = 0;
  974. int longrc = 0;
  975. struct scsi_sense_hdr sshdr;
  976. int sense_valid = 0;
  977. struct scsi_device *sdp = sdkp->device;
  978. repeat:
  979. retries = 3;
  980. do {
  981. if (longrc) {
  982. memset((void *) cmd, 0, 16);
  983. cmd[0] = SERVICE_ACTION_IN;
  984. cmd[1] = SAI_READ_CAPACITY_16;
  985. cmd[13] = 12;
  986. memset((void *) buffer, 0, 12);
  987. } else {
  988. cmd[0] = READ_CAPACITY;
  989. memset((void *) &cmd[1], 0, 9);
  990. memset((void *) buffer, 0, 8);
  991. }
  992. the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
  993. buffer, longrc ? 12 : 8, &sshdr,
  994. SD_TIMEOUT, SD_MAX_RETRIES);
  995. if (media_not_present(sdkp, &sshdr))
  996. return;
  997. if (the_result)
  998. sense_valid = scsi_sense_valid(&sshdr);
  999. retries--;
  1000. } while (the_result && retries);
  1001. if (the_result && !longrc) {
  1002. sd_printk(KERN_NOTICE, sdkp, "READ CAPACITY failed\n");
  1003. sd_print_result(sdkp, the_result);
  1004. if (driver_byte(the_result) & DRIVER_SENSE)
  1005. sd_print_sense_hdr(sdkp, &sshdr);
  1006. else
  1007. sd_printk(KERN_NOTICE, sdkp, "Sense not available.\n");
  1008. /* Set dirty bit for removable devices if not ready -
  1009. * sometimes drives will not report this properly. */
  1010. if (sdp->removable &&
  1011. sense_valid && sshdr.sense_key == NOT_READY)
  1012. sdp->changed = 1;
  1013. /* Either no media are present but the drive didn't tell us,
  1014. or they are present but the read capacity command fails */
  1015. /* sdkp->media_present = 0; -- not always correct */
  1016. sdkp->capacity = 0; /* unknown mapped to zero - as usual */
  1017. return;
  1018. } else if (the_result && longrc) {
  1019. /* READ CAPACITY(16) has been failed */
  1020. sd_printk(KERN_NOTICE, sdkp, "READ CAPACITY(16) failed\n");
  1021. sd_print_result(sdkp, the_result);
  1022. sd_printk(KERN_NOTICE, sdkp, "Use 0xffffffff as device size\n");
  1023. sdkp->capacity = 1 + (sector_t) 0xffffffff;
  1024. goto got_data;
  1025. }
  1026. if (!longrc) {
  1027. sector_size = (buffer[4] << 24) |
  1028. (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
  1029. if (buffer[0] == 0xff && buffer[1] == 0xff &&
  1030. buffer[2] == 0xff && buffer[3] == 0xff) {
  1031. if(sizeof(sdkp->capacity) > 4) {
  1032. sd_printk(KERN_NOTICE, sdkp, "Very big device. "
  1033. "Trying to use READ CAPACITY(16).\n");
  1034. longrc = 1;
  1035. goto repeat;
  1036. }
  1037. sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use "
  1038. "a kernel compiled with support for large "
  1039. "block devices.\n");
  1040. sdkp->capacity = 0;
  1041. goto got_data;
  1042. }
  1043. sdkp->capacity = 1 + (((sector_t)buffer[0] << 24) |
  1044. (buffer[1] << 16) |
  1045. (buffer[2] << 8) |
  1046. buffer[3]);
  1047. } else {
  1048. sdkp->capacity = 1 + (((u64)buffer[0] << 56) |
  1049. ((u64)buffer[1] << 48) |
  1050. ((u64)buffer[2] << 40) |
  1051. ((u64)buffer[3] << 32) |
  1052. ((sector_t)buffer[4] << 24) |
  1053. ((sector_t)buffer[5] << 16) |
  1054. ((sector_t)buffer[6] << 8) |
  1055. (sector_t)buffer[7]);
  1056. sector_size = (buffer[8] << 24) |
  1057. (buffer[9] << 16) | (buffer[10] << 8) | buffer[11];
  1058. }
  1059. /* Some devices return the total number of sectors, not the
  1060. * highest sector number. Make the necessary adjustment. */
  1061. if (sdp->fix_capacity) {
  1062. --sdkp->capacity;
  1063. /* Some devices have version which report the correct sizes
  1064. * and others which do not. We guess size according to a heuristic
  1065. * and err on the side of lowering the capacity. */
  1066. } else {
  1067. if (sdp->guess_capacity)
  1068. if (sdkp->capacity & 0x01) /* odd sizes are odd */
  1069. --sdkp->capacity;
  1070. }
  1071. got_data:
  1072. if (sector_size == 0) {
  1073. sector_size = 512;
  1074. sd_printk(KERN_NOTICE, sdkp, "Sector size 0 reported, "
  1075. "assuming 512.\n");
  1076. }
  1077. if (sector_size != 512 &&
  1078. sector_size != 1024 &&
  1079. sector_size != 2048 &&
  1080. sector_size != 4096 &&
  1081. sector_size != 256) {
  1082. sd_printk(KERN_NOTICE, sdkp, "Unsupported sector size %d.\n",
  1083. sector_size);
  1084. /*
  1085. * The user might want to re-format the drive with
  1086. * a supported sectorsize. Once this happens, it
  1087. * would be relatively trivial to set the thing up.
  1088. * For this reason, we leave the thing in the table.
  1089. */
  1090. sdkp->capacity = 0;
  1091. /*
  1092. * set a bogus sector size so the normal read/write
  1093. * logic in the block layer will eventually refuse any
  1094. * request on this device without tripping over power
  1095. * of two sector size assumptions
  1096. */
  1097. sector_size = 512;
  1098. }
  1099. {
  1100. /*
  1101. * The msdos fs needs to know the hardware sector size
  1102. * So I have created this table. See ll_rw_blk.c
  1103. * Jacques Gelinas (Jacques@solucorp.qc.ca)
  1104. */
  1105. int hard_sector = sector_size;
  1106. sector_t sz = (sdkp->capacity/2) * (hard_sector/256);
  1107. request_queue_t *queue = sdp->request_queue;
  1108. sector_t mb = sz;
  1109. blk_queue_hardsect_size(queue, hard_sector);
  1110. /* avoid 64-bit division on 32-bit platforms */
  1111. sector_div(sz, 625);
  1112. mb -= sz - 974;
  1113. sector_div(mb, 1950);
  1114. sd_printk(KERN_NOTICE, sdkp,
  1115. "%llu %d-byte hardware sectors (%llu MB)\n",
  1116. (unsigned long long)sdkp->capacity,
  1117. hard_sector, (unsigned long long)mb);
  1118. }
  1119. /* Rescale capacity to 512-byte units */
  1120. if (sector_size == 4096)
  1121. sdkp->capacity <<= 3;
  1122. else if (sector_size == 2048)
  1123. sdkp->capacity <<= 2;
  1124. else if (sector_size == 1024)
  1125. sdkp->capacity <<= 1;
  1126. else if (sector_size == 256)
  1127. sdkp->capacity >>= 1;
  1128. sdkp->device->sector_size = sector_size;
  1129. }
  1130. /* called with buffer of length 512 */
  1131. static inline int
  1132. sd_do_mode_sense(struct scsi_device *sdp, int dbd, int modepage,
  1133. unsigned char *buffer, int len, struct scsi_mode_data *data,
  1134. struct scsi_sense_hdr *sshdr)
  1135. {
  1136. return scsi_mode_sense(sdp, dbd, modepage, buffer, len,
  1137. SD_TIMEOUT, SD_MAX_RETRIES, data,
  1138. sshdr);
  1139. }
  1140. /*
  1141. * read write protect setting, if possible - called only in sd_revalidate_disk()
  1142. * called with buffer of length SD_BUF_SIZE
  1143. */
  1144. static void
  1145. sd_read_write_protect_flag(struct scsi_disk *sdkp, unsigned char *buffer)
  1146. {
  1147. int res;
  1148. struct scsi_device *sdp = sdkp->device;
  1149. struct scsi_mode_data data;
  1150. set_disk_ro(sdkp->disk, 0);
  1151. if (sdp->skip_ms_page_3f) {
  1152. sd_printk(KERN_NOTICE, sdkp, "Assuming Write Enabled\n");
  1153. return;
  1154. }
  1155. if (sdp->use_192_bytes_for_3f) {
  1156. res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 192, &data, NULL);
  1157. } else {
  1158. /*
  1159. * First attempt: ask for all pages (0x3F), but only 4 bytes.
  1160. * We have to start carefully: some devices hang if we ask
  1161. * for more than is available.
  1162. */
  1163. res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 4, &data, NULL);
  1164. /*
  1165. * Second attempt: ask for page 0 When only page 0 is
  1166. * implemented, a request for page 3F may return Sense Key
  1167. * 5: Illegal Request, Sense Code 24: Invalid field in
  1168. * CDB.
  1169. */
  1170. if (!scsi_status_is_good(res))
  1171. res = sd_do_mode_sense(sdp, 0, 0, buffer, 4, &data, NULL);
  1172. /*
  1173. * Third attempt: ask 255 bytes, as we did earlier.
  1174. */
  1175. if (!scsi_status_is_good(res))
  1176. res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 255,
  1177. &data, NULL);
  1178. }
  1179. if (!scsi_status_is_good(res)) {
  1180. sd_printk(KERN_WARNING, sdkp,
  1181. "Test WP failed, assume Write Enabled\n");
  1182. } else {
  1183. sdkp->write_prot = ((data.device_specific & 0x80) != 0);
  1184. set_disk_ro(sdkp->disk, sdkp->write_prot);
  1185. sd_printk(KERN_NOTICE, sdkp, "Write Protect is %s\n",
  1186. sdkp->write_prot ? "on" : "off");
  1187. sd_printk(KERN_DEBUG, sdkp,
  1188. "Mode Sense: %02x %02x %02x %02x\n",
  1189. buffer[0], buffer[1], buffer[2], buffer[3]);
  1190. }
  1191. }
  1192. /*
  1193. * sd_read_cache_type - called only from sd_revalidate_disk()
  1194. * called with buffer of length SD_BUF_SIZE
  1195. */
  1196. static void
  1197. sd_read_cache_type(struct scsi_disk *sdkp, unsigned char *buffer)
  1198. {
  1199. int len = 0, res;
  1200. struct scsi_device *sdp = sdkp->device;
  1201. int dbd;
  1202. int modepage;
  1203. struct scsi_mode_data data;
  1204. struct scsi_sense_hdr sshdr;
  1205. if (sdp->skip_ms_page_8)
  1206. goto defaults;
  1207. if (sdp->type == TYPE_RBC) {
  1208. modepage = 6;
  1209. dbd = 8;
  1210. } else {
  1211. modepage = 8;
  1212. dbd = 0;
  1213. }
  1214. /* cautiously ask */
  1215. res = sd_do_mode_sense(sdp, dbd, modepage, buffer, 4, &data, &sshdr);
  1216. if (!scsi_status_is_good(res))
  1217. goto bad_sense;
  1218. if (!data.header_length) {
  1219. modepage = 6;
  1220. sd_printk(KERN_ERR, sdkp, "Missing header in MODE_SENSE response\n");
  1221. }
  1222. /* that went OK, now ask for the proper length */
  1223. len = data.length;
  1224. /*
  1225. * We're only interested in the first three bytes, actually.
  1226. * But the data cache page is defined for the first 20.
  1227. */
  1228. if (len < 3)
  1229. goto bad_sense;
  1230. if (len > 20)
  1231. len = 20;
  1232. /* Take headers and block descriptors into account */
  1233. len += data.header_length + data.block_descriptor_length;
  1234. if (len > SD_BUF_SIZE)
  1235. goto bad_sense;
  1236. /* Get the data */
  1237. res = sd_do_mode_sense(sdp, dbd, modepage, buffer, len, &data, &sshdr);
  1238. if (scsi_status_is_good(res)) {
  1239. int offset = data.header_length + data.block_descriptor_length;
  1240. if (offset >= SD_BUF_SIZE - 2) {
  1241. sd_printk(KERN_ERR, sdkp, "Malformed MODE SENSE response\n");
  1242. goto defaults;
  1243. }
  1244. if ((buffer[offset] & 0x3f) != modepage) {
  1245. sd_printk(KERN_ERR, sdkp, "Got wrong page\n");
  1246. goto defaults;
  1247. }
  1248. if (modepage == 8) {
  1249. sdkp->WCE = ((buffer[offset + 2] & 0x04) != 0);
  1250. sdkp->RCD = ((buffer[offset + 2] & 0x01) != 0);
  1251. } else {
  1252. sdkp->WCE = ((buffer[offset + 2] & 0x01) == 0);
  1253. sdkp->RCD = 0;
  1254. }
  1255. sdkp->DPOFUA = (data.device_specific & 0x10) != 0;
  1256. if (sdkp->DPOFUA && !sdkp->device->use_10_for_rw) {
  1257. sd_printk(KERN_NOTICE, sdkp,
  1258. "Uses READ/WRITE(6), disabling FUA\n");
  1259. sdkp->DPOFUA = 0;
  1260. }
  1261. sd_printk(KERN_NOTICE, sdkp,
  1262. "Write cache: %s, read cache: %s, %s\n",
  1263. sdkp->WCE ? "enabled" : "disabled",
  1264. sdkp->RCD ? "disabled" : "enabled",
  1265. sdkp->DPOFUA ? "supports DPO and FUA"
  1266. : "doesn't support DPO or FUA");
  1267. return;
  1268. }
  1269. bad_sense:
  1270. if (scsi_sense_valid(&sshdr) &&
  1271. sshdr.sense_key == ILLEGAL_REQUEST &&
  1272. sshdr.asc == 0x24 && sshdr.ascq == 0x0)
  1273. /* Invalid field in CDB */
  1274. sd_printk(KERN_NOTICE, sdkp, "Cache data unavailable\n");
  1275. else
  1276. sd_printk(KERN_ERR, sdkp, "Asking for cache data failed\n");
  1277. defaults:
  1278. sd_printk(KERN_ERR, sdkp, "Assuming drive cache: write through\n");
  1279. sdkp->WCE = 0;
  1280. sdkp->RCD = 0;
  1281. sdkp->DPOFUA = 0;
  1282. }
  1283. /**
  1284. * sd_revalidate_disk - called the first time a new disk is seen,
  1285. * performs disk spin up, read_capacity, etc.
  1286. * @disk: struct gendisk we care about
  1287. **/
  1288. static int sd_revalidate_disk(struct gendisk *disk)
  1289. {
  1290. struct scsi_disk *sdkp = scsi_disk(disk);
  1291. struct scsi_device *sdp = sdkp->device;
  1292. unsigned char *buffer;
  1293. unsigned ordered;
  1294. SCSI_LOG_HLQUEUE(3, printk("sd_revalidate_disk: disk=%s\n", disk->disk_name));
  1295. /*
  1296. * If the device is offline, don't try and read capacity or any
  1297. * of the other niceties.
  1298. */
  1299. if (!scsi_device_online(sdp))
  1300. goto out;
  1301. buffer = kmalloc(SD_BUF_SIZE, GFP_KERNEL | __GFP_DMA);
  1302. if (!buffer) {
  1303. sd_printk(KERN_WARNING, sdkp, "sd_revalidate_disk: Memory "
  1304. "allocation failure.\n");
  1305. goto out;
  1306. }
  1307. /* defaults, until the device tells us otherwise */
  1308. sdp->sector_size = 512;
  1309. sdkp->capacity = 0;
  1310. sdkp->media_present = 1;
  1311. sdkp->write_prot = 0;
  1312. sdkp->WCE = 0;
  1313. sdkp->RCD = 0;
  1314. sd_spinup_disk(sdkp);
  1315. /*
  1316. * Without media there is no reason to ask; moreover, some devices
  1317. * react badly if we do.
  1318. */
  1319. if (sdkp->media_present) {
  1320. sd_read_capacity(sdkp, buffer);
  1321. sd_read_write_protect_flag(sdkp, buffer);
  1322. sd_read_cache_type(sdkp, buffer);
  1323. }
  1324. /*
  1325. * We now have all cache related info, determine how we deal
  1326. * with ordered requests. Note that as the current SCSI
  1327. * dispatch function can alter request order, we cannot use
  1328. * QUEUE_ORDERED_TAG_* even when ordered tag is supported.
  1329. */
  1330. if (sdkp->WCE)
  1331. ordered = sdkp->DPOFUA
  1332. ? QUEUE_ORDERED_DRAIN_FUA : QUEUE_ORDERED_DRAIN_FLUSH;
  1333. else
  1334. ordered = QUEUE_ORDERED_DRAIN;
  1335. blk_queue_ordered(sdkp->disk->queue, ordered, sd_prepare_flush);
  1336. set_capacity(disk, sdkp->capacity);
  1337. kfree(buffer);
  1338. out:
  1339. return 0;
  1340. }
  1341. /**
  1342. * sd_probe - called during driver initialization and whenever a
  1343. * new scsi device is attached to the system. It is called once
  1344. * for each scsi device (not just disks) present.
  1345. * @dev: pointer to device object
  1346. *
  1347. * Returns 0 if successful (or not interested in this scsi device
  1348. * (e.g. scanner)); 1 when there is an error.
  1349. *
  1350. * Note: this function is invoked from the scsi mid-level.
  1351. * This function sets up the mapping between a given
  1352. * <host,channel,id,lun> (found in sdp) and new device name
  1353. * (e.g. /dev/sda). More precisely it is the block device major
  1354. * and minor number that is chosen here.
  1355. *
  1356. * Assume sd_attach is not re-entrant (for time being)
  1357. * Also think about sd_attach() and sd_remove() running coincidentally.
  1358. **/
  1359. static int sd_probe(struct device *dev)
  1360. {
  1361. struct scsi_device *sdp = to_scsi_device(dev);
  1362. struct scsi_disk *sdkp;
  1363. struct gendisk *gd;
  1364. u32 index;
  1365. int error;
  1366. error = -ENODEV;
  1367. if (sdp->type != TYPE_DISK && sdp->type != TYPE_MOD && sdp->type != TYPE_RBC)
  1368. goto out;
  1369. SCSI_LOG_HLQUEUE(3, sdev_printk(KERN_INFO, sdp,
  1370. "sd_attach\n"));
  1371. error = -ENOMEM;
  1372. sdkp = kzalloc(sizeof(*sdkp), GFP_KERNEL);
  1373. if (!sdkp)
  1374. goto out;
  1375. gd = alloc_disk(16);
  1376. if (!gd)
  1377. goto out_free;
  1378. if (!idr_pre_get(&sd_index_idr, GFP_KERNEL))
  1379. goto out_put;
  1380. spin_lock(&sd_index_lock);
  1381. error = idr_get_new(&sd_index_idr, NULL, &index);
  1382. spin_unlock(&sd_index_lock);
  1383. if (index >= SD_MAX_DISKS)
  1384. error = -EBUSY;
  1385. if (error)
  1386. goto out_put;
  1387. sdkp->device = sdp;
  1388. sdkp->driver = &sd_template;
  1389. sdkp->disk = gd;
  1390. sdkp->index = index;
  1391. sdkp->openers = 0;
  1392. if (!sdp->timeout) {
  1393. if (sdp->type != TYPE_MOD)
  1394. sdp->timeout = SD_TIMEOUT;
  1395. else
  1396. sdp->timeout = SD_MOD_TIMEOUT;
  1397. }
  1398. class_device_initialize(&sdkp->cdev);
  1399. sdkp->cdev.dev = &sdp->sdev_gendev;
  1400. sdkp->cdev.class = &sd_disk_class;
  1401. strncpy(sdkp->cdev.class_id, sdp->sdev_gendev.bus_id, BUS_ID_SIZE);
  1402. if (class_device_add(&sdkp->cdev))
  1403. goto out_put;
  1404. get_device(&sdp->sdev_gendev);
  1405. gd->major = sd_major((index & 0xf0) >> 4);
  1406. gd->first_minor = ((index & 0xf) << 4) | (index & 0xfff00);
  1407. gd->minors = 16;
  1408. gd->fops = &sd_fops;
  1409. if (index < 26) {
  1410. sprintf(gd->disk_name, "sd%c", 'a' + index % 26);
  1411. } else if (index < (26 + 1) * 26) {
  1412. sprintf(gd->disk_name, "sd%c%c",
  1413. 'a' + index / 26 - 1,'a' + index % 26);
  1414. } else {
  1415. const unsigned int m1 = (index / 26 - 1) / 26 - 1;
  1416. const unsigned int m2 = (index / 26 - 1) % 26;
  1417. const unsigned int m3 = index % 26;
  1418. sprintf(gd->disk_name, "sd%c%c%c",
  1419. 'a' + m1, 'a' + m2, 'a' + m3);
  1420. }
  1421. gd->private_data = &sdkp->driver;
  1422. gd->queue = sdkp->device->request_queue;
  1423. sd_revalidate_disk(gd);
  1424. gd->driverfs_dev = &sdp->sdev_gendev;
  1425. gd->flags = GENHD_FL_DRIVERFS;
  1426. if (sdp->removable)
  1427. gd->flags |= GENHD_FL_REMOVABLE;
  1428. dev_set_drvdata(dev, sdkp);
  1429. add_disk(gd);
  1430. sd_printk(KERN_NOTICE, sdkp, "Attached SCSI %sdisk\n",
  1431. sdp->removable ? "removable " : "");
  1432. return 0;
  1433. out_put:
  1434. put_disk(gd);
  1435. out_free:
  1436. kfree(sdkp);
  1437. out:
  1438. return error;
  1439. }
  1440. /**
  1441. * sd_remove - called whenever a scsi disk (previously recognized by
  1442. * sd_probe) is detached from the system. It is called (potentially
  1443. * multiple times) during sd module unload.
  1444. * @sdp: pointer to mid level scsi device object
  1445. *
  1446. * Note: this function is invoked from the scsi mid-level.
  1447. * This function potentially frees up a device name (e.g. /dev/sdc)
  1448. * that could be re-used by a subsequent sd_probe().
  1449. * This function is not called when the built-in sd driver is "exit-ed".
  1450. **/
  1451. static int sd_remove(struct device *dev)
  1452. {
  1453. struct scsi_disk *sdkp = dev_get_drvdata(dev);
  1454. class_device_del(&sdkp->cdev);
  1455. del_gendisk(sdkp->disk);
  1456. sd_shutdown(dev);
  1457. mutex_lock(&sd_ref_mutex);
  1458. dev_set_drvdata(dev, NULL);
  1459. class_device_put(&sdkp->cdev);
  1460. mutex_unlock(&sd_ref_mutex);
  1461. return 0;
  1462. }
  1463. /**
  1464. * scsi_disk_release - Called to free the scsi_disk structure
  1465. * @cdev: pointer to embedded class device
  1466. *
  1467. * sd_ref_mutex must be held entering this routine. Because it is
  1468. * called on last put, you should always use the scsi_disk_get()
  1469. * scsi_disk_put() helpers which manipulate the semaphore directly
  1470. * and never do a direct class_device_put().
  1471. **/
  1472. static void scsi_disk_release(struct class_device *cdev)
  1473. {
  1474. struct scsi_disk *sdkp = to_scsi_disk(cdev);
  1475. struct gendisk *disk = sdkp->disk;
  1476. spin_lock(&sd_index_lock);
  1477. idr_remove(&sd_index_idr, sdkp->index);
  1478. spin_unlock(&sd_index_lock);
  1479. disk->private_data = NULL;
  1480. put_disk(disk);
  1481. put_device(&sdkp->device->sdev_gendev);
  1482. kfree(sdkp);
  1483. }
  1484. /*
  1485. * Send a SYNCHRONIZE CACHE instruction down to the device through
  1486. * the normal SCSI command structure. Wait for the command to
  1487. * complete.
  1488. */
  1489. static void sd_shutdown(struct device *dev)
  1490. {
  1491. struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
  1492. if (!sdkp)
  1493. return; /* this can happen */
  1494. if (sdkp->WCE) {
  1495. sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n");
  1496. sd_sync_cache(sdkp);
  1497. }
  1498. scsi_disk_put(sdkp);
  1499. }
  1500. /**
  1501. * init_sd - entry point for this driver (both when built in or when
  1502. * a module).
  1503. *
  1504. * Note: this function registers this driver with the scsi mid-level.
  1505. **/
  1506. static int __init init_sd(void)
  1507. {
  1508. int majors = 0, i, err;
  1509. SCSI_LOG_HLQUEUE(3, printk("init_sd: sd driver entry point\n"));
  1510. for (i = 0; i < SD_MAJORS; i++)
  1511. if (register_blkdev(sd_major(i), "sd") == 0)
  1512. majors++;
  1513. if (!majors)
  1514. return -ENODEV;
  1515. err = class_register(&sd_disk_class);
  1516. if (err)
  1517. goto err_out;
  1518. err = scsi_register_driver(&sd_template.gendrv);
  1519. if (err)
  1520. goto err_out_class;
  1521. return 0;
  1522. err_out_class:
  1523. class_unregister(&sd_disk_class);
  1524. err_out:
  1525. for (i = 0; i < SD_MAJORS; i++)
  1526. unregister_blkdev(sd_major(i), "sd");
  1527. return err;
  1528. }
  1529. /**
  1530. * exit_sd - exit point for this driver (when it is a module).
  1531. *
  1532. * Note: this function unregisters this driver from the scsi mid-level.
  1533. **/
  1534. static void __exit exit_sd(void)
  1535. {
  1536. int i;
  1537. SCSI_LOG_HLQUEUE(3, printk("exit_sd: exiting sd driver\n"));
  1538. scsi_unregister_driver(&sd_template.gendrv);
  1539. class_unregister(&sd_disk_class);
  1540. for (i = 0; i < SD_MAJORS; i++)
  1541. unregister_blkdev(sd_major(i), "sd");
  1542. }
  1543. module_init(init_sd);
  1544. module_exit(exit_sd);
  1545. static void sd_print_sense_hdr(struct scsi_disk *sdkp,
  1546. struct scsi_sense_hdr *sshdr)
  1547. {
  1548. sd_printk(KERN_INFO, sdkp, "");
  1549. scsi_show_sense_hdr(sshdr);
  1550. sd_printk(KERN_INFO, sdkp, "");
  1551. scsi_show_extd_sense(sshdr->asc, sshdr->ascq);
  1552. }
  1553. static void sd_print_result(struct scsi_disk *sdkp, int result)
  1554. {
  1555. sd_printk(KERN_INFO, sdkp, "");
  1556. scsi_show_result(result);
  1557. }