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