zfcp_unit.c 6.3 KB

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