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