zfcp_unit.c 6.2 KB

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