1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * rio_cm - RapidIO Channelized Messaging Driver 4 * 5 * Copyright 2013-2016 Integrated Device Technology, Inc. 6 * Copyright (c) 2015, Prodrive Technologies 7 * Copyright (c) 2015, RapidIO Trade Association 8 */ 9 10 #include <linux/module.h> 11 #include <linux/kernel.h> 12 #include <linux/dma-mapping.h> 13 #include <linux/delay.h> 14 #include <linux/sched.h> 15 #include <linux/rio.h> 16 #include <linux/rio_drv.h> 17 #include <linux/slab.h> 18 #include <linux/idr.h> 19 #include <linux/interrupt.h> 20 #include <linux/cdev.h> 21 #include <linux/fs.h> 22 #include <linux/poll.h> 23 #include <linux/reboot.h> 24 #include <linux/bitops.h> 25 #include <linux/printk.h> 26 #include <linux/rio_cm_cdev.h> 27 28 #define DRV_NAME "rio_cm" 29 #define DRV_VERSION "1.0.0" 30 #define DRV_AUTHOR "Alexandre Bounine <alexandre.bounine@idt.com>" 31 #define DRV_DESC "RapidIO Channelized Messaging Driver" 32 #define DEV_NAME "rio_cm" 33 34 /* Debug output filtering masks */ 35 enum { 36 DBG_NONE = 0, 37 DBG_INIT = BIT(0), /* driver init */ 38 DBG_EXIT = BIT(1), /* driver exit */ 39 DBG_MPORT = BIT(2), /* mport add/remove */ 40 DBG_RDEV = BIT(3), /* RapidIO device add/remove */ 41 DBG_CHOP = BIT(4), /* channel operations */ 42 DBG_WAIT = BIT(5), /* waiting for events */ 43 DBG_TX = BIT(6), /* message TX */ 44 DBG_TX_EVENT = BIT(7), /* message TX event */ 45 DBG_RX_DATA = BIT(8), /* inbound data messages */ 46 DBG_RX_CMD = BIT(9), /* inbound REQ/ACK/NACK messages */ 47 DBG_ALL = ~0, 48 }; 49 50 #ifdef DEBUG 51 #define riocm_debug(level, fmt, arg...) \ 52 do { \ 53 if (DBG_##level & dbg_level) \ 54 pr_debug(DRV_NAME ": %s " fmt "\n", \ 55 __func__, ##arg); \ 56 } while (0) 57 #else 58 #define riocm_debug(level, fmt, arg...) \ 59 no_printk(KERN_DEBUG pr_fmt(DRV_NAME fmt "\n"), ##arg) 60 #endif 61 62 #define riocm_warn(fmt, arg...) \ 63 pr_warn(DRV_NAME ": %s WARNING " fmt "\n", __func__, ##arg) 64 65 #define riocm_error(fmt, arg...) \ 66 pr_err(DRV_NAME ": %s ERROR " fmt "\n", __func__, ##arg) 67 68 69 static int cmbox = 1; 70 module_param(cmbox, int, S_IRUGO); 71 MODULE_PARM_DESC(cmbox, "RapidIO Mailbox number (default 1)"); 72 73 static int chstart = 256; 74 module_param(chstart, int, S_IRUGO); 75 MODULE_PARM_DESC(chstart, 76 "Start channel number for dynamic allocation (default 256)"); 77 78 #ifdef DEBUG 79 static u32 dbg_level = DBG_NONE; 80 module_param(dbg_level, uint, S_IWUSR | S_IRUGO); 81 MODULE_PARM_DESC(dbg_level, "Debugging output level (default 0 = none)"); 82 #endif 83 84 MODULE_AUTHOR(DRV_AUTHOR); 85 MODULE_DESCRIPTION(DRV_DESC); 86 MODULE_LICENSE("GPL"); 87 MODULE_VERSION(DRV_VERSION); 88 89 #define RIOCM_TX_RING_SIZE 128 90 #define RIOCM_RX_RING_SIZE 128 91 #define RIOCM_CONNECT_TO 3 /* connect response TO (in sec) */ 92 93 #define RIOCM_MAX_CHNUM 0xffff /* Use full range of u16 field */ 94 #define RIOCM_CHNUM_AUTO 0 95 #define RIOCM_MAX_EP_COUNT 0x10000 /* Max number of endpoints */ 96 97 enum rio_cm_state { 98 RIO_CM_IDLE, 99 RIO_CM_CONNECT, 100 RIO_CM_CONNECTED, 101 RIO_CM_DISCONNECT, 102 RIO_CM_CHAN_BOUND, 103 RIO_CM_LISTEN, 104 RIO_CM_DESTROYING, 105 }; 106 107 enum rio_cm_pkt_type { 108 RIO_CM_SYS = 0xaa, 109 RIO_CM_CHAN = 0x55, 110 }; 111 112 enum rio_cm_chop { 113 CM_CONN_REQ, 114 CM_CONN_ACK, 115 CM_CONN_CLOSE, 116 CM_DATA_MSG, 117 }; 118 119 struct rio_ch_base_bhdr { 120 u32 src_id; 121 u32 dst_id; 122 #define RIO_HDR_LETTER_MASK 0xffff0000 123 #define RIO_HDR_MBOX_MASK 0x0000ffff 124 u8 src_mbox; 125 u8 dst_mbox; 126 u8 type; 127 } __attribute__((__packed__)); 128 129 struct rio_ch_chan_hdr { 130 struct rio_ch_base_bhdr bhdr; 131 u8 ch_op; 132 u16 dst_ch; 133 u16 src_ch; 134 u16 msg_len; 135 u16 rsrvd; 136 } __attribute__((__packed__)); 137 138 struct tx_req { 139 struct list_head node; 140 struct rio_dev *rdev; 141 void *buffer; 142 size_t len; 143 }; 144 145 struct cm_dev { 146 struct list_head list; 147 struct rio_mport *mport; 148 void *rx_buf[RIOCM_RX_RING_SIZE]; 149 int rx_slots; 150 struct mutex rx_lock; 151 152 void *tx_buf[RIOCM_TX_RING_SIZE]; 153 int tx_slot; 154 int tx_cnt; 155 int tx_ack_slot; 156 struct list_head tx_reqs; 157 spinlock_t tx_lock; 158 159 struct list_head peers; 160 u32 npeers; 161 struct workqueue_struct *rx_wq; 162 struct work_struct rx_work; 163 }; 164 165 struct chan_rx_ring { 166 void *buf[RIOCM_RX_RING_SIZE]; 167 int head; 168 int tail; 169 int count; 170 171 /* Tracking RX buffers reported to upper level */ 172 void *inuse[RIOCM_RX_RING_SIZE]; 173 int inuse_cnt; 174 }; 175 176 struct rio_channel { 177 u16 id; /* local channel ID */ 178 struct kref ref; /* channel refcount */ 179 struct file *filp; 180 struct cm_dev *cmdev; /* associated CM device object */ 181 struct rio_dev *rdev; /* remote RapidIO device */ 182 enum rio_cm_state state; 183 int error; 184 spinlock_t lock; 185 void *context; 186 u32 loc_destid; /* local destID */ 187 u32 rem_destid; /* remote destID */ 188 u16 rem_channel; /* remote channel ID */ 189 struct list_head accept_queue; 190 struct list_head ch_node; 191 struct completion comp; 192 struct completion comp_close; 193 struct chan_rx_ring rx_ring; 194 }; 195 196 struct cm_peer { 197 struct list_head node; 198 struct rio_dev *rdev; 199 }; 200 201 struct conn_req { 202 struct list_head node; 203 u32 destid; /* requester destID */ 204 u16 chan; /* requester channel ID */ 205 struct cm_dev *cmdev; 206 }; 207 208 /* 209 * A channel_dev structure represents a CM_CDEV 210 * @cdev Character device 211 * @dev Associated device object 212 */ 213 struct channel_dev { 214 struct cdev cdev; 215 struct device *dev; 216 }; 217 218 static struct rio_channel *riocm_ch_alloc(u16 ch_num); 219 static void riocm_ch_free(struct kref *ref); 220 static int riocm_post_send(struct cm_dev *cm, struct rio_dev *rdev, 221 void *buffer, size_t len); 222 static int riocm_ch_close(struct rio_channel *ch); 223 224 static DEFINE_SPINLOCK(idr_lock); 225 static DEFINE_IDR(ch_idr); 226 227 static LIST_HEAD(cm_dev_list); 228 static DECLARE_RWSEM(rdev_sem); 229 230 static const struct class dev_class = { 231 .name = DRV_NAME, 232 }; 233 static unsigned int dev_major; 234 static unsigned int dev_minor_base; 235 static dev_t dev_number; 236 static struct channel_dev riocm_cdev; 237 238 #define is_msg_capable(src_ops, dst_ops) \ 239 ((src_ops & RIO_SRC_OPS_DATA_MSG) && \ 240 (dst_ops & RIO_DST_OPS_DATA_MSG)) 241 #define dev_cm_capable(dev) \ 242 is_msg_capable(dev->src_ops, dev->dst_ops) 243 244 static int riocm_cmp(struct rio_channel *ch, enum rio_cm_state cmp) 245 { 246 int ret; 247 248 spin_lock_bh(&ch->lock); 249 ret = (ch->state == cmp); 250 spin_unlock_bh(&ch->lock); 251 return ret; 252 } 253 254 static int riocm_cmp_exch(struct rio_channel *ch, 255 enum rio_cm_state cmp, enum rio_cm_state exch) 256 { 257 int ret; 258 259 spin_lock_bh(&ch->lock); 260 ret = (ch->state == cmp); 261 if (ret) 262 ch->state = exch; 263 spin_unlock_bh(&ch->lock); 264 return ret; 265 } 266 267 static enum rio_cm_state riocm_exch(struct rio_channel *ch, 268 enum rio_cm_state exch) 269 { 270 enum rio_cm_state old; 271 272 spin_lock_bh(&ch->lock); 273 old = ch->state; 274 ch->state = exch; 275 spin_unlock_bh(&ch->lock); 276 return old; 277 } 278 279 static struct rio_channel *riocm_get_channel(u16 nr) 280 { 281 struct rio_channel *ch; 282 283 spin_lock_bh(&idr_lock); 284 ch = idr_find(&ch_idr, nr); 285 if (ch) 286 kref_get(&ch->ref); 287 spin_unlock_bh(&idr_lock); 288 return ch; 289 } 290 291 static void riocm_put_channel(struct rio_channel *ch) 292 { 293 kref_put(&ch->ref, riocm_ch_free); 294 } 295 296 static void *riocm_rx_get_msg(struct cm_dev *cm) 297 { 298 void *msg; 299 int i; 300 301 msg = rio_get_inb_message(cm->mport, cmbox); 302 if (msg) { 303 for (i = 0; i < RIOCM_RX_RING_SIZE; i++) { 304 if (cm->rx_buf[i] == msg) { 305 cm->rx_buf[i] = NULL; 306 cm->rx_slots++; 307 break; 308 } 309 } 310 311 if (i == RIOCM_RX_RING_SIZE) 312 riocm_warn("no record for buffer 0x%p", msg); 313 } 314 315 return msg; 316 } 317 318 /* 319 * riocm_rx_fill - fills a ring of receive buffers for given cm device 320 * @cm: cm_dev object 321 * @nent: max number of entries to fill 322 * 323 * Returns: none 324 */ 325 static void riocm_rx_fill(struct cm_dev *cm, int nent) 326 { 327 int i; 328 329 if (cm->rx_slots == 0) 330 return; 331 332 for (i = 0; i < RIOCM_RX_RING_SIZE && cm->rx_slots && nent; i++) { 333 if (cm->rx_buf[i] == NULL) { 334 cm->rx_buf[i] = kmalloc(RIO_MAX_MSG_SIZE, GFP_KERNEL); 335 if (cm->rx_buf[i] == NULL) 336 break; 337 rio_add_inb_buffer(cm->mport, cmbox, cm->rx_buf[i]); 338 cm->rx_slots--; 339 nent--; 340 } 341 } 342 } 343 344 /* 345 * riocm_rx_free - frees all receive buffers associated with given cm device 346 * @cm: cm_dev object 347 * 348 * Returns: none 349 */ 350 static void riocm_rx_free(struct cm_dev *cm) 351 { 352 int i; 353 354 for (i = 0; i < RIOCM_RX_RING_SIZE; i++) { 355 if (cm->rx_buf[i] != NULL) { 356 kfree(cm->rx_buf[i]); 357 cm->rx_buf[i] = NULL; 358 } 359 } 360 } 361 362 /* 363 * riocm_req_handler - connection request handler 364 * @cm: cm_dev object 365 * @req_data: pointer to the request packet 366 * 367 * Returns: 0 if success, or 368 * -EINVAL if channel is not in correct state, 369 * -ENODEV if cannot find a channel with specified ID, 370 * -ENOMEM if unable to allocate memory to store the request 371 */ 372 static int riocm_req_handler(struct cm_dev *cm, void *req_data) 373 { 374 struct rio_channel *ch; 375 struct conn_req *req; 376 struct rio_ch_chan_hdr *hh = req_data; 377 u16 chnum; 378 379 chnum = ntohs(hh->dst_ch); 380 381 ch = riocm_get_channel(chnum); 382 383 if (!ch) 384 return -ENODEV; 385 386 if (ch->state != RIO_CM_LISTEN) { 387 riocm_debug(RX_CMD, "channel %d is not in listen state", chnum); 388 riocm_put_channel(ch); 389 return -EINVAL; 390 } 391 392 req = kzalloc(sizeof(*req), GFP_KERNEL); 393 if (!req) { 394 riocm_put_channel(ch); 395 return -ENOMEM; 396 } 397 398 req->destid = ntohl(hh->bhdr.src_id); 399 req->chan = ntohs(hh->src_ch); 400 req->cmdev = cm; 401 402 spin_lock_bh(&ch->lock); 403 list_add_tail(&req->node, &ch->accept_queue); 404 spin_unlock_bh(&ch->lock); 405 complete(&ch->comp); 406 riocm_put_channel(ch); 407 408 return 0; 409 } 410 411 /* 412 * riocm_resp_handler - response to connection request handler 413 * @resp_data: pointer to the response packet 414 * 415 * Returns: 0 if success, or 416 * -EINVAL if channel is not in correct state, 417 * -ENODEV if cannot find a channel with specified ID, 418 */ 419 static int riocm_resp_handler(void *resp_data) 420 { 421 struct rio_channel *ch; 422 struct rio_ch_chan_hdr *hh = resp_data; 423 u16 chnum; 424 425 chnum = ntohs(hh->dst_ch); 426 ch = riocm_get_channel(chnum); 427 if (!ch) 428 return -ENODEV; 429 430 if (ch->state != RIO_CM_CONNECT) { 431 riocm_put_channel(ch); 432 return -EINVAL; 433 } 434 435 riocm_exch(ch, RIO_CM_CONNECTED); 436 ch->rem_channel = ntohs(hh->src_ch); 437 complete(&ch->comp); 438 riocm_put_channel(ch); 439 440 return 0; 441 } 442 443 /* 444 * riocm_close_handler - channel close request handler 445 * @req_data: pointer to the request packet 446 * 447 * Returns: 0 if success, or 448 * -ENODEV if cannot find a channel with specified ID, 449 * + error codes returned by riocm_ch_close. 450 */ 451 static int riocm_close_handler(void *data) 452 { 453 struct rio_channel *ch; 454 struct rio_ch_chan_hdr *hh = data; 455 int ret; 456 457 riocm_debug(RX_CMD, "for ch=%d", ntohs(hh->dst_ch)); 458 459 spin_lock_bh(&idr_lock); 460 ch = idr_find(&ch_idr, ntohs(hh->dst_ch)); 461 if (!ch) { 462 spin_unlock_bh(&idr_lock); 463 return -ENODEV; 464 } 465 idr_remove(&ch_idr, ch->id); 466 spin_unlock_bh(&idr_lock); 467 468 riocm_exch(ch, RIO_CM_DISCONNECT); 469 470 ret = riocm_ch_close(ch); 471 if (ret) 472 riocm_debug(RX_CMD, "riocm_ch_close() returned %d", ret); 473 474 return 0; 475 } 476 477 /* 478 * rio_cm_handler - function that services request (non-data) packets 479 * @cm: cm_dev object 480 * @data: pointer to the packet 481 */ 482 static void rio_cm_handler(struct cm_dev *cm, void *data) 483 { 484 struct rio_ch_chan_hdr *hdr; 485 486 if (!rio_mport_is_running(cm->mport)) 487 goto out; 488 489 hdr = data; 490 491 riocm_debug(RX_CMD, "OP=%x for ch=%d from %d", 492 hdr->ch_op, ntohs(hdr->dst_ch), ntohs(hdr->src_ch)); 493 494 switch (hdr->ch_op) { 495 case CM_CONN_REQ: 496 riocm_req_handler(cm, data); 497 break; 498 case CM_CONN_ACK: 499 riocm_resp_handler(data); 500 break; 501 case CM_CONN_CLOSE: 502 riocm_close_handler(data); 503 break; 504 default: 505 riocm_error("Invalid packet header"); 506 break; 507 } 508 out: 509 kfree(data); 510 } 511 512 /* 513 * rio_rx_data_handler - received data packet handler 514 * @cm: cm_dev object 515 * @buf: data packet 516 * 517 * Returns: 0 if success, or 518 * -ENODEV if cannot find a channel with specified ID, 519 * -EIO if channel is not in CONNECTED state, 520 * -ENOMEM if channel RX queue is full (packet discarded) 521 */ 522 static int rio_rx_data_handler(struct cm_dev *cm, void *buf) 523 { 524 struct rio_ch_chan_hdr *hdr; 525 struct rio_channel *ch; 526 527 hdr = buf; 528 529 riocm_debug(RX_DATA, "for ch=%d", ntohs(hdr->dst_ch)); 530 531 ch = riocm_get_channel(ntohs(hdr->dst_ch)); 532 if (!ch) { 533 /* Discard data message for non-existing channel */ 534 kfree(buf); 535 return -ENODEV; 536 } 537 538 /* Place pointer to the buffer into channel's RX queue */ 539 spin_lock(&ch->lock); 540 541 if (ch->state != RIO_CM_CONNECTED) { 542 /* Channel is not ready to receive data, discard a packet */ 543 riocm_debug(RX_DATA, "ch=%d is in wrong state=%d", 544 ch->id, ch->state); 545 spin_unlock(&ch->lock); 546 kfree(buf); 547 riocm_put_channel(ch); 548 return -EIO; 549 } 550 551 if (ch->rx_ring.count == RIOCM_RX_RING_SIZE) { 552 /* If RX ring is full, discard a packet */ 553 riocm_debug(RX_DATA, "ch=%d is full", ch->id); 554 spin_unlock(&ch->lock); 555 kfree(buf); 556 riocm_put_channel(ch); 557 return -ENOMEM; 558 } 559 560 ch->rx_ring.buf[ch->rx_ring.head] = buf; 561 ch->rx_ring.head++; 562 ch->rx_ring.count++; 563 ch->rx_ring.head %= RIOCM_RX_RING_SIZE; 564 565 complete(&ch->comp); 566 567 spin_unlock(&ch->lock); 568 riocm_put_channel(ch); 569 570 return 0; 571 } 572 573 /* 574 * rio_ibmsg_handler - inbound message packet handler 575 */ 576 static void rio_ibmsg_handler(struct work_struct *work) 577 { 578 struct cm_dev *cm = container_of(work, struct cm_dev, rx_work); 579 void *data; 580 struct rio_ch_chan_hdr *hdr; 581 582 if (!rio_mport_is_running(cm->mport)) 583 return; 584 585 while (1) { 586 mutex_lock(&cm->rx_lock); 587 data = riocm_rx_get_msg(cm); 588 if (data) 589 riocm_rx_fill(cm, 1); 590 mutex_unlock(&cm->rx_lock); 591 592 if (data == NULL) 593 break; 594 595 hdr = data; 596 597 if (hdr->bhdr.type != RIO_CM_CHAN) { 598 /* For now simply discard packets other than channel */ 599 riocm_error("Unsupported TYPE code (0x%x). Msg dropped", 600 hdr->bhdr.type); 601 kfree(data); 602 continue; 603 } 604 605 /* Process a channel message */ 606 if (hdr->ch_op == CM_DATA_MSG) 607 rio_rx_data_handler(cm, data); 608 else 609 rio_cm_handler(cm, data); 610 } 611 } 612 613 static void riocm_inb_msg_event(struct rio_mport *mport, void *dev_id, 614 int mbox, int slot) 615 { 616 struct cm_dev *cm = dev_id; 617 618 if (rio_mport_is_running(cm->mport) && !work_pending(&cm->rx_work)) 619 queue_work(cm->rx_wq, &cm->rx_work); 620 } 621 622 /* 623 * rio_txcq_handler - TX completion handler 624 * @cm: cm_dev object 625 * @slot: TX queue slot 626 * 627 * TX completion handler also ensures that pending request packets are placed 628 * into transmit queue as soon as a free slot becomes available. This is done 629 * to give higher priority to request packets during high intensity data flow. 630 */ 631 static void rio_txcq_handler(struct cm_dev *cm, int slot) 632 { 633 int ack_slot; 634 635 /* ATTN: Add TX completion notification if/when direct buffer 636 * transfer is implemented. At this moment only correct tracking 637 * of tx_count is important. 638 */ 639 riocm_debug(TX_EVENT, "for mport_%d slot %d tx_cnt %d", 640 cm->mport->id, slot, cm->tx_cnt); 641 642 spin_lock(&cm->tx_lock); 643 ack_slot = cm->tx_ack_slot; 644 645 if (ack_slot == slot) 646 riocm_debug(TX_EVENT, "slot == ack_slot"); 647 648 while (cm->tx_cnt && ((ack_slot != slot) || 649 (cm->tx_cnt == RIOCM_TX_RING_SIZE))) { 650 651 cm->tx_buf[ack_slot] = NULL; 652 ++ack_slot; 653 ack_slot &= (RIOCM_TX_RING_SIZE - 1); 654 cm->tx_cnt--; 655 } 656 657 if (cm->tx_cnt < 0 || cm->tx_cnt > RIOCM_TX_RING_SIZE) 658 riocm_error("tx_cnt %d out of sync", cm->tx_cnt); 659 660 WARN_ON((cm->tx_cnt < 0) || (cm->tx_cnt > RIOCM_TX_RING_SIZE)); 661 662 cm->tx_ack_slot = ack_slot; 663 664 /* 665 * If there are pending requests, insert them into transmit queue 666 */ 667 if (!list_empty(&cm->tx_reqs) && (cm->tx_cnt < RIOCM_TX_RING_SIZE)) { 668 struct tx_req *req, *_req; 669 int rc; 670 671 list_for_each_entry_safe(req, _req, &cm->tx_reqs, node) { 672 list_del(&req->node); 673 cm->tx_buf[cm->tx_slot] = req->buffer; 674 rc = rio_add_outb_message(cm->mport, req->rdev, cmbox, 675 req->buffer, req->len); 676 kfree(req->buffer); 677 kfree(req); 678 679 ++cm->tx_cnt; 680 ++cm->tx_slot; 681 cm->tx_slot &= (RIOCM_TX_RING_SIZE - 1); 682 if (cm->tx_cnt == RIOCM_TX_RING_SIZE) 683 break; 684 } 685 } 686 687 spin_unlock(&cm->tx_lock); 688 } 689 690 static void riocm_outb_msg_event(struct rio_mport *mport, void *dev_id, 691 int mbox, int slot) 692 { 693 struct cm_dev *cm = dev_id; 694 695 if (cm && rio_mport_is_running(cm->mport)) 696 rio_txcq_handler(cm, slot); 697 } 698 699 static int riocm_queue_req(struct cm_dev *cm, struct rio_dev *rdev, 700 void *buffer, size_t len) 701 { 702 unsigned long flags; 703 struct tx_req *treq; 704 705 treq = kzalloc(sizeof(*treq), GFP_KERNEL); 706 if (treq == NULL) 707 return -ENOMEM; 708 709 treq->rdev = rdev; 710 treq->buffer = buffer; 711 treq->len = len; 712 713 spin_lock_irqsave(&cm->tx_lock, flags); 714 list_add_tail(&treq->node, &cm->tx_reqs); 715 spin_unlock_irqrestore(&cm->tx_lock, flags); 716 return 0; 717 } 718 719 /* 720 * riocm_post_send - helper function that places packet into msg TX queue 721 * @cm: cm_dev object 722 * @rdev: target RapidIO device object (required by outbound msg interface) 723 * @buffer: pointer to a packet buffer to send 724 * @len: length of data to transfer 725 * @req: request priority flag 726 * 727 * Returns: 0 if success, or error code otherwise. 728 */ 729 static int riocm_post_send(struct cm_dev *cm, struct rio_dev *rdev, 730 void *buffer, size_t len) 731 { 732 int rc; 733 unsigned long flags; 734 735 spin_lock_irqsave(&cm->tx_lock, flags); 736 737 if (cm->mport == NULL) { 738 rc = -ENODEV; 739 goto err_out; 740 } 741 742 if (cm->tx_cnt == RIOCM_TX_RING_SIZE) { 743 riocm_debug(TX, "Tx Queue is full"); 744 rc = -EBUSY; 745 goto err_out; 746 } 747 748 cm->tx_buf[cm->tx_slot] = buffer; 749 rc = rio_add_outb_message(cm->mport, rdev, cmbox, buffer, len); 750 751 riocm_debug(TX, "Add buf@%p destid=%x tx_slot=%d tx_cnt=%d", 752 buffer, rdev->destid, cm->tx_slot, cm->tx_cnt); 753 754 ++cm->tx_cnt; 755 ++cm->tx_slot; 756 cm->tx_slot &= (RIOCM_TX_RING_SIZE - 1); 757 758 err_out: 759 spin_unlock_irqrestore(&cm->tx_lock, flags); 760 return rc; 761 } 762 763 /* 764 * riocm_ch_send - sends a data packet to a remote device 765 * @ch_id: local channel ID 766 * @buf: pointer to a data buffer to send (including CM header) 767 * @len: length of data to transfer (including CM header) 768 * 769 * ATTN: ASSUMES THAT THE HEADER SPACE IS RESERVED PART OF THE DATA PACKET 770 * 771 * Returns: 0 if success, or 772 * -EINVAL if one or more input parameters is/are not valid, 773 * -ENODEV if cannot find a channel with specified ID, 774 * -EAGAIN if a channel is not in CONNECTED state, 775 * + error codes returned by HW send routine. 776 */ 777 static int riocm_ch_send(u16 ch_id, void *buf, int len) 778 { 779 struct rio_channel *ch; 780 struct rio_ch_chan_hdr *hdr; 781 int ret; 782 783 if (buf == NULL || ch_id == 0 || len == 0 || len > RIO_MAX_MSG_SIZE) 784 return -EINVAL; 785 786 ch = riocm_get_channel(ch_id); 787 if (!ch) { 788 riocm_error("%s(%d) ch_%d not found", current->comm, 789 task_pid_nr(current), ch_id); 790 return -ENODEV; 791 } 792 793 if (!riocm_cmp(ch, RIO_CM_CONNECTED)) { 794 ret = -EAGAIN; 795 goto err_out; 796 } 797 798 /* 799 * Fill buffer header section with corresponding channel data 800 */ 801 hdr = buf; 802 803 hdr->bhdr.src_id = htonl(ch->loc_destid); 804 hdr->bhdr.dst_id = htonl(ch->rem_destid); 805 hdr->bhdr.src_mbox = cmbox; 806 hdr->bhdr.dst_mbox = cmbox; 807 hdr->bhdr.type = RIO_CM_CHAN; 808 hdr->ch_op = CM_DATA_MSG; 809 hdr->dst_ch = htons(ch->rem_channel); 810 hdr->src_ch = htons(ch->id); 811 hdr->msg_len = htons((u16)len); 812 813 /* ATTN: the function call below relies on the fact that underlying 814 * HW-specific add_outb_message() routine copies TX data into its own 815 * internal transfer buffer (true for all RIONET compatible mport 816 * drivers). Must be reviewed if mport driver uses the buffer directly. 817 */ 818 819 ret = riocm_post_send(ch->cmdev, ch->rdev, buf, len); 820 if (ret) 821 riocm_debug(TX, "ch %d send_err=%d", ch->id, ret); 822 err_out: 823 riocm_put_channel(ch); 824 return ret; 825 } 826 827 static int riocm_ch_free_rxbuf(struct rio_channel *ch, void *buf) 828 { 829 int i, ret = -EINVAL; 830 831 spin_lock_bh(&ch->lock); 832 833 for (i = 0; i < RIOCM_RX_RING_SIZE; i++) { 834 if (ch->rx_ring.inuse[i] == buf) { 835 ch->rx_ring.inuse[i] = NULL; 836 ch->rx_ring.inuse_cnt--; 837 ret = 0; 838 break; 839 } 840 } 841 842 spin_unlock_bh(&ch->lock); 843 844 if (!ret) 845 kfree(buf); 846 847 return ret; 848 } 849 850 /* 851 * riocm_ch_receive - fetch a data packet received for the specified channel 852 * @ch: local channel ID 853 * @buf: pointer to a packet buffer 854 * @timeout: timeout to wait for incoming packet (in jiffies) 855 * 856 * Returns: 0 and valid buffer pointer if success, or NULL pointer and one of: 857 * -EAGAIN if a channel is not in CONNECTED state, 858 * -ENOMEM if in-use tracking queue is full, 859 * -ETIME if wait timeout expired, 860 * -EINTR if wait was interrupted. 861 */ 862 static int riocm_ch_receive(struct rio_channel *ch, void **buf, long timeout) 863 { 864 void *rxmsg = NULL; 865 int i, ret = 0; 866 long wret; 867 868 if (!riocm_cmp(ch, RIO_CM_CONNECTED)) { 869 ret = -EAGAIN; 870 goto out; 871 } 872 873 if (ch->rx_ring.inuse_cnt == RIOCM_RX_RING_SIZE) { 874 /* If we do not have entries to track buffers given to upper 875 * layer, reject request. 876 */ 877 ret = -ENOMEM; 878 goto out; 879 } 880 881 wret = wait_for_completion_interruptible_timeout(&ch->comp, timeout); 882 883 riocm_debug(WAIT, "wait on %d returned %ld", ch->id, wret); 884 885 if (!wret) 886 ret = -ETIME; 887 else if (wret == -ERESTARTSYS) 888 ret = -EINTR; 889 else 890 ret = riocm_cmp(ch, RIO_CM_CONNECTED) ? 0 : -ECONNRESET; 891 892 if (ret) 893 goto out; 894 895 spin_lock_bh(&ch->lock); 896 897 rxmsg = ch->rx_ring.buf[ch->rx_ring.tail]; 898 ch->rx_ring.buf[ch->rx_ring.tail] = NULL; 899 ch->rx_ring.count--; 900 ch->rx_ring.tail++; 901 ch->rx_ring.tail %= RIOCM_RX_RING_SIZE; 902 ret = -ENOMEM; 903 904 for (i = 0; i < RIOCM_RX_RING_SIZE; i++) { 905 if (ch->rx_ring.inuse[i] == NULL) { 906 ch->rx_ring.inuse[i] = rxmsg; 907 ch->rx_ring.inuse_cnt++; 908 ret = 0; 909 break; 910 } 911 } 912 913 if (ret) { 914 /* We have no entry to store pending message: drop it */ 915 kfree(rxmsg); 916 rxmsg = NULL; 917 } 918 919 spin_unlock_bh(&ch->lock); 920 out: 921 *buf = rxmsg; 922 return ret; 923 } 924 925 /* 926 * riocm_ch_connect - sends a connect request to a remote device 927 * @loc_ch: local channel ID 928 * @cm: CM device to send connect request 929 * @peer: target RapidIO device 930 * @rem_ch: remote channel ID 931 * 932 * Returns: 0 if success, or 933 * -EINVAL if the channel is not in IDLE state, 934 * -EAGAIN if no connection request available immediately, 935 * -ETIME if ACK response timeout expired, 936 * -EINTR if wait for response was interrupted. 937 */ 938 static int riocm_ch_connect(u16 loc_ch, struct cm_dev *cm, 939 struct cm_peer *peer, u16 rem_ch) 940 { 941 struct rio_channel *ch = NULL; 942 struct rio_ch_chan_hdr *hdr; 943 int ret; 944 long wret; 945 946 ch = riocm_get_channel(loc_ch); 947 if (!ch) 948 return -ENODEV; 949 950 if (!riocm_cmp_exch(ch, RIO_CM_IDLE, RIO_CM_CONNECT)) { 951 ret = -EINVAL; 952 goto conn_done; 953 } 954 955 ch->cmdev = cm; 956 ch->rdev = peer->rdev; 957 ch->context = NULL; 958 ch->loc_destid = cm->mport->host_deviceid; 959 ch->rem_channel = rem_ch; 960 961 /* 962 * Send connect request to the remote RapidIO device 963 */ 964 965 hdr = kzalloc(sizeof(*hdr), GFP_KERNEL); 966 if (hdr == NULL) { 967 ret = -ENOMEM; 968 goto conn_done; 969 } 970 971 hdr->bhdr.src_id = htonl(ch->loc_destid); 972 hdr->bhdr.dst_id = htonl(peer->rdev->destid); 973 hdr->bhdr.src_mbox = cmbox; 974 hdr->bhdr.dst_mbox = cmbox; 975 hdr->bhdr.type = RIO_CM_CHAN; 976 hdr->ch_op = CM_CONN_REQ; 977 hdr->dst_ch = htons(rem_ch); 978 hdr->src_ch = htons(loc_ch); 979 980 /* ATTN: the function call below relies on the fact that underlying 981 * HW-specific add_outb_message() routine copies TX data into its 982 * internal transfer buffer. Must be reviewed if mport driver uses 983 * this buffer directly. 984 */ 985 ret = riocm_post_send(cm, peer->rdev, hdr, sizeof(*hdr)); 986 987 if (ret != -EBUSY) { 988 kfree(hdr); 989 } else { 990 ret = riocm_queue_req(cm, peer->rdev, hdr, sizeof(*hdr)); 991 if (ret) 992 kfree(hdr); 993 } 994 995 if (ret) { 996 riocm_cmp_exch(ch, RIO_CM_CONNECT, RIO_CM_IDLE); 997 goto conn_done; 998 } 999 1000 /* Wait for connect response from the remote device */ 1001 wret = wait_for_completion_interruptible_timeout(&ch->comp, 1002 RIOCM_CONNECT_TO * HZ); 1003 riocm_debug(WAIT, "wait on %d returns %ld", ch->id, wret); 1004 1005 if (!wret) 1006 ret = -ETIME; 1007 else if (wret == -ERESTARTSYS) 1008 ret = -EINTR; 1009 else 1010 ret = riocm_cmp(ch, RIO_CM_CONNECTED) ? 0 : -1; 1011 1012 conn_done: 1013 riocm_put_channel(ch); 1014 return ret; 1015 } 1016 1017 static int riocm_send_ack(struct rio_channel *ch) 1018 { 1019 struct rio_ch_chan_hdr *hdr; 1020 int ret; 1021 1022 hdr = kzalloc(sizeof(*hdr), GFP_KERNEL); 1023 if (hdr == NULL) 1024 return -ENOMEM; 1025 1026 hdr->bhdr.src_id = htonl(ch->loc_destid); 1027 hdr->bhdr.dst_id = htonl(ch->rem_destid); 1028 hdr->dst_ch = htons(ch->rem_channel); 1029 hdr->src_ch = htons(ch->id); 1030 hdr->bhdr.src_mbox = cmbox; 1031 hdr->bhdr.dst_mbox = cmbox; 1032 hdr->bhdr.type = RIO_CM_CHAN; 1033 hdr->ch_op = CM_CONN_ACK; 1034 1035 /* ATTN: the function call below relies on the fact that underlying 1036 * add_outb_message() routine copies TX data into its internal transfer 1037 * buffer. Review if switching to direct buffer version. 1038 */ 1039 ret = riocm_post_send(ch->cmdev, ch->rdev, hdr, sizeof(*hdr)); 1040 1041 if (ret == -EBUSY && !riocm_queue_req(ch->cmdev, 1042 ch->rdev, hdr, sizeof(*hdr))) 1043 return 0; 1044 kfree(hdr); 1045 1046 if (ret) 1047 riocm_error("send ACK to ch_%d on %s failed (ret=%d)", 1048 ch->id, rio_name(ch->rdev), ret); 1049 return ret; 1050 } 1051 1052 /* 1053 * riocm_ch_accept - accept incoming connection request 1054 * @ch_id: channel ID 1055 * @new_ch_id: local mport device 1056 * @timeout: wait timeout (if 0 non-blocking call, do not wait if connection 1057 * request is not available). 1058 * 1059 * Returns: pointer to new channel struct if success, or error-valued pointer: 1060 * -ENODEV - cannot find specified channel or mport, 1061 * -EINVAL - the channel is not in IDLE state, 1062 * -EAGAIN - no connection request available immediately (timeout=0), 1063 * -ENOMEM - unable to allocate new channel, 1064 * -ETIME - wait timeout expired, 1065 * -EINTR - wait was interrupted. 1066 */ 1067 static struct rio_channel *riocm_ch_accept(u16 ch_id, u16 *new_ch_id, 1068 long timeout) 1069 { 1070 struct rio_channel *ch; 1071 struct rio_channel *new_ch; 1072 struct conn_req *req; 1073 struct cm_peer *peer; 1074 int found = 0; 1075 int err = 0; 1076 long wret; 1077 1078 ch = riocm_get_channel(ch_id); 1079 if (!ch) 1080 return ERR_PTR(-EINVAL); 1081 1082 if (!riocm_cmp(ch, RIO_CM_LISTEN)) { 1083 err = -EINVAL; 1084 goto err_put; 1085 } 1086 1087 /* Don't sleep if this is a non blocking call */ 1088 if (!timeout) { 1089 if (!try_wait_for_completion(&ch->comp)) { 1090 err = -EAGAIN; 1091 goto err_put; 1092 } 1093 } else { 1094 riocm_debug(WAIT, "on %d", ch->id); 1095 1096 wret = wait_for_completion_interruptible_timeout(&ch->comp, 1097 timeout); 1098 if (!wret) { 1099 err = -ETIME; 1100 goto err_put; 1101 } else if (wret == -ERESTARTSYS) { 1102 err = -EINTR; 1103 goto err_put; 1104 } 1105 } 1106 1107 spin_lock_bh(&ch->lock); 1108 1109 if (ch->state != RIO_CM_LISTEN) { 1110 err = -ECANCELED; 1111 } else if (list_empty(&ch->accept_queue)) { 1112 riocm_debug(WAIT, "on %d accept_queue is empty on completion", 1113 ch->id); 1114 err = -EIO; 1115 } 1116 1117 spin_unlock_bh(&ch->lock); 1118 1119 if (err) { 1120 riocm_debug(WAIT, "on %d returns %d", ch->id, err); 1121 goto err_put; 1122 } 1123 1124 /* Create new channel for this connection */ 1125 new_ch = riocm_ch_alloc(RIOCM_CHNUM_AUTO); 1126 1127 if (IS_ERR(new_ch)) { 1128 riocm_error("failed to get channel for new req (%ld)", 1129 PTR_ERR(new_ch)); 1130 err = -ENOMEM; 1131 goto err_put; 1132 } 1133 1134 spin_lock_bh(&ch->lock); 1135 1136 req = list_first_entry(&ch->accept_queue, struct conn_req, node); 1137 list_del(&req->node); 1138 new_ch->cmdev = ch->cmdev; 1139 new_ch->loc_destid = ch->loc_destid; 1140 new_ch->rem_destid = req->destid; 1141 new_ch->rem_channel = req->chan; 1142 1143 spin_unlock_bh(&ch->lock); 1144 riocm_put_channel(ch); 1145 ch = NULL; 1146 kfree(req); 1147 1148 down_read(&rdev_sem); 1149 /* Find requester's device object */ 1150 list_for_each_entry(peer, &new_ch->cmdev->peers, node) { 1151 if (peer->rdev->destid == new_ch->rem_destid) { 1152 riocm_debug(RX_CMD, "found matching device(%s)", 1153 rio_name(peer->rdev)); 1154 found = 1; 1155 break; 1156 } 1157 } 1158 up_read(&rdev_sem); 1159 1160 if (!found) { 1161 /* If peer device object not found, simply ignore the request */ 1162 err = -ENODEV; 1163 goto err_put_new_ch; 1164 } 1165 1166 new_ch->rdev = peer->rdev; 1167 new_ch->state = RIO_CM_CONNECTED; 1168 spin_lock_init(&new_ch->lock); 1169 1170 /* Acknowledge the connection request. */ 1171 riocm_send_ack(new_ch); 1172 1173 *new_ch_id = new_ch->id; 1174 return new_ch; 1175 1176 err_put_new_ch: 1177 spin_lock_bh(&idr_lock); 1178 idr_remove(&ch_idr, new_ch->id); 1179 spin_unlock_bh(&idr_lock); 1180 riocm_put_channel(new_ch); 1181 1182 err_put: 1183 if (ch) 1184 riocm_put_channel(ch); 1185 *new_ch_id = 0; 1186 return ERR_PTR(err); 1187 } 1188 1189 /* 1190 * riocm_ch_listen - puts a channel into LISTEN state 1191 * @ch_id: channel ID 1192 * 1193 * Returns: 0 if success, or 1194 * -EINVAL if the specified channel does not exists or 1195 * is not in CHAN_BOUND state. 1196 */ 1197 static int riocm_ch_listen(u16 ch_id) 1198 { 1199 struct rio_channel *ch = NULL; 1200 int ret = 0; 1201 1202 riocm_debug(CHOP, "(ch_%d)", ch_id); 1203 1204 ch = riocm_get_channel(ch_id); 1205 if (!ch) 1206 return -EINVAL; 1207 if (!riocm_cmp_exch(ch, RIO_CM_CHAN_BOUND, RIO_CM_LISTEN)) 1208 ret = -EINVAL; 1209 riocm_put_channel(ch); 1210 return ret; 1211 } 1212 1213 /* 1214 * riocm_ch_bind - associate a channel object and an mport device 1215 * @ch_id: channel ID 1216 * @mport_id: local mport device ID 1217 * @context: pointer to the additional caller's context 1218 * 1219 * Returns: 0 if success, or 1220 * -ENODEV if cannot find specified mport, 1221 * -EINVAL if the specified channel does not exist or 1222 * is not in IDLE state. 1223 */ 1224 static int riocm_ch_bind(u16 ch_id, u8 mport_id, void *context) 1225 { 1226 struct rio_channel *ch = NULL; 1227 struct cm_dev *cm; 1228 int rc = -ENODEV; 1229 1230 riocm_debug(CHOP, "ch_%d to mport_%d", ch_id, mport_id); 1231 1232 /* Find matching cm_dev object */ 1233 down_read(&rdev_sem); 1234 list_for_each_entry(cm, &cm_dev_list, list) { 1235 if ((cm->mport->id == mport_id) && 1236 rio_mport_is_running(cm->mport)) { 1237 rc = 0; 1238 break; 1239 } 1240 } 1241 1242 if (rc) 1243 goto exit; 1244 1245 ch = riocm_get_channel(ch_id); 1246 if (!ch) { 1247 rc = -EINVAL; 1248 goto exit; 1249 } 1250 1251 spin_lock_bh(&ch->lock); 1252 if (ch->state != RIO_CM_IDLE) { 1253 spin_unlock_bh(&ch->lock); 1254 rc = -EINVAL; 1255 goto err_put; 1256 } 1257 1258 ch->cmdev = cm; 1259 ch->loc_destid = cm->mport->host_deviceid; 1260 ch->context = context; 1261 ch->state = RIO_CM_CHAN_BOUND; 1262 spin_unlock_bh(&ch->lock); 1263 err_put: 1264 riocm_put_channel(ch); 1265 exit: 1266 up_read(&rdev_sem); 1267 return rc; 1268 } 1269 1270 /* 1271 * riocm_ch_alloc - channel object allocation helper routine 1272 * @ch_num: channel ID (1 ... RIOCM_MAX_CHNUM, 0 = automatic) 1273 * 1274 * Return value: pointer to newly created channel object, 1275 * or error-valued pointer 1276 */ 1277 static struct rio_channel *riocm_ch_alloc(u16 ch_num) 1278 { 1279 int id; 1280 int start, end; 1281 struct rio_channel *ch; 1282 1283 ch = kzalloc(sizeof(*ch), GFP_KERNEL); 1284 if (!ch) 1285 return ERR_PTR(-ENOMEM); 1286 1287 if (ch_num) { 1288 /* If requested, try to obtain the specified channel ID */ 1289 start = ch_num; 1290 end = ch_num + 1; 1291 } else { 1292 /* Obtain channel ID from the dynamic allocation range */ 1293 start = chstart; 1294 end = RIOCM_MAX_CHNUM + 1; 1295 } 1296 1297 idr_preload(GFP_KERNEL); 1298 spin_lock_bh(&idr_lock); 1299 id = idr_alloc_cyclic(&ch_idr, ch, start, end, GFP_NOWAIT); 1300 spin_unlock_bh(&idr_lock); 1301 idr_preload_end(); 1302 1303 if (id < 0) { 1304 kfree(ch); 1305 return ERR_PTR(id == -ENOSPC ? -EBUSY : id); 1306 } 1307 1308 ch->id = (u16)id; 1309 ch->state = RIO_CM_IDLE; 1310 spin_lock_init(&ch->lock); 1311 INIT_LIST_HEAD(&ch->accept_queue); 1312 INIT_LIST_HEAD(&ch->ch_node); 1313 init_completion(&ch->comp); 1314 init_completion(&ch->comp_close); 1315 kref_init(&ch->ref); 1316 ch->rx_ring.head = 0; 1317 ch->rx_ring.tail = 0; 1318 ch->rx_ring.count = 0; 1319 ch->rx_ring.inuse_cnt = 0; 1320 1321 return ch; 1322 } 1323 1324 /* 1325 * riocm_ch_create - creates a new channel object and allocates ID for it 1326 * @ch_num: channel ID (1 ... RIOCM_MAX_CHNUM, 0 = automatic) 1327 * 1328 * Allocates and initializes a new channel object. If the parameter ch_num > 0 1329 * and is within the valid range, riocm_ch_create tries to allocate the 1330 * specified ID for the new channel. If ch_num = 0, channel ID will be assigned 1331 * automatically from the range (chstart ... RIOCM_MAX_CHNUM). 1332 * Module parameter 'chstart' defines start of an ID range available for dynamic 1333 * allocation. Range below 'chstart' is reserved for pre-defined ID numbers. 1334 * Available channel numbers are limited by 16-bit size of channel numbers used 1335 * in the packet header. 1336 * 1337 * Return value: PTR to rio_channel structure if successful (with channel number 1338 * updated via pointer) or error-valued pointer if error. 1339 */ 1340 static struct rio_channel *riocm_ch_create(u16 *ch_num) 1341 { 1342 struct rio_channel *ch = NULL; 1343 1344 ch = riocm_ch_alloc(*ch_num); 1345 1346 if (IS_ERR(ch)) 1347 riocm_debug(CHOP, "Failed to allocate channel %d (err=%ld)", 1348 *ch_num, PTR_ERR(ch)); 1349 else 1350 *ch_num = ch->id; 1351 1352 return ch; 1353 } 1354 1355 /* 1356 * riocm_ch_free - channel object release routine 1357 * @ref: pointer to a channel's kref structure 1358 */ 1359 static void riocm_ch_free(struct kref *ref) 1360 { 1361 struct rio_channel *ch = container_of(ref, struct rio_channel, ref); 1362 int i; 1363 1364 riocm_debug(CHOP, "(ch_%d)", ch->id); 1365 1366 if (ch->rx_ring.inuse_cnt) { 1367 for (i = 0; 1368 i < RIOCM_RX_RING_SIZE && ch->rx_ring.inuse_cnt; i++) { 1369 if (ch->rx_ring.inuse[i] != NULL) { 1370 kfree(ch->rx_ring.inuse[i]); 1371 ch->rx_ring.inuse_cnt--; 1372 } 1373 } 1374 } 1375 1376 if (ch->rx_ring.count) 1377 for (i = 0; i < RIOCM_RX_RING_SIZE && ch->rx_ring.count; i++) { 1378 if (ch->rx_ring.buf[i] != NULL) { 1379 kfree(ch->rx_ring.buf[i]); 1380 ch->rx_ring.count--; 1381 } 1382 } 1383 1384 complete(&ch->comp_close); 1385 } 1386 1387 static int riocm_send_close(struct rio_channel *ch) 1388 { 1389 struct rio_ch_chan_hdr *hdr; 1390 int ret; 1391 1392 /* 1393 * Send CH_CLOSE notification to the remote RapidIO device 1394 */ 1395 1396 hdr = kzalloc(sizeof(*hdr), GFP_KERNEL); 1397 if (hdr == NULL) 1398 return -ENOMEM; 1399 1400 hdr->bhdr.src_id = htonl(ch->loc_destid); 1401 hdr->bhdr.dst_id = htonl(ch->rem_destid); 1402 hdr->bhdr.src_mbox = cmbox; 1403 hdr->bhdr.dst_mbox = cmbox; 1404 hdr->bhdr.type = RIO_CM_CHAN; 1405 hdr->ch_op = CM_CONN_CLOSE; 1406 hdr->dst_ch = htons(ch->rem_channel); 1407 hdr->src_ch = htons(ch->id); 1408 1409 /* ATTN: the function call below relies on the fact that underlying 1410 * add_outb_message() routine copies TX data into its internal transfer 1411 * buffer. Needs to be reviewed if switched to direct buffer mode. 1412 */ 1413 ret = riocm_post_send(ch->cmdev, ch->rdev, hdr, sizeof(*hdr)); 1414 1415 if (ret == -EBUSY && !riocm_queue_req(ch->cmdev, ch->rdev, 1416 hdr, sizeof(*hdr))) 1417 return 0; 1418 kfree(hdr); 1419 1420 if (ret) 1421 riocm_error("ch(%d) send CLOSE failed (ret=%d)", ch->id, ret); 1422 1423 return ret; 1424 } 1425 1426 /* 1427 * riocm_ch_close - closes a channel object with specified ID (by local request) 1428 * @ch: channel to be closed 1429 */ 1430 static int riocm_ch_close(struct rio_channel *ch) 1431 { 1432 unsigned long tmo = msecs_to_jiffies(3000); 1433 enum rio_cm_state state; 1434 long wret; 1435 int ret = 0; 1436 1437 riocm_debug(CHOP, "ch_%d by %s(%d)", 1438 ch->id, current->comm, task_pid_nr(current)); 1439 1440 state = riocm_exch(ch, RIO_CM_DESTROYING); 1441 if (state == RIO_CM_CONNECTED) 1442 riocm_send_close(ch); 1443 1444 complete_all(&ch->comp); 1445 1446 riocm_put_channel(ch); 1447 wret = wait_for_completion_interruptible_timeout(&ch->comp_close, tmo); 1448 1449 riocm_debug(WAIT, "wait on %d returns %ld", ch->id, wret); 1450 1451 if (wret == 0) { 1452 /* Timeout on wait occurred */ 1453 riocm_debug(CHOP, "%s(%d) timed out waiting for ch %d", 1454 current->comm, task_pid_nr(current), ch->id); 1455 ret = -ETIMEDOUT; 1456 } else if (wret == -ERESTARTSYS) { 1457 /* Wait_for_completion was interrupted by a signal */ 1458 riocm_debug(CHOP, "%s(%d) wait for ch %d was interrupted", 1459 current->comm, task_pid_nr(current), ch->id); 1460 ret = -EINTR; 1461 } 1462 1463 if (!ret) { 1464 riocm_debug(CHOP, "ch_%d resources released", ch->id); 1465 kfree(ch); 1466 } else { 1467 riocm_debug(CHOP, "failed to release ch_%d resources", ch->id); 1468 } 1469 1470 return ret; 1471 } 1472 1473 /* 1474 * riocm_cdev_open() - Open character device 1475 */ 1476 static int riocm_cdev_open(struct inode *inode, struct file *filp) 1477 { 1478 riocm_debug(INIT, "by %s(%d) filp=%p ", 1479 current->comm, task_pid_nr(current), filp); 1480 1481 if (list_empty(&cm_dev_list)) 1482 return -ENODEV; 1483 1484 return 0; 1485 } 1486 1487 /* 1488 * riocm_cdev_release() - Release character device 1489 */ 1490 static int riocm_cdev_release(struct inode *inode, struct file *filp) 1491 { 1492 struct rio_channel *ch, *_c; 1493 unsigned int i; 1494 LIST_HEAD(list); 1495 1496 riocm_debug(EXIT, "by %s(%d) filp=%p", 1497 current->comm, task_pid_nr(current), filp); 1498 1499 /* Check if there are channels associated with this file descriptor */ 1500 spin_lock_bh(&idr_lock); 1501 idr_for_each_entry(&ch_idr, ch, i) { 1502 if (ch && ch->filp == filp) { 1503 riocm_debug(EXIT, "ch_%d not released by %s(%d)", 1504 ch->id, current->comm, 1505 task_pid_nr(current)); 1506 idr_remove(&ch_idr, ch->id); 1507 list_add(&ch->ch_node, &list); 1508 } 1509 } 1510 spin_unlock_bh(&idr_lock); 1511 1512 if (!list_empty(&list)) { 1513 list_for_each_entry_safe(ch, _c, &list, ch_node) { 1514 list_del(&ch->ch_node); 1515 riocm_ch_close(ch); 1516 } 1517 } 1518 1519 return 0; 1520 } 1521 1522 /* 1523 * cm_ep_get_list_size() - Reports number of endpoints in the network 1524 */ 1525 static int cm_ep_get_list_size(void __user *arg) 1526 { 1527 u32 __user *p = arg; 1528 u32 mport_id; 1529 u32 count = 0; 1530 struct cm_dev *cm; 1531 1532 if (get_user(mport_id, p)) 1533 return -EFAULT; 1534 if (mport_id >= RIO_MAX_MPORTS) 1535 return -EINVAL; 1536 1537 /* Find a matching cm_dev object */ 1538 down_read(&rdev_sem); 1539 list_for_each_entry(cm, &cm_dev_list, list) { 1540 if (cm->mport->id == mport_id) { 1541 count = cm->npeers; 1542 up_read(&rdev_sem); 1543 if (copy_to_user(arg, &count, sizeof(u32))) 1544 return -EFAULT; 1545 return 0; 1546 } 1547 } 1548 up_read(&rdev_sem); 1549 1550 return -ENODEV; 1551 } 1552 1553 /* 1554 * cm_ep_get_list() - Returns list of attached endpoints 1555 */ 1556 static int cm_ep_get_list(void __user *arg) 1557 { 1558 struct cm_dev *cm; 1559 struct cm_peer *peer; 1560 u32 info[2]; 1561 void *buf; 1562 u32 nent; 1563 u32 *entry_ptr; 1564 u32 i = 0; 1565 int ret = 0; 1566 1567 if (copy_from_user(&info, arg, sizeof(info))) 1568 return -EFAULT; 1569 1570 if (info[1] >= RIO_MAX_MPORTS || info[0] > RIOCM_MAX_EP_COUNT) 1571 return -EINVAL; 1572 1573 /* Find a matching cm_dev object */ 1574 down_read(&rdev_sem); 1575 list_for_each_entry(cm, &cm_dev_list, list) 1576 if (cm->mport->id == (u8)info[1]) 1577 goto found; 1578 1579 up_read(&rdev_sem); 1580 return -ENODEV; 1581 1582 found: 1583 nent = min(info[0], cm->npeers); 1584 buf = kcalloc(nent + 2, sizeof(u32), GFP_KERNEL); 1585 if (!buf) { 1586 up_read(&rdev_sem); 1587 return -ENOMEM; 1588 } 1589 1590 entry_ptr = (u32 *)((uintptr_t)buf + 2*sizeof(u32)); 1591 1592 list_for_each_entry(peer, &cm->peers, node) { 1593 *entry_ptr = (u32)peer->rdev->destid; 1594 entry_ptr++; 1595 if (++i == nent) 1596 break; 1597 } 1598 up_read(&rdev_sem); 1599 1600 ((u32 *)buf)[0] = i; /* report an updated number of entries */ 1601 ((u32 *)buf)[1] = info[1]; /* put back an mport ID */ 1602 if (copy_to_user(arg, buf, sizeof(u32) * (info[0] + 2))) 1603 ret = -EFAULT; 1604 1605 kfree(buf); 1606 return ret; 1607 } 1608 1609 /* 1610 * cm_mport_get_list() - Returns list of available local mport devices 1611 */ 1612 static int cm_mport_get_list(void __user *arg) 1613 { 1614 int ret = 0; 1615 u32 entries; 1616 void *buf; 1617 struct cm_dev *cm; 1618 u32 *entry_ptr; 1619 int count = 0; 1620 1621 if (copy_from_user(&entries, arg, sizeof(entries))) 1622 return -EFAULT; 1623 if (entries == 0 || entries > RIO_MAX_MPORTS) 1624 return -EINVAL; 1625 buf = kcalloc(entries + 1, sizeof(u32), GFP_KERNEL); 1626 if (!buf) 1627 return -ENOMEM; 1628 1629 /* Scan all registered cm_dev objects */ 1630 entry_ptr = (u32 *)((uintptr_t)buf + sizeof(u32)); 1631 down_read(&rdev_sem); 1632 list_for_each_entry(cm, &cm_dev_list, list) { 1633 if (count++ < entries) { 1634 *entry_ptr = (cm->mport->id << 16) | 1635 cm->mport->host_deviceid; 1636 entry_ptr++; 1637 } 1638 } 1639 up_read(&rdev_sem); 1640 1641 *((u32 *)buf) = count; /* report a real number of entries */ 1642 if (copy_to_user(arg, buf, sizeof(u32) * (count + 1))) 1643 ret = -EFAULT; 1644 1645 kfree(buf); 1646 return ret; 1647 } 1648 1649 /* 1650 * cm_chan_create() - Create a message exchange channel 1651 */ 1652 static int cm_chan_create(struct file *filp, void __user *arg) 1653 { 1654 u16 __user *p = arg; 1655 u16 ch_num; 1656 struct rio_channel *ch; 1657 1658 if (get_user(ch_num, p)) 1659 return -EFAULT; 1660 1661 riocm_debug(CHOP, "ch_%d requested by %s(%d)", 1662 ch_num, current->comm, task_pid_nr(current)); 1663 ch = riocm_ch_create(&ch_num); 1664 if (IS_ERR(ch)) 1665 return PTR_ERR(ch); 1666 1667 ch->filp = filp; 1668 riocm_debug(CHOP, "ch_%d created by %s(%d)", 1669 ch_num, current->comm, task_pid_nr(current)); 1670 return put_user(ch_num, p); 1671 } 1672 1673 /* 1674 * cm_chan_close() - Close channel 1675 * @filp: Pointer to file object 1676 * @arg: Channel to close 1677 */ 1678 static int cm_chan_close(struct file *filp, void __user *arg) 1679 { 1680 u16 __user *p = arg; 1681 u16 ch_num; 1682 struct rio_channel *ch; 1683 1684 if (get_user(ch_num, p)) 1685 return -EFAULT; 1686 1687 riocm_debug(CHOP, "ch_%d by %s(%d)", 1688 ch_num, current->comm, task_pid_nr(current)); 1689 1690 spin_lock_bh(&idr_lock); 1691 ch = idr_find(&ch_idr, ch_num); 1692 if (!ch) { 1693 spin_unlock_bh(&idr_lock); 1694 return 0; 1695 } 1696 if (ch->filp != filp) { 1697 spin_unlock_bh(&idr_lock); 1698 return -EINVAL; 1699 } 1700 idr_remove(&ch_idr, ch->id); 1701 spin_unlock_bh(&idr_lock); 1702 1703 return riocm_ch_close(ch); 1704 } 1705 1706 /* 1707 * cm_chan_bind() - Bind channel 1708 * @arg: Channel number 1709 */ 1710 static int cm_chan_bind(void __user *arg) 1711 { 1712 struct rio_cm_channel chan; 1713 1714 if (copy_from_user(&chan, arg, sizeof(chan))) 1715 return -EFAULT; 1716 if (chan.mport_id >= RIO_MAX_MPORTS) 1717 return -EINVAL; 1718 1719 return riocm_ch_bind(chan.id, chan.mport_id, NULL); 1720 } 1721 1722 /* 1723 * cm_chan_listen() - Listen on channel 1724 * @arg: Channel number 1725 */ 1726 static int cm_chan_listen(void __user *arg) 1727 { 1728 u16 __user *p = arg; 1729 u16 ch_num; 1730 1731 if (get_user(ch_num, p)) 1732 return -EFAULT; 1733 1734 return riocm_ch_listen(ch_num); 1735 } 1736 1737 /* 1738 * cm_chan_accept() - Accept incoming connection 1739 * @filp: Pointer to file object 1740 * @arg: Channel number 1741 */ 1742 static int cm_chan_accept(struct file *filp, void __user *arg) 1743 { 1744 struct rio_cm_accept param; 1745 long accept_to; 1746 struct rio_channel *ch; 1747 1748 if (copy_from_user(¶m, arg, sizeof(param))) 1749 return -EFAULT; 1750 1751 riocm_debug(CHOP, "on ch_%d by %s(%d)", 1752 param.ch_num, current->comm, task_pid_nr(current)); 1753 1754 accept_to = param.wait_to ? 1755 msecs_to_jiffies(param.wait_to) : 0; 1756 1757 ch = riocm_ch_accept(param.ch_num, ¶m.ch_num, accept_to); 1758 if (IS_ERR(ch)) 1759 return PTR_ERR(ch); 1760 ch->filp = filp; 1761 1762 riocm_debug(CHOP, "new ch_%d for %s(%d)", 1763 ch->id, current->comm, task_pid_nr(current)); 1764 1765 if (copy_to_user(arg, ¶m, sizeof(param))) 1766 return -EFAULT; 1767 return 0; 1768 } 1769 1770 /* 1771 * cm_chan_connect() - Connect on channel 1772 * @arg: Channel information 1773 */ 1774 static int cm_chan_connect(void __user *arg) 1775 { 1776 struct rio_cm_channel chan; 1777 struct cm_dev *cm; 1778 struct cm_peer *peer; 1779 int ret = -ENODEV; 1780 1781 if (copy_from_user(&chan, arg, sizeof(chan))) 1782 return -EFAULT; 1783 if (chan.mport_id >= RIO_MAX_MPORTS) 1784 return -EINVAL; 1785 1786 down_read(&rdev_sem); 1787 1788 /* Find matching cm_dev object */ 1789 list_for_each_entry(cm, &cm_dev_list, list) { 1790 if (cm->mport->id == chan.mport_id) { 1791 ret = 0; 1792 break; 1793 } 1794 } 1795 1796 if (ret) 1797 goto err_out; 1798 1799 if (chan.remote_destid >= RIO_ANY_DESTID(cm->mport->sys_size)) { 1800 ret = -EINVAL; 1801 goto err_out; 1802 } 1803 1804 /* Find corresponding RapidIO endpoint device object */ 1805 ret = -ENODEV; 1806 1807 list_for_each_entry(peer, &cm->peers, node) { 1808 if (peer->rdev->destid == chan.remote_destid) { 1809 ret = 0; 1810 break; 1811 } 1812 } 1813 1814 if (ret) 1815 goto err_out; 1816 1817 up_read(&rdev_sem); 1818 1819 return riocm_ch_connect(chan.id, cm, peer, chan.remote_channel); 1820 err_out: 1821 up_read(&rdev_sem); 1822 return ret; 1823 } 1824 1825 /* 1826 * cm_chan_msg_send() - Send a message through channel 1827 * @arg: Outbound message information 1828 */ 1829 static int cm_chan_msg_send(void __user *arg) 1830 { 1831 struct rio_cm_msg msg; 1832 void *buf; 1833 int ret; 1834 1835 if (copy_from_user(&msg, arg, sizeof(msg))) 1836 return -EFAULT; 1837 if (msg.size > RIO_MAX_MSG_SIZE) 1838 return -EINVAL; 1839 1840 buf = memdup_user((void __user *)(uintptr_t)msg.msg, msg.size); 1841 if (IS_ERR(buf)) 1842 return PTR_ERR(buf); 1843 1844 ret = riocm_ch_send(msg.ch_num, buf, msg.size); 1845 1846 kfree(buf); 1847 return ret; 1848 } 1849 1850 /* 1851 * cm_chan_msg_rcv() - Receive a message through channel 1852 * @arg: Inbound message information 1853 */ 1854 static int cm_chan_msg_rcv(void __user *arg) 1855 { 1856 struct rio_cm_msg msg; 1857 struct rio_channel *ch; 1858 void *buf; 1859 long rxto; 1860 int ret = 0, msg_size; 1861 1862 if (copy_from_user(&msg, arg, sizeof(msg))) 1863 return -EFAULT; 1864 1865 if (msg.ch_num == 0 || msg.size == 0) 1866 return -EINVAL; 1867 1868 ch = riocm_get_channel(msg.ch_num); 1869 if (!ch) 1870 return -ENODEV; 1871 1872 rxto = msg.rxto ? msecs_to_jiffies(msg.rxto) : MAX_SCHEDULE_TIMEOUT; 1873 1874 ret = riocm_ch_receive(ch, &buf, rxto); 1875 if (ret) 1876 goto out; 1877 1878 msg_size = min(msg.size, (u16)(RIO_MAX_MSG_SIZE)); 1879 1880 if (copy_to_user((void __user *)(uintptr_t)msg.msg, buf, msg_size)) 1881 ret = -EFAULT; 1882 1883 riocm_ch_free_rxbuf(ch, buf); 1884 out: 1885 riocm_put_channel(ch); 1886 return ret; 1887 } 1888 1889 /* 1890 * riocm_cdev_ioctl() - IOCTL requests handler 1891 */ 1892 static long 1893 riocm_cdev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) 1894 { 1895 switch (cmd) { 1896 case RIO_CM_EP_GET_LIST_SIZE: 1897 return cm_ep_get_list_size((void __user *)arg); 1898 case RIO_CM_EP_GET_LIST: 1899 return cm_ep_get_list((void __user *)arg); 1900 case RIO_CM_CHAN_CREATE: 1901 return cm_chan_create(filp, (void __user *)arg); 1902 case RIO_CM_CHAN_CLOSE: 1903 return cm_chan_close(filp, (void __user *)arg); 1904 case RIO_CM_CHAN_BIND: 1905 return cm_chan_bind((void __user *)arg); 1906 case RIO_CM_CHAN_LISTEN: 1907 return cm_chan_listen((void __user *)arg); 1908 case RIO_CM_CHAN_ACCEPT: 1909 return cm_chan_accept(filp, (void __user *)arg); 1910 case RIO_CM_CHAN_CONNECT: 1911 return cm_chan_connect((void __user *)arg); 1912 case RIO_CM_CHAN_SEND: 1913 return cm_chan_msg_send((void __user *)arg); 1914 case RIO_CM_CHAN_RECEIVE: 1915 return cm_chan_msg_rcv((void __user *)arg); 1916 case RIO_CM_MPORT_GET_LIST: 1917 return cm_mport_get_list((void __user *)arg); 1918 default: 1919 break; 1920 } 1921 1922 return -EINVAL; 1923 } 1924 1925 static const struct file_operations riocm_cdev_fops = { 1926 .owner = THIS_MODULE, 1927 .open = riocm_cdev_open, 1928 .release = riocm_cdev_release, 1929 .unlocked_ioctl = riocm_cdev_ioctl, 1930 }; 1931 1932 /* 1933 * riocm_add_dev - add new remote RapidIO device into channel management core 1934 * @dev: device object associated with RapidIO device 1935 * @sif: subsystem interface 1936 * 1937 * Adds the specified RapidIO device (if applicable) into peers list of 1938 * the corresponding channel management device (cm_dev). 1939 */ 1940 static int riocm_add_dev(struct device *dev, struct subsys_interface *sif) 1941 { 1942 struct cm_peer *peer; 1943 struct rio_dev *rdev = to_rio_dev(dev); 1944 struct cm_dev *cm; 1945 1946 /* Check if the remote device has capabilities required to support CM */ 1947 if (!dev_cm_capable(rdev)) 1948 return 0; 1949 1950 riocm_debug(RDEV, "(%s)", rio_name(rdev)); 1951 1952 peer = kmalloc(sizeof(*peer), GFP_KERNEL); 1953 if (!peer) 1954 return -ENOMEM; 1955 1956 /* Find a corresponding cm_dev object */ 1957 down_write(&rdev_sem); 1958 list_for_each_entry(cm, &cm_dev_list, list) { 1959 if (cm->mport == rdev->net->hport) 1960 goto found; 1961 } 1962 1963 up_write(&rdev_sem); 1964 kfree(peer); 1965 return -ENODEV; 1966 1967 found: 1968 peer->rdev = rdev; 1969 list_add_tail(&peer->node, &cm->peers); 1970 cm->npeers++; 1971 1972 up_write(&rdev_sem); 1973 return 0; 1974 } 1975 1976 /* 1977 * riocm_remove_dev - remove remote RapidIO device from channel management core 1978 * @dev: device object associated with RapidIO device 1979 * @sif: subsystem interface 1980 * 1981 * Removes the specified RapidIO device (if applicable) from peers list of 1982 * the corresponding channel management device (cm_dev). 1983 */ 1984 static void riocm_remove_dev(struct device *dev, struct subsys_interface *sif) 1985 { 1986 struct rio_dev *rdev = to_rio_dev(dev); 1987 struct cm_dev *cm; 1988 struct cm_peer *peer; 1989 struct rio_channel *ch, *_c; 1990 unsigned int i; 1991 bool found = false; 1992 LIST_HEAD(list); 1993 1994 /* Check if the remote device has capabilities required to support CM */ 1995 if (!dev_cm_capable(rdev)) 1996 return; 1997 1998 riocm_debug(RDEV, "(%s)", rio_name(rdev)); 1999 2000 /* Find matching cm_dev object */ 2001 down_write(&rdev_sem); 2002 list_for_each_entry(cm, &cm_dev_list, list) { 2003 if (cm->mport == rdev->net->hport) { 2004 found = true; 2005 break; 2006 } 2007 } 2008 2009 if (!found) { 2010 up_write(&rdev_sem); 2011 return; 2012 } 2013 2014 /* Remove remote device from the list of peers */ 2015 found = false; 2016 list_for_each_entry(peer, &cm->peers, node) { 2017 if (peer->rdev == rdev) { 2018 riocm_debug(RDEV, "removing peer %s", rio_name(rdev)); 2019 found = true; 2020 list_del(&peer->node); 2021 cm->npeers--; 2022 kfree(peer); 2023 break; 2024 } 2025 } 2026 2027 up_write(&rdev_sem); 2028 2029 if (!found) 2030 return; 2031 2032 /* 2033 * Release channels associated with this peer 2034 */ 2035 2036 spin_lock_bh(&idr_lock); 2037 idr_for_each_entry(&ch_idr, ch, i) { 2038 if (ch && ch->rdev == rdev) { 2039 if (atomic_read(&rdev->state) != RIO_DEVICE_SHUTDOWN) 2040 riocm_exch(ch, RIO_CM_DISCONNECT); 2041 idr_remove(&ch_idr, ch->id); 2042 list_add(&ch->ch_node, &list); 2043 } 2044 } 2045 spin_unlock_bh(&idr_lock); 2046 2047 if (!list_empty(&list)) { 2048 list_for_each_entry_safe(ch, _c, &list, ch_node) { 2049 list_del(&ch->ch_node); 2050 riocm_ch_close(ch); 2051 } 2052 } 2053 } 2054 2055 /* 2056 * riocm_cdev_add() - Create rio_cm char device 2057 * @devno: device number assigned to device (MAJ + MIN) 2058 */ 2059 static int riocm_cdev_add(dev_t devno) 2060 { 2061 int ret; 2062 2063 cdev_init(&riocm_cdev.cdev, &riocm_cdev_fops); 2064 riocm_cdev.cdev.owner = THIS_MODULE; 2065 ret = cdev_add(&riocm_cdev.cdev, devno, 1); 2066 if (ret < 0) { 2067 riocm_error("Cannot register a device with error %d", ret); 2068 return ret; 2069 } 2070 2071 riocm_cdev.dev = device_create(&dev_class, NULL, devno, NULL, DEV_NAME); 2072 if (IS_ERR(riocm_cdev.dev)) { 2073 cdev_del(&riocm_cdev.cdev); 2074 return PTR_ERR(riocm_cdev.dev); 2075 } 2076 2077 riocm_debug(MPORT, "Added %s cdev(%d:%d)", 2078 DEV_NAME, MAJOR(devno), MINOR(devno)); 2079 2080 return 0; 2081 } 2082 2083 /* 2084 * riocm_add_mport - add new local mport device into channel management core 2085 * @dev: device object associated with mport 2086 * 2087 * When a new mport device is added, CM immediately reserves inbound and 2088 * outbound RapidIO mailboxes that will be used. 2089 */ 2090 static int riocm_add_mport(struct device *dev) 2091 { 2092 int rc; 2093 int i; 2094 struct cm_dev *cm; 2095 struct rio_mport *mport = to_rio_mport(dev); 2096 2097 riocm_debug(MPORT, "add mport %s", mport->name); 2098 2099 cm = kzalloc(sizeof(*cm), GFP_KERNEL); 2100 if (!cm) 2101 return -ENOMEM; 2102 2103 cm->mport = mport; 2104 2105 rc = rio_request_outb_mbox(mport, cm, cmbox, 2106 RIOCM_TX_RING_SIZE, riocm_outb_msg_event); 2107 if (rc) { 2108 riocm_error("failed to allocate OBMBOX_%d on %s", 2109 cmbox, mport->name); 2110 kfree(cm); 2111 return -ENODEV; 2112 } 2113 2114 rc = rio_request_inb_mbox(mport, cm, cmbox, 2115 RIOCM_RX_RING_SIZE, riocm_inb_msg_event); 2116 if (rc) { 2117 riocm_error("failed to allocate IBMBOX_%d on %s", 2118 cmbox, mport->name); 2119 rio_release_outb_mbox(mport, cmbox); 2120 kfree(cm); 2121 return -ENODEV; 2122 } 2123 2124 cm->rx_wq = create_workqueue(DRV_NAME "/rxq"); 2125 if (!cm->rx_wq) { 2126 rio_release_inb_mbox(mport, cmbox); 2127 rio_release_outb_mbox(mport, cmbox); 2128 kfree(cm); 2129 return -ENOMEM; 2130 } 2131 2132 /* 2133 * Allocate and register inbound messaging buffers to be ready 2134 * to receive channel and system management requests 2135 */ 2136 for (i = 0; i < RIOCM_RX_RING_SIZE; i++) 2137 cm->rx_buf[i] = NULL; 2138 2139 cm->rx_slots = RIOCM_RX_RING_SIZE; 2140 mutex_init(&cm->rx_lock); 2141 riocm_rx_fill(cm, RIOCM_RX_RING_SIZE); 2142 INIT_WORK(&cm->rx_work, rio_ibmsg_handler); 2143 2144 cm->tx_slot = 0; 2145 cm->tx_cnt = 0; 2146 cm->tx_ack_slot = 0; 2147 spin_lock_init(&cm->tx_lock); 2148 2149 INIT_LIST_HEAD(&cm->peers); 2150 cm->npeers = 0; 2151 INIT_LIST_HEAD(&cm->tx_reqs); 2152 2153 down_write(&rdev_sem); 2154 list_add_tail(&cm->list, &cm_dev_list); 2155 up_write(&rdev_sem); 2156 2157 return 0; 2158 } 2159 2160 /* 2161 * riocm_remove_mport - remove local mport device from channel management core 2162 * @dev: device object associated with mport 2163 * 2164 * Removes a local mport device from the list of registered devices that provide 2165 * channel management services. Returns an error if the specified mport is not 2166 * registered with the CM core. 2167 */ 2168 static void riocm_remove_mport(struct device *dev) 2169 { 2170 struct rio_mport *mport = to_rio_mport(dev); 2171 struct cm_dev *cm; 2172 struct cm_peer *peer, *temp; 2173 struct rio_channel *ch, *_c; 2174 unsigned int i; 2175 bool found = false; 2176 LIST_HEAD(list); 2177 2178 riocm_debug(MPORT, "%s", mport->name); 2179 2180 /* Find a matching cm_dev object */ 2181 down_write(&rdev_sem); 2182 list_for_each_entry(cm, &cm_dev_list, list) { 2183 if (cm->mport == mport) { 2184 list_del(&cm->list); 2185 found = true; 2186 break; 2187 } 2188 } 2189 up_write(&rdev_sem); 2190 if (!found) 2191 return; 2192 2193 flush_workqueue(cm->rx_wq); 2194 destroy_workqueue(cm->rx_wq); 2195 2196 /* Release channels bound to this mport */ 2197 spin_lock_bh(&idr_lock); 2198 idr_for_each_entry(&ch_idr, ch, i) { 2199 if (ch->cmdev == cm) { 2200 riocm_debug(RDEV, "%s drop ch_%d", 2201 mport->name, ch->id); 2202 idr_remove(&ch_idr, ch->id); 2203 list_add(&ch->ch_node, &list); 2204 } 2205 } 2206 spin_unlock_bh(&idr_lock); 2207 2208 if (!list_empty(&list)) { 2209 list_for_each_entry_safe(ch, _c, &list, ch_node) { 2210 list_del(&ch->ch_node); 2211 riocm_ch_close(ch); 2212 } 2213 } 2214 2215 rio_release_inb_mbox(mport, cmbox); 2216 rio_release_outb_mbox(mport, cmbox); 2217 2218 /* Remove and free peer entries */ 2219 if (!list_empty(&cm->peers)) 2220 riocm_debug(RDEV, "ATTN: peer list not empty"); 2221 list_for_each_entry_safe(peer, temp, &cm->peers, node) { 2222 riocm_debug(RDEV, "removing peer %s", rio_name(peer->rdev)); 2223 list_del(&peer->node); 2224 kfree(peer); 2225 } 2226 2227 riocm_rx_free(cm); 2228 kfree(cm); 2229 riocm_debug(MPORT, "%s done", mport->name); 2230 } 2231 2232 static int rio_cm_shutdown(struct notifier_block *nb, unsigned long code, 2233 void *unused) 2234 { 2235 struct rio_channel *ch; 2236 unsigned int i; 2237 LIST_HEAD(list); 2238 2239 riocm_debug(EXIT, "."); 2240 2241 /* 2242 * If there are any channels left in connected state send 2243 * close notification to the connection partner. 2244 * First build a list of channels that require a closing 2245 * notification because function riocm_send_close() should 2246 * be called outside of spinlock protected code. 2247 */ 2248 spin_lock_bh(&idr_lock); 2249 idr_for_each_entry(&ch_idr, ch, i) { 2250 if (ch->state == RIO_CM_CONNECTED) { 2251 riocm_debug(EXIT, "close ch %d", ch->id); 2252 idr_remove(&ch_idr, ch->id); 2253 list_add(&ch->ch_node, &list); 2254 } 2255 } 2256 spin_unlock_bh(&idr_lock); 2257 2258 list_for_each_entry(ch, &list, ch_node) 2259 riocm_send_close(ch); 2260 2261 return NOTIFY_DONE; 2262 } 2263 2264 /* 2265 * riocm_interface handles addition/removal of remote RapidIO devices 2266 */ 2267 static struct subsys_interface riocm_interface = { 2268 .name = "rio_cm", 2269 .subsys = &rio_bus_type, 2270 .add_dev = riocm_add_dev, 2271 .remove_dev = riocm_remove_dev, 2272 }; 2273 2274 /* 2275 * rio_mport_interface handles addition/removal local mport devices 2276 */ 2277 static struct class_interface rio_mport_interface __refdata = { 2278 .class = &rio_mport_class, 2279 .add_dev = riocm_add_mport, 2280 .remove_dev = riocm_remove_mport, 2281 }; 2282 2283 static struct notifier_block rio_cm_notifier = { 2284 .notifier_call = rio_cm_shutdown, 2285 }; 2286 2287 static int __init riocm_init(void) 2288 { 2289 int ret; 2290 2291 /* Create device class needed by udev */ 2292 ret = class_register(&dev_class); 2293 if (ret) { 2294 riocm_error("Cannot create " DRV_NAME " class"); 2295 return ret; 2296 } 2297 2298 ret = alloc_chrdev_region(&dev_number, 0, 1, DRV_NAME); 2299 if (ret) { 2300 class_unregister(&dev_class); 2301 return ret; 2302 } 2303 2304 dev_major = MAJOR(dev_number); 2305 dev_minor_base = MINOR(dev_number); 2306 riocm_debug(INIT, "Registered class with %d major", dev_major); 2307 2308 /* 2309 * Register as rapidio_port class interface to get notifications about 2310 * mport additions and removals. 2311 */ 2312 ret = class_interface_register(&rio_mport_interface); 2313 if (ret) { 2314 riocm_error("class_interface_register error: %d", ret); 2315 goto err_reg; 2316 } 2317 2318 /* 2319 * Register as RapidIO bus interface to get notifications about 2320 * addition/removal of remote RapidIO devices. 2321 */ 2322 ret = subsys_interface_register(&riocm_interface); 2323 if (ret) { 2324 riocm_error("subsys_interface_register error: %d", ret); 2325 goto err_cl; 2326 } 2327 2328 ret = register_reboot_notifier(&rio_cm_notifier); 2329 if (ret) { 2330 riocm_error("failed to register reboot notifier (err=%d)", ret); 2331 goto err_sif; 2332 } 2333 2334 ret = riocm_cdev_add(dev_number); 2335 if (ret) { 2336 unregister_reboot_notifier(&rio_cm_notifier); 2337 ret = -ENODEV; 2338 goto err_sif; 2339 } 2340 2341 return 0; 2342 err_sif: 2343 subsys_interface_unregister(&riocm_interface); 2344 err_cl: 2345 class_interface_unregister(&rio_mport_interface); 2346 err_reg: 2347 unregister_chrdev_region(dev_number, 1); 2348 class_unregister(&dev_class); 2349 return ret; 2350 } 2351 2352 static void __exit riocm_exit(void) 2353 { 2354 riocm_debug(EXIT, "enter"); 2355 unregister_reboot_notifier(&rio_cm_notifier); 2356 subsys_interface_unregister(&riocm_interface); 2357 class_interface_unregister(&rio_mport_interface); 2358 idr_destroy(&ch_idr); 2359 2360 device_unregister(riocm_cdev.dev); 2361 cdev_del(&(riocm_cdev.cdev)); 2362 2363 class_unregister(&dev_class); 2364 unregister_chrdev_region(dev_number, 1); 2365 } 2366 2367 late_initcall(riocm_init); 2368 module_exit(riocm_exit); 2369