2 * Copyright (c) 2012 Alexander Motin <mav@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * without modification, immediately at the beginning of the file.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/module.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
36 #include <sys/endian.h>
37 #include <sys/malloc.h>
39 #include <sys/mutex.h>
40 #include <machine/stdarg.h>
41 #include <machine/resource.h>
42 #include <machine/bus.h>
44 #include <dev/led/led.h>
45 #include <dev/pci/pcivar.h>
46 #include <dev/pci/pcireg.h>
50 #include <cam/cam_ccb.h>
51 #include <cam/cam_sim.h>
52 #include <cam/cam_xpt_sim.h>
53 #include <cam/cam_debug.h>
54 #include <cam/scsi/scsi_ses.h>
56 /* local prototypes */
57 static void ahciemaction(struct cam_sim *sim, union ccb *ccb);
58 static void ahciempoll(struct cam_sim *sim);
59 static int ahci_em_reset(device_t dev);
60 static void ahci_em_led(void *priv, int onoff);
61 static void ahci_em_setleds(device_t dev, int c);
64 ahci_em_probe(device_t dev)
67 device_set_desc_copy(dev, "AHCI enclosure management bridge");
68 return (BUS_PROBE_DEFAULT);
72 ahci_em_attach(device_t dev)
74 device_t parent = device_get_parent(dev);
75 struct ahci_controller *ctlr = device_get_softc(parent);
76 struct ahci_enclosure *enc = device_get_softc(dev);
77 struct cam_devq *devq;
82 enc->quirks = ctlr->quirks;
83 enc->channels = ctlr->channels;
84 enc->ichannels = ctlr->ichannels;
85 mtx_init(&enc->mtx, "AHCI enclosure lock", NULL, MTX_DEF);
87 if (!(enc->r_memc = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
89 mtx_destroy(&enc->mtx);
92 enc->capsem = ATA_INL(enc->r_memc, 0);
94 if (!(enc->r_memt = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
99 if ((enc->capsem & (AHCI_EM_XMT | AHCI_EM_SMB)) == 0) {
101 if (!(enc->r_memr = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
109 if (ahci_em_reset(dev) != 0) {
114 /* Create the device queue for our SIM. */
115 devq = cam_simq_alloc(1);
117 device_printf(dev, "Unable to allocate SIM queue\n");
121 /* Construct SIM entry */
122 enc->sim = cam_sim_alloc(ahciemaction, ahciempoll, "ahciem", enc,
123 device_get_unit(dev), &enc->mtx,
125 if (enc->sim == NULL) {
127 device_printf(dev, "Unable to allocate SIM\n");
131 if (xpt_bus_register(enc->sim, dev, 0) != CAM_SUCCESS) {
132 device_printf(dev, "unable to register xpt bus\n");
136 if (xpt_create_path(&enc->path, /*periph*/NULL, cam_sim_path(enc->sim),
137 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
138 device_printf(dev, "Unable to create path\n");
142 mtx_unlock(&enc->mtx);
144 device_printf(dev, "Caps:%s%s%s%s%s%s%s%s\n",
145 (enc->capsem & AHCI_EM_PM) ? " PM":"",
146 (enc->capsem & AHCI_EM_ALHD) ? " ALHD":"",
147 (enc->capsem & AHCI_EM_XMT) ? " XMT":"",
148 (enc->capsem & AHCI_EM_SMB) ? " SMB":"",
149 (enc->capsem & AHCI_EM_SGPIO) ? " SGPIO":"",
150 (enc->capsem & AHCI_EM_SES2) ? " SES-2":"",
151 (enc->capsem & AHCI_EM_SAFTE) ? " SAF-TE":"",
152 (enc->capsem & AHCI_EM_LED) ? " LED":"");
154 if ((enc->capsem & AHCI_EM_LED)) {
155 for (c = 0; c < enc->channels; c++) {
156 if ((enc->ichannels & (1 << c)) == 0)
158 for (i = 0; i < AHCI_NUM_LEDS; i++) {
159 enc->leds[c * AHCI_NUM_LEDS + i].dev = dev;
160 enc->leds[c * AHCI_NUM_LEDS + i].num =
161 c * AHCI_NUM_LEDS + i;
163 if ((enc->capsem & AHCI_EM_ALHD) == 0) {
164 snprintf(buf, sizeof(buf), "%s.%d.act",
165 device_get_nameunit(parent), c);
166 enc->leds[c * AHCI_NUM_LEDS + 0].led =
167 led_create(ahci_em_led,
168 &enc->leds[c * AHCI_NUM_LEDS + 0], buf);
170 snprintf(buf, sizeof(buf), "%s.%d.locate",
171 device_get_nameunit(parent), c);
172 enc->leds[c * AHCI_NUM_LEDS + 1].led =
173 led_create(ahci_em_led,
174 &enc->leds[c * AHCI_NUM_LEDS + 1], buf);
175 snprintf(buf, sizeof(buf), "%s.%d.fault",
176 device_get_nameunit(parent), c);
177 enc->leds[c * AHCI_NUM_LEDS + 2].led =
178 led_create(ahci_em_led,
179 &enc->leds[c * AHCI_NUM_LEDS + 2], buf);
185 xpt_bus_deregister(cam_sim_path(enc->sim));
187 cam_sim_free(enc->sim, /*free_devq*/TRUE);
189 mtx_unlock(&enc->mtx);
191 bus_release_resource(dev, SYS_RES_MEMORY, 2, enc->r_memr);
194 bus_release_resource(dev, SYS_RES_MEMORY, 1, enc->r_memt);
195 bus_release_resource(dev, SYS_RES_MEMORY, 0, enc->r_memc);
196 mtx_destroy(&enc->mtx);
201 ahci_em_detach(device_t dev)
203 struct ahci_enclosure *enc = device_get_softc(dev);
206 for (i = 0; i < enc->channels * AHCI_NUM_LEDS; i++) {
207 if (enc->leds[i].led)
208 led_destroy(enc->leds[i].led);
211 xpt_async(AC_LOST_DEVICE, enc->path, NULL);
212 xpt_free_path(enc->path);
213 xpt_bus_deregister(cam_sim_path(enc->sim));
214 cam_sim_free(enc->sim, /*free_devq*/TRUE);
215 mtx_unlock(&enc->mtx);
217 bus_release_resource(dev, SYS_RES_MEMORY, 0, enc->r_memc);
218 bus_release_resource(dev, SYS_RES_MEMORY, 1, enc->r_memt);
220 bus_release_resource(dev, SYS_RES_MEMORY, 2, enc->r_memr);
221 mtx_destroy(&enc->mtx);
226 ahci_em_reset(device_t dev)
228 struct ahci_enclosure *enc;
231 enc = device_get_softc(dev);
232 ATA_OUTL(enc->r_memc, 0, AHCI_EM_RST);
234 while ((ATA_INL(enc->r_memc, 0) & AHCI_EM_RST) &&
238 device_printf(dev, "EM timeout\n");
241 for (i = 0; i < enc->channels; i++)
242 ahci_em_setleds(dev, i);
247 ahci_em_suspend(device_t dev)
249 struct ahci_enclosure *enc = device_get_softc(dev);
252 xpt_freeze_simq(enc->sim, 1);
253 mtx_unlock(&enc->mtx);
258 ahci_em_resume(device_t dev)
260 struct ahci_enclosure *enc = device_get_softc(dev);
264 xpt_release_simq(enc->sim, TRUE);
265 mtx_unlock(&enc->mtx);
269 devclass_t ahciem_devclass;
270 static device_method_t ahciem_methods[] = {
271 DEVMETHOD(device_probe, ahci_em_probe),
272 DEVMETHOD(device_attach, ahci_em_attach),
273 DEVMETHOD(device_detach, ahci_em_detach),
274 DEVMETHOD(device_suspend, ahci_em_suspend),
275 DEVMETHOD(device_resume, ahci_em_resume),
278 static driver_t ahciem_driver = {
281 sizeof(struct ahci_enclosure)
283 DRIVER_MODULE(ahciem, ahci, ahciem_driver, ahciem_devclass, NULL, NULL);
286 ahci_em_setleds(device_t dev, int c)
288 struct ahci_enclosure *enc;
292 enc = device_get_softc(dev);
295 if (enc->status[c][2] & 0x80) /* Activity */
297 if (enc->status[c][2] & SESCTL_RQSID) /* Identification */
299 else if (enc->status[c][3] & SESCTL_RQSFLT) /* Fault */
301 else if (enc->status[c][1] & 0x02) /* Rebuild */
302 val |= (1 << 6) | (1 << 3);
305 while (ATA_INL(enc->r_memc, 0) & (AHCI_EM_TM | AHCI_EM_RST) &&
309 device_printf(dev, "Transmit timeout\n");
310 ATA_OUTL(enc->r_memt, 0, (1 << 8) | (0 << 16) | (0 << 24));
311 ATA_OUTL(enc->r_memt, 4, c | (0 << 8) | (val << 16));
312 ATA_OUTL(enc->r_memc, 0, AHCI_EM_TM);
316 ahci_em_led(void *priv, int onoff)
318 struct ahci_led *led;
319 struct ahci_enclosure *enc;
322 led = (struct ahci_led *)priv;
323 enc = device_get_softc(led->dev);
324 c = led->num / AHCI_NUM_LEDS;
325 l = led->num % AHCI_NUM_LEDS;
329 enc->status[c][2] |= 0x80;
331 enc->status[c][2] &= ~0x80;
334 enc->status[c][2] |= SESCTL_RQSID;
336 enc->status[c][2] &= ~SESCTL_RQSID;
339 enc->status[c][3] |= SESCTL_RQSFLT;
341 enc->status[c][3] &= SESCTL_RQSFLT;
343 ahci_em_setleds(led->dev, c);
347 ahci_check_ids(union ccb *ccb)
350 if (ccb->ccb_h.target_id != 0) {
351 ccb->ccb_h.status = CAM_TID_INVALID;
355 if (ccb->ccb_h.target_lun != 0) {
356 ccb->ccb_h.status = CAM_LUN_INVALID;
364 ahci_em_emulate_ses_on_led(device_t dev, union ccb *ccb)
366 struct ahci_enclosure *enc;
367 struct ses_status_page *page;
368 struct ses_status_array_dev_slot *ads, *ads0;
369 struct ses_elm_desc_hdr *elmd;
373 enc = device_get_softc(dev);
374 buf = ccb->ataio.data_ptr;
376 /* General request validation. */
377 if (ccb->ataio.cmd.command != ATA_SEP_ATTN ||
378 ccb->ataio.dxfer_len < ccb->ataio.cmd.sector_count * 4) {
379 ccb->ccb_h.status = CAM_REQ_INVALID;
384 if (ccb->ataio.cmd.features == 0xEC &&
385 ccb->ataio.cmd.sector_count >= 16) {
386 bzero(buf, ccb->ataio.dxfer_len);
387 buf[0] = 64; /* Valid bytes. */
388 buf[2] = 0x30; /* NAA Locally Assigned. */
389 strncpy(&buf[3], device_get_nameunit(dev), 7);
390 strncpy(&buf[10], "AHCI ", SID_VENDOR_SIZE);
391 strncpy(&buf[18], "SGPIO Enclosure ", SID_PRODUCT_SIZE);
392 strncpy(&buf[34], "1.00", SID_REVISION_SIZE);
393 strncpy(&buf[39], "0001", 4);
394 strncpy(&buf[43], "S-E-S ", 6);
395 strncpy(&buf[49], "2.00", 4);
396 ccb->ccb_h.status = CAM_REQ_CMP;
400 /* SEMB RECEIVE DIAGNOSTIC RESULT (0) */
401 page = (struct ses_status_page *)buf;
402 if (ccb->ataio.cmd.lba_low == 0x02 &&
403 ccb->ataio.cmd.features == 0x00 &&
404 ccb->ataio.cmd.sector_count >= 2) {
405 bzero(buf, ccb->ataio.dxfer_len);
406 page->hdr.page_code = 0;
407 scsi_ulto2b(4, page->hdr.length);
412 ccb->ccb_h.status = CAM_REQ_CMP;
416 /* SEMB RECEIVE DIAGNOSTIC RESULT (1) */
417 if (ccb->ataio.cmd.lba_low == 0x02 &&
418 ccb->ataio.cmd.features == 0x01 &&
419 ccb->ataio.cmd.sector_count >= 13) {
420 struct ses_enc_desc *ed;
421 struct ses_elm_type_desc *td;
423 bzero(buf, ccb->ataio.dxfer_len);
424 page->hdr.page_code = 0x01;
425 scsi_ulto2b(4 + 4 + 36 + 4, page->hdr.length);
426 ed = (struct ses_enc_desc *)&buf[8];
431 strncpy(ed->vendor_id, "AHCI ", SID_VENDOR_SIZE);
432 strncpy(ed->product_id, "SGPIO Enclosure ", SID_PRODUCT_SIZE);
433 strncpy(ed->product_rev, " ", SID_REVISION_SIZE);
434 td = (struct ses_elm_type_desc *)ses_enc_desc_next(ed);
435 td->etype_elm_type = 0x17;
436 td->etype_maxelt = enc->channels;
437 td->etype_subenc = 0;
438 td->etype_txt_len = 0;
439 ccb->ccb_h.status = CAM_REQ_CMP;
443 /* SEMB RECEIVE DIAGNOSTIC RESULT (2) */
444 if (ccb->ataio.cmd.lba_low == 0x02 &&
445 ccb->ataio.cmd.features == 0x02 &&
446 ccb->ataio.cmd.sector_count >= (3 + enc->channels)) {
447 bzero(buf, ccb->ataio.dxfer_len);
448 page->hdr.page_code = 0x02;
449 scsi_ulto2b(4 + 4 * (1 + enc->channels),
451 for (i = 0; i < enc->channels; i++) {
452 ads = &page->elements[i + 1].array_dev_slot;
453 memcpy(ads, enc->status[i], 4);
454 ads->common.bytes[0] |=
455 (enc->ichannels & (1 << i)) ?
456 SES_OBJSTAT_UNKNOWN :
457 SES_OBJSTAT_NOTINSTALLED;
459 ccb->ccb_h.status = CAM_REQ_CMP;
463 /* SEMB SEND DIAGNOSTIC (2) */
464 if (ccb->ataio.cmd.lba_low == 0x82 &&
465 ccb->ataio.cmd.features == 0x02 &&
466 ccb->ataio.cmd.sector_count >= (3 + enc->channels)) {
467 ads0 = &page->elements[0].array_dev_slot;
468 for (i = 0; i < enc->channels; i++) {
469 ads = &page->elements[i + 1].array_dev_slot;
470 if (ads->common.bytes[0] & SESCTL_CSEL) {
471 enc->status[i][0] = 0;
473 ads->bytes[0] & 0x02;
475 ads->bytes[1] & (0x80 | SESCTL_RQSID);
477 ads->bytes[2] & SESCTL_RQSFLT;
478 ahci_em_setleds(dev, i);
479 } else if (ads0->common.bytes[0] & SESCTL_CSEL) {
480 enc->status[i][0] = 0;
482 ads0->bytes[0] & 0x02;
484 ads0->bytes[1] & (0x80 | SESCTL_RQSID);
486 ads0->bytes[2] & SESCTL_RQSFLT;
487 ahci_em_setleds(dev, i);
490 ccb->ccb_h.status = CAM_REQ_CMP;
494 /* SEMB RECEIVE DIAGNOSTIC RESULT (7) */
495 if (ccb->ataio.cmd.lba_low == 0x02 &&
496 ccb->ataio.cmd.features == 0x07 &&
497 ccb->ataio.cmd.sector_count >= (3 + 3 * enc->channels)) {
498 bzero(buf, ccb->ataio.dxfer_len);
499 page->hdr.page_code = 0x07;
500 scsi_ulto2b(4 + 4 + 12 * enc->channels,
502 for (i = 0; i < enc->channels; i++) {
503 elmd = (struct ses_elm_desc_hdr *)&buf[8 + 4 + 12 * i];
504 scsi_ulto2b(8, elmd->length);
505 snprintf((char *)(elmd + 1), 9, "SLOT %03d", i);
507 ccb->ccb_h.status = CAM_REQ_CMP;
511 ccb->ccb_h.status = CAM_REQ_INVALID;
517 ahci_em_begin_transaction(device_t dev, union ccb *ccb)
519 struct ahci_enclosure *enc;
522 enc = device_get_softc(dev);
523 res = &ccb->ataio.res;
524 bzero(res, sizeof(*res));
525 if ((ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) &&
526 (ccb->ataio.cmd.control & ATA_A_RESET)) {
527 res->lba_high = 0xc3;
529 ccb->ccb_h.status = CAM_REQ_CMP;
534 if (enc->capsem & AHCI_EM_LED) {
535 ahci_em_emulate_ses_on_led(dev, ccb);
538 device_printf(dev, "Unsupported enclosure interface\n");
540 ccb->ccb_h.status = CAM_REQ_INVALID;
545 ahciemaction(struct cam_sim *sim, union ccb *ccb)
547 device_t dev, parent;
548 struct ahci_enclosure *enc;
550 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
551 ("ahciemaction func_code=%x\n", ccb->ccb_h.func_code));
553 enc = cam_sim_softc(sim);
555 switch (ccb->ccb_h.func_code) {
556 case XPT_ATA_IO: /* Execute the requested I/O operation */
557 if (ahci_check_ids(ccb))
559 ahci_em_begin_transaction(dev, ccb);
561 case XPT_RESET_BUS: /* Reset the specified bus */
562 case XPT_RESET_DEV: /* Bus Device Reset the specified device */
564 ccb->ccb_h.status = CAM_REQ_CMP;
566 case XPT_PATH_INQ: /* Path routing inquiry */
568 struct ccb_pathinq *cpi = &ccb->cpi;
570 parent = device_get_parent(dev);
571 cpi->version_num = 1; /* XXX??? */
572 cpi->hba_inquiry = PI_SDTR_ABLE;
573 cpi->target_sprt = 0;
574 cpi->hba_misc = PIM_SEQSCAN;
575 cpi->hba_eng_cnt = 0;
578 cpi->initiator_id = 0;
579 cpi->bus_id = cam_sim_bus(sim);
580 cpi->base_transfer_speed = 150000;
581 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
582 strncpy(cpi->hba_vid, "AHCI", HBA_IDLEN);
583 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
584 cpi->unit_number = cam_sim_unit(sim);
585 cpi->transport = XPORT_SATA;
586 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
587 cpi->protocol = PROTO_ATA;
588 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
589 cpi->maxio = MAXPHYS;
590 cpi->hba_vendor = pci_get_vendor(parent);
591 cpi->hba_device = pci_get_device(parent);
592 cpi->hba_subvendor = pci_get_subvendor(parent);
593 cpi->hba_subdevice = pci_get_subdevice(parent);
594 cpi->ccb_h.status = CAM_REQ_CMP;
598 ccb->ccb_h.status = CAM_REQ_INVALID;
605 ahciempoll(struct cam_sim *sim)