device_fsm.c 31 KB

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