From owner-svn-src-all@FreeBSD.ORG Thu Jul 26 13:44:49 2012 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id AD6291065673; Thu, 26 Jul 2012 13:44:49 +0000 (UTC) (envelope-from mav@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id 950CD8FC12; Thu, 26 Jul 2012 13:44:49 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.4/8.14.4) with ESMTP id q6QDinQm089869; Thu, 26 Jul 2012 13:44:49 GMT (envelope-from mav@svn.freebsd.org) Received: (from mav@localhost) by svn.freebsd.org (8.14.4/8.14.4/Submit) id q6QDinAs089862; Thu, 26 Jul 2012 13:44:49 GMT (envelope-from mav@svn.freebsd.org) Message-Id: <201207261344.q6QDinAs089862@svn.freebsd.org> From: Alexander Motin Date: Thu, 26 Jul 2012 13:44:49 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r238805 - in head: share/man/man4 sys/conf sys/dev/ahci sys/modules/ahci X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 26 Jul 2012 13:44:49 -0000 Author: mav Date: Thu Jul 26 13:44:48 2012 New Revision: 238805 URL: http://svn.freebsd.org/changeset/base/238805 Log: Refactor enclosure manegement support in ahci(4). Move it out into separate subdevice ahciem. Emulate SEMB SES device from AHCI LED interface to expose it to users in form of ses(4) CAM device. If we ever see AHCI controllers supporting SES of SAF-TE over I2C as described by specification, they should fit well into this new picture. Sponsored by: iXsystems, Inc. Added: head/sys/dev/ahci/ahciem.c (contents, props changed) Modified: head/share/man/man4/ahci.4 head/sys/conf/files head/sys/dev/ahci/ahci.c head/sys/dev/ahci/ahci.h head/sys/modules/ahci/Makefile Modified: head/share/man/man4/ahci.4 ============================================================================== --- head/share/man/man4/ahci.4 Thu Jul 26 12:18:23 2012 (r238804) +++ head/share/man/man4/ahci.4 Thu Jul 26 13:44:48 2012 (r238805) @@ -1,4 +1,4 @@ -.\" Copyright (c) 2009 Alexander Motin +.\" Copyright (c) 2009-2012 Alexander Motin .\" All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without @@ -24,7 +24,7 @@ .\" .\" $FreeBSD$ .\" -.Dd June 18, 2012 +.Dd July 25, 2012 .Dt AHCI 4 .Os .Sh NAME @@ -132,7 +132,9 @@ Driver supports "LED" enclosure manageme When supported by hardware, it allows to control per-port activity, locate and fault LEDs via the .Xr led 4 -API for localization and status reporting purposes. +API or emulated +.Xr ses 4 +device for localization and status reporting purposes. Supporting AHCI controllers may transmit that information to the backplane controllers via SGPIO interface. Backplane controllers interpret received statuses in some way (IBPI standard) to report them using present indicators. @@ -153,11 +155,11 @@ it supports AHCI part of legacy-PATA + A such as JMicron JMB36x and Marvell 88SE61xx. .Sh FILES .Bl -tag -width /dev/led/ahcich*.locate -.It Pa /dev/led/ahcich*.act +.It Pa /dev/led/ahci*.*.act activity LED device nodes -.It Pa /dev/led/ahcich*.fault +.It Pa /dev/led/ahci*.*.fault fault LED device nodes -.It Pa /dev/led/ahcich*.locate +.It Pa /dev/led/ahci*.*.locate locate LED device nodes .El .Sh SEE ALSO @@ -166,7 +168,8 @@ locate LED device nodes .Xr cam 4 , .Xr cd 4 , .Xr da 4 , -.Xr sa 4 +.Xr sa 4 , +.Xr ses 4 .Sh HISTORY The .Nm Modified: head/sys/conf/files ============================================================================== --- head/sys/conf/files Thu Jul 26 12:18:23 2012 (r238804) +++ head/sys/conf/files Thu Jul 26 13:44:48 2012 (r238805) @@ -639,6 +639,7 @@ dev/aha/aha_isa.c optional aha isa dev/aha/aha_mca.c optional aha mca dev/ahb/ahb.c optional ahb eisa dev/ahci/ahci.c optional ahci pci +dev/ahci/ahciem.c optional ahci pci dev/aic/aic.c optional aic dev/aic/aic_pccard.c optional aic pccard dev/aic7xxx/ahc_eisa.c optional ahc eisa Modified: head/sys/dev/ahci/ahci.c ============================================================================== --- head/sys/dev/ahci/ahci.c Thu Jul 26 12:18:23 2012 (r238804) +++ head/sys/dev/ahci/ahci.c Thu Jul 26 13:44:48 2012 (r238805) @@ -1,5 +1,5 @@ /*- - * Copyright (c) 2009 Alexander Motin + * Copyright (c) 2009-2012 Alexander Motin * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -31,21 +31,16 @@ __FBSDID("$FreeBSD$"); #include #include #include -#include #include #include #include #include #include #include -#include -#include -#include #include #include #include #include -#include #include #include #include "ahci.h" @@ -69,7 +64,6 @@ static int ahci_ch_resume(device_t dev); static void ahci_ch_pm(void *arg); static void ahci_ch_intr_locked(void *data); static void ahci_ch_intr(void *data); -static void ahci_ch_led(void *priv, int onoff); static int ahci_ctlr_reset(device_t dev); static int ahci_ctlr_setup(device_t dev); static void ahci_begin_transaction(device_t dev, union ccb *ccb); @@ -441,7 +435,6 @@ ahci_attach(device_t dev) ctlr->caps &= ~AHCI_CAP_SNCQ; if ((ctlr->caps & AHCI_CAP_CCCS) == 0) ctlr->ccc = 0; - mtx_init(&ctlr->em_mtx, "AHCI EM lock", NULL, MTX_DEF); ctlr->emloc = ATA_INL(ctlr->r_mem, AHCI_EM_LOC); ahci_ctlr_setup(dev); /* Setup interrupts. */ @@ -494,17 +487,6 @@ ahci_attach(device_t dev) (ctlr->caps2 & AHCI_CAP2_NVMP) ? " NVMP":"", (ctlr->caps2 & AHCI_CAP2_BOH) ? " BOH":""); } - if (bootverbose && (ctlr->caps & AHCI_CAP_EMS)) { - device_printf(dev, "EM Caps:%s%s%s%s%s%s%s%s\n", - (ctlr->capsem & AHCI_EM_PM) ? " PM":"", - (ctlr->capsem & AHCI_EM_ALHD) ? " ALHD":"", - (ctlr->capsem & AHCI_EM_XMT) ? " XMT":"", - (ctlr->capsem & AHCI_EM_SMB) ? " SMB":"", - (ctlr->capsem & AHCI_EM_SGPIO) ? " SGPIO":"", - (ctlr->capsem & AHCI_EM_SES2) ? " SES-2":"", - (ctlr->capsem & AHCI_EM_SAFTE) ? " SAF-TE":"", - (ctlr->capsem & AHCI_EM_LED) ? " LED":""); - } /* Attach all channels on this controller */ for (unit = 0; unit < ctlr->channels; unit++) { child = device_add_child(dev, "ahcich", -1); @@ -516,6 +498,13 @@ ahci_attach(device_t dev) if ((ctlr->ichannels & (1 << unit)) == 0) device_disable(child); } + if (ctlr->caps & AHCI_CAP_EMS) { + child = device_add_child(dev, "ahciem", -1); + if (child == NULL) + device_printf(dev, "failed to add enclosure device\n"); + else + device_set_ivars(child, (void *)(intptr_t)-1); + } bus_generic_attach(dev); return 0; } @@ -543,7 +532,6 @@ ahci_detach(device_t dev) rman_fini(&ctlr->sc_iomem); if (ctlr->r_mem) bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem); - mtx_destroy(&ctlr->em_mtx); return (0); } @@ -753,16 +741,34 @@ ahci_alloc_resource(device_t dev, device u_long start, u_long end, u_long count, u_int flags) { struct ahci_controller *ctlr = device_get_softc(dev); - int unit = ((struct ahci_channel *)device_get_softc(child))->unit; - struct resource *res = NULL; - int offset = AHCI_OFFSET + (unit << 7); + struct resource *res; long st; + int offset, size, unit; + unit = (intptr_t)device_get_ivars(child); + res = NULL; switch (type) { case SYS_RES_MEMORY: + if (unit >= 0) { + offset = AHCI_OFFSET + (unit << 7); + size = 128; + } else if (*rid == 0) { + offset = AHCI_EM_CTL; + size = 4; + } else { + offset = (ctlr->emloc & 0xffff0000) >> 14; + size = (ctlr->emloc & 0x0000ffff) << 2; + if (*rid != 1) { + if (*rid == 2 && (ctlr->capsem & + (AHCI_EM_XMT | AHCI_EM_SMB)) == 0) + offset += size; + else + break; + } + } st = rman_get_start(ctlr->r_mem); res = rman_reserve_resource(&ctlr->sc_iomem, st + offset, - st + offset + 127, 128, RF_ACTIVE, child); + st + offset + size - 1, size, RF_ACTIVE, child); if (res) { bus_space_handle_t bsh; bus_space_tag_t bst; @@ -830,13 +836,13 @@ ahci_teardown_intr(device_t dev, device_ static int ahci_print_child(device_t dev, device_t child) { - int retval; + int retval, channel; retval = bus_print_child_header(dev, child); - retval += printf(" at channel %d", - (int)(intptr_t)device_get_ivars(child)); + channel = (int)(intptr_t)device_get_ivars(child); + if (channel >= 0) + retval += printf(" at channel %d", channel); retval += bus_print_child_footer(dev, child); - return (retval); } @@ -844,9 +850,11 @@ static int ahci_child_location_str(device_t dev, device_t child, char *buf, size_t buflen) { + int channel; - snprintf(buf, buflen, "channel=%d", - (int)(intptr_t)device_get_ivars(child)); + channel = (int)(intptr_t)device_get_ivars(child); + if (channel >= 0) + snprintf(buf, buflen, "channel=%d", channel); return (0); } @@ -910,7 +918,6 @@ ahci_ch_attach(device_t dev) struct cam_devq *devq; int rid, error, i, sata_rev = 0; u_int32_t version; - char buf[32]; ch->dev = dev; ch->unit = (intptr_t)device_get_ivars(dev); @@ -950,7 +957,7 @@ ahci_ch_attach(device_t dev) ch->user[i].caps |= CTS_SATA_CAPS_H_DMAAA | CTS_SATA_CAPS_H_AN; } - rid = ch->unit; + rid = 0; if (!(ch->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE))) return (ENXIO); @@ -1019,25 +1026,6 @@ ahci_ch_attach(device_t dev) ahci_ch_pm, dev); } mtx_unlock(&ch->mtx); - if ((ch->caps & AHCI_CAP_EMS) && - (ctlr->capsem & AHCI_EM_LED)) { - for (i = 0; i < AHCI_NUM_LEDS; i++) { - ch->leds[i].dev = dev; - ch->leds[i].num = i; - } - if ((ctlr->capsem & AHCI_EM_ALHD) == 0) { - snprintf(buf, sizeof(buf), "%s.act", - device_get_nameunit(dev)); - ch->leds[0].led = led_create(ahci_ch_led, - &ch->leds[0], buf); - } - snprintf(buf, sizeof(buf), "%s.locate", - device_get_nameunit(dev)); - ch->leds[1].led = led_create(ahci_ch_led, &ch->leds[1], buf); - snprintf(buf, sizeof(buf), "%s.fault", - device_get_nameunit(dev)); - ch->leds[2].led = led_create(ahci_ch_led, &ch->leds[2], buf); - } return (0); err3: @@ -1057,12 +1045,7 @@ static int ahci_ch_detach(device_t dev) { struct ahci_channel *ch = device_get_softc(dev); - int i; - for (i = 0; i < AHCI_NUM_LEDS; i++) { - if (ch->leds[i].led) - led_destroy(ch->leds[i].led); - } mtx_lock(&ch->mtx); xpt_async(AC_LOST_DEVICE, ch->path, NULL); /* Forget about reset. */ @@ -1185,47 +1168,6 @@ static driver_t ahcich_driver = { }; DRIVER_MODULE(ahcich, ahci, ahcich_driver, ahcich_devclass, 0, 0); -static void -ahci_ch_setleds(device_t dev) -{ - struct ahci_channel *ch; - struct ahci_controller *ctlr; - size_t buf; - int i, timeout; - int16_t val; - - ctlr = device_get_softc(device_get_parent(dev)); - ch = device_get_softc(dev); - - val = 0; - for (i = 0; i < AHCI_NUM_LEDS; i++) - val |= ch->leds[i].state << (i * 3); - - buf = (ctlr->emloc & 0xffff0000) >> 14; - mtx_lock(&ctlr->em_mtx); - timeout = 1000; - while (ATA_INL(ctlr->r_mem, AHCI_EM_CTL) & (AHCI_EM_TM | AHCI_EM_RST) && - --timeout > 0) - DELAY(1000); - if (timeout == 0) - device_printf(dev, "EM timeout\n"); - ATA_OUTL(ctlr->r_mem, buf, (1 << 8) | (0 << 16) | (0 << 24)); - ATA_OUTL(ctlr->r_mem, buf + 4, ch->unit | (val << 16)); - ATA_OUTL(ctlr->r_mem, AHCI_EM_CTL, AHCI_EM_TM); - mtx_unlock(&ctlr->em_mtx); -} - -static void -ahci_ch_led(void *priv, int onoff) -{ - struct ahci_led *led; - - led = (struct ahci_led *)priv; - - led->state = onoff; - ahci_ch_setleds(led->dev); -} - struct ahci_dc_cb_args { bus_addr_t maddr; int error; Modified: head/sys/dev/ahci/ahci.h ============================================================================== --- head/sys/dev/ahci/ahci.h Thu Jul 26 12:18:23 2012 (r238804) +++ head/sys/dev/ahci/ahci.h Thu Jul 26 13:44:48 2012 (r238805) @@ -1,6 +1,6 @@ /*- * Copyright (c) 1998 - 2008 Søren Schmidt - * Copyright (c) 2009 Alexander Motin + * Copyright (c) 2009-2012 Alexander Motin * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -395,7 +395,6 @@ struct ahci_channel { struct ata_dma dma; /* DMA data */ struct cam_sim *sim; struct cam_path *path; - struct ahci_led leds[3]; uint32_t caps; /* Controller capabilities */ uint32_t caps2; /* Controller capabilities */ uint32_t chcaps; /* Channel capabilities */ @@ -435,6 +434,22 @@ struct ahci_channel { struct ahci_device curr[16]; /* Current settings */ }; +struct ahci_enclosure { + device_t dev; /* Device handle */ + struct resource *r_memc; /* Control register */ + struct resource *r_memt; /* Transmit buffer */ + struct resource *r_memr; /* Recieve buffer */ + struct cam_sim *sim; + struct cam_path *path; + struct mtx mtx; /* state lock */ + struct ahci_led leds[AHCI_MAX_PORTS * 3]; + uint32_t capsem; /* Controller capabilities */ + uint8_t status[AHCI_MAX_PORTS][4]; /* ArrayDev statuses */ + int quirks; + int channels; + int ichannels; +}; + /* structure describing a AHCI controller */ struct ahci_controller { device_t dev; @@ -465,7 +480,6 @@ struct ahci_controller { void (*function)(void *); void *argument; } interrupt[AHCI_MAX_PORTS]; - struct mtx em_mtx; /* EM access lock */ }; enum ahci_err_type { Added: head/sys/dev/ahci/ahciem.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/sys/dev/ahci/ahciem.c Thu Jul 26 13:44:48 2012 (r238805) @@ -0,0 +1,600 @@ +/*- + * Copyright (c) 2012 Alexander Motin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer, + * without modification, immediately at the beginning of the file. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "ahci.h" + +#include +#include +#include +#include +#include +#include + +/* local prototypes */ +static void ahciemaction(struct cam_sim *sim, union ccb *ccb); +static void ahciempoll(struct cam_sim *sim); +static int ahci_em_reset(device_t dev); +static void ahci_em_led(void *priv, int onoff); +static void ahci_em_setleds(device_t dev, int c); + +static int +ahci_em_probe(device_t dev) +{ + + device_set_desc_copy(dev, "AHCI enclosure management bridge"); + return (0); +} + +static int +ahci_em_attach(device_t dev) +{ + device_t parent = device_get_parent(dev); + struct ahci_controller *ctlr = device_get_softc(parent); + struct ahci_enclosure *enc = device_get_softc(dev); + struct cam_devq *devq; + int i, c, rid, error; + char buf[32]; + + enc->dev = dev; + enc->quirks = ctlr->quirks; + enc->channels = ctlr->channels; + enc->ichannels = ctlr->ichannels; + mtx_init(&enc->mtx, "AHCI enclosure lock", NULL, MTX_DEF); + rid = 0; + if (!(enc->r_memc = bus_alloc_resource_any(dev, SYS_RES_MEMORY, + &rid, RF_ACTIVE))) + return (ENXIO); + enc->capsem = ATA_INL(enc->r_memc, 0);; + rid = 1; + if (!(enc->r_memt = bus_alloc_resource_any(dev, SYS_RES_MEMORY, + &rid, RF_ACTIVE))) { + error = ENXIO; + goto err0; + } + if ((enc->capsem & (AHCI_EM_XMT | AHCI_EM_SMB)) == 0) { + rid = 2; + if (!(enc->r_memr = bus_alloc_resource_any(dev, SYS_RES_MEMORY, + &rid, RF_ACTIVE))) { + error = ENXIO; + goto err0; + } + } else + enc->r_memr = NULL; + mtx_lock(&enc->mtx); + ahci_em_reset(dev); + rid = ATA_IRQ_RID; + /* Create the device queue for our SIM. */ + devq = cam_simq_alloc(1); + if (devq == NULL) { + device_printf(dev, "Unable to allocate SIM queue\n"); + error = ENOMEM; + goto err1; + } + /* Construct SIM entry */ + enc->sim = cam_sim_alloc(ahciemaction, ahciempoll, "ahciem", enc, + device_get_unit(dev), &enc->mtx, + 1, 0, devq); + if (enc->sim == NULL) { + cam_simq_free(devq); + device_printf(dev, "Unable to allocate SIM\n"); + error = ENOMEM; + goto err1; + } + if (xpt_bus_register(enc->sim, dev, 0) != CAM_SUCCESS) { + device_printf(dev, "unable to register xpt bus\n"); + error = ENXIO; + goto err2; + } + if (xpt_create_path(&enc->path, /*periph*/NULL, cam_sim_path(enc->sim), + CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) { + device_printf(dev, "Unable to create path\n"); + error = ENXIO; + goto err3; + } + mtx_unlock(&enc->mtx); + if (bootverbose) { + device_printf(dev, "Caps:%s%s%s%s%s%s%s%s\n", + (enc->capsem & AHCI_EM_PM) ? " PM":"", + (enc->capsem & AHCI_EM_ALHD) ? " ALHD":"", + (enc->capsem & AHCI_EM_XMT) ? " XMT":"", + (enc->capsem & AHCI_EM_SMB) ? " SMB":"", + (enc->capsem & AHCI_EM_SGPIO) ? " SGPIO":"", + (enc->capsem & AHCI_EM_SES2) ? " SES-2":"", + (enc->capsem & AHCI_EM_SAFTE) ? " SAF-TE":"", + (enc->capsem & AHCI_EM_LED) ? " LED":""); + } + if ((enc->capsem & AHCI_EM_LED)) { + for (c = 0; c < enc->channels; c++) { + if ((enc->ichannels & (1 << c)) == 0) + continue; + for (i = 0; i < AHCI_NUM_LEDS; i++) { + enc->leds[c * AHCI_NUM_LEDS + i].dev = dev; + enc->leds[c * AHCI_NUM_LEDS + i].num = + c * AHCI_NUM_LEDS + i; + } + if ((enc->capsem & AHCI_EM_ALHD) == 0) { + snprintf(buf, sizeof(buf), "%s.%d.act", + device_get_nameunit(parent), c); + enc->leds[c * AHCI_NUM_LEDS + 0].led = + led_create(ahci_em_led, + &enc->leds[c * AHCI_NUM_LEDS + 0], buf); + } + snprintf(buf, sizeof(buf), "%s.%d.locate", + device_get_nameunit(parent), c); + enc->leds[c * AHCI_NUM_LEDS + 1].led = + led_create(ahci_em_led, + &enc->leds[c * AHCI_NUM_LEDS + 1], buf); + snprintf(buf, sizeof(buf), "%s.%d.fault", + device_get_nameunit(parent), c); + enc->leds[c * AHCI_NUM_LEDS + 2].led = + led_create(ahci_em_led, + &enc->leds[c * AHCI_NUM_LEDS + 2], buf); + } + } + return (0); + +err3: + xpt_bus_deregister(cam_sim_path(enc->sim)); +err2: + cam_sim_free(enc->sim, /*free_devq*/TRUE); +err1: + mtx_unlock(&enc->mtx); + if (enc->r_memr) + bus_release_resource(dev, SYS_RES_MEMORY, 2, enc->r_memr); +err0: + if (enc->r_memt) + bus_release_resource(dev, SYS_RES_MEMORY, 1, enc->r_memt); + bus_release_resource(dev, SYS_RES_MEMORY, 0, enc->r_memc); + mtx_destroy(&enc->mtx); + return (error); +} + +static int +ahci_em_detach(device_t dev) +{ + struct ahci_enclosure *enc = device_get_softc(dev); + int i; + + for (i = 0; i < enc->channels * AHCI_NUM_LEDS; i++) { + if (enc->leds[i].led) + led_destroy(enc->leds[i].led); + } + mtx_lock(&enc->mtx); + xpt_async(AC_LOST_DEVICE, enc->path, NULL); + xpt_free_path(enc->path); + xpt_bus_deregister(cam_sim_path(enc->sim)); + cam_sim_free(enc->sim, /*free_devq*/TRUE); + mtx_unlock(&enc->mtx); + + bus_release_resource(dev, SYS_RES_MEMORY, 0, enc->r_memc); + bus_release_resource(dev, SYS_RES_MEMORY, 1, enc->r_memt); + if (enc->r_memr) + bus_release_resource(dev, SYS_RES_MEMORY, 2, enc->r_memr); + mtx_destroy(&enc->mtx); + return (0); +} + +static int +ahci_em_reset(device_t dev) +{ + struct ahci_enclosure *enc; + int i, timeout; + + enc = device_get_softc(dev); + ATA_OUTL(enc->r_memc, 0, AHCI_EM_RST); + timeout = 1000; + while ((ATA_INL(enc->r_memc, 0) & AHCI_EM_RST) && + --timeout > 0) + DELAY(1000); + if (timeout == 0) { + device_printf(dev, "EM timeout\n"); + return (1); + } + for (i = 0; i < enc->channels; i++) + ahci_em_setleds(dev, i); + return (0); +} + +static int +ahci_em_suspend(device_t dev) +{ + struct ahci_enclosure *enc = device_get_softc(dev); + + mtx_lock(&enc->mtx); + xpt_freeze_simq(enc->sim, 1); + mtx_unlock(&enc->mtx); + return (0); +} + +static int +ahci_em_resume(device_t dev) +{ + struct ahci_enclosure *enc = device_get_softc(dev); + + mtx_lock(&enc->mtx); + ahci_em_reset(dev); + xpt_release_simq(enc->sim, TRUE); + mtx_unlock(&enc->mtx); + return (0); +} + +devclass_t ahciem_devclass; +static device_method_t ahciem_methods[] = { + DEVMETHOD(device_probe, ahci_em_probe), + DEVMETHOD(device_attach, ahci_em_attach), + DEVMETHOD(device_detach, ahci_em_detach), + DEVMETHOD(device_suspend, ahci_em_suspend), + DEVMETHOD(device_resume, ahci_em_resume), + { 0, 0 } +}; +static driver_t ahciem_driver = { + "ahciem", + ahciem_methods, + sizeof(struct ahci_enclosure) +}; +DRIVER_MODULE(ahciem, ahci, ahciem_driver, ahciem_devclass, 0, 0); + +static void +ahci_em_setleds(device_t dev, int c) +{ + struct ahci_enclosure *enc; + int timeout; + int16_t val; + + enc = device_get_softc(dev); + + val = 0; + if (enc->status[c][2] & 0x80) /* Activity */ + val |= (1 << 0); + if (enc->status[c][2] & SESCTL_RQSID) /* Identification */ + val |= (1 << 3); + else if (enc->status[c][3] & SESCTL_RQSFLT) /* Fault */ + val |= (1 << 6); + else if (enc->status[c][1] & 0x02) /* Rebuild */ + val |= (1 << 6) | (1 << 3); + + timeout = 10000; + while (ATA_INL(enc->r_memc, 0) & (AHCI_EM_TM | AHCI_EM_RST) && + --timeout > 0) + DELAY(100); + if (timeout == 0) + device_printf(dev, "Transmit timeout\n"); + ATA_OUTL(enc->r_memt, 0, (1 << 8) | (0 << 16) | (0 << 24)); + ATA_OUTL(enc->r_memt, 4, c | (0 << 8) | (val << 16)); + ATA_OUTL(enc->r_memc, 0, AHCI_EM_TM); +} + +static void +ahci_em_led(void *priv, int onoff) +{ + struct ahci_led *led; + struct ahci_enclosure *enc; + int c, l; + + led = (struct ahci_led *)priv; + enc = device_get_softc(led->dev); + c = led->num / AHCI_NUM_LEDS; + l = led->num % AHCI_NUM_LEDS; + + if (l == 0) { + if (onoff) + enc->status[c][2] |= 0x80; + else + enc->status[c][2] &= ~0x80; + } else if (l == 1) { + if (onoff) + enc->status[c][2] |= SESCTL_RQSID; + else + enc->status[c][2] &= ~SESCTL_RQSID; + } else if (l == 2) { + if (onoff) + enc->status[c][3] |= SESCTL_RQSFLT; + else + enc->status[c][3] &= SESCTL_RQSFLT; + } + ahci_em_setleds(led->dev, c); +} + +static int +ahci_check_ids(device_t dev, union ccb *ccb) +{ + + if (ccb->ccb_h.target_id != 0) { + ccb->ccb_h.status = CAM_TID_INVALID; + xpt_done(ccb); + return (-1); + } + if (ccb->ccb_h.target_lun != 0) { + ccb->ccb_h.status = CAM_LUN_INVALID; + xpt_done(ccb); + return (-1); + } + return (0); +} + +static void +ahci_em_emulate_ses_on_led(device_t dev, union ccb *ccb) +{ + struct ahci_enclosure *enc; + struct ses_status_page *page; + struct ses_status_array_dev_slot *ads, *ads0; + struct ses_elm_desc_hdr *elmd; + uint8_t *buf; + int i; + + enc = device_get_softc(dev); + buf = ccb->ataio.data_ptr; + + /* General request validation. */ + if (ccb->ataio.cmd.command != ATA_SEP_ATTN || + ccb->ataio.dxfer_len < ccb->ataio.cmd.sector_count * 4) { + ccb->ccb_h.status = CAM_REQ_INVALID; + goto out; + } + + /* SEMB IDENTIFY */ + if (ccb->ataio.cmd.features == 0xEC && + ccb->ataio.cmd.sector_count >= 16) { + bzero(buf, ccb->ataio.dxfer_len); + buf[0] = 64; /* Valid bytes. */ + strncpy(&buf[10], "AHCI ", SID_VENDOR_SIZE); + strncpy(&buf[18], "SGPIO Enclosure ", SID_PRODUCT_SIZE); + strncpy(&buf[34], "1.00", SID_REVISION_SIZE); + strncpy(&buf[39], "0001", 4); + strncpy(&buf[43], "S-E-S ", 6); + strncpy(&buf[49], "2.00", 4); + ccb->ccb_h.status = CAM_REQ_CMP; + goto out; + } + + /* SEMB RECEIVE DIAGNOSTIC RESULT (0) */ + page = (struct ses_status_page *)buf; + if (ccb->ataio.cmd.lba_low == 0x02 && + ccb->ataio.cmd.features == 0x00 && + ccb->ataio.cmd.sector_count >= 2) { + bzero(buf, ccb->ataio.dxfer_len); + page->hdr.page_code = 0; + scsi_ulto2b(3, page->hdr.length); + buf[4] = 0; + buf[5] = 1; + buf[6] = 2; + ccb->ccb_h.status = CAM_REQ_CMP; + goto out; + } + + /* SEMB RECEIVE DIAGNOSTIC RESULT (1) */ + if (ccb->ataio.cmd.lba_low == 0x02 && + ccb->ataio.cmd.features == 0x01 && + ccb->ataio.cmd.sector_count >= 13) { + struct ses_enc_desc *ed; + struct ses_elm_type_desc *td; + + bzero(buf, ccb->ataio.dxfer_len); + page->hdr.page_code = 0x01; + scsi_ulto2b(4 + 4 + 36 + 4, page->hdr.length); + ed = (struct ses_enc_desc *)&buf[8]; + ed->byte0 = 0x11; + ed->subenc_id = 0; + ed->num_types = 1; + ed->length = 36; + strncpy(ed->vendor_id, "AHCI ", SID_VENDOR_SIZE); + strncpy(ed->product_id, "SGPIO Enclosure ", SID_PRODUCT_SIZE); + strncpy(ed->product_rev, " ", SID_REVISION_SIZE); + td = (struct ses_elm_type_desc *)ses_enc_desc_next(ed); + td->etype_elm_type = 0x17; + td->etype_maxelt = enc->channels; + td->etype_subenc = 0; + td->etype_txt_len = 0; + ccb->ccb_h.status = CAM_REQ_CMP; + goto out; + } + + /* SEMB RECEIVE DIAGNOSTIC RESULT (2) */ + if (ccb->ataio.cmd.lba_low == 0x02 && + ccb->ataio.cmd.features == 0x02 && + ccb->ataio.cmd.sector_count >= (3 + enc->channels)) { + bzero(buf, ccb->ataio.dxfer_len); + page->hdr.page_code = 0x02; + scsi_ulto2b(4 + 4 * (1 + enc->channels), + page->hdr.length); + for (i = 0; i < enc->channels; i++) { + ads = &page->elements[i + 1].array_dev_slot; + memcpy(ads, enc->status[i], 4); + ads->common.bytes[0] |= + (enc->ichannels & (1 << i)) ? + SES_OBJSTAT_UNKNOWN : + SES_OBJSTAT_NOTINSTALLED; + } + ccb->ccb_h.status = CAM_REQ_CMP; + goto out; + } + + /* SEMB SEND DIAGNOSTIC (2) */ + if (ccb->ataio.cmd.lba_low == 0x82 && + ccb->ataio.cmd.features == 0x02 && + ccb->ataio.cmd.sector_count >= (3 + enc->channels)) { + ads0 = &page->elements[0].array_dev_slot; + for (i = 0; i < enc->channels; i++) { + ads = &page->elements[i + 1].array_dev_slot; + if (ads->common.bytes[0] & SESCTL_CSEL) { + enc->status[i][0] = 0; + enc->status[i][1] = + ads->bytes[0] & 0x02; + enc->status[i][2] = + ads->bytes[1] & (0x80 | SESCTL_RQSID); + enc->status[i][3] = + ads->bytes[2] & SESCTL_RQSFLT; + ahci_em_setleds(dev, i); + } else if (ads0->common.bytes[0] & SESCTL_CSEL) { + enc->status[i][0] = 0; + enc->status[i][1] = + ads0->bytes[0] & 0x02; + enc->status[i][2] = + ads0->bytes[1] & (0x80 | SESCTL_RQSID); + enc->status[i][3] = + ads0->bytes[2] & SESCTL_RQSFLT; + ahci_em_setleds(dev, i); + } + } + ccb->ccb_h.status = CAM_REQ_CMP; + goto out; + } + + /* SEMB RECEIVE DIAGNOSTIC RESULT (7) */ + if (ccb->ataio.cmd.lba_low == 0x02 && + ccb->ataio.cmd.features == 0x07 && + ccb->ataio.cmd.sector_count >= (3 + 3 * enc->channels)) { + bzero(buf, ccb->ataio.dxfer_len); + page->hdr.page_code = 0x07; + scsi_ulto2b(4 + 4 + 12 * enc->channels, + page->hdr.length); + for (i = 0; i < enc->channels; i++) { + elmd = (struct ses_elm_desc_hdr *)&buf[8 + 4 + 12 * i]; + scsi_ulto2b(8, elmd->length); + snprintf((char *)(elmd + 1), 9, "SLOT %03d", i); + } + ccb->ccb_h.status = CAM_REQ_CMP; + goto out; + } + + ccb->ccb_h.status = CAM_REQ_INVALID; +out: + xpt_done(ccb); +} + +static void +ahci_em_begin_transaction(device_t dev, union ccb *ccb) +{ + struct ahci_enclosure *enc; + struct ata_res *res; + + enc = device_get_softc(dev); + res = &ccb->ataio.res; + bzero(res, sizeof(*res)); + if ((ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) && + (ccb->ataio.cmd.control & ATA_A_RESET)) { + res->lba_high = 0xc3; + res->lba_mid = 0x3c; + ccb->ccb_h.status = CAM_REQ_CMP; + xpt_done(ccb); + return; + } + + if (enc->capsem & AHCI_EM_LED) { + ahci_em_emulate_ses_on_led(dev, ccb); + return; + } else + device_printf(dev, "Unsupported enclosure interface\n"); + + ccb->ccb_h.status = CAM_REQ_INVALID; + xpt_done(ccb); +} + +static void +ahciemaction(struct cam_sim *sim, union ccb *ccb) +{ + device_t dev, parent; + struct ahci_enclosure *enc; + + CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, + ("ahciemaction func_code=%x\n", ccb->ccb_h.func_code)); + + enc = cam_sim_softc(sim); + dev = enc->dev; + switch (ccb->ccb_h.func_code) { + case XPT_ATA_IO: /* Execute the requested I/O operation */ + if (ahci_check_ids(dev, ccb)) + return; + ahci_em_begin_transaction(dev, ccb); + return; + case XPT_RESET_BUS: /* Reset the specified bus */ + case XPT_RESET_DEV: /* Bus Device Reset the specified device */ + ahci_em_reset(dev); + ccb->ccb_h.status = CAM_REQ_CMP; + break; + case XPT_PATH_INQ: /* Path routing inquiry */ + { + struct ccb_pathinq *cpi = &ccb->cpi; + + parent = device_get_parent(dev); + cpi->version_num = 1; /* XXX??? */ + cpi->hba_inquiry = PI_SDTR_ABLE; + cpi->target_sprt = 0; + cpi->hba_misc = PIM_SEQSCAN; + cpi->hba_eng_cnt = 0; + cpi->max_target = 0; + cpi->max_lun = 0; + cpi->initiator_id = 0; + cpi->bus_id = cam_sim_bus(sim); + cpi->base_transfer_speed = 150000; + strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); + strncpy(cpi->hba_vid, "AHCI", HBA_IDLEN); + strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN); + cpi->unit_number = cam_sim_unit(sim); + cpi->transport = XPORT_SATA; + cpi->transport_version = XPORT_VERSION_UNSPECIFIED; + cpi->protocol = PROTO_ATA; + cpi->protocol_version = PROTO_VERSION_UNSPECIFIED; + cpi->maxio = MAXPHYS; + cpi->hba_vendor = pci_get_vendor(parent); + cpi->hba_device = pci_get_device(parent); + cpi->hba_subvendor = pci_get_subvendor(parent); + cpi->hba_subdevice = pci_get_subdevice(parent); + cpi->ccb_h.status = CAM_REQ_CMP; + break; + } + default: + ccb->ccb_h.status = CAM_REQ_INVALID; + break; + } + xpt_done(ccb); +} + +static void +ahciempoll(struct cam_sim *sim) +{ + +} Modified: head/sys/modules/ahci/Makefile ============================================================================== --- head/sys/modules/ahci/Makefile Thu Jul 26 12:18:23 2012 (r238804) +++ head/sys/modules/ahci/Makefile Thu Jul 26 13:44:48 2012 (r238805) @@ -3,6 +3,6 @@ .PATH: ${.CURDIR}/../../dev/ahci KMOD= ahci -SRCS= ahci.c ahci.h device_if.h bus_if.h pci_if.h opt_cam.h +SRCS= ahci.c ahciem.c ahci.h device_if.h bus_if.h pci_if.h opt_cam.h .include