device_pgid.c 14 KB

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