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