2 * Intel MIC Platform Software Stack (MPSS)
4 * Copyright(c) 2014 Intel Corporation.
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.
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.
18 #include <linux/module.h>
19 #include <linux/idr.h>
21 #include <linux/mic_common.h>
22 #include "../common/mic_dev.h"
23 #include "../bus/scif_bus.h"
24 #include "scif_peer_bus.h"
25 #include "scif_main.h"
28 struct scif_info scif_info
= {
30 .minor
= MISC_DYNAMIC_MINOR
,
36 struct scif_dev
*scif_dev
;
37 static atomic_t g_loopb_cnt
;
39 /* Runs in the context of intr_wq */
40 static void scif_intr_bh_handler(struct work_struct
*work
)
42 struct scif_dev
*scifdev
=
43 container_of(work
, struct scif_dev
, intr_bh
);
45 if (scifdev_self(scifdev
))
46 scif_loopb_msg_handler(scifdev
, scifdev
->qpairs
);
48 scif_nodeqp_intrhandler(scifdev
, scifdev
->qpairs
);
51 int scif_setup_intr_wq(struct scif_dev
*scifdev
)
53 if (!scifdev
->intr_wq
) {
54 snprintf(scifdev
->intr_wqname
, sizeof(scifdev
->intr_wqname
),
55 "SCIF INTR %d", scifdev
->node
);
57 alloc_ordered_workqueue(scifdev
->intr_wqname
, 0);
58 if (!scifdev
->intr_wq
)
60 INIT_WORK(&scifdev
->intr_bh
, scif_intr_bh_handler
);
65 void scif_destroy_intr_wq(struct scif_dev
*scifdev
)
67 if (scifdev
->intr_wq
) {
68 destroy_workqueue(scifdev
->intr_wq
);
69 scifdev
->intr_wq
= NULL
;
73 irqreturn_t
scif_intr_handler(int irq
, void *data
)
75 struct scif_dev
*scifdev
= data
;
76 struct scif_hw_dev
*sdev
= scifdev
->sdev
;
78 sdev
->hw_ops
->ack_interrupt(sdev
, scifdev
->db
);
79 queue_work(scifdev
->intr_wq
, &scifdev
->intr_bh
);
83 static int scif_peer_probe(struct scif_peer_dev
*spdev
)
85 struct scif_dev
*scifdev
= &scif_dev
[spdev
->dnode
];
87 mutex_lock(&scif_info
.conflock
);
89 scif_info
.maxid
= max_t(u32
, spdev
->dnode
, scif_info
.maxid
);
90 mutex_unlock(&scif_info
.conflock
);
91 rcu_assign_pointer(scifdev
->spdev
, spdev
);
93 /* In the future SCIF kernel client devices will be added here */
97 static void scif_peer_remove(struct scif_peer_dev
*spdev
)
99 struct scif_dev
*scifdev
= &scif_dev
[spdev
->dnode
];
101 /* In the future SCIF kernel client devices will be removed here */
102 spdev
= rcu_dereference(scifdev
->spdev
);
104 RCU_INIT_POINTER(scifdev
->spdev
, NULL
);
107 mutex_lock(&scif_info
.conflock
);
109 mutex_unlock(&scif_info
.conflock
);
112 static void scif_qp_setup_handler(struct work_struct
*work
)
114 struct scif_dev
*scifdev
= container_of(work
, struct scif_dev
,
116 struct scif_hw_dev
*sdev
= scifdev
->sdev
;
120 if (scif_is_mgmt_node()) {
121 struct mic_bootparam
*bp
= sdev
->dp
;
123 da
= bp
->scif_card_dma_addr
;
124 scifdev
->rdb
= bp
->h2c_scif_db
;
126 struct mic_bootparam __iomem
*bp
= sdev
->rdp
;
128 da
= readq(&bp
->scif_host_dma_addr
);
129 scifdev
->rdb
= ioread8(&bp
->c2h_scif_db
);
132 err
= scif_qp_response(da
, scifdev
);
134 dev_err(&scifdev
->sdev
->dev
,
135 "scif_qp_response err %d\n", err
);
137 schedule_delayed_work(&scifdev
->qp_dwork
,
138 msecs_to_jiffies(1000));
142 static int scif_setup_scifdev(struct scif_hw_dev
*sdev
)
148 struct mic_bootparam __iomem
*bp
= sdev
->rdp
;
150 num_nodes
= ioread8(&bp
->tot_nodes
);
152 struct mic_bootparam
*bp
= sdev
->dp
;
154 num_nodes
= bp
->tot_nodes
;
156 scif_dev
= kcalloc(num_nodes
, sizeof(*scif_dev
), GFP_KERNEL
);
159 for (i
= 0; i
< num_nodes
; i
++) {
160 struct scif_dev
*scifdev
= &scif_dev
[i
];
163 scifdev
->exit
= OP_IDLE
;
164 init_waitqueue_head(&scifdev
->disconn_wq
);
165 mutex_init(&scifdev
->lock
);
166 INIT_WORK(&scifdev
->init_msg_work
, scif_qp_response_ack
);
167 INIT_DELAYED_WORK(&scifdev
->p2p_dwork
,
169 INIT_DELAYED_WORK(&scifdev
->qp_dwork
,
170 scif_qp_setup_handler
);
171 INIT_LIST_HEAD(&scifdev
->p2p
);
172 RCU_INIT_POINTER(scifdev
->spdev
, NULL
);
177 static void scif_destroy_scifdev(void)
182 static int scif_probe(struct scif_hw_dev
*sdev
)
184 struct scif_dev
*scifdev
;
187 dev_set_drvdata(&sdev
->dev
, sdev
);
188 if (1 == atomic_add_return(1, &g_loopb_cnt
)) {
189 struct scif_dev
*loopb_dev
;
191 rc
= scif_setup_scifdev(sdev
);
194 scifdev
= &scif_dev
[sdev
->dnode
];
195 scifdev
->sdev
= sdev
;
196 loopb_dev
= &scif_dev
[sdev
->snode
];
197 loopb_dev
->sdev
= sdev
;
198 rc
= scif_setup_loopback_qp(loopb_dev
);
202 scifdev
= &scif_dev
[sdev
->dnode
];
203 scifdev
->sdev
= sdev
;
205 rc
= scif_setup_intr_wq(scifdev
);
208 rc
= scif_setup_qp(scifdev
);
211 scifdev
->db
= sdev
->hw_ops
->next_db(sdev
);
212 scifdev
->cookie
= sdev
->hw_ops
->request_irq(sdev
, scif_intr_handler
,
213 "SCIF_INTR", scifdev
,
215 if (IS_ERR(scifdev
->cookie
)) {
216 rc
= PTR_ERR(scifdev
->cookie
);
219 if (scif_is_mgmt_node()) {
220 struct mic_bootparam
*bp
= sdev
->dp
;
222 bp
->c2h_scif_db
= scifdev
->db
;
223 bp
->scif_host_dma_addr
= scifdev
->qp_dma_addr
;
225 struct mic_bootparam __iomem
*bp
= sdev
->rdp
;
227 iowrite8(scifdev
->db
, &bp
->h2c_scif_db
);
228 writeq(scifdev
->qp_dma_addr
, &bp
->scif_card_dma_addr
);
230 schedule_delayed_work(&scifdev
->qp_dwork
,
231 msecs_to_jiffies(1000));
234 scif_free_qp(scifdev
);
236 scif_destroy_intr_wq(scifdev
);
238 if (atomic_dec_and_test(&g_loopb_cnt
))
239 scif_destroy_loopback_qp(&scif_dev
[sdev
->snode
]);
241 scif_destroy_scifdev();
246 void scif_stop(struct scif_dev
*scifdev
)
248 struct scif_dev
*dev
;
251 for (i
= scif_info
.maxid
; i
>= 0; i
--) {
253 if (scifdev_self(dev
))
255 scif_handle_remove_node(i
);
259 static void scif_remove(struct scif_hw_dev
*sdev
)
261 struct scif_dev
*scifdev
= &scif_dev
[sdev
->dnode
];
263 if (scif_is_mgmt_node()) {
264 struct mic_bootparam
*bp
= sdev
->dp
;
266 bp
->c2h_scif_db
= -1;
267 bp
->scif_host_dma_addr
= 0x0;
269 struct mic_bootparam __iomem
*bp
= sdev
->rdp
;
271 iowrite8(-1, &bp
->h2c_scif_db
);
272 writeq(0x0, &bp
->scif_card_dma_addr
);
274 if (scif_is_mgmt_node()) {
275 scif_disconnect_node(scifdev
->node
, true);
277 scif_info
.card_initiated_exit
= true;
280 if (atomic_dec_and_test(&g_loopb_cnt
))
281 scif_destroy_loopback_qp(&scif_dev
[sdev
->snode
]);
282 if (scifdev
->cookie
) {
283 sdev
->hw_ops
->free_irq(sdev
, scifdev
->cookie
, scifdev
);
284 scifdev
->cookie
= NULL
;
286 scif_destroy_intr_wq(scifdev
);
287 cancel_delayed_work(&scifdev
->qp_dwork
);
288 scif_free_qp(scifdev
);
290 scifdev
->sdev
= NULL
;
293 static struct scif_peer_driver scif_peer_driver
= {
294 .driver
.name
= KBUILD_MODNAME
,
295 .driver
.owner
= THIS_MODULE
,
296 .probe
= scif_peer_probe
,
297 .remove
= scif_peer_remove
,
300 static struct scif_hw_dev_id id_table
[] = {
301 { MIC_SCIF_DEV
, SCIF_DEV_ANY_ID
},
305 static struct scif_driver scif_driver
= {
306 .driver
.name
= KBUILD_MODNAME
,
307 .driver
.owner
= THIS_MODULE
,
308 .id_table
= id_table
,
310 .remove
= scif_remove
,
313 static int _scif_init(void)
315 spin_lock_init(&scif_info
.eplock
);
316 spin_lock_init(&scif_info
.nb_connect_lock
);
317 spin_lock_init(&scif_info
.port_lock
);
318 mutex_init(&scif_info
.conflock
);
319 mutex_init(&scif_info
.connlock
);
320 INIT_LIST_HEAD(&scif_info
.uaccept
);
321 INIT_LIST_HEAD(&scif_info
.listen
);
322 INIT_LIST_HEAD(&scif_info
.zombie
);
323 INIT_LIST_HEAD(&scif_info
.connected
);
324 INIT_LIST_HEAD(&scif_info
.disconnected
);
325 INIT_LIST_HEAD(&scif_info
.nb_connect_list
);
326 init_waitqueue_head(&scif_info
.exitwq
);
327 scif_info
.en_msg_log
= 0;
328 scif_info
.p2p_enable
= 1;
329 INIT_WORK(&scif_info
.misc_work
, scif_misc_handler
);
330 INIT_WORK(&scif_info
.conn_work
, scif_conn_handler
);
331 idr_init(&scif_ports
);
335 static void _scif_exit(void)
337 idr_destroy(&scif_ports
);
338 scif_destroy_scifdev();
341 static int __init
scif_init(void)
343 struct miscdevice
*mdev
= &scif_info
.mdev
;
347 rc
= scif_peer_bus_init();
350 rc
= scif_peer_register_driver(&scif_peer_driver
);
353 rc
= scif_register_driver(&scif_driver
);
355 goto unreg_scif_peer
;
356 rc
= misc_register(mdev
);
362 scif_unregister_driver(&scif_driver
);
364 scif_peer_unregister_driver(&scif_peer_driver
);
366 scif_peer_bus_exit();
372 static void __exit
scif_exit(void)
375 misc_deregister(&scif_info
.mdev
);
376 scif_unregister_driver(&scif_driver
);
377 scif_peer_unregister_driver(&scif_peer_driver
);
378 scif_peer_bus_exit();
382 module_init(scif_init
);
383 module_exit(scif_exit
);
385 MODULE_DEVICE_TABLE(scif
, id_table
);
386 MODULE_AUTHOR("Intel Corporation");
387 MODULE_DESCRIPTION("Intel(R) SCIF driver");
388 MODULE_LICENSE("GPL v2");