device_pgid.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. /*
  2. * CCW device PGID and path verification I/O handling.
  3. *
  4. * Copyright IBM Corp. 2002,2009
  5. * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
  6. * Martin Schwidefsky <schwidefsky@de.ibm.com>
  7. * Peter Oberparleiter <peter.oberparleiter@de.ibm.com>
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/string.h>
  11. #include <linux/types.h>
  12. #include <linux/errno.h>
  13. #include <linux/bitops.h>
  14. #include <asm/ccwdev.h>
  15. #include <asm/cio.h>
  16. #include "cio.h"
  17. #include "cio_debug.h"
  18. #include "device.h"
  19. #include "io_sch.h"
  20. #define PGID_RETRIES 256
  21. #define PGID_TIMEOUT (10 * HZ)
  22. /*
  23. * Process path verification data and report result.
  24. */
  25. static void verify_done(struct ccw_device *cdev, int rc)
  26. {
  27. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  28. struct ccw_dev_id *id = &cdev->private->dev_id;
  29. int mpath = cdev->private->flags.mpath;
  30. int pgroup = cdev->private->flags.pgroup;
  31. if (rc)
  32. goto out;
  33. /* Ensure consistent multipathing state at device and channel. */
  34. if (sch->config.mp != mpath) {
  35. sch->config.mp = mpath;
  36. rc = cio_commit_config(sch);
  37. }
  38. out:
  39. CIO_MSG_EVENT(2, "vrfy: device 0.%x.%04x: rc=%d pgroup=%d mpath=%d "
  40. "vpm=%02x\n", id->ssid, id->devno, rc, pgroup, mpath,
  41. sch->vpm);
  42. ccw_device_verify_done(cdev, rc);
  43. }
  44. /*
  45. * Create channel program to perform a NOOP.
  46. */
  47. static void nop_build_cp(struct ccw_device *cdev)
  48. {
  49. struct ccw_request *req = &cdev->private->req;
  50. struct ccw1 *cp = cdev->private->iccws;
  51. cp->cmd_code = CCW_CMD_NOOP;
  52. cp->cda = 0;
  53. cp->count = 0;
  54. cp->flags = CCW_FLAG_SLI;
  55. req->cp = cp;
  56. }
  57. /*
  58. * Perform NOOP on a single path.
  59. */
  60. static void nop_do(struct ccw_device *cdev)
  61. {
  62. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  63. struct ccw_request *req = &cdev->private->req;
  64. /* Adjust lpm. */
  65. req->lpm = lpm_adjust(req->lpm, sch->schib.pmcw.pam & sch->opm);
  66. if (!req->lpm)
  67. goto out_nopath;
  68. nop_build_cp(cdev);
  69. ccw_request_start(cdev);
  70. return;
  71. out_nopath:
  72. verify_done(cdev, sch->vpm ? 0 : -EACCES);
  73. }
  74. /*
  75. * Adjust NOOP I/O status.
  76. */
  77. static enum io_status nop_filter(struct ccw_device *cdev, void *data,
  78. struct irb *irb, enum io_status status)
  79. {
  80. /* Only subchannel status might indicate a path error. */
  81. if (status == IO_STATUS_ERROR && irb->scsw.cmd.cstat == 0)
  82. return IO_DONE;
  83. return status;
  84. }
  85. /*
  86. * Process NOOP request result for a single path.
  87. */
  88. static void nop_callback(struct ccw_device *cdev, void *data, int rc)
  89. {
  90. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  91. struct ccw_request *req = &cdev->private->req;
  92. if (rc == 0)
  93. sch->vpm |= req->lpm;
  94. else if (rc != -EACCES)
  95. goto err;
  96. req->lpm >>= 1;
  97. nop_do(cdev);
  98. return;
  99. err:
  100. verify_done(cdev, rc);
  101. }
  102. /*
  103. * Create channel program to perform SET PGID on a single path.
  104. */
  105. static void spid_build_cp(struct ccw_device *cdev, u8 fn)
  106. {
  107. struct ccw_request *req = &cdev->private->req;
  108. struct ccw1 *cp = cdev->private->iccws;
  109. int i = 8 - ffs(req->lpm);
  110. struct pgid *pgid = &cdev->private->pgid[i];
  111. pgid->inf.fc = fn;
  112. cp->cmd_code = CCW_CMD_SET_PGID;
  113. cp->cda = (u32) (addr_t) pgid;
  114. cp->count = sizeof(*pgid);
  115. cp->flags = CCW_FLAG_SLI;
  116. req->cp = cp;
  117. }
  118. /*
  119. * Perform establish/resign SET PGID on a single path.
  120. */
  121. static void spid_do(struct ccw_device *cdev)
  122. {
  123. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  124. struct ccw_request *req = &cdev->private->req;
  125. u8 fn;
  126. /* Use next available path that is not already in correct state. */
  127. req->lpm = lpm_adjust(req->lpm, cdev->private->pgid_todo_mask);
  128. if (!req->lpm)
  129. goto out_nopath;
  130. /* Channel program setup. */
  131. if (req->lpm & sch->opm)
  132. fn = SPID_FUNC_ESTABLISH;
  133. else
  134. fn = SPID_FUNC_RESIGN;
  135. if (cdev->private->flags.mpath)
  136. fn |= SPID_FUNC_MULTI_PATH;
  137. spid_build_cp(cdev, fn);
  138. ccw_request_start(cdev);
  139. return;
  140. out_nopath:
  141. verify_done(cdev, sch->vpm ? 0 : -EACCES);
  142. }
  143. static void verify_start(struct ccw_device *cdev);
  144. /*
  145. * Process SET PGID request result for a single path.
  146. */
  147. static void spid_callback(struct ccw_device *cdev, void *data, int rc)
  148. {
  149. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  150. struct ccw_request *req = &cdev->private->req;
  151. switch (rc) {
  152. case 0:
  153. sch->vpm |= req->lpm & sch->opm;
  154. break;
  155. case -EACCES:
  156. break;
  157. case -EOPNOTSUPP:
  158. if (cdev->private->flags.mpath) {
  159. /* Try without multipathing. */
  160. cdev->private->flags.mpath = 0;
  161. goto out_restart;
  162. }
  163. /* Try without pathgrouping. */
  164. cdev->private->flags.pgroup = 0;
  165. goto out_restart;
  166. default:
  167. goto err;
  168. }
  169. req->lpm >>= 1;
  170. spid_do(cdev);
  171. return;
  172. out_restart:
  173. verify_start(cdev);
  174. return;
  175. err:
  176. verify_done(cdev, rc);
  177. }
  178. static void spid_start(struct ccw_device *cdev)
  179. {
  180. struct ccw_request *req = &cdev->private->req;
  181. /* Initialize request data. */
  182. memset(req, 0, sizeof(*req));
  183. req->timeout = PGID_TIMEOUT;
  184. req->maxretries = PGID_RETRIES;
  185. req->lpm = 0x80;
  186. req->singlepath = 1;
  187. req->callback = spid_callback;
  188. spid_do(cdev);
  189. }
  190. static int pgid_is_reset(struct pgid *p)
  191. {
  192. char *c;
  193. for (c = (char *)p + 1; c < (char *)(p + 1); c++) {
  194. if (*c != 0)
  195. return 0;
  196. }
  197. return 1;
  198. }
  199. static int pgid_cmp(struct pgid *p1, struct pgid *p2)
  200. {
  201. return memcmp((char *) p1 + 1, (char *) p2 + 1,
  202. sizeof(struct pgid) - 1);
  203. }
  204. /*
  205. * Determine pathgroup state from PGID data.
  206. */
  207. static void pgid_analyze(struct ccw_device *cdev, struct pgid **p,
  208. int *mismatch, u8 *reserved, u8 *reset)
  209. {
  210. struct pgid *pgid = &cdev->private->pgid[0];
  211. struct pgid *first = NULL;
  212. int lpm;
  213. int i;
  214. *mismatch = 0;
  215. *reserved = 0;
  216. *reset = 0;
  217. for (i = 0, lpm = 0x80; i < 8; i++, pgid++, lpm >>= 1) {
  218. if ((cdev->private->pgid_valid_mask & lpm) == 0)
  219. continue;
  220. if (pgid->inf.ps.state2 == SNID_STATE2_RESVD_ELSE)
  221. *reserved |= lpm;
  222. if (pgid_is_reset(pgid)) {
  223. *reset |= lpm;
  224. continue;
  225. }
  226. if (!first) {
  227. first = pgid;
  228. continue;
  229. }
  230. if (pgid_cmp(pgid, first) != 0)
  231. *mismatch = 1;
  232. }
  233. if (!first)
  234. first = &channel_subsystems[0]->global_pgid;
  235. *p = first;
  236. }
  237. static u8 pgid_to_donepm(struct ccw_device *cdev)
  238. {
  239. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  240. struct pgid *pgid;
  241. int i;
  242. int lpm;
  243. u8 donepm = 0;
  244. /* Set bits for paths which are already in the target state. */
  245. for (i = 0; i < 8; i++) {
  246. lpm = 0x80 >> i;
  247. if ((cdev->private->pgid_valid_mask & lpm) == 0)
  248. continue;
  249. pgid = &cdev->private->pgid[i];
  250. if (sch->opm & lpm) {
  251. if (pgid->inf.ps.state1 != SNID_STATE1_GROUPED)
  252. continue;
  253. } else {
  254. if (pgid->inf.ps.state1 != SNID_STATE1_UNGROUPED)
  255. continue;
  256. }
  257. if (cdev->private->flags.mpath) {
  258. if (pgid->inf.ps.state3 != SNID_STATE3_MULTI_PATH)
  259. continue;
  260. } else {
  261. if (pgid->inf.ps.state3 != SNID_STATE3_SINGLE_PATH)
  262. continue;
  263. }
  264. donepm |= lpm;
  265. }
  266. return donepm;
  267. }
  268. static void pgid_fill(struct ccw_device *cdev, struct pgid *pgid)
  269. {
  270. int i;
  271. for (i = 0; i < 8; i++)
  272. memcpy(&cdev->private->pgid[i], pgid, sizeof(struct pgid));
  273. }
  274. /*
  275. * Process SENSE PGID data and report result.
  276. */
  277. static void snid_done(struct ccw_device *cdev, int rc)
  278. {
  279. struct ccw_dev_id *id = &cdev->private->dev_id;
  280. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  281. struct pgid *pgid;
  282. int mismatch = 0;
  283. u8 reserved = 0;
  284. u8 reset = 0;
  285. u8 donepm;
  286. if (rc)
  287. goto out;
  288. pgid_analyze(cdev, &pgid, &mismatch, &reserved, &reset);
  289. if (reserved == cdev->private->pgid_valid_mask)
  290. rc = -EUSERS;
  291. else if (mismatch)
  292. rc = -EOPNOTSUPP;
  293. else {
  294. donepm = pgid_to_donepm(cdev);
  295. sch->vpm = donepm & sch->opm;
  296. cdev->private->pgid_todo_mask &= ~donepm;
  297. cdev->private->pgid_reset_mask |= reset;
  298. pgid_fill(cdev, pgid);
  299. }
  300. out:
  301. CIO_MSG_EVENT(2, "snid: device 0.%x.%04x: rc=%d pvm=%02x vpm=%02x "
  302. "todo=%02x mism=%d rsvd=%02x reset=%02x\n", id->ssid,
  303. id->devno, rc, cdev->private->pgid_valid_mask, sch->vpm,
  304. cdev->private->pgid_todo_mask, mismatch, reserved, reset);
  305. switch (rc) {
  306. case 0:
  307. /* Anything left to do? */
  308. if (cdev->private->pgid_todo_mask == 0) {
  309. verify_done(cdev, sch->vpm == 0 ? -EACCES : 0);
  310. return;
  311. }
  312. /* Perform path-grouping. */
  313. spid_start(cdev);
  314. break;
  315. case -EOPNOTSUPP:
  316. /* Path-grouping not supported. */
  317. cdev->private->flags.pgroup = 0;
  318. cdev->private->flags.mpath = 0;
  319. verify_start(cdev);
  320. break;
  321. default:
  322. verify_done(cdev, rc);
  323. }
  324. }
  325. /*
  326. * Create channel program to perform a SENSE PGID on a single path.
  327. */
  328. static void snid_build_cp(struct ccw_device *cdev)
  329. {
  330. struct ccw_request *req = &cdev->private->req;
  331. struct ccw1 *cp = cdev->private->iccws;
  332. int i = 8 - ffs(req->lpm);
  333. /* Channel program setup. */
  334. cp->cmd_code = CCW_CMD_SENSE_PGID;
  335. cp->cda = (u32) (addr_t) &cdev->private->pgid[i];
  336. cp->count = sizeof(struct pgid);
  337. cp->flags = CCW_FLAG_SLI;
  338. req->cp = cp;
  339. }
  340. /*
  341. * Perform SENSE PGID on a single path.
  342. */
  343. static void snid_do(struct ccw_device *cdev)
  344. {
  345. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  346. struct ccw_request *req = &cdev->private->req;
  347. /* Adjust lpm if paths are not set in pam. */
  348. req->lpm = lpm_adjust(req->lpm, sch->schib.pmcw.pam);
  349. if (!req->lpm)
  350. goto out_nopath;
  351. snid_build_cp(cdev);
  352. ccw_request_start(cdev);
  353. return;
  354. out_nopath:
  355. snid_done(cdev, cdev->private->pgid_valid_mask ? 0 : -EACCES);
  356. }
  357. /*
  358. * Process SENSE PGID request result for single path.
  359. */
  360. static void snid_callback(struct ccw_device *cdev, void *data, int rc)
  361. {
  362. struct ccw_request *req = &cdev->private->req;
  363. if (rc == 0)
  364. cdev->private->pgid_valid_mask |= req->lpm;
  365. else if (rc != -EACCES)
  366. goto err;
  367. req->lpm >>= 1;
  368. snid_do(cdev);
  369. return;
  370. err:
  371. snid_done(cdev, rc);
  372. }
  373. /*
  374. * Perform path verification.
  375. */
  376. static void verify_start(struct ccw_device *cdev)
  377. {
  378. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  379. struct ccw_request *req = &cdev->private->req;
  380. struct ccw_dev_id *devid = &cdev->private->dev_id;
  381. sch->vpm = 0;
  382. sch->lpm = sch->schib.pmcw.pam;
  383. /* Initialize request data. */
  384. memset(req, 0, sizeof(*req));
  385. req->timeout = PGID_TIMEOUT;
  386. req->maxretries = PGID_RETRIES;
  387. req->lpm = 0x80;
  388. req->singlepath = 1;
  389. if (cdev->private->flags.pgroup) {
  390. CIO_TRACE_EVENT(4, "snid");
  391. CIO_HEX_EVENT(4, devid, sizeof(*devid));
  392. req->callback = snid_callback;
  393. snid_do(cdev);
  394. } else {
  395. CIO_TRACE_EVENT(4, "nop");
  396. CIO_HEX_EVENT(4, devid, sizeof(*devid));
  397. req->filter = nop_filter;
  398. req->callback = nop_callback;
  399. nop_do(cdev);
  400. }
  401. }
  402. /**
  403. * ccw_device_verify_start - perform path verification
  404. * @cdev: ccw device
  405. *
  406. * Perform an I/O on each available channel path to @cdev to determine which
  407. * paths are operational. The resulting path mask is stored in sch->vpm.
  408. * If device options specify pathgrouping, establish a pathgroup for the
  409. * operational paths. When finished, call ccw_device_verify_done with a
  410. * return code specifying the result.
  411. */
  412. void ccw_device_verify_start(struct ccw_device *cdev)
  413. {
  414. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  415. CIO_TRACE_EVENT(4, "vrfy");
  416. CIO_HEX_EVENT(4, &cdev->private->dev_id, sizeof(cdev->private->dev_id));
  417. /* Initialize PGID data. */
  418. memset(cdev->private->pgid, 0, sizeof(cdev->private->pgid));
  419. cdev->private->pgid_valid_mask = 0;
  420. cdev->private->pgid_todo_mask = sch->schib.pmcw.pam;
  421. /*
  422. * Initialize pathgroup and multipath state with target values.
  423. * They may change in the course of path verification.
  424. */
  425. cdev->private->flags.pgroup = cdev->private->options.pgroup;
  426. cdev->private->flags.mpath = cdev->private->options.mpath;
  427. cdev->private->flags.doverify = 0;
  428. verify_start(cdev);
  429. }
  430. /*
  431. * Process disband SET PGID request result.
  432. */
  433. static void disband_callback(struct ccw_device *cdev, void *data, int rc)
  434. {
  435. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  436. struct ccw_dev_id *id = &cdev->private->dev_id;
  437. if (rc)
  438. goto out;
  439. /* Ensure consistent multipathing state at device and channel. */
  440. cdev->private->flags.mpath = 0;
  441. if (sch->config.mp) {
  442. sch->config.mp = 0;
  443. rc = cio_commit_config(sch);
  444. }
  445. out:
  446. CIO_MSG_EVENT(0, "disb: device 0.%x.%04x: rc=%d\n", id->ssid, id->devno,
  447. rc);
  448. ccw_device_disband_done(cdev, rc);
  449. }
  450. /**
  451. * ccw_device_disband_start - disband pathgroup
  452. * @cdev: ccw device
  453. *
  454. * Execute a SET PGID channel program on @cdev to disband a previously
  455. * established pathgroup. When finished, call ccw_device_disband_done with
  456. * a return code specifying the result.
  457. */
  458. void ccw_device_disband_start(struct ccw_device *cdev)
  459. {
  460. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  461. struct ccw_request *req = &cdev->private->req;
  462. u8 fn;
  463. CIO_TRACE_EVENT(4, "disb");
  464. CIO_HEX_EVENT(4, &cdev->private->dev_id, sizeof(cdev->private->dev_id));
  465. /* Request setup. */
  466. memset(req, 0, sizeof(*req));
  467. req->timeout = PGID_TIMEOUT;
  468. req->maxretries = PGID_RETRIES;
  469. req->lpm = sch->schib.pmcw.pam & sch->opm;
  470. req->singlepath = 1;
  471. req->callback = disband_callback;
  472. fn = SPID_FUNC_DISBAND;
  473. if (cdev->private->flags.mpath)
  474. fn |= SPID_FUNC_MULTI_PATH;
  475. spid_build_cp(cdev, fn);
  476. ccw_request_start(cdev);
  477. }
  478. static void stlck_build_cp(struct ccw_device *cdev, void *buf1, void *buf2)
  479. {
  480. struct ccw_request *req = &cdev->private->req;
  481. struct ccw1 *cp = cdev->private->iccws;
  482. cp[0].cmd_code = CCW_CMD_STLCK;
  483. cp[0].cda = (u32) (addr_t) buf1;
  484. cp[0].count = 32;
  485. cp[0].flags = CCW_FLAG_CC;
  486. cp[1].cmd_code = CCW_CMD_RELEASE;
  487. cp[1].cda = (u32) (addr_t) buf2;
  488. cp[1].count = 32;
  489. cp[1].flags = 0;
  490. req->cp = cp;
  491. }
  492. static void stlck_callback(struct ccw_device *cdev, void *data, int rc)
  493. {
  494. ccw_device_stlck_done(cdev, data, rc);
  495. }
  496. /**
  497. * ccw_device_stlck_start - perform unconditional release
  498. * @cdev: ccw device
  499. * @data: data pointer to be passed to ccw_device_stlck_done
  500. * @buf1: data pointer used in channel program
  501. * @buf2: data pointer used in channel program
  502. *
  503. * Execute a channel program on @cdev to release an existing PGID reservation.
  504. * When finished, call ccw_device_stlck_done with a return code specifying the
  505. * result.
  506. */
  507. void ccw_device_stlck_start(struct ccw_device *cdev, void *data, void *buf1,
  508. void *buf2)
  509. {
  510. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  511. struct ccw_request *req = &cdev->private->req;
  512. CIO_TRACE_EVENT(4, "stlck");
  513. CIO_HEX_EVENT(4, &cdev->private->dev_id, sizeof(cdev->private->dev_id));
  514. /* Request setup. */
  515. memset(req, 0, sizeof(*req));
  516. req->timeout = PGID_TIMEOUT;
  517. req->maxretries = PGID_RETRIES;
  518. req->lpm = sch->schib.pmcw.pam & sch->opm;
  519. req->data = data;
  520. req->callback = stlck_callback;
  521. stlck_build_cp(cdev, buf1, buf2);
  522. ccw_request_start(cdev);
  523. }