blob: 1cd2b99ab256c06132dd85c0a7a3d4293e98cb87 [file] [log] [blame]
Christof Schmitt1daa4eb2010-09-08 14:39:52 +02001/*
2 * zfcp device driver
3 *
4 * Tracking of manually configured LUNs and helper functions to
5 * register the LUNs with the SCSI midlayer.
6 *
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02007 * Copyright IBM Corp. 2010
Christof Schmitt1daa4eb2010-09-08 14:39:52 +02008 */
9
10#include "zfcp_def.h"
11#include "zfcp_ext.h"
12
13/**
14 * zfcp_unit_scsi_scan - Register LUN with SCSI midlayer
15 * @unit: The zfcp LUN/unit to register
16 *
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.
20 */
21void zfcp_unit_scsi_scan(struct zfcp_unit *unit)
22{
23 struct fc_rport *rport = unit->port->rport;
24 unsigned int lun;
25
26 lun = scsilun_to_int((struct scsi_lun *) &unit->fcp_lun);
27
28 if (rport && rport->port_state == FC_PORTSTATE_ONLINE)
29 scsi_scan_target(&rport->dev, 0, rport->scsi_target_id, lun, 1);
30}
31
32static void zfcp_unit_scsi_scan_work(struct work_struct *work)
33{
34 struct zfcp_unit *unit = container_of(work, struct zfcp_unit,
35 scsi_work);
36
37 zfcp_unit_scsi_scan(unit);
38 put_device(&unit->dev);
39}
40
41/**
42 * zfcp_unit_queue_scsi_scan - Register configured units on port
43 * @port: The zfcp_port where to register units
44 *
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.
50 */
51void zfcp_unit_queue_scsi_scan(struct zfcp_port *port)
52{
53 struct zfcp_unit *unit;
54
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);
61 }
62 read_unlock_irq(&port->unit_list_lock);
63}
64
65static struct zfcp_unit *_zfcp_unit_find(struct zfcp_port *port, u64 fcp_lun)
66{
67 struct zfcp_unit *unit;
68
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;
73 }
74
75 return NULL;
76}
77
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
82 *
83 * If zfcp_unit is found, a reference is acquired that has to be
84 * released later.
85 *
86 * Returns: Pointer to the zfcp_unit, or NULL if there is no zfcp_unit
87 * with the specified FCP LUN.
88 */
89struct zfcp_unit *zfcp_unit_find(struct zfcp_port *port, u64 fcp_lun)
90{
91 struct zfcp_unit *unit;
92
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;
97}
98
99/**
100 * zfcp_unit_release - Drop reference to zfcp_port and free memory of zfcp_unit.
101 * @dev: pointer to device in zfcp_unit
102 */
103static void zfcp_unit_release(struct device *dev)
104{
105 struct zfcp_unit *unit = container_of(dev, struct zfcp_unit, dev);
106
Steffen Maierd99b6012012-09-04 15:23:34 +0200107 atomic_dec(&unit->port->units);
Christof Schmitt1daa4eb2010-09-08 14:39:52 +0200108 kfree(unit);
109}
110
111/**
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
116 *
117 * Sets up some unit internal structures and creates sysfs entry.
118 */
119int zfcp_unit_add(struct zfcp_port *port, u64 fcp_lun)
120{
121 struct zfcp_unit *unit;
Steffen Maierd99b6012012-09-04 15:23:34 +0200122 int retval = 0;
123
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;
129 }
Christof Schmitt1daa4eb2010-09-08 14:39:52 +0200130
131 unit = zfcp_unit_find(port, fcp_lun);
132 if (unit) {
133 put_device(&unit->dev);
Steffen Maierd99b6012012-09-04 15:23:34 +0200134 retval = -EEXIST;
135 goto out;
Christof Schmitt1daa4eb2010-09-08 14:39:52 +0200136 }
137
138 unit = kzalloc(sizeof(struct zfcp_unit), GFP_KERNEL);
Steffen Maierd99b6012012-09-04 15:23:34 +0200139 if (!unit) {
140 retval = -ENOMEM;
141 goto out;
142 }
Christof Schmitt1daa4eb2010-09-08 14:39:52 +0200143
144 unit->port = port;
145 unit->fcp_lun = fcp_lun;
146 unit->dev.parent = &port->dev;
147 unit->dev.release = zfcp_unit_release;
Christof Schmitt1daa4eb2010-09-08 14:39:52 +0200148 INIT_WORK(&unit->scsi_work, zfcp_unit_scsi_scan_work);
Christof Schmitt1daa4eb2010-09-08 14:39:52 +0200149
150 if (dev_set_name(&unit->dev, "0x%016llx",
151 (unsigned long long) fcp_lun)) {
152 kfree(unit);
Steffen Maierd99b6012012-09-04 15:23:34 +0200153 retval = -ENOMEM;
154 goto out;
Christof Schmitt1daa4eb2010-09-08 14:39:52 +0200155 }
156
157 if (device_register(&unit->dev)) {
158 put_device(&unit->dev);
Steffen Maierd99b6012012-09-04 15:23:34 +0200159 retval = -ENOMEM;
160 goto out;
Christof Schmitt1daa4eb2010-09-08 14:39:52 +0200161 }
162
163 if (sysfs_create_group(&unit->dev.kobj, &zfcp_sysfs_unit_attrs)) {
164 device_unregister(&unit->dev);
Steffen Maierd99b6012012-09-04 15:23:34 +0200165 retval = -EINVAL;
166 goto out;
Christof Schmitt1daa4eb2010-09-08 14:39:52 +0200167 }
168
Steffen Maierd99b6012012-09-04 15:23:34 +0200169 atomic_inc(&port->units); /* under zfcp_sysfs_port_units_mutex ! */
170
Christof Schmitt1daa4eb2010-09-08 14:39:52 +0200171 write_lock_irq(&port->unit_list_lock);
172 list_add_tail(&unit->list, &port->unit_list);
173 write_unlock_irq(&port->unit_list_lock);
174
Christof Schmitt1daa4eb2010-09-08 14:39:52 +0200175 zfcp_unit_scsi_scan(unit);
176
Steffen Maierd99b6012012-09-04 15:23:34 +0200177out:
178 mutex_unlock(&zfcp_sysfs_port_units_mutex);
179 return retval;
Christof Schmitt1daa4eb2010-09-08 14:39:52 +0200180}
181
182/**
183 * zfcp_unit_sdev - Return SCSI device for zfcp_unit
184 * @unit: The zfcp_unit where to get the SCSI device for
185 *
186 * Returns: scsi_device pointer on success, NULL if there is no SCSI
187 * device for this zfcp_unit
188 *
189 * On success, the caller also holds a reference to the SCSI device
190 * that must be released with scsi_device_put.
191 */
192struct scsi_device *zfcp_unit_sdev(struct zfcp_unit *unit)
193{
194 struct Scsi_Host *shost;
195 struct zfcp_port *port;
196 unsigned int lun;
197
198 lun = scsilun_to_int((struct scsi_lun *) &unit->fcp_lun);
199 port = unit->port;
200 shost = port->adapter->scsi_host;
201 return scsi_device_lookup(shost, 0, port->starget_id, lun);
202}
203
204/**
205 * zfcp_unit_sdev_status - Return zfcp LUN status for SCSI device
206 * @unit: The unit to lookup the SCSI device for
207 *
208 * Returns the zfcp LUN status field of the SCSI device if the SCSI device
209 * for the zfcp_unit exists, 0 otherwise.
210 */
211unsigned int zfcp_unit_sdev_status(struct zfcp_unit *unit)
212{
213 unsigned int status = 0;
214 struct scsi_device *sdev;
215 struct zfcp_scsi_dev *zfcp_sdev;
216
217 sdev = zfcp_unit_sdev(unit);
218 if (sdev) {
219 zfcp_sdev = sdev_to_zfcp(sdev);
220 status = atomic_read(&zfcp_sdev->status);
221 scsi_device_put(sdev);
222 }
223
224 return status;
225}
226
227/**
228 * zfcp_unit_remove - Remove entry from list of configured units
229 * @port: The port where to remove the unit from the configuration
230 * @fcp_lun: The 64 bit LUN of the unit to remove
231 *
232 * Returns: -EINVAL if a unit with the specified LUN does not exist,
233 * 0 on success.
234 */
235int zfcp_unit_remove(struct zfcp_port *port, u64 fcp_lun)
236{
237 struct zfcp_unit *unit;
238 struct scsi_device *sdev;
239
240 write_lock_irq(&port->unit_list_lock);
241 unit = _zfcp_unit_find(port, fcp_lun);
242 if (unit)
243 list_del(&unit->list);
244 write_unlock_irq(&port->unit_list_lock);
245
246 if (!unit)
247 return -EINVAL;
248
249 sdev = zfcp_unit_sdev(unit);
250 if (sdev) {
251 scsi_remove_device(sdev);
252 scsi_device_put(sdev);
253 }
254
255 put_device(&unit->dev);
256
Christof Schmitt1daa4eb2010-09-08 14:39:52 +0200257 zfcp_device_unregister(&unit->dev, &zfcp_sysfs_unit_attrs);
258
259 return 0;
260}