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
|
/* $OpenBSD: mesh.c,v 1.3 2001/11/06 19:53:15 miod Exp $ */
/* $NetBSD: mesh.c,v 1.1 1999/02/19 13:06:03 tsubai Exp $ */
/*-
* Copyright (C) 1999 Internet Research Institute, Inc.
* 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
* Internet Research Institute, Inc.
* 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 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.
*/
#include <sys/param.h>
#include <sys/buf.h>
#include <sys/device.h>
#include <sys/errno.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/queue.h>
#include <sys/systm.h>
#include <uvm/uvm_extern.h>
#include <scsi/scsi_all.h>
#include <scsi/scsiconf.h>
#include <scsi/scsi_message.h>
/*
#include "scsipi/scsi_all.h"
#include "scsipi/scsipi_all.h"
#include "scsipi/scsiconf.h"
#include "scsipi/scsi_message.h"*/
#include <dev/ofw/openfirm.h>
#include <machine/bus.h>
#include <machine/autoconf.h>
#include <machine/cpu.h>
#include "dbdma.h"
#include "meshreg.h"
#define T_SYNCMODE 0x01 /* target uses sync mode */
#define T_SYNCNEGO 0x02 /* sync negotiation done */
struct mesh_tinfo {
int flags;
int period;
int offset;
};
/* scb flags */
#define MESH_POLL 0x01
#define MESH_CHECK 0x02
#define MESH_SENSE 0x04
#define MESH_READ 0x80
struct mesh_scb {
TAILQ_ENTRY(mesh_scb) chain;
int flags;
struct scsi_xfer *xs;
struct scsi_generic cmd;
int cmdlen;
int target; /* target SCSI ID */
int resid;
vaddr_t daddr;
vsize_t dlen;
int status;
};
/* sc_flags value */
#define MESH_DMA_ACTIVE 0x01
#define MESH_DMALIST_MAX 32
struct mesh_softc {
struct device sc_dev; /* us as a device */
struct scsi_link sc_link;
struct scsi_adapter sc_adapter;
u_char *sc_reg; /* MESH base address */
bus_dma_tag_t sc_dmat;
dbdma_regmap_t *sc_dmareg; /* DMA register address */
dbdma_command_t *sc_dmacmd; /* DMA command area */
dbdma_t sc_dbdma;
int sc_flags;
int sc_cfflags; /* copy of config flags */
int sc_meshid; /* MESH version */
int sc_minsync; /* minimum sync period */
int sc_irq;
int sc_freq; /* SCSI bus frequency in MHz */
int sc_id; /* our SCSI ID */
struct mesh_tinfo sc_tinfo[8]; /* target information */
int sc_nextstate;
int sc_prevphase;
struct mesh_scb *sc_nexus; /* current command */
int sc_msgout;
int sc_imsglen;
int sc_omsglen;
u_char sc_imsg[16];
u_char sc_omsg[16];
TAILQ_HEAD(, mesh_scb) free_scb;
TAILQ_HEAD(, mesh_scb) ready_scb;
struct mesh_scb sc_scb[16];
struct timeout sc_tmo;
};
/* mesh_msgout() values */
#define SEND_REJECT 1
#define SEND_IDENTIFY 2
#define SEND_SDTR 4
#ifdef __OpenBSD__
#define scsi_print_addr sc_print_addr
#define scsipi_done scsi_done
#endif
static __inline int mesh_read_reg __P((struct mesh_softc *, int));
static __inline void mesh_set_reg __P((struct mesh_softc *, int, int));
int mesh_match __P((struct device *, struct cfdata *, void *));
void mesh_attach __P((struct device *, struct device *, void *));
void mesh_shutdownhook __P((void *));
int mesh_intr __P((void *));
void mesh_error __P((struct mesh_softc *, struct mesh_scb *, int, int));
void mesh_select __P((struct mesh_softc *, struct mesh_scb *));
void mesh_identify __P((struct mesh_softc *, struct mesh_scb *));
void mesh_command __P((struct mesh_softc *, struct mesh_scb *));
int mesh_dma_setup __P((struct mesh_softc *, struct mesh_scb *));
int mesh_dataio __P((struct mesh_softc *, struct mesh_scb *));
void mesh_status __P((struct mesh_softc *, struct mesh_scb *));
void mesh_msgin __P((struct mesh_softc *, struct mesh_scb *));
void mesh_msgout __P((struct mesh_softc *, int));
void mesh_bus_reset __P((struct mesh_softc *));
void mesh_reset __P((struct mesh_softc *));
int mesh_stp __P((struct mesh_softc *, int));
void mesh_setsync __P((struct mesh_softc *, struct mesh_tinfo *));
struct mesh_scb *mesh_get_scb __P((struct mesh_softc *));
void mesh_free_scb __P((struct mesh_softc *, struct mesh_scb *));
int mesh_scsi_cmd __P((struct scsi_xfer *));
void mesh_sched __P((struct mesh_softc *));
int mesh_poll __P((struct mesh_softc *, struct scsi_xfer *));
void mesh_done __P((struct mesh_softc *, struct mesh_scb *));
void mesh_timeout __P((void *));
void mesh_sense __P((struct mesh_softc *, struct mesh_scb *));
void mesh_minphys __P((struct buf *));
#define MESH_DATAOUT 0
#define MESH_DATAIN MESH_STATUS0_IO
#define MESH_COMMAND MESH_STATUS0_CD
#define MESH_STATUS (MESH_STATUS0_CD | MESH_STATUS0_IO)
#define MESH_MSGOUT (MESH_STATUS0_MSG | MESH_STATUS0_CD)
#define MESH_MSGIN (MESH_STATUS0_MSG | MESH_STATUS0_CD | MESH_STATUS0_IO)
#define MESH_SELECTING 8
#define MESH_IDENTIFY 9
#define MESH_COMPLETE 10
#define MESH_BUSFREE 11
#define MESH_UNKNOWN -1
#define MESH_PHASE_MASK (MESH_STATUS0_MSG | MESH_STATUS0_CD | MESH_STATUS0_IO)
struct cfattach mesh_ca = {
sizeof(struct mesh_softc),(cfmatch_t)mesh_match,
mesh_attach
};
struct cfdriver mesh_cd = {
NULL, "mesh", DV_DULL
};
struct scsi_device mesh_dev = {
NULL, /* Use default error handler */
NULL, /* have a queue, served by this */
NULL, /* have no async handler */
NULL, /* Use default 'done' routine */
};
int
mesh_match(parent, cf, aux)
struct device *parent;
struct cfdata *cf;
void *aux;
{
struct confargs *ca = aux;
printf("MESH_MATCH called ,ca->ca_name= %s\n",ca->ca_name);
if (strcmp(ca->ca_name, "mesh") != 0)
return 0;
return 1;
}
void
mesh_attach(parent, self, aux)
struct device *parent, *self;
void *aux;
{
struct mesh_softc *sc = (void *)self;
struct confargs *ca = aux;
int i, error;
u_int *reg;
printf("MESH_ATTACH called\n");
reg = ca->ca_reg;
reg[0] += ca->ca_baseaddr;
reg[2] += ca->ca_baseaddr;
sc->sc_reg = mapiodev(reg[0], reg[1]);
sc->sc_irq = ca->ca_intr[0];
sc->sc_dmareg = mapiodev(reg[2], reg[3]);
sc->sc_cfflags = self->dv_cfdata->cf_flags;
sc->sc_meshid = mesh_read_reg(sc, MESH_MESH_ID) & 0x1f;
#if 0
if (sc->sc_meshid != (MESH_SIGNATURE & 0x1f) {
printf(": unknown MESH ID (0x%x)\n", sc->sc_meshid);
return;
}
#endif
if (OF_getprop(ca->ca_node, "clock-frequency", &sc->sc_freq, 4) != 4) {
printf(": cannot get clock-frequency\n");
return;
}
sc->sc_dmat = ca->ca_dmat;
if ((error = bus_dmamap_create(sc->sc_dmat,
MESH_DMALIST_MAX * DBDMA_COUNT_MAX, MESH_DMALIST_MAX,
DBDMA_COUNT_MAX, NBPG, BUS_DMA_NOWAIT, &sc->sc_dmamap)) != 0) {
printf(": cannot create dma map, error = %d\n", error);
return;
}
sc->sc_freq /= 1000000; /* in MHz */
sc->sc_minsync = 25; /* maximum sync rate = 10MB/sec */
sc->sc_id = 7;
TAILQ_INIT(&sc->free_scb);
TAILQ_INIT(&sc->ready_scb);
for (i = 0; i < sizeof(sc->sc_scb)/sizeof(sc->sc_scb[0]); i++)
TAILQ_INSERT_TAIL(&sc->free_scb, &sc->sc_scb[i], chain);
sc->sc_dbdma = dbdma_alloc(sc->sc_dmat, MESH_DMALIST_MAX);
sc->sc_dmacmd = sc->sc_dbdma->d_addr;
timeout_set(&sc->sc_tmo, mesh_timeout, scb);
mesh_reset(sc);
mesh_bus_reset(sc);
printf(" irq %d: %dMHz, SCSI ID %d\n",
sc->sc_irq, sc->sc_freq, sc->sc_id);
sc->sc_adapter.scsi_cmd = mesh_scsi_cmd;
sc->sc_adapter.scsi_minphys = mesh_minphys;
/* sc->sc_link.scsi_scsi.channel = SCSI_CHANNEL_ONLY_ONE;*/
sc->sc_link.adapter_softc = sc;
sc->sc_link.adapter_target = sc->sc_id;
sc->sc_link.adapter = &sc->sc_adapter;
sc->sc_link.device = &mesh_dev;
sc->sc_link.openings = 2;
/* sc->sc_link.scsi_scsi.max_target = 7;
sc->sc_link.scsi_scsi.max_lun = 7;
sc->sc_link.type = BUS_SCSI;
*/
config_found(&sc->sc_dev, &sc->sc_link, scsiprint);
printf("in mesh_attach, after config_found,calling mac_intr_establish,sc->sc_irq = %d\n",sc->sc_irq);
/*intr_establish(sc->sc_irq, IST_LEVEL, IPL_BIO, mesh_intr, sc);*/
mac_intr_establish(parent,sc->sc_irq,IST_LEVEL, IPL_BIO, mesh_intr, sc,"mesh intr");
/* Reset SCSI bus when halt. */
shutdownhook_establish(mesh_shutdownhook, sc);
}
#define MESH_SET_XFER(sc, count) do { \
mesh_set_reg(sc, MESH_XFER_COUNT0, count); \
mesh_set_reg(sc, MESH_XFER_COUNT1, count >> 8); \
} while (0)
#define MESH_GET_XFER(sc) ((mesh_read_reg(sc, MESH_XFER_COUNT1) << 8) | \
mesh_read_reg(sc, MESH_XFER_COUNT0))
int
mesh_read_reg(sc, reg)
struct mesh_softc *sc;
int reg;
{
return in8(sc->sc_reg + reg);
}
void
mesh_set_reg(sc, reg, val)
struct mesh_softc *sc;
int reg, val;
{
out8(sc->sc_reg + reg, val);
}
void
mesh_shutdownhook(arg)
void *arg;
{
struct mesh_softc *sc = arg;
/* Set to async mode. */
mesh_set_reg(sc, MESH_SYNC_PARAM, 2);
}
int
mesh_intr(arg)
void *arg;
{
struct mesh_softc *sc = arg;
struct mesh_scb *scb;
u_char intr, exception, error, status0, status1;
int i;
intr = mesh_read_reg(sc, MESH_INTERRUPT);
#ifdef MESH_DEBUG
if (intr == 0) {
printf("mesh: stray interrupt\n");
return 0;
}
#endif
exception = mesh_read_reg(sc, MESH_EXCEPTION);
error = mesh_read_reg(sc, MESH_ERROR);
status0 = mesh_read_reg(sc, MESH_BUS_STATUS0);
status1 = mesh_read_reg(sc, MESH_BUS_STATUS1);
/* clear interrupt */
mesh_set_reg(sc, MESH_INTERRUPT, intr);
scb = sc->sc_nexus;
if (scb == NULL) {
#ifdef MESH_DEBUG
printf("mesh: NULL nexus\n");
#endif
return 1;
}
if (sc->sc_flags & MESH_DMA_ACTIVE) {
dbdma_stop(sc->sc_dmareg);
bus_dmamap_unload(sc->sc_dmat, sc->sc_dmamap);
sc->sc_flags &= ~MESH_DMA_ACTIVE;
scb->resid = MESH_GET_XFER(sc);
if (mesh_read_reg(sc, MESH_FIFO_COUNT) != 0)
panic("mesh: FIFO != 0"); /* XXX */
}
if (intr & MESH_INTR_ERROR) {
mesh_error(sc, scb, error, 0);
return 1;
}
if (intr & MESH_INTR_EXCEPTION) {
/* selection timeout */
if (exception & MESH_EXC_SELTO) {
mesh_error(sc, scb, 0, exception);
return 1;
}
/* phase mismatch */
if (exception & MESH_EXC_PHASEMM) {
sc->sc_nextstate = status0 & MESH_PHASE_MASK;
#if 0
printf("mesh: PHASE MISMATCH cdb =");
printf(" %02x", scb->cmd.opcode);
for (i = 0; i < 5; i++) {
printf(" %02x", scb->cmd.bytes[i]);
}
printf("\n");
#endif
}
}
if (sc->sc_nextstate == MESH_UNKNOWN)
sc->sc_nextstate = status0 & MESH_PHASE_MASK;
switch (sc->sc_nextstate) {
case MESH_IDENTIFY:
mesh_identify(sc, scb);
break;
case MESH_COMMAND:
mesh_command(sc, scb);
printf("mesh_intr:case MESH_COMMAND\n");
break;
case MESH_DATAIN:
case MESH_DATAOUT:
if (mesh_dataio(sc, scb))
return (1);
printf("mesh_intr:case MESH_DATAIN or MESH_DATAOUT\n");
break;
case MESH_STATUS:
mesh_status(sc, scb);
printf("mesh_intr:case MESH_STATUS\n");
break;
case MESH_MSGIN:
mesh_msgin(sc, scb);
printf("mesh_intr:case MESH_MSGIN\n");
break;
case MESH_COMPLETE:
printf("mesh_intr:case MESH_COMPLETE\n");
mesh_done(sc, scb);
break;
default:
panic("mesh: unknown state (0x%x)", sc->sc_nextstate);
}
return 1;
}
void
mesh_error(sc, scb, error, exception)
struct mesh_softc *sc;
struct mesh_scb *scb;
int error, exception;
{
if (error & MESH_ERR_SCSI_RESET) {
printf("mesh: SCSI RESET\n");
/* Wait until the RST signal is deasserted. */
while (mesh_read_reg(sc, MESH_BUS_STATUS1) & MESH_STATUS1_RST);
mesh_reset(sc);
return;
}
if (error & MESH_ERR_PARITY_ERR0) {
printf("mesh: parity error\n");
scb->xs->error = XS_DRIVER_STUFFUP;
}
if (error & MESH_ERR_DISCONNECT) {
printf("mesh: unexpected disconnect\n");
if (sc->sc_nextstate != MESH_COMPLETE)
scb->xs->error = XS_DRIVER_STUFFUP;
}
if (exception & MESH_EXC_SELTO) {
/* XXX should reset bus here? */
scb->xs->error = XS_DRIVER_STUFFUP;
}
mesh_done(sc, scb);
}
void
mesh_select(sc, scb)
struct mesh_softc *sc;
struct mesh_scb *scb;
{
struct mesh_tinfo *ti = &sc->sc_tinfo[scb->target];
mesh_setsync(sc, ti);
MESH_SET_XFER(sc, 0);
/* arbitration */
/*
* MESH mistakenly asserts TARGET ID bit along with its own ID bit
* in arbitration phase (like selection). So we should load
* initiator ID to DestID register temporarily.
*/
mesh_set_reg(sc, MESH_DEST_ID, sc->sc_id);
mesh_set_reg(sc, MESH_INTR_MASK, 0); /* disable intr. */
mesh_set_reg(sc, MESH_SEQUENCE, MESH_CMD_ARBITRATE);
while (mesh_read_reg(sc, MESH_INTERRUPT) == 0);
mesh_set_reg(sc, MESH_INTERRUPT, 1);
mesh_set_reg(sc, MESH_INTR_MASK, 7);
/* selection */
mesh_set_reg(sc, MESH_DEST_ID, scb->target);
mesh_set_reg(sc, MESH_SEQUENCE, MESH_CMD_SELECT | MESH_SEQ_ATN);
sc->sc_prevphase = MESH_SELECTING;
sc->sc_nextstate = MESH_IDENTIFY;
timeout_add(&sc->sc_tmo, 10*hz);
}
void
mesh_identify(sc, scb)
struct mesh_softc *sc;
struct mesh_scb *scb;
{
mesh_set_reg(sc, MESH_SEQUENCE, MESH_CMD_FLUSH_FIFO);
mesh_msgout(sc, SEND_IDENTIFY);
sc->sc_nextstate = MESH_COMMAND;
printf("mesh_identify called\n");
}
void
mesh_command(sc, scb)
struct mesh_softc *sc;
struct mesh_scb *scb;
{
struct mesh_tinfo *ti = &sc->sc_tinfo[scb->target];
int i;
char *cmdp;
if ((ti->flags & T_SYNCNEGO) == 0) {
ti->period = sc->sc_minsync;
ti->offset = 15;
mesh_msgout(sc, SEND_SDTR);
sc->sc_prevphase = MESH_COMMAND;
sc->sc_nextstate = MESH_MSGIN;
return;
}
mesh_set_reg(sc, MESH_SEQUENCE, MESH_CMD_FLUSH_FIFO);
MESH_SET_XFER(sc, scb->cmdlen);
mesh_set_reg(sc, MESH_SEQUENCE, MESH_CMD_COMMAND);
cmdp = (char *)&scb->cmd;
for (i = 0; i < scb->cmdlen; i++)
mesh_set_reg(sc, MESH_FIFO, *cmdp++);
if (scb->resid == 0)
sc->sc_nextstate = MESH_STATUS; /* no data xfer */
else
sc->sc_nextstate = MESH_DATAIN;
}
int
mesh_dma_setup(sc, scb)
struct mesh_softc *sc;
struct mesh_scb *scb;
{
struct scsi_xfer *xs = scb->xs;
int datain = scb->flags & MESH_READ;
dbdma_command_t *cmdp;
u_int cmd;
int i, error;
if ((error = bus_dmamap_load(sc->dmat, sc->sc_dmamap, scb->daddr,
scb->dlen, NULL, BUS_DMA_NOWAIT)) != 0)
return (error);
cmdp = sc->sc_dmacmd;
cmd = datain ? DBDMA_CMD_IN_MORE : DBDMA_CMD_OUT_MORE;
for (i = 0; i < sc->sc_dmamap->dm_nsegs; i++, cmdp++) {
if (i + 1 == sc->sc_dmamap->dm_nsegs)
cmd = read ? DBDMA_CMD_IN_LAST : DBDMA_CMD_OUT_LAST;
DBDMA_BUILD(cmdp, cmd, 0, sc->sc_dmamap->dm_segs[i].ds_len,
sc->sc_dmamap->dm_segs[i].ds_addr,
DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
}
DBDMA_BUILD(cmdp, DBDMA_CMD_STOP, 0, 0, 0,
DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
return (0);
}
int
mesh_dataio(sc, scb)
struct mesh_softc *sc;
struct mesh_scb *scb;
{
int error;
if ((error = mesh_dma_setup(sc, scb)))
return (error);
if (scb->dlen == 65536)
MESH_SET_XFER(sc, 0); /* TC = 0 means 64KB transfer */
else
MESH_SET_XFER(sc, scb->dlen);
mesh_set_reg(sc, MESH_SEQUENCE, MESH_SEQ_DMA |
(scb->flags & MESH_READ)? MESH_CMD_DATAIN : MESH_CMD_DATAOUT);
dbdma_start(sc->sc_dmareg, sc->sc_dbdma);
sc->sc_flags |= MESH_DMA_ACTIVE;
sc->sc_nextstate = MESH_STATUS;
return (0);
}
void
mesh_status(sc, scb)
struct mesh_softc *sc;
struct mesh_scb *scb;
{
if (mesh_read_reg(sc, MESH_FIFO_COUNT) == 0) { /* XXX cheat */
MESH_SET_XFER(sc, 1);
mesh_set_reg(sc, MESH_SEQUENCE, MESH_CMD_STATUS);
sc->sc_nextstate = MESH_STATUS;
return;
}
scb->status = mesh_read_reg(sc, MESH_FIFO);
mesh_set_reg(sc, MESH_SEQUENCE, MESH_CMD_FLUSH_FIFO);
MESH_SET_XFER(sc, 1);
mesh_set_reg(sc, MESH_SEQUENCE, MESH_CMD_MSGIN);
sc->sc_nextstate = MESH_MSGIN;
}
#define IS1BYTEMSG(m) (((m) != 1 && (m) < 0x20) || (m) & 0x80)
#define IS2BYTEMSG(m) (((m) & 0xf0) == 0x20)
#define ISEXTMSG(m) ((m) == 1)
void
mesh_msgin(sc, scb)
struct mesh_softc *sc;
struct mesh_scb *scb;
{
int i;
if (mesh_read_reg(sc, MESH_FIFO_COUNT) == 0) { /* XXX cheat */
MESH_SET_XFER(sc, 1);
mesh_set_reg(sc, MESH_SEQUENCE, MESH_CMD_MSGIN);
sc->sc_imsglen = 0;
sc->sc_nextstate = MESH_MSGIN;
return;
}
sc->sc_imsg[sc->sc_imsglen++] = mesh_read_reg(sc, MESH_FIFO);
if (sc->sc_imsglen == 1 && IS1BYTEMSG(sc->sc_imsg[0]))
goto gotit;
if (sc->sc_imsglen == 2 && IS2BYTEMSG(sc->sc_imsg[0]))
goto gotit;
if (sc->sc_imsglen >= 3 && ISEXTMSG(sc->sc_imsg[0]) &&
sc->sc_imsglen == sc->sc_imsg[1] + 2)
goto gotit;
sc->sc_nextstate = MESH_MSGIN;
MESH_SET_XFER(sc, 1);
mesh_set_reg(sc, MESH_SEQUENCE, MESH_CMD_MSGIN);
return;
gotit:
#ifdef DEBUG
printf("msgin:");
for (i = 0; i < sc->sc_imsglen; i++)
printf(" 0x%02x", sc->sc_imsg[i]);
printf("\n");
#endif
switch (sc->sc_imsg[0]) {
case MSG_CMDCOMPLETE:
mesh_set_reg(sc, MESH_SEQUENCE, MESH_CMD_BUSFREE);
sc->sc_nextstate = MESH_COMPLETE;
sc->sc_imsglen = 0;
return;
case MSG_MESSAGE_REJECT:
switch (sc->sc_msgout) {
case SEND_SDTR:
printf("SDTR rejected\n");
printf("using async mode\n");
sc->sc_tinfo[scb->target].period = 0;
sc->sc_tinfo[scb->target].offset = 0;
mesh_setsync(sc, &sc->sc_tinfo[scb->target]);
break;
}
break;
case MSG_NOOP:
break;
case MSG_EXTENDED:
goto extended_msg;
default:
scsi_print_addr(scb->xs->sc_link);
printf("unrecognized MESSAGE(0x%02x); sending REJECT\n",
sc->sc_imsg[0]);
reject:
mesh_msgout(sc, SEND_REJECT);
return;
}
goto done;
extended_msg:
/* process an extended message */
switch (sc->sc_imsg[2]) {
case MSG_EXT_SDTR:
{
struct mesh_tinfo *ti = &sc->sc_tinfo[scb->target];
int period = sc->sc_imsg[3];
int offset = sc->sc_imsg[4];
int r = 250 / period;
int s = (100*250) / period - 100 * r;
if (period < sc->sc_minsync) {
ti->period = sc->sc_minsync;
ti->offset = 15;
mesh_msgout(sc, SEND_SDTR);
return;
}
scsi_print_addr(scb->xs->sc_link);
/* XXX if (offset != 0) ... */
printf("max sync rate %d.%02dMb/s\n", r, s);
ti->period = period;
ti->offset = offset;
ti->flags |= T_SYNCNEGO;
ti->flags |= T_SYNCMODE;
mesh_setsync(sc, ti);
goto done;
}
default:
printf("%s target %d: rejecting extended message 0x%x\n",
sc->sc_dev.dv_xname, scb->target, sc->sc_imsg[0]);
goto reject;
}
done:
sc->sc_imsglen = 0;
sc->sc_nextstate = MESH_UNKNOWN;
mesh_set_reg(sc, MESH_SEQUENCE, MESH_CMD_BUSFREE); /* XXX really? */
}
void
mesh_msgout(sc, msg)
struct mesh_softc *sc;
int msg;
{
struct mesh_scb *scb = sc->sc_nexus;
struct mesh_tinfo *ti;
int lun, i;
switch (msg) {
case SEND_REJECT:
sc->sc_omsglen = 1;
sc->sc_omsg[0] = MSG_MESSAGE_REJECT;
break;
case SEND_IDENTIFY:
lun = scb->xs->sc_link->lun;
sc->sc_omsglen = 1;
sc->sc_omsg[0] = MSG_IDENTIFY(lun, 0);
break;
case SEND_SDTR:
ti = &sc->sc_tinfo[scb->target];
sc->sc_omsglen = 5;
sc->sc_omsg[0] = MSG_EXTENDED;
sc->sc_omsg[1] = 3;
sc->sc_omsg[2] = MSG_EXT_SDTR;
sc->sc_omsg[3] = ti->period;
sc->sc_omsg[4] = ti->offset;
break;
}
sc->sc_msgout = msg;
MESH_SET_XFER(sc, sc->sc_omsglen);
mesh_set_reg(sc, MESH_SEQUENCE, MESH_CMD_MSGOUT | MESH_SEQ_ATN);
for (i = 0; i < sc->sc_omsglen; i++)
mesh_set_reg(sc, MESH_FIFO, sc->sc_omsg[i]);
sc->sc_nextstate = MESH_UNKNOWN;
}
void
mesh_bus_reset(sc)
struct mesh_softc *sc;
{
/* Disable interrupts. */
mesh_set_reg(sc, MESH_INTR_MASK, 0);
/* Assert RST line. */
mesh_set_reg(sc, MESH_BUS_STATUS1, MESH_STATUS1_RST);
delay(50);
mesh_set_reg(sc, MESH_BUS_STATUS1, 0);
mesh_reset(sc);
}
void
mesh_reset(sc)
struct mesh_softc *sc;
{
int i;
/* Reset DMA first. */
dbdma_reset(sc->sc_dmareg);
/* Disable interrupts. */
mesh_set_reg(sc, MESH_INTR_MASK, 0);
mesh_set_reg(sc, MESH_SEQUENCE, MESH_CMD_RESET_MESH);
delay(1);
/* Wait for reset done. */
while (mesh_read_reg(sc, MESH_INTERRUPT) == 0);
/* Clear interrupts */
mesh_set_reg(sc, MESH_INTERRUPT, 0x7);
/* Set SCSI ID */
mesh_set_reg(sc, MESH_SOURCE_ID, sc->sc_id);
/* Set to async mode by default. */
mesh_set_reg(sc, MESH_SYNC_PARAM, 2);
/* Set selection timeout to 250ms. */
mesh_set_reg(sc, MESH_SEL_TIMEOUT, 250 * sc->sc_freq / 500);
/* Enable parity check. */
mesh_set_reg(sc, MESH_SEQUENCE, MESH_CMD_ENABLE_PARITY);
/* Enable all interrupts. */
mesh_set_reg(sc, MESH_INTR_MASK, 0x7);
for (i = 0; i < 7; i++) {
struct mesh_tinfo *ti = &sc->sc_tinfo[i];
ti->flags = 0;
ti->period = ti->offset = 0;
if (sc->sc_cfflags & (1 << i)) {
ti->flags |= T_SYNCNEGO;
}
}
sc->sc_nexus = NULL;
}
int
mesh_stp(sc, v)
struct mesh_softc *sc;
int v;
{
/*
* stp(v) = 5 * clock_period (v == 0)
* = (v + 2) * 2 clock_period (v > 0)
*/
if (v == 0)
return 5 * 250 / sc->sc_freq;
else
return (v + 2) * 2 * 250 / sc->sc_freq;
}
void
mesh_setsync(sc, ti)
struct mesh_softc *sc;
struct mesh_tinfo *ti;
{
int period = ti->period;
int offset = ti->offset;
int v;
if ((ti->flags & T_SYNCMODE) == 0)
offset = 0;
if (offset == 0) { /* async mode */
mesh_set_reg(sc, MESH_SYNC_PARAM, 2);
return;
}
v = period * sc->sc_freq / 250 / 2 - 2;
if (v < 0)
v = 0;
if (mesh_stp(sc, v) < period)
v++;
if (v > 15)
v = 15;
mesh_set_reg(sc, MESH_SYNC_PARAM, (offset << 4) | v);
}
struct mesh_scb *
mesh_get_scb(sc)
struct mesh_softc *sc;
{
struct mesh_scb *scb;
int s;
s = splbio();
while ((scb = sc->free_scb.tqh_first) == NULL)
tsleep(&sc->free_scb, PRIBIO, "meshscb", 0);
TAILQ_REMOVE(&sc->free_scb, scb, chain);
splx(s);
return scb;
}
void
mesh_free_scb(sc, scb)
struct mesh_softc *sc;
struct mesh_scb *scb;
{
int s;
s = splbio();
TAILQ_INSERT_HEAD(&sc->free_scb, scb, chain);
if (scb->chain.tqe_next == NULL)
wakeup(&sc->free_scb);
splx(s);
}
int
mesh_scsi_cmd(xs)
struct scsi_xfer *xs;
{
struct scsi_link *sc_link = xs->sc_link;
struct mesh_softc *sc = sc_link->adapter_softc;
struct mesh_scb *scb;
u_int flags;
int s;
flags = xs->flags;
scb = mesh_get_scb(sc);
scb->xs = xs;
scb->flags = 0;
scb->status = 0;
scb->daddr = (vaddr_t)xs->data;
scb->dlen = xs->datalen;
scb->resid = xs->datalen;
bcopy(xs->cmd, &scb->cmd, xs->cmdlen);
scb->cmdlen = xs->cmdlen;
scb->target = sc_link->target;
sc->sc_imsglen = 0; /* XXX ? */
printf("messh_scsi_cmd,scb->target=%d\n",scb->target);
if (flags & SCSI_POLL){
printf("mesh_scsi_cmd:flags=SCSI_POLL\n");
scb->flags |= MESH_POLL;
}
#if 0
if (flags & SCSI_DATA_OUT)
scb->flags &= ~MESH_READ;
#endif
if (flags & SCSI_DATA_IN){
printf("mesh_scsi_cmd:flags=SCSI_DATA_IN\n");
scb->flags |= MESH_READ;
}
s = splbio();
TAILQ_INSERT_TAIL(&sc->ready_scb, scb, chain);
if (sc->sc_nexus == NULL){
printf("mesh_scsi_cmd:sc->sc_nexus == NULL,calling mesh_sched\n"); /* IDLE */
mesh_sched(sc);
}
splx(s);
if ((flags & SCSI_POLL) == 0){
printf("mesh_scsi_cmd: returning SUCCESSFULLY_QUEUED\n");
return SUCCESSFULLY_QUEUED;
}
if (mesh_poll(sc, xs)) {
printf("mesh: timeout\n");
if (mesh_poll(sc, xs))
printf("mesh: timeout again\n");
}
printf("mesh_scsi_cmd: returning COMPLETE\n");
return COMPLETE;
}
void
mesh_sched(sc)
struct mesh_softc *sc;
{
struct scsi_xfer *xs;
struct scsi_link *sc_link;
struct mesh_scb *scb;
scb = sc->ready_scb.tqh_first;
start:
if (scb == NULL)
return;
xs = scb->xs;
sc_link = xs->sc_link;
if (sc->sc_nexus == NULL) {
TAILQ_REMOVE(&sc->ready_scb, scb, chain);
sc->sc_nexus = scb;
mesh_select(sc, scb);
return;
}
scb = scb->chain.tqe_next;
goto start;
}
int
mesh_poll(sc, xs)
struct mesh_softc *sc;
struct scsi_xfer *xs;
{
int count = xs->timeout;
printf("in mesh_poll,timeout=%d\n",xs->timeout);
while (count) {
if (mesh_read_reg(sc, MESH_INTERRUPT))
mesh_intr(sc);
if (xs->flags & ITSDONE)
return 0;
DELAY(1000);
count--;
};
return 1;
}
void
mesh_done(sc, scb)
struct mesh_softc *sc;
struct mesh_scb *scb;
{
struct scsi_xfer *xs = scb->xs;
#ifdef MESH_SHOWSTATE
printf("mesh_done\n");
#endif
sc->sc_nextstate = MESH_BUSFREE;
sc->sc_nexus = NULL;
timeout_del(&sc->sc_tmo);
if (scb->status == SCSI_BUSY) {
xs->error = XS_BUSY;
printf("Target busy\n");
}
if (scb->status == SCSI_CHECK) {
if (scb->flags & MESH_SENSE)
panic("SCSI_CHECK && MESH_SENSE?");
xs->resid = scb->resid;
mesh_sense(sc, scb);
return;
}
if (xs->error == XS_NOERROR) {
xs->status = scb->status;
if (scb->flags & MESH_SENSE)
xs->error = XS_SENSE;
else
xs->resid = scb->resid;
}
xs->flags |= ITSDONE;
mesh_set_reg(sc, MESH_SYNC_PARAM, 2);
if ((xs->flags & SCSI_POLL) == 0)
mesh_sched(sc);
scsi_done(xs);
mesh_free_scb(sc, scb);
}
void
mesh_timeout(arg)
void *arg;
{
struct mesh_scb *scb = arg;
struct mesh_softc *sc = scb->xs->sc_link->adapter_softc;
int s;
int status0, status1;
int intr, error, exception;
printf("mesh: timeout state=%x\n", sc->sc_nextstate);
intr = mesh_read_reg(sc, MESH_INTERRUPT);
exception = mesh_read_reg(sc, MESH_EXCEPTION);
error = mesh_read_reg(sc, MESH_ERROR);
status0 = mesh_read_reg(sc, MESH_BUS_STATUS0);
status1 = mesh_read_reg(sc, MESH_BUS_STATUS1);
#if 1
printf("intr 0x%02x, except 0x%02x, err 0x%02x\n", intr, exception, error);
#endif
s = splbio();
if (sc->sc_flags & MESH_DMA_ACTIVE) {
printf("mesh: resetting dma\n");
dbdma_reset(sc->sc_dmareg);
}
scb->xs->error = XS_TIMEOUT;
mesh_set_reg(sc, MESH_SEQUENCE, MESH_CMD_BUSFREE);
sc->sc_nextstate = MESH_COMPLETE;
splx(s);
printf("rerturning from mesh_timeout\n");
}
void
mesh_sense(sc, scb)
struct mesh_softc *sc;
struct mesh_scb *scb;
{
struct scsi_xfer *xs = scb->xs;
struct scsi_link *sc_link = xs->sc_link;
struct scsi_sense *ss = (void *)&scb->cmd;
bzero(ss, sizeof(*ss));
ss->opcode = REQUEST_SENSE;
ss->byte2 = sc_link->lun << 5;
ss->length = sizeof(struct scsi_sense_data);
scb->cmdlen = sizeof(*ss);
scb->daddr = (vaddr_t)&xs->sense;
scb->dlen = sizeof(struct scsi_sense_data);
scb->resid = scb->dlen;
bzero((void *)scb->daddr, scb->dlen);
scb->flags |= MESH_SENSE | MESH_READ;
TAILQ_INSERT_HEAD(&sc->ready_scb, scb, chain);
if (sc->sc_nexus == NULL)
mesh_sched(sc);
}
void
mesh_minphys(bp)
struct buf *bp;
{
if (bp->b_bcount > 64*1024)
bp->b_bcount = 64*1024;
minphys(bp);
}
|