scsi_sysfs.c
来自「linux 内核源代码」· C语言 代码 · 共 1,066 行 · 第 1/2 页
C
1,066 行
/* * scsi_sysfs.c * * SCSI sysfs interface routines. * * Created to pull SCSI mid layer sysfs routines into one file. */#include <linux/module.h>#include <linux/init.h>#include <linux/blkdev.h>#include <linux/device.h>#include <scsi/scsi.h>#include <scsi/scsi_device.h>#include <scsi/scsi_host.h>#include <scsi/scsi_tcq.h>#include <scsi/scsi_transport.h>#include <scsi/scsi_driver.h>#include "scsi_priv.h"#include "scsi_logging.h"static const struct { enum scsi_device_state value; char *name;} sdev_states[] = { { SDEV_CREATED, "created" }, { SDEV_RUNNING, "running" }, { SDEV_CANCEL, "cancel" }, { SDEV_DEL, "deleted" }, { SDEV_QUIESCE, "quiesce" }, { SDEV_OFFLINE, "offline" }, { SDEV_BLOCK, "blocked" },};const char *scsi_device_state_name(enum scsi_device_state state){ int i; char *name = NULL; for (i = 0; i < ARRAY_SIZE(sdev_states); i++) { if (sdev_states[i].value == state) { name = sdev_states[i].name; break; } } return name;}static const struct { enum scsi_host_state value; char *name;} shost_states[] = { { SHOST_CREATED, "created" }, { SHOST_RUNNING, "running" }, { SHOST_CANCEL, "cancel" }, { SHOST_DEL, "deleted" }, { SHOST_RECOVERY, "recovery" }, { SHOST_CANCEL_RECOVERY, "cancel/recovery" }, { SHOST_DEL_RECOVERY, "deleted/recovery", },};const char *scsi_host_state_name(enum scsi_host_state state){ int i; char *name = NULL; for (i = 0; i < ARRAY_SIZE(shost_states); i++) { if (shost_states[i].value == state) { name = shost_states[i].name; break; } } return name;}static int check_set(unsigned int *val, char *src){ char *last; if (strncmp(src, "-", 20) == 0) { *val = SCAN_WILD_CARD; } else { /* * Doesn't check for int overflow */ *val = simple_strtoul(src, &last, 0); if (*last != '\0') return 1; } return 0;}static int scsi_scan(struct Scsi_Host *shost, const char *str){ char s1[15], s2[15], s3[15], junk; unsigned int channel, id, lun; int res; res = sscanf(str, "%10s %10s %10s %c", s1, s2, s3, &junk); if (res != 3) return -EINVAL; if (check_set(&channel, s1)) return -EINVAL; if (check_set(&id, s2)) return -EINVAL; if (check_set(&lun, s3)) return -EINVAL; if (shost->transportt->user_scan) res = shost->transportt->user_scan(shost, channel, id, lun); else res = scsi_scan_host_selected(shost, channel, id, lun, 1); return res;}/* * shost_show_function: macro to create an attr function that can be used to * show a non-bit field. */#define shost_show_function(name, field, format_string) \static ssize_t \show_##name (struct class_device *class_dev, char *buf) \{ \ struct Scsi_Host *shost = class_to_shost(class_dev); \ return snprintf (buf, 20, format_string, shost->field); \}/* * shost_rd_attr: macro to create a function and attribute variable for a * read only field. */#define shost_rd_attr2(name, field, format_string) \ shost_show_function(name, field, format_string) \static CLASS_DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);#define shost_rd_attr(field, format_string) \shost_rd_attr2(field, field, format_string)/* * Create the actual show/store functions and data structures. */static ssize_t store_scan(struct class_device *class_dev, const char *buf, size_t count){ struct Scsi_Host *shost = class_to_shost(class_dev); int res; res = scsi_scan(shost, buf); if (res == 0) res = count; return res;};static CLASS_DEVICE_ATTR(scan, S_IWUSR, NULL, store_scan);static ssize_tstore_shost_state(struct class_device *class_dev, const char *buf, size_t count){ int i; struct Scsi_Host *shost = class_to_shost(class_dev); enum scsi_host_state state = 0; for (i = 0; i < ARRAY_SIZE(shost_states); i++) { const int len = strlen(shost_states[i].name); if (strncmp(shost_states[i].name, buf, len) == 0 && buf[len] == '\n') { state = shost_states[i].value; break; } } if (!state) return -EINVAL; if (scsi_host_set_state(shost, state)) return -EINVAL; return count;}static ssize_tshow_shost_state(struct class_device *class_dev, char *buf){ struct Scsi_Host *shost = class_to_shost(class_dev); const char *name = scsi_host_state_name(shost->shost_state); if (!name) return -EINVAL; return snprintf(buf, 20, "%s\n", name);}static CLASS_DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_shost_state, store_shost_state);static ssize_tshow_shost_mode(unsigned int mode, char *buf){ ssize_t len = 0; if (mode & MODE_INITIATOR) len = sprintf(buf, "%s", "Initiator"); if (mode & MODE_TARGET) len += sprintf(buf + len, "%s%s", len ? ", " : "", "Target"); len += sprintf(buf + len, "\n"); return len;}static ssize_t show_shost_supported_mode(struct class_device *class_dev, char *buf){ struct Scsi_Host *shost = class_to_shost(class_dev); unsigned int supported_mode = shost->hostt->supported_mode; if (supported_mode == MODE_UNKNOWN) /* by default this should be initiator */ supported_mode = MODE_INITIATOR; return show_shost_mode(supported_mode, buf);}static CLASS_DEVICE_ATTR(supported_mode, S_IRUGO | S_IWUSR, show_shost_supported_mode, NULL);static ssize_t show_shost_active_mode(struct class_device *class_dev, char *buf){ struct Scsi_Host *shost = class_to_shost(class_dev); if (shost->active_mode == MODE_UNKNOWN) return snprintf(buf, 20, "unknown\n"); else return show_shost_mode(shost->active_mode, buf);}static CLASS_DEVICE_ATTR(active_mode, S_IRUGO | S_IWUSR, show_shost_active_mode, NULL);shost_rd_attr(unique_id, "%u\n");shost_rd_attr(host_busy, "%hu\n");shost_rd_attr(cmd_per_lun, "%hd\n");shost_rd_attr(can_queue, "%hd\n");shost_rd_attr(sg_tablesize, "%hu\n");shost_rd_attr(unchecked_isa_dma, "%d\n");shost_rd_attr2(proc_name, hostt->proc_name, "%s\n");static struct class_device_attribute *scsi_sysfs_shost_attrs[] = { &class_device_attr_unique_id, &class_device_attr_host_busy, &class_device_attr_cmd_per_lun, &class_device_attr_can_queue, &class_device_attr_sg_tablesize, &class_device_attr_unchecked_isa_dma, &class_device_attr_proc_name, &class_device_attr_scan, &class_device_attr_state, &class_device_attr_supported_mode, &class_device_attr_active_mode, NULL};static void scsi_device_cls_release(struct class_device *class_dev){ struct scsi_device *sdev; sdev = class_to_sdev(class_dev); put_device(&sdev->sdev_gendev);}static void scsi_device_dev_release_usercontext(struct work_struct *work){ struct scsi_device *sdev; struct device *parent; struct scsi_target *starget; struct list_head *this, *tmp; unsigned long flags; sdev = container_of(work, struct scsi_device, ew.work); parent = sdev->sdev_gendev.parent; starget = to_scsi_target(parent); spin_lock_irqsave(sdev->host->host_lock, flags); starget->reap_ref++; list_del(&sdev->siblings); list_del(&sdev->same_target_siblings); list_del(&sdev->starved_entry); spin_unlock_irqrestore(sdev->host->host_lock, flags); cancel_work_sync(&sdev->event_work); list_for_each_safe(this, tmp, &sdev->event_list) { struct scsi_event *evt; evt = list_entry(this, struct scsi_event, node); list_del(&evt->node); kfree(evt); } if (sdev->request_queue) { sdev->request_queue->queuedata = NULL; /* user context needed to free queue */ scsi_free_queue(sdev->request_queue); /* temporary expedient, try to catch use of queue lock * after free of sdev */ sdev->request_queue = NULL; } scsi_target_reap(scsi_target(sdev)); kfree(sdev->inquiry); kfree(sdev); if (parent) put_device(parent);}static void scsi_device_dev_release(struct device *dev){ struct scsi_device *sdp = to_scsi_device(dev); execute_in_process_context(scsi_device_dev_release_usercontext, &sdp->ew);}static struct class sdev_class = { .name = "scsi_device", .release = scsi_device_cls_release,};/* all probing is done in the individual ->probe routines */static int scsi_bus_match(struct device *dev, struct device_driver *gendrv){ struct scsi_device *sdp = to_scsi_device(dev); if (sdp->no_uld_attach) return 0; return (sdp->inq_periph_qual == SCSI_INQ_PQ_CON)? 1: 0;}static int scsi_bus_uevent(struct device *dev, struct kobj_uevent_env *env){ struct scsi_device *sdev = to_scsi_device(dev); add_uevent_var(env, "MODALIAS=" SCSI_DEVICE_MODALIAS_FMT, sdev->type); return 0;}static int scsi_bus_suspend(struct device * dev, pm_message_t state){ struct device_driver *drv = dev->driver; struct scsi_device *sdev = to_scsi_device(dev); int err; err = scsi_device_quiesce(sdev); if (err) return err; if (drv && drv->suspend) { err = drv->suspend(dev, state); if (err) return err; } return 0;}static int scsi_bus_resume(struct device * dev){ struct device_driver *drv = dev->driver; struct scsi_device *sdev = to_scsi_device(dev); int err = 0; if (drv && drv->resume) err = drv->resume(dev); scsi_device_resume(sdev); return err;}static int scsi_bus_remove(struct device *dev){ struct device_driver *drv = dev->driver; struct scsi_device *sdev = to_scsi_device(dev); int err = 0; /* reset the prep_fn back to the default since the * driver may have altered it and it's being removed */ blk_queue_prep_rq(sdev->request_queue, scsi_prep_fn); if (drv && drv->remove) err = drv->remove(dev); return 0;}struct bus_type scsi_bus_type = { .name = "scsi", .match = scsi_bus_match, .uevent = scsi_bus_uevent, .suspend = scsi_bus_suspend, .resume = scsi_bus_resume, .remove = scsi_bus_remove,};int scsi_sysfs_register(void){ int error; error = bus_register(&scsi_bus_type); if (!error) { error = class_register(&sdev_class); if (error) bus_unregister(&scsi_bus_type); } return error;}void scsi_sysfs_unregister(void){ class_unregister(&sdev_class); bus_unregister(&scsi_bus_type);}/* * sdev_show_function: macro to create an attr function that can be used to * show a non-bit field. */#define sdev_show_function(field, format_string) \static ssize_t \sdev_show_##field (struct device *dev, struct device_attribute *attr, char *buf) \{ \ struct scsi_device *sdev; \ sdev = to_scsi_device(dev); \ return snprintf (buf, 20, format_string, sdev->field); \} \/* * sdev_rd_attr: macro to create a function and attribute variable for a * read only field. */#define sdev_rd_attr(field, format_string) \ sdev_show_function(field, format_string) \static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);/* * sdev_rd_attr: create a function and attribute variable for a * read/write field. */#define sdev_rw_attr(field, format_string) \ sdev_show_function(field, format_string) \ \static ssize_t \sdev_store_##field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \{ \ struct scsi_device *sdev; \ sdev = to_scsi_device(dev); \ snscanf (buf, 20, format_string, &sdev->field); \ return count; \} \static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);/* Currently we don't export bit fields, but we might in future, * so leave this code in */#if 0/* * sdev_rd_attr: create a function and attribute variable for a * read/write bit field. */#define sdev_rw_attr_bit(field) \ sdev_show_function(field, "%d\n") \ \static ssize_t \sdev_store_##field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \{ \ int ret; \ struct scsi_device *sdev; \ ret = scsi_sdev_check_buf_bit(buf); \ if (ret >= 0) { \ sdev = to_scsi_device(dev); \ sdev->field = ret; \ ret = count; \ } \ return ret; \} \static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);/* * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1", * else return -EINVAL. */static int scsi_sdev_check_buf_bit(const char *buf){ if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) { if (buf[0] == '1') return 1; else if (buf[0] == '0') return 0; else return -EINVAL; } else return -EINVAL;}#endif/* * Create the actual show/store functions and data structures. */sdev_rd_attr (device_blocked, "%d\n");sdev_rd_attr (queue_depth, "%d\n");sdev_rd_attr (type, "%d\n");sdev_rd_attr (scsi_level, "%d\n");sdev_rd_attr (vendor, "%.8s\n");sdev_rd_attr (model, "%.16s\n");sdev_rd_attr (rev, "%.4s\n");static ssize_tsdev_show_timeout (struct device *dev, struct device_attribute *attr, char *buf){ struct scsi_device *sdev; sdev = to_scsi_device(dev); return snprintf (buf, 20, "%d\n", sdev->timeout / HZ);}static ssize_tsdev_store_timeout (struct device *dev, struct device_attribute *attr, const char *buf, size_t count){ struct scsi_device *sdev; int timeout; sdev = to_scsi_device(dev); sscanf (buf, "%d\n", &timeout); sdev->timeout = timeout * HZ; return count;}static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);static ssize_t
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?