Greg Kroah-Hartman | d5d1903 | 2014-08-11 19:03:20 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Greybus gbuf handling |
| 3 | * |
| 4 | * Copyright 2014 Google Inc. |
| 5 | * |
| 6 | * Released under the GPLv2 only. |
| 7 | */ |
| 8 | |
| 9 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 10 | |
| 11 | #include <linux/types.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/moduleparam.h> |
| 14 | #include <linux/kernel.h> |
Greg Kroah-Hartman | a39879f | 2014-09-06 16:57:36 -0700 | [diff] [blame] | 15 | #include <linux/kref.h> |
Greg Kroah-Hartman | d5d1903 | 2014-08-11 19:03:20 +0800 | [diff] [blame] | 16 | #include <linux/device.h> |
Greg Kroah-Hartman | a39879f | 2014-09-06 16:57:36 -0700 | [diff] [blame] | 17 | #include <linux/slab.h> |
Greg Kroah-Hartman | d5d1903 | 2014-08-11 19:03:20 +0800 | [diff] [blame] | 18 | |
| 19 | #include "greybus.h" |
| 20 | |
Greg Kroah-Hartman | 3e7736e | 2014-09-21 17:34:28 -0700 | [diff] [blame] | 21 | static struct kmem_cache *gbuf_head_cache; |
| 22 | |
Greg Kroah-Hartman | a39879f | 2014-09-06 16:57:36 -0700 | [diff] [blame] | 23 | /** |
| 24 | * greybus_alloc_gbuf - allocate a greybus buffer |
| 25 | * |
Alex Elder | 778c69c | 2014-09-22 19:19:03 -0500 | [diff] [blame] | 26 | * @gmod: greybus device that wants to allocate this |
Greg Kroah-Hartman | a39879f | 2014-09-06 16:57:36 -0700 | [diff] [blame] | 27 | * @cport: cport to send the data to |
| 28 | * @complete: callback when the gbuf is finished with |
| 29 | * @size: size of the buffer |
| 30 | * @gfp_mask: allocation mask |
Greg Kroah-Hartman | a39879f | 2014-09-06 16:57:36 -0700 | [diff] [blame] | 31 | * |
| 32 | * TODO: someday it will be nice to handle DMA, but for now, due to the |
| 33 | * architecture we are stuck with, the greybus core has to allocate the buffer |
| 34 | * that the driver can then fill up with the data to be sent out. Curse |
| 35 | * hardware designers for this issue... |
| 36 | */ |
Alex Elder | ba99346 | 2014-11-17 08:08:43 -0600 | [diff] [blame] | 37 | struct gbuf *greybus_alloc_gbuf(struct greybus_host_device *hd, |
Alex Elder | 63921d8 | 2014-11-17 08:08:41 -0600 | [diff] [blame] | 38 | u16 dest_cport_id, |
Greg Kroah-Hartman | a39879f | 2014-09-06 16:57:36 -0700 | [diff] [blame] | 39 | unsigned int size, |
Alex Elder | a77b068 | 2014-11-06 07:01:03 -0600 | [diff] [blame] | 40 | gfp_t gfp_mask) |
Greg Kroah-Hartman | d5d1903 | 2014-08-11 19:03:20 +0800 | [diff] [blame] | 41 | { |
Greg Kroah-Hartman | a39879f | 2014-09-06 16:57:36 -0700 | [diff] [blame] | 42 | struct gbuf *gbuf; |
| 43 | int retval; |
| 44 | |
Alex Elder | fdb594f | 2014-10-06 06:53:12 -0500 | [diff] [blame] | 45 | gbuf = kmem_cache_zalloc(gbuf_head_cache, gfp_mask); |
Greg Kroah-Hartman | a39879f | 2014-09-06 16:57:36 -0700 | [diff] [blame] | 46 | if (!gbuf) |
| 47 | return NULL; |
| 48 | |
Alex Elder | fdb594f | 2014-10-06 06:53:12 -0500 | [diff] [blame] | 49 | kref_init(&gbuf->kref); |
Alex Elder | ba99346 | 2014-11-17 08:08:43 -0600 | [diff] [blame] | 50 | gbuf->hd = hd; |
Alex Elder | 63921d8 | 2014-11-17 08:08:41 -0600 | [diff] [blame] | 51 | gbuf->dest_cport_id = dest_cport_id; |
Alex Elder | bedfdf3 | 2014-10-17 05:18:22 -0500 | [diff] [blame] | 52 | gbuf->status = -EBADR; /* Initial value--means "never set" */ |
Alex Elder | fdb594f | 2014-10-06 06:53:12 -0500 | [diff] [blame] | 53 | |
Greg Kroah-Hartman | a39879f | 2014-09-06 16:57:36 -0700 | [diff] [blame] | 54 | /* Host controller specific allocation for the actual buffer */ |
Alex Elder | ef45fa3 | 2014-11-06 07:01:02 -0600 | [diff] [blame] | 55 | retval = hd->driver->alloc_gbuf_data(gbuf, size, gfp_mask); |
Greg Kroah-Hartman | a39879f | 2014-09-06 16:57:36 -0700 | [diff] [blame] | 56 | if (retval) { |
Alex Elder | fdb594f | 2014-10-06 06:53:12 -0500 | [diff] [blame] | 57 | kmem_cache_free(gbuf_head_cache, gbuf); |
Greg Kroah-Hartman | a39879f | 2014-09-06 16:57:36 -0700 | [diff] [blame] | 58 | return NULL; |
| 59 | } |
| 60 | |
| 61 | return gbuf; |
| 62 | } |
| 63 | EXPORT_SYMBOL_GPL(greybus_alloc_gbuf); |
| 64 | |
| 65 | static DEFINE_MUTEX(gbuf_mutex); |
| 66 | |
| 67 | static void free_gbuf(struct kref *kref) |
| 68 | { |
| 69 | struct gbuf *gbuf = container_of(kref, struct gbuf, kref); |
| 70 | |
Alex Elder | ba99346 | 2014-11-17 08:08:43 -0600 | [diff] [blame] | 71 | gbuf->hd->driver->free_gbuf_data(gbuf); |
Greg Kroah-Hartman | a39879f | 2014-09-06 16:57:36 -0700 | [diff] [blame] | 72 | |
Greg Kroah-Hartman | 3e7736e | 2014-09-21 17:34:28 -0700 | [diff] [blame] | 73 | kmem_cache_free(gbuf_head_cache, gbuf); |
Alex Elder | ad8cd0d6 | 2014-10-16 06:35:25 -0500 | [diff] [blame] | 74 | mutex_unlock(&gbuf_mutex); |
Greg Kroah-Hartman | d5d1903 | 2014-08-11 19:03:20 +0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void greybus_free_gbuf(struct gbuf *gbuf) |
| 78 | { |
Greg Kroah-Hartman | a39879f | 2014-09-06 16:57:36 -0700 | [diff] [blame] | 79 | /* drop the reference count and get out of here */ |
| 80 | kref_put_mutex(&gbuf->kref, free_gbuf, &gbuf_mutex); |
Greg Kroah-Hartman | d5d1903 | 2014-08-11 19:03:20 +0800 | [diff] [blame] | 81 | } |
Greg Kroah-Hartman | a39879f | 2014-09-06 16:57:36 -0700 | [diff] [blame] | 82 | EXPORT_SYMBOL_GPL(greybus_free_gbuf); |
| 83 | |
| 84 | struct gbuf *greybus_get_gbuf(struct gbuf *gbuf) |
| 85 | { |
| 86 | mutex_lock(&gbuf_mutex); |
| 87 | kref_get(&gbuf->kref); |
| 88 | mutex_unlock(&gbuf_mutex); |
| 89 | return gbuf; |
| 90 | } |
| 91 | EXPORT_SYMBOL_GPL(greybus_get_gbuf); |
| 92 | |
Greg Kroah-Hartman | f036e05 | 2014-09-19 19:13:33 -0700 | [diff] [blame] | 93 | int greybus_submit_gbuf(struct gbuf *gbuf, gfp_t gfp_mask) |
Greg Kroah-Hartman | d5d1903 | 2014-08-11 19:03:20 +0800 | [diff] [blame] | 94 | { |
Alex Elder | bedfdf3 | 2014-10-17 05:18:22 -0500 | [diff] [blame] | 95 | gbuf->status = -EINPROGRESS; |
| 96 | |
Alex Elder | ba99346 | 2014-11-17 08:08:43 -0600 | [diff] [blame] | 97 | return gbuf->hd->driver->submit_gbuf(gbuf, gfp_mask); |
Greg Kroah-Hartman | d5d1903 | 2014-08-11 19:03:20 +0800 | [diff] [blame] | 98 | } |
| 99 | |
Greg Kroah-Hartman | 4afbba0 | 2014-10-27 14:01:06 +0800 | [diff] [blame] | 100 | void greybus_kill_gbuf(struct gbuf *gbuf) |
Greg Kroah-Hartman | d5d1903 | 2014-08-11 19:03:20 +0800 | [diff] [blame] | 101 | { |
Greg Kroah-Hartman | d814488 | 2014-10-27 13:31:01 +0800 | [diff] [blame] | 102 | if (gbuf->status != -EINPROGRESS) |
Greg Kroah-Hartman | 4afbba0 | 2014-10-27 14:01:06 +0800 | [diff] [blame] | 103 | return; |
Greg Kroah-Hartman | d814488 | 2014-10-27 13:31:01 +0800 | [diff] [blame] | 104 | |
Alex Elder | ba99346 | 2014-11-17 08:08:43 -0600 | [diff] [blame] | 105 | gbuf->hd->driver->kill_gbuf(gbuf); |
Greg Kroah-Hartman | d5d1903 | 2014-08-11 19:03:20 +0800 | [diff] [blame] | 106 | } |
Greg Kroah-Hartman | 9c8d3af | 2014-09-13 11:09:35 -0700 | [diff] [blame] | 107 | |
Alex Elder | 1cfc667 | 2014-09-30 19:25:21 -0500 | [diff] [blame] | 108 | void greybus_cport_in(struct greybus_host_device *hd, u16 cport_id, |
| 109 | u8 *data, size_t length) |
Greg Kroah-Hartman | 80e04f0 | 2014-09-13 18:20:54 -0700 | [diff] [blame] | 110 | { |
Alex Elder | 00d2e75 | 2014-10-06 06:53:09 -0500 | [diff] [blame] | 111 | struct gb_connection *connection; |
| 112 | |
| 113 | connection = gb_hd_connection_find(hd, cport_id); |
| 114 | if (!connection) { |
| 115 | dev_err(hd->parent, |
| 116 | "nonexistent connection (%zu bytes dropped)\n", length); |
| 117 | return; |
| 118 | } |
Alex Elder | d90c25b | 2014-10-16 06:35:33 -0500 | [diff] [blame] | 119 | gb_connection_operation_recv(connection, data, length); |
Greg Kroah-Hartman | 80e04f0 | 2014-09-13 18:20:54 -0700 | [diff] [blame] | 120 | } |
Alex Elder | 0db32a6 | 2014-09-24 05:16:14 -0500 | [diff] [blame] | 121 | EXPORT_SYMBOL_GPL(greybus_cport_in); |
Greg Kroah-Hartman | 80e04f0 | 2014-09-13 18:20:54 -0700 | [diff] [blame] | 122 | |
Greg Kroah-Hartman | 45f3678 | 2014-09-14 11:40:35 -0700 | [diff] [blame] | 123 | int gb_gbuf_init(void) |
Greg Kroah-Hartman | 80e04f0 | 2014-09-13 18:20:54 -0700 | [diff] [blame] | 124 | { |
Greg Kroah-Hartman | 3e7736e | 2014-09-21 17:34:28 -0700 | [diff] [blame] | 125 | gbuf_head_cache = kmem_cache_create("gbuf_head_cache", |
| 126 | sizeof(struct gbuf), 0, 0, NULL); |
Greg Kroah-Hartman | 80e04f0 | 2014-09-13 18:20:54 -0700 | [diff] [blame] | 127 | return 0; |
| 128 | } |
| 129 | |
Greg Kroah-Hartman | 45f3678 | 2014-09-14 11:40:35 -0700 | [diff] [blame] | 130 | void gb_gbuf_exit(void) |
Greg Kroah-Hartman | 80e04f0 | 2014-09-13 18:20:54 -0700 | [diff] [blame] | 131 | { |
Greg Kroah-Hartman | 3e7736e | 2014-09-21 17:34:28 -0700 | [diff] [blame] | 132 | kmem_cache_destroy(gbuf_head_cache); |
Viresh Kumar | f66832da | 2014-11-14 17:25:01 +0530 | [diff] [blame] | 133 | gbuf_head_cache = NULL; |
Greg Kroah-Hartman | 80e04f0 | 2014-09-13 18:20:54 -0700 | [diff] [blame] | 134 | } |