summaryrefslogtreecommitdiffstats
path: root/drivers/message/fusion/mptlan.c
blob: 603ffd008c73fb65275e1001af623f02332163d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
/*
 *  linux/drivers/message/fusion/mptlan.c
 *      IP Over Fibre Channel device driver.
 *      For use with LSI Fibre Channel PCI chip/adapters
 *      running LSI Fusion MPT (Message Passing Technology) firmware.
 *
 *  Copyright (c) 2000-2008 LSI Corporation
 *  (mailto:DL-MPTFusionLinux@lsi.com)
 *
 */
/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; version 2 of the License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    NO WARRANTY
    THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
    CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
    LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
    MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
    solely responsible for determining the appropriateness of using and
    distributing the Program and assumes all risks associated with its
    exercise of rights under this Agreement, including but not limited to
    the risks and costs of program errors, damage to or loss of data,
    programs or equipment, and unavailability or interruption of operations.

    DISCLAIMER OF LIABILITY
    NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), 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 OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
    HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*
 * Define statements used for debugging
 */
//#define MPT_LAN_IO_DEBUG

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

#include "mptlan.h"
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>

#define my_VERSION	MPT_LINUX_VERSION_COMMON
#define MYNAM		"mptlan"

MODULE_LICENSE("GPL");
MODULE_VERSION(my_VERSION);

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*
 * MPT LAN message sizes without variable part.
 */
#define MPT_LAN_RECEIVE_POST_REQUEST_SIZE \
	(sizeof(LANReceivePostRequest_t) - sizeof(SGE_MPI_UNION))

#define MPT_LAN_TRANSACTION32_SIZE \
	(sizeof(SGETransaction32_t) - sizeof(u32))

/*
 *  Fusion MPT LAN private structures
 */

struct BufferControl {
	struct sk_buff	*skb;
	dma_addr_t	dma;
	unsigned int	len;
};

struct mpt_lan_priv {
	MPT_ADAPTER *mpt_dev;
	u8 pnum; /* Port number in the IOC. This is not a Unix network port! */

	atomic_t buckets_out;		/* number of unused buckets on IOC */
	int bucketthresh;		/* Send more when this many left */

	int *mpt_txfidx; /* Free Tx Context list */
	int mpt_txfidx_tail;
	spinlock_t txfidx_lock;

	int *mpt_rxfidx; /* Free Rx Context list */
	int mpt_rxfidx_tail;
	spinlock_t rxfidx_lock;

	struct BufferControl *RcvCtl;	/* Receive BufferControl structs */
	struct BufferControl *SendCtl;	/* Send BufferControl structs */

	int max_buckets_out;		/* Max buckets to send to IOC */
	int tx_max_out;			/* IOC's Tx queue len */

	u32 total_posted;
	u32 total_received;
	struct net_device_stats stats;	/* Per device statistics */

	struct delayed_work post_buckets_task;
	struct net_device *dev;
	unsigned long post_buckets_active;
};

struct mpt_lan_ohdr {
	u16	dtype;
	u8	daddr[FC_ALEN];
	u16	stype;
	u8	saddr[FC_ALEN];
};

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

/*
 *  Forward protos...
 */
static int  lan_reply (MPT_ADAPTER *ioc, MPT_FRAME_HDR *mf,
		       MPT_FRAME_HDR *reply);
static int  mpt_lan_open(struct net_device *dev);
static int  mpt_lan_reset(struct net_device *dev);
static int  mpt_lan_close(struct net_device *dev);
static void mpt_lan_post_receive_buckets(struct mpt_lan_priv *priv);
static void mpt_lan_wake_post_buckets_task(struct net_device *dev,
					   int priority);
static int  mpt_lan_receive_post_turbo(struct net_device *dev, u32 tmsg);
static int  mpt_lan_receive_post_reply(struct net_device *dev,
				       LANReceivePostReply_t *pRecvRep);
static int  mpt_lan_send_turbo(struct net_device *dev, u32 tmsg);
static int  mpt_lan_send_reply(struct net_device *dev,
			       LANSendReply_t *pSendRep);
static int  mpt_lan_ioc_reset(MPT_ADAPTER *ioc, int reset_phase);
static int  mpt_lan_event_process(MPT_ADAPTER *ioc, EventNotificationReply_t *pEvReply);
static unsigned short mpt_lan_type_trans(struct sk_buff *skb,
					 struct net_device *dev);

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*
 *  Fusion MPT LAN private data
 */
static u8 LanCtx = MPT_MAX_PROTOCOL_DRIVERS;

static u32 max_buckets_out = 127;
static u32 tx_max_out_p = 127 - 16;

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/**
 *	lan_reply - Handle all data sent from the hardware.
 *	@ioc: Pointer to MPT_ADAPTER structure
 *	@mf: Pointer to original MPT request frame (NULL if TurboReply)
 *	@reply: Pointer to MPT reply frame
 *
 *	Returns 1 indicating original alloc'd request frame ptr
 *	should be freed, or 0 if it shouldn't.
 */
static int
lan_reply (MPT_ADAPTER *ioc, MPT_FRAME_HDR *mf, MPT_FRAME_HDR *reply)
{
	struct net_device *dev = ioc->netdev;
	int FreeReqFrame = 0;

	dioprintk((KERN_INFO MYNAM ": %s/%s: Got reply.\n",
		  IOC_AND_NETDEV_NAMES_s_s(dev)));

//	dioprintk((KERN_INFO MYNAM "@lan_reply: mf = %p, reply = %p\n",
//			mf, reply));

	if (mf == NULL) {
		u32 tmsg = CAST_PTR_TO_U32(reply);

		dioprintk((KERN_INFO MYNAM ": %s/%s: @lan_reply, tmsg %08x\n",
				IOC_AND_NETDEV_NAMES_s_s(dev),
				tmsg));

		switch (GET_LAN_FORM(tmsg)) {

		// NOTE!  (Optimization) First case here is now caught in
		//  mptbase.c::mpt_interrupt() routine and callcack here
		//  is now skipped for this case!
#if 0
		case LAN_REPLY_FORM_MESSAGE_CONTEXT:
//			dioprintk((KERN_INFO MYNAM "/lan_reply: "
//				  "MessageContext turbo reply received\n"));
			FreeReqFrame = 1;
			break;
#endif

		case LAN_REPLY_FORM_SEND_SINGLE:
//			dioprintk((MYNAM "/lan_reply: "
//				  "calling mpt_lan_send_reply (turbo)\n"));

			// Potential BUG here?
			//	FreeReqFrame = mpt_lan_send_turbo(dev, tmsg);
			//  If/when mpt_lan_send_turbo would return 1 here,
			//  calling routine (mptbase.c|mpt_interrupt)
			//  would Oops because mf has already been set
			//  to NULL.  So after return from this func,
			//  mpt_interrupt() will attempt to put (NULL) mf ptr
			//  item back onto its adapter FreeQ - Oops!:-(
			//  It's Ok, since mpt_lan_send_turbo() *currently*
			//  always returns 0, but..., just in case:

			(void) mpt_lan_send_turbo(dev, tmsg);
			FreeReqFrame = 0;

			break;

		case LAN_REPLY_FORM_RECEIVE_SINGLE:
//			dioprintk((KERN_INFO MYNAM "@lan_reply: "
//				  "rcv-Turbo = %08x\n", tmsg));
			mpt_lan_receive_post_turbo(dev, tmsg);
			break;

		default:
			printk (KERN_ERR MYNAM "/lan_reply: Got a turbo reply "
				"that I don't know what to do with\n");

			/* CHECKME!  Hmmm...  FreeReqFrame is 0 here; is that right? */

			break;
		}

		return FreeReqFrame;
	}

//	msg = (u32 *) reply;
//	dioprintk((KERN_INFO MYNAM "@lan_reply: msg = %08x %08x %08x %08x\n",
//		  le32_to_cpu(msg[0]), le32_to_cpu(msg[1]),
//		  le32_to_cpu(msg[2]), le32_to_cpu(msg[3])));
//	dioprintk((KERN_INFO MYNAM "@lan_reply: Function = %02xh\n",
//		  reply->u.hdr.Function));

	switch (reply->u.hdr.Function) {

	case MPI_FUNCTION_LAN_SEND:
	{
		LANSendReply_t *pSendRep;

		pSendRep = (LANSendReply_t *) reply;
		FreeReqFrame = mpt_lan_send_reply(dev, pSendRep);
		break;
	}

	case MPI_FUNCTION_LAN_RECEIVE:
	{
		LANReceivePostReply_t *pRecvRep;

		pRecvRep = (LANReceivePostReply_t *) reply;
		if (pRecvRep->NumberOfContexts) {
			mpt_lan_receive_post_reply(dev, pRecvRep);
			if (!(pRecvRep->MsgFlags & MPI_MSGFLAGS_CONTINUATION_REPLY))
				FreeReqFrame = 1;
		} else
			dioprintk((KERN_INFO MYNAM "@lan_reply: zero context "
				  "ReceivePostReply received.\n"));
		break;
	}

	case MPI_FUNCTION_LAN_RESET:
		/* Just a default reply. Might want to check it to
		 * make sure that everything went ok.
		 */
		FreeReqFrame = 1;
		break;

	case MPI_FUNCTION_EVENT_NOTIFICATION:
	case MPI_FUNCTION_EVENT_ACK:
		/*  _EVENT_NOTIFICATION should NOT come down this path any more.
		 *  Should be routed to mpt_lan_event_process(), but just in case...
		 */
		FreeReqFrame = 1;
		break;

	default:
		printk (KERN_ERR MYNAM "/lan_reply: Got a non-turbo "
			"reply that I don't know what to do with\n");

		/* CHECKME!  Hmmm...  FreeReqFrame is 0 here; is that right? */
		FreeReqFrame = 1;

		break;
	}

	return FreeReqFrame;
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
static int
mpt_lan_ioc_reset(MPT_ADAPTER *ioc, int reset_phase)
{
	struct net_device *dev = ioc->netdev;
	struct mpt_lan_priv *priv;

	if (dev == NULL)
		return(1);
	else
		priv = netdev_priv(dev);

	dlprintk((KERN_INFO MYNAM ": IOC %s_reset routed to LAN driver!\n",
			reset_phase==MPT_IOC_SETUP_RESET ? "setup" : (
			reset_phase==MPT_IOC_PRE_RESET ? "pre" : "post")));

	if (priv->mpt_rxfidx == NULL)
		return (1);

	if (reset_phase == MPT_IOC_SETUP_RESET) {
		;
	} else if (reset_phase == MPT_IOC_PRE_RESET) {
		int i;
		unsigned long flags;

		netif_stop_queue(dev);

		dlprintk ((KERN_INFO "mptlan/ioc_reset: called netif_stop_queue for %s.\n", dev->name));

		atomic_set(&priv->buckets_out, 0);

		/* Reset Rx Free Tail index and re-populate the queue. */
		spin_lock_irqsave(&priv->rxfidx_lock, flags);
		priv->mpt_rxfidx_tail = -1;
		for (i = 0; i < priv->max_buckets_out; i++)
			priv->mpt_rxfidx[++priv->mpt_rxfidx_tail] = i;
		spin_unlock_irqrestore(&priv->rxfidx_lock, flags);
	} else {
		mpt_lan_post_receive_buckets(priv);
		netif_wake_queue(dev);
	}

	return 1;
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
static int
mpt_lan_event_process(MPT_ADAPTER *ioc, EventNotificationReply_t *pEvReply)
{
	dlprintk((KERN_INFO MYNAM ": MPT event routed to LAN driver!\n"));

	switch (le32_to_cpu(pEvReply->Event)) {
	case MPI_EVENT_NONE:				/* 00 */
	case MPI_EVENT_LOG_DATA:			/* 01 */
	case MPI_EVENT_STATE_CHANGE:			/* 02 */
	case MPI_EVENT_UNIT_ATTENTION:			/* 03 */
	case MPI_EVENT_IOC_BUS_RESET:			/* 04 */
	case MPI_EVENT_EXT_BUS_RESET:			/* 05 */
	case MPI_EVENT_RESCAN:				/* 06 */
		/* Ok, do we need to do anything here? As far as
		   I can tell, this is when a new device gets added
		   to the loop. */
	case MPI_EVENT_LINK_STATUS_CHANGE:		/* 07 */
	case MPI_EVENT_LOOP_STATE_CHANGE:		/* 08 */
	case MPI_EVENT_LOGOUT:				/* 09 */
	case MPI_EVENT_EVENT_CHANGE:			/* 0A */
	default:
		break;
	}

	/*
	 *  NOTE: pEvent->AckRequired handling now done in mptbase.c;
	 *  Do NOT do it here now!
	 */

	return 1;
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
static int
mpt_lan_open(struct net_device *dev)
{
	struct mpt_lan_priv *priv = netdev_priv(dev);
	int i;

	if (mpt_lan_reset(dev) != 0) {
		MPT_ADAPTER *mpt_dev = priv->mpt_dev;

		printk (KERN_WARNING MYNAM "/lan_open: lan_reset failed.");

		if (mpt_dev->active)
			printk ("The ioc is active. Perhaps it needs to be"
				" reset?\n");
		else
			printk ("The ioc in inactive, most likely in the "
				"process of being reset. Please try again in "
				"a moment.\n");
	}

	priv->mpt_txfidx = kmalloc(priv->tx_max_out * sizeof(int), GFP_KERNEL);
	if (priv->mpt_txfidx == NULL)
		goto out;
	priv->mpt_txfidx_tail = -1;

	priv->SendCtl = kcalloc(priv->tx_max_out, sizeof(struct BufferControl),
				GFP_KERNEL);
	if (priv->SendCtl == NULL)
		goto out_mpt_txfidx;
	for (i = 0; i < priv->tx_max_out; i++)
		priv->mpt_txfidx[++priv->mpt_txfidx_tail] = i;

	dlprintk((KERN_INFO MYNAM "@lo: Finished initializing SendCtl\n"));

	priv->mpt_rxfidx = kmalloc(priv->max_buckets_out * sizeof(int),
				   GFP_KERNEL);
	if (priv->mpt_rxfidx == NULL)
		goto out_SendCtl;
	priv->mpt_rxfidx_tail = -1;

	priv->RcvCtl = kcalloc(priv->max_buckets_out,
			       sizeof(struct BufferControl),
			       GFP_KERNEL);
	if (priv->RcvCtl == NULL)
		goto out_mpt_rxfidx;
	for (i = 0; i < priv->max_buckets_out; i++)
		priv->mpt_rxfidx[++priv->mpt_rxfidx_tail] = i;

/**/	dlprintk((KERN_INFO MYNAM "/lo: txfidx contains - "));
/**/	for (i = 0; i < priv->tx_max_out; i++)
/**/		dlprintk((" %xh", priv->mpt_txfidx[i]));
/**/	dlprintk(("\n"));

	dlprintk((KERN_INFO MYNAM "/lo: Finished initializing RcvCtl\n"));

	mpt_lan_post_receive_buckets(priv);
	printk(KERN_INFO MYNAM ": %s/%s: interface up & active\n",
			IOC_AND_NETDEV_NAMES_s_s(dev));

	if (mpt_event_register(LanCtx, mpt_lan_event_process) != 0) {
		printk (KERN_WARNING MYNAM "/lo: Unable to register for Event"
			" Notifications. This is a bad thing! We're not going "
			"to go ahead, but I'd be leery of system stability at "
			"this point.\n");
	}

	netif_start_queue(dev);
	dlprintk((KERN_INFO MYNAM "/lo: Done.\n"));

	return 0;
out_mpt_rxfidx:
	kfree(priv->mpt_rxfidx);
	priv->mpt_rxfidx = NULL;
out_SendCtl:
	kfree(priv->SendCtl);
	priv->SendCtl = NULL;
out_mpt_txfidx:
	kfree(priv->mpt_txfidx);
	priv->mpt_txfidx = NULL;
out:	return -ENOMEM;
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/* Send a LanReset message to the FW. This should result in the FW returning
   any buckets it still has. */
static int
mpt_lan_reset(struct net_device *dev)
{
	MPT_FRAME_HDR *mf;
	LANResetRequest_t *pResetReq;
	struct mpt_lan_priv *priv = netdev_priv(dev);

	mf = mpt_get_msg_frame(LanCtx, priv->mpt_dev);

	if (mf == NULL) {
/*		dlprintk((KERN_ERR MYNAM "/reset: Evil funkiness abounds! "
		"Unable to allocate a request frame.\n"));
*/
		return -1;
	}

	pResetReq = (LANResetRequest_t *) mf;

	pResetReq->Function	= MPI_FUNCTION_LAN_RESET;
	pResetReq->ChainOffset	= 0;
	pResetReq->Reserved	= 0;
	pResetReq->PortNumber	= priv->pnum;
	pResetReq->MsgFlags	= 0;
	pResetReq->Reserved2	= 0;

	mpt_put_msg_frame(LanCtx, priv->mpt_dev, mf);

	return 0;
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
static int
mpt_lan_close(struct net_device *dev)
{
	struct mpt_lan_priv *priv = netdev_priv(dev);
	MPT_ADAPTER *mpt_dev = priv->mpt_dev;
	unsigned long timeout;
	int i;

	dlprintk((KERN_INFO MYNAM ": mpt_lan_close called\n"));

	mpt_event_deregister(LanCtx);

	dlprintk((KERN_INFO MYNAM ":lan_close: Posted %d buckets "
		  "since driver was loaded, %d still out\n",
		  priv->total_posted,atomic_read(&priv->buckets_out)));

	netif_stop_queue(dev);

	mpt_lan_reset(dev);

	timeout = jiffies + 2 * HZ;
	while (atomic_read(&priv->buckets_out) && time_before(jiffies, timeout))
		schedule_timeout_interruptible(1);

	for (i = 0; i < priv->max_buckets_out; i++) {
		if (priv->RcvCtl[i].skb != NULL) {
/**/			dlprintk((KERN_INFO MYNAM "/lan_close: bucket %05x "
/**/				  "is still out\n", i));
			pci_unmap_single(mpt_dev->pcidev, priv->RcvCtl[i].dma,
					 priv->RcvCtl[i].len,
					 PCI_DMA_FROMDEVICE);
			dev_kfree_skb(priv->RcvCtl[i].skb);
		}
	}

	kfree(priv->RcvCtl);
	kfree(priv->mpt_rxfidx);

	for (i = 0; i < priv->tx_max_out; i++) {
		if (priv->SendCtl[i].skb != NULL) {
			pci_unmap_single(mpt_dev->pcidev, priv->SendCtl[i].dma,
					 priv->SendCtl[i].len,
					 PCI_DMA_TODEVICE);
			dev_kfree_skb(priv->SendCtl[i].skb);
		}
	}

	kfree(priv->SendCtl);
	kfree(priv->mpt_txfidx);

	atomic_set(&priv->buckets_out, 0);

	printk(KERN_INFO MYNAM ": %s/%s: interface down & inactive\n",
			IOC_AND_NETDEV_NAMES_s_s(dev));

	return 0;
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
static struct net_device_stats *
mpt_lan_get_stats(struct net_device *dev)
{
	struct mpt_lan_priv *priv = netdev_priv(dev);

	return (struct net_device_stats *) &priv->stats;
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
static int
mpt_lan_change_mtu(struct net_device *dev, int new_mtu)
{
	if ((new_mtu < MPT_LAN_MIN_MTU) || (new_mtu > MPT_LAN_MAX_MTU))
		return -EINVAL;
	dev->mtu = new_mtu;
	return 0;
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/* Tx timeout handler. */
static void
mpt_lan_tx_timeout(struct net_device *dev)
{
	struct mpt_lan_priv *priv = netdev_priv(dev);
	MPT_ADAPTER *mpt_dev = priv->mpt_dev;

	if (mpt_dev->active) {
		dlprintk (("mptlan/tx_timeout: calling netif_wake_queue for %s.\n", dev->name));
		netif_wake_queue(dev);
	}
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
//static inline int
static int
mpt_lan_send_turbo(struct net_device *dev, u32 tmsg)
{
	struct mpt_lan_priv *priv = netdev_priv(dev);
	MPT_ADAPTER *mpt_dev = priv->mpt_dev;
	struct sk_buff *sent;
	unsigned long flags;
	u32 ctx;

	ctx = GET_LAN_BUFFER_CONTEXT(tmsg);
	sent = priv->SendCtl[ctx].skb;

	priv->stats.tx_packets++;
	priv->stats.tx_bytes += sent->len;

	dioprintk((KERN_INFO MYNAM ": %s/%s: @%s, skb %p sent.\n",
			IOC_AND_NETDEV_NAMES_s_s(dev),
			__func__, sent));

	priv->SendCtl[ctx].skb = NULL;
	pci_unmap_single(mpt_dev->pcidev, priv->SendCtl[ctx].dma,
			 priv->SendCtl[ctx].len, PCI_DMA_TODEVICE);
	dev_kfree_skb_irq(sent);

	spin_lock_irqsave(&priv->txfidx_lock, flags);
	priv->mpt_txfidx[++priv->mpt_txfidx_tail] = ctx;
	spin_unlock_irqrestore(&priv->txfidx_lock, flags);

	netif_wake_queue(dev);
	return 0;
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
static int
mpt_lan_send_reply(struct net_device *dev, LANSendReply_t *pSendRep)
{
	struct mpt_lan_priv *priv = netdev_priv(dev);
	MPT_ADAPTER *mpt_dev = priv->mpt_dev;
	struct sk_buff *sent;
	unsigned long flags;
	int FreeReqFrame = 0;
	u32 *pContext;
	u32 ctx;
	u8 count;

	count = pSendRep->NumberOfContexts;

	dioprintk((KERN_INFO MYNAM ": send_reply: IOCStatus: %04x\n",
		 le16_to_cpu(pSendRep->IOCStatus)));

	/* Add check for Loginfo Flag in IOCStatus */

	switch (le16_to_cpu(pSendRep->IOCStatus) & MPI_IOCSTATUS_MASK) {
	case MPI_IOCSTATUS_SUCCESS:
		priv->stats.tx_packets += count;
		break;

	case MPI_IOCSTATUS_LAN_CANCELED:
	case MPI_IOCSTATUS_LAN_TRANSMIT_ABORTED:
		break;

	case MPI_IOCSTATUS_INVALID_SGL:
		priv->stats.tx_errors += count;
		printk (KERN_ERR MYNAM ": %s/%s: ERROR - Invalid SGL sent to IOC!\n",
				IOC_AND_NETDEV_NAMES_s_s(dev));
		goto out;

	default:
		priv->stats.tx_errors += count;
		break;
	}

	pContext = &pSendRep->BufferContext;

	spin_lock_irqsave(&priv->txfidx_lock, flags);
	while (count > 0) {
		ctx = GET_LAN_BUFFER_CONTEXT(le32_to_cpu(*pContext));

		sent = priv->SendCtl[ctx].skb;
		priv->stats.tx_bytes += sent->len;

		dioprintk((KERN_INFO MYNAM ": %s/%s: @%s, skb %p sent.\n",
				IOC_AND_NETDEV_NAMES_s_s(dev),
				__func__, sent));

		priv->SendCtl[ctx].skb = NULL;
		pci_unmap_single(mpt_dev->pcidev, priv->SendCtl[ctx].dma,
				 priv->SendCtl[ctx].len, PCI_DMA_TODEVICE);
		dev_kfree_skb_irq(sent);

		priv->mpt_txfidx[++priv->mpt_txfidx_tail] = ctx;

		pContext++;
		count--;
	}
	spin_unlock_irqrestore(&priv->txfidx_lock, flags);

out:
	if (!(pSendRep->MsgFlags & MPI_MSGFLAGS_CONTINUATION_REPLY))
		FreeReqFrame = 1;

	netif_wake_queue(dev);
	return FreeReqFrame;
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
static int
mpt_lan_sdu_send (struct sk_buff *skb, struct net_device *dev)
{
	struct mpt_lan_priv *priv = netdev_priv(dev);
	MPT_ADAPTER *mpt_dev = priv->mpt_dev;
	MPT_FRAME_HDR *mf;
	LANSendRequest_t *pSendReq;
	SGETransaction32_t *pTrans;
	SGESimple64_t *pSimple;
	const unsigned char *mac;
	dma_addr_t dma;
	unsigned long flags;
	int ctx;
	u16 cur_naa = 0x1000;

	dioprintk((KERN_INFO MYNAM ": %s called, skb_addr = %p\n",
			__func__, skb));

	spin_lock_irqsave(&priv->txfidx_lock, flags);
	if (priv->mpt_txfidx_tail < 0) {
		netif_stop_queue(dev);
		spin_unlock_irqrestore(&priv->txfidx_lock, flags);

		printk (KERN_ERR "%s: no tx context available: %u\n",
			__func__, priv->mpt_txfidx_tail);
		return 1;
	}

	mf = mpt_get_msg_frame(LanCtx, mpt_dev);
	if (mf == NULL) {
		netif_stop_queue(dev);
		spin_unlock_irqrestore(&priv->txfidx_lock, flags);

		printk (KERN_ERR "%s: Unable to alloc request frame\n",
			__func__);
		return 1;
	}

	ctx = priv->mpt_txfidx[priv->mpt_txfidx_tail--];
	spin_unlock_irqrestore(&priv->txfidx_lock, flags);

//	dioprintk((KERN_INFO MYNAM ": %s/%s: Creating new msg frame (send).\n",
//			IOC_AND_NETDEV_NAMES_s_s(dev)));

	pSendReq = (LANSendRequest_t *) mf;

	/* Set the mac.raw pointer, since this apparently isn't getting
	 * done before we get the skb. Pull the data pointer past the mac data.
	 */
	skb_reset_mac_header(skb);
	skb_pull(skb, 12);

        dma = pci_map_single(mpt_dev->pcidev, skb->data, skb->len,
			     PCI_DMA_TODEVICE);

	priv->SendCtl[ctx].skb = skb;
	priv->SendCtl[ctx].dma = dma;
	priv->SendCtl[ctx].len = skb->len;

	/* Message Header */
	pSendReq->Reserved    = 0;
	pSendReq->Function    = MPI_FUNCTION_LAN_SEND;
	pSendReq->ChainOffset = 0;
	pSendReq->Reserved2   = 0;
	pSendReq->MsgFlags    = 0;
	pSendReq->PortNumber  = priv->pnum;

	/* Transaction Context Element */
	pTrans = (SGETransaction32_t *) pSendReq->SG_List;

	/* No Flags, 8 bytes of Details, 32bit Context (bloody turbo replies) */
	pTrans->ContextSize   = sizeof(u32);
	pTrans->DetailsLength = 2 * sizeof(u32);
	pTrans->Flags         = 0;
	pTrans->TransactionContext[0] = cpu_to_le32(ctx);

//	dioprintk((KERN_INFO MYNAM ": %s/%s: BC = %08x, skb = %p, buff = %p\n",
//			IOC_AND_NETDEV_NAMES_s_s(dev),
//			ctx, skb, skb->data));

	mac = skb_mac_header(skb);

	pTrans->TransactionDetails[0] = cpu_to_le32((cur_naa         << 16) |
						    (mac[0] <<  8) |
						    (mac[1] <<  0));
	pTrans->TransactionDetails[1] = cpu_to_le32((mac[2] << 24) |
						    (mac[3] << 16) |
						    (mac[4] <<  8) |
						    (mac[5] <<  0));

	pSimple = (SGESimple64_t *) &pTrans->TransactionDetails[2];

	/* If we ever decide to send more than one Simple SGE per LANSend, then
	   we will need to make sure that LAST_ELEMENT only gets set on the
	   last one. Otherwise, bad voodoo and evil funkiness will commence. */
	pSimple->FlagsLength = cpu_to_le32(
			((MPI_SGE_FLAGS_LAST_ELEMENT |
			  MPI_SGE_FLAGS_END_OF_BUFFER |
			  MPI_SGE_FLAGS_SIMPLE_ELEMENT |
			  MPI_SGE_FLAGS_SYSTEM_ADDRESS |
			  MPI_SGE_FLAGS_HOST_TO_IOC |
			  MPI_SGE_FLAGS_64_BIT_ADDRESSING |
			  MPI_SGE_FLAGS_END_OF_LIST) << MPI_SGE_FLAGS_SHIFT) |
			skb->len);
	pSimple->Address.Low = cpu_to_le32((u32) dma);
	if (sizeof(dma_addr_t) > sizeof(u32))
		pSimple->Address.High = cpu_to_le32((u32) ((u64) dma >> 32));
	else
		pSimple->Address.High = 0;

	mpt_put_msg_frame (LanCtx, mpt_dev, mf);
	dev->trans_start = jiffies;

	dioprintk((KERN_INFO MYNAM ": %s/%s: Sending packet. FlagsLength = %08x.\n",
			IOC_AND_NETDEV_NAMES_s_s(dev),
			le32_to_cpu(pSimple->FlagsLength)));

	return 0;
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
static void
mpt_lan_wake_post_buckets_task(struct net_device *dev, int priority)
/*
 * @priority: 0 = put it on the timer queue, 1 = put it on the immediate queue
 */
{
	struct mpt_lan_priv *priv = dev->priv;
	
	if (test_and_set_bit(0, &priv->post_buckets_active) == 0) {
		if (priority) {
			schedule_delayed_work(&priv->post_buckets_task, 0);
		} else {
			schedule_delayed_work(&priv->post_buckets_task, 1);
			dioprintk((KERN_INFO MYNAM ": post_buckets queued on "
				   "timer.\n"));
		}
	        dioprintk((KERN_INFO MYNAM ": %s/%s: Queued post_buckets task.\n",
			   IOC_AND_NETDEV_NAMES_s_s(dev) ));
	}
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
static int
mpt_lan_receive_skb(struct net_device *dev, struct sk_buff *skb)
{
	struct mpt_lan_priv *priv = dev->priv;

	skb->protocol = mpt_lan_type_trans(skb, dev);

	dioprintk((KERN_INFO MYNAM ": %s/%s: Incoming packet (%d bytes) "
		 "delivered to upper level.\n",
			IOC_AND_NETDEV_NAMES_s_s(dev), skb->len));

	priv->stats.rx_bytes += skb->len;
	priv->stats.rx_packets++;

	skb->dev = dev;
	netif_rx(skb);

	dioprintk((MYNAM "/receive_skb: %d buckets remaining\n",
		 atomic_read(&priv->buckets_out)));

	if (atomic_read(&priv->buckets_out) < priv->bucketthresh)
		mpt_lan_wake_post_buckets_task(dev, 1);

	dioprintk((KERN_INFO MYNAM "/receive_post_reply: %d buckets "
		  "remaining, %d received back since sod\n",
		  atomic_read(&priv->buckets_out), priv->total_received));

	return 0;
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
//static inline int
static int
mpt_lan_receive_post_turbo(struct net_device *dev, u32 tmsg)
{
	struct mpt_lan_priv *priv = dev->priv;
	MPT_ADAPTER *mpt_dev = priv->mpt_dev;
	struct sk_buff *skb, *old_skb;
	unsigned long flags;
	u32 ctx, len;

	ctx = GET_LAN_BUCKET_CONTEXT(tmsg);
	skb = priv->RcvCtl[ctx].skb;

	len = GET_LAN_PACKET_LENGTH(tmsg);

	if (len < MPT_LAN_RX_COPYBREAK) {
		old_skb = skb;

		skb = (struct sk_buff *)dev_alloc_skb(len);
		if (!skb) {
			printk (KERN_ERR MYNAM ": %s/%s: ERROR - Can't allocate skb! (%s@%d)\n",
					IOC_AND_NETDEV_NAMES_s_s(dev),
					__FILE__, __LINE__);
			return -ENOMEM;
		}

		pci_dma_sync_single_for_cpu(mpt_dev->pcidev, priv->RcvCtl[ctx].dma,
					    priv->RcvCtl[ctx].len, PCI_DMA_FROMDEVICE);

		skb_copy_from_linear_data(old_skb, skb_put(skb, len), len);

		pci_dma_sync_single_for_device(mpt_dev->pcidev, priv->RcvCtl[ctx].dma,
					       priv->RcvCtl[ctx].len, PCI_DMA_FROMDEVICE);
		goto out;
	}

	skb_put(skb, len);

	priv->RcvCtl[ctx].skb = NULL;

	pci_unmap_single(mpt_dev->pcidev, priv->RcvCtl[ctx].dma,
			 priv->RcvCtl[ctx].len, PCI_DMA_FROMDEVICE);

out:
	spin_lock_irqsave(&priv->rxfidx_lock, flags);
	priv->mpt_rxfidx[++priv->mpt_rxfidx_tail] = ctx;
	spin_unlock_irqrestore(&priv->rxfidx_lock, flags);

	atomic_dec(&priv->buckets_out);
	priv->total_received++;

	return mpt_lan_receive_skb(dev, skb);
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
static int
mpt_lan_receive_post_free(struct net_device *dev,
			  LANReceivePostReply_t *pRecvRep)
{
	struct mpt_lan_priv *priv = dev->priv;
	MPT_ADAPTER *mpt_dev = priv->mpt_dev;
	unsigned long flags;
	struct sk_buff *skb;
	u32 ctx;
	int count;
	int i;

	count = pRecvRep->NumberOfContexts;

/**/	dlprintk((KERN_INFO MYNAM "/receive_post_reply: "
		  "IOC returned %d buckets, freeing them...\n", count));

	spin_lock_irqsave(&priv->rxfidx_lock, flags);
	for (i = 0; i < count; i++) {
		ctx = le32_to_cpu(pRecvRep->BucketContext[i]);

		skb = priv->RcvCtl[ctx].skb;

//		dlprintk((KERN_INFO MYNAM ": %s: dev_name = %s\n",
//				IOC_AND_NETDEV_NAMES_s_s(dev)));
//		dlprintk((KERN_INFO MYNAM "@rpr[2], priv = %p, buckets_out addr = %p",
//				priv, &(priv->buckets_out)));
//		dlprintk((KERN_INFO MYNAM "@rpr[2] TC + 3\n"));

		priv->RcvCtl[ctx].skb = NULL;
		pci_unmap_single(mpt_dev->pcidev, priv->RcvCtl[ctx].dma,
				 priv->RcvCtl[ctx].len, PCI_DMA_FROMDEVICE);
		dev_kfree_skb_any(skb);

		priv->mpt_rxfidx[++priv->mpt_rxfidx_tail] = ctx;
	}
	spin_unlock_irqrestore(&priv->rxfidx_lock, flags);

	atomic_sub(count, &priv->buckets_out);

//	for (i = 0; i < priv->max_buckets_out; i++)
//		if (priv->RcvCtl[i].skb != NULL)
//			dlprintk((KERN_INFO MYNAM "@rpr: bucket %03x "
//				  "is still out\n", i));

/*	dlprintk((KERN_INFO MYNAM "/receive_post_reply: freed %d buckets\n",
		  count));
*/
/**/	dlprintk((KERN_INFO MYNAM "@receive_post_reply: %d buckets "
/**/		  "remaining, %d received back since sod.\n",
/**/		  atomic_read(&priv->buckets_out), priv->total_received));
	return 0;
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
static int
mpt_lan_receive_post_reply(struct net_device *dev,
			   LANReceivePostReply_t *pRecvRep)
{
	struct mpt_lan_priv *priv = dev->priv;
	MPT_ADAPTER *mpt_dev = priv->mpt_dev;
	struct sk_buff *skb, *old_skb;
	unsigned long flags;
	u32 len, ctx, offset;
	u32 remaining = le32_to_cpu(pRecvRep->BucketsRemaining);
	int count;
	int i, l;

	dioprintk((KERN_INFO MYNAM ": mpt_lan_receive_post_reply called\n"));
	dioprintk((KERN_INFO MYNAM ": receive_post_reply: IOCStatus: %04x\n",
		 le16_to_cpu(pRecvRep->IOCStatus)));

	if ((le16_to_cpu(pRecvRep->IOCStatus) & MPI_IOCSTATUS_MASK) ==
						MPI_IOCSTATUS_LAN_CANCELED)
		return mpt_lan_receive_post_free(dev, pRecvRep);

	len = le32_to_cpu(pRecvRep->PacketLength);
	if (len == 0) {
		printk (KERN_ERR MYNAM ": %s/%s: ERROR - Got a non-TURBO "
			"ReceivePostReply w/ PacketLength zero!\n",
				IOC_AND_NETDEV_NAMES_s_s(dev));
		printk (KERN_ERR MYNAM ": MsgFlags = %02x, IOCStatus = %04x\n",
				pRecvRep->MsgFlags, le16_to_cpu(pRecvRep->IOCStatus));
		return -1;
	}

	ctx    = le32_to_cpu(pRecvRep->BucketContext[0]);
	count  = pRecvRep->NumberOfContexts;
	skb    = priv->RcvCtl[ctx].skb;

	offset = le32_to_cpu(pRecvRep->PacketOffset);
//	if (offset != 0) {
//		printk (KERN_INFO MYNAM ": %s/%s: Got a ReceivePostReply "
//			"w/ PacketOffset %u\n",
//				IOC_AND_NETDEV_NAMES_s_s(dev),
//				offset);
//	}

	dioprintk((KERN_INFO MYNAM ": %s/%s: @rpr, offset = %d, len = %d\n",
			IOC_AND_NETDEV_NAMES_s_s(dev),
			offset, len));

	if (count > 1) {
		int szrem = len;

//		dioprintk((KERN_INFO MYNAM ": %s/%s: Multiple buckets returned "
//			"for single packet, concatenating...\n",
//				IOC_AND_NETDEV_NAMES_s_s(dev)));

		skb = (struct sk_buff *)dev_alloc_skb(len);
		if (!skb) {
			printk (KERN_ERR MYNAM ": %s/%s: ERROR - Can't allocate skb! (%s@%d)\n",
					IOC_AND_NETDEV_NAMES_s_s(dev),
					__FILE__, __LINE__);
			return -ENOMEM;
		}

		spin_lock_irqsave(&priv->rxfidx_lock, flags);
		for (i = 0; i < count; i++) {

			ctx = le32_to_cpu(pRecvRep->BucketContext[i]);
			old_skb = priv->RcvCtl[ctx].skb;

			l = priv->RcvCtl[ctx].len;
			if (szrem < l)
				l = szrem;

//			dioprintk((KERN_INFO MYNAM ": %s/%s: Buckets = %d, len = %u\n",
//					IOC_AND_NETDEV_NAMES_s_s(dev),
//					i, l));

			pci_dma_sync_single_for_cpu(mpt_dev->pcidev,
						    priv->RcvCtl[ctx].dma,
						    priv->RcvCtl[ctx].len,
						    PCI_DMA_FROMDEVICE);
			skb_copy_from_linear_data(old_skb, skb_put(skb, l), l);

			pci_dma_sync_single_for_device(mpt_dev->pcidev,
						       priv->RcvCtl[ctx].dma,
						       priv->RcvCtl[ctx].len,
						       PCI_DMA_FROMDEVICE);

			priv->mpt_rxfidx[++priv->mpt_rxfidx_tail] = ctx;
			szrem -= l;
		}
		spin_unlock_irqrestore(&priv->rxfidx_lock, flags);

	} else if (len < MPT_LAN_RX_COPYBREAK) {

		old_skb = skb;

		skb = (struct sk_buff *)dev_alloc_skb(len);
		if (!skb) {
			printk (KERN_ERR MYNAM ": %s/%s: ERROR - Can't allocate skb! (%s@%d)\n",
					IOC_AND_NETDEV_NAMES_s_s(dev),
					__FILE__, __LINE__);
			return -ENOMEM;
		}

		pci_dma_sync_single_for_cpu(mpt_dev->pcidev,
					    priv->RcvCtl[ctx].dma,
					    priv->RcvCtl[ctx].len,
					    PCI_DMA_FROMDEVICE);

		skb_copy_from_linear_data(old_skb, skb_put(skb, len), len);

		pci_dma_sync_single_for_device(mpt_dev->pcidev,
					       priv->RcvCtl[ctx].dma,
					       priv->RcvCtl[ctx].len,
					       PCI_DMA_FROMDEVICE);

		spin_lock_irqsave(&priv->rxfidx_lock, flags);
		priv->mpt_rxfidx[++priv->mpt_rxfidx_tail] = ctx;
		spin_unlock_irqrestore(&priv->rxfidx_lock, flags);

	} else {
		spin_lock_irqsave(&priv->rxfidx_lock, flags);

		priv->RcvCtl[ctx].skb = NULL;

		pci_unmap_single(mpt_dev->pcidev, priv->RcvCtl[ctx].dma,
				 priv->RcvCtl[ctx].len, PCI_DMA_FROMDEVICE);
		priv->RcvCtl[ctx].dma = 0;

		priv->mpt_rxfidx[++priv->mpt_rxfidx_tail] = ctx;
		spin_unlock_irqrestore(&priv->rxfidx_lock, flags);

		skb_put(skb,len);
	}

	atomic_sub(count, &priv->buckets_out);
	priv->total_received += count;

	if (priv->mpt_rxfidx_tail >= MPT_LAN_MAX_BUCKETS_OUT) {
		printk (KERN_ERR MYNAM ": %s/%s: Yoohoo! mpt_rxfidx_tail = %d, "
			"MPT_LAN_MAX_BUCKETS_OUT = %d\n",
				IOC_AND_NETDEV_NAMES_s_s(dev),
				priv->mpt_rxfidx_tail,
				MPT_LAN_MAX_BUCKETS_OUT);

		return -1;
	}

	if (remaining == 0)
		printk (KERN_WARNING MYNAM ": %s/%s: WARNING - IOC out of buckets! "
			"(priv->buckets_out = %d)\n",
			IOC_AND_NETDEV_NAMES_s_s(dev),
			atomic_read(&priv->buckets_out));
	else if (remaining < 10)
		printk (KERN_INFO MYNAM ": %s/%s: IOC says %d buckets left. "
			"(priv->buckets_out = %d)\n",
			IOC_AND_NETDEV_NAMES_s_s(dev),
			remaining, atomic_read(&priv->buckets_out));
	
	if ((remaining < priv->bucketthresh) &&
	    ((atomic_read(&priv->buckets_out) - remaining) >
	     MPT_LAN_BUCKETS_REMAIN_MISMATCH_THRESH)) {
		
		printk (KERN_WARNING MYNAM " Mismatch between driver's "
			"buckets_out count and fw's BucketsRemaining "
			"count has crossed the threshold, issuing a "
			"LanReset to clear the fw's hashtable. You may "
			"want to check your /var/log/messages for \"CRC "
			"error\" event notifications.\n");
		
		mpt_lan_reset(dev);
		mpt_lan_wake_post_buckets_task(dev, 0);
	}
	
	return mpt_lan_receive_skb(dev, skb);
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/* Simple SGE's only at the moment */

static void
mpt_lan_post_receive_buckets(struct mpt_lan_priv *priv)
{
	struct net_device *dev = priv->dev;
	MPT_ADAPTER *mpt_dev = priv->mpt_dev;
	MPT_FRAME_HDR *mf;
	LANReceivePostRequest_t *pRecvReq;
	SGETransaction32_t *pTrans;
	SGESimple64_t *pSimple;
	struct sk_buff *skb;
	dma_addr_t dma;
	u32 curr, buckets, count, max;
	u32 len = (dev->mtu + dev->hard_header_len + 4);
	unsigned long flags;
	int i;

	curr = atomic_read(&priv->buckets_out);
	buckets = (priv->max_buckets_out - curr);

	dioprintk((KERN_INFO MYNAM ": %s/%s: @%s, Start_buckets = %u, buckets_out = %u\n",
			IOC_AND_NETDEV_NAMES_s_s(dev),
			__func__, buckets, curr));

	max = (mpt_dev->req_sz - MPT_LAN_RECEIVE_POST_REQUEST_SIZE) /
			(MPT_LAN_TRANSACTION32_SIZE + sizeof(SGESimple64_t));

	while (buckets) {
		mf = mpt_get_msg_frame(LanCtx, mpt_dev);
		if (mf == NULL) {
			printk (KERN_ERR "%s: Unable to alloc request frame\n",
				__func__);
			dioprintk((KERN_ERR "%s: %u buckets remaining\n",
				 __func__, buckets));
			goto out;
		}
		pRecvReq = (LANReceivePostRequest_t *) mf;

		i = le16_to_cpu(mf->u.frame.hwhdr.msgctxu.fld.req_idx);
		mpt_dev->RequestNB[i] = 0;
		count = buckets;
		if (count > max)
			count = max;

		pRecvReq->Function    = MPI_FUNCTION_LAN_RECEIVE;
		pRecvReq->ChainOffset = 0;
		pRecvReq->MsgFlags    = 0;
		pRecvReq->PortNumber  = priv->pnum;

		pTrans = (SGETransaction32_t *) pRecvReq->SG_List;
		pSimple = NULL;

		for (i = 0; i < count; i++) {
			int ctx;

			spin_lock_irqsave(&priv->rxfidx_lock, flags);
			if (priv->mpt_rxfidx_tail < 0) {
				printk (KERN_ERR "%s: Can't alloc context\n",
					__func__);
				spin_unlock_irqrestore(&priv->rxfidx_lock,
						       flags);
				break;
			}

			ctx = priv->mpt_rxfidx[priv->mpt_rxfidx_tail--];

			skb = priv->RcvCtl[ctx].skb;
			if (skb && (priv->RcvCtl[ctx].len != len)) {
				pci_unmap_single(mpt_dev->pcidev,
						 priv->RcvCtl[ctx].dma,
						 priv->RcvCtl[ctx].len,
						 PCI_DMA_FROMDEVICE);
				dev_kfree_skb(priv->RcvCtl[ctx].skb);
				skb = priv->RcvCtl[ctx].skb = NULL;
			}

			if (skb == NULL) {
				skb = dev_alloc_skb(len);
				if (skb == NULL) {
					printk (KERN_WARNING
						MYNAM "/%s: Can't alloc skb\n",
						__func__);
					priv->mpt_rxfidx[++priv->mpt_rxfidx_tail] = ctx;
					spin_unlock_irqrestore(&priv->rxfidx_lock, flags);
					break;
				}

				dma = pci_map_single(mpt_dev->pcidev, skb->data,
						     len, PCI_DMA_FROMDEVICE);

				priv->RcvCtl[ctx].skb = skb;
				priv->RcvCtl[ctx].dma = dma;
				priv->RcvCtl[ctx].len = len;
			}

			spin_unlock_irqrestore(&priv->rxfidx_lock, flags);

			pTrans->ContextSize   = sizeof(u32);
			pTrans->DetailsLength = 0;
			pTrans->Flags         = 0;
			pTrans->TransactionContext[0] = cpu_to_le32(ctx);

			pSimple = (SGESimple64_t *) pTrans->TransactionDetails;

			pSimple->FlagsLength = cpu_to_le32(
				((MPI_SGE_FLAGS_END_OF_BUFFER |
				  MPI_SGE_FLAGS_SIMPLE_ELEMENT |
				  MPI_SGE_FLAGS_64_BIT_ADDRESSING) << MPI_SGE_FLAGS_SHIFT) | len);
			pSimple->Address.Low = cpu_to_le32((u32) priv->RcvCtl[ctx].dma);
			if (sizeof(dma_addr_t) > sizeof(u32))
				pSimple->Address.High = cpu_to_le32((u32) ((u64) priv->RcvCtl[ctx].dma >> 32));
			else
				pSimple->Address.High = 0;

			pTrans = (SGETransaction32_t *) (pSimple + 1);
		}

		if (pSimple == NULL) {
/**/			printk (KERN_WARNING MYNAM "/%s: No buckets posted\n",
/**/				__func__);
			mpt_free_msg_frame(mpt_dev, mf);
			goto out;
		}

		pSimple->FlagsLength |= cpu_to_le32(MPI_SGE_FLAGS_END_OF_LIST << MPI_SGE_FLAGS_SHIFT);

		pRecvReq->BucketCount = cpu_to_le32(i);

/*	printk(KERN_INFO MYNAM ": posting buckets\n   ");
 *	for (i = 0; i < j + 2; i ++)
 *	    printk (" %08x", le32_to_cpu(msg[i]));
 *	printk ("\n");
 */

		mpt_put_msg_frame(LanCtx, mpt_dev, mf);

		priv->total_posted += i;
		buckets -= i;
		atomic_add(i, &priv->buckets_out);
	}

out:
	dioprintk((KERN_INFO MYNAM "/%s: End_buckets = %u, priv->buckets_out = %u\n",
		  __func__, buckets, atomic_read(&priv->buckets_out)));
	dioprintk((KERN_INFO MYNAM "/%s: Posted %u buckets and received %u back\n",
	__func__, priv->total_posted, priv->total_received));

	clear_bit(0, &priv->post_buckets_active);
}

static void
mpt_lan_post_receive_buckets_work(struct work_struct *work)
{
	mpt_lan_post_receive_buckets(container_of(work, struct mpt_lan_priv,
						  post_buckets_task.work));
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
static struct net_device *
mpt_register_lan_device (MPT_ADAPTER *mpt_dev, int pnum)
{
	struct net_device *dev;
	struct mpt_lan_priv *priv;
	u8 HWaddr[FC_ALEN], *a;

	dev = alloc_fcdev(sizeof(struct mpt_lan_priv));
	if (!dev)
		return NULL;

	dev->mtu = MPT_LAN_MTU;

	priv = netdev_priv(dev);

	priv->dev = dev;
	priv->mpt_dev = mpt_dev;
	priv->pnum = pnum;

	INIT_DELAYED_WORK(&priv->post_buckets_task,
			  mpt_lan_post_receive_buckets_work);
	priv->post_buckets_active = 0;

	dlprintk((KERN_INFO MYNAM "@%d: bucketlen = %d\n",
			__LINE__, dev->mtu + dev->hard_header_len + 4));

	atomic_set(&priv->buckets_out, 0);
	priv->total_posted = 0;
	priv->total_received = 0;
	priv->max_buckets_out = max_buckets_out;
	if (mpt_dev->pfacts[0].MaxLanBuckets < max_buckets_out)
		priv->max_buckets_out = mpt_dev->pfacts[0].MaxLanBuckets;

	dlprintk((KERN_INFO MYNAM "@%d: MaxLanBuckets=%d, max_buckets_out/priv=%d/%d\n",
			__LINE__,
			mpt_dev->pfacts[0].MaxLanBuckets,
			max_buckets_out,
			priv->max_buckets_out));

	priv->bucketthresh = priv->max_buckets_out * 2 / 3;
	spin_lock_init(&priv->txfidx_lock);
	spin_lock_init(&priv->rxfidx_lock);

	/*  Grab pre-fetched LANPage1 stuff. :-) */
	a = (u8 *) &mpt_dev->lan_cnfg_page1.HardwareAddressLow;

	HWaddr[0] = a[5];
	HWaddr[1] = a[4];
	HWaddr[2] = a[3];
	HWaddr[3] = a[2];
	HWaddr[4] = a[1];
	HWaddr[5] = a[0];

	dev->addr_len = FC_ALEN;
	memcpy(dev->dev_addr, HWaddr, FC_ALEN);
	memset(dev->broadcast, 0xff, FC_ALEN);

	/* The Tx queue is 127 deep on the 909.
	 * Give ourselves some breathing room.
	 */
	priv->tx_max_out = (tx_max_out_p <= MPT_TX_MAX_OUT_LIM) ?
			    tx_max_out_p : MPT_TX_MAX_OUT_LIM;

	dev->open = mpt_lan_open;
	dev->stop = mpt_lan_close;
	dev->get_stats = mpt_lan_get_stats;
	dev->set_multicast_list = NULL;
	dev->change_mtu = mpt_lan_change_mtu;
	dev->hard_start_xmit = mpt_lan_sdu_send;

/* Not in 2.3.42. Need 2.3.45+ */
	dev->tx_timeout = mpt_lan_tx_timeout;
	dev->watchdog_timeo = MPT_LAN_TX_TIMEOUT;

	dlprintk((KERN_INFO MYNAM ": Finished registering dev "
		"and setting initial values\n"));

	if (register_netdev(dev) != 0) {
		free_netdev(dev);
		dev = NULL;
	}
	return dev;
}

static int
mptlan_probe(struct pci_dev *pdev, const struct pci_device_id *id)
{
	MPT_ADAPTER 		*ioc = pci_get_drvdata(pdev);
	struct net_device	*dev;
	int			i;

	for (i = 0; i < ioc->facts.NumberOfPorts; i++) {
		printk(KERN_INFO MYNAM ": %s: PortNum=%x, "
		       "ProtocolFlags=%02Xh (%c%c%c%c)\n",
		       ioc->name, ioc->pfacts[i].PortNumber,
		       ioc->pfacts[i].ProtocolFlags,
		       MPT_PROTOCOL_FLAGS_c_c_c_c(
			       ioc->pfacts[i].ProtocolFlags));

		if (!(ioc->pfacts[i].ProtocolFlags &
					MPI_PORTFACTS_PROTOCOL_LAN)) {
			printk(KERN_INFO MYNAM ": %s: Hmmm... LAN protocol "
			       "seems to be disabled on this adapter port!\n",
			       ioc->name);
			continue;
		}

		dev = mpt_register_lan_device(ioc, i);
		if (!dev) {
			printk(KERN_ERR MYNAM ": %s: Unable to register "
			       "port%d as a LAN device\n", ioc->name,
			       ioc->pfacts[i].PortNumber);
			continue;
		}
		
		printk(KERN_INFO MYNAM ": %s: Fusion MPT LAN device "
		       "registered as '%s'\n", ioc->name, dev->name);
		printk(KERN_INFO MYNAM ": %s/%s: "
		       "LanAddr = %02X:%02X:%02X:%02X:%02X:%02X\n",
		       IOC_AND_NETDEV_NAMES_s_s(dev),
		       dev->dev_addr[0], dev->dev_addr[1],
		       dev->dev_addr[2], dev->dev_addr[3],
		       dev->dev_addr[4], dev->dev_addr[5]);
	
		ioc->netdev = dev;

		return 0;
	}

	return -ENODEV;
}

static void
mptlan_remove(struct pci_dev *pdev)
{
	MPT_ADAPTER 		*ioc = pci_get_drvdata(pdev);
	struct net_device	*dev = ioc->netdev;

	if(dev != NULL) {
		unregister_netdev(dev);
		free_netdev(dev);
	}
}

static struct mpt_pci_driver mptlan_driver = {
	.probe		= mptlan_probe,
	.remove		= mptlan_remove,
};

static int __init mpt_lan_init (void)
{
	show_mptmod_ver(LANAME, LANVER);

	if ((LanCtx = mpt_register(lan_reply, MPTLAN_DRIVER)) <= 0) {
		printk (KERN_ERR MYNAM ": Failed to register with MPT base driver\n");
		return -EBUSY;
	}

	dlprintk((KERN_INFO MYNAM ": assigned context of %d\n", LanCtx));

	if (mpt_reset_register(LanCtx, mpt_lan_ioc_reset)) {
		printk(KERN_ERR MYNAM ": Eieee! unable to register a reset "
		       "handler with mptbase! The world is at an end! "
		       "Everything is fading to black! Goodbye.\n");
		return -EBUSY;
	}

	dlprintk((KERN_INFO MYNAM ": Registered for IOC reset notifications\n"));
	
	mpt_device_driver_register(&mptlan_driver, MPTLAN_DRIVER);
	return 0;
}

static void __exit mpt_lan_exit(void)
{
	mpt_device_driver_deregister(MPTLAN_DRIVER);
	mpt_reset_deregister(LanCtx);

	if (LanCtx) {
		mpt_deregister(LanCtx);
		LanCtx = MPT_MAX_PROTOCOL_DRIVERS;
	}
}

module_init(mpt_lan_init);
module_exit(mpt_lan_exit);

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
static unsigned short
mpt_lan_type_trans(struct sk_buff *skb, struct net_device *dev)
{
	struct mpt_lan_ohdr *fch = (struct mpt_lan_ohdr *)skb->data;
	struct fcllc *fcllc;

	skb_reset_mac_header(skb);
	skb_pull(skb, sizeof(struct mpt_lan_ohdr));

	if (fch->dtype == htons(0xffff)) {
		u32 *p = (u32 *) fch;

		swab32s(p + 0);
		swab32s(p + 1);
		swab32s(p + 2);
		swab32s(p + 3);

		printk (KERN_WARNING MYNAM ": %s: WARNING - Broadcast swap F/W bug detected!\n",
				NETDEV_PTR_TO_IOC_NAME_s(dev));
		printk (KERN_WARNING MYNAM ": Please update sender @ MAC_addr = %02x:%02x:%02x:%02x:%02x:%02x\n",
				fch->saddr[0], fch->saddr[1], fch->saddr[2],
				fch->saddr[3], fch->saddr[4], fch->saddr[5]);
	}

	if (*fch->daddr & 1) {
		if (!memcmp(fch->daddr, dev->broadcast, FC_ALEN)) {
			skb->pkt_type = PACKET_BROADCAST;
		} else {
			skb->pkt_type = PACKET_MULTICAST;
		}
	} else {
		if (memcmp(fch->daddr, dev->dev_addr, FC_ALEN)) {
			skb->pkt_type = PACKET_OTHERHOST;
		} else {
			skb->pkt_type = PACKET_HOST;
		}
	}

	fcllc = (struct fcllc *)skb->data;


	/* Strip the SNAP header from ARP packets since we don't
	 * pass them through to the 802.2/SNAP layers.
	 */
	if (fcllc->dsap == EXTENDED_SAP &&
		(fcllc->ethertype == htons(ETH_P_IP) ||
		 fcllc->ethertype == htons(ETH_P_ARP))) {
		skb_pull(skb, sizeof(struct fcllc));
		return fcllc->ethertype;
	}

	return htons(ETH_P_802_2);
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/