/drivers/ide/ide-cd.c

https://bitbucket.org/evzijst/gittest · C · 3524 lines · 2182 code · 535 blank · 807 comment · 568 complexity · 28a50b51c4f2de81781a39e90cf8186b MD5 · raw file

Large files are truncated click here to view the full file

  1. /*
  2. * linux/drivers/ide/ide-cd.c
  3. *
  4. * Copyright (C) 1994, 1995, 1996 scott snyder <snyder@fnald0.fnal.gov>
  5. * Copyright (C) 1996-1998 Erik Andersen <andersee@debian.org>
  6. * Copyright (C) 1998-2000 Jens Axboe <axboe@suse.de>
  7. *
  8. * May be copied or modified under the terms of the GNU General Public
  9. * License. See linux/COPYING for more information.
  10. *
  11. * ATAPI CD-ROM driver. To be used with ide.c.
  12. * See Documentation/cdrom/ide-cd for usage information.
  13. *
  14. * Suggestions are welcome. Patches that work are more welcome though. ;-)
  15. * For those wishing to work on this driver, please be sure you download
  16. * and comply with the latest Mt. Fuji (SFF8090 version 4) and ATAPI
  17. * (SFF-8020i rev 2.6) standards. These documents can be obtained by
  18. * anonymous ftp from:
  19. * ftp://fission.dt.wdc.com/pub/standards/SFF_atapi/spec/SFF8020-r2.6/PS/8020r26.ps
  20. * ftp://ftp.avc-pioneer.com/Mtfuji4/Spec/Fuji4r10.pdf
  21. *
  22. * Drives that deviate from these standards will be accommodated as much
  23. * as possible via compile time or command-line options. Since I only have
  24. * a few drives, you generally need to send me patches...
  25. *
  26. * ----------------------------------
  27. * TO DO LIST:
  28. * -Make it so that Pioneer CD DR-A24X and friends don't get screwed up on
  29. * boot
  30. *
  31. * ----------------------------------
  32. * 1.00 Oct 31, 1994 -- Initial version.
  33. * 1.01 Nov 2, 1994 -- Fixed problem with starting request in
  34. * cdrom_check_status.
  35. * 1.03 Nov 25, 1994 -- leaving unmask_intr[] as a user-setting (as for disks)
  36. * (from mlord) -- minor changes to cdrom_setup()
  37. * -- renamed ide_dev_s to ide_drive_t, enable irq on command
  38. * 2.00 Nov 27, 1994 -- Generalize packet command interface;
  39. * add audio ioctls.
  40. * 2.01 Dec 3, 1994 -- Rework packet command interface to handle devices
  41. * which send an interrupt when ready for a command.
  42. * 2.02 Dec 11, 1994 -- Cache the TOC in the driver.
  43. * Don't use SCMD_PLAYAUDIO_TI; it's not included
  44. * in the current version of ATAPI.
  45. * Try to use LBA instead of track or MSF addressing
  46. * when possible.
  47. * Don't wait for READY_STAT.
  48. * 2.03 Jan 10, 1995 -- Rewrite block read routines to handle block sizes
  49. * other than 2k and to move multiple sectors in a
  50. * single transaction.
  51. * 2.04 Apr 21, 1995 -- Add work-around for Creative Labs CD220E drives.
  52. * Thanks to Nick Saw <cwsaw@pts7.pts.mot.com> for
  53. * help in figuring this out. Ditto for Acer and
  54. * Aztech drives, which seem to have the same problem.
  55. * 2.04b May 30, 1995 -- Fix to match changes in ide.c version 3.16 -ml
  56. * 2.05 Jun 8, 1995 -- Don't attempt to retry after an illegal request
  57. * or data protect error.
  58. * Use HWIF and DEV_HWIF macros as in ide.c.
  59. * Always try to do a request_sense after
  60. * a failed command.
  61. * Include an option to give textual descriptions
  62. * of ATAPI errors.
  63. * Fix a bug in handling the sector cache which
  64. * showed up if the drive returned data in 512 byte
  65. * blocks (like Pioneer drives). Thanks to
  66. * Richard Hirst <srh@gpt.co.uk> for diagnosing this.
  67. * Properly supply the page number field in the
  68. * MODE_SELECT command.
  69. * PLAYAUDIO12 is broken on the Aztech; work around it.
  70. * 2.05x Aug 11, 1995 -- lots of data structure renaming/restructuring in ide.c
  71. * (my apologies to Scott, but now ide-cd.c is independent)
  72. * 3.00 Aug 22, 1995 -- Implement CDROMMULTISESSION ioctl.
  73. * Implement CDROMREADAUDIO ioctl (UNTESTED).
  74. * Use input_ide_data() and output_ide_data().
  75. * Add door locking.
  76. * Fix usage count leak in cdrom_open, which happened
  77. * when a read-write mount was attempted.
  78. * Try to load the disk on open.
  79. * Implement CDROMEJECT_SW ioctl (off by default).
  80. * Read total cdrom capacity during open.
  81. * Rearrange logic in cdrom_decode_status. Issue
  82. * request sense commands for failed packet commands
  83. * from here instead of from cdrom_queue_packet_command.
  84. * Fix a race condition in retrieving error information.
  85. * Suppress printing normal unit attention errors and
  86. * some drive not ready errors.
  87. * Implement CDROMVOLREAD ioctl.
  88. * Implement CDROMREADMODE1/2 ioctls.
  89. * Fix race condition in setting up interrupt handlers
  90. * when the `serialize' option is used.
  91. * 3.01 Sep 2, 1995 -- Fix ordering of reenabling interrupts in
  92. * cdrom_queue_request.
  93. * Another try at using ide_[input,output]_data.
  94. * 3.02 Sep 16, 1995 -- Stick total disk capacity in partition table as well.
  95. * Make VERBOSE_IDE_CD_ERRORS dump failed command again.
  96. * Dump out more information for ILLEGAL REQUEST errs.
  97. * Fix handling of errors occurring before the
  98. * packet command is transferred.
  99. * Fix transfers with odd bytelengths.
  100. * 3.03 Oct 27, 1995 -- Some Creative drives have an id of just `CD'.
  101. * `DCI-2S10' drives are broken too.
  102. * 3.04 Nov 20, 1995 -- So are Vertos drives.
  103. * 3.05 Dec 1, 1995 -- Changes to go with overhaul of ide.c and ide-tape.c
  104. * 3.06 Dec 16, 1995 -- Add support needed for partitions.
  105. * More workarounds for Vertos bugs (based on patches
  106. * from Holger Dietze <dietze@aix520.informatik.uni-leipzig.de>).
  107. * Try to eliminate byteorder assumptions.
  108. * Use atapi_cdrom_subchnl struct definition.
  109. * Add STANDARD_ATAPI compilation option.
  110. * 3.07 Jan 29, 1996 -- More twiddling for broken drives: Sony 55D,
  111. * Vertos 300.
  112. * Add NO_DOOR_LOCKING configuration option.
  113. * Handle drive_cmd requests w/NULL args (for hdparm -t).
  114. * Work around sporadic Sony55e audio play problem.
  115. * 3.07a Feb 11, 1996 -- check drive->id for NULL before dereferencing, to fix
  116. * problem with "hde=cdrom" with no drive present. -ml
  117. * 3.08 Mar 6, 1996 -- More Vertos workarounds.
  118. * 3.09 Apr 5, 1996 -- Add CDROMCLOSETRAY ioctl.
  119. * Switch to using MSF addressing for audio commands.
  120. * Reformat to match kernel tabbing style.
  121. * Add CDROM_GET_UPC ioctl.
  122. * 3.10 Apr 10, 1996 -- Fix compilation error with STANDARD_ATAPI.
  123. * 3.11 Apr 29, 1996 -- Patch from Heiko Eissfeldt <heiko@colossus.escape.de>
  124. * to remove redundant verify_area calls.
  125. * 3.12 May 7, 1996 -- Rudimentary changer support. Based on patches
  126. * from Gerhard Zuber <zuber@berlin.snafu.de>.
  127. * Let open succeed even if there's no loaded disc.
  128. * 3.13 May 19, 1996 -- Fixes for changer code.
  129. * 3.14 May 29, 1996 -- Add work-around for Vertos 600.
  130. * (From Hennus Bergman <hennus@sky.ow.nl>.)
  131. * 3.15 July 2, 1996 -- Added support for Sanyo 3 CD changers
  132. * from Ben Galliart <bgallia@luc.edu> with
  133. * special help from Jeff Lightfoot
  134. * <jeffml@pobox.com>
  135. * 3.15a July 9, 1996 -- Improved Sanyo 3 CD changer identification
  136. * 3.16 Jul 28, 1996 -- Fix from Gadi to reduce kernel stack usage for ioctl.
  137. * 3.17 Sep 17, 1996 -- Tweak audio reads for some drives.
  138. * Start changing CDROMLOADFROMSLOT to CDROM_SELECT_DISC.
  139. * 3.18 Oct 31, 1996 -- Added module and DMA support.
  140. *
  141. *
  142. * 4.00 Nov 5, 1996 -- New ide-cd maintainer,
  143. * Erik B. Andersen <andersee@debian.org>
  144. * -- Newer Creative drives don't always set the error
  145. * register correctly. Make sure we see media changes
  146. * regardless.
  147. * -- Integrate with generic cdrom driver.
  148. * -- CDROMGETSPINDOWN and CDROMSETSPINDOWN ioctls, based on
  149. * a patch from Ciro Cattuto <>.
  150. * -- Call set_device_ro.
  151. * -- Implement CDROMMECHANISMSTATUS and CDROMSLOTTABLE
  152. * ioctls, based on patch by Erik Andersen
  153. * -- Add some probes of drive capability during setup.
  154. *
  155. * 4.01 Nov 11, 1996 -- Split into ide-cd.c and ide-cd.h
  156. * -- Removed CDROMMECHANISMSTATUS and CDROMSLOTTABLE
  157. * ioctls in favor of a generalized approach
  158. * using the generic cdrom driver.
  159. * -- Fully integrated with the 2.1.X kernel.
  160. * -- Other stuff that I forgot (lots of changes)
  161. *
  162. * 4.02 Dec 01, 1996 -- Applied patch from Gadi Oxman <gadio@netvision.net.il>
  163. * to fix the drive door locking problems.
  164. *
  165. * 4.03 Dec 04, 1996 -- Added DSC overlap support.
  166. * 4.04 Dec 29, 1996 -- Added CDROMREADRAW ioclt based on patch
  167. * by Ales Makarov (xmakarov@sun.felk.cvut.cz)
  168. *
  169. * 4.05 Nov 20, 1997 -- Modified to print more drive info on init
  170. * Minor other changes
  171. * Fix errors on CDROMSTOP (If you have a "Dolphin",
  172. * you must define IHAVEADOLPHIN)
  173. * Added identifier so new Sanyo CD-changer works
  174. * Better detection if door locking isn't supported
  175. *
  176. * 4.06 Dec 17, 1997 -- fixed endless "tray open" messages -ml
  177. * 4.07 Dec 17, 1997 -- fallback to set pc->stat on "tray open"
  178. * 4.08 Dec 18, 1997 -- spew less noise when tray is empty
  179. * -- fix speed display for ACER 24X, 18X
  180. * 4.09 Jan 04, 1998 -- fix handling of the last block so we return
  181. * an end of file instead of an I/O error (Gadi)
  182. * 4.10 Jan 24, 1998 -- fixed a bug so now changers can change to a new
  183. * slot when there is no disc in the current slot.
  184. * -- Fixed a memory leak where info->changer_info was
  185. * malloc'ed but never free'd when closing the device.
  186. * -- Cleaned up the global namespace a bit by making more
  187. * functions static that should already have been.
  188. * 4.11 Mar 12, 1998 -- Added support for the CDROM_SELECT_SPEED ioctl
  189. * based on a patch for 2.0.33 by Jelle Foks
  190. * <jelle@scintilla.utwente.nl>, a patch for 2.0.33
  191. * by Toni Giorgino <toni@pcape2.pi.infn.it>, the SCSI
  192. * version, and my own efforts. -erik
  193. * -- Fixed a stupid bug which egcs was kind enough to
  194. * inform me of where "Illegal mode for this track"
  195. * was never returned due to a comparison on data
  196. * types of limited range.
  197. * 4.12 Mar 29, 1998 -- Fixed bug in CDROM_SELECT_SPEED so write speed is
  198. * now set ionly for CD-R and CD-RW drives. I had
  199. * removed this support because it produced errors.
  200. * It produced errors _only_ for non-writers. duh.
  201. * 4.13 May 05, 1998 -- Suppress useless "in progress of becoming ready"
  202. * messages, since this is not an error.
  203. * -- Change error messages to be const
  204. * -- Remove a "\t" which looks ugly in the syslogs
  205. * 4.14 July 17, 1998 -- Change to pointing to .ps version of ATAPI spec
  206. * since the .pdf version doesn't seem to work...
  207. * -- Updated the TODO list to something more current.
  208. *
  209. * 4.15 Aug 25, 1998 -- Updated ide-cd.h to respect mechine endianess,
  210. * patch thanks to "Eddie C. Dost" <ecd@skynet.be>
  211. *
  212. * 4.50 Oct 19, 1998 -- New maintainers!
  213. * Jens Axboe <axboe@image.dk>
  214. * Chris Zwilling <chris@cloudnet.com>
  215. *
  216. * 4.51 Dec 23, 1998 -- Jens Axboe <axboe@image.dk>
  217. * - ide_cdrom_reset enabled since the ide subsystem
  218. * handles resets fine now. <axboe@image.dk>
  219. * - Transfer size fix for Samsung CD-ROMs, thanks to
  220. * "Ville Hallik" <ville.hallik@mail.ee>.
  221. * - other minor stuff.
  222. *
  223. * 4.52 Jan 19, 1999 -- Jens Axboe <axboe@image.dk>
  224. * - Detect DVD-ROM/RAM drives
  225. *
  226. * 4.53 Feb 22, 1999 - Include other model Samsung and one Goldstar
  227. * drive in transfer size limit.
  228. * - Fix the I/O error when doing eject without a medium
  229. * loaded on some drives.
  230. * - CDROMREADMODE2 is now implemented through
  231. * CDROMREADRAW, since many drives don't support
  232. * MODE2 (even though ATAPI 2.6 says they must).
  233. * - Added ignore parameter to ide-cd (as a module), eg
  234. * insmod ide-cd ignore='hda hdb'
  235. * Useful when using ide-cd in conjunction with
  236. * ide-scsi. TODO: non-modular way of doing the
  237. * same.
  238. *
  239. * 4.54 Aug 5, 1999 - Support for MMC2 class commands through the generic
  240. * packet interface to cdrom.c.
  241. * - Unified audio ioctl support, most of it.
  242. * - cleaned up various deprecated verify_area().
  243. * - Added ide_cdrom_packet() as the interface for
  244. * the Uniform generic_packet().
  245. * - bunch of other stuff, will fill in logs later.
  246. * - report 1 slot for non-changers, like the other
  247. * cd-rom drivers. don't report select disc for
  248. * non-changers as well.
  249. * - mask out audio playing, if the device can't do it.
  250. *
  251. * 4.55 Sep 1, 1999 - Eliminated the rest of the audio ioctls, except
  252. * for CDROMREADTOC[ENTRY|HEADER]. Some of the drivers
  253. * use this independently of the actual audio handling.
  254. * They will disappear later when I get the time to
  255. * do it cleanly.
  256. * - Minimize the TOC reading - only do it when we
  257. * know a media change has occurred.
  258. * - Moved all the CDROMREADx ioctls to the Uniform layer.
  259. * - Heiko Eissfeldt <heiko@colossus.escape.de> supplied
  260. * some fixes for CDI.
  261. * - CD-ROM leaving door locked fix from Andries
  262. * Brouwer <Andries.Brouwer@cwi.nl>
  263. * - Erik Andersen <andersen@xmission.com> unified
  264. * commands across the various drivers and how
  265. * sense errors are handled.
  266. *
  267. * 4.56 Sep 12, 1999 - Removed changer support - it is now in the
  268. * Uniform layer.
  269. * - Added partition based multisession handling.
  270. * - Mode sense and mode select moved to the
  271. * Uniform layer.
  272. * - Fixed a problem with WPI CDS-32X drive - it
  273. * failed the capabilities
  274. *
  275. * 4.57 Apr 7, 2000 - Fixed sense reporting.
  276. * - Fixed possible oops in ide_cdrom_get_last_session()
  277. * - Fix locking mania and make ide_cdrom_reset relock
  278. * - Stop spewing errors to log when magicdev polls with
  279. * TEST_UNIT_READY on some drives.
  280. * - Various fixes from Tobias Ringstrom:
  281. * tray if it was locked prior to the reset.
  282. * - cdrom_read_capacity returns one frame too little.
  283. * - Fix real capacity reporting.
  284. *
  285. * 4.58 May 1, 2000 - Clean up ACER50 stuff.
  286. * - Fix small problem with ide_cdrom_capacity
  287. *
  288. * 4.59 Aug 11, 2000 - Fix changer problem in cdrom_read_toc, we weren't
  289. * correctly sensing a disc change.
  290. * - Rearranged some code
  291. * - Use extended sense on drives that support it for
  292. * correctly reporting tray status -- from
  293. * Michael D Johnson <johnsom@orst.edu>
  294. * 4.60 Dec 17, 2003 - Add mt rainier support
  295. * - Bump timeout for packet commands, matches sr
  296. * - Odd stuff
  297. * 4.61 Jan 22, 2004 - support hardware sector sizes other than 2kB,
  298. * Pascal Schmidt <der.eremit@email.de>
  299. *
  300. *************************************************************************/
  301. #define IDECD_VERSION "4.61"
  302. #include <linux/config.h>
  303. #include <linux/module.h>
  304. #include <linux/types.h>
  305. #include <linux/kernel.h>
  306. #include <linux/delay.h>
  307. #include <linux/timer.h>
  308. #include <linux/slab.h>
  309. #include <linux/interrupt.h>
  310. #include <linux/errno.h>
  311. #include <linux/cdrom.h>
  312. #include <linux/ide.h>
  313. #include <linux/completion.h>
  314. #include <scsi/scsi.h> /* For SCSI -> ATAPI command conversion */
  315. #include <asm/irq.h>
  316. #include <asm/io.h>
  317. #include <asm/byteorder.h>
  318. #include <asm/uaccess.h>
  319. #include <asm/unaligned.h>
  320. #include "ide-cd.h"
  321. static DECLARE_MUTEX(idecd_ref_sem);
  322. #define to_ide_cd(obj) container_of(obj, struct cdrom_info, kref)
  323. #define ide_cd_g(disk) \
  324. container_of((disk)->private_data, struct cdrom_info, driver)
  325. static struct cdrom_info *ide_cd_get(struct gendisk *disk)
  326. {
  327. struct cdrom_info *cd = NULL;
  328. down(&idecd_ref_sem);
  329. cd = ide_cd_g(disk);
  330. if (cd)
  331. kref_get(&cd->kref);
  332. up(&idecd_ref_sem);
  333. return cd;
  334. }
  335. static void ide_cd_release(struct kref *);
  336. static void ide_cd_put(struct cdrom_info *cd)
  337. {
  338. down(&idecd_ref_sem);
  339. kref_put(&cd->kref, ide_cd_release);
  340. up(&idecd_ref_sem);
  341. }
  342. /****************************************************************************
  343. * Generic packet command support and error handling routines.
  344. */
  345. /* Mark that we've seen a media change, and invalidate our internal
  346. buffers. */
  347. static void cdrom_saw_media_change (ide_drive_t *drive)
  348. {
  349. struct cdrom_info *info = drive->driver_data;
  350. CDROM_STATE_FLAGS (drive)->media_changed = 1;
  351. CDROM_STATE_FLAGS (drive)->toc_valid = 0;
  352. info->nsectors_buffered = 0;
  353. }
  354. static int cdrom_log_sense(ide_drive_t *drive, struct request *rq,
  355. struct request_sense *sense)
  356. {
  357. int log = 0;
  358. if (!sense || !rq || (rq->flags & REQ_QUIET))
  359. return 0;
  360. switch (sense->sense_key) {
  361. case NO_SENSE: case RECOVERED_ERROR:
  362. break;
  363. case NOT_READY:
  364. /*
  365. * don't care about tray state messages for
  366. * e.g. capacity commands or in-progress or
  367. * becoming ready
  368. */
  369. if (sense->asc == 0x3a || sense->asc == 0x04)
  370. break;
  371. log = 1;
  372. break;
  373. case ILLEGAL_REQUEST:
  374. /*
  375. * don't log START_STOP unit with LoEj set, since
  376. * we cannot reliably check if drive can auto-close
  377. */
  378. if (rq->cmd[0] == GPCMD_START_STOP_UNIT && sense->asc == 0x24)
  379. log = 0;
  380. break;
  381. case UNIT_ATTENTION:
  382. /*
  383. * Make good and sure we've seen this potential media
  384. * change. Some drives (i.e. Creative) fail to present
  385. * the correct sense key in the error register.
  386. */
  387. cdrom_saw_media_change(drive);
  388. break;
  389. default:
  390. log = 1;
  391. break;
  392. }
  393. return log;
  394. }
  395. static
  396. void cdrom_analyze_sense_data(ide_drive_t *drive,
  397. struct request *failed_command,
  398. struct request_sense *sense)
  399. {
  400. if (!cdrom_log_sense(drive, failed_command, sense))
  401. return;
  402. /*
  403. * If a read toc is executed for a CD-R or CD-RW medium where
  404. * the first toc has not been recorded yet, it will fail with
  405. * 05/24/00 (which is a confusing error)
  406. */
  407. if (failed_command && failed_command->cmd[0] == GPCMD_READ_TOC_PMA_ATIP)
  408. if (sense->sense_key == 0x05 && sense->asc == 0x24)
  409. return;
  410. #if VERBOSE_IDE_CD_ERRORS
  411. {
  412. int i;
  413. const char *s;
  414. char buf[80];
  415. printk ("ATAPI device %s:\n", drive->name);
  416. if (sense->error_code==0x70)
  417. printk(" Error: ");
  418. else if (sense->error_code==0x71)
  419. printk(" Deferred Error: ");
  420. else if (sense->error_code == 0x7f)
  421. printk(" Vendor-specific Error: ");
  422. else
  423. printk(" Unknown Error Type: ");
  424. if (sense->sense_key < ARY_LEN(sense_key_texts))
  425. s = sense_key_texts[sense->sense_key];
  426. else
  427. s = "bad sense key!";
  428. printk("%s -- (Sense key=0x%02x)\n", s, sense->sense_key);
  429. if (sense->asc == 0x40) {
  430. sprintf(buf, "Diagnostic failure on component 0x%02x",
  431. sense->ascq);
  432. s = buf;
  433. } else {
  434. int lo = 0, mid, hi = ARY_LEN(sense_data_texts);
  435. unsigned long key = (sense->sense_key << 16);
  436. key |= (sense->asc << 8);
  437. if (!(sense->ascq >= 0x80 && sense->ascq <= 0xdd))
  438. key |= sense->ascq;
  439. s = NULL;
  440. while (hi > lo) {
  441. mid = (lo + hi) / 2;
  442. if (sense_data_texts[mid].asc_ascq == key ||
  443. sense_data_texts[mid].asc_ascq == (0xff0000|key)) {
  444. s = sense_data_texts[mid].text;
  445. break;
  446. }
  447. else if (sense_data_texts[mid].asc_ascq > key)
  448. hi = mid;
  449. else
  450. lo = mid+1;
  451. }
  452. }
  453. if (s == NULL) {
  454. if (sense->asc > 0x80)
  455. s = "(vendor-specific error)";
  456. else
  457. s = "(reserved error code)";
  458. }
  459. printk(KERN_ERR " %s -- (asc=0x%02x, ascq=0x%02x)\n",
  460. s, sense->asc, sense->ascq);
  461. if (failed_command != NULL) {
  462. int lo=0, mid, hi= ARY_LEN (packet_command_texts);
  463. s = NULL;
  464. while (hi > lo) {
  465. mid = (lo + hi) / 2;
  466. if (packet_command_texts[mid].packet_command ==
  467. failed_command->cmd[0]) {
  468. s = packet_command_texts[mid].text;
  469. break;
  470. }
  471. if (packet_command_texts[mid].packet_command >
  472. failed_command->cmd[0])
  473. hi = mid;
  474. else
  475. lo = mid+1;
  476. }
  477. printk (KERN_ERR " The failed \"%s\" packet command was: \n \"", s);
  478. for (i=0; i<sizeof (failed_command->cmd); i++)
  479. printk ("%02x ", failed_command->cmd[i]);
  480. printk ("\"\n");
  481. }
  482. /* The SKSV bit specifies validity of the sense_key_specific
  483. * in the next two commands. It is bit 7 of the first byte.
  484. * In the case of NOT_READY, if SKSV is set the drive can
  485. * give us nice ETA readings.
  486. */
  487. if (sense->sense_key == NOT_READY && (sense->sks[0] & 0x80)) {
  488. int progress = (sense->sks[1] << 8 | sense->sks[2]) * 100;
  489. printk(KERN_ERR " Command is %02d%% complete\n", progress / 0xffff);
  490. }
  491. if (sense->sense_key == ILLEGAL_REQUEST &&
  492. (sense->sks[0] & 0x80) != 0) {
  493. printk(KERN_ERR " Error in %s byte %d",
  494. (sense->sks[0] & 0x40) != 0 ?
  495. "command packet" : "command data",
  496. (sense->sks[1] << 8) + sense->sks[2]);
  497. if ((sense->sks[0] & 0x40) != 0)
  498. printk (" bit %d", sense->sks[0] & 0x07);
  499. printk ("\n");
  500. }
  501. }
  502. #else /* not VERBOSE_IDE_CD_ERRORS */
  503. /* Suppress printing unit attention and `in progress of becoming ready'
  504. errors when we're not being verbose. */
  505. if (sense->sense_key == UNIT_ATTENTION ||
  506. (sense->sense_key == NOT_READY && (sense->asc == 4 ||
  507. sense->asc == 0x3a)))
  508. return;
  509. printk(KERN_ERR "%s: error code: 0x%02x sense_key: 0x%02x asc: 0x%02x ascq: 0x%02x\n",
  510. drive->name,
  511. sense->error_code, sense->sense_key,
  512. sense->asc, sense->ascq);
  513. #endif /* not VERBOSE_IDE_CD_ERRORS */
  514. }
  515. /*
  516. * Initialize a ide-cd packet command request
  517. */
  518. static void cdrom_prepare_request(ide_drive_t *drive, struct request *rq)
  519. {
  520. struct cdrom_info *cd = drive->driver_data;
  521. ide_init_drive_cmd(rq);
  522. rq->flags = REQ_PC;
  523. rq->rq_disk = cd->disk;
  524. }
  525. static void cdrom_queue_request_sense(ide_drive_t *drive, void *sense,
  526. struct request *failed_command)
  527. {
  528. struct cdrom_info *info = drive->driver_data;
  529. struct request *rq = &info->request_sense_request;
  530. if (sense == NULL)
  531. sense = &info->sense_data;
  532. /* stuff the sense request in front of our current request */
  533. cdrom_prepare_request(drive, rq);
  534. rq->data = sense;
  535. rq->cmd[0] = GPCMD_REQUEST_SENSE;
  536. rq->cmd[4] = rq->data_len = 18;
  537. rq->flags = REQ_SENSE;
  538. /* NOTE! Save the failed command in "rq->buffer" */
  539. rq->buffer = (void *) failed_command;
  540. (void) ide_do_drive_cmd(drive, rq, ide_preempt);
  541. }
  542. static void cdrom_end_request (ide_drive_t *drive, int uptodate)
  543. {
  544. struct request *rq = HWGROUP(drive)->rq;
  545. int nsectors = rq->hard_cur_sectors;
  546. if ((rq->flags & REQ_SENSE) && uptodate) {
  547. /*
  548. * For REQ_SENSE, "rq->buffer" points to the original failed
  549. * request
  550. */
  551. struct request *failed = (struct request *) rq->buffer;
  552. struct cdrom_info *info = drive->driver_data;
  553. void *sense = &info->sense_data;
  554. unsigned long flags;
  555. if (failed) {
  556. if (failed->sense) {
  557. sense = failed->sense;
  558. failed->sense_len = rq->sense_len;
  559. }
  560. /*
  561. * now end failed request
  562. */
  563. spin_lock_irqsave(&ide_lock, flags);
  564. end_that_request_chunk(failed, 0, failed->data_len);
  565. end_that_request_last(failed);
  566. spin_unlock_irqrestore(&ide_lock, flags);
  567. }
  568. cdrom_analyze_sense_data(drive, failed, sense);
  569. }
  570. if (!rq->current_nr_sectors && blk_fs_request(rq))
  571. uptodate = 1;
  572. /* make sure it's fully ended */
  573. if (blk_pc_request(rq))
  574. nsectors = (rq->data_len + 511) >> 9;
  575. if (!nsectors)
  576. nsectors = 1;
  577. ide_end_request(drive, uptodate, nsectors);
  578. }
  579. /* Returns 0 if the request should be continued.
  580. Returns 1 if the request was ended. */
  581. static int cdrom_decode_status(ide_drive_t *drive, int good_stat, int *stat_ret)
  582. {
  583. struct request *rq = HWGROUP(drive)->rq;
  584. int stat, err, sense_key;
  585. /* Check for errors. */
  586. stat = HWIF(drive)->INB(IDE_STATUS_REG);
  587. if (stat_ret)
  588. *stat_ret = stat;
  589. if (OK_STAT(stat, good_stat, BAD_R_STAT))
  590. return 0;
  591. /* Get the IDE error register. */
  592. err = HWIF(drive)->INB(IDE_ERROR_REG);
  593. sense_key = err >> 4;
  594. if (rq == NULL) {
  595. printk("%s: missing rq in cdrom_decode_status\n", drive->name);
  596. return 1;
  597. }
  598. if (rq->flags & REQ_SENSE) {
  599. /* We got an error trying to get sense info
  600. from the drive (probably while trying
  601. to recover from a former error). Just give up. */
  602. rq->flags |= REQ_FAILED;
  603. cdrom_end_request(drive, 0);
  604. ide_error(drive, "request sense failure", stat);
  605. return 1;
  606. } else if (rq->flags & (REQ_PC | REQ_BLOCK_PC)) {
  607. /* All other functions, except for READ. */
  608. unsigned long flags;
  609. /*
  610. * if we have an error, pass back CHECK_CONDITION as the
  611. * scsi status byte
  612. */
  613. if ((rq->flags & REQ_BLOCK_PC) && !rq->errors)
  614. rq->errors = SAM_STAT_CHECK_CONDITION;
  615. /* Check for tray open. */
  616. if (sense_key == NOT_READY) {
  617. cdrom_saw_media_change (drive);
  618. } else if (sense_key == UNIT_ATTENTION) {
  619. /* Check for media change. */
  620. cdrom_saw_media_change (drive);
  621. /*printk("%s: media changed\n",drive->name);*/
  622. return 0;
  623. } else if (!(rq->flags & REQ_QUIET)) {
  624. /* Otherwise, print an error. */
  625. ide_dump_status(drive, "packet command error", stat);
  626. }
  627. rq->flags |= REQ_FAILED;
  628. /*
  629. * instead of playing games with moving completions around,
  630. * remove failed request completely and end it when the
  631. * request sense has completed
  632. */
  633. if (stat & ERR_STAT) {
  634. spin_lock_irqsave(&ide_lock, flags);
  635. blkdev_dequeue_request(rq);
  636. HWGROUP(drive)->rq = NULL;
  637. spin_unlock_irqrestore(&ide_lock, flags);
  638. cdrom_queue_request_sense(drive, rq->sense, rq);
  639. } else
  640. cdrom_end_request(drive, 0);
  641. } else if (blk_fs_request(rq)) {
  642. int do_end_request = 0;
  643. /* Handle errors from READ and WRITE requests. */
  644. if (blk_noretry_request(rq))
  645. do_end_request = 1;
  646. if (sense_key == NOT_READY) {
  647. /* Tray open. */
  648. if (rq_data_dir(rq) == READ) {
  649. cdrom_saw_media_change (drive);
  650. /* Fail the request. */
  651. printk ("%s: tray open\n", drive->name);
  652. do_end_request = 1;
  653. } else {
  654. struct cdrom_info *info = drive->driver_data;
  655. /* allow the drive 5 seconds to recover, some
  656. * devices will return this error while flushing
  657. * data from cache */
  658. if (!rq->errors)
  659. info->write_timeout = jiffies + ATAPI_WAIT_WRITE_BUSY;
  660. rq->errors = 1;
  661. if (time_after(jiffies, info->write_timeout))
  662. do_end_request = 1;
  663. else {
  664. unsigned long flags;
  665. /*
  666. * take a breather relying on the
  667. * unplug timer to kick us again
  668. */
  669. spin_lock_irqsave(&ide_lock, flags);
  670. blk_plug_device(drive->queue);
  671. spin_unlock_irqrestore(&ide_lock,flags);
  672. return 1;
  673. }
  674. }
  675. } else if (sense_key == UNIT_ATTENTION) {
  676. /* Media change. */
  677. cdrom_saw_media_change (drive);
  678. /* Arrange to retry the request.
  679. But be sure to give up if we've retried
  680. too many times. */
  681. if (++rq->errors > ERROR_MAX)
  682. do_end_request = 1;
  683. } else if (sense_key == ILLEGAL_REQUEST ||
  684. sense_key == DATA_PROTECT) {
  685. /* No point in retrying after an illegal
  686. request or data protect error.*/
  687. ide_dump_status (drive, "command error", stat);
  688. do_end_request = 1;
  689. } else if (sense_key == MEDIUM_ERROR) {
  690. /* No point in re-trying a zillion times on a bad
  691. * sector... If we got here the error is not correctable */
  692. ide_dump_status (drive, "media error (bad sector)", stat);
  693. do_end_request = 1;
  694. } else if (sense_key == BLANK_CHECK) {
  695. /* Disk appears blank ?? */
  696. ide_dump_status (drive, "media error (blank)", stat);
  697. do_end_request = 1;
  698. } else if ((err & ~ABRT_ERR) != 0) {
  699. /* Go to the default handler
  700. for other errors. */
  701. ide_error(drive, "cdrom_decode_status", stat);
  702. return 1;
  703. } else if ((++rq->errors > ERROR_MAX)) {
  704. /* We've racked up too many retries. Abort. */
  705. do_end_request = 1;
  706. }
  707. if (do_end_request)
  708. cdrom_end_request(drive, 0);
  709. /* If we got a CHECK_CONDITION status,
  710. queue a request sense command. */
  711. if ((stat & ERR_STAT) != 0)
  712. cdrom_queue_request_sense(drive, NULL, NULL);
  713. } else {
  714. blk_dump_rq_flags(rq, "ide-cd: bad rq");
  715. cdrom_end_request(drive, 0);
  716. }
  717. /* Retry, or handle the next request. */
  718. return 1;
  719. }
  720. static int cdrom_timer_expiry(ide_drive_t *drive)
  721. {
  722. struct request *rq = HWGROUP(drive)->rq;
  723. unsigned long wait = 0;
  724. /*
  725. * Some commands are *slow* and normally take a long time to
  726. * complete. Usually we can use the ATAPI "disconnect" to bypass
  727. * this, but not all commands/drives support that. Let
  728. * ide_timer_expiry keep polling us for these.
  729. */
  730. switch (rq->cmd[0]) {
  731. case GPCMD_BLANK:
  732. case GPCMD_FORMAT_UNIT:
  733. case GPCMD_RESERVE_RZONE_TRACK:
  734. case GPCMD_CLOSE_TRACK:
  735. case GPCMD_FLUSH_CACHE:
  736. wait = ATAPI_WAIT_PC;
  737. break;
  738. default:
  739. if (!(rq->flags & REQ_QUIET))
  740. printk(KERN_INFO "ide-cd: cmd 0x%x timed out\n", rq->cmd[0]);
  741. wait = 0;
  742. break;
  743. }
  744. return wait;
  745. }
  746. /* Set up the device registers for transferring a packet command on DEV,
  747. expecting to later transfer XFERLEN bytes. HANDLER is the routine
  748. which actually transfers the command to the drive. If this is a
  749. drq_interrupt device, this routine will arrange for HANDLER to be
  750. called when the interrupt from the drive arrives. Otherwise, HANDLER
  751. will be called immediately after the drive is prepared for the transfer. */
  752. static ide_startstop_t cdrom_start_packet_command(ide_drive_t *drive,
  753. int xferlen,
  754. ide_handler_t *handler)
  755. {
  756. ide_startstop_t startstop;
  757. struct cdrom_info *info = drive->driver_data;
  758. ide_hwif_t *hwif = drive->hwif;
  759. /* Wait for the controller to be idle. */
  760. if (ide_wait_stat(&startstop, drive, 0, BUSY_STAT, WAIT_READY))
  761. return startstop;
  762. if (info->dma)
  763. info->dma = !hwif->dma_setup(drive);
  764. /* Set up the controller registers. */
  765. /* FIXME: for Virtual DMA we must check harder */
  766. HWIF(drive)->OUTB(info->dma, IDE_FEATURE_REG);
  767. HWIF(drive)->OUTB(0, IDE_IREASON_REG);
  768. HWIF(drive)->OUTB(0, IDE_SECTOR_REG);
  769. HWIF(drive)->OUTB(xferlen & 0xff, IDE_BCOUNTL_REG);
  770. HWIF(drive)->OUTB(xferlen >> 8 , IDE_BCOUNTH_REG);
  771. if (IDE_CONTROL_REG)
  772. HWIF(drive)->OUTB(drive->ctl, IDE_CONTROL_REG);
  773. if (CDROM_CONFIG_FLAGS (drive)->drq_interrupt) {
  774. /* packet command */
  775. ide_execute_command(drive, WIN_PACKETCMD, handler, ATAPI_WAIT_PC, cdrom_timer_expiry);
  776. return ide_started;
  777. } else {
  778. unsigned long flags;
  779. /* packet command */
  780. spin_lock_irqsave(&ide_lock, flags);
  781. hwif->OUTBSYNC(drive, WIN_PACKETCMD, IDE_COMMAND_REG);
  782. ndelay(400);
  783. spin_unlock_irqrestore(&ide_lock, flags);
  784. return (*handler) (drive);
  785. }
  786. }
  787. /* Send a packet command to DRIVE described by CMD_BUF and CMD_LEN.
  788. The device registers must have already been prepared
  789. by cdrom_start_packet_command.
  790. HANDLER is the interrupt handler to call when the command completes
  791. or there's data ready. */
  792. /*
  793. * changed 5 parameters to 3 for dvd-ram
  794. * struct packet_command *pc; now packet_command_t *pc;
  795. */
  796. #define ATAPI_MIN_CDB_BYTES 12
  797. static ide_startstop_t cdrom_transfer_packet_command (ide_drive_t *drive,
  798. struct request *rq,
  799. ide_handler_t *handler)
  800. {
  801. ide_hwif_t *hwif = drive->hwif;
  802. int cmd_len;
  803. struct cdrom_info *info = drive->driver_data;
  804. ide_startstop_t startstop;
  805. if (CDROM_CONFIG_FLAGS(drive)->drq_interrupt) {
  806. /* Here we should have been called after receiving an interrupt
  807. from the device. DRQ should how be set. */
  808. /* Check for errors. */
  809. if (cdrom_decode_status(drive, DRQ_STAT, NULL))
  810. return ide_stopped;
  811. } else {
  812. /* Otherwise, we must wait for DRQ to get set. */
  813. if (ide_wait_stat(&startstop, drive, DRQ_STAT,
  814. BUSY_STAT, WAIT_READY))
  815. return startstop;
  816. }
  817. /* Arm the interrupt handler. */
  818. ide_set_handler(drive, handler, rq->timeout, cdrom_timer_expiry);
  819. /* ATAPI commands get padded out to 12 bytes minimum */
  820. cmd_len = COMMAND_SIZE(rq->cmd[0]);
  821. if (cmd_len < ATAPI_MIN_CDB_BYTES)
  822. cmd_len = ATAPI_MIN_CDB_BYTES;
  823. /* Send the command to the device. */
  824. HWIF(drive)->atapi_output_bytes(drive, rq->cmd, cmd_len);
  825. /* Start the DMA if need be */
  826. if (info->dma)
  827. hwif->dma_start(drive);
  828. return ide_started;
  829. }
  830. /****************************************************************************
  831. * Block read functions.
  832. */
  833. /*
  834. * Buffer up to SECTORS_TO_TRANSFER sectors from the drive in our sector
  835. * buffer. Once the first sector is added, any subsequent sectors are
  836. * assumed to be continuous (until the buffer is cleared). For the first
  837. * sector added, SECTOR is its sector number. (SECTOR is then ignored until
  838. * the buffer is cleared.)
  839. */
  840. static void cdrom_buffer_sectors (ide_drive_t *drive, unsigned long sector,
  841. int sectors_to_transfer)
  842. {
  843. struct cdrom_info *info = drive->driver_data;
  844. /* Number of sectors to read into the buffer. */
  845. int sectors_to_buffer = min_t(int, sectors_to_transfer,
  846. (SECTOR_BUFFER_SIZE >> SECTOR_BITS) -
  847. info->nsectors_buffered);
  848. char *dest;
  849. /* If we couldn't get a buffer, don't try to buffer anything... */
  850. if (info->buffer == NULL)
  851. sectors_to_buffer = 0;
  852. /* If this is the first sector in the buffer, remember its number. */
  853. if (info->nsectors_buffered == 0)
  854. info->sector_buffered = sector;
  855. /* Read the data into the buffer. */
  856. dest = info->buffer + info->nsectors_buffered * SECTOR_SIZE;
  857. while (sectors_to_buffer > 0) {
  858. HWIF(drive)->atapi_input_bytes(drive, dest, SECTOR_SIZE);
  859. --sectors_to_buffer;
  860. --sectors_to_transfer;
  861. ++info->nsectors_buffered;
  862. dest += SECTOR_SIZE;
  863. }
  864. /* Throw away any remaining data. */
  865. while (sectors_to_transfer > 0) {
  866. static char dum[SECTOR_SIZE];
  867. HWIF(drive)->atapi_input_bytes(drive, dum, sizeof (dum));
  868. --sectors_to_transfer;
  869. }
  870. }
  871. /*
  872. * Check the contents of the interrupt reason register from the cdrom
  873. * and attempt to recover if there are problems. Returns 0 if everything's
  874. * ok; nonzero if the request has been terminated.
  875. */
  876. static inline
  877. int cdrom_read_check_ireason (ide_drive_t *drive, int len, int ireason)
  878. {
  879. if (ireason == 2)
  880. return 0;
  881. else if (ireason == 0) {
  882. /* Whoops... The drive is expecting to receive data from us! */
  883. printk(KERN_ERR "%s: read_intr: Drive wants to transfer data the "
  884. "wrong way!\n", drive->name);
  885. /* Throw some data at the drive so it doesn't hang
  886. and quit this request. */
  887. while (len > 0) {
  888. int dum = 0;
  889. HWIF(drive)->atapi_output_bytes(drive, &dum, sizeof (dum));
  890. len -= sizeof (dum);
  891. }
  892. } else if (ireason == 1) {
  893. /* Some drives (ASUS) seem to tell us that status
  894. * info is available. just get it and ignore.
  895. */
  896. (void) HWIF(drive)->INB(IDE_STATUS_REG);
  897. return 0;
  898. } else {
  899. /* Drive wants a command packet, or invalid ireason... */
  900. printk(KERN_ERR "%s: read_intr: bad interrupt reason %x\n", drive->name,
  901. ireason);
  902. }
  903. cdrom_end_request(drive, 0);
  904. return -1;
  905. }
  906. /*
  907. * Interrupt routine. Called when a read request has completed.
  908. */
  909. static ide_startstop_t cdrom_read_intr (ide_drive_t *drive)
  910. {
  911. int stat;
  912. int ireason, len, sectors_to_transfer, nskip;
  913. struct cdrom_info *info = drive->driver_data;
  914. u8 lowcyl = 0, highcyl = 0;
  915. int dma = info->dma, dma_error = 0;
  916. struct request *rq = HWGROUP(drive)->rq;
  917. /*
  918. * handle dma case
  919. */
  920. if (dma) {
  921. info->dma = 0;
  922. if ((dma_error = HWIF(drive)->ide_dma_end(drive)))
  923. __ide_dma_off(drive);
  924. }
  925. if (cdrom_decode_status(drive, 0, &stat))
  926. return ide_stopped;
  927. if (dma) {
  928. if (!dma_error) {
  929. ide_end_request(drive, 1, rq->nr_sectors);
  930. return ide_stopped;
  931. } else
  932. return ide_error(drive, "dma error", stat);
  933. }
  934. /* Read the interrupt reason and the transfer length. */
  935. ireason = HWIF(drive)->INB(IDE_IREASON_REG) & 0x3;
  936. lowcyl = HWIF(drive)->INB(IDE_BCOUNTL_REG);
  937. highcyl = HWIF(drive)->INB(IDE_BCOUNTH_REG);
  938. len = lowcyl + (256 * highcyl);
  939. /* If DRQ is clear, the command has completed. */
  940. if ((stat & DRQ_STAT) == 0) {
  941. /* If we're not done filling the current buffer, complain.
  942. Otherwise, complete the command normally. */
  943. if (rq->current_nr_sectors > 0) {
  944. printk (KERN_ERR "%s: cdrom_read_intr: data underrun (%d blocks)\n",
  945. drive->name, rq->current_nr_sectors);
  946. rq->flags |= REQ_FAILED;
  947. cdrom_end_request(drive, 0);
  948. } else
  949. cdrom_end_request(drive, 1);
  950. return ide_stopped;
  951. }
  952. /* Check that the drive is expecting to do the same thing we are. */
  953. if (cdrom_read_check_ireason (drive, len, ireason))
  954. return ide_stopped;
  955. /* Assume that the drive will always provide data in multiples
  956. of at least SECTOR_SIZE, as it gets hairy to keep track
  957. of the transfers otherwise. */
  958. if ((len % SECTOR_SIZE) != 0) {
  959. printk (KERN_ERR "%s: cdrom_read_intr: Bad transfer size %d\n",
  960. drive->name, len);
  961. if (CDROM_CONFIG_FLAGS(drive)->limit_nframes)
  962. printk (KERN_ERR " This drive is not supported by this version of the driver\n");
  963. else {
  964. printk (KERN_ERR " Trying to limit transfer sizes\n");
  965. CDROM_CONFIG_FLAGS(drive)->limit_nframes = 1;
  966. }
  967. cdrom_end_request(drive, 0);
  968. return ide_stopped;
  969. }
  970. /* The number of sectors we need to read from the drive. */
  971. sectors_to_transfer = len / SECTOR_SIZE;
  972. /* First, figure out if we need to bit-bucket
  973. any of the leading sectors. */
  974. nskip = min_t(int, rq->current_nr_sectors - bio_cur_sectors(rq->bio), sectors_to_transfer);
  975. while (nskip > 0) {
  976. /* We need to throw away a sector. */
  977. static char dum[SECTOR_SIZE];
  978. HWIF(drive)->atapi_input_bytes(drive, dum, sizeof (dum));
  979. --rq->current_nr_sectors;
  980. --nskip;
  981. --sectors_to_transfer;
  982. }
  983. /* Now loop while we still have data to read from the drive. */
  984. while (sectors_to_transfer > 0) {
  985. int this_transfer;
  986. /* If we've filled the present buffer but there's another
  987. chained buffer after it, move on. */
  988. if (rq->current_nr_sectors == 0 && rq->nr_sectors)
  989. cdrom_end_request(drive, 1);
  990. /* If the buffers are full, cache the rest of the data in our
  991. internal buffer. */
  992. if (rq->current_nr_sectors == 0) {
  993. cdrom_buffer_sectors(drive, rq->sector, sectors_to_transfer);
  994. sectors_to_transfer = 0;
  995. } else {
  996. /* Transfer data to the buffers.
  997. Figure out how many sectors we can transfer
  998. to the current buffer. */
  999. this_transfer = min_t(int, sectors_to_transfer,
  1000. rq->current_nr_sectors);
  1001. /* Read this_transfer sectors
  1002. into the current buffer. */
  1003. while (this_transfer > 0) {
  1004. HWIF(drive)->atapi_input_bytes(drive, rq->buffer, SECTOR_SIZE);
  1005. rq->buffer += SECTOR_SIZE;
  1006. --rq->nr_sectors;
  1007. --rq->current_nr_sectors;
  1008. ++rq->sector;
  1009. --this_transfer;
  1010. --sectors_to_transfer;
  1011. }
  1012. }
  1013. }
  1014. /* Done moving data! Wait for another interrupt. */
  1015. ide_set_handler(drive, &cdrom_read_intr, ATAPI_WAIT_PC, NULL);
  1016. return ide_started;
  1017. }
  1018. /*
  1019. * Try to satisfy some of the current read request from our cached data.
  1020. * Returns nonzero if the request has been completed, zero otherwise.
  1021. */
  1022. static int cdrom_read_from_buffer (ide_drive_t *drive)
  1023. {
  1024. struct cdrom_info *info = drive->driver_data;
  1025. struct request *rq = HWGROUP(drive)->rq;
  1026. unsigned short sectors_per_frame;
  1027. sectors_per_frame = queue_hardsect_size(drive->queue) >> SECTOR_BITS;
  1028. /* Can't do anything if there's no buffer. */
  1029. if (info->buffer == NULL) return 0;
  1030. /* Loop while this request needs data and the next block is present
  1031. in our cache. */
  1032. while (rq->nr_sectors > 0 &&
  1033. rq->sector >= info->sector_buffered &&
  1034. rq->sector < info->sector_buffered + info->nsectors_buffered) {
  1035. if (rq->current_nr_sectors == 0)
  1036. cdrom_end_request(drive, 1);
  1037. memcpy (rq->buffer,
  1038. info->buffer +
  1039. (rq->sector - info->sector_buffered) * SECTOR_SIZE,
  1040. SECTOR_SIZE);
  1041. rq->buffer += SECTOR_SIZE;
  1042. --rq->current_nr_sectors;
  1043. --rq->nr_sectors;
  1044. ++rq->sector;
  1045. }
  1046. /* If we've satisfied the current request,
  1047. terminate it successfully. */
  1048. if (rq->nr_sectors == 0) {
  1049. cdrom_end_request(drive, 1);
  1050. return -1;
  1051. }
  1052. /* Move on to the next buffer if needed. */
  1053. if (rq->current_nr_sectors == 0)
  1054. cdrom_end_request(drive, 1);
  1055. /* If this condition does not hold, then the kluge i use to
  1056. represent the number of sectors to skip at the start of a transfer
  1057. will fail. I think that this will never happen, but let's be
  1058. paranoid and check. */
  1059. if (rq->current_nr_sectors < bio_cur_sectors(rq->bio) &&
  1060. (rq->sector & (sectors_per_frame - 1))) {
  1061. printk(KERN_ERR "%s: cdrom_read_from_buffer: buffer botch (%ld)\n",
  1062. drive->name, (long)rq->sector);
  1063. cdrom_end_request(drive, 0);
  1064. return -1;
  1065. }
  1066. return 0;
  1067. }
  1068. /*
  1069. * Routine to send a read packet command to the drive.
  1070. * This is usually called directly from cdrom_start_read.
  1071. * However, for drq_interrupt devices, it is called from an interrupt
  1072. * when the drive is ready to accept the command.
  1073. */
  1074. static ide_startstop_t cdrom_start_read_continuation (ide_drive_t *drive)
  1075. {
  1076. struct request *rq = HWGROUP(drive)->rq;
  1077. unsigned short sectors_per_frame;
  1078. int nskip;
  1079. sectors_per_frame = queue_hardsect_size(drive->queue) >> SECTOR_BITS;
  1080. /* If the requested sector doesn't start on a cdrom block boundary,
  1081. we must adjust the start of the transfer so that it does,
  1082. and remember to skip the first few sectors.
  1083. If the CURRENT_NR_SECTORS field is larger than the size
  1084. of the buffer, it will mean that we're to skip a number
  1085. of sectors equal to the amount by which CURRENT_NR_SECTORS
  1086. is larger than the buffer size. */
  1087. nskip = rq->sector & (sectors_per_frame - 1);
  1088. if (nskip > 0) {
  1089. /* Sanity check... */
  1090. if (rq->current_nr_sectors != bio_cur_sectors(rq->bio) &&
  1091. (rq->sector & (sectors_per_frame - 1))) {
  1092. printk(KERN_ERR "%s: cdrom_start_read_continuation: buffer botch (%u)\n",
  1093. drive->name, rq->current_nr_sectors);
  1094. cdrom_end_request(drive, 0);
  1095. return ide_stopped;
  1096. }
  1097. rq->current_nr_sectors += nskip;
  1098. }
  1099. /* Set up the command */
  1100. rq->timeout = ATAPI_WAIT_PC;
  1101. /* Send the command to the drive and return. */
  1102. return cdrom_transfer_packet_command(drive, rq, &cdrom_read_intr);
  1103. }
  1104. #define IDECD_SEEK_THRESHOLD (1000) /* 1000 blocks */
  1105. #define IDECD_SEEK_TIMER (5 * WAIT_MIN_SLEEP) /* 100 ms */
  1106. #define IDECD_SEEK_TIMEOUT (2 * WAIT_CMD) /* 20 sec */
  1107. static ide_startstop_t cdrom_seek_intr (ide_drive_t *drive)
  1108. {
  1109. struct cdrom_info *info = drive->driver_data;
  1110. int stat;
  1111. static int retry = 10;
  1112. if (cdrom_decode_status(drive, 0, &stat))
  1113. return ide_stopped;
  1114. CDROM_CONFIG_FLAGS(drive)->seeking = 1;
  1115. if (retry && time_after(jiffies, info->start_seek + IDECD_SEEK_TIMER)) {
  1116. if (--retry == 0) {
  1117. /*
  1118. * this condition is far too common, to bother
  1119. * users about it
  1120. */
  1121. /* printk("%s: disabled DSC seek overlap\n", drive->name);*/
  1122. drive->dsc_overlap = 0;
  1123. }
  1124. }
  1125. return ide_stopped;
  1126. }
  1127. static ide_startstop_t cdrom_start_seek_continuation (ide_drive_t *drive)
  1128. {
  1129. struct request *rq = HWGROUP(drive)->rq;
  1130. sector_t frame = rq->sector;
  1131. sector_div(frame, queue_hardsect_size(drive->queue) >> SECTOR_BITS);
  1132. memset(rq->cmd, 0, sizeof(rq->cmd));
  1133. rq->cmd[0] = GPCMD_SEEK;
  1134. put_unaligned(cpu_to_be32(frame), (unsigned int *) &rq->cmd[2]);
  1135. rq->timeout = ATAPI_WAIT_PC;
  1136. return cdrom_transfer_packet_command(drive, rq, &cdrom_seek_intr);
  1137. }
  1138. static ide_startstop_t cdrom_start_seek (ide_drive_t *drive, unsigned int block)
  1139. {
  1140. struct cdrom_info *info = drive->driver_data;
  1141. info->dma = 0;
  1142. info->cmd = 0;
  1143. info->start_seek = jiffies;
  1144. return cdrom_start_packet_command(drive, 0, cdrom_start_seek_continuation);
  1145. }
  1146. /* Fix up a possibly partially-processed request so that we can
  1147. start it over entirely, or even put it back on the request queue. */
  1148. static void restore_request (struct request *rq)
  1149. {
  1150. if (rq->buffer != bio_data(rq->bio)) {
  1151. sector_t n = (rq->buffer - (char *) bio_data(rq->bio)) / SECTOR_SIZE;
  1152. rq->buffer = bio_data(rq->bio);
  1153. rq->nr_sectors += n;
  1154. rq->sector -= n;
  1155. }
  1156. rq->hard_cur_sectors = rq->current_nr_sectors = bio_cur_sectors(rq->bio);
  1157. rq->hard_nr_sectors = rq->nr_sectors;
  1158. rq->hard_sector = rq->sector;
  1159. rq->q->prep_rq_fn(rq->q, rq);
  1160. }
  1161. /*
  1162. * Start a read request from the CD-ROM.
  1163. */
  1164. static ide_startstop_t cdrom_start_read (ide_drive_t *drive, unsigned int block)
  1165. {
  1166. struct cdrom_info *info = drive->driver_data;
  1167. struct request *rq = HWGROUP(drive)->rq;
  1168. unsigned short sectors_per_frame;
  1169. sectors_per_frame = queue_hardsect_size(drive->queue) >> SECTOR_BITS;
  1170. /* We may be retrying this request after an error. Fix up
  1171. any weirdness which might be present in the request packet. */
  1172. restore_request(rq);
  1173. /* Satisfy whatever we can of this request from our cached sector. */
  1174. if (cdrom_read_from_buffer(drive))
  1175. return ide_stopped;
  1176. blk_attempt_remerge(drive->queue, rq);
  1177. /* Clear the local sector buffer. */
  1178. info->nsectors_buffered = 0;
  1179. /* use dma, if possible. */
  1180. info->dma = drive->using_dma;
  1181. if ((rq->sector & (sectors_per_frame - 1)) ||
  1182. (rq->nr_sectors & (sectors_per_frame - 1)))
  1183. info->dma = 0;
  1184. info->cmd = READ;
  1185. /* Start sending the read request to the drive. */
  1186. return cdrom_start_packet_command(drive, 32768, cdrom_start_read_continuation);
  1187. }
  1188. /****************************************************************************
  1189. * Execute all other packet commands.
  1190. */
  1191. /* Interrupt routine for packet command completion. */
  1192. static ide_startstop_t cdrom_pc_intr (ide_drive_t *drive)
  1193. {
  1194. int ireason, len, thislen;
  1195. struct request *rq = HWGROUP(drive)->rq;
  1196. u8 lowcyl = 0, highcyl = 0;
  1197. int stat;
  1198. /* Check for errors. */
  1199. if (cdrom_decode_status(drive, 0, &stat))
  1200. return ide_stopped;
  1201. /* Read the interrupt reason and the transfer length. */
  1202. ireason = HWIF(drive)->INB(IDE_IREASON_REG);
  1203. lowcyl = HWIF(drive)->INB(IDE_BCOUNTL_REG);
  1204. highcyl = HWIF(drive)->INB(IDE_BCOUNTH_REG);
  1205. len = lowcyl + (256 * highcyl);
  1206. /* If DRQ is clear, the command has completed.
  1207. Complain if we still have data left to transfer. */
  1208. if ((stat & DRQ_STAT) == 0) {
  1209. /* Some of the trailing request sense fields are optional, and
  1210. some drives don't send them. Sigh. */
  1211. if (rq->cmd[0] == GPCMD_REQUEST_SENSE &&
  1212. rq->data_len > 0 &&
  1213. rq->data_len <= 5) {
  1214. while (rq->data_len > 0) {
  1215. *(unsigned char *)rq->data++ = 0;
  1216. --rq->data_len;
  1217. }
  1218. }
  1219. if (rq->data_len == 0)
  1220. cdrom_end_request(drive, 1);
  1221. else {
  1222. /* Comment this out, because this always happens
  1223. right after a reset occurs, and it is annoying to
  1224. always print expected stuff. */
  1225. /*
  1226. printk ("%s: cdrom_pc_intr: data underrun %d\n",
  1227. drive->name, pc->buflen);
  1228. */
  1229. rq->flags |= REQ_FAILED;
  1230. cdrom_end_request(drive, 0);
  1231. }
  1232. return ide_stopped;
  1233. }
  1234. /* Figure out how much data to transfer. */
  1235. thislen = rq->data_len;
  1236. if (thislen > len) thislen = len;
  1237. /* The drive wants to be written to. */
  1238. if ((ireason & 3) == 0) {
  1239. if (!rq->data) {
  1240. blk_dump_rq_flags(rq, "cdrom_pc_intr, write");
  1241. goto confused;
  1242. }
  1243. /* Transfer the data. */
  1244. HWIF(drive)->atapi_output_bytes(drive, rq->data, thislen);
  1245. /* If we haven't moved enough data to satisfy the drive,
  1246. add some padding. */
  1247. while (len > thislen) {
  1248. int dum = 0;
  1249. HWIF(drive)->atapi_output_bytes(drive, &dum, sizeof(dum));
  1250. len -= sizeof(dum);
  1251. }
  1252. /* Keep count of how much data we've moved. */
  1253. rq->data += thislen;
  1254. rq->data_len -= thislen;
  1255. }
  1256. /* Same drill for reading. */
  1257. else if ((ireason & 3) == 2) {
  1258. if (!rq->data) {
  1259. blk_dump_rq_flags(rq, "cdrom_pc_intr, write");
  1260. goto confused;
  1261. }
  1262. /* Transfer the data. */
  1263. HWIF(drive)->atapi_input_bytes(drive, rq->data, thislen);
  1264. /* If we haven't moved enough data to satisfy the drive,
  1265. add some padding. */
  1266. while (len > thislen) {
  1267. int dum = 0;
  1268. HWIF(drive)->atapi_input_bytes(drive, &dum, sizeof(dum));
  1269. len -= sizeof(dum);
  1270. }
  1271. /* Keep count of how much data we've moved. */
  1272. rq->data += thislen;
  1273. rq->data_len -= thislen;
  1274. if (rq->flags & REQ_SENSE)
  1275. rq->sense_len += thislen;
  1276. } else {
  1277. confused:
  1278. printk (KERN_ERR "%s: cdrom_pc_intr: The drive "
  1279. "appears confused (ireason = 0x%02x)\n",
  1280. drive->name, ireason);
  1281. rq->flags |= REQ_FAILED;
  1282. }
  1283. /* Now we wait for another interrupt. */
  1284. ide_set_handler(drive, &cdrom_pc_intr, ATAPI_WAIT_PC, cdrom_timer_ex