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