virtio_pci_modern.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Virtio PCI driver - modern (virtio 1.0) device support
  4. *
  5. * This module allows virtio devices to be used over a virtual PCI device.
  6. * This can be used with QEMU based VMMs like KVM or Xen.
  7. *
  8. * Copyright IBM Corp. 2007
  9. * Copyright Red Hat, Inc. 2014
  10. *
  11. * Authors:
  12. * Anthony Liguori <aliguori@us.ibm.com>
  13. * Rusty Russell <rusty@rustcorp.com.au>
  14. * Michael S. Tsirkin <mst@redhat.com>
  15. */
  16. #include <linux/delay.h>
  17. #include <linux/virtio_pci_admin.h>
  18. #define VIRTIO_PCI_NO_LEGACY
  19. #define VIRTIO_RING_NO_LEGACY
  20. #include "virtio_pci_common.h"
  21. #define VIRTIO_AVQ_SGS_MAX 4
  22. static void vp_get_features(struct virtio_device *vdev, u64 *features)
  23. {
  24. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  25. vp_modern_get_extended_features(&vp_dev->mdev, features);
  26. }
  27. static int vp_avq_index(struct virtio_device *vdev, u16 *index, u16 *num)
  28. {
  29. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  30. *num = 0;
  31. if (!virtio_has_feature(vdev, VIRTIO_F_ADMIN_VQ))
  32. return 0;
  33. *num = vp_modern_avq_num(&vp_dev->mdev);
  34. if (!(*num))
  35. return -EINVAL;
  36. *index = vp_modern_avq_index(&vp_dev->mdev);
  37. return 0;
  38. }
  39. void vp_modern_avq_done(struct virtqueue *vq)
  40. {
  41. struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
  42. struct virtio_pci_admin_vq *admin_vq = &vp_dev->admin_vq;
  43. unsigned int status_size = sizeof(struct virtio_admin_cmd_status);
  44. struct virtio_admin_cmd *cmd;
  45. unsigned long flags;
  46. unsigned int len;
  47. spin_lock_irqsave(&admin_vq->lock, flags);
  48. do {
  49. virtqueue_disable_cb(vq);
  50. while ((cmd = virtqueue_get_buf(vq, &len))) {
  51. /* If the number of bytes written by the device is less
  52. * than the size of struct virtio_admin_cmd_status, the
  53. * remaining status bytes will remain zero-initialized,
  54. * since the buffer was zeroed during allocation.
  55. * In this case, set the size of command_specific_result
  56. * to 0.
  57. */
  58. if (len < status_size)
  59. cmd->result_sg_size = 0;
  60. else
  61. cmd->result_sg_size = len - status_size;
  62. complete(&cmd->completion);
  63. }
  64. } while (!virtqueue_enable_cb(vq));
  65. spin_unlock_irqrestore(&admin_vq->lock, flags);
  66. }
  67. static int virtqueue_exec_admin_cmd(struct virtio_pci_admin_vq *admin_vq,
  68. u16 opcode,
  69. struct scatterlist **sgs,
  70. unsigned int out_num,
  71. unsigned int in_num,
  72. struct virtio_admin_cmd *cmd)
  73. {
  74. struct virtqueue *vq;
  75. unsigned long flags;
  76. int ret;
  77. vq = admin_vq->info->vq;
  78. if (!vq)
  79. return -EIO;
  80. if (opcode != VIRTIO_ADMIN_CMD_LIST_QUERY &&
  81. opcode != VIRTIO_ADMIN_CMD_LIST_USE &&
  82. !((1ULL << opcode) & admin_vq->supported_cmds))
  83. return -EOPNOTSUPP;
  84. init_completion(&cmd->completion);
  85. again:
  86. if (virtqueue_is_broken(vq))
  87. return -EIO;
  88. spin_lock_irqsave(&admin_vq->lock, flags);
  89. ret = virtqueue_add_sgs(vq, sgs, out_num, in_num, cmd, GFP_KERNEL);
  90. if (ret < 0) {
  91. if (ret == -ENOSPC) {
  92. spin_unlock_irqrestore(&admin_vq->lock, flags);
  93. cpu_relax();
  94. goto again;
  95. }
  96. goto unlock_err;
  97. }
  98. if (!virtqueue_kick(vq))
  99. goto unlock_err;
  100. spin_unlock_irqrestore(&admin_vq->lock, flags);
  101. wait_for_completion(&cmd->completion);
  102. return cmd->ret;
  103. unlock_err:
  104. spin_unlock_irqrestore(&admin_vq->lock, flags);
  105. return -EIO;
  106. }
  107. int vp_modern_admin_cmd_exec(struct virtio_device *vdev,
  108. struct virtio_admin_cmd *cmd)
  109. {
  110. struct scatterlist *sgs[VIRTIO_AVQ_SGS_MAX], hdr, stat;
  111. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  112. struct virtio_admin_cmd_status *va_status;
  113. unsigned int out_num = 0, in_num = 0;
  114. struct virtio_admin_cmd_hdr *va_hdr;
  115. u16 status;
  116. int ret;
  117. if (!virtio_has_feature(vdev, VIRTIO_F_ADMIN_VQ))
  118. return -EOPNOTSUPP;
  119. va_status = kzalloc_obj(*va_status);
  120. if (!va_status)
  121. return -ENOMEM;
  122. va_hdr = kzalloc_obj(*va_hdr);
  123. if (!va_hdr) {
  124. ret = -ENOMEM;
  125. goto err_alloc;
  126. }
  127. va_hdr->opcode = cmd->opcode;
  128. va_hdr->group_type = cmd->group_type;
  129. va_hdr->group_member_id = cmd->group_member_id;
  130. /* Add header */
  131. sg_init_one(&hdr, va_hdr, sizeof(*va_hdr));
  132. sgs[out_num] = &hdr;
  133. out_num++;
  134. if (cmd->data_sg) {
  135. sgs[out_num] = cmd->data_sg;
  136. out_num++;
  137. }
  138. /* Add return status */
  139. sg_init_one(&stat, va_status, sizeof(*va_status));
  140. sgs[out_num + in_num] = &stat;
  141. in_num++;
  142. if (cmd->result_sg) {
  143. sgs[out_num + in_num] = cmd->result_sg;
  144. in_num++;
  145. }
  146. ret = virtqueue_exec_admin_cmd(&vp_dev->admin_vq,
  147. le16_to_cpu(cmd->opcode),
  148. sgs, out_num, in_num, cmd);
  149. if (ret) {
  150. dev_err(&vdev->dev,
  151. "Failed to execute command on admin vq: %d\n.", ret);
  152. goto err_cmd_exec;
  153. }
  154. status = le16_to_cpu(va_status->status);
  155. if (status != VIRTIO_ADMIN_STATUS_OK) {
  156. dev_err(&vdev->dev,
  157. "admin command error: status(%#x) qualifier(%#x)\n",
  158. status, le16_to_cpu(va_status->status_qualifier));
  159. ret = -status;
  160. }
  161. err_cmd_exec:
  162. kfree(va_hdr);
  163. err_alloc:
  164. kfree(va_status);
  165. return ret;
  166. }
  167. static void virtio_pci_admin_cmd_list_init(struct virtio_device *virtio_dev)
  168. {
  169. struct virtio_pci_device *vp_dev = to_vp_device(virtio_dev);
  170. struct virtio_admin_cmd cmd = {};
  171. struct scatterlist result_sg;
  172. struct scatterlist data_sg;
  173. __le64 *data;
  174. int ret;
  175. data = kzalloc_obj(*data);
  176. if (!data)
  177. return;
  178. sg_init_one(&result_sg, data, sizeof(*data));
  179. cmd.opcode = cpu_to_le16(VIRTIO_ADMIN_CMD_LIST_QUERY);
  180. cmd.group_type = cpu_to_le16(VIRTIO_ADMIN_GROUP_TYPE_SRIOV);
  181. cmd.result_sg = &result_sg;
  182. ret = vp_modern_admin_cmd_exec(virtio_dev, &cmd);
  183. if (ret)
  184. goto end;
  185. *data &= cpu_to_le64(VIRTIO_ADMIN_CMD_BITMAP);
  186. sg_init_one(&data_sg, data, sizeof(*data));
  187. cmd.opcode = cpu_to_le16(VIRTIO_ADMIN_CMD_LIST_USE);
  188. cmd.data_sg = &data_sg;
  189. cmd.result_sg = NULL;
  190. ret = vp_modern_admin_cmd_exec(virtio_dev, &cmd);
  191. if (ret)
  192. goto end;
  193. vp_dev->admin_vq.supported_cmds = le64_to_cpu(*data);
  194. end:
  195. kfree(data);
  196. }
  197. static void
  198. virtio_pci_admin_cmd_dev_parts_objects_enable(struct virtio_device *virtio_dev)
  199. {
  200. struct virtio_pci_device *vp_dev = to_vp_device(virtio_dev);
  201. struct virtio_admin_cmd_cap_get_data *get_data;
  202. struct virtio_admin_cmd_cap_set_data *set_data;
  203. struct virtio_dev_parts_cap *result;
  204. struct virtio_admin_cmd cmd = {};
  205. struct scatterlist result_sg;
  206. struct scatterlist data_sg;
  207. u8 resource_objects_limit;
  208. u16 set_data_size;
  209. int ret;
  210. get_data = kzalloc_obj(*get_data);
  211. if (!get_data)
  212. return;
  213. result = kzalloc_obj(*result);
  214. if (!result)
  215. goto end;
  216. get_data->id = cpu_to_le16(VIRTIO_DEV_PARTS_CAP);
  217. sg_init_one(&data_sg, get_data, sizeof(*get_data));
  218. sg_init_one(&result_sg, result, sizeof(*result));
  219. cmd.opcode = cpu_to_le16(VIRTIO_ADMIN_CMD_DEVICE_CAP_GET);
  220. cmd.group_type = cpu_to_le16(VIRTIO_ADMIN_GROUP_TYPE_SELF);
  221. cmd.data_sg = &data_sg;
  222. cmd.result_sg = &result_sg;
  223. ret = vp_modern_admin_cmd_exec(virtio_dev, &cmd);
  224. if (ret)
  225. goto err_get;
  226. set_data_size = sizeof(*set_data) + sizeof(*result);
  227. set_data = kzalloc(set_data_size, GFP_KERNEL);
  228. if (!set_data)
  229. goto err_get;
  230. set_data->id = cpu_to_le16(VIRTIO_DEV_PARTS_CAP);
  231. /* Set the limit to the minimum value between the GET and SET values
  232. * supported by the device. Since the obj_id for VIRTIO_DEV_PARTS_CAP
  233. * is a globally unique value per PF, there is no possibility of
  234. * overlap between GET and SET operations.
  235. */
  236. resource_objects_limit = min(result->get_parts_resource_objects_limit,
  237. result->set_parts_resource_objects_limit);
  238. result->get_parts_resource_objects_limit = resource_objects_limit;
  239. result->set_parts_resource_objects_limit = resource_objects_limit;
  240. memcpy(set_data->cap_specific_data, result, sizeof(*result));
  241. sg_init_one(&data_sg, set_data, set_data_size);
  242. cmd.data_sg = &data_sg;
  243. cmd.result_sg = NULL;
  244. cmd.opcode = cpu_to_le16(VIRTIO_ADMIN_CMD_DRIVER_CAP_SET);
  245. ret = vp_modern_admin_cmd_exec(virtio_dev, &cmd);
  246. if (ret)
  247. goto err_set;
  248. /* Allocate IDR to manage the dev caps objects */
  249. ida_init(&vp_dev->admin_vq.dev_parts_ida);
  250. vp_dev->admin_vq.max_dev_parts_objects = resource_objects_limit;
  251. err_set:
  252. kfree(set_data);
  253. err_get:
  254. kfree(result);
  255. end:
  256. kfree(get_data);
  257. }
  258. static void virtio_pci_admin_cmd_cap_init(struct virtio_device *virtio_dev)
  259. {
  260. struct virtio_pci_device *vp_dev = to_vp_device(virtio_dev);
  261. struct virtio_admin_cmd_query_cap_id_result *data;
  262. struct virtio_admin_cmd cmd = {};
  263. struct scatterlist result_sg;
  264. int ret;
  265. data = kzalloc_obj(*data);
  266. if (!data)
  267. return;
  268. sg_init_one(&result_sg, data, sizeof(*data));
  269. cmd.opcode = cpu_to_le16(VIRTIO_ADMIN_CMD_CAP_ID_LIST_QUERY);
  270. cmd.group_type = cpu_to_le16(VIRTIO_ADMIN_GROUP_TYPE_SELF);
  271. cmd.result_sg = &result_sg;
  272. ret = vp_modern_admin_cmd_exec(virtio_dev, &cmd);
  273. if (ret)
  274. goto end;
  275. /* Max number of caps fits into a single u64 */
  276. BUILD_BUG_ON(sizeof(data->supported_caps) > sizeof(u64));
  277. vp_dev->admin_vq.supported_caps = le64_to_cpu(data->supported_caps[0]);
  278. if (!(vp_dev->admin_vq.supported_caps & (1 << VIRTIO_DEV_PARTS_CAP)))
  279. goto end;
  280. virtio_pci_admin_cmd_dev_parts_objects_enable(virtio_dev);
  281. end:
  282. kfree(data);
  283. }
  284. static void vp_modern_avq_activate(struct virtio_device *vdev)
  285. {
  286. if (!virtio_has_feature(vdev, VIRTIO_F_ADMIN_VQ))
  287. return;
  288. virtio_pci_admin_cmd_list_init(vdev);
  289. virtio_pci_admin_cmd_cap_init(vdev);
  290. }
  291. static void vp_modern_avq_cleanup(struct virtio_device *vdev)
  292. {
  293. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  294. struct virtio_admin_cmd *cmd;
  295. struct virtqueue *vq;
  296. if (!virtio_has_feature(vdev, VIRTIO_F_ADMIN_VQ))
  297. return;
  298. vq = vp_dev->admin_vq.info->vq;
  299. if (!vq)
  300. return;
  301. while ((cmd = virtqueue_detach_unused_buf(vq))) {
  302. cmd->ret = -EIO;
  303. complete(&cmd->completion);
  304. }
  305. }
  306. static void vp_transport_features(struct virtio_device *vdev, u64 features)
  307. {
  308. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  309. struct pci_dev *pci_dev = vp_dev->pci_dev;
  310. if ((features & BIT_ULL(VIRTIO_F_SR_IOV)) &&
  311. pci_find_ext_capability(pci_dev, PCI_EXT_CAP_ID_SRIOV))
  312. __virtio_set_bit(vdev, VIRTIO_F_SR_IOV);
  313. if (features & BIT_ULL(VIRTIO_F_RING_RESET))
  314. __virtio_set_bit(vdev, VIRTIO_F_RING_RESET);
  315. if (features & BIT_ULL(VIRTIO_F_ADMIN_VQ))
  316. __virtio_set_bit(vdev, VIRTIO_F_ADMIN_VQ);
  317. }
  318. static int __vp_check_common_size_one_feature(struct virtio_device *vdev, u32 fbit,
  319. u32 offset, const char *fname)
  320. {
  321. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  322. if (!__virtio_test_bit(vdev, fbit))
  323. return 0;
  324. if (likely(vp_dev->mdev.common_len >= offset))
  325. return 0;
  326. dev_err(&vdev->dev,
  327. "virtio: common cfg size(%zu) does not match the feature %s\n",
  328. vp_dev->mdev.common_len, fname);
  329. return -EINVAL;
  330. }
  331. #define vp_check_common_size_one_feature(vdev, fbit, field) \
  332. __vp_check_common_size_one_feature(vdev, fbit, \
  333. offsetofend(struct virtio_pci_modern_common_cfg, field), #fbit)
  334. static int vp_check_common_size(struct virtio_device *vdev)
  335. {
  336. if (vp_check_common_size_one_feature(vdev, VIRTIO_F_NOTIF_CONFIG_DATA, queue_notify_data))
  337. return -EINVAL;
  338. if (vp_check_common_size_one_feature(vdev, VIRTIO_F_RING_RESET, queue_reset))
  339. return -EINVAL;
  340. if (vp_check_common_size_one_feature(vdev, VIRTIO_F_ADMIN_VQ, admin_queue_num))
  341. return -EINVAL;
  342. return 0;
  343. }
  344. /* virtio config->finalize_features() implementation */
  345. static int vp_finalize_features(struct virtio_device *vdev)
  346. {
  347. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  348. u64 features = vdev->features;
  349. /* Give virtio_ring a chance to accept features. */
  350. vring_transport_features(vdev);
  351. /* Give virtio_pci a chance to accept features. */
  352. vp_transport_features(vdev, features);
  353. if (!__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) {
  354. dev_err(&vdev->dev, "virtio: device uses modern interface "
  355. "but does not have VIRTIO_F_VERSION_1\n");
  356. return -EINVAL;
  357. }
  358. if (vp_check_common_size(vdev))
  359. return -EINVAL;
  360. vp_modern_set_extended_features(&vp_dev->mdev, vdev->features_array);
  361. return 0;
  362. }
  363. /* virtio config->get() implementation */
  364. static void vp_get(struct virtio_device *vdev, unsigned int offset,
  365. void *buf, unsigned int len)
  366. {
  367. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  368. struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
  369. void __iomem *device = mdev->device;
  370. u8 b;
  371. __le16 w;
  372. __le32 l;
  373. BUG_ON(offset + len > mdev->device_len);
  374. switch (len) {
  375. case 1:
  376. b = ioread8(device + offset);
  377. memcpy(buf, &b, sizeof b);
  378. break;
  379. case 2:
  380. w = cpu_to_le16(ioread16(device + offset));
  381. memcpy(buf, &w, sizeof w);
  382. break;
  383. case 4:
  384. l = cpu_to_le32(ioread32(device + offset));
  385. memcpy(buf, &l, sizeof l);
  386. break;
  387. case 8:
  388. l = cpu_to_le32(ioread32(device + offset));
  389. memcpy(buf, &l, sizeof l);
  390. l = cpu_to_le32(ioread32(device + offset + sizeof l));
  391. memcpy(buf + sizeof l, &l, sizeof l);
  392. break;
  393. default:
  394. BUG();
  395. }
  396. }
  397. /* the config->set() implementation. it's symmetric to the config->get()
  398. * implementation */
  399. static void vp_set(struct virtio_device *vdev, unsigned int offset,
  400. const void *buf, unsigned int len)
  401. {
  402. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  403. struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
  404. void __iomem *device = mdev->device;
  405. u8 b;
  406. __le16 w;
  407. __le32 l;
  408. BUG_ON(offset + len > mdev->device_len);
  409. switch (len) {
  410. case 1:
  411. memcpy(&b, buf, sizeof b);
  412. iowrite8(b, device + offset);
  413. break;
  414. case 2:
  415. memcpy(&w, buf, sizeof w);
  416. iowrite16(le16_to_cpu(w), device + offset);
  417. break;
  418. case 4:
  419. memcpy(&l, buf, sizeof l);
  420. iowrite32(le32_to_cpu(l), device + offset);
  421. break;
  422. case 8:
  423. memcpy(&l, buf, sizeof l);
  424. iowrite32(le32_to_cpu(l), device + offset);
  425. memcpy(&l, buf + sizeof l, sizeof l);
  426. iowrite32(le32_to_cpu(l), device + offset + sizeof l);
  427. break;
  428. default:
  429. BUG();
  430. }
  431. }
  432. static u32 vp_generation(struct virtio_device *vdev)
  433. {
  434. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  435. return vp_modern_generation(&vp_dev->mdev);
  436. }
  437. /* config->{get,set}_status() implementations */
  438. static u8 vp_get_status(struct virtio_device *vdev)
  439. {
  440. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  441. return vp_modern_get_status(&vp_dev->mdev);
  442. }
  443. static void vp_set_status(struct virtio_device *vdev, u8 status)
  444. {
  445. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  446. /* We should never be setting status to 0. */
  447. BUG_ON(status == 0);
  448. vp_modern_set_status(&vp_dev->mdev, status);
  449. if (status & VIRTIO_CONFIG_S_DRIVER_OK)
  450. vp_modern_avq_activate(vdev);
  451. }
  452. static void vp_reset(struct virtio_device *vdev)
  453. {
  454. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  455. struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
  456. /* 0 status means a reset. */
  457. vp_modern_set_status(mdev, 0);
  458. /* After writing 0 to device_status, the driver MUST wait for a read of
  459. * device_status to return 0 before reinitializing the device.
  460. * This will flush out the status write, and flush in device writes,
  461. * including MSI-X interrupts, if any.
  462. */
  463. while (vp_modern_get_status(mdev))
  464. msleep(1);
  465. vp_modern_avq_cleanup(vdev);
  466. /* Flush pending VQ/configuration callbacks. */
  467. vp_synchronize_vectors(vdev);
  468. }
  469. static int vp_active_vq(struct virtqueue *vq, u16 msix_vec)
  470. {
  471. struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
  472. struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
  473. unsigned long index;
  474. index = vq->index;
  475. /* activate the queue */
  476. vp_modern_set_queue_size(mdev, index, virtqueue_get_vring_size(vq));
  477. vp_modern_queue_address(mdev, index, virtqueue_get_desc_addr(vq),
  478. virtqueue_get_avail_addr(vq),
  479. virtqueue_get_used_addr(vq));
  480. if (msix_vec != VIRTIO_MSI_NO_VECTOR) {
  481. msix_vec = vp_modern_queue_vector(mdev, index, msix_vec);
  482. if (msix_vec == VIRTIO_MSI_NO_VECTOR)
  483. return -EBUSY;
  484. }
  485. return 0;
  486. }
  487. static int vp_modern_disable_vq_and_reset(struct virtqueue *vq)
  488. {
  489. struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
  490. struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
  491. struct virtio_pci_vq_info *info;
  492. unsigned long flags;
  493. if (!virtio_has_feature(vq->vdev, VIRTIO_F_RING_RESET))
  494. return -ENOENT;
  495. vp_modern_set_queue_reset(mdev, vq->index);
  496. info = vp_dev->vqs[vq->index];
  497. /* delete vq from irq handler */
  498. spin_lock_irqsave(&vp_dev->lock, flags);
  499. list_del(&info->node);
  500. spin_unlock_irqrestore(&vp_dev->lock, flags);
  501. INIT_LIST_HEAD(&info->node);
  502. #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
  503. __virtqueue_break(vq);
  504. #endif
  505. /* For the case where vq has an exclusive irq, call synchronize_irq() to
  506. * wait for completion.
  507. *
  508. * note: We can't use disable_irq() since it conflicts with the affinity
  509. * managed IRQ that is used by some drivers.
  510. */
  511. if (vp_dev->per_vq_vectors && info->msix_vector != VIRTIO_MSI_NO_VECTOR)
  512. synchronize_irq(pci_irq_vector(vp_dev->pci_dev, info->msix_vector));
  513. vq->reset = true;
  514. return 0;
  515. }
  516. static int vp_modern_enable_vq_after_reset(struct virtqueue *vq)
  517. {
  518. struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
  519. struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
  520. struct virtio_pci_vq_info *info;
  521. unsigned long flags, index;
  522. int err;
  523. if (!vq->reset)
  524. return -EBUSY;
  525. index = vq->index;
  526. info = vp_dev->vqs[index];
  527. if (vp_modern_get_queue_reset(mdev, index))
  528. return -EBUSY;
  529. if (vp_modern_get_queue_enable(mdev, index))
  530. return -EBUSY;
  531. err = vp_active_vq(vq, info->msix_vector);
  532. if (err)
  533. return err;
  534. if (vq->callback) {
  535. spin_lock_irqsave(&vp_dev->lock, flags);
  536. list_add(&info->node, &vp_dev->virtqueues);
  537. spin_unlock_irqrestore(&vp_dev->lock, flags);
  538. } else {
  539. INIT_LIST_HEAD(&info->node);
  540. }
  541. #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
  542. __virtqueue_unbreak(vq);
  543. #endif
  544. vp_modern_set_queue_enable(&vp_dev->mdev, index, true);
  545. vq->reset = false;
  546. return 0;
  547. }
  548. static u16 vp_config_vector(struct virtio_pci_device *vp_dev, u16 vector)
  549. {
  550. return vp_modern_config_vector(&vp_dev->mdev, vector);
  551. }
  552. static bool vp_notify_with_data(struct virtqueue *vq)
  553. {
  554. u32 data = vring_notification_data(vq);
  555. iowrite32(data, (void __iomem *)vq->priv);
  556. return true;
  557. }
  558. static struct virtqueue *setup_vq(struct virtio_pci_device *vp_dev,
  559. struct virtio_pci_vq_info *info,
  560. unsigned int index,
  561. void (*callback)(struct virtqueue *vq),
  562. const char *name,
  563. bool ctx,
  564. u16 msix_vec)
  565. {
  566. struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
  567. bool (*notify)(struct virtqueue *vq);
  568. struct virtqueue *vq;
  569. bool is_avq;
  570. u16 num;
  571. int err;
  572. if (__virtio_test_bit(&vp_dev->vdev, VIRTIO_F_NOTIFICATION_DATA))
  573. notify = vp_notify_with_data;
  574. else
  575. notify = vp_notify;
  576. is_avq = vp_is_avq(&vp_dev->vdev, index);
  577. if (index >= vp_modern_get_num_queues(mdev) && !is_avq)
  578. return ERR_PTR(-EINVAL);
  579. num = vp_modern_get_queue_size(mdev, index);
  580. /* Check if queue is either not available or already active. */
  581. if (!num || vp_modern_get_queue_enable(mdev, index))
  582. return ERR_PTR(-ENOENT);
  583. info->msix_vector = msix_vec;
  584. /* create the vring */
  585. vq = vring_create_virtqueue(index, num,
  586. SMP_CACHE_BYTES, &vp_dev->vdev,
  587. true, true, ctx,
  588. notify, callback, name);
  589. if (!vq)
  590. return ERR_PTR(-ENOMEM);
  591. vq->num_max = num;
  592. err = vp_active_vq(vq, msix_vec);
  593. if (err)
  594. goto err;
  595. vq->priv = (void __force *)vp_modern_map_vq_notify(mdev, index, NULL);
  596. if (!vq->priv) {
  597. err = -ENOMEM;
  598. goto err;
  599. }
  600. return vq;
  601. err:
  602. vring_del_virtqueue(vq);
  603. return ERR_PTR(err);
  604. }
  605. static int vp_modern_find_vqs(struct virtio_device *vdev, unsigned int nvqs,
  606. struct virtqueue *vqs[],
  607. struct virtqueue_info vqs_info[],
  608. struct irq_affinity *desc)
  609. {
  610. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  611. struct virtqueue *vq;
  612. int rc = vp_find_vqs(vdev, nvqs, vqs, vqs_info, desc);
  613. if (rc)
  614. return rc;
  615. /* Select and activate all queues. Has to be done last: once we do
  616. * this, there's no way to go back except reset.
  617. */
  618. list_for_each_entry(vq, &vdev->vqs, list)
  619. vp_modern_set_queue_enable(&vp_dev->mdev, vq->index, true);
  620. return 0;
  621. }
  622. static void del_vq(struct virtio_pci_vq_info *info)
  623. {
  624. struct virtqueue *vq = info->vq;
  625. struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
  626. struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
  627. if (vp_dev->msix_enabled)
  628. vp_modern_queue_vector(mdev, vq->index,
  629. VIRTIO_MSI_NO_VECTOR);
  630. if (!mdev->notify_base)
  631. pci_iounmap(mdev->pci_dev, (void __force __iomem *)vq->priv);
  632. vring_del_virtqueue(vq);
  633. }
  634. static int virtio_pci_find_shm_cap(struct pci_dev *dev, u8 required_id,
  635. u8 *bar, u64 *offset, u64 *len)
  636. {
  637. int pos;
  638. for (pos = pci_find_capability(dev, PCI_CAP_ID_VNDR); pos > 0;
  639. pos = pci_find_next_capability(dev, pos, PCI_CAP_ID_VNDR)) {
  640. u8 type, cap_len, id, res_bar;
  641. u32 tmp32;
  642. u64 res_offset, res_length;
  643. pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
  644. cfg_type), &type);
  645. if (type != VIRTIO_PCI_CAP_SHARED_MEMORY_CFG)
  646. continue;
  647. pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
  648. cap_len), &cap_len);
  649. if (cap_len != sizeof(struct virtio_pci_cap64)) {
  650. dev_err(&dev->dev, "%s: shm cap with bad size offset:"
  651. " %d size: %d\n", __func__, pos, cap_len);
  652. continue;
  653. }
  654. pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
  655. id), &id);
  656. if (id != required_id)
  657. continue;
  658. pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
  659. bar), &res_bar);
  660. if (res_bar >= PCI_STD_NUM_BARS)
  661. continue;
  662. /* Type and ID match, and the BAR value isn't reserved.
  663. * Looks good.
  664. */
  665. /* Read the lower 32bit of length and offset */
  666. pci_read_config_dword(dev, pos + offsetof(struct virtio_pci_cap,
  667. offset), &tmp32);
  668. res_offset = tmp32;
  669. pci_read_config_dword(dev, pos + offsetof(struct virtio_pci_cap,
  670. length), &tmp32);
  671. res_length = tmp32;
  672. /* and now the top half */
  673. pci_read_config_dword(dev,
  674. pos + offsetof(struct virtio_pci_cap64,
  675. offset_hi), &tmp32);
  676. res_offset |= ((u64)tmp32) << 32;
  677. pci_read_config_dword(dev,
  678. pos + offsetof(struct virtio_pci_cap64,
  679. length_hi), &tmp32);
  680. res_length |= ((u64)tmp32) << 32;
  681. *bar = res_bar;
  682. *offset = res_offset;
  683. *len = res_length;
  684. return pos;
  685. }
  686. return 0;
  687. }
  688. static bool vp_get_shm_region(struct virtio_device *vdev,
  689. struct virtio_shm_region *region, u8 id)
  690. {
  691. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  692. struct pci_dev *pci_dev = vp_dev->pci_dev;
  693. u8 bar;
  694. u64 offset, len;
  695. phys_addr_t phys_addr;
  696. size_t bar_len;
  697. if (!virtio_pci_find_shm_cap(pci_dev, id, &bar, &offset, &len))
  698. return false;
  699. phys_addr = pci_resource_start(pci_dev, bar);
  700. bar_len = pci_resource_len(pci_dev, bar);
  701. if ((offset + len) < offset) {
  702. dev_err(&pci_dev->dev, "%s: cap offset+len overflow detected\n",
  703. __func__);
  704. return false;
  705. }
  706. if (offset + len > bar_len) {
  707. dev_err(&pci_dev->dev, "%s: bar shorter than cap offset+len\n",
  708. __func__);
  709. return false;
  710. }
  711. region->len = len;
  712. region->addr = (u64) phys_addr + offset;
  713. return true;
  714. }
  715. /*
  716. * virtio_pci_admin_has_dev_parts - Checks whether the device parts
  717. * functionality is supported
  718. * @pdev: VF pci_dev
  719. *
  720. * Returns true on success.
  721. */
  722. bool virtio_pci_admin_has_dev_parts(struct pci_dev *pdev)
  723. {
  724. struct virtio_device *virtio_dev = virtio_pci_vf_get_pf_dev(pdev);
  725. struct virtio_pci_device *vp_dev;
  726. if (!virtio_dev)
  727. return false;
  728. if (!virtio_has_feature(virtio_dev, VIRTIO_F_ADMIN_VQ))
  729. return false;
  730. vp_dev = to_vp_device(virtio_dev);
  731. if (!((vp_dev->admin_vq.supported_cmds & VIRTIO_DEV_PARTS_ADMIN_CMD_BITMAP) ==
  732. VIRTIO_DEV_PARTS_ADMIN_CMD_BITMAP))
  733. return false;
  734. return vp_dev->admin_vq.max_dev_parts_objects;
  735. }
  736. EXPORT_SYMBOL_GPL(virtio_pci_admin_has_dev_parts);
  737. /*
  738. * virtio_pci_admin_mode_set - Sets the mode of a member device
  739. * @pdev: VF pci_dev
  740. * @flags: device mode's flags
  741. *
  742. * Note: caller must serialize access for the given device.
  743. * Returns 0 on success, or negative on failure.
  744. */
  745. int virtio_pci_admin_mode_set(struct pci_dev *pdev, u8 flags)
  746. {
  747. struct virtio_device *virtio_dev = virtio_pci_vf_get_pf_dev(pdev);
  748. struct virtio_admin_cmd_dev_mode_set_data *data;
  749. struct virtio_admin_cmd cmd = {};
  750. struct scatterlist data_sg;
  751. int vf_id;
  752. int ret;
  753. if (!virtio_dev)
  754. return -ENODEV;
  755. vf_id = pci_iov_vf_id(pdev);
  756. if (vf_id < 0)
  757. return vf_id;
  758. data = kzalloc_obj(*data);
  759. if (!data)
  760. return -ENOMEM;
  761. data->flags = flags;
  762. sg_init_one(&data_sg, data, sizeof(*data));
  763. cmd.opcode = cpu_to_le16(VIRTIO_ADMIN_CMD_DEV_MODE_SET);
  764. cmd.group_type = cpu_to_le16(VIRTIO_ADMIN_GROUP_TYPE_SRIOV);
  765. cmd.group_member_id = cpu_to_le64(vf_id + 1);
  766. cmd.data_sg = &data_sg;
  767. ret = vp_modern_admin_cmd_exec(virtio_dev, &cmd);
  768. kfree(data);
  769. return ret;
  770. }
  771. EXPORT_SYMBOL_GPL(virtio_pci_admin_mode_set);
  772. /*
  773. * virtio_pci_admin_obj_create - Creates an object for a given type and operation,
  774. * following the max objects that can be created for that request.
  775. * @pdev: VF pci_dev
  776. * @obj_type: Object type
  777. * @operation_type: Operation type
  778. * @obj_id: Output unique object id
  779. *
  780. * Note: caller must serialize access for the given device.
  781. * Returns 0 on success, or negative on failure.
  782. */
  783. int virtio_pci_admin_obj_create(struct pci_dev *pdev, u16 obj_type, u8 operation_type,
  784. u32 *obj_id)
  785. {
  786. struct virtio_device *virtio_dev = virtio_pci_vf_get_pf_dev(pdev);
  787. u16 data_size = sizeof(struct virtio_admin_cmd_resource_obj_create_data);
  788. struct virtio_admin_cmd_resource_obj_create_data *obj_create_data;
  789. struct virtio_resource_obj_dev_parts obj_dev_parts = {};
  790. struct virtio_pci_admin_vq *avq;
  791. struct virtio_admin_cmd cmd = {};
  792. struct scatterlist data_sg;
  793. void *data;
  794. int id = -1;
  795. int vf_id;
  796. int ret;
  797. if (!virtio_dev)
  798. return -ENODEV;
  799. vf_id = pci_iov_vf_id(pdev);
  800. if (vf_id < 0)
  801. return vf_id;
  802. if (obj_type != VIRTIO_RESOURCE_OBJ_DEV_PARTS)
  803. return -EOPNOTSUPP;
  804. if (operation_type != VIRTIO_RESOURCE_OBJ_DEV_PARTS_TYPE_GET &&
  805. operation_type != VIRTIO_RESOURCE_OBJ_DEV_PARTS_TYPE_SET)
  806. return -EINVAL;
  807. avq = &to_vp_device(virtio_dev)->admin_vq;
  808. if (!avq->max_dev_parts_objects)
  809. return -EOPNOTSUPP;
  810. id = ida_alloc_range(&avq->dev_parts_ida, 0,
  811. avq->max_dev_parts_objects - 1, GFP_KERNEL);
  812. if (id < 0)
  813. return id;
  814. *obj_id = id;
  815. data_size += sizeof(obj_dev_parts);
  816. data = kzalloc(data_size, GFP_KERNEL);
  817. if (!data) {
  818. ret = -ENOMEM;
  819. goto end;
  820. }
  821. obj_create_data = data;
  822. obj_create_data->hdr.type = cpu_to_le16(obj_type);
  823. obj_create_data->hdr.id = cpu_to_le32(*obj_id);
  824. obj_dev_parts.type = operation_type;
  825. memcpy(obj_create_data->resource_obj_specific_data, &obj_dev_parts,
  826. sizeof(obj_dev_parts));
  827. sg_init_one(&data_sg, data, data_size);
  828. cmd.opcode = cpu_to_le16(VIRTIO_ADMIN_CMD_RESOURCE_OBJ_CREATE);
  829. cmd.group_type = cpu_to_le16(VIRTIO_ADMIN_GROUP_TYPE_SRIOV);
  830. cmd.group_member_id = cpu_to_le64(vf_id + 1);
  831. cmd.data_sg = &data_sg;
  832. ret = vp_modern_admin_cmd_exec(virtio_dev, &cmd);
  833. kfree(data);
  834. end:
  835. if (ret)
  836. ida_free(&avq->dev_parts_ida, id);
  837. return ret;
  838. }
  839. EXPORT_SYMBOL_GPL(virtio_pci_admin_obj_create);
  840. /*
  841. * virtio_pci_admin_obj_destroy - Destroys an object of a given type and id
  842. * @pdev: VF pci_dev
  843. * @obj_type: Object type
  844. * @id: Object id
  845. *
  846. * Note: caller must serialize access for the given device.
  847. * Returns 0 on success, or negative on failure.
  848. */
  849. int virtio_pci_admin_obj_destroy(struct pci_dev *pdev, u16 obj_type, u32 id)
  850. {
  851. struct virtio_device *virtio_dev = virtio_pci_vf_get_pf_dev(pdev);
  852. struct virtio_admin_cmd_resource_obj_cmd_hdr *data;
  853. struct virtio_pci_device *vp_dev;
  854. struct virtio_admin_cmd cmd = {};
  855. struct scatterlist data_sg;
  856. int vf_id;
  857. int ret;
  858. if (!virtio_dev)
  859. return -ENODEV;
  860. vf_id = pci_iov_vf_id(pdev);
  861. if (vf_id < 0)
  862. return vf_id;
  863. if (obj_type != VIRTIO_RESOURCE_OBJ_DEV_PARTS)
  864. return -EINVAL;
  865. data = kzalloc_obj(*data);
  866. if (!data)
  867. return -ENOMEM;
  868. data->type = cpu_to_le16(obj_type);
  869. data->id = cpu_to_le32(id);
  870. sg_init_one(&data_sg, data, sizeof(*data));
  871. cmd.opcode = cpu_to_le16(VIRTIO_ADMIN_CMD_RESOURCE_OBJ_DESTROY);
  872. cmd.group_type = cpu_to_le16(VIRTIO_ADMIN_GROUP_TYPE_SRIOV);
  873. cmd.group_member_id = cpu_to_le64(vf_id + 1);
  874. cmd.data_sg = &data_sg;
  875. ret = vp_modern_admin_cmd_exec(virtio_dev, &cmd);
  876. if (!ret) {
  877. vp_dev = to_vp_device(virtio_dev);
  878. ida_free(&vp_dev->admin_vq.dev_parts_ida, id);
  879. }
  880. kfree(data);
  881. return ret;
  882. }
  883. EXPORT_SYMBOL_GPL(virtio_pci_admin_obj_destroy);
  884. /*
  885. * virtio_pci_admin_dev_parts_metadata_get - Gets the metadata of the device parts
  886. * identified by the below attributes.
  887. * @pdev: VF pci_dev
  888. * @obj_type: Object type
  889. * @id: Object id
  890. * @metadata_type: Metadata type
  891. * @out: Upon success holds the output for 'metadata type size'
  892. *
  893. * Note: caller must serialize access for the given device.
  894. * Returns 0 on success, or negative on failure.
  895. */
  896. int virtio_pci_admin_dev_parts_metadata_get(struct pci_dev *pdev, u16 obj_type,
  897. u32 id, u8 metadata_type, u32 *out)
  898. {
  899. struct virtio_device *virtio_dev = virtio_pci_vf_get_pf_dev(pdev);
  900. struct virtio_admin_cmd_dev_parts_metadata_result *result;
  901. struct virtio_admin_cmd_dev_parts_metadata_data *data;
  902. struct scatterlist data_sg, result_sg;
  903. struct virtio_admin_cmd cmd = {};
  904. int vf_id;
  905. int ret;
  906. if (!virtio_dev)
  907. return -ENODEV;
  908. if (metadata_type != VIRTIO_ADMIN_CMD_DEV_PARTS_METADATA_TYPE_SIZE)
  909. return -EOPNOTSUPP;
  910. vf_id = pci_iov_vf_id(pdev);
  911. if (vf_id < 0)
  912. return vf_id;
  913. data = kzalloc_obj(*data);
  914. if (!data)
  915. return -ENOMEM;
  916. result = kzalloc_obj(*result);
  917. if (!result) {
  918. ret = -ENOMEM;
  919. goto end;
  920. }
  921. data->hdr.type = cpu_to_le16(obj_type);
  922. data->hdr.id = cpu_to_le32(id);
  923. data->type = metadata_type;
  924. sg_init_one(&data_sg, data, sizeof(*data));
  925. sg_init_one(&result_sg, result, sizeof(*result));
  926. cmd.opcode = cpu_to_le16(VIRTIO_ADMIN_CMD_DEV_PARTS_METADATA_GET);
  927. cmd.group_type = cpu_to_le16(VIRTIO_ADMIN_GROUP_TYPE_SRIOV);
  928. cmd.group_member_id = cpu_to_le64(vf_id + 1);
  929. cmd.data_sg = &data_sg;
  930. cmd.result_sg = &result_sg;
  931. ret = vp_modern_admin_cmd_exec(virtio_dev, &cmd);
  932. if (!ret)
  933. *out = le32_to_cpu(result->parts_size.size);
  934. kfree(result);
  935. end:
  936. kfree(data);
  937. return ret;
  938. }
  939. EXPORT_SYMBOL_GPL(virtio_pci_admin_dev_parts_metadata_get);
  940. /*
  941. * virtio_pci_admin_dev_parts_get - Gets the device parts identified by the below attributes.
  942. * @pdev: VF pci_dev
  943. * @obj_type: Object type
  944. * @id: Object id
  945. * @get_type: Get type
  946. * @res_sg: Upon success holds the output result data
  947. * @res_size: Upon success holds the output result size
  948. *
  949. * Note: caller must serialize access for the given device.
  950. * Returns 0 on success, or negative on failure.
  951. */
  952. int virtio_pci_admin_dev_parts_get(struct pci_dev *pdev, u16 obj_type, u32 id,
  953. u8 get_type, struct scatterlist *res_sg,
  954. u32 *res_size)
  955. {
  956. struct virtio_device *virtio_dev = virtio_pci_vf_get_pf_dev(pdev);
  957. struct virtio_admin_cmd_dev_parts_get_data *data;
  958. struct scatterlist data_sg;
  959. struct virtio_admin_cmd cmd = {};
  960. int vf_id;
  961. int ret;
  962. if (!virtio_dev)
  963. return -ENODEV;
  964. if (get_type != VIRTIO_ADMIN_CMD_DEV_PARTS_GET_TYPE_ALL)
  965. return -EOPNOTSUPP;
  966. vf_id = pci_iov_vf_id(pdev);
  967. if (vf_id < 0)
  968. return vf_id;
  969. data = kzalloc_obj(*data);
  970. if (!data)
  971. return -ENOMEM;
  972. data->hdr.type = cpu_to_le16(obj_type);
  973. data->hdr.id = cpu_to_le32(id);
  974. data->type = get_type;
  975. sg_init_one(&data_sg, data, sizeof(*data));
  976. cmd.opcode = cpu_to_le16(VIRTIO_ADMIN_CMD_DEV_PARTS_GET);
  977. cmd.group_type = cpu_to_le16(VIRTIO_ADMIN_GROUP_TYPE_SRIOV);
  978. cmd.group_member_id = cpu_to_le64(vf_id + 1);
  979. cmd.data_sg = &data_sg;
  980. cmd.result_sg = res_sg;
  981. ret = vp_modern_admin_cmd_exec(virtio_dev, &cmd);
  982. if (!ret)
  983. *res_size = cmd.result_sg_size;
  984. kfree(data);
  985. return ret;
  986. }
  987. EXPORT_SYMBOL_GPL(virtio_pci_admin_dev_parts_get);
  988. /*
  989. * virtio_pci_admin_dev_parts_set - Sets the device parts identified by the below attributes.
  990. * @pdev: VF pci_dev
  991. * @data_sg: The device parts data, its layout follows struct virtio_admin_cmd_dev_parts_set_data
  992. *
  993. * Note: caller must serialize access for the given device.
  994. * Returns 0 on success, or negative on failure.
  995. */
  996. int virtio_pci_admin_dev_parts_set(struct pci_dev *pdev, struct scatterlist *data_sg)
  997. {
  998. struct virtio_device *virtio_dev = virtio_pci_vf_get_pf_dev(pdev);
  999. struct virtio_admin_cmd cmd = {};
  1000. int vf_id;
  1001. if (!virtio_dev)
  1002. return -ENODEV;
  1003. vf_id = pci_iov_vf_id(pdev);
  1004. if (vf_id < 0)
  1005. return vf_id;
  1006. cmd.opcode = cpu_to_le16(VIRTIO_ADMIN_CMD_DEV_PARTS_SET);
  1007. cmd.group_type = cpu_to_le16(VIRTIO_ADMIN_GROUP_TYPE_SRIOV);
  1008. cmd.group_member_id = cpu_to_le64(vf_id + 1);
  1009. cmd.data_sg = data_sg;
  1010. return vp_modern_admin_cmd_exec(virtio_dev, &cmd);
  1011. }
  1012. EXPORT_SYMBOL_GPL(virtio_pci_admin_dev_parts_set);
  1013. static const struct virtio_config_ops virtio_pci_config_nodev_ops = {
  1014. .get = NULL,
  1015. .set = NULL,
  1016. .generation = vp_generation,
  1017. .get_status = vp_get_status,
  1018. .set_status = vp_set_status,
  1019. .reset = vp_reset,
  1020. .find_vqs = vp_modern_find_vqs,
  1021. .del_vqs = vp_del_vqs,
  1022. .synchronize_cbs = vp_synchronize_vectors,
  1023. .get_extended_features = vp_get_features,
  1024. .finalize_features = vp_finalize_features,
  1025. .bus_name = vp_bus_name,
  1026. .set_vq_affinity = vp_set_vq_affinity,
  1027. .get_vq_affinity = vp_get_vq_affinity,
  1028. .get_shm_region = vp_get_shm_region,
  1029. .disable_vq_and_reset = vp_modern_disable_vq_and_reset,
  1030. .enable_vq_after_reset = vp_modern_enable_vq_after_reset,
  1031. };
  1032. static const struct virtio_config_ops virtio_pci_config_ops = {
  1033. .get = vp_get,
  1034. .set = vp_set,
  1035. .generation = vp_generation,
  1036. .get_status = vp_get_status,
  1037. .set_status = vp_set_status,
  1038. .reset = vp_reset,
  1039. .find_vqs = vp_modern_find_vqs,
  1040. .del_vqs = vp_del_vqs,
  1041. .synchronize_cbs = vp_synchronize_vectors,
  1042. .get_extended_features = vp_get_features,
  1043. .finalize_features = vp_finalize_features,
  1044. .bus_name = vp_bus_name,
  1045. .set_vq_affinity = vp_set_vq_affinity,
  1046. .get_vq_affinity = vp_get_vq_affinity,
  1047. .get_shm_region = vp_get_shm_region,
  1048. .disable_vq_and_reset = vp_modern_disable_vq_and_reset,
  1049. .enable_vq_after_reset = vp_modern_enable_vq_after_reset,
  1050. };
  1051. /* the PCI probing function */
  1052. int virtio_pci_modern_probe(struct virtio_pci_device *vp_dev)
  1053. {
  1054. struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
  1055. struct pci_dev *pci_dev = vp_dev->pci_dev;
  1056. int err;
  1057. mdev->pci_dev = pci_dev;
  1058. err = vp_modern_probe(mdev);
  1059. if (err)
  1060. return err;
  1061. if (mdev->device)
  1062. vp_dev->vdev.config = &virtio_pci_config_ops;
  1063. else
  1064. vp_dev->vdev.config = &virtio_pci_config_nodev_ops;
  1065. vp_dev->config_vector = vp_config_vector;
  1066. vp_dev->setup_vq = setup_vq;
  1067. vp_dev->del_vq = del_vq;
  1068. vp_dev->avq_index = vp_avq_index;
  1069. vp_dev->isr = mdev->isr;
  1070. vp_dev->vdev.id = mdev->id;
  1071. spin_lock_init(&vp_dev->admin_vq.lock);
  1072. return 0;
  1073. }
  1074. void virtio_pci_modern_remove(struct virtio_pci_device *vp_dev)
  1075. {
  1076. struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
  1077. vp_modern_remove(mdev);
  1078. }