zfcp_unit.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 Corporation 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. unsigned int 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. INIT_WORK(&unit->scsi_work, zfcp_unit_scsi_scan_work);
  129. if (dev_set_name(&unit->dev, "0x%016llx",
  130. (unsigned long long) fcp_lun)) {
  131. kfree(unit);
  132. retval = -ENOMEM;
  133. goto out;
  134. }
  135. if (device_register(&unit->dev)) {
  136. put_device(&unit->dev);
  137. retval = -ENOMEM;
  138. goto out;
  139. }
  140. if (sysfs_create_group(&unit->dev.kobj, &zfcp_sysfs_unit_attrs)) {
  141. device_unregister(&unit->dev);
  142. retval = -EINVAL;
  143. goto out;
  144. }
  145. atomic_inc(&port->units); /* under zfcp_sysfs_port_units_mutex ! */
  146. write_lock_irq(&port->unit_list_lock);
  147. list_add_tail(&unit->list, &port->unit_list);
  148. write_unlock_irq(&port->unit_list_lock);
  149. zfcp_unit_scsi_scan(unit);
  150. out:
  151. mutex_unlock(&zfcp_sysfs_port_units_mutex);
  152. return retval;
  153. }
  154. /**
  155. * zfcp_unit_sdev - Return SCSI device for zfcp_unit
  156. * @unit: The zfcp_unit where to get the SCSI device for
  157. *
  158. * Returns: scsi_device pointer on success, NULL if there is no SCSI
  159. * device for this zfcp_unit
  160. *
  161. * On success, the caller also holds a reference to the SCSI device
  162. * that must be released with scsi_device_put.
  163. */
  164. struct scsi_device *zfcp_unit_sdev(struct zfcp_unit *unit)
  165. {
  166. struct Scsi_Host *shost;
  167. struct zfcp_port *port;
  168. unsigned int lun;
  169. lun = scsilun_to_int((struct scsi_lun *) &unit->fcp_lun);
  170. port = unit->port;
  171. shost = port->adapter->scsi_host;
  172. return scsi_device_lookup(shost, 0, port->starget_id, lun);
  173. }
  174. /**
  175. * zfcp_unit_sdev_status - Return zfcp LUN status for SCSI device
  176. * @unit: The unit to lookup the SCSI device for
  177. *
  178. * Returns the zfcp LUN status field of the SCSI device if the SCSI device
  179. * for the zfcp_unit exists, 0 otherwise.
  180. */
  181. unsigned int zfcp_unit_sdev_status(struct zfcp_unit *unit)
  182. {
  183. unsigned int status = 0;
  184. struct scsi_device *sdev;
  185. struct zfcp_scsi_dev *zfcp_sdev;
  186. sdev = zfcp_unit_sdev(unit);
  187. if (sdev) {
  188. zfcp_sdev = sdev_to_zfcp(sdev);
  189. status = atomic_read(&zfcp_sdev->status);
  190. scsi_device_put(sdev);
  191. }
  192. return status;
  193. }
  194. /**
  195. * zfcp_unit_remove - Remove entry from list of configured units
  196. * @port: The port where to remove the unit from the configuration
  197. * @fcp_lun: The 64 bit LUN of the unit to remove
  198. *
  199. * Returns: -EINVAL if a unit with the specified LUN does not exist,
  200. * 0 on success.
  201. */
  202. int zfcp_unit_remove(struct zfcp_port *port, u64 fcp_lun)
  203. {
  204. struct zfcp_unit *unit;
  205. struct scsi_device *sdev;
  206. write_lock_irq(&port->unit_list_lock);
  207. unit = _zfcp_unit_find(port, fcp_lun);
  208. if (unit)
  209. list_del(&unit->list);
  210. write_unlock_irq(&port->unit_list_lock);
  211. if (!unit)
  212. return -EINVAL;
  213. sdev = zfcp_unit_sdev(unit);
  214. if (sdev) {
  215. scsi_remove_device(sdev);
  216. scsi_device_put(sdev);
  217. }
  218. put_device(&unit->dev);
  219. zfcp_device_unregister(&unit->dev, &zfcp_sysfs_unit_attrs);
  220. return 0;
  221. }