blob: da58020a144eebfd47c169eca77cc438e218d8c0 [file] [log] [blame]
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +02001/*
2 * ATAPI support.
3 */
4
5#include <linux/kernel.h>
Borislav Petkov4cad0852009-01-02 16:12:53 +01006#include <linux/cdrom.h>
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +02007#include <linux/delay.h>
Paul Gortmaker38789fd2011-07-17 15:33:58 -04008#include <linux/export.h>
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +02009#include <linux/ide.h>
Geert Uytterhoeven479edf02009-03-31 20:14:57 +020010#include <linux/scatterlist.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/gfp.h>
Geert Uytterhoeven479edf02009-03-31 20:14:57 +020012
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +020013#include <scsi/scsi.h>
14
Borislav Petkov103f7032009-04-26 10:39:07 +020015#define DRV_NAME "ide-atapi"
16#define PFX DRV_NAME ": "
17
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +020018#ifdef DEBUG
19#define debug_log(fmt, args...) \
20 printk(KERN_INFO "ide: " fmt, ## args)
21#else
22#define debug_log(fmt, args...) do {} while (0)
23#endif
24
Borislav Petkov8c662852009-01-02 16:12:54 +010025#define ATAPI_MIN_CDB_BYTES 12
26
Borislav Petkov991cb262009-01-02 16:12:52 +010027static inline int dev_is_idecd(ide_drive_t *drive)
28{
Borislav Petkov53174642009-01-02 16:12:54 +010029 return drive->media == ide_cdrom || drive->media == ide_optical;
Borislav Petkov991cb262009-01-02 16:12:52 +010030}
31
Bartlomiej Zolnierkiewicz51509ee2008-10-10 22:39:34 +020032/*
33 * Check whether we can support a device,
34 * based on the ATAPI IDENTIFY command results.
35 */
36int ide_check_atapi_device(ide_drive_t *drive, const char *s)
37{
38 u16 *id = drive->id;
39 u8 gcw[2], protocol, device_type, removable, drq_type, packet_size;
40
41 *((u16 *)&gcw) = id[ATA_ID_CONFIG];
42
43 protocol = (gcw[1] & 0xC0) >> 6;
44 device_type = gcw[1] & 0x1F;
45 removable = (gcw[0] & 0x80) >> 7;
46 drq_type = (gcw[0] & 0x60) >> 5;
47 packet_size = gcw[0] & 0x03;
48
49#ifdef CONFIG_PPC
50 /* kludge for Apple PowerBook internal zip */
51 if (drive->media == ide_floppy && device_type == 5 &&
52 !strstr((char *)&id[ATA_ID_PROD], "CD-ROM") &&
53 strstr((char *)&id[ATA_ID_PROD], "ZIP"))
54 device_type = 0;
55#endif
56
57 if (protocol != 2)
58 printk(KERN_ERR "%s: %s: protocol (0x%02x) is not ATAPI\n",
59 s, drive->name, protocol);
60 else if ((drive->media == ide_floppy && device_type != 0) ||
61 (drive->media == ide_tape && device_type != 1))
62 printk(KERN_ERR "%s: %s: invalid device type (0x%02x)\n",
63 s, drive->name, device_type);
64 else if (removable == 0)
65 printk(KERN_ERR "%s: %s: the removable flag is not set\n",
66 s, drive->name);
67 else if (drive->media == ide_floppy && drq_type == 3)
68 printk(KERN_ERR "%s: %s: sorry, DRQ type (0x%02x) not "
69 "supported\n", s, drive->name, drq_type);
70 else if (packet_size != 0)
71 printk(KERN_ERR "%s: %s: packet size (0x%02x) is not 12 "
72 "bytes\n", s, drive->name, packet_size);
73 else
74 return 1;
75 return 0;
76}
77EXPORT_SYMBOL_GPL(ide_check_atapi_device);
78
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +020079void ide_init_pc(struct ide_atapi_pc *pc)
80{
81 memset(pc, 0, sizeof(*pc));
Bartlomiej Zolnierkiewicz7bf74202008-10-10 22:39:37 +020082}
83EXPORT_SYMBOL_GPL(ide_init_pc);
84
Bartlomiej Zolnierkiewicz7645c152008-10-10 22:39:37 +020085/*
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +020086 * Add a special packet command request to the tail of the request queue,
87 * and wait for it to be serviced.
88 */
89int ide_queue_pc_tail(ide_drive_t *drive, struct gendisk *disk,
Borislav Petkovb13345f2009-05-02 10:26:12 +020090 struct ide_atapi_pc *pc, void *buf, unsigned int bufflen)
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +020091{
92 struct request *rq;
93 int error;
94
Christoph Hellwigff005a02018-05-09 09:54:05 +020095 rq = blk_get_request(drive->queue, REQ_OP_DRV_IN, 0);
Christoph Hellwig2f5a8e802017-01-31 16:57:30 +010096 ide_req(rq)->type = ATA_PRIV_MISC;
Christoph Hellwig22ce0a72018-11-10 09:30:49 +010097 ide_req(rq)->special = pc;
Borislav Petkov3eb76c12009-03-13 21:16:12 +010098
Borislav Petkovb13345f2009-05-02 10:26:12 +020099 if (buf && bufflen) {
100 error = blk_rq_map_kern(drive->queue, rq, buf, bufflen,
Tejun Heo5c4be572009-04-19 07:00:42 +0900101 GFP_NOIO);
102 if (error)
103 goto put_req;
Borislav Petkov3eb76c12009-03-13 21:16:12 +0100104 }
105
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100106 memcpy(scsi_req(rq)->cmd, pc->c, 12);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200107 if (drive->media == ide_tape)
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100108 scsi_req(rq)->cmd[13] = REQ_IDETAPE_PC1;
Christoph Hellwigb7819b92017-04-20 16:02:55 +0200109 blk_execute_rq(drive->queue, disk, rq, 0);
Christoph Hellwig17d53632017-04-20 16:03:01 +0200110 error = scsi_req(rq)->result ? -EIO : 0;
Tejun Heo5c4be572009-04-19 07:00:42 +0900111put_req:
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200112 blk_put_request(rq);
Bartlomiej Zolnierkiewicz2ac07d92008-10-10 22:39:38 +0200113 return error;
114}
115EXPORT_SYMBOL_GPL(ide_queue_pc_tail);
116
Bartlomiej Zolnierkiewiczde699ad2008-10-10 22:39:39 +0200117int ide_do_test_unit_ready(ide_drive_t *drive, struct gendisk *disk)
118{
119 struct ide_atapi_pc pc;
120
121 ide_init_pc(&pc);
122 pc.c[0] = TEST_UNIT_READY;
123
Borislav Petkovb13345f2009-05-02 10:26:12 +0200124 return ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
Bartlomiej Zolnierkiewiczde699ad2008-10-10 22:39:39 +0200125}
126EXPORT_SYMBOL_GPL(ide_do_test_unit_ready);
127
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +0200128int ide_do_start_stop(ide_drive_t *drive, struct gendisk *disk, int start)
129{
130 struct ide_atapi_pc pc;
131
132 ide_init_pc(&pc);
133 pc.c[0] = START_STOP;
134 pc.c[4] = start;
135
136 if (drive->media == ide_tape)
137 pc.flags |= PC_FLAG_WAIT_FOR_DSC;
138
Borislav Petkovb13345f2009-05-02 10:26:12 +0200139 return ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
Bartlomiej Zolnierkiewicz0c8a6c72008-10-10 22:39:39 +0200140}
141EXPORT_SYMBOL_GPL(ide_do_start_stop);
142
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +0200143int ide_set_media_lock(ide_drive_t *drive, struct gendisk *disk, int on)
144{
145 struct ide_atapi_pc pc;
146
Bartlomiej Zolnierkiewicz42619d32008-10-17 18:09:11 +0200147 if ((drive->dev_flags & IDE_DFLAG_DOORLOCKING) == 0)
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +0200148 return 0;
149
150 ide_init_pc(&pc);
151 pc.c[0] = ALLOW_MEDIUM_REMOVAL;
152 pc.c[4] = on;
153
Borislav Petkovb13345f2009-05-02 10:26:12 +0200154 return ide_queue_pc_tail(drive, disk, &pc, NULL, 0);
Bartlomiej Zolnierkiewicz05780422008-10-10 22:39:38 +0200155}
156EXPORT_SYMBOL_GPL(ide_set_media_lock);
157
Bartlomiej Zolnierkiewicz6b0da282008-10-13 21:39:32 +0200158void ide_create_request_sense_cmd(ide_drive_t *drive, struct ide_atapi_pc *pc)
159{
160 ide_init_pc(pc);
161 pc->c[0] = REQUEST_SENSE;
162 if (drive->media == ide_floppy) {
163 pc->c[4] = 255;
164 pc->req_xfer = 18;
165 } else {
166 pc->c[4] = 20;
167 pc->req_xfer = 20;
168 }
169}
170EXPORT_SYMBOL_GPL(ide_create_request_sense_cmd);
171
Borislav Petkova1df5162009-04-19 07:00:42 +0900172void ide_prep_sense(ide_drive_t *drive, struct request *rq)
173{
174 struct request_sense *sense = &drive->sense_data;
Jens Axboe60033522018-10-26 09:53:52 -0600175 struct request *sense_rq;
176 struct scsi_request *req;
Borislav Petkova1df5162009-04-19 07:00:42 +0900177 unsigned int cmd_len, sense_len;
Tejun Heo5c4be572009-04-19 07:00:42 +0900178 int err;
Borislav Petkova1df5162009-04-19 07:00:42 +0900179
Borislav Petkova1df5162009-04-19 07:00:42 +0900180 switch (drive->media) {
181 case ide_floppy:
182 cmd_len = 255;
183 sense_len = 18;
184 break;
185 case ide_tape:
186 cmd_len = 20;
187 sense_len = 20;
188 break;
189 default:
190 cmd_len = 18;
191 sense_len = 18;
192 }
193
194 BUG_ON(sense_len > sizeof(*sense));
195
Christoph Hellwig2f5a8e802017-01-31 16:57:30 +0100196 if (ata_sense_request(rq) || drive->sense_rq_armed)
Borislav Petkova1df5162009-04-19 07:00:42 +0900197 return;
198
Jens Axboe60033522018-10-26 09:53:52 -0600199 sense_rq = drive->sense_rq;
200 if (!sense_rq) {
201 sense_rq = blk_mq_alloc_request(drive->queue, REQ_OP_DRV_IN,
202 BLK_MQ_REQ_RESERVED | BLK_MQ_REQ_NOWAIT);
203 drive->sense_rq = sense_rq;
204 }
205 req = scsi_req(sense_rq);
206
Borislav Petkova1df5162009-04-19 07:00:42 +0900207 memset(sense, 0, sizeof(*sense));
208
Bart Van Asschec8d9cf22017-06-20 11:15:42 -0700209 scsi_req_init(req);
Borislav Petkova1df5162009-04-19 07:00:42 +0900210
Tejun Heo5c4be572009-04-19 07:00:42 +0900211 err = blk_rq_map_kern(drive->queue, sense_rq, sense, sense_len,
212 GFP_NOIO);
213 if (unlikely(err)) {
214 if (printk_ratelimit())
Borislav Petkov103f7032009-04-26 10:39:07 +0200215 printk(KERN_WARNING PFX "%s: failed to map sense "
216 "buffer\n", drive->name);
Jens Axboe60033522018-10-26 09:53:52 -0600217 blk_mq_free_request(sense_rq);
218 drive->sense_rq = NULL;
Tejun Heo5c4be572009-04-19 07:00:42 +0900219 return;
220 }
221
222 sense_rq->rq_disk = rq->rq_disk;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100223 sense_rq->cmd_flags = REQ_OP_DRV_IN;
Christoph Hellwig2f5a8e802017-01-31 16:57:30 +0100224 ide_req(sense_rq)->type = ATA_PRIV_SENSE;
Christoph Hellwige8064022016-10-20 15:12:13 +0200225 sense_rq->rq_flags |= RQF_PREEMPT;
Borislav Petkova1df5162009-04-19 07:00:42 +0900226
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100227 req->cmd[0] = GPCMD_REQUEST_SENSE;
228 req->cmd[4] = cmd_len;
Borislav Petkova1df5162009-04-19 07:00:42 +0900229 if (drive->media == ide_tape)
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100230 req->cmd[13] = REQ_IDETAPE_PC1;
Borislav Petkova1df5162009-04-19 07:00:42 +0900231
232 drive->sense_rq_armed = true;
233}
234EXPORT_SYMBOL_GPL(ide_prep_sense);
235
Tejun Heo5c4be572009-04-19 07:00:42 +0900236int ide_queue_sense_rq(ide_drive_t *drive, void *special)
Borislav Petkova1df5162009-04-19 07:00:42 +0900237{
Jens Axboe60033522018-10-26 09:53:52 -0600238 struct request *sense_rq = drive->sense_rq;
239
Tejun Heo5c4be572009-04-19 07:00:42 +0900240 /* deferred failure from ide_prep_sense() */
241 if (!drive->sense_rq_armed) {
Borislav Petkov103f7032009-04-26 10:39:07 +0200242 printk(KERN_WARNING PFX "%s: error queuing a sense request\n",
Tejun Heo5c4be572009-04-19 07:00:42 +0900243 drive->name);
244 return -ENOMEM;
245 }
Borislav Petkova1df5162009-04-19 07:00:42 +0900246
Christoph Hellwig22ce0a72018-11-10 09:30:49 +0100247 ide_req(sense_rq)->special = special;
Borislav Petkova1df5162009-04-19 07:00:42 +0900248 drive->sense_rq_armed = false;
249
250 drive->hwif->rq = NULL;
251
Jens Axboe60033522018-10-26 09:53:52 -0600252 ide_insert_request_head(drive, sense_rq);
Tejun Heo5c4be572009-04-19 07:00:42 +0900253 return 0;
Borislav Petkova1df5162009-04-19 07:00:42 +0900254}
255EXPORT_SYMBOL_GPL(ide_queue_sense_rq);
256
Bartlomiej Zolnierkiewicz6b0da282008-10-13 21:39:32 +0200257/*
258 * Called when an error was detected during the last packet command.
Borislav Petkov6b544fc2009-04-19 07:00:42 +0900259 * We queue a request sense packet command at the head of the request
260 * queue.
Bartlomiej Zolnierkiewicz6b0da282008-10-13 21:39:32 +0200261 */
Borislav Petkov6b544fc2009-04-19 07:00:42 +0900262void ide_retry_pc(ide_drive_t *drive)
Bartlomiej Zolnierkiewicz6b0da282008-10-13 21:39:32 +0200263{
Tejun Heo8f6205c2009-05-08 11:53:59 +0900264 struct request *failed_rq = drive->hwif->rq;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100265 struct request *sense_rq = drive->sense_rq;
Bartlomiej Zolnierkiewicz6b0da282008-10-13 21:39:32 +0200266 struct ide_atapi_pc *pc = &drive->request_sense_pc;
267
268 (void)ide_read_error(drive);
Borislav Petkov6b544fc2009-04-19 07:00:42 +0900269
270 /* init pc from sense_rq */
271 ide_init_pc(pc);
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100272 memcpy(pc->c, scsi_req(sense_rq)->cmd, 12);
Borislav Petkov6b544fc2009-04-19 07:00:42 +0900273
Bartlomiej Zolnierkiewicz6b0da282008-10-13 21:39:32 +0200274 if (drive->media == ide_tape)
Borislav Petkov626542c2009-06-07 15:37:05 +0200275 drive->atapi_flags |= IDE_AFLAG_IGNORE_DSC;
Borislav Petkov6b544fc2009-04-19 07:00:42 +0900276
Tejun Heo8f6205c2009-05-08 11:53:59 +0900277 /*
278 * Push back the failed request and put request sense on top
279 * of it. The failed command will be retried after sense data
280 * is acquired.
281 */
Tejun Heo8f6205c2009-05-08 11:53:59 +0900282 drive->hwif->rq = NULL;
Herbert Xu1af18502010-03-31 20:13:39 +0000283 ide_requeue_and_plug(drive, failed_rq);
Jens Axboe60033522018-10-26 09:53:52 -0600284 if (ide_queue_sense_rq(drive, pc))
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200285 ide_complete_rq(drive, BLK_STS_IOERR, blk_rq_bytes(failed_rq));
Bartlomiej Zolnierkiewicz6b0da282008-10-13 21:39:32 +0200286}
287EXPORT_SYMBOL_GPL(ide_retry_pc);
288
Borislav Petkov4cad0852009-01-02 16:12:53 +0100289int ide_cd_expiry(ide_drive_t *drive)
290{
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100291 struct request *rq = drive->hwif->rq;
Borislav Petkov4cad0852009-01-02 16:12:53 +0100292 unsigned long wait = 0;
293
Hongxu Jia8dc7a312017-11-10 15:59:17 +0800294 debug_log("%s: scsi_req(rq)->cmd[0]: 0x%x\n", __func__, scsi_req(rq)->cmd[0]);
Borislav Petkov4cad0852009-01-02 16:12:53 +0100295
296 /*
297 * Some commands are *slow* and normally take a long time to complete.
298 * Usually we can use the ATAPI "disconnect" to bypass this, but not all
299 * commands/drives support that. Let ide_timer_expiry keep polling us
300 * for these.
301 */
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100302 switch (scsi_req(rq)->cmd[0]) {
Borislav Petkov4cad0852009-01-02 16:12:53 +0100303 case GPCMD_BLANK:
304 case GPCMD_FORMAT_UNIT:
305 case GPCMD_RESERVE_RZONE_TRACK:
306 case GPCMD_CLOSE_TRACK:
307 case GPCMD_FLUSH_CACHE:
308 wait = ATAPI_WAIT_PC;
309 break;
310 default:
Christoph Hellwige8064022016-10-20 15:12:13 +0200311 if (!(rq->rq_flags & RQF_QUIET))
Borislav Petkov103f7032009-04-26 10:39:07 +0200312 printk(KERN_INFO PFX "cmd 0x%x timed out\n",
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100313 scsi_req(rq)->cmd[0]);
Borislav Petkov4cad0852009-01-02 16:12:53 +0100314 wait = 0;
315 break;
316 }
317 return wait;
318}
319EXPORT_SYMBOL_GPL(ide_cd_expiry);
320
Borislav Petkov392de1d2009-01-02 16:12:52 +0100321int ide_cd_get_xferlen(struct request *rq)
322{
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100323 switch (req_op(rq)) {
324 default:
Borislav Petkov392de1d2009-01-02 16:12:52 +0100325 return 32768;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100326 case REQ_OP_SCSI_IN:
327 case REQ_OP_SCSI_OUT:
Tejun Heo34b7d2c2009-05-07 22:24:43 +0900328 return blk_rq_bytes(rq);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100329 case REQ_OP_DRV_IN:
330 case REQ_OP_DRV_OUT:
Christoph Hellwig2f5a8e802017-01-31 16:57:30 +0100331 switch (ide_req(rq)->type) {
332 case ATA_PRIV_PC:
333 case ATA_PRIV_SENSE:
334 return blk_rq_bytes(rq);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100335 default:
336 return 0;
Christoph Hellwig2f5a8e802017-01-31 16:57:30 +0100337 }
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200338 }
Borislav Petkov392de1d2009-01-02 16:12:52 +0100339}
340EXPORT_SYMBOL_GPL(ide_cd_get_xferlen);
341
Bartlomiej Zolnierkiewicz0d6a9752009-03-24 23:22:46 +0100342void ide_read_bcount_and_ireason(ide_drive_t *drive, u16 *bcount, u8 *ireason)
343{
Sergei Shtylyov3153c262009-04-08 14:13:03 +0200344 struct ide_taskfile tf;
Bartlomiej Zolnierkiewicz0d6a9752009-03-24 23:22:46 +0100345
Sergei Shtylyov3153c262009-04-08 14:13:03 +0200346 drive->hwif->tp_ops->tf_read(drive, &tf, IDE_VALID_NSECT |
347 IDE_VALID_LBAM | IDE_VALID_LBAH);
Bartlomiej Zolnierkiewicz0d6a9752009-03-24 23:22:46 +0100348
Sergei Shtylyov3153c262009-04-08 14:13:03 +0200349 *bcount = (tf.lbah << 8) | tf.lbam;
350 *ireason = tf.nsect & 3;
Bartlomiej Zolnierkiewicz0d6a9752009-03-24 23:22:46 +0100351}
352EXPORT_SYMBOL_GPL(ide_read_bcount_and_ireason);
353
Bartlomiej Zolnierkiewiczaa5d2de72008-10-13 21:39:32 +0200354/*
Borislav Petkov103f7032009-04-26 10:39:07 +0200355 * Check the contents of the interrupt reason register and attempt to recover if
356 * there are problems.
357 *
358 * Returns:
359 * - 0 if everything's ok
360 * - 1 if the request has to be terminated.
361 */
362int ide_check_ireason(ide_drive_t *drive, struct request *rq, int len,
363 int ireason, int rw)
364{
365 ide_hwif_t *hwif = drive->hwif;
366
367 debug_log("ireason: 0x%x, rw: 0x%x\n", ireason, rw);
368
369 if (ireason == (!rw << 1))
370 return 0;
371 else if (ireason == (rw << 1)) {
372 printk(KERN_ERR PFX "%s: %s: wrong transfer direction!\n",
373 drive->name, __func__);
374
375 if (dev_is_idecd(drive))
376 ide_pad_transfer(drive, rw, len);
377 } else if (!rw && ireason == ATAPI_COD) {
378 if (dev_is_idecd(drive)) {
379 /*
380 * Some drives (ASUS) seem to tell us that status info
381 * is available. Just get it and ignore.
382 */
383 (void)hwif->tp_ops->read_status(hwif);
384 return 0;
385 }
386 } else {
387 if (ireason & ATAPI_COD)
388 printk(KERN_ERR PFX "%s: CoD != 0 in %s\n", drive->name,
389 __func__);
390
391 /* drive wants a command packet, or invalid ireason... */
392 printk(KERN_ERR PFX "%s: %s: bad interrupt reason 0x%02x\n",
393 drive->name, __func__, ireason);
394 }
395
Christoph Hellwig2f5a8e802017-01-31 16:57:30 +0100396 if (dev_is_idecd(drive) && ata_pc_request(rq))
Christoph Hellwige8064022016-10-20 15:12:13 +0200397 rq->rq_flags |= RQF_FAILED;
Borislav Petkov103f7032009-04-26 10:39:07 +0200398
399 return 1;
400}
401EXPORT_SYMBOL_GPL(ide_check_ireason);
402
403/*
Bartlomiej Zolnierkiewiczaa5d2de72008-10-13 21:39:32 +0200404 * This is the usual interrupt handler which will be called during a packet
405 * command. We will transfer some of the data (as requested by the drive)
406 * and will re-point interrupt handler to us.
407 */
408static ide_startstop_t ide_pc_intr(ide_drive_t *drive)
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200409{
Bartlomiej Zolnierkiewicz2b9efba2008-10-13 21:39:31 +0200410 struct ide_atapi_pc *pc = drive->pc;
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200411 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewiczf094d4d82009-03-31 20:15:24 +0200412 struct ide_cmd *cmd = &hwif->cmd;
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100413 struct request *rq = hwif->rq;
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200414 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
Bartlomiej Zolnierkiewiczd93bc452009-03-31 20:15:26 +0200415 unsigned int timeout, done;
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200416 u16 bcount;
Borislav Petkov5d655a02009-01-02 16:12:54 +0100417 u8 stat, ireason, dsc = 0;
Bartlomiej Zolnierkiewiczd93bc452009-03-31 20:15:26 +0200418 u8 write = !!(pc->flags & PC_FLAG_WRITING);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200419
420 debug_log("Enter %s - interrupt handler\n", __func__);
421
Borislav Petkov5d655a02009-01-02 16:12:54 +0100422 timeout = (drive->media == ide_floppy) ? WAIT_FLOPPY_CMD
423 : WAIT_TAPE_CMD;
Bartlomiej Zolnierkiewicz844b9462008-10-13 21:39:31 +0200424
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200425 /* Clear the interrupt */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200426 stat = tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200427
428 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
Bartlomiej Zolnierkiewicz88b41322009-03-31 20:15:22 +0200429 int rc;
Bartlomiej Zolnierkiewicz44530112009-03-31 20:15:20 +0200430
Bartlomiej Zolnierkiewicz88b41322009-03-31 20:15:22 +0200431 drive->waiting_for_dma = 0;
432 rc = hwif->dma_ops->dma_end(drive);
Bartlomiej Zolnierkiewiczf094d4d82009-03-31 20:15:24 +0200433 ide_dma_unmap_sg(drive, cmd);
Bartlomiej Zolnierkiewicz44530112009-03-31 20:15:20 +0200434
435 if (rc || (drive->media == ide_tape && (stat & ATA_ERR))) {
Borislav Petkov5d655a02009-01-02 16:12:54 +0100436 if (drive->media == ide_floppy)
Borislav Petkov103f7032009-04-26 10:39:07 +0200437 printk(KERN_ERR PFX "%s: DMA %s error\n",
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200438 drive->name, rq_data_dir(pc->rq)
439 ? "write" : "read");
440 pc->flags |= PC_FLAG_DMA_ERROR;
Tejun Heo6d700382009-04-19 08:46:03 +0900441 } else
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100442 scsi_req(rq)->resid_len = 0;
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200443 debug_log("%s: DMA finished\n", drive->name);
444 }
445
446 /* No more interrupts */
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200447 if ((stat & ATA_DRQ) == 0) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200448 int uptodate;
449 blk_status_t error;
Bartlomiej Zolnierkiewicz03a2faa2009-03-27 12:46:36 +0100450
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200451 debug_log("Packet command completed, %d bytes transferred\n",
Borislav Petkov077e6db2009-05-01 21:21:02 +0200452 blk_rq_bytes(rq));
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200453
454 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
455
456 local_irq_enable_in_hardirq();
457
Borislav Petkov5d655a02009-01-02 16:12:54 +0100458 if (drive->media == ide_tape &&
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100459 (stat & ATA_ERR) && scsi_req(rq)->cmd[0] == REQUEST_SENSE)
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200460 stat &= ~ATA_ERR;
Borislav Petkov8fccf892008-07-23 19:56:01 +0200461
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200462 if ((stat & ATA_ERR) || (pc->flags & PC_FLAG_DMA_ERROR)) {
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200463 /* Error detected */
464 debug_log("%s: I/O error\n", drive->name);
465
Borislav Petkov5d655a02009-01-02 16:12:54 +0100466 if (drive->media != ide_tape)
Christoph Hellwig17d53632017-04-20 16:03:01 +0200467 scsi_req(pc->rq)->result++;
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200468
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100469 if (scsi_req(rq)->cmd[0] == REQUEST_SENSE) {
Borislav Petkov103f7032009-04-26 10:39:07 +0200470 printk(KERN_ERR PFX "%s: I/O error in request "
471 "sense command\n", drive->name);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200472 return ide_do_reset(drive);
473 }
474
Hongxu Jia8dc7a312017-11-10 15:59:17 +0800475 debug_log("[cmd %x]: check condition\n", scsi_req(rq)->cmd[0]);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200476
477 /* Retry operation */
Borislav Petkov6b544fc2009-04-19 07:00:42 +0900478 ide_retry_pc(drive);
Borislav Petkov8fccf892008-07-23 19:56:01 +0200479
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200480 /* queued, but not started */
481 return ide_stopped;
482 }
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200483 pc->error = 0;
Bartlomiej Zolnierkiewiczb14c7212008-10-13 21:39:30 +0200484
485 if ((pc->flags & PC_FLAG_WAIT_FOR_DSC) && (stat & ATA_DSC) == 0)
486 dsc = 1;
Borislav Petkov8fccf892008-07-23 19:56:01 +0200487
Tejun Heob3071d12009-04-19 08:46:02 +0900488 /*
489 * ->pc_callback() might change rq->data_len for
490 * residual count, cache total length.
491 */
Tejun Heo21d9c5d2009-04-19 08:46:02 +0900492 done = blk_rq_bytes(rq);
Tejun Heob3071d12009-04-19 08:46:02 +0900493
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200494 /* Command finished - Call the callback function */
Bartlomiej Zolnierkiewicz03a2faa2009-03-27 12:46:36 +0100495 uptodate = drive->pc_callback(drive, dsc);
496
497 if (uptodate == 0)
498 drive->failed_pc = NULL;
499
Christoph Hellwig2f5a8e802017-01-31 16:57:30 +0100500 if (ata_misc_request(rq)) {
Christoph Hellwig17d53632017-04-20 16:03:01 +0200501 scsi_req(rq)->result = 0;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200502 error = BLK_STS_OK;
Bartlomiej Zolnierkiewicz89f78b32009-03-27 12:46:43 +0100503 } else {
Bartlomiej Zolnierkiewicz349d12a2009-03-31 20:15:26 +0200504
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100505 if (blk_rq_is_passthrough(rq) && uptodate <= 0) {
Christoph Hellwig17d53632017-04-20 16:03:01 +0200506 if (scsi_req(rq)->result == 0)
507 scsi_req(rq)->result = -EIO;
Bartlomiej Zolnierkiewicz89f78b32009-03-27 12:46:43 +0100508 }
Bartlomiej Zolnierkiewicz349d12a2009-03-31 20:15:26 +0200509
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200510 error = uptodate ? BLK_STS_OK : BLK_STS_IOERR;
Bartlomiej Zolnierkiewicz89f78b32009-03-27 12:46:43 +0100511 }
Borislav Petkov8fccf892008-07-23 19:56:01 +0200512
Tejun Heoc3a4d782009-05-07 22:24:37 +0900513 ide_complete_rq(drive, error, blk_rq_bytes(rq));
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200514 return ide_stopped;
515 }
516
517 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
518 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
Borislav Petkov103f7032009-04-26 10:39:07 +0200519 printk(KERN_ERR PFX "%s: The device wants to issue more "
520 "interrupts in DMA mode\n", drive->name);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200521 ide_dma_off(drive);
522 return ide_do_reset(drive);
523 }
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200524
Bartlomiej Zolnierkiewicz18236492008-07-23 19:55:54 +0200525 /* Get the number of bytes to transfer on this interrupt. */
526 ide_read_bcount_and_ireason(drive, &bcount, &ireason);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200527
Borislav Petkov103f7032009-04-26 10:39:07 +0200528 if (ide_check_ireason(drive, rq, bcount, ireason, write))
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200529 return ide_do_reset(drive);
Borislav Petkov8fccf892008-07-23 19:56:01 +0200530
Tejun Heo6d700382009-04-19 08:46:03 +0900531 done = min_t(unsigned int, bcount, cmd->nleft);
532 ide_pio_bytes(drive, cmd, write, done);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200533
Tejun Heo6d700382009-04-19 08:46:03 +0900534 /* Update transferred byte count */
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100535 scsi_req(rq)->resid_len -= done;
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200536
Bartlomiej Zolnierkiewiczd93bc452009-03-31 20:15:26 +0200537 bcount -= done;
538
539 if (bcount)
540 ide_pad_transfer(drive, write, bcount);
541
Borislav Petkov077e6db2009-05-01 21:21:02 +0200542 debug_log("[cmd %x] transferred %d bytes, padded %d bytes, resid: %u\n",
Hongxu Jia8dc7a312017-11-10 15:59:17 +0800543 scsi_req(rq)->cmd[0], done, bcount, scsi_req(rq)->resid_len);
Bartlomiej Zolnierkiewiczd93bc452009-03-31 20:15:26 +0200544
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200545 /* And set the interrupt handler again */
Bartlomiej Zolnierkiewicz60c0cd02009-03-27 12:46:46 +0100546 ide_set_handler(drive, ide_pc_intr, timeout);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200547 return ide_started;
548}
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200549
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200550static void ide_init_packet_cmd(struct ide_cmd *cmd, u8 valid_tf,
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100551 u16 bcount, u8 dma)
Bartlomiej Zolnierkiewicz7a254df2009-03-24 23:22:39 +0100552{
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200553 cmd->protocol = dma ? ATAPI_PROT_DMA : ATAPI_PROT_PIO;
554 cmd->valid.out.tf = IDE_VALID_LBAH | IDE_VALID_LBAM |
555 IDE_VALID_FEATURE | valid_tf;
Bartlomiej Zolnierkiewicz35b5d0b2009-03-27 12:46:47 +0100556 cmd->tf.command = ATA_CMD_PACKET;
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100557 cmd->tf.feature = dma; /* Use PIO/DMA */
558 cmd->tf.lbam = bcount & 0xff;
559 cmd->tf.lbah = (bcount >> 8) & 0xff;
Bartlomiej Zolnierkiewicz7a254df2009-03-24 23:22:39 +0100560}
561
Bartlomiej Zolnierkiewicz88a72102008-07-23 19:55:54 +0200562static u8 ide_read_ireason(ide_drive_t *drive)
563{
Sergei Shtylyov3153c262009-04-08 14:13:03 +0200564 struct ide_taskfile tf;
Bartlomiej Zolnierkiewicz88a72102008-07-23 19:55:54 +0200565
Sergei Shtylyov3153c262009-04-08 14:13:03 +0200566 drive->hwif->tp_ops->tf_read(drive, &tf, IDE_VALID_NSECT);
Bartlomiej Zolnierkiewicz88a72102008-07-23 19:55:54 +0200567
Sergei Shtylyov3153c262009-04-08 14:13:03 +0200568 return tf.nsect & 3;
Bartlomiej Zolnierkiewicz88a72102008-07-23 19:55:54 +0200569}
570
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200571static u8 ide_wait_ireason(ide_drive_t *drive, u8 ireason)
572{
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200573 int retries = 100;
574
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200575 while (retries-- && ((ireason & ATAPI_COD) == 0 ||
576 (ireason & ATAPI_IO))) {
Borislav Petkov103f7032009-04-26 10:39:07 +0200577 printk(KERN_ERR PFX "%s: (IO,CoD != (0,1) while issuing "
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200578 "a packet command, retrying\n", drive->name);
579 udelay(100);
Bartlomiej Zolnierkiewicz88a72102008-07-23 19:55:54 +0200580 ireason = ide_read_ireason(drive);
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200581 if (retries == 0) {
Borislav Petkov103f7032009-04-26 10:39:07 +0200582 printk(KERN_ERR PFX "%s: (IO,CoD != (0,1) while issuing"
583 " a packet command, ignoring\n",
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200584 drive->name);
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200585 ireason |= ATAPI_COD;
586 ireason &= ~ATAPI_IO;
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200587 }
588 }
589
590 return ireason;
591}
592
Bartlomiej Zolnierkiewiczbaf08f02008-10-13 21:39:32 +0200593static int ide_delayed_transfer_pc(ide_drive_t *drive)
594{
595 /* Send the actual packet */
596 drive->hwif->tp_ops->output_data(drive, NULL, drive->pc->c, 12);
597
598 /* Timeout for the packet command */
599 return WAIT_FLOPPY_CMD;
600}
601
602static ide_startstop_t ide_transfer_pc(ide_drive_t *drive)
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200603{
Borislav Petkovb16aabc2009-01-02 16:12:56 +0100604 struct ide_atapi_pc *uninitialized_var(pc);
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200605 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100606 struct request *rq = hwif->rq;
Bartlomiej Zolnierkiewiczbaf08f02008-10-13 21:39:32 +0200607 ide_expiry_t *expiry;
608 unsigned int timeout;
Borislav Petkov8c662852009-01-02 16:12:54 +0100609 int cmd_len;
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200610 ide_startstop_t startstop;
611 u8 ireason;
612
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200613 if (ide_wait_stat(&startstop, drive, ATA_DRQ, ATA_BUSY, WAIT_READY)) {
Borislav Petkov103f7032009-04-26 10:39:07 +0200614 printk(KERN_ERR PFX "%s: Strange, packet command initiated yet "
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200615 "DRQ isn't asserted\n", drive->name);
616 return startstop;
617 }
618
Borislav Petkov5f258432009-01-02 16:12:53 +0100619 if (drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT) {
620 if (drive->dma)
621 drive->waiting_for_dma = 1;
622 }
623
Borislav Petkov8c662852009-01-02 16:12:54 +0100624 if (dev_is_idecd(drive)) {
625 /* ATAPI commands get padded out to 12 bytes minimum */
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100626 cmd_len = COMMAND_SIZE(scsi_req(rq)->cmd[0]);
Borislav Petkov8c662852009-01-02 16:12:54 +0100627 if (cmd_len < ATAPI_MIN_CDB_BYTES)
628 cmd_len = ATAPI_MIN_CDB_BYTES;
Borislav Petkovdef860d2009-01-02 16:12:55 +0100629
630 timeout = rq->timeout;
631 expiry = ide_cd_expiry;
632 } else {
Borislav Petkovb16aabc2009-01-02 16:12:56 +0100633 pc = drive->pc;
634
Borislav Petkov8c662852009-01-02 16:12:54 +0100635 cmd_len = ATAPI_MIN_CDB_BYTES;
636
Borislav Petkovdef860d2009-01-02 16:12:55 +0100637 /*
638 * If necessary schedule the packet transfer to occur 'timeout'
Martin Olsson19af5cd2009-04-23 11:37:37 +0200639 * milliseconds later in ide_delayed_transfer_pc() after the
Borislav Petkovdef860d2009-01-02 16:12:55 +0100640 * device says it's ready for a packet.
641 */
642 if (drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) {
643 timeout = drive->pc_delay;
644 expiry = &ide_delayed_transfer_pc;
645 } else {
646 timeout = (drive->media == ide_floppy) ? WAIT_FLOPPY_CMD
647 : WAIT_TAPE_CMD;
648 expiry = NULL;
649 }
Borislav Petkov06cc2772009-01-02 16:12:56 +0100650
651 ireason = ide_read_ireason(drive);
652 if (drive->media == ide_tape)
653 ireason = ide_wait_ireason(drive, ireason);
654
655 if ((ireason & ATAPI_COD) == 0 || (ireason & ATAPI_IO)) {
Borislav Petkov103f7032009-04-26 10:39:07 +0200656 printk(KERN_ERR PFX "%s: (IO,CoD) != (0,1) while "
657 "issuing a packet command\n", drive->name);
Borislav Petkov06cc2772009-01-02 16:12:56 +0100658
659 return ide_do_reset(drive);
660 }
Bartlomiej Zolnierkiewiczbaf08f02008-10-13 21:39:32 +0200661 }
662
Bartlomiej Zolnierkiewicz60c0cd02009-03-27 12:46:46 +0100663 hwif->expiry = expiry;
664
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200665 /* Set the interrupt routine */
Borislav Petkovd6251d442009-01-06 17:20:58 +0100666 ide_set_handler(drive,
667 (dev_is_idecd(drive) ? drive->irq_handler
668 : ide_pc_intr),
Bartlomiej Zolnierkiewicz60c0cd02009-03-27 12:46:46 +0100669 timeout);
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200670
Borislav Petkov2eba0822009-03-31 20:14:58 +0200671 /* Send the actual packet */
672 if ((drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) == 0)
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100673 hwif->tp_ops->output_data(drive, NULL, scsi_req(rq)->cmd, cmd_len);
Borislav Petkov2eba0822009-03-31 20:14:58 +0200674
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200675 /* Begin DMA, if necessary */
Borislav Petkovb16aabc2009-01-02 16:12:56 +0100676 if (dev_is_idecd(drive)) {
677 if (drive->dma)
678 hwif->dma_ops->dma_start(drive);
679 } else {
680 if (pc->flags & PC_FLAG_DMA_OK) {
681 pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
682 hwif->dma_ops->dma_start(drive);
683 }
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200684 }
685
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200686 return ide_started;
687}
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200688
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100689ide_startstop_t ide_issue_pc(ide_drive_t *drive, struct ide_cmd *cmd)
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200690{
Borislav Petkovd77612a2009-01-02 16:12:55 +0100691 struct ide_atapi_pc *pc;
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200692 ide_hwif_t *hwif = drive->hwif;
Borislav Petkov4cad0852009-01-02 16:12:53 +0100693 ide_expiry_t *expiry = NULL;
Bartlomiej Zolnierkiewicze6830a82009-03-27 12:46:37 +0100694 struct request *rq = hwif->rq;
Borislav Petkovdfb7e622009-05-01 20:35:21 +0200695 unsigned int timeout, bytes;
Borislav Petkov392de1d2009-01-02 16:12:52 +0100696 u16 bcount;
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200697 u8 valid_tf;
Bartlomiej Zolnierkiewicz35b5d0b2009-03-27 12:46:47 +0100698 u8 drq_int = !!(drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT);
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200699
Borislav Petkoved485542009-01-02 16:12:52 +0100700 if (dev_is_idecd(drive)) {
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200701 valid_tf = IDE_VALID_NSECT | IDE_VALID_LBAL;
Bartlomiej Zolnierkiewicze6830a82009-03-27 12:46:37 +0100702 bcount = ide_cd_get_xferlen(rq);
Borislav Petkov4cad0852009-01-02 16:12:53 +0100703 expiry = ide_cd_expiry;
Borislav Petkov28ad91d2009-01-02 16:12:56 +0100704 timeout = ATAPI_WAIT_PC;
Borislav Petkovd77612a2009-01-02 16:12:55 +0100705
Bartlomiej Zolnierkiewicz5ae54122009-03-31 20:15:20 +0200706 if (drive->dma)
707 drive->dma = !ide_dma_prepare(drive, cmd);
Borislav Petkoved485542009-01-02 16:12:52 +0100708 } else {
Borislav Petkovd77612a2009-01-02 16:12:55 +0100709 pc = drive->pc;
710
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200711 valid_tf = IDE_VALID_DEVICE;
Borislav Petkovdfb7e622009-05-01 20:35:21 +0200712 bytes = blk_rq_bytes(rq);
Borislav Petkovdfb7e622009-05-01 20:35:21 +0200713 bcount = ((drive->media == ide_tape) ? bytes
714 : min_t(unsigned int,
715 bytes, 63 * 1024));
Borislav Petkovd77612a2009-01-02 16:12:55 +0100716
Borislav Petkov077e6db2009-05-01 21:21:02 +0200717 /* We haven't transferred any data yet */
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100718 scsi_req(rq)->resid_len = bcount;
Borislav Petkovd77612a2009-01-02 16:12:55 +0100719
720 if (pc->flags & PC_FLAG_DMA_ERROR) {
721 pc->flags &= ~PC_FLAG_DMA_ERROR;
722 ide_dma_off(drive);
723 }
724
Bartlomiej Zolnierkiewicz5ae54122009-03-31 20:15:20 +0200725 if (pc->flags & PC_FLAG_DMA_OK)
726 drive->dma = !ide_dma_prepare(drive, cmd);
Borislav Petkovd77612a2009-01-02 16:12:55 +0100727
728 if (!drive->dma)
729 pc->flags &= ~PC_FLAG_DMA_OK;
Borislav Petkov28ad91d2009-01-02 16:12:56 +0100730
731 timeout = (drive->media == ide_floppy) ? WAIT_FLOPPY_CMD
732 : WAIT_TAPE_CMD;
Borislav Petkoved485542009-01-02 16:12:52 +0100733 }
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200734
Sergei Shtylyov60f85012009-04-08 14:13:01 +0200735 ide_init_packet_cmd(cmd, valid_tf, bcount, drive->dma);
Bartlomiej Zolnierkiewiczb788ee92009-03-27 12:46:46 +0100736
737 (void)do_rw_taskfile(drive, cmd);
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200738
Bartlomiej Zolnierkiewicz35b5d0b2009-03-27 12:46:47 +0100739 if (drq_int) {
Borislav Petkov5f258432009-01-02 16:12:53 +0100740 if (drive->dma)
741 drive->waiting_for_dma = 0;
Bartlomiej Zolnierkiewicz22117d62009-03-27 12:46:47 +0100742 hwif->expiry = expiry;
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200743 }
Bartlomiej Zolnierkiewicz35b5d0b2009-03-27 12:46:47 +0100744
745 ide_execute_command(drive, cmd, ide_transfer_pc, timeout);
746
747 return drq_int ? ide_started : ide_transfer_pc(drive);
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200748}
749EXPORT_SYMBOL_GPL(ide_issue_pc);