ccwreq.c 7.8 KB

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