siis.c revision 198896
186560Sjake/*-
286560Sjake * Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org>
386560Sjake * All rights reserved.
486560Sjake *
586560Sjake * Redistribution and use in source and binary forms, with or without
686560Sjake * modification, are permitted provided that the following conditions
786560Sjake * are met:
886560Sjake * 1. Redistributions of source code must retain the above copyright
986560Sjake *    notice, this list of conditions and the following disclaimer,
1086560Sjake *    without modification, immediately at the beginning of the file.
1186560Sjake * 2. Redistributions in binary form must reproduce the above copyright
1286560Sjake *    notice, this list of conditions and the following disclaimer in the
1386560Sjake *    documentation and/or other materials provided with the distribution.
1486560Sjake *
1586560Sjake * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1686560Sjake * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1786560Sjake * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1886560Sjake * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1986560Sjake * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2086560Sjake * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2186560Sjake * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2286560Sjake * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2386560Sjake * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2486560Sjake * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2586560Sjake */
2686560Sjake
2786560Sjake#include <sys/cdefs.h>
2886560Sjake__FBSDID("$FreeBSD: head/sys/dev/siis/siis.c 198896 2009-11-04 15:10:46Z mav $");
2986560Sjake
3086560Sjake#include <sys/param.h>
3186560Sjake#include <sys/module.h>
3286560Sjake#include <sys/systm.h>
3386560Sjake#include <sys/kernel.h>
3486560Sjake#include <sys/ata.h>
3586560Sjake#include <sys/bus.h>
3686560Sjake#include <sys/endian.h>
3786560Sjake#include <sys/malloc.h>
3886560Sjake#include <sys/lock.h>
3986560Sjake#include <sys/mutex.h>
4086560Sjake#include <sys/sema.h>
4186560Sjake#include <sys/taskqueue.h>
4286560Sjake#include <vm/uma.h>
4386560Sjake#include <machine/stdarg.h>
4486560Sjake#include <machine/resource.h>
4586560Sjake#include <machine/bus.h>
4686560Sjake#include <sys/rman.h>
4786560Sjake#include <dev/pci/pcivar.h>
4886560Sjake#include <dev/pci/pcireg.h>
4986560Sjake#include "siis.h"
5086560Sjake
5186560Sjake#include <cam/cam.h>
5286560Sjake#include <cam/cam_ccb.h>
5386560Sjake#include <cam/cam_sim.h>
5486560Sjake#include <cam/cam_xpt_sim.h>
5586560Sjake#include <cam/cam_xpt_periph.h>
5686560Sjake#include <cam/cam_debug.h>
5786560Sjake
5886560Sjake/* local prototypes */
5986560Sjakestatic int siis_setup_interrupt(device_t dev);
6086560Sjakestatic void siis_intr(void *data);
6186560Sjakestatic int siis_suspend(device_t dev);
6286560Sjakestatic int siis_resume(device_t dev);
6386560Sjakestatic int siis_ch_suspend(device_t dev);
6486560Sjakestatic int siis_ch_resume(device_t dev);
6586560Sjakestatic void siis_ch_intr_locked(void *data);
6686560Sjakestatic void siis_ch_intr(void *data);
6786560Sjakestatic void siis_begin_transaction(device_t dev, union ccb *ccb);
6886560Sjakestatic void siis_dmasetprd(void *arg, bus_dma_segment_t *segs, int nsegs, int error);
6986560Sjakestatic void siis_execute_transaction(struct siis_slot *slot);
7086560Sjakestatic void siis_timeout(struct siis_slot *slot);
7186560Sjakestatic void siis_end_transaction(struct siis_slot *slot, enum siis_err_type et);
7286560Sjakestatic int siis_setup_fis(struct siis_cmd *ctp, union ccb *ccb, int tag);
7386560Sjakestatic void siis_dmainit(device_t dev);
7486560Sjakestatic void siis_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error);
7586560Sjakestatic void siis_dmafini(device_t dev);
7686560Sjakestatic void siis_slotsalloc(device_t dev);
7786560Sjakestatic void siis_slotsfree(device_t dev);
7886560Sjakestatic void siis_reset(device_t dev);
7986560Sjakestatic void siis_portinit(device_t dev);
8086560Sjakestatic int siis_wait_ready(device_t dev, int t);
8186560Sjake
8286560Sjakestatic int siis_sata_connect(struct siis_channel *ch);
8386560Sjake
8486560Sjakestatic void siis_issue_read_log(device_t dev);
8586560Sjakestatic void siis_process_read_log(device_t dev, union ccb *ccb);
8686560Sjake
8786560Sjakestatic void siisaction(struct cam_sim *sim, union ccb *ccb);
8886560Sjakestatic void siispoll(struct cam_sim *sim);
8986560Sjake
9086560SjakeMALLOC_DEFINE(M_SIIS, "SIIS driver", "SIIS driver data buffers");
9186560Sjake
9286560Sjakestatic int
9386560Sjakesiis_probe(device_t dev)
9486560Sjake{
9586560Sjake	uint32_t devid = pci_get_devid(dev);
9686560Sjake
9786560Sjake	if (devid == SIIS_SII3124) {
9886560Sjake		device_set_desc_copy(dev, "SiI3124 SATA2 controller");
9986560Sjake	} else if (devid == SIIS_SII3132 ||
10086560Sjake		   devid == SIIS_SII3132_1 ||
10186560Sjake		   devid == SIIS_SII3132_2) {
10286560Sjake		device_set_desc_copy(dev, "SiI3132 SATA2 controller");
10386560Sjake	} else if (devid == SIIS_SII3531) {
10486560Sjake		device_set_desc_copy(dev, "SiI3531 SATA2 controller");
10586560Sjake	} else {
10686560Sjake		return (ENXIO);
10786560Sjake	}
10886560Sjake
10986560Sjake	return (BUS_PROBE_VENDOR);
11086560Sjake}
11186560Sjake
11286560Sjakestatic int
11386560Sjakesiis_attach(device_t dev)
11486560Sjake{
11586560Sjake	struct siis_controller *ctlr = device_get_softc(dev);
11686560Sjake	uint32_t devid = pci_get_devid(dev);
11786560Sjake	device_t child;
11886560Sjake	int	error, unit;
11988615Sjake
12088615Sjake	ctlr->dev = dev;
12186560Sjake	/* Global memory */
12286560Sjake	ctlr->r_grid = PCIR_BAR(0);
12388615Sjake	if (!(ctlr->r_gmem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
12486560Sjake	    &ctlr->r_grid, RF_ACTIVE)))
12588615Sjake		return (ENXIO);
12686560Sjake	/* Channels memory */
12786560Sjake	ctlr->r_rid = PCIR_BAR(2);
12886560Sjake	if (!(ctlr->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
12986560Sjake	    &ctlr->r_rid, RF_ACTIVE)))
13088615Sjake		return (ENXIO);
13186560Sjake	/* Setup our own memory management for channels. */
13286560Sjake	ctlr->sc_iomem.rm_type = RMAN_ARRAY;
13388615Sjake	ctlr->sc_iomem.rm_descr = "I/O memory addresses";
13486560Sjake	if ((error = rman_init(&ctlr->sc_iomem)) != 0) {
13586560Sjake		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
13686560Sjake		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_grid, ctlr->r_gmem);
13788615Sjake		return (error);
13886560Sjake	}
13986560Sjake	if ((error = rman_manage_region(&ctlr->sc_iomem,
14086560Sjake	    rman_get_start(ctlr->r_mem), rman_get_end(ctlr->r_mem))) != 0) {
14188615Sjake		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
14286560Sjake		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_grid, ctlr->r_gmem);
14386560Sjake		rman_fini(&ctlr->sc_iomem);
14488615Sjake		return (error);
14586560Sjake	}
14686560Sjake	/* Reset controller */
14786560Sjake	siis_resume(dev);
14886560Sjake	/* Number of HW channels */
14986560Sjake	ctlr->channels = (devid == SIIS_SII3124) ? 4 :
15086560Sjake	    (devid == SIIS_SII3531 ? 1 : 2);
151	/* Setup interrupts. */
152	if (siis_setup_interrupt(dev)) {
153		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
154		bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_grid, ctlr->r_gmem);
155		rman_fini(&ctlr->sc_iomem);
156		return ENXIO;
157	}
158	/* Attach all channels on this controller */
159	for (unit = 0; unit < ctlr->channels; unit++) {
160		child = device_add_child(dev, "siisch", -1);
161		if (child == NULL)
162			device_printf(dev, "failed to add channel device\n");
163		else
164			device_set_ivars(child, (void *)(intptr_t)unit);
165	}
166	bus_generic_attach(dev);
167	return 0;
168}
169
170static int
171siis_detach(device_t dev)
172{
173	struct siis_controller *ctlr = device_get_softc(dev);
174	device_t *children;
175	int nchildren, i;
176
177	/* Detach & delete all children */
178	if (!device_get_children(dev, &children, &nchildren)) {
179		for (i = 0; i < nchildren; i++)
180			device_delete_child(dev, children[i]);
181		free(children, M_TEMP);
182	}
183	/* Free interrupts. */
184	if (ctlr->irq.r_irq) {
185		bus_teardown_intr(dev, ctlr->irq.r_irq,
186		    ctlr->irq.handle);
187		bus_release_resource(dev, SYS_RES_IRQ,
188		    ctlr->irq.r_irq_rid, ctlr->irq.r_irq);
189	}
190	pci_release_msi(dev);
191	/* Free memory. */
192	rman_fini(&ctlr->sc_iomem);
193	bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
194	bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_grid, ctlr->r_gmem);
195	return (0);
196}
197
198static int
199siis_suspend(device_t dev)
200{
201	struct siis_controller *ctlr = device_get_softc(dev);
202
203	bus_generic_suspend(dev);
204	/* Put controller into reset state. */
205	ATA_OUTL(ctlr->r_gmem, SIIS_GCTL, SIIS_GCTL_GRESET);
206	return 0;
207}
208
209static int
210siis_resume(device_t dev)
211{
212	struct siis_controller *ctlr = device_get_softc(dev);
213
214	/* Put controller into reset state. */
215	ATA_OUTL(ctlr->r_gmem, SIIS_GCTL, SIIS_GCTL_GRESET);
216	DELAY(10000);
217	/* Get controller out of reset state and enable port interrupts. */
218	ATA_OUTL(ctlr->r_gmem, SIIS_GCTL, 0x0000000f);
219	return (bus_generic_resume(dev));
220}
221
222static int
223siis_setup_interrupt(device_t dev)
224{
225	struct siis_controller *ctlr = device_get_softc(dev);
226	int msi = 0;
227
228	/* Process hints. */
229	resource_int_value(device_get_name(dev),
230	    device_get_unit(dev), "msi", &msi);
231	if (msi < 0)
232		msi = 0;
233	else if (msi > 0)
234		msi = min(1, pci_msi_count(dev));
235	/* Allocate MSI if needed/present. */
236	if (msi && pci_alloc_msi(dev, &msi) != 0)
237		msi = 0;
238	/* Allocate all IRQs. */
239	ctlr->irq.r_irq_rid = msi ? 1 : 0;
240	if (!(ctlr->irq.r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
241	    &ctlr->irq.r_irq_rid, RF_SHAREABLE | RF_ACTIVE))) {
242		device_printf(dev, "unable to map interrupt\n");
243		return ENXIO;
244	}
245	if ((bus_setup_intr(dev, ctlr->irq.r_irq, ATA_INTR_FLAGS, NULL,
246	    siis_intr, ctlr, &ctlr->irq.handle))) {
247		/* SOS XXX release r_irq */
248		device_printf(dev, "unable to setup interrupt\n");
249		return ENXIO;
250	}
251	return (0);
252}
253
254/*
255 * Common case interrupt handler.
256 */
257static void
258siis_intr(void *data)
259{
260	struct siis_controller *ctlr = (struct siis_controller *)data;
261	u_int32_t is;
262	void *arg;
263	int unit;
264
265	is = ATA_INL(ctlr->r_gmem, SIIS_IS);
266	for (unit = 0; unit < ctlr->channels; unit++) {
267		if ((is & SIIS_IS_PORT(unit)) != 0 &&
268		    (arg = ctlr->interrupt[unit].argument)) {
269			ctlr->interrupt[unit].function(arg);
270		}
271	}
272}
273
274static struct resource *
275siis_alloc_resource(device_t dev, device_t child, int type, int *rid,
276		       u_long start, u_long end, u_long count, u_int flags)
277{
278	struct siis_controller *ctlr = device_get_softc(dev);
279	int unit = ((struct siis_channel *)device_get_softc(child))->unit;
280	struct resource *res = NULL;
281	int offset = unit << 13;
282	long st;
283
284	switch (type) {
285	case SYS_RES_MEMORY:
286		st = rman_get_start(ctlr->r_mem);
287		res = rman_reserve_resource(&ctlr->sc_iomem, st + offset,
288		    st + offset + 0x2000, 0x2000, RF_ACTIVE, child);
289		if (res) {
290			bus_space_handle_t bsh;
291			bus_space_tag_t bst;
292			bsh = rman_get_bushandle(ctlr->r_mem);
293			bst = rman_get_bustag(ctlr->r_mem);
294			bus_space_subregion(bst, bsh, offset, 0x2000, &bsh);
295			rman_set_bushandle(res, bsh);
296			rman_set_bustag(res, bst);
297		}
298		break;
299	case SYS_RES_IRQ:
300		if (*rid == ATA_IRQ_RID)
301			res = ctlr->irq.r_irq;
302		break;
303	}
304	return (res);
305}
306
307static int
308siis_release_resource(device_t dev, device_t child, int type, int rid,
309			 struct resource *r)
310{
311
312	switch (type) {
313	case SYS_RES_MEMORY:
314		rman_release_resource(r);
315		return (0);
316	case SYS_RES_IRQ:
317		if (rid != ATA_IRQ_RID)
318			return ENOENT;
319		return (0);
320	}
321	return (EINVAL);
322}
323
324static int
325siis_setup_intr(device_t dev, device_t child, struct resource *irq,
326		   int flags, driver_filter_t *filter, driver_intr_t *function,
327		   void *argument, void **cookiep)
328{
329	struct siis_controller *ctlr = device_get_softc(dev);
330	int unit = (intptr_t)device_get_ivars(child);
331
332	if (filter != NULL) {
333		printf("siis.c: we cannot use a filter here\n");
334		return (EINVAL);
335	}
336	ctlr->interrupt[unit].function = function;
337	ctlr->interrupt[unit].argument = argument;
338	return (0);
339}
340
341static int
342siis_teardown_intr(device_t dev, device_t child, struct resource *irq,
343		      void *cookie)
344{
345	struct siis_controller *ctlr = device_get_softc(dev);
346	int unit = (intptr_t)device_get_ivars(child);
347
348	ctlr->interrupt[unit].function = NULL;
349	ctlr->interrupt[unit].argument = NULL;
350	return (0);
351}
352
353static int
354siis_print_child(device_t dev, device_t child)
355{
356	int retval;
357
358	retval = bus_print_child_header(dev, child);
359	retval += printf(" at channel %d",
360	    (int)(intptr_t)device_get_ivars(child));
361	retval += bus_print_child_footer(dev, child);
362
363	return (retval);
364}
365
366devclass_t siis_devclass;
367static device_method_t siis_methods[] = {
368	DEVMETHOD(device_probe,     siis_probe),
369	DEVMETHOD(device_attach,    siis_attach),
370	DEVMETHOD(device_detach,    siis_detach),
371	DEVMETHOD(device_suspend,   siis_suspend),
372	DEVMETHOD(device_resume,    siis_resume),
373	DEVMETHOD(bus_print_child,  siis_print_child),
374	DEVMETHOD(bus_alloc_resource,       siis_alloc_resource),
375	DEVMETHOD(bus_release_resource,     siis_release_resource),
376	DEVMETHOD(bus_setup_intr,   siis_setup_intr),
377	DEVMETHOD(bus_teardown_intr,siis_teardown_intr),
378	{ 0, 0 }
379};
380static driver_t siis_driver = {
381        "siis",
382        siis_methods,
383        sizeof(struct siis_controller)
384};
385DRIVER_MODULE(siis, pci, siis_driver, siis_devclass, 0, 0);
386MODULE_VERSION(siis, 1);
387MODULE_DEPEND(siis, cam, 1, 1, 1);
388
389static int
390siis_ch_probe(device_t dev)
391{
392
393	device_set_desc_copy(dev, "SIIS channel");
394	return (0);
395}
396
397static int
398siis_ch_attach(device_t dev)
399{
400	struct siis_channel *ch = device_get_softc(dev);
401	struct cam_devq *devq;
402	int rid, error;
403
404	ch->dev = dev;
405	ch->unit = (intptr_t)device_get_ivars(dev);
406	resource_int_value(device_get_name(dev),
407	    device_get_unit(dev), "pm_level", &ch->pm_level);
408	resource_int_value(device_get_name(dev),
409	    device_get_unit(dev), "sata_rev", &ch->sata_rev);
410	mtx_init(&ch->mtx, "SIIS channel lock", NULL, MTX_DEF);
411	rid = ch->unit;
412	if (!(ch->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
413	    &rid, RF_ACTIVE)))
414		return (ENXIO);
415	siis_dmainit(dev);
416	siis_slotsalloc(dev);
417	siis_ch_resume(dev);
418	mtx_lock(&ch->mtx);
419	rid = ATA_IRQ_RID;
420	if (!(ch->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
421	    &rid, RF_SHAREABLE | RF_ACTIVE))) {
422		bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem);
423		device_printf(dev, "Unable to map interrupt\n");
424		return (ENXIO);
425	}
426	if ((bus_setup_intr(dev, ch->r_irq, ATA_INTR_FLAGS, NULL,
427	    siis_ch_intr_locked, dev, &ch->ih))) {
428		device_printf(dev, "Unable to setup interrupt\n");
429		error = ENXIO;
430		goto err1;
431	}
432	/* Create the device queue for our SIM. */
433	devq = cam_simq_alloc(SIIS_MAX_SLOTS);
434	if (devq == NULL) {
435		device_printf(dev, "Unable to allocate simq\n");
436		error = ENOMEM;
437		goto err1;
438	}
439	/* Construct SIM entry */
440	ch->sim = cam_sim_alloc(siisaction, siispoll, "siisch", ch,
441	    device_get_unit(dev), &ch->mtx, SIIS_MAX_SLOTS, 0, devq);
442	if (ch->sim == NULL) {
443		device_printf(dev, "unable to allocate sim\n");
444		error = ENOMEM;
445		goto err2;
446	}
447	if (xpt_bus_register(ch->sim, dev, 0) != CAM_SUCCESS) {
448		device_printf(dev, "unable to register xpt bus\n");
449		error = ENXIO;
450		goto err2;
451	}
452	if (xpt_create_path(&ch->path, /*periph*/NULL, cam_sim_path(ch->sim),
453	    CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
454		device_printf(dev, "unable to create path\n");
455		error = ENXIO;
456		goto err3;
457	}
458	mtx_unlock(&ch->mtx);
459	return (0);
460
461err3:
462	xpt_bus_deregister(cam_sim_path(ch->sim));
463err2:
464	cam_sim_free(ch->sim, /*free_devq*/TRUE);
465err1:
466	bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
467	bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem);
468	mtx_unlock(&ch->mtx);
469	return (error);
470}
471
472static int
473siis_ch_detach(device_t dev)
474{
475	struct siis_channel *ch = device_get_softc(dev);
476
477	mtx_lock(&ch->mtx);
478	xpt_async(AC_LOST_DEVICE, ch->path, NULL);
479	xpt_free_path(ch->path);
480	xpt_bus_deregister(cam_sim_path(ch->sim));
481	cam_sim_free(ch->sim, /*free_devq*/TRUE);
482	mtx_unlock(&ch->mtx);
483
484	bus_teardown_intr(dev, ch->r_irq, ch->ih);
485	bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
486
487	siis_ch_suspend(dev);
488	siis_slotsfree(dev);
489	siis_dmafini(dev);
490
491	bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem);
492	mtx_destroy(&ch->mtx);
493	return (0);
494}
495
496static int
497siis_ch_suspend(device_t dev)
498{
499	struct siis_channel *ch = device_get_softc(dev);
500
501	/* Put port into reset state. */
502	ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PORT_RESET);
503	return (0);
504}
505
506static int
507siis_ch_resume(device_t dev)
508{
509	struct siis_channel *ch = device_get_softc(dev);
510
511	/* Get port out of reset state. */
512	ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PORT_RESET);
513	ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_32BIT);
514	if (ch->pm_present)
515		ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PME);
516	else
517		ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PME);
518	/* Enable port interrupts */
519	ATA_OUTL(ch->r_mem, SIIS_P_IESET, SIIS_P_IX_ENABLED);
520	return (0);
521}
522
523devclass_t siisch_devclass;
524static device_method_t siisch_methods[] = {
525	DEVMETHOD(device_probe,     siis_ch_probe),
526	DEVMETHOD(device_attach,    siis_ch_attach),
527	DEVMETHOD(device_detach,    siis_ch_detach),
528	DEVMETHOD(device_suspend,   siis_ch_suspend),
529	DEVMETHOD(device_resume,    siis_ch_resume),
530	{ 0, 0 }
531};
532static driver_t siisch_driver = {
533        "siisch",
534        siisch_methods,
535        sizeof(struct siis_channel)
536};
537DRIVER_MODULE(siisch, siis, siisch_driver, siis_devclass, 0, 0);
538
539struct siis_dc_cb_args {
540	bus_addr_t maddr;
541	int error;
542};
543
544static void
545siis_dmainit(device_t dev)
546{
547	struct siis_channel *ch = device_get_softc(dev);
548	struct siis_dc_cb_args dcba;
549
550	/* Command area. */
551	if (bus_dma_tag_create(bus_get_dma_tag(dev), 1024, 0,
552	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
553	    NULL, NULL, SIIS_WORK_SIZE, 1, SIIS_WORK_SIZE,
554	    0, NULL, NULL, &ch->dma.work_tag))
555		goto error;
556	if (bus_dmamem_alloc(ch->dma.work_tag, (void **)&ch->dma.work, 0,
557	    &ch->dma.work_map))
558		goto error;
559	if (bus_dmamap_load(ch->dma.work_tag, ch->dma.work_map, ch->dma.work,
560	    SIIS_WORK_SIZE, siis_dmasetupc_cb, &dcba, 0) || dcba.error) {
561		bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map);
562		goto error;
563	}
564	ch->dma.work_bus = dcba.maddr;
565	/* Data area. */
566	if (bus_dma_tag_create(bus_get_dma_tag(dev), 2, 0,
567	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
568	    NULL, NULL,
569	    SIIS_SG_ENTRIES * PAGE_SIZE * SIIS_MAX_SLOTS,
570	    SIIS_SG_ENTRIES, 0xFFFFFFFF,
571	    0, busdma_lock_mutex, &ch->mtx, &ch->dma.data_tag)) {
572		goto error;
573	}
574	return;
575
576error:
577	device_printf(dev, "WARNING - DMA initialization failed\n");
578	siis_dmafini(dev);
579}
580
581static void
582siis_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
583{
584	struct siis_dc_cb_args *dcba = (struct siis_dc_cb_args *)xsc;
585
586	if (!(dcba->error = error))
587		dcba->maddr = segs[0].ds_addr;
588}
589
590static void
591siis_dmafini(device_t dev)
592{
593	struct siis_channel *ch = device_get_softc(dev);
594
595	if (ch->dma.data_tag) {
596		bus_dma_tag_destroy(ch->dma.data_tag);
597		ch->dma.data_tag = NULL;
598	}
599	if (ch->dma.work_bus) {
600		bus_dmamap_unload(ch->dma.work_tag, ch->dma.work_map);
601		bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map);
602		ch->dma.work_bus = 0;
603		ch->dma.work_map = NULL;
604		ch->dma.work = NULL;
605	}
606	if (ch->dma.work_tag) {
607		bus_dma_tag_destroy(ch->dma.work_tag);
608		ch->dma.work_tag = NULL;
609	}
610}
611
612static void
613siis_slotsalloc(device_t dev)
614{
615	struct siis_channel *ch = device_get_softc(dev);
616	int i;
617
618	/* Alloc and setup command/dma slots */
619	bzero(ch->slot, sizeof(ch->slot));
620	for (i = 0; i < SIIS_MAX_SLOTS; i++) {
621		struct siis_slot *slot = &ch->slot[i];
622
623		slot->dev = dev;
624		slot->slot = i;
625		slot->state = SIIS_SLOT_EMPTY;
626		slot->ccb = NULL;
627		callout_init_mtx(&slot->timeout, &ch->mtx, 0);
628
629		if (bus_dmamap_create(ch->dma.data_tag, 0, &slot->dma.data_map))
630			device_printf(ch->dev, "FAILURE - create data_map\n");
631	}
632}
633
634static void
635siis_slotsfree(device_t dev)
636{
637	struct siis_channel *ch = device_get_softc(dev);
638	int i;
639
640	/* Free all dma slots */
641	for (i = 0; i < SIIS_MAX_SLOTS; i++) {
642		struct siis_slot *slot = &ch->slot[i];
643
644		callout_drain(&slot->timeout);
645		if (slot->dma.data_map) {
646			bus_dmamap_destroy(ch->dma.data_tag, slot->dma.data_map);
647			slot->dma.data_map = NULL;
648		}
649	}
650}
651
652static void
653siis_notify_events(device_t dev)
654{
655	struct siis_channel *ch = device_get_softc(dev);
656	struct cam_path *dpath;
657	u_int32_t status;
658	int i;
659
660	status = ATA_INL(ch->r_mem, SIIS_P_SNTF);
661	ATA_OUTL(ch->r_mem, SIIS_P_SNTF, status);
662	if (bootverbose)
663		device_printf(dev, "SNTF 0x%04x\n", status);
664	for (i = 0; i < 16; i++) {
665		if ((status & (1 << i)) == 0)
666			continue;
667		if (xpt_create_path(&dpath, NULL,
668		    xpt_path_path_id(ch->path), i, 0) == CAM_REQ_CMP) {
669			xpt_async(AC_SCSI_AEN, dpath, NULL);
670			xpt_free_path(dpath);
671		}
672	}
673
674}
675
676static void
677siis_phy_check_events(device_t dev)
678{
679	struct siis_channel *ch = device_get_softc(dev);
680
681	/* If we have a connection event, deal with it */
682	if (ch->pm_level == 0) {
683		u_int32_t status = ATA_INL(ch->r_mem, SIIS_P_SSTS);
684		if (((status & ATA_SS_DET_MASK) == ATA_SS_DET_PHY_ONLINE) &&
685		    ((status & ATA_SS_SPD_MASK) != ATA_SS_SPD_NO_SPEED) &&
686		    ((status & ATA_SS_IPM_MASK) == ATA_SS_IPM_ACTIVE)) {
687			if (bootverbose)
688				device_printf(dev, "CONNECT requested\n");
689			siis_reset(dev);
690		} else {
691			if (bootverbose)
692				device_printf(dev, "DISCONNECT requested\n");
693			ch->devices = 0;
694		}
695	}
696}
697
698static void
699siis_ch_intr_locked(void *data)
700{
701	device_t dev = (device_t)data;
702	struct siis_channel *ch = device_get_softc(dev);
703
704	mtx_lock(&ch->mtx);
705	siis_ch_intr(data);
706	mtx_unlock(&ch->mtx);
707}
708
709static void
710siis_ch_intr(void *data)
711{
712	device_t dev = (device_t)data;
713	struct siis_channel *ch = device_get_softc(dev);
714	uint32_t istatus, sstatus, ctx, estatus, ok, err = 0;
715	enum siis_err_type et;
716	int i, ccs, port, tslots;
717
718	mtx_assert(&ch->mtx, MA_OWNED);
719	/* Read command statuses. */
720	sstatus = ATA_INL(ch->r_mem, SIIS_P_SS);
721	ok = ch->rslots & ~sstatus;
722	/* Complete all successfull commands. */
723	for (i = 0; i < SIIS_MAX_SLOTS; i++) {
724		if ((ok >> i) & 1)
725			siis_end_transaction(&ch->slot[i], SIIS_ERR_NONE);
726	}
727	/* Do we have any other events? */
728	if ((sstatus & SIIS_P_SS_ATTN) == 0)
729		return;
730	/* Read and clear interrupt statuses. */
731	istatus = ATA_INL(ch->r_mem, SIIS_P_IS) &
732	    (0xFFFF & ~SIIS_P_IX_COMMCOMP);
733	ATA_OUTL(ch->r_mem, SIIS_P_IS, istatus);
734	/* Process PHY events */
735	if (istatus & SIIS_P_IX_PHYRDYCHG)
736		siis_phy_check_events(dev);
737	/* Process NOTIFY events */
738	if (istatus & SIIS_P_IX_SDBN)
739		siis_notify_events(dev);
740	/* Process command errors */
741	if (istatus & SIIS_P_IX_COMMERR) {
742		estatus = ATA_INL(ch->r_mem, SIIS_P_CMDERR);
743		ctx = ATA_INL(ch->r_mem, SIIS_P_CTX);
744		ccs = (ctx & SIIS_P_CTX_SLOT) >> SIIS_P_CTX_SLOT_SHIFT;
745		port = (ctx & SIIS_P_CTX_PMP) >> SIIS_P_CTX_PMP_SHIFT;
746		err = ch->rslots & sstatus;
747//device_printf(dev, "%s ERROR ss %08x is %08x rs %08x es %d act %d port %d serr %08x\n",
748//    __func__, sstatus, istatus, ch->rslots, estatus, ccs, port,
749//    ATA_INL(ch->r_mem, SIIS_P_SERR));
750
751		if (!ch->readlog && !ch->recovery) {
752			xpt_freeze_simq(ch->sim, ch->numrslots);
753			ch->recovery = 1;
754		}
755		if (ch->frozen) {
756			union ccb *fccb = ch->frozen;
757			ch->frozen = NULL;
758			fccb->ccb_h.status &= ~CAM_STATUS_MASK;
759			fccb->ccb_h.status |= CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ;
760			if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) {
761				xpt_freeze_devq(fccb->ccb_h.path, 1);
762				fccb->ccb_h.status |= CAM_DEV_QFRZN;
763			}
764			xpt_done(fccb);
765		}
766		if (estatus == SIIS_P_CMDERR_DEV ||
767		    estatus == SIIS_P_CMDERR_SDB ||
768		    estatus == SIIS_P_CMDERR_DATAFIS) {
769			tslots = ch->numtslots[port];
770			for (i = 0; i < SIIS_MAX_SLOTS; i++) {
771				/* XXX: requests in loading state. */
772				if (((ch->rslots >> i) & 1) == 0)
773					continue;
774				if (ch->slot[i].ccb->ccb_h.target_id != port)
775					continue;
776				if (tslots == 0) {
777					/* Untagged operation. */
778					if (i == ccs)
779						et = SIIS_ERR_TFE;
780					else
781						et = SIIS_ERR_INNOCENT;
782				} else {
783					/* Tagged operation. */
784					et = SIIS_ERR_NCQ;
785				}
786				siis_end_transaction(&ch->slot[i], et);
787			}
788			/*
789			 * We can't reinit port if there are some other
790			 * commands active, use resume to complete them.
791			 */
792			if (ch->rslots != 0)
793				ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_RESUME);
794		} else {
795			if (estatus == SIIS_P_CMDERR_SENDFIS ||
796			    estatus == SIIS_P_CMDERR_INCSTATE ||
797			    estatus == SIIS_P_CMDERR_PPE ||
798			    estatus == SIIS_P_CMDERR_SERVICE) {
799				et = SIIS_ERR_SATA;
800			} else
801				et = SIIS_ERR_INVALID;
802			for (i = 0; i < SIIS_MAX_SLOTS; i++) {
803				/* XXX: requests in loading state. */
804				if (((ch->rslots >> i) & 1) == 0)
805					continue;
806				siis_end_transaction(&ch->slot[i], et);
807			}
808		}
809	}
810}
811
812/* Must be called with channel locked. */
813static int
814siis_check_collision(device_t dev, union ccb *ccb)
815{
816	struct siis_channel *ch = device_get_softc(dev);
817
818	mtx_assert(&ch->mtx, MA_OWNED);
819	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
820	    (ccb->ataio.cmd.flags & (CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT))) {
821		/* Atomic command while anything active. */
822		if (ch->numrslots != 0)
823			return (1);
824	}
825       /* We have some atomic command running. */
826       if (ch->aslots != 0)
827               return (1);
828	return (0);
829}
830
831/* Must be called with channel locked. */
832static void
833siis_begin_transaction(device_t dev, union ccb *ccb)
834{
835	struct siis_channel *ch = device_get_softc(dev);
836	struct siis_slot *slot;
837	int tag;
838
839	mtx_assert(&ch->mtx, MA_OWNED);
840	/* Choose empty slot. */
841	tag = ch->lastslot;
842	while (ch->slot[tag].state != SIIS_SLOT_EMPTY) {
843		if (++tag >= SIIS_MAX_SLOTS)
844			tag = 0;
845		KASSERT(tag != ch->lastslot, ("siis: ALL SLOTS BUSY!"));
846	}
847	ch->lastslot = tag;
848	/* Occupy chosen slot. */
849	slot = &ch->slot[tag];
850	slot->ccb = ccb;
851	/* Update channel stats. */
852	ch->numrslots++;
853	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
854	    (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) {
855		ch->numtslots[ccb->ccb_h.target_id]++;
856	}
857	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
858	    (ccb->ataio.cmd.flags & (CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT)))
859		ch->aslots |= (1 << slot->slot);
860	slot->dma.nsegs = 0;
861	/* If request moves data, setup and load SG list */
862	if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
863		void *buf;
864		bus_size_t size;
865
866		slot->state = SIIS_SLOT_LOADING;
867		if (ccb->ccb_h.func_code == XPT_ATA_IO) {
868			buf = ccb->ataio.data_ptr;
869			size = ccb->ataio.dxfer_len;
870		} else {
871			buf = ccb->csio.data_ptr;
872			size = ccb->csio.dxfer_len;
873		}
874		bus_dmamap_load(ch->dma.data_tag, slot->dma.data_map,
875		    buf, size, siis_dmasetprd, slot, 0);
876	} else
877		siis_execute_transaction(slot);
878}
879
880/* Locked by busdma engine. */
881static void
882siis_dmasetprd(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
883{
884	struct siis_slot *slot = arg;
885	struct siis_channel *ch = device_get_softc(slot->dev);
886	struct siis_cmd *ctp;
887	struct siis_dma_prd *prd;
888	int i;
889
890	mtx_assert(&ch->mtx, MA_OWNED);
891	if (error) {
892		device_printf(slot->dev, "DMA load error\n");
893		if (!ch->readlog)
894			xpt_freeze_simq(ch->sim, 1);
895		siis_end_transaction(slot, SIIS_ERR_INVALID);
896		return;
897	}
898	KASSERT(nsegs <= SIIS_SG_ENTRIES, ("too many DMA segment entries\n"));
899	/* Get a piece of the workspace for this request */
900	ctp = (struct siis_cmd *)
901		(ch->dma.work + SIIS_CT_OFFSET + (SIIS_CT_SIZE * slot->slot));
902	/* Fill S/G table */
903	if (slot->ccb->ccb_h.func_code == XPT_ATA_IO)
904		prd = &ctp->u.ata.prd[0];
905	else
906		prd = &ctp->u.atapi.prd[0];
907	for (i = 0; i < nsegs; i++) {
908		prd[i].dba = htole64(segs[i].ds_addr);
909		prd[i].dbc = htole32(segs[i].ds_len);
910		prd[i].control = 0;
911	}
912	prd[nsegs - 1].control = htole32(SIIS_PRD_TRM);
913	slot->dma.nsegs = nsegs;
914	bus_dmamap_sync(ch->dma.data_tag, slot->dma.data_map,
915	    ((slot->ccb->ccb_h.flags & CAM_DIR_IN) ?
916	    BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE));
917	siis_execute_transaction(slot);
918}
919
920/* Must be called with channel locked. */
921static void
922siis_execute_transaction(struct siis_slot *slot)
923{
924	device_t dev = slot->dev;
925	struct siis_channel *ch = device_get_softc(dev);
926	struct siis_cmd *ctp;
927	union ccb *ccb = slot->ccb;
928	u_int64_t prb_bus;
929
930	mtx_assert(&ch->mtx, MA_OWNED);
931	/* Get a piece of the workspace for this request */
932	ctp = (struct siis_cmd *)
933		(ch->dma.work + SIIS_CT_OFFSET + (SIIS_CT_SIZE * slot->slot));
934	ctp->control = 0;
935	ctp->protocol_override = 0;
936	ctp->transfer_count = 0;
937	/* Special handling for Soft Reset command. */
938	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
939	    (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL)) {
940		ctp->control |= htole16(SIIS_PRB_SOFT_RESET);
941	} else if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
942		if (ccb->ccb_h.flags & CAM_DIR_IN)
943			ctp->control |= htole16(SIIS_PRB_PACKET_READ);
944		if (ccb->ccb_h.flags & CAM_DIR_OUT)
945			ctp->control |= htole16(SIIS_PRB_PACKET_WRITE);
946	}
947	/* Setup the FIS for this request */
948	if (!siis_setup_fis(ctp, ccb, slot->slot)) {
949		device_printf(ch->dev, "Setting up SATA FIS failed\n");
950		if (!ch->readlog)
951			xpt_freeze_simq(ch->sim, 1);
952		siis_end_transaction(slot, SIIS_ERR_INVALID);
953		return;
954	}
955	bus_dmamap_sync(ch->dma.work_tag, ch->dma.work_map,
956	    BUS_DMASYNC_PREWRITE);
957	/* Issue command to the controller. */
958	slot->state = SIIS_SLOT_RUNNING;
959	ch->rslots |= (1 << slot->slot);
960	prb_bus = ch->dma.work_bus +
961	      SIIS_CT_OFFSET + (SIIS_CT_SIZE * slot->slot);
962	ATA_OUTL(ch->r_mem, SIIS_P_CACTL(slot->slot), prb_bus);
963	ATA_OUTL(ch->r_mem, SIIS_P_CACTH(slot->slot), prb_bus >> 32);
964	/* Start command execution timeout */
965	callout_reset(&slot->timeout, (int)ccb->ccb_h.timeout * hz / 1000,
966	    (timeout_t*)siis_timeout, slot);
967	return;
968}
969
970/* Must be called with channel locked. */
971static void
972siis_process_timeout(device_t dev)
973{
974	struct siis_channel *ch = device_get_softc(dev);
975	int i;
976
977	mtx_assert(&ch->mtx, MA_OWNED);
978	if (!ch->readlog && !ch->recovery) {
979		xpt_freeze_simq(ch->sim, ch->numrslots);
980		ch->recovery = 1;
981	}
982	/* Handle the rest of commands. */
983	for (i = 0; i < SIIS_MAX_SLOTS; i++) {
984		/* Do we have a running request on slot? */
985		if (ch->slot[i].state < SIIS_SLOT_RUNNING)
986			continue;
987		siis_end_transaction(&ch->slot[i], SIIS_ERR_TIMEOUT);
988	}
989}
990
991/* Locked by callout mechanism. */
992static void
993siis_timeout(struct siis_slot *slot)
994{
995	device_t dev = slot->dev;
996	struct siis_channel *ch = device_get_softc(dev);
997
998	mtx_assert(&ch->mtx, MA_OWNED);
999	/* Check for stale timeout. */
1000	if (slot->state < SIIS_SLOT_RUNNING)
1001		return;
1002	device_printf(dev, "Timeout on slot %d\n", slot->slot);
1003device_printf(dev, "%s is %08x ss %08x rs %08x es %08x sts %08x serr %08x\n",
1004    __func__, ATA_INL(ch->r_mem, SIIS_P_IS), ATA_INL(ch->r_mem, SIIS_P_SS), ch->rslots,
1005    ATA_INL(ch->r_mem, SIIS_P_CMDERR), ATA_INL(ch->r_mem, SIIS_P_STS),
1006    ATA_INL(ch->r_mem, SIIS_P_SERR));
1007
1008	if (ch->toslots == 0)
1009		xpt_freeze_simq(ch->sim, 1);
1010	ch->toslots |= (1 << slot->slot);
1011	if ((ch->rslots & ~ch->toslots) == 0)
1012		siis_process_timeout(dev);
1013	else
1014		device_printf(dev, " ... waiting for slots %08x\n",
1015		    ch->rslots & ~ch->toslots);
1016}
1017
1018/* Must be called with channel locked. */
1019static void
1020siis_end_transaction(struct siis_slot *slot, enum siis_err_type et)
1021{
1022	device_t dev = slot->dev;
1023	struct siis_channel *ch = device_get_softc(dev);
1024	union ccb *ccb = slot->ccb;
1025
1026	mtx_assert(&ch->mtx, MA_OWNED);
1027	bus_dmamap_sync(ch->dma.work_tag, ch->dma.work_map,
1028	    BUS_DMASYNC_POSTWRITE);
1029	/* Read result registers to the result struct
1030	 * May be incorrect if several commands finished same time,
1031	 * so read only when sure or have to.
1032	 */
1033	if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1034		struct ata_res *res = &ccb->ataio.res;
1035		if ((et == SIIS_ERR_TFE) ||
1036		    (ccb->ataio.cmd.flags & CAM_ATAIO_NEEDRESULT)) {
1037			int offs = SIIS_P_LRAM_SLOT(slot->slot) + 8;
1038
1039			res->status = ATA_INB(ch->r_mem, offs + 2);
1040			res->error = ATA_INB(ch->r_mem, offs + 3);
1041			res->lba_low = ATA_INB(ch->r_mem, offs + 4);
1042			res->lba_mid = ATA_INB(ch->r_mem, offs + 5);
1043			res->lba_high = ATA_INB(ch->r_mem, offs + 6);
1044			res->device = ATA_INB(ch->r_mem, offs + 7);
1045			res->lba_low_exp = ATA_INB(ch->r_mem, offs + 8);
1046			res->lba_mid_exp = ATA_INB(ch->r_mem, offs + 9);
1047			res->lba_high_exp = ATA_INB(ch->r_mem, offs + 10);
1048			res->sector_count = ATA_INB(ch->r_mem, offs + 12);
1049			res->sector_count_exp = ATA_INB(ch->r_mem, offs + 13);
1050		} else
1051			bzero(res, sizeof(*res));
1052	}
1053	if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1054		bus_dmamap_sync(ch->dma.data_tag, slot->dma.data_map,
1055		    (ccb->ccb_h.flags & CAM_DIR_IN) ?
1056		    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
1057		bus_dmamap_unload(ch->dma.data_tag, slot->dma.data_map);
1058	}
1059	/* Set proper result status. */
1060	if (et != SIIS_ERR_NONE || ch->recovery) {
1061		ch->eslots |= (1 << slot->slot);
1062		ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1063	}
1064	/* In case of error, freeze device for proper recovery. */
1065	if (et != SIIS_ERR_NONE &&
1066	    !(ccb->ccb_h.status & CAM_DEV_QFRZN)) {
1067		xpt_freeze_devq(ccb->ccb_h.path, 1);
1068		ccb->ccb_h.status |= CAM_DEV_QFRZN;
1069	}
1070	ccb->ccb_h.status &= ~CAM_STATUS_MASK;
1071	switch (et) {
1072	case SIIS_ERR_NONE:
1073		ccb->ccb_h.status |= CAM_REQ_CMP;
1074		if (ccb->ccb_h.func_code == XPT_SCSI_IO)
1075			ccb->csio.scsi_status = SCSI_STATUS_OK;
1076		break;
1077	case SIIS_ERR_INVALID:
1078		ch->fatalerr = 1;
1079		ccb->ccb_h.status |= CAM_REQ_INVALID;
1080		break;
1081	case SIIS_ERR_INNOCENT:
1082		ccb->ccb_h.status |= CAM_REQUEUE_REQ;
1083		break;
1084	case SIIS_ERR_TFE:
1085	case SIIS_ERR_NCQ:
1086		if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
1087			ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
1088			ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1089		} else {
1090			ccb->ccb_h.status |= CAM_ATA_STATUS_ERROR;
1091		}
1092		break;
1093	case SIIS_ERR_SATA:
1094		ch->fatalerr = 1;
1095		ccb->ccb_h.status |= CAM_UNCOR_PARITY;
1096		break;
1097	case SIIS_ERR_TIMEOUT:
1098		ch->fatalerr = 1;
1099		ccb->ccb_h.status |= CAM_CMD_TIMEOUT;
1100		break;
1101	default:
1102		ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
1103	}
1104	/* Free slot. */
1105	ch->rslots &= ~(1 << slot->slot);
1106	ch->aslots &= ~(1 << slot->slot);
1107	if (et != SIIS_ERR_TIMEOUT) {
1108		if (ch->toslots == (1 << slot->slot))
1109			xpt_release_simq(ch->sim, TRUE);
1110		ch->toslots &= ~(1 << slot->slot);
1111	}
1112	slot->state = SIIS_SLOT_EMPTY;
1113	slot->ccb = NULL;
1114	/* Update channel stats. */
1115	ch->numrslots--;
1116	if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
1117	    (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) {
1118		ch->numtslots[ccb->ccb_h.target_id]--;
1119	}
1120	/* If it was our READ LOG command - process it. */
1121	if (ch->readlog) {
1122		siis_process_read_log(dev, ccb);
1123	/* If it was NCQ command error, put result on hold. */
1124	} else if (et == SIIS_ERR_NCQ) {
1125		ch->hold[slot->slot] = ccb;
1126		ch->numhslots++;
1127	} else
1128		xpt_done(ccb);
1129	/* Unfreeze frozen command. */
1130	if (ch->frozen && ch->numrslots == 0) {
1131		union ccb *fccb = ch->frozen;
1132		ch->frozen = NULL;
1133		siis_begin_transaction(dev, fccb);
1134		xpt_release_simq(ch->sim, TRUE);
1135	}
1136	/* If we have no other active commands, ... */
1137	if (ch->rslots == 0) {
1138		/* if there were timeouts or fatal error - reset port. */
1139		if (ch->toslots != 0 || ch->fatalerr) {
1140			siis_reset(dev);
1141		} else {
1142			/* if we have slots in error, we can reinit port. */
1143			if (ch->eslots != 0)
1144				siis_portinit(dev);
1145			/* if there commands on hold, we can do READ LOG. */
1146			if (!ch->readlog && ch->numhslots)
1147				siis_issue_read_log(dev);
1148		}
1149	/* If all the reset of commands are in timeout - abort them. */
1150	} else if ((ch->rslots & ~ch->toslots) == 0)
1151		siis_process_timeout(dev);
1152}
1153
1154static void
1155siis_issue_read_log(device_t dev)
1156{
1157	struct siis_channel *ch = device_get_softc(dev);
1158	union ccb *ccb;
1159	struct ccb_ataio *ataio;
1160	int i;
1161
1162	/* Find some holden command. */
1163	for (i = 0; i < SIIS_MAX_SLOTS; i++) {
1164		if (ch->hold[i])
1165			break;
1166	}
1167	if (i == SIIS_MAX_SLOTS)
1168		return;
1169	ch->readlog = 1;
1170	ccb = xpt_alloc_ccb_nowait();
1171	if (ccb == NULL) {
1172		device_printf(dev, "Unable allocate READ LOG command");
1173		return; /* XXX */
1174	}
1175	ccb->ccb_h = ch->hold[i]->ccb_h;	/* Reuse old header. */
1176	ccb->ccb_h.func_code = XPT_ATA_IO;
1177	ccb->ccb_h.flags = CAM_DIR_IN;
1178	ccb->ccb_h.timeout = 1000;	/* 1s should be enough. */
1179	ataio = &ccb->ataio;
1180	ataio->data_ptr = malloc(512, M_SIIS, M_NOWAIT);
1181	if (ataio->data_ptr == NULL) {
1182		device_printf(dev, "Unable allocate memory for READ LOG command");
1183		return; /* XXX */
1184	}
1185	ataio->dxfer_len = 512;
1186	bzero(&ataio->cmd, sizeof(ataio->cmd));
1187	ataio->cmd.flags = CAM_ATAIO_48BIT;
1188	ataio->cmd.command = 0x2F;	/* READ LOG EXT */
1189	ataio->cmd.sector_count = 1;
1190	ataio->cmd.sector_count_exp = 0;
1191	ataio->cmd.lba_low = 0x10;
1192	ataio->cmd.lba_mid = 0;
1193	ataio->cmd.lba_mid_exp = 0;
1194	siis_begin_transaction(dev, ccb);
1195}
1196
1197static void
1198siis_process_read_log(device_t dev, union ccb *ccb)
1199{
1200	struct siis_channel *ch = device_get_softc(dev);
1201	uint8_t *data;
1202	struct ata_res *res;
1203	int i;
1204
1205	ch->readlog = 0;
1206	data = ccb->ataio.data_ptr;
1207	if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP &&
1208	    (data[0] & 0x80) == 0) {
1209		for (i = 0; i < SIIS_MAX_SLOTS; i++) {
1210			if (!ch->hold[i])
1211				continue;
1212			if (ch->hold[i]->ccb_h.target_id != ccb->ccb_h.target_id)
1213				continue;
1214			if ((data[0] & 0x1F) == i) {
1215				res = &ch->hold[i]->ataio.res;
1216				res->status = data[2];
1217				res->error = data[3];
1218				res->lba_low = data[4];
1219				res->lba_mid = data[5];
1220				res->lba_high = data[6];
1221				res->device = data[7];
1222				res->lba_low_exp = data[8];
1223				res->lba_mid_exp = data[9];
1224				res->lba_high_exp = data[10];
1225				res->sector_count = data[12];
1226				res->sector_count_exp = data[13];
1227			} else {
1228				ch->hold[i]->ccb_h.status &= ~CAM_STATUS_MASK;
1229				ch->hold[i]->ccb_h.status |= CAM_REQUEUE_REQ;
1230			}
1231			xpt_done(ch->hold[i]);
1232			ch->hold[i] = NULL;
1233			ch->numhslots--;
1234		}
1235	} else {
1236		if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
1237			device_printf(dev, "Error while READ LOG EXT\n");
1238		else if ((data[0] & 0x80) == 0) {
1239			device_printf(dev, "Non-queued command error in READ LOG EXT\n");
1240		}
1241		for (i = 0; i < SIIS_MAX_SLOTS; i++) {
1242			if (!ch->hold[i])
1243				continue;
1244			if (ch->hold[i]->ccb_h.target_id != ccb->ccb_h.target_id)
1245				continue;
1246			xpt_done(ch->hold[i]);
1247			ch->hold[i] = NULL;
1248			ch->numhslots--;
1249		}
1250	}
1251	free(ccb->ataio.data_ptr, M_SIIS);
1252	xpt_free_ccb(ccb);
1253}
1254
1255static void
1256siis_portinit(device_t dev)
1257{
1258	struct siis_channel *ch = device_get_softc(dev);
1259	int i;
1260
1261	ch->eslots = 0;
1262	ch->recovery = 0;
1263	ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_RESUME);
1264	for (i = 0; i < 16; i++) {
1265		ATA_OUTL(ch->r_mem, SIIS_P_PMPSTS(i), 0),
1266		ATA_OUTL(ch->r_mem, SIIS_P_PMPQACT(i), 0);
1267	}
1268	ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PORT_INIT);
1269	siis_wait_ready(dev, 1000);
1270}
1271
1272static int
1273siis_devreset(device_t dev)
1274{
1275	struct siis_channel *ch = device_get_softc(dev);
1276	int timeout = 0;
1277	uint32_t val;
1278
1279	ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_DEV_RESET);
1280	while (((val = ATA_INL(ch->r_mem, SIIS_P_STS)) &
1281	    SIIS_P_CTL_DEV_RESET) != 0) {
1282		DELAY(1000);
1283		if (timeout++ > 100) {
1284			device_printf(dev, "device reset stuck (timeout %dms) "
1285			    "status = %08x\n", timeout, val);
1286			return (EBUSY);
1287		}
1288	}
1289	if (bootverbose)
1290		device_printf(dev, "device reset time=%dms\n", timeout);
1291	return (0);
1292}
1293
1294static int
1295siis_wait_ready(device_t dev, int t)
1296{
1297	struct siis_channel *ch = device_get_softc(dev);
1298	int timeout = 0;
1299	uint32_t val;
1300
1301	while (((val = ATA_INL(ch->r_mem, SIIS_P_STS)) &
1302	    SIIS_P_CTL_READY) == 0) {
1303		DELAY(1000);
1304		if (timeout++ > t) {
1305			device_printf(dev, "port is not ready (timeout %dms) "
1306			    "status = %08x\n", t, val);
1307			return (EBUSY);
1308		}
1309	}
1310	if (bootverbose)
1311		device_printf(dev, "ready wait time=%dms\n", timeout);
1312	return (0);
1313}
1314
1315static void
1316siis_reset(device_t dev)
1317{
1318	struct siis_channel *ch = device_get_softc(dev);
1319	int i, retry = 0;
1320	uint32_t val;
1321
1322	if (bootverbose)
1323		device_printf(dev, "SIIS reset...\n");
1324	if (!ch->readlog && !ch->recovery)
1325		xpt_freeze_simq(ch->sim, ch->numrslots);
1326	/* Requeue frozen command. */
1327	if (ch->frozen) {
1328		union ccb *fccb = ch->frozen;
1329		ch->frozen = NULL;
1330		fccb->ccb_h.status &= ~CAM_STATUS_MASK;
1331		fccb->ccb_h.status |= CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ;
1332		if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) {
1333			xpt_freeze_devq(fccb->ccb_h.path, 1);
1334			fccb->ccb_h.status |= CAM_DEV_QFRZN;
1335		}
1336		xpt_done(fccb);
1337	}
1338	/* Requeue all running commands. */
1339	for (i = 0; i < SIIS_MAX_SLOTS; i++) {
1340		/* Do we have a running request on slot? */
1341		if (ch->slot[i].state < SIIS_SLOT_RUNNING)
1342			continue;
1343		/* XXX; Commands in loading state. */
1344		siis_end_transaction(&ch->slot[i], SIIS_ERR_INNOCENT);
1345	}
1346	/* Finish all holden commands as-is. */
1347	for (i = 0; i < SIIS_MAX_SLOTS; i++) {
1348		if (!ch->hold[i])
1349			continue;
1350		xpt_done(ch->hold[i]);
1351		ch->hold[i] = NULL;
1352		ch->numhslots--;
1353	}
1354	if (ch->toslots != 0)
1355		xpt_release_simq(ch->sim, TRUE);
1356	ch->eslots = 0;
1357	ch->recovery = 0;
1358	ch->toslots = 0;
1359	ch->fatalerr = 0;
1360	/* Disable port interrupts */
1361	ATA_OUTL(ch->r_mem, SIIS_P_IECLR, 0x0000FFFF);
1362	/* Set speed limit. */
1363	if (ch->sata_rev == 1)
1364		val = ATA_SC_SPD_SPEED_GEN1;
1365	else if (ch->sata_rev == 2)
1366		val = ATA_SC_SPD_SPEED_GEN2;
1367	else if (ch->sata_rev == 3)
1368		val = ATA_SC_SPD_SPEED_GEN3;
1369	else
1370		val = 0;
1371	ATA_OUTL(ch->r_mem, SIIS_P_SCTL,
1372	    ATA_SC_DET_IDLE | val | ((ch->pm_level > 0) ? 0 :
1373	    (ATA_SC_IPM_DIS_PARTIAL | ATA_SC_IPM_DIS_SLUMBER)));
1374retry:
1375	siis_devreset(dev);
1376	/* Reset and reconnect PHY, */
1377	if (!siis_sata_connect(ch)) {
1378		ch->devices = 0;
1379		/* Enable port interrupts */
1380		ATA_OUTL(ch->r_mem, SIIS_P_IESET, SIIS_P_IX_ENABLED);
1381		if (bootverbose)
1382			device_printf(dev,
1383			    "SIIS reset done: phy reset found no device\n");
1384		/* Tell the XPT about the event */
1385		xpt_async(AC_BUS_RESET, ch->path, NULL);
1386		return;
1387	}
1388	/* Wait for clearing busy status. */
1389	if (siis_wait_ready(dev, 10000)) {
1390		device_printf(dev, "device ready timeout\n");
1391		if (!retry) {
1392			device_printf(dev, "trying full port reset ...\n");
1393			/* Get port to the reset state. */
1394			ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PORT_RESET);
1395			DELAY(10000);
1396			/* Get port out of reset state. */
1397			ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PORT_RESET);
1398			ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_32BIT);
1399			if (ch->pm_present)
1400				ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PME);
1401			else
1402				ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PME);
1403			siis_wait_ready(dev, 5000);
1404			retry = 1;
1405			goto retry;
1406		}
1407	}
1408	ch->devices = 1;
1409	/* Enable port interrupts */
1410	ATA_OUTL(ch->r_mem, SIIS_P_IS, 0xFFFFFFFF);
1411	ATA_OUTL(ch->r_mem, SIIS_P_IESET, SIIS_P_IX_ENABLED);
1412	if (bootverbose)
1413		device_printf(dev, "SIIS reset done: devices=%08x\n", ch->devices);
1414	/* Tell the XPT about the event */
1415	xpt_async(AC_BUS_RESET, ch->path, NULL);
1416}
1417
1418static int
1419siis_setup_fis(struct siis_cmd *ctp, union ccb *ccb, int tag)
1420{
1421	u_int8_t *fis = &ctp->fis[0];
1422
1423	bzero(fis, 24);
1424	fis[0] = 0x27;  		/* host to device */
1425	fis[1] = (ccb->ccb_h.target_id & 0x0f);
1426	if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
1427		fis[1] |= 0x80;
1428		fis[2] = ATA_PACKET_CMD;
1429		if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE)
1430			fis[3] = ATA_F_DMA;
1431		else {
1432			fis[5] = ccb->csio.dxfer_len;
1433		        fis[6] = ccb->csio.dxfer_len >> 8;
1434		}
1435		fis[7] = ATA_D_LBA;
1436		fis[15] = ATA_A_4BIT;
1437		bzero(ctp->u.atapi.ccb, 16);
1438		bcopy((ccb->ccb_h.flags & CAM_CDB_POINTER) ?
1439		    ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes,
1440		    ctp->u.atapi.ccb, ccb->csio.cdb_len);
1441	} else if ((ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) == 0) {
1442		fis[1] |= 0x80;
1443		fis[2] = ccb->ataio.cmd.command;
1444		fis[3] = ccb->ataio.cmd.features;
1445		fis[4] = ccb->ataio.cmd.lba_low;
1446		fis[5] = ccb->ataio.cmd.lba_mid;
1447		fis[6] = ccb->ataio.cmd.lba_high;
1448		fis[7] = ccb->ataio.cmd.device;
1449		fis[8] = ccb->ataio.cmd.lba_low_exp;
1450		fis[9] = ccb->ataio.cmd.lba_mid_exp;
1451		fis[10] = ccb->ataio.cmd.lba_high_exp;
1452		fis[11] = ccb->ataio.cmd.features_exp;
1453		if (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA) {
1454			fis[12] = tag << 3;
1455			fis[13] = 0;
1456		} else {
1457			fis[12] = ccb->ataio.cmd.sector_count;
1458			fis[13] = ccb->ataio.cmd.sector_count_exp;
1459		}
1460		fis[15] = ATA_A_4BIT;
1461	} else {
1462		/* Soft reset. */
1463	}
1464	return (20);
1465}
1466
1467static int
1468siis_sata_connect(struct siis_channel *ch)
1469{
1470	u_int32_t status;
1471	int timeout;
1472
1473	/* Wait up to 100ms for "connect well" */
1474	for (timeout = 0; timeout < 100 ; timeout++) {
1475		status = ATA_INL(ch->r_mem, SIIS_P_SSTS);
1476		if (((status & ATA_SS_DET_MASK) == ATA_SS_DET_PHY_ONLINE) &&
1477		    ((status & ATA_SS_SPD_MASK) != ATA_SS_SPD_NO_SPEED) &&
1478		    ((status & ATA_SS_IPM_MASK) == ATA_SS_IPM_ACTIVE))
1479			break;
1480		DELAY(1000);
1481	}
1482	if (timeout >= 100) {
1483		if (bootverbose) {
1484			device_printf(ch->dev, "SATA connect timeout status=%08x\n",
1485			    status);
1486		}
1487		return (0);
1488	}
1489	if (bootverbose) {
1490		device_printf(ch->dev, "SATA connect time=%dms status=%08x\n",
1491		    timeout, status);
1492	}
1493	/* Clear SATA error register */
1494	ATA_OUTL(ch->r_mem, SIIS_P_SERR, 0xffffffff);
1495	return (1);
1496}
1497
1498static void
1499siisaction(struct cam_sim *sim, union ccb *ccb)
1500{
1501	device_t dev;
1502	struct siis_channel *ch;
1503
1504	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("siisaction func_code=%x\n",
1505	    ccb->ccb_h.func_code));
1506
1507	ch = (struct siis_channel *)cam_sim_softc(sim);
1508	dev = ch->dev;
1509	mtx_assert(&ch->mtx, MA_OWNED);
1510	switch (ccb->ccb_h.func_code) {
1511	/* Common cases first */
1512	case XPT_ATA_IO:	/* Execute the requested I/O operation */
1513	case XPT_SCSI_IO:
1514		if (ch->devices == 0) {
1515			ccb->ccb_h.status = CAM_SEL_TIMEOUT;
1516			xpt_done(ccb);
1517			break;
1518		}
1519		/* Check for command collision. */
1520		if (siis_check_collision(dev, ccb)) {
1521			/* Freeze command. */
1522			ch->frozen = ccb;
1523			/* We have only one frozen slot, so freeze simq also. */
1524			xpt_freeze_simq(ch->sim, 1);
1525			return;
1526		}
1527		siis_begin_transaction(dev, ccb);
1528		break;
1529	case XPT_EN_LUN:		/* Enable LUN as a target */
1530	case XPT_TARGET_IO:		/* Execute target I/O request */
1531	case XPT_ACCEPT_TARGET_IO:	/* Accept Host Target Mode CDB */
1532	case XPT_CONT_TARGET_IO:	/* Continue Host Target I/O Connection*/
1533	case XPT_ABORT:			/* Abort the specified CCB */
1534		/* XXX Implement */
1535		ccb->ccb_h.status = CAM_REQ_INVALID;
1536		xpt_done(ccb);
1537		break;
1538	case XPT_SET_TRAN_SETTINGS:
1539	{
1540		struct	ccb_trans_settings *cts = &ccb->cts;
1541
1542		if (cts->xport_specific.sata.valid & CTS_SATA_VALID_PM) {
1543			ch->pm_present = cts->xport_specific.sata.pm_present;
1544			if (ch->pm_present)
1545				ATA_OUTL(ch->r_mem, SIIS_P_CTLSET, SIIS_P_CTL_PME);
1546			else
1547				ATA_OUTL(ch->r_mem, SIIS_P_CTLCLR, SIIS_P_CTL_PME);
1548		}
1549		ccb->ccb_h.status = CAM_REQ_CMP;
1550		xpt_done(ccb);
1551		break;
1552	}
1553	case XPT_GET_TRAN_SETTINGS:
1554	/* Get default/user set transfer settings for the target */
1555	{
1556		struct	ccb_trans_settings *cts = &ccb->cts;
1557		uint32_t status;
1558
1559		cts->protocol = PROTO_ATA;
1560		cts->protocol_version = PROTO_VERSION_UNSPECIFIED;
1561		cts->transport = XPORT_SATA;
1562		cts->transport_version = XPORT_VERSION_UNSPECIFIED;
1563		cts->proto_specific.valid = 0;
1564		cts->xport_specific.sata.valid = 0;
1565		if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
1566			status = ATA_INL(ch->r_mem, SIIS_P_SSTS) & ATA_SS_SPD_MASK;
1567		else
1568			status = ATA_INL(ch->r_mem, SIIS_P_SCTL) & ATA_SC_SPD_MASK;
1569		if (status & ATA_SS_SPD_GEN3) {
1570			cts->xport_specific.sata.bitrate = 600000;
1571			cts->xport_specific.sata.valid |= CTS_SATA_VALID_SPEED;
1572		} else if (status & ATA_SS_SPD_GEN2) {
1573			cts->xport_specific.sata.bitrate = 300000;
1574			cts->xport_specific.sata.valid |= CTS_SATA_VALID_SPEED;
1575		} else if (status & ATA_SS_SPD_GEN1) {
1576			cts->xport_specific.sata.bitrate = 150000;
1577			cts->xport_specific.sata.valid |= CTS_SATA_VALID_SPEED;
1578		}
1579		cts->xport_specific.sata.pm_present = ch->pm_present;
1580		cts->xport_specific.sata.valid |= CTS_SATA_VALID_PM;
1581		ccb->ccb_h.status = CAM_REQ_CMP;
1582		xpt_done(ccb);
1583		break;
1584	}
1585#if 0
1586	case XPT_CALC_GEOMETRY:
1587	{
1588		struct	  ccb_calc_geometry *ccg;
1589		uint32_t size_mb;
1590		uint32_t secs_per_cylinder;
1591
1592		ccg = &ccb->ccg;
1593		size_mb = ccg->volume_size
1594			/ ((1024L * 1024L) / ccg->block_size);
1595		if (size_mb >= 1024 && (aha->extended_trans != 0)) {
1596			if (size_mb >= 2048) {
1597				ccg->heads = 255;
1598				ccg->secs_per_track = 63;
1599			} else {
1600				ccg->heads = 128;
1601				ccg->secs_per_track = 32;
1602			}
1603		} else {
1604			ccg->heads = 64;
1605			ccg->secs_per_track = 32;
1606		}
1607		secs_per_cylinder = ccg->heads * ccg->secs_per_track;
1608		ccg->cylinders = ccg->volume_size / secs_per_cylinder;
1609		ccb->ccb_h.status = CAM_REQ_CMP;
1610		xpt_done(ccb);
1611		break;
1612	}
1613#endif
1614	case XPT_RESET_BUS:		/* Reset the specified SCSI bus */
1615	case XPT_RESET_DEV:	/* Bus Device Reset the specified SCSI device */
1616		siis_reset(dev);
1617		ccb->ccb_h.status = CAM_REQ_CMP;
1618		xpt_done(ccb);
1619		break;
1620	case XPT_TERM_IO:		/* Terminate the I/O process */
1621		/* XXX Implement */
1622		ccb->ccb_h.status = CAM_REQ_INVALID;
1623		xpt_done(ccb);
1624		break;
1625	case XPT_PATH_INQ:		/* Path routing inquiry */
1626	{
1627		struct ccb_pathinq *cpi = &ccb->cpi;
1628
1629		cpi->version_num = 1; /* XXX??? */
1630		cpi->hba_inquiry = PI_SDTR_ABLE | PI_TAG_ABLE;
1631		cpi->hba_inquiry |= PI_SATAPM;
1632		cpi->target_sprt = 0;
1633		cpi->hba_misc = PIM_SEQSCAN;
1634		cpi->hba_eng_cnt = 0;
1635		cpi->max_target = 15;
1636		cpi->max_lun = 0;
1637		cpi->initiator_id = 0;
1638		cpi->bus_id = cam_sim_bus(sim);
1639		cpi->base_transfer_speed = 150000;
1640		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1641		strncpy(cpi->hba_vid, "SIIS", HBA_IDLEN);
1642		strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1643		cpi->unit_number = cam_sim_unit(sim);
1644		cpi->transport = XPORT_SATA;
1645		cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
1646		cpi->protocol = PROTO_ATA;
1647		cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
1648		cpi->ccb_h.status = CAM_REQ_CMP;
1649		cpi->maxio = MAXPHYS;
1650		xpt_done(ccb);
1651		break;
1652	}
1653	default:
1654		ccb->ccb_h.status = CAM_REQ_INVALID;
1655		xpt_done(ccb);
1656		break;
1657	}
1658}
1659
1660static void
1661siispoll(struct cam_sim *sim)
1662{
1663	struct siis_channel *ch = (struct siis_channel *)cam_sim_softc(sim);
1664
1665	siis_ch_intr(ch->dev);
1666}
1667