ccwreq.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. enum uc_todo todo;
  147. /* Perform BASIC SENSE if needed. */
  148. if (ccw_device_accumulate_and_sense(cdev, lcirb))
  149. return IO_RUNNING;
  150. /* Check for halt/clear interrupt. */
  151. if (scsw->fctl & (SCSW_FCTL_HALT_FUNC | SCSW_FCTL_CLEAR_FUNC))
  152. return IO_KILLED;
  153. /* Check for path error. */
  154. if (scsw->cc == 3 || scsw->pno)
  155. return IO_PATH_ERROR;
  156. /* Handle BASIC SENSE data. */
  157. if (irb->esw.esw0.erw.cons) {
  158. CIO_TRACE_EVENT(2, "sensedata");
  159. CIO_HEX_EVENT(2, &cdev->private->dev_id,
  160. sizeof(struct ccw_dev_id));
  161. CIO_HEX_EVENT(2, &cdev->private->irb.ecw, SENSE_MAX_COUNT);
  162. /* Check for command reject. */
  163. if (irb->ecw[0] & SNS0_CMD_REJECT)
  164. return IO_REJECTED;
  165. /* Ask the driver what to do */
  166. if (cdev->drv && cdev->drv->uc_handler) {
  167. todo = cdev->drv->uc_handler(cdev, lcirb);
  168. switch (todo) {
  169. case UC_TODO_RETRY:
  170. return IO_STATUS_ERROR;
  171. case UC_TODO_RETRY_ON_NEW_PATH:
  172. return IO_PATH_ERROR;
  173. case UC_TODO_STOP:
  174. return IO_REJECTED;
  175. default:
  176. return IO_STATUS_ERROR;
  177. }
  178. }
  179. /* Assume that unexpected SENSE data implies an error. */
  180. return IO_STATUS_ERROR;
  181. }
  182. /* Check for channel errors. */
  183. if (scsw->cstat != 0)
  184. return IO_STATUS_ERROR;
  185. /* Check for device errors. */
  186. if (scsw->dstat & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END))
  187. return IO_STATUS_ERROR;
  188. /* Check for final state. */
  189. if (!(scsw->dstat & DEV_STAT_DEV_END))
  190. return IO_RUNNING;
  191. /* Check for other improper status. */
  192. if (scsw->cc == 1 && (scsw->stctl & SCSW_STCTL_ALERT_STATUS))
  193. return IO_STATUS_ERROR;
  194. return IO_DONE;
  195. }
  196. /*
  197. * Log ccw request status.
  198. */
  199. static void ccwreq_log_status(struct ccw_device *cdev, enum io_status status)
  200. {
  201. struct ccw_request *req = &cdev->private->req;
  202. struct {
  203. struct ccw_dev_id dev_id;
  204. u16 retries;
  205. u8 lpm;
  206. u8 status;
  207. } __attribute__ ((packed)) data;
  208. data.dev_id = cdev->private->dev_id;
  209. data.retries = req->retries;
  210. data.lpm = (u8) req->mask;
  211. data.status = (u8) status;
  212. CIO_TRACE_EVENT(2, "reqstat");
  213. CIO_HEX_EVENT(2, &data, sizeof(data));
  214. }
  215. /**
  216. * ccw_request_handler - interrupt handler for I/O request procedure.
  217. * @cdev: ccw device
  218. *
  219. * Handle interrupt during I/O request procedure.
  220. */
  221. void ccw_request_handler(struct ccw_device *cdev)
  222. {
  223. struct irb *irb = (struct irb *)&S390_lowcore.irb;
  224. struct ccw_request *req = &cdev->private->req;
  225. enum io_status status;
  226. int rc = -EOPNOTSUPP;
  227. /* Check status of I/O request. */
  228. status = ccwreq_status(cdev, irb);
  229. if (req->filter)
  230. status = req->filter(cdev, req->data, irb, status);
  231. if (status != IO_RUNNING)
  232. ccw_device_set_timeout(cdev, 0);
  233. if (status != IO_DONE && status != IO_RUNNING)
  234. ccwreq_log_status(cdev, status);
  235. switch (status) {
  236. case IO_DONE:
  237. break;
  238. case IO_RUNNING:
  239. return;
  240. case IO_REJECTED:
  241. goto err;
  242. case IO_PATH_ERROR:
  243. goto out_next_path;
  244. case IO_STATUS_ERROR:
  245. goto out_restart;
  246. case IO_KILLED:
  247. /* Check if request was cancelled on purpose. */
  248. if (req->cancel) {
  249. rc = -EIO;
  250. goto err;
  251. }
  252. goto out_restart;
  253. }
  254. /* Check back with request initiator. */
  255. if (!req->check)
  256. goto out;
  257. switch (req->check(cdev, req->data)) {
  258. case 0:
  259. break;
  260. case -EAGAIN:
  261. goto out_restart;
  262. case -EACCES:
  263. goto out_next_path;
  264. default:
  265. goto err;
  266. }
  267. out:
  268. ccwreq_stop(cdev, 0);
  269. return;
  270. out_next_path:
  271. /* Try next path and restart I/O. */
  272. if (!ccwreq_next_path(cdev)) {
  273. rc = -EACCES;
  274. goto err;
  275. }
  276. out_restart:
  277. /* Restart. */
  278. ccwreq_do(cdev);
  279. return;
  280. err:
  281. ccwreq_stop(cdev, rc);
  282. }
  283. /**
  284. * ccw_request_timeout - timeout handler for I/O request procedure
  285. * @cdev: ccw device
  286. *
  287. * Handle timeout during I/O request procedure.
  288. */
  289. void ccw_request_timeout(struct ccw_device *cdev)
  290. {
  291. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  292. struct ccw_request *req = &cdev->private->req;
  293. int rc;
  294. if (!ccwreq_next_path(cdev)) {
  295. /* set the final return code for this request */
  296. req->drc = -ETIME;
  297. }
  298. rc = cio_clear(sch);
  299. if (rc)
  300. goto err;
  301. return;
  302. err:
  303. ccwreq_stop(cdev, rc);
  304. }
  305. /**
  306. * ccw_request_notoper - notoper handler for I/O request procedure
  307. * @cdev: ccw device
  308. *
  309. * Handle timeout during I/O request procedure.
  310. */
  311. void ccw_request_notoper(struct ccw_device *cdev)
  312. {
  313. ccwreq_stop(cdev, -ENODEV);
  314. }