aboutsummaryrefslogtreecommitdiffstats
path: root/lib/dma-debug.c
blob: 65b0d99b6d0aa8f7219741e731394d1514b1706e (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
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
/*
 * Copyright (C) 2008 Advanced Micro Devices, Inc.
 *
 * Author: Joerg Roedel <joerg.roedel@amd.com>
 *
 * 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.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * 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
 */

#include <linux/scatterlist.h>
#include <linux/dma-mapping.h>
#include <linux/stacktrace.h>
#include <linux/dma-debug.h>
#include <linux/spinlock.h>
#include <linux/debugfs.h>
#include <linux/uaccess.h>
#include <linux/device.h>
#include <linux/types.h>
#include <linux/sched.h>
#include <linux/ctype.h>
#include <linux/list.h>
#include <linux/slab.h>

#include <asm/sections.h>

#define HASH_SIZE       1024ULL
#define HASH_FN_SHIFT   13
#define HASH_FN_MASK    (HASH_SIZE - 1)

enum {
	dma_debug_single,
	dma_debug_page,
	dma_debug_sg,
	dma_debug_coherent,
};

#define DMA_DEBUG_STACKTRACE_ENTRIES 5

struct dma_debug_entry {
	struct list_head list;
	struct device    *dev;
	int              type;
	phys_addr_t      paddr;
	u64              dev_addr;
	u64              size;
	int              direction;
	int		 sg_call_ents;
	int		 sg_mapped_ents;
#ifdef CONFIG_STACKTRACE
	struct		 stack_trace stacktrace;
	unsigned long	 st_entries[DMA_DEBUG_STACKTRACE_ENTRIES];
#endif
};

struct hash_bucket {
	struct list_head list;
	spinlock_t lock;
} ____cacheline_aligned_in_smp;

/* Hash list to save the allocated dma addresses */
static struct hash_bucket dma_entry_hash[HASH_SIZE];
/* List of pre-allocated dma_debug_entry's */
static LIST_HEAD(free_entries);
/* Lock for the list above */
static DEFINE_SPINLOCK(free_entries_lock);

/* Global disable flag - will be set in case of an error */
static bool global_disable __read_mostly;

/* Global error count */
static u32 error_count;

/* Global error show enable*/
static u32 show_all_errors __read_mostly;
/* Number of errors to show */
static u32 show_num_errors = 1;

static u32 num_free_entries;
static u32 min_free_entries;
static u32 nr_total_entries;

/* number of preallocated entries requested by kernel cmdline */
static u32 req_entries;

/* debugfs dentry's for the stuff above */
static struct dentry *dma_debug_dent        __read_mostly;
static struct dentry *global_disable_dent   __read_mostly;
static struct dentry *error_count_dent      __read_mostly;
static struct dentry *show_all_errors_dent  __read_mostly;
static struct dentry *show_num_errors_dent  __read_mostly;
static struct dentry *num_free_entries_dent __read_mostly;
static struct dentry *min_free_entries_dent __read_mostly;
static struct dentry *filter_dent           __read_mostly;

/* per-driver filter related state */

#define NAME_MAX_LEN	64

static char                  current_driver_name[NAME_MAX_LEN] __read_mostly;
static struct device_driver *current_driver                    __read_mostly;

static DEFINE_RWLOCK(driver_name_lock);

static const char *type2name[4] = { "single", "page",
				    "scather-gather", "coherent" };

static const char *dir2name[4] = { "DMA_BIDIRECTIONAL", "DMA_TO_DEVICE",
				   "DMA_FROM_DEVICE", "DMA_NONE" };

/* little merge helper - remove it after the merge window */
#ifndef BUS_NOTIFY_UNBOUND_DRIVER
#define BUS_NOTIFY_UNBOUND_DRIVER 0x0005
#endif

/*
 * The access to some variables in this macro is racy. We can't use atomic_t
 * here because all these variables are exported to debugfs. Some of them even
 * writeable. This is also the reason why a lock won't help much. But anyway,
 * the races are no big deal. Here is why:
 *
 *   error_count: the addition is racy, but the worst thing that can happen is
 *                that we don't count some errors
 *   show_num_errors: the subtraction is racy. Also no big deal because in
 *                    worst case this will result in one warning more in the
 *                    system log than the user configured. This variable is
 *                    writeable via debugfs.
 */
static inline void dump_entry_trace(struct dma_debug_entry *entry)
{
#ifdef CONFIG_STACKTRACE
	if (entry) {
		pr_warning("Mapped at:\n");
		print_stack_trace(&entry->stacktrace, 0);
	}
#endif
}

static bool driver_filter(struct device *dev)
{
	struct device_driver *drv;
	unsigned long flags;
	bool ret;

	/* driver filter off */
	if (likely(!current_driver_name[0]))
		return true;

	/* driver filter on and initialized */
	if (current_driver && dev->driver == current_driver)
		return true;

	if (current_driver || !current_driver_name[0])
		return false;

	/* driver filter on but not yet initialized */
	drv = get_driver(dev->driver);
	if (!drv)
		return false;

	/* lock to protect against change of current_driver_name */
	read_lock_irqsave(&driver_name_lock, flags);

	ret = false;
	if (drv->name &&
	    strncmp(current_driver_name, drv->name, NAME_MAX_LEN - 1) == 0) {
		current_driver = drv;
		ret = true;
	}

	read_unlock_irqrestore(&driver_name_lock, flags);
	put_driver(drv);

	return ret;
}

#define err_printk(dev, entry, format, arg...) do {		\
		error_count += 1;				\
		if (driver_filter(dev) &&			\
		    (show_all_errors || show_num_errors > 0)) {	\
			WARN(1, "%s %s: " format,		\
			     dev_driver_string(dev),		\
			     dev_name(dev) , ## arg);		\
			dump_entry_trace(entry);		\
		}						\
		if (!show_all_errors && show_num_errors > 0)	\
			show_num_errors -= 1;			\
	} while (0);

/*
 * Hash related functions
 *
 * Every DMA-API request is saved into a struct dma_debug_entry. To
 * have quick access to these structs they are stored into a hash.
 */
static int hash_fn(struct dma_debug_entry *entry)
{
	/*
	 * Hash function is based on the dma address.
	 * We use bits 20-27 here as the index into the hash
	 */
	return (entry->dev_addr >> HASH_FN_SHIFT) & HASH_FN_MASK;
}

/*
 * Request exclusive access to a hash bucket for a given dma_debug_entry.
 */
static struct hash_bucket *get_hash_bucket(struct dma_debug_entry *entry,
					   unsigned long *flags)
{
	int idx = hash_fn(entry);
	unsigned long __flags;

	spin_lock_irqsave(&dma_entry_hash[idx].lock, __flags);
	*flags = __flags;
	return &dma_entry_hash[idx];
}

/*
 * Give up exclusive access to the hash bucket
 */
static void put_hash_bucket(struct hash_bucket *bucket,
			    unsigned long *flags)
{
	unsigned long __flags = *flags;

	spin_unlock_irqrestore(&bucket->lock, __flags);
}

/*
 * Search a given entry in the hash bucket list
 */
static struct dma_debug_entry *hash_bucket_find(struct hash_bucket *bucket,
						struct dma_debug_entry *ref)
{
	struct dma_debug_entry *entry, *ret = NULL;
	int matches = 0, match_lvl, last_lvl = 0;

	list_for_each_entry(entry, &bucket->list, list) {
		if ((entry->dev_addr != ref->dev_addr) ||
		    (entry->dev != ref->dev))
			continue;

		/*
		 * Some drivers map the same physical address multiple
		 * times. Without a hardware IOMMU this results in the
		 * same device addresses being put into the dma-debug
		 * hash multiple times too. This can result in false
		 * positives being reported. Therfore we implement a
		 * best-fit algorithm here which returns the entry from
		 * the hash which fits best to the reference value
		 * instead of the first-fit.
		 */
		matches += 1;
		match_lvl = 0;
		entry->size         == ref->size         ? ++match_lvl : 0;
		entry->type         == ref->type         ? ++match_lvl : 0;
		entry->direction    == ref->direction    ? ++match_lvl : 0;
		entry->sg_call_ents == ref->sg_call_ents ? ++match_lvl : 0;

		if (match_lvl == 4) {
			/* perfect-fit - return the result */
			return entry;
		} else if (match_lvl > last_lvl) {
			/*
			 * We found an entry that fits better then the
			 * previous one
			 */
			last_lvl = match_lvl;
			ret      = entry;
		}
	}

	/*
	 * If we have multiple matches but no perfect-fit, just return
	 * NULL.
	 */
	ret = (matches == 1) ? ret : NULL;

	return ret;
}

/*
 * Add an entry to a hash bucket
 */
static void hash_bucket_add(struct hash_bucket *bucket,
			    struct dma_debug_entry *entry)
{
	list_add_tail(&entry->list, &bucket->list);
}

/*
 * Remove entry from a hash bucket list
 */
static void hash_bucket_del(struct dma_debug_entry *entry)
{
	list_del(&entry->list);
}

/*
 * Dump mapping entries for debugging purposes
 */
void debug_dma_dump_mappings(struct device *dev)
{
	int idx;

	for (idx = 0; idx < HASH_SIZE; idx++) {
		struct hash_bucket *bucket = &dma_entry_hash[idx];
		struct dma_debug_entry *entry;
		unsigned long flags;

		spin_lock_irqsave(&bucket->lock, flags);

		list_for_each_entry(entry, &bucket->list, list) {
			if (!dev || dev == entry->dev) {
				dev_info(entry->dev,
					 "%s idx %d P=%Lx D=%Lx L=%Lx %s\n",
					 type2name[entry->type], idx,
					 (unsigned long long)entry->paddr,
					 entry->dev_addr, entry->size,
					 dir2name[entry->direction]);
			}
		}

		spin_unlock_irqrestore(&bucket->lock, flags);
	}
}
EXPORT_SYMBOL(debug_dma_dump_mappings);

/*
 * Wrapper function for adding an entry to the hash.
 * This function takes care of locking itself.
 */
static void add_dma_entry(struct dma_debug_entry *entry)
{
	struct hash_bucket *bucket;
	unsigned long flags;

	bucket = get_hash_bucket(entry, &flags);
	hash_bucket_add(bucket, entry);
	put_hash_bucket(bucket, &flags);
}

static struct dma_debug_entry *__dma_entry_alloc(void)
{
	struct dma_debug_entry *entry;

	entry = list_entry(free_entries.next, struct dma_debug_entry, list);
	list_del(&entry->list);
	memset(entry, 0, sizeof(*entry));

	num_free_entries -= 1;
	if (num_free_entries < min_free_entries)
		min_free_entries = num_free_entries;

	return entry;
}

/* struct dma_entry allocator
 *
 * The next two functions implement the allocator for
 * struct dma_debug_entries.
 */
static struct dma_debug_entry *dma_entry_alloc(void)
{
	struct dma_debug_entry *entry = NULL;
	unsigned long flags;

	spin_lock_irqsave(&free_entries_lock, flags);

	if (list_empty(&free_entries)) {
		pr_err("DMA-API: debugging out of memory - disabling\n");
		global_disable = true;
		goto out;
	}

	entry = __dma_entry_alloc();

#ifdef CONFIG_STACKTRACE
	entry->stacktrace.max_entries = DMA_DEBUG_STACKTRACE_ENTRIES;
	entry->stacktrace.entries = entry->st_entries;
	entry->stacktrace.skip = 2;
	save_stack_trace(&entry->stacktrace);
#endif

out:
	spin_unlock_irqrestore(&free_entries_lock, flags);

	return entry;
}

static void dma_entry_free(struct dma_debug_entry *entry)
{
	unsigned long flags;

	/*
	 * add to beginning of the list - this way the entries are
	 * more likely cache hot when they are reallocated.
	 */
	spin_lock_irqsave(&free_entries_lock, flags);
	list_add(&entry->list, &free_entries);
	num_free_entries += 1;
	spin_unlock_irqrestore(&free_entries_lock, flags);
}

int dma_debug_resize_entries(u32 num_entries)
{
	int i, delta, ret = 0;
	unsigned long flags;
	struct dma_debug_entry *entry;
	LIST_HEAD(tmp);

	spin_lock_irqsave(&free_entries_lock, flags);

	if (nr_total_entries < num_entries) {
		delta = num_entries - nr_total_entries;

		spin_unlock_irqrestore(&free_entries_lock, flags);

		for (i = 0; i < delta; i++) {
			entry = kzalloc(sizeof(*entry), GFP_KERNEL);
			if (!entry)
				break;

			list_add_tail(&entry->list, &tmp);
		}

		spin_lock_irqsave(&free_entries_lock, flags);

		list_splice(&tmp, &free_entries);
		nr_total_entries += i;
		num_free_entries += i;
	} else {
		delta = nr_total_entries - num_entries;

		for (i = 0; i < delta && !list_empty(&free_entries); i++) {
			entry = __dma_entry_alloc();
			kfree(entry);
		}

		nr_total_entries -= i;
	}

	if (nr_total_entries != num_entries)
		ret = 1;

	spin_unlock_irqrestore(&free_entries_lock, flags);

	return ret;
}
EXPORT_SYMBOL(dma_debug_resize_entries);

/*
 * DMA-API debugging init code
 *
 * The init code does two things:
 *   1. Initialize core data structures
 *   2. Preallocate a given number of dma_debug_entry structs
 */

static int prealloc_memory(u32 num_entries)
{
	struct dma_debug_entry *entry, *next_entry;
	int i;

	for (i = 0; i < num_entries; ++i) {
		entry = kzalloc(sizeof(*entry), GFP_KERNEL);
		if (!entry)
			goto out_err;

		list_add_tail(&entry->list, &free_entries);
	}

	num_free_entries = num_entries;
	min_free_entries = num_entries;

	pr_info("DMA-API: preallocated %d debug entries\n", num_entries);

	return 0;

out_err:

	list_for_each_entry_safe(entry, next_entry, &free_entries, list) {
		list_del(&entry->list);
		kfree(entry);
	}

	return -ENOMEM;
}

static ssize_t filter_read(struct file *file, char __user *user_buf,
			   size_t count, loff_t *ppos)
{
	char buf[NAME_MAX_LEN + 1];
	unsigned long flags;
	int len;

	if (!current_driver_name[0])
		return 0;

	/*
	 * We can't copy to userspace directly because current_driver_name can
	 * only be read under the driver_name_lock with irqs disabled. So
	 * create a temporary copy first.
	 */
	read_lock_irqsave(&driver_name_lock, flags);
	len = scnprintf(buf, NAME_MAX_LEN + 1, "%s\n", current_driver_name);
	read_unlock_irqrestore(&driver_name_lock, flags);

	return simple_read_from_buffer(user_buf, count, ppos, buf, len);
}

static ssize_t filter_write(struct file *file, const char __user *userbuf,
			    size_t count, loff_t *ppos)
{
	char buf[NAME_MAX_LEN];
	unsigned long flags;
	size_t len;
	int i;

	/*
	 * We can't copy from userspace directly. Access to
	 * current_driver_name is protected with a write_lock with irqs
	 * disabled. Since copy_from_user can fault and may sleep we
	 * need to copy to temporary buffer first
	 */
	len = min(count, (size_t)(NAME_MAX_LEN - 1));
	if (copy_from_user(buf, userbuf, len))
		return -EFAULT;

	buf[len] = 0;

	write_lock_irqsave(&driver_name_lock, flags);

	/*
	 * Now handle the string we got from userspace very carefully.
	 * The rules are:
	 *         - only use the first token we got
	 *         - token delimiter is everything looking like a space
	 *           character (' ', '\n', '\t' ...)
	 *
	 */
	if (!isalnum(buf[0])) {
		/*
		 * If the first character userspace gave us is not
		 * alphanumerical then assume the filter should be
		 * switched off.
		 */
		if (current_driver_name[0])
			pr_info("DMA-API: switching off dma-debug driver filter\n");
		current_driver_name[0] = 0;
		current_driver = NULL;
		goto out_unlock;
	}

	/*
	 * Now parse out the first token and use it as the name for the
	 * driver to filter for.
	 */
	for (i = 0; i < NAME_MAX_LEN; ++i) {
		current_driver_name[i] = buf[i];
		if (isspace(buf[i]) || buf[i] == ' ' || buf[i] == 0)
			break;
	}
	current_driver_name[i] = 0;
	current_driver = NULL;

	pr_info("DMA-API: enable driver filter for driver [%s]\n",
		current_driver_name);

out_unlock:
	write_unlock_irqrestore(&driver_name_lock, flags);

	return count;
}

const struct file_operations filter_fops = {
	.read  = filter_read,
	.write = filter_write,
};

static int dma_debug_fs_init(void)
{
	dma_debug_dent = debugfs_create_dir("dma-api", NULL);
	if (!dma_debug_dent) {
		pr_err("DMA-API: can not create debugfs directory\n");
		return -ENOMEM;
	}

	global_disable_dent = debugfs_create_bool("disabled", 0444,
			dma_debug_dent,
			(u32 *)&global_disable);
	if (!global_disable_dent)
		goto out_err;

	error_count_dent = debugfs_create_u32("error_count", 0444,
			dma_debug_dent, &error_count);
	if (!error_count_dent)
		goto out_err;

	show_all_errors_dent = debugfs_create_u32("all_errors", 0644,
			dma_debug_dent,
			&show_all_errors);
	if (!show_all_errors_dent)
		goto out_err;

	show_num_errors_dent = debugfs_create_u32("num_errors", 0644,
			dma_debug_dent,
			&show_num_errors);
	if (!show_num_errors_dent)
		goto out_err;

	num_free_entries_dent = debugfs_create_u32("num_free_entries", 0444,
			dma_debug_dent,
			&num_free_entries);
	if (!num_free_entries_dent)
		goto out_err;

	min_free_entries_dent = debugfs_create_u32("min_free_entries", 0444,
			dma_debug_dent,
			&min_free_entries);
	if (!min_free_entries_dent)
		goto out_err;

	filter_dent = debugfs_create_file("driver_filter", 0644,
					  dma_debug_dent, NULL, &filter_fops);
	if (!filter_dent)
		goto out_err;

	return 0;

out_err:
	debugfs_remove_recursive(dma_debug_dent);

	return -ENOMEM;
}

static int device_dma_allocations(struct device *dev)
{
	struct dma_debug_entry *entry;
	unsigned long flags;
	int count = 0, i;

	local_irq_save(flags);

	for (i = 0; i < HASH_SIZE; ++i) {
		spin_lock(&dma_entry_hash[i].lock);
		list_for_each_entry(entry, &dma_entry_hash[i].list, list) {
			if (entry->dev == dev)
				count += 1;
		}
		spin_unlock(&dma_entry_hash[i].lock);
	}

	local_irq_restore(flags);

	return count;
}

static int dma_debug_device_change(struct notifier_block *nb,
				    unsigned long action, void *data)
{
	struct device *dev = data;
	int count;


	switch (action) {
	case BUS_NOTIFY_UNBOUND_DRIVER:
		count = device_dma_allocations(dev);
		if (count == 0)
			break;
		err_printk(dev, NULL, "DMA-API: device driver has pending "
				"DMA allocations while released from device "
				"[count=%d]\n", count);
		break;
	default:
		break;
	}

	return 0;
}

void dma_debug_add_bus(struct bus_type *bus)
{
	struct notifier_block *nb;

	nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
	if (nb == NULL) {
		pr_err("dma_debug_add_bus: out of memory\n");
		return;
	}

	nb->notifier_call = dma_debug_device_change;

	bus_register_notifier(bus, nb);
}

/*
 * Let the architectures decide how many entries should be preallocated.
 */
void dma_debug_init(u32 num_entries)
{
	int i;

	if (global_disable)
		return;

	for (i = 0; i < HASH_SIZE; ++i) {
		INIT_LIST_HEAD(&dma_entry_hash[i].list);
		spin_lock_init(&dma_entry_hash[i].lock);
	}

	if (dma_debug_fs_init() != 0) {
		pr_err("DMA-API: error creating debugfs entries - disabling\n");
		global_disable = true;

		return;
	}

	if (req_entries)
		num_entries = req_entries;

	if (prealloc_memory(num_entries) != 0) {
		pr_err("DMA-API: debugging out of memory error - disabled\n");
		global_disable = true;

		return;
	}

	nr_total_entries = num_free_entries;

	pr_info("DMA-API: debugging enabled by kernel config\n");
}

static __init int dma_debug_cmdline(char *str)
{
	if (!str)
		return -EINVAL;

	if (strncmp(str, "off", 3) == 0) {
		pr_info("DMA-API: debugging disabled on kernel command line\n");
		global_disable = true;
	}

	return 0;
}

static __init int dma_debug_entries_cmdline(char *str)
{
	int res;

	if (!str)
		return -EINVAL;

	res = get_option(&str, &req_entries);

	if (!res)
		req_entries = 0;

	return 0;
}

__setup("dma_debug=", dma_debug_cmdline);
__setup("dma_debug_entries=", dma_debug_entries_cmdline);

static void check_unmap(struct dma_debug_entry *ref)
{
	struct dma_debug_entry *entry;
	struct hash_bucket *bucket;
	unsigned long flags;

	if (dma_mapping_error(ref->dev, ref->dev_addr)) {
		err_printk(ref->dev, NULL, "DMA-API: device driver tries "
			   "to free an invalid DMA memory address\n");
		return;
	}

	bucket = get_hash_bucket(ref, &flags);
	entry = hash_bucket_find(bucket, ref);

	if (!entry) {
		err_printk(ref->dev, NULL, "DMA-API: device driver tries "
			   "to free DMA memory it has not allocated "
			   "[device address=0x%016llx] [size=%llu bytes]\n",
			   ref->dev_addr, ref->size);
		goto out;
	}

	if (ref->size != entry->size) {
		err_printk(ref->dev, entry, "DMA-API: device driver frees "
			   "DMA memory with different size "
			   "[device address=0x%016llx] [map size=%llu bytes] "
			   "[unmap size=%llu bytes]\n",
			   ref->dev_addr, entry->size, ref->size);
	}

	if (ref->type != entry->type) {
		err_printk(ref->dev, entry, "DMA-API: device driver frees "
			   "DMA memory with wrong function "
			   "[device address=0x%016llx] [size=%llu bytes] "
			   "[mapped as %s] [unmapped as %s]\n",
			   ref->dev_addr, ref->size,
			   type2name[entry->type], type2name[ref->type]);
	} else if ((entry->type == dma_debug_coherent) &&
		   (ref->paddr != entry->paddr)) {
		err_printk(ref->dev, entry, "DMA-API: device driver frees "
			   "DMA memory with different CPU address "
			   "[device address=0x%016llx] [size=%llu bytes] "
			   "[cpu alloc address=%p] [cpu free address=%p]",
			   ref->dev_addr, ref->size,
			   (void *)entry->paddr, (void *)ref->paddr);
	}

	if (ref->sg_call_ents && ref->type == dma_debug_sg &&
	    ref->sg_call_ents != entry->sg_call_ents) {
		err_printk(ref->dev, entry, "DMA-API: device driver frees "
			   "DMA sg list with different entry count "
			   "[map count=%d] [unmap count=%d]\n",
			   entry->sg_call_ents, ref->sg_call_ents);
	}

	/*
	 * This may be no bug in reality - but most implementations of the
	 * DMA API don't handle this properly, so check for it here
	 */
	if (ref->direction != entry->direction) {
		err_printk(ref->dev, entry, "DMA-API: device driver frees "
			   "DMA memory with different direction "
			   "[device address=0x%016llx] [size=%llu bytes] "
			   "[mapped with %s] [unmapped with %s]\n",
			   ref->dev_addr, ref->size,
			   dir2name[entry->direction],
			   dir2name[ref->direction]);
	}

	hash_bucket_del(entry);
	dma_entry_free(entry);

out:
	put_hash_bucket(bucket, &flags);
}

static void check_for_stack(struct device *dev, void *addr)
{
	if (object_is_on_stack(addr))
		err_printk(dev, NULL, "DMA-API: device driver maps memory from"
				"stack [addr=%p]\n", addr);
}

static inline bool overlap(void *addr, unsigned long len, void *start, void *end)
{
	unsigned long a1 = (unsigned long)addr;
	unsigned long b1 = a1 + len;
	unsigned long a2 = (unsigned long)start;
	unsigned long b2 = (unsigned long)end;

	return !(b1 <= a2 || a1 >= b2);
}

static void check_for_illegal_area(struct device *dev, void *addr, unsigned long len)
{
	if (overlap(addr, len, _text, _etext) ||
	    overlap(addr, len, __start_rodata, __end_rodata))
		err_printk(dev, NULL, "DMA-API: device driver maps memory from kernel text or rodata [addr=%p] [len=%lu]\n", addr, len);
}

static void check_sync(struct device *dev,
		       struct dma_debug_entry *ref,
		       bool to_cpu)
{
	struct dma_debug_entry *entry;
	struct hash_bucket *bucket;
	unsigned long flags;

	bucket = get_hash_bucket(ref, &flags);

	entry = hash_bucket_find(bucket, ref);

	if (!entry) {
		err_printk(dev, NULL, "DMA-API: device driver tries "
				"to sync DMA memory it has not allocated "
				"[device address=0x%016llx] [size=%llu bytes]\n",
				(unsigned long long)ref->dev_addr, ref->size);
		goto out;
	}

	if (ref->size > entry->size) {
		err_printk(dev, entry, "DMA-API: device driver syncs"
				" DMA memory outside allocated range "
				"[device address=0x%016llx] "
				"[allocation size=%llu bytes] "
				"[sync offset+size=%llu]\n",
				entry->dev_addr, entry->size,
				ref->size);
	}

	if (ref->direction != entry->direction) {
		err_printk(dev, entry, "DMA-API: device driver syncs "
				"DMA memory with different direction "
				"[device address=0x%016llx] [size=%llu bytes] "
				"[mapped with %s] [synced with %s]\n",
				(unsigned long long)ref->dev_addr, entry->size,
				dir2name[entry->direction],
				dir2name[ref->direction]);
	}

	if (entry->direction == DMA_BIDIRECTIONAL)
		goto out;

	if (to_cpu && !(entry->direction == DMA_FROM_DEVICE) &&
		      !(ref->direction == DMA_TO_DEVICE))
		err_printk(dev, entry, "DMA-API: device driver syncs "
				"device read-only DMA memory for cpu "
				"[device address=0x%016llx] [size=%llu bytes] "
				"[mapped with %s] [synced with %s]\n",
				(unsigned long long)ref->dev_addr, entry->size,
				dir2name[entry->direction],
				dir2name[ref->direction]);

	if (!to_cpu && !(entry->direction == DMA_TO_DEVICE) &&
		       !(ref->direction == DMA_FROM_DEVICE))
		err_printk(dev, entry, "DMA-API: device driver syncs "
				"device write-only DMA memory to device "
				"[device address=0x%016llx] [size=%llu bytes] "
				"[mapped with %s] [synced with %s]\n",
				(unsigned long long)ref->dev_addr, entry->size,
				dir2name[entry->direction],
				dir2name[ref->direction]);

out:
	put_hash_bucket(bucket, &flags);

}

void debug_dma_map_page(struct device *dev, struct page *page, size_t offset,
			size_t size, int direction, dma_addr_t dma_addr,
			bool map_single)
{
	struct dma_debug_entry *entry;

	if (unlikely(global_disable))
		return;

	if (unlikely(dma_mapping_error(dev, dma_addr)))
		return;

	entry = dma_entry_alloc();
	if (!entry)
		return;

	entry->dev       = dev;
	entry->type      = dma_debug_page;
	entry->paddr     = page_to_phys(page) + offset;
	entry->dev_addr  = dma_addr;
	entry->size      = size;
	entry->direction = direction;

	if (map_single)
		entry->type = dma_debug_single;

	if (!PageHighMem(page)) {
		void *addr = page_address(page) + offset;

		check_for_stack(dev, addr);
		check_for_illegal_area(dev, addr, size);
	}

	add_dma_entry(entry);
}
EXPORT_SYMBOL(debug_dma_map_page);

void debug_dma_unmap_page(struct device *dev, dma_addr_t addr,
			  size_t size, int direction, bool map_single)
{
	struct dma_debug_entry ref = {
		.type           = dma_debug_page,
		.dev            = dev,
		.dev_addr       = addr,
		.size           = size,
		.direction      = direction,
	};

	if (unlikely(global_disable))
		return;

	if (map_single)
		ref.type = dma_debug_single;

	check_unmap(&ref);
}
EXPORT_SYMBOL(debug_dma_unmap_page);

void debug_dma_map_sg(struct device *dev, struct scatterlist *sg,
		      int nents, int mapped_ents, int direction)
{
	struct dma_debug_entry *entry;
	struct scatterlist *s;
	int i;

	if (unlikely(global_disable))
		return;

	for_each_sg(sg, s, mapped_ents, i) {
		entry = dma_entry_alloc();
		if (!entry)
			return;

		entry->type           = dma_debug_sg;
		entry->dev            = dev;
		entry->paddr          = sg_phys(s);
		entry->size           = sg_dma_len(s);
		entry->dev_addr       = sg_dma_address(s);
		entry->direction      = direction;
		entry->sg_call_ents   = nents;
		entry->sg_mapped_ents = mapped_ents;

		if (!PageHighMem(sg_page(s))) {
			check_for_stack(dev, sg_virt(s));
			check_for_illegal_area(dev, sg_virt(s), sg_dma_len(s));
		}

		add_dma_entry(entry);
	}
}
EXPORT_SYMBOL(debug_dma_map_sg);

static int get_nr_mapped_entries(struct device *dev,
				 struct dma_debug_entry *ref)
{
	struct dma_debug_entry *entry;
	struct hash_bucket *bucket;
	unsigned long flags;
	int mapped_ents;

	bucket       = get_hash_bucket(ref, &flags);
	entry        = hash_bucket_find(bucket, ref);
	mapped_ents  = 0;

	if (entry)
		mapped_ents = entry->sg_mapped_ents;
	put_hash_bucket(bucket, &flags);

	return mapped_ents;
}

void debug_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
			int nelems, int dir)
{
	struct scatterlist *s;
	int mapped_ents = 0, i;

	if (unlikely(global_disable))
		return;

	for_each_sg(sglist, s, nelems, i) {

		struct dma_debug_entry ref = {
			.type           = dma_debug_sg,
			.dev            = dev,
			.paddr          = sg_phys(s),
			.dev_addr       = sg_dma_address(s),
			.size           = sg_dma_len(s),
			.direction      = dir,
			.sg_call_ents   = nelems,
		};

		if (mapped_ents && i >= mapped_ents)
			break;

		if (!i)
			mapped_ents = get_nr_mapped_entries(dev, &ref);

		check_unmap(&ref);
	}
}
EXPORT_SYMBOL(debug_dma_unmap_sg);

void debug_dma_alloc_coherent(struct device *dev, size_t size,
			      dma_addr_t dma_addr, void *virt)
{
	struct dma_debug_entry *entry;

	if (unlikely(global_disable))
		return;

	if (unlikely(virt == NULL))
		return;

	entry = dma_entry_alloc();
	if (!entry)
		return;

	entry->type      = dma_debug_coherent;
	entry->dev       = dev;
	entry->paddr     = virt_to_phys(virt);
	entry->size      = size;
	entry->dev_addr  = dma_addr;
	entry->direction = DMA_BIDIRECTIONAL;

	add_dma_entry(entry);
}
EXPORT_SYMBOL(debug_dma_alloc_coherent);

void debug_dma_free_coherent(struct device *dev, size_t size,
			 void *virt, dma_addr_t addr)
{
	struct dma_debug_entry ref = {
		.type           = dma_debug_coherent,
		.dev            = dev,
		.paddr          = virt_to_phys(virt),
		.dev_addr       = addr,
		.size           = size,
		.direction      = DMA_BIDIRECTIONAL,
	};

	if (unlikely(global_disable))
		return;

	check_unmap(&ref);
}
EXPORT_SYMBOL(debug_dma_free_coherent);

void debug_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
				   size_t size, int direction)
{
	struct dma_debug_entry ref;

	if (unlikely(global_disable))
		return;

	ref.type         = dma_debug_single;
	ref.dev          = dev;
	ref.dev_addr     = dma_handle;
	ref.size         = size;
	ref.direction    = direction;
	ref.sg_call_ents = 0;

	check_sync(dev, &ref, true);
}
EXPORT_SYMBOL(debug_dma_sync_single_for_cpu);

void debug_dma_sync_single_for_device(struct device *dev,
				      dma_addr_t dma_handle, size_t size,
				      int direction)
{
	struct dma_debug_entry ref;

	if (unlikely(global_disable))
		return;

	ref.type         = dma_debug_single;
	ref.dev          = dev;
	ref.dev_addr     = dma_handle;
	ref.size         = size;
	ref.direction    = direction;
	ref.sg_call_ents = 0;

	check_sync(dev, &ref, false);
}
EXPORT_SYMBOL(debug_dma_sync_single_for_device);

void debug_dma_sync_single_range_for_cpu(struct device *dev,
					 dma_addr_t dma_handle,
					 unsigned long offset, size_t size,
					 int direction)
{
	struct dma_debug_entry ref;

	if (unlikely(global_disable))
		return;

	ref.type         = dma_debug_single;
	ref.dev          = dev;
	ref.dev_addr     = dma_handle;
	ref.size         = offset + size;
	ref.direction    = direction;
	ref.sg_call_ents = 0;

	check_sync(dev, &ref, true);
}
EXPORT_SYMBOL(debug_dma_sync_single_range_for_cpu);

void debug_dma_sync_single_range_for_device(struct device *dev,
					    dma_addr_t dma_handle,
					    unsigned long offset,
					    size_t size, int direction)
{
	struct dma_debug_entry ref;

	if (unlikely(global_disable))
		return;

	ref.type         = dma_debug_single;
	ref.dev          = dev;
	ref.dev_addr     = dma_handle;
	ref.size         = offset + size;
	ref.direction    = direction;
	ref.sg_call_ents = 0;

	check_sync(dev, &ref, false);
}
EXPORT_SYMBOL(debug_dma_sync_single_range_for_device);

void debug_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
			       int nelems, int direction)
{
	struct scatterlist *s;
	int mapped_ents = 0, i;

	if (unlikely(global_disable))
		return;

	for_each_sg(sg, s, nelems, i) {

		struct dma_debug_entry ref = {
			.type           = dma_debug_sg,
			.dev            = dev,
			.paddr          = sg_phys(s),
			.dev_addr       = sg_dma_address(s),
			.size           = sg_dma_len(s),
			.direction      = direction,
			.sg_call_ents   = nelems,
		};

		if (!i)
			mapped_ents = get_nr_mapped_entries(dev, &ref);

		if (i >= mapped_ents)
			break;

		check_sync(dev, &ref, true);
	}
}
EXPORT_SYMBOL(debug_dma_sync_sg_for_cpu);

void debug_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
				  int nelems, int direction)
{
	struct scatterlist *s;
	int mapped_ents = 0, i;

	if (unlikely(global_disable))
		return;

	for_each_sg(sg, s, nelems, i) {

		struct dma_debug_entry ref = {
			.type           = dma_debug_sg,
			.dev            = dev,
			.paddr          = sg_phys(s),
			.dev_addr       = sg_dma_address(s),
			.size           = sg_dma_len(s),
			.direction      = direction,
			.sg_call_ents   = nelems,
		};
		if (!i)
			mapped_ents = get_nr_mapped_entries(dev, &ref);

		if (i >= mapped_ents)
			break;

		check_sync(dev, &ref, false);
	}
}
EXPORT_SYMBOL(debug_dma_sync_sg_for_device);

static int __init dma_debug_driver_setup(char *str)
{
	int i;

	for (i = 0; i < NAME_MAX_LEN - 1; ++i, ++str) {
		current_driver_name[i] = *str;
		if (*str == 0)
			break;
	}

	if (current_driver_name[0])
		pr_info("DMA-API: enable driver filter for driver [%s]\n",
			current_driver_name);


	return 1;
}
__setup("dma_debug_driver=", dma_debug_driver_setup);
id='n4792' href='#n4792'>4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516
/* Copyright(c) 2000, Compaq Computer Corporation 
 * Fibre Channel Host Bus Adapter 
 * 64-bit, 66MHz PCI 
 * Originally developed and tested on:
 * (front): [chip] Tachyon TS HPFC-5166A/1.2  L2C1090 ...
 *          SP# P225CXCBFIEL6T, Rev XC
 *          SP# 161290-001, Rev XD
 * (back): Board No. 010008-001 A/W Rev X5, FAB REV X5
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * Written by Don Zimmerman
*/

#include <linux/sched.h>
#include <linux/timer.h>
#include <linux/string.h>
#include <linux/slab.h>
#include <linux/ioport.h>
#include <linux/kernel.h>
#include <linux/stat.h>
#include <linux/blkdev.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/smp_lock.h>
#include <linux/pci.h>

#define SHUTDOWN_SIGS	(sigmask(SIGKILL)|sigmask(SIGINT)|sigmask(SIGTERM))

#include <asm/system.h>
#include <asm/irq.h>
#include <asm/dma.h>

#include "scsi.h"
#include <scsi/scsi_host.h>   // struct Scsi_Host definition for T handler
#include "cpqfcTSchip.h"
#include "cpqfcTSstructs.h"
#include "cpqfcTStrigger.h"

//#define LOGIN_DBG 1

// REMARKS:
// Since Tachyon chips may be permitted to wait from 500ms up to 2 sec
// to empty an outgoing frame from its FIFO to the Fibre Channel stream,
// we cannot do everything we need to in the interrupt handler.  Specifically,
// every time a link re-init (e.g. LIP) takes place, all SCSI I/O has to be
// suspended until the login sequences have been completed.  Login commands
// are frames just like SCSI commands are frames; they are subject to the same
// timeout issues and delays.  Also, various specs provide up to 2 seconds for
// devices to log back in (i.e. respond with ACC to a login frame), so I/O to
// that device has to be suspended.
// A serious problem here occurs on highly loaded FC-AL systems.  If our FC port
// has a low priority (e.g. high arbitrated loop physical address, alpa), and
// some other device is hogging bandwidth (permissible under FC-AL), we might
// time out thinking the link is hung, when it's simply busy.  Many such
// considerations complicate the design.  Although Tachyon assumes control
// (in silicon) for many link-specific issues, the Linux driver is left with the
// rest, which turns out to be a difficult, time critical chore.

// These "worker" functions will handle things like FC Logins; all
// processes with I/O to our device must wait for the Login to complete
// and (if successful) I/O to resume.  In the event of a malfunctioning or  
// very busy loop, it may take hundreds of millisecs or even seconds to complete
// a frame send.  We don't want to hang up the entire server (and all
// processes which don't depend on Fibre) during this wait.

// The Tachyon chip can have around 30,000 I/O operations ("exchanges")
// open at one time.  However, each exchange must be initiated 
// synchronously (i.e. each of the 30k I/O had to be started one at a
// time by sending a starting frame via Tachyon's outbound que).  

// To accommodate kernel "module" build, this driver limits the exchanges
// to 256, because of the contiguous physical memory limitation of 128M.

// Typical FC Exchanges are opened presuming the FC frames start without errors,
// while Exchange completion is handled in the interrupt handler.  This
// optimizes performance for the "everything's working" case.
// However, when we have FC related errors or hot plugging of FC ports, we pause
// I/O and handle FC-specific tasks in the worker thread.  These FC-specific
// functions will handle things like FC Logins and Aborts.  As the Login sequence
// completes to each and every target, I/O can resume to that target.  

// Our kernel "worker thread" must share the HBA with threads calling 
// "queuecommand".  We define a "BoardLock" semaphore which indicates
// to "queuecommand" that the HBA is unavailable, and Cmnds are added to a
// board lock Q.  When the worker thread finishes with the board, the board
// lock Q commands are completed with status causing immediate retry.
// Typically, the board is locked while Logins are in progress after an
// FC Link Down condition.  When Cmnds are re-queued after board lock, the
// particular Scsi channel/target may or may not have logged back in.  When
// the device is waiting for login, the "prli" flag is clear, in which case
// commands are passed to a Link Down Q.  Whenever the login finally completes,
// the LinkDown Q is completed, again with status causing immediate retry.
// When FC devices are logged in, we build and start FC commands to the
// devices.

// NOTE!! As of May 2000, kernel 2.2.14, the error recovery logic for devices 
// that never log back in (e.g. physically removed) is NOT completely
// understood.  I've still seen instances of system hangs on failed Write 
// commands (possibly from the ext2 layer?) on device removal.  Such special
// cases need to be evaluated from a system/application view - e.g., how
// exactly does the system want me to complete commands when the device is
// physically removed??

// local functions

static void SetLoginFields(
  PFC_LOGGEDIN_PORT pLoggedInPort,
  TachFCHDR_GCMND* fchs,
  BOOLEAN PDisc,
  BOOLEAN Originator);

static void AnalyzeIncomingFrame( 
       CPQFCHBA *cpqfcHBAdata,
       ULONG QNdx );

static void SendLogins( CPQFCHBA *cpqfcHBAdata, __u32 *FabricPortIds );

static int verify_PLOGI( PTACHYON fcChip,
      TachFCHDR_GCMND* fchs, ULONG* reject_explain);
static int verify_PRLI( TachFCHDR_GCMND* fchs, ULONG* reject_explain);

static void LoadWWN( PTACHYON fcChip, UCHAR* dest, UCHAR type);
static void BuildLinkServicePayload( 
              PTACHYON fcChip, ULONG type, void* payload);

static void UnblockScsiDevice( struct Scsi_Host *HostAdapter, 
        PFC_LOGGEDIN_PORT pLoggedInPort);

static void cpqfcTSCheckandSnoopFCP( PTACHYON fcChip, ULONG x_ID);

static void CompleteBoardLockCmnd( CPQFCHBA *cpqfcHBAdata);

static void RevalidateSEST( struct Scsi_Host *HostAdapter, 
		        PFC_LOGGEDIN_PORT pLoggedInPort);

static void IssueReportLunsCommand( 
              CPQFCHBA* cpqfcHBAdata, 
	      TachFCHDR_GCMND* fchs);

// (see scsi_error.c comments on kernel task creation)

void cpqfcTSWorkerThread( void *host)
{
  struct Scsi_Host *HostAdapter = (struct Scsi_Host*)host;
  CPQFCHBA *cpqfcHBAdata = (CPQFCHBA *)HostAdapter->hostdata; 
#ifdef PCI_KERNEL_TRACE
  PTACHYON fcChip = &cpqfcHBAdata->fcChip;
#endif
  DECLARE_MUTEX_LOCKED(fcQueReady);
  DECLARE_MUTEX_LOCKED(fcTYOBcomplete); 
  DECLARE_MUTEX_LOCKED(TachFrozen);  
  DECLARE_MUTEX_LOCKED(BoardLock);  

  ENTER("WorkerThread");

  lock_kernel();
  daemonize("cpqfcTS_wt_%d", HostAdapter->host_no);
  siginitsetinv(&current->blocked, SHUTDOWN_SIGS);


  cpqfcHBAdata->fcQueReady = &fcQueReady;  // primary wait point
  cpqfcHBAdata->TYOBcomplete = &fcTYOBcomplete;
  cpqfcHBAdata->TachFrozen = &TachFrozen;
    
 
  cpqfcHBAdata->worker_thread = current;
  
  unlock_kernel();

  if( cpqfcHBAdata->notify_wt != NULL )
    up( cpqfcHBAdata->notify_wt); // OK to continue

  while(1)
  {
    unsigned long flags;

    down_interruptible( &fcQueReady);  // wait for something to do

    if (signal_pending(current) )
      break;
    
    PCI_TRACE( 0x90)
    // first, take the IO lock so the SCSI upper layers can't call
    // into our _quecommand function (this also disables INTs)
    spin_lock_irqsave( HostAdapter->host_lock, flags); // STOP _que function
    PCI_TRACE( 0x90)
         
    CPQ_SPINLOCK_HBA( cpqfcHBAdata)
    // next, set this pointer to indicate to the _quecommand function
    // that the board is in use, so it should que the command and 
    // immediately return (we don't actually require the semaphore function
    // in this driver rev)

    cpqfcHBAdata->BoardLock = &BoardLock;

    PCI_TRACE( 0x90)

    // release the IO lock (and re-enable interrupts)
    spin_unlock_irqrestore( HostAdapter->host_lock, flags);

    // disable OUR HBA interrupt (keep them off as much as possible
    // during error recovery)
    disable_irq( cpqfcHBAdata->HostAdapter->irq);

    // OK, let's process the Fibre Channel Link Q and do the work
    cpqfcTS_WorkTask( HostAdapter);

    // hopefully, no more "work" to do;
    // re-enable our INTs for "normal" completion processing
    enable_irq( cpqfcHBAdata->HostAdapter->irq);
 

    cpqfcHBAdata->BoardLock = NULL; // allow commands to be queued
    CPQ_SPINUNLOCK_HBA( cpqfcHBAdata)


    // Now, complete any Cmnd we Q'd up while BoardLock was held

    CompleteBoardLockCmnd( cpqfcHBAdata);
  

  }
  // hopefully, the signal was for our module exit...
  if( cpqfcHBAdata->notify_wt != NULL )
    up( cpqfcHBAdata->notify_wt); // yep, we're outta here
}


// Freeze Tachyon routine.
// If Tachyon is already frozen, return FALSE
// If Tachyon is not frozen, call freeze function, return TRUE
//
static BOOLEAN FreezeTach( CPQFCHBA *cpqfcHBAdata)
{
  PTACHYON fcChip = &cpqfcHBAdata->fcChip;
  BOOLEAN FrozeTach = FALSE;
  // It's possible that the chip is already frozen; if so,
  // "Freezing" again will NOT! generate another Freeze
  // Completion Message.

  if( (fcChip->Registers.TYstatus.value & 0x70000) != 0x70000)
  {  // (need to freeze...)
    fcChip->FreezeTachyon( fcChip, 2);  // both ERQ and FCP assists

    // 2. Get Tach freeze confirmation
    // (synchronize SEST manipulation with Freeze Completion Message)
    // we need INTs on so semaphore can be set.	
    enable_irq( cpqfcHBAdata->HostAdapter->irq); // only way to get Semaphore
    down_interruptible( cpqfcHBAdata->TachFrozen); // wait for INT handler sem.
    // can we TIMEOUT semaphore wait?? TBD
    disable_irq( cpqfcHBAdata->HostAdapter->irq); 

    FrozeTach = TRUE;
  }  // (else, already frozen)
 
  return FrozeTach;
}  




// This is the kernel worker thread task, which processes FC
// tasks which were queued by the Interrupt handler or by
// other WorkTask functions.

#define DBG 1
//#undef DBG
void cpqfcTS_WorkTask( struct Scsi_Host *HostAdapter)
{
  CPQFCHBA *cpqfcHBAdata = (CPQFCHBA *)HostAdapter->hostdata;
  PTACHYON fcChip = &cpqfcHBAdata->fcChip;
  FC_EXCHANGES *Exchanges = fcChip->Exchanges;
  ULONG QconsumerNdx;
  LONG ExchangeID;
  ULONG ulStatus=0;
  TachFCHDR_GCMND fchs;
  PFC_LINK_QUE fcLQ = cpqfcHBAdata->fcLQ;

  ENTER("WorkTask");

  // copy current index to work on
  QconsumerNdx = fcLQ->consumer;

  PCI_TRACEO( fcLQ->Qitem[QconsumerNdx].Type, 0x90)
  

  // NOTE: when this switch completes, we will "consume" the Que item
//  printk("Que type %Xh\n", fcLQ->Qitem[QconsumerNdx].Type);
  switch( fcLQ->Qitem[QconsumerNdx].Type )
  {
      // incoming frame - link service (ACC, UNSOL REQ, etc.)
      // or FCP-SCSI command
    case SFQ_UNKNOWN:  
      AnalyzeIncomingFrame( cpqfcHBAdata, QconsumerNdx );

      break;
  
    
    
    case EXCHANGE_QUEUED:  // an Exchange (i.e. FCP-SCSI) was previously
                           // Queued because the link was down.  The  
                           // heartbeat timer detected it and Queued it here.
                           // We attempt to start it again, and if
                           // successful we clear the EXCHANGE_Q flag.
                           // If the link doesn't come up, the Exchange
                           // will eventually time-out.

      ExchangeID = (LONG)  // x_ID copied from DPC timeout function
                   fcLQ->Qitem[QconsumerNdx].ulBuff[0];

      // It's possible that a Q'd exchange could have already
      // been started by other logic (e.g. ABTS process)
      // Don't start if already started (Q'd flag clear)

      if( Exchanges->fcExchange[ExchangeID].status & EXCHANGE_QUEUED )
      {
//        printk(" *Start Q'd x_ID %Xh: type %Xh ", 
//          ExchangeID, Exchanges->fcExchange[ExchangeID].type);
      
        ulStatus = cpqfcTSStartExchange( cpqfcHBAdata, ExchangeID);
        if( !ulStatus )
        {
//          printk("success* ");
        }	
        else
        {
#ifdef DBG
      
          if( ulStatus == EXCHANGE_QUEUED)
            printk("Queued* ");
          else
            printk("failed* ");
		
#endif
	} 
      }
      break;


    case LINKDOWN:  
      // (lots of things already done in INT handler) future here?
      break;
    
    
    case LINKACTIVE:   // Tachyon set the Lup bit in FM status
                       // NOTE: some misbehaving FC ports (like Tach2.1)
                       // can re-LIP immediately after a LIP completes.
      
      // if "initiator", need to verify LOGs with ports
//      printk("\n*LNKUP* ");

      if( fcChip->Options.initiator )
        SendLogins( cpqfcHBAdata, NULL ); // PLOGI or PDISC, based on fcPort data
                  // if SendLogins successfully completes, PortDiscDone
                  // will be set.
      
      
      // If SendLogins was successful, then we expect to get incoming
      // ACCepts or REJECTs, which are handled below.

      break;

    // LinkService and Fabric request/reply processing
    case ELS_FDISC:      // need to send Fabric Discovery (Login)
    case ELS_FLOGI:      // need to send Fabric Login
    case ELS_SCR:        // need to send State Change Registration
    case FCS_NSR:        // need to send Name Service Request
    case ELS_PLOGI:      // need to send PLOGI
    case ELS_ACC:        // send generic ACCept
    case ELS_PLOGI_ACC:  // need to send ELS ACCept frame to recv'd PLOGI
    case ELS_PRLI_ACC:   // need to send ELS ACCept frame to recv'd PRLI
    case ELS_LOGO:      // need to send ELS LOGO (logout)
    case ELS_LOGO_ACC:  // need to send ELS ACCept frame to recv'd PLOGI
    case ELS_RJT:         // ReJecT reply
    case ELS_PRLI:       // need to send ELS PRLI
 
    
//      printk(" *ELS %Xh* ", fcLQ->Qitem[QconsumerNdx].Type);
      // if PortDiscDone is not set, it means the SendLogins routine
      // failed to complete -- assume that LDn occurred, so login frames
      // are invalid
      if( !cpqfcHBAdata->PortDiscDone) // cleared by LDn
      {
        printk("Discard Q'd ELS login frame\n");
        break;  
      }

      ulStatus = cpqfcTSBuildExchange(
          cpqfcHBAdata,
          fcLQ->Qitem[QconsumerNdx].Type, // e.g. PLOGI
          (TachFCHDR_GCMND*)
            fcLQ->Qitem[QconsumerNdx].ulBuff, // incoming fchs
          NULL,         // no data (no scatter/gather list)
          &ExchangeID );// fcController->fcExchanges index, -1 if failed

      if( !ulStatus ) // Exchange setup?
      {
        ulStatus = cpqfcTSStartExchange( cpqfcHBAdata, ExchangeID );
        if( !ulStatus )
        {
          // submitted to Tach's Outbound Que (ERQ PI incremented)
          // waited for completion for ELS type (Login frames issued
          // synchronously)
        }
        else
          // check reason for Exchange not being started - we might
          // want to Queue and start later, or fail with error
        {

        }
      }

      else   // Xchange setup failed...
        printk(" cpqfcTSBuildExchange failed: %Xh\n", ulStatus );

      break;

    case SCSI_REPORT_LUNS:
      // pass the incoming frame (actually, it's a PRLI frame)
      // so we can send REPORT_LUNS, in order to determine VSA/PDU
      // FCP-SCSI Lun address mode
      IssueReportLunsCommand( cpqfcHBAdata, (TachFCHDR_GCMND*)
            fcLQ->Qitem[QconsumerNdx].ulBuff); 

      break;
      



    case BLS_ABTS:       // need to ABORT one or more exchanges
    {
      LONG x_ID = fcLQ->Qitem[QconsumerNdx].ulBuff[0];
      BOOLEAN FrozeTach = FALSE;   
     
      if ( x_ID >= TACH_SEST_LEN )  // (in)sanity check
      {
//	printk( " cpqfcTS ERROR! BOGUS x_ID %Xh", x_ID);
	break;
      }


      if( Exchanges->fcExchange[ x_ID].Cmnd == NULL ) // should be RARE
      {
//	printk(" ABTS %Xh Scsi Cmnd null! ", x_ID);
	
       break;  // nothing to abort!
      }

//#define ABTS_DBG
#ifdef ABTS_DBG
      printk("INV SEST[%X] ", x_ID); 
      if( Exchanges->fcExchange[x_ID].status & FC2_TIMEOUT)
      {
        printk("FC2TO");
      }
      if( Exchanges->fcExchange[x_ID].status & INITIATOR_ABORT)
      {
        printk("IA");
      }
      if( Exchanges->fcExchange[x_ID].status & PORTID_CHANGED)
      {
        printk("PORTID");
      }
      if( Exchanges->fcExchange[x_ID].status & DEVICE_REMOVED)
      {
        printk("DEVRM");
      }
      if( Exchanges->fcExchange[x_ID].status & LINKFAIL_TX)
      {
        printk("LKF");
      }
      if( Exchanges->fcExchange[x_ID].status & FRAME_TO)
      {
        printk("FRMTO");
      }
      if( Exchanges->fcExchange[x_ID].status & ABORTSEQ_NOTIFY)
      {
        printk("ABSQ");
      }
      if( Exchanges->fcExchange[x_ID].status & SFQ_FRAME)
      {
        printk("SFQFR");
      }

      if( Exchanges->fcExchange[ x_ID].type == 0x2000)
        printk(" WR");
      else if( Exchanges->fcExchange[ x_ID].type == 0x3000)
        printk(" RD");
      else if( Exchanges->fcExchange[ x_ID].type == 0x10)
        printk(" ABTS");
      else
        printk(" %Xh", Exchanges->fcExchange[ x_ID].type); 

      if( !(Exchanges->fcExchange[x_ID].status & INITIATOR_ABORT))
      {
	printk(" Cmd %p, ", 
          Exchanges->fcExchange[ x_ID].Cmnd);

        printk(" brd/chn/trg/lun %d/%d/%d/%d port_id %06X\n", 
          cpqfcHBAdata->HBAnum,
          Exchanges->fcExchange[ x_ID].Cmnd->channel,
          Exchanges->fcExchange[ x_ID].Cmnd->target,
          Exchanges->fcExchange[ x_ID].Cmnd->lun,
          Exchanges->fcExchange[ x_ID].fchs.d_id & 0xFFFFFF);
      }
      else  // assume that Cmnd ptr is invalid on _abort()
      {
	printk(" Cmd ptr invalid\n");
      }
     
#endif      

      
      // Steps to ABORT a SEST exchange:
      // 1. Freeze TL SCSI assists & ERQ (everything)
      // 2. Receive FROZEN inbound CM (must succeed!)
      // 3. Invalidate x_ID SEST entry 
      // 4. Resume TL SCSI assists & ERQ (everything)
      // 5. Build/start on exchange - change "type" to BLS_ABTS,
      //    timeout to X sec (RA_TOV from PLDA is actually 0)
      // 6. Set Exchange Q'd status if ABTS cannot be started,
      //    or simply complete Exchange in "Terminate" condition

  PCI_TRACEO( x_ID, 0xB4)
      
      // 1 & 2 . Freeze Tach & get confirmation of freeze
      FrozeTach = FreezeTach( cpqfcHBAdata);

      // 3. OK, Tachyon is frozen, so we can invalidate SEST exchange.
      // FC2_TIMEOUT means we are originating the abort, while
      // TARGET_ABORT means we are ACCepting an abort.
      // LINKFAIL_TX, ABORTSEQ_NOFITY, INV_ENTRY or FRAME_TO are 
      // all from Tachyon:
      // Exchange was corrupted by LDn or other FC physical failure
      // INITIATOR_ABORT means the upper layer driver/application
      // requested the abort.


	  
      // clear bit 31 (VALid), to invalidate & take control from TL
      fcChip->SEST->u[ x_ID].IWE.Hdr_Len &= 0x7FFFFFFF;


      // examine and Tach's "Linked List" for IWEs that 
      // received (nearly) simultaneous transfer ready (XRDY) 
      // repair linked list if necessary (TBD!)
      // (If we ignore the "Linked List", we will time out
      // WRITE commands where we received the FCP-SCSI XFRDY
      // frame (because Tachyon didn't processes it).  Linked List
      // management should be done as an optimization.

//       readl( fcChip->Registers.ReMapMemBase+TL_MEM_SEST_LINKED_LIST ));


      

      // 4. Resume all Tachlite functions (for other open Exchanges)
      // as quickly as possible to allow other exchanges to other ports
      // to resume.  Freezing Tachyon may cause cascading errors, because
      // any received SEST frame cannot be processed by the SEST.
      // Don't "unfreeze" unless Link is operational
      if( FrozeTach )  // did we just freeze it (above)?
        fcChip->UnFreezeTachyon( fcChip, 2);  // both ERQ and FCP assists
      

  PCI_TRACEO( x_ID, 0xB4)

      // Note there is no confirmation that the chip is "unfrozen".  Also,
      // if the Link is down when unfreeze is called, it has no effect.
      // Chip will unfreeze when the Link is back up.

      // 5. Now send out Abort commands if possible
      // Some Aborts can't be "sent" (Port_id changed or gone);
      // if the device is gone, there is no port_id to send the ABTS to.

      if( !(Exchanges->fcExchange[ x_ID].status & PORTID_CHANGED)
			  &&
          !(Exchanges->fcExchange[ x_ID].status & DEVICE_REMOVED) )
      {
        Exchanges->fcExchange[ x_ID].type = BLS_ABTS;
        fchs.s_id = Exchanges->fcExchange[ x_ID].fchs.d_id;
        ulStatus = cpqfcTSBuildExchange(
          cpqfcHBAdata,
          BLS_ABTS,
          &fchs,        // (uses only s_id)
          NULL,         // (no scatter/gather list for ABTS)
          &x_ID );// ABTS on this Exchange ID

        if( !ulStatus ) // Exchange setup build OK?
        {

            // ABTS may be needed because an Exchange was corrupted
            // by a Link disruption.  If the Link is UP, we can
	    // presume that this ABTS can start immediately; otherwise,
	    // set Que'd status so the Login functions
            // can restart it when the FC physical Link is restored
          if( ((fcChip->Registers.FMstatus.value &0xF0) &0x80)) // loop init?
          {			    
//                printk(" *set Q status x_ID %Xh on LDn* ", x_ID);
                Exchanges->fcExchange[ x_ID].status |= EXCHANGE_QUEUED;
          }

          else  // what FC device (port_id) does the Cmd belong to?
          {
            PFC_LOGGEDIN_PORT pLoggedInPort = 
              Exchanges->fcExchange[ x_ID].pLoggedInPort;
            
            // if Port is logged in, we might start the abort.
	
            if( (pLoggedInPort != NULL) 
			      &&
                (pLoggedInPort->prli == TRUE) ) 
            {
              // it's possible that an Exchange has already been Queued
              // to start after Login completes.  Check and don't
	      // start it (again) here if Q'd status set
//	    printk(" ABTS xchg %Xh ", x_ID);            
 	      if( Exchanges->fcExchange[x_ID].status & EXCHANGE_QUEUED)
	      {
//		    printk("already Q'd ");
	      }
	      else
	      {
//	            printk("starting ");
		
                fcChip->fcStats.FC2aborted++; 
                ulStatus = cpqfcTSStartExchange( cpqfcHBAdata, x_ID );
                if( !ulStatus )
                {
                    // OK
                    // submitted to Tach's Outbound Que (ERQ PI incremented)
                }
                else
                {
/*                   printk("ABTS exchange start failed -status %Xh, x_ID %Xh ",
                        ulStatus, x_ID);
*/
                } 
	      }
	    }
	    else
	    {
/*         	  printk(" ABTS NOT starting xchg %Xh, %p ",
			       x_ID, pLoggedInPort);
	          if( pLoggedInPort )
	            printk("prli %d ", pLoggedInPort->prli);
*/
	    }		
 	  }
        }
        else  // what the #@!
        {  // how do we fail to build an Exchange for ABTS??
              printk("ABTS exchange build failed -status %Xh, x_ID %Xh\n",
                ulStatus, x_ID);
        }
      }
      else   // abort without ABTS -- just complete exchange/Cmnd to Linux
      {
//            printk(" *Terminating x_ID %Xh on %Xh* ", 
//		    x_ID, Exchanges->fcExchange[x_ID].status);
        cpqfcTSCompleteExchange( cpqfcHBAdata->PciDev, fcChip, x_ID);

      }
    } // end of ABTS case
      break;



    case BLS_ABTS_ACC:   // need to ACCept one ABTS
                         // (NOTE! this code not updated for Linux yet..)
      

      printk(" *ABTS_ACC* ");
      // 1. Freeze TL

      fcChip->FreezeTachyon( fcChip, 2);  // both ERQ and FCP assists

      memcpy(  // copy the incoming ABTS frame
        &fchs,
        fcLQ->Qitem[QconsumerNdx].ulBuff, // incoming fchs
        sizeof( fchs));

      // 3. OK, Tachyon is frozen so we can invalidate SEST entry 
      // (if necessary)
      // Status FC2_TIMEOUT means we are originating the abort, while
      // TARGET_ABORT means we are ACCepting an abort
      
      ExchangeID = fchs.ox_rx_id & 0x7FFF; // RX_ID for exchange
//      printk("ABTS ACC for Target ExchangeID %Xh\n", ExchangeID);


      // sanity check on received ExchangeID
      if( Exchanges->fcExchange[ ExchangeID].status == TARGET_ABORT )
      {
          // clear bit 31 (VALid), to invalidate & take control from TL
//          printk("Invalidating SEST exchange %Xh\n", ExchangeID);
          fcChip->SEST->u[ ExchangeID].IWE.Hdr_Len &= 0x7FFFFFFF;
      }


      // 4. Resume all Tachlite functions (for other open Exchanges)
      // as quickly as possible to allow other exchanges to other ports
      // to resume.  Freezing Tachyon for too long may royally screw
      // up everything!
      fcChip->UnFreezeTachyon( fcChip, 2);  // both ERQ and FCP assists
      
      // Note there is no confirmation that the chip is "unfrozen".  Also,
      // if the Link is down when unfreeze is called, it has no effect.
      // Chip will unfreeze when the Link is back up.

      // 5. Now send out Abort ACC reply for this exchange
      Exchanges->fcExchange[ ExchangeID].type = BLS_ABTS_ACC;
      
      fchs.s_id = Exchanges->fcExchange[ ExchangeID].fchs.d_id;
      ulStatus = cpqfcTSBuildExchange(
            cpqfcHBAdata,
            BLS_ABTS_ACC,
            &fchs,
            NULL,         // no data (no scatter/gather list)
            &ExchangeID );// fcController->fcExchanges index, -1 if failed

      if( !ulStatus ) // Exchange setup?
      {
        ulStatus = cpqfcTSStartExchange( cpqfcHBAdata, ExchangeID );
        if( !ulStatus )
        {
          // submitted to Tach's Outbound Que (ERQ PI incremented)
          // waited for completion for ELS type (Login frames issued
          // synchronously)
        }
        else
          // check reason for Exchange not being started - we might
          // want to Queue and start later, or fail with error
        {

        } 
      }
      break;


    case BLS_ABTS_RJT:   // need to ReJecT one ABTS; reject implies the
                         // exchange doesn't exist in the TARGET context.
                         // ExchangeID has to come from LinkService space.

      printk(" *ABTS_RJT* ");
      ulStatus = cpqfcTSBuildExchange(
            cpqfcHBAdata,
            BLS_ABTS_RJT,
            (TachFCHDR_GCMND*)
              fcLQ->Qitem[QconsumerNdx].ulBuff, // incoming fchs
            NULL,         // no data (no scatter/gather list)
            &ExchangeID );// fcController->fcExchanges index, -1 if failed

      if( !ulStatus ) // Exchange setup OK?
      {
        ulStatus = cpqfcTSStartExchange( cpqfcHBAdata, ExchangeID );
        // If it fails, we aren't required to retry.
      }
      if( ulStatus )
      {
        printk("Failed to send BLS_RJT for ABTS, X_ID %Xh\n", ExchangeID);
      }
      else
      {
        printk("Sent BLS_RJT for ABTS, X_ID %Xh\n", ExchangeID);
      
      }

      break;



    default:
      break;
  }                   // end switch
//doNothing:
    // done with this item - now set the NEXT index

  if( QconsumerNdx+1 >= FC_LINKQ_DEPTH ) // rollover test
  {
    fcLQ->consumer = 0;
  }
  else
  { 
    fcLQ->consumer++;
  }

  PCI_TRACEO( fcLQ->Qitem[QconsumerNdx].Type, 0x94)

  LEAVE("WorkTask");
  return;
}




// When Tachyon reports link down, bad al_pa, or Link Service (e.g. Login)
// commands come in, post to the LinkQ so that action can be taken outside the
// interrupt handler.  
// This circular Q works like Tachyon's que - the producer points to the next
// (unused) entry.  Called by Interrupt handler, WorkerThread, Timer
// sputlinkq
void cpqfcTSPutLinkQue( CPQFCHBA *cpqfcHBAdata,
  int Type, 
  void *QueContent)
{
  PTACHYON fcChip = &cpqfcHBAdata->fcChip;
//  FC_EXCHANGES *Exchanges = fcChip->Exchanges;
  PFC_LINK_QUE fcLQ = cpqfcHBAdata->fcLQ;
  ULONG ndx;
  
  ENTER("cpqfcTSPutLinkQ");

  ndx = fcLQ->producer;
  
  ndx += 1;  // test for Que full


  
  if( ndx >= FC_LINKQ_DEPTH ) // rollover test
    ndx = 0;

  if( ndx == fcLQ->consumer )   // QUE full test
  {
                       // QUE was full! lost LK command (fatal to logic)
    fcChip->fcStats.lnkQueFull++;

    printk("*LinkQ Full!*");
    TriggerHBA( fcChip->Registers.ReMapMemBase, 1);
/*
    {
      int i;
      printk("LinkQ PI %d, CI %d\n", fcLQ->producer,
        fcLQ->consumer);
		      
      for( i=0; i< FC_LINKQ_DEPTH; )
      {
	printk(" [%d]%Xh ", i, fcLQ->Qitem[i].Type);
	if( (++i %8) == 0) printk("\n");
      }
  
    }
*/    
    printk( "cpqfcTS: WARNING!! PutLinkQue - FULL!\n"); // we're hung
  }
  else                        // QUE next element
  {
    // Prevent certain multiple (back-to-back) requests.
    // This is important in that we don't want to issue multiple
    // ABTS for the same Exchange, or do multiple FM inits, etc.
    // We can never be sure of the timing of events reported to
    // us by Tach's IMQ, which can depend on system/bus speeds,
    // FC physical link circumstances, etc.
     
    if( (fcLQ->producer != fcLQ->consumer)
	    && 
        (Type == FMINIT)  )
    {
      LONG lastNdx;  // compute previous producer index
      if( fcLQ->producer)
        lastNdx = fcLQ->producer- 1;
      else
	lastNdx = FC_LINKQ_DEPTH-1;


      if( fcLQ->Qitem[lastNdx].Type == FMINIT)
      {
//        printk(" *skip FMINIT Q post* ");
//        goto DoneWithPutQ;
      }

    }

    // OK, add the Q'd item...
    
    fcLQ->Qitem[fcLQ->producer].Type = Type;
   
    memcpy(
        fcLQ->Qitem[fcLQ->producer].ulBuff,
        QueContent, 
        sizeof(fcLQ->Qitem[fcLQ->producer].ulBuff));

    fcLQ->producer = ndx;  // increment Que producer

    // set semaphore to wake up Kernel (worker) thread
    // 
    up( cpqfcHBAdata->fcQueReady );
  }

//DoneWithPutQ:

  LEAVE("cpqfcTSPutLinkQ");
}




// reset device ext FC link Q
void cpqfcTSLinkQReset( CPQFCHBA *cpqfcHBAdata)
   
{
  PFC_LINK_QUE fcLQ = cpqfcHBAdata->fcLQ;
  fcLQ->producer = 0;
  fcLQ->consumer = 0;

}





// When Tachyon gets an unassisted FCP-SCSI frame, post here so
// an arbitrary context thread (e.g. IOCTL loopback test function)
// can process it.

// (NOTE: Not revised for Linux)
// This Q works like Tachyon's que - the producer points to the next
// (unused) entry.
void cpqfcTSPutScsiQue( CPQFCHBA *cpqfcHBAdata,
  int Type, 
  void *QueContent)
{
//  CPQFCHBA *cpqfcHBAdata = (CPQFCHBA *)HostAdapter->hostdata;
//  PTACHYON fcChip = &cpqfcHBAdata->fcChip;

//  ULONG ndx;

//  ULONG *pExchangeID;
//  LONG ExchangeID;

/*
  KeAcquireSpinLockAtDpcLevel( &pDevExt->fcScsiQueLock);
  ndx = pDevExt->fcScsiQue.producer + 1;  // test for Que full

  if( ndx >= FC_SCSIQ_DEPTH ) // rollover test
    ndx = 0;

  if( ndx == pDevExt->fcScsiQue.consumer )   // QUE full test
  {
                       // QUE was full! lost LK command (fatal to logic)
    fcChip->fcStats.ScsiQueFull++;
#ifdef DBG
    printk( "fcPutScsiQue - FULL!\n");
#endif

  }
  else                        // QUE next element
  {
    pDevExt->fcScsiQue.Qitem[pDevExt->fcScsiQue.producer].Type = Type;
    
    if( Type == FCP_RSP )
    {
      // this TL inbound message type means that a TL SEST exchange has
      // copied an FCP response frame into a buffer pointed to by the SEST
      // entry.  That buffer is allocated in the SEST structure at ->RspHDR.
      // Copy the RspHDR for use by the Que handler.
      pExchangeID = (ULONG *)QueContent;
      
      memcpy(
	      pDevExt->fcScsiQue.Qitem[pDevExt->fcScsiQue.producer].ulBuff,
        &fcChip->SEST->RspHDR[ *pExchangeID ],
	      sizeof(pDevExt->fcScsiQue.Qitem[0].ulBuff)); // (any element for size)
      
    }
    else
    {
      memcpy(
	      pDevExt->fcScsiQue.Qitem[pDevExt->fcScsiQue.producer].ulBuff,
        QueContent, 
	      sizeof(pDevExt->fcScsiQue.Qitem[pDevExt->fcScsiQue.producer].ulBuff));
    }
      
    pDevExt->fcScsiQue.producer = ndx;  // increment Que


    KeSetEvent( &pDevExt->TYIBscsi,  // signal any waiting thread
       0,                    // no priority boost
		   FALSE );              // no waiting later for this event
  }
  KeReleaseSpinLockFromDpcLevel( &pDevExt->fcScsiQueLock);
*/
}







static void ProcessELS_Request( CPQFCHBA*,TachFCHDR_GCMND*);

static void ProcessELS_Reply( CPQFCHBA*,TachFCHDR_GCMND*);

static void ProcessFCS_Reply( CPQFCHBA*,TachFCHDR_GCMND*);

void cpqfcTSImplicitLogout( CPQFCHBA* cpqfcHBAdata,
		PFC_LOGGEDIN_PORT pFcPort)
{
  PTACHYON fcChip = &cpqfcHBAdata->fcChip;

  if( pFcPort->port_id != 0xFFFC01 ) // don't care about Fabric
  {
    fcChip->fcStats.logouts++;
    printk("cpqfcTS: Implicit logout of WWN %08X%08X, port_id %06X\n", 
        (ULONG)pFcPort->u.liWWN,
        (ULONG)(pFcPort->u.liWWN >>32),
	pFcPort->port_id);

  // Terminate I/O with this (Linux) Scsi target
    cpqfcTSTerminateExchange( cpqfcHBAdata, 
                            &pFcPort->ScsiNexus,
	                    DEVICE_REMOVED);
  }
			
  // Do an "implicit logout" - we can't really Logout the device
  // (i.e. with LOGOut Request) because of port_id confusion
  // (i.e. the Other port has no port_id).
  // A new login for that WWN will have to re-write port_id (0 invalid)
  pFcPort->port_id = 0;  // invalid!
  pFcPort->pdisc = FALSE;
  pFcPort->prli = FALSE;
  pFcPort->plogi = FALSE;
  pFcPort->flogi = FALSE;
  pFcPort->LOGO_timer = 0;
  pFcPort->device_blocked = TRUE; // block Scsi Requests
  pFcPort->ScsiNexus.VolumeSetAddressing=0;	
}

  
// On FC-AL, there is a chance that a previously known device can
// be quietly removed (e.g. with non-managed hub), 
// while a NEW device (with different WWN) took the same alpa or
// even 24-bit port_id.  This chance is unlikely but we must always
// check for it.
static void TestDuplicatePortId( CPQFCHBA* cpqfcHBAdata,
		PFC_LOGGEDIN_PORT pLoggedInPort)
{
  PTACHYON fcChip = &cpqfcHBAdata->fcChip;
  // set "other port" at beginning of fcPorts list
  PFC_LOGGEDIN_PORT pOtherPortWithPortId = fcChip->fcPorts.pNextPort;
  while( pOtherPortWithPortId ) 
  {
    if( (pOtherPortWithPortId->port_id == 
         pLoggedInPort->port_id) 
		    &&
         (pOtherPortWithPortId != pLoggedInPort) )
    {
      // trouble!  (Implicitly) Log the other guy out
      printk(" *port_id %Xh is duplicated!* ", 
        pOtherPortWithPortId->port_id);
      cpqfcTSImplicitLogout( cpqfcHBAdata, pOtherPortWithPortId); 
   }
    pOtherPortWithPortId = pOtherPortWithPortId->pNextPort;
  }
}






// Dynamic Memory Allocation for newly discovered FC Ports.
// For simplicity, maintain fcPorts structs for ALL
// for discovered devices, including those we never do I/O with
// (e.g. Fabric addresses)

static PFC_LOGGEDIN_PORT CreateFcPort( 
	  CPQFCHBA* cpqfcHBAdata, 
	  PFC_LOGGEDIN_PORT pLastLoggedInPort, 
	  TachFCHDR_GCMND* fchs,
	  LOGIN_PAYLOAD* plogi)
{
  PTACHYON fcChip = &cpqfcHBAdata->fcChip;
  PFC_LOGGEDIN_PORT pNextLoggedInPort = NULL;
  int i;


  printk("cpqfcTS: New FC port %06Xh WWN: ", fchs->s_id);
  for( i=3; i>=0; i--)   // copy the LOGIN port's WWN
    printk("%02X", plogi->port_name[i]);
  for( i=7; i>3; i--)   // copy the LOGIN port's WWN
    printk("%02X", plogi->port_name[i]);


  // allocate mem for new port
  // (these are small and rare allocations...)
  pNextLoggedInPort = kmalloc( sizeof( FC_LOGGEDIN_PORT), GFP_ATOMIC );

    
  // allocation succeeded?  Fill out NEW PORT
  if( pNextLoggedInPort )
  {    
                              // clear out any garbage (sometimes exists)
    memset( pNextLoggedInPort, 0, sizeof( FC_LOGGEDIN_PORT));


    // If we login to a Fabric, we don't want to treat it
    // as a SCSI device...
    if( (fchs->s_id & 0xFFF000) != 0xFFF000)
    {
      int i;
      
      // create a unique "virtual" SCSI Nexus (for now, just a
      // new target ID) -- we will update channel/target on REPORT_LUNS
      // special case for very first SCSI target...
      if( cpqfcHBAdata->HostAdapter->max_id == 0)
      {
        pNextLoggedInPort->ScsiNexus.target = 0;
        fcChip->fcPorts.ScsiNexus.target = -1; // don't use "stub"
      }
      else
      {
        pNextLoggedInPort->ScsiNexus.target =
          cpqfcHBAdata->HostAdapter->max_id;
      }

      // initialize the lun[] Nexus struct for lun masking      
      for( i=0; i< CPQFCTS_MAX_LUN; i++)
        pNextLoggedInPort->ScsiNexus.lun[i] = 0xFF; // init to NOT USED
      
      pNextLoggedInPort->ScsiNexus.channel = 0; // cpqfcTS has 1 FC port
      
      printk(" SCSI Chan/Trgt %d/%d", 
          pNextLoggedInPort->ScsiNexus.channel,
          pNextLoggedInPort->ScsiNexus.target);
 
      // tell Scsi layers about the new target...
      cpqfcHBAdata->HostAdapter->max_id++; 
//    printk("HostAdapter->max_id = %d\n",
//      cpqfcHBAdata->HostAdapter->max_id);		    
    }                          
    else
    {
      // device is NOT SCSI (in case of Fabric)
      pNextLoggedInPort->ScsiNexus.target = -1;  // invalid
    }

	  // create forward link to new port
    pLastLoggedInPort->pNextPort = pNextLoggedInPort;
    printk("\n");

  }     
  return pNextLoggedInPort;  // NULL on allocation failure
}   // end NEW PORT (WWN) logic



// For certain cases, we want to terminate exchanges without
// sending ABTS to the device.  Examples include when an FC
// device changed it's port_id after Loop re-init, or when
// the device sent us a logout.  In the case of changed port_id,
// we want to complete the command and return SOFT_ERROR to
// force a re-try.  In the case of LOGOut, we might return
// BAD_TARGET if the device is really gone.
// Since we must ensure that Tachyon is not operating on the
// exchange, we have to freeze the chip
// sterminateex
void cpqfcTSTerminateExchange( 
  CPQFCHBA* cpqfcHBAdata, SCSI_NEXUS *ScsiNexus, int TerminateStatus)
{
  PTACHYON fcChip = &cpqfcHBAdata->fcChip;
  FC_EXCHANGES *Exchanges = fcChip->Exchanges;
  ULONG x_ID;

  if( ScsiNexus )
  {
//    printk("TerminateExchange: ScsiNexus chan/target %d/%d\n",
//		    ScsiNexus->channel, ScsiNexus->target);

  } 
  
  for( x_ID = 0; x_ID < TACH_SEST_LEN; x_ID++)
  {
    if( Exchanges->fcExchange[x_ID].type )  // in use?
    {
      if( ScsiNexus == NULL ) // our HBA changed - term. all
      {
	Exchanges->fcExchange[x_ID].status = TerminateStatus;
        cpqfcTSPutLinkQue( cpqfcHBAdata, BLS_ABTS, &x_ID ); 
      }
      else
      {
	// If a device, according to WWN, has been removed, it's
	// port_id may be used by another working device, so we
	// have to terminate by SCSI target, NOT port_id.
        if( Exchanges->fcExchange[x_ID].Cmnd) // Cmnd in progress?
	{	                 
	  if( (Exchanges->fcExchange[x_ID].Cmnd->device->id == ScsiNexus->target)
			&&
            (Exchanges->fcExchange[x_ID].Cmnd->device->channel == ScsiNexus->channel)) 
          {
            Exchanges->fcExchange[x_ID].status = TerminateStatus;
            cpqfcTSPutLinkQue( cpqfcHBAdata, BLS_ABTS, &x_ID ); // timed-out
          }
	}

	// (in case we ever need it...)
	// all SEST structures have a remote node ID at SEST DWORD 2
        //          if( (fcChip->SEST->u[ x_ID ].TWE.Remote_Node_ID >> 8)
        //                ==  port_id)
      } 
    }
  }
}


static void ProcessELS_Request( 
              CPQFCHBA* cpqfcHBAdata, TachFCHDR_GCMND* fchs)
{
  PTACHYON fcChip = &cpqfcHBAdata->fcChip;
//  FC_EXCHANGES *Exchanges = fcChip->Exchanges;
//  ULONG ox_id = (fchs->ox_rx_id >>16);
  PFC_LOGGEDIN_PORT pLoggedInPort=NULL, pLastLoggedInPort;
  BOOLEAN NeedReject = FALSE;
  ULONG ls_reject_code = 0; // default don'n know??


  // Check the incoming frame for a supported ELS type
  switch( fchs->pl[0] & 0xFFFF)
  {
  case 0x0050: //  PDISC?

    // Payload for PLOGI and PDISC is identical (request & reply)
    if( !verify_PLOGI( fcChip, fchs, &ls_reject_code) ) // valid payload?
    {
      LOGIN_PAYLOAD logi;       // FC-PH Port Login
      
      // PDISC payload OK. If critical login fields
      // (e.g. WWN) matches last login for this port_id,
      // we may resume any prior exchanges
      // with the other port

      
      BigEndianSwap( (UCHAR*)&fchs->pl[0], (UCHAR*)&logi, sizeof(logi));
   
      pLoggedInPort = fcFindLoggedInPort( 
             fcChip, 
	     NULL,     // don't search Scsi Nexus
	     0,        // don't search linked list for port_id
             &logi.port_name[0],     // search linked list for WWN
             &pLastLoggedInPort);  // must return non-NULL; when a port_id
                                   // is not found, this pointer marks the
                                   // end of the singly linked list
    
      if( pLoggedInPort != NULL)   // WWN found (prior login OK)
      { 
           
 	if( (fchs->s_id & 0xFFFFFF) == pLoggedInPort->port_id)
	{
          // Yes.  We were expecting PDISC?
          if( pLoggedInPort->pdisc )
	  {
	    // Yes; set fields accordingly.     (PDISC, not Originator)
            SetLoginFields( pLoggedInPort, fchs, TRUE, FALSE);
       
            // send 'ACC' reply 
            cpqfcTSPutLinkQue( cpqfcHBAdata, 
                          ELS_PLOGI_ACC, // (PDISC same as PLOGI ACC)
                          fchs );

	    // OK to resume I/O...
	  }
	  else
 	  {
	    printk("Not expecting PDISC (pdisc=FALSE)\n");
	    NeedReject = TRUE;
	    // set reject reason code 
            ls_reject_code = 
              LS_RJT_REASON( PROTOCOL_ERROR, INITIATOR_CTL_ERROR);
	  }
	}
	else
	{
	  if( pLoggedInPort->port_id != 0)
	  {
  	    printk("PDISC PortID change: old %Xh, new %Xh\n",
              pLoggedInPort->port_id, fchs->s_id &0xFFFFFF);
	  }
          NeedReject = TRUE;
          // set reject reason code 
          ls_reject_code = 
	    LS_RJT_REASON( PROTOCOL_ERROR, INITIATOR_CTL_ERROR);
		  
	}
      }
      else
      {
	printk("PDISC Request from unknown WWN\n");
        NeedReject = TRUE;
          
	// set reject reason code 
        ls_reject_code = 
          LS_RJT_REASON( LOGICAL_ERROR, INVALID_PORT_NAME);
      }

    }
    else // Payload unacceptable
    {
      printk("payload unacceptable\n");
      NeedReject = TRUE;  // reject code already set
      
    }

    if( NeedReject)
    {
      ULONG port_id;
      // The PDISC failed.  Set login struct flags accordingly,
      // terminate any I/O to this port, and Q a PLOGI
      if( pLoggedInPort )
      {
        pLoggedInPort->pdisc = FALSE;
        pLoggedInPort->prli = FALSE;
        pLoggedInPort->plogi = FALSE;
	
        cpqfcTSTerminateExchange( cpqfcHBAdata, 
          &pLoggedInPort->ScsiNexus, PORTID_CHANGED);
	port_id = pLoggedInPort->port_id;
      }
      else
      {
	port_id = fchs->s_id &0xFFFFFF;
      }
      fchs->reserved = ls_reject_code; // borrow this (unused) field
      cpqfcTSPutLinkQue( cpqfcHBAdata, ELS_RJT, fchs );
    }
   
    break;



  case 0x0003: //  PLOGI?

    // Payload for PLOGI and PDISC is identical (request & reply)
    if( !verify_PLOGI( fcChip, fchs, &ls_reject_code) ) // valid payload?
    {
      LOGIN_PAYLOAD logi;       // FC-PH Port Login
      BOOLEAN NeedReject = FALSE;
      
      // PDISC payload OK. If critical login fields
      // (e.g. WWN) matches last login for this port_id,
      // we may resume any prior exchanges
      // with the other port

      
      BigEndianSwap( (UCHAR*)&fchs->pl[0], (UCHAR*)&logi, sizeof(logi));
   
      pLoggedInPort = fcFindLoggedInPort( 
             fcChip, 
	     NULL,       // don't search Scsi Nexus
	     0,        // don't search linked list for port_id
             &logi.port_name[0],     // search linked list for WWN
             &pLastLoggedInPort);  // must return non-NULL; when a port_id
                                   // is not found, this pointer marks the
                                   // end of the singly linked list
    
      if( pLoggedInPort == NULL)   // WWN not found -New Port
      { 
      	pLoggedInPort = CreateFcPort( 
			  cpqfcHBAdata, 
			  pLastLoggedInPort, 
			  fchs,
			  &logi);
        if( pLoggedInPort == NULL )
        {
          printk(" cpqfcTS: New port allocation failed - lost FC device!\n");
          // Now Q a LOGOut Request, since we won't be talking to that device
	
          NeedReject = TRUE;  
	  
          // set reject reason code 
          ls_reject_code = 
            LS_RJT_REASON( LOGICAL_ERROR, NO_LOGIN_RESOURCES);
	    
	}
      }
      if( !NeedReject )
      {
      
        // OK - we have valid fcPort ptr; set fields accordingly.   
	//                         (not PDISC, not Originator)
        SetLoginFields( pLoggedInPort, fchs, FALSE, FALSE); 

        // send 'ACC' reply 
        cpqfcTSPutLinkQue( cpqfcHBAdata, 
                      ELS_PLOGI_ACC, // (PDISC same as PLOGI ACC)
                      fchs );
      }
    }
    else // Payload unacceptable
    {
      printk("payload unacceptable\n");
      NeedReject = TRUE;  // reject code already set
    }

    if( NeedReject)
    {
      // The PDISC failed.  Set login struct flags accordingly,
      // terminate any I/O to this port, and Q a PLOGI
      pLoggedInPort->pdisc = FALSE;
      pLoggedInPort->prli = FALSE;
      pLoggedInPort->plogi = FALSE;
	
      fchs->reserved = ls_reject_code; // borrow this (unused) field

      // send 'RJT' reply 
      cpqfcTSPutLinkQue( cpqfcHBAdata, ELS_RJT, fchs );
    }
   
    // terminate any exchanges with this device...
    if( pLoggedInPort )
    {
      cpqfcTSTerminateExchange( cpqfcHBAdata, 
        &pLoggedInPort->ScsiNexus, PORTID_CHANGED);
    }
    break;



  case 0x1020:  // PRLI?
  {
    BOOLEAN NeedReject = TRUE;
    pLoggedInPort = fcFindLoggedInPort( 
           fcChip, 
           NULL,       // don't search Scsi Nexus
	   (fchs->s_id & 0xFFFFFF),  // search linked list for port_id
           NULL,     // DON'T search linked list for WWN
           NULL);    // don't care
      
    if( pLoggedInPort == NULL ) 
    {
      // huh?
      printk(" Unexpected PRLI Request -not logged in!\n");

      // set reject reason code 
      ls_reject_code = LS_RJT_REASON( PROTOCOL_ERROR, INITIATOR_CTL_ERROR);
      
      // Q a LOGOut here?
    }
    else
    {
      // verify the PRLI ACC payload
      if( !verify_PRLI( fchs, &ls_reject_code) )
      {
        // PRLI Reply is acceptable; were we expecting it?
        if( pLoggedInPort->plogi ) 
        { 
  	  // yes, we expected the PRLI ACC  (not PDISC; not Originator)
          SetLoginFields( pLoggedInPort, fchs, FALSE, FALSE);

          // Q an ACCept Reply
	  cpqfcTSPutLinkQue( cpqfcHBAdata,
                        ELS_PRLI_ACC, 
                        fchs );   
	  
	  NeedReject = FALSE;
	}
        else
        {
          // huh?
          printk(" (unexpected) PRLI REQEST with plogi FALSE\n");

          // set reject reason code 
          ls_reject_code = LS_RJT_REASON( PROTOCOL_ERROR, INITIATOR_CTL_ERROR);
    
    	  // Q a LOGOut here?
	  
        }
      }
      else
      {
        printk(" PRLI REQUEST payload failed verify\n");
        // (reject code set by "verify")

        // Q a LOGOut here?
      }
    }

    if( NeedReject )
    {
      // Q a ReJecT Reply with reason code
      fchs->reserved = ls_reject_code;
      cpqfcTSPutLinkQue( cpqfcHBAdata,
                    ELS_RJT, // Q Type
                    fchs );  
    }
  }
    break;
 

    

  case  0x0005:  // LOGOut?
  {
  // was this LOGOUT because we sent a ELS_PDISC to an FC device
  // with changed (or new) port_id, or does the port refuse 
  // to communicate to us?
  // We maintain a logout counter - if we get 3 consecutive LOGOuts,
  // give up!
    LOGOUT_PAYLOAD logo;
    BOOLEAN GiveUpOnDevice = FALSE;
    ULONG ls_reject_code = 0;
    
    BigEndianSwap( (UCHAR*)&fchs->pl[0], (UCHAR*)&logo, sizeof(logo));

    pLoggedInPort = fcFindLoggedInPort( 
           fcChip, 
           NULL,     // don't search Scsi Nexus
	   0,        // don't search linked list for port_id
           &logo.port_name[0],     // search linked list for WWN
           NULL);    // don't care about end of list
    
    if( pLoggedInPort ) // found the device?
    {
      // Q an ACC reply 
      cpqfcTSPutLinkQue( cpqfcHBAdata,
                    ELS_LOGO_ACC, // Q Type
                    fchs );       // device to respond to

      // set login struct fields (LOGO_counter increment)
      SetLoginFields( pLoggedInPort, fchs, FALSE, FALSE);
      
      // are we an Initiator?
      if( fcChip->Options.initiator)  
      {
        // we're an Initiator, so check if we should 
	// try (another?) login

	// Fabrics routinely log out from us after
	// getting device info - don't try to log them
	// back in.
	if( (fchs->s_id & 0xFFF000) == 0xFFF000 )
	{
	  ; // do nothing
	}
	else if( pLoggedInPort->LOGO_counter <= 3)
	{
	  // try (another) login (PLOGI request)
	  
          cpqfcTSPutLinkQue( cpqfcHBAdata,
                    ELS_PLOGI, // Q Type
                    fchs );  
	
	  // Terminate I/O with "retry" potential
	  cpqfcTSTerminateExchange( cpqfcHBAdata, 
			            &pLoggedInPort->ScsiNexus,
				    PORTID_CHANGED);
	}
	else
	{
	  printk(" Got 3 LOGOuts - terminating comm. with port_id %Xh\n",
			  fchs->s_id &&0xFFFFFF);
	  GiveUpOnDevice = TRUE;
	}
      }
      else
      {
	GiveUpOnDevice = TRUE;
      }


      if( GiveUpOnDevice == TRUE )
      {
        cpqfcTSTerminateExchange( cpqfcHBAdata, 
	                          &pLoggedInPort->ScsiNexus,
		                  DEVICE_REMOVED);
      }
    }  
    else  // we don't know this WWN!
    {
      // Q a ReJecT Reply with reason code
      fchs->reserved = ls_reject_code;
      cpqfcTSPutLinkQue( cpqfcHBAdata,
                    ELS_RJT, // Q Type
                    fchs );  
    }
  }
    break;




  // FABRIC only case
  case 0x0461:  // ELS RSCN (Registered State Change Notification)?
  {
    int Ports;
    int i;
    __u32 Buff;
    // Typically, one or more devices have been added to or dropped
    // from the Fabric.
    // The format of this frame is defined in FC-FLA (Rev 2.7, Aug 1997)
    // The first 32-bit word has a 2-byte Payload Length, which
    // includes the 4 bytes of the first word.  Consequently,
    // this PL len must never be less than 4, must be a multiple of 4,
    // and has a specified max value 256.
    // (Endianess!)
    Ports = ((fchs->pl[0] >>24) - 4) / 4;
    Ports = Ports > 63 ? 63 : Ports;
    
    printk(" RSCN ports: %d\n", Ports);
    if( Ports <= 0 )  // huh?
    {
      // ReJecT the command
      fchs->reserved = LS_RJT_REASON( UNABLE_TO_PERFORM, 0);
    
      cpqfcTSPutLinkQue( cpqfcHBAdata,
                    ELS_RJT, // Q Type
                    fchs ); 
      
      break;
    }
    else  // Accept the command
    {
       cpqfcTSPutLinkQue( cpqfcHBAdata,
                    ELS_ACC, // Q Type
                    fchs ); 
    }
    
      // Check the "address format" to determine action.
      // We have 3 cases:
      // 0 = Port Address; 24-bit address of affected device
      // 1 = Area Address; MS 16 bits valid
      // 2 = Domain Address; MS 8 bits valid
    for( i=0; i<Ports; i++)
    { 
      BigEndianSwap( (UCHAR*)&fchs->pl[i+1],(UCHAR*)&Buff, 4);
      switch( Buff & 0xFF000000)
      {

      case 0:  // Port Address?
	
      case 0x01000000: // Area Domain?
      case 0x02000000: // Domain Address
        // For example, "port_id" 0x201300 
	// OK, let's try a Name Service Request (Query)
      fchs->s_id = 0xFFFFFC;  // Name Server Address
      cpqfcTSPutLinkQue( cpqfcHBAdata, FCS_NSR, fchs);

      break;
	
	
      default:  // huh? new value on version change?
      break;
      }
    }
  }    
  break;    



    
  default:  // don't support this request (yet)
    // set reject reason code 
    fchs->reserved = LS_RJT_REASON( UNABLE_TO_PERFORM, 
		                    REQUEST_NOT_SUPPORTED);
    
    cpqfcTSPutLinkQue( cpqfcHBAdata,
                    ELS_RJT, // Q Type
                    fchs );     
    break;  
  }
}


static void ProcessELS_Reply( 
		CPQFCHBA* cpqfcHBAdata, TachFCHDR_GCMND* fchs)
{
  PTACHYON fcChip = &cpqfcHBAdata->fcChip;
  FC_EXCHANGES *Exchanges = fcChip->Exchanges;
  ULONG ox_id = (fchs->ox_rx_id >>16);
  ULONG ls_reject_code;
  PFC_LOGGEDIN_PORT pLoggedInPort, pLastLoggedInPort;
  
  // If this is a valid reply, then we MUST have sent a request.
  // Verify that we can find a valid request OX_ID corresponding to
  // this reply

  
  if( Exchanges->fcExchange[(fchs->ox_rx_id >>16)].type == 0)
  {
    printk(" *Discarding ACC/RJT frame, xID %04X/%04X* ", 
		    ox_id, fchs->ox_rx_id & 0xffff);
    goto Quit;  // exit this routine
  }


  // Is the reply a RJT (reject)?
  if( (fchs->pl[0] & 0xFFFFL) == 0x01) // Reject reply?
  {
//  ******  REJECT REPLY  ********
    switch( Exchanges->fcExchange[ox_id].type )
    {
	  
    case ELS_FDISC:  // we sent out Fabric Discovery
    case ELS_FLOGI:  // we sent out FLOGI

      printk("RJT received on Fabric Login from %Xh, reason %Xh\n", 
        fchs->s_id, fchs->pl[1]);    

    break;

    default:
    break;
    }
      
    goto Done;
  }

  // OK, we have an ACCept...
  // What's the ACC type? (according to what we sent)
  switch( Exchanges->fcExchange[ox_id].type )
  {
	  
  case ELS_PLOGI:  // we sent out PLOGI
    if( !verify_PLOGI( fcChip, fchs, &ls_reject_code) )
    {
      LOGIN_PAYLOAD logi;       // FC-PH Port Login
      
      // login ACC payload acceptable; search for WWN in our list
      // of fcPorts
      
      BigEndianSwap( (UCHAR*)&fchs->pl[0], (UCHAR*)&logi, sizeof(logi));
   
      pLoggedInPort = fcFindLoggedInPort( 
             fcChip, 
	     NULL,     // don't search Scsi Nexus
	     0,        // don't search linked list for port_id
             &logi.port_name[0],     // search linked list for WWN
             &pLastLoggedInPort);  // must return non-NULL; when a port_id
                                   // is not found, this pointer marks the
                                   // end of the singly linked list
    
      if( pLoggedInPort == NULL)         // WWN not found - new port
      {

	pLoggedInPort = CreateFcPort( 
			  cpqfcHBAdata, 
			  pLastLoggedInPort, 
			  fchs,
			  &logi);

        if( pLoggedInPort == NULL )
        {
          printk(" cpqfcTS: New port allocation failed - lost FC device!\n");
          // Now Q a LOGOut Request, since we won't be talking to that device
	
          goto Done;  // exit with error! dropped login frame
	}
      }
      else      // WWN was already known.  Ensure that any open
	        // exchanges for this WWN are terminated.
      	// NOTE: It's possible that a device can change its 
	// 24-bit port_id after a Link init or Fabric change 
	// (e.g. LIP or Fabric RSCN).  In that case, the old
	// 24-bit port_id may be duplicated, or no longer exist.
      {

        cpqfcTSTerminateExchange( cpqfcHBAdata, 
          &pLoggedInPort->ScsiNexus, PORTID_CHANGED);
      }

      // We have an fcPort struct - set fields accordingly
                                    // not PDISC, originator 
      SetLoginFields( pLoggedInPort, fchs, FALSE, TRUE);
			
      // We just set a "port_id"; is it duplicated?
      TestDuplicatePortId( cpqfcHBAdata, pLoggedInPort);

      // For Fabric operation, we issued PLOGI to 0xFFFFFC
      // so we can send SCR (State Change Registration) 
      // Check for this special case...
      if( fchs->s_id == 0xFFFFFC ) 
      {
        // PLOGI ACC was a Fabric response... issue SCR
	fchs->s_id = 0xFFFFFD;  // address for SCR
        cpqfcTSPutLinkQue( cpqfcHBAdata, ELS_SCR, fchs);
      }

      else
      {
      // Now we need a PRLI to enable FCP-SCSI operation
      // set flags and Q up a ELS_PRLI
        cpqfcTSPutLinkQue( cpqfcHBAdata, ELS_PRLI, fchs);
      }
    }
    else
    {
      // login payload unacceptable - reason in ls_reject_code
      // Q up a Logout Request
      printk("Login Payload unacceptable\n");

    }
    break;


  // PDISC logic very similar to PLOGI, except we never want
  // to allocate mem for "new" port, and we set flags differently
  // (might combine later with PLOGI logic for efficiency)  
  case ELS_PDISC:  // we sent out PDISC
    if( !verify_PLOGI( fcChip, fchs, &ls_reject_code) )
    {
      LOGIN_PAYLOAD logi;       // FC-PH Port Login
      BOOLEAN NeedLogin = FALSE;
      
      // login payload acceptable; search for WWN in our list
      // of (previously seen) fcPorts
      
      BigEndianSwap( (UCHAR*)&fchs->pl[0], (UCHAR*)&logi, sizeof(logi));
   
      pLoggedInPort = fcFindLoggedInPort( 
             fcChip, 
	     NULL,     // don't search Scsi Nexus
	     0,        // don't search linked list for port_id
             &logi.port_name[0],     // search linked list for WWN
             &pLastLoggedInPort);  // must return non-NULL; when a port_id
                                   // is not found, this pointer marks the
                                   // end of the singly linked list
    
      if( pLoggedInPort != NULL)   // WWN found?
      {
        // WWN has same port_id as last login?  (Of course, a properly
	// working FC device should NEVER ACCept a PDISC if it's
	// port_id changed, but check just in case...)
	if( (fchs->s_id & 0xFFFFFF) == pLoggedInPort->port_id)
	{
          // Yes.  We were expecting PDISC?
          if( pLoggedInPort->pdisc )
	  {
            int i;
	    
	    
	    // PDISC expected -- set fields.  (PDISC, Originator)
            SetLoginFields( pLoggedInPort, fchs, TRUE, TRUE);

	    // We are ready to resume FCP-SCSI to this device...
            // Do we need to start anything that was Queued?

            for( i=0; i< TACH_SEST_LEN; i++)
            {
              // see if any exchange for this PDISC'd port was queued
              if( ((fchs->s_id &0xFFFFFF) == 
                   (Exchanges->fcExchange[i].fchs.d_id & 0xFFFFFF))
                      &&
                  (Exchanges->fcExchange[i].status & EXCHANGE_QUEUED))
              {
                fchs->reserved = i; // copy ExchangeID
//                printk(" *Q x_ID %Xh after PDISC* ",i);

                cpqfcTSPutLinkQue( cpqfcHBAdata, EXCHANGE_QUEUED, fchs );
              }
            }

	    // Complete commands Q'd while we were waiting for Login

	    UnblockScsiDevice( cpqfcHBAdata->HostAdapter, pLoggedInPort);
	  }
	  else
	  {
	    printk("Not expecting PDISC (pdisc=FALSE)\n");
	    NeedLogin = TRUE;
	  }
	}
	else
	{
	  printk("PDISC PortID change: old %Xh, new %Xh\n",
            pLoggedInPort->port_id, fchs->s_id &0xFFFFFF);
          NeedLogin = TRUE;
		  
	}
      }
      else
      {
	printk("PDISC ACC from unknown WWN\n");
        NeedLogin = TRUE;
      }

      if( NeedLogin)
      {
	
        // The PDISC failed.  Set login struct flags accordingly,
	// terminate any I/O to this port, and Q a PLOGI
	if( pLoggedInPort )  // FC device previously known?
	{

          cpqfcTSPutLinkQue( cpqfcHBAdata,
                    ELS_LOGO, // Q Type
                    fchs );   // has port_id to send to 

	  // There are a variety of error scenarios which can result
  	  // in PDISC failure, so as a catchall, add the check for
	  // duplicate port_id.
	  TestDuplicatePortId( cpqfcHBAdata, pLoggedInPort);

//    TriggerHBA( fcChip->Registers.ReMapMemBase, 0);
          pLoggedInPort->pdisc = FALSE;
          pLoggedInPort->prli = FALSE;
          pLoggedInPort->plogi = FALSE;
	
          cpqfcTSTerminateExchange( cpqfcHBAdata, 
	    &pLoggedInPort->ScsiNexus, PORTID_CHANGED);
        }
        cpqfcTSPutLinkQue( cpqfcHBAdata, ELS_PLOGI, fchs );
      }
    }
    else
    {
      // login payload unacceptable - reason in ls_reject_code
      // Q up a Logout Request
      printk("ERROR: Login Payload unacceptable!\n");

    }
   
    break;
    


  case ELS_PRLI:  // we sent out PRLI


    pLoggedInPort = fcFindLoggedInPort( 
           fcChip, 
           NULL,       // don't search Scsi Nexus
	   (fchs->s_id & 0xFFFFFF),  // search linked list for port_id
           NULL,     // DON'T search linked list for WWN
           NULL);    // don't care
      
    if( pLoggedInPort == NULL ) 
    {
      // huh?
      printk(" Unexpected PRLI ACCept frame!\n");

      // Q a LOGOut here?

      goto Done;
    }

    // verify the PRLI ACC payload
    if( !verify_PRLI( fchs, &ls_reject_code) )
    {
      // PRLI Reply is acceptable; were we expecting it?
      if( pLoggedInPort->plogi ) 
      { 
	// yes, we expected the PRLI ACC  (not PDISC; Originator)
	SetLoginFields( pLoggedInPort, fchs, FALSE, TRUE);

        // OK, let's send a REPORT_LUNS command to determine
	// whether VSA or PDA FCP-LUN addressing is used.
	
        cpqfcTSPutLinkQue( cpqfcHBAdata, SCSI_REPORT_LUNS, fchs );
	
	// It's possible that a device we were talking to changed 
	// port_id, and has logged back in.  This function ensures
	// that I/O will resume.
        UnblockScsiDevice( cpqfcHBAdata->HostAdapter, pLoggedInPort);

      }
      else
      {
        // huh?
        printk(" (unexpected) PRLI ACCept with plogi FALSE\n");

        // Q a LOGOut here?
        goto Done;
      }
    }
    else
    {
      printk(" PRLI ACCept payload failed verify\n");

      // Q a LOGOut here?
    }

    break;
 
  case ELS_FLOGI:  // we sent out FLOGI (Fabric Login)

    // update the upper 16 bits of our port_id in Tachyon
    // the switch adds those upper 16 bits when responding
    // to us (i.e. we are the destination_id)
    fcChip->Registers.my_al_pa = (fchs->d_id & 0xFFFFFF);
    writel( fcChip->Registers.my_al_pa,  
      fcChip->Registers.ReMapMemBase + TL_MEM_TACH_My_ID);

    // now send out a PLOGI to the well known port_id 0xFFFFFC
    fchs->s_id = 0xFFFFFC;
    cpqfcTSPutLinkQue( cpqfcHBAdata, ELS_PLOGI, fchs);
  
   break; 


  case ELS_FDISC:  // we sent out FDISC (Fabric Discovery (Login))

   printk( " ELS_FDISC success ");
   break;
   

  case ELS_SCR:  // we sent out State Change Registration
    // now we can issue Name Service Request to find any
    // Fabric-connected devices we might want to login to.
   
	
    fchs->s_id = 0xFFFFFC;  // Name Server Address
    cpqfcTSPutLinkQue( cpqfcHBAdata, FCS_NSR, fchs);
    

    break;

    
  default:
    printk(" *Discarding unknown ACC frame, xID %04X/%04X* ", 
   		    ox_id, fchs->ox_rx_id & 0xffff);
    break;
  }

  
Done:
  // Regardless of whether the Reply is valid or not, the
  // the exchange is done - complete
  cpqfcTSCompleteExchange(cpqfcHBAdata->PciDev, fcChip, (fchs->ox_rx_id >>16)); 
	  
Quit:    
  return;
}






// ****************  Fibre Channel Services  **************
// This is where we process the Directory (Name) Service Reply
// to know which devices are on the Fabric

static void ProcessFCS_Reply( 
	CPQFCHBA* cpqfcHBAdata, TachFCHDR_GCMND* fchs)
{
  PTACHYON fcChip = &cpqfcHBAdata->fcChip;
  FC_EXCHANGES *Exchanges = fcChip->Exchanges;
  ULONG ox_id = (fchs->ox_rx_id >>16);
//  ULONG ls_reject_code;
//  PFC_LOGGEDIN_PORT pLoggedInPort, pLastLoggedInPort;
  
  // If this is a valid reply, then we MUST have sent a request.
  // Verify that we can find a valid request OX_ID corresponding to
  // this reply

  if( Exchanges->fcExchange[(fchs->ox_rx_id >>16)].type == 0)
  {
    printk(" *Discarding Reply frame, xID %04X/%04X* ", 
		    ox_id, fchs->ox_rx_id & 0xffff);
    goto Quit;  // exit this routine
  }


  // OK, we were expecting it.  Now check to see if it's a
  // "Name Service" Reply, and if so force a re-validation of
  // Fabric device logins (i.e. Start the login timeout and
  // send PDISC or PLOGI)
  // (Endianess Byte Swap?)
  if( fchs->pl[1] == 0x02FC )  // Name Service
  {
    // got a new (or NULL) list of Fabric attach devices... 
    // Invalidate current logins
    
    PFC_LOGGEDIN_PORT pLoggedInPort = &fcChip->fcPorts;
    while( pLoggedInPort ) // for all ports which are expecting
                           // PDISC after the next LIP, set the
                           // logoutTimer
    {

      if( (pLoggedInPort->port_id & 0xFFFF00)  // Fabric device?
		      &&
          (pLoggedInPort->port_id != 0xFFFFFC) ) // NOT the F_Port
      {
        pLoggedInPort->LOGO_timer = 6;  // what's the Fabric timeout??
                                // suspend any I/O in progress until
                                // PDISC received...
        pLoggedInPort->prli = FALSE;   // block FCP-SCSI commands
      }
	    
      pLoggedInPort = pLoggedInPort->pNextPort;
    }
    
    if( fchs->pl[2] == 0x0280)  // ACCept?
    {
      // Send PLOGI or PDISC to these Fabric devices
      SendLogins( cpqfcHBAdata, &fchs->pl[4] );  
    }


    // As of this writing, the only reason to reject is because NO
    // devices are left on the Fabric.  We already started
    // "logged out" timers; if the device(s) don't come
    // back, we'll do the implicit logout in the heart beat 
    // timer routine
    else  // ReJecT
    {
      // this just means no Fabric device is visible at this instant
    } 
  }

  // Regardless of whether the Reply is valid or not, the
  // the exchange is done - complete
  cpqfcTSCompleteExchange(cpqfcHBAdata->PciDev, fcChip, (fchs->ox_rx_id >>16));
	  
Quit:    
  return;
}







static void AnalyzeIncomingFrame( 
        CPQFCHBA *cpqfcHBAdata,
        ULONG QNdx )
{
  PTACHYON fcChip = &cpqfcHBAdata->fcChip;
  FC_EXCHANGES *Exchanges = fcChip->Exchanges;
  PFC_LINK_QUE fcLQ = cpqfcHBAdata->fcLQ;
  TachFCHDR_GCMND* fchs = 
    (TachFCHDR_GCMND*)fcLQ->Qitem[QNdx].ulBuff;
//  ULONG ls_reject_code;  // reason for rejecting login
  LONG ExchangeID;
//  FC_LOGGEDIN_PORT *pLoggedInPort;
  BOOLEAN AbortAccept;  

  ENTER("AnalyzeIncomingFrame");



  switch( fcLQ->Qitem[QNdx].Type) // FCP or Unknown
  {

  case SFQ_UNKNOWN:  // unknown frame (e.g. LIP position frame, NOP, etc.)
 

      // *********  FC-4 Device Data/ Fibre Channel Service *************
    if( ((fchs->d_id &0xF0000000) == 0)   // R_CTL (upper nibble) 0x0?
                &&   
      (fchs->f_ctl & 0x20000000) )  // TYPE 20h is Fibre Channel Service
    {

      // ************** FCS Reply **********************

      if( (fchs->d_id & 0xff000000L) == 0x03000000L)  // (31:23 R_CTL)
      {
	ProcessFCS_Reply( cpqfcHBAdata, fchs );

      }  // end of  FCS logic

    }
    

      // ***********  Extended Link Service **************

    else if( fchs->d_id & 0x20000000   // R_CTL 0x2?
                  &&   
      (fchs->f_ctl & 0x01000000) )  // TYPE = 1
    {

                           // these frames are either a response to
                           // something we sent (0x23) or "unsolicited"
                           // frames (0x22).


      // **************Extended Link REPLY **********************
                           // R_CTL Solicited Control Reply

      if( (fchs->d_id & 0xff000000L) == 0x23000000L)  // (31:23 R_CTL)
      {

	ProcessELS_Reply( cpqfcHBAdata, fchs );

      }  // end of  "R_CTL Solicited Control Reply"




       // **************Extended Link REQUEST **********************
       // (unsolicited commands from another port or task...)

                           // R_CTL Ext Link REQUEST
      else if( (fchs->d_id & 0xff000000L) == 0x22000000L &&
              (fchs->ox_rx_id != 0xFFFFFFFFL) ) // (ignore LIP frame)
      {



	ProcessELS_Request( cpqfcHBAdata, fchs );

      }



        // ************** LILP **********************
      else if( (fchs->d_id & 0xff000000L) == 0x22000000L &&
               (fchs->ox_rx_id == 0xFFFFFFFFL)) // (e.g., LIP frames)

      {
        // SANMark specifies that when available, we must use
	// the LILP frame to determine which ALPAs to send Port Discovery
	// to...

        if( fchs->pl[0] == 0x0711L) //  ELS_PLOGI?
	{
//	  UCHAR *ptr = (UCHAR*)&fchs->pl[1];
//	  printk(" %d ALPAs found\n", *ptr);
	  memcpy( fcChip->LILPmap, &fchs->pl[1], 32*4); // 32 DWORDs
	  fcChip->Options.LILPin = 1; // our LILPmap is valid!
	  // now post to make Port Discovery happen...
          cpqfcTSPutLinkQue( cpqfcHBAdata, LINKACTIVE, fchs);  
	}
      }
    }

     
    // *****************  BASIC LINK SERVICE *****************
    
    else if( fchs->d_id & 0x80000000  // R_CTL:
                    &&           // Basic Link Service Request
           !(fchs->f_ctl & 0xFF000000) )  // type=0 for BLS
    {