sd.c 50 KB

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