zfcp_cfdc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. .llseek = no_llseek,
  222. };
  223. struct miscdevice zfcp_cfdc_misc = {
  224. .minor = MISC_DYNAMIC_MINOR,
  225. .name = "zfcp_cfdc",
  226. .fops = &zfcp_cfdc_fops,
  227. };
  228. /**
  229. * zfcp_cfdc_adapter_access_changed - Process change in adapter ACT
  230. * @adapter: Adapter where the Access Control Table (ACT) changed
  231. *
  232. * After a change in the adapter ACT, check if access to any
  233. * previously denied resources is now possible.
  234. */
  235. void zfcp_cfdc_adapter_access_changed(struct zfcp_adapter *adapter)
  236. {
  237. unsigned long flags;
  238. struct zfcp_port *port;
  239. struct scsi_device *sdev;
  240. struct zfcp_scsi_dev *zfcp_sdev;
  241. int status;
  242. if (adapter->connection_features & FSF_FEATURE_NPIV_MODE)
  243. return;
  244. read_lock_irqsave(&adapter->port_list_lock, flags);
  245. list_for_each_entry(port, &adapter->port_list, list) {
  246. status = atomic_read(&port->status);
  247. if ((status & ZFCP_STATUS_COMMON_ACCESS_DENIED) ||
  248. (status & ZFCP_STATUS_COMMON_ACCESS_BOXED))
  249. zfcp_erp_port_reopen(port,
  250. ZFCP_STATUS_COMMON_ERP_FAILED,
  251. "cfaac_1");
  252. }
  253. read_unlock_irqrestore(&adapter->port_list_lock, flags);
  254. shost_for_each_device(sdev, port->adapter->scsi_host) {
  255. zfcp_sdev = sdev_to_zfcp(sdev);
  256. status = atomic_read(&zfcp_sdev->status);
  257. if ((status & ZFCP_STATUS_COMMON_ACCESS_DENIED) ||
  258. (status & ZFCP_STATUS_COMMON_ACCESS_BOXED))
  259. zfcp_erp_lun_reopen(sdev,
  260. ZFCP_STATUS_COMMON_ERP_FAILED,
  261. "cfaac_2");
  262. }
  263. }
  264. static void zfcp_act_eval_err(struct zfcp_adapter *adapter, u32 table)
  265. {
  266. u16 subtable = table >> 16;
  267. u16 rule = table & 0xffff;
  268. const char *act_type[] = { "unknown", "OS", "WWPN", "DID", "LUN" };
  269. if (subtable && subtable < ARRAY_SIZE(act_type))
  270. dev_warn(&adapter->ccw_device->dev,
  271. "Access denied according to ACT rule type %s, "
  272. "rule %d\n", act_type[subtable], rule);
  273. }
  274. /**
  275. * zfcp_cfdc_port_denied - Process "access denied" for port
  276. * @port: The port where the acces has been denied
  277. * @qual: The FSF status qualifier for the access denied FSF status
  278. */
  279. void zfcp_cfdc_port_denied(struct zfcp_port *port,
  280. union fsf_status_qual *qual)
  281. {
  282. dev_warn(&port->adapter->ccw_device->dev,
  283. "Access denied to port 0x%016Lx\n",
  284. (unsigned long long)port->wwpn);
  285. zfcp_act_eval_err(port->adapter, qual->halfword[0]);
  286. zfcp_act_eval_err(port->adapter, qual->halfword[1]);
  287. zfcp_erp_set_port_status(port,
  288. ZFCP_STATUS_COMMON_ERP_FAILED |
  289. ZFCP_STATUS_COMMON_ACCESS_DENIED);
  290. }
  291. /**
  292. * zfcp_cfdc_lun_denied - Process "access denied" for LUN
  293. * @sdev: The SCSI device / LUN where the access has been denied
  294. * @qual: The FSF status qualifier for the access denied FSF status
  295. */
  296. void zfcp_cfdc_lun_denied(struct scsi_device *sdev,
  297. union fsf_status_qual *qual)
  298. {
  299. struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
  300. dev_warn(&zfcp_sdev->port->adapter->ccw_device->dev,
  301. "Access denied to LUN 0x%016Lx on port 0x%016Lx\n",
  302. zfcp_scsi_dev_lun(sdev),
  303. (unsigned long long)zfcp_sdev->port->wwpn);
  304. zfcp_act_eval_err(zfcp_sdev->port->adapter, qual->halfword[0]);
  305. zfcp_act_eval_err(zfcp_sdev->port->adapter, qual->halfword[1]);
  306. zfcp_erp_set_lun_status(sdev,
  307. ZFCP_STATUS_COMMON_ERP_FAILED |
  308. ZFCP_STATUS_COMMON_ACCESS_DENIED);
  309. atomic_clear_mask(ZFCP_STATUS_LUN_SHARED, &zfcp_sdev->status);
  310. atomic_clear_mask(ZFCP_STATUS_LUN_READONLY, &zfcp_sdev->status);
  311. }
  312. /**
  313. * zfcp_cfdc_lun_shrng_vltn - Evaluate LUN sharing violation status
  314. * @sdev: The LUN / SCSI device where sharing violation occurred
  315. * @qual: The FSF status qualifier from the LUN sharing violation
  316. */
  317. void zfcp_cfdc_lun_shrng_vltn(struct scsi_device *sdev,
  318. union fsf_status_qual *qual)
  319. {
  320. struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
  321. if (qual->word[0])
  322. dev_warn(&zfcp_sdev->port->adapter->ccw_device->dev,
  323. "LUN 0x%Lx on port 0x%Lx is already in "
  324. "use by CSS%d, MIF Image ID %x\n",
  325. zfcp_scsi_dev_lun(sdev),
  326. (unsigned long long)zfcp_sdev->port->wwpn,
  327. qual->fsf_queue_designator.cssid,
  328. qual->fsf_queue_designator.hla);
  329. else
  330. zfcp_act_eval_err(zfcp_sdev->port->adapter, qual->word[2]);
  331. zfcp_erp_set_lun_status(sdev,
  332. ZFCP_STATUS_COMMON_ERP_FAILED |
  333. ZFCP_STATUS_COMMON_ACCESS_DENIED);
  334. atomic_clear_mask(ZFCP_STATUS_LUN_SHARED, &zfcp_sdev->status);
  335. atomic_clear_mask(ZFCP_STATUS_LUN_READONLY, &zfcp_sdev->status);
  336. }
  337. /**
  338. * zfcp_cfdc_open_lun_eval - Eval access ctrl. status for successful "open lun"
  339. * @sdev: The SCSI device / LUN where to evaluate the status
  340. * @bottom: The qtcb bottom with the status from the "open lun"
  341. *
  342. * Returns: 0 if LUN is usable, -EACCES if the access control table
  343. * reports an unsupported configuration.
  344. */
  345. int zfcp_cfdc_open_lun_eval(struct scsi_device *sdev,
  346. struct fsf_qtcb_bottom_support *bottom)
  347. {
  348. int shared, rw;
  349. struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
  350. struct zfcp_adapter *adapter = zfcp_sdev->port->adapter;
  351. if ((adapter->connection_features & FSF_FEATURE_NPIV_MODE) ||
  352. !(adapter->adapter_features & FSF_FEATURE_LUN_SHARING) ||
  353. zfcp_ccw_priv_sch(adapter))
  354. return 0;
  355. shared = !(bottom->lun_access_info & FSF_UNIT_ACCESS_EXCLUSIVE);
  356. rw = (bottom->lun_access_info & FSF_UNIT_ACCESS_OUTBOUND_TRANSFER);
  357. if (shared)
  358. atomic_set_mask(ZFCP_STATUS_LUN_SHARED, &zfcp_sdev->status);
  359. if (!rw) {
  360. atomic_set_mask(ZFCP_STATUS_LUN_READONLY, &zfcp_sdev->status);
  361. dev_info(&adapter->ccw_device->dev, "SCSI device at LUN "
  362. "0x%016Lx on port 0x%016Lx opened read-only\n",
  363. zfcp_scsi_dev_lun(sdev),
  364. (unsigned long long)zfcp_sdev->port->wwpn);
  365. }
  366. if (!shared && !rw) {
  367. dev_err(&adapter->ccw_device->dev, "Exclusive read-only access "
  368. "not supported (LUN 0x%016Lx, port 0x%016Lx)\n",
  369. zfcp_scsi_dev_lun(sdev),
  370. (unsigned long long)zfcp_sdev->port->wwpn);
  371. zfcp_erp_set_lun_status(sdev, ZFCP_STATUS_COMMON_ERP_FAILED);
  372. zfcp_erp_lun_shutdown(sdev, 0, "fsouh_6");
  373. return -EACCES;
  374. }
  375. if (shared && rw) {
  376. dev_err(&adapter->ccw_device->dev,
  377. "Shared read-write access not supported "
  378. "(LUN 0x%016Lx, port 0x%016Lx)\n",
  379. zfcp_scsi_dev_lun(sdev),
  380. (unsigned long long)zfcp_sdev->port->wwpn);
  381. zfcp_erp_set_lun_status(sdev, ZFCP_STATUS_COMMON_ERP_FAILED);
  382. zfcp_erp_lun_shutdown(sdev, 0, "fsosh_8");
  383. return -EACCES;
  384. }
  385. return 0;
  386. }