|
root / base / usr / src / uts / common / io / upf
upf Plain Text 1379 lines 32.8 KB
   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
/*
 * Copyright (c) 2004-2011 Masayuki Murayama.  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. Neither the name of the author nor the names of its contributors may be
 *    used to endorse or promote products derived from this software without
 *    specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
 * COPYRIGHT OWNER 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.
 */

UPF DRIVER
/*
 * @(#)adm8511reg.h	1.1 09/06/20
 * Register dehinitsions of ADMtek ADM8511 Fast Ethernet to USB controller.
 * Codeded by Masayuki Murayama(KHF04453@nifty.ne.jp)
 * This file is public domain.
 */

#ifndef __ADM8511_H__
#define	__ADM8511_H__

#define	EC0		0x00	/* B */
#define	EC1		0x01	/* B */
#define	EC2		0x02	/* B */
#define	MA		0x08	/* 8byte array */
#define	EID		0x10	/* B */
#define	PAUSETIMER	0x18	/* B pause timer */
#define	RPNBFC		0x1a	/* B */
#define	ORFBFC		0x1b	/* B */
#define	EP1C		0x1c	/* B */
#define	RXFC		0x1d	/* B */
#define	BIST		0x1e	/* B */
#define	EEOFFSET	0x20	/* B */
#define	EEDATA		0x21	/* W */
#define	EECTRL		0x23	/* B */
#define	PHYA		0x25	/* B */
#define	PHYD		0x26 	/* W */
#define	PHYAC		0x28	/* B */
#define	USBSTAT		0x2a	/* B */
#define	ETHTXSTAT	0x2b	/* W */
#define	ETHRXSTAT	0x2d	/* B */
#define	LOSTCNT		0x2e	/* W */
#define	WF0MASK		0x30	/* 16byte array */
#define	WF0OFFSET	0x40	/* W */
#define	WF0CRC		0x41	/* W */
#define	WF1MASK		0x48	/* 16byte array */
#define	WF1OFFSET	0x58	/* W */
#define	WF1CRC		0x59	/* W */
#define	WF2MASK		0x60	/* 16byte array */
#define	WF2OFFSET	0x70	/* W */
#define	WF2CRC		0x71	/* W */
#define	WCTRL		0x78	/* B */
#define	WSTAT		0x7a	/* B */
#define	IPHYC		0x7b	/* B */
#define	GPIO54		0x7c	/* B */
#define	GPIO10		0x7e	/* B */
#define	GPIO32		0x7f	/* B */
#define	TEST		0x80	/* B */
#define	TM		0x81	/* B */
#define	RPN		0x82	/* B */

/* Ethernet control register 0: offset 0 */
#define	EC0_TXE		0x80U
#define	EC0_RXE		0x40U
#define	EC0_RXFCE	0x20U
#define	EC0_WOE		0x10U
#define	EC0_RXSA	0x08U
#define	EC0_SBO		0x04U
#define	EC0_RXMA	0x02U
#define	EC0_RXCS	0x01U

#define	EC0_BITS	\
	"\020"	\
	"\010TXE"	\
	"\007RXE"	\
	"\006RXFCE"	\
	"\005WOE"	\
	"\004RXSA"	\
	"\003SBO"	\
	"\002RXMA"	\
	"\001RXCS"

/* Ethernet control register 1: offset 1 */
#define	EC1_FD		0x20U
#define	EC1_100M	0x10U	/* 0:10Mbps 1:100Mbps */
#define	EC1_RM		0x08U	/* reset mac */

#define	EC1_BITS	\
	"\020"	\
	"\006FD"	\
	"\005100M"	\
	"\004RM"

/* Ethernet control register 2: offset 2 */
#define	EC2_MEPL	0x80U	/* 8515: MTU 0:1528, 1:1638 */
#define	EC2_RPNC	0x40U
#define	EC2_LEEPRS	0x20U
#define	EC2_EEPRW	0x10U
#define	EC2_LB		0x08U
#define	EC2_PROM	0x04U
#define	EC2_RXBP	0x02U
#define	EC2_EP3RC	0x01U

#define	EC2_BITS	\
	"\020"	\
	"\010MEPS"	\
	"\007RPNC"	\
	"\006LEEPRS"	\
	"\005EEPRW"	\
	"\004LB"	\
	"\003PROM"	\
	"\002RXBP"	\
	"\001EP3RC"

/* Recieve Packet number based Flow Control register: offset 0x1a */
#define	RPNBFC_PN	0x7eU	/* */
#define		RPNBFC_PN_SHIFT	1
#define	RPNBFC_FCP	0x01U	/* enable rx flow control */

/* Occupied Recieve FIFO based Flow Control register: offset 0x1b */
#define	ORFBFC_RXS	0x7eU	/* */
#define		ORFBFC_RXS_SHIFT	1
#define		ORFBFC_RXS_UNIT	1024U
#define	ORFBFC_FCRXS	0x01U	/* enable rx flow control */

/* EP1 control register: offset 0x1c */
#define	EP1C_EP1S0E	0x80U	/* send 0 enable */
#define	EP1C_ITMA	0x60U	/* internal test mode A */
#define	EP1C_ITMB	0x1fU	/* internal test mode B */

#define	EP1C_BITS	\
	"\020"	\
	"\010EP1S0E"

/* Rx FIFO Control register: offset 0x1d */
#define	RXFC_EXT_SRAM	0x02	/* enable external 32k sram */
#define	RXFC_RX32PKT	0x01	/* max 32 packet */

/* EEPROM offset register: offset 0x20 */
#define	EEOFFSET_MASK	0x3f	/* eeprom offset address in word */

/* EEPROM access control register: offset 0x23 */
#define	EECTRL_DONE	0x04
#define	EECTRL_RD	0x02
#define	EECTRL_WR	0x01

#define	EECTRL_BITS	\
	"\020"	\
	"\003DONE"	\
	"\002RD"	\
	"\001WR"

/* PHY control register: offset 28 */
#define	PHYAC_DO	0x80U	/* Done */
#define	PHYAC_RDPHY	0x40U	/* read phy */
#define	PHYAC_WRPHY	0x20U	/* write phy */
#define	PHYAC_PHYRA	0x1fU	/* PHY register address */

#define	PHYCTRL_BITS	\
	"\020"	\
	"\010DO"	\
	"\007RDPHY"	\
	"\006WRPHY"

/* Internal PHY control register: offset 7b */
#define	IPHYC_EPHY	0x02
#define	IPHYC_PHYR	0x01

#define	IPHYC_BITS	\
	"\020"	\
	"\002EPHY"	\
	"\001PHYR"

/* GPIO45 register: offset 7c */
#define	GPIO54_5OE	0x20
#define	GPIO54_5O	0x10
#define	GPIO54_5I	0x08
#define	GPIO54_4OE	0x04
#define	GPIO54_4O	0x02
#define	GPIO54_4I	0x01

/* GPIO01 register: offset 7e */
#define	GPIO10_1OE	0x20
#define	GPIO10_1O	0x10
#define	GPIO10_1I	0x08
#define	GPIO10_0OE	0x04
#define	GPIO10_0O	0x02
#define	GPIO10_0I	0x01

/* GPIO23 register: offset 7f */
#define	GPIO32_3OE	0x20
#define	GPIO32_3O	0x10
#define	GPIO32_3I	0x08
#define	GPIO32_2OE	0x04
#define	GPIO32_2O	0x02
#define	GPIO32_2I	0x01

/* rx status at the end of received packets */
/* byte 0 and 1 is packet length in little endian */
/* byte 2 is receive status */
#define	RSR_DRIBBLE	0x10
#define	RSR_CRC		0x08
#define	RSR_RUNT	0x04
#define	RSR_LONG	0x02
#define	RSR_MULTI	0x01

#define	RSR_ERRORS	\
	(RSR_DRIBBLE | RSR_CRC | RSR_RUNT | RSR_LONG | RSR_MULTI)

#define	RSR_BITS	\
	"\020"	\
	"\005DRIBBLE"	\
	"\004CRC"	\
	"\003RUNT"	\
	"\002LONG"	\
	"\001MULTI"
/* byte 3 is reserved */

/* TEST register: offset 80 */

#endif /* __ADM8511_H__ */
/*
 * upf_usbgem.c : ADMtek an986/adm8511/adm8513/adm8515 USB to
 * Fast Ethernet Driver for Solaris
 */

/*
 * Copyright (c) 2004-2011 Masayuki Murayama.  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. Neither the name of the author nor the names of its contributors may be
 *    used to endorse or promote products derived from this software without
 *    specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
 * COPYRIGHT OWNER 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.
 */

/*
 *  Changelog:
 */

/*
 * TODO
 */
/* ======================================================= */

/*
 * Solaris system header files and macros
 */
#include <sys/types.h>
#include <sys/conf.h>
#include <sys/debug.h>
#include <sys/kmem.h>
#include <sys/modctl.h>
#include <sys/errno.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/byteorder.h>

/* ethernet stuff */
#include <sys/ethernet.h>

/* interface card depend stuff */
#include <sys/stropts.h>
#include <sys/stream.h>
#include <sys/strlog.h>
#include <sys/usb/usba.h>
#include "usbgem.h"

/* hardware stuff */
#include "usbgem_mii.h"
#include "adm8511reg.h"

char	ident[] = "pegasus usbnic driver v" VERSION;

/*
 * Useful macros
 */
#define	CHECK_AND_JUMP(val, label)	\
	if ((val) != USB_SUCCESS) { goto label; }

/*
 * Debugging
 */
#ifdef DEBUG_LEVEL
static int upf_debug = DEBUG_LEVEL;
#define	DPRINTF(n, args)	if (upf_debug > (n)) cmn_err args
#else
#define	DPRINTF(n, args)
#endif

/*
 * Our configration for ADMtek Pegasus/PegasusII
 */
/* timeouts */
#define	ONESEC		(drv_usectohz(1*1000000))

/*
 * Local device definitions
 */
struct upf_dev {
	/*
	 * Misc HW information
	 */
	uint8_t		ec[3];
	uint8_t		mac_addr[ETHERADDRL];
	int		chip_type;
#define	CHIP_AN986	1	/* avoid 0 */
#define	CHIP_ADM8511	2	/* including adm8515 */
#define	CHIP_ADM8513	3
	boolean_t	phy_init_done;
	uint8_t		last_link_state;

	uint16_t	vid;	/* vendor id */
	uint16_t	pid;	/* product id */
};

/*
 * private functions
 */

/* mii operations */
static uint16_t upf_mii_read(struct usbgem_dev *, uint_t, int *errp);
static void upf_mii_write(struct usbgem_dev *, uint_t, uint16_t, int *errp);

/* nic operations */
static int upf_attach_chip(struct usbgem_dev *);
static int upf_reset_chip(struct usbgem_dev *);
static int upf_init_chip(struct usbgem_dev *);
static int upf_start_chip(struct usbgem_dev *);
static int upf_stop_chip(struct usbgem_dev *);
static int upf_set_media(struct usbgem_dev *);
static int upf_set_rx_filter(struct usbgem_dev *);
static int upf_get_stats(struct usbgem_dev *);

/* packet operations */
static mblk_t *upf_tx_make_packet(struct usbgem_dev *, mblk_t *);
static mblk_t *upf_rx_make_packet(struct usbgem_dev *, mblk_t *);

/* interrupt handler */
static void upf_interrupt(struct usbgem_dev *, mblk_t *);

/* =============================================================== */
/*
 * I/O functions
 */
/* =============================================================== */
#define	UPF_REQ_GET_REGISTER	0xf0
#define	UPF_REQ_SET_REGISTER	0xf1
#define	OUTB(dp, p, v, errp, label)	\
	if ((*(errp) = usbgem_ctrl_out((dp),	\
	/* bmRequestType */ USB_DEV_REQ_HOST_TO_DEV	\
		| USB_DEV_REQ_TYPE_VENDOR | USB_DEV_REQ_RCPT_DEV,	\
	/* bRequest */	UPF_REQ_SET_REGISTER,	\
	/* wValue */	(v),	\
	/* wIndex */	(p),	\
	/* wLength */	1,	\
	/* buf */	NULL,	\
	/* size */	0)) != USB_SUCCESS) goto label;

#define	OUTW(dp, p, v, errp, label)	\
	if ((*(errp) = usbgem_ctrl_out_val((dp),	\
	/* bmRequestType */ USB_DEV_REQ_HOST_TO_DEV	\
		| USB_DEV_REQ_TYPE_VENDOR | USB_DEV_REQ_RCPT_DEV,	\
	/* bRequest */	UPF_REQ_SET_REGISTER,	\
	/* wValue */	0,	\
	/* wIndex */	(p),	\
	/* wLength */	2,	\
	/* value */	(v))) != USB_SUCCESS) goto label

#define	OUTS(dp, p, buf, len, errp, label)	\
	if ((*(errp) = usbgem_ctrl_out((dp),	\
	/* bmRequestType */ USB_DEV_REQ_HOST_TO_DEV	\
		| USB_DEV_REQ_TYPE_VENDOR | USB_DEV_REQ_RCPT_DEV,	\
	/* bRequest */	UPF_REQ_SET_REGISTER,	\
	/* wValue */	0,	\
	/* wIndex */	(p),	\
	/* wLength */	(len),	\
	/* buf */	(buf),	\
	/* size */	(len))) != USB_SUCCESS) goto label

#define	INB(dp, p, vp, errp, label)	\
	if ((*(errp) = usbgem_ctrl_in_val((dp),		\
	/* bmRequestType */ USB_DEV_REQ_DEV_TO_HOST	\
		| USB_DEV_REQ_TYPE_VENDOR | USB_DEV_REQ_RCPT_DEV,	\
	/* bRequest */ UPF_REQ_GET_REGISTER,	\
	/* wValue */	0,	\
	/* wIndex */	(p),	\
	/* wLength */	1,	\
	/* valuep */	(vp))) != USB_SUCCESS) goto label

#define	INW(dp, p, vp, errp, label)	\
	if ((*(errp) = usbgem_ctrl_in_val((dp),		\
	/* bmRequestType */ USB_DEV_REQ_DEV_TO_HOST	\
		| USB_DEV_REQ_TYPE_VENDOR | USB_DEV_REQ_RCPT_DEV,	\
	/* bRequest */ UPF_REQ_GET_REGISTER,	\
	/* wValue */	0,	\
	/* wIndex */	(p),	\
	/* wLength */	2,	\
	/* valuep */	(vp))) != USB_SUCCESS) goto label

#define	INS(dp, p, buf, len, errp, label)	\
	if ((*(errp) = usbgem_ctrl_in((dp),	\
	/* bmRequestType */ USB_DEV_REQ_DEV_TO_HOST	\
		    | USB_DEV_REQ_TYPE_VENDOR | USB_DEV_REQ_RCPT_DEV,	\
	/* bRequest */ UPF_REQ_GET_REGISTER,	\
	/* wValue */	0,	\
	/* wIndex */	(p),	\
	/* wLength */	(len),	\
	/* buf */	(buf),	\
	/* size */	(len))) != USB_SUCCESS) goto label

/* =============================================================== */
/*
 * Hardware manupilation
 */
/* =============================================================== */
static int
upf_reset_chip(struct usbgem_dev *dp)
{
	int		i;
	uint8_t		val;
	int		err;
	struct upf_dev	*lp = dp->private;

	DPRINTF(0, (CE_CONT, "!%s: %s: called", dp->name, __func__));
	bzero(lp->mac_addr, sizeof (lp->mac_addr));

	lp->ec[1] = 0;
	OUTB(dp, EC1, EC1_RM, &err, usberr);

	for (i = 0; i < 1000; i++) {
		INB(dp, EC1, &val, &err, usberr);
		if ((val & EC1_RM) == 0) {
			lp->ec[1] = val;
			return (USB_SUCCESS);
		}
		drv_usecwait(10);
	}

	/* time out */
	cmn_err(CE_WARN, "!%s: failed to reset: timeout", dp->name);
	return (USB_FAILURE);

usberr:
	cmn_err(CE_NOTE, "!%s: %s: usberr detected", dp->name, __func__);
	return (USB_FAILURE);
}

/*
 * Setup an986/adm8511/adm8513/adm8515
 */
static int
upf_init_chip(struct usbgem_dev *dp)
{
	uint64_t	zero64 = 0;
	int	err = USB_SUCCESS;
	struct upf_dev	*lp = dp->private;

	DPRINTF(0, (CE_CONT, "!%s: %s: called", dp->name, __func__));

	/* ethernet control register 0 */
	lp->ec[0] |= EC0_RXSA | EC0_RXCS;
	OUTB(dp, EC0, lp->ec[0], &err, usberr);

	/* ethernet control reg1: will be set later in set_rx_filter() */

	/* ethernet control register 2: will be set later in set_rx_filter() */
	INB(dp, EC2, &lp->ec[2], &err, usberr);
	lp->ec[2] |= EC2_RXBP | EC2_EP3RC;
#ifdef CONFIG_VLAN
	if (dp->misc_flag & USBGEM_VLAN) {
		lp->ec[2] |= EC2_MEPL;
	}
#endif
	OUTB(dp, EC2, lp->ec[2], &err, usberr);

	/* Multicast address hash: clear */
	OUTS(dp, MA, &zero64, 8, &err, usberr);

	/* Ethernet ID : will be set later in upf_set_rx_filter() */

	/* PAUSE timer */
	OUTB(dp, PAUSETIMER, 0x1f, &err, usberr);

	/* receive packet number based pause control:set in upf_set_media() */

	/* occupied receive FIFO based pause control:set in upf_set_media() */

	/* EP1 control: default */

	/* Rx FIFO control */
	if (lp->chip_type != CHIP_AN986) {
		/* use 24K internal sram, 16pkts in fifo */
		OUTB(dp, RXFC, 0, &err, usberr);
	}

	/* BIST contror: do nothing */
	err = upf_set_media(dp);
	CHECK_AND_JUMP(err, usberr);

	DPRINTF(2, (CE_CONT, "!%s: %s: end (success)", dp->name, __func__));
	return (USB_SUCCESS);

usberr:
	cmn_err(CE_NOTE, "!%s: %s: usberr(%d) detected",
	    dp->name, __func__, err);
	return (err);
}

static int
upf_start_chip(struct usbgem_dev *dp)
{
	int	err = USB_SUCCESS;
	struct upf_dev	*lp = dp->private;

	DPRINTF(0, (CE_CONT, "!%s: %s: called", dp->name, __func__));

	/* enable RX and TX */
	lp->ec[0] |= EC0_TXE | EC0_RXE;
	OUTB(dp, EC0, lp->ec[0], &err, usberr);
	return (USB_SUCCESS);

usberr:
	cmn_err(CE_WARN, "!%s: %s: usberr(%d) detected",
	    dp->name, __func__, err);
	return (err);
}

static int
upf_stop_chip(struct usbgem_dev *dp)
{
	int	err;
	struct upf_dev	*lp = dp->private;

	DPRINTF(0, (CE_CONT, "!%s: %s: called", dp->name, __func__));

	/* disable RX and TX */
	lp->ec[0] &= ~(EC0_TXE | EC0_RXE);
	OUTB(dp, EC0, lp->ec[0], &err, usberr);

	return (USB_SUCCESS);

usberr:
	cmn_err(CE_WARN, "!%s: %s: usberr(%d) detected",
	    dp->name, __func__, err);
	return (err);
}

static int
upf_get_stats(struct usbgem_dev *dp)
{
	/* do nothing */
	return (USB_SUCCESS);
}

static uint_t
upf_mcast_hash(struct usbgem_dev *dp, const uint8_t *addr)
{
	/* hash table is 64 = 2^6 bit width */
	return (usbgem_ether_crc_le(addr) & 0x3f);
}

static int
upf_set_rx_filter(struct usbgem_dev *dp)
{
	int		i;
	int		err;
#ifdef DEBUG_LEVEL
	uint8_t		reg0;
	uint8_t		reg1;
	uint8_t		reg2;
#endif
	struct upf_dev	*lp = dp->private;

	DPRINTF(0, (CE_CONT, "!%s: %s: called, rxmode:%b",
	    dp->name, __func__, dp->rxmode, RXMODE_BITS));

	/* reset rx mode */
	lp->ec[0] &= ~EC0_RXMA;
	lp->ec[2] &= ~EC2_PROM;

	if (dp->rxmode & RXMODE_PROMISC) {
		/* promiscious mode implies all multicast and all physical */
		lp->ec[0] |= EC0_RXMA;
		lp->ec[2] |= EC2_PROM;
	} else if ((dp->rxmode & RXMODE_ALLMULTI) || dp->mc_count > 0) {
		/* XXX - multicast hash table didin't work */
		/* accept all multicast packets */
		lp->ec[0] |= EC0_RXMA;
	}

	if (bcmp(dp->cur_addr.ether_addr_octet,
	    lp->mac_addr, ETHERADDRL) != 0) {

		/* need to update mac address */
		bcopy(dp->cur_addr.ether_addr_octet,
		    lp->mac_addr, ETHERADDRL);
		OUTS(dp, EID,
		    lp->mac_addr, ETHERADDRL, &err, usberr);
	}

	/* update rx mode */
	OUTS(dp, EC0, lp->ec, 3, &err, usberr);

#if DEBUG_LEVEL > 0
	INB(dp, EC0, &reg0, &err, usberr);
	INB(dp, EC1, &reg1, &err, usberr);
	INB(dp, EC2, &reg2, &err, usberr);

	cmn_err(CE_CONT, "!%s: %s: returned, ec:%b %b %b",
	    dp->name, __func__,
	    reg0, EC0_BITS, reg1, EC1_BITS, reg2, EC2_BITS);
#endif
	return (USB_SUCCESS);

usberr:
	cmn_err(CE_NOTE, "!%s: %s: usberr detected", dp->name, __func__);
	return (err);
}

static int
upf_set_media(struct usbgem_dev *dp)
{
	int	err;
	struct upf_dev	*lp = dp->private;

	DPRINTF(0, (CE_CONT, "!%s: %s: called", dp->name, __func__));

	lp->ec[1] &= ~(EC1_FD | EC1_100M);

	/* select duplex */
	if (dp->full_duplex) {
		lp->ec[1] |= EC1_FD;
	}

	/* select speed */
	if (dp->speed == USBGEM_SPD_100) {
		lp->ec[1] |= EC1_100M;
	}

	/* rx flow control */
	switch (dp->flow_control) {
	case FLOW_CONTROL_SYMMETRIC:
	case FLOW_CONTROL_RX_PAUSE:
		lp->ec[0] |= EC0_RXFCE;
		break;

	default:
		lp->ec[0] &= ~EC0_RXFCE;
		break;
	}

	/* tx flow control */
	switch (dp->flow_control) {
	case FLOW_CONTROL_SYMMETRIC:
	case FLOW_CONTROL_TX_PAUSE:
		if (lp->chip_type != CHIP_AN986) {
			/* pegasus II has internal 24k fifo */
			OUTB(dp, ORFBFC,
			    (12 << ORFBFC_RXS_SHIFT) |  ORFBFC_FCRXS,
			    &err, usberr);

			/* 16 packts can be stored in rx fifo */
			OUTB(dp, RPNBFC_PN,
			    (8 << RPNBFC_PN_SHIFT) |  RPNBFC_FCP,
			    &err, usberr);
		} else {
			/* an986 has external 32k fifo */
			OUTB(dp, ORFBFC,
			    (16 << ORFBFC_RXS_SHIFT) |  ORFBFC_FCRXS,
			    &err, usberr);

			/* AN986 fails to link up when RPNBFC is enabled */
			OUTB(dp, RPNBFC, 0, &err, usberr);
		}
		break;

	default:
		OUTB(dp, ORFBFC, 0, &err, usberr);
		OUTB(dp, RPNBFC, 0, &err, usberr);
		break;
	}

	/* update ether control registers */
	OUTS(dp, EC0, lp->ec, 2, &err, usberr);
	DPRINTF(0, (CE_CONT, "!%s: %s: returned, ec0:%b, ec1:%b",
	    dp->name, __func__, lp->ec[0], EC0_BITS, lp->ec[1], EC1_BITS));

	return (USB_SUCCESS);

usberr:
	cmn_err(CE_WARN, "%s: %s: failed to write ec1", dp->name, __func__);
	return (err);
}

/*
 * send/receive packet check
 */
static mblk_t *
upf_tx_make_packet(struct usbgem_dev *dp, mblk_t *mp)
{
	size_t		len;
	mblk_t		*new;
	mblk_t		*tp;
	uint8_t		*bp;
	uint8_t		*last_pos;
	int		msglen;

	DPRINTF(3, (CE_CONT, "!%s: %s: called", dp->name, __func__));

	len = msgdsize(mp);
	if (len < ETHERMIN) {
		len = ETHERMIN;
	}

	/* allocate msg block */
	msglen = len + sizeof (uint16_t);

	/* avoid usb controller bug */
	if ((msglen & 0x3f) == 0) {
		/* add a header for additional 0-length usb message */
		msglen += sizeof (uint16_t);
	}

	if ((new = allocb(msglen, 0)) == NULL) {
		return (NULL);
	}

	/* copy contents of the buffer */
	new->b_wptr = new->b_rptr + msglen;
	bp = new->b_rptr;

	/* the nic requires a two byte header of the packet size */
	bp[0] = (uint8_t)len;
	bp[1] = (uint8_t)(len >> 8);
	bp += sizeof (uint16_t);

	/* copy the payload */
	for (tp = mp; tp; tp = tp->b_cont) {
		len = (uintptr_t)tp->b_wptr - (uintptr_t)tp->b_rptr;
		if (len > 0) {
			bcopy(tp->b_rptr, bp, len);
			bp += len;
		}
	}

	/* clear ethernet pads and additional usb header if we have */
	last_pos = new->b_wptr;
	while (bp < last_pos) {
		*bp++ = 0;
	}

	return (new);
}

static void
upf_dump_packet(struct usbgem_dev *dp, uint8_t *bp, int n)
{
	int	i;

	for (i = 0; i < n; i += 8, bp += 8) {
		cmn_err(CE_CONT, "%02x %02x %02x %02x %02x %02x %02x %02x",
		    bp[0], bp[1], bp[2], bp[3], bp[4], bp[5], bp[6], bp[7]);
	}
}

static mblk_t *
upf_rx_make_packet(struct usbgem_dev *dp, mblk_t *mp)
{
	uint8_t		*p;
	uint16_t	rxhd;
	uint_t		len;
	uint8_t		rsr;
	struct upf_dev	*lp = dp->private;

	ASSERT(mp != NULL);

#ifdef DEBUG_LEVEL
	len = msgdsize(mp);
	DPRINTF(2, (CE_CONT, "!%s: time:%d %s: cont:%p",
	    dp->name, ddi_get_lbolt(), __func__, len, mp->b_cont));

	if (upf_debug > 3) {
		upf_dump_packet(dp, mp->b_rptr, max(6, len));
	}
#endif
	/* get the length of Rx packet */
	p = mp->b_wptr - 4;
	rsr = p[3];
	if (lp->chip_type == CHIP_ADM8513) {
		/* As Rx packets from ADM8513 have two byte header, remove it */
		p = mp->b_rptr;
		len = ((p[1] << 8) | p[0]) & 0x0fff;
		mp->b_rptr += 2;
	} else {
		len = (((p[1] << 8) | p[0]) & 0x0fff) - ETHERFCSL - 4;
	}

	DPRINTF(2, (CE_CONT, "!%s: %s: rsr:%b len:%d",
	    dp->name, __func__, rsr, RSR_BITS, len));

	/* check if error happen */
	if (rsr & RSR_ERRORS) {
		DPRINTF(0, (CE_CONT, "!%s: rsr:%b", dp->name, rsr, RSR_BITS));
		if (rsr & (RSR_CRC | RSR_DRIBBLE)) {
			dp->stats.frame++;
		}
		if (rsr & RSR_LONG) {
			dp->stats.frame_too_long++;
		}
		if (rsr & RSR_RUNT) {
			dp->stats.runt++;
		}

		dp->stats.errrcv++;
		return (NULL);
	}
#ifndef CONFIG_VLAN
	/* check packet size */
	if (len > ETHERMAX) {
		/* too long */
		dp->stats.frame_too_long++;
		dp->stats.errrcv++;
		return (NULL);
	} else if (len < ETHERMIN) {
		dp->stats.runt++;
		dp->stats.errrcv++;
		return (NULL);
	}
#endif
	/* remove tailing crc and rx status fields */
	mp->b_wptr = mp->b_rptr + len;
	ASSERT(mp->b_next == NULL);
	return (mp);
}

/*
 * Device depend interrupt handler
 */
static void
upf_interrupt(struct usbgem_dev *dp, mblk_t *mp)
{
	uint8_t	*bp;
	struct upf_dev	*lp = dp->private;

	bp = mp->b_rptr;

	DPRINTF(2, (CE_CONT,
	    "!%s: %s: size:%d, %02x %02x %02x %02x %02x %02x %02x %02x",
	    dp->name, __func__, mp->b_wptr - mp->b_rptr,
	    bp[0], bp[1], bp[2], bp[3], bp[4], bp[5], bp[6], bp[7]));

	if ((lp->last_link_state ^ bp[5]) & 1) {
		DPRINTF(1, (CE_CONT, "!%s:%s link status changed:",
		    dp->name, __func__));
		usbgem_mii_update_link(dp);
	}

	lp->last_link_state = bp[5] & 1;
}

/*
 * MII Interfaces
 */
static uint16_t
upf_mii_read(struct usbgem_dev *dp, uint_t index, int *errp)
{
	uint8_t		phyctrl;
	uint16_t	val;
	int		i;

	DPRINTF(4, (CE_CONT, "!%s: %s: called, ix:%d",
	    dp->name, __func__, index));
	ASSERT(index >= 0 && index < 32);

	*errp = USB_SUCCESS;

	/* set PHYADDR */
	OUTB(dp, PHYA, dp->mii_phy_addr, errp, usberr);

	/* Initiate MII read transaction */
	OUTB(dp, PHYAC, index | PHYAC_RDPHY, errp, usberr);

	for (i = 0; i < 100; i++) {
		INB(dp, PHYAC, &phyctrl, errp, usberr);
		if (phyctrl & PHYAC_DO) {
			/* done */
			INW(dp, PHYD, &val, errp, usberr);
			DPRINTF(4, (CE_CONT, "!%s: %s: return %04x",
			    dp->name, __func__, val));
			return (val);
		}
		drv_usecwait(10);
	}
	/* timeout */
	cmn_err(CE_WARN, "!%s: %s: timeout detected", dp->name, __func__);
	*errp = USB_FAILURE;
	return (0);

usberr:
	cmn_err(CE_CONT,
	    "!%s: %s: usberr(%d) detected", dp->name, __func__, *errp);
	return (0);
}

static void
upf_mii_write(struct usbgem_dev *dp, uint_t index, uint16_t val, int *errp)
{
	int		i;
	uint8_t		phyctrl;

	DPRINTF(4, (CE_CONT, "!%s: %s called index:%d val:0x%04x",
	    dp->name, __func__, index, val));
	ASSERT(index >= 0 && index < 32);

	*errp = USB_SUCCESS;

	OUTW(dp, PHYD, val, errp, usberr);
	OUTB(dp, PHYA, dp->mii_phy_addr, errp, usberr);
	OUTB(dp, PHYAC, index | PHYAC_WRPHY, errp, usberr);

	for (i = 0; i < 100; i++) {
		INB(dp, PHYAC, &phyctrl, errp, usberr);
		if (phyctrl & PHYAC_DO) {
			/* done */
			return;
		}
		drv_usecwait(10);
	}

	/* time out */
	cmn_err(CE_WARN, "!%s: %s: timeout detected", dp->name, __func__);
	*errp = USB_FAILURE;
	return;

usberr:
	cmn_err(CE_CONT,
	    "!%s: %s: usberr(%d) detected", dp->name, __func__, *errp);
}


static int
upf_enable_phy(struct usbgem_dev *dp)
{
	uint8_t	val;
	int	err;
	struct upf_dev	*lp = dp->private;

	/*
	 * first, try to enable internal phy
	 */
	INB(dp, IPHYC, &val, &err, usberr);
	val = (val | IPHYC_EPHY) & ~IPHYC_PHYR;
	OUTB(dp, IPHYC, val, &err, usberr);

	INB(dp, IPHYC, &val, &err, usberr);
	DPRINTF(0, (CE_CONT, "!%s: %s: IPHYC: %b",
	    dp->name, __func__, val, IPHYC_BITS));
	if (val) {
		/* reset internal phy */
		OUTB(dp, IPHYC, val | IPHYC_PHYR, &err, usberr);
		OUTB(dp, IPHYC, val, &err, usberr);
		delay(drv_usectohz(10000));

		/* identify the chip generation */
		OUTB(dp, 0x83, 0xa5, &err, usberr);
		INB(dp, 0x83, &val, &err, usberr);
		if (val == 0xa5) {
			lp->chip_type = CHIP_ADM8513;
		} else {
			/* adm8511 or adm8515 */
			lp->chip_type = CHIP_ADM8511;
		}
		dp->ugc.usbgc_mii_hw_link_detection = B_TRUE;
	} else {
		/*
		 * It should be AN986 which doesn't have an internal PHY.
		 * We need to setup gpio ports in AN986, which are
		 * connected to external PHY control pins.
		 */
		lp->chip_type = CHIP_AN986;

		/* reset external phy */
		/* output port#0 L, port#1 L */
		OUTB(dp, GPIO10, GPIO10_0O | GPIO10_0OE, &err, usberr);

		/* output port#0 H, port#1 L */
		OUTB(dp, GPIO10,
		    GPIO10_0O | GPIO10_0OE | GPIO10_1OE, &err, usberr);

		/* hw link detection doesn't work correctly */
		dp->ugc.usbgc_mii_hw_link_detection = B_FALSE;
	}

	return (USB_SUCCESS);

usberr:
	cmn_err(CE_NOTE, "!%s: %s: usberr detected", dp->name, __func__);
	return (USB_FAILURE);
}

static int
upf_mii_probe(struct usbgem_dev *dp)
{
	int	err;
	uint16_t	val;
	struct upf_dev	*lp = dp->private;

	if (!lp->phy_init_done) {
		upf_enable_phy(dp);
		lp->phy_init_done = B_TRUE;
	}

	return (usbgem_mii_probe_default(dp));
}

static int
upf_mii_init(struct usbgem_dev *dp)
{
	uint16_t	val;
	int		err = USB_SUCCESS;
	struct upf_dev	*lp = dp->private;

	if (!lp->phy_init_done) {
		upf_enable_phy(dp);
	}
	lp->phy_init_done = B_FALSE;

	if (lp->chip_type == CHIP_AN986 &&
	    (lp->vid == 0x0db7 /* elecom */ ||
	    lp->vid == 0x066b /* linksys */ ||
	    lp->vid == 0x077b /* linksys */ ||
	    lp->vid == 0x2001 /* dlink */)) {
		/* special treatment for Linksys products */
		val = upf_mii_read(dp, 0x1b, &err) | 0x4;
		upf_mii_write(dp, 0x1b, val, &err);
	}
	return (err);
}

/* ======================================================== */
/*
 * OS depend (device driver DKI) routine
 */
/* ======================================================== */
static uint16_t
upf_read_eeprom(struct usbgem_dev *dp, int index, int *errp)
{
	int		i;
	uint8_t		eectrl;
	uint16_t	data;

	*errp = USB_SUCCESS;

	OUTB(dp, EECTRL, 0, errp, usberr);

	OUTB(dp, EEOFFSET, index, errp, usberr);
	OUTB(dp, EECTRL, EECTRL_RD, errp, usberr);

	for (i = 0; i < 100; i++) {
		INB(dp, EECTRL, &eectrl, errp, usberr);
		if (eectrl & EECTRL_DONE) {
			INW(dp, EEDATA, &data, errp, usberr);
			return (data);
		}
		drv_usecwait(10);
	}

	/* time out */
	*errp = USB_FAILURE;
	return (0);

usberr:
	cmn_err(CE_CONT,
	    "!%s: %s: usberr(%d) detected", dp->name, __func__, *errp);
	return (0);
}

static void
upf_eeprom_dump(struct usbgem_dev *dp, int size)
{
	int	i;
	int	err;

	cmn_err(CE_CONT, "!%s: %s dump:", dp->name, __func__);

	for (i = 0; i < size; i += 4) {
		cmn_err(CE_CONT, "!0x%02x: 0x%04x 0x%04x 0x%04x 0x%04x",
		    i*2,
		    upf_read_eeprom(dp, i + 0, &err),
		    upf_read_eeprom(dp, i + 1, &err),
		    upf_read_eeprom(dp, i + 2, &err),
		    upf_read_eeprom(dp, i + 3, &err));
	}
}

static int
upf_attach_chip(struct usbgem_dev *dp)
{
	int		i;
	int		err;
	uint16_t	val;
	uint8_t		*mac;
	struct upf_dev	*lp = dp->private;

	/*
	 * Read mac address from EEPROM
	 */
	mac = dp->dev_addr.ether_addr_octet;
	for (i = 0; i < 3; i++) {
		val = upf_read_eeprom(dp, i, &err);
		if (err != USB_SUCCESS) {
			goto usberr;
		}
		mac[i*2+0] = (uint8_t)val;
		mac[i*2+1] = (uint8_t)(val >> 8);
	}

	DPRINTF(0, (CE_CONT,
	    "%s: %s: mac: %02x:%02x:%02x:%02x:%02x:%02x",
	    dp->name, __func__,
	    mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]));

	dp->misc_flag = 0;
#ifdef CONFIG_VLAN
	dp->misc_flag |= USBGEM_VLAN;
#endif
#if DEBUG_LEVEL > 3
	upf_eeprom_dump(dp, 0x80);
#endif
	return (USB_SUCCESS);

usberr:
	cmn_err(CE_WARN, "!%s: %s: usb error detected", dp->name, __func__);
	return (USB_FAILURE);
}

static int
upfattach(dev_info_t *dip, ddi_attach_cmd_t cmd)
{
	int			i;
	ddi_iblock_cookie_t	c;
	int			ret;
	int			unit;
	uint32_t		tcr;
	int			len;
	const char		*drv_name;
	struct usbgem_dev	*dp;
	void			*base;
	struct usbgem_conf	*ugcp;
	struct upf_dev		*lp;

	unit = ddi_get_instance(dip);
	drv_name = ddi_driver_name(dip);

	DPRINTF(3, (CE_CONT, "!%s%d: %s: called, cmd:%d",
	    drv_name, unit, __func__, cmd));

	if (cmd == DDI_ATTACH) {
		/*
		 * construct usbgem configration
		 */
		ugcp = kmem_zalloc(sizeof (*ugcp), KM_SLEEP);

		/* name */
		(void) sprintf(ugcp->usbgc_name, "%s%d", drv_name, unit);
		ugcp->usbgc_ppa = unit;

		ugcp->usbgc_ifnum = 0;
		ugcp->usbgc_alt = 0;

		ugcp->usbgc_tx_list_max = 16;

		ugcp->usbgc_rx_header_len = 4;
		ugcp->usbgc_rx_list_max = 64;

		/* time out parameters */
		ugcp->usbgc_tx_timeout = USBGEM_TX_TIMEOUT;
		ugcp->usbgc_tx_timeout_interval = USBGEM_TX_TIMEOUT_INTERVAL;

		/* flow control */
		ugcp->usbgc_flow_control = FLOW_CONTROL_NONE;
		ugcp->usbgc_flow_control = FLOW_CONTROL_RX_PAUSE;

		/* MII timeout parameters */
		ugcp->usbgc_mii_link_watch_interval = ONESEC;
		ugcp->usbgc_mii_an_watch_interval = ONESEC/5;
		ugcp->usbgc_mii_reset_timeout = MII_RESET_TIMEOUT; /* 1 sec */
		ugcp->usbgc_mii_an_timeout = MII_AN_TIMEOUT;	/* 5 sec */
		ugcp->usbgc_mii_an_wait = MII_AN_TIMEOUT/2;
		ugcp->usbgc_mii_linkdown_timeout = MII_LINKDOWN_TIMEOUT;
		ugcp->usbgc_mii_an_delay = ONESEC/10;

		ugcp->usbgc_mii_linkdown_action = MII_ACTION_RESET;
		ugcp->usbgc_mii_linkdown_timeout_action = MII_ACTION_RESET;
		ugcp->usbgc_mii_dont_reset = B_FALSE;

		/* I/O methods */

		/* mac operation */
		ugcp->usbgc_attach_chip = &upf_attach_chip;
		ugcp->usbgc_reset_chip = &upf_reset_chip;
		ugcp->usbgc_init_chip = &upf_init_chip;
		ugcp->usbgc_start_chip = &upf_start_chip;
		ugcp->usbgc_stop_chip = &upf_stop_chip;
		ugcp->usbgc_multicast_hash = &upf_mcast_hash;

		ugcp->usbgc_set_rx_filter = &upf_set_rx_filter;
		ugcp->usbgc_set_media = &upf_set_media;
		ugcp->usbgc_get_stats = &upf_get_stats;
		ugcp->usbgc_interrupt = &upf_interrupt;

		/* packet operation */
		ugcp->usbgc_tx_make_packet = &upf_tx_make_packet;
		ugcp->usbgc_rx_make_packet = &upf_rx_make_packet;

		/* mii operations */
		ugcp->usbgc_mii_probe = &upf_mii_probe;
		ugcp->usbgc_mii_init = &upf_mii_init;
		ugcp->usbgc_mii_config = &usbgem_mii_config_default;
		ugcp->usbgc_mii_read = &upf_mii_read;
		ugcp->usbgc_mii_write = &upf_mii_write;

		/* mtu */
		ugcp->usbgc_min_mtu = ETHERMTU;
		ugcp->usbgc_max_mtu = ETHERMTU;
		ugcp->usbgc_default_mtu = ETHERMTU;

		lp = kmem_zalloc(sizeof (struct upf_dev), KM_SLEEP);

		lp->vid = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
		    DDI_PROP_DONTPASS, "usb-vendor-id", -1);
		lp->pid = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
		    DDI_PROP_DONTPASS, "usb-product-id", -1);

		dp = usbgem_do_attach(dip, ugcp, lp, sizeof (struct upf_dev));

		kmem_free(ugcp, sizeof (*ugcp));

		if (dp != NULL) {
			return (DDI_SUCCESS);
		}

		kmem_free(lp, sizeof (struct upf_dev));
		return (DDI_FAILURE);
	}
	if (cmd == DDI_RESUME) {
		dp = USBGEM_GET_DEV(dip);
		lp = dp->private;
		lp->phy_init_done = B_FALSE;

		return (usbgem_resume(dip));
	}
	return (DDI_FAILURE);
}

static int
upfdetach(dev_info_t *dip, ddi_detach_cmd_t cmd)
{
	int	ret;

	if (cmd == DDI_DETACH) {
		ret = usbgem_do_detach(dip);
		if (ret != DDI_SUCCESS) {
			return (DDI_FAILURE);
		}
		return (DDI_SUCCESS);
	}
	if (cmd == DDI_SUSPEND) {
		return (usbgem_suspend(dip));
	}
	return (DDI_FAILURE);
}

/* ======================================================== */
/*
 * OS depend (loadable streams driver) routine
 */
/* ======================================================== */

USBGEM_STREAM_OPS(upf_ops, upfattach, upfdetach);

static struct modldrv modldrv = {
	&mod_driverops,	/* Type of module.  This one is a driver */
	ident,
	&upf_ops,	/* driver ops */
};

static struct modlinkage modlinkage = {
	MODREV_1, &modldrv, NULL
};

/* ======================================================== */
/*
 * _init : done
 */
/* ======================================================== */
int
_init(void)
{
	int	status;

	DPRINTF(2, (CE_CONT, "!upf: _init: called"));

	status = usbgem_mod_init(&upf_ops, "upf");
	if (status != DDI_SUCCESS) {
		return (status);
	}
	status = mod_install(&modlinkage);
	if (status != DDI_SUCCESS) {
		usbgem_mod_fini(&upf_ops);
	}
	return (status);
}

/*
 * _fini : done
 */
int
_fini(void)
{
	int	status;

	DPRINTF(2, (CE_CONT, "!upf: _fini: called"));
	status = mod_remove(&modlinkage);
	if (status == DDI_SUCCESS) {
		usbgem_mod_fini(&upf_ops);
	}
	return (status);
}

int
_info(struct modinfo *modinfop)
{
	return (mod_info(&modlinkage, modinfop));
}