aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/staging/heci/interrupt.c
blob: 2a3a01a62bbf748d3fe43e6cb0718d8242c6add8 (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
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
/*
 * Part of Intel(R) Manageability Engine Interface Linux driver
 *
 * Copyright (c) 2003 - 2008 Intel Corp.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions, and the following disclaimer,
 *    without modification.
 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
 *    substantially similar to the "NO WARRANTY" disclaimer below
 *    ("Disclaimer") and any redistribution must be conditioned upon
 *    including a substantially similar Disclaimer requirement for further
 *    binary redistribution.
 * 3. Neither the names of the above-listed copyright holders nor the names
 *    of any contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * Alternatively, this software may be distributed under the terms of the
 * GNU General Public License ("GPL") version 2 as published by the Free
 * Software Foundation.
 *
 * NO WARRANTY
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 *
 */

#include <linux/kthread.h>

#include "heci.h"
#include "heci_interface.h"

/*
 *  interrupt function prototypes
 */
static void heci_bh_handler(struct work_struct *work);
static int heci_bh_read_handler(struct io_heci_list *complete_list,
		struct iamt_heci_device *dev,
		__s32 *slots);
static int heci_bh_write_handler(struct io_heci_list *complete_list,
		struct iamt_heci_device *dev,
		__s32 *slots);
static void heci_bh_read_bus_message(struct iamt_heci_device *dev,
		struct heci_msg_hdr *heci_hdr);
static int heci_bh_read_pthi_message(struct io_heci_list *complete_list,
		struct iamt_heci_device *dev,
		struct heci_msg_hdr *heci_hdr);
static int heci_bh_read_client_message(struct io_heci_list *complete_list,
		struct iamt_heci_device *dev,
		struct heci_msg_hdr *heci_hdr);
static void heci_client_connect_response(struct iamt_heci_device *dev,
		struct hbm_client_connect_response *connect_res);
static void heci_client_disconnect_response(struct iamt_heci_device *dev,
		struct hbm_client_connect_response *disconnect_res);
static void heci_client_flow_control_response(struct iamt_heci_device *dev,
		struct hbm_flow_control *flow_control);
static void heci_client_disconnect_request(struct iamt_heci_device *dev,
		struct hbm_client_disconnect_request *disconnect_req);


/**
 * heci_isr_interrupt - The ISR of the HECI device
 *
 * @irq: The irq number
 * @dev_id: pointer to the device structure
 *
 * returns irqreturn_t
 */
irqreturn_t heci_isr_interrupt(int irq, void *dev_id)
{
	int err;
	struct iamt_heci_device *dev = (struct iamt_heci_device *) dev_id;

	dev->host_hw_state = read_heci_register(dev, H_CSR);

	if ((dev->host_hw_state & H_IS) != H_IS)
		return IRQ_NONE;

	/* disable interrupts */
	heci_csr_disable_interrupts(dev);

	/* clear H_IS bit in H_CSR */
	heci_csr_clear_his(dev);

	/*
	 * Our device interrupted, schedule work the heci_bh_handler
	 * to handle the interrupt processing. This needs to be a
	 * workqueue item since the handler can sleep.
	 */
	PREPARE_WORK(&dev->work, heci_bh_handler);
	DBG("schedule work the heci_bh_handler.\n");
	err = schedule_work(&dev->work);
	if (!err)
		DBG("heci_bh_handler was already on the workqueue.\n");
	return IRQ_HANDLED;
}

/**
 * _heci_cmpl - process completed operation.
 *
 * @file_ext: private data of the file object.
 * @priv_cb_pos: callback block.
 */
static void _heci_cmpl(struct heci_file_private *file_ext,
				struct heci_cb_private *priv_cb_pos)
{
	if (priv_cb_pos->major_file_operations == HECI_WRITE) {
		heci_free_cb_private(priv_cb_pos);
		DBG("completing write call back.\n");
		file_ext->writing_state = HECI_WRITE_COMPLETE;
		if ((&file_ext->tx_wait) &&
		    waitqueue_active(&file_ext->tx_wait))
			wake_up_interruptible(&file_ext->tx_wait);

	} else if (priv_cb_pos->major_file_operations == HECI_READ
				&& HECI_READING == file_ext->reading_state) {
		DBG("completing read call back information= %lu\n",
				priv_cb_pos->information);
		file_ext->reading_state = HECI_READ_COMPLETE;
		if ((&file_ext->rx_wait) &&
		    waitqueue_active(&file_ext->rx_wait))
			wake_up_interruptible(&file_ext->rx_wait);

	}
}

/**
 * _heci_cmpl_iamthif - process completed iamthif operation.
 *
 * @dev: Device object for our driver.
 * @priv_cb_pos: callback block.
 */
static void _heci_cmpl_iamthif(struct iamt_heci_device *dev,
				struct heci_cb_private *priv_cb_pos)
{
	if (dev->iamthif_canceled != 1) {
		dev->iamthif_state = HECI_IAMTHIF_READ_COMPLETE;
		dev->iamthif_stall_timer = 0;
		memcpy(priv_cb_pos->response_buffer.data,
				dev->iamthif_msg_buf,
				dev->iamthif_msg_buf_index);
		list_add_tail(&priv_cb_pos->cb_list,
				&dev->pthi_read_complete_list.heci_cb.cb_list);
		DBG("pthi read completed.\n");
	} else {
		run_next_iamthif_cmd(dev);
	}
	if (&dev->iamthif_file_ext.wait) {
		DBG("completing pthi call back.\n");
		wake_up_interruptible(&dev->iamthif_file_ext.wait);
	}
}
/**
 * heci_bh_handler - function called after ISR to handle the interrupt
 * processing.
 *
 * @work: pointer to the work structure
 *
 * NOTE: This function is called by schedule work
 */
static void heci_bh_handler(struct work_struct *work)
{
	struct iamt_heci_device *dev =
		container_of(work, struct iamt_heci_device, work);
	struct io_heci_list complete_list;
	__s32 slots;
	int rets;
	struct heci_cb_private *cb_pos = NULL, *cb_next = NULL;
	struct heci_file_private *file_ext;
	int bus_message_received = 0;
	struct task_struct *tsk;

	DBG("function called after ISR to handle the interrupt processing.\n");
	/* initialize our complete list */
	spin_lock_bh(&dev->device_lock);
	heci_initialize_list(&complete_list, dev);
	dev->host_hw_state = read_heci_register(dev, H_CSR);
	dev->me_hw_state = read_heci_register(dev, ME_CSR_HA);

	/* check if ME wants a reset */
	if (((dev->me_hw_state & ME_RDY_HRA) == 0)
	    && (dev->heci_state != HECI_RESETING)
	    && (dev->heci_state != HECI_INITIALIZING)) {
		DBG("FW not ready.\n");
		heci_reset(dev, 1);
		spin_unlock_bh(&dev->device_lock);
		return;
	}

	/*  check if we need to start the dev */
	if ((dev->host_hw_state & H_RDY) == 0) {
		if ((dev->me_hw_state & ME_RDY_HRA) == ME_RDY_HRA) {
			DBG("we need to start the dev.\n");
			dev->host_hw_state |= (H_IE | H_IG | H_RDY);
			heci_set_csr_register(dev);
			if (dev->heci_state == HECI_INITIALIZING) {
				dev->recvd_msg = 1;
				spin_unlock_bh(&dev->device_lock);
				wake_up_interruptible(&dev->wait_recvd_msg);
				return;

			} else {
				spin_unlock_bh(&dev->device_lock);
				tsk = kthread_run(heci_task_initialize_clients,
						  dev, "heci_reinit");
				if (IS_ERR(tsk)) {
					int rc = PTR_ERR(tsk);
					printk(KERN_WARNING "heci: Unable to"
					"start the heci thread: %d\n", rc);
				}
				return;
			}
		} else {
			DBG("enable interrupt FW not ready.\n");
			heci_csr_enable_interrupts(dev);
			spin_unlock_bh(&dev->device_lock);
			return;
		}
	}
	/* check slots avalable for reading */
	slots = count_full_read_slots(dev);
	DBG("slots =%08x  extra_write_index =%08x.\n",
		slots, dev->extra_write_index);
	while ((slots > 0) && (!dev->extra_write_index)) {
		DBG("slots =%08x  extra_write_index =%08x.\n", slots,
				dev->extra_write_index);
		DBG("call heci_bh_read_handler.\n");
		rets = heci_bh_read_handler(&complete_list, dev, &slots);
		if (rets != 0)
			goto end;
	}
	rets = heci_bh_write_handler(&complete_list, dev, &slots);
end:
	DBG("end of bottom half function.\n");
	dev->host_hw_state = read_heci_register(dev, H_CSR);
	dev->host_buffer_is_empty = host_buffer_is_empty(dev);

	if ((dev->host_hw_state & H_IS) == H_IS) {
		/* acknowledge interrupt and disable interrupts */
		heci_csr_disable_interrupts(dev);

		/* clear H_IS bit in H_CSR */
		heci_csr_clear_his(dev);

		PREPARE_WORK(&dev->work, heci_bh_handler);
		DBG("schedule work the heci_bh_handler.\n");
		rets = schedule_work(&dev->work);
		if (!rets)
			DBG("heci_bh_handler was already queued.\n");
	} else {
		heci_csr_enable_interrupts(dev);
	}

	if (dev->recvd_msg && waitqueue_active(&dev->wait_recvd_msg)) {
		DBG("received waiting bus message\n");
		bus_message_received = 1;
	}
	spin_unlock_bh(&dev->device_lock);
	if (bus_message_received) {
		DBG("wake up dev->wait_recvd_msg\n");
		wake_up_interruptible(&dev->wait_recvd_msg);
		bus_message_received = 0;
	}
	if ((complete_list.status != 0)
	    || list_empty(&complete_list.heci_cb.cb_list))
		return;


	list_for_each_entry_safe(cb_pos, cb_next,
			&complete_list.heci_cb.cb_list, cb_list) {
		file_ext = (struct heci_file_private *)cb_pos->file_private;
		list_del(&cb_pos->cb_list);
		if (file_ext != NULL) {
			if (file_ext != &dev->iamthif_file_ext) {
				DBG("completing call back.\n");
				_heci_cmpl(file_ext, cb_pos);
				cb_pos = NULL;
			} else if (file_ext == &dev->iamthif_file_ext) {
				_heci_cmpl_iamthif(dev, cb_pos);
			}
		}
	}
}


/**
 * heci_bh_read_handler - bottom half read routine after ISR to
 * handle the read processing.
 *
 * @cmpl_list: An instance of our list structure
 * @dev: Device object for our driver
 * @slots: slots to read.
 *
 * returns 0 on success, <0 on failure.
 */
static int heci_bh_read_handler(struct io_heci_list *cmpl_list,
		struct iamt_heci_device *dev,
		__s32 *slots)
{
	struct heci_msg_hdr *heci_hdr;
	int ret = 0;
	struct heci_file_private *file_pos = NULL;
	struct heci_file_private *file_next = NULL;

	if (!dev->rd_msg_hdr) {
		dev->rd_msg_hdr = read_heci_register(dev, ME_CB_RW);
		DBG("slots=%08x.\n", *slots);
		(*slots)--;
		DBG("slots=%08x.\n", *slots);
	}
	heci_hdr = (struct heci_msg_hdr *) &dev->rd_msg_hdr;
	DBG("heci_hdr->length =%d\n", heci_hdr->length);

	if ((heci_hdr->reserved) || !(dev->rd_msg_hdr)) {
		DBG("corrupted message header.\n");
		ret = -ECORRUPTED_MESSAGE_HEADER;
		goto end;
	}

	if ((heci_hdr->host_addr) || (heci_hdr->me_addr)) {
		list_for_each_entry_safe(file_pos, file_next,
				&dev->file_list, link) {
			DBG("list_for_each_entry_safe read host"
					" client = %d, ME client = %d\n",
					file_pos->host_client_id,
					file_pos->me_client_id);
			if ((file_pos->host_client_id == heci_hdr->host_addr)
			    && (file_pos->me_client_id == heci_hdr->me_addr))
				break;
		}

		if (&file_pos->link == &dev->file_list) {
			DBG("corrupted message header\n");
			ret = -ECORRUPTED_MESSAGE_HEADER;
			goto end;
		}
	}
	if (((*slots) * sizeof(__u32)) < heci_hdr->length) {
		DBG("we can't read the message slots=%08x.\n", *slots);
		/* we can't read the message */
		ret = -ERANGE;
		goto end;
	}

	/* decide where to read the message too */
	if (!heci_hdr->host_addr) {
		DBG("call heci_bh_read_bus_message.\n");
		heci_bh_read_bus_message(dev, heci_hdr);
		DBG("end heci_bh_read_bus_message.\n");
	} else if ((heci_hdr->host_addr == dev->iamthif_file_ext.host_client_id)
		   && (HECI_FILE_CONNECTED == dev->iamthif_file_ext.state)
		   && (dev->iamthif_state == HECI_IAMTHIF_READING)) {
		DBG("call heci_bh_read_iamthif_message.\n");
		DBG("heci_hdr->length =%d\n", heci_hdr->length);
		ret = heci_bh_read_pthi_message(cmpl_list, dev, heci_hdr);
		if (ret != 0)
			goto end;

	} else {
		DBG("call heci_bh_read_client_message.\n");
		ret = heci_bh_read_client_message(cmpl_list, dev, heci_hdr);
		if (ret != 0)
			goto end;

	}

	/* reset the number of slots and header */
	*slots = count_full_read_slots(dev);
	dev->rd_msg_hdr = 0;

	if (*slots == -ESLOTS_OVERFLOW) {
		/* overflow - reset */
		DBG("reseting due to slots overflow.\n");
		/* set the event since message has been read */
		ret = -ERANGE;
		goto end;
	}
end:
	return ret;
}


/**
 * heci_bh_read_bus_message - bottom half read routine after ISR to
 * handle the read bus message cmd  processing.
 *
 * @dev: Device object for our driver
 * @heci_hdr: header of bus message
 */
static void heci_bh_read_bus_message(struct iamt_heci_device *dev,
		struct heci_msg_hdr *heci_hdr)
{
	struct heci_bus_message *heci_msg;
	struct hbm_host_version_response *version_res;
	struct hbm_client_connect_response *connect_res;
	struct hbm_client_connect_response *disconnect_res;
	struct hbm_flow_control *flow_control;
	struct hbm_props_response *props_res;
	struct hbm_host_enum_response *enum_res;
	struct hbm_client_disconnect_request *disconnect_req;
	struct hbm_host_stop_request *h_stop_req;
	int i;
	unsigned char *buffer;

	/*  read the message to our buffer */
	buffer = (unsigned char *) dev->rd_msg_buf;
	BUG_ON(heci_hdr->length >= sizeof(dev->rd_msg_buf));
	heci_read_slots(dev, buffer, heci_hdr->length);
	heci_msg = (struct heci_bus_message *) buffer;

	switch (*(__u8 *) heci_msg) {
	case HOST_START_RES_CMD:
		version_res = (struct hbm_host_version_response *) heci_msg;
		if (version_res->host_version_supported) {
			dev->version.major_version = HBM_MAJOR_VERSION;
			dev->version.minor_version = HBM_MINOR_VERSION;
		} else {
			dev->version = version_res->me_max_version;
		}
		dev->recvd_msg = 1;
		DBG("host start response message received.\n");
		break;

	case CLIENT_CONNECT_RES_CMD:
		connect_res =
			(struct hbm_client_connect_response *) heci_msg;
		heci_client_connect_response(dev, connect_res);
		DBG("client connect response message received.\n");
		wake_up(&dev->wait_recvd_msg);
		break;

	case CLIENT_DISCONNECT_RES_CMD:
		disconnect_res =
			(struct hbm_client_connect_response *) heci_msg;
		heci_client_disconnect_response(dev,	 disconnect_res);
		DBG("client disconnect response message received.\n");
		wake_up(&dev->wait_recvd_msg);
		break;

	case HECI_FLOW_CONTROL_CMD:
		flow_control = (struct hbm_flow_control *) heci_msg;
		heci_client_flow_control_response(dev, flow_control);
		DBG("client flow control response message received.\n");
		break;

	case HOST_CLIENT_PROPERTEIS_RES_CMD:
		props_res = (struct hbm_props_response *) heci_msg;
		if (props_res->status != 0) {
			BUG();
			break;
		}
		for (i = 0; i < dev->num_heci_me_clients; i++) {
			if (dev->me_clients[i].client_id ==
					props_res->address) {
				dev->me_clients[i].props =
					props_res->client_properties;
				break;
			}

		}
		dev->recvd_msg = 1;
		break;

	case HOST_ENUM_RES_CMD:
		enum_res = (struct hbm_host_enum_response *) heci_msg;
		memcpy(dev->heci_me_clients, enum_res->valid_addresses, 32);
		dev->recvd_msg = 1;
		break;

	case HOST_STOP_RES_CMD:
		dev->heci_state = HECI_DISABLED;
		DBG("reseting because of FW stop response.\n");
		heci_reset(dev, 1);
		break;

	case CLIENT_DISCONNECT_REQ_CMD:
		/* search for client */
		disconnect_req =
			(struct hbm_client_disconnect_request *) heci_msg;
		heci_client_disconnect_request(dev, disconnect_req);
		break;

	case ME_STOP_REQ_CMD:
		/* prepare stop request */
		heci_hdr = (struct heci_msg_hdr *) &dev->ext_msg_buf[0];
		heci_hdr->host_addr = 0;
		heci_hdr->me_addr = 0;
		heci_hdr->length = sizeof(struct hbm_host_stop_request);
		heci_hdr->msg_complete = 1;
		heci_hdr->reserved = 0;
		h_stop_req =
			(struct hbm_host_stop_request *) &dev->ext_msg_buf[1];
		memset(h_stop_req, 0, sizeof(struct hbm_host_stop_request));
		h_stop_req->cmd.cmd = HOST_STOP_REQ_CMD;
		h_stop_req->reason = DRIVER_STOP_REQUEST;
		h_stop_req->reserved[0] = 0;
		h_stop_req->reserved[1] = 0;
		dev->extra_write_index = 2;
		break;

	default:
		BUG();
		break;

	}
}

/**
 * heci_bh_read_pthi_message - bottom half read routine after ISR to
 * handle the read pthi message data processing.
 *
 * @complete_list: An instance of our list structure
 * @dev: Device object for our driver
 * @heci_hdr: header of pthi message
 *
 * returns 0 on success, <0 on failure.
 */
static int heci_bh_read_pthi_message(struct io_heci_list *complete_list,
		struct iamt_heci_device *dev,
		struct heci_msg_hdr *heci_hdr)
{
	struct heci_file_private *file_ext;
	struct heci_cb_private *priv_cb;
	unsigned char *buffer;

	BUG_ON(heci_hdr->me_addr != dev->iamthif_file_ext.me_client_id);
	BUG_ON(dev->iamthif_state != HECI_IAMTHIF_READING);

	buffer = (unsigned char *) (dev->iamthif_msg_buf +
			dev->iamthif_msg_buf_index);
	BUG_ON(sizeof(dev->iamthif_msg_buf) <
			(dev->iamthif_msg_buf_index + heci_hdr->length));

	heci_read_slots(dev, buffer, heci_hdr->length);

	dev->iamthif_msg_buf_index += heci_hdr->length;

	if (!(heci_hdr->msg_complete))
		return 0;

	DBG("pthi_message_buffer_index=%d\n", heci_hdr->length);
	DBG("completed pthi read.\n ");
	if (!dev->iamthif_current_cb)
		return -ENODEV;

	priv_cb = dev->iamthif_current_cb;
	dev->iamthif_current_cb = NULL;

	file_ext = (struct heci_file_private *)priv_cb->file_private;
	if (!file_ext)
		return -ENODEV;

	dev->iamthif_stall_timer = 0;
	priv_cb->information =	dev->iamthif_msg_buf_index;
	priv_cb->read_time = get_seconds();
	if ((dev->iamthif_ioctl) && (file_ext == &dev->iamthif_file_ext)) {
		/* found the iamthif cb */
		DBG("complete the pthi read cb.\n ");
		if (&dev->iamthif_file_ext) {
			DBG("add the pthi read cb to complete.\n ");
			list_add_tail(&priv_cb->cb_list,
				      &complete_list->heci_cb.cb_list);
		}
	}
	return 0;
}

/**
 * _heci_bh_state_ok - check if heci header matches file private data
 *
 * @file_ext: private data of the file object
 * @heci_hdr: header of heci client message
 *
 * returns  !=0 if matches, 0 if no match.
 */
static int _heci_bh_state_ok(struct heci_file_private *file_ext,
					struct heci_msg_hdr *heci_hdr)
{
	return ((file_ext->host_client_id == heci_hdr->host_addr)
		&& (file_ext->me_client_id == heci_hdr->me_addr)
		&& (file_ext->state == HECI_FILE_CONNECTED)
		&& (HECI_READ_COMPLETE != file_ext->reading_state));
}

/**
 * heci_bh_read_client_message - bottom half read routine after ISR to
 * handle the read heci client message data  processing.
 *
 * @complete_list: An instance of our list structure
 * @dev: Device object for our driver
 * @heci_hdr: header of heci client message
 *
 * returns  0 on success, <0 on failure.
 */
static int heci_bh_read_client_message(struct io_heci_list *complete_list,
		struct iamt_heci_device *dev,
		struct heci_msg_hdr *heci_hdr)
{
	struct heci_file_private *file_ext;
	struct heci_cb_private *priv_cb_pos = NULL, *priv_cb_next = NULL;
	unsigned char *buffer = NULL;

	DBG("start client msg\n");
	if (!((dev->read_list.status == 0) &&
	      !list_empty(&dev->read_list.heci_cb.cb_list)))
		goto quit;

	list_for_each_entry_safe(priv_cb_pos, priv_cb_next,
			&dev->read_list.heci_cb.cb_list, cb_list) {
		file_ext = (struct heci_file_private *)
				priv_cb_pos->file_private;
		if ((file_ext != NULL) &&
		    (_heci_bh_state_ok(file_ext, heci_hdr))) {
			spin_lock_bh(&file_ext->read_io_lock);
			file_ext->reading_state = HECI_READING;
			buffer = (unsigned char *)
				(priv_cb_pos->response_buffer.data +
				priv_cb_pos->information);
			BUG_ON(priv_cb_pos->response_buffer.size <
					heci_hdr->length +
					priv_cb_pos->information);

			if (priv_cb_pos->response_buffer.size <
					heci_hdr->length +
					priv_cb_pos->information) {
				DBG("message overflow.\n");
				list_del(&priv_cb_pos->cb_list);
				spin_unlock_bh(&file_ext->read_io_lock);
				return -ENOMEM;
			}
			if (buffer) {
				heci_read_slots(dev, buffer,
						heci_hdr->length);
			}
			priv_cb_pos->information += heci_hdr->length;
			if (heci_hdr->msg_complete) {
				file_ext->status = 0;
				list_del(&priv_cb_pos->cb_list);
				spin_unlock_bh(&file_ext->read_io_lock);
				DBG("completed read host client = %d,"
					"ME client = %d, "
					"data length = %lu\n",
					file_ext->host_client_id,
					file_ext->me_client_id,
					priv_cb_pos->information);

				*(priv_cb_pos->response_buffer.data +
					priv_cb_pos->information) = '\0';
				DBG("priv_cb_pos->res_buffer - %s\n",
					priv_cb_pos->response_buffer.data);
				list_add_tail(&priv_cb_pos->cb_list,
					&complete_list->heci_cb.cb_list);
			} else {
				spin_unlock_bh(&file_ext->read_io_lock);
			}

			break;
		}

	}

quit:
	DBG("message read\n");
	if (!buffer) {
		heci_read_slots(dev, (unsigned char *) dev->rd_msg_buf,
						heci_hdr->length);
		DBG("discarding message, header=%08x.\n",
				*(__u32 *) dev->rd_msg_buf);
	}

	return 0;
}

/**
 * _heci_bh_iamthif_read - prepare to read iamthif data.
 *
 * @dev: Device object for our driver.
 * @slots: free slots.
 *
 * returns  0, OK; otherwise, error.
 */
static int _heci_bh_iamthif_read(struct iamt_heci_device *dev,	__s32 *slots)
{

	if (((*slots) * sizeof(__u32)) >= (sizeof(struct heci_msg_hdr)
			+ sizeof(struct hbm_flow_control))) {
		*slots -= (sizeof(struct heci_msg_hdr) +
				sizeof(struct hbm_flow_control) + 3) / 4;
		if (!heci_send_flow_control(dev, &dev->iamthif_file_ext)) {
			DBG("iamthif flow control failed\n");
		} else {
			DBG("iamthif flow control success\n");
			dev->iamthif_state = HECI_IAMTHIF_READING;
			dev->iamthif_flow_control_pending = 0;
			dev->iamthif_msg_buf_index = 0;
			dev->iamthif_msg_buf_size = 0;
			dev->iamthif_stall_timer = IAMTHIF_STALL_TIMER;
			dev->host_buffer_is_empty = host_buffer_is_empty(dev);
		}
		return 0;
	} else {
		return -ECOMPLETE_MESSAGE;
	}
}

/**
 * _heci_bh_close - process close related operation.
 *
 * @dev: Device object for our driver.
 * @slots: free slots.
 * @priv_cb_pos: callback block.
 * @file_ext: private data of the file object.
 * @cmpl_list: complete list.
 *
 * returns  0, OK; otherwise, error.
 */
static int _heci_bh_close(struct iamt_heci_device *dev,	__s32 *slots,
			struct heci_cb_private *priv_cb_pos,
			struct heci_file_private *file_ext,
			struct io_heci_list *cmpl_list)
{
	if ((*slots * sizeof(__u32)) >= (sizeof(struct heci_msg_hdr) +
			sizeof(struct hbm_client_disconnect_request))) {
		*slots -= (sizeof(struct heci_msg_hdr) +
			sizeof(struct hbm_client_disconnect_request) + 3) / 4;

		if (!heci_disconnect(dev, file_ext)) {
			file_ext->status = 0;
			priv_cb_pos->information = 0;
			list_move_tail(&priv_cb_pos->cb_list,
					&cmpl_list->heci_cb.cb_list);
			return -ECOMPLETE_MESSAGE;
		} else {
			file_ext->state = HECI_FILE_DISCONNECTING;
			file_ext->status = 0;
			priv_cb_pos->information = 0;
			list_move_tail(&priv_cb_pos->cb_list,
					&dev->ctrl_rd_list.heci_cb.cb_list);
			file_ext->timer_count = HECI_CONNECT_TIMEOUT;
		}
	} else {
		/* return the cancel routine */
		return -ECORRUPTED_MESSAGE_HEADER;
	}

	return 0;
}

/**
 * _heci_hb_close - process read related operation.
 *
 * @dev: Device object for our driver.
 * @slots: free slots.
 * @priv_cb_pos: callback block.
 * @file_ext: private data of the file object.
 * @cmpl_list: complete list.
 *
 * returns 0, OK; otherwise, error.
 */
static int _heci_bh_read(struct iamt_heci_device *dev,	__s32 *slots,
			struct heci_cb_private *priv_cb_pos,
			struct heci_file_private *file_ext,
			struct io_heci_list *cmpl_list)
{
	if ((*slots * sizeof(__u32)) >= (sizeof(struct heci_msg_hdr) +
			sizeof(struct hbm_flow_control))) {
		*slots -= (sizeof(struct heci_msg_hdr) +
			sizeof(struct hbm_flow_control) + 3) / 4;
		if (!heci_send_flow_control(dev, file_ext)) {
			file_ext->status = -ENODEV;
			priv_cb_pos->information = 0;
			list_move_tail(&priv_cb_pos->cb_list,
					&cmpl_list->heci_cb.cb_list);
			return -ENODEV;
		} else {
			list_move_tail(&priv_cb_pos->cb_list,
					&dev->read_list.heci_cb.cb_list);
		}
	} else {
		/* return the cancel routine */
		list_del(&priv_cb_pos->cb_list);
		return -ECORRUPTED_MESSAGE_HEADER;
	}

	return 0;
}


/**
 * _heci_bh_ioctl - process ioctl related operation.
 *
 * @dev: Device object for our driver.
 * @slots: free slots.
 * @priv_cb_pos: callback block.
 * @file_ext: private data of the file object.
 * @cmpl_list: complete list.
 *
 * returns  0, OK; otherwise, error.
 */
static int _heci_bh_ioctl(struct iamt_heci_device *dev,	__s32 *slots,
			struct heci_cb_private *priv_cb_pos,
			struct heci_file_private *file_ext,
			struct io_heci_list *cmpl_list)
{
	if ((*slots * sizeof(__u32)) >= (sizeof(struct heci_msg_hdr) +
			sizeof(struct hbm_client_connect_request))) {
		file_ext->state = HECI_FILE_CONNECTING;
		*slots -= (sizeof(struct heci_msg_hdr) +
			sizeof(struct hbm_client_connect_request) + 3) / 4;
		if (!heci_connect(dev, file_ext)) {
			file_ext->status = -ENODEV;
			priv_cb_pos->information = 0;
			list_del(&priv_cb_pos->cb_list);
			return -ENODEV;
		} else {
			list_move_tail(&priv_cb_pos->cb_list,
				&dev->ctrl_rd_list.heci_cb.cb_list);
			file_ext->timer_count = HECI_CONNECT_TIMEOUT;
		}
	} else {
		/* return the cancel routine */
		list_del(&priv_cb_pos->cb_list);
		return -ECORRUPTED_MESSAGE_HEADER;
	}

	return 0;
}

/**
 * _heci_bh_cmpl - process completed and no-iamthif operation.
 *
 * @dev: Device object for our driver.
 * @slots: free slots.
 * @priv_cb_pos: callback block.
 * @file_ext: private data of the file object.
 * @cmpl_list: complete list.
 *
 * returns  0, OK; otherwise, error.
 */
static int _heci_bh_cmpl(struct iamt_heci_device *dev,	__s32 *slots,
			struct heci_cb_private *priv_cb_pos,
			struct heci_file_private *file_ext,
			struct io_heci_list *cmpl_list)
{
	struct heci_msg_hdr *heci_hdr;

	if ((*slots * sizeof(__u32)) >= (sizeof(struct heci_msg_hdr) +
			(priv_cb_pos->request_buffer.size -
			priv_cb_pos->information))) {
		heci_hdr = (struct heci_msg_hdr *) &dev->wr_msg_buf[0];
		heci_hdr->host_addr = file_ext->host_client_id;
		heci_hdr->me_addr = file_ext->me_client_id;
		heci_hdr->length = ((priv_cb_pos->request_buffer.size) -
				(priv_cb_pos->information));
		heci_hdr->msg_complete = 1;
		heci_hdr->reserved = 0;
		DBG("priv_cb_pos->request_buffer.size =%d"
			"heci_hdr->msg_complete= %d\n",
				priv_cb_pos->request_buffer.size,
				heci_hdr->msg_complete);
		DBG("priv_cb_pos->information  =%lu\n",
				priv_cb_pos->information);
		DBG("heci_hdr->length  =%d\n",
				heci_hdr->length);
		*slots -= (sizeof(struct heci_msg_hdr) +
				heci_hdr->length + 3) / 4;
		if (!heci_write_message(dev, heci_hdr,
				(unsigned char *)
				(priv_cb_pos->request_buffer.data +
				priv_cb_pos->information),
				heci_hdr->length)) {
			file_ext->status = -ENODEV;
			list_move_tail(&priv_cb_pos->cb_list,
				&cmpl_list->heci_cb.cb_list);
			return -ENODEV;
		} else {
			flow_ctrl_reduce(dev, file_ext);
			file_ext->status = 0;
			priv_cb_pos->information += heci_hdr->length;
			list_move_tail(&priv_cb_pos->cb_list,
				&dev->write_waiting_list.heci_cb.cb_list);
		}
	} else if (*slots == ((dev->host_hw_state & H_CBD) >> 24)) {
		/* buffer is still empty */
		heci_hdr = (struct heci_msg_hdr *) &dev->wr_msg_buf[0];
		heci_hdr->host_addr = file_ext->host_client_id;
		heci_hdr->me_addr = file_ext->me_client_id;
		heci_hdr->length =
			(*slots * sizeof(__u32)) - sizeof(struct heci_msg_hdr);
		heci_hdr->msg_complete = 0;
		heci_hdr->reserved = 0;

		(*slots) -= (sizeof(struct heci_msg_hdr) +
				heci_hdr->length + 3) / 4;
		if (!heci_write_message(dev, heci_hdr,
					(unsigned char *)
					(priv_cb_pos->request_buffer.data +
					priv_cb_pos->information),
					heci_hdr->length)) {
			file_ext->status = -ENODEV;
			list_move_tail(&priv_cb_pos->cb_list,
				&cmpl_list->heci_cb.cb_list);
			return -ENODEV;
		} else {
			priv_cb_pos->information += heci_hdr->length;
			DBG("priv_cb_pos->request_buffer.size =%d"
					" heci_hdr->msg_complete= %d\n",
					priv_cb_pos->request_buffer.size,
					heci_hdr->msg_complete);
			DBG("priv_cb_pos->information  =%lu\n",
					priv_cb_pos->information);
			DBG("heci_hdr->length  =%d\n", heci_hdr->length);
		}
		return -ECOMPLETE_MESSAGE;
	} else {
		return -ECORRUPTED_MESSAGE_HEADER;
	}

	return 0;
}

/**
 * _heci_bh_cmpl_iamthif - process completed iamthif operation.
 *
 * @dev: Device object for our driver.
 * @slots: free slots.
 * @priv_cb_pos: callback block.
 * @file_ext: private data of the file object.
 * @cmpl_list: complete list.
 *
 * returns  0, OK; otherwise, error.
 */
static int _heci_bh_cmpl_iamthif(struct iamt_heci_device *dev, __s32 *slots,
			struct heci_cb_private *priv_cb_pos,
			struct heci_file_private *file_ext,
			struct io_heci_list *cmpl_list)
{
	struct heci_msg_hdr *heci_hdr;

	if ((*slots * sizeof(__u32)) >= (sizeof(struct heci_msg_hdr) +
			dev->iamthif_msg_buf_size -
			dev->iamthif_msg_buf_index)) {
		heci_hdr = (struct heci_msg_hdr *) &dev->wr_msg_buf[0];
		heci_hdr->host_addr = file_ext->host_client_id;
		heci_hdr->me_addr = file_ext->me_client_id;
		heci_hdr->length = dev->iamthif_msg_buf_size -
			dev->iamthif_msg_buf_index;
		heci_hdr->msg_complete = 1;
		heci_hdr->reserved = 0;

		*slots -= (sizeof(struct heci_msg_hdr) +
				heci_hdr->length + 3) / 4;

		if (!heci_write_message(dev, heci_hdr,
					(dev->iamthif_msg_buf +
					dev->iamthif_msg_buf_index),
					heci_hdr->length)) {
			dev->iamthif_state = HECI_IAMTHIF_IDLE;
			file_ext->status = -ENODEV;
			list_del(&priv_cb_pos->cb_list);
			return -ENODEV;
		} else {
			flow_ctrl_reduce(dev, file_ext);
			dev->iamthif_msg_buf_index += heci_hdr->length;
			priv_cb_pos->information = dev->iamthif_msg_buf_index;
			file_ext->status = 0;
			dev->iamthif_state = HECI_IAMTHIF_FLOW_CONTROL;
			dev->iamthif_flow_control_pending = 1;
			/* save iamthif cb sent to pthi client */
			dev->iamthif_current_cb = priv_cb_pos;
			list_move_tail(&priv_cb_pos->cb_list,
				&dev->write_waiting_list.heci_cb.cb_list);

		}
	} else if (*slots == ((dev->host_hw_state & H_CBD) >> 24)) {
			/* buffer is still empty */
		heci_hdr = (struct heci_msg_hdr *) &dev->wr_msg_buf[0];
		heci_hdr->host_addr = file_ext->host_client_id;
		heci_hdr->me_addr = file_ext->me_client_id;
		heci_hdr->length =
			(*slots * sizeof(__u32)) - sizeof(struct heci_msg_hdr);
		heci_hdr->msg_complete = 0;
		heci_hdr->reserved = 0;

		*slots -= (sizeof(struct heci_msg_hdr) +
				heci_hdr->length + 3) / 4;

		if (!heci_write_message(dev, heci_hdr,
					(dev->iamthif_msg_buf +
					dev->iamthif_msg_buf_index),
					heci_hdr->length)) {
			file_ext->status = -ENODEV;
			list_del(&priv_cb_pos->cb_list);
		} else {
			dev->iamthif_msg_buf_index += heci_hdr->length;
		}
		return -ECOMPLETE_MESSAGE;
	} else {
		return -ECORRUPTED_MESSAGE_HEADER;
	}

	return 0;
}

/**
 * heci_bh_write_handler - bottom half write routine after
 * ISR to handle the write processing.
 *
 * @cmpl_list: An instance of our list structure
 * @dev: Device object for our driver
 * @slots: slots to write.
 *
 * returns 0 on success, <0 on failure.
 */
static int heci_bh_write_handler(struct io_heci_list *cmpl_list,
		struct iamt_heci_device *dev,
		__s32 *slots)
{

	struct heci_file_private *file_ext;
	struct heci_cb_private *priv_cb_pos = NULL, *priv_cb_next = NULL;
	struct io_heci_list *list;
	int ret;

	if (!host_buffer_is_empty(dev)) {
		DBG("host buffer is not empty.\n");
		return 0;
	}
	dev->write_hang = -1;
	*slots = count_empty_write_slots(dev);
	/* complete all waiting for write CB */
	DBG("complete all waiting for write cb.\n");

	list = &dev->write_waiting_list;
	if ((list->status == 0)
	    && !list_empty(&list->heci_cb.cb_list)) {
		list_for_each_entry_safe(priv_cb_pos, priv_cb_next,
				&list->heci_cb.cb_list, cb_list) {
			file_ext = (struct heci_file_private *)
					priv_cb_pos->file_private;
			if (file_ext != NULL) {
				file_ext->status = 0;
				list_del(&priv_cb_pos->cb_list);
				if ((HECI_WRITING == file_ext->writing_state) &&
					(priv_cb_pos->major_file_operations ==
						HECI_WRITE) &&
					(file_ext != &dev->iamthif_file_ext)) {
					DBG("HECI WRITE COMPLETE\n");
					file_ext->writing_state =
						HECI_WRITE_COMPLETE;
					list_add_tail(&priv_cb_pos->cb_list,
						&cmpl_list->heci_cb.cb_list);
				}
				if (file_ext == &dev->iamthif_file_ext) {
					DBG("check iamthif flow control.\n");
					if (dev->iamthif_flow_control_pending) {
						ret = _heci_bh_iamthif_read(dev,
									slots);
						if (ret != 0)
							return ret;
					}
				}
			}

		}
	}

	if ((dev->stop) && (!dev->wd_pending)) {
		dev->wd_stoped = 1;
		wake_up_interruptible(&dev->wait_stop_wd);
		return 0;
	}

	if (dev->extra_write_index != 0) {
		DBG("extra_write_index =%d.\n",	dev->extra_write_index);
		heci_write_message(dev,
				(struct heci_msg_hdr *) &dev->ext_msg_buf[0],
				(unsigned char *) &dev->ext_msg_buf[1],
				(dev->extra_write_index - 1) * sizeof(__u32));
		*slots -= dev->extra_write_index;
		dev->extra_write_index = 0;
	}
	if (dev->heci_state == HECI_ENABLED) {
		if ((dev->wd_pending)
		    && flow_ctrl_creds(dev, &dev->wd_file_ext)) {
			if (!heci_send_wd(dev))
				DBG("wd send failed.\n");
			else
				flow_ctrl_reduce(dev, &dev->wd_file_ext);

			dev->wd_pending = 0;

			if (dev->wd_timeout != 0) {
				*slots -= (sizeof(struct heci_msg_hdr) +
					 HECI_START_WD_DATA_SIZE + 3) / 4;
				dev->wd_due_counter = 2;
			} else {
				*slots -= (sizeof(struct heci_msg_hdr) +
					 HECI_WD_PARAMS_SIZE + 3) / 4;
				dev->wd_due_counter = 0;
			}

		}
	}
	if (dev->stop)
		return ~ENODEV;

	/* complete control write list CB */
	if (dev->ctrl_wr_list.status == 0) {
		/* complete control write list CB */
		DBG("complete control write list cb.\n");
		list_for_each_entry_safe(priv_cb_pos, priv_cb_next,
				&dev->ctrl_wr_list.heci_cb.cb_list, cb_list) {
			file_ext = (struct heci_file_private *)
				priv_cb_pos->file_private;
			if (file_ext == NULL) {
				list_del(&priv_cb_pos->cb_list);
				return -ENODEV;
			}
			switch (priv_cb_pos->major_file_operations) {
			case HECI_CLOSE:
				/* send disconnect message */
				ret = _heci_bh_close(dev, slots,
						     priv_cb_pos,
						     file_ext, cmpl_list);
				if (ret != 0)
					return ret;

				break;
			case HECI_READ:
				/* send flow control message */
				ret = _heci_bh_read(dev, slots,
						    priv_cb_pos,
						    file_ext, cmpl_list);
				if (ret != 0)
					return ret;

				break;
			case HECI_IOCTL:
				/* connect message */
				if (!other_client_is_connecting(dev, file_ext))
					continue;
				ret = _heci_bh_ioctl(dev, slots,
						     priv_cb_pos,
						     file_ext, cmpl_list);
				if (ret != 0)
					return ret;

				break;

			default:
				BUG();
			}

		}
	}
	/* complete  write list CB */
	if ((dev->write_list.status == 0)
	    && !list_empty(&dev->write_list.heci_cb.cb_list)) {
		DBG("complete write list cb.\n");
		list_for_each_entry_safe(priv_cb_pos, priv_cb_next,
				&dev->write_list.heci_cb.cb_list, cb_list) {
			file_ext = (struct heci_file_private *)
					priv_cb_pos->file_private;

			if (file_ext != NULL) {
				if (file_ext != &dev->iamthif_file_ext) {
					if (!flow_ctrl_creds(dev, file_ext)) {
						DBG("No flow control"
						    " credentials for client"
						    " %d, not sending.\n",
						    file_ext->host_client_id);
						continue;
					}
					ret = _heci_bh_cmpl(dev, slots,
							    priv_cb_pos,
							    file_ext,
							    cmpl_list);
					if (ret != 0)
						return ret;

				} else if (file_ext == &dev->iamthif_file_ext) {
					/* IAMTHIF IOCTL */
					DBG("complete pthi write cb.\n");
					if (!flow_ctrl_creds(dev, file_ext)) {
						DBG("No flow control"
						    " credentials for pthi"
						    " client %d.\n",
						    file_ext->host_client_id);
						continue;
					}
					ret = _heci_bh_cmpl_iamthif(dev, slots,
								   priv_cb_pos,
								   file_ext,
								   cmpl_list);
					if (ret != 0)
						return ret;

				}
			}

		}
	}
	return 0;
}


/**
 * is_treat_specially_client  - check if the message belong
 * to the file private data.
 *
 * @file_ext: private data of the file object
 * @rs: connect response bus message
 * @dev: Device object for our driver
 *
 * returns 0 on success, <0 on failure.
 */
static int is_treat_specially_client(struct heci_file_private *file_ext,
		struct hbm_client_connect_response *rs)
{
	int ret = 0;

	if ((file_ext->host_client_id == rs->host_addr) &&
	    (file_ext->me_client_id == rs->me_addr)) {
		if (rs->status == 0) {
			DBG("client connect status = 0x%08x.\n", rs->status);
			file_ext->state = HECI_FILE_CONNECTED;
			file_ext->status = 0;
		} else {
			DBG("client connect status = 0x%08x.\n", rs->status);
			file_ext->state = HECI_FILE_DISCONNECTED;
			file_ext->status = -ENODEV;
		}
		ret = 1;
	}
	DBG("client state = %d.\n", file_ext->state);
	return ret;
}

/**
 * heci_client_connect_response  - connect response bh routine
 *
 * @dev: Device object for our driver
 * @rs: connect response bus message
 */
static void heci_client_connect_response(struct iamt_heci_device *dev,
		struct hbm_client_connect_response *rs)
{

	struct heci_file_private *file_ext;
	struct heci_cb_private *priv_cb_pos = NULL, *priv_cb_next = NULL;

	/* if WD or iamthif client treat specially */

	if ((is_treat_specially_client(&(dev->wd_file_ext), rs)) ||
	    (is_treat_specially_client(&(dev->iamthif_file_ext), rs)))
		return;

	if (dev->ctrl_rd_list.status == 0
	    && !list_empty(&dev->ctrl_rd_list.heci_cb.cb_list)) {
		list_for_each_entry_safe(priv_cb_pos, priv_cb_next,
			&dev->ctrl_rd_list.heci_cb.cb_list, cb_list) {
			file_ext = (struct heci_file_private *)
					priv_cb_pos->file_private;
			if (file_ext == NULL) {
				list_del(&priv_cb_pos->cb_list);
				return;
			}
			if (HECI_IOCTL == priv_cb_pos->major_file_operations) {
				if (is_treat_specially_client(file_ext, rs)) {
					list_del(&priv_cb_pos->cb_list);
					file_ext->status = 0;
					file_ext->timer_count = 0;
					break;
				}
			}
		}
	}
}

/**
 * heci_client_disconnect_response  - disconnect response bh routine
 *
 * @dev: Device object for our driver
 * @rs: disconnect response bus message
 */
static void heci_client_disconnect_response(struct iamt_heci_device *dev,
					struct hbm_client_connect_response *rs)
{
	struct heci_file_private *file_ext;
	struct heci_cb_private *priv_cb_pos = NULL, *priv_cb_next = NULL;

	if (dev->ctrl_rd_list.status == 0
	    && !list_empty(&dev->ctrl_rd_list.heci_cb.cb_list)) {
		list_for_each_entry_safe(priv_cb_pos, priv_cb_next,
				&dev->ctrl_rd_list.heci_cb.cb_list, cb_list) {
			file_ext = (struct heci_file_private *)
				priv_cb_pos->file_private;

			if (file_ext == NULL) {
				list_del(&priv_cb_pos->cb_list);
				return;
			}

			DBG("list_for_each_entry_safe in ctrl_rd_list.\n");
			if ((file_ext->host_client_id == rs->host_addr) &&
				(file_ext->me_client_id == rs->me_addr)) {

				list_del(&priv_cb_pos->cb_list);
				if (rs->status == 0) {
					file_ext->state =
					    HECI_FILE_DISCONNECTED;
				}

				file_ext->status = 0;
				file_ext->timer_count = 0;
				break;
			}
		}
	}
}

/**
 * same_flow_addr - tell they have same address.
 *
 * @file: private data of the file object.
 * @flow: flow control.
 *
 * returns  !=0, same; 0,not.
 */
static int same_flow_addr(struct heci_file_private *file,
					struct hbm_flow_control *flow)
{
	return ((file->host_client_id == flow->host_addr)
		&& (file->me_client_id == flow->me_addr));
}

/**
 * add_single_flow_creds - add single buffer credentials.
 *
 * @file: private data ot the file object.
 * @flow: flow control.
 */
static void add_single_flow_creds(struct iamt_heci_device *dev,
				  struct hbm_flow_control *flow)
{
	struct heci_me_client *client;
	int i;

	for (i = 0; i < dev->num_heci_me_clients; i++) {
		client = &dev->me_clients[i];
		if ((client != NULL) &&
		    (flow->me_addr == client->client_id)) {
			if (client->props.single_recv_buf != 0) {
				client->flow_ctrl_creds++;
				DBG("recv flow ctrl msg ME %d (single).\n",
				    flow->me_addr);
				DBG("flow control credentials=%d.\n",
				    client->flow_ctrl_creds);
			} else {
				BUG();	/* error in flow control */
			}
		}
	}
}

/**
 * heci_client_flow_control_response  - flow control response bh routine
 *
 * @dev: Device object for our driver
 * @flow_control: flow control response bus message
 */
static void heci_client_flow_control_response(struct iamt_heci_device *dev,
		struct hbm_flow_control *flow_control)
{
	struct heci_file_private *file_pos = NULL;
	struct heci_file_private *file_next = NULL;

	if (flow_control->host_addr == 0) {
		/* single receive buffer */
		add_single_flow_creds(dev, flow_control);
	} else {
		/* normal connection */
		list_for_each_entry_safe(file_pos, file_next,
				&dev->file_list, link) {
			DBG("list_for_each_entry_safe in file_list\n");

			DBG("file_ext of host client %d ME client %d.\n",
			    file_pos->host_client_id,
			    file_pos->me_client_id);
			DBG("flow ctrl msg for host %d ME %d.\n",
			    flow_control->host_addr,
			    flow_control->me_addr);
			if (same_flow_addr(file_pos, flow_control)) {
				DBG("recv ctrl msg for host  %d ME %d.\n",
				    flow_control->host_addr,
				    flow_control->me_addr);
				file_pos->flow_ctrl_creds++;
				DBG("flow control credentials=%d.\n",
				    file_pos->flow_ctrl_creds);
				break;
			}
		}
	}
}

/**
 * same_disconn_addr - tell they have same address
 *
 * @file: private data of the file object.
 * @disconn: disconnection request.
 *
 * returns !=0, same; 0,not.
 */
static int same_disconn_addr(struct heci_file_private *file,
			     struct hbm_client_disconnect_request *disconn)
{
	return ((file->host_client_id == disconn->host_addr)
		&& (file->me_client_id == disconn->me_addr));
}

/**
 * heci_client_disconnect_request  - disconnect request bh routine
 *
 * @dev: Device object for our driver.
 * @disconnect_req: disconnect request bus message.
 */
static void heci_client_disconnect_request(struct iamt_heci_device *dev,
		struct hbm_client_disconnect_request *disconnect_req)
{
	struct heci_msg_hdr *heci_hdr;
	struct hbm_client_connect_response *disconnect_res;
	struct heci_file_private *file_pos = NULL;
	struct heci_file_private *file_next = NULL;

	list_for_each_entry_safe(file_pos, file_next, &dev->file_list, link) {
		if (same_disconn_addr(file_pos, disconnect_req)) {
			DBG("disconnect request host client %d ME client %d.\n",
					disconnect_req->host_addr,
					disconnect_req->me_addr);
			file_pos->state = HECI_FILE_DISCONNECTED;
			file_pos->timer_count = 0;
			if (file_pos == &dev->wd_file_ext) {
				dev->wd_due_counter = 0;
				dev->wd_pending = 0;
			} else if (file_pos == &dev->iamthif_file_ext)
				dev->iamthif_timer = 0;

			/* prepare disconnect response */
			heci_hdr =
				(struct heci_msg_hdr *) &dev->ext_msg_buf[0];
			heci_hdr->host_addr = 0;
			heci_hdr->me_addr = 0;
			heci_hdr->length =
				sizeof(struct hbm_client_connect_response);
			heci_hdr->msg_complete = 1;
			heci_hdr->reserved = 0;

			disconnect_res =
				(struct hbm_client_connect_response *)
				&dev->ext_msg_buf[1];
			disconnect_res->host_addr = file_pos->host_client_id;
			disconnect_res->me_addr = file_pos->me_client_id;
			*(__u8 *) (&disconnect_res->cmd) =
				CLIENT_DISCONNECT_RES_CMD;
			disconnect_res->status = 0;
			dev->extra_write_index = 2;
			break;
		}
	}
}

/**
 * heci_timer - timer function.
 *
 * @data: pointer to the device structure
 *
 * NOTE: This function is called by timer interrupt work
 */
void heci_wd_timer(unsigned long data)
{
	struct iamt_heci_device *dev = (struct iamt_heci_device *) data;

	DBG("send watchdog.\n");
	spin_lock_bh(&dev->device_lock);
	if (dev->heci_state != HECI_ENABLED) {
		mod_timer(&dev->wd_timer, round_jiffies(jiffies + 2 * HZ));
		spin_unlock_bh(&dev->device_lock);
		return;
	}
	if (dev->wd_file_ext.state != HECI_FILE_CONNECTED) {
		mod_timer(&dev->wd_timer, round_jiffies(jiffies + 2 * HZ));
		spin_unlock_bh(&dev->device_lock);
		return;
	}
	/* Watchdog */
	if ((dev->wd_due_counter != 0) && (dev->wd_bypass == 0)) {
		if (--dev->wd_due_counter == 0) {
			if (dev->host_buffer_is_empty &&
			    flow_ctrl_creds(dev, &dev->wd_file_ext)) {
				dev->host_buffer_is_empty = 0;
				if (!heci_send_wd(dev)) {
					DBG("wd send failed.\n");
				} else {
					flow_ctrl_reduce(dev,
							 &dev->wd_file_ext);
				}

				if (dev->wd_timeout != 0)
					dev->wd_due_counter = 2;
				else
					dev->wd_due_counter = 0;

			} else
				dev->wd_pending = 1;

		}
	}
	if (dev->iamthif_stall_timer != 0) {
		if (--dev->iamthif_stall_timer == 0) {
			DBG("reseting because of hang to PTHI.\n");
			heci_reset(dev, 1);
			dev->iamthif_msg_buf_size = 0;
			dev->iamthif_msg_buf_index = 0;
			dev->iamthif_canceled = 0;
			dev->iamthif_ioctl = 1;
			dev->iamthif_state = HECI_IAMTHIF_IDLE;
			dev->iamthif_timer = 0;
			spin_unlock_bh(&dev->device_lock);

			if (dev->iamthif_current_cb)
				heci_free_cb_private(dev->iamthif_current_cb);

			spin_lock_bh(&dev->device_lock);
			dev->iamthif_file_object = NULL;
			dev->iamthif_current_cb = NULL;
			run_next_iamthif_cmd(dev);
		}
	}
	mod_timer(&dev->wd_timer, round_jiffies(jiffies + 2 * HZ));
	spin_unlock_bh(&dev->device_lock);
}