ccwreq.c 8.3 KB

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