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 cam_path *dpath;
222 * De-register any async callbacks.
224 xpt_register_async(0, pmpasync, periph, periph->path);
226 for (i = 0; i < 15; i++) {
227 if (xpt_create_path(&dpath, periph,
228 xpt_path_path_id(periph->path),
229 i, 0) == CAM_REQ_CMP) {
230 xpt_async(AC_LOST_DEVICE, dpath, NULL);
231 xpt_free_path(dpath);
234 pmprelease(periph, -1);
235 xpt_print(periph->path, "lost device\n");
239 pmpcleanup(struct cam_periph *periph)
241 struct pmp_softc *softc;
243 softc = (struct pmp_softc *)periph->softc;
245 xpt_print(periph->path, "removing device entry\n");
246 cam_periph_unlock(periph);
249 * If we can't free the sysctl tree, oh well...
251 if ((softc->flags & PMP_FLAG_SCTX_INIT) != 0
252 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
253 xpt_print(periph->path, "can't remove sysctl context\n");
256 free(softc, M_DEVBUF);
257 cam_periph_lock(periph);
261 pmpasync(void *callback_arg, u_int32_t code,
262 struct cam_path *path, void *arg)
264 struct cam_periph *periph;
265 struct pmp_softc *softc;
267 periph = (struct cam_periph *)callback_arg;
269 case AC_FOUND_DEVICE:
271 struct ccb_getdev *cgd;
274 cgd = (struct ccb_getdev *)arg;
278 if (cgd->protocol != PROTO_SATAPM)
282 * Allocate a peripheral instance for
283 * this device and start the probe
286 status = cam_periph_alloc(pmpregister, pmponinvalidate,
287 pmpcleanup, pmpstart,
288 "pmp", CAM_PERIPH_BIO,
289 cgd->ccb_h.path, pmpasync,
290 AC_FOUND_DEVICE, cgd);
292 if (status != CAM_REQ_CMP
293 && status != CAM_REQ_INPROG)
294 printf("pmpasync: Unable to attach to new device "
295 "due to status 0x%x\n", status);
301 softc = (struct pmp_softc *)periph->softc;
302 cam_periph_async(periph, code, path, arg);
303 if (code == AC_SCSI_AEN)
304 softc->events |= PMP_EV_RESCAN;
306 softc->events |= PMP_EV_RESET;
307 if (code == AC_SCSI_AEN && softc->state != PMP_STATE_NORMAL)
310 pmpfreeze(periph, softc->found);
311 if (code == AC_SENT_BDR || code == AC_BUS_RESET)
312 softc->found = 0; /* We have to reset everything. */
313 if (softc->state == PMP_STATE_NORMAL) {
314 softc->state = PMP_STATE_PRECONFIG;
315 cam_periph_acquire(periph);
316 xpt_schedule(periph, CAM_PRIORITY_DEV);
321 cam_periph_async(periph, code, path, arg);
327 pmpsysctlinit(void *context, int pending)
329 struct cam_periph *periph;
330 struct pmp_softc *softc;
331 char tmpstr[80], tmpstr2[80];
333 periph = (struct cam_periph *)context;
334 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
337 softc = (struct pmp_softc *)periph->softc;
338 snprintf(tmpstr, sizeof(tmpstr), "CAM PMP unit %d", periph->unit_number);
339 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
341 sysctl_ctx_init(&softc->sysctl_ctx);
342 softc->flags |= PMP_FLAG_SCTX_INIT;
343 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
344 SYSCTL_STATIC_CHILDREN(_kern_cam_pmp), OID_AUTO, tmpstr2,
345 CTLFLAG_RD, 0, tmpstr);
346 if (softc->sysctl_tree == NULL) {
347 printf("pmpsysctlinit: unable to allocate sysctl tree\n");
348 cam_periph_release(periph);
352 cam_periph_release(periph);
356 pmpregister(struct cam_periph *periph, void *arg)
358 struct pmp_softc *softc;
359 struct ccb_getdev *cgd;
361 cgd = (struct ccb_getdev *)arg;
362 if (periph == NULL) {
363 printf("pmpregister: periph was NULL!!\n");
364 return(CAM_REQ_CMP_ERR);
368 printf("pmpregister: no getdev CCB, can't register device\n");
369 return(CAM_REQ_CMP_ERR);
372 softc = (struct pmp_softc *)malloc(sizeof(*softc), M_DEVBUF,
376 printf("pmpregister: Unable to probe new device. "
377 "Unable to allocate softc\n");
378 return(CAM_REQ_CMP_ERR);
380 periph->softc = softc;
382 softc->pm_pid = ((uint32_t *)&cgd->ident_data)[0];
383 softc->pm_prv = ((uint32_t *)&cgd->ident_data)[1];
384 TASK_INIT(&softc->sysctl_task, 0, pmpsysctlinit, periph);
386 xpt_announce_periph(periph, NULL);
389 * Add async callbacks for bus reset and
390 * bus device reset calls. I don't bother
391 * checking if this fails as, in most cases,
392 * the system will function just fine without
393 * them and the only alternative would be to
394 * not attach the device on failure.
396 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
397 AC_SCSI_AEN, pmpasync, periph, periph->path);
400 * Take an exclusive refcount on the periph while pmpstart is called
401 * to finish the probe. The reference will be dropped in pmpdone at
404 (void)cam_periph_acquire(periph);
406 softc->state = PMP_STATE_PORTS;
407 softc->events = PMP_EV_RESCAN;
408 xpt_schedule(periph, CAM_PRIORITY_DEV);
414 pmpstart(struct cam_periph *periph, union ccb *start_ccb)
416 struct ccb_trans_settings cts;
417 struct ccb_ataio *ataio;
418 struct pmp_softc *softc;
419 struct cam_path *dpath;
422 softc = (struct pmp_softc *)periph->softc;
423 ataio = &start_ccb->ataio;
425 if (softc->restart) {
427 softc->state = min(softc->state, PMP_STATE_PRECONFIG);
429 /* Fetch user wanted device speed. */
430 if (softc->state == PMP_STATE_RESET ||
431 softc->state == PMP_STATE_CONNECT) {
432 if (xpt_create_path(&dpath, periph,
433 xpt_path_path_id(periph->path),
434 softc->pm_step, 0) == CAM_REQ_CMP) {
435 bzero(&cts, sizeof(cts));
436 xpt_setup_ccb(&cts.ccb_h, dpath, CAM_PRIORITY_NONE);
437 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
438 cts.type = CTS_TYPE_USER_SETTINGS;
439 xpt_action((union ccb *)&cts);
440 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_REVISION)
441 revision = cts.xport_specific.sata.revision;
442 xpt_free_path(dpath);
445 switch (softc->state) {
446 case PMP_STATE_PORTS:
447 cam_fill_ataio(ataio,
450 /*flags*/CAM_DIR_NONE,
454 pmp_default_timeout * 1000);
455 ata_pm_read_cmd(ataio, 2, 15);
457 case PMP_STATE_PRECONFIG:
458 /* Get/update host SATA capabilities. */
459 bzero(&cts, sizeof(cts));
460 xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
461 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
462 cts.type = CTS_TYPE_CURRENT_SETTINGS;
463 xpt_action((union ccb *)&cts);
464 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
465 softc->caps = cts.xport_specific.sata.caps;
466 cam_fill_ataio(ataio,
469 /*flags*/CAM_DIR_NONE,
473 pmp_default_timeout * 1000);
474 ata_pm_write_cmd(ataio, 0x60, 15, 0x0);
476 case PMP_STATE_RESET:
477 cam_fill_ataio(ataio,
480 /*flags*/CAM_DIR_NONE,
484 pmp_default_timeout * 1000);
485 ata_pm_write_cmd(ataio, 2, softc->pm_step,
487 ((softc->found & (1 << softc->pm_step)) ? 0 : 1));
489 case PMP_STATE_CONNECT:
490 cam_fill_ataio(ataio,
493 /*flags*/CAM_DIR_NONE,
497 pmp_default_timeout * 1000);
498 ata_pm_write_cmd(ataio, 2, softc->pm_step,
501 case PMP_STATE_CHECK:
502 cam_fill_ataio(ataio,
505 /*flags*/CAM_DIR_NONE,
509 pmp_default_timeout * 1000);
510 ata_pm_read_cmd(ataio, 0, softc->pm_step);
512 case PMP_STATE_CLEAR:
514 cam_fill_ataio(ataio,
517 /*flags*/CAM_DIR_NONE,
521 pmp_default_timeout * 1000);
522 ata_pm_write_cmd(ataio, 1, softc->pm_step, 0xFFFFFFFF);
524 case PMP_STATE_CONFIG:
525 cam_fill_ataio(ataio,
528 /*flags*/CAM_DIR_NONE,
532 pmp_default_timeout * 1000);
533 ata_pm_write_cmd(ataio, 0x60, 15, 0x07 |
534 ((softc->caps & CTS_SATA_CAPS_H_AN) ? 0x08 : 0));
539 xpt_action(start_ccb);
543 pmpdone(struct cam_periph *periph, union ccb *done_ccb)
545 struct ccb_trans_settings cts;
546 struct pmp_softc *softc;
547 struct ccb_ataio *ataio;
548 struct cam_path *dpath;
549 u_int32_t priority, res;
552 softc = (struct pmp_softc *)periph->softc;
553 ataio = &done_ccb->ataio;
555 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("pmpdone\n"));
557 priority = done_ccb->ccb_h.pinfo.priority;
559 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
560 if (cam_periph_error(done_ccb, 0, 0, NULL) == ERESTART) {
562 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
563 cam_release_devq(done_ccb->ccb_h.path,
572 if (softc->restart) {
574 xpt_release_ccb(done_ccb);
575 softc->state = min(softc->state, PMP_STATE_PRECONFIG);
576 xpt_schedule(periph, priority);
580 switch (softc->state) {
581 case PMP_STATE_PORTS:
582 softc->pm_ports = (ataio->res.lba_high << 24) +
583 (ataio->res.lba_mid << 16) +
584 (ataio->res.lba_low << 8) +
585 ataio->res.sector_count;
586 /* This PMP declares 6 ports, while only 5 of them are real.
587 * Port 5 is enclosure management bridge port, which has implementation
588 * problems, causing probe faults. Hide it for now. */
589 if (softc->pm_pid == 0x37261095 && softc->pm_ports == 6)
591 /* This PMP declares 7 ports, while only 5 of them are real.
592 * Port 5 is some fake "Config Disk" with 640 sectors size,
593 * port 6 is enclosure management bridge port.
594 * Both fake ports has implementation problems, causing
595 * probe faults. Hide them for now. */
596 if (softc->pm_pid == 0x47261095 && softc->pm_ports == 7)
598 /* These PMPs declare one more port then actually have,
599 * for configuration purposes. Hide it for now. */
600 if (softc->pm_pid == 0x57231095 || softc->pm_pid == 0x57331095 ||
601 softc->pm_pid == 0x57341095 || softc->pm_pid == 0x57441095)
603 printf("%s%d: %d fan-out ports\n",
604 periph->periph_name, periph->unit_number,
606 softc->state = PMP_STATE_PRECONFIG;
607 xpt_release_ccb(done_ccb);
608 xpt_schedule(periph, priority);
610 case PMP_STATE_PRECONFIG:
612 softc->state = PMP_STATE_RESET;
613 softc->reset |= ~softc->found;
614 xpt_release_ccb(done_ccb);
615 xpt_schedule(periph, priority);
617 case PMP_STATE_RESET:
619 if (softc->pm_step >= softc->pm_ports) {
621 cam_freeze_devq(periph->path);
622 cam_release_devq(periph->path,
623 RELSIM_RELEASE_AFTER_TIMEOUT,
627 softc->state = PMP_STATE_CONNECT;
629 xpt_release_ccb(done_ccb);
630 xpt_schedule(periph, priority);
632 case PMP_STATE_CONNECT:
634 if (softc->pm_step >= softc->pm_ports) {
637 cam_freeze_devq(periph->path);
638 cam_release_devq(periph->path,
639 RELSIM_RELEASE_AFTER_TIMEOUT,
643 softc->state = PMP_STATE_CHECK;
645 xpt_release_ccb(done_ccb);
646 xpt_schedule(periph, priority);
648 case PMP_STATE_CHECK:
649 res = (ataio->res.lba_high << 24) +
650 (ataio->res.lba_mid << 16) +
651 (ataio->res.lba_low << 8) +
652 ataio->res.sector_count;
653 if (((res & 0xf0f) == 0x103 && (res & 0x0f0) != 0) ||
654 (res & 0x600) != 0) {
656 printf("%s%d: port %d status: %08x\n",
657 periph->periph_name, periph->unit_number,
658 softc->pm_step, res);
660 /* Report device speed if it is online. */
661 if ((res & 0xf0f) == 0x103 &&
662 xpt_create_path(&dpath, periph,
663 xpt_path_path_id(periph->path),
664 softc->pm_step, 0) == CAM_REQ_CMP) {
665 bzero(&cts, sizeof(cts));
666 xpt_setup_ccb(&cts.ccb_h, dpath, CAM_PRIORITY_NONE);
667 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
668 cts.type = CTS_TYPE_CURRENT_SETTINGS;
669 cts.xport_specific.sata.revision = (res & 0x0f0) >> 4;
670 cts.xport_specific.sata.valid = CTS_SATA_VALID_REVISION;
671 cts.xport_specific.sata.caps = softc->caps &
672 (CTS_SATA_CAPS_H_PMREQ |
673 CTS_SATA_CAPS_H_DMAAA |
675 cts.xport_specific.sata.valid |= CTS_SATA_VALID_CAPS;
676 xpt_action((union ccb *)&cts);
677 xpt_free_path(dpath);
679 softc->found |= (1 << softc->pm_step);
682 if (softc->pm_try < 10) {
683 cam_freeze_devq(periph->path);
684 cam_release_devq(periph->path,
685 RELSIM_RELEASE_AFTER_TIMEOUT,
692 printf("%s%d: port %d status: %08x\n",
693 periph->periph_name, periph->unit_number,
694 softc->pm_step, res);
696 softc->found &= ~(1 << softc->pm_step);
697 if (xpt_create_path(&dpath, periph,
698 done_ccb->ccb_h.path_id,
699 softc->pm_step, 0) == CAM_REQ_CMP) {
700 xpt_async(AC_LOST_DEVICE, dpath, NULL);
701 xpt_free_path(dpath);
706 if (softc->pm_step >= softc->pm_ports) {
707 if (softc->reset & softc->found) {
708 cam_freeze_devq(periph->path);
709 cam_release_devq(periph->path,
710 RELSIM_RELEASE_AFTER_TIMEOUT,
715 softc->state = PMP_STATE_CLEAR;
718 xpt_release_ccb(done_ccb);
719 xpt_schedule(periph, priority);
721 case PMP_STATE_CLEAR:
723 if (softc->pm_step >= softc->pm_ports) {
724 softc->state = PMP_STATE_CONFIG;
727 xpt_release_ccb(done_ccb);
728 xpt_schedule(periph, priority);
730 case PMP_STATE_CONFIG:
731 for (i = 0; i < softc->pm_ports; i++) {
734 if ((softc->found & (1 << i)) == 0)
736 if (xpt_create_path(&dpath, periph,
737 xpt_path_path_id(periph->path),
738 i, 0) != CAM_REQ_CMP) {
739 printf("pmpdone: xpt_create_path failed\n");
742 /* If we did hard reset to this device, inform XPT. */
743 if ((softc->reset & softc->found & (1 << i)) != 0)
744 xpt_async(AC_SENT_BDR, dpath, NULL);
745 /* If rescan requested, scan this device. */
746 if (softc->events & PMP_EV_RESCAN) {
747 ccb = xpt_alloc_ccb_nowait();
749 xpt_free_path(dpath);
752 xpt_setup_ccb(&ccb->ccb_h, dpath, CAM_PRIORITY_XPT);
755 xpt_free_path(dpath);
762 xpt_release_ccb(done_ccb);
763 softc->state = PMP_STATE_NORMAL;
766 pmprelease(periph, -1);
767 cam_periph_release_locked(periph);