virtio_ccw.c 36 KB

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