adf_cfg.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. qat-linux@intel.com
  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/slab.h>
  43. #include <linux/list.h>
  44. #include <linux/seq_file.h>
  45. #include "adf_accel_devices.h"
  46. #include "adf_cfg.h"
  47. #include "adf_common_drv.h"
  48. static DEFINE_MUTEX(qat_cfg_read_lock);
  49. static void *qat_dev_cfg_start(struct seq_file *sfile, loff_t *pos)
  50. {
  51. struct adf_cfg_device_data *dev_cfg = sfile->private;
  52. mutex_lock(&qat_cfg_read_lock);
  53. return seq_list_start(&dev_cfg->sec_list, *pos);
  54. }
  55. static int qat_dev_cfg_show(struct seq_file *sfile, void *v)
  56. {
  57. struct list_head *list;
  58. struct adf_cfg_section *sec =
  59. list_entry(v, struct adf_cfg_section, list);
  60. seq_printf(sfile, "[%s]\n", sec->name);
  61. list_for_each(list, &sec->param_head) {
  62. struct adf_cfg_key_val *ptr =
  63. list_entry(list, struct adf_cfg_key_val, list);
  64. seq_printf(sfile, "%s = %s\n", ptr->key, ptr->val);
  65. }
  66. return 0;
  67. }
  68. static void *qat_dev_cfg_next(struct seq_file *sfile, void *v, loff_t *pos)
  69. {
  70. struct adf_cfg_device_data *dev_cfg = sfile->private;
  71. return seq_list_next(v, &dev_cfg->sec_list, pos);
  72. }
  73. static void qat_dev_cfg_stop(struct seq_file *sfile, void *v)
  74. {
  75. mutex_unlock(&qat_cfg_read_lock);
  76. }
  77. static const struct seq_operations qat_dev_cfg_sops = {
  78. .start = qat_dev_cfg_start,
  79. .next = qat_dev_cfg_next,
  80. .stop = qat_dev_cfg_stop,
  81. .show = qat_dev_cfg_show
  82. };
  83. static int qat_dev_cfg_open(struct inode *inode, struct file *file)
  84. {
  85. int ret = seq_open(file, &qat_dev_cfg_sops);
  86. if (!ret) {
  87. struct seq_file *seq_f = file->private_data;
  88. seq_f->private = inode->i_private;
  89. }
  90. return ret;
  91. }
  92. static const struct file_operations qat_dev_cfg_fops = {
  93. .open = qat_dev_cfg_open,
  94. .read = seq_read,
  95. .llseek = seq_lseek,
  96. .release = seq_release
  97. };
  98. /**
  99. * adf_cfg_dev_add() - Create an acceleration device configuration table.
  100. * @accel_dev: Pointer to acceleration device.
  101. *
  102. * Function creates a configuration table for the given acceleration device.
  103. * The table stores device specific config values.
  104. * To be used by QAT device specific drivers.
  105. *
  106. * Return: 0 on success, error code otherwise.
  107. */
  108. int adf_cfg_dev_add(struct adf_accel_dev *accel_dev)
  109. {
  110. struct adf_cfg_device_data *dev_cfg_data;
  111. dev_cfg_data = kzalloc(sizeof(*dev_cfg_data), GFP_KERNEL);
  112. if (!dev_cfg_data)
  113. return -ENOMEM;
  114. INIT_LIST_HEAD(&dev_cfg_data->sec_list);
  115. init_rwsem(&dev_cfg_data->lock);
  116. accel_dev->cfg = dev_cfg_data;
  117. /* accel_dev->debugfs_dir should always be non-NULL here */
  118. dev_cfg_data->debug = debugfs_create_file("dev_cfg", S_IRUSR,
  119. accel_dev->debugfs_dir,
  120. dev_cfg_data,
  121. &qat_dev_cfg_fops);
  122. if (!dev_cfg_data->debug) {
  123. dev_err(&GET_DEV(accel_dev),
  124. "Failed to create qat cfg debugfs entry.\n");
  125. kfree(dev_cfg_data);
  126. accel_dev->cfg = NULL;
  127. return -EFAULT;
  128. }
  129. return 0;
  130. }
  131. EXPORT_SYMBOL_GPL(adf_cfg_dev_add);
  132. static void adf_cfg_section_del_all(struct list_head *head);
  133. void adf_cfg_del_all(struct adf_accel_dev *accel_dev)
  134. {
  135. struct adf_cfg_device_data *dev_cfg_data = accel_dev->cfg;
  136. down_write(&dev_cfg_data->lock);
  137. adf_cfg_section_del_all(&dev_cfg_data->sec_list);
  138. up_write(&dev_cfg_data->lock);
  139. clear_bit(ADF_STATUS_CONFIGURED, &accel_dev->status);
  140. }
  141. /**
  142. * adf_cfg_dev_remove() - Clears acceleration device configuration table.
  143. * @accel_dev: Pointer to acceleration device.
  144. *
  145. * Function removes configuration table from the given acceleration device
  146. * and frees all allocated memory.
  147. * To be used by QAT device specific drivers.
  148. *
  149. * Return: void
  150. */
  151. void adf_cfg_dev_remove(struct adf_accel_dev *accel_dev)
  152. {
  153. struct adf_cfg_device_data *dev_cfg_data = accel_dev->cfg;
  154. if (!dev_cfg_data)
  155. return;
  156. down_write(&dev_cfg_data->lock);
  157. adf_cfg_section_del_all(&dev_cfg_data->sec_list);
  158. up_write(&dev_cfg_data->lock);
  159. debugfs_remove(dev_cfg_data->debug);
  160. kfree(dev_cfg_data);
  161. accel_dev->cfg = NULL;
  162. }
  163. EXPORT_SYMBOL_GPL(adf_cfg_dev_remove);
  164. static void adf_cfg_keyval_add(struct adf_cfg_key_val *new,
  165. struct adf_cfg_section *sec)
  166. {
  167. list_add_tail(&new->list, &sec->param_head);
  168. }
  169. static void adf_cfg_keyval_del_all(struct list_head *head)
  170. {
  171. struct list_head *list_ptr, *tmp;
  172. list_for_each_prev_safe(list_ptr, tmp, head) {
  173. struct adf_cfg_key_val *ptr =
  174. list_entry(list_ptr, struct adf_cfg_key_val, list);
  175. list_del(list_ptr);
  176. kfree(ptr);
  177. }
  178. }
  179. static void adf_cfg_section_del_all(struct list_head *head)
  180. {
  181. struct adf_cfg_section *ptr;
  182. struct list_head *list, *tmp;
  183. list_for_each_prev_safe(list, tmp, head) {
  184. ptr = list_entry(list, struct adf_cfg_section, list);
  185. adf_cfg_keyval_del_all(&ptr->param_head);
  186. list_del(list);
  187. kfree(ptr);
  188. }
  189. }
  190. static struct adf_cfg_key_val *adf_cfg_key_value_find(struct adf_cfg_section *s,
  191. const char *key)
  192. {
  193. struct list_head *list;
  194. list_for_each(list, &s->param_head) {
  195. struct adf_cfg_key_val *ptr =
  196. list_entry(list, struct adf_cfg_key_val, list);
  197. if (!strcmp(ptr->key, key))
  198. return ptr;
  199. }
  200. return NULL;
  201. }
  202. static struct adf_cfg_section *adf_cfg_sec_find(struct adf_accel_dev *accel_dev,
  203. const char *sec_name)
  204. {
  205. struct adf_cfg_device_data *cfg = accel_dev->cfg;
  206. struct list_head *list;
  207. list_for_each(list, &cfg->sec_list) {
  208. struct adf_cfg_section *ptr =
  209. list_entry(list, struct adf_cfg_section, list);
  210. if (!strcmp(ptr->name, sec_name))
  211. return ptr;
  212. }
  213. return NULL;
  214. }
  215. static int adf_cfg_key_val_get(struct adf_accel_dev *accel_dev,
  216. const char *sec_name,
  217. const char *key_name,
  218. char *val)
  219. {
  220. struct adf_cfg_section *sec = adf_cfg_sec_find(accel_dev, sec_name);
  221. struct adf_cfg_key_val *keyval = NULL;
  222. if (sec)
  223. keyval = adf_cfg_key_value_find(sec, key_name);
  224. if (keyval) {
  225. memcpy(val, keyval->val, ADF_CFG_MAX_VAL_LEN_IN_BYTES);
  226. return 0;
  227. }
  228. return -1;
  229. }
  230. /**
  231. * adf_cfg_add_key_value_param() - Add key-value config entry to config table.
  232. * @accel_dev: Pointer to acceleration device.
  233. * @section_name: Name of the section where the param will be added
  234. * @key: The key string
  235. * @val: Value pain for the given @key
  236. * @type: Type - string, int or address
  237. *
  238. * Function adds configuration key - value entry in the appropriate section
  239. * in the given acceleration device
  240. * To be used by QAT device specific drivers.
  241. *
  242. * Return: 0 on success, error code otherwise.
  243. */
  244. int adf_cfg_add_key_value_param(struct adf_accel_dev *accel_dev,
  245. const char *section_name,
  246. const char *key, const void *val,
  247. enum adf_cfg_val_type type)
  248. {
  249. struct adf_cfg_device_data *cfg = accel_dev->cfg;
  250. struct adf_cfg_key_val *key_val;
  251. struct adf_cfg_section *section = adf_cfg_sec_find(accel_dev,
  252. section_name);
  253. if (!section)
  254. return -EFAULT;
  255. key_val = kzalloc(sizeof(*key_val), GFP_KERNEL);
  256. if (!key_val)
  257. return -ENOMEM;
  258. INIT_LIST_HEAD(&key_val->list);
  259. strlcpy(key_val->key, key, sizeof(key_val->key));
  260. if (type == ADF_DEC) {
  261. snprintf(key_val->val, ADF_CFG_MAX_VAL_LEN_IN_BYTES,
  262. "%ld", (*((long *)val)));
  263. } else if (type == ADF_STR) {
  264. strlcpy(key_val->val, (char *)val, sizeof(key_val->val));
  265. } else if (type == ADF_HEX) {
  266. snprintf(key_val->val, ADF_CFG_MAX_VAL_LEN_IN_BYTES,
  267. "0x%lx", (unsigned long)val);
  268. } else {
  269. dev_err(&GET_DEV(accel_dev), "Unknown type given.\n");
  270. kfree(key_val);
  271. return -1;
  272. }
  273. key_val->type = type;
  274. down_write(&cfg->lock);
  275. adf_cfg_keyval_add(key_val, section);
  276. up_write(&cfg->lock);
  277. return 0;
  278. }
  279. EXPORT_SYMBOL_GPL(adf_cfg_add_key_value_param);
  280. /**
  281. * adf_cfg_section_add() - Add config section entry to config table.
  282. * @accel_dev: Pointer to acceleration device.
  283. * @name: Name of the section
  284. *
  285. * Function adds configuration section where key - value entries
  286. * will be stored.
  287. * To be used by QAT device specific drivers.
  288. *
  289. * Return: 0 on success, error code otherwise.
  290. */
  291. int adf_cfg_section_add(struct adf_accel_dev *accel_dev, const char *name)
  292. {
  293. struct adf_cfg_device_data *cfg = accel_dev->cfg;
  294. struct adf_cfg_section *sec = adf_cfg_sec_find(accel_dev, name);
  295. if (sec)
  296. return 0;
  297. sec = kzalloc(sizeof(*sec), GFP_KERNEL);
  298. if (!sec)
  299. return -ENOMEM;
  300. strlcpy(sec->name, name, sizeof(sec->name));
  301. INIT_LIST_HEAD(&sec->param_head);
  302. down_write(&cfg->lock);
  303. list_add_tail(&sec->list, &cfg->sec_list);
  304. up_write(&cfg->lock);
  305. return 0;
  306. }
  307. EXPORT_SYMBOL_GPL(adf_cfg_section_add);
  308. int adf_cfg_get_param_value(struct adf_accel_dev *accel_dev,
  309. const char *section, const char *name,
  310. char *value)
  311. {
  312. struct adf_cfg_device_data *cfg = accel_dev->cfg;
  313. int ret;
  314. down_read(&cfg->lock);
  315. ret = adf_cfg_key_val_get(accel_dev, section, name, value);
  316. up_read(&cfg->lock);
  317. return ret;
  318. }