summaryrefslogtreecommitdiff
path: root/drivers/scsi/esas2r/esas2r_main.c
blob: f700a16cd88534c1edc9fb0ed8ed8b8d1db4fe51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
/*
 *  linux/drivers/scsi/esas2r/esas2r_main.c
 *      For use with ATTO ExpressSAS R6xx SAS/SATA RAID controllers
 *
 *  Copyright (c) 2001-2013 ATTO Technology, Inc.
 *  (mailto:linuxdrivers@attotech.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, 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.
 *
 * NO WARRANTY
 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
 * solely responsible for determining the appropriateness of using and
 * distributing the Program and assumes all risks associated with its
 * exercise of rights under this Agreement, including but not limited to
 * the risks and costs of program errors, damage to or loss of data,
 * programs or equipment, and unavailability or interruption of operations.
 *
 * DISCLAIMER OF LIABILITY
 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 * USA.
 */

#include "esas2r.h"

MODULE_DESCRIPTION(ESAS2R_DRVR_NAME ": " ESAS2R_LONGNAME " driver");
MODULE_AUTHOR("ATTO Technology, Inc.");
MODULE_LICENSE("GPL");
MODULE_VERSION(ESAS2R_VERSION_STR);

/* global definitions */

static int found_adapters;
struct esas2r_adapter *esas2r_adapters[MAX_ADAPTERS];

#define ESAS2R_VDA_EVENT_PORT1       54414
#define ESAS2R_VDA_EVENT_PORT2       54415
#define ESAS2R_VDA_EVENT_SOCK_COUNT  2

static struct esas2r_adapter *esas2r_adapter_from_kobj(struct kobject *kobj)
{
	struct device *dev = container_of(kobj, struct device, kobj);
	struct Scsi_Host *host = class_to_shost(dev);

	return (struct esas2r_adapter *)host->hostdata;
}

static ssize_t read_fw(struct file *file, struct kobject *kobj,
		       struct bin_attribute *attr,
		       char *buf, loff_t off, size_t count)
{
	struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);

	return esas2r_read_fw(a, buf, off, count);
}

static ssize_t write_fw(struct file *file, struct kobject *kobj,
			struct bin_attribute *attr,
			char *buf, loff_t off, size_t count)
{
	struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);

	return esas2r_write_fw(a, buf, off, count);
}

static ssize_t read_fs(struct file *file, struct kobject *kobj,
		       struct bin_attribute *attr,
		       char *buf, loff_t off, size_t count)
{
	struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);

	return esas2r_read_fs(a, buf, off, count);
}

static ssize_t write_fs(struct file *file, struct kobject *kobj,
			struct bin_attribute *attr,
			char *buf, loff_t off, size_t count)
{
	struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
	int length = min(sizeof(struct esas2r_ioctl_fs), count);
	int result = 0;

	result = esas2r_write_fs(a, buf, off, count);

	if (result < 0)
		result = 0;

	return length;
}

static ssize_t read_vda(struct file *file, struct kobject *kobj,
			struct bin_attribute *attr,
			char *buf, loff_t off, size_t count)
{
	struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);

	return esas2r_read_vda(a, buf, off, count);
}

static ssize_t write_vda(struct file *file, struct kobject *kobj,
			 struct bin_attribute *attr,
			 char *buf, loff_t off, size_t count)
{
	struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);

	return esas2r_write_vda(a, buf, off, count);
}

static ssize_t read_live_nvram(struct file *file, struct kobject *kobj,
			       struct bin_attribute *attr,
			       char *buf, loff_t off, size_t count)
{
	struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
	int length = min_t(size_t, sizeof(struct esas2r_sas_nvram), PAGE_SIZE);

	memcpy(buf, a->nvram, length);
	return length;
}

static ssize_t write_live_nvram(struct file *file, struct kobject *kobj,
				struct bin_attribute *attr,
				char *buf, loff_t off, size_t count)
{
	struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
	struct esas2r_request *rq;
	int result = -EFAULT;

	rq = esas2r_alloc_request(a);
	if (rq == NULL)
		return -ENOMEM;

	if (esas2r_write_params(a, rq, (struct esas2r_sas_nvram *)buf))
		result = count;

	esas2r_free_request(a, rq);

	return result;
}

static ssize_t read_default_nvram(struct file *file, struct kobject *kobj,
				  struct bin_attribute *attr,
				  char *buf, loff_t off, size_t count)
{
	struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);

	esas2r_nvram_get_defaults(a, (struct esas2r_sas_nvram *)buf);

	return sizeof(struct esas2r_sas_nvram);
}

static ssize_t read_hw(struct file *file, struct kobject *kobj,
		       struct bin_attribute *attr,
		       char *buf, loff_t off, size_t count)
{
	struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
	int length = min_t(size_t, sizeof(struct atto_ioctl), PAGE_SIZE);

	if (!a->local_atto_ioctl)
		return -ENOMEM;

	if (handle_hba_ioctl(a, a->local_atto_ioctl) != IOCTL_SUCCESS)
		return -ENOMEM;

	memcpy(buf, a->local_atto_ioctl, length);

	return length;
}

static ssize_t write_hw(struct file *file, struct kobject *kobj,
			struct bin_attribute *attr,
			char *buf, loff_t off, size_t count)
{
	struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
	int length = min(sizeof(struct atto_ioctl), count);

	if (!a->local_atto_ioctl) {
		a->local_atto_ioctl = kmalloc(sizeof(struct atto_ioctl),
					      GFP_KERNEL);
		if (a->local_atto_ioctl == NULL) {
			esas2r_log(ESAS2R_LOG_WARN,
				   "write_hw kzalloc failed for %zu bytes",
				   sizeof(struct atto_ioctl));
			return -ENOMEM;
		}
	}

	memset(a->local_atto_ioctl, 0, sizeof(struct atto_ioctl));
	memcpy(a->local_atto_ioctl, buf, length);

	return length;
}

#define ESAS2R_RW_BIN_ATTR(_name) \
	struct bin_attribute bin_attr_ ## _name = { \
		.attr	= \
		{ .name = __stringify(_name), .mode  = S_IRUSR | S_IWUSR }, \
		.size	= 0, \
		.read	= read_ ## _name, \
		.write	= write_ ## _name }

ESAS2R_RW_BIN_ATTR(fw);
ESAS2R_RW_BIN_ATTR(fs);
ESAS2R_RW_BIN_ATTR(vda);
ESAS2R_RW_BIN_ATTR(hw);
ESAS2R_RW_BIN_ATTR(live_nvram);

struct bin_attribute bin_attr_default_nvram = {
	.attr	= { .name = "default_nvram", .mode = S_IRUGO },
	.size	= 0,
	.read	= read_default_nvram,
	.write	= NULL
};

static const struct scsi_host_template driver_template = {
	.module				= THIS_MODULE,
	.show_info			= esas2r_show_info,
	.name				= ESAS2R_LONGNAME,
	.info				= esas2r_info,
	.ioctl				= esas2r_ioctl,
	.queuecommand			= esas2r_queuecommand,
	.eh_abort_handler		= esas2r_eh_abort,
	.eh_device_reset_handler	= esas2r_device_reset,
	.eh_bus_reset_handler		= esas2r_bus_reset,
	.eh_host_reset_handler		= esas2r_host_reset,
	.eh_target_reset_handler	= esas2r_target_reset,
	.can_queue			= 128,
	.this_id			= -1,
	.sg_tablesize			= SG_CHUNK_SIZE,
	.cmd_per_lun			=
		ESAS2R_DEFAULT_CMD_PER_LUN,
	.proc_name			= ESAS2R_DRVR_NAME,
	.change_queue_depth		= scsi_change_queue_depth,
	.max_sectors			= 0xFFFF,
};

int sgl_page_size = 512;
module_param(sgl_page_size, int, 0);
MODULE_PARM_DESC(sgl_page_size,
		 "Scatter/gather list (SGL) page size in number of S/G "
		 "entries.  If your application is doing a lot of very large "
		 "transfers, you may want to increase the SGL page size.  "
		 "Default 512.");

int num_sg_lists = 1024;
module_param(num_sg_lists, int, 0);
MODULE_PARM_DESC(num_sg_lists,
		 "Number of scatter/gather lists.  Default 1024.");

int sg_tablesize = SG_CHUNK_SIZE;
module_param(sg_tablesize, int, 0);
MODULE_PARM_DESC(sg_tablesize,
		 "Maximum number of entries in a scatter/gather table.");

int num_requests = 256;
module_param(num_requests, int, 0);
MODULE_PARM_DESC(num_requests,
		 "Number of requests.  Default 256.");

int num_ae_requests = 4;
module_param(num_ae_requests, int, 0);
MODULE_PARM_DESC(num_ae_requests,
		 "Number of VDA asynchronous event requests.  Default 4.");

int cmd_per_lun = ESAS2R_DEFAULT_CMD_PER_LUN;
module_param(cmd_per_lun, int, 0);
MODULE_PARM_DESC(cmd_per_lun,
		 "Maximum number of commands per LUN.  Default "
		 DEFINED_NUM_TO_STR(ESAS2R_DEFAULT_CMD_PER_LUN) ".");

int can_queue = 128;
module_param(can_queue, int, 0);
MODULE_PARM_DESC(can_queue,
		 "Maximum number of commands per adapter.  Default 128.");

int esas2r_max_sectors = 0xFFFF;
module_param(esas2r_max_sectors, int, 0);
MODULE_PARM_DESC(esas2r_max_sectors,
		 "Maximum number of disk sectors in a single data transfer.  "
		 "Default 65535 (largest possible setting).");

int interrupt_mode = 1;
module_param(interrupt_mode, int, 0);
MODULE_PARM_DESC(interrupt_mode,
		 "Defines the interrupt mode to use.  0 for legacy"
		 ", 1 for MSI.  Default is MSI (1).");

static const struct pci_device_id
	esas2r_pci_table[] = {
	{ ATTO_VENDOR_ID, 0x0049,	  ATTO_VENDOR_ID, 0x0049,
	  0,
	  0, 0 },
	{ ATTO_VENDOR_ID, 0x0049,	  ATTO_VENDOR_ID, 0x004A,
	  0,
	  0, 0 },
	{ ATTO_VENDOR_ID, 0x0049,	  ATTO_VENDOR_ID, 0x004B,
	  0,
	  0, 0 },
	{ ATTO_VENDOR_ID, 0x0049,	  ATTO_VENDOR_ID, 0x004C,
	  0,
	  0, 0 },
	{ ATTO_VENDOR_ID, 0x0049,	  ATTO_VENDOR_ID, 0x004D,
	  0,
	  0, 0 },
	{ ATTO_VENDOR_ID, 0x0049,	  ATTO_VENDOR_ID, 0x004E,
	  0,
	  0, 0 },
	{ 0,		  0,		  0,		  0,
	  0,
	  0, 0 }
};

MODULE_DEVICE_TABLE(pci, esas2r_pci_table);

static int
esas2r_probe(struct pci_dev *pcid, const struct pci_device_id *id);

static void
esas2r_remove(struct pci_dev *pcid);

static struct pci_driver
	esas2r_pci_driver = {
	.name		= ESAS2R_DRVR_NAME,
	.id_table	= esas2r_pci_table,
	.probe		= esas2r_probe,
	.remove		= esas2r_remove,
	.driver.pm	= &esas2r_pm_ops,
};

static int esas2r_probe(struct pci_dev *pcid,
			const struct pci_device_id *id)
{
	struct Scsi_Host *host = NULL;
	struct esas2r_adapter *a;
	int err;

	size_t host_alloc_size = sizeof(struct esas2r_adapter)
				 + ((num_requests) +
				    1) * sizeof(struct esas2r_request);

	esas2r_log_dev(ESAS2R_LOG_DEBG, &(pcid->dev),
		       "esas2r_probe() 0x%02x 0x%02x 0x%02x 0x%02x",
		       pcid->vendor,
		       pcid->device,
		       pcid->subsystem_vendor,
		       pcid->subsystem_device);

	esas2r_log_dev(ESAS2R_LOG_INFO, &(pcid->dev),
		       "before pci_enable_device() "
		       "enable_cnt: %d",
		       pcid->enable_cnt.counter);

	err = pci_enable_device(pcid);
	if (err != 0) {
		esas2r_log_dev(ESAS2R_LOG_CRIT, &(pcid->dev),
			       "pci_enable_device() FAIL (%d)",
			       err);
		return -ENODEV;
	}

	esas2r_log_dev(ESAS2R_LOG_INFO, &(pcid->dev),
		       "pci_enable_device() OK");
	esas2r_log_dev(ESAS2R_LOG_INFO, &(pcid->dev),
		       "after pci_enable_device() enable_cnt: %d",
		       pcid->enable_cnt.counter);

	host = scsi_host_alloc(&driver_template, host_alloc_size);
	if (host == NULL) {
		esas2r_log(ESAS2R_LOG_CRIT, "scsi_host_alloc() FAIL");
		return -ENODEV;
	}

	memset(host->hostdata, 0, host_alloc_size);

	a = (struct esas2r_adapter *)host->hostdata;

	esas2r_log(ESAS2R_LOG_INFO, "scsi_host_alloc() OK host: %p", host);

	/* override max LUN and max target id */

	host->max_id = ESAS2R_MAX_ID + 1;
	host->max_lun = 255;

	/* we can handle 16-byte CDbs */

	host->max_cmd_len = 16;

	host->can_queue = can_queue;
	host->cmd_per_lun = cmd_per_lun;
	host->this_id = host->max_id + 1;
	host->max_channel = 0;
	host->unique_id = found_adapters;
	host->sg_tablesize = sg_tablesize;
	host->max_sectors = esas2r_max_sectors;

	/* set to bus master for BIOses that don't do it for us */

	esas2r_log(ESAS2R_LOG_INFO, "pci_set_master() called");

	pci_set_master(pcid);

	if (!esas2r_init_adapter(host, pcid, found_adapters)) {
		esas2r_log(ESAS2R_LOG_CRIT,
			   "unable to initialize device at PCI bus %x:%x",
			   pcid->bus->number,
			   pcid->devfn);

		esas2r_log_dev(ESAS2R_LOG_INFO, &(host->shost_gendev),
			       "scsi_host_put() called");

		scsi_host_put(host);

		return 0;

	}

	esas2r_log(ESAS2R_LOG_INFO, "pci_set_drvdata(%p, %p) called", pcid,
		   host->hostdata);

	pci_set_drvdata(pcid, host);

	esas2r_log(ESAS2R_LOG_INFO, "scsi_add_host() called");

	err = scsi_add_host(host, &pcid->dev);

	if (err) {
		esas2r_log(ESAS2R_LOG_CRIT, "scsi_add_host returned %d", err);
		esas2r_log_dev(ESAS2R_LOG_CRIT, &(host->shost_gendev),
			       "scsi_add_host() FAIL");

		esas2r_log_dev(ESAS2R_LOG_INFO, &(host->shost_gendev),
			       "scsi_host_put() called");

		scsi_host_put(host);

		esas2r_log_dev(ESAS2R_LOG_INFO, &(host->shost_gendev),
			       "pci_set_drvdata(%p, NULL) called",
			       pcid);

		pci_set_drvdata(pcid, NULL);

		return -ENODEV;
	}


	esas2r_fw_event_on(a);

	esas2r_log_dev(ESAS2R_LOG_INFO, &(host->shost_gendev),
		       "scsi_scan_host() called");

	scsi_scan_host(host);

	/* Add sysfs binary files */
	if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_fw))
		esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
			       "Failed to create sysfs binary file: fw");
	else
		a->sysfs_fw_created = 1;

	if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_fs))
		esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
			       "Failed to create sysfs binary file: fs");
	else
		a->sysfs_fs_created = 1;

	if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_vda))
		esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
			       "Failed to create sysfs binary file: vda");
	else
		a->sysfs_vda_created = 1;

	if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_hw))
		esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
			       "Failed to create sysfs binary file: hw");
	else
		a->sysfs_hw_created = 1;

	if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_live_nvram))
		esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
			       "Failed to create sysfs binary file: live_nvram");
	else
		a->sysfs_live_nvram_created = 1;

	if (sysfs_create_bin_file(&host->shost_dev.kobj,
				  &bin_attr_default_nvram))
		esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
			       "Failed to create sysfs binary file: default_nvram");
	else
		a->sysfs_default_nvram_created = 1;

	found_adapters++;

	return 0;
}

static void esas2r_remove(struct pci_dev *pdev)
{
	struct Scsi_Host *host = pci_get_drvdata(pdev);
	struct esas2r_adapter *a = (struct esas2r_adapter *)host->hostdata;

	esas2r_log_dev(ESAS2R_LOG_INFO, &(pdev->dev),
		       "esas2r_remove(%p) called; "
		       "host:%p", pdev,
		       host);

	esas2r_kill_adapter(a->index);
	found_adapters--;
}

static int __init esas2r_init(void)
{
	int i;

	esas2r_log(ESAS2R_LOG_INFO, "%s called", __func__);

	/* verify valid parameters */

	if (can_queue < 1) {
		esas2r_log(ESAS2R_LOG_WARN,
			   "warning: can_queue must be at least 1, value "
			   "forced.");
		can_queue = 1;
	} else if (can_queue > 2048) {
		esas2r_log(ESAS2R_LOG_WARN,
			   "warning: can_queue must be no larger than 2048, "
			   "value forced.");
		can_queue = 2048;
	}

	if (cmd_per_lun < 1) {
		esas2r_log(ESAS2R_LOG_WARN,
			   "warning: cmd_per_lun must be at least 1, value "
			   "forced.");
		cmd_per_lun = 1;
	} else if (cmd_per_lun > 2048) {
		esas2r_log(ESAS2R_LOG_WARN,
			   "warning: cmd_per_lun must be no larger than "
			   "2048, value forced.");
		cmd_per_lun = 2048;
	}

	if (sg_tablesize < 32) {
		esas2r_log(ESAS2R_LOG_WARN,
			   "warning: sg_tablesize must be at least 32, "
			   "value forced.");
		sg_tablesize = 32;
	}

	if (esas2r_max_sectors < 1) {
		esas2r_log(ESAS2R_LOG_WARN,
			   "warning: esas2r_max_sectors must be at least "
			   "1, value forced.");
		esas2r_max_sectors = 1;
	} else if (esas2r_max_sectors > 0xffff) {
		esas2r_log(ESAS2R_LOG_WARN,
			   "warning: esas2r_max_sectors must be no larger "
			   "than 0xffff, value forced.");
		esas2r_max_sectors = 0xffff;
	}

	sgl_page_size &= ~(ESAS2R_SGL_ALIGN - 1);

	if (sgl_page_size < SGL_PG_SZ_MIN)
		sgl_page_size = SGL_PG_SZ_MIN;
	else if (sgl_page_size > SGL_PG_SZ_MAX)
		sgl_page_size = SGL_PG_SZ_MAX;

	if (num_sg_lists < NUM_SGL_MIN)
		num_sg_lists = NUM_SGL_MIN;
	else if (num_sg_lists > NUM_SGL_MAX)
		num_sg_lists = NUM_SGL_MAX;

	if (num_requests < NUM_REQ_MIN)
		num_requests = NUM_REQ_MIN;
	else if (num_requests > NUM_REQ_MAX)
		num_requests = NUM_REQ_MAX;

	if (num_ae_requests < NUM_AE_MIN)
		num_ae_requests = NUM_AE_MIN;
	else if (num_ae_requests > NUM_AE_MAX)
		num_ae_requests = NUM_AE_MAX;

	/* set up other globals */

	for (i = 0; i < MAX_ADAPTERS; i++)
		esas2r_adapters[i] = NULL;

	return pci_register_driver(&esas2r_pci_driver);
}

/* Handle ioctl calls to "/proc/scsi/esas2r/ATTOnode" */
static const struct file_operations esas2r_proc_fops = {
	.compat_ioctl	= compat_ptr_ioctl,
	.unlocked_ioctl = esas2r_proc_ioctl,
};

static const struct proc_ops esas2r_proc_ops = {
	.proc_lseek		= default_llseek,
	.proc_ioctl		= esas2r_proc_ioctl,
#ifdef CONFIG_COMPAT
	.proc_compat_ioctl	= compat_ptr_ioctl,
#endif
};

static struct Scsi_Host *esas2r_proc_host;
static int esas2r_proc_major;

long esas2r_proc_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
{
	return esas2r_ioctl_handler(esas2r_proc_host->hostdata,
				    cmd, (void __user *)arg);
}

static void __exit esas2r_exit(void)
{
	esas2r_log(ESAS2R_LOG_INFO, "%s called", __func__);

	if (esas2r_proc_major > 0) {
		struct proc_dir_entry *proc_dir;

		esas2r_log(ESAS2R_LOG_INFO, "unregister proc");

		proc_dir = scsi_template_proc_dir(esas2r_proc_host->hostt);
		if (proc_dir)
			remove_proc_entry(ATTONODE_NAME, proc_dir);
		unregister_chrdev(esas2r_proc_major, ESAS2R_DRVR_NAME);

		esas2r_proc_major = 0;
	}

	esas2r_log(ESAS2R_LOG_INFO, "pci_unregister_driver() called");

	pci_unregister_driver(&esas2r_pci_driver);
}

int esas2r_show_info(struct seq_file *m, struct Scsi_Host *sh)
{
	struct esas2r_adapter *a = (struct esas2r_adapter *)sh->hostdata;

	struct esas2r_target *t;
	int dev_count = 0;

	esas2r_log(ESAS2R_LOG_DEBG, "esas2r_show_info (%p,%d)", m, sh->host_no);

	seq_printf(m, ESAS2R_LONGNAME "\n"
		   "Driver version: "ESAS2R_VERSION_STR "\n"
		   "Flash version: %s\n"
		   "Firmware version: %s\n"
		   "Copyright "ESAS2R_COPYRIGHT_YEARS "\n"
		   "http://www.attotech.com\n"
		   "\n",
		   a->flash_rev,
		   a->fw_rev[0] ? a->fw_rev : "(none)");


	seq_printf(m, "Adapter information:\n"
		   "--------------------\n"
		   "Model: %s\n"
		   "SAS address: %02X%02X%02X%02X:%02X%02X%02X%02X\n",
		   esas2r_get_model_name(a),
		   a->nvram->sas_addr[0],
		   a->nvram->sas_addr[1],
		   a->nvram->sas_addr[2],
		   a->nvram->sas_addr[3],
		   a->nvram->sas_addr[4],
		   a->nvram->sas_addr[5],
		   a->nvram->sas_addr[6],
		   a->nvram->sas_addr[7]);

	seq_puts(m, "\n"
		   "Discovered devices:\n"
		   "\n"
		   "   #  Target ID\n"
		   "---------------\n");

	for (t = a->targetdb; t < a->targetdb_end; t++)
		if (t->buffered_target_state == TS_PRESENT) {
			seq_printf(m, " %3d   %3d\n",
				   ++dev_count,
				   (u16)(uintptr_t)(t - a->targetdb));
		}

	if (dev_count == 0)
		seq_puts(m, "none\n");

	seq_putc(m, '\n');
	return 0;

}

const char *esas2r_info(struct Scsi_Host *sh)
{
	struct esas2r_adapter *a = (struct esas2r_adapter *)sh->hostdata;
	static char esas2r_info_str[512];

	esas2r_log_dev(ESAS2R_LOG_INFO, &(sh->shost_gendev),
		       "esas2r_info() called");

	/*
	 * if we haven't done so already, register as a char driver
	 * and stick a node under "/proc/scsi/esas2r/ATTOnode"
	 */

	if (esas2r_proc_major <= 0) {
		esas2r_proc_host = sh;

		esas2r_proc_major = register_chrdev(0, ESAS2R_DRVR_NAME,
						    &esas2r_proc_fops);

		esas2r_log_dev(ESAS2R_LOG_DEBG, &(sh->shost_gendev),
			       "register_chrdev (major %d)",
			       esas2r_proc_major);

		if (esas2r_proc_major > 0) {
			struct proc_dir_entry *proc_dir;
			struct proc_dir_entry *pde = NULL;

			proc_dir = scsi_template_proc_dir(sh->hostt);
			if (proc_dir)
				pde = proc_create(ATTONODE_NAME, 0, proc_dir,
						  &esas2r_proc_ops);

			if (!pde) {
				esas2r_log_dev(ESAS2R_LOG_WARN,
					       &(sh->shost_gendev),
					       "failed to create_proc_entry");
				esas2r_proc_major = -1;
			}
		}
	}

	sprintf(esas2r_info_str,
		ESAS2R_LONGNAME " (bus 0x%02X, device 0x%02X, IRQ 0x%02X)"
		" driver version: "ESAS2R_VERSION_STR "  firmware version: "
		"%s\n",
		a->pcid->bus->number, a->pcid->devfn, a->pcid->irq,
		a->fw_rev[0] ? a->fw_rev : "(none)");

	return esas2r_info_str;
}

/* Callback for building a request scatter/gather list */
static u32 get_physaddr_from_sgc(struct esas2r_sg_context *sgc, u64 *addr)
{
	u32 len;

	if (likely(sgc->cur_offset == sgc->exp_offset)) {
		/*
		 * the normal case: caller used all bytes from previous call, so
		 * expected offset is the same as the current offset.
		 */

		if (sgc->sgel_count < sgc->num_sgel) {
			/* retrieve next segment, except for first time */
			if (sgc->exp_offset > (u8 *)0) {
				/* advance current segment */
				sgc->cur_sgel = sg_next(sgc->cur_sgel);
				++(sgc->sgel_count);
			}


			len = sg_dma_len(sgc->cur_sgel);
			(*addr) = sg_dma_address(sgc->cur_sgel);

			/* save the total # bytes returned to caller so far */
			sgc->exp_offset += len;

		} else {
			len = 0;
		}
	} else if (sgc->cur_offset < sgc->exp_offset) {
		/*
		 * caller did not use all bytes from previous call. need to
		 * compute the address based on current segment.
		 */

		len = sg_dma_len(sgc->cur_sgel);
		(*addr) = sg_dma_address(sgc->cur_sgel);

		sgc->exp_offset -= len;

		/* calculate PA based on prev segment address and offsets */
		*addr = *addr +
			(sgc->cur_offset - sgc->exp_offset);

		sgc->exp_offset += len;

		/* re-calculate length based on offset */
		len = lower_32_bits(
			sgc->exp_offset - sgc->cur_offset);
	} else {   /* if ( sgc->cur_offset > sgc->exp_offset ) */
		   /*
		    * we don't expect the caller to skip ahead.
		    * cur_offset will never exceed the len we return
		    */
		len = 0;
	}

	return len;
}

int esas2r_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
{
	struct esas2r_adapter *a =
		(struct esas2r_adapter *)cmd->device->host->hostdata;
	struct esas2r_request *rq;
	struct esas2r_sg_context sgc;
	unsigned bufflen;

	/* Assume success, if it fails we will fix the result later. */
	cmd->result = DID_OK << 16;

	if (unlikely(test_bit(AF_DEGRADED_MODE, &a->flags))) {
		cmd->result = DID_NO_CONNECT << 16;
		scsi_done(cmd);
		return 0;
	}

	rq = esas2r_alloc_request(a);
	if (unlikely(rq == NULL)) {
		esas2r_debug("esas2r_alloc_request failed");
		return SCSI_MLQUEUE_HOST_BUSY;
	}

	rq->cmd = cmd;
	bufflen = scsi_bufflen(cmd);

	if (likely(bufflen != 0)) {
		if (cmd->sc_data_direction == DMA_TO_DEVICE)
			rq->vrq->scsi.flags |= cpu_to_le32(FCP_CMND_WRD);
		else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
			rq->vrq->scsi.flags |= cpu_to_le32(FCP_CMND_RDD);
	}

	memcpy(rq->vrq->scsi.cdb, cmd->cmnd, cmd->cmd_len);
	rq->vrq->scsi.length = cpu_to_le32(bufflen);
	rq->target_id = cmd->device->id;
	rq->vrq->scsi.flags |= cpu_to_le32(cmd->device->lun);
	rq->sense_buf = cmd->sense_buffer;
	rq->sense_len = SCSI_SENSE_BUFFERSIZE;

	esas2r_sgc_init(&sgc, a, rq, NULL);

	sgc.length = bufflen;
	sgc.cur_offset = NULL;

	sgc.cur_sgel = scsi_sglist(cmd);
	sgc.exp_offset = NULL;
	sgc.num_sgel = scsi_dma_map(cmd);
	sgc.sgel_count = 0;

	if (unlikely(sgc.num_sgel < 0)) {
		esas2r_free_request(a, rq);
		return SCSI_MLQUEUE_HOST_BUSY;
	}

	sgc.get_phys_addr = (PGETPHYSADDR)get_physaddr_from_sgc;

	if (unlikely(!esas2r_build_sg_list(a, rq, &sgc))) {
		scsi_dma_unmap(cmd);
		esas2r_free_request(a, rq);
		return SCSI_MLQUEUE_HOST_BUSY;
	}

	esas2r_debug("start request %p to %d:%d\n", rq, (int)cmd->device->id,
		     (int)cmd->device->lun);

	esas2r_start_request(a, rq);

	return 0;
}

static void complete_task_management_request(struct esas2r_adapter *a,
					     struct esas2r_request *rq)
{
	(*rq->task_management_status_ptr) = rq->req_stat;
	esas2r_free_request(a, rq);
}

/*
 * Searches the specified queue for the specified queue for the command
 * to abort.
 *
 * Return 0 on failure, 1 if command was not found, 2 if command was found
 */
static int esas2r_check_active_queue(struct esas2r_adapter *a,
				     struct esas2r_request **abort_request,
				     struct scsi_cmnd *cmd,
				     struct list_head *queue)
{
	bool found = false;
	struct esas2r_request *ar = *abort_request;
	struct esas2r_request *rq;
	struct list_head *element, *next;

	list_for_each_safe(element, next, queue) {

		rq = list_entry(element, struct esas2r_request, req_list);

		if (rq->cmd == cmd) {

			/* Found the request.  See what to do with it. */
			if (queue == &a->active_list) {
				/*
				 * We are searching the active queue, which
				 * means that we need to send an abort request
				 * to the firmware.
				 */
				ar = esas2r_alloc_request(a);
				if (ar == NULL) {
					esas2r_log_dev(ESAS2R_LOG_WARN,
						       &(a->host->shost_gendev),
						       "unable to allocate an abort request for cmd %p",
						       cmd);
					return 0; /* Failure */
				}

				/*
				 * Task management request must be formatted
				 * with a lock held.
				 */
				ar->sense_len = 0;
				ar->vrq->scsi.length = 0;
				ar->target_id = rq->target_id;
				ar->vrq->scsi.flags |= cpu_to_le32(
					(u8)le32_to_cpu(rq->vrq->scsi.flags));

				memset(ar->vrq->scsi.cdb, 0,
				       sizeof(ar->vrq->scsi.cdb));

				ar->vrq->scsi.flags |= cpu_to_le32(
					FCP_CMND_TRM);
				ar->vrq->scsi.u.abort_handle =
					rq->vrq->scsi.handle;
			} else {
				/*
				 * The request is pending but not active on
				 * the firmware.  Just free it now and we'll
				 * report the successful abort below.
				 */
				list_del_init(&rq->req_list);
				esas2r_free_request(a, rq);
			}

			found = true;
			break;
		}

	}

	if (!found)
		return 1;       /* Not found */

	return 2;               /* found */


}

int esas2r_eh_abort(struct scsi_cmnd *cmd)
{
	struct esas2r_adapter *a =
		(struct esas2r_adapter *)cmd->device->host->hostdata;
	struct esas2r_request *abort_request = NULL;
	unsigned long flags;
	struct list_head *queue;
	int result;

	esas2r_log(ESAS2R_LOG_INFO, "eh_abort (%p)", cmd);

	if (test_bit(AF_DEGRADED_MODE, &a->flags)) {
		cmd->result = DID_ABORT << 16;

		scsi_set_resid(cmd, 0);

		scsi_done(cmd);

		return SUCCESS;
	}

	spin_lock_irqsave(&a->queue_lock, flags);

	/*
	 * Run through the defer and active queues looking for the request
	 * to abort.
	 */

	queue = &a->defer_list;

check_active_queue:

	result = esas2r_check_active_queue(a, &abort_request, cmd, queue);

	if (!result) {
		spin_unlock_irqrestore(&a->queue_lock, flags);
		return FAILED;
	} else if (result == 2 && (queue == &a->defer_list)) {
		queue = &a->active_list;
		goto check_active_queue;
	}

	spin_unlock_irqrestore(&a->queue_lock, flags);

	if (abort_request) {
		u8 task_management_status = RS_PENDING;

		/*
		 * the request is already active, so we need to tell
		 * the firmware to abort it and wait for the response.
		 */

		abort_request->comp_cb = complete_task_management_request;
		abort_request->task_management_status_ptr =
			&task_management_status;

		esas2r_start_request(a, abort_request);

		if (atomic_read(&a->disable_cnt) == 0)
			esas2r_do_deferred_processes(a);

		while (task_management_status == RS_PENDING)
			msleep(10);

		/*
		 * Once we get here, the original request will have been
		 * completed by the firmware and the abort request will have
		 * been cleaned up.  we're done!
		 */

		return SUCCESS;
	}

	/*
	 * If we get here, either we found the inactive request and
	 * freed it, or we didn't find it at all.  Either way, success!
	 */

	cmd->result = DID_ABORT << 16;

	scsi_set_resid(cmd, 0);

	scsi_done(cmd);

	return SUCCESS;
}

static int esas2r_host_bus_reset(struct scsi_cmnd *cmd, bool host_reset)
{
	struct esas2r_adapter *a =
		(struct esas2r_adapter *)cmd->device->host->hostdata;

	if (test_bit(AF_DEGRADED_MODE, &a->flags))
		return FAILED;

	if (host_reset)
		esas2r_reset_adapter(a);
	else
		esas2r_reset_bus(a);

	/* above call sets the AF_OS_RESET flag.  wait for it to clear. */

	while (test_bit(AF_OS_RESET, &a->flags)) {
		msleep(10);

		if (test_bit(AF_DEGRADED_MODE, &a->flags))
			return FAILED;
	}

	if (test_bit(AF_DEGRADED_MODE, &a->flags))
		return FAILED;

	return SUCCESS;
}

int esas2r_host_reset(struct scsi_cmnd *cmd)
{
	esas2r_log(ESAS2R_LOG_INFO, "host_reset (%p)", cmd);

	return esas2r_host_bus_reset(cmd, true);
}

int esas2r_bus_reset(struct scsi_cmnd *cmd)
{
	esas2r_log(ESAS2R_LOG_INFO, "bus_reset (%p)", cmd);

	return esas2r_host_bus_reset(cmd, false);
}

static int esas2r_dev_targ_reset(struct scsi_cmnd *cmd, bool target_reset)
{
	struct esas2r_adapter *a =
		(struct esas2r_adapter *)cmd->device->host->hostdata;
	struct esas2r_request *rq;
	u8 task_management_status = RS_PENDING;
	bool completed;

	if (test_bit(AF_DEGRADED_MODE, &a->flags))
		return FAILED;

retry:
	rq = esas2r_alloc_request(a);
	if (rq == NULL) {
		if (target_reset) {
			esas2r_log(ESAS2R_LOG_CRIT,
				   "unable to allocate a request for a "
				   "target reset (%d)!",
				   cmd->device->id);
		} else {
			esas2r_log(ESAS2R_LOG_CRIT,
				   "unable to allocate a request for a "
				   "device reset (%d:%llu)!",
				   cmd->device->id,
				   cmd->device->lun);
		}


		return FAILED;
	}

	rq->target_id = cmd->device->id;
	rq->vrq->scsi.flags |= cpu_to_le32(cmd->device->lun);
	rq->req_stat = RS_PENDING;

	rq->comp_cb = complete_task_management_request;
	rq->task_management_status_ptr = &task_management_status;

	if (target_reset) {
		esas2r_debug("issuing target reset (%p) to id %d", rq,
			     cmd->device->id);
		completed = esas2r_send_task_mgmt(a, rq, 0x20);
	} else {
		esas2r_debug("issuing device reset (%p) to id %d lun %d", rq,
			     cmd->device->id, cmd->device->lun);
		completed = esas2r_send_task_mgmt(a, rq, 0x10);
	}

	if (completed) {
		/* Task management cmd completed right away, need to free it. */

		esas2r_free_request(a, rq);
	} else {
		/*
		 * Wait for firmware to complete the request.  Completion
		 * callback will free it.
		 */
		while (task_management_status == RS_PENDING)
			msleep(10);
	}

	if (test_bit(AF_DEGRADED_MODE, &a->flags))
		return FAILED;

	if (task_management_status == RS_BUSY) {
		/*
		 * Busy, probably because we are flashing.  Wait a bit and
		 * try again.
		 */
		msleep(100);
		goto retry;
	}

	return SUCCESS;
}

int esas2r_device_reset(struct scsi_cmnd *cmd)
{
	esas2r_log(ESAS2R_LOG_INFO, "device_reset (%p)", cmd);

	return esas2r_dev_targ_reset(cmd, false);

}

int esas2r_target_reset(struct scsi_cmnd *cmd)
{
	esas2r_log(ESAS2R_LOG_INFO, "target_reset (%p)", cmd);

	return esas2r_dev_targ_reset(cmd, true);
}

void esas2r_log_request_failure(struct esas2r_adapter *a,
				struct esas2r_request *rq)
{
	u8 reqstatus = rq->req_stat;

	if (reqstatus == RS_SUCCESS)
		return;

	if (rq->vrq->scsi.function == VDA_FUNC_SCSI) {
		if (reqstatus == RS_SCSI_ERROR) {
			if (rq->func_rsp.scsi_rsp.sense_len >= 13) {
				esas2r_log(ESAS2R_LOG_WARN,
					   "request failure - SCSI error %x ASC:%x ASCQ:%x CDB:%x",
					   rq->sense_buf[2], rq->sense_buf[12],
					   rq->sense_buf[13],
					   rq->vrq->scsi.cdb[0]);
			} else {
				esas2r_log(ESAS2R_LOG_WARN,
					   "request failure - SCSI error CDB:%x\n",
					   rq->vrq->scsi.cdb[0]);
			}
		} else if ((rq->vrq->scsi.cdb[0] != INQUIRY
			    && rq->vrq->scsi.cdb[0] != REPORT_LUNS)
			   || (reqstatus != RS_SEL
			       && reqstatus != RS_SEL2)) {
			if ((reqstatus == RS_UNDERRUN) &&
			    (rq->vrq->scsi.cdb[0] == INQUIRY)) {
				/* Don't log inquiry underruns */
			} else {
				esas2r_log(ESAS2R_LOG_WARN,
					   "request failure - cdb:%x reqstatus:%d target:%d",
					   rq->vrq->scsi.cdb[0], reqstatus,
					   rq->target_id);
			}
		}
	}
}

void esas2r_wait_request(struct esas2r_adapter *a, struct esas2r_request *rq)
{
	u32 starttime;
	u32 timeout;

	starttime = jiffies_to_msecs(jiffies);
	timeout = rq->timeout ? rq->timeout : 5000;

	while (true) {
		esas2r_polled_interrupt(a);

		if (rq->req_stat != RS_STARTED)
			break;

		schedule_timeout_interruptible(msecs_to_jiffies(100));

		if ((jiffies_to_msecs(jiffies) - starttime) > timeout) {
			esas2r_hdebug("request TMO");
			esas2r_bugon();

			rq->req_stat = RS_TIMEOUT;

			esas2r_local_reset_adapter(a);
			return;
		}
	}
}

u32 esas2r_map_data_window(struct esas2r_adapter *a, u32 addr_lo)
{
	u32 offset = addr_lo & (MW_DATA_WINDOW_SIZE - 1);
	u32 base = addr_lo & -(signed int)MW_DATA_WINDOW_SIZE;

	if (a->window_base != base) {
		esas2r_write_register_dword(a, MVR_PCI_WIN1_REMAP,
					    base | MVRPW1R_ENABLE);
		esas2r_flush_register_dword(a, MVR_PCI_WIN1_REMAP);
		a->window_base = base;
	}

	return offset;
}

/* Read a block of data from chip memory */
bool esas2r_read_mem_block(struct esas2r_adapter *a,
			   void *to,
			   u32 from,
			   u32 size)
{
	u8 *end = (u8 *)to;

	while (size) {
		u32 len;
		u32 offset;
		u32 iatvr;

		iatvr = (from & -(signed int)MW_DATA_WINDOW_SIZE);

		esas2r_map_data_window(a, iatvr);

		offset = from & (MW_DATA_WINDOW_SIZE - 1);
		len = size;

		if (len > MW_DATA_WINDOW_SIZE - offset)
			len = MW_DATA_WINDOW_SIZE - offset;

		from += len;
		size -= len;

		while (len--) {
			*end++ = esas2r_read_data_byte(a, offset);
			offset++;
		}
	}

	return true;
}

void esas2r_nuxi_mgt_data(u8 function, void *data)
{
	struct atto_vda_grp_info *g;
	struct atto_vda_devinfo *d;
	struct atto_vdapart_info *p;
	struct atto_vda_dh_info *h;
	struct atto_vda_metrics_info *m;
	struct atto_vda_schedule_info *s;
	struct atto_vda_buzzer_info *b;
	u8 i;

	switch (function) {
	case VDAMGT_BUZZER_INFO:
	case VDAMGT_BUZZER_SET:

		b = (struct atto_vda_buzzer_info *)data;

		b->duration = le32_to_cpu(b->duration);
		break;

	case VDAMGT_SCHEDULE_INFO:
	case VDAMGT_SCHEDULE_EVENT:

		s = (struct atto_vda_schedule_info *)data;

		s->id = le32_to_cpu(s->id);

		break;

	case VDAMGT_DEV_INFO:
	case VDAMGT_DEV_CLEAN:
	case VDAMGT_DEV_PT_INFO:
	case VDAMGT_DEV_FEATURES:
	case VDAMGT_DEV_PT_FEATURES:
	case VDAMGT_DEV_OPERATION:

		d = (struct atto_vda_devinfo *)data;

		d->capacity = le64_to_cpu(d->capacity);
		d->block_size = le32_to_cpu(d->block_size);
		d->ses_dev_index = le16_to_cpu(d->ses_dev_index);
		d->target_id = le16_to_cpu(d->target_id);
		d->lun = le16_to_cpu(d->lun);
		d->features = le16_to_cpu(d->features);
		break;

	case VDAMGT_GRP_INFO:
	case VDAMGT_GRP_CREATE:
	case VDAMGT_GRP_DELETE:
	case VDAMGT_ADD_STORAGE:
	case VDAMGT_MEMBER_ADD:
	case VDAMGT_GRP_COMMIT:
	case VDAMGT_GRP_REBUILD:
	case VDAMGT_GRP_COMMIT_INIT:
	case VDAMGT_QUICK_RAID:
	case VDAMGT_GRP_FEATURES:
	case VDAMGT_GRP_COMMIT_INIT_AUTOMAP:
	case VDAMGT_QUICK_RAID_INIT_AUTOMAP:
	case VDAMGT_SPARE_LIST:
	case VDAMGT_SPARE_ADD:
	case VDAMGT_SPARE_REMOVE:
	case VDAMGT_LOCAL_SPARE_ADD:
	case VDAMGT_GRP_OPERATION:

		g = (struct atto_vda_grp_info *)data;

		g->capacity = le64_to_cpu(g->capacity);
		g->block_size = le32_to_cpu(g->block_size);
		g->interleave = le32_to_cpu(g->interleave);
		g->features = le16_to_cpu(g->features);

		for (i = 0; i < 32; i++)
			g->members[i] = le16_to_cpu(g->members[i]);

		break;

	case VDAMGT_PART_INFO:
	case VDAMGT_PART_MAP:
	case VDAMGT_PART_UNMAP:
	case VDAMGT_PART_AUTOMAP:
	case VDAMGT_PART_SPLIT:
	case VDAMGT_PART_MERGE:

		p = (struct atto_vdapart_info *)data;

		p->part_size = le64_to_cpu(p->part_size);
		p->start_lba = le32_to_cpu(p->start_lba);
		p->block_size = le32_to_cpu(p->block_size);
		p->target_id = le16_to_cpu(p->target_id);
		break;

	case VDAMGT_DEV_HEALTH_REQ:

		h = (struct atto_vda_dh_info *)data;

		h->med_defect_cnt = le32_to_cpu(h->med_defect_cnt);
		h->info_exc_cnt = le32_to_cpu(h->info_exc_cnt);
		break;

	case VDAMGT_DEV_METRICS:

		m = (struct atto_vda_metrics_info *)data;

		for (i = 0; i < 32; i++)
			m->dev_indexes[i] = le16_to_cpu(m->dev_indexes[i]);

		break;

	default:
		break;
	}
}

void esas2r_nuxi_cfg_data(u8 function, void *data)
{
	struct atto_vda_cfg_init *ci;

	switch (function) {
	case VDA_CFG_INIT:
	case VDA_CFG_GET_INIT:
	case VDA_CFG_GET_INIT2:

		ci = (struct atto_vda_cfg_init *)data;

		ci->date_time.year = le16_to_cpu(ci->date_time.year);
		ci->sgl_page_size = le32_to_cpu(ci->sgl_page_size);
		ci->vda_version = le32_to_cpu(ci->vda_version);
		ci->epoch_time = le32_to_cpu(ci->epoch_time);
		ci->ioctl_tunnel = le32_to_cpu(ci->ioctl_tunnel);
		ci->num_targets_backend = le32_to_cpu(ci->num_targets_backend);
		break;

	default:
		break;
	}
}

void esas2r_nuxi_ae_data(union atto_vda_ae *ae)
{
	struct atto_vda_ae_raid *r = &ae->raid;
	struct atto_vda_ae_lu *l = &ae->lu;

	switch (ae->hdr.bytype) {
	case VDAAE_HDR_TYPE_RAID:

		r->dwflags = le32_to_cpu(r->dwflags);
		break;

	case VDAAE_HDR_TYPE_LU:

		l->dwevent = le32_to_cpu(l->dwevent);
		l->wphys_target_id = le16_to_cpu(l->wphys_target_id);
		l->id.tgtlun.wtarget_id = le16_to_cpu(l->id.tgtlun.wtarget_id);

		if (l->hdr.bylength >= offsetof(struct atto_vda_ae_lu, id)
		    + sizeof(struct atto_vda_ae_lu_tgt_lun_raid)) {
			l->id.tgtlun_raid.dwinterleave
				= le32_to_cpu(l->id.tgtlun_raid.dwinterleave);
			l->id.tgtlun_raid.dwblock_size
				= le32_to_cpu(l->id.tgtlun_raid.dwblock_size);
		}

		break;

	case VDAAE_HDR_TYPE_DISK:
	default:
		break;
	}
}

void esas2r_free_request(struct esas2r_adapter *a, struct esas2r_request *rq)
{
	unsigned long flags;

	esas2r_rq_destroy_request(rq, a);
	spin_lock_irqsave(&a->request_lock, flags);
	list_add(&rq->comp_list, &a->avail_request);
	spin_unlock_irqrestore(&a->request_lock, flags);
}

struct esas2r_request *esas2r_alloc_request(struct esas2r_adapter *a)
{
	struct esas2r_request *rq;
	unsigned long flags;

	spin_lock_irqsave(&a->request_lock, flags);

	if (unlikely(list_empty(&a->avail_request))) {
		spin_unlock_irqrestore(&a->request_lock, flags);
		return NULL;
	}

	rq = list_first_entry(&a->avail_request, struct esas2r_request,
			      comp_list);
	list_del(&rq->comp_list);
	spin_unlock_irqrestore(&a->request_lock, flags);
	esas2r_rq_init_request(rq, a);

	return rq;

}

void esas2r_complete_request_cb(struct esas2r_adapter *a,
				struct esas2r_request *rq)
{
	esas2r_debug("completing request %p\n", rq);

	scsi_dma_unmap(rq->cmd);

	if (unlikely(rq->req_stat != RS_SUCCESS)) {
		esas2r_debug("[%x STATUS %x:%x (%x)]", rq->target_id,
			     rq->req_stat,
			     rq->func_rsp.scsi_rsp.scsi_stat,
			     rq->cmd);

		rq->cmd->result =
			((esas2r_req_status_to_error(rq->req_stat) << 16)
			 | rq->func_rsp.scsi_rsp.scsi_stat);

		if (rq->req_stat == RS_UNDERRUN)
			scsi_set_resid(rq->cmd,
				       le32_to_cpu(rq->func_rsp.scsi_rsp.
						   residual_length));
		else
			scsi_set_resid(rq->cmd, 0);
	}

	scsi_done(rq->cmd);

	esas2r_free_request(a, rq);
}

/* Run tasklet to handle stuff outside of interrupt context. */
void esas2r_adapter_tasklet(unsigned long context)
{
	struct esas2r_adapter *a = (struct esas2r_adapter *)context;

	if (unlikely(test_bit(AF2_TIMER_TICK, &a->flags2))) {
		clear_bit(AF2_TIMER_TICK, &a->flags2);
		esas2r_timer_tick(a);
	}

	if (likely(test_bit(AF2_INT_PENDING, &a->flags2))) {
		clear_bit(AF2_INT_PENDING, &a->flags2);
		esas2r_adapter_interrupt(a);
	}

	if (esas2r_is_tasklet_pending(a))
		esas2r_do_tasklet_tasks(a);

	if (esas2r_is_tasklet_pending(a)
	    || (test_bit(AF2_INT_PENDING, &a->flags2))
	    || (test_bit(AF2_TIMER_TICK, &a->flags2))) {
		clear_bit(AF_TASKLET_SCHEDULED, &a->flags);
		esas2r_schedule_tasklet(a);
	} else {
		clear_bit(AF_TASKLET_SCHEDULED, &a->flags);
	}
}

static void esas2r_timer_callback(struct timer_list *t);

void esas2r_kickoff_timer(struct esas2r_adapter *a)
{
	timer_setup(&a->timer, esas2r_timer_callback, 0);

	a->timer.expires = jiffies +
			   msecs_to_jiffies(100);

	add_timer(&a->timer);
}

static void esas2r_timer_callback(struct timer_list *t)
{
	struct esas2r_adapter *a = from_timer(a, t, timer);

	set_bit(AF2_TIMER_TICK, &a->flags2);

	esas2r_schedule_tasklet(a);

	esas2r_kickoff_timer(a);
}

/*
 * Firmware events need to be handled outside of interrupt context
 * so we schedule a delayed_work to handle them.
 */

static void
esas2r_free_fw_event(struct esas2r_fw_event_work *fw_event)
{
	unsigned long flags;
	struct esas2r_adapter *a = fw_event->a;

	spin_lock_irqsave(&a->fw_event_lock, flags);
	list_del(&fw_event->list);
	kfree(fw_event);
	spin_unlock_irqrestore(&a->fw_event_lock, flags);
}

void
esas2r_fw_event_off(struct esas2r_adapter *a)
{
	unsigned long flags;

	spin_lock_irqsave(&a->fw_event_lock, flags);
	a->fw_events_off = 1;
	spin_unlock_irqrestore(&a->fw_event_lock, flags);
}

void
esas2r_fw_event_on(struct esas2r_adapter *a)
{
	unsigned long flags;

	spin_lock_irqsave(&a->fw_event_lock, flags);
	a->fw_events_off = 0;
	spin_unlock_irqrestore(&a->fw_event_lock, flags);
}

static void esas2r_add_device(struct esas2r_adapter *a, u16 target_id)
{
	int ret;
	struct scsi_device *scsi_dev;

	scsi_dev = scsi_device_lookup(a->host, 0, target_id, 0);

	if (scsi_dev) {
		esas2r_log_dev(
			ESAS2R_LOG_WARN,
			&(scsi_dev->
			  sdev_gendev),
			"scsi device already exists at id %d", target_id);

		scsi_device_put(scsi_dev);
	} else {
		esas2r_log_dev(
			ESAS2R_LOG_INFO,
			&(a->host->
			  shost_gendev),
			"scsi_add_device() called for 0:%d:0",
			target_id);

		ret = scsi_add_device(a->host, 0, target_id, 0);
		if (ret) {
			esas2r_log_dev(
				ESAS2R_LOG_CRIT,
				&(a->host->
				  shost_gendev),
				"scsi_add_device failed with %d for id %d",
				ret, target_id);
		}
	}
}

static void esas2r_remove_device(struct esas2r_adapter *a, u16 target_id)
{
	struct scsi_device *scsi_dev;

	scsi_dev = scsi_device_lookup(a->host, 0, target_id, 0);

	if (scsi_dev) {
		scsi_device_set_state(scsi_dev, SDEV_OFFLINE);

		esas2r_log_dev(
			ESAS2R_LOG_INFO,
			&(scsi_dev->
			  sdev_gendev),
			"scsi_remove_device() called for 0:%d:0",
			target_id);

		scsi_remove_device(scsi_dev);

		esas2r_log_dev(
			ESAS2R_LOG_INFO,
			&(scsi_dev->
			  sdev_gendev),
			"scsi_device_put() called");

		scsi_device_put(scsi_dev);
	} else {
		esas2r_log_dev(
			ESAS2R_LOG_WARN,
			&(a->host->shost_gendev),
			"no target found at id %d",
			target_id);
	}
}

/*
 * Sends a firmware asynchronous event to anyone who happens to be
 * listening on the defined ATTO VDA event ports.
 */
static void esas2r_send_ae_event(struct esas2r_fw_event_work *fw_event)
{
	struct esas2r_vda_ae *ae = (struct esas2r_vda_ae *)fw_event->data;
	char *type;

	switch (ae->vda_ae.hdr.bytype) {
	case VDAAE_HDR_TYPE_RAID:
		type = "RAID group state change";
		break;

	case VDAAE_HDR_TYPE_LU:
		type = "Mapped destination LU change";
		break;

	case VDAAE_HDR_TYPE_DISK:
		type = "Physical disk inventory change";
		break;

	case VDAAE_HDR_TYPE_RESET:
		type = "Firmware reset";
		break;

	case VDAAE_HDR_TYPE_LOG_INFO:
		type = "Event Log message (INFO level)";
		break;

	case VDAAE_HDR_TYPE_LOG_WARN:
		type = "Event Log message (WARN level)";
		break;

	case VDAAE_HDR_TYPE_LOG_CRIT:
		type = "Event Log message (CRIT level)";
		break;

	case VDAAE_HDR_TYPE_LOG_FAIL:
		type = "Event Log message (FAIL level)";
		break;

	case VDAAE_HDR_TYPE_NVC:
		type = "NVCache change";
		break;

	case VDAAE_HDR_TYPE_TLG_INFO:
		type = "Time stamped log message (INFO level)";
		break;

	case VDAAE_HDR_TYPE_TLG_WARN:
		type = "Time stamped log message (WARN level)";
		break;

	case VDAAE_HDR_TYPE_TLG_CRIT:
		type = "Time stamped log message (CRIT level)";
		break;

	case VDAAE_HDR_TYPE_PWRMGT:
		type = "Power management";
		break;

	case VDAAE_HDR_TYPE_MUTE:
		type = "Mute button pressed";
		break;

	case VDAAE_HDR_TYPE_DEV:
		type = "Device attribute change";
		break;

	default:
		type = "Unknown";
		break;
	}

	esas2r_log(ESAS2R_LOG_WARN,
		   "An async event of type \"%s\" was received from the firmware.  The event contents are:",
		   type);
	esas2r_log_hexdump(ESAS2R_LOG_WARN, &ae->vda_ae,
			   ae->vda_ae.hdr.bylength);

}

static void
esas2r_firmware_event_work(struct work_struct *work)
{
	struct esas2r_fw_event_work *fw_event =
		container_of(work, struct esas2r_fw_event_work, work.work);

	struct esas2r_adapter *a = fw_event->a;

	u16 target_id = *(u16 *)&fw_event->data[0];

	if (a->fw_events_off)
		goto done;

	switch (fw_event->type) {
	case fw_event_null:
		break; /* do nothing */

	case fw_event_lun_change:
		esas2r_remove_device(a, target_id);
		esas2r_add_device(a, target_id);
		break;

	case fw_event_present:
		esas2r_add_device(a, target_id);
		break;

	case fw_event_not_present:
		esas2r_remove_device(a, target_id);
		break;

	case fw_event_vda_ae:
		esas2r_send_ae_event(fw_event);
		break;
	}

done:
	esas2r_free_fw_event(fw_event);
}

void esas2r_queue_fw_event(struct esas2r_adapter *a,
			   enum fw_event_type type,
			   void *data,
			   int data_sz)
{
	struct esas2r_fw_event_work *fw_event;
	unsigned long flags;

	fw_event = kzalloc(sizeof(struct esas2r_fw_event_work), GFP_ATOMIC);
	if (!fw_event) {
		esas2r_log(ESAS2R_LOG_WARN,
			   "esas2r_queue_fw_event failed to alloc");
		return;
	}

	if (type == fw_event_vda_ae) {
		struct esas2r_vda_ae *ae =
			(struct esas2r_vda_ae *)fw_event->data;

		ae->signature = ESAS2R_VDA_EVENT_SIG;
		ae->bus_number = a->pcid->bus->number;
		ae->devfn = a->pcid->devfn;
		memcpy(&ae->vda_ae, data, sizeof(ae->vda_ae));
	} else {
		memcpy(fw_event->data, data, data_sz);
	}

	fw_event->type = type;
	fw_event->a = a;

	spin_lock_irqsave(&a->fw_event_lock, flags);
	list_add_tail(&fw_event->list, &a->fw_event_list);
	INIT_DELAYED_WORK(&fw_event->work, esas2r_firmware_event_work);
	queue_delayed_work_on(
		smp_processor_id(), a->fw_event_q, &fw_event->work,
		msecs_to_jiffies(1));
	spin_unlock_irqrestore(&a->fw_event_lock, flags);
}

void esas2r_target_state_changed(struct esas2r_adapter *a, u16 targ_id,
				 u8 state)
{
	if (state == TS_LUN_CHANGE)
		esas2r_queue_fw_event(a, fw_event_lun_change, &targ_id,
				      sizeof(targ_id));
	else if (state == TS_PRESENT)
		esas2r_queue_fw_event(a, fw_event_present, &targ_id,
				      sizeof(targ_id));
	else if (state == TS_NOT_PRESENT)
		esas2r_queue_fw_event(a, fw_event_not_present, &targ_id,
				      sizeof(targ_id));
}

/* Translate status to a Linux SCSI mid-layer error code */
int esas2r_req_status_to_error(u8 req_stat)
{
	switch (req_stat) {
	case RS_OVERRUN:
	case RS_UNDERRUN:
	case RS_SUCCESS:
	/*
	 * NOTE: SCSI mid-layer wants a good status for a SCSI error, because
	 *       it will check the scsi_stat value in the completion anyway.
	 */
	case RS_SCSI_ERROR:
		return DID_OK;

	case RS_SEL:
	case RS_SEL2:
		return DID_NO_CONNECT;

	case RS_RESET:
		return DID_RESET;

	case RS_ABORTED:
		return DID_ABORT;

	case RS_BUSY:
		return DID_BUS_BUSY;
	}

	/* everything else is just an error. */

	return DID_ERROR;
}

module_init(esas2r_init);
module_exit(esas2r_exit);