~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/drivers/i2o/i2o_block.c

Version: ~ [ 2.4.0 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * I2O Random Block Storage Class OSM
  3  *
  4  * (C) Copyright 1999 Red Hat Software
  5  *      
  6  * Written by Alan Cox, Building Number Three Ltd
  7  *
  8  * This program is free software; you can redistribute it and/or
  9  * modify it under the terms of the GNU General Public License
 10  * as published by the Free Software Foundation; either version
 11  * 2 of the License, or (at your option) any later version.
 12  *
 13  * This is a beta test release. Most of the good code was taken
 14  * from the nbd driver by Pavel Machek, who in turn took some of it
 15  * from loop.c. Isn't free software great for reusability 8)
 16  *
 17  * Fixes/additions:
 18  *      Steve Ralston:  
 19  *              Multiple device handling error fixes,
 20  *              Added a queue depth.
 21  *      Alan Cox:       
 22  *              FC920 has an rmw bug. Dont or in the end marker.
 23  *              Removed queue walk, fixed for 64bitness.
 24  *      Deepak Saxena:
 25  *              Independent queues per IOP
 26  *              Support for dynamic device creation/deletion
 27  *              Code cleanup    
 28  *              Support for larger I/Os through merge* functions 
 29  *              (taken from DAC960 driver)
 30  *      Boji T Kannanthanam:
 31  *              Reduced the timeout during RAID 5 creation. 
 32  *              This is to prevent race condition when a RAID volume
 33  *              is created and immediately deleted.
 34  *
 35  *      To do:
 36  *              Serial number scanning to find duplicates for FC multipathing
 37  *              Remove the random timeout in the code needed for RAID 5
 38  *                      volume creation.
 39  */
 40 
 41 #include <linux/major.h>
 42 
 43 #include <linux/module.h>
 44 
 45 #include <linux/sched.h>
 46 #include <linux/fs.h>
 47 #include <linux/stat.h>
 48 #include <linux/errno.h>
 49 #include <linux/file.h>
 50 #include <linux/ioctl.h>
 51 #include <linux/i2o.h>
 52 #include <linux/blkdev.h>
 53 #include <linux/blkpg.h>
 54 #include <linux/malloc.h>
 55 #include <linux/hdreg.h>
 56 
 57 #include <linux/notifier.h>
 58 #include <linux/reboot.h>
 59 
 60 #include <asm/uaccess.h>
 61 #include <asm/semaphore.h>
 62 #include <asm/io.h>
 63 #include <asm/atomic.h>
 64 #include <linux/smp_lock.h>
 65 #include <linux/wait.h>
 66 
 67 #define MAJOR_NR I2O_MAJOR
 68 
 69 #include <linux/blk.h>
 70 
 71 #define MAX_I2OB        16
 72 
 73 #define MAX_I2OB_DEPTH  128
 74 #define MAX_I2OB_RETRIES 4
 75 
 76 //#define DRIVERDEBUG
 77 #ifdef DRIVERDEBUG
 78 #define DEBUG( s )
 79 #else
 80 #define DEBUG( s ) printk( s ) 
 81 #endif
 82 
 83 /*
 84  * Events that this OSM is interested in
 85  */
 86 #define I2OB_EVENT_MASK         (I2O_EVT_IND_BSA_VOLUME_LOAD |  \
 87                                  I2O_EVT_IND_BSA_VOLUME_UNLOAD | \
 88                                  I2O_EVT_IND_BSA_VOLUME_UNLOAD_REQ | \
 89                                  I2O_EVT_IND_BSA_CAPACITY_CHANGE)
 90 
 91 
 92 /*
 93  * I2O Block Error Codes - should be in a header file really...
 94  */
 95 #define I2O_BSA_DSC_SUCCESS             0x0000
 96 #define I2O_BSA_DSC_MEDIA_ERROR         0x0001
 97 #define I2O_BSA_DSC_ACCESS_ERROR        0x0002
 98 #define I2O_BSA_DSC_DEVICE_FAILURE      0x0003
 99 #define I2O_BSA_DSC_DEVICE_NOT_READY    0x0004
100 #define I2O_BSA_DSC_MEDIA_NOT_PRESENT   0x0005
101 #define I2O_BSA_DSC_MEDIA_LOCKED        0x0006
102 #define I2O_BSA_DSC_MEDIA_FAILURE       0x0007
103 #define I2O_BSA_DSC_PROTOCOL_FAILURE    0x0008
104 #define I2O_BSA_DSC_BUS_FAILURE         0x0009
105 #define I2O_BSA_DSC_ACCESS_VIOLATION    0x000A
106 #define I2O_BSA_DSC_WRITE_PROTECTED     0x000B
107 #define I2O_BSA_DSC_DEVICE_RESET        0x000C
108 #define I2O_BSA_DSC_VOLUME_CHANGED      0x000D
109 #define I2O_BSA_DSC_TIMEOUT             0x000E
110 
111 /*
112  *      Some of these can be made smaller later
113  */
114 
115 static int i2ob_blksizes[MAX_I2OB<<4];
116 static int i2ob_hardsizes[MAX_I2OB<<4];
117 static int i2ob_sizes[MAX_I2OB<<4];
118 static int i2ob_media_change_flag[MAX_I2OB];
119 static u32 i2ob_max_sectors[MAX_I2OB<<4];
120 
121 static int i2ob_context;
122 
123 /*
124  * I2O Block device descriptor 
125  */
126 struct i2ob_device
127 {
128         struct i2o_controller *controller;
129         struct i2o_device *i2odev;
130         int unit;
131         int tid;
132         int flags;
133         int refcnt;
134         struct request *head, *tail;
135         request_queue_t *req_queue;
136         int max_segments;
137         int done_flag;
138 };
139 
140 /*
141  *      FIXME:
142  *      We should cache align these to avoid ping-ponging lines on SMP
143  *      boxes under heavy I/O load...
144  */
145 struct i2ob_request
146 {
147         struct i2ob_request *next;
148         struct request *req;
149         int num;
150 };
151 
152 /*
153  * Per IOP requst queue information
154  *
155  * We have a separate requeust_queue_t per IOP so that a heavilly
156  * loaded I2O block device on an IOP does not starve block devices
157  * across all I2O controllers.
158  * 
159  */
160 struct i2ob_iop_queue
161 {
162         atomic_t queue_depth;
163         struct i2ob_request request_queue[MAX_I2OB_DEPTH];
164         struct i2ob_request *i2ob_qhead;
165         request_queue_t req_queue;
166 };
167 static struct i2ob_iop_queue *i2ob_queues[MAX_I2O_CONTROLLERS] = {NULL};
168 
169 /*
170  *      Each I2O disk is one of these.
171  */
172 
173 static struct i2ob_device i2ob_dev[MAX_I2OB<<4];
174 static int i2ob_dev_count = 0;
175 static struct hd_struct i2ob[MAX_I2OB<<4];
176 static struct gendisk i2ob_gendisk;     /* Declared later */
177 
178 /*
179  * Mutex and spin lock for event handling synchronization
180  * evt_msg contains the last event.
181  */
182 DECLARE_MUTEX(i2ob_evt_sem);
183 static spinlock_t i2ob_evt_lock = SPIN_LOCK_UNLOCKED;
184 static unsigned int evt_msg[MSG_FRAME_SIZE>>2];
185 DECLARE_WAIT_QUEUE_HEAD(i2ob_evt_wait);
186 
187 static struct timer_list i2ob_timer;
188 static int i2ob_timer_started = 0;
189 
190 static void i2o_block_reply(struct i2o_handler *, struct i2o_controller *,
191          struct i2o_message *);
192 static void i2ob_new_device(struct i2o_controller *, struct i2o_device *);
193 static void i2ob_del_device(struct i2o_controller *, struct i2o_device *);
194 static void i2ob_reboot_event(void);
195 static int i2ob_install_device(struct i2o_controller *, struct i2o_device *, int);
196 static void i2ob_end_request(struct request *);
197 static void i2ob_request(request_queue_t *);
198 static int i2ob_init_iop(unsigned int);
199 static request_queue_t* i2ob_get_queue(kdev_t);
200 static int i2ob_query_device(struct i2ob_device *, int, int, void*, int);
201 static int do_i2ob_revalidate(kdev_t, int);
202 static int i2ob_evt(void *);
203 
204 static int evt_pid = 0;
205 static int evt_running = 0;
206 
207 /*
208  * I2O OSM registration structure...keeps getting bigger and bigger :)
209  */
210 static struct i2o_handler i2o_block_handler =
211 {
212         i2o_block_reply,
213         i2ob_new_device,
214         i2ob_del_device,
215         i2ob_reboot_event,
216         "I2O Block OSM",
217         0,
218         I2O_CLASS_RANDOM_BLOCK_STORAGE
219 };
220 
221 /*
222  *      Get a message
223  */
224 
225 static u32 i2ob_get(struct i2ob_device *dev)
226 {
227         struct i2o_controller *c=dev->controller;
228         return I2O_POST_READ32(c);
229 }
230  
231 /*
232  *      Turn a Linux block request into an I2O block read/write.
233  */
234 
235 static int i2ob_send(u32 m, struct i2ob_device *dev, struct i2ob_request *ireq, u32 base, int unit)
236 {
237         struct i2o_controller *c = dev->controller;
238         int tid = dev->tid;
239         unsigned long msg;
240         unsigned long mptr;
241         u64 offset;
242         struct request *req = ireq->req;
243         struct buffer_head *bh = req->bh;
244         int count = req->nr_sectors<<9;
245         char *last = NULL;
246         unsigned short size = 0;
247 
248         // printk(KERN_INFO "i2ob_send called\n");
249         /* Map the message to a virtual address */
250         msg = c->mem_offset + m;
251         
252         /*
253          * Build the message based on the request.
254          */
255         __raw_writel(i2ob_context|(unit<<8), msg+8);
256         __raw_writel(ireq->num, msg+12);
257         __raw_writel(req->nr_sectors << 9, msg+20);
258         
259         /* This can be optimised later - just want to be sure its right for
260            starters */
261         offset = ((u64)(req->sector+base)) << 9;
262         __raw_writel( offset & 0xFFFFFFFF, msg+24);
263         __raw_writel(offset>>32, msg+28);
264         mptr=msg+32;
265         
266         if(req->cmd == READ)
267         {
268                 __raw_writel(I2O_CMD_BLOCK_READ<<24|HOST_TID<<12|tid, msg+4);
269                 /* We don't yet do cache/readahead and other magic */
270                 __raw_writel(1<<16, msg+16);
271                 while(bh!=NULL)
272                 {
273                         if(bh->b_data == last) {
274                                 size += bh->b_size;
275                                 last += bh->b_size;
276                                 if(bh->b_reqnext)
277                                         __raw_writel(0x14000000|(size), mptr-8);
278                                 else
279                                         __raw_writel(0xD4000000|(size), mptr-8);
280                         }
281                         else
282                         {
283                                 if(bh->b_reqnext)
284                                         __raw_writel(0x10000000|(bh->b_size), mptr);
285                                 else
286                                         __raw_writel(0xD0000000|(bh->b_size), mptr);
287                                 __raw_writel(virt_to_bus(bh->b_data), mptr+4);
288                                 mptr += 8;      
289                                 size = bh->b_size;
290                                 last = bh->b_data + size;
291                         }
292 
293                         count -= bh->b_size;
294                         bh = bh->b_reqnext;
295                 }
296         }
297         else if(req->cmd == WRITE)
298         {
299                 __raw_writel(I2O_CMD_BLOCK_WRITE<<24|HOST_TID<<12|tid, msg+4);
300                 /* 
301                  * Allow replies to come back once data is cached in the controller
302                  * This allows us to handle writes quickly thus giving more of the
303                  * queue to reads.
304                  */
305                 __raw_writel(0x00000010, msg+16);
306                 while(bh!=NULL)
307                 {
308                         if(bh->b_data == last) {
309                                 size += bh->b_size;
310                                 last += bh->b_size;
311                                 if(bh->b_reqnext)
312                                         __raw_writel(0x14000000|(size), mptr-8);
313                                 else
314                                         __raw_writel(0xD4000000|(size), mptr-8);
315                         }
316                         else
317                         {
318                                 if(bh->b_reqnext)
319                                         __raw_writel(0x14000000|(bh->b_size), mptr);
320                                 else
321                                         __raw_writel(0xD4000000|(bh->b_size), mptr);
322                                 __raw_writel(virt_to_bus(bh->b_data), mptr+4);
323                                 mptr += 8;      
324                                 size = bh->b_size;
325                                 last = bh->b_data + size;
326                         }
327 
328                         count -= bh->b_size;
329                         bh = bh->b_reqnext;
330                 }
331         }
332         __raw_writel(I2O_MESSAGE_SIZE(mptr-msg)>>2 | SGL_OFFSET_8, msg);
333         
334         if(req->current_nr_sectors > i2ob_max_sectors[unit])
335                 printk("Gathered sectors %ld.\n", 
336                         req->current_nr_sectors);
337                         
338         if(count != 0)
339         {
340                 printk(KERN_ERR "Request count botched by %d.\n", count);
341         }
342 
343         i2o_post_message(c,m);
344         atomic_inc(&i2ob_queues[c->unit]->queue_depth);
345 
346         return 0;
347 }
348 
349 /*
350  *      Remove a request from the _locked_ request list. We update both the
351  *      list chain and if this is the last item the tail pointer. Caller
352  *      must hold the lock.
353  */
354  
355 static inline void i2ob_unhook_request(struct i2ob_request *ireq, 
356         unsigned int iop)
357 {
358         ireq->next = i2ob_queues[iop]->i2ob_qhead;
359         i2ob_queues[iop]->i2ob_qhead = ireq;
360 }
361 
362 /*
363  *      Request completion handler
364  */
365  
366 static inline void i2ob_end_request(struct request *req)
367 {
368         /*
369          * Loop until all of the buffers that are linked
370          * to this request have been marked updated and
371          * unlocked.
372          */
373 
374         while (end_that_request_first( req, !req->errors, "i2o block" ));
375 
376         /*
377          * It is now ok to complete the request.
378          */
379         end_that_request_last( req );
380 }
381 
382 /*
383  * Request merging functions
384  */
385 static inline int i2ob_new_segment(request_queue_t *q, struct request *req,
386                                   int __max_segments)
387 {
388         int max_segments = i2ob_dev[MINOR(req->rq_dev)].max_segments;
389 
390         if (__max_segments < max_segments)
391                 max_segments = __max_segments;
392 
393         if (req->nr_segments < max_segments) {
394                 req->nr_segments++;
395                 q->elevator.nr_segments++;
396                 return 1;
397         }
398         return 0;
399 }
400 
401 static int i2ob_back_merge(request_queue_t *q, struct request *req, 
402                              struct buffer_head *bh, int __max_segments)
403 {
404         if (req->bhtail->b_data + req->bhtail->b_size == bh->b_data)
405                 return 1;
406         return i2ob_new_segment(q, req, __max_segments);
407 }
408 
409 static int i2ob_front_merge(request_queue_t *q, struct request *req, 
410                               struct buffer_head *bh, int __max_segments)
411 {
412         if (bh->b_data + bh->b_size == req->bh->b_data)
413                 return 1;
414         return i2ob_new_segment(q, req, __max_segments);
415 }
416 
417 static int i2ob_merge_requests(request_queue_t *q,
418                                 struct request *req,
419                                 struct request *next,
420                                 int __max_segments)
421 {
422         int max_segments = i2ob_dev[MINOR(req->rq_dev)].max_segments;
423         int total_segments = req->nr_segments + next->nr_segments;
424         int same_segment;
425 
426         if (__max_segments < max_segments)
427                 max_segments = __max_segments;
428 
429         same_segment = 0;
430         if (req->bhtail->b_data + req->bhtail->b_size == next->bh->b_data)
431         {
432                 total_segments--;
433                 same_segment = 1;
434         }
435     
436         if (total_segments > max_segments)
437                 return 0;
438 
439         q->elevator.nr_segments -= same_segment;
440         req->nr_segments = total_segments;
441         return 1;
442 }
443 
444 
445 /*
446  *      OSM reply handler. This gets all the message replies
447  */
448 
449 static void i2o_block_reply(struct i2o_handler *h, struct i2o_controller *c, struct i2o_message *msg)
450 {
451         unsigned long flags;
452         struct i2ob_request *ireq = NULL;
453         u8 st;
454         u32 *m = (u32 *)msg;
455         u8 unit = (m[2]>>8)&0xF0;       /* low 4 bits are partition */
456         struct i2ob_device *dev = &i2ob_dev[(unit&0xF0)];
457 
458         /*
459          * FAILed message
460          */
461         if(m[0] & (1<<13))
462         {
463                 /*
464                  * FAILed message from controller
465                  * We increment the error count and abort it
466                  *
467                  * In theory this will never happen.  The I2O block class
468                  * speficiation states that block devices never return
469                  * FAILs but instead use the REQ status field...but
470                  * better be on the safe side since no one really follows
471                  * the spec to the book :)
472                  */
473                 ireq=&i2ob_queues[c->unit]->request_queue[m[3]];
474                 ireq->req->errors++;
475 
476                 spin_lock_irqsave(&io_request_lock, flags);
477                 i2ob_unhook_request(ireq, c->unit);
478                 i2ob_end_request(ireq->req);
479                 spin_unlock_irqrestore(&io_request_lock, flags);
480         
481                 /* Now flush the message by making it a NOP */
482                 m[0]&=0x00FFFFFF;
483                 m[0]|=(I2O_CMD_UTIL_NOP)<<24;
484                 i2o_post_message(c,virt_to_bus(m));
485 
486                 return;
487         }
488 
489         if(msg->function == I2O_CMD_UTIL_EVT_REGISTER)
490         {
491                 spin_lock(&i2ob_evt_lock);
492                 memcpy(&evt_msg, m, msg->size);
493                 spin_unlock(&i2ob_evt_lock);
494                 wake_up_interruptible(&i2ob_evt_wait);
495                 return;
496         }
497 
498         if(!dev->i2odev)
499         {
500                 /*
501                  * This is HACK, but Intel Integrated RAID allows user
502                  * to delete a volume that is claimed, locked, and in use 
503                  * by the OS. We have to check for a reply from a
504                  * non-existent device and flag it as an error or the system 
505                  * goes kaput...
506                  */
507                 ireq=&i2ob_queues[c->unit]->request_queue[m[3]];
508                 ireq->req->errors++;
509                 printk(KERN_WARNING "I2O Block: Data transfer to deleted device!\n");
510                 spin_lock_irqsave(&io_request_lock, flags);
511                 i2ob_unhook_request(ireq, c->unit);
512                 i2ob_end_request(ireq->req);
513                 spin_unlock_irqrestore(&io_request_lock, flags);
514                 return;
515         }       
516 
517         /*
518          *      Lets see what is cooking. We stuffed the
519          *      request in the context.
520          */
521                  
522         ireq=&i2ob_queues[c->unit]->request_queue[m[3]];
523         st=m[4]>>24;
524 
525         if(st!=0)
526         {
527                 char *bsa_errors[] = 
528                 { 
529                         "Success", 
530                         "Media Error", 
531                         "Failure communicating to device",
532                         "Device Failure",
533                         "Device is not ready",
534                         "Media not present",
535                         "Media is locked by another user",
536                         "Media has failed",
537                         "Failure communicating to device",
538                         "Device bus failure",
539                         "Device is locked by another user",
540                         "Device is write protected",
541                         "Device has reset",
542                         "Volume has changed, waiting for acknowledgement"
543                 };
544                                 
545                 printk(KERN_ERR "\n/dev/%s error: %s", dev->i2odev->dev_name, 
546                         bsa_errors[m[4]&0XFFFF]);
547                 if(m[4]&0x00FF0000)
548                         printk(" - DDM attempted %d retries", (m[4]>>16)&0x00FF );
549                 printk("\n");
550 
551                 ireq->req->errors++;    
552         }
553         else
554                 ireq->req->errors = 0;
555 
556         /*
557          *      Dequeue the request. We use irqsave locks as one day we
558          *      may be running polled controllers from a BH...
559          */
560         
561         spin_lock_irqsave(&io_request_lock, flags);
562         i2ob_unhook_request(ireq, c->unit);
563         i2ob_end_request(ireq->req);
564         atomic_dec(&i2ob_queues[c->unit]->queue_depth);
565 
566         /*
567          *      We may be able to do more I/O
568          */
569         i2ob_request(dev->req_queue);
570 
571         spin_unlock_irqrestore(&io_request_lock, flags);
572 }
573 
574 /* 
575  * Event handler.  Needs to be a separate thread b/c we may have
576  * to do things like scan a partition table, or query parameters
577  * which cannot be done from an interrupt or from a bottom half.
578  */
579 static int i2ob_evt(void *dummy)
580 {
581         unsigned int evt;
582         unsigned int flags;
583         int unit;
584         int i;
585 
586         lock_kernel();
587         daemonize();
588         unlock_kernel();
589 
590         strcpy(current->comm, "i2oblock");
591         evt_running = 1;
592 
593         while(1)
594         {
595 #warning "RACE"
596                 interruptible_sleep_on(&i2ob_evt_wait);
597                 if(signal_pending(current)) {
598                         evt_running = 0;
599                         return 0;
600                 }
601 
602                 printk(KERN_INFO "Doing something in i2o_block event thread\n");
603                 
604                 /*
605                  * Keep another CPU/interrupt from overwriting the 
606                  * message while we're reading it
607                  *
608                  * We stuffed the unit in the TxContext and grab the event mask
609                  * None of the BSA we care about events have EventData
610                  */
611                 spin_lock_irqsave(&i2ob_evt_lock, flags);
612                 unit = evt_msg[3];
613                 evt = evt_msg[4];
614                 spin_unlock_irqrestore(&i2ob_evt_lock, flags);
615 
616                 switch(evt)
617                 {
618                         /*
619                          * New volume loaded on same TID, so we just re-install.
620                          * The TID/controller don't change as it is the same
621                          * I2O device.  It's just new media that we have to
622                          * rescan.
623                          */
624                         case I2O_EVT_IND_BSA_VOLUME_LOAD:
625                         {
626                                 i2ob_install_device(i2ob_dev[unit].i2odev->controller, 
627                                         i2ob_dev[unit].i2odev, unit);
628                                 break;
629                         }
630 
631                         /*
632                          * No media, so set all parameters to 0 and set the media
633                          * change flag. The I2O device is still valid, just doesn't
634                          * have media, so we don't want to clear the controller or
635                          * device pointer.
636                          */
637                         case I2O_EVT_IND_BSA_VOLUME_UNLOAD:
638                         {
639                                 for(i = unit; i <= unit+15; i++)
640                                 {
641                                         i2ob_sizes[i] = 0;
642                                         i2ob_hardsizes[i] = 0;
643                                         i2ob_max_sectors[i] = 0;
644                                         i2ob[i].nr_sects = 0;
645                                         i2ob_gendisk.part[i].nr_sects = 0;
646                                 }
647                                 i2ob_media_change_flag[unit] = 1;
648                                 break;
649                         }
650 
651                         case I2O_EVT_IND_BSA_VOLUME_UNLOAD_REQ:
652                                 printk(KERN_WARNING "%s: Attempt to eject locked media\n", 
653                                         i2ob_dev[unit].i2odev->dev_name);
654                                 break;
655 
656                         /*
657                          * The capacity has changed and we are going to be
658                          * updating the max_sectors and other information 
659                          * about this disk.  We try a revalidate first. If
660                          * the block device is in use, we don't want to
661                          * do that as there may be I/Os bound for the disk
662                          * at the moment.  In that case we read the size 
663                          * from the device and update the information ourselves
664                          * and the user can later force a partition table
665                          * update through an ioctl.
666                          */
667                         case I2O_EVT_IND_BSA_CAPACITY_CHANGE:
668                         {
669                                 u64 size;
670 
671                                 if(do_i2ob_revalidate(MKDEV(MAJOR_NR, unit),0) != -EBUSY)
672                                         continue;
673 
674                                 if(i2ob_query_device(&i2ob_dev[unit], 0x0004, 0, &size, 8) !=0 )
675                                         i2ob_query_device(&i2ob_dev[unit], 0x0000, 4, &size, 8);
676 
677                                 spin_lock_irqsave(&io_request_lock, flags);     
678                                 i2ob_sizes[unit] = (int)(size>>10);
679                                 i2ob_gendisk.part[unit].nr_sects = size>>9;
680                                 i2ob[unit].nr_sects = (int)(size>>9);
681                                 spin_unlock_irqrestore(&io_request_lock, flags);        
682                                 break;
683                         }
684 
685                         /*
686                          * An event we didn't ask for.  Call the card manufacturer
687                          * and tell them to fix their firmware :)
688                          */
689                         default:
690                                 printk(KERN_INFO "%s: Received event we didn't register for\n"
691                                         KERN_INFO "   Call I2O card manufacturer\n", 
692                                         i2ob_dev[unit].i2odev->dev_name);
693                                 break;
694                 }
695         };
696 
697         return 0;
698 }
699 
700 /*
701  * The timer handler will attempt to restart requests 
702  * that are queued to the driver.  This handler
703  * currently only gets called if the controller
704  * had no more room in its inbound fifo.  
705  */
706 
707 static void i2ob_timer_handler(unsigned long q)
708 {
709         unsigned long flags;
710 
711         /*
712          * We cannot touch the request queue or the timer
713          * flag without holding the io_request_lock.
714          */
715         spin_lock_irqsave(&io_request_lock,flags);
716 
717         /* 
718          * Clear the timer started flag so that 
719          * the timer can be queued again.
720          */
721         i2ob_timer_started = 0;
722 
723         /* 
724          * Restart any requests.
725          */
726         i2ob_request((request_queue_t*)q);
727 
728         /* 
729          * Free the lock.
730          */
731         spin_unlock_irqrestore(&io_request_lock,flags);
732 }
733 
734 /*
735  *      The I2O block driver is listed as one of those that pulls the
736  *      front entry off the queue before processing it. This is important
737  *      to remember here. If we drop the io lock then CURRENT will change
738  *      on us. We must unlink CURRENT in this routine before we return, if
739  *      we use it.
740  */
741 static void i2ob_request(request_queue_t *q)
742 {
743         struct request *req;
744         struct i2ob_request *ireq;
745         int unit;
746         struct i2ob_device *dev;
747         u32 m;
748 
749         // printk(KERN_INFO "i2ob_request() called with queue %p\n", q);
750 
751         while (!list_empty(&q->queue_head)) {
752                 /*
753                  *      On an IRQ completion if there is an inactive
754                  *      request on the queue head it means it isnt yet
755                  *      ready to dispatch.
756                  */
757                 req = blkdev_entry_next_request(&q->queue_head);
758 
759                 if(req->rq_status == RQ_INACTIVE)
760                         return;
761                         
762                 unit = MINOR(req->rq_dev);
763                 dev = &i2ob_dev[(unit&0xF0)];
764 
765                 /* 
766                  *      Queue depths probably belong with some kind of 
767                  *      generic IOP commit control. Certainly its not right 
768                  *      its global!  
769                  */
770                 if(atomic_read(&i2ob_queues[dev->unit]->queue_depth)>=MAX_I2OB_DEPTH)
771                         break;
772 
773                 /* Get a message */
774                 m = i2ob_get(dev);
775 
776                 if(m==0xFFFFFFFF)
777                 {
778                         /* 
779                          * See if the timer has already been queued.
780                          */
781                         if (!i2ob_timer_started)
782                         {
783                                 printk(KERN_ERR "i2ob: starting timer\n");
784 
785                                 /*
786                                  * Set the timer_started flag to insure
787                                  * that the timer is only queued once.
788                                  * Queing it more than once will corrupt
789                                  * the timer queue.
790                                  */
791                                 i2ob_timer_started = 1;
792 
793                                 /* 
794                                  * Set up the timer to expire in
795                                  * 500ms.
796                                  */
797                                 i2ob_timer.expires = jiffies + (HZ >> 1);
798                                 i2ob_timer.data = (unsigned int)q;
799 
800                                 /*
801                                  * Start it.
802                                  */
803                                  
804                                 add_timer(&i2ob_timer);
805                         }
806                 }
807 
808                 /*
809                  * Everything ok, so pull from kernel queue onto our queue
810                  */
811                 req->errors = 0;
812                 blkdev_dequeue_request(req);    
813                 req->sem = NULL;
814 
815                 ireq = i2ob_queues[dev->unit]->i2ob_qhead;
816                 i2ob_queues[dev->unit]->i2ob_qhead = ireq->next;
817                 ireq->req = req;
818 
819                 i2ob_send(m, dev, ireq, i2ob[unit].start_sect, (unit&0xF0));
820         }
821 }
822 
823 
824 /*
825  *      SCSI-CAM for ioctl geometry mapping
826  *      Duplicated with SCSI - this should be moved into somewhere common
827  *      perhaps genhd ?
828  *
829  * LBA -> CHS mapping table taken from:
830  *
831  * "Incorporating the I2O Architecture into BIOS for Intel Architecture 
832  *  Platforms" 
833  *
834  * This is an I2O document that is only available to I2O members,
835  * not developers.
836  *
837  * From my understanding, this is how all the I2O cards do this
838  *
839  * Disk Size      | Sectors | Heads | Cylinders
840  * ---------------+---------+-------+-------------------
841  * 1 < X <= 528M  | 63      | 16    | X/(63 * 16 * 512)
842  * 528M < X <= 1G | 63      | 32    | X/(63 * 32 * 512)
843  * 1 < X <528M    | 63      | 16    | X/(63 * 16 * 512)
844  * 1 < X <528M    | 63      | 16    | X/(63 * 16 * 512)
845  *
846  */
847 #define BLOCK_SIZE_528M         1081344
848 #define BLOCK_SIZE_1G           2097152
849 #define BLOCK_SIZE_21G          4403200
850 #define BLOCK_SIZE_42G          8806400
851 #define BLOCK_SIZE_84G          17612800
852 
853 static void i2o_block_biosparam(
854         unsigned long capacity,
855         unsigned short *cyls,
856         unsigned char *hds,
857         unsigned char *secs) 
858 { 
859         unsigned long heads, sectors, cylinders; 
860 
861         sectors = 63L;                          /* Maximize sectors per track */ 
862         if(capacity <= BLOCK_SIZE_528M)
863                 heads = 16;
864         else if(capacity <= BLOCK_SIZE_1G)
865                 heads = 32;
866         else if(capacity <= BLOCK_SIZE_21G)
867                 heads = 64;
868         else if(capacity <= BLOCK_SIZE_42G)
869                 heads = 128;
870         else
871                 heads = 255;
872 
873         cylinders = capacity / (heads * sectors);
874 
875         *cyls = (unsigned short) cylinders;     /* Stuff return values */ 
876         *secs = (unsigned char) sectors; 
877         *hds  = (unsigned char) heads; 
878 }
879 
880 
881 /*
882  *      Rescan the partition tables
883  */
884  
885 static int do_i2ob_revalidate(kdev_t dev, int maxu)
886 {
887         int minor=MINOR(dev);
888         int i;
889         
890         minor&=0xF0;
891 
892         i2ob_dev[minor].refcnt++;
893         if(i2ob_dev[minor].refcnt>maxu+1)
894         {
895                 i2ob_dev[minor].refcnt--;
896                 return -EBUSY;
897         }
898         
899         for( i = 15; i>=0 ; i--)
900         {
901                 int m = minor+i;
902                 kdev_t d = MKDEV(MAJOR_NR, m);
903                 struct super_block *sb = get_super(d);
904                 
905                 sync_dev(d);
906                 if(sb)
907                         invalidate_inodes(sb);
908                 invalidate_buffers(d);
909                 i2ob_gendisk.part[m].start_sect = 0;
910                 i2ob_gendisk.part[m].nr_sects = 0;
911         }
912 
913         /*
914          *      Do a physical check and then reconfigure
915          */
916          
917         i2ob_install_device(i2ob_dev[minor].controller, i2ob_dev[minor].i2odev,
918                 minor);
919         i2ob_dev[minor].refcnt--;
920         return 0;
921 }
922 
923 /*
924  *      Issue device specific ioctl calls.
925  */
926 
927 static int i2ob_ioctl(struct inode *inode, struct file *file,
928                      unsigned int cmd, unsigned long arg)
929 {
930         struct i2ob_device *dev;
931         int minor;
932 
933         /* Anyone capable of this syscall can do *real bad* things */
934 
935         if (!capable(CAP_SYS_ADMIN))
936                 return -EPERM;
937         if (!inode)
938                 return -EINVAL;
939         minor = MINOR(inode->i_rdev);
940         if (minor >= (MAX_I2OB<<4))
941                 return -ENODEV;
942 
943         dev = &i2ob_dev[minor];
944         switch (cmd) {
945                 case BLKGETSIZE:
946                         return put_user(i2ob[minor].nr_sects, (long *) arg);
947 
948                 case HDIO_GETGEO:
949                 {
950                         struct hd_geometry g;
951                         int u=minor&0xF0;
952                         i2o_block_biosparam(i2ob_sizes[u]<<1, 
953                                 &g.cylinders, &g.heads, &g.sectors);
954                         g.start = i2ob[minor].start_sect;
955                         return copy_to_user((void *)arg,&g, sizeof(g))?-EFAULT:0;
956                 }
957         
958                 case BLKRRPART:
959                         if(!capable(CAP_SYS_ADMIN))
960                                 return -EACCES;
961                         return do_i2ob_revalidate(inode->i_rdev,1);
962                         
963                 case BLKFLSBUF:
964                 case BLKROSET:
965                 case BLKROGET:
966                 case BLKRASET:
967                 case BLKRAGET:
968                 case BLKPG:
969                         return blk_ioctl(inode->i_rdev, cmd, arg);
970                         
971                 default:
972                         return -EINVAL;
973         }
974 }
975 
976 /*
977  *      Close the block device down
978  */
979  
980 static int i2ob_release(struct inode *inode, struct file *file)
981 {
982         struct i2ob_device *dev;
983         int minor;
984 
985         minor = MINOR(inode->i_rdev);
986         if (minor >= (MAX_I2OB<<4))
987                 return -ENODEV;
988         dev = &i2ob_dev[(minor&0xF0)];
989 
990         /*
991          * This is to deail with the case of an application
992          * opening a device and then the device dissapears while
993          * it's in use, and then the application tries to release
994          * it.  ex: Unmounting a deleted RAID volume at reboot. 
995          * If we send messages, it will just cause FAILs since
996          * the TID no longer exists.
997          */
998         if(!dev->i2odev)
999                 return 0;
1000 
1001         /* Sync the device so we don't get errors */
1002         fsync_dev(inode->i_rdev);
1003 
1004         if (dev->refcnt <= 0)
1005                 printk(KERN_ALERT "i2ob_release: refcount(%d) <= 0\n", dev->refcnt);
1006         dev->refcnt--;
1007         if(dev->refcnt==0)
1008         {
1009                 /*
1010                  *      Flush the onboard cache on unmount
1011                  */
1012                 u32 msg[5];
1013                 int *query_done = &dev->done_flag;
1014                 msg[0] = FIVE_WORD_MSG_SIZE|SGL_OFFSET_0;
1015                 msg[1] = I2O_CMD_BLOCK_CFLUSH<<24|HOST_TID<<12|dev->tid;
1016                 msg[2] = i2ob_context|0x40000000;
1017                 msg[3] = (u32)query_done;
1018                 msg[4] = 60<<16;
1019                 i2o_post_wait(dev->controller, msg, 20, 2);
1020 
1021                 /*
1022                  *      Unlock the media
1023                  */
1024                 msg[0] = FIVE_WORD_MSG_SIZE|SGL_OFFSET_0;
1025                 msg[1] = I2O_CMD_BLOCK_MUNLOCK<<24|HOST_TID<<12|dev->tid;
1026                 msg[2] = i2ob_context|0x40000000;
1027                 msg[3] = (u32)query_done;
1028                 msg[4] = -1;
1029                 i2o_post_wait(dev->controller, msg, 20, 2);
1030         
1031                 /*
1032                  * Now unclaim the device.
1033                  */
1034                 if (i2o_release_device(dev->i2odev, &i2o_block_handler))
1035                         printk(KERN_ERR "i2ob_release: controller rejected unclaim.\n");
1036 
1037         }
1038         MOD_DEC_USE_COUNT;
1039         return 0;
1040 }
1041 
1042 /*
1043  *      Open the block device.
1044  */
1045  
1046 static int i2ob_open(struct inode *inode, struct file *file)
1047 {
1048         int minor;
1049         struct i2ob_device *dev;
1050         
1051         if (!inode)
1052                 return -EINVAL;
1053         minor = MINOR(inode->i_rdev);
1054         if (minor >= MAX_I2OB<<4)
1055                 return -ENODEV;
1056         dev=&i2ob_dev[(minor&0xF0)];
1057 
1058         if(!dev->i2odev)        
1059                 return -ENODEV;
1060         
1061         if(dev->refcnt++==0)
1062         { 
1063                 u32 msg[6];
1064                 
1065                 if(i2o_claim_device(dev->i2odev, &i2o_block_handler))
1066                 {
1067                         dev->refcnt--;
1068                         printk(KERN_INFO "I2O Block: Could not open device\n");
1069                         return -EBUSY;
1070                 }
1071                 
1072                 /*
1073                  *      Mount the media if needed. Note that we don't use
1074                  *      the lock bit. Since we have to issue a lock if it
1075                  *      refuses a mount (quite possible) then we might as
1076                  *      well just send two messages out.
1077                  */
1078                 msg[0] = FIVE_WORD_MSG_SIZE|SGL_OFFSET_0;               
1079                 msg[1] = I2O_CMD_BLOCK_MMOUNT<<24|HOST_TID<<12|dev->tid;
1080                 msg[4] = -1;
1081                 msg[5] = 0;
1082                 i2o_post_wait(dev->controller, msg, 24, 2);
1083 
1084                 /*
1085                  *      Lock the media
1086                  */
1087                 msg[0] = FIVE_WORD_MSG_SIZE|SGL_OFFSET_0;
1088                 msg[1] = I2O_CMD_BLOCK_MLOCK<<24|HOST_TID<<12|dev->tid;
1089                 msg[4] = -1;
1090                 i2o_post_wait(dev->controller, msg, 20, 2);
1091         }               
1092         MOD_INC_USE_COUNT;
1093         return 0;
1094 }
1095 
1096 /*
1097  *      Issue a device query
1098  */
1099  
1100 static int i2ob_query_device(struct i2ob_device *dev, int table, 
1101         int field, void *buf, int buflen)
1102 {
1103         return i2o_query_scalar(dev->controller, dev->tid,
1104                 table, field, buf, buflen);
1105 }
1106 
1107 
1108 /*
1109  *      Install the I2O block device we found.
1110  */
1111  
1112 static int i2ob_install_device(struct i2o_controller *c, struct i2o_device *d, int unit)
1113 {
1114         u64 size;
1115         u32 blocksize;
1116         u32 limit;
1117         u8 type;
1118         u32 flags, status;
1119         struct i2ob_device *dev=&i2ob_dev[unit];
1120         int i;
1121 
1122         /*
1123          * For logging purposes...
1124          */
1125         printk(KERN_INFO "i2ob: Installing tid %d device at unit %d\n", 
1126                         d->lct_data.tid, unit); 
1127 
1128         /*
1129          *      Ask for the current media data. If that isn't supported
1130          *      then we ask for the device capacity data
1131          */
1132         if(i2ob_query_device(dev, 0x0004, 1, &blocksize, 4) != 0
1133           || i2ob_query_device(dev, 0x0004, 0, &size, 8) !=0 )
1134         {
1135                 i2ob_query_device(dev, 0x0000, 3, &blocksize, 4);
1136                 i2ob_query_device(dev, 0x0000, 4, &size, 8);
1137         }
1138         
1139         i2ob_query_device(dev, 0x0000, 5, &flags, 4);
1140         i2ob_query_device(dev, 0x0000, 6, &status, 4);
1141         i2ob_sizes[unit] = (int)(size>>10);
1142         i2ob_hardsizes[unit] = blocksize;
1143         i2ob_gendisk.part[unit].nr_sects = size>>9;
1144         i2ob[unit].nr_sects = (int)(size>>9);
1145 
1146         /* Set limit based on inbound frame size */
1147         limit = (d->controller->status_block->inbound_frame_size - 8)/2;
1148         limit = limit<<9;
1149 
1150         /*
1151          * Max number of Scatter-Gather Elements
1152          */     
1153         i2ob_dev[unit].max_segments = 
1154                 (d->controller->status_block->inbound_frame_size - 8)/2;
1155 
1156         printk(KERN_INFO "Max Segments set to %d\n", 
1157                                 i2ob_dev[unit].max_segments);
1158         printk(KERN_INFO "Byte limit is %d.\n", limit);
1159         
1160         for(i=unit;i<=unit+15;i++)
1161         {
1162                 i2ob_max_sectors[i]=MAX_SECTORS;
1163                 i2ob_dev[i].max_segments = 
1164                         (d->controller->status_block->inbound_frame_size - 8)/2;
1165         }
1166 
1167         i2ob_query_device(dev, 0x0000, 0, &type, 1);
1168         
1169         sprintf(d->dev_name, "%s%c", i2ob_gendisk.major_name, 'a' + (unit>>4));
1170 
1171         printk(KERN_INFO "%s: ", d->dev_name);
1172         switch(type)
1173         {
1174                 case 0: printk("Disk Storage");break;
1175                 case 4: printk("WORM");break;
1176                 case 5: printk("CD-ROM");break;
1177                 case 7: printk("Optical device");break;
1178                 default:
1179                         printk("Type %d", type);
1180         }
1181         if(status&(1<<10))
1182                 printk("(RAID)");
1183         if(((flags & (1<<3)) && !(status & (1<<3))) ||
1184            ((flags & (1<<4)) && !(status & (1<<4))))
1185         {
1186                 printk(KERN_INFO " Not loaded.\n");
1187                 return 1;
1188         }
1189         printk("- %dMb, %d byte sectors",
1190                 (int)(size>>20), blocksize);
1191         if(status&(1<<0))
1192         {
1193                 u32 cachesize;
1194                 i2ob_query_device(dev, 0x0003, 0, &cachesize, 4);
1195                 cachesize>>=10;
1196                 if(cachesize>4095)
1197                         printk(", %dMb cache", cachesize>>10);
1198                 else
1199                         printk(", %dKb cache", cachesize);
1200         }
1201         printk(".\n");
1202         printk(KERN_INFO "%s: Maximum sectors/read set to %d.\n", 
1203                 d->dev_name, i2ob_max_sectors[unit]);
1204 
1205         /* 
1206          * If this is the first I2O block device found on this IOP,
1207          * we need to initialize all the queue data structures
1208          * before any I/O can be performed. If it fails, this
1209          * device is useless.
1210          */
1211         if(!i2ob_queues[c->unit]) {
1212                 if(i2ob_init_iop(c->unit))
1213                         return 1;
1214         }
1215 
1216         /* 
1217          * This will save one level of lookup/indirection in critical 
1218          * code so that we can directly get the queue ptr from the
1219          * device instead of having to go the IOP data structure.
1220          */
1221         dev->req_queue = &i2ob_queues[c->unit]->req_queue;
1222 
1223         grok_partitions(&i2ob_gendisk, unit>>4, 1<<4, (long)(size>>9));
1224 
1225         /*
1226          * Register for the events we're interested in and that the
1227          * device actually supports.
1228          */
1229         i2o_event_register(c, d->lct_data.tid, i2ob_context, unit, 
1230                 (I2OB_EVENT_MASK & d->lct_data.event_capabilities));
1231 
1232         return 0;
1233 }
1234 
1235 /*
1236  * Initialize IOP specific queue structures.  This is called
1237  * once for each IOP that has a block device sitting behind it.
1238  */
1239 static int i2ob_init_iop(unsigned int unit)
1240 {
1241         int i;
1242 
1243         i2ob_queues[unit] = (struct i2ob_iop_queue*)
1244                 kmalloc(sizeof(struct i2ob_iop_queue), GFP_ATOMIC);
1245         if(!i2ob_queues[unit])
1246         {
1247                 printk(KERN_WARNING
1248                         "Could not allocate request queue for I2O block device!\n");
1249                 return -1;
1250         }
1251 
1252         for(i = 0; i< MAX_I2OB_DEPTH; i++)
1253         {
1254                 i2ob_queues[unit]->request_queue[i].next = 
1255                         &i2ob_queues[unit]->request_queue[i+1];
1256                 i2ob_queues[unit]->request_queue[i].num = i;
1257         }
1258         
1259         /* Queue is MAX_I2OB + 1... */
1260         i2ob_queues[unit]->request_queue[i].next = NULL;
1261         i2ob_queues[unit]->i2ob_qhead = &i2ob_queues[unit]->request_queue[0];
1262         atomic_set(&i2ob_queues[unit]->queue_depth, 0);
1263 
1264         blk_init_queue(&i2ob_queues[unit]->req_queue, i2ob_request);
1265         blk_queue_headactive(&i2ob_queues[unit]->req_queue, 0);
1266         i2ob_queues[unit]->req_queue.back_merge_fn = i2ob_back_merge;
1267         i2ob_queues[unit]->req_queue.front_merge_fn = i2ob_front_merge;
1268         i2ob_queues[unit]->req_queue.merge_requests_fn = i2ob_merge_requests;
1269         i2ob_queues[unit]->req_queue.queuedata = &i2ob_queues[unit];
1270 
1271         return 0;
1272 }
1273 
1274 /*
1275  * Get the request queue for the given device.
1276  */     
1277 static request_queue_t* i2ob_get_queue(kdev_t dev)
1278 {
1279         int unit = MINOR(dev)&0xF0;
1280 
1281         return i2ob_dev[unit].req_queue;
1282 }
1283 
1284 /*
1285  * Probe the I2O subsytem for block class devices
1286  */
1287 static void i2ob_probe(void)
1288 {
1289         int i;
1290         int unit = 0;
1291         int warned = 0;
1292                 
1293         for(i=0; i< MAX_I2O_CONTROLLERS; i++)
1294         {
1295                 struct i2o_controller *c=i2o_find_controller(i);
1296                 struct i2o_device *d;
1297         
1298                 if(c==NULL)
1299                         continue;
1300 
1301                 for(d=c->devices;d!=NULL;d=d->next)
1302                 {
1303                         if(d->lct_data.class_id!=I2O_CLASS_RANDOM_BLOCK_STORAGE)
1304                                 continue;
1305 
1306                         if(d->lct_data.user_tid != 0xFFF)
1307                                 continue;
1308 
1309                         if(i2o_claim_device(d, &i2o_block_handler))
1310                         {
1311                                 printk(KERN_WARNING "i2o_block: Controller %d, TID %d\n", c->unit,
1312                                         d->lct_data.tid);
1313                                 printk(KERN_WARNING "\tDevice refused claim! Skipping installation\n");
1314                                 continue;
1315                         }
1316 
1317                         if(unit<MAX_I2OB<<4)
1318                         {
1319                                 /*
1320                                  * Get the device and fill in the
1321                                  * Tid and controller.
1322                                  */
1323                                 struct i2ob_device *dev=&i2ob_dev[unit];
1324                                 dev->i2odev = d; 
1325                                 dev->controller = c;
1326                                 dev->unit = c->unit;
1327                                 dev->tid = d->lct_data.tid;
1328 
1329                                 if(i2ob_install_device(c,d,unit))
1330                                         printk(KERN_WARNING "Could not install I2O block device\n");
1331                                 else
1332                                 {
1333                                         unit+=16;
1334                                         i2ob_dev_count++;
1335 
1336                                         /* We want to know when device goes away */
1337                                         i2o_device_notify_on(d, &i2o_block_handler);
1338                                 }
1339                         }
1340                         else
1341                         {
1342                                 if(!warned++)
1343                                         printk(KERN_WARNING "i2o_block: too many device, registering only %d.\n", unit>>4);
1344                         }
1345                         i2o_release_device(d, &i2o_block_handler);
1346                 }
1347                 i2o_unlock_controller(c);
1348         }
1349 }
1350 
1351 /*
1352  * New device notification handler.  Called whenever a new
1353  * I2O block storage device is added to the system.
1354  * 
1355  * Should we spin lock around this to keep multiple devs from 
1356  * getting updated at the same time? 
1357  * 
1358  */
1359 void i2ob_new_device(struct i2o_controller *c, struct i2o_device *d)
1360 {
1361         struct i2ob_device *dev;
1362         int unit = 0;
1363 
1364         printk(KERN_INFO "i2o_block: New device detected\n");
1365         printk(KERN_INFO "   Controller %d Tid %d\n",c->unit, d->lct_data.tid);
1366 
1367         /* Check for available space */
1368         if(i2ob_dev_count>=MAX_I2OB<<4)
1369         {
1370                 printk(KERN_ERR "i2o_block: No more devices allowed!\n");
1371                 return;
1372         }
1373         for(unit = 0; unit < (MAX_I2OB<<4); unit += 16)
1374         {
1375                 if(!i2ob_dev[unit].i2odev)
1376                         break;
1377         }
1378 
1379         /*
1380          * Creating a RAID 5 volume takes a little while and the UTIL_CLAIM
1381          * will fail if we don't give the card enough time to do it's magic, 
1382          * so we just sleep for a little while and let it do it's thing
1383          */
1384         current->state = TASK_INTERRUPTIBLE;
1385         schedule_timeout(3*HZ);
1386 
1387         if(i2o_claim_device(d, &i2o_block_handler))
1388         {
1389                 printk(KERN_INFO 
1390                         "i2o_block: Unable to claim device. Installation aborted\n");
1391                 return;
1392         }
1393 
1394         dev = &i2ob_dev[unit];
1395         dev->i2odev = d; 
1396         dev->controller = c;
1397         dev->tid = d->lct_data.tid;
1398 
1399         if(i2ob_install_device(c,d,unit))
1400                 printk(KERN_ERR "i2o_block: Could not install new device\n");
1401         else    
1402         {
1403                 i2ob_dev_count++;
1404                 i2o_device_notify_on(d, &i2o_block_handler);
1405         }
1406 
1407         i2o_release_device(d, &i2o_block_handler);
1408  
1409         return;
1410 }
1411 
1412 /*
1413  * Deleted device notification handler.  Called when a device we
1414  * are talking to has been deleted by the user or some other
1415  * mysterious fource outside the kernel.
1416  */
1417 void i2ob_del_device(struct i2o_controller *c, struct i2o_device *d)
1418 {       
1419         int unit = 0;
1420         int i = 0;
1421         int flags;
1422 
1423         spin_lock_irqsave(&io_request_lock, flags);
1424 
1425         /*
1426          * Need to do this...we somtimes get two events from the IRTOS
1427          * in a row and that causes lots of problems.
1428          */
1429         i2o_device_notify_off(d, &i2o_block_handler);
1430 
1431         printk(KERN_INFO "I2O Block Device Deleted\n");
1432 
1433         for(unit = 0; unit < MAX_I2OB<<4; unit += 16)
1434         {
1435                 if(i2ob_dev[unit].i2odev == d)
1436                 {
1437                         printk(KERN_INFO "  /dev/%s: Controller %d Tid %d\n", 
1438                                 d->dev_name, c->unit, d->lct_data.tid);
1439                         break;
1440                 }
1441         }
1442         if(unit >= MAX_I2OB<<4)
1443         {
1444                 printk(KERN_ERR "i2ob_del_device called, but not in dev table!\n");
1445                 return;
1446         }
1447 
1448         /* 
1449          * This will force errors when i2ob_get_queue() is called
1450          * by the kenrel.
1451          */
1452         i2ob_dev[unit].req_queue = NULL;
1453         for(i = unit; i <= unit+15; i++)
1454         {
1455                 i2ob_dev[i].i2odev = NULL;
1456                 i2ob_sizes[i] = 0;
1457                 i2ob_hardsizes[i] = 0;
1458                 i2ob_max_sectors[i] = 0;
1459                 i2ob[i].nr_sects = 0;
1460                 i2ob_gendisk.part[i].nr_sects = 0;
1461         }
1462         spin_unlock_irqrestore(&io_request_lock, flags);
1463 
1464         /*
1465          * Sync the device...this will force all outstanding I/Os
1466          * to attempt to complete, thus causing error messages.
1467          * We have to do this as the user could immediatelly create
1468          * a new volume that gets assigned the same minor number.
1469          * If there are still outstanding writes to the device,
1470          * that could cause data corruption on the new volume!
1471          *
1472          * The truth is that deleting a volume that you are currently
1473          * accessing will do _bad things_ to your system.  This 
1474          * handler will keep it from crashing, but must probably 
1475          * you'll have to do a 'reboot' to get the system running
1476          * properly.  Deleting disks you are using is dumb.  
1477          * Umount them first and all will be good!
1478          *
1479          * It's not this driver's job to protect the system from
1480          * dumb user mistakes :)
1481          */
1482         if(i2ob_dev[unit].refcnt)
1483                 fsync_dev(MKDEV(MAJOR_NR,unit));
1484 
1485         /*
1486          * Decrease usage count for module
1487          */     
1488         while(i2ob_dev[unit].refcnt--)
1489                 MOD_DEC_USE_COUNT;
1490 
1491         i2ob_dev[unit].refcnt = 0;
1492         
1493         i2ob_dev[i].tid = 0;
1494 
1495         /* 
1496          * Do we need this?
1497          * The media didn't really change...the device is just gone
1498          */
1499         i2ob_media_change_flag[unit] = 1;
1500 
1501         i2ob_dev_count--;       
1502 
1503         return;
1504 }
1505 
1506 /*
1507  *      Have we seen a media change ?
1508  */
1509 static int i2ob_media_change(kdev_t dev)
1510 {
1511         int i=MINOR(dev);
1512         i>>=4;
1513         if(i2ob_media_change_flag[i])
1514         {
1515                 i2ob_media_change_flag[i]=0;
1516                 return 1;
1517         }
1518         return 0;
1519 }
1520 
1521 static int i2ob_revalidate(kdev_t dev)
1522 {
1523         return do_i2ob_revalidate(dev, 0);
1524 }
1525 
1526 /*
1527  * Reboot notifier.  This is called by i2o_core when the system
1528  * shuts down.
1529  */
1530 static void i2ob_reboot_event(void)
1531 {
1532         int i;
1533         
1534         for(i=0;i<MAX_I2OB;i++)
1535         {
1536                 struct i2ob_device *dev=&i2ob_dev[(i<<4)];
1537                 
1538                 if(dev->refcnt!=0)
1539                 {
1540                         /*
1541                          *      Flush the onboard cache
1542                          */
1543                         u32 msg[5];
1544                         int *query_done = &dev->done_flag;
1545                         msg[0] = FIVE_WORD_MSG_SIZE|SGL_OFFSET_0;
1546                         msg[1] = I2O_CMD_BLOCK_CFLUSH<<24|HOST_TID<<12|dev->tid;
1547                         msg[2] = i2ob_context|0x40000000;
1548                         msg[3] = (u32)query_done;
1549                         msg[4] = 60<<16;
1550                         i2o_post_wait(dev->controller, msg, 20, 2);
1551 
1552                         /*
1553                          *      Unlock the media
1554                          */
1555                         msg[0] = FIVE_WORD_MSG_SIZE|SGL_OFFSET_0;
1556                         msg[1] = I2O_CMD_BLOCK_MUNLOCK<<24|HOST_TID<<12|dev->tid;
1557                         msg[2] = i2ob_context|0x40000000;
1558                         msg[3] = (u32)query_done;
1559                         msg[4] = -1;
1560                         i2o_post_wait(dev->controller, msg, 20, 2);
1561                 }
1562         }       
1563 }
1564 
1565 static struct block_device_operations i2ob_fops =
1566 {
1567    open:       i2ob_open,
1568    release:    i2ob_release,
1569    ioctl:         i2ob_ioctl,
1570    check_media_change:  i2ob_media_change,
1571    revalidate:    i2ob_revalidate,
1572 };
1573 
1574 
1575 static struct gendisk i2ob_gendisk = 
1576 {
1577         MAJOR_NR,
1578         "i2o/hd",
1579         4,
1580         1<<4,
1581         i2ob,
1582         i2ob_sizes,
1583         0,
1584         NULL,
1585         NULL
1586 };
1587 
1588 
1589 /*
1590  * And here should be modules and kernel interface 
1591  *  (Just smiley confuses emacs :-)
1592  */
1593 
1594 #ifdef MODULE
1595 #define i2o_block_init init_module
1596 #endif
1597 
1598 int i2o_block_init(void)
1599 {
1600         int i;
1601 
1602         printk(KERN_INFO "I2O Block Storage OSM v0.9\n");
1603         printk(KERN_INFO "   (c) Copyright 1999, 2000 Red Hat Software.\n");
1604         
1605         /*
1606          *      Register the block device interfaces
1607          */
1608 
1609         if (register_blkdev(MAJOR_NR, "i2o_block", &i2ob_fops)) {
1610                 printk(KERN_ERR "Unable to get major number %d for i2o_block\n",
1611                        MAJOR_NR);
1612                 return -EIO;
1613         }
1614 #ifdef MODULE
1615         printk(KERN_INFO "i2o_block: registered device at major %d\n", MAJOR_NR);
1616 #endif
1617 
1618         /*
1619          *      Now fill in the boiler plate
1620          */
1621          
1622         blksize_size[MAJOR_NR] = i2ob_blksizes;
1623         hardsect_size[MAJOR_NR] = i2ob_hardsizes;
1624         blk_size[MAJOR_NR] = i2ob_sizes;
1625         max_sectors[MAJOR_NR] = i2ob_max_sectors;
1626         blk_dev[MAJOR_NR].queue = i2ob_get_queue;
1627         
1628         blk_init_queue(BLK_DEFAULT_QUEUE(MAJOR_NR), i2ob_request);
1629         blk_queue_headactive(BLK_DEFAULT_QUEUE(MAJOR_NR), 0);
1630 
1631         for (i = 0; i < MAX_I2OB << 4; i++) {
1632                 i2ob_dev[i].refcnt = 0;
1633                 i2ob_dev[i].flags = 0;
1634                 i2ob_dev[i].controller = NULL;
1635                 i2ob_dev[i].i2odev = NULL;
1636                 i2ob_dev[i].tid = 0;
1637                 i2ob_dev[i].head = NULL;
1638                 i2ob_dev[i].tail = NULL;
1639                 i2ob_blksizes[i] = 1024;
1640                 i2ob_max_sectors[i] = 2;
1641         }
1642         
1643         /*
1644          *      Set up the queue
1645          */
1646         for(i = 0; i < MAX_I2O_CONTROLLERS; i++)
1647         {
1648                 i2ob_queues[i] = NULL;
1649         }
1650 
1651         /*
1652          *      Timers
1653          */
1654          
1655         init_timer(&i2ob_timer);
1656         i2ob_timer.function = i2ob_timer_handler;
1657         i2ob_timer.data = 0;
1658         
1659         /*
1660          *      Register the OSM handler as we will need this to probe for
1661          *      drives, geometry and other goodies.
1662          */
1663 
1664         if(i2o_install_handler(&i2o_block_handler)<0)
1665         {
1666                 unregister_blkdev(MAJOR_NR, "i2o_block");
1667                 blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR));
1668                 printk(KERN_ERR "i2o_block: unable to register OSM.\n");
1669                 return -EINVAL;
1670         }
1671         i2ob_context = i2o_block_handler.context;        
1672 
1673         /*
1674          * Initialize event handling thread
1675          */
1676         init_MUTEX_LOCKED(&i2ob_evt_sem);
1677         evt_pid = kernel_thread(i2ob_evt, NULL, CLONE_SIGHAND);
1678         if(evt_pid < 0)
1679         {
1680                 printk(KERN_ERR 
1681                         "i2o_block: Could not initialize event thread.  Aborting\n");
1682                 i2o_remove_handler(&i2o_block_handler);
1683                 return 0;
1684         }
1685 
1686         /*
1687          *      Finally see what is actually plugged in to our controllers
1688          */
1689         for (i = 0; i < MAX_I2OB; i++)
1690                 register_disk(&i2ob_gendisk, MKDEV(MAJOR_NR,i<<4), 1<<4,
1691                         &i2ob_fops, 0);
1692         i2ob_probe();
1693         
1694         return 0;
1695 }
1696 
1697 #ifdef MODULE
1698 
1699 EXPORT_NO_SYMBOLS;
1700 MODULE_AUTHOR("Red Hat Software");
1701 MODULE_DESCRIPTION("I2O Block Device OSM");
1702 
1703 void cleanup_module(void)
1704 {
1705         struct gendisk **gdp;
1706         int i;
1707         
1708         /*
1709          * Unregister for updates from any devices..otherwise we still
1710          * get them and the core jumps to random memory :O
1711          */
1712         if(i2ob_dev_count) {
1713                 struct i2o_device *d;
1714                 for(i = 0; i < MAX_I2OB; i++)
1715                 if((d=i2ob_dev[i<<4].i2odev)) {
1716                         i2o_device_notify_off(d, &i2o_block_handler);
1717                         i2o_event_register(d->controller, d->lct_data.tid, 
1718                                 i2ob_context, i<<4, 0);
1719                 }
1720         }
1721         
1722         /*
1723          *      Flush the OSM
1724          */
1725 
1726         i2o_remove_handler(&i2o_block_handler);
1727                  
1728         /*
1729          *      Return the block device
1730          */
1731         if (unregister_blkdev(MAJOR_NR, "i2o_block") != 0)
1732                 printk("i2o_block: cleanup_module failed\n");
1733 
1734         /*
1735          * free request queue
1736          */
1737         blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR));
1738 
1739         if(evt_running) {
1740                 i = kill_proc(evt_pid, SIGTERM, 1);
1741                 if(!i) {
1742                         int count = 5 * 100;
1743                         while(evt_running && --count) {
1744                                 current->state = TASK_INTERRUPTIBLE;
1745                                 schedule_timeout(1);
1746                         }
1747         
1748                         if(!count)
1749                                 printk(KERN_ERR "Giving up on i2oblock thread...\n");   
1750                 }
1751         }
1752 
1753 
1754         /*
1755          *      Why isnt register/unregister gendisk in the kernel ???
1756          */
1757 
1758         for (gdp = &gendisk_head; *gdp; gdp = &((*gdp)->next))
1759                 if (*gdp == &i2ob_gendisk)
1760                         break;
1761 
1762 }
1763 #endif
1764 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.