xref: /dragonfly/sys/dev/raid/hptrr/hptrr_osm_bsd.c (revision 7d84b73d)
1 /*
2  * Copyright (c) HighPoint Technologies, Inc.
3  * 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: head/sys/dev/hptrr/hptrr_osm_bsd.c 313554 2017-02-10 15:18:41Z pfg $
27  */
28 
29 #include <dev/raid/hptrr/hptrr_config.h>
30 /* $Id: osm_bsd.c,v 1.27 2007/11/22 07:35:49 gmm Exp $
31  *
32  * HighPoint RAID Driver for FreeBSD
33  * Copyright (C) 2005 HighPoint Technologies, Inc. All Rights Reserved.
34  */
35 #include <dev/raid/hptrr/os_bsd.h>
36 #include <dev/raid/hptrr/hptintf.h>
37 
38 static int attach_generic = 0;
39 TUNABLE_INT("hw.hptrr.attach_generic", &attach_generic);
40 
41 static HIM *hpt_match(device_t dev)
42 {
43 	PCI_ID pci_id;
44 	int i;
45 	HIM *him;
46 
47 	/* Some of supported chips are used not only by HPT. */
48 	if (pci_get_vendor(dev) != 0x1103 && !attach_generic)
49 		return (NULL);
50 	for (him = him_list; him; him = him->next) {
51 		for (i=0; him->get_supported_device_id(i, &pci_id); i++) {
52 			if ((pci_get_vendor(dev) == pci_id.vid) &&
53 				(pci_get_device(dev) == pci_id.did)){
54 				return (him);
55 			}
56 		}
57 	}
58 	return (NULL);
59 }
60 
61 static int hpt_probe(device_t dev)
62 {
63 	HIM *him;
64 
65 	him = hpt_match(dev);
66 	if (him != NULL) {
67 		KdPrint(("hpt_probe: adapter at PCI %d:%d:%d, IRQ %d",
68 			pci_get_bus(dev), pci_get_slot(dev), pci_get_function(dev), pci_get_irq(dev)
69 			    ));
70 		device_set_desc(dev, him->name);
71 		return (BUS_PROBE_DEFAULT);
72 	}
73 
74 	return (ENXIO);
75 }
76 
77 static int hpt_attach(device_t dev)
78 {
79 	PHBA hba = (PHBA)device_get_softc(dev);
80 	HIM *him;
81 	PCI_ID pci_id;
82 	HPT_UINT size;
83 	PVBUS vbus;
84 	PVBUS_EXT vbus_ext;
85 
86 	KdPrint(("hpt_attach(%d/%d/%d)", pci_get_bus(dev), pci_get_slot(dev), pci_get_function(dev)));
87 
88 	him = hpt_match(dev);
89 	hba->ext_type = EXT_TYPE_HBA;
90 	hba->ldm_adapter.him = him;
91 
92 	pci_enable_busmaster(dev);
93 
94 	pci_id.vid = pci_get_vendor(dev);
95 	pci_id.did = pci_get_device(dev);
96 	pci_id.rev = pci_get_revid(dev);
97 
98 	size = him->get_adapter_size(&pci_id);
99 	hba->ldm_adapter.him_handle = kmalloc(size, M_DEVBUF, M_WAITOK);
100 
101 	hba->pcidev = dev;
102 	hba->pciaddr.tree = 0;
103 	hba->pciaddr.bus = pci_get_bus(dev);
104 	hba->pciaddr.device = pci_get_slot(dev);
105 	hba->pciaddr.function = pci_get_function(dev);
106 
107 	if (!him->create_adapter(&pci_id, hba->pciaddr, hba->ldm_adapter.him_handle, hba)) {
108 		kfree(hba->ldm_adapter.him_handle, M_DEVBUF);
109 		return ENXIO;
110 	}
111 
112 	os_printk("adapter at PCI %d:%d:%d, IRQ %d",
113 		hba->pciaddr.bus, hba->pciaddr.device, hba->pciaddr.function, pci_get_irq(dev));
114 
115 	if (!ldm_register_adapter(&hba->ldm_adapter)) {
116 		size = ldm_get_vbus_size();
117 		vbus_ext = kmalloc(sizeof(VBUS_EXT) + size, M_DEVBUF, M_WAITOK |
118 		    M_ZERO);
119 		vbus_ext->ext_type = EXT_TYPE_VBUS;
120 		ldm_create_vbus((PVBUS)vbus_ext->vbus, vbus_ext);
121 		ldm_register_adapter(&hba->ldm_adapter);
122 	}
123 
124 	ldm_for_each_vbus(vbus, vbus_ext) {
125 		if (hba->ldm_adapter.vbus==vbus) {
126 			hba->vbus_ext = vbus_ext;
127 			hba->next = vbus_ext->hba_list;
128 			vbus_ext->hba_list = hba;
129 			break;
130 		}
131 	}
132 	return 0;
133 }
134 
135 /*
136  * Maybe we'd better to use the bus_dmamem_alloc to alloc DMA memory,
137  * but there are some problems currently (alignment, etc).
138  */
139 static __inline void *__get_free_pages(int order)
140 {
141 	/* don't use low memory - other devices may get starved */
142 	return contigmalloc(PAGE_SIZE<<order,
143 			M_DEVBUF, M_WAITOK, BUS_SPACE_MAXADDR_24BIT, BUS_SPACE_MAXADDR, PAGE_SIZE, 0);
144 }
145 
146 static __inline void free_pages(void *p, int order)
147 {
148 	contigfree(p, PAGE_SIZE<<order, M_DEVBUF);
149 }
150 
151 static int hpt_alloc_mem(PVBUS_EXT vbus_ext)
152 {
153 	PHBA hba;
154 	struct freelist *f;
155 	HPT_UINT i;
156 	void **p;
157 
158 	for (hba = vbus_ext->hba_list; hba; hba = hba->next)
159 		hba->ldm_adapter.him->get_meminfo(hba->ldm_adapter.him_handle);
160 
161 	ldm_get_mem_info((PVBUS)vbus_ext->vbus, 0);
162 
163 	for (f=vbus_ext->freelist_head; f; f=f->next) {
164 		KdPrint(("%s: %d*%d=%d bytes",
165 			f->tag, f->count, f->size, f->count*f->size));
166 		for (i=0; i<f->count; i++) {
167 			p = (void **)kmalloc(f->size, M_DEVBUF, M_WAITOK);
168 			if (!p)	return (ENXIO);
169 			*p = f->head;
170 			f->head = p;
171 		}
172 	}
173 
174 	for (f=vbus_ext->freelist_dma_head; f; f=f->next) {
175 		int order, size, j;
176 
177 		HPT_ASSERT((f->size & (f->alignment-1))==0);
178 
179 		for (order=0, size=PAGE_SIZE; size<f->size; order++, size<<=1)
180 			;
181 
182 		KdPrint(("%s: %d*%d=%d bytes, order %d",
183 			f->tag, f->count, f->size, f->count*f->size, order));
184 		HPT_ASSERT(f->alignment<=PAGE_SIZE);
185 
186 		for (i=0; i<f->count;) {
187 			p = (void **)__get_free_pages(order);
188 			if (!p) return -1;
189 			for (j = size/f->size; j && i<f->count; i++,j--) {
190 				*p = f->head;
191 				*(BUS_ADDRESS *)(p+1) = (BUS_ADDRESS)vtophys(p);
192 				f->head = p;
193 				p = (void **)((unsigned long)p + f->size);
194 			}
195 		}
196 	}
197 
198 	HPT_ASSERT(PAGE_SIZE==DMAPOOL_PAGE_SIZE);
199 
200 	for (i=0; i<os_max_cache_pages; i++) {
201 		p = (void **)__get_free_pages(0);
202 		if (!p) return -1;
203 		HPT_ASSERT(((HPT_UPTR)p & (DMAPOOL_PAGE_SIZE-1))==0);
204 		dmapool_put_page((PVBUS)vbus_ext->vbus, p, (BUS_ADDRESS)vtophys(p));
205 	}
206 
207 	return 0;
208 }
209 
210 static void hpt_free_mem(PVBUS_EXT vbus_ext)
211 {
212 	struct freelist *f;
213 	void *p;
214 	int i;
215 	BUS_ADDRESS bus;
216 
217 	for (f=vbus_ext->freelist_head; f; f=f->next) {
218 #if DBG
219 		if (f->count!=f->reserved_count) {
220 			KdPrint(("memory leak for freelist %s (%d/%d)", f->tag, f->count, f->reserved_count));
221 		}
222 #endif
223 		while ((p=freelist_get(f)))
224 			kfree(p, M_DEVBUF);
225 	}
226 
227 	for (i=0; i<os_max_cache_pages; i++) {
228 		p = dmapool_get_page((PVBUS)vbus_ext->vbus, &bus);
229 		HPT_ASSERT(p);
230 		free_pages(p, 0);
231 	}
232 
233 	for (f=vbus_ext->freelist_dma_head; f; f=f->next) {
234 		int order, size;
235 #if DBG
236 		if (f->count!=f->reserved_count) {
237 			KdPrint(("memory leak for dma freelist %s (%d/%d)", f->tag, f->count, f->reserved_count));
238 		}
239 #endif
240 		for (order=0, size=PAGE_SIZE; size<f->size; order++, size<<=1) ;
241 
242 		while ((p=freelist_get_dma(f, &bus))) {
243 			if (order)
244 				free_pages(p, order);
245 			else {
246 			/* can't free immediately since other blocks in this page may still be in the list */
247 				if (((HPT_UPTR)p & (PAGE_SIZE-1))==0)
248 					dmapool_put_page((PVBUS)vbus_ext->vbus, p, bus);
249 			}
250 		}
251 	}
252 
253 	while ((p = dmapool_get_page((PVBUS)vbus_ext->vbus, &bus)))
254 		free_pages(p, 0);
255 }
256 
257 static int hpt_init_vbus(PVBUS_EXT vbus_ext)
258 {
259 	PHBA hba;
260 
261 	for (hba = vbus_ext->hba_list; hba; hba = hba->next)
262 		if (!hba->ldm_adapter.him->initialize(hba->ldm_adapter.him_handle)) {
263 			KdPrint(("fail to initialize %p", hba));
264 			return -1;
265 		}
266 
267 	ldm_initialize_vbus((PVBUS)vbus_ext->vbus, &vbus_ext->hba_list->ldm_adapter);
268 	return 0;
269 }
270 
271 static void hpt_flush_done(PCOMMAND pCmd)
272 {
273 	PVDEV vd = pCmd->target;
274 
275 	if (mIsArray(vd->type) && vd->u.array.transform && vd!=vd->u.array.transform->target) {
276 		vd = vd->u.array.transform->target;
277 		HPT_ASSERT(vd);
278 		pCmd->target = vd;
279 		pCmd->Result = RETURN_PENDING;
280 		vdev_queue_cmd(pCmd);
281 		return;
282 	}
283 
284 	*(int *)pCmd->priv = 1;
285 	wakeup(pCmd);
286 }
287 
288 /*
289  * flush a vdev (without retry).
290  */
291 static int hpt_flush_vdev(PVBUS_EXT vbus_ext, PVDEV vd)
292 {
293 	PCOMMAND pCmd;
294 	int result = 0, done;
295 	HPT_UINT count;
296 
297 	KdPrint(("flusing dev %p", vd));
298 
299 	hpt_assert_vbus_locked(vbus_ext);
300 
301 	if (mIsArray(vd->type) && vd->u.array.transform)
302 		count = max(vd->u.array.transform->source->cmds_per_request,
303 					vd->u.array.transform->target->cmds_per_request);
304 	else
305 		count = vd->cmds_per_request;
306 
307 	pCmd = ldm_alloc_cmds(vd->vbus, count);
308 
309 	if (!pCmd) {
310 		return -1;
311 	}
312 
313 	pCmd->type = CMD_TYPE_FLUSH;
314 	pCmd->flags.hard_flush = 1;
315 	pCmd->target = vd;
316 	pCmd->done = hpt_flush_done;
317 	done = 0;
318 	pCmd->priv = &done;
319 
320 	ldm_queue_cmd(pCmd);
321 
322 	if (!done) {
323 		while (hpt_sleep(vbus_ext, pCmd, 0, "hptfls", HPT_OSM_TIMEOUT)) {
324 			ldm_reset_vbus(vd->vbus);
325 		}
326 	}
327 
328 	KdPrint(("flush result %d", pCmd->Result));
329 
330 	if (pCmd->Result!=RETURN_SUCCESS)
331 		result = -1;
332 
333 	ldm_free_cmds(pCmd);
334 
335 	return result;
336 }
337 
338 static void hpt_stop_tasks(PVBUS_EXT vbus_ext);
339 static void hpt_shutdown_vbus(PVBUS_EXT vbus_ext, int howto)
340 {
341 	PVBUS     vbus = (PVBUS)vbus_ext->vbus;
342 	PHBA hba;
343 	int i;
344 
345 	KdPrint(("hpt_shutdown_vbus"));
346 
347 	/* stop all ctl tasks and disable the worker taskqueue */
348 	hpt_stop_tasks(vbus_ext);
349 	hpt_lock_vbus(vbus_ext);
350 	vbus_ext->worker.ta_context = NULL;
351 
352 	/* flush devices */
353 	for (i=0; i<osm_max_targets; i++) {
354 		PVDEV vd = ldm_find_target(vbus, i);
355 		if (vd) {
356 			/* retry once */
357 			if (hpt_flush_vdev(vbus_ext, vd))
358 				hpt_flush_vdev(vbus_ext, vd);
359 		}
360 	}
361 
362 	ldm_shutdown(vbus);
363 	hpt_unlock_vbus(vbus_ext);
364 
365 	ldm_release_vbus(vbus);
366 
367 	for (hba=vbus_ext->hba_list; hba; hba=hba->next)
368 		bus_teardown_intr(hba->pcidev, hba->irq_res, hba->irq_handle);
369 
370 	hpt_free_mem(vbus_ext);
371 
372 	while ((hba=vbus_ext->hba_list)) {
373 		vbus_ext->hba_list = hba->next;
374 		kfree(hba->ldm_adapter.him_handle, M_DEVBUF);
375 	}
376 
377 	callout_terminate(&vbus_ext->timer);
378 	lockuninit(&vbus_ext->lock);
379 	kfree(vbus_ext, M_DEVBUF);
380 	KdPrint(("hpt_shutdown_vbus done"));
381 }
382 
383 static void __hpt_do_tasks(PVBUS_EXT vbus_ext)
384 {
385 	OSM_TASK *tasks;
386 
387 	tasks = vbus_ext->tasks;
388 	vbus_ext->tasks = NULL;
389 
390 	while (tasks) {
391 		OSM_TASK *t = tasks;
392 		tasks = t->next;
393 		t->next = NULL;
394 		t->func(vbus_ext->vbus, t->data);
395 	}
396 }
397 
398 static void hpt_do_tasks(PVBUS_EXT vbus_ext, int pending)
399 {
400 	if(vbus_ext){
401 		hpt_lock_vbus(vbus_ext);
402 		__hpt_do_tasks(vbus_ext);
403 		hpt_unlock_vbus(vbus_ext);
404 	}
405 }
406 
407 static void hpt_action(struct cam_sim *sim, union ccb *ccb);
408 static void hpt_poll(struct cam_sim *sim);
409 static void hpt_async(void * callback_arg, u_int32_t code, struct cam_path * path, void * arg);
410 static void hpt_pci_intr(void *arg);
411 
412 static __inline POS_CMDEXT cmdext_get(PVBUS_EXT vbus_ext)
413 {
414 	POS_CMDEXT p = vbus_ext->cmdext_list;
415 	if (p)
416 		vbus_ext->cmdext_list = p->next;
417 	return p;
418 }
419 
420 static __inline void cmdext_put(POS_CMDEXT p)
421 {
422 	p->next = p->vbus_ext->cmdext_list;
423 	p->vbus_ext->cmdext_list = p;
424 }
425 
426 static void hpt_timeout(void *arg)
427 {
428 	PCOMMAND pCmd = (PCOMMAND)arg;
429 	POS_CMDEXT ext = (POS_CMDEXT)pCmd->priv;
430 
431 	KdPrint(("pCmd %p timeout", pCmd));
432 
433 	ldm_reset_vbus((PVBUS)ext->vbus_ext->vbus);
434 }
435 
436 static void os_cmddone(PCOMMAND pCmd)
437 {
438 	POS_CMDEXT ext = (POS_CMDEXT)pCmd->priv;
439 	union ccb *ccb = ext->ccb;
440 
441 	KdPrint(("os_cmddone(%p, %d)", pCmd, pCmd->Result));
442 
443 	callout_stop(&ext->timeout);
444 
445 	switch(pCmd->Result) {
446 	case RETURN_SUCCESS:
447 		ccb->ccb_h.status = CAM_REQ_CMP;
448 		break;
449 	case RETURN_BAD_DEVICE:
450 		ccb->ccb_h.status = CAM_DEV_NOT_THERE;
451 		break;
452 	case RETURN_DEVICE_BUSY:
453 		ccb->ccb_h.status = CAM_BUSY;
454 		break;
455 	case RETURN_INVALID_REQUEST:
456 		ccb->ccb_h.status = CAM_REQ_INVALID;
457 		break;
458 	case RETURN_SELECTION_TIMEOUT:
459 		ccb->ccb_h.status = CAM_SEL_TIMEOUT;
460 		break;
461 	case RETURN_RETRY:
462 		ccb->ccb_h.status = CAM_BUSY;
463 		break;
464 	default:
465 		ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
466 		break;
467 	}
468 
469 	if (pCmd->flags.data_in) {
470 		bus_dmamap_sync(ext->vbus_ext->io_dmat, ext->dma_map, BUS_DMASYNC_POSTREAD);
471 	}
472 	else if (pCmd->flags.data_out) {
473 		bus_dmamap_sync(ext->vbus_ext->io_dmat, ext->dma_map, BUS_DMASYNC_POSTWRITE);
474 	}
475 
476 	bus_dmamap_unload(ext->vbus_ext->io_dmat, ext->dma_map);
477 
478 	cmdext_put(ext);
479 	ldm_free_cmds(pCmd);
480 	xpt_done(ccb);
481 }
482 
483 static int os_buildsgl(PCOMMAND pCmd, PSG pSg, int logical)
484 {
485 	POS_CMDEXT ext = (POS_CMDEXT)pCmd->priv;
486 	union ccb *ccb = ext->ccb;
487 
488 	if(logical)	{
489 		os_set_sgptr(pSg, (HPT_U8 *)ccb->csio.data_ptr);
490 		pSg->size = ccb->csio.dxfer_len;
491 		pSg->eot = 1;
492 		return TRUE;
493 	}
494 
495 	/* since we have provided physical sg, nobody will ask us to build physical sg */
496 	HPT_ASSERT(0);
497 	return FALSE;
498 }
499 
500 static void hpt_io_dmamap_callback(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
501 {
502 	PCOMMAND pCmd = (PCOMMAND)arg;
503 	POS_CMDEXT ext = (POS_CMDEXT)pCmd->priv;
504 	PSG psg = pCmd->psg;
505 	int idx;
506 
507 	HPT_ASSERT(pCmd->flags.physical_sg);
508 
509 	if (error)
510 		panic("busdma error");
511 
512 	HPT_ASSERT(nsegs<=os_max_sg_descriptors);
513 
514 	if (nsegs != 0) {
515 		for (idx = 0; idx < nsegs; idx++, psg++) {
516 			psg->addr.bus = segs[idx].ds_addr;
517 			psg->size = segs[idx].ds_len;
518 			psg->eot = 0;
519 		}
520 			psg[-1].eot = 1;
521 
522 		if (pCmd->flags.data_in) {
523 			bus_dmamap_sync(ext->vbus_ext->io_dmat, ext->dma_map,
524 			    BUS_DMASYNC_PREREAD);
525 		}
526 		else if (pCmd->flags.data_out) {
527 			bus_dmamap_sync(ext->vbus_ext->io_dmat, ext->dma_map,
528 			    BUS_DMASYNC_PREWRITE);
529 		}
530 	}
531 	callout_reset(&ext->timeout, HPT_OSM_TIMEOUT, hpt_timeout, pCmd);
532 	ldm_queue_cmd(pCmd);
533 }
534 
535 static void hpt_scsi_io(PVBUS_EXT vbus_ext, union ccb *ccb)
536 {
537 	PVBUS vbus = (PVBUS)vbus_ext->vbus;
538 	PVDEV vd;
539 	PCOMMAND pCmd;
540 	POS_CMDEXT ext;
541 	HPT_U8 *cdb;
542 
543 	if (ccb->ccb_h.flags & CAM_CDB_POINTER)
544 		cdb = ccb->csio.cdb_io.cdb_ptr;
545 	else
546 		cdb = ccb->csio.cdb_io.cdb_bytes;
547 
548 	KdPrint(("hpt_scsi_io: ccb %x id %d lun %d cdb %x-%x-%x",
549 		ccb,
550 		ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
551 		*(HPT_U32 *)&cdb[0], *(HPT_U32 *)&cdb[4], *(HPT_U32 *)&cdb[8]
552 	));
553 
554 	/* ccb->ccb_h.path_id is not our bus id - don't check it */
555 	if (ccb->ccb_h.target_lun != 0 ||
556 		ccb->ccb_h.target_id >= osm_max_targets ||
557 		(ccb->ccb_h.flags & CAM_CDB_PHYS))
558 	{
559 		ccb->ccb_h.status = CAM_TID_INVALID;
560 		xpt_done(ccb);
561 		return;
562 	}
563 
564 	vd = ldm_find_target(vbus, ccb->ccb_h.target_id);
565 
566 	if (!vd) {
567 		ccb->ccb_h.status = CAM_SEL_TIMEOUT;
568 		xpt_done(ccb);
569 		return;
570 	}
571 
572 	switch (cdb[0]) {
573 	case TEST_UNIT_READY:
574 	case START_STOP_UNIT:
575 	case SYNCHRONIZE_CACHE:
576 		ccb->ccb_h.status = CAM_REQ_CMP;
577 		break;
578 
579 	case INQUIRY:
580 		{
581 			PINQUIRYDATA inquiryData;
582 			memset(ccb->csio.data_ptr, 0, ccb->csio.dxfer_len);
583 			inquiryData = (PINQUIRYDATA)ccb->csio.data_ptr;
584 
585 			inquiryData->AdditionalLength = 31;
586 			inquiryData->CommandQueue = 1;
587 			memcpy(&inquiryData->VendorId, "HPT     ", 8);
588 			memcpy(&inquiryData->ProductId, "DISK 0_0        ", 16);
589 
590 			if (vd->target_id / 10) {
591 				inquiryData->ProductId[7] = (vd->target_id % 100) / 10 + '0';
592 				inquiryData->ProductId[8] = (vd->target_id % 100) % 10 + '0';
593 			}
594 			else
595 				inquiryData->ProductId[7] = (vd->target_id % 100) % 10 + '0';
596 
597 			memcpy(&inquiryData->ProductRevisionLevel, "4.00", 4);
598 
599 			ccb->ccb_h.status = CAM_REQ_CMP;
600 		}
601 		break;
602 
603 	case READ_CAPACITY:
604 	{
605 		HPT_U8 *rbuf = ccb->csio.data_ptr;
606 		HPT_U32 cap;
607 
608 		if (vd->capacity>0xfffffffful)
609 			cap = 0xfffffffful;
610 		else
611 			cap = vd->capacity - 1;
612 
613 		rbuf[0] = (HPT_U8)(cap>>24);
614 		rbuf[1] = (HPT_U8)(cap>>16);
615 		rbuf[2] = (HPT_U8)(cap>>8);
616 		rbuf[3] = (HPT_U8)cap;
617 		rbuf[4] = 0;
618 		rbuf[5] = 0;
619 		rbuf[6] = 2;
620 		rbuf[7] = 0;
621 
622 		ccb->ccb_h.status = CAM_REQ_CMP;
623 		break;
624 	}
625 
626 	case SERVICE_ACTION_IN:
627 	{
628 		HPT_U8 *rbuf = ccb->csio.data_ptr;
629 		HPT_U64	cap = vd->capacity - 1;
630 
631 		rbuf[0] = (HPT_U8)(cap>>56);
632 		rbuf[1] = (HPT_U8)(cap>>48);
633 		rbuf[2] = (HPT_U8)(cap>>40);
634 		rbuf[3] = (HPT_U8)(cap>>32);
635 		rbuf[4] = (HPT_U8)(cap>>24);
636 		rbuf[5] = (HPT_U8)(cap>>16);
637 		rbuf[6] = (HPT_U8)(cap>>8);
638 		rbuf[7] = (HPT_U8)cap;
639 		rbuf[8] = 0;
640 		rbuf[9] = 0;
641 		rbuf[10] = 2;
642 		rbuf[11] = 0;
643 
644 		ccb->ccb_h.status = CAM_REQ_CMP;
645 		break;
646 	}
647 
648 	case READ_6:
649 	case READ_10:
650 	case READ_16:
651 	case WRITE_6:
652 	case WRITE_10:
653 	case WRITE_16:
654 	case 0x13:
655 	case 0x2f:
656 	{
657 		int error;
658 
659 		pCmd = ldm_alloc_cmds(vbus, vd->cmds_per_request);
660 		if(!pCmd){
661 			KdPrint(("Failed to allocate command!"));
662 			ccb->ccb_h.status = CAM_BUSY;
663 			break;
664 		}
665 
666 		switch (cdb[0])	{
667 		case READ_6:
668 		case WRITE_6:
669 		case 0x13:
670 			pCmd->uCmd.Ide.Lba =  ((HPT_U32)cdb[1] << 16) | ((HPT_U32)cdb[2] << 8) | (HPT_U32)cdb[3];
671 			pCmd->uCmd.Ide.nSectors = (HPT_U16) cdb[4];
672 			break;
673 		case READ_16:
674 		case WRITE_16:
675 		{
676 			HPT_U64 block =
677 				((HPT_U64)cdb[2]<<56) |
678 				((HPT_U64)cdb[3]<<48) |
679 				((HPT_U64)cdb[4]<<40) |
680 				((HPT_U64)cdb[5]<<32) |
681 				((HPT_U64)cdb[6]<<24) |
682 				((HPT_U64)cdb[7]<<16) |
683 				((HPT_U64)cdb[8]<<8) |
684 				((HPT_U64)cdb[9]);
685 			pCmd->uCmd.Ide.Lba = block;
686 			pCmd->uCmd.Ide.nSectors = (HPT_U16)cdb[13] | ((HPT_U16)cdb[12]<<8);
687 			break;
688 		}
689 
690 		default:
691 			pCmd->uCmd.Ide.Lba = (HPT_U32)cdb[5] | ((HPT_U32)cdb[4] << 8) | ((HPT_U32)cdb[3] << 16) | ((HPT_U32)cdb[2] << 24);
692 			pCmd->uCmd.Ide.nSectors = (HPT_U16) cdb[8] | ((HPT_U16)cdb[7]<<8);
693 			break;
694 		}
695 
696 		switch (cdb[0]) {
697 		case READ_6:
698 		case READ_10:
699 		case READ_16:
700 			pCmd->flags.data_in = 1;
701 			break;
702 		case WRITE_6:
703 		case WRITE_10:
704 		case WRITE_16:
705 			pCmd->flags.data_out = 1;
706 			break;
707 		}
708 		pCmd->priv = ext = cmdext_get(vbus_ext);
709 		HPT_ASSERT(ext);
710 		ext->ccb = ccb;
711 		pCmd->target = vd;
712 		pCmd->done = os_cmddone;
713 		pCmd->buildsgl = os_buildsgl;
714 		pCmd->psg = ext->psg;
715 		pCmd->flags.physical_sg = 1;
716 		error = bus_dmamap_load_ccb(vbus_ext->io_dmat,
717 					ext->dma_map,
718 					ccb,
719 					hpt_io_dmamap_callback, pCmd,
720 			    		BUS_DMA_WAITOK
721 					);
722 		KdPrint(("bus_dmamap_load return %d", error));
723 		if (error && error!=EINPROGRESS) {
724 			os_printk("bus_dmamap_load error %d", error);
725 			cmdext_put(ext);
726 			ldm_free_cmds(pCmd);
727 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
728 			xpt_done(ccb);
729 		}
730 		return;
731 	}
732 
733 	default:
734 		ccb->ccb_h.status = CAM_REQ_INVALID;
735 		break;
736 	}
737 
738 	xpt_done(ccb);
739 	return;
740 }
741 
742 static void hpt_action(struct cam_sim *sim, union ccb *ccb)
743 {
744 	PVBUS_EXT vbus_ext = (PVBUS_EXT)cam_sim_softc(sim);
745 
746 	KdPrint(("hpt_action(fn=%d, id=%d)", ccb->ccb_h.func_code, ccb->ccb_h.target_id));
747 
748 	hpt_assert_vbus_locked(vbus_ext);
749 	switch (ccb->ccb_h.func_code) {
750 
751 	case XPT_SCSI_IO:
752 		hpt_scsi_io(vbus_ext, ccb);
753 		return;
754 
755 	case XPT_RESET_BUS:
756 		ldm_reset_vbus((PVBUS)vbus_ext->vbus);
757 		break;
758 
759 	case XPT_GET_TRAN_SETTINGS:
760 	case XPT_SET_TRAN_SETTINGS:
761 		ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
762 		break;
763 
764 	case XPT_CALC_GEOMETRY:
765 		cam_calc_geometry(&ccb->ccg, 1);
766 		break;
767 
768 	case XPT_PATH_INQ:
769 	{
770 		struct ccb_pathinq *cpi = &ccb->cpi;
771 
772 		cpi->version_num = 1;
773 		cpi->hba_inquiry = PI_SDTR_ABLE;
774 		cpi->target_sprt = 0;
775 		cpi->hba_misc = PIM_NOBUSRESET;
776 		cpi->hba_eng_cnt = 0;
777 		cpi->max_target = osm_max_targets;
778 		cpi->max_lun = 0;
779 		cpi->unit_number = cam_sim_unit(sim);
780 		cpi->bus_id = cam_sim_bus(sim);
781 		cpi->initiator_id = osm_max_targets;
782 		cpi->base_transfer_speed = 3300;
783 
784 		strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
785 		strlcpy(cpi->hba_vid, "HPT   ", HBA_IDLEN);
786 		strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
787 		cpi->transport = XPORT_SPI;
788 		cpi->transport_version = 2;
789 		cpi->protocol = PROTO_SCSI;
790 		cpi->protocol_version = SCSI_REV_2;
791 		cpi->maxio = HPTRR_DFLTPHYS;
792 		cpi->ccb_h.status = CAM_REQ_CMP;
793 		break;
794 	}
795 
796 	default:
797 		ccb->ccb_h.status = CAM_REQ_INVALID;
798 		break;
799 	}
800 
801 	xpt_done(ccb);
802 	return;
803 }
804 
805 static void hpt_pci_intr(void *arg)
806 {
807 	PVBUS_EXT vbus_ext = (PVBUS_EXT)arg;
808 	hpt_lock_vbus(vbus_ext);
809 	ldm_intr((PVBUS)vbus_ext->vbus);
810 	hpt_unlock_vbus(vbus_ext);
811 }
812 
813 static void hpt_poll(struct cam_sim *sim)
814 {
815 	PVBUS_EXT vbus_ext = cam_sim_softc(sim);
816 	hpt_assert_vbus_locked(vbus_ext);
817 	ldm_intr((PVBUS)vbus_ext->vbus);
818 }
819 
820 static void hpt_async(void * callback_arg, u_int32_t code, struct cam_path * path, void * arg)
821 {
822 	KdPrint(("hpt_async"));
823 }
824 
825 static int hpt_shutdown(device_t dev)
826 {
827 	KdPrint(("hpt_shutdown(dev=%p)", dev));
828 	return 0;
829 }
830 
831 static int hpt_detach(device_t dev)
832 {
833 	/* we don't allow the driver to be unloaded. */
834 	return EBUSY;
835 }
836 
837 static void hpt_ioctl_done(struct _IOCTL_ARG *arg)
838 {
839 	arg->ioctl_cmnd = NULL;
840 	wakeup(arg);
841 }
842 
843 static void __hpt_do_ioctl(PVBUS_EXT vbus_ext, IOCTL_ARG *ioctl_args)
844 {
845 	ioctl_args->result = -1;
846 	ioctl_args->done = hpt_ioctl_done;
847 	ioctl_args->ioctl_cmnd = (void *)1;
848 
849 	hpt_lock_vbus(vbus_ext);
850 	ldm_ioctl((PVBUS)vbus_ext->vbus, ioctl_args);
851 
852 	while (ioctl_args->ioctl_cmnd) {
853 		if (hpt_sleep(vbus_ext, ioctl_args, 0, "hptctl", HPT_OSM_TIMEOUT)==0)
854 			break;
855 		ldm_reset_vbus((PVBUS)vbus_ext->vbus);
856 		__hpt_do_tasks(vbus_ext);
857 	}
858 
859 	/* KdPrint(("ioctl %x result %d", ioctl_args->dwIoControlCode, ioctl_args->result)); */
860 
861 	hpt_unlock_vbus(vbus_ext);
862 }
863 
864 static void hpt_do_ioctl(IOCTL_ARG *ioctl_args)
865 {
866 	PVBUS vbus;
867 	PVBUS_EXT vbus_ext;
868 
869 	ldm_for_each_vbus(vbus, vbus_ext) {
870 		__hpt_do_ioctl(vbus_ext, ioctl_args);
871 		if (ioctl_args->result!=HPT_IOCTL_RESULT_WRONG_VBUS)
872 			return;
873 	}
874 }
875 
876 #define HPT_DO_IOCTL(code, inbuf, insize, outbuf, outsize) ({\
877 	IOCTL_ARG arg;\
878 	arg.dwIoControlCode = code;\
879 	arg.lpInBuffer = inbuf;\
880 	arg.lpOutBuffer = outbuf;\
881 	arg.nInBufferSize = insize;\
882 	arg.nOutBufferSize = outsize;\
883 	arg.lpBytesReturned = NULL;\
884 	hpt_do_ioctl(&arg);\
885 	arg.result;\
886 })
887 
888 #define DEVICEID_VALID(id) ((id) && ((HPT_U32)(id)!=0xffffffff))
889 
890 static int hpt_get_logical_devices(DEVICEID * pIds, int nMaxCount)
891 {
892 	int i;
893 	HPT_U32 count = nMaxCount-1;
894 
895 	if (HPT_DO_IOCTL(HPT_IOCTL_GET_LOGICAL_DEVICES,
896 			&count, sizeof(HPT_U32), pIds, sizeof(DEVICEID)*nMaxCount))
897 		return -1;
898 
899 	nMaxCount = (int)pIds[0];
900 	for (i=0; i<nMaxCount; i++) pIds[i] = pIds[i+1];
901 	return nMaxCount;
902 }
903 
904 static int hpt_get_device_info_v3(DEVICEID id, PLOGICAL_DEVICE_INFO_V3 pInfo)
905 {
906 	return HPT_DO_IOCTL(HPT_IOCTL_GET_DEVICE_INFO_V3,
907 				&id, sizeof(DEVICEID), pInfo, sizeof(LOGICAL_DEVICE_INFO_V3));
908 }
909 
910 /* not belong to this file logically, but we want to use ioctl interface */
911 static int __hpt_stop_tasks(PVBUS_EXT vbus_ext, DEVICEID id)
912 {
913 	LOGICAL_DEVICE_INFO_V3 devinfo;
914 	int i, result;
915 	DEVICEID param[2] = { id, 0 };
916 
917 	if (hpt_get_device_info_v3(id, &devinfo))
918 		return -1;
919 
920 	if (devinfo.Type!=LDT_ARRAY)
921 		return -1;
922 
923 	if (devinfo.u.array.Flags & ARRAY_FLAG_REBUILDING)
924 		param[1] = AS_REBUILD_ABORT;
925 	else if (devinfo.u.array.Flags & ARRAY_FLAG_VERIFYING)
926 		param[1] = AS_VERIFY_ABORT;
927 	else if (devinfo.u.array.Flags & ARRAY_FLAG_INITIALIZING)
928 		param[1] = AS_INITIALIZE_ABORT;
929 	else if (devinfo.u.array.Flags & ARRAY_FLAG_TRANSFORMING)
930 		param[1] = AS_TRANSFORM_ABORT;
931 	else
932 		return -1;
933 
934 	KdPrint(("SET_ARRAY_STATE(%x, %d)", param[0], param[1]));
935 	result = HPT_DO_IOCTL(HPT_IOCTL_SET_ARRAY_STATE,
936 				param, sizeof(param), NULL, 0);
937 
938 	for (i=0; i<devinfo.u.array.nDisk; i++)
939 		if (DEVICEID_VALID(devinfo.u.array.Members[i]))
940 			__hpt_stop_tasks(vbus_ext, devinfo.u.array.Members[i]);
941 
942 	return result;
943 }
944 
945 static void hpt_stop_tasks(PVBUS_EXT vbus_ext)
946 {
947 	DEVICEID ids[32];
948 	int i, count;
949 
950 	count = hpt_get_logical_devices((DEVICEID *)&ids, sizeof(ids)/sizeof(ids[0]));
951 
952 	for (i=0; i<count; i++)
953 		__hpt_stop_tasks(vbus_ext, ids[i]);
954 }
955 
956 static	d_open_t	hpt_open;
957 static	d_close_t	hpt_close;
958 static	d_ioctl_t	hpt_ioctl;
959 static  int 		hpt_rescan_bus(void);
960 static  void 		hpt_rescan_callback(struct cam_periph *periph, union ccb *ccb);
961 
962 static struct dev_ops hpt_ops = {
963 	{ driver_name, 0, D_MPSAFE },
964 	.d_open =	hpt_open,
965 	.d_close =	hpt_close,
966 	.d_ioctl =	hpt_ioctl,
967 };
968 
969 static struct intr_config_hook hpt_ich;
970 
971 /*
972  * hpt_final_init will be called after all hpt_attach.
973  */
974 static void hpt_final_init(void *dummy)
975 {
976 	int       i;
977 	PVBUS_EXT vbus_ext;
978 	PVBUS vbus;
979 	PHBA hba;
980 
981 	/* Clear the config hook */
982 	config_intrhook_disestablish(&hpt_ich);
983 
984 	/* allocate memory */
985 	i = 0;
986 	ldm_for_each_vbus(vbus, vbus_ext) {
987 		if (hpt_alloc_mem(vbus_ext)) {
988 			os_printk("out of memory");
989 			return;
990 		}
991 		i++;
992 	}
993 
994 	if (!i) {
995 		if (bootverbose)
996 			os_printk("no controller detected.");
997 		return;
998 	}
999 
1000 	/* initializing hardware */
1001 	ldm_for_each_vbus(vbus, vbus_ext) {
1002 		/* make timer available here */
1003 		lockinit(&vbus_ext->lock, "hptsleeplock", 0, LK_CANRECURSE);
1004 		callout_init_lk(&vbus_ext->timer, &vbus_ext->lock);
1005 		if (hpt_init_vbus(vbus_ext)) {
1006 			os_printk("fail to initialize hardware");
1007 			break; /* FIXME */
1008 		}
1009 	}
1010 
1011 	/* register CAM interface */
1012 	ldm_for_each_vbus(vbus, vbus_ext) {
1013 		struct cam_devq *devq;
1014 		struct ccb_setasync *ccb;
1015 
1016 		if (bus_dma_tag_create(NULL,/* parent */
1017 				4,	/* alignment */
1018 				BUS_SPACE_MAXADDR_32BIT+1, /* boundary */
1019 				BUS_SPACE_MAXADDR,	/* lowaddr */
1020 				BUS_SPACE_MAXADDR, 	/* highaddr */
1021 				PAGE_SIZE * (os_max_sg_descriptors-1),	/* maxsize */
1022 				os_max_sg_descriptors,	/* nsegments */
1023 				0x10000,	/* maxsegsize */
1024 				BUS_DMA_WAITOK,		/* flags */
1025 				&vbus_ext->io_dmat	/* tag */))
1026 		{
1027 			return ;
1028 		}
1029 
1030 		for (i=0; i<os_max_queue_comm; i++) {
1031 			POS_CMDEXT ext = (POS_CMDEXT)kmalloc(sizeof(OS_CMDEXT), M_DEVBUF, M_WAITOK);
1032 			if (!ext) {
1033 				os_printk("Can't alloc cmdext(%d)", i);
1034 				return ;
1035 			}
1036 			ext->vbus_ext = vbus_ext;
1037 			ext->next = vbus_ext->cmdext_list;
1038 			vbus_ext->cmdext_list = ext;
1039 
1040 			if (bus_dmamap_create(vbus_ext->io_dmat, 0, &ext->dma_map)) {
1041 				os_printk("Can't create dma map(%d)", i);
1042 				return ;
1043 			}
1044 			callout_init_lk(&ext->timeout, &vbus_ext->lock);
1045 		}
1046 
1047 		if ((devq = cam_simq_alloc(os_max_queue_comm)) == NULL) {
1048 			os_printk("cam_simq_alloc failed");
1049 			return ;
1050 		}
1051 
1052 		vbus_ext->sim = cam_sim_alloc(hpt_action, hpt_poll, driver_name,
1053 				vbus_ext, 0, &vbus_ext->lock, os_max_queue_comm,
1054 				/*tagged*/8,  devq);
1055 		cam_simq_release(devq);
1056 
1057 		if (!vbus_ext->sim) {
1058 			os_printk("cam_sim_alloc failed");
1059 			return ;
1060 		}
1061 
1062 		hpt_lock_vbus(vbus_ext);
1063 		if (xpt_bus_register(vbus_ext->sim, 0) != CAM_SUCCESS) {
1064 			os_printk("xpt_bus_register failed");
1065 			cam_sim_free(vbus_ext->sim);
1066 			hpt_unlock_vbus(vbus_ext);
1067 			vbus_ext->sim = NULL;
1068 			return ;
1069 		}
1070 
1071 		if (xpt_create_path(&vbus_ext->path, /*periph */ NULL,
1072 				cam_sim_path(vbus_ext->sim), CAM_TARGET_WILDCARD,
1073 				CAM_LUN_WILDCARD) != CAM_REQ_CMP)
1074 		{
1075 			os_printk("xpt_create_path failed");
1076 			xpt_bus_deregister(cam_sim_path(vbus_ext->sim));
1077 			cam_sim_free(vbus_ext->sim);
1078 			hpt_unlock_vbus(vbus_ext);
1079 			vbus_ext->sim = NULL;
1080 			return ;
1081 		}
1082 		hpt_unlock_vbus(vbus_ext);
1083 
1084 		ccb = &xpt_alloc_ccb()->csa;
1085 		xpt_setup_ccb(&ccb->ccb_h, vbus_ext->path, /*priority*/5);
1086 		ccb->ccb_h.func_code = XPT_SASYNC_CB;
1087 		ccb->event_enable = AC_LOST_DEVICE;
1088 		ccb->callback = hpt_async;
1089 		ccb->callback_arg = vbus_ext;
1090 		xpt_action((union ccb *)ccb);
1091 		xpt_free_ccb(&ccb->ccb_h);
1092 
1093 		for (hba = vbus_ext->hba_list; hba; hba = hba->next) {
1094 			int rid = 0;
1095 			if ((hba->irq_res = bus_alloc_resource_any(hba->pcidev,
1096 				SYS_RES_IRQ, &rid, RF_SHAREABLE | RF_ACTIVE)) == NULL)
1097 			{
1098 				os_printk("can't allocate interrupt");
1099 				return ;
1100 			}
1101 
1102 			if (bus_setup_intr(hba->pcidev, hba->irq_res, INTR_MPSAFE,
1103 				hpt_pci_intr, vbus_ext, &hba->irq_handle, NULL))
1104 			{
1105 				os_printk("can't set up interrupt");
1106 				return ;
1107 			}
1108 			hba->ldm_adapter.him->intr_control(hba->ldm_adapter.him_handle, HPT_TRUE);
1109 		}
1110 
1111 		vbus_ext->shutdown_eh = EVENTHANDLER_REGISTER(shutdown_final,
1112 									hpt_shutdown_vbus, vbus_ext, SHUTDOWN_PRI_DEFAULT);
1113 		if (!vbus_ext->shutdown_eh)
1114 			os_printk("Shutdown event registration failed");
1115 	}
1116 
1117 	ldm_for_each_vbus(vbus, vbus_ext) {
1118 		TASK_INIT(&vbus_ext->worker, 0, (task_fn_t *)hpt_do_tasks, vbus_ext);
1119 		if (vbus_ext->tasks)
1120 			TASK_ENQUEUE(&vbus_ext->worker);
1121 	}
1122 
1123 	make_dev(&hpt_ops, DRIVER_MINOR, UID_ROOT, GID_OPERATOR,
1124 	    S_IRUSR | S_IWUSR, "%s", driver_name);
1125 }
1126 
1127 #if defined(KLD_MODULE)
1128 
1129 typedef struct driverlink *driverlink_t;
1130 struct driverlink {
1131 	kobj_class_t	driver;
1132 	TAILQ_ENTRY(driverlink) link;	/* list of drivers in devclass */
1133 };
1134 
1135 typedef TAILQ_HEAD(driver_list, driverlink) driver_list_t;
1136 
1137 struct devclass {
1138 	TAILQ_ENTRY(devclass) link;
1139 	devclass_t	parent;		/* parent in devclass hierarchy */
1140 	driver_list_t	drivers;     /* bus devclasses store drivers for bus */
1141 	char		*name;
1142 	device_t	*devices;	/* array of devices indexed by unit */
1143 	int		maxunit;	/* size of devices array */
1144 };
1145 
1146 static void override_kernel_driver(void)
1147 {
1148 	driverlink_t dl, dlfirst;
1149 	driver_t *tmpdriver;
1150 	devclass_t dc = devclass_find("pci");
1151 
1152 	if (dc){
1153 		dlfirst = TAILQ_FIRST(&dc->drivers);
1154 		for (dl = dlfirst; dl; dl = TAILQ_NEXT(dl, link)) {
1155 			if(strcmp(dl->driver->name, driver_name) == 0) {
1156 				tmpdriver=dl->driver;
1157 				dl->driver=dlfirst->driver;
1158 				dlfirst->driver=tmpdriver;
1159 				break;
1160 			}
1161 		}
1162 	}
1163 }
1164 
1165 #else
1166 #define override_kernel_driver()
1167 #endif
1168 
1169 static void hpt_init(void *dummy)
1170 {
1171 	if (bootverbose)
1172 		os_printk("%s %s", driver_name_long, driver_ver);
1173 
1174 	override_kernel_driver();
1175 	init_config();
1176 
1177 	hpt_ich.ich_func = hpt_final_init;
1178 	hpt_ich.ich_arg = NULL;
1179 	hpt_ich.ich_desc = "hptrr";
1180 	if (config_intrhook_establish(&hpt_ich) != 0) {
1181 		kprintf("%s: cannot establish configuration hook\n",
1182 		    driver_name_long);
1183 	}
1184 
1185 }
1186 SYSINIT(hptinit, SI_SUB_CONFIGURE, SI_ORDER_FIRST, hpt_init, NULL);
1187 
1188 /*
1189  * CAM driver interface
1190  */
1191 static device_method_t driver_methods[] = {
1192 	/* Device interface */
1193 	DEVMETHOD(device_probe,		hpt_probe),
1194 	DEVMETHOD(device_attach,	hpt_attach),
1195 	DEVMETHOD(device_detach,	hpt_detach),
1196 	DEVMETHOD(device_shutdown,	hpt_shutdown),
1197 	DEVMETHOD_END
1198 };
1199 
1200 static driver_t hpt_pci_driver = {
1201 	driver_name,
1202 	driver_methods,
1203 	sizeof(HBA)
1204 };
1205 
1206 static devclass_t	hpt_devclass;
1207 
1208 #ifndef TARGETNAME
1209 #error "no TARGETNAME found"
1210 #endif
1211 
1212 /* use this to make TARGETNAME be expanded */
1213 #define __DRIVER_MODULE(p1, p2, p3, p4, p5, p6) DRIVER_MODULE(p1, p2, p3, p4, p5, p6)
1214 #define __MODULE_VERSION(p1, p2) MODULE_VERSION(p1, p2)
1215 #define __MODULE_DEPEND(p1, p2, p3, p4, p5) MODULE_DEPEND(p1, p2, p3, p4, p5)
1216 __DRIVER_MODULE(TARGETNAME, pci, hpt_pci_driver, hpt_devclass, NULL, NULL);
1217 __MODULE_VERSION(TARGETNAME, 1);
1218 __MODULE_DEPEND(TARGETNAME, cam, 1, 1, 1);
1219 
1220 static int hpt_open(struct dev_open_args *ap)
1221 {
1222 	return 0;
1223 }
1224 
1225 static int hpt_close(struct dev_close_args *ap)
1226 {
1227 	return 0;
1228 }
1229 
1230 static int hpt_ioctl(struct dev_ioctl_args *ap)
1231 {
1232 	u_long cmd = ap->a_cmd;
1233 	caddr_t data = ap->a_data;
1234 	PHPT_IOCTL_PARAM piop=(PHPT_IOCTL_PARAM)data;
1235 	IOCTL_ARG ioctl_args;
1236 	HPT_U32 bytesReturned;
1237 
1238 	switch (cmd){
1239 	case HPT_DO_IOCONTROL:
1240 	{
1241 		if (piop->Magic == HPT_IOCTL_MAGIC || piop->Magic == HPT_IOCTL_MAGIC32) {
1242 			KdPrint(("ioctl=%x in=%p len=%d out=%p len=%d\n",
1243 				piop->dwIoControlCode,
1244 				piop->lpInBuffer,
1245 				piop->nInBufferSize,
1246 				piop->lpOutBuffer,
1247 				piop->nOutBufferSize));
1248 
1249 		memset(&ioctl_args, 0, sizeof(ioctl_args));
1250 
1251 		ioctl_args.dwIoControlCode = piop->dwIoControlCode;
1252 		ioctl_args.nInBufferSize = piop->nInBufferSize;
1253 		ioctl_args.nOutBufferSize = piop->nOutBufferSize;
1254 		ioctl_args.lpBytesReturned = &bytesReturned;
1255 
1256 		if (ioctl_args.nInBufferSize) {
1257 			ioctl_args.lpInBuffer = kmalloc(ioctl_args.nInBufferSize, M_DEVBUF, M_WAITOK);
1258 			if (!ioctl_args.lpInBuffer)
1259 				goto invalid;
1260 			if (copyin((void*)piop->lpInBuffer,
1261 					ioctl_args.lpInBuffer, piop->nInBufferSize))
1262 				goto invalid;
1263 		}
1264 
1265 		if (ioctl_args.nOutBufferSize) {
1266 			ioctl_args.lpOutBuffer = kmalloc(ioctl_args.nOutBufferSize, M_DEVBUF, M_WAITOK);
1267 			if (!ioctl_args.lpOutBuffer)
1268 				goto invalid;
1269 		}
1270 
1271 		hpt_do_ioctl(&ioctl_args);
1272 
1273 		if (ioctl_args.result==HPT_IOCTL_RESULT_OK) {
1274 			if (piop->nOutBufferSize) {
1275 				if (copyout(ioctl_args.lpOutBuffer,
1276 					(void*)piop->lpOutBuffer, piop->nOutBufferSize))
1277 					goto invalid;
1278 			}
1279 			if (piop->lpBytesReturned) {
1280 				if (copyout(&bytesReturned,
1281 					(void*)piop->lpBytesReturned, sizeof(HPT_U32)))
1282 					goto invalid;
1283 			}
1284 			if (ioctl_args.lpInBuffer) kfree(ioctl_args.lpInBuffer, M_DEVBUF);
1285 			if (ioctl_args.lpOutBuffer) kfree(ioctl_args.lpOutBuffer, M_DEVBUF);
1286 			return 0;
1287 		}
1288 invalid:
1289 		if (ioctl_args.lpInBuffer) kfree(ioctl_args.lpInBuffer, M_DEVBUF);
1290 		if (ioctl_args.lpOutBuffer) kfree(ioctl_args.lpOutBuffer, M_DEVBUF);
1291 		return EFAULT;
1292 	}
1293 	return EFAULT;
1294 	}
1295 
1296 	case HPT_SCAN_BUS:
1297 	{
1298 		return hpt_rescan_bus();
1299 	}
1300 	default:
1301 		KdPrint(("invalid command!"));
1302 		return EFAULT;
1303 	}
1304 
1305 }
1306 
1307 static void	hpt_rescan_callback(struct cam_periph *periph, union ccb *ccb)
1308 {
1309 	xpt_free_path(ccb->ccb_h.path);
1310 	xpt_free_ccb(&ccb->ccb_h);
1311 }
1312 
1313 static int	hpt_rescan_bus(void)
1314 {
1315 	union ccb			*ccb;
1316 	PVBUS 				vbus;
1317 	PVBUS_EXT			vbus_ext;
1318 
1319 	ldm_for_each_vbus(vbus, vbus_ext) {
1320 		if ((ccb = xpt_alloc_ccb()) == NULL)
1321 			return(ENOMEM);
1322 		if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
1323 		    cam_sim_path(vbus_ext->sim),
1324 		    CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1325 			xpt_free_ccb(&ccb->ccb_h);
1326 			return(EIO);
1327 		}
1328 
1329 		xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, 5/*priority (low)*/);
1330 		ccb->ccb_h.func_code = XPT_SCAN_BUS;
1331 		ccb->ccb_h.cbfcnp = hpt_rescan_callback;
1332 		ccb->crcn.flags = CAM_FLAG_NONE;
1333 		xpt_action(ccb); /* scan is now in progress */
1334 	}
1335 
1336 	return(0);
1337 }
1338