This source file includes following definitions.
- ata_sff_check_status
- ata_sff_altstatus
- ata_sff_irq_status
- ata_sff_sync
- ata_sff_pause
- ata_sff_dma_pause
- ata_sff_busy_sleep
- ata_sff_check_ready
- ata_sff_wait_ready
- ata_sff_set_devctl
- ata_sff_dev_select
- ata_dev_select
- ata_sff_irq_on
- ata_sff_tf_load
- ata_sff_tf_read
- ata_sff_exec_command
- ata_tf_to_host
- ata_sff_data_xfer
- ata_sff_data_xfer32
- ata_pio_sector
- ata_pio_sectors
- atapi_send_cdb
- __atapi_pio_bytes
- atapi_pio_bytes
- ata_hsm_ok_in_wq
- ata_hsm_qc_complete
- ata_sff_hsm_move
- ata_sff_queue_work
- ata_sff_queue_delayed_work
- ata_sff_queue_pio_task
- ata_sff_flush_pio_task
- ata_sff_pio_task
- ata_sff_qc_issue
- ata_sff_qc_fill_rtf
- ata_sff_idle_irq
- __ata_sff_port_intr
- ata_sff_port_intr
- __ata_sff_interrupt
- ata_sff_interrupt
- ata_sff_lost_interrupt
- ata_sff_freeze
- ata_sff_thaw
- ata_sff_prereset
- ata_devchk
- ata_sff_dev_classify
- ata_sff_wait_after_reset
- ata_bus_softreset
- ata_sff_softreset
- sata_sff_hardreset
- ata_sff_postreset
- ata_sff_drain_fifo
- ata_sff_error_handler
- ata_sff_std_ports
- ata_resources_present
- ata_pci_sff_init_host
- ata_pci_sff_prepare_host
- ata_pci_sff_activate_host
- ata_sff_find_valid_pi
- ata_pci_init_one
- ata_pci_sff_init_one
- ata_bmdma_fill_sg
- ata_bmdma_fill_sg_dumb
- ata_bmdma_qc_prep
- ata_bmdma_dumb_qc_prep
- ata_bmdma_qc_issue
- ata_bmdma_port_intr
- ata_bmdma_interrupt
- ata_bmdma_error_handler
- ata_bmdma_post_internal_cmd
- ata_bmdma_irq_clear
- ata_bmdma_setup
- ata_bmdma_start
- ata_bmdma_stop
- ata_bmdma_status
- ata_bmdma_port_start
- ata_bmdma_port_start32
- ata_pci_bmdma_clear_simplex
- ata_bmdma_nodma
- ata_pci_bmdma_init
- ata_pci_bmdma_prepare_host
- ata_pci_bmdma_init_one
- ata_sff_port_init
- ata_sff_init
- ata_sff_exit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 #include <linux/kernel.h>
20 #include <linux/gfp.h>
21 #include <linux/pci.h>
22 #include <linux/module.h>
23 #include <linux/libata.h>
24 #include <linux/highmem.h>
25
26 #include "libata.h"
27
28 static struct workqueue_struct *ata_sff_wq;
29
30 const struct ata_port_operations ata_sff_port_ops = {
31 .inherits = &ata_base_port_ops,
32
33 .qc_prep = ata_noop_qc_prep,
34 .qc_issue = ata_sff_qc_issue,
35 .qc_fill_rtf = ata_sff_qc_fill_rtf,
36
37 .freeze = ata_sff_freeze,
38 .thaw = ata_sff_thaw,
39 .prereset = ata_sff_prereset,
40 .softreset = ata_sff_softreset,
41 .hardreset = sata_sff_hardreset,
42 .postreset = ata_sff_postreset,
43 .error_handler = ata_sff_error_handler,
44
45 .sff_dev_select = ata_sff_dev_select,
46 .sff_check_status = ata_sff_check_status,
47 .sff_tf_load = ata_sff_tf_load,
48 .sff_tf_read = ata_sff_tf_read,
49 .sff_exec_command = ata_sff_exec_command,
50 .sff_data_xfer = ata_sff_data_xfer,
51 .sff_drain_fifo = ata_sff_drain_fifo,
52
53 .lost_interrupt = ata_sff_lost_interrupt,
54 };
55 EXPORT_SYMBOL_GPL(ata_sff_port_ops);
56
57
58
59
60
61
62
63
64
65
66
67
68 u8 ata_sff_check_status(struct ata_port *ap)
69 {
70 return ioread8(ap->ioaddr.status_addr);
71 }
72 EXPORT_SYMBOL_GPL(ata_sff_check_status);
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87 static u8 ata_sff_altstatus(struct ata_port *ap)
88 {
89 if (ap->ops->sff_check_altstatus)
90 return ap->ops->sff_check_altstatus(ap);
91
92 return ioread8(ap->ioaddr.altstatus_addr);
93 }
94
95
96
97
98
99
100
101
102
103
104
105
106
107 static u8 ata_sff_irq_status(struct ata_port *ap)
108 {
109 u8 status;
110
111 if (ap->ops->sff_check_altstatus || ap->ioaddr.altstatus_addr) {
112 status = ata_sff_altstatus(ap);
113
114 if (status & ATA_BUSY)
115 return status;
116 }
117
118 status = ap->ops->sff_check_status(ap);
119 return status;
120 }
121
122
123
124
125
126
127
128
129
130
131
132
133
134 static void ata_sff_sync(struct ata_port *ap)
135 {
136 if (ap->ops->sff_check_altstatus)
137 ap->ops->sff_check_altstatus(ap);
138 else if (ap->ioaddr.altstatus_addr)
139 ioread8(ap->ioaddr.altstatus_addr);
140 }
141
142
143
144
145
146
147
148
149
150
151
152
153
154 void ata_sff_pause(struct ata_port *ap)
155 {
156 ata_sff_sync(ap);
157 ndelay(400);
158 }
159 EXPORT_SYMBOL_GPL(ata_sff_pause);
160
161
162
163
164
165
166
167
168
169 void ata_sff_dma_pause(struct ata_port *ap)
170 {
171 if (ap->ops->sff_check_altstatus || ap->ioaddr.altstatus_addr) {
172
173
174 ata_sff_altstatus(ap);
175 return;
176 }
177
178
179
180 BUG();
181 }
182 EXPORT_SYMBOL_GPL(ata_sff_dma_pause);
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199 int ata_sff_busy_sleep(struct ata_port *ap,
200 unsigned long tmout_pat, unsigned long tmout)
201 {
202 unsigned long timer_start, timeout;
203 u8 status;
204
205 status = ata_sff_busy_wait(ap, ATA_BUSY, 300);
206 timer_start = jiffies;
207 timeout = ata_deadline(timer_start, tmout_pat);
208 while (status != 0xff && (status & ATA_BUSY) &&
209 time_before(jiffies, timeout)) {
210 ata_msleep(ap, 50);
211 status = ata_sff_busy_wait(ap, ATA_BUSY, 3);
212 }
213
214 if (status != 0xff && (status & ATA_BUSY))
215 ata_port_warn(ap,
216 "port is slow to respond, please be patient (Status 0x%x)\n",
217 status);
218
219 timeout = ata_deadline(timer_start, tmout);
220 while (status != 0xff && (status & ATA_BUSY) &&
221 time_before(jiffies, timeout)) {
222 ata_msleep(ap, 50);
223 status = ap->ops->sff_check_status(ap);
224 }
225
226 if (status == 0xff)
227 return -ENODEV;
228
229 if (status & ATA_BUSY) {
230 ata_port_err(ap,
231 "port failed to respond (%lu secs, Status 0x%x)\n",
232 DIV_ROUND_UP(tmout, 1000), status);
233 return -EBUSY;
234 }
235
236 return 0;
237 }
238 EXPORT_SYMBOL_GPL(ata_sff_busy_sleep);
239
240 static int ata_sff_check_ready(struct ata_link *link)
241 {
242 u8 status = link->ap->ops->sff_check_status(link->ap);
243
244 return ata_check_ready(status);
245 }
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261 int ata_sff_wait_ready(struct ata_link *link, unsigned long deadline)
262 {
263 return ata_wait_ready(link, deadline, ata_sff_check_ready);
264 }
265 EXPORT_SYMBOL_GPL(ata_sff_wait_ready);
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280 static void ata_sff_set_devctl(struct ata_port *ap, u8 ctl)
281 {
282 if (ap->ops->sff_set_devctl)
283 ap->ops->sff_set_devctl(ap, ctl);
284 else
285 iowrite8(ctl, ap->ioaddr.ctl_addr);
286 }
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302 void ata_sff_dev_select(struct ata_port *ap, unsigned int device)
303 {
304 u8 tmp;
305
306 if (device == 0)
307 tmp = ATA_DEVICE_OBS;
308 else
309 tmp = ATA_DEVICE_OBS | ATA_DEV1;
310
311 iowrite8(tmp, ap->ioaddr.device_addr);
312 ata_sff_pause(ap);
313 }
314 EXPORT_SYMBOL_GPL(ata_sff_dev_select);
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334 static void ata_dev_select(struct ata_port *ap, unsigned int device,
335 unsigned int wait, unsigned int can_sleep)
336 {
337 if (ata_msg_probe(ap))
338 ata_port_info(ap, "ata_dev_select: ENTER, device %u, wait %u\n",
339 device, wait);
340
341 if (wait)
342 ata_wait_idle(ap);
343
344 ap->ops->sff_dev_select(ap, device);
345
346 if (wait) {
347 if (can_sleep && ap->link.device[device].class == ATA_DEV_ATAPI)
348 ata_msleep(ap, 150);
349 ata_wait_idle(ap);
350 }
351 }
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366 void ata_sff_irq_on(struct ata_port *ap)
367 {
368 struct ata_ioports *ioaddr = &ap->ioaddr;
369
370 if (ap->ops->sff_irq_on) {
371 ap->ops->sff_irq_on(ap);
372 return;
373 }
374
375 ap->ctl &= ~ATA_NIEN;
376 ap->last_ctl = ap->ctl;
377
378 if (ap->ops->sff_set_devctl || ioaddr->ctl_addr)
379 ata_sff_set_devctl(ap, ap->ctl);
380 ata_wait_idle(ap);
381
382 if (ap->ops->sff_irq_clear)
383 ap->ops->sff_irq_clear(ap);
384 }
385 EXPORT_SYMBOL_GPL(ata_sff_irq_on);
386
387
388
389
390
391
392
393
394
395
396
397 void ata_sff_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
398 {
399 struct ata_ioports *ioaddr = &ap->ioaddr;
400 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
401
402 if (tf->ctl != ap->last_ctl) {
403 if (ioaddr->ctl_addr)
404 iowrite8(tf->ctl, ioaddr->ctl_addr);
405 ap->last_ctl = tf->ctl;
406 ata_wait_idle(ap);
407 }
408
409 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
410 WARN_ON_ONCE(!ioaddr->ctl_addr);
411 iowrite8(tf->hob_feature, ioaddr->feature_addr);
412 iowrite8(tf->hob_nsect, ioaddr->nsect_addr);
413 iowrite8(tf->hob_lbal, ioaddr->lbal_addr);
414 iowrite8(tf->hob_lbam, ioaddr->lbam_addr);
415 iowrite8(tf->hob_lbah, ioaddr->lbah_addr);
416 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
417 tf->hob_feature,
418 tf->hob_nsect,
419 tf->hob_lbal,
420 tf->hob_lbam,
421 tf->hob_lbah);
422 }
423
424 if (is_addr) {
425 iowrite8(tf->feature, ioaddr->feature_addr);
426 iowrite8(tf->nsect, ioaddr->nsect_addr);
427 iowrite8(tf->lbal, ioaddr->lbal_addr);
428 iowrite8(tf->lbam, ioaddr->lbam_addr);
429 iowrite8(tf->lbah, ioaddr->lbah_addr);
430 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
431 tf->feature,
432 tf->nsect,
433 tf->lbal,
434 tf->lbam,
435 tf->lbah);
436 }
437
438 if (tf->flags & ATA_TFLAG_DEVICE) {
439 iowrite8(tf->device, ioaddr->device_addr);
440 VPRINTK("device 0x%X\n", tf->device);
441 }
442
443 ata_wait_idle(ap);
444 }
445 EXPORT_SYMBOL_GPL(ata_sff_tf_load);
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460 void ata_sff_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
461 {
462 struct ata_ioports *ioaddr = &ap->ioaddr;
463
464 tf->command = ata_sff_check_status(ap);
465 tf->feature = ioread8(ioaddr->error_addr);
466 tf->nsect = ioread8(ioaddr->nsect_addr);
467 tf->lbal = ioread8(ioaddr->lbal_addr);
468 tf->lbam = ioread8(ioaddr->lbam_addr);
469 tf->lbah = ioread8(ioaddr->lbah_addr);
470 tf->device = ioread8(ioaddr->device_addr);
471
472 if (tf->flags & ATA_TFLAG_LBA48) {
473 if (likely(ioaddr->ctl_addr)) {
474 iowrite8(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
475 tf->hob_feature = ioread8(ioaddr->error_addr);
476 tf->hob_nsect = ioread8(ioaddr->nsect_addr);
477 tf->hob_lbal = ioread8(ioaddr->lbal_addr);
478 tf->hob_lbam = ioread8(ioaddr->lbam_addr);
479 tf->hob_lbah = ioread8(ioaddr->lbah_addr);
480 iowrite8(tf->ctl, ioaddr->ctl_addr);
481 ap->last_ctl = tf->ctl;
482 } else
483 WARN_ON_ONCE(1);
484 }
485 }
486 EXPORT_SYMBOL_GPL(ata_sff_tf_read);
487
488
489
490
491
492
493
494
495
496
497
498
499 void ata_sff_exec_command(struct ata_port *ap, const struct ata_taskfile *tf)
500 {
501 DPRINTK("ata%u: cmd 0x%X\n", ap->print_id, tf->command);
502
503 iowrite8(tf->command, ap->ioaddr.command_addr);
504 ata_sff_pause(ap);
505 }
506 EXPORT_SYMBOL_GPL(ata_sff_exec_command);
507
508
509
510
511
512
513
514
515
516
517
518
519
520 static inline void ata_tf_to_host(struct ata_port *ap,
521 const struct ata_taskfile *tf)
522 {
523 ap->ops->sff_tf_load(ap, tf);
524 ap->ops->sff_exec_command(ap, tf);
525 }
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542 unsigned int ata_sff_data_xfer(struct ata_queued_cmd *qc, unsigned char *buf,
543 unsigned int buflen, int rw)
544 {
545 struct ata_port *ap = qc->dev->link->ap;
546 void __iomem *data_addr = ap->ioaddr.data_addr;
547 unsigned int words = buflen >> 1;
548
549
550 if (rw == READ)
551 ioread16_rep(data_addr, buf, words);
552 else
553 iowrite16_rep(data_addr, buf, words);
554
555
556 if (unlikely(buflen & 0x01)) {
557 unsigned char pad[2] = { };
558
559
560 buf += buflen - 1;
561
562
563
564
565
566 if (rw == READ) {
567 ioread16_rep(data_addr, pad, 1);
568 *buf = pad[0];
569 } else {
570 pad[0] = *buf;
571 iowrite16_rep(data_addr, pad, 1);
572 }
573 words++;
574 }
575
576 return words << 1;
577 }
578 EXPORT_SYMBOL_GPL(ata_sff_data_xfer);
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597 unsigned int ata_sff_data_xfer32(struct ata_queued_cmd *qc, unsigned char *buf,
598 unsigned int buflen, int rw)
599 {
600 struct ata_device *dev = qc->dev;
601 struct ata_port *ap = dev->link->ap;
602 void __iomem *data_addr = ap->ioaddr.data_addr;
603 unsigned int words = buflen >> 2;
604 int slop = buflen & 3;
605
606 if (!(ap->pflags & ATA_PFLAG_PIO32))
607 return ata_sff_data_xfer(qc, buf, buflen, rw);
608
609
610 if (rw == READ)
611 ioread32_rep(data_addr, buf, words);
612 else
613 iowrite32_rep(data_addr, buf, words);
614
615
616 if (unlikely(slop)) {
617 unsigned char pad[4] = { };
618
619
620 buf += buflen - slop;
621
622
623
624
625
626 if (rw == READ) {
627 if (slop < 3)
628 ioread16_rep(data_addr, pad, 1);
629 else
630 ioread32_rep(data_addr, pad, 1);
631 memcpy(buf, pad, slop);
632 } else {
633 memcpy(pad, buf, slop);
634 if (slop < 3)
635 iowrite16_rep(data_addr, pad, 1);
636 else
637 iowrite32_rep(data_addr, pad, 1);
638 }
639 }
640 return (buflen + 1) & ~1;
641 }
642 EXPORT_SYMBOL_GPL(ata_sff_data_xfer32);
643
644
645
646
647
648
649
650
651
652
653 static void ata_pio_sector(struct ata_queued_cmd *qc)
654 {
655 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
656 struct ata_port *ap = qc->ap;
657 struct page *page;
658 unsigned int offset;
659 unsigned char *buf;
660
661 if (!qc->cursg) {
662 qc->curbytes = qc->nbytes;
663 return;
664 }
665 if (qc->curbytes == qc->nbytes - qc->sect_size)
666 ap->hsm_task_state = HSM_ST_LAST;
667
668 page = sg_page(qc->cursg);
669 offset = qc->cursg->offset + qc->cursg_ofs;
670
671
672 page = nth_page(page, (offset >> PAGE_SHIFT));
673 offset %= PAGE_SIZE;
674
675 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
676
677
678 buf = kmap_atomic(page);
679 ap->ops->sff_data_xfer(qc, buf + offset, qc->sect_size, do_write);
680 kunmap_atomic(buf);
681
682 if (!do_write && !PageSlab(page))
683 flush_dcache_page(page);
684
685 qc->curbytes += qc->sect_size;
686 qc->cursg_ofs += qc->sect_size;
687
688 if (qc->cursg_ofs == qc->cursg->length) {
689 qc->cursg = sg_next(qc->cursg);
690 if (!qc->cursg)
691 ap->hsm_task_state = HSM_ST_LAST;
692 qc->cursg_ofs = 0;
693 }
694 }
695
696
697
698
699
700
701
702
703
704
705
706 static void ata_pio_sectors(struct ata_queued_cmd *qc)
707 {
708 if (is_multi_taskfile(&qc->tf)) {
709
710 unsigned int nsect;
711
712 WARN_ON_ONCE(qc->dev->multi_count == 0);
713
714 nsect = min((qc->nbytes - qc->curbytes) / qc->sect_size,
715 qc->dev->multi_count);
716 while (nsect--)
717 ata_pio_sector(qc);
718 } else
719 ata_pio_sector(qc);
720
721 ata_sff_sync(qc->ap);
722 }
723
724
725
726
727
728
729
730
731
732
733
734
735 static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
736 {
737
738 DPRINTK("send cdb\n");
739 WARN_ON_ONCE(qc->dev->cdb_len < 12);
740
741 ap->ops->sff_data_xfer(qc, qc->cdb, qc->dev->cdb_len, 1);
742 ata_sff_sync(ap);
743
744
745 switch (qc->tf.protocol) {
746 case ATAPI_PROT_PIO:
747 ap->hsm_task_state = HSM_ST;
748 break;
749 case ATAPI_PROT_NODATA:
750 ap->hsm_task_state = HSM_ST_LAST;
751 break;
752 #ifdef CONFIG_ATA_BMDMA
753 case ATAPI_PROT_DMA:
754 ap->hsm_task_state = HSM_ST_LAST;
755
756 ap->ops->bmdma_start(qc);
757 break;
758 #endif
759 default:
760 BUG();
761 }
762 }
763
764
765
766
767
768
769
770
771
772
773
774
775 static int __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
776 {
777 int rw = (qc->tf.flags & ATA_TFLAG_WRITE) ? WRITE : READ;
778 struct ata_port *ap = qc->ap;
779 struct ata_device *dev = qc->dev;
780 struct ata_eh_info *ehi = &dev->link->eh_info;
781 struct scatterlist *sg;
782 struct page *page;
783 unsigned char *buf;
784 unsigned int offset, count, consumed;
785
786 next_sg:
787 sg = qc->cursg;
788 if (unlikely(!sg)) {
789 ata_ehi_push_desc(ehi, "unexpected or too much trailing data "
790 "buf=%u cur=%u bytes=%u",
791 qc->nbytes, qc->curbytes, bytes);
792 return -1;
793 }
794
795 page = sg_page(sg);
796 offset = sg->offset + qc->cursg_ofs;
797
798
799 page = nth_page(page, (offset >> PAGE_SHIFT));
800 offset %= PAGE_SIZE;
801
802
803 count = min(sg->length - qc->cursg_ofs, bytes);
804
805
806 count = min(count, (unsigned int)PAGE_SIZE - offset);
807
808 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
809
810
811 buf = kmap_atomic(page);
812 consumed = ap->ops->sff_data_xfer(qc, buf + offset, count, rw);
813 kunmap_atomic(buf);
814
815 bytes -= min(bytes, consumed);
816 qc->curbytes += count;
817 qc->cursg_ofs += count;
818
819 if (qc->cursg_ofs == sg->length) {
820 qc->cursg = sg_next(qc->cursg);
821 qc->cursg_ofs = 0;
822 }
823
824
825
826
827
828
829
830 if (bytes)
831 goto next_sg;
832 return 0;
833 }
834
835
836
837
838
839
840
841
842
843
844 static void atapi_pio_bytes(struct ata_queued_cmd *qc)
845 {
846 struct ata_port *ap = qc->ap;
847 struct ata_device *dev = qc->dev;
848 struct ata_eh_info *ehi = &dev->link->eh_info;
849 unsigned int ireason, bc_lo, bc_hi, bytes;
850 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
851
852
853
854
855
856
857
858 ap->ops->sff_tf_read(ap, &qc->result_tf);
859 ireason = qc->result_tf.nsect;
860 bc_lo = qc->result_tf.lbam;
861 bc_hi = qc->result_tf.lbah;
862 bytes = (bc_hi << 8) | bc_lo;
863
864
865 if (unlikely(ireason & ATAPI_COD))
866 goto atapi_check;
867
868
869 i_write = ((ireason & ATAPI_IO) == 0) ? 1 : 0;
870 if (unlikely(do_write != i_write))
871 goto atapi_check;
872
873 if (unlikely(!bytes))
874 goto atapi_check;
875
876 VPRINTK("ata%u: xfering %d bytes\n", ap->print_id, bytes);
877
878 if (unlikely(__atapi_pio_bytes(qc, bytes)))
879 goto err_out;
880 ata_sff_sync(ap);
881
882 return;
883
884 atapi_check:
885 ata_ehi_push_desc(ehi, "ATAPI check failed (ireason=0x%x bytes=%u)",
886 ireason, bytes);
887 err_out:
888 qc->err_mask |= AC_ERR_HSM;
889 ap->hsm_task_state = HSM_ST_ERR;
890 }
891
892
893
894
895
896
897
898
899
900 static inline int ata_hsm_ok_in_wq(struct ata_port *ap,
901 struct ata_queued_cmd *qc)
902 {
903 if (qc->tf.flags & ATA_TFLAG_POLLING)
904 return 1;
905
906 if (ap->hsm_task_state == HSM_ST_FIRST) {
907 if (qc->tf.protocol == ATA_PROT_PIO &&
908 (qc->tf.flags & ATA_TFLAG_WRITE))
909 return 1;
910
911 if (ata_is_atapi(qc->tf.protocol) &&
912 !(qc->dev->flags & ATA_DFLAG_CDB_INTR))
913 return 1;
914 }
915
916 return 0;
917 }
918
919
920
921
922
923
924
925
926
927
928
929
930 static void ata_hsm_qc_complete(struct ata_queued_cmd *qc, int in_wq)
931 {
932 struct ata_port *ap = qc->ap;
933
934 if (ap->ops->error_handler) {
935 if (in_wq) {
936
937
938
939 qc = ata_qc_from_tag(ap, qc->tag);
940 if (qc) {
941 if (likely(!(qc->err_mask & AC_ERR_HSM))) {
942 ata_sff_irq_on(ap);
943 ata_qc_complete(qc);
944 } else
945 ata_port_freeze(ap);
946 }
947 } else {
948 if (likely(!(qc->err_mask & AC_ERR_HSM)))
949 ata_qc_complete(qc);
950 else
951 ata_port_freeze(ap);
952 }
953 } else {
954 if (in_wq) {
955 ata_sff_irq_on(ap);
956 ata_qc_complete(qc);
957 } else
958 ata_qc_complete(qc);
959 }
960 }
961
962
963
964
965
966
967
968
969
970
971
972 int ata_sff_hsm_move(struct ata_port *ap, struct ata_queued_cmd *qc,
973 u8 status, int in_wq)
974 {
975 struct ata_link *link = qc->dev->link;
976 struct ata_eh_info *ehi = &link->eh_info;
977 int poll_next;
978
979 lockdep_assert_held(ap->lock);
980
981 WARN_ON_ONCE((qc->flags & ATA_QCFLAG_ACTIVE) == 0);
982
983
984
985
986
987 WARN_ON_ONCE(in_wq != ata_hsm_ok_in_wq(ap, qc));
988
989 fsm_start:
990 DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
991 ap->print_id, qc->tf.protocol, ap->hsm_task_state, status);
992
993 switch (ap->hsm_task_state) {
994 case HSM_ST_FIRST:
995
996
997
998
999
1000
1001 poll_next = (qc->tf.flags & ATA_TFLAG_POLLING);
1002
1003
1004 if (unlikely((status & ATA_DRQ) == 0)) {
1005
1006 if (likely(status & (ATA_ERR | ATA_DF)))
1007
1008 qc->err_mask |= AC_ERR_DEV;
1009 else {
1010
1011 ata_ehi_push_desc(ehi,
1012 "ST_FIRST: !(DRQ|ERR|DF)");
1013 qc->err_mask |= AC_ERR_HSM;
1014 }
1015
1016 ap->hsm_task_state = HSM_ST_ERR;
1017 goto fsm_start;
1018 }
1019
1020
1021
1022
1023
1024
1025
1026 if (unlikely(status & (ATA_ERR | ATA_DF))) {
1027
1028
1029
1030
1031
1032 if (!(qc->dev->horkage & ATA_HORKAGE_STUCK_ERR)) {
1033 ata_ehi_push_desc(ehi, "ST_FIRST: "
1034 "DRQ=1 with device error, "
1035 "dev_stat 0x%X", status);
1036 qc->err_mask |= AC_ERR_HSM;
1037 ap->hsm_task_state = HSM_ST_ERR;
1038 goto fsm_start;
1039 }
1040 }
1041
1042 if (qc->tf.protocol == ATA_PROT_PIO) {
1043
1044
1045
1046
1047
1048
1049
1050
1051 ap->hsm_task_state = HSM_ST;
1052 ata_pio_sectors(qc);
1053 } else
1054
1055 atapi_send_cdb(ap, qc);
1056
1057
1058
1059
1060 break;
1061
1062 case HSM_ST:
1063
1064 if (qc->tf.protocol == ATAPI_PROT_PIO) {
1065
1066 if ((status & ATA_DRQ) == 0) {
1067
1068
1069
1070 ap->hsm_task_state = HSM_ST_LAST;
1071 goto fsm_start;
1072 }
1073
1074
1075
1076
1077
1078
1079
1080 if (unlikely(status & (ATA_ERR | ATA_DF))) {
1081 ata_ehi_push_desc(ehi, "ST-ATAPI: "
1082 "DRQ=1 with device error, "
1083 "dev_stat 0x%X", status);
1084 qc->err_mask |= AC_ERR_HSM;
1085 ap->hsm_task_state = HSM_ST_ERR;
1086 goto fsm_start;
1087 }
1088
1089 atapi_pio_bytes(qc);
1090
1091 if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
1092
1093 goto fsm_start;
1094
1095 } else {
1096
1097 if (unlikely((status & ATA_DRQ) == 0)) {
1098
1099 if (likely(status & (ATA_ERR | ATA_DF))) {
1100
1101 qc->err_mask |= AC_ERR_DEV;
1102
1103
1104
1105
1106
1107 if (qc->dev->horkage &
1108 ATA_HORKAGE_DIAGNOSTIC)
1109 qc->err_mask |=
1110 AC_ERR_NODEV_HINT;
1111 } else {
1112
1113
1114
1115
1116 ata_ehi_push_desc(ehi, "ST-ATA: "
1117 "DRQ=0 without device error, "
1118 "dev_stat 0x%X", status);
1119 qc->err_mask |= AC_ERR_HSM |
1120 AC_ERR_NODEV_HINT;
1121 }
1122
1123 ap->hsm_task_state = HSM_ST_ERR;
1124 goto fsm_start;
1125 }
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137 if (unlikely(status & (ATA_ERR | ATA_DF))) {
1138
1139 qc->err_mask |= AC_ERR_DEV;
1140
1141 if (!(qc->tf.flags & ATA_TFLAG_WRITE)) {
1142 ata_pio_sectors(qc);
1143 status = ata_wait_idle(ap);
1144 }
1145
1146 if (status & (ATA_BUSY | ATA_DRQ)) {
1147 ata_ehi_push_desc(ehi, "ST-ATA: "
1148 "BUSY|DRQ persists on ERR|DF, "
1149 "dev_stat 0x%X", status);
1150 qc->err_mask |= AC_ERR_HSM;
1151 }
1152
1153
1154
1155
1156
1157
1158
1159
1160 if (status == 0x7f)
1161 qc->err_mask |= AC_ERR_NODEV_HINT;
1162
1163
1164
1165
1166
1167 ap->hsm_task_state = HSM_ST_ERR;
1168 goto fsm_start;
1169 }
1170
1171 ata_pio_sectors(qc);
1172
1173 if (ap->hsm_task_state == HSM_ST_LAST &&
1174 (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
1175
1176 status = ata_wait_idle(ap);
1177 goto fsm_start;
1178 }
1179 }
1180
1181 poll_next = 1;
1182 break;
1183
1184 case HSM_ST_LAST:
1185 if (unlikely(!ata_ok(status))) {
1186 qc->err_mask |= __ac_err_mask(status);
1187 ap->hsm_task_state = HSM_ST_ERR;
1188 goto fsm_start;
1189 }
1190
1191
1192 DPRINTK("ata%u: dev %u command complete, drv_stat 0x%x\n",
1193 ap->print_id, qc->dev->devno, status);
1194
1195 WARN_ON_ONCE(qc->err_mask & (AC_ERR_DEV | AC_ERR_HSM));
1196
1197 ap->hsm_task_state = HSM_ST_IDLE;
1198
1199
1200 ata_hsm_qc_complete(qc, in_wq);
1201
1202 poll_next = 0;
1203 break;
1204
1205 case HSM_ST_ERR:
1206 ap->hsm_task_state = HSM_ST_IDLE;
1207
1208
1209 ata_hsm_qc_complete(qc, in_wq);
1210
1211 poll_next = 0;
1212 break;
1213 default:
1214 poll_next = 0;
1215 WARN(true, "ata%d: SFF host state machine in invalid state %d",
1216 ap->print_id, ap->hsm_task_state);
1217 }
1218
1219 return poll_next;
1220 }
1221 EXPORT_SYMBOL_GPL(ata_sff_hsm_move);
1222
1223 void ata_sff_queue_work(struct work_struct *work)
1224 {
1225 queue_work(ata_sff_wq, work);
1226 }
1227 EXPORT_SYMBOL_GPL(ata_sff_queue_work);
1228
1229 void ata_sff_queue_delayed_work(struct delayed_work *dwork, unsigned long delay)
1230 {
1231 queue_delayed_work(ata_sff_wq, dwork, delay);
1232 }
1233 EXPORT_SYMBOL_GPL(ata_sff_queue_delayed_work);
1234
1235 void ata_sff_queue_pio_task(struct ata_link *link, unsigned long delay)
1236 {
1237 struct ata_port *ap = link->ap;
1238
1239 WARN_ON((ap->sff_pio_task_link != NULL) &&
1240 (ap->sff_pio_task_link != link));
1241 ap->sff_pio_task_link = link;
1242
1243
1244 ata_sff_queue_delayed_work(&ap->sff_pio_task, msecs_to_jiffies(delay));
1245 }
1246 EXPORT_SYMBOL_GPL(ata_sff_queue_pio_task);
1247
1248 void ata_sff_flush_pio_task(struct ata_port *ap)
1249 {
1250 DPRINTK("ENTER\n");
1251
1252 cancel_delayed_work_sync(&ap->sff_pio_task);
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262 spin_lock_irq(ap->lock);
1263 ap->hsm_task_state = HSM_ST_IDLE;
1264 spin_unlock_irq(ap->lock);
1265
1266 ap->sff_pio_task_link = NULL;
1267
1268 if (ata_msg_ctl(ap))
1269 ata_port_dbg(ap, "%s: EXIT\n", __func__);
1270 }
1271
1272 static void ata_sff_pio_task(struct work_struct *work)
1273 {
1274 struct ata_port *ap =
1275 container_of(work, struct ata_port, sff_pio_task.work);
1276 struct ata_link *link = ap->sff_pio_task_link;
1277 struct ata_queued_cmd *qc;
1278 u8 status;
1279 int poll_next;
1280
1281 spin_lock_irq(ap->lock);
1282
1283 BUG_ON(ap->sff_pio_task_link == NULL);
1284
1285 qc = ata_qc_from_tag(ap, link->active_tag);
1286 if (!qc) {
1287 ap->sff_pio_task_link = NULL;
1288 goto out_unlock;
1289 }
1290
1291 fsm_start:
1292 WARN_ON_ONCE(ap->hsm_task_state == HSM_ST_IDLE);
1293
1294
1295
1296
1297
1298
1299
1300
1301 status = ata_sff_busy_wait(ap, ATA_BUSY, 5);
1302 if (status & ATA_BUSY) {
1303 spin_unlock_irq(ap->lock);
1304 ata_msleep(ap, 2);
1305 spin_lock_irq(ap->lock);
1306
1307 status = ata_sff_busy_wait(ap, ATA_BUSY, 10);
1308 if (status & ATA_BUSY) {
1309 ata_sff_queue_pio_task(link, ATA_SHORT_PAUSE);
1310 goto out_unlock;
1311 }
1312 }
1313
1314
1315
1316
1317
1318 ap->sff_pio_task_link = NULL;
1319
1320 poll_next = ata_sff_hsm_move(ap, qc, status, 1);
1321
1322
1323
1324
1325 if (poll_next)
1326 goto fsm_start;
1327 out_unlock:
1328 spin_unlock_irq(ap->lock);
1329 }
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344 unsigned int ata_sff_qc_issue(struct ata_queued_cmd *qc)
1345 {
1346 struct ata_port *ap = qc->ap;
1347 struct ata_link *link = qc->dev->link;
1348
1349
1350
1351
1352 if (ap->flags & ATA_FLAG_PIO_POLLING)
1353 qc->tf.flags |= ATA_TFLAG_POLLING;
1354
1355
1356 ata_dev_select(ap, qc->dev->devno, 1, 0);
1357
1358
1359 switch (qc->tf.protocol) {
1360 case ATA_PROT_NODATA:
1361 if (qc->tf.flags & ATA_TFLAG_POLLING)
1362 ata_qc_set_polling(qc);
1363
1364 ata_tf_to_host(ap, &qc->tf);
1365 ap->hsm_task_state = HSM_ST_LAST;
1366
1367 if (qc->tf.flags & ATA_TFLAG_POLLING)
1368 ata_sff_queue_pio_task(link, 0);
1369
1370 break;
1371
1372 case ATA_PROT_PIO:
1373 if (qc->tf.flags & ATA_TFLAG_POLLING)
1374 ata_qc_set_polling(qc);
1375
1376 ata_tf_to_host(ap, &qc->tf);
1377
1378 if (qc->tf.flags & ATA_TFLAG_WRITE) {
1379
1380 ap->hsm_task_state = HSM_ST_FIRST;
1381 ata_sff_queue_pio_task(link, 0);
1382
1383
1384
1385
1386 } else {
1387
1388 ap->hsm_task_state = HSM_ST;
1389
1390 if (qc->tf.flags & ATA_TFLAG_POLLING)
1391 ata_sff_queue_pio_task(link, 0);
1392
1393
1394
1395
1396
1397 }
1398
1399 break;
1400
1401 case ATAPI_PROT_PIO:
1402 case ATAPI_PROT_NODATA:
1403 if (qc->tf.flags & ATA_TFLAG_POLLING)
1404 ata_qc_set_polling(qc);
1405
1406 ata_tf_to_host(ap, &qc->tf);
1407
1408 ap->hsm_task_state = HSM_ST_FIRST;
1409
1410
1411 if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
1412 (qc->tf.flags & ATA_TFLAG_POLLING))
1413 ata_sff_queue_pio_task(link, 0);
1414 break;
1415
1416 default:
1417 return AC_ERR_SYSTEM;
1418 }
1419
1420 return 0;
1421 }
1422 EXPORT_SYMBOL_GPL(ata_sff_qc_issue);
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437 bool ata_sff_qc_fill_rtf(struct ata_queued_cmd *qc)
1438 {
1439 qc->ap->ops->sff_tf_read(qc->ap, &qc->result_tf);
1440 return true;
1441 }
1442 EXPORT_SYMBOL_GPL(ata_sff_qc_fill_rtf);
1443
1444 static unsigned int ata_sff_idle_irq(struct ata_port *ap)
1445 {
1446 ap->stats.idle_irq++;
1447
1448 #ifdef ATA_IRQ_TRAP
1449 if ((ap->stats.idle_irq % 1000) == 0) {
1450 ap->ops->sff_check_status(ap);
1451 if (ap->ops->sff_irq_clear)
1452 ap->ops->sff_irq_clear(ap);
1453 ata_port_warn(ap, "irq trap\n");
1454 return 1;
1455 }
1456 #endif
1457 return 0;
1458 }
1459
1460 static unsigned int __ata_sff_port_intr(struct ata_port *ap,
1461 struct ata_queued_cmd *qc,
1462 bool hsmv_on_idle)
1463 {
1464 u8 status;
1465
1466 VPRINTK("ata%u: protocol %d task_state %d\n",
1467 ap->print_id, qc->tf.protocol, ap->hsm_task_state);
1468
1469
1470 switch (ap->hsm_task_state) {
1471 case HSM_ST_FIRST:
1472
1473
1474
1475
1476
1477
1478
1479
1480 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
1481 return ata_sff_idle_irq(ap);
1482 break;
1483 case HSM_ST_IDLE:
1484 return ata_sff_idle_irq(ap);
1485 default:
1486 break;
1487 }
1488
1489
1490 status = ata_sff_irq_status(ap);
1491 if (status & ATA_BUSY) {
1492 if (hsmv_on_idle) {
1493
1494 qc->err_mask |= AC_ERR_HSM;
1495 ap->hsm_task_state = HSM_ST_ERR;
1496 } else
1497 return ata_sff_idle_irq(ap);
1498 }
1499
1500
1501 if (ap->ops->sff_irq_clear)
1502 ap->ops->sff_irq_clear(ap);
1503
1504 ata_sff_hsm_move(ap, qc, status, 0);
1505
1506 return 1;
1507 }
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522 unsigned int ata_sff_port_intr(struct ata_port *ap, struct ata_queued_cmd *qc)
1523 {
1524 return __ata_sff_port_intr(ap, qc, false);
1525 }
1526 EXPORT_SYMBOL_GPL(ata_sff_port_intr);
1527
1528 static inline irqreturn_t __ata_sff_interrupt(int irq, void *dev_instance,
1529 unsigned int (*port_intr)(struct ata_port *, struct ata_queued_cmd *))
1530 {
1531 struct ata_host *host = dev_instance;
1532 bool retried = false;
1533 unsigned int i;
1534 unsigned int handled, idle, polling;
1535 unsigned long flags;
1536
1537
1538 spin_lock_irqsave(&host->lock, flags);
1539
1540 retry:
1541 handled = idle = polling = 0;
1542 for (i = 0; i < host->n_ports; i++) {
1543 struct ata_port *ap = host->ports[i];
1544 struct ata_queued_cmd *qc;
1545
1546 qc = ata_qc_from_tag(ap, ap->link.active_tag);
1547 if (qc) {
1548 if (!(qc->tf.flags & ATA_TFLAG_POLLING))
1549 handled |= port_intr(ap, qc);
1550 else
1551 polling |= 1 << i;
1552 } else
1553 idle |= 1 << i;
1554 }
1555
1556
1557
1558
1559
1560
1561 if (!handled && !retried) {
1562 bool retry = false;
1563
1564 for (i = 0; i < host->n_ports; i++) {
1565 struct ata_port *ap = host->ports[i];
1566
1567 if (polling & (1 << i))
1568 continue;
1569
1570 if (!ap->ops->sff_irq_check ||
1571 !ap->ops->sff_irq_check(ap))
1572 continue;
1573
1574 if (idle & (1 << i)) {
1575 ap->ops->sff_check_status(ap);
1576 if (ap->ops->sff_irq_clear)
1577 ap->ops->sff_irq_clear(ap);
1578 } else {
1579
1580 if (!(ap->ops->sff_check_status(ap) & ATA_BUSY))
1581 retry |= true;
1582
1583
1584
1585
1586 }
1587 }
1588
1589 if (retry) {
1590 retried = true;
1591 goto retry;
1592 }
1593 }
1594
1595 spin_unlock_irqrestore(&host->lock, flags);
1596
1597 return IRQ_RETVAL(handled);
1598 }
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614 irqreturn_t ata_sff_interrupt(int irq, void *dev_instance)
1615 {
1616 return __ata_sff_interrupt(irq, dev_instance, ata_sff_port_intr);
1617 }
1618 EXPORT_SYMBOL_GPL(ata_sff_interrupt);
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633 void ata_sff_lost_interrupt(struct ata_port *ap)
1634 {
1635 u8 status;
1636 struct ata_queued_cmd *qc;
1637
1638
1639 qc = ata_qc_from_tag(ap, ap->link.active_tag);
1640
1641 if (!qc || qc->tf.flags & ATA_TFLAG_POLLING)
1642 return;
1643
1644
1645 status = ata_sff_altstatus(ap);
1646 if (status & ATA_BUSY)
1647 return;
1648
1649
1650
1651 ata_port_warn(ap, "lost interrupt (Status 0x%x)\n",
1652 status);
1653
1654
1655 ata_sff_port_intr(ap, qc);
1656 }
1657 EXPORT_SYMBOL_GPL(ata_sff_lost_interrupt);
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668 void ata_sff_freeze(struct ata_port *ap)
1669 {
1670 ap->ctl |= ATA_NIEN;
1671 ap->last_ctl = ap->ctl;
1672
1673 if (ap->ops->sff_set_devctl || ap->ioaddr.ctl_addr)
1674 ata_sff_set_devctl(ap, ap->ctl);
1675
1676
1677
1678
1679
1680 ap->ops->sff_check_status(ap);
1681
1682 if (ap->ops->sff_irq_clear)
1683 ap->ops->sff_irq_clear(ap);
1684 }
1685 EXPORT_SYMBOL_GPL(ata_sff_freeze);
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696 void ata_sff_thaw(struct ata_port *ap)
1697 {
1698
1699 ap->ops->sff_check_status(ap);
1700 if (ap->ops->sff_irq_clear)
1701 ap->ops->sff_irq_clear(ap);
1702 ata_sff_irq_on(ap);
1703 }
1704 EXPORT_SYMBOL_GPL(ata_sff_thaw);
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721 int ata_sff_prereset(struct ata_link *link, unsigned long deadline)
1722 {
1723 struct ata_eh_context *ehc = &link->eh_context;
1724 int rc;
1725
1726 rc = ata_std_prereset(link, deadline);
1727 if (rc)
1728 return rc;
1729
1730
1731 if (ehc->i.action & ATA_EH_HARDRESET)
1732 return 0;
1733
1734
1735 if (!ata_link_offline(link)) {
1736 rc = ata_sff_wait_ready(link, deadline);
1737 if (rc && rc != -ENODEV) {
1738 ata_link_warn(link,
1739 "device not ready (errno=%d), forcing hardreset\n",
1740 rc);
1741 ehc->i.action |= ATA_EH_HARDRESET;
1742 }
1743 }
1744
1745 return 0;
1746 }
1747 EXPORT_SYMBOL_GPL(ata_sff_prereset);
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766 static unsigned int ata_devchk(struct ata_port *ap, unsigned int device)
1767 {
1768 struct ata_ioports *ioaddr = &ap->ioaddr;
1769 u8 nsect, lbal;
1770
1771 ap->ops->sff_dev_select(ap, device);
1772
1773 iowrite8(0x55, ioaddr->nsect_addr);
1774 iowrite8(0xaa, ioaddr->lbal_addr);
1775
1776 iowrite8(0xaa, ioaddr->nsect_addr);
1777 iowrite8(0x55, ioaddr->lbal_addr);
1778
1779 iowrite8(0x55, ioaddr->nsect_addr);
1780 iowrite8(0xaa, ioaddr->lbal_addr);
1781
1782 nsect = ioread8(ioaddr->nsect_addr);
1783 lbal = ioread8(ioaddr->lbal_addr);
1784
1785 if ((nsect == 0x55) && (lbal == 0xaa))
1786 return 1;
1787
1788 return 0;
1789 }
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812 unsigned int ata_sff_dev_classify(struct ata_device *dev, int present,
1813 u8 *r_err)
1814 {
1815 struct ata_port *ap = dev->link->ap;
1816 struct ata_taskfile tf;
1817 unsigned int class;
1818 u8 err;
1819
1820 ap->ops->sff_dev_select(ap, dev->devno);
1821
1822 memset(&tf, 0, sizeof(tf));
1823
1824 ap->ops->sff_tf_read(ap, &tf);
1825 err = tf.feature;
1826 if (r_err)
1827 *r_err = err;
1828
1829
1830 if (err == 0)
1831
1832 dev->horkage |= ATA_HORKAGE_DIAGNOSTIC;
1833 else if (err == 1)
1834 ;
1835 else if ((dev->devno == 0) && (err == 0x81))
1836 ;
1837 else
1838 return ATA_DEV_NONE;
1839
1840
1841 class = ata_dev_classify(&tf);
1842
1843 if (class == ATA_DEV_UNKNOWN) {
1844
1845
1846
1847
1848
1849
1850 if (present && (dev->horkage & ATA_HORKAGE_DIAGNOSTIC))
1851 class = ATA_DEV_ATA;
1852 else
1853 class = ATA_DEV_NONE;
1854 } else if ((class == ATA_DEV_ATA) &&
1855 (ap->ops->sff_check_status(ap) == 0))
1856 class = ATA_DEV_NONE;
1857
1858 return class;
1859 }
1860 EXPORT_SYMBOL_GPL(ata_sff_dev_classify);
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879 int ata_sff_wait_after_reset(struct ata_link *link, unsigned int devmask,
1880 unsigned long deadline)
1881 {
1882 struct ata_port *ap = link->ap;
1883 struct ata_ioports *ioaddr = &ap->ioaddr;
1884 unsigned int dev0 = devmask & (1 << 0);
1885 unsigned int dev1 = devmask & (1 << 1);
1886 int rc, ret = 0;
1887
1888 ata_msleep(ap, ATA_WAIT_AFTER_RESET);
1889
1890
1891 rc = ata_sff_wait_ready(link, deadline);
1892
1893
1894
1895 if (rc)
1896 return rc;
1897
1898
1899
1900
1901 if (dev1) {
1902 int i;
1903
1904 ap->ops->sff_dev_select(ap, 1);
1905
1906
1907
1908
1909
1910 for (i = 0; i < 2; i++) {
1911 u8 nsect, lbal;
1912
1913 nsect = ioread8(ioaddr->nsect_addr);
1914 lbal = ioread8(ioaddr->lbal_addr);
1915 if ((nsect == 1) && (lbal == 1))
1916 break;
1917 ata_msleep(ap, 50);
1918 }
1919
1920 rc = ata_sff_wait_ready(link, deadline);
1921 if (rc) {
1922 if (rc != -ENODEV)
1923 return rc;
1924 ret = rc;
1925 }
1926 }
1927
1928
1929 ap->ops->sff_dev_select(ap, 0);
1930 if (dev1)
1931 ap->ops->sff_dev_select(ap, 1);
1932 if (dev0)
1933 ap->ops->sff_dev_select(ap, 0);
1934
1935 return ret;
1936 }
1937 EXPORT_SYMBOL_GPL(ata_sff_wait_after_reset);
1938
1939 static int ata_bus_softreset(struct ata_port *ap, unsigned int devmask,
1940 unsigned long deadline)
1941 {
1942 struct ata_ioports *ioaddr = &ap->ioaddr;
1943
1944 DPRINTK("ata%u: bus reset via SRST\n", ap->print_id);
1945
1946 if (ap->ioaddr.ctl_addr) {
1947
1948 iowrite8(ap->ctl, ioaddr->ctl_addr);
1949 udelay(20);
1950 iowrite8(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1951 udelay(20);
1952 iowrite8(ap->ctl, ioaddr->ctl_addr);
1953 ap->last_ctl = ap->ctl;
1954 }
1955
1956
1957 return ata_sff_wait_after_reset(&ap->link, devmask, deadline);
1958 }
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974 int ata_sff_softreset(struct ata_link *link, unsigned int *classes,
1975 unsigned long deadline)
1976 {
1977 struct ata_port *ap = link->ap;
1978 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1979 unsigned int devmask = 0;
1980 int rc;
1981 u8 err;
1982
1983 DPRINTK("ENTER\n");
1984
1985
1986 if (ata_devchk(ap, 0))
1987 devmask |= (1 << 0);
1988 if (slave_possible && ata_devchk(ap, 1))
1989 devmask |= (1 << 1);
1990
1991
1992 ap->ops->sff_dev_select(ap, 0);
1993
1994
1995 DPRINTK("about to softreset, devmask=%x\n", devmask);
1996 rc = ata_bus_softreset(ap, devmask, deadline);
1997
1998 if (rc && (rc != -ENODEV || sata_scr_valid(link))) {
1999 ata_link_err(link, "SRST failed (errno=%d)\n", rc);
2000 return rc;
2001 }
2002
2003
2004 classes[0] = ata_sff_dev_classify(&link->device[0],
2005 devmask & (1 << 0), &err);
2006 if (slave_possible && err != 0x81)
2007 classes[1] = ata_sff_dev_classify(&link->device[1],
2008 devmask & (1 << 1), &err);
2009
2010 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
2011 return 0;
2012 }
2013 EXPORT_SYMBOL_GPL(ata_sff_softreset);
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030 int sata_sff_hardreset(struct ata_link *link, unsigned int *class,
2031 unsigned long deadline)
2032 {
2033 struct ata_eh_context *ehc = &link->eh_context;
2034 const unsigned long *timing = sata_ehc_deb_timing(ehc);
2035 bool online;
2036 int rc;
2037
2038 rc = sata_link_hardreset(link, timing, deadline, &online,
2039 ata_sff_check_ready);
2040 if (online)
2041 *class = ata_sff_dev_classify(link->device, 1, NULL);
2042
2043 DPRINTK("EXIT, class=%u\n", *class);
2044 return rc;
2045 }
2046 EXPORT_SYMBOL_GPL(sata_sff_hardreset);
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060 void ata_sff_postreset(struct ata_link *link, unsigned int *classes)
2061 {
2062 struct ata_port *ap = link->ap;
2063
2064 ata_std_postreset(link, classes);
2065
2066
2067 if (classes[0] != ATA_DEV_NONE)
2068 ap->ops->sff_dev_select(ap, 1);
2069 if (classes[1] != ATA_DEV_NONE)
2070 ap->ops->sff_dev_select(ap, 0);
2071
2072
2073 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
2074 DPRINTK("EXIT, no device\n");
2075 return;
2076 }
2077
2078
2079 if (ap->ops->sff_set_devctl || ap->ioaddr.ctl_addr) {
2080 ata_sff_set_devctl(ap, ap->ctl);
2081 ap->last_ctl = ap->ctl;
2082 }
2083 }
2084 EXPORT_SYMBOL_GPL(ata_sff_postreset);
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096 void ata_sff_drain_fifo(struct ata_queued_cmd *qc)
2097 {
2098 int count;
2099 struct ata_port *ap;
2100
2101
2102 if (qc == NULL || qc->dma_dir == DMA_TO_DEVICE)
2103 return;
2104
2105 ap = qc->ap;
2106
2107 for (count = 0; (ap->ops->sff_check_status(ap) & ATA_DRQ)
2108 && count < 65536; count += 2)
2109 ioread16(ap->ioaddr.data_addr);
2110
2111
2112 if (count)
2113 ata_port_dbg(ap, "drained %d bytes to clear DRQ\n", count);
2114
2115 }
2116 EXPORT_SYMBOL_GPL(ata_sff_drain_fifo);
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130 void ata_sff_error_handler(struct ata_port *ap)
2131 {
2132 ata_reset_fn_t softreset = ap->ops->softreset;
2133 ata_reset_fn_t hardreset = ap->ops->hardreset;
2134 struct ata_queued_cmd *qc;
2135 unsigned long flags;
2136
2137 qc = __ata_qc_from_tag(ap, ap->link.active_tag);
2138 if (qc && !(qc->flags & ATA_QCFLAG_FAILED))
2139 qc = NULL;
2140
2141 spin_lock_irqsave(ap->lock, flags);
2142
2143
2144
2145
2146
2147
2148
2149
2150 if (ap->ops->sff_drain_fifo)
2151 ap->ops->sff_drain_fifo(qc);
2152
2153 spin_unlock_irqrestore(ap->lock, flags);
2154
2155
2156 if ((hardreset == sata_std_hardreset ||
2157 hardreset == sata_sff_hardreset) && !sata_scr_valid(&ap->link))
2158 hardreset = NULL;
2159
2160 ata_do_eh(ap, ap->ops->prereset, softreset, hardreset,
2161 ap->ops->postreset);
2162 }
2163 EXPORT_SYMBOL_GPL(ata_sff_error_handler);
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176 void ata_sff_std_ports(struct ata_ioports *ioaddr)
2177 {
2178 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
2179 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
2180 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
2181 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
2182 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
2183 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
2184 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
2185 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
2186 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
2187 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
2188 }
2189 EXPORT_SYMBOL_GPL(ata_sff_std_ports);
2190
2191 #ifdef CONFIG_PCI
2192
2193 static int ata_resources_present(struct pci_dev *pdev, int port)
2194 {
2195 int i;
2196
2197
2198 port = port * 2;
2199 for (i = 0; i < 2; i++) {
2200 if (pci_resource_start(pdev, port + i) == 0 ||
2201 pci_resource_len(pdev, port + i) == 0)
2202 return 0;
2203 }
2204 return 1;
2205 }
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226 int ata_pci_sff_init_host(struct ata_host *host)
2227 {
2228 struct device *gdev = host->dev;
2229 struct pci_dev *pdev = to_pci_dev(gdev);
2230 unsigned int mask = 0;
2231 int i, rc;
2232
2233
2234 for (i = 0; i < 2; i++) {
2235 struct ata_port *ap = host->ports[i];
2236 int base = i * 2;
2237 void __iomem * const *iomap;
2238
2239 if (ata_port_is_dummy(ap))
2240 continue;
2241
2242
2243
2244
2245
2246 if (!ata_resources_present(pdev, i)) {
2247 ap->ops = &ata_dummy_port_ops;
2248 continue;
2249 }
2250
2251 rc = pcim_iomap_regions(pdev, 0x3 << base,
2252 dev_driver_string(gdev));
2253 if (rc) {
2254 dev_warn(gdev,
2255 "failed to request/iomap BARs for port %d (errno=%d)\n",
2256 i, rc);
2257 if (rc == -EBUSY)
2258 pcim_pin_device(pdev);
2259 ap->ops = &ata_dummy_port_ops;
2260 continue;
2261 }
2262 host->iomap = iomap = pcim_iomap_table(pdev);
2263
2264 ap->ioaddr.cmd_addr = iomap[base];
2265 ap->ioaddr.altstatus_addr =
2266 ap->ioaddr.ctl_addr = (void __iomem *)
2267 ((unsigned long)iomap[base + 1] | ATA_PCI_CTL_OFS);
2268 ata_sff_std_ports(&ap->ioaddr);
2269
2270 ata_port_desc(ap, "cmd 0x%llx ctl 0x%llx",
2271 (unsigned long long)pci_resource_start(pdev, base),
2272 (unsigned long long)pci_resource_start(pdev, base + 1));
2273
2274 mask |= 1 << i;
2275 }
2276
2277 if (!mask) {
2278 dev_err(gdev, "no available native port\n");
2279 return -ENODEV;
2280 }
2281
2282 return 0;
2283 }
2284 EXPORT_SYMBOL_GPL(ata_pci_sff_init_host);
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301 int ata_pci_sff_prepare_host(struct pci_dev *pdev,
2302 const struct ata_port_info * const *ppi,
2303 struct ata_host **r_host)
2304 {
2305 struct ata_host *host;
2306 int rc;
2307
2308 if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL))
2309 return -ENOMEM;
2310
2311 host = ata_host_alloc_pinfo(&pdev->dev, ppi, 2);
2312 if (!host) {
2313 dev_err(&pdev->dev, "failed to allocate ATA host\n");
2314 rc = -ENOMEM;
2315 goto err_out;
2316 }
2317
2318 rc = ata_pci_sff_init_host(host);
2319 if (rc)
2320 goto err_out;
2321
2322 devres_remove_group(&pdev->dev, NULL);
2323 *r_host = host;
2324 return 0;
2325
2326 err_out:
2327 devres_release_group(&pdev->dev, NULL);
2328 return rc;
2329 }
2330 EXPORT_SYMBOL_GPL(ata_pci_sff_prepare_host);
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348 int ata_pci_sff_activate_host(struct ata_host *host,
2349 irq_handler_t irq_handler,
2350 struct scsi_host_template *sht)
2351 {
2352 struct device *dev = host->dev;
2353 struct pci_dev *pdev = to_pci_dev(dev);
2354 const char *drv_name = dev_driver_string(host->dev);
2355 int legacy_mode = 0, rc;
2356
2357 rc = ata_host_start(host);
2358 if (rc)
2359 return rc;
2360
2361 if ((pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
2362 u8 tmp8, mask = 0;
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
2373 if (!ata_port_is_dummy(host->ports[0]))
2374 mask |= (1 << 0);
2375 if (!ata_port_is_dummy(host->ports[1]))
2376 mask |= (1 << 2);
2377 if ((tmp8 & mask) != mask)
2378 legacy_mode = 1;
2379 }
2380
2381 if (!devres_open_group(dev, NULL, GFP_KERNEL))
2382 return -ENOMEM;
2383
2384 if (!legacy_mode && pdev->irq) {
2385 int i;
2386
2387 rc = devm_request_irq(dev, pdev->irq, irq_handler,
2388 IRQF_SHARED, drv_name, host);
2389 if (rc)
2390 goto out;
2391
2392 for (i = 0; i < 2; i++) {
2393 if (ata_port_is_dummy(host->ports[i]))
2394 continue;
2395 ata_port_desc(host->ports[i], "irq %d", pdev->irq);
2396 }
2397 } else if (legacy_mode) {
2398 if (!ata_port_is_dummy(host->ports[0])) {
2399 rc = devm_request_irq(dev, ATA_PRIMARY_IRQ(pdev),
2400 irq_handler, IRQF_SHARED,
2401 drv_name, host);
2402 if (rc)
2403 goto out;
2404
2405 ata_port_desc(host->ports[0], "irq %d",
2406 ATA_PRIMARY_IRQ(pdev));
2407 }
2408
2409 if (!ata_port_is_dummy(host->ports[1])) {
2410 rc = devm_request_irq(dev, ATA_SECONDARY_IRQ(pdev),
2411 irq_handler, IRQF_SHARED,
2412 drv_name, host);
2413 if (rc)
2414 goto out;
2415
2416 ata_port_desc(host->ports[1], "irq %d",
2417 ATA_SECONDARY_IRQ(pdev));
2418 }
2419 }
2420
2421 rc = ata_host_register(host, sht);
2422 out:
2423 if (rc == 0)
2424 devres_remove_group(dev, NULL);
2425 else
2426 devres_release_group(dev, NULL);
2427
2428 return rc;
2429 }
2430 EXPORT_SYMBOL_GPL(ata_pci_sff_activate_host);
2431
2432 static const struct ata_port_info *ata_sff_find_valid_pi(
2433 const struct ata_port_info * const *ppi)
2434 {
2435 int i;
2436
2437
2438 for (i = 0; i < 2 && ppi[i]; i++)
2439 if (ppi[i]->port_ops != &ata_dummy_port_ops)
2440 return ppi[i];
2441
2442 return NULL;
2443 }
2444
2445 static int ata_pci_init_one(struct pci_dev *pdev,
2446 const struct ata_port_info * const *ppi,
2447 struct scsi_host_template *sht, void *host_priv,
2448 int hflags, bool bmdma)
2449 {
2450 struct device *dev = &pdev->dev;
2451 const struct ata_port_info *pi;
2452 struct ata_host *host = NULL;
2453 int rc;
2454
2455 DPRINTK("ENTER\n");
2456
2457 pi = ata_sff_find_valid_pi(ppi);
2458 if (!pi) {
2459 dev_err(&pdev->dev, "no valid port_info specified\n");
2460 return -EINVAL;
2461 }
2462
2463 if (!devres_open_group(dev, NULL, GFP_KERNEL))
2464 return -ENOMEM;
2465
2466 rc = pcim_enable_device(pdev);
2467 if (rc)
2468 goto out;
2469
2470 #ifdef CONFIG_ATA_BMDMA
2471 if (bmdma)
2472
2473 rc = ata_pci_bmdma_prepare_host(pdev, ppi, &host);
2474 else
2475 #endif
2476
2477 rc = ata_pci_sff_prepare_host(pdev, ppi, &host);
2478 if (rc)
2479 goto out;
2480 host->private_data = host_priv;
2481 host->flags |= hflags;
2482
2483 #ifdef CONFIG_ATA_BMDMA
2484 if (bmdma) {
2485 pci_set_master(pdev);
2486 rc = ata_pci_sff_activate_host(host, ata_bmdma_interrupt, sht);
2487 } else
2488 #endif
2489 rc = ata_pci_sff_activate_host(host, ata_sff_interrupt, sht);
2490 out:
2491 if (rc == 0)
2492 devres_remove_group(&pdev->dev, NULL);
2493 else
2494 devres_release_group(&pdev->dev, NULL);
2495
2496 return rc;
2497 }
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521 int ata_pci_sff_init_one(struct pci_dev *pdev,
2522 const struct ata_port_info * const *ppi,
2523 struct scsi_host_template *sht, void *host_priv, int hflag)
2524 {
2525 return ata_pci_init_one(pdev, ppi, sht, host_priv, hflag, 0);
2526 }
2527 EXPORT_SYMBOL_GPL(ata_pci_sff_init_one);
2528
2529 #endif
2530
2531
2532
2533
2534
2535 #ifdef CONFIG_ATA_BMDMA
2536
2537 const struct ata_port_operations ata_bmdma_port_ops = {
2538 .inherits = &ata_sff_port_ops,
2539
2540 .error_handler = ata_bmdma_error_handler,
2541 .post_internal_cmd = ata_bmdma_post_internal_cmd,
2542
2543 .qc_prep = ata_bmdma_qc_prep,
2544 .qc_issue = ata_bmdma_qc_issue,
2545
2546 .sff_irq_clear = ata_bmdma_irq_clear,
2547 .bmdma_setup = ata_bmdma_setup,
2548 .bmdma_start = ata_bmdma_start,
2549 .bmdma_stop = ata_bmdma_stop,
2550 .bmdma_status = ata_bmdma_status,
2551
2552 .port_start = ata_bmdma_port_start,
2553 };
2554 EXPORT_SYMBOL_GPL(ata_bmdma_port_ops);
2555
2556 const struct ata_port_operations ata_bmdma32_port_ops = {
2557 .inherits = &ata_bmdma_port_ops,
2558
2559 .sff_data_xfer = ata_sff_data_xfer32,
2560 .port_start = ata_bmdma_port_start32,
2561 };
2562 EXPORT_SYMBOL_GPL(ata_bmdma32_port_ops);
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575 static void ata_bmdma_fill_sg(struct ata_queued_cmd *qc)
2576 {
2577 struct ata_port *ap = qc->ap;
2578 struct ata_bmdma_prd *prd = ap->bmdma_prd;
2579 struct scatterlist *sg;
2580 unsigned int si, pi;
2581
2582 pi = 0;
2583 for_each_sg(qc->sg, sg, qc->n_elem, si) {
2584 u32 addr, offset;
2585 u32 sg_len, len;
2586
2587
2588
2589
2590
2591 addr = (u32) sg_dma_address(sg);
2592 sg_len = sg_dma_len(sg);
2593
2594 while (sg_len) {
2595 offset = addr & 0xffff;
2596 len = sg_len;
2597 if ((offset + sg_len) > 0x10000)
2598 len = 0x10000 - offset;
2599
2600 prd[pi].addr = cpu_to_le32(addr);
2601 prd[pi].flags_len = cpu_to_le32(len & 0xffff);
2602 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", pi, addr, len);
2603
2604 pi++;
2605 sg_len -= len;
2606 addr += len;
2607 }
2608 }
2609
2610 prd[pi - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2611 }
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626 static void ata_bmdma_fill_sg_dumb(struct ata_queued_cmd *qc)
2627 {
2628 struct ata_port *ap = qc->ap;
2629 struct ata_bmdma_prd *prd = ap->bmdma_prd;
2630 struct scatterlist *sg;
2631 unsigned int si, pi;
2632
2633 pi = 0;
2634 for_each_sg(qc->sg, sg, qc->n_elem, si) {
2635 u32 addr, offset;
2636 u32 sg_len, len, blen;
2637
2638
2639
2640
2641
2642 addr = (u32) sg_dma_address(sg);
2643 sg_len = sg_dma_len(sg);
2644
2645 while (sg_len) {
2646 offset = addr & 0xffff;
2647 len = sg_len;
2648 if ((offset + sg_len) > 0x10000)
2649 len = 0x10000 - offset;
2650
2651 blen = len & 0xffff;
2652 prd[pi].addr = cpu_to_le32(addr);
2653 if (blen == 0) {
2654
2655
2656
2657 prd[pi].flags_len = cpu_to_le32(0x8000);
2658 blen = 0x8000;
2659 prd[++pi].addr = cpu_to_le32(addr + 0x8000);
2660 }
2661 prd[pi].flags_len = cpu_to_le32(blen);
2662 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", pi, addr, len);
2663
2664 pi++;
2665 sg_len -= len;
2666 addr += len;
2667 }
2668 }
2669
2670 prd[pi - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2671 }
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682 void ata_bmdma_qc_prep(struct ata_queued_cmd *qc)
2683 {
2684 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2685 return;
2686
2687 ata_bmdma_fill_sg(qc);
2688 }
2689 EXPORT_SYMBOL_GPL(ata_bmdma_qc_prep);
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700 void ata_bmdma_dumb_qc_prep(struct ata_queued_cmd *qc)
2701 {
2702 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2703 return;
2704
2705 ata_bmdma_fill_sg_dumb(qc);
2706 }
2707 EXPORT_SYMBOL_GPL(ata_bmdma_dumb_qc_prep);
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723 unsigned int ata_bmdma_qc_issue(struct ata_queued_cmd *qc)
2724 {
2725 struct ata_port *ap = qc->ap;
2726 struct ata_link *link = qc->dev->link;
2727
2728
2729 if (!ata_is_dma(qc->tf.protocol))
2730 return ata_sff_qc_issue(qc);
2731
2732
2733 ata_dev_select(ap, qc->dev->devno, 1, 0);
2734
2735
2736 switch (qc->tf.protocol) {
2737 case ATA_PROT_DMA:
2738 WARN_ON_ONCE(qc->tf.flags & ATA_TFLAG_POLLING);
2739
2740 ap->ops->sff_tf_load(ap, &qc->tf);
2741 ap->ops->bmdma_setup(qc);
2742 ap->ops->bmdma_start(qc);
2743 ap->hsm_task_state = HSM_ST_LAST;
2744 break;
2745
2746 case ATAPI_PROT_DMA:
2747 WARN_ON_ONCE(qc->tf.flags & ATA_TFLAG_POLLING);
2748
2749 ap->ops->sff_tf_load(ap, &qc->tf);
2750 ap->ops->bmdma_setup(qc);
2751 ap->hsm_task_state = HSM_ST_FIRST;
2752
2753
2754 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
2755 ata_sff_queue_pio_task(link, 0);
2756 break;
2757
2758 default:
2759 WARN_ON(1);
2760 return AC_ERR_SYSTEM;
2761 }
2762
2763 return 0;
2764 }
2765 EXPORT_SYMBOL_GPL(ata_bmdma_qc_issue);
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780 unsigned int ata_bmdma_port_intr(struct ata_port *ap, struct ata_queued_cmd *qc)
2781 {
2782 struct ata_eh_info *ehi = &ap->link.eh_info;
2783 u8 host_stat = 0;
2784 bool bmdma_stopped = false;
2785 unsigned int handled;
2786
2787 if (ap->hsm_task_state == HSM_ST_LAST && ata_is_dma(qc->tf.protocol)) {
2788
2789 host_stat = ap->ops->bmdma_status(ap);
2790 VPRINTK("ata%u: host_stat 0x%X\n", ap->print_id, host_stat);
2791
2792
2793 if (!(host_stat & ATA_DMA_INTR))
2794 return ata_sff_idle_irq(ap);
2795
2796
2797 ap->ops->bmdma_stop(qc);
2798 bmdma_stopped = true;
2799
2800 if (unlikely(host_stat & ATA_DMA_ERR)) {
2801
2802 qc->err_mask |= AC_ERR_HOST_BUS;
2803 ap->hsm_task_state = HSM_ST_ERR;
2804 }
2805 }
2806
2807 handled = __ata_sff_port_intr(ap, qc, bmdma_stopped);
2808
2809 if (unlikely(qc->err_mask) && ata_is_dma(qc->tf.protocol))
2810 ata_ehi_push_desc(ehi, "BMDMA stat 0x%x", host_stat);
2811
2812 return handled;
2813 }
2814 EXPORT_SYMBOL_GPL(ata_bmdma_port_intr);
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830 irqreturn_t ata_bmdma_interrupt(int irq, void *dev_instance)
2831 {
2832 return __ata_sff_interrupt(irq, dev_instance, ata_bmdma_port_intr);
2833 }
2834 EXPORT_SYMBOL_GPL(ata_bmdma_interrupt);
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848 void ata_bmdma_error_handler(struct ata_port *ap)
2849 {
2850 struct ata_queued_cmd *qc;
2851 unsigned long flags;
2852 bool thaw = false;
2853
2854 qc = __ata_qc_from_tag(ap, ap->link.active_tag);
2855 if (qc && !(qc->flags & ATA_QCFLAG_FAILED))
2856 qc = NULL;
2857
2858
2859 spin_lock_irqsave(ap->lock, flags);
2860
2861 if (qc && ata_is_dma(qc->tf.protocol)) {
2862 u8 host_stat;
2863
2864 host_stat = ap->ops->bmdma_status(ap);
2865
2866
2867
2868
2869
2870
2871 if (qc->err_mask == AC_ERR_TIMEOUT && (host_stat & ATA_DMA_ERR)) {
2872 qc->err_mask = AC_ERR_HOST_BUS;
2873 thaw = true;
2874 }
2875
2876 ap->ops->bmdma_stop(qc);
2877
2878
2879 if (thaw) {
2880 ap->ops->sff_check_status(ap);
2881 if (ap->ops->sff_irq_clear)
2882 ap->ops->sff_irq_clear(ap);
2883 }
2884 }
2885
2886 spin_unlock_irqrestore(ap->lock, flags);
2887
2888 if (thaw)
2889 ata_eh_thaw_port(ap);
2890
2891 ata_sff_error_handler(ap);
2892 }
2893 EXPORT_SYMBOL_GPL(ata_bmdma_error_handler);
2894
2895
2896
2897
2898
2899
2900
2901
2902 void ata_bmdma_post_internal_cmd(struct ata_queued_cmd *qc)
2903 {
2904 struct ata_port *ap = qc->ap;
2905 unsigned long flags;
2906
2907 if (ata_is_dma(qc->tf.protocol)) {
2908 spin_lock_irqsave(ap->lock, flags);
2909 ap->ops->bmdma_stop(qc);
2910 spin_unlock_irqrestore(ap->lock, flags);
2911 }
2912 }
2913 EXPORT_SYMBOL_GPL(ata_bmdma_post_internal_cmd);
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926 void ata_bmdma_irq_clear(struct ata_port *ap)
2927 {
2928 void __iomem *mmio = ap->ioaddr.bmdma_addr;
2929
2930 if (!mmio)
2931 return;
2932
2933 iowrite8(ioread8(mmio + ATA_DMA_STATUS), mmio + ATA_DMA_STATUS);
2934 }
2935 EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
2936
2937
2938
2939
2940
2941
2942
2943
2944 void ata_bmdma_setup(struct ata_queued_cmd *qc)
2945 {
2946 struct ata_port *ap = qc->ap;
2947 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
2948 u8 dmactl;
2949
2950
2951 mb();
2952 iowrite32(ap->bmdma_prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
2953
2954
2955 dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2956 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
2957 if (!rw)
2958 dmactl |= ATA_DMA_WR;
2959 iowrite8(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2960
2961
2962 ap->ops->sff_exec_command(ap, &qc->tf);
2963 }
2964 EXPORT_SYMBOL_GPL(ata_bmdma_setup);
2965
2966
2967
2968
2969
2970
2971
2972
2973 void ata_bmdma_start(struct ata_queued_cmd *qc)
2974 {
2975 struct ata_port *ap = qc->ap;
2976 u8 dmactl;
2977
2978
2979 dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2980 iowrite8(dmactl | ATA_DMA_START, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996 }
2997 EXPORT_SYMBOL_GPL(ata_bmdma_start);
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010 void ata_bmdma_stop(struct ata_queued_cmd *qc)
3011 {
3012 struct ata_port *ap = qc->ap;
3013 void __iomem *mmio = ap->ioaddr.bmdma_addr;
3014
3015
3016 iowrite8(ioread8(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
3017 mmio + ATA_DMA_CMD);
3018
3019
3020 ata_sff_dma_pause(ap);
3021 }
3022 EXPORT_SYMBOL_GPL(ata_bmdma_stop);
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035 u8 ata_bmdma_status(struct ata_port *ap)
3036 {
3037 return ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
3038 }
3039 EXPORT_SYMBOL_GPL(ata_bmdma_status);
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054 int ata_bmdma_port_start(struct ata_port *ap)
3055 {
3056 if (ap->mwdma_mask || ap->udma_mask) {
3057 ap->bmdma_prd =
3058 dmam_alloc_coherent(ap->host->dev, ATA_PRD_TBL_SZ,
3059 &ap->bmdma_prd_dma, GFP_KERNEL);
3060 if (!ap->bmdma_prd)
3061 return -ENOMEM;
3062 }
3063
3064 return 0;
3065 }
3066 EXPORT_SYMBOL_GPL(ata_bmdma_port_start);
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082 int ata_bmdma_port_start32(struct ata_port *ap)
3083 {
3084 ap->pflags |= ATA_PFLAG_PIO32 | ATA_PFLAG_PIO32CHANGE;
3085 return ata_bmdma_port_start(ap);
3086 }
3087 EXPORT_SYMBOL_GPL(ata_bmdma_port_start32);
3088
3089 #ifdef CONFIG_PCI
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100 int ata_pci_bmdma_clear_simplex(struct pci_dev *pdev)
3101 {
3102 unsigned long bmdma = pci_resource_start(pdev, 4);
3103 u8 simplex;
3104
3105 if (bmdma == 0)
3106 return -ENOENT;
3107
3108 simplex = inb(bmdma + 0x02);
3109 outb(simplex & 0x60, bmdma + 0x02);
3110 simplex = inb(bmdma + 0x02);
3111 if (simplex & 0x80)
3112 return -EOPNOTSUPP;
3113 return 0;
3114 }
3115 EXPORT_SYMBOL_GPL(ata_pci_bmdma_clear_simplex);
3116
3117 static void ata_bmdma_nodma(struct ata_host *host, const char *reason)
3118 {
3119 int i;
3120
3121 dev_err(host->dev, "BMDMA: %s, falling back to PIO\n", reason);
3122
3123 for (i = 0; i < 2; i++) {
3124 host->ports[i]->mwdma_mask = 0;
3125 host->ports[i]->udma_mask = 0;
3126 }
3127 }
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138 void ata_pci_bmdma_init(struct ata_host *host)
3139 {
3140 struct device *gdev = host->dev;
3141 struct pci_dev *pdev = to_pci_dev(gdev);
3142 int i, rc;
3143
3144
3145 if (pci_resource_start(pdev, 4) == 0) {
3146 ata_bmdma_nodma(host, "BAR4 is zero");
3147 return;
3148 }
3149
3150
3151
3152
3153
3154
3155
3156 rc = dma_set_mask_and_coherent(&pdev->dev, ATA_DMA_MASK);
3157 if (rc)
3158 ata_bmdma_nodma(host, "failed to set dma mask");
3159
3160
3161 rc = pcim_iomap_regions(pdev, 1 << 4, dev_driver_string(gdev));
3162 if (rc) {
3163 ata_bmdma_nodma(host, "failed to request/iomap BAR4");
3164 return;
3165 }
3166 host->iomap = pcim_iomap_table(pdev);
3167
3168 for (i = 0; i < 2; i++) {
3169 struct ata_port *ap = host->ports[i];
3170 void __iomem *bmdma = host->iomap[4] + 8 * i;
3171
3172 if (ata_port_is_dummy(ap))
3173 continue;
3174
3175 ap->ioaddr.bmdma_addr = bmdma;
3176 if ((!(ap->flags & ATA_FLAG_IGN_SIMPLEX)) &&
3177 (ioread8(bmdma + 2) & 0x80))
3178 host->flags |= ATA_HOST_SIMPLEX;
3179
3180 ata_port_desc(ap, "bmdma 0x%llx",
3181 (unsigned long long)pci_resource_start(pdev, 4) + 8 * i);
3182 }
3183 }
3184 EXPORT_SYMBOL_GPL(ata_pci_bmdma_init);
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201 int ata_pci_bmdma_prepare_host(struct pci_dev *pdev,
3202 const struct ata_port_info * const * ppi,
3203 struct ata_host **r_host)
3204 {
3205 int rc;
3206
3207 rc = ata_pci_sff_prepare_host(pdev, ppi, r_host);
3208 if (rc)
3209 return rc;
3210
3211 ata_pci_bmdma_init(*r_host);
3212 return 0;
3213 }
3214 EXPORT_SYMBOL_GPL(ata_pci_bmdma_prepare_host);
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233 int ata_pci_bmdma_init_one(struct pci_dev *pdev,
3234 const struct ata_port_info * const * ppi,
3235 struct scsi_host_template *sht, void *host_priv,
3236 int hflags)
3237 {
3238 return ata_pci_init_one(pdev, ppi, sht, host_priv, hflags, 1);
3239 }
3240 EXPORT_SYMBOL_GPL(ata_pci_bmdma_init_one);
3241
3242 #endif
3243 #endif
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255 void ata_sff_port_init(struct ata_port *ap)
3256 {
3257 INIT_DELAYED_WORK(&ap->sff_pio_task, ata_sff_pio_task);
3258 ap->ctl = ATA_DEVCTL_OBS;
3259 ap->last_ctl = 0xFF;
3260 }
3261
3262 int __init ata_sff_init(void)
3263 {
3264 ata_sff_wq = alloc_workqueue("ata_sff", WQ_MEM_RECLAIM, WQ_MAX_ACTIVE);
3265 if (!ata_sff_wq)
3266 return -ENOMEM;
3267
3268 return 0;
3269 }
3270
3271 void ata_sff_exit(void)
3272 {
3273 destroy_workqueue(ata_sff_wq);
3274 }