Whamcloud - gitweb
- name switch: "light" --> "lite"
[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 EXPORT_SYMTAB
24
25 #include <linux/config.h>
26 #include <linux/module.h>
27 #include <linux/kernel.h>
28
29 #define DEBUG_SUBSYSTEM S_RPC
30
31 #include <linux/obd_support.h>
32 #include <linux/obd_class.h>
33 #include <linux/lustre_net.h>
34
35 ptl_handle_eq_t request_out_eq, 
36                 reply_in_eq, 
37                 reply_out_eq,
38                 bulk_source_eq, 
39                 bulk_sink_eq;
40 static const ptl_handle_ni_t *socknal_nip = NULL, *qswnal_nip = NULL;
41
42 /*
43  *  Free the packet when it has gone out
44  */
45 static int request_out_callback(ptl_event_t *ev, void *data)
46 {
47         struct ptlrpc_request *req = ev->mem_desc.user_ptr;
48         struct ptlrpc_client *cl = req->rq_client; 
49         
50         ENTRY;
51
52         if (ev->type == PTL_EVENT_SENT) {
53                 list_del(&req->rq_list);
54                 list_add(&req->rq_list, &cl->cli_sent_head);
55         } else { 
56                 // XXX make sure we understand all events, including ACK's
57                 CERROR("Unknown event %d\n", ev->type); 
58                 LBUG();
59         }
60
61         RETURN(1);
62 }
63
64
65 /*
66  *  Free the packet when it has gone out
67  */
68 static int reply_out_callback(ptl_event_t *ev, void *data)
69 {
70         ENTRY;
71
72         if (ev->type == PTL_EVENT_SENT) {
73                 OBD_FREE(ev->mem_desc.start, ev->mem_desc.length);
74         } else { 
75                 // XXX make sure we understand all events, including ACK's
76                 CERROR("Unknown event %d\n", ev->type); 
77                 LBUG();
78         }
79
80         RETURN(1);
81 }
82
83 /*
84  * Wake up the thread waiting for the reply once it comes in.
85  */
86 static int reply_in_callback(ptl_event_t *ev, void *data)
87 {
88         struct ptlrpc_request *rpc = ev->mem_desc.user_ptr;
89         ENTRY;
90
91         if (ev->type == PTL_EVENT_PUT) {
92                 rpc->rq_repbuf = ev->mem_desc.start + ev->offset;
93                 barrier();
94                 wake_up_interruptible(&rpc->rq_wait_for_rep);
95         } else { 
96                 // XXX make sure we understand all events, including ACK's
97                 CERROR("Unknown event %d\n", ev->type); 
98                 LBUG();
99         }
100
101         RETURN(1);
102 }
103
104 int request_in_callback(ptl_event_t *ev, void *data)
105 {
106         struct ptlrpc_service *service = data;
107         int index;
108
109         if (ev->rlength != ev->mlength)
110                 CERROR("Warning: Possibly truncated rpc (%d/%d)\n",
111                        ev->mlength, ev->rlength);
112
113         spin_lock(&service->srv_lock); 
114         for (index = 0; index < service->srv_ring_length; index++)
115                 if ( service->srv_buf[index] == ev->mem_desc.start) 
116                         break;
117
118         if (index == service->srv_ring_length)
119                 LBUG();
120
121         service->srv_ref_count[index]++;
122
123         if (ptl_is_valid_handle(&ev->unlinked_me)) {
124                 int idx;
125
126                 for (idx = 0; idx < service->srv_ring_length; idx++)
127                         if (service->srv_me_h[idx].handle_idx ==
128                             ev->unlinked_me.handle_idx)
129                                 break;
130                 if (idx == service->srv_ring_length)
131                         LBUG();
132
133                 CDEBUG(D_NET, "unlinked %d\n", idx);
134                 ptl_set_inv_handle(&(service->srv_me_h[idx]));
135
136                 if (service->srv_ref_count[idx] == 0)
137                         ptlrpc_link_svc_me(service, idx); 
138         }
139
140         spin_unlock(&service->srv_lock); 
141         if (ev->type == PTL_EVENT_PUT)
142                 wake_up(&service->srv_waitq);
143         else
144                 CERROR("Unexpected event type: %d\n", ev->type);
145
146         return 0;
147 }
148
149 static int bulk_source_callback(ptl_event_t *ev, void *data)
150 {
151         struct ptlrpc_bulk_desc *bulk = ev->mem_desc.user_ptr;
152
153         ENTRY;
154
155         if (ev->type == PTL_EVENT_SENT) {
156                 CDEBUG(D_NET, "got SENT event\n");
157         } else if (ev->type == PTL_EVENT_ACK) {
158                 CDEBUG(D_NET, "got ACK event\n");
159                 bulk->b_flags = PTL_BULK_SENT;
160                 wake_up_interruptible(&bulk->b_waitq);
161         } else {
162                 CERROR("Unexpected event type!\n");
163                 LBUG();
164         }
165
166         EXIT;
167         return 1;
168 }
169
170 static int bulk_sink_callback(ptl_event_t *ev, void *data)
171 {
172         struct ptlrpc_bulk_desc *bulk = ev->mem_desc.user_ptr;
173
174         ENTRY;
175
176         if (ev->type == PTL_EVENT_PUT) {
177                 if (bulk->b_buf != ev->mem_desc.start + ev->offset)
178                         CERROR("bulkbuf != mem_desc -- why?\n");
179                 bulk->b_flags = PTL_BULK_RCVD;
180                 if (bulk->b_cb != NULL)
181                         bulk->b_cb(bulk, data);
182                 wake_up_interruptible(&bulk->b_waitq);
183         } else {
184                 CERROR("Unexpected event type!\n");
185                 LBUG();
186         }
187
188         /* FIXME: This should happen unconditionally */
189         if (bulk->b_cb != NULL)
190                 OBD_FREE(bulk, sizeof(*bulk));
191
192         EXIT;
193         return 1;
194 }
195
196 int ptlrpc_init_portals(void)
197 {
198         int rc;
199         ptl_handle_ni_t ni;
200
201         socknal_nip = inter_module_get_request("ksocknal_ni", "ksocknal");
202         qswnal_nip = inter_module_get_request("kqswnal_ni", "kqswnal");
203         if (socknal_nip == NULL && qswnal_nip == NULL) {
204                 CERROR("get_ni failed: is a NAL module loaded?\n");
205                 return -EIO;
206         }
207
208         /* Use the qswnal if it's there */
209         if (qswnal_nip != NULL)
210                 ni = *qswnal_nip;
211         else
212                 ni = *socknal_nip;
213
214         rc = PtlEQAlloc(ni, 128, request_out_callback, NULL, &request_out_eq);
215         if (rc != PTL_OK)
216                 CERROR("PtlEQAlloc failed: %d\n", rc);
217
218         rc = PtlEQAlloc(ni, 128, reply_out_callback, NULL, &reply_out_eq);
219         if (rc != PTL_OK)
220                 CERROR("PtlEQAlloc failed: %d\n", rc);
221
222         rc = PtlEQAlloc(ni, 128, reply_in_callback, NULL, &reply_in_eq);
223         if (rc != PTL_OK)
224                 CERROR("PtlEQAlloc failed: %d\n", rc);
225
226         rc = PtlEQAlloc(ni, 128, bulk_source_callback, NULL, &bulk_source_eq);
227         if (rc != PTL_OK)
228                 CERROR("PtlEQAlloc failed: %d\n", rc);
229
230         rc = PtlEQAlloc(ni, 128, bulk_sink_callback, NULL, &bulk_sink_eq);
231         if (rc != PTL_OK)
232                 CERROR("PtlEQAlloc failed: %d\n", rc);
233
234         return rc;
235 }
236
237 void ptlrpc_exit_portals(void)
238 {
239         PtlEQFree(request_out_eq);
240         PtlEQFree(reply_out_eq);
241         PtlEQFree(reply_in_eq);
242         PtlEQFree(bulk_source_eq);
243         PtlEQFree(bulk_sink_eq);
244
245         if (qswnal_nip != NULL)
246                 inter_module_put("kqswnal_ni");
247         if (socknal_nip != NULL)
248                 inter_module_put("ksocknal_ni");
249 }