ccwreq.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Handling of internal CCW device requests.
  4. *
  5. * Copyright IBM Corp. 2009, 2011
  6. * Author(s): Peter Oberparleiter <peter.oberparleiter@de.ibm.com>
  7. */
  8. #define KMSG_COMPONENT "cio"
  9. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  10. #include <linux/types.h>
  11. #include <linux/err.h>
  12. #include <asm/ccwdev.h>
  13. #include <asm/cio.h>
  14. #include "io_sch.h"
  15. #include "cio.h"
  16. #include "device.h"
  17. #include "cio_debug.h"
  18. /**
  19. * lpm_adjust - adjust path mask
  20. * @lpm: path mask to adjust
  21. * @mask: mask of available paths
  22. *
  23. * Shift @lpm right until @lpm and @mask have at least one bit in common or
  24. * until @lpm is zero. Return the resulting lpm.
  25. */
  26. int lpm_adjust(int lpm, int mask)
  27. {
  28. while (lpm && ((lpm & mask) == 0))
  29. lpm >>= 1;
  30. return lpm;
  31. }
  32. /*
  33. * Adjust path mask to use next path and reset retry count. Return resulting
  34. * path mask.
  35. */
  36. static u16 ccwreq_next_path(struct ccw_device *cdev)
  37. {
  38. struct ccw_request *req = &cdev->private->req;
  39. if (!req->singlepath) {
  40. req->mask = 0;
  41. goto out;
  42. }
  43. req->retries = req->maxretries;
  44. req->mask = lpm_adjust(req->mask >> 1, req->lpm);
  45. out:
  46. return req->mask;
  47. }
  48. /*
  49. * Clean up device state and report to callback.
  50. */
  51. static void ccwreq_stop(struct ccw_device *cdev, int rc)
  52. {
  53. struct ccw_request *req = &cdev->private->req;
  54. if (req->done)
  55. return;
  56. req->done = 1;
  57. ccw_device_set_timeout(cdev, 0);
  58. memset(&cdev->private->irb, 0, sizeof(struct irb));
  59. if (rc && rc != -ENODEV && req->drc)
  60. rc = req->drc;
  61. req->callback(cdev, req->data, rc);
  62. }
  63. /*
  64. * (Re-)Start the operation until retries and paths are exhausted.
  65. */
  66. static void ccwreq_do(struct ccw_device *cdev)
  67. {
  68. struct ccw_request *req = &cdev->private->req;
  69. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  70. struct ccw1 *cp = req->cp;
  71. int rc = -EACCES;
  72. while (req->mask) {
  73. if (req->retries-- == 0) {
  74. /* Retries exhausted, try next path. */
  75. ccwreq_next_path(cdev);
  76. continue;
  77. }
  78. /* Perform start function. */
  79. memset(&cdev->private->irb, 0, sizeof(struct irb));
  80. rc = cio_start(sch, cp, (u8) req->mask);
  81. if (rc == 0) {
  82. /* I/O started successfully. */
  83. ccw_device_set_timeout(cdev, req->timeout);
  84. return;
  85. }
  86. if (rc == -ENODEV) {
  87. /* Permanent device error. */
  88. break;
  89. }
  90. if (rc == -EACCES) {
  91. /* Permant path error. */
  92. ccwreq_next_path(cdev);
  93. continue;
  94. }
  95. /* Temporary improper status. */
  96. rc = cio_clear(sch);
  97. if (rc)
  98. break;
  99. return;
  100. }
  101. ccwreq_stop(cdev, rc);
  102. }
  103. /**
  104. * ccw_request_start - perform I/O request
  105. * @cdev: ccw device
  106. *
  107. * Perform the I/O request specified by cdev->req.
  108. */
  109. void ccw_request_start(struct ccw_device *cdev)
  110. {
  111. struct ccw_request *req = &cdev->private->req;
  112. if (req->singlepath) {
  113. /* Try all paths twice to counter link flapping. */
  114. req->mask = 0x8080;
  115. } else
  116. req->mask = req->lpm;
  117. req->retries = req->maxretries;
  118. req->mask = lpm_adjust(req->mask, req->lpm);
  119. req->drc = 0;
  120. req->done = 0;
  121. req->cancel = 0;
  122. if (!req->mask)
  123. goto out_nopath;
  124. ccwreq_do(cdev);
  125. return;
  126. out_nopath:
  127. ccwreq_stop(cdev, -EACCES);
  128. }
  129. /**
  130. * ccw_request_cancel - cancel running I/O request
  131. * @cdev: ccw device
  132. *
  133. * Cancel the I/O request specified by cdev->req. Return non-zero if request
  134. * has already finished, zero otherwise.
  135. */
  136. int ccw_request_cancel(struct ccw_device *cdev)
  137. {
  138. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  139. struct ccw_request *req = &cdev->private->req;
  140. int rc;
  141. if (req->done)
  142. return 1;
  143. req->cancel = 1;
  144. rc = cio_clear(sch);
  145. if (rc)
  146. ccwreq_stop(cdev, rc);
  147. return 0;
  148. }
  149. /*
  150. * Return the status of the internal I/O started on the specified ccw device.
  151. * Perform BASIC SENSE if required.
  152. */
  153. static enum io_status ccwreq_status(struct ccw_device *cdev, struct irb *lcirb)
  154. {
  155. struct irb *irb = &cdev->private->irb;
  156. struct cmd_scsw *scsw = &irb->scsw.cmd;
  157. enum uc_todo todo;
  158. /* Perform BASIC SENSE if needed. */
  159. if (ccw_device_accumulate_and_sense(cdev, lcirb))
  160. return IO_RUNNING;
  161. /* Check for halt/clear interrupt. */
  162. if (scsw->fctl & (SCSW_FCTL_HALT_FUNC | SCSW_FCTL_CLEAR_FUNC))
  163. return IO_KILLED;
  164. /* Check for path error. */
  165. if (scsw->cc == 3 || scsw->pno)
  166. return IO_PATH_ERROR;
  167. /* Handle BASIC SENSE data. */
  168. if (irb->esw.esw0.erw.cons) {
  169. CIO_TRACE_EVENT(2, "sensedata");
  170. CIO_HEX_EVENT(2, &cdev->private->dev_id,
  171. sizeof(struct ccw_dev_id));
  172. CIO_HEX_EVENT(2, &cdev->private->irb.ecw, SENSE_MAX_COUNT);
  173. /* Check for command reject. */
  174. if (irb->ecw[0] & SNS0_CMD_REJECT)
  175. return IO_REJECTED;
  176. /* Ask the driver what to do */
  177. if (cdev->drv && cdev->drv->uc_handler) {
  178. todo = cdev->drv->uc_handler(cdev, lcirb);
  179. CIO_TRACE_EVENT(2, "uc_response");
  180. CIO_HEX_EVENT(2, &todo, sizeof(todo));
  181. switch (todo) {
  182. case UC_TODO_RETRY:
  183. return IO_STATUS_ERROR;
  184. case UC_TODO_RETRY_ON_NEW_PATH:
  185. return IO_PATH_ERROR;
  186. case UC_TODO_STOP:
  187. return IO_REJECTED;
  188. default:
  189. return IO_STATUS_ERROR;
  190. }
  191. }
  192. /* Assume that unexpected SENSE data implies an error. */
  193. return IO_STATUS_ERROR;
  194. }
  195. /* Check for channel errors. */
  196. if (scsw->cstat != 0)
  197. return IO_STATUS_ERROR;
  198. /* Check for device errors. */
  199. if (scsw->dstat & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END))
  200. return IO_STATUS_ERROR;
  201. /* Check for final state. */
  202. if (!(scsw->dstat & DEV_STAT_DEV_END))
  203. return IO_RUNNING;
  204. /* Check for other improper status. */
  205. if (scsw->cc == 1 && (scsw->stctl & SCSW_STCTL_ALERT_STATUS))
  206. return IO_STATUS_ERROR;
  207. return IO_DONE;
  208. }
  209. /*
  210. * Log ccw request status.
  211. */
  212. static void ccwreq_log_status(struct ccw_device *cdev, enum io_status status)
  213. {
  214. struct ccw_request *req = &cdev->private->req;
  215. struct {
  216. struct ccw_dev_id dev_id;
  217. u16 retries;
  218. u8 lpm;
  219. u8 status;
  220. } __attribute__ ((packed)) data;
  221. data.dev_id = cdev->private->dev_id;
  222. data.retries = req->retries;
  223. data.lpm = (u8) req->mask;
  224. data.status = (u8) status;
  225. CIO_TRACE_EVENT(2, "reqstat");
  226. CIO_HEX_EVENT(2, &data, sizeof(data));
  227. }
  228. /**
  229. * ccw_request_handler - interrupt handler for I/O request procedure.
  230. * @cdev: ccw device
  231. *
  232. * Handle interrupt during I/O request procedure.
  233. */
  234. void ccw_request_handler(struct ccw_device *cdev)
  235. {
  236. struct irb *irb = this_cpu_ptr(&cio_irb);
  237. struct ccw_request *req = &cdev->private->req;
  238. enum io_status status;
  239. int rc = -EOPNOTSUPP;
  240. /* Check status of I/O request. */
  241. status = ccwreq_status(cdev, irb);
  242. if (req->filter)
  243. status = req->filter(cdev, req->data, irb, status);
  244. if (status != IO_RUNNING)
  245. ccw_device_set_timeout(cdev, 0);
  246. if (status != IO_DONE && status != IO_RUNNING)
  247. ccwreq_log_status(cdev, status);
  248. switch (status) {
  249. case IO_DONE:
  250. break;
  251. case IO_RUNNING:
  252. return;
  253. case IO_REJECTED:
  254. goto err;
  255. case IO_PATH_ERROR:
  256. goto out_next_path;
  257. case IO_STATUS_ERROR:
  258. goto out_restart;
  259. case IO_KILLED:
  260. /* Check if request was cancelled on purpose. */
  261. if (req->cancel) {
  262. rc = -EIO;
  263. goto err;
  264. }
  265. goto out_restart;
  266. }
  267. /* Check back with request initiator. */
  268. if (!req->check)
  269. goto out;
  270. switch (req->check(cdev, req->data)) {
  271. case 0:
  272. break;
  273. case -EAGAIN:
  274. goto out_restart;
  275. case -EACCES:
  276. goto out_next_path;
  277. default:
  278. goto err;
  279. }
  280. out:
  281. ccwreq_stop(cdev, 0);
  282. return;
  283. out_next_path:
  284. /* Try next path and restart I/O. */
  285. if (!ccwreq_next_path(cdev)) {
  286. rc = -EACCES;
  287. goto err;
  288. }
  289. out_restart:
  290. /* Restart. */
  291. ccwreq_do(cdev);
  292. return;
  293. err:
  294. ccwreq_stop(cdev, rc);
  295. }
  296. /**
  297. * ccw_request_timeout - timeout handler for I/O request procedure
  298. * @cdev: ccw device
  299. *
  300. * Handle timeout during I/O request procedure.
  301. */
  302. void ccw_request_timeout(struct ccw_device *cdev)
  303. {
  304. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  305. struct ccw_request *req = &cdev->private->req;
  306. int rc = -ENODEV, chp;
  307. if (cio_update_schib(sch))
  308. goto err;
  309. for (chp = 0; chp < 8; chp++) {
  310. if ((0x80 >> chp) & sch->schib.pmcw.lpum)
  311. pr_warn("%s: No interrupt was received within %lus (CS=%02x, DS=%02x, CHPID=%x.%02x)\n",
  312. dev_name(&cdev->dev), req->timeout / HZ,
  313. scsw_cstat(&sch->schib.scsw),
  314. scsw_dstat(&sch->schib.scsw),
  315. sch->schid.cssid,
  316. sch->schib.pmcw.chpid[chp]);
  317. }
  318. if (!ccwreq_next_path(cdev)) {
  319. /* set the final return code for this request */
  320. req->drc = -ETIME;
  321. }
  322. rc = cio_clear(sch);
  323. if (rc)
  324. goto err;
  325. return;
  326. err:
  327. ccwreq_stop(cdev, rc);
  328. }
  329. /**
  330. * ccw_request_notoper - notoper handler for I/O request procedure
  331. * @cdev: ccw device
  332. *
  333. * Handle notoper during I/O request procedure.
  334. */
  335. void ccw_request_notoper(struct ccw_device *cdev)
  336. {
  337. ccwreq_stop(cdev, -ENODEV);
  338. }