i2o_block.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:46k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

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