blob: f952b89b108a8b3ba3397d253330902d4e9d6f38 [file] [log] [blame]
Christof Schmitt45633fd2008-06-10 18:20:55 +02001/*
2 * zfcp device driver
3 *
4 * Userspace interface for accessing the
Christof Schmitta1ca4832010-09-08 14:39:59 +02005 * Access Control Lists / Control File Data Channel;
6 * handling of response code and states for ports and LUNs.
Christof Schmitt45633fd2008-06-10 18:20:55 +02007 *
Christof Schmitta1ca4832010-09-08 14:39:59 +02008 * Copyright IBM Corporation 2008, 2010
Christof Schmitt45633fd2008-06-10 18:20:55 +02009 */
10
Christof Schmittecf39d42008-12-25 13:39:53 +010011#define KMSG_COMPONENT "zfcp"
12#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Christof Schmitt45633fd2008-06-10 18:20:55 +020015#include <linux/types.h>
16#include <linux/miscdevice.h>
Heiko Carstens9e2ab1f2010-01-13 17:52:38 +010017#include <asm/compat.h>
Christof Schmitt45633fd2008-06-10 18:20:55 +020018#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 */
47struct 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
57static 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
73static 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
89static struct zfcp_adapter *zfcp_cfdc_get_adapter(u32 devno)
90{
Christof Schmittb228af02008-12-19 16:56:55 +010091 char busid[9];
Swen Schilligde3dc572009-11-24 16:54:00 +010092 struct ccw_device *cdev;
93 struct zfcp_adapter *adapter;
Christof Schmittc5afd812009-09-24 10:23:22 +020094
Christof Schmittb228af02008-12-19 16:56:55 +010095 snprintf(busid, sizeof(busid), "0.0.%04x", devno);
Swen Schilligde3dc572009-11-24 16:54:00 +010096 cdev = get_ccwdev_by_busid(&zfcp_ccw_driver, busid);
97 if (!cdev)
98 return NULL;
Christof Schmittc5afd812009-09-24 10:23:22 +020099
Swen Schilligde3dc572009-11-24 16:54:00 +0100100 adapter = zfcp_ccw_adapter_by_cdev(cdev);
Christof Schmittc5afd812009-09-24 10:23:22 +0200101
Swen Schilligde3dc572009-11-24 16:54:00 +0100102 put_device(&cdev->dev);
Christof Schmittc5afd812009-09-24 10:23:22 +0200103 return adapter;
Christof Schmitt45633fd2008-06-10 18:20:55 +0200104}
105
106static 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
136static 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
158static 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
168static long zfcp_cfdc_dev_ioctl(struct file *file, unsigned int command,
Heiko Carstens9e2ab1f2010-01-13 17:52:38 +0100169 unsigned long arg)
Christof Schmitt45633fd2008-06-10 18:20:55 +0200170{
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
Heiko Carstens9e2ab1f2010-01-13 17:52:38 +0100181 if (is_compat_task())
182 data_user = compat_ptr(arg);
183 else
184 data_user = (void __user *)arg;
185
Christof Schmitt45633fd2008-06-10 18:20:55 +0200186 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
Christof Schmitt674c3a92010-07-16 15:37:34 +0200193 data = memdup_user(data_user, sizeof(*data_user));
194 if (IS_ERR(data)) {
195 retval = PTR_ERR(data);
Christof Schmitt45633fd2008-06-10 18:20:55 +0200196 goto no_mem_sense;
197 }
198
Christof Schmitt45633fd2008-06-10 18:20:55 +0200199 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 }
Christof Schmitt45633fd2008-06-10 18:20:55 +0200211
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:
Swen Schilligde3dc572009-11-24 16:54:00 +0100243 zfcp_ccw_adapter_put(adapter);
Christof Schmitt45633fd2008-06-10 18:20:55 +0200244 free_buffer:
245 kfree(data);
246 no_mem_sense:
247 kfree(fsf_cfdc);
248 return retval;
249}
250
251static const struct file_operations zfcp_cfdc_fops = {
Martin Schwidefsky58ea91c2010-05-17 10:00:07 +0200252 .open = nonseekable_open,
Christof Schmitt45633fd2008-06-10 18:20:55 +0200253 .unlocked_ioctl = zfcp_cfdc_dev_ioctl,
254#ifdef CONFIG_COMPAT
255 .compat_ioctl = zfcp_cfdc_dev_ioctl
256#endif
257};
258
259struct miscdevice zfcp_cfdc_misc = {
260 .minor = MISC_DYNAMIC_MINOR,
261 .name = "zfcp_cfdc",
262 .fops = &zfcp_cfdc_fops,
263};
Christof Schmitta1ca4832010-09-08 14:39:59 +0200264
265/**
266 * zfcp_cfdc_adapter_access_changed - Process change in adapter ACT
267 * @adapter: Adapter where the Access Control Table (ACT) changed
268 *
269 * After a change in the adapter ACT, check if access to any
270 * previously denied resources is now possible.
271 */
272void zfcp_cfdc_adapter_access_changed(struct zfcp_adapter *adapter)
273{
274 unsigned long flags;
275 struct zfcp_port *port;
276 struct scsi_device *sdev;
277 struct zfcp_scsi_dev *zfcp_sdev;
278 int status;
279
280 if (adapter->connection_features & FSF_FEATURE_NPIV_MODE)
281 return;
282
283 read_lock_irqsave(&adapter->port_list_lock, flags);
284 list_for_each_entry(port, &adapter->port_list, list) {
285 status = atomic_read(&port->status);
286 if ((status & ZFCP_STATUS_COMMON_ACCESS_DENIED) ||
287 (status & ZFCP_STATUS_COMMON_ACCESS_BOXED))
288 zfcp_erp_port_reopen(port,
289 ZFCP_STATUS_COMMON_ERP_FAILED,
290 "cfaac_1", NULL);
291 }
292 read_unlock_irqrestore(&adapter->port_list_lock, flags);
293
294 shost_for_each_device(sdev, port->adapter->scsi_host) {
295 zfcp_sdev = sdev_to_zfcp(sdev);
296 status = atomic_read(&zfcp_sdev->status);
297 if ((status & ZFCP_STATUS_COMMON_ACCESS_DENIED) ||
298 (status & ZFCP_STATUS_COMMON_ACCESS_BOXED))
299 zfcp_erp_lun_reopen(sdev,
300 ZFCP_STATUS_COMMON_ERP_FAILED,
301 "cfaac_2", NULL);
302 }
303}
304
305static void zfcp_act_eval_err(struct zfcp_adapter *adapter, u32 table)
306{
307 u16 subtable = table >> 16;
308 u16 rule = table & 0xffff;
309 const char *act_type[] = { "unknown", "OS", "WWPN", "DID", "LUN" };
310
311 if (subtable && subtable < ARRAY_SIZE(act_type))
312 dev_warn(&adapter->ccw_device->dev,
313 "Access denied according to ACT rule type %s, "
314 "rule %d\n", act_type[subtable], rule);
315}
316
317/**
318 * zfcp_cfdc_port_denied - Process "access denied" for port
319 * @port: The port where the acces has been denied
320 * @qual: The FSF status qualifier for the access denied FSF status
321 */
322void zfcp_cfdc_port_denied(struct zfcp_port *port,
323 union fsf_status_qual *qual)
324{
325 dev_warn(&port->adapter->ccw_device->dev,
326 "Access denied to port 0x%016Lx\n",
327 (unsigned long long)port->wwpn);
328
329 zfcp_act_eval_err(port->adapter, qual->halfword[0]);
330 zfcp_act_eval_err(port->adapter, qual->halfword[1]);
331 zfcp_erp_modify_port_status(port, "cfadp_1", NULL,
332 ZFCP_STATUS_COMMON_ERP_FAILED |
333 ZFCP_STATUS_COMMON_ACCESS_DENIED, ZFCP_SET);
334}
335
336/**
337 * zfcp_cfdc_lun_denied - Process "access denied" for LUN
338 * @sdev: The SCSI device / LUN where the access has been denied
339 * @qual: The FSF status qualifier for the access denied FSF status
340 */
341void zfcp_cfdc_lun_denied(struct scsi_device *sdev,
342 union fsf_status_qual *qual)
343{
344 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
345
346 dev_warn(&zfcp_sdev->port->adapter->ccw_device->dev,
347 "Access denied to LUN 0x%016Lx on port 0x%016Lx\n",
348 zfcp_scsi_dev_lun(sdev),
349 (unsigned long long)zfcp_sdev->port->wwpn);
350 zfcp_act_eval_err(zfcp_sdev->port->adapter, qual->halfword[0]);
351 zfcp_act_eval_err(zfcp_sdev->port->adapter, qual->halfword[1]);
352 zfcp_erp_modify_lun_status(sdev, "cfadl_1", NULL,
353 ZFCP_STATUS_COMMON_ERP_FAILED |
354 ZFCP_STATUS_COMMON_ACCESS_DENIED, ZFCP_SET);
355
356 atomic_clear_mask(ZFCP_STATUS_LUN_SHARED, &zfcp_sdev->status);
357 atomic_clear_mask(ZFCP_STATUS_LUN_READONLY, &zfcp_sdev->status);
358}
359
360/**
361 * zfcp_cfdc_lun_shrng_vltn - Evaluate LUN sharing violation status
362 * @sdev: The LUN / SCSI device where sharing violation occurred
363 * @qual: The FSF status qualifier from the LUN sharing violation
364 */
365void zfcp_cfdc_lun_shrng_vltn(struct scsi_device *sdev,
366 union fsf_status_qual *qual)
367{
368 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
369
370 if (qual->word[0])
371 dev_warn(&zfcp_sdev->port->adapter->ccw_device->dev,
372 "LUN 0x%Lx on port 0x%Lx is already in "
373 "use by CSS%d, MIF Image ID %x\n",
374 zfcp_scsi_dev_lun(sdev),
375 (unsigned long long)zfcp_sdev->port->wwpn,
376 qual->fsf_queue_designator.cssid,
377 qual->fsf_queue_designator.hla);
378 else
379 zfcp_act_eval_err(zfcp_sdev->port->adapter, qual->word[2]);
380
381 zfcp_erp_modify_lun_status(sdev, "fsosh_3", NULL,
382 ZFCP_STATUS_COMMON_ERP_FAILED |
383 ZFCP_STATUS_COMMON_ACCESS_DENIED, ZFCP_SET);
384 atomic_clear_mask(ZFCP_STATUS_LUN_SHARED, &zfcp_sdev->status);
385 atomic_clear_mask(ZFCP_STATUS_LUN_READONLY, &zfcp_sdev->status);
386}
387
388/**
389 * zfcp_cfdc_open_lun_eval - Eval access ctrl. status for successful "open lun"
390 * @sdev: The SCSI device / LUN where to evaluate the status
391 * @bottom: The qtcb bottom with the status from the "open lun"
392 *
393 * Returns: 0 if LUN is usable, -EACCES if the access control table
394 * reports an unsupported configuration.
395 */
396int zfcp_cfdc_open_lun_eval(struct scsi_device *sdev,
397 struct fsf_qtcb_bottom_support *bottom)
398{
399 int shared, rw;
400 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
401 struct zfcp_adapter *adapter = zfcp_sdev->port->adapter;
402
403 if ((adapter->connection_features & FSF_FEATURE_NPIV_MODE) ||
404 !(adapter->adapter_features & FSF_FEATURE_LUN_SHARING) ||
405 zfcp_ccw_priv_sch(adapter))
406 return 0;
407
408 shared = !(bottom->lun_access_info & FSF_UNIT_ACCESS_EXCLUSIVE);
409 rw = (bottom->lun_access_info & FSF_UNIT_ACCESS_OUTBOUND_TRANSFER);
410
411 if (shared)
412 atomic_set_mask(ZFCP_STATUS_LUN_SHARED, &zfcp_sdev->status);
413
414 if (!rw) {
415 atomic_set_mask(ZFCP_STATUS_LUN_READONLY, &zfcp_sdev->status);
416 dev_info(&adapter->ccw_device->dev, "SCSI device at LUN "
417 "0x%016Lx on port 0x%016Lx opened read-only\n",
418 zfcp_scsi_dev_lun(sdev),
419 (unsigned long long)zfcp_sdev->port->wwpn);
420 }
421
422 if (!shared && !rw) {
423 dev_err(&adapter->ccw_device->dev, "Exclusive read-only access "
424 "not supported (LUN 0x%016Lx, port 0x%016Lx)\n",
425 zfcp_scsi_dev_lun(sdev),
426 (unsigned long long)zfcp_sdev->port->wwpn);
427 zfcp_erp_lun_failed(sdev, "fsosh_5", NULL);
428 zfcp_erp_lun_shutdown(sdev, 0, "fsouh_6", NULL);
429 return -EACCES;
430 }
431
432 if (shared && rw) {
433 dev_err(&adapter->ccw_device->dev,
434 "Shared read-write access not supported "
435 "(LUN 0x%016Lx, port 0x%016Lx)\n",
436 zfcp_scsi_dev_lun(sdev),
437 (unsigned long long)zfcp_sdev->port->wwpn);
438 zfcp_erp_lun_failed(sdev, "fsosh_7", NULL);
439 zfcp_erp_lun_shutdown(sdev, 0, "fsosh_8", NULL);
440 return -EACCES;
441 }
442
443 return 0;
444}