ide: add ->pc_{update,io}_buffers methods
[GitHub/LineageOS/android_kernel_motorola_exynos9610.git] / drivers / ide / ide-atapi.c
CommitLineData
594c16d8
BZ
1/*
2 * ATAPI support.
3 */
4
5#include <linux/kernel.h>
6#include <linux/delay.h>
7#include <linux/ide.h>
646c0cb6
BZ
8#include <scsi/scsi.h>
9
10#ifdef DEBUG
11#define debug_log(fmt, args...) \
12 printk(KERN_INFO "ide: " fmt, ## args)
13#else
14#define debug_log(fmt, args...) do {} while (0)
15#endif
16
51509eec
BZ
17/*
18 * Check whether we can support a device,
19 * based on the ATAPI IDENTIFY command results.
20 */
21int ide_check_atapi_device(ide_drive_t *drive, const char *s)
22{
23 u16 *id = drive->id;
24 u8 gcw[2], protocol, device_type, removable, drq_type, packet_size;
25
26 *((u16 *)&gcw) = id[ATA_ID_CONFIG];
27
28 protocol = (gcw[1] & 0xC0) >> 6;
29 device_type = gcw[1] & 0x1F;
30 removable = (gcw[0] & 0x80) >> 7;
31 drq_type = (gcw[0] & 0x60) >> 5;
32 packet_size = gcw[0] & 0x03;
33
34#ifdef CONFIG_PPC
35 /* kludge for Apple PowerBook internal zip */
36 if (drive->media == ide_floppy && device_type == 5 &&
37 !strstr((char *)&id[ATA_ID_PROD], "CD-ROM") &&
38 strstr((char *)&id[ATA_ID_PROD], "ZIP"))
39 device_type = 0;
40#endif
41
42 if (protocol != 2)
43 printk(KERN_ERR "%s: %s: protocol (0x%02x) is not ATAPI\n",
44 s, drive->name, protocol);
45 else if ((drive->media == ide_floppy && device_type != 0) ||
46 (drive->media == ide_tape && device_type != 1))
47 printk(KERN_ERR "%s: %s: invalid device type (0x%02x)\n",
48 s, drive->name, device_type);
49 else if (removable == 0)
50 printk(KERN_ERR "%s: %s: the removable flag is not set\n",
51 s, drive->name);
52 else if (drive->media == ide_floppy && drq_type == 3)
53 printk(KERN_ERR "%s: %s: sorry, DRQ type (0x%02x) not "
54 "supported\n", s, drive->name, drq_type);
55 else if (packet_size != 0)
56 printk(KERN_ERR "%s: %s: packet size (0x%02x) is not 12 "
57 "bytes\n", s, drive->name, packet_size);
58 else
59 return 1;
60 return 0;
61}
62EXPORT_SYMBOL_GPL(ide_check_atapi_device);
63
acaa0f5f
BZ
64/* PIO data transfer routine using the scatter gather table. */
65int ide_io_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
66 unsigned int bcount, int write)
67{
68 ide_hwif_t *hwif = drive->hwif;
69 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
70 xfer_func_t *xf = write ? tp_ops->output_data : tp_ops->input_data;
71 struct scatterlist *sg = pc->sg;
72 char *buf;
73 int count, done = 0;
74
75 while (bcount) {
76 count = min(sg->length - pc->b_count, bcount);
77
78 if (PageHighMem(sg_page(sg))) {
79 unsigned long flags;
80
81 local_irq_save(flags);
82 buf = kmap_atomic(sg_page(sg), KM_IRQ0) + sg->offset;
83 xf(drive, NULL, buf + pc->b_count, count);
84 kunmap_atomic(buf - sg->offset, KM_IRQ0);
85 local_irq_restore(flags);
86 } else {
87 buf = sg_virt(sg);
88 xf(drive, NULL, buf + pc->b_count, count);
89 }
90
91 bcount -= count;
92 pc->b_count += count;
93 done += count;
94
95 if (pc->b_count == sg->length) {
96 if (!--pc->sg_cnt)
97 break;
98 pc->sg = sg = sg_next(sg);
99 pc->b_count = 0;
100 }
101 }
102
103 if (bcount) {
104 printk(KERN_ERR "%s: %d leftover bytes, %s\n", drive->name,
105 bcount, write ? "padding with zeros"
106 : "discarding data");
107 ide_pad_transfer(drive, write, bcount);
108 }
109
110 return done;
111}
112EXPORT_SYMBOL_GPL(ide_io_buffers);
113
7bf7420a
BZ
114void ide_init_pc(struct ide_atapi_pc *pc)
115{
116 memset(pc, 0, sizeof(*pc));
117 pc->buf = pc->pc_buf;
118 pc->buf_size = IDE_PC_BUFFER_SIZE;
119}
120EXPORT_SYMBOL_GPL(ide_init_pc);
121
7645c151
BZ
122/*
123 * Generate a new packet command request in front of the request queue, before
124 * the current request, so that it will be processed immediately, on the next
125 * pass through the driver.
126 */
6b0da28b
BZ
127static void ide_queue_pc_head(ide_drive_t *drive, struct gendisk *disk,
128 struct ide_atapi_pc *pc, struct request *rq)
7645c151
BZ
129{
130 blk_rq_init(NULL, rq);
131 rq->cmd_type = REQ_TYPE_SPECIAL;
132 rq->cmd_flags |= REQ_PREEMPT;
133 rq->buffer = (char *)pc;
134 rq->rq_disk = disk;
135 memcpy(rq->cmd, pc->c, 12);
136 if (drive->media == ide_tape)
137 rq->cmd[13] = REQ_IDETAPE_PC1;
138 ide_do_drive_cmd(drive, rq);
139}
7645c151 140
2ac07d92
BZ
141/*
142 * Add a special packet command request to the tail of the request queue,
143 * and wait for it to be serviced.
144 */
145int ide_queue_pc_tail(ide_drive_t *drive, struct gendisk *disk,
146 struct ide_atapi_pc *pc)
147{
148 struct request *rq;
149 int error;
150
151 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
152 rq->cmd_type = REQ_TYPE_SPECIAL;
153 rq->buffer = (char *)pc;
154 memcpy(rq->cmd, pc->c, 12);
155 if (drive->media == ide_tape)
156 rq->cmd[13] = REQ_IDETAPE_PC1;
157 error = blk_execute_rq(drive->queue, disk, rq, 0);
158 blk_put_request(rq);
159
160 return error;
161}
162EXPORT_SYMBOL_GPL(ide_queue_pc_tail);
163
de699ad5
BZ
164int ide_do_test_unit_ready(ide_drive_t *drive, struct gendisk *disk)
165{
166 struct ide_atapi_pc pc;
167
168 ide_init_pc(&pc);
169 pc.c[0] = TEST_UNIT_READY;
170
171 return ide_queue_pc_tail(drive, disk, &pc);
172}
173EXPORT_SYMBOL_GPL(ide_do_test_unit_ready);
174
0c8a6c7a
BZ
175int ide_do_start_stop(ide_drive_t *drive, struct gendisk *disk, int start)
176{
177 struct ide_atapi_pc pc;
178
179 ide_init_pc(&pc);
180 pc.c[0] = START_STOP;
181 pc.c[4] = start;
182
183 if (drive->media == ide_tape)
184 pc.flags |= PC_FLAG_WAIT_FOR_DSC;
185
186 return ide_queue_pc_tail(drive, disk, &pc);
187}
188EXPORT_SYMBOL_GPL(ide_do_start_stop);
189
0578042d
BZ
190int ide_set_media_lock(ide_drive_t *drive, struct gendisk *disk, int on)
191{
192 struct ide_atapi_pc pc;
193
194 if (drive->atapi_flags & IDE_AFLAG_NO_DOORLOCK)
195 return 0;
196
197 ide_init_pc(&pc);
198 pc.c[0] = ALLOW_MEDIUM_REMOVAL;
199 pc.c[4] = on;
200
201 return ide_queue_pc_tail(drive, disk, &pc);
202}
203EXPORT_SYMBOL_GPL(ide_set_media_lock);
204
6b0da28b
BZ
205void ide_create_request_sense_cmd(ide_drive_t *drive, struct ide_atapi_pc *pc)
206{
207 ide_init_pc(pc);
208 pc->c[0] = REQUEST_SENSE;
209 if (drive->media == ide_floppy) {
210 pc->c[4] = 255;
211 pc->req_xfer = 18;
212 } else {
213 pc->c[4] = 20;
214 pc->req_xfer = 20;
215 }
216}
217EXPORT_SYMBOL_GPL(ide_create_request_sense_cmd);
218
219/*
220 * Called when an error was detected during the last packet command.
221 * We queue a request sense packet command in the head of the request list.
222 */
223void ide_retry_pc(ide_drive_t *drive, struct gendisk *disk)
224{
225 struct request *rq = &drive->request_sense_rq;
226 struct ide_atapi_pc *pc = &drive->request_sense_pc;
227
228 (void)ide_read_error(drive);
229 ide_create_request_sense_cmd(drive, pc);
230 if (drive->media == ide_tape)
231 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
232 ide_queue_pc_head(drive, disk, pc, rq);
233}
234EXPORT_SYMBOL_GPL(ide_retry_pc);
235
844b9468
BZ
236int ide_scsi_expiry(ide_drive_t *drive)
237{
238 struct ide_atapi_pc *pc = drive->pc;
239
240 debug_log("%s called for %lu at %lu\n", __func__,
241 pc->scsi_cmd->serial_number, jiffies);
242
243 pc->flags |= PC_FLAG_TIMEDOUT;
244
245 return 0; /* we do not want the IDE subsystem to retry */
246}
247EXPORT_SYMBOL_GPL(ide_scsi_expiry);
248
85e39035 249ide_startstop_t ide_pc_intr(ide_drive_t *drive, ide_handler_t *handler)
646c0cb6 250{
2b9efba4 251 struct ide_atapi_pc *pc = drive->pc;
646c0cb6 252 ide_hwif_t *hwif = drive->hwif;
8fccf899 253 struct request *rq = hwif->hwgroup->rq;
374e042c 254 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
646c0cb6 255 xfer_func_t *xferfunc;
844b9468
BZ
256 ide_expiry_t *expiry;
257 unsigned int timeout, temp;
646c0cb6 258 u16 bcount;
b14c7212 259 u8 stat, ireason, scsi = drive->scsi, dsc = 0;
646c0cb6
BZ
260
261 debug_log("Enter %s - interrupt handler\n", __func__);
262
844b9468
BZ
263 if (scsi) {
264 timeout = ide_scsi_get_timeout(pc);
265 expiry = ide_scsi_expiry;
266 } else {
267 timeout = (drive->media == ide_floppy) ? WAIT_FLOPPY_CMD
268 : WAIT_TAPE_CMD;
269 expiry = NULL;
270 }
271
646c0cb6 272 if (pc->flags & PC_FLAG_TIMEDOUT) {
b14c7212 273 drive->pc_callback(drive, 0);
646c0cb6
BZ
274 return ide_stopped;
275 }
276
277 /* Clear the interrupt */
374e042c 278 stat = tp_ops->read_status(hwif);
646c0cb6
BZ
279
280 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
281 if (hwif->dma_ops->dma_end(drive) ||
3a7d2484 282 (drive->media == ide_tape && !scsi && (stat & ATA_ERR))) {
646c0cb6
BZ
283 if (drive->media == ide_floppy && !scsi)
284 printk(KERN_ERR "%s: DMA %s error\n",
285 drive->name, rq_data_dir(pc->rq)
286 ? "write" : "read");
287 pc->flags |= PC_FLAG_DMA_ERROR;
288 } else {
289 pc->xferred = pc->req_xfer;
85e39035
BZ
290 if (drive->pc_update_buffers)
291 drive->pc_update_buffers(drive, pc);
646c0cb6
BZ
292 }
293 debug_log("%s: DMA finished\n", drive->name);
294 }
295
296 /* No more interrupts */
3a7d2484 297 if ((stat & ATA_DRQ) == 0) {
646c0cb6
BZ
298 debug_log("Packet command completed, %d bytes transferred\n",
299 pc->xferred);
300
301 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
302
303 local_irq_enable_in_hardirq();
304
305 if (drive->media == ide_tape && !scsi &&
3a7d2484
BZ
306 (stat & ATA_ERR) && rq->cmd[0] == REQUEST_SENSE)
307 stat &= ~ATA_ERR;
8fccf899 308
3a7d2484 309 if ((stat & ATA_ERR) || (pc->flags & PC_FLAG_DMA_ERROR)) {
646c0cb6
BZ
310 /* Error detected */
311 debug_log("%s: I/O error\n", drive->name);
312
313 if (drive->media != ide_tape || scsi) {
314 pc->rq->errors++;
315 if (scsi)
316 goto cmd_finished;
317 }
318
8fccf899 319 if (rq->cmd[0] == REQUEST_SENSE) {
646c0cb6
BZ
320 printk(KERN_ERR "%s: I/O error in request sense"
321 " command\n", drive->name);
322 return ide_do_reset(drive);
323 }
324
8fccf899 325 debug_log("[cmd %x]: check condition\n", rq->cmd[0]);
646c0cb6
BZ
326
327 /* Retry operation */
6b0da28b 328 ide_retry_pc(drive, rq->rq_disk);
8fccf899 329
646c0cb6
BZ
330 /* queued, but not started */
331 return ide_stopped;
332 }
333cmd_finished:
334 pc->error = 0;
b14c7212
BZ
335
336 if ((pc->flags & PC_FLAG_WAIT_FOR_DSC) && (stat & ATA_DSC) == 0)
337 dsc = 1;
8fccf899 338
646c0cb6 339 /* Command finished - Call the callback function */
b14c7212 340 drive->pc_callback(drive, dsc);
8fccf899 341
646c0cb6
BZ
342 return ide_stopped;
343 }
344
345 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
346 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
347 printk(KERN_ERR "%s: The device wants to issue more interrupts "
348 "in DMA mode\n", drive->name);
349 ide_dma_off(drive);
350 return ide_do_reset(drive);
351 }
646c0cb6 352
1823649b
BZ
353 /* Get the number of bytes to transfer on this interrupt. */
354 ide_read_bcount_and_ireason(drive, &bcount, &ireason);
646c0cb6 355
3a7d2484 356 if (ireason & ATAPI_COD) {
646c0cb6
BZ
357 printk(KERN_ERR "%s: CoD != 0 in %s\n", drive->name, __func__);
358 return ide_do_reset(drive);
359 }
8fccf899 360
3a7d2484
BZ
361 if (((ireason & ATAPI_IO) == ATAPI_IO) ==
362 !!(pc->flags & PC_FLAG_WRITING)) {
646c0cb6
BZ
363 /* Hopefully, we will never get here */
364 printk(KERN_ERR "%s: We wanted to %s, but the device wants us "
365 "to %s!\n", drive->name,
3a7d2484
BZ
366 (ireason & ATAPI_IO) ? "Write" : "Read",
367 (ireason & ATAPI_IO) ? "Read" : "Write");
646c0cb6
BZ
368 return ide_do_reset(drive);
369 }
8fccf899 370
646c0cb6
BZ
371 if (!(pc->flags & PC_FLAG_WRITING)) {
372 /* Reading - Check that we have enough space */
373 temp = pc->xferred + bcount;
374 if (temp > pc->req_xfer) {
375 if (temp > pc->buf_size) {
376 printk(KERN_ERR "%s: The device wants to send "
377 "us more data than expected - "
378 "discarding data\n",
379 drive->name);
380 if (scsi)
381 temp = pc->buf_size - pc->xferred;
382 else
383 temp = 0;
384 if (temp) {
385 if (pc->sg)
85e39035
BZ
386 drive->pc_io_buffers(drive, pc,
387 temp, 0);
646c0cb6 388 else
374e042c 389 tp_ops->input_data(drive, NULL,
646c0cb6
BZ
390 pc->cur_pos, temp);
391 printk(KERN_ERR "%s: transferred %d of "
392 "%d bytes\n",
393 drive->name,
394 temp, bcount);
395 }
396 pc->xferred += temp;
397 pc->cur_pos += temp;
398 ide_pad_transfer(drive, 0, bcount - temp);
844b9468 399 goto next_irq;
646c0cb6
BZ
400 }
401 debug_log("The device wants to send us more data than "
402 "expected - allowing transfer\n");
403 }
374e042c 404 xferfunc = tp_ops->input_data;
646c0cb6 405 } else
374e042c 406 xferfunc = tp_ops->output_data;
646c0cb6
BZ
407
408 if ((drive->media == ide_floppy && !scsi && !pc->buf) ||
409 (drive->media == ide_tape && !scsi && pc->bh) ||
acaa0f5f 410 (scsi && pc->sg)) {
85e39035 411 int done = drive->pc_io_buffers(drive, pc, bcount,
acaa0f5f
BZ
412 !!(pc->flags & PC_FLAG_WRITING));
413
414 /* FIXME: don't do partial completions */
415 if (drive->media == ide_floppy && !scsi)
416 ide_end_request(drive, 1, done >> 9);
417 } else
646c0cb6
BZ
418 xferfunc(drive, NULL, pc->cur_pos, bcount);
419
420 /* Update the current position */
421 pc->xferred += bcount;
422 pc->cur_pos += bcount;
423
424 debug_log("[cmd %x] transferred %d bytes on that intr.\n",
8fccf899 425 rq->cmd[0], bcount);
844b9468 426next_irq:
646c0cb6
BZ
427 /* And set the interrupt handler again */
428 ide_set_handler(drive, handler, timeout, expiry);
429 return ide_started;
430}
431EXPORT_SYMBOL_GPL(ide_pc_intr);
594c16d8 432
88a72109
BZ
433static u8 ide_read_ireason(ide_drive_t *drive)
434{
435 ide_task_t task;
436
437 memset(&task, 0, sizeof(task));
438 task.tf_flags = IDE_TFLAG_IN_NSECT;
439
374e042c 440 drive->hwif->tp_ops->tf_read(drive, &task);
88a72109
BZ
441
442 return task.tf.nsect & 3;
443}
444
594c16d8
BZ
445static u8 ide_wait_ireason(ide_drive_t *drive, u8 ireason)
446{
594c16d8
BZ
447 int retries = 100;
448
3a7d2484
BZ
449 while (retries-- && ((ireason & ATAPI_COD) == 0 ||
450 (ireason & ATAPI_IO))) {
594c16d8
BZ
451 printk(KERN_ERR "%s: (IO,CoD != (0,1) while issuing "
452 "a packet command, retrying\n", drive->name);
453 udelay(100);
88a72109 454 ireason = ide_read_ireason(drive);
594c16d8
BZ
455 if (retries == 0) {
456 printk(KERN_ERR "%s: (IO,CoD != (0,1) while issuing "
457 "a packet command, ignoring\n",
458 drive->name);
3a7d2484
BZ
459 ireason |= ATAPI_COD;
460 ireason &= ~ATAPI_IO;
594c16d8
BZ
461 }
462 }
463
464 return ireason;
465}
466
2b9efba4 467ide_startstop_t ide_transfer_pc(ide_drive_t *drive,
594c16d8
BZ
468 ide_handler_t *handler, unsigned int timeout,
469 ide_expiry_t *expiry)
470{
2b9efba4 471 struct ide_atapi_pc *pc = drive->pc;
594c16d8 472 ide_hwif_t *hwif = drive->hwif;
8fccf899 473 struct request *rq = hwif->hwgroup->rq;
594c16d8
BZ
474 ide_startstop_t startstop;
475 u8 ireason;
476
3a7d2484 477 if (ide_wait_stat(&startstop, drive, ATA_DRQ, ATA_BUSY, WAIT_READY)) {
594c16d8
BZ
478 printk(KERN_ERR "%s: Strange, packet command initiated yet "
479 "DRQ isn't asserted\n", drive->name);
480 return startstop;
481 }
482
88a72109 483 ireason = ide_read_ireason(drive);
594c16d8
BZ
484 if (drive->media == ide_tape && !drive->scsi)
485 ireason = ide_wait_ireason(drive, ireason);
486
3a7d2484 487 if ((ireason & ATAPI_COD) == 0 || (ireason & ATAPI_IO)) {
594c16d8
BZ
488 printk(KERN_ERR "%s: (IO,CoD) != (0,1) while issuing "
489 "a packet command\n", drive->name);
490 return ide_do_reset(drive);
491 }
492
493 /* Set the interrupt routine */
494 ide_set_handler(drive, handler, timeout, expiry);
495
496 /* Begin DMA, if necessary */
497 if (pc->flags & PC_FLAG_DMA_OK) {
498 pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
499 hwif->dma_ops->dma_start(drive);
500 }
501
502 /* Send the actual packet */
ea68d270 503 if ((drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) == 0)
8fccf899 504 hwif->tp_ops->output_data(drive, NULL, rq->cmd, 12);
594c16d8
BZ
505
506 return ide_started;
507}
508EXPORT_SYMBOL_GPL(ide_transfer_pc);
6bf1641c 509
2b9efba4 510ide_startstop_t ide_issue_pc(ide_drive_t *drive,
6bf1641c
BZ
511 ide_handler_t *handler, unsigned int timeout,
512 ide_expiry_t *expiry)
513{
2b9efba4 514 struct ide_atapi_pc *pc = drive->pc;
6bf1641c
BZ
515 ide_hwif_t *hwif = drive->hwif;
516 u16 bcount;
517 u8 dma = 0;
518
519 /* We haven't transferred any data yet */
520 pc->xferred = 0;
521 pc->cur_pos = pc->buf;
522
523 /* Request to transfer the entire buffer at once */
524 if (drive->media == ide_tape && !drive->scsi)
525 bcount = pc->req_xfer;
526 else
527 bcount = min(pc->req_xfer, 63 * 1024);
528
529 if (pc->flags & PC_FLAG_DMA_ERROR) {
530 pc->flags &= ~PC_FLAG_DMA_ERROR;
531 ide_dma_off(drive);
532 }
533
534 if ((pc->flags & PC_FLAG_DMA_OK) && drive->using_dma) {
535 if (drive->scsi)
536 hwif->sg_mapped = 1;
537 dma = !hwif->dma_ops->dma_setup(drive);
538 if (drive->scsi)
539 hwif->sg_mapped = 0;
540 }
541
542 if (!dma)
543 pc->flags &= ~PC_FLAG_DMA_OK;
544
545 ide_pktcmd_tf_load(drive, drive->scsi ? 0 : IDE_TFLAG_OUT_DEVICE,
546 bcount, dma);
547
548 /* Issue the packet command */
ac77ef8b 549 if (drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT) {
aaaade3f 550 ide_execute_command(drive, ATA_CMD_PACKET, handler,
6bf1641c
BZ
551 timeout, NULL);
552 return ide_started;
553 } else {
554 ide_execute_pkt_cmd(drive);
555 return (*handler)(drive);
556 }
557}
558EXPORT_SYMBOL_GPL(ide_issue_pc);