device.c 56 KB

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