[PATCH] I2O: first code cleanup of spare warnings and unused functions
[cascardo/linux.git] / drivers / message / i2o / i2o_scsi.c
1 /*
2  * This program is free software; you can redistribute it and/or modify it
3  * under the terms of the GNU General Public License as published by the
4  * Free Software Foundation; either version 2, or (at your option) any
5  * later version.
6  *
7  * This program is distributed in the hope that it will be useful, but
8  * WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10  * General Public License for more details.
11  *
12  * For the avoidance of doubt the "preferred form" of this code is one which
13  * is in an open non patent encumbered format. Where cryptographic key signing
14  * forms part of the process of creating an executable the information
15  * including keys needed to generate an equivalently functional executable
16  * are deemed to be part of the source code.
17  *
18  *  Complications for I2O scsi
19  *
20  *      o       Each (bus,lun) is a logical device in I2O. We keep a map
21  *              table. We spoof failed selection for unmapped units
22  *      o       Request sense buffers can come back for free.
23  *      o       Scatter gather is a bit dynamic. We have to investigate at
24  *              setup time.
25  *      o       Some of our resources are dynamically shared. The i2o core
26  *              needs a message reservation protocol to avoid swap v net
27  *              deadlocking. We need to back off queue requests.
28  *
29  *      In general the firmware wants to help. Where its help isn't performance
30  *      useful we just ignore the aid. Its not worth the code in truth.
31  *
32  * Fixes/additions:
33  *      Steve Ralston:
34  *              Scatter gather now works
35  *      Markus Lidel <Markus.Lidel@shadowconnect.com>:
36  *              Minor fixes for 2.6.
37  *
38  * To Do:
39  *      64bit cleanups
40  *      Fix the resource management problems.
41  */
42
43 #define DEBUG 1
44 #include <linux/module.h>
45 #include <linux/kernel.h>
46 #include <linux/types.h>
47 #include <linux/string.h>
48 #include <linux/ioport.h>
49 #include <linux/jiffies.h>
50 #include <linux/interrupt.h>
51 #include <linux/timer.h>
52 #include <linux/delay.h>
53 #include <linux/proc_fs.h>
54 #include <linux/prefetch.h>
55 #include <linux/pci.h>
56 #include <linux/blkdev.h>
57 #include <linux/i2o.h>
58
59 #include <asm/dma.h>
60 #include <asm/system.h>
61 #include <asm/io.h>
62 #include <asm/atomic.h>
63
64 #include <scsi/scsi.h>
65 #include <scsi/scsi_host.h>
66 #include <scsi/scsi_device.h>
67 #include <scsi/scsi_cmnd.h>
68
69 #define OSM_NAME        "scsi-osm"
70 #define OSM_VERSION     "$Rev$"
71 #define OSM_DESCRIPTION "I2O SCSI Peripheral OSM"
72
73 static struct i2o_driver i2o_scsi_driver;
74
75 static int i2o_scsi_max_id = 16;
76 static int i2o_scsi_max_lun = 8;
77
78 struct i2o_scsi_host {
79         struct Scsi_Host *scsi_host;    /* pointer to the SCSI host */
80         struct i2o_controller *iop;     /* pointer to the I2O controller */
81         struct i2o_device *channel[0];  /* channel->i2o_dev mapping table */
82 };
83
84 static struct scsi_host_template i2o_scsi_host_template;
85
86 #define I2O_SCSI_CAN_QUEUE      4
87
88 /* SCSI OSM class handling definition */
89 static struct i2o_class_id i2o_scsi_class_id[] = {
90         {I2O_CLASS_SCSI_PERIPHERAL},
91         {I2O_CLASS_END}
92 };
93
94 static struct i2o_scsi_host *i2o_scsi_host_alloc(struct i2o_controller *c)
95 {
96         struct i2o_scsi_host *i2o_shost;
97         struct i2o_device *i2o_dev;
98         struct Scsi_Host *scsi_host;
99         int max_channel = 0;
100         u8 type;
101         int i;
102         size_t size;
103         i2o_status_block *sb;
104
105         list_for_each_entry(i2o_dev, &c->devices, list)
106             if (i2o_dev->lct_data.class_id == I2O_CLASS_BUS_ADAPTER_PORT) {
107                 if (i2o_parm_field_get(i2o_dev, 0x0000, 0, &type, 1)
108                    && (type == 0x01))   /* SCSI bus */
109                         max_channel++;
110         }
111
112         if (!max_channel) {
113                 osm_warn("no channels found on %s\n", c->name);
114                 return ERR_PTR(-EFAULT);
115         }
116
117         size = max_channel * sizeof(struct i2o_device *)
118             + sizeof(struct i2o_scsi_host);
119
120         scsi_host = scsi_host_alloc(&i2o_scsi_host_template, size);
121         if (!scsi_host) {
122                 osm_warn("Could not allocate SCSI host\n");
123                 return ERR_PTR(-ENOMEM);
124         }
125
126         scsi_host->max_channel = max_channel - 1;
127         scsi_host->max_id = i2o_scsi_max_id;
128         scsi_host->max_lun = i2o_scsi_max_lun;
129         scsi_host->this_id = c->unit;
130
131         sb = c->status_block.virt;
132
133         scsi_host->sg_tablesize = (sb->inbound_frame_size -
134                                    sizeof(struct i2o_message) / 4 - 6) / 2;
135
136         i2o_shost = (struct i2o_scsi_host *)scsi_host->hostdata;
137         i2o_shost->scsi_host = scsi_host;
138         i2o_shost->iop = c;
139
140         i = 0;
141         list_for_each_entry(i2o_dev, &c->devices, list)
142             if (i2o_dev->lct_data.class_id == I2O_CLASS_BUS_ADAPTER_PORT) {
143                 if (i2o_parm_field_get(i2o_dev, 0x0000, 0, &type, 1) || (type == 1))    /* only SCSI bus */
144                         i2o_shost->channel[i++] = i2o_dev;
145
146                 if (i >= max_channel)
147                         break;
148         }
149
150         return i2o_shost;
151 };
152
153 /**
154  *      i2o_scsi_get_host - Get an I2O SCSI host
155  *      @c: I2O controller to for which to get the SCSI host
156  *
157  *      If the I2O controller already exists as SCSI host, the SCSI host
158  *      is returned, otherwise the I2O controller is added to the SCSI
159  *      core.
160  *
161  *      Returns pointer to the I2O SCSI host on success or NULL on failure.
162  */
163 static struct i2o_scsi_host *i2o_scsi_get_host(struct i2o_controller *c)
164 {
165         return c->driver_data[i2o_scsi_driver.context];
166 };
167
168 /**
169  *      i2o_scsi_remove - Remove I2O device from SCSI core
170  *      @dev: device which should be removed
171  *
172  *      Removes the I2O device from the SCSI core again.
173  *
174  *      Returns 0 on success.
175  */
176 static int i2o_scsi_remove(struct device *dev)
177 {
178         struct i2o_device *i2o_dev = to_i2o_device(dev);
179         struct i2o_controller *c = i2o_dev->iop;
180         struct i2o_scsi_host *i2o_shost;
181         struct scsi_device *scsi_dev;
182
183         osm_info("device removed (TID: %03x)\n", i2o_dev->lct_data.tid);
184
185         i2o_shost = i2o_scsi_get_host(c);
186
187         shost_for_each_device(scsi_dev, i2o_shost->scsi_host)
188             if (scsi_dev->hostdata == i2o_dev) {
189                 scsi_remove_device(scsi_dev);
190                 scsi_device_put(scsi_dev);
191                 break;
192         }
193
194         return 0;
195 };
196
197 /**
198  *      i2o_scsi_probe - verify if dev is a I2O SCSI device and install it
199  *      @dev: device to verify if it is a I2O SCSI device
200  *
201  *      Retrieve channel, id and lun for I2O device. If everthing goes well
202  *      register the I2O device as SCSI device on the I2O SCSI controller.
203  *
204  *      Returns 0 on success or negative error code on failure.
205  */
206 static int i2o_scsi_probe(struct device *dev)
207 {
208         struct i2o_device *i2o_dev = to_i2o_device(dev);
209         struct i2o_controller *c = i2o_dev->iop;
210         struct i2o_scsi_host *i2o_shost;
211         struct Scsi_Host *scsi_host;
212         struct i2o_device *parent;
213         struct scsi_device *scsi_dev;
214         u32 id;
215         u64 lun;
216         int channel = -1;
217         int i;
218
219         i2o_shost = i2o_scsi_get_host(c);
220         if (!i2o_shost)
221                 return -EFAULT;
222
223         scsi_host = i2o_shost->scsi_host;
224
225         if (i2o_parm_field_get(i2o_dev, 0, 3, &id, 4) < 0)
226                 return -EFAULT;
227
228         if (id >= scsi_host->max_id) {
229                 osm_warn("SCSI device id (%d) >= max_id of I2O host (%d)", id,
230                          scsi_host->max_id);
231                 return -EFAULT;
232         }
233
234         if (i2o_parm_field_get(i2o_dev, 0, 4, &lun, 8) < 0)
235                 return -EFAULT;
236         if (lun >= scsi_host->max_lun) {
237                 osm_warn("SCSI device id (%d) >= max_lun of I2O host (%d)",
238                          (unsigned int)lun, scsi_host->max_lun);
239                 return -EFAULT;
240         }
241
242         parent = i2o_iop_find_device(c, i2o_dev->lct_data.parent_tid);
243         if (!parent) {
244                 osm_warn("can not find parent of device %03x\n",
245                          i2o_dev->lct_data.tid);
246                 return -EFAULT;
247         }
248
249         for (i = 0; i <= i2o_shost->scsi_host->max_channel; i++)
250                 if (i2o_shost->channel[i] == parent)
251                         channel = i;
252
253         if (channel == -1) {
254                 osm_warn("can not find channel of device %03x\n",
255                          i2o_dev->lct_data.tid);
256                 return -EFAULT;
257         }
258
259         scsi_dev =
260             __scsi_add_device(i2o_shost->scsi_host, channel, id, lun, i2o_dev);
261
262         if (!scsi_dev) {
263                 osm_warn("can not add SCSI device %03x\n",
264                          i2o_dev->lct_data.tid);
265                 return -EFAULT;
266         }
267
268         osm_info("device added (TID: %03x) channel: %d, id: %d, lun: %d\n",
269                  i2o_dev->lct_data.tid, channel, id, (unsigned int)lun);
270
271         return 0;
272 };
273
274 static const char *i2o_scsi_info(struct Scsi_Host *SChost)
275 {
276         struct i2o_scsi_host *hostdata;
277         hostdata = (struct i2o_scsi_host *)SChost->hostdata;
278         return hostdata->iop->name;
279 }
280
281 /**
282  *      i2o_scsi_reply - SCSI OSM message reply handler
283  *      @c: controller issuing the reply
284  *      @m: message id for flushing
285  *      @msg: the message from the controller
286  *
287  *      Process reply messages (interrupts in normal scsi controller think).
288  *      We can get a variety of messages to process. The normal path is
289  *      scsi command completions. We must also deal with IOP failures,
290  *      the reply to a bus reset and the reply to a LUN query.
291  *
292  *      Returns 0 on success and if the reply should not be flushed or > 0
293  *      on success and if the reply should be flushed. Returns negative error
294  *      code on failure and if the reply should be flushed.
295  */
296 static int i2o_scsi_reply(struct i2o_controller *c, u32 m,
297                           struct i2o_message *msg)
298 {
299         struct scsi_cmnd *cmd;
300         struct device *dev;
301         u8 as, ds, st;
302
303         cmd = i2o_cntxt_list_get(c, le32_to_cpu(msg->u.s.tcntxt));
304
305         if (msg->u.head[0] & (1 << 13)) {
306                 struct i2o_message __iomem *pmsg;       /* preserved message */
307                 u32 pm;
308                 int err = DID_ERROR;
309
310                 pm = le32_to_cpu(msg->body[3]);
311
312                 pmsg = i2o_msg_in_to_virt(c, pm);
313
314                 osm_err("IOP fail.\n");
315                 osm_err("From %d To %d Cmd %d.\n",
316                         (msg->u.head[1] >> 12) & 0xFFF,
317                         msg->u.head[1] & 0xFFF, msg->u.head[1] >> 24);
318                 osm_err("Failure Code %d.\n", msg->body[0] >> 24);
319                 if (msg->body[0] & (1 << 16))
320                         osm_err("Format error.\n");
321                 if (msg->body[0] & (1 << 17))
322                         osm_err("Path error.\n");
323                 if (msg->body[0] & (1 << 18))
324                         osm_err("Path State.\n");
325                 if (msg->body[0] & (1 << 18))
326                 {
327                         osm_err("Congestion.\n");
328                         err = DID_BUS_BUSY;
329                 }
330
331                 osm_debug("Failing message is %p.\n", pmsg);
332
333                 cmd = i2o_cntxt_list_get(c, readl(&pmsg->u.s.tcntxt));
334                 if (!cmd)
335                         return 1;
336
337                 cmd->result = err << 16;
338                 cmd->scsi_done(cmd);
339
340                 /* Now flush the message by making it a NOP */
341                 i2o_msg_nop(c, pm);
342
343                 return 1;
344         }
345
346         /*
347          *      Low byte is device status, next is adapter status,
348          *      (then one byte reserved), then request status.
349          */
350         ds = (u8) le32_to_cpu(msg->body[0]);
351         as = (u8) (le32_to_cpu(msg->body[0]) >> 8);
352         st = (u8) (le32_to_cpu(msg->body[0]) >> 24);
353
354         /*
355          *      Is this a control request coming back - eg an abort ?
356          */
357
358         if (!cmd) {
359                 if (st)
360                         osm_warn("SCSI abort: %08X", le32_to_cpu(msg->body[0]));
361                 osm_info("SCSI abort completed.\n");
362                 return -EFAULT;
363         }
364
365         osm_debug("Completed %ld\n", cmd->serial_number);
366
367         if (st) {
368                 u32 count, error;
369                 /* An error has occurred */
370
371                 switch (st) {
372                 case 0x06:
373                         count = le32_to_cpu(msg->body[1]);
374                         if (count < cmd->underflow) {
375                                 int i;
376
377                                 osm_err("SCSI underflow 0x%08X 0x%08X\n", count,
378                                         cmd->underflow);
379                                 osm_debug("Cmd: ");
380                                 for (i = 0; i < 15; i++)
381                                         pr_debug("%02X ", cmd->cmnd[i]);
382                                 pr_debug(".\n");
383                                 cmd->result = (DID_ERROR << 16);
384                         }
385                         break;
386
387                 default:
388                         error = le32_to_cpu(msg->body[0]);
389
390                         osm_err("SCSI error %08x\n", error);
391
392                         if ((error & 0xff) == 0x02 /*CHECK_CONDITION */ ) {
393                                 int i;
394                                 u32 len = sizeof(cmd->sense_buffer);
395                                 len = (len > 40) ? 40 : len;
396                                 // Copy over the sense data
397                                 memcpy(cmd->sense_buffer, (void *)&msg->body[3],
398                                        len);
399                                 for (i = 0; i <= len; i++)
400                                         osm_info("%02x\n",
401                                                  cmd->sense_buffer[i]);
402                                 if (cmd->sense_buffer[0] == 0x70
403                                     && cmd->sense_buffer[2] == DATA_PROTECT) {
404                                         /* This is to handle an array failed */
405                                         cmd->result = (DID_TIME_OUT << 16);
406                                         printk(KERN_WARNING "%s: SCSI Data "
407                                                "Protect-Device (%d,%d,%d) "
408                                                "hba_status=0x%x, dev_status="
409                                                "0x%x, cmd=0x%x\n", c->name,
410                                                (u32) cmd->device->channel,
411                                                (u32) cmd->device->id,
412                                                (u32) cmd->device->lun,
413                                                (error >> 8) & 0xff,
414                                                error & 0xff, cmd->cmnd[0]);
415                                 } else
416                                         cmd->result = (DID_ERROR << 16);
417
418                                 break;
419                         }
420
421                         switch (as) {
422                         case 0x0E:
423                                 /* SCSI Reset */
424                                 cmd->result = DID_RESET << 16;
425                                 break;
426
427                         case 0x0F:
428                                 cmd->result = DID_PARITY << 16;
429                                 break;
430
431                         default:
432                                 cmd->result = DID_ERROR << 16;
433                                 break;
434                         }
435
436                         break;
437                 }
438
439                 cmd->scsi_done(cmd);
440                 return 1;
441         }
442
443         cmd->result = DID_OK << 16 | ds;
444
445         dev = &c->pdev->dev;
446         if (cmd->use_sg)
447                 dma_unmap_sg(dev, (struct scatterlist *)cmd->buffer,
448                              cmd->use_sg, cmd->sc_data_direction);
449         else if (cmd->request_bufflen)
450                 dma_unmap_single(dev, (dma_addr_t) ((long)cmd->SCp.ptr),
451                                  cmd->request_bufflen, cmd->sc_data_direction);
452
453         cmd->scsi_done(cmd);
454
455         return 1;
456 };
457
458 /**
459  *      i2o_scsi_notify_controller_add - Retrieve notifications of added
460  *                                       controllers
461  *      @c: the controller which was added
462  *
463  *      If a I2O controller is added, we catch the notification to add a
464  *      corresponding Scsi_Host.
465  */
466 static void i2o_scsi_notify_controller_add(struct i2o_controller *c)
467 {
468         struct i2o_scsi_host *i2o_shost;
469         int rc;
470
471         i2o_shost = i2o_scsi_host_alloc(c);
472         if (IS_ERR(i2o_shost)) {
473                 osm_err("Could not initialize SCSI host\n");
474                 return;
475         }
476
477         rc = scsi_add_host(i2o_shost->scsi_host, &c->device);
478         if (rc) {
479                 osm_err("Could not add SCSI host\n");
480                 scsi_host_put(i2o_shost->scsi_host);
481                 return;
482         }
483
484         c->driver_data[i2o_scsi_driver.context] = i2o_shost;
485
486         osm_debug("new I2O SCSI host added\n");
487 };
488
489 /**
490  *      i2o_scsi_notify_controller_remove - Retrieve notifications of removed
491  *                                          controllers
492  *      @c: the controller which was removed
493  *
494  *      If a I2O controller is removed, we catch the notification to remove the
495  *      corresponding Scsi_Host.
496  */
497 static void i2o_scsi_notify_controller_remove(struct i2o_controller *c)
498 {
499         struct i2o_scsi_host *i2o_shost;
500         i2o_shost = i2o_scsi_get_host(c);
501         if (!i2o_shost)
502                 return;
503
504         c->driver_data[i2o_scsi_driver.context] = NULL;
505
506         scsi_remove_host(i2o_shost->scsi_host);
507         scsi_host_put(i2o_shost->scsi_host);
508         osm_debug("I2O SCSI host removed\n");
509 };
510
511 /* SCSI OSM driver struct */
512 static struct i2o_driver i2o_scsi_driver = {
513         .name = OSM_NAME,
514         .reply = i2o_scsi_reply,
515         .classes = i2o_scsi_class_id,
516         .notify_controller_add = i2o_scsi_notify_controller_add,
517         .notify_controller_remove = i2o_scsi_notify_controller_remove,
518         .driver = {
519                    .probe = i2o_scsi_probe,
520                    .remove = i2o_scsi_remove,
521                    },
522 };
523
524 /**
525  *      i2o_scsi_queuecommand - queue a SCSI command
526  *      @SCpnt: scsi command pointer
527  *      @done: callback for completion
528  *
529  *      Issue a scsi command asynchronously. Return 0 on success or 1 if
530  *      we hit an error (normally message queue congestion). The only
531  *      minor complication here is that I2O deals with the device addressing
532  *      so we have to map the bus/dev/lun back to an I2O handle as well
533  *      as faking absent devices ourself.
534  *
535  *      Locks: takes the controller lock on error path only
536  */
537
538 static int i2o_scsi_queuecommand(struct scsi_cmnd *SCpnt,
539                                  void (*done) (struct scsi_cmnd *))
540 {
541         struct i2o_controller *c;
542         struct Scsi_Host *host;
543         struct i2o_device *i2o_dev;
544         struct device *dev;
545         int tid;
546         struct i2o_message __iomem *msg;
547         u32 m;
548         u32 scsi_flags, sg_flags;
549         u32 __iomem *mptr;
550         u32 __iomem *lenptr;
551         u32 len;
552         int i;
553
554         /*
555          *      Do the incoming paperwork
556          */
557
558         i2o_dev = SCpnt->device->hostdata;
559         host = SCpnt->device->host;
560         c = i2o_dev->iop;
561         dev = &c->pdev->dev;
562
563         SCpnt->scsi_done = done;
564
565         if (unlikely(!i2o_dev)) {
566                 osm_warn("no I2O device in request\n");
567                 SCpnt->result = DID_NO_CONNECT << 16;
568                 done(SCpnt);
569                 return 0;
570         }
571
572         tid = i2o_dev->lct_data.tid;
573
574         osm_debug("qcmd: Tid = %03x\n", tid);
575         osm_debug("Real scsi messages.\n");
576
577         /*
578          *      Obtain an I2O message. If there are none free then
579          *      throw it back to the scsi layer
580          */
581
582         m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
583         if (m == I2O_QUEUE_EMPTY)
584                 return SCSI_MLQUEUE_HOST_BUSY;
585
586         mptr = &msg->body[0];
587
588         /*
589          *      Put together a scsi execscb message
590          */
591
592         switch (SCpnt->sc_data_direction) {
593         case PCI_DMA_NONE:
594                 scsi_flags = 0x00000000;        // DATA NO XFER
595                 sg_flags = 0x00000000;
596                 break;
597
598         case PCI_DMA_TODEVICE:
599                 scsi_flags = 0x80000000;        // DATA OUT (iop-->dev)
600                 sg_flags = 0x14000000;
601                 break;
602
603         case PCI_DMA_FROMDEVICE:
604                 scsi_flags = 0x40000000;        // DATA IN  (iop<--dev)
605                 sg_flags = 0x10000000;
606                 break;
607
608         default:
609                 /* Unknown - kill the command */
610                 SCpnt->result = DID_NO_CONNECT << 16;
611                 done(SCpnt);
612                 return 0;
613         }
614
615         writel(I2O_CMD_SCSI_EXEC << 24 | HOST_TID << 12 | tid, &msg->u.head[1]);
616         writel(i2o_scsi_driver.context, &msg->u.s.icntxt);
617
618         /* We want the SCSI control block back */
619         writel(i2o_cntxt_list_add(c, SCpnt), &msg->u.s.tcntxt);
620
621         /* LSI_920_PCI_QUIRK
622          *
623          *      Intermittant observations of msg frame word data corruption
624          *      observed on msg[4] after:
625          *        WRITE, READ-MODIFY-WRITE
626          *      operations.  19990606 -sralston
627          *
628          *      (Hence we build this word via tag. Its good practice anyway
629          *       we don't want fetches over PCI needlessly)
630          */
631
632         /* Attach tags to the devices */
633         /*
634            if(SCpnt->device->tagged_supported) {
635            if(SCpnt->tag == HEAD_OF_QUEUE_TAG)
636            scsi_flags |= 0x01000000;
637            else if(SCpnt->tag == ORDERED_QUEUE_TAG)
638            scsi_flags |= 0x01800000;
639            }
640          */
641
642         /* Direction, disconnect ok, tag, CDBLen */
643         writel(scsi_flags | 0x20200000 | SCpnt->cmd_len, mptr ++);
644
645         /* Write SCSI command into the message - always 16 byte block */
646         memcpy_toio(mptr, SCpnt->cmnd, 16);
647         mptr += 4;
648         lenptr = mptr++;        /* Remember me - fill in when we know */
649
650         /* Now fill in the SGList and command */
651         if (SCpnt->use_sg) {
652                 struct scatterlist *sg;
653                 int sg_count;
654
655                 sg = SCpnt->request_buffer;
656                 len = 0;
657
658                 sg_count = dma_map_sg(dev, sg, SCpnt->use_sg,
659                                       SCpnt->sc_data_direction);
660
661                 if (unlikely(sg_count <= 0))
662                         return -ENOMEM;
663
664                 for (i = SCpnt->use_sg; i > 0; i--) {
665                         if (i == 1)
666                                 sg_flags |= 0xC0000000;
667                         writel(sg_flags | sg_dma_len(sg), mptr++);
668                         writel(sg_dma_address(sg), mptr++);
669                         len += sg_dma_len(sg);
670                         sg++;
671                 }
672
673                 writel(len, lenptr);
674         } else {
675                 len = SCpnt->request_bufflen;
676
677                 writel(len, lenptr);
678
679                 if (len > 0) {
680                         dma_addr_t dma_addr;
681
682                         dma_addr = dma_map_single(dev, SCpnt->request_buffer,
683                                                   SCpnt->request_bufflen,
684                                                   SCpnt->sc_data_direction);
685                         if (!dma_addr)
686                                 return -ENOMEM;
687
688                         SCpnt->SCp.ptr = (void *)(unsigned long)dma_addr;
689                         sg_flags |= 0xC0000000;
690                         writel(sg_flags | SCpnt->request_bufflen, mptr++);
691                         writel(dma_addr, mptr++);
692                 }
693         }
694
695         /* Stick the headers on */
696         writel((mptr - &msg->u.head[0]) << 16 | SGL_OFFSET_10, &msg->u.head[0]);
697
698         /* Queue the message */
699         i2o_msg_post(c, m);
700
701         osm_debug("Issued %ld\n", SCpnt->serial_number);
702
703         return 0;
704 };
705
706 /**
707  *      i2o_scsi_abort - abort a running command
708  *      @SCpnt: command to abort
709  *
710  *      Ask the I2O controller to abort a command. This is an asynchrnous
711  *      process and our callback handler will see the command complete with an
712  *      aborted message if it succeeds.
713  *
714  *      Returns 0 if the command is successfully aborted or negative error code
715  *      on failure.
716  */
717 static int i2o_scsi_abort(struct scsi_cmnd *SCpnt)
718 {
719         struct i2o_device *i2o_dev;
720         struct i2o_controller *c;
721         struct i2o_message __iomem *msg;
722         u32 m;
723         int tid;
724         int status = FAILED;
725
726         osm_warn("Aborting command block.\n");
727
728         i2o_dev = SCpnt->device->hostdata;
729         c = i2o_dev->iop;
730         tid = i2o_dev->lct_data.tid;
731
732         m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
733         if (m == I2O_QUEUE_EMPTY)
734                 return SCSI_MLQUEUE_HOST_BUSY;
735
736         writel(FIVE_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
737         writel(I2O_CMD_SCSI_ABORT << 24 | HOST_TID << 12 | tid,
738                &msg->u.head[1]);
739         writel(i2o_cntxt_list_get_ptr(c, SCpnt), &msg->body[0]);
740
741         if (i2o_msg_post_wait(c, m, I2O_TIMEOUT_SCSI_SCB_ABORT))
742                 status = SUCCESS;
743
744         return status;
745 }
746
747 /**
748  *      i2o_scsi_bios_param     -       Invent disk geometry
749  *      @sdev: scsi device
750  *      @dev: block layer device
751  *      @capacity: size in sectors
752  *      @ip: geometry array
753  *
754  *      This is anyones guess quite frankly. We use the same rules everyone
755  *      else appears to and hope. It seems to work.
756  */
757
758 static int i2o_scsi_bios_param(struct scsi_device *sdev,
759                                struct block_device *dev, sector_t capacity,
760                                int *ip)
761 {
762         int size;
763
764         size = capacity;
765         ip[0] = 64;             /* heads                        */
766         ip[1] = 32;             /* sectors                      */
767         if ((ip[2] = size >> 11) > 1024) {      /* cylinders, test for big disk */
768                 ip[0] = 255;    /* heads                        */
769                 ip[1] = 63;     /* sectors                      */
770                 ip[2] = size / (255 * 63);      /* cylinders                    */
771         }
772         return 0;
773 }
774
775 static struct scsi_host_template i2o_scsi_host_template = {
776         .proc_name = OSM_NAME,
777         .name = OSM_DESCRIPTION,
778         .info = i2o_scsi_info,
779         .queuecommand = i2o_scsi_queuecommand,
780         .eh_abort_handler = i2o_scsi_abort,
781         .bios_param = i2o_scsi_bios_param,
782         .can_queue = I2O_SCSI_CAN_QUEUE,
783         .sg_tablesize = 8,
784         .cmd_per_lun = 6,
785         .use_clustering = ENABLE_CLUSTERING,
786 };
787
788 /**
789  *      i2o_scsi_init - SCSI OSM initialization function
790  *
791  *      Register SCSI OSM into I2O core.
792  *
793  *      Returns 0 on success or negative error code on failure.
794  */
795 static int __init i2o_scsi_init(void)
796 {
797         int rc;
798
799         printk(KERN_INFO OSM_DESCRIPTION " v" OSM_VERSION "\n");
800
801         /* Register SCSI OSM into I2O core */
802         rc = i2o_driver_register(&i2o_scsi_driver);
803         if (rc) {
804                 osm_err("Could not register SCSI driver\n");
805                 return rc;
806         }
807
808         return 0;
809 };
810
811 /**
812  *      i2o_scsi_exit - SCSI OSM exit function
813  *
814  *      Unregisters SCSI OSM from I2O core.
815  */
816 static void __exit i2o_scsi_exit(void)
817 {
818         /* Unregister I2O SCSI OSM from I2O core */
819         i2o_driver_unregister(&i2o_scsi_driver);
820 };
821
822 MODULE_AUTHOR("Red Hat Software");
823 MODULE_LICENSE("GPL");
824 MODULE_DESCRIPTION(OSM_DESCRIPTION);
825 MODULE_VERSION(OSM_VERSION);
826
827 module_init(i2o_scsi_init);
828 module_exit(i2o_scsi_exit);