Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
zfcp_unit.c
Go to the documentation of this file.
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 
10 #include "zfcp_def.h"
11 #include "zfcp_ext.h"
12 
22 {
23  struct fc_rport *rport = unit->port->rport;
24  unsigned int lun;
25 
26  lun = scsilun_to_int((struct scsi_lun *) &unit->fcp_lun);
27 
28  if (rport && rport->port_state == FC_PORTSTATE_ONLINE)
29  scsi_scan_target(&rport->dev, 0, rport->scsi_target_id, lun, 1);
30 }
31 
32 static void zfcp_unit_scsi_scan_work(struct work_struct *work)
33 {
34  struct zfcp_unit *unit = container_of(work, struct zfcp_unit,
35  scsi_work);
36 
37  zfcp_unit_scsi_scan(unit);
38  put_device(&unit->dev);
39 }
40 
52 {
53  struct zfcp_unit *unit;
54 
56  list_for_each_entry(unit, &port->unit_list, list) {
57  get_device(&unit->dev);
58  if (scsi_queue_work(port->adapter->scsi_host,
59  &unit->scsi_work) <= 0)
60  put_device(&unit->dev);
61  }
63 }
64 
65 static struct zfcp_unit *_zfcp_unit_find(struct zfcp_port *port, u64 fcp_lun)
66 {
67  struct zfcp_unit *unit;
68 
69  list_for_each_entry(unit, &port->unit_list, list)
70  if (unit->fcp_lun == fcp_lun) {
71  get_device(&unit->dev);
72  return unit;
73  }
74 
75  return NULL;
76 }
77 
90 {
91  struct zfcp_unit *unit;
92 
94  unit = _zfcp_unit_find(port, fcp_lun);
96  return unit;
97 }
98 
103 static void zfcp_unit_release(struct device *dev)
104 {
105  struct zfcp_unit *unit = container_of(dev, struct zfcp_unit, dev);
106 
107  atomic_dec(&unit->port->units);
108  kfree(unit);
109 }
110 
120 {
121  struct zfcp_unit *unit;
122  int retval = 0;
123 
125  if (atomic_read(&port->units) == -1) {
126  /* port is already gone */
127  retval = -ENODEV;
128  goto out;
129  }
130 
131  unit = zfcp_unit_find(port, fcp_lun);
132  if (unit) {
133  put_device(&unit->dev);
134  retval = -EEXIST;
135  goto out;
136  }
137 
138  unit = kzalloc(sizeof(struct zfcp_unit), GFP_KERNEL);
139  if (!unit) {
140  retval = -ENOMEM;
141  goto out;
142  }
143 
144  unit->port = port;
145  unit->fcp_lun = fcp_lun;
146  unit->dev.parent = &port->dev;
147  unit->dev.release = zfcp_unit_release;
148  INIT_WORK(&unit->scsi_work, zfcp_unit_scsi_scan_work);
149 
150  if (dev_set_name(&unit->dev, "0x%016llx",
151  (unsigned long long) fcp_lun)) {
152  kfree(unit);
153  retval = -ENOMEM;
154  goto out;
155  }
156 
157  if (device_register(&unit->dev)) {
158  put_device(&unit->dev);
159  retval = -ENOMEM;
160  goto out;
161  }
162 
163  if (sysfs_create_group(&unit->dev.kobj, &zfcp_sysfs_unit_attrs)) {
164  device_unregister(&unit->dev);
165  retval = -EINVAL;
166  goto out;
167  }
168 
169  atomic_inc(&port->units); /* under zfcp_sysfs_port_units_mutex ! */
170 
172  list_add_tail(&unit->list, &port->unit_list);
174 
175  zfcp_unit_scsi_scan(unit);
176 
177 out:
179  return retval;
180 }
181 
193 {
194  struct Scsi_Host *shost;
195  struct zfcp_port *port;
196  unsigned int lun;
197 
198  lun = scsilun_to_int((struct scsi_lun *) &unit->fcp_lun);
199  port = unit->port;
200  shost = port->adapter->scsi_host;
201  return scsi_device_lookup(shost, 0, port->starget_id, lun);
202 }
203 
211 unsigned int zfcp_unit_sdev_status(struct zfcp_unit *unit)
212 {
213  unsigned int status = 0;
214  struct scsi_device *sdev;
215  struct zfcp_scsi_dev *zfcp_sdev;
216 
217  sdev = zfcp_unit_sdev(unit);
218  if (sdev) {
219  zfcp_sdev = sdev_to_zfcp(sdev);
220  status = atomic_read(&zfcp_sdev->status);
221  scsi_device_put(sdev);
222  }
223 
224  return status;
225 }
226 
236 {
237  struct zfcp_unit *unit;
238  struct scsi_device *sdev;
239 
241  unit = _zfcp_unit_find(port, fcp_lun);
242  if (unit)
243  list_del(&unit->list);
245 
246  if (!unit)
247  return -EINVAL;
248 
249  sdev = zfcp_unit_sdev(unit);
250  if (sdev) {
251  scsi_remove_device(sdev);
252  scsi_device_put(sdev);
253  }
254 
255  put_device(&unit->dev);
256 
258 
259  return 0;
260 }