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