[SCSI] zfcp: No ERP escalation on gpn_ft eval
[linux-2.6.git] / drivers / s390 / scsi / zfcp_cfdc.c
1 /*
2  * zfcp device driver
3  *
4  * Userspace interface for accessing the
5  * Access Control Lists / Control File Data Channel;
6  * handling of response code and states for ports and LUNs.
7  *
8  * Copyright IBM Corporation 2008, 2010
9  */
10
11 #define KMSG_COMPONENT "zfcp"
12 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13
14 #include <linux/slab.h>
15 #include <linux/types.h>
16 #include <linux/miscdevice.h>
17 #include <asm/compat.h>
18 #include <asm/ccwdev.h>
19 #include "zfcp_def.h"
20 #include "zfcp_ext.h"
21 #include "zfcp_fsf.h"
22
23 #define ZFCP_CFDC_CMND_DOWNLOAD_NORMAL          0x00010001
24 #define ZFCP_CFDC_CMND_DOWNLOAD_FORCE           0x00010101
25 #define ZFCP_CFDC_CMND_FULL_ACCESS              0x00000201
26 #define ZFCP_CFDC_CMND_RESTRICTED_ACCESS        0x00000401
27 #define ZFCP_CFDC_CMND_UPLOAD                   0x00010002
28
29 #define ZFCP_CFDC_DOWNLOAD                      0x00000001
30 #define ZFCP_CFDC_UPLOAD                        0x00000002
31 #define ZFCP_CFDC_WITH_CONTROL_FILE             0x00010000
32
33 #define ZFCP_CFDC_IOC_MAGIC                     0xDD
34 #define ZFCP_CFDC_IOC \
35         _IOWR(ZFCP_CFDC_IOC_MAGIC, 0, struct zfcp_cfdc_data)
36
37 /**
38  * struct zfcp_cfdc_data - data for ioctl cfdc interface
39  * @signature: request signature
40  * @devno: FCP adapter device number
41  * @command: command code
42  * @fsf_status: returns status of FSF command to userspace
43  * @fsf_status_qual: returned to userspace
44  * @payloads: access conflicts list
45  * @control_file: access control table
46  */
47 struct zfcp_cfdc_data {
48         u32 signature;
49         u32 devno;
50         u32 command;
51         u32 fsf_status;
52         u8  fsf_status_qual[FSF_STATUS_QUALIFIER_SIZE];
53         u8  payloads[256];
54         u8  control_file[0];
55 };
56
57 static int zfcp_cfdc_copy_from_user(struct scatterlist *sg,
58                                     void __user *user_buffer)
59 {
60         unsigned int length;
61         unsigned int size = ZFCP_CFDC_MAX_SIZE;
62
63         while (size) {
64                 length = min((unsigned int)size, sg->length);
65                 if (copy_from_user(sg_virt(sg++), user_buffer, length))
66                         return -EFAULT;
67                 user_buffer += length;
68                 size -= length;
69         }
70         return 0;
71 }
72
73 static int zfcp_cfdc_copy_to_user(void __user  *user_buffer,
74                                   struct scatterlist *sg)
75 {
76         unsigned int length;
77         unsigned int size = ZFCP_CFDC_MAX_SIZE;
78
79         while (size) {
80                 length = min((unsigned int) size, sg->length);
81                 if (copy_to_user(user_buffer, sg_virt(sg++), length))
82                         return -EFAULT;
83                 user_buffer += length;
84                 size -= length;
85         }
86         return 0;
87 }
88
89 static struct zfcp_adapter *zfcp_cfdc_get_adapter(u32 devno)
90 {
91         char busid[9];
92         struct ccw_device *cdev;
93         struct zfcp_adapter *adapter;
94
95         snprintf(busid, sizeof(busid), "0.0.%04x", devno);
96         cdev = get_ccwdev_by_busid(&zfcp_ccw_driver, busid);
97         if (!cdev)
98                 return NULL;
99
100         adapter = zfcp_ccw_adapter_by_cdev(cdev);
101
102         put_device(&cdev->dev);
103         return adapter;
104 }
105
106 static int zfcp_cfdc_set_fsf(struct zfcp_fsf_cfdc *fsf_cfdc, int command)
107 {
108         switch (command) {
109         case ZFCP_CFDC_CMND_DOWNLOAD_NORMAL:
110                 fsf_cfdc->command = FSF_QTCB_DOWNLOAD_CONTROL_FILE;
111                 fsf_cfdc->option = FSF_CFDC_OPTION_NORMAL_MODE;
112                 break;
113         case ZFCP_CFDC_CMND_DOWNLOAD_FORCE:
114                 fsf_cfdc->command = FSF_QTCB_DOWNLOAD_CONTROL_FILE;
115                 fsf_cfdc->option = FSF_CFDC_OPTION_FORCE;
116                 break;
117         case ZFCP_CFDC_CMND_FULL_ACCESS:
118                 fsf_cfdc->command = FSF_QTCB_DOWNLOAD_CONTROL_FILE;
119                 fsf_cfdc->option = FSF_CFDC_OPTION_FULL_ACCESS;
120                 break;
121         case ZFCP_CFDC_CMND_RESTRICTED_ACCESS:
122                 fsf_cfdc->command = FSF_QTCB_DOWNLOAD_CONTROL_FILE;
123                 fsf_cfdc->option = FSF_CFDC_OPTION_RESTRICTED_ACCESS;
124                 break;
125         case ZFCP_CFDC_CMND_UPLOAD:
126                 fsf_cfdc->command = FSF_QTCB_UPLOAD_CONTROL_FILE;
127                 fsf_cfdc->option = 0;
128                 break;
129         default:
130                 return -EINVAL;
131         }
132
133         return 0;
134 }
135
136 static int zfcp_cfdc_sg_setup(int command, struct scatterlist *sg,
137                               u8 __user *control_file)
138 {
139         int retval;
140         retval = zfcp_sg_setup_table(sg, ZFCP_CFDC_PAGES);
141         if (retval)
142                 return retval;
143
144         sg[ZFCP_CFDC_PAGES - 1].length = ZFCP_CFDC_MAX_SIZE % PAGE_SIZE;
145
146         if (command & ZFCP_CFDC_WITH_CONTROL_FILE &&
147             command & ZFCP_CFDC_DOWNLOAD) {
148                 retval = zfcp_cfdc_copy_from_user(sg, control_file);
149                 if (retval) {
150                         zfcp_sg_free_table(sg, ZFCP_CFDC_PAGES);
151                         return -EFAULT;
152                 }
153         }
154
155         return 0;
156 }
157
158 static void zfcp_cfdc_req_to_sense(struct zfcp_cfdc_data *data,
159                                    struct zfcp_fsf_req *req)
160 {
161         data->fsf_status = req->qtcb->header.fsf_status;
162         memcpy(&data->fsf_status_qual, &req->qtcb->header.fsf_status_qual,
163                sizeof(union fsf_status_qual));
164         memcpy(&data->payloads, &req->qtcb->bottom.support.els,
165                sizeof(req->qtcb->bottom.support.els));
166 }
167
168 static long zfcp_cfdc_dev_ioctl(struct file *file, unsigned int command,
169                                 unsigned long arg)
170 {
171         struct zfcp_cfdc_data *data;
172         struct zfcp_cfdc_data __user *data_user;
173         struct zfcp_adapter *adapter;
174         struct zfcp_fsf_req *req;
175         struct zfcp_fsf_cfdc *fsf_cfdc;
176         int retval;
177
178         if (command != ZFCP_CFDC_IOC)
179                 return -ENOTTY;
180
181         if (is_compat_task())
182                 data_user = compat_ptr(arg);
183         else
184                 data_user = (void __user *)arg;
185
186         if (!data_user)
187                 return -EINVAL;
188
189         fsf_cfdc = kmalloc(sizeof(struct zfcp_fsf_cfdc), GFP_KERNEL);
190         if (!fsf_cfdc)
191                 return -ENOMEM;
192
193         data = memdup_user(data_user, sizeof(*data_user));
194         if (IS_ERR(data)) {
195                 retval = PTR_ERR(data);
196                 goto no_mem_sense;
197         }
198
199         if (data->signature != 0xCFDCACDF) {
200                 retval = -EINVAL;
201                 goto free_buffer;
202         }
203
204         retval = zfcp_cfdc_set_fsf(fsf_cfdc, data->command);
205
206         adapter = zfcp_cfdc_get_adapter(data->devno);
207         if (!adapter) {
208                 retval = -ENXIO;
209                 goto free_buffer;
210         }
211
212         retval = zfcp_cfdc_sg_setup(data->command, fsf_cfdc->sg,
213                                     data_user->control_file);
214         if (retval)
215                 goto adapter_put;
216         req = zfcp_fsf_control_file(adapter, fsf_cfdc);
217         if (IS_ERR(req)) {
218                 retval = PTR_ERR(req);
219                 goto free_sg;
220         }
221
222         if (req->status & ZFCP_STATUS_FSFREQ_ERROR) {
223                 retval = -ENXIO;
224                 goto free_fsf;
225         }
226
227         zfcp_cfdc_req_to_sense(data, req);
228         retval = copy_to_user(data_user, data, sizeof(*data_user));
229         if (retval) {
230                 retval = -EFAULT;
231                 goto free_fsf;
232         }
233
234         if (data->command & ZFCP_CFDC_UPLOAD)
235                 retval = zfcp_cfdc_copy_to_user(&data_user->control_file,
236                                                 fsf_cfdc->sg);
237
238  free_fsf:
239         zfcp_fsf_req_free(req);
240  free_sg:
241         zfcp_sg_free_table(fsf_cfdc->sg, ZFCP_CFDC_PAGES);
242  adapter_put:
243         zfcp_ccw_adapter_put(adapter);
244  free_buffer:
245         kfree(data);
246  no_mem_sense:
247         kfree(fsf_cfdc);
248         return retval;
249 }
250
251 static const struct file_operations zfcp_cfdc_fops = {
252         .open = nonseekable_open,
253         .unlocked_ioctl = zfcp_cfdc_dev_ioctl,
254 #ifdef CONFIG_COMPAT
255         .compat_ioctl = zfcp_cfdc_dev_ioctl,
256 #endif
257         .llseek = no_llseek,
258 };
259
260 struct miscdevice zfcp_cfdc_misc = {
261         .minor = MISC_DYNAMIC_MINOR,
262         .name = "zfcp_cfdc",
263         .fops = &zfcp_cfdc_fops,
264 };
265
266 /**
267  * zfcp_cfdc_adapter_access_changed - Process change in adapter ACT
268  * @adapter: Adapter where the Access Control Table (ACT) changed
269  *
270  * After a change in the adapter ACT, check if access to any
271  * previously denied resources is now possible.
272  */
273 void zfcp_cfdc_adapter_access_changed(struct zfcp_adapter *adapter)
274 {
275         unsigned long flags;
276         struct zfcp_port *port;
277         struct scsi_device *sdev;
278         struct zfcp_scsi_dev *zfcp_sdev;
279         int status;
280
281         if (adapter->connection_features & FSF_FEATURE_NPIV_MODE)
282                 return;
283
284         read_lock_irqsave(&adapter->port_list_lock, flags);
285         list_for_each_entry(port, &adapter->port_list, list) {
286                 status = atomic_read(&port->status);
287                 if ((status & ZFCP_STATUS_COMMON_ACCESS_DENIED) ||
288                     (status & ZFCP_STATUS_COMMON_ACCESS_BOXED))
289                         zfcp_erp_port_reopen(port,
290                                              ZFCP_STATUS_COMMON_ERP_FAILED,
291                                              "cfaac_1", NULL);
292         }
293         read_unlock_irqrestore(&adapter->port_list_lock, flags);
294
295         shost_for_each_device(sdev, port->adapter->scsi_host) {
296                 zfcp_sdev = sdev_to_zfcp(sdev);
297                 status = atomic_read(&zfcp_sdev->status);
298                 if ((status & ZFCP_STATUS_COMMON_ACCESS_DENIED) ||
299                     (status & ZFCP_STATUS_COMMON_ACCESS_BOXED))
300                         zfcp_erp_lun_reopen(sdev,
301                                             ZFCP_STATUS_COMMON_ERP_FAILED,
302                                             "cfaac_2", NULL);
303         }
304 }
305
306 static void zfcp_act_eval_err(struct zfcp_adapter *adapter, u32 table)
307 {
308         u16 subtable = table >> 16;
309         u16 rule = table & 0xffff;
310         const char *act_type[] = { "unknown", "OS", "WWPN", "DID", "LUN" };
311
312         if (subtable && subtable < ARRAY_SIZE(act_type))
313                 dev_warn(&adapter->ccw_device->dev,
314                          "Access denied according to ACT rule type %s, "
315                          "rule %d\n", act_type[subtable], rule);
316 }
317
318 /**
319  * zfcp_cfdc_port_denied - Process "access denied" for port
320  * @port: The port where the acces has been denied
321  * @qual: The FSF status qualifier for the access denied FSF status
322  */
323 void zfcp_cfdc_port_denied(struct zfcp_port *port,
324                            union fsf_status_qual *qual)
325 {
326         dev_warn(&port->adapter->ccw_device->dev,
327                  "Access denied to port 0x%016Lx\n",
328                  (unsigned long long)port->wwpn);
329
330         zfcp_act_eval_err(port->adapter, qual->halfword[0]);
331         zfcp_act_eval_err(port->adapter, qual->halfword[1]);
332         zfcp_erp_set_port_status(port,
333                                  ZFCP_STATUS_COMMON_ERP_FAILED |
334                                  ZFCP_STATUS_COMMON_ACCESS_DENIED);
335 }
336
337 /**
338  * zfcp_cfdc_lun_denied - Process "access denied" for LUN
339  * @sdev: The SCSI device / LUN where the access has been denied
340  * @qual: The FSF status qualifier for the access denied FSF status
341  */
342 void zfcp_cfdc_lun_denied(struct scsi_device *sdev,
343                           union fsf_status_qual *qual)
344 {
345         struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
346
347         dev_warn(&zfcp_sdev->port->adapter->ccw_device->dev,
348                  "Access denied to LUN 0x%016Lx on port 0x%016Lx\n",
349                  zfcp_scsi_dev_lun(sdev),
350                  (unsigned long long)zfcp_sdev->port->wwpn);
351         zfcp_act_eval_err(zfcp_sdev->port->adapter, qual->halfword[0]);
352         zfcp_act_eval_err(zfcp_sdev->port->adapter, qual->halfword[1]);
353         zfcp_erp_set_lun_status(sdev,
354                                 ZFCP_STATUS_COMMON_ERP_FAILED |
355                                 ZFCP_STATUS_COMMON_ACCESS_DENIED);
356
357         atomic_clear_mask(ZFCP_STATUS_LUN_SHARED, &zfcp_sdev->status);
358         atomic_clear_mask(ZFCP_STATUS_LUN_READONLY, &zfcp_sdev->status);
359 }
360
361 /**
362  * zfcp_cfdc_lun_shrng_vltn - Evaluate LUN sharing violation status
363  * @sdev: The LUN / SCSI device where sharing violation occurred
364  * @qual: The FSF status qualifier from the LUN sharing violation
365  */
366 void zfcp_cfdc_lun_shrng_vltn(struct scsi_device *sdev,
367                               union fsf_status_qual *qual)
368 {
369         struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
370
371         if (qual->word[0])
372                 dev_warn(&zfcp_sdev->port->adapter->ccw_device->dev,
373                          "LUN 0x%Lx on port 0x%Lx is already in "
374                          "use by CSS%d, MIF Image ID %x\n",
375                          zfcp_scsi_dev_lun(sdev),
376                          (unsigned long long)zfcp_sdev->port->wwpn,
377                          qual->fsf_queue_designator.cssid,
378                          qual->fsf_queue_designator.hla);
379         else
380                 zfcp_act_eval_err(zfcp_sdev->port->adapter, qual->word[2]);
381
382         zfcp_erp_set_lun_status(sdev,
383                                 ZFCP_STATUS_COMMON_ERP_FAILED |
384                                 ZFCP_STATUS_COMMON_ACCESS_DENIED);
385         atomic_clear_mask(ZFCP_STATUS_LUN_SHARED, &zfcp_sdev->status);
386         atomic_clear_mask(ZFCP_STATUS_LUN_READONLY, &zfcp_sdev->status);
387 }
388
389 /**
390  * zfcp_cfdc_open_lun_eval - Eval access ctrl. status for successful "open lun"
391  * @sdev: The SCSI device / LUN where to evaluate the status
392  * @bottom: The qtcb bottom with the status from the "open lun"
393  *
394  * Returns: 0 if LUN is usable, -EACCES if the access control table
395  *          reports an unsupported configuration.
396  */
397 int zfcp_cfdc_open_lun_eval(struct scsi_device *sdev,
398                             struct fsf_qtcb_bottom_support *bottom)
399 {
400         int shared, rw;
401         struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
402         struct zfcp_adapter *adapter = zfcp_sdev->port->adapter;
403
404         if ((adapter->connection_features & FSF_FEATURE_NPIV_MODE) ||
405             !(adapter->adapter_features & FSF_FEATURE_LUN_SHARING) ||
406             zfcp_ccw_priv_sch(adapter))
407                 return 0;
408
409         shared = !(bottom->lun_access_info & FSF_UNIT_ACCESS_EXCLUSIVE);
410         rw = (bottom->lun_access_info & FSF_UNIT_ACCESS_OUTBOUND_TRANSFER);
411
412         if (shared)
413                 atomic_set_mask(ZFCP_STATUS_LUN_SHARED, &zfcp_sdev->status);
414
415         if (!rw) {
416                 atomic_set_mask(ZFCP_STATUS_LUN_READONLY, &zfcp_sdev->status);
417                 dev_info(&adapter->ccw_device->dev, "SCSI device at LUN "
418                          "0x%016Lx on port 0x%016Lx opened read-only\n",
419                          zfcp_scsi_dev_lun(sdev),
420                          (unsigned long long)zfcp_sdev->port->wwpn);
421         }
422
423         if (!shared && !rw) {
424                 dev_err(&adapter->ccw_device->dev, "Exclusive read-only access "
425                         "not supported (LUN 0x%016Lx, port 0x%016Lx)\n",
426                         zfcp_scsi_dev_lun(sdev),
427                         (unsigned long long)zfcp_sdev->port->wwpn);
428                 zfcp_erp_set_lun_status(sdev, ZFCP_STATUS_COMMON_ERP_FAILED);
429                 zfcp_erp_lun_shutdown(sdev, 0, "fsouh_6", NULL);
430                 return -EACCES;
431         }
432
433         if (shared && rw) {
434                 dev_err(&adapter->ccw_device->dev,
435                         "Shared read-write access not supported "
436                         "(LUN 0x%016Lx, port 0x%016Lx)\n",
437                         zfcp_scsi_dev_lun(sdev),
438                         (unsigned long long)zfcp_sdev->port->wwpn);
439                 zfcp_erp_set_lun_status(sdev, ZFCP_STATUS_COMMON_ERP_FAILED);
440                 zfcp_erp_lun_shutdown(sdev, 0, "fsosh_8", NULL);
441                 return -EACCES;
442         }
443
444         return 0;
445 }