virtio_ccw.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425
  1. /*
  2. * ccw based virtio transport
  3. *
  4. * Copyright IBM Corp. 2012, 2014
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License (version 2 only)
  8. * as published by the Free Software Foundation.
  9. *
  10. * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
  11. */
  12. #include <linux/kernel_stat.h>
  13. #include <linux/init.h>
  14. #include <linux/bootmem.h>
  15. #include <linux/err.h>
  16. #include <linux/virtio.h>
  17. #include <linux/virtio_config.h>
  18. #include <linux/slab.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/virtio_ring.h>
  21. #include <linux/pfn.h>
  22. #include <linux/async.h>
  23. #include <linux/wait.h>
  24. #include <linux/list.h>
  25. #include <linux/bitops.h>
  26. #include <linux/moduleparam.h>
  27. #include <linux/io.h>
  28. #include <linux/kvm_para.h>
  29. #include <linux/notifier.h>
  30. #include <asm/diag.h>
  31. #include <asm/setup.h>
  32. #include <asm/irq.h>
  33. #include <asm/cio.h>
  34. #include <asm/ccwdev.h>
  35. #include <asm/virtio-ccw.h>
  36. #include <asm/isc.h>
  37. #include <asm/airq.h>
  38. /*
  39. * virtio related functions
  40. */
  41. struct vq_config_block {
  42. __u16 index;
  43. __u16 num;
  44. } __packed;
  45. #define VIRTIO_CCW_CONFIG_SIZE 0x100
  46. /* same as PCI config space size, should be enough for all drivers */
  47. struct virtio_ccw_device {
  48. struct virtio_device vdev;
  49. __u8 *status;
  50. __u8 config[VIRTIO_CCW_CONFIG_SIZE];
  51. struct ccw_device *cdev;
  52. __u32 curr_io;
  53. int err;
  54. unsigned int revision; /* Transport revision */
  55. wait_queue_head_t wait_q;
  56. spinlock_t lock;
  57. struct list_head virtqueues;
  58. unsigned long indicators;
  59. unsigned long indicators2;
  60. struct vq_config_block *config_block;
  61. bool is_thinint;
  62. bool going_away;
  63. bool device_lost;
  64. unsigned int config_ready;
  65. void *airq_info;
  66. };
  67. struct vq_info_block_legacy {
  68. __u64 queue;
  69. __u32 align;
  70. __u16 index;
  71. __u16 num;
  72. } __packed;
  73. struct vq_info_block {
  74. __u64 desc;
  75. __u32 res0;
  76. __u16 index;
  77. __u16 num;
  78. __u64 avail;
  79. __u64 used;
  80. } __packed;
  81. struct virtio_feature_desc {
  82. __le32 features;
  83. __u8 index;
  84. } __packed;
  85. struct virtio_thinint_area {
  86. unsigned long summary_indicator;
  87. unsigned long indicator;
  88. u64 bit_nr;
  89. u8 isc;
  90. } __packed;
  91. struct virtio_rev_info {
  92. __u16 revision;
  93. __u16 length;
  94. __u8 data[];
  95. };
  96. /* the highest virtio-ccw revision we support */
  97. #define VIRTIO_CCW_REV_MAX 1
  98. struct virtio_ccw_vq_info {
  99. struct virtqueue *vq;
  100. int num;
  101. void *queue;
  102. union {
  103. struct vq_info_block s;
  104. struct vq_info_block_legacy l;
  105. } *info_block;
  106. int bit_nr;
  107. struct list_head node;
  108. long cookie;
  109. };
  110. #define VIRTIO_AIRQ_ISC IO_SCH_ISC /* inherit from subchannel */
  111. #define VIRTIO_IV_BITS (L1_CACHE_BYTES * 8)
  112. #define MAX_AIRQ_AREAS 20
  113. static int virtio_ccw_use_airq = 1;
  114. struct airq_info {
  115. rwlock_t lock;
  116. u8 summary_indicator;
  117. struct airq_struct airq;
  118. struct airq_iv *aiv;
  119. };
  120. static struct airq_info *airq_areas[MAX_AIRQ_AREAS];
  121. #define CCW_CMD_SET_VQ 0x13
  122. #define CCW_CMD_VDEV_RESET 0x33
  123. #define CCW_CMD_SET_IND 0x43
  124. #define CCW_CMD_SET_CONF_IND 0x53
  125. #define CCW_CMD_READ_FEAT 0x12
  126. #define CCW_CMD_WRITE_FEAT 0x11
  127. #define CCW_CMD_READ_CONF 0x22
  128. #define CCW_CMD_WRITE_CONF 0x21
  129. #define CCW_CMD_WRITE_STATUS 0x31
  130. #define CCW_CMD_READ_VQ_CONF 0x32
  131. #define CCW_CMD_READ_STATUS 0x72
  132. #define CCW_CMD_SET_IND_ADAPTER 0x73
  133. #define CCW_CMD_SET_VIRTIO_REV 0x83
  134. #define VIRTIO_CCW_DOING_SET_VQ 0x00010000
  135. #define VIRTIO_CCW_DOING_RESET 0x00040000
  136. #define VIRTIO_CCW_DOING_READ_FEAT 0x00080000
  137. #define VIRTIO_CCW_DOING_WRITE_FEAT 0x00100000
  138. #define VIRTIO_CCW_DOING_READ_CONFIG 0x00200000
  139. #define VIRTIO_CCW_DOING_WRITE_CONFIG 0x00400000
  140. #define VIRTIO_CCW_DOING_WRITE_STATUS 0x00800000
  141. #define VIRTIO_CCW_DOING_SET_IND 0x01000000
  142. #define VIRTIO_CCW_DOING_READ_VQ_CONF 0x02000000
  143. #define VIRTIO_CCW_DOING_SET_CONF_IND 0x04000000
  144. #define VIRTIO_CCW_DOING_SET_IND_ADAPTER 0x08000000
  145. #define VIRTIO_CCW_DOING_SET_VIRTIO_REV 0x10000000
  146. #define VIRTIO_CCW_DOING_READ_STATUS 0x20000000
  147. #define VIRTIO_CCW_INTPARM_MASK 0xffff0000
  148. static struct virtio_ccw_device *to_vc_device(struct virtio_device *vdev)
  149. {
  150. return container_of(vdev, struct virtio_ccw_device, vdev);
  151. }
  152. static void drop_airq_indicator(struct virtqueue *vq, struct airq_info *info)
  153. {
  154. unsigned long i, flags;
  155. write_lock_irqsave(&info->lock, flags);
  156. for (i = 0; i < airq_iv_end(info->aiv); i++) {
  157. if (vq == (void *)airq_iv_get_ptr(info->aiv, i)) {
  158. airq_iv_free_bit(info->aiv, i);
  159. airq_iv_set_ptr(info->aiv, i, 0);
  160. break;
  161. }
  162. }
  163. write_unlock_irqrestore(&info->lock, flags);
  164. }
  165. static void virtio_airq_handler(struct airq_struct *airq)
  166. {
  167. struct airq_info *info = container_of(airq, struct airq_info, airq);
  168. unsigned long ai;
  169. inc_irq_stat(IRQIO_VAI);
  170. read_lock(&info->lock);
  171. /* Walk through indicators field, summary indicator active. */
  172. for (ai = 0;;) {
  173. ai = airq_iv_scan(info->aiv, ai, airq_iv_end(info->aiv));
  174. if (ai == -1UL)
  175. break;
  176. vring_interrupt(0, (void *)airq_iv_get_ptr(info->aiv, ai));
  177. }
  178. info->summary_indicator = 0;
  179. smp_wmb();
  180. /* Walk through indicators field, summary indicator not active. */
  181. for (ai = 0;;) {
  182. ai = airq_iv_scan(info->aiv, ai, airq_iv_end(info->aiv));
  183. if (ai == -1UL)
  184. break;
  185. vring_interrupt(0, (void *)airq_iv_get_ptr(info->aiv, ai));
  186. }
  187. read_unlock(&info->lock);
  188. }
  189. static struct airq_info *new_airq_info(void)
  190. {
  191. struct airq_info *info;
  192. int rc;
  193. info = kzalloc(sizeof(*info), GFP_KERNEL);
  194. if (!info)
  195. return NULL;
  196. rwlock_init(&info->lock);
  197. info->aiv = airq_iv_create(VIRTIO_IV_BITS, AIRQ_IV_ALLOC | AIRQ_IV_PTR);
  198. if (!info->aiv) {
  199. kfree(info);
  200. return NULL;
  201. }
  202. info->airq.handler = virtio_airq_handler;
  203. info->airq.lsi_ptr = &info->summary_indicator;
  204. info->airq.lsi_mask = 0xff;
  205. info->airq.isc = VIRTIO_AIRQ_ISC;
  206. rc = register_adapter_interrupt(&info->airq);
  207. if (rc) {
  208. airq_iv_release(info->aiv);
  209. kfree(info);
  210. return NULL;
  211. }
  212. return info;
  213. }
  214. static unsigned long get_airq_indicator(struct virtqueue *vqs[], int nvqs,
  215. u64 *first, void **airq_info)
  216. {
  217. int i, j;
  218. struct airq_info *info;
  219. unsigned long indicator_addr = 0;
  220. unsigned long bit, flags;
  221. for (i = 0; i < MAX_AIRQ_AREAS && !indicator_addr; i++) {
  222. if (!airq_areas[i])
  223. airq_areas[i] = new_airq_info();
  224. info = airq_areas[i];
  225. if (!info)
  226. return 0;
  227. write_lock_irqsave(&info->lock, flags);
  228. bit = airq_iv_alloc(info->aiv, nvqs);
  229. if (bit == -1UL) {
  230. /* Not enough vacancies. */
  231. write_unlock_irqrestore(&info->lock, flags);
  232. continue;
  233. }
  234. *first = bit;
  235. *airq_info = info;
  236. indicator_addr = (unsigned long)info->aiv->vector;
  237. for (j = 0; j < nvqs; j++) {
  238. airq_iv_set_ptr(info->aiv, bit + j,
  239. (unsigned long)vqs[j]);
  240. }
  241. write_unlock_irqrestore(&info->lock, flags);
  242. }
  243. return indicator_addr;
  244. }
  245. static void virtio_ccw_drop_indicators(struct virtio_ccw_device *vcdev)
  246. {
  247. struct virtio_ccw_vq_info *info;
  248. list_for_each_entry(info, &vcdev->virtqueues, node)
  249. drop_airq_indicator(info->vq, vcdev->airq_info);
  250. }
  251. static int doing_io(struct virtio_ccw_device *vcdev, __u32 flag)
  252. {
  253. unsigned long flags;
  254. __u32 ret;
  255. spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags);
  256. if (vcdev->err)
  257. ret = 0;
  258. else
  259. ret = vcdev->curr_io & flag;
  260. spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags);
  261. return ret;
  262. }
  263. static int ccw_io_helper(struct virtio_ccw_device *vcdev,
  264. struct ccw1 *ccw, __u32 intparm)
  265. {
  266. int ret;
  267. unsigned long flags;
  268. int flag = intparm & VIRTIO_CCW_INTPARM_MASK;
  269. do {
  270. spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags);
  271. ret = ccw_device_start(vcdev->cdev, ccw, intparm, 0, 0);
  272. if (!ret) {
  273. if (!vcdev->curr_io)
  274. vcdev->err = 0;
  275. vcdev->curr_io |= flag;
  276. }
  277. spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags);
  278. cpu_relax();
  279. } while (ret == -EBUSY);
  280. wait_event(vcdev->wait_q, doing_io(vcdev, flag) == 0);
  281. return ret ? ret : vcdev->err;
  282. }
  283. static void virtio_ccw_drop_indicator(struct virtio_ccw_device *vcdev,
  284. struct ccw1 *ccw)
  285. {
  286. int ret;
  287. unsigned long *indicatorp = NULL;
  288. struct virtio_thinint_area *thinint_area = NULL;
  289. struct airq_info *airq_info = vcdev->airq_info;
  290. if (vcdev->is_thinint) {
  291. thinint_area = kzalloc(sizeof(*thinint_area),
  292. GFP_DMA | GFP_KERNEL);
  293. if (!thinint_area)
  294. return;
  295. thinint_area->summary_indicator =
  296. (unsigned long) &airq_info->summary_indicator;
  297. thinint_area->isc = VIRTIO_AIRQ_ISC;
  298. ccw->cmd_code = CCW_CMD_SET_IND_ADAPTER;
  299. ccw->count = sizeof(*thinint_area);
  300. ccw->cda = (__u32)(unsigned long) thinint_area;
  301. } else {
  302. /* payload is the address of the indicators */
  303. indicatorp = kmalloc(sizeof(&vcdev->indicators),
  304. GFP_DMA | GFP_KERNEL);
  305. if (!indicatorp)
  306. return;
  307. *indicatorp = 0;
  308. ccw->cmd_code = CCW_CMD_SET_IND;
  309. ccw->count = sizeof(&vcdev->indicators);
  310. ccw->cda = (__u32)(unsigned long) indicatorp;
  311. }
  312. /* Deregister indicators from host. */
  313. vcdev->indicators = 0;
  314. ccw->flags = 0;
  315. ret = ccw_io_helper(vcdev, ccw,
  316. vcdev->is_thinint ?
  317. VIRTIO_CCW_DOING_SET_IND_ADAPTER :
  318. VIRTIO_CCW_DOING_SET_IND);
  319. if (ret && (ret != -ENODEV))
  320. dev_info(&vcdev->cdev->dev,
  321. "Failed to deregister indicators (%d)\n", ret);
  322. else if (vcdev->is_thinint)
  323. virtio_ccw_drop_indicators(vcdev);
  324. kfree(indicatorp);
  325. kfree(thinint_area);
  326. }
  327. static inline long __do_kvm_notify(struct subchannel_id schid,
  328. unsigned long queue_index,
  329. long cookie)
  330. {
  331. register unsigned long __nr asm("1") = KVM_S390_VIRTIO_CCW_NOTIFY;
  332. register struct subchannel_id __schid asm("2") = schid;
  333. register unsigned long __index asm("3") = queue_index;
  334. register long __rc asm("2");
  335. register long __cookie asm("4") = cookie;
  336. asm volatile ("diag 2,4,0x500\n"
  337. : "=d" (__rc) : "d" (__nr), "d" (__schid), "d" (__index),
  338. "d"(__cookie)
  339. : "memory", "cc");
  340. return __rc;
  341. }
  342. static inline long do_kvm_notify(struct subchannel_id schid,
  343. unsigned long queue_index,
  344. long cookie)
  345. {
  346. diag_stat_inc(DIAG_STAT_X500);
  347. return __do_kvm_notify(schid, queue_index, cookie);
  348. }
  349. static bool virtio_ccw_kvm_notify(struct virtqueue *vq)
  350. {
  351. struct virtio_ccw_vq_info *info = vq->priv;
  352. struct virtio_ccw_device *vcdev;
  353. struct subchannel_id schid;
  354. vcdev = to_vc_device(info->vq->vdev);
  355. ccw_device_get_schid(vcdev->cdev, &schid);
  356. info->cookie = do_kvm_notify(schid, vq->index, info->cookie);
  357. if (info->cookie < 0)
  358. return false;
  359. return true;
  360. }
  361. static int virtio_ccw_read_vq_conf(struct virtio_ccw_device *vcdev,
  362. struct ccw1 *ccw, int index)
  363. {
  364. int ret;
  365. vcdev->config_block->index = index;
  366. ccw->cmd_code = CCW_CMD_READ_VQ_CONF;
  367. ccw->flags = 0;
  368. ccw->count = sizeof(struct vq_config_block);
  369. ccw->cda = (__u32)(unsigned long)(vcdev->config_block);
  370. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_VQ_CONF);
  371. if (ret)
  372. return ret;
  373. return vcdev->config_block->num;
  374. }
  375. static void virtio_ccw_del_vq(struct virtqueue *vq, struct ccw1 *ccw)
  376. {
  377. struct virtio_ccw_device *vcdev = to_vc_device(vq->vdev);
  378. struct virtio_ccw_vq_info *info = vq->priv;
  379. unsigned long flags;
  380. unsigned long size;
  381. int ret;
  382. unsigned int index = vq->index;
  383. /* Remove from our list. */
  384. spin_lock_irqsave(&vcdev->lock, flags);
  385. list_del(&info->node);
  386. spin_unlock_irqrestore(&vcdev->lock, flags);
  387. /* Release from host. */
  388. if (vcdev->revision == 0) {
  389. info->info_block->l.queue = 0;
  390. info->info_block->l.align = 0;
  391. info->info_block->l.index = index;
  392. info->info_block->l.num = 0;
  393. ccw->count = sizeof(info->info_block->l);
  394. } else {
  395. info->info_block->s.desc = 0;
  396. info->info_block->s.index = index;
  397. info->info_block->s.num = 0;
  398. info->info_block->s.avail = 0;
  399. info->info_block->s.used = 0;
  400. ccw->count = sizeof(info->info_block->s);
  401. }
  402. ccw->cmd_code = CCW_CMD_SET_VQ;
  403. ccw->flags = 0;
  404. ccw->cda = (__u32)(unsigned long)(info->info_block);
  405. ret = ccw_io_helper(vcdev, ccw,
  406. VIRTIO_CCW_DOING_SET_VQ | index);
  407. /*
  408. * -ENODEV isn't considered an error: The device is gone anyway.
  409. * This may happen on device detach.
  410. */
  411. if (ret && (ret != -ENODEV))
  412. dev_warn(&vq->vdev->dev, "Error %d while deleting queue %d\n",
  413. ret, index);
  414. vring_del_virtqueue(vq);
  415. size = PAGE_ALIGN(vring_size(info->num, KVM_VIRTIO_CCW_RING_ALIGN));
  416. free_pages_exact(info->queue, size);
  417. kfree(info->info_block);
  418. kfree(info);
  419. }
  420. static void virtio_ccw_del_vqs(struct virtio_device *vdev)
  421. {
  422. struct virtqueue *vq, *n;
  423. struct ccw1 *ccw;
  424. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  425. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  426. if (!ccw)
  427. return;
  428. virtio_ccw_drop_indicator(vcdev, ccw);
  429. list_for_each_entry_safe(vq, n, &vdev->vqs, list)
  430. virtio_ccw_del_vq(vq, ccw);
  431. kfree(ccw);
  432. }
  433. static struct virtqueue *virtio_ccw_setup_vq(struct virtio_device *vdev,
  434. int i, vq_callback_t *callback,
  435. const char *name, bool ctx,
  436. struct ccw1 *ccw)
  437. {
  438. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  439. int err;
  440. struct virtqueue *vq = NULL;
  441. struct virtio_ccw_vq_info *info;
  442. unsigned long size = 0; /* silence the compiler */
  443. unsigned long flags;
  444. /* Allocate queue. */
  445. info = kzalloc(sizeof(struct virtio_ccw_vq_info), GFP_KERNEL);
  446. if (!info) {
  447. dev_warn(&vcdev->cdev->dev, "no info\n");
  448. err = -ENOMEM;
  449. goto out_err;
  450. }
  451. info->info_block = kzalloc(sizeof(*info->info_block),
  452. GFP_DMA | GFP_KERNEL);
  453. if (!info->info_block) {
  454. dev_warn(&vcdev->cdev->dev, "no info block\n");
  455. err = -ENOMEM;
  456. goto out_err;
  457. }
  458. info->num = virtio_ccw_read_vq_conf(vcdev, ccw, i);
  459. if (info->num < 0) {
  460. err = info->num;
  461. goto out_err;
  462. }
  463. size = PAGE_ALIGN(vring_size(info->num, KVM_VIRTIO_CCW_RING_ALIGN));
  464. info->queue = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO);
  465. if (info->queue == NULL) {
  466. dev_warn(&vcdev->cdev->dev, "no queue\n");
  467. err = -ENOMEM;
  468. goto out_err;
  469. }
  470. vq = vring_new_virtqueue(i, info->num, KVM_VIRTIO_CCW_RING_ALIGN, vdev,
  471. true, ctx, info->queue, virtio_ccw_kvm_notify,
  472. callback, name);
  473. if (!vq) {
  474. /* For now, we fail if we can't get the requested size. */
  475. dev_warn(&vcdev->cdev->dev, "no vq\n");
  476. err = -ENOMEM;
  477. goto out_err;
  478. }
  479. /* Register it with the host. */
  480. if (vcdev->revision == 0) {
  481. info->info_block->l.queue = (__u64)info->queue;
  482. info->info_block->l.align = KVM_VIRTIO_CCW_RING_ALIGN;
  483. info->info_block->l.index = i;
  484. info->info_block->l.num = info->num;
  485. ccw->count = sizeof(info->info_block->l);
  486. } else {
  487. info->info_block->s.desc = (__u64)info->queue;
  488. info->info_block->s.index = i;
  489. info->info_block->s.num = info->num;
  490. info->info_block->s.avail = (__u64)virtqueue_get_avail(vq);
  491. info->info_block->s.used = (__u64)virtqueue_get_used(vq);
  492. ccw->count = sizeof(info->info_block->s);
  493. }
  494. ccw->cmd_code = CCW_CMD_SET_VQ;
  495. ccw->flags = 0;
  496. ccw->cda = (__u32)(unsigned long)(info->info_block);
  497. err = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_VQ | i);
  498. if (err) {
  499. dev_warn(&vcdev->cdev->dev, "SET_VQ failed\n");
  500. goto out_err;
  501. }
  502. info->vq = vq;
  503. vq->priv = info;
  504. /* Save it to our list. */
  505. spin_lock_irqsave(&vcdev->lock, flags);
  506. list_add(&info->node, &vcdev->virtqueues);
  507. spin_unlock_irqrestore(&vcdev->lock, flags);
  508. return vq;
  509. out_err:
  510. if (vq)
  511. vring_del_virtqueue(vq);
  512. if (info) {
  513. if (info->queue)
  514. free_pages_exact(info->queue, size);
  515. kfree(info->info_block);
  516. }
  517. kfree(info);
  518. return ERR_PTR(err);
  519. }
  520. static int virtio_ccw_register_adapter_ind(struct virtio_ccw_device *vcdev,
  521. struct virtqueue *vqs[], int nvqs,
  522. struct ccw1 *ccw)
  523. {
  524. int ret;
  525. struct virtio_thinint_area *thinint_area = NULL;
  526. struct airq_info *info;
  527. thinint_area = kzalloc(sizeof(*thinint_area), GFP_DMA | GFP_KERNEL);
  528. if (!thinint_area) {
  529. ret = -ENOMEM;
  530. goto out;
  531. }
  532. /* Try to get an indicator. */
  533. thinint_area->indicator = get_airq_indicator(vqs, nvqs,
  534. &thinint_area->bit_nr,
  535. &vcdev->airq_info);
  536. if (!thinint_area->indicator) {
  537. ret = -ENOSPC;
  538. goto out;
  539. }
  540. info = vcdev->airq_info;
  541. thinint_area->summary_indicator =
  542. (unsigned long) &info->summary_indicator;
  543. thinint_area->isc = VIRTIO_AIRQ_ISC;
  544. ccw->cmd_code = CCW_CMD_SET_IND_ADAPTER;
  545. ccw->flags = CCW_FLAG_SLI;
  546. ccw->count = sizeof(*thinint_area);
  547. ccw->cda = (__u32)(unsigned long)thinint_area;
  548. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND_ADAPTER);
  549. if (ret) {
  550. if (ret == -EOPNOTSUPP) {
  551. /*
  552. * The host does not support adapter interrupts
  553. * for virtio-ccw, stop trying.
  554. */
  555. virtio_ccw_use_airq = 0;
  556. pr_info("Adapter interrupts unsupported on host\n");
  557. } else
  558. dev_warn(&vcdev->cdev->dev,
  559. "enabling adapter interrupts = %d\n", ret);
  560. virtio_ccw_drop_indicators(vcdev);
  561. }
  562. out:
  563. kfree(thinint_area);
  564. return ret;
  565. }
  566. static int virtio_ccw_find_vqs(struct virtio_device *vdev, unsigned nvqs,
  567. struct virtqueue *vqs[],
  568. vq_callback_t *callbacks[],
  569. const char * const names[],
  570. const bool *ctx,
  571. struct irq_affinity *desc)
  572. {
  573. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  574. unsigned long *indicatorp = NULL;
  575. int ret, i;
  576. struct ccw1 *ccw;
  577. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  578. if (!ccw)
  579. return -ENOMEM;
  580. for (i = 0; i < nvqs; ++i) {
  581. vqs[i] = virtio_ccw_setup_vq(vdev, i, callbacks[i], names[i],
  582. ctx ? ctx[i] : false, ccw);
  583. if (IS_ERR(vqs[i])) {
  584. ret = PTR_ERR(vqs[i]);
  585. vqs[i] = NULL;
  586. goto out;
  587. }
  588. }
  589. ret = -ENOMEM;
  590. /*
  591. * We need a data area under 2G to communicate. Our payload is
  592. * the address of the indicators.
  593. */
  594. indicatorp = kmalloc(sizeof(&vcdev->indicators), GFP_DMA | GFP_KERNEL);
  595. if (!indicatorp)
  596. goto out;
  597. *indicatorp = (unsigned long) &vcdev->indicators;
  598. if (vcdev->is_thinint) {
  599. ret = virtio_ccw_register_adapter_ind(vcdev, vqs, nvqs, ccw);
  600. if (ret)
  601. /* no error, just fall back to legacy interrupts */
  602. vcdev->is_thinint = false;
  603. }
  604. if (!vcdev->is_thinint) {
  605. /* Register queue indicators with host. */
  606. vcdev->indicators = 0;
  607. ccw->cmd_code = CCW_CMD_SET_IND;
  608. ccw->flags = 0;
  609. ccw->count = sizeof(&vcdev->indicators);
  610. ccw->cda = (__u32)(unsigned long) indicatorp;
  611. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND);
  612. if (ret)
  613. goto out;
  614. }
  615. /* Register indicators2 with host for config changes */
  616. *indicatorp = (unsigned long) &vcdev->indicators2;
  617. vcdev->indicators2 = 0;
  618. ccw->cmd_code = CCW_CMD_SET_CONF_IND;
  619. ccw->flags = 0;
  620. ccw->count = sizeof(&vcdev->indicators2);
  621. ccw->cda = (__u32)(unsigned long) indicatorp;
  622. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_CONF_IND);
  623. if (ret)
  624. goto out;
  625. kfree(indicatorp);
  626. kfree(ccw);
  627. return 0;
  628. out:
  629. kfree(indicatorp);
  630. kfree(ccw);
  631. virtio_ccw_del_vqs(vdev);
  632. return ret;
  633. }
  634. static void virtio_ccw_reset(struct virtio_device *vdev)
  635. {
  636. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  637. struct ccw1 *ccw;
  638. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  639. if (!ccw)
  640. return;
  641. /* Zero status bits. */
  642. *vcdev->status = 0;
  643. /* Send a reset ccw on device. */
  644. ccw->cmd_code = CCW_CMD_VDEV_RESET;
  645. ccw->flags = 0;
  646. ccw->count = 0;
  647. ccw->cda = 0;
  648. ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_RESET);
  649. kfree(ccw);
  650. }
  651. static u64 virtio_ccw_get_features(struct virtio_device *vdev)
  652. {
  653. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  654. struct virtio_feature_desc *features;
  655. int ret;
  656. u64 rc;
  657. struct ccw1 *ccw;
  658. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  659. if (!ccw)
  660. return 0;
  661. features = kzalloc(sizeof(*features), GFP_DMA | GFP_KERNEL);
  662. if (!features) {
  663. rc = 0;
  664. goto out_free;
  665. }
  666. /* Read the feature bits from the host. */
  667. features->index = 0;
  668. ccw->cmd_code = CCW_CMD_READ_FEAT;
  669. ccw->flags = 0;
  670. ccw->count = sizeof(*features);
  671. ccw->cda = (__u32)(unsigned long)features;
  672. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT);
  673. if (ret) {
  674. rc = 0;
  675. goto out_free;
  676. }
  677. rc = le32_to_cpu(features->features);
  678. if (vcdev->revision == 0)
  679. goto out_free;
  680. /* Read second half of the feature bits from the host. */
  681. features->index = 1;
  682. ccw->cmd_code = CCW_CMD_READ_FEAT;
  683. ccw->flags = 0;
  684. ccw->count = sizeof(*features);
  685. ccw->cda = (__u32)(unsigned long)features;
  686. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT);
  687. if (ret == 0)
  688. rc |= (u64)le32_to_cpu(features->features) << 32;
  689. out_free:
  690. kfree(features);
  691. kfree(ccw);
  692. return rc;
  693. }
  694. static int virtio_ccw_finalize_features(struct virtio_device *vdev)
  695. {
  696. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  697. struct virtio_feature_desc *features;
  698. struct ccw1 *ccw;
  699. int ret;
  700. if (vcdev->revision >= 1 &&
  701. !__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) {
  702. dev_err(&vdev->dev, "virtio: device uses revision 1 "
  703. "but does not have VIRTIO_F_VERSION_1\n");
  704. return -EINVAL;
  705. }
  706. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  707. if (!ccw)
  708. return -ENOMEM;
  709. features = kzalloc(sizeof(*features), GFP_DMA | GFP_KERNEL);
  710. if (!features) {
  711. ret = -ENOMEM;
  712. goto out_free;
  713. }
  714. /* Give virtio_ring a chance to accept features. */
  715. vring_transport_features(vdev);
  716. features->index = 0;
  717. features->features = cpu_to_le32((u32)vdev->features);
  718. /* Write the first half of the feature bits to the host. */
  719. ccw->cmd_code = CCW_CMD_WRITE_FEAT;
  720. ccw->flags = 0;
  721. ccw->count = sizeof(*features);
  722. ccw->cda = (__u32)(unsigned long)features;
  723. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
  724. if (ret)
  725. goto out_free;
  726. if (vcdev->revision == 0)
  727. goto out_free;
  728. features->index = 1;
  729. features->features = cpu_to_le32(vdev->features >> 32);
  730. /* Write the second half of the feature bits to the host. */
  731. ccw->cmd_code = CCW_CMD_WRITE_FEAT;
  732. ccw->flags = 0;
  733. ccw->count = sizeof(*features);
  734. ccw->cda = (__u32)(unsigned long)features;
  735. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
  736. out_free:
  737. kfree(features);
  738. kfree(ccw);
  739. return ret;
  740. }
  741. static void virtio_ccw_get_config(struct virtio_device *vdev,
  742. unsigned int offset, void *buf, unsigned len)
  743. {
  744. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  745. int ret;
  746. struct ccw1 *ccw;
  747. void *config_area;
  748. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  749. if (!ccw)
  750. return;
  751. config_area = kzalloc(VIRTIO_CCW_CONFIG_SIZE, GFP_DMA | GFP_KERNEL);
  752. if (!config_area)
  753. goto out_free;
  754. /* Read the config area from the host. */
  755. ccw->cmd_code = CCW_CMD_READ_CONF;
  756. ccw->flags = 0;
  757. ccw->count = offset + len;
  758. ccw->cda = (__u32)(unsigned long)config_area;
  759. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_CONFIG);
  760. if (ret)
  761. goto out_free;
  762. memcpy(vcdev->config, config_area, offset + len);
  763. if (buf)
  764. memcpy(buf, &vcdev->config[offset], len);
  765. if (vcdev->config_ready < offset + len)
  766. vcdev->config_ready = offset + len;
  767. out_free:
  768. kfree(config_area);
  769. kfree(ccw);
  770. }
  771. static void virtio_ccw_set_config(struct virtio_device *vdev,
  772. unsigned int offset, const void *buf,
  773. unsigned len)
  774. {
  775. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  776. struct ccw1 *ccw;
  777. void *config_area;
  778. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  779. if (!ccw)
  780. return;
  781. config_area = kzalloc(VIRTIO_CCW_CONFIG_SIZE, GFP_DMA | GFP_KERNEL);
  782. if (!config_area)
  783. goto out_free;
  784. /* Make sure we don't overwrite fields. */
  785. if (vcdev->config_ready < offset)
  786. virtio_ccw_get_config(vdev, 0, NULL, offset);
  787. memcpy(&vcdev->config[offset], buf, len);
  788. /* Write the config area to the host. */
  789. memcpy(config_area, vcdev->config, sizeof(vcdev->config));
  790. ccw->cmd_code = CCW_CMD_WRITE_CONF;
  791. ccw->flags = 0;
  792. ccw->count = offset + len;
  793. ccw->cda = (__u32)(unsigned long)config_area;
  794. ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_CONFIG);
  795. out_free:
  796. kfree(config_area);
  797. kfree(ccw);
  798. }
  799. static u8 virtio_ccw_get_status(struct virtio_device *vdev)
  800. {
  801. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  802. u8 old_status = *vcdev->status;
  803. struct ccw1 *ccw;
  804. if (vcdev->revision < 1)
  805. return *vcdev->status;
  806. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  807. if (!ccw)
  808. return old_status;
  809. ccw->cmd_code = CCW_CMD_READ_STATUS;
  810. ccw->flags = 0;
  811. ccw->count = sizeof(*vcdev->status);
  812. ccw->cda = (__u32)(unsigned long)vcdev->status;
  813. ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_STATUS);
  814. /*
  815. * If the channel program failed (should only happen if the device
  816. * was hotunplugged, and then we clean up via the machine check
  817. * handler anyway), vcdev->status was not overwritten and we just
  818. * return the old status, which is fine.
  819. */
  820. kfree(ccw);
  821. return *vcdev->status;
  822. }
  823. static void virtio_ccw_set_status(struct virtio_device *vdev, u8 status)
  824. {
  825. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  826. u8 old_status = *vcdev->status;
  827. struct ccw1 *ccw;
  828. int ret;
  829. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  830. if (!ccw)
  831. return;
  832. /* Write the status to the host. */
  833. *vcdev->status = status;
  834. ccw->cmd_code = CCW_CMD_WRITE_STATUS;
  835. ccw->flags = 0;
  836. ccw->count = sizeof(status);
  837. ccw->cda = (__u32)(unsigned long)vcdev->status;
  838. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_STATUS);
  839. /* Write failed? We assume status is unchanged. */
  840. if (ret)
  841. *vcdev->status = old_status;
  842. kfree(ccw);
  843. }
  844. static const struct virtio_config_ops virtio_ccw_config_ops = {
  845. .get_features = virtio_ccw_get_features,
  846. .finalize_features = virtio_ccw_finalize_features,
  847. .get = virtio_ccw_get_config,
  848. .set = virtio_ccw_set_config,
  849. .get_status = virtio_ccw_get_status,
  850. .set_status = virtio_ccw_set_status,
  851. .reset = virtio_ccw_reset,
  852. .find_vqs = virtio_ccw_find_vqs,
  853. .del_vqs = virtio_ccw_del_vqs,
  854. };
  855. /*
  856. * ccw bus driver related functions
  857. */
  858. static void virtio_ccw_release_dev(struct device *_d)
  859. {
  860. struct virtio_device *dev = dev_to_virtio(_d);
  861. struct virtio_ccw_device *vcdev = to_vc_device(dev);
  862. kfree(vcdev->status);
  863. kfree(vcdev->config_block);
  864. kfree(vcdev);
  865. }
  866. static int irb_is_error(struct irb *irb)
  867. {
  868. if (scsw_cstat(&irb->scsw) != 0)
  869. return 1;
  870. if (scsw_dstat(&irb->scsw) & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END))
  871. return 1;
  872. if (scsw_cc(&irb->scsw) != 0)
  873. return 1;
  874. return 0;
  875. }
  876. static struct virtqueue *virtio_ccw_vq_by_ind(struct virtio_ccw_device *vcdev,
  877. int index)
  878. {
  879. struct virtio_ccw_vq_info *info;
  880. unsigned long flags;
  881. struct virtqueue *vq;
  882. vq = NULL;
  883. spin_lock_irqsave(&vcdev->lock, flags);
  884. list_for_each_entry(info, &vcdev->virtqueues, node) {
  885. if (info->vq->index == index) {
  886. vq = info->vq;
  887. break;
  888. }
  889. }
  890. spin_unlock_irqrestore(&vcdev->lock, flags);
  891. return vq;
  892. }
  893. static void virtio_ccw_check_activity(struct virtio_ccw_device *vcdev,
  894. __u32 activity)
  895. {
  896. if (vcdev->curr_io & activity) {
  897. switch (activity) {
  898. case VIRTIO_CCW_DOING_READ_FEAT:
  899. case VIRTIO_CCW_DOING_WRITE_FEAT:
  900. case VIRTIO_CCW_DOING_READ_CONFIG:
  901. case VIRTIO_CCW_DOING_WRITE_CONFIG:
  902. case VIRTIO_CCW_DOING_WRITE_STATUS:
  903. case VIRTIO_CCW_DOING_READ_STATUS:
  904. case VIRTIO_CCW_DOING_SET_VQ:
  905. case VIRTIO_CCW_DOING_SET_IND:
  906. case VIRTIO_CCW_DOING_SET_CONF_IND:
  907. case VIRTIO_CCW_DOING_RESET:
  908. case VIRTIO_CCW_DOING_READ_VQ_CONF:
  909. case VIRTIO_CCW_DOING_SET_IND_ADAPTER:
  910. case VIRTIO_CCW_DOING_SET_VIRTIO_REV:
  911. vcdev->curr_io &= ~activity;
  912. wake_up(&vcdev->wait_q);
  913. break;
  914. default:
  915. /* don't know what to do... */
  916. dev_warn(&vcdev->cdev->dev,
  917. "Suspicious activity '%08x'\n", activity);
  918. WARN_ON(1);
  919. break;
  920. }
  921. }
  922. }
  923. static void virtio_ccw_int_handler(struct ccw_device *cdev,
  924. unsigned long intparm,
  925. struct irb *irb)
  926. {
  927. __u32 activity = intparm & VIRTIO_CCW_INTPARM_MASK;
  928. struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
  929. int i;
  930. struct virtqueue *vq;
  931. if (!vcdev)
  932. return;
  933. if (IS_ERR(irb)) {
  934. vcdev->err = PTR_ERR(irb);
  935. virtio_ccw_check_activity(vcdev, activity);
  936. /* Don't poke around indicators, something's wrong. */
  937. return;
  938. }
  939. /* Check if it's a notification from the host. */
  940. if ((intparm == 0) &&
  941. (scsw_stctl(&irb->scsw) ==
  942. (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND))) {
  943. /* OK */
  944. }
  945. if (irb_is_error(irb)) {
  946. /* Command reject? */
  947. if ((scsw_dstat(&irb->scsw) & DEV_STAT_UNIT_CHECK) &&
  948. (irb->ecw[0] & SNS0_CMD_REJECT))
  949. vcdev->err = -EOPNOTSUPP;
  950. else
  951. /* Map everything else to -EIO. */
  952. vcdev->err = -EIO;
  953. }
  954. virtio_ccw_check_activity(vcdev, activity);
  955. for_each_set_bit(i, &vcdev->indicators,
  956. sizeof(vcdev->indicators) * BITS_PER_BYTE) {
  957. /* The bit clear must happen before the vring kick. */
  958. clear_bit(i, &vcdev->indicators);
  959. barrier();
  960. vq = virtio_ccw_vq_by_ind(vcdev, i);
  961. vring_interrupt(0, vq);
  962. }
  963. if (test_bit(0, &vcdev->indicators2)) {
  964. virtio_config_changed(&vcdev->vdev);
  965. clear_bit(0, &vcdev->indicators2);
  966. }
  967. }
  968. /*
  969. * We usually want to autoonline all devices, but give the admin
  970. * a way to exempt devices from this.
  971. */
  972. #define __DEV_WORDS ((__MAX_SUBCHANNEL + (8*sizeof(long) - 1)) / \
  973. (8*sizeof(long)))
  974. static unsigned long devs_no_auto[__MAX_SSID + 1][__DEV_WORDS];
  975. static char *no_auto = "";
  976. module_param(no_auto, charp, 0444);
  977. MODULE_PARM_DESC(no_auto, "list of ccw bus id ranges not to be auto-onlined");
  978. static int virtio_ccw_check_autoonline(struct ccw_device *cdev)
  979. {
  980. struct ccw_dev_id id;
  981. ccw_device_get_id(cdev, &id);
  982. if (test_bit(id.devno, devs_no_auto[id.ssid]))
  983. return 0;
  984. return 1;
  985. }
  986. static void virtio_ccw_auto_online(void *data, async_cookie_t cookie)
  987. {
  988. struct ccw_device *cdev = data;
  989. int ret;
  990. ret = ccw_device_set_online(cdev);
  991. if (ret)
  992. dev_warn(&cdev->dev, "Failed to set online: %d\n", ret);
  993. }
  994. static int virtio_ccw_probe(struct ccw_device *cdev)
  995. {
  996. cdev->handler = virtio_ccw_int_handler;
  997. if (virtio_ccw_check_autoonline(cdev))
  998. async_schedule(virtio_ccw_auto_online, cdev);
  999. return 0;
  1000. }
  1001. static struct virtio_ccw_device *virtio_grab_drvdata(struct ccw_device *cdev)
  1002. {
  1003. unsigned long flags;
  1004. struct virtio_ccw_device *vcdev;
  1005. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  1006. vcdev = dev_get_drvdata(&cdev->dev);
  1007. if (!vcdev || vcdev->going_away) {
  1008. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1009. return NULL;
  1010. }
  1011. vcdev->going_away = true;
  1012. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1013. return vcdev;
  1014. }
  1015. static void virtio_ccw_remove(struct ccw_device *cdev)
  1016. {
  1017. unsigned long flags;
  1018. struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev);
  1019. if (vcdev && cdev->online) {
  1020. if (vcdev->device_lost)
  1021. virtio_break_device(&vcdev->vdev);
  1022. unregister_virtio_device(&vcdev->vdev);
  1023. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  1024. dev_set_drvdata(&cdev->dev, NULL);
  1025. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1026. }
  1027. cdev->handler = NULL;
  1028. }
  1029. static int virtio_ccw_offline(struct ccw_device *cdev)
  1030. {
  1031. unsigned long flags;
  1032. struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev);
  1033. if (!vcdev)
  1034. return 0;
  1035. if (vcdev->device_lost)
  1036. virtio_break_device(&vcdev->vdev);
  1037. unregister_virtio_device(&vcdev->vdev);
  1038. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  1039. dev_set_drvdata(&cdev->dev, NULL);
  1040. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1041. return 0;
  1042. }
  1043. static int virtio_ccw_set_transport_rev(struct virtio_ccw_device *vcdev)
  1044. {
  1045. struct virtio_rev_info *rev;
  1046. struct ccw1 *ccw;
  1047. int ret;
  1048. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  1049. if (!ccw)
  1050. return -ENOMEM;
  1051. rev = kzalloc(sizeof(*rev), GFP_DMA | GFP_KERNEL);
  1052. if (!rev) {
  1053. kfree(ccw);
  1054. return -ENOMEM;
  1055. }
  1056. /* Set transport revision */
  1057. ccw->cmd_code = CCW_CMD_SET_VIRTIO_REV;
  1058. ccw->flags = 0;
  1059. ccw->count = sizeof(*rev);
  1060. ccw->cda = (__u32)(unsigned long)rev;
  1061. vcdev->revision = VIRTIO_CCW_REV_MAX;
  1062. do {
  1063. rev->revision = vcdev->revision;
  1064. /* none of our supported revisions carry payload */
  1065. rev->length = 0;
  1066. ret = ccw_io_helper(vcdev, ccw,
  1067. VIRTIO_CCW_DOING_SET_VIRTIO_REV);
  1068. if (ret == -EOPNOTSUPP) {
  1069. if (vcdev->revision == 0)
  1070. /*
  1071. * The host device does not support setting
  1072. * the revision: let's operate it in legacy
  1073. * mode.
  1074. */
  1075. ret = 0;
  1076. else
  1077. vcdev->revision--;
  1078. }
  1079. } while (ret == -EOPNOTSUPP);
  1080. kfree(ccw);
  1081. kfree(rev);
  1082. return ret;
  1083. }
  1084. static int virtio_ccw_online(struct ccw_device *cdev)
  1085. {
  1086. int ret;
  1087. struct virtio_ccw_device *vcdev;
  1088. unsigned long flags;
  1089. vcdev = kzalloc(sizeof(*vcdev), GFP_KERNEL);
  1090. if (!vcdev) {
  1091. dev_warn(&cdev->dev, "Could not get memory for virtio\n");
  1092. ret = -ENOMEM;
  1093. goto out_free;
  1094. }
  1095. vcdev->config_block = kzalloc(sizeof(*vcdev->config_block),
  1096. GFP_DMA | GFP_KERNEL);
  1097. if (!vcdev->config_block) {
  1098. ret = -ENOMEM;
  1099. goto out_free;
  1100. }
  1101. vcdev->status = kzalloc(sizeof(*vcdev->status), GFP_DMA | GFP_KERNEL);
  1102. if (!vcdev->status) {
  1103. ret = -ENOMEM;
  1104. goto out_free;
  1105. }
  1106. vcdev->is_thinint = virtio_ccw_use_airq; /* at least try */
  1107. vcdev->vdev.dev.parent = &cdev->dev;
  1108. vcdev->vdev.dev.release = virtio_ccw_release_dev;
  1109. vcdev->vdev.config = &virtio_ccw_config_ops;
  1110. vcdev->cdev = cdev;
  1111. init_waitqueue_head(&vcdev->wait_q);
  1112. INIT_LIST_HEAD(&vcdev->virtqueues);
  1113. spin_lock_init(&vcdev->lock);
  1114. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  1115. dev_set_drvdata(&cdev->dev, vcdev);
  1116. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1117. vcdev->vdev.id.vendor = cdev->id.cu_type;
  1118. vcdev->vdev.id.device = cdev->id.cu_model;
  1119. ret = virtio_ccw_set_transport_rev(vcdev);
  1120. if (ret)
  1121. goto out_free;
  1122. ret = register_virtio_device(&vcdev->vdev);
  1123. if (ret) {
  1124. dev_warn(&cdev->dev, "Failed to register virtio device: %d\n",
  1125. ret);
  1126. goto out_put;
  1127. }
  1128. return 0;
  1129. out_put:
  1130. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  1131. dev_set_drvdata(&cdev->dev, NULL);
  1132. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1133. put_device(&vcdev->vdev.dev);
  1134. return ret;
  1135. out_free:
  1136. if (vcdev) {
  1137. kfree(vcdev->status);
  1138. kfree(vcdev->config_block);
  1139. }
  1140. kfree(vcdev);
  1141. return ret;
  1142. }
  1143. static int virtio_ccw_cio_notify(struct ccw_device *cdev, int event)
  1144. {
  1145. int rc;
  1146. struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
  1147. /*
  1148. * Make sure vcdev is set
  1149. * i.e. set_offline/remove callback not already running
  1150. */
  1151. if (!vcdev)
  1152. return NOTIFY_DONE;
  1153. switch (event) {
  1154. case CIO_GONE:
  1155. vcdev->device_lost = true;
  1156. rc = NOTIFY_DONE;
  1157. break;
  1158. default:
  1159. rc = NOTIFY_DONE;
  1160. break;
  1161. }
  1162. return rc;
  1163. }
  1164. static struct ccw_device_id virtio_ids[] = {
  1165. { CCW_DEVICE(0x3832, 0) },
  1166. {},
  1167. };
  1168. static struct ccw_driver virtio_ccw_driver = {
  1169. .driver = {
  1170. .owner = THIS_MODULE,
  1171. .name = "virtio_ccw",
  1172. },
  1173. .ids = virtio_ids,
  1174. .probe = virtio_ccw_probe,
  1175. .remove = virtio_ccw_remove,
  1176. .set_offline = virtio_ccw_offline,
  1177. .set_online = virtio_ccw_online,
  1178. .notify = virtio_ccw_cio_notify,
  1179. .int_class = IRQIO_VIR,
  1180. };
  1181. static int __init pure_hex(char **cp, unsigned int *val, int min_digit,
  1182. int max_digit, int max_val)
  1183. {
  1184. int diff;
  1185. diff = 0;
  1186. *val = 0;
  1187. while (diff <= max_digit) {
  1188. int value = hex_to_bin(**cp);
  1189. if (value < 0)
  1190. break;
  1191. *val = *val * 16 + value;
  1192. (*cp)++;
  1193. diff++;
  1194. }
  1195. if ((diff < min_digit) || (diff > max_digit) || (*val > max_val))
  1196. return 1;
  1197. return 0;
  1198. }
  1199. static int __init parse_busid(char *str, unsigned int *cssid,
  1200. unsigned int *ssid, unsigned int *devno)
  1201. {
  1202. char *str_work;
  1203. int rc, ret;
  1204. rc = 1;
  1205. if (*str == '\0')
  1206. goto out;
  1207. str_work = str;
  1208. ret = pure_hex(&str_work, cssid, 1, 2, __MAX_CSSID);
  1209. if (ret || (str_work[0] != '.'))
  1210. goto out;
  1211. str_work++;
  1212. ret = pure_hex(&str_work, ssid, 1, 1, __MAX_SSID);
  1213. if (ret || (str_work[0] != '.'))
  1214. goto out;
  1215. str_work++;
  1216. ret = pure_hex(&str_work, devno, 4, 4, __MAX_SUBCHANNEL);
  1217. if (ret || (str_work[0] != '\0'))
  1218. goto out;
  1219. rc = 0;
  1220. out:
  1221. return rc;
  1222. }
  1223. static void __init no_auto_parse(void)
  1224. {
  1225. unsigned int from_cssid, to_cssid, from_ssid, to_ssid, from, to;
  1226. char *parm, *str;
  1227. int rc;
  1228. str = no_auto;
  1229. while ((parm = strsep(&str, ","))) {
  1230. rc = parse_busid(strsep(&parm, "-"), &from_cssid,
  1231. &from_ssid, &from);
  1232. if (rc)
  1233. continue;
  1234. if (parm != NULL) {
  1235. rc = parse_busid(parm, &to_cssid,
  1236. &to_ssid, &to);
  1237. if ((from_ssid > to_ssid) ||
  1238. ((from_ssid == to_ssid) && (from > to)))
  1239. rc = -EINVAL;
  1240. } else {
  1241. to_cssid = from_cssid;
  1242. to_ssid = from_ssid;
  1243. to = from;
  1244. }
  1245. if (rc)
  1246. continue;
  1247. while ((from_ssid < to_ssid) ||
  1248. ((from_ssid == to_ssid) && (from <= to))) {
  1249. set_bit(from, devs_no_auto[from_ssid]);
  1250. from++;
  1251. if (from > __MAX_SUBCHANNEL) {
  1252. from_ssid++;
  1253. from = 0;
  1254. }
  1255. }
  1256. }
  1257. }
  1258. static int __init virtio_ccw_init(void)
  1259. {
  1260. /* parse no_auto string before we do anything further */
  1261. no_auto_parse();
  1262. return ccw_driver_register(&virtio_ccw_driver);
  1263. }
  1264. device_initcall(virtio_ccw_init);