blob: 1045c8a518bbbdb453ba9e40a951646b48e26bf2 [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
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>
Markus Lidelb2aaee32005-06-23 22:02:19 -070057#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
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>
Markus Lidelb2aaee32005-06-23 22:02:19 -070068#include <scsi/sg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70#define OSM_NAME "scsi-osm"
Markus Lidel2e1973a2006-01-06 00:19:32 -080071#define OSM_VERSION "1.316"
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define OSM_DESCRIPTION "I2O SCSI Peripheral OSM"
73
74static struct i2o_driver i2o_scsi_driver;
75
Markus Lidelb2aaee32005-06-23 22:02:19 -070076static unsigned int i2o_scsi_max_id = 16;
77static unsigned int i2o_scsi_max_lun = 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79struct i2o_scsi_host {
80 struct Scsi_Host *scsi_host; /* pointer to the SCSI host */
81 struct i2o_controller *iop; /* pointer to the I2O controller */
Markus Lidelb2aaee32005-06-23 22:02:19 -070082 unsigned int lun; /* lun's used for block devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 struct i2o_device *channel[0]; /* channel->i2o_dev mapping table */
84};
85
86static struct scsi_host_template i2o_scsi_host_template;
87
88#define I2O_SCSI_CAN_QUEUE 4
89
90/* SCSI OSM class handling definition */
91static struct i2o_class_id i2o_scsi_class_id[] = {
92 {I2O_CLASS_SCSI_PERIPHERAL},
93 {I2O_CLASS_END}
94};
95
96static struct i2o_scsi_host *i2o_scsi_host_alloc(struct i2o_controller *c)
97{
98 struct i2o_scsi_host *i2o_shost;
99 struct i2o_device *i2o_dev;
100 struct Scsi_Host *scsi_host;
101 int max_channel = 0;
102 u8 type;
103 int i;
104 size_t size;
Markus Lidelb2aaee32005-06-23 22:02:19 -0700105 u16 body_size = 6;
106
107#ifdef CONFIG_I2O_EXT_ADAPTEC
108 if (c->adaptec)
109 body_size = 8;
110#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112 list_for_each_entry(i2o_dev, &c->devices, list)
Markus Lidelf10378f2005-06-23 22:02:16 -0700113 if (i2o_dev->lct_data.class_id == I2O_CLASS_BUS_ADAPTER) {
Markus Lidel793fd152006-01-06 00:19:30 -0800114 if (!i2o_parm_field_get(i2o_dev, 0x0000, 0, &type, 1)
Markus Lidelb2aaee32005-06-23 22:02:19 -0700115 && (type == 0x01)) /* SCSI bus */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 max_channel++;
117 }
118
119 if (!max_channel) {
120 osm_warn("no channels found on %s\n", c->name);
121 return ERR_PTR(-EFAULT);
122 }
123
124 size = max_channel * sizeof(struct i2o_device *)
125 + sizeof(struct i2o_scsi_host);
126
127 scsi_host = scsi_host_alloc(&i2o_scsi_host_template, size);
128 if (!scsi_host) {
129 osm_warn("Could not allocate SCSI host\n");
130 return ERR_PTR(-ENOMEM);
131 }
132
133 scsi_host->max_channel = max_channel - 1;
134 scsi_host->max_id = i2o_scsi_max_id;
135 scsi_host->max_lun = i2o_scsi_max_lun;
136 scsi_host->this_id = c->unit;
Markus Lidelb2aaee32005-06-23 22:02:19 -0700137 scsi_host->sg_tablesize = i2o_sg_tablesize(c, body_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 i2o_shost = (struct i2o_scsi_host *)scsi_host->hostdata;
140 i2o_shost->scsi_host = scsi_host;
141 i2o_shost->iop = c;
Markus Lidelb2aaee32005-06-23 22:02:19 -0700142 i2o_shost->lun = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 i = 0;
145 list_for_each_entry(i2o_dev, &c->devices, list)
Markus Lidelf10378f2005-06-23 22:02:16 -0700146 if (i2o_dev->lct_data.class_id == I2O_CLASS_BUS_ADAPTER) {
Markus Lidel793fd152006-01-06 00:19:30 -0800147 if (!i2o_parm_field_get(i2o_dev, 0x0000, 0, &type, 1)
Markus Lidelb2aaee32005-06-23 22:02:19 -0700148 && (type == 0x01)) /* only SCSI bus */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 i2o_shost->channel[i++] = i2o_dev;
150
151 if (i >= max_channel)
152 break;
153 }
154
155 return i2o_shost;
156};
157
158/**
159 * i2o_scsi_get_host - Get an I2O SCSI host
160 * @c: I2O controller to for which to get the SCSI host
161 *
162 * If the I2O controller already exists as SCSI host, the SCSI host
163 * is returned, otherwise the I2O controller is added to the SCSI
164 * core.
165 *
166 * Returns pointer to the I2O SCSI host on success or NULL on failure.
167 */
168static struct i2o_scsi_host *i2o_scsi_get_host(struct i2o_controller *c)
169{
170 return c->driver_data[i2o_scsi_driver.context];
171};
172
173/**
174 * i2o_scsi_remove - Remove I2O device from SCSI core
175 * @dev: device which should be removed
176 *
177 * Removes the I2O device from the SCSI core again.
178 *
179 * Returns 0 on success.
180 */
181static int i2o_scsi_remove(struct device *dev)
182{
183 struct i2o_device *i2o_dev = to_i2o_device(dev);
184 struct i2o_controller *c = i2o_dev->iop;
185 struct i2o_scsi_host *i2o_shost;
186 struct scsi_device *scsi_dev;
187
Markus Lidelf88e1192005-06-23 22:02:14 -0700188 osm_info("device removed (TID: %03x)\n", i2o_dev->lct_data.tid);
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 i2o_shost = i2o_scsi_get_host(c);
191
192 shost_for_each_device(scsi_dev, i2o_shost->scsi_host)
193 if (scsi_dev->hostdata == i2o_dev) {
Markus Lidelf10378f2005-06-23 22:02:16 -0700194 sysfs_remove_link(&i2o_dev->device.kobj, "scsi");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 scsi_remove_device(scsi_dev);
196 scsi_device_put(scsi_dev);
197 break;
198 }
199
200 return 0;
201};
202
203/**
204 * i2o_scsi_probe - verify if dev is a I2O SCSI device and install it
205 * @dev: device to verify if it is a I2O SCSI device
206 *
207 * Retrieve channel, id and lun for I2O device. If everthing goes well
208 * register the I2O device as SCSI device on the I2O SCSI controller.
209 *
210 * Returns 0 on success or negative error code on failure.
211 */
212static int i2o_scsi_probe(struct device *dev)
213{
214 struct i2o_device *i2o_dev = to_i2o_device(dev);
215 struct i2o_controller *c = i2o_dev->iop;
216 struct i2o_scsi_host *i2o_shost;
217 struct Scsi_Host *scsi_host;
218 struct i2o_device *parent;
219 struct scsi_device *scsi_dev;
Markus Lidelb2aaee32005-06-23 22:02:19 -0700220 u32 id = -1;
221 u64 lun = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 int channel = -1;
Jeff Garzik3889b262006-12-06 20:35:31 -0800223 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 i2o_shost = i2o_scsi_get_host(c);
226 if (!i2o_shost)
227 return -EFAULT;
228
229 scsi_host = i2o_shost->scsi_host;
230
Markus Lidelb2aaee32005-06-23 22:02:19 -0700231 switch (i2o_dev->lct_data.class_id) {
232 case I2O_CLASS_RANDOM_BLOCK_STORAGE:
233 case I2O_CLASS_EXECUTIVE:
234#ifdef CONFIG_I2O_EXT_ADAPTEC
235 if (c->adaptec) {
236 u8 type;
237 struct i2o_device *d = i2o_shost->channel[0];
238
Markus Lidel793fd152006-01-06 00:19:30 -0800239 if (!i2o_parm_field_get(d, 0x0000, 0, &type, 1)
Markus Lidelb2aaee32005-06-23 22:02:19 -0700240 && (type == 0x01)) /* SCSI bus */
Markus Lidel793fd152006-01-06 00:19:30 -0800241 if (!i2o_parm_field_get(d, 0x0200, 4, &id, 4)) {
Markus Lidelb2aaee32005-06-23 22:02:19 -0700242 channel = 0;
243 if (i2o_dev->lct_data.class_id ==
244 I2O_CLASS_RANDOM_BLOCK_STORAGE)
Markus Lidel793fd152006-01-06 00:19:30 -0800245 lun =
246 cpu_to_le64(i2o_shost->
247 lun++);
Markus Lidelb2aaee32005-06-23 22:02:19 -0700248 else
249 lun = 0;
250 }
251 }
252#endif
253 break;
254
255 case I2O_CLASS_SCSI_PERIPHERAL:
Markus Lidel793fd152006-01-06 00:19:30 -0800256 if (i2o_parm_field_get(i2o_dev, 0x0000, 3, &id, 4))
Markus Lidelb2aaee32005-06-23 22:02:19 -0700257 return -EFAULT;
258
Markus Lidel793fd152006-01-06 00:19:30 -0800259 if (i2o_parm_field_get(i2o_dev, 0x0000, 4, &lun, 8))
Markus Lidelb2aaee32005-06-23 22:02:19 -0700260 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:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 return -EFAULT;
Markus Lidelb2aaee32005-06-23 22:02:19 -0700276 }
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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Markus Lidel793fd152006-01-06 00:19:30 -0800284 if (le32_to_cpu(id) >= scsi_host->max_id) {
285 osm_warn("SCSI device id (%d) >= max_id of I2O host (%d)",
286 le32_to_cpu(id), scsi_host->max_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return -EFAULT;
288 }
289
Markus Lidel793fd152006-01-06 00:19:30 -0800290 if (le64_to_cpu(lun) >= scsi_host->max_lun) {
291 osm_warn("SCSI device lun (%lu) >= max_lun of I2O host (%d)",
292 (long unsigned int)le64_to_cpu(lun),
293 scsi_host->max_lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 return -EFAULT;
295 }
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 scsi_dev =
Markus Lidel793fd152006-01-06 00:19:30 -0800298 __scsi_add_device(i2o_shost->scsi_host, channel, le32_to_cpu(id),
299 le64_to_cpu(lun), i2o_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Markus Lidelf10378f2005-06-23 22:02:16 -0700301 if (IS_ERR(scsi_dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 osm_warn("can not add SCSI device %03x\n",
303 i2o_dev->lct_data.tid);
Markus Lidelf10378f2005-06-23 22:02:16 -0700304 return PTR_ERR(scsi_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 }
306
Jeff Garzik3889b262006-12-06 20:35:31 -0800307 rc = sysfs_create_link(&i2o_dev->device.kobj,
308 &scsi_dev->sdev_gendev.kobj, "scsi");
309 if (rc)
310 goto err;
Markus Lidelf10378f2005-06-23 22:02:16 -0700311
Markus Lideldcceafe2006-01-06 00:19:32 -0800312 osm_info("device added (TID: %03x) channel: %d, id: %d, lun: %ld\n",
Markus Lidel793fd152006-01-06 00:19:30 -0800313 i2o_dev->lct_data.tid, channel, le32_to_cpu(id),
Markus Lideldcceafe2006-01-06 00:19:32 -0800314 (long unsigned int)le64_to_cpu(lun));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 return 0;
Jeff Garzik3889b262006-12-06 20:35:31 -0800317
318err:
319 scsi_remove_device(scsi_dev);
320 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321};
322
323static const char *i2o_scsi_info(struct Scsi_Host *SChost)
324{
325 struct i2o_scsi_host *hostdata;
326 hostdata = (struct i2o_scsi_host *)SChost->hostdata;
327 return hostdata->iop->name;
328}
329
330/**
331 * i2o_scsi_reply - SCSI OSM message reply handler
332 * @c: controller issuing the reply
333 * @m: message id for flushing
334 * @msg: the message from the controller
335 *
336 * Process reply messages (interrupts in normal scsi controller think).
337 * We can get a variety of messages to process. The normal path is
338 * scsi command completions. We must also deal with IOP failures,
339 * the reply to a bus reset and the reply to a LUN query.
340 *
341 * Returns 0 on success and if the reply should not be flushed or > 0
342 * on success and if the reply should be flushed. Returns negative error
343 * code on failure and if the reply should be flushed.
344 */
345static int i2o_scsi_reply(struct i2o_controller *c, u32 m,
346 struct i2o_message *msg)
347{
348 struct scsi_cmnd *cmd;
Markus Lidel9e875452005-06-23 22:02:21 -0700349 u32 error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 cmd = i2o_cntxt_list_get(c, le32_to_cpu(msg->u.s.tcntxt));
Markus Lidel9e875452005-06-23 22:02:21 -0700353 if (unlikely(!cmd)) {
354 osm_err("NULL reply received!\n");
355 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 }
357
358 /*
359 * Low byte is device status, next is adapter status,
360 * (then one byte reserved), then request status.
361 */
Markus Lidel9e875452005-06-23 22:02:21 -0700362 error = le32_to_cpu(msg->body[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 osm_debug("Completed %ld\n", cmd->serial_number);
365
Markus Lidel9e875452005-06-23 22:02:21 -0700366 cmd->result = error & 0xff;
367 /*
368 * if DeviceStatus is not SCSI_SUCCESS copy over the sense data and let
369 * the SCSI layer handle the error
370 */
371 if (cmd->result)
372 memcpy(cmd->sense_buffer, &msg->body[3],
373 min(sizeof(cmd->sense_buffer), (size_t) 40));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Markus Lidel9e875452005-06-23 22:02:21 -0700375 /* only output error code if AdapterStatus is not HBA_SUCCESS */
376 if ((error >> 8) & 0xff)
377 osm_err("SCSI error %08x\n", error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 dev = &c->pdev->dev;
380 if (cmd->use_sg)
Markus Lidel9e875452005-06-23 22:02:21 -0700381 dma_unmap_sg(dev, cmd->request_buffer, cmd->use_sg,
382 cmd->sc_data_direction);
383 else if (cmd->SCp.dma_handle)
384 dma_unmap_single(dev, cmd->SCp.dma_handle, cmd->request_bufflen,
385 cmd->sc_data_direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Markus Lidelf88e1192005-06-23 22:02:14 -0700387 cmd->scsi_done(cmd);
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return 1;
390};
391
392/**
Markus Lidel9e875452005-06-23 22:02:21 -0700393 * i2o_scsi_notify_device_add - Retrieve notifications of added devices
394 * @i2o_dev: the I2O device which was added
395 *
396 * If a I2O device is added we catch the notification, because I2O classes
397 * other then SCSI peripheral will not be received through
398 * i2o_scsi_probe().
399 */
400static void i2o_scsi_notify_device_add(struct i2o_device *i2o_dev)
401{
402 switch (i2o_dev->lct_data.class_id) {
403 case I2O_CLASS_EXECUTIVE:
404 case I2O_CLASS_RANDOM_BLOCK_STORAGE:
405 i2o_scsi_probe(&i2o_dev->device);
406 break;
407
408 default:
409 break;
410 }
411};
412
413/**
Randy Dunlapd9489fb2006-12-06 20:38:43 -0800414 * i2o_scsi_notify_device_remove - Retrieve notifications of removed devices
Markus Lidel9e875452005-06-23 22:02:21 -0700415 * @i2o_dev: the I2O device which was removed
416 *
417 * If a I2O device is removed, we catch the notification to remove the
418 * corresponding SCSI device.
419 */
420static void i2o_scsi_notify_device_remove(struct i2o_device *i2o_dev)
421{
422 switch (i2o_dev->lct_data.class_id) {
423 case I2O_CLASS_EXECUTIVE:
424 case I2O_CLASS_RANDOM_BLOCK_STORAGE:
425 i2o_scsi_remove(&i2o_dev->device);
426 break;
427
428 default:
429 break;
430 }
431};
432
433/**
Randy Dunlapd9489fb2006-12-06 20:38:43 -0800434 * i2o_scsi_notify_controller_add - Retrieve notifications of added controllers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 * @c: the controller which was added
436 *
437 * If a I2O controller is added, we catch the notification to add a
438 * corresponding Scsi_Host.
439 */
440static void i2o_scsi_notify_controller_add(struct i2o_controller *c)
441{
442 struct i2o_scsi_host *i2o_shost;
443 int rc;
444
445 i2o_shost = i2o_scsi_host_alloc(c);
446 if (IS_ERR(i2o_shost)) {
447 osm_err("Could not initialize SCSI host\n");
448 return;
449 }
450
451 rc = scsi_add_host(i2o_shost->scsi_host, &c->device);
452 if (rc) {
453 osm_err("Could not add SCSI host\n");
454 scsi_host_put(i2o_shost->scsi_host);
455 return;
456 }
457
458 c->driver_data[i2o_scsi_driver.context] = i2o_shost;
459
460 osm_debug("new I2O SCSI host added\n");
461};
462
463/**
Randy Dunlapd9489fb2006-12-06 20:38:43 -0800464 * i2o_scsi_notify_controller_remove - Retrieve notifications of removed controllers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 * @c: the controller which was removed
466 *
467 * If a I2O controller is removed, we catch the notification to remove the
468 * corresponding Scsi_Host.
469 */
470static void i2o_scsi_notify_controller_remove(struct i2o_controller *c)
471{
472 struct i2o_scsi_host *i2o_shost;
473 i2o_shost = i2o_scsi_get_host(c);
474 if (!i2o_shost)
475 return;
476
477 c->driver_data[i2o_scsi_driver.context] = NULL;
478
479 scsi_remove_host(i2o_shost->scsi_host);
480 scsi_host_put(i2o_shost->scsi_host);
Markus Lidelf88e1192005-06-23 22:02:14 -0700481 osm_debug("I2O SCSI host removed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482};
483
484/* SCSI OSM driver struct */
485static struct i2o_driver i2o_scsi_driver = {
486 .name = OSM_NAME,
487 .reply = i2o_scsi_reply,
488 .classes = i2o_scsi_class_id,
Markus Lidel9e875452005-06-23 22:02:21 -0700489 .notify_device_add = i2o_scsi_notify_device_add,
490 .notify_device_remove = i2o_scsi_notify_device_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 .notify_controller_add = i2o_scsi_notify_controller_add,
492 .notify_controller_remove = i2o_scsi_notify_controller_remove,
493 .driver = {
494 .probe = i2o_scsi_probe,
495 .remove = i2o_scsi_remove,
496 },
497};
498
499/**
500 * i2o_scsi_queuecommand - queue a SCSI command
501 * @SCpnt: scsi command pointer
502 * @done: callback for completion
503 *
504 * Issue a scsi command asynchronously. Return 0 on success or 1 if
505 * we hit an error (normally message queue congestion). The only
506 * minor complication here is that I2O deals with the device addressing
507 * so we have to map the bus/dev/lun back to an I2O handle as well
508 * as faking absent devices ourself.
509 *
510 * Locks: takes the controller lock on error path only
511 */
512
513static int i2o_scsi_queuecommand(struct scsi_cmnd *SCpnt,
514 void (*done) (struct scsi_cmnd *))
515{
516 struct i2o_controller *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 struct i2o_device *i2o_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 int tid;
Markus Lidela1a5ea72006-01-06 00:19:29 -0800519 struct i2o_message *msg;
Markus Lidelf10378f2005-06-23 22:02:16 -0700520 /*
521 * ENABLE_DISCONNECT
522 * SIMPLE_TAG
523 * RETURN_SENSE_DATA_IN_REPLY_MESSAGE_FRAME
524 */
525 u32 scsi_flags = 0x20a00000;
Markus Lidelb2aaee32005-06-23 22:02:19 -0700526 u32 sgl_offset;
Markus Lidela1a5ea72006-01-06 00:19:29 -0800527 u32 *mptr;
Markus Lidelb2aaee32005-06-23 22:02:19 -0700528 u32 cmd = I2O_CMD_SCSI_EXEC << 24;
529 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 /*
532 * Do the incoming paperwork
533 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 i2o_dev = SCpnt->device->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 c = i2o_dev->iop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 SCpnt->scsi_done = done;
538
539 if (unlikely(!i2o_dev)) {
540 osm_warn("no I2O device in request\n");
541 SCpnt->result = DID_NO_CONNECT << 16;
542 done(SCpnt);
Markus Lidelb2aaee32005-06-23 22:02:19 -0700543 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 }
545
546 tid = i2o_dev->lct_data.tid;
547
548 osm_debug("qcmd: Tid = %03x\n", tid);
549 osm_debug("Real scsi messages.\n");
550
551 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 * Put together a scsi execscb message
553 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 switch (SCpnt->sc_data_direction) {
555 case PCI_DMA_NONE:
Markus Lidelf10378f2005-06-23 22:02:16 -0700556 /* DATA NO XFER */
Markus Lidelb2aaee32005-06-23 22:02:19 -0700557 sgl_offset = SGL_OFFSET_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 break;
559
560 case PCI_DMA_TODEVICE:
Markus Lidelf10378f2005-06-23 22:02:16 -0700561 /* DATA OUT (iop-->dev) */
562 scsi_flags |= 0x80000000;
Markus Lidelb2aaee32005-06-23 22:02:19 -0700563 sgl_offset = SGL_OFFSET_10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 break;
565
566 case PCI_DMA_FROMDEVICE:
Markus Lidelf10378f2005-06-23 22:02:16 -0700567 /* DATA IN (iop<--dev) */
568 scsi_flags |= 0x40000000;
Markus Lidelb2aaee32005-06-23 22:02:19 -0700569 sgl_offset = SGL_OFFSET_10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 break;
571
572 default:
573 /* Unknown - kill the command */
574 SCpnt->result = DID_NO_CONNECT << 16;
575 done(SCpnt);
Markus Lidelb2aaee32005-06-23 22:02:19 -0700576 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
578
Markus Lidelb2aaee32005-06-23 22:02:19 -0700579 /*
580 * Obtain an I2O message. If there are none free then
581 * throw it back to the scsi layer
582 */
583
Markus Lidela1a5ea72006-01-06 00:19:29 -0800584 msg = i2o_msg_get(c);
585 if (IS_ERR(msg)) {
Markus Lidelb2aaee32005-06-23 22:02:19 -0700586 rc = SCSI_MLQUEUE_HOST_BUSY;
587 goto exit;
588 }
589
590 mptr = &msg->body[0];
591
Christoph Hellwigbeb40482006-06-10 18:01:03 +0200592#if 0 /* this code can't work */
Markus Lidelb2aaee32005-06-23 22:02:19 -0700593#ifdef CONFIG_I2O_EXT_ADAPTEC
594 if (c->adaptec) {
595 u32 adpt_flags = 0;
596
597 if (SCpnt->sc_request && SCpnt->sc_request->upper_private_data) {
598 i2o_sg_io_hdr_t __user *usr_ptr =
599 ((Sg_request *) (SCpnt->sc_request->
600 upper_private_data))->header.
601 usr_ptr;
602
603 if (usr_ptr)
604 get_user(adpt_flags, &usr_ptr->flags);
605 }
606
607 switch (i2o_dev->lct_data.class_id) {
608 case I2O_CLASS_EXECUTIVE:
609 case I2O_CLASS_RANDOM_BLOCK_STORAGE:
610 /* interpret flag has to be set for executive */
611 adpt_flags ^= I2O_DPT_SG_FLAG_INTERPRET;
612 break;
613
614 default:
615 break;
616 }
617
618 /*
619 * for Adaptec controllers we use the PRIVATE command, because
620 * the normal SCSI EXEC doesn't support all SCSI commands on
621 * all controllers (for example READ CAPACITY).
622 */
623 if (sgl_offset == SGL_OFFSET_10)
624 sgl_offset = SGL_OFFSET_12;
625 cmd = I2O_CMD_PRIVATE << 24;
Markus Lidela1a5ea72006-01-06 00:19:29 -0800626 *mptr++ = cpu_to_le32(I2O_VENDOR_DPT << 16 | I2O_CMD_SCSI_EXEC);
627 *mptr++ = cpu_to_le32(adpt_flags | tid);
Markus Lidelb2aaee32005-06-23 22:02:19 -0700628 }
629#endif
Christoph Hellwigbeb40482006-06-10 18:01:03 +0200630#endif
Markus Lidelb2aaee32005-06-23 22:02:19 -0700631
Markus Lidela1a5ea72006-01-06 00:19:29 -0800632 msg->u.head[1] = cpu_to_le32(cmd | HOST_TID << 12 | tid);
633 msg->u.s.icntxt = cpu_to_le32(i2o_scsi_driver.context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 /* We want the SCSI control block back */
Markus Lidela1a5ea72006-01-06 00:19:29 -0800636 msg->u.s.tcntxt = cpu_to_le32(i2o_cntxt_list_add(c, SCpnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 /* LSI_920_PCI_QUIRK
639 *
640 * Intermittant observations of msg frame word data corruption
641 * observed on msg[4] after:
642 * WRITE, READ-MODIFY-WRITE
643 * operations. 19990606 -sralston
644 *
645 * (Hence we build this word via tag. Its good practice anyway
646 * we don't want fetches over PCI needlessly)
647 */
648
649 /* Attach tags to the devices */
Markus Lidel9e875452005-06-23 22:02:21 -0700650 /* FIXME: implement
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 if(SCpnt->device->tagged_supported) {
652 if(SCpnt->tag == HEAD_OF_QUEUE_TAG)
653 scsi_flags |= 0x01000000;
654 else if(SCpnt->tag == ORDERED_QUEUE_TAG)
655 scsi_flags |= 0x01800000;
656 }
657 */
658
Markus Lidela1a5ea72006-01-06 00:19:29 -0800659 *mptr++ = cpu_to_le32(scsi_flags | SCpnt->cmd_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 /* Write SCSI command into the message - always 16 byte block */
Markus Lidela1a5ea72006-01-06 00:19:29 -0800662 memcpy(mptr, SCpnt->cmnd, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 mptr += 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Markus Lidelb2aaee32005-06-23 22:02:19 -0700665 if (sgl_offset != SGL_OFFSET_0) {
666 /* write size of data addressed by SGL */
Markus Lidela1a5ea72006-01-06 00:19:29 -0800667 *mptr++ = cpu_to_le32(SCpnt->request_bufflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Markus Lidelb2aaee32005-06-23 22:02:19 -0700669 /* Now fill in the SGList and command */
670 if (SCpnt->use_sg) {
671 if (!i2o_dma_map_sg(c, SCpnt->request_buffer,
672 SCpnt->use_sg,
673 SCpnt->sc_data_direction, &mptr))
674 goto nomem;
675 } else {
676 SCpnt->SCp.dma_handle =
677 i2o_dma_map_single(c, SCpnt->request_buffer,
678 SCpnt->request_bufflen,
679 SCpnt->sc_data_direction, &mptr);
680 if (dma_mapping_error(SCpnt->SCp.dma_handle))
681 goto nomem;
Markus Lidelf88e1192005-06-23 22:02:14 -0700682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
684
685 /* Stick the headers on */
Markus Lidela1a5ea72006-01-06 00:19:29 -0800686 msg->u.head[0] =
687 cpu_to_le32(I2O_MESSAGE_SIZE(mptr - &msg->u.head[0]) | sgl_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 /* Queue the message */
Markus Lidela1a5ea72006-01-06 00:19:29 -0800690 i2o_msg_post(c, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 osm_debug("Issued %ld\n", SCpnt->serial_number);
693
694 return 0;
Markus Lidelb2aaee32005-06-23 22:02:19 -0700695
696 nomem:
697 rc = -ENOMEM;
Markus Lidela1a5ea72006-01-06 00:19:29 -0800698 i2o_msg_nop(c, msg);
Markus Lidelb2aaee32005-06-23 22:02:19 -0700699
700 exit:
701 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702};
703
704/**
705 * i2o_scsi_abort - abort a running command
706 * @SCpnt: command to abort
707 *
708 * Ask the I2O controller to abort a command. This is an asynchrnous
709 * process and our callback handler will see the command complete with an
710 * aborted message if it succeeds.
711 *
712 * Returns 0 if the command is successfully aborted or negative error code
713 * on failure.
714 */
715static int i2o_scsi_abort(struct scsi_cmnd *SCpnt)
716{
717 struct i2o_device *i2o_dev;
718 struct i2o_controller *c;
Markus Lidela1a5ea72006-01-06 00:19:29 -0800719 struct i2o_message *msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 int tid;
721 int status = FAILED;
722
723 osm_warn("Aborting command block.\n");
724
725 i2o_dev = SCpnt->device->hostdata;
726 c = i2o_dev->iop;
727 tid = i2o_dev->lct_data.tid;
728
Markus Lidela1a5ea72006-01-06 00:19:29 -0800729 msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
730 if (IS_ERR(msg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 return SCSI_MLQUEUE_HOST_BUSY;
732
Markus Lidela1a5ea72006-01-06 00:19:29 -0800733 msg->u.head[0] = cpu_to_le32(FIVE_WORD_MSG_SIZE | SGL_OFFSET_0);
734 msg->u.head[1] =
735 cpu_to_le32(I2O_CMD_SCSI_ABORT << 24 | HOST_TID << 12 | tid);
736 msg->body[0] = cpu_to_le32(i2o_cntxt_list_get_ptr(c, SCpnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Markus Lidele22bec22006-02-04 23:27:39 -0800738 if (!i2o_msg_post_wait(c, msg, I2O_TIMEOUT_SCSI_SCB_ABORT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 status = SUCCESS;
740
741 return status;
742}
743
744/**
745 * i2o_scsi_bios_param - Invent disk geometry
746 * @sdev: scsi device
747 * @dev: block layer device
748 * @capacity: size in sectors
749 * @ip: geometry array
750 *
Randy Dunlapd9489fb2006-12-06 20:38:43 -0800751 * This is anyone's guess quite frankly. We use the same rules everyone
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 * else appears to and hope. It seems to work.
753 */
754
755static int i2o_scsi_bios_param(struct scsi_device *sdev,
756 struct block_device *dev, sector_t capacity,
757 int *ip)
758{
759 int size;
760
761 size = capacity;
762 ip[0] = 64; /* heads */
763 ip[1] = 32; /* sectors */
764 if ((ip[2] = size >> 11) > 1024) { /* cylinders, test for big disk */
765 ip[0] = 255; /* heads */
766 ip[1] = 63; /* sectors */
767 ip[2] = size / (255 * 63); /* cylinders */
768 }
769 return 0;
770}
771
772static struct scsi_host_template i2o_scsi_host_template = {
773 .proc_name = OSM_NAME,
774 .name = OSM_DESCRIPTION,
775 .info = i2o_scsi_info,
776 .queuecommand = i2o_scsi_queuecommand,
777 .eh_abort_handler = i2o_scsi_abort,
778 .bios_param = i2o_scsi_bios_param,
779 .can_queue = I2O_SCSI_CAN_QUEUE,
780 .sg_tablesize = 8,
781 .cmd_per_lun = 6,
782 .use_clustering = ENABLE_CLUSTERING,
783};
784
785/**
786 * i2o_scsi_init - SCSI OSM initialization function
787 *
788 * Register SCSI OSM into I2O core.
789 *
790 * Returns 0 on success or negative error code on failure.
791 */
792static int __init i2o_scsi_init(void)
793{
794 int rc;
795
796 printk(KERN_INFO OSM_DESCRIPTION " v" OSM_VERSION "\n");
797
798 /* Register SCSI OSM into I2O core */
799 rc = i2o_driver_register(&i2o_scsi_driver);
800 if (rc) {
801 osm_err("Could not register SCSI driver\n");
802 return rc;
803 }
804
805 return 0;
806};
807
808/**
809 * i2o_scsi_exit - SCSI OSM exit function
810 *
811 * Unregisters SCSI OSM from I2O core.
812 */
813static void __exit i2o_scsi_exit(void)
814{
815 /* Unregister I2O SCSI OSM from I2O core */
816 i2o_driver_unregister(&i2o_scsi_driver);
817};
818
819MODULE_AUTHOR("Red Hat Software");
820MODULE_LICENSE("GPL");
821MODULE_DESCRIPTION(OSM_DESCRIPTION);
822MODULE_VERSION(OSM_VERSION);
823
824module_init(i2o_scsi_init);
825module_exit(i2o_scsi_exit);