1 // SPDX-License-Identifier: GPL-2.0
5 * Tracking of manually configured LUNs and helper functions to
6 * register the LUNs with the SCSI midlayer.
8 * Copyright IBM Corp. 2010
15 * zfcp_unit_scsi_scan - Register LUN with SCSI midlayer
16 * @unit: The zfcp LUN/unit to register
18 * When the SCSI midlayer is not allowed to automatically scan and
19 * attach SCSI devices, zfcp has to register the single devices with
22 void zfcp_unit_scsi_scan(struct zfcp_unit
*unit
)
24 struct fc_rport
*rport
= unit
->port
->rport
;
27 lun
= scsilun_to_int((struct scsi_lun
*) &unit
->fcp_lun
);
29 if (rport
&& rport
->port_state
== FC_PORTSTATE_ONLINE
)
30 scsi_scan_target(&rport
->dev
, 0, rport
->scsi_target_id
, lun
,
34 static void zfcp_unit_scsi_scan_work(struct work_struct
*work
)
36 struct zfcp_unit
*unit
= container_of(work
, struct zfcp_unit
,
39 zfcp_unit_scsi_scan(unit
);
40 put_device(&unit
->dev
);
44 * zfcp_unit_queue_scsi_scan - Register configured units on port
45 * @port: The zfcp_port where to register units
47 * After opening a port, all units configured on this port have to be
48 * registered with the SCSI midlayer. This function should be called
49 * after calling fc_remote_port_add, so that the fc_rport is already
50 * ONLINE and the call to scsi_scan_target runs the same way as the
51 * call in the FC transport class.
53 void zfcp_unit_queue_scsi_scan(struct zfcp_port
*port
)
55 struct zfcp_unit
*unit
;
57 read_lock_irq(&port
->unit_list_lock
);
58 list_for_each_entry(unit
, &port
->unit_list
, list
) {
59 get_device(&unit
->dev
);
60 if (scsi_queue_work(port
->adapter
->scsi_host
,
61 &unit
->scsi_work
) <= 0)
62 put_device(&unit
->dev
);
64 read_unlock_irq(&port
->unit_list_lock
);
67 static struct zfcp_unit
*_zfcp_unit_find(struct zfcp_port
*port
, u64 fcp_lun
)
69 struct zfcp_unit
*unit
;
71 list_for_each_entry(unit
, &port
->unit_list
, list
)
72 if (unit
->fcp_lun
== fcp_lun
) {
73 get_device(&unit
->dev
);
81 * zfcp_unit_find - Find and return zfcp_unit with specified FCP LUN
82 * @port: zfcp_port where to look for the unit
83 * @fcp_lun: 64 Bit FCP LUN used to identify the zfcp_unit
85 * If zfcp_unit is found, a reference is acquired that has to be
88 * Returns: Pointer to the zfcp_unit, or NULL if there is no zfcp_unit
89 * with the specified FCP LUN.
91 struct zfcp_unit
*zfcp_unit_find(struct zfcp_port
*port
, u64 fcp_lun
)
93 struct zfcp_unit
*unit
;
95 read_lock_irq(&port
->unit_list_lock
);
96 unit
= _zfcp_unit_find(port
, fcp_lun
);
97 read_unlock_irq(&port
->unit_list_lock
);
102 * zfcp_unit_release - Drop reference to zfcp_port and free memory of zfcp_unit.
103 * @dev: pointer to device in zfcp_unit
105 static void zfcp_unit_release(struct device
*dev
)
107 struct zfcp_unit
*unit
= container_of(dev
, struct zfcp_unit
, dev
);
109 atomic_dec(&unit
->port
->units
);
114 * zfcp_unit_enqueue - enqueue unit to unit list of a port.
115 * @port: pointer to port where unit is added
116 * @fcp_lun: FCP LUN of unit to be enqueued
119 * Sets up some unit internal structures and creates sysfs entry.
121 int zfcp_unit_add(struct zfcp_port
*port
, u64 fcp_lun
)
123 struct zfcp_unit
*unit
;
126 mutex_lock(&zfcp_sysfs_port_units_mutex
);
127 if (atomic_read(&port
->units
) == -1) {
128 /* port is already gone */
133 unit
= zfcp_unit_find(port
, fcp_lun
);
135 put_device(&unit
->dev
);
140 unit
= kzalloc(sizeof(struct zfcp_unit
), GFP_KERNEL
);
147 unit
->fcp_lun
= fcp_lun
;
148 unit
->dev
.parent
= &port
->dev
;
149 unit
->dev
.release
= zfcp_unit_release
;
150 unit
->dev
.groups
= zfcp_unit_attr_groups
;
151 INIT_WORK(&unit
->scsi_work
, zfcp_unit_scsi_scan_work
);
153 if (dev_set_name(&unit
->dev
, "0x%016llx",
154 (unsigned long long) fcp_lun
)) {
160 if (device_register(&unit
->dev
)) {
161 put_device(&unit
->dev
);
166 atomic_inc(&port
->units
); /* under zfcp_sysfs_port_units_mutex ! */
168 write_lock_irq(&port
->unit_list_lock
);
169 list_add_tail(&unit
->list
, &port
->unit_list
);
170 write_unlock_irq(&port
->unit_list_lock
);
172 zfcp_unit_scsi_scan(unit
);
175 mutex_unlock(&zfcp_sysfs_port_units_mutex
);
180 * zfcp_unit_sdev - Return SCSI device for zfcp_unit
181 * @unit: The zfcp_unit where to get the SCSI device for
183 * Returns: scsi_device pointer on success, NULL if there is no SCSI
184 * device for this zfcp_unit
186 * On success, the caller also holds a reference to the SCSI device
187 * that must be released with scsi_device_put.
189 struct scsi_device
*zfcp_unit_sdev(struct zfcp_unit
*unit
)
191 struct Scsi_Host
*shost
;
192 struct zfcp_port
*port
;
195 lun
= scsilun_to_int((struct scsi_lun
*) &unit
->fcp_lun
);
197 shost
= port
->adapter
->scsi_host
;
198 return scsi_device_lookup(shost
, 0, port
->starget_id
, lun
);
202 * zfcp_unit_sdev_status - Return zfcp LUN status for SCSI device
203 * @unit: The unit to lookup the SCSI device for
205 * Returns the zfcp LUN status field of the SCSI device if the SCSI device
206 * for the zfcp_unit exists, 0 otherwise.
208 unsigned int zfcp_unit_sdev_status(struct zfcp_unit
*unit
)
210 unsigned int status
= 0;
211 struct scsi_device
*sdev
;
212 struct zfcp_scsi_dev
*zfcp_sdev
;
214 sdev
= zfcp_unit_sdev(unit
);
216 zfcp_sdev
= sdev_to_zfcp(sdev
);
217 status
= atomic_read(&zfcp_sdev
->status
);
218 scsi_device_put(sdev
);
225 * zfcp_unit_remove - Remove entry from list of configured units
226 * @port: The port where to remove the unit from the configuration
227 * @fcp_lun: The 64 bit LUN of the unit to remove
229 * Returns: -EINVAL if a unit with the specified LUN does not exist,
232 int zfcp_unit_remove(struct zfcp_port
*port
, u64 fcp_lun
)
234 struct zfcp_unit
*unit
;
235 struct scsi_device
*sdev
;
237 write_lock_irq(&port
->unit_list_lock
);
238 unit
= _zfcp_unit_find(port
, fcp_lun
);
240 list_del(&unit
->list
);
241 write_unlock_irq(&port
->unit_list_lock
);
246 sdev
= zfcp_unit_sdev(unit
);
248 scsi_remove_device(sdev
);
249 scsi_device_put(sdev
);
252 put_device(&unit
->dev
);
254 device_unregister(&unit
->dev
);