sd.c 51 KB

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