blob: 24061dfd46e4310c34701ae110798eeb5115cf91 [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/scsi_request.h>
69#include <scsi/sg.h>
70#include <scsi/sg_request.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72#define OSM_NAME "scsi-osm"
Markus Lidelb2aaee32005-06-23 22:02:19 -070073#define OSM_VERSION "1.282"
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define OSM_DESCRIPTION "I2O SCSI Peripheral OSM"
75
76static struct i2o_driver i2o_scsi_driver;
77
Markus Lidelb2aaee32005-06-23 22:02:19 -070078static unsigned int i2o_scsi_max_id = 16;
79static unsigned int i2o_scsi_max_lun = 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81struct i2o_scsi_host {
82 struct Scsi_Host *scsi_host; /* pointer to the SCSI host */
83 struct i2o_controller *iop; /* pointer to the I2O controller */
Markus Lidelb2aaee32005-06-23 22:02:19 -070084 unsigned int lun; /* lun's used for block devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 struct i2o_device *channel[0]; /* channel->i2o_dev mapping table */
86};
87
88static struct scsi_host_template i2o_scsi_host_template;
89
90#define I2O_SCSI_CAN_QUEUE 4
91
92/* SCSI OSM class handling definition */
93static struct i2o_class_id i2o_scsi_class_id[] = {
94 {I2O_CLASS_SCSI_PERIPHERAL},
95 {I2O_CLASS_END}
96};
97
98static 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;
Markus Lidelb2aaee32005-06-23 22:02:19 -0700107 u16 body_size = 6;
108
109#ifdef CONFIG_I2O_EXT_ADAPTEC
110 if (c->adaptec)
111 body_size = 8;
112#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 list_for_each_entry(i2o_dev, &c->devices, list)
Markus Lidelf10378f2005-06-23 22:02:16 -0700115 if (i2o_dev->lct_data.class_id == I2O_CLASS_BUS_ADAPTER) {
Markus Lidel793fd152006-01-06 00:19:30 -0800116 if (!i2o_parm_field_get(i2o_dev, 0x0000, 0, &type, 1)
Markus Lidelb2aaee32005-06-23 22:02:19 -0700117 && (type == 0x01)) /* SCSI bus */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 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;
Markus Lidelb2aaee32005-06-23 22:02:19 -0700139 scsi_host->sg_tablesize = i2o_sg_tablesize(c, body_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 i2o_shost = (struct i2o_scsi_host *)scsi_host->hostdata;
142 i2o_shost->scsi_host = scsi_host;
143 i2o_shost->iop = c;
Markus Lidelb2aaee32005-06-23 22:02:19 -0700144 i2o_shost->lun = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 i = 0;
147 list_for_each_entry(i2o_dev, &c->devices, list)
Markus Lidelf10378f2005-06-23 22:02:16 -0700148 if (i2o_dev->lct_data.class_id == I2O_CLASS_BUS_ADAPTER) {
Markus Lidel793fd152006-01-06 00:19:30 -0800149 if (!i2o_parm_field_get(i2o_dev, 0x0000, 0, &type, 1)
Markus Lidelb2aaee32005-06-23 22:02:19 -0700150 && (type == 0x01)) /* only SCSI bus */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 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 */
170static 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 */
183static 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
Markus Lidelf88e1192005-06-23 22:02:14 -0700190 osm_info("device removed (TID: %03x)\n", i2o_dev->lct_data.tid);
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 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) {
Markus Lidelf10378f2005-06-23 22:02:16 -0700196 sysfs_remove_link(&i2o_dev->device.kobj, "scsi");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 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 */
214static 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;
Markus Lidelb2aaee32005-06-23 22:02:19 -0700222 u32 id = -1;
223 u64 lun = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 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
Markus Lidelb2aaee32005-06-23 22:02:19 -0700233 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
Markus Lidel793fd152006-01-06 00:19:30 -0800241 if (!i2o_parm_field_get(d, 0x0000, 0, &type, 1)
Markus Lidelb2aaee32005-06-23 22:02:19 -0700242 && (type == 0x01)) /* SCSI bus */
Markus Lidel793fd152006-01-06 00:19:30 -0800243 if (!i2o_parm_field_get(d, 0x0200, 4, &id, 4)) {
Markus Lidelb2aaee32005-06-23 22:02:19 -0700244 channel = 0;
245 if (i2o_dev->lct_data.class_id ==
246 I2O_CLASS_RANDOM_BLOCK_STORAGE)
Markus Lidel793fd152006-01-06 00:19:30 -0800247 lun =
248 cpu_to_le64(i2o_shost->
249 lun++);
Markus Lidelb2aaee32005-06-23 22:02:19 -0700250 else
251 lun = 0;
252 }
253 }
254#endif
255 break;
256
257 case I2O_CLASS_SCSI_PERIPHERAL:
Markus Lidel793fd152006-01-06 00:19:30 -0800258 if (i2o_parm_field_get(i2o_dev, 0x0000, 3, &id, 4))
Markus Lidelb2aaee32005-06-23 22:02:19 -0700259 return -EFAULT;
260
Markus Lidel793fd152006-01-06 00:19:30 -0800261 if (i2o_parm_field_get(i2o_dev, 0x0000, 4, &lun, 8))
Markus Lidelb2aaee32005-06-23 22:02:19 -0700262 return -EFAULT;
263
264 parent = i2o_iop_find_device(c, i2o_dev->lct_data.parent_tid);
265 if (!parent) {
266 osm_warn("can not find parent of device %03x\n",
267 i2o_dev->lct_data.tid);
268 return -EFAULT;
269 }
270
271 for (i = 0; i <= i2o_shost->scsi_host->max_channel; i++)
272 if (i2o_shost->channel[i] == parent)
273 channel = i;
274 break;
275
276 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 return -EFAULT;
Markus Lidelb2aaee32005-06-23 22:02:19 -0700278 }
279
280 if (channel == -1) {
281 osm_warn("can not find channel of device %03x\n",
282 i2o_dev->lct_data.tid);
283 return -EFAULT;
284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Markus Lidel793fd152006-01-06 00:19:30 -0800286 if (le32_to_cpu(id) >= scsi_host->max_id) {
287 osm_warn("SCSI device id (%d) >= max_id of I2O host (%d)",
288 le32_to_cpu(id), scsi_host->max_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return -EFAULT;
290 }
291
Markus Lidel793fd152006-01-06 00:19:30 -0800292 if (le64_to_cpu(lun) >= scsi_host->max_lun) {
293 osm_warn("SCSI device lun (%lu) >= max_lun of I2O host (%d)",
294 (long unsigned int)le64_to_cpu(lun),
295 scsi_host->max_lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 return -EFAULT;
297 }
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 scsi_dev =
Markus Lidel793fd152006-01-06 00:19:30 -0800300 __scsi_add_device(i2o_shost->scsi_host, channel, le32_to_cpu(id),
301 le64_to_cpu(lun), i2o_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Markus Lidelf10378f2005-06-23 22:02:16 -0700303 if (IS_ERR(scsi_dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 osm_warn("can not add SCSI device %03x\n",
305 i2o_dev->lct_data.tid);
Markus Lidelf10378f2005-06-23 22:02:16 -0700306 return PTR_ERR(scsi_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
308
Markus Lidelb2aaee32005-06-23 22:02:19 -0700309 sysfs_create_link(&i2o_dev->device.kobj, &scsi_dev->sdev_gendev.kobj,
310 "scsi");
Markus Lidelf10378f2005-06-23 22:02:16 -0700311
Markus Lidelf88e1192005-06-23 22:02:14 -0700312 osm_info("device added (TID: %03x) channel: %d, id: %d, lun: %d\n",
Markus Lidel793fd152006-01-06 00:19:30 -0800313 i2o_dev->lct_data.tid, channel, le32_to_cpu(id),
314 (unsigned int)le64_to_cpu(lun));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 return 0;
317};
318
319static const char *i2o_scsi_info(struct Scsi_Host *SChost)
320{
321 struct i2o_scsi_host *hostdata;
322 hostdata = (struct i2o_scsi_host *)SChost->hostdata;
323 return hostdata->iop->name;
324}
325
326/**
327 * i2o_scsi_reply - SCSI OSM message reply handler
328 * @c: controller issuing the reply
329 * @m: message id for flushing
330 * @msg: the message from the controller
331 *
332 * Process reply messages (interrupts in normal scsi controller think).
333 * We can get a variety of messages to process. The normal path is
334 * scsi command completions. We must also deal with IOP failures,
335 * the reply to a bus reset and the reply to a LUN query.
336 *
337 * Returns 0 on success and if the reply should not be flushed or > 0
338 * on success and if the reply should be flushed. Returns negative error
339 * code on failure and if the reply should be flushed.
340 */
341static int i2o_scsi_reply(struct i2o_controller *c, u32 m,
342 struct i2o_message *msg)
343{
344 struct scsi_cmnd *cmd;
Markus Lidel9e875452005-06-23 22:02:21 -0700345 u32 error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 cmd = i2o_cntxt_list_get(c, le32_to_cpu(msg->u.s.tcntxt));
Markus Lidel9e875452005-06-23 22:02:21 -0700349 if (unlikely(!cmd)) {
350 osm_err("NULL reply received!\n");
351 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
353
354 /*
355 * Low byte is device status, next is adapter status,
356 * (then one byte reserved), then request status.
357 */
Markus Lidel9e875452005-06-23 22:02:21 -0700358 error = le32_to_cpu(msg->body[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 osm_debug("Completed %ld\n", cmd->serial_number);
361
Markus Lidel9e875452005-06-23 22:02:21 -0700362 cmd->result = error & 0xff;
363 /*
364 * if DeviceStatus is not SCSI_SUCCESS copy over the sense data and let
365 * the SCSI layer handle the error
366 */
367 if (cmd->result)
368 memcpy(cmd->sense_buffer, &msg->body[3],
369 min(sizeof(cmd->sense_buffer), (size_t) 40));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Markus Lidel9e875452005-06-23 22:02:21 -0700371 /* only output error code if AdapterStatus is not HBA_SUCCESS */
372 if ((error >> 8) & 0xff)
373 osm_err("SCSI error %08x\n", error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 dev = &c->pdev->dev;
376 if (cmd->use_sg)
Markus Lidel9e875452005-06-23 22:02:21 -0700377 dma_unmap_sg(dev, cmd->request_buffer, cmd->use_sg,
378 cmd->sc_data_direction);
379 else if (cmd->SCp.dma_handle)
380 dma_unmap_single(dev, cmd->SCp.dma_handle, cmd->request_bufflen,
381 cmd->sc_data_direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Markus Lidelf88e1192005-06-23 22:02:14 -0700383 cmd->scsi_done(cmd);
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 return 1;
386};
387
388/**
Markus Lidel9e875452005-06-23 22:02:21 -0700389 * i2o_scsi_notify_device_add - Retrieve notifications of added devices
390 * @i2o_dev: the I2O device which was added
391 *
392 * If a I2O device is added we catch the notification, because I2O classes
393 * other then SCSI peripheral will not be received through
394 * i2o_scsi_probe().
395 */
396static void i2o_scsi_notify_device_add(struct i2o_device *i2o_dev)
397{
398 switch (i2o_dev->lct_data.class_id) {
399 case I2O_CLASS_EXECUTIVE:
400 case I2O_CLASS_RANDOM_BLOCK_STORAGE:
401 i2o_scsi_probe(&i2o_dev->device);
402 break;
403
404 default:
405 break;
406 }
407};
408
409/**
410 * i2o_scsi_notify_device_remove - Retrieve notifications of removed
411 * devices
412 * @i2o_dev: the I2O device which was removed
413 *
414 * If a I2O device is removed, we catch the notification to remove the
415 * corresponding SCSI device.
416 */
417static void i2o_scsi_notify_device_remove(struct i2o_device *i2o_dev)
418{
419 switch (i2o_dev->lct_data.class_id) {
420 case I2O_CLASS_EXECUTIVE:
421 case I2O_CLASS_RANDOM_BLOCK_STORAGE:
422 i2o_scsi_remove(&i2o_dev->device);
423 break;
424
425 default:
426 break;
427 }
428};
429
430/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 * i2o_scsi_notify_controller_add - Retrieve notifications of added
432 * controllers
433 * @c: the controller which was added
434 *
435 * If a I2O controller is added, we catch the notification to add a
436 * corresponding Scsi_Host.
437 */
438static void i2o_scsi_notify_controller_add(struct i2o_controller *c)
439{
440 struct i2o_scsi_host *i2o_shost;
441 int rc;
442
443 i2o_shost = i2o_scsi_host_alloc(c);
444 if (IS_ERR(i2o_shost)) {
445 osm_err("Could not initialize SCSI host\n");
446 return;
447 }
448
449 rc = scsi_add_host(i2o_shost->scsi_host, &c->device);
450 if (rc) {
451 osm_err("Could not add SCSI host\n");
452 scsi_host_put(i2o_shost->scsi_host);
453 return;
454 }
455
456 c->driver_data[i2o_scsi_driver.context] = i2o_shost;
457
458 osm_debug("new I2O SCSI host added\n");
459};
460
461/**
462 * i2o_scsi_notify_controller_remove - Retrieve notifications of removed
463 * controllers
464 * @c: the controller which was removed
465 *
466 * If a I2O controller is removed, we catch the notification to remove the
467 * corresponding Scsi_Host.
468 */
469static void i2o_scsi_notify_controller_remove(struct i2o_controller *c)
470{
471 struct i2o_scsi_host *i2o_shost;
472 i2o_shost = i2o_scsi_get_host(c);
473 if (!i2o_shost)
474 return;
475
476 c->driver_data[i2o_scsi_driver.context] = NULL;
477
478 scsi_remove_host(i2o_shost->scsi_host);
479 scsi_host_put(i2o_shost->scsi_host);
Markus Lidelf88e1192005-06-23 22:02:14 -0700480 osm_debug("I2O SCSI host removed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481};
482
483/* SCSI OSM driver struct */
484static struct i2o_driver i2o_scsi_driver = {
485 .name = OSM_NAME,
486 .reply = i2o_scsi_reply,
487 .classes = i2o_scsi_class_id,
Markus Lidel9e875452005-06-23 22:02:21 -0700488 .notify_device_add = i2o_scsi_notify_device_add,
489 .notify_device_remove = i2o_scsi_notify_device_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 .notify_controller_add = i2o_scsi_notify_controller_add,
491 .notify_controller_remove = i2o_scsi_notify_controller_remove,
492 .driver = {
493 .probe = i2o_scsi_probe,
494 .remove = i2o_scsi_remove,
495 },
496};
497
498/**
499 * i2o_scsi_queuecommand - queue a SCSI command
500 * @SCpnt: scsi command pointer
501 * @done: callback for completion
502 *
503 * Issue a scsi command asynchronously. Return 0 on success or 1 if
504 * we hit an error (normally message queue congestion). The only
505 * minor complication here is that I2O deals with the device addressing
506 * so we have to map the bus/dev/lun back to an I2O handle as well
507 * as faking absent devices ourself.
508 *
509 * Locks: takes the controller lock on error path only
510 */
511
512static int i2o_scsi_queuecommand(struct scsi_cmnd *SCpnt,
513 void (*done) (struct scsi_cmnd *))
514{
515 struct i2o_controller *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 struct i2o_device *i2o_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 int tid;
Markus Lidela1a5ea72006-01-06 00:19:29 -0800518 struct i2o_message *msg;
Markus Lidelf10378f2005-06-23 22:02:16 -0700519 /*
520 * ENABLE_DISCONNECT
521 * SIMPLE_TAG
522 * RETURN_SENSE_DATA_IN_REPLY_MESSAGE_FRAME
523 */
524 u32 scsi_flags = 0x20a00000;
Markus Lidelb2aaee32005-06-23 22:02:19 -0700525 u32 sgl_offset;
Markus Lidela1a5ea72006-01-06 00:19:29 -0800526 u32 *mptr;
Markus Lidelb2aaee32005-06-23 22:02:19 -0700527 u32 cmd = I2O_CMD_SCSI_EXEC << 24;
528 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 /*
531 * Do the incoming paperwork
532 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 i2o_dev = SCpnt->device->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 c = i2o_dev->iop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 SCpnt->scsi_done = done;
537
538 if (unlikely(!i2o_dev)) {
539 osm_warn("no I2O device in request\n");
540 SCpnt->result = DID_NO_CONNECT << 16;
541 done(SCpnt);
Markus Lidelb2aaee32005-06-23 22:02:19 -0700542 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 }
544
545 tid = i2o_dev->lct_data.tid;
546
547 osm_debug("qcmd: Tid = %03x\n", tid);
548 osm_debug("Real scsi messages.\n");
549
550 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 * Put together a scsi execscb message
552 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 switch (SCpnt->sc_data_direction) {
554 case PCI_DMA_NONE:
Markus Lidelf10378f2005-06-23 22:02:16 -0700555 /* DATA NO XFER */
Markus Lidelb2aaee32005-06-23 22:02:19 -0700556 sgl_offset = SGL_OFFSET_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 break;
558
559 case PCI_DMA_TODEVICE:
Markus Lidelf10378f2005-06-23 22:02:16 -0700560 /* DATA OUT (iop-->dev) */
561 scsi_flags |= 0x80000000;
Markus Lidelb2aaee32005-06-23 22:02:19 -0700562 sgl_offset = SGL_OFFSET_10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 break;
564
565 case PCI_DMA_FROMDEVICE:
Markus Lidelf10378f2005-06-23 22:02:16 -0700566 /* DATA IN (iop<--dev) */
567 scsi_flags |= 0x40000000;
Markus Lidelb2aaee32005-06-23 22:02:19 -0700568 sgl_offset = SGL_OFFSET_10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 break;
570
571 default:
572 /* Unknown - kill the command */
573 SCpnt->result = DID_NO_CONNECT << 16;
574 done(SCpnt);
Markus Lidelb2aaee32005-06-23 22:02:19 -0700575 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
577
Markus Lidelb2aaee32005-06-23 22:02:19 -0700578 /*
579 * Obtain an I2O message. If there are none free then
580 * throw it back to the scsi layer
581 */
582
Markus Lidela1a5ea72006-01-06 00:19:29 -0800583 msg = i2o_msg_get(c);
584 if (IS_ERR(msg)) {
Markus Lidelb2aaee32005-06-23 22:02:19 -0700585 rc = SCSI_MLQUEUE_HOST_BUSY;
586 goto exit;
587 }
588
589 mptr = &msg->body[0];
590
591#ifdef CONFIG_I2O_EXT_ADAPTEC
592 if (c->adaptec) {
593 u32 adpt_flags = 0;
594
595 if (SCpnt->sc_request && SCpnt->sc_request->upper_private_data) {
596 i2o_sg_io_hdr_t __user *usr_ptr =
597 ((Sg_request *) (SCpnt->sc_request->
598 upper_private_data))->header.
599 usr_ptr;
600
601 if (usr_ptr)
602 get_user(adpt_flags, &usr_ptr->flags);
603 }
604
605 switch (i2o_dev->lct_data.class_id) {
606 case I2O_CLASS_EXECUTIVE:
607 case I2O_CLASS_RANDOM_BLOCK_STORAGE:
608 /* interpret flag has to be set for executive */
609 adpt_flags ^= I2O_DPT_SG_FLAG_INTERPRET;
610 break;
611
612 default:
613 break;
614 }
615
616 /*
617 * for Adaptec controllers we use the PRIVATE command, because
618 * the normal SCSI EXEC doesn't support all SCSI commands on
619 * all controllers (for example READ CAPACITY).
620 */
621 if (sgl_offset == SGL_OFFSET_10)
622 sgl_offset = SGL_OFFSET_12;
623 cmd = I2O_CMD_PRIVATE << 24;
Markus Lidela1a5ea72006-01-06 00:19:29 -0800624 *mptr++ = cpu_to_le32(I2O_VENDOR_DPT << 16 | I2O_CMD_SCSI_EXEC);
625 *mptr++ = cpu_to_le32(adpt_flags | tid);
Markus Lidelb2aaee32005-06-23 22:02:19 -0700626 }
627#endif
628
Markus Lidela1a5ea72006-01-06 00:19:29 -0800629 msg->u.head[1] = cpu_to_le32(cmd | HOST_TID << 12 | tid);
630 msg->u.s.icntxt = cpu_to_le32(i2o_scsi_driver.context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 /* We want the SCSI control block back */
Markus Lidela1a5ea72006-01-06 00:19:29 -0800633 msg->u.s.tcntxt = cpu_to_le32(i2o_cntxt_list_add(c, SCpnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 /* LSI_920_PCI_QUIRK
636 *
637 * Intermittant observations of msg frame word data corruption
638 * observed on msg[4] after:
639 * WRITE, READ-MODIFY-WRITE
640 * operations. 19990606 -sralston
641 *
642 * (Hence we build this word via tag. Its good practice anyway
643 * we don't want fetches over PCI needlessly)
644 */
645
646 /* Attach tags to the devices */
Markus Lidel9e875452005-06-23 22:02:21 -0700647 /* FIXME: implement
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 if(SCpnt->device->tagged_supported) {
649 if(SCpnt->tag == HEAD_OF_QUEUE_TAG)
650 scsi_flags |= 0x01000000;
651 else if(SCpnt->tag == ORDERED_QUEUE_TAG)
652 scsi_flags |= 0x01800000;
653 }
654 */
655
Markus Lidela1a5ea72006-01-06 00:19:29 -0800656 *mptr++ = cpu_to_le32(scsi_flags | SCpnt->cmd_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 /* Write SCSI command into the message - always 16 byte block */
Markus Lidela1a5ea72006-01-06 00:19:29 -0800659 memcpy(mptr, SCpnt->cmnd, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 mptr += 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Markus Lidelb2aaee32005-06-23 22:02:19 -0700662 if (sgl_offset != SGL_OFFSET_0) {
663 /* write size of data addressed by SGL */
Markus Lidela1a5ea72006-01-06 00:19:29 -0800664 *mptr++ = cpu_to_le32(SCpnt->request_bufflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Markus Lidelb2aaee32005-06-23 22:02:19 -0700666 /* Now fill in the SGList and command */
667 if (SCpnt->use_sg) {
668 if (!i2o_dma_map_sg(c, SCpnt->request_buffer,
669 SCpnt->use_sg,
670 SCpnt->sc_data_direction, &mptr))
671 goto nomem;
672 } else {
673 SCpnt->SCp.dma_handle =
674 i2o_dma_map_single(c, SCpnt->request_buffer,
675 SCpnt->request_bufflen,
676 SCpnt->sc_data_direction, &mptr);
677 if (dma_mapping_error(SCpnt->SCp.dma_handle))
678 goto nomem;
Markus Lidelf88e1192005-06-23 22:02:14 -0700679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
681
682 /* Stick the headers on */
Markus Lidela1a5ea72006-01-06 00:19:29 -0800683 msg->u.head[0] =
684 cpu_to_le32(I2O_MESSAGE_SIZE(mptr - &msg->u.head[0]) | sgl_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 /* Queue the message */
Markus Lidela1a5ea72006-01-06 00:19:29 -0800687 i2o_msg_post(c, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 osm_debug("Issued %ld\n", SCpnt->serial_number);
690
691 return 0;
Markus Lidelb2aaee32005-06-23 22:02:19 -0700692
693 nomem:
694 rc = -ENOMEM;
Markus Lidela1a5ea72006-01-06 00:19:29 -0800695 i2o_msg_nop(c, msg);
Markus Lidelb2aaee32005-06-23 22:02:19 -0700696
697 exit:
698 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699};
700
701/**
702 * i2o_scsi_abort - abort a running command
703 * @SCpnt: command to abort
704 *
705 * Ask the I2O controller to abort a command. This is an asynchrnous
706 * process and our callback handler will see the command complete with an
707 * aborted message if it succeeds.
708 *
709 * Returns 0 if the command is successfully aborted or negative error code
710 * on failure.
711 */
712static int i2o_scsi_abort(struct scsi_cmnd *SCpnt)
713{
714 struct i2o_device *i2o_dev;
715 struct i2o_controller *c;
Markus Lidela1a5ea72006-01-06 00:19:29 -0800716 struct i2o_message *msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 int tid;
718 int status = FAILED;
719
720 osm_warn("Aborting command block.\n");
721
722 i2o_dev = SCpnt->device->hostdata;
723 c = i2o_dev->iop;
724 tid = i2o_dev->lct_data.tid;
725
Markus Lidela1a5ea72006-01-06 00:19:29 -0800726 msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
727 if (IS_ERR(msg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 return SCSI_MLQUEUE_HOST_BUSY;
729
Markus Lidela1a5ea72006-01-06 00:19:29 -0800730 msg->u.head[0] = cpu_to_le32(FIVE_WORD_MSG_SIZE | SGL_OFFSET_0);
731 msg->u.head[1] =
732 cpu_to_le32(I2O_CMD_SCSI_ABORT << 24 | HOST_TID << 12 | tid);
733 msg->body[0] = cpu_to_le32(i2o_cntxt_list_get_ptr(c, SCpnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Markus Lidela1a5ea72006-01-06 00:19:29 -0800735 if (i2o_msg_post_wait(c, msg, I2O_TIMEOUT_SCSI_SCB_ABORT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 status = SUCCESS;
737
738 return status;
739}
740
741/**
742 * i2o_scsi_bios_param - Invent disk geometry
743 * @sdev: scsi device
744 * @dev: block layer device
745 * @capacity: size in sectors
746 * @ip: geometry array
747 *
748 * This is anyones guess quite frankly. We use the same rules everyone
749 * else appears to and hope. It seems to work.
750 */
751
752static int i2o_scsi_bios_param(struct scsi_device *sdev,
753 struct block_device *dev, sector_t capacity,
754 int *ip)
755{
756 int size;
757
758 size = capacity;
759 ip[0] = 64; /* heads */
760 ip[1] = 32; /* sectors */
761 if ((ip[2] = size >> 11) > 1024) { /* cylinders, test for big disk */
762 ip[0] = 255; /* heads */
763 ip[1] = 63; /* sectors */
764 ip[2] = size / (255 * 63); /* cylinders */
765 }
766 return 0;
767}
768
769static struct scsi_host_template i2o_scsi_host_template = {
770 .proc_name = OSM_NAME,
771 .name = OSM_DESCRIPTION,
772 .info = i2o_scsi_info,
773 .queuecommand = i2o_scsi_queuecommand,
774 .eh_abort_handler = i2o_scsi_abort,
775 .bios_param = i2o_scsi_bios_param,
776 .can_queue = I2O_SCSI_CAN_QUEUE,
777 .sg_tablesize = 8,
778 .cmd_per_lun = 6,
779 .use_clustering = ENABLE_CLUSTERING,
780};
781
782/**
783 * i2o_scsi_init - SCSI OSM initialization function
784 *
785 * Register SCSI OSM into I2O core.
786 *
787 * Returns 0 on success or negative error code on failure.
788 */
789static int __init i2o_scsi_init(void)
790{
791 int rc;
792
793 printk(KERN_INFO OSM_DESCRIPTION " v" OSM_VERSION "\n");
794
795 /* Register SCSI OSM into I2O core */
796 rc = i2o_driver_register(&i2o_scsi_driver);
797 if (rc) {
798 osm_err("Could not register SCSI driver\n");
799 return rc;
800 }
801
802 return 0;
803};
804
805/**
806 * i2o_scsi_exit - SCSI OSM exit function
807 *
808 * Unregisters SCSI OSM from I2O core.
809 */
810static void __exit i2o_scsi_exit(void)
811{
812 /* Unregister I2O SCSI OSM from I2O core */
813 i2o_driver_unregister(&i2o_scsi_driver);
814};
815
816MODULE_AUTHOR("Red Hat Software");
817MODULE_LICENSE("GPL");
818MODULE_DESCRIPTION(OSM_DESCRIPTION);
819MODULE_VERSION(OSM_VERSION);
820
821module_init(i2o_scsi_init);
822module_exit(i2o_scsi_exit);