virtio_ccw.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424
  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. __u32 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,
  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, 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. struct irq_affinity *desc)
  571. {
  572. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  573. unsigned long *indicatorp = NULL;
  574. int ret, i;
  575. struct ccw1 *ccw;
  576. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  577. if (!ccw)
  578. return -ENOMEM;
  579. for (i = 0; i < nvqs; ++i) {
  580. vqs[i] = virtio_ccw_setup_vq(vdev, i, callbacks[i], names[i],
  581. ccw);
  582. if (IS_ERR(vqs[i])) {
  583. ret = PTR_ERR(vqs[i]);
  584. vqs[i] = NULL;
  585. goto out;
  586. }
  587. }
  588. ret = -ENOMEM;
  589. /*
  590. * We need a data area under 2G to communicate. Our payload is
  591. * the address of the indicators.
  592. */
  593. indicatorp = kmalloc(sizeof(&vcdev->indicators), GFP_DMA | GFP_KERNEL);
  594. if (!indicatorp)
  595. goto out;
  596. *indicatorp = (unsigned long) &vcdev->indicators;
  597. if (vcdev->is_thinint) {
  598. ret = virtio_ccw_register_adapter_ind(vcdev, vqs, nvqs, ccw);
  599. if (ret)
  600. /* no error, just fall back to legacy interrupts */
  601. vcdev->is_thinint = false;
  602. }
  603. if (!vcdev->is_thinint) {
  604. /* Register queue indicators with host. */
  605. vcdev->indicators = 0;
  606. ccw->cmd_code = CCW_CMD_SET_IND;
  607. ccw->flags = 0;
  608. ccw->count = sizeof(&vcdev->indicators);
  609. ccw->cda = (__u32)(unsigned long) indicatorp;
  610. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND);
  611. if (ret)
  612. goto out;
  613. }
  614. /* Register indicators2 with host for config changes */
  615. *indicatorp = (unsigned long) &vcdev->indicators2;
  616. vcdev->indicators2 = 0;
  617. ccw->cmd_code = CCW_CMD_SET_CONF_IND;
  618. ccw->flags = 0;
  619. ccw->count = sizeof(&vcdev->indicators2);
  620. ccw->cda = (__u32)(unsigned long) indicatorp;
  621. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_CONF_IND);
  622. if (ret)
  623. goto out;
  624. kfree(indicatorp);
  625. kfree(ccw);
  626. return 0;
  627. out:
  628. kfree(indicatorp);
  629. kfree(ccw);
  630. virtio_ccw_del_vqs(vdev);
  631. return ret;
  632. }
  633. static void virtio_ccw_reset(struct virtio_device *vdev)
  634. {
  635. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  636. struct ccw1 *ccw;
  637. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  638. if (!ccw)
  639. return;
  640. /* Zero status bits. */
  641. *vcdev->status = 0;
  642. /* Send a reset ccw on device. */
  643. ccw->cmd_code = CCW_CMD_VDEV_RESET;
  644. ccw->flags = 0;
  645. ccw->count = 0;
  646. ccw->cda = 0;
  647. ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_RESET);
  648. kfree(ccw);
  649. }
  650. static u64 virtio_ccw_get_features(struct virtio_device *vdev)
  651. {
  652. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  653. struct virtio_feature_desc *features;
  654. int ret;
  655. u64 rc;
  656. struct ccw1 *ccw;
  657. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  658. if (!ccw)
  659. return 0;
  660. features = kzalloc(sizeof(*features), GFP_DMA | GFP_KERNEL);
  661. if (!features) {
  662. rc = 0;
  663. goto out_free;
  664. }
  665. /* Read the feature bits from the host. */
  666. features->index = 0;
  667. ccw->cmd_code = CCW_CMD_READ_FEAT;
  668. ccw->flags = 0;
  669. ccw->count = sizeof(*features);
  670. ccw->cda = (__u32)(unsigned long)features;
  671. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT);
  672. if (ret) {
  673. rc = 0;
  674. goto out_free;
  675. }
  676. rc = le32_to_cpu(features->features);
  677. if (vcdev->revision == 0)
  678. goto out_free;
  679. /* Read second half of the feature bits from the host. */
  680. features->index = 1;
  681. ccw->cmd_code = CCW_CMD_READ_FEAT;
  682. ccw->flags = 0;
  683. ccw->count = sizeof(*features);
  684. ccw->cda = (__u32)(unsigned long)features;
  685. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT);
  686. if (ret == 0)
  687. rc |= (u64)le32_to_cpu(features->features) << 32;
  688. out_free:
  689. kfree(features);
  690. kfree(ccw);
  691. return rc;
  692. }
  693. static int virtio_ccw_finalize_features(struct virtio_device *vdev)
  694. {
  695. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  696. struct virtio_feature_desc *features;
  697. struct ccw1 *ccw;
  698. int ret;
  699. if (vcdev->revision >= 1 &&
  700. !__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) {
  701. dev_err(&vdev->dev, "virtio: device uses revision 1 "
  702. "but does not have VIRTIO_F_VERSION_1\n");
  703. return -EINVAL;
  704. }
  705. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  706. if (!ccw)
  707. return -ENOMEM;
  708. features = kzalloc(sizeof(*features), GFP_DMA | GFP_KERNEL);
  709. if (!features) {
  710. ret = -ENOMEM;
  711. goto out_free;
  712. }
  713. /* Give virtio_ring a chance to accept features. */
  714. vring_transport_features(vdev);
  715. features->index = 0;
  716. features->features = cpu_to_le32((u32)vdev->features);
  717. /* Write the first half of the feature bits to the host. */
  718. ccw->cmd_code = CCW_CMD_WRITE_FEAT;
  719. ccw->flags = 0;
  720. ccw->count = sizeof(*features);
  721. ccw->cda = (__u32)(unsigned long)features;
  722. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
  723. if (ret)
  724. goto out_free;
  725. if (vcdev->revision == 0)
  726. goto out_free;
  727. features->index = 1;
  728. features->features = cpu_to_le32(vdev->features >> 32);
  729. /* Write the second half of the feature bits to the host. */
  730. ccw->cmd_code = CCW_CMD_WRITE_FEAT;
  731. ccw->flags = 0;
  732. ccw->count = sizeof(*features);
  733. ccw->cda = (__u32)(unsigned long)features;
  734. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
  735. out_free:
  736. kfree(features);
  737. kfree(ccw);
  738. return ret;
  739. }
  740. static void virtio_ccw_get_config(struct virtio_device *vdev,
  741. unsigned int offset, void *buf, unsigned len)
  742. {
  743. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  744. int ret;
  745. struct ccw1 *ccw;
  746. void *config_area;
  747. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  748. if (!ccw)
  749. return;
  750. config_area = kzalloc(VIRTIO_CCW_CONFIG_SIZE, GFP_DMA | GFP_KERNEL);
  751. if (!config_area)
  752. goto out_free;
  753. /* Read the config area from the host. */
  754. ccw->cmd_code = CCW_CMD_READ_CONF;
  755. ccw->flags = 0;
  756. ccw->count = offset + len;
  757. ccw->cda = (__u32)(unsigned long)config_area;
  758. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_CONFIG);
  759. if (ret)
  760. goto out_free;
  761. memcpy(vcdev->config, config_area, offset + len);
  762. if (buf)
  763. memcpy(buf, &vcdev->config[offset], len);
  764. if (vcdev->config_ready < offset + len)
  765. vcdev->config_ready = offset + len;
  766. out_free:
  767. kfree(config_area);
  768. kfree(ccw);
  769. }
  770. static void virtio_ccw_set_config(struct virtio_device *vdev,
  771. unsigned int offset, const void *buf,
  772. unsigned len)
  773. {
  774. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  775. struct ccw1 *ccw;
  776. void *config_area;
  777. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  778. if (!ccw)
  779. return;
  780. config_area = kzalloc(VIRTIO_CCW_CONFIG_SIZE, GFP_DMA | GFP_KERNEL);
  781. if (!config_area)
  782. goto out_free;
  783. /* Make sure we don't overwrite fields. */
  784. if (vcdev->config_ready < offset)
  785. virtio_ccw_get_config(vdev, 0, NULL, offset);
  786. memcpy(&vcdev->config[offset], buf, len);
  787. /* Write the config area to the host. */
  788. memcpy(config_area, vcdev->config, sizeof(vcdev->config));
  789. ccw->cmd_code = CCW_CMD_WRITE_CONF;
  790. ccw->flags = 0;
  791. ccw->count = offset + len;
  792. ccw->cda = (__u32)(unsigned long)config_area;
  793. ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_CONFIG);
  794. out_free:
  795. kfree(config_area);
  796. kfree(ccw);
  797. }
  798. static u8 virtio_ccw_get_status(struct virtio_device *vdev)
  799. {
  800. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  801. u8 old_status = *vcdev->status;
  802. struct ccw1 *ccw;
  803. if (vcdev->revision < 1)
  804. return *vcdev->status;
  805. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  806. if (!ccw)
  807. return old_status;
  808. ccw->cmd_code = CCW_CMD_READ_STATUS;
  809. ccw->flags = 0;
  810. ccw->count = sizeof(*vcdev->status);
  811. ccw->cda = (__u32)(unsigned long)vcdev->status;
  812. ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_STATUS);
  813. /*
  814. * If the channel program failed (should only happen if the device
  815. * was hotunplugged, and then we clean up via the machine check
  816. * handler anyway), vcdev->status was not overwritten and we just
  817. * return the old status, which is fine.
  818. */
  819. kfree(ccw);
  820. return *vcdev->status;
  821. }
  822. static void virtio_ccw_set_status(struct virtio_device *vdev, u8 status)
  823. {
  824. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  825. u8 old_status = *vcdev->status;
  826. struct ccw1 *ccw;
  827. int ret;
  828. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  829. if (!ccw)
  830. return;
  831. /* Write the status to the host. */
  832. *vcdev->status = status;
  833. ccw->cmd_code = CCW_CMD_WRITE_STATUS;
  834. ccw->flags = 0;
  835. ccw->count = sizeof(status);
  836. ccw->cda = (__u32)(unsigned long)vcdev->status;
  837. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_STATUS);
  838. /* Write failed? We assume status is unchanged. */
  839. if (ret)
  840. *vcdev->status = old_status;
  841. kfree(ccw);
  842. }
  843. static const struct virtio_config_ops virtio_ccw_config_ops = {
  844. .get_features = virtio_ccw_get_features,
  845. .finalize_features = virtio_ccw_finalize_features,
  846. .get = virtio_ccw_get_config,
  847. .set = virtio_ccw_set_config,
  848. .get_status = virtio_ccw_get_status,
  849. .set_status = virtio_ccw_set_status,
  850. .reset = virtio_ccw_reset,
  851. .find_vqs = virtio_ccw_find_vqs,
  852. .del_vqs = virtio_ccw_del_vqs,
  853. };
  854. /*
  855. * ccw bus driver related functions
  856. */
  857. static void virtio_ccw_release_dev(struct device *_d)
  858. {
  859. struct virtio_device *dev = dev_to_virtio(_d);
  860. struct virtio_ccw_device *vcdev = to_vc_device(dev);
  861. kfree(vcdev->status);
  862. kfree(vcdev->config_block);
  863. kfree(vcdev);
  864. }
  865. static int irb_is_error(struct irb *irb)
  866. {
  867. if (scsw_cstat(&irb->scsw) != 0)
  868. return 1;
  869. if (scsw_dstat(&irb->scsw) & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END))
  870. return 1;
  871. if (scsw_cc(&irb->scsw) != 0)
  872. return 1;
  873. return 0;
  874. }
  875. static struct virtqueue *virtio_ccw_vq_by_ind(struct virtio_ccw_device *vcdev,
  876. int index)
  877. {
  878. struct virtio_ccw_vq_info *info;
  879. unsigned long flags;
  880. struct virtqueue *vq;
  881. vq = NULL;
  882. spin_lock_irqsave(&vcdev->lock, flags);
  883. list_for_each_entry(info, &vcdev->virtqueues, node) {
  884. if (info->vq->index == index) {
  885. vq = info->vq;
  886. break;
  887. }
  888. }
  889. spin_unlock_irqrestore(&vcdev->lock, flags);
  890. return vq;
  891. }
  892. static void virtio_ccw_check_activity(struct virtio_ccw_device *vcdev,
  893. __u32 activity)
  894. {
  895. if (vcdev->curr_io & activity) {
  896. switch (activity) {
  897. case VIRTIO_CCW_DOING_READ_FEAT:
  898. case VIRTIO_CCW_DOING_WRITE_FEAT:
  899. case VIRTIO_CCW_DOING_READ_CONFIG:
  900. case VIRTIO_CCW_DOING_WRITE_CONFIG:
  901. case VIRTIO_CCW_DOING_WRITE_STATUS:
  902. case VIRTIO_CCW_DOING_READ_STATUS:
  903. case VIRTIO_CCW_DOING_SET_VQ:
  904. case VIRTIO_CCW_DOING_SET_IND:
  905. case VIRTIO_CCW_DOING_SET_CONF_IND:
  906. case VIRTIO_CCW_DOING_RESET:
  907. case VIRTIO_CCW_DOING_READ_VQ_CONF:
  908. case VIRTIO_CCW_DOING_SET_IND_ADAPTER:
  909. case VIRTIO_CCW_DOING_SET_VIRTIO_REV:
  910. vcdev->curr_io &= ~activity;
  911. wake_up(&vcdev->wait_q);
  912. break;
  913. default:
  914. /* don't know what to do... */
  915. dev_warn(&vcdev->cdev->dev,
  916. "Suspicious activity '%08x'\n", activity);
  917. WARN_ON(1);
  918. break;
  919. }
  920. }
  921. }
  922. static void virtio_ccw_int_handler(struct ccw_device *cdev,
  923. unsigned long intparm,
  924. struct irb *irb)
  925. {
  926. __u32 activity = intparm & VIRTIO_CCW_INTPARM_MASK;
  927. struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
  928. int i;
  929. struct virtqueue *vq;
  930. if (!vcdev)
  931. return;
  932. if (IS_ERR(irb)) {
  933. vcdev->err = PTR_ERR(irb);
  934. virtio_ccw_check_activity(vcdev, activity);
  935. /* Don't poke around indicators, something's wrong. */
  936. return;
  937. }
  938. /* Check if it's a notification from the host. */
  939. if ((intparm == 0) &&
  940. (scsw_stctl(&irb->scsw) ==
  941. (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND))) {
  942. /* OK */
  943. }
  944. if (irb_is_error(irb)) {
  945. /* Command reject? */
  946. if ((scsw_dstat(&irb->scsw) & DEV_STAT_UNIT_CHECK) &&
  947. (irb->ecw[0] & SNS0_CMD_REJECT))
  948. vcdev->err = -EOPNOTSUPP;
  949. else
  950. /* Map everything else to -EIO. */
  951. vcdev->err = -EIO;
  952. }
  953. virtio_ccw_check_activity(vcdev, activity);
  954. for_each_set_bit(i, &vcdev->indicators,
  955. sizeof(vcdev->indicators) * BITS_PER_BYTE) {
  956. /* The bit clear must happen before the vring kick. */
  957. clear_bit(i, &vcdev->indicators);
  958. barrier();
  959. vq = virtio_ccw_vq_by_ind(vcdev, i);
  960. vring_interrupt(0, vq);
  961. }
  962. if (test_bit(0, &vcdev->indicators2)) {
  963. virtio_config_changed(&vcdev->vdev);
  964. clear_bit(0, &vcdev->indicators2);
  965. }
  966. }
  967. /*
  968. * We usually want to autoonline all devices, but give the admin
  969. * a way to exempt devices from this.
  970. */
  971. #define __DEV_WORDS ((__MAX_SUBCHANNEL + (8*sizeof(long) - 1)) / \
  972. (8*sizeof(long)))
  973. static unsigned long devs_no_auto[__MAX_SSID + 1][__DEV_WORDS];
  974. static char *no_auto = "";
  975. module_param(no_auto, charp, 0444);
  976. MODULE_PARM_DESC(no_auto, "list of ccw bus id ranges not to be auto-onlined");
  977. static int virtio_ccw_check_autoonline(struct ccw_device *cdev)
  978. {
  979. struct ccw_dev_id id;
  980. ccw_device_get_id(cdev, &id);
  981. if (test_bit(id.devno, devs_no_auto[id.ssid]))
  982. return 0;
  983. return 1;
  984. }
  985. static void virtio_ccw_auto_online(void *data, async_cookie_t cookie)
  986. {
  987. struct ccw_device *cdev = data;
  988. int ret;
  989. ret = ccw_device_set_online(cdev);
  990. if (ret)
  991. dev_warn(&cdev->dev, "Failed to set online: %d\n", ret);
  992. }
  993. static int virtio_ccw_probe(struct ccw_device *cdev)
  994. {
  995. cdev->handler = virtio_ccw_int_handler;
  996. if (virtio_ccw_check_autoonline(cdev))
  997. async_schedule(virtio_ccw_auto_online, cdev);
  998. return 0;
  999. }
  1000. static struct virtio_ccw_device *virtio_grab_drvdata(struct ccw_device *cdev)
  1001. {
  1002. unsigned long flags;
  1003. struct virtio_ccw_device *vcdev;
  1004. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  1005. vcdev = dev_get_drvdata(&cdev->dev);
  1006. if (!vcdev || vcdev->going_away) {
  1007. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1008. return NULL;
  1009. }
  1010. vcdev->going_away = true;
  1011. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1012. return vcdev;
  1013. }
  1014. static void virtio_ccw_remove(struct ccw_device *cdev)
  1015. {
  1016. unsigned long flags;
  1017. struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev);
  1018. if (vcdev && cdev->online) {
  1019. if (vcdev->device_lost)
  1020. virtio_break_device(&vcdev->vdev);
  1021. unregister_virtio_device(&vcdev->vdev);
  1022. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  1023. dev_set_drvdata(&cdev->dev, NULL);
  1024. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1025. }
  1026. cdev->handler = NULL;
  1027. }
  1028. static int virtio_ccw_offline(struct ccw_device *cdev)
  1029. {
  1030. unsigned long flags;
  1031. struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev);
  1032. if (!vcdev)
  1033. return 0;
  1034. if (vcdev->device_lost)
  1035. virtio_break_device(&vcdev->vdev);
  1036. unregister_virtio_device(&vcdev->vdev);
  1037. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  1038. dev_set_drvdata(&cdev->dev, NULL);
  1039. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1040. return 0;
  1041. }
  1042. static int virtio_ccw_set_transport_rev(struct virtio_ccw_device *vcdev)
  1043. {
  1044. struct virtio_rev_info *rev;
  1045. struct ccw1 *ccw;
  1046. int ret;
  1047. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  1048. if (!ccw)
  1049. return -ENOMEM;
  1050. rev = kzalloc(sizeof(*rev), GFP_DMA | GFP_KERNEL);
  1051. if (!rev) {
  1052. kfree(ccw);
  1053. return -ENOMEM;
  1054. }
  1055. /* Set transport revision */
  1056. ccw->cmd_code = CCW_CMD_SET_VIRTIO_REV;
  1057. ccw->flags = 0;
  1058. ccw->count = sizeof(*rev);
  1059. ccw->cda = (__u32)(unsigned long)rev;
  1060. vcdev->revision = VIRTIO_CCW_REV_MAX;
  1061. do {
  1062. rev->revision = vcdev->revision;
  1063. /* none of our supported revisions carry payload */
  1064. rev->length = 0;
  1065. ret = ccw_io_helper(vcdev, ccw,
  1066. VIRTIO_CCW_DOING_SET_VIRTIO_REV);
  1067. if (ret == -EOPNOTSUPP) {
  1068. if (vcdev->revision == 0)
  1069. /*
  1070. * The host device does not support setting
  1071. * the revision: let's operate it in legacy
  1072. * mode.
  1073. */
  1074. ret = 0;
  1075. else
  1076. vcdev->revision--;
  1077. }
  1078. } while (ret == -EOPNOTSUPP);
  1079. kfree(ccw);
  1080. kfree(rev);
  1081. return ret;
  1082. }
  1083. static int virtio_ccw_online(struct ccw_device *cdev)
  1084. {
  1085. int ret;
  1086. struct virtio_ccw_device *vcdev;
  1087. unsigned long flags;
  1088. vcdev = kzalloc(sizeof(*vcdev), GFP_KERNEL);
  1089. if (!vcdev) {
  1090. dev_warn(&cdev->dev, "Could not get memory for virtio\n");
  1091. ret = -ENOMEM;
  1092. goto out_free;
  1093. }
  1094. vcdev->config_block = kzalloc(sizeof(*vcdev->config_block),
  1095. GFP_DMA | GFP_KERNEL);
  1096. if (!vcdev->config_block) {
  1097. ret = -ENOMEM;
  1098. goto out_free;
  1099. }
  1100. vcdev->status = kzalloc(sizeof(*vcdev->status), GFP_DMA | GFP_KERNEL);
  1101. if (!vcdev->status) {
  1102. ret = -ENOMEM;
  1103. goto out_free;
  1104. }
  1105. vcdev->is_thinint = virtio_ccw_use_airq; /* at least try */
  1106. vcdev->vdev.dev.parent = &cdev->dev;
  1107. vcdev->vdev.dev.release = virtio_ccw_release_dev;
  1108. vcdev->vdev.config = &virtio_ccw_config_ops;
  1109. vcdev->cdev = cdev;
  1110. init_waitqueue_head(&vcdev->wait_q);
  1111. INIT_LIST_HEAD(&vcdev->virtqueues);
  1112. spin_lock_init(&vcdev->lock);
  1113. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  1114. dev_set_drvdata(&cdev->dev, vcdev);
  1115. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1116. vcdev->vdev.id.vendor = cdev->id.cu_type;
  1117. vcdev->vdev.id.device = cdev->id.cu_model;
  1118. ret = virtio_ccw_set_transport_rev(vcdev);
  1119. if (ret)
  1120. goto out_free;
  1121. ret = register_virtio_device(&vcdev->vdev);
  1122. if (ret) {
  1123. dev_warn(&cdev->dev, "Failed to register virtio device: %d\n",
  1124. ret);
  1125. goto out_put;
  1126. }
  1127. return 0;
  1128. out_put:
  1129. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  1130. dev_set_drvdata(&cdev->dev, NULL);
  1131. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1132. put_device(&vcdev->vdev.dev);
  1133. return ret;
  1134. out_free:
  1135. if (vcdev) {
  1136. kfree(vcdev->status);
  1137. kfree(vcdev->config_block);
  1138. }
  1139. kfree(vcdev);
  1140. return ret;
  1141. }
  1142. static int virtio_ccw_cio_notify(struct ccw_device *cdev, int event)
  1143. {
  1144. int rc;
  1145. struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
  1146. /*
  1147. * Make sure vcdev is set
  1148. * i.e. set_offline/remove callback not already running
  1149. */
  1150. if (!vcdev)
  1151. return NOTIFY_DONE;
  1152. switch (event) {
  1153. case CIO_GONE:
  1154. vcdev->device_lost = true;
  1155. rc = NOTIFY_DONE;
  1156. break;
  1157. default:
  1158. rc = NOTIFY_DONE;
  1159. break;
  1160. }
  1161. return rc;
  1162. }
  1163. static struct ccw_device_id virtio_ids[] = {
  1164. { CCW_DEVICE(0x3832, 0) },
  1165. {},
  1166. };
  1167. static struct ccw_driver virtio_ccw_driver = {
  1168. .driver = {
  1169. .owner = THIS_MODULE,
  1170. .name = "virtio_ccw",
  1171. },
  1172. .ids = virtio_ids,
  1173. .probe = virtio_ccw_probe,
  1174. .remove = virtio_ccw_remove,
  1175. .set_offline = virtio_ccw_offline,
  1176. .set_online = virtio_ccw_online,
  1177. .notify = virtio_ccw_cio_notify,
  1178. .int_class = IRQIO_VIR,
  1179. };
  1180. static int __init pure_hex(char **cp, unsigned int *val, int min_digit,
  1181. int max_digit, int max_val)
  1182. {
  1183. int diff;
  1184. diff = 0;
  1185. *val = 0;
  1186. while (diff <= max_digit) {
  1187. int value = hex_to_bin(**cp);
  1188. if (value < 0)
  1189. break;
  1190. *val = *val * 16 + value;
  1191. (*cp)++;
  1192. diff++;
  1193. }
  1194. if ((diff < min_digit) || (diff > max_digit) || (*val > max_val))
  1195. return 1;
  1196. return 0;
  1197. }
  1198. static int __init parse_busid(char *str, unsigned int *cssid,
  1199. unsigned int *ssid, unsigned int *devno)
  1200. {
  1201. char *str_work;
  1202. int rc, ret;
  1203. rc = 1;
  1204. if (*str == '\0')
  1205. goto out;
  1206. str_work = str;
  1207. ret = pure_hex(&str_work, cssid, 1, 2, __MAX_CSSID);
  1208. if (ret || (str_work[0] != '.'))
  1209. goto out;
  1210. str_work++;
  1211. ret = pure_hex(&str_work, ssid, 1, 1, __MAX_SSID);
  1212. if (ret || (str_work[0] != '.'))
  1213. goto out;
  1214. str_work++;
  1215. ret = pure_hex(&str_work, devno, 4, 4, __MAX_SUBCHANNEL);
  1216. if (ret || (str_work[0] != '\0'))
  1217. goto out;
  1218. rc = 0;
  1219. out:
  1220. return rc;
  1221. }
  1222. static void __init no_auto_parse(void)
  1223. {
  1224. unsigned int from_cssid, to_cssid, from_ssid, to_ssid, from, to;
  1225. char *parm, *str;
  1226. int rc;
  1227. str = no_auto;
  1228. while ((parm = strsep(&str, ","))) {
  1229. rc = parse_busid(strsep(&parm, "-"), &from_cssid,
  1230. &from_ssid, &from);
  1231. if (rc)
  1232. continue;
  1233. if (parm != NULL) {
  1234. rc = parse_busid(parm, &to_cssid,
  1235. &to_ssid, &to);
  1236. if ((from_ssid > to_ssid) ||
  1237. ((from_ssid == to_ssid) && (from > to)))
  1238. rc = -EINVAL;
  1239. } else {
  1240. to_cssid = from_cssid;
  1241. to_ssid = from_ssid;
  1242. to = from;
  1243. }
  1244. if (rc)
  1245. continue;
  1246. while ((from_ssid < to_ssid) ||
  1247. ((from_ssid == to_ssid) && (from <= to))) {
  1248. set_bit(from, devs_no_auto[from_ssid]);
  1249. from++;
  1250. if (from > __MAX_SUBCHANNEL) {
  1251. from_ssid++;
  1252. from = 0;
  1253. }
  1254. }
  1255. }
  1256. }
  1257. static int __init virtio_ccw_init(void)
  1258. {
  1259. /* parse no_auto string before we do anything further */
  1260. no_auto_parse();
  1261. return ccw_driver_register(&virtio_ccw_driver);
  1262. }
  1263. device_initcall(virtio_ccw_init);