2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012 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>
38 #include <sys/endian.h>
39 #include <sys/malloc.h>
41 #include <sys/mutex.h>
42 #include <machine/stdarg.h>
43 #include <machine/resource.h>
44 #include <machine/bus.h>
46 #include <dev/led/led.h>
47 #include <dev/pci/pcivar.h>
48 #include <dev/pci/pcireg.h>
52 #include <cam/cam_ccb.h>
53 #include <cam/cam_sim.h>
54 #include <cam/cam_xpt_sim.h>
55 #include <cam/cam_debug.h>
56 #include <cam/scsi/scsi_ses.h>
58 /* local prototypes */
59 static void ahciemaction(struct cam_sim *sim, union ccb *ccb);
60 static void ahciempoll(struct cam_sim *sim);
61 static int ahci_em_reset(device_t dev);
62 static void ahci_em_led(void *priv, int onoff);
63 static void ahci_em_setleds(device_t dev, int c);
66 ahci_em_probe(device_t dev)
69 device_set_desc_copy(dev, "AHCI enclosure management bridge");
70 return (BUS_PROBE_DEFAULT);
74 ahci_em_attach(device_t dev)
76 device_t parent = device_get_parent(dev);
77 struct ahci_controller *ctlr = device_get_softc(parent);
78 struct ahci_enclosure *enc = device_get_softc(dev);
79 struct cam_devq *devq;
84 enc->quirks = ctlr->quirks;
85 enc->channels = ctlr->channels;
86 enc->ichannels = ctlr->ichannels;
87 mtx_init(&enc->mtx, "AHCI enclosure lock", NULL, MTX_DEF);
89 if (!(enc->r_memc = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
91 mtx_destroy(&enc->mtx);
94 enc->capsem = ATA_INL(enc->r_memc, 0);
96 if (!(enc->r_memt = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
101 if ((enc->capsem & (AHCI_EM_XMT | AHCI_EM_SMB)) == 0) {
103 if (!(enc->r_memr = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
111 if (ahci_em_reset(dev) != 0) {
116 /* Create the device queue for our SIM. */
117 devq = cam_simq_alloc(1);
119 device_printf(dev, "Unable to allocate SIM queue\n");
123 /* Construct SIM entry */
124 enc->sim = cam_sim_alloc(ahciemaction, ahciempoll, "ahciem", enc,
125 device_get_unit(dev), &enc->mtx,
127 if (enc->sim == NULL) {
129 device_printf(dev, "Unable to allocate SIM\n");
133 if (xpt_bus_register(enc->sim, dev, 0) != CAM_SUCCESS) {
134 device_printf(dev, "unable to register xpt bus\n");
138 if (xpt_create_path(&enc->path, /*periph*/NULL, cam_sim_path(enc->sim),
139 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
140 device_printf(dev, "Unable to create path\n");
144 mtx_unlock(&enc->mtx);
146 device_printf(dev, "Caps:%s%s%s%s%s%s%s%s\n",
147 (enc->capsem & AHCI_EM_PM) ? " PM":"",
148 (enc->capsem & AHCI_EM_ALHD) ? " ALHD":"",
149 (enc->capsem & AHCI_EM_XMT) ? " XMT":"",
150 (enc->capsem & AHCI_EM_SMB) ? " SMB":"",
151 (enc->capsem & AHCI_EM_SGPIO) ? " SGPIO":"",
152 (enc->capsem & AHCI_EM_SES2) ? " SES-2":"",
153 (enc->capsem & AHCI_EM_SAFTE) ? " SAF-TE":"",
154 (enc->capsem & AHCI_EM_LED) ? " LED":"");
156 if ((enc->capsem & AHCI_EM_LED)) {
157 for (c = 0; c < enc->channels; c++) {
158 if ((enc->ichannels & (1 << c)) == 0)
160 for (i = 0; i < AHCI_NUM_LEDS; i++) {
161 enc->leds[c * AHCI_NUM_LEDS + i].dev = dev;
162 enc->leds[c * AHCI_NUM_LEDS + i].num =
163 c * AHCI_NUM_LEDS + i;
165 if ((enc->capsem & AHCI_EM_ALHD) == 0) {
166 snprintf(buf, sizeof(buf), "%s.%d.act",
167 device_get_nameunit(parent), c);
168 enc->leds[c * AHCI_NUM_LEDS + 0].led =
169 led_create(ahci_em_led,
170 &enc->leds[c * AHCI_NUM_LEDS + 0], buf);
172 snprintf(buf, sizeof(buf), "%s.%d.locate",
173 device_get_nameunit(parent), c);
174 enc->leds[c * AHCI_NUM_LEDS + 1].led =
175 led_create(ahci_em_led,
176 &enc->leds[c * AHCI_NUM_LEDS + 1], buf);
177 snprintf(buf, sizeof(buf), "%s.%d.fault",
178 device_get_nameunit(parent), c);
179 enc->leds[c * AHCI_NUM_LEDS + 2].led =
180 led_create(ahci_em_led,
181 &enc->leds[c * AHCI_NUM_LEDS + 2], buf);
187 xpt_bus_deregister(cam_sim_path(enc->sim));
189 cam_sim_free(enc->sim, /*free_devq*/TRUE);
191 mtx_unlock(&enc->mtx);
193 bus_release_resource(dev, SYS_RES_MEMORY, 2, enc->r_memr);
196 bus_release_resource(dev, SYS_RES_MEMORY, 1, enc->r_memt);
197 bus_release_resource(dev, SYS_RES_MEMORY, 0, enc->r_memc);
198 mtx_destroy(&enc->mtx);
203 ahci_em_detach(device_t dev)
205 struct ahci_enclosure *enc = device_get_softc(dev);
208 for (i = 0; i < enc->channels * AHCI_NUM_LEDS; i++) {
209 if (enc->leds[i].led)
210 led_destroy(enc->leds[i].led);
213 xpt_async(AC_LOST_DEVICE, enc->path, NULL);
214 xpt_free_path(enc->path);
215 xpt_bus_deregister(cam_sim_path(enc->sim));
216 cam_sim_free(enc->sim, /*free_devq*/TRUE);
217 mtx_unlock(&enc->mtx);
219 bus_release_resource(dev, SYS_RES_MEMORY, 0, enc->r_memc);
220 bus_release_resource(dev, SYS_RES_MEMORY, 1, enc->r_memt);
222 bus_release_resource(dev, SYS_RES_MEMORY, 2, enc->r_memr);
223 mtx_destroy(&enc->mtx);
228 ahci_em_reset(device_t dev)
230 struct ahci_enclosure *enc;
233 enc = device_get_softc(dev);
234 ATA_OUTL(enc->r_memc, 0, AHCI_EM_RST);
236 while ((ATA_INL(enc->r_memc, 0) & AHCI_EM_RST) &&
240 device_printf(dev, "EM timeout\n");
243 for (i = 0; i < enc->channels; i++)
244 ahci_em_setleds(dev, i);
249 ahci_em_suspend(device_t dev)
251 struct ahci_enclosure *enc = device_get_softc(dev);
254 xpt_freeze_simq(enc->sim, 1);
255 mtx_unlock(&enc->mtx);
260 ahci_em_resume(device_t dev)
262 struct ahci_enclosure *enc = device_get_softc(dev);
266 xpt_release_simq(enc->sim, TRUE);
267 mtx_unlock(&enc->mtx);
271 devclass_t ahciem_devclass;
272 static device_method_t ahciem_methods[] = {
273 DEVMETHOD(device_probe, ahci_em_probe),
274 DEVMETHOD(device_attach, ahci_em_attach),
275 DEVMETHOD(device_detach, ahci_em_detach),
276 DEVMETHOD(device_suspend, ahci_em_suspend),
277 DEVMETHOD(device_resume, ahci_em_resume),
280 static driver_t ahciem_driver = {
283 sizeof(struct ahci_enclosure)
285 DRIVER_MODULE(ahciem, ahci, ahciem_driver, ahciem_devclass, NULL, NULL);
288 ahci_em_setleds(device_t dev, int c)
290 struct ahci_enclosure *enc;
294 enc = device_get_softc(dev);
297 if (enc->status[c][2] & 0x80) /* Activity */
299 if (enc->status[c][2] & SESCTL_RQSID) /* Identification */
301 else if (enc->status[c][3] & SESCTL_RQSFLT) /* Fault */
303 else if (enc->status[c][1] & 0x02) /* Rebuild */
304 val |= (1 << 6) | (1 << 3);
307 while (ATA_INL(enc->r_memc, 0) & (AHCI_EM_TM | AHCI_EM_RST) &&
311 device_printf(dev, "Transmit timeout\n");
312 ATA_OUTL(enc->r_memt, 0, (1 << 8) | (0 << 16) | (0 << 24));
313 ATA_OUTL(enc->r_memt, 4, c | (0 << 8) | (val << 16));
314 ATA_OUTL(enc->r_memc, 0, AHCI_EM_TM);
318 ahci_em_led(void *priv, int onoff)
320 struct ahci_led *led;
321 struct ahci_enclosure *enc;
324 led = (struct ahci_led *)priv;
325 enc = device_get_softc(led->dev);
326 c = led->num / AHCI_NUM_LEDS;
327 l = led->num % AHCI_NUM_LEDS;
331 enc->status[c][2] |= 0x80;
333 enc->status[c][2] &= ~0x80;
336 enc->status[c][2] |= SESCTL_RQSID;
338 enc->status[c][2] &= ~SESCTL_RQSID;
341 enc->status[c][3] |= SESCTL_RQSFLT;
343 enc->status[c][3] &= SESCTL_RQSFLT;
345 ahci_em_setleds(led->dev, c);
349 ahci_check_ids(union ccb *ccb)
352 if (ccb->ccb_h.target_id != 0) {
353 ccb->ccb_h.status = CAM_TID_INVALID;
357 if (ccb->ccb_h.target_lun != 0) {
358 ccb->ccb_h.status = CAM_LUN_INVALID;
366 ahci_em_emulate_ses_on_led(device_t dev, union ccb *ccb)
368 struct ahci_enclosure *enc;
369 struct ses_status_page *page;
370 struct ses_status_array_dev_slot *ads, *ads0;
371 struct ses_elm_desc_hdr *elmd;
375 enc = device_get_softc(dev);
376 buf = ccb->ataio.data_ptr;
378 /* General request validation. */
379 if (ccb->ataio.cmd.command != ATA_SEP_ATTN ||
380 ccb->ataio.dxfer_len < ccb->ataio.cmd.sector_count * 4) {
381 ccb->ccb_h.status = CAM_REQ_INVALID;
386 if (ccb->ataio.cmd.features == 0xEC &&
387 ccb->ataio.cmd.sector_count >= 16) {
388 bzero(buf, ccb->ataio.dxfer_len);
389 buf[0] = 64; /* Valid bytes. */
390 buf[2] = 0x30; /* NAA Locally Assigned. */
391 strncpy(&buf[3], device_get_nameunit(dev), 7);
392 strncpy(&buf[10], "AHCI ", SID_VENDOR_SIZE);
393 strncpy(&buf[18], "SGPIO Enclosure ", SID_PRODUCT_SIZE);
394 strncpy(&buf[34], "1.00", SID_REVISION_SIZE);
395 strncpy(&buf[39], "0001", 4);
396 strncpy(&buf[43], "S-E-S ", 6);
397 strncpy(&buf[49], "2.00", 4);
398 ccb->ccb_h.status = CAM_REQ_CMP;
402 /* SEMB RECEIVE DIAGNOSTIC RESULT (0) */
403 page = (struct ses_status_page *)buf;
404 if (ccb->ataio.cmd.lba_low == 0x02 &&
405 ccb->ataio.cmd.features == 0x00 &&
406 ccb->ataio.cmd.sector_count >= 2) {
407 bzero(buf, ccb->ataio.dxfer_len);
408 page->hdr.page_code = 0;
409 scsi_ulto2b(4, page->hdr.length);
414 ccb->ccb_h.status = CAM_REQ_CMP;
418 /* SEMB RECEIVE DIAGNOSTIC RESULT (1) */
419 if (ccb->ataio.cmd.lba_low == 0x02 &&
420 ccb->ataio.cmd.features == 0x01 &&
421 ccb->ataio.cmd.sector_count >= 13) {
422 struct ses_enc_desc *ed;
423 struct ses_elm_type_desc *td;
425 bzero(buf, ccb->ataio.dxfer_len);
426 page->hdr.page_code = 0x01;
427 scsi_ulto2b(4 + 4 + 36 + 4, page->hdr.length);
428 ed = (struct ses_enc_desc *)&buf[8];
433 strncpy(ed->vendor_id, "AHCI ", SID_VENDOR_SIZE);
434 strncpy(ed->product_id, "SGPIO Enclosure ", SID_PRODUCT_SIZE);
435 strncpy(ed->product_rev, " ", SID_REVISION_SIZE);
436 td = (struct ses_elm_type_desc *)ses_enc_desc_next(ed);
437 td->etype_elm_type = 0x17;
438 td->etype_maxelt = enc->channels;
439 td->etype_subenc = 0;
440 td->etype_txt_len = 0;
441 ccb->ccb_h.status = CAM_REQ_CMP;
445 /* SEMB RECEIVE DIAGNOSTIC RESULT (2) */
446 if (ccb->ataio.cmd.lba_low == 0x02 &&
447 ccb->ataio.cmd.features == 0x02 &&
448 ccb->ataio.cmd.sector_count >= (3 + enc->channels)) {
449 bzero(buf, ccb->ataio.dxfer_len);
450 page->hdr.page_code = 0x02;
451 scsi_ulto2b(4 + 4 * (1 + enc->channels),
453 for (i = 0; i < enc->channels; i++) {
454 ads = &page->elements[i + 1].array_dev_slot;
455 memcpy(ads, enc->status[i], 4);
456 ads->common.bytes[0] |=
457 (enc->ichannels & (1 << i)) ?
458 SES_OBJSTAT_UNKNOWN :
459 SES_OBJSTAT_NOTINSTALLED;
461 ccb->ccb_h.status = CAM_REQ_CMP;
465 /* SEMB SEND DIAGNOSTIC (2) */
466 if (ccb->ataio.cmd.lba_low == 0x82 &&
467 ccb->ataio.cmd.features == 0x02 &&
468 ccb->ataio.cmd.sector_count >= (3 + enc->channels)) {
469 ads0 = &page->elements[0].array_dev_slot;
470 for (i = 0; i < enc->channels; i++) {
471 ads = &page->elements[i + 1].array_dev_slot;
472 if (ads->common.bytes[0] & SESCTL_CSEL) {
473 enc->status[i][0] = 0;
475 ads->bytes[0] & 0x02;
477 ads->bytes[1] & (0x80 | SESCTL_RQSID);
479 ads->bytes[2] & SESCTL_RQSFLT;
480 ahci_em_setleds(dev, i);
481 } else if (ads0->common.bytes[0] & SESCTL_CSEL) {
482 enc->status[i][0] = 0;
484 ads0->bytes[0] & 0x02;
486 ads0->bytes[1] & (0x80 | SESCTL_RQSID);
488 ads0->bytes[2] & SESCTL_RQSFLT;
489 ahci_em_setleds(dev, i);
492 ccb->ccb_h.status = CAM_REQ_CMP;
496 /* SEMB RECEIVE DIAGNOSTIC RESULT (7) */
497 if (ccb->ataio.cmd.lba_low == 0x02 &&
498 ccb->ataio.cmd.features == 0x07 &&
499 ccb->ataio.cmd.sector_count >= (3 + 3 * enc->channels)) {
500 bzero(buf, ccb->ataio.dxfer_len);
501 page->hdr.page_code = 0x07;
502 scsi_ulto2b(4 + 4 + 12 * enc->channels,
504 for (i = 0; i < enc->channels; i++) {
505 elmd = (struct ses_elm_desc_hdr *)&buf[8 + 4 + 12 * i];
506 scsi_ulto2b(8, elmd->length);
507 snprintf((char *)(elmd + 1), 9, "SLOT %03d", i);
509 ccb->ccb_h.status = CAM_REQ_CMP;
513 ccb->ccb_h.status = CAM_REQ_INVALID;
519 ahci_em_begin_transaction(device_t dev, union ccb *ccb)
521 struct ahci_enclosure *enc;
524 enc = device_get_softc(dev);
525 res = &ccb->ataio.res;
526 bzero(res, sizeof(*res));
527 if ((ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) &&
528 (ccb->ataio.cmd.control & ATA_A_RESET)) {
529 res->lba_high = 0xc3;
531 ccb->ccb_h.status = CAM_REQ_CMP;
536 if (enc->capsem & AHCI_EM_LED) {
537 ahci_em_emulate_ses_on_led(dev, ccb);
540 device_printf(dev, "Unsupported enclosure interface\n");
542 ccb->ccb_h.status = CAM_REQ_INVALID;
547 ahciemaction(struct cam_sim *sim, union ccb *ccb)
549 device_t dev, parent;
550 struct ahci_enclosure *enc;
552 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
553 ("ahciemaction func_code=%x\n", ccb->ccb_h.func_code));
555 enc = cam_sim_softc(sim);
557 switch (ccb->ccb_h.func_code) {
558 case XPT_ATA_IO: /* Execute the requested I/O operation */
559 if (ahci_check_ids(ccb))
561 ahci_em_begin_transaction(dev, ccb);
563 case XPT_RESET_BUS: /* Reset the specified bus */
564 case XPT_RESET_DEV: /* Bus Device Reset the specified device */
566 ccb->ccb_h.status = CAM_REQ_CMP;
568 case XPT_PATH_INQ: /* Path routing inquiry */
570 struct ccb_pathinq *cpi = &ccb->cpi;
572 parent = device_get_parent(dev);
573 cpi->version_num = 1; /* XXX??? */
574 cpi->hba_inquiry = PI_SDTR_ABLE;
575 cpi->target_sprt = 0;
576 cpi->hba_misc = PIM_SEQSCAN;
577 cpi->hba_eng_cnt = 0;
580 cpi->initiator_id = 0;
581 cpi->bus_id = cam_sim_bus(sim);
582 cpi->base_transfer_speed = 150000;
583 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
584 strlcpy(cpi->hba_vid, "AHCI", HBA_IDLEN);
585 strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
586 cpi->unit_number = cam_sim_unit(sim);
587 cpi->transport = XPORT_SATA;
588 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
589 cpi->protocol = PROTO_ATA;
590 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
591 cpi->maxio = MAXPHYS;
592 cpi->hba_vendor = pci_get_vendor(parent);
593 cpi->hba_device = pci_get_device(parent);
594 cpi->hba_subvendor = pci_get_subvendor(parent);
595 cpi->hba_subdevice = pci_get_subdevice(parent);
596 cpi->ccb_h.status = CAM_REQ_CMP;
600 ccb->ccb_h.status = CAM_REQ_INVALID;
607 ahciempoll(struct cam_sim *sim)