device.c 50 KB

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