device.c 50 KB

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