device_fsm.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  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. * Function called from device_pgid.c after sense path ground has completed.
  362. */
  363. void
  364. ccw_device_sense_pgid_done(struct ccw_device *cdev, int err)
  365. {
  366. struct subchannel *sch;
  367. sch = to_subchannel(cdev->dev.parent);
  368. switch (err) {
  369. case -EOPNOTSUPP: /* path grouping not supported, use nop instead. */
  370. case 0: /* success */
  371. case -EACCES: /* partial success, some paths not operational */
  372. break;
  373. case -ETIME: /* Sense path group id stopped by timeout. */
  374. case -EUSERS: /* device is reserved for someone else. */
  375. ccw_device_done(cdev, DEV_STATE_BOXED);
  376. return;
  377. default:
  378. ccw_device_done(cdev, DEV_STATE_NOT_OPER);
  379. return;
  380. }
  381. /* Start Path Group verification. */
  382. cdev->private->state = DEV_STATE_VERIFY;
  383. ccw_device_verify_start(cdev);
  384. }
  385. /*
  386. * Start device recognition.
  387. */
  388. void ccw_device_recognition(struct ccw_device *cdev)
  389. {
  390. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  391. /*
  392. * We used to start here with a sense pgid to find out whether a device
  393. * is locked by someone else. Unfortunately, the sense pgid command
  394. * code has other meanings on devices predating the path grouping
  395. * algorithm, so we start with sense id and box the device after an
  396. * timeout (or if sense pgid during path verification detects the device
  397. * is locked, as may happen on newer devices).
  398. */
  399. cdev->private->flags.recog_done = 0;
  400. cdev->private->state = DEV_STATE_SENSE_ID;
  401. if (cio_enable_subchannel(sch, (u32) (addr_t) sch)) {
  402. ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER);
  403. return;
  404. }
  405. ccw_device_sense_id_start(cdev);
  406. }
  407. /*
  408. * Handle events for states that use the ccw request infrastructure.
  409. */
  410. static void ccw_device_request_event(struct ccw_device *cdev, enum dev_event e)
  411. {
  412. switch (e) {
  413. case DEV_EVENT_NOTOPER:
  414. ccw_request_notoper(cdev);
  415. break;
  416. case DEV_EVENT_INTERRUPT:
  417. ccw_request_handler(cdev);
  418. break;
  419. case DEV_EVENT_TIMEOUT:
  420. ccw_request_timeout(cdev);
  421. break;
  422. default:
  423. break;
  424. }
  425. }
  426. void
  427. ccw_device_verify_done(struct ccw_device *cdev, int err)
  428. {
  429. struct subchannel *sch;
  430. sch = to_subchannel(cdev->dev.parent);
  431. /* Update schib - pom may have changed. */
  432. if (cio_update_schib(sch)) {
  433. err = -ENODEV;
  434. goto callback;
  435. }
  436. /* Update lpm with verified path mask. */
  437. sch->lpm = sch->vpm;
  438. /* Repeat path verification? */
  439. if (cdev->private->flags.doverify) {
  440. ccw_device_verify_start(cdev);
  441. return;
  442. }
  443. callback:
  444. switch (err) {
  445. case 0:
  446. ccw_device_done(cdev, DEV_STATE_ONLINE);
  447. /* Deliver fake irb to device driver, if needed. */
  448. if (cdev->private->flags.fake_irb) {
  449. memset(&cdev->private->irb, 0, sizeof(struct irb));
  450. cdev->private->irb.scsw.cmd.cc = 1;
  451. cdev->private->irb.scsw.cmd.fctl = SCSW_FCTL_START_FUNC;
  452. cdev->private->irb.scsw.cmd.actl = SCSW_ACTL_START_PEND;
  453. cdev->private->irb.scsw.cmd.stctl =
  454. SCSW_STCTL_STATUS_PEND;
  455. cdev->private->flags.fake_irb = 0;
  456. if (cdev->handler)
  457. cdev->handler(cdev, cdev->private->intparm,
  458. &cdev->private->irb);
  459. memset(&cdev->private->irb, 0, sizeof(struct irb));
  460. }
  461. break;
  462. case -ETIME:
  463. /* Reset oper notify indication after verify error. */
  464. cdev->private->flags.donotify = 0;
  465. ccw_device_done(cdev, DEV_STATE_BOXED);
  466. break;
  467. case -EACCES:
  468. /* Reset oper notify indication after verify error. */
  469. cdev->private->flags.donotify = 0;
  470. ccw_device_done(cdev, DEV_STATE_DISCONNECTED);
  471. break;
  472. default:
  473. /* Reset oper notify indication after verify error. */
  474. cdev->private->flags.donotify = 0;
  475. ccw_device_done(cdev, DEV_STATE_NOT_OPER);
  476. break;
  477. }
  478. }
  479. /*
  480. * Get device online.
  481. */
  482. int
  483. ccw_device_online(struct ccw_device *cdev)
  484. {
  485. struct subchannel *sch;
  486. int ret;
  487. if ((cdev->private->state != DEV_STATE_OFFLINE) &&
  488. (cdev->private->state != DEV_STATE_BOXED))
  489. return -EINVAL;
  490. sch = to_subchannel(cdev->dev.parent);
  491. ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
  492. if (ret != 0) {
  493. /* Couldn't enable the subchannel for i/o. Sick device. */
  494. if (ret == -ENODEV)
  495. dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
  496. return ret;
  497. }
  498. /* Do we want to do path grouping? */
  499. if (!cdev->private->options.pgroup) {
  500. /* Start initial path verification. */
  501. cdev->private->state = DEV_STATE_VERIFY;
  502. ccw_device_verify_start(cdev);
  503. return 0;
  504. }
  505. /* Do a SensePGID first. */
  506. cdev->private->state = DEV_STATE_SENSE_PGID;
  507. ccw_device_sense_pgid_start(cdev);
  508. return 0;
  509. }
  510. void
  511. ccw_device_disband_done(struct ccw_device *cdev, int err)
  512. {
  513. switch (err) {
  514. case 0:
  515. ccw_device_done(cdev, DEV_STATE_OFFLINE);
  516. break;
  517. case -ETIME:
  518. ccw_device_done(cdev, DEV_STATE_BOXED);
  519. break;
  520. default:
  521. cdev->private->flags.donotify = 0;
  522. ccw_device_done(cdev, DEV_STATE_NOT_OPER);
  523. break;
  524. }
  525. }
  526. /*
  527. * Shutdown device.
  528. */
  529. int
  530. ccw_device_offline(struct ccw_device *cdev)
  531. {
  532. struct subchannel *sch;
  533. /* Allow ccw_device_offline while disconnected. */
  534. if (cdev->private->state == DEV_STATE_DISCONNECTED ||
  535. cdev->private->state == DEV_STATE_NOT_OPER) {
  536. cdev->private->flags.donotify = 0;
  537. ccw_device_done(cdev, DEV_STATE_NOT_OPER);
  538. return 0;
  539. }
  540. if (cdev->private->state == DEV_STATE_BOXED) {
  541. ccw_device_done(cdev, DEV_STATE_BOXED);
  542. return 0;
  543. }
  544. if (ccw_device_is_orphan(cdev)) {
  545. ccw_device_done(cdev, DEV_STATE_OFFLINE);
  546. return 0;
  547. }
  548. sch = to_subchannel(cdev->dev.parent);
  549. if (cio_update_schib(sch))
  550. return -ENODEV;
  551. if (scsw_actl(&sch->schib.scsw) != 0)
  552. return -EBUSY;
  553. if (cdev->private->state != DEV_STATE_ONLINE)
  554. return -EINVAL;
  555. /* Are we doing path grouping? */
  556. if (!cdev->private->options.pgroup) {
  557. /* No, set state offline immediately. */
  558. ccw_device_done(cdev, DEV_STATE_OFFLINE);
  559. return 0;
  560. }
  561. /* Start Set Path Group commands. */
  562. cdev->private->state = DEV_STATE_DISBAND_PGID;
  563. ccw_device_disband_start(cdev);
  564. return 0;
  565. }
  566. /*
  567. * Handle not operational event in non-special state.
  568. */
  569. static void ccw_device_generic_notoper(struct ccw_device *cdev,
  570. enum dev_event dev_event)
  571. {
  572. if (!ccw_device_notify(cdev, CIO_GONE))
  573. ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
  574. else
  575. ccw_device_set_disconnected(cdev);
  576. }
  577. /*
  578. * Handle path verification event in offline state.
  579. */
  580. static void ccw_device_offline_verify(struct ccw_device *cdev,
  581. enum dev_event dev_event)
  582. {
  583. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  584. css_schedule_eval(sch->schid);
  585. }
  586. /*
  587. * Handle path verification event.
  588. */
  589. static void
  590. ccw_device_online_verify(struct ccw_device *cdev, enum dev_event dev_event)
  591. {
  592. struct subchannel *sch;
  593. if (cdev->private->state == DEV_STATE_W4SENSE) {
  594. cdev->private->flags.doverify = 1;
  595. return;
  596. }
  597. sch = to_subchannel(cdev->dev.parent);
  598. /*
  599. * Since we might not just be coming from an interrupt from the
  600. * subchannel we have to update the schib.
  601. */
  602. if (cio_update_schib(sch)) {
  603. ccw_device_verify_done(cdev, -ENODEV);
  604. return;
  605. }
  606. if (scsw_actl(&sch->schib.scsw) != 0 ||
  607. (scsw_stctl(&sch->schib.scsw) & SCSW_STCTL_STATUS_PEND) ||
  608. (scsw_stctl(&cdev->private->irb.scsw) & SCSW_STCTL_STATUS_PEND)) {
  609. /*
  610. * No final status yet or final status not yet delivered
  611. * to the device driver. Can't do path verfication now,
  612. * delay until final status was delivered.
  613. */
  614. cdev->private->flags.doverify = 1;
  615. return;
  616. }
  617. /* Device is idle, we can do the path verification. */
  618. cdev->private->state = DEV_STATE_VERIFY;
  619. ccw_device_verify_start(cdev);
  620. }
  621. /*
  622. * Got an interrupt for a normal io (state online).
  623. */
  624. static void
  625. ccw_device_irq(struct ccw_device *cdev, enum dev_event dev_event)
  626. {
  627. struct irb *irb;
  628. int is_cmd;
  629. irb = (struct irb *) __LC_IRB;
  630. is_cmd = !scsw_is_tm(&irb->scsw);
  631. /* Check for unsolicited interrupt. */
  632. if (!scsw_is_solicited(&irb->scsw)) {
  633. if (is_cmd && (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) &&
  634. !irb->esw.esw0.erw.cons) {
  635. /* Unit check but no sense data. Need basic sense. */
  636. if (ccw_device_do_sense(cdev, irb) != 0)
  637. goto call_handler_unsol;
  638. memcpy(&cdev->private->irb, irb, sizeof(struct irb));
  639. cdev->private->state = DEV_STATE_W4SENSE;
  640. cdev->private->intparm = 0;
  641. return;
  642. }
  643. call_handler_unsol:
  644. if (cdev->handler)
  645. cdev->handler (cdev, 0, irb);
  646. if (cdev->private->flags.doverify)
  647. ccw_device_online_verify(cdev, 0);
  648. return;
  649. }
  650. /* Accumulate status and find out if a basic sense is needed. */
  651. ccw_device_accumulate_irb(cdev, irb);
  652. if (is_cmd && cdev->private->flags.dosense) {
  653. if (ccw_device_do_sense(cdev, irb) == 0) {
  654. cdev->private->state = DEV_STATE_W4SENSE;
  655. }
  656. return;
  657. }
  658. /* Call the handler. */
  659. if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
  660. /* Start delayed path verification. */
  661. ccw_device_online_verify(cdev, 0);
  662. }
  663. /*
  664. * Got an timeout in online state.
  665. */
  666. static void
  667. ccw_device_online_timeout(struct ccw_device *cdev, enum dev_event dev_event)
  668. {
  669. int ret;
  670. ccw_device_set_timeout(cdev, 0);
  671. ret = ccw_device_cancel_halt_clear(cdev);
  672. if (ret == -EBUSY) {
  673. ccw_device_set_timeout(cdev, 3*HZ);
  674. cdev->private->state = DEV_STATE_TIMEOUT_KILL;
  675. return;
  676. }
  677. if (ret == -ENODEV)
  678. dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
  679. else if (cdev->handler)
  680. cdev->handler(cdev, cdev->private->intparm,
  681. ERR_PTR(-ETIMEDOUT));
  682. }
  683. /*
  684. * Got an interrupt for a basic sense.
  685. */
  686. static void
  687. ccw_device_w4sense(struct ccw_device *cdev, enum dev_event dev_event)
  688. {
  689. struct irb *irb;
  690. irb = (struct irb *) __LC_IRB;
  691. /* Check for unsolicited interrupt. */
  692. if (scsw_stctl(&irb->scsw) ==
  693. (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) {
  694. if (scsw_cc(&irb->scsw) == 1)
  695. /* Basic sense hasn't started. Try again. */
  696. ccw_device_do_sense(cdev, irb);
  697. else {
  698. CIO_MSG_EVENT(0, "0.%x.%04x: unsolicited "
  699. "interrupt during w4sense...\n",
  700. cdev->private->dev_id.ssid,
  701. cdev->private->dev_id.devno);
  702. if (cdev->handler)
  703. cdev->handler (cdev, 0, irb);
  704. }
  705. return;
  706. }
  707. /*
  708. * Check if a halt or clear has been issued in the meanwhile. If yes,
  709. * only deliver the halt/clear interrupt to the device driver as if it
  710. * had killed the original request.
  711. */
  712. if (scsw_fctl(&irb->scsw) &
  713. (SCSW_FCTL_CLEAR_FUNC | SCSW_FCTL_HALT_FUNC)) {
  714. /* Retry Basic Sense if requested. */
  715. if (cdev->private->flags.intretry) {
  716. cdev->private->flags.intretry = 0;
  717. ccw_device_do_sense(cdev, irb);
  718. return;
  719. }
  720. cdev->private->flags.dosense = 0;
  721. memset(&cdev->private->irb, 0, sizeof(struct irb));
  722. ccw_device_accumulate_irb(cdev, irb);
  723. goto call_handler;
  724. }
  725. /* Add basic sense info to irb. */
  726. ccw_device_accumulate_basic_sense(cdev, irb);
  727. if (cdev->private->flags.dosense) {
  728. /* Another basic sense is needed. */
  729. ccw_device_do_sense(cdev, irb);
  730. return;
  731. }
  732. call_handler:
  733. cdev->private->state = DEV_STATE_ONLINE;
  734. /* In case sensing interfered with setting the device online */
  735. wake_up(&cdev->private->wait_q);
  736. /* Call the handler. */
  737. if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify)
  738. /* Start delayed path verification. */
  739. ccw_device_online_verify(cdev, 0);
  740. }
  741. static void
  742. ccw_device_clear_verify(struct ccw_device *cdev, enum dev_event dev_event)
  743. {
  744. struct irb *irb;
  745. irb = (struct irb *) __LC_IRB;
  746. /* Accumulate status. We don't do basic sense. */
  747. ccw_device_accumulate_irb(cdev, irb);
  748. /* Remember to clear irb to avoid residuals. */
  749. memset(&cdev->private->irb, 0, sizeof(struct irb));
  750. /* Try to start delayed device verification. */
  751. ccw_device_online_verify(cdev, 0);
  752. /* Note: Don't call handler for cio initiated clear! */
  753. }
  754. static void
  755. ccw_device_killing_irq(struct ccw_device *cdev, enum dev_event dev_event)
  756. {
  757. struct subchannel *sch;
  758. sch = to_subchannel(cdev->dev.parent);
  759. ccw_device_set_timeout(cdev, 0);
  760. /* Start delayed path verification. */
  761. ccw_device_online_verify(cdev, 0);
  762. /* OK, i/o is dead now. Call interrupt handler. */
  763. if (cdev->handler)
  764. cdev->handler(cdev, cdev->private->intparm,
  765. ERR_PTR(-EIO));
  766. }
  767. static void
  768. ccw_device_killing_timeout(struct ccw_device *cdev, enum dev_event dev_event)
  769. {
  770. int ret;
  771. ret = ccw_device_cancel_halt_clear(cdev);
  772. if (ret == -EBUSY) {
  773. ccw_device_set_timeout(cdev, 3*HZ);
  774. return;
  775. }
  776. /* Start delayed path verification. */
  777. ccw_device_online_verify(cdev, 0);
  778. if (cdev->handler)
  779. cdev->handler(cdev, cdev->private->intparm,
  780. ERR_PTR(-EIO));
  781. }
  782. void ccw_device_kill_io(struct ccw_device *cdev)
  783. {
  784. int ret;
  785. ret = ccw_device_cancel_halt_clear(cdev);
  786. if (ret == -EBUSY) {
  787. ccw_device_set_timeout(cdev, 3*HZ);
  788. cdev->private->state = DEV_STATE_TIMEOUT_KILL;
  789. return;
  790. }
  791. /* Start delayed path verification. */
  792. ccw_device_online_verify(cdev, 0);
  793. if (cdev->handler)
  794. cdev->handler(cdev, cdev->private->intparm,
  795. ERR_PTR(-EIO));
  796. }
  797. static void
  798. ccw_device_delay_verify(struct ccw_device *cdev, enum dev_event dev_event)
  799. {
  800. /* Start verification after current task finished. */
  801. cdev->private->flags.doverify = 1;
  802. }
  803. static void
  804. ccw_device_stlck_done(struct ccw_device *cdev, enum dev_event dev_event)
  805. {
  806. struct irb *irb;
  807. switch (dev_event) {
  808. case DEV_EVENT_INTERRUPT:
  809. irb = (struct irb *) __LC_IRB;
  810. /* Check for unsolicited interrupt. */
  811. if ((scsw_stctl(&irb->scsw) ==
  812. (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) &&
  813. (!scsw_cc(&irb->scsw)))
  814. /* FIXME: we should restart stlck here, but this
  815. * is extremely unlikely ... */
  816. goto out_wakeup;
  817. ccw_device_accumulate_irb(cdev, irb);
  818. /* We don't care about basic sense etc. */
  819. break;
  820. default: /* timeout */
  821. break;
  822. }
  823. out_wakeup:
  824. wake_up(&cdev->private->wait_q);
  825. }
  826. static void
  827. ccw_device_start_id(struct ccw_device *cdev, enum dev_event dev_event)
  828. {
  829. struct subchannel *sch;
  830. sch = to_subchannel(cdev->dev.parent);
  831. if (cio_enable_subchannel(sch, (u32)(addr_t)sch) != 0)
  832. /* Couldn't enable the subchannel for i/o. Sick device. */
  833. return;
  834. cdev->private->state = DEV_STATE_DISCONNECTED_SENSE_ID;
  835. ccw_device_sense_id_start(cdev);
  836. }
  837. void ccw_device_trigger_reprobe(struct ccw_device *cdev)
  838. {
  839. struct subchannel *sch;
  840. if (cdev->private->state != DEV_STATE_DISCONNECTED)
  841. return;
  842. sch = to_subchannel(cdev->dev.parent);
  843. /* Update some values. */
  844. if (cio_update_schib(sch))
  845. return;
  846. /*
  847. * The pim, pam, pom values may not be accurate, but they are the best
  848. * we have before performing device selection :/
  849. */
  850. sch->lpm = sch->schib.pmcw.pam & sch->opm;
  851. /*
  852. * Use the initial configuration since we can't be shure that the old
  853. * paths are valid.
  854. */
  855. io_subchannel_init_config(sch);
  856. if (cio_commit_config(sch))
  857. return;
  858. /* We should also udate ssd info, but this has to wait. */
  859. /* Check if this is another device which appeared on the same sch. */
  860. if (sch->schib.pmcw.dev != cdev->private->dev_id.devno)
  861. css_schedule_eval(sch->schid);
  862. else
  863. ccw_device_start_id(cdev, 0);
  864. }
  865. static void ccw_device_disabled_irq(struct ccw_device *cdev,
  866. enum dev_event dev_event)
  867. {
  868. struct subchannel *sch;
  869. sch = to_subchannel(cdev->dev.parent);
  870. /*
  871. * An interrupt in a disabled state means a previous disable was not
  872. * successful - should not happen, but we try to disable again.
  873. */
  874. cio_disable_subchannel(sch);
  875. }
  876. static void
  877. ccw_device_change_cmfstate(struct ccw_device *cdev, enum dev_event dev_event)
  878. {
  879. retry_set_schib(cdev);
  880. cdev->private->state = DEV_STATE_ONLINE;
  881. dev_fsm_event(cdev, dev_event);
  882. }
  883. static void ccw_device_update_cmfblock(struct ccw_device *cdev,
  884. enum dev_event dev_event)
  885. {
  886. cmf_retry_copy_block(cdev);
  887. cdev->private->state = DEV_STATE_ONLINE;
  888. dev_fsm_event(cdev, dev_event);
  889. }
  890. static void
  891. ccw_device_quiesce_done(struct ccw_device *cdev, enum dev_event dev_event)
  892. {
  893. ccw_device_set_timeout(cdev, 0);
  894. if (dev_event == DEV_EVENT_NOTOPER)
  895. cdev->private->state = DEV_STATE_NOT_OPER;
  896. else
  897. cdev->private->state = DEV_STATE_OFFLINE;
  898. wake_up(&cdev->private->wait_q);
  899. }
  900. static void
  901. ccw_device_quiesce_timeout(struct ccw_device *cdev, enum dev_event dev_event)
  902. {
  903. int ret;
  904. ret = ccw_device_cancel_halt_clear(cdev);
  905. switch (ret) {
  906. case 0:
  907. cdev->private->state = DEV_STATE_OFFLINE;
  908. wake_up(&cdev->private->wait_q);
  909. break;
  910. case -ENODEV:
  911. cdev->private->state = DEV_STATE_NOT_OPER;
  912. wake_up(&cdev->private->wait_q);
  913. break;
  914. default:
  915. ccw_device_set_timeout(cdev, HZ/10);
  916. }
  917. }
  918. /*
  919. * No operation action. This is used e.g. to ignore a timeout event in
  920. * state offline.
  921. */
  922. static void
  923. ccw_device_nop(struct ccw_device *cdev, enum dev_event dev_event)
  924. {
  925. }
  926. /*
  927. * device statemachine
  928. */
  929. fsm_func_t *dev_jumptable[NR_DEV_STATES][NR_DEV_EVENTS] = {
  930. [DEV_STATE_NOT_OPER] = {
  931. [DEV_EVENT_NOTOPER] = ccw_device_nop,
  932. [DEV_EVENT_INTERRUPT] = ccw_device_disabled_irq,
  933. [DEV_EVENT_TIMEOUT] = ccw_device_nop,
  934. [DEV_EVENT_VERIFY] = ccw_device_nop,
  935. },
  936. [DEV_STATE_SENSE_PGID] = {
  937. [DEV_EVENT_NOTOPER] = ccw_device_request_event,
  938. [DEV_EVENT_INTERRUPT] = ccw_device_request_event,
  939. [DEV_EVENT_TIMEOUT] = ccw_device_request_event,
  940. [DEV_EVENT_VERIFY] = ccw_device_nop,
  941. },
  942. [DEV_STATE_SENSE_ID] = {
  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_nop,
  947. },
  948. [DEV_STATE_OFFLINE] = {
  949. [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
  950. [DEV_EVENT_INTERRUPT] = ccw_device_disabled_irq,
  951. [DEV_EVENT_TIMEOUT] = ccw_device_nop,
  952. [DEV_EVENT_VERIFY] = ccw_device_offline_verify,
  953. },
  954. [DEV_STATE_VERIFY] = {
  955. [DEV_EVENT_NOTOPER] = ccw_device_request_event,
  956. [DEV_EVENT_INTERRUPT] = ccw_device_request_event,
  957. [DEV_EVENT_TIMEOUT] = ccw_device_request_event,
  958. [DEV_EVENT_VERIFY] = ccw_device_delay_verify,
  959. },
  960. [DEV_STATE_ONLINE] = {
  961. [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
  962. [DEV_EVENT_INTERRUPT] = ccw_device_irq,
  963. [DEV_EVENT_TIMEOUT] = ccw_device_online_timeout,
  964. [DEV_EVENT_VERIFY] = ccw_device_online_verify,
  965. },
  966. [DEV_STATE_W4SENSE] = {
  967. [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
  968. [DEV_EVENT_INTERRUPT] = ccw_device_w4sense,
  969. [DEV_EVENT_TIMEOUT] = ccw_device_nop,
  970. [DEV_EVENT_VERIFY] = ccw_device_online_verify,
  971. },
  972. [DEV_STATE_DISBAND_PGID] = {
  973. [DEV_EVENT_NOTOPER] = ccw_device_request_event,
  974. [DEV_EVENT_INTERRUPT] = ccw_device_request_event,
  975. [DEV_EVENT_TIMEOUT] = ccw_device_request_event,
  976. [DEV_EVENT_VERIFY] = ccw_device_nop,
  977. },
  978. [DEV_STATE_BOXED] = {
  979. [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
  980. [DEV_EVENT_INTERRUPT] = ccw_device_stlck_done,
  981. [DEV_EVENT_TIMEOUT] = ccw_device_stlck_done,
  982. [DEV_EVENT_VERIFY] = ccw_device_nop,
  983. },
  984. /* states to wait for i/o completion before doing something */
  985. [DEV_STATE_CLEAR_VERIFY] = {
  986. [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
  987. [DEV_EVENT_INTERRUPT] = ccw_device_clear_verify,
  988. [DEV_EVENT_TIMEOUT] = ccw_device_nop,
  989. [DEV_EVENT_VERIFY] = ccw_device_nop,
  990. },
  991. [DEV_STATE_TIMEOUT_KILL] = {
  992. [DEV_EVENT_NOTOPER] = ccw_device_generic_notoper,
  993. [DEV_EVENT_INTERRUPT] = ccw_device_killing_irq,
  994. [DEV_EVENT_TIMEOUT] = ccw_device_killing_timeout,
  995. [DEV_EVENT_VERIFY] = ccw_device_nop, //FIXME
  996. },
  997. [DEV_STATE_QUIESCE] = {
  998. [DEV_EVENT_NOTOPER] = ccw_device_quiesce_done,
  999. [DEV_EVENT_INTERRUPT] = ccw_device_quiesce_done,
  1000. [DEV_EVENT_TIMEOUT] = ccw_device_quiesce_timeout,
  1001. [DEV_EVENT_VERIFY] = ccw_device_nop,
  1002. },
  1003. /* special states for devices gone not operational */
  1004. [DEV_STATE_DISCONNECTED] = {
  1005. [DEV_EVENT_NOTOPER] = ccw_device_nop,
  1006. [DEV_EVENT_INTERRUPT] = ccw_device_start_id,
  1007. [DEV_EVENT_TIMEOUT] = ccw_device_nop,
  1008. [DEV_EVENT_VERIFY] = ccw_device_start_id,
  1009. },
  1010. [DEV_STATE_DISCONNECTED_SENSE_ID] = {
  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. [DEV_STATE_CMFCHANGE] = {
  1017. [DEV_EVENT_NOTOPER] = ccw_device_change_cmfstate,
  1018. [DEV_EVENT_INTERRUPT] = ccw_device_change_cmfstate,
  1019. [DEV_EVENT_TIMEOUT] = ccw_device_change_cmfstate,
  1020. [DEV_EVENT_VERIFY] = ccw_device_change_cmfstate,
  1021. },
  1022. [DEV_STATE_CMFUPDATE] = {
  1023. [DEV_EVENT_NOTOPER] = ccw_device_update_cmfblock,
  1024. [DEV_EVENT_INTERRUPT] = ccw_device_update_cmfblock,
  1025. [DEV_EVENT_TIMEOUT] = ccw_device_update_cmfblock,
  1026. [DEV_EVENT_VERIFY] = ccw_device_update_cmfblock,
  1027. },
  1028. };
  1029. EXPORT_SYMBOL_GPL(ccw_device_set_timeout);