ccwreq.c 7.3 KB

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