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