misc: mic: Add support for kernel mode SCIF clients
[firefly-linux-kernel-4.4.55.git] / drivers / misc / mic / scif / scif_nodeqp.c
1 /*
2  * Intel MIC Platform Software Stack (MPSS)
3  *
4  * Copyright(c) 2014 Intel Corporation.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License, version 2, as
8  * published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * Intel SCIF driver.
16  *
17  */
18 #include "../bus/scif_bus.h"
19 #include "scif_peer_bus.h"
20 #include "scif_main.h"
21 #include "scif_nodeqp.h"
22 #include "scif_map.h"
23
24 /*
25  ************************************************************************
26  * SCIF node Queue Pair (QP) setup flow:
27  *
28  * 1) SCIF driver gets probed with a scif_hw_dev via the scif_hw_bus
29  * 2) scif_setup_qp(..) allocates the local qp and calls
30  *      scif_setup_qp_connect(..) which allocates and maps the local
31  *      buffer for the inbound QP
32  * 3) The local node updates the device page with the DMA address of the QP
33  * 4) A delayed work is scheduled (qp_dwork) which periodically reads if
34  *      the peer node has updated its QP DMA address
35  * 5) Once a valid non zero address is found in the QP DMA address field
36  *      in the device page, the local node maps the remote node's QP,
37  *      updates its outbound QP and sends a SCIF_INIT message to the peer
38  * 6) The SCIF_INIT message is received by the peer node QP interrupt bottom
39  *      half handler by calling scif_init(..)
40  * 7) scif_init(..) registers a new SCIF peer node by calling
41  *      scif_peer_register_device(..) which signifies the addition of a new
42  *      SCIF node
43  * 8) On the mgmt node, P2P network setup/teardown is initiated if all the
44  *      remote nodes are online via scif_p2p_setup(..)
45  * 9) For P2P setup, the host maps the remote nodes' aperture and memory
46  *      bars and sends a SCIF_NODE_ADD message to both nodes
47  * 10) As part of scif_nodeadd, both nodes set up their local inbound
48  *      QPs and send a SCIF_NODE_ADD_ACK to the mgmt node
49  * 11) As part of scif_node_add_ack(..) the mgmt node forwards the
50  *      SCIF_NODE_ADD_ACK to the remote nodes
51  * 12) As part of scif_node_add_ack(..) the remote nodes update their
52  *      outbound QPs, make sure they can access memory on the remote node
53  *      and then add a new SCIF peer node by calling
54  *      scif_peer_register_device(..) which signifies the addition of a new
55  *      SCIF node.
56  * 13) The SCIF network is now established across all nodes.
57  *
58  ************************************************************************
59  * SCIF node QP teardown flow (initiated by non mgmt node):
60  *
61  * 1) SCIF driver gets a remove callback with a scif_hw_dev via the scif_hw_bus
62  * 2) The device page QP DMA address field is updated with 0x0
63  * 3) A non mgmt node now cleans up all local data structures and sends a
64  *      SCIF_EXIT message to the peer and waits for a SCIF_EXIT_ACK
65  * 4) As part of scif_exit(..) handling scif_disconnect_node(..) is called
66  * 5) scif_disconnect_node(..) sends a SCIF_NODE_REMOVE message to all the
67  *      peers and waits for a SCIF_NODE_REMOVE_ACK
68  * 6) As part of scif_node_remove(..) a remote node unregisters the peer
69  *      node from the SCIF network and sends a SCIF_NODE_REMOVE_ACK
70  * 7) When the mgmt node has received all the SCIF_NODE_REMOVE_ACKs
71  *      it sends itself a node remove message whose handling cleans up local
72  *      data structures and unregisters the peer node from the SCIF network
73  * 8) The mgmt node sends a SCIF_EXIT_ACK
74  * 9) Upon receipt of the SCIF_EXIT_ACK the node initiating the teardown
75  *      completes the SCIF remove routine
76  * 10) The SCIF network is now torn down for the node initiating the
77  *      teardown sequence
78  *
79  ************************************************************************
80  * SCIF node QP teardown flow (initiated by mgmt node):
81  *
82  * 1) SCIF driver gets a remove callback with a scif_hw_dev via the scif_hw_bus
83  * 2) The device page QP DMA address field is updated with 0x0
84  * 3) The mgmt node calls scif_disconnect_node(..)
85  * 4) scif_disconnect_node(..) sends a SCIF_NODE_REMOVE message to all the peers
86  *      and waits for a SCIF_NODE_REMOVE_ACK
87  * 5) As part of scif_node_remove(..) a remote node unregisters the peer
88  *      node from the SCIF network and sends a SCIF_NODE_REMOVE_ACK
89  * 6) When the mgmt node has received all the SCIF_NODE_REMOVE_ACKs
90  *      it unregisters the peer node from the SCIF network
91  * 7) The mgmt node sends a SCIF_EXIT message and waits for a SCIF_EXIT_ACK.
92  * 8) A non mgmt node upon receipt of a SCIF_EXIT message calls scif_stop(..)
93  *      which would clean up local data structures for all SCIF nodes and
94  *      then send a SCIF_EXIT_ACK back to the mgmt node
95  * 9) Upon receipt of the SCIF_EXIT_ACK the the mgmt node sends itself a node
96  *      remove message whose handling cleans up local data structures and
97  *      destroys any P2P mappings.
98  * 10) The SCIF hardware device for which a remove callback was received is now
99  *      disconnected from the SCIF network.
100  */
101 /*
102  * Initializes "local" data structures for the QP. Allocates the QP
103  * ring buffer (rb) and initializes the "in bound" queue.
104  */
105 int scif_setup_qp_connect(struct scif_qp *qp, dma_addr_t *qp_offset,
106                           int local_size, struct scif_dev *scifdev)
107 {
108         void *local_q = NULL;
109         int err = 0;
110         u32 tmp_rd = 0;
111
112         spin_lock_init(&qp->send_lock);
113         spin_lock_init(&qp->recv_lock);
114
115         local_q = kzalloc(local_size, GFP_KERNEL);
116         if (!local_q) {
117                 err = -ENOMEM;
118                 return err;
119         }
120         err = scif_map_single(&qp->local_buf, local_q, scifdev, local_size);
121         if (err)
122                 goto kfree;
123         /*
124          * To setup the inbound_q, the buffer lives locally, the read pointer
125          * is remote and the write pointer is local.
126          */
127         scif_rb_init(&qp->inbound_q,
128                      &tmp_rd,
129                      &qp->local_write,
130                      local_q, get_count_order(local_size));
131         /*
132          * The read pointer is NULL initially and it is unsafe to use the ring
133          * buffer til this changes!
134          */
135         qp->inbound_q.read_ptr = NULL;
136         err = scif_map_single(qp_offset, qp,
137                               scifdev, sizeof(struct scif_qp));
138         if (err)
139                 goto unmap;
140         qp->local_qp = *qp_offset;
141         return err;
142 unmap:
143         scif_unmap_single(qp->local_buf, scifdev, local_size);
144         qp->local_buf = 0;
145 kfree:
146         kfree(local_q);
147         return err;
148 }
149
150 /* When the other side has already done it's allocation, this is called */
151 int scif_setup_qp_accept(struct scif_qp *qp, dma_addr_t *qp_offset,
152                          dma_addr_t phys, int local_size,
153                          struct scif_dev *scifdev)
154 {
155         void *local_q;
156         void *remote_q;
157         struct scif_qp *remote_qp;
158         int remote_size;
159         int err = 0;
160
161         spin_lock_init(&qp->send_lock);
162         spin_lock_init(&qp->recv_lock);
163         /* Start by figuring out where we need to point */
164         remote_qp = scif_ioremap(phys, sizeof(struct scif_qp), scifdev);
165         if (!remote_qp)
166                 return -EIO;
167         qp->remote_qp = remote_qp;
168         if (qp->remote_qp->magic != SCIFEP_MAGIC) {
169                 err = -EIO;
170                 goto iounmap;
171         }
172         qp->remote_buf = remote_qp->local_buf;
173         remote_size = qp->remote_qp->inbound_q.size;
174         remote_q = scif_ioremap(qp->remote_buf, remote_size, scifdev);
175         if (!remote_q) {
176                 err = -EIO;
177                 goto iounmap;
178         }
179         qp->remote_qp->local_write = 0;
180         /*
181          * To setup the outbound_q, the buffer lives in remote memory,
182          * the read pointer is local, the write pointer is remote
183          */
184         scif_rb_init(&qp->outbound_q,
185                      &qp->local_read,
186                      &qp->remote_qp->local_write,
187                      remote_q,
188                      get_count_order(remote_size));
189         local_q = kzalloc(local_size, GFP_KERNEL);
190         if (!local_q) {
191                 err = -ENOMEM;
192                 goto iounmap_1;
193         }
194         err = scif_map_single(&qp->local_buf, local_q, scifdev, local_size);
195         if (err)
196                 goto kfree;
197         qp->remote_qp->local_read = 0;
198         /*
199          * To setup the inbound_q, the buffer lives locally, the read pointer
200          * is remote and the write pointer is local
201          */
202         scif_rb_init(&qp->inbound_q,
203                      &qp->remote_qp->local_read,
204                      &qp->local_write,
205                      local_q, get_count_order(local_size));
206         err = scif_map_single(qp_offset, qp, scifdev,
207                               sizeof(struct scif_qp));
208         if (err)
209                 goto unmap;
210         qp->local_qp = *qp_offset;
211         return err;
212 unmap:
213         scif_unmap_single(qp->local_buf, scifdev, local_size);
214         qp->local_buf = 0;
215 kfree:
216         kfree(local_q);
217 iounmap_1:
218         scif_iounmap(remote_q, remote_size, scifdev);
219         qp->outbound_q.rb_base = NULL;
220 iounmap:
221         scif_iounmap(qp->remote_qp, sizeof(struct scif_qp), scifdev);
222         qp->remote_qp = NULL;
223         return err;
224 }
225
226 int scif_setup_qp_connect_response(struct scif_dev *scifdev,
227                                    struct scif_qp *qp, u64 payload)
228 {
229         int err = 0;
230         void *r_buf;
231         int remote_size;
232         phys_addr_t tmp_phys;
233
234         qp->remote_qp = scif_ioremap(payload, sizeof(struct scif_qp), scifdev);
235
236         if (!qp->remote_qp) {
237                 err = -ENOMEM;
238                 goto error;
239         }
240
241         if (qp->remote_qp->magic != SCIFEP_MAGIC) {
242                 dev_err(&scifdev->sdev->dev,
243                         "SCIFEP_MAGIC mismatch between self %d remote %d\n",
244                         scif_dev[scif_info.nodeid].node, scifdev->node);
245                 err = -ENODEV;
246                 goto error;
247         }
248
249         tmp_phys = qp->remote_qp->local_buf;
250         remote_size = qp->remote_qp->inbound_q.size;
251         r_buf = scif_ioremap(tmp_phys, remote_size, scifdev);
252
253         if (!r_buf)
254                 return -EIO;
255
256         qp->local_read = 0;
257         scif_rb_init(&qp->outbound_q,
258                      &qp->local_read,
259                      &qp->remote_qp->local_write,
260                      r_buf,
261                      get_count_order(remote_size));
262         /*
263          * Because the node QP may already be processing an INIT message, set
264          * the read pointer so the cached read offset isn't lost
265          */
266         qp->remote_qp->local_read = qp->inbound_q.current_read_offset;
267         /*
268          * resetup the inbound_q now that we know where the
269          * inbound_read really is.
270          */
271         scif_rb_init(&qp->inbound_q,
272                      &qp->remote_qp->local_read,
273                      &qp->local_write,
274                      qp->inbound_q.rb_base,
275                      get_count_order(qp->inbound_q.size));
276 error:
277         return err;
278 }
279
280 static __always_inline void
281 scif_send_msg_intr(struct scif_dev *scifdev)
282 {
283         struct scif_hw_dev *sdev = scifdev->sdev;
284
285         if (scifdev_is_p2p(scifdev))
286                 sdev->hw_ops->send_p2p_intr(sdev, scifdev->rdb, &scifdev->mmio);
287         else
288                 sdev->hw_ops->send_intr(sdev, scifdev->rdb);
289 }
290
291 int scif_qp_response(phys_addr_t phys, struct scif_dev *scifdev)
292 {
293         int err = 0;
294         struct scifmsg msg;
295
296         err = scif_setup_qp_connect_response(scifdev, scifdev->qpairs, phys);
297         if (!err) {
298                 /*
299                  * Now that everything is setup and mapped, we're ready
300                  * to tell the peer about our queue's location
301                  */
302                 msg.uop = SCIF_INIT;
303                 msg.dst.node = scifdev->node;
304                 err = scif_nodeqp_send(scifdev, &msg);
305         }
306         return err;
307 }
308
309 void scif_send_exit(struct scif_dev *scifdev)
310 {
311         struct scifmsg msg;
312         int ret;
313
314         scifdev->exit = OP_IN_PROGRESS;
315         msg.uop = SCIF_EXIT;
316         msg.src.node = scif_info.nodeid;
317         msg.dst.node = scifdev->node;
318         ret = scif_nodeqp_send(scifdev, &msg);
319         if (ret)
320                 goto done;
321         /* Wait for a SCIF_EXIT_ACK message */
322         wait_event_timeout(scif_info.exitwq, scifdev->exit == OP_COMPLETED,
323                            SCIF_NODE_ALIVE_TIMEOUT);
324 done:
325         scifdev->exit = OP_IDLE;
326 }
327
328 int scif_setup_qp(struct scif_dev *scifdev)
329 {
330         int err = 0;
331         int local_size;
332         struct scif_qp *qp;
333
334         local_size = SCIF_NODE_QP_SIZE;
335
336         qp = kzalloc(sizeof(*qp), GFP_KERNEL);
337         if (!qp) {
338                 err = -ENOMEM;
339                 return err;
340         }
341         qp->magic = SCIFEP_MAGIC;
342         scifdev->qpairs = qp;
343         err = scif_setup_qp_connect(qp, &scifdev->qp_dma_addr,
344                                     local_size, scifdev);
345         if (err)
346                 goto free_qp;
347         /*
348          * We're as setup as we can be. The inbound_q is setup, w/o a usable
349          * outbound q.  When we get a message, the read_ptr will be updated,
350          * and we will pull the message.
351          */
352         return err;
353 free_qp:
354         kfree(scifdev->qpairs);
355         scifdev->qpairs = NULL;
356         return err;
357 }
358
359 static void scif_p2p_freesg(struct scatterlist *sg)
360 {
361         kfree(sg);
362 }
363
364 static struct scatterlist *
365 scif_p2p_setsg(phys_addr_t pa, int page_size, int page_cnt)
366 {
367         struct scatterlist *sg;
368         struct page *page;
369         int i;
370
371         sg = kcalloc(page_cnt, sizeof(struct scatterlist), GFP_KERNEL);
372         if (!sg)
373                 return NULL;
374         sg_init_table(sg, page_cnt);
375         for (i = 0; i < page_cnt; i++) {
376                 page = pfn_to_page(pa >> PAGE_SHIFT);
377                 sg_set_page(&sg[i], page, page_size, 0);
378                 pa += page_size;
379         }
380         return sg;
381 }
382
383 /* Init p2p mappings required to access peerdev from scifdev */
384 static struct scif_p2p_info *
385 scif_init_p2p_info(struct scif_dev *scifdev, struct scif_dev *peerdev)
386 {
387         struct scif_p2p_info *p2p;
388         int num_mmio_pages, num_aper_pages, sg_page_shift, err, num_aper_chunks;
389         struct scif_hw_dev *psdev = peerdev->sdev;
390         struct scif_hw_dev *sdev = scifdev->sdev;
391
392         num_mmio_pages = psdev->mmio->len >> PAGE_SHIFT;
393         num_aper_pages = psdev->aper->len >> PAGE_SHIFT;
394
395         p2p = kzalloc(sizeof(*p2p), GFP_KERNEL);
396         if (!p2p)
397                 return NULL;
398         p2p->ppi_sg[SCIF_PPI_MMIO] = scif_p2p_setsg(psdev->mmio->pa,
399                                                     PAGE_SIZE, num_mmio_pages);
400         if (!p2p->ppi_sg[SCIF_PPI_MMIO])
401                 goto free_p2p;
402         p2p->sg_nentries[SCIF_PPI_MMIO] = num_mmio_pages;
403         sg_page_shift = get_order(min(psdev->aper->len, (u64)(1 << 30)));
404         num_aper_chunks = num_aper_pages >> (sg_page_shift - PAGE_SHIFT);
405         p2p->ppi_sg[SCIF_PPI_APER] = scif_p2p_setsg(psdev->aper->pa,
406                                                     1 << sg_page_shift,
407                                                     num_aper_chunks);
408         p2p->sg_nentries[SCIF_PPI_APER] = num_aper_chunks;
409         err = dma_map_sg(&sdev->dev, p2p->ppi_sg[SCIF_PPI_MMIO],
410                          num_mmio_pages, PCI_DMA_BIDIRECTIONAL);
411         if (err != num_mmio_pages)
412                 goto scif_p2p_free;
413         err = dma_map_sg(&sdev->dev, p2p->ppi_sg[SCIF_PPI_APER],
414                          num_aper_chunks, PCI_DMA_BIDIRECTIONAL);
415         if (err != num_aper_chunks)
416                 goto dma_unmap;
417         p2p->ppi_da[SCIF_PPI_MMIO] = sg_dma_address(p2p->ppi_sg[SCIF_PPI_MMIO]);
418         p2p->ppi_da[SCIF_PPI_APER] = sg_dma_address(p2p->ppi_sg[SCIF_PPI_APER]);
419         p2p->ppi_len[SCIF_PPI_MMIO] = num_mmio_pages;
420         p2p->ppi_len[SCIF_PPI_APER] = num_aper_pages;
421         p2p->ppi_peer_id = peerdev->node;
422         return p2p;
423 dma_unmap:
424         dma_unmap_sg(&sdev->dev, p2p->ppi_sg[SCIF_PPI_MMIO],
425                      p2p->sg_nentries[SCIF_PPI_MMIO], DMA_BIDIRECTIONAL);
426 scif_p2p_free:
427         scif_p2p_freesg(p2p->ppi_sg[SCIF_PPI_MMIO]);
428         scif_p2p_freesg(p2p->ppi_sg[SCIF_PPI_APER]);
429 free_p2p:
430         kfree(p2p);
431         return NULL;
432 }
433
434 /**
435  * scif_node_connect: Respond to SCIF_NODE_CONNECT interrupt message
436  * @dst: Destination node
437  *
438  * Connect the src and dst node by setting up the p2p connection
439  * between them. Management node here acts like a proxy.
440  */
441 static void scif_node_connect(struct scif_dev *scifdev, int dst)
442 {
443         struct scif_dev *dev_j = scifdev;
444         struct scif_dev *dev_i = NULL;
445         struct scif_p2p_info *p2p_ij = NULL;    /* bus addr for j from i */
446         struct scif_p2p_info *p2p_ji = NULL;    /* bus addr for i from j */
447         struct scif_p2p_info *p2p;
448         struct list_head *pos, *tmp;
449         struct scifmsg msg;
450         int err;
451         u64 tmppayload;
452
453         if (dst < 1 || dst > scif_info.maxid)
454                 return;
455
456         dev_i = &scif_dev[dst];
457
458         if (!_scifdev_alive(dev_i))
459                 return;
460         /*
461          * If the p2p connection is already setup or in the process of setting
462          * up then just ignore this request. The requested node will get
463          * informed by SCIF_NODE_ADD_ACK or SCIF_NODE_ADD_NACK
464          */
465         if (!list_empty(&dev_i->p2p)) {
466                 list_for_each_safe(pos, tmp, &dev_i->p2p) {
467                         p2p = list_entry(pos, struct scif_p2p_info, ppi_list);
468                         if (p2p->ppi_peer_id == dev_j->node)
469                                 return;
470                 }
471         }
472         p2p_ij = scif_init_p2p_info(dev_i, dev_j);
473         if (!p2p_ij)
474                 return;
475         p2p_ji = scif_init_p2p_info(dev_j, dev_i);
476         if (!p2p_ji)
477                 return;
478         list_add_tail(&p2p_ij->ppi_list, &dev_i->p2p);
479         list_add_tail(&p2p_ji->ppi_list, &dev_j->p2p);
480
481         /*
482          * Send a SCIF_NODE_ADD to dev_i, pass it its bus address
483          * as seen from dev_j
484          */
485         msg.uop = SCIF_NODE_ADD;
486         msg.src.node = dev_j->node;
487         msg.dst.node = dev_i->node;
488
489         msg.payload[0] = p2p_ji->ppi_da[SCIF_PPI_APER];
490         msg.payload[1] = p2p_ij->ppi_da[SCIF_PPI_MMIO];
491         msg.payload[2] = p2p_ij->ppi_da[SCIF_PPI_APER];
492         msg.payload[3] = p2p_ij->ppi_len[SCIF_PPI_APER] << PAGE_SHIFT;
493
494         err = scif_nodeqp_send(dev_i,  &msg);
495         if (err) {
496                 dev_err(&scifdev->sdev->dev,
497                         "%s %d error %d\n", __func__, __LINE__, err);
498                 return;
499         }
500
501         /* Same as above but to dev_j */
502         msg.uop = SCIF_NODE_ADD;
503         msg.src.node = dev_i->node;
504         msg.dst.node = dev_j->node;
505
506         tmppayload = msg.payload[0];
507         msg.payload[0] = msg.payload[2];
508         msg.payload[2] = tmppayload;
509         msg.payload[1] = p2p_ji->ppi_da[SCIF_PPI_MMIO];
510         msg.payload[3] = p2p_ji->ppi_len[SCIF_PPI_APER] << PAGE_SHIFT;
511
512         scif_nodeqp_send(dev_j, &msg);
513 }
514
515 static void scif_p2p_setup(void)
516 {
517         int i, j;
518
519         if (!scif_info.p2p_enable)
520                 return;
521
522         for (i = 1; i <= scif_info.maxid; i++)
523                 if (!_scifdev_alive(&scif_dev[i]))
524                         return;
525
526         for (i = 1; i <= scif_info.maxid; i++) {
527                 for (j = 1; j <= scif_info.maxid; j++) {
528                         struct scif_dev *scifdev = &scif_dev[i];
529
530                         if (i == j)
531                                 continue;
532                         scif_node_connect(scifdev, j);
533                 }
534         }
535 }
536
537 static char *message_types[] = {"BAD",
538                                 "INIT",
539                                 "EXIT",
540                                 "SCIF_EXIT_ACK",
541                                 "SCIF_NODE_ADD",
542                                 "SCIF_NODE_ADD_ACK",
543                                 "SCIF_NODE_ADD_NACK",
544                                 "REMOVE_NODE",
545                                 "REMOVE_NODE_ACK",
546                                 "CNCT_REQ",
547                                 "CNCT_GNT",
548                                 "CNCT_GNTACK",
549                                 "CNCT_GNTNACK",
550                                 "CNCT_REJ",
551                                 "DISCNCT",
552                                 "DISCNT_ACK",
553                                 "CLIENT_SENT",
554                                 "CLIENT_RCVD",
555                                 "SCIF_GET_NODE_INFO"};
556
557 static void
558 scif_display_message(struct scif_dev *scifdev, struct scifmsg *msg,
559                      const char *label)
560 {
561         if (!scif_info.en_msg_log)
562                 return;
563         if (msg->uop > SCIF_MAX_MSG) {
564                 dev_err(&scifdev->sdev->dev,
565                         "%s: unknown msg type %d\n", label, msg->uop);
566                 return;
567         }
568         dev_info(&scifdev->sdev->dev,
569                  "%s: msg type %s, src %d:%d, dest %d:%d payload 0x%llx:0x%llx:0x%llx:0x%llx\n",
570                  label, message_types[msg->uop], msg->src.node, msg->src.port,
571                  msg->dst.node, msg->dst.port, msg->payload[0], msg->payload[1],
572                  msg->payload[2], msg->payload[3]);
573 }
574
575 int _scif_nodeqp_send(struct scif_dev *scifdev, struct scifmsg *msg)
576 {
577         struct scif_qp *qp = scifdev->qpairs;
578         int err = -ENOMEM, loop_cnt = 0;
579
580         scif_display_message(scifdev, msg, "Sent");
581         if (!qp) {
582                 err = -EINVAL;
583                 goto error;
584         }
585         spin_lock(&qp->send_lock);
586
587         while ((err = scif_rb_write(&qp->outbound_q,
588                                     msg, sizeof(struct scifmsg)))) {
589                 mdelay(1);
590 #define SCIF_NODEQP_SEND_TO_MSEC (3 * 1000)
591                 if (loop_cnt++ > (SCIF_NODEQP_SEND_TO_MSEC)) {
592                         err = -ENODEV;
593                         break;
594                 }
595         }
596         if (!err)
597                 scif_rb_commit(&qp->outbound_q);
598         spin_unlock(&qp->send_lock);
599         if (!err) {
600                 if (scifdev_self(scifdev))
601                         /*
602                          * For loopback we need to emulate an interrupt by
603                          * queuing work for the queue handling real node
604                          * Qp interrupts.
605                          */
606                         queue_work(scifdev->intr_wq, &scifdev->intr_bh);
607                 else
608                         scif_send_msg_intr(scifdev);
609         }
610 error:
611         if (err)
612                 dev_dbg(&scifdev->sdev->dev,
613                         "%s %d error %d uop %d\n",
614                          __func__, __LINE__, err, msg->uop);
615         return err;
616 }
617
618 /**
619  * scif_nodeqp_send - Send a message on the node queue pair
620  * @scifdev: Scif Device.
621  * @msg: The message to be sent.
622  */
623 int scif_nodeqp_send(struct scif_dev *scifdev, struct scifmsg *msg)
624 {
625         int err;
626         struct device *spdev = NULL;
627
628         if (msg->uop > SCIF_EXIT_ACK) {
629                 /* Dont send messages once the exit flow has begun */
630                 if (OP_IDLE != scifdev->exit)
631                         return -ENODEV;
632                 spdev = scif_get_peer_dev(scifdev);
633                 if (IS_ERR(spdev)) {
634                         err = PTR_ERR(spdev);
635                         return err;
636                 }
637         }
638         err = _scif_nodeqp_send(scifdev, msg);
639         if (msg->uop > SCIF_EXIT_ACK)
640                 scif_put_peer_dev(spdev);
641         return err;
642 }
643
644 /*
645  * scif_misc_handler:
646  *
647  * Work queue handler for servicing miscellaneous SCIF tasks.
648  * Examples include:
649  * 1) Cleanup of zombie endpoints.
650  */
651 void scif_misc_handler(struct work_struct *work)
652 {
653         scif_cleanup_zombie_epd();
654 }
655
656 /**
657  * scif_init() - Respond to SCIF_INIT interrupt message
658  * @scifdev:    Remote SCIF device node
659  * @msg:        Interrupt message
660  */
661 static __always_inline void
662 scif_init(struct scif_dev *scifdev, struct scifmsg *msg)
663 {
664         /*
665          * Allow the thread waiting for device page updates for the peer QP DMA
666          * address to complete initializing the inbound_q.
667          */
668         flush_delayed_work(&scifdev->qp_dwork);
669
670         scif_peer_register_device(scifdev);
671
672         if (scif_is_mgmt_node()) {
673                 mutex_lock(&scif_info.conflock);
674                 scif_p2p_setup();
675                 mutex_unlock(&scif_info.conflock);
676         }
677 }
678
679 /**
680  * scif_exit() - Respond to SCIF_EXIT interrupt message
681  * @scifdev:    Remote SCIF device node
682  * @msg:        Interrupt message
683  *
684  * This function stops the SCIF interface for the node which sent
685  * the SCIF_EXIT message and starts waiting for that node to
686  * resetup the queue pair again.
687  */
688 static __always_inline void
689 scif_exit(struct scif_dev *scifdev, struct scifmsg *unused)
690 {
691         scifdev->exit_ack_pending = true;
692         if (scif_is_mgmt_node())
693                 scif_disconnect_node(scifdev->node, false);
694         else
695                 scif_stop(scifdev);
696         schedule_delayed_work(&scifdev->qp_dwork,
697                               msecs_to_jiffies(1000));
698 }
699
700 /**
701  * scif_exitack() - Respond to SCIF_EXIT_ACK interrupt message
702  * @scifdev:    Remote SCIF device node
703  * @msg:        Interrupt message
704  *
705  */
706 static __always_inline void
707 scif_exit_ack(struct scif_dev *scifdev, struct scifmsg *unused)
708 {
709         scifdev->exit = OP_COMPLETED;
710         wake_up(&scif_info.exitwq);
711 }
712
713 /**
714  * scif_node_add() - Respond to SCIF_NODE_ADD interrupt message
715  * @scifdev:    Remote SCIF device node
716  * @msg:        Interrupt message
717  *
718  * When the mgmt node driver has finished initializing a MIC node queue pair it
719  * marks the node as online. It then looks for all currently online MIC cards
720  * and send a SCIF_NODE_ADD message to identify the ID of the new card for
721  * peer to peer initialization
722  *
723  * The local node allocates its incoming queue and sends its address in the
724  * SCIF_NODE_ADD_ACK message back to the mgmt node, the mgmt node "reflects"
725  * this message to the new node
726  */
727 static __always_inline void
728 scif_node_add(struct scif_dev *scifdev, struct scifmsg *msg)
729 {
730         struct scif_dev *newdev;
731         dma_addr_t qp_offset;
732         int qp_connect;
733         struct scif_hw_dev *sdev;
734
735         dev_dbg(&scifdev->sdev->dev,
736                 "Scifdev %d:%d received NODE_ADD msg for node %d\n",
737                 scifdev->node, msg->dst.node, msg->src.node);
738         dev_dbg(&scifdev->sdev->dev,
739                 "Remote address for this node's aperture %llx\n",
740                 msg->payload[0]);
741         newdev = &scif_dev[msg->src.node];
742         newdev->node = msg->src.node;
743         newdev->sdev = scif_dev[SCIF_MGMT_NODE].sdev;
744         sdev = newdev->sdev;
745
746         if (scif_setup_intr_wq(newdev)) {
747                 dev_err(&scifdev->sdev->dev,
748                         "failed to setup interrupts for %d\n", msg->src.node);
749                 goto interrupt_setup_error;
750         }
751         newdev->mmio.va = ioremap_nocache(msg->payload[1], sdev->mmio->len);
752         if (!newdev->mmio.va) {
753                 dev_err(&scifdev->sdev->dev,
754                         "failed to map mmio for %d\n", msg->src.node);
755                 goto mmio_map_error;
756         }
757         newdev->qpairs = kzalloc(sizeof(*newdev->qpairs), GFP_KERNEL);
758         if (!newdev->qpairs)
759                 goto qp_alloc_error;
760         /*
761          * Set the base address of the remote node's memory since it gets
762          * added to qp_offset
763          */
764         newdev->base_addr = msg->payload[0];
765
766         qp_connect = scif_setup_qp_connect(newdev->qpairs, &qp_offset,
767                                            SCIF_NODE_QP_SIZE, newdev);
768         if (qp_connect) {
769                 dev_err(&scifdev->sdev->dev,
770                         "failed to setup qp_connect %d\n", qp_connect);
771                 goto qp_connect_error;
772         }
773
774         newdev->db = sdev->hw_ops->next_db(sdev);
775         newdev->cookie = sdev->hw_ops->request_irq(sdev, scif_intr_handler,
776                                                    "SCIF_INTR", newdev,
777                                                    newdev->db);
778         if (IS_ERR(newdev->cookie))
779                 goto qp_connect_error;
780         newdev->qpairs->magic = SCIFEP_MAGIC;
781         newdev->qpairs->qp_state = SCIF_QP_OFFLINE;
782
783         msg->uop = SCIF_NODE_ADD_ACK;
784         msg->dst.node = msg->src.node;
785         msg->src.node = scif_info.nodeid;
786         msg->payload[0] = qp_offset;
787         msg->payload[2] = newdev->db;
788         scif_nodeqp_send(&scif_dev[SCIF_MGMT_NODE], msg);
789         return;
790 qp_connect_error:
791         kfree(newdev->qpairs);
792         newdev->qpairs = NULL;
793 qp_alloc_error:
794         iounmap(newdev->mmio.va);
795         newdev->mmio.va = NULL;
796 mmio_map_error:
797 interrupt_setup_error:
798         dev_err(&scifdev->sdev->dev,
799                 "node add failed for node %d\n", msg->src.node);
800         msg->uop = SCIF_NODE_ADD_NACK;
801         msg->dst.node = msg->src.node;
802         msg->src.node = scif_info.nodeid;
803         scif_nodeqp_send(&scif_dev[SCIF_MGMT_NODE], msg);
804 }
805
806 void scif_poll_qp_state(struct work_struct *work)
807 {
808 #define SCIF_NODE_QP_RETRY 100
809 #define SCIF_NODE_QP_TIMEOUT 100
810         struct scif_dev *peerdev = container_of(work, struct scif_dev,
811                                                         p2p_dwork.work);
812         struct scif_qp *qp = &peerdev->qpairs[0];
813
814         if (qp->qp_state != SCIF_QP_ONLINE ||
815             qp->remote_qp->qp_state != SCIF_QP_ONLINE) {
816                 if (peerdev->p2p_retry++ == SCIF_NODE_QP_RETRY) {
817                         dev_err(&peerdev->sdev->dev,
818                                 "Warning: QP check timeout with state %d\n",
819                                 qp->qp_state);
820                         goto timeout;
821                 }
822                 schedule_delayed_work(&peerdev->p2p_dwork,
823                                       msecs_to_jiffies(SCIF_NODE_QP_TIMEOUT));
824                 return;
825         }
826         return;
827 timeout:
828         dev_err(&peerdev->sdev->dev,
829                 "%s %d remote node %d offline,  state = 0x%x\n",
830                 __func__, __LINE__, peerdev->node, qp->qp_state);
831         qp->remote_qp->qp_state = SCIF_QP_OFFLINE;
832         scif_peer_unregister_device(peerdev);
833         scif_cleanup_scifdev(peerdev);
834 }
835
836 /**
837  * scif_node_add_ack() - Respond to SCIF_NODE_ADD_ACK interrupt message
838  * @scifdev:    Remote SCIF device node
839  * @msg:        Interrupt message
840  *
841  * After a MIC node receives the SCIF_NODE_ADD_ACK message it send this
842  * message to the mgmt node to confirm the sequence is finished.
843  *
844  */
845 static __always_inline void
846 scif_node_add_ack(struct scif_dev *scifdev, struct scifmsg *msg)
847 {
848         struct scif_dev *peerdev;
849         struct scif_qp *qp;
850         struct scif_dev *dst_dev = &scif_dev[msg->dst.node];
851
852         dev_dbg(&scifdev->sdev->dev,
853                 "Scifdev %d received SCIF_NODE_ADD_ACK msg src %d dst %d\n",
854                 scifdev->node, msg->src.node, msg->dst.node);
855         dev_dbg(&scifdev->sdev->dev,
856                 "payload %llx %llx %llx %llx\n", msg->payload[0],
857                 msg->payload[1], msg->payload[2], msg->payload[3]);
858         if (scif_is_mgmt_node()) {
859                 /*
860                  * the lock serializes with scif_qp_response_ack. The mgmt node
861                  * is forwarding the NODE_ADD_ACK message from src to dst we
862                  * need to make sure that the dst has already received a
863                  * NODE_ADD for src and setup its end of the qp to dst
864                  */
865                 mutex_lock(&scif_info.conflock);
866                 msg->payload[1] = scif_info.maxid;
867                 scif_nodeqp_send(dst_dev, msg);
868                 mutex_unlock(&scif_info.conflock);
869                 return;
870         }
871         peerdev = &scif_dev[msg->src.node];
872         peerdev->sdev = scif_dev[SCIF_MGMT_NODE].sdev;
873         peerdev->node = msg->src.node;
874
875         qp = &peerdev->qpairs[0];
876
877         if ((scif_setup_qp_connect_response(peerdev, &peerdev->qpairs[0],
878                                             msg->payload[0])))
879                 goto local_error;
880         peerdev->rdb = msg->payload[2];
881         qp->remote_qp->qp_state = SCIF_QP_ONLINE;
882
883         scif_peer_register_device(peerdev);
884
885         schedule_delayed_work(&peerdev->p2p_dwork, 0);
886         return;
887 local_error:
888         scif_cleanup_scifdev(peerdev);
889 }
890
891 /**
892  * scif_node_add_nack: Respond to SCIF_NODE_ADD_NACK interrupt message
893  * @msg:        Interrupt message
894  *
895  * SCIF_NODE_ADD failed, so inform the waiting wq.
896  */
897 static __always_inline void
898 scif_node_add_nack(struct scif_dev *scifdev, struct scifmsg *msg)
899 {
900         if (scif_is_mgmt_node()) {
901                 struct scif_dev *dst_dev = &scif_dev[msg->dst.node];
902
903                 dev_dbg(&scifdev->sdev->dev,
904                         "SCIF_NODE_ADD_NACK received from %d\n", scifdev->node);
905                 scif_nodeqp_send(dst_dev, msg);
906         }
907 }
908
909 /*
910  * scif_node_remove: Handle SCIF_NODE_REMOVE message
911  * @msg: Interrupt message
912  *
913  * Handle node removal.
914  */
915 static __always_inline void
916 scif_node_remove(struct scif_dev *scifdev, struct scifmsg *msg)
917 {
918         int node = msg->payload[0];
919         struct scif_dev *scdev = &scif_dev[node];
920
921         scdev->node_remove_ack_pending = true;
922         scif_handle_remove_node(node);
923 }
924
925 /*
926  * scif_node_remove_ack: Handle SCIF_NODE_REMOVE_ACK message
927  * @msg: Interrupt message
928  *
929  * The peer has acked a SCIF_NODE_REMOVE message.
930  */
931 static __always_inline void
932 scif_node_remove_ack(struct scif_dev *scifdev, struct scifmsg *msg)
933 {
934         struct scif_dev *sdev = &scif_dev[msg->payload[0]];
935
936         atomic_inc(&sdev->disconn_rescnt);
937         wake_up(&sdev->disconn_wq);
938 }
939
940 /**
941  * scif_get_node_info: Respond to SCIF_GET_NODE_INFO interrupt message
942  * @msg:        Interrupt message
943  *
944  * Retrieve node info i.e maxid and total from the mgmt node.
945  */
946 static __always_inline void
947 scif_get_node_info_resp(struct scif_dev *scifdev, struct scifmsg *msg)
948 {
949         if (scif_is_mgmt_node()) {
950                 swap(msg->dst.node, msg->src.node);
951                 mutex_lock(&scif_info.conflock);
952                 msg->payload[1] = scif_info.maxid;
953                 msg->payload[2] = scif_info.total;
954                 mutex_unlock(&scif_info.conflock);
955                 scif_nodeqp_send(scifdev, msg);
956         } else {
957                 struct completion *node_info =
958                         (struct completion *)msg->payload[3];
959
960                 mutex_lock(&scif_info.conflock);
961                 scif_info.maxid = msg->payload[1];
962                 scif_info.total = msg->payload[2];
963                 complete_all(node_info);
964                 mutex_unlock(&scif_info.conflock);
965         }
966 }
967
968 static void
969 scif_msg_unknown(struct scif_dev *scifdev, struct scifmsg *msg)
970 {
971         /* Bogus Node Qp Message? */
972         dev_err(&scifdev->sdev->dev,
973                 "Unknown message 0x%xn scifdev->node 0x%x\n",
974                 msg->uop, scifdev->node);
975 }
976
977 static void (*scif_intr_func[SCIF_MAX_MSG + 1])
978             (struct scif_dev *, struct scifmsg *msg) = {
979         scif_msg_unknown,       /* Error */
980         scif_init,              /* SCIF_INIT */
981         scif_exit,              /* SCIF_EXIT */
982         scif_exit_ack,          /* SCIF_EXIT_ACK */
983         scif_node_add,          /* SCIF_NODE_ADD */
984         scif_node_add_ack,      /* SCIF_NODE_ADD_ACK */
985         scif_node_add_nack,     /* SCIF_NODE_ADD_NACK */
986         scif_node_remove,       /* SCIF_NODE_REMOVE */
987         scif_node_remove_ack,   /* SCIF_NODE_REMOVE_ACK */
988         scif_cnctreq,           /* SCIF_CNCT_REQ */
989         scif_cnctgnt,           /* SCIF_CNCT_GNT */
990         scif_cnctgnt_ack,       /* SCIF_CNCT_GNTACK */
991         scif_cnctgnt_nack,      /* SCIF_CNCT_GNTNACK */
992         scif_cnctrej,           /* SCIF_CNCT_REJ */
993         scif_discnct,           /* SCIF_DISCNCT */
994         scif_discnt_ack,        /* SCIF_DISCNT_ACK */
995         scif_clientsend,        /* SCIF_CLIENT_SENT */
996         scif_clientrcvd,        /* SCIF_CLIENT_RCVD */
997         scif_get_node_info_resp,/* SCIF_GET_NODE_INFO */
998 };
999
1000 /**
1001  * scif_nodeqp_msg_handler() - Common handler for node messages
1002  * @scifdev: Remote device to respond to
1003  * @qp: Remote memory pointer
1004  * @msg: The message to be handled.
1005  *
1006  * This routine calls the appropriate routine to handle a Node Qp
1007  * message receipt
1008  */
1009 static int scif_max_msg_id = SCIF_MAX_MSG;
1010
1011 static void
1012 scif_nodeqp_msg_handler(struct scif_dev *scifdev,
1013                         struct scif_qp *qp, struct scifmsg *msg)
1014 {
1015         scif_display_message(scifdev, msg, "Rcvd");
1016
1017         if (msg->uop > (u32)scif_max_msg_id) {
1018                 /* Bogus Node Qp Message? */
1019                 dev_err(&scifdev->sdev->dev,
1020                         "Unknown message 0x%xn scifdev->node 0x%x\n",
1021                         msg->uop, scifdev->node);
1022                 return;
1023         }
1024
1025         scif_intr_func[msg->uop](scifdev, msg);
1026 }
1027
1028 /**
1029  * scif_nodeqp_intrhandler() - Interrupt handler for node messages
1030  * @scifdev:    Remote device to respond to
1031  * @qp:         Remote memory pointer
1032  *
1033  * This routine is triggered by the interrupt mechanism.  It reads
1034  * messages from the node queue RB and calls the Node QP Message handling
1035  * routine.
1036  */
1037 void scif_nodeqp_intrhandler(struct scif_dev *scifdev, struct scif_qp *qp)
1038 {
1039         struct scifmsg msg;
1040         int read_size;
1041
1042         do {
1043                 read_size = scif_rb_get_next(&qp->inbound_q, &msg, sizeof(msg));
1044                 if (!read_size)
1045                         break;
1046                 scif_nodeqp_msg_handler(scifdev, qp, &msg);
1047                 /*
1048                  * The node queue pair is unmapped so skip the read pointer
1049                  * update after receipt of a SCIF_EXIT_ACK
1050                  */
1051                 if (SCIF_EXIT_ACK == msg.uop)
1052                         break;
1053                 scif_rb_update_read_ptr(&qp->inbound_q);
1054         } while (1);
1055 }
1056
1057 /**
1058  * scif_loopb_wq_handler - Loopback Workqueue Handler.
1059  * @work: loop back work
1060  *
1061  * This work queue routine is invoked by the loopback work queue handler.
1062  * It grabs the recv lock, dequeues any available messages from the head
1063  * of the loopback message list, calls the node QP message handler,
1064  * waits for it to return, then frees up this message and dequeues more
1065  * elements of the list if available.
1066  */
1067 static void scif_loopb_wq_handler(struct work_struct *unused)
1068 {
1069         struct scif_dev *scifdev = scif_info.loopb_dev;
1070         struct scif_qp *qp = scifdev->qpairs;
1071         struct scif_loopb_msg *msg;
1072
1073         do {
1074                 msg = NULL;
1075                 spin_lock(&qp->recv_lock);
1076                 if (!list_empty(&scif_info.loopb_recv_q)) {
1077                         msg = list_first_entry(&scif_info.loopb_recv_q,
1078                                                struct scif_loopb_msg,
1079                                                list);
1080                         list_del(&msg->list);
1081                 }
1082                 spin_unlock(&qp->recv_lock);
1083
1084                 if (msg) {
1085                         scif_nodeqp_msg_handler(scifdev, qp, &msg->msg);
1086                         kfree(msg);
1087                 }
1088         } while (msg);
1089 }
1090
1091 /**
1092  * scif_loopb_msg_handler() - Workqueue handler for loopback messages.
1093  * @scifdev: SCIF device
1094  * @qp: Queue pair.
1095  *
1096  * This work queue routine is triggered when a loopback message is received.
1097  *
1098  * We need special handling for receiving Node Qp messages on a loopback SCIF
1099  * device via two workqueues for receiving messages.
1100  *
1101  * The reason we need the extra workqueue which is not required with *normal*
1102  * non-loopback SCIF devices is the potential classic deadlock described below:
1103  *
1104  * Thread A tries to send a message on a loopback SCIF device and blocks since
1105  * there is no space in the RB while it has the send_lock held or another
1106  * lock called lock X for example.
1107  *
1108  * Thread B: The Loopback Node QP message receive workqueue receives the message
1109  * and tries to send a message (eg an ACK) to the loopback SCIF device. It tries
1110  * to grab the send lock again or lock X and deadlocks with Thread A. The RB
1111  * cannot be drained any further due to this classic deadlock.
1112  *
1113  * In order to avoid deadlocks as mentioned above we have an extra level of
1114  * indirection achieved by having two workqueues.
1115  * 1) The first workqueue whose handler is scif_loopb_msg_handler reads
1116  * messages from the Node QP RB, adds them to a list and queues work for the
1117  * second workqueue.
1118  *
1119  * 2) The second workqueue whose handler is scif_loopb_wq_handler dequeues
1120  * messages from the list, handles them, frees up the memory and dequeues
1121  * more elements from the list if possible.
1122  */
1123 int
1124 scif_loopb_msg_handler(struct scif_dev *scifdev, struct scif_qp *qp)
1125 {
1126         int read_size;
1127         struct scif_loopb_msg *msg;
1128
1129         do {
1130                 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1131                 if (!msg)
1132                         return -ENOMEM;
1133                 read_size = scif_rb_get_next(&qp->inbound_q, &msg->msg,
1134                                              sizeof(struct scifmsg));
1135                 if (read_size != sizeof(struct scifmsg)) {
1136                         kfree(msg);
1137                         scif_rb_update_read_ptr(&qp->inbound_q);
1138                         break;
1139                 }
1140                 spin_lock(&qp->recv_lock);
1141                 list_add_tail(&msg->list, &scif_info.loopb_recv_q);
1142                 spin_unlock(&qp->recv_lock);
1143                 queue_work(scif_info.loopb_wq, &scif_info.loopb_work);
1144                 scif_rb_update_read_ptr(&qp->inbound_q);
1145         } while (read_size == sizeof(struct scifmsg));
1146         return read_size;
1147 }
1148
1149 /**
1150  * scif_setup_loopback_qp - One time setup work for Loopback Node Qp.
1151  * @scifdev: SCIF device
1152  *
1153  * Sets up the required loopback workqueues, queue pairs and ring buffers
1154  */
1155 int scif_setup_loopback_qp(struct scif_dev *scifdev)
1156 {
1157         int err = 0;
1158         void *local_q;
1159         struct scif_qp *qp;
1160
1161         err = scif_setup_intr_wq(scifdev);
1162         if (err)
1163                 goto exit;
1164         INIT_LIST_HEAD(&scif_info.loopb_recv_q);
1165         snprintf(scif_info.loopb_wqname, sizeof(scif_info.loopb_wqname),
1166                  "SCIF LOOPB %d", scifdev->node);
1167         scif_info.loopb_wq =
1168                 alloc_ordered_workqueue(scif_info.loopb_wqname, 0);
1169         if (!scif_info.loopb_wq) {
1170                 err = -ENOMEM;
1171                 goto destroy_intr;
1172         }
1173         INIT_WORK(&scif_info.loopb_work, scif_loopb_wq_handler);
1174         /* Allocate Self Qpair */
1175         scifdev->qpairs = kzalloc(sizeof(*scifdev->qpairs), GFP_KERNEL);
1176         if (!scifdev->qpairs) {
1177                 err = -ENOMEM;
1178                 goto destroy_loopb_wq;
1179         }
1180
1181         qp = scifdev->qpairs;
1182         qp->magic = SCIFEP_MAGIC;
1183         spin_lock_init(&qp->send_lock);
1184         spin_lock_init(&qp->recv_lock);
1185
1186         local_q = kzalloc(SCIF_NODE_QP_SIZE, GFP_KERNEL);
1187         if (!local_q) {
1188                 err = -ENOMEM;
1189                 goto free_qpairs;
1190         }
1191         /*
1192          * For loopback the inbound_q and outbound_q are essentially the same
1193          * since the Node sends a message on the loopback interface to the
1194          * outbound_q which is then received on the inbound_q.
1195          */
1196         scif_rb_init(&qp->outbound_q,
1197                      &qp->local_read,
1198                      &qp->local_write,
1199                      local_q, get_count_order(SCIF_NODE_QP_SIZE));
1200
1201         scif_rb_init(&qp->inbound_q,
1202                      &qp->local_read,
1203                      &qp->local_write,
1204                      local_q, get_count_order(SCIF_NODE_QP_SIZE));
1205         scif_info.nodeid = scifdev->node;
1206
1207         scif_peer_register_device(scifdev);
1208
1209         scif_info.loopb_dev = scifdev;
1210         return err;
1211 free_qpairs:
1212         kfree(scifdev->qpairs);
1213 destroy_loopb_wq:
1214         destroy_workqueue(scif_info.loopb_wq);
1215 destroy_intr:
1216         scif_destroy_intr_wq(scifdev);
1217 exit:
1218         return err;
1219 }
1220
1221 /**
1222  * scif_destroy_loopback_qp - One time uninit work for Loopback Node Qp
1223  * @scifdev: SCIF device
1224  *
1225  * Destroys the workqueues and frees up the Ring Buffer and Queue Pair memory.
1226  */
1227 int scif_destroy_loopback_qp(struct scif_dev *scifdev)
1228 {
1229         scif_peer_unregister_device(scifdev);
1230         destroy_workqueue(scif_info.loopb_wq);
1231         scif_destroy_intr_wq(scifdev);
1232         kfree(scifdev->qpairs->outbound_q.rb_base);
1233         kfree(scifdev->qpairs);
1234         scifdev->sdev = NULL;
1235         scif_info.loopb_dev = NULL;
1236         return 0;
1237 }
1238
1239 void scif_destroy_p2p(struct scif_dev *scifdev)
1240 {
1241         struct scif_dev *peer_dev;
1242         struct scif_p2p_info *p2p;
1243         struct list_head *pos, *tmp;
1244         int bd;
1245
1246         mutex_lock(&scif_info.conflock);
1247         /* Free P2P mappings in the given node for all its peer nodes */
1248         list_for_each_safe(pos, tmp, &scifdev->p2p) {
1249                 p2p = list_entry(pos, struct scif_p2p_info, ppi_list);
1250                 dma_unmap_sg(&scifdev->sdev->dev, p2p->ppi_sg[SCIF_PPI_MMIO],
1251                              p2p->sg_nentries[SCIF_PPI_MMIO],
1252                              DMA_BIDIRECTIONAL);
1253                 dma_unmap_sg(&scifdev->sdev->dev, p2p->ppi_sg[SCIF_PPI_APER],
1254                              p2p->sg_nentries[SCIF_PPI_APER],
1255                              DMA_BIDIRECTIONAL);
1256                 scif_p2p_freesg(p2p->ppi_sg[SCIF_PPI_MMIO]);
1257                 scif_p2p_freesg(p2p->ppi_sg[SCIF_PPI_APER]);
1258                 list_del(pos);
1259                 kfree(p2p);
1260         }
1261
1262         /* Free P2P mapping created in the peer nodes for the given node */
1263         for (bd = SCIF_MGMT_NODE + 1; bd <= scif_info.maxid; bd++) {
1264                 peer_dev = &scif_dev[bd];
1265                 list_for_each_safe(pos, tmp, &peer_dev->p2p) {
1266                         p2p = list_entry(pos, struct scif_p2p_info, ppi_list);
1267                         if (p2p->ppi_peer_id == scifdev->node) {
1268                                 dma_unmap_sg(&peer_dev->sdev->dev,
1269                                              p2p->ppi_sg[SCIF_PPI_MMIO],
1270                                              p2p->sg_nentries[SCIF_PPI_MMIO],
1271                                              DMA_BIDIRECTIONAL);
1272                                 dma_unmap_sg(&peer_dev->sdev->dev,
1273                                              p2p->ppi_sg[SCIF_PPI_APER],
1274                                              p2p->sg_nentries[SCIF_PPI_APER],
1275                                              DMA_BIDIRECTIONAL);
1276                                 scif_p2p_freesg(p2p->ppi_sg[SCIF_PPI_MMIO]);
1277                                 scif_p2p_freesg(p2p->ppi_sg[SCIF_PPI_APER]);
1278                                 list_del(pos);
1279                                 kfree(p2p);
1280                         }
1281                 }
1282         }
1283         mutex_unlock(&scif_info.conflock);
1284 }