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
|
/* $NetBSD: if_dse.c,v 1.3 2022/12/22 23:06:11 nat Exp $ */
/*
* Driver for DaynaPORT SCSI/Link SCSI-Ethernet
*
* Written by Hiroshi Noguchi <ngc@ff.iij4u.or.jp>
*
* Modified by Matt Sandstrom <mattias@beauty.se> for NetBSD 1.5.3
*
* This driver is written based on "if_se.c".
*/
/*
* Copyright (c) 1997 Ian W. Dall <ian.dall@dsto.defence.gov.au>
* 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 Ian W. Dall.
* 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 "opt_inet.h"
#include "opt_net_mpsafe.h"
#include "opt_atalk.h"
#include <sys/types.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/callout.h>
#include <sys/syslog.h>
#include <sys/kernel.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/buf.h>
#include <sys/uio.h>
#include <sys/malloc.h>
#include <sys/errno.h>
#include <sys/device.h>
#include <sys/disklabel.h>
#include <sys/disk.h>
#include <sys/proc.h>
#include <sys/conf.h>
#include <sys/workqueue.h>
#include <dev/scsipi/scsipi_all.h>
#include <dev/scsipi/scsiconf.h>
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_ether.h>
#include <net/if_media.h>
#ifdef INET
#include <netinet/in.h>
#include <netinet/if_inarp.h>
#endif
#ifdef NETATALK
#include <netatalk/at.h>
#endif
#include <net/bpf.h>
/*
* debug flag
*/
#if 0
#define DSE_DEBUG
#endif
#define DSE_TIMEOUT 100000
#define DSE_OUTSTANDING 4
#define DSE_RETRIES 4
#define DSE_MINSIZE 60
#define DSE_HEADER_TX 4
#define DSE_TAIL_TX 4
#define DSE_EXTRAS_TX (DSE_HEADER_TX + DSE_TAIL_TX)
#define DSE_HEADER_RX 6
#define DSE_TAIL_RX 0
#define DSE_EXTRAS_RX (DSE_HEADER_RX + DSE_TAIL_RX)
#define MAX_BYTES_RX (ETHERMTU + sizeof(struct ether_header) + ETHER_CRC_LEN)
/* 10 full length packets appears to be the max ever returned. 16k is OK */
#define RBUF_LEN (16 * 1024)
/*
* Tuning parameters:
* We will attempt to adapt to polling fast enough to get RDATA_GOAL packets
* per read
*/
#define RDATA_MAX 10 /* maximum of returned packets (guessed) */
#define RDATA_GOAL 8
/*
* maximum of available multicast address entries (guessed)
*/
#define DSE_MCAST_MAX 10
/* dse_poll and dse_poll0 are the normal polling rate and the minimum
* polling rate respectively. dse_poll0 should be chosen so that at
* maximum ethernet speed, we will read nearly RDATA_MAX packets. dse_poll
* should be chosen for reasonable maximum latency.
* In practice, if we are being saturated with min length packets, we
* can't poll fast enough. Polling with zero delay actually
* worsens performance. dse_poll0 is enforced to be always at least 1
*/
#if MAC68K_DEBUG
#define DSE_POLL 50 /* default in milliseconds */
#define DSE_POLL0 30 /* default in milliseconds */
#else
#define DSE_POLL 80 /* default in milliseconds */
#define DSE_POLL0 40 /* default in milliseconds */
#endif
int dse_poll = 0; /* Delay in ticks set at attach time */
int dse_poll0 = 0;
int dse_max_received = 0; /* Instrumentation */
/*==========================================
data type defs
==========================================*/
typedef struct scsipi_inquiry_data dayna_ether_inquiry_data;
typedef struct {
uint8_t opcode[2];
uint8_t byte3;
uint8_t length[2];
uint8_t byte6;
} scsi_dayna_ether_generic;
#define DAYNA_CMD_SEND 0x0A /* same as generic "Write" */
#define DAYNA_CMD_RECV 0x08 /* same as generic "Read" */
#define DAYNA_CMD_GET_ADDR 0x09 /* ???: read MAC address ? */
#define REQ_LEN_GET_ADDR 0x12
#define DAYNA_CMD_SET_MULTI 0x0D /* set multicast address */
#define DAYNA_CMD_VENDOR1 0x0E /* ???: initialize signal ? */
#define IS_SEND(generic) ((generic)->opcode == DAYNA_CMD_SEND)
#define IS_RECV(generic) ((generic)->opcode == DAYNA_CMD_RECV)
struct dse_softc {
device_t sc_dev;
struct ethercom sc_ethercom; /* Ethernet common part */
struct scsipi_periph *sc_periph;/* contains our targ, lun, etc. */
struct callout sc_recv_ch;
struct kmutex sc_iflock;
struct if_percpuq *sc_ipq;
struct workqueue *sc_recv_wq, *sc_send_wq;
struct work sc_recv_work, sc_send_work;
int sc_recv_work_pending, sc_send_work_pending;
char *sc_tbuf;
char *sc_rbuf;
int sc_debug;
int sc_flags;
int sc_last_timeout;
int sc_enabled;
int sc_attach_state;
};
/* bit defs of "sc_flags" */
#define DSE_NEED_RECV 0x1
static int dsematch(device_t, cfdata_t, void *);
static void dseattach(device_t, device_t, void *);
static int dsedetach(device_t, int);
static void dse_ifstart(struct ifnet *);
static void dse_send_worker(struct work *wk, void *cookie);
static void dsedone(struct scsipi_xfer *, int);
static int dse_ioctl(struct ifnet *, u_long, void *);
static void dsewatchdog(struct ifnet *);
static void dse_recv_callout(void *);
static void dse_recv_worker(struct work *wk, void *cookie);
static void dse_recv(struct dse_softc *);
static struct mbuf* dse_get(struct dse_softc *, uint8_t *, int);
static int dse_read(struct dse_softc *, uint8_t *, int);
static int dse_init_adaptor(struct dse_softc *);
static int dse_get_addr(struct dse_softc *, uint8_t *);
static int dse_set_multi(struct dse_softc *);
static int dse_reset(struct dse_softc *);
#if 0 /* 07/16/2000 comment-out */
static int dse_set_mode(struct dse_softc *, int, int);
#endif
static int dse_init(struct dse_softc *);
static void dse_stop(struct dse_softc *);
#if 0
static __inline uint16_t ether_cmp(void *, void *);
#endif
static inline int dse_scsipi_cmd(struct scsipi_periph *periph,
struct scsipi_generic *scsipi_cmd,
int cmdlen, u_char *data_addr, int datalen,
int retries, int timeout, struct buf *bp,
int flags);
int dse_enable(struct dse_softc *);
void dse_disable(struct dse_softc *);
CFATTACH_DECL_NEW(dse, sizeof(struct dse_softc),
dsematch, dseattach, dsedetach, NULL);
extern struct cfdriver dse_cd;
dev_type_open(dseopen);
dev_type_close(dseclose);
dev_type_ioctl(dseioctl);
const struct cdevsw dse_cdevsw = {
.d_open = dseopen,
.d_close = dseclose,
.d_read = noread,
.d_write = nowrite,
.d_ioctl = dseioctl,
.d_stop = nostop,
.d_tty = notty,
.d_poll = nopoll,
.d_mmap = nommap,
.d_kqfilter = nokqfilter,
.d_discard = nodiscard,
.d_flag = D_OTHER | D_MPSAFE
};
const struct scsipi_periphsw dse_switch = {
NULL, /* Use default error handler */
NULL, /* have no queue */
NULL, /* have no async handler */
dsedone, /* deal with stats at interrupt time */
};
struct scsipi_inquiry_pattern dse_patterns[] = {
{ T_PROCESSOR, T_FIXED,
"Dayna", "SCSI/Link", "" },
};
/*====================================================
definitions for SCSI commands
====================================================*/
/*
* command templates
*/
/* unknown commands */
/* Vendor #1 */
static const scsi_dayna_ether_generic sonic_ether_vendor1 = {
{ DAYNA_CMD_VENDOR1, 0x00 },
0x00,
{ 0x00, 0x00 },
0x80
};
#if 0
/*
* Compare two Ether/802 addredses for equality, inlined and
* unrolled for speed.
* Note: use this like memcmp()
*/
static __inline uint16_t
ether_cmp(void *one, void *two)
{
uint16_t* a;
uint16_t* b;
uint16_t diff;
a = (uint16_t *) one;
b = (uint16_t *) two;
diff = (a[0] - b[0]) | (a[1] - b[1]) | (a[2] - b[2]);
return (diff);
}
#define ETHER_CMP ether_cmp
#endif
/*
* check to match with SCSI inquiry information
*/
static int
dsematch(device_t parent, cfdata_t match, void *aux)
{
struct scsipibus_attach_args *sa = aux;
int priority;
(void)scsipi_inqmatch(&sa->sa_inqbuf,
dse_patterns, sizeof(dse_patterns) / sizeof(dse_patterns[0]),
sizeof(dse_patterns[0]), &priority);
return priority;
}
/*
* The routine called by the low level scsi routine when it discovers
* a device suitable for this driver.
*/
static void
dseattach(device_t parent, device_t self, void *aux)
{
struct dse_softc *sc = device_private(self);
struct scsipibus_attach_args *sa = aux;
struct scsipi_periph *periph = sa->sa_periph;
struct ifnet *ifp = &sc->sc_ethercom.ec_if;
uint8_t myaddr[ETHER_ADDR_LEN];
char wqname[MAXCOMLEN];
int rv;
sc->sc_dev = self;
aprint_normal("\n");
SC_DEBUG(periph, SCSIPI_DB2, ("dseattach: "));
sc->sc_attach_state = 0;
callout_init(&sc->sc_recv_ch, CALLOUT_MPSAFE);
callout_setfunc(&sc->sc_recv_ch, dse_recv_callout, (void *)sc);
mutex_init(&sc->sc_iflock, MUTEX_DEFAULT, IPL_SOFTNET);
/*
* Store information needed to contact our base driver
*/
sc->sc_periph = periph;
periph->periph_dev = sc->sc_dev;
periph->periph_switch = &dse_switch;
#if 0
sc_periph->sc_link_dbflags = SCSIPI_DB1;
#endif
dse_poll = mstohz(DSE_POLL);
dse_poll = dse_poll? dse_poll: 1;
dse_poll0 = mstohz(DSE_POLL0);
dse_poll0 = dse_poll0? dse_poll0: 1;
/*
* Initialize and attach send and receive buffers
*/
sc->sc_tbuf = malloc(ETHERMTU + sizeof(struct ether_header) +
DSE_EXTRAS_TX + 16, M_DEVBUF, M_WAITOK);
sc->sc_rbuf = malloc(RBUF_LEN + 16, M_DEVBUF, M_WAITOK);
/* initialize adaptor and obtain MAC address */
dse_init_adaptor(sc);
sc->sc_attach_state = 1;
/* Initialize ifnet structure. */
strcpy(ifp->if_xname, device_xname(sc->sc_dev));
ifp->if_softc = sc;
ifp->if_start = dse_ifstart;
ifp->if_ioctl = dse_ioctl;
ifp->if_watchdog = dsewatchdog;
ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
ifp->if_extflags = IFEF_MPSAFE;
dse_get_addr(sc, myaddr);
/* Attach the interface. */
if_initialize(ifp);
snprintf(wqname, sizeof(wqname), "%sRx", device_xname(sc->sc_dev));
rv = workqueue_create(&sc->sc_recv_wq, wqname, dse_recv_worker, sc,
PRI_SOFTNET, IPL_NET, WQ_MPSAFE);
if (rv != 0) {
aprint_error_dev(sc->sc_dev,
"unable to create recv Rx workqueue\n");
dsedetach(sc->sc_dev, 0);
return; /* Error */
}
sc->sc_recv_work_pending = false;
sc->sc_attach_state = 2;
snprintf(wqname, sizeof(wqname), "%sTx", device_xname(sc->sc_dev));
rv = workqueue_create(&sc->sc_send_wq, wqname, dse_send_worker, ifp,
PRI_SOFTNET, IPL_NET, WQ_MPSAFE);
if (rv != 0) {
aprint_error_dev(sc->sc_dev,
"unable to create send Tx workqueue\n");
dsedetach(sc->sc_dev, 0);
return; /* Error */
}
sc->sc_send_work_pending = false;
sc->sc_ipq = if_percpuq_create(&sc->sc_ethercom.ec_if);
ether_ifattach(ifp, myaddr);
if_register(ifp);
sc->sc_attach_state = 4;
bpf_attach(ifp, DLT_EN10MB, sizeof(struct ether_header));
}
static int
dsedetach(device_t self, int flags)
{
struct dse_softc *sc = device_private(self);
struct ifnet *ifp = &sc->sc_ethercom.ec_if;
switch(sc->sc_attach_state) {
case 4:
dse_stop(sc);
mutex_enter(&sc->sc_iflock);
ifp->if_flags &= ~IFF_RUNNING;
dse_disable(sc);
ether_ifdetach(ifp);
if_detach(ifp);
mutex_exit(&sc->sc_iflock);
if_percpuq_destroy(sc->sc_ipq);
/*FALLTHROUGH*/
case 3:
workqueue_destroy(sc->sc_send_wq);
/*FALLTHROUGH*/
case 2:
workqueue_destroy(sc->sc_recv_wq);
/*FALLTHROUGH*/
case 1:
free(sc->sc_rbuf, M_DEVBUF);
free(sc->sc_tbuf, M_DEVBUF);
callout_destroy(&sc->sc_recv_ch);
mutex_destroy(&sc->sc_iflock);
break;
default:
aprint_error_dev(sc->sc_dev, "detach failed (state %d)\n",
sc->sc_attach_state);
return 1;
break;
}
return 0;
}
/*
* submit SCSI command
*/
static __inline int
dse_scsipi_cmd(struct scsipi_periph *periph, struct scsipi_generic *cmd,
int cmdlen, u_char *data_addr, int datalen, int retries, int timeout,
struct buf *bp, int flags)
{
int error = 0;
error = scsipi_command(periph, cmd, cmdlen, data_addr,
datalen, retries, timeout, bp, flags);
return error;
}
/*
* Start routine for calling from network sub system
*/
static void
dse_ifstart(struct ifnet *ifp)
{
struct dse_softc *sc = ifp->if_softc;
mutex_enter(&sc->sc_iflock);
if (!sc->sc_send_work_pending) {
sc->sc_send_work_pending = true;
workqueue_enqueue(sc->sc_send_wq, &sc->sc_send_work, NULL);
}
mutex_exit(&sc->sc_iflock);
if (sc->sc_flags & DSE_NEED_RECV) {
sc->sc_flags &= ~DSE_NEED_RECV;
}
}
/*
* Invoke the transmit workqueue and transmission on the interface.
*/
static void
dse_send_worker(struct work *wk, void *cookie)
{
struct ifnet *ifp = cookie;
struct dse_softc *sc = ifp->if_softc;
scsi_dayna_ether_generic cmd_send;
struct mbuf *m, *m0;
int len, error;
u_char *cp;
mutex_enter(&sc->sc_iflock);
sc->sc_send_work_pending = false;
mutex_exit(&sc->sc_iflock);
KASSERT(if_is_mpsafe(ifp));
/* Don't transmit if interface is busy or not running */
if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
return;
while (1) {
IFQ_DEQUEUE(&ifp->if_snd, m0);
if (m0 == NULL)
break;
/* If BPF is listening on this interface, let it see the
* packet before we commit it to the wire.
*/
bpf_mtap(ifp, m0, BPF_D_OUT);
/* We need to use m->m_pkthdr.len, so require the header */
if ((m0->m_flags & M_PKTHDR) == 0)
panic("ctscstart: no header mbuf");
len = m0->m_pkthdr.len;
/* Mark the interface busy. */
ifp->if_flags |= IFF_OACTIVE;
/* Chain; copy into linear buffer allocated at attach time. */
cp = sc->sc_tbuf;
for (m = m0; m != NULL; ) {
memcpy(cp, mtod(m, u_char *), m->m_len);
cp += m->m_len;
m = m0 = m_free(m);
}
if (len < DSE_MINSIZE) {
#ifdef DSE_DEBUG
if (sc->sc_debug)
aprint_error_dev(sc->sc_dev,
"packet size %d (%zu) < %d\n", len,
cp - (u_char *)sc->sc_tbuf, DSE_MINSIZE);
#endif
memset(cp, 0, DSE_MINSIZE - len);
len = DSE_MINSIZE;
}
/* Fill out SCSI command. */
memset(&cmd_send, 0, sizeof(cmd_send));
cmd_send.opcode[0] = DAYNA_CMD_SEND;
_lto2b(len, &(cmd_send.length[0]));
cmd_send.byte6 = 0x00;
/* Send command to device. */
error = dse_scsipi_cmd(sc->sc_periph,
(void *)&cmd_send, sizeof(cmd_send),
sc->sc_tbuf, len, DSE_RETRIES,
DSE_TIMEOUT, NULL, XS_CTL_NOSLEEP | XS_CTL_POLL |
XS_CTL_DATA_OUT);
if (error) {
aprint_error_dev(sc->sc_dev,
"not queued, error %d\n", error);
if_statinc(ifp, if_oerrors);
ifp->if_flags &= ~IFF_OACTIVE;
} else
if_statinc(ifp, if_opackets);
}
}
/*
* Called from the scsibus layer via our scsi device switch.
*/
static void
dsedone(struct scsipi_xfer *xs, int error)
{
struct dse_softc *sc = device_private(xs->xs_periph->periph_dev);
struct scsipi_generic *cmd = xs->cmd;
struct ifnet *ifp = &sc->sc_ethercom.ec_if;
if (IS_SEND(cmd)) {
ifp->if_flags &= ~IFF_OACTIVE;
} else if (IS_RECV(cmd)) {
/* RECV complete */
/* pass data up. reschedule a recv */
/* scsipi_free_xs will call start. Harmless. */
if (error) {
/* Reschedule after a delay */
callout_schedule(&sc->sc_recv_ch, dse_poll);
} else {
int n, ntimeo;
n = dse_read(sc, xs->data, xs->datalen - xs->resid);
if (n > dse_max_received)
dse_max_received = n;
if (n == 0)
ntimeo = dse_poll;
else if (n >= RDATA_MAX)
ntimeo = dse_poll0;
else {
ntimeo = sc->sc_last_timeout;
ntimeo = (ntimeo * RDATA_GOAL)/n;
ntimeo = (ntimeo < dse_poll0?
dse_poll0: ntimeo);
ntimeo = (ntimeo > dse_poll?
dse_poll: ntimeo);
}
sc->sc_last_timeout = ntimeo;
callout_schedule(&sc->sc_recv_ch, ntimeo);
}
}
}
/*
* Setup a receive command by queuing the work.
* Usually called from a callout, but also from se_init().
*/
static void
dse_recv_callout(void *v)
{
/* do a recv command */
struct dse_softc *sc = (struct dse_softc *) v;
if (sc->sc_enabled == 0)
return;
mutex_enter(&sc->sc_iflock);
if (sc->sc_recv_work_pending == true) {
callout_schedule(&sc->sc_recv_ch, dse_poll);
mutex_exit(&sc->sc_iflock);
return;
}
sc->sc_recv_work_pending = true;
workqueue_enqueue(sc->sc_recv_wq, &sc->sc_recv_work, NULL);
mutex_exit(&sc->sc_iflock);
}
/*
* Invoke the receive workqueue
*/
static void
dse_recv_worker(struct work *wk, void *cookie)
{
struct dse_softc *sc = (struct dse_softc *) cookie;
dse_recv(sc);
mutex_enter(&sc->sc_iflock);
sc->sc_recv_work_pending = false;
mutex_exit(&sc->sc_iflock);
}
/*
* Do the actual work of receiving data.
*/
static void
dse_recv(struct dse_softc *sc)
{
scsi_dayna_ether_generic cmd_recv;
int error, len;
/* do a recv command */
/* fill out command buffer */
memset(&cmd_recv, 0, sizeof(cmd_recv));
cmd_recv.opcode[0] = DAYNA_CMD_RECV;
len = MAX_BYTES_RX + DSE_EXTRAS_RX;
_lto2b(len, &(cmd_recv.length[0]));
cmd_recv.byte6 = 0xC0;
error = dse_scsipi_cmd(sc->sc_periph,
(void *)&cmd_recv, sizeof(cmd_recv),
sc->sc_rbuf, RBUF_LEN, DSE_RETRIES, DSE_TIMEOUT, NULL,
XS_CTL_NOSLEEP | XS_CTL_POLL | XS_CTL_DATA_IN);
if (error)
callout_schedule(&sc->sc_recv_ch, dse_poll);
}
/*
* We copy the data into mbufs. When full cluster sized units are present
* we copy into clusters.
*/
static struct mbuf *
dse_get(struct dse_softc *sc, uint8_t *data, int totlen)
{
struct ifnet *ifp = &sc->sc_ethercom.ec_if;
struct mbuf *m, *m0, *newm;
int len;
MGETHDR(m0, M_DONTWAIT, MT_DATA);
if (m0 == NULL)
return NULL;
m_set_rcvif(m0, ifp);
m0->m_pkthdr.len = totlen;
len = MHLEN;
m = m0;
while (totlen > 0) {
if (totlen >= MINCLSIZE) {
MCLGET(m, M_DONTWAIT);
if((m->m_flags & M_EXT) == 0)
goto bad;
len = MCLBYTES;
}
if (m == m0) {
char *newdata = (char *)
ALIGN(m->m_data + sizeof(struct ether_header)) -
sizeof(struct ether_header);
len -= newdata - m->m_data;
m->m_data = newdata;
}
m->m_len = len = uimin(totlen, len);
memcpy(mtod(m, void *), data, len);
data += len;
totlen -= len;
if (totlen > 0) {
MGET(newm, M_DONTWAIT, MT_DATA);
if (newm == NULL)
goto bad;
len = MLEN;
m = m->m_next = newm;
}
}
return m0;
bad:
m_freem(m0);
return NULL ;
}
#ifdef MAC68K_DEBUG
static int
peek_packet(uint8_t* buf)
{
struct ether_header *eh;
uint16_t type;
int len;
eh = (struct ether_header*)buf;
type = _2btol((uint8_t*)&(eh->ether_type));
len = sizeof(struct ether_header);
if (type <= ETHERMTU) {
/* for 802.3 */
len += type;
} else{
/* for Ethernet II (DIX) */
switch (type) {
case ETHERTYPE_ARP:
len += 28;
break;
case ETHERTYPE_IP:
len += _2btol(buf + sizeof(struct ether_header) + 2);
break;
default:
len = 0;
goto l_end;
break;
}
}
if (len < DSE_MINSIZE) {
len = DSE_MINSIZE;
}
len += ETHER_CRC_LEN;
l_end:;
return len;
}
#endif
/*
* Pass packets to higher levels.
*/
static int
dse_read(struct dse_softc *sc, uint8_t *data, int datalen)
{
struct mbuf *m;
struct ifnet *ifp = &sc->sc_ethercom.ec_if;
int len;
int n;
#ifdef MAC68K_DEBUG
int peek_flag = 1;
#endif
mutex_enter(&sc->sc_iflock);
n = 0;
while (datalen >= DSE_HEADER_RX) {
/*
* fetch bytes of stream.
* here length = (ether frame length) + (FCS's 4 bytes)
*/
/* fetch frame length */
len = _2btol(data);
/* skip header part */
data += DSE_HEADER_RX;
datalen -= DSE_HEADER_RX;
#if 0 /* 03/10/2001 only for debug */
{
printf("DATALEN %d len %d\n", datalen, len);
int j;
printf("\ndump[%d]: ",n);
for ( j = 0 ; j < datalen ; j++ ) {
printf("%02X ",data[j-DSE_HEADER_RX]);
}
}
#endif
#ifdef MAC68K_DEBUG
if (peek_flag) {
peek_flag = 0;
len = peek_packet(data);
}
#endif
if (len == 0)
break;
#ifdef DSE_DEBUG
aprint_error_dev(sc->sc_dev, "dse_read: datalen = %d, packetlen"
" = %d, proto = 0x%04x\n", datalen, len,
ntohs(((struct ether_header *)data)->ether_type));
#endif
if ((len < (DSE_MINSIZE + ETHER_CRC_LEN)) ||
(MAX_BYTES_RX < len)) {
#ifdef DSE_DEBUG
aprint_error_dev(sc->sc_dev, "invalid packet size "
"%d; dropping\n", len);
#endif
if_statinc(ifp, if_ierrors);
break;
}
/* Don't need crc. Must keep ether header for BPF */
m = dse_get(sc, data, len - ETHER_CRC_LEN);
if (m == NULL) {
#ifdef DSE_DEBUG
if (sc->sc_debug)
aprint_error_dev(sc->sc_dev, "dse_read: "
"dse_get returned null\n");
#endif
if_statinc(ifp, if_ierrors);
goto next_packet;
}
if_statinc(ifp, if_ipackets);
/*
* Check if there's a BPF listener on this interface.
* If so, hand off the raw packet to BPF.
*/
if (ifp->if_bpf)
bpf_mtap(ifp, m, BPF_D_OUT);
/* Pass the packet up. */
if_percpuq_enqueue(sc->sc_ipq, m);
next_packet:
data += len;
datalen -= len;
n++;
}
mutex_exit(&sc->sc_iflock);
return n;
}
static void
dsewatchdog(struct ifnet *ifp)
{
struct dse_softc *sc = ifp->if_softc;
log(LOG_ERR, "%s: device timeout\n", device_xname(sc->sc_dev));
if_statinc(ifp, if_oerrors);
dse_reset(sc);
}
static int
dse_reset(struct dse_softc *sc)
{
int error;
#if 0
/* Maybe we don't *really* want to reset the entire bus
* because the ctron isn't working. We would like to send a
* "BUS DEVICE RESET" message, but don't think the ctron
* understands it.
*/
error = dse_scsipi_cmd(sc->sc_periph, 0, 0, 0, 0, DSE_RETRIES, 2000,
NULL, XS_CTL_RESET);
#endif
error = dse_init(sc);
return error;
}
static int
dse_init_adaptor(struct dse_softc *sc)
{
scsi_dayna_ether_generic cmd_vend1;
u_char tmpbuf[sizeof(cmd_vend1)];
int error;
#if 0 /* 07/21/2001 for test */
/* Maybe we don't *really* want to reset the entire bus
* because the ctron isn't working. We would like to send a
* "BUS DEVICE RESET" message, but don't think the ctron
* understands it.
*/
error = dse_scsipi_cmd(sc->sc_periph, 0, 0, 0, 0, DSE_RETRIES,
2000, NULL, XS_CTL_RESET);
#endif
cmd_vend1 = sonic_ether_vendor1;
error = dse_scsipi_cmd(sc->sc_periph,
(struct scsipi_generic *)&cmd_vend1, sizeof(cmd_vend1),
&(tmpbuf[0]), sizeof(tmpbuf),
DSE_RETRIES, DSE_TIMEOUT, NULL, XS_CTL_POLL | XS_CTL_DATA_IN);
if (error)
goto l_end;
/* wait 500 msec */
kpause("dsesleep", false, hz / 2, NULL);
l_end:
return error;
}
static int
dse_get_addr(struct dse_softc *sc, uint8_t *myaddr)
{
scsi_dayna_ether_generic cmd_get_addr;
u_char tmpbuf[REQ_LEN_GET_ADDR];
int error;
memset(&cmd_get_addr, 0, sizeof(cmd_get_addr));
cmd_get_addr.opcode[0] = DAYNA_CMD_GET_ADDR;
_lto2b(REQ_LEN_GET_ADDR, cmd_get_addr.length);
error = dse_scsipi_cmd(sc->sc_periph,
(struct scsipi_generic *)&cmd_get_addr, sizeof(cmd_get_addr),
tmpbuf, sizeof(tmpbuf),
DSE_RETRIES, DSE_TIMEOUT, NULL, XS_CTL_POLL | XS_CTL_DATA_IN);
if (error == 0) {
memcpy(myaddr, &(tmpbuf[0]), ETHER_ADDR_LEN);
aprint_error_dev(sc->sc_dev, "ethernet address %s\n",
ether_sprintf(myaddr));
}
return error;
}
#if 0 /* 07/16/2000 comment-out */
static int
dse_set_mode(struct dse_softc *sc, int len, int mode)
return 0;
}
#endif
static int
dse_init(struct dse_softc *sc)
{
struct ifnet *ifp = &sc->sc_ethercom.ec_if;
int error = 0;
if ((ifp->if_flags & (IFF_RUNNING | IFF_UP)) == IFF_UP) {
ifp->if_flags |= IFF_RUNNING;
mutex_enter(&sc->sc_iflock);
if (!sc->sc_recv_work_pending) {
sc->sc_recv_work_pending = true;
workqueue_enqueue(sc->sc_recv_wq, &sc->sc_recv_work,
NULL);
}
mutex_exit(&sc->sc_iflock);
ifp->if_flags &= ~IFF_OACTIVE;
mutex_enter(&sc->sc_iflock);
if (!sc->sc_send_work_pending) {
sc->sc_send_work_pending = true;
workqueue_enqueue(sc->sc_send_wq, &sc->sc_send_work,
NULL);
}
mutex_exit(&sc->sc_iflock);
}
return error;
}
static uint8_t BROADCAST_ADDR[ETHER_ADDR_LEN] =
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
static int
dse_set_multi(struct dse_softc *sc)
{
scsi_dayna_ether_generic cmd_set_multi;
struct ether_multistep step;
struct ether_multi *enm;
u_char *cp, *mybuf;
int error, len;
error = 0;
#ifdef DSE_DEBUG
aprint_error_dev(sc->sc_dev, "dse_set_multi\n");
#endif
mybuf = malloc(ETHER_ADDR_LEN * DSE_MCAST_MAX, M_DEVBUF, M_NOWAIT);
if (mybuf == NULL) {
error = EIO;
goto l_end;
}
/*
* copy all entries to transfer buffer
*/
cp = mybuf;
len = 0;
ETHER_FIRST_MULTI(step, &(sc->sc_ethercom), enm);
while ((len < (DSE_MCAST_MAX - 1)) && (enm != NULL)) {
/* ### refer low side entry */
memcpy(cp, enm->enm_addrlo, ETHER_ADDR_LEN);
cp += ETHER_ADDR_LEN;
len++;
ETHER_NEXT_MULTI(step, enm);
}
/* add broadcast address as default */
memcpy(cp, BROADCAST_ADDR, ETHER_ADDR_LEN);
len++;
len *= ETHER_ADDR_LEN;
memset(&cmd_set_multi, 0, sizeof(cmd_set_multi));
cmd_set_multi.opcode[0] = DAYNA_CMD_SET_MULTI;
_lto2b(len, cmd_set_multi.length);
error = dse_scsipi_cmd(sc->sc_periph,
(struct scsipi_generic*)&cmd_set_multi, sizeof(cmd_set_multi),
mybuf, len, DSE_RETRIES, DSE_TIMEOUT, NULL, XS_CTL_POLL | XS_CTL_DATA_OUT);
free(mybuf, M_DEVBUF);
l_end:
return error;
}
static void
dse_stop(struct dse_softc *sc)
{
/* Don't schedule any reads */
callout_stop(&sc->sc_recv_ch);
/* Wait for the workqueues to finish */
mutex_enter(&sc->sc_iflock);
workqueue_wait(sc->sc_recv_wq, &sc->sc_recv_work);
workqueue_wait(sc->sc_send_wq, &sc->sc_send_work);
mutex_exit(&sc->sc_iflock);
/* Abort any scsi cmds in progress */
mutex_enter(chan_mtx(sc->sc_periph->periph_channel));
scsipi_kill_pending(sc->sc_periph);
mutex_exit(chan_mtx(sc->sc_periph->periph_channel));
}
/*
* Process an ioctl request.
*/
static int
dse_ioctl(struct ifnet *ifp, u_long cmd, void *data)
{
struct dse_softc *sc;
struct ifaddr *ifa;
struct ifreq *ifr;
struct sockaddr *sa;
int error;
error = 0;
sc = ifp->if_softc;
ifa = (struct ifaddr *)data;
ifr = (struct ifreq *)data;
switch (cmd) {
case SIOCINITIFADDR:
mutex_enter(&sc->sc_iflock);
if ((error = dse_enable(sc)) != 0)
break;
ifp->if_flags |= IFF_UP;
mutex_exit(&sc->sc_iflock);
#if 0
if ((error = dse_set_media(sc, CMEDIA_AUTOSENSE)) != 0)
break;
#endif
switch (ifa->ifa_addr->sa_family) {
#ifdef INET
case AF_INET:
if ((error = dse_init(sc)) != 0)
break;
arp_ifinit(ifp, ifa);
break;
#endif
#ifdef NETATALK
case AF_APPLETALK:
if ((error = dse_init(sc)) != 0)
break;
break;
#endif
default:
error = dse_init(sc);
break;
}
break;
case SIOCSIFADDR:
mutex_enter(&sc->sc_iflock);
error = dse_enable(sc);
mutex_exit(&sc->sc_iflock);
if (error != 0)
break;
ifp->if_flags |= IFF_UP;
switch (ifa->ifa_addr->sa_family) {
#ifdef INET
case AF_INET:
if ((error = dse_init(sc)) != 0)
break;
arp_ifinit(ifp, ifa);
break;
#endif
#ifdef NETATALK
case AF_APPLETALK:
if ((error = dse_init(sc)) != 0)
break;
break;
#endif
default:
error = dse_init(sc);
break;
}
break;
case SIOCSIFFLAGS:
if ((error = ifioctl_common(ifp, cmd, data)) != 0)
break;
/* XXX re-use ether_ioctl() */
switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
case IFF_RUNNING:
/*
* If interface is marked down and it is running, then
* stop it.
*/
dse_stop(sc);
mutex_enter(&sc->sc_iflock);
ifp->if_flags &= ~IFF_RUNNING;
dse_disable(sc);
mutex_exit(&sc->sc_iflock);
break;
case IFF_UP:
/*
* If interface is marked up and it is stopped, then
* start it.
*/
mutex_enter(&sc->sc_iflock);
error = dse_enable(sc);
mutex_exit(&sc->sc_iflock);
if (error)
break;
error = dse_init(sc);
break;
default:
/*
* Reset the interface to pick up changes in any other
* flags that affect hardware registers.
*/
mutex_enter(&sc->sc_iflock);
if (sc->sc_enabled)
error = dse_init(sc);
mutex_exit(&sc->sc_iflock);
break;
}
#ifdef DSE_DEBUG
if (ifp->if_flags & IFF_DEBUG)
sc->sc_debug = 1;
else
sc->sc_debug = 0;
#endif
break;
case SIOCADDMULTI:
if (sc->sc_enabled == 0) {
error = EIO;
break;
}
mutex_enter(&sc->sc_iflock);
sa = sockaddr_dup(ifreq_getaddr(cmd, ifr), M_WAITOK);
mutex_exit(&sc->sc_iflock);
if (ether_addmulti(sa, &sc->sc_ethercom) == ENETRESET) {
error = dse_set_multi(sc);
#ifdef DSE_DEBUG
aprint_error_dev(sc->sc_dev, "add multi: %s\n",
ether_sprintf(ifr->ifr_addr.sa_data));
#endif
} else
error = 0;
mutex_enter(&sc->sc_iflock);
sockaddr_free(sa);
mutex_exit(&sc->sc_iflock);
break;
case SIOCDELMULTI:
if (sc->sc_enabled == 0) {
error = EIO;
break;
}
mutex_enter(&sc->sc_iflock);
sa = sockaddr_dup(ifreq_getaddr(cmd, ifr), M_WAITOK);
mutex_exit(&sc->sc_iflock);
if (ether_delmulti(sa, &sc->sc_ethercom) == ENETRESET) {
error = dse_set_multi(sc);
#ifdef DSE_DEBUG
aprint_error_dev(sc->sc_dev, "delete multi: %s\n",
ether_sprintf(ifr->ifr_addr.sa_data));
#endif
} else
error = 0;
mutex_enter(&sc->sc_iflock);
sockaddr_free(sa);
mutex_exit(&sc->sc_iflock);
break;
default:
error = ether_ioctl(ifp, cmd, data);
break;
}
return error;
}
/*
* Enable the network interface.
*/
int
dse_enable(struct dse_softc *sc)
{
struct scsipi_periph *periph = sc->sc_periph;
struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
int error = 0;
if (sc->sc_enabled == 0) {
if ((error = scsipi_adapter_addref(adapt)) == 0)
sc->sc_enabled = 1;
else
aprint_error_dev(sc->sc_dev, "device enable failed\n");
}
return error;
}
/*
* Disable the network interface.
*/
void
dse_disable(struct dse_softc *sc)
{
struct scsipi_periph *periph = sc->sc_periph;
struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
if (sc->sc_enabled != 0) {
scsipi_adapter_delref(adapt);
sc->sc_enabled = 0;
}
}
#define DSEUNIT(z) (minor(z))
/*
* open the device.
*/
int
dseopen(dev_t dev, int flag, int fmt, struct lwp *l)
{
int unit, error;
struct dse_softc *sc;
struct scsipi_periph *periph;
struct scsipi_adapter *adapt;
unit = DSEUNIT(dev);
sc = device_lookup_private(&dse_cd, unit);
if (sc == NULL)
return ENXIO;
periph = sc->sc_periph;
adapt = periph->periph_channel->chan_adapter;
if ((error = scsipi_adapter_addref(adapt)) != 0)
return error;
SC_DEBUG(periph, SCSIPI_DB1,
("scopen: dev=0x%"PRIx64" (unit %d (of %d))\n", dev, unit,
dse_cd.cd_ndevs));
periph->periph_flags |= PERIPH_OPEN;
SC_DEBUG(periph, SCSIPI_DB3, ("open complete\n"));
return 0;
}
/*
* close the device.. only called if we are the LAST
* occurence of an open device
*/
int
dseclose(dev_t dev, int flag, int fmt, struct lwp *l)
{
struct dse_softc *sc = device_lookup_private(&dse_cd, DSEUNIT(dev));
struct scsipi_periph *periph = sc->sc_periph;
struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
SC_DEBUG(sc->sc_periph, SCSIPI_DB1, ("closing\n"));
scsipi_wait_drain(periph);
scsipi_adapter_delref(adapt);
periph->periph_flags &= ~PERIPH_OPEN;
return 0;
}
/*
* Perform special action on behalf of the user
* Only does generic scsi ioctls.
*/
int
dseioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
{
struct dse_softc *sc = device_lookup_private(&dse_cd, DSEUNIT(dev));
return (scsipi_do_ioctl(sc->sc_periph, dev, cmd, addr, flag, l));
}
|