aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/net/chelsio/cxgb2.c
blob: 349ebe783ed6b87892d242e679e5a9b210a51696 (plain) (blame)
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
/*****************************************************************************
 *                                                                           *
 * File: cxgb2.c                                                             *
 * $Revision: 1.25 $                                                         *
 * $Date: 2005/06/22 00:43:25 $                                              *
 * Description:                                                              *
 *  Chelsio 10Gb Ethernet Driver.                                            *
 *                                                                           *
 * This program is free software; you can redistribute it and/or modify      *
 * it under the terms of the GNU General Public License, version 2, as       *
 * published by the Free Software Foundation.                                *
 *                                                                           *
 * 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.                 *
 *                                                                           *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED    *
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF      *
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.                     *
 *                                                                           *
 * http://www.chelsio.com                                                    *
 *                                                                           *
 * Copyright (c) 2003 - 2005 Chelsio Communications, Inc.                    *
 * All rights reserved.                                                      *
 *                                                                           *
 * Maintainers: maintainers@chelsio.com                                      *
 *                                                                           *
 * Authors: Dimitrios Michailidis   <dm@chelsio.com>                         *
 *          Tina Yang               <tainay@chelsio.com>                     *
 *          Felix Marti             <felix@chelsio.com>                      *
 *          Scott Bardone           <sbardone@chelsio.com>                   *
 *          Kurt Ottaway            <kottaway@chelsio.com>                   *
 *          Frank DiMambro          <frank@chelsio.com>                      *
 *                                                                           *
 * History:                                                                  *
 *                                                                           *
 ****************************************************************************/

#include "common.h"
#include <linux/config.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/pci.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/if_vlan.h>
#include <linux/mii.h>
#include <linux/sockios.h>
#include <linux/proc_fs.h>
#include <linux/dma-mapping.h>
#include <asm/uaccess.h>

#include "cpl5_cmd.h"
#include "regs.h"
#include "gmac.h"
#include "cphy.h"
#include "sge.h"
#include "espi.h"

#ifdef work_struct
#include <linux/tqueue.h>
#define INIT_WORK INIT_TQUEUE
#define schedule_work schedule_task
#define flush_scheduled_work flush_scheduled_tasks

static inline void schedule_mac_stats_update(struct adapter *ap, int secs)
{
	mod_timer(&ap->stats_update_timer, jiffies + secs * HZ);
}

static inline void cancel_mac_stats_update(struct adapter *ap)
{
	del_timer_sync(&ap->stats_update_timer);
	flush_scheduled_tasks();
}

/*
 * Stats update timer for 2.4.  It schedules a task to do the actual update as
 * we need to access MAC statistics in process context.
 */
static void mac_stats_timer(unsigned long data)
{
	struct adapter *ap = (struct adapter *)data;

	schedule_task(&ap->stats_update_task);
}
#else
#include <linux/workqueue.h>

static inline void schedule_mac_stats_update(struct adapter *ap, int secs)
{
	schedule_delayed_work(&ap->stats_update_task, secs * HZ);
}

static inline void cancel_mac_stats_update(struct adapter *ap)
{
	cancel_delayed_work(&ap->stats_update_task);
}
#endif

#define MAX_CMDQ_ENTRIES 16384
#define MAX_CMDQ1_ENTRIES 1024
#define MAX_RX_BUFFERS 16384
#define MAX_RX_JUMBO_BUFFERS 16384
#define MAX_TX_BUFFERS_HIGH	16384U
#define MAX_TX_BUFFERS_LOW	1536U
#define MIN_FL_ENTRIES 32

#define PORT_MASK ((1 << MAX_NPORTS) - 1)

#define DFLT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK | \
			 NETIF_MSG_TIMER | NETIF_MSG_IFDOWN | NETIF_MSG_IFUP |\
			 NETIF_MSG_RX_ERR | NETIF_MSG_TX_ERR)

/*
 * The EEPROM is actually bigger but only the first few bytes are used so we
 * only report those.
 */
#define EEPROM_SIZE 32

MODULE_DESCRIPTION(DRV_DESCRIPTION);
MODULE_AUTHOR("Chelsio Communications");
MODULE_LICENSE("GPL");

static int dflt_msg_enable = DFLT_MSG_ENABLE;

MODULE_PARM(dflt_msg_enable, "i");
MODULE_PARM_DESC(dflt_msg_enable, "Chelsio T1 message enable bitmap");


static const char pci_speed[][4] = {
	"33", "66", "100", "133"
};

/*
 * Setup MAC to receive the types of packets we want.
 */
static void t1_set_rxmode(struct net_device *dev)
{
	struct adapter *adapter = dev->priv;
	struct cmac *mac = adapter->port[dev->if_port].mac;
	struct t1_rx_mode rm;

	rm.dev = dev;
	rm.idx = 0;
	rm.list = dev->mc_list;
	mac->ops->set_rx_mode(mac, &rm);
}

static void link_report(struct port_info *p)
{
	if (!netif_carrier_ok(p->dev))
        	printk(KERN_INFO "%s: link down\n", p->dev->name);
	else {
		const char *s = "10Mbps";

		switch (p->link_config.speed) {
			case SPEED_10000: s = "10Gbps"; break;
			case SPEED_1000:  s = "1000Mbps"; break;
			case SPEED_100:   s = "100Mbps"; break;
		}

        printk(KERN_INFO "%s: link up, %s, %s-duplex\n",
		       p->dev->name, s,
		       p->link_config.duplex == DUPLEX_FULL ? "full" : "half");
	}
}

void t1_link_changed(struct adapter *adapter, int port_id, int link_stat,
			int speed, int duplex, int pause)
{
	struct port_info *p = &adapter->port[port_id];

	if (link_stat != netif_carrier_ok(p->dev)) {
		if (link_stat)
			netif_carrier_on(p->dev);
		else
			netif_carrier_off(p->dev);
		link_report(p);

	}
}

static void link_start(struct port_info *p)
{
	struct cmac *mac = p->mac;

	mac->ops->reset(mac);
	if (mac->ops->macaddress_set)
		mac->ops->macaddress_set(mac, p->dev->dev_addr);
	t1_set_rxmode(p->dev);
	t1_link_start(p->phy, mac, &p->link_config);
	mac->ops->enable(mac, MAC_DIRECTION_RX | MAC_DIRECTION_TX);
}

static void enable_hw_csum(struct adapter *adapter)
{
	if (adapter->flags & TSO_CAPABLE)
		t1_tp_set_ip_checksum_offload(adapter, 1); /* for TSO only */
	t1_tp_set_tcp_checksum_offload(adapter, 1);
}

/*
 * Things to do upon first use of a card.
 * This must run with the rtnl lock held.
 */
static int cxgb_up(struct adapter *adapter)
{
	int err = 0;

	if (!(adapter->flags & FULL_INIT_DONE)) {
		err = t1_init_hw_modules(adapter);
		if (err)
			goto out_err;

		enable_hw_csum(adapter);
		adapter->flags |= FULL_INIT_DONE;
	}

	t1_interrupts_clear(adapter);
	if ((err = request_irq(adapter->pdev->irq,
			       t1_select_intr_handler(adapter), SA_SHIRQ,
			       adapter->name, adapter))) {
		goto out_err;
	}
	t1_sge_start(adapter->sge);
	t1_interrupts_enable(adapter);
 out_err:
	return err;
}

/*
 * Release resources when all the ports have been stopped.
 */
static void cxgb_down(struct adapter *adapter)
{
	t1_sge_stop(adapter->sge);
	t1_interrupts_disable(adapter);
	free_irq(adapter->pdev->irq, adapter);
}

static int cxgb_open(struct net_device *dev)
{
	int err;
	struct adapter *adapter = dev->priv;
	int other_ports = adapter->open_device_map & PORT_MASK;

	if (!adapter->open_device_map && (err = cxgb_up(adapter)) < 0)
		return err;

	__set_bit(dev->if_port, &adapter->open_device_map);
	link_start(&adapter->port[dev->if_port]);
	netif_start_queue(dev);
	if (!other_ports && adapter->params.stats_update_period)
		schedule_mac_stats_update(adapter,
					  adapter->params.stats_update_period);
	return 0;
}

static int cxgb_close(struct net_device *dev)
{
	struct adapter *adapter = dev->priv;
	struct port_info *p = &adapter->port[dev->if_port];
	struct cmac *mac = p->mac;

	netif_stop_queue(dev);
	mac->ops->disable(mac, MAC_DIRECTION_TX | MAC_DIRECTION_RX);
	netif_carrier_off(dev);

	clear_bit(dev->if_port, &adapter->open_device_map);
	if (adapter->params.stats_update_period &&
	    !(adapter->open_device_map & PORT_MASK)) {
		/* Stop statistics accumulation. */
		smp_mb__after_clear_bit();
		spin_lock(&adapter->work_lock);   /* sync with update task */
		spin_unlock(&adapter->work_lock);
		cancel_mac_stats_update(adapter);
	}

	if (!adapter->open_device_map)
		cxgb_down(adapter);
	return 0;
}

static struct net_device_stats *t1_get_stats(struct net_device *dev)
{
	struct adapter *adapter = dev->priv;
	struct port_info *p = &adapter->port[dev->if_port];
	struct net_device_stats *ns = &p->netstats;
	const struct cmac_statistics *pstats;

	/* Do a full update of the MAC stats */
	pstats = p->mac->ops->statistics_update(p->mac,
						      MAC_STATS_UPDATE_FULL);

	ns->tx_packets = pstats->TxUnicastFramesOK +
		pstats->TxMulticastFramesOK + pstats->TxBroadcastFramesOK;

	ns->rx_packets = pstats->RxUnicastFramesOK +
		pstats->RxMulticastFramesOK + pstats->RxBroadcastFramesOK;

	ns->tx_bytes = pstats->TxOctetsOK;
	ns->rx_bytes = pstats->RxOctetsOK;

	ns->tx_errors = pstats->TxLateCollisions + pstats->TxLengthErrors +
		pstats->TxUnderrun + pstats->TxFramesAbortedDueToXSCollisions;
	ns->rx_errors = pstats->RxDataErrors + pstats->RxJabberErrors +
		pstats->RxFCSErrors + pstats->RxAlignErrors +
		pstats->RxSequenceErrors + pstats->RxFrameTooLongErrors +
		pstats->RxSymbolErrors + pstats->RxRuntErrors;

	ns->multicast  = pstats->RxMulticastFramesOK;
	ns->collisions = pstats->TxTotalCollisions;

	/* detailed rx_errors */
	ns->rx_length_errors = pstats->RxFrameTooLongErrors +
		pstats->RxJabberErrors;
	ns->rx_over_errors   = 0;
	ns->rx_crc_errors    = pstats->RxFCSErrors;
	ns->rx_frame_errors  = pstats->RxAlignErrors;
	ns->rx_fifo_errors   = 0;
	ns->rx_missed_errors = 0;

	/* detailed tx_errors */
	ns->tx_aborted_errors   = pstats->TxFramesAbortedDueToXSCollisions;
	ns->tx_carrier_errors   = 0;
	ns->tx_fifo_errors      = pstats->TxUnderrun;
	ns->tx_heartbeat_errors = 0;
	ns->tx_window_errors    = pstats->TxLateCollisions;
	return ns;
}

static u32 get_msglevel(struct net_device *dev)
{
	struct adapter *adapter = dev->priv;

	return adapter->msg_enable;
}

static void set_msglevel(struct net_device *dev, u32 val)
{
	struct adapter *adapter = dev->priv;

	adapter->msg_enable = val;
}

static char stats_strings[][ETH_GSTRING_LEN] = {
        "TxOctetsOK",
        "TxOctetsBad",
        "TxUnicastFramesOK",
        "TxMulticastFramesOK",
        "TxBroadcastFramesOK",
        "TxPauseFrames",
        "TxFramesWithDeferredXmissions",
        "TxLateCollisions",
        "TxTotalCollisions",
        "TxFramesAbortedDueToXSCollisions",
        "TxUnderrun",
        "TxLengthErrors",
        "TxInternalMACXmitError",
        "TxFramesWithExcessiveDeferral",
        "TxFCSErrors",

        "RxOctetsOK",
        "RxOctetsBad",
        "RxUnicastFramesOK",
        "RxMulticastFramesOK",
        "RxBroadcastFramesOK",
        "RxPauseFrames",
        "RxFCSErrors",
        "RxAlignErrors",
        "RxSymbolErrors",
        "RxDataErrors",
        "RxSequenceErrors",
        "RxRuntErrors",
        "RxJabberErrors",
        "RxInternalMACRcvError",
        "RxInRangeLengthErrors",
        "RxOutOfRangeLengthField",
        "RxFrameTooLongErrors",

	"TSO",
	"VLANextractions",
	"VLANinsertions",
	"RxCsumGood",
	"TxCsumOffload",
	"RxDrops"

	"respQ_empty",
	"respQ_overflow",
	"freelistQ_empty",
	"pkt_too_big",
	"pkt_mismatch",
	"cmdQ_full0",
	"cmdQ_full1",
	"tx_ipfrags",
	"tx_reg_pkts",
	"tx_lso_pkts",
	"tx_do_cksum",
	
	"espi_DIP2ParityErr",
	"espi_DIP4Err",
	"espi_RxDrops",
	"espi_TxDrops",
	"espi_RxOvfl",
	"espi_ParityErr"
};
 
#define T2_REGMAP_SIZE (3 * 1024)

static int get_regs_len(struct net_device *dev)
{
	return T2_REGMAP_SIZE;
}

static void get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
{
	struct adapter *adapter = dev->priv;

	strcpy(info->driver, DRV_NAME);
	strcpy(info->version, DRV_VERSION);
	strcpy(info->fw_version, "N/A");
	strcpy(info->bus_info, pci_name(adapter->pdev));
}

static int get_stats_count(struct net_device *dev)
{
	return ARRAY_SIZE(stats_strings);
}

static void get_strings(struct net_device *dev, u32 stringset, u8 *data)
{
	if (stringset == ETH_SS_STATS)
		memcpy(data, stats_strings, sizeof(stats_strings));
}

static void get_stats(struct net_device *dev, struct ethtool_stats *stats,
		      u64 *data)
{
	struct adapter *adapter = dev->priv;
	struct cmac *mac = adapter->port[dev->if_port].mac;
	const struct cmac_statistics *s;
	const struct sge_port_stats *ss;
	const struct sge_intr_counts *t;

	s = mac->ops->statistics_update(mac, MAC_STATS_UPDATE_FULL);
	ss = t1_sge_get_port_stats(adapter->sge, dev->if_port);
	t = t1_sge_get_intr_counts(adapter->sge);

        *data++ = s->TxOctetsOK;
        *data++ = s->TxOctetsBad;
        *data++ = s->TxUnicastFramesOK;
        *data++ = s->TxMulticastFramesOK;
        *data++ = s->TxBroadcastFramesOK;
        *data++ = s->TxPauseFrames;
        *data++ = s->TxFramesWithDeferredXmissions;
        *data++ = s->TxLateCollisions;
        *data++ = s->TxTotalCollisions;
        *data++ = s->TxFramesAbortedDueToXSCollisions;
        *data++ = s->TxUnderrun;
        *data++ = s->TxLengthErrors;
        *data++ = s->TxInternalMACXmitError;
        *data++ = s->TxFramesWithExcessiveDeferral;
        *data++ = s->TxFCSErrors;

        *data++ = s->RxOctetsOK;
        *data++ = s->RxOctetsBad;
        *data++ = s->RxUnicastFramesOK;
        *data++ = s->RxMulticastFramesOK;
        *data++ = s->RxBroadcastFramesOK;
        *data++ = s->RxPauseFrames;
        *data++ = s->RxFCSErrors;
        *data++ = s->RxAlignErrors;
        *data++ = s->RxSymbolErrors;
        *data++ = s->RxDataErrors;
        *data++ = s->RxSequenceErrors;
        *data++ = s->RxRuntErrors;
        *data++ = s->RxJabberErrors;
        *data++ = s->RxInternalMACRcvError;
        *data++ = s->RxInRangeLengthErrors;
        *data++ = s->RxOutOfRangeLengthField;
        *data++ = s->RxFrameTooLongErrors;

	*data++ = ss->tso;
	*data++ = ss->vlan_xtract;
	*data++ = ss->vlan_insert;
	*data++ = ss->rx_cso_good;
	*data++ = ss->tx_cso;
	*data++ = ss->rx_drops;

	*data++ = (u64)t->respQ_empty;
	*data++ = (u64)t->respQ_overflow;
	*data++ = (u64)t->freelistQ_empty;
	*data++ = (u64)t->pkt_too_big;
	*data++ = (u64)t->pkt_mismatch;
	*data++ = (u64)t->cmdQ_full[0];
	*data++ = (u64)t->cmdQ_full[1];
	*data++ = (u64)t->tx_ipfrags;
	*data++ = (u64)t->tx_reg_pkts;
	*data++ = (u64)t->tx_lso_pkts;
	*data++ = (u64)t->tx_do_cksum;
}

static inline void reg_block_dump(struct adapter *ap, void *buf,
				  unsigned int start, unsigned int end)
{
	u32 *p = buf + start;

	for ( ; start <= end; start += sizeof(u32))
		*p++ = readl(ap->regs + start);
}

static void get_regs(struct net_device *dev, struct ethtool_regs *regs,
		     void *buf)
{
	struct adapter *ap = dev->priv;

	/*
	 * Version scheme: bits 0..9: chip version, bits 10..15: chip revision
	 */
	regs->version = 2;

	memset(buf, 0, T2_REGMAP_SIZE);
	reg_block_dump(ap, buf, 0, A_SG_RESPACCUTIMER);
}

static int get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
{
	struct adapter *adapter = dev->priv;
	struct port_info *p = &adapter->port[dev->if_port];

	cmd->supported = p->link_config.supported;
	cmd->advertising = p->link_config.advertising;

	if (netif_carrier_ok(dev)) {
		cmd->speed = p->link_config.speed;
		cmd->duplex = p->link_config.duplex;
	} else {
		cmd->speed = -1;
		cmd->duplex = -1;
	}

        cmd->port = (cmd->supported & SUPPORTED_TP) ? PORT_TP : PORT_FIBRE;
        cmd->phy_address = p->phy->addr;
        cmd->transceiver = XCVR_EXTERNAL;
        cmd->autoneg = p->link_config.autoneg;
        cmd->maxtxpkt = 0;
        cmd->maxrxpkt = 0;
	return 0;
}

static int speed_duplex_to_caps(int speed, int duplex)
{
	int cap = 0;

	switch (speed) {
	case SPEED_10:
		if (duplex == DUPLEX_FULL)
			cap = SUPPORTED_10baseT_Full;
		else
			cap = SUPPORTED_10baseT_Half;
		break;
	case SPEED_100:
		if (duplex == DUPLEX_FULL)
			cap = SUPPORTED_100baseT_Full;
		else
			cap = SUPPORTED_100baseT_Half;
		break;
	case SPEED_1000:
		if (duplex == DUPLEX_FULL)
			cap = SUPPORTED_1000baseT_Full;
		else
			cap = SUPPORTED_1000baseT_Half;
		break;
	case SPEED_10000:
		if (duplex == DUPLEX_FULL)
			cap = SUPPORTED_10000baseT_Full;
	}
	return cap;
}

#define ADVERTISED_MASK (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full | \
		      ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full | \
		      ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full | \
		      ADVERTISED_10000baseT_Full)

static int set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
{
	struct adapter *adapter = dev->priv;
	struct port_info *p = &adapter->port[dev->if_port];
	struct link_config *lc = &p->link_config;

	if (!(lc->supported & SUPPORTED_Autoneg))
		return -EOPNOTSUPP;             /* can't change speed/duplex */

	if (cmd->autoneg == AUTONEG_DISABLE) {
		int cap = speed_duplex_to_caps(cmd->speed, cmd->duplex);

		if (!(lc->supported & cap) || cmd->speed == SPEED_1000)
			return -EINVAL;
		lc->requested_speed = cmd->speed;
		lc->requested_duplex = cmd->duplex;
		lc->advertising = 0;
	} else {
		cmd->advertising &= ADVERTISED_MASK;
		if (cmd->advertising & (cmd->advertising - 1))
			cmd->advertising = lc->supported;
		cmd->advertising &= lc->supported;
		if (!cmd->advertising)
			return -EINVAL;
		lc->requested_speed = SPEED_INVALID;
		lc->requested_duplex = DUPLEX_INVALID;
		lc->advertising = cmd->advertising | ADVERTISED_Autoneg;
	}
	lc->autoneg = cmd->autoneg;
	if (netif_running(dev))
		t1_link_start(p->phy, p->mac, lc);
	return 0;
}

static void get_pauseparam(struct net_device *dev,
			   struct ethtool_pauseparam *epause)
{
	struct adapter *adapter = dev->priv;
	struct port_info *p = &adapter->port[dev->if_port];

	epause->autoneg = (p->link_config.requested_fc & PAUSE_AUTONEG) != 0;
	epause->rx_pause = (p->link_config.fc & PAUSE_RX) != 0;
	epause->tx_pause = (p->link_config.fc & PAUSE_TX) != 0;
}

static int set_pauseparam(struct net_device *dev,
			  struct ethtool_pauseparam *epause)
{
	struct adapter *adapter = dev->priv;
	struct port_info *p = &adapter->port[dev->if_port];
	struct link_config *lc = &p->link_config;

	if (epause->autoneg == AUTONEG_DISABLE)
		lc->requested_fc = 0;
	else if (lc->supported & SUPPORTED_Autoneg)
		lc->requested_fc = PAUSE_AUTONEG;
	else
		return -EINVAL;

	if (epause->rx_pause)
		lc->requested_fc |= PAUSE_RX;
	if (epause->tx_pause)
		lc->requested_fc |= PAUSE_TX;
	if (lc->autoneg == AUTONEG_ENABLE) {
		if (netif_running(dev))
			t1_link_start(p->phy, p->mac, lc);
	} else {
		lc->fc = lc->requested_fc & (PAUSE_RX | PAUSE_TX);
		if (netif_running(dev))
			p->mac->ops->set_speed_duplex_fc(p->mac, -1, -1,
							 lc->fc);
	}
	return 0;
}

static u32 get_rx_csum(struct net_device *dev)
{
	struct adapter *adapter = dev->priv;

	return (adapter->flags & RX_CSUM_ENABLED) != 0;
}

static int set_rx_csum(struct net_device *dev, u32 data)
{
	struct adapter *adapter = dev->priv;

	if (data)
		adapter->flags |= RX_CSUM_ENABLED;
	else
		adapter->flags &= ~RX_CSUM_ENABLED;
	return 0;
}

static int set_tso(struct net_device *dev, u32 value)
{
	struct adapter *adapter = dev->priv;

	if (!(adapter->flags & TSO_CAPABLE))
		return value ? -EOPNOTSUPP : 0;
	return ethtool_op_set_tso(dev, value);
}

static void get_sge_param(struct net_device *dev, struct ethtool_ringparam *e)
{
	struct adapter *adapter = dev->priv;
	int jumbo_fl = t1_is_T1B(adapter) ? 1 : 0;

	e->rx_max_pending = MAX_RX_BUFFERS;
	e->rx_mini_max_pending = 0;
	e->rx_jumbo_max_pending = MAX_RX_JUMBO_BUFFERS;
	e->tx_max_pending = MAX_CMDQ_ENTRIES;

	e->rx_pending = adapter->params.sge.freelQ_size[!jumbo_fl];
	e->rx_mini_pending = 0;
	e->rx_jumbo_pending = adapter->params.sge.freelQ_size[jumbo_fl];
	e->tx_pending = adapter->params.sge.cmdQ_size[0];
}

static int set_sge_param(struct net_device *dev, struct ethtool_ringparam *e)
{
	struct adapter *adapter = dev->priv;
	int jumbo_fl = t1_is_T1B(adapter) ? 1 : 0;

	if (e->rx_pending > MAX_RX_BUFFERS || e->rx_mini_pending ||
	    e->rx_jumbo_pending > MAX_RX_JUMBO_BUFFERS ||
	    e->tx_pending > MAX_CMDQ_ENTRIES ||
	    e->rx_pending < MIN_FL_ENTRIES ||
	    e->rx_jumbo_pending < MIN_FL_ENTRIES ||
	    e->tx_pending < (adapter->params.nports + 1) * (MAX_SKB_FRAGS + 1))
		return -EINVAL;

	if (adapter->flags & FULL_INIT_DONE)
        return -EBUSY;

	adapter->params.sge.freelQ_size[!jumbo_fl] = e->rx_pending;
	adapter->params.sge.freelQ_size[jumbo_fl] = e->rx_jumbo_pending;
	adapter->params.sge.cmdQ_size[0] = e->tx_pending;
	adapter->params.sge.cmdQ_size[1] = e->tx_pending > MAX_CMDQ1_ENTRIES ?
		MAX_CMDQ1_ENTRIES : e->tx_pending;
	return 0;
}

static int set_coalesce(struct net_device *dev, struct ethtool_coalesce *c)
{
	struct adapter *adapter = dev->priv;

	/*
	 * If RX coalescing is requested we use NAPI, otherwise interrupts.
	 * This choice can be made only when all ports and the TOE are off.
	 */
	if (adapter->open_device_map == 0)
		adapter->params.sge.polling = c->use_adaptive_rx_coalesce;

	if (adapter->params.sge.polling) {
		adapter->params.sge.rx_coalesce_usecs = 0;
	} else {
		adapter->params.sge.rx_coalesce_usecs = c->rx_coalesce_usecs;
	}
 	adapter->params.sge.coalesce_enable = c->use_adaptive_rx_coalesce;
	adapter->params.sge.sample_interval_usecs = c->rate_sample_interval;
	t1_sge_set_coalesce_params(adapter->sge, &adapter->params.sge);
	return 0;
}

static int get_coalesce(struct net_device *dev, struct ethtool_coalesce *c)
{
	struct adapter *adapter = dev->priv;

	c->rx_coalesce_usecs = adapter->params.sge.rx_coalesce_usecs;
	c->rate_sample_interval = adapter->params.sge.sample_interval_usecs;
	c->use_adaptive_rx_coalesce = adapter->params.sge.coalesce_enable;
	return 0;
}

static int get_eeprom_len(struct net_device *dev)
{
    return EEPROM_SIZE;
}

#define EEPROM_MAGIC(ap) \
	(PCI_VENDOR_ID_CHELSIO | ((ap)->params.chip_version << 16))

static int get_eeprom(struct net_device *dev, struct ethtool_eeprom *e,
		      u8 *data)
{
	int i;
	u8 buf[EEPROM_SIZE] __attribute__((aligned(4)));
	struct adapter *adapter = dev->priv;

	e->magic = EEPROM_MAGIC(adapter);
	for (i = e->offset & ~3; i < e->offset + e->len; i += sizeof(u32))
		t1_seeprom_read(adapter, i, (u32 *)&buf[i]);
	memcpy(data, buf + e->offset, e->len);
	return 0;
}

static struct ethtool_ops t1_ethtool_ops = {
	.get_settings      = get_settings,
	.set_settings      = set_settings,
	.get_drvinfo       = get_drvinfo,
	.get_msglevel      = get_msglevel,
	.set_msglevel      = set_msglevel,
	.get_ringparam     = get_sge_param,
	.set_ringparam     = set_sge_param,
	.get_coalesce      = get_coalesce,
	.set_coalesce      = set_coalesce,
	.get_eeprom_len    = get_eeprom_len,
	.get_eeprom        = get_eeprom,
	.get_pauseparam    = get_pauseparam,
	.set_pauseparam    = set_pauseparam,
	.get_rx_csum       = get_rx_csum,
	.set_rx_csum       = set_rx_csum,
	.get_tx_csum       = ethtool_op_get_tx_csum,
	.set_tx_csum       = ethtool_op_set_tx_csum,
	.get_sg            = ethtool_op_get_sg,
	.set_sg            = ethtool_op_set_sg,
	.get_link          = ethtool_op_get_link,
	.get_strings       = get_strings,
	.get_stats_count   = get_stats_count,
	.get_ethtool_stats = get_stats,
	.get_regs_len      = get_regs_len,
	.get_regs          = get_regs,
	.get_tso           = ethtool_op_get_tso,
	.set_tso           = set_tso,
};

static void cxgb_proc_cleanup(struct adapter *adapter,
					struct proc_dir_entry *dir)
{
	const char *name;
	name = adapter->name;
	remove_proc_entry(name, dir);
}
//#define chtoe_setup_toedev(adapter) NULL
#define update_mtu_tab(adapter)
#define write_smt_entry(adapter, idx)

static int t1_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
{
        struct adapter *adapter = dev->priv;
        struct mii_ioctl_data *data = if_mii(req);

	switch (cmd) {
        case SIOCGMIIPHY:
                data->phy_id = adapter->port[dev->if_port].phy->addr;
                /* FALLTHRU */
        case SIOCGMIIREG: {
		struct cphy *phy = adapter->port[dev->if_port].phy;
		u32 val;

		if (!phy->mdio_read)
            return -EOPNOTSUPP;
		phy->mdio_read(adapter, data->phy_id, 0, data->reg_num & 0x1f,
			       &val);
                data->val_out = val;
                break;
	}
        case SIOCSMIIREG: {
		struct cphy *phy = adapter->port[dev->if_port].phy;

                if (!capable(CAP_NET_ADMIN))
                    return -EPERM;
		if (!phy->mdio_write)
            return -EOPNOTSUPP;
		phy->mdio_write(adapter, data->phy_id, 0, data->reg_num & 0x1f,
			        data->val_in);
                break;
	}

	default:
		return -EOPNOTSUPP;
	}
	return 0;
}

static int t1_change_mtu(struct net_device *dev, int new_mtu)
{
	int ret;
	struct adapter *adapter = dev->priv;
	struct cmac *mac = adapter->port[dev->if_port].mac;

	if (!mac->ops->set_mtu)
        return -EOPNOTSUPP;
	if (new_mtu < 68)
        return -EINVAL;
	if ((ret = mac->ops->set_mtu(mac, new_mtu)))
		return ret;
	dev->mtu = new_mtu;
	return 0;
}

static int t1_set_mac_addr(struct net_device *dev, void *p)
{
	struct adapter *adapter = dev->priv;
	struct cmac *mac = adapter->port[dev->if_port].mac;
	struct sockaddr *addr = p;

	if (!mac->ops->macaddress_set)
		return -EOPNOTSUPP;

	memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
	mac->ops->macaddress_set(mac, dev->dev_addr);
	return 0;
}

#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
static void vlan_rx_register(struct net_device *dev,
				   struct vlan_group *grp)
{
	struct adapter *adapter = dev->priv;

	spin_lock_irq(&adapter->async_lock);
	adapter->vlan_grp = grp;
	t1_set_vlan_accel(adapter, grp != NULL);
	spin_unlock_irq(&adapter->async_lock);
}

static void vlan_rx_kill_vid(struct net_device *dev, unsigned short vid)
{
	struct adapter *adapter = dev->priv;

	spin_lock_irq(&adapter->async_lock);
	if (adapter->vlan_grp)
		adapter->vlan_grp->vlan_devices[vid] = NULL;
	spin_unlock_irq(&adapter->async_lock);
}
#endif

#ifdef CONFIG_NET_POLL_CONTROLLER
static void t1_netpoll(struct net_device *dev)
{
	unsigned long flags;
	struct adapter *adapter = dev->priv;

	local_irq_save(flags);
        t1_select_intr_handler(adapter)(adapter->pdev->irq, adapter, NULL);
	local_irq_restore(flags);
}
#endif

/*
 * Periodic accumulation of MAC statistics.  This is used only if the MAC
 * does not have any other way to prevent stats counter overflow.
 */
static void mac_stats_task(void *data)
{
	int i;
	struct adapter *adapter = data;

	for_each_port(adapter, i) {
		struct port_info *p = &adapter->port[i];

		if (netif_running(p->dev))
			p->mac->ops->statistics_update(p->mac,
						       MAC_STATS_UPDATE_FAST);
	}

	/* Schedule the next statistics update if any port is active. */
	spin_lock(&adapter->work_lock);
	if (adapter->open_device_map & PORT_MASK)
		schedule_mac_stats_update(adapter,
					  adapter->params.stats_update_period);
	spin_unlock(&adapter->work_lock);
}

/*
 * Processes elmer0 external interrupts in process context.
 */
static void ext_intr_task(void *data)
{
	struct adapter *adapter = data;

	elmer0_ext_intr_handler(adapter);

	/* Now reenable external interrupts */
	spin_lock_irq(&adapter->async_lock);
	adapter->slow_intr_mask |= F_PL_INTR_EXT;
	writel(F_PL_INTR_EXT, adapter->regs + A_PL_CAUSE);
	writel(adapter->slow_intr_mask | F_PL_INTR_SGE_DATA,
                   adapter->regs + A_PL_ENABLE);
	spin_unlock_irq(&adapter->async_lock);
}

/*
 * Interrupt-context handler for elmer0 external interrupts.
 */
void t1_elmer0_ext_intr(struct adapter *adapter)
{
	/*
	 * Schedule a task to handle external interrupts as we require
	 * a process context.  We disable EXT interrupts in the interim
	 * and let the task reenable them when it's done.
	 */
	adapter->slow_intr_mask &= ~F_PL_INTR_EXT;
	writel(adapter->slow_intr_mask | F_PL_INTR_SGE_DATA,
                   adapter->regs + A_PL_ENABLE);
	schedule_work(&adapter->ext_intr_handler_task);
}

void t1_fatal_err(struct adapter *adapter)
{
	if (adapter->flags & FULL_INIT_DONE) {
		t1_sge_stop(adapter->sge);
		t1_interrupts_disable(adapter);
	}
	CH_ALERT("%s: encountered fatal error, operation suspended\n",
		 adapter->name);
}

static int __devinit init_one(struct pci_dev *pdev,
			      const struct pci_device_id *ent)
{
	static int version_printed;

	int i, err, pci_using_dac = 0;
	unsigned long mmio_start, mmio_len;
	const struct board_info *bi;
	struct adapter *adapter = NULL;
	struct port_info *pi;

	if (!version_printed) {
		printk(KERN_INFO "%s - version %s\n", DRV_DESCRIPTION,
		       DRV_VERSION);
		++version_printed;
	}

	err = pci_enable_device(pdev);
	if (err)
        return err;

	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
		CH_ERR("%s: cannot find PCI device memory base address\n",
		       pci_name(pdev));
		err = -ENODEV;
		goto out_disable_pdev;
	}

	if (!pci_set_dma_mask(pdev, DMA_64BIT_MASK)) {
		pci_using_dac = 1;

		if (pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK)) {
			CH_ERR("%s: unable to obtain 64-bit DMA for"
			       "consistent allocations\n", pci_name(pdev));
			err = -ENODEV;
			goto out_disable_pdev;
		}

	} else if ((err = pci_set_dma_mask(pdev, DMA_32BIT_MASK)) != 0) {
		CH_ERR("%s: no usable DMA configuration\n", pci_name(pdev));
		goto out_disable_pdev;
	}

	err = pci_request_regions(pdev, DRV_NAME);
	if (err) {
		CH_ERR("%s: cannot obtain PCI resources\n", pci_name(pdev));
		goto out_disable_pdev;
	}

	pci_set_master(pdev);

    mmio_start = pci_resource_start(pdev, 0);
	mmio_len = pci_resource_len(pdev, 0);
	bi = t1_get_board_info(ent->driver_data);

	for (i = 0; i < bi->port_number; ++i) {
		struct net_device *netdev;

		netdev = alloc_etherdev(adapter ? 0 : sizeof(*adapter));
		if (!netdev) {
			err = -ENOMEM;
			goto out_free_dev;
		}

		SET_MODULE_OWNER(netdev);
		SET_NETDEV_DEV(netdev, &pdev->dev);

		if (!adapter) {
			adapter = netdev->priv;
			adapter->pdev = pdev;
			adapter->port[0].dev = netdev;  /* so we don't leak it */

			adapter->regs = ioremap(mmio_start, mmio_len);
			if (!adapter->regs) {
				CH_ERR("%s: cannot map device registers\n",
				       pci_name(pdev));
				err = -ENOMEM;
				goto out_free_dev;
			}

			if (t1_get_board_rev(adapter, bi, &adapter->params)) {
				err = -ENODEV;	  /* Can't handle this chip rev */
				goto out_free_dev;
			}

			adapter->name = pci_name(pdev);
			adapter->msg_enable = dflt_msg_enable;
			adapter->mmio_len = mmio_len;

			init_MUTEX(&adapter->mib_mutex);
			spin_lock_init(&adapter->tpi_lock);
			spin_lock_init(&adapter->work_lock);
			spin_lock_init(&adapter->async_lock);

			INIT_WORK(&adapter->ext_intr_handler_task,
				  ext_intr_task, adapter);
			INIT_WORK(&adapter->stats_update_task, mac_stats_task,
				  adapter);
#ifdef work_struct
			init_timer(&adapter->stats_update_timer);
			adapter->stats_update_timer.function = mac_stats_timer;
			adapter->stats_update_timer.data =
				(unsigned long)adapter;
#endif

			pci_set_drvdata(pdev, netdev);
		}

		pi = &adapter->port[i];
		pi->dev = netdev;
		netif_carrier_off(netdev);
		netdev->irq = pdev->irq;
		netdev->if_port = i;
		netdev->mem_start = mmio_start;
		netdev->mem_end = mmio_start + mmio_len - 1;
		netdev->priv = adapter;
		netdev->features |= NETIF_F_SG | NETIF_F_IP_CSUM;
		netdev->features |= NETIF_F_LLTX;

		adapter->flags |= RX_CSUM_ENABLED | TCP_CSUM_CAPABLE;
		if (pci_using_dac)
			netdev->features |= NETIF_F_HIGHDMA;
		if (vlan_tso_capable(adapter)) {
#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
			adapter->flags |= VLAN_ACCEL_CAPABLE;
			netdev->features |=
				NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
			netdev->vlan_rx_register = vlan_rx_register;
			netdev->vlan_rx_kill_vid = vlan_rx_kill_vid;
#endif
			adapter->flags |= TSO_CAPABLE;
			netdev->features |= NETIF_F_TSO;
		}

		netdev->open = cxgb_open;
		netdev->stop = cxgb_close;
		netdev->hard_start_xmit = t1_start_xmit;
		netdev->hard_header_len += (adapter->flags & TSO_CAPABLE) ?
			sizeof(struct cpl_tx_pkt_lso) :
			sizeof(struct cpl_tx_pkt);
		netdev->get_stats = t1_get_stats;
		netdev->set_multicast_list = t1_set_rxmode;
		netdev->do_ioctl = t1_ioctl;
		netdev->change_mtu = t1_change_mtu;
		netdev->set_mac_address = t1_set_mac_addr;
#ifdef CONFIG_NET_POLL_CONTROLLER
		netdev->poll_controller = t1_netpoll;
#endif
		netdev->weight = 64;

        SET_ETHTOOL_OPS(netdev, &t1_ethtool_ops);
	}

	if (t1_init_sw_modules(adapter, bi) < 0) {
		err = -ENODEV;
		goto out_free_dev;
	}

	/*
	 * The card is now ready to go.  If any errors occur during device
	 * registration we do not fail the whole card but rather proceed only
	 * with the ports we manage to register successfully.  However we must
	 * register at least one net device.
	 */
	for (i = 0; i < bi->port_number; ++i) {
		err = register_netdev(adapter->port[i].dev);
		if (err)
			CH_WARN("%s: cannot register net device %s, skipping\n",
				pci_name(pdev), adapter->port[i].dev->name);
		else {
			/*
			 * Change the name we use for messages to the name of
			 * the first successfully registered interface.
			 */
			if (!adapter->registered_device_map)
				adapter->name = adapter->port[i].dev->name;

                __set_bit(i, &adapter->registered_device_map);
		}
	}
	if (!adapter->registered_device_map) {
		CH_ERR("%s: could not register any net devices\n",
		       pci_name(pdev));
		goto out_release_adapter_res;
	}

	printk(KERN_INFO "%s: %s (rev %d), %s %dMHz/%d-bit\n", adapter->name,
	       bi->desc, adapter->params.chip_revision,
	       adapter->params.pci.is_pcix ? "PCIX" : "PCI",
	       adapter->params.pci.speed, adapter->params.pci.width);
	return 0;

 out_release_adapter_res:
	t1_free_sw_modules(adapter);
 out_free_dev:
	if (adapter) {
		if (adapter->regs) iounmap(adapter->regs);
		for (i = bi->port_number - 1; i >= 0; --i)
			if (adapter->port[i].dev) {
				cxgb_proc_cleanup(adapter, proc_root_driver);
				kfree(adapter->port[i].dev);
			}
	}
	pci_release_regions(pdev);
 out_disable_pdev:
	pci_disable_device(pdev);
	pci_set_drvdata(pdev, NULL);
	return err;
}

static inline void t1_sw_reset(struct pci_dev *pdev)
{
	pci_write_config_dword(pdev, A_PCICFG_PM_CSR, 3);
	pci_write_config_dword(pdev, A_PCICFG_PM_CSR, 0);
}

static void __devexit remove_one(struct pci_dev *pdev)
{
	struct net_device *dev = pci_get_drvdata(pdev);

	if (dev) {
		int i;
		struct adapter *adapter = dev->priv;

		for_each_port(adapter, i)
			if (test_bit(i, &adapter->registered_device_map))
				unregister_netdev(adapter->port[i].dev);

		t1_free_sw_modules(adapter);
		iounmap(adapter->regs);
		while (--i >= 0)
			if (adapter->port[i].dev) {
				cxgb_proc_cleanup(adapter, proc_root_driver);
				kfree(adapter->port[i].dev);
			}
		pci_release_regions(pdev);
		pci_disable_device(pdev);
		pci_set_drvdata(pdev, NULL);
		t1_sw_reset(pdev);
	}
}

static struct pci_driver driver = {
	.name     = DRV_NAME,
	.id_table = t1_pci_tbl,
	.probe    = init_one,
	.remove   = __devexit_p(remove_one),
};

static int __init t1_init_module(void)
{
	return pci_module_init(&driver);
}

static void __exit t1_cleanup_module(void)
{
	pci_unregister_driver(&driver);
}

module_init(t1_init_module);
module_exit(t1_cleanup_module);
span> (((privptr->channel[READ].flag | privptr->channel[WRITE].flag) & CLAW_TIMER) != 0x00)) { dev_info(&privptr->channel[READ].cdev->dev, "%s: remote side is not ready\n", dev->name); CLAW_DBF_TEXT(2, trace, "notrdy"); for ( i = 0; i < 2; i++) { spin_lock_irqsave( get_ccwdev_lock(privptr->channel[i].cdev), saveflags); parm = (unsigned long) &privptr->channel[i]; privptr->channel[i].claw_state = CLAW_STOP; rc = ccw_device_halt( (struct ccw_device *)&privptr->channel[i].cdev, parm); spin_unlock_irqrestore( get_ccwdev_lock(privptr->channel[i].cdev), saveflags); if (rc != 0) { ccw_check_return_code( privptr->channel[i].cdev, rc); } } free_pages((unsigned long)privptr->p_buff_ccw, (int)pages_to_order_of_mag(privptr->p_buff_ccw_num)); if (privptr->p_env->read_size < PAGE_SIZE) { free_pages((unsigned long)privptr->p_buff_read, (int)pages_to_order_of_mag( privptr->p_buff_read_num)); } else { p_buf=privptr->p_read_active_first; while (p_buf!=NULL) { free_pages((unsigned long)p_buf->p_buffer, (int)pages_to_order_of_mag( privptr->p_buff_pages_perread )); p_buf=p_buf->next; } } if (privptr->p_env->write_size < PAGE_SIZE ) { free_pages((unsigned long)privptr->p_buff_write, (int)pages_to_order_of_mag( privptr->p_buff_write_num)); } else { p_buf=privptr->p_write_active_first; while (p_buf!=NULL) { free_pages((unsigned long)p_buf->p_buffer, (int)pages_to_order_of_mag( privptr->p_buff_pages_perwrite )); p_buf=p_buf->next; } } privptr->buffs_alloc = 0; privptr->channel[READ].flag= 0x00; privptr->channel[WRITE].flag = 0x00; privptr->p_buff_ccw=NULL; privptr->p_buff_read=NULL; privptr->p_buff_write=NULL; claw_clear_busy(dev); CLAW_DBF_TEXT(2, trace, "open EIO"); return -EIO; } /* Send SystemValidate command */ claw_clear_busy(dev); CLAW_DBF_TEXT(4, trace, "openok"); return 0; } /* end of claw_open */ /*-------------------------------------------------------------------* * * * claw_irq_handler * * * *--------------------------------------------------------------------*/ static void claw_irq_handler(struct ccw_device *cdev, unsigned long intparm, struct irb *irb) { struct chbk *p_ch = NULL; struct claw_privbk *privptr = NULL; struct net_device *dev = NULL; struct claw_env *p_env; struct chbk *p_ch_r=NULL; CLAW_DBF_TEXT(4, trace, "clawirq"); /* Bypass all 'unsolicited interrupts' */ privptr = dev_get_drvdata(&cdev->dev); if (!privptr) { dev_warn(&cdev->dev, "An uninitialized CLAW device received an" " IRQ, c-%02x d-%02x\n", irb->scsw.cmd.cstat, irb->scsw.cmd.dstat); CLAW_DBF_TEXT(2, trace, "badirq"); return; } /* Try to extract channel from driver data. */ if (privptr->channel[READ].cdev == cdev) p_ch = &privptr->channel[READ]; else if (privptr->channel[WRITE].cdev == cdev) p_ch = &privptr->channel[WRITE]; else { dev_warn(&cdev->dev, "The device is not a CLAW device\n"); CLAW_DBF_TEXT(2, trace, "badchan"); return; } CLAW_DBF_TEXT_(4, trace, "IRQCH=%d", p_ch->flag); dev = (struct net_device *) (p_ch->ndev); p_env=privptr->p_env; /* Copy interruption response block. */ memcpy(p_ch->irb, irb, sizeof(struct irb)); /* Check for good subchannel return code, otherwise info message */ if (irb->scsw.cmd.cstat && !(irb->scsw.cmd.cstat & SCHN_STAT_PCI)) { dev_info(&cdev->dev, "%s: subchannel check for device: %04x -" " Sch Stat %02x Dev Stat %02x CPA - %04x\n", dev->name, p_ch->devno, irb->scsw.cmd.cstat, irb->scsw.cmd.dstat, irb->scsw.cmd.cpa); CLAW_DBF_TEXT(2, trace, "chanchk"); /* return; */ } /* Check the reason-code of a unit check */ if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) ccw_check_unit_check(p_ch, irb->ecw[0]); /* State machine to bring the connection up, down and to restart */ p_ch->last_dstat = irb->scsw.cmd.dstat; switch (p_ch->claw_state) { case CLAW_STOP:/* HALT_IO by claw_release (halt sequence) */ if (!((p_ch->irb->scsw.cmd.stctl & SCSW_STCTL_SEC_STATUS) || (p_ch->irb->scsw.cmd.stctl == SCSW_STCTL_STATUS_PEND) || (p_ch->irb->scsw.cmd.stctl == (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND)))) return; wake_up(&p_ch->wait); /* wake up claw_release */ CLAW_DBF_TEXT(4, trace, "stop"); return; case CLAW_START_HALT_IO: /* HALT_IO issued by claw_open */ if (!((p_ch->irb->scsw.cmd.stctl & SCSW_STCTL_SEC_STATUS) || (p_ch->irb->scsw.cmd.stctl == SCSW_STCTL_STATUS_PEND) || (p_ch->irb->scsw.cmd.stctl == (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND)))) { CLAW_DBF_TEXT(4, trace, "haltio"); return; } if (p_ch->flag == CLAW_READ) { p_ch->claw_state = CLAW_START_READ; wake_up(&p_ch->wait); /* wake claw_open (READ)*/ } else if (p_ch->flag == CLAW_WRITE) { p_ch->claw_state = CLAW_START_WRITE; /* send SYSTEM_VALIDATE */ claw_strt_read(dev, LOCK_NO); claw_send_control(dev, SYSTEM_VALIDATE_REQUEST, 0, 0, 0, p_env->host_name, p_env->adapter_name); } else { dev_warn(&cdev->dev, "The CLAW device received" " an unexpected IRQ, " "c-%02x d-%02x\n", irb->scsw.cmd.cstat, irb->scsw.cmd.dstat); return; } CLAW_DBF_TEXT(4, trace, "haltio"); return; case CLAW_START_READ: CLAW_DBF_TEXT(4, trace, "ReadIRQ"); if (p_ch->irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) { clear_bit(0, (void *)&p_ch->IO_active); if ((p_ch->irb->ecw[0] & 0x41) == 0x41 || (p_ch->irb->ecw[0] & 0x40) == 0x40 || (p_ch->irb->ecw[0]) == 0) { privptr->stats.rx_errors++; dev_info(&cdev->dev, "%s: Restart is required after remote " "side recovers \n", dev->name); } CLAW_DBF_TEXT(4, trace, "notrdy"); return; } if ((p_ch->irb->scsw.cmd.cstat & SCHN_STAT_PCI) && (p_ch->irb->scsw.cmd.dstat == 0)) { if (test_and_set_bit(CLAW_BH_ACTIVE, (void *)&p_ch->flag_a) == 0) tasklet_schedule(&p_ch->tasklet); else CLAW_DBF_TEXT(4, trace, "PCINoBH"); CLAW_DBF_TEXT(4, trace, "PCI_read"); return; } if (!((p_ch->irb->scsw.cmd.stctl & SCSW_STCTL_SEC_STATUS) || (p_ch->irb->scsw.cmd.stctl == SCSW_STCTL_STATUS_PEND) || (p_ch->irb->scsw.cmd.stctl == (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND)))) { CLAW_DBF_TEXT(4, trace, "SPend_rd"); return; } clear_bit(0, (void *)&p_ch->IO_active); claw_clearbit_busy(TB_RETRY, dev); if (test_and_set_bit(CLAW_BH_ACTIVE, (void *)&p_ch->flag_a) == 0) tasklet_schedule(&p_ch->tasklet); else CLAW_DBF_TEXT(4, trace, "RdBHAct"); CLAW_DBF_TEXT(4, trace, "RdIRQXit"); return; case CLAW_START_WRITE: if (p_ch->irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) { dev_info(&cdev->dev, "%s: Unit Check Occured in " "write channel\n", dev->name); clear_bit(0, (void *)&p_ch->IO_active); if (p_ch->irb->ecw[0] & 0x80) { dev_info(&cdev->dev, "%s: Resetting Event " "occurred:\n", dev->name); init_timer(&p_ch->timer); p_ch->timer.function = (void *)claw_write_retry; p_ch->timer.data = (unsigned long)p_ch; p_ch->timer.expires = jiffies + 10*HZ; add_timer(&p_ch->timer); dev_info(&cdev->dev, "%s: write connection " "restarting\n", dev->name); } CLAW_DBF_TEXT(4, trace, "rstrtwrt"); return; } if (p_ch->irb->scsw.cmd.dstat & DEV_STAT_UNIT_EXCEP) { clear_bit(0, (void *)&p_ch->IO_active); dev_info(&cdev->dev, "%s: Unit Exception " "occurred in write channel\n", dev->name); } if (!((p_ch->irb->scsw.cmd.stctl & SCSW_STCTL_SEC_STATUS) || (p_ch->irb->scsw.cmd.stctl == SCSW_STCTL_STATUS_PEND) || (p_ch->irb->scsw.cmd.stctl == (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND)))) { CLAW_DBF_TEXT(4, trace, "writeUE"); return; } clear_bit(0, (void *)&p_ch->IO_active); if (claw_test_and_setbit_busy(TB_TX, dev) == 0) { claw_write_next(p_ch); claw_clearbit_busy(TB_TX, dev); claw_clear_busy(dev); } p_ch_r = (struct chbk *)&privptr->channel[READ]; if (test_and_set_bit(CLAW_BH_ACTIVE, (void *)&p_ch_r->flag_a) == 0) tasklet_schedule(&p_ch_r->tasklet); CLAW_DBF_TEXT(4, trace, "StWtExit"); return; default: dev_warn(&cdev->dev, "The CLAW device for %s received an unexpected IRQ\n", dev->name); CLAW_DBF_TEXT(2, trace, "badIRQ"); return; } } /* end of claw_irq_handler */ /*-------------------------------------------------------------------* * claw_irq_tasklet * * * *--------------------------------------------------------------------*/ static void claw_irq_tasklet ( unsigned long data ) { struct chbk * p_ch; struct net_device *dev; struct claw_privbk * privptr; p_ch = (struct chbk *) data; dev = (struct net_device *)p_ch->ndev; CLAW_DBF_TEXT(4, trace, "IRQtask"); privptr = (struct claw_privbk *)dev->ml_priv; unpack_read(dev); clear_bit(CLAW_BH_ACTIVE, (void *)&p_ch->flag_a); CLAW_DBF_TEXT(4, trace, "TskletXt"); return; } /* end of claw_irq_bh */ /*-------------------------------------------------------------------* * claw_release * * * *--------------------------------------------------------------------*/ static int claw_release(struct net_device *dev) { int rc; int i; unsigned long saveflags; unsigned long parm; struct claw_privbk *privptr; DECLARE_WAITQUEUE(wait, current); struct ccwbk* p_this_ccw; struct ccwbk* p_buf; if (!dev) return 0; privptr = (struct claw_privbk *)dev->ml_priv; if (!privptr) return 0; CLAW_DBF_TEXT(4, trace, "release"); privptr->release_pend=1; claw_setbit_busy(TB_STOP,dev); for ( i = 1; i >=0 ; i--) { spin_lock_irqsave( get_ccwdev_lock(privptr->channel[i].cdev), saveflags); /* del_timer(&privptr->channel[READ].timer); */ privptr->channel[i].claw_state = CLAW_STOP; privptr->channel[i].IO_active = 0; parm = (unsigned long) &privptr->channel[i]; if (i == WRITE) claw_purge_skb_queue( &privptr->channel[WRITE].collect_queue); rc = ccw_device_halt (privptr->channel[i].cdev, parm); if (privptr->system_validate_comp==0x00) /* never opened? */ init_waitqueue_head(&privptr->channel[i].wait); add_wait_queue(&privptr->channel[i].wait, &wait); set_current_state(TASK_INTERRUPTIBLE); spin_unlock_irqrestore( get_ccwdev_lock(privptr->channel[i].cdev), saveflags); schedule(); set_current_state(TASK_RUNNING); remove_wait_queue(&privptr->channel[i].wait, &wait); if (rc != 0) { ccw_check_return_code(privptr->channel[i].cdev, rc); } } if (privptr->pk_skb != NULL) { dev_kfree_skb_any(privptr->pk_skb); privptr->pk_skb = NULL; } if(privptr->buffs_alloc != 1) { CLAW_DBF_TEXT(4, trace, "none2fre"); return 0; } CLAW_DBF_TEXT(4, trace, "freebufs"); if (privptr->p_buff_ccw != NULL) { free_pages((unsigned long)privptr->p_buff_ccw, (int)pages_to_order_of_mag(privptr->p_buff_ccw_num)); } CLAW_DBF_TEXT(4, trace, "freeread"); if (privptr->p_env->read_size < PAGE_SIZE) { if (privptr->p_buff_read != NULL) { free_pages((unsigned long)privptr->p_buff_read, (int)pages_to_order_of_mag(privptr->p_buff_read_num)); } } else { p_buf=privptr->p_read_active_first; while (p_buf!=NULL) { free_pages((unsigned long)p_buf->p_buffer, (int)pages_to_order_of_mag( privptr->p_buff_pages_perread )); p_buf=p_buf->next; } } CLAW_DBF_TEXT(4, trace, "freewrit"); if (privptr->p_env->write_size < PAGE_SIZE ) { free_pages((unsigned long)privptr->p_buff_write, (int)pages_to_order_of_mag(privptr->p_buff_write_num)); } else { p_buf=privptr->p_write_active_first; while (p_buf!=NULL) { free_pages((unsigned long)p_buf->p_buffer, (int)pages_to_order_of_mag( privptr->p_buff_pages_perwrite )); p_buf=p_buf->next; } } CLAW_DBF_TEXT(4, trace, "clearptr"); privptr->buffs_alloc = 0; privptr->p_buff_ccw=NULL; privptr->p_buff_read=NULL; privptr->p_buff_write=NULL; privptr->system_validate_comp=0; privptr->release_pend=0; /* Remove any writes that were pending and reset all reads */ p_this_ccw=privptr->p_read_active_first; while (p_this_ccw!=NULL) { p_this_ccw->header.length=0xffff; p_this_ccw->header.opcode=0xff; p_this_ccw->header.flag=0x00; p_this_ccw=p_this_ccw->next; } while (privptr->p_write_active_first!=NULL) { p_this_ccw=privptr->p_write_active_first; p_this_ccw->header.flag=CLAW_PENDING; privptr->p_write_active_first=p_this_ccw->next; p_this_ccw->next=privptr->p_write_free_chain; privptr->p_write_free_chain=p_this_ccw; ++privptr->write_free_count; } privptr->p_write_active_last=NULL; privptr->mtc_logical_link = -1; privptr->mtc_skipping = 1; privptr->mtc_offset=0; if (((privptr->channel[READ].last_dstat | privptr->channel[WRITE].last_dstat) & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END)) != 0x00) { dev_warn(&privptr->channel[READ].cdev->dev, "Deactivating %s completed with incorrect" " subchannel status " "(read %02x, write %02x)\n", dev->name, privptr->channel[READ].last_dstat, privptr->channel[WRITE].last_dstat); CLAW_DBF_TEXT(2, trace, "badclose"); } CLAW_DBF_TEXT(4, trace, "rlsexit"); return 0; } /* end of claw_release */ /*-------------------------------------------------------------------* * claw_write_retry * * * *--------------------------------------------------------------------*/ static void claw_write_retry ( struct chbk *p_ch ) { struct net_device *dev=p_ch->ndev; CLAW_DBF_TEXT(4, trace, "w_retry"); if (p_ch->claw_state == CLAW_STOP) { return; } claw_strt_out_IO( dev ); CLAW_DBF_TEXT(4, trace, "rtry_xit"); return; } /* end of claw_write_retry */ /*-------------------------------------------------------------------* * claw_write_next * * * *--------------------------------------------------------------------*/ static void claw_write_next ( struct chbk * p_ch ) { struct net_device *dev; struct claw_privbk *privptr=NULL; struct sk_buff *pk_skb; int rc; CLAW_DBF_TEXT(4, trace, "claw_wrt"); if (p_ch->claw_state == CLAW_STOP) return; dev = (struct net_device *) p_ch->ndev; privptr = (struct claw_privbk *) dev->ml_priv; claw_free_wrt_buf( dev ); if ((privptr->write_free_count > 0) && !skb_queue_empty(&p_ch->collect_queue)) { pk_skb = claw_pack_skb(privptr); while (pk_skb != NULL) { rc = claw_hw_tx( pk_skb, dev,1); if (privptr->write_free_count > 0) { pk_skb = claw_pack_skb(privptr); } else pk_skb = NULL; } } if (privptr->p_write_active_first!=NULL) { claw_strt_out_IO(dev); } return; } /* end of claw_write_next */ /*-------------------------------------------------------------------* * * * claw_timer * *--------------------------------------------------------------------*/ static void claw_timer ( struct chbk * p_ch ) { CLAW_DBF_TEXT(4, trace, "timer"); p_ch->flag |= CLAW_TIMER; wake_up(&p_ch->wait); return; } /* end of claw_timer */ /* * * functions */ /*-------------------------------------------------------------------* * * * pages_to_order_of_mag * * * * takes a number of pages from 1 to 512 and returns the * * log(num_pages)/log(2) get_free_pages() needs a base 2 order * * of magnitude get_free_pages() has an upper order of 9 * *--------------------------------------------------------------------*/ static int pages_to_order_of_mag(int num_of_pages) { int order_of_mag=1; /* assume 2 pages */ int nump; CLAW_DBF_TEXT_(5, trace, "pages%d", num_of_pages); if (num_of_pages == 1) {return 0; } /* magnitude of 0 = 1 page */ /* 512 pages = 2Meg on 4k page systems */ if (num_of_pages >= 512) {return 9; } /* we have two or more pages order is at least 1 */ for (nump=2 ;nump <= 512;nump*=2) { if (num_of_pages <= nump) break; order_of_mag +=1; } if (order_of_mag > 9) { order_of_mag = 9; } /* I know it's paranoid */ CLAW_DBF_TEXT_(5, trace, "mag%d", order_of_mag); return order_of_mag; } /*-------------------------------------------------------------------* * * * add_claw_reads * * * *--------------------------------------------------------------------*/ static int add_claw_reads(struct net_device *dev, struct ccwbk* p_first, struct ccwbk* p_last) { struct claw_privbk *privptr; struct ccw1 temp_ccw; struct endccw * p_end; CLAW_DBF_TEXT(4, trace, "addreads"); privptr = dev->ml_priv; p_end = privptr->p_end_ccw; /* first CCW and last CCW contains a new set of read channel programs * to apend the running channel programs */ if ( p_first==NULL) { CLAW_DBF_TEXT(4, trace, "addexit"); return 0; } /* set up ending CCW sequence for this segment */ if (p_end->read1) { p_end->read1=0x00; /* second ending CCW is now active */ /* reset ending CCWs and setup TIC CCWs */ p_end->read2_nop2.cmd_code = CCW_CLAW_CMD_READFF; p_end->read2_nop2.flags = CCW_FLAG_SLI | CCW_FLAG_SKIP; p_last->r_TIC_1.cda =(__u32)__pa(&p_end->read2_nop1); p_last->r_TIC_2.cda =(__u32)__pa(&p_end->read2_nop1); p_end->read2_nop2.cda=0; p_end->read2_nop2.count=1; } else { p_end->read1=0x01; /* first ending CCW is now active */ /* reset ending CCWs and setup TIC CCWs */ p_end->read1_nop2.cmd_code = CCW_CLAW_CMD_READFF; p_end->read1_nop2.flags = CCW_FLAG_SLI | CCW_FLAG_SKIP; p_last->r_TIC_1.cda = (__u32)__pa(&p_end->read1_nop1); p_last->r_TIC_2.cda = (__u32)__pa(&p_end->read1_nop1); p_end->read1_nop2.cda=0; p_end->read1_nop2.count=1; } if ( privptr-> p_read_active_first ==NULL ) { privptr->p_read_active_first = p_first; /* set new first */ privptr->p_read_active_last = p_last; /* set new last */ } else { /* set up TIC ccw */ temp_ccw.cda= (__u32)__pa(&p_first->read); temp_ccw.count=0; temp_ccw.flags=0; temp_ccw.cmd_code = CCW_CLAW_CMD_TIC; if (p_end->read1) { /* first set of CCW's is chained to the new read */ /* chain, so the second set is chained to the active chain. */ /* Therefore modify the second set to point to the new */ /* read chain set up TIC CCWs */ /* make sure we update the CCW so channel doesn't fetch it */ /* when it's only half done */ memcpy( &p_end->read2_nop2, &temp_ccw , sizeof(struct ccw1)); privptr->p_read_active_last->r_TIC_1.cda= (__u32)__pa(&p_first->read); privptr->p_read_active_last->r_TIC_2.cda= (__u32)__pa(&p_first->read); } else { /* make sure we update the CCW so channel doesn't */ /* fetch it when it is only half done */ memcpy( &p_end->read1_nop2, &temp_ccw , sizeof(struct ccw1)); privptr->p_read_active_last->r_TIC_1.cda= (__u32)__pa(&p_first->read); privptr->p_read_active_last->r_TIC_2.cda= (__u32)__pa(&p_first->read); } /* chain in new set of blocks */ privptr->p_read_active_last->next = p_first; privptr->p_read_active_last=p_last; } /* end of if ( privptr-> p_read_active_first ==NULL) */ CLAW_DBF_TEXT(4, trace, "addexit"); return 0; } /* end of add_claw_reads */ /*-------------------------------------------------------------------* * ccw_check_return_code * * * *-------------------------------------------------------------------*/ static void ccw_check_return_code(struct ccw_device *cdev, int return_code) { CLAW_DBF_TEXT(4, trace, "ccwret"); if (return_code != 0) { switch (return_code) { case -EBUSY: /* BUSY is a transient state no action needed */ break; case -ENODEV: dev_err(&cdev->dev, "The remote channel adapter is not" " available\n"); break; case -EINVAL: dev_err(&cdev->dev, "The status of the remote channel adapter" " is not valid\n"); break; default: dev_err(&cdev->dev, "The common device layer" " returned error code %d\n", return_code); } } CLAW_DBF_TEXT(4, trace, "ccwret"); } /* end of ccw_check_return_code */ /*-------------------------------------------------------------------* * ccw_check_unit_check * *--------------------------------------------------------------------*/ static void ccw_check_unit_check(struct chbk * p_ch, unsigned char sense ) { struct net_device *ndev = p_ch->ndev; struct device *dev = &p_ch->cdev->dev; CLAW_DBF_TEXT(4, trace, "unitchek"); dev_warn(dev, "The communication peer of %s disconnected\n", ndev->name); if (sense & 0x40) { if (sense & 0x01) { dev_warn(dev, "The remote channel adapter for" " %s has been reset\n", ndev->name); } } else if (sense & 0x20) { if (sense & 0x04) { dev_warn(dev, "A data streaming timeout occurred" " for %s\n", ndev->name); } else if (sense & 0x10) { dev_warn(dev, "The remote channel adapter for %s" " is faulty\n", ndev->name); } else { dev_warn(dev, "A data transfer parity error occurred" " for %s\n", ndev->name); } } else if (sense & 0x10) { dev_warn(dev, "A read data parity error occurred" " for %s\n", ndev->name); } } /* end of ccw_check_unit_check */ /*-------------------------------------------------------------------* * find_link * *--------------------------------------------------------------------*/ static int find_link(struct net_device *dev, char *host_name, char *ws_name ) { struct claw_privbk *privptr; struct claw_env *p_env; int rc=0; CLAW_DBF_TEXT(2, setup, "findlink"); privptr = dev->ml_priv; p_env=privptr->p_env; switch (p_env->packing) { case PACKING_ASK: if ((memcmp(WS_APPL_NAME_PACKED, host_name, 8)!=0) || (memcmp(WS_APPL_NAME_PACKED, ws_name, 8)!=0 )) rc = EINVAL; break; case DO_PACKED: case PACK_SEND: if ((memcmp(WS_APPL_NAME_IP_NAME, host_name, 8)!=0) || (memcmp(WS_APPL_NAME_IP_NAME, ws_name, 8)!=0 )) rc = EINVAL; break; default: if ((memcmp(HOST_APPL_NAME, host_name, 8)!=0) || (memcmp(p_env->api_type , ws_name, 8)!=0)) rc = EINVAL; break; } return rc; } /* end of find_link */ /*-------------------------------------------------------------------* * claw_hw_tx * * * * * *-------------------------------------------------------------------*/ static int claw_hw_tx(struct sk_buff *skb, struct net_device *dev, long linkid) { int rc=0; struct claw_privbk *privptr; struct ccwbk *p_this_ccw; struct ccwbk *p_first_ccw; struct ccwbk *p_last_ccw; __u32 numBuffers; signed long len_of_data; unsigned long bytesInThisBuffer; unsigned char *pDataAddress; struct endccw *pEnd; struct ccw1 tempCCW; struct chbk *p_ch; struct claw_env *p_env; int lock; struct clawph *pk_head; struct chbk *ch; CLAW_DBF_TEXT(4, trace, "hw_tx"); privptr = (struct claw_privbk *)(dev->ml_priv); p_ch=(struct chbk *)&privptr->channel[WRITE]; p_env =privptr->p_env; claw_free_wrt_buf(dev); /* Clean up free chain if posible */ /* scan the write queue to free any completed write packets */ p_first_ccw=NULL; p_last_ccw=NULL; if ((p_env->packing >= PACK_SEND) && (skb->cb[1] != 'P')) { skb_push(skb,sizeof(struct clawph)); pk_head=(struct clawph *)skb->data; pk_head->len=skb->len-sizeof(struct clawph); if (pk_head->len%4) { pk_head->len+= 4-(pk_head->len%4); skb_pad(skb,4-(pk_head->len%4)); skb_put(skb,4-(pk_head->len%4)); } if (p_env->packing == DO_PACKED) pk_head->link_num = linkid; else pk_head->link_num = 0; pk_head->flag = 0x00; skb_pad(skb,4); skb->cb[1] = 'P'; } if (linkid == 0) { if (claw_check_busy(dev)) { if (privptr->write_free_count!=0) { claw_clear_busy(dev); } else { claw_strt_out_IO(dev ); claw_free_wrt_buf( dev ); if (privptr->write_free_count==0) { ch = &privptr->channel[WRITE]; atomic_inc(&skb->users); skb_queue_tail(&ch->collect_queue, skb); goto Done; } else { claw_clear_busy(dev); } } } /* tx lock */ if (claw_test_and_setbit_busy(TB_TX,dev)) { /* set to busy */ ch = &privptr->channel[WRITE]; atomic_inc(&skb->users); skb_queue_tail(&ch->collect_queue, skb); claw_strt_out_IO(dev ); rc=-EBUSY; goto Done2; } } /* See how many write buffers are required to hold this data */ numBuffers = DIV_ROUND_UP(skb->len, privptr->p_env->write_size); /* If that number of buffers isn't available, give up for now */ if (privptr->write_free_count < numBuffers || privptr->p_write_free_chain == NULL ) { claw_setbit_busy(TB_NOBUFFER,dev); ch = &privptr->channel[WRITE]; atomic_inc(&skb->users); skb_queue_tail(&ch->collect_queue, skb); CLAW_DBF_TEXT(2, trace, "clawbusy"); goto Done2; } pDataAddress=skb->data; len_of_data=skb->len; while (len_of_data > 0) { p_this_ccw=privptr->p_write_free_chain; /* get a block */ if (p_this_ccw == NULL) { /* lost the race */ ch = &privptr->channel[WRITE]; atomic_inc(&skb->users); skb_queue_tail(&ch->collect_queue, skb); goto Done2; } privptr->p_write_free_chain=p_this_ccw->next; p_this_ccw->next=NULL; --privptr->write_free_count; /* -1 */ if (len_of_data >= privptr->p_env->write_size) bytesInThisBuffer = privptr->p_env->write_size; else bytesInThisBuffer = len_of_data; memcpy( p_this_ccw->p_buffer,pDataAddress, bytesInThisBuffer); len_of_data-=bytesInThisBuffer; pDataAddress+=(unsigned long)bytesInThisBuffer; /* setup write CCW */ p_this_ccw->write.cmd_code = (linkid * 8) +1; if (len_of_data>0) { p_this_ccw->write.cmd_code+=MORE_to_COME_FLAG; } p_this_ccw->write.count=bytesInThisBuffer; /* now add to end of this chain */ if (p_first_ccw==NULL) { p_first_ccw=p_this_ccw; } if (p_last_ccw!=NULL) { p_last_ccw->next=p_this_ccw; /* set up TIC ccws */ p_last_ccw->w_TIC_1.cda= (__u32)__pa(&p_this_ccw->write); } p_last_ccw=p_this_ccw; /* save new last block */ } /* FirstCCW and LastCCW now contain a new set of write channel * programs to append to the running channel program */ if (p_first_ccw!=NULL) { /* setup ending ccw sequence for this segment */ pEnd=privptr->p_end_ccw; if (pEnd->write1) { pEnd->write1=0x00; /* second end ccw is now active */ /* set up Tic CCWs */ p_last_ccw->w_TIC_1.cda= (__u32)__pa(&pEnd->write2_nop1); pEnd->write2_nop2.cmd_code = CCW_CLAW_CMD_READFF; pEnd->write2_nop2.flags = CCW_FLAG_SLI | CCW_FLAG_SKIP; pEnd->write2_nop2.cda=0; pEnd->write2_nop2.count=1; } else { /* end of if (pEnd->write1)*/ pEnd->write1=0x01; /* first end ccw is now active */ /* set up Tic CCWs */ p_last_ccw->w_TIC_1.cda= (__u32)__pa(&pEnd->write1_nop1); pEnd->write1_nop2.cmd_code = CCW_CLAW_CMD_READFF; pEnd->write1_nop2.flags = CCW_FLAG_SLI | CCW_FLAG_SKIP; pEnd->write1_nop2.cda=0; pEnd->write1_nop2.count=1; } /* end if if (pEnd->write1) */ if (privptr->p_write_active_first==NULL ) { privptr->p_write_active_first=p_first_ccw; privptr->p_write_active_last=p_last_ccw; } else { /* set up Tic CCWs */ tempCCW.cda=(__u32)__pa(&p_first_ccw->write); tempCCW.count=0; tempCCW.flags=0; tempCCW.cmd_code=CCW_CLAW_CMD_TIC; if (pEnd->write1) { /* * first set of ending CCW's is chained to the new write * chain, so the second set is chained to the active chain * Therefore modify the second set to point the new write chain. * make sure we update the CCW atomically * so channel does not fetch it when it's only half done */ memcpy( &pEnd->write2_nop2, &tempCCW , sizeof(struct ccw1)); privptr->p_write_active_last->w_TIC_1.cda= (__u32)__pa(&p_first_ccw->write); } else { /*make sure we update the CCW atomically *so channel does not fetch it when it's only half done */ memcpy(&pEnd->write1_nop2, &tempCCW , sizeof(struct ccw1)); privptr->p_write_active_last->w_TIC_1.cda= (__u32)__pa(&p_first_ccw->write); } /* end if if (pEnd->write1) */ privptr->p_write_active_last->next=p_first_ccw; privptr->p_write_active_last=p_last_ccw; } } /* endif (p_first_ccw!=NULL) */ dev_kfree_skb_any(skb); if (linkid==0) { lock=LOCK_NO; } else { lock=LOCK_YES; } claw_strt_out_IO(dev ); /* if write free count is zero , set NOBUFFER */ if (privptr->write_free_count==0) { claw_setbit_busy(TB_NOBUFFER,dev); } Done2: claw_clearbit_busy(TB_TX,dev); Done: return(rc); } /* end of claw_hw_tx */ /*-------------------------------------------------------------------* * * * init_ccw_bk * * * *--------------------------------------------------------------------*/ static int init_ccw_bk(struct net_device *dev) { __u32 ccw_blocks_required; __u32 ccw_blocks_perpage; __u32 ccw_pages_required; __u32 claw_reads_perpage=1; __u32 claw_read_pages; __u32 claw_writes_perpage=1; __u32 claw_write_pages; void *p_buff=NULL; struct ccwbk*p_free_chain; struct ccwbk*p_buf; struct ccwbk*p_last_CCWB; struct ccwbk*p_first_CCWB; struct endccw *p_endccw=NULL; addr_t real_address; struct claw_privbk *privptr = dev->ml_priv; struct clawh *pClawH=NULL; addr_t real_TIC_address; int i,j; CLAW_DBF_TEXT(4, trace, "init_ccw"); /* initialize statistics field */ privptr->active_link_ID=0; /* initialize ccwbk pointers */ privptr->p_write_free_chain=NULL; /* pointer to free ccw chain*/ privptr->p_write_active_first=NULL; /* pointer to the first write ccw*/ privptr->p_write_active_last=NULL; /* pointer to the last write ccw*/ privptr->p_read_active_first=NULL; /* pointer to the first read ccw*/ privptr->p_read_active_last=NULL; /* pointer to the last read ccw */ privptr->p_end_ccw=NULL; /* pointer to ending ccw */ privptr->p_claw_signal_blk=NULL; /* pointer to signal block */ privptr->buffs_alloc = 0; memset(&privptr->end_ccw, 0x00, sizeof(struct endccw)); memset(&privptr->ctl_bk, 0x00, sizeof(struct clawctl)); /* initialize free write ccwbk counter */ privptr->write_free_count=0; /* number of free bufs on write chain */ p_last_CCWB = NULL; p_first_CCWB= NULL; /* * We need 1 CCW block for each read buffer, 1 for each * write buffer, plus 1 for ClawSignalBlock */ ccw_blocks_required = privptr->p_env->read_buffers+privptr->p_env->write_buffers+1; /* * compute number of CCW blocks that will fit in a page */ ccw_blocks_perpage= PAGE_SIZE / CCWBK_SIZE; ccw_pages_required= DIV_ROUND_UP(ccw_blocks_required, ccw_blocks_perpage); /* * read and write sizes are set by 2 constants in claw.h * 4k and 32k. Unpacked values other than 4k are not going to * provide good performance. With packing buffers support 32k * buffers are used. */ if (privptr->p_env->read_size < PAGE_SIZE) { claw_reads_perpage = PAGE_SIZE / privptr->p_env->read_size; claw_read_pages = DIV_ROUND_UP(privptr->p_env->read_buffers, claw_reads_perpage); } else { /* > or equal */ privptr->p_buff_pages_perread = DIV_ROUND_UP(privptr->p_env->read_size, PAGE_SIZE); claw_read_pages = privptr->p_env->read_buffers * privptr->p_buff_pages_perread; } if (privptr->p_env->write_size < PAGE_SIZE) { claw_writes_perpage = PAGE_SIZE / privptr->p_env->write_size; claw_write_pages = DIV_ROUND_UP(privptr->p_env->write_buffers, claw_writes_perpage); } else { /* > or equal */ privptr->p_buff_pages_perwrite = DIV_ROUND_UP(privptr->p_env->read_size, PAGE_SIZE); claw_write_pages = privptr->p_env->write_buffers * privptr->p_buff_pages_perwrite; } /* * allocate ccw_pages_required */ if (privptr->p_buff_ccw==NULL) { privptr->p_buff_ccw= (void *)__get_free_pages(__GFP_DMA, (int)pages_to_order_of_mag(ccw_pages_required )); if (privptr->p_buff_ccw==NULL) { return -ENOMEM; } privptr->p_buff_ccw_num=ccw_pages_required; } memset(privptr->p_buff_ccw, 0x00, privptr->p_buff_ccw_num * PAGE_SIZE); /* * obtain ending ccw block address * */ privptr->p_end_ccw = (struct endccw *)&privptr->end_ccw; real_address = (__u32)__pa(privptr->p_end_ccw); /* Initialize ending CCW block */ p_endccw=privptr->p_end_ccw; p_endccw->real=real_address; p_endccw->write1=0x00; p_endccw->read1=0x00; /* write1_nop1 */ p_endccw->write1_nop1.cmd_code = CCW_CLAW_CMD_NOP; p_endccw->write1_nop1.flags = CCW_FLAG_SLI | CCW_FLAG_CC; p_endccw->write1_nop1.count = 1; p_endccw->write1_nop1.cda = 0; /* write1_nop2 */ p_endccw->write1_nop2.cmd_code = CCW_CLAW_CMD_READFF; p_endccw->write1_nop2.flags = CCW_FLAG_SLI | CCW_FLAG_SKIP; p_endccw->write1_nop2.count = 1; p_endccw->write1_nop2.cda = 0; /* write2_nop1 */ p_endccw->write2_nop1.cmd_code = CCW_CLAW_CMD_NOP; p_endccw->write2_nop1.flags = CCW_FLAG_SLI | CCW_FLAG_CC; p_endccw->write2_nop1.count = 1; p_endccw->write2_nop1.cda = 0; /* write2_nop2 */ p_endccw->write2_nop2.cmd_code = CCW_CLAW_CMD_READFF; p_endccw->write2_nop2.flags = CCW_FLAG_SLI | CCW_FLAG_SKIP; p_endccw->write2_nop2.count = 1; p_endccw->write2_nop2.cda = 0; /* read1_nop1 */ p_endccw->read1_nop1.cmd_code = CCW_CLAW_CMD_NOP; p_endccw->read1_nop1.flags = CCW_FLAG_SLI | CCW_FLAG_CC; p_endccw->read1_nop1.count = 1; p_endccw->read1_nop1.cda = 0; /* read1_nop2 */ p_endccw->read1_nop2.cmd_code = CCW_CLAW_CMD_READFF; p_endccw->read1_nop2.flags = CCW_FLAG_SLI | CCW_FLAG_SKIP; p_endccw->read1_nop2.count = 1; p_endccw->read1_nop2.cda = 0; /* read2_nop1 */ p_endccw->read2_nop1.cmd_code = CCW_CLAW_CMD_NOP; p_endccw->read2_nop1.flags = CCW_FLAG_SLI | CCW_FLAG_CC; p_endccw->read2_nop1.count = 1; p_endccw->read2_nop1.cda = 0; /* read2_nop2 */ p_endccw->read2_nop2.cmd_code = CCW_CLAW_CMD_READFF; p_endccw->read2_nop2.flags = CCW_FLAG_SLI | CCW_FLAG_SKIP; p_endccw->read2_nop2.count = 1; p_endccw->read2_nop2.cda = 0; /* * Build a chain of CCWs * */ p_buff=privptr->p_buff_ccw; p_free_chain=NULL; for (i=0 ; i < ccw_pages_required; i++ ) { real_address = (__u32)__pa(p_buff); p_buf=p_buff; for (j=0 ; j < ccw_blocks_perpage ; j++) { p_buf->next = p_free_chain; p_free_chain = p_buf; p_buf->real=(__u32)__pa(p_buf); ++p_buf; } p_buff+=PAGE_SIZE; } /* * Initialize ClawSignalBlock * */ if (privptr->p_claw_signal_blk==NULL) { privptr->p_claw_signal_blk=p_free_chain; p_free_chain=p_free_chain->next; pClawH=(struct clawh *)privptr->p_claw_signal_blk; pClawH->length=0xffff; pClawH->opcode=0xff; pClawH->flag=CLAW_BUSY; } /* * allocate write_pages_required and add to free chain */ if (privptr->p_buff_write==NULL) { if (privptr->p_env->write_size < PAGE_SIZE) { privptr->p_buff_write= (void *)__get_free_pages(__GFP_DMA, (int)pages_to_order_of_mag(claw_write_pages )); if (privptr->p_buff_write==NULL) { privptr->p_buff_ccw=NULL; return -ENOMEM; } /* * Build CLAW write free chain * */ memset(privptr->p_buff_write, 0x00, ccw_pages_required * PAGE_SIZE); privptr->p_write_free_chain=NULL; p_buff=privptr->p_buff_write; for (i=0 ; i< privptr->p_env->write_buffers ; i++) { p_buf = p_free_chain; /* get a CCW */ p_free_chain = p_buf->next; p_buf->next =privptr->p_write_free_chain; privptr->p_write_free_chain = p_buf; p_buf-> p_buffer = (struct clawbuf *)p_buff; p_buf-> write.cda = (__u32)__pa(p_buff); p_buf-> write.flags = CCW_FLAG_SLI | CCW_FLAG_CC; p_buf-> w_read_FF.cmd_code = CCW_CLAW_CMD_READFF; p_buf-> w_read_FF.flags = CCW_FLAG_SLI | CCW_FLAG_CC; p_buf-> w_read_FF.count = 1; p_buf-> w_read_FF.cda = (__u32)__pa(&p_buf-> header.flag); p_buf-> w_TIC_1.cmd_code = CCW_CLAW_CMD_TIC; p_buf-> w_TIC_1.flags = 0; p_buf-> w_TIC_1.count = 0; if (((unsigned long)p_buff + privptr->p_env->write_size) >= ((unsigned long)(p_buff+2* (privptr->p_env->write_size) - 1) & PAGE_MASK)) { p_buff = p_buff+privptr->p_env->write_size; } } } else /* Buffers are => PAGE_SIZE. 1 buff per get_free_pages */ { privptr->p_write_free_chain=NULL; for (i = 0; i< privptr->p_env->write_buffers ; i++) { p_buff=(void *)__get_free_pages(__GFP_DMA, (int)pages_to_order_of_mag( privptr->p_buff_pages_perwrite) ); if (p_buff==NULL) { free_pages((unsigned long)privptr->p_buff_ccw, (int)pages_to_order_of_mag( privptr->p_buff_ccw_num)); privptr->p_buff_ccw=NULL; p_buf=privptr->p_buff_write; while (p_buf!=NULL) { free_pages((unsigned long) p_buf->p_buffer, (int)pages_to_order_of_mag( privptr->p_buff_pages_perwrite)); p_buf=p_buf->next; } return -ENOMEM; } /* Error on get_pages */ memset(p_buff, 0x00, privptr->p_env->write_size ); p_buf = p_free_chain; p_free_chain = p_buf->next; p_buf->next = privptr->p_write_free_chain; privptr->p_write_free_chain = p_buf; privptr->p_buff_write = p_buf; p_buf->p_buffer=(struct clawbuf *)p_buff; p_buf-> write.cda = (__u32)__pa(p_buff); p_buf-> write.flags = CCW_FLAG_SLI | CCW_FLAG_CC; p_buf-> w_read_FF.cmd_code = CCW_CLAW_CMD_READFF; p_buf-> w_read_FF.flags = CCW_FLAG_SLI | CCW_FLAG_CC; p_buf-> w_read_FF.count = 1; p_buf-> w_read_FF.cda = (__u32)__pa(&p_buf-> header.flag); p_buf-> w_TIC_1.cmd_code = CCW_CLAW_CMD_TIC; p_buf-> w_TIC_1.flags = 0; p_buf-> w_TIC_1.count = 0; } /* for all write_buffers */ } /* else buffers are PAGE_SIZE or bigger */ } privptr->p_buff_write_num=claw_write_pages; privptr->write_free_count=privptr->p_env->write_buffers; /* * allocate read_pages_required and chain to free chain */ if (privptr->p_buff_read==NULL) { if (privptr->p_env->read_size < PAGE_SIZE) { privptr->p_buff_read= (void *)__get_free_pages(__GFP_DMA, (int)pages_to_order_of_mag(claw_read_pages) ); if (privptr->p_buff_read==NULL) { free_pages((unsigned long)privptr->p_buff_ccw, (int)pages_to_order_of_mag( privptr->p_buff_ccw_num)); /* free the write pages size is < page size */ free_pages((unsigned long)privptr->p_buff_write, (int)pages_to_order_of_mag( privptr->p_buff_write_num)); privptr->p_buff_ccw=NULL; privptr->p_buff_write=NULL; return -ENOMEM; } memset(privptr->p_buff_read, 0x00, claw_read_pages * PAGE_SIZE); privptr->p_buff_read_num=claw_read_pages; /* * Build CLAW read free chain * */ p_buff=privptr->p_buff_read; for (i=0 ; i< privptr->p_env->read_buffers ; i++) { p_buf = p_free_chain; p_free_chain = p_buf->next; if (p_last_CCWB==NULL) { p_buf->next=NULL; real_TIC_address=0; p_last_CCWB=p_buf; } else { p_buf->next=p_first_CCWB; real_TIC_address= (__u32)__pa(&p_first_CCWB -> read ); } p_first_CCWB=p_buf; p_buf->p_buffer=(struct clawbuf *)p_buff; /* initialize read command */ p_buf-> read.cmd_code = CCW_CLAW_CMD_READ; p_buf-> read.cda = (__u32)__pa(p_buff); p_buf-> read.flags = CCW_FLAG_SLI | CCW_FLAG_CC; p_buf-> read.count = privptr->p_env->read_size; /* initialize read_h command */ p_buf-> read_h.cmd_code = CCW_CLAW_CMD_READHEADER; p_buf-> read_h.cda = (__u32)__pa(&(p_buf->header)); p_buf-> read_h.flags = CCW_FLAG_SLI | CCW_FLAG_CC; p_buf-> read_h.count = sizeof(struct clawh); /* initialize Signal command */ p_buf-> signal.cmd_code = CCW_CLAW_CMD_SIGNAL_SMOD; p_buf-> signal.cda = (__u32)__pa(&(pClawH->flag)); p_buf-> signal.flags = CCW_FLAG_SLI | CCW_FLAG_CC; p_buf-> signal.count = 1; /* initialize r_TIC_1 command */ p_buf-> r_TIC_1.cmd_code = CCW_CLAW_CMD_TIC; p_buf-> r_TIC_1.cda = (__u32)real_TIC_address; p_buf-> r_TIC_1.flags = 0; p_buf-> r_TIC_1.count = 0; /* initialize r_read_FF command */ p_buf-> r_read_FF.cmd_code = CCW_CLAW_CMD_READFF; p_buf-> r_read_FF.cda = (__u32)__pa(&(pClawH->flag)); p_buf-> r_read_FF.flags = CCW_FLAG_SLI | CCW_FLAG_CC | CCW_FLAG_PCI; p_buf-> r_read_FF.count = 1; /* initialize r_TIC_2 */ memcpy(&p_buf->r_TIC_2, &p_buf->r_TIC_1, sizeof(struct ccw1)); /* initialize Header */ p_buf->header.length=0xffff; p_buf->header.opcode=0xff; p_buf->header.flag=CLAW_PENDING; if (((unsigned long)p_buff+privptr->p_env->read_size) >= ((unsigned long)(p_buff+2*(privptr->p_env->read_size) -1) & PAGE_MASK)) { p_buff= p_buff+privptr->p_env->read_size; } else { p_buff= (void *)((unsigned long) (p_buff+2*(privptr->p_env->read_size)-1) & PAGE_MASK) ; } } /* for read_buffers */ } /* read_size < PAGE_SIZE */ else { /* read Size >= PAGE_SIZE */ for (i=0 ; i< privptr->p_env->read_buffers ; i++) { p_buff = (void *)__get_free_pages(__GFP_DMA, (int)pages_to_order_of_mag( privptr->p_buff_pages_perread)); if (p_buff==NULL) { free_pages((unsigned long)privptr->p_buff_ccw, (int)pages_to_order_of_mag(privptr-> p_buff_ccw_num)); /* free the write pages */ p_buf=privptr->p_buff_write; while (p_buf!=NULL) { free_pages( (unsigned long)p_buf->p_buffer, (int)pages_to_order_of_mag( privptr->p_buff_pages_perwrite)); p_buf=p_buf->next; } /* free any read pages already alloc */ p_buf=privptr->p_buff_read; while (p_buf!=NULL) { free_pages( (unsigned long)p_buf->p_buffer, (int)pages_to_order_of_mag( privptr->p_buff_pages_perread)); p_buf=p_buf->next; } privptr->p_buff_ccw=NULL; privptr->p_buff_write=NULL; return -ENOMEM; } memset(p_buff, 0x00, privptr->p_env->read_size); p_buf = p_free_chain; privptr->p_buff_read = p_buf; p_free_chain = p_buf->next; if (p_last_CCWB==NULL) { p_buf->next=NULL; real_TIC_address=0; p_last_CCWB=p_buf; } else { p_buf->next=p_first_CCWB; real_TIC_address= (addr_t)__pa( &p_first_CCWB -> read ); } p_first_CCWB=p_buf; /* save buff address */ p_buf->p_buffer=(struct clawbuf *)p_buff; /* initialize read command */ p_buf-> read.cmd_code = CCW_CLAW_CMD_READ; p_buf-> read.cda = (__u32)__pa(p_buff); p_buf-> read.flags = CCW_FLAG_SLI | CCW_FLAG_CC; p_buf-> read.count = privptr->p_env->read_size; /* initialize read_h command */ p_buf-> read_h.cmd_code = CCW_CLAW_CMD_READHEADER; p_buf-> read_h.cda = (__u32)__pa(&(p_buf->header)); p_buf-> read_h.flags = CCW_FLAG_SLI | CCW_FLAG_CC; p_buf-> read_h.count = sizeof(struct clawh); /* initialize Signal command */ p_buf-> signal.cmd_code = CCW_CLAW_CMD_SIGNAL_SMOD; p_buf-> signal.cda = (__u32)__pa(&(pClawH->flag)); p_buf-> signal.flags = CCW_FLAG_SLI | CCW_FLAG_CC; p_buf-> signal.count = 1; /* initialize r_TIC_1 command */ p_buf-> r_TIC_1.cmd_code = CCW_CLAW_CMD_TIC; p_buf-> r_TIC_1.cda = (__u32)real_TIC_address; p_buf-> r_TIC_1.flags = 0; p_buf-> r_TIC_1.count = 0; /* initialize r_read_FF command */ p_buf-> r_read_FF.cmd_code = CCW_CLAW_CMD_READFF; p_buf-> r_read_FF.cda = (__u32)__pa(&(pClawH->flag)); p_buf-> r_read_FF.flags = CCW_FLAG_SLI | CCW_FLAG_CC | CCW_FLAG_PCI; p_buf-> r_read_FF.count = 1; /* initialize r_TIC_2 */ memcpy(&p_buf->r_TIC_2, &p_buf->r_TIC_1, sizeof(struct ccw1)); /* initialize Header */ p_buf->header.length=0xffff; p_buf->header.opcode=0xff; p_buf->header.flag=CLAW_PENDING; } /* For read_buffers */ } /* read_size >= PAGE_SIZE */ } /* pBuffread = NULL */ add_claw_reads( dev ,p_first_CCWB , p_last_CCWB); privptr->buffs_alloc = 1; return 0; } /* end of init_ccw_bk */ /*-------------------------------------------------------------------* * * * probe_error * * * *--------------------------------------------------------------------*/ static void probe_error( struct ccwgroup_device *cgdev) { struct claw_privbk *privptr; CLAW_DBF_TEXT(4, trace, "proberr"); privptr = dev_get_drvdata(&cgdev->dev); if (privptr != NULL) { dev_set_drvdata(&cgdev->dev, NULL); kfree(privptr->p_env); kfree(privptr->p_mtc_envelope); kfree(privptr); } } /* probe_error */ /*-------------------------------------------------------------------* * claw_process_control * * * * * *--------------------------------------------------------------------*/ static int claw_process_control( struct net_device *dev, struct ccwbk * p_ccw) { struct clawbuf *p_buf; struct clawctl ctlbk; struct clawctl *p_ctlbk; char temp_host_name[8]; char temp_ws_name[8]; struct claw_privbk *privptr; struct claw_env *p_env; struct sysval *p_sysval; struct conncmd *p_connect=NULL; int rc; struct chbk *p_ch = NULL; struct device *tdev; CLAW_DBF_TEXT(2, setup, "clw_cntl"); udelay(1000); /* Wait a ms for the control packets to *catch up to each other */ privptr = dev->ml_priv; p_env=privptr->p_env; tdev = &privptr->channel[READ].cdev->dev; memcpy( &temp_host_name, p_env->host_name, 8); memcpy( &temp_ws_name, p_env->adapter_name , 8); dev_info(tdev, "%s: CLAW device %.8s: " "Received Control Packet\n", dev->name, temp_ws_name); if (privptr->release_pend==1) { return 0; } p_buf=p_ccw->p_buffer; p_ctlbk=&ctlbk; if (p_env->packing == DO_PACKED) { /* packing in progress?*/ memcpy(p_ctlbk, &p_buf->buffer[4], sizeof(struct clawctl)); } else { memcpy(p_ctlbk, p_buf, sizeof(struct clawctl)); } switch (p_ctlbk->command) { case SYSTEM_VALIDATE_REQUEST: if (p_ctlbk->version != CLAW_VERSION_ID) { claw_snd_sys_validate_rsp(dev, p_ctlbk, CLAW_RC_WRONG_VERSION); dev_warn(tdev, "The communication peer of %s" " uses an incorrect API version %d\n", dev->name, p_ctlbk->version); } p_sysval = (struct sysval *)&(p_ctlbk->data); dev_info(tdev, "%s: Recv Sys Validate Request: " "Vers=%d,link_id=%d,Corr=%d,WS name=%.8s," "Host name=%.8s\n", dev->name, p_ctlbk->version, p_ctlbk->linkid, p_ctlbk->correlator, p_sysval->WS_name, p_sysval->host_name); if (memcmp(temp_host_name, p_sysval->host_name, 8)) { claw_snd_sys_validate_rsp(dev, p_ctlbk, CLAW_RC_NAME_MISMATCH); CLAW_DBF_TEXT(2, setup, "HSTBAD"); CLAW_DBF_TEXT_(2, setup, "%s", p_sysval->host_name); CLAW_DBF_TEXT_(2, setup, "%s", temp_host_name); dev_warn(tdev, "Host name %s for %s does not match the" " remote adapter name %s\n", p_sysval->host_name, dev->name, temp_host_name); } if (memcmp(temp_ws_name, p_sysval->WS_name, 8)) { claw_snd_sys_validate_rsp(dev, p_ctlbk, CLAW_RC_NAME_MISMATCH); CLAW_DBF_TEXT(2, setup, "WSNBAD"); CLAW_DBF_TEXT_(2, setup, "%s", p_sysval->WS_name); CLAW_DBF_TEXT_(2, setup, "%s", temp_ws_name); dev_warn(tdev, "Adapter name %s for %s does not match" " the remote host name %s\n", p_sysval->WS_name, dev->name, temp_ws_name); } if ((p_sysval->write_frame_size < p_env->write_size) && (p_env->packing == 0)) { claw_snd_sys_validate_rsp(dev, p_ctlbk, CLAW_RC_HOST_RCV_TOO_SMALL); dev_warn(tdev, "The local write buffer is smaller than the" " remote read buffer\n"); CLAW_DBF_TEXT(2, setup, "wrtszbad"); } if ((p_sysval->read_frame_size < p_env->read_size) && (p_env->packing == 0)) { claw_snd_sys_validate_rsp(dev, p_ctlbk, CLAW_RC_HOST_RCV_TOO_SMALL); dev_warn(tdev, "The local read buffer is smaller than the" " remote write buffer\n"); CLAW_DBF_TEXT(2, setup, "rdsizbad"); } claw_snd_sys_validate_rsp(dev, p_ctlbk, 0); dev_info(tdev, "CLAW device %.8s: System validate" " completed.\n", temp_ws_name); dev_info(tdev, "%s: sys Validate Rsize:%d Wsize:%d\n", dev->name, p_sysval->read_frame_size, p_sysval->write_frame_size); privptr->system_validate_comp = 1; if (strncmp(p_env->api_type, WS_APPL_NAME_PACKED, 6) == 0) p_env->packing = PACKING_ASK; claw_strt_conn_req(dev); break; case SYSTEM_VALIDATE_RESPONSE: p_sysval = (struct sysval *)&(p_ctlbk->data); dev_info(tdev, "Settings for %s validated (version=%d, " "remote device=%d, rc=%d, adapter name=%.8s, " "host name=%.8s)\n", dev->name, p_ctlbk->version, p_ctlbk->correlator, p_ctlbk->rc, p_sysval->WS_name, p_sysval->host_name); switch (p_ctlbk->rc) { case 0: dev_info(tdev, "%s: CLAW device " "%.8s: System validate completed.\n", dev->name, temp_ws_name); if (privptr->system_validate_comp == 0) claw_strt_conn_req(dev); privptr->system_validate_comp = 1; break; case CLAW_RC_NAME_MISMATCH: dev_warn(tdev, "Validating %s failed because of" " a host or adapter name mismatch\n", dev->name); break; case CLAW_RC_WRONG_VERSION: dev_warn(tdev, "Validating %s failed because of a" " version conflict\n", dev->name); break; case CLAW_RC_HOST_RCV_TOO_SMALL: dev_warn(tdev, "Validating %s failed because of a" " frame size conflict\n", dev->name); break; default: dev_warn(tdev, "The communication peer of %s rejected" " the connection\n", dev->name); break; } break; case CONNECTION_REQUEST: p_connect = (struct conncmd *)&(p_ctlbk->data); dev_info(tdev, "%s: Recv Conn Req: Vers=%d,link_id=%d," "Corr=%d,HOST appl=%.8s,WS appl=%.8s\n", dev->name, p_ctlbk->version, p_ctlbk->linkid, p_ctlbk->correlator, p_connect->host_name, p_connect->WS_name); if (privptr->active_link_ID != 0) { claw_snd_disc(dev, p_ctlbk); dev_info(tdev, "%s rejected a connection request" " because it is already active\n", dev->name); } if (p_ctlbk->linkid != 1) { claw_snd_disc(dev, p_ctlbk); dev_info(tdev, "%s rejected a request to open multiple" " connections\n", dev->name); } rc = find_link(dev, p_connect->host_name, p_connect->WS_name); if (rc != 0) { claw_snd_disc(dev, p_ctlbk); dev_info(tdev, "%s rejected a connection request" " because of a type mismatch\n", dev->name); } claw_send_control(dev, CONNECTION_CONFIRM, p_ctlbk->linkid, p_ctlbk->correlator, 0, p_connect->host_name, p_connect->WS_name); if (p_env->packing == PACKING_ASK) { p_env->packing = PACK_SEND; claw_snd_conn_req(dev, 0); } dev_info(tdev, "%s: CLAW device %.8s: Connection " "completed link_id=%d.\n", dev->name, temp_ws_name, p_ctlbk->linkid); privptr->active_link_ID = p_ctlbk->linkid; p_ch = &privptr->channel[WRITE]; wake_up(&p_ch->wait); /* wake up claw_open ( WRITE) */ break; case CONNECTION_RESPONSE: p_connect = (struct conncmd *)&(p_ctlbk->data); dev_info(tdev, "%s: Recv Conn Resp: Vers=%d,link_id=%d," "Corr=%d,RC=%d,Host appl=%.8s, WS appl=%.8s\n", dev->name, p_ctlbk->version, p_ctlbk->linkid, p_ctlbk->correlator, p_ctlbk->rc, p_connect->host_name, p_connect->WS_name); if (p_ctlbk->rc != 0) { dev_warn(tdev, "The communication peer of %s rejected" " a connection request\n", dev->name); return 1; } rc = find_link(dev, p_connect->host_name, p_connect->WS_name); if (rc != 0) { claw_snd_disc(dev, p_ctlbk); dev_warn(tdev, "The communication peer of %s" " rejected a connection " "request because of a type mismatch\n", dev->name); } /* should be until CONNECTION_CONFIRM */ privptr->active_link_ID = -(p_ctlbk->linkid); break; case CONNECTION_CONFIRM: p_connect = (struct conncmd *)&(p_ctlbk->data); dev_info(tdev, "%s: Recv Conn Confirm:Vers=%d,link_id=%d," "Corr=%d,Host appl=%.8s,WS appl=%.8s\n", dev->name, p_ctlbk->version, p_ctlbk->linkid, p_ctlbk->correlator, p_connect->host_name, p_connect->WS_name); if (p_ctlbk->linkid == -(privptr->active_link_ID)) { privptr->active_link_ID = p_ctlbk->linkid; if (p_env->packing > PACKING_ASK) { dev_info(tdev, "%s: Confirmed Now packing\n", dev->name); p_env->packing = DO_PACKED; } p_ch = &privptr->channel[WRITE]; wake_up(&p_ch->wait); } else { dev_warn(tdev, "Activating %s failed because of" " an incorrect link ID=%d\n", dev->name, p_ctlbk->linkid); claw_snd_disc(dev, p_ctlbk); } break; case DISCONNECT: dev_info(tdev, "%s: Disconnect: " "Vers=%d,link_id=%d,Corr=%d\n", dev->name, p_ctlbk->version, p_ctlbk->linkid, p_ctlbk->correlator); if ((p_ctlbk->linkid == 2) && (p_env->packing == PACK_SEND)) { privptr->active_link_ID = 1; p_env->packing = DO_PACKED; } else privptr->active_link_ID = 0; break; case CLAW_ERROR: dev_warn(tdev, "The communication peer of %s failed\n", dev->name); break; default: dev_warn(tdev, "The communication peer of %s sent" " an unknown command code\n", dev->name); break; } return 0; } /* end of claw_process_control */ /*-------------------------------------------------------------------* * claw_send_control * * * *--------------------------------------------------------------------*/ static int claw_send_control(struct net_device *dev, __u8 type, __u8 link, __u8 correlator, __u8 rc, char *local_name, char *remote_name) { struct claw_privbk *privptr; struct clawctl *p_ctl; struct sysval *p_sysval; struct conncmd *p_connect; struct sk_buff *skb; CLAW_DBF_TEXT(2, setup, "sndcntl"); privptr = dev->ml_priv; p_ctl=(struct clawctl *)&privptr->ctl_bk; p_ctl->command=type; p_ctl->version=CLAW_VERSION_ID; p_ctl->linkid=link; p_ctl->correlator=correlator; p_ctl->rc=rc; p_sysval=(struct sysval *)&p_ctl->data; p_connect=(struct conncmd *)&p_ctl->data; switch (p_ctl->command) { case SYSTEM_VALIDATE_REQUEST: case SYSTEM_VALIDATE_RESPONSE: memcpy(&p_sysval->host_name, local_name, 8); memcpy(&p_sysval->WS_name, remote_name, 8); if (privptr->p_env->packing > 0) { p_sysval->read_frame_size = DEF_PACK_BUFSIZE; p_sysval->write_frame_size = DEF_PACK_BUFSIZE; } else { /* how big is the biggest group of packets */ p_sysval->read_frame_size = privptr->p_env->read_size; p_sysval->write_frame_size = privptr->p_env->write_size; } memset(&p_sysval->reserved, 0x00, 4); break; case CONNECTION_REQUEST: case CONNECTION_RESPONSE: case CONNECTION_CONFIRM: case DISCONNECT: memcpy(&p_sysval->host_name, local_name, 8); memcpy(&p_sysval->WS_name, remote_name, 8); if (privptr->p_env->packing > 0) { /* How big is the biggest packet */ p_connect->reserved1[0]=CLAW_FRAME_SIZE; p_connect->reserved1[1]=CLAW_FRAME_SIZE; } else { memset(&p_connect->reserved1, 0x00, 4); memset(&p_connect->reserved2, 0x00, 4); } break; default: break; } /* write Control Record to the device */ skb = dev_alloc_skb(sizeof(struct clawctl)); if (!skb) { return -ENOMEM; } memcpy(skb_put(skb, sizeof(struct clawctl)), p_ctl, sizeof(struct clawctl)); if (privptr->p_env->packing >= PACK_SEND) claw_hw_tx(skb, dev, 1); else claw_hw_tx(skb, dev, 0); return 0; } /* end of claw_send_control */ /*-------------------------------------------------------------------* * claw_snd_conn_req * * * *--------------------------------------------------------------------*/ static int claw_snd_conn_req(struct net_device *dev, __u8 link) { int rc; struct claw_privbk *privptr = dev->ml_priv; struct clawctl *p_ctl; CLAW_DBF_TEXT(2, setup, "snd_conn"); rc = 1; p_ctl=(struct clawctl *)&privptr->ctl_bk; p_ctl->linkid = link; if ( privptr->system_validate_comp==0x00 ) { return rc; } if (privptr->p_env->packing == PACKING_ASK ) rc=claw_send_control(dev, CONNECTION_REQUEST,0,0,0, WS_APPL_NAME_PACKED, WS_APPL_NAME_PACKED); if (privptr->p_env->packing == PACK_SEND) { rc=claw_send_control(dev, CONNECTION_REQUEST,0,0,0, WS_APPL_NAME_IP_NAME, WS_APPL_NAME_IP_NAME); } if (privptr->p_env->packing == 0) rc=claw_send_control(dev, CONNECTION_REQUEST,0,0,0, HOST_APPL_NAME, privptr->p_env->api_type); return rc; } /* end of claw_snd_conn_req */ /*-------------------------------------------------------------------* * claw_snd_disc * * * *--------------------------------------------------------------------*/ static int claw_snd_disc(struct net_device *dev, struct clawctl * p_ctl) { int rc; struct conncmd * p_connect; CLAW_DBF_TEXT(2, setup, "snd_dsc"); p_connect=(struct conncmd *)&p_ctl->data; rc=claw_send_control(dev, DISCONNECT, p_ctl->linkid, p_ctl->correlator, 0, p_connect->host_name, p_connect->WS_name); return rc; } /* end of claw_snd_disc */ /*-------------------------------------------------------------------* * claw_snd_sys_validate_rsp * * * *--------------------------------------------------------------------*/ static int claw_snd_sys_validate_rsp(struct net_device *dev, struct clawctl *p_ctl, __u32 return_code) { struct claw_env * p_env; struct claw_privbk *privptr; int rc; CLAW_DBF_TEXT(2, setup, "chkresp"); privptr = dev->ml_priv; p_env=privptr->p_env; rc=claw_send_control(dev, SYSTEM_VALIDATE_RESPONSE, p_ctl->linkid, p_ctl->correlator, return_code, p_env->host_name, p_env->adapter_name ); return rc; } /* end of claw_snd_sys_validate_rsp */ /*-------------------------------------------------------------------* * claw_strt_conn_req * * * *--------------------------------------------------------------------*/ static int claw_strt_conn_req(struct net_device *dev ) { int rc; CLAW_DBF_TEXT(2, setup, "conn_req"); rc=claw_snd_conn_req(dev, 1); return rc; } /* end of claw_strt_conn_req */ /*-------------------------------------------------------------------* * claw_stats * *-------------------------------------------------------------------*/ static struct net_device_stats *claw_stats(struct net_device *dev) { struct claw_privbk *privptr; CLAW_DBF_TEXT(4, trace, "stats"); privptr = dev->ml_priv; return &privptr->stats; } /* end of claw_stats */ /*-------------------------------------------------------------------* * unpack_read * * * *--------------------------------------------------------------------*/ static void unpack_read(struct net_device *dev ) { struct sk_buff *skb; struct claw_privbk *privptr; struct claw_env *p_env; struct ccwbk *p_this_ccw; struct ccwbk *p_first_ccw; struct ccwbk *p_last_ccw; struct clawph *p_packh; void *p_packd; struct clawctl *p_ctlrec=NULL; struct device *p_dev; __u32 len_of_data; __u32 pack_off; __u8 link_num; __u8 mtc_this_frm=0; __u32 bytes_to_mov; int i=0; int p=0; CLAW_DBF_TEXT(4, trace, "unpkread"); p_first_ccw=NULL; p_last_ccw=NULL; p_packh=NULL; p_packd=NULL; privptr = dev->ml_priv; p_dev = &privptr->channel[READ].cdev->dev; p_env = privptr->p_env; p_this_ccw=privptr->p_read_active_first; while (p_this_ccw!=NULL && p_this_ccw->header.flag!=CLAW_PENDING) { pack_off = 0; p = 0; p_this_ccw->header.flag=CLAW_PENDING; privptr->p_read_active_first=p_this_ccw->next; p_this_ccw->next=NULL; p_packh = (struct clawph *)p_this_ccw->p_buffer; if ((p_env->packing == PACK_SEND) && (p_packh->len == 32) && (p_packh->link_num == 0)) { /* is it a packed ctl rec? */ p_packh++; /* peek past pack header */ p_ctlrec = (struct clawctl *)p_packh; p_packh--; /* un peek */ if ((p_ctlrec->command == CONNECTION_RESPONSE) || (p_ctlrec->command == CONNECTION_CONFIRM)) p_env->packing = DO_PACKED; } if (p_env->packing == DO_PACKED) link_num=p_packh->link_num; else link_num=p_this_ccw->header.opcode / 8; if ((p_this_ccw->header.opcode & MORE_to_COME_FLAG)!=0) { mtc_this_frm=1; if (p_this_ccw->header.length!= privptr->p_env->read_size ) { dev_warn(p_dev, "The communication peer of %s" " sent a faulty" " frame of length %02x\n", dev->name, p_this_ccw->header.length); } } if (privptr->mtc_skipping) { /* * We're in the mode of skipping past a * multi-frame message * that we can't process for some reason or other. * The first frame without the More-To-Come flag is * the last frame of the skipped message. */ /* in case of More-To-Come not set in this frame */ if (mtc_this_frm==0) { privptr->mtc_skipping=0; /* Ok, the end */ privptr->mtc_logical_link=-1; } goto NextFrame; } if (link_num==0) { claw_process_control(dev, p_this_ccw); CLAW_DBF_TEXT(4, trace, "UnpkCntl"); goto NextFrame; } unpack_next: if (p_env->packing == DO_PACKED) { if (pack_off > p_env->read_size) goto NextFrame; p_packd = p_this_ccw->p_buffer+pack_off; p_packh = (struct clawph *) p_packd; if ((p_packh->len == 0) || /* done with this frame? */ (p_packh->flag != 0)) goto NextFrame; bytes_to_mov = p_packh->len; pack_off += bytes_to_mov+sizeof(struct clawph); p++; } else { bytes_to_mov=p_this_ccw->header.length; } if (privptr->mtc_logical_link<0) { /* * if More-To-Come is set in this frame then we don't know * length of entire message, and hence have to allocate * large buffer */ /* We are starting a new envelope */ privptr->mtc_offset=0; privptr->mtc_logical_link=link_num; } if (bytes_to_mov > (MAX_ENVELOPE_SIZE- privptr->mtc_offset) ) { /* error */ privptr->stats.rx_frame_errors++; goto NextFrame; } if (p_env->packing == DO_PACKED) { memcpy( privptr->p_mtc_envelope+ privptr->mtc_offset, p_packd+sizeof(struct clawph), bytes_to_mov); } else { memcpy( privptr->p_mtc_envelope+ privptr->mtc_offset, p_this_ccw->p_buffer, bytes_to_mov); } if (mtc_this_frm==0) { len_of_data=privptr->mtc_offset+bytes_to_mov; skb=dev_alloc_skb(len_of_data); if (skb) { memcpy(skb_put(skb,len_of_data), privptr->p_mtc_envelope, len_of_data); skb->dev=dev; skb_reset_mac_header(skb); skb->protocol=htons(ETH_P_IP); skb->ip_summed=CHECKSUM_UNNECESSARY; privptr->stats.rx_packets++; privptr->stats.rx_bytes+=len_of_data; netif_rx(skb); } else { dev_info(p_dev, "Allocating a buffer for" " incoming data failed\n"); privptr->stats.rx_dropped++; } privptr->mtc_offset=0; privptr->mtc_logical_link=-1; } else { privptr->mtc_offset+=bytes_to_mov; } if (p_env->packing == DO_PACKED) goto unpack_next; NextFrame: /* * Remove ThisCCWblock from active read queue, and add it * to queue of free blocks to be reused. */ i++; p_this_ccw->header.length=0xffff; p_this_ccw->header.opcode=0xff; /* * add this one to the free queue for later reuse */ if (p_first_ccw==NULL) { p_first_ccw = p_this_ccw; } else { p_last_ccw->next = p_this_ccw; } p_last_ccw = p_this_ccw; /* * chain to next block on active read queue */ p_this_ccw = privptr->p_read_active_first; CLAW_DBF_TEXT_(4, trace, "rxpkt %d", p); } /* end of while */ /* check validity */ CLAW_DBF_TEXT_(4, trace, "rxfrm %d", i); add_claw_reads(dev, p_first_ccw, p_last_ccw); claw_strt_read(dev, LOCK_YES); return; } /* end of unpack_read */ /*-------------------------------------------------------------------* * claw_strt_read * * * *--------------------------------------------------------------------*/ static void claw_strt_read (struct net_device *dev, int lock ) { int rc = 0; __u32 parm; unsigned long saveflags = 0; struct claw_privbk *privptr = dev->ml_priv; struct ccwbk*p_ccwbk; struct chbk *p_ch; struct clawh *p_clawh; p_ch=&privptr->channel[READ]; CLAW_DBF_TEXT(4, trace, "StRdNter"); p_clawh=(struct clawh *)privptr->p_claw_signal_blk; p_clawh->flag=CLAW_IDLE; /* 0x00 */ if ((privptr->p_write_active_first!=NULL && privptr->p_write_active_first->header.flag!=CLAW_PENDING) || (privptr->p_read_active_first!=NULL && privptr->p_read_active_first->header.flag!=CLAW_PENDING )) { p_clawh->flag=CLAW_BUSY; /* 0xff */ } if (lock==LOCK_YES) { spin_lock_irqsave(get_ccwdev_lock(p_ch->cdev), saveflags); } if (test_and_set_bit(0, (void *)&p_ch->IO_active) == 0) { CLAW_DBF_TEXT(4, trace, "HotRead"); p_ccwbk=privptr->p_read_active_first; parm = (unsigned long) p_ch; rc = ccw_device_start (p_ch->cdev, &p_ccwbk->read, parm, 0xff, 0); if (rc != 0) { ccw_check_return_code(p_ch->cdev, rc); } } else { CLAW_DBF_TEXT(2, trace, "ReadAct"); } if (lock==LOCK_YES) { spin_unlock_irqrestore(get_ccwdev_lock(p_ch->cdev), saveflags); } CLAW_DBF_TEXT(4, trace, "StRdExit"); return; } /* end of claw_strt_read */ /*-------------------------------------------------------------------* * claw_strt_out_IO * * * *--------------------------------------------------------------------*/ static void claw_strt_out_IO( struct net_device *dev ) { int rc = 0; unsigned long parm; struct claw_privbk *privptr; struct chbk *p_ch; struct ccwbk *p_first_ccw; if (!dev) { return; } privptr = (struct claw_privbk *)dev->ml_priv; p_ch=&privptr->channel[WRITE]; CLAW_DBF_TEXT(4, trace, "strt_io"); p_first_ccw=privptr->p_write_active_first; if (p_ch->claw_state == CLAW_STOP) return; if (p_first_ccw == NULL) { return; } if (test_and_set_bit(0, (void *)&p_ch->IO_active) == 0) { parm = (unsigned long) p_ch; CLAW_DBF_TEXT(2, trace, "StWrtIO"); rc = ccw_device_start(p_ch->cdev, &p_first_ccw->write, parm, 0xff, 0); if (rc != 0) { ccw_check_return_code(p_ch->cdev, rc); } } dev->trans_start = jiffies; return; } /* end of claw_strt_out_IO */ /*-------------------------------------------------------------------* * Free write buffers * * * *--------------------------------------------------------------------*/ static void claw_free_wrt_buf( struct net_device *dev ) { struct claw_privbk *privptr = (struct claw_privbk *)dev->ml_priv; struct ccwbk*p_first_ccw; struct ccwbk*p_last_ccw; struct ccwbk*p_this_ccw; struct ccwbk*p_next_ccw; CLAW_DBF_TEXT(4, trace, "freewrtb"); /* scan the write queue to free any completed write packets */ p_first_ccw=NULL; p_last_ccw=NULL; p_this_ccw=privptr->p_write_active_first; while ( (p_this_ccw!=NULL) && (p_this_ccw->header.flag!=CLAW_PENDING)) { p_next_ccw = p_this_ccw->next; if (((p_next_ccw!=NULL) && (p_next_ccw->header.flag!=CLAW_PENDING)) || ((p_this_ccw == privptr->p_write_active_last) && (p_this_ccw->header.flag!=CLAW_PENDING))) { /* The next CCW is OK or this is */ /* the last CCW...free it @A1A */ privptr->p_write_active_first=p_this_ccw->next; p_this_ccw->header.flag=CLAW_PENDING; p_this_ccw->next=privptr->p_write_free_chain; privptr->p_write_free_chain=p_this_ccw; ++privptr->write_free_count; privptr->stats.tx_bytes+= p_this_ccw->write.count; p_this_ccw=privptr->p_write_active_first; privptr->stats.tx_packets++; } else { break; } } if (privptr->write_free_count!=0) { claw_clearbit_busy(TB_NOBUFFER,dev); } /* whole chain removed? */ if (privptr->p_write_active_first==NULL) { privptr->p_write_active_last=NULL; } CLAW_DBF_TEXT_(4, trace, "FWC=%d", privptr->write_free_count); return; } /*-------------------------------------------------------------------* * claw free netdevice * * * *--------------------------------------------------------------------*/ static void claw_free_netdevice(struct net_device * dev, int free_dev) { struct claw_privbk *privptr; CLAW_DBF_TEXT(2, setup, "free_dev"); if (!dev) return; CLAW_DBF_TEXT_(2, setup, "%s", dev->name); privptr = dev->ml_priv; if (dev->flags & IFF_RUNNING) claw_release(dev); if (privptr) { privptr->channel[READ].ndev = NULL; /* say it's free */ } dev->ml_priv = NULL; #ifdef MODULE if (free_dev) { free_netdev(dev); } #endif CLAW_DBF_TEXT(2, setup, "free_ok"); } /** * Claw init netdevice * Initialize everything of the net device except the name and the * channel structs. */ static const struct net_device_ops claw_netdev_ops = { .ndo_open = claw_open, .ndo_stop = claw_release, .ndo_get_stats = claw_stats, .ndo_start_xmit = claw_tx, .ndo_change_mtu = claw_change_mtu, }; static void claw_init_netdevice(struct net_device * dev) { CLAW_DBF_TEXT(2, setup, "init_dev"); CLAW_DBF_TEXT_(2, setup, "%s", dev->name); dev->mtu = CLAW_DEFAULT_MTU_SIZE; dev->hard_header_len = 0; dev->addr_len = 0; dev->type = ARPHRD_SLIP; dev->tx_queue_len = 1300; dev->flags = IFF_POINTOPOINT | IFF_NOARP; dev->netdev_ops = &claw_netdev_ops; CLAW_DBF_TEXT(2, setup, "initok"); return; } /** * Init a new channel in the privptr->channel[i]. * * @param cdev The ccw_device to be added. * * @return 0 on success, !0 on error. */ static int add_channel(struct ccw_device *cdev,int i,struct claw_privbk *privptr) { struct chbk *p_ch; struct ccw_dev_id dev_id; CLAW_DBF_TEXT_(2, setup, "%s", dev_name(&cdev->dev)); privptr->channel[i].flag = i+1; /* Read is 1 Write is 2 */ p_ch = &privptr->channel[i]; p_ch->cdev = cdev; snprintf(p_ch->id, CLAW_ID_SIZE, "cl-%s", dev_name(&cdev->dev)); ccw_device_get_id(cdev, &dev_id); p_ch->devno = dev_id.devno; if ((p_ch->irb = kzalloc(sizeof (struct irb),GFP_KERNEL)) == NULL) { return -ENOMEM; } return 0; } /** * * Setup an interface. * * @param cgdev Device to be setup. * * @returns 0 on success, !0 on failure. */ static int claw_new_device(struct ccwgroup_device *cgdev) { struct claw_privbk *privptr; struct claw_env *p_env; struct net_device *dev; int ret; struct ccw_dev_id dev_id; dev_info(&cgdev->dev, "add for %s\n", dev_name(&cgdev->cdev[READ]->dev)); CLAW_DBF_TEXT(2, setup, "new_dev"); privptr = dev_get_drvdata(&cgdev->dev); dev_set_drvdata(&cgdev->cdev[READ]->dev, privptr); dev_set_drvdata(&cgdev->cdev[WRITE]->dev, privptr); if (!privptr) return -ENODEV; p_env = privptr->p_env; ccw_device_get_id(cgdev->cdev[READ], &dev_id); p_env->devno[READ] = dev_id.devno; ccw_device_get_id(cgdev->cdev[WRITE], &dev_id); p_env->devno[WRITE] = dev_id.devno; ret = add_channel(cgdev->cdev[0],0,privptr); if (ret == 0) ret = add_channel(cgdev->cdev[1],1,privptr); if (ret != 0) { dev_warn(&cgdev->dev, "Creating a CLAW group device" " failed with error code %d\n", ret); goto out; } ret = ccw_device_set_online(cgdev->cdev[READ]); if (ret != 0) { dev_warn(&cgdev->dev, "Setting the read subchannel online" " failed with error code %d\n", ret); goto out; } ret = ccw_device_set_online(cgdev->cdev[WRITE]); if (ret != 0) { dev_warn(&cgdev->dev, "Setting the write subchannel online " "failed with error code %d\n", ret); goto out; } dev = alloc_netdev(0,"claw%d",claw_init_netdevice); if (!dev) { dev_warn(&cgdev->dev, "Activating the CLAW device failed\n"); goto out; } dev->ml_priv = privptr; dev_set_drvdata(&cgdev->dev, privptr); dev_set_drvdata(&cgdev->cdev[READ]->dev, privptr); dev_set_drvdata(&cgdev->cdev[WRITE]->dev, privptr); /* sysfs magic */ SET_NETDEV_DEV(dev, &cgdev->dev); if (register_netdev(dev) != 0) { claw_free_netdevice(dev, 1); CLAW_DBF_TEXT(2, trace, "regfail"); goto out; } dev->flags &=~IFF_RUNNING; if (privptr->buffs_alloc == 0) { ret=init_ccw_bk(dev); if (ret !=0) { unregister_netdev(dev); claw_free_netdevice(dev,1); CLAW_DBF_TEXT(2, trace, "ccwmem"); goto out; } } privptr->channel[READ].ndev = dev; privptr->channel[WRITE].ndev = dev; privptr->p_env->ndev = dev; dev_info(&cgdev->dev, "%s:readsize=%d writesize=%d " "readbuffer=%d writebuffer=%d read=0x%04x write=0x%04x\n", dev->name, p_env->read_size, p_env->write_size, p_env->read_buffers, p_env->write_buffers, p_env->devno[READ], p_env->devno[WRITE]); dev_info(&cgdev->dev, "%s:host_name:%.8s, adapter_name " ":%.8s api_type: %.8s\n", dev->name, p_env->host_name, p_env->adapter_name , p_env->api_type); return 0; out: ccw_device_set_offline(cgdev->cdev[1]); ccw_device_set_offline(cgdev->cdev[0]); return -ENODEV; } static void claw_purge_skb_queue(struct sk_buff_head *q) { struct sk_buff *skb; CLAW_DBF_TEXT(4, trace, "purgque"); while ((skb = skb_dequeue(q))) { atomic_dec(&skb->users); dev_kfree_skb_any(skb); } } /** * Shutdown an interface. * * @param cgdev Device to be shut down. * * @returns 0 on success, !0 on failure. */ static int claw_shutdown_device(struct ccwgroup_device *cgdev) { struct claw_privbk *priv; struct net_device *ndev; int ret; CLAW_DBF_TEXT_(2, setup, "%s", dev_name(&cgdev->dev)); priv = dev_get_drvdata(&cgdev->dev); if (!priv) return -ENODEV; ndev = priv->channel[READ].ndev; if (ndev) { /* Close the device */ dev_info(&cgdev->dev, "%s: shutting down \n", ndev->name); if (ndev->flags & IFF_RUNNING) ret = claw_release(ndev); ndev->flags &=~IFF_RUNNING; unregister_netdev(ndev); ndev->ml_priv = NULL; /* cgdev data, not ndev's to free */ claw_free_netdevice(ndev, 1); priv->channel[READ].ndev = NULL; priv->channel[WRITE].ndev = NULL; priv->p_env->ndev = NULL; } ccw_device_set_offline(cgdev->cdev[1]); ccw_device_set_offline(cgdev->cdev[0]); return 0; } static void claw_remove_device(struct ccwgroup_device *cgdev) { struct claw_privbk *priv; BUG_ON(!cgdev); CLAW_DBF_TEXT_(2, setup, "%s", dev_name(&cgdev->dev)); priv = dev_get_drvdata(&cgdev->dev); BUG_ON(!priv); dev_info(&cgdev->dev, " will be removed.\n"); if (cgdev->state == CCWGROUP_ONLINE) claw_shutdown_device(cgdev); claw_remove_files(&cgdev->dev); kfree(priv->p_mtc_envelope); priv->p_mtc_envelope=NULL; kfree(priv->p_env); priv->p_env=NULL; kfree(priv->channel[0].irb); priv->channel[0].irb=NULL; kfree(priv->channel[1].irb); priv->channel[1].irb=NULL; kfree(priv); dev_set_drvdata(&cgdev->dev, NULL); dev_set_drvdata(&cgdev->cdev[READ]->dev, NULL); dev_set_drvdata(&cgdev->cdev[WRITE]->dev, NULL); put_device(&cgdev->dev); return; } /* * sysfs attributes */ static ssize_t claw_hname_show(struct device *dev, struct device_attribute *attr, char *buf) { struct claw_privbk *priv; struct claw_env * p_env; priv = dev_get_drvdata(dev); if (!priv) return -ENODEV; p_env = priv->p_env; return sprintf(buf, "%s\n",p_env->host_name); } static ssize_t claw_hname_write(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { struct claw_privbk *priv; struct claw_env * p_env; priv = dev_get_drvdata(dev); if (!priv) return -ENODEV; p_env = priv->p_env; if (count > MAX_NAME_LEN+1) return -EINVAL; memset(p_env->host_name, 0x20, MAX_NAME_LEN); strncpy(p_env->host_name,buf, count); p_env->host_name[count-1] = 0x20; /* clear extra 0x0a */ p_env->host_name[MAX_NAME_LEN] = 0x00; CLAW_DBF_TEXT(2, setup, "HstnSet"); CLAW_DBF_TEXT_(2, setup, "%s", p_env->host_name); return count; } static DEVICE_ATTR(host_name, 0644, claw_hname_show, claw_hname_write); static ssize_t claw_adname_show(struct device *dev, struct device_attribute *attr, char *buf) { struct claw_privbk *priv; struct claw_env * p_env; priv = dev_get_drvdata(dev); if (!priv) return -ENODEV; p_env = priv->p_env; return sprintf(buf, "%s\n", p_env->adapter_name); } static ssize_t claw_adname_write(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { struct claw_privbk *priv; struct claw_env * p_env; priv = dev_get_drvdata(dev); if (!priv) return -ENODEV; p_env = priv->p_env; if (count > MAX_NAME_LEN+1) return -EINVAL; memset(p_env->adapter_name, 0x20, MAX_NAME_LEN); strncpy(p_env->adapter_name,buf, count); p_env->adapter_name[count-1] = 0x20; /* clear extra 0x0a */ p_env->adapter_name[MAX_NAME_LEN] = 0x00; CLAW_DBF_TEXT(2, setup, "AdnSet"); CLAW_DBF_TEXT_(2, setup, "%s", p_env->adapter_name); return count; } static DEVICE_ATTR(adapter_name, 0644, claw_adname_show, claw_adname_write); static ssize_t claw_apname_show(struct device *dev, struct device_attribute *attr, char *buf) { struct claw_privbk *priv; struct claw_env * p_env; priv = dev_get_drvdata(dev); if (!priv) return -ENODEV; p_env = priv->p_env; return sprintf(buf, "%s\n", p_env->api_type); } static ssize_t claw_apname_write(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { struct claw_privbk *priv; struct claw_env * p_env; priv = dev_get_drvdata(dev); if (!priv) return -ENODEV; p_env = priv->p_env; if (count > MAX_NAME_LEN+1) return -EINVAL; memset(p_env->api_type, 0x20, MAX_NAME_LEN); strncpy(p_env->api_type,buf, count); p_env->api_type[count-1] = 0x20; /* we get a loose 0x0a */ p_env->api_type[MAX_NAME_LEN] = 0x00; if(strncmp(p_env->api_type,WS_APPL_NAME_PACKED,6) == 0) { p_env->read_size=DEF_PACK_BUFSIZE; p_env->write_size=DEF_PACK_BUFSIZE; p_env->packing=PACKING_ASK; CLAW_DBF_TEXT(2, setup, "PACKING"); } else { p_env->packing=0; p_env->read_size=CLAW_FRAME_SIZE; p_env->write_size=CLAW_FRAME_SIZE; CLAW_DBF_TEXT(2, setup, "ApiSet"); } CLAW_DBF_TEXT_(2, setup, "%s", p_env->api_type); return count; } static DEVICE_ATTR(api_type, 0644, claw_apname_show, claw_apname_write); static ssize_t claw_wbuff_show(struct device *dev, struct device_attribute *attr, char *buf) { struct claw_privbk *priv; struct claw_env * p_env; priv = dev_get_drvdata(dev); if (!priv) return -ENODEV; p_env = priv->p_env; return sprintf(buf, "%d\n", p_env->write_buffers); } static ssize_t claw_wbuff_write(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { struct claw_privbk *priv; struct claw_env * p_env; int nnn,max; priv = dev_get_drvdata(dev); if (!priv) return -ENODEV; p_env = priv->p_env; sscanf(buf, "%i", &nnn); if (p_env->packing) { max = 64; } else { max = 512; } if ((nnn > max ) || (nnn < 2)) return -EINVAL; p_env->write_buffers = nnn; CLAW_DBF_TEXT(2, setup, "Wbufset"); CLAW_DBF_TEXT_(2, setup, "WB=%d", p_env->write_buffers); return count; } static DEVICE_ATTR(write_buffer, 0644, claw_wbuff_show, claw_wbuff_write); static ssize_t claw_rbuff_show(struct device *dev, struct device_attribute *attr, char *buf) { struct claw_privbk *priv; struct claw_env * p_env; priv = dev_get_drvdata(dev); if (!priv) return -ENODEV; p_env = priv->p_env; return sprintf(buf, "%d\n", p_env->read_buffers); } static ssize_t claw_rbuff_write(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { struct claw_privbk *priv; struct claw_env *p_env; int nnn,max; priv = dev_get_drvdata(dev); if (!priv) return -ENODEV; p_env = priv->p_env; sscanf(buf, "%i", &nnn); if (p_env->packing) { max = 64; } else { max = 512; } if ((nnn > max ) || (nnn < 2)) return -EINVAL; p_env->read_buffers = nnn; CLAW_DBF_TEXT(2, setup, "Rbufset"); CLAW_DBF_TEXT_(2, setup, "RB=%d", p_env->read_buffers); return count; } static DEVICE_ATTR(read_buffer, 0644, claw_rbuff_show, claw_rbuff_write); static struct attribute *claw_attr[] = { &dev_attr_read_buffer.attr, &dev_attr_write_buffer.attr, &dev_attr_adapter_name.attr, &dev_attr_api_type.attr, &dev_attr_host_name.attr, NULL, }; static struct attribute_group claw_attr_group = { .attrs = claw_attr, }; static int claw_add_files(struct device *dev) { CLAW_DBF_TEXT(2, setup, "add_file"); return sysfs_create_group(&dev->kobj, &claw_attr_group); } static void claw_remove_files(struct device *dev) { CLAW_DBF_TEXT(2, setup, "rem_file"); sysfs_remove_group(&dev->kobj, &claw_attr_group); } /*--------------------------------------------------------------------* * claw_init and cleanup * *---------------------------------------------------------------------*/ static void __exit claw_cleanup(void) { unregister_cu3088_discipline(&claw_group_driver); claw_unregister_debug_facility(); pr_info("Driver unloaded\n"); } /** * Initialize module. * This is called just after the module is loaded. * * @return 0 on success, !0 on error. */ static int __init claw_init(void) { int ret = 0; pr_info("Loading %s\n", version); ret = claw_register_debug_facility(); if (ret) { pr_err("Registering with the S/390 debug feature" " failed with error code %d\n", ret); return ret; } CLAW_DBF_TEXT(2, setup, "init_mod"); ret = register_cu3088_discipline(&claw_group_driver); if (ret) { CLAW_DBF_TEXT(2, setup, "init_bad"); claw_unregister_debug_facility(); pr_err("Registering with the cu3088 device driver failed " "with error code %d\n", ret); } return ret; } module_init(claw_init); module_exit(claw_cleanup); MODULE_AUTHOR("Andy Richter <richtera@us.ibm.com>"); MODULE_DESCRIPTION("Linux for System z CLAW Driver\n" \ "Copyright 2000,2008 IBM Corporation\n"); MODULE_LICENSE("GPL");