sd.c 48 KB

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