device_pgid.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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_cmp(struct pgid *p1, struct pgid *p2)
  191. {
  192. return memcmp((char *) p1 + 1, (char *) p2 + 1,
  193. sizeof(struct pgid) - 1);
  194. }
  195. /*
  196. * Determine pathgroup state from PGID data.
  197. */
  198. static void pgid_analyze(struct ccw_device *cdev, struct pgid **p,
  199. int *mismatch, int *reserved, int *reset)
  200. {
  201. struct pgid *pgid = &cdev->private->pgid[0];
  202. struct pgid *first = NULL;
  203. int lpm;
  204. int i;
  205. *mismatch = 0;
  206. *reserved = 0;
  207. *reset = 0;
  208. for (i = 0, lpm = 0x80; i < 8; i++, pgid++, lpm >>= 1) {
  209. if ((cdev->private->pgid_valid_mask & lpm) == 0)
  210. continue;
  211. if (pgid->inf.ps.state2 == SNID_STATE2_RESVD_ELSE)
  212. *reserved = 1;
  213. if (pgid->inf.ps.state1 == SNID_STATE1_RESET) {
  214. /* A PGID was reset. */
  215. *reset = 1;
  216. continue;
  217. }
  218. if (!first) {
  219. first = pgid;
  220. continue;
  221. }
  222. if (pgid_cmp(pgid, first) != 0)
  223. *mismatch = 1;
  224. }
  225. if (!first)
  226. first = &channel_subsystems[0]->global_pgid;
  227. *p = first;
  228. }
  229. static u8 pgid_to_donepm(struct ccw_device *cdev)
  230. {
  231. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  232. struct pgid *pgid;
  233. int i;
  234. int lpm;
  235. u8 donepm = 0;
  236. /* Set bits for paths which are already in the target state. */
  237. for (i = 0; i < 8; i++) {
  238. lpm = 0x80 >> i;
  239. if ((cdev->private->pgid_valid_mask & lpm) == 0)
  240. continue;
  241. pgid = &cdev->private->pgid[i];
  242. if (sch->opm & lpm) {
  243. if (pgid->inf.ps.state1 != SNID_STATE1_GROUPED)
  244. continue;
  245. } else {
  246. if (pgid->inf.ps.state1 != SNID_STATE1_UNGROUPED)
  247. continue;
  248. }
  249. if (cdev->private->flags.mpath) {
  250. if (pgid->inf.ps.state3 != SNID_STATE3_MULTI_PATH)
  251. continue;
  252. } else {
  253. if (pgid->inf.ps.state3 != SNID_STATE3_SINGLE_PATH)
  254. continue;
  255. }
  256. donepm |= lpm;
  257. }
  258. return donepm;
  259. }
  260. static void pgid_fill(struct ccw_device *cdev, struct pgid *pgid)
  261. {
  262. int i;
  263. for (i = 0; i < 8; i++)
  264. memcpy(&cdev->private->pgid[i], pgid, sizeof(struct pgid));
  265. }
  266. /*
  267. * Process SENSE PGID data and report result.
  268. */
  269. static void snid_done(struct ccw_device *cdev, int rc)
  270. {
  271. struct ccw_dev_id *id = &cdev->private->dev_id;
  272. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  273. struct pgid *pgid;
  274. int mismatch = 0;
  275. int reserved = 0;
  276. int reset = 0;
  277. u8 donepm;
  278. if (rc)
  279. goto out;
  280. pgid_analyze(cdev, &pgid, &mismatch, &reserved, &reset);
  281. if (reserved)
  282. rc = -EUSERS;
  283. else if (mismatch)
  284. rc = -EOPNOTSUPP;
  285. else {
  286. donepm = pgid_to_donepm(cdev);
  287. sch->vpm = donepm & sch->opm;
  288. cdev->private->pgid_todo_mask &= ~donepm;
  289. pgid_fill(cdev, pgid);
  290. }
  291. out:
  292. CIO_MSG_EVENT(2, "snid: device 0.%x.%04x: rc=%d pvm=%02x vpm=%02x "
  293. "todo=%02x mism=%d rsvd=%d reset=%d\n", id->ssid,
  294. id->devno, rc, cdev->private->pgid_valid_mask, sch->vpm,
  295. cdev->private->pgid_todo_mask, mismatch, reserved, reset);
  296. switch (rc) {
  297. case 0:
  298. /* Anything left to do? */
  299. if (cdev->private->pgid_todo_mask == 0) {
  300. verify_done(cdev, sch->vpm == 0 ? -EACCES : 0);
  301. return;
  302. }
  303. /* Perform path-grouping. */
  304. spid_start(cdev);
  305. break;
  306. case -EOPNOTSUPP:
  307. /* Path-grouping not supported. */
  308. cdev->private->flags.pgroup = 0;
  309. cdev->private->flags.mpath = 0;
  310. verify_start(cdev);
  311. break;
  312. default:
  313. verify_done(cdev, rc);
  314. }
  315. }
  316. /*
  317. * Create channel program to perform a SENSE PGID on a single path.
  318. */
  319. static void snid_build_cp(struct ccw_device *cdev)
  320. {
  321. struct ccw_request *req = &cdev->private->req;
  322. struct ccw1 *cp = cdev->private->iccws;
  323. int i = 8 - ffs(req->lpm);
  324. /* Channel program setup. */
  325. cp->cmd_code = CCW_CMD_SENSE_PGID;
  326. cp->cda = (u32) (addr_t) &cdev->private->pgid[i];
  327. cp->count = sizeof(struct pgid);
  328. cp->flags = CCW_FLAG_SLI;
  329. req->cp = cp;
  330. }
  331. /*
  332. * Perform SENSE PGID on a single path.
  333. */
  334. static void snid_do(struct ccw_device *cdev)
  335. {
  336. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  337. struct ccw_request *req = &cdev->private->req;
  338. /* Adjust lpm if paths are not set in pam. */
  339. req->lpm = lpm_adjust(req->lpm, sch->schib.pmcw.pam);
  340. if (!req->lpm)
  341. goto out_nopath;
  342. snid_build_cp(cdev);
  343. ccw_request_start(cdev);
  344. return;
  345. out_nopath:
  346. snid_done(cdev, cdev->private->pgid_valid_mask ? 0 : -EACCES);
  347. }
  348. /*
  349. * Process SENSE PGID request result for single path.
  350. */
  351. static void snid_callback(struct ccw_device *cdev, void *data, int rc)
  352. {
  353. struct ccw_request *req = &cdev->private->req;
  354. if (rc == 0)
  355. cdev->private->pgid_valid_mask |= req->lpm;
  356. else if (rc != -EACCES)
  357. goto err;
  358. req->lpm >>= 1;
  359. snid_do(cdev);
  360. return;
  361. err:
  362. snid_done(cdev, rc);
  363. }
  364. /*
  365. * Perform path verification.
  366. */
  367. static void verify_start(struct ccw_device *cdev)
  368. {
  369. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  370. struct ccw_request *req = &cdev->private->req;
  371. struct ccw_dev_id *devid = &cdev->private->dev_id;
  372. sch->vpm = 0;
  373. sch->lpm = sch->schib.pmcw.pam;
  374. /* Initialize request data. */
  375. memset(req, 0, sizeof(*req));
  376. req->timeout = PGID_TIMEOUT;
  377. req->maxretries = PGID_RETRIES;
  378. req->lpm = 0x80;
  379. req->singlepath = 1;
  380. if (cdev->private->flags.pgroup) {
  381. CIO_TRACE_EVENT(4, "snid");
  382. CIO_HEX_EVENT(4, devid, sizeof(*devid));
  383. req->callback = snid_callback;
  384. snid_do(cdev);
  385. } else {
  386. CIO_TRACE_EVENT(4, "nop");
  387. CIO_HEX_EVENT(4, devid, sizeof(*devid));
  388. req->filter = nop_filter;
  389. req->callback = nop_callback;
  390. nop_do(cdev);
  391. }
  392. }
  393. /**
  394. * ccw_device_verify_start - perform path verification
  395. * @cdev: ccw device
  396. *
  397. * Perform an I/O on each available channel path to @cdev to determine which
  398. * paths are operational. The resulting path mask is stored in sch->vpm.
  399. * If device options specify pathgrouping, establish a pathgroup for the
  400. * operational paths. When finished, call ccw_device_verify_done with a
  401. * return code specifying the result.
  402. */
  403. void ccw_device_verify_start(struct ccw_device *cdev)
  404. {
  405. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  406. CIO_TRACE_EVENT(4, "vrfy");
  407. CIO_HEX_EVENT(4, &cdev->private->dev_id, sizeof(cdev->private->dev_id));
  408. /* Initialize PGID data. */
  409. memset(cdev->private->pgid, 0, sizeof(cdev->private->pgid));
  410. cdev->private->pgid_valid_mask = 0;
  411. cdev->private->pgid_todo_mask = sch->schib.pmcw.pam;
  412. /*
  413. * Initialize pathgroup and multipath state with target values.
  414. * They may change in the course of path verification.
  415. */
  416. cdev->private->flags.pgroup = cdev->private->options.pgroup;
  417. cdev->private->flags.mpath = cdev->private->options.mpath;
  418. cdev->private->flags.doverify = 0;
  419. verify_start(cdev);
  420. }
  421. /*
  422. * Process disband SET PGID request result.
  423. */
  424. static void disband_callback(struct ccw_device *cdev, void *data, int rc)
  425. {
  426. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  427. struct ccw_dev_id *id = &cdev->private->dev_id;
  428. if (rc)
  429. goto out;
  430. /* Ensure consistent multipathing state at device and channel. */
  431. cdev->private->flags.mpath = 0;
  432. if (sch->config.mp) {
  433. sch->config.mp = 0;
  434. rc = cio_commit_config(sch);
  435. }
  436. out:
  437. CIO_MSG_EVENT(0, "disb: device 0.%x.%04x: rc=%d\n", id->ssid, id->devno,
  438. rc);
  439. ccw_device_disband_done(cdev, rc);
  440. }
  441. /**
  442. * ccw_device_disband_start - disband pathgroup
  443. * @cdev: ccw device
  444. *
  445. * Execute a SET PGID channel program on @cdev to disband a previously
  446. * established pathgroup. When finished, call ccw_device_disband_done with
  447. * a return code specifying the result.
  448. */
  449. void ccw_device_disband_start(struct ccw_device *cdev)
  450. {
  451. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  452. struct ccw_request *req = &cdev->private->req;
  453. u8 fn;
  454. CIO_TRACE_EVENT(4, "disb");
  455. CIO_HEX_EVENT(4, &cdev->private->dev_id, sizeof(cdev->private->dev_id));
  456. /* Request setup. */
  457. memset(req, 0, sizeof(*req));
  458. req->timeout = PGID_TIMEOUT;
  459. req->maxretries = PGID_RETRIES;
  460. req->lpm = sch->schib.pmcw.pam & sch->opm;
  461. req->singlepath = 1;
  462. req->callback = disband_callback;
  463. fn = SPID_FUNC_DISBAND;
  464. if (cdev->private->flags.mpath)
  465. fn |= SPID_FUNC_MULTI_PATH;
  466. spid_build_cp(cdev, fn);
  467. ccw_request_start(cdev);
  468. }
  469. static void stlck_build_cp(struct ccw_device *cdev, void *buf1, void *buf2)
  470. {
  471. struct ccw_request *req = &cdev->private->req;
  472. struct ccw1 *cp = cdev->private->iccws;
  473. cp[0].cmd_code = CCW_CMD_STLCK;
  474. cp[0].cda = (u32) (addr_t) buf1;
  475. cp[0].count = 32;
  476. cp[0].flags = CCW_FLAG_CC;
  477. cp[1].cmd_code = CCW_CMD_RELEASE;
  478. cp[1].cda = (u32) (addr_t) buf2;
  479. cp[1].count = 32;
  480. cp[1].flags = 0;
  481. req->cp = cp;
  482. }
  483. static void stlck_callback(struct ccw_device *cdev, void *data, int rc)
  484. {
  485. ccw_device_stlck_done(cdev, data, rc);
  486. }
  487. /**
  488. * ccw_device_stlck_start - perform unconditional release
  489. * @cdev: ccw device
  490. * @data: data pointer to be passed to ccw_device_stlck_done
  491. * @buf1: data pointer used in channel program
  492. * @buf2: data pointer used in channel program
  493. *
  494. * Execute a channel program on @cdev to release an existing PGID reservation.
  495. * When finished, call ccw_device_stlck_done with a return code specifying the
  496. * result.
  497. */
  498. void ccw_device_stlck_start(struct ccw_device *cdev, void *data, void *buf1,
  499. void *buf2)
  500. {
  501. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  502. struct ccw_request *req = &cdev->private->req;
  503. CIO_TRACE_EVENT(4, "stlck");
  504. CIO_HEX_EVENT(4, &cdev->private->dev_id, sizeof(cdev->private->dev_id));
  505. /* Request setup. */
  506. memset(req, 0, sizeof(*req));
  507. req->timeout = PGID_TIMEOUT;
  508. req->maxretries = PGID_RETRIES;
  509. req->lpm = sch->schib.pmcw.pam & sch->opm;
  510. req->data = data;
  511. req->callback = stlck_callback;
  512. stlck_build_cp(cdev, buf1, buf2);
  513. ccw_request_start(cdev);
  514. }