blob: fef53b509a61c2139c7b49fd8a28b70a08c9efac [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>
Markus Lidelb2aaee32005-06-23 22:02:19 -070058#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
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>
Markus Lidelb2aaee32005-06-23 22:02:19 -070069#include <scsi/scsi_request.h>
70#include <scsi/sg.h>
71#include <scsi/sg_request.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73#define OSM_NAME "scsi-osm"
Markus Lidelb2aaee32005-06-23 22:02:19 -070074#define OSM_VERSION "1.282"
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#define OSM_DESCRIPTION "I2O SCSI Peripheral OSM"
76
77static struct i2o_driver i2o_scsi_driver;
78
Markus Lidelb2aaee32005-06-23 22:02:19 -070079static unsigned int i2o_scsi_max_id = 16;
80static unsigned int i2o_scsi_max_lun = 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82struct i2o_scsi_host {
83 struct Scsi_Host *scsi_host; /* pointer to the SCSI host */
84 struct i2o_controller *iop; /* pointer to the I2O controller */
Markus Lidelb2aaee32005-06-23 22:02:19 -070085 unsigned int lun; /* lun's used for block devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 struct i2o_device *channel[0]; /* channel->i2o_dev mapping table */
87};
88
89static struct scsi_host_template i2o_scsi_host_template;
90
91#define I2O_SCSI_CAN_QUEUE 4
92
93/* SCSI OSM class handling definition */
94static struct i2o_class_id i2o_scsi_class_id[] = {
95 {I2O_CLASS_SCSI_PERIPHERAL},
96 {I2O_CLASS_END}
97};
98
99static 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;
Markus Lidelb2aaee32005-06-23 22:02:19 -0700108 u16 body_size = 6;
109
110#ifdef CONFIG_I2O_EXT_ADAPTEC
111 if (c->adaptec)
112 body_size = 8;
113#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115 list_for_each_entry(i2o_dev, &c->devices, list)
Markus Lidelf10378f2005-06-23 22:02:16 -0700116 if (i2o_dev->lct_data.class_id == I2O_CLASS_BUS_ADAPTER) {
Markus Lidel61fbfa82005-06-23 22:02:11 -0700117 if (i2o_parm_field_get(i2o_dev, 0x0000, 0, &type, 1)
Markus Lidelb2aaee32005-06-23 22:02:19 -0700118 && (type == 0x01)) /* SCSI bus */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 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;
Markus Lidelb2aaee32005-06-23 22:02:19 -0700140 scsi_host->sg_tablesize = i2o_sg_tablesize(c, body_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 i2o_shost = (struct i2o_scsi_host *)scsi_host->hostdata;
143 i2o_shost->scsi_host = scsi_host;
144 i2o_shost->iop = c;
Markus Lidelb2aaee32005-06-23 22:02:19 -0700145 i2o_shost->lun = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 i = 0;
148 list_for_each_entry(i2o_dev, &c->devices, list)
Markus Lidelf10378f2005-06-23 22:02:16 -0700149 if (i2o_dev->lct_data.class_id == I2O_CLASS_BUS_ADAPTER) {
Markus Lidelb2aaee32005-06-23 22:02:19 -0700150 if (i2o_parm_field_get(i2o_dev, 0x0000, 0, &type, 1)
151 && (type == 0x01)) /* only SCSI bus */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 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 */
171static 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 */
184static 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
Markus Lidelf88e1192005-06-23 22:02:14 -0700191 osm_info("device removed (TID: %03x)\n", i2o_dev->lct_data.tid);
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 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) {
Markus Lidelf10378f2005-06-23 22:02:16 -0700197 sysfs_remove_link(&i2o_dev->device.kobj, "scsi");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 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 */
215static 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;
Markus Lidelb2aaee32005-06-23 22:02:19 -0700223 u32 id = -1;
224 u64 lun = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 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
Markus Lidelb2aaee32005-06-23 22:02:19 -0700234 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:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return -EFAULT;
Markus Lidelb2aaee32005-06-23 22:02:19 -0700277 }
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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 scsi_dev =
298 __scsi_add_device(i2o_shost->scsi_host, channel, id, lun, i2o_dev);
299
Markus Lidelf10378f2005-06-23 22:02:16 -0700300 if (IS_ERR(scsi_dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 osm_warn("can not add SCSI device %03x\n",
302 i2o_dev->lct_data.tid);
Markus Lidelf10378f2005-06-23 22:02:16 -0700303 return PTR_ERR(scsi_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
305
Markus Lidelb2aaee32005-06-23 22:02:19 -0700306 sysfs_create_link(&i2o_dev->device.kobj, &scsi_dev->sdev_gendev.kobj,
307 "scsi");
Markus Lidelf10378f2005-06-23 22:02:16 -0700308
Markus Lidelf88e1192005-06-23 22:02:14 -0700309 osm_info("device added (TID: %03x) channel: %d, id: %d, lun: %d\n",
310 i2o_dev->lct_data.tid, channel, id, (unsigned int)lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 return 0;
313};
314
315static 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 */
337static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 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
Markus Lidelf88e1192005-06-23 22:02:14 -0700494 cmd->scsi_done(cmd);
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 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 */
507static 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 */
538static 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);
Markus Lidelf88e1192005-06-23 22:02:14 -0700549 osm_debug("I2O SCSI host removed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550};
551
552/* SCSI OSM driver struct */
553static 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
579static int i2o_scsi_queuecommand(struct scsi_cmnd *SCpnt,
580 void (*done) (struct scsi_cmnd *))
581{
582 struct i2o_controller *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 struct i2o_device *i2o_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 int tid;
585 struct i2o_message __iomem *msg;
586 u32 m;
Markus Lidelf10378f2005-06-23 22:02:16 -0700587 /*
588 * ENABLE_DISCONNECT
589 * SIMPLE_TAG
590 * RETURN_SENSE_DATA_IN_REPLY_MESSAGE_FRAME
591 */
592 u32 scsi_flags = 0x20a00000;
Markus Lidelb2aaee32005-06-23 22:02:19 -0700593 u32 sgl_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 u32 __iomem *mptr;
Markus Lidelb2aaee32005-06-23 22:02:19 -0700595 u32 cmd = I2O_CMD_SCSI_EXEC << 24;
596 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 /*
599 * Do the incoming paperwork
600 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 i2o_dev = SCpnt->device->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 c = i2o_dev->iop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
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);
Markus Lidelb2aaee32005-06-23 22:02:19 -0700610 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 }
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 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 * Put together a scsi execscb message
620 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 switch (SCpnt->sc_data_direction) {
622 case PCI_DMA_NONE:
Markus Lidelf10378f2005-06-23 22:02:16 -0700623 /* DATA NO XFER */
Markus Lidelb2aaee32005-06-23 22:02:19 -0700624 sgl_offset = SGL_OFFSET_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 break;
626
627 case PCI_DMA_TODEVICE:
Markus Lidelf10378f2005-06-23 22:02:16 -0700628 /* DATA OUT (iop-->dev) */
629 scsi_flags |= 0x80000000;
Markus Lidelb2aaee32005-06-23 22:02:19 -0700630 sgl_offset = SGL_OFFSET_10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 break;
632
633 case PCI_DMA_FROMDEVICE:
Markus Lidelf10378f2005-06-23 22:02:16 -0700634 /* DATA IN (iop<--dev) */
635 scsi_flags |= 0x40000000;
Markus Lidelb2aaee32005-06-23 22:02:19 -0700636 sgl_offset = SGL_OFFSET_10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 break;
638
639 default:
640 /* Unknown - kill the command */
641 SCpnt->result = DID_NO_CONNECT << 16;
642 done(SCpnt);
Markus Lidelb2aaee32005-06-23 22:02:19 -0700643 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 }
645
Markus Lidelb2aaee32005-06-23 22:02:19 -0700646 /*
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]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 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
Markus Lidelf10378f2005-06-23 22:02:16 -0700724 writel(scsi_flags | SCpnt->cmd_len, mptr++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726 /* Write SCSI command into the message - always 16 byte block */
727 memcpy_toio(mptr, SCpnt->cmnd, 16);
728 mptr += 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Markus Lidelb2aaee32005-06-23 22:02:19 -0700730 if (sgl_offset != SGL_OFFSET_0) {
731 /* write size of data addressed by SGL */
732 writel(SCpnt->request_bufflen, mptr++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Markus Lidelb2aaee32005-06-23 22:02:19 -0700734 /* 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;
Markus Lidelf88e1192005-06-23 22:02:14 -0700747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 }
749
750 /* Stick the headers on */
Markus Lidelb2aaee32005-06-23 22:02:19 -0700751 writel(I2O_MESSAGE_SIZE(mptr - &msg->u.head[0]) | sgl_offset,
752 &msg->u.head[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 /* Queue the message */
755 i2o_msg_post(c, m);
756
757 osm_debug("Issued %ld\n", SCpnt->serial_number);
758
759 return 0;
Markus Lidelb2aaee32005-06-23 22:02:19 -0700760
761 nomem:
762 rc = -ENOMEM;
763 i2o_msg_nop(c, m);
764
765 exit:
766 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767};
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 */
780static 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
821static 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
838static 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 */
858static 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 */
879static void __exit i2o_scsi_exit(void)
880{
881 /* Unregister I2O SCSI OSM from I2O core */
882 i2o_driver_unregister(&i2o_scsi_driver);
883};
884
885MODULE_AUTHOR("Red Hat Software");
886MODULE_LICENSE("GPL");
887MODULE_DESCRIPTION(OSM_DESCRIPTION);
888MODULE_VERSION(OSM_VERSION);
889
890module_init(i2o_scsi_init);
891module_exit(i2o_scsi_exit);