xref: /dragonfly/sys/dev/raid/hptiop/hptiop.c (revision d321a619)
1 /*
2  * HighPoint RR3xxx/4xxx RAID Driver for FreeBSD
3  * Copyright (C) 2007-2008 HighPoint Technologies, Inc. All Rights Reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/dev/hptiop/hptiop.c,v 1.11 2011/11/23 21:43:51 marius Exp $
27  */
28 
29 #include <sys/param.h>
30 #include <sys/types.h>
31 #include <sys/cons.h>
32 #include <sys/time.h>
33 #include <sys/systm.h>
34 
35 #include <sys/stat.h>
36 #include <sys/malloc.h>
37 #include <sys/conf.h>
38 #include <sys/libkern.h>
39 #include <sys/kernel.h>
40 
41 #include <sys/kthread.h>
42 #include <sys/lock.h>
43 #include <sys/module.h>
44 
45 #include <sys/eventhandler.h>
46 #include <sys/bus.h>
47 #include <sys/taskqueue.h>
48 #include <sys/ioccom.h>
49 #include <sys/device.h>
50 #include <sys/mplock2.h>
51 
52 #include <machine/stdarg.h>
53 #include <sys/rman.h>
54 
55 #include <vm/vm.h>
56 #include <vm/pmap.h>
57 
58 #include <bus/pci/pcireg.h>
59 #include <bus/pci/pcivar.h>
60 
61 #include <bus/cam/cam.h>
62 #include <bus/cam/cam_ccb.h>
63 #include <bus/cam/cam_sim.h>
64 #include <bus/cam/cam_xpt_periph.h>
65 #include <bus/cam/cam_xpt_sim.h>
66 #include <bus/cam/cam_debug.h>
67 #include <bus/cam/cam_periph.h>
68 #include <bus/cam/scsi/scsi_all.h>
69 #include <bus/cam/scsi/scsi_message.h>
70 
71 #include <dev/raid/hptiop/hptiop.h>
72 
73 static char driver_name[] = "hptiop";
74 static char driver_version[] = "v1.3 (010208)";
75 
76 static devclass_t hptiop_devclass;
77 
78 static int hptiop_send_sync_msg(struct hpt_iop_hba *hba,
79 				u_int32_t msg, u_int32_t millisec);
80 static void hptiop_request_callback_itl(struct hpt_iop_hba *hba,
81 							u_int32_t req);
82 static void hptiop_request_callback_mv(struct hpt_iop_hba *hba, u_int64_t req);
83 static void hptiop_os_message_callback(struct hpt_iop_hba *hba, u_int32_t msg);
84 static int  hptiop_do_ioctl_itl(struct hpt_iop_hba *hba,
85 				struct hpt_iop_ioctl_param *pParams);
86 static int  hptiop_do_ioctl_mv(struct hpt_iop_hba *hba,
87 				struct hpt_iop_ioctl_param *pParams);
88 static void hptiop_bus_scan_cb(struct cam_periph *periph, union ccb *ccb);
89 static int  hptiop_rescan_bus(struct hpt_iop_hba *hba);
90 static int hptiop_alloc_pci_res_itl(struct hpt_iop_hba *hba);
91 static int hptiop_alloc_pci_res_mv(struct hpt_iop_hba *hba);
92 static int hptiop_get_config_itl(struct hpt_iop_hba *hba,
93 				struct hpt_iop_request_get_config *config);
94 static int hptiop_get_config_mv(struct hpt_iop_hba *hba,
95 				struct hpt_iop_request_get_config *config);
96 static int hptiop_set_config_itl(struct hpt_iop_hba *hba,
97 				struct hpt_iop_request_set_config *config);
98 static int hptiop_set_config_mv(struct hpt_iop_hba *hba,
99 				struct hpt_iop_request_set_config *config);
100 static int hptiop_internal_memalloc_mv(struct hpt_iop_hba *hba);
101 static int hptiop_internal_memfree_mv(struct hpt_iop_hba *hba);
102 static int  hptiop_post_ioctl_command_itl(struct hpt_iop_hba *hba,
103 			u_int32_t req32, struct hpt_iop_ioctl_param *pParams);
104 static int  hptiop_post_ioctl_command_mv(struct hpt_iop_hba *hba,
105 				struct hpt_iop_request_ioctl_command *req,
106 				struct hpt_iop_ioctl_param *pParams);
107 static void hptiop_post_req_itl(struct hpt_iop_hba *hba,
108 				struct hpt_iop_srb *srb,
109 				bus_dma_segment_t *segs, int nsegs);
110 static void hptiop_post_req_mv(struct hpt_iop_hba *hba,
111 				struct hpt_iop_srb *srb,
112 				bus_dma_segment_t *segs, int nsegs);
113 static void hptiop_post_msg_itl(struct hpt_iop_hba *hba, u_int32_t msg);
114 static void hptiop_post_msg_mv(struct hpt_iop_hba *hba, u_int32_t msg);
115 static void hptiop_enable_intr_itl(struct hpt_iop_hba *hba);
116 static void hptiop_enable_intr_mv(struct hpt_iop_hba *hba);
117 static void hptiop_disable_intr_itl(struct hpt_iop_hba *hba);
118 static void hptiop_disable_intr_mv(struct hpt_iop_hba *hba);
119 static void hptiop_free_srb(struct hpt_iop_hba *hba, struct hpt_iop_srb *srb);
120 static int  hptiop_os_query_remove_device(struct hpt_iop_hba *hba, int tid);
121 static int  hptiop_probe(device_t dev);
122 static int  hptiop_attach(device_t dev);
123 static int  hptiop_detach(device_t dev);
124 static int  hptiop_shutdown(device_t dev);
125 static void hptiop_action(struct cam_sim *sim, union ccb *ccb);
126 static void hptiop_poll(struct cam_sim *sim);
127 static void hptiop_async(void *callback_arg, u_int32_t code,
128 					struct cam_path *path, void *arg);
129 static void hptiop_pci_intr(void *arg);
130 static void hptiop_release_resource(struct hpt_iop_hba *hba);
131 static int  hptiop_reset_adapter(struct hpt_iop_hba *hba);
132 
133 static d_open_t hptiop_open;
134 static d_close_t hptiop_close;
135 static d_ioctl_t hptiop_ioctl;
136 
137 static struct dev_ops hptiop_ops = {
138 	{ driver_name, 0, 0 },
139 	.d_open = hptiop_open,
140 	.d_close = hptiop_close,
141 	.d_ioctl = hptiop_ioctl,
142 };
143 
144 #define hba_from_dev(dev) ((struct hpt_iop_hba *)(dev)->si_drv1)
145 
146 #define BUS_SPACE_WRT4_ITL(offset, value) bus_space_write_4(hba->bar0t,\
147 		hba->bar0h, offsetof(struct hpt_iopmu_itl, offset), (value))
148 #define BUS_SPACE_RD4_ITL(offset) bus_space_read_4(hba->bar0t,\
149 		hba->bar0h, offsetof(struct hpt_iopmu_itl, offset))
150 
151 #define BUS_SPACE_WRT4_MV0(offset, value) bus_space_write_4(hba->bar0t,\
152 		hba->bar0h, offsetof(struct hpt_iopmv_regs, offset), value)
153 #define BUS_SPACE_RD4_MV0(offset) bus_space_read_4(hba->bar0t,\
154 		hba->bar0h, offsetof(struct hpt_iopmv_regs, offset))
155 #define BUS_SPACE_WRT4_MV2(offset, value) bus_space_write_4(hba->bar2t,\
156 		hba->bar2h, offsetof(struct hpt_iopmu_mv, offset), value)
157 #define BUS_SPACE_RD4_MV2(offset) bus_space_read_4(hba->bar2t,\
158 		hba->bar2h, offsetof(struct hpt_iopmu_mv, offset))
159 
160 static int hptiop_open(struct dev_open_args *ap)
161 {
162 	cdev_t dev = ap->a_head.a_dev;
163 	struct hpt_iop_hba *hba = hba_from_dev(dev);
164 
165 	if (hba==NULL)
166 		return ENXIO;
167 	if (hba->flag & HPT_IOCTL_FLAG_OPEN)
168 		return EBUSY;
169 	hba->flag |= HPT_IOCTL_FLAG_OPEN;
170 	return 0;
171 }
172 
173 static int hptiop_close(struct dev_close_args *ap)
174 {
175 	cdev_t dev = ap->a_head.a_dev;
176 	struct hpt_iop_hba *hba = hba_from_dev(dev);
177 	hba->flag &= ~(u_int32_t)HPT_IOCTL_FLAG_OPEN;
178 	return 0;
179 }
180 
181 static int hptiop_ioctl(struct dev_ioctl_args *ap)
182 {
183 	cdev_t dev = ap->a_head.a_dev;
184 	u_long cmd = ap->a_cmd;
185 	caddr_t data = ap->a_data;
186 	int ret = EFAULT;
187 	struct hpt_iop_hba *hba = hba_from_dev(dev);
188 
189 	get_mplock();
190 
191 	switch (cmd) {
192 	case HPT_DO_IOCONTROL:
193 		ret = hba->ops->do_ioctl(hba,
194 				(struct hpt_iop_ioctl_param *)data);
195 		break;
196 	case HPT_SCAN_BUS:
197 		ret = hptiop_rescan_bus(hba);
198 		break;
199 	}
200 
201 	rel_mplock();
202 
203 	return ret;
204 }
205 
206 static u_int64_t hptiop_mv_outbound_read(struct hpt_iop_hba *hba)
207 {
208 	u_int64_t p;
209 	u_int32_t outbound_tail = BUS_SPACE_RD4_MV2(outbound_tail);
210 	u_int32_t outbound_head = BUS_SPACE_RD4_MV2(outbound_head);
211 
212 	if (outbound_tail != outbound_head) {
213 		bus_space_read_region_4(hba->bar2t, hba->bar2h,
214 			offsetof(struct hpt_iopmu_mv,
215 				outbound_q[outbound_tail]),
216 			(u_int32_t *)&p, 2);
217 
218 		outbound_tail++;
219 
220 		if (outbound_tail == MVIOP_QUEUE_LEN)
221 			outbound_tail = 0;
222 
223 		BUS_SPACE_WRT4_MV2(outbound_tail, outbound_tail);
224 		return p;
225 	} else
226 		return 0;
227 }
228 
229 static void hptiop_mv_inbound_write(u_int64_t p, struct hpt_iop_hba *hba)
230 {
231 	u_int32_t inbound_head = BUS_SPACE_RD4_MV2(inbound_head);
232 	u_int32_t head = inbound_head + 1;
233 
234 	if (head == MVIOP_QUEUE_LEN)
235 		head = 0;
236 
237 	bus_space_write_region_4(hba->bar2t, hba->bar2h,
238 			offsetof(struct hpt_iopmu_mv, inbound_q[inbound_head]),
239 			(u_int32_t *)&p, 2);
240 	BUS_SPACE_WRT4_MV2(inbound_head, head);
241 	BUS_SPACE_WRT4_MV0(inbound_doorbell, MVIOP_MU_INBOUND_INT_POSTQUEUE);
242 }
243 
244 static void hptiop_post_msg_itl(struct hpt_iop_hba *hba, u_int32_t msg)
245 {
246 	BUS_SPACE_WRT4_ITL(inbound_msgaddr0, msg);
247 	BUS_SPACE_RD4_ITL(outbound_intstatus);
248 }
249 
250 static void hptiop_post_msg_mv(struct hpt_iop_hba *hba, u_int32_t msg)
251 {
252 
253 	BUS_SPACE_WRT4_MV2(inbound_msg, msg);
254 	BUS_SPACE_WRT4_MV0(inbound_doorbell, MVIOP_MU_INBOUND_INT_MSG);
255 
256 	BUS_SPACE_RD4_MV0(outbound_intmask);
257 }
258 
259 static int hptiop_wait_ready_itl(struct hpt_iop_hba * hba, u_int32_t millisec)
260 {
261 	u_int32_t req=0;
262 	int i;
263 
264 	for (i = 0; i < millisec; i++) {
265 		req = BUS_SPACE_RD4_ITL(inbound_queue);
266 		if (req != IOPMU_QUEUE_EMPTY)
267 			break;
268 		DELAY(1000);
269 	}
270 
271 	if (req!=IOPMU_QUEUE_EMPTY) {
272 		BUS_SPACE_WRT4_ITL(outbound_queue, req);
273 		BUS_SPACE_RD4_ITL(outbound_intstatus);
274 		return 0;
275 	}
276 
277 	return -1;
278 }
279 
280 static int hptiop_wait_ready_mv(struct hpt_iop_hba * hba, u_int32_t millisec)
281 {
282 	if (hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_NOP, millisec))
283 		return -1;
284 
285 	return 0;
286 }
287 
288 static void hptiop_request_callback_itl(struct hpt_iop_hba * hba,
289 							u_int32_t index)
290 {
291 	struct hpt_iop_srb *srb;
292 	struct hpt_iop_request_scsi_command *req=NULL;
293 	union ccb *ccb;
294 	u_int8_t *cdb;
295 	u_int32_t result, temp, dxfer;
296 	u_int64_t temp64;
297 
298 	if (index & IOPMU_QUEUE_MASK_HOST_BITS) { /*host req*/
299 		if (hba->firmware_version > 0x01020000 ||
300 			hba->interface_version > 0x01020000) {
301 			srb = hba->srb[index & ~(u_int32_t)
302 				(IOPMU_QUEUE_ADDR_HOST_BIT
303 				| IOPMU_QUEUE_REQUEST_RESULT_BIT)];
304 			req = (struct hpt_iop_request_scsi_command *)srb;
305 			if (index & IOPMU_QUEUE_REQUEST_RESULT_BIT)
306 				result = IOP_RESULT_SUCCESS;
307 			else
308 				result = req->header.result;
309 		} else {
310 			srb = hba->srb[index &
311 				~(u_int32_t)IOPMU_QUEUE_ADDR_HOST_BIT];
312 			req = (struct hpt_iop_request_scsi_command *)srb;
313 			result = req->header.result;
314 		}
315 		dxfer = req->dataxfer_length;
316 		goto srb_complete;
317 	}
318 
319 	/*iop req*/
320 	temp = bus_space_read_4(hba->bar0t, hba->bar0h, index +
321 		offsetof(struct hpt_iop_request_header, type));
322 	result = bus_space_read_4(hba->bar0t, hba->bar0h, index +
323 		offsetof(struct hpt_iop_request_header, result));
324 	switch(temp) {
325 	case IOP_REQUEST_TYPE_IOCTL_COMMAND:
326 	{
327 		temp64 = 0;
328 		bus_space_write_region_4(hba->bar0t, hba->bar0h, index +
329 			offsetof(struct hpt_iop_request_header, context),
330 			(u_int32_t *)&temp64, 2);
331 		wakeup((void *)((unsigned long)hba->u.itl.mu + index));
332 		break;
333 	}
334 
335 	case IOP_REQUEST_TYPE_SCSI_COMMAND:
336 		bus_space_read_region_4(hba->bar0t, hba->bar0h, index +
337 			offsetof(struct hpt_iop_request_header, context),
338 			(u_int32_t *)&temp64, 2);
339 		srb = (struct hpt_iop_srb *)(unsigned long)temp64;
340 		dxfer = bus_space_read_4(hba->bar0t, hba->bar0h,
341 				index + offsetof(struct hpt_iop_request_scsi_command,
342 				dataxfer_length));
343 srb_complete:
344 		ccb = (union ccb *)srb->ccb;
345 		if (ccb->ccb_h.flags & CAM_CDB_POINTER)
346 			cdb = ccb->csio.cdb_io.cdb_ptr;
347 		else
348 			cdb = ccb->csio.cdb_io.cdb_bytes;
349 
350 		if (cdb[0] == SYNCHRONIZE_CACHE) { /* ??? */
351 			ccb->ccb_h.status = CAM_REQ_CMP;
352 			goto scsi_done;
353 		}
354 
355 		switch (result) {
356 		case IOP_RESULT_SUCCESS:
357 			switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
358 			case CAM_DIR_IN:
359 				bus_dmamap_sync(hba->io_dmat,
360 					srb->dma_map, BUS_DMASYNC_POSTREAD);
361 				bus_dmamap_unload(hba->io_dmat, srb->dma_map);
362 				break;
363 			case CAM_DIR_OUT:
364 				bus_dmamap_sync(hba->io_dmat,
365 					srb->dma_map, BUS_DMASYNC_POSTWRITE);
366 				bus_dmamap_unload(hba->io_dmat, srb->dma_map);
367 				break;
368 			}
369 
370 			ccb->ccb_h.status = CAM_REQ_CMP;
371 			break;
372 
373 		case IOP_RESULT_BAD_TARGET:
374 			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
375 			break;
376 		case IOP_RESULT_BUSY:
377 			ccb->ccb_h.status = CAM_BUSY;
378 			break;
379 		case IOP_RESULT_INVALID_REQUEST:
380 			ccb->ccb_h.status = CAM_REQ_INVALID;
381 			break;
382 		case IOP_RESULT_FAIL:
383 			ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
384 			break;
385 		case IOP_RESULT_RESET:
386 			ccb->ccb_h.status = CAM_BUSY;
387 			break;
388 		case IOP_RESULT_CHECK_CONDITION:
389 			memset(&ccb->csio.sense_data, 0,
390 			    sizeof(ccb->csio.sense_data));
391 			if (dxfer < ccb->csio.sense_len)
392 				ccb->csio.sense_resid = ccb->csio.sense_len -
393 				    dxfer;
394 			else
395 				ccb->csio.sense_resid = 0;
396 			if (srb->srb_flag & HPT_SRB_FLAG_HIGH_MEM_ACESS) {/*iop*/
397 				bus_space_read_region_1(hba->bar0t, hba->bar0h,
398 					index + offsetof(struct hpt_iop_request_scsi_command,
399 					sg_list), (u_int8_t *)&ccb->csio.sense_data,
400 					MIN(dxfer, sizeof(ccb->csio.sense_data)));
401 			} else {
402 				memcpy(&ccb->csio.sense_data, &req->sg_list,
403 					MIN(dxfer, sizeof(ccb->csio.sense_data)));
404 			}
405 			ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
406 			ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
407 			ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
408 			break;
409 		default:
410 			ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
411 			break;
412 		}
413 scsi_done:
414 		if (srb->srb_flag & HPT_SRB_FLAG_HIGH_MEM_ACESS)
415 			BUS_SPACE_WRT4_ITL(outbound_queue, index);
416 
417 		ccb->csio.resid = ccb->csio.dxfer_len - dxfer;
418 
419 		hptiop_free_srb(hba, srb);
420 		xpt_done(ccb);
421 		break;
422 	}
423 }
424 
425 static void hptiop_drain_outbound_queue_itl(struct hpt_iop_hba *hba)
426 {
427 	u_int32_t req, temp;
428 
429 	while ((req = BUS_SPACE_RD4_ITL(outbound_queue)) !=IOPMU_QUEUE_EMPTY) {
430 		if (req & IOPMU_QUEUE_MASK_HOST_BITS)
431 			hptiop_request_callback_itl(hba, req);
432 		else {
433 			temp = bus_space_read_4(hba->bar0t,
434 					hba->bar0h,req +
435 					offsetof(struct hpt_iop_request_header,
436 						flags));
437 			if (temp & IOP_REQUEST_FLAG_SYNC_REQUEST) {
438 				u_int64_t temp64;
439 				bus_space_read_region_4(hba->bar0t,
440 					hba->bar0h,req +
441 					offsetof(struct hpt_iop_request_header,
442 						context),
443 					(u_int32_t *)&temp64, 2);
444 				if (temp64) {
445 					hptiop_request_callback_itl(hba, req);
446 				} else {
447 					temp64 = 1;
448 					bus_space_write_region_4(hba->bar0t,
449 						hba->bar0h,req +
450 						offsetof(struct hpt_iop_request_header,
451 							context),
452 						(u_int32_t *)&temp64, 2);
453 				}
454 			} else
455 				hptiop_request_callback_itl(hba, req);
456 		}
457 	}
458 }
459 
460 static int hptiop_intr_itl(struct hpt_iop_hba * hba)
461 {
462 	u_int32_t status;
463 	int ret = 0;
464 
465 	status = BUS_SPACE_RD4_ITL(outbound_intstatus);
466 
467 	if (status & IOPMU_OUTBOUND_INT_MSG0) {
468 		u_int32_t msg = BUS_SPACE_RD4_ITL(outbound_msgaddr0);
469 		KdPrint(("hptiop: received outbound msg %x\n", msg));
470 		BUS_SPACE_WRT4_ITL(outbound_intstatus, IOPMU_OUTBOUND_INT_MSG0);
471 		hptiop_os_message_callback(hba, msg);
472 		ret = 1;
473 	}
474 
475 	if (status & IOPMU_OUTBOUND_INT_POSTQUEUE) {
476 		hptiop_drain_outbound_queue_itl(hba);
477 		ret = 1;
478 	}
479 
480 	return ret;
481 }
482 
483 static void hptiop_request_callback_mv(struct hpt_iop_hba * hba,
484 							u_int64_t _tag)
485 {
486 	u_int32_t context = (u_int32_t)_tag;
487 
488 	if (context & MVIOP_CMD_TYPE_SCSI) {
489 		struct hpt_iop_srb *srb;
490 		struct hpt_iop_request_scsi_command *req;
491 		union ccb *ccb;
492 		u_int8_t *cdb;
493 
494 		srb = hba->srb[context >> MVIOP_REQUEST_NUMBER_START_BIT];
495 		req = (struct hpt_iop_request_scsi_command *)srb;
496 		ccb = (union ccb *)srb->ccb;
497 		if (ccb->ccb_h.flags & CAM_CDB_POINTER)
498 			cdb = ccb->csio.cdb_io.cdb_ptr;
499 		else
500 			cdb = ccb->csio.cdb_io.cdb_bytes;
501 
502 		if (cdb[0] == SYNCHRONIZE_CACHE) { /* ??? */
503 			ccb->ccb_h.status = CAM_REQ_CMP;
504 			goto scsi_done;
505 		}
506 		if (context & MVIOP_MU_QUEUE_REQUEST_RESULT_BIT)
507 			req->header.result = IOP_RESULT_SUCCESS;
508 
509 		switch (req->header.result) {
510 		case IOP_RESULT_SUCCESS:
511 			switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
512 			case CAM_DIR_IN:
513 				bus_dmamap_sync(hba->io_dmat,
514 					srb->dma_map, BUS_DMASYNC_POSTREAD);
515 				bus_dmamap_unload(hba->io_dmat, srb->dma_map);
516 				break;
517 			case CAM_DIR_OUT:
518 				bus_dmamap_sync(hba->io_dmat,
519 					srb->dma_map, BUS_DMASYNC_POSTWRITE);
520 				bus_dmamap_unload(hba->io_dmat, srb->dma_map);
521 				break;
522 			}
523 			ccb->ccb_h.status = CAM_REQ_CMP;
524 			break;
525 		case IOP_RESULT_BAD_TARGET:
526 			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
527 			break;
528 		case IOP_RESULT_BUSY:
529 			ccb->ccb_h.status = CAM_BUSY;
530 			break;
531 		case IOP_RESULT_INVALID_REQUEST:
532 			ccb->ccb_h.status = CAM_REQ_INVALID;
533 			break;
534 		case IOP_RESULT_FAIL:
535 			ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
536 			break;
537 		case IOP_RESULT_RESET:
538 			ccb->ccb_h.status = CAM_BUSY;
539 			break;
540 		case IOP_RESULT_CHECK_CONDITION:
541 			memset(&ccb->csio.sense_data, 0,
542 			    sizeof(ccb->csio.sense_data));
543 			if (req->dataxfer_length < ccb->csio.sense_len)
544 				ccb->csio.sense_resid = ccb->csio.sense_len -
545 				    req->dataxfer_length;
546 			else
547 				ccb->csio.sense_resid = 0;
548 			memcpy(&ccb->csio.sense_data, &req->sg_list,
549 				MIN(req->dataxfer_length, sizeof(ccb->csio.sense_data)));
550 			ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
551 			ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
552 			ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
553 			break;
554 		default:
555 			ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
556 			break;
557 		}
558 scsi_done:
559 		ccb->csio.resid = ccb->csio.dxfer_len - req->dataxfer_length;
560 
561 		hptiop_free_srb(hba, srb);
562 		xpt_done(ccb);
563 	} else if (context & MVIOP_CMD_TYPE_IOCTL) {
564 		struct hpt_iop_request_ioctl_command *req = hba->ctlcfg_ptr;
565 		if (context & MVIOP_MU_QUEUE_REQUEST_RESULT_BIT)
566 			hba->config_done = 1;
567 		else
568 			hba->config_done = -1;
569 		wakeup(req);
570 	} else if (context &
571 			(MVIOP_CMD_TYPE_SET_CONFIG |
572 				MVIOP_CMD_TYPE_GET_CONFIG))
573 		hba->config_done = 1;
574 	else {
575 		device_printf(hba->pcidev, "wrong callback type\n");
576 	}
577 }
578 
579 static void hptiop_drain_outbound_queue_mv(struct hpt_iop_hba * hba)
580 {
581 	u_int64_t req;
582 
583 	while ((req = hptiop_mv_outbound_read(hba))) {
584 		if (req & MVIOP_MU_QUEUE_ADDR_HOST_BIT) {
585 			if (req & MVIOP_MU_QUEUE_REQUEST_RETURN_CONTEXT) {
586 				hptiop_request_callback_mv(hba, req);
587 			}
588 		}
589 	}
590 }
591 
592 static int hptiop_intr_mv(struct hpt_iop_hba * hba)
593 {
594 	u_int32_t status;
595 	int ret = 0;
596 
597 	status = BUS_SPACE_RD4_MV0(outbound_doorbell);
598 
599 	if (status)
600 		BUS_SPACE_WRT4_MV0(outbound_doorbell, ~status);
601 
602 	if (status & MVIOP_MU_OUTBOUND_INT_MSG) {
603 		u_int32_t msg = BUS_SPACE_RD4_MV2(outbound_msg);
604 		KdPrint(("hptiop: received outbound msg %x\n", msg));
605 		hptiop_os_message_callback(hba, msg);
606 		ret = 1;
607 	}
608 
609 	if (status & MVIOP_MU_OUTBOUND_INT_POSTQUEUE) {
610 		hptiop_drain_outbound_queue_mv(hba);
611 		ret = 1;
612 	}
613 
614 	return ret;
615 }
616 
617 static int hptiop_send_sync_request_itl(struct hpt_iop_hba * hba,
618 					u_int32_t req32, u_int32_t millisec)
619 {
620 	u_int32_t i;
621 	u_int64_t temp64;
622 
623 	BUS_SPACE_WRT4_ITL(inbound_queue, req32);
624 	BUS_SPACE_RD4_ITL(outbound_intstatus);
625 
626 	for (i = 0; i < millisec; i++) {
627 		hptiop_intr_itl(hba);
628 		bus_space_read_region_4(hba->bar0t, hba->bar0h, req32 +
629 			offsetof(struct hpt_iop_request_header, context),
630 			(u_int32_t *)&temp64, 2);
631 		if (temp64)
632 			return 0;
633 		DELAY(1000);
634 	}
635 
636 	return -1;
637 }
638 
639 static int hptiop_send_sync_request_mv(struct hpt_iop_hba *hba,
640 					void *req, u_int32_t millisec)
641 {
642 	u_int32_t i;
643 	u_int64_t phy_addr;
644 	hba->config_done = 0;
645 
646 	phy_addr = hba->ctlcfgcmd_phy |
647 			(u_int64_t)MVIOP_MU_QUEUE_ADDR_HOST_BIT;
648 	((struct hpt_iop_request_get_config *)req)->header.flags |=
649 		IOP_REQUEST_FLAG_SYNC_REQUEST |
650 		IOP_REQUEST_FLAG_OUTPUT_CONTEXT;
651 	hptiop_mv_inbound_write(phy_addr, hba);
652 	BUS_SPACE_RD4_MV0(outbound_intmask);
653 
654 	for (i = 0; i < millisec; i++) {
655 		hptiop_intr_mv(hba);
656 		if (hba->config_done)
657 			return 0;
658 		DELAY(1000);
659 	}
660 	return -1;
661 }
662 
663 static int hptiop_send_sync_msg(struct hpt_iop_hba *hba,
664 					u_int32_t msg, u_int32_t millisec)
665 {
666 	u_int32_t i;
667 
668 	hba->msg_done = 0;
669 	hba->ops->post_msg(hba, msg);
670 
671 	for (i=0; i<millisec; i++) {
672 		hba->ops->iop_intr(hba);
673 		if (hba->msg_done)
674 			break;
675 		DELAY(1000);
676 	}
677 
678 	return hba->msg_done? 0 : -1;
679 }
680 
681 static int hptiop_get_config_itl(struct hpt_iop_hba * hba,
682 				struct hpt_iop_request_get_config * config)
683 {
684 	u_int32_t req32;
685 
686 	config->header.size = sizeof(struct hpt_iop_request_get_config);
687 	config->header.type = IOP_REQUEST_TYPE_GET_CONFIG;
688 	config->header.flags = IOP_REQUEST_FLAG_SYNC_REQUEST;
689 	config->header.result = IOP_RESULT_PENDING;
690 	config->header.context = 0;
691 
692 	req32 = BUS_SPACE_RD4_ITL(inbound_queue);
693 	if (req32 == IOPMU_QUEUE_EMPTY)
694 		return -1;
695 
696 	bus_space_write_region_4(hba->bar0t, hba->bar0h,
697 			req32, (u_int32_t *)config,
698 			sizeof(struct hpt_iop_request_header) >> 2);
699 
700 	if (hptiop_send_sync_request_itl(hba, req32, 20000)) {
701 		KdPrint(("hptiop: get config send cmd failed"));
702 		return -1;
703 	}
704 
705 	bus_space_read_region_4(hba->bar0t, hba->bar0h,
706 			req32, (u_int32_t *)config,
707 			sizeof(struct hpt_iop_request_get_config) >> 2);
708 
709 	BUS_SPACE_WRT4_ITL(outbound_queue, req32);
710 
711 	return 0;
712 }
713 
714 static int hptiop_get_config_mv(struct hpt_iop_hba * hba,
715 				struct hpt_iop_request_get_config * config)
716 {
717 	struct hpt_iop_request_get_config *req;
718 
719 	if (!(req = hba->ctlcfg_ptr))
720 		return -1;
721 
722 	req->header.flags = 0;
723 	req->header.type = IOP_REQUEST_TYPE_GET_CONFIG;
724 	req->header.size = sizeof(struct hpt_iop_request_get_config);
725 	req->header.result = IOP_RESULT_PENDING;
726 	req->header.context = MVIOP_CMD_TYPE_GET_CONFIG;
727 
728 	if (hptiop_send_sync_request_mv(hba, req, 20000)) {
729 		KdPrint(("hptiop: get config send cmd failed"));
730 		return -1;
731 	}
732 
733 	*config = *req;
734 	return 0;
735 }
736 
737 static int hptiop_set_config_itl(struct hpt_iop_hba *hba,
738 				struct hpt_iop_request_set_config *config)
739 {
740 	u_int32_t req32;
741 
742 	req32 = BUS_SPACE_RD4_ITL(inbound_queue);
743 
744 	if (req32 == IOPMU_QUEUE_EMPTY)
745 		return -1;
746 
747 	config->header.size = sizeof(struct hpt_iop_request_set_config);
748 	config->header.type = IOP_REQUEST_TYPE_SET_CONFIG;
749 	config->header.flags = IOP_REQUEST_FLAG_SYNC_REQUEST;
750 	config->header.result = IOP_RESULT_PENDING;
751 	config->header.context = 0;
752 
753 	bus_space_write_region_4(hba->bar0t, hba->bar0h, req32,
754 		(u_int32_t *)config,
755 		sizeof(struct hpt_iop_request_set_config) >> 2);
756 
757 	if (hptiop_send_sync_request_itl(hba, req32, 20000)) {
758 		KdPrint(("hptiop: set config send cmd failed"));
759 		return -1;
760 	}
761 
762 	BUS_SPACE_WRT4_ITL(outbound_queue, req32);
763 
764 	return 0;
765 }
766 
767 static int hptiop_set_config_mv(struct hpt_iop_hba *hba,
768 				struct hpt_iop_request_set_config *config)
769 {
770 	struct hpt_iop_request_set_config *req;
771 
772 	if (!(req = hba->ctlcfg_ptr))
773 		return -1;
774 
775 	memcpy((u_int8_t *)req + sizeof(struct hpt_iop_request_header),
776 		(u_int8_t *)config + sizeof(struct hpt_iop_request_header),
777 		sizeof(struct hpt_iop_request_set_config) -
778 			sizeof(struct hpt_iop_request_header));
779 
780 	req->header.flags = 0;
781 	req->header.type = IOP_REQUEST_TYPE_SET_CONFIG;
782 	req->header.size = sizeof(struct hpt_iop_request_set_config);
783 	req->header.result = IOP_RESULT_PENDING;
784 	req->header.context = MVIOP_CMD_TYPE_SET_CONFIG;
785 
786 	if (hptiop_send_sync_request_mv(hba, req, 20000)) {
787 		KdPrint(("hptiop: set config send cmd failed"));
788 		return -1;
789 	}
790 
791 	return 0;
792 }
793 
794 static int hptiop_post_ioctl_command_itl(struct hpt_iop_hba *hba,
795 				u_int32_t req32,
796 				struct hpt_iop_ioctl_param *pParams)
797 {
798 	u_int64_t temp64;
799 	struct hpt_iop_request_ioctl_command req;
800 
801 	if ((((pParams->nInBufferSize + 3) & ~3) + pParams->nOutBufferSize) >
802 			(hba->max_request_size -
803 			offsetof(struct hpt_iop_request_ioctl_command, buf))) {
804 		device_printf(hba->pcidev, "request size beyond max value");
805 		return -1;
806 	}
807 
808 	req.header.size = offsetof(struct hpt_iop_request_ioctl_command, buf)
809 		+ pParams->nInBufferSize;
810 	req.header.type = IOP_REQUEST_TYPE_IOCTL_COMMAND;
811 	req.header.flags = IOP_REQUEST_FLAG_SYNC_REQUEST;
812 	req.header.result = IOP_RESULT_PENDING;
813 	req.header.context = req32 + (u_int64_t)(unsigned long)hba->u.itl.mu;
814 	req.ioctl_code = HPT_CTL_CODE_BSD_TO_IOP(pParams->dwIoControlCode);
815 	req.inbuf_size = pParams->nInBufferSize;
816 	req.outbuf_size = pParams->nOutBufferSize;
817 	req.bytes_returned = 0;
818 
819 	bus_space_write_region_4(hba->bar0t, hba->bar0h, req32, (u_int32_t *)&req,
820 		offsetof(struct hpt_iop_request_ioctl_command, buf)>>2);
821 
822 	hptiop_lock_adapter(hba);
823 
824 	BUS_SPACE_WRT4_ITL(inbound_queue, req32);
825 	BUS_SPACE_RD4_ITL(outbound_intstatus);
826 
827 	bus_space_read_region_4(hba->bar0t, hba->bar0h, req32 +
828 		offsetof(struct hpt_iop_request_ioctl_command, header.context),
829 		(u_int32_t *)&temp64, 2);
830 	while (temp64) {
831 		if (hptiop_sleep(hba, (void *)((unsigned long)hba->u.itl.mu + req32),
832 				0, "hptctl", HPT_OSM_TIMEOUT)==0)
833 			break;
834 		hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_RESET, 60000);
835 		bus_space_read_region_4(hba->bar0t, hba->bar0h,req32 +
836 			offsetof(struct hpt_iop_request_ioctl_command,
837 				header.context),
838 			(u_int32_t *)&temp64, 2);
839 	}
840 
841 	hptiop_unlock_adapter(hba);
842 	return 0;
843 }
844 
845 static int hptiop_bus_space_copyin(struct hpt_iop_hba *hba, u_int32_t bus, void *user, int size)
846 {
847 	unsigned char byte;
848 	int i;
849 
850 	for (i=0; i<size; i++) {
851 		if (copyin((u_int8_t *)user + i, &byte, 1))
852 			return -1;
853 		bus_space_write_1(hba->bar0t, hba->bar0h, bus + i, byte);
854 	}
855 
856 	return 0;
857 }
858 
859 static int hptiop_bus_space_copyout(struct hpt_iop_hba *hba, u_int32_t bus, void *user, int size)
860 {
861 	unsigned char byte;
862 	int i;
863 
864 	for (i=0; i<size; i++) {
865 		byte = bus_space_read_1(hba->bar0t, hba->bar0h, bus + i);
866 		if (copyout(&byte, (u_int8_t *)user + i, 1))
867 			return -1;
868 	}
869 
870 	return 0;
871 }
872 
873 static int hptiop_do_ioctl_itl(struct hpt_iop_hba *hba,
874 				struct hpt_iop_ioctl_param * pParams)
875 {
876 	u_int32_t req32;
877 	u_int32_t result;
878 
879 	if ((pParams->Magic != HPT_IOCTL_MAGIC) &&
880 		(pParams->Magic != HPT_IOCTL_MAGIC32))
881 		return EFAULT;
882 
883 	req32 = BUS_SPACE_RD4_ITL(inbound_queue);
884 	if (req32 == IOPMU_QUEUE_EMPTY)
885 		return EFAULT;
886 
887 	if (pParams->nInBufferSize)
888 		if (hptiop_bus_space_copyin(hba, req32 +
889 			offsetof(struct hpt_iop_request_ioctl_command, buf),
890 			(void *)pParams->lpInBuffer, pParams->nInBufferSize))
891 			goto invalid;
892 
893 	if (hptiop_post_ioctl_command_itl(hba, req32, pParams))
894 		goto invalid;
895 
896 	result = bus_space_read_4(hba->bar0t, hba->bar0h, req32 +
897 			offsetof(struct hpt_iop_request_ioctl_command,
898 				header.result));
899 
900 	if (result == IOP_RESULT_SUCCESS) {
901 		if (pParams->nOutBufferSize)
902 			if (hptiop_bus_space_copyout(hba, req32 +
903 				offsetof(struct hpt_iop_request_ioctl_command, buf) +
904 					((pParams->nInBufferSize + 3) & ~3),
905 				(void *)pParams->lpOutBuffer, pParams->nOutBufferSize))
906 				goto invalid;
907 
908 		if (pParams->lpBytesReturned) {
909 			if (hptiop_bus_space_copyout(hba, req32 +
910 				offsetof(struct hpt_iop_request_ioctl_command, bytes_returned),
911 				(void *)pParams->lpBytesReturned, sizeof(unsigned  long)))
912 				goto invalid;
913 		}
914 
915 		BUS_SPACE_WRT4_ITL(outbound_queue, req32);
916 
917 		return 0;
918 	} else{
919 invalid:
920 		BUS_SPACE_WRT4_ITL(outbound_queue, req32);
921 
922 		return EFAULT;
923 	}
924 }
925 
926 static int hptiop_post_ioctl_command_mv(struct hpt_iop_hba *hba,
927 				struct hpt_iop_request_ioctl_command *req,
928 				struct hpt_iop_ioctl_param *pParams)
929 {
930 	u_int64_t req_phy;
931 	int size = 0;
932 
933 	if ((((pParams->nInBufferSize + 3) & ~3) + pParams->nOutBufferSize) >
934 			(hba->max_request_size -
935 			offsetof(struct hpt_iop_request_ioctl_command, buf))) {
936 		device_printf(hba->pcidev, "request size beyond max value");
937 		return -1;
938 	}
939 
940 	req->ioctl_code = HPT_CTL_CODE_BSD_TO_IOP(pParams->dwIoControlCode);
941 	req->inbuf_size = pParams->nInBufferSize;
942 	req->outbuf_size = pParams->nOutBufferSize;
943 	req->header.size = offsetof(struct hpt_iop_request_ioctl_command, buf)
944 					+ pParams->nInBufferSize;
945 	req->header.context = (u_int64_t)MVIOP_CMD_TYPE_IOCTL;
946 	req->header.type = IOP_REQUEST_TYPE_IOCTL_COMMAND;
947 	req->header.result = IOP_RESULT_PENDING;
948 	req->header.flags = IOP_REQUEST_FLAG_OUTPUT_CONTEXT;
949 	size = req->header.size >> 8;
950 	size = size > 3 ? 3 : size;
951 	req_phy = hba->ctlcfgcmd_phy | MVIOP_MU_QUEUE_ADDR_HOST_BIT | size;
952 	hptiop_mv_inbound_write(req_phy, hba);
953 
954 	BUS_SPACE_RD4_MV0(outbound_intmask);
955 
956 	while (hba->config_done == 0) {
957 		if (hptiop_sleep(hba, req, 0,
958 			"hptctl", HPT_OSM_TIMEOUT)==0)
959 			continue;
960 		hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_RESET, 60000);
961 	}
962 	return 0;
963 }
964 
965 static int hptiop_do_ioctl_mv(struct hpt_iop_hba *hba,
966 				struct hpt_iop_ioctl_param *pParams)
967 {
968 	struct hpt_iop_request_ioctl_command *req;
969 
970 	if ((pParams->Magic != HPT_IOCTL_MAGIC) &&
971 		(pParams->Magic != HPT_IOCTL_MAGIC32))
972 		return EFAULT;
973 
974 	req = (struct hpt_iop_request_ioctl_command *)(hba->ctlcfg_ptr);
975 	hba->config_done = 0;
976 	hptiop_lock_adapter(hba);
977 	if (pParams->nInBufferSize)
978 		if (copyin((void *)pParams->lpInBuffer,
979 				req->buf, pParams->nInBufferSize))
980 			goto invalid;
981 	if (hptiop_post_ioctl_command_mv(hba, req, pParams))
982 		goto invalid;
983 
984 	if (hba->config_done == 1) {
985 		if (pParams->nOutBufferSize)
986 			if (copyout(req->buf +
987 				((pParams->nInBufferSize + 3) & ~3),
988 				(void *)pParams->lpOutBuffer,
989 				pParams->nOutBufferSize))
990 				goto invalid;
991 
992 		if (pParams->lpBytesReturned)
993 			if (copyout(&req->bytes_returned,
994 				(void*)pParams->lpBytesReturned,
995 				sizeof(u_int32_t)))
996 				goto invalid;
997 		hptiop_unlock_adapter(hba);
998 		return 0;
999 	} else{
1000 invalid:
1001 		hptiop_unlock_adapter(hba);
1002 		return EFAULT;
1003 	}
1004 }
1005 
1006 static int  hptiop_rescan_bus(struct hpt_iop_hba * hba)
1007 {
1008 	union ccb           *ccb;
1009 
1010 	if ((ccb = xpt_alloc_ccb()) == NULL)
1011 		return(ENOMEM);
1012 	if (xpt_create_path(&ccb->ccb_h.path, xpt_periph, cam_sim_path(hba->sim),
1013 		CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1014 		xpt_free_ccb(ccb);
1015 		return(EIO);
1016 	}
1017 
1018 	xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, 5/*priority (low)*/);
1019 	ccb->ccb_h.func_code = XPT_SCAN_BUS;
1020 	ccb->ccb_h.cbfcnp = hptiop_bus_scan_cb;
1021 	ccb->crcn.flags = CAM_FLAG_NONE;
1022 	xpt_action(ccb);
1023 	return(0);
1024 }
1025 
1026 static void hptiop_bus_scan_cb(struct cam_periph *periph, union ccb *ccb)
1027 {
1028 	xpt_free_path(ccb->ccb_h.path);
1029 	kfree(ccb, M_TEMP);
1030 }
1031 
1032 static  bus_dmamap_callback_t   hptiop_map_srb;
1033 static  bus_dmamap_callback_t   hptiop_post_scsi_command;
1034 static  bus_dmamap_callback_t   hptiop_mv_map_ctlcfg;
1035 
1036 static int hptiop_alloc_pci_res_itl(struct hpt_iop_hba *hba)
1037 {
1038 	hba->bar0_rid = 0x10;
1039 	hba->bar0_res = bus_alloc_resource_any(hba->pcidev,
1040 			SYS_RES_MEMORY, &hba->bar0_rid, RF_ACTIVE);
1041 
1042 	if (hba->bar0_res == NULL) {
1043 		device_printf(hba->pcidev,
1044 			"failed to get iop base adrress.\n");
1045 		return -1;
1046 	}
1047 	hba->bar0t = rman_get_bustag(hba->bar0_res);
1048 	hba->bar0h = rman_get_bushandle(hba->bar0_res);
1049 	hba->u.itl.mu = (struct hpt_iopmu_itl *)
1050 				rman_get_virtual(hba->bar0_res);
1051 
1052 	if (!hba->u.itl.mu) {
1053 		bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1054 					hba->bar0_rid, hba->bar0_res);
1055 		device_printf(hba->pcidev, "alloc mem res failed\n");
1056 		return -1;
1057 	}
1058 
1059 	return 0;
1060 }
1061 
1062 static int hptiop_alloc_pci_res_mv(struct hpt_iop_hba *hba)
1063 {
1064 	hba->bar0_rid = 0x10;
1065 	hba->bar0_res = bus_alloc_resource_any(hba->pcidev,
1066 			SYS_RES_MEMORY, &hba->bar0_rid, RF_ACTIVE);
1067 
1068 	if (hba->bar0_res == NULL) {
1069 		device_printf(hba->pcidev, "failed to get iop bar0.\n");
1070 		return -1;
1071 	}
1072 	hba->bar0t = rman_get_bustag(hba->bar0_res);
1073 	hba->bar0h = rman_get_bushandle(hba->bar0_res);
1074 	hba->u.mv.regs = (struct hpt_iopmv_regs *)
1075 				rman_get_virtual(hba->bar0_res);
1076 
1077 	if (!hba->u.mv.regs) {
1078 		bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1079 					hba->bar0_rid, hba->bar0_res);
1080 		device_printf(hba->pcidev, "alloc bar0 mem res failed\n");
1081 		return -1;
1082 	}
1083 
1084 	hba->bar2_rid = 0x18;
1085 	hba->bar2_res = bus_alloc_resource_any(hba->pcidev,
1086 			SYS_RES_MEMORY, &hba->bar2_rid, RF_ACTIVE);
1087 
1088 	if (hba->bar2_res == NULL) {
1089 		bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1090 					hba->bar0_rid, hba->bar0_res);
1091 		device_printf(hba->pcidev, "failed to get iop bar2.\n");
1092 		return -1;
1093 	}
1094 
1095 	hba->bar2t = rman_get_bustag(hba->bar2_res);
1096 	hba->bar2h = rman_get_bushandle(hba->bar2_res);
1097 	hba->u.mv.mu = (struct hpt_iopmu_mv *)rman_get_virtual(hba->bar2_res);
1098 
1099 	if (!hba->u.mv.mu) {
1100 		bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1101 					hba->bar0_rid, hba->bar0_res);
1102 		bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1103 					hba->bar2_rid, hba->bar2_res);
1104 		device_printf(hba->pcidev, "alloc mem bar2 res failed\n");
1105 		return -1;
1106 	}
1107 
1108 	return 0;
1109 }
1110 
1111 static void hptiop_release_pci_res_itl(struct hpt_iop_hba *hba)
1112 {
1113 	if (hba->bar0_res)
1114 		bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1115 			hba->bar0_rid, hba->bar0_res);
1116 }
1117 
1118 static void hptiop_release_pci_res_mv(struct hpt_iop_hba *hba)
1119 {
1120 	if (hba->bar0_res)
1121 		bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1122 			hba->bar0_rid, hba->bar0_res);
1123 	if (hba->bar2_res)
1124 		bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
1125 			hba->bar2_rid, hba->bar2_res);
1126 }
1127 
1128 static int hptiop_internal_memalloc_mv(struct hpt_iop_hba *hba)
1129 {
1130 	if (bus_dma_tag_create(hba->parent_dmat,
1131 				1,
1132 				0,
1133 				BUS_SPACE_MAXADDR_32BIT,
1134 				BUS_SPACE_MAXADDR,
1135 				NULL, NULL,
1136 				0x800 - 0x8,
1137 				1,
1138 				BUS_SPACE_MAXSIZE_32BIT,
1139 				BUS_DMA_ALLOCNOW,
1140 				&hba->ctlcfg_dmat)) {
1141 		device_printf(hba->pcidev, "alloc ctlcfg_dmat failed\n");
1142 		return -1;
1143 	}
1144 
1145 	if (bus_dmamem_alloc(hba->ctlcfg_dmat, (void **)&hba->ctlcfg_ptr,
1146 		BUS_DMA_WAITOK | BUS_DMA_COHERENT,
1147 		&hba->ctlcfg_dmamap) != 0) {
1148 			device_printf(hba->pcidev,
1149 					"bus_dmamem_alloc failed!\n");
1150 			bus_dma_tag_destroy(hba->ctlcfg_dmat);
1151 			return -1;
1152 	}
1153 
1154 	if (bus_dmamap_load(hba->ctlcfg_dmat,
1155 			hba->ctlcfg_dmamap, hba->ctlcfg_ptr,
1156 			MVIOP_IOCTLCFG_SIZE,
1157 			hptiop_mv_map_ctlcfg, hba, 0)) {
1158 		device_printf(hba->pcidev, "bus_dmamap_load failed!\n");
1159 		if (hba->ctlcfg_dmat)
1160 			bus_dmamem_free(hba->ctlcfg_dmat,
1161 				hba->ctlcfg_ptr, hba->ctlcfg_dmamap);
1162 			bus_dma_tag_destroy(hba->ctlcfg_dmat);
1163 		return -1;
1164 	}
1165 
1166 	return 0;
1167 }
1168 
1169 static int hptiop_internal_memfree_mv(struct hpt_iop_hba *hba)
1170 {
1171 	if (hba->ctlcfg_dmat) {
1172 		bus_dmamap_unload(hba->ctlcfg_dmat, hba->ctlcfg_dmamap);
1173 		bus_dmamem_free(hba->ctlcfg_dmat,
1174 					hba->ctlcfg_ptr, hba->ctlcfg_dmamap);
1175 		bus_dma_tag_destroy(hba->ctlcfg_dmat);
1176 	}
1177 
1178 	return 0;
1179 }
1180 
1181 /*
1182  * CAM driver interface
1183  */
1184 static device_method_t driver_methods[] = {
1185 	/* Device interface */
1186 	DEVMETHOD(device_probe,     hptiop_probe),
1187 	DEVMETHOD(device_attach,    hptiop_attach),
1188 	DEVMETHOD(device_detach,    hptiop_detach),
1189 	DEVMETHOD(device_shutdown,  hptiop_shutdown),
1190 	{ 0, 0 }
1191 };
1192 
1193 static struct hptiop_adapter_ops hptiop_itl_ops = {
1194 	.iop_wait_ready    = hptiop_wait_ready_itl,
1195 	.internal_memalloc = 0,
1196 	.internal_memfree  = 0,
1197 	.alloc_pci_res     = hptiop_alloc_pci_res_itl,
1198 	.release_pci_res   = hptiop_release_pci_res_itl,
1199 	.enable_intr       = hptiop_enable_intr_itl,
1200 	.disable_intr      = hptiop_disable_intr_itl,
1201 	.get_config        = hptiop_get_config_itl,
1202 	.set_config        = hptiop_set_config_itl,
1203 	.iop_intr          = hptiop_intr_itl,
1204 	.post_msg          = hptiop_post_msg_itl,
1205 	.post_req          = hptiop_post_req_itl,
1206 	.do_ioctl          = hptiop_do_ioctl_itl,
1207 };
1208 
1209 static struct hptiop_adapter_ops hptiop_mv_ops = {
1210 	.iop_wait_ready    = hptiop_wait_ready_mv,
1211 	.internal_memalloc = hptiop_internal_memalloc_mv,
1212 	.internal_memfree  = hptiop_internal_memfree_mv,
1213 	.alloc_pci_res     = hptiop_alloc_pci_res_mv,
1214 	.release_pci_res   = hptiop_release_pci_res_mv,
1215 	.enable_intr       = hptiop_enable_intr_mv,
1216 	.disable_intr      = hptiop_disable_intr_mv,
1217 	.get_config        = hptiop_get_config_mv,
1218 	.set_config        = hptiop_set_config_mv,
1219 	.iop_intr          = hptiop_intr_mv,
1220 	.post_msg          = hptiop_post_msg_mv,
1221 	.post_req          = hptiop_post_req_mv,
1222 	.do_ioctl          = hptiop_do_ioctl_mv,
1223 };
1224 
1225 static driver_t hptiop_pci_driver = {
1226 	driver_name,
1227 	driver_methods,
1228 	sizeof(struct hpt_iop_hba)
1229 };
1230 
1231 DRIVER_MODULE(hptiop, pci, hptiop_pci_driver, hptiop_devclass, NULL, NULL);
1232 MODULE_VERSION(hptiop, 1);
1233 
1234 static int hptiop_probe(device_t dev)
1235 {
1236 	struct hpt_iop_hba *hba;
1237 	u_int32_t id;
1238 	static char buf[256];
1239 	int sas = 0;
1240 	struct hptiop_adapter_ops *ops;
1241 
1242 	if (pci_get_vendor(dev) != 0x1103)
1243 		return (ENXIO);
1244 
1245 	id = pci_get_device(dev);
1246 
1247 	switch (id) {
1248 		case 0x4322:
1249 		case 0x4321:
1250 		case 0x4320:
1251 			sas = 1;
1252 		case 0x3220:
1253 		case 0x3320:
1254 		case 0x3410:
1255 		case 0x3520:
1256 		case 0x3510:
1257 		case 0x3511:
1258 		case 0x3521:
1259 		case 0x3522:
1260 		case 0x3540:
1261 			ops = &hptiop_itl_ops;
1262 			break;
1263 		case 0x3120:
1264 		case 0x3122:
1265 		case 0x3020:
1266 			ops = &hptiop_mv_ops;
1267 			break;
1268 		default:
1269 			return (ENXIO);
1270 	}
1271 
1272 	device_printf(dev, "adapter at PCI %d:%d:%d, IRQ %d\n",
1273 		pci_get_bus(dev), pci_get_slot(dev),
1274 		pci_get_function(dev), pci_get_irq(dev));
1275 
1276 	ksprintf(buf, "RocketRAID %x %s Controller",
1277 				id, sas ? "SAS" : "SATA");
1278 	device_set_desc_copy(dev, buf);
1279 
1280 	hba = (struct hpt_iop_hba *)device_get_softc(dev);
1281 	bzero(hba, sizeof(struct hpt_iop_hba));
1282 	hba->ops = ops;
1283 
1284 	KdPrint(("hba->ops=%p\n", hba->ops));
1285 	return 0;
1286 }
1287 
1288 static int hptiop_attach(device_t dev)
1289 {
1290 	struct hpt_iop_hba *hba = (struct hpt_iop_hba *)device_get_softc(dev);
1291 	struct hpt_iop_request_get_config  iop_config;
1292 	struct hpt_iop_request_set_config  set_config;
1293 	int rid = 0;
1294 	struct cam_devq *devq;
1295 	struct ccb_setasync ccb;
1296 	u_int32_t unit = device_get_unit(dev);
1297 
1298 	device_printf(dev, "RocketRAID 3xxx/4xxx controller driver %s\n",
1299 	    driver_version);
1300 
1301 	KdPrint(("hptiop: attach(%d, %d/%d/%d) ops=%p\n", unit,
1302 		pci_get_bus(dev), pci_get_slot(dev),
1303 		pci_get_function(dev), hba->ops));
1304 
1305 	pci_enable_busmaster(dev);
1306 	hba->pcidev = dev;
1307 
1308 	if (hba->ops->alloc_pci_res(hba))
1309 		return ENXIO;
1310 
1311 	if (hba->ops->iop_wait_ready(hba, 2000)) {
1312 		device_printf(dev, "adapter is not ready\n");
1313 		goto release_pci_res;
1314 	}
1315 
1316 	lockinit(&hba->lock, "hptioplock", 0, LK_CANRECURSE);
1317 
1318 	if (bus_dma_tag_create(NULL,/* parent */
1319 			1,  /* alignment */
1320 			0, /* boundary */
1321 			BUS_SPACE_MAXADDR,  /* lowaddr */
1322 			BUS_SPACE_MAXADDR,  /* highaddr */
1323 			NULL, NULL,         /* filter, filterarg */
1324 			BUS_SPACE_MAXSIZE_32BIT,    /* maxsize */
1325 			BUS_SPACE_UNRESTRICTED, /* nsegments */
1326 			BUS_SPACE_MAXSIZE_32BIT,    /* maxsegsize */
1327 			0,      /* flags */
1328 			&hba->parent_dmat   /* tag */))
1329 	{
1330 		device_printf(dev, "alloc parent_dmat failed\n");
1331 		goto release_pci_res;
1332 	}
1333 
1334 	if (hba->ops->internal_memalloc) {
1335 		if (hba->ops->internal_memalloc(hba)) {
1336 			device_printf(dev, "alloc srb_dmat failed\n");
1337 			goto destroy_parent_tag;
1338 		}
1339 	}
1340 
1341 	if (hba->ops->get_config(hba, &iop_config)) {
1342 		device_printf(dev, "get iop config failed.\n");
1343 		goto get_config_failed;
1344 	}
1345 
1346 	hba->firmware_version = iop_config.firmware_version;
1347 	hba->interface_version = iop_config.interface_version;
1348 	hba->max_requests = iop_config.max_requests;
1349 	hba->max_devices = iop_config.max_devices;
1350 	hba->max_request_size = iop_config.request_size;
1351 	hba->max_sg_count = iop_config.max_sg_count;
1352 
1353 	if (bus_dma_tag_create(hba->parent_dmat,/* parent */
1354 			4,  /* alignment */
1355 			BUS_SPACE_MAXADDR_32BIT+1, /* boundary */
1356 			BUS_SPACE_MAXADDR,  /* lowaddr */
1357 			BUS_SPACE_MAXADDR,  /* highaddr */
1358 			NULL, NULL,         /* filter, filterarg */
1359 			PAGE_SIZE * (hba->max_sg_count-1),  /* maxsize */
1360 			hba->max_sg_count,  /* nsegments */
1361 			0x20000,    /* maxsegsize */
1362 			BUS_DMA_ALLOCNOW,       /* flags */
1363 			&hba->io_dmat   /* tag */))
1364 	{
1365 		device_printf(dev, "alloc io_dmat failed\n");
1366 		goto get_config_failed;
1367 	}
1368 
1369 	if (bus_dma_tag_create(hba->parent_dmat,/* parent */
1370 			1,  /* alignment */
1371 			0, /* boundary */
1372 			BUS_SPACE_MAXADDR_32BIT,    /* lowaddr */
1373 			BUS_SPACE_MAXADDR,  /* highaddr */
1374 			NULL, NULL,         /* filter, filterarg */
1375 			HPT_SRB_MAX_SIZE * HPT_SRB_MAX_QUEUE_SIZE + 0x20,
1376 			1,  /* nsegments */
1377 			BUS_SPACE_MAXSIZE_32BIT,    /* maxsegsize */
1378 			0,      /* flags */
1379 			&hba->srb_dmat  /* tag */))
1380 	{
1381 		device_printf(dev, "alloc srb_dmat failed\n");
1382 		goto destroy_io_dmat;
1383 	}
1384 
1385 	if (bus_dmamem_alloc(hba->srb_dmat, (void **)&hba->uncached_ptr,
1386 			BUS_DMA_WAITOK | BUS_DMA_COHERENT,
1387 			&hba->srb_dmamap) != 0)
1388 	{
1389 		device_printf(dev, "srb bus_dmamem_alloc failed!\n");
1390 		goto destroy_srb_dmat;
1391 	}
1392 
1393 	if (bus_dmamap_load(hba->srb_dmat,
1394 			hba->srb_dmamap, hba->uncached_ptr,
1395 			(HPT_SRB_MAX_SIZE * HPT_SRB_MAX_QUEUE_SIZE) + 0x20,
1396 			hptiop_map_srb, hba, 0))
1397 	{
1398 		device_printf(dev, "bus_dmamap_load failed!\n");
1399 		goto srb_dmamem_free;
1400 	}
1401 
1402 	if ((devq = cam_simq_alloc(hba->max_requests - 1 )) == NULL) {
1403 		device_printf(dev, "cam_simq_alloc failed\n");
1404 		goto srb_dmamap_unload;
1405 	}
1406 
1407 	hba->sim = cam_sim_alloc(hptiop_action, hptiop_poll, driver_name,
1408 			hba, unit, &sim_mplock, hba->max_requests - 1, 1, devq);
1409 	if (!hba->sim) {
1410 		device_printf(dev, "cam_sim_alloc failed\n");
1411 		cam_simq_release(devq);
1412 		goto srb_dmamap_unload;
1413 	}
1414 	if (xpt_bus_register(hba->sim, 0) != CAM_SUCCESS)
1415 	{
1416 		device_printf(dev, "xpt_bus_register failed\n");
1417 		goto free_cam_sim;
1418 	}
1419 
1420 	if (xpt_create_path(&hba->path, /*periph */ NULL,
1421 			cam_sim_path(hba->sim), CAM_TARGET_WILDCARD,
1422 			CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1423 		device_printf(dev, "xpt_create_path failed\n");
1424 		goto deregister_xpt_bus;
1425 	}
1426 
1427 	bzero(&set_config, sizeof(set_config));
1428 	set_config.iop_id = unit;
1429 	set_config.vbus_id = cam_sim_path(hba->sim);
1430 	set_config.max_host_request_size = HPT_SRB_MAX_REQ_SIZE;
1431 
1432 	if (hba->ops->set_config(hba, &set_config)) {
1433 		device_printf(dev, "set iop config failed.\n");
1434 		goto free_hba_path;
1435 	}
1436 
1437 	xpt_setup_ccb(&ccb.ccb_h, hba->path, /*priority*/5);
1438 	ccb.ccb_h.func_code = XPT_SASYNC_CB;
1439 	ccb.event_enable = (AC_FOUND_DEVICE | AC_LOST_DEVICE);
1440 	ccb.callback = hptiop_async;
1441 	ccb.callback_arg = hba->sim;
1442 	xpt_action((union ccb *)&ccb);
1443 
1444 	rid = 0;
1445 	if ((hba->irq_res = bus_alloc_resource(hba->pcidev, SYS_RES_IRQ,
1446 			&rid, 0, ~0ul, 1, RF_SHAREABLE | RF_ACTIVE)) == NULL) {
1447 		device_printf(dev, "allocate irq failed!\n");
1448 		goto free_hba_path;
1449 	}
1450 
1451 	if (bus_setup_intr(hba->pcidev, hba->irq_res, 0,
1452 				hptiop_pci_intr, hba, &hba->irq_handle, NULL))
1453 	{
1454 		device_printf(dev, "allocate intr function failed!\n");
1455 		goto free_irq_resource;
1456 	}
1457 
1458 	if (hptiop_send_sync_msg(hba,
1459 			IOPMU_INBOUND_MSG0_START_BACKGROUND_TASK, 5000)) {
1460 		device_printf(dev, "fail to start background task\n");
1461 		goto teartown_irq_resource;
1462 	}
1463 
1464 	hba->ops->enable_intr(hba);
1465 
1466 	hba->ioctl_dev = make_dev(&hptiop_ops, unit,
1467 				UID_ROOT, GID_WHEEL /*GID_OPERATOR*/,
1468 				S_IRUSR | S_IWUSR, "%s%d", driver_name, unit);
1469 
1470 	hba->ioctl_dev->si_drv1 = hba;
1471 
1472 	hptiop_rescan_bus(hba);
1473 
1474 	return 0;
1475 
1476 
1477 teartown_irq_resource:
1478 	bus_teardown_intr(dev, hba->irq_res, hba->irq_handle);
1479 
1480 free_irq_resource:
1481 	bus_release_resource(dev, SYS_RES_IRQ, 0, hba->irq_res);
1482 
1483 free_hba_path:
1484 	xpt_free_path(hba->path);
1485 
1486 deregister_xpt_bus:
1487 	xpt_bus_deregister(cam_sim_path(hba->sim));
1488 
1489 free_cam_sim:
1490 	cam_sim_free(hba->sim);
1491 
1492 srb_dmamap_unload:
1493 	if (hba->uncached_ptr)
1494 		bus_dmamap_unload(hba->srb_dmat, hba->srb_dmamap);
1495 
1496 srb_dmamem_free:
1497 	if (hba->uncached_ptr)
1498 		bus_dmamem_free(hba->srb_dmat,
1499 			hba->uncached_ptr, hba->srb_dmamap);
1500 
1501 destroy_srb_dmat:
1502 	if (hba->srb_dmat)
1503 		bus_dma_tag_destroy(hba->srb_dmat);
1504 
1505 destroy_io_dmat:
1506 	if (hba->io_dmat)
1507 		bus_dma_tag_destroy(hba->io_dmat);
1508 
1509 get_config_failed:
1510 	if (hba->ops->internal_memfree)
1511 		hba->ops->internal_memfree(hba);
1512 
1513 destroy_parent_tag:
1514 	if (hba->parent_dmat)
1515 		bus_dma_tag_destroy(hba->parent_dmat);
1516 
1517 release_pci_res:
1518 	if (hba->ops->release_pci_res)
1519 		hba->ops->release_pci_res(hba);
1520 
1521 	return ENXIO;
1522 }
1523 
1524 static int hptiop_detach(device_t dev)
1525 {
1526 	struct hpt_iop_hba * hba = (struct hpt_iop_hba *)device_get_softc(dev);
1527 	int i;
1528 	int error = EBUSY;
1529 
1530 	hptiop_lock_adapter(hba);
1531 	for (i = 0; i < hba->max_devices; i++)
1532 		if (hptiop_os_query_remove_device(hba, i)) {
1533 			device_printf(dev, "file system is busy. id=%d", i);
1534 			goto out;
1535 		}
1536 
1537 	if ((error = hptiop_shutdown(dev)) != 0)
1538 		goto out;
1539 	if (hptiop_send_sync_msg(hba,
1540 		IOPMU_INBOUND_MSG0_STOP_BACKGROUND_TASK, 60000))
1541 		goto out;
1542 
1543 	hptiop_release_resource(hba);
1544 	error = 0;
1545 out:
1546 	hptiop_unlock_adapter(hba);
1547 	return error;
1548 }
1549 
1550 static int hptiop_shutdown(device_t dev)
1551 {
1552 	struct hpt_iop_hba * hba = (struct hpt_iop_hba *)device_get_softc(dev);
1553 
1554 	int error = 0;
1555 
1556 	if (hba->flag & HPT_IOCTL_FLAG_OPEN) {
1557 		device_printf(dev, "device is busy");
1558 		return EBUSY;
1559 	}
1560 
1561 	hba->ops->disable_intr(hba);
1562 
1563 	if (hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_SHUTDOWN, 60000))
1564 		error = EBUSY;
1565 
1566 	return error;
1567 }
1568 
1569 static void hptiop_pci_intr(void *arg)
1570 {
1571 	struct hpt_iop_hba * hba = (struct hpt_iop_hba *)arg;
1572 	hptiop_lock_adapter(hba);
1573 	hba->ops->iop_intr(hba);
1574 	hptiop_unlock_adapter(hba);
1575 }
1576 
1577 static void hptiop_poll(struct cam_sim *sim)
1578 {
1579 	hptiop_pci_intr(cam_sim_softc(sim));
1580 }
1581 
1582 static void hptiop_async(void * callback_arg, u_int32_t code,
1583 					struct cam_path * path, void * arg)
1584 {
1585 }
1586 
1587 static void hptiop_enable_intr_itl(struct hpt_iop_hba *hba)
1588 {
1589 	BUS_SPACE_WRT4_ITL(outbound_intmask,
1590 		~(IOPMU_OUTBOUND_INT_POSTQUEUE | IOPMU_OUTBOUND_INT_MSG0));
1591 }
1592 
1593 static void hptiop_enable_intr_mv(struct hpt_iop_hba *hba)
1594 {
1595 	u_int32_t int_mask;
1596 
1597 	int_mask = BUS_SPACE_RD4_MV0(outbound_intmask);
1598 
1599 	int_mask |= MVIOP_MU_OUTBOUND_INT_POSTQUEUE
1600 			| MVIOP_MU_OUTBOUND_INT_MSG;
1601 	BUS_SPACE_WRT4_MV0(outbound_intmask,int_mask);
1602 }
1603 
1604 static void hptiop_disable_intr_itl(struct hpt_iop_hba *hba)
1605 {
1606 	u_int32_t int_mask;
1607 
1608 	int_mask = BUS_SPACE_RD4_ITL(outbound_intmask);
1609 
1610 	int_mask |= IOPMU_OUTBOUND_INT_POSTQUEUE | IOPMU_OUTBOUND_INT_MSG0;
1611 	BUS_SPACE_WRT4_ITL(outbound_intmask, int_mask);
1612 	BUS_SPACE_RD4_ITL(outbound_intstatus);
1613 }
1614 
1615 static void hptiop_disable_intr_mv(struct hpt_iop_hba *hba)
1616 {
1617 	u_int32_t int_mask;
1618 	int_mask = BUS_SPACE_RD4_MV0(outbound_intmask);
1619 
1620 	int_mask &= ~(MVIOP_MU_OUTBOUND_INT_MSG
1621 			| MVIOP_MU_OUTBOUND_INT_POSTQUEUE);
1622 	BUS_SPACE_WRT4_MV0(outbound_intmask,int_mask);
1623 	BUS_SPACE_RD4_MV0(outbound_intmask);
1624 }
1625 
1626 static int hptiop_reset_adapter(struct hpt_iop_hba * hba)
1627 {
1628 	return hptiop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_RESET, 60000);
1629 }
1630 
1631 static void *hptiop_get_srb(struct hpt_iop_hba * hba)
1632 {
1633 	struct hpt_iop_srb * srb;
1634 
1635 	if (hba->srb_list) {
1636 		srb = hba->srb_list;
1637 		hba->srb_list = srb->next;
1638 		return srb;
1639 	}
1640 
1641 	return NULL;
1642 }
1643 
1644 static void hptiop_free_srb(struct hpt_iop_hba *hba, struct hpt_iop_srb *srb)
1645 {
1646 	srb->next = hba->srb_list;
1647 	hba->srb_list = srb;
1648 }
1649 
1650 static void hptiop_action(struct cam_sim *sim, union ccb *ccb)
1651 {
1652 	struct hpt_iop_hba * hba = (struct hpt_iop_hba *)cam_sim_softc(sim);
1653 	struct hpt_iop_srb * srb;
1654 
1655 	switch (ccb->ccb_h.func_code) {
1656 
1657 	case XPT_SCSI_IO:
1658 		hptiop_lock_adapter(hba);
1659 		if (ccb->ccb_h.target_lun != 0 ||
1660 			ccb->ccb_h.target_id >= hba->max_devices ||
1661 			(ccb->ccb_h.flags & CAM_CDB_PHYS))
1662 		{
1663 			ccb->ccb_h.status = CAM_TID_INVALID;
1664 			xpt_done(ccb);
1665 			goto scsi_done;
1666 		}
1667 
1668 		if ((srb = hptiop_get_srb(hba)) == NULL) {
1669 			device_printf(hba->pcidev, "srb allocated failed");
1670 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1671 			xpt_done(ccb);
1672 			goto scsi_done;
1673 		}
1674 
1675 		srb->ccb = ccb;
1676 
1677 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
1678 			hptiop_post_scsi_command(srb, NULL, 0, 0);
1679 		else if ((ccb->ccb_h.flags & CAM_SCATTER_VALID) == 0) {
1680 			if ((ccb->ccb_h.flags & CAM_DATA_PHYS) == 0) {
1681 				int error;
1682 
1683 				error = bus_dmamap_load(hba->io_dmat,
1684 						srb->dma_map,
1685 						ccb->csio.data_ptr,
1686 						ccb->csio.dxfer_len,
1687 						hptiop_post_scsi_command,
1688 						srb, 0);
1689 
1690 				if (error && error != EINPROGRESS) {
1691 					device_printf(hba->pcidev,
1692 					    "bus_dmamap_load error %d", error);
1693 					xpt_freeze_simq(hba->sim, 1);
1694 					ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1695 invalid:
1696 					hptiop_free_srb(hba, srb);
1697 					xpt_done(ccb);
1698 					goto scsi_done;
1699 				}
1700 			}
1701 			else {
1702 				device_printf(hba->pcidev,
1703 					"CAM_DATA_PHYS not supported");
1704 				ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1705 				goto invalid;
1706 			}
1707 		}
1708 		else {
1709 			struct bus_dma_segment *segs;
1710 
1711 			if ((ccb->ccb_h.flags & CAM_SG_LIST_PHYS) == 0 ||
1712 				(ccb->ccb_h.flags & CAM_DATA_PHYS) != 0) {
1713 				device_printf(hba->pcidev, "SCSI cmd failed");
1714 				ccb->ccb_h.status=CAM_PROVIDE_FAIL;
1715 				goto invalid;
1716 			}
1717 
1718 			segs = (struct bus_dma_segment *)ccb->csio.data_ptr;
1719 			hptiop_post_scsi_command(srb, segs,
1720 						ccb->csio.sglist_cnt, 0);
1721 		}
1722 
1723 scsi_done:
1724 		hptiop_unlock_adapter(hba);
1725 		return;
1726 
1727 	case XPT_RESET_BUS:
1728 		device_printf(hba->pcidev, "reset adapter");
1729 		hptiop_lock_adapter(hba);
1730 		hba->msg_done = 0;
1731 		hptiop_reset_adapter(hba);
1732 		hptiop_unlock_adapter(hba);
1733 		break;
1734 
1735 	case XPT_GET_TRAN_SETTINGS:
1736 	case XPT_SET_TRAN_SETTINGS:
1737 		ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1738 		break;
1739 
1740 	case XPT_CALC_GEOMETRY:
1741 		cam_calc_geometry(&ccb->ccg, 1);
1742 		break;
1743 
1744 	case XPT_PATH_INQ:
1745 	{
1746 		struct ccb_pathinq *cpi = &ccb->cpi;
1747 
1748 		cpi->version_num = 1;
1749 		cpi->hba_inquiry = PI_SDTR_ABLE;
1750 		cpi->target_sprt = 0;
1751 		cpi->hba_misc = PIM_NOBUSRESET;
1752 		cpi->hba_eng_cnt = 0;
1753 		cpi->max_target = hba->max_devices;
1754 		cpi->max_lun = 0;
1755 		cpi->unit_number = cam_sim_unit(sim);
1756 		cpi->bus_id = cam_sim_bus(sim);
1757 		cpi->initiator_id = hba->max_devices;
1758 		cpi->base_transfer_speed = 3300;
1759 
1760 		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1761 		strncpy(cpi->hba_vid, "HPT   ", HBA_IDLEN);
1762 		strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1763 		cpi->transport = XPORT_SPI;
1764 		cpi->transport_version = 2;
1765 		cpi->protocol = PROTO_SCSI;
1766 		cpi->protocol_version = SCSI_REV_2;
1767 		cpi->ccb_h.status = CAM_REQ_CMP;
1768 		break;
1769 	}
1770 
1771 	default:
1772 		ccb->ccb_h.status = CAM_REQ_INVALID;
1773 		break;
1774 	}
1775 
1776 	xpt_done(ccb);
1777 	return;
1778 }
1779 
1780 static void hptiop_post_req_itl(struct hpt_iop_hba *hba,
1781 				struct hpt_iop_srb *srb,
1782 				bus_dma_segment_t *segs, int nsegs)
1783 {
1784 	int idx;
1785 	union ccb *ccb = srb->ccb;
1786 	u_int8_t *cdb;
1787 
1788 	if (ccb->ccb_h.flags & CAM_CDB_POINTER)
1789 		cdb = ccb->csio.cdb_io.cdb_ptr;
1790 	else
1791 		cdb = ccb->csio.cdb_io.cdb_bytes;
1792 
1793 	KdPrint(("ccb=%p %x-%x-%x\n",
1794 		ccb, *(u_int32_t *)cdb, *((u_int32_t *)cdb+1), *((u_int32_t *)cdb+2)));
1795 
1796 	if (srb->srb_flag & HPT_SRB_FLAG_HIGH_MEM_ACESS) {
1797 		u_int32_t iop_req32;
1798 		struct hpt_iop_request_scsi_command req;
1799 
1800 		iop_req32 = BUS_SPACE_RD4_ITL(inbound_queue);
1801 
1802 		if (iop_req32 == IOPMU_QUEUE_EMPTY) {
1803 			device_printf(hba->pcidev, "invaild req offset\n");
1804 			ccb->ccb_h.status = CAM_BUSY;
1805 			bus_dmamap_unload(hba->io_dmat, srb->dma_map);
1806 			hptiop_free_srb(hba, srb);
1807 			xpt_done(ccb);
1808 			return;
1809 		}
1810 
1811 		if (ccb->csio.dxfer_len && nsegs > 0) {
1812 			struct hpt_iopsg *psg = req.sg_list;
1813 			for (idx = 0; idx < nsegs; idx++, psg++) {
1814 				psg->pci_address = (u_int64_t)segs[idx].ds_addr;
1815 				psg->size = segs[idx].ds_len;
1816 				psg->eot = 0;
1817 			}
1818 			psg[-1].eot = 1;
1819 		}
1820 
1821 		bcopy(cdb, req.cdb, ccb->csio.cdb_len);
1822 
1823 		req.header.size = offsetof(struct hpt_iop_request_scsi_command, sg_list)
1824 				+ nsegs*sizeof(struct hpt_iopsg);
1825 		req.header.type = IOP_REQUEST_TYPE_SCSI_COMMAND;
1826 		req.header.flags = 0;
1827 		req.header.result = IOP_RESULT_PENDING;
1828 		req.header.context = (u_int64_t)(unsigned long)srb;
1829 		req.dataxfer_length = ccb->csio.dxfer_len;
1830 		req.channel =  0;
1831 		req.target =  ccb->ccb_h.target_id;
1832 		req.lun =  ccb->ccb_h.target_lun;
1833 
1834 		bus_space_write_region_1(hba->bar0t, hba->bar0h, iop_req32,
1835 			(u_int8_t *)&req, req.header.size);
1836 
1837 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1838 			bus_dmamap_sync(hba->io_dmat,
1839 				srb->dma_map, BUS_DMASYNC_PREREAD);
1840 		}
1841 		else if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
1842 			bus_dmamap_sync(hba->io_dmat,
1843 				srb->dma_map, BUS_DMASYNC_PREWRITE);
1844 
1845 		BUS_SPACE_WRT4_ITL(inbound_queue,iop_req32);
1846 	} else {
1847 		struct hpt_iop_request_scsi_command *req;
1848 
1849 		req = (struct hpt_iop_request_scsi_command *)srb;
1850 		if (ccb->csio.dxfer_len && nsegs > 0) {
1851 			struct hpt_iopsg *psg = req->sg_list;
1852 			for (idx = 0; idx < nsegs; idx++, psg++) {
1853 				psg->pci_address =
1854 					(u_int64_t)segs[idx].ds_addr;
1855 				psg->size = segs[idx].ds_len;
1856 				psg->eot = 0;
1857 			}
1858 			psg[-1].eot = 1;
1859 		}
1860 
1861 		bcopy(cdb, req->cdb, ccb->csio.cdb_len);
1862 
1863 		req->header.type = IOP_REQUEST_TYPE_SCSI_COMMAND;
1864 		req->header.result = IOP_RESULT_PENDING;
1865 		req->dataxfer_length = ccb->csio.dxfer_len;
1866 		req->channel =  0;
1867 		req->target =  ccb->ccb_h.target_id;
1868 		req->lun =  ccb->ccb_h.target_lun;
1869 		req->header.size = offsetof(struct hpt_iop_request_scsi_command, sg_list)
1870 			+ nsegs*sizeof(struct hpt_iopsg);
1871 		req->header.context = (u_int64_t)srb->index |
1872 						IOPMU_QUEUE_ADDR_HOST_BIT;
1873 		req->header.flags = IOP_REQUEST_FLAG_OUTPUT_CONTEXT;
1874 
1875 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1876 			bus_dmamap_sync(hba->io_dmat,
1877 				srb->dma_map, BUS_DMASYNC_PREREAD);
1878 		}else if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
1879 			bus_dmamap_sync(hba->io_dmat,
1880 				srb->dma_map, BUS_DMASYNC_PREWRITE);
1881 		}
1882 
1883 		if (hba->firmware_version > 0x01020000
1884 			|| hba->interface_version > 0x01020000) {
1885 			u_int32_t size_bits;
1886 
1887 			if (req->header.size < 256)
1888 				size_bits = IOPMU_QUEUE_REQUEST_SIZE_BIT;
1889 			else if (req->header.size < 512)
1890 				size_bits = IOPMU_QUEUE_ADDR_HOST_BIT;
1891 			else
1892 				size_bits = IOPMU_QUEUE_REQUEST_SIZE_BIT
1893 						| IOPMU_QUEUE_ADDR_HOST_BIT;
1894 
1895 			BUS_SPACE_WRT4_ITL(inbound_queue,
1896 				(u_int32_t)srb->phy_addr | size_bits);
1897 		} else
1898 			BUS_SPACE_WRT4_ITL(inbound_queue, (u_int32_t)srb->phy_addr
1899 				|IOPMU_QUEUE_ADDR_HOST_BIT);
1900 	}
1901 }
1902 
1903 static void hptiop_post_req_mv(struct hpt_iop_hba *hba,
1904 				struct hpt_iop_srb *srb,
1905 				bus_dma_segment_t *segs, int nsegs)
1906 {
1907 	int idx, size;
1908 	union ccb *ccb = srb->ccb;
1909 	u_int8_t *cdb;
1910 	struct hpt_iop_request_scsi_command *req;
1911 	u_int64_t req_phy;
1912 
1913 	req = (struct hpt_iop_request_scsi_command *)srb;
1914 	req_phy = srb->phy_addr;
1915 
1916 	if (ccb->csio.dxfer_len && nsegs > 0) {
1917 		struct hpt_iopsg *psg = req->sg_list;
1918 		for (idx = 0; idx < nsegs; idx++, psg++) {
1919 			psg->pci_address = (u_int64_t)segs[idx].ds_addr;
1920 			psg->size = segs[idx].ds_len;
1921 			psg->eot = 0;
1922 		}
1923 		psg[-1].eot = 1;
1924 	}
1925 	if (ccb->ccb_h.flags & CAM_CDB_POINTER)
1926 		cdb = ccb->csio.cdb_io.cdb_ptr;
1927 	else
1928 		cdb = ccb->csio.cdb_io.cdb_bytes;
1929 
1930 	bcopy(cdb, req->cdb, ccb->csio.cdb_len);
1931 	req->header.type = IOP_REQUEST_TYPE_SCSI_COMMAND;
1932 	req->header.result = IOP_RESULT_PENDING;
1933 	req->dataxfer_length = ccb->csio.dxfer_len;
1934 	req->channel = 0;
1935 	req->target =  ccb->ccb_h.target_id;
1936 	req->lun =  ccb->ccb_h.target_lun;
1937 	req->header.size = sizeof(struct hpt_iop_request_scsi_command)
1938 				- sizeof(struct hpt_iopsg)
1939 				+ nsegs * sizeof(struct hpt_iopsg);
1940 	if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1941 		bus_dmamap_sync(hba->io_dmat,
1942 			srb->dma_map, BUS_DMASYNC_PREREAD);
1943 	}
1944 	else if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
1945 		bus_dmamap_sync(hba->io_dmat,
1946 			srb->dma_map, BUS_DMASYNC_PREWRITE);
1947 	req->header.context = (u_int64_t)srb->index
1948 					<< MVIOP_REQUEST_NUMBER_START_BIT
1949 					| MVIOP_CMD_TYPE_SCSI;
1950 	req->header.flags = IOP_REQUEST_FLAG_OUTPUT_CONTEXT;
1951 	size = req->header.size >> 8;
1952 	hptiop_mv_inbound_write(req_phy
1953 			| MVIOP_MU_QUEUE_ADDR_HOST_BIT
1954 			| (size > 3 ? 3 : size), hba);
1955 }
1956 
1957 static void hptiop_post_scsi_command(void *arg, bus_dma_segment_t *segs,
1958 					int nsegs, int error)
1959 {
1960 	struct hpt_iop_srb *srb = (struct hpt_iop_srb *)arg;
1961 	union ccb *ccb = srb->ccb;
1962 	struct hpt_iop_hba *hba = srb->hba;
1963 
1964 	if (error || nsegs > hba->max_sg_count) {
1965 		KdPrint(("hptiop: func_code=%x tid=%x lun=%x nsegs=%d\n",
1966 			ccb->ccb_h.func_code,
1967 			ccb->ccb_h.target_id,
1968 			ccb->ccb_h.target_lun, nsegs));
1969 		ccb->ccb_h.status = CAM_BUSY;
1970 		bus_dmamap_unload(hba->io_dmat, srb->dma_map);
1971 		hptiop_free_srb(hba, srb);
1972 		xpt_done(ccb);
1973 		return;
1974 	}
1975 
1976 	hba->ops->post_req(hba, srb, segs, nsegs);
1977 }
1978 
1979 static void hptiop_mv_map_ctlcfg(void *arg, bus_dma_segment_t *segs,
1980 				int nsegs, int error)
1981 {
1982 	struct hpt_iop_hba *hba = (struct hpt_iop_hba *)arg;
1983 	hba->ctlcfgcmd_phy = ((u_int64_t)segs->ds_addr + 0x1F)
1984 				& ~(u_int64_t)0x1F;
1985 	hba->ctlcfg_ptr = (u_int8_t *)(((unsigned long)hba->ctlcfg_ptr + 0x1F)
1986 				& ~0x1F);
1987 }
1988 
1989 static void hptiop_map_srb(void *arg, bus_dma_segment_t *segs,
1990 				int nsegs, int error)
1991 {
1992 	struct hpt_iop_hba * hba = (struct hpt_iop_hba *)arg;
1993 	bus_addr_t phy_addr = (segs->ds_addr + 0x1F) & ~(bus_addr_t)0x1F;
1994 	struct hpt_iop_srb *srb, *tmp_srb;
1995 	int i;
1996 
1997 	if (error || nsegs == 0) {
1998 		device_printf(hba->pcidev, "hptiop_map_srb error");
1999 		return;
2000 	}
2001 
2002 	/* map srb */
2003 	srb = (struct hpt_iop_srb *)
2004 		(((unsigned long)hba->uncached_ptr + 0x1F)
2005 		& ~(unsigned long)0x1F);
2006 
2007 	for (i = 0; i < HPT_SRB_MAX_QUEUE_SIZE; i++) {
2008 		tmp_srb = (struct hpt_iop_srb *)
2009 					((char *)srb + i * HPT_SRB_MAX_SIZE);
2010 		if (((unsigned long)tmp_srb & 0x1F) == 0) {
2011 			if (bus_dmamap_create(hba->io_dmat,
2012 						0, &tmp_srb->dma_map)) {
2013 				device_printf(hba->pcidev, "dmamap create failed");
2014 				return;
2015 			}
2016 
2017 			bzero(tmp_srb, sizeof(struct hpt_iop_srb));
2018 			tmp_srb->hba = hba;
2019 			tmp_srb->index = i;
2020 			if (hba->ctlcfg_ptr == 0) {/*itl iop*/
2021 				tmp_srb->phy_addr = (u_int64_t)(u_int32_t)
2022 							(phy_addr >> 5);
2023 				if (phy_addr & IOPMU_MAX_MEM_SUPPORT_MASK_32G)
2024 					tmp_srb->srb_flag =
2025 						HPT_SRB_FLAG_HIGH_MEM_ACESS;
2026 			} else {
2027 				tmp_srb->phy_addr = phy_addr;
2028 			}
2029 
2030 			hptiop_free_srb(hba, tmp_srb);
2031 			hba->srb[i] = tmp_srb;
2032 			phy_addr += HPT_SRB_MAX_SIZE;
2033 		}
2034 		else {
2035 			device_printf(hba->pcidev, "invalid alignment");
2036 			return;
2037 		}
2038 	}
2039 }
2040 
2041 static void hptiop_os_message_callback(struct hpt_iop_hba * hba, u_int32_t msg)
2042 {
2043 		hba->msg_done = 1;
2044 }
2045 
2046 static  int hptiop_os_query_remove_device(struct hpt_iop_hba * hba,
2047 						int target_id)
2048 {
2049 	struct cam_periph       *periph = NULL;
2050 	struct cam_path         *path;
2051 	int                     status, retval = 0;
2052 
2053 	status = xpt_create_path(&path, NULL, hba->sim->path_id, target_id, 0);
2054 
2055 	if (status == CAM_REQ_CMP) {
2056 		if ((periph = cam_periph_find(path, "da")) != NULL) {
2057 			if (periph->refcount >= 1) {
2058 				device_printf(hba->pcidev, "target_id=0x%x,"
2059 				    "refcount=%d", target_id, periph->refcount);
2060 				retval = -1;
2061 			}
2062 		}
2063 		xpt_free_path(path);
2064 	}
2065 	return retval;
2066 }
2067 
2068 static void hptiop_release_resource(struct hpt_iop_hba *hba)
2069 {
2070 	int i;
2071 	if (hba->path) {
2072 		struct ccb_setasync ccb;
2073 
2074 		xpt_setup_ccb(&ccb.ccb_h, hba->path, /*priority*/5);
2075 		ccb.ccb_h.func_code = XPT_SASYNC_CB;
2076 		ccb.event_enable = 0;
2077 		ccb.callback = hptiop_async;
2078 		ccb.callback_arg = hba->sim;
2079 		xpt_action((union ccb *)&ccb);
2080 		xpt_free_path(hba->path);
2081 	}
2082 
2083 	if (hba->sim) {
2084 		xpt_bus_deregister(cam_sim_path(hba->sim));
2085 		cam_sim_free(hba->sim);
2086 	}
2087 
2088 	if (hba->ctlcfg_dmat) {
2089 		bus_dmamap_unload(hba->ctlcfg_dmat, hba->ctlcfg_dmamap);
2090 		bus_dmamem_free(hba->ctlcfg_dmat,
2091 					hba->ctlcfg_ptr, hba->ctlcfg_dmamap);
2092 		bus_dma_tag_destroy(hba->ctlcfg_dmat);
2093 	}
2094 
2095 	for (i = 0; i < HPT_SRB_MAX_QUEUE_SIZE; i++) {
2096 		struct hpt_iop_srb *srb = hba->srb[i];
2097 		if (srb->dma_map)
2098 			bus_dmamap_destroy(hba->io_dmat, srb->dma_map);
2099 	}
2100 
2101 	if (hba->srb_dmat) {
2102 		bus_dmamap_unload(hba->srb_dmat, hba->srb_dmamap);
2103 		bus_dmamap_destroy(hba->srb_dmat, hba->srb_dmamap);
2104 		bus_dma_tag_destroy(hba->srb_dmat);
2105 	}
2106 
2107 	if (hba->io_dmat)
2108 		bus_dma_tag_destroy(hba->io_dmat);
2109 
2110 	if (hba->parent_dmat)
2111 		bus_dma_tag_destroy(hba->parent_dmat);
2112 
2113 	if (hba->irq_handle)
2114 		bus_teardown_intr(hba->pcidev, hba->irq_res, hba->irq_handle);
2115 
2116 	if (hba->irq_res)
2117 		bus_release_resource(hba->pcidev, SYS_RES_IRQ,
2118 					0, hba->irq_res);
2119 
2120 	if (hba->bar0_res)
2121 		bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
2122 					hba->bar0_rid, hba->bar0_res);
2123 	if (hba->bar2_res)
2124 		bus_release_resource(hba->pcidev, SYS_RES_MEMORY,
2125 					hba->bar2_rid, hba->bar2_res);
2126 	if (hba->ioctl_dev)
2127 		destroy_dev(hba->ioctl_dev);
2128 	dev_ops_remove_minor(&hptiop_ops, device_get_unit(hba->pcidev));
2129 }
2130