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