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
|
/*
* HighPoint RR3xxx RAID Driver for FreeBSD
* Copyright (C) 2005-2007 HighPoint Technologies, Inc. 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.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, 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 DAMAGE.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/types.h>
#include <sys/cons.h>
#if (__FreeBSD_version >= 500000)
#include <sys/time.h>
#include <sys/systm.h>
#else
#include <machine/clock.h>
#endif
#include <sys/stat.h>
#include <sys/malloc.h>
#include <sys/conf.h>
#include <sys/libkern.h>
#include <sys/kernel.h>
#if (__FreeBSD_version >= 500000)
#include <sys/kthread.h>
#include <sys/mutex.h>
#include <sys/module.h>
#endif
#include <sys/eventhandler.h>
#include <sys/bus.h>
#include <sys/taskqueue.h>
#include <sys/ioccom.h>
#include <machine/resource.h>
#include <machine/bus.h>
#include <machine/stdarg.h>
#include <sys/rman.h>
#include <vm/vm.h>
#include <vm/pmap.h>
#if (__FreeBSD_version >= 500000)
#include <dev/pci/pcireg.h>
#include <dev/pci/pcivar.h>
#else
#include <pci/pcivar.h>
#include <pci/pcireg.h>
#endif
#if (__FreeBSD_version <= 500043)
#include <sys/devicestat.h>
#endif
#include <cam/cam.h>
#include <cam/cam_ccb.h>
#include <cam/cam_sim.h>
#include <cam/cam_xpt_sim.h>
#include <cam/cam_debug.h>
#include <cam/cam_xpt_periph.h>
#include <cam/cam_periph.h>
#include <cam/scsi/scsi_all.h>
#include <cam/scsi/scsi_message.h>
#if (__FreeBSD_version < 500043)
#include <sys/bus_private.h>
#endif
#include <dev/hptiop/hptiop.h>
static struct hpt_iop_hba * g_hba[4];
static int iop_count = 0;
static char driver_name[] = "hptiop";
static char driver_version[] = "v1.2 (041307)";
static int osm_max_targets = 32;
static devclass_t hptiop_devclass;
static void os_request_callback(struct hpt_iop_hba * hba, u_int32_t req);
static void os_message_callback(struct hpt_iop_hba * hba, u_int32_t msg);
static int hptiop_do_ioctl(struct hpt_iop_hba * hba, struct hpt_iop_ioctl_param * pParams);
static void hptiop_bus_scan_cb(struct cam_periph *periph, union ccb *ccb);
static int hptiop_rescan_bus(struct hpt_iop_hba * hba);
static int hptiop_post_ioctl_command(struct hpt_iop_hba * hba,
struct hpt_iop_request_ioctl_command * req, struct hpt_iop_ioctl_param * pParams);
static int os_query_remove_device(struct hpt_iop_hba * hba, int target_id);
static int hptiop_probe(device_t dev);
static int hptiop_attach(device_t dev);
static int hptiop_detach(device_t dev);
static int hptiop_shutdown(device_t dev);
static void hptiop_action(struct cam_sim *sim, union ccb *ccb);
static void hptiop_poll(struct cam_sim *sim);
static void hptiop_async(void * callback_arg, u_int32_t code,
struct cam_path * path, void * arg);
static void hptiop_pci_intr(void *arg);
static void hptiop_release_resource(struct hpt_iop_hba * hba);
static int hptiop_reset_adapter(struct hpt_iop_hba * hba);
static void hptiop_enable_interrupts(struct hpt_iop_hba * hba);
static void hptiop_disable_interrupts(struct hpt_iop_hba * hba);
static d_open_t hptiop_open;
static d_close_t hptiop_close;
static d_ioctl_t hptiop_ioctl;
static struct cdevsw hptiop_cdevsw = {
.d_open = hptiop_open,
.d_close = hptiop_close,
.d_ioctl = hptiop_ioctl,
.d_name = driver_name,
#if __FreeBSD_version>=503000
.d_version = D_VERSION,
#endif
#if (__FreeBSD_version>=503000 && __FreeBSD_version<600034)
.d_flags = D_NEEDGIANT,
#endif
#if __FreeBSD_version<600034
#if __FreeBSD_version>=501000
.d_maj = MAJOR_AUTO,
#else
.d_maj = HPT_DEV_MAJOR,
#endif
#endif
};
#if __FreeBSD_version < 503000
#define hba_from_dev(dev) ((struct hpt_iop_hba *)(dev)->si_drv1)
#else
#define hba_from_dev(dev) \
((struct hpt_iop_hba *)devclass_get_softc(hptiop_devclass, minor(dev)))
#endif
static int hptiop_open(ioctl_dev_t dev, int flags,
int devtype, ioctl_thread_t proc)
{
struct hpt_iop_hba * hba = hba_from_dev(dev);
if (hba==NULL)
return ENXIO;
if (hba->flag & HPT_IOCTL_FLAG_OPEN)
return EBUSY;
hba->flag |= HPT_IOCTL_FLAG_OPEN;
return 0;
}
static int hptiop_close(ioctl_dev_t dev, int flags,
int devtype, ioctl_thread_t proc)
{
struct hpt_iop_hba * hba = hba_from_dev(dev);
hba->flag &= ~(u_int32_t)HPT_IOCTL_FLAG_OPEN;
return 0;
}
static int hptiop_ioctl(ioctl_dev_t dev, u_long cmd, caddr_t data,
int flags, ioctl_thread_t proc)
{
int ret = EFAULT;
struct hpt_iop_hba * hba = hba_from_dev(dev);
#if (__FreeBSD_version >= 500000)
mtx_lock(&Giant);
#endif
switch (cmd) {
case HPT_DO_IOCONTROL:
ret = hptiop_do_ioctl(hba, (struct hpt_iop_ioctl_param *)data);
break;
case HPT_SCAN_BUS:
ret = hptiop_rescan_bus(hba);
break;
}
#if (__FreeBSD_version >= 500000)
mtx_unlock(&Giant);
#endif
return ret;
}
static __inline void * iop_get_inbound_request(struct hpt_iopmu * iop)
{
u_int32_t m = readl(&iop->inbound_queue);
return (m == 0xFFFFFFFF)? 0 : ((char *)iop + m);
}
static __inline void iop_post_inbound_request(struct hpt_iopmu * iop, void *req)
{
writel(&iop->inbound_queue, (char *)req - (char *)iop);
}
static __inline void iop_post_outbound_request(struct hpt_iopmu * iop, void *req)
{
writel(&iop->outbound_queue, (char *)req - (char *)iop);
}
static __inline void hptiop_pci_posting_flush(struct hpt_iopmu * iop)
{
readl(&iop->outbound_intstatus);
}
static int iop_wait_ready(struct hpt_iopmu * iop, u_int32_t millisec)
{
u_int32_t req=0;
int i;
for (i = 0; i < millisec; i++) {
req = readl(&iop->inbound_queue);
if (req != IOPMU_QUEUE_EMPTY)
break;
DELAY(1000);
}
if (req!=IOPMU_QUEUE_EMPTY) {
writel(&iop->outbound_queue, req);
hptiop_pci_posting_flush(iop);
return 0;
}
return -1;
}
static int iop_intr(struct hpt_iop_hba * hba)
{
struct hpt_iopmu * iop = hba->iop;
u_int32_t status;
int ret = 0;
status = readl(&iop->outbound_intstatus);
if (status & IOPMU_OUTBOUND_INT_MSG0) {
u_int32_t msg = readl(&iop->outbound_msgaddr0);
KdPrint(("received outbound msg %x", msg));
writel(&iop->outbound_intstatus, IOPMU_OUTBOUND_INT_MSG0);
os_message_callback(hba, msg);
ret = 1;
}
if (status & IOPMU_OUTBOUND_INT_POSTQUEUE) {
u_int32_t req;
while ((req = readl(&iop->outbound_queue))
!=IOPMU_QUEUE_EMPTY) {
if (req & IOPMU_QUEUE_MASK_HOST_BITS)
os_request_callback(hba, req);
else {
struct hpt_iop_request_header * p;
p = (struct hpt_iop_request_header *)((char *)hba->iop + req);
if (p->flags & IOP_REQUEST_FLAG_SYNC_REQUEST) {
if (p->context)
os_request_callback(hba, req);
else
p->context= 1;
}
else
os_request_callback(hba, req);
}
}
ret = 1;
}
return ret;
}
static int iop_send_sync_request(struct hpt_iop_hba * hba, void *req, u_int32_t millisec)
{
u_int32_t i;
((struct hpt_iop_request_header *)req)->flags |= IOP_REQUEST_FLAG_SYNC_REQUEST;
((struct hpt_iop_request_header *)req)->context = 0;
writel(&hba->iop->inbound_queue,
(u_int32_t)((char *)req - (char *)hba->iop));
hptiop_pci_posting_flush(hba->iop);
for (i = 0; i < millisec; i++) {
iop_intr(hba);
if (((struct hpt_iop_request_header *)req)->context)
return 0;
DELAY(1000);
}
return -1;
}
static int iop_send_sync_msg(struct hpt_iop_hba * hba, u_int32_t msg, int *done, u_int32_t millisec)
{
u_int32_t i;
*done = 0;
writel(&hba->iop->inbound_msgaddr0, msg);
hptiop_pci_posting_flush(hba->iop);
for (i = 0; i < millisec; i++) {
iop_intr(hba);
if (*done)
break;
DELAY(1000);
}
return *done? 0 : -1;
}
static int iop_get_config(struct hpt_iop_hba * hba, struct hpt_iop_request_get_config * config)
{
u_int32_t req32=0;
struct hpt_iop_request_get_config * req;
if ((req32 = readl(&hba->iop->inbound_queue)) == IOPMU_QUEUE_EMPTY)
return -1;
req = (struct hpt_iop_request_get_config *)((char *)hba->iop + req32);
req->header.flags = 0;
req->header.type = IOP_REQUEST_TYPE_GET_CONFIG;
req->header.size = sizeof(struct hpt_iop_request_get_config);
req->header.result = IOP_RESULT_PENDING;
if (iop_send_sync_request(hba, req, 20000)) {
KdPrint(("Get config send cmd failed"));
return -1;
}
*config = *req;
writel(&hba->iop->outbound_queue, req32);
return 0;
}
static int iop_set_config(struct hpt_iop_hba * hba, struct hpt_iop_request_set_config *config)
{
u_int32_t req32;
struct hpt_iop_request_set_config *req;
req32 = readl(&hba->iop->inbound_queue);
if (req32 == IOPMU_QUEUE_EMPTY)
return -1;
req = (struct hpt_iop_request_set_config *)((char *)hba->iop + req32);
memcpy((u_int8_t *)req + sizeof(struct hpt_iop_request_header),
(u_int8_t *)config + sizeof(struct hpt_iop_request_header),
sizeof(struct hpt_iop_request_set_config) - sizeof(struct hpt_iop_request_header));
req->header.flags = 0;
req->header.type = IOP_REQUEST_TYPE_SET_CONFIG;
req->header.size = sizeof(struct hpt_iop_request_set_config);
req->header.result = IOP_RESULT_PENDING;
if (iop_send_sync_request(hba, req, 20000)) {
KdPrint(("Set config send cmd failed"));
return -1;
}
writel(&hba->iop->outbound_queue, req32);
return 0;
}
static int hptiop_do_ioctl(struct hpt_iop_hba * hba, struct hpt_iop_ioctl_param * pParams)
{
struct hpt_iop_request_ioctl_command * req;
if ((pParams->Magic != HPT_IOCTL_MAGIC) &&
(pParams->Magic != HPT_IOCTL_MAGIC32))
return EFAULT;
req = (struct hpt_iop_request_ioctl_command *)iop_get_inbound_request(hba->iop);
if (!req) {
printf("hptiop: ioctl command failed");
return EFAULT;
}
if (pParams->nInBufferSize)
if (copyin((void *)pParams->lpInBuffer,
req->buf, pParams->nInBufferSize))
goto invalid;
if (hptiop_post_ioctl_command(hba, req, pParams))
goto invalid;
if (req->header.result == IOP_RESULT_SUCCESS) {
if (pParams->nOutBufferSize)
if (copyout(req->buf +
((pParams->nInBufferSize + 3) & ~3),
(void*)pParams->lpOutBuffer,
pParams->nOutBufferSize))
goto invalid;
if (pParams->lpBytesReturned)
if (copyout(&req->bytes_returned,
(void*)pParams->lpBytesReturned,
sizeof(u_int32_t)))
goto invalid;
iop_post_outbound_request(hba->iop, req);
return 0;
} else{
invalid:
iop_post_outbound_request(hba->iop, req);
return EFAULT;
}
}
static int hptiop_post_ioctl_command(struct hpt_iop_hba *hba,
struct hpt_iop_request_ioctl_command *req, struct hpt_iop_ioctl_param *pParams)
{
if (((pParams->nInBufferSize + 3) & ~3) + pParams->nOutBufferSize >
hba->max_request_size - sizeof(struct hpt_iop_request_header) -
4 * sizeof(u_int32_t)) {
printf("hptiop: request size beyond max value");
return -1;
}
req->ioctl_code = HPT_CTL_CODE_BSD_TO_IOP(pParams->dwIoControlCode);
req->inbuf_size = pParams->nInBufferSize;
req->outbuf_size = pParams->nOutBufferSize;
req->header.size = sizeof(struct hpt_iop_request_ioctl_command)
- 4 + pParams->nInBufferSize;
req->header.context = (u_int64_t)(unsigned long)req;
req->header.type = IOP_REQUEST_TYPE_IOCTL_COMMAND;
req->header.result = IOP_RESULT_PENDING;
req->header.flags |= IOP_REQUEST_FLAG_SYNC_REQUEST;
hptiop_lock_adapter(hba);
iop_post_inbound_request(hba->iop, req);
hptiop_pci_posting_flush(hba->iop);
while (req->header.context) {
if (hptiop_sleep(hba, req,
PPAUSE, "hptctl", HPT_OSM_TIMEOUT)==0)
break;
iop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_RESET,
&hba->msg_done, 60000);
}
hptiop_unlock_adapter(hba);
return 0;
}
static int hptiop_rescan_bus(struct hpt_iop_hba * hba)
{
struct cam_path *path;
union ccb *ccb;
if (xpt_create_path(&path, xpt_periph, cam_sim_path(hba->sim),
CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP)
return(EIO);
if ((ccb = malloc(sizeof(union ccb), M_TEMP, M_WAITOK)) == NULL)
return(ENOMEM);
bzero(ccb, sizeof(union ccb));
xpt_setup_ccb(&ccb->ccb_h, path, 5);
ccb->ccb_h.func_code = XPT_SCAN_BUS;
ccb->ccb_h.cbfcnp = hptiop_bus_scan_cb;
ccb->crcn.flags = CAM_FLAG_NONE;
xpt_action(ccb);
return(0);
}
static void hptiop_bus_scan_cb(struct cam_periph *periph, union ccb *ccb)
{
xpt_free_path(ccb->ccb_h.path);
free(ccb, M_TEMP);
return;
}
static bus_dmamap_callback_t hptiop_map_srb;
static bus_dmamap_callback_t hptiop_post_scsi_command;
/*
* CAM driver interface
*/
static device_method_t driver_methods[] = {
/* Device interface */
DEVMETHOD(device_probe, hptiop_probe),
DEVMETHOD(device_attach, hptiop_attach),
DEVMETHOD(device_detach, hptiop_detach),
DEVMETHOD(device_shutdown, hptiop_shutdown),
{ 0, 0 }
};
static driver_t hptiop_pci_driver = {
driver_name,
driver_methods,
sizeof(struct hpt_iop_hba)
};
DRIVER_MODULE(hptiop, pci, hptiop_pci_driver, hptiop_devclass, 0, 0);
static int hptiop_probe(device_t dev)
{
struct hpt_iop_hba *hba;
if ((pci_get_vendor(dev) == 0x1103 && pci_get_device(dev) == 0x3220) ||
(pci_get_vendor(dev) == 0x1103 && pci_get_device(dev) == 0x3320) ||
(pci_get_vendor(dev) == 0x1103 && pci_get_device(dev) == 0x3520)) {
printf("hptiop: adapter at PCI %d:%d:%d, IRQ %d",
pci_get_bus(dev), pci_get_slot(dev),
pci_get_function(dev), pci_get_irq(dev));
device_set_desc(dev, driver_name);
hba = (struct hpt_iop_hba *)device_get_softc(dev);
memset(hba, 0, sizeof(struct hpt_iop_hba));
return 0;
}
return ENXIO;
}
static int hptiop_attach(device_t dev)
{
struct hpt_iop_hba * hba = (struct hpt_iop_hba *)device_get_softc(dev);
struct hpt_iop_request_get_config iop_config;
struct hpt_iop_request_set_config set_config;
int rid = 0;
struct cam_devq *devq;
struct ccb_setasync ccb;
u_int32_t unit = device_get_unit(dev);
printf("%s%d: RocketRAID 3xxx controller driver %s\n",
driver_name, unit, driver_version);
KdPrint(("hptiop_attach(%d, %d/%d/%d)", unit,
pci_get_bus(dev), pci_get_slot(dev), pci_get_function(dev)));
#if __FreeBSD_version >=440000
pci_enable_busmaster(dev);
#endif
hba->pcidev = dev;
hba->pciunit = unit;
hba->bar0_rid = 0x10;
hba->bar0_res = bus_alloc_resource(hba->pcidev,
SYS_RES_MEMORY, &hba->bar0_rid,
0, ~0, 0x100000, RF_ACTIVE);
if (hba->bar0_res == NULL) {
printf("hptiop: Failed to get iop base adrress.\n");
return ENXIO;
}
hba->iop = (struct hpt_iopmu *)rman_get_virtual(hba->bar0_res);
if (!hba->iop) {
printf("hptiop: alloc mem res failed\n");
return ENXIO;
}
if (iop_wait_ready(hba->iop, 2000)) {
printf("hptiop: adapter is not ready\n");
return ENXIO;
}
if (iop_get_config(hba, &iop_config)) {
printf("hptiop: Get iop config failed.\n");
return ENXIO;
}
hba->firmware_version = iop_config.firmware_version;
hba->interface_version = iop_config.interface_version;
hba->max_requests = iop_config.max_requests;
hba->max_devices = iop_config.max_devices;
hba->max_request_size = iop_config.request_size;
hba->max_sg_count = iop_config.max_sg_count;
#if (__FreeBSD_version >= 500000)
mtx_init(&hba->lock, "hptioplock", NULL, MTX_DEF);
#endif
if (bus_dma_tag_create(NULL,/* parent */
1, /* alignment */
0, /* boundary */
BUS_SPACE_MAXADDR, /* lowaddr */
BUS_SPACE_MAXADDR, /* highaddr */
NULL, NULL, /* filter, filterarg */
BUS_SPACE_MAXSIZE_32BIT, /* maxsize */
BUS_SPACE_UNRESTRICTED, /* nsegments */
BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
0, /* flags */
#if __FreeBSD_version>502000
NULL, /* lockfunc */
NULL, /* lockfuncarg */
#endif
&hba->parent_dmat /* tag */))
{
printf("hptiop: alloc parent_dmat failed\n");
return ENXIO;
}
if (bus_dma_tag_create(hba->parent_dmat,/* parent */
4, /* alignment */
BUS_SPACE_MAXADDR_32BIT+1, /* boundary */
BUS_SPACE_MAXADDR, /* lowaddr */
BUS_SPACE_MAXADDR, /* highaddr */
NULL, NULL, /* filter, filterarg */
PAGE_SIZE * (hba->max_sg_count-1), /* maxsize */
hba->max_sg_count, /* nsegments */
0x20000, /* maxsegsize */
BUS_DMA_ALLOCNOW, /* flags */
#if __FreeBSD_version>502000
busdma_lock_mutex, /* lockfunc */
&hba->lock, /* lockfuncarg */
#endif
&hba->io_dmat /* tag */))
{
printf("hptiop: alloc io_dmat failed\n");
bus_dma_tag_destroy(hba->parent_dmat);
return ENXIO;
}
if (bus_dma_tag_create(hba->parent_dmat,/* parent */
1, /* alignment */
0, /* boundary */
BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
BUS_SPACE_MAXADDR, /* highaddr */
NULL, NULL, /* filter, filterarg */
HPT_SRB_MAX_SIZE * HPT_SRB_MAX_QUEUE_SIZE + 0x20,
1, /* nsegments */
BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
0, /* flags */
#if __FreeBSD_version>502000
NULL, /* lockfunc */
NULL, /* lockfuncarg */
#endif
&hba->srb_dmat /* tag */))
{
printf("hptiop: alloc srb_dmat failed\n");
bus_dma_tag_destroy(hba->io_dmat);
bus_dma_tag_destroy(hba->parent_dmat);
return ENXIO;
}
if (bus_dmamem_alloc(hba->srb_dmat, (void **)&hba->uncached_ptr,
#if __FreeBSD_version>501000
BUS_DMA_WAITOK | BUS_DMA_COHERENT,
#else
BUS_DMA_WAITOK,
#endif
&hba->srb_dmamap) != 0)
{
printf("hptiop: bus_dmamem_alloc failed!\n");
release_tag:
bus_dma_tag_destroy(hba->srb_dmat);
bus_dma_tag_destroy(hba->io_dmat);
bus_dma_tag_destroy(hba->parent_dmat);
return ENXIO;
}
if (bus_dmamap_load(hba->srb_dmat,
hba->srb_dmamap, hba->uncached_ptr,
(HPT_SRB_MAX_SIZE * HPT_SRB_MAX_QUEUE_SIZE) + 0x20,
hptiop_map_srb, hba, 0))
{
printf("hptiop: bus_dmamap_load failed!\n");
goto release_tag;
}
if ((devq = cam_simq_alloc(hba->max_requests - 1 )) == NULL) {
printf("hptiop: cam_simq_alloc failed\n");
attach_failed:
hptiop_release_resource(hba);
return ENXIO;
}
hba->sim = cam_sim_alloc(hptiop_action, hptiop_poll, driver_name,
hba, unit, &Giant, hba->max_requests - 1, 1, devq);
if (!hba->sim) {
printf("hptiop: cam_sim_alloc failed\n");
cam_simq_free(devq);
goto attach_failed;
}
if (xpt_bus_register(hba->sim, 0) != CAM_SUCCESS) {
printf("hptiop: xpt_bus_register failed\n");
cam_sim_free(hba->sim, /*free devq*/ TRUE);
hba->sim = NULL;
goto attach_failed;
}
if (xpt_create_path(&hba->path, /*periph */ NULL,
cam_sim_path(hba->sim), CAM_TARGET_WILDCARD,
CAM_LUN_WILDCARD) != CAM_REQ_CMP)
{
printf("hptiop: xpt_create_path failed\n");
xpt_bus_deregister(cam_sim_path(hba->sim));
cam_sim_free(hba->sim, /*free_devq*/TRUE);
hba->sim = NULL;
goto attach_failed;
}
bzero(&set_config, sizeof(set_config));
set_config.iop_id = iop_count;
set_config.vbus_id = cam_sim_path(hba->sim);
set_config.max_host_request_size = HPT_SRB_MAX_REQ_SIZE;
if (iop_set_config(hba, &set_config)) {
printf("hptiop: Set iop config failed.\n");
goto attach_failed;
}
xpt_setup_ccb(&ccb.ccb_h, hba->path, /*priority*/5);
ccb.ccb_h.func_code = XPT_SASYNC_CB;
ccb.event_enable = (AC_FOUND_DEVICE | AC_LOST_DEVICE);
ccb.callback = hptiop_async;
ccb.callback_arg = hba->sim;
xpt_action((union ccb *)&ccb);
rid = 0;
if ((hba->irq_res = bus_alloc_resource(hba->pcidev, SYS_RES_IRQ,
&rid, 0, ~0ul, 1, RF_SHAREABLE | RF_ACTIVE)) == NULL) {
printf("hptiop: allocate irq failed!\n");
goto attach_failed;
}
if (bus_setup_intr(hba->pcidev, hba->irq_res, INTR_TYPE_CAM,
NULL, hptiop_pci_intr, hba, &hba->irq_handle)) {
printf("hptiop: allocate intr function failed!\n");
goto attach_failed;
}
hptiop_enable_interrupts(hba);
if (iop_send_sync_msg(hba,
IOPMU_INBOUND_MSG0_START_BACKGROUND_TASK,
&hba->msg_done, 5000)) {
printf("hptiop: Fail to start background task\n");
goto attach_failed;
}
hba->ioctl_dev = make_dev(&hptiop_cdevsw, unit,
UID_ROOT, GID_WHEEL /*GID_OPERATOR*/,
S_IRUSR | S_IWUSR, "%s%d", driver_name, unit);
#if __FreeBSD_version < 503000
hba->ioctl_dev->si_drv1 = hba;
#endif
hptiop_rescan_bus(hba);
g_hba[iop_count++] = hba;
return 0;
}
static int hptiop_detach(device_t dev)
{
struct hpt_iop_hba * hba = (struct hpt_iop_hba *)device_get_softc(dev);
int i;
int error = EBUSY;
hptiop_lock_adapter(hba);
for (i = 0; i < osm_max_targets; i++)
if (os_query_remove_device(hba, i)) {
printf("hptiop%d file system is busy. id=%d",
hba->pciunit, i);
goto out;
}
if ((error = hptiop_shutdown(dev)) != 0)
goto out;
if (iop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_STOP_BACKGROUND_TASK,
&hba->msg_done, 60000))
goto out;
hptiop_release_resource(hba);
error = 0;
out:
hptiop_unlock_adapter(hba);
return error;
}
static int hptiop_shutdown(device_t dev)
{
struct hpt_iop_hba * hba = (struct hpt_iop_hba *)device_get_softc(dev);
int error = 0;
if (hba->flag & HPT_IOCTL_FLAG_OPEN) {
printf("hptiop: %d server is busy", hba->pciunit);
return EBUSY;
}
hptiop_disable_interrupts(hba);
if (iop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_SHUTDOWN,
&hba->msg_done, 60000))
error = EBUSY;
return error;
}
static void hptiop_pci_intr(void *arg)
{
struct hpt_iop_hba * hba = (struct hpt_iop_hba *)arg;
hptiop_lock_adapter(hba);
iop_intr(hba);
hptiop_unlock_adapter(hba);
}
static void hptiop_poll(struct cam_sim *sim)
{
hptiop_pci_intr(cam_sim_softc(sim));
}
static void hptiop_async(void * callback_arg, u_int32_t code,
struct cam_path * path, void * arg)
{
}
static void hptiop_enable_interrupts(struct hpt_iop_hba * hba)
{
writel(&hba->iop->outbound_intmask,
~(IOPMU_OUTBOUND_INT_POSTQUEUE | IOPMU_OUTBOUND_INT_MSG0));
}
static void hptiop_disable_interrupts(struct hpt_iop_hba * hba)
{
u_int32_t int_mask;
int_mask = readl(&hba->iop->outbound_intmask);
writel(&hba->iop->outbound_intmask, int_mask |
IOPMU_OUTBOUND_INT_POSTQUEUE | IOPMU_OUTBOUND_INT_MSG0);
hptiop_pci_posting_flush(hba->iop);
}
static int hptiop_reset_adapter(struct hpt_iop_hba * hba)
{
return iop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_RESET,
&hba->msg_done, 60000);
}
static void *hptiop_get_srb(struct hpt_iop_hba * hba)
{
struct hpt_iop_srb * srb;
if (hba->srb_list) {
srb = hba->srb_list;
hba->srb_list = srb->next;
}
else
srb=NULL;
return srb;
}
static void hptiop_free_srb(struct hpt_iop_hba * hba, struct hpt_iop_srb * srb)
{
srb->next = hba->srb_list;
hba->srb_list = srb;
}
static void hptiop_action(struct cam_sim *sim, union ccb *ccb)
{
struct hpt_iop_hba * hba = (struct hpt_iop_hba *)cam_sim_softc(sim);
struct hpt_iop_srb * srb;
switch (ccb->ccb_h.func_code) {
case XPT_SCSI_IO:
hptiop_lock_adapter(hba);
if (ccb->ccb_h.target_lun != 0 ||
ccb->ccb_h.target_id >= osm_max_targets ||
(ccb->ccb_h.flags & CAM_CDB_PHYS))
{
ccb->ccb_h.status = CAM_TID_INVALID;
xpt_done(ccb);
goto scsi_done;
}
if ((srb = hptiop_get_srb(hba)) == NULL) {
printf("hptiop: srb allocated failed");
ccb->ccb_h.status = CAM_REQ_CMP_ERR;
xpt_done(ccb);
goto scsi_done;
}
srb->ccb = ccb;
if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
hptiop_post_scsi_command(srb, NULL, 0, 0);
else if ((ccb->ccb_h.flags & CAM_SCATTER_VALID) == 0) {
if ((ccb->ccb_h.flags & CAM_DATA_PHYS) == 0) {
int error;
error = bus_dmamap_load(hba->io_dmat,
srb->dma_map,
ccb->csio.data_ptr, ccb->csio.dxfer_len,
hptiop_post_scsi_command, srb, 0);
if (error && error != EINPROGRESS) {
printf("hptiop: %d bus_dmamap_load error %d",
hba->pciunit, error);
xpt_freeze_simq(hba->sim, 1);
ccb->ccb_h.status = CAM_REQ_CMP_ERR;
invalid:
hptiop_free_srb(hba, srb);
xpt_done(ccb);
goto scsi_done;
}
}
else {
printf("hptiop: CAM_DATA_PHYS not supported");
ccb->ccb_h.status = CAM_REQ_CMP_ERR;
goto invalid;
}
}
else {
struct bus_dma_segment *segs;
if ((ccb->ccb_h.flags & CAM_SG_LIST_PHYS) == 0 ||
(ccb->ccb_h.flags & CAM_DATA_PHYS) != 0) {
printf("hptiop: SCSI cmd failed");
ccb->ccb_h.status=CAM_PROVIDE_FAIL;
goto invalid;
}
segs = (struct bus_dma_segment *)ccb->csio.data_ptr;
hptiop_post_scsi_command(srb, segs,
ccb->csio.sglist_cnt, 0);
}
scsi_done:
hptiop_unlock_adapter(hba);
return;
case XPT_RESET_BUS:
printf("hptiop: reset adapter");
hptiop_lock_adapter(hba);
hba->msg_done = 0;
hptiop_reset_adapter(hba);
hptiop_unlock_adapter(hba);
break;
case XPT_GET_TRAN_SETTINGS:
case XPT_SET_TRAN_SETTINGS:
ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
break;
case XPT_CALC_GEOMETRY:
ccb->ccg.heads = 255;
ccb->ccg.secs_per_track = 63;
ccb->ccg.cylinders = ccb->ccg.volume_size /
(ccb->ccg.heads * ccb->ccg.secs_per_track);
ccb->ccb_h.status = CAM_REQ_CMP;
break;
case XPT_PATH_INQ:
{
struct ccb_pathinq *cpi = &ccb->cpi;
cpi->version_num = 1;
cpi->hba_inquiry = PI_SDTR_ABLE;
cpi->target_sprt = 0;
cpi->hba_misc = PIM_NOBUSRESET;
cpi->hba_eng_cnt = 0;
cpi->max_target = osm_max_targets;
cpi->max_lun = 0;
cpi->unit_number = cam_sim_unit(sim);
cpi->bus_id = cam_sim_bus(sim);
cpi->initiator_id = osm_max_targets;
cpi->base_transfer_speed = 3300;
strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
strncpy(cpi->hba_vid, "HPT ", HBA_IDLEN);
strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
cpi->ccb_h.status = CAM_REQ_CMP;
break;
}
default:
ccb->ccb_h.status = CAM_REQ_INVALID;
break;
}
xpt_done(ccb);
return;
}
static void hptiop_post_scsi_command(void *arg, bus_dma_segment_t *segs,
int nsegs, int error)
{
int idx;
struct hpt_iop_srb *srb = (struct hpt_iop_srb *)arg;
union ccb *ccb = srb->ccb;
u_int8_t *cdb;
struct hpt_iop_hba * hba = srb->hba;
struct hpt_iop_request_scsi_command * req;
if (error) {
scsi_error:
printf("hptiop: post scsi command: dma error, err = %d, nsegs = %d",
error, nsegs);
ccb->ccb_h.status = CAM_BUSY;
bus_dmamap_unload(hba->io_dmat, srb->dma_map);
hptiop_free_srb(hba, srb);
xpt_done(ccb);
return;
}
if (nsegs > hba->max_sg_count) {
printf("hptiop: nsegs is too large: nsegs=%d, Allowed count=%d",
nsegs, hba->max_sg_count);
goto scsi_error;
}
if (!srb) {
printf("hptiop: invalid srb");
goto scsi_error;
}
if (srb->srb_flag & HPT_SRB_FLAG_HIGH_MEM_ACESS) {
u_int32_t m = readl(&hba->iop->inbound_queue);
if (m == 0xFFFFFFFF) {
printf("hptiop: invaild req offset: %d", m);
goto scsi_error;
}
req = (struct hpt_iop_request_scsi_command *)((char *)hba->iop + m);
}
else
req = (struct hpt_iop_request_scsi_command *)srb;
if (ccb->csio.dxfer_len && nsegs > 0) {
struct hpt_iopsg *psg = req->sg_list;
for (idx = 0; idx < nsegs; idx++, psg++) {
psg->pci_address = (u_int64_t)segs[idx].ds_addr;
psg->size = segs[idx].ds_len;
psg->eot = 0;
}
psg[-1].eot = 1;
}
if (ccb->ccb_h.flags & CAM_CDB_POINTER)
cdb = ccb->csio.cdb_io.cdb_ptr;
else
cdb = ccb->csio.cdb_io.cdb_bytes;
bcopy(cdb, req->cdb, ccb->csio.cdb_len);
req->header.type = IOP_REQUEST_TYPE_SCSI_COMMAND;
req->header.result = IOP_RESULT_PENDING;
req->dataxfer_length = ccb->csio.dxfer_len;
req->channel = 0;
req->target = ccb->ccb_h.target_id;
req->lun = ccb->ccb_h.target_lun;
req->header.size = sizeof(struct hpt_iop_request_scsi_command)
- sizeof(struct hpt_iopsg) + nsegs*sizeof(struct hpt_iopsg);
if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
bus_dmamap_sync(hba->io_dmat,
srb->dma_map, BUS_DMASYNC_PREREAD);
}
else if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
bus_dmamap_sync(hba->io_dmat,
srb->dma_map, BUS_DMASYNC_PREWRITE);
if (srb->srb_flag & HPT_SRB_FLAG_HIGH_MEM_ACESS) {
req->header.context = (u_int64_t)(unsigned long)srb;
req->header.flags = 0;
writel(&hba->iop->inbound_queue,
((char *)req - (char *)hba->iop));
}
else {
req->header.context = (u_int64_t)srb->index |
IOPMU_QUEUE_ADDR_HOST_BIT;
req->header.flags = IOP_REQUEST_FLAG_OUTPUT_CONTEXT;
if (hba->firmware_version > 0x01020000 || hba->interface_version > 0x01020000) {
u_int32_t size_bits;
if (req->header.size < 256)
size_bits = IOPMU_QUEUE_REQUEST_SIZE_BIT;
else if (req->header.size < 512)
size_bits = IOPMU_QUEUE_ADDR_HOST_BIT;
else
size_bits = IOPMU_QUEUE_REQUEST_SIZE_BIT | IOPMU_QUEUE_ADDR_HOST_BIT;
writel(&hba->iop->inbound_queue, srb->phy_addr | size_bits);
} else
writel(&hba->iop->inbound_queue,
srb->phy_addr | IOPMU_QUEUE_ADDR_HOST_BIT);
}
}
static void os_request_callback(struct hpt_iop_hba * hba, u_int32_t index)
{
struct hpt_iop_srb * srb;
struct hpt_iop_request_scsi_command * req;
union ccb *ccb;
u_int8_t *cdb;
if (index & IOPMU_QUEUE_MASK_HOST_BITS) {
if (hba->firmware_version > 0x01020000 || hba->interface_version > 0x01020000) {
srb = hba->srb[index & ~(u_int32_t)
(IOPMU_QUEUE_ADDR_HOST_BIT | IOPMU_QUEUE_REQUEST_RESULT_BIT)];
req = (struct hpt_iop_request_scsi_command *)srb;
if (index & IOPMU_QUEUE_REQUEST_RESULT_BIT)
req->header.result = IOP_RESULT_SUCCESS;
} else {
srb = hba->srb[index & ~(u_int32_t)IOPMU_QUEUE_ADDR_HOST_BIT];
req = (struct hpt_iop_request_scsi_command *)srb;
}
goto srb_complete;
}
req = (struct hpt_iop_request_scsi_command *)((char *)hba->iop + index);
switch(((struct hpt_iop_request_header *)req)->type) {
case IOP_REQUEST_TYPE_IOCTL_COMMAND:
{
struct hpt_iop_request_ioctl_command * p;
p = (struct hpt_iop_request_ioctl_command *)(unsigned long)
(((struct hpt_iop_request_header *)req)->context);
((struct hpt_iop_request_header *)req)->context = 0;
wakeup(req);
break;
}
case IOP_REQUEST_TYPE_SCSI_COMMAND:
srb = (struct hpt_iop_srb *)(unsigned long)req->header.context;
srb_complete:
ccb = (union ccb *)srb->ccb;
if (ccb->ccb_h.flags & CAM_CDB_POINTER)
cdb = ccb->csio.cdb_io.cdb_ptr;
else
cdb = ccb->csio.cdb_io.cdb_bytes;
if (cdb[0] == SYNCHRONIZE_CACHE) { /* ??? */
ccb->ccb_h.status = CAM_REQ_CMP;
goto scsi_done;
}
switch (((struct hpt_iop_request_header *)req)->result) {
case IOP_RESULT_SUCCESS:
switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
case CAM_DIR_IN:
bus_dmamap_sync(hba->io_dmat,
srb->dma_map, BUS_DMASYNC_POSTREAD);
bus_dmamap_unload(hba->io_dmat, srb->dma_map);
break;
case CAM_DIR_OUT:
bus_dmamap_sync(hba->io_dmat,
srb->dma_map, BUS_DMASYNC_POSTWRITE);
bus_dmamap_unload(hba->io_dmat, srb->dma_map);
break;
}
ccb->ccb_h.status = CAM_REQ_CMP;
break;
case IOP_RESULT_BAD_TARGET:
ccb->ccb_h.status = CAM_DEV_NOT_THERE;
break;
case IOP_RESULT_BUSY:
ccb->ccb_h.status = CAM_BUSY;
break;
case IOP_RESULT_INVALID_REQUEST:
ccb->ccb_h.status = CAM_REQ_INVALID;
break;
case IOP_RESULT_FAIL:
ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
break;
case IOP_RESULT_RESET:
ccb->ccb_h.status = CAM_BUSY;
break;
default:
ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
break;
}
scsi_done:
if (srb->srb_flag & HPT_SRB_FLAG_HIGH_MEM_ACESS)
iop_post_outbound_request(hba->iop, req);
hptiop_free_srb(hba, srb);
xpt_done(ccb);
break;
}
}
static void hptiop_map_srb(void *arg, bus_dma_segment_t *segs,
int nsegs, int error)
{
struct hpt_iop_hba * hba = (struct hpt_iop_hba *)arg;
bus_addr_t phy_addr = (segs->ds_addr + 0x1F) & ~(bus_addr_t)0x1F;
struct hpt_iop_srb *srb, *tmp_srb;
int i;
if (error || nsegs == 0) {
printf("hptiop_map_srb error");
return;
}
/* map srb */
srb = (struct hpt_iop_srb *)
(((unsigned long)hba->uncached_ptr + 0x1F) & ~(unsigned long)0x1F);
for (i = 0; i < HPT_SRB_MAX_QUEUE_SIZE; i++) {
tmp_srb = (struct hpt_iop_srb *)
((char *)srb + i * HPT_SRB_MAX_SIZE);
if (((unsigned long)tmp_srb & 0x1F) == 0) {
if (bus_dmamap_create(hba->io_dmat,
0, &tmp_srb->dma_map)) {
printf("hptiop: dmamap create failed");
return;
}
bzero(tmp_srb, sizeof(struct hpt_iop_srb));
tmp_srb->hba = hba;
tmp_srb->index = i;
if (phy_addr & IOPMU_MAX_MEM_SUPPORT_MASK_32G)
tmp_srb->srb_flag = HPT_SRB_FLAG_HIGH_MEM_ACESS;
tmp_srb->phy_addr = (u_int32_t)(phy_addr >> 5);
hptiop_free_srb(hba, tmp_srb);
hba->srb[i] = tmp_srb;
phy_addr += HPT_SRB_MAX_SIZE;
}
else {
printf("hptiop: invalid alignment");
return;
}
}
}
static void os_message_callback(struct hpt_iop_hba * hba, u_int32_t msg)
{
hba->msg_done = 1;
}
static int os_query_remove_device(struct hpt_iop_hba * hba, int target_id)
{
struct cam_periph *periph = NULL;
struct cam_path *path;
int status, retval = 0;
status = xpt_create_path(&path, NULL, hba->sim->path_id, target_id, 0);
if (status == CAM_REQ_CMP) {
if ((periph = cam_periph_find(path, "da")) != NULL) {
if (periph->refcount >= 1) {
printf("hptiop: %d ,target_id=0x%x, refcount=%d",
hba->pciunit, target_id, periph->refcount);
retval = -1;
}
}
xpt_free_path(path);
}
return retval;
}
static void hptiop_release_resource(struct hpt_iop_hba *hba)
{
struct ccb_setasync ccb;
if (hba->path) {
xpt_setup_ccb(&ccb.ccb_h, hba->path, /*priority*/5);
ccb.ccb_h.func_code = XPT_SASYNC_CB;
ccb.event_enable = 0;
ccb.callback = hptiop_async;
ccb.callback_arg = hba->sim;
xpt_action((union ccb *)&ccb);
xpt_free_path(hba->path);
}
if (hba->sim) {
xpt_bus_deregister(cam_sim_path(hba->sim));
cam_sim_free(hba->sim, TRUE);
}
if (hba->srb_dmat) {
bus_dmamem_free(hba->srb_dmat,
hba->uncached_ptr, hba->srb_dmamap);
bus_dmamap_unload(hba->srb_dmat, hba->srb_dmamap);
bus_dma_tag_destroy(hba->srb_dmat);
}
if (hba->io_dmat)
bus_dma_tag_destroy(hba->io_dmat);
if (hba->parent_dmat)
bus_dma_tag_destroy(hba->parent_dmat);
if (hba->irq_handle)
bus_teardown_intr(hba->pcidev, hba->irq_res, hba->irq_handle);
if (hba->irq_res)
bus_release_resource(hba->pcidev, SYS_RES_IRQ, 0, hba->irq_res);
if (hba->bar0_res)
bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
hba->bar0_rid, hba->bar0_res);
if (hba->ioctl_dev)
destroy_dev(hba->ioctl_dev);
}
|