device.c 57 KB

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