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