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