zfcp_unit.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * zfcp device driver
  4. *
  5. * Tracking of manually configured LUNs and helper functions to
  6. * register the LUNs with the SCSI midlayer.
  7. *
  8. * Copyright IBM Corp. 2010
  9. */
  10. #include "zfcp_def.h"
  11. #include "zfcp_ext.h"
  12. /**
  13. * zfcp_unit_scsi_scan - Register LUN with SCSI midlayer
  14. * @unit: The zfcp LUN/unit to register
  15. *
  16. * When the SCSI midlayer is not allowed to automatically scan and
  17. * attach SCSI devices, zfcp has to register the single devices with
  18. * the SCSI midlayer.
  19. */
  20. void zfcp_unit_scsi_scan(struct zfcp_unit *unit)
  21. {
  22. struct fc_rport *rport = unit->port->rport;
  23. u64 lun;
  24. lun = scsilun_to_int((struct scsi_lun *) &unit->fcp_lun);
  25. if (rport && rport->port_state == FC_PORTSTATE_ONLINE)
  26. scsi_scan_target(&rport->dev, 0, rport->scsi_target_id, lun,
  27. SCSI_SCAN_MANUAL);
  28. }
  29. static void zfcp_unit_scsi_scan_work(struct work_struct *work)
  30. {
  31. struct zfcp_unit *unit = container_of(work, struct zfcp_unit,
  32. scsi_work);
  33. zfcp_unit_scsi_scan(unit);
  34. put_device(&unit->dev);
  35. }
  36. /**
  37. * zfcp_unit_queue_scsi_scan - Register configured units on port
  38. * @port: The zfcp_port where to register units
  39. *
  40. * After opening a port, all units configured on this port have to be
  41. * registered with the SCSI midlayer. This function should be called
  42. * after calling fc_remote_port_add, so that the fc_rport is already
  43. * ONLINE and the call to scsi_scan_target runs the same way as the
  44. * call in the FC transport class.
  45. */
  46. void zfcp_unit_queue_scsi_scan(struct zfcp_port *port)
  47. {
  48. struct zfcp_unit *unit;
  49. read_lock_irq(&port->unit_list_lock);
  50. list_for_each_entry(unit, &port->unit_list, list) {
  51. get_device(&unit->dev);
  52. if (scsi_queue_work(port->adapter->scsi_host,
  53. &unit->scsi_work) <= 0)
  54. put_device(&unit->dev);
  55. }
  56. read_unlock_irq(&port->unit_list_lock);
  57. }
  58. static struct zfcp_unit *_zfcp_unit_find(struct zfcp_port *port, u64 fcp_lun)
  59. {
  60. struct zfcp_unit *unit;
  61. list_for_each_entry(unit, &port->unit_list, list)
  62. if (unit->fcp_lun == fcp_lun) {
  63. get_device(&unit->dev);
  64. return unit;
  65. }
  66. return NULL;
  67. }
  68. /**
  69. * zfcp_unit_find - Find and return zfcp_unit with specified FCP LUN
  70. * @port: zfcp_port where to look for the unit
  71. * @fcp_lun: 64 Bit FCP LUN used to identify the zfcp_unit
  72. *
  73. * If zfcp_unit is found, a reference is acquired that has to be
  74. * released later.
  75. *
  76. * Returns: Pointer to the zfcp_unit, or NULL if there is no zfcp_unit
  77. * with the specified FCP LUN.
  78. */
  79. struct zfcp_unit *zfcp_unit_find(struct zfcp_port *port, u64 fcp_lun)
  80. {
  81. struct zfcp_unit *unit;
  82. read_lock_irq(&port->unit_list_lock);
  83. unit = _zfcp_unit_find(port, fcp_lun);
  84. read_unlock_irq(&port->unit_list_lock);
  85. return unit;
  86. }
  87. /**
  88. * zfcp_unit_release - Drop reference to zfcp_port and free memory of zfcp_unit.
  89. * @dev: pointer to device in zfcp_unit
  90. */
  91. static void zfcp_unit_release(struct device *dev)
  92. {
  93. struct zfcp_unit *unit = container_of(dev, struct zfcp_unit, dev);
  94. atomic_dec(&unit->port->units);
  95. kfree(unit);
  96. }
  97. /**
  98. * zfcp_unit_enqueue - enqueue unit to unit list of a port.
  99. * @port: pointer to port where unit is added
  100. * @fcp_lun: FCP LUN of unit to be enqueued
  101. * Returns: 0 success
  102. *
  103. * Sets up some unit internal structures and creates sysfs entry.
  104. */
  105. int zfcp_unit_add(struct zfcp_port *port, u64 fcp_lun)
  106. {
  107. struct zfcp_unit *unit;
  108. int retval = 0;
  109. mutex_lock(&zfcp_sysfs_port_units_mutex);
  110. if (zfcp_sysfs_port_is_removing(port)) {
  111. /* port is already gone */
  112. retval = -ENODEV;
  113. goto out;
  114. }
  115. unit = zfcp_unit_find(port, fcp_lun);
  116. if (unit) {
  117. put_device(&unit->dev);
  118. retval = -EEXIST;
  119. goto out;
  120. }
  121. unit = kzalloc(sizeof(struct zfcp_unit), GFP_KERNEL);
  122. if (!unit) {
  123. retval = -ENOMEM;
  124. goto out;
  125. }
  126. unit->port = port;
  127. unit->fcp_lun = fcp_lun;
  128. unit->dev.parent = &port->dev;
  129. unit->dev.release = zfcp_unit_release;
  130. unit->dev.groups = zfcp_unit_attr_groups;
  131. INIT_WORK(&unit->scsi_work, zfcp_unit_scsi_scan_work);
  132. if (dev_set_name(&unit->dev, "0x%016llx",
  133. (unsigned long long) fcp_lun)) {
  134. kfree(unit);
  135. retval = -ENOMEM;
  136. goto out;
  137. }
  138. if (device_register(&unit->dev)) {
  139. put_device(&unit->dev);
  140. retval = -ENOMEM;
  141. goto out;
  142. }
  143. atomic_inc(&port->units); /* under zfcp_sysfs_port_units_mutex ! */
  144. write_lock_irq(&port->unit_list_lock);
  145. list_add_tail(&unit->list, &port->unit_list);
  146. write_unlock_irq(&port->unit_list_lock);
  147. /*
  148. * lock order: shost->scan_mutex before zfcp_sysfs_port_units_mutex
  149. * due to zfcp_unit_scsi_scan() => zfcp_scsi_slave_alloc()
  150. */
  151. mutex_unlock(&zfcp_sysfs_port_units_mutex);
  152. zfcp_unit_scsi_scan(unit);
  153. return retval;
  154. out:
  155. mutex_unlock(&zfcp_sysfs_port_units_mutex);
  156. return retval;
  157. }
  158. /**
  159. * zfcp_unit_sdev - Return SCSI device for zfcp_unit
  160. * @unit: The zfcp_unit where to get the SCSI device for
  161. *
  162. * Returns: scsi_device pointer on success, NULL if there is no SCSI
  163. * device for this zfcp_unit
  164. *
  165. * On success, the caller also holds a reference to the SCSI device
  166. * that must be released with scsi_device_put.
  167. */
  168. struct scsi_device *zfcp_unit_sdev(struct zfcp_unit *unit)
  169. {
  170. struct Scsi_Host *shost;
  171. struct zfcp_port *port;
  172. u64 lun;
  173. lun = scsilun_to_int((struct scsi_lun *) &unit->fcp_lun);
  174. port = unit->port;
  175. shost = port->adapter->scsi_host;
  176. return scsi_device_lookup(shost, 0, port->starget_id, lun);
  177. }
  178. /**
  179. * zfcp_unit_sdev_status - Return zfcp LUN status for SCSI device
  180. * @unit: The unit to lookup the SCSI device for
  181. *
  182. * Returns the zfcp LUN status field of the SCSI device if the SCSI device
  183. * for the zfcp_unit exists, 0 otherwise.
  184. */
  185. unsigned int zfcp_unit_sdev_status(struct zfcp_unit *unit)
  186. {
  187. unsigned int status = 0;
  188. struct scsi_device *sdev;
  189. struct zfcp_scsi_dev *zfcp_sdev;
  190. sdev = zfcp_unit_sdev(unit);
  191. if (sdev) {
  192. zfcp_sdev = sdev_to_zfcp(sdev);
  193. status = atomic_read(&zfcp_sdev->status);
  194. scsi_device_put(sdev);
  195. }
  196. return status;
  197. }
  198. /**
  199. * zfcp_unit_remove - Remove entry from list of configured units
  200. * @port: The port where to remove the unit from the configuration
  201. * @fcp_lun: The 64 bit LUN of the unit to remove
  202. *
  203. * Returns: -EINVAL if a unit with the specified LUN does not exist,
  204. * 0 on success.
  205. */
  206. int zfcp_unit_remove(struct zfcp_port *port, u64 fcp_lun)
  207. {
  208. struct zfcp_unit *unit;
  209. struct scsi_device *sdev;
  210. write_lock_irq(&port->unit_list_lock);
  211. unit = _zfcp_unit_find(port, fcp_lun);
  212. if (unit)
  213. list_del(&unit->list);
  214. write_unlock_irq(&port->unit_list_lock);
  215. if (!unit)
  216. return -EINVAL;
  217. sdev = zfcp_unit_sdev(unit);
  218. if (sdev) {
  219. scsi_remove_device(sdev);
  220. scsi_device_put(sdev);
  221. }
  222. put_device(&unit->dev);
  223. device_unregister(&unit->dev);
  224. return 0;
  225. }