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 static int pmp_retry_count = PMP_DEFAULT_RETRY;
130 static int pmp_default_timeout = PMP_DEFAULT_TIMEOUT;
132 SYSCTL_NODE(_kern_cam, OID_AUTO, pmp, CTLFLAG_RD, 0,
133 "CAM Direct Access Disk driver");
134 SYSCTL_INT(_kern_cam_pmp, OID_AUTO, retry_count, CTLFLAG_RW,
135 &pmp_retry_count, 0, "Normal I/O retry count");
136 TUNABLE_INT("kern.cam.pmp.retry_count", &pmp_retry_count);
137 SYSCTL_INT(_kern_cam_pmp, OID_AUTO, default_timeout, CTLFLAG_RW,
138 &pmp_default_timeout, 0, "Normal I/O timeout (in seconds)");
139 TUNABLE_INT("kern.cam.pmp.default_timeout", &pmp_default_timeout);
141 static struct periph_driver pmpdriver =
144 TAILQ_HEAD_INITIALIZER(pmpdriver.units), /* generation */ 0,
148 PERIPHDRIVER_DECLARE(pmp, pmpdriver);
150 MALLOC_DEFINE(M_ATPMP, "ata_pmp", "ata_pmp buffers");
158 * Install a global async callback. This callback will
159 * receive async callbacks like "new device found".
161 status = xpt_register_async(AC_FOUND_DEVICE, pmpasync, NULL, NULL);
163 if (status != CAM_REQ_CMP) {
164 printf("pmp: Failed to attach master async callback "
165 "due to status 0x%x!\n", status);
170 pmpfreeze(struct cam_periph *periph, int mask)
172 struct pmp_softc *softc = (struct pmp_softc *)periph->softc;
173 struct cam_path *dpath;
176 mask &= ~softc->frozen;
177 for (i = 0; i < 15; i++) {
178 if ((mask & (1 << i)) == 0)
180 if (xpt_create_path(&dpath, periph,
181 xpt_path_path_id(periph->path),
182 i, 0) == CAM_REQ_CMP) {
183 softc->frozen |= (1 << i);
184 xpt_acquire_device(dpath->device);
185 cam_freeze_devq_arg(dpath,
186 RELSIM_RELEASE_RUNLEVEL, CAM_RL_BUS + 1);
187 xpt_free_path(dpath);
193 pmprelease(struct cam_periph *periph, int mask)
195 struct pmp_softc *softc = (struct pmp_softc *)periph->softc;
196 struct cam_path *dpath;
199 mask &= softc->frozen;
200 for (i = 0; i < 15; i++) {
201 if ((mask & (1 << i)) == 0)
203 if (xpt_create_path(&dpath, periph,
204 xpt_path_path_id(periph->path),
205 i, 0) == CAM_REQ_CMP) {
206 softc->frozen &= ~(1 << i);
207 cam_release_devq(dpath,
208 RELSIM_RELEASE_RUNLEVEL, 0, CAM_RL_BUS + 1, FALSE);
209 xpt_release_device(dpath->device);
210 xpt_free_path(dpath);
216 pmponinvalidate(struct cam_periph *periph)
218 struct pmp_softc *softc;
219 struct cam_path *dpath;
222 softc = (struct pmp_softc *)periph->softc;
225 * De-register any async callbacks.
227 xpt_register_async(0, pmpasync, periph, periph->path);
229 for (i = 0; i < 15; i++) {
230 if (xpt_create_path(&dpath, periph,
231 xpt_path_path_id(periph->path),
232 i, 0) == CAM_REQ_CMP) {
233 xpt_async(AC_LOST_DEVICE, dpath, NULL);
234 xpt_free_path(dpath);
237 pmprelease(periph, -1);
238 xpt_print(periph->path, "lost device\n");
242 pmpcleanup(struct cam_periph *periph)
244 struct pmp_softc *softc;
246 softc = (struct pmp_softc *)periph->softc;
248 xpt_print(periph->path, "removing device entry\n");
249 cam_periph_unlock(periph);
252 * If we can't free the sysctl tree, oh well...
254 if ((softc->flags & PMP_FLAG_SCTX_INIT) != 0
255 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
256 xpt_print(periph->path, "can't remove sysctl context\n");
259 free(softc, M_DEVBUF);
260 cam_periph_lock(periph);
264 pmpasync(void *callback_arg, u_int32_t code,
265 struct cam_path *path, void *arg)
267 struct cam_periph *periph;
268 struct pmp_softc *softc;
270 periph = (struct cam_periph *)callback_arg;
272 case AC_FOUND_DEVICE:
274 struct ccb_getdev *cgd;
277 cgd = (struct ccb_getdev *)arg;
281 if (cgd->protocol != PROTO_SATAPM)
285 * Allocate a peripheral instance for
286 * this device and start the probe
289 status = cam_periph_alloc(pmpregister, pmponinvalidate,
290 pmpcleanup, pmpstart,
291 "pmp", CAM_PERIPH_BIO,
292 cgd->ccb_h.path, pmpasync,
293 AC_FOUND_DEVICE, cgd);
295 if (status != CAM_REQ_CMP
296 && status != CAM_REQ_INPROG)
297 printf("pmpasync: Unable to attach to new device "
298 "due to status 0x%x\n", status);
304 softc = (struct pmp_softc *)periph->softc;
305 cam_periph_async(periph, code, path, arg);
306 if (code == AC_SCSI_AEN)
307 softc->events |= PMP_EV_RESCAN;
309 softc->events |= PMP_EV_RESET;
310 if (code == AC_SCSI_AEN && softc->state != PMP_STATE_NORMAL)
313 pmpfreeze(periph, softc->found);
314 if (code == AC_SENT_BDR || code == AC_BUS_RESET)
315 softc->found = 0; /* We have to reset everything. */
316 if (softc->state == PMP_STATE_NORMAL) {
317 softc->state = PMP_STATE_PRECONFIG;
318 cam_periph_acquire(periph);
319 xpt_schedule(periph, CAM_PRIORITY_DEV);
324 cam_periph_async(periph, code, path, arg);
330 pmpsysctlinit(void *context, int pending)
332 struct cam_periph *periph;
333 struct pmp_softc *softc;
334 char tmpstr[80], tmpstr2[80];
336 periph = (struct cam_periph *)context;
337 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
340 softc = (struct pmp_softc *)periph->softc;
341 snprintf(tmpstr, sizeof(tmpstr), "CAM PMP unit %d", periph->unit_number);
342 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
344 sysctl_ctx_init(&softc->sysctl_ctx);
345 softc->flags |= PMP_FLAG_SCTX_INIT;
346 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
347 SYSCTL_STATIC_CHILDREN(_kern_cam_pmp), OID_AUTO, tmpstr2,
348 CTLFLAG_RD, 0, tmpstr);
349 if (softc->sysctl_tree == NULL) {
350 printf("pmpsysctlinit: unable to allocate sysctl tree\n");
351 cam_periph_release(periph);
355 cam_periph_release(periph);
359 pmpregister(struct cam_periph *periph, void *arg)
361 struct pmp_softc *softc;
362 struct ccb_getdev *cgd;
364 cgd = (struct ccb_getdev *)arg;
365 if (periph == NULL) {
366 printf("pmpregister: periph was NULL!!\n");
367 return(CAM_REQ_CMP_ERR);
371 printf("pmpregister: no getdev CCB, can't register device\n");
372 return(CAM_REQ_CMP_ERR);
375 softc = (struct pmp_softc *)malloc(sizeof(*softc), M_DEVBUF,
379 printf("pmpregister: Unable to probe new device. "
380 "Unable to allocate softc\n");
381 return(CAM_REQ_CMP_ERR);
383 periph->softc = softc;
385 softc->pm_pid = ((uint32_t *)&cgd->ident_data)[0];
386 softc->pm_prv = ((uint32_t *)&cgd->ident_data)[1];
387 TASK_INIT(&softc->sysctl_task, 0, pmpsysctlinit, periph);
389 xpt_announce_periph(periph, NULL);
392 * Add async callbacks for bus reset and
393 * bus device reset calls. I don't bother
394 * checking if this fails as, in most cases,
395 * the system will function just fine without
396 * them and the only alternative would be to
397 * not attach the device on failure.
399 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
400 AC_SCSI_AEN, pmpasync, periph, periph->path);
403 * Take an exclusive refcount on the periph while pmpstart is called
404 * to finish the probe. The reference will be dropped in pmpdone at
407 (void)cam_periph_acquire(periph);
409 softc->state = PMP_STATE_PORTS;
410 softc->events = PMP_EV_RESCAN;
411 xpt_schedule(periph, CAM_PRIORITY_DEV);
417 pmpstart(struct cam_periph *periph, union ccb *start_ccb)
419 struct ccb_trans_settings cts;
420 struct ccb_ataio *ataio;
421 struct pmp_softc *softc;
422 struct cam_path *dpath;
425 softc = (struct pmp_softc *)periph->softc;
426 ataio = &start_ccb->ataio;
428 if (softc->restart) {
430 softc->state = min(softc->state, PMP_STATE_PRECONFIG);
432 /* Fetch user wanted device speed. */
433 if (softc->state == PMP_STATE_RESET ||
434 softc->state == PMP_STATE_CONNECT) {
435 if (xpt_create_path(&dpath, periph,
436 xpt_path_path_id(periph->path),
437 softc->pm_step, 0) == CAM_REQ_CMP) {
438 bzero(&cts, sizeof(cts));
439 xpt_setup_ccb(&cts.ccb_h, dpath, CAM_PRIORITY_NONE);
440 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
441 cts.type = CTS_TYPE_USER_SETTINGS;
442 xpt_action((union ccb *)&cts);
443 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_REVISION)
444 revision = cts.xport_specific.sata.revision;
445 xpt_free_path(dpath);
448 switch (softc->state) {
449 case PMP_STATE_PORTS:
450 cam_fill_ataio(ataio,
453 /*flags*/CAM_DIR_NONE,
457 pmp_default_timeout * 1000);
458 ata_pm_read_cmd(ataio, 2, 15);
460 case PMP_STATE_PRECONFIG:
461 /* Get/update host SATA capabilities. */
462 bzero(&cts, sizeof(cts));
463 xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
464 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
465 cts.type = CTS_TYPE_CURRENT_SETTINGS;
466 xpt_action((union ccb *)&cts);
467 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
468 softc->caps = cts.xport_specific.sata.caps;
469 cam_fill_ataio(ataio,
472 /*flags*/CAM_DIR_NONE,
476 pmp_default_timeout * 1000);
477 ata_pm_write_cmd(ataio, 0x60, 15, 0x0);
479 case PMP_STATE_RESET:
480 cam_fill_ataio(ataio,
483 /*flags*/CAM_DIR_NONE,
487 pmp_default_timeout * 1000);
488 ata_pm_write_cmd(ataio, 2, softc->pm_step,
490 ((softc->found & (1 << softc->pm_step)) ? 0 : 1));
492 case PMP_STATE_CONNECT:
493 cam_fill_ataio(ataio,
496 /*flags*/CAM_DIR_NONE,
500 pmp_default_timeout * 1000);
501 ata_pm_write_cmd(ataio, 2, softc->pm_step,
504 case PMP_STATE_CHECK:
505 cam_fill_ataio(ataio,
508 /*flags*/CAM_DIR_NONE,
512 pmp_default_timeout * 1000);
513 ata_pm_read_cmd(ataio, 0, softc->pm_step);
515 case PMP_STATE_CLEAR:
517 cam_fill_ataio(ataio,
520 /*flags*/CAM_DIR_NONE,
524 pmp_default_timeout * 1000);
525 ata_pm_write_cmd(ataio, 1, softc->pm_step, 0xFFFFFFFF);
527 case PMP_STATE_CONFIG:
528 cam_fill_ataio(ataio,
531 /*flags*/CAM_DIR_NONE,
535 pmp_default_timeout * 1000);
536 ata_pm_write_cmd(ataio, 0x60, 15, 0xf);
541 xpt_action(start_ccb);
545 pmpdone(struct cam_periph *periph, union ccb *done_ccb)
547 struct ccb_trans_settings cts;
548 struct pmp_softc *softc;
549 struct ccb_ataio *ataio;
550 struct cam_path *path, *dpath;
551 u_int32_t priority, res;
554 softc = (struct pmp_softc *)periph->softc;
555 ataio = &done_ccb->ataio;
557 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("pmpdone\n"));
559 path = done_ccb->ccb_h.path;
560 priority = done_ccb->ccb_h.pinfo.priority;
562 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
563 if (cam_periph_error(done_ccb, 0, 0, NULL) == ERESTART) {
565 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
566 cam_release_devq(done_ccb->ccb_h.path,
575 if (softc->restart) {
577 xpt_release_ccb(done_ccb);
578 softc->state = min(softc->state, PMP_STATE_PRECONFIG);
579 xpt_schedule(periph, priority);
583 switch (softc->state) {
584 case PMP_STATE_PORTS:
585 softc->pm_ports = (done_ccb->ataio.res.lba_high << 24) +
586 (done_ccb->ataio.res.lba_mid << 16) +
587 (done_ccb->ataio.res.lba_low << 8) +
588 done_ccb->ataio.res.sector_count;
589 /* This PMP declares 6 ports, while only 5 of them are real.
590 * Port 5 is enclosure management bridge port, which has implementation
591 * problems, causing probe faults. Hide it for now. */
592 if (softc->pm_pid == 0x37261095 && softc->pm_ports == 6)
594 /* This PMP declares 7 ports, while only 5 of them are real.
595 * Port 5 is some fake "Config Disk" with 640 sectors size,
596 * port 6 is enclosure management bridge port.
597 * Both fake ports has implementation problems, causing
598 * probe faults. Hide them for now. */
599 if (softc->pm_pid == 0x47261095 && softc->pm_ports == 7)
601 /* These PMPs declare one more port then actually have,
602 * for configuration purposes. Hide it for now. */
603 if (softc->pm_pid == 0x57231095 || softc->pm_pid == 0x57331095 ||
604 softc->pm_pid == 0x57341095 || softc->pm_pid == 0x57441095)
606 printf("%s%d: %d fan-out ports\n",
607 periph->periph_name, periph->unit_number,
609 softc->state = PMP_STATE_PRECONFIG;
610 xpt_release_ccb(done_ccb);
611 xpt_schedule(periph, priority);
613 case PMP_STATE_PRECONFIG:
615 softc->state = PMP_STATE_RESET;
616 softc->reset |= ~softc->found;
617 xpt_release_ccb(done_ccb);
618 xpt_schedule(periph, priority);
620 case PMP_STATE_RESET:
622 if (softc->pm_step >= softc->pm_ports) {
624 cam_freeze_devq(periph->path);
625 cam_release_devq(periph->path,
626 RELSIM_RELEASE_AFTER_TIMEOUT,
630 softc->state = PMP_STATE_CONNECT;
632 xpt_release_ccb(done_ccb);
633 xpt_schedule(periph, priority);
635 case PMP_STATE_CONNECT:
637 if (softc->pm_step >= softc->pm_ports) {
640 cam_freeze_devq(periph->path);
641 cam_release_devq(periph->path,
642 RELSIM_RELEASE_AFTER_TIMEOUT,
646 softc->state = PMP_STATE_CHECK;
648 xpt_release_ccb(done_ccb);
649 xpt_schedule(periph, priority);
651 case PMP_STATE_CHECK:
652 res = (done_ccb->ataio.res.lba_high << 24) +
653 (done_ccb->ataio.res.lba_mid << 16) +
654 (done_ccb->ataio.res.lba_low << 8) +
655 done_ccb->ataio.res.sector_count;
656 if (((res & 0xf0f) == 0x103 && (res & 0x0f0) != 0) ||
657 (res & 0x600) != 0) {
659 printf("%s%d: port %d status: %08x\n",
660 periph->periph_name, periph->unit_number,
661 softc->pm_step, res);
663 /* Report device speed if it is online. */
664 if ((res & 0xf0f) == 0x103 &&
665 xpt_create_path(&dpath, periph,
666 xpt_path_path_id(periph->path),
667 softc->pm_step, 0) == CAM_REQ_CMP) {
668 bzero(&cts, sizeof(cts));
669 xpt_setup_ccb(&cts.ccb_h, dpath, CAM_PRIORITY_NONE);
670 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
671 cts.type = CTS_TYPE_CURRENT_SETTINGS;
672 cts.xport_specific.sata.revision = (res & 0x0f0) >> 4;
673 cts.xport_specific.sata.valid = CTS_SATA_VALID_REVISION;
674 cts.xport_specific.sata.caps = softc->caps &
675 (CTS_SATA_CAPS_H_PMREQ | CTS_SATA_CAPS_H_DMAAA);
676 cts.xport_specific.sata.valid |= CTS_SATA_VALID_CAPS;
677 xpt_action((union ccb *)&cts);
678 xpt_free_path(dpath);
680 softc->found |= (1 << softc->pm_step);
683 if (softc->pm_try < 10) {
684 cam_freeze_devq(periph->path);
685 cam_release_devq(periph->path,
686 RELSIM_RELEASE_AFTER_TIMEOUT,
693 printf("%s%d: port %d status: %08x\n",
694 periph->periph_name, periph->unit_number,
695 softc->pm_step, res);
697 softc->found &= ~(1 << softc->pm_step);
698 if (xpt_create_path(&dpath, periph,
699 done_ccb->ccb_h.path_id,
700 softc->pm_step, 0) == CAM_REQ_CMP) {
701 xpt_async(AC_LOST_DEVICE, dpath, NULL);
702 xpt_free_path(dpath);
707 if (softc->pm_step >= softc->pm_ports) {
708 if (softc->reset & softc->found) {
709 cam_freeze_devq(periph->path);
710 cam_release_devq(periph->path,
711 RELSIM_RELEASE_AFTER_TIMEOUT,
716 softc->state = PMP_STATE_CLEAR;
719 xpt_release_ccb(done_ccb);
720 xpt_schedule(periph, priority);
722 case PMP_STATE_CLEAR:
724 if (softc->pm_step >= softc->pm_ports) {
725 softc->state = PMP_STATE_CONFIG;
728 xpt_release_ccb(done_ccb);
729 xpt_schedule(periph, priority);
731 case PMP_STATE_CONFIG:
732 for (i = 0; i < softc->pm_ports; i++) {
735 if ((softc->found & (1 << i)) == 0)
737 if (xpt_create_path(&dpath, periph,
738 xpt_path_path_id(periph->path),
739 i, 0) != CAM_REQ_CMP) {
740 printf("pmpdone: xpt_create_path failed"
741 ", bus scan halted\n");
742 xpt_free_ccb(done_ccb);
745 /* If we did hard reset to this device, inform XPT. */
746 if ((softc->reset & softc->found & (1 << i)) != 0)
747 xpt_async(AC_SENT_BDR, dpath, NULL);
748 /* If rescan requested, scan this device. */
749 if (softc->events & PMP_EV_RESCAN) {
750 ccb = xpt_alloc_ccb_nowait();
752 xpt_free_path(dpath);
755 xpt_setup_ccb(&ccb->ccb_h, dpath, CAM_PRIORITY_XPT);
758 xpt_free_path(dpath);
765 xpt_release_ccb(done_ccb);
766 softc->state = PMP_STATE_NORMAL;
769 pmprelease(periph, -1);
770 cam_periph_release_locked(periph);