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