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