device.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635
  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 *io_subchannel_attrs[] = {
  518. &dev_attr_chpids.attr,
  519. &dev_attr_pimpampom.attr,
  520. NULL,
  521. };
  522. static struct attribute_group io_subchannel_attr_group = {
  523. .attrs = io_subchannel_attrs,
  524. };
  525. static struct attribute * ccwdev_attrs[] = {
  526. &dev_attr_devtype.attr,
  527. &dev_attr_cutype.attr,
  528. &dev_attr_modalias.attr,
  529. &dev_attr_online.attr,
  530. &dev_attr_cmb_enable.attr,
  531. &dev_attr_availability.attr,
  532. NULL,
  533. };
  534. static struct attribute_group ccwdev_attr_group = {
  535. .attrs = ccwdev_attrs,
  536. };
  537. static struct attribute_group *ccwdev_attr_groups[] = {
  538. &ccwdev_attr_group,
  539. NULL,
  540. };
  541. /* this is a simple abstraction for device_register that sets the
  542. * correct bus type and adds the bus specific files */
  543. static int ccw_device_register(struct ccw_device *cdev)
  544. {
  545. struct device *dev = &cdev->dev;
  546. int ret;
  547. dev->bus = &ccw_bus_type;
  548. if ((ret = device_add(dev)))
  549. return ret;
  550. set_bit(1, &cdev->private->registered);
  551. return ret;
  552. }
  553. struct match_data {
  554. struct ccw_dev_id dev_id;
  555. struct ccw_device * sibling;
  556. };
  557. static int
  558. match_devno(struct device * dev, void * data)
  559. {
  560. struct match_data * d = data;
  561. struct ccw_device * cdev;
  562. cdev = to_ccwdev(dev);
  563. if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
  564. !ccw_device_is_orphan(cdev) &&
  565. ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
  566. (cdev != d->sibling))
  567. return 1;
  568. return 0;
  569. }
  570. static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
  571. struct ccw_device *sibling)
  572. {
  573. struct device *dev;
  574. struct match_data data;
  575. data.dev_id = *dev_id;
  576. data.sibling = sibling;
  577. dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
  578. return dev ? to_ccwdev(dev) : NULL;
  579. }
  580. static int match_orphan(struct device *dev, void *data)
  581. {
  582. struct ccw_dev_id *dev_id;
  583. struct ccw_device *cdev;
  584. dev_id = data;
  585. cdev = to_ccwdev(dev);
  586. return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
  587. }
  588. static struct ccw_device *
  589. get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css,
  590. struct ccw_dev_id *dev_id)
  591. {
  592. struct device *dev;
  593. dev = device_find_child(&css->pseudo_subchannel->dev, dev_id,
  594. match_orphan);
  595. return dev ? to_ccwdev(dev) : NULL;
  596. }
  597. static void
  598. ccw_device_add_changed(struct work_struct *work)
  599. {
  600. struct ccw_device_private *priv;
  601. struct ccw_device *cdev;
  602. priv = container_of(work, struct ccw_device_private, kick_work);
  603. cdev = priv->cdev;
  604. if (device_add(&cdev->dev)) {
  605. put_device(&cdev->dev);
  606. return;
  607. }
  608. set_bit(1, &cdev->private->registered);
  609. }
  610. void ccw_device_do_unreg_rereg(struct work_struct *work)
  611. {
  612. struct ccw_device_private *priv;
  613. struct ccw_device *cdev;
  614. struct subchannel *sch;
  615. priv = container_of(work, struct ccw_device_private, kick_work);
  616. cdev = priv->cdev;
  617. sch = to_subchannel(cdev->dev.parent);
  618. ccw_device_unregister(cdev);
  619. PREPARE_WORK(&cdev->private->kick_work,
  620. ccw_device_add_changed);
  621. queue_work(ccw_device_work, &cdev->private->kick_work);
  622. }
  623. static void
  624. ccw_device_release(struct device *dev)
  625. {
  626. struct ccw_device *cdev;
  627. cdev = to_ccwdev(dev);
  628. kfree(cdev->private);
  629. kfree(cdev);
  630. }
  631. static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
  632. {
  633. struct ccw_device *cdev;
  634. cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
  635. if (cdev) {
  636. cdev->private = kzalloc(sizeof(struct ccw_device_private),
  637. GFP_KERNEL | GFP_DMA);
  638. if (cdev->private)
  639. return cdev;
  640. }
  641. kfree(cdev);
  642. return ERR_PTR(-ENOMEM);
  643. }
  644. static int io_subchannel_initialize_dev(struct subchannel *sch,
  645. struct ccw_device *cdev)
  646. {
  647. cdev->private->cdev = cdev;
  648. atomic_set(&cdev->private->onoff, 0);
  649. cdev->dev.parent = &sch->dev;
  650. cdev->dev.release = ccw_device_release;
  651. INIT_WORK(&cdev->private->kick_work, NULL);
  652. cdev->dev.groups = ccwdev_attr_groups;
  653. /* Do first half of device_register. */
  654. device_initialize(&cdev->dev);
  655. if (!get_device(&sch->dev)) {
  656. if (cdev->dev.release)
  657. cdev->dev.release(&cdev->dev);
  658. return -ENODEV;
  659. }
  660. return 0;
  661. }
  662. static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
  663. {
  664. struct ccw_device *cdev;
  665. int ret;
  666. cdev = io_subchannel_allocate_dev(sch);
  667. if (!IS_ERR(cdev)) {
  668. ret = io_subchannel_initialize_dev(sch, cdev);
  669. if (ret) {
  670. kfree(cdev);
  671. cdev = ERR_PTR(ret);
  672. }
  673. }
  674. return cdev;
  675. }
  676. static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
  677. static void sch_attach_device(struct subchannel *sch,
  678. struct ccw_device *cdev)
  679. {
  680. css_update_ssd_info(sch);
  681. spin_lock_irq(sch->lock);
  682. sch_set_cdev(sch, cdev);
  683. cdev->private->schid = sch->schid;
  684. cdev->ccwlock = sch->lock;
  685. device_trigger_reprobe(sch);
  686. spin_unlock_irq(sch->lock);
  687. }
  688. static void sch_attach_disconnected_device(struct subchannel *sch,
  689. struct ccw_device *cdev)
  690. {
  691. struct subchannel *other_sch;
  692. int ret;
  693. other_sch = to_subchannel(get_device(cdev->dev.parent));
  694. ret = device_move(&cdev->dev, &sch->dev);
  695. if (ret) {
  696. CIO_MSG_EVENT(0, "Moving disconnected device 0.%x.%04x failed "
  697. "(ret=%d)!\n", cdev->private->dev_id.ssid,
  698. cdev->private->dev_id.devno, ret);
  699. put_device(&other_sch->dev);
  700. return;
  701. }
  702. sch_set_cdev(other_sch, NULL);
  703. /* No need to keep a subchannel without ccw device around. */
  704. css_sch_device_unregister(other_sch);
  705. put_device(&other_sch->dev);
  706. sch_attach_device(sch, cdev);
  707. }
  708. static void sch_attach_orphaned_device(struct subchannel *sch,
  709. struct ccw_device *cdev)
  710. {
  711. int ret;
  712. /* Try to move the ccw device to its new subchannel. */
  713. ret = device_move(&cdev->dev, &sch->dev);
  714. if (ret) {
  715. CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
  716. "failed (ret=%d)!\n",
  717. cdev->private->dev_id.ssid,
  718. cdev->private->dev_id.devno, ret);
  719. return;
  720. }
  721. sch_attach_device(sch, cdev);
  722. }
  723. static void sch_create_and_recog_new_device(struct subchannel *sch)
  724. {
  725. struct ccw_device *cdev;
  726. /* Need to allocate a new ccw device. */
  727. cdev = io_subchannel_create_ccwdev(sch);
  728. if (IS_ERR(cdev)) {
  729. /* OK, we did everything we could... */
  730. css_sch_device_unregister(sch);
  731. return;
  732. }
  733. spin_lock_irq(sch->lock);
  734. sch_set_cdev(sch, cdev);
  735. spin_unlock_irq(sch->lock);
  736. /* Start recognition for the new ccw device. */
  737. if (io_subchannel_recog(cdev, sch)) {
  738. spin_lock_irq(sch->lock);
  739. sch_set_cdev(sch, NULL);
  740. spin_unlock_irq(sch->lock);
  741. if (cdev->dev.release)
  742. cdev->dev.release(&cdev->dev);
  743. css_sch_device_unregister(sch);
  744. }
  745. }
  746. void ccw_device_move_to_orphanage(struct work_struct *work)
  747. {
  748. struct ccw_device_private *priv;
  749. struct ccw_device *cdev;
  750. struct ccw_device *replacing_cdev;
  751. struct subchannel *sch;
  752. int ret;
  753. struct channel_subsystem *css;
  754. struct ccw_dev_id dev_id;
  755. priv = container_of(work, struct ccw_device_private, kick_work);
  756. cdev = priv->cdev;
  757. sch = to_subchannel(cdev->dev.parent);
  758. css = to_css(sch->dev.parent);
  759. dev_id.devno = sch->schib.pmcw.dev;
  760. dev_id.ssid = sch->schid.ssid;
  761. /*
  762. * Move the orphaned ccw device to the orphanage so the replacing
  763. * ccw device can take its place on the subchannel.
  764. */
  765. ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev);
  766. if (ret) {
  767. CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
  768. "(ret=%d)!\n", cdev->private->dev_id.ssid,
  769. cdev->private->dev_id.devno, ret);
  770. return;
  771. }
  772. cdev->ccwlock = css->pseudo_subchannel->lock;
  773. /*
  774. * Search for the replacing ccw device
  775. * - among the disconnected devices
  776. * - in the orphanage
  777. */
  778. replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
  779. if (replacing_cdev) {
  780. sch_attach_disconnected_device(sch, replacing_cdev);
  781. return;
  782. }
  783. replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id);
  784. if (replacing_cdev) {
  785. sch_attach_orphaned_device(sch, replacing_cdev);
  786. return;
  787. }
  788. sch_create_and_recog_new_device(sch);
  789. }
  790. /*
  791. * Register recognized device.
  792. */
  793. static void
  794. io_subchannel_register(struct work_struct *work)
  795. {
  796. struct ccw_device_private *priv;
  797. struct ccw_device *cdev;
  798. struct subchannel *sch;
  799. int ret;
  800. unsigned long flags;
  801. priv = container_of(work, struct ccw_device_private, kick_work);
  802. cdev = priv->cdev;
  803. sch = to_subchannel(cdev->dev.parent);
  804. css_update_ssd_info(sch);
  805. /*
  806. * io_subchannel_register() will also be called after device
  807. * recognition has been done for a boxed device (which will already
  808. * be registered). We need to reprobe since we may now have sense id
  809. * information.
  810. */
  811. if (klist_node_attached(&cdev->dev.knode_parent)) {
  812. if (!cdev->drv) {
  813. ret = device_reprobe(&cdev->dev);
  814. if (ret)
  815. /* We can't do much here. */
  816. CIO_MSG_EVENT(0, "device_reprobe() returned"
  817. " %d for 0.%x.%04x\n", ret,
  818. cdev->private->dev_id.ssid,
  819. cdev->private->dev_id.devno);
  820. }
  821. goto out;
  822. }
  823. /*
  824. * Now we know this subchannel will stay, we can throw
  825. * our delayed uevent.
  826. */
  827. sch->dev.uevent_suppress = 0;
  828. kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
  829. /* make it known to the system */
  830. ret = ccw_device_register(cdev);
  831. if (ret) {
  832. CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
  833. cdev->private->dev_id.ssid,
  834. cdev->private->dev_id.devno, ret);
  835. put_device(&cdev->dev);
  836. spin_lock_irqsave(sch->lock, flags);
  837. sch_set_cdev(sch, NULL);
  838. spin_unlock_irqrestore(sch->lock, flags);
  839. kfree (cdev->private);
  840. kfree (cdev);
  841. put_device(&sch->dev);
  842. if (atomic_dec_and_test(&ccw_device_init_count))
  843. wake_up(&ccw_device_init_wq);
  844. return;
  845. }
  846. put_device(&cdev->dev);
  847. out:
  848. cdev->private->flags.recog_done = 1;
  849. put_device(&sch->dev);
  850. wake_up(&cdev->private->wait_q);
  851. if (atomic_dec_and_test(&ccw_device_init_count))
  852. wake_up(&ccw_device_init_wq);
  853. }
  854. static void ccw_device_call_sch_unregister(struct work_struct *work)
  855. {
  856. struct ccw_device_private *priv;
  857. struct ccw_device *cdev;
  858. struct subchannel *sch;
  859. priv = container_of(work, struct ccw_device_private, kick_work);
  860. cdev = priv->cdev;
  861. sch = to_subchannel(cdev->dev.parent);
  862. css_sch_device_unregister(sch);
  863. /* Reset intparm to zeroes. */
  864. sch->schib.pmcw.intparm = 0;
  865. cio_modify(sch);
  866. put_device(&cdev->dev);
  867. put_device(&sch->dev);
  868. }
  869. /*
  870. * subchannel recognition done. Called from the state machine.
  871. */
  872. void
  873. io_subchannel_recog_done(struct ccw_device *cdev)
  874. {
  875. struct subchannel *sch;
  876. if (css_init_done == 0) {
  877. cdev->private->flags.recog_done = 1;
  878. return;
  879. }
  880. switch (cdev->private->state) {
  881. case DEV_STATE_NOT_OPER:
  882. cdev->private->flags.recog_done = 1;
  883. /* Remove device found not operational. */
  884. if (!get_device(&cdev->dev))
  885. break;
  886. sch = to_subchannel(cdev->dev.parent);
  887. PREPARE_WORK(&cdev->private->kick_work,
  888. ccw_device_call_sch_unregister);
  889. queue_work(slow_path_wq, &cdev->private->kick_work);
  890. if (atomic_dec_and_test(&ccw_device_init_count))
  891. wake_up(&ccw_device_init_wq);
  892. break;
  893. case DEV_STATE_BOXED:
  894. /* Device did not respond in time. */
  895. case DEV_STATE_OFFLINE:
  896. /*
  897. * We can't register the device in interrupt context so
  898. * we schedule a work item.
  899. */
  900. if (!get_device(&cdev->dev))
  901. break;
  902. PREPARE_WORK(&cdev->private->kick_work,
  903. io_subchannel_register);
  904. queue_work(slow_path_wq, &cdev->private->kick_work);
  905. break;
  906. }
  907. }
  908. static int
  909. io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
  910. {
  911. int rc;
  912. struct ccw_device_private *priv;
  913. sch_set_cdev(sch, cdev);
  914. cdev->ccwlock = sch->lock;
  915. /* Init private data. */
  916. priv = cdev->private;
  917. priv->dev_id.devno = sch->schib.pmcw.dev;
  918. priv->dev_id.ssid = sch->schid.ssid;
  919. priv->schid = sch->schid;
  920. priv->state = DEV_STATE_NOT_OPER;
  921. INIT_LIST_HEAD(&priv->cmb_list);
  922. init_waitqueue_head(&priv->wait_q);
  923. init_timer(&priv->timer);
  924. /* Set an initial name for the device. */
  925. snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x",
  926. sch->schid.ssid, sch->schib.pmcw.dev);
  927. /* Increase counter of devices currently in recognition. */
  928. atomic_inc(&ccw_device_init_count);
  929. /* Start async. device sensing. */
  930. spin_lock_irq(sch->lock);
  931. rc = ccw_device_recognition(cdev);
  932. spin_unlock_irq(sch->lock);
  933. if (rc) {
  934. if (atomic_dec_and_test(&ccw_device_init_count))
  935. wake_up(&ccw_device_init_wq);
  936. }
  937. return rc;
  938. }
  939. static void ccw_device_move_to_sch(struct work_struct *work)
  940. {
  941. struct ccw_device_private *priv;
  942. int rc;
  943. struct subchannel *sch;
  944. struct ccw_device *cdev;
  945. struct subchannel *former_parent;
  946. priv = container_of(work, struct ccw_device_private, kick_work);
  947. sch = priv->sch;
  948. cdev = priv->cdev;
  949. former_parent = ccw_device_is_orphan(cdev) ?
  950. NULL : to_subchannel(get_device(cdev->dev.parent));
  951. mutex_lock(&sch->reg_mutex);
  952. /* Try to move the ccw device to its new subchannel. */
  953. rc = device_move(&cdev->dev, &sch->dev);
  954. mutex_unlock(&sch->reg_mutex);
  955. if (rc) {
  956. CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to subchannel "
  957. "0.%x.%04x failed (ret=%d)!\n",
  958. cdev->private->dev_id.ssid,
  959. cdev->private->dev_id.devno, sch->schid.ssid,
  960. sch->schid.sch_no, rc);
  961. css_sch_device_unregister(sch);
  962. goto out;
  963. }
  964. if (former_parent) {
  965. spin_lock_irq(former_parent->lock);
  966. sch_set_cdev(former_parent, NULL);
  967. spin_unlock_irq(former_parent->lock);
  968. css_sch_device_unregister(former_parent);
  969. /* Reset intparm to zeroes. */
  970. former_parent->schib.pmcw.intparm = 0;
  971. cio_modify(former_parent);
  972. }
  973. sch_attach_device(sch, cdev);
  974. out:
  975. if (former_parent)
  976. put_device(&former_parent->dev);
  977. put_device(&cdev->dev);
  978. }
  979. static void io_subchannel_irq(struct subchannel *sch)
  980. {
  981. struct ccw_device *cdev;
  982. cdev = sch_get_cdev(sch);
  983. CIO_TRACE_EVENT(3, "IRQ");
  984. CIO_TRACE_EVENT(3, sch->dev.bus_id);
  985. if (cdev)
  986. dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
  987. }
  988. static void io_subchannel_init_fields(struct subchannel *sch)
  989. {
  990. if (cio_is_console(sch->schid))
  991. sch->opm = 0xff;
  992. else
  993. sch->opm = chp_get_sch_opm(sch);
  994. sch->lpm = sch->schib.pmcw.pam & sch->opm;
  995. sch->isc = cio_is_console(sch->schid) ? 1 : 3;
  996. CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
  997. " - PIM = %02X, PAM = %02X, POM = %02X\n",
  998. sch->schib.pmcw.dev, sch->schid.ssid,
  999. sch->schid.sch_no, sch->schib.pmcw.pim,
  1000. sch->schib.pmcw.pam, sch->schib.pmcw.pom);
  1001. /* Initially set up some fields in the pmcw. */
  1002. sch->schib.pmcw.ena = 0;
  1003. sch->schib.pmcw.csense = 1; /* concurrent sense */
  1004. if ((sch->lpm & (sch->lpm - 1)) != 0)
  1005. sch->schib.pmcw.mp = 1; /* multipath mode */
  1006. /* clean up possible residual cmf stuff */
  1007. sch->schib.pmcw.mme = 0;
  1008. sch->schib.pmcw.mbfc = 0;
  1009. sch->schib.pmcw.mbi = 0;
  1010. sch->schib.mba = 0;
  1011. }
  1012. static int io_subchannel_probe(struct subchannel *sch)
  1013. {
  1014. struct ccw_device *cdev;
  1015. int rc;
  1016. unsigned long flags;
  1017. struct ccw_dev_id dev_id;
  1018. cdev = sch_get_cdev(sch);
  1019. if (cdev) {
  1020. rc = sysfs_create_group(&sch->dev.kobj,
  1021. &io_subchannel_attr_group);
  1022. if (rc)
  1023. CIO_MSG_EVENT(0, "Failed to create io subchannel "
  1024. "attributes for subchannel "
  1025. "0.%x.%04x (rc=%d)\n",
  1026. sch->schid.ssid, sch->schid.sch_no, rc);
  1027. /*
  1028. * This subchannel already has an associated ccw_device.
  1029. * Throw the delayed uevent for the subchannel, register
  1030. * the ccw_device and exit. This happens for all early
  1031. * devices, e.g. the console.
  1032. */
  1033. sch->dev.uevent_suppress = 0;
  1034. kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
  1035. cdev->dev.groups = ccwdev_attr_groups;
  1036. device_initialize(&cdev->dev);
  1037. ccw_device_register(cdev);
  1038. /*
  1039. * Check if the device is already online. If it is
  1040. * the reference count needs to be corrected
  1041. * (see ccw_device_online and css_init_done for the
  1042. * ugly details).
  1043. */
  1044. if (cdev->private->state != DEV_STATE_NOT_OPER &&
  1045. cdev->private->state != DEV_STATE_OFFLINE &&
  1046. cdev->private->state != DEV_STATE_BOXED)
  1047. get_device(&cdev->dev);
  1048. return 0;
  1049. }
  1050. io_subchannel_init_fields(sch);
  1051. /*
  1052. * First check if a fitting device may be found amongst the
  1053. * disconnected devices or in the orphanage.
  1054. */
  1055. dev_id.devno = sch->schib.pmcw.dev;
  1056. dev_id.ssid = sch->schid.ssid;
  1057. rc = sysfs_create_group(&sch->dev.kobj,
  1058. &io_subchannel_attr_group);
  1059. if (rc)
  1060. return rc;
  1061. /* Allocate I/O subchannel private data. */
  1062. sch->private = kzalloc(sizeof(struct io_subchannel_private),
  1063. GFP_KERNEL | GFP_DMA);
  1064. if (!sch->private) {
  1065. rc = -ENOMEM;
  1066. goto out_err;
  1067. }
  1068. cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL);
  1069. if (!cdev)
  1070. cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent),
  1071. &dev_id);
  1072. if (cdev) {
  1073. /*
  1074. * Schedule moving the device until when we have a registered
  1075. * subchannel to move to and succeed the probe. We can
  1076. * unregister later again, when the probe is through.
  1077. */
  1078. cdev->private->sch = sch;
  1079. PREPARE_WORK(&cdev->private->kick_work,
  1080. ccw_device_move_to_sch);
  1081. queue_work(slow_path_wq, &cdev->private->kick_work);
  1082. return 0;
  1083. }
  1084. cdev = io_subchannel_create_ccwdev(sch);
  1085. if (IS_ERR(cdev)) {
  1086. rc = PTR_ERR(cdev);
  1087. goto out_err;
  1088. }
  1089. rc = io_subchannel_recog(cdev, sch);
  1090. if (rc) {
  1091. spin_lock_irqsave(sch->lock, flags);
  1092. sch_set_cdev(sch, NULL);
  1093. spin_unlock_irqrestore(sch->lock, flags);
  1094. if (cdev->dev.release)
  1095. cdev->dev.release(&cdev->dev);
  1096. goto out_err;
  1097. }
  1098. return 0;
  1099. out_err:
  1100. kfree(sch->private);
  1101. sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
  1102. return rc;
  1103. }
  1104. static int
  1105. io_subchannel_remove (struct subchannel *sch)
  1106. {
  1107. struct ccw_device *cdev;
  1108. unsigned long flags;
  1109. cdev = sch_get_cdev(sch);
  1110. if (!cdev)
  1111. return 0;
  1112. /* Set ccw device to not operational and drop reference. */
  1113. spin_lock_irqsave(cdev->ccwlock, flags);
  1114. sch_set_cdev(sch, NULL);
  1115. cdev->private->state = DEV_STATE_NOT_OPER;
  1116. spin_unlock_irqrestore(cdev->ccwlock, flags);
  1117. ccw_device_unregister(cdev);
  1118. put_device(&cdev->dev);
  1119. kfree(sch->private);
  1120. sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
  1121. return 0;
  1122. }
  1123. static int io_subchannel_notify(struct subchannel *sch, int event)
  1124. {
  1125. struct ccw_device *cdev;
  1126. cdev = sch_get_cdev(sch);
  1127. if (!cdev)
  1128. return 0;
  1129. if (!cdev->drv)
  1130. return 0;
  1131. if (!cdev->online)
  1132. return 0;
  1133. return cdev->drv->notify ? cdev->drv->notify(cdev, event) : 0;
  1134. }
  1135. static void io_subchannel_verify(struct subchannel *sch)
  1136. {
  1137. struct ccw_device *cdev;
  1138. cdev = sch_get_cdev(sch);
  1139. if (cdev)
  1140. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1141. }
  1142. static void io_subchannel_ioterm(struct subchannel *sch)
  1143. {
  1144. struct ccw_device *cdev;
  1145. cdev = sch_get_cdev(sch);
  1146. if (!cdev)
  1147. return;
  1148. /* Internal I/O will be retried by the interrupt handler. */
  1149. if (cdev->private->flags.intretry)
  1150. return;
  1151. cdev->private->state = DEV_STATE_CLEAR_VERIFY;
  1152. if (cdev->handler)
  1153. cdev->handler(cdev, cdev->private->intparm,
  1154. ERR_PTR(-EIO));
  1155. }
  1156. static void
  1157. io_subchannel_shutdown(struct subchannel *sch)
  1158. {
  1159. struct ccw_device *cdev;
  1160. int ret;
  1161. cdev = sch_get_cdev(sch);
  1162. if (cio_is_console(sch->schid))
  1163. return;
  1164. if (!sch->schib.pmcw.ena)
  1165. /* Nothing to do. */
  1166. return;
  1167. ret = cio_disable_subchannel(sch);
  1168. if (ret != -EBUSY)
  1169. /* Subchannel is disabled, we're done. */
  1170. return;
  1171. cdev->private->state = DEV_STATE_QUIESCE;
  1172. if (cdev->handler)
  1173. cdev->handler(cdev, cdev->private->intparm,
  1174. ERR_PTR(-EIO));
  1175. ret = ccw_device_cancel_halt_clear(cdev);
  1176. if (ret == -EBUSY) {
  1177. ccw_device_set_timeout(cdev, HZ/10);
  1178. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  1179. }
  1180. cio_disable_subchannel(sch);
  1181. }
  1182. #ifdef CONFIG_CCW_CONSOLE
  1183. static struct ccw_device console_cdev;
  1184. static struct ccw_device_private console_private;
  1185. static int console_cdev_in_use;
  1186. static DEFINE_SPINLOCK(ccw_console_lock);
  1187. spinlock_t * cio_get_console_lock(void)
  1188. {
  1189. return &ccw_console_lock;
  1190. }
  1191. static int ccw_device_console_enable(struct ccw_device *cdev,
  1192. struct subchannel *sch)
  1193. {
  1194. int rc;
  1195. /* Attach subchannel private data. */
  1196. sch->private = cio_get_console_priv();
  1197. memset(sch->private, 0, sizeof(struct io_subchannel_private));
  1198. io_subchannel_init_fields(sch);
  1199. sch->driver = &io_subchannel_driver;
  1200. /* Initialize the ccw_device structure. */
  1201. cdev->dev.parent= &sch->dev;
  1202. rc = io_subchannel_recog(cdev, sch);
  1203. if (rc)
  1204. return rc;
  1205. /* Now wait for the async. recognition to come to an end. */
  1206. spin_lock_irq(cdev->ccwlock);
  1207. while (!dev_fsm_final_state(cdev))
  1208. wait_cons_dev();
  1209. rc = -EIO;
  1210. if (cdev->private->state != DEV_STATE_OFFLINE)
  1211. goto out_unlock;
  1212. ccw_device_online(cdev);
  1213. while (!dev_fsm_final_state(cdev))
  1214. wait_cons_dev();
  1215. if (cdev->private->state != DEV_STATE_ONLINE)
  1216. goto out_unlock;
  1217. rc = 0;
  1218. out_unlock:
  1219. spin_unlock_irq(cdev->ccwlock);
  1220. return 0;
  1221. }
  1222. struct ccw_device *
  1223. ccw_device_probe_console(void)
  1224. {
  1225. struct subchannel *sch;
  1226. int ret;
  1227. if (xchg(&console_cdev_in_use, 1) != 0)
  1228. return ERR_PTR(-EBUSY);
  1229. sch = cio_probe_console();
  1230. if (IS_ERR(sch)) {
  1231. console_cdev_in_use = 0;
  1232. return (void *) sch;
  1233. }
  1234. memset(&console_cdev, 0, sizeof(struct ccw_device));
  1235. memset(&console_private, 0, sizeof(struct ccw_device_private));
  1236. console_cdev.private = &console_private;
  1237. console_private.cdev = &console_cdev;
  1238. ret = ccw_device_console_enable(&console_cdev, sch);
  1239. if (ret) {
  1240. cio_release_console();
  1241. console_cdev_in_use = 0;
  1242. return ERR_PTR(ret);
  1243. }
  1244. console_cdev.online = 1;
  1245. return &console_cdev;
  1246. }
  1247. #endif
  1248. /*
  1249. * get ccw_device matching the busid, but only if owned by cdrv
  1250. */
  1251. static int
  1252. __ccwdev_check_busid(struct device *dev, void *id)
  1253. {
  1254. char *bus_id;
  1255. bus_id = id;
  1256. return (strncmp(bus_id, dev->bus_id, BUS_ID_SIZE) == 0);
  1257. }
  1258. /**
  1259. * get_ccwdev_by_busid() - obtain device from a bus id
  1260. * @cdrv: driver the device is owned by
  1261. * @bus_id: bus id of the device to be searched
  1262. *
  1263. * This function searches all devices owned by @cdrv for a device with a bus
  1264. * id matching @bus_id.
  1265. * Returns:
  1266. * If a match is found, its reference count of the found device is increased
  1267. * and it is returned; else %NULL is returned.
  1268. */
  1269. struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
  1270. const char *bus_id)
  1271. {
  1272. struct device *dev;
  1273. struct device_driver *drv;
  1274. drv = get_driver(&cdrv->driver);
  1275. if (!drv)
  1276. return NULL;
  1277. dev = driver_find_device(drv, NULL, (void *)bus_id,
  1278. __ccwdev_check_busid);
  1279. put_driver(drv);
  1280. return dev ? to_ccwdev(dev) : NULL;
  1281. }
  1282. /************************** device driver handling ************************/
  1283. /* This is the implementation of the ccw_driver class. The probe, remove
  1284. * and release methods are initially very similar to the device_driver
  1285. * implementations, with the difference that they have ccw_device
  1286. * arguments.
  1287. *
  1288. * A ccw driver also contains the information that is needed for
  1289. * device matching.
  1290. */
  1291. static int
  1292. ccw_device_probe (struct device *dev)
  1293. {
  1294. struct ccw_device *cdev = to_ccwdev(dev);
  1295. struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
  1296. int ret;
  1297. cdev->drv = cdrv; /* to let the driver call _set_online */
  1298. ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
  1299. if (ret) {
  1300. cdev->drv = NULL;
  1301. return ret;
  1302. }
  1303. return 0;
  1304. }
  1305. static int
  1306. ccw_device_remove (struct device *dev)
  1307. {
  1308. struct ccw_device *cdev = to_ccwdev(dev);
  1309. struct ccw_driver *cdrv = cdev->drv;
  1310. int ret;
  1311. if (cdrv->remove)
  1312. cdrv->remove(cdev);
  1313. if (cdev->online) {
  1314. cdev->online = 0;
  1315. spin_lock_irq(cdev->ccwlock);
  1316. ret = ccw_device_offline(cdev);
  1317. spin_unlock_irq(cdev->ccwlock);
  1318. if (ret == 0)
  1319. wait_event(cdev->private->wait_q,
  1320. dev_fsm_final_state(cdev));
  1321. else
  1322. CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
  1323. "device 0.%x.%04x\n",
  1324. ret, cdev->private->dev_id.ssid,
  1325. cdev->private->dev_id.devno);
  1326. }
  1327. ccw_device_set_timeout(cdev, 0);
  1328. cdev->drv = NULL;
  1329. return 0;
  1330. }
  1331. static void ccw_device_shutdown(struct device *dev)
  1332. {
  1333. struct ccw_device *cdev;
  1334. cdev = to_ccwdev(dev);
  1335. if (cdev->drv && cdev->drv->shutdown)
  1336. cdev->drv->shutdown(cdev);
  1337. disable_cmf(cdev);
  1338. }
  1339. struct bus_type ccw_bus_type = {
  1340. .name = "ccw",
  1341. .match = ccw_bus_match,
  1342. .uevent = ccw_uevent,
  1343. .probe = ccw_device_probe,
  1344. .remove = ccw_device_remove,
  1345. .shutdown = ccw_device_shutdown,
  1346. };
  1347. /**
  1348. * ccw_driver_register() - register a ccw driver
  1349. * @cdriver: driver to be registered
  1350. *
  1351. * This function is mainly a wrapper around driver_register().
  1352. * Returns:
  1353. * %0 on success and a negative error value on failure.
  1354. */
  1355. int ccw_driver_register(struct ccw_driver *cdriver)
  1356. {
  1357. struct device_driver *drv = &cdriver->driver;
  1358. drv->bus = &ccw_bus_type;
  1359. drv->name = cdriver->name;
  1360. drv->owner = cdriver->owner;
  1361. return driver_register(drv);
  1362. }
  1363. /**
  1364. * ccw_driver_unregister() - deregister a ccw driver
  1365. * @cdriver: driver to be deregistered
  1366. *
  1367. * This function is mainly a wrapper around driver_unregister().
  1368. */
  1369. void ccw_driver_unregister(struct ccw_driver *cdriver)
  1370. {
  1371. driver_unregister(&cdriver->driver);
  1372. }
  1373. /* Helper func for qdio. */
  1374. struct subchannel_id
  1375. ccw_device_get_subchannel_id(struct ccw_device *cdev)
  1376. {
  1377. struct subchannel *sch;
  1378. sch = to_subchannel(cdev->dev.parent);
  1379. return sch->schid;
  1380. }
  1381. static int recovery_check(struct device *dev, void *data)
  1382. {
  1383. struct ccw_device *cdev = to_ccwdev(dev);
  1384. int *redo = data;
  1385. spin_lock_irq(cdev->ccwlock);
  1386. switch (cdev->private->state) {
  1387. case DEV_STATE_DISCONNECTED:
  1388. CIO_MSG_EVENT(4, "recovery: trigger 0.%x.%04x\n",
  1389. cdev->private->dev_id.ssid,
  1390. cdev->private->dev_id.devno);
  1391. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1392. *redo = 1;
  1393. break;
  1394. case DEV_STATE_DISCONNECTED_SENSE_ID:
  1395. *redo = 1;
  1396. break;
  1397. }
  1398. spin_unlock_irq(cdev->ccwlock);
  1399. return 0;
  1400. }
  1401. static void recovery_work_func(struct work_struct *unused)
  1402. {
  1403. int redo = 0;
  1404. bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
  1405. if (redo) {
  1406. spin_lock_irq(&recovery_lock);
  1407. if (!timer_pending(&recovery_timer)) {
  1408. if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
  1409. recovery_phase++;
  1410. mod_timer(&recovery_timer, jiffies +
  1411. recovery_delay[recovery_phase] * HZ);
  1412. }
  1413. spin_unlock_irq(&recovery_lock);
  1414. } else
  1415. CIO_MSG_EVENT(4, "recovery: end\n");
  1416. }
  1417. static DECLARE_WORK(recovery_work, recovery_work_func);
  1418. static void recovery_func(unsigned long data)
  1419. {
  1420. /*
  1421. * We can't do our recovery in softirq context and it's not
  1422. * performance critical, so we schedule it.
  1423. */
  1424. schedule_work(&recovery_work);
  1425. }
  1426. void ccw_device_schedule_recovery(void)
  1427. {
  1428. unsigned long flags;
  1429. CIO_MSG_EVENT(4, "recovery: schedule\n");
  1430. spin_lock_irqsave(&recovery_lock, flags);
  1431. if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
  1432. recovery_phase = 0;
  1433. mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
  1434. }
  1435. spin_unlock_irqrestore(&recovery_lock, flags);
  1436. }
  1437. MODULE_LICENSE("GPL");
  1438. EXPORT_SYMBOL(ccw_device_set_online);
  1439. EXPORT_SYMBOL(ccw_device_set_offline);
  1440. EXPORT_SYMBOL(ccw_driver_register);
  1441. EXPORT_SYMBOL(ccw_driver_unregister);
  1442. EXPORT_SYMBOL(get_ccwdev_by_busid);
  1443. EXPORT_SYMBOL(ccw_bus_type);
  1444. EXPORT_SYMBOL(ccw_device_work);
  1445. EXPORT_SYMBOL(ccw_device_notify_work);
  1446. EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);