device.c 50 KB

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