1010
1111#include <scsi/scsi.h>
1212
13+ #define DRV_NAME "ide-atapi"
14+ #define PFX DRV_NAME ": "
15+
1316#ifdef DEBUG
1417#define debug_log (fmt , args ...) \
1518 printk(KERN_INFO "ide: " fmt, ## args)
@@ -74,8 +77,6 @@ EXPORT_SYMBOL_GPL(ide_check_atapi_device);
7477void ide_init_pc (struct ide_atapi_pc * pc )
7578{
7679 memset (pc , 0 , sizeof (* pc ));
77- pc -> buf = pc -> pc_buf ;
78- pc -> buf_size = IDE_PC_BUFFER_SIZE ;
7980}
8081EXPORT_SYMBOL_GPL (ide_init_pc );
8182
@@ -84,7 +85,7 @@ EXPORT_SYMBOL_GPL(ide_init_pc);
8485 * and wait for it to be serviced.
8586 */
8687int ide_queue_pc_tail (ide_drive_t * drive , struct gendisk * disk ,
87- struct ide_atapi_pc * pc )
88+ struct ide_atapi_pc * pc , void * buf , unsigned int bufflen )
8889{
8990 struct request * rq ;
9091 int error ;
@@ -93,8 +94,8 @@ int ide_queue_pc_tail(ide_drive_t *drive, struct gendisk *disk,
9394 rq -> cmd_type = REQ_TYPE_SPECIAL ;
9495 rq -> special = (char * )pc ;
9596
96- if (pc -> req_xfer ) {
97- error = blk_rq_map_kern (drive -> queue , rq , pc -> buf , pc -> req_xfer ,
97+ if (buf && bufflen ) {
98+ error = blk_rq_map_kern (drive -> queue , rq , buf , bufflen ,
9899 GFP_NOIO );
99100 if (error )
100101 goto put_req ;
@@ -117,7 +118,7 @@ int ide_do_test_unit_ready(ide_drive_t *drive, struct gendisk *disk)
117118 ide_init_pc (& pc );
118119 pc .c [0 ] = TEST_UNIT_READY ;
119120
120- return ide_queue_pc_tail (drive , disk , & pc );
121+ return ide_queue_pc_tail (drive , disk , & pc , NULL , 0 );
121122}
122123EXPORT_SYMBOL_GPL (ide_do_test_unit_ready );
123124
@@ -132,7 +133,7 @@ int ide_do_start_stop(ide_drive_t *drive, struct gendisk *disk, int start)
132133 if (drive -> media == ide_tape )
133134 pc .flags |= PC_FLAG_WAIT_FOR_DSC ;
134135
135- return ide_queue_pc_tail (drive , disk , & pc );
136+ return ide_queue_pc_tail (drive , disk , & pc , NULL , 0 );
136137}
137138EXPORT_SYMBOL_GPL (ide_do_start_stop );
138139
@@ -147,7 +148,7 @@ int ide_set_media_lock(ide_drive_t *drive, struct gendisk *disk, int on)
147148 pc .c [0 ] = ALLOW_MEDIUM_REMOVAL ;
148149 pc .c [4 ] = on ;
149150
150- return ide_queue_pc_tail (drive , disk , & pc );
151+ return ide_queue_pc_tail (drive , disk , & pc , NULL , 0 );
151152}
152153EXPORT_SYMBOL_GPL (ide_set_media_lock );
153154
@@ -172,8 +173,6 @@ void ide_prep_sense(ide_drive_t *drive, struct request *rq)
172173 unsigned int cmd_len , sense_len ;
173174 int err ;
174175
175- debug_log ("%s: enter\n" , __func__ );
176-
177176 switch (drive -> media ) {
178177 case ide_floppy :
179178 cmd_len = 255 ;
@@ -201,8 +200,8 @@ void ide_prep_sense(ide_drive_t *drive, struct request *rq)
201200 GFP_NOIO );
202201 if (unlikely (err )) {
203202 if (printk_ratelimit ())
204- printk (KERN_WARNING "%s: failed to map sense buffer\n" ,
205- drive -> name );
203+ printk (KERN_WARNING PFX "%s: failed to map sense "
204+ "buffer\n" , drive -> name );
206205 return ;
207206 }
208207
@@ -223,7 +222,7 @@ int ide_queue_sense_rq(ide_drive_t *drive, void *special)
223222{
224223 /* deferred failure from ide_prep_sense() */
225224 if (!drive -> sense_rq_armed ) {
226- printk (KERN_WARNING "%s: failed queue sense request\n" ,
225+ printk (KERN_WARNING PFX "%s: error queuing a sense request\n" ,
227226 drive -> name );
228227 return - ENOMEM ;
229228 }
@@ -255,8 +254,6 @@ void ide_retry_pc(ide_drive_t *drive)
255254 /* init pc from sense_rq */
256255 ide_init_pc (pc );
257256 memcpy (pc -> c , sense_rq -> cmd , 12 );
258- pc -> buf = bio_data (sense_rq -> bio ); /* pointer to mapped address */
259- pc -> req_xfer = blk_rq_bytes (sense_rq );
260257
261258 if (drive -> media == ide_tape )
262259 drive -> atapi_flags |= IDE_AFLAG_IGNORE_DSC ;
@@ -298,7 +295,7 @@ int ide_cd_expiry(ide_drive_t *drive)
298295 break ;
299296 default :
300297 if (!(rq -> cmd_flags & REQ_QUIET ))
301- printk (KERN_INFO "cmd 0x%x timed out\n" ,
298+ printk (KERN_INFO PFX "cmd 0x%x timed out\n" ,
302299 rq -> cmd [0 ]);
303300 wait = 0 ;
304301 break ;
@@ -331,6 +328,55 @@ void ide_read_bcount_and_ireason(ide_drive_t *drive, u16 *bcount, u8 *ireason)
331328}
332329EXPORT_SYMBOL_GPL (ide_read_bcount_and_ireason );
333330
331+ /*
332+ * Check the contents of the interrupt reason register and attempt to recover if
333+ * there are problems.
334+ *
335+ * Returns:
336+ * - 0 if everything's ok
337+ * - 1 if the request has to be terminated.
338+ */
339+ int ide_check_ireason (ide_drive_t * drive , struct request * rq , int len ,
340+ int ireason , int rw )
341+ {
342+ ide_hwif_t * hwif = drive -> hwif ;
343+
344+ debug_log ("ireason: 0x%x, rw: 0x%x\n" , ireason , rw );
345+
346+ if (ireason == (!rw << 1 ))
347+ return 0 ;
348+ else if (ireason == (rw << 1 )) {
349+ printk (KERN_ERR PFX "%s: %s: wrong transfer direction!\n" ,
350+ drive -> name , __func__ );
351+
352+ if (dev_is_idecd (drive ))
353+ ide_pad_transfer (drive , rw , len );
354+ } else if (!rw && ireason == ATAPI_COD ) {
355+ if (dev_is_idecd (drive )) {
356+ /*
357+ * Some drives (ASUS) seem to tell us that status info
358+ * is available. Just get it and ignore.
359+ */
360+ (void )hwif -> tp_ops -> read_status (hwif );
361+ return 0 ;
362+ }
363+ } else {
364+ if (ireason & ATAPI_COD )
365+ printk (KERN_ERR PFX "%s: CoD != 0 in %s\n" , drive -> name ,
366+ __func__ );
367+
368+ /* drive wants a command packet, or invalid ireason... */
369+ printk (KERN_ERR PFX "%s: %s: bad interrupt reason 0x%02x\n" ,
370+ drive -> name , __func__ , ireason );
371+ }
372+
373+ if (dev_is_idecd (drive ) && rq -> cmd_type == REQ_TYPE_ATA_PC )
374+ rq -> cmd_flags |= REQ_FAILED ;
375+
376+ return 1 ;
377+ }
378+ EXPORT_SYMBOL_GPL (ide_check_ireason );
379+
334380/*
335381 * This is the usual interrupt handler which will be called during a packet
336382 * command. We will transfer some of the data (as requested by the drive)
@@ -365,12 +411,12 @@ static ide_startstop_t ide_pc_intr(ide_drive_t *drive)
365411
366412 if (rc || (drive -> media == ide_tape && (stat & ATA_ERR ))) {
367413 if (drive -> media == ide_floppy )
368- printk (KERN_ERR "%s: DMA %s error\n" ,
414+ printk (KERN_ERR PFX "%s: DMA %s error\n" ,
369415 drive -> name , rq_data_dir (pc -> rq )
370416 ? "write" : "read" );
371417 pc -> flags |= PC_FLAG_DMA_ERROR ;
372418 } else
373- pc -> xferred = pc -> req_xfer ;
419+ rq -> resid_len = 0 ;
374420 debug_log ("%s: DMA finished\n" , drive -> name );
375421 }
376422
@@ -379,7 +425,7 @@ static ide_startstop_t ide_pc_intr(ide_drive_t *drive)
379425 int uptodate , error ;
380426
381427 debug_log ("Packet command completed, %d bytes transferred\n" ,
382- pc -> xferred );
428+ blk_rq_bytes ( rq ) );
383429
384430 pc -> flags &= ~PC_FLAG_DMA_IN_PROGRESS ;
385431
@@ -397,8 +443,8 @@ static ide_startstop_t ide_pc_intr(ide_drive_t *drive)
397443 pc -> rq -> errors ++ ;
398444
399445 if (rq -> cmd [0 ] == REQUEST_SENSE ) {
400- printk (KERN_ERR "%s: I/O error in request sense "
401- " command\n" , drive -> name );
446+ printk (KERN_ERR PFX "%s: I/O error in request "
447+ "sense command\n" , drive -> name );
402448 return ide_do_reset (drive );
403449 }
404450
@@ -446,42 +492,31 @@ static ide_startstop_t ide_pc_intr(ide_drive_t *drive)
446492
447493 if (pc -> flags & PC_FLAG_DMA_IN_PROGRESS ) {
448494 pc -> flags &= ~PC_FLAG_DMA_IN_PROGRESS ;
449- printk (KERN_ERR "%s: The device wants to issue more interrupts "
450- "in DMA mode\n" , drive -> name );
495+ printk (KERN_ERR PFX "%s: The device wants to issue more "
496+ "interrupts in DMA mode\n" , drive -> name );
451497 ide_dma_off (drive );
452498 return ide_do_reset (drive );
453499 }
454500
455501 /* Get the number of bytes to transfer on this interrupt. */
456502 ide_read_bcount_and_ireason (drive , & bcount , & ireason );
457503
458- if (ireason & ATAPI_COD ) {
459- printk (KERN_ERR "%s: CoD != 0 in %s\n" , drive -> name , __func__ );
504+ if (ide_check_ireason (drive , rq , bcount , ireason , write ))
460505 return ide_do_reset (drive );
461- }
462-
463- if (((ireason & ATAPI_IO ) == ATAPI_IO ) == write ) {
464- /* Hopefully, we will never get here */
465- printk (KERN_ERR "%s: We wanted to %s, but the device wants us "
466- "to %s!\n" , drive -> name ,
467- (ireason & ATAPI_IO ) ? "Write" : "Read" ,
468- (ireason & ATAPI_IO ) ? "Read" : "Write" );
469- return ide_do_reset (drive );
470- }
471506
472507 done = min_t (unsigned int , bcount , cmd -> nleft );
473508 ide_pio_bytes (drive , cmd , write , done );
474509
475510 /* Update transferred byte count */
476- pc -> xferred + = done ;
511+ rq -> resid_len - = done ;
477512
478513 bcount -= done ;
479514
480515 if (bcount )
481516 ide_pad_transfer (drive , write , bcount );
482517
483- debug_log ("[cmd %x] transferred %d bytes, padded %d bytes\n" ,
484- rq -> cmd [0 ], done , bcount );
518+ debug_log ("[cmd %x] transferred %d bytes, padded %d bytes, resid: %u \n" ,
519+ rq -> cmd [0 ], done , bcount , rq -> resid_len );
485520
486521 /* And set the interrupt handler again */
487522 ide_set_handler (drive , ide_pc_intr , timeout );
@@ -515,13 +550,13 @@ static u8 ide_wait_ireason(ide_drive_t *drive, u8 ireason)
515550
516551 while (retries -- && ((ireason & ATAPI_COD ) == 0 ||
517552 (ireason & ATAPI_IO ))) {
518- printk (KERN_ERR "%s: (IO,CoD != (0,1) while issuing "
553+ printk (KERN_ERR PFX "%s: (IO,CoD != (0,1) while issuing "
519554 "a packet command, retrying\n" , drive -> name );
520555 udelay (100 );
521556 ireason = ide_read_ireason (drive );
522557 if (retries == 0 ) {
523- printk (KERN_ERR "%s: (IO,CoD != (0,1) while issuing "
524- "a packet command, ignoring\n" ,
558+ printk (KERN_ERR PFX "%s: (IO,CoD != (0,1) while issuing"
559+ " a packet command, ignoring\n" ,
525560 drive -> name );
526561 ireason |= ATAPI_COD ;
527562 ireason &= ~ATAPI_IO ;
@@ -552,7 +587,7 @@ static ide_startstop_t ide_transfer_pc(ide_drive_t *drive)
552587 u8 ireason ;
553588
554589 if (ide_wait_stat (& startstop , drive , ATA_DRQ , ATA_BUSY , WAIT_READY )) {
555- printk (KERN_ERR "%s: Strange, packet command initiated yet "
590+ printk (KERN_ERR PFX "%s: Strange, packet command initiated yet "
556591 "DRQ isn't asserted\n" , drive -> name );
557592 return startstop ;
558593 }
@@ -594,8 +629,8 @@ static ide_startstop_t ide_transfer_pc(ide_drive_t *drive)
594629 ireason = ide_wait_ireason (drive , ireason );
595630
596631 if ((ireason & ATAPI_COD ) == 0 || (ireason & ATAPI_IO )) {
597- printk (KERN_ERR "%s: (IO,CoD) != (0,1) while issuing "
598- " a packet command\n" , drive -> name );
632+ printk (KERN_ERR PFX "%s: (IO,CoD) != (0,1) while "
633+ "issuing a packet command\n" , drive -> name );
599634
600635 return ide_do_reset (drive );
601636 }
@@ -633,7 +668,7 @@ ide_startstop_t ide_issue_pc(ide_drive_t *drive, struct ide_cmd *cmd)
633668 ide_hwif_t * hwif = drive -> hwif ;
634669 ide_expiry_t * expiry = NULL ;
635670 struct request * rq = hwif -> rq ;
636- unsigned int timeout ;
671+ unsigned int timeout , bytes ;
637672 u16 bcount ;
638673 u8 valid_tf ;
639674 u8 drq_int = !!(drive -> atapi_flags & IDE_AFLAG_DRQ_INTERRUPT );
@@ -649,13 +684,14 @@ ide_startstop_t ide_issue_pc(ide_drive_t *drive, struct ide_cmd *cmd)
649684 } else {
650685 pc = drive -> pc ;
651686
652- /* We haven't transferred any data yet */
653- pc -> xferred = 0 ;
654-
655687 valid_tf = IDE_VALID_DEVICE ;
656- bcount = ((drive -> media == ide_tape ) ?
657- pc -> req_xfer :
658- min (pc -> req_xfer , 63 * 1024 ));
688+ bytes = blk_rq_bytes (rq );
689+ bcount = ((drive -> media == ide_tape ) ? bytes
690+ : min_t (unsigned int ,
691+ bytes , 63 * 1024 ));
692+
693+ /* We haven't transferred any data yet */
694+ rq -> resid_len = bcount ;
659695
660696 if (pc -> flags & PC_FLAG_DMA_ERROR ) {
661697 pc -> flags &= ~PC_FLAG_DMA_ERROR ;
0 commit comments