ccwreq.c 7.4 KB

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