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