device.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235
  1. /*
  2. * drivers/s390/cio/device.c
  3. * bus driver for ccw devices
  4. *
  5. * Copyright IBM Corp. 2002,2008
  6. * Author(s): Arnd Bergmann (arndb@de.ibm.com)
  7. * Cornelia Huck (cornelia.huck@de.ibm.com)
  8. * Martin Schwidefsky (schwidefsky@de.ibm.com)
  9. */
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/errno.h>
  14. #include <linux/err.h>
  15. #include <linux/slab.h>
  16. #include <linux/list.h>
  17. #include <linux/device.h>
  18. #include <linux/workqueue.h>
  19. #include <linux/timer.h>
  20. #include <asm/ccwdev.h>
  21. #include <asm/cio.h>
  22. #include <asm/param.h> /* HZ */
  23. #include <asm/cmb.h>
  24. #include <asm/isc.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. #include "blacklist.h"
  33. static struct timer_list recovery_timer;
  34. static DEFINE_SPINLOCK(recovery_lock);
  35. static int recovery_phase;
  36. static const unsigned long recovery_delay[] = { 3, 30, 300 };
  37. /******************* bus type handling ***********************/
  38. /* The Linux driver model distinguishes between a bus type and
  39. * the bus itself. Of course we only have one channel
  40. * subsystem driver and one channel system per machine, but
  41. * we still use the abstraction. T.R. says it's a good idea. */
  42. static int
  43. ccw_bus_match (struct device * dev, struct device_driver * drv)
  44. {
  45. struct ccw_device *cdev = to_ccwdev(dev);
  46. struct ccw_driver *cdrv = to_ccwdrv(drv);
  47. const struct ccw_device_id *ids = cdrv->ids, *found;
  48. if (!ids)
  49. return 0;
  50. found = ccw_device_id_match(ids, &cdev->id);
  51. if (!found)
  52. return 0;
  53. cdev->id.driver_info = found->driver_info;
  54. return 1;
  55. }
  56. /* Store modalias string delimited by prefix/suffix string into buffer with
  57. * specified size. Return length of resulting string (excluding trailing '\0')
  58. * even if string doesn't fit buffer (snprintf semantics). */
  59. static int snprint_alias(char *buf, size_t size,
  60. struct ccw_device_id *id, const char *suffix)
  61. {
  62. int len;
  63. len = snprintf(buf, size, "ccw:t%04Xm%02X", id->cu_type, id->cu_model);
  64. if (len > size)
  65. return len;
  66. buf += len;
  67. size -= len;
  68. if (id->dev_type != 0)
  69. len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type,
  70. id->dev_model, suffix);
  71. else
  72. len += snprintf(buf, size, "dtdm%s", suffix);
  73. return len;
  74. }
  75. /* Set up environment variables for ccw device uevent. Return 0 on success,
  76. * non-zero otherwise. */
  77. static int ccw_uevent(struct device *dev, struct kobj_uevent_env *env)
  78. {
  79. struct ccw_device *cdev = to_ccwdev(dev);
  80. struct ccw_device_id *id = &(cdev->id);
  81. int ret;
  82. char modalias_buf[30];
  83. /* CU_TYPE= */
  84. ret = add_uevent_var(env, "CU_TYPE=%04X", id->cu_type);
  85. if (ret)
  86. return ret;
  87. /* CU_MODEL= */
  88. ret = add_uevent_var(env, "CU_MODEL=%02X", id->cu_model);
  89. if (ret)
  90. return ret;
  91. /* The next two can be zero, that's ok for us */
  92. /* DEV_TYPE= */
  93. ret = add_uevent_var(env, "DEV_TYPE=%04X", id->dev_type);
  94. if (ret)
  95. return ret;
  96. /* DEV_MODEL= */
  97. ret = add_uevent_var(env, "DEV_MODEL=%02X", id->dev_model);
  98. if (ret)
  99. return ret;
  100. /* MODALIAS= */
  101. snprint_alias(modalias_buf, sizeof(modalias_buf), id, "");
  102. ret = add_uevent_var(env, "MODALIAS=%s", modalias_buf);
  103. return ret;
  104. }
  105. struct bus_type ccw_bus_type;
  106. static void io_subchannel_irq(struct subchannel *);
  107. static int io_subchannel_probe(struct subchannel *);
  108. static int io_subchannel_remove(struct subchannel *);
  109. static void io_subchannel_shutdown(struct subchannel *);
  110. static int io_subchannel_sch_event(struct subchannel *, int);
  111. static int io_subchannel_chp_event(struct subchannel *, struct chp_link *,
  112. int);
  113. static struct css_device_id io_subchannel_ids[] = {
  114. { .match_flags = 0x1, .type = SUBCHANNEL_TYPE_IO, },
  115. { /* end of list */ },
  116. };
  117. MODULE_DEVICE_TABLE(css, io_subchannel_ids);
  118. static int io_subchannel_prepare(struct subchannel *sch)
  119. {
  120. struct ccw_device *cdev;
  121. /*
  122. * Don't allow suspend while a ccw device registration
  123. * is still outstanding.
  124. */
  125. cdev = sch_get_cdev(sch);
  126. if (cdev && !device_is_registered(&cdev->dev))
  127. return -EAGAIN;
  128. return 0;
  129. }
  130. static struct css_driver io_subchannel_driver = {
  131. .owner = THIS_MODULE,
  132. .subchannel_type = io_subchannel_ids,
  133. .name = "io_subchannel",
  134. .irq = io_subchannel_irq,
  135. .sch_event = io_subchannel_sch_event,
  136. .chp_event = io_subchannel_chp_event,
  137. .probe = io_subchannel_probe,
  138. .remove = io_subchannel_remove,
  139. .shutdown = io_subchannel_shutdown,
  140. .prepare = io_subchannel_prepare,
  141. };
  142. struct workqueue_struct *ccw_device_work;
  143. wait_queue_head_t ccw_device_init_wq;
  144. atomic_t ccw_device_init_count;
  145. static void recovery_func(unsigned long data);
  146. static int __init
  147. init_ccw_bus_type (void)
  148. {
  149. int ret;
  150. init_waitqueue_head(&ccw_device_init_wq);
  151. atomic_set(&ccw_device_init_count, 0);
  152. setup_timer(&recovery_timer, recovery_func, 0);
  153. ccw_device_work = create_singlethread_workqueue("cio");
  154. if (!ccw_device_work)
  155. return -ENOMEM; /* FIXME: better errno ? */
  156. slow_path_wq = create_singlethread_workqueue("kslowcrw");
  157. if (!slow_path_wq) {
  158. ret = -ENOMEM; /* FIXME: better errno ? */
  159. goto out_err;
  160. }
  161. if ((ret = bus_register (&ccw_bus_type)))
  162. goto out_err;
  163. ret = css_driver_register(&io_subchannel_driver);
  164. if (ret)
  165. goto out_err;
  166. wait_event(ccw_device_init_wq,
  167. atomic_read(&ccw_device_init_count) == 0);
  168. flush_workqueue(ccw_device_work);
  169. return 0;
  170. out_err:
  171. if (ccw_device_work)
  172. destroy_workqueue(ccw_device_work);
  173. if (slow_path_wq)
  174. destroy_workqueue(slow_path_wq);
  175. return ret;
  176. }
  177. static void __exit
  178. cleanup_ccw_bus_type (void)
  179. {
  180. css_driver_unregister(&io_subchannel_driver);
  181. bus_unregister(&ccw_bus_type);
  182. destroy_workqueue(ccw_device_work);
  183. }
  184. subsys_initcall(init_ccw_bus_type);
  185. module_exit(cleanup_ccw_bus_type);
  186. /************************ device handling **************************/
  187. /*
  188. * A ccw_device has some interfaces in sysfs in addition to the
  189. * standard ones.
  190. * The following entries are designed to export the information which
  191. * resided in 2.4 in /proc/subchannels. Subchannel and device number
  192. * are obvious, so they don't have an entry :)
  193. * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
  194. */
  195. static ssize_t
  196. chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
  197. {
  198. struct subchannel *sch = to_subchannel(dev);
  199. struct chsc_ssd_info *ssd = &sch->ssd_info;
  200. ssize_t ret = 0;
  201. int chp;
  202. int mask;
  203. for (chp = 0; chp < 8; chp++) {
  204. mask = 0x80 >> chp;
  205. if (ssd->path_mask & mask)
  206. ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
  207. else
  208. ret += sprintf(buf + ret, "00 ");
  209. }
  210. ret += sprintf (buf+ret, "\n");
  211. return min((ssize_t)PAGE_SIZE, ret);
  212. }
  213. static ssize_t
  214. pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
  215. {
  216. struct subchannel *sch = to_subchannel(dev);
  217. struct pmcw *pmcw = &sch->schib.pmcw;
  218. return sprintf (buf, "%02x %02x %02x\n",
  219. pmcw->pim, pmcw->pam, pmcw->pom);
  220. }
  221. static ssize_t
  222. devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
  223. {
  224. struct ccw_device *cdev = to_ccwdev(dev);
  225. struct ccw_device_id *id = &(cdev->id);
  226. if (id->dev_type != 0)
  227. return sprintf(buf, "%04x/%02x\n",
  228. id->dev_type, id->dev_model);
  229. else
  230. return sprintf(buf, "n/a\n");
  231. }
  232. static ssize_t
  233. cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
  234. {
  235. struct ccw_device *cdev = to_ccwdev(dev);
  236. struct ccw_device_id *id = &(cdev->id);
  237. return sprintf(buf, "%04x/%02x\n",
  238. id->cu_type, id->cu_model);
  239. }
  240. static ssize_t
  241. modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
  242. {
  243. struct ccw_device *cdev = to_ccwdev(dev);
  244. struct ccw_device_id *id = &(cdev->id);
  245. int len;
  246. len = snprint_alias(buf, PAGE_SIZE, id, "\n");
  247. return len > PAGE_SIZE ? PAGE_SIZE : len;
  248. }
  249. static ssize_t
  250. online_show (struct device *dev, struct device_attribute *attr, char *buf)
  251. {
  252. struct ccw_device *cdev = to_ccwdev(dev);
  253. return sprintf(buf, cdev->online ? "1\n" : "0\n");
  254. }
  255. int ccw_device_is_orphan(struct ccw_device *cdev)
  256. {
  257. return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
  258. }
  259. static void ccw_device_unregister(struct ccw_device *cdev)
  260. {
  261. if (test_and_clear_bit(1, &cdev->private->registered))
  262. device_del(&cdev->dev);
  263. }
  264. static void ccw_device_remove_orphan_cb(struct work_struct *work)
  265. {
  266. struct ccw_device_private *priv;
  267. struct ccw_device *cdev;
  268. priv = container_of(work, struct ccw_device_private, kick_work);
  269. cdev = priv->cdev;
  270. ccw_device_unregister(cdev);
  271. put_device(&cdev->dev);
  272. /* Release cdev reference for workqueue processing. */
  273. put_device(&cdev->dev);
  274. }
  275. static void
  276. ccw_device_remove_disconnected(struct ccw_device *cdev)
  277. {
  278. unsigned long flags;
  279. /*
  280. * Forced offline in disconnected state means
  281. * 'throw away device'.
  282. */
  283. if (ccw_device_is_orphan(cdev)) {
  284. /*
  285. * Deregister ccw device.
  286. * Unfortunately, we cannot do this directly from the
  287. * attribute method.
  288. */
  289. /* Get cdev reference for workqueue processing. */
  290. if (!get_device(&cdev->dev))
  291. return;
  292. spin_lock_irqsave(cdev->ccwlock, flags);
  293. cdev->private->state = DEV_STATE_NOT_OPER;
  294. spin_unlock_irqrestore(cdev->ccwlock, flags);
  295. PREPARE_WORK(&cdev->private->kick_work,
  296. ccw_device_remove_orphan_cb);
  297. queue_work(slow_path_wq, &cdev->private->kick_work);
  298. } else
  299. /* Deregister subchannel, which will kill the ccw device. */
  300. ccw_device_schedule_sch_unregister(cdev);
  301. }
  302. /**
  303. * ccw_device_set_offline() - disable a ccw device for I/O
  304. * @cdev: target ccw device
  305. *
  306. * This function calls the driver's set_offline() function for @cdev, if
  307. * given, and then disables @cdev.
  308. * Returns:
  309. * %0 on success and a negative error value on failure.
  310. * Context:
  311. * enabled, ccw device lock not held
  312. */
  313. int ccw_device_set_offline(struct ccw_device *cdev)
  314. {
  315. int ret;
  316. if (!cdev)
  317. return -ENODEV;
  318. if (!cdev->online || !cdev->drv)
  319. return -EINVAL;
  320. if (cdev->drv->set_offline) {
  321. ret = cdev->drv->set_offline(cdev);
  322. if (ret != 0)
  323. return ret;
  324. }
  325. cdev->online = 0;
  326. spin_lock_irq(cdev->ccwlock);
  327. /* Wait until a final state or DISCONNECTED is reached */
  328. while (!dev_fsm_final_state(cdev) &&
  329. cdev->private->state != DEV_STATE_DISCONNECTED) {
  330. spin_unlock_irq(cdev->ccwlock);
  331. wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
  332. cdev->private->state == DEV_STATE_DISCONNECTED));
  333. spin_lock_irq(cdev->ccwlock);
  334. }
  335. ret = ccw_device_offline(cdev);
  336. if (ret)
  337. goto error;
  338. spin_unlock_irq(cdev->ccwlock);
  339. wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
  340. cdev->private->state == DEV_STATE_DISCONNECTED));
  341. /* Give up reference from ccw_device_set_online(). */
  342. put_device(&cdev->dev);
  343. return 0;
  344. error:
  345. CIO_MSG_EVENT(0, "ccw_device_offline returned %d, device 0.%x.%04x\n",
  346. ret, cdev->private->dev_id.ssid,
  347. cdev->private->dev_id.devno);
  348. cdev->private->state = DEV_STATE_OFFLINE;
  349. dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
  350. spin_unlock_irq(cdev->ccwlock);
  351. /* Give up reference from ccw_device_set_online(). */
  352. put_device(&cdev->dev);
  353. return -ENODEV;
  354. }
  355. /**
  356. * ccw_device_set_online() - enable a ccw device for I/O
  357. * @cdev: target ccw device
  358. *
  359. * This function first enables @cdev and then calls the driver's set_online()
  360. * function for @cdev, if given. If set_online() returns an error, @cdev is
  361. * disabled again.
  362. * Returns:
  363. * %0 on success and a negative error value on failure.
  364. * Context:
  365. * enabled, ccw device lock not held
  366. */
  367. int ccw_device_set_online(struct ccw_device *cdev)
  368. {
  369. int ret;
  370. int ret2;
  371. if (!cdev)
  372. return -ENODEV;
  373. if (cdev->online || !cdev->drv)
  374. return -EINVAL;
  375. /* Hold on to an extra reference while device is online. */
  376. if (!get_device(&cdev->dev))
  377. return -ENODEV;
  378. spin_lock_irq(cdev->ccwlock);
  379. ret = ccw_device_online(cdev);
  380. spin_unlock_irq(cdev->ccwlock);
  381. if (ret == 0)
  382. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  383. else {
  384. CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
  385. "device 0.%x.%04x\n",
  386. ret, cdev->private->dev_id.ssid,
  387. cdev->private->dev_id.devno);
  388. /* Give up online reference since onlining failed. */
  389. put_device(&cdev->dev);
  390. return ret;
  391. }
  392. spin_lock_irq(cdev->ccwlock);
  393. /* Check if online processing was successful */
  394. if ((cdev->private->state != DEV_STATE_ONLINE) &&
  395. (cdev->private->state != DEV_STATE_W4SENSE)) {
  396. spin_unlock_irq(cdev->ccwlock);
  397. /* Give up online reference since onlining failed. */
  398. put_device(&cdev->dev);
  399. return -ENODEV;
  400. }
  401. spin_unlock_irq(cdev->ccwlock);
  402. if (cdev->drv->set_online)
  403. ret = cdev->drv->set_online(cdev);
  404. if (ret)
  405. goto rollback;
  406. cdev->online = 1;
  407. return 0;
  408. rollback:
  409. spin_lock_irq(cdev->ccwlock);
  410. /* Wait until a final state or DISCONNECTED is reached */
  411. while (!dev_fsm_final_state(cdev) &&
  412. cdev->private->state != DEV_STATE_DISCONNECTED) {
  413. spin_unlock_irq(cdev->ccwlock);
  414. wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
  415. cdev->private->state == DEV_STATE_DISCONNECTED));
  416. spin_lock_irq(cdev->ccwlock);
  417. }
  418. ret2 = ccw_device_offline(cdev);
  419. if (ret2)
  420. goto error;
  421. spin_unlock_irq(cdev->ccwlock);
  422. wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
  423. cdev->private->state == DEV_STATE_DISCONNECTED));
  424. /* Give up online reference since onlining failed. */
  425. put_device(&cdev->dev);
  426. return ret;
  427. error:
  428. CIO_MSG_EVENT(0, "rollback ccw_device_offline returned %d, "
  429. "device 0.%x.%04x\n",
  430. ret2, cdev->private->dev_id.ssid,
  431. cdev->private->dev_id.devno);
  432. cdev->private->state = DEV_STATE_OFFLINE;
  433. spin_unlock_irq(cdev->ccwlock);
  434. /* Give up online reference since onlining failed. */
  435. put_device(&cdev->dev);
  436. return ret;
  437. }
  438. static int online_store_handle_offline(struct ccw_device *cdev)
  439. {
  440. if (cdev->private->state == DEV_STATE_DISCONNECTED)
  441. ccw_device_remove_disconnected(cdev);
  442. else if (cdev->online && cdev->drv && cdev->drv->set_offline)
  443. return ccw_device_set_offline(cdev);
  444. return 0;
  445. }
  446. static int online_store_recog_and_online(struct ccw_device *cdev)
  447. {
  448. int ret;
  449. /* Do device recognition, if needed. */
  450. if (cdev->private->state == DEV_STATE_BOXED) {
  451. ret = ccw_device_recognition(cdev);
  452. if (ret) {
  453. CIO_MSG_EVENT(0, "Couldn't start recognition "
  454. "for device 0.%x.%04x (ret=%d)\n",
  455. cdev->private->dev_id.ssid,
  456. cdev->private->dev_id.devno, ret);
  457. return ret;
  458. }
  459. wait_event(cdev->private->wait_q,
  460. cdev->private->flags.recog_done);
  461. if (cdev->private->state != DEV_STATE_OFFLINE)
  462. /* recognition failed */
  463. return -EAGAIN;
  464. }
  465. if (cdev->drv && cdev->drv->set_online)
  466. ccw_device_set_online(cdev);
  467. return 0;
  468. }
  469. static int online_store_handle_online(struct ccw_device *cdev, int force)
  470. {
  471. int ret;
  472. ret = online_store_recog_and_online(cdev);
  473. if (ret && !force)
  474. return ret;
  475. if (force && cdev->private->state == DEV_STATE_BOXED) {
  476. ret = ccw_device_stlck(cdev);
  477. if (ret)
  478. return ret;
  479. if (cdev->id.cu_type == 0)
  480. cdev->private->state = DEV_STATE_NOT_OPER;
  481. ret = online_store_recog_and_online(cdev);
  482. if (ret)
  483. return ret;
  484. }
  485. return 0;
  486. }
  487. static ssize_t online_store (struct device *dev, struct device_attribute *attr,
  488. const char *buf, size_t count)
  489. {
  490. struct ccw_device *cdev = to_ccwdev(dev);
  491. int force, ret;
  492. unsigned long i;
  493. if ((cdev->private->state != DEV_STATE_OFFLINE &&
  494. cdev->private->state != DEV_STATE_ONLINE &&
  495. cdev->private->state != DEV_STATE_BOXED &&
  496. cdev->private->state != DEV_STATE_DISCONNECTED) ||
  497. atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
  498. return -EAGAIN;
  499. if (cdev->drv && !try_module_get(cdev->drv->owner)) {
  500. atomic_set(&cdev->private->onoff, 0);
  501. return -EINVAL;
  502. }
  503. if (!strncmp(buf, "force\n", count)) {
  504. force = 1;
  505. i = 1;
  506. ret = 0;
  507. } else {
  508. force = 0;
  509. ret = strict_strtoul(buf, 16, &i);
  510. }
  511. if (ret)
  512. goto out;
  513. switch (i) {
  514. case 0:
  515. ret = online_store_handle_offline(cdev);
  516. break;
  517. case 1:
  518. ret = online_store_handle_online(cdev, force);
  519. break;
  520. default:
  521. ret = -EINVAL;
  522. }
  523. out:
  524. if (cdev->drv)
  525. module_put(cdev->drv->owner);
  526. atomic_set(&cdev->private->onoff, 0);
  527. return (ret < 0) ? ret : count;
  528. }
  529. static ssize_t
  530. available_show (struct device *dev, struct device_attribute *attr, char *buf)
  531. {
  532. struct ccw_device *cdev = to_ccwdev(dev);
  533. struct subchannel *sch;
  534. if (ccw_device_is_orphan(cdev))
  535. return sprintf(buf, "no device\n");
  536. switch (cdev->private->state) {
  537. case DEV_STATE_BOXED:
  538. return sprintf(buf, "boxed\n");
  539. case DEV_STATE_DISCONNECTED:
  540. case DEV_STATE_DISCONNECTED_SENSE_ID:
  541. case DEV_STATE_NOT_OPER:
  542. sch = to_subchannel(dev->parent);
  543. if (!sch->lpm)
  544. return sprintf(buf, "no path\n");
  545. else
  546. return sprintf(buf, "no device\n");
  547. default:
  548. /* All other states considered fine. */
  549. return sprintf(buf, "good\n");
  550. }
  551. }
  552. static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
  553. static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
  554. static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
  555. static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
  556. static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
  557. static DEVICE_ATTR(online, 0644, online_show, online_store);
  558. static DEVICE_ATTR(availability, 0444, available_show, NULL);
  559. static struct attribute *io_subchannel_attrs[] = {
  560. &dev_attr_chpids.attr,
  561. &dev_attr_pimpampom.attr,
  562. NULL,
  563. };
  564. static struct attribute_group io_subchannel_attr_group = {
  565. .attrs = io_subchannel_attrs,
  566. };
  567. static struct attribute * ccwdev_attrs[] = {
  568. &dev_attr_devtype.attr,
  569. &dev_attr_cutype.attr,
  570. &dev_attr_modalias.attr,
  571. &dev_attr_online.attr,
  572. &dev_attr_cmb_enable.attr,
  573. &dev_attr_availability.attr,
  574. NULL,
  575. };
  576. static struct attribute_group ccwdev_attr_group = {
  577. .attrs = ccwdev_attrs,
  578. };
  579. static struct attribute_group *ccwdev_attr_groups[] = {
  580. &ccwdev_attr_group,
  581. NULL,
  582. };
  583. /* this is a simple abstraction for device_register that sets the
  584. * correct bus type and adds the bus specific files */
  585. static int ccw_device_register(struct ccw_device *cdev)
  586. {
  587. struct device *dev = &cdev->dev;
  588. int ret;
  589. dev->bus = &ccw_bus_type;
  590. if ((ret = device_add(dev)))
  591. return ret;
  592. set_bit(1, &cdev->private->registered);
  593. return ret;
  594. }
  595. struct match_data {
  596. struct ccw_dev_id dev_id;
  597. struct ccw_device * sibling;
  598. };
  599. static int
  600. match_devno(struct device * dev, void * data)
  601. {
  602. struct match_data * d = data;
  603. struct ccw_device * cdev;
  604. cdev = to_ccwdev(dev);
  605. if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
  606. !ccw_device_is_orphan(cdev) &&
  607. ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
  608. (cdev != d->sibling))
  609. return 1;
  610. return 0;
  611. }
  612. static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
  613. struct ccw_device *sibling)
  614. {
  615. struct device *dev;
  616. struct match_data data;
  617. data.dev_id = *dev_id;
  618. data.sibling = sibling;
  619. dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
  620. return dev ? to_ccwdev(dev) : NULL;
  621. }
  622. static int match_orphan(struct device *dev, void *data)
  623. {
  624. struct ccw_dev_id *dev_id;
  625. struct ccw_device *cdev;
  626. dev_id = data;
  627. cdev = to_ccwdev(dev);
  628. return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
  629. }
  630. static struct ccw_device *
  631. get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css,
  632. struct ccw_dev_id *dev_id)
  633. {
  634. struct device *dev;
  635. dev = device_find_child(&css->pseudo_subchannel->dev, dev_id,
  636. match_orphan);
  637. return dev ? to_ccwdev(dev) : NULL;
  638. }
  639. void ccw_device_do_unbind_bind(struct work_struct *work)
  640. {
  641. struct ccw_device_private *priv;
  642. struct ccw_device *cdev;
  643. struct subchannel *sch;
  644. int ret;
  645. priv = container_of(work, struct ccw_device_private, kick_work);
  646. cdev = priv->cdev;
  647. sch = to_subchannel(cdev->dev.parent);
  648. if (test_bit(1, &cdev->private->registered)) {
  649. device_release_driver(&cdev->dev);
  650. ret = device_attach(&cdev->dev);
  651. WARN_ON(ret == -ENODEV);
  652. }
  653. }
  654. static void
  655. ccw_device_release(struct device *dev)
  656. {
  657. struct ccw_device *cdev;
  658. cdev = to_ccwdev(dev);
  659. /* Release reference of parent subchannel. */
  660. put_device(cdev->dev.parent);
  661. kfree(cdev->private);
  662. kfree(cdev);
  663. }
  664. static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
  665. {
  666. struct ccw_device *cdev;
  667. cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
  668. if (cdev) {
  669. cdev->private = kzalloc(sizeof(struct ccw_device_private),
  670. GFP_KERNEL | GFP_DMA);
  671. if (cdev->private)
  672. return cdev;
  673. }
  674. kfree(cdev);
  675. return ERR_PTR(-ENOMEM);
  676. }
  677. static int io_subchannel_initialize_dev(struct subchannel *sch,
  678. struct ccw_device *cdev)
  679. {
  680. cdev->private->cdev = cdev;
  681. atomic_set(&cdev->private->onoff, 0);
  682. cdev->dev.parent = &sch->dev;
  683. cdev->dev.release = ccw_device_release;
  684. INIT_WORK(&cdev->private->kick_work, NULL);
  685. cdev->dev.groups = ccwdev_attr_groups;
  686. /* Do first half of device_register. */
  687. device_initialize(&cdev->dev);
  688. if (!get_device(&sch->dev)) {
  689. /* Release reference from device_initialize(). */
  690. put_device(&cdev->dev);
  691. return -ENODEV;
  692. }
  693. return 0;
  694. }
  695. static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
  696. {
  697. struct ccw_device *cdev;
  698. int ret;
  699. cdev = io_subchannel_allocate_dev(sch);
  700. if (!IS_ERR(cdev)) {
  701. ret = io_subchannel_initialize_dev(sch, cdev);
  702. if (ret)
  703. cdev = ERR_PTR(ret);
  704. }
  705. return cdev;
  706. }
  707. static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
  708. static void sch_attach_device(struct subchannel *sch,
  709. struct ccw_device *cdev)
  710. {
  711. css_update_ssd_info(sch);
  712. spin_lock_irq(sch->lock);
  713. sch_set_cdev(sch, cdev);
  714. cdev->private->schid = sch->schid;
  715. cdev->ccwlock = sch->lock;
  716. ccw_device_trigger_reprobe(cdev);
  717. spin_unlock_irq(sch->lock);
  718. }
  719. static void sch_attach_disconnected_device(struct subchannel *sch,
  720. struct ccw_device *cdev)
  721. {
  722. struct subchannel *other_sch;
  723. int ret;
  724. /* Get reference for new parent. */
  725. if (!get_device(&sch->dev))
  726. return;
  727. other_sch = to_subchannel(cdev->dev.parent);
  728. /* Note: device_move() changes cdev->dev.parent */
  729. ret = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
  730. if (ret) {
  731. CIO_MSG_EVENT(0, "Moving disconnected device 0.%x.%04x failed "
  732. "(ret=%d)!\n", cdev->private->dev_id.ssid,
  733. cdev->private->dev_id.devno, ret);
  734. /* Put reference for new parent. */
  735. put_device(&sch->dev);
  736. return;
  737. }
  738. sch_set_cdev(other_sch, NULL);
  739. /* No need to keep a subchannel without ccw device around. */
  740. css_sch_device_unregister(other_sch);
  741. sch_attach_device(sch, cdev);
  742. /* Put reference for old parent. */
  743. put_device(&other_sch->dev);
  744. }
  745. static void sch_attach_orphaned_device(struct subchannel *sch,
  746. struct ccw_device *cdev)
  747. {
  748. int ret;
  749. struct subchannel *pseudo_sch;
  750. /* Get reference for new parent. */
  751. if (!get_device(&sch->dev))
  752. return;
  753. pseudo_sch = to_subchannel(cdev->dev.parent);
  754. /*
  755. * Try to move the ccw device to its new subchannel.
  756. * Note: device_move() changes cdev->dev.parent
  757. */
  758. ret = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
  759. if (ret) {
  760. CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
  761. "failed (ret=%d)!\n",
  762. cdev->private->dev_id.ssid,
  763. cdev->private->dev_id.devno, ret);
  764. /* Put reference for new parent. */
  765. put_device(&sch->dev);
  766. return;
  767. }
  768. sch_attach_device(sch, cdev);
  769. /* Put reference on pseudo subchannel. */
  770. put_device(&pseudo_sch->dev);
  771. }
  772. static void sch_create_and_recog_new_device(struct subchannel *sch)
  773. {
  774. struct ccw_device *cdev;
  775. /* Need to allocate a new ccw device. */
  776. cdev = io_subchannel_create_ccwdev(sch);
  777. if (IS_ERR(cdev)) {
  778. /* OK, we did everything we could... */
  779. css_sch_device_unregister(sch);
  780. return;
  781. }
  782. spin_lock_irq(sch->lock);
  783. sch_set_cdev(sch, cdev);
  784. spin_unlock_irq(sch->lock);
  785. /* Start recognition for the new ccw device. */
  786. if (io_subchannel_recog(cdev, sch)) {
  787. spin_lock_irq(sch->lock);
  788. sch_set_cdev(sch, NULL);
  789. spin_unlock_irq(sch->lock);
  790. css_sch_device_unregister(sch);
  791. /* Put reference from io_subchannel_create_ccwdev(). */
  792. put_device(&sch->dev);
  793. /* Give up initial reference. */
  794. put_device(&cdev->dev);
  795. }
  796. }
  797. void ccw_device_move_to_orphanage(struct work_struct *work)
  798. {
  799. struct ccw_device_private *priv;
  800. struct ccw_device *cdev;
  801. struct ccw_device *replacing_cdev;
  802. struct subchannel *sch;
  803. int ret;
  804. struct channel_subsystem *css;
  805. struct ccw_dev_id dev_id;
  806. priv = container_of(work, struct ccw_device_private, kick_work);
  807. cdev = priv->cdev;
  808. sch = to_subchannel(cdev->dev.parent);
  809. css = to_css(sch->dev.parent);
  810. dev_id.devno = sch->schib.pmcw.dev;
  811. dev_id.ssid = sch->schid.ssid;
  812. /* Increase refcount for pseudo subchannel. */
  813. get_device(&css->pseudo_subchannel->dev);
  814. /*
  815. * Move the orphaned ccw device to the orphanage so the replacing
  816. * ccw device can take its place on the subchannel.
  817. * Note: device_move() changes cdev->dev.parent
  818. */
  819. ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev,
  820. DPM_ORDER_NONE);
  821. if (ret) {
  822. CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
  823. "(ret=%d)!\n", cdev->private->dev_id.ssid,
  824. cdev->private->dev_id.devno, ret);
  825. /* Decrease refcount for pseudo subchannel again. */
  826. put_device(&css->pseudo_subchannel->dev);
  827. return;
  828. }
  829. cdev->ccwlock = css->pseudo_subchannel->lock;
  830. /*
  831. * Search for the replacing ccw device
  832. * - among the disconnected devices
  833. * - in the orphanage
  834. */
  835. replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
  836. if (replacing_cdev) {
  837. sch_attach_disconnected_device(sch, replacing_cdev);
  838. /* Release reference from get_disc_ccwdev_by_dev_id() */
  839. put_device(&replacing_cdev->dev);
  840. /* Release reference of subchannel from old cdev. */
  841. put_device(&sch->dev);
  842. return;
  843. }
  844. replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id);
  845. if (replacing_cdev) {
  846. sch_attach_orphaned_device(sch, replacing_cdev);
  847. /* Release reference from get_orphaned_ccwdev_by_dev_id() */
  848. put_device(&replacing_cdev->dev);
  849. /* Release reference of subchannel from old cdev. */
  850. put_device(&sch->dev);
  851. return;
  852. }
  853. sch_create_and_recog_new_device(sch);
  854. /* Release reference of subchannel from old cdev. */
  855. put_device(&sch->dev);
  856. }
  857. /*
  858. * Register recognized device.
  859. */
  860. static void
  861. io_subchannel_register(struct work_struct *work)
  862. {
  863. struct ccw_device_private *priv;
  864. struct ccw_device *cdev;
  865. struct subchannel *sch;
  866. int ret;
  867. unsigned long flags;
  868. priv = container_of(work, struct ccw_device_private, kick_work);
  869. cdev = priv->cdev;
  870. sch = to_subchannel(cdev->dev.parent);
  871. /*
  872. * Check if subchannel is still registered. It may have become
  873. * unregistered if a machine check hit us after finishing
  874. * device recognition but before the register work could be
  875. * queued.
  876. */
  877. if (!device_is_registered(&sch->dev))
  878. goto out_err;
  879. css_update_ssd_info(sch);
  880. /*
  881. * io_subchannel_register() will also be called after device
  882. * recognition has been done for a boxed device (which will already
  883. * be registered). We need to reprobe since we may now have sense id
  884. * information.
  885. */
  886. if (device_is_registered(&cdev->dev)) {
  887. if (!cdev->drv) {
  888. ret = device_reprobe(&cdev->dev);
  889. if (ret)
  890. /* We can't do much here. */
  891. CIO_MSG_EVENT(0, "device_reprobe() returned"
  892. " %d for 0.%x.%04x\n", ret,
  893. cdev->private->dev_id.ssid,
  894. cdev->private->dev_id.devno);
  895. }
  896. goto out;
  897. }
  898. /*
  899. * Now we know this subchannel will stay, we can throw
  900. * our delayed uevent.
  901. */
  902. dev_set_uevent_suppress(&sch->dev, 0);
  903. kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
  904. /* make it known to the system */
  905. ret = ccw_device_register(cdev);
  906. if (ret) {
  907. CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
  908. cdev->private->dev_id.ssid,
  909. cdev->private->dev_id.devno, ret);
  910. spin_lock_irqsave(sch->lock, flags);
  911. sch_set_cdev(sch, NULL);
  912. spin_unlock_irqrestore(sch->lock, flags);
  913. /* Release initial device reference. */
  914. put_device(&cdev->dev);
  915. goto out_err;
  916. }
  917. out:
  918. cdev->private->flags.recog_done = 1;
  919. wake_up(&cdev->private->wait_q);
  920. out_err:
  921. /* Release reference for workqueue processing. */
  922. put_device(&cdev->dev);
  923. if (atomic_dec_and_test(&ccw_device_init_count))
  924. wake_up(&ccw_device_init_wq);
  925. }
  926. static void ccw_device_call_sch_unregister(struct work_struct *work)
  927. {
  928. struct ccw_device_private *priv;
  929. struct ccw_device *cdev;
  930. struct subchannel *sch;
  931. priv = container_of(work, struct ccw_device_private, kick_work);
  932. cdev = priv->cdev;
  933. /* Get subchannel reference for local processing. */
  934. if (!get_device(cdev->dev.parent))
  935. return;
  936. sch = to_subchannel(cdev->dev.parent);
  937. css_sch_device_unregister(sch);
  938. /* Release cdev reference for workqueue processing.*/
  939. put_device(&cdev->dev);
  940. /* Release subchannel reference for local processing. */
  941. put_device(&sch->dev);
  942. }
  943. void ccw_device_schedule_sch_unregister(struct ccw_device *cdev)
  944. {
  945. /* Get cdev reference for workqueue processing. */
  946. if (!get_device(&cdev->dev))
  947. return;
  948. PREPARE_WORK(&cdev->private->kick_work,
  949. ccw_device_call_sch_unregister);
  950. queue_work(slow_path_wq, &cdev->private->kick_work);
  951. }
  952. /*
  953. * subchannel recognition done. Called from the state machine.
  954. */
  955. void
  956. io_subchannel_recog_done(struct ccw_device *cdev)
  957. {
  958. if (css_init_done == 0) {
  959. cdev->private->flags.recog_done = 1;
  960. return;
  961. }
  962. switch (cdev->private->state) {
  963. case DEV_STATE_BOXED:
  964. /* Device did not respond in time. */
  965. case DEV_STATE_NOT_OPER:
  966. cdev->private->flags.recog_done = 1;
  967. ccw_device_schedule_sch_unregister(cdev);
  968. if (atomic_dec_and_test(&ccw_device_init_count))
  969. wake_up(&ccw_device_init_wq);
  970. break;
  971. case DEV_STATE_OFFLINE:
  972. /*
  973. * We can't register the device in interrupt context so
  974. * we schedule a work item.
  975. */
  976. if (!get_device(&cdev->dev))
  977. break;
  978. PREPARE_WORK(&cdev->private->kick_work,
  979. io_subchannel_register);
  980. queue_work(slow_path_wq, &cdev->private->kick_work);
  981. break;
  982. }
  983. }
  984. static int
  985. io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
  986. {
  987. int rc;
  988. struct ccw_device_private *priv;
  989. sch_set_cdev(sch, cdev);
  990. cdev->ccwlock = sch->lock;
  991. /* Init private data. */
  992. priv = cdev->private;
  993. priv->dev_id.devno = sch->schib.pmcw.dev;
  994. priv->dev_id.ssid = sch->schid.ssid;
  995. priv->schid = sch->schid;
  996. priv->state = DEV_STATE_NOT_OPER;
  997. INIT_LIST_HEAD(&priv->cmb_list);
  998. init_waitqueue_head(&priv->wait_q);
  999. init_timer(&priv->timer);
  1000. /* Set an initial name for the device. */
  1001. if (cio_is_console(sch->schid))
  1002. cdev->dev.init_name = cio_get_console_cdev_name(sch);
  1003. else
  1004. dev_set_name(&cdev->dev, "0.%x.%04x",
  1005. sch->schid.ssid, sch->schib.pmcw.dev);
  1006. /* Increase counter of devices currently in recognition. */
  1007. atomic_inc(&ccw_device_init_count);
  1008. /* Start async. device sensing. */
  1009. spin_lock_irq(sch->lock);
  1010. rc = ccw_device_recognition(cdev);
  1011. spin_unlock_irq(sch->lock);
  1012. if (rc) {
  1013. if (atomic_dec_and_test(&ccw_device_init_count))
  1014. wake_up(&ccw_device_init_wq);
  1015. }
  1016. return rc;
  1017. }
  1018. static void ccw_device_move_to_sch(struct work_struct *work)
  1019. {
  1020. struct ccw_device_private *priv;
  1021. int rc;
  1022. struct subchannel *sch;
  1023. struct ccw_device *cdev;
  1024. struct subchannel *former_parent;
  1025. priv = container_of(work, struct ccw_device_private, kick_work);
  1026. sch = priv->sch;
  1027. cdev = priv->cdev;
  1028. former_parent = to_subchannel(cdev->dev.parent);
  1029. /* Get reference for new parent. */
  1030. if (!get_device(&sch->dev))
  1031. return;
  1032. mutex_lock(&sch->reg_mutex);
  1033. /*
  1034. * Try to move the ccw device to its new subchannel.
  1035. * Note: device_move() changes cdev->dev.parent
  1036. */
  1037. rc = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
  1038. mutex_unlock(&sch->reg_mutex);
  1039. if (rc) {
  1040. CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to subchannel "
  1041. "0.%x.%04x failed (ret=%d)!\n",
  1042. cdev->private->dev_id.ssid,
  1043. cdev->private->dev_id.devno, sch->schid.ssid,
  1044. sch->schid.sch_no, rc);
  1045. css_sch_device_unregister(sch);
  1046. /* Put reference for new parent again. */
  1047. put_device(&sch->dev);
  1048. goto out;
  1049. }
  1050. if (!sch_is_pseudo_sch(former_parent)) {
  1051. spin_lock_irq(former_parent->lock);
  1052. sch_set_cdev(former_parent, NULL);
  1053. spin_unlock_irq(former_parent->lock);
  1054. css_sch_device_unregister(former_parent);
  1055. /* Reset intparm to zeroes. */
  1056. former_parent->config.intparm = 0;
  1057. cio_commit_config(former_parent);
  1058. }
  1059. sch_attach_device(sch, cdev);
  1060. out:
  1061. /* Put reference for old parent. */
  1062. put_device(&former_parent->dev);
  1063. put_device(&cdev->dev);
  1064. }
  1065. static void io_subchannel_irq(struct subchannel *sch)
  1066. {
  1067. struct ccw_device *cdev;
  1068. cdev = sch_get_cdev(sch);
  1069. CIO_TRACE_EVENT(6, "IRQ");
  1070. CIO_TRACE_EVENT(6, dev_name(&sch->dev));
  1071. if (cdev)
  1072. dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
  1073. }
  1074. void io_subchannel_init_config(struct subchannel *sch)
  1075. {
  1076. memset(&sch->config, 0, sizeof(sch->config));
  1077. sch->config.csense = 1;
  1078. /* Use subchannel mp mode when there is more than 1 installed CHPID. */
  1079. if ((sch->schib.pmcw.pim & (sch->schib.pmcw.pim - 1)) != 0)
  1080. sch->config.mp = 1;
  1081. }
  1082. static void io_subchannel_init_fields(struct subchannel *sch)
  1083. {
  1084. if (cio_is_console(sch->schid))
  1085. sch->opm = 0xff;
  1086. else
  1087. sch->opm = chp_get_sch_opm(sch);
  1088. sch->lpm = sch->schib.pmcw.pam & sch->opm;
  1089. sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
  1090. CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
  1091. " - PIM = %02X, PAM = %02X, POM = %02X\n",
  1092. sch->schib.pmcw.dev, sch->schid.ssid,
  1093. sch->schid.sch_no, sch->schib.pmcw.pim,
  1094. sch->schib.pmcw.pam, sch->schib.pmcw.pom);
  1095. io_subchannel_init_config(sch);
  1096. }
  1097. static void io_subchannel_do_unreg(struct work_struct *work)
  1098. {
  1099. struct subchannel *sch;
  1100. sch = container_of(work, struct subchannel, work);
  1101. css_sch_device_unregister(sch);
  1102. put_device(&sch->dev);
  1103. }
  1104. /* Schedule unregister if we have no cdev. */
  1105. static void io_subchannel_schedule_removal(struct subchannel *sch)
  1106. {
  1107. get_device(&sch->dev);
  1108. INIT_WORK(&sch->work, io_subchannel_do_unreg);
  1109. queue_work(slow_path_wq, &sch->work);
  1110. }
  1111. /*
  1112. * Note: We always return 0 so that we bind to the device even on error.
  1113. * This is needed so that our remove function is called on unregister.
  1114. */
  1115. static int io_subchannel_probe(struct subchannel *sch)
  1116. {
  1117. struct ccw_device *cdev;
  1118. int rc;
  1119. unsigned long flags;
  1120. struct ccw_dev_id dev_id;
  1121. cdev = sch_get_cdev(sch);
  1122. if (cdev) {
  1123. rc = sysfs_create_group(&sch->dev.kobj,
  1124. &io_subchannel_attr_group);
  1125. if (rc)
  1126. CIO_MSG_EVENT(0, "Failed to create io subchannel "
  1127. "attributes for subchannel "
  1128. "0.%x.%04x (rc=%d)\n",
  1129. sch->schid.ssid, sch->schid.sch_no, rc);
  1130. /*
  1131. * This subchannel already has an associated ccw_device.
  1132. * Throw the delayed uevent for the subchannel, register
  1133. * the ccw_device and exit. This happens for all early
  1134. * devices, e.g. the console.
  1135. */
  1136. dev_set_uevent_suppress(&sch->dev, 0);
  1137. kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
  1138. cdev->dev.groups = ccwdev_attr_groups;
  1139. device_initialize(&cdev->dev);
  1140. ccw_device_register(cdev);
  1141. /*
  1142. * Check if the device is already online. If it is
  1143. * the reference count needs to be corrected since we
  1144. * didn't obtain a reference in ccw_device_set_online.
  1145. */
  1146. if (cdev->private->state != DEV_STATE_NOT_OPER &&
  1147. cdev->private->state != DEV_STATE_OFFLINE &&
  1148. cdev->private->state != DEV_STATE_BOXED)
  1149. get_device(&cdev->dev);
  1150. return 0;
  1151. }
  1152. io_subchannel_init_fields(sch);
  1153. rc = cio_commit_config(sch);
  1154. if (rc)
  1155. goto out_schedule;
  1156. rc = sysfs_create_group(&sch->dev.kobj,
  1157. &io_subchannel_attr_group);
  1158. if (rc)
  1159. goto out_schedule;
  1160. /* Allocate I/O subchannel private data. */
  1161. sch->private = kzalloc(sizeof(struct io_subchannel_private),
  1162. GFP_KERNEL | GFP_DMA);
  1163. if (!sch->private)
  1164. goto out_err;
  1165. /*
  1166. * First check if a fitting device may be found amongst the
  1167. * disconnected devices or in the orphanage.
  1168. */
  1169. dev_id.devno = sch->schib.pmcw.dev;
  1170. dev_id.ssid = sch->schid.ssid;
  1171. cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL);
  1172. if (!cdev)
  1173. cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent),
  1174. &dev_id);
  1175. if (cdev) {
  1176. /*
  1177. * Schedule moving the device until when we have a registered
  1178. * subchannel to move to and succeed the probe. We can
  1179. * unregister later again, when the probe is through.
  1180. */
  1181. cdev->private->sch = sch;
  1182. PREPARE_WORK(&cdev->private->kick_work,
  1183. ccw_device_move_to_sch);
  1184. queue_work(slow_path_wq, &cdev->private->kick_work);
  1185. return 0;
  1186. }
  1187. cdev = io_subchannel_create_ccwdev(sch);
  1188. if (IS_ERR(cdev))
  1189. goto out_err;
  1190. rc = io_subchannel_recog(cdev, sch);
  1191. if (rc) {
  1192. spin_lock_irqsave(sch->lock, flags);
  1193. io_subchannel_recog_done(cdev);
  1194. spin_unlock_irqrestore(sch->lock, flags);
  1195. }
  1196. return 0;
  1197. out_err:
  1198. kfree(sch->private);
  1199. sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
  1200. out_schedule:
  1201. io_subchannel_schedule_removal(sch);
  1202. return 0;
  1203. }
  1204. static int
  1205. io_subchannel_remove (struct subchannel *sch)
  1206. {
  1207. struct ccw_device *cdev;
  1208. unsigned long flags;
  1209. cdev = sch_get_cdev(sch);
  1210. if (!cdev)
  1211. return 0;
  1212. /* Set ccw device to not operational and drop reference. */
  1213. spin_lock_irqsave(cdev->ccwlock, flags);
  1214. sch_set_cdev(sch, NULL);
  1215. cdev->private->state = DEV_STATE_NOT_OPER;
  1216. spin_unlock_irqrestore(cdev->ccwlock, flags);
  1217. ccw_device_unregister(cdev);
  1218. put_device(&cdev->dev);
  1219. kfree(sch->private);
  1220. sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
  1221. return 0;
  1222. }
  1223. static int io_subchannel_notify(struct subchannel *sch, int event)
  1224. {
  1225. struct ccw_device *cdev;
  1226. cdev = sch_get_cdev(sch);
  1227. if (!cdev)
  1228. return 0;
  1229. return ccw_device_notify(cdev, event);
  1230. }
  1231. static void io_subchannel_verify(struct subchannel *sch)
  1232. {
  1233. struct ccw_device *cdev;
  1234. cdev = sch_get_cdev(sch);
  1235. if (cdev)
  1236. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1237. }
  1238. static int check_for_io_on_path(struct subchannel *sch, int mask)
  1239. {
  1240. if (cio_update_schib(sch))
  1241. return 0;
  1242. if (scsw_actl(&sch->schib.scsw) && sch->schib.pmcw.lpum == mask)
  1243. return 1;
  1244. return 0;
  1245. }
  1246. static void terminate_internal_io(struct subchannel *sch,
  1247. struct ccw_device *cdev)
  1248. {
  1249. if (cio_clear(sch)) {
  1250. /* Recheck device in case clear failed. */
  1251. sch->lpm = 0;
  1252. if (cdev->online)
  1253. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1254. else
  1255. css_schedule_eval(sch->schid);
  1256. return;
  1257. }
  1258. cdev->private->state = DEV_STATE_CLEAR_VERIFY;
  1259. /* Request retry of internal operation. */
  1260. cdev->private->flags.intretry = 1;
  1261. /* Call handler. */
  1262. if (cdev->handler)
  1263. cdev->handler(cdev, cdev->private->intparm,
  1264. ERR_PTR(-EIO));
  1265. }
  1266. static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
  1267. {
  1268. struct ccw_device *cdev;
  1269. cdev = sch_get_cdev(sch);
  1270. if (!cdev)
  1271. return;
  1272. if (check_for_io_on_path(sch, mask)) {
  1273. if (cdev->private->state == DEV_STATE_ONLINE)
  1274. ccw_device_kill_io(cdev);
  1275. else {
  1276. terminate_internal_io(sch, cdev);
  1277. /* Re-start path verification. */
  1278. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1279. }
  1280. } else
  1281. /* trigger path verification. */
  1282. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1283. }
  1284. static int io_subchannel_chp_event(struct subchannel *sch,
  1285. struct chp_link *link, int event)
  1286. {
  1287. int mask;
  1288. mask = chp_ssd_get_mask(&sch->ssd_info, link);
  1289. if (!mask)
  1290. return 0;
  1291. switch (event) {
  1292. case CHP_VARY_OFF:
  1293. sch->opm &= ~mask;
  1294. sch->lpm &= ~mask;
  1295. io_subchannel_terminate_path(sch, mask);
  1296. break;
  1297. case CHP_VARY_ON:
  1298. sch->opm |= mask;
  1299. sch->lpm |= mask;
  1300. io_subchannel_verify(sch);
  1301. break;
  1302. case CHP_OFFLINE:
  1303. if (cio_update_schib(sch))
  1304. return -ENODEV;
  1305. io_subchannel_terminate_path(sch, mask);
  1306. break;
  1307. case CHP_ONLINE:
  1308. if (cio_update_schib(sch))
  1309. return -ENODEV;
  1310. sch->lpm |= mask & sch->opm;
  1311. io_subchannel_verify(sch);
  1312. break;
  1313. }
  1314. return 0;
  1315. }
  1316. static void
  1317. io_subchannel_shutdown(struct subchannel *sch)
  1318. {
  1319. struct ccw_device *cdev;
  1320. int ret;
  1321. cdev = sch_get_cdev(sch);
  1322. if (cio_is_console(sch->schid))
  1323. return;
  1324. if (!sch->schib.pmcw.ena)
  1325. /* Nothing to do. */
  1326. return;
  1327. ret = cio_disable_subchannel(sch);
  1328. if (ret != -EBUSY)
  1329. /* Subchannel is disabled, we're done. */
  1330. return;
  1331. cdev->private->state = DEV_STATE_QUIESCE;
  1332. if (cdev->handler)
  1333. cdev->handler(cdev, cdev->private->intparm,
  1334. ERR_PTR(-EIO));
  1335. ret = ccw_device_cancel_halt_clear(cdev);
  1336. if (ret == -EBUSY) {
  1337. ccw_device_set_timeout(cdev, HZ/10);
  1338. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  1339. }
  1340. cio_disable_subchannel(sch);
  1341. }
  1342. static int io_subchannel_get_status(struct subchannel *sch)
  1343. {
  1344. struct schib schib;
  1345. if (stsch(sch->schid, &schib) || !schib.pmcw.dnv)
  1346. return CIO_GONE;
  1347. if (sch->schib.pmcw.dnv && (schib.pmcw.dev != sch->schib.pmcw.dev))
  1348. return CIO_REVALIDATE;
  1349. if (!sch->lpm)
  1350. return CIO_NO_PATH;
  1351. return CIO_OPER;
  1352. }
  1353. static int device_is_disconnected(struct ccw_device *cdev)
  1354. {
  1355. if (!cdev)
  1356. return 0;
  1357. return (cdev->private->state == DEV_STATE_DISCONNECTED ||
  1358. cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
  1359. }
  1360. static int recovery_check(struct device *dev, void *data)
  1361. {
  1362. struct ccw_device *cdev = to_ccwdev(dev);
  1363. int *redo = data;
  1364. spin_lock_irq(cdev->ccwlock);
  1365. switch (cdev->private->state) {
  1366. case DEV_STATE_DISCONNECTED:
  1367. CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
  1368. cdev->private->dev_id.ssid,
  1369. cdev->private->dev_id.devno);
  1370. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1371. *redo = 1;
  1372. break;
  1373. case DEV_STATE_DISCONNECTED_SENSE_ID:
  1374. *redo = 1;
  1375. break;
  1376. }
  1377. spin_unlock_irq(cdev->ccwlock);
  1378. return 0;
  1379. }
  1380. static void recovery_work_func(struct work_struct *unused)
  1381. {
  1382. int redo = 0;
  1383. bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
  1384. if (redo) {
  1385. spin_lock_irq(&recovery_lock);
  1386. if (!timer_pending(&recovery_timer)) {
  1387. if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
  1388. recovery_phase++;
  1389. mod_timer(&recovery_timer, jiffies +
  1390. recovery_delay[recovery_phase] * HZ);
  1391. }
  1392. spin_unlock_irq(&recovery_lock);
  1393. } else
  1394. CIO_MSG_EVENT(4, "recovery: end\n");
  1395. }
  1396. static DECLARE_WORK(recovery_work, recovery_work_func);
  1397. static void recovery_func(unsigned long data)
  1398. {
  1399. /*
  1400. * We can't do our recovery in softirq context and it's not
  1401. * performance critical, so we schedule it.
  1402. */
  1403. schedule_work(&recovery_work);
  1404. }
  1405. static void ccw_device_schedule_recovery(void)
  1406. {
  1407. unsigned long flags;
  1408. CIO_MSG_EVENT(4, "recovery: schedule\n");
  1409. spin_lock_irqsave(&recovery_lock, flags);
  1410. if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
  1411. recovery_phase = 0;
  1412. mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
  1413. }
  1414. spin_unlock_irqrestore(&recovery_lock, flags);
  1415. }
  1416. static int purge_fn(struct device *dev, void *data)
  1417. {
  1418. struct ccw_device *cdev = to_ccwdev(dev);
  1419. struct ccw_device_private *priv = cdev->private;
  1420. int unreg;
  1421. spin_lock_irq(cdev->ccwlock);
  1422. unreg = is_blacklisted(priv->dev_id.ssid, priv->dev_id.devno) &&
  1423. (priv->state == DEV_STATE_OFFLINE);
  1424. spin_unlock_irq(cdev->ccwlock);
  1425. if (!unreg)
  1426. goto out;
  1427. CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", priv->dev_id.ssid,
  1428. priv->dev_id.devno);
  1429. ccw_device_schedule_sch_unregister(cdev);
  1430. out:
  1431. /* Abort loop in case of pending signal. */
  1432. if (signal_pending(current))
  1433. return -EINTR;
  1434. return 0;
  1435. }
  1436. /**
  1437. * ccw_purge_blacklisted - purge unused, blacklisted devices
  1438. *
  1439. * Unregister all ccw devices that are offline and on the blacklist.
  1440. */
  1441. int ccw_purge_blacklisted(void)
  1442. {
  1443. CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
  1444. bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
  1445. return 0;
  1446. }
  1447. static void device_set_disconnected(struct ccw_device *cdev)
  1448. {
  1449. if (!cdev)
  1450. return;
  1451. ccw_device_set_timeout(cdev, 0);
  1452. cdev->private->flags.fake_irb = 0;
  1453. cdev->private->state = DEV_STATE_DISCONNECTED;
  1454. if (cdev->online)
  1455. ccw_device_schedule_recovery();
  1456. }
  1457. void ccw_device_set_notoper(struct ccw_device *cdev)
  1458. {
  1459. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  1460. CIO_TRACE_EVENT(2, "notoper");
  1461. CIO_TRACE_EVENT(2, dev_name(&sch->dev));
  1462. ccw_device_set_timeout(cdev, 0);
  1463. cio_disable_subchannel(sch);
  1464. cdev->private->state = DEV_STATE_NOT_OPER;
  1465. }
  1466. static int io_subchannel_sch_event(struct subchannel *sch, int slow)
  1467. {
  1468. int event, ret, disc;
  1469. unsigned long flags;
  1470. enum { NONE, UNREGISTER, UNREGISTER_PROBE, REPROBE, DISC } action;
  1471. struct ccw_device *cdev;
  1472. spin_lock_irqsave(sch->lock, flags);
  1473. cdev = sch_get_cdev(sch);
  1474. disc = device_is_disconnected(cdev);
  1475. if (disc && slow) {
  1476. /* Disconnected devices are evaluated directly only.*/
  1477. spin_unlock_irqrestore(sch->lock, flags);
  1478. return 0;
  1479. }
  1480. /* No interrupt after machine check - kill pending timers. */
  1481. if (cdev)
  1482. ccw_device_set_timeout(cdev, 0);
  1483. if (!disc && !slow) {
  1484. /* Non-disconnected devices are evaluated on the slow path. */
  1485. spin_unlock_irqrestore(sch->lock, flags);
  1486. return -EAGAIN;
  1487. }
  1488. event = io_subchannel_get_status(sch);
  1489. CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
  1490. sch->schid.ssid, sch->schid.sch_no, event,
  1491. disc ? "disconnected" : "normal",
  1492. slow ? "slow" : "fast");
  1493. /* Analyze subchannel status. */
  1494. action = NONE;
  1495. switch (event) {
  1496. case CIO_NO_PATH:
  1497. if (disc) {
  1498. /* Check if paths have become available. */
  1499. action = REPROBE;
  1500. break;
  1501. }
  1502. /* fall through */
  1503. case CIO_GONE:
  1504. /* Ask driver what to do with device. */
  1505. if (io_subchannel_notify(sch, event))
  1506. action = DISC;
  1507. else
  1508. action = UNREGISTER;
  1509. break;
  1510. case CIO_REVALIDATE:
  1511. /* Device will be removed, so no notify necessary. */
  1512. if (disc)
  1513. /* Reprobe because immediate unregister might block. */
  1514. action = REPROBE;
  1515. else
  1516. action = UNREGISTER_PROBE;
  1517. break;
  1518. case CIO_OPER:
  1519. if (disc)
  1520. /* Get device operational again. */
  1521. action = REPROBE;
  1522. break;
  1523. }
  1524. /* Perform action. */
  1525. ret = 0;
  1526. switch (action) {
  1527. case UNREGISTER:
  1528. case UNREGISTER_PROBE:
  1529. ccw_device_set_notoper(cdev);
  1530. /* Unregister device (will use subchannel lock). */
  1531. spin_unlock_irqrestore(sch->lock, flags);
  1532. css_sch_device_unregister(sch);
  1533. spin_lock_irqsave(sch->lock, flags);
  1534. break;
  1535. case REPROBE:
  1536. ccw_device_trigger_reprobe(cdev);
  1537. break;
  1538. case DISC:
  1539. device_set_disconnected(cdev);
  1540. break;
  1541. default:
  1542. break;
  1543. }
  1544. spin_unlock_irqrestore(sch->lock, flags);
  1545. /* Probe if necessary. */
  1546. if (action == UNREGISTER_PROBE)
  1547. ret = css_probe_device(sch->schid);
  1548. return ret;
  1549. }
  1550. #ifdef CONFIG_CCW_CONSOLE
  1551. static struct ccw_device console_cdev;
  1552. static char console_cdev_name[10] = "0.x.xxxx";
  1553. static struct ccw_device_private console_private;
  1554. static int console_cdev_in_use;
  1555. static DEFINE_SPINLOCK(ccw_console_lock);
  1556. spinlock_t * cio_get_console_lock(void)
  1557. {
  1558. return &ccw_console_lock;
  1559. }
  1560. static int ccw_device_console_enable(struct ccw_device *cdev,
  1561. struct subchannel *sch)
  1562. {
  1563. int rc;
  1564. /* Attach subchannel private data. */
  1565. sch->private = cio_get_console_priv();
  1566. memset(sch->private, 0, sizeof(struct io_subchannel_private));
  1567. io_subchannel_init_fields(sch);
  1568. rc = cio_commit_config(sch);
  1569. if (rc)
  1570. return rc;
  1571. sch->driver = &io_subchannel_driver;
  1572. /* Initialize the ccw_device structure. */
  1573. cdev->dev.parent= &sch->dev;
  1574. rc = io_subchannel_recog(cdev, sch);
  1575. if (rc)
  1576. return rc;
  1577. /* Now wait for the async. recognition to come to an end. */
  1578. spin_lock_irq(cdev->ccwlock);
  1579. while (!dev_fsm_final_state(cdev))
  1580. wait_cons_dev();
  1581. rc = -EIO;
  1582. if (cdev->private->state != DEV_STATE_OFFLINE)
  1583. goto out_unlock;
  1584. ccw_device_online(cdev);
  1585. while (!dev_fsm_final_state(cdev))
  1586. wait_cons_dev();
  1587. if (cdev->private->state != DEV_STATE_ONLINE)
  1588. goto out_unlock;
  1589. rc = 0;
  1590. out_unlock:
  1591. spin_unlock_irq(cdev->ccwlock);
  1592. return 0;
  1593. }
  1594. struct ccw_device *
  1595. ccw_device_probe_console(void)
  1596. {
  1597. struct subchannel *sch;
  1598. int ret;
  1599. if (xchg(&console_cdev_in_use, 1) != 0)
  1600. return ERR_PTR(-EBUSY);
  1601. sch = cio_probe_console();
  1602. if (IS_ERR(sch)) {
  1603. console_cdev_in_use = 0;
  1604. return (void *) sch;
  1605. }
  1606. memset(&console_cdev, 0, sizeof(struct ccw_device));
  1607. memset(&console_private, 0, sizeof(struct ccw_device_private));
  1608. console_cdev.private = &console_private;
  1609. console_private.cdev = &console_cdev;
  1610. ret = ccw_device_console_enable(&console_cdev, sch);
  1611. if (ret) {
  1612. cio_release_console();
  1613. console_cdev_in_use = 0;
  1614. return ERR_PTR(ret);
  1615. }
  1616. console_cdev.online = 1;
  1617. return &console_cdev;
  1618. }
  1619. static int ccw_device_pm_restore(struct device *dev);
  1620. int ccw_device_force_console(void)
  1621. {
  1622. if (!console_cdev_in_use)
  1623. return -ENODEV;
  1624. return ccw_device_pm_restore(&console_cdev.dev);
  1625. }
  1626. EXPORT_SYMBOL_GPL(ccw_device_force_console);
  1627. const char *cio_get_console_cdev_name(struct subchannel *sch)
  1628. {
  1629. snprintf(console_cdev_name, 10, "0.%x.%04x",
  1630. sch->schid.ssid, sch->schib.pmcw.dev);
  1631. return (const char *)console_cdev_name;
  1632. }
  1633. #endif
  1634. /*
  1635. * get ccw_device matching the busid, but only if owned by cdrv
  1636. */
  1637. static int
  1638. __ccwdev_check_busid(struct device *dev, void *id)
  1639. {
  1640. char *bus_id;
  1641. bus_id = id;
  1642. return (strcmp(bus_id, dev_name(dev)) == 0);
  1643. }
  1644. /**
  1645. * get_ccwdev_by_busid() - obtain device from a bus id
  1646. * @cdrv: driver the device is owned by
  1647. * @bus_id: bus id of the device to be searched
  1648. *
  1649. * This function searches all devices owned by @cdrv for a device with a bus
  1650. * id matching @bus_id.
  1651. * Returns:
  1652. * If a match is found, its reference count of the found device is increased
  1653. * and it is returned; else %NULL is returned.
  1654. */
  1655. struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
  1656. const char *bus_id)
  1657. {
  1658. struct device *dev;
  1659. struct device_driver *drv;
  1660. drv = get_driver(&cdrv->driver);
  1661. if (!drv)
  1662. return NULL;
  1663. dev = driver_find_device(drv, NULL, (void *)bus_id,
  1664. __ccwdev_check_busid);
  1665. put_driver(drv);
  1666. return dev ? to_ccwdev(dev) : NULL;
  1667. }
  1668. /************************** device driver handling ************************/
  1669. /* This is the implementation of the ccw_driver class. The probe, remove
  1670. * and release methods are initially very similar to the device_driver
  1671. * implementations, with the difference that they have ccw_device
  1672. * arguments.
  1673. *
  1674. * A ccw driver also contains the information that is needed for
  1675. * device matching.
  1676. */
  1677. static int
  1678. ccw_device_probe (struct device *dev)
  1679. {
  1680. struct ccw_device *cdev = to_ccwdev(dev);
  1681. struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
  1682. int ret;
  1683. cdev->drv = cdrv; /* to let the driver call _set_online */
  1684. ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
  1685. if (ret) {
  1686. cdev->drv = NULL;
  1687. return ret;
  1688. }
  1689. return 0;
  1690. }
  1691. static int
  1692. ccw_device_remove (struct device *dev)
  1693. {
  1694. struct ccw_device *cdev = to_ccwdev(dev);
  1695. struct ccw_driver *cdrv = cdev->drv;
  1696. int ret;
  1697. if (cdrv->remove)
  1698. cdrv->remove(cdev);
  1699. if (cdev->online) {
  1700. cdev->online = 0;
  1701. spin_lock_irq(cdev->ccwlock);
  1702. ret = ccw_device_offline(cdev);
  1703. spin_unlock_irq(cdev->ccwlock);
  1704. if (ret == 0)
  1705. wait_event(cdev->private->wait_q,
  1706. dev_fsm_final_state(cdev));
  1707. else
  1708. CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
  1709. "device 0.%x.%04x\n",
  1710. ret, cdev->private->dev_id.ssid,
  1711. cdev->private->dev_id.devno);
  1712. /* Give up reference obtained in ccw_device_set_online(). */
  1713. put_device(&cdev->dev);
  1714. }
  1715. ccw_device_set_timeout(cdev, 0);
  1716. cdev->drv = NULL;
  1717. return 0;
  1718. }
  1719. static void ccw_device_shutdown(struct device *dev)
  1720. {
  1721. struct ccw_device *cdev;
  1722. cdev = to_ccwdev(dev);
  1723. if (cdev->drv && cdev->drv->shutdown)
  1724. cdev->drv->shutdown(cdev);
  1725. disable_cmf(cdev);
  1726. }
  1727. static int ccw_device_pm_prepare(struct device *dev)
  1728. {
  1729. struct ccw_device *cdev = to_ccwdev(dev);
  1730. if (work_pending(&cdev->private->kick_work))
  1731. return -EAGAIN;
  1732. /* Fail while device is being set online/offline. */
  1733. if (atomic_read(&cdev->private->onoff))
  1734. return -EAGAIN;
  1735. if (cdev->online && cdev->drv && cdev->drv->prepare)
  1736. return cdev->drv->prepare(cdev);
  1737. return 0;
  1738. }
  1739. static void ccw_device_pm_complete(struct device *dev)
  1740. {
  1741. struct ccw_device *cdev = to_ccwdev(dev);
  1742. if (cdev->online && cdev->drv && cdev->drv->complete)
  1743. cdev->drv->complete(cdev);
  1744. }
  1745. static int ccw_device_pm_freeze(struct device *dev)
  1746. {
  1747. struct ccw_device *cdev = to_ccwdev(dev);
  1748. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  1749. int ret, cm_enabled;
  1750. /* Fail suspend while device is in transistional state. */
  1751. if (!dev_fsm_final_state(cdev))
  1752. return -EAGAIN;
  1753. if (!cdev->online)
  1754. return 0;
  1755. if (cdev->drv && cdev->drv->freeze) {
  1756. ret = cdev->drv->freeze(cdev);
  1757. if (ret)
  1758. return ret;
  1759. }
  1760. spin_lock_irq(sch->lock);
  1761. cm_enabled = cdev->private->cmb != NULL;
  1762. spin_unlock_irq(sch->lock);
  1763. if (cm_enabled) {
  1764. /* Don't have the css write on memory. */
  1765. ret = ccw_set_cmf(cdev, 0);
  1766. if (ret)
  1767. return ret;
  1768. }
  1769. /* From here on, disallow device driver I/O. */
  1770. spin_lock_irq(sch->lock);
  1771. ret = cio_disable_subchannel(sch);
  1772. spin_unlock_irq(sch->lock);
  1773. return ret;
  1774. }
  1775. static int ccw_device_pm_thaw(struct device *dev)
  1776. {
  1777. struct ccw_device *cdev = to_ccwdev(dev);
  1778. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  1779. int ret, cm_enabled;
  1780. if (!cdev->online)
  1781. return 0;
  1782. spin_lock_irq(sch->lock);
  1783. /* Allow device driver I/O again. */
  1784. ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
  1785. cm_enabled = cdev->private->cmb != NULL;
  1786. spin_unlock_irq(sch->lock);
  1787. if (ret)
  1788. return ret;
  1789. if (cm_enabled) {
  1790. ret = ccw_set_cmf(cdev, 1);
  1791. if (ret)
  1792. return ret;
  1793. }
  1794. if (cdev->drv && cdev->drv->thaw)
  1795. ret = cdev->drv->thaw(cdev);
  1796. return ret;
  1797. }
  1798. static void __ccw_device_pm_restore(struct ccw_device *cdev)
  1799. {
  1800. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  1801. int ret;
  1802. if (cio_is_console(sch->schid))
  1803. goto out;
  1804. /*
  1805. * While we were sleeping, devices may have gone or become
  1806. * available again. Kick re-detection.
  1807. */
  1808. spin_lock_irq(sch->lock);
  1809. cdev->private->flags.resuming = 1;
  1810. ret = ccw_device_recognition(cdev);
  1811. spin_unlock_irq(sch->lock);
  1812. if (ret) {
  1813. CIO_MSG_EVENT(0, "Couldn't start recognition for device "
  1814. "0.%x.%04x (ret=%d)\n",
  1815. cdev->private->dev_id.ssid,
  1816. cdev->private->dev_id.devno, ret);
  1817. spin_lock_irq(sch->lock);
  1818. cdev->private->state = DEV_STATE_DISCONNECTED;
  1819. spin_unlock_irq(sch->lock);
  1820. /* notify driver after the resume cb */
  1821. goto out;
  1822. }
  1823. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev) ||
  1824. cdev->private->state == DEV_STATE_DISCONNECTED);
  1825. out:
  1826. cdev->private->flags.resuming = 0;
  1827. }
  1828. static int resume_handle_boxed(struct ccw_device *cdev)
  1829. {
  1830. cdev->private->state = DEV_STATE_BOXED;
  1831. if (ccw_device_notify(cdev, CIO_BOXED))
  1832. return 0;
  1833. ccw_device_schedule_sch_unregister(cdev);
  1834. return -ENODEV;
  1835. }
  1836. static int resume_handle_disc(struct ccw_device *cdev)
  1837. {
  1838. cdev->private->state = DEV_STATE_DISCONNECTED;
  1839. if (ccw_device_notify(cdev, CIO_GONE))
  1840. return 0;
  1841. ccw_device_schedule_sch_unregister(cdev);
  1842. return -ENODEV;
  1843. }
  1844. static int ccw_device_pm_restore(struct device *dev)
  1845. {
  1846. struct ccw_device *cdev = to_ccwdev(dev);
  1847. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  1848. int ret = 0, cm_enabled;
  1849. __ccw_device_pm_restore(cdev);
  1850. spin_lock_irq(sch->lock);
  1851. if (cio_is_console(sch->schid)) {
  1852. cio_enable_subchannel(sch, (u32)(addr_t)sch);
  1853. spin_unlock_irq(sch->lock);
  1854. goto out_restore;
  1855. }
  1856. cdev->private->flags.donotify = 0;
  1857. /* check recognition results */
  1858. switch (cdev->private->state) {
  1859. case DEV_STATE_OFFLINE:
  1860. break;
  1861. case DEV_STATE_BOXED:
  1862. ret = resume_handle_boxed(cdev);
  1863. spin_unlock_irq(sch->lock);
  1864. if (ret)
  1865. goto out;
  1866. goto out_restore;
  1867. case DEV_STATE_DISCONNECTED:
  1868. goto out_disc_unlock;
  1869. default:
  1870. goto out_unreg_unlock;
  1871. }
  1872. /* check if the device id has changed */
  1873. if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
  1874. CIO_MSG_EVENT(0, "resume: sch 0.%x.%04x: failed (devno "
  1875. "changed from %04x to %04x)\n",
  1876. sch->schid.ssid, sch->schid.sch_no,
  1877. cdev->private->dev_id.devno,
  1878. sch->schib.pmcw.dev);
  1879. goto out_unreg_unlock;
  1880. }
  1881. /* check if the device type has changed */
  1882. if (!ccw_device_test_sense_data(cdev)) {
  1883. ccw_device_update_sense_data(cdev);
  1884. PREPARE_WORK(&cdev->private->kick_work,
  1885. ccw_device_do_unbind_bind);
  1886. queue_work(ccw_device_work, &cdev->private->kick_work);
  1887. ret = -ENODEV;
  1888. goto out_unlock;
  1889. }
  1890. if (!cdev->online) {
  1891. ret = 0;
  1892. goto out_unlock;
  1893. }
  1894. ret = ccw_device_online(cdev);
  1895. if (ret)
  1896. goto out_disc_unlock;
  1897. cm_enabled = cdev->private->cmb != NULL;
  1898. spin_unlock_irq(sch->lock);
  1899. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  1900. if (cdev->private->state != DEV_STATE_ONLINE) {
  1901. spin_lock_irq(sch->lock);
  1902. goto out_disc_unlock;
  1903. }
  1904. if (cm_enabled) {
  1905. ret = ccw_set_cmf(cdev, 1);
  1906. if (ret) {
  1907. CIO_MSG_EVENT(2, "resume: cdev 0.%x.%04x: cmf failed "
  1908. "(rc=%d)\n", cdev->private->dev_id.ssid,
  1909. cdev->private->dev_id.devno, ret);
  1910. ret = 0;
  1911. }
  1912. }
  1913. out_restore:
  1914. if (cdev->online && cdev->drv && cdev->drv->restore)
  1915. ret = cdev->drv->restore(cdev);
  1916. out:
  1917. return ret;
  1918. out_disc_unlock:
  1919. ret = resume_handle_disc(cdev);
  1920. spin_unlock_irq(sch->lock);
  1921. if (ret)
  1922. return ret;
  1923. goto out_restore;
  1924. out_unreg_unlock:
  1925. ccw_device_schedule_sch_unregister(cdev);
  1926. ret = -ENODEV;
  1927. out_unlock:
  1928. spin_unlock_irq(sch->lock);
  1929. return ret;
  1930. }
  1931. static struct dev_pm_ops ccw_pm_ops = {
  1932. .prepare = ccw_device_pm_prepare,
  1933. .complete = ccw_device_pm_complete,
  1934. .freeze = ccw_device_pm_freeze,
  1935. .thaw = ccw_device_pm_thaw,
  1936. .restore = ccw_device_pm_restore,
  1937. };
  1938. struct bus_type ccw_bus_type = {
  1939. .name = "ccw",
  1940. .match = ccw_bus_match,
  1941. .uevent = ccw_uevent,
  1942. .probe = ccw_device_probe,
  1943. .remove = ccw_device_remove,
  1944. .shutdown = ccw_device_shutdown,
  1945. .pm = &ccw_pm_ops,
  1946. };
  1947. /**
  1948. * ccw_driver_register() - register a ccw driver
  1949. * @cdriver: driver to be registered
  1950. *
  1951. * This function is mainly a wrapper around driver_register().
  1952. * Returns:
  1953. * %0 on success and a negative error value on failure.
  1954. */
  1955. int ccw_driver_register(struct ccw_driver *cdriver)
  1956. {
  1957. struct device_driver *drv = &cdriver->driver;
  1958. drv->bus = &ccw_bus_type;
  1959. drv->name = cdriver->name;
  1960. drv->owner = cdriver->owner;
  1961. return driver_register(drv);
  1962. }
  1963. /**
  1964. * ccw_driver_unregister() - deregister a ccw driver
  1965. * @cdriver: driver to be deregistered
  1966. *
  1967. * This function is mainly a wrapper around driver_unregister().
  1968. */
  1969. void ccw_driver_unregister(struct ccw_driver *cdriver)
  1970. {
  1971. driver_unregister(&cdriver->driver);
  1972. }
  1973. /* Helper func for qdio. */
  1974. struct subchannel_id
  1975. ccw_device_get_subchannel_id(struct ccw_device *cdev)
  1976. {
  1977. struct subchannel *sch;
  1978. sch = to_subchannel(cdev->dev.parent);
  1979. return sch->schid;
  1980. }
  1981. MODULE_LICENSE("GPL");
  1982. EXPORT_SYMBOL(ccw_device_set_online);
  1983. EXPORT_SYMBOL(ccw_device_set_offline);
  1984. EXPORT_SYMBOL(ccw_driver_register);
  1985. EXPORT_SYMBOL(ccw_driver_unregister);
  1986. EXPORT_SYMBOL(get_ccwdev_by_busid);
  1987. EXPORT_SYMBOL(ccw_bus_type);
  1988. EXPORT_SYMBOL(ccw_device_work);
  1989. EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);