ccwreq.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 <[email protected]>
  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->dma_area->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->dma_area->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->dma_area->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->dma_area->irb.ecw,
  173. SENSE_MAX_COUNT);
  174. /* Check for command reject. */
  175. if (irb->ecw[0] & SNS0_CMD_REJECT)
  176. return IO_REJECTED;
  177. /* Ask the driver what to do */
  178. if (cdev->drv && cdev->drv->uc_handler) {
  179. todo = cdev->drv->uc_handler(cdev, lcirb);
  180. CIO_TRACE_EVENT(2, "uc_response");
  181. CIO_HEX_EVENT(2, &todo, sizeof(todo));
  182. switch (todo) {
  183. case UC_TODO_RETRY:
  184. return IO_STATUS_ERROR;
  185. case UC_TODO_RETRY_ON_NEW_PATH:
  186. return IO_PATH_ERROR;
  187. case UC_TODO_STOP:
  188. return IO_REJECTED;
  189. default:
  190. return IO_STATUS_ERROR;
  191. }
  192. }
  193. /* Assume that unexpected SENSE data implies an error. */
  194. return IO_STATUS_ERROR;
  195. }
  196. /* Check for channel errors. */
  197. if (scsw->cstat != 0)
  198. return IO_STATUS_ERROR;
  199. /* Check for device errors. */
  200. if (scsw->dstat & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END))
  201. return IO_STATUS_ERROR;
  202. /* Check for final state. */
  203. if (!(scsw->dstat & DEV_STAT_DEV_END))
  204. return IO_RUNNING;
  205. /* Check for other improper status. */
  206. if (scsw->cc == 1 && (scsw->stctl & SCSW_STCTL_ALERT_STATUS))
  207. return IO_STATUS_ERROR;
  208. return IO_DONE;
  209. }
  210. /*
  211. * Log ccw request status.
  212. */
  213. static void ccwreq_log_status(struct ccw_device *cdev, enum io_status status)
  214. {
  215. struct ccw_request *req = &cdev->private->req;
  216. struct {
  217. struct ccw_dev_id dev_id;
  218. u16 retries;
  219. u8 lpm;
  220. u8 status;
  221. } __attribute__ ((packed)) data;
  222. data.dev_id = cdev->private->dev_id;
  223. data.retries = req->retries;
  224. data.lpm = (u8) req->mask;
  225. data.status = (u8) status;
  226. CIO_TRACE_EVENT(2, "reqstat");
  227. CIO_HEX_EVENT(2, &data, sizeof(data));
  228. }
  229. /**
  230. * ccw_request_handler - interrupt handler for I/O request procedure.
  231. * @cdev: ccw device
  232. *
  233. * Handle interrupt during I/O request procedure.
  234. */
  235. void ccw_request_handler(struct ccw_device *cdev)
  236. {
  237. struct irb *irb = this_cpu_ptr(&cio_irb);
  238. struct ccw_request *req = &cdev->private->req;
  239. enum io_status status;
  240. int rc = -EOPNOTSUPP;
  241. /* Check status of I/O request. */
  242. status = ccwreq_status(cdev, irb);
  243. if (req->filter)
  244. status = req->filter(cdev, req->data, irb, status);
  245. if (status != IO_RUNNING)
  246. ccw_device_set_timeout(cdev, 0);
  247. if (status != IO_DONE && status != IO_RUNNING)
  248. ccwreq_log_status(cdev, status);
  249. switch (status) {
  250. case IO_DONE:
  251. break;
  252. case IO_RUNNING:
  253. return;
  254. case IO_REJECTED:
  255. goto err;
  256. case IO_PATH_ERROR:
  257. goto out_next_path;
  258. case IO_STATUS_ERROR:
  259. goto out_restart;
  260. case IO_KILLED:
  261. /* Check if request was cancelled on purpose. */
  262. if (req->cancel) {
  263. rc = -EIO;
  264. goto err;
  265. }
  266. goto out_restart;
  267. }
  268. /* Check back with request initiator. */
  269. if (!req->check)
  270. goto out;
  271. switch (req->check(cdev, req->data)) {
  272. case 0:
  273. break;
  274. case -EAGAIN:
  275. goto out_restart;
  276. case -EACCES:
  277. goto out_next_path;
  278. default:
  279. goto err;
  280. }
  281. out:
  282. ccwreq_stop(cdev, 0);
  283. return;
  284. out_next_path:
  285. /* Try next path and restart I/O. */
  286. if (!ccwreq_next_path(cdev)) {
  287. rc = -EACCES;
  288. goto err;
  289. }
  290. out_restart:
  291. /* Restart. */
  292. ccwreq_do(cdev);
  293. return;
  294. err:
  295. ccwreq_stop(cdev, rc);
  296. }
  297. /**
  298. * ccw_request_timeout - timeout handler for I/O request procedure
  299. * @cdev: ccw device
  300. *
  301. * Handle timeout during I/O request procedure.
  302. */
  303. void ccw_request_timeout(struct ccw_device *cdev)
  304. {
  305. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  306. struct ccw_request *req = &cdev->private->req;
  307. int rc = -ENODEV, chp;
  308. if (cio_update_schib(sch))
  309. goto err;
  310. for (chp = 0; chp < 8; chp++) {
  311. if ((0x80 >> chp) & sch->schib.pmcw.lpum)
  312. pr_warn("%s: No interrupt was received within %lus (CS=%02x, DS=%02x, CHPID=%x.%02x)\n",
  313. dev_name(&cdev->dev), req->timeout / HZ,
  314. scsw_cstat(&sch->schib.scsw),
  315. scsw_dstat(&sch->schib.scsw),
  316. sch->schid.cssid,
  317. sch->schib.pmcw.chpid[chp]);
  318. }
  319. if (!ccwreq_next_path(cdev)) {
  320. /* set the final return code for this request */
  321. req->drc = -ETIME;
  322. }
  323. rc = cio_clear(sch);
  324. if (rc)
  325. goto err;
  326. return;
  327. err:
  328. ccwreq_stop(cdev, rc);
  329. }
  330. /**
  331. * ccw_request_notoper - notoper handler for I/O request procedure
  332. * @cdev: ccw device
  333. *
  334. * Handle notoper during I/O request procedure.
  335. */
  336. void ccw_request_notoper(struct ccw_device *cdev)
  337. {
  338. ccwreq_stop(cdev, -ENODEV);
  339. }