zfcp_cfdc.c 6.0 KB

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