zfcp_cfdc.c 6.2 KB

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