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