sd.c 51 KB

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