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