device_fsm.c 28 KB

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