forked from lordarcadius/electrablue_mido
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatp870u.c
3898 lines (3658 loc) · 84.2 KB
/
atp870u.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 1997 Wu Ching Chen
* 2.1.x update (C) 1998 Krzysztof G. Baranowski
* 2.5.x update (C) 2002 Red Hat
* 2.6.x update (C) 2004 Red Hat
*
* Marcelo Tosatti <marcelo@conectiva.com.br> : SMP fixes
*
* Wu Ching Chen : NULL pointer fixes 2000/06/02
* support atp876 chip
* enable 32 bit fifo transfer
* support cdrom & remove device run ultra speed
* fix disconnect bug 2000/12/21
* support atp880 chip lvd u160 2001/05/15
* fix prd table bug 2001/09/12 (7.1)
*
* atp885 support add by ACARD Hao Ping Lian 2005/01/05
*/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/string.h>
#include <linux/ioport.h>
#include <linux/delay.h>
#include <linux/proc_fs.h>
#include <linux/spinlock.h>
#include <linux/pci.h>
#include <linux/blkdev.h>
#include <linux/dma-mapping.h>
#include <linux/slab.h>
#include <asm/io.h>
#include <scsi/scsi.h>
#include <scsi/scsi_cmnd.h>
#include <scsi/scsi_device.h>
#include <scsi/scsi_host.h>
#include "atp870u.h"
static struct scsi_host_template atp870u_template;
static void send_s870(struct atp_unit *dev,unsigned char c);
static void is885(struct atp_unit *dev, unsigned int wkport,unsigned char c);
static void tscam_885(void);
static irqreturn_t atp870u_intr_handle(int irq, void *dev_id)
{
unsigned long flags;
unsigned short int tmpcip, id;
unsigned char i, j, c, target_id, lun,cmdp;
unsigned char *prd;
struct scsi_cmnd *workreq;
unsigned int workport, tmport, tmport1;
unsigned long adrcnt, k;
#ifdef ED_DBGP
unsigned long l;
#endif
int errstus;
struct Scsi_Host *host = dev_id;
struct atp_unit *dev = (struct atp_unit *)&host->hostdata;
for (c = 0; c < 2; c++) {
tmport = dev->ioport[c] + 0x1f;
j = inb(tmport);
if ((j & 0x80) != 0)
{
goto ch_sel;
}
dev->in_int[c] = 0;
}
return IRQ_NONE;
ch_sel:
#ifdef ED_DBGP
printk("atp870u_intr_handle enter\n");
#endif
dev->in_int[c] = 1;
cmdp = inb(dev->ioport[c] + 0x10);
workport = dev->ioport[c];
if (dev->working[c] != 0) {
if (dev->dev_id == ATP885_DEVID) {
tmport1 = workport + 0x16;
if ((inb(tmport1) & 0x80) == 0)
outb((inb(tmport1) | 0x80), tmport1);
}
tmpcip = dev->pciport[c];
if ((inb(tmpcip) & 0x08) != 0)
{
tmpcip += 0x2;
for (k=0; k < 1000; k++) {
if ((inb(tmpcip) & 0x08) == 0) {
goto stop_dma;
}
if ((inb(tmpcip) & 0x01) == 0) {
goto stop_dma;
}
}
}
stop_dma:
tmpcip = dev->pciport[c];
outb(0x00, tmpcip);
tmport -= 0x08;
i = inb(tmport);
if (dev->dev_id == ATP885_DEVID) {
tmpcip += 2;
outb(0x06, tmpcip);
tmpcip -= 2;
}
tmport -= 0x02;
target_id = inb(tmport);
tmport += 0x02;
/*
* Remap wide devices onto id numbers
*/
if ((target_id & 0x40) != 0) {
target_id = (target_id & 0x07) | 0x08;
} else {
target_id &= 0x07;
}
if ((j & 0x40) != 0) {
if (dev->last_cmd[c] == 0xff) {
dev->last_cmd[c] = target_id;
}
dev->last_cmd[c] |= 0x40;
}
if (dev->dev_id == ATP885_DEVID)
dev->r1f[c][target_id] |= j;
#ifdef ED_DBGP
printk("atp870u_intr_handle status = %x\n",i);
#endif
if (i == 0x85) {
if ((dev->last_cmd[c] & 0xf0) != 0x40) {
dev->last_cmd[c] = 0xff;
}
if (dev->dev_id == ATP885_DEVID) {
tmport -= 0x05;
adrcnt = 0;
((unsigned char *) &adrcnt)[2] = inb(tmport++);
((unsigned char *) &adrcnt)[1] = inb(tmport++);
((unsigned char *) &adrcnt)[0] = inb(tmport);
if (dev->id[c][target_id].last_len != adrcnt)
{
k = dev->id[c][target_id].last_len;
k -= adrcnt;
dev->id[c][target_id].tran_len = k;
dev->id[c][target_id].last_len = adrcnt;
}
#ifdef ED_DBGP
printk("tmport = %x dev->id[c][target_id].last_len = %d dev->id[c][target_id].tran_len = %d\n",tmport,dev->id[c][target_id].last_len,dev->id[c][target_id].tran_len);
#endif
}
/*
* Flip wide
*/
if (dev->wide_id[c] != 0) {
tmport = workport + 0x1b;
outb(0x01, tmport);
while ((inb(tmport) & 0x01) != 0x01) {
outb(0x01, tmport);
}
}
/*
* Issue more commands
*/
spin_lock_irqsave(dev->host->host_lock, flags);
if (((dev->quhd[c] != dev->quend[c]) || (dev->last_cmd[c] != 0xff)) &&
(dev->in_snd[c] == 0)) {
#ifdef ED_DBGP
printk("Call sent_s870\n");
#endif
send_s870(dev,c);
}
spin_unlock_irqrestore(dev->host->host_lock, flags);
/*
* Done
*/
dev->in_int[c] = 0;
#ifdef ED_DBGP
printk("Status 0x85 return\n");
#endif
goto handled;
}
if (i == 0x40) {
dev->last_cmd[c] |= 0x40;
dev->in_int[c] = 0;
goto handled;
}
if (i == 0x21) {
if ((dev->last_cmd[c] & 0xf0) != 0x40) {
dev->last_cmd[c] = 0xff;
}
tmport -= 0x05;
adrcnt = 0;
((unsigned char *) &adrcnt)[2] = inb(tmport++);
((unsigned char *) &adrcnt)[1] = inb(tmport++);
((unsigned char *) &adrcnt)[0] = inb(tmport);
k = dev->id[c][target_id].last_len;
k -= adrcnt;
dev->id[c][target_id].tran_len = k;
dev->id[c][target_id].last_len = adrcnt;
tmport -= 0x04;
outb(0x41, tmport);
tmport += 0x08;
outb(0x08, tmport);
dev->in_int[c] = 0;
goto handled;
}
if (dev->dev_id == ATP885_DEVID) {
if ((i == 0x4c) || (i == 0x4d) || (i == 0x8c) || (i == 0x8d)) {
if ((i == 0x4c) || (i == 0x8c))
i=0x48;
else
i=0x49;
}
}
if ((i == 0x80) || (i == 0x8f)) {
#ifdef ED_DBGP
printk(KERN_DEBUG "Device reselect\n");
#endif
lun = 0;
tmport -= 0x07;
if (cmdp == 0x44 || i==0x80) {
tmport += 0x0d;
lun = inb(tmport) & 0x07;
} else {
if ((dev->last_cmd[c] & 0xf0) != 0x40) {
dev->last_cmd[c] = 0xff;
}
if (cmdp == 0x41) {
#ifdef ED_DBGP
printk("cmdp = 0x41\n");
#endif
tmport += 0x02;
adrcnt = 0;
((unsigned char *) &adrcnt)[2] = inb(tmport++);
((unsigned char *) &adrcnt)[1] = inb(tmport++);
((unsigned char *) &adrcnt)[0] = inb(tmport);
k = dev->id[c][target_id].last_len;
k -= adrcnt;
dev->id[c][target_id].tran_len = k;
dev->id[c][target_id].last_len = adrcnt;
tmport += 0x04;
outb(0x08, tmport);
dev->in_int[c] = 0;
goto handled;
} else {
#ifdef ED_DBGP
printk("cmdp != 0x41\n");
#endif
outb(0x46, tmport);
dev->id[c][target_id].dirct = 0x00;
tmport += 0x02;
outb(0x00, tmport++);
outb(0x00, tmport++);
outb(0x00, tmport++);
tmport += 0x03;
outb(0x08, tmport);
dev->in_int[c] = 0;
goto handled;
}
}
if (dev->last_cmd[c] != 0xff) {
dev->last_cmd[c] |= 0x40;
}
if (dev->dev_id == ATP885_DEVID) {
j = inb(dev->baseport + 0x29) & 0xfe;
outb(j, dev->baseport + 0x29);
tmport = workport + 0x16;
} else {
tmport = workport + 0x10;
outb(0x45, tmport);
tmport += 0x06;
}
target_id = inb(tmport);
/*
* Remap wide identifiers
*/
if ((target_id & 0x10) != 0) {
target_id = (target_id & 0x07) | 0x08;
} else {
target_id &= 0x07;
}
if (dev->dev_id == ATP885_DEVID) {
tmport = workport + 0x10;
outb(0x45, tmport);
}
workreq = dev->id[c][target_id].curr_req;
#ifdef ED_DBGP
scmd_printk(KERN_DEBUG, workreq, "CDB");
for (l = 0; l < workreq->cmd_len; l++)
printk(KERN_DEBUG " %x",workreq->cmnd[l]);
printk("\n");
#endif
tmport = workport + 0x0f;
outb(lun, tmport);
tmport += 0x02;
outb(dev->id[c][target_id].devsp, tmport++);
adrcnt = dev->id[c][target_id].tran_len;
k = dev->id[c][target_id].last_len;
outb(((unsigned char *) &k)[2], tmport++);
outb(((unsigned char *) &k)[1], tmport++);
outb(((unsigned char *) &k)[0], tmport++);
#ifdef ED_DBGP
printk("k %x, k[0] 0x%x k[1] 0x%x k[2] 0x%x\n", k, inb(tmport-1), inb(tmport-2), inb(tmport-3));
#endif
/* Remap wide */
j = target_id;
if (target_id > 7) {
j = (j & 0x07) | 0x40;
}
/* Add direction */
j |= dev->id[c][target_id].dirct;
outb(j, tmport++);
outb(0x80,tmport);
/* enable 32 bit fifo transfer */
if (dev->dev_id == ATP885_DEVID) {
tmpcip = dev->pciport[c] + 1;
i=inb(tmpcip) & 0xf3;
//j=workreq->cmnd[0];
if ((workreq->cmnd[0] == 0x08) || (workreq->cmnd[0] == 0x28) || (workreq->cmnd[0] == 0x0a) || (workreq->cmnd[0] == 0x2a)) {
i |= 0x0c;
}
outb(i,tmpcip);
} else if ((dev->dev_id == ATP880_DEVID1) ||
(dev->dev_id == ATP880_DEVID2) ) {
tmport = workport - 0x05;
if ((workreq->cmnd[0] == 0x08) || (workreq->cmnd[0] == 0x28) || (workreq->cmnd[0] == 0x0a) || (workreq->cmnd[0] == 0x2a)) {
outb((unsigned char) ((inb(tmport) & 0x3f) | 0xc0), tmport);
} else {
outb((unsigned char) (inb(tmport) & 0x3f), tmport);
}
} else {
tmport = workport + 0x3a;
if ((workreq->cmnd[0] == 0x08) || (workreq->cmnd[0] == 0x28) || (workreq->cmnd[0] == 0x0a) || (workreq->cmnd[0] == 0x2a)) {
outb((unsigned char) ((inb(tmport) & 0xf3) | 0x08), tmport);
} else {
outb((unsigned char) (inb(tmport) & 0xf3), tmport);
}
}
tmport = workport + 0x1b;
j = 0;
id = 1;
id = id << target_id;
/*
* Is this a wide device
*/
if ((id & dev->wide_id[c]) != 0) {
j |= 0x01;
}
outb(j, tmport);
while ((inb(tmport) & 0x01) != j) {
outb(j,tmport);
}
if (dev->id[c][target_id].last_len == 0) {
tmport = workport + 0x18;
outb(0x08, tmport);
dev->in_int[c] = 0;
#ifdef ED_DBGP
printk("dev->id[c][target_id].last_len = 0\n");
#endif
goto handled;
}
#ifdef ED_DBGP
printk("target_id = %d adrcnt = %d\n",target_id,adrcnt);
#endif
prd = dev->id[c][target_id].prd_pos;
while (adrcnt != 0) {
id = ((unsigned short int *)prd)[2];
if (id == 0) {
k = 0x10000;
} else {
k = id;
}
if (k > adrcnt) {
((unsigned short int *)prd)[2] = (unsigned short int)
(k - adrcnt);
((unsigned long *)prd)[0] += adrcnt;
adrcnt = 0;
dev->id[c][target_id].prd_pos = prd;
} else {
adrcnt -= k;
dev->id[c][target_id].prdaddr += 0x08;
prd += 0x08;
if (adrcnt == 0) {
dev->id[c][target_id].prd_pos = prd;
}
}
}
tmpcip = dev->pciport[c] + 0x04;
outl(dev->id[c][target_id].prdaddr, tmpcip);
#ifdef ED_DBGP
printk("dev->id[%d][%d].prdaddr 0x%8x\n", c, target_id, dev->id[c][target_id].prdaddr);
#endif
if (dev->dev_id == ATP885_DEVID) {
tmpcip -= 0x04;
} else {
tmpcip -= 0x02;
outb(0x06, tmpcip);
outb(0x00, tmpcip);
tmpcip -= 0x02;
}
tmport = workport + 0x18;
/*
* Check transfer direction
*/
if (dev->id[c][target_id].dirct != 0) {
outb(0x08, tmport);
outb(0x01, tmpcip);
dev->in_int[c] = 0;
#ifdef ED_DBGP
printk("status 0x80 return dirct != 0\n");
#endif
goto handled;
}
outb(0x08, tmport);
outb(0x09, tmpcip);
dev->in_int[c] = 0;
#ifdef ED_DBGP
printk("status 0x80 return dirct = 0\n");
#endif
goto handled;
}
/*
* Current scsi request on this target
*/
workreq = dev->id[c][target_id].curr_req;
if (i == 0x42) {
if ((dev->last_cmd[c] & 0xf0) != 0x40)
{
dev->last_cmd[c] = 0xff;
}
errstus = 0x02;
workreq->result = errstus;
goto go_42;
}
if (i == 0x16) {
if ((dev->last_cmd[c] & 0xf0) != 0x40) {
dev->last_cmd[c] = 0xff;
}
errstus = 0;
tmport -= 0x08;
errstus = inb(tmport);
if (((dev->r1f[c][target_id] & 0x10) != 0)&&(dev->dev_id==ATP885_DEVID)) {
printk(KERN_WARNING "AEC67162 CRC ERROR !\n");
errstus = 0x02;
}
workreq->result = errstus;
go_42:
if (dev->dev_id == ATP885_DEVID) {
j = inb(dev->baseport + 0x29) | 0x01;
outb(j, dev->baseport + 0x29);
}
/*
* Complete the command
*/
scsi_dma_unmap(workreq);
spin_lock_irqsave(dev->host->host_lock, flags);
(*workreq->scsi_done) (workreq);
#ifdef ED_DBGP
printk("workreq->scsi_done\n");
#endif
/*
* Clear it off the queue
*/
dev->id[c][target_id].curr_req = NULL;
dev->working[c]--;
spin_unlock_irqrestore(dev->host->host_lock, flags);
/*
* Take it back wide
*/
if (dev->wide_id[c] != 0) {
tmport = workport + 0x1b;
outb(0x01, tmport);
while ((inb(tmport) & 0x01) != 0x01) {
outb(0x01, tmport);
}
}
/*
* If there is stuff to send and nothing going then send it
*/
spin_lock_irqsave(dev->host->host_lock, flags);
if (((dev->last_cmd[c] != 0xff) || (dev->quhd[c] != dev->quend[c])) &&
(dev->in_snd[c] == 0)) {
#ifdef ED_DBGP
printk("Call sent_s870(scsi_done)\n");
#endif
send_s870(dev,c);
}
spin_unlock_irqrestore(dev->host->host_lock, flags);
dev->in_int[c] = 0;
goto handled;
}
if ((dev->last_cmd[c] & 0xf0) != 0x40) {
dev->last_cmd[c] = 0xff;
}
if (i == 0x4f) {
i = 0x89;
}
i &= 0x0f;
if (i == 0x09) {
tmpcip += 4;
outl(dev->id[c][target_id].prdaddr, tmpcip);
tmpcip = tmpcip - 2;
outb(0x06, tmpcip);
outb(0x00, tmpcip);
tmpcip = tmpcip - 2;
tmport = workport + 0x10;
outb(0x41, tmport);
if (dev->dev_id == ATP885_DEVID) {
tmport += 2;
k = dev->id[c][target_id].last_len;
outb((unsigned char) (((unsigned char *) (&k))[2]), tmport++);
outb((unsigned char) (((unsigned char *) (&k))[1]), tmport++);
outb((unsigned char) (((unsigned char *) (&k))[0]), tmport);
dev->id[c][target_id].dirct = 0x00;
tmport += 0x04;
} else {
dev->id[c][target_id].dirct = 0x00;
tmport += 0x08;
}
outb(0x08, tmport);
outb(0x09, tmpcip);
dev->in_int[c] = 0;
goto handled;
}
if (i == 0x08) {
tmpcip += 4;
outl(dev->id[c][target_id].prdaddr, tmpcip);
tmpcip = tmpcip - 2;
outb(0x06, tmpcip);
outb(0x00, tmpcip);
tmpcip = tmpcip - 2;
tmport = workport + 0x10;
outb(0x41, tmport);
if (dev->dev_id == ATP885_DEVID) {
tmport += 2;
k = dev->id[c][target_id].last_len;
outb((unsigned char) (((unsigned char *) (&k))[2]), tmport++);
outb((unsigned char) (((unsigned char *) (&k))[1]), tmport++);
outb((unsigned char) (((unsigned char *) (&k))[0]), tmport++);
} else {
tmport += 5;
}
outb((unsigned char) (inb(tmport) | 0x20), tmport);
dev->id[c][target_id].dirct = 0x20;
tmport += 0x03;
outb(0x08, tmport);
outb(0x01, tmpcip);
dev->in_int[c] = 0;
goto handled;
}
tmport -= 0x07;
if (i == 0x0a) {
outb(0x30, tmport);
} else {
outb(0x46, tmport);
}
dev->id[c][target_id].dirct = 0x00;
tmport += 0x02;
outb(0x00, tmport++);
outb(0x00, tmport++);
outb(0x00, tmport++);
tmport += 0x03;
outb(0x08, tmport);
dev->in_int[c] = 0;
goto handled;
} else {
// tmport = workport + 0x17;
// inb(tmport);
// dev->working[c] = 0;
dev->in_int[c] = 0;
goto handled;
}
handled:
#ifdef ED_DBGP
printk("atp870u_intr_handle exit\n");
#endif
return IRQ_HANDLED;
}
/**
* atp870u_queuecommand - Queue SCSI command
* @req_p: request block
* @done: completion function
*
* Queue a command to the ATP queue. Called with the host lock held.
*/
static int atp870u_queuecommand_lck(struct scsi_cmnd *req_p,
void (*done) (struct scsi_cmnd *))
{
unsigned char c;
unsigned int tmport,m;
struct atp_unit *dev;
struct Scsi_Host *host;
c = scmd_channel(req_p);
req_p->sense_buffer[0]=0;
scsi_set_resid(req_p, 0);
if (scmd_channel(req_p) > 1) {
req_p->result = 0x00040000;
done(req_p);
#ifdef ED_DBGP
printk("atp870u_queuecommand : req_p->device->channel > 1\n");
#endif
return 0;
}
host = req_p->device->host;
dev = (struct atp_unit *)&host->hostdata;
m = 1;
m = m << scmd_id(req_p);
/*
* Fake a timeout for missing targets
*/
if ((m & dev->active_id[c]) == 0) {
req_p->result = 0x00040000;
done(req_p);
return 0;
}
if (done) {
req_p->scsi_done = done;
} else {
#ifdef ED_DBGP
printk( "atp870u_queuecommand: done can't be NULL\n");
#endif
req_p->result = 0;
done(req_p);
return 0;
}
/*
* Count new command
*/
dev->quend[c]++;
if (dev->quend[c] >= qcnt) {
dev->quend[c] = 0;
}
/*
* Check queue state
*/
if (dev->quhd[c] == dev->quend[c]) {
if (dev->quend[c] == 0) {
dev->quend[c] = qcnt;
}
#ifdef ED_DBGP
printk("atp870u_queuecommand : dev->quhd[c] == dev->quend[c]\n");
#endif
dev->quend[c]--;
req_p->result = 0x00020000;
done(req_p);
return 0;
}
dev->quereq[c][dev->quend[c]] = req_p;
tmport = dev->ioport[c] + 0x1c;
#ifdef ED_DBGP
printk("dev->ioport[c] = %x inb(tmport) = %x dev->in_int[%d] = %d dev->in_snd[%d] = %d\n",dev->ioport[c],inb(tmport),c,dev->in_int[c],c,dev->in_snd[c]);
#endif
if ((inb(tmport) == 0) && (dev->in_int[c] == 0) && (dev->in_snd[c] == 0)) {
#ifdef ED_DBGP
printk("Call sent_s870(atp870u_queuecommand)\n");
#endif
send_s870(dev,c);
}
#ifdef ED_DBGP
printk("atp870u_queuecommand : exit\n");
#endif
return 0;
}
static DEF_SCSI_QCMD(atp870u_queuecommand)
/**
* send_s870 - send a command to the controller
* @host: host
*
* On entry there is work queued to be done. We move some of that work to the
* controller itself.
*
* Caller holds the host lock.
*/
static void send_s870(struct atp_unit *dev,unsigned char c)
{
unsigned int tmport;
struct scsi_cmnd *workreq;
unsigned int i;//,k;
unsigned char j, target_id;
unsigned char *prd;
unsigned short int tmpcip, w;
unsigned long l, bttl = 0;
unsigned int workport;
unsigned long sg_count;
if (dev->in_snd[c] != 0) {
#ifdef ED_DBGP
printk("cmnd in_snd\n");
#endif
return;
}
#ifdef ED_DBGP
printk("Sent_s870 enter\n");
#endif
dev->in_snd[c] = 1;
if ((dev->last_cmd[c] != 0xff) && ((dev->last_cmd[c] & 0x40) != 0)) {
dev->last_cmd[c] &= 0x0f;
workreq = dev->id[c][dev->last_cmd[c]].curr_req;
if (workreq != NULL) { /* check NULL pointer */
goto cmd_subp;
}
dev->last_cmd[c] = 0xff;
if (dev->quhd[c] == dev->quend[c]) {
dev->in_snd[c] = 0;
return ;
}
}
if ((dev->last_cmd[c] != 0xff) && (dev->working[c] != 0)) {
dev->in_snd[c] = 0;
return ;
}
dev->working[c]++;
j = dev->quhd[c];
dev->quhd[c]++;
if (dev->quhd[c] >= qcnt) {
dev->quhd[c] = 0;
}
workreq = dev->quereq[c][dev->quhd[c]];
if (dev->id[c][scmd_id(workreq)].curr_req == NULL) {
dev->id[c][scmd_id(workreq)].curr_req = workreq;
dev->last_cmd[c] = scmd_id(workreq);
goto cmd_subp;
}
dev->quhd[c] = j;
dev->working[c]--;
dev->in_snd[c] = 0;
return;
cmd_subp:
workport = dev->ioport[c];
tmport = workport + 0x1f;
if ((inb(tmport) & 0xb0) != 0) {
goto abortsnd;
}
tmport = workport + 0x1c;
if (inb(tmport) == 0) {
goto oktosend;
}
abortsnd:
#ifdef ED_DBGP
printk("Abort to Send\n");
#endif
dev->last_cmd[c] |= 0x40;
dev->in_snd[c] = 0;
return;
oktosend:
#ifdef ED_DBGP
printk("OK to Send\n");
scmd_printk(KERN_DEBUG, workreq, "CDB");
for(i=0;i<workreq->cmd_len;i++) {
printk(" %x",workreq->cmnd[i]);
}
printk("\n");
#endif
l = scsi_bufflen(workreq);
if (dev->dev_id == ATP885_DEVID) {
j = inb(dev->baseport + 0x29) & 0xfe;
outb(j, dev->baseport + 0x29);
dev->r1f[c][scmd_id(workreq)] = 0;
}
if (workreq->cmnd[0] == READ_CAPACITY) {
if (l > 8)
l = 8;
}
if (workreq->cmnd[0] == 0x00) {
l = 0;
}
tmport = workport + 0x1b;
j = 0;
target_id = scmd_id(workreq);
/*
* Wide ?
*/
w = 1;
w = w << target_id;
if ((w & dev->wide_id[c]) != 0) {
j |= 0x01;
}
outb(j, tmport);
while ((inb(tmport) & 0x01) != j) {
outb(j,tmport);
#ifdef ED_DBGP
printk("send_s870 while loop 1\n");
#endif
}
/*
* Write the command
*/
tmport = workport;
outb(workreq->cmd_len, tmport++);
outb(0x2c, tmport++);
if (dev->dev_id == ATP885_DEVID) {
outb(0x7f, tmport++);
} else {
outb(0xcf, tmport++);
}
for (i = 0; i < workreq->cmd_len; i++) {
outb(workreq->cmnd[i], tmport++);
}
tmport = workport + 0x0f;
outb(workreq->device->lun, tmport);
tmport += 0x02;
/*
* Write the target
*/
outb(dev->id[c][target_id].devsp, tmport++);
#ifdef ED_DBGP
printk("dev->id[%d][%d].devsp = %2x\n",c,target_id,dev->id[c][target_id].devsp);
#endif
sg_count = scsi_dma_map(workreq);
/*
* Write transfer size
*/
outb((unsigned char) (((unsigned char *) (&l))[2]), tmport++);
outb((unsigned char) (((unsigned char *) (&l))[1]), tmport++);
outb((unsigned char) (((unsigned char *) (&l))[0]), tmport++);
j = target_id;
dev->id[c][j].last_len = l;
dev->id[c][j].tran_len = 0;
#ifdef ED_DBGP
printk("dev->id[%2d][%2d].last_len = %d\n",c,j,dev->id[c][j].last_len);
#endif
/*
* Flip the wide bits
*/
if ((j & 0x08) != 0) {
j = (j & 0x07) | 0x40;
}
/*
* Check transfer direction
*/
if (workreq->sc_data_direction == DMA_TO_DEVICE) {
outb((unsigned char) (j | 0x20), tmport++);
} else {
outb(j, tmport++);
}
outb((unsigned char) (inb(tmport) | 0x80), tmport);
outb(0x80, tmport);
tmport = workport + 0x1c;
dev->id[c][target_id].dirct = 0;
if (l == 0) {
if (inb(tmport) == 0) {
tmport = workport + 0x18;
#ifdef ED_DBGP
printk("change SCSI_CMD_REG 0x08\n");
#endif
outb(0x08, tmport);
} else {
dev->last_cmd[c] |= 0x40;
}
dev->in_snd[c] = 0;
return;
}
tmpcip = dev->pciport[c];
prd = dev->id[c][target_id].prd_table;
dev->id[c][target_id].prd_pos = prd;
/*
* Now write the request list. Either as scatter/gather or as
* a linear chain.
*/
if (l) {
struct scatterlist *sgpnt;
i = 0;
scsi_for_each_sg(workreq, sgpnt, sg_count, j) {
bttl = sg_dma_address(sgpnt);
l=sg_dma_len(sgpnt);
#ifdef ED_DBGP
printk("1. bttl %x, l %x\n",bttl, l);
#endif
while (l > 0x10000) {
(((u16 *) (prd))[i + 3]) = 0x0000;
(((u16 *) (prd))[i + 2]) = 0x0000;
(((u32 *) (prd))[i >> 1]) = cpu_to_le32(bttl);
l -= 0x10000;
bttl += 0x10000;
i += 0x04;
}
(((u32 *) (prd))[i >> 1]) = cpu_to_le32(bttl);
(((u16 *) (prd))[i + 2]) = cpu_to_le16(l);
(((u16 *) (prd))[i + 3]) = 0;
i += 0x04;
}
(((u16 *) (prd))[i - 1]) = cpu_to_le16(0x8000);
#ifdef ED_DBGP
printk("prd %4x %4x %4x %4x\n",(((unsigned short int *)prd)[0]),(((unsigned short int *)prd)[1]),(((unsigned short int *)prd)[2]),(((unsigned short int *)prd)[3]));
printk("2. bttl %x, l %x\n",bttl, l);
#endif
}
tmpcip += 4;
#ifdef ED_DBGP
printk("send_s870: prdaddr_2 0x%8x tmpcip %x target_id %d\n", dev->id[c][target_id].prdaddr,tmpcip,target_id);
#endif
dev->id[c][target_id].prdaddr = dev->id[c][target_id].prd_bus;
outl(dev->id[c][target_id].prdaddr, tmpcip);
tmpcip = tmpcip - 2;
outb(0x06, tmpcip);
outb(0x00, tmpcip);
if (dev->dev_id == ATP885_DEVID) {
tmpcip--;
j=inb(tmpcip) & 0xf3;
if ((workreq->cmnd[0] == 0x08) || (workreq->cmnd[0] == 0x28) ||
(workreq->cmnd[0] == 0x0a) || (workreq->cmnd[0] == 0x2a)) {
j |= 0x0c;
}
outb(j,tmpcip);
tmpcip--;
} else if ((dev->dev_id == ATP880_DEVID1) ||
(dev->dev_id == ATP880_DEVID2)) {
tmpcip =tmpcip -2;
tmport = workport - 0x05;
if ((workreq->cmnd[0] == 0x08) || (workreq->cmnd[0] == 0x28) || (workreq->cmnd[0] == 0x0a) || (workreq->cmnd[0] == 0x2a)) {
outb((unsigned char) ((inb(tmport) & 0x3f) | 0xc0), tmport);
} else {
outb((unsigned char) (inb(tmport) & 0x3f), tmport);
}
} else {
tmpcip =tmpcip -2;
tmport = workport + 0x3a;
if ((workreq->cmnd[0] == 0x08) || (workreq->cmnd[0] == 0x28) || (workreq->cmnd[0] == 0x0a) || (workreq->cmnd[0] == 0x2a)) {
outb((inb(tmport) & 0xf3) | 0x08, tmport);
} else {
outb(inb(tmport) & 0xf3, tmport);
}
}
tmport = workport + 0x1c;
if(workreq->sc_data_direction == DMA_TO_DEVICE) {
dev->id[c][target_id].dirct = 0x20;
if (inb(tmport) == 0) {
tmport = workport + 0x18;
outb(0x08, tmport);
outb(0x01, tmpcip);
#ifdef ED_DBGP
printk( "start DMA(to target)\n");
#endif
} else {
dev->last_cmd[c] |= 0x40;
}
dev->in_snd[c] = 0;
return;
}
if (inb(tmport) == 0) {
tmport = workport + 0x18;
outb(0x08, tmport);
outb(0x09, tmpcip);
#ifdef ED_DBGP
printk( "start DMA(to host)\n");
#endif
} else {
dev->last_cmd[c] |= 0x40;
}
dev->in_snd[c] = 0;
return;
}
static unsigned char fun_scam(struct atp_unit *dev, unsigned short int *val)
{
unsigned int tmport;
unsigned short int i, k;