adf_dev_mgr.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /*
  2. This file is provided under a dual BSD/GPLv2 license. When using or
  3. redistributing this file, you may do so under either license.
  4. GPL LICENSE SUMMARY
  5. Copyright(c) 2014 Intel Corporation.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of version 2 of the GNU General Public License as
  8. published by the Free Software Foundation.
  9. This program is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. Contact Information:
  14. [email protected]
  15. BSD LICENSE
  16. Copyright(c) 2014 Intel Corporation.
  17. Redistribution and use in source and binary forms, with or without
  18. modification, are permitted provided that the following conditions
  19. are met:
  20. * Redistributions of source code must retain the above copyright
  21. notice, this list of conditions and the following disclaimer.
  22. * Redistributions in binary form must reproduce the above copyright
  23. notice, this list of conditions and the following disclaimer in
  24. the documentation and/or other materials provided with the
  25. distribution.
  26. * Neither the name of Intel Corporation nor the names of its
  27. contributors may be used to endorse or promote products derived
  28. from this software without specific prior written permission.
  29. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  30. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  31. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  32. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  33. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  34. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  35. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  36. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  37. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  38. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  39. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  40. */
  41. #include <linux/mutex.h>
  42. #include <linux/list.h>
  43. #include "adf_cfg.h"
  44. #include "adf_common_drv.h"
  45. static LIST_HEAD(accel_table);
  46. static LIST_HEAD(vfs_table);
  47. static DEFINE_MUTEX(table_lock);
  48. static uint32_t num_devices;
  49. static u8 id_map[ADF_MAX_DEVICES];
  50. struct vf_id_map {
  51. u32 bdf;
  52. u32 id;
  53. u32 fake_id;
  54. bool attached;
  55. struct list_head list;
  56. };
  57. static int adf_get_vf_id(struct adf_accel_dev *vf)
  58. {
  59. return ((7 * (PCI_SLOT(accel_to_pci_dev(vf)->devfn) - 1)) +
  60. PCI_FUNC(accel_to_pci_dev(vf)->devfn) +
  61. (PCI_SLOT(accel_to_pci_dev(vf)->devfn) - 1));
  62. }
  63. static int adf_get_vf_num(struct adf_accel_dev *vf)
  64. {
  65. return (accel_to_pci_dev(vf)->bus->number << 8) | adf_get_vf_id(vf);
  66. }
  67. static struct vf_id_map *adf_find_vf(u32 bdf)
  68. {
  69. struct list_head *itr;
  70. list_for_each(itr, &vfs_table) {
  71. struct vf_id_map *ptr =
  72. list_entry(itr, struct vf_id_map, list);
  73. if (ptr->bdf == bdf)
  74. return ptr;
  75. }
  76. return NULL;
  77. }
  78. static int adf_get_vf_real_id(u32 fake)
  79. {
  80. struct list_head *itr;
  81. list_for_each(itr, &vfs_table) {
  82. struct vf_id_map *ptr =
  83. list_entry(itr, struct vf_id_map, list);
  84. if (ptr->fake_id == fake)
  85. return ptr->id;
  86. }
  87. return -1;
  88. }
  89. /**
  90. * adf_clean_vf_map() - Cleans VF id mapings
  91. *
  92. * Function cleans internal ids for virtual functions.
  93. * @vf: flag indicating whether mappings is cleaned
  94. * for vfs only or for vfs and pfs
  95. */
  96. void adf_clean_vf_map(bool vf)
  97. {
  98. struct vf_id_map *map;
  99. struct list_head *ptr, *tmp;
  100. mutex_lock(&table_lock);
  101. list_for_each_safe(ptr, tmp, &vfs_table) {
  102. map = list_entry(ptr, struct vf_id_map, list);
  103. if (map->bdf != -1) {
  104. id_map[map->id] = 0;
  105. num_devices--;
  106. }
  107. if (vf && map->bdf == -1)
  108. continue;
  109. list_del(ptr);
  110. kfree(map);
  111. }
  112. mutex_unlock(&table_lock);
  113. }
  114. EXPORT_SYMBOL_GPL(adf_clean_vf_map);
  115. /**
  116. * adf_devmgr_update_class_index() - Update internal index
  117. * @hw_data: Pointer to internal device data.
  118. *
  119. * Function updates internal dev index for VFs
  120. */
  121. void adf_devmgr_update_class_index(struct adf_hw_device_data *hw_data)
  122. {
  123. struct adf_hw_device_class *class = hw_data->dev_class;
  124. struct list_head *itr;
  125. int i = 0;
  126. list_for_each(itr, &accel_table) {
  127. struct adf_accel_dev *ptr =
  128. list_entry(itr, struct adf_accel_dev, list);
  129. if (ptr->hw_device->dev_class == class)
  130. ptr->hw_device->instance_id = i++;
  131. if (i == class->instances)
  132. break;
  133. }
  134. }
  135. EXPORT_SYMBOL_GPL(adf_devmgr_update_class_index);
  136. static unsigned int adf_find_free_id(void)
  137. {
  138. unsigned int i;
  139. for (i = 0; i < ADF_MAX_DEVICES; i++) {
  140. if (!id_map[i]) {
  141. id_map[i] = 1;
  142. return i;
  143. }
  144. }
  145. return ADF_MAX_DEVICES + 1;
  146. }
  147. /**
  148. * adf_devmgr_add_dev() - Add accel_dev to the acceleration framework
  149. * @accel_dev: Pointer to acceleration device.
  150. * @pf: Corresponding PF if the accel_dev is a VF
  151. *
  152. * Function adds acceleration device to the acceleration framework.
  153. * To be used by QAT device specific drivers.
  154. *
  155. * Return: 0 on success, error code otherwise.
  156. */
  157. int adf_devmgr_add_dev(struct adf_accel_dev *accel_dev,
  158. struct adf_accel_dev *pf)
  159. {
  160. struct list_head *itr;
  161. int ret = 0;
  162. if (num_devices == ADF_MAX_DEVICES) {
  163. dev_err(&GET_DEV(accel_dev), "Only support up to %d devices\n",
  164. ADF_MAX_DEVICES);
  165. return -EFAULT;
  166. }
  167. mutex_lock(&table_lock);
  168. atomic_set(&accel_dev->ref_count, 0);
  169. /* PF on host or VF on guest */
  170. if (!accel_dev->is_vf || (accel_dev->is_vf && !pf)) {
  171. struct vf_id_map *map;
  172. list_for_each(itr, &accel_table) {
  173. struct adf_accel_dev *ptr =
  174. list_entry(itr, struct adf_accel_dev, list);
  175. if (ptr == accel_dev) {
  176. ret = -EEXIST;
  177. goto unlock;
  178. }
  179. }
  180. list_add_tail(&accel_dev->list, &accel_table);
  181. accel_dev->accel_id = adf_find_free_id();
  182. if (accel_dev->accel_id > ADF_MAX_DEVICES) {
  183. ret = -EFAULT;
  184. goto unlock;
  185. }
  186. num_devices++;
  187. map = kzalloc(sizeof(*map), GFP_KERNEL);
  188. if (!map) {
  189. ret = -ENOMEM;
  190. goto unlock;
  191. }
  192. map->bdf = ~0;
  193. map->id = accel_dev->accel_id;
  194. map->fake_id = map->id;
  195. map->attached = true;
  196. list_add_tail(&map->list, &vfs_table);
  197. } else if (accel_dev->is_vf && pf) {
  198. /* VF on host */
  199. struct adf_accel_vf_info *vf_info;
  200. struct vf_id_map *map;
  201. vf_info = pf->pf.vf_info + adf_get_vf_id(accel_dev);
  202. map = adf_find_vf(adf_get_vf_num(accel_dev));
  203. if (map) {
  204. struct vf_id_map *next;
  205. accel_dev->accel_id = map->id;
  206. list_add_tail(&accel_dev->list, &accel_table);
  207. map->fake_id++;
  208. map->attached = true;
  209. next = list_next_entry(map, list);
  210. while (next && &next->list != &vfs_table) {
  211. next->fake_id++;
  212. next = list_next_entry(next, list);
  213. }
  214. ret = 0;
  215. goto unlock;
  216. }
  217. map = kzalloc(sizeof(*map), GFP_KERNEL);
  218. if (!map) {
  219. ret = -ENOMEM;
  220. goto unlock;
  221. }
  222. accel_dev->accel_id = adf_find_free_id();
  223. if (accel_dev->accel_id > ADF_MAX_DEVICES) {
  224. kfree(map);
  225. ret = -EFAULT;
  226. goto unlock;
  227. }
  228. num_devices++;
  229. list_add_tail(&accel_dev->list, &accel_table);
  230. map->bdf = adf_get_vf_num(accel_dev);
  231. map->id = accel_dev->accel_id;
  232. map->fake_id = map->id;
  233. map->attached = true;
  234. list_add_tail(&map->list, &vfs_table);
  235. }
  236. unlock:
  237. mutex_unlock(&table_lock);
  238. return ret;
  239. }
  240. EXPORT_SYMBOL_GPL(adf_devmgr_add_dev);
  241. struct list_head *adf_devmgr_get_head(void)
  242. {
  243. return &accel_table;
  244. }
  245. /**
  246. * adf_devmgr_rm_dev() - Remove accel_dev from the acceleration framework.
  247. * @accel_dev: Pointer to acceleration device.
  248. * @pf: Corresponding PF if the accel_dev is a VF
  249. *
  250. * Function removes acceleration device from the acceleration framework.
  251. * To be used by QAT device specific drivers.
  252. *
  253. * Return: void
  254. */
  255. void adf_devmgr_rm_dev(struct adf_accel_dev *accel_dev,
  256. struct adf_accel_dev *pf)
  257. {
  258. mutex_lock(&table_lock);
  259. if (!accel_dev->is_vf || (accel_dev->is_vf && !pf)) {
  260. id_map[accel_dev->accel_id] = 0;
  261. num_devices--;
  262. } else if (accel_dev->is_vf && pf) {
  263. struct vf_id_map *map, *next;
  264. map = adf_find_vf(adf_get_vf_num(accel_dev));
  265. if (!map) {
  266. dev_err(&GET_DEV(accel_dev), "Failed to find VF map\n");
  267. goto unlock;
  268. }
  269. map->fake_id--;
  270. map->attached = false;
  271. next = list_next_entry(map, list);
  272. while (next && &next->list != &vfs_table) {
  273. next->fake_id--;
  274. next = list_next_entry(next, list);
  275. }
  276. }
  277. unlock:
  278. list_del(&accel_dev->list);
  279. mutex_unlock(&table_lock);
  280. }
  281. EXPORT_SYMBOL_GPL(adf_devmgr_rm_dev);
  282. struct adf_accel_dev *adf_devmgr_get_first(void)
  283. {
  284. struct adf_accel_dev *dev = NULL;
  285. if (!list_empty(&accel_table))
  286. dev = list_first_entry(&accel_table, struct adf_accel_dev,
  287. list);
  288. return dev;
  289. }
  290. /**
  291. * adf_devmgr_pci_to_accel_dev() - Get accel_dev associated with the pci_dev.
  292. * @accel_dev: Pointer to pci device.
  293. *
  294. * Function returns acceleration device associated with the given pci device.
  295. * To be used by QAT device specific drivers.
  296. *
  297. * Return: pointer to accel_dev or NULL if not found.
  298. */
  299. struct adf_accel_dev *adf_devmgr_pci_to_accel_dev(struct pci_dev *pci_dev)
  300. {
  301. struct list_head *itr;
  302. mutex_lock(&table_lock);
  303. list_for_each(itr, &accel_table) {
  304. struct adf_accel_dev *ptr =
  305. list_entry(itr, struct adf_accel_dev, list);
  306. if (ptr->accel_pci_dev.pci_dev == pci_dev) {
  307. mutex_unlock(&table_lock);
  308. return ptr;
  309. }
  310. }
  311. mutex_unlock(&table_lock);
  312. return NULL;
  313. }
  314. EXPORT_SYMBOL_GPL(adf_devmgr_pci_to_accel_dev);
  315. struct adf_accel_dev *adf_devmgr_get_dev_by_id(uint32_t id)
  316. {
  317. struct list_head *itr;
  318. int real_id;
  319. mutex_lock(&table_lock);
  320. real_id = adf_get_vf_real_id(id);
  321. if (real_id < 0)
  322. goto unlock;
  323. id = real_id;
  324. list_for_each(itr, &accel_table) {
  325. struct adf_accel_dev *ptr =
  326. list_entry(itr, struct adf_accel_dev, list);
  327. if (ptr->accel_id == id) {
  328. mutex_unlock(&table_lock);
  329. return ptr;
  330. }
  331. }
  332. unlock:
  333. mutex_unlock(&table_lock);
  334. return NULL;
  335. }
  336. int adf_devmgr_verify_id(uint32_t id)
  337. {
  338. if (id == ADF_CFG_ALL_DEVICES)
  339. return 0;
  340. if (adf_devmgr_get_dev_by_id(id))
  341. return 0;
  342. return -ENODEV;
  343. }
  344. static int adf_get_num_dettached_vfs(void)
  345. {
  346. struct list_head *itr;
  347. int vfs = 0;
  348. mutex_lock(&table_lock);
  349. list_for_each(itr, &vfs_table) {
  350. struct vf_id_map *ptr =
  351. list_entry(itr, struct vf_id_map, list);
  352. if (ptr->bdf != ~0 && !ptr->attached)
  353. vfs++;
  354. }
  355. mutex_unlock(&table_lock);
  356. return vfs;
  357. }
  358. void adf_devmgr_get_num_dev(uint32_t *num)
  359. {
  360. *num = num_devices - adf_get_num_dettached_vfs();
  361. }
  362. /**
  363. * adf_dev_in_use() - Check whether accel_dev is currently in use
  364. * @accel_dev: Pointer to acceleration device.
  365. *
  366. * To be used by QAT device specific drivers.
  367. *
  368. * Return: 1 when device is in use, 0 otherwise.
  369. */
  370. int adf_dev_in_use(struct adf_accel_dev *accel_dev)
  371. {
  372. return atomic_read(&accel_dev->ref_count) != 0;
  373. }
  374. EXPORT_SYMBOL_GPL(adf_dev_in_use);
  375. /**
  376. * adf_dev_get() - Increment accel_dev reference count
  377. * @accel_dev: Pointer to acceleration device.
  378. *
  379. * Increment the accel_dev refcount and if this is the first time
  380. * incrementing it during this period the accel_dev is in use,
  381. * increment the module refcount too.
  382. * To be used by QAT device specific drivers.
  383. *
  384. * Return: 0 when successful, EFAULT when fail to bump module refcount
  385. */
  386. int adf_dev_get(struct adf_accel_dev *accel_dev)
  387. {
  388. if (atomic_add_return(1, &accel_dev->ref_count) == 1)
  389. if (!try_module_get(accel_dev->owner))
  390. return -EFAULT;
  391. return 0;
  392. }
  393. EXPORT_SYMBOL_GPL(adf_dev_get);
  394. /**
  395. * adf_dev_put() - Decrement accel_dev reference count
  396. * @accel_dev: Pointer to acceleration device.
  397. *
  398. * Decrement the accel_dev refcount and if this is the last time
  399. * decrementing it during this period the accel_dev is in use,
  400. * decrement the module refcount too.
  401. * To be used by QAT device specific drivers.
  402. *
  403. * Return: void
  404. */
  405. void adf_dev_put(struct adf_accel_dev *accel_dev)
  406. {
  407. if (atomic_sub_return(1, &accel_dev->ref_count) == 0)
  408. module_put(accel_dev->owner);
  409. }
  410. EXPORT_SYMBOL_GPL(adf_dev_put);
  411. /**
  412. * adf_devmgr_in_reset() - Check whether device is in reset
  413. * @accel_dev: Pointer to acceleration device.
  414. *
  415. * To be used by QAT device specific drivers.
  416. *
  417. * Return: 1 when the device is being reset, 0 otherwise.
  418. */
  419. int adf_devmgr_in_reset(struct adf_accel_dev *accel_dev)
  420. {
  421. return test_bit(ADF_STATUS_RESTARTING, &accel_dev->status);
  422. }
  423. EXPORT_SYMBOL_GPL(adf_devmgr_in_reset);
  424. /**
  425. * adf_dev_started() - Check whether device has started
  426. * @accel_dev: Pointer to acceleration device.
  427. *
  428. * To be used by QAT device specific drivers.
  429. *
  430. * Return: 1 when the device has started, 0 otherwise
  431. */
  432. int adf_dev_started(struct adf_accel_dev *accel_dev)
  433. {
  434. return test_bit(ADF_STATUS_STARTED, &accel_dev->status);
  435. }
  436. EXPORT_SYMBOL_GPL(adf_dev_started);