Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 1 | /********************************************************************** |
| 2 | * Author: Cavium, Inc. |
| 3 | * |
| 4 | * Contact: support@cavium.com |
| 5 | * Please include "LiquidIO" in the subject. |
| 6 | * |
| 7 | * Copyright (c) 2003-2015 Cavium, Inc. |
| 8 | * |
| 9 | * This file is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License, Version 2, as |
| 11 | * published by the Free Software Foundation. |
| 12 | * |
| 13 | * This file is distributed in the hope that it will be useful, but |
| 14 | * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty |
| 15 | * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or |
| 16 | * NONINFRINGEMENT. See the GNU General Public License for more |
| 17 | * details. |
| 18 | * |
| 19 | * This file may also be available under a different license from Cavium. |
| 20 | * Contact Cavium, Inc. for more information |
| 21 | **********************************************************************/ |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 22 | #include <linux/pci.h> |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 23 | #include <linux/netdevice.h> |
Raghu Vatsavayi | 5b173cf | 2015-06-12 18:11:50 -0700 | [diff] [blame] | 24 | #include <linux/vmalloc.h> |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 25 | #include "liquidio_common.h" |
| 26 | #include "octeon_droq.h" |
| 27 | #include "octeon_iq.h" |
| 28 | #include "response_manager.h" |
| 29 | #include "octeon_device.h" |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 30 | #include "octeon_main.h" |
| 31 | #include "octeon_network.h" |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 32 | #include "cn66xx_device.h" |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 33 | |
| 34 | #define INCR_INSTRQUEUE_PKT_COUNT(octeon_dev_ptr, iq_no, field, count) \ |
| 35 | (octeon_dev_ptr->instr_queue[iq_no]->stats.field += count) |
| 36 | |
| 37 | struct iq_post_status { |
| 38 | int status; |
| 39 | int index; |
| 40 | }; |
| 41 | |
| 42 | static void check_db_timeout(struct work_struct *work); |
Raghu Vatsavayi | 9a96bde | 2016-06-21 22:53:06 -0700 | [diff] [blame] | 43 | static void __check_db_timeout(struct octeon_device *oct, u64 iq_no); |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 44 | |
| 45 | static void (*reqtype_free_fn[MAX_OCTEON_DEVICES][REQTYPE_LAST + 1]) (void *); |
| 46 | |
| 47 | static inline int IQ_INSTR_MODE_64B(struct octeon_device *oct, int iq_no) |
| 48 | { |
| 49 | struct octeon_instr_queue *iq = |
| 50 | (struct octeon_instr_queue *)oct->instr_queue[iq_no]; |
| 51 | return iq->iqcmd_64B; |
| 52 | } |
| 53 | |
| 54 | #define IQ_INSTR_MODE_32B(oct, iq_no) (!IQ_INSTR_MODE_64B(oct, iq_no)) |
| 55 | |
| 56 | /* Define this to return the request status comaptible to old code */ |
| 57 | /*#define OCTEON_USE_OLD_REQ_STATUS*/ |
| 58 | |
| 59 | /* Return 0 on success, 1 on failure */ |
| 60 | int octeon_init_instr_queue(struct octeon_device *oct, |
Raghu Vatsavayi | 26236fa | 2016-06-14 16:54:44 -0700 | [diff] [blame] | 61 | union oct_txpciq txpciq, |
| 62 | u32 num_descs) |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 63 | { |
| 64 | struct octeon_instr_queue *iq; |
| 65 | struct octeon_iq_config *conf = NULL; |
Raghu Vatsavayi | 26236fa | 2016-06-14 16:54:44 -0700 | [diff] [blame] | 66 | u32 iq_no = (u32)txpciq.s.q_no; |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 67 | u32 q_size; |
| 68 | struct cavium_wq *db_wq; |
Raghu Vatsavayi | 26236fa | 2016-06-14 16:54:44 -0700 | [diff] [blame] | 69 | int orig_node = dev_to_node(&oct->pci_dev->dev); |
| 70 | int numa_node = cpu_to_node(iq_no % num_online_cpus()); |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 71 | |
| 72 | if (OCTEON_CN6XXX(oct)) |
| 73 | conf = &(CFG_GET_IQ_CFG(CHIP_FIELD(oct, cn6xxx, conf))); |
| 74 | |
| 75 | if (!conf) { |
| 76 | dev_err(&oct->pci_dev->dev, "Unsupported Chip %x\n", |
| 77 | oct->chip_id); |
| 78 | return 1; |
| 79 | } |
| 80 | |
| 81 | if (num_descs & (num_descs - 1)) { |
| 82 | dev_err(&oct->pci_dev->dev, |
| 83 | "Number of descriptors for instr queue %d not in power of 2.\n", |
| 84 | iq_no); |
| 85 | return 1; |
| 86 | } |
| 87 | |
| 88 | q_size = (u32)conf->instr_type * num_descs; |
| 89 | |
| 90 | iq = oct->instr_queue[iq_no]; |
Raghu Vatsavayi | 6a885b6 | 2016-06-14 16:54:51 -0700 | [diff] [blame] | 91 | iq->oct_dev = oct; |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 92 | |
Raghu Vatsavayi | 26236fa | 2016-06-14 16:54:44 -0700 | [diff] [blame] | 93 | set_dev_node(&oct->pci_dev->dev, numa_node); |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 94 | iq->base_addr = lio_dma_alloc(oct, q_size, |
| 95 | (dma_addr_t *)&iq->base_addr_dma); |
Raghu Vatsavayi | 26236fa | 2016-06-14 16:54:44 -0700 | [diff] [blame] | 96 | set_dev_node(&oct->pci_dev->dev, orig_node); |
| 97 | if (!iq->base_addr) |
| 98 | iq->base_addr = lio_dma_alloc(oct, q_size, |
| 99 | (dma_addr_t *)&iq->base_addr_dma); |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 100 | if (!iq->base_addr) { |
| 101 | dev_err(&oct->pci_dev->dev, "Cannot allocate memory for instr queue %d\n", |
| 102 | iq_no); |
| 103 | return 1; |
| 104 | } |
| 105 | |
| 106 | iq->max_count = num_descs; |
| 107 | |
| 108 | /* Initialize a list to holds requests that have been posted to Octeon |
| 109 | * but has yet to be fetched by octeon |
| 110 | */ |
Raghu Vatsavayi | 26236fa | 2016-06-14 16:54:44 -0700 | [diff] [blame] | 111 | iq->request_list = vmalloc_node((sizeof(*iq->request_list) * num_descs), |
| 112 | numa_node); |
| 113 | if (!iq->request_list) |
| 114 | iq->request_list = vmalloc(sizeof(*iq->request_list) * |
| 115 | num_descs); |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 116 | if (!iq->request_list) { |
| 117 | lio_dma_free(oct, q_size, iq->base_addr, iq->base_addr_dma); |
| 118 | dev_err(&oct->pci_dev->dev, "Alloc failed for IQ[%d] nr free list\n", |
| 119 | iq_no); |
| 120 | return 1; |
| 121 | } |
| 122 | |
| 123 | memset(iq->request_list, 0, sizeof(*iq->request_list) * num_descs); |
| 124 | |
| 125 | dev_dbg(&oct->pci_dev->dev, "IQ[%d]: base: %p basedma: %llx count: %d\n", |
| 126 | iq_no, iq->base_addr, iq->base_addr_dma, iq->max_count); |
| 127 | |
Raghu Vatsavayi | 26236fa | 2016-06-14 16:54:44 -0700 | [diff] [blame] | 128 | iq->txpciq.u64 = txpciq.u64; |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 129 | iq->fill_threshold = (u32)conf->db_min; |
| 130 | iq->fill_cnt = 0; |
| 131 | iq->host_write_index = 0; |
| 132 | iq->octeon_read_index = 0; |
| 133 | iq->flush_index = 0; |
| 134 | iq->last_db_time = 0; |
| 135 | iq->do_auto_flush = 1; |
| 136 | iq->db_timeout = (u32)conf->db_timeout; |
| 137 | atomic_set(&iq->instr_pending, 0); |
| 138 | |
| 139 | /* Initialize the spinlock for this instruction queue */ |
| 140 | spin_lock_init(&iq->lock); |
Raghu Vatsavayi | 9a96bde | 2016-06-21 22:53:06 -0700 | [diff] [blame] | 141 | spin_lock_init(&iq->post_lock); |
| 142 | |
| 143 | spin_lock_init(&iq->iq_flush_running_lock); |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 144 | |
Raghu Vatsavayi | 63da840 | 2016-06-21 22:53:03 -0700 | [diff] [blame] | 145 | oct->io_qmask.iq |= (1ULL << iq_no); |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 146 | |
| 147 | /* Set the 32B/64B mode for each input queue */ |
| 148 | oct->io_qmask.iq64B |= ((conf->instr_type == 64) << iq_no); |
| 149 | iq->iqcmd_64B = (conf->instr_type == 64); |
| 150 | |
| 151 | oct->fn_list.setup_iq_regs(oct, iq_no); |
| 152 | |
Bhaktipriya Shridhar | aaa7672 | 2016-06-04 20:54:00 +0530 | [diff] [blame] | 153 | oct->check_db_wq[iq_no].wq = alloc_workqueue("check_iq_db", |
| 154 | WQ_MEM_RECLAIM, |
| 155 | 0); |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 156 | if (!oct->check_db_wq[iq_no].wq) { |
| 157 | lio_dma_free(oct, q_size, iq->base_addr, iq->base_addr_dma); |
| 158 | dev_err(&oct->pci_dev->dev, "check db wq create failed for iq %d\n", |
| 159 | iq_no); |
| 160 | return 1; |
| 161 | } |
| 162 | |
| 163 | db_wq = &oct->check_db_wq[iq_no]; |
| 164 | |
| 165 | INIT_DELAYED_WORK(&db_wq->wk.work, check_db_timeout); |
| 166 | db_wq->wk.ctxptr = oct; |
| 167 | db_wq->wk.ctxul = iq_no; |
| 168 | queue_delayed_work(db_wq->wq, &db_wq->wk.work, msecs_to_jiffies(1)); |
| 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | int octeon_delete_instr_queue(struct octeon_device *oct, u32 iq_no) |
| 174 | { |
| 175 | u64 desc_size = 0, q_size; |
| 176 | struct octeon_instr_queue *iq = oct->instr_queue[iq_no]; |
| 177 | |
| 178 | cancel_delayed_work_sync(&oct->check_db_wq[iq_no].wk.work); |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 179 | destroy_workqueue(oct->check_db_wq[iq_no].wq); |
| 180 | |
| 181 | if (OCTEON_CN6XXX(oct)) |
| 182 | desc_size = |
| 183 | CFG_GET_IQ_INSTR_TYPE(CHIP_FIELD(oct, cn6xxx, conf)); |
| 184 | |
Markus Elfring | 9686f31 | 2015-06-29 12:22:24 +0200 | [diff] [blame] | 185 | vfree(iq->request_list); |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 186 | |
| 187 | if (iq->base_addr) { |
| 188 | q_size = iq->max_count * desc_size; |
| 189 | lio_dma_free(oct, (u32)q_size, iq->base_addr, |
| 190 | iq->base_addr_dma); |
| 191 | return 0; |
| 192 | } |
| 193 | return 1; |
| 194 | } |
| 195 | |
| 196 | /* Return 0 on success, 1 on failure */ |
| 197 | int octeon_setup_iq(struct octeon_device *oct, |
Raghu Vatsavayi | 0cece6c | 2016-06-14 16:54:50 -0700 | [diff] [blame] | 198 | int ifidx, |
| 199 | int q_index, |
Raghu Vatsavayi | 26236fa | 2016-06-14 16:54:44 -0700 | [diff] [blame] | 200 | union oct_txpciq txpciq, |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 201 | u32 num_descs, |
| 202 | void *app_ctx) |
| 203 | { |
Raghu Vatsavayi | 26236fa | 2016-06-14 16:54:44 -0700 | [diff] [blame] | 204 | u32 iq_no = (u32)txpciq.s.q_no; |
| 205 | int numa_node = cpu_to_node(iq_no % num_online_cpus()); |
| 206 | |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 207 | if (oct->instr_queue[iq_no]) { |
| 208 | dev_dbg(&oct->pci_dev->dev, "IQ is in use. Cannot create the IQ: %d again\n", |
| 209 | iq_no); |
Raghu Vatsavayi | 26236fa | 2016-06-14 16:54:44 -0700 | [diff] [blame] | 210 | oct->instr_queue[iq_no]->txpciq.u64 = txpciq.u64; |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 211 | oct->instr_queue[iq_no]->app_ctx = app_ctx; |
| 212 | return 0; |
| 213 | } |
| 214 | oct->instr_queue[iq_no] = |
Raghu Vatsavayi | 26236fa | 2016-06-14 16:54:44 -0700 | [diff] [blame] | 215 | vmalloc_node(sizeof(struct octeon_instr_queue), numa_node); |
| 216 | if (!oct->instr_queue[iq_no]) |
| 217 | oct->instr_queue[iq_no] = |
| 218 | vmalloc(sizeof(struct octeon_instr_queue)); |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 219 | if (!oct->instr_queue[iq_no]) |
| 220 | return 1; |
| 221 | |
| 222 | memset(oct->instr_queue[iq_no], 0, |
| 223 | sizeof(struct octeon_instr_queue)); |
| 224 | |
Raghu Vatsavayi | 0cece6c | 2016-06-14 16:54:50 -0700 | [diff] [blame] | 225 | oct->instr_queue[iq_no]->q_index = q_index; |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 226 | oct->instr_queue[iq_no]->app_ctx = app_ctx; |
Raghu Vatsavayi | 0cece6c | 2016-06-14 16:54:50 -0700 | [diff] [blame] | 227 | oct->instr_queue[iq_no]->ifidx = ifidx; |
| 228 | |
Raghu Vatsavayi | 26236fa | 2016-06-14 16:54:44 -0700 | [diff] [blame] | 229 | if (octeon_init_instr_queue(oct, txpciq, num_descs)) { |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 230 | vfree(oct->instr_queue[iq_no]); |
| 231 | oct->instr_queue[iq_no] = NULL; |
| 232 | return 1; |
| 233 | } |
| 234 | |
| 235 | oct->num_iqs++; |
| 236 | oct->fn_list.enable_io_queues(oct); |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | int lio_wait_for_instr_fetch(struct octeon_device *oct) |
| 241 | { |
| 242 | int i, retry = 1000, pending, instr_cnt = 0; |
| 243 | |
| 244 | do { |
| 245 | instr_cnt = 0; |
| 246 | |
| 247 | /*for (i = 0; i < oct->num_iqs; i++) {*/ |
Raghu Vatsavayi | 63da840 | 2016-06-21 22:53:03 -0700 | [diff] [blame] | 248 | for (i = 0; i < MAX_OCTEON_INSTR_QUEUES(oct); i++) { |
| 249 | if (!(oct->io_qmask.iq & (1ULL << i))) |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 250 | continue; |
| 251 | pending = |
| 252 | atomic_read(&oct-> |
| 253 | instr_queue[i]->instr_pending); |
| 254 | if (pending) |
| 255 | __check_db_timeout(oct, i); |
| 256 | instr_cnt += pending; |
| 257 | } |
| 258 | |
| 259 | if (instr_cnt == 0) |
| 260 | break; |
| 261 | |
| 262 | schedule_timeout_uninterruptible(1); |
| 263 | |
| 264 | } while (retry-- && instr_cnt); |
| 265 | |
| 266 | return instr_cnt; |
| 267 | } |
| 268 | |
| 269 | static inline void |
| 270 | ring_doorbell(struct octeon_device *oct, struct octeon_instr_queue *iq) |
| 271 | { |
| 272 | if (atomic_read(&oct->status) == OCT_DEV_RUNNING) { |
| 273 | writel(iq->fill_cnt, iq->doorbell_reg); |
| 274 | /* make sure doorbell write goes through */ |
| 275 | mmiowb(); |
| 276 | iq->fill_cnt = 0; |
| 277 | iq->last_db_time = jiffies; |
| 278 | return; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | static inline void __copy_cmd_into_iq(struct octeon_instr_queue *iq, |
| 283 | u8 *cmd) |
| 284 | { |
| 285 | u8 *iqptr, cmdsize; |
| 286 | |
| 287 | cmdsize = ((iq->iqcmd_64B) ? 64 : 32); |
| 288 | iqptr = iq->base_addr + (cmdsize * iq->host_write_index); |
| 289 | |
| 290 | memcpy(iqptr, cmd, cmdsize); |
| 291 | } |
| 292 | |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 293 | static inline struct iq_post_status |
Raghu Vatsavayi | a7d5a3d | 2016-07-03 13:56:48 -0700 | [diff] [blame] | 294 | __post_command2(struct octeon_instr_queue *iq, u8 *cmd) |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 295 | { |
| 296 | struct iq_post_status st; |
| 297 | |
| 298 | st.status = IQ_SEND_OK; |
| 299 | |
| 300 | /* This ensures that the read index does not wrap around to the same |
| 301 | * position if queue gets full before Octeon could fetch any instr. |
| 302 | */ |
| 303 | if (atomic_read(&iq->instr_pending) >= (s32)(iq->max_count - 1)) { |
| 304 | st.status = IQ_SEND_FAILED; |
| 305 | st.index = -1; |
| 306 | return st; |
| 307 | } |
| 308 | |
| 309 | if (atomic_read(&iq->instr_pending) >= (s32)(iq->max_count - 2)) |
| 310 | st.status = IQ_SEND_STOP; |
| 311 | |
| 312 | __copy_cmd_into_iq(iq, cmd); |
| 313 | |
| 314 | /* "index" is returned, host_write_index is modified. */ |
| 315 | st.index = iq->host_write_index; |
| 316 | INCR_INDEX_BY1(iq->host_write_index, iq->max_count); |
| 317 | iq->fill_cnt++; |
| 318 | |
| 319 | /* Flush the command into memory. We need to be sure the data is in |
| 320 | * memory before indicating that the instruction is pending. |
| 321 | */ |
| 322 | wmb(); |
| 323 | |
| 324 | atomic_inc(&iq->instr_pending); |
| 325 | |
| 326 | return st; |
| 327 | } |
| 328 | |
| 329 | int |
| 330 | octeon_register_reqtype_free_fn(struct octeon_device *oct, int reqtype, |
| 331 | void (*fn)(void *)) |
| 332 | { |
| 333 | if (reqtype > REQTYPE_LAST) { |
| 334 | dev_err(&oct->pci_dev->dev, "%s: Invalid reqtype: %d\n", |
| 335 | __func__, reqtype); |
| 336 | return -EINVAL; |
| 337 | } |
| 338 | |
| 339 | reqtype_free_fn[oct->octeon_id][reqtype] = fn; |
| 340 | |
| 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | static inline void |
| 345 | __add_to_request_list(struct octeon_instr_queue *iq, |
| 346 | int idx, void *buf, int reqtype) |
| 347 | { |
| 348 | iq->request_list[idx].buf = buf; |
| 349 | iq->request_list[idx].reqtype = reqtype; |
| 350 | } |
| 351 | |
| 352 | int |
| 353 | lio_process_iq_request_list(struct octeon_device *oct, |
Raghu Vatsavayi | 9a96bde | 2016-06-21 22:53:06 -0700 | [diff] [blame] | 354 | struct octeon_instr_queue *iq, u32 napi_budget) |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 355 | { |
| 356 | int reqtype; |
| 357 | void *buf; |
| 358 | u32 old = iq->flush_index; |
| 359 | u32 inst_count = 0; |
Raghu Vatsavayi | 9a96bde | 2016-06-21 22:53:06 -0700 | [diff] [blame] | 360 | unsigned int pkts_compl = 0, bytes_compl = 0; |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 361 | struct octeon_soft_command *sc; |
| 362 | struct octeon_instr_irh *irh; |
Raghu Vatsavayi | 14866cc | 2016-07-03 13:56:49 -0700 | [diff] [blame] | 363 | unsigned long flags; |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 364 | |
| 365 | while (old != iq->octeon_read_index) { |
| 366 | reqtype = iq->request_list[old].reqtype; |
| 367 | buf = iq->request_list[old].buf; |
| 368 | |
| 369 | if (reqtype == REQTYPE_NONE) |
| 370 | goto skip_this; |
| 371 | |
| 372 | octeon_update_tx_completion_counters(buf, reqtype, &pkts_compl, |
| 373 | &bytes_compl); |
| 374 | |
| 375 | switch (reqtype) { |
| 376 | case REQTYPE_NORESP_NET: |
| 377 | case REQTYPE_NORESP_NET_SG: |
| 378 | case REQTYPE_RESP_NET_SG: |
| 379 | reqtype_free_fn[oct->octeon_id][reqtype](buf); |
| 380 | break; |
| 381 | case REQTYPE_RESP_NET: |
| 382 | case REQTYPE_SOFT_COMMAND: |
| 383 | sc = buf; |
| 384 | |
Raghu Vatsavayi | 6a885b6 | 2016-06-14 16:54:51 -0700 | [diff] [blame] | 385 | irh = (struct octeon_instr_irh *)&sc->cmd.cmd2.irh; |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 386 | if (irh->rflag) { |
| 387 | /* We're expecting a response from Octeon. |
| 388 | * It's up to lio_process_ordered_list() to |
| 389 | * process sc. Add sc to the ordered soft |
| 390 | * command response list because we expect |
| 391 | * a response from Octeon. |
| 392 | */ |
Raghu Vatsavayi | 14866cc | 2016-07-03 13:56:49 -0700 | [diff] [blame] | 393 | spin_lock_irqsave |
| 394 | (&oct->response_list |
| 395 | [OCTEON_ORDERED_SC_LIST].lock, |
| 396 | flags); |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 397 | atomic_inc(&oct->response_list |
| 398 | [OCTEON_ORDERED_SC_LIST]. |
| 399 | pending_req_count); |
| 400 | list_add_tail(&sc->node, &oct->response_list |
| 401 | [OCTEON_ORDERED_SC_LIST].head); |
Raghu Vatsavayi | 14866cc | 2016-07-03 13:56:49 -0700 | [diff] [blame] | 402 | spin_unlock_irqrestore |
| 403 | (&oct->response_list |
| 404 | [OCTEON_ORDERED_SC_LIST].lock, |
| 405 | flags); |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 406 | } else { |
| 407 | if (sc->callback) { |
| 408 | sc->callback(oct, OCTEON_REQUEST_DONE, |
| 409 | sc->callback_arg); |
| 410 | } |
| 411 | } |
| 412 | break; |
| 413 | default: |
| 414 | dev_err(&oct->pci_dev->dev, |
| 415 | "%s Unknown reqtype: %d buf: %p at idx %d\n", |
| 416 | __func__, reqtype, buf, old); |
| 417 | } |
| 418 | |
| 419 | iq->request_list[old].buf = NULL; |
| 420 | iq->request_list[old].reqtype = 0; |
| 421 | |
| 422 | skip_this: |
| 423 | inst_count++; |
| 424 | INCR_INDEX_BY1(old, iq->max_count); |
Raghu Vatsavayi | 9a96bde | 2016-06-21 22:53:06 -0700 | [diff] [blame] | 425 | |
| 426 | if ((napi_budget) && (inst_count >= napi_budget)) |
| 427 | break; |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 428 | } |
| 429 | if (bytes_compl) |
| 430 | octeon_report_tx_completion_to_bql(iq->app_ctx, pkts_compl, |
| 431 | bytes_compl); |
| 432 | iq->flush_index = old; |
| 433 | |
| 434 | return inst_count; |
| 435 | } |
| 436 | |
Raghu Vatsavayi | 9a96bde | 2016-06-21 22:53:06 -0700 | [diff] [blame] | 437 | /* Can only be called from process context */ |
| 438 | int |
| 439 | octeon_flush_iq(struct octeon_device *oct, struct octeon_instr_queue *iq, |
| 440 | u32 pending_thresh, u32 napi_budget) |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 441 | { |
| 442 | u32 inst_processed = 0; |
Raghu Vatsavayi | 9a96bde | 2016-06-21 22:53:06 -0700 | [diff] [blame] | 443 | u32 tot_inst_processed = 0; |
| 444 | int tx_done = 1; |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 445 | |
Raghu Vatsavayi | 9a96bde | 2016-06-21 22:53:06 -0700 | [diff] [blame] | 446 | if (!spin_trylock(&iq->iq_flush_running_lock)) |
| 447 | return tx_done; |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 448 | |
Raghu Vatsavayi | 9a96bde | 2016-06-21 22:53:06 -0700 | [diff] [blame] | 449 | spin_lock_bh(&iq->lock); |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 450 | |
Raghu Vatsavayi | 9a96bde | 2016-06-21 22:53:06 -0700 | [diff] [blame] | 451 | iq->octeon_read_index = oct->fn_list.update_iq_read_idx(iq); |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 452 | |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 453 | if (atomic_read(&iq->instr_pending) >= (s32)pending_thresh) { |
Raghu Vatsavayi | 9a96bde | 2016-06-21 22:53:06 -0700 | [diff] [blame] | 454 | do { |
| 455 | /* Process any outstanding IQ packets. */ |
| 456 | if (iq->flush_index == iq->octeon_read_index) |
| 457 | break; |
| 458 | |
| 459 | if (napi_budget) |
| 460 | inst_processed = lio_process_iq_request_list |
| 461 | (oct, iq, |
| 462 | napi_budget - tot_inst_processed); |
| 463 | else |
| 464 | inst_processed = |
| 465 | lio_process_iq_request_list(oct, iq, 0); |
| 466 | |
| 467 | if (inst_processed) { |
| 468 | atomic_sub(inst_processed, &iq->instr_pending); |
| 469 | iq->stats.instr_processed += inst_processed; |
| 470 | } |
| 471 | |
| 472 | tot_inst_processed += inst_processed; |
| 473 | inst_processed = 0; |
| 474 | |
| 475 | } while (tot_inst_processed < napi_budget); |
| 476 | |
| 477 | if (napi_budget && (tot_inst_processed >= napi_budget)) |
| 478 | tx_done = 0; |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 479 | } |
Raghu Vatsavayi | 9a96bde | 2016-06-21 22:53:06 -0700 | [diff] [blame] | 480 | |
| 481 | iq->last_db_time = jiffies; |
| 482 | |
| 483 | spin_unlock_bh(&iq->lock); |
| 484 | |
| 485 | spin_unlock(&iq->iq_flush_running_lock); |
| 486 | |
| 487 | return tx_done; |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 488 | } |
| 489 | |
Raghu Vatsavayi | 9a96bde | 2016-06-21 22:53:06 -0700 | [diff] [blame] | 490 | /* Process instruction queue after timeout. |
| 491 | * This routine gets called from a workqueue or when removing the module. |
| 492 | */ |
| 493 | static void __check_db_timeout(struct octeon_device *oct, u64 iq_no) |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 494 | { |
| 495 | struct octeon_instr_queue *iq; |
| 496 | u64 next_time; |
| 497 | |
| 498 | if (!oct) |
| 499 | return; |
| 500 | iq = oct->instr_queue[iq_no]; |
| 501 | if (!iq) |
| 502 | return; |
| 503 | |
Raghu Vatsavayi | 9a96bde | 2016-06-21 22:53:06 -0700 | [diff] [blame] | 504 | /* return immediately, if no work pending */ |
| 505 | if (!atomic_read(&iq->instr_pending)) |
| 506 | return; |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 507 | /* If jiffies - last_db_time < db_timeout do nothing */ |
| 508 | next_time = iq->last_db_time + iq->db_timeout; |
| 509 | if (!time_after(jiffies, (unsigned long)next_time)) |
| 510 | return; |
| 511 | iq->last_db_time = jiffies; |
| 512 | |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 513 | /* Flush the instruction queue */ |
Raghu Vatsavayi | 9a96bde | 2016-06-21 22:53:06 -0700 | [diff] [blame] | 514 | octeon_flush_iq(oct, iq, 1, 0); |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | /* Called by the Poll thread at regular intervals to check the instruction |
| 518 | * queue for commands to be posted and for commands that were fetched by Octeon. |
| 519 | */ |
| 520 | static void check_db_timeout(struct work_struct *work) |
| 521 | { |
| 522 | struct cavium_wk *wk = (struct cavium_wk *)work; |
| 523 | struct octeon_device *oct = (struct octeon_device *)wk->ctxptr; |
| 524 | unsigned long iq_no = wk->ctxul; |
| 525 | struct cavium_wq *db_wq = &oct->check_db_wq[iq_no]; |
Raghu Vatsavayi | 55893a6 | 2016-07-03 13:56:50 -0700 | [diff] [blame^] | 526 | u32 delay = 10; |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 527 | |
| 528 | __check_db_timeout(oct, iq_no); |
Raghu Vatsavayi | 55893a6 | 2016-07-03 13:56:50 -0700 | [diff] [blame^] | 529 | queue_delayed_work(db_wq->wq, &db_wq->wk.work, msecs_to_jiffies(delay)); |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | int |
| 533 | octeon_send_command(struct octeon_device *oct, u32 iq_no, |
| 534 | u32 force_db, void *cmd, void *buf, |
| 535 | u32 datasize, u32 reqtype) |
| 536 | { |
| 537 | struct iq_post_status st; |
| 538 | struct octeon_instr_queue *iq = oct->instr_queue[iq_no]; |
| 539 | |
Raghu Vatsavayi | 9a96bde | 2016-06-21 22:53:06 -0700 | [diff] [blame] | 540 | /* Get the lock and prevent other tasks and tx interrupt handler from |
| 541 | * running. |
| 542 | */ |
| 543 | spin_lock_bh(&iq->post_lock); |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 544 | |
Raghu Vatsavayi | a7d5a3d | 2016-07-03 13:56:48 -0700 | [diff] [blame] | 545 | st = __post_command2(iq, cmd); |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 546 | |
| 547 | if (st.status != IQ_SEND_FAILED) { |
| 548 | octeon_report_sent_bytes_to_bql(buf, reqtype); |
| 549 | __add_to_request_list(iq, st.index, buf, reqtype); |
| 550 | INCR_INSTRQUEUE_PKT_COUNT(oct, iq_no, bytes_sent, datasize); |
| 551 | INCR_INSTRQUEUE_PKT_COUNT(oct, iq_no, instr_posted, 1); |
| 552 | |
| 553 | if (iq->fill_cnt >= iq->fill_threshold || force_db) |
| 554 | ring_doorbell(oct, iq); |
| 555 | } else { |
| 556 | INCR_INSTRQUEUE_PKT_COUNT(oct, iq_no, instr_dropped, 1); |
| 557 | } |
| 558 | |
Raghu Vatsavayi | 9a96bde | 2016-06-21 22:53:06 -0700 | [diff] [blame] | 559 | spin_unlock_bh(&iq->post_lock); |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 560 | |
Raghu Vatsavayi | 9a96bde | 2016-06-21 22:53:06 -0700 | [diff] [blame] | 561 | /* This is only done here to expedite packets being flushed |
| 562 | * for cases where there are no IQ completion interrupts. |
| 563 | */ |
| 564 | /*if (iq->do_auto_flush)*/ |
| 565 | /* octeon_flush_iq(oct, iq, 2, 0);*/ |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 566 | |
| 567 | return st.status; |
| 568 | } |
| 569 | |
| 570 | void |
| 571 | octeon_prepare_soft_command(struct octeon_device *oct, |
| 572 | struct octeon_soft_command *sc, |
| 573 | u8 opcode, |
| 574 | u8 subcode, |
| 575 | u32 irh_ossp, |
| 576 | u64 ossp0, |
| 577 | u64 ossp1) |
| 578 | { |
| 579 | struct octeon_config *oct_cfg; |
Raghu Vatsavayi | 6a885b6 | 2016-06-14 16:54:51 -0700 | [diff] [blame] | 580 | struct octeon_instr_ih2 *ih2; |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 581 | struct octeon_instr_irh *irh; |
| 582 | struct octeon_instr_rdp *rdp; |
| 583 | |
Raghu Vatsavayi | a7d5a3d | 2016-07-03 13:56:48 -0700 | [diff] [blame] | 584 | WARN_ON(opcode > 15); |
| 585 | WARN_ON(subcode > 127); |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 586 | |
| 587 | oct_cfg = octeon_get_conf(oct); |
| 588 | |
Raghu Vatsavayi | 6a885b6 | 2016-06-14 16:54:51 -0700 | [diff] [blame] | 589 | ih2 = (struct octeon_instr_ih2 *)&sc->cmd.cmd2.ih2; |
| 590 | ih2->tagtype = ATOMIC_TAG; |
| 591 | ih2->tag = LIO_CONTROL; |
| 592 | ih2->raw = 1; |
| 593 | ih2->grp = CFG_GET_CTRL_Q_GRP(oct_cfg); |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 594 | |
| 595 | if (sc->datasize) { |
Raghu Vatsavayi | 6a885b6 | 2016-06-14 16:54:51 -0700 | [diff] [blame] | 596 | ih2->dlengsz = sc->datasize; |
| 597 | ih2->rs = 1; |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 598 | } |
| 599 | |
Raghu Vatsavayi | 6a885b6 | 2016-06-14 16:54:51 -0700 | [diff] [blame] | 600 | irh = (struct octeon_instr_irh *)&sc->cmd.cmd2.irh; |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 601 | irh->opcode = opcode; |
| 602 | irh->subcode = subcode; |
| 603 | |
| 604 | /* opcode/subcode specific parameters (ossp) */ |
| 605 | irh->ossp = irh_ossp; |
Raghu Vatsavayi | 6a885b6 | 2016-06-14 16:54:51 -0700 | [diff] [blame] | 606 | sc->cmd.cmd2.ossp[0] = ossp0; |
| 607 | sc->cmd.cmd2.ossp[1] = ossp1; |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 608 | |
| 609 | if (sc->rdatasize) { |
Raghu Vatsavayi | 6a885b6 | 2016-06-14 16:54:51 -0700 | [diff] [blame] | 610 | rdp = (struct octeon_instr_rdp *)&sc->cmd.cmd2.rdp; |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 611 | rdp->pcie_port = oct->pcie_port; |
| 612 | rdp->rlen = sc->rdatasize; |
| 613 | |
| 614 | irh->rflag = 1; |
Raghu Vatsavayi | 6a885b6 | 2016-06-14 16:54:51 -0700 | [diff] [blame] | 615 | ih2->fsz = 40; /* irh+ossp[0]+ossp[1]+rdp+rptr = 40 bytes */ |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 616 | } else { |
| 617 | irh->rflag = 0; |
Raghu Vatsavayi | 6a885b6 | 2016-06-14 16:54:51 -0700 | [diff] [blame] | 618 | ih2->fsz = 24; /* irh + ossp[0] + ossp[1] = 24 bytes */ |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 619 | } |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | int octeon_send_soft_command(struct octeon_device *oct, |
| 623 | struct octeon_soft_command *sc) |
| 624 | { |
Raghu Vatsavayi | 6a885b6 | 2016-06-14 16:54:51 -0700 | [diff] [blame] | 625 | struct octeon_instr_ih2 *ih2; |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 626 | struct octeon_instr_irh *irh; |
Raghu Vatsavayi | 6a885b6 | 2016-06-14 16:54:51 -0700 | [diff] [blame] | 627 | u32 len; |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 628 | |
Raghu Vatsavayi | 6a885b6 | 2016-06-14 16:54:51 -0700 | [diff] [blame] | 629 | ih2 = (struct octeon_instr_ih2 *)&sc->cmd.cmd2.ih2; |
| 630 | if (ih2->dlengsz) { |
| 631 | WARN_ON(!sc->dmadptr); |
| 632 | sc->cmd.cmd2.dptr = sc->dmadptr; |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 633 | } |
Raghu Vatsavayi | 6a885b6 | 2016-06-14 16:54:51 -0700 | [diff] [blame] | 634 | irh = (struct octeon_instr_irh *)&sc->cmd.cmd2.irh; |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 635 | if (irh->rflag) { |
Raghu Vatsavayi | a7d5a3d | 2016-07-03 13:56:48 -0700 | [diff] [blame] | 636 | WARN_ON(!sc->dmarptr); |
| 637 | WARN_ON(!sc->status_word); |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 638 | *sc->status_word = COMPLETION_WORD_INIT; |
| 639 | |
Raghu Vatsavayi | 6a885b6 | 2016-06-14 16:54:51 -0700 | [diff] [blame] | 640 | sc->cmd.cmd2.rptr = sc->dmarptr; |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 641 | } |
Raghu Vatsavayi | 6a885b6 | 2016-06-14 16:54:51 -0700 | [diff] [blame] | 642 | len = (u32)ih2->dlengsz; |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 643 | |
| 644 | if (sc->wait_time) |
| 645 | sc->timeout = jiffies + sc->wait_time; |
| 646 | |
Raghu Vatsavayi | 6a885b6 | 2016-06-14 16:54:51 -0700 | [diff] [blame] | 647 | return (octeon_send_command(oct, sc->iq_no, 1, &sc->cmd, sc, |
| 648 | len, REQTYPE_SOFT_COMMAND)); |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | int octeon_setup_sc_buffer_pool(struct octeon_device *oct) |
| 652 | { |
| 653 | int i; |
| 654 | u64 dma_addr; |
| 655 | struct octeon_soft_command *sc; |
| 656 | |
| 657 | INIT_LIST_HEAD(&oct->sc_buf_pool.head); |
| 658 | spin_lock_init(&oct->sc_buf_pool.lock); |
| 659 | atomic_set(&oct->sc_buf_pool.alloc_buf_count, 0); |
| 660 | |
| 661 | for (i = 0; i < MAX_SOFT_COMMAND_BUFFERS; i++) { |
| 662 | sc = (struct octeon_soft_command *) |
| 663 | lio_dma_alloc(oct, |
| 664 | SOFT_COMMAND_BUFFER_SIZE, |
| 665 | (dma_addr_t *)&dma_addr); |
| 666 | if (!sc) |
| 667 | return 1; |
| 668 | |
| 669 | sc->dma_addr = dma_addr; |
| 670 | sc->size = SOFT_COMMAND_BUFFER_SIZE; |
| 671 | |
| 672 | list_add_tail(&sc->node, &oct->sc_buf_pool.head); |
| 673 | } |
| 674 | |
| 675 | return 0; |
| 676 | } |
| 677 | |
| 678 | int octeon_free_sc_buffer_pool(struct octeon_device *oct) |
| 679 | { |
| 680 | struct list_head *tmp, *tmp2; |
| 681 | struct octeon_soft_command *sc; |
| 682 | |
Raghu Vatsavayi | 14866cc | 2016-07-03 13:56:49 -0700 | [diff] [blame] | 683 | spin_lock_bh(&oct->sc_buf_pool.lock); |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 684 | |
| 685 | list_for_each_safe(tmp, tmp2, &oct->sc_buf_pool.head) { |
| 686 | list_del(tmp); |
| 687 | |
| 688 | sc = (struct octeon_soft_command *)tmp; |
| 689 | |
| 690 | lio_dma_free(oct, sc->size, sc, sc->dma_addr); |
| 691 | } |
| 692 | |
| 693 | INIT_LIST_HEAD(&oct->sc_buf_pool.head); |
| 694 | |
Raghu Vatsavayi | 14866cc | 2016-07-03 13:56:49 -0700 | [diff] [blame] | 695 | spin_unlock_bh(&oct->sc_buf_pool.lock); |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 696 | |
| 697 | return 0; |
| 698 | } |
| 699 | |
| 700 | struct octeon_soft_command *octeon_alloc_soft_command(struct octeon_device *oct, |
| 701 | u32 datasize, |
| 702 | u32 rdatasize, |
| 703 | u32 ctxsize) |
| 704 | { |
| 705 | u64 dma_addr; |
| 706 | u32 size; |
| 707 | u32 offset = sizeof(struct octeon_soft_command); |
| 708 | struct octeon_soft_command *sc = NULL; |
| 709 | struct list_head *tmp; |
| 710 | |
Raghu Vatsavayi | a7d5a3d | 2016-07-03 13:56:48 -0700 | [diff] [blame] | 711 | WARN_ON((offset + datasize + rdatasize + ctxsize) > |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 712 | SOFT_COMMAND_BUFFER_SIZE); |
| 713 | |
Raghu Vatsavayi | 14866cc | 2016-07-03 13:56:49 -0700 | [diff] [blame] | 714 | spin_lock_bh(&oct->sc_buf_pool.lock); |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 715 | |
| 716 | if (list_empty(&oct->sc_buf_pool.head)) { |
Raghu Vatsavayi | 14866cc | 2016-07-03 13:56:49 -0700 | [diff] [blame] | 717 | spin_unlock_bh(&oct->sc_buf_pool.lock); |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 718 | return NULL; |
| 719 | } |
| 720 | |
| 721 | list_for_each(tmp, &oct->sc_buf_pool.head) |
| 722 | break; |
| 723 | |
| 724 | list_del(tmp); |
| 725 | |
| 726 | atomic_inc(&oct->sc_buf_pool.alloc_buf_count); |
| 727 | |
Raghu Vatsavayi | 14866cc | 2016-07-03 13:56:49 -0700 | [diff] [blame] | 728 | spin_unlock_bh(&oct->sc_buf_pool.lock); |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 729 | |
| 730 | sc = (struct octeon_soft_command *)tmp; |
| 731 | |
| 732 | dma_addr = sc->dma_addr; |
| 733 | size = sc->size; |
| 734 | |
| 735 | memset(sc, 0, sc->size); |
| 736 | |
| 737 | sc->dma_addr = dma_addr; |
| 738 | sc->size = size; |
| 739 | |
| 740 | if (ctxsize) { |
| 741 | sc->ctxptr = (u8 *)sc + offset; |
| 742 | sc->ctxsize = ctxsize; |
| 743 | } |
| 744 | |
| 745 | /* Start data at 128 byte boundary */ |
| 746 | offset = (offset + ctxsize + 127) & 0xffffff80; |
| 747 | |
| 748 | if (datasize) { |
| 749 | sc->virtdptr = (u8 *)sc + offset; |
| 750 | sc->dmadptr = dma_addr + offset; |
| 751 | sc->datasize = datasize; |
| 752 | } |
| 753 | |
| 754 | /* Start rdata at 128 byte boundary */ |
| 755 | offset = (offset + datasize + 127) & 0xffffff80; |
| 756 | |
| 757 | if (rdatasize) { |
Raghu Vatsavayi | a7d5a3d | 2016-07-03 13:56:48 -0700 | [diff] [blame] | 758 | WARN_ON(rdatasize < 16); |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 759 | sc->virtrptr = (u8 *)sc + offset; |
| 760 | sc->dmarptr = dma_addr + offset; |
| 761 | sc->rdatasize = rdatasize; |
| 762 | sc->status_word = (u64 *)((u8 *)(sc->virtrptr) + rdatasize - 8); |
| 763 | } |
| 764 | |
| 765 | return sc; |
| 766 | } |
| 767 | |
| 768 | void octeon_free_soft_command(struct octeon_device *oct, |
| 769 | struct octeon_soft_command *sc) |
| 770 | { |
Raghu Vatsavayi | 14866cc | 2016-07-03 13:56:49 -0700 | [diff] [blame] | 771 | spin_lock_bh(&oct->sc_buf_pool.lock); |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 772 | |
| 773 | list_add_tail(&sc->node, &oct->sc_buf_pool.head); |
| 774 | |
| 775 | atomic_dec(&oct->sc_buf_pool.alloc_buf_count); |
| 776 | |
Raghu Vatsavayi | 14866cc | 2016-07-03 13:56:49 -0700 | [diff] [blame] | 777 | spin_unlock_bh(&oct->sc_buf_pool.lock); |
Raghu Vatsavayi | f21fb3e | 2015-06-09 18:15:23 -0700 | [diff] [blame] | 778 | } |