sd.c 47 KB

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