Whamcloud - gitweb
WARNING: This commit breaks everything. It will be back in shape within 12
[fs/lustre-release.git] / lustre / ptlrpc / events.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2002 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22
23 #define DEBUG_SUBSYSTEM S_RPC
24
25 #include <linux/module.h>
26 #include <linux/lustre_net.h>
27
28 ptl_handle_eq_t request_out_eq, reply_in_eq, reply_out_eq, bulk_source_eq,
29         bulk_sink_eq;
30 static const ptl_handle_ni_t *socknal_nip = NULL, *qswnal_nip = NULL;
31
32 /*
33  *  Free the packet when it has gone out
34  */
35 static int request_out_callback(ptl_event_t *ev)
36 {
37         ENTRY;
38
39         if (ev->type != PTL_EVENT_SENT) {
40                 // XXX make sure we understand all events, including ACK's
41                 CERROR("Unknown event %d\n", ev->type);
42                 LBUG();
43         }
44
45         RETURN(1);
46 }
47
48
49 /*
50  *  Free the packet when it has gone out
51  */
52 static int reply_out_callback(ptl_event_t *ev)
53 {
54         ENTRY;
55
56         if (ev->type == PTL_EVENT_SENT) {
57                 OBD_FREE(ev->mem_desc.start, ev->mem_desc.length);
58         } else {
59                 // XXX make sure we understand all events, including ACK's
60                 CERROR("Unknown event %d\n", ev->type);
61                 LBUG();
62         }
63
64         RETURN(1);
65 }
66
67 /*
68  * Wake up the thread waiting for the reply once it comes in.
69  */
70 static int reply_in_callback(ptl_event_t *ev)
71 {
72         struct ptlrpc_request *rpc = ev->mem_desc.user_ptr;
73         ENTRY;
74
75         if (ev->type == PTL_EVENT_PUT) {
76                 rpc->rq_repmsg = ev->mem_desc.start + ev->offset;
77                 barrier();
78                 wake_up_interruptible(&rpc->rq_wait_for_rep);
79         } else {
80                 // XXX make sure we understand all events, including ACK's
81                 CERROR("Unknown event %d\n", ev->type);
82                 LBUG();
83         }
84
85         RETURN(1);
86 }
87
88 int request_in_callback(ptl_event_t *ev)
89 {
90         struct ptlrpc_service *service = ev->mem_desc.user_ptr;
91         int index;
92
93         if (ev->rlength != ev->mlength)
94                 CERROR("Warning: Possibly truncated rpc (%d/%d)\n",
95                        ev->mlength, ev->rlength);
96
97         spin_lock(&service->srv_lock);
98         for (index = 0; index < service->srv_ring_length; index++)
99                 if ( service->srv_buf[index] == ev->mem_desc.start)
100                         break;
101
102         if (index == service->srv_ring_length)
103                 LBUG();
104
105         service->srv_ref_count[index]++;
106
107         if (ptl_is_valid_handle(&ev->unlinked_me)) {
108                 int idx;
109
110                 for (idx = 0; idx < service->srv_ring_length; idx++)
111                         if (service->srv_me_h[idx].handle_idx ==
112                             ev->unlinked_me.handle_idx)
113                                 break;
114                 if (idx == service->srv_ring_length)
115                         LBUG();
116
117                 CDEBUG(D_NET, "unlinked %d\n", idx);
118                 ptl_set_inv_handle(&(service->srv_me_h[idx]));
119
120                 if (service->srv_ref_count[idx] == 0)
121                         ptlrpc_link_svc_me(service, idx);
122         }
123
124         spin_unlock(&service->srv_lock);
125         if (ev->type == PTL_EVENT_PUT)
126                 wake_up(&service->srv_waitq);
127         else
128                 CERROR("Unexpected event type: %d\n", ev->type);
129
130         return 0;
131 }
132
133 static int bulk_source_callback(ptl_event_t *ev)
134 {
135         struct ptlrpc_bulk_page *bulk = ev->mem_desc.user_ptr;
136         struct ptlrpc_bulk_desc *desc = bulk->b_desc;
137         ENTRY;
138
139         if (ev->type == PTL_EVENT_SENT) {
140                 CDEBUG(D_NET, "got SENT event\n");
141         } else if (ev->type == PTL_EVENT_ACK) {
142                 CDEBUG(D_NET, "got ACK event\n");
143                 if (bulk->b_cb != NULL)
144                         bulk->b_cb(bulk);
145                 if (atomic_dec_and_test(&desc->b_finished_count)) {
146                         desc->b_flags |= PTL_BULK_FL_SENT;
147                         wake_up_interruptible(&desc->b_waitq);
148                         if (desc->b_cb != NULL)
149                                 desc->b_cb(desc, desc->b_cb_data);
150                 }
151         } else {
152                 CERROR("Unexpected event type!\n");
153                 LBUG();
154         }
155
156         RETURN(1);
157 }
158
159 static int bulk_sink_callback(ptl_event_t *ev)
160 {
161         struct ptlrpc_bulk_page *bulk = ev->mem_desc.user_ptr;
162         struct ptlrpc_bulk_desc *desc = bulk->b_desc;
163         ENTRY;
164
165         if (ev->type == PTL_EVENT_PUT) {
166                 if (bulk->b_buf != ev->mem_desc.start + ev->offset)
167                         CERROR("bulkbuf != mem_desc -- why?\n");
168                 if (bulk->b_cb != NULL)
169                         bulk->b_cb(bulk);
170                 if (atomic_dec_and_test(&desc->b_finished_count)) {
171                         desc->b_flags |= PTL_BULK_FL_RCVD;
172                         wake_up_interruptible(&desc->b_waitq);
173                         if (desc->b_cb != NULL)
174                                 desc->b_cb(desc, desc->b_cb_data);
175                 }
176         } else {
177                 CERROR("Unexpected event type!\n");
178                 LBUG();
179         }
180
181         RETURN(1);
182 }
183
184 int ptlrpc_init_portals(void)
185 {
186         int rc;
187         ptl_handle_ni_t ni;
188
189         socknal_nip = inter_module_get_request("ksocknal_ni", "ksocknal");
190         qswnal_nip = inter_module_get_request("kqswnal_ni", "kqswnal");
191         if (socknal_nip == NULL && qswnal_nip == NULL) {
192                 CERROR("get_ni failed: is a NAL module loaded?\n");
193                 return -EIO;
194         }
195
196         /* Use the qswnal if it's there */
197         if (qswnal_nip != NULL)
198                 ni = *qswnal_nip;
199         else
200                 ni = *socknal_nip;
201
202         rc = PtlEQAlloc(ni, 128, request_out_callback, &request_out_eq);
203         if (rc != PTL_OK)
204                 CERROR("PtlEQAlloc failed: %d\n", rc);
205
206         rc = PtlEQAlloc(ni, 128, reply_out_callback, &reply_out_eq);
207         if (rc != PTL_OK)
208                 CERROR("PtlEQAlloc failed: %d\n", rc);
209
210         rc = PtlEQAlloc(ni, 128, reply_in_callback, &reply_in_eq);
211         if (rc != PTL_OK)
212                 CERROR("PtlEQAlloc failed: %d\n", rc);
213
214         rc = PtlEQAlloc(ni, 128, bulk_source_callback, &bulk_source_eq);
215         if (rc != PTL_OK)
216                 CERROR("PtlEQAlloc failed: %d\n", rc);
217
218         rc = PtlEQAlloc(ni, 128, bulk_sink_callback, &bulk_sink_eq);
219         if (rc != PTL_OK)
220                 CERROR("PtlEQAlloc failed: %d\n", rc);
221
222         return rc;
223 }
224
225 void ptlrpc_exit_portals(void)
226 {
227         PtlEQFree(request_out_eq);
228         PtlEQFree(reply_out_eq);
229         PtlEQFree(reply_in_eq);
230         PtlEQFree(bulk_source_eq);
231         PtlEQFree(bulk_sink_eq);
232
233         if (qswnal_nip != NULL)
234                 inter_module_put("kqswnal_ni");
235         if (socknal_nip != NULL)
236                 inter_module_put("ksocknal_ni");
237 }