sd.c 46 KB

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