device_pgid.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * CCW device PGID and path verification I/O handling.
  4. *
  5. * Copyright IBM Corp. 2002, 2009
  6. * Author(s): Cornelia Huck <[email protected]>
  7. * Martin Schwidefsky <[email protected]>
  8. * Peter Oberparleiter <[email protected]>
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/string.h>
  12. #include <linux/bitops.h>
  13. #include <linux/types.h>
  14. #include <linux/errno.h>
  15. #include <linux/slab.h>
  16. #include <asm/ccwdev.h>
  17. #include <asm/cio.h>
  18. #include "cio.h"
  19. #include "cio_debug.h"
  20. #include "device.h"
  21. #include "io_sch.h"
  22. #define PGID_RETRIES 256
  23. #define PGID_TIMEOUT (10 * HZ)
  24. static void verify_start(struct ccw_device *cdev);
  25. /*
  26. * Process path verification data and report result.
  27. */
  28. static void verify_done(struct ccw_device *cdev, int rc)
  29. {
  30. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  31. struct ccw_dev_id *id = &cdev->private->dev_id;
  32. int mpath = cdev->private->flags.mpath;
  33. int pgroup = cdev->private->flags.pgroup;
  34. if (rc)
  35. goto out;
  36. /* Ensure consistent multipathing state at device and channel. */
  37. if (sch->config.mp != mpath) {
  38. sch->config.mp = mpath;
  39. rc = cio_commit_config(sch);
  40. }
  41. out:
  42. CIO_MSG_EVENT(2, "vrfy: device 0.%x.%04x: rc=%d pgroup=%d mpath=%d "
  43. "vpm=%02x\n", id->ssid, id->devno, rc, pgroup, mpath,
  44. sch->vpm);
  45. ccw_device_verify_done(cdev, rc);
  46. }
  47. /*
  48. * Create channel program to perform a NOOP.
  49. */
  50. static void nop_build_cp(struct ccw_device *cdev)
  51. {
  52. struct ccw_request *req = &cdev->private->req;
  53. struct ccw1 *cp = cdev->private->dma_area->iccws;
  54. cp->cmd_code = CCW_CMD_NOOP;
  55. cp->cda = 0;
  56. cp->count = 0;
  57. cp->flags = CCW_FLAG_SLI;
  58. req->cp = cp;
  59. }
  60. /*
  61. * Perform NOOP on a single path.
  62. */
  63. static void nop_do(struct ccw_device *cdev)
  64. {
  65. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  66. struct ccw_request *req = &cdev->private->req;
  67. req->lpm = lpm_adjust(req->lpm, sch->schib.pmcw.pam & sch->opm &
  68. ~cdev->private->path_noirq_mask);
  69. if (!req->lpm)
  70. goto out_nopath;
  71. nop_build_cp(cdev);
  72. ccw_request_start(cdev);
  73. return;
  74. out_nopath:
  75. verify_done(cdev, sch->vpm ? 0 : -EACCES);
  76. }
  77. /*
  78. * Adjust NOOP I/O status.
  79. */
  80. static enum io_status nop_filter(struct ccw_device *cdev, void *data,
  81. struct irb *irb, enum io_status status)
  82. {
  83. /* Only subchannel status might indicate a path error. */
  84. if (status == IO_STATUS_ERROR && irb->scsw.cmd.cstat == 0)
  85. return IO_DONE;
  86. return status;
  87. }
  88. /*
  89. * Process NOOP request result for a single path.
  90. */
  91. static void nop_callback(struct ccw_device *cdev, void *data, int rc)
  92. {
  93. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  94. struct ccw_request *req = &cdev->private->req;
  95. switch (rc) {
  96. case 0:
  97. sch->vpm |= req->lpm;
  98. break;
  99. case -ETIME:
  100. cdev->private->path_noirq_mask |= req->lpm;
  101. break;
  102. case -EACCES:
  103. cdev->private->path_notoper_mask |= req->lpm;
  104. break;
  105. default:
  106. goto err;
  107. }
  108. /* Continue on the next path. */
  109. req->lpm >>= 1;
  110. nop_do(cdev);
  111. return;
  112. err:
  113. verify_done(cdev, rc);
  114. }
  115. /*
  116. * Create channel program to perform SET PGID on a single path.
  117. */
  118. static void spid_build_cp(struct ccw_device *cdev, u8 fn)
  119. {
  120. struct ccw_request *req = &cdev->private->req;
  121. struct ccw1 *cp = cdev->private->dma_area->iccws;
  122. int i = pathmask_to_pos(req->lpm);
  123. struct pgid *pgid = &cdev->private->dma_area->pgid[i];
  124. pgid->inf.fc = fn;
  125. cp->cmd_code = CCW_CMD_SET_PGID;
  126. cp->cda = (u32) (addr_t) pgid;
  127. cp->count = sizeof(*pgid);
  128. cp->flags = CCW_FLAG_SLI;
  129. req->cp = cp;
  130. }
  131. static void pgid_wipeout_callback(struct ccw_device *cdev, void *data, int rc)
  132. {
  133. if (rc) {
  134. /* We don't know the path groups' state. Abort. */
  135. verify_done(cdev, rc);
  136. return;
  137. }
  138. /*
  139. * Path groups have been reset. Restart path verification but
  140. * leave paths in path_noirq_mask out.
  141. */
  142. cdev->private->flags.pgid_unknown = 0;
  143. verify_start(cdev);
  144. }
  145. /*
  146. * Reset pathgroups and restart path verification, leave unusable paths out.
  147. */
  148. static void pgid_wipeout_start(struct ccw_device *cdev)
  149. {
  150. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  151. struct ccw_dev_id *id = &cdev->private->dev_id;
  152. struct ccw_request *req = &cdev->private->req;
  153. u8 fn;
  154. CIO_MSG_EVENT(2, "wipe: device 0.%x.%04x: pvm=%02x nim=%02x\n",
  155. id->ssid, id->devno, cdev->private->pgid_valid_mask,
  156. cdev->private->path_noirq_mask);
  157. /* Initialize request data. */
  158. memset(req, 0, sizeof(*req));
  159. req->timeout = PGID_TIMEOUT;
  160. req->maxretries = PGID_RETRIES;
  161. req->lpm = sch->schib.pmcw.pam;
  162. req->callback = pgid_wipeout_callback;
  163. fn = SPID_FUNC_DISBAND;
  164. if (cdev->private->flags.mpath)
  165. fn |= SPID_FUNC_MULTI_PATH;
  166. spid_build_cp(cdev, fn);
  167. ccw_request_start(cdev);
  168. }
  169. /*
  170. * Perform establish/resign SET PGID on a single path.
  171. */
  172. static void spid_do(struct ccw_device *cdev)
  173. {
  174. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  175. struct ccw_request *req = &cdev->private->req;
  176. u8 fn;
  177. /* Use next available path that is not already in correct state. */
  178. req->lpm = lpm_adjust(req->lpm, cdev->private->pgid_todo_mask);
  179. if (!req->lpm)
  180. goto out_nopath;
  181. /* Channel program setup. */
  182. if (req->lpm & sch->opm)
  183. fn = SPID_FUNC_ESTABLISH;
  184. else
  185. fn = SPID_FUNC_RESIGN;
  186. if (cdev->private->flags.mpath)
  187. fn |= SPID_FUNC_MULTI_PATH;
  188. spid_build_cp(cdev, fn);
  189. ccw_request_start(cdev);
  190. return;
  191. out_nopath:
  192. if (cdev->private->flags.pgid_unknown) {
  193. /* At least one SPID could be partially done. */
  194. pgid_wipeout_start(cdev);
  195. return;
  196. }
  197. verify_done(cdev, sch->vpm ? 0 : -EACCES);
  198. }
  199. /*
  200. * Process SET PGID request result for a single path.
  201. */
  202. static void spid_callback(struct ccw_device *cdev, void *data, int rc)
  203. {
  204. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  205. struct ccw_request *req = &cdev->private->req;
  206. switch (rc) {
  207. case 0:
  208. sch->vpm |= req->lpm & sch->opm;
  209. break;
  210. case -ETIME:
  211. cdev->private->flags.pgid_unknown = 1;
  212. cdev->private->path_noirq_mask |= req->lpm;
  213. break;
  214. case -EACCES:
  215. cdev->private->path_notoper_mask |= req->lpm;
  216. break;
  217. case -EOPNOTSUPP:
  218. if (cdev->private->flags.mpath) {
  219. /* Try without multipathing. */
  220. cdev->private->flags.mpath = 0;
  221. goto out_restart;
  222. }
  223. /* Try without pathgrouping. */
  224. cdev->private->flags.pgroup = 0;
  225. goto out_restart;
  226. default:
  227. goto err;
  228. }
  229. req->lpm >>= 1;
  230. spid_do(cdev);
  231. return;
  232. out_restart:
  233. verify_start(cdev);
  234. return;
  235. err:
  236. verify_done(cdev, rc);
  237. }
  238. static void spid_start(struct ccw_device *cdev)
  239. {
  240. struct ccw_request *req = &cdev->private->req;
  241. /* Initialize request data. */
  242. memset(req, 0, sizeof(*req));
  243. req->timeout = PGID_TIMEOUT;
  244. req->maxretries = PGID_RETRIES;
  245. req->lpm = 0x80;
  246. req->singlepath = 1;
  247. req->callback = spid_callback;
  248. spid_do(cdev);
  249. }
  250. static int pgid_is_reset(struct pgid *p)
  251. {
  252. char *c;
  253. for (c = (char *)p + 1; c < (char *)(p + 1); c++) {
  254. if (*c != 0)
  255. return 0;
  256. }
  257. return 1;
  258. }
  259. static int pgid_cmp(struct pgid *p1, struct pgid *p2)
  260. {
  261. return memcmp((char *) p1 + 1, (char *) p2 + 1,
  262. sizeof(struct pgid) - 1);
  263. }
  264. /*
  265. * Determine pathgroup state from PGID data.
  266. */
  267. static void pgid_analyze(struct ccw_device *cdev, struct pgid **p,
  268. int *mismatch, u8 *reserved, u8 *reset)
  269. {
  270. struct pgid *pgid = &cdev->private->dma_area->pgid[0];
  271. struct pgid *first = NULL;
  272. int lpm;
  273. int i;
  274. *mismatch = 0;
  275. *reserved = 0;
  276. *reset = 0;
  277. for (i = 0, lpm = 0x80; i < 8; i++, pgid++, lpm >>= 1) {
  278. if ((cdev->private->pgid_valid_mask & lpm) == 0)
  279. continue;
  280. if (pgid->inf.ps.state2 == SNID_STATE2_RESVD_ELSE)
  281. *reserved |= lpm;
  282. if (pgid_is_reset(pgid)) {
  283. *reset |= lpm;
  284. continue;
  285. }
  286. if (!first) {
  287. first = pgid;
  288. continue;
  289. }
  290. if (pgid_cmp(pgid, first) != 0)
  291. *mismatch = 1;
  292. }
  293. if (!first)
  294. first = &channel_subsystems[0]->global_pgid;
  295. *p = first;
  296. }
  297. static u8 pgid_to_donepm(struct ccw_device *cdev)
  298. {
  299. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  300. struct pgid *pgid;
  301. int i;
  302. int lpm;
  303. u8 donepm = 0;
  304. /* Set bits for paths which are already in the target state. */
  305. for (i = 0; i < 8; i++) {
  306. lpm = 0x80 >> i;
  307. if ((cdev->private->pgid_valid_mask & lpm) == 0)
  308. continue;
  309. pgid = &cdev->private->dma_area->pgid[i];
  310. if (sch->opm & lpm) {
  311. if (pgid->inf.ps.state1 != SNID_STATE1_GROUPED)
  312. continue;
  313. } else {
  314. if (pgid->inf.ps.state1 != SNID_STATE1_UNGROUPED)
  315. continue;
  316. }
  317. if (cdev->private->flags.mpath) {
  318. if (pgid->inf.ps.state3 != SNID_STATE3_MULTI_PATH)
  319. continue;
  320. } else {
  321. if (pgid->inf.ps.state3 != SNID_STATE3_SINGLE_PATH)
  322. continue;
  323. }
  324. donepm |= lpm;
  325. }
  326. return donepm;
  327. }
  328. static void pgid_fill(struct ccw_device *cdev, struct pgid *pgid)
  329. {
  330. int i;
  331. for (i = 0; i < 8; i++)
  332. memcpy(&cdev->private->dma_area->pgid[i], pgid,
  333. sizeof(struct pgid));
  334. }
  335. /*
  336. * Process SENSE PGID data and report result.
  337. */
  338. static void snid_done(struct ccw_device *cdev, int rc)
  339. {
  340. struct ccw_dev_id *id = &cdev->private->dev_id;
  341. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  342. struct pgid *pgid;
  343. int mismatch = 0;
  344. u8 reserved = 0;
  345. u8 reset = 0;
  346. u8 donepm;
  347. if (rc)
  348. goto out;
  349. pgid_analyze(cdev, &pgid, &mismatch, &reserved, &reset);
  350. if (reserved == cdev->private->pgid_valid_mask)
  351. rc = -EUSERS;
  352. else if (mismatch)
  353. rc = -EOPNOTSUPP;
  354. else {
  355. donepm = pgid_to_donepm(cdev);
  356. sch->vpm = donepm & sch->opm;
  357. cdev->private->pgid_reset_mask |= reset;
  358. cdev->private->pgid_todo_mask &=
  359. ~(donepm | cdev->private->path_noirq_mask);
  360. pgid_fill(cdev, pgid);
  361. }
  362. out:
  363. CIO_MSG_EVENT(2, "snid: device 0.%x.%04x: rc=%d pvm=%02x vpm=%02x "
  364. "todo=%02x mism=%d rsvd=%02x reset=%02x\n", id->ssid,
  365. id->devno, rc, cdev->private->pgid_valid_mask, sch->vpm,
  366. cdev->private->pgid_todo_mask, mismatch, reserved, reset);
  367. switch (rc) {
  368. case 0:
  369. if (cdev->private->flags.pgid_unknown) {
  370. pgid_wipeout_start(cdev);
  371. return;
  372. }
  373. /* Anything left to do? */
  374. if (cdev->private->pgid_todo_mask == 0) {
  375. verify_done(cdev, sch->vpm == 0 ? -EACCES : 0);
  376. return;
  377. }
  378. /* Perform path-grouping. */
  379. spid_start(cdev);
  380. break;
  381. case -EOPNOTSUPP:
  382. /* Path-grouping not supported. */
  383. cdev->private->flags.pgroup = 0;
  384. cdev->private->flags.mpath = 0;
  385. verify_start(cdev);
  386. break;
  387. default:
  388. verify_done(cdev, rc);
  389. }
  390. }
  391. /*
  392. * Create channel program to perform a SENSE PGID on a single path.
  393. */
  394. static void snid_build_cp(struct ccw_device *cdev)
  395. {
  396. struct ccw_request *req = &cdev->private->req;
  397. struct ccw1 *cp = cdev->private->dma_area->iccws;
  398. int i = pathmask_to_pos(req->lpm);
  399. /* Channel program setup. */
  400. cp->cmd_code = CCW_CMD_SENSE_PGID;
  401. cp->cda = (u32) (addr_t) &cdev->private->dma_area->pgid[i];
  402. cp->count = sizeof(struct pgid);
  403. cp->flags = CCW_FLAG_SLI;
  404. req->cp = cp;
  405. }
  406. /*
  407. * Perform SENSE PGID on a single path.
  408. */
  409. static void snid_do(struct ccw_device *cdev)
  410. {
  411. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  412. struct ccw_request *req = &cdev->private->req;
  413. int ret;
  414. req->lpm = lpm_adjust(req->lpm, sch->schib.pmcw.pam &
  415. ~cdev->private->path_noirq_mask);
  416. if (!req->lpm)
  417. goto out_nopath;
  418. snid_build_cp(cdev);
  419. ccw_request_start(cdev);
  420. return;
  421. out_nopath:
  422. if (cdev->private->pgid_valid_mask)
  423. ret = 0;
  424. else if (cdev->private->path_noirq_mask)
  425. ret = -ETIME;
  426. else
  427. ret = -EACCES;
  428. snid_done(cdev, ret);
  429. }
  430. /*
  431. * Process SENSE PGID request result for single path.
  432. */
  433. static void snid_callback(struct ccw_device *cdev, void *data, int rc)
  434. {
  435. struct ccw_request *req = &cdev->private->req;
  436. switch (rc) {
  437. case 0:
  438. cdev->private->pgid_valid_mask |= req->lpm;
  439. break;
  440. case -ETIME:
  441. cdev->private->flags.pgid_unknown = 1;
  442. cdev->private->path_noirq_mask |= req->lpm;
  443. break;
  444. case -EACCES:
  445. cdev->private->path_notoper_mask |= req->lpm;
  446. break;
  447. default:
  448. goto err;
  449. }
  450. /* Continue on the next path. */
  451. req->lpm >>= 1;
  452. snid_do(cdev);
  453. return;
  454. err:
  455. snid_done(cdev, rc);
  456. }
  457. /*
  458. * Perform path verification.
  459. */
  460. static void verify_start(struct ccw_device *cdev)
  461. {
  462. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  463. struct ccw_request *req = &cdev->private->req;
  464. struct ccw_dev_id *devid = &cdev->private->dev_id;
  465. sch->vpm = 0;
  466. sch->lpm = sch->schib.pmcw.pam;
  467. /* Initialize PGID data. */
  468. memset(cdev->private->dma_area->pgid, 0,
  469. sizeof(cdev->private->dma_area->pgid));
  470. cdev->private->pgid_valid_mask = 0;
  471. cdev->private->pgid_todo_mask = sch->schib.pmcw.pam;
  472. cdev->private->path_notoper_mask = 0;
  473. /* Initialize request data. */
  474. memset(req, 0, sizeof(*req));
  475. req->timeout = PGID_TIMEOUT;
  476. req->maxretries = PGID_RETRIES;
  477. req->lpm = 0x80;
  478. req->singlepath = 1;
  479. if (cdev->private->flags.pgroup) {
  480. CIO_TRACE_EVENT(4, "snid");
  481. CIO_HEX_EVENT(4, devid, sizeof(*devid));
  482. req->callback = snid_callback;
  483. snid_do(cdev);
  484. } else {
  485. CIO_TRACE_EVENT(4, "nop");
  486. CIO_HEX_EVENT(4, devid, sizeof(*devid));
  487. req->filter = nop_filter;
  488. req->callback = nop_callback;
  489. nop_do(cdev);
  490. }
  491. }
  492. /**
  493. * ccw_device_verify_start - perform path verification
  494. * @cdev: ccw device
  495. *
  496. * Perform an I/O on each available channel path to @cdev to determine which
  497. * paths are operational. The resulting path mask is stored in sch->vpm.
  498. * If device options specify pathgrouping, establish a pathgroup for the
  499. * operational paths. When finished, call ccw_device_verify_done with a
  500. * return code specifying the result.
  501. */
  502. void ccw_device_verify_start(struct ccw_device *cdev)
  503. {
  504. CIO_TRACE_EVENT(4, "vrfy");
  505. CIO_HEX_EVENT(4, &cdev->private->dev_id, sizeof(cdev->private->dev_id));
  506. /*
  507. * Initialize pathgroup and multipath state with target values.
  508. * They may change in the course of path verification.
  509. */
  510. cdev->private->flags.pgroup = cdev->private->options.pgroup;
  511. cdev->private->flags.mpath = cdev->private->options.mpath;
  512. cdev->private->flags.doverify = 0;
  513. cdev->private->path_noirq_mask = 0;
  514. verify_start(cdev);
  515. }
  516. /*
  517. * Process disband SET PGID request result.
  518. */
  519. static void disband_callback(struct ccw_device *cdev, void *data, int rc)
  520. {
  521. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  522. struct ccw_dev_id *id = &cdev->private->dev_id;
  523. if (rc)
  524. goto out;
  525. /* Ensure consistent multipathing state at device and channel. */
  526. cdev->private->flags.mpath = 0;
  527. if (sch->config.mp) {
  528. sch->config.mp = 0;
  529. rc = cio_commit_config(sch);
  530. }
  531. out:
  532. CIO_MSG_EVENT(0, "disb: device 0.%x.%04x: rc=%d\n", id->ssid, id->devno,
  533. rc);
  534. ccw_device_disband_done(cdev, rc);
  535. }
  536. /**
  537. * ccw_device_disband_start - disband pathgroup
  538. * @cdev: ccw device
  539. *
  540. * Execute a SET PGID channel program on @cdev to disband a previously
  541. * established pathgroup. When finished, call ccw_device_disband_done with
  542. * a return code specifying the result.
  543. */
  544. void ccw_device_disband_start(struct ccw_device *cdev)
  545. {
  546. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  547. struct ccw_request *req = &cdev->private->req;
  548. u8 fn;
  549. CIO_TRACE_EVENT(4, "disb");
  550. CIO_HEX_EVENT(4, &cdev->private->dev_id, sizeof(cdev->private->dev_id));
  551. /* Request setup. */
  552. memset(req, 0, sizeof(*req));
  553. req->timeout = PGID_TIMEOUT;
  554. req->maxretries = PGID_RETRIES;
  555. req->lpm = sch->schib.pmcw.pam & sch->opm;
  556. req->singlepath = 1;
  557. req->callback = disband_callback;
  558. fn = SPID_FUNC_DISBAND;
  559. if (cdev->private->flags.mpath)
  560. fn |= SPID_FUNC_MULTI_PATH;
  561. spid_build_cp(cdev, fn);
  562. ccw_request_start(cdev);
  563. }
  564. struct stlck_data {
  565. struct completion done;
  566. int rc;
  567. };
  568. static void stlck_build_cp(struct ccw_device *cdev, void *buf1, void *buf2)
  569. {
  570. struct ccw_request *req = &cdev->private->req;
  571. struct ccw1 *cp = cdev->private->dma_area->iccws;
  572. cp[0].cmd_code = CCW_CMD_STLCK;
  573. cp[0].cda = (u32) (addr_t) buf1;
  574. cp[0].count = 32;
  575. cp[0].flags = CCW_FLAG_CC;
  576. cp[1].cmd_code = CCW_CMD_RELEASE;
  577. cp[1].cda = (u32) (addr_t) buf2;
  578. cp[1].count = 32;
  579. cp[1].flags = 0;
  580. req->cp = cp;
  581. }
  582. static void stlck_callback(struct ccw_device *cdev, void *data, int rc)
  583. {
  584. struct stlck_data *sdata = data;
  585. sdata->rc = rc;
  586. complete(&sdata->done);
  587. }
  588. /**
  589. * ccw_device_stlck_start - perform unconditional release
  590. * @cdev: ccw device
  591. * @data: data pointer to be passed to ccw_device_stlck_done
  592. * @buf1: data pointer used in channel program
  593. * @buf2: data pointer used in channel program
  594. *
  595. * Execute a channel program on @cdev to release an existing PGID reservation.
  596. */
  597. static void ccw_device_stlck_start(struct ccw_device *cdev, void *data,
  598. void *buf1, void *buf2)
  599. {
  600. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  601. struct ccw_request *req = &cdev->private->req;
  602. CIO_TRACE_EVENT(4, "stlck");
  603. CIO_HEX_EVENT(4, &cdev->private->dev_id, sizeof(cdev->private->dev_id));
  604. /* Request setup. */
  605. memset(req, 0, sizeof(*req));
  606. req->timeout = PGID_TIMEOUT;
  607. req->maxretries = PGID_RETRIES;
  608. req->lpm = sch->schib.pmcw.pam & sch->opm;
  609. req->data = data;
  610. req->callback = stlck_callback;
  611. stlck_build_cp(cdev, buf1, buf2);
  612. ccw_request_start(cdev);
  613. }
  614. /*
  615. * Perform unconditional reserve + release.
  616. */
  617. int ccw_device_stlck(struct ccw_device *cdev)
  618. {
  619. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  620. struct stlck_data data;
  621. u8 *buffer;
  622. int rc;
  623. /* Check if steal lock operation is valid for this device. */
  624. if (cdev->drv) {
  625. if (!cdev->private->options.force)
  626. return -EINVAL;
  627. }
  628. buffer = kzalloc(64, GFP_DMA | GFP_KERNEL);
  629. if (!buffer)
  630. return -ENOMEM;
  631. init_completion(&data.done);
  632. data.rc = -EIO;
  633. spin_lock_irq(sch->lock);
  634. rc = cio_enable_subchannel(sch, (u32) (addr_t) sch);
  635. if (rc)
  636. goto out_unlock;
  637. /* Perform operation. */
  638. cdev->private->state = DEV_STATE_STEAL_LOCK;
  639. ccw_device_stlck_start(cdev, &data, &buffer[0], &buffer[32]);
  640. spin_unlock_irq(sch->lock);
  641. /* Wait for operation to finish. */
  642. if (wait_for_completion_interruptible(&data.done)) {
  643. /* Got a signal. */
  644. spin_lock_irq(sch->lock);
  645. ccw_request_cancel(cdev);
  646. spin_unlock_irq(sch->lock);
  647. wait_for_completion(&data.done);
  648. }
  649. rc = data.rc;
  650. /* Check results. */
  651. spin_lock_irq(sch->lock);
  652. cio_disable_subchannel(sch);
  653. cdev->private->state = DEV_STATE_BOXED;
  654. out_unlock:
  655. spin_unlock_irq(sch->lock);
  656. kfree(buffer);
  657. return rc;
  658. }