device.c 56 KB

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