Linux 4.1.16
[linux/fpc-iii.git] / drivers / s390 / scsi / zfcp_unit.c
blob157d3d203ba1db0ade520c8f225acf04ff6fd8ad
1 /*
2 * zfcp device driver
4 * Tracking of manually configured LUNs and helper functions to
5 * register the LUNs with the SCSI midlayer.
7 * Copyright IBM Corp. 2010
8 */
10 #include "zfcp_def.h"
11 #include "zfcp_ext.h"
13 /**
14 * zfcp_unit_scsi_scan - Register LUN with SCSI midlayer
15 * @unit: The zfcp LUN/unit to register
17 * When the SCSI midlayer is not allowed to automatically scan and
18 * attach SCSI devices, zfcp has to register the single devices with
19 * the SCSI midlayer.
21 void zfcp_unit_scsi_scan(struct zfcp_unit *unit)
23 struct fc_rport *rport = unit->port->rport;
24 u64 lun;
26 lun = scsilun_to_int((struct scsi_lun *) &unit->fcp_lun);
28 if (rport && rport->port_state == FC_PORTSTATE_ONLINE)
29 scsi_scan_target(&rport->dev, 0, rport->scsi_target_id, lun, 1);
32 static void zfcp_unit_scsi_scan_work(struct work_struct *work)
34 struct zfcp_unit *unit = container_of(work, struct zfcp_unit,
35 scsi_work);
37 zfcp_unit_scsi_scan(unit);
38 put_device(&unit->dev);
41 /**
42 * zfcp_unit_queue_scsi_scan - Register configured units on port
43 * @port: The zfcp_port where to register units
45 * After opening a port, all units configured on this port have to be
46 * registered with the SCSI midlayer. This function should be called
47 * after calling fc_remote_port_add, so that the fc_rport is already
48 * ONLINE and the call to scsi_scan_target runs the same way as the
49 * call in the FC transport class.
51 void zfcp_unit_queue_scsi_scan(struct zfcp_port *port)
53 struct zfcp_unit *unit;
55 read_lock_irq(&port->unit_list_lock);
56 list_for_each_entry(unit, &port->unit_list, list) {
57 get_device(&unit->dev);
58 if (scsi_queue_work(port->adapter->scsi_host,
59 &unit->scsi_work) <= 0)
60 put_device(&unit->dev);
62 read_unlock_irq(&port->unit_list_lock);
65 static struct zfcp_unit *_zfcp_unit_find(struct zfcp_port *port, u64 fcp_lun)
67 struct zfcp_unit *unit;
69 list_for_each_entry(unit, &port->unit_list, list)
70 if (unit->fcp_lun == fcp_lun) {
71 get_device(&unit->dev);
72 return unit;
75 return NULL;
78 /**
79 * zfcp_unit_find - Find and return zfcp_unit with specified FCP LUN
80 * @port: zfcp_port where to look for the unit
81 * @fcp_lun: 64 Bit FCP LUN used to identify the zfcp_unit
83 * If zfcp_unit is found, a reference is acquired that has to be
84 * released later.
86 * Returns: Pointer to the zfcp_unit, or NULL if there is no zfcp_unit
87 * with the specified FCP LUN.
89 struct zfcp_unit *zfcp_unit_find(struct zfcp_port *port, u64 fcp_lun)
91 struct zfcp_unit *unit;
93 read_lock_irq(&port->unit_list_lock);
94 unit = _zfcp_unit_find(port, fcp_lun);
95 read_unlock_irq(&port->unit_list_lock);
96 return unit;
99 /**
100 * zfcp_unit_release - Drop reference to zfcp_port and free memory of zfcp_unit.
101 * @dev: pointer to device in zfcp_unit
103 static void zfcp_unit_release(struct device *dev)
105 struct zfcp_unit *unit = container_of(dev, struct zfcp_unit, dev);
107 atomic_dec(&unit->port->units);
108 kfree(unit);
112 * zfcp_unit_enqueue - enqueue unit to unit list of a port.
113 * @port: pointer to port where unit is added
114 * @fcp_lun: FCP LUN of unit to be enqueued
115 * Returns: 0 success
117 * Sets up some unit internal structures and creates sysfs entry.
119 int zfcp_unit_add(struct zfcp_port *port, u64 fcp_lun)
121 struct zfcp_unit *unit;
122 int retval = 0;
124 mutex_lock(&zfcp_sysfs_port_units_mutex);
125 if (atomic_read(&port->units) == -1) {
126 /* port is already gone */
127 retval = -ENODEV;
128 goto out;
131 unit = zfcp_unit_find(port, fcp_lun);
132 if (unit) {
133 put_device(&unit->dev);
134 retval = -EEXIST;
135 goto out;
138 unit = kzalloc(sizeof(struct zfcp_unit), GFP_KERNEL);
139 if (!unit) {
140 retval = -ENOMEM;
141 goto out;
144 unit->port = port;
145 unit->fcp_lun = fcp_lun;
146 unit->dev.parent = &port->dev;
147 unit->dev.release = zfcp_unit_release;
148 unit->dev.groups = zfcp_unit_attr_groups;
149 INIT_WORK(&unit->scsi_work, zfcp_unit_scsi_scan_work);
151 if (dev_set_name(&unit->dev, "0x%016llx",
152 (unsigned long long) fcp_lun)) {
153 kfree(unit);
154 retval = -ENOMEM;
155 goto out;
158 if (device_register(&unit->dev)) {
159 put_device(&unit->dev);
160 retval = -ENOMEM;
161 goto out;
164 atomic_inc(&port->units); /* under zfcp_sysfs_port_units_mutex ! */
166 write_lock_irq(&port->unit_list_lock);
167 list_add_tail(&unit->list, &port->unit_list);
168 write_unlock_irq(&port->unit_list_lock);
170 zfcp_unit_scsi_scan(unit);
172 out:
173 mutex_unlock(&zfcp_sysfs_port_units_mutex);
174 return retval;
178 * zfcp_unit_sdev - Return SCSI device for zfcp_unit
179 * @unit: The zfcp_unit where to get the SCSI device for
181 * Returns: scsi_device pointer on success, NULL if there is no SCSI
182 * device for this zfcp_unit
184 * On success, the caller also holds a reference to the SCSI device
185 * that must be released with scsi_device_put.
187 struct scsi_device *zfcp_unit_sdev(struct zfcp_unit *unit)
189 struct Scsi_Host *shost;
190 struct zfcp_port *port;
191 u64 lun;
193 lun = scsilun_to_int((struct scsi_lun *) &unit->fcp_lun);
194 port = unit->port;
195 shost = port->adapter->scsi_host;
196 return scsi_device_lookup(shost, 0, port->starget_id, lun);
200 * zfcp_unit_sdev_status - Return zfcp LUN status for SCSI device
201 * @unit: The unit to lookup the SCSI device for
203 * Returns the zfcp LUN status field of the SCSI device if the SCSI device
204 * for the zfcp_unit exists, 0 otherwise.
206 unsigned int zfcp_unit_sdev_status(struct zfcp_unit *unit)
208 unsigned int status = 0;
209 struct scsi_device *sdev;
210 struct zfcp_scsi_dev *zfcp_sdev;
212 sdev = zfcp_unit_sdev(unit);
213 if (sdev) {
214 zfcp_sdev = sdev_to_zfcp(sdev);
215 status = atomic_read(&zfcp_sdev->status);
216 scsi_device_put(sdev);
219 return status;
223 * zfcp_unit_remove - Remove entry from list of configured units
224 * @port: The port where to remove the unit from the configuration
225 * @fcp_lun: The 64 bit LUN of the unit to remove
227 * Returns: -EINVAL if a unit with the specified LUN does not exist,
228 * 0 on success.
230 int zfcp_unit_remove(struct zfcp_port *port, u64 fcp_lun)
232 struct zfcp_unit *unit;
233 struct scsi_device *sdev;
235 write_lock_irq(&port->unit_list_lock);
236 unit = _zfcp_unit_find(port, fcp_lun);
237 if (unit)
238 list_del(&unit->list);
239 write_unlock_irq(&port->unit_list_lock);
241 if (!unit)
242 return -EINVAL;
244 sdev = zfcp_unit_sdev(unit);
245 if (sdev) {
246 scsi_remove_device(sdev);
247 scsi_device_put(sdev);
250 put_device(&unit->dev);
252 device_unregister(&unit->dev);
254 return 0;