device.c 55 KB

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