zfcp_cfdc.c 6.2 KB

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