2 * Copyright (c) 2009 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>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
36 #include <sys/sysctl.h>
37 #include <sys/taskqueue.h>
39 #include <sys/mutex.h>
41 #include <sys/devicestat.h>
42 #include <sys/eventhandler.h>
43 #include <sys/malloc.h>
45 #include <geom/geom_disk.h>
54 #include <cam/cam_ccb.h>
55 #include <cam/cam_periph.h>
56 #include <cam/cam_xpt_periph.h>
57 #include <cam/cam_xpt_internal.h>
58 #include <cam/cam_sim.h>
60 #include <cam/ata/ata_all.h>
77 PMP_FLAG_SCTX_INIT = 0x200
84 /* Offsets into our private area for storing information */
85 #define ccb_state ppriv_field0
86 #define ccb_bp ppriv_ptr1
89 SLIST_ENTRY(pmp_softc) links;
102 #define PMP_EV_RESET 1
103 #define PMP_EV_RESCAN 2
105 struct task sysctl_task;
106 struct sysctl_ctx_list sysctl_ctx;
107 struct sysctl_oid *sysctl_tree;
110 static periph_init_t pmpinit;
111 static void pmpasync(void *callback_arg, u_int32_t code,
112 struct cam_path *path, void *arg);
113 static void pmpsysctlinit(void *context, int pending);
114 static periph_ctor_t pmpregister;
115 static periph_dtor_t pmpcleanup;
116 static periph_start_t pmpstart;
117 static periph_oninv_t pmponinvalidate;
118 static void pmpdone(struct cam_periph *periph,
119 union ccb *done_ccb);
121 #ifndef PMP_DEFAULT_TIMEOUT
122 #define PMP_DEFAULT_TIMEOUT 30 /* Timeout in seconds */
125 #ifndef PMP_DEFAULT_RETRY
126 #define PMP_DEFAULT_RETRY 1
129 #ifndef PMP_DEFAULT_HIDE_SPECIAL
130 #define PMP_DEFAULT_HIDE_SPECIAL 1
133 static int pmp_retry_count = PMP_DEFAULT_RETRY;
134 static int pmp_default_timeout = PMP_DEFAULT_TIMEOUT;
135 static int pmp_hide_special = PMP_DEFAULT_HIDE_SPECIAL;
137 SYSCTL_NODE(_kern_cam, OID_AUTO, pmp, CTLFLAG_RD, 0,
138 "CAM Direct Access Disk driver");
139 SYSCTL_INT(_kern_cam_pmp, OID_AUTO, retry_count, CTLFLAG_RW,
140 &pmp_retry_count, 0, "Normal I/O retry count");
141 TUNABLE_INT("kern.cam.pmp.retry_count", &pmp_retry_count);
142 SYSCTL_INT(_kern_cam_pmp, OID_AUTO, default_timeout, CTLFLAG_RW,
143 &pmp_default_timeout, 0, "Normal I/O timeout (in seconds)");
144 TUNABLE_INT("kern.cam.pmp.default_timeout", &pmp_default_timeout);
145 SYSCTL_INT(_kern_cam_pmp, OID_AUTO, hide_special, CTLFLAG_RW,
146 &pmp_hide_special, 0, "Hide extra ports");
147 TUNABLE_INT("kern.cam.pmp.hide_special", &pmp_hide_special);
149 static struct periph_driver pmpdriver =
152 TAILQ_HEAD_INITIALIZER(pmpdriver.units), /* generation */ 0,
156 PERIPHDRIVER_DECLARE(pmp, pmpdriver);
158 MALLOC_DEFINE(M_ATPMP, "ata_pmp", "ata_pmp buffers");
166 * Install a global async callback. This callback will
167 * receive async callbacks like "new device found".
169 status = xpt_register_async(AC_FOUND_DEVICE, pmpasync, NULL, NULL);
171 if (status != CAM_REQ_CMP) {
172 printf("pmp: Failed to attach master async callback "
173 "due to status 0x%x!\n", status);
178 pmpfreeze(struct cam_periph *periph, int mask)
180 struct pmp_softc *softc = (struct pmp_softc *)periph->softc;
181 struct cam_path *dpath;
184 mask &= ~softc->frozen;
185 for (i = 0; i < 15; i++) {
186 if ((mask & (1 << i)) == 0)
188 if (xpt_create_path(&dpath, periph,
189 xpt_path_path_id(periph->path),
190 i, 0) == CAM_REQ_CMP) {
191 softc->frozen |= (1 << i);
192 xpt_acquire_device(dpath->device);
193 cam_freeze_devq_arg(dpath,
194 RELSIM_RELEASE_RUNLEVEL, CAM_RL_BUS + 1);
195 xpt_free_path(dpath);
201 pmprelease(struct cam_periph *periph, int mask)
203 struct pmp_softc *softc = (struct pmp_softc *)periph->softc;
204 struct cam_path *dpath;
207 mask &= softc->frozen;
208 for (i = 0; i < 15; i++) {
209 if ((mask & (1 << i)) == 0)
211 if (xpt_create_path(&dpath, periph,
212 xpt_path_path_id(periph->path),
213 i, 0) == CAM_REQ_CMP) {
214 softc->frozen &= ~(1 << i);
215 cam_release_devq(dpath,
216 RELSIM_RELEASE_RUNLEVEL, 0, CAM_RL_BUS + 1, FALSE);
217 xpt_release_device(dpath->device);
218 xpt_free_path(dpath);
224 pmponinvalidate(struct cam_periph *periph)
226 struct cam_path *dpath;
230 * De-register any async callbacks.
232 xpt_register_async(0, pmpasync, periph, periph->path);
234 for (i = 0; i < 15; i++) {
235 if (xpt_create_path(&dpath, periph,
236 xpt_path_path_id(periph->path),
237 i, 0) == CAM_REQ_CMP) {
238 xpt_async(AC_LOST_DEVICE, dpath, NULL);
239 xpt_free_path(dpath);
242 pmprelease(periph, -1);
243 xpt_print(periph->path, "lost device\n");
247 pmpcleanup(struct cam_periph *periph)
249 struct pmp_softc *softc;
251 softc = (struct pmp_softc *)periph->softc;
253 xpt_print(periph->path, "removing device entry\n");
254 cam_periph_unlock(periph);
257 * If we can't free the sysctl tree, oh well...
259 if ((softc->flags & PMP_FLAG_SCTX_INIT) != 0
260 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
261 xpt_print(periph->path, "can't remove sysctl context\n");
264 free(softc, M_DEVBUF);
265 cam_periph_lock(periph);
269 pmpasync(void *callback_arg, u_int32_t code,
270 struct cam_path *path, void *arg)
272 struct cam_periph *periph;
273 struct pmp_softc *softc;
275 periph = (struct cam_periph *)callback_arg;
277 case AC_FOUND_DEVICE:
279 struct ccb_getdev *cgd;
282 cgd = (struct ccb_getdev *)arg;
286 if (cgd->protocol != PROTO_SATAPM)
290 * Allocate a peripheral instance for
291 * this device and start the probe
294 status = cam_periph_alloc(pmpregister, pmponinvalidate,
295 pmpcleanup, pmpstart,
296 "pmp", CAM_PERIPH_BIO,
297 cgd->ccb_h.path, pmpasync,
298 AC_FOUND_DEVICE, cgd);
300 if (status != CAM_REQ_CMP
301 && status != CAM_REQ_INPROG)
302 printf("pmpasync: Unable to attach to new device "
303 "due to status 0x%x\n", status);
309 softc = (struct pmp_softc *)periph->softc;
310 cam_periph_async(periph, code, path, arg);
311 if (code == AC_SCSI_AEN)
312 softc->events |= PMP_EV_RESCAN;
314 softc->events |= PMP_EV_RESET;
315 if (code == AC_SCSI_AEN && softc->state != PMP_STATE_NORMAL)
318 pmpfreeze(periph, softc->found);
319 if (code == AC_SENT_BDR || code == AC_BUS_RESET)
320 softc->found = 0; /* We have to reset everything. */
321 if (softc->state == PMP_STATE_NORMAL) {
322 softc->state = PMP_STATE_PRECONFIG;
323 cam_periph_acquire(periph);
324 xpt_schedule(periph, CAM_PRIORITY_DEV);
329 cam_periph_async(periph, code, path, arg);
335 pmpsysctlinit(void *context, int pending)
337 struct cam_periph *periph;
338 struct pmp_softc *softc;
339 char tmpstr[80], tmpstr2[80];
341 periph = (struct cam_periph *)context;
342 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
345 softc = (struct pmp_softc *)periph->softc;
346 snprintf(tmpstr, sizeof(tmpstr), "CAM PMP unit %d", periph->unit_number);
347 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
349 sysctl_ctx_init(&softc->sysctl_ctx);
350 softc->flags |= PMP_FLAG_SCTX_INIT;
351 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
352 SYSCTL_STATIC_CHILDREN(_kern_cam_pmp), OID_AUTO, tmpstr2,
353 CTLFLAG_RD, 0, tmpstr);
354 if (softc->sysctl_tree == NULL) {
355 printf("pmpsysctlinit: unable to allocate sysctl tree\n");
356 cam_periph_release(periph);
360 cam_periph_release(periph);
364 pmpregister(struct cam_periph *periph, void *arg)
366 struct pmp_softc *softc;
367 struct ccb_getdev *cgd;
369 cgd = (struct ccb_getdev *)arg;
370 if (periph == NULL) {
371 printf("pmpregister: periph was NULL!!\n");
372 return(CAM_REQ_CMP_ERR);
376 printf("pmpregister: no getdev CCB, can't register device\n");
377 return(CAM_REQ_CMP_ERR);
380 softc = (struct pmp_softc *)malloc(sizeof(*softc), M_DEVBUF,
384 printf("pmpregister: Unable to probe new device. "
385 "Unable to allocate softc\n");
386 return(CAM_REQ_CMP_ERR);
388 periph->softc = softc;
390 softc->pm_pid = ((uint32_t *)&cgd->ident_data)[0];
391 softc->pm_prv = ((uint32_t *)&cgd->ident_data)[1];
392 TASK_INIT(&softc->sysctl_task, 0, pmpsysctlinit, periph);
394 xpt_announce_periph(periph, NULL);
397 * Add async callbacks for bus reset and
398 * bus device reset calls. I don't bother
399 * checking if this fails as, in most cases,
400 * the system will function just fine without
401 * them and the only alternative would be to
402 * not attach the device on failure.
404 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
405 AC_SCSI_AEN, pmpasync, periph, periph->path);
408 * Take an exclusive refcount on the periph while pmpstart is called
409 * to finish the probe. The reference will be dropped in pmpdone at
412 (void)cam_periph_acquire(periph);
414 softc->state = PMP_STATE_PORTS;
415 softc->events = PMP_EV_RESCAN;
416 xpt_schedule(periph, CAM_PRIORITY_DEV);
422 pmpstart(struct cam_periph *periph, union ccb *start_ccb)
424 struct ccb_trans_settings cts;
425 struct ccb_ataio *ataio;
426 struct pmp_softc *softc;
427 struct cam_path *dpath;
430 softc = (struct pmp_softc *)periph->softc;
431 ataio = &start_ccb->ataio;
433 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("pmpstart\n"));
435 if (softc->restart) {
437 softc->state = min(softc->state, PMP_STATE_PRECONFIG);
439 /* Fetch user wanted device speed. */
440 if (softc->state == PMP_STATE_RESET ||
441 softc->state == PMP_STATE_CONNECT) {
442 if (xpt_create_path(&dpath, periph,
443 xpt_path_path_id(periph->path),
444 softc->pm_step, 0) == CAM_REQ_CMP) {
445 bzero(&cts, sizeof(cts));
446 xpt_setup_ccb(&cts.ccb_h, dpath, CAM_PRIORITY_NONE);
447 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
448 cts.type = CTS_TYPE_USER_SETTINGS;
449 xpt_action((union ccb *)&cts);
450 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_REVISION)
451 revision = cts.xport_specific.sata.revision;
452 xpt_free_path(dpath);
455 switch (softc->state) {
456 case PMP_STATE_PORTS:
457 cam_fill_ataio(ataio,
460 /*flags*/CAM_DIR_NONE,
464 pmp_default_timeout * 1000);
465 ata_pm_read_cmd(ataio, 2, 15);
467 case PMP_STATE_PRECONFIG:
468 /* Get/update host SATA capabilities. */
469 bzero(&cts, sizeof(cts));
470 xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
471 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
472 cts.type = CTS_TYPE_CURRENT_SETTINGS;
473 xpt_action((union ccb *)&cts);
474 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
475 softc->caps = cts.xport_specific.sata.caps;
476 cam_fill_ataio(ataio,
479 /*flags*/CAM_DIR_NONE,
483 pmp_default_timeout * 1000);
484 ata_pm_write_cmd(ataio, 0x60, 15, 0x0);
486 case PMP_STATE_RESET:
487 cam_fill_ataio(ataio,
490 /*flags*/CAM_DIR_NONE,
494 pmp_default_timeout * 1000);
495 ata_pm_write_cmd(ataio, 2, softc->pm_step,
497 ((softc->found & (1 << softc->pm_step)) ? 0 : 1));
499 case PMP_STATE_CONNECT:
500 cam_fill_ataio(ataio,
503 /*flags*/CAM_DIR_NONE,
507 pmp_default_timeout * 1000);
508 ata_pm_write_cmd(ataio, 2, softc->pm_step,
511 case PMP_STATE_CHECK:
512 cam_fill_ataio(ataio,
515 /*flags*/CAM_DIR_NONE,
519 pmp_default_timeout * 1000);
520 ata_pm_read_cmd(ataio, 0, softc->pm_step);
522 case PMP_STATE_CLEAR:
524 cam_fill_ataio(ataio,
527 /*flags*/CAM_DIR_NONE,
531 pmp_default_timeout * 1000);
532 ata_pm_write_cmd(ataio, 1, softc->pm_step, 0xFFFFFFFF);
534 case PMP_STATE_CONFIG:
535 cam_fill_ataio(ataio,
538 /*flags*/CAM_DIR_NONE,
542 pmp_default_timeout * 1000);
543 ata_pm_write_cmd(ataio, 0x60, 15, 0x07 |
544 ((softc->caps & CTS_SATA_CAPS_H_AN) ? 0x08 : 0));
549 xpt_action(start_ccb);
553 pmpdone(struct cam_periph *periph, union ccb *done_ccb)
555 struct ccb_trans_settings cts;
556 struct pmp_softc *softc;
557 struct ccb_ataio *ataio;
558 struct cam_path *dpath;
559 u_int32_t priority, res;
562 softc = (struct pmp_softc *)periph->softc;
563 ataio = &done_ccb->ataio;
565 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("pmpdone\n"));
567 priority = done_ccb->ccb_h.pinfo.priority;
569 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
570 if (cam_periph_error(done_ccb, 0, 0, NULL) == ERESTART) {
572 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
573 cam_release_devq(done_ccb->ccb_h.path,
582 if (softc->restart) {
584 xpt_release_ccb(done_ccb);
585 softc->state = min(softc->state, PMP_STATE_PRECONFIG);
586 xpt_schedule(periph, priority);
590 switch (softc->state) {
591 case PMP_STATE_PORTS:
592 softc->pm_ports = (ataio->res.lba_high << 24) +
593 (ataio->res.lba_mid << 16) +
594 (ataio->res.lba_low << 8) +
595 ataio->res.sector_count;
596 if (pmp_hide_special) {
598 * This PMP declares 6 ports, while only 5 of them
599 * are real. Port 5 is a SEMB port, probing which
600 * causes timeouts if external SEP is not connected
603 if (softc->pm_pid == 0x37261095 && softc->pm_ports == 6)
607 * This PMP declares 7 ports, while only 5 of them
608 * are real. Port 5 is a fake "Config Disk" with
609 * 640 sectors size. Port 6 is a SEMB port.
611 if (softc->pm_pid == 0x47261095 && softc->pm_ports == 7)
615 * These PMPs have extra configuration port.
617 if (softc->pm_pid == 0x57231095 ||
618 softc->pm_pid == 0x57331095 ||
619 softc->pm_pid == 0x57341095 ||
620 softc->pm_pid == 0x57441095)
623 printf("%s%d: %d fan-out ports\n",
624 periph->periph_name, periph->unit_number,
626 softc->state = PMP_STATE_PRECONFIG;
627 xpt_release_ccb(done_ccb);
628 xpt_schedule(periph, priority);
630 case PMP_STATE_PRECONFIG:
632 softc->state = PMP_STATE_RESET;
633 softc->reset |= ~softc->found;
634 xpt_release_ccb(done_ccb);
635 xpt_schedule(periph, priority);
637 case PMP_STATE_RESET:
639 if (softc->pm_step >= softc->pm_ports) {
641 cam_freeze_devq(periph->path);
642 cam_release_devq(periph->path,
643 RELSIM_RELEASE_AFTER_TIMEOUT,
647 softc->state = PMP_STATE_CONNECT;
649 xpt_release_ccb(done_ccb);
650 xpt_schedule(periph, priority);
652 case PMP_STATE_CONNECT:
654 if (softc->pm_step >= softc->pm_ports) {
657 cam_freeze_devq(periph->path);
658 cam_release_devq(periph->path,
659 RELSIM_RELEASE_AFTER_TIMEOUT,
663 softc->state = PMP_STATE_CHECK;
665 xpt_release_ccb(done_ccb);
666 xpt_schedule(periph, priority);
668 case PMP_STATE_CHECK:
669 res = (ataio->res.lba_high << 24) +
670 (ataio->res.lba_mid << 16) +
671 (ataio->res.lba_low << 8) +
672 ataio->res.sector_count;
673 if (((res & 0xf0f) == 0x103 && (res & 0x0f0) != 0) ||
674 (res & 0x600) != 0) {
676 printf("%s%d: port %d status: %08x\n",
677 periph->periph_name, periph->unit_number,
678 softc->pm_step, res);
680 /* Report device speed if it is online. */
681 if ((res & 0xf0f) == 0x103 &&
682 xpt_create_path(&dpath, periph,
683 xpt_path_path_id(periph->path),
684 softc->pm_step, 0) == CAM_REQ_CMP) {
685 bzero(&cts, sizeof(cts));
686 xpt_setup_ccb(&cts.ccb_h, dpath, CAM_PRIORITY_NONE);
687 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
688 cts.type = CTS_TYPE_CURRENT_SETTINGS;
689 cts.xport_specific.sata.revision = (res & 0x0f0) >> 4;
690 cts.xport_specific.sata.valid = CTS_SATA_VALID_REVISION;
691 cts.xport_specific.sata.caps = softc->caps &
692 (CTS_SATA_CAPS_H_PMREQ |
693 CTS_SATA_CAPS_H_DMAAA |
695 cts.xport_specific.sata.valid |= CTS_SATA_VALID_CAPS;
696 xpt_action((union ccb *)&cts);
697 xpt_free_path(dpath);
699 softc->found |= (1 << softc->pm_step);
702 if (softc->pm_try < 10) {
703 cam_freeze_devq(periph->path);
704 cam_release_devq(periph->path,
705 RELSIM_RELEASE_AFTER_TIMEOUT,
712 printf("%s%d: port %d status: %08x\n",
713 periph->periph_name, periph->unit_number,
714 softc->pm_step, res);
716 softc->found &= ~(1 << softc->pm_step);
717 if (xpt_create_path(&dpath, periph,
718 done_ccb->ccb_h.path_id,
719 softc->pm_step, 0) == CAM_REQ_CMP) {
720 xpt_async(AC_LOST_DEVICE, dpath, NULL);
721 xpt_free_path(dpath);
726 if (softc->pm_step >= softc->pm_ports) {
727 if (softc->reset & softc->found) {
728 cam_freeze_devq(periph->path);
729 cam_release_devq(periph->path,
730 RELSIM_RELEASE_AFTER_TIMEOUT,
735 softc->state = PMP_STATE_CLEAR;
738 xpt_release_ccb(done_ccb);
739 xpt_schedule(periph, priority);
741 case PMP_STATE_CLEAR:
743 if (softc->pm_step >= softc->pm_ports) {
744 softc->state = PMP_STATE_CONFIG;
747 xpt_release_ccb(done_ccb);
748 xpt_schedule(periph, priority);
750 case PMP_STATE_CONFIG:
751 for (i = 0; i < softc->pm_ports; i++) {
754 if ((softc->found & (1 << i)) == 0)
756 if (xpt_create_path(&dpath, periph,
757 xpt_path_path_id(periph->path),
758 i, 0) != CAM_REQ_CMP) {
759 printf("pmpdone: xpt_create_path failed\n");
762 /* If we did hard reset to this device, inform XPT. */
763 if ((softc->reset & softc->found & (1 << i)) != 0)
764 xpt_async(AC_SENT_BDR, dpath, NULL);
765 /* If rescan requested, scan this device. */
766 if (softc->events & PMP_EV_RESCAN) {
767 ccb = xpt_alloc_ccb_nowait();
769 xpt_free_path(dpath);
772 xpt_setup_ccb(&ccb->ccb_h, dpath, CAM_PRIORITY_XPT);
775 xpt_free_path(dpath);
782 xpt_release_ccb(done_ccb);
783 softc->state = PMP_STATE_NORMAL;
786 pmprelease(periph, -1);
787 cam_periph_release_locked(periph);