Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/sparc-2.6
[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  *
7  * Copyright IBM Corporation 2008, 2009
8  */
9
10 #define KMSG_COMPONENT "zfcp"
11 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12
13 #include <linux/types.h>
14 #include <linux/miscdevice.h>
15 #include <asm/compat.h>
16 #include <asm/ccwdev.h>
17 #include "zfcp_def.h"
18 #include "zfcp_ext.h"
19 #include "zfcp_fsf.h"
20
21 #define ZFCP_CFDC_CMND_DOWNLOAD_NORMAL          0x00010001
22 #define ZFCP_CFDC_CMND_DOWNLOAD_FORCE           0x00010101
23 #define ZFCP_CFDC_CMND_FULL_ACCESS              0x00000201
24 #define ZFCP_CFDC_CMND_RESTRICTED_ACCESS        0x00000401
25 #define ZFCP_CFDC_CMND_UPLOAD                   0x00010002
26
27 #define ZFCP_CFDC_DOWNLOAD                      0x00000001
28 #define ZFCP_CFDC_UPLOAD                        0x00000002
29 #define ZFCP_CFDC_WITH_CONTROL_FILE             0x00010000
30
31 #define ZFCP_CFDC_IOC_MAGIC                     0xDD
32 #define ZFCP_CFDC_IOC \
33         _IOWR(ZFCP_CFDC_IOC_MAGIC, 0, struct zfcp_cfdc_data)
34
35 /**
36  * struct zfcp_cfdc_data - data for ioctl cfdc interface
37  * @signature: request signature
38  * @devno: FCP adapter device number
39  * @command: command code
40  * @fsf_status: returns status of FSF command to userspace
41  * @fsf_status_qual: returned to userspace
42  * @payloads: access conflicts list
43  * @control_file: access control table
44  */
45 struct zfcp_cfdc_data {
46         u32 signature;
47         u32 devno;
48         u32 command;
49         u32 fsf_status;
50         u8  fsf_status_qual[FSF_STATUS_QUALIFIER_SIZE];
51         u8  payloads[256];
52         u8  control_file[0];
53 };
54
55 static int zfcp_cfdc_copy_from_user(struct scatterlist *sg,
56                                     void __user *user_buffer)
57 {
58         unsigned int length;
59         unsigned int size = ZFCP_CFDC_MAX_SIZE;
60
61         while (size) {
62                 length = min((unsigned int)size, sg->length);
63                 if (copy_from_user(sg_virt(sg++), user_buffer, length))
64                         return -EFAULT;
65                 user_buffer += length;
66                 size -= length;
67         }
68         return 0;
69 }
70
71 static int zfcp_cfdc_copy_to_user(void __user  *user_buffer,
72                                   struct scatterlist *sg)
73 {
74         unsigned int length;
75         unsigned int size = ZFCP_CFDC_MAX_SIZE;
76
77         while (size) {
78                 length = min((unsigned int) size, sg->length);
79                 if (copy_to_user(user_buffer, sg_virt(sg++), length))
80                         return -EFAULT;
81                 user_buffer += length;
82                 size -= length;
83         }
84         return 0;
85 }
86
87 static struct zfcp_adapter *zfcp_cfdc_get_adapter(u32 devno)
88 {
89         char busid[9];
90         struct ccw_device *cdev;
91         struct zfcp_adapter *adapter;
92
93         snprintf(busid, sizeof(busid), "0.0.%04x", devno);
94         cdev = get_ccwdev_by_busid(&zfcp_ccw_driver, busid);
95         if (!cdev)
96                 return NULL;
97
98         adapter = zfcp_ccw_adapter_by_cdev(cdev);
99
100         put_device(&cdev->dev);
101         return adapter;
102 }
103
104 static int zfcp_cfdc_set_fsf(struct zfcp_fsf_cfdc *fsf_cfdc, int command)
105 {
106         switch (command) {
107         case ZFCP_CFDC_CMND_DOWNLOAD_NORMAL:
108                 fsf_cfdc->command = FSF_QTCB_DOWNLOAD_CONTROL_FILE;
109                 fsf_cfdc->option = FSF_CFDC_OPTION_NORMAL_MODE;
110                 break;
111         case ZFCP_CFDC_CMND_DOWNLOAD_FORCE:
112                 fsf_cfdc->command = FSF_QTCB_DOWNLOAD_CONTROL_FILE;
113                 fsf_cfdc->option = FSF_CFDC_OPTION_FORCE;
114                 break;
115         case ZFCP_CFDC_CMND_FULL_ACCESS:
116                 fsf_cfdc->command = FSF_QTCB_DOWNLOAD_CONTROL_FILE;
117                 fsf_cfdc->option = FSF_CFDC_OPTION_FULL_ACCESS;
118                 break;
119         case ZFCP_CFDC_CMND_RESTRICTED_ACCESS:
120                 fsf_cfdc->command = FSF_QTCB_DOWNLOAD_CONTROL_FILE;
121                 fsf_cfdc->option = FSF_CFDC_OPTION_RESTRICTED_ACCESS;
122                 break;
123         case ZFCP_CFDC_CMND_UPLOAD:
124                 fsf_cfdc->command = FSF_QTCB_UPLOAD_CONTROL_FILE;
125                 fsf_cfdc->option = 0;
126                 break;
127         default:
128                 return -EINVAL;
129         }
130
131         return 0;
132 }
133
134 static int zfcp_cfdc_sg_setup(int command, struct scatterlist *sg,
135                               u8 __user *control_file)
136 {
137         int retval;
138         retval = zfcp_sg_setup_table(sg, ZFCP_CFDC_PAGES);
139         if (retval)
140                 return retval;
141
142         sg[ZFCP_CFDC_PAGES - 1].length = ZFCP_CFDC_MAX_SIZE % PAGE_SIZE;
143
144         if (command & ZFCP_CFDC_WITH_CONTROL_FILE &&
145             command & ZFCP_CFDC_DOWNLOAD) {
146                 retval = zfcp_cfdc_copy_from_user(sg, control_file);
147                 if (retval) {
148                         zfcp_sg_free_table(sg, ZFCP_CFDC_PAGES);
149                         return -EFAULT;
150                 }
151         }
152
153         return 0;
154 }
155
156 static void zfcp_cfdc_req_to_sense(struct zfcp_cfdc_data *data,
157                                    struct zfcp_fsf_req *req)
158 {
159         data->fsf_status = req->qtcb->header.fsf_status;
160         memcpy(&data->fsf_status_qual, &req->qtcb->header.fsf_status_qual,
161                sizeof(union fsf_status_qual));
162         memcpy(&data->payloads, &req->qtcb->bottom.support.els,
163                sizeof(req->qtcb->bottom.support.els));
164 }
165
166 static long zfcp_cfdc_dev_ioctl(struct file *file, unsigned int command,
167                                 unsigned long arg)
168 {
169         struct zfcp_cfdc_data *data;
170         struct zfcp_cfdc_data __user *data_user;
171         struct zfcp_adapter *adapter;
172         struct zfcp_fsf_req *req;
173         struct zfcp_fsf_cfdc *fsf_cfdc;
174         int retval;
175
176         if (command != ZFCP_CFDC_IOC)
177                 return -ENOTTY;
178
179         if (is_compat_task())
180                 data_user = compat_ptr(arg);
181         else
182                 data_user = (void __user *)arg;
183
184         if (!data_user)
185                 return -EINVAL;
186
187         fsf_cfdc = kmalloc(sizeof(struct zfcp_fsf_cfdc), GFP_KERNEL);
188         if (!fsf_cfdc)
189                 return -ENOMEM;
190
191         data = kmalloc(sizeof(struct zfcp_cfdc_data), GFP_KERNEL);
192         if (!data) {
193                 retval = -ENOMEM;
194                 goto no_mem_sense;
195         }
196
197         retval = copy_from_user(data, data_user, sizeof(*data));
198         if (retval) {
199                 retval = -EFAULT;
200                 goto free_buffer;
201         }
202
203         if (data->signature != 0xCFDCACDF) {
204                 retval = -EINVAL;
205                 goto free_buffer;
206         }
207
208         retval = zfcp_cfdc_set_fsf(fsf_cfdc, data->command);
209
210         adapter = zfcp_cfdc_get_adapter(data->devno);
211         if (!adapter) {
212                 retval = -ENXIO;
213                 goto free_buffer;
214         }
215
216         retval = zfcp_cfdc_sg_setup(data->command, fsf_cfdc->sg,
217                                     data_user->control_file);
218         if (retval)
219                 goto adapter_put;
220         req = zfcp_fsf_control_file(adapter, fsf_cfdc);
221         if (IS_ERR(req)) {
222                 retval = PTR_ERR(req);
223                 goto free_sg;
224         }
225
226         if (req->status & ZFCP_STATUS_FSFREQ_ERROR) {
227                 retval = -ENXIO;
228                 goto free_fsf;
229         }
230
231         zfcp_cfdc_req_to_sense(data, req);
232         retval = copy_to_user(data_user, data, sizeof(*data_user));
233         if (retval) {
234                 retval = -EFAULT;
235                 goto free_fsf;
236         }
237
238         if (data->command & ZFCP_CFDC_UPLOAD)
239                 retval = zfcp_cfdc_copy_to_user(&data_user->control_file,
240                                                 fsf_cfdc->sg);
241
242  free_fsf:
243         zfcp_fsf_req_free(req);
244  free_sg:
245         zfcp_sg_free_table(fsf_cfdc->sg, ZFCP_CFDC_PAGES);
246  adapter_put:
247         zfcp_ccw_adapter_put(adapter);
248  free_buffer:
249         kfree(data);
250  no_mem_sense:
251         kfree(fsf_cfdc);
252         return retval;
253 }
254
255 static const struct file_operations zfcp_cfdc_fops = {
256         .unlocked_ioctl = zfcp_cfdc_dev_ioctl,
257 #ifdef CONFIG_COMPAT
258         .compat_ioctl = zfcp_cfdc_dev_ioctl
259 #endif
260 };
261
262 struct miscdevice zfcp_cfdc_misc = {
263         .minor = MISC_DYNAMIC_MINOR,
264         .name = "zfcp_cfdc",
265         .fops = &zfcp_cfdc_fops,
266 };