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, 0x07 |
537 ((softc->caps & CTS_SATA_CAPS_H_AN) ? 0x08 : 0));
542 xpt_action(start_ccb);
546 pmpdone(struct cam_periph *periph, union ccb *done_ccb)
548 struct ccb_trans_settings cts;
549 struct pmp_softc *softc;
550 struct ccb_ataio *ataio;
551 struct cam_path *path, *dpath;
552 u_int32_t priority, res;
555 softc = (struct pmp_softc *)periph->softc;
556 ataio = &done_ccb->ataio;
558 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("pmpdone\n"));
560 path = done_ccb->ccb_h.path;
561 priority = done_ccb->ccb_h.pinfo.priority;
563 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
564 if (cam_periph_error(done_ccb, 0, 0, NULL) == ERESTART) {
566 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
567 cam_release_devq(done_ccb->ccb_h.path,
576 if (softc->restart) {
578 xpt_release_ccb(done_ccb);
579 softc->state = min(softc->state, PMP_STATE_PRECONFIG);
580 xpt_schedule(periph, priority);
584 switch (softc->state) {
585 case PMP_STATE_PORTS:
586 softc->pm_ports = (done_ccb->ataio.res.lba_high << 24) +
587 (done_ccb->ataio.res.lba_mid << 16) +
588 (done_ccb->ataio.res.lba_low << 8) +
589 done_ccb->ataio.res.sector_count;
590 /* This PMP declares 6 ports, while only 5 of them are real.
591 * Port 5 is enclosure management bridge port, which has implementation
592 * problems, causing probe faults. Hide it for now. */
593 if (softc->pm_pid == 0x37261095 && softc->pm_ports == 6)
595 /* This PMP declares 7 ports, while only 5 of them are real.
596 * Port 5 is some fake "Config Disk" with 640 sectors size,
597 * port 6 is enclosure management bridge port.
598 * Both fake ports has implementation problems, causing
599 * probe faults. Hide them for now. */
600 if (softc->pm_pid == 0x47261095 && softc->pm_ports == 7)
602 /* These PMPs declare one more port then actually have,
603 * for configuration purposes. Hide it for now. */
604 if (softc->pm_pid == 0x57231095 || softc->pm_pid == 0x57331095 ||
605 softc->pm_pid == 0x57341095 || softc->pm_pid == 0x57441095)
607 printf("%s%d: %d fan-out ports\n",
608 periph->periph_name, periph->unit_number,
610 softc->state = PMP_STATE_PRECONFIG;
611 xpt_release_ccb(done_ccb);
612 xpt_schedule(periph, priority);
614 case PMP_STATE_PRECONFIG:
616 softc->state = PMP_STATE_RESET;
617 softc->reset |= ~softc->found;
618 xpt_release_ccb(done_ccb);
619 xpt_schedule(periph, priority);
621 case PMP_STATE_RESET:
623 if (softc->pm_step >= softc->pm_ports) {
625 cam_freeze_devq(periph->path);
626 cam_release_devq(periph->path,
627 RELSIM_RELEASE_AFTER_TIMEOUT,
631 softc->state = PMP_STATE_CONNECT;
633 xpt_release_ccb(done_ccb);
634 xpt_schedule(periph, priority);
636 case PMP_STATE_CONNECT:
638 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_CHECK;
649 xpt_release_ccb(done_ccb);
650 xpt_schedule(periph, priority);
652 case PMP_STATE_CHECK:
653 res = (done_ccb->ataio.res.lba_high << 24) +
654 (done_ccb->ataio.res.lba_mid << 16) +
655 (done_ccb->ataio.res.lba_low << 8) +
656 done_ccb->ataio.res.sector_count;
657 if (((res & 0xf0f) == 0x103 && (res & 0x0f0) != 0) ||
658 (res & 0x600) != 0) {
660 printf("%s%d: port %d status: %08x\n",
661 periph->periph_name, periph->unit_number,
662 softc->pm_step, res);
664 /* Report device speed if it is online. */
665 if ((res & 0xf0f) == 0x103 &&
666 xpt_create_path(&dpath, periph,
667 xpt_path_path_id(periph->path),
668 softc->pm_step, 0) == CAM_REQ_CMP) {
669 bzero(&cts, sizeof(cts));
670 xpt_setup_ccb(&cts.ccb_h, dpath, CAM_PRIORITY_NONE);
671 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
672 cts.type = CTS_TYPE_CURRENT_SETTINGS;
673 cts.xport_specific.sata.revision = (res & 0x0f0) >> 4;
674 cts.xport_specific.sata.valid = CTS_SATA_VALID_REVISION;
675 cts.xport_specific.sata.caps = softc->caps &
676 (CTS_SATA_CAPS_H_PMREQ |
677 CTS_SATA_CAPS_H_DMAAA |
679 cts.xport_specific.sata.valid |= CTS_SATA_VALID_CAPS;
680 xpt_action((union ccb *)&cts);
681 xpt_free_path(dpath);
683 softc->found |= (1 << softc->pm_step);
686 if (softc->pm_try < 10) {
687 cam_freeze_devq(periph->path);
688 cam_release_devq(periph->path,
689 RELSIM_RELEASE_AFTER_TIMEOUT,
696 printf("%s%d: port %d status: %08x\n",
697 periph->periph_name, periph->unit_number,
698 softc->pm_step, res);
700 softc->found &= ~(1 << softc->pm_step);
701 if (xpt_create_path(&dpath, periph,
702 done_ccb->ccb_h.path_id,
703 softc->pm_step, 0) == CAM_REQ_CMP) {
704 xpt_async(AC_LOST_DEVICE, dpath, NULL);
705 xpt_free_path(dpath);
710 if (softc->pm_step >= softc->pm_ports) {
711 if (softc->reset & softc->found) {
712 cam_freeze_devq(periph->path);
713 cam_release_devq(periph->path,
714 RELSIM_RELEASE_AFTER_TIMEOUT,
719 softc->state = PMP_STATE_CLEAR;
722 xpt_release_ccb(done_ccb);
723 xpt_schedule(periph, priority);
725 case PMP_STATE_CLEAR:
727 if (softc->pm_step >= softc->pm_ports) {
728 softc->state = PMP_STATE_CONFIG;
731 xpt_release_ccb(done_ccb);
732 xpt_schedule(periph, priority);
734 case PMP_STATE_CONFIG:
735 for (i = 0; i < softc->pm_ports; i++) {
738 if ((softc->found & (1 << i)) == 0)
740 if (xpt_create_path(&dpath, periph,
741 xpt_path_path_id(periph->path),
742 i, 0) != CAM_REQ_CMP) {
743 printf("pmpdone: xpt_create_path failed\n");
746 /* If we did hard reset to this device, inform XPT. */
747 if ((softc->reset & softc->found & (1 << i)) != 0)
748 xpt_async(AC_SENT_BDR, dpath, NULL);
749 /* If rescan requested, scan this device. */
750 if (softc->events & PMP_EV_RESCAN) {
751 ccb = xpt_alloc_ccb_nowait();
753 xpt_free_path(dpath);
756 xpt_setup_ccb(&ccb->ccb_h, dpath, CAM_PRIORITY_XPT);
759 xpt_free_path(dpath);
766 xpt_release_ccb(done_ccb);
767 softc->state = PMP_STATE_NORMAL;
770 pmprelease(periph, -1);
771 cam_periph_release_locked(periph);