Allen Hubbe | 578b881 | 2015-05-21 02:51:39 -0400 | [diff] [blame] | 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 | * |
| 5 | * GPL LICENSE SUMMARY |
| 6 | * |
| 7 | * Copyright (C) 2015 EMC Corporation. All Rights Reserved. |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of version 2 of the GNU General Public License as |
| 11 | * published by the Free Software Foundation. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, but |
| 14 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * General Public License for more details. |
| 17 | * |
| 18 | * BSD LICENSE |
| 19 | * |
| 20 | * Copyright (C) 2015 EMC Corporation. All Rights Reserved. |
| 21 | * |
| 22 | * Redistribution and use in source and binary forms, with or without |
| 23 | * modification, are permitted provided that the following conditions |
| 24 | * are met: |
| 25 | * |
| 26 | * * Redistributions of source code must retain the above copyright |
| 27 | * notice, this list of conditions and the following disclaimer. |
| 28 | * * Redistributions in binary form must reproduce the above copy |
| 29 | * notice, this list of conditions and the following disclaimer in |
| 30 | * the documentation and/or other materials provided with the |
| 31 | * distribution. |
| 32 | * * Neither the name of Intel Corporation nor the names of its |
| 33 | * contributors may be used to endorse or promote products derived |
| 34 | * from this software without specific prior written permission. |
| 35 | * |
| 36 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 37 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 38 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 39 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 40 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 41 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 42 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 43 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 44 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 45 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 46 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 47 | * |
| 48 | * PCIe NTB Debugging Tool Linux driver |
| 49 | * |
| 50 | * Contact Information: |
| 51 | * Allen Hubbe <Allen.Hubbe@emc.com> |
| 52 | */ |
| 53 | |
| 54 | /* |
| 55 | * How to use this tool, by example. |
| 56 | * |
| 57 | * Assuming $DBG_DIR is something like: |
| 58 | * '/sys/kernel/debug/ntb_tool/0000:00:03.0' |
| 59 | * |
| 60 | * Eg: check if clearing the doorbell mask generates an interrupt. |
| 61 | * |
| 62 | * # Set the doorbell mask |
| 63 | * root@self# echo 's 1' > $DBG_DIR/mask |
| 64 | * |
| 65 | * # Ring the doorbell from the peer |
| 66 | * root@peer# echo 's 1' > $DBG_DIR/peer_db |
| 67 | * |
| 68 | * # Clear the doorbell mask |
| 69 | * root@self# echo 'c 1' > $DBG_DIR/mask |
| 70 | * |
| 71 | * Observe debugging output in dmesg or your console. You should see a |
| 72 | * doorbell event triggered by clearing the mask. If not, this may indicate an |
| 73 | * issue with the hardware that needs to be worked around in the driver. |
| 74 | * |
| 75 | * Eg: read and write scratchpad registers |
| 76 | * |
| 77 | * root@peer# echo '0 0x01010101 1 0x7f7f7f7f' > $DBG_DIR/peer_spad |
| 78 | * |
| 79 | * root@self# cat $DBG_DIR/spad |
| 80 | * |
| 81 | * Observe that spad 0 and 1 have the values set by the peer. |
| 82 | */ |
| 83 | |
| 84 | #include <linux/init.h> |
| 85 | #include <linux/kernel.h> |
| 86 | #include <linux/module.h> |
| 87 | |
| 88 | #include <linux/debugfs.h> |
| 89 | #include <linux/dma-mapping.h> |
| 90 | #include <linux/pci.h> |
| 91 | #include <linux/slab.h> |
Logan Gunthorpe | 8b71d28 | 2016-06-03 14:50:33 -0600 | [diff] [blame^] | 92 | #include <linux/uaccess.h> |
Allen Hubbe | 578b881 | 2015-05-21 02:51:39 -0400 | [diff] [blame] | 93 | |
| 94 | #include <linux/ntb.h> |
| 95 | |
| 96 | #define DRIVER_NAME "ntb_tool" |
| 97 | #define DRIVER_DESCRIPTION "PCIe NTB Debugging Tool" |
| 98 | |
| 99 | #define DRIVER_LICENSE "Dual BSD/GPL" |
| 100 | #define DRIVER_VERSION "1.0" |
| 101 | #define DRIVER_RELDATE "22 April 2015" |
| 102 | #define DRIVER_AUTHOR "Allen Hubbe <Allen.Hubbe@emc.com>" |
| 103 | |
| 104 | MODULE_LICENSE(DRIVER_LICENSE); |
| 105 | MODULE_VERSION(DRIVER_VERSION); |
| 106 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 107 | MODULE_DESCRIPTION(DRIVER_DESCRIPTION); |
| 108 | |
Logan Gunthorpe | 8b71d28 | 2016-06-03 14:50:33 -0600 | [diff] [blame^] | 109 | #define MAX_MWS 16 |
| 110 | |
| 111 | static unsigned long mw_size = 16; |
| 112 | module_param(mw_size, ulong, 0644); |
| 113 | MODULE_PARM_DESC(mw_size, "size order [n^2] of the memory window for testing"); |
| 114 | |
Allen Hubbe | 578b881 | 2015-05-21 02:51:39 -0400 | [diff] [blame] | 115 | static struct dentry *tool_dbgfs; |
| 116 | |
Logan Gunthorpe | 8b71d28 | 2016-06-03 14:50:33 -0600 | [diff] [blame^] | 117 | struct tool_mw { |
| 118 | resource_size_t size; |
| 119 | u8 __iomem *local; |
| 120 | u8 *peer; |
| 121 | dma_addr_t peer_dma; |
| 122 | }; |
| 123 | |
Allen Hubbe | 578b881 | 2015-05-21 02:51:39 -0400 | [diff] [blame] | 124 | struct tool_ctx { |
| 125 | struct ntb_dev *ntb; |
| 126 | struct dentry *dbgfs; |
Logan Gunthorpe | 8b71d28 | 2016-06-03 14:50:33 -0600 | [diff] [blame^] | 127 | struct work_struct link_cleanup; |
| 128 | bool link_is_up; |
| 129 | struct delayed_work link_work; |
| 130 | int mw_count; |
| 131 | struct tool_mw mws[MAX_MWS]; |
Allen Hubbe | 578b881 | 2015-05-21 02:51:39 -0400 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | #define SPAD_FNAME_SIZE 0x10 |
| 135 | #define INT_PTR(x) ((void *)(unsigned long)x) |
| 136 | #define PTR_INT(x) ((int)(unsigned long)x) |
| 137 | |
| 138 | #define TOOL_FOPS_RDWR(__name, __read, __write) \ |
| 139 | const struct file_operations __name = { \ |
| 140 | .owner = THIS_MODULE, \ |
| 141 | .open = simple_open, \ |
| 142 | .read = __read, \ |
| 143 | .write = __write, \ |
| 144 | } |
| 145 | |
Logan Gunthorpe | 8b71d28 | 2016-06-03 14:50:33 -0600 | [diff] [blame^] | 146 | static int tool_setup_mw(struct tool_ctx *tc, int idx) |
| 147 | { |
| 148 | int rc; |
| 149 | struct tool_mw *mw = &tc->mws[idx]; |
| 150 | phys_addr_t base; |
| 151 | resource_size_t size, align, align_size; |
| 152 | |
| 153 | if (mw->local) |
| 154 | return 0; |
| 155 | |
| 156 | rc = ntb_mw_get_range(tc->ntb, idx, &base, &size, &align, |
| 157 | &align_size); |
| 158 | if (rc) |
| 159 | return rc; |
| 160 | |
| 161 | mw->size = min_t(resource_size_t, 1 << mw_size, size); |
| 162 | mw->size = round_up(mw->size, align); |
| 163 | mw->size = round_up(mw->size, align_size); |
| 164 | |
| 165 | mw->local = ioremap_wc(base, size); |
| 166 | if (mw->local == NULL) |
| 167 | return -EFAULT; |
| 168 | |
| 169 | mw->peer = dma_alloc_coherent(&tc->ntb->pdev->dev, mw->size, |
| 170 | &mw->peer_dma, GFP_KERNEL); |
| 171 | |
| 172 | if (mw->peer == NULL) |
| 173 | return -ENOMEM; |
| 174 | |
| 175 | rc = ntb_mw_set_trans(tc->ntb, idx, mw->peer_dma, mw->size); |
| 176 | if (rc) |
| 177 | return rc; |
| 178 | |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | static void tool_free_mws(struct tool_ctx *tc) |
| 183 | { |
| 184 | int i; |
| 185 | |
| 186 | for (i = 0; i < tc->mw_count; i++) { |
| 187 | if (tc->mws[i].peer) { |
| 188 | ntb_mw_clear_trans(tc->ntb, i); |
| 189 | dma_free_coherent(&tc->ntb->pdev->dev, tc->mws[i].size, |
| 190 | tc->mws[i].peer, |
| 191 | tc->mws[i].peer_dma); |
| 192 | |
| 193 | } |
| 194 | |
| 195 | tc->mws[i].peer = NULL; |
| 196 | tc->mws[i].peer_dma = 0; |
| 197 | |
| 198 | if (tc->mws[i].local) |
| 199 | iounmap(tc->mws[i].local); |
| 200 | |
| 201 | tc->mws[i].local = NULL; |
| 202 | } |
| 203 | |
| 204 | tc->mw_count = 0; |
| 205 | } |
| 206 | |
| 207 | static int tool_setup_mws(struct tool_ctx *tc) |
| 208 | { |
| 209 | int i; |
| 210 | int rc; |
| 211 | |
| 212 | tc->mw_count = min(ntb_mw_count(tc->ntb), MAX_MWS); |
| 213 | |
| 214 | for (i = 0; i < tc->mw_count; i++) { |
| 215 | rc = tool_setup_mw(tc, i); |
| 216 | if (rc) |
| 217 | goto err_out; |
| 218 | } |
| 219 | |
| 220 | return 0; |
| 221 | |
| 222 | err_out: |
| 223 | tool_free_mws(tc); |
| 224 | return rc; |
| 225 | } |
| 226 | |
| 227 | static void tool_link_work(struct work_struct *work) |
| 228 | { |
| 229 | int rc; |
| 230 | struct tool_ctx *tc = container_of(work, struct tool_ctx, |
| 231 | link_work.work); |
| 232 | |
| 233 | tool_free_mws(tc); |
| 234 | rc = tool_setup_mws(tc); |
| 235 | if (rc) |
| 236 | dev_err(&tc->ntb->dev, |
| 237 | "Error setting up memory windows: %d\n", rc); |
| 238 | |
| 239 | tc->link_is_up = true; |
| 240 | } |
| 241 | |
| 242 | static void tool_link_cleanup(struct work_struct *work) |
| 243 | { |
| 244 | struct tool_ctx *tc = container_of(work, struct tool_ctx, |
| 245 | link_cleanup); |
| 246 | |
| 247 | if (!tc->link_is_up) |
| 248 | cancel_delayed_work_sync(&tc->link_work); |
| 249 | } |
| 250 | |
Allen Hubbe | 578b881 | 2015-05-21 02:51:39 -0400 | [diff] [blame] | 251 | static void tool_link_event(void *ctx) |
| 252 | { |
| 253 | struct tool_ctx *tc = ctx; |
| 254 | enum ntb_speed speed; |
| 255 | enum ntb_width width; |
| 256 | int up; |
| 257 | |
| 258 | up = ntb_link_is_up(tc->ntb, &speed, &width); |
| 259 | |
| 260 | dev_dbg(&tc->ntb->dev, "link is %s speed %d width %d\n", |
| 261 | up ? "up" : "down", speed, width); |
Logan Gunthorpe | 8b71d28 | 2016-06-03 14:50:33 -0600 | [diff] [blame^] | 262 | |
| 263 | if (up) |
| 264 | schedule_delayed_work(&tc->link_work, 2*HZ); |
| 265 | else |
| 266 | schedule_work(&tc->link_cleanup); |
Allen Hubbe | 578b881 | 2015-05-21 02:51:39 -0400 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | static void tool_db_event(void *ctx, int vec) |
| 270 | { |
| 271 | struct tool_ctx *tc = ctx; |
| 272 | u64 db_bits, db_mask; |
| 273 | |
| 274 | db_mask = ntb_db_vector_mask(tc->ntb, vec); |
| 275 | db_bits = ntb_db_read(tc->ntb); |
| 276 | |
| 277 | dev_dbg(&tc->ntb->dev, "doorbell vec %d mask %#llx bits %#llx\n", |
| 278 | vec, db_mask, db_bits); |
| 279 | } |
| 280 | |
| 281 | static const struct ntb_ctx_ops tool_ops = { |
| 282 | .link_event = tool_link_event, |
| 283 | .db_event = tool_db_event, |
| 284 | }; |
| 285 | |
| 286 | static ssize_t tool_dbfn_read(struct tool_ctx *tc, char __user *ubuf, |
| 287 | size_t size, loff_t *offp, |
| 288 | u64 (*db_read_fn)(struct ntb_dev *)) |
| 289 | { |
| 290 | size_t buf_size; |
| 291 | char *buf; |
| 292 | ssize_t pos, rc; |
| 293 | |
| 294 | if (!db_read_fn) |
| 295 | return -EINVAL; |
| 296 | |
| 297 | buf_size = min_t(size_t, size, 0x20); |
| 298 | |
| 299 | buf = kmalloc(buf_size, GFP_KERNEL); |
| 300 | if (!buf) |
| 301 | return -ENOMEM; |
| 302 | |
| 303 | pos = scnprintf(buf, buf_size, "%#llx\n", |
| 304 | db_read_fn(tc->ntb)); |
| 305 | |
| 306 | rc = simple_read_from_buffer(ubuf, size, offp, buf, pos); |
| 307 | |
| 308 | kfree(buf); |
| 309 | |
| 310 | return rc; |
| 311 | } |
| 312 | |
| 313 | static ssize_t tool_dbfn_write(struct tool_ctx *tc, |
| 314 | const char __user *ubuf, |
| 315 | size_t size, loff_t *offp, |
| 316 | int (*db_set_fn)(struct ntb_dev *, u64), |
| 317 | int (*db_clear_fn)(struct ntb_dev *, u64)) |
| 318 | { |
| 319 | u64 db_bits; |
| 320 | char *buf, cmd; |
| 321 | ssize_t rc; |
| 322 | int n; |
| 323 | |
| 324 | buf = kmalloc(size + 1, GFP_KERNEL); |
| 325 | if (!buf) |
| 326 | return -ENOMEM; |
| 327 | |
| 328 | rc = simple_write_to_buffer(buf, size, offp, ubuf, size); |
| 329 | if (rc < 0) { |
| 330 | kfree(buf); |
| 331 | return rc; |
| 332 | } |
| 333 | |
| 334 | buf[size] = 0; |
| 335 | |
| 336 | n = sscanf(buf, "%c %lli", &cmd, &db_bits); |
| 337 | |
| 338 | kfree(buf); |
| 339 | |
| 340 | if (n != 2) { |
| 341 | rc = -EINVAL; |
| 342 | } else if (cmd == 's') { |
| 343 | if (!db_set_fn) |
| 344 | rc = -EINVAL; |
| 345 | else |
| 346 | rc = db_set_fn(tc->ntb, db_bits); |
| 347 | } else if (cmd == 'c') { |
| 348 | if (!db_clear_fn) |
| 349 | rc = -EINVAL; |
| 350 | else |
| 351 | rc = db_clear_fn(tc->ntb, db_bits); |
| 352 | } else { |
| 353 | rc = -EINVAL; |
| 354 | } |
| 355 | |
| 356 | return rc ? : size; |
| 357 | } |
| 358 | |
| 359 | static ssize_t tool_spadfn_read(struct tool_ctx *tc, char __user *ubuf, |
| 360 | size_t size, loff_t *offp, |
| 361 | u32 (*spad_read_fn)(struct ntb_dev *, int)) |
| 362 | { |
| 363 | size_t buf_size; |
| 364 | char *buf; |
| 365 | ssize_t pos, rc; |
| 366 | int i, spad_count; |
| 367 | |
| 368 | if (!spad_read_fn) |
| 369 | return -EINVAL; |
| 370 | |
Logan Gunthorpe | 625f080 | 2016-06-20 13:15:08 -0600 | [diff] [blame] | 371 | spad_count = ntb_spad_count(tc->ntb); |
| 372 | |
| 373 | /* |
| 374 | * We multiply the number of spads by 15 to get the buffer size |
| 375 | * this is from 3 for the %d, 10 for the largest hex value |
| 376 | * (0x00000000) and 2 for the tab and line feed. |
| 377 | */ |
| 378 | buf_size = min_t(size_t, size, spad_count * 15); |
Allen Hubbe | 578b881 | 2015-05-21 02:51:39 -0400 | [diff] [blame] | 379 | |
| 380 | buf = kmalloc(buf_size, GFP_KERNEL); |
| 381 | if (!buf) |
| 382 | return -ENOMEM; |
| 383 | |
| 384 | pos = 0; |
| 385 | |
Allen Hubbe | 578b881 | 2015-05-21 02:51:39 -0400 | [diff] [blame] | 386 | for (i = 0; i < spad_count; ++i) { |
| 387 | pos += scnprintf(buf + pos, buf_size - pos, "%d\t%#x\n", |
| 388 | i, spad_read_fn(tc->ntb, i)); |
| 389 | } |
| 390 | |
| 391 | rc = simple_read_from_buffer(ubuf, size, offp, buf, pos); |
| 392 | |
| 393 | kfree(buf); |
| 394 | |
| 395 | return rc; |
| 396 | } |
| 397 | |
| 398 | static ssize_t tool_spadfn_write(struct tool_ctx *tc, |
| 399 | const char __user *ubuf, |
| 400 | size_t size, loff_t *offp, |
| 401 | int (*spad_write_fn)(struct ntb_dev *, |
| 402 | int, u32)) |
| 403 | { |
| 404 | int spad_idx; |
| 405 | u32 spad_val; |
Logan Gunthorpe | c792eba | 2016-05-27 14:38:31 -0600 | [diff] [blame] | 406 | char *buf, *buf_ptr; |
Allen Hubbe | 578b881 | 2015-05-21 02:51:39 -0400 | [diff] [blame] | 407 | int pos, n; |
| 408 | ssize_t rc; |
| 409 | |
| 410 | if (!spad_write_fn) { |
| 411 | dev_dbg(&tc->ntb->dev, "no spad write fn\n"); |
| 412 | return -EINVAL; |
| 413 | } |
| 414 | |
| 415 | buf = kmalloc(size + 1, GFP_KERNEL); |
| 416 | if (!buf) |
| 417 | return -ENOMEM; |
| 418 | |
| 419 | rc = simple_write_to_buffer(buf, size, offp, ubuf, size); |
| 420 | if (rc < 0) { |
| 421 | kfree(buf); |
| 422 | return rc; |
| 423 | } |
| 424 | |
| 425 | buf[size] = 0; |
Logan Gunthorpe | c792eba | 2016-05-27 14:38:31 -0600 | [diff] [blame] | 426 | buf_ptr = buf; |
| 427 | n = sscanf(buf_ptr, "%d %i%n", &spad_idx, &spad_val, &pos); |
Allen Hubbe | 578b881 | 2015-05-21 02:51:39 -0400 | [diff] [blame] | 428 | while (n == 2) { |
Logan Gunthorpe | c792eba | 2016-05-27 14:38:31 -0600 | [diff] [blame] | 429 | buf_ptr += pos; |
Allen Hubbe | 578b881 | 2015-05-21 02:51:39 -0400 | [diff] [blame] | 430 | rc = spad_write_fn(tc->ntb, spad_idx, spad_val); |
| 431 | if (rc) |
| 432 | break; |
| 433 | |
Logan Gunthorpe | c792eba | 2016-05-27 14:38:31 -0600 | [diff] [blame] | 434 | n = sscanf(buf_ptr, "%d %i%n", &spad_idx, &spad_val, &pos); |
Allen Hubbe | 578b881 | 2015-05-21 02:51:39 -0400 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | if (n < 0) |
| 438 | rc = n; |
| 439 | |
| 440 | kfree(buf); |
| 441 | |
| 442 | return rc ? : size; |
| 443 | } |
| 444 | |
| 445 | static ssize_t tool_db_read(struct file *filep, char __user *ubuf, |
| 446 | size_t size, loff_t *offp) |
| 447 | { |
| 448 | struct tool_ctx *tc = filep->private_data; |
| 449 | |
| 450 | return tool_dbfn_read(tc, ubuf, size, offp, |
| 451 | tc->ntb->ops->db_read); |
| 452 | } |
| 453 | |
| 454 | static ssize_t tool_db_write(struct file *filep, const char __user *ubuf, |
| 455 | size_t size, loff_t *offp) |
| 456 | { |
| 457 | struct tool_ctx *tc = filep->private_data; |
| 458 | |
| 459 | return tool_dbfn_write(tc, ubuf, size, offp, |
| 460 | tc->ntb->ops->db_set, |
| 461 | tc->ntb->ops->db_clear); |
| 462 | } |
| 463 | |
| 464 | static TOOL_FOPS_RDWR(tool_db_fops, |
| 465 | tool_db_read, |
| 466 | tool_db_write); |
| 467 | |
| 468 | static ssize_t tool_mask_read(struct file *filep, char __user *ubuf, |
| 469 | size_t size, loff_t *offp) |
| 470 | { |
| 471 | struct tool_ctx *tc = filep->private_data; |
| 472 | |
| 473 | return tool_dbfn_read(tc, ubuf, size, offp, |
| 474 | tc->ntb->ops->db_read_mask); |
| 475 | } |
| 476 | |
| 477 | static ssize_t tool_mask_write(struct file *filep, const char __user *ubuf, |
| 478 | size_t size, loff_t *offp) |
| 479 | { |
| 480 | struct tool_ctx *tc = filep->private_data; |
| 481 | |
| 482 | return tool_dbfn_write(tc, ubuf, size, offp, |
| 483 | tc->ntb->ops->db_set_mask, |
| 484 | tc->ntb->ops->db_clear_mask); |
| 485 | } |
| 486 | |
| 487 | static TOOL_FOPS_RDWR(tool_mask_fops, |
| 488 | tool_mask_read, |
| 489 | tool_mask_write); |
| 490 | |
| 491 | static ssize_t tool_peer_db_read(struct file *filep, char __user *ubuf, |
| 492 | size_t size, loff_t *offp) |
| 493 | { |
| 494 | struct tool_ctx *tc = filep->private_data; |
| 495 | |
| 496 | return tool_dbfn_read(tc, ubuf, size, offp, |
| 497 | tc->ntb->ops->peer_db_read); |
| 498 | } |
| 499 | |
| 500 | static ssize_t tool_peer_db_write(struct file *filep, const char __user *ubuf, |
| 501 | size_t size, loff_t *offp) |
| 502 | { |
| 503 | struct tool_ctx *tc = filep->private_data; |
| 504 | |
| 505 | return tool_dbfn_write(tc, ubuf, size, offp, |
| 506 | tc->ntb->ops->peer_db_set, |
| 507 | tc->ntb->ops->peer_db_clear); |
| 508 | } |
| 509 | |
| 510 | static TOOL_FOPS_RDWR(tool_peer_db_fops, |
| 511 | tool_peer_db_read, |
| 512 | tool_peer_db_write); |
| 513 | |
| 514 | static ssize_t tool_peer_mask_read(struct file *filep, char __user *ubuf, |
| 515 | size_t size, loff_t *offp) |
| 516 | { |
| 517 | struct tool_ctx *tc = filep->private_data; |
| 518 | |
| 519 | return tool_dbfn_read(tc, ubuf, size, offp, |
| 520 | tc->ntb->ops->peer_db_read_mask); |
| 521 | } |
| 522 | |
| 523 | static ssize_t tool_peer_mask_write(struct file *filep, const char __user *ubuf, |
| 524 | size_t size, loff_t *offp) |
| 525 | { |
| 526 | struct tool_ctx *tc = filep->private_data; |
| 527 | |
| 528 | return tool_dbfn_write(tc, ubuf, size, offp, |
| 529 | tc->ntb->ops->peer_db_set_mask, |
| 530 | tc->ntb->ops->peer_db_clear_mask); |
| 531 | } |
| 532 | |
| 533 | static TOOL_FOPS_RDWR(tool_peer_mask_fops, |
| 534 | tool_peer_mask_read, |
| 535 | tool_peer_mask_write); |
| 536 | |
| 537 | static ssize_t tool_spad_read(struct file *filep, char __user *ubuf, |
| 538 | size_t size, loff_t *offp) |
| 539 | { |
| 540 | struct tool_ctx *tc = filep->private_data; |
| 541 | |
| 542 | return tool_spadfn_read(tc, ubuf, size, offp, |
| 543 | tc->ntb->ops->spad_read); |
| 544 | } |
| 545 | |
| 546 | static ssize_t tool_spad_write(struct file *filep, const char __user *ubuf, |
| 547 | size_t size, loff_t *offp) |
| 548 | { |
| 549 | struct tool_ctx *tc = filep->private_data; |
| 550 | |
| 551 | return tool_spadfn_write(tc, ubuf, size, offp, |
| 552 | tc->ntb->ops->spad_write); |
| 553 | } |
| 554 | |
| 555 | static TOOL_FOPS_RDWR(tool_spad_fops, |
| 556 | tool_spad_read, |
| 557 | tool_spad_write); |
| 558 | |
| 559 | static ssize_t tool_peer_spad_read(struct file *filep, char __user *ubuf, |
| 560 | size_t size, loff_t *offp) |
| 561 | { |
| 562 | struct tool_ctx *tc = filep->private_data; |
| 563 | |
| 564 | return tool_spadfn_read(tc, ubuf, size, offp, |
| 565 | tc->ntb->ops->peer_spad_read); |
| 566 | } |
| 567 | |
| 568 | static ssize_t tool_peer_spad_write(struct file *filep, const char __user *ubuf, |
| 569 | size_t size, loff_t *offp) |
| 570 | { |
| 571 | struct tool_ctx *tc = filep->private_data; |
| 572 | |
| 573 | return tool_spadfn_write(tc, ubuf, size, offp, |
| 574 | tc->ntb->ops->peer_spad_write); |
| 575 | } |
| 576 | |
| 577 | static TOOL_FOPS_RDWR(tool_peer_spad_fops, |
| 578 | tool_peer_spad_read, |
| 579 | tool_peer_spad_write); |
| 580 | |
Logan Gunthorpe | 8b71d28 | 2016-06-03 14:50:33 -0600 | [diff] [blame^] | 581 | |
| 582 | static ssize_t tool_mw_read(struct file *filep, char __user *ubuf, |
| 583 | size_t size, loff_t *offp) |
| 584 | { |
| 585 | struct tool_mw *mw = filep->private_data; |
| 586 | ssize_t rc; |
| 587 | loff_t pos = *offp; |
| 588 | void *buf; |
| 589 | |
| 590 | if (mw->local == NULL) |
| 591 | return -EIO; |
| 592 | if (pos < 0) |
| 593 | return -EINVAL; |
| 594 | if (pos >= mw->size || !size) |
| 595 | return 0; |
| 596 | if (size > mw->size - pos) |
| 597 | size = mw->size - pos; |
| 598 | |
| 599 | buf = kmalloc(size, GFP_KERNEL); |
| 600 | if (!buf) |
| 601 | return -ENOMEM; |
| 602 | |
| 603 | memcpy_fromio(buf, mw->local + pos, size); |
| 604 | rc = copy_to_user(ubuf, buf, size); |
| 605 | if (rc == size) { |
| 606 | rc = -EFAULT; |
| 607 | goto err_free; |
| 608 | } |
| 609 | |
| 610 | size -= rc; |
| 611 | *offp = pos + size; |
| 612 | rc = size; |
| 613 | |
| 614 | err_free: |
| 615 | kfree(buf); |
| 616 | |
| 617 | return rc; |
| 618 | } |
| 619 | |
| 620 | static ssize_t tool_mw_write(struct file *filep, const char __user *ubuf, |
| 621 | size_t size, loff_t *offp) |
| 622 | { |
| 623 | struct tool_mw *mw = filep->private_data; |
| 624 | ssize_t rc; |
| 625 | loff_t pos = *offp; |
| 626 | void *buf; |
| 627 | |
| 628 | if (pos < 0) |
| 629 | return -EINVAL; |
| 630 | if (pos >= mw->size || !size) |
| 631 | return 0; |
| 632 | if (size > mw->size - pos) |
| 633 | size = mw->size - pos; |
| 634 | |
| 635 | buf = kmalloc(size, GFP_KERNEL); |
| 636 | if (!buf) |
| 637 | return -ENOMEM; |
| 638 | |
| 639 | rc = copy_from_user(buf, ubuf, size); |
| 640 | if (rc == size) { |
| 641 | rc = -EFAULT; |
| 642 | goto err_free; |
| 643 | } |
| 644 | |
| 645 | size -= rc; |
| 646 | *offp = pos + size; |
| 647 | rc = size; |
| 648 | |
| 649 | memcpy_toio(mw->local + pos, buf, size); |
| 650 | |
| 651 | err_free: |
| 652 | kfree(buf); |
| 653 | |
| 654 | return rc; |
| 655 | } |
| 656 | |
| 657 | static TOOL_FOPS_RDWR(tool_mw_fops, |
| 658 | tool_mw_read, |
| 659 | tool_mw_write); |
| 660 | |
| 661 | |
| 662 | static ssize_t tool_peer_mw_read(struct file *filep, char __user *ubuf, |
| 663 | size_t size, loff_t *offp) |
| 664 | { |
| 665 | struct tool_mw *mw = filep->private_data; |
| 666 | |
| 667 | return simple_read_from_buffer(ubuf, size, offp, mw->peer, mw->size); |
| 668 | } |
| 669 | |
| 670 | static ssize_t tool_peer_mw_write(struct file *filep, const char __user *ubuf, |
| 671 | size_t size, loff_t *offp) |
| 672 | { |
| 673 | struct tool_mw *mw = filep->private_data; |
| 674 | |
| 675 | return simple_write_to_buffer(mw->peer, mw->size, offp, ubuf, size); |
| 676 | } |
| 677 | |
| 678 | static TOOL_FOPS_RDWR(tool_peer_mw_fops, |
| 679 | tool_peer_mw_read, |
| 680 | tool_peer_mw_write); |
| 681 | |
Allen Hubbe | 578b881 | 2015-05-21 02:51:39 -0400 | [diff] [blame] | 682 | static void tool_setup_dbgfs(struct tool_ctx *tc) |
| 683 | { |
Logan Gunthorpe | 8b71d28 | 2016-06-03 14:50:33 -0600 | [diff] [blame^] | 684 | int mw_count; |
| 685 | int i; |
| 686 | |
Allen Hubbe | 578b881 | 2015-05-21 02:51:39 -0400 | [diff] [blame] | 687 | /* This modules is useless without dbgfs... */ |
| 688 | if (!tool_dbgfs) { |
| 689 | tc->dbgfs = NULL; |
| 690 | return; |
| 691 | } |
| 692 | |
| 693 | tc->dbgfs = debugfs_create_dir(dev_name(&tc->ntb->dev), |
| 694 | tool_dbgfs); |
| 695 | if (!tc->dbgfs) |
| 696 | return; |
| 697 | |
| 698 | debugfs_create_file("db", S_IRUSR | S_IWUSR, tc->dbgfs, |
| 699 | tc, &tool_db_fops); |
| 700 | |
| 701 | debugfs_create_file("mask", S_IRUSR | S_IWUSR, tc->dbgfs, |
| 702 | tc, &tool_mask_fops); |
| 703 | |
| 704 | debugfs_create_file("peer_db", S_IRUSR | S_IWUSR, tc->dbgfs, |
| 705 | tc, &tool_peer_db_fops); |
| 706 | |
| 707 | debugfs_create_file("peer_mask", S_IRUSR | S_IWUSR, tc->dbgfs, |
| 708 | tc, &tool_peer_mask_fops); |
| 709 | |
| 710 | debugfs_create_file("spad", S_IRUSR | S_IWUSR, tc->dbgfs, |
| 711 | tc, &tool_spad_fops); |
| 712 | |
| 713 | debugfs_create_file("peer_spad", S_IRUSR | S_IWUSR, tc->dbgfs, |
| 714 | tc, &tool_peer_spad_fops); |
Logan Gunthorpe | 8b71d28 | 2016-06-03 14:50:33 -0600 | [diff] [blame^] | 715 | |
| 716 | mw_count = min(ntb_mw_count(tc->ntb), MAX_MWS); |
| 717 | for (i = 0; i < mw_count; i++) { |
| 718 | char buf[30]; |
| 719 | |
| 720 | snprintf(buf, sizeof(buf), "mw%d", i); |
| 721 | debugfs_create_file(buf, S_IRUSR | S_IWUSR, tc->dbgfs, |
| 722 | &tc->mws[i], &tool_mw_fops); |
| 723 | |
| 724 | snprintf(buf, sizeof(buf), "peer_mw%d", i); |
| 725 | debugfs_create_file(buf, S_IRUSR | S_IWUSR, tc->dbgfs, |
| 726 | &tc->mws[i], &tool_peer_mw_fops); |
| 727 | |
| 728 | } |
Allen Hubbe | 578b881 | 2015-05-21 02:51:39 -0400 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | static int tool_probe(struct ntb_client *self, struct ntb_dev *ntb) |
| 732 | { |
| 733 | struct tool_ctx *tc; |
| 734 | int rc; |
| 735 | |
| 736 | if (ntb_db_is_unsafe(ntb)) |
| 737 | dev_dbg(&ntb->dev, "doorbell is unsafe\n"); |
| 738 | |
| 739 | if (ntb_spad_is_unsafe(ntb)) |
| 740 | dev_dbg(&ntb->dev, "scratchpad is unsafe\n"); |
| 741 | |
Logan Gunthorpe | 8b71d28 | 2016-06-03 14:50:33 -0600 | [diff] [blame^] | 742 | tc = kzalloc(sizeof(*tc), GFP_KERNEL); |
Allen Hubbe | 578b881 | 2015-05-21 02:51:39 -0400 | [diff] [blame] | 743 | if (!tc) { |
| 744 | rc = -ENOMEM; |
| 745 | goto err_tc; |
| 746 | } |
| 747 | |
| 748 | tc->ntb = ntb; |
Logan Gunthorpe | 8b71d28 | 2016-06-03 14:50:33 -0600 | [diff] [blame^] | 749 | INIT_DELAYED_WORK(&tc->link_work, tool_link_work); |
| 750 | INIT_WORK(&tc->link_cleanup, tool_link_cleanup); |
Allen Hubbe | 578b881 | 2015-05-21 02:51:39 -0400 | [diff] [blame] | 751 | |
| 752 | tool_setup_dbgfs(tc); |
| 753 | |
| 754 | rc = ntb_set_ctx(ntb, tc, &tool_ops); |
| 755 | if (rc) |
| 756 | goto err_ctx; |
| 757 | |
| 758 | ntb_link_enable(ntb, NTB_SPEED_AUTO, NTB_WIDTH_AUTO); |
| 759 | ntb_link_event(ntb); |
| 760 | |
| 761 | return 0; |
| 762 | |
| 763 | err_ctx: |
| 764 | debugfs_remove_recursive(tc->dbgfs); |
Logan Gunthorpe | 8b71d28 | 2016-06-03 14:50:33 -0600 | [diff] [blame^] | 765 | cancel_delayed_work_sync(&tc->link_work); |
| 766 | cancel_work_sync(&tc->link_cleanup); |
Allen Hubbe | 578b881 | 2015-05-21 02:51:39 -0400 | [diff] [blame] | 767 | kfree(tc); |
| 768 | err_tc: |
| 769 | return rc; |
| 770 | } |
| 771 | |
| 772 | static void tool_remove(struct ntb_client *self, struct ntb_dev *ntb) |
| 773 | { |
| 774 | struct tool_ctx *tc = ntb->ctx; |
| 775 | |
Logan Gunthorpe | 8b71d28 | 2016-06-03 14:50:33 -0600 | [diff] [blame^] | 776 | cancel_delayed_work_sync(&tc->link_work); |
| 777 | cancel_work_sync(&tc->link_cleanup); |
| 778 | |
| 779 | tool_free_mws(tc); |
| 780 | |
Allen Hubbe | 578b881 | 2015-05-21 02:51:39 -0400 | [diff] [blame] | 781 | ntb_clear_ctx(ntb); |
| 782 | ntb_link_disable(ntb); |
| 783 | |
| 784 | debugfs_remove_recursive(tc->dbgfs); |
| 785 | kfree(tc); |
| 786 | } |
| 787 | |
| 788 | static struct ntb_client tool_client = { |
| 789 | .ops = { |
| 790 | .probe = tool_probe, |
| 791 | .remove = tool_remove, |
| 792 | }, |
| 793 | }; |
| 794 | |
| 795 | static int __init tool_init(void) |
| 796 | { |
| 797 | int rc; |
| 798 | |
| 799 | if (debugfs_initialized()) |
| 800 | tool_dbgfs = debugfs_create_dir(KBUILD_MODNAME, NULL); |
| 801 | |
| 802 | rc = ntb_register_client(&tool_client); |
| 803 | if (rc) |
| 804 | goto err_client; |
| 805 | |
| 806 | return 0; |
| 807 | |
| 808 | err_client: |
| 809 | debugfs_remove_recursive(tool_dbgfs); |
| 810 | return rc; |
| 811 | } |
| 812 | module_init(tool_init); |
| 813 | |
| 814 | static void __exit tool_exit(void) |
| 815 | { |
| 816 | ntb_unregister_client(&tool_client); |
| 817 | debugfs_remove_recursive(tool_dbgfs); |
| 818 | } |
| 819 | module_exit(tool_exit); |