device_fsm.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. /*
  2. * drivers/s390/cio/device_fsm.c
  3. * finite state machine for device handling
  4. *
  5. * Copyright IBM Corp. 2002,2008
  6. * Author(s): Cornelia Huck (cornelia.huck@de.ibm.com)
  7. * Martin Schwidefsky (schwidefsky@de.ibm.com)
  8. */
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/jiffies.h>
  12. #include <linux/string.h>
  13. #include <asm/ccwdev.h>
  14. #include <asm/cio.h>
  15. #include <asm/chpid.h>
  16. #include "cio.h"
  17. #include "cio_debug.h"
  18. #include "css.h"
  19. #include "device.h"
  20. #include "chsc.h"
  21. #include "ioasm.h"
  22. #include "chp.h"
  23. static int timeout_log_enabled;
  24. static int __init ccw_timeout_log_setup(char *unused)
  25. {
  26. timeout_log_enabled = 1;
  27. return 1;
  28. }
  29. __setup("ccw_timeout_log", ccw_timeout_log_setup);
  30. static void ccw_timeout_log(struct ccw_device *cdev)
  31. {
  32. struct schib schib;
  33. struct subchannel *sch;
  34. struct io_subchannel_private *private;
  35. union orb *orb;
  36. int cc;
  37. sch = to_subchannel(cdev->dev.parent);
  38. private = to_io_private(sch);
  39. orb = &private->orb;
  40. cc = stsch_err(sch->schid, &schib);
  41. printk(KERN_WARNING "cio: ccw device timeout occurred at %llx, "
  42. "device information:\n", get_clock());
  43. printk(KERN_WARNING "cio: orb:\n");
  44. print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
  45. orb, sizeof(*orb), 0);
  46. printk(KERN_WARNING "cio: ccw device bus id: %s\n",
  47. dev_name(&cdev->dev));
  48. printk(KERN_WARNING "cio: subchannel bus id: %s\n",
  49. dev_name(&sch->dev));
  50. printk(KERN_WARNING "cio: subchannel lpm: %02x, opm: %02x, "
  51. "vpm: %02x\n", sch->lpm, sch->opm, sch->vpm);
  52. if (orb->tm.b) {
  53. printk(KERN_WARNING "cio: orb indicates transport mode\n");
  54. printk(KERN_WARNING "cio: last tcw:\n");
  55. print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
  56. (void *)(addr_t)orb->tm.tcw,
  57. sizeof(struct tcw), 0);
  58. } else {
  59. printk(KERN_WARNING "cio: orb indicates command mode\n");
  60. if ((void *)(addr_t)orb->cmd.cpa == &private->sense_ccw ||
  61. (void *)(addr_t)orb->cmd.cpa == cdev->private->iccws)
  62. printk(KERN_WARNING "cio: last channel program "
  63. "(intern):\n");
  64. else
  65. printk(KERN_WARNING "cio: last channel program:\n");
  66. print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
  67. (void *)(addr_t)orb->cmd.cpa,
  68. sizeof(struct ccw1), 0);
  69. }
  70. printk(KERN_WARNING "cio: ccw device state: %d\n",
  71. cdev->private->state);
  72. printk(KERN_WARNING "cio: store subchannel returned: cc=%d\n", cc);
  73. printk(KERN_WARNING "cio: schib:\n");
  74. print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
  75. &schib, sizeof(schib), 0);
  76. printk(KERN_WARNING "cio: ccw device flags:\n");
  77. print_hex_dump(KERN_WARNING, "cio: ", DUMP_PREFIX_NONE, 16, 1,
  78. &cdev->private->flags, sizeof(cdev->private->flags), 0);
  79. }
  80. /*
  81. * Timeout function. It just triggers a DEV_EVENT_TIMEOUT.
  82. */
  83. static void
  84. ccw_device_timeout(unsigned long data)
  85. {
  86. struct ccw_device *cdev;
  87. cdev = (struct ccw_device *) data;
  88. spin_lock_irq(cdev->ccwlock);
  89. if (timeout_log_enabled)
  90. ccw_timeout_log(cdev);
  91. dev_fsm_event(cdev, DEV_EVENT_TIMEOUT);
  92. spin_unlock_irq(cdev->ccwlock);
  93. }
  94. /*
  95. * Set timeout
  96. */
  97. void
  98. ccw_device_set_timeout(struct ccw_device *cdev, int expires)
  99. {
  100. if (expires == 0) {
  101. del_timer(&cdev->private->timer);
  102. return;
  103. }
  104. if (timer_pending(&cdev->private->timer)) {
  105. if (mod_timer(&cdev->private->timer, jiffies + expires))
  106. return;
  107. }
  108. cdev->private->timer.function = ccw_device_timeout;
  109. cdev->private->timer.data = (unsigned long) cdev;
  110. cdev->private->timer.expires = jiffies + expires;
  111. add_timer(&cdev->private->timer);
  112. }
  113. /*
  114. * Cancel running i/o. This is called repeatedly since halt/clear are
  115. * asynchronous operations. We do one try with cio_cancel, two tries
  116. * with cio_halt, 255 tries with cio_clear. If everythings fails panic.
  117. * Returns 0 if device now idle, -ENODEV for device not operational and
  118. * -EBUSY if an interrupt is expected (either from halt/clear or from a
  119. * status pending).
  120. */
  121. int
  122. ccw_device_cancel_halt_clear(struct ccw_device *cdev)
  123. {
  124. struct subchannel *sch;
  125. int ret;
  126. sch = to_subchannel(cdev->dev.parent);
  127. if (cio_update_schib(sch))
  128. return -ENODEV;
  129. if (!sch->schib.pmcw.ena)
  130. /* Not operational -> done. */
  131. return 0;
  132. /* Stage 1: cancel io. */
  133. if (!(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_HALT_PEND) &&
  134. !(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_CLEAR_PEND)) {
  135. if (!scsw_is_tm(&sch->schib.scsw)) {
  136. ret = cio_cancel(sch);
  137. if (ret != -EINVAL)
  138. return ret;
  139. }
  140. /* cancel io unsuccessful or not applicable (transport mode).
  141. * Continue with asynchronous instructions. */
  142. cdev->private->iretry = 3; /* 3 halt retries. */
  143. }
  144. if (!(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_CLEAR_PEND)) {
  145. /* Stage 2: halt io. */
  146. if (cdev->private->iretry) {
  147. cdev->private->iretry--;
  148. ret = cio_halt(sch);
  149. if (ret != -EBUSY)
  150. return (ret == 0) ? -EBUSY : ret;
  151. }
  152. /* halt io unsuccessful. */
  153. cdev->private->iretry = 255; /* 255 clear retries. */
  154. }
  155. /* Stage 3: clear io. */
  156. if (cdev->private->iretry) {
  157. cdev->private->iretry--;
  158. ret = cio_clear (sch);
  159. return (ret == 0) ? -EBUSY : ret;
  160. }
  161. /* Function was unsuccessful */
  162. CIO_MSG_EVENT(0, "0.%x.%04x: could not stop I/O\n",
  163. cdev->private->dev_id.ssid, cdev->private->dev_id.devno);
  164. return -EIO;
  165. }
  166. void ccw_device_update_sense_data(struct ccw_device *cdev)
  167. {
  168. memset(&cdev->id, 0, sizeof(cdev->id));
  169. cdev->id.cu_type = cdev->private->senseid.cu_type;
  170. cdev->id.cu_model = cdev->private->senseid.cu_model;
  171. cdev->id.dev_type = cdev->private->senseid.dev_type;
  172. cdev->id.dev_model = cdev->private->senseid.dev_model;
  173. }
  174. int ccw_device_test_sense_data(struct ccw_device *cdev)
  175. {
  176. return cdev->id.cu_type == cdev->private->senseid.cu_type &&
  177. cdev->id.cu_model == cdev->private->senseid.cu_model &&
  178. cdev->id.dev_type == cdev->private->senseid.dev_type &&
  179. cdev->id.dev_model == cdev->private->senseid.dev_model;
  180. }
  181. /*
  182. * The machine won't give us any notification by machine check if a chpid has
  183. * been varied online on the SE so we have to find out by magic (i. e. driving
  184. * the channel subsystem to device selection and updating our path masks).
  185. */
  186. static void
  187. __recover_lost_chpids(struct subchannel *sch, int old_lpm)
  188. {
  189. int mask, i;
  190. struct chp_id chpid;
  191. chp_id_init(&chpid);
  192. for (i = 0; i<8; i++) {
  193. mask = 0x80 >> i;
  194. if (!(sch->lpm & mask))
  195. continue;
  196. if (old_lpm & mask)
  197. continue;
  198. chpid.id = sch->schib.pmcw.chpid[i];
  199. if (!chp_is_registered(chpid))
  200. css_schedule_eval_all();
  201. }
  202. }
  203. /*
  204. * Stop device recognition.
  205. */
  206. static void
  207. ccw_device_recog_done(struct ccw_device *cdev, int state)
  208. {
  209. struct subchannel *sch;
  210. int old_lpm;
  211. sch = to_subchannel(cdev->dev.parent);
  212. if (cio_disable_subchannel(sch))
  213. state = DEV_STATE_NOT_OPER;
  214. /*
  215. * Now that we tried recognition, we have performed device selection
  216. * through ssch() and the path information is up to date.
  217. */
  218. old_lpm = sch->lpm;
  219. /* Check since device may again have become not operational. */
  220. if (cio_update_schib(sch))
  221. state = DEV_STATE_NOT_OPER;
  222. else
  223. sch->lpm = sch->schib.pmcw.pam & sch->opm;
  224. if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID)
  225. /* Force reprobe on all chpids. */
  226. old_lpm = 0;
  227. if (sch->lpm != old_lpm)
  228. __recover_lost_chpids(sch, old_lpm);
  229. if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID &&
  230. (state == DEV_STATE_NOT_OPER || state == DEV_STATE_BOXED)) {
  231. cdev->private->flags.recog_done = 1;
  232. cdev->private->state = DEV_STATE_DISCONNECTED;
  233. wake_up(&cdev->private->wait_q);
  234. return;
  235. }
  236. if (cdev->private->flags.resuming) {
  237. cdev->private->state = state;
  238. cdev->private->flags.recog_done = 1;
  239. wake_up(&cdev->private->wait_q);
  240. return;
  241. }
  242. switch (state) {
  243. case DEV_STATE_NOT_OPER:
  244. break;
  245. case DEV_STATE_OFFLINE:
  246. if (!cdev->online) {
  247. ccw_device_update_sense_data(cdev);
  248. break;
  249. }
  250. cdev->private->state = DEV_STATE_OFFLINE;
  251. cdev->private->flags.recog_done = 1;
  252. if (ccw_device_test_sense_data(cdev)) {
  253. cdev->private->flags.donotify = 1;
  254. ccw_device_online(cdev);
  255. wake_up(&cdev->private->wait_q);
  256. } else {
  257. ccw_device_update_sense_data(cdev);
  258. ccw_device_sched_todo(cdev, CDEV_TODO_REBIND);
  259. }
  260. return;
  261. case DEV_STATE_BOXED:
  262. if (cdev->id.cu_type != 0) { /* device was recognized before */
  263. cdev->private->flags.recog_done = 1;
  264. cdev->private->state = DEV_STATE_BOXED;
  265. wake_up(&cdev->private->wait_q);
  266. return;
  267. }
  268. break;
  269. }
  270. cdev->private->state = state;
  271. io_subchannel_recog_done(cdev);
  272. wake_up(&cdev->private->wait_q);
  273. }
  274. /*
  275. * Function called from device_id.c after sense id has completed.
  276. */
  277. void
  278. ccw_device_sense_id_done(struct ccw_device *cdev, int err)
  279. {
  280. switch (err) {
  281. case 0:
  282. ccw_device_recog_done(cdev, DEV_STATE_OFFLINE);
  283. break;
  284. case -ETIME: /* Sense id stopped by timeout. */
  285. ccw_device_recog_done(cdev, DEV_STATE_BOXED);
  286. break;
  287. default:
  288. ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
  289. break;
  290. }
  291. }
  292. /**
  293. * ccw_device_notify() - inform the device's driver about an event
  294. * @cdev: device for which an event occurred
  295. * @event: event that occurred
  296. *
  297. * Returns:
  298. * -%EINVAL if the device is offline or has no driver.
  299. * -%EOPNOTSUPP if the device's driver has no notifier registered.
  300. * %NOTIFY_OK if the driver wants to keep the device.
  301. * %NOTIFY_BAD if the driver doesn't want to keep the device.
  302. */
  303. int ccw_device_notify(struct ccw_device *cdev, int event)
  304. {
  305. int ret = -EINVAL;
  306. if (!cdev->drv)
  307. goto out;
  308. if (!cdev->online)
  309. goto out;
  310. CIO_MSG_EVENT(2, "notify called for 0.%x.%04x, event=%d\n",
  311. cdev->private->dev_id.ssid, cdev->private->dev_id.devno,
  312. event);
  313. if (!cdev->drv->notify) {
  314. ret = -EOPNOTSUPP;
  315. goto out;
  316. }
  317. if (cdev->drv->notify(cdev, event))
  318. ret = NOTIFY_OK;
  319. else
  320. ret = NOTIFY_BAD;
  321. out:
  322. return ret;
  323. }
  324. static void ccw_device_oper_notify(struct ccw_device *cdev)
  325. {
  326. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  327. if (ccw_device_notify(cdev, CIO_OPER) == NOTIFY_OK) {
  328. /* Reenable channel measurements, if needed. */
  329. ccw_device_sched_todo(cdev, CDEV_TODO_ENABLE_CMF);
  330. /* Save indication for new paths. */
  331. cdev->private->path_new_mask = sch->vpm;
  332. return;
  333. }
  334. /* Driver doesn't want device back. */
  335. ccw_device_set_notoper(cdev);
  336. ccw_device_sched_todo(cdev, CDEV_TODO_REBIND);
  337. }
  338. /*
  339. * Finished with online/offline processing.
  340. */
  341. static void
  342. ccw_device_done(struct ccw_device *cdev, int state)
  343. {
  344. struct subchannel *sch;
  345. sch = to_subchannel(cdev->dev.parent);
  346. ccw_device_set_timeout(cdev, 0);
  347. if (state != DEV_STATE_ONLINE)
  348. cio_disable_subchannel(sch);
  349. /* Reset device status. */
  350. memset(&cdev->private->irb, 0, sizeof(struct irb));
  351. cdev->private->state = state;
  352. switch (state) {
  353. case DEV_STATE_BOXED:
  354. CIO_MSG_EVENT(0, "Boxed device %04x on subchannel %04x\n",
  355. cdev->private->dev_id.devno, sch->schid.sch_no);
  356. if (cdev->online &&
  357. ccw_device_notify(cdev, CIO_BOXED) != NOTIFY_OK)
  358. ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
  359. cdev->private->flags.donotify = 0;
  360. break;
  361. case DEV_STATE_NOT_OPER:
  362. CIO_MSG_EVENT(0, "Device %04x gone on subchannel %04x\n",
  363. cdev->private->dev_id.devno, sch->schid.sch_no);
  364. if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
  365. ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
  366. else
  367. ccw_device_set_disconnected(cdev);
  368. cdev->private->flags.donotify = 0;
  369. break;
  370. case DEV_STATE_DISCONNECTED:
  371. CIO_MSG_EVENT(0, "Disconnected device %04x on subchannel "
  372. "%04x\n", cdev->private->dev_id.devno,
  373. sch->schid.sch_no);
  374. if (ccw_device_notify(cdev, CIO_NO_PATH) != NOTIFY_OK) {
  375. cdev->private->state = DEV_STATE_NOT_OPER;
  376. ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
  377. } else
  378. ccw_device_set_disconnected(cdev);
  379. cdev->private->flags.donotify = 0;
  380. break;
  381. default:
  382. break;
  383. }
  384. if (cdev->private->flags.donotify) {
  385. cdev->private->flags.donotify = 0;
  386. ccw_device_oper_notify(cdev);
  387. }
  388. wake_up(&cdev->private->wait_q);
  389. }
  390. /*
  391. * Start device recognition.
  392. */
  393. void ccw_device_recognition(struct ccw_device *cdev)
  394. {
  395. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  396. /*
  397. * We used to start here with a sense pgid to find out whether a device
  398. * is locked by someone else. Unfortunately, the sense pgid command
  399. * code has other meanings on devices predating the path grouping
  400. * algorithm, so we start with sense id and box the device after an
  401. * timeout (or if sense pgid during path verification detects the device
  402. * is locked, as may happen on newer devices).
  403. */
  404. cdev->private->flags.recog_done = 0;
  405. cdev->private->state = DEV_STATE_SENSE_ID;
  406. if (cio_enable_subchannel(sch, (u32) (addr_t) sch)) {
  407. ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
  408. return;
  409. }
  410. ccw_device_sense_id_start(cdev);
  411. }
  412. /*
  413. * Handle events for states that use the ccw request infrastructure.
  414. */
  415. static void ccw_device_request_event(struct ccw_device *cdev, enum dev_event e)
  416. {
  417. switch (e) {
  418. case DEV_EVENT_NOTOPER:
  419. ccw_request_notoper(cdev);
  420. break;
  421. case DEV_EVENT_INTERRUPT:
  422. ccw_request_handler(cdev);
  423. break;
  424. case DEV_EVENT_TIMEOUT:
  425. ccw_request_timeout(cdev);
  426. break;
  427. default:
  428. break;
  429. }
  430. }
  431. static void ccw_device_report_path_events(struct ccw_device *cdev)
  432. {
  433. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  434. int path_event[8];
  435. int chp, mask;
  436. for (chp = 0, mask = 0x80; chp < 8; chp++, mask >>= 1) {
  437. path_event[chp] = PE_NONE;
  438. if (mask & cdev->private->path_gone_mask & ~(sch->vpm))
  439. path_event[chp] |= PE_PATH_GONE;
  440. if (mask & cdev->private->path_new_mask & sch->vpm)
  441. path_event[chp] |= PE_PATH_AVAILABLE;
  442. if (mask & cdev->private->pgid_reset_mask & sch->vpm)
  443. path_event[chp] |= PE_PATHGROUP_ESTABLISHED;
  444. }
  445. if (cdev->online && cdev->drv->path_event)
  446. cdev->drv->path_event(cdev, path_event);
  447. }
  448. static void ccw_device_reset_path_events(struct ccw_device *cdev)
  449. {
  450. cdev->private->path_gone_mask = 0;
  451. cdev->private->path_new_mask = 0;
  452. cdev->private->pgid_reset_mask = 0;
  453. }
  454. void
  455. ccw_device_verify_done(struct ccw_device *cdev, int err)
  456. {
  457. struct subchannel *sch;
  458. sch = to_subchannel(cdev->dev.parent);
  459. /* Update schib - pom may have changed. */
  460. if (cio_update_schib(sch)) {
  461. err = -ENODEV;
  462. goto callback;
  463. }
  464. /* Update lpm with verified path mask. */
  465. sch->lpm = sch->vpm;
  466. /* Repeat path verification? */
  467. if (cdev->private->flags.doverify) {
  468. ccw_device_verify_start(cdev);
  469. return;
  470. }
  471. callback:
  472. switch (err) {
  473. case 0:
  474. ccw_device_done(cdev, DEV_STATE_ONLINE);
  475. /* Deliver fake irb to device driver, if needed. */
  476. if (cdev->private->flags.fake_irb) {
  477. memset(&cdev->private->irb, 0, sizeof(struct irb));
  478. cdev->private->irb.scsw.cmd.cc = 1;
  479. cdev->private->irb.scsw.cmd.fctl = SCSW_FCTL_START_FUNC;
  480. cdev->private->irb.scsw.cmd.actl = SCSW_ACTL_START_PEND;
  481. cdev->private->irb.scsw.cmd.stctl =
  482. SCSW_STCTL_STATUS_PEND;
  483. cdev->private->flags.fake_irb = 0;
  484. if (cdev->handler)
  485. cdev->handler(cdev, cdev->private->intparm,
  486. &cdev->private->irb);
  487. memset(&cdev->private->irb, 0, sizeof(struct irb));
  488. }
  489. ccw_device_report_path_events(cdev);
  490. break;
  491. case -ETIME:
  492. case -EUSERS:
  493. /* Reset oper notify indication after verify error. */
  494. cdev->private->flags.donotify = 0;
  495. ccw_device_done(cdev, DEV_STATE_BOXED);
  496. break;
  497. case -EACCES:
  498. /* Reset oper notify indication after verify error. */
  499. cdev->private->flags.donotify = 0;
  500. ccw_device_done(cdev, DEV_STATE_DISCONNECTED);
  501. break;
  502. default:
  503. /* Reset oper notify indication after verify error. */
  504. cdev->private->flags.donotify = 0;
  505. ccw_device_done(cdev, DEV_STATE_NOT_OPER);
  506. break;
  507. }
  508. ccw_device_reset_path_events(cdev);
  509. }
  510. /*
  511. * Get device online.
  512. */
  513. int
  514. ccw_device_online(struct ccw_device *cdev)
  515. {
  516. struct subchannel *sch;
  517. int ret;
  518. if ((cdev->private->state != DEV_STATE_OFFLINE) &&
  519. (cdev->private->state != DEV_STATE_BOXED))
  520. return -EINVAL;
  521. sch = to_subchannel(cdev->dev.parent);
  522. ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
  523. if (ret != 0) {
  524. /* Couldn't enable the subchannel for i/o. Sick device. */
  525. if (ret == -ENODEV)
  526. dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
  527. return ret;
  528. }
  529. /* Start initial path verification. */
  530. cdev->private->state = DEV_STATE_VERIFY;
  531. ccw_device_verify_start(cdev);
  532. return 0;
  533. }
  534. void
  535. ccw_device_disband_done(struct ccw_device *cdev, int err)
  536. {
  537. switch (err) {
  538. case 0:
  539. ccw_device_done(cdev, DEV_STATE_OFFLINE);
  540. break;
  541. case -ETIME:
  542. ccw_device_done(cdev, DEV_STATE_BOXED);
  543. break;
  544. default:
  545. cdev->private->flags.donotify = 0;
  546. ccw_device_done(cdev, DEV_STATE_NOT_OPER);
  547. break;
  548. }
  549. }
  550. /*
  551. * Shutdown device.
  552. */
  553. int
  554. ccw_device_offline(struct ccw_device *cdev)
  555. {
  556. struct subchannel *sch;
  557. /* Allow ccw_device_offline while disconnected. */
  558. if (cdev->private->state == DEV_STATE_DISCONNECTED ||
  559. cdev->private->state == DEV_STATE_NOT_OPER) {
  560. cdev->private->flags.donotify = 0;
  561. ccw_device_done(cdev, DEV_STATE_NOT_OPER);
  562. return 0;
  563. }
  564. if (cdev->private->state == DEV_STATE_BOXED) {
  565. ccw_device_done(cdev, DEV_STATE_BOXED);
  566. return 0;
  567. }
  568. if (ccw_device_is_orphan(cdev)) {
  569. ccw_device_done(cdev, DEV_STATE_OFFLINE);
  570. return 0;
  571. }
  572. sch = to_subchannel(cdev->dev.parent);
  573. if (cio_update_schib(sch))
  574. return -ENODEV;
  575. if (scsw_actl(&sch->schib.scsw) != 0)
  576. return -EBUSY;
  577. if (cdev->private->state != DEV_STATE_ONLINE)
  578. return -EINVAL;
  579. /* Are we doing path grouping? */
  580. if (!cdev->private->flags.pgroup) {
  581. /* No, set state offline immediately. */
  582. ccw_device_done(cdev, DEV_STATE_OFFLINE);
  583. return 0;
  584. }
  585. /* Start Set Path Group commands. */
  586. cdev->private->state = DEV_STATE_DISBAND_PGID;
  587. ccw_device_disband_start(cdev);
  588. return 0;
  589. }
  590. /*
  591. * Handle not operational event in non-special state.
  592. */
  593. static void ccw_device_generic_notoper(struct ccw_device *cdev,
  594. enum dev_event dev_event)
  595. {
  596. if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
  597. ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
  598. else
  599. ccw_device_set_disconnected(cdev);
  600. }
  601. /*
  602. * Handle path verification event in offline state.
  603. */
  604. static void ccw_device_offline_verify(struct ccw_device *cdev,
  605. enum dev_event dev_event)
  606. {
  607. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  608. css_schedule_eval(sch->schid);
  609. }
  610. /*
  611. * Handle path verification event.
  612. */
  613. static void
  614. ccw_device_online_verify(struct ccw_device *cdev, enum dev_event dev_event)
  615. {
  616. struct subchannel *sch;
  617. if (cdev->private->state == DEV_STATE_W4SENSE) {
  618. cdev->private->flags.doverify = 1;
  619. return;
  620. }
  621. sch = to_subchannel(cdev->dev.parent);
  622. /*
  623. * Since we might not just be coming from an interrupt from the
  624. * subchannel we have to update the schib.
  625. */
  626. if (cio_update_schib(sch)) {
  627. ccw_device_verify_done(cdev, -ENODEV);
  628. return;
  629. }
  630. if (scsw_actl(&sch->schib.scsw) != 0 ||
  631. (scsw_stctl(&sch->schib.scsw) & SCSW_STCTL_STATUS_PEND) ||
  632. (scsw_stctl(&cdev->private->irb.scsw) & SCSW_STCTL_STATUS_PEND)) {
  633. /*
  634. * No final status yet or final status not yet delivered
  635. * to the device driver. Can't do path verification now,
  636. * delay until final status was delivered.
  637. */
  638. cdev->private->flags.doverify = 1;
  639. return;
  640. }
  641. /* Device is idle, we can do the path verification. */
  642. cdev->private->state = DEV_STATE_VERIFY;
  643. ccw_device_verify_start(cdev);
  644. }
  645. /*
  646. * Handle path verification event in boxed state.
  647. */
  648. static void ccw_device_boxed_verify(struct ccw_device *cdev,
  649. enum dev_event dev_event)
  650. {
  651. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  652. if (cdev->online) {
  653. if (cio_enable_subchannel(sch, (u32) (addr_t) sch))
  654. ccw_device_done(cdev, DEV_STATE_NOT_OPER);
  655. else
  656. ccw_device_online_verify(cdev, dev_event);
  657. } else
  658. css_schedule_eval(sch->schid);
  659. }
  660. /*
  661. * Got an interrupt for a normal io (state online).
  662. */
  663. static void
  664. ccw_device_irq(struct ccw_device *cdev, enum dev_event dev_event)
  665. {
  666. struct irb *irb;
  667. int is_cmd;
  668. irb = (struct irb *)&S390_lowcore.irb;
  669. is_cmd = !scsw_is_tm(&irb->scsw);
  670. /* Check for unsolicited interrupt. */
  671. if (!scsw_is_solicited(&irb->scsw)) {
  672. if (is_cmd && (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) &&
  673. !irb->esw.esw0.erw.cons) {
  674. /* Unit check but no sense data. Need basic sense. */
  675. if (ccw_device_do_sense(cdev, irb) != 0)
  676. goto call_handler_unsol;
  677. memcpy(&cdev->private->irb, irb, sizeof(struct irb));
  678. cdev->private->state = DEV_STATE_W4SENSE;
  679. cdev->private->intparm = 0;
  680. return;
  681. }
  682. call_handler_unsol:
  683. if (cdev->handler)
  684. cdev->handler (cdev, 0, irb);
  685. if (cdev->private->flags.doverify)
  686. ccw_device_online_verify(cdev, 0);
  687. return;
  688. }
  689. /* Accumulate status and find out if a basic sense is needed. */
  690. ccw_device_accumulate_irb(cdev, irb);
  691. if (is_cmd && cdev->private->flags.dosense) {
  692. if (ccw_device_do_sense(cdev, irb) == 0) {
  693. cdev->private->state = DEV_STATE_W4SENSE;
  694. }
  695. return;
  696. }
  697. /* Call the handler. */
  698. if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
  699. /* Start delayed path verification. */
  700. ccw_device_online_verify(cdev, 0);
  701. }
  702. /*
  703. * Got an timeout in online state.
  704. */
  705. static void
  706. ccw_device_online_timeout(struct ccw_device *cdev, enum dev_event dev_event)
  707. {
  708. int ret;
  709. ccw_device_set_timeout(cdev, 0);
  710. cdev->private->iretry = 255;
  711. ret = ccw_device_cancel_halt_clear(cdev);
  712. if (ret == -EBUSY) {
  713. ccw_device_set_timeout(cdev, 3*HZ);
  714. cdev->private->state = DEV_STATE_TIMEOUT_KILL;
  715. return;
  716. }
  717. if (ret)
  718. dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
  719. else if (cdev->handler)
  720. cdev->handler(cdev, cdev->private->intparm,
  721. ERR_PTR(-ETIMEDOUT));
  722. }
  723. /*
  724. * Got an interrupt for a basic sense.
  725. */
  726. static void
  727. ccw_device_w4sense(struct ccw_device *cdev, enum dev_event dev_event)
  728. {
  729. struct irb *irb;
  730. irb = (struct irb *)&S390_lowcore.irb;
  731. /* Check for unsolicited interrupt. */
  732. if (scsw_stctl(&irb->scsw) ==
  733. (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) {
  734. if (scsw_cc(&irb->scsw) == 1)
  735. /* Basic sense hasn't started. Try again. */
  736. ccw_device_do_sense(cdev, irb);
  737. else {
  738. CIO_MSG_EVENT(0, "0.%x.%04x: unsolicited "
  739. "interrupt during w4sense...\n",
  740. cdev->private->dev_id.ssid,
  741. cdev->private->dev_id.devno);
  742. if (cdev->handler)
  743. cdev->handler (cdev, 0, irb);
  744. }
  745. return;
  746. }
  747. /*
  748. * Check if a halt or clear has been issued in the meanwhile. If yes,
  749. * only deliver the halt/clear interrupt to the device driver as if it
  750. * had killed the original request.
  751. */
  752. if (scsw_fctl(&irb->scsw) &
  753. (SCSW_FCTL_CLEAR_FUNC | SCSW_FCTL_HALT_FUNC)) {
  754. cdev->private->flags.dosense = 0;
  755. memset(&cdev->private->irb, 0, sizeof(struct irb));
  756. ccw_device_accumulate_irb(cdev, irb);
  757. goto call_handler;
  758. }
  759. /* Add basic sense info to irb. */
  760. ccw_device_accumulate_basic_sense(cdev, irb);
  761. if (cdev->private->flags.dosense) {
  762. /* Another basic sense is needed. */
  763. ccw_device_do_sense(cdev, irb);
  764. return;
  765. }
  766. call_handler:
  767. cdev->private->state = DEV_STATE_ONLINE;
  768. /* In case sensing interfered with setting the device online */
  769. wake_up(&cdev->private->wait_q);
  770. /* Call the handler. */
  771. if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
  772. /* Start delayed path verification. */
  773. ccw_device_online_verify(cdev, 0);
  774. }
  775. static void
  776. ccw_device_killing_irq(struct ccw_device *cdev, enum dev_event dev_event)
  777. {
  778. ccw_device_set_timeout(cdev, 0);
  779. /* Start delayed path verification. */
  780. ccw_device_online_verify(cdev, 0);
  781. /* OK, i/o is dead now. Call interrupt handler. */
  782. if (cdev->handler)
  783. cdev->handler(cdev, cdev->private->intparm,
  784. ERR_PTR(-EIO));
  785. }
  786. static void
  787. ccw_device_killing_timeout(struct ccw_device *cdev, enum dev_event dev_event)
  788. {
  789. int ret;
  790. ret = ccw_device_cancel_halt_clear(cdev);
  791. if (ret == -EBUSY) {
  792. ccw_device_set_timeout(cdev, 3*HZ);
  793. return;
  794. }
  795. /* Start delayed path verification. */
  796. ccw_device_online_verify(cdev, 0);
  797. if (cdev->handler)
  798. cdev->handler(cdev, cdev->private->intparm,
  799. ERR_PTR(-EIO));
  800. }
  801. void ccw_device_kill_io(struct ccw_device *cdev)
  802. {
  803. int ret;
  804. cdev->private->iretry = 255;
  805. ret = ccw_device_cancel_halt_clear(cdev);
  806. if (ret == -EBUSY) {
  807. ccw_device_set_timeout(cdev, 3*HZ);
  808. cdev->private->state = DEV_STATE_TIMEOUT_KILL;
  809. return;
  810. }
  811. /* Start delayed path verification. */
  812. ccw_device_online_verify(cdev, 0);
  813. if (cdev->handler)
  814. cdev->handler(cdev, cdev->private->intparm,
  815. ERR_PTR(-EIO));
  816. }
  817. static void
  818. ccw_device_delay_verify(struct ccw_device *cdev, enum dev_event dev_event)
  819. {
  820. /* Start verification after current task finished. */
  821. cdev->private->flags.doverify = 1;
  822. }
  823. static void
  824. ccw_device_start_id(struct ccw_device *cdev, enum dev_event dev_event)
  825. {
  826. struct subchannel *sch;
  827. sch = to_subchannel(cdev->dev.parent);
  828. if (cio_enable_subchannel(sch, (u32)(addr_t)sch) != 0)
  829. /* Couldn't enable the subchannel for i/o. Sick device. */
  830. return;
  831. cdev->private->state = DEV_STATE_DISCONNECTED_SENSE_ID;
  832. ccw_device_sense_id_start(cdev);
  833. }
  834. void ccw_device_trigger_reprobe(struct ccw_device *cdev)
  835. {
  836. struct subchannel *sch;
  837. if (cdev->private->state != DEV_STATE_DISCONNECTED)
  838. return;
  839. sch = to_subchannel(cdev->dev.parent);
  840. /* Update some values. */
  841. if (cio_update_schib(sch))
  842. return;
  843. /*
  844. * The pim, pam, pom values may not be accurate, but they are the best
  845. * we have before performing device selection :/
  846. */
  847. sch->lpm = sch->schib.pmcw.pam & sch->opm;
  848. /*
  849. * Use the initial configuration since we can't be shure that the old
  850. * paths are valid.
  851. */
  852. io_subchannel_init_config(sch);
  853. if (cio_commit_config(sch))
  854. return;
  855. /* We should also udate ssd info, but this has to wait. */
  856. /* Check if this is another device which appeared on the same sch. */
  857. if (sch->schib.pmcw.dev != cdev->private->dev_id.devno)
  858. css_schedule_eval(sch->schid);
  859. else
  860. ccw_device_start_id(cdev, 0);
  861. }
  862. static void ccw_device_disabled_irq(struct ccw_device *cdev,
  863. enum dev_event dev_event)
  864. {
  865. struct subchannel *sch;
  866. sch = to_subchannel(cdev->dev.parent);
  867. /*
  868. * An interrupt in a disabled state means a previous disable was not
  869. * successful - should not happen, but we try to disable again.
  870. */
  871. cio_disable_subchannel(sch);
  872. }
  873. static void
  874. ccw_device_change_cmfstate(struct ccw_device *cdev, enum dev_event dev_event)
  875. {
  876. retry_set_schib(cdev);
  877. cdev->private->state = DEV_STATE_ONLINE;
  878. dev_fsm_event(cdev, dev_event);
  879. }
  880. static void ccw_device_update_cmfblock(struct ccw_device *cdev,
  881. enum dev_event dev_event)
  882. {
  883. cmf_retry_copy_block(cdev);
  884. cdev->private->state = DEV_STATE_ONLINE;
  885. dev_fsm_event(cdev, dev_event);
  886. }
  887. static void
  888. ccw_device_quiesce_done(struct ccw_device *cdev, enum dev_event dev_event)
  889. {
  890. ccw_device_set_timeout(cdev, 0);
  891. cdev->private->state = DEV_STATE_NOT_OPER;
  892. wake_up(&cdev->private->wait_q);
  893. }
  894. static void
  895. ccw_device_quiesce_timeout(struct ccw_device *cdev, enum dev_event dev_event)
  896. {
  897. int ret;
  898. ret = ccw_device_cancel_halt_clear(cdev);
  899. if (ret == -EBUSY) {
  900. ccw_device_set_timeout(cdev, HZ/10);
  901. } else {
  902. cdev->private->state = DEV_STATE_NOT_OPER;
  903. wake_up(&cdev->private->wait_q);
  904. }
  905. }
  906. /*
  907. * No operation action. This is used e.g. to ignore a timeout event in
  908. * state offline.
  909. */
  910. static void
  911. ccw_device_nop(struct ccw_device *cdev, enum dev_event dev_event)
  912. {
  913. }
  914. /*
  915. * device statemachine
  916. */
  917. fsm_func_t *dev_jumptable[NR_DEV_STATES][NR_DEV_EVENTS] = {
  918. [DEV_STATE_NOT_OPER] = {
  919. [DEV_EVENT_NOTOPER] = ccw_device_nop,
  920. [DEV_EVENT_INTERRUPT] = ccw_device_disabled_irq,
  921. [DEV_EVENT_TIMEOUT] = ccw_device_nop,
  922. [DEV_EVENT_VERIFY] = ccw_device_nop,
  923. },
  924. [DEV_STATE_SENSE_PGID] = {
  925. [DEV_EVENT_NOTOPER] = ccw_device_request_event,
  926. [DEV_EVENT_INTERRUPT] = ccw_device_request_event,
  927. [DEV_EVENT_TIMEOUT] = ccw_device_request_event,
  928. [DEV_EVENT_VERIFY] = ccw_device_nop,
  929. },
  930. [DEV_STATE_SENSE_ID] = {
  931. [DEV_EVENT_NOTOPER] = ccw_device_request_event,
  932. [DEV_EVENT_INTERRUPT] = ccw_device_request_event,
  933. [DEV_EVENT_TIMEOUT] = ccw_device_request_event,
  934. [DEV_EVENT_VERIFY] = ccw_device_nop,
  935. },
  936. [DEV_STATE_OFFLINE] = {
  937. [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
  938. [DEV_EVENT_INTERRUPT] = ccw_device_disabled_irq,
  939. [DEV_EVENT_TIMEOUT] = ccw_device_nop,
  940. [DEV_EVENT_VERIFY] = ccw_device_offline_verify,
  941. },
  942. [DEV_STATE_VERIFY] = {
  943. [DEV_EVENT_NOTOPER] = ccw_device_request_event,
  944. [DEV_EVENT_INTERRUPT] = ccw_device_request_event,
  945. [DEV_EVENT_TIMEOUT] = ccw_device_request_event,
  946. [DEV_EVENT_VERIFY] = ccw_device_delay_verify,
  947. },
  948. [DEV_STATE_ONLINE] = {
  949. [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
  950. [DEV_EVENT_INTERRUPT] = ccw_device_irq,
  951. [DEV_EVENT_TIMEOUT] = ccw_device_online_timeout,
  952. [DEV_EVENT_VERIFY] = ccw_device_online_verify,
  953. },
  954. [DEV_STATE_W4SENSE] = {
  955. [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
  956. [DEV_EVENT_INTERRUPT] = ccw_device_w4sense,
  957. [DEV_EVENT_TIMEOUT] = ccw_device_nop,
  958. [DEV_EVENT_VERIFY] = ccw_device_online_verify,
  959. },
  960. [DEV_STATE_DISBAND_PGID] = {
  961. [DEV_EVENT_NOTOPER] = ccw_device_request_event,
  962. [DEV_EVENT_INTERRUPT] = ccw_device_request_event,
  963. [DEV_EVENT_TIMEOUT] = ccw_device_request_event,
  964. [DEV_EVENT_VERIFY] = ccw_device_nop,
  965. },
  966. [DEV_STATE_BOXED] = {
  967. [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
  968. [DEV_EVENT_INTERRUPT] = ccw_device_nop,
  969. [DEV_EVENT_TIMEOUT] = ccw_device_nop,
  970. [DEV_EVENT_VERIFY] = ccw_device_boxed_verify,
  971. },
  972. /* states to wait for i/o completion before doing something */
  973. [DEV_STATE_TIMEOUT_KILL] = {
  974. [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
  975. [DEV_EVENT_INTERRUPT] = ccw_device_killing_irq,
  976. [DEV_EVENT_TIMEOUT] = ccw_device_killing_timeout,
  977. [DEV_EVENT_VERIFY] = ccw_device_nop, //FIXME
  978. },
  979. [DEV_STATE_QUIESCE] = {
  980. [DEV_EVENT_NOTOPER] = ccw_device_quiesce_done,
  981. [DEV_EVENT_INTERRUPT] = ccw_device_quiesce_done,
  982. [DEV_EVENT_TIMEOUT] = ccw_device_quiesce_timeout,
  983. [DEV_EVENT_VERIFY] = ccw_device_nop,
  984. },
  985. /* special states for devices gone not operational */
  986. [DEV_STATE_DISCONNECTED] = {
  987. [DEV_EVENT_NOTOPER] = ccw_device_nop,
  988. [DEV_EVENT_INTERRUPT] = ccw_device_start_id,
  989. [DEV_EVENT_TIMEOUT] = ccw_device_nop,
  990. [DEV_EVENT_VERIFY] = ccw_device_start_id,
  991. },
  992. [DEV_STATE_DISCONNECTED_SENSE_ID] = {
  993. [DEV_EVENT_NOTOPER] = ccw_device_request_event,
  994. [DEV_EVENT_INTERRUPT] = ccw_device_request_event,
  995. [DEV_EVENT_TIMEOUT] = ccw_device_request_event,
  996. [DEV_EVENT_VERIFY] = ccw_device_nop,
  997. },
  998. [DEV_STATE_CMFCHANGE] = {
  999. [DEV_EVENT_NOTOPER] = ccw_device_change_cmfstate,
  1000. [DEV_EVENT_INTERRUPT] = ccw_device_change_cmfstate,
  1001. [DEV_EVENT_TIMEOUT] = ccw_device_change_cmfstate,
  1002. [DEV_EVENT_VERIFY] = ccw_device_change_cmfstate,
  1003. },
  1004. [DEV_STATE_CMFUPDATE] = {
  1005. [DEV_EVENT_NOTOPER] = ccw_device_update_cmfblock,
  1006. [DEV_EVENT_INTERRUPT] = ccw_device_update_cmfblock,
  1007. [DEV_EVENT_TIMEOUT] = ccw_device_update_cmfblock,
  1008. [DEV_EVENT_VERIFY] = ccw_device_update_cmfblock,
  1009. },
  1010. [DEV_STATE_STEAL_LOCK] = {
  1011. [DEV_EVENT_NOTOPER] = ccw_device_request_event,
  1012. [DEV_EVENT_INTERRUPT] = ccw_device_request_event,
  1013. [DEV_EVENT_TIMEOUT] = ccw_device_request_event,
  1014. [DEV_EVENT_VERIFY] = ccw_device_nop,
  1015. },
  1016. };
  1017. EXPORT_SYMBOL_GPL(ccw_device_set_timeout);