2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2010 Alexander Motin <mav@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/module.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
39 #include <sys/endian.h>
40 #include <sys/malloc.h>
42 #include <sys/mutex.h>
44 #include <machine/stdarg.h>
45 #include <machine/resource.h>
46 #include <machine/bus.h>
48 #include <dev/pci/pcivar.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>
57 /* local prototypes */
58 static int mvs_ch_init(device_t dev);
59 static int mvs_ch_deinit(device_t dev);
60 static int mvs_ch_suspend(device_t dev);
61 static int mvs_ch_resume(device_t dev);
62 static void mvs_dmainit(device_t dev);
63 static void mvs_dmasetupc_cb(void *xsc,
64 bus_dma_segment_t *segs, int nsegs, int error);
65 static void mvs_dmafini(device_t dev);
66 static void mvs_slotsalloc(device_t dev);
67 static void mvs_slotsfree(device_t dev);
68 static void mvs_setup_edma_queues(device_t dev);
69 static void mvs_set_edma_mode(device_t dev, enum mvs_edma_mode mode);
70 static void mvs_ch_pm(void *arg);
71 static void mvs_ch_intr_locked(void *data);
72 static void mvs_ch_intr(void *data);
73 static void mvs_reset(device_t dev);
74 static void mvs_softreset(device_t dev, union ccb *ccb);
76 static int mvs_sata_connect(struct mvs_channel *ch);
77 static int mvs_sata_phy_reset(device_t dev);
78 static int mvs_wait(device_t dev, u_int s, u_int c, int t);
79 static void mvs_tfd_read(device_t dev, union ccb *ccb);
80 static void mvs_tfd_write(device_t dev, union ccb *ccb);
81 static void mvs_legacy_intr(device_t dev, int poll);
82 static void mvs_crbq_intr(device_t dev);
83 static void mvs_begin_transaction(device_t dev, union ccb *ccb);
84 static void mvs_legacy_execute_transaction(struct mvs_slot *slot);
85 static void mvs_timeout(void *arg);
86 static void mvs_dmasetprd(void *arg,
87 bus_dma_segment_t *segs, int nsegs, int error);
88 static void mvs_requeue_frozen(device_t dev);
89 static void mvs_execute_transaction(struct mvs_slot *slot);
90 static void mvs_end_transaction(struct mvs_slot *slot, enum mvs_err_type et);
92 static void mvs_issue_recovery(device_t dev);
93 static void mvs_process_read_log(device_t dev, union ccb *ccb);
94 static void mvs_process_request_sense(device_t dev, union ccb *ccb);
96 static void mvsaction(struct cam_sim *sim, union ccb *ccb);
97 static void mvspoll(struct cam_sim *sim);
99 static MALLOC_DEFINE(M_MVS, "MVS driver", "MVS driver data buffers");
101 #define recovery_type spriv_field0
102 #define RECOVERY_NONE 0
103 #define RECOVERY_READ_LOG 1
104 #define RECOVERY_REQUEST_SENSE 2
105 #define recovery_slot spriv_field1
108 mvs_ch_probe(device_t dev)
111 device_set_desc_copy(dev, "Marvell SATA channel");
112 return (BUS_PROBE_DEFAULT);
116 mvs_ch_attach(device_t dev)
118 struct mvs_controller *ctlr = device_get_softc(device_get_parent(dev));
119 struct mvs_channel *ch = device_get_softc(dev);
120 struct cam_devq *devq;
121 int rid, error, i, sata_rev = 0;
124 ch->unit = (intptr_t)device_get_ivars(dev);
125 ch->quirks = ctlr->quirks;
126 mtx_init(&ch->mtx, "MVS channel lock", NULL, MTX_DEF);
128 resource_int_value(device_get_name(dev),
129 device_get_unit(dev), "pm_level", &ch->pm_level);
130 if (ch->pm_level > 3)
131 callout_init_mtx(&ch->pm_timer, &ch->mtx, 0);
132 callout_init_mtx(&ch->reset_timer, &ch->mtx, 0);
133 resource_int_value(device_get_name(dev),
134 device_get_unit(dev), "sata_rev", &sata_rev);
135 for (i = 0; i < 16; i++) {
136 ch->user[i].revision = sata_rev;
137 ch->user[i].mode = 0;
138 ch->user[i].bytecount = (ch->quirks & MVS_Q_GENIIE) ? 8192 : 2048;
139 ch->user[i].tags = MVS_MAX_SLOTS;
140 ch->curr[i] = ch->user[i];
142 ch->user[i].caps = CTS_SATA_CAPS_H_PMREQ |
143 CTS_SATA_CAPS_H_APST |
144 CTS_SATA_CAPS_D_PMREQ | CTS_SATA_CAPS_D_APST;
146 ch->user[i].caps |= CTS_SATA_CAPS_H_AN;
149 if (!(ch->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
157 if (!(ch->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
158 &rid, RF_SHAREABLE | RF_ACTIVE))) {
159 device_printf(dev, "Unable to map interrupt\n");
163 if ((bus_setup_intr(dev, ch->r_irq, ATA_INTR_FLAGS, NULL,
164 mvs_ch_intr_locked, dev, &ch->ih))) {
165 device_printf(dev, "Unable to setup interrupt\n");
169 /* Create the device queue for our SIM. */
170 devq = cam_simq_alloc(MVS_MAX_SLOTS - 1);
172 device_printf(dev, "Unable to allocate simq\n");
176 /* Construct SIM entry */
177 ch->sim = cam_sim_alloc(mvsaction, mvspoll, "mvsch", ch,
178 device_get_unit(dev), &ch->mtx,
179 2, (ch->quirks & MVS_Q_GENI) ? 0 : MVS_MAX_SLOTS - 1,
181 if (ch->sim == NULL) {
183 device_printf(dev, "unable to allocate sim\n");
187 if (xpt_bus_register(ch->sim, dev, 0) != CAM_SUCCESS) {
188 device_printf(dev, "unable to register xpt bus\n");
192 if (xpt_create_path(&ch->path, /*periph*/NULL, cam_sim_path(ch->sim),
193 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
194 device_printf(dev, "unable to create path\n");
198 if (ch->pm_level > 3) {
199 callout_reset(&ch->pm_timer,
200 (ch->pm_level == 4) ? hz / 1000 : hz / 8,
203 mtx_unlock(&ch->mtx);
207 xpt_bus_deregister(cam_sim_path(ch->sim));
209 cam_sim_free(ch->sim, /*free_devq*/TRUE);
211 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
213 bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem);
214 mtx_unlock(&ch->mtx);
215 mtx_destroy(&ch->mtx);
220 mvs_ch_detach(device_t dev)
222 struct mvs_channel *ch = device_get_softc(dev);
225 xpt_async(AC_LOST_DEVICE, ch->path, NULL);
226 /* Forget about reset. */
229 xpt_release_simq(ch->sim, TRUE);
231 xpt_free_path(ch->path);
232 xpt_bus_deregister(cam_sim_path(ch->sim));
233 cam_sim_free(ch->sim, /*free_devq*/TRUE);
234 mtx_unlock(&ch->mtx);
236 if (ch->pm_level > 3)
237 callout_drain(&ch->pm_timer);
238 callout_drain(&ch->reset_timer);
239 bus_teardown_intr(dev, ch->r_irq, ch->ih);
240 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
246 bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem);
247 mtx_destroy(&ch->mtx);
252 mvs_ch_init(device_t dev)
254 struct mvs_channel *ch = device_get_softc(dev);
257 /* Disable port interrupts */
258 ATA_OUTL(ch->r_mem, EDMA_IEM, 0);
260 ch->curr_mode = MVS_EDMA_UNKNOWN;
261 mvs_set_edma_mode(dev, MVS_EDMA_OFF);
262 /* Clear and configure FIS interrupts. */
263 ATA_OUTL(ch->r_mem, SATA_FISIC, 0);
264 reg = ATA_INL(ch->r_mem, SATA_FISC);
265 reg |= SATA_FISC_FISWAIT4HOSTRDYEN_B1;
266 ATA_OUTL(ch->r_mem, SATA_FISC, reg);
267 reg = ATA_INL(ch->r_mem, SATA_FISIM);
268 reg |= SATA_FISC_FISWAIT4HOSTRDYEN_B1;
269 ATA_OUTL(ch->r_mem, SATA_FISC, reg);
270 /* Clear SATA error register. */
271 ATA_OUTL(ch->r_mem, SATA_SE, 0xffffffff);
272 /* Clear any outstanding error interrupts. */
273 ATA_OUTL(ch->r_mem, EDMA_IEC, 0);
274 /* Unmask all error interrupts */
275 ATA_OUTL(ch->r_mem, EDMA_IEM, ~EDMA_IE_TRANSIENT);
280 mvs_ch_deinit(device_t dev)
282 struct mvs_channel *ch = device_get_softc(dev);
285 mvs_set_edma_mode(dev, MVS_EDMA_OFF);
286 /* Disable port interrupts. */
287 ATA_OUTL(ch->r_mem, EDMA_IEM, 0);
292 mvs_ch_suspend(device_t dev)
294 struct mvs_channel *ch = device_get_softc(dev);
297 xpt_freeze_simq(ch->sim, 1);
299 msleep(ch, &ch->mtx, PRIBIO, "mvssusp", hz/100);
300 /* Forget about reset. */
303 callout_stop(&ch->reset_timer);
304 xpt_release_simq(ch->sim, TRUE);
307 mtx_unlock(&ch->mtx);
312 mvs_ch_resume(device_t dev)
314 struct mvs_channel *ch = device_get_softc(dev);
319 xpt_release_simq(ch->sim, TRUE);
320 mtx_unlock(&ch->mtx);
324 struct mvs_dc_cb_args {
330 mvs_dmainit(device_t dev)
332 struct mvs_channel *ch = device_get_softc(dev);
333 struct mvs_dc_cb_args dcba;
335 /* EDMA command request area. */
336 if (bus_dma_tag_create(bus_get_dma_tag(dev), 1024, 0,
337 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
338 NULL, NULL, MVS_WORKRQ_SIZE, 1, MVS_WORKRQ_SIZE,
339 0, NULL, NULL, &ch->dma.workrq_tag))
341 if (bus_dmamem_alloc(ch->dma.workrq_tag, (void **)&ch->dma.workrq, 0,
342 &ch->dma.workrq_map))
344 if (bus_dmamap_load(ch->dma.workrq_tag, ch->dma.workrq_map,
345 ch->dma.workrq, MVS_WORKRQ_SIZE, mvs_dmasetupc_cb, &dcba, 0) ||
347 bus_dmamem_free(ch->dma.workrq_tag,
348 ch->dma.workrq, ch->dma.workrq_map);
351 ch->dma.workrq_bus = dcba.maddr;
352 /* EDMA command response area. */
353 if (bus_dma_tag_create(bus_get_dma_tag(dev), 256, 0,
354 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
355 NULL, NULL, MVS_WORKRP_SIZE, 1, MVS_WORKRP_SIZE,
356 0, NULL, NULL, &ch->dma.workrp_tag))
358 if (bus_dmamem_alloc(ch->dma.workrp_tag, (void **)&ch->dma.workrp, 0,
359 &ch->dma.workrp_map))
361 if (bus_dmamap_load(ch->dma.workrp_tag, ch->dma.workrp_map,
362 ch->dma.workrp, MVS_WORKRP_SIZE, mvs_dmasetupc_cb, &dcba, 0) ||
364 bus_dmamem_free(ch->dma.workrp_tag,
365 ch->dma.workrp, ch->dma.workrp_map);
368 ch->dma.workrp_bus = dcba.maddr;
370 if (bus_dma_tag_create(bus_get_dma_tag(dev), 2, MVS_EPRD_MAX,
371 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
373 MVS_SG_ENTRIES * PAGE_SIZE, MVS_SG_ENTRIES, MVS_EPRD_MAX,
374 0, busdma_lock_mutex, &ch->mtx, &ch->dma.data_tag)) {
380 device_printf(dev, "WARNING - DMA initialization failed\n");
385 mvs_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
387 struct mvs_dc_cb_args *dcba = (struct mvs_dc_cb_args *)xsc;
389 if (!(dcba->error = error))
390 dcba->maddr = segs[0].ds_addr;
394 mvs_dmafini(device_t dev)
396 struct mvs_channel *ch = device_get_softc(dev);
398 if (ch->dma.data_tag) {
399 bus_dma_tag_destroy(ch->dma.data_tag);
400 ch->dma.data_tag = NULL;
402 if (ch->dma.workrp_bus) {
403 bus_dmamap_unload(ch->dma.workrp_tag, ch->dma.workrp_map);
404 bus_dmamem_free(ch->dma.workrp_tag,
405 ch->dma.workrp, ch->dma.workrp_map);
406 ch->dma.workrp_bus = 0;
407 ch->dma.workrp = NULL;
409 if (ch->dma.workrp_tag) {
410 bus_dma_tag_destroy(ch->dma.workrp_tag);
411 ch->dma.workrp_tag = NULL;
413 if (ch->dma.workrq_bus) {
414 bus_dmamap_unload(ch->dma.workrq_tag, ch->dma.workrq_map);
415 bus_dmamem_free(ch->dma.workrq_tag,
416 ch->dma.workrq, ch->dma.workrq_map);
417 ch->dma.workrq_bus = 0;
418 ch->dma.workrq = NULL;
420 if (ch->dma.workrq_tag) {
421 bus_dma_tag_destroy(ch->dma.workrq_tag);
422 ch->dma.workrq_tag = NULL;
427 mvs_slotsalloc(device_t dev)
429 struct mvs_channel *ch = device_get_softc(dev);
432 /* Alloc and setup command/dma slots */
433 bzero(ch->slot, sizeof(ch->slot));
434 for (i = 0; i < MVS_MAX_SLOTS; i++) {
435 struct mvs_slot *slot = &ch->slot[i];
439 slot->state = MVS_SLOT_EMPTY;
440 slot->eprd_offset = MVS_EPRD_OFFSET + MVS_EPRD_SIZE * i;
442 callout_init_mtx(&slot->timeout, &ch->mtx, 0);
444 if (bus_dmamap_create(ch->dma.data_tag, 0, &slot->dma.data_map))
445 device_printf(ch->dev, "FAILURE - create data_map\n");
450 mvs_slotsfree(device_t dev)
452 struct mvs_channel *ch = device_get_softc(dev);
455 /* Free all dma slots */
456 for (i = 0; i < MVS_MAX_SLOTS; i++) {
457 struct mvs_slot *slot = &ch->slot[i];
459 callout_drain(&slot->timeout);
460 if (slot->dma.data_map) {
461 bus_dmamap_destroy(ch->dma.data_tag, slot->dma.data_map);
462 slot->dma.data_map = NULL;
468 mvs_setup_edma_queues(device_t dev)
470 struct mvs_channel *ch = device_get_softc(dev);
473 /* Requests queue. */
474 work = ch->dma.workrq_bus;
475 ATA_OUTL(ch->r_mem, EDMA_REQQBAH, work >> 32);
476 ATA_OUTL(ch->r_mem, EDMA_REQQIP, work & 0xffffffff);
477 ATA_OUTL(ch->r_mem, EDMA_REQQOP, work & 0xffffffff);
478 bus_dmamap_sync(ch->dma.workrq_tag, ch->dma.workrq_map,
479 BUS_DMASYNC_PREWRITE);
480 /* Responses queue. */
481 memset(ch->dma.workrp, 0xff, MVS_WORKRP_SIZE);
482 work = ch->dma.workrp_bus;
483 ATA_OUTL(ch->r_mem, EDMA_RESQBAH, work >> 32);
484 ATA_OUTL(ch->r_mem, EDMA_RESQIP, work & 0xffffffff);
485 ATA_OUTL(ch->r_mem, EDMA_RESQOP, work & 0xffffffff);
486 bus_dmamap_sync(ch->dma.workrp_tag, ch->dma.workrp_map,
487 BUS_DMASYNC_PREREAD);
493 mvs_set_edma_mode(device_t dev, enum mvs_edma_mode mode)
495 struct mvs_channel *ch = device_get_softc(dev);
497 uint32_t ecfg, fcfg, hc, ltm, unkn;
499 if (mode == ch->curr_mode)
501 /* If we are running, we should stop first. */
502 if (ch->curr_mode != MVS_EDMA_OFF) {
503 ATA_OUTL(ch->r_mem, EDMA_CMD, EDMA_CMD_EDSEDMA);
505 while (ATA_INL(ch->r_mem, EDMA_CMD) & EDMA_CMD_EENEDMA) {
507 if (timeout++ > 1000) {
508 device_printf(dev, "stopping EDMA engine failed\n");
513 ch->curr_mode = mode;
516 /* Report mode to controller. Needed for correct CCC operation. */
517 MVS_EDMA(device_get_parent(dev), dev, mode);
518 /* Configure new mode. */
519 ecfg = EDMA_CFG_RESERVED | EDMA_CFG_RESERVED2 | EDMA_CFG_EHOSTQUEUECACHEEN;
520 if (ch->pm_present) {
521 ecfg |= EDMA_CFG_EMASKRXPM;
522 if (ch->quirks & MVS_Q_GENIIE) {
523 ecfg |= EDMA_CFG_EEDMAFBS;
527 if (ch->quirks & MVS_Q_GENI)
528 ecfg |= EDMA_CFG_ERDBSZ;
529 else if (ch->quirks & MVS_Q_GENII)
530 ecfg |= EDMA_CFG_ERDBSZEXT | EDMA_CFG_EWRBUFFERLEN;
531 if (ch->quirks & MVS_Q_CT)
532 ecfg |= EDMA_CFG_ECUTTHROUGHEN;
533 if (mode != MVS_EDMA_OFF)
534 ecfg |= EDMA_CFG_EEARLYCOMPLETIONEN;
535 if (mode == MVS_EDMA_QUEUED)
536 ecfg |= EDMA_CFG_EQUE;
537 else if (mode == MVS_EDMA_NCQ)
538 ecfg |= EDMA_CFG_ESATANATVCMDQUE;
539 ATA_OUTL(ch->r_mem, EDMA_CFG, ecfg);
540 mvs_setup_edma_queues(dev);
541 if (ch->quirks & MVS_Q_GENIIE) {
542 /* Configure FBS-related registers */
543 fcfg = ATA_INL(ch->r_mem, SATA_FISC);
544 ltm = ATA_INL(ch->r_mem, SATA_LTM);
545 hc = ATA_INL(ch->r_mem, EDMA_HC);
546 if (ch->fbs_enabled) {
547 fcfg |= SATA_FISC_FISDMAACTIVATESYNCRESP;
548 if (mode == MVS_EDMA_NCQ) {
549 fcfg &= ~SATA_FISC_FISWAIT4HOSTRDYEN_B0;
550 hc &= ~EDMA_IE_EDEVERR;
552 fcfg |= SATA_FISC_FISWAIT4HOSTRDYEN_B0;
553 hc |= EDMA_IE_EDEVERR;
557 fcfg &= ~SATA_FISC_FISDMAACTIVATESYNCRESP;
558 fcfg &= ~SATA_FISC_FISWAIT4HOSTRDYEN_B0;
559 hc |= EDMA_IE_EDEVERR;
562 ATA_OUTL(ch->r_mem, SATA_FISC, fcfg);
563 ATA_OUTL(ch->r_mem, SATA_LTM, ltm);
564 ATA_OUTL(ch->r_mem, EDMA_HC, hc);
565 /* This is some magic, required to handle several DRQs
567 unkn = ATA_INL(ch->r_mem, EDMA_UNKN_RESD);
568 if (mode == MVS_EDMA_OFF)
572 ATA_OUTL(ch->r_mem, EDMA_UNKN_RESD, unkn);
575 if (mode != MVS_EDMA_OFF)
576 ATA_OUTL(ch->r_mem, EDMA_CMD, EDMA_CMD_EENEDMA);
579 devclass_t mvs_devclass;
580 devclass_t mvsch_devclass;
581 static device_method_t mvsch_methods[] = {
582 DEVMETHOD(device_probe, mvs_ch_probe),
583 DEVMETHOD(device_attach, mvs_ch_attach),
584 DEVMETHOD(device_detach, mvs_ch_detach),
585 DEVMETHOD(device_suspend, mvs_ch_suspend),
586 DEVMETHOD(device_resume, mvs_ch_resume),
589 static driver_t mvsch_driver = {
592 sizeof(struct mvs_channel)
594 DRIVER_MODULE(mvsch, mvs, mvsch_driver, mvsch_devclass, 0, 0);
595 DRIVER_MODULE(mvsch, sata, mvsch_driver, mvsch_devclass, 0, 0);
598 mvs_phy_check_events(device_t dev, u_int32_t serr)
600 struct mvs_channel *ch = device_get_softc(dev);
602 if (ch->pm_level == 0) {
603 u_int32_t status = ATA_INL(ch->r_mem, SATA_SS);
607 if (((status & SATA_SS_DET_MASK) == SATA_SS_DET_PHY_ONLINE) &&
608 ((status & SATA_SS_SPD_MASK) != SATA_SS_SPD_NO_SPEED) &&
609 ((status & SATA_SS_IPM_MASK) == SATA_SS_IPM_ACTIVE)) {
610 device_printf(dev, "CONNECT requested\n");
612 device_printf(dev, "DISCONNECT requested\n");
615 if ((ccb = xpt_alloc_ccb_nowait()) == NULL)
617 if (xpt_create_path(&ccb->ccb_h.path, NULL,
618 cam_sim_path(ch->sim),
619 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
628 mvs_notify_events(device_t dev)
630 struct mvs_channel *ch = device_get_softc(dev);
631 struct cam_path *dpath;
635 /* Try to read PMP field from SDB FIS. Present only for Gen-IIe. */
636 fis = ATA_INL(ch->r_mem, SATA_FISDW0);
637 if ((fis & 0x80ff) == 0x80a1)
638 d = (fis & 0x0f00) >> 8;
640 d = ch->pm_present ? 15 : 0;
642 device_printf(dev, "SNTF %d\n", d);
643 if (xpt_create_path(&dpath, NULL,
644 xpt_path_path_id(ch->path), d, 0) == CAM_REQ_CMP) {
645 xpt_async(AC_SCSI_AEN, dpath, NULL);
646 xpt_free_path(dpath);
651 mvs_ch_intr_locked(void *data)
653 struct mvs_intr_arg *arg = (struct mvs_intr_arg *)data;
654 device_t dev = (device_t)arg->arg;
655 struct mvs_channel *ch = device_get_softc(dev);
659 mtx_unlock(&ch->mtx);
665 device_t dev = (device_t)arg;
666 struct mvs_channel *ch = device_get_softc(dev);
669 if (ch->numrslots != 0)
671 /* If we are idle - request power state transition. */
672 work = ATA_INL(ch->r_mem, SATA_SC);
673 work &= ~SATA_SC_SPM_MASK;
674 if (ch->pm_level == 4)
675 work |= SATA_SC_SPM_PARTIAL;
677 work |= SATA_SC_SPM_SLUMBER;
678 ATA_OUTL(ch->r_mem, SATA_SC, work);
682 mvs_ch_pm_wake(device_t dev)
684 struct mvs_channel *ch = device_get_softc(dev);
688 work = ATA_INL(ch->r_mem, SATA_SS);
689 if (work & SATA_SS_IPM_ACTIVE)
691 /* If we are not in active state - request power state transition. */
692 work = ATA_INL(ch->r_mem, SATA_SC);
693 work &= ~SATA_SC_SPM_MASK;
694 work |= SATA_SC_SPM_ACTIVE;
695 ATA_OUTL(ch->r_mem, SATA_SC, work);
696 /* Wait for transition to happen. */
697 while ((ATA_INL(ch->r_mem, SATA_SS) & SATA_SS_IPM_ACTIVE) == 0 &&
704 mvs_ch_intr(void *data)
706 struct mvs_intr_arg *arg = (struct mvs_intr_arg *)data;
707 device_t dev = (device_t)arg->arg;
708 struct mvs_channel *ch = device_get_softc(dev);
709 uint32_t iec, serr = 0, fisic = 0;
710 enum mvs_err_type et;
711 int i, ccs, port = -1, selfdis = 0;
712 int edma = (ch->numtslots != 0 || ch->numdslots != 0);
714 /* New item in response queue. */
715 if ((arg->cause & 2) && edma)
717 /* Some error or special event. */
718 if (arg->cause & 1) {
719 iec = ATA_INL(ch->r_mem, EDMA_IEC);
720 if (iec & EDMA_IE_SERRINT) {
721 serr = ATA_INL(ch->r_mem, SATA_SE);
722 ATA_OUTL(ch->r_mem, SATA_SE, serr);
724 /* EDMA self-disabled due to error. */
725 if (iec & EDMA_IE_ESELFDIS)
727 /* Transport interrupt. */
728 if (iec & EDMA_IE_ETRANSINT) {
729 /* For Gen-I this bit means self-disable. */
730 if (ch->quirks & MVS_Q_GENI)
732 /* For Gen-II this bit means SDB-N. */
733 else if (ch->quirks & MVS_Q_GENII)
734 fisic = SATA_FISC_FISWAIT4HOSTRDYEN_B1;
735 else /* For Gen-IIe - read FIS interrupt cause. */
736 fisic = ATA_INL(ch->r_mem, SATA_FISIC);
739 ch->curr_mode = MVS_EDMA_UNKNOWN;
740 ATA_OUTL(ch->r_mem, EDMA_IEC, ~iec);
741 /* Interface errors or Device error. */
742 if (iec & (0xfc1e9000 | EDMA_IE_EDEVERR)) {
744 if (ch->numpslots != 0) {
747 if (ch->quirks & MVS_Q_GENIIE)
748 ccs = EDMA_S_EIOID(ATA_INL(ch->r_mem, EDMA_S));
750 ccs = EDMA_S_EDEVQUETAG(ATA_INL(ch->r_mem, EDMA_S));
751 /* Check if error is one-PMP-port-specific, */
752 if (ch->fbs_enabled) {
753 /* Which ports were active. */
754 for (i = 0; i < 16; i++) {
755 if (ch->numrslotspd[i] == 0)
759 else if (port != i) {
764 /* If several ports were active and EDMA still enabled -
765 * other ports are probably unaffected and may continue.
767 if (port == -2 && !selfdis) {
768 uint16_t p = ATA_INL(ch->r_mem, SATA_SATAITC) >> 16;
770 if (port != (fls(p) - 1))
775 mvs_requeue_frozen(dev);
776 for (i = 0; i < MVS_MAX_SLOTS; i++) {
777 /* XXX: reqests in loading state. */
778 if (((ch->rslots >> i) & 1) == 0)
781 ch->slot[i].ccb->ccb_h.target_id != port)
783 if (iec & EDMA_IE_EDEVERR) { /* Device error. */
785 if (ch->numtslots == 0) {
786 /* Untagged operation. */
790 et = MVS_ERR_INNOCENT;
792 /* Tagged operation. */
799 } else if (iec & 0xfc1e9000) {
800 if (ch->numtslots == 0 &&
801 i != ccs && port != -2)
802 et = MVS_ERR_INNOCENT;
806 et = MVS_ERR_INVALID;
807 mvs_end_transaction(&ch->slot[i], et);
811 if (fisic & SATA_FISC_FISWAIT4HOSTRDYEN_B1)
812 mvs_notify_events(dev);
814 ATA_OUTL(ch->r_mem, SATA_FISIC, ~fisic);
815 /* Process hot-plug. */
816 if ((iec & (EDMA_IE_EDEVDIS | EDMA_IE_EDEVCON)) ||
817 (serr & SATA_SE_PHY_CHANGED))
818 mvs_phy_check_events(dev, serr);
820 /* Legacy mode device interrupt. */
821 if ((arg->cause & 2) && !edma)
822 mvs_legacy_intr(dev, arg->cause & 4);
826 mvs_getstatus(device_t dev, int clear)
828 struct mvs_channel *ch = device_get_softc(dev);
829 uint8_t status = ATA_INB(ch->r_mem, clear ? ATA_STATUS : ATA_ALTSTAT);
832 if (status & (ATA_S_BUSY | ATA_S_DRQ | ATA_S_ERROR))
835 status |= ATA_S_BUSY;
841 mvs_legacy_intr(device_t dev, int poll)
843 struct mvs_channel *ch = device_get_softc(dev);
844 struct mvs_slot *slot = &ch->slot[0]; /* PIO is always in slot 0. */
845 union ccb *ccb = slot->ccb;
846 enum mvs_err_type et = MVS_ERR_NONE;
848 u_int length, resid, size;
850 uint8_t status, ireason;
852 /* Clear interrupt and get status. */
853 status = mvs_getstatus(dev, 1);
854 if (slot->state < MVS_SLOT_RUNNING)
856 port = ccb->ccb_h.target_id & 0x0f;
857 /* Wait a bit for late !BUSY status update. */
858 if (status & ATA_S_BUSY) {
862 if ((status = mvs_getstatus(dev, 1)) & ATA_S_BUSY) {
864 if ((status = mvs_getstatus(dev, 1)) & ATA_S_BUSY)
868 /* If we got an error, we are done. */
869 if (status & ATA_S_ERROR) {
873 if (ccb->ccb_h.func_code == XPT_ATA_IO) { /* ATA PIO */
874 ccb->ataio.res.status = status;
875 /* Are we moving data? */
876 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
877 /* If data read command - get them. */
878 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
879 if (mvs_wait(dev, ATA_S_DRQ, ATA_S_BUSY, 1000) < 0) {
880 device_printf(dev, "timeout waiting for read DRQ\n");
881 et = MVS_ERR_TIMEOUT;
882 xpt_freeze_simq(ch->sim, 1);
883 ch->toslots |= (1 << slot->slot);
886 ATA_INSW_STRM(ch->r_mem, ATA_DATA,
887 (uint16_t *)(ccb->ataio.data_ptr + ch->donecount),
888 ch->transfersize / 2);
890 /* Update how far we've gotten. */
891 ch->donecount += ch->transfersize;
892 /* Do we need more? */
893 if (ccb->ataio.dxfer_len > ch->donecount) {
894 /* Set this transfer size according to HW capabilities */
895 ch->transfersize = min(ccb->ataio.dxfer_len - ch->donecount,
897 /* If data write command - put them */
898 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
899 if (mvs_wait(dev, ATA_S_DRQ, ATA_S_BUSY, 1000) < 0) {
901 "timeout waiting for write DRQ\n");
902 et = MVS_ERR_TIMEOUT;
903 xpt_freeze_simq(ch->sim, 1);
904 ch->toslots |= (1 << slot->slot);
907 ATA_OUTSW_STRM(ch->r_mem, ATA_DATA,
908 (uint16_t *)(ccb->ataio.data_ptr + ch->donecount),
909 ch->transfersize / 2);
912 /* If data read command, return & wait for interrupt */
913 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
917 } else if (ch->basic_dma) { /* ATAPI DMA */
918 if (status & ATA_S_DWF)
920 else if (ATA_INL(ch->r_mem, DMA_S) & DMA_S_ERR)
922 /* Stop basic DMA. */
923 ATA_OUTL(ch->r_mem, DMA_C, 0);
925 } else { /* ATAPI PIO */
926 length = ATA_INB(ch->r_mem,ATA_CYL_LSB) |
927 (ATA_INB(ch->r_mem,ATA_CYL_MSB) << 8);
928 size = min(ch->transfersize, length);
929 ireason = ATA_INB(ch->r_mem,ATA_IREASON);
930 switch ((ireason & (ATA_I_CMD | ATA_I_IN)) |
931 (status & ATA_S_DRQ)) {
933 device_printf(dev, "ATAPI CMDOUT\n");
934 /* Return wait for interrupt */
938 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
939 device_printf(dev, "trying to write on read buffer\n");
944 ATA_OUTSW_STRM(ch->r_mem, ATA_DATA,
945 (uint16_t *)(ccb->csio.data_ptr + ch->donecount),
947 for (resid = ch->transfersize + (size & 1);
948 resid < length; resid += sizeof(int16_t))
949 ATA_OUTW(ch->r_mem, ATA_DATA, 0);
950 ch->donecount += length;
951 /* Set next transfer size according to HW capabilities */
952 ch->transfersize = min(ccb->csio.dxfer_len - ch->donecount,
953 ch->curr[ccb->ccb_h.target_id].bytecount);
954 /* Return wait for interrupt */
958 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
959 device_printf(dev, "trying to read on write buffer\n");
964 ATA_INSW_STRM(ch->r_mem, ATA_DATA,
965 (uint16_t *)(ccb->csio.data_ptr + ch->donecount),
969 ATA_INSW_STRM(ch->r_mem, ATA_DATA, (void*)buf, 1);
970 ((uint8_t *)ccb->csio.data_ptr + ch->donecount +
971 (size & ~1))[0] = buf[0];
973 for (resid = ch->transfersize + (size & 1);
974 resid < length; resid += sizeof(int16_t))
975 ATA_INW(ch->r_mem, ATA_DATA);
976 ch->donecount += length;
977 /* Set next transfer size according to HW capabilities */
978 ch->transfersize = min(ccb->csio.dxfer_len - ch->donecount,
979 ch->curr[ccb->ccb_h.target_id].bytecount);
980 /* Return wait for interrupt */
983 case ATAPI_P_DONEDRQ:
985 "WARNING - DONEDRQ non conformant device\n");
986 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
987 ATA_INSW_STRM(ch->r_mem, ATA_DATA,
988 (uint16_t *)(ccb->csio.data_ptr + ch->donecount),
990 ch->donecount += length;
992 else if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
993 ATA_OUTSW_STRM(ch->r_mem, ATA_DATA,
994 (uint16_t *)(ccb->csio.data_ptr + ch->donecount),
996 ch->donecount += length;
1004 if (status & (ATA_S_ERROR | ATA_S_DWF))
1009 device_printf(dev, "unknown transfer phase"
1010 " (status %02x, ireason %02x)\n",
1017 mvs_end_transaction(slot, et);
1021 mvs_crbq_intr(device_t dev)
1023 struct mvs_channel *ch = device_get_softc(dev);
1024 struct mvs_crpb *crpb;
1026 int in_idx, fin_idx, cin_idx, slot;
1030 val = ATA_INL(ch->r_mem, EDMA_RESQIP);
1032 val = ATA_INL(ch->r_mem, EDMA_RESQIP);
1033 in_idx = (val & EDMA_RESQP_ERPQP_MASK) >>
1034 EDMA_RESQP_ERPQP_SHIFT;
1035 bus_dmamap_sync(ch->dma.workrp_tag, ch->dma.workrp_map,
1036 BUS_DMASYNC_POSTREAD);
1037 fin_idx = cin_idx = ch->in_idx;
1038 ch->in_idx = in_idx;
1039 while (in_idx != cin_idx) {
1040 crpb = (struct mvs_crpb *)
1041 (ch->dma.workrp + MVS_CRPB_OFFSET +
1042 (MVS_CRPB_SIZE * cin_idx));
1043 slot = le16toh(crpb->id) & MVS_CRPB_TAG_MASK;
1044 flags = le16toh(crpb->rspflg);
1046 * Handle only successful completions here.
1047 * Errors will be handled by main intr handler.
1049 #if defined(__i386__) || defined(__amd64__)
1050 if (crpb->id == 0xffff && crpb->rspflg == 0xffff) {
1051 device_printf(dev, "Unfilled CRPB "
1052 "%d (%d->%d) tag %d flags %04x rs %08x\n",
1053 cin_idx, fin_idx, in_idx, slot, flags, ch->rslots);
1056 if (ch->numtslots != 0 ||
1057 (flags & EDMA_IE_EDEVERR) == 0) {
1058 #if defined(__i386__) || defined(__amd64__)
1060 crpb->rspflg = 0xffff;
1062 if (ch->slot[slot].state >= MVS_SLOT_RUNNING) {
1063 ccb = ch->slot[slot].ccb;
1064 ccb->ataio.res.status =
1065 (flags & MVS_CRPB_ATASTS_MASK) >>
1066 MVS_CRPB_ATASTS_SHIFT;
1067 mvs_end_transaction(&ch->slot[slot], MVS_ERR_NONE);
1069 device_printf(dev, "Unused tag in CRPB "
1070 "%d (%d->%d) tag %d flags %04x rs %08x\n",
1071 cin_idx, fin_idx, in_idx, slot, flags,
1076 "CRPB with error %d tag %d flags %04x\n",
1077 cin_idx, slot, flags);
1079 cin_idx = (cin_idx + 1) & (MVS_MAX_SLOTS - 1);
1081 bus_dmamap_sync(ch->dma.workrp_tag, ch->dma.workrp_map,
1082 BUS_DMASYNC_PREREAD);
1083 if (cin_idx == ch->in_idx) {
1084 ATA_OUTL(ch->r_mem, EDMA_RESQOP,
1085 ch->dma.workrp_bus | (cin_idx << EDMA_RESQP_ERPQP_SHIFT));
1089 /* Must be called with channel locked. */
1091 mvs_check_collision(device_t dev, union ccb *ccb)
1093 struct mvs_channel *ch = device_get_softc(dev);
1095 if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1097 if (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA) {
1098 /* Can't mix NCQ and non-NCQ DMA commands. */
1099 if (ch->numdslots != 0)
1101 /* Can't mix NCQ and PIO commands. */
1102 if (ch->numpslots != 0)
1104 /* If we have no FBS */
1105 if (!ch->fbs_enabled) {
1106 /* Tagged command while tagged to other target is active. */
1107 if (ch->numtslots != 0 &&
1108 ch->taggedtarget != ccb->ccb_h.target_id)
1112 } else if (ccb->ataio.cmd.flags & CAM_ATAIO_DMA) {
1113 /* Can't mix non-NCQ DMA and NCQ commands. */
1114 if (ch->numtslots != 0)
1116 /* Can't mix non-NCQ DMA and PIO commands. */
1117 if (ch->numpslots != 0)
1121 /* Can't mix PIO with anything. */
1122 if (ch->numrslots != 0)
1125 if (ccb->ataio.cmd.flags & (CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT)) {
1126 /* Atomic command while anything active. */
1127 if (ch->numrslots != 0)
1130 } else { /* ATAPI */
1131 /* ATAPI goes without EDMA, so can't mix it with anything. */
1132 if (ch->numrslots != 0)
1135 /* We have some atomic command running. */
1136 if (ch->aslots != 0)
1142 mvs_tfd_read(device_t dev, union ccb *ccb)
1144 struct mvs_channel *ch = device_get_softc(dev);
1145 struct ata_res *res = &ccb->ataio.res;
1147 res->status = ATA_INB(ch->r_mem, ATA_ALTSTAT);
1148 res->error = ATA_INB(ch->r_mem, ATA_ERROR);
1149 res->device = ATA_INB(ch->r_mem, ATA_DRIVE);
1150 ATA_OUTB(ch->r_mem, ATA_CONTROL, ATA_A_HOB);
1151 res->sector_count_exp = ATA_INB(ch->r_mem, ATA_COUNT);
1152 res->lba_low_exp = ATA_INB(ch->r_mem, ATA_SECTOR);
1153 res->lba_mid_exp = ATA_INB(ch->r_mem, ATA_CYL_LSB);
1154 res->lba_high_exp = ATA_INB(ch->r_mem, ATA_CYL_MSB);
1155 ATA_OUTB(ch->r_mem, ATA_CONTROL, 0);
1156 res->sector_count = ATA_INB(ch->r_mem, ATA_COUNT);
1157 res->lba_low = ATA_INB(ch->r_mem, ATA_SECTOR);
1158 res->lba_mid = ATA_INB(ch->r_mem, ATA_CYL_LSB);
1159 res->lba_high = ATA_INB(ch->r_mem, ATA_CYL_MSB);
1163 mvs_tfd_write(device_t dev, union ccb *ccb)
1165 struct mvs_channel *ch = device_get_softc(dev);
1166 struct ata_cmd *cmd = &ccb->ataio.cmd;
1168 ATA_OUTB(ch->r_mem, ATA_DRIVE, cmd->device);
1169 ATA_OUTB(ch->r_mem, ATA_CONTROL, cmd->control);
1170 ATA_OUTB(ch->r_mem, ATA_FEATURE, cmd->features_exp);
1171 ATA_OUTB(ch->r_mem, ATA_FEATURE, cmd->features);
1172 ATA_OUTB(ch->r_mem, ATA_COUNT, cmd->sector_count_exp);
1173 ATA_OUTB(ch->r_mem, ATA_COUNT, cmd->sector_count);
1174 ATA_OUTB(ch->r_mem, ATA_SECTOR, cmd->lba_low_exp);
1175 ATA_OUTB(ch->r_mem, ATA_SECTOR, cmd->lba_low);
1176 ATA_OUTB(ch->r_mem, ATA_CYL_LSB, cmd->lba_mid_exp);
1177 ATA_OUTB(ch->r_mem, ATA_CYL_LSB, cmd->lba_mid);
1178 ATA_OUTB(ch->r_mem, ATA_CYL_MSB, cmd->lba_high_exp);
1179 ATA_OUTB(ch->r_mem, ATA_CYL_MSB, cmd->lba_high);
1180 ATA_OUTB(ch->r_mem, ATA_COMMAND, cmd->command);
1183 /* Must be called with channel locked. */
1185 mvs_begin_transaction(device_t dev, union ccb *ccb)
1187 struct mvs_channel *ch = device_get_softc(dev);
1188 struct mvs_slot *slot;
1191 if (ch->pm_level > 0)
1192 mvs_ch_pm_wake(dev);
1193 /* Softreset is a special case. */
1194 if (ccb->ccb_h.func_code == XPT_ATA_IO &&
1195 (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL)) {
1196 mvs_softreset(dev, ccb);
1199 /* Choose empty slot. */
1200 slotn = ffs(~ch->oslots) - 1;
1201 if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
1202 (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) {
1203 if (ch->quirks & MVS_Q_GENIIE)
1204 tag = ffs(~ch->otagspd[ccb->ccb_h.target_id]) - 1;
1209 /* Occupy chosen slot. */
1210 slot = &ch->slot[slotn];
1213 /* Stop PM timer. */
1214 if (ch->numrslots == 0 && ch->pm_level > 3)
1215 callout_stop(&ch->pm_timer);
1216 /* Update channel stats. */
1217 ch->oslots |= (1 << slot->slot);
1219 ch->numrslotspd[ccb->ccb_h.target_id]++;
1220 if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1221 if (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA) {
1222 ch->otagspd[ccb->ccb_h.target_id] |= (1 << slot->tag);
1224 ch->numtslotspd[ccb->ccb_h.target_id]++;
1225 ch->taggedtarget = ccb->ccb_h.target_id;
1226 mvs_set_edma_mode(dev, MVS_EDMA_NCQ);
1227 } else if (ccb->ataio.cmd.flags & CAM_ATAIO_DMA) {
1229 mvs_set_edma_mode(dev, MVS_EDMA_ON);
1232 mvs_set_edma_mode(dev, MVS_EDMA_OFF);
1234 if (ccb->ataio.cmd.flags &
1235 (CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT)) {
1236 ch->aslots |= (1 << slot->slot);
1239 uint8_t *cdb = (ccb->ccb_h.flags & CAM_CDB_POINTER) ?
1240 ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes;
1242 /* Use ATAPI DMA only for commands without under-/overruns. */
1243 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE &&
1244 ch->curr[ccb->ccb_h.target_id].mode >= ATA_DMA &&
1245 (ch->quirks & MVS_Q_SOC) == 0 &&
1257 mvs_set_edma_mode(dev, MVS_EDMA_OFF);
1259 if (ch->numpslots == 0 || ch->basic_dma) {
1260 slot->state = MVS_SLOT_LOADING;
1261 bus_dmamap_load_ccb(ch->dma.data_tag, slot->dma.data_map,
1262 ccb, mvs_dmasetprd, slot, 0);
1264 mvs_legacy_execute_transaction(slot);
1267 /* Locked by busdma engine. */
1269 mvs_dmasetprd(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
1271 struct mvs_slot *slot = arg;
1272 struct mvs_channel *ch = device_get_softc(slot->dev);
1273 struct mvs_eprd *eprd;
1277 device_printf(slot->dev, "DMA load error\n");
1278 mvs_end_transaction(slot, MVS_ERR_INVALID);
1281 KASSERT(nsegs <= MVS_SG_ENTRIES, ("too many DMA segment entries\n"));
1282 /* If there is only one segment - no need to use S/G table on Gen-IIe. */
1283 if (nsegs == 1 && ch->basic_dma == 0 && (ch->quirks & MVS_Q_GENIIE)) {
1284 slot->dma.addr = segs[0].ds_addr;
1285 slot->dma.len = segs[0].ds_len;
1288 /* Get a piece of the workspace for this EPRD */
1289 eprd = (struct mvs_eprd *)(ch->dma.workrq + slot->eprd_offset);
1290 /* Fill S/G table */
1291 for (i = 0; i < nsegs; i++) {
1292 eprd[i].prdbal = htole32(segs[i].ds_addr);
1293 eprd[i].bytecount = htole32(segs[i].ds_len & MVS_EPRD_MASK);
1294 eprd[i].prdbah = htole32((segs[i].ds_addr >> 16) >> 16);
1296 eprd[i - 1].bytecount |= htole32(MVS_EPRD_EOF);
1298 bus_dmamap_sync(ch->dma.data_tag, slot->dma.data_map,
1299 ((slot->ccb->ccb_h.flags & CAM_DIR_IN) ?
1300 BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE));
1302 mvs_legacy_execute_transaction(slot);
1304 mvs_execute_transaction(slot);
1308 mvs_legacy_execute_transaction(struct mvs_slot *slot)
1310 device_t dev = slot->dev;
1311 struct mvs_channel *ch = device_get_softc(dev);
1313 union ccb *ccb = slot->ccb;
1314 int port = ccb->ccb_h.target_id & 0x0f;
1317 slot->state = MVS_SLOT_RUNNING;
1318 ch->rslots |= (1 << slot->slot);
1319 ATA_OUTB(ch->r_mem, SATA_SATAICTL, port << SATA_SATAICTL_PMPTX_SHIFT);
1320 if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1321 mvs_tfd_write(dev, ccb);
1322 /* Device reset doesn't interrupt. */
1323 if (ccb->ataio.cmd.command == ATA_DEVICE_RESET) {
1324 int timeout = 1000000;
1327 ccb->ataio.res.status = ATA_INB(ch->r_mem, ATA_STATUS);
1328 } while (ccb->ataio.res.status & ATA_S_BUSY && timeout--);
1329 mvs_legacy_intr(dev, 1);
1333 if (ccb->ataio.cmd.command == ATA_READ_MUL ||
1334 ccb->ataio.cmd.command == ATA_READ_MUL48 ||
1335 ccb->ataio.cmd.command == ATA_WRITE_MUL ||
1336 ccb->ataio.cmd.command == ATA_WRITE_MUL48) {
1337 ch->transfersize = min(ccb->ataio.dxfer_len,
1338 ch->curr[port].bytecount);
1340 ch->transfersize = min(ccb->ataio.dxfer_len, 512);
1341 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE)
1343 /* If data write command - output the data */
1344 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
1345 if (mvs_wait(dev, ATA_S_DRQ, ATA_S_BUSY, 1000) < 0) {
1347 "timeout waiting for write DRQ\n");
1348 xpt_freeze_simq(ch->sim, 1);
1349 ch->toslots |= (1 << slot->slot);
1350 mvs_end_transaction(slot, MVS_ERR_TIMEOUT);
1353 ATA_OUTSW_STRM(ch->r_mem, ATA_DATA,
1354 (uint16_t *)(ccb->ataio.data_ptr + ch->donecount),
1355 ch->transfersize / 2);
1359 ch->transfersize = min(ccb->csio.dxfer_len,
1360 ch->curr[port].bytecount);
1361 /* Write ATA PACKET command. */
1362 if (ch->basic_dma) {
1363 ATA_OUTB(ch->r_mem, ATA_FEATURE, ATA_F_DMA);
1364 ATA_OUTB(ch->r_mem, ATA_CYL_LSB, 0);
1365 ATA_OUTB(ch->r_mem, ATA_CYL_MSB, 0);
1367 ATA_OUTB(ch->r_mem, ATA_FEATURE, 0);
1368 ATA_OUTB(ch->r_mem, ATA_CYL_LSB, ch->transfersize);
1369 ATA_OUTB(ch->r_mem, ATA_CYL_MSB, ch->transfersize >> 8);
1371 ATA_OUTB(ch->r_mem, ATA_COMMAND, ATA_PACKET_CMD);
1373 /* Wait for ready to write ATAPI command block */
1374 if (mvs_wait(dev, 0, ATA_S_BUSY, 1000) < 0) {
1375 device_printf(dev, "timeout waiting for ATAPI !BUSY\n");
1376 xpt_freeze_simq(ch->sim, 1);
1377 ch->toslots |= (1 << slot->slot);
1378 mvs_end_transaction(slot, MVS_ERR_TIMEOUT);
1383 int reason = ATA_INB(ch->r_mem, ATA_IREASON);
1384 int status = ATA_INB(ch->r_mem, ATA_STATUS);
1386 if (((reason & (ATA_I_CMD | ATA_I_IN)) |
1387 (status & (ATA_S_DRQ | ATA_S_BUSY))) == ATAPI_P_CMDOUT)
1393 "timeout waiting for ATAPI command ready\n");
1394 xpt_freeze_simq(ch->sim, 1);
1395 ch->toslots |= (1 << slot->slot);
1396 mvs_end_transaction(slot, MVS_ERR_TIMEOUT);
1399 /* Write ATAPI command. */
1400 ATA_OUTSW_STRM(ch->r_mem, ATA_DATA,
1401 (uint16_t *)((ccb->ccb_h.flags & CAM_CDB_POINTER) ?
1402 ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes),
1403 ch->curr[port].atapi / 2);
1405 if (ch->basic_dma) {
1406 /* Start basic DMA. */
1407 eprd = ch->dma.workrq_bus + slot->eprd_offset;
1408 ATA_OUTL(ch->r_mem, DMA_DTLBA, eprd);
1409 ATA_OUTL(ch->r_mem, DMA_DTHBA, (eprd >> 16) >> 16);
1410 ATA_OUTL(ch->r_mem, DMA_C, DMA_C_START |
1411 (((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) ?
1415 /* Start command execution timeout */
1416 callout_reset_sbt(&slot->timeout, SBT_1MS * ccb->ccb_h.timeout, 0,
1417 mvs_timeout, slot, 0);
1420 /* Must be called with channel locked. */
1422 mvs_execute_transaction(struct mvs_slot *slot)
1424 device_t dev = slot->dev;
1425 struct mvs_channel *ch = device_get_softc(dev);
1427 struct mvs_crqb *crqb;
1428 struct mvs_crqb_gen2e *crqb2e;
1429 union ccb *ccb = slot->ccb;
1430 int port = ccb->ccb_h.target_id & 0x0f;
1433 /* Get address of the prepared EPRD */
1434 eprd = ch->dma.workrq_bus + slot->eprd_offset;
1435 /* Prepare CRQB. Gen IIe uses different CRQB format. */
1436 if (ch->quirks & MVS_Q_GENIIE) {
1437 crqb2e = (struct mvs_crqb_gen2e *)
1438 (ch->dma.workrq + MVS_CRQB_OFFSET + (MVS_CRQB_SIZE * ch->out_idx));
1439 crqb2e->ctrlflg = htole32(
1440 ((ccb->ccb_h.flags & CAM_DIR_IN) ? MVS_CRQB2E_READ : 0) |
1441 (slot->tag << MVS_CRQB2E_DTAG_SHIFT) |
1442 (port << MVS_CRQB2E_PMP_SHIFT) |
1443 (slot->slot << MVS_CRQB2E_HTAG_SHIFT));
1444 /* If there is only one segment - no need to use S/G table. */
1445 if (slot->dma.addr != 0) {
1446 eprd = slot->dma.addr;
1447 crqb2e->ctrlflg |= htole32(MVS_CRQB2E_CPRD);
1448 crqb2e->drbc = slot->dma.len;
1450 crqb2e->cprdbl = htole32(eprd);
1451 crqb2e->cprdbh = htole32((eprd >> 16) >> 16);
1454 crqb2e->cmd[2] = ccb->ataio.cmd.command;
1455 crqb2e->cmd[3] = ccb->ataio.cmd.features;
1456 crqb2e->cmd[4] = ccb->ataio.cmd.lba_low;
1457 crqb2e->cmd[5] = ccb->ataio.cmd.lba_mid;
1458 crqb2e->cmd[6] = ccb->ataio.cmd.lba_high;
1459 crqb2e->cmd[7] = ccb->ataio.cmd.device;
1460 crqb2e->cmd[8] = ccb->ataio.cmd.lba_low_exp;
1461 crqb2e->cmd[9] = ccb->ataio.cmd.lba_mid_exp;
1462 crqb2e->cmd[10] = ccb->ataio.cmd.lba_high_exp;
1463 crqb2e->cmd[11] = ccb->ataio.cmd.features_exp;
1464 if (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA) {
1465 crqb2e->cmd[12] = slot->tag << 3;
1466 crqb2e->cmd[13] = 0;
1468 crqb2e->cmd[12] = ccb->ataio.cmd.sector_count;
1469 crqb2e->cmd[13] = ccb->ataio.cmd.sector_count_exp;
1471 crqb2e->cmd[14] = 0;
1472 crqb2e->cmd[15] = 0;
1474 crqb = (struct mvs_crqb *)
1475 (ch->dma.workrq + MVS_CRQB_OFFSET + (MVS_CRQB_SIZE * ch->out_idx));
1476 crqb->cprdbl = htole32(eprd);
1477 crqb->cprdbh = htole32((eprd >> 16) >> 16);
1478 crqb->ctrlflg = htole16(
1479 ((ccb->ccb_h.flags & CAM_DIR_IN) ? MVS_CRQB_READ : 0) |
1480 (slot->slot << MVS_CRQB_TAG_SHIFT) |
1481 (port << MVS_CRQB_PMP_SHIFT));
1484 * Controller can handle only 11 of 12 ATA registers,
1485 * so we have to choose which one to skip.
1487 if (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA) {
1488 crqb->cmd[i++] = ccb->ataio.cmd.features_exp;
1489 crqb->cmd[i++] = 0x11;
1491 crqb->cmd[i++] = ccb->ataio.cmd.features;
1492 crqb->cmd[i++] = 0x11;
1493 if (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA) {
1494 crqb->cmd[i++] = (slot->tag << 3) |
1495 (ccb->ataio.cmd.sector_count & 0x07);
1496 crqb->cmd[i++] = 0x12;
1498 crqb->cmd[i++] = ccb->ataio.cmd.sector_count_exp;
1499 crqb->cmd[i++] = 0x12;
1500 crqb->cmd[i++] = ccb->ataio.cmd.sector_count;
1501 crqb->cmd[i++] = 0x12;
1503 crqb->cmd[i++] = ccb->ataio.cmd.lba_low_exp;
1504 crqb->cmd[i++] = 0x13;
1505 crqb->cmd[i++] = ccb->ataio.cmd.lba_low;
1506 crqb->cmd[i++] = 0x13;
1507 crqb->cmd[i++] = ccb->ataio.cmd.lba_mid_exp;
1508 crqb->cmd[i++] = 0x14;
1509 crqb->cmd[i++] = ccb->ataio.cmd.lba_mid;
1510 crqb->cmd[i++] = 0x14;
1511 crqb->cmd[i++] = ccb->ataio.cmd.lba_high_exp;
1512 crqb->cmd[i++] = 0x15;
1513 crqb->cmd[i++] = ccb->ataio.cmd.lba_high;
1514 crqb->cmd[i++] = 0x15;
1515 crqb->cmd[i++] = ccb->ataio.cmd.device;
1516 crqb->cmd[i++] = 0x16;
1517 crqb->cmd[i++] = ccb->ataio.cmd.command;
1518 crqb->cmd[i++] = 0x97;
1520 bus_dmamap_sync(ch->dma.workrq_tag, ch->dma.workrq_map,
1521 BUS_DMASYNC_PREWRITE);
1522 bus_dmamap_sync(ch->dma.workrp_tag, ch->dma.workrp_map,
1523 BUS_DMASYNC_PREREAD);
1524 slot->state = MVS_SLOT_RUNNING;
1525 ch->rslots |= (1 << slot->slot);
1526 /* Issue command to the controller. */
1527 ch->out_idx = (ch->out_idx + 1) & (MVS_MAX_SLOTS - 1);
1528 ATA_OUTL(ch->r_mem, EDMA_REQQIP,
1529 ch->dma.workrq_bus + MVS_CRQB_OFFSET + (MVS_CRQB_SIZE * ch->out_idx));
1530 /* Start command execution timeout */
1531 callout_reset_sbt(&slot->timeout, SBT_1MS * ccb->ccb_h.timeout, 0,
1532 mvs_timeout, slot, 0);
1536 /* Must be called with channel locked. */
1538 mvs_process_timeout(device_t dev)
1540 struct mvs_channel *ch = device_get_softc(dev);
1543 mtx_assert(&ch->mtx, MA_OWNED);
1544 /* Handle the rest of commands. */
1545 for (i = 0; i < MVS_MAX_SLOTS; i++) {
1546 /* Do we have a running request on slot? */
1547 if (ch->slot[i].state < MVS_SLOT_RUNNING)
1549 mvs_end_transaction(&ch->slot[i], MVS_ERR_TIMEOUT);
1553 /* Must be called with channel locked. */
1555 mvs_rearm_timeout(device_t dev)
1557 struct mvs_channel *ch = device_get_softc(dev);
1560 mtx_assert(&ch->mtx, MA_OWNED);
1561 for (i = 0; i < MVS_MAX_SLOTS; i++) {
1562 struct mvs_slot *slot = &ch->slot[i];
1564 /* Do we have a running request on slot? */
1565 if (slot->state < MVS_SLOT_RUNNING)
1567 if ((ch->toslots & (1 << i)) == 0)
1569 callout_reset_sbt(&slot->timeout,
1570 SBT_1MS * slot->ccb->ccb_h.timeout / 2, 0,
1571 mvs_timeout, slot, 0);
1575 /* Locked by callout mechanism. */
1577 mvs_timeout(void *arg)
1579 struct mvs_slot *slot = arg;
1580 device_t dev = slot->dev;
1581 struct mvs_channel *ch = device_get_softc(dev);
1583 /* Check for stale timeout. */
1584 if (slot->state < MVS_SLOT_RUNNING)
1586 device_printf(dev, "Timeout on slot %d\n", slot->slot);
1587 device_printf(dev, "iec %08x sstat %08x serr %08x edma_s %08x "
1588 "dma_c %08x dma_s %08x rs %08x status %02x\n",
1589 ATA_INL(ch->r_mem, EDMA_IEC),
1590 ATA_INL(ch->r_mem, SATA_SS), ATA_INL(ch->r_mem, SATA_SE),
1591 ATA_INL(ch->r_mem, EDMA_S), ATA_INL(ch->r_mem, DMA_C),
1592 ATA_INL(ch->r_mem, DMA_S), ch->rslots,
1593 ATA_INB(ch->r_mem, ATA_ALTSTAT));
1594 /* Handle frozen command. */
1595 mvs_requeue_frozen(dev);
1596 /* We wait for other commands timeout and pray. */
1597 if (ch->toslots == 0)
1598 xpt_freeze_simq(ch->sim, 1);
1599 ch->toslots |= (1 << slot->slot);
1600 if ((ch->rslots & ~ch->toslots) == 0)
1601 mvs_process_timeout(dev);
1603 device_printf(dev, " ... waiting for slots %08x\n",
1604 ch->rslots & ~ch->toslots);
1607 /* Must be called with channel locked. */
1609 mvs_end_transaction(struct mvs_slot *slot, enum mvs_err_type et)
1611 device_t dev = slot->dev;
1612 struct mvs_channel *ch = device_get_softc(dev);
1613 union ccb *ccb = slot->ccb;
1616 bus_dmamap_sync(ch->dma.workrq_tag, ch->dma.workrq_map,
1617 BUS_DMASYNC_POSTWRITE);
1618 /* Read result registers to the result struct
1619 * May be incorrect if several commands finished same time,
1620 * so read only when sure or have to.
1622 if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1623 struct ata_res *res = &ccb->ataio.res;
1625 if ((et == MVS_ERR_TFE) ||
1626 (ccb->ataio.cmd.flags & CAM_ATAIO_NEEDRESULT)) {
1627 mvs_tfd_read(dev, ccb);
1629 bzero(res, sizeof(*res));
1631 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE &&
1633 ccb->csio.resid = ccb->csio.dxfer_len - ch->donecount;
1635 if (ch->numpslots == 0 || ch->basic_dma) {
1636 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1637 bus_dmamap_sync(ch->dma.data_tag, slot->dma.data_map,
1638 (ccb->ccb_h.flags & CAM_DIR_IN) ?
1639 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
1640 bus_dmamap_unload(ch->dma.data_tag, slot->dma.data_map);
1643 if (et != MVS_ERR_NONE)
1644 ch->eslots |= (1 << slot->slot);
1645 /* In case of error, freeze device for proper recovery. */
1646 if ((et != MVS_ERR_NONE) && (!ch->recoverycmd) &&
1647 !(ccb->ccb_h.status & CAM_DEV_QFRZN)) {
1648 xpt_freeze_devq(ccb->ccb_h.path, 1);
1649 ccb->ccb_h.status |= CAM_DEV_QFRZN;
1651 /* Set proper result status. */
1652 ccb->ccb_h.status &= ~CAM_STATUS_MASK;
1655 ccb->ccb_h.status |= CAM_REQ_CMP;
1656 if (ccb->ccb_h.func_code == XPT_SCSI_IO)
1657 ccb->csio.scsi_status = SCSI_STATUS_OK;
1659 case MVS_ERR_INVALID:
1661 ccb->ccb_h.status |= CAM_REQ_INVALID;
1663 case MVS_ERR_INNOCENT:
1664 ccb->ccb_h.status |= CAM_REQUEUE_REQ;
1668 if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
1669 ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
1670 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1672 ccb->ccb_h.status |= CAM_ATA_STATUS_ERROR;
1677 if (!ch->recoverycmd) {
1678 xpt_freeze_simq(ch->sim, 1);
1679 ccb->ccb_h.status &= ~CAM_STATUS_MASK;
1680 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1682 ccb->ccb_h.status |= CAM_UNCOR_PARITY;
1684 case MVS_ERR_TIMEOUT:
1685 if (!ch->recoverycmd) {
1686 xpt_freeze_simq(ch->sim, 1);
1687 ccb->ccb_h.status &= ~CAM_STATUS_MASK;
1688 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1690 ccb->ccb_h.status |= CAM_CMD_TIMEOUT;
1694 ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
1697 ch->oslots &= ~(1 << slot->slot);
1698 ch->rslots &= ~(1 << slot->slot);
1699 ch->aslots &= ~(1 << slot->slot);
1700 slot->state = MVS_SLOT_EMPTY;
1702 /* Update channel stats. */
1704 ch->numrslotspd[ccb->ccb_h.target_id]--;
1705 if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1706 if (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA) {
1707 ch->otagspd[ccb->ccb_h.target_id] &= ~(1 << slot->tag);
1709 ch->numtslotspd[ccb->ccb_h.target_id]--;
1710 } else if (ccb->ataio.cmd.flags & CAM_ATAIO_DMA) {
1719 /* Cancel timeout state if request completed normally. */
1720 if (et != MVS_ERR_TIMEOUT) {
1721 lastto = (ch->toslots == (1 << slot->slot));
1722 ch->toslots &= ~(1 << slot->slot);
1724 xpt_release_simq(ch->sim, TRUE);
1726 /* If it was our READ LOG command - process it. */
1727 if (ccb->ccb_h.recovery_type == RECOVERY_READ_LOG) {
1728 mvs_process_read_log(dev, ccb);
1729 /* If it was our REQUEST SENSE command - process it. */
1730 } else if (ccb->ccb_h.recovery_type == RECOVERY_REQUEST_SENSE) {
1731 mvs_process_request_sense(dev, ccb);
1732 /* If it was NCQ or ATAPI command error, put result on hold. */
1733 } else if (et == MVS_ERR_NCQ ||
1734 ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_SCSI_STATUS_ERROR &&
1735 (ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0)) {
1736 ch->hold[slot->slot] = ccb;
1737 ch->holdtag[slot->slot] = slot->tag;
1741 /* If we have no other active commands, ... */
1742 if (ch->rslots == 0) {
1743 /* if there was fatal error - reset port. */
1744 if (ch->toslots != 0 || ch->fatalerr) {
1747 /* if we have slots in error, we can reinit port. */
1748 if (ch->eslots != 0) {
1749 mvs_set_edma_mode(dev, MVS_EDMA_OFF);
1752 /* if there commands on hold, we can do READ LOG. */
1753 if (!ch->recoverycmd && ch->numhslots)
1754 mvs_issue_recovery(dev);
1756 /* If all the rest of commands are in timeout - give them chance. */
1757 } else if ((ch->rslots & ~ch->toslots) == 0 &&
1758 et != MVS_ERR_TIMEOUT)
1759 mvs_rearm_timeout(dev);
1760 /* Unfreeze frozen command. */
1761 if (ch->frozen && !mvs_check_collision(dev, ch->frozen)) {
1762 union ccb *fccb = ch->frozen;
1764 mvs_begin_transaction(dev, fccb);
1765 xpt_release_simq(ch->sim, TRUE);
1767 /* Start PM timer. */
1768 if (ch->numrslots == 0 && ch->pm_level > 3 &&
1769 (ch->curr[ch->pm_present ? 15 : 0].caps & CTS_SATA_CAPS_D_PMREQ)) {
1770 callout_schedule(&ch->pm_timer,
1771 (ch->pm_level == 4) ? hz / 1000 : hz / 8);
1776 mvs_issue_recovery(device_t dev)
1778 struct mvs_channel *ch = device_get_softc(dev);
1780 struct ccb_ataio *ataio;
1781 struct ccb_scsiio *csio;
1784 /* Find some held command. */
1785 for (i = 0; i < MVS_MAX_SLOTS; i++) {
1789 ccb = xpt_alloc_ccb_nowait();
1791 device_printf(dev, "Unable to allocate recovery command\n");
1793 /* We can't do anything -- complete held commands. */
1794 for (i = 0; i < MVS_MAX_SLOTS; i++) {
1795 if (ch->hold[i] == NULL)
1797 ch->hold[i]->ccb_h.status &= ~CAM_STATUS_MASK;
1798 ch->hold[i]->ccb_h.status |= CAM_RESRC_UNAVAIL;
1799 xpt_done(ch->hold[i]);
1806 ccb->ccb_h = ch->hold[i]->ccb_h; /* Reuse old header. */
1807 if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1809 ccb->ccb_h.recovery_type = RECOVERY_READ_LOG;
1810 ccb->ccb_h.func_code = XPT_ATA_IO;
1811 ccb->ccb_h.flags = CAM_DIR_IN;
1812 ccb->ccb_h.timeout = 1000; /* 1s should be enough. */
1813 ataio = &ccb->ataio;
1814 ataio->data_ptr = malloc(512, M_MVS, M_NOWAIT);
1815 if (ataio->data_ptr == NULL) {
1818 "Unable to allocate memory for READ LOG command\n");
1821 ataio->dxfer_len = 512;
1822 bzero(&ataio->cmd, sizeof(ataio->cmd));
1823 ataio->cmd.flags = CAM_ATAIO_48BIT;
1824 ataio->cmd.command = 0x2F; /* READ LOG EXT */
1825 ataio->cmd.sector_count = 1;
1826 ataio->cmd.sector_count_exp = 0;
1827 ataio->cmd.lba_low = 0x10;
1828 ataio->cmd.lba_mid = 0;
1829 ataio->cmd.lba_mid_exp = 0;
1832 ccb->ccb_h.recovery_type = RECOVERY_REQUEST_SENSE;
1833 ccb->ccb_h.recovery_slot = i;
1834 ccb->ccb_h.func_code = XPT_SCSI_IO;
1835 ccb->ccb_h.flags = CAM_DIR_IN;
1836 ccb->ccb_h.status = 0;
1837 ccb->ccb_h.timeout = 1000; /* 1s should be enough. */
1839 csio->data_ptr = (void *)&ch->hold[i]->csio.sense_data;
1840 csio->dxfer_len = ch->hold[i]->csio.sense_len;
1842 bzero(&csio->cdb_io, sizeof(csio->cdb_io));
1843 csio->cdb_io.cdb_bytes[0] = 0x03;
1844 csio->cdb_io.cdb_bytes[4] = csio->dxfer_len;
1846 /* Freeze SIM while doing recovery. */
1847 ch->recoverycmd = 1;
1848 xpt_freeze_simq(ch->sim, 1);
1849 mvs_begin_transaction(dev, ccb);
1853 mvs_process_read_log(device_t dev, union ccb *ccb)
1855 struct mvs_channel *ch = device_get_softc(dev);
1857 struct ata_res *res;
1860 ch->recoverycmd = 0;
1862 data = ccb->ataio.data_ptr;
1863 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP &&
1864 (data[0] & 0x80) == 0) {
1865 for (i = 0; i < MVS_MAX_SLOTS; i++) {
1868 if (ch->hold[i]->ccb_h.target_id != ccb->ccb_h.target_id)
1870 if ((data[0] & 0x1F) == ch->holdtag[i]) {
1871 res = &ch->hold[i]->ataio.res;
1872 res->status = data[2];
1873 res->error = data[3];
1874 res->lba_low = data[4];
1875 res->lba_mid = data[5];
1876 res->lba_high = data[6];
1877 res->device = data[7];
1878 res->lba_low_exp = data[8];
1879 res->lba_mid_exp = data[9];
1880 res->lba_high_exp = data[10];
1881 res->sector_count = data[12];
1882 res->sector_count_exp = data[13];
1884 ch->hold[i]->ccb_h.status &= ~CAM_STATUS_MASK;
1885 ch->hold[i]->ccb_h.status |= CAM_REQUEUE_REQ;
1887 xpt_done(ch->hold[i]);
1892 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
1893 device_printf(dev, "Error while READ LOG EXT\n");
1894 else if ((data[0] & 0x80) == 0) {
1896 "Non-queued command error in READ LOG EXT\n");
1898 for (i = 0; i < MVS_MAX_SLOTS; i++) {
1901 if (ch->hold[i]->ccb_h.target_id != ccb->ccb_h.target_id)
1903 xpt_done(ch->hold[i]);
1908 free(ccb->ataio.data_ptr, M_MVS);
1910 xpt_release_simq(ch->sim, TRUE);
1914 mvs_process_request_sense(device_t dev, union ccb *ccb)
1916 struct mvs_channel *ch = device_get_softc(dev);
1919 ch->recoverycmd = 0;
1921 i = ccb->ccb_h.recovery_slot;
1922 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1923 ch->hold[i]->ccb_h.status |= CAM_AUTOSNS_VALID;
1925 ch->hold[i]->ccb_h.status &= ~CAM_STATUS_MASK;
1926 ch->hold[i]->ccb_h.status |= CAM_AUTOSENSE_FAIL;
1928 xpt_done(ch->hold[i]);
1932 xpt_release_simq(ch->sim, TRUE);
1936 mvs_wait(device_t dev, u_int s, u_int c, int t)
1941 while (((st = mvs_getstatus(dev, 0)) & (s | c)) != s) {
1944 device_printf(dev, "Wait status %02x\n", st);
1954 mvs_requeue_frozen(device_t dev)
1956 struct mvs_channel *ch = device_get_softc(dev);
1957 union ccb *fccb = ch->frozen;
1961 fccb->ccb_h.status = CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ;
1962 if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) {
1963 xpt_freeze_devq(fccb->ccb_h.path, 1);
1964 fccb->ccb_h.status |= CAM_DEV_QFRZN;
1971 mvs_reset_to(void *arg)
1974 struct mvs_channel *ch = device_get_softc(dev);
1977 if (ch->resetting == 0)
1980 if ((t = mvs_wait(dev, 0, ATA_S_BUSY | ATA_S_DRQ, 0)) >= 0) {
1983 "MVS reset: device ready after %dms\n",
1984 (310 - ch->resetting) * 100);
1987 xpt_release_simq(ch->sim, TRUE);
1990 if (ch->resetting == 0) {
1992 "MVS reset: device not ready after 31000ms\n");
1993 xpt_release_simq(ch->sim, TRUE);
1996 callout_schedule(&ch->reset_timer, hz / 10);
2000 mvs_errata(device_t dev)
2002 struct mvs_channel *ch = device_get_softc(dev);
2005 if (ch->quirks & MVS_Q_SOC65) {
2006 val = ATA_INL(ch->r_mem, SATA_PHYM3);
2007 val &= ~(0x3 << 27); /* SELMUPF = 1 */
2009 val &= ~(0x3 << 29); /* SELMUPI = 1 */
2011 ATA_OUTL(ch->r_mem, SATA_PHYM3, val);
2013 val = ATA_INL(ch->r_mem, SATA_PHYM4);
2014 val &= ~0x1; /* SATU_OD8 = 0 */
2015 val |= (0x1 << 16); /* reserved bit 16 = 1 */
2016 ATA_OUTL(ch->r_mem, SATA_PHYM4, val);
2018 val = ATA_INL(ch->r_mem, SATA_PHYM9_GEN2);
2019 val &= ~0xf; /* TXAMP[3:0] = 8 */
2021 val &= ~(0x1 << 14); /* TXAMP[4] = 0 */
2022 ATA_OUTL(ch->r_mem, SATA_PHYM9_GEN2, val);
2024 val = ATA_INL(ch->r_mem, SATA_PHYM9_GEN1);
2025 val &= ~0xf; /* TXAMP[3:0] = 8 */
2027 val &= ~(0x1 << 14); /* TXAMP[4] = 0 */
2028 ATA_OUTL(ch->r_mem, SATA_PHYM9_GEN1, val);
2033 mvs_reset(device_t dev)
2035 struct mvs_channel *ch = device_get_softc(dev);
2038 xpt_freeze_simq(ch->sim, 1);
2040 device_printf(dev, "MVS reset...\n");
2041 /* Forget about previous reset. */
2042 if (ch->resetting) {
2044 callout_stop(&ch->reset_timer);
2045 xpt_release_simq(ch->sim, TRUE);
2047 /* Requeue freezed command. */
2048 mvs_requeue_frozen(dev);
2049 /* Kill the engine and requeue all running commands. */
2050 mvs_set_edma_mode(dev, MVS_EDMA_OFF);
2051 ATA_OUTL(ch->r_mem, DMA_C, 0);
2052 for (i = 0; i < MVS_MAX_SLOTS; i++) {
2053 /* Do we have a running request on slot? */
2054 if (ch->slot[i].state < MVS_SLOT_RUNNING)
2056 /* XXX; Commands in loading state. */
2057 mvs_end_transaction(&ch->slot[i], MVS_ERR_INNOCENT);
2059 for (i = 0; i < MVS_MAX_SLOTS; i++) {
2062 xpt_done(ch->hold[i]);
2066 if (ch->toslots != 0)
2067 xpt_release_simq(ch->sim, TRUE);
2072 /* Tell the XPT about the event */
2073 xpt_async(AC_BUS_RESET, ch->path, NULL);
2074 ATA_OUTL(ch->r_mem, EDMA_IEM, 0);
2075 ATA_OUTL(ch->r_mem, EDMA_CMD, EDMA_CMD_EATARST);
2077 ATA_OUTL(ch->r_mem, EDMA_CMD, 0);
2079 /* Reset and reconnect PHY, */
2080 if (!mvs_sata_phy_reset(dev)) {
2082 device_printf(dev, "MVS reset: device not found\n");
2084 ATA_OUTL(ch->r_mem, SATA_SE, 0xffffffff);
2085 ATA_OUTL(ch->r_mem, EDMA_IEC, 0);
2086 ATA_OUTL(ch->r_mem, EDMA_IEM, ~EDMA_IE_TRANSIENT);
2087 xpt_release_simq(ch->sim, TRUE);
2091 device_printf(dev, "MVS reset: device found\n");
2092 /* Wait for clearing busy status. */
2093 if ((i = mvs_wait(dev, 0, ATA_S_BUSY | ATA_S_DRQ,
2094 dumping ? 31000 : 0)) < 0) {
2097 "MVS reset: device not ready after 31000ms\n");
2099 ch->resetting = 310;
2100 } else if (bootverbose)
2101 device_printf(dev, "MVS reset: device ready after %dms\n", i);
2103 ATA_OUTL(ch->r_mem, SATA_SE, 0xffffffff);
2104 ATA_OUTL(ch->r_mem, EDMA_IEC, 0);
2105 ATA_OUTL(ch->r_mem, EDMA_IEM, ~EDMA_IE_TRANSIENT);
2107 callout_reset(&ch->reset_timer, hz / 10, mvs_reset_to, dev);
2109 xpt_release_simq(ch->sim, TRUE);
2113 mvs_softreset(device_t dev, union ccb *ccb)
2115 struct mvs_channel *ch = device_get_softc(dev);
2116 int port = ccb->ccb_h.target_id & 0x0f;
2120 mvs_set_edma_mode(dev, MVS_EDMA_OFF);
2121 ATA_OUTB(ch->r_mem, SATA_SATAICTL, port << SATA_SATAICTL_PMPTX_SHIFT);
2122 ATA_OUTB(ch->r_mem, ATA_CONTROL, ATA_A_RESET);
2124 ATA_OUTB(ch->r_mem, ATA_CONTROL, 0);
2125 ccb->ccb_h.status &= ~CAM_STATUS_MASK;
2126 /* Wait for clearing busy status. */
2127 if ((i = mvs_wait(dev, 0, ATA_S_BUSY, ccb->ccb_h.timeout)) < 0) {
2128 ccb->ccb_h.status |= CAM_CMD_TIMEOUT;
2131 status = mvs_getstatus(dev, 0);
2132 if (status & ATA_S_ERROR)
2133 ccb->ccb_h.status |= CAM_ATA_STATUS_ERROR;
2135 ccb->ccb_h.status |= CAM_REQ_CMP;
2136 if (status & ATA_S_DRQ)
2141 mvs_tfd_read(dev, ccb);
2144 * XXX: If some device on PMP failed to soft-reset,
2145 * try to recover by sending dummy soft-reset to PMP.
2147 if (stuck && ch->pm_present && port != 15) {
2148 ATA_OUTB(ch->r_mem, SATA_SATAICTL,
2149 15 << SATA_SATAICTL_PMPTX_SHIFT);
2150 ATA_OUTB(ch->r_mem, ATA_CONTROL, ATA_A_RESET);
2152 ATA_OUTB(ch->r_mem, ATA_CONTROL, 0);
2153 mvs_wait(dev, 0, ATA_S_BUSY | ATA_S_DRQ, ccb->ccb_h.timeout);
2160 mvs_sata_connect(struct mvs_channel *ch)
2163 int timeout, found = 0;
2165 /* Wait up to 100ms for "connect well" */
2166 for (timeout = 0; timeout < 1000 ; timeout++) {
2167 status = ATA_INL(ch->r_mem, SATA_SS);
2168 if ((status & SATA_SS_DET_MASK) != SATA_SS_DET_NO_DEVICE)
2170 if (((status & SATA_SS_DET_MASK) == SATA_SS_DET_PHY_ONLINE) &&
2171 ((status & SATA_SS_SPD_MASK) != SATA_SS_SPD_NO_SPEED) &&
2172 ((status & SATA_SS_IPM_MASK) == SATA_SS_IPM_ACTIVE))
2174 if ((status & SATA_SS_DET_MASK) == SATA_SS_DET_PHY_OFFLINE) {
2176 device_printf(ch->dev, "SATA offline status=%08x\n",
2181 if (found == 0 && timeout >= 100)
2185 if (timeout >= 1000 || !found) {
2187 device_printf(ch->dev,
2188 "SATA connect timeout time=%dus status=%08x\n",
2189 timeout * 100, status);
2194 device_printf(ch->dev, "SATA connect time=%dus status=%08x\n",
2195 timeout * 100, status);
2197 /* Clear SATA error register */
2198 ATA_OUTL(ch->r_mem, SATA_SE, 0xffffffff);
2203 mvs_sata_phy_reset(device_t dev)
2205 struct mvs_channel *ch = device_get_softc(dev);
2209 sata_rev = ch->user[ch->pm_present ? 15 : 0].revision;
2211 val = SATA_SC_SPD_SPEED_GEN1;
2212 else if (sata_rev == 2)
2213 val = SATA_SC_SPD_SPEED_GEN2;
2214 else if (sata_rev == 3)
2215 val = SATA_SC_SPD_SPEED_GEN3;
2218 ATA_OUTL(ch->r_mem, SATA_SC,
2219 SATA_SC_DET_RESET | val |
2220 SATA_SC_IPM_DIS_PARTIAL | SATA_SC_IPM_DIS_SLUMBER);
2222 ATA_OUTL(ch->r_mem, SATA_SC,
2223 SATA_SC_DET_IDLE | val | ((ch->pm_level > 0) ? 0 :
2224 (SATA_SC_IPM_DIS_PARTIAL | SATA_SC_IPM_DIS_SLUMBER)));
2225 if (!mvs_sata_connect(ch)) {
2226 if (ch->pm_level > 0)
2227 ATA_OUTL(ch->r_mem, SATA_SC, SATA_SC_DET_DISABLE);
2234 mvs_check_ids(device_t dev, union ccb *ccb)
2236 struct mvs_channel *ch = device_get_softc(dev);
2238 if (ccb->ccb_h.target_id > ((ch->quirks & MVS_Q_GENI) ? 0 : 15)) {
2239 ccb->ccb_h.status = CAM_TID_INVALID;
2243 if (ccb->ccb_h.target_lun != 0) {
2244 ccb->ccb_h.status = CAM_LUN_INVALID;
2249 * It's a programming error to see AUXILIARY register requests.
2251 KASSERT(ccb->ccb_h.func_code != XPT_ATA_IO ||
2252 ((ccb->ataio.ata_flags & ATA_FLAG_AUX) == 0),
2253 ("AUX register unsupported"));
2258 mvsaction(struct cam_sim *sim, union ccb *ccb)
2260 device_t dev, parent;
2261 struct mvs_channel *ch;
2263 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("mvsaction func_code=%x\n",
2264 ccb->ccb_h.func_code));
2266 ch = (struct mvs_channel *)cam_sim_softc(sim);
2268 switch (ccb->ccb_h.func_code) {
2269 /* Common cases first */
2270 case XPT_ATA_IO: /* Execute the requested I/O operation */
2272 if (mvs_check_ids(dev, ccb))
2274 if (ch->devices == 0 ||
2275 (ch->pm_present == 0 &&
2276 ccb->ccb_h.target_id > 0 && ccb->ccb_h.target_id < 15)) {
2277 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
2280 ccb->ccb_h.recovery_type = RECOVERY_NONE;
2281 /* Check for command collision. */
2282 if (mvs_check_collision(dev, ccb)) {
2283 /* Freeze command. */
2285 /* We have only one frozen slot, so freeze simq also. */
2286 xpt_freeze_simq(ch->sim, 1);
2289 mvs_begin_transaction(dev, ccb);
2291 case XPT_ABORT: /* Abort the specified CCB */
2293 ccb->ccb_h.status = CAM_REQ_INVALID;
2295 case XPT_SET_TRAN_SETTINGS:
2297 struct ccb_trans_settings *cts = &ccb->cts;
2298 struct mvs_device *d;
2300 if (mvs_check_ids(dev, ccb))
2302 if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
2303 d = &ch->curr[ccb->ccb_h.target_id];
2305 d = &ch->user[ccb->ccb_h.target_id];
2306 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_REVISION)
2307 d->revision = cts->xport_specific.sata.revision;
2308 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_MODE)
2309 d->mode = cts->xport_specific.sata.mode;
2310 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT) {
2311 d->bytecount = min((ch->quirks & MVS_Q_GENIIE) ? 8192 : 2048,
2312 cts->xport_specific.sata.bytecount);
2314 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_TAGS)
2315 d->tags = min(MVS_MAX_SLOTS, cts->xport_specific.sata.tags);
2316 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_PM)
2317 ch->pm_present = cts->xport_specific.sata.pm_present;
2318 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_ATAPI)
2319 d->atapi = cts->xport_specific.sata.atapi;
2320 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
2321 d->caps = cts->xport_specific.sata.caps;
2322 ccb->ccb_h.status = CAM_REQ_CMP;
2325 case XPT_GET_TRAN_SETTINGS:
2326 /* Get default/user set transfer settings for the target */
2328 struct ccb_trans_settings *cts = &ccb->cts;
2329 struct mvs_device *d;
2332 if (mvs_check_ids(dev, ccb))
2334 if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
2335 d = &ch->curr[ccb->ccb_h.target_id];
2337 d = &ch->user[ccb->ccb_h.target_id];
2338 cts->protocol = PROTO_UNSPECIFIED;
2339 cts->protocol_version = PROTO_VERSION_UNSPECIFIED;
2340 cts->transport = XPORT_SATA;
2341 cts->transport_version = XPORT_VERSION_UNSPECIFIED;
2342 cts->proto_specific.valid = 0;
2343 cts->xport_specific.sata.valid = 0;
2344 if (cts->type == CTS_TYPE_CURRENT_SETTINGS &&
2345 (ccb->ccb_h.target_id == 15 ||
2346 (ccb->ccb_h.target_id == 0 && !ch->pm_present))) {
2347 status = ATA_INL(ch->r_mem, SATA_SS) & SATA_SS_SPD_MASK;
2348 if (status & 0x0f0) {
2349 cts->xport_specific.sata.revision =
2350 (status & 0x0f0) >> 4;
2351 cts->xport_specific.sata.valid |=
2352 CTS_SATA_VALID_REVISION;
2354 cts->xport_specific.sata.caps = d->caps & CTS_SATA_CAPS_D;
2355 // if (ch->pm_level)
2356 // cts->xport_specific.sata.caps |= CTS_SATA_CAPS_H_PMREQ;
2357 cts->xport_specific.sata.caps |= CTS_SATA_CAPS_H_AN;
2358 cts->xport_specific.sata.caps &=
2359 ch->user[ccb->ccb_h.target_id].caps;
2360 cts->xport_specific.sata.valid |= CTS_SATA_VALID_CAPS;
2362 cts->xport_specific.sata.revision = d->revision;
2363 cts->xport_specific.sata.valid |= CTS_SATA_VALID_REVISION;
2364 cts->xport_specific.sata.caps = d->caps;
2365 if (cts->type == CTS_TYPE_CURRENT_SETTINGS/* &&
2366 (ch->quirks & MVS_Q_GENIIE) == 0*/)
2367 cts->xport_specific.sata.caps &= ~CTS_SATA_CAPS_H_AN;
2368 cts->xport_specific.sata.valid |= CTS_SATA_VALID_CAPS;
2370 cts->xport_specific.sata.mode = d->mode;
2371 cts->xport_specific.sata.valid |= CTS_SATA_VALID_MODE;
2372 cts->xport_specific.sata.bytecount = d->bytecount;
2373 cts->xport_specific.sata.valid |= CTS_SATA_VALID_BYTECOUNT;
2374 cts->xport_specific.sata.pm_present = ch->pm_present;
2375 cts->xport_specific.sata.valid |= CTS_SATA_VALID_PM;
2376 cts->xport_specific.sata.tags = d->tags;
2377 cts->xport_specific.sata.valid |= CTS_SATA_VALID_TAGS;
2378 cts->xport_specific.sata.atapi = d->atapi;
2379 cts->xport_specific.sata.valid |= CTS_SATA_VALID_ATAPI;
2380 ccb->ccb_h.status = CAM_REQ_CMP;
2383 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
2384 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
2386 ccb->ccb_h.status = CAM_REQ_CMP;
2388 case XPT_TERM_IO: /* Terminate the I/O process */
2390 ccb->ccb_h.status = CAM_REQ_INVALID;
2392 case XPT_PATH_INQ: /* Path routing inquiry */
2394 struct ccb_pathinq *cpi = &ccb->cpi;
2396 parent = device_get_parent(dev);
2397 cpi->version_num = 1; /* XXX??? */
2398 cpi->hba_inquiry = PI_SDTR_ABLE;
2399 if (!(ch->quirks & MVS_Q_GENI)) {
2400 cpi->hba_inquiry |= PI_SATAPM;
2401 /* Gen-II is extremely slow with NCQ on PMP. */
2402 if ((ch->quirks & MVS_Q_GENIIE) || ch->pm_present == 0)
2403 cpi->hba_inquiry |= PI_TAG_ABLE;
2405 cpi->target_sprt = 0;
2406 cpi->hba_misc = PIM_SEQSCAN;
2407 cpi->hba_eng_cnt = 0;
2408 if (!(ch->quirks & MVS_Q_GENI))
2409 cpi->max_target = 15;
2411 cpi->max_target = 0;
2413 cpi->initiator_id = 0;
2414 cpi->bus_id = cam_sim_bus(sim);
2415 cpi->base_transfer_speed = 150000;
2416 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2417 strlcpy(cpi->hba_vid, "Marvell", HBA_IDLEN);
2418 strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
2419 cpi->unit_number = cam_sim_unit(sim);
2420 cpi->transport = XPORT_SATA;
2421 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
2422 cpi->protocol = PROTO_ATA;
2423 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
2424 cpi->maxio = maxphys;
2425 if ((ch->quirks & MVS_Q_SOC) == 0) {
2426 cpi->hba_vendor = pci_get_vendor(parent);
2427 cpi->hba_device = pci_get_device(parent);
2428 cpi->hba_subvendor = pci_get_subvendor(parent);
2429 cpi->hba_subdevice = pci_get_subdevice(parent);
2431 cpi->ccb_h.status = CAM_REQ_CMP;
2435 ccb->ccb_h.status = CAM_REQ_INVALID;
2442 mvspoll(struct cam_sim *sim)
2444 struct mvs_channel *ch = (struct mvs_channel *)cam_sim_softc(sim);
2445 struct mvs_intr_arg arg;
2448 arg.cause = 2 | 4; /* XXX */
2450 if (ch->resetting != 0 &&
2451 (--ch->resetpolldiv <= 0 || !callout_pending(&ch->reset_timer))) {
2452 ch->resetpolldiv = 1000;
2453 mvs_reset_to(ch->dev);