scsi_cd.c
来自「基于组件方式开发操作系统的OSKIT源代码」· C语言 代码 · 共 2,447 行 · 第 1/5 页
C
2,447 行
num_changers--; } devstat_remove_entry(&softc->device_stats); cam_extend_release(cdperiphs, periph->unit_number); free(softc, M_DEVBUF); splx(s);}static voidcdasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg){ struct cam_periph *periph; periph = (struct cam_periph *)callback_arg; switch (code) { case AC_FOUND_DEVICE: { struct ccb_getdev *cgd; cam_status status; cgd = (struct ccb_getdev *)arg; if ((cgd->pd_type != T_CDROM) && (cgd->pd_type != T_WORM)) break; /* * Allocate a peripheral instance for * this device and start the probe * process. */ status = cam_periph_alloc(cdregister, cdoninvalidate, cdcleanup, cdstart, "cd", CAM_PERIPH_BIO, cgd->ccb_h.path, cdasync, AC_FOUND_DEVICE, cgd); if (status != CAM_REQ_CMP && status != CAM_REQ_INPROG) printf("cdasync: Unable to attach new device " "due to status 0x%x\n", status); break; } case AC_LOST_DEVICE: cam_periph_invalidate(periph); break; case AC_SENT_BDR: case AC_BUS_RESET: { struct cd_softc *softc; struct ccb_hdr *ccbh; int s; softc = (struct cd_softc *)periph->softc; s = splsoftcam(); /* * Don't fail on the expected unit attention * that will occur. */ softc->flags |= CD_FLAG_RETRY_UA; for (ccbh = LIST_FIRST(&softc->pending_ccbs); ccbh != NULL; ccbh = LIST_NEXT(ccbh, periph_links.le)) ccbh->ccb_state |= CD_CCB_RETRY_UA; splx(s); break; } case AC_TRANSFER_NEG: case AC_SCSI_AEN: case AC_UNSOL_RESEL: default: break; }}static cam_statuscdregister(struct cam_periph *periph, void *arg){ struct cd_softc *softc; struct ccb_setasync csa; struct ccb_getdev *cgd; caddr_t match; cgd = (struct ccb_getdev *)arg; if (periph == NULL) { printf("cdregister: periph was NULL!!\n"); return(CAM_REQ_CMP_ERR); } if (cgd == NULL) { printf("cdregister: no getdev CCB, can't register device\n"); return(CAM_REQ_CMP_ERR); } softc = (struct cd_softc *)malloc(sizeof(*softc),M_DEVBUF,M_NOWAIT); if (softc == NULL) { printf("cdregister: Unable to probe new device. " "Unable to allocate softc\n"); return(CAM_REQ_CMP_ERR); } bzero(softc, sizeof(*softc)); LIST_INIT(&softc->pending_ccbs); softc->state = CD_STATE_PROBE; bufq_init(&softc->buf_queue); if (SID_IS_REMOVABLE(&cgd->inq_data)) softc->flags |= CD_FLAG_DISC_REMOVABLE; if ((cgd->inq_data.flags & SID_CmdQue) != 0) softc->flags |= CD_FLAG_TAGGED_QUEUING; periph->softc = softc; softc->periph = periph; cam_extend_set(cdperiphs, periph->unit_number, periph); /* * See if this device has any quirks. */ match = cam_quirkmatch((caddr_t)&cgd->inq_data, (caddr_t)cd_quirk_table, sizeof(cd_quirk_table)/sizeof(*cd_quirk_table), sizeof(*cd_quirk_table), scsi_inquiry_match); if (match != NULL) softc->quirks = ((struct cd_quirk_entry *)match)->quirks; else softc->quirks = CD_Q_NONE; /* * We need to register the statistics structure for this device, * but we don't have the blocksize yet for it. So, we register * the structure and indicate that we don't have the blocksize * yet. Unlike other SCSI peripheral drivers, we explicitly set * the device type here to be CDROM, rather than just ORing in * cgd->pd_type. This is because this driver can attach to either * CDROM or WORM devices, and we want this peripheral driver to * show up in the devstat list as a CD peripheral driver, not a * WORM peripheral driver. WORM drives will also have the WORM * driver attached to them. */ devstat_add_entry(&softc->device_stats, "cd", periph->unit_number, 0, DEVSTAT_BS_UNAVAILABLE, DEVSTAT_TYPE_CDROM | DEVSTAT_TYPE_IF_SCSI, DEVSTAT_PRIORITY_CD); /* * Add an async callback so that we get * notified if this device goes away. */ xpt_setup_ccb(&csa.ccb_h, periph->path, /* priority */ 5); csa.ccb_h.func_code = XPT_SASYNC_CB; csa.event_enable = AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE; csa.callback = cdasync; csa.callback_arg = periph; xpt_action((union ccb *)&csa); /* * If the target lun is greater than 0, we most likely have a CD * changer device. Check the quirk entries as well, though, just * in case someone has a CD tower with one lun per drive or * something like that. Also, if we know up front that a * particular device is a changer, we can mark it as such starting * with lun 0, instead of lun 1. It shouldn't be necessary to have * a quirk entry to define something as a changer, however. */ if (((cgd->ccb_h.target_lun > 0) && ((softc->quirks & CD_Q_NO_CHANGER) == 0)) || ((softc->quirks & CD_Q_CHANGER) != 0)) { struct cdchanger *nchanger; struct cam_periph *nperiph; struct cam_path *path; cam_status status; int found; /* Set the changer flag in the current device's softc */ softc->flags |= CD_FLAG_CHANGER; if (num_changers == 0) STAILQ_INIT(&changerq); /* * Now, look around for an existing changer device with the * same path and target ID as the current device. */ for (found = 0, nchanger = (struct cdchanger *)STAILQ_FIRST(&changerq); nchanger != NULL; nchanger = STAILQ_NEXT(nchanger, changer_links)){ if ((nchanger->path_id == cgd->ccb_h.path_id) && (nchanger->target_id == cgd->ccb_h.target_id)) { found = 1; break; } } /* * If we found a matching entry, just add this device to * the list of devices on this changer. */ if (found == 1) { struct chdevlist *chlunhead; chlunhead = &nchanger->chluns; /* * XXX KDM look at consolidating this code with the * code below in a separate function. */ /* * Create a path with lun id 0, and see if we can * find a matching device */ status = xpt_create_path(&path, /*periph*/ periph, cgd->ccb_h.path_id, cgd->ccb_h.target_id, 0); if ((status == CAM_REQ_CMP) && ((nperiph = cam_periph_find(path, "cd")) != NULL)){ struct cd_softc *nsoftc; nsoftc = (struct cd_softc *)nperiph->softc; if ((nsoftc->flags & CD_FLAG_CHANGER) == 0){ nsoftc->flags |= CD_FLAG_CHANGER; nchanger->num_devices++; if (camq_resize(&nchanger->devq, nchanger->num_devices)!=CAM_REQ_CMP){ printf("cdregister: " "camq_resize " "failed, changer " "support may " "be messed up\n"); } nsoftc->changer = nchanger; nsoftc->pinfo.index =CAM_UNQUEUED_INDEX; STAILQ_INSERT_TAIL(&nchanger->chluns, nsoftc,changer_links); } } else if (status == CAM_REQ_CMP) xpt_free_path(path); else { printf("cdregister: unable to allocate path\n" "cdregister: changer support may be " "broken\n"); } nchanger->num_devices++; softc->changer = nchanger; softc->pinfo.index = CAM_UNQUEUED_INDEX; if (camq_resize(&nchanger->devq, nchanger->num_devices) != CAM_REQ_CMP) { printf("cdregister: camq_resize " "failed, changer support may " "be messed up\n"); } STAILQ_INSERT_TAIL(chlunhead, softc, changer_links); } /* * In this case, we don't already have an entry for this * particular changer, so we need to create one, add it to * the queue, and queue this device on the list for this * changer. Before we queue this device, however, we need * to search for lun id 0 on this target, and add it to the * queue first, if it exists. (and if it hasn't already * been marked as part of the changer.) */ else { nchanger = malloc(sizeof(struct cdchanger), M_DEVBUF, M_NOWAIT); if (nchanger == NULL) { softc->flags &= ~CD_FLAG_CHANGER; printf("cdregister: unable to malloc " "changer structure\ncdregister: " "changer support disabled\n"); /* * Yes, gotos can be gross but in this case * I think it's justified.. */ goto cdregisterexit; } /* zero the structure */ bzero(nchanger, sizeof(struct cdchanger)); if (camq_init(&nchanger->devq, 1) != 0) { softc->flags &= ~CD_FLAG_CHANGER; printf("cdregister: changer support " "disabled\n"); goto cdregisterexit; } num_changers++; nchanger->path_id = cgd->ccb_h.path_id; nchanger->target_id = cgd->ccb_h.target_id; /* this is superfluous, but it makes things clearer */ nchanger->num_devices = 0; STAILQ_INIT(&nchanger->chluns); STAILQ_INSERT_TAIL(&changerq, nchanger, changer_links); /* * Create a path with lun id 0, and see if we can * find a matching device */ status = xpt_create_path(&path, /*periph*/ periph, cgd->ccb_h.path_id, cgd->ccb_h.target_id, 0); /* * If we were able to allocate the path, and if we * find a matching device and it isn't already * marked as part of a changer, then we add it to * the current changer. */ if ((status == CAM_REQ_CMP) && ((nperiph = cam_periph_find(path, "cd")) != NULL) && ((((struct cd_softc *)periph->softc)->flags & CD_FLAG_CHANGER) == 0)) { struct cd_softc *nsoftc; nsoftc = (struct cd_softc *)nperiph->softc; nsoftc->flags |= CD_FLAG_CHANGER; nchanger->num_devices++; if (camq_resize(&nchanger->devq, nchanger->num_devices) != CAM_REQ_CMP) { printf("cdregister: camq_resize " "failed, changer support may " "be messed up\n"); } nsoftc->changer = nchanger; nsoftc->pinfo.index = CAM_UNQUEUED_INDEX; STAILQ_INSERT_TAIL(&nchanger->chluns, nsoftc, changer_links); } else if (status == CAM_REQ_CMP) xpt_free_path(path); else { printf("cdregister: unable to allocate path\n" "cdregister: changer support may be " "broken\n"); } softc->changer = nchanger; softc->pinfo.index = CAM_UNQUEUED_INDEX; nchanger->num_devices++; if (camq_resize(&nchanger->devq, nchanger->num_devices) != CAM_REQ_CMP) { printf("cdregister: camq_resize " "failed, changer support may " "be messed up\n"); } STAILQ_INSERT_TAIL(&nchanger->chluns, softc, changer_links); } }cdregisterexit: /* Lock this peripheral until we are setup */ /* Can't block */ cam_periph_lock(periph, PRIBIO); if ((softc->flags & CD_FLAG_CHANGER) == 0) xpt_schedule(periph, /*priority*/5); else cdschedule(periph, /*priority*/ 5); return(CAM_REQ_CMP);}static intcdopen(dev_t dev, int flags, int fmt, struct proc *p){ struct disklabel label; struct cam_periph *periph; struct cd_softc *softc; struct ccb_getdev cgd; u_int32_t size; int unit, error; int s; unit = dkunit(dev); periph = cam_extend_get(cdperiphs, unit); if (periph == NULL) return (ENXIO); softc = (struct cd_softc *)periph->softc; /* * Grab splsoftcam and hold it until we lock the peripheral. */ s = splsoftcam(); if (softc->flags & CD_FLAG_INVALID) { splx(s); return(ENXIO); } if ((error = cam_periph_lock(periph, PRIBIO | PCATCH)) != 0) { splx(s); return (error); } splx(s); if ((softc->flags & CD_FLAG_OPEN) == 0) { if (cam_periph_acquire(periph) != CAM_REQ_CMP) return(ENXIO); softc->flags |= CD_FLAG_OPEN; cdprevent(periph, PR_PREVENT); } /* find out the size */ if ((error = cdsize(dev, &size)) != 0) { if (dsisopen(softc->cd_slices) == 0) { cdprevent(periph, PR_ALLOW); softc->flags &= ~CD_FLAG_OPEN; } cam_periph_unlock(periph); if ((softc->flags & CD_FLAG_OPEN) == 0) cam_periph_release(periph); return(error); } /* * Build prototype label for whole disk. * Should take information about different data tracks from the * TOC and put it in the partition table. */ bzero(&label, sizeof(label)); label.d_type = DTYPE_SCSI; /* * Grab the inquiry data to get the vendor and product names. * Put them in the typename and packname for the label. */ xpt_setup_ccb(&cgd.ccb_h, periph->path, /*priority*/ 1); cgd.ccb_h.func_code = XPT_GDEV_TYPE; xpt_action((union ccb *)&cgd); strncpy(label.d_typename, cgd.inq_data.vendor, min(SID_VENDOR_SIZE, sizeof(label.d_typename))); strncpy(label.d_packname, cgd.inq_data.product, min(SID_PRODUCT_SIZE, sizeof(label.d_packname))); label.d_secsize = softc->params.blksize; label.d_secperunit = softc->params.disksize; label.d_flags = D_REMOVABLE; /* * Make partition 'a' cover the whole disk. This is a temporary * compatibility hack. The 'a' partition should not exist, so * the slice code won't create it. The slice code will make * partition (RAW_PART + 'a') cover the whole disk and fill in * some more defaults. */ label.d_partitions[0].p_size = label.d_secperunit; label.d_partitions[0].p_fstype = FS_OTHER; /* Initialize slice tables. */ error = dsopen("cd", dev, fmt, DSO_NOLABELS | DSO_ONESLICE, &softc->cd_slices, &label, cdstrategy1, (ds_setgeom_t *)NULL, &cd_cdevsw); if (error == 0) { /* * We unconditionally (re)set the blocksize each time the * CD device is opened. This is because the CD can change, * and therefore the blocksize might change. * XXX problems here if some slice or partition is still * open with the old size? */ if ((softc->device_stats.flags & DEVSTAT_BS_UNAVAILABLE) != 0) softc->device_stats.flags &= ~DEVSTAT_BS_UNAVAILABLE;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?