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