18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * zfcp device driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Tracking of manually configured LUNs and helper functions to
68c2ecf20Sopenharmony_ci * register the LUNs with the SCSI midlayer.
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Copyright IBM Corp. 2010
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include "zfcp_def.h"
128c2ecf20Sopenharmony_ci#include "zfcp_ext.h"
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci/**
158c2ecf20Sopenharmony_ci * zfcp_unit_scsi_scan - Register LUN with SCSI midlayer
168c2ecf20Sopenharmony_ci * @unit: The zfcp LUN/unit to register
178c2ecf20Sopenharmony_ci *
188c2ecf20Sopenharmony_ci * When the SCSI midlayer is not allowed to automatically scan and
198c2ecf20Sopenharmony_ci * attach SCSI devices, zfcp has to register the single devices with
208c2ecf20Sopenharmony_ci * the SCSI midlayer.
218c2ecf20Sopenharmony_ci */
228c2ecf20Sopenharmony_civoid zfcp_unit_scsi_scan(struct zfcp_unit *unit)
238c2ecf20Sopenharmony_ci{
248c2ecf20Sopenharmony_ci	struct fc_rport *rport = unit->port->rport;
258c2ecf20Sopenharmony_ci	u64 lun;
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci	lun = scsilun_to_int((struct scsi_lun *) &unit->fcp_lun);
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci	if (rport && rport->port_state == FC_PORTSTATE_ONLINE)
308c2ecf20Sopenharmony_ci		scsi_scan_target(&rport->dev, 0, rport->scsi_target_id, lun,
318c2ecf20Sopenharmony_ci				 SCSI_SCAN_MANUAL);
328c2ecf20Sopenharmony_ci}
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_cistatic void zfcp_unit_scsi_scan_work(struct work_struct *work)
358c2ecf20Sopenharmony_ci{
368c2ecf20Sopenharmony_ci	struct zfcp_unit *unit = container_of(work, struct zfcp_unit,
378c2ecf20Sopenharmony_ci					      scsi_work);
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci	zfcp_unit_scsi_scan(unit);
408c2ecf20Sopenharmony_ci	put_device(&unit->dev);
418c2ecf20Sopenharmony_ci}
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci/**
448c2ecf20Sopenharmony_ci * zfcp_unit_queue_scsi_scan - Register configured units on port
458c2ecf20Sopenharmony_ci * @port: The zfcp_port where to register units
468c2ecf20Sopenharmony_ci *
478c2ecf20Sopenharmony_ci * After opening a port, all units configured on this port have to be
488c2ecf20Sopenharmony_ci * registered with the SCSI midlayer. This function should be called
498c2ecf20Sopenharmony_ci * after calling fc_remote_port_add, so that the fc_rport is already
508c2ecf20Sopenharmony_ci * ONLINE and the call to scsi_scan_target runs the same way as the
518c2ecf20Sopenharmony_ci * call in the FC transport class.
528c2ecf20Sopenharmony_ci */
538c2ecf20Sopenharmony_civoid zfcp_unit_queue_scsi_scan(struct zfcp_port *port)
548c2ecf20Sopenharmony_ci{
558c2ecf20Sopenharmony_ci	struct zfcp_unit *unit;
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	read_lock_irq(&port->unit_list_lock);
588c2ecf20Sopenharmony_ci	list_for_each_entry(unit, &port->unit_list, list) {
598c2ecf20Sopenharmony_ci		get_device(&unit->dev);
608c2ecf20Sopenharmony_ci		if (scsi_queue_work(port->adapter->scsi_host,
618c2ecf20Sopenharmony_ci				    &unit->scsi_work) <= 0)
628c2ecf20Sopenharmony_ci			put_device(&unit->dev);
638c2ecf20Sopenharmony_ci	}
648c2ecf20Sopenharmony_ci	read_unlock_irq(&port->unit_list_lock);
658c2ecf20Sopenharmony_ci}
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_cistatic struct zfcp_unit *_zfcp_unit_find(struct zfcp_port *port, u64 fcp_lun)
688c2ecf20Sopenharmony_ci{
698c2ecf20Sopenharmony_ci	struct zfcp_unit *unit;
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	list_for_each_entry(unit, &port->unit_list, list)
728c2ecf20Sopenharmony_ci		if (unit->fcp_lun == fcp_lun) {
738c2ecf20Sopenharmony_ci			get_device(&unit->dev);
748c2ecf20Sopenharmony_ci			return unit;
758c2ecf20Sopenharmony_ci		}
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	return NULL;
788c2ecf20Sopenharmony_ci}
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci/**
818c2ecf20Sopenharmony_ci * zfcp_unit_find - Find and return zfcp_unit with specified FCP LUN
828c2ecf20Sopenharmony_ci * @port: zfcp_port where to look for the unit
838c2ecf20Sopenharmony_ci * @fcp_lun: 64 Bit FCP LUN used to identify the zfcp_unit
848c2ecf20Sopenharmony_ci *
858c2ecf20Sopenharmony_ci * If zfcp_unit is found, a reference is acquired that has to be
868c2ecf20Sopenharmony_ci * released later.
878c2ecf20Sopenharmony_ci *
888c2ecf20Sopenharmony_ci * Returns: Pointer to the zfcp_unit, or NULL if there is no zfcp_unit
898c2ecf20Sopenharmony_ci *          with the specified FCP LUN.
908c2ecf20Sopenharmony_ci */
918c2ecf20Sopenharmony_cistruct zfcp_unit *zfcp_unit_find(struct zfcp_port *port, u64 fcp_lun)
928c2ecf20Sopenharmony_ci{
938c2ecf20Sopenharmony_ci	struct zfcp_unit *unit;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	read_lock_irq(&port->unit_list_lock);
968c2ecf20Sopenharmony_ci	unit = _zfcp_unit_find(port, fcp_lun);
978c2ecf20Sopenharmony_ci	read_unlock_irq(&port->unit_list_lock);
988c2ecf20Sopenharmony_ci	return unit;
998c2ecf20Sopenharmony_ci}
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci/**
1028c2ecf20Sopenharmony_ci * zfcp_unit_release - Drop reference to zfcp_port and free memory of zfcp_unit.
1038c2ecf20Sopenharmony_ci * @dev: pointer to device in zfcp_unit
1048c2ecf20Sopenharmony_ci */
1058c2ecf20Sopenharmony_cistatic void zfcp_unit_release(struct device *dev)
1068c2ecf20Sopenharmony_ci{
1078c2ecf20Sopenharmony_ci	struct zfcp_unit *unit = container_of(dev, struct zfcp_unit, dev);
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	atomic_dec(&unit->port->units);
1108c2ecf20Sopenharmony_ci	kfree(unit);
1118c2ecf20Sopenharmony_ci}
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci/**
1148c2ecf20Sopenharmony_ci * zfcp_unit_enqueue - enqueue unit to unit list of a port.
1158c2ecf20Sopenharmony_ci * @port: pointer to port where unit is added
1168c2ecf20Sopenharmony_ci * @fcp_lun: FCP LUN of unit to be enqueued
1178c2ecf20Sopenharmony_ci * Returns: 0 success
1188c2ecf20Sopenharmony_ci *
1198c2ecf20Sopenharmony_ci * Sets up some unit internal structures and creates sysfs entry.
1208c2ecf20Sopenharmony_ci */
1218c2ecf20Sopenharmony_ciint zfcp_unit_add(struct zfcp_port *port, u64 fcp_lun)
1228c2ecf20Sopenharmony_ci{
1238c2ecf20Sopenharmony_ci	struct zfcp_unit *unit;
1248c2ecf20Sopenharmony_ci	int retval = 0;
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	mutex_lock(&zfcp_sysfs_port_units_mutex);
1278c2ecf20Sopenharmony_ci	if (zfcp_sysfs_port_is_removing(port)) {
1288c2ecf20Sopenharmony_ci		/* port is already gone */
1298c2ecf20Sopenharmony_ci		retval = -ENODEV;
1308c2ecf20Sopenharmony_ci		goto out;
1318c2ecf20Sopenharmony_ci	}
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	unit = zfcp_unit_find(port, fcp_lun);
1348c2ecf20Sopenharmony_ci	if (unit) {
1358c2ecf20Sopenharmony_ci		put_device(&unit->dev);
1368c2ecf20Sopenharmony_ci		retval = -EEXIST;
1378c2ecf20Sopenharmony_ci		goto out;
1388c2ecf20Sopenharmony_ci	}
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	unit = kzalloc(sizeof(struct zfcp_unit), GFP_KERNEL);
1418c2ecf20Sopenharmony_ci	if (!unit) {
1428c2ecf20Sopenharmony_ci		retval = -ENOMEM;
1438c2ecf20Sopenharmony_ci		goto out;
1448c2ecf20Sopenharmony_ci	}
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	unit->port = port;
1478c2ecf20Sopenharmony_ci	unit->fcp_lun = fcp_lun;
1488c2ecf20Sopenharmony_ci	unit->dev.parent = &port->dev;
1498c2ecf20Sopenharmony_ci	unit->dev.release = zfcp_unit_release;
1508c2ecf20Sopenharmony_ci	unit->dev.groups = zfcp_unit_attr_groups;
1518c2ecf20Sopenharmony_ci	INIT_WORK(&unit->scsi_work, zfcp_unit_scsi_scan_work);
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	if (dev_set_name(&unit->dev, "0x%016llx",
1548c2ecf20Sopenharmony_ci			 (unsigned long long) fcp_lun)) {
1558c2ecf20Sopenharmony_ci		kfree(unit);
1568c2ecf20Sopenharmony_ci		retval = -ENOMEM;
1578c2ecf20Sopenharmony_ci		goto out;
1588c2ecf20Sopenharmony_ci	}
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	if (device_register(&unit->dev)) {
1618c2ecf20Sopenharmony_ci		put_device(&unit->dev);
1628c2ecf20Sopenharmony_ci		retval = -ENOMEM;
1638c2ecf20Sopenharmony_ci		goto out;
1648c2ecf20Sopenharmony_ci	}
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	atomic_inc(&port->units); /* under zfcp_sysfs_port_units_mutex ! */
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	write_lock_irq(&port->unit_list_lock);
1698c2ecf20Sopenharmony_ci	list_add_tail(&unit->list, &port->unit_list);
1708c2ecf20Sopenharmony_ci	write_unlock_irq(&port->unit_list_lock);
1718c2ecf20Sopenharmony_ci	/*
1728c2ecf20Sopenharmony_ci	 * lock order: shost->scan_mutex before zfcp_sysfs_port_units_mutex
1738c2ecf20Sopenharmony_ci	 * due to      zfcp_unit_scsi_scan() => zfcp_scsi_slave_alloc()
1748c2ecf20Sopenharmony_ci	 */
1758c2ecf20Sopenharmony_ci	mutex_unlock(&zfcp_sysfs_port_units_mutex);
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	zfcp_unit_scsi_scan(unit);
1788c2ecf20Sopenharmony_ci	return retval;
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ciout:
1818c2ecf20Sopenharmony_ci	mutex_unlock(&zfcp_sysfs_port_units_mutex);
1828c2ecf20Sopenharmony_ci	return retval;
1838c2ecf20Sopenharmony_ci}
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci/**
1868c2ecf20Sopenharmony_ci * zfcp_unit_sdev - Return SCSI device for zfcp_unit
1878c2ecf20Sopenharmony_ci * @unit: The zfcp_unit where to get the SCSI device for
1888c2ecf20Sopenharmony_ci *
1898c2ecf20Sopenharmony_ci * Returns: scsi_device pointer on success, NULL if there is no SCSI
1908c2ecf20Sopenharmony_ci *          device for this zfcp_unit
1918c2ecf20Sopenharmony_ci *
1928c2ecf20Sopenharmony_ci * On success, the caller also holds a reference to the SCSI device
1938c2ecf20Sopenharmony_ci * that must be released with scsi_device_put.
1948c2ecf20Sopenharmony_ci */
1958c2ecf20Sopenharmony_cistruct scsi_device *zfcp_unit_sdev(struct zfcp_unit *unit)
1968c2ecf20Sopenharmony_ci{
1978c2ecf20Sopenharmony_ci	struct Scsi_Host *shost;
1988c2ecf20Sopenharmony_ci	struct zfcp_port *port;
1998c2ecf20Sopenharmony_ci	u64 lun;
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	lun = scsilun_to_int((struct scsi_lun *) &unit->fcp_lun);
2028c2ecf20Sopenharmony_ci	port = unit->port;
2038c2ecf20Sopenharmony_ci	shost = port->adapter->scsi_host;
2048c2ecf20Sopenharmony_ci	return scsi_device_lookup(shost, 0, port->starget_id, lun);
2058c2ecf20Sopenharmony_ci}
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci/**
2088c2ecf20Sopenharmony_ci * zfcp_unit_sdev_status - Return zfcp LUN status for SCSI device
2098c2ecf20Sopenharmony_ci * @unit: The unit to lookup the SCSI device for
2108c2ecf20Sopenharmony_ci *
2118c2ecf20Sopenharmony_ci * Returns the zfcp LUN status field of the SCSI device if the SCSI device
2128c2ecf20Sopenharmony_ci * for the zfcp_unit exists, 0 otherwise.
2138c2ecf20Sopenharmony_ci */
2148c2ecf20Sopenharmony_ciunsigned int zfcp_unit_sdev_status(struct zfcp_unit *unit)
2158c2ecf20Sopenharmony_ci{
2168c2ecf20Sopenharmony_ci	unsigned int status = 0;
2178c2ecf20Sopenharmony_ci	struct scsi_device *sdev;
2188c2ecf20Sopenharmony_ci	struct zfcp_scsi_dev *zfcp_sdev;
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	sdev = zfcp_unit_sdev(unit);
2218c2ecf20Sopenharmony_ci	if (sdev) {
2228c2ecf20Sopenharmony_ci		zfcp_sdev = sdev_to_zfcp(sdev);
2238c2ecf20Sopenharmony_ci		status = atomic_read(&zfcp_sdev->status);
2248c2ecf20Sopenharmony_ci		scsi_device_put(sdev);
2258c2ecf20Sopenharmony_ci	}
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci	return status;
2288c2ecf20Sopenharmony_ci}
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci/**
2318c2ecf20Sopenharmony_ci * zfcp_unit_remove - Remove entry from list of configured units
2328c2ecf20Sopenharmony_ci * @port: The port where to remove the unit from the configuration
2338c2ecf20Sopenharmony_ci * @fcp_lun: The 64 bit LUN of the unit to remove
2348c2ecf20Sopenharmony_ci *
2358c2ecf20Sopenharmony_ci * Returns: -EINVAL if a unit with the specified LUN does not exist,
2368c2ecf20Sopenharmony_ci *          0 on success.
2378c2ecf20Sopenharmony_ci */
2388c2ecf20Sopenharmony_ciint zfcp_unit_remove(struct zfcp_port *port, u64 fcp_lun)
2398c2ecf20Sopenharmony_ci{
2408c2ecf20Sopenharmony_ci	struct zfcp_unit *unit;
2418c2ecf20Sopenharmony_ci	struct scsi_device *sdev;
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	write_lock_irq(&port->unit_list_lock);
2448c2ecf20Sopenharmony_ci	unit = _zfcp_unit_find(port, fcp_lun);
2458c2ecf20Sopenharmony_ci	if (unit)
2468c2ecf20Sopenharmony_ci		list_del(&unit->list);
2478c2ecf20Sopenharmony_ci	write_unlock_irq(&port->unit_list_lock);
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci	if (!unit)
2508c2ecf20Sopenharmony_ci		return -EINVAL;
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci	sdev = zfcp_unit_sdev(unit);
2538c2ecf20Sopenharmony_ci	if (sdev) {
2548c2ecf20Sopenharmony_ci		scsi_remove_device(sdev);
2558c2ecf20Sopenharmony_ci		scsi_device_put(sdev);
2568c2ecf20Sopenharmony_ci	}
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci	put_device(&unit->dev);
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci	device_unregister(&unit->dev);
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci	return 0;
2638c2ecf20Sopenharmony_ci}
264