zfcp_cfdc.c 12 KB

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