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