device_fsm.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  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. panic("Can't stop i/o on subchannel.\n");
  162. }
  163. void ccw_device_update_sense_data(struct ccw_device *cdev)
  164. {
  165. memset(&cdev->id, 0, sizeof(cdev->id));
  166. cdev->id.cu_type = cdev->private->senseid.cu_type;
  167. cdev->id.cu_model = cdev->private->senseid.cu_model;
  168. cdev->id.dev_type = cdev->private->senseid.dev_type;
  169. cdev->id.dev_model = cdev->private->senseid.dev_model;
  170. }
  171. int ccw_device_test_sense_data(struct ccw_device *cdev)
  172. {
  173. return cdev->id.cu_type == cdev->private->senseid.cu_type &&
  174. cdev->id.cu_model == cdev->private->senseid.cu_model &&
  175. cdev->id.dev_type == cdev->private->senseid.dev_type &&
  176. cdev->id.dev_model == cdev->private->senseid.dev_model;
  177. }
  178. /*
  179. * The machine won't give us any notification by machine check if a chpid has
  180. * been varied online on the SE so we have to find out by magic (i. e. driving
  181. * the channel subsystem to device selection and updating our path masks).
  182. */
  183. static void
  184. __recover_lost_chpids(struct subchannel *sch, int old_lpm)
  185. {
  186. int mask, i;
  187. struct chp_id chpid;
  188. chp_id_init(&chpid);
  189. for (i = 0; i<8; i++) {
  190. mask = 0x80 >> i;
  191. if (!(sch->lpm & mask))
  192. continue;
  193. if (old_lpm & mask)
  194. continue;
  195. chpid.id = sch->schib.pmcw.chpid[i];
  196. if (!chp_is_registered(chpid))
  197. css_schedule_eval_all();
  198. }
  199. }
  200. /*
  201. * Stop device recognition.
  202. */
  203. static void
  204. ccw_device_recog_done(struct ccw_device *cdev, int state)
  205. {
  206. struct subchannel *sch;
  207. int old_lpm;
  208. sch = to_subchannel(cdev->dev.parent);
  209. if (cio_disable_subchannel(sch))
  210. state = DEV_STATE_NOT_OPER;
  211. /*
  212. * Now that we tried recognition, we have performed device selection
  213. * through ssch() and the path information is up to date.
  214. */
  215. old_lpm = sch->lpm;
  216. /* Check since device may again have become not operational. */
  217. if (cio_update_schib(sch))
  218. state = DEV_STATE_NOT_OPER;
  219. else
  220. sch->lpm = sch->schib.pmcw.pam & sch->opm;
  221. if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID)
  222. /* Force reprobe on all chpids. */
  223. old_lpm = 0;
  224. if (sch->lpm != old_lpm)
  225. __recover_lost_chpids(sch, old_lpm);
  226. if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID &&
  227. (state == DEV_STATE_NOT_OPER || state == DEV_STATE_BOXED)) {
  228. cdev->private->flags.recog_done = 1;
  229. cdev->private->state = DEV_STATE_DISCONNECTED;
  230. wake_up(&cdev->private->wait_q);
  231. return;
  232. }
  233. if (cdev->private->flags.resuming) {
  234. cdev->private->state = state;
  235. cdev->private->flags.recog_done = 1;
  236. wake_up(&cdev->private->wait_q);
  237. return;
  238. }
  239. switch (state) {
  240. case DEV_STATE_NOT_OPER:
  241. break;
  242. case DEV_STATE_OFFLINE:
  243. if (!cdev->online) {
  244. ccw_device_update_sense_data(cdev);
  245. break;
  246. }
  247. cdev->private->state = DEV_STATE_OFFLINE;
  248. cdev->private->flags.recog_done = 1;
  249. if (ccw_device_test_sense_data(cdev)) {
  250. cdev->private->flags.donotify = 1;
  251. ccw_device_online(cdev);
  252. wake_up(&cdev->private->wait_q);
  253. } else {
  254. ccw_device_update_sense_data(cdev);
  255. ccw_device_sched_todo(cdev, CDEV_TODO_REBIND);
  256. }
  257. return;
  258. case DEV_STATE_BOXED:
  259. if (cdev->id.cu_type != 0) { /* device was recognized before */
  260. cdev->private->flags.recog_done = 1;
  261. cdev->private->state = DEV_STATE_BOXED;
  262. wake_up(&cdev->private->wait_q);
  263. return;
  264. }
  265. break;
  266. }
  267. cdev->private->state = state;
  268. io_subchannel_recog_done(cdev);
  269. wake_up(&cdev->private->wait_q);
  270. }
  271. /*
  272. * Function called from device_id.c after sense id has completed.
  273. */
  274. void
  275. ccw_device_sense_id_done(struct ccw_device *cdev, int err)
  276. {
  277. switch (err) {
  278. case 0:
  279. ccw_device_recog_done(cdev, DEV_STATE_OFFLINE);
  280. break;
  281. case -ETIME: /* Sense id stopped by timeout. */
  282. ccw_device_recog_done(cdev, DEV_STATE_BOXED);
  283. break;
  284. default:
  285. ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
  286. break;
  287. }
  288. }
  289. /**
  290. * ccw_device_notify() - inform the device's driver about an event
  291. * @cdev: device for which an event occured
  292. * @event: event that occurred
  293. *
  294. * Returns:
  295. * -%EINVAL if the device is offline or has no driver.
  296. * -%EOPNOTSUPP if the device's driver has no notifier registered.
  297. * %NOTIFY_OK if the driver wants to keep the device.
  298. * %NOTIFY_BAD if the driver doesn't want to keep the device.
  299. */
  300. int ccw_device_notify(struct ccw_device *cdev, int event)
  301. {
  302. int ret = -EINVAL;
  303. if (!cdev->drv)
  304. goto out;
  305. if (!cdev->online)
  306. goto out;
  307. CIO_MSG_EVENT(2, "notify called for 0.%x.%04x, event=%d\n",
  308. cdev->private->dev_id.ssid, cdev->private->dev_id.devno,
  309. event);
  310. if (!cdev->drv->notify) {
  311. ret = -EOPNOTSUPP;
  312. goto out;
  313. }
  314. if (cdev->drv->notify(cdev, event))
  315. ret = NOTIFY_OK;
  316. else
  317. ret = NOTIFY_BAD;
  318. out:
  319. return ret;
  320. }
  321. static void ccw_device_oper_notify(struct ccw_device *cdev)
  322. {
  323. if (ccw_device_notify(cdev, CIO_OPER) == NOTIFY_OK) {
  324. /* Reenable channel measurements, if needed. */
  325. ccw_device_sched_todo(cdev, CDEV_TODO_ENABLE_CMF);
  326. return;
  327. }
  328. /* Driver doesn't want device back. */
  329. ccw_device_set_notoper(cdev);
  330. ccw_device_sched_todo(cdev, CDEV_TODO_REBIND);
  331. }
  332. /*
  333. * Finished with online/offline processing.
  334. */
  335. static void
  336. ccw_device_done(struct ccw_device *cdev, int state)
  337. {
  338. struct subchannel *sch;
  339. sch = to_subchannel(cdev->dev.parent);
  340. ccw_device_set_timeout(cdev, 0);
  341. if (state != DEV_STATE_ONLINE)
  342. cio_disable_subchannel(sch);
  343. /* Reset device status. */
  344. memset(&cdev->private->irb, 0, sizeof(struct irb));
  345. cdev->private->state = state;
  346. switch (state) {
  347. case DEV_STATE_BOXED:
  348. CIO_MSG_EVENT(0, "Boxed device %04x on subchannel %04x\n",
  349. cdev->private->dev_id.devno, sch->schid.sch_no);
  350. if (cdev->online &&
  351. ccw_device_notify(cdev, CIO_BOXED) != NOTIFY_OK)
  352. ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
  353. cdev->private->flags.donotify = 0;
  354. break;
  355. case DEV_STATE_NOT_OPER:
  356. CIO_MSG_EVENT(0, "Device %04x gone on subchannel %04x\n",
  357. cdev->private->dev_id.devno, sch->schid.sch_no);
  358. if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
  359. ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
  360. else
  361. ccw_device_set_disconnected(cdev);
  362. cdev->private->flags.donotify = 0;
  363. break;
  364. case DEV_STATE_DISCONNECTED:
  365. CIO_MSG_EVENT(0, "Disconnected device %04x on subchannel "
  366. "%04x\n", cdev->private->dev_id.devno,
  367. sch->schid.sch_no);
  368. if (ccw_device_notify(cdev, CIO_NO_PATH) != NOTIFY_OK)
  369. ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
  370. else
  371. ccw_device_set_disconnected(cdev);
  372. cdev->private->flags.donotify = 0;
  373. break;
  374. default:
  375. break;
  376. }
  377. if (cdev->private->flags.donotify) {
  378. cdev->private->flags.donotify = 0;
  379. ccw_device_oper_notify(cdev);
  380. }
  381. wake_up(&cdev->private->wait_q);
  382. }
  383. /*
  384. * Start device recognition.
  385. */
  386. void ccw_device_recognition(struct ccw_device *cdev)
  387. {
  388. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  389. /*
  390. * We used to start here with a sense pgid to find out whether a device
  391. * is locked by someone else. Unfortunately, the sense pgid command
  392. * code has other meanings on devices predating the path grouping
  393. * algorithm, so we start with sense id and box the device after an
  394. * timeout (or if sense pgid during path verification detects the device
  395. * is locked, as may happen on newer devices).
  396. */
  397. cdev->private->flags.recog_done = 0;
  398. cdev->private->state = DEV_STATE_SENSE_ID;
  399. if (cio_enable_subchannel(sch, (u32) (addr_t) sch)) {
  400. ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
  401. return;
  402. }
  403. ccw_device_sense_id_start(cdev);
  404. }
  405. /*
  406. * Handle events for states that use the ccw request infrastructure.
  407. */
  408. static void ccw_device_request_event(struct ccw_device *cdev, enum dev_event e)
  409. {
  410. switch (e) {
  411. case DEV_EVENT_NOTOPER:
  412. ccw_request_notoper(cdev);
  413. break;
  414. case DEV_EVENT_INTERRUPT:
  415. ccw_request_handler(cdev);
  416. break;
  417. case DEV_EVENT_TIMEOUT:
  418. ccw_request_timeout(cdev);
  419. break;
  420. default:
  421. break;
  422. }
  423. }
  424. void
  425. ccw_device_verify_done(struct ccw_device *cdev, int err)
  426. {
  427. struct subchannel *sch;
  428. sch = to_subchannel(cdev->dev.parent);
  429. /* Update schib - pom may have changed. */
  430. if (cio_update_schib(sch)) {
  431. err = -ENODEV;
  432. goto callback;
  433. }
  434. /* Update lpm with verified path mask. */
  435. sch->lpm = sch->vpm;
  436. /* Repeat path verification? */
  437. if (cdev->private->flags.doverify) {
  438. ccw_device_verify_start(cdev);
  439. return;
  440. }
  441. callback:
  442. switch (err) {
  443. case 0:
  444. ccw_device_done(cdev, DEV_STATE_ONLINE);
  445. /* Deliver fake irb to device driver, if needed. */
  446. if (cdev->private->flags.fake_irb) {
  447. memset(&cdev->private->irb, 0, sizeof(struct irb));
  448. cdev->private->irb.scsw.cmd.cc = 1;
  449. cdev->private->irb.scsw.cmd.fctl = SCSW_FCTL_START_FUNC;
  450. cdev->private->irb.scsw.cmd.actl = SCSW_ACTL_START_PEND;
  451. cdev->private->irb.scsw.cmd.stctl =
  452. SCSW_STCTL_STATUS_PEND;
  453. cdev->private->flags.fake_irb = 0;
  454. if (cdev->handler)
  455. cdev->handler(cdev, cdev->private->intparm,
  456. &cdev->private->irb);
  457. memset(&cdev->private->irb, 0, sizeof(struct irb));
  458. }
  459. break;
  460. case -ETIME:
  461. case -EUSERS:
  462. /* Reset oper notify indication after verify error. */
  463. cdev->private->flags.donotify = 0;
  464. ccw_device_done(cdev, DEV_STATE_BOXED);
  465. break;
  466. case -EACCES:
  467. /* Reset oper notify indication after verify error. */
  468. cdev->private->flags.donotify = 0;
  469. ccw_device_done(cdev, DEV_STATE_DISCONNECTED);
  470. break;
  471. default:
  472. /* Reset oper notify indication after verify error. */
  473. cdev->private->flags.donotify = 0;
  474. ccw_device_done(cdev, DEV_STATE_NOT_OPER);
  475. break;
  476. }
  477. }
  478. /*
  479. * Get device online.
  480. */
  481. int
  482. ccw_device_online(struct ccw_device *cdev)
  483. {
  484. struct subchannel *sch;
  485. int ret;
  486. if ((cdev->private->state != DEV_STATE_OFFLINE) &&
  487. (cdev->private->state != DEV_STATE_BOXED))
  488. return -EINVAL;
  489. sch = to_subchannel(cdev->dev.parent);
  490. ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
  491. if (ret != 0) {
  492. /* Couldn't enable the subchannel for i/o. Sick device. */
  493. if (ret == -ENODEV)
  494. dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
  495. return ret;
  496. }
  497. /* Start initial path verification. */
  498. cdev->private->state = DEV_STATE_VERIFY;
  499. ccw_device_verify_start(cdev);
  500. return 0;
  501. }
  502. void
  503. ccw_device_disband_done(struct ccw_device *cdev, int err)
  504. {
  505. switch (err) {
  506. case 0:
  507. ccw_device_done(cdev, DEV_STATE_OFFLINE);
  508. break;
  509. case -ETIME:
  510. ccw_device_done(cdev, DEV_STATE_BOXED);
  511. break;
  512. default:
  513. cdev->private->flags.donotify = 0;
  514. ccw_device_done(cdev, DEV_STATE_NOT_OPER);
  515. break;
  516. }
  517. }
  518. /*
  519. * Shutdown device.
  520. */
  521. int
  522. ccw_device_offline(struct ccw_device *cdev)
  523. {
  524. struct subchannel *sch;
  525. /* Allow ccw_device_offline while disconnected. */
  526. if (cdev->private->state == DEV_STATE_DISCONNECTED ||
  527. cdev->private->state == DEV_STATE_NOT_OPER) {
  528. cdev->private->flags.donotify = 0;
  529. ccw_device_done(cdev, DEV_STATE_NOT_OPER);
  530. return 0;
  531. }
  532. if (cdev->private->state == DEV_STATE_BOXED) {
  533. ccw_device_done(cdev, DEV_STATE_BOXED);
  534. return 0;
  535. }
  536. if (ccw_device_is_orphan(cdev)) {
  537. ccw_device_done(cdev, DEV_STATE_OFFLINE);
  538. return 0;
  539. }
  540. sch = to_subchannel(cdev->dev.parent);
  541. if (cio_update_schib(sch))
  542. return -ENODEV;
  543. if (scsw_actl(&sch->schib.scsw) != 0)
  544. return -EBUSY;
  545. if (cdev->private->state != DEV_STATE_ONLINE)
  546. return -EINVAL;
  547. /* Are we doing path grouping? */
  548. if (!cdev->private->flags.pgroup) {
  549. /* No, set state offline immediately. */
  550. ccw_device_done(cdev, DEV_STATE_OFFLINE);
  551. return 0;
  552. }
  553. /* Start Set Path Group commands. */
  554. cdev->private->state = DEV_STATE_DISBAND_PGID;
  555. ccw_device_disband_start(cdev);
  556. return 0;
  557. }
  558. /*
  559. * Handle not operational event in non-special state.
  560. */
  561. static void ccw_device_generic_notoper(struct ccw_device *cdev,
  562. enum dev_event dev_event)
  563. {
  564. if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
  565. ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
  566. else
  567. ccw_device_set_disconnected(cdev);
  568. }
  569. /*
  570. * Handle path verification event in offline state.
  571. */
  572. static void ccw_device_offline_verify(struct ccw_device *cdev,
  573. enum dev_event dev_event)
  574. {
  575. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  576. css_schedule_eval(sch->schid);
  577. }
  578. /*
  579. * Handle path verification event.
  580. */
  581. static void
  582. ccw_device_online_verify(struct ccw_device *cdev, enum dev_event dev_event)
  583. {
  584. struct subchannel *sch;
  585. if (cdev->private->state == DEV_STATE_W4SENSE) {
  586. cdev->private->flags.doverify = 1;
  587. return;
  588. }
  589. sch = to_subchannel(cdev->dev.parent);
  590. /*
  591. * Since we might not just be coming from an interrupt from the
  592. * subchannel we have to update the schib.
  593. */
  594. if (cio_update_schib(sch)) {
  595. ccw_device_verify_done(cdev, -ENODEV);
  596. return;
  597. }
  598. if (scsw_actl(&sch->schib.scsw) != 0 ||
  599. (scsw_stctl(&sch->schib.scsw) & SCSW_STCTL_STATUS_PEND) ||
  600. (scsw_stctl(&cdev->private->irb.scsw) & SCSW_STCTL_STATUS_PEND)) {
  601. /*
  602. * No final status yet or final status not yet delivered
  603. * to the device driver. Can't do path verfication now,
  604. * delay until final status was delivered.
  605. */
  606. cdev->private->flags.doverify = 1;
  607. return;
  608. }
  609. /* Device is idle, we can do the path verification. */
  610. cdev->private->state = DEV_STATE_VERIFY;
  611. ccw_device_verify_start(cdev);
  612. }
  613. /*
  614. * Handle path verification event in boxed state.
  615. */
  616. static void ccw_device_boxed_verify(struct ccw_device *cdev,
  617. enum dev_event dev_event)
  618. {
  619. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  620. if (cdev->online) {
  621. if (cio_enable_subchannel(sch, (u32) (addr_t) sch))
  622. ccw_device_done(cdev, DEV_STATE_NOT_OPER);
  623. else
  624. ccw_device_online_verify(cdev, dev_event);
  625. } else
  626. css_schedule_eval(sch->schid);
  627. }
  628. /*
  629. * Got an interrupt for a normal io (state online).
  630. */
  631. static void
  632. ccw_device_irq(struct ccw_device *cdev, enum dev_event dev_event)
  633. {
  634. struct irb *irb;
  635. int is_cmd;
  636. irb = (struct irb *)&S390_lowcore.irb;
  637. is_cmd = !scsw_is_tm(&irb->scsw);
  638. /* Check for unsolicited interrupt. */
  639. if (!scsw_is_solicited(&irb->scsw)) {
  640. if (is_cmd && (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) &&
  641. !irb->esw.esw0.erw.cons) {
  642. /* Unit check but no sense data. Need basic sense. */
  643. if (ccw_device_do_sense(cdev, irb) != 0)
  644. goto call_handler_unsol;
  645. memcpy(&cdev->private->irb, irb, sizeof(struct irb));
  646. cdev->private->state = DEV_STATE_W4SENSE;
  647. cdev->private->intparm = 0;
  648. return;
  649. }
  650. call_handler_unsol:
  651. if (cdev->handler)
  652. cdev->handler (cdev, 0, irb);
  653. if (cdev->private->flags.doverify)
  654. ccw_device_online_verify(cdev, 0);
  655. return;
  656. }
  657. /* Accumulate status and find out if a basic sense is needed. */
  658. ccw_device_accumulate_irb(cdev, irb);
  659. if (is_cmd && cdev->private->flags.dosense) {
  660. if (ccw_device_do_sense(cdev, irb) == 0) {
  661. cdev->private->state = DEV_STATE_W4SENSE;
  662. }
  663. return;
  664. }
  665. /* Call the handler. */
  666. if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
  667. /* Start delayed path verification. */
  668. ccw_device_online_verify(cdev, 0);
  669. }
  670. /*
  671. * Got an timeout in online state.
  672. */
  673. static void
  674. ccw_device_online_timeout(struct ccw_device *cdev, enum dev_event dev_event)
  675. {
  676. int ret;
  677. ccw_device_set_timeout(cdev, 0);
  678. ret = ccw_device_cancel_halt_clear(cdev);
  679. if (ret == -EBUSY) {
  680. ccw_device_set_timeout(cdev, 3*HZ);
  681. cdev->private->state = DEV_STATE_TIMEOUT_KILL;
  682. return;
  683. }
  684. if (ret == -ENODEV)
  685. dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
  686. else if (cdev->handler)
  687. cdev->handler(cdev, cdev->private->intparm,
  688. ERR_PTR(-ETIMEDOUT));
  689. }
  690. /*
  691. * Got an interrupt for a basic sense.
  692. */
  693. static void
  694. ccw_device_w4sense(struct ccw_device *cdev, enum dev_event dev_event)
  695. {
  696. struct irb *irb;
  697. irb = (struct irb *)&S390_lowcore.irb;
  698. /* Check for unsolicited interrupt. */
  699. if (scsw_stctl(&irb->scsw) ==
  700. (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) {
  701. if (scsw_cc(&irb->scsw) == 1)
  702. /* Basic sense hasn't started. Try again. */
  703. ccw_device_do_sense(cdev, irb);
  704. else {
  705. CIO_MSG_EVENT(0, "0.%x.%04x: unsolicited "
  706. "interrupt during w4sense...\n",
  707. cdev->private->dev_id.ssid,
  708. cdev->private->dev_id.devno);
  709. if (cdev->handler)
  710. cdev->handler (cdev, 0, irb);
  711. }
  712. return;
  713. }
  714. /*
  715. * Check if a halt or clear has been issued in the meanwhile. If yes,
  716. * only deliver the halt/clear interrupt to the device driver as if it
  717. * had killed the original request.
  718. */
  719. if (scsw_fctl(&irb->scsw) &
  720. (SCSW_FCTL_CLEAR_FUNC | SCSW_FCTL_HALT_FUNC)) {
  721. cdev->private->flags.dosense = 0;
  722. memset(&cdev->private->irb, 0, sizeof(struct irb));
  723. ccw_device_accumulate_irb(cdev, irb);
  724. goto call_handler;
  725. }
  726. /* Add basic sense info to irb. */
  727. ccw_device_accumulate_basic_sense(cdev, irb);
  728. if (cdev->private->flags.dosense) {
  729. /* Another basic sense is needed. */
  730. ccw_device_do_sense(cdev, irb);
  731. return;
  732. }
  733. call_handler:
  734. cdev->private->state = DEV_STATE_ONLINE;
  735. /* In case sensing interfered with setting the device online */
  736. wake_up(&cdev->private->wait_q);
  737. /* Call the handler. */
  738. if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
  739. /* Start delayed path verification. */
  740. ccw_device_online_verify(cdev, 0);
  741. }
  742. static void
  743. ccw_device_killing_irq(struct ccw_device *cdev, enum dev_event dev_event)
  744. {
  745. struct subchannel *sch;
  746. sch = to_subchannel(cdev->dev.parent);
  747. ccw_device_set_timeout(cdev, 0);
  748. /* Start delayed path verification. */
  749. ccw_device_online_verify(cdev, 0);
  750. /* OK, i/o is dead now. Call interrupt handler. */
  751. if (cdev->handler)
  752. cdev->handler(cdev, cdev->private->intparm,
  753. ERR_PTR(-EIO));
  754. }
  755. static void
  756. ccw_device_killing_timeout(struct ccw_device *cdev, enum dev_event dev_event)
  757. {
  758. int ret;
  759. ret = ccw_device_cancel_halt_clear(cdev);
  760. if (ret == -EBUSY) {
  761. ccw_device_set_timeout(cdev, 3*HZ);
  762. return;
  763. }
  764. /* Start delayed path verification. */
  765. ccw_device_online_verify(cdev, 0);
  766. if (cdev->handler)
  767. cdev->handler(cdev, cdev->private->intparm,
  768. ERR_PTR(-EIO));
  769. }
  770. void ccw_device_kill_io(struct ccw_device *cdev)
  771. {
  772. int ret;
  773. ret = ccw_device_cancel_halt_clear(cdev);
  774. if (ret == -EBUSY) {
  775. ccw_device_set_timeout(cdev, 3*HZ);
  776. cdev->private->state = DEV_STATE_TIMEOUT_KILL;
  777. return;
  778. }
  779. /* Start delayed path verification. */
  780. ccw_device_online_verify(cdev, 0);
  781. if (cdev->handler)
  782. cdev->handler(cdev, cdev->private->intparm,
  783. ERR_PTR(-EIO));
  784. }
  785. static void
  786. ccw_device_delay_verify(struct ccw_device *cdev, enum dev_event dev_event)
  787. {
  788. /* Start verification after current task finished. */
  789. cdev->private->flags.doverify = 1;
  790. }
  791. static void
  792. ccw_device_start_id(struct ccw_device *cdev, enum dev_event dev_event)
  793. {
  794. struct subchannel *sch;
  795. sch = to_subchannel(cdev->dev.parent);
  796. if (cio_enable_subchannel(sch, (u32)(addr_t)sch) != 0)
  797. /* Couldn't enable the subchannel for i/o. Sick device. */
  798. return;
  799. cdev->private->state = DEV_STATE_DISCONNECTED_SENSE_ID;
  800. ccw_device_sense_id_start(cdev);
  801. }
  802. void ccw_device_trigger_reprobe(struct ccw_device *cdev)
  803. {
  804. struct subchannel *sch;
  805. if (cdev->private->state != DEV_STATE_DISCONNECTED)
  806. return;
  807. sch = to_subchannel(cdev->dev.parent);
  808. /* Update some values. */
  809. if (cio_update_schib(sch))
  810. return;
  811. /*
  812. * The pim, pam, pom values may not be accurate, but they are the best
  813. * we have before performing device selection :/
  814. */
  815. sch->lpm = sch->schib.pmcw.pam & sch->opm;
  816. /*
  817. * Use the initial configuration since we can't be shure that the old
  818. * paths are valid.
  819. */
  820. io_subchannel_init_config(sch);
  821. if (cio_commit_config(sch))
  822. return;
  823. /* We should also udate ssd info, but this has to wait. */
  824. /* Check if this is another device which appeared on the same sch. */
  825. if (sch->schib.pmcw.dev != cdev->private->dev_id.devno)
  826. css_schedule_eval(sch->schid);
  827. else
  828. ccw_device_start_id(cdev, 0);
  829. }
  830. static void ccw_device_disabled_irq(struct ccw_device *cdev,
  831. enum dev_event dev_event)
  832. {
  833. struct subchannel *sch;
  834. sch = to_subchannel(cdev->dev.parent);
  835. /*
  836. * An interrupt in a disabled state means a previous disable was not
  837. * successful - should not happen, but we try to disable again.
  838. */
  839. cio_disable_subchannel(sch);
  840. }
  841. static void
  842. ccw_device_change_cmfstate(struct ccw_device *cdev, enum dev_event dev_event)
  843. {
  844. retry_set_schib(cdev);
  845. cdev->private->state = DEV_STATE_ONLINE;
  846. dev_fsm_event(cdev, dev_event);
  847. }
  848. static void ccw_device_update_cmfblock(struct ccw_device *cdev,
  849. enum dev_event dev_event)
  850. {
  851. cmf_retry_copy_block(cdev);
  852. cdev->private->state = DEV_STATE_ONLINE;
  853. dev_fsm_event(cdev, dev_event);
  854. }
  855. static void
  856. ccw_device_quiesce_done(struct ccw_device *cdev, enum dev_event dev_event)
  857. {
  858. ccw_device_set_timeout(cdev, 0);
  859. cdev->private->state = DEV_STATE_NOT_OPER;
  860. wake_up(&cdev->private->wait_q);
  861. }
  862. static void
  863. ccw_device_quiesce_timeout(struct ccw_device *cdev, enum dev_event dev_event)
  864. {
  865. int ret;
  866. ret = ccw_device_cancel_halt_clear(cdev);
  867. if (ret == -EBUSY) {
  868. ccw_device_set_timeout(cdev, HZ/10);
  869. } else {
  870. cdev->private->state = DEV_STATE_NOT_OPER;
  871. wake_up(&cdev->private->wait_q);
  872. }
  873. }
  874. /*
  875. * No operation action. This is used e.g. to ignore a timeout event in
  876. * state offline.
  877. */
  878. static void
  879. ccw_device_nop(struct ccw_device *cdev, enum dev_event dev_event)
  880. {
  881. }
  882. /*
  883. * device statemachine
  884. */
  885. fsm_func_t *dev_jumptable[NR_DEV_STATES][NR_DEV_EVENTS] = {
  886. [DEV_STATE_NOT_OPER] = {
  887. [DEV_EVENT_NOTOPER] = ccw_device_nop,
  888. [DEV_EVENT_INTERRUPT] = ccw_device_disabled_irq,
  889. [DEV_EVENT_TIMEOUT] = ccw_device_nop,
  890. [DEV_EVENT_VERIFY] = ccw_device_nop,
  891. },
  892. [DEV_STATE_SENSE_PGID] = {
  893. [DEV_EVENT_NOTOPER] = ccw_device_request_event,
  894. [DEV_EVENT_INTERRUPT] = ccw_device_request_event,
  895. [DEV_EVENT_TIMEOUT] = ccw_device_request_event,
  896. [DEV_EVENT_VERIFY] = ccw_device_nop,
  897. },
  898. [DEV_STATE_SENSE_ID] = {
  899. [DEV_EVENT_NOTOPER] = ccw_device_request_event,
  900. [DEV_EVENT_INTERRUPT] = ccw_device_request_event,
  901. [DEV_EVENT_TIMEOUT] = ccw_device_request_event,
  902. [DEV_EVENT_VERIFY] = ccw_device_nop,
  903. },
  904. [DEV_STATE_OFFLINE] = {
  905. [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
  906. [DEV_EVENT_INTERRUPT] = ccw_device_disabled_irq,
  907. [DEV_EVENT_TIMEOUT] = ccw_device_nop,
  908. [DEV_EVENT_VERIFY] = ccw_device_offline_verify,
  909. },
  910. [DEV_STATE_VERIFY] = {
  911. [DEV_EVENT_NOTOPER] = ccw_device_request_event,
  912. [DEV_EVENT_INTERRUPT] = ccw_device_request_event,
  913. [DEV_EVENT_TIMEOUT] = ccw_device_request_event,
  914. [DEV_EVENT_VERIFY] = ccw_device_delay_verify,
  915. },
  916. [DEV_STATE_ONLINE] = {
  917. [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
  918. [DEV_EVENT_INTERRUPT] = ccw_device_irq,
  919. [DEV_EVENT_TIMEOUT] = ccw_device_online_timeout,
  920. [DEV_EVENT_VERIFY] = ccw_device_online_verify,
  921. },
  922. [DEV_STATE_W4SENSE] = {
  923. [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
  924. [DEV_EVENT_INTERRUPT] = ccw_device_w4sense,
  925. [DEV_EVENT_TIMEOUT] = ccw_device_nop,
  926. [DEV_EVENT_VERIFY] = ccw_device_online_verify,
  927. },
  928. [DEV_STATE_DISBAND_PGID] = {
  929. [DEV_EVENT_NOTOPER] = ccw_device_request_event,
  930. [DEV_EVENT_INTERRUPT] = ccw_device_request_event,
  931. [DEV_EVENT_TIMEOUT] = ccw_device_request_event,
  932. [DEV_EVENT_VERIFY] = ccw_device_nop,
  933. },
  934. [DEV_STATE_BOXED] = {
  935. [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
  936. [DEV_EVENT_INTERRUPT] = ccw_device_nop,
  937. [DEV_EVENT_TIMEOUT] = ccw_device_nop,
  938. [DEV_EVENT_VERIFY] = ccw_device_boxed_verify,
  939. },
  940. /* states to wait for i/o completion before doing something */
  941. [DEV_STATE_TIMEOUT_KILL] = {
  942. [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
  943. [DEV_EVENT_INTERRUPT] = ccw_device_killing_irq,
  944. [DEV_EVENT_TIMEOUT] = ccw_device_killing_timeout,
  945. [DEV_EVENT_VERIFY] = ccw_device_nop, //FIXME
  946. },
  947. [DEV_STATE_QUIESCE] = {
  948. [DEV_EVENT_NOTOPER] = ccw_device_quiesce_done,
  949. [DEV_EVENT_INTERRUPT] = ccw_device_quiesce_done,
  950. [DEV_EVENT_TIMEOUT] = ccw_device_quiesce_timeout,
  951. [DEV_EVENT_VERIFY] = ccw_device_nop,
  952. },
  953. /* special states for devices gone not operational */
  954. [DEV_STATE_DISCONNECTED] = {
  955. [DEV_EVENT_NOTOPER] = ccw_device_nop,
  956. [DEV_EVENT_INTERRUPT] = ccw_device_start_id,
  957. [DEV_EVENT_TIMEOUT] = ccw_device_nop,
  958. [DEV_EVENT_VERIFY] = ccw_device_start_id,
  959. },
  960. [DEV_STATE_DISCONNECTED_SENSE_ID] = {
  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_CMFCHANGE] = {
  967. [DEV_EVENT_NOTOPER] = ccw_device_change_cmfstate,
  968. [DEV_EVENT_INTERRUPT] = ccw_device_change_cmfstate,
  969. [DEV_EVENT_TIMEOUT] = ccw_device_change_cmfstate,
  970. [DEV_EVENT_VERIFY] = ccw_device_change_cmfstate,
  971. },
  972. [DEV_STATE_CMFUPDATE] = {
  973. [DEV_EVENT_NOTOPER] = ccw_device_update_cmfblock,
  974. [DEV_EVENT_INTERRUPT] = ccw_device_update_cmfblock,
  975. [DEV_EVENT_TIMEOUT] = ccw_device_update_cmfblock,
  976. [DEV_EVENT_VERIFY] = ccw_device_update_cmfblock,
  977. },
  978. [DEV_STATE_STEAL_LOCK] = {
  979. [DEV_EVENT_NOTOPER] = ccw_device_request_event,
  980. [DEV_EVENT_INTERRUPT] = ccw_device_request_event,
  981. [DEV_EVENT_TIMEOUT] = ccw_device_request_event,
  982. [DEV_EVENT_VERIFY] = ccw_device_nop,
  983. },
  984. };
  985. EXPORT_SYMBOL_GPL(ccw_device_set_timeout);