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