device_fsm.c 29 KB

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