0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include "zfcp_def.h"
0012 #include "zfcp_ext.h"
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022 void zfcp_unit_scsi_scan(struct zfcp_unit *unit)
0023 {
0024 struct fc_rport *rport = unit->port->rport;
0025 u64 lun;
0026
0027 lun = scsilun_to_int((struct scsi_lun *) &unit->fcp_lun);
0028
0029 if (rport && rport->port_state == FC_PORTSTATE_ONLINE)
0030 scsi_scan_target(&rport->dev, 0, rport->scsi_target_id, lun,
0031 SCSI_SCAN_MANUAL);
0032 }
0033
0034 static void zfcp_unit_scsi_scan_work(struct work_struct *work)
0035 {
0036 struct zfcp_unit *unit = container_of(work, struct zfcp_unit,
0037 scsi_work);
0038
0039 zfcp_unit_scsi_scan(unit);
0040 put_device(&unit->dev);
0041 }
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053 void zfcp_unit_queue_scsi_scan(struct zfcp_port *port)
0054 {
0055 struct zfcp_unit *unit;
0056
0057 read_lock_irq(&port->unit_list_lock);
0058 list_for_each_entry(unit, &port->unit_list, list) {
0059 get_device(&unit->dev);
0060 if (scsi_queue_work(port->adapter->scsi_host,
0061 &unit->scsi_work) <= 0)
0062 put_device(&unit->dev);
0063 }
0064 read_unlock_irq(&port->unit_list_lock);
0065 }
0066
0067 static struct zfcp_unit *_zfcp_unit_find(struct zfcp_port *port, u64 fcp_lun)
0068 {
0069 struct zfcp_unit *unit;
0070
0071 list_for_each_entry(unit, &port->unit_list, list)
0072 if (unit->fcp_lun == fcp_lun) {
0073 get_device(&unit->dev);
0074 return unit;
0075 }
0076
0077 return NULL;
0078 }
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091 struct zfcp_unit *zfcp_unit_find(struct zfcp_port *port, u64 fcp_lun)
0092 {
0093 struct zfcp_unit *unit;
0094
0095 read_lock_irq(&port->unit_list_lock);
0096 unit = _zfcp_unit_find(port, fcp_lun);
0097 read_unlock_irq(&port->unit_list_lock);
0098 return unit;
0099 }
0100
0101
0102
0103
0104
0105 static void zfcp_unit_release(struct device *dev)
0106 {
0107 struct zfcp_unit *unit = container_of(dev, struct zfcp_unit, dev);
0108
0109 atomic_dec(&unit->port->units);
0110 kfree(unit);
0111 }
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121 int zfcp_unit_add(struct zfcp_port *port, u64 fcp_lun)
0122 {
0123 struct zfcp_unit *unit;
0124 int retval = 0;
0125
0126 mutex_lock(&zfcp_sysfs_port_units_mutex);
0127 if (zfcp_sysfs_port_is_removing(port)) {
0128
0129 retval = -ENODEV;
0130 goto out;
0131 }
0132
0133 unit = zfcp_unit_find(port, fcp_lun);
0134 if (unit) {
0135 put_device(&unit->dev);
0136 retval = -EEXIST;
0137 goto out;
0138 }
0139
0140 unit = kzalloc(sizeof(struct zfcp_unit), GFP_KERNEL);
0141 if (!unit) {
0142 retval = -ENOMEM;
0143 goto out;
0144 }
0145
0146 unit->port = port;
0147 unit->fcp_lun = fcp_lun;
0148 unit->dev.parent = &port->dev;
0149 unit->dev.release = zfcp_unit_release;
0150 unit->dev.groups = zfcp_unit_attr_groups;
0151 INIT_WORK(&unit->scsi_work, zfcp_unit_scsi_scan_work);
0152
0153 if (dev_set_name(&unit->dev, "0x%016llx",
0154 (unsigned long long) fcp_lun)) {
0155 kfree(unit);
0156 retval = -ENOMEM;
0157 goto out;
0158 }
0159
0160 if (device_register(&unit->dev)) {
0161 put_device(&unit->dev);
0162 retval = -ENOMEM;
0163 goto out;
0164 }
0165
0166 atomic_inc(&port->units);
0167
0168 write_lock_irq(&port->unit_list_lock);
0169 list_add_tail(&unit->list, &port->unit_list);
0170 write_unlock_irq(&port->unit_list_lock);
0171
0172
0173
0174
0175 mutex_unlock(&zfcp_sysfs_port_units_mutex);
0176
0177 zfcp_unit_scsi_scan(unit);
0178 return retval;
0179
0180 out:
0181 mutex_unlock(&zfcp_sysfs_port_units_mutex);
0182 return retval;
0183 }
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194
0195 struct scsi_device *zfcp_unit_sdev(struct zfcp_unit *unit)
0196 {
0197 struct Scsi_Host *shost;
0198 struct zfcp_port *port;
0199 u64 lun;
0200
0201 lun = scsilun_to_int((struct scsi_lun *) &unit->fcp_lun);
0202 port = unit->port;
0203 shost = port->adapter->scsi_host;
0204 return scsi_device_lookup(shost, 0, port->starget_id, lun);
0205 }
0206
0207
0208
0209
0210
0211
0212
0213
0214 unsigned int zfcp_unit_sdev_status(struct zfcp_unit *unit)
0215 {
0216 unsigned int status = 0;
0217 struct scsi_device *sdev;
0218 struct zfcp_scsi_dev *zfcp_sdev;
0219
0220 sdev = zfcp_unit_sdev(unit);
0221 if (sdev) {
0222 zfcp_sdev = sdev_to_zfcp(sdev);
0223 status = atomic_read(&zfcp_sdev->status);
0224 scsi_device_put(sdev);
0225 }
0226
0227 return status;
0228 }
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238 int zfcp_unit_remove(struct zfcp_port *port, u64 fcp_lun)
0239 {
0240 struct zfcp_unit *unit;
0241 struct scsi_device *sdev;
0242
0243 write_lock_irq(&port->unit_list_lock);
0244 unit = _zfcp_unit_find(port, fcp_lun);
0245 if (unit)
0246 list_del(&unit->list);
0247 write_unlock_irq(&port->unit_list_lock);
0248
0249 if (!unit)
0250 return -EINVAL;
0251
0252 sdev = zfcp_unit_sdev(unit);
0253 if (sdev) {
0254 scsi_remove_device(sdev);
0255 scsi_device_put(sdev);
0256 }
0257
0258 device_unregister(&unit->dev);
0259
0260 put_device(&unit->dev);
0261
0262 return 0;
0263 }