sd.c 46 KB

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