ia64/xen-unstable

view xen/drivers/scsi/aacraid/commctrl.c @ 945:db2e1ea917df

bitkeeper revision 1.596.1.3 (3fb3b41eWUoRU0H8A0jEX5roXjxKkA)

Many files:
Greatly simplified Xen softirqs. They are now only executed in outermost Xen activation; they are never called within an irq context.
author kaf24@scramble.cl.cam.ac.uk
date Thu Nov 13 16:41:02 2003 +0000 (2003-11-13)
parents 3946af49a538
children 890460f07ddf
line source
1 /*
2 * Adaptec AAC series RAID controller driver
3 * (c) Copyright 2001 Red Hat Inc. <alan@redhat.com>
4 *
5 * based on the old aacraid driver that is..
6 * Adaptec aacraid device driver for Linux.
7 *
8 * Copyright (c) 2000 Adaptec, Inc. (aacraid@adaptec.com)
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2, or (at your option)
13 * any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; see the file COPYING. If not, write to
22 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 * Module Name:
25 * commctrl.c
26 *
27 * Abstract: Contains all routines for control of the AFA comm layer
28 *
29 */
31 #include <linux/config.h>
32 #include <linux/kernel.h>
33 #include <linux/init.h>
34 #include <linux/types.h>
35 #include <linux/sched.h>
36 #include <linux/pci.h>
37 #include <linux/spinlock.h>
38 #include <linux/slab.h>
39 /*#include <linux/completion.h>*/
40 #include <linux/blk.h>
41 /*#include <asm/semaphore.h>*/
42 #include <asm/uaccess.h>
43 #include "scsi.h"
44 #include "hosts.h"
46 #include "aacraid.h"
48 /**
49 * ioctl_send_fib - send a FIB from userspace
50 * @dev: adapter is being processed
51 * @arg: arguments to the ioctl call
52 *
53 * This routine sends a fib to the adapter on behalf of a user level
54 * program.
55 */
57 static int ioctl_send_fib(struct aac_dev * dev, void *arg)
58 {
59 struct hw_fib * kfib;
60 struct fib *fibptr;
62 fibptr = fib_alloc(dev);
63 if(fibptr == NULL)
64 return -ENOMEM;
66 kfib = fibptr->hw_fib;
67 /*
68 * First copy in the header so that we can check the size field.
69 */
70 if (copy_from_user((void *)kfib, arg, sizeof(struct aac_fibhdr))) {
71 fib_free(fibptr);
72 return -EFAULT;
73 }
74 /*
75 * Since we copy based on the fib header size, make sure that we
76 * will not overrun the buffer when we copy the memory. Return
77 * an error if we would.
78 */
79 if(le32_to_cpu(kfib->header.Size) > sizeof(struct hw_fib) - sizeof(struct aac_fibhdr)) {
80 fib_free(fibptr);
81 return -EINVAL;
82 }
84 if (copy_from_user((void *) kfib, arg, le32_to_cpu(kfib->header.Size) + sizeof(struct aac_fibhdr))) {
85 fib_free(fibptr);
86 return -EFAULT;
87 }
89 if (kfib->header.Command == cpu_to_le32(TakeABreakPt)) {
90 aac_adapter_interrupt(dev);
91 /*
92 * Since we didn't really send a fib, zero out the state to allow
93 * cleanup code not to assert.
94 */
95 kfib->header.XferState = 0;
96 } else {
97 if (fib_send(kfib->header.Command, fibptr, le32_to_cpu(kfib->header.Size) , FsaNormal,
98 1, 1, NULL, NULL) != 0)
99 {
100 fib_free(fibptr);
101 return -EINVAL;
102 }
103 if (fib_complete(fibptr) != 0) {
104 fib_free(fibptr);
105 return -EINVAL;
106 }
107 }
108 /*
109 * Make sure that the size returned by the adapter (which includes
110 * the header) is less than or equal to the size of a fib, so we
111 * don't corrupt application data. Then copy that size to the user
112 * buffer. (Don't try to add the header information again, since it
113 * was already included by the adapter.)
114 */
116 if (copy_to_user(arg, (void *)kfib, kfib->header.Size)) {
117 fib_free(fibptr);
118 return -EFAULT;
119 }
120 fib_free(fibptr);
121 return 0;
122 }
124 /**
125 * open_getadapter_fib - Get the next fib
126 *
127 * This routine will get the next Fib, if available, from the AdapterFibContext
128 * passed in from the user.
129 */
131 static int open_getadapter_fib(struct aac_dev * dev, void *arg)
132 {
133 struct aac_fib_context * fibctx;
134 int status;
135 unsigned long flags;
137 fibctx = kmalloc(sizeof(struct aac_fib_context), GFP_KERNEL);
138 if (fibctx == NULL) {
139 status = -ENOMEM;
140 } else {
141 fibctx->type = FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT;
142 fibctx->size = sizeof(struct aac_fib_context);
143 #if 0
144 /*
145 * Initialize the mutex used to wait for the next AIF.
146 */
147 init_MUTEX_LOCKED(&fibctx->wait_sem);
148 #endif
149 fibctx->wait = 0;
150 /*
151 * Initialize the fibs and set the count of fibs on
152 * the list to 0.
153 */
154 fibctx->count = 0;
155 INIT_LIST_HEAD(&fibctx->fib_list);
156 fibctx->jiffies = jiffies/HZ;
157 /*
158 * Now add this context onto the adapter's
159 * AdapterFibContext list.
160 */
161 spin_lock_irqsave(&dev->fib_lock, flags);
162 list_add_tail(&fibctx->next, &dev->fib_list);
163 spin_unlock_irqrestore(&dev->fib_lock, flags);
164 if (copy_to_user(arg, &fibctx, sizeof(struct aac_fib_context *))) {
165 status = -EFAULT;
166 } else {
167 status = 0;
168 }
169 }
170 return status;
171 }
173 /**
174 * next_getadapter_fib - get the next fib
175 * @dev: adapter to use
176 * @arg: ioctl argument
177 *
178 * This routine will get the next Fib, if available, from the AdapterFibContext
179 * passed in from the user.
180 */
182 static int next_getadapter_fib(struct aac_dev * dev, void *arg)
183 {
184 struct fib_ioctl f;
185 struct aac_fib_context *fibctx, *aifcp;
186 struct fib * fib;
187 int status;
188 struct list_head * entry;
189 int found;
190 unsigned long flags;
192 if(copy_from_user((void *)&f, arg, sizeof(struct fib_ioctl)))
193 return -EFAULT;
194 /*
195 * Extract the AdapterFibContext from the Input parameters.
196 */
197 fibctx = (struct aac_fib_context *) f.fibctx;
199 /*
200 * Verify that the HANDLE passed in was a valid AdapterFibContext
201 *
202 * Search the list of AdapterFibContext addresses on the adapter
203 * to be sure this is a valid address
204 */
205 found = 0;
206 entry = dev->fib_list.next;
208 while(entry != &dev->fib_list) {
209 aifcp = list_entry(entry, struct aac_fib_context, next);
210 if(fibctx == aifcp) { /* We found a winner */
211 found = 1;
212 break;
213 }
214 entry = entry->next;
215 }
216 if (found == 0) {
217 dprintk ((KERN_INFO "Fib not found\n"));
218 return -EINVAL;
219 }
221 if((fibctx->type != FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT) ||
222 (fibctx->size != sizeof(struct aac_fib_context))) {
223 dprintk ((KERN_INFO "Fib Context corrupt?\n"));
224 return -EINVAL;
225 }
226 status = 0;
227 spin_lock_irqsave(&dev->fib_lock, flags);
228 /*
229 * If there are no fibs to send back, then either wait or return
230 * -EAGAIN
231 */
232 return_fib:
233 if (!list_empty(&fibctx->fib_list)) {
234 struct list_head * entry;
235 /*
236 * Pull the next fib from the fibs
237 */
238 entry = fibctx->fib_list.next;
239 list_del(entry);
241 fib = list_entry(entry, struct fib, fiblink);
242 fibctx->count--;
243 spin_unlock_irqrestore(&dev->fib_lock, flags);
244 if (copy_to_user(f.fib, fib->hw_fib, sizeof(struct hw_fib))) {
245 kfree(fib->hw_fib);
246 kfree(fib);
247 return -EFAULT;
248 }
249 /*
250 * Free the space occupied by this copy of the fib.
251 */
252 kfree(fib->hw_fib);
253 kfree(fib);
254 status = 0;
255 } else {
256 spin_unlock_irqrestore(&dev->fib_lock, flags);
257 if (f.wait) {
258 #if 0
259 if(down_interruptible(&fibctx->wait_sem) < 0) {
260 status = -EINTR;
261 } else {
262 #else
263 {
264 #endif
265 /* Lock again and retry */
266 spin_lock_irqsave(&dev->fib_lock, flags);
267 goto return_fib;
268 }
269 } else {
270 status = -EAGAIN;
271 }
272 }
273 fibctx->jiffies = jiffies/HZ;
274 return status;
275 }
277 int aac_close_fib_context(struct aac_dev * dev, struct aac_fib_context * fibctx)
278 {
279 struct fib *fib;
281 /*
282 * First free any FIBs that have not been consumed.
283 */
284 while (!list_empty(&fibctx->fib_list)) {
285 struct list_head * entry;
286 /*
287 * Pull the next fib from the fibs
288 */
289 entry = fibctx->fib_list.next;
290 list_del(entry);
291 fib = list_entry(entry, struct fib, fiblink);
292 fibctx->count--;
293 /*
294 * Free the space occupied by this copy of the fib.
295 */
296 kfree(fib->hw_fib);
297 kfree(fib);
298 }
299 /*
300 * Remove the Context from the AdapterFibContext List
301 */
302 list_del(&fibctx->next);
303 /*
304 * Invalidate context
305 */
306 fibctx->type = 0;
307 /*
308 * Free the space occupied by the Context
309 */
310 kfree(fibctx);
311 return 0;
312 }
314 /**
315 * close_getadapter_fib - close down user fib context
316 * @dev: adapter
317 * @arg: ioctl arguments
318 *
319 * This routine will close down the fibctx passed in from the user.
320 */
322 static int close_getadapter_fib(struct aac_dev * dev, void *arg)
323 {
324 struct aac_fib_context *fibctx, *aifcp;
325 int status;
326 unsigned long flags;
327 struct list_head * entry;
328 int found;
330 /*
331 * Extract the fibctx from the input parameters
332 */
333 fibctx = arg;
335 /*
336 * Verify that the HANDLE passed in was a valid AdapterFibContext
337 *
338 * Search the list of AdapterFibContext addresses on the adapter
339 * to be sure this is a valid address
340 */
342 found = 0;
343 entry = dev->fib_list.next;
345 while(entry != &dev->fib_list) {
346 aifcp = list_entry(entry, struct aac_fib_context, next);
347 if(fibctx == aifcp) { /* We found a winner */
348 found = 1;
349 break;
350 }
351 entry = entry->next;
352 }
354 if(found == 0)
355 return 0; /* Already gone */
357 if((fibctx->type != FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT) ||
358 (fibctx->size != sizeof(struct aac_fib_context)))
359 return -EINVAL;
360 spin_lock_irqsave(&dev->fib_lock, flags);
361 status = aac_close_fib_context(dev, fibctx);
362 spin_unlock_irqrestore(&dev->fib_lock, flags);
363 return status;
364 }
366 /**
367 * check_revision - close down user fib context
368 * @dev: adapter
369 * @arg: ioctl arguments
370 *
371 * This routine returns the firmware version.
372 * Under Linux, there have been no version incompatibilities, so this is simple!
373 */
375 static int check_revision(struct aac_dev *dev, void *arg)
376 {
377 struct revision response;
379 response.compat = 1;
380 response.version = dev->adapter_info.kernelrev;
381 response.build = dev->adapter_info.kernelbuild;
383 if (copy_to_user(arg, &response, sizeof(response)))
384 return -EFAULT;
385 return 0;
386 }
389 struct aac_pci_info {
390 u32 bus;
391 u32 slot;
392 };
395 int aac_get_pci_info(struct aac_dev* dev, void* arg)
396 {
397 struct aac_pci_info pci_info;
399 pci_info.bus = dev->pdev->bus->number;
400 pci_info.slot = PCI_SLOT(dev->pdev->devfn);
402 if(copy_to_user( arg, (void*)&pci_info, sizeof(struct aac_pci_info)))
403 return -EFAULT;
404 return 0;
405 }
408 int aac_do_ioctl(struct aac_dev * dev, int cmd, void *arg)
409 {
410 int status;
412 /*
413 * HBA gets first crack
414 */
416 status = aac_dev_ioctl(dev, cmd, arg);
417 if(status != -ENOTTY)
418 return status;
420 switch (cmd) {
421 case FSACTL_MINIPORT_REV_CHECK:
422 status = check_revision(dev, arg);
423 break;
424 case FSACTL_SENDFIB:
425 status = ioctl_send_fib(dev, arg);
426 break;
427 case FSACTL_OPEN_GET_ADAPTER_FIB:
428 status = open_getadapter_fib(dev, arg);
429 break;
430 case FSACTL_GET_NEXT_ADAPTER_FIB:
431 status = next_getadapter_fib(dev, arg);
432 break;
433 case FSACTL_CLOSE_GET_ADAPTER_FIB:
434 status = close_getadapter_fib(dev, arg);
435 break;
436 case FSACTL_GET_PCI_INFO:
437 status = aac_get_pci_info(dev,arg);
438 break;
439 default:
440 status = -ENOTTY;
441 break;
442 }
443 return status;
444 }