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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
|
/* $OpenBSD: ami.c,v 1.7 2001/04/30 15:45:05 mickey Exp $ */
/*
* Copyright (c) 2001 Michael Shalayeff
* All rights reserved.
*
* The SCSI emulation layer is derived from gdt(4) driver,
* Copyright (c) 1999, 2000 Niklas Hallqvist. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Michael Shalayeff.
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* American Megatrends Inc. MegaRAID controllers driver
*
* This driver was made because these ppl and organizations
* donated hardware and provided documentation:
*
* - 428 model card
* John Kerbawy, Stephan Matis, Mark Stovall;
*
* - 467 and 475 model cards, docs
* American Megatrends Inc.;
*
* - uninterruptable electric power for cvs
* Theo de Raadt.
*/
#define AMI_DEBUG
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/buf.h>
#include <sys/device.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <machine/bus.h>
#include <vm/vm.h>
#include <vm/vm_kern.h>
#include <uvm/uvm_extern.h>
#include <scsi/scsi_all.h>
#include <scsi/scsi_disk.h>
#include <scsi/scsiconf.h>
#include <dev/ic/amireg.h>
#include <dev/ic/amivar.h>
#ifdef AMI_DEBUG
#define AMI_DPRINTF(m,a) if (ami_debug & (m)) printf a
#define AMI_D_CMD 0x0001
#define AMI_D_INTR 0x0002
#define AMI_D_MISC 0x0004
#define AMI_D_DMA 0x0008
int ami_debug = 0
| AMI_D_CMD
| AMI_D_INTR
/* | AMI_D_MISC */
/* | AMI_D_DMA */
;
#else
#define AMI_DPRINTF(m,a) /* m, a */
#endif
struct cfdriver ami_cd = {
NULL, "ami", DV_DULL
};
int ami_scsi_cmd __P((struct scsi_xfer *xs));
void amiminphys __P((struct buf *bp));
struct scsi_adapter ami_switch = {
ami_scsi_cmd, amiminphys, 0, 0,
};
struct scsi_device ami_dev = {
NULL, NULL, NULL, NULL
};
int ami_scsi_raw_cmd __P((struct scsi_xfer *xs));
struct scsi_adapter ami_raw_switch = {
ami_scsi_raw_cmd, amiminphys, 0, 0,
};
struct scsi_device ami_raw_dev = {
NULL, NULL, NULL, NULL
};
static __inline struct ami_ccb *ami_get_ccb __P((struct ami_softc *sc));
static __inline void ami_put_ccb __P((struct ami_ccb *ccb));
void ami_copyhds __P((struct ami_softc *sc, const u_int32_t *sizes,
const u_int8_t *props, const u_int8_t *stats));
void *ami_allocmem __P((bus_dma_tag_t dmat, bus_dmamap_t *map,
bus_dma_segment_t *segp, size_t isize, size_t nent, const char *iname));
void ami_freemem __P((bus_dma_tag_t dmat, bus_dmamap_t *map,
bus_dma_segment_t *segp, size_t isize, size_t nent, const char *iname));
void ami_dispose __P((struct ami_softc *sc));
void ami_stimeout __P((void *v));
int ami_cmd __P((struct ami_ccb *ccb, int flags, int wait));
int ami_start __P((struct ami_ccb *ccb, int wait));
int ami_complete __P((struct ami_ccb *ccb));
int ami_done __P((struct ami_softc *sc, int idx));
void ami_copy_internal_data __P((struct scsi_xfer *xs, void *v, size_t size));
int ami_inquire __P((struct ami_softc *sc, u_int8_t op));
static __inline struct ami_ccb *
ami_get_ccb(sc)
struct ami_softc *sc;
{
struct ami_ccb *ccb;
ccb = TAILQ_LAST(&sc->sc_free_ccb, ami_queue_head);
if (ccb) {
TAILQ_REMOVE(&sc->sc_free_ccb, ccb, ccb_link);
ccb->ccb_state = AMI_CCB_READY;
}
return ccb;
}
static __inline void
ami_put_ccb(ccb)
struct ami_ccb *ccb;
{
struct ami_softc *sc = ccb->ccb_sc;
ccb->ccb_state = AMI_CCB_FREE;
TAILQ_INSERT_TAIL(&sc->sc_free_ccb, ccb, ccb_link);
}
void *
ami_allocmem(dmat, map, segp, isize, nent, iname)
bus_dma_tag_t dmat;
bus_dmamap_t *map;
bus_dma_segment_t *segp;
size_t isize, nent;
const char *iname;
{
size_t total = isize * nent;
caddr_t p;
int error, rseg;
/* XXX this is because we might have no dmamem_load_raw */
if ((error = bus_dmamem_alloc(dmat, total, PAGE_SIZE, 0, segp, 1,
&rseg, BUS_DMA_NOWAIT))) {
printf(": cannot allocate %s%s (%d)\n",
iname, nent==1? "": "s", error);
return (NULL);
}
if ((error = bus_dmamem_map(dmat, segp, rseg, total, &p,
BUS_DMA_NOWAIT))) {
printf(": cannot map %s%s (%d)\n",
iname, nent==1? "": "s", error);
return (NULL);
}
bzero(p, total);
if ((error = bus_dmamap_create(dmat, total, 1,
total, 0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, map))) {
printf(": cannot create %s dmamap (%d)\n", iname, error);
return (NULL);
}
if ((error = bus_dmamap_load(dmat, *map, p, total, NULL,
BUS_DMA_NOWAIT))) {
printf(": cannot load %s dma map (%d)\n", iname, error);
return (NULL);
}
return (p);
}
void
ami_freemem(dmat, map, segp, isize, nent, iname)
bus_dma_tag_t dmat;
bus_dmamap_t *map;
bus_dma_segment_t *segp;
size_t isize, nent;
const char *iname;
{
bus_dmamem_free(dmat, segp, 1);
bus_dmamap_destroy(dmat, *map);
*map = NULL;
}
void
ami_dispose(sc)
struct ami_softc *sc;
{
register struct ami_ccb *ccb;
/* traverse the ccbs and destroy the maps */
for (ccb = &sc->sc_ccbs[AMI_MAXCMDS - 1]; ccb > sc->sc_ccbs; ccb--)
if (ccb->ccb_dmamap)
bus_dmamap_destroy(sc->dmat, ccb->ccb_dmamap);
ami_freemem(sc->dmat, &sc->sc_sgmap, sc->sc_sgseg,
sizeof(struct ami_sgent) * AMI_SGEPERCMD, AMI_MAXCMDS, "sglist");
ami_freemem(sc->dmat, &sc->sc_cmdmap, sc->sc_cmdseg,
sizeof(struct ami_iocmd), AMI_MAXCMDS + 1, "command");
}
void
ami_copyhds(sc, sizes, props, stats)
struct ami_softc *sc;
const u_int32_t *sizes;
const u_int8_t *props, *stats;
{
int i;
for (i = 0; i < sc->sc_nunits; i++) {
sc->sc_hdr[i].hd_present = 1;
sc->sc_hdr[i].hd_is_logdrv = 1;
sc->sc_hdr[i].hd_size = sizes[i];
sc->sc_hdr[i].hd_prop = props[i];
sc->sc_hdr[i].hd_stat = stats[i];
if (sc->sc_hdr[i].hd_size > 0x200000) {
sc->sc_hdr[i].hd_heads = 255;
sc->sc_hdr[i].hd_secs = 63;
} else {
sc->sc_hdr[i].hd_heads = 64;
sc->sc_hdr[i].hd_secs = 32;
}
}
}
int
ami_attach(sc)
struct ami_softc *sc;
{
/* struct ami_rawsoftc *rsc; */
struct ami_ccb *ccb;
struct ami_iocmd *cmd;
struct ami_sgent *sg;
bus_dmamap_t idatamap;
bus_dma_segment_t idataseg[1];
const char *p;
void *idata;
int error;
if (!(idata = ami_allocmem(sc->dmat, &idatamap, idataseg,
NBPG, 1, "init data"))) {
ami_freemem(sc->dmat, &idatamap, idataseg,
NBPG, 1, "init data");
return 1;
}
sc->sc_cmds = ami_allocmem(sc->dmat, &sc->sc_cmdmap, sc->sc_cmdseg,
sizeof(struct ami_iocmd), AMI_MAXCMDS+1, "command");
if (!sc->sc_cmds) {
ami_dispose(sc);
ami_freemem(sc->dmat, &idatamap,
idataseg, NBPG, 1, "init data");
return 1;
}
sc->sc_sgents = ami_allocmem(sc->dmat, &sc->sc_sgmap, sc->sc_sgseg,
sizeof(struct ami_sgent) * AMI_SGEPERCMD, AMI_MAXCMDS+1, "sglist");
if (!sc->sc_sgents) {
ami_dispose(sc);
ami_freemem(sc->dmat, &idatamap,
idataseg, NBPG, 1, "init data");
return 1;
}
TAILQ_INIT(&sc->sc_ccbq);
TAILQ_INIT(&sc->sc_ccb2q);
TAILQ_INIT(&sc->sc_ccbdone);
TAILQ_INIT(&sc->sc_free_ccb);
/* 0th command is a mailbox */
for (ccb = &sc->sc_ccbs[AMI_MAXCMDS-1],
cmd = sc->sc_cmds + sizeof(*cmd) * AMI_MAXCMDS,
sg = sc->sc_sgents + sizeof(*sg) * AMI_MAXCMDS * AMI_SGEPERCMD;
cmd >= (struct ami_iocmd *)sc->sc_cmds;
cmd--, ccb--, sg -= AMI_SGEPERCMD) {
cmd->acc_id = cmd - (struct ami_iocmd *)sc->sc_cmds;
if (cmd->acc_id) {
error = bus_dmamap_create(sc->dmat,
AMI_MAXFER, AMI_MAXOFFSETS, AMI_MAXFER, 0,
BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
&ccb->ccb_dmamap);
if (error) {
printf(": cannot create ccb dmamap (%d)\n",
error);
ami_dispose(sc);
ami_freemem(sc->dmat, &idatamap,
idataseg, NBPG, 1, "init data");
return (1);
}
ccb->ccb_sc = sc;
ccb->ccb_cmd = cmd;
ccb->ccb_state = AMI_CCB_FREE;
ccb->ccb_cmdpa = sc->sc_cmdseg[0].ds_addr +
cmd->acc_id * sizeof(*cmd);
ccb->ccb_sglist = sg;
ccb->ccb_sglistpa = sc->sc_sgseg[0].ds_addr +
cmd->acc_id * sizeof(*sg) * AMI_SGEPERCMD;
TAILQ_INSERT_TAIL(&sc->sc_free_ccb, ccb, ccb_link);
} else {
sc->sc_mbox = cmd;
sc->sc_mbox_pa = sc->sc_cmdseg[0].ds_addr;
}
}
timeout_set(&sc->sc_poll_tmo, (void (*)__P((void *)))ami_intr, sc);
(sc->sc_init)(sc);
{
paddr_t pa = idataseg[0].ds_addr;
ami_lock_t lock;
lock = AMI_LOCK_AMI(sc);
ccb = ami_get_ccb(sc);
cmd = ccb->ccb_cmd;
/* try FC inquiry first */
cmd->acc_cmd = AMI_FCOP;
cmd->acc_io.aio_channel = AMI_FC_EINQ3;
cmd->acc_io.aio_param = AMI_FC_EINQ3_SOLICITED_FULL;
cmd->acc_io.aio_data = pa;
if (ami_cmd(ccb, 0, 1) == 0) {
struct ami_fc_einquiry *einq = idata;
struct ami_fc_prodinfo *pi = idata;
sc->sc_nunits = einq->ain_nlogdrv;
ami_copyhds(sc, einq->ain_ldsize, einq->ain_ldprop,
einq->ain_ldstat);
ccb = ami_get_ccb(sc);
cmd = ccb->ccb_cmd;
cmd->acc_cmd = AMI_FCOP;
cmd->acc_io.aio_channel = AMI_FC_PRODINF;
cmd->acc_io.aio_param = 0;
cmd->acc_io.aio_data = pa;
if (ami_cmd(ccb, 0, 1) == 0) {
sc->sc_maxunits = AMI_BIG_MAX_LDRIVES;
bcopy (pi->api_fwver, sc->sc_fwver, 16);
sc->sc_fwver[16] = '\0';
bcopy (pi->api_biosver, sc->sc_biosver, 16);
sc->sc_biosver[16] = '\0';
sc->sc_channels = pi->api_channels;
sc->sc_targets = pi->api_fcloops;
sc->sc_memory = pi->api_ramsize;
sc->sc_maxcmds = pi->api_maxcmd;
p = "FC loop";
}
}
if (sc->sc_maxunits == 0) {
struct ami_inquiry *inq = idata;
ccb = ami_get_ccb(sc);
cmd = ccb->ccb_cmd;
cmd->acc_cmd = AMI_EINQUIRY;
cmd->acc_io.aio_channel = 0;
cmd->acc_io.aio_param = 0;
cmd->acc_io.aio_data = pa;
if (ami_cmd(ccb, 0, 1) != 0) {
ccb = ami_get_ccb(sc);
cmd = ccb->ccb_cmd;
cmd->acc_cmd = AMI_INQUIRY;
cmd->acc_io.aio_channel = 0;
cmd->acc_io.aio_param = 0;
cmd->acc_io.aio_data = kvtop((caddr_t)&inq);
if (ami_cmd(ccb, 0, 1) != 0) {
AMI_UNLOCK_AMI(sc, lock);
printf(": cannot do inquiry\n");
ami_dispose(sc);
ami_freemem(sc->dmat, &idatamap,
idataseg, NBPG, 1, "init data");
return (1);
}
}
sc->sc_maxunits = AMI_MAX_LDRIVES;
sc->sc_nunits = inq->ain_nlogdrv;
ami_copyhds(sc, inq->ain_ldsize, inq->ain_ldprop,
inq->ain_ldstat);
bcopy (inq->ain_fwver, sc->sc_fwver, 4);
sc->sc_fwver[4] = '\0';
bcopy (inq->ain_biosver, sc->sc_biosver, 4);
sc->sc_biosver[4] = '\0';
sc->sc_channels = inq->ain_channels;
sc->sc_targets = inq->ain_targets;
sc->sc_memory = inq->ain_ramsize;
sc->sc_maxcmds = inq->ain_maxcmd;
p = "target";
}
AMI_UNLOCK_AMI(sc, lock);
if (sc->sc_maxcmds > AMI_MAXCMDS)
sc->sc_maxcmds = 1 /* AMI_MAXCMDS */;
}
ami_freemem(sc->dmat, &idatamap, idataseg, NBPG, 1, "init data");
/* hack for hp netraid version encoding */
if ('A' <= sc->sc_fwver[2] && sc->sc_fwver[2] <= 'Z' &&
sc->sc_fwver[1] < ' ' && sc->sc_fwver[0] < ' ' &&
'A' <= sc->sc_biosver[2] && sc->sc_biosver[2] <= 'Z' &&
sc->sc_biosver[1] < ' ' && sc->sc_biosver[0] < ' ') {
sprintf(sc->sc_fwver, "%c.%02d.%02d", sc->sc_fwver[2],
sc->sc_fwver[1], sc->sc_fwver[0]);
sprintf(sc->sc_biosver, "%c.%02d.%02d", sc->sc_biosver[2],
sc->sc_biosver[1], sc->sc_biosver[0]);
}
printf(": FW %s, BIOS v%s, %dMB RAM\n"
"%s: %d channels, %d %ss, %d logical drives\n",
sc->sc_fwver, sc->sc_biosver, sc->sc_memory,
sc->sc_dev.dv_xname,
sc->sc_channels, sc->sc_targets, p, sc->sc_nunits);
/* TODO: fetch & print cache strategy */
/* TODO: fetch & print scsi and raid info */
sc->sc_link.device = &ami_dev;
sc->sc_link.openings = sc->sc_maxcmds;
sc->sc_link.adapter_softc = sc;
sc->sc_link.adapter = &ami_switch;
sc->sc_link.adapter_target = sc->sc_maxunits;
sc->sc_link.adapter_buswidth = sc->sc_maxunits;
config_found(&sc->sc_dev, &sc->sc_link, scsiprint);
#if 0
rsc = malloc(sizeof(struct ami_rawsoftc) * sc->sc_channels,
M_DEVBUF, M_NOWAIT);
if (!rsc) {
printf("%s: no memory for raw interface\n",
sc->sc_dev.dv_xname);
return (0);
}
bzero(rsc, sizeof(struct ami_rawsoftc) * sc->sc_channels);
for (sc->sc_rawsoftcs = rsc;
rsc < &sc->sc_rawsoftcs[sc->sc_channels]; rsc++) {
/* TODO fetch and print channel properties */
rsc->sc_softc = sc;
rsc->sc_channel = rsc - sc->sc_rawsoftcs;
rsc->sc_link.device = &ami_raw_dev;
rsc->sc_link.openings = sc->sc_maxcmds;
rsc->sc_link.adapter_softc = rsc;
rsc->sc_link.adapter = &ami_raw_switch;
/* TODO fetch it from the controller */
rsc->sc_link.adapter_target = sc->sc_targets;
rsc->sc_link.adapter_buswidth = sc->sc_targets;
config_found(&sc->sc_dev, &rsc->sc_link, scsiprint);
}
#endif
return 0;
}
int
ami_quartz_init(sc)
struct ami_softc *sc;
{
return 0;
}
int
ami_quartz_exec(sc, cmd)
struct ami_softc *sc;
struct ami_iocmd *cmd;
{
u_int32_t qidb;
qidb = bus_space_read_4(sc->iot, sc->ioh, AMI_QIDB);
if (qidb & htole32((AMI_QIDB_EXEC | AMI_QIDB_ACK)))
return (EBUSY);
*sc->sc_mbox = *cmd;
qidb = sc->sc_mbox_pa | AMI_QIDB_EXEC;
bus_space_write_4(sc->iot, sc->ioh, AMI_QIDB, htole32(qidb));
DELAY(10);
return 0;
}
int
ami_quartz_done(sc, mbox)
struct ami_softc *sc;
struct ami_iocmd *mbox;
{
u_int32_t qdb;
/* do not scramble the busy mailbox */
if (sc->sc_mbox->acc_busy)
return (0);
qdb = bus_space_read_4(sc->iot, sc->ioh, AMI_QIDB);
if (qdb & htole32((AMI_QIDB_EXEC | AMI_QIDB_ACK)))
return (0);
qdb = bus_space_read_4(sc->iot, sc->ioh, AMI_QODB);
if (letoh32(qdb) == AMI_QODB_READY) {
bus_dmamap_sync(sc->dmat, sc->sc_cmdmap, BUS_DMASYNC_POSTREAD);
*mbox = *sc->sc_mbox;
/* ack interrupt */
bus_space_write_4(sc->iot, sc->ioh, AMI_QODB, AMI_QODB_READY);
qdb = sc->sc_mbox_pa | AMI_QIDB_ACK;
bus_space_write_4(sc->iot, sc->ioh, AMI_QIDB, htole32(qdb));
DELAY(10);
return (1);
}
return (0);
}
int
ami_schwartz_init(sc)
struct ami_softc *sc;
{
u_int32_t a = (u_int32_t)sc->sc_mbox_pa;
bus_space_write_4(sc->iot, sc->ioh, AMI_SMBADDR, a);
/* XXX 40bit address ??? */
bus_space_write_1(sc->iot, sc->ioh, AMI_SMBENA, 0);
bus_space_write_1(sc->iot, sc->ioh, AMI_SCMD, AMI_SCMD_ACK);
bus_space_write_1(sc->iot, sc->ioh, AMI_SIEM, AMI_SEIM_ENA |
bus_space_read_1(sc->iot, sc->ioh, AMI_SIEM));
return 0;
}
int
ami_schwartz_exec(sc, cmd)
struct ami_softc *sc;
struct ami_iocmd *cmd;
{
if (bus_space_read_1(sc->iot, sc->ioh, AMI_SMBSTAT) & AMI_SMBST_BUSY)
return EBUSY;
*sc->sc_mbox = *cmd;
bus_space_write_1(sc->iot, sc->ioh, AMI_SCMD, AMI_SCMD_EXEC);
return 0;
}
int
ami_schwartz_done(sc, mbox)
struct ami_softc *sc;
struct ami_iocmd *mbox;
{
u_int8_t stat;
/* do not scramble the busy mailbox */
if (sc->sc_mbox->acc_busy)
return (0);
if (bus_space_read_1(sc->iot, sc->ioh, AMI_SMBSTAT) & AMI_SMBST_BUSY)
return 0;
stat = bus_space_read_1(sc->iot, sc->ioh, AMI_ISTAT);
if (stat & AMI_ISTAT_PEND) {
bus_space_write_1(sc->iot, sc->ioh, AMI_ISTAT, stat);
*mbox = *sc->sc_mbox;
bus_space_write_1(sc->iot, sc->ioh, AMI_SCMD, AMI_SCMD_ACK);
return 1;
}
return 0;
}
int
ami_cmd(ccb, flags, wait)
struct ami_ccb *ccb;
int flags, wait;
{
struct ami_softc *sc = ccb->ccb_sc;
bus_dmamap_t dmap = ccb->ccb_dmamap;
int error = 0, i;
if (ccb->ccb_data) {
struct ami_iocmd *cmd = ccb->ccb_cmd;
bus_dma_segment_t *sgd;
error = bus_dmamap_load(sc->dmat, dmap, ccb->ccb_data,
ccb->ccb_len, NULL, flags);
if (error) {
if (error == EFBIG)
printf("more than %d dma segs\n", AMI_MAXOFFSETS);
else
printf("error %d loading dma map\n", error);
ami_put_ccb(ccb);
return error;
}
sgd = dmap->dm_segs;
AMI_DPRINTF(AMI_D_DMA, ("data=%p/%u<0x%lx/%u",
ccb->ccb_data, ccb->ccb_len,
sgd->ds_addr, sgd->ds_len));
if(dmap->dm_nsegs > 1) {
struct ami_sgent *sgl = ccb->ccb_sglist;
cmd->acc_mbox.amb_nsge = htole32(dmap->dm_nsegs);
cmd->acc_mbox.amb_data = htole32(ccb->ccb_sglistpa);
for (i = 0; i < dmap->dm_nsegs; i++, sgd++) {
sgl[i].asg_addr = htole32(sgd->ds_addr);
sgl[i].asg_len = htole32(sgd->ds_len);
if (i)
AMI_DPRINTF(AMI_D_DMA, (",0x%lx/%u",
sgd->ds_addr, sgd->ds_len));
}
} else {
cmd->acc_mbox.amb_nsge = htole32(0);
cmd->acc_mbox.amb_data = htole32(sgd->ds_addr);
}
AMI_DPRINTF(AMI_D_DMA, ("> "));
bus_dmamap_sync(sc->dmat, dmap, BUS_DMASYNC_PREWRITE);
}
bus_dmamap_sync(sc->dmat, sc->sc_cmdmap, BUS_DMASYNC_PREWRITE);
if ((error = ami_start(ccb, wait))) {
AMI_DPRINTF(AMI_D_DMA, ("error=%d ", error));
__asm __volatile(".globl _bpamierr\n_bpamierr:");
if (ccb->ccb_data)
bus_dmamap_unload(sc->dmat, dmap);
ami_put_ccb(ccb);
} else if (wait) {
AMI_DPRINTF(AMI_D_DMA, ("waiting "));
if ((error = ami_complete(ccb)))
ami_put_ccb(ccb);
}
return error;
}
int
ami_start(ccb, wait)
struct ami_ccb *ccb;
int wait;
{
struct ami_softc *sc = ccb->ccb_sc;
struct ami_iocmd *cmd = ccb->ccb_cmd;
struct scsi_xfer *xs = ccb->ccb_xs;
volatile struct ami_iocmd *mbox = sc->sc_mbox;
int i;
AMI_DPRINTF(AMI_D_CMD, ("start(%d) ", cmd->acc_id));
if (ccb->ccb_state != AMI_CCB_READY) {
printf("%s: ccb %d not ready <%d>\n",
sc->sc_dev.dv_xname, cmd->acc_id, ccb->ccb_state);
return EINVAL;
}
if (xs)
timeout_set(&xs->stimeout, ami_stimeout, ccb);
if (mbox->acc_busy) {
if (!wait) {
AMI_DPRINTF(AMI_D_CMD, ("2queue(%d) ", cmd->acc_id));
ccb->ccb_state = AMI_CCB_PREQUEUED;
if (xs)
timeout_add(&xs->stimeout, 1);
return (xs? 0 : EBUSY);
}
for (i = 100000; i-- && mbox->acc_busy; DELAY(10));
if (mbox->acc_busy) {
AMI_DPRINTF(AMI_D_CMD, ("mbox_busy "));
return (EAGAIN);
}
}
AMI_DPRINTF(AMI_D_CMD, ("exec "));
cmd->acc_busy = 1;
cmd->acc_poll = 0;
cmd->acc_ack = 0;
if (!(i = (sc->sc_exec)(sc, cmd))) {
ccb->ccb_state = AMI_CCB_QUEUED;
TAILQ_INSERT_TAIL(&sc->sc_ccbq, ccb, ccb_link);
if (!wait) {
#ifdef AMI_POLLING
if (!timeout_pending(&sc->sc_poll_tmo))
timeout_add(&sc->sc_poll_tmo, 1);
#endif
if (xs)
/*timeout_add(&xs->stimeout, hz * xs->timeout / 1000)*/;
}
} else if (!wait) {
AMI_DPRINTF(AMI_D_CMD, ("2queue1(%d) ", cmd->acc_id));
ccb->ccb_state = AMI_CCB_PREQUEUED;
if (xs) {
timeout_add(&xs->stimeout, 1);
return (0);
}
}
return i;
}
void
ami_stimeout(v)
void *v;
{
struct ami_ccb *ccb = v;
struct ami_softc *sc = ccb->ccb_sc;
struct scsi_xfer *xs = ccb->ccb_xs;
struct ami_iocmd *cmd = ccb->ccb_cmd;
volatile struct ami_iocmd *mbox = sc->sc_mbox;
ami_lock_t lock;
switch (ccb->ccb_state) {
case AMI_CCB_PREQUEUED:
if (mbox->acc_busy) {
timeout_add(&xs->stimeout, 1);
return;
}
AMI_DPRINTF(AMI_D_CMD, ("requeue(%d) ", cmd->acc_id));
ccb->ccb_state = AMI_CCB_READY;
lock = AMI_LOCK_AMI(sc);
if (ami_start(ccb, 0)) {
AMI_DPRINTF(AMI_D_CMD, ("requeue(%d) again\n", cmd->acc_id));
ccb->ccb_state = AMI_CCB_PREQUEUED;
timeout_add(&xs->stimeout, 1);
}
AMI_UNLOCK_AMI(sc, lock);
break;
case AMI_CCB_QUEUED:
/* XXX need to kill all cmds in the queue and reset the card */
AMI_DPRINTF(AMI_D_CMD, ("timeout(%d) ", cmd->acc_id));
lock = AMI_LOCK_AMI(sc);
TAILQ_REMOVE(&sc->sc_ccbq, ccb, ccb_link);
ami_put_ccb(ccb);
AMI_UNLOCK_AMI(sc, lock);
xs->error = XS_TIMEOUT;
scsi_done(xs);
break;
case AMI_CCB_FREE:
case AMI_CCB_READY:
panic("ami_stimeout(%p) botch", cmd->acc_id);
}
}
int
ami_complete(ccb)
struct ami_ccb *ccb;
{
struct ami_softc *sc = ccb->ccb_sc;
struct scsi_xfer *xs = ccb->ccb_xs;
struct ami_iocmd mbox;
int i, j, rv, status;
for (rv = 1, status = 0, i = 100 * (xs? xs->timeout: 1000);
!status && rv && i--; DELAY(10))
if ((sc->sc_done)(sc, &mbox)) {
AMI_DPRINTF(AMI_D_CMD, ("got#%d ", mbox.acc_nstat));
status = mbox.acc_status;
for (j = 0; j < mbox.acc_nstat; j++ ) {
int ready = mbox.acc_cmplidl[j];
AMI_DPRINTF(AMI_D_CMD, ("ready=%x ", ready));
if (!ami_done(sc, ready) &&
ccb->ccb_cmd->acc_id == ready)
rv = 0;
}
}
if (status) {
AMI_DPRINTF(AMI_D_CMD, ("aborted\n"));
} else if (!rv) {
AMI_DPRINTF(AMI_D_CMD, ("complete\n"));
} else if (i < 0) {
AMI_DPRINTF(AMI_D_CMD, ("timeout\n"));
} else
AMI_DPRINTF(AMI_D_CMD, ("screwed\n"));
return rv? rv : status;
}
int
ami_done(sc, idx)
struct ami_softc *sc;
int idx;
{
struct ami_ccb *ccb = &sc->sc_ccbs[idx - 1];
struct scsi_xfer *xs = ccb->ccb_xs;
ami_lock_t lock;
AMI_DPRINTF(AMI_D_CMD, ("done(%d) ", ccb->ccb_cmd->acc_id));
if (ccb->ccb_state != AMI_CCB_QUEUED) {
printf("%s: unqueued ccb %d ready, state = %d\n",
sc->sc_dev.dv_xname, idx, ccb->ccb_state);
return 1;
}
lock = AMI_LOCK_AMI(sc);
TAILQ_REMOVE(&sc->sc_ccbq, ccb, ccb_link);
ccb->ccb_state = AMI_CCB_READY;
AMI_UNLOCK_AMI(sc, lock);
if (xs) {
timeout_del(&xs->stimeout);
if (xs->cmd->opcode != PREVENT_ALLOW &&
xs->cmd->opcode != SYNCHRONIZE_CACHE) {
bus_dmamap_sync(sc->dmat, ccb->ccb_dmamap,
(xs->flags & SCSI_DATA_IN) ?
BUS_DMASYNC_POSTREAD :
BUS_DMASYNC_POSTWRITE);
bus_dmamap_unload(sc->dmat, ccb->ccb_dmamap);
}
} else {
struct ami_iocmd *cmd = ccb->ccb_cmd;
switch (cmd->acc_cmd) {
case AMI_INQUIRY:
case AMI_EINQUIRY:
case AMI_EINQUIRY3:
bus_dmamap_sync(sc->dmat, ccb->ccb_dmamap,
BUS_DMASYNC_POSTREAD);
bus_dmamap_unload(sc->dmat, ccb->ccb_dmamap);
break;
default:
/* no data */
}
}
lock = AMI_LOCK_AMI(sc);
ami_put_ccb(ccb);
AMI_UNLOCK_AMI(sc, lock);
if (xs) {
xs->resid = 0;
xs->flags |= ITSDONE;
AMI_DPRINTF(AMI_D_CMD, ("scsi_done(%d) ", idx));
scsi_done(xs);
}
return 0;
}
void
amiminphys(bp)
struct buf *bp;
{
if (bp->b_bcount > AMI_MAXFER)
bp->b_bcount = AMI_MAXFER;
minphys(bp);
}
void
ami_copy_internal_data(xs, v, size)
struct scsi_xfer *xs;
void *v;
size_t size;
{
size_t copy_cnt;
AMI_DPRINTF(AMI_D_MISC, ("ami_copy_internal_data "));
if (!xs->datalen)
printf("uio move not yet supported\n");
else {
copy_cnt = MIN(size, xs->datalen);
bcopy(v, xs->data, copy_cnt);
}
}
int
ami_scsi_raw_cmd(xs)
struct scsi_xfer *xs;
{
struct scsi_link *link = xs->sc_link;
struct ami_rawsoftc *rsc = link->adapter_softc;
struct ami_softc *sc = rsc->sc_softc;
u_int8_t channel = rsc->sc_channel, target = link->target;
struct ami_ccb *ccb, *ccb1;
struct ami_iocmd *cmd;
struct ami_passthrough *ps;
int error;
ami_lock_t lock;
AMI_DPRINTF(AMI_D_CMD, ("ami_scsi_raw_cmd "));
if (xs->cmdlen > AMI_MAX_CDB) {
AMI_DPRINTF(AMI_D_CMD, ("CDB too big %p ", xs));
bzero(&xs->sense, sizeof(xs->sense));
xs->sense.error_code = SSD_ERRCODE_VALID | 0x70;
xs->sense.flags = SKEY_ILLEGAL_REQUEST;
xs->sense.add_sense_code = 0x20; /* illcmd, 0x24 illfield */
xs->error = XS_SENSE;
scsi_done(xs);
return (COMPLETE);
}
xs->error = XS_NOERROR;
lock = AMI_LOCK_AMI(sc);
if ((ccb = ami_get_ccb(sc)) == NULL) {
AMI_UNLOCK_AMI(sc, lock);
xs->error = XS_DRIVER_STUFFUP;
scsi_done(xs);
return (COMPLETE);
}
if ((ccb1 = ami_get_ccb(sc)) == NULL) {
ami_put_ccb(ccb);
AMI_UNLOCK_AMI(sc, lock);
xs->error = XS_DRIVER_STUFFUP;
scsi_done(xs);
return (COMPLETE);
}
ccb->ccb_xs = xs;
ccb->ccb_ccb1 = ccb1;
ccb->ccb_len = xs->datalen;
ccb->ccb_data = xs->data;
ps = (struct ami_passthrough *)ccb1->ccb_cmd;
ps->apt_param = AMI_PTPARAM(AMI_TIMEOUT_6,1,0);
ps->apt_channel = channel;
ps->apt_target = target;
bcopy(xs->cmd, ps->apt_cdb, AMI_MAX_CDB);
ps->apt_ncdb = xs->cmdlen;
ps->apt_nsense = AMI_MAX_SENSE;
cmd = ccb->ccb_cmd;
cmd->acc_cmd = AMI_PASSTHRU;
cmd->acc_passthru.apt_data = ccb1->ccb_cmdpa;
if ((error = ami_cmd(ccb, ((xs->flags & SCSI_NOSLEEP)?
BUS_DMA_NOWAIT : BUS_DMA_WAITOK), xs->flags & SCSI_POLL))) {
AMI_UNLOCK_AMI(sc, lock);
AMI_DPRINTF(AMI_D_CMD, ("failed %p ", xs));
if (xs->flags & SCSI_POLL) {
xs->error = XS_TIMEOUT;
return (TRY_AGAIN_LATER);
} else {
xs->error = XS_DRIVER_STUFFUP;
scsi_done(xs);
return (COMPLETE);
}
}
AMI_UNLOCK_AMI(sc, lock);
if (xs->flags & SCSI_POLL) {
scsi_done(xs);
return (COMPLETE);
}
return (SUCCESSFULLY_QUEUED);
}
int
ami_scsi_cmd(xs)
struct scsi_xfer *xs;
{
struct scsi_link *link = xs->sc_link;
struct ami_softc *sc = link->adapter_softc;
struct ami_ccb *ccb;
struct ami_iocmd *cmd;
struct scsi_inquiry_data inq;
struct scsi_sense_data sd;
struct {
struct scsi_mode_header hd;
struct scsi_blk_desc bd;
union scsi_disk_pages dp;
} mpd;
struct scsi_read_cap_data rcd;
u_int8_t target = link->target;
u_int32_t blockno, blockcnt;
struct scsi_rw *rw;
struct scsi_rw_big *rwb;
int error, flags;
ami_lock_t lock;
if (target >= sc->sc_nunits || !sc->sc_hdr[target].hd_present ||
link->lun != 0) {
/* XXX should be XS_SENSE and sense filled out */
xs->error = XS_DRIVER_STUFFUP;
xs->flags |= ITSDONE;
scsi_done(xs);
return (COMPLETE);
}
AMI_DPRINTF(AMI_D_CMD, ("ami_scsi_cmd "));
error = 0;
xs->error = XS_NOERROR;
switch (xs->cmd->opcode) {
case TEST_UNIT_READY:
case START_STOP:
#if 0
case VERIFY:
#endif
AMI_DPRINTF(AMI_D_CMD, ("opc %d tgt %d ", xs->cmd->opcode,
target));
break;
case REQUEST_SENSE:
AMI_DPRINTF(AMI_D_CMD, ("REQUEST SENSE tgt %d ", target));
bzero(&sd, sizeof sd);
sd.error_code = 0x70;
sd.segment = 0;
sd.flags = SKEY_NO_SENSE;
*(u_int32_t*)sd.info = htole32(0);
sd.extra_len = 0;
ami_copy_internal_data(xs, &sd, sizeof sd);
break;
case INQUIRY:
AMI_DPRINTF(AMI_D_CMD, ("INQUIRY tgt %d ", target));
bzero(&inq, sizeof inq);
inq.device = T_DIRECT;
inq.dev_qual2 = 0;
inq.version = 2;
inq.response_format = 2;
inq.additional_length = 32;
strcpy(inq.vendor, "AMI ");
sprintf(inq.product, "Host drive #%02d", target);
strcpy(inq.revision, " ");
ami_copy_internal_data(xs, &inq, sizeof inq);
break;
case MODE_SENSE:
AMI_DPRINTF(AMI_D_CMD, ("MODE SENSE tgt %d ", target));
bzero(&mpd, sizeof mpd);
switch (((struct scsi_mode_sense *)xs->cmd)->page) {
case 4:
/* scsi_disk.h says this should be 0x16 */
mpd.dp.rigid_geometry.pg_length = 0x16;
mpd.hd.data_length = sizeof mpd.hd + sizeof mpd.bd +
mpd.dp.rigid_geometry.pg_length;
mpd.hd.blk_desc_len = sizeof mpd.bd;
mpd.hd.dev_spec = 0; /* writeprotect ? XXX */
_lto3b(AMI_SECTOR_SIZE, mpd.bd.blklen);
mpd.dp.rigid_geometry.pg_code = 4;
_lto3b(sc->sc_hdr[target].hd_size /
sc->sc_hdr[target].hd_heads /
sc->sc_hdr[target].hd_secs,
mpd.dp.rigid_geometry.ncyl);
mpd.dp.rigid_geometry.nheads =
sc->sc_hdr[target].hd_heads;
ami_copy_internal_data(xs, (u_int8_t *)&mpd,
sizeof mpd);
break;
default:
printf("%s: mode sense page %d not simulated\n",
sc->sc_dev.dv_xname,
((struct scsi_mode_sense *)xs->cmd)->page);
xs->error = XS_DRIVER_STUFFUP;
}
break;
case READ_CAPACITY:
AMI_DPRINTF(AMI_D_CMD, ("READ CAPACITY tgt %d ", target));
bzero(&rcd, sizeof rcd);
_lto4b(sc->sc_hdr[target].hd_size - 1, rcd.addr);
_lto4b(AMI_SECTOR_SIZE, rcd.length);
ami_copy_internal_data(xs, &rcd, sizeof rcd);
break;
case PREVENT_ALLOW:
AMI_DPRINTF(AMI_D_CMD, ("PREVENT/ALLOW "));
return (COMPLETE);
case SYNCHRONIZE_CACHE:
AMI_DPRINTF(AMI_D_CMD, ("SYNCHRONIZE CACHE "));
error++;
case READ_COMMAND:
if (!error) {
AMI_DPRINTF(AMI_D_CMD, ("READ "));
error++;
}
case READ_BIG:
if (!error) {
AMI_DPRINTF(AMI_D_CMD, ("READ BIG "));
error++;
}
case WRITE_COMMAND:
if (!error) {
AMI_DPRINTF(AMI_D_CMD, ("WRITE "));
error++;
}
case WRITE_BIG:
if (!error) {
AMI_DPRINTF(AMI_D_CMD, ("WRITE BIG "));
error++;
}
lock = AMI_LOCK_AMI(sc);
flags = 0;
if (xs->cmd->opcode != SYNCHRONIZE_CACHE) {
/* A read or write operation. */
if (xs->cmdlen == 6) {
rw = (struct scsi_rw *)xs->cmd;
blockno = _3btol(rw->addr) &
(SRW_TOPADDR << 16 | 0xffff);
blockcnt = rw->length ? rw->length : 0x100;
} else {
rwb = (struct scsi_rw_big *)xs->cmd;
blockno = _4btol(rwb->addr);
blockcnt = _2btol(rwb->length);
/* TODO: reflect DPO & FUA flags */
if (xs->cmd->opcode == WRITE_BIG &&
rwb->byte2 & 0x18)
flags = 0;
}
if (blockno >= sc->sc_hdr[target].hd_size ||
blockno + blockcnt > sc->sc_hdr[target].hd_size) {
AMI_UNLOCK_AMI(sc, lock);
printf("%s: out of bounds %u-%u >= %u\n",
sc->sc_dev.dv_xname, blockno, blockcnt,
sc->sc_hdr[target].hd_size);
xs->error = XS_DRIVER_STUFFUP;
break;
}
}
if ((ccb = ami_get_ccb(sc)) == NULL) {
AMI_UNLOCK_AMI(sc, lock);
AMI_DPRINTF(AMI_D_CMD, ("no more ccbs "));
xs->error = XS_DRIVER_STUFFUP;
__asm __volatile(".globl _bpamiccb\n_bpamiccb:");
break;
}
ccb->ccb_xs = xs;
ccb->ccb_ccb1 = NULL;
ccb->ccb_len = xs->datalen;
ccb->ccb_data = xs->data;
cmd = ccb->ccb_cmd;
cmd->acc_mbox.amb_nsect = blockcnt;
cmd->acc_mbox.amb_lba = blockno;
cmd->acc_mbox.amb_ldn = target;
cmd->acc_mbox.amb_data = 0;
switch (xs->cmd->opcode) {
case SYNCHRONIZE_CACHE:
cmd->acc_cmd = AMI_FLUSH;
/* XXX do other fields matter ? */
break;
case READ_COMMAND: case READ_BIG:
cmd->acc_cmd = AMI_READ;
break;
case WRITE_COMMAND: case WRITE_BIG:
cmd->acc_cmd = AMI_WRITE;
break;
}
if ((error = ami_cmd(ccb, ((xs->flags & SCSI_NOSLEEP)?
BUS_DMA_NOWAIT : BUS_DMA_WAITOK), xs->flags & SCSI_POLL))) {
AMI_UNLOCK_AMI(sc, lock);
AMI_DPRINTF(AMI_D_CMD, ("failed %p ", xs));
__asm __volatile(".globl _bpamifail\n_bpamifail:");
if (xs->flags & SCSI_POLL) {
xs->error = XS_TIMEOUT;
return (TRY_AGAIN_LATER);
} else {
xs->error = XS_DRIVER_STUFFUP;
break;
}
}
AMI_UNLOCK_AMI(sc, lock);
if (xs->flags & SCSI_POLL)
break;
return (SUCCESSFULLY_QUEUED);
default:
AMI_DPRINTF(AMI_D_CMD, ("unknown opc %d ", xs->cmd->opcode));
xs->error = XS_DRIVER_STUFFUP;
}
xs->flags |= ITSDONE;
scsi_done(xs);
return (xs->flags & SCSI_POLL? COMPLETE : SUCCESSFULLY_QUEUED);
}
int
ami_intr(v)
void *v;
{
struct ami_softc *sc = v;
struct ami_iocmd mbox;
int i, rv = 0;
ami_lock_t lock;
if (TAILQ_EMPTY(&sc->sc_ccbq))
return (0);
AMI_DPRINTF(AMI_D_INTR, ("intr "));
lock = AMI_LOCK_AMI(sc);
while ((sc->sc_done)(sc, &mbox)) {
AMI_UNLOCK_AMI(sc, lock);
AMI_DPRINTF(AMI_D_CMD, ("got#%d ", mbox.acc_nstat));
for (i = 0; i < mbox.acc_nstat; i++ ) {
register int ready = mbox.acc_cmplidl[i];
AMI_DPRINTF(AMI_D_CMD, ("ready=%d ", ready));
if (!ami_done(sc, ready))
rv = 1;
}
lock = AMI_LOCK_AMI(sc);
}
#ifdef AMI_POLLING
if (!TAILQ_EMPTY(&sc->sc_ccbq) && !timeout_pending(&sc->sc_poll_tmo)) {
AMI_DPRINTF(AMI_D_INTR, ("tmo "));
timeout_add(&sc->sc_poll_tmo, 100);
}
#endif
AMI_UNLOCK_AMI(sc, lock);
return rv;
}
|