1 // SPDX-License-Identifier: GPL-2.0-only
3 * Intel MIC Platform Software Stack (MPSS)
5 * Copyright(c) 2014 Intel Corporation.
12 void scif_cleanup_ep_qp(struct scif_endpt *ep)
14 struct scif_qp *qp = ep->qp_info.qp;
16 if (qp->outbound_q.rb_base) {
17 scif_iounmap((void *)qp->outbound_q.rb_base,
18 qp->outbound_q.size, ep->remote_dev);
19 qp->outbound_q.rb_base = NULL;
22 scif_iounmap((void *)qp->remote_qp,
23 sizeof(struct scif_qp), ep->remote_dev);
27 scif_unmap_single(qp->local_qp, ep->remote_dev,
28 sizeof(struct scif_qp));
32 scif_unmap_single(qp->local_buf, ep->remote_dev,
38 void scif_teardown_ep(void *endpt)
40 struct scif_endpt *ep = endpt;
41 struct scif_qp *qp = ep->qp_info.qp;
45 scif_cleanup_ep_qp(ep);
46 spin_unlock(&ep->lock);
47 kfree(qp->inbound_q.rb_base);
53 * Enqueue the endpoint to the zombie list for cleanup.
54 * The endpoint should not be accessed once this API returns.
56 void scif_add_epd_to_zombie_list(struct scif_endpt *ep, bool eplock_held)
59 mutex_lock(&scif_info.eplock);
61 ep->state = SCIFEP_ZOMBIE;
62 spin_unlock(&ep->lock);
63 list_add_tail(&ep->list, &scif_info.zombie);
64 scif_info.nr_zombies++;
66 mutex_unlock(&scif_info.eplock);
67 schedule_work(&scif_info.misc_work);
70 static struct scif_endpt *scif_find_listen_ep(u16 port)
72 struct scif_endpt *ep = NULL;
73 struct list_head *pos, *tmpq;
75 mutex_lock(&scif_info.eplock);
76 list_for_each_safe(pos, tmpq, &scif_info.listen) {
77 ep = list_entry(pos, struct scif_endpt, list);
78 if (ep->port.port == port) {
79 mutex_unlock(&scif_info.eplock);
83 mutex_unlock(&scif_info.eplock);
87 void scif_cleanup_zombie_epd(void)
89 struct list_head *pos, *tmpq;
90 struct scif_endpt *ep;
92 mutex_lock(&scif_info.eplock);
93 list_for_each_safe(pos, tmpq, &scif_info.zombie) {
94 ep = list_entry(pos, struct scif_endpt, list);
95 if (scif_rma_ep_can_uninit(ep)) {
97 scif_info.nr_zombies--;
98 put_iova_domain(&ep->rma_info.iovad);
102 mutex_unlock(&scif_info.eplock);
106 * scif_cnctreq() - Respond to SCIF_CNCT_REQ interrupt message
107 * @scifdev: SCIF device
108 * @msg: Interrupt message
110 * This message is initiated by the remote node to request a connection
111 * to the local node. This function looks for an end point in the
112 * listen state on the requested port id.
114 * If it finds a listening port it places the connect request on the
115 * listening end points queue and wakes up any pending accept calls.
117 * If it does not find a listening end point it sends a connection
118 * reject message to the remote node.
120 void scif_cnctreq(struct scif_dev *scifdev, struct scifmsg *msg)
122 struct scif_endpt *ep = NULL;
123 struct scif_conreq *conreq;
125 conreq = kmalloc(sizeof(*conreq), GFP_KERNEL);
127 /* Lack of resources so reject the request. */
130 ep = scif_find_listen_ep(msg->dst.port);
132 /* Send reject due to no listening ports */
133 goto conreq_sendrej_free;
135 spin_lock(&ep->lock);
137 if (ep->backlog <= ep->conreqcnt) {
138 /* Send reject due to too many pending requests */
139 spin_unlock(&ep->lock);
140 goto conreq_sendrej_free;
144 list_add_tail(&conreq->list, &ep->conlist);
146 wake_up_interruptible(&ep->conwq);
147 spin_unlock(&ep->lock);
153 msg->uop = SCIF_CNCT_REJ;
154 scif_nodeqp_send(&scif_dev[msg->src.node], msg);
158 * scif_cnctgnt() - Respond to SCIF_CNCT_GNT interrupt message
159 * @scifdev: SCIF device
160 * @msg: Interrupt message
162 * An accept() on the remote node has occurred and sent this message
163 * to indicate success. Place the end point in the MAPPING state and
164 * save the remote nodes memory information. Then wake up the connect
165 * request so it can finish.
167 void scif_cnctgnt(struct scif_dev *scifdev, struct scifmsg *msg)
169 struct scif_endpt *ep = (struct scif_endpt *)msg->payload[0];
171 spin_lock(&ep->lock);
172 if (SCIFEP_CONNECTING == ep->state) {
173 ep->peer.node = msg->src.node;
174 ep->peer.port = msg->src.port;
175 ep->qp_info.gnt_pld = msg->payload[1];
176 ep->remote_ep = msg->payload[2];
177 ep->state = SCIFEP_MAPPING;
181 spin_unlock(&ep->lock);
185 * scif_cnctgnt_ack() - Respond to SCIF_CNCT_GNTACK interrupt message
186 * @scifdev: SCIF device
187 * @msg: Interrupt message
189 * The remote connection request has finished mapping the local memory.
190 * Place the connection in the connected state and wake up the pending
193 void scif_cnctgnt_ack(struct scif_dev *scifdev, struct scifmsg *msg)
195 struct scif_endpt *ep = (struct scif_endpt *)msg->payload[0];
197 mutex_lock(&scif_info.connlock);
198 spin_lock(&ep->lock);
199 /* New ep is now connected with all resources set. */
200 ep->state = SCIFEP_CONNECTED;
201 list_add_tail(&ep->list, &scif_info.connected);
203 spin_unlock(&ep->lock);
204 mutex_unlock(&scif_info.connlock);
208 * scif_cnctgnt_nack() - Respond to SCIF_CNCT_GNTNACK interrupt message
209 * @scifdev: SCIF device
210 * @msg: Interrupt message
212 * The remote connection request failed to map the local memory it was sent.
213 * Place the end point in the CLOSING state to indicate it and wake up
214 * the pending accept();
216 void scif_cnctgnt_nack(struct scif_dev *scifdev, struct scifmsg *msg)
218 struct scif_endpt *ep = (struct scif_endpt *)msg->payload[0];
220 spin_lock(&ep->lock);
221 ep->state = SCIFEP_CLOSING;
223 spin_unlock(&ep->lock);
227 * scif_cnctrej() - Respond to SCIF_CNCT_REJ interrupt message
228 * @scifdev: SCIF device
229 * @msg: Interrupt message
231 * The remote end has rejected the connection request. Set the end
232 * point back to the bound state and wake up the pending connect().
234 void scif_cnctrej(struct scif_dev *scifdev, struct scifmsg *msg)
236 struct scif_endpt *ep = (struct scif_endpt *)msg->payload[0];
238 spin_lock(&ep->lock);
239 if (SCIFEP_CONNECTING == ep->state) {
240 ep->state = SCIFEP_BOUND;
243 spin_unlock(&ep->lock);
247 * scif_discnct() - Respond to SCIF_DISCNCT interrupt message
248 * @scifdev: SCIF device
249 * @msg: Interrupt message
251 * The remote node has indicated close() has been called on its end
252 * point. Remove the local end point from the connected list, set its
253 * state to disconnected and ensure accesses to the remote node are
256 * When all accesses to the remote end have completed then send a
257 * DISCNT_ACK to indicate it can remove its resources and complete
260 void scif_discnct(struct scif_dev *scifdev, struct scifmsg *msg)
262 struct scif_endpt *ep = NULL;
263 struct scif_endpt *tmpep;
264 struct list_head *pos, *tmpq;
266 mutex_lock(&scif_info.connlock);
267 list_for_each_safe(pos, tmpq, &scif_info.connected) {
268 tmpep = list_entry(pos, struct scif_endpt, list);
270 * The local ep may have sent a disconnect and and been closed
271 * due to a message response time out. It may have been
272 * allocated again and formed a new connection so we want to
273 * check if the remote ep matches
275 if (((u64)tmpep == msg->payload[1]) &&
276 ((u64)tmpep->remote_ep == msg->payload[0])) {
279 spin_lock(&ep->lock);
285 * If the terminated end is not found then this side started closing
286 * before the other side sent the disconnect. If so the ep will no
287 * longer be on the connected list. Regardless the other side
288 * needs to be acked to let it know close is complete.
291 mutex_unlock(&scif_info.connlock);
295 ep->state = SCIFEP_DISCONNECTED;
296 list_add_tail(&ep->list, &scif_info.disconnected);
298 wake_up_interruptible(&ep->sendwq);
299 wake_up_interruptible(&ep->recvwq);
300 spin_unlock(&ep->lock);
301 mutex_unlock(&scif_info.connlock);
304 msg->uop = SCIF_DISCNT_ACK;
305 scif_nodeqp_send(&scif_dev[msg->src.node], msg);
309 * scif_discnct_ack() - Respond to SCIF_DISCNT_ACK interrupt message
310 * @scifdev: SCIF device
311 * @msg: Interrupt message
313 * Remote side has indicated it has not more references to local resources
315 void scif_discnt_ack(struct scif_dev *scifdev, struct scifmsg *msg)
317 struct scif_endpt *ep = (struct scif_endpt *)msg->payload[0];
319 spin_lock(&ep->lock);
320 ep->state = SCIFEP_DISCONNECTED;
321 spin_unlock(&ep->lock);
322 complete(&ep->discon);
326 * scif_clientsend() - Respond to SCIF_CLIENT_SEND interrupt message
327 * @scifdev: SCIF device
328 * @msg: Interrupt message
330 * Remote side is confirming send or receive interrupt handling is complete.
332 void scif_clientsend(struct scif_dev *scifdev, struct scifmsg *msg)
334 struct scif_endpt *ep = (struct scif_endpt *)msg->payload[0];
336 spin_lock(&ep->lock);
337 if (SCIFEP_CONNECTED == ep->state)
338 wake_up_interruptible(&ep->recvwq);
339 spin_unlock(&ep->lock);
343 * scif_clientrcvd() - Respond to SCIF_CLIENT_RCVD interrupt message
344 * @scifdev: SCIF device
345 * @msg: Interrupt message
347 * Remote side is confirming send or receive interrupt handling is complete.
349 void scif_clientrcvd(struct scif_dev *scifdev, struct scifmsg *msg)
351 struct scif_endpt *ep = (struct scif_endpt *)msg->payload[0];
353 spin_lock(&ep->lock);
354 if (SCIFEP_CONNECTED == ep->state)
355 wake_up_interruptible(&ep->sendwq);
356 spin_unlock(&ep->lock);