zfcp_cfdc.c 12 KB

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