sd.c 52 KB

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