zfcp_cfdc.c 12 KB

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