ccwreq.c 7.3 KB

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