ccwreq.c 7.7 KB

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