1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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>
57#include <linux/scatterlist.h>
58
59#include <asm/dma.h>
60#include <asm/system.h>
61#include <asm/io.h>
62#include <asm/atomic.h>
63
64#include <scsi/scsi.h>
65#include <scsi/scsi_host.h>
66#include <scsi/scsi_device.h>
67#include <scsi/scsi_cmnd.h>
68#include <scsi/sg.h>
69
70#define OSM_NAME "scsi-osm"
71#define OSM_VERSION "1.316"
72#define OSM_DESCRIPTION "I2O SCSI Peripheral OSM"
73
74static struct i2o_driver i2o_scsi_driver;
75
76static unsigned int i2o_scsi_max_id = 16;
77static unsigned int i2o_scsi_max_lun = 255;
78
79struct i2o_scsi_host {
80 struct Scsi_Host *scsi_host;
81 struct i2o_controller *iop;
82 unsigned int lun;
83 struct i2o_device *channel[0];
84};
85
86static struct scsi_host_template i2o_scsi_host_template;
87
88#define I2O_SCSI_CAN_QUEUE 4
89
90
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;
105 u16 body_size = 6;
106
107#ifdef CONFIG_I2O_EXT_ADAPTEC
108 if (c->adaptec)
109 body_size = 8;
110#endif
111
112 list_for_each_entry(i2o_dev, &c->devices, list)
113 if (i2o_dev->lct_data.class_id == I2O_CLASS_BUS_ADAPTER) {
114 if (!i2o_parm_field_get(i2o_dev, 0x0000, 0, &type, 1)
115 && (type == 0x01))
116 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;
137 scsi_host->sg_tablesize = i2o_sg_tablesize(c, body_size);
138
139 i2o_shost = (struct i2o_scsi_host *)scsi_host->hostdata;
140 i2o_shost->scsi_host = scsi_host;
141 i2o_shost->iop = c;
142 i2o_shost->lun = 1;
143
144 i = 0;
145 list_for_each_entry(i2o_dev, &c->devices, list)
146 if (i2o_dev->lct_data.class_id == I2O_CLASS_BUS_ADAPTER) {
147 if (!i2o_parm_field_get(i2o_dev, 0x0000, 0, &type, 1)
148 && (type == 0x01))
149 i2o_shost->channel[i++] = i2o_dev;
150
151 if (i >= max_channel)
152 break;
153 }
154
155 return i2o_shost;
156};
157
158
159
160
161
162
163
164
165
166
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
175
176
177
178
179
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
188 osm_info("device removed (TID: %03x)\n", i2o_dev->lct_data.tid);
189
190 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) {
194 sysfs_remove_link(&i2o_dev->device.kobj, "scsi");
195 scsi_remove_device(scsi_dev);
196 scsi_device_put(scsi_dev);
197 break;
198 }
199
200 return 0;
201};
202
203
204
205
206
207
208
209
210
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;
220 u32 id = -1;
221 u64 lun = -1;
222 int channel = -1;
223 int i, rc;
224
225 i2o_shost = i2o_scsi_get_host(c);
226 if (!i2o_shost)
227 return -EFAULT;
228
229 scsi_host = i2o_shost->scsi_host;
230
231 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
239 if (!i2o_parm_field_get(d, 0x0000, 0, &type, 1)
240 && (type == 0x01))
241 if (!i2o_parm_field_get(d, 0x0200, 4, &id, 4)) {
242 channel = 0;
243 if (i2o_dev->lct_data.class_id ==
244 I2O_CLASS_RANDOM_BLOCK_STORAGE)
245 lun =
246 cpu_to_le64(i2o_shost->
247 lun++);
248 else
249 lun = 0;
250 }
251 }
252#endif
253 break;
254
255 case I2O_CLASS_SCSI_PERIPHERAL:
256 if (i2o_parm_field_get(i2o_dev, 0x0000, 3, &id, 4))
257 return -EFAULT;
258
259 if (i2o_parm_field_get(i2o_dev, 0x0000, 4, &lun, 8))
260 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:
275 return -EFAULT;
276 }
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 }
283
284 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);
287 return -EFAULT;
288 }
289
290 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);
294 return -EFAULT;
295 }
296
297 scsi_dev =
298 __scsi_add_device(i2o_shost->scsi_host, channel, le32_to_cpu(id),
299 le64_to_cpu(lun), i2o_dev);
300
301 if (IS_ERR(scsi_dev)) {
302 osm_warn("can not add SCSI device %03x\n",
303 i2o_dev->lct_data.tid);
304 return PTR_ERR(scsi_dev);
305 }
306
307 rc = sysfs_create_link(&i2o_dev->device.kobj,
308 &scsi_dev->sdev_gendev.kobj, "scsi");
309 if (rc)
310 goto err;
311
312 osm_info("device added (TID: %03x) channel: %d, id: %d, lun: %ld\n",
313 i2o_dev->lct_data.tid, channel, le32_to_cpu(id),
314 (long unsigned int)le64_to_cpu(lun));
315
316 return 0;
317
318err:
319 scsi_remove_device(scsi_dev);
320 return rc;
321};
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345static int i2o_scsi_reply(struct i2o_controller *c, u32 m,
346 struct i2o_message *msg)
347{
348 struct scsi_cmnd *cmd;
349 u32 error;
350 struct device *dev;
351
352 cmd = i2o_cntxt_list_get(c, le32_to_cpu(msg->u.s.tcntxt));
353 if (unlikely(!cmd)) {
354 osm_err("NULL reply received!\n");
355 return -1;
356 }
357
358
359
360
361
362 error = le32_to_cpu(msg->body[0]);
363
364 osm_debug("Completed %ld\n", cmd->serial_number);
365
366 cmd->result = error & 0xff;
367
368
369
370
371 if (cmd->result)
372 memcpy(cmd->sense_buffer, &msg->body[3],
373 min(SCSI_SENSE_BUFFERSIZE, 40));
374
375
376 if ((error >> 8) & 0xff)
377 osm_err("SCSI error %08x\n", error);
378
379 dev = &c->pdev->dev;
380
381 scsi_dma_unmap(cmd);
382
383 cmd->scsi_done(cmd);
384
385 return 1;
386};
387
388
389
390
391
392
393
394
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
411
412
413
414
415
416static void i2o_scsi_notify_device_remove(struct i2o_device *i2o_dev)
417{
418 switch (i2o_dev->lct_data.class_id) {
419 case I2O_CLASS_EXECUTIVE:
420 case I2O_CLASS_RANDOM_BLOCK_STORAGE:
421 i2o_scsi_remove(&i2o_dev->device);
422 break;
423
424 default:
425 break;
426 }
427};
428
429
430
431
432
433
434
435
436static void i2o_scsi_notify_controller_add(struct i2o_controller *c)
437{
438 struct i2o_scsi_host *i2o_shost;
439 int rc;
440
441 i2o_shost = i2o_scsi_host_alloc(c);
442 if (IS_ERR(i2o_shost)) {
443 osm_err("Could not initialize SCSI host\n");
444 return;
445 }
446
447 rc = scsi_add_host(i2o_shost->scsi_host, &c->device);
448 if (rc) {
449 osm_err("Could not add SCSI host\n");
450 scsi_host_put(i2o_shost->scsi_host);
451 return;
452 }
453
454 c->driver_data[i2o_scsi_driver.context] = i2o_shost;
455
456 osm_debug("new I2O SCSI host added\n");
457};
458
459
460
461
462
463
464
465
466static void i2o_scsi_notify_controller_remove(struct i2o_controller *c)
467{
468 struct i2o_scsi_host *i2o_shost;
469 i2o_shost = i2o_scsi_get_host(c);
470 if (!i2o_shost)
471 return;
472
473 c->driver_data[i2o_scsi_driver.context] = NULL;
474
475 scsi_remove_host(i2o_shost->scsi_host);
476 scsi_host_put(i2o_shost->scsi_host);
477 osm_debug("I2O SCSI host removed\n");
478};
479
480
481static struct i2o_driver i2o_scsi_driver = {
482 .name = OSM_NAME,
483 .reply = i2o_scsi_reply,
484 .classes = i2o_scsi_class_id,
485 .notify_device_add = i2o_scsi_notify_device_add,
486 .notify_device_remove = i2o_scsi_notify_device_remove,
487 .notify_controller_add = i2o_scsi_notify_controller_add,
488 .notify_controller_remove = i2o_scsi_notify_controller_remove,
489 .driver = {
490 .probe = i2o_scsi_probe,
491 .remove = i2o_scsi_remove,
492 },
493};
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509static int i2o_scsi_queuecommand(struct scsi_cmnd *SCpnt,
510 void (*done) (struct scsi_cmnd *))
511{
512 struct i2o_controller *c;
513 struct i2o_device *i2o_dev;
514 int tid;
515 struct i2o_message *msg;
516
517
518
519
520
521 u32 scsi_flags = 0x20a00000;
522 u32 sgl_offset;
523 u32 *mptr;
524 u32 cmd = I2O_CMD_SCSI_EXEC << 24;
525 int rc = 0;
526
527
528
529
530 i2o_dev = SCpnt->device->hostdata;
531 c = i2o_dev->iop;
532
533 SCpnt->scsi_done = done;
534
535 if (unlikely(!i2o_dev)) {
536 osm_warn("no I2O device in request\n");
537 SCpnt->result = DID_NO_CONNECT << 16;
538 done(SCpnt);
539 goto exit;
540 }
541
542 tid = i2o_dev->lct_data.tid;
543
544 osm_debug("qcmd: Tid = %03x\n", tid);
545 osm_debug("Real scsi messages.\n");
546
547
548
549
550 switch (SCpnt->sc_data_direction) {
551 case PCI_DMA_NONE:
552
553 sgl_offset = SGL_OFFSET_0;
554 break;
555
556 case PCI_DMA_TODEVICE:
557
558 scsi_flags |= 0x80000000;
559 sgl_offset = SGL_OFFSET_10;
560 break;
561
562 case PCI_DMA_FROMDEVICE:
563
564 scsi_flags |= 0x40000000;
565 sgl_offset = SGL_OFFSET_10;
566 break;
567
568 default:
569
570 SCpnt->result = DID_NO_CONNECT << 16;
571 done(SCpnt);
572 goto exit;
573 }
574
575
576
577
578
579
580 msg = i2o_msg_get(c);
581 if (IS_ERR(msg)) {
582 rc = SCSI_MLQUEUE_HOST_BUSY;
583 goto exit;
584 }
585
586 mptr = &msg->body[0];
587
588#if 0
589#ifdef CONFIG_I2O_EXT_ADAPTEC
590 if (c->adaptec) {
591 u32 adpt_flags = 0;
592
593 if (SCpnt->sc_request && SCpnt->sc_request->upper_private_data) {
594 i2o_sg_io_hdr_t __user *usr_ptr =
595 ((Sg_request *) (SCpnt->sc_request->
596 upper_private_data))->header.
597 usr_ptr;
598
599 if (usr_ptr)
600 get_user(adpt_flags, &usr_ptr->flags);
601 }
602
603 switch (i2o_dev->lct_data.class_id) {
604 case I2O_CLASS_EXECUTIVE:
605 case I2O_CLASS_RANDOM_BLOCK_STORAGE:
606
607 adpt_flags ^= I2O_DPT_SG_FLAG_INTERPRET;
608 break;
609
610 default:
611 break;
612 }
613
614
615
616
617
618
619 if (sgl_offset == SGL_OFFSET_10)
620 sgl_offset = SGL_OFFSET_12;
621 cmd = I2O_CMD_PRIVATE << 24;
622 *mptr++ = cpu_to_le32(I2O_VENDOR_DPT << 16 | I2O_CMD_SCSI_EXEC);
623 *mptr++ = cpu_to_le32(adpt_flags | tid);
624 }
625#endif
626#endif
627
628 msg->u.head[1] = cpu_to_le32(cmd | HOST_TID << 12 | tid);
629 msg->u.s.icntxt = cpu_to_le32(i2o_scsi_driver.context);
630
631
632 msg->u.s.tcntxt = cpu_to_le32(i2o_cntxt_list_add(c, SCpnt));
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655 *mptr++ = cpu_to_le32(scsi_flags | SCpnt->cmd_len);
656
657
658 memcpy(mptr, SCpnt->cmnd, 16);
659 mptr += 4;
660
661 if (sgl_offset != SGL_OFFSET_0) {
662
663 *mptr++ = cpu_to_le32(scsi_bufflen(SCpnt));
664
665
666
667 if (scsi_sg_count(SCpnt)) {
668 if (!i2o_dma_map_sg(c, scsi_sglist(SCpnt),
669 scsi_sg_count(SCpnt),
670 SCpnt->sc_data_direction, &mptr))
671 goto nomem;
672 }
673 }
674
675
676 msg->u.head[0] =
677 cpu_to_le32(I2O_MESSAGE_SIZE(mptr - &msg->u.head[0]) | sgl_offset);
678
679
680 i2o_msg_post(c, msg);
681
682 osm_debug("Issued %ld\n", SCpnt->serial_number);
683
684 return 0;
685
686 nomem:
687 rc = -ENOMEM;
688 i2o_msg_nop(c, msg);
689
690 exit:
691 return rc;
692};
693
694
695
696
697
698
699
700
701
702
703
704
705static int i2o_scsi_abort(struct scsi_cmnd *SCpnt)
706{
707 struct i2o_device *i2o_dev;
708 struct i2o_controller *c;
709 struct i2o_message *msg;
710 int tid;
711 int status = FAILED;
712
713 osm_warn("Aborting command block.\n");
714
715 i2o_dev = SCpnt->device->hostdata;
716 c = i2o_dev->iop;
717 tid = i2o_dev->lct_data.tid;
718
719 msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
720 if (IS_ERR(msg))
721 return SCSI_MLQUEUE_HOST_BUSY;
722
723 msg->u.head[0] = cpu_to_le32(FIVE_WORD_MSG_SIZE | SGL_OFFSET_0);
724 msg->u.head[1] =
725 cpu_to_le32(I2O_CMD_SCSI_ABORT << 24 | HOST_TID << 12 | tid);
726 msg->body[0] = cpu_to_le32(i2o_cntxt_list_get_ptr(c, SCpnt));
727
728 if (!i2o_msg_post_wait(c, msg, I2O_TIMEOUT_SCSI_SCB_ABORT))
729 status = SUCCESS;
730
731 return status;
732}
733
734
735
736
737
738
739
740
741
742
743
744
745static int i2o_scsi_bios_param(struct scsi_device *sdev,
746 struct block_device *dev, sector_t capacity,
747 int *ip)
748{
749 int size;
750
751 size = capacity;
752 ip[0] = 64;
753 ip[1] = 32;
754 if ((ip[2] = size >> 11) > 1024) {
755 ip[0] = 255;
756 ip[1] = 63;
757 ip[2] = size / (255 * 63);
758 }
759 return 0;
760}
761
762static struct scsi_host_template i2o_scsi_host_template = {
763 .proc_name = OSM_NAME,
764 .name = OSM_DESCRIPTION,
765 .info = i2o_scsi_info,
766 .queuecommand = i2o_scsi_queuecommand,
767 .eh_abort_handler = i2o_scsi_abort,
768 .bios_param = i2o_scsi_bios_param,
769 .can_queue = I2O_SCSI_CAN_QUEUE,
770 .sg_tablesize = 8,
771 .cmd_per_lun = 6,
772 .use_clustering = ENABLE_CLUSTERING,
773};
774
775
776
777
778
779
780
781
782static int __init i2o_scsi_init(void)
783{
784 int rc;
785
786 printk(KERN_INFO OSM_DESCRIPTION " v" OSM_VERSION "\n");
787
788
789 rc = i2o_driver_register(&i2o_scsi_driver);
790 if (rc) {
791 osm_err("Could not register SCSI driver\n");
792 return rc;
793 }
794
795 return 0;
796};
797
798
799
800
801
802
803static void __exit i2o_scsi_exit(void)
804{
805
806 i2o_driver_unregister(&i2o_scsi_driver);
807};
808
809MODULE_AUTHOR("Red Hat Software");
810MODULE_LICENSE("GPL");
811MODULE_DESCRIPTION(OSM_DESCRIPTION);
812MODULE_VERSION(OSM_VERSION);
813
814module_init(i2o_scsi_init);
815module_exit(i2o_scsi_exit);
816