device_fsm.c 30 KB

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