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
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
|
/*-
* Copyright (c) 1999 Michael Smith
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``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 CONTRIBUTORS 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 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.
*
* $FreeBSD$
*/
/*
* Driver for the AMI MegaRaid family of controllers
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/malloc.h>
#include <sys/kernel.h>
#include <sys/buf.h>
#include <sys/bus.h>
#include <sys/conf.h>
#include <sys/devicestat.h>
#include <sys/disk.h>
#include <machine/resource.h>
#include <machine/bus.h>
#include <machine/clock.h>
#include <sys/rman.h>
#include <dev/amr/amrio.h>
#include <dev/amr/amrreg.h>
#include <dev/amr/amrvar.h>
#if 0
#define debug(fmt, args...) printf("%s: " fmt "\n", __FUNCTION__ , ##args)
#else
#define debug(fmt, args...)
#endif
#define AMR_CDEV_MAJOR 132
static struct cdevsw amr_cdevsw = {
/* open */ amr_open,
/* close */ amr_close,
/* read */ noread,
/* write */ nowrite,
/* ioctl */ amr_ioctl,
/* poll */ nopoll,
/* mmap */ nommap,
/* strategy */ nostrategy,
/* name */ "amr",
/* maj */ AMR_CDEV_MAJOR,
/* dump */ nodump,
/* psize */ nopsize,
/* flags */ 0,
/* bmaj */ 254 /* XXX magic no-bdev */
};
static int cdev_registered = 0;
devclass_t amr_devclass;
/*
* Command wrappers
*/
static int amr_query_controller(struct amr_softc *sc);
static void *amr_enquiry(struct amr_softc *sc, size_t bufsize,
u_int8_t cmd, u_int8_t cmdsub, u_int8_t cmdqual);
static int amr_flush(struct amr_softc *sc);
static void amr_startio(struct amr_softc *sc);
static void amr_completeio(struct amr_command *ac);
/*
* Command processing.
*/
static int amr_wait_command(struct amr_command *ac);
static int amr_poll_command(struct amr_command *ac);
static int amr_getslot(struct amr_command *ac);
static void amr_mapcmd(struct amr_command *ac);
static void amr_unmapcmd(struct amr_command *ac);
static int amr_start(struct amr_command *ac);
static int amr_done(struct amr_softc *sc);
static void amr_complete(struct amr_softc *sc);
/*
* Command buffer allocation.
*/
static struct amr_command *amr_alloccmd(struct amr_softc *sc);
static void amr_releasecmd(struct amr_command *ac);
static void amr_freecmd(struct amr_command *ac);
/*
* Interface-specific shims
*/
static void amr_quartz_submit_command(struct amr_softc *sc);
static int amr_quartz_get_work(struct amr_softc *sc, struct amr_mailbox *mbsave);
static void amr_quartz_attach_mailbox(struct amr_softc *sc);
static void amr_std_submit_command(struct amr_softc *sc);
static int amr_std_get_work(struct amr_softc *sc, struct amr_mailbox *mbsave);
static void amr_std_attach_mailbox(struct amr_softc *sc);
/*
* Debugging
*/
static void amr_printcommand(struct amr_command *ac);
/********************************************************************************
********************************************************************************
Public Interfaces
********************************************************************************
********************************************************************************/
/********************************************************************************
* Free all of the resources associated with (sc)
*
* Should not be called if the controller is active.
*/
void
amr_free(struct amr_softc *sc)
{
struct amr_command *ac;
u_int8_t *p;
debug("called");
/* throw away any command buffers */
while ((ac = TAILQ_FIRST(&sc->amr_freecmds)) != NULL) {
TAILQ_REMOVE(&sc->amr_freecmds, ac, ac_link);
amr_freecmd(ac);
}
/* destroy data-transfer DMA tag */
if (sc->amr_buffer_dmat)
bus_dma_tag_destroy(sc->amr_buffer_dmat);
/* free and destroy DMA memory and tag for s/g lists */
if (sc->amr_sgtable)
bus_dmamem_free(sc->amr_sg_dmat, sc->amr_sgtable, sc->amr_sg_dmamap);
if (sc->amr_sg_dmat)
bus_dma_tag_destroy(sc->amr_sg_dmat);
/* free and destroy DMA memory and tag for mailbox */
if (sc->amr_mailbox) {
p = (u_int8_t *)sc->amr_mailbox;
bus_dmamem_free(sc->amr_sg_dmat, p - 16, sc->amr_sg_dmamap);
}
if (sc->amr_sg_dmat)
bus_dma_tag_destroy(sc->amr_sg_dmat);
/* disconnect the interrupt handler */
if (sc->amr_intr)
bus_teardown_intr(sc->amr_dev, sc->amr_irq, sc->amr_intr);
if (sc->amr_irq != NULL)
bus_release_resource(sc->amr_dev, SYS_RES_IRQ, 0, sc->amr_irq);
/* destroy the parent DMA tag */
if (sc->amr_parent_dmat)
bus_dma_tag_destroy(sc->amr_parent_dmat);
/* release the register window mapping */
if (sc->amr_reg != NULL)
bus_release_resource(sc->amr_dev,
(sc->amr_type == AMR_TYPE_QUARTZ) ? SYS_RES_MEMORY : SYS_RES_IOPORT,
AMR_CFG_BASE, sc->amr_reg);
}
/********************************************************************************
* Allocate and map the scatter/gather table in bus space.
*/
static void
amr_dma_map_sg(void *arg, bus_dma_segment_t *segs, int nseg, int error)
{
struct amr_softc *sc = (struct amr_softc *)arg;
debug("called");
/* save base of s/g table's address in bus space */
sc->amr_sgbusaddr = segs->ds_addr;
}
static int
amr_sglist_map(struct amr_softc *sc)
{
size_t segsize;
int error;
debug("called");
/* destroy any existing mappings */
if (sc->amr_sgtable)
bus_dmamem_free(sc->amr_sg_dmat, sc->amr_sgtable, sc->amr_sg_dmamap);
if (sc->amr_sg_dmat)
bus_dma_tag_destroy(sc->amr_sg_dmat);
/*
* Create a single tag describing a region large enough to hold all of
* the s/g lists we will need.
*/
segsize = sizeof(struct amr_sgentry) * AMR_NSEG * sc->amr_maxio;
error = bus_dma_tag_create(sc->amr_parent_dmat, /* parent */
1, 0, /* alignment, boundary */
BUS_SPACE_MAXADDR, /* lowaddr */
BUS_SPACE_MAXADDR, /* highaddr */
NULL, NULL, /* filter, filterarg */
segsize, 1, /* maxsize, nsegments */
BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
0, /* flags */
&sc->amr_sg_dmat);
if (error != 0) {
device_printf(sc->amr_dev, "can't allocate scatter/gather DMA tag\n");
return(ENOMEM);
}
/*
* Allocate enough s/g maps for all commands and permanently map them into
* controller-visible space.
*
* XXX this assumes we can get enough space for all the s/g maps in one
* contiguous slab. We may need to switch to a more complex arrangement where
* we allocate in smaller chunks and keep a lookup table from slot to bus address.
*/
error = bus_dmamem_alloc(sc->amr_sg_dmat, (void **)&sc->amr_sgtable, BUS_DMA_NOWAIT, &sc->amr_sg_dmamap);
if (error) {
device_printf(sc->amr_dev, "can't allocate s/g table\n");
return(ENOMEM);
}
bus_dmamap_load(sc->amr_sg_dmat, sc->amr_sg_dmamap, sc->amr_sgtable, segsize, amr_dma_map_sg, sc, 0);
return(0);
}
/********************************************************************************
* Allocate and set up mailbox areas for the controller (sc)
*
* The basic mailbox structure should be 16-byte aligned. This means that the
* mailbox64 structure has 4 bytes hanging off the bottom.
*/
static void
amr_map_mailbox(void *arg, bus_dma_segment_t *segs, int nseg, int error)
{
struct amr_softc *sc = (struct amr_softc *)arg;
debug("called");
/* save phsyical base of the basic mailbox structure */
sc->amr_mailboxphys = segs->ds_addr + 16;
}
static int
amr_setup_mbox(struct amr_softc *sc)
{
int error;
u_int8_t *p;
debug("called");
/*
* Create a single tag describing a region large enough to hold the entire
* mailbox.
*/
error = bus_dma_tag_create(sc->amr_parent_dmat, /* parent */
16, 0, /* alignment, boundary */
BUS_SPACE_MAXADDR, /* lowaddr */
BUS_SPACE_MAXADDR, /* highaddr */
NULL, NULL, /* filter, filterarg */
sizeof(struct amr_mailbox) + 16, 1, /* maxsize, nsegments */
BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
0, /* flags */
&sc->amr_mailbox_dmat);
if (error != 0) {
device_printf(sc->amr_dev, "can't allocate mailbox tag\n");
return(ENOMEM);
}
/*
* Allocate the mailbox structure and permanently map it into
* controller-visible space.
*/
error = bus_dmamem_alloc(sc->amr_mailbox_dmat, (void **)&p, BUS_DMA_NOWAIT,
&sc->amr_mailbox_dmamap);
if (error) {
device_printf(sc->amr_dev, "can't allocate mailbox memory\n");
return(ENOMEM);
}
bus_dmamap_load(sc->amr_mailbox_dmat, sc->amr_mailbox_dmamap, p,
sizeof(struct amr_mailbox64), amr_map_mailbox, sc, 0);
/*
* Conventional mailbox is inside the mailbox64 region.
*/
bzero(p, sizeof(struct amr_mailbox64));
sc->amr_mailbox64 = (struct amr_mailbox64 *)(p + 12);
sc->amr_mailbox = (struct amr_mailbox *)(p + 16);
if (sc->amr_type == AMR_TYPE_STD) {
/* XXX we have to tell the controller where we put it */
}
return(0);
}
/********************************************************************************
* Initialise the controller and softc.
*/
int
amr_attach(struct amr_softc *sc)
{
int rid, error;
/*
* Initialise per-controller queues.
*/
TAILQ_INIT(&sc->amr_work);
TAILQ_INIT(&sc->amr_freecmds);
bufq_init(&sc->amr_bufq);
/*
* Configure for this controller type.
*/
if (sc->amr_type == AMR_TYPE_QUARTZ) {
sc->amr_submit_command = amr_quartz_submit_command;
sc->amr_get_work = amr_quartz_get_work;
sc->amr_attach_mailbox = amr_quartz_attach_mailbox;
} else {
sc->amr_submit_command = amr_std_submit_command;
sc->amr_get_work = amr_std_get_work;
sc->amr_attach_mailbox = amr_std_attach_mailbox;
}
/*
* Allocate and connect our interrupt.
*/
rid = 0;
sc->amr_irq = bus_alloc_resource(sc->amr_dev, SYS_RES_IRQ, &rid, 0, ~0, 1, RF_SHAREABLE | RF_ACTIVE);
if (sc->amr_irq == NULL) {
device_printf(sc->amr_dev, "couldn't allocate interrupt\n");
amr_free(sc);
return(ENXIO);
}
error = bus_setup_intr(sc->amr_dev, sc->amr_irq, INTR_TYPE_BIO, amr_intr, sc, &sc->amr_intr);
if (error) {
device_printf(sc->amr_dev, "couldn't set up interrupt\n");
amr_free(sc);
return(ENXIO);
}
/*
* Create DMA tag for mapping buffers into controller-addressable space.
*/
error = bus_dma_tag_create(sc->amr_parent_dmat, /* parent */
1, 0, /* alignment, boundary */
BUS_SPACE_MAXADDR, /* lowaddr */
BUS_SPACE_MAXADDR, /* highaddr */
NULL, NULL, /* filter, filterarg */
MAXBSIZE, AMR_NSEG, /* maxsize, nsegments */
BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
0, /* flags */
&sc->amr_buffer_dmat);
if (error != 0) {
device_printf(sc->amr_dev, "can't allocate buffer DMA tag\n");
return(ENOMEM);
}
/*
* Allocate and set up mailbox in a bus-visible fashion, attach to controller.
*/
if ((error = amr_setup_mbox(sc)) != 0)
return(error);
sc->amr_attach_mailbox(sc);
/*
* Build a temporary set of scatter/gather buffers.
*/
sc->amr_maxio = 2;
if (amr_sglist_map(sc))
return(ENXIO);
/*
* Quiz controller for features and limits.
*/
if (amr_query_controller(sc))
return(ENXIO);
/*
* Rebuild the scatter/gather buffers now we know how many we need.
*/
if (amr_sglist_map(sc))
return(ENXIO);
return(0);
}
/********************************************************************************
* Locate disk resources and attach children to them.
*/
void
amr_startup(struct amr_softc *sc)
{
struct amr_logdrive *dr;
int i, error;
debug("called");
/* get up-to-date drive information */
if (amr_query_controller(sc)) {
device_printf(sc->amr_dev, "couldn't scan controller for drives\n");
return;
}
/* iterate over available drives */
for (i = 0, dr = &sc->amr_drive[0]; (i < AMR_MAXLD) && (dr->al_size != 0xffffffff); i++, dr++) {
/* are we already attached to this drive? */
if (dr->al_disk == 0) {
/* generate geometry information */
if (dr->al_size > 0x200000) { /* extended translation? */
dr->al_heads = 255;
dr->al_sectors = 63;
} else {
dr->al_heads = 64;
dr->al_sectors = 32;
}
dr->al_cylinders = dr->al_size / (dr->al_heads * dr->al_sectors);
dr->al_disk = device_add_child(sc->amr_dev, NULL, -1);
if (dr->al_disk == 0)
device_printf(sc->amr_dev, "device_add_child failed\n");
device_set_ivars(dr->al_disk, dr);
}
}
if ((error = bus_generic_attach(sc->amr_dev)) != 0)
device_printf(sc->amr_dev, "bus_generic_attach returned %d\n", error);
/* mark controller back up */
sc->amr_state &= ~AMR_STATE_SHUTDOWN;
/* interrupts will be enabled before we do anything more */
sc->amr_state |= AMR_STATE_INTEN;
}
/********************************************************************************
* Disconnect from the controller completely, in preparation for unload.
*/
int
amr_detach(device_t dev)
{
struct amr_softc *sc = device_get_softc(dev);
int error;
debug("called");
if (sc->amr_state & AMR_STATE_OPEN)
return(EBUSY);
if ((error = amr_shutdown(dev)))
return(error);
amr_free(sc);
/*
* Deregister the control device on last detach.
*/
if (--cdev_registered == 0)
cdevsw_remove(&amr_cdevsw);
return(0);
}
/********************************************************************************
* Bring the controller down to a dormant state and detach all child devices.
*
* This function is called before detach, system shutdown, or before performing
* an operation which may add or delete system disks. (Call amr_startup to
* resume normal operation.)
*
* Note that we can assume that the bufq on the controller is empty, as we won't
* allow shutdown if any device is open.
*/
int
amr_shutdown(device_t dev)
{
struct amr_softc *sc = device_get_softc(dev);
struct amrd_softc *ad;
int i, s, error;
debug("called");
s = splbio();
error = 0;
/* assume we're going to shut down */
sc->amr_state |= AMR_STATE_SHUTDOWN;
for (i = 0; i < AMR_MAXLD; i++) {
if (sc->amr_drive[i].al_disk != 0) {
ad = device_get_softc(sc->amr_drive[i].al_disk);
if (ad->amrd_flags & AMRD_OPEN) { /* drive is mounted, abort shutdown */
sc->amr_state &= ~AMR_STATE_SHUTDOWN;
device_printf(sc->amr_drive[i].al_disk, "still open, can't shutdown\n");
error = EBUSY;
goto out;
}
}
}
/* flush controller */
device_printf(sc->amr_dev, "flushing cache...");
if (amr_flush(sc)) {
printf("failed\n");
} else {
printf("done\n");
}
/* delete all our child devices */
for (i = 0; i < AMR_MAXLD; i++) {
if (sc->amr_drive[i].al_disk != 0) {
if ((error = device_delete_child(sc->amr_dev, sc->amr_drive[i].al_disk)) != 0)
goto out;
sc->amr_drive[i].al_disk = 0;
}
}
bus_generic_detach(sc->amr_dev);
out:
splx(s);
return(error);
}
/********************************************************************************
* Bring the controller to a quiescent state, ready for system suspend.
*/
int
amr_suspend(device_t dev)
{
struct amr_softc *sc = device_get_softc(dev);
debug("called");
sc->amr_state |= AMR_STATE_SUSPEND;
/* flush controller */
device_printf(sc->amr_dev, "flushing cache...");
printf("%s\n", amr_flush(sc) ? "failed" : "done");
return(0);
}
/********************************************************************************
* Bring the controller back to a state ready for operation.
*/
int
amr_resume(device_t dev)
{
struct amr_softc *sc = device_get_softc(dev);
debug("called");
sc->amr_state &= ~AMR_STATE_SUSPEND;
return(0);
}
/*******************************************************************************
* Take an interrupt, or be poked by other code to look for interrupt-worthy
* status.
*/
void
amr_intr(void *arg)
{
struct amr_softc *sc = (struct amr_softc *)arg;
int worked;
debug("called on %p", sc);
/* spin collecting finished commands, process them if we find anything */
worked = 0;
while (amr_done(sc))
worked = 1;
if (worked)
amr_complete(sc);
};
/*******************************************************************************
* Receive a buf structure from a child device and queue it on a particular
* disk resource, then poke the disk resource to start as much work as it can.
*/
int
amr_submit_buf(struct amr_softc *sc, struct buf *bp)
{
int s;
debug("called");
s = splbio();
bufq_insert_tail(&sc->amr_bufq, bp);
splx(s);
sc->amr_waitbufs++;
amr_startio(sc);
return(0);
}
/********************************************************************************
* Accept an open operation on the control device.
*/
int
amr_open(dev_t dev, int flags, int fmt, struct proc *p)
{
int unit = minor(dev);
struct amr_softc *sc = devclass_get_softc(amr_devclass, unit);
sc->amr_state |= AMR_STATE_OPEN;
return(0);
}
/********************************************************************************
* Accept the last close on the control device.
*/
int
amr_close(dev_t dev, int flags, int fmt, struct proc *p)
{
int unit = minor(dev);
struct amr_softc *sc = devclass_get_softc(amr_devclass, unit);
sc->amr_state &= ~AMR_STATE_OPEN;
return (0);
}
/********************************************************************************
* Handle controller-specific control operations.
*/
int
amr_ioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, struct proc *p)
{
switch(cmd) {
default:
return(ENOTTY);
}
}
/********************************************************************************
* Handle operations requested by a drive connected to this controller.
*/
int
amr_submit_ioctl(struct amr_softc *sc, struct amr_logdrive *drive, u_long cmd,
caddr_t addr, int32_t flag, struct proc *p)
{
return(ENOTTY);
}
/********************************************************************************
********************************************************************************
Command Wrappers
********************************************************************************
********************************************************************************/
/********************************************************************************
* Interrogate the controller for the operational parameters we require.
*/
static int
amr_query_controller(struct amr_softc *sc)
{
void *buf;
int i;
/* try to issue an ENQUIRY3 command */
if ((buf = amr_enquiry(sc, 2048, AMR_CMD_CONFIG, AMR_CONFIG_ENQ3,
AMR_CONFIG_ENQ3_SOLICITED_FULL)) == NULL) {
struct amr_enquiry *ae;
/* failed, try the old ENQUIRY command */
if ((ae = (struct amr_enquiry *)amr_enquiry(sc, 2048, AMR_CMD_ENQUIRY, 0, 0)) == NULL) {
device_printf(sc->amr_dev, "could not obtain configuration data from controller\n");
return(1);
}
/* first-time enquiry? */
if (sc->amr_maxdrives == 0) {
device_printf(sc->amr_dev, "firmware %.4s bios %.4s %dMB memory\n",
ae->ae_adapter.aa_firmware, ae->ae_adapter.aa_bios,
ae->ae_adapter.aa_memorysize);
}
sc->amr_maxdrives = 8;
sc->amr_maxio = ae->ae_adapter.aa_maxio;
for (i = 0; i < ae->ae_ldrv.al_numdrives; i++) {
sc->amr_drive[i].al_size = ae->ae_ldrv.al_size[i];
sc->amr_drive[i].al_state = ae->ae_ldrv.al_state[i];
sc->amr_drive[i].al_properties = ae->ae_ldrv.al_properties[i];
debug(" drive %d: %d state %x properties %x\n", i, sc->amr_drive[i].al_size,
sc->amr_drive[i].al_state, sc->amr_drive[i].al_properties);
}
for (; i < AMR_MAXLD; i++)
sc->amr_drive[i].al_size = 0xffffffff;
free(ae, M_DEVBUF);
} else {
free(buf, M_DEVBUF);
sc->amr_maxdrives = 40;
/* get static product info */
if ((buf = amr_enquiry(sc, 2048, AMR_CMD_CONFIG, AMR_CONFIG_PRODINFO, 0)) == NULL) {
device_printf(sc->amr_dev, "controller supports 40ld but CONFIG_PRODINFO failed\n");
return(1);
}
free(buf, M_DEVBUF);
device_printf(sc->amr_dev, "40LD firmware unsupported; send controller to msmith@freebsd.org\n");
return(1);
}
return(0);
}
/********************************************************************************
* Run a generic enquiry-style command.
*/
static void *
amr_enquiry(struct amr_softc *sc, size_t bufsize, u_int8_t cmd, u_int8_t cmdsub, u_int8_t cmdqual)
{
struct amr_command *ac;
void *result;
u_int8_t *mbox;
int error;
debug("called");
error = 1;
result = NULL;
/* get ourselves a command buffer */
if ((ac = amr_alloccmd(sc)) == NULL)
goto out;
/* allocate the response structure */
if ((result = malloc(bufsize, M_DEVBUF, M_NOWAIT)) == NULL)
goto out;
/* get a command slot */
ac->ac_flags |= AMR_CMD_PRIORITY | AMR_CMD_DATAOUT;
if (amr_getslot(ac))
goto out;
/* map the command so the controller can see it */
ac->ac_data = result;
ac->ac_length = bufsize;
amr_mapcmd(ac);
/* build the command proper */
mbox = (u_int8_t *)&ac->ac_mailbox; /* XXX want a real structure for this? */
mbox[0] = cmd;
mbox[2] = cmdsub;
mbox[3] = cmdqual;
ac->ac_mailbox.mb_physaddr = ac->ac_dataphys;
/* run the command in polled/wait mode as suits the current mode */
if ((sc->amr_state & AMR_STATE_INTEN) ? amr_wait_command(ac) : amr_poll_command(ac))
goto out;
error = ac->ac_status;
out:
if (ac != NULL)
amr_releasecmd(ac);
if ((error != 0) && (result != NULL)) {
free(result, M_DEVBUF);
result = NULL;
}
return(result);
}
/********************************************************************************
* Flush the controller's internal cache, return status.
*/
static int
amr_flush(struct amr_softc *sc)
{
struct amr_command *ac;
int error;
/* get ourselves a command buffer */
error = 1;
if ((ac = amr_alloccmd(sc)) == NULL)
goto out;
/* get a command slot */
ac->ac_flags |= AMR_CMD_PRIORITY | AMR_CMD_DATAOUT;
if (amr_getslot(ac))
goto out;
/* build the command proper */
ac->ac_mailbox.mb_command = AMR_CMD_FLUSH;
/* run the command in polled/wait mode as suits the current mode */
if ((sc->amr_state & AMR_STATE_INTEN) ? amr_wait_command(ac) : amr_poll_command(ac))
goto out;
error = ac->ac_status;
out:
if (ac != NULL)
amr_releasecmd(ac);
return(error);
}
/********************************************************************************
* Pull as much work off the softc's work queue as possible and give it to the
* controller. Leave a couple of slots free for emergencies.
*
* We avoid running at splbio() whenever possible.
*/
static void
amr_startio(struct amr_softc *sc)
{
struct amr_command *ac;
struct amrd_softc *amrd;
struct buf *bp;
int blkcount;
int driveno;
int cmd;
int s;
/* spin until something prevents us from doing any work */
s = splbio();
for (;;) {
/* see if there's work to be done */
if ((bp = bufq_first(&sc->amr_bufq)) == NULL)
break;
/* get a command */
if ((ac = amr_alloccmd(sc)) == NULL)
break;
/* get a slot for the command */
if (amr_getslot(ac) != 0) {
amr_releasecmd(ac);
break;
}
/* get the buf containing our work */
bufq_remove(&sc->amr_bufq, bp);
sc->amr_waitbufs--;
splx(s);
/* connect the buf to the command */
ac->ac_complete = amr_completeio;
ac->ac_private = bp;
ac->ac_data = bp->b_data;
ac->ac_length = bp->b_bcount;
if (bp->b_iocmd == BIO_READ) {
ac->ac_flags |= AMR_CMD_DATAIN;
cmd = AMR_CMD_LREAD;
} else {
ac->ac_flags |= AMR_CMD_DATAOUT;
cmd = AMR_CMD_LWRITE;
}
/* map the command so the controller can work with it */
amr_mapcmd(ac);
/* build a suitable I/O command (assumes 512-byte rounded transfers) */
amrd = (struct amrd_softc *)bp->b_driver1;
driveno = amrd->amrd_drive - &sc->amr_drive[0];
blkcount = bp->b_bcount / AMR_BLKSIZE;
if ((bp->b_pblkno + blkcount) > sc->amr_drive[driveno].al_size)
device_printf(sc->amr_dev, "I/O beyond end of unit (%u,%d > %u)\n",
bp->b_pblkno, blkcount, sc->amr_drive[driveno].al_size);
/*
* Build the I/O command.
*/
ac->ac_mailbox.mb_command = cmd;
ac->ac_mailbox.mb_blkcount = blkcount;
ac->ac_mailbox.mb_lba = bp->b_pblkno;
ac->ac_mailbox.mb_physaddr = ac->ac_sgphys;
ac->ac_mailbox.mb_drive = driveno;
ac->ac_mailbox.mb_nsgelem = ac->ac_nsgent;
/* try to give command to controller */
if (amr_start(ac) != 0) {
/* fail the command */
ac->ac_status = AMR_STATUS_WEDGED;
amr_completeio(ac);
}
s = splbio();
}
splx(s);
}
/********************************************************************************
* Handle completion of an I/O command.
*/
static void
amr_completeio(struct amr_command *ac)
{
struct amr_softc *sc = ac->ac_sc;
struct buf *bp = (struct buf *)ac->ac_private;
if (ac->ac_status != AMR_STATUS_SUCCESS) { /* could be more verbose here? */
bp->b_error = EIO;
bp->b_flags |= B_ERROR;
switch(ac->ac_status) {
/* XXX need more information on I/O error reasons */
default:
device_printf(sc->amr_dev, "I/O error - %x\n", ac->ac_status);
amr_printcommand(ac);
break;
}
}
amr_releasecmd(ac);
amrd_intr(bp);
}
/********************************************************************************
********************************************************************************
Command Processing
********************************************************************************
********************************************************************************/
/********************************************************************************
* Take a command, submit it to the controller and sleep until it completes
* or fails. Interrupts must be enabled, returns nonzero on error.
*/
static int
amr_wait_command(struct amr_command *ac)
{
struct amr_softc *sc = ac->ac_sc;
int error, count;
debug("called");
ac->ac_complete = NULL;
ac->ac_private = ac;
if ((error = amr_start(ac)) != 0)
return(error);
count = 0;
/* XXX better timeout? */
while ((ac->ac_status == AMR_STATUS_BUSY) && (count < 30)) {
tsleep(ac->ac_private, PRIBIO | PCATCH, "amrwcmd", hz);
}
if (ac->ac_status != 0) {
device_printf(sc->amr_dev, "I/O error 0x%x\n", ac->ac_status);
return(EIO);
}
return(0);
}
/********************************************************************************
* Take a command, submit it to the controller and busy-wait for it to return.
* Returns nonzero on error. Can be safely called with interrupts enabled.
*/
static int
amr_poll_command(struct amr_command *ac)
{
struct amr_softc *sc = ac->ac_sc;
int error, count, s;
debug("called");
ac->ac_complete = NULL;
ac->ac_private = NULL;
if ((error = amr_start(ac)) != 0)
return(error);
count = 0;
do {
/*
* Poll for completion, although the interrupt handler may beat us to it.
* Note that the timeout here is somewhat arbitrary.
*/
amr_done(sc);
} while ((ac->ac_status == AMR_STATUS_BUSY) && (count++ < 100000));
s = splbio();
if (ac->ac_status != AMR_STATUS_BUSY) {
TAILQ_REMOVE(&sc->amr_work, ac, ac_link);
sc->amr_workcount--;
error = 0;
} else {
/* take the command out of the busy list, mark slot as bogus */
sc->amr_busycmd[ac->ac_slot] = (struct amr_command *)sc;
error = EIO;
device_printf(sc->amr_dev, "I/O error 0x%x\n", ac->ac_status);
}
splx(s);
return(error);
}
/********************************************************************************
* Get a free command slot.
*/
static int
amr_getslot(struct amr_command *ac)
{
struct amr_softc *sc = ac->ac_sc;
int s, slot, limit;
debug("called");
/* enforce slot usage limit */
limit = (ac->ac_flags & AMR_CMD_PRIORITY) ? sc->amr_maxio : sc->amr_maxio - 4;
if (sc->amr_busycmdcount > limit)
return(EBUSY);
/*
* Allocate a slot
*/
s = splbio();
for (slot = 0; slot < sc->amr_maxio; slot++) {
if (sc->amr_busycmd[slot] == NULL)
break;
}
if (slot < sc->amr_maxio) {
sc->amr_busycmdcount++;
sc->amr_busycmd[slot] = ac;
}
splx(s);
/* out of slots? */
if (slot >= sc->amr_maxio)
return(EBUSY);
ac->ac_slot = slot;
return(0);
}
/********************************************************************************
* Map/unmap (ac)'s data in the controller's addressable space.
*/
static void
amr_setup_dmamap(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
{
struct amr_command *ac = (struct amr_command *)arg;
struct amr_softc *sc = ac->ac_sc;
struct amr_sgentry *sg;
int i;
debug("called");
/* get base address of s/g table */
sg = sc->amr_sgtable + (ac->ac_slot * AMR_NSEG);
/* save s/g table information in command */
ac->ac_nsgent = nsegments;
ac->ac_sgphys = sc->amr_sgbusaddr + (ac->ac_slot * AMR_NSEG * sizeof(struct amr_sgentry));
ac->ac_dataphys = segs[0].ds_addr;
/* populate s/g table */
for (i = 0; i < nsegments; i++, sg++) {
sg->sg_addr = segs[i].ds_addr;
sg->sg_count = segs[i].ds_len;
}
}
static void
amr_mapcmd(struct amr_command *ac)
{
struct amr_softc *sc = ac->ac_sc;
debug("called");
/* if the command involves data at all */
if (ac->ac_data != NULL) {
/* map the data buffer into bus space and build the s/g list */
bus_dmamap_load(sc->amr_buffer_dmat, ac->ac_dmamap, ac->ac_data, ac->ac_length,
amr_setup_dmamap, ac, 0);
if (ac->ac_flags & AMR_CMD_DATAIN)
bus_dmamap_sync(sc->amr_buffer_dmat, ac->ac_dmamap, BUS_DMASYNC_PREREAD);
if (ac->ac_flags & AMR_CMD_DATAOUT)
bus_dmamap_sync(sc->amr_buffer_dmat, ac->ac_dmamap, BUS_DMASYNC_PREWRITE);
}
}
static void
amr_unmapcmd(struct amr_command *ac)
{
struct amr_softc *sc = ac->ac_sc;
debug("called");
/* if the command involved data at all */
if (ac->ac_data != NULL) {
if (ac->ac_flags & AMR_CMD_DATAIN)
bus_dmamap_sync(sc->amr_buffer_dmat, ac->ac_dmamap, BUS_DMASYNC_POSTREAD);
if (ac->ac_flags & AMR_CMD_DATAOUT)
bus_dmamap_sync(sc->amr_buffer_dmat, ac->ac_dmamap, BUS_DMASYNC_POSTWRITE);
bus_dmamap_unload(sc->amr_buffer_dmat, ac->ac_dmamap);
}
}
/********************************************************************************
* Take a command and give it to the controller. Take care of any completed
* commands we encouter while waiting.
*/
static int
amr_start(struct amr_command *ac)
{
struct amr_softc *sc = ac->ac_sc;
int worked, done, s, i;
debug("called");
/*
* Save the slot number so that we can locate this command when complete.
* Note that ident = 0 seems to be special, so we don't use it.
*/
ac->ac_mailbox.mb_ident = ac->ac_slot + 1;
/* set the busy flag when we copy the mailbox in */
ac->ac_mailbox.mb_busy = 1;
/* set impossible status so that a woken sleeper can tell the command is busy */
ac->ac_status = AMR_STATUS_BUSY;
/* spin waiting for the mailbox */
debug("wait for mailbox");
for (i = 100000, done = 0, worked = 0; (i > 0) && !done; i--) {
s = splbio();
/* is the mailbox free? */
if (sc->amr_mailbox->mb_busy == 0) {
debug("got mailbox");
sc->amr_mailbox64->mb64_segment = 0;
bcopy(&ac->ac_mailbox, sc->amr_mailbox, AMR_MBOX_CMDSIZE);
sc->amr_submit_command(sc);
done = 1;
sc->amr_workcount++;
TAILQ_INSERT_TAIL(&sc->amr_work, ac, ac_link);
/* not free, try to clean up while we wait */
} else {
debug("busy flag %x\n", sc->amr_mailbox->mb_busy);
worked = amr_done(sc);
}
splx(s);
}
/* do completion processing if we picked anything up */
if (worked)
amr_complete(sc);
/* command is enqueued? */
if (done) {
ac->ac_stamp = time_second;
debug("posted command");
return(0);
}
/*
* The controller wouldn't take the command. Revoke the slot
* that the command was given and return with a bad status.
*/
sc->amr_busycmd[ac->ac_slot] = NULL;
device_printf(sc->amr_dev, "controller wedged (not taking commands)\n");
ac->ac_status = AMR_STATUS_WEDGED;
return(EIO);
}
/********************************************************************************
* Extract one or more completed commands from the controller (sc)
*
* Returns nonzero if any commands on the work queue were marked as completed.
*/
static int
amr_done(struct amr_softc *sc)
{
struct amr_command *ac;
struct amr_mailbox mbox;
int i, idx, result;
debug("called");
/* See if there's anything for us to do */
result = 0;
if (sc->amr_get_work(sc, &mbox)) {
/* iterate over completed commands */
for (i = 0; i < mbox.mb_nstatus; i++) {
/* get pointer to busy command */
idx = mbox.mb_completed[i] - 1;
ac = sc->amr_busycmd[idx];
/* really a busy command? */
if (ac != NULL) {
/* pull the command from the busy index */
sc->amr_busycmd[idx] = NULL;
sc->amr_busycmdcount--;
/* unmap data buffer */
amr_unmapcmd(ac);
/* aborted command? */
if (ac == (struct amr_command *)sc) {
device_printf(sc->amr_dev, "aborted command completed (%d)\n", idx);
ac = NULL;
/* completed normally, save status */
} else {
ac->ac_status = mbox.mb_status;
debug("completed command with status %x", mbox.mb_status);
}
result = 1;
}
}
}
return(result);
}
/********************************************************************************
* Do completion processing on done commands on (sc)
*/
static void
amr_complete(struct amr_softc *sc)
{
struct amr_command *ac, *nc;
int s, count;
debug("called");
count = 0;
s = splbio();
ac = TAILQ_FIRST(&sc->amr_work);
while (ac != NULL) {
nc = TAILQ_NEXT(ac, ac_link);
/* Skip if command is still active */
if (ac->ac_status != AMR_STATUS_BUSY) {
/*
* Is there a completion handler?
*/
if (ac->ac_complete != NULL) {
/* remove and give to completion handler */
TAILQ_REMOVE(&sc->amr_work, ac, ac_link);
sc->amr_workcount--;
ac->ac_complete(ac);
/*
* Is someone sleeping on this one?
*/
} else if (ac->ac_private != NULL) {
/* remove and wake up */
TAILQ_REMOVE(&sc->amr_work, ac, ac_link);
sc->amr_workcount--;
wakeup_one(ac->ac_private);
/*
* Leave it for a polling caller.
*/
} else {
}
}
ac = nc;
}
splx(s);
/* queue more work if we can */
amr_startio(sc);
}
/********************************************************************************
********************************************************************************
Command Buffer Management
********************************************************************************
********************************************************************************/
/********************************************************************************
* Get a new command buffer.
*
* This may return NULL in low-memory cases.
*
* Note that using malloc() is expensive (the command buffer is << 1 page) but
* necessary if we are to be a loadable module before the zone allocator is fixed.
*
* If possible, we recycle a command buffer that's been used before.
*
* XXX Note that command buffers are not cleaned out - it is the caller's
* responsibility to ensure that all required fields are filled in before
* using a buffer.
*/
static struct amr_command *
amr_alloccmd(struct amr_softc *sc)
{
struct amr_command *ac;
int error;
int s;
debug("called");
s = splbio();
if ((ac = TAILQ_FIRST(&sc->amr_freecmds)) != NULL)
TAILQ_REMOVE(&sc->amr_freecmds, ac, ac_link);
splx(s);
/* allocate a new command buffer? */
if (ac == NULL) {
ac = (struct amr_command *)malloc(sizeof(*ac), M_DEVBUF, M_NOWAIT);
if (ac != NULL) {
bzero(ac, sizeof(*ac));
ac->ac_sc = sc;
error = bus_dmamap_create(sc->amr_buffer_dmat, 0, &ac->ac_dmamap);
if (error) {
free(ac, M_DEVBUF);
return(NULL);
}
}
}
bzero(&ac->ac_mailbox, sizeof(struct amr_mailbox));
return(ac);
}
/********************************************************************************
* Release a command buffer for recycling.
*
* XXX It might be a good idea to limit the number of commands we save for reuse
* if it's shown that this list bloats out massively.
*/
static void
amr_releasecmd(struct amr_command *ac)
{
int s;
debug("called");
s = splbio();
TAILQ_INSERT_HEAD(&ac->ac_sc->amr_freecmds, ac, ac_link);
splx(s);
}
/********************************************************************************
* Permanently discard a command buffer.
*/
static void
amr_freecmd(struct amr_command *ac)
{
struct amr_softc *sc = ac->ac_sc;
debug("called");
bus_dmamap_destroy(sc->amr_buffer_dmat, ac->ac_dmamap);
free(ac, M_DEVBUF);
}
/********************************************************************************
********************************************************************************
Interface-specific Shims
********************************************************************************
********************************************************************************/
/********************************************************************************
* Tell the controller that the mailbox contains a valid command
*/
static void
amr_quartz_submit_command(struct amr_softc *sc)
{
debug("called");
sc->amr_mailbox->mb_poll = 0;
sc->amr_mailbox->mb_ack = 0;
/* XXX write barrier? */
while(AMR_QGET_IDB(sc) & AMR_QIDB_SUBMIT)
; /* XXX aiee! what if it dies? */
AMR_QPUT_IDB(sc, sc->amr_mailboxphys | AMR_QIDB_SUBMIT);
}
static void
amr_std_submit_command(struct amr_softc *sc)
{
debug("called");
/* XXX write barrier? */
while (AMR_SGET_MBSTAT(sc) & AMR_SMBOX_BUSYFLAG)
; /* XXX aiee! what if it dies? */
AMR_SPOST_COMMAND(sc);
}
/********************************************************************************
* Claim any work that the controller has completed; acknowledge completion,
* save details of the completion in (mbsave)
*/
static int
amr_quartz_get_work(struct amr_softc *sc, struct amr_mailbox *mbsave)
{
int s, worked;
u_int32_t outd;
debug("called");
worked = 0;
s = splbio();
/* work waiting for us? */
if ((outd = AMR_QGET_ODB(sc)) == AMR_QODB_READY) {
AMR_QPUT_ODB(sc, AMR_QODB_READY);
/* save mailbox, which contains a list of completed commands */
/* XXX read barrier? */
bcopy(sc->amr_mailbox, mbsave, sizeof(*mbsave));
/* acknowledge that we have the commands */
AMR_QPUT_IDB(sc, sc->amr_mailboxphys | AMR_QIDB_ACK);
while(AMR_QGET_IDB(sc) & AMR_QIDB_ACK)
; /* XXX aiee! what if it dies? */
worked = 1; /* got some work */
}
splx(s);
return(worked);
}
static int
amr_std_get_work(struct amr_softc *sc, struct amr_mailbox *mbsave)
{
int s, worked;
u_int8_t istat;
debug("called");
worked = 0;
s = splbio();
/* check for valid interrupt status */
istat = AMR_SGET_ISTAT(sc);
if ((istat & AMR_SINTR_VALID) != 0) {
AMR_SPUT_ISTAT(sc, istat); /* ack interrupt status */
/* save mailbox, which contains a list of completed commands */
/* XXX read barrier? */
bcopy(sc->amr_mailbox, mbsave, sizeof(*mbsave));
AMR_SACK_INTERRUPT(sc); /* acknowledge we have the mailbox */
worked = 1;
}
splx(s);
return(worked);
}
/********************************************************************************
* Notify the controller of the mailbox location.
*/
static void
amr_quartz_attach_mailbox(struct amr_softc *sc)
{
/* Quartz is given the mailbox location when a command is submitted */
}
static void
amr_std_attach_mailbox(struct amr_softc *sc)
{
/* program the mailbox physical address */
AMR_SBYTE_SET(sc, AMR_SMBOX_0, sc->amr_mailboxphys & 0xff);
AMR_SBYTE_SET(sc, AMR_SMBOX_1, (sc->amr_mailboxphys >> 8) & 0xff);
AMR_SBYTE_SET(sc, AMR_SMBOX_2, (sc->amr_mailboxphys >> 16) & 0xff);
AMR_SBYTE_SET(sc, AMR_SMBOX_3, (sc->amr_mailboxphys >> 24) & 0xff);
AMR_SBYTE_SET(sc, AMR_SMBOX_ENABLE, AMR_SMBOX_ADDR);
/* clear any outstanding interrupt and enable interrupts proper */
AMR_SACK_INTERRUPT(sc);
AMR_SENABLE_INTR(sc);
}
/********************************************************************************
********************************************************************************
Debugging
********************************************************************************
********************************************************************************/
/********************************************************************************
* Print the command (ac) in human-readable format
*/
static void
amr_printcommand(struct amr_command *ac)
{
struct amr_softc *sc = ac->ac_sc;
struct amr_sgentry *sg;
int i;
device_printf(sc->amr_dev, "cmd %x ident %d drive %d\n",
ac->ac_mailbox.mb_command, ac->ac_mailbox.mb_ident, ac->ac_mailbox.mb_drive);
device_printf(sc->amr_dev, "blkcount %d lba %d\n",
ac->ac_mailbox.mb_blkcount, ac->ac_mailbox.mb_lba);
device_printf(sc->amr_dev, "virtaddr %p length %lu\n", ac->ac_data, (unsigned long)ac->ac_length);
device_printf(sc->amr_dev, "physaddr %08x nsg %d\n",
ac->ac_mailbox.mb_physaddr, ac->ac_mailbox.mb_nsgelem);
/* get base address of s/g table */
sg = sc->amr_sgtable + (ac->ac_slot * AMR_NSEG);
for (i = 0; i < ac->ac_mailbox.mb_nsgelem; i++, sg++)
device_printf(sc->amr_dev, " %x/%d\n", sg->sg_addr, sg->sg_count);
}
|