device_fsm.c 31 KB

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