virtio_ccw.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. /*
  2. * ccw based virtio transport
  3. *
  4. * Copyright IBM Corp. 2012
  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/module.h>
  27. #include <linux/io.h>
  28. #include <linux/kvm_para.h>
  29. #include <asm/setup.h>
  30. #include <asm/irq.h>
  31. #include <asm/cio.h>
  32. #include <asm/ccwdev.h>
  33. #include <asm/virtio-ccw.h>
  34. /*
  35. * virtio related functions
  36. */
  37. struct vq_config_block {
  38. __u16 index;
  39. __u16 num;
  40. } __packed;
  41. #define VIRTIO_CCW_CONFIG_SIZE 0x100
  42. /* same as PCI config space size, should be enough for all drivers */
  43. struct virtio_ccw_device {
  44. struct virtio_device vdev;
  45. __u8 *status;
  46. __u8 config[VIRTIO_CCW_CONFIG_SIZE];
  47. struct ccw_device *cdev;
  48. __u32 curr_io;
  49. int err;
  50. wait_queue_head_t wait_q;
  51. spinlock_t lock;
  52. struct list_head virtqueues;
  53. unsigned long indicators;
  54. unsigned long indicators2;
  55. struct vq_config_block *config_block;
  56. };
  57. struct vq_info_block {
  58. __u64 queue;
  59. __u32 align;
  60. __u16 index;
  61. __u16 num;
  62. } __packed;
  63. struct virtio_feature_desc {
  64. __u32 features;
  65. __u8 index;
  66. } __packed;
  67. struct virtio_ccw_vq_info {
  68. struct virtqueue *vq;
  69. int num;
  70. void *queue;
  71. struct vq_info_block *info_block;
  72. struct list_head node;
  73. long cookie;
  74. };
  75. #define CCW_CMD_SET_VQ 0x13
  76. #define CCW_CMD_VDEV_RESET 0x33
  77. #define CCW_CMD_SET_IND 0x43
  78. #define CCW_CMD_SET_CONF_IND 0x53
  79. #define CCW_CMD_READ_FEAT 0x12
  80. #define CCW_CMD_WRITE_FEAT 0x11
  81. #define CCW_CMD_READ_CONF 0x22
  82. #define CCW_CMD_WRITE_CONF 0x21
  83. #define CCW_CMD_WRITE_STATUS 0x31
  84. #define CCW_CMD_READ_VQ_CONF 0x32
  85. #define VIRTIO_CCW_DOING_SET_VQ 0x00010000
  86. #define VIRTIO_CCW_DOING_RESET 0x00040000
  87. #define VIRTIO_CCW_DOING_READ_FEAT 0x00080000
  88. #define VIRTIO_CCW_DOING_WRITE_FEAT 0x00100000
  89. #define VIRTIO_CCW_DOING_READ_CONFIG 0x00200000
  90. #define VIRTIO_CCW_DOING_WRITE_CONFIG 0x00400000
  91. #define VIRTIO_CCW_DOING_WRITE_STATUS 0x00800000
  92. #define VIRTIO_CCW_DOING_SET_IND 0x01000000
  93. #define VIRTIO_CCW_DOING_READ_VQ_CONF 0x02000000
  94. #define VIRTIO_CCW_DOING_SET_CONF_IND 0x04000000
  95. #define VIRTIO_CCW_INTPARM_MASK 0xffff0000
  96. static struct virtio_ccw_device *to_vc_device(struct virtio_device *vdev)
  97. {
  98. return container_of(vdev, struct virtio_ccw_device, vdev);
  99. }
  100. static int doing_io(struct virtio_ccw_device *vcdev, __u32 flag)
  101. {
  102. unsigned long flags;
  103. __u32 ret;
  104. spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags);
  105. if (vcdev->err)
  106. ret = 0;
  107. else
  108. ret = vcdev->curr_io & flag;
  109. spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags);
  110. return ret;
  111. }
  112. static int ccw_io_helper(struct virtio_ccw_device *vcdev,
  113. struct ccw1 *ccw, __u32 intparm)
  114. {
  115. int ret;
  116. unsigned long flags;
  117. int flag = intparm & VIRTIO_CCW_INTPARM_MASK;
  118. do {
  119. spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags);
  120. ret = ccw_device_start(vcdev->cdev, ccw, intparm, 0, 0);
  121. if (!ret) {
  122. if (!vcdev->curr_io)
  123. vcdev->err = 0;
  124. vcdev->curr_io |= flag;
  125. }
  126. spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags);
  127. cpu_relax();
  128. } while (ret == -EBUSY);
  129. wait_event(vcdev->wait_q, doing_io(vcdev, flag) == 0);
  130. return ret ? ret : vcdev->err;
  131. }
  132. static inline long do_kvm_notify(struct subchannel_id schid,
  133. unsigned long queue_index,
  134. long cookie)
  135. {
  136. register unsigned long __nr asm("1") = KVM_S390_VIRTIO_CCW_NOTIFY;
  137. register struct subchannel_id __schid asm("2") = schid;
  138. register unsigned long __index asm("3") = queue_index;
  139. register long __rc asm("2");
  140. register long __cookie asm("4") = cookie;
  141. asm volatile ("diag 2,4,0x500\n"
  142. : "=d" (__rc) : "d" (__nr), "d" (__schid), "d" (__index),
  143. "d"(__cookie)
  144. : "memory", "cc");
  145. return __rc;
  146. }
  147. static void virtio_ccw_kvm_notify(struct virtqueue *vq)
  148. {
  149. struct virtio_ccw_vq_info *info = vq->priv;
  150. struct virtio_ccw_device *vcdev;
  151. struct subchannel_id schid;
  152. vcdev = to_vc_device(info->vq->vdev);
  153. ccw_device_get_schid(vcdev->cdev, &schid);
  154. info->cookie = do_kvm_notify(schid, virtqueue_get_queue_index(vq),
  155. info->cookie);
  156. }
  157. static int virtio_ccw_read_vq_conf(struct virtio_ccw_device *vcdev,
  158. struct ccw1 *ccw, int index)
  159. {
  160. vcdev->config_block->index = index;
  161. ccw->cmd_code = CCW_CMD_READ_VQ_CONF;
  162. ccw->flags = 0;
  163. ccw->count = sizeof(struct vq_config_block);
  164. ccw->cda = (__u32)(unsigned long)(vcdev->config_block);
  165. ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_VQ_CONF);
  166. return vcdev->config_block->num;
  167. }
  168. static void virtio_ccw_del_vq(struct virtqueue *vq, struct ccw1 *ccw)
  169. {
  170. struct virtio_ccw_device *vcdev = to_vc_device(vq->vdev);
  171. struct virtio_ccw_vq_info *info = vq->priv;
  172. unsigned long flags;
  173. unsigned long size;
  174. int ret;
  175. unsigned int index = virtqueue_get_queue_index(vq);
  176. /* Remove from our list. */
  177. spin_lock_irqsave(&vcdev->lock, flags);
  178. list_del(&info->node);
  179. spin_unlock_irqrestore(&vcdev->lock, flags);
  180. /* Release from host. */
  181. info->info_block->queue = 0;
  182. info->info_block->align = 0;
  183. info->info_block->index = index;
  184. info->info_block->num = 0;
  185. ccw->cmd_code = CCW_CMD_SET_VQ;
  186. ccw->flags = 0;
  187. ccw->count = sizeof(*info->info_block);
  188. ccw->cda = (__u32)(unsigned long)(info->info_block);
  189. ret = ccw_io_helper(vcdev, ccw,
  190. VIRTIO_CCW_DOING_SET_VQ | index);
  191. /*
  192. * -ENODEV isn't considered an error: The device is gone anyway.
  193. * This may happen on device detach.
  194. */
  195. if (ret && (ret != -ENODEV))
  196. dev_warn(&vq->vdev->dev, "Error %d while deleting queue %d",
  197. ret, index);
  198. vring_del_virtqueue(vq);
  199. size = PAGE_ALIGN(vring_size(info->num, KVM_VIRTIO_CCW_RING_ALIGN));
  200. free_pages_exact(info->queue, size);
  201. kfree(info->info_block);
  202. kfree(info);
  203. }
  204. static void virtio_ccw_del_vqs(struct virtio_device *vdev)
  205. {
  206. struct virtqueue *vq, *n;
  207. struct ccw1 *ccw;
  208. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  209. if (!ccw)
  210. return;
  211. list_for_each_entry_safe(vq, n, &vdev->vqs, list)
  212. virtio_ccw_del_vq(vq, ccw);
  213. kfree(ccw);
  214. }
  215. static struct virtqueue *virtio_ccw_setup_vq(struct virtio_device *vdev,
  216. int i, vq_callback_t *callback,
  217. const char *name,
  218. struct ccw1 *ccw)
  219. {
  220. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  221. int err;
  222. struct virtqueue *vq = NULL;
  223. struct virtio_ccw_vq_info *info;
  224. unsigned long size = 0; /* silence the compiler */
  225. unsigned long flags;
  226. /* Allocate queue. */
  227. info = kzalloc(sizeof(struct virtio_ccw_vq_info), GFP_KERNEL);
  228. if (!info) {
  229. dev_warn(&vcdev->cdev->dev, "no info\n");
  230. err = -ENOMEM;
  231. goto out_err;
  232. }
  233. info->info_block = kzalloc(sizeof(*info->info_block),
  234. GFP_DMA | GFP_KERNEL);
  235. if (!info->info_block) {
  236. dev_warn(&vcdev->cdev->dev, "no info block\n");
  237. err = -ENOMEM;
  238. goto out_err;
  239. }
  240. info->num = virtio_ccw_read_vq_conf(vcdev, ccw, i);
  241. size = PAGE_ALIGN(vring_size(info->num, KVM_VIRTIO_CCW_RING_ALIGN));
  242. info->queue = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO);
  243. if (info->queue == NULL) {
  244. dev_warn(&vcdev->cdev->dev, "no queue\n");
  245. err = -ENOMEM;
  246. goto out_err;
  247. }
  248. vq = vring_new_virtqueue(i, info->num, KVM_VIRTIO_CCW_RING_ALIGN, vdev,
  249. true, info->queue, virtio_ccw_kvm_notify,
  250. callback, name);
  251. if (!vq) {
  252. /* For now, we fail if we can't get the requested size. */
  253. dev_warn(&vcdev->cdev->dev, "no vq\n");
  254. err = -ENOMEM;
  255. goto out_err;
  256. }
  257. /* Register it with the host. */
  258. info->info_block->queue = (__u64)info->queue;
  259. info->info_block->align = KVM_VIRTIO_CCW_RING_ALIGN;
  260. info->info_block->index = i;
  261. info->info_block->num = info->num;
  262. ccw->cmd_code = CCW_CMD_SET_VQ;
  263. ccw->flags = 0;
  264. ccw->count = sizeof(*info->info_block);
  265. ccw->cda = (__u32)(unsigned long)(info->info_block);
  266. err = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_VQ | i);
  267. if (err) {
  268. dev_warn(&vcdev->cdev->dev, "SET_VQ failed\n");
  269. goto out_err;
  270. }
  271. info->vq = vq;
  272. vq->priv = info;
  273. /* Save it to our list. */
  274. spin_lock_irqsave(&vcdev->lock, flags);
  275. list_add(&info->node, &vcdev->virtqueues);
  276. spin_unlock_irqrestore(&vcdev->lock, flags);
  277. return vq;
  278. out_err:
  279. if (vq)
  280. vring_del_virtqueue(vq);
  281. if (info) {
  282. if (info->queue)
  283. free_pages_exact(info->queue, size);
  284. kfree(info->info_block);
  285. }
  286. kfree(info);
  287. return ERR_PTR(err);
  288. }
  289. static int virtio_ccw_find_vqs(struct virtio_device *vdev, unsigned nvqs,
  290. struct virtqueue *vqs[],
  291. vq_callback_t *callbacks[],
  292. const char *names[])
  293. {
  294. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  295. unsigned long *indicatorp = NULL;
  296. int ret, i;
  297. struct ccw1 *ccw;
  298. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  299. if (!ccw)
  300. return -ENOMEM;
  301. for (i = 0; i < nvqs; ++i) {
  302. vqs[i] = virtio_ccw_setup_vq(vdev, i, callbacks[i], names[i],
  303. ccw);
  304. if (IS_ERR(vqs[i])) {
  305. ret = PTR_ERR(vqs[i]);
  306. vqs[i] = NULL;
  307. goto out;
  308. }
  309. }
  310. ret = -ENOMEM;
  311. /* We need a data area under 2G to communicate. */
  312. indicatorp = kmalloc(sizeof(&vcdev->indicators), GFP_DMA | GFP_KERNEL);
  313. if (!indicatorp)
  314. goto out;
  315. *indicatorp = (unsigned long) &vcdev->indicators;
  316. /* Register queue indicators with host. */
  317. vcdev->indicators = 0;
  318. ccw->cmd_code = CCW_CMD_SET_IND;
  319. ccw->flags = 0;
  320. ccw->count = sizeof(vcdev->indicators);
  321. ccw->cda = (__u32)(unsigned long) indicatorp;
  322. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND);
  323. if (ret)
  324. goto out;
  325. /* Register indicators2 with host for config changes */
  326. *indicatorp = (unsigned long) &vcdev->indicators2;
  327. vcdev->indicators2 = 0;
  328. ccw->cmd_code = CCW_CMD_SET_CONF_IND;
  329. ccw->flags = 0;
  330. ccw->count = sizeof(vcdev->indicators2);
  331. ccw->cda = (__u32)(unsigned long) indicatorp;
  332. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_CONF_IND);
  333. if (ret)
  334. goto out;
  335. kfree(indicatorp);
  336. kfree(ccw);
  337. return 0;
  338. out:
  339. kfree(indicatorp);
  340. kfree(ccw);
  341. virtio_ccw_del_vqs(vdev);
  342. return ret;
  343. }
  344. static void virtio_ccw_reset(struct virtio_device *vdev)
  345. {
  346. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  347. struct ccw1 *ccw;
  348. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  349. if (!ccw)
  350. return;
  351. /* Zero status bits. */
  352. *vcdev->status = 0;
  353. /* Send a reset ccw on device. */
  354. ccw->cmd_code = CCW_CMD_VDEV_RESET;
  355. ccw->flags = 0;
  356. ccw->count = 0;
  357. ccw->cda = 0;
  358. ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_RESET);
  359. kfree(ccw);
  360. }
  361. static u32 virtio_ccw_get_features(struct virtio_device *vdev)
  362. {
  363. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  364. struct virtio_feature_desc *features;
  365. int ret, rc;
  366. struct ccw1 *ccw;
  367. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  368. if (!ccw)
  369. return 0;
  370. features = kzalloc(sizeof(*features), GFP_DMA | GFP_KERNEL);
  371. if (!features) {
  372. rc = 0;
  373. goto out_free;
  374. }
  375. /* Read the feature bits from the host. */
  376. /* TODO: Features > 32 bits */
  377. features->index = 0;
  378. ccw->cmd_code = CCW_CMD_READ_FEAT;
  379. ccw->flags = 0;
  380. ccw->count = sizeof(*features);
  381. ccw->cda = (__u32)(unsigned long)features;
  382. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT);
  383. if (ret) {
  384. rc = 0;
  385. goto out_free;
  386. }
  387. rc = le32_to_cpu(features->features);
  388. out_free:
  389. kfree(features);
  390. kfree(ccw);
  391. return rc;
  392. }
  393. static void virtio_ccw_finalize_features(struct virtio_device *vdev)
  394. {
  395. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  396. struct virtio_feature_desc *features;
  397. int i;
  398. struct ccw1 *ccw;
  399. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  400. if (!ccw)
  401. return;
  402. features = kzalloc(sizeof(*features), GFP_DMA | GFP_KERNEL);
  403. if (!features)
  404. goto out_free;
  405. /* Give virtio_ring a chance to accept features. */
  406. vring_transport_features(vdev);
  407. for (i = 0; i < sizeof(*vdev->features) / sizeof(features->features);
  408. i++) {
  409. int highbits = i % 2 ? 32 : 0;
  410. features->index = i;
  411. features->features = cpu_to_le32(vdev->features[i / 2]
  412. >> highbits);
  413. /* Write the feature bits to the host. */
  414. ccw->cmd_code = CCW_CMD_WRITE_FEAT;
  415. ccw->flags = 0;
  416. ccw->count = sizeof(*features);
  417. ccw->cda = (__u32)(unsigned long)features;
  418. ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
  419. }
  420. out_free:
  421. kfree(features);
  422. kfree(ccw);
  423. }
  424. static void virtio_ccw_get_config(struct virtio_device *vdev,
  425. unsigned int offset, void *buf, unsigned len)
  426. {
  427. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  428. int ret;
  429. struct ccw1 *ccw;
  430. void *config_area;
  431. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  432. if (!ccw)
  433. return;
  434. config_area = kzalloc(VIRTIO_CCW_CONFIG_SIZE, GFP_DMA | GFP_KERNEL);
  435. if (!config_area)
  436. goto out_free;
  437. /* Read the config area from the host. */
  438. ccw->cmd_code = CCW_CMD_READ_CONF;
  439. ccw->flags = 0;
  440. ccw->count = offset + len;
  441. ccw->cda = (__u32)(unsigned long)config_area;
  442. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_CONFIG);
  443. if (ret)
  444. goto out_free;
  445. memcpy(vcdev->config, config_area, sizeof(vcdev->config));
  446. memcpy(buf, &vcdev->config[offset], len);
  447. out_free:
  448. kfree(config_area);
  449. kfree(ccw);
  450. }
  451. static void virtio_ccw_set_config(struct virtio_device *vdev,
  452. unsigned int offset, const void *buf,
  453. unsigned len)
  454. {
  455. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  456. struct ccw1 *ccw;
  457. void *config_area;
  458. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  459. if (!ccw)
  460. return;
  461. config_area = kzalloc(VIRTIO_CCW_CONFIG_SIZE, GFP_DMA | GFP_KERNEL);
  462. if (!config_area)
  463. goto out_free;
  464. memcpy(&vcdev->config[offset], buf, len);
  465. /* Write the config area to the host. */
  466. memcpy(config_area, vcdev->config, sizeof(vcdev->config));
  467. ccw->cmd_code = CCW_CMD_WRITE_CONF;
  468. ccw->flags = 0;
  469. ccw->count = offset + len;
  470. ccw->cda = (__u32)(unsigned long)config_area;
  471. ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_CONFIG);
  472. out_free:
  473. kfree(config_area);
  474. kfree(ccw);
  475. }
  476. static u8 virtio_ccw_get_status(struct virtio_device *vdev)
  477. {
  478. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  479. return *vcdev->status;
  480. }
  481. static void virtio_ccw_set_status(struct virtio_device *vdev, u8 status)
  482. {
  483. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  484. struct ccw1 *ccw;
  485. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  486. if (!ccw)
  487. return;
  488. /* Write the status to the host. */
  489. *vcdev->status = status;
  490. ccw->cmd_code = CCW_CMD_WRITE_STATUS;
  491. ccw->flags = 0;
  492. ccw->count = sizeof(status);
  493. ccw->cda = (__u32)(unsigned long)vcdev->status;
  494. ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_STATUS);
  495. kfree(ccw);
  496. }
  497. static struct virtio_config_ops virtio_ccw_config_ops = {
  498. .get_features = virtio_ccw_get_features,
  499. .finalize_features = virtio_ccw_finalize_features,
  500. .get = virtio_ccw_get_config,
  501. .set = virtio_ccw_set_config,
  502. .get_status = virtio_ccw_get_status,
  503. .set_status = virtio_ccw_set_status,
  504. .reset = virtio_ccw_reset,
  505. .find_vqs = virtio_ccw_find_vqs,
  506. .del_vqs = virtio_ccw_del_vqs,
  507. };
  508. /*
  509. * ccw bus driver related functions
  510. */
  511. static void virtio_ccw_release_dev(struct device *_d)
  512. {
  513. struct virtio_device *dev = container_of(_d, struct virtio_device,
  514. dev);
  515. struct virtio_ccw_device *vcdev = to_vc_device(dev);
  516. kfree(vcdev->status);
  517. kfree(vcdev->config_block);
  518. kfree(vcdev);
  519. }
  520. static int irb_is_error(struct irb *irb)
  521. {
  522. if (scsw_cstat(&irb->scsw) != 0)
  523. return 1;
  524. if (scsw_dstat(&irb->scsw) & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END))
  525. return 1;
  526. if (scsw_cc(&irb->scsw) != 0)
  527. return 1;
  528. return 0;
  529. }
  530. static struct virtqueue *virtio_ccw_vq_by_ind(struct virtio_ccw_device *vcdev,
  531. int index)
  532. {
  533. struct virtio_ccw_vq_info *info;
  534. unsigned long flags;
  535. struct virtqueue *vq;
  536. vq = NULL;
  537. spin_lock_irqsave(&vcdev->lock, flags);
  538. list_for_each_entry(info, &vcdev->virtqueues, node) {
  539. if (virtqueue_get_queue_index(info->vq) == index) {
  540. vq = info->vq;
  541. break;
  542. }
  543. }
  544. spin_unlock_irqrestore(&vcdev->lock, flags);
  545. return vq;
  546. }
  547. static void virtio_ccw_int_handler(struct ccw_device *cdev,
  548. unsigned long intparm,
  549. struct irb *irb)
  550. {
  551. __u32 activity = intparm & VIRTIO_CCW_INTPARM_MASK;
  552. struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
  553. int i;
  554. struct virtqueue *vq;
  555. struct virtio_driver *drv;
  556. /* Check if it's a notification from the host. */
  557. if ((intparm == 0) &&
  558. (scsw_stctl(&irb->scsw) ==
  559. (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND))) {
  560. /* OK */
  561. }
  562. if (irb_is_error(irb))
  563. vcdev->err = -EIO; /* XXX - use real error */
  564. if (vcdev->curr_io & activity) {
  565. switch (activity) {
  566. case VIRTIO_CCW_DOING_READ_FEAT:
  567. case VIRTIO_CCW_DOING_WRITE_FEAT:
  568. case VIRTIO_CCW_DOING_READ_CONFIG:
  569. case VIRTIO_CCW_DOING_WRITE_CONFIG:
  570. case VIRTIO_CCW_DOING_WRITE_STATUS:
  571. case VIRTIO_CCW_DOING_SET_VQ:
  572. case VIRTIO_CCW_DOING_SET_IND:
  573. case VIRTIO_CCW_DOING_SET_CONF_IND:
  574. case VIRTIO_CCW_DOING_RESET:
  575. case VIRTIO_CCW_DOING_READ_VQ_CONF:
  576. vcdev->curr_io &= ~activity;
  577. wake_up(&vcdev->wait_q);
  578. break;
  579. default:
  580. /* don't know what to do... */
  581. dev_warn(&cdev->dev, "Suspicious activity '%08x'\n",
  582. activity);
  583. WARN_ON(1);
  584. break;
  585. }
  586. }
  587. for_each_set_bit(i, &vcdev->indicators,
  588. sizeof(vcdev->indicators) * BITS_PER_BYTE) {
  589. /* The bit clear must happen before the vring kick. */
  590. clear_bit(i, &vcdev->indicators);
  591. barrier();
  592. vq = virtio_ccw_vq_by_ind(vcdev, i);
  593. vring_interrupt(0, vq);
  594. }
  595. if (test_bit(0, &vcdev->indicators2)) {
  596. drv = container_of(vcdev->vdev.dev.driver,
  597. struct virtio_driver, driver);
  598. if (drv && drv->config_changed)
  599. drv->config_changed(&vcdev->vdev);
  600. clear_bit(0, &vcdev->indicators2);
  601. }
  602. }
  603. /*
  604. * We usually want to autoonline all devices, but give the admin
  605. * a way to exempt devices from this.
  606. */
  607. #define __DEV_WORDS ((__MAX_SUBCHANNEL + (8*sizeof(long) - 1)) / \
  608. (8*sizeof(long)))
  609. static unsigned long devs_no_auto[__MAX_SSID + 1][__DEV_WORDS];
  610. static char *no_auto = "";
  611. module_param(no_auto, charp, 0444);
  612. MODULE_PARM_DESC(no_auto, "list of ccw bus id ranges not to be auto-onlined");
  613. static int virtio_ccw_check_autoonline(struct ccw_device *cdev)
  614. {
  615. struct ccw_dev_id id;
  616. ccw_device_get_id(cdev, &id);
  617. if (test_bit(id.devno, devs_no_auto[id.ssid]))
  618. return 0;
  619. return 1;
  620. }
  621. static void virtio_ccw_auto_online(void *data, async_cookie_t cookie)
  622. {
  623. struct ccw_device *cdev = data;
  624. int ret;
  625. ret = ccw_device_set_online(cdev);
  626. if (ret)
  627. dev_warn(&cdev->dev, "Failed to set online: %d\n", ret);
  628. }
  629. static int virtio_ccw_probe(struct ccw_device *cdev)
  630. {
  631. cdev->handler = virtio_ccw_int_handler;
  632. if (virtio_ccw_check_autoonline(cdev))
  633. async_schedule(virtio_ccw_auto_online, cdev);
  634. return 0;
  635. }
  636. static void virtio_ccw_remove(struct ccw_device *cdev)
  637. {
  638. struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
  639. if (cdev->online) {
  640. unregister_virtio_device(&vcdev->vdev);
  641. dev_set_drvdata(&cdev->dev, NULL);
  642. }
  643. cdev->handler = NULL;
  644. }
  645. static int virtio_ccw_offline(struct ccw_device *cdev)
  646. {
  647. struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
  648. unregister_virtio_device(&vcdev->vdev);
  649. dev_set_drvdata(&cdev->dev, NULL);
  650. return 0;
  651. }
  652. static int virtio_ccw_online(struct ccw_device *cdev)
  653. {
  654. int ret;
  655. struct virtio_ccw_device *vcdev;
  656. vcdev = kzalloc(sizeof(*vcdev), GFP_KERNEL);
  657. if (!vcdev) {
  658. dev_warn(&cdev->dev, "Could not get memory for virtio\n");
  659. ret = -ENOMEM;
  660. goto out_free;
  661. }
  662. vcdev->config_block = kzalloc(sizeof(*vcdev->config_block),
  663. GFP_DMA | GFP_KERNEL);
  664. if (!vcdev->config_block) {
  665. ret = -ENOMEM;
  666. goto out_free;
  667. }
  668. vcdev->status = kzalloc(sizeof(*vcdev->status), GFP_DMA | GFP_KERNEL);
  669. if (!vcdev->status) {
  670. ret = -ENOMEM;
  671. goto out_free;
  672. }
  673. vcdev->vdev.dev.parent = &cdev->dev;
  674. vcdev->vdev.dev.release = virtio_ccw_release_dev;
  675. vcdev->vdev.config = &virtio_ccw_config_ops;
  676. vcdev->cdev = cdev;
  677. init_waitqueue_head(&vcdev->wait_q);
  678. INIT_LIST_HEAD(&vcdev->virtqueues);
  679. spin_lock_init(&vcdev->lock);
  680. dev_set_drvdata(&cdev->dev, vcdev);
  681. vcdev->vdev.id.vendor = cdev->id.cu_type;
  682. vcdev->vdev.id.device = cdev->id.cu_model;
  683. ret = register_virtio_device(&vcdev->vdev);
  684. if (ret) {
  685. dev_warn(&cdev->dev, "Failed to register virtio device: %d\n",
  686. ret);
  687. goto out_put;
  688. }
  689. return 0;
  690. out_put:
  691. dev_set_drvdata(&cdev->dev, NULL);
  692. put_device(&vcdev->vdev.dev);
  693. return ret;
  694. out_free:
  695. if (vcdev) {
  696. kfree(vcdev->status);
  697. kfree(vcdev->config_block);
  698. }
  699. kfree(vcdev);
  700. return ret;
  701. }
  702. static int virtio_ccw_cio_notify(struct ccw_device *cdev, int event)
  703. {
  704. /* TODO: Check whether we need special handling here. */
  705. return 0;
  706. }
  707. static struct ccw_device_id virtio_ids[] = {
  708. { CCW_DEVICE(0x3832, 0) },
  709. {},
  710. };
  711. MODULE_DEVICE_TABLE(ccw, virtio_ids);
  712. static struct ccw_driver virtio_ccw_driver = {
  713. .driver = {
  714. .owner = THIS_MODULE,
  715. .name = "virtio_ccw",
  716. },
  717. .ids = virtio_ids,
  718. .probe = virtio_ccw_probe,
  719. .remove = virtio_ccw_remove,
  720. .set_offline = virtio_ccw_offline,
  721. .set_online = virtio_ccw_online,
  722. .notify = virtio_ccw_cio_notify,
  723. .int_class = IRQIO_VIR,
  724. };
  725. static int __init pure_hex(char **cp, unsigned int *val, int min_digit,
  726. int max_digit, int max_val)
  727. {
  728. int diff;
  729. diff = 0;
  730. *val = 0;
  731. while (diff <= max_digit) {
  732. int value = hex_to_bin(**cp);
  733. if (value < 0)
  734. break;
  735. *val = *val * 16 + value;
  736. (*cp)++;
  737. diff++;
  738. }
  739. if ((diff < min_digit) || (diff > max_digit) || (*val > max_val))
  740. return 1;
  741. return 0;
  742. }
  743. static int __init parse_busid(char *str, unsigned int *cssid,
  744. unsigned int *ssid, unsigned int *devno)
  745. {
  746. char *str_work;
  747. int rc, ret;
  748. rc = 1;
  749. if (*str == '\0')
  750. goto out;
  751. str_work = str;
  752. ret = pure_hex(&str_work, cssid, 1, 2, __MAX_CSSID);
  753. if (ret || (str_work[0] != '.'))
  754. goto out;
  755. str_work++;
  756. ret = pure_hex(&str_work, ssid, 1, 1, __MAX_SSID);
  757. if (ret || (str_work[0] != '.'))
  758. goto out;
  759. str_work++;
  760. ret = pure_hex(&str_work, devno, 4, 4, __MAX_SUBCHANNEL);
  761. if (ret || (str_work[0] != '\0'))
  762. goto out;
  763. rc = 0;
  764. out:
  765. return rc;
  766. }
  767. static void __init no_auto_parse(void)
  768. {
  769. unsigned int from_cssid, to_cssid, from_ssid, to_ssid, from, to;
  770. char *parm, *str;
  771. int rc;
  772. str = no_auto;
  773. while ((parm = strsep(&str, ","))) {
  774. rc = parse_busid(strsep(&parm, "-"), &from_cssid,
  775. &from_ssid, &from);
  776. if (rc)
  777. continue;
  778. if (parm != NULL) {
  779. rc = parse_busid(parm, &to_cssid,
  780. &to_ssid, &to);
  781. if ((from_ssid > to_ssid) ||
  782. ((from_ssid == to_ssid) && (from > to)))
  783. rc = -EINVAL;
  784. } else {
  785. to_cssid = from_cssid;
  786. to_ssid = from_ssid;
  787. to = from;
  788. }
  789. if (rc)
  790. continue;
  791. while ((from_ssid < to_ssid) ||
  792. ((from_ssid == to_ssid) && (from <= to))) {
  793. set_bit(from, devs_no_auto[from_ssid]);
  794. from++;
  795. if (from > __MAX_SUBCHANNEL) {
  796. from_ssid++;
  797. from = 0;
  798. }
  799. }
  800. }
  801. }
  802. static int __init virtio_ccw_init(void)
  803. {
  804. /* parse no_auto string before we do anything further */
  805. no_auto_parse();
  806. return ccw_driver_register(&virtio_ccw_driver);
  807. }
  808. module_init(virtio_ccw_init);
  809. static void __exit virtio_ccw_exit(void)
  810. {
  811. ccw_driver_unregister(&virtio_ccw_driver);
  812. }
  813. module_exit(virtio_ccw_exit);