device.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620
  1. /*
  2. * drivers/s390/cio/device.c
  3. * bus driver for ccw devices
  4. *
  5. * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
  6. * IBM Corporation
  7. * Author(s): Arnd Bergmann (arndb@de.ibm.com)
  8. * Cornelia Huck (cornelia.huck@de.ibm.com)
  9. * Martin Schwidefsky (schwidefsky@de.ibm.com)
  10. */
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/errno.h>
  15. #include <linux/err.h>
  16. #include <linux/slab.h>
  17. #include <linux/list.h>
  18. #include <linux/device.h>
  19. #include <linux/workqueue.h>
  20. #include <linux/timer.h>
  21. #include <asm/ccwdev.h>
  22. #include <asm/cio.h>
  23. #include <asm/param.h> /* HZ */
  24. #include <asm/cmb.h>
  25. #include "chp.h"
  26. #include "cio.h"
  27. #include "cio_debug.h"
  28. #include "css.h"
  29. #include "device.h"
  30. #include "ioasm.h"
  31. #include "io_sch.h"
  32. static struct timer_list recovery_timer;
  33. static DEFINE_SPINLOCK(recovery_lock);
  34. static int recovery_phase;
  35. static const unsigned long recovery_delay[] = { 3, 30, 300 };
  36. /******************* bus type handling ***********************/
  37. /* The Linux driver model distinguishes between a bus type and
  38. * the bus itself. Of course we only have one channel
  39. * subsystem driver and one channel system per machine, but
  40. * we still use the abstraction. T.R. says it's a good idea. */
  41. static int
  42. ccw_bus_match (struct device * dev, struct device_driver * drv)
  43. {
  44. struct ccw_device *cdev = to_ccwdev(dev);
  45. struct ccw_driver *cdrv = to_ccwdrv(drv);
  46. const struct ccw_device_id *ids = cdrv->ids, *found;
  47. if (!ids)
  48. return 0;
  49. found = ccw_device_id_match(ids, &cdev->id);
  50. if (!found)
  51. return 0;
  52. cdev->id.driver_info = found->driver_info;
  53. return 1;
  54. }
  55. /* Store modalias string delimited by prefix/suffix string into buffer with
  56. * specified size. Return length of resulting string (excluding trailing '\0')
  57. * even if string doesn't fit buffer (snprintf semantics). */
  58. static int snprint_alias(char *buf, size_t size,
  59. struct ccw_device_id *id, const char *suffix)
  60. {
  61. int len;
  62. len = snprintf(buf, size, "ccw:t%04Xm%02X", id->cu_type, id->cu_model);
  63. if (len > size)
  64. return len;
  65. buf += len;
  66. size -= len;
  67. if (id->dev_type != 0)
  68. len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type,
  69. id->dev_model, suffix);
  70. else
  71. len += snprintf(buf, size, "dtdm%s", suffix);
  72. return len;
  73. }
  74. /* Set up environment variables for ccw device uevent. Return 0 on success,
  75. * non-zero otherwise. */
  76. static int ccw_uevent(struct device *dev, struct kobj_uevent_env *env)
  77. {
  78. struct ccw_device *cdev = to_ccwdev(dev);
  79. struct ccw_device_id *id = &(cdev->id);
  80. int ret;
  81. char modalias_buf[30];
  82. /* CU_TYPE= */
  83. ret = add_uevent_var(env, "CU_TYPE=%04X", id->cu_type);
  84. if (ret)
  85. return ret;
  86. /* CU_MODEL= */
  87. ret = add_uevent_var(env, "CU_MODEL=%02X", id->cu_model);
  88. if (ret)
  89. return ret;
  90. /* The next two can be zero, that's ok for us */
  91. /* DEV_TYPE= */
  92. ret = add_uevent_var(env, "DEV_TYPE=%04X", id->dev_type);
  93. if (ret)
  94. return ret;
  95. /* DEV_MODEL= */
  96. ret = add_uevent_var(env, "DEV_MODEL=%02X", id->dev_model);
  97. if (ret)
  98. return ret;
  99. /* MODALIAS= */
  100. snprint_alias(modalias_buf, sizeof(modalias_buf), id, "");
  101. ret = add_uevent_var(env, "MODALIAS=%s", modalias_buf);
  102. return ret;
  103. }
  104. struct bus_type ccw_bus_type;
  105. static void io_subchannel_irq(struct subchannel *);
  106. static int io_subchannel_probe(struct subchannel *);
  107. static int io_subchannel_remove(struct subchannel *);
  108. static int io_subchannel_notify(struct subchannel *, int);
  109. static void io_subchannel_verify(struct subchannel *);
  110. static void io_subchannel_ioterm(struct subchannel *);
  111. static void io_subchannel_shutdown(struct subchannel *);
  112. static struct css_driver io_subchannel_driver = {
  113. .owner = THIS_MODULE,
  114. .subchannel_type = SUBCHANNEL_TYPE_IO,
  115. .name = "io_subchannel",
  116. .irq = io_subchannel_irq,
  117. .notify = io_subchannel_notify,
  118. .verify = io_subchannel_verify,
  119. .termination = io_subchannel_ioterm,
  120. .probe = io_subchannel_probe,
  121. .remove = io_subchannel_remove,
  122. .shutdown = io_subchannel_shutdown,
  123. };
  124. struct workqueue_struct *ccw_device_work;
  125. struct workqueue_struct *ccw_device_notify_work;
  126. wait_queue_head_t ccw_device_init_wq;
  127. atomic_t ccw_device_init_count;
  128. static void recovery_func(unsigned long data);
  129. static int __init
  130. init_ccw_bus_type (void)
  131. {
  132. int ret;
  133. init_waitqueue_head(&ccw_device_init_wq);
  134. atomic_set(&ccw_device_init_count, 0);
  135. setup_timer(&recovery_timer, recovery_func, 0);
  136. ccw_device_work = create_singlethread_workqueue("cio");
  137. if (!ccw_device_work)
  138. return -ENOMEM; /* FIXME: better errno ? */
  139. ccw_device_notify_work = create_singlethread_workqueue("cio_notify");
  140. if (!ccw_device_notify_work) {
  141. ret = -ENOMEM; /* FIXME: better errno ? */
  142. goto out_err;
  143. }
  144. slow_path_wq = create_singlethread_workqueue("kslowcrw");
  145. if (!slow_path_wq) {
  146. ret = -ENOMEM; /* FIXME: better errno ? */
  147. goto out_err;
  148. }
  149. if ((ret = bus_register (&ccw_bus_type)))
  150. goto out_err;
  151. ret = css_driver_register(&io_subchannel_driver);
  152. if (ret)
  153. goto out_err;
  154. wait_event(ccw_device_init_wq,
  155. atomic_read(&ccw_device_init_count) == 0);
  156. flush_workqueue(ccw_device_work);
  157. return 0;
  158. out_err:
  159. if (ccw_device_work)
  160. destroy_workqueue(ccw_device_work);
  161. if (ccw_device_notify_work)
  162. destroy_workqueue(ccw_device_notify_work);
  163. if (slow_path_wq)
  164. destroy_workqueue(slow_path_wq);
  165. return ret;
  166. }
  167. static void __exit
  168. cleanup_ccw_bus_type (void)
  169. {
  170. css_driver_unregister(&io_subchannel_driver);
  171. bus_unregister(&ccw_bus_type);
  172. destroy_workqueue(ccw_device_notify_work);
  173. destroy_workqueue(ccw_device_work);
  174. }
  175. subsys_initcall(init_ccw_bus_type);
  176. module_exit(cleanup_ccw_bus_type);
  177. /************************ device handling **************************/
  178. /*
  179. * A ccw_device has some interfaces in sysfs in addition to the
  180. * standard ones.
  181. * The following entries are designed to export the information which
  182. * resided in 2.4 in /proc/subchannels. Subchannel and device number
  183. * are obvious, so they don't have an entry :)
  184. * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
  185. */
  186. static ssize_t
  187. chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
  188. {
  189. struct subchannel *sch = to_subchannel(dev);
  190. struct chsc_ssd_info *ssd = &sch->ssd_info;
  191. ssize_t ret = 0;
  192. int chp;
  193. int mask;
  194. for (chp = 0; chp < 8; chp++) {
  195. mask = 0x80 >> chp;
  196. if (ssd->path_mask & mask)
  197. ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
  198. else
  199. ret += sprintf(buf + ret, "00 ");
  200. }
  201. ret += sprintf (buf+ret, "\n");
  202. return min((ssize_t)PAGE_SIZE, ret);
  203. }
  204. static ssize_t
  205. pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
  206. {
  207. struct subchannel *sch = to_subchannel(dev);
  208. struct pmcw *pmcw = &sch->schib.pmcw;
  209. return sprintf (buf, "%02x %02x %02x\n",
  210. pmcw->pim, pmcw->pam, pmcw->pom);
  211. }
  212. static ssize_t
  213. devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
  214. {
  215. struct ccw_device *cdev = to_ccwdev(dev);
  216. struct ccw_device_id *id = &(cdev->id);
  217. if (id->dev_type != 0)
  218. return sprintf(buf, "%04x/%02x\n",
  219. id->dev_type, id->dev_model);
  220. else
  221. return sprintf(buf, "n/a\n");
  222. }
  223. static ssize_t
  224. cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
  225. {
  226. struct ccw_device *cdev = to_ccwdev(dev);
  227. struct ccw_device_id *id = &(cdev->id);
  228. return sprintf(buf, "%04x/%02x\n",
  229. id->cu_type, id->cu_model);
  230. }
  231. static ssize_t
  232. modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
  233. {
  234. struct ccw_device *cdev = to_ccwdev(dev);
  235. struct ccw_device_id *id = &(cdev->id);
  236. int len;
  237. len = snprint_alias(buf, PAGE_SIZE, id, "\n");
  238. return len > PAGE_SIZE ? PAGE_SIZE : len;
  239. }
  240. static ssize_t
  241. online_show (struct device *dev, struct device_attribute *attr, char *buf)
  242. {
  243. struct ccw_device *cdev = to_ccwdev(dev);
  244. return sprintf(buf, cdev->online ? "1\n" : "0\n");
  245. }
  246. int ccw_device_is_orphan(struct ccw_device *cdev)
  247. {
  248. return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
  249. }
  250. static void ccw_device_unregister(struct ccw_device *cdev)
  251. {
  252. if (test_and_clear_bit(1, &cdev->private->registered))
  253. device_del(&cdev->dev);
  254. }
  255. static void ccw_device_remove_orphan_cb(struct device *dev)
  256. {
  257. struct ccw_device *cdev = to_ccwdev(dev);
  258. ccw_device_unregister(cdev);
  259. put_device(&cdev->dev);
  260. }
  261. static void ccw_device_remove_sch_cb(struct device *dev)
  262. {
  263. struct subchannel *sch;
  264. sch = to_subchannel(dev);
  265. css_sch_device_unregister(sch);
  266. /* Reset intparm to zeroes. */
  267. sch->schib.pmcw.intparm = 0;
  268. cio_modify(sch);
  269. put_device(&sch->dev);
  270. }
  271. static void
  272. ccw_device_remove_disconnected(struct ccw_device *cdev)
  273. {
  274. unsigned long flags;
  275. int rc;
  276. /*
  277. * Forced offline in disconnected state means
  278. * 'throw away device'.
  279. */
  280. if (ccw_device_is_orphan(cdev)) {
  281. /*
  282. * Deregister ccw device.
  283. * Unfortunately, we cannot do this directly from the
  284. * attribute method.
  285. */
  286. spin_lock_irqsave(cdev->ccwlock, flags);
  287. cdev->private->state = DEV_STATE_NOT_OPER;
  288. spin_unlock_irqrestore(cdev->ccwlock, flags);
  289. rc = device_schedule_callback(&cdev->dev,
  290. ccw_device_remove_orphan_cb);
  291. if (rc)
  292. CIO_MSG_EVENT(0, "Couldn't unregister orphan "
  293. "0.%x.%04x\n",
  294. cdev->private->dev_id.ssid,
  295. cdev->private->dev_id.devno);
  296. return;
  297. }
  298. /* Deregister subchannel, which will kill the ccw device. */
  299. rc = device_schedule_callback(cdev->dev.parent,
  300. ccw_device_remove_sch_cb);
  301. if (rc)
  302. CIO_MSG_EVENT(0, "Couldn't unregister disconnected device "
  303. "0.%x.%04x\n",
  304. cdev->private->dev_id.ssid,
  305. cdev->private->dev_id.devno);
  306. }
  307. /**
  308. * ccw_device_set_offline() - disable a ccw device for I/O
  309. * @cdev: target ccw device
  310. *
  311. * This function calls the driver's set_offline() function for @cdev, if
  312. * given, and then disables @cdev.
  313. * Returns:
  314. * %0 on success and a negative error value on failure.
  315. * Context:
  316. * enabled, ccw device lock not held
  317. */
  318. int ccw_device_set_offline(struct ccw_device *cdev)
  319. {
  320. int ret;
  321. if (!cdev)
  322. return -ENODEV;
  323. if (!cdev->online || !cdev->drv)
  324. return -EINVAL;
  325. if (cdev->drv->set_offline) {
  326. ret = cdev->drv->set_offline(cdev);
  327. if (ret != 0)
  328. return ret;
  329. }
  330. cdev->online = 0;
  331. spin_lock_irq(cdev->ccwlock);
  332. ret = ccw_device_offline(cdev);
  333. if (ret == -ENODEV) {
  334. if (cdev->private->state != DEV_STATE_NOT_OPER) {
  335. cdev->private->state = DEV_STATE_OFFLINE;
  336. dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
  337. }
  338. spin_unlock_irq(cdev->ccwlock);
  339. return ret;
  340. }
  341. spin_unlock_irq(cdev->ccwlock);
  342. if (ret == 0)
  343. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  344. else {
  345. CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
  346. "device 0.%x.%04x\n",
  347. ret, cdev->private->dev_id.ssid,
  348. cdev->private->dev_id.devno);
  349. cdev->online = 1;
  350. }
  351. return ret;
  352. }
  353. /**
  354. * ccw_device_set_online() - enable a ccw device for I/O
  355. * @cdev: target ccw device
  356. *
  357. * This function first enables @cdev and then calls the driver's set_online()
  358. * function for @cdev, if given. If set_online() returns an error, @cdev is
  359. * disabled again.
  360. * Returns:
  361. * %0 on success and a negative error value on failure.
  362. * Context:
  363. * enabled, ccw device lock not held
  364. */
  365. int ccw_device_set_online(struct ccw_device *cdev)
  366. {
  367. int ret;
  368. if (!cdev)
  369. return -ENODEV;
  370. if (cdev->online || !cdev->drv)
  371. return -EINVAL;
  372. spin_lock_irq(cdev->ccwlock);
  373. ret = ccw_device_online(cdev);
  374. spin_unlock_irq(cdev->ccwlock);
  375. if (ret == 0)
  376. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  377. else {
  378. CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
  379. "device 0.%x.%04x\n",
  380. ret, cdev->private->dev_id.ssid,
  381. cdev->private->dev_id.devno);
  382. return ret;
  383. }
  384. if (cdev->private->state != DEV_STATE_ONLINE)
  385. return -ENODEV;
  386. if (!cdev->drv->set_online || cdev->drv->set_online(cdev) == 0) {
  387. cdev->online = 1;
  388. return 0;
  389. }
  390. spin_lock_irq(cdev->ccwlock);
  391. ret = ccw_device_offline(cdev);
  392. spin_unlock_irq(cdev->ccwlock);
  393. if (ret == 0)
  394. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  395. else
  396. CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
  397. "device 0.%x.%04x\n",
  398. ret, cdev->private->dev_id.ssid,
  399. cdev->private->dev_id.devno);
  400. return (ret == 0) ? -ENODEV : ret;
  401. }
  402. static void online_store_handle_offline(struct ccw_device *cdev)
  403. {
  404. if (cdev->private->state == DEV_STATE_DISCONNECTED)
  405. ccw_device_remove_disconnected(cdev);
  406. else if (cdev->drv && cdev->drv->set_offline)
  407. ccw_device_set_offline(cdev);
  408. }
  409. static int online_store_recog_and_online(struct ccw_device *cdev)
  410. {
  411. int ret;
  412. /* Do device recognition, if needed. */
  413. if (cdev->id.cu_type == 0) {
  414. ret = ccw_device_recognition(cdev);
  415. if (ret) {
  416. CIO_MSG_EVENT(0, "Couldn't start recognition "
  417. "for device 0.%x.%04x (ret=%d)\n",
  418. cdev->private->dev_id.ssid,
  419. cdev->private->dev_id.devno, ret);
  420. return ret;
  421. }
  422. wait_event(cdev->private->wait_q,
  423. cdev->private->flags.recog_done);
  424. }
  425. if (cdev->drv && cdev->drv->set_online)
  426. ccw_device_set_online(cdev);
  427. return 0;
  428. }
  429. static void online_store_handle_online(struct ccw_device *cdev, int force)
  430. {
  431. int ret;
  432. ret = online_store_recog_and_online(cdev);
  433. if (ret)
  434. return;
  435. if (force && cdev->private->state == DEV_STATE_BOXED) {
  436. ret = ccw_device_stlck(cdev);
  437. if (ret) {
  438. dev_warn(&cdev->dev,
  439. "ccw_device_stlck returned %d!\n", ret);
  440. return;
  441. }
  442. if (cdev->id.cu_type == 0)
  443. cdev->private->state = DEV_STATE_NOT_OPER;
  444. online_store_recog_and_online(cdev);
  445. }
  446. }
  447. static ssize_t online_store (struct device *dev, struct device_attribute *attr,
  448. const char *buf, size_t count)
  449. {
  450. struct ccw_device *cdev = to_ccwdev(dev);
  451. int force, ret;
  452. unsigned long i;
  453. if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
  454. return -EAGAIN;
  455. if (cdev->drv && !try_module_get(cdev->drv->owner)) {
  456. atomic_set(&cdev->private->onoff, 0);
  457. return -EINVAL;
  458. }
  459. if (!strncmp(buf, "force\n", count)) {
  460. force = 1;
  461. i = 1;
  462. ret = 0;
  463. } else {
  464. force = 0;
  465. ret = strict_strtoul(buf, 16, &i);
  466. }
  467. if (ret)
  468. goto out;
  469. switch (i) {
  470. case 0:
  471. online_store_handle_offline(cdev);
  472. ret = count;
  473. break;
  474. case 1:
  475. online_store_handle_online(cdev, force);
  476. ret = count;
  477. break;
  478. default:
  479. ret = -EINVAL;
  480. }
  481. out:
  482. if (cdev->drv)
  483. module_put(cdev->drv->owner);
  484. atomic_set(&cdev->private->onoff, 0);
  485. return ret;
  486. }
  487. static ssize_t
  488. available_show (struct device *dev, struct device_attribute *attr, char *buf)
  489. {
  490. struct ccw_device *cdev = to_ccwdev(dev);
  491. struct subchannel *sch;
  492. if (ccw_device_is_orphan(cdev))
  493. return sprintf(buf, "no device\n");
  494. switch (cdev->private->state) {
  495. case DEV_STATE_BOXED:
  496. return sprintf(buf, "boxed\n");
  497. case DEV_STATE_DISCONNECTED:
  498. case DEV_STATE_DISCONNECTED_SENSE_ID:
  499. case DEV_STATE_NOT_OPER:
  500. sch = to_subchannel(dev->parent);
  501. if (!sch->lpm)
  502. return sprintf(buf, "no path\n");
  503. else
  504. return sprintf(buf, "no device\n");
  505. default:
  506. /* All other states considered fine. */
  507. return sprintf(buf, "good\n");
  508. }
  509. }
  510. static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
  511. static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
  512. static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
  513. static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
  514. static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
  515. static DEVICE_ATTR(online, 0644, online_show, online_store);
  516. static DEVICE_ATTR(availability, 0444, available_show, NULL);
  517. static struct attribute * subch_attrs[] = {
  518. &dev_attr_chpids.attr,
  519. &dev_attr_pimpampom.attr,
  520. NULL,
  521. };
  522. static struct attribute_group subch_attr_group = {
  523. .attrs = subch_attrs,
  524. };
  525. struct attribute_group *subch_attr_groups[] = {
  526. &subch_attr_group,
  527. NULL,
  528. };
  529. static struct attribute * ccwdev_attrs[] = {
  530. &dev_attr_devtype.attr,
  531. &dev_attr_cutype.attr,
  532. &dev_attr_modalias.attr,
  533. &dev_attr_online.attr,
  534. &dev_attr_cmb_enable.attr,
  535. &dev_attr_availability.attr,
  536. NULL,
  537. };
  538. static struct attribute_group ccwdev_attr_group = {
  539. .attrs = ccwdev_attrs,
  540. };
  541. static struct attribute_group *ccwdev_attr_groups[] = {
  542. &ccwdev_attr_group,
  543. NULL,
  544. };
  545. /* this is a simple abstraction for device_register that sets the
  546. * correct bus type and adds the bus specific files */
  547. static int ccw_device_register(struct ccw_device *cdev)
  548. {
  549. struct device *dev = &cdev->dev;
  550. int ret;
  551. dev->bus = &ccw_bus_type;
  552. if ((ret = device_add(dev)))
  553. return ret;
  554. set_bit(1, &cdev->private->registered);
  555. return ret;
  556. }
  557. struct match_data {
  558. struct ccw_dev_id dev_id;
  559. struct ccw_device * sibling;
  560. };
  561. static int
  562. match_devno(struct device * dev, void * data)
  563. {
  564. struct match_data * d = data;
  565. struct ccw_device * cdev;
  566. cdev = to_ccwdev(dev);
  567. if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
  568. !ccw_device_is_orphan(cdev) &&
  569. ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
  570. (cdev != d->sibling))
  571. return 1;
  572. return 0;
  573. }
  574. static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
  575. struct ccw_device *sibling)
  576. {
  577. struct device *dev;
  578. struct match_data data;
  579. data.dev_id = *dev_id;
  580. data.sibling = sibling;
  581. dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
  582. return dev ? to_ccwdev(dev) : NULL;
  583. }
  584. static int match_orphan(struct device *dev, void *data)
  585. {
  586. struct ccw_dev_id *dev_id;
  587. struct ccw_device *cdev;
  588. dev_id = data;
  589. cdev = to_ccwdev(dev);
  590. return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
  591. }
  592. static struct ccw_device *
  593. get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css,
  594. struct ccw_dev_id *dev_id)
  595. {
  596. struct device *dev;
  597. dev = device_find_child(&css->pseudo_subchannel->dev, dev_id,
  598. match_orphan);
  599. return dev ? to_ccwdev(dev) : NULL;
  600. }
  601. static void
  602. ccw_device_add_changed(struct work_struct *work)
  603. {
  604. struct ccw_device_private *priv;
  605. struct ccw_device *cdev;
  606. priv = container_of(work, struct ccw_device_private, kick_work);
  607. cdev = priv->cdev;
  608. if (device_add(&cdev->dev)) {
  609. put_device(&cdev->dev);
  610. return;
  611. }
  612. set_bit(1, &cdev->private->registered);
  613. }
  614. void ccw_device_do_unreg_rereg(struct work_struct *work)
  615. {
  616. struct ccw_device_private *priv;
  617. struct ccw_device *cdev;
  618. struct subchannel *sch;
  619. priv = container_of(work, struct ccw_device_private, kick_work);
  620. cdev = priv->cdev;
  621. sch = to_subchannel(cdev->dev.parent);
  622. ccw_device_unregister(cdev);
  623. PREPARE_WORK(&cdev->private->kick_work,
  624. ccw_device_add_changed);
  625. queue_work(ccw_device_work, &cdev->private->kick_work);
  626. }
  627. static void
  628. ccw_device_release(struct device *dev)
  629. {
  630. struct ccw_device *cdev;
  631. cdev = to_ccwdev(dev);
  632. kfree(cdev->private);
  633. kfree(cdev);
  634. }
  635. static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
  636. {
  637. struct ccw_device *cdev;
  638. cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
  639. if (cdev) {
  640. cdev->private = kzalloc(sizeof(struct ccw_device_private),
  641. GFP_KERNEL | GFP_DMA);
  642. if (cdev->private)
  643. return cdev;
  644. }
  645. kfree(cdev);
  646. return ERR_PTR(-ENOMEM);
  647. }
  648. static int io_subchannel_initialize_dev(struct subchannel *sch,
  649. struct ccw_device *cdev)
  650. {
  651. cdev->private->cdev = cdev;
  652. atomic_set(&cdev->private->onoff, 0);
  653. cdev->dev.parent = &sch->dev;
  654. cdev->dev.release = ccw_device_release;
  655. INIT_WORK(&cdev->private->kick_work, NULL);
  656. cdev->dev.groups = ccwdev_attr_groups;
  657. /* Do first half of device_register. */
  658. device_initialize(&cdev->dev);
  659. if (!get_device(&sch->dev)) {
  660. if (cdev->dev.release)
  661. cdev->dev.release(&cdev->dev);
  662. return -ENODEV;
  663. }
  664. return 0;
  665. }
  666. static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
  667. {
  668. struct ccw_device *cdev;
  669. int ret;
  670. cdev = io_subchannel_allocate_dev(sch);
  671. if (!IS_ERR(cdev)) {
  672. ret = io_subchannel_initialize_dev(sch, cdev);
  673. if (ret) {
  674. kfree(cdev);
  675. cdev = ERR_PTR(ret);
  676. }
  677. }
  678. return cdev;
  679. }
  680. static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
  681. static void sch_attach_device(struct subchannel *sch,
  682. struct ccw_device *cdev)
  683. {
  684. css_update_ssd_info(sch);
  685. spin_lock_irq(sch->lock);
  686. sch_set_cdev(sch, cdev);
  687. cdev->private->schid = sch->schid;
  688. cdev->ccwlock = sch->lock;
  689. device_trigger_reprobe(sch);
  690. spin_unlock_irq(sch->lock);
  691. }
  692. static void sch_attach_disconnected_device(struct subchannel *sch,
  693. struct ccw_device *cdev)
  694. {
  695. struct subchannel *other_sch;
  696. int ret;
  697. other_sch = to_subchannel(get_device(cdev->dev.parent));
  698. ret = device_move(&cdev->dev, &sch->dev);
  699. if (ret) {
  700. CIO_MSG_EVENT(0, "Moving disconnected device 0.%x.%04x failed "
  701. "(ret=%d)!\n", cdev->private->dev_id.ssid,
  702. cdev->private->dev_id.devno, ret);
  703. put_device(&other_sch->dev);
  704. return;
  705. }
  706. sch_set_cdev(other_sch, NULL);
  707. /* No need to keep a subchannel without ccw device around. */
  708. css_sch_device_unregister(other_sch);
  709. put_device(&other_sch->dev);
  710. sch_attach_device(sch, cdev);
  711. }
  712. static void sch_attach_orphaned_device(struct subchannel *sch,
  713. struct ccw_device *cdev)
  714. {
  715. int ret;
  716. /* Try to move the ccw device to its new subchannel. */
  717. ret = device_move(&cdev->dev, &sch->dev);
  718. if (ret) {
  719. CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
  720. "failed (ret=%d)!\n",
  721. cdev->private->dev_id.ssid,
  722. cdev->private->dev_id.devno, ret);
  723. return;
  724. }
  725. sch_attach_device(sch, cdev);
  726. }
  727. static void sch_create_and_recog_new_device(struct subchannel *sch)
  728. {
  729. struct ccw_device *cdev;
  730. /* Need to allocate a new ccw device. */
  731. cdev = io_subchannel_create_ccwdev(sch);
  732. if (IS_ERR(cdev)) {
  733. /* OK, we did everything we could... */
  734. css_sch_device_unregister(sch);
  735. return;
  736. }
  737. spin_lock_irq(sch->lock);
  738. sch_set_cdev(sch, cdev);
  739. spin_unlock_irq(sch->lock);
  740. /* Start recognition for the new ccw device. */
  741. if (io_subchannel_recog(cdev, sch)) {
  742. spin_lock_irq(sch->lock);
  743. sch_set_cdev(sch, NULL);
  744. spin_unlock_irq(sch->lock);
  745. if (cdev->dev.release)
  746. cdev->dev.release(&cdev->dev);
  747. css_sch_device_unregister(sch);
  748. }
  749. }
  750. void ccw_device_move_to_orphanage(struct work_struct *work)
  751. {
  752. struct ccw_device_private *priv;
  753. struct ccw_device *cdev;
  754. struct ccw_device *replacing_cdev;
  755. struct subchannel *sch;
  756. int ret;
  757. struct channel_subsystem *css;
  758. struct ccw_dev_id dev_id;
  759. priv = container_of(work, struct ccw_device_private, kick_work);
  760. cdev = priv->cdev;
  761. sch = to_subchannel(cdev->dev.parent);
  762. css = to_css(sch->dev.parent);
  763. dev_id.devno = sch->schib.pmcw.dev;
  764. dev_id.ssid = sch->schid.ssid;
  765. /*
  766. * Move the orphaned ccw device to the orphanage so the replacing
  767. * ccw device can take its place on the subchannel.
  768. */
  769. ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev);
  770. if (ret) {
  771. CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
  772. "(ret=%d)!\n", cdev->private->dev_id.ssid,
  773. cdev->private->dev_id.devno, ret);
  774. return;
  775. }
  776. cdev->ccwlock = css->pseudo_subchannel->lock;
  777. /*
  778. * Search for the replacing ccw device
  779. * - among the disconnected devices
  780. * - in the orphanage
  781. */
  782. replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
  783. if (replacing_cdev) {
  784. sch_attach_disconnected_device(sch, replacing_cdev);
  785. return;
  786. }
  787. replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id);
  788. if (replacing_cdev) {
  789. sch_attach_orphaned_device(sch, replacing_cdev);
  790. return;
  791. }
  792. sch_create_and_recog_new_device(sch);
  793. }
  794. /*
  795. * Register recognized device.
  796. */
  797. static void
  798. io_subchannel_register(struct work_struct *work)
  799. {
  800. struct ccw_device_private *priv;
  801. struct ccw_device *cdev;
  802. struct subchannel *sch;
  803. int ret;
  804. unsigned long flags;
  805. priv = container_of(work, struct ccw_device_private, kick_work);
  806. cdev = priv->cdev;
  807. sch = to_subchannel(cdev->dev.parent);
  808. css_update_ssd_info(sch);
  809. /*
  810. * io_subchannel_register() will also be called after device
  811. * recognition has been done for a boxed device (which will already
  812. * be registered). We need to reprobe since we may now have sense id
  813. * information.
  814. */
  815. if (klist_node_attached(&cdev->dev.knode_parent)) {
  816. if (!cdev->drv) {
  817. ret = device_reprobe(&cdev->dev);
  818. if (ret)
  819. /* We can't do much here. */
  820. CIO_MSG_EVENT(0, "device_reprobe() returned"
  821. " %d for 0.%x.%04x\n", ret,
  822. cdev->private->dev_id.ssid,
  823. cdev->private->dev_id.devno);
  824. }
  825. goto out;
  826. }
  827. /*
  828. * Now we know this subchannel will stay, we can throw
  829. * our delayed uevent.
  830. */
  831. sch->dev.uevent_suppress = 0;
  832. kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
  833. /* make it known to the system */
  834. ret = ccw_device_register(cdev);
  835. if (ret) {
  836. CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
  837. cdev->private->dev_id.ssid,
  838. cdev->private->dev_id.devno, ret);
  839. put_device(&cdev->dev);
  840. spin_lock_irqsave(sch->lock, flags);
  841. sch_set_cdev(sch, NULL);
  842. spin_unlock_irqrestore(sch->lock, flags);
  843. kfree (cdev->private);
  844. kfree (cdev);
  845. put_device(&sch->dev);
  846. if (atomic_dec_and_test(&ccw_device_init_count))
  847. wake_up(&ccw_device_init_wq);
  848. return;
  849. }
  850. put_device(&cdev->dev);
  851. out:
  852. cdev->private->flags.recog_done = 1;
  853. put_device(&sch->dev);
  854. wake_up(&cdev->private->wait_q);
  855. if (atomic_dec_and_test(&ccw_device_init_count))
  856. wake_up(&ccw_device_init_wq);
  857. }
  858. static void ccw_device_call_sch_unregister(struct work_struct *work)
  859. {
  860. struct ccw_device_private *priv;
  861. struct ccw_device *cdev;
  862. struct subchannel *sch;
  863. priv = container_of(work, struct ccw_device_private, kick_work);
  864. cdev = priv->cdev;
  865. sch = to_subchannel(cdev->dev.parent);
  866. css_sch_device_unregister(sch);
  867. /* Reset intparm to zeroes. */
  868. sch->schib.pmcw.intparm = 0;
  869. cio_modify(sch);
  870. put_device(&cdev->dev);
  871. put_device(&sch->dev);
  872. }
  873. /*
  874. * subchannel recognition done. Called from the state machine.
  875. */
  876. void
  877. io_subchannel_recog_done(struct ccw_device *cdev)
  878. {
  879. struct subchannel *sch;
  880. if (css_init_done == 0) {
  881. cdev->private->flags.recog_done = 1;
  882. return;
  883. }
  884. switch (cdev->private->state) {
  885. case DEV_STATE_NOT_OPER:
  886. cdev->private->flags.recog_done = 1;
  887. /* Remove device found not operational. */
  888. if (!get_device(&cdev->dev))
  889. break;
  890. sch = to_subchannel(cdev->dev.parent);
  891. PREPARE_WORK(&cdev->private->kick_work,
  892. ccw_device_call_sch_unregister);
  893. queue_work(slow_path_wq, &cdev->private->kick_work);
  894. if (atomic_dec_and_test(&ccw_device_init_count))
  895. wake_up(&ccw_device_init_wq);
  896. break;
  897. case DEV_STATE_BOXED:
  898. /* Device did not respond in time. */
  899. case DEV_STATE_OFFLINE:
  900. /*
  901. * We can't register the device in interrupt context so
  902. * we schedule a work item.
  903. */
  904. if (!get_device(&cdev->dev))
  905. break;
  906. PREPARE_WORK(&cdev->private->kick_work,
  907. io_subchannel_register);
  908. queue_work(slow_path_wq, &cdev->private->kick_work);
  909. break;
  910. }
  911. }
  912. static int
  913. io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
  914. {
  915. int rc;
  916. struct ccw_device_private *priv;
  917. sch_set_cdev(sch, cdev);
  918. cdev->ccwlock = sch->lock;
  919. /* Init private data. */
  920. priv = cdev->private;
  921. priv->dev_id.devno = sch->schib.pmcw.dev;
  922. priv->dev_id.ssid = sch->schid.ssid;
  923. priv->schid = sch->schid;
  924. priv->state = DEV_STATE_NOT_OPER;
  925. INIT_LIST_HEAD(&priv->cmb_list);
  926. init_waitqueue_head(&priv->wait_q);
  927. init_timer(&priv->timer);
  928. /* Set an initial name for the device. */
  929. snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x",
  930. sch->schid.ssid, sch->schib.pmcw.dev);
  931. /* Increase counter of devices currently in recognition. */
  932. atomic_inc(&ccw_device_init_count);
  933. /* Start async. device sensing. */
  934. spin_lock_irq(sch->lock);
  935. rc = ccw_device_recognition(cdev);
  936. spin_unlock_irq(sch->lock);
  937. if (rc) {
  938. if (atomic_dec_and_test(&ccw_device_init_count))
  939. wake_up(&ccw_device_init_wq);
  940. }
  941. return rc;
  942. }
  943. static void ccw_device_move_to_sch(struct work_struct *work)
  944. {
  945. struct ccw_device_private *priv;
  946. int rc;
  947. struct subchannel *sch;
  948. struct ccw_device *cdev;
  949. struct subchannel *former_parent;
  950. priv = container_of(work, struct ccw_device_private, kick_work);
  951. sch = priv->sch;
  952. cdev = priv->cdev;
  953. former_parent = ccw_device_is_orphan(cdev) ?
  954. NULL : to_subchannel(get_device(cdev->dev.parent));
  955. mutex_lock(&sch->reg_mutex);
  956. /* Try to move the ccw device to its new subchannel. */
  957. rc = device_move(&cdev->dev, &sch->dev);
  958. mutex_unlock(&sch->reg_mutex);
  959. if (rc) {
  960. CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to subchannel "
  961. "0.%x.%04x failed (ret=%d)!\n",
  962. cdev->private->dev_id.ssid,
  963. cdev->private->dev_id.devno, sch->schid.ssid,
  964. sch->schid.sch_no, rc);
  965. css_sch_device_unregister(sch);
  966. goto out;
  967. }
  968. if (former_parent) {
  969. spin_lock_irq(former_parent->lock);
  970. sch_set_cdev(former_parent, NULL);
  971. spin_unlock_irq(former_parent->lock);
  972. css_sch_device_unregister(former_parent);
  973. /* Reset intparm to zeroes. */
  974. former_parent->schib.pmcw.intparm = 0;
  975. cio_modify(former_parent);
  976. }
  977. sch_attach_device(sch, cdev);
  978. out:
  979. if (former_parent)
  980. put_device(&former_parent->dev);
  981. put_device(&cdev->dev);
  982. }
  983. static void io_subchannel_irq(struct subchannel *sch)
  984. {
  985. struct ccw_device *cdev;
  986. cdev = sch_get_cdev(sch);
  987. CIO_TRACE_EVENT(3, "IRQ");
  988. CIO_TRACE_EVENT(3, sch->dev.bus_id);
  989. if (cdev)
  990. dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
  991. }
  992. static void io_subchannel_init_fields(struct subchannel *sch)
  993. {
  994. if (cio_is_console(sch->schid))
  995. sch->opm = 0xff;
  996. else
  997. sch->opm = chp_get_sch_opm(sch);
  998. sch->lpm = sch->schib.pmcw.pam & sch->opm;
  999. sch->isc = cio_is_console(sch->schid) ? 1 : 3;
  1000. CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
  1001. " - PIM = %02X, PAM = %02X, POM = %02X\n",
  1002. sch->schib.pmcw.dev, sch->schid.ssid,
  1003. sch->schid.sch_no, sch->schib.pmcw.pim,
  1004. sch->schib.pmcw.pam, sch->schib.pmcw.pom);
  1005. /* Initially set up some fields in the pmcw. */
  1006. sch->schib.pmcw.ena = 0;
  1007. sch->schib.pmcw.csense = 1; /* concurrent sense */
  1008. if ((sch->lpm & (sch->lpm - 1)) != 0)
  1009. sch->schib.pmcw.mp = 1; /* multipath mode */
  1010. /* clean up possible residual cmf stuff */
  1011. sch->schib.pmcw.mme = 0;
  1012. sch->schib.pmcw.mbfc = 0;
  1013. sch->schib.pmcw.mbi = 0;
  1014. sch->schib.mba = 0;
  1015. }
  1016. static int io_subchannel_probe(struct subchannel *sch)
  1017. {
  1018. struct ccw_device *cdev;
  1019. int rc;
  1020. unsigned long flags;
  1021. struct ccw_dev_id dev_id;
  1022. cdev = sch_get_cdev(sch);
  1023. if (cdev) {
  1024. /*
  1025. * This subchannel already has an associated ccw_device.
  1026. * Register it and exit. This happens for all early
  1027. * device, e.g. the console.
  1028. */
  1029. cdev->dev.groups = ccwdev_attr_groups;
  1030. device_initialize(&cdev->dev);
  1031. ccw_device_register(cdev);
  1032. /*
  1033. * Check if the device is already online. If it is
  1034. * the reference count needs to be corrected
  1035. * (see ccw_device_online and css_init_done for the
  1036. * ugly details).
  1037. */
  1038. if (cdev->private->state != DEV_STATE_NOT_OPER &&
  1039. cdev->private->state != DEV_STATE_OFFLINE &&
  1040. cdev->private->state != DEV_STATE_BOXED)
  1041. get_device(&cdev->dev);
  1042. return 0;
  1043. }
  1044. io_subchannel_init_fields(sch);
  1045. /*
  1046. * First check if a fitting device may be found amongst the
  1047. * disconnected devices or in the orphanage.
  1048. */
  1049. dev_id.devno = sch->schib.pmcw.dev;
  1050. dev_id.ssid = sch->schid.ssid;
  1051. /* Allocate I/O subchannel private data. */
  1052. sch->private = kzalloc(sizeof(struct io_subchannel_private),
  1053. GFP_KERNEL | GFP_DMA);
  1054. if (!sch->private)
  1055. return -ENOMEM;
  1056. cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL);
  1057. if (!cdev)
  1058. cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent),
  1059. &dev_id);
  1060. if (cdev) {
  1061. /*
  1062. * Schedule moving the device until when we have a registered
  1063. * subchannel to move to and succeed the probe. We can
  1064. * unregister later again, when the probe is through.
  1065. */
  1066. cdev->private->sch = sch;
  1067. PREPARE_WORK(&cdev->private->kick_work,
  1068. ccw_device_move_to_sch);
  1069. queue_work(slow_path_wq, &cdev->private->kick_work);
  1070. return 0;
  1071. }
  1072. cdev = io_subchannel_create_ccwdev(sch);
  1073. if (IS_ERR(cdev)) {
  1074. kfree(sch->private);
  1075. return PTR_ERR(cdev);
  1076. }
  1077. rc = io_subchannel_recog(cdev, sch);
  1078. if (rc) {
  1079. spin_lock_irqsave(sch->lock, flags);
  1080. sch_set_cdev(sch, NULL);
  1081. spin_unlock_irqrestore(sch->lock, flags);
  1082. if (cdev->dev.release)
  1083. cdev->dev.release(&cdev->dev);
  1084. kfree(sch->private);
  1085. }
  1086. return rc;
  1087. }
  1088. static int
  1089. io_subchannel_remove (struct subchannel *sch)
  1090. {
  1091. struct ccw_device *cdev;
  1092. unsigned long flags;
  1093. cdev = sch_get_cdev(sch);
  1094. if (!cdev)
  1095. return 0;
  1096. /* Set ccw device to not operational and drop reference. */
  1097. spin_lock_irqsave(cdev->ccwlock, flags);
  1098. sch_set_cdev(sch, NULL);
  1099. cdev->private->state = DEV_STATE_NOT_OPER;
  1100. spin_unlock_irqrestore(cdev->ccwlock, flags);
  1101. ccw_device_unregister(cdev);
  1102. put_device(&cdev->dev);
  1103. kfree(sch->private);
  1104. return 0;
  1105. }
  1106. static int io_subchannel_notify(struct subchannel *sch, int event)
  1107. {
  1108. struct ccw_device *cdev;
  1109. cdev = sch_get_cdev(sch);
  1110. if (!cdev)
  1111. return 0;
  1112. if (!cdev->drv)
  1113. return 0;
  1114. if (!cdev->online)
  1115. return 0;
  1116. return cdev->drv->notify ? cdev->drv->notify(cdev, event) : 0;
  1117. }
  1118. static void io_subchannel_verify(struct subchannel *sch)
  1119. {
  1120. struct ccw_device *cdev;
  1121. cdev = sch_get_cdev(sch);
  1122. if (cdev)
  1123. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1124. }
  1125. static void io_subchannel_ioterm(struct subchannel *sch)
  1126. {
  1127. struct ccw_device *cdev;
  1128. cdev = sch_get_cdev(sch);
  1129. if (!cdev)
  1130. return;
  1131. /* Internal I/O will be retried by the interrupt handler. */
  1132. if (cdev->private->flags.intretry)
  1133. return;
  1134. cdev->private->state = DEV_STATE_CLEAR_VERIFY;
  1135. if (cdev->handler)
  1136. cdev->handler(cdev, cdev->private->intparm,
  1137. ERR_PTR(-EIO));
  1138. }
  1139. static void
  1140. io_subchannel_shutdown(struct subchannel *sch)
  1141. {
  1142. struct ccw_device *cdev;
  1143. int ret;
  1144. cdev = sch_get_cdev(sch);
  1145. if (cio_is_console(sch->schid))
  1146. return;
  1147. if (!sch->schib.pmcw.ena)
  1148. /* Nothing to do. */
  1149. return;
  1150. ret = cio_disable_subchannel(sch);
  1151. if (ret != -EBUSY)
  1152. /* Subchannel is disabled, we're done. */
  1153. return;
  1154. cdev->private->state = DEV_STATE_QUIESCE;
  1155. if (cdev->handler)
  1156. cdev->handler(cdev, cdev->private->intparm,
  1157. ERR_PTR(-EIO));
  1158. ret = ccw_device_cancel_halt_clear(cdev);
  1159. if (ret == -EBUSY) {
  1160. ccw_device_set_timeout(cdev, HZ/10);
  1161. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  1162. }
  1163. cio_disable_subchannel(sch);
  1164. }
  1165. #ifdef CONFIG_CCW_CONSOLE
  1166. static struct ccw_device console_cdev;
  1167. static struct ccw_device_private console_private;
  1168. static int console_cdev_in_use;
  1169. static DEFINE_SPINLOCK(ccw_console_lock);
  1170. spinlock_t * cio_get_console_lock(void)
  1171. {
  1172. return &ccw_console_lock;
  1173. }
  1174. static int ccw_device_console_enable(struct ccw_device *cdev,
  1175. struct subchannel *sch)
  1176. {
  1177. int rc;
  1178. /* Attach subchannel private data. */
  1179. sch->private = cio_get_console_priv();
  1180. memset(sch->private, 0, sizeof(struct io_subchannel_private));
  1181. io_subchannel_init_fields(sch);
  1182. sch->driver = &io_subchannel_driver;
  1183. /* Initialize the ccw_device structure. */
  1184. cdev->dev.parent= &sch->dev;
  1185. rc = io_subchannel_recog(cdev, sch);
  1186. if (rc)
  1187. return rc;
  1188. /* Now wait for the async. recognition to come to an end. */
  1189. spin_lock_irq(cdev->ccwlock);
  1190. while (!dev_fsm_final_state(cdev))
  1191. wait_cons_dev();
  1192. rc = -EIO;
  1193. if (cdev->private->state != DEV_STATE_OFFLINE)
  1194. goto out_unlock;
  1195. ccw_device_online(cdev);
  1196. while (!dev_fsm_final_state(cdev))
  1197. wait_cons_dev();
  1198. if (cdev->private->state != DEV_STATE_ONLINE)
  1199. goto out_unlock;
  1200. rc = 0;
  1201. out_unlock:
  1202. spin_unlock_irq(cdev->ccwlock);
  1203. return 0;
  1204. }
  1205. struct ccw_device *
  1206. ccw_device_probe_console(void)
  1207. {
  1208. struct subchannel *sch;
  1209. int ret;
  1210. if (xchg(&console_cdev_in_use, 1) != 0)
  1211. return ERR_PTR(-EBUSY);
  1212. sch = cio_probe_console();
  1213. if (IS_ERR(sch)) {
  1214. console_cdev_in_use = 0;
  1215. return (void *) sch;
  1216. }
  1217. memset(&console_cdev, 0, sizeof(struct ccw_device));
  1218. memset(&console_private, 0, sizeof(struct ccw_device_private));
  1219. console_cdev.private = &console_private;
  1220. console_private.cdev = &console_cdev;
  1221. ret = ccw_device_console_enable(&console_cdev, sch);
  1222. if (ret) {
  1223. cio_release_console();
  1224. console_cdev_in_use = 0;
  1225. return ERR_PTR(ret);
  1226. }
  1227. console_cdev.online = 1;
  1228. return &console_cdev;
  1229. }
  1230. #endif
  1231. /*
  1232. * get ccw_device matching the busid, but only if owned by cdrv
  1233. */
  1234. static int
  1235. __ccwdev_check_busid(struct device *dev, void *id)
  1236. {
  1237. char *bus_id;
  1238. bus_id = id;
  1239. return (strncmp(bus_id, dev->bus_id, BUS_ID_SIZE) == 0);
  1240. }
  1241. /**
  1242. * get_ccwdev_by_busid() - obtain device from a bus id
  1243. * @cdrv: driver the device is owned by
  1244. * @bus_id: bus id of the device to be searched
  1245. *
  1246. * This function searches all devices owned by @cdrv for a device with a bus
  1247. * id matching @bus_id.
  1248. * Returns:
  1249. * If a match is found, its reference count of the found device is increased
  1250. * and it is returned; else %NULL is returned.
  1251. */
  1252. struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
  1253. const char *bus_id)
  1254. {
  1255. struct device *dev;
  1256. struct device_driver *drv;
  1257. drv = get_driver(&cdrv->driver);
  1258. if (!drv)
  1259. return NULL;
  1260. dev = driver_find_device(drv, NULL, (void *)bus_id,
  1261. __ccwdev_check_busid);
  1262. put_driver(drv);
  1263. return dev ? to_ccwdev(dev) : NULL;
  1264. }
  1265. /************************** device driver handling ************************/
  1266. /* This is the implementation of the ccw_driver class. The probe, remove
  1267. * and release methods are initially very similar to the device_driver
  1268. * implementations, with the difference that they have ccw_device
  1269. * arguments.
  1270. *
  1271. * A ccw driver also contains the information that is needed for
  1272. * device matching.
  1273. */
  1274. static int
  1275. ccw_device_probe (struct device *dev)
  1276. {
  1277. struct ccw_device *cdev = to_ccwdev(dev);
  1278. struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
  1279. int ret;
  1280. cdev->drv = cdrv; /* to let the driver call _set_online */
  1281. ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
  1282. if (ret) {
  1283. cdev->drv = NULL;
  1284. return ret;
  1285. }
  1286. return 0;
  1287. }
  1288. static int
  1289. ccw_device_remove (struct device *dev)
  1290. {
  1291. struct ccw_device *cdev = to_ccwdev(dev);
  1292. struct ccw_driver *cdrv = cdev->drv;
  1293. int ret;
  1294. if (cdrv->remove)
  1295. cdrv->remove(cdev);
  1296. if (cdev->online) {
  1297. cdev->online = 0;
  1298. spin_lock_irq(cdev->ccwlock);
  1299. ret = ccw_device_offline(cdev);
  1300. spin_unlock_irq(cdev->ccwlock);
  1301. if (ret == 0)
  1302. wait_event(cdev->private->wait_q,
  1303. dev_fsm_final_state(cdev));
  1304. else
  1305. CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
  1306. "device 0.%x.%04x\n",
  1307. ret, cdev->private->dev_id.ssid,
  1308. cdev->private->dev_id.devno);
  1309. }
  1310. ccw_device_set_timeout(cdev, 0);
  1311. cdev->drv = NULL;
  1312. return 0;
  1313. }
  1314. static void ccw_device_shutdown(struct device *dev)
  1315. {
  1316. struct ccw_device *cdev;
  1317. cdev = to_ccwdev(dev);
  1318. if (cdev->drv && cdev->drv->shutdown)
  1319. cdev->drv->shutdown(cdev);
  1320. disable_cmf(cdev);
  1321. }
  1322. struct bus_type ccw_bus_type = {
  1323. .name = "ccw",
  1324. .match = ccw_bus_match,
  1325. .uevent = ccw_uevent,
  1326. .probe = ccw_device_probe,
  1327. .remove = ccw_device_remove,
  1328. .shutdown = ccw_device_shutdown,
  1329. };
  1330. /**
  1331. * ccw_driver_register() - register a ccw driver
  1332. * @cdriver: driver to be registered
  1333. *
  1334. * This function is mainly a wrapper around driver_register().
  1335. * Returns:
  1336. * %0 on success and a negative error value on failure.
  1337. */
  1338. int ccw_driver_register(struct ccw_driver *cdriver)
  1339. {
  1340. struct device_driver *drv = &cdriver->driver;
  1341. drv->bus = &ccw_bus_type;
  1342. drv->name = cdriver->name;
  1343. drv->owner = cdriver->owner;
  1344. return driver_register(drv);
  1345. }
  1346. /**
  1347. * ccw_driver_unregister() - deregister a ccw driver
  1348. * @cdriver: driver to be deregistered
  1349. *
  1350. * This function is mainly a wrapper around driver_unregister().
  1351. */
  1352. void ccw_driver_unregister(struct ccw_driver *cdriver)
  1353. {
  1354. driver_unregister(&cdriver->driver);
  1355. }
  1356. /* Helper func for qdio. */
  1357. struct subchannel_id
  1358. ccw_device_get_subchannel_id(struct ccw_device *cdev)
  1359. {
  1360. struct subchannel *sch;
  1361. sch = to_subchannel(cdev->dev.parent);
  1362. return sch->schid;
  1363. }
  1364. static int recovery_check(struct device *dev, void *data)
  1365. {
  1366. struct ccw_device *cdev = to_ccwdev(dev);
  1367. int *redo = data;
  1368. spin_lock_irq(cdev->ccwlock);
  1369. switch (cdev->private->state) {
  1370. case DEV_STATE_DISCONNECTED:
  1371. CIO_MSG_EVENT(4, "recovery: trigger 0.%x.%04x\n",
  1372. cdev->private->dev_id.ssid,
  1373. cdev->private->dev_id.devno);
  1374. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1375. *redo = 1;
  1376. break;
  1377. case DEV_STATE_DISCONNECTED_SENSE_ID:
  1378. *redo = 1;
  1379. break;
  1380. }
  1381. spin_unlock_irq(cdev->ccwlock);
  1382. return 0;
  1383. }
  1384. static void recovery_work_func(struct work_struct *unused)
  1385. {
  1386. int redo = 0;
  1387. bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
  1388. if (redo) {
  1389. spin_lock_irq(&recovery_lock);
  1390. if (!timer_pending(&recovery_timer)) {
  1391. if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
  1392. recovery_phase++;
  1393. mod_timer(&recovery_timer, jiffies +
  1394. recovery_delay[recovery_phase] * HZ);
  1395. }
  1396. spin_unlock_irq(&recovery_lock);
  1397. } else
  1398. CIO_MSG_EVENT(4, "recovery: end\n");
  1399. }
  1400. static DECLARE_WORK(recovery_work, recovery_work_func);
  1401. static void recovery_func(unsigned long data)
  1402. {
  1403. /*
  1404. * We can't do our recovery in softirq context and it's not
  1405. * performance critical, so we schedule it.
  1406. */
  1407. schedule_work(&recovery_work);
  1408. }
  1409. void ccw_device_schedule_recovery(void)
  1410. {
  1411. unsigned long flags;
  1412. CIO_MSG_EVENT(4, "recovery: schedule\n");
  1413. spin_lock_irqsave(&recovery_lock, flags);
  1414. if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
  1415. recovery_phase = 0;
  1416. mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
  1417. }
  1418. spin_unlock_irqrestore(&recovery_lock, flags);
  1419. }
  1420. MODULE_LICENSE("GPL");
  1421. EXPORT_SYMBOL(ccw_device_set_online);
  1422. EXPORT_SYMBOL(ccw_device_set_offline);
  1423. EXPORT_SYMBOL(ccw_driver_register);
  1424. EXPORT_SYMBOL(ccw_driver_unregister);
  1425. EXPORT_SYMBOL(get_ccwdev_by_busid);
  1426. EXPORT_SYMBOL(ccw_bus_type);
  1427. EXPORT_SYMBOL(ccw_device_work);
  1428. EXPORT_SYMBOL(ccw_device_notify_work);
  1429. EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);