21 #include <linux/ctype.h>
23 #include <linux/module.h>
28 #include <linux/slab.h>
29 #include <scsi/scsi.h>
38 #define SPI_NUM_ATTRS 14
39 #define SPI_OTHER_ATTRS 1
41 #define SPI_HOST_ATTRS 1
43 #define SPI_MAX_ECHO_BUFFER_SIZE 4096
46 #define DV_TIMEOUT (10*HZ)
67 #define spi_dv_in_progress(x) (((struct spi_transport_attrs *)&(x)->starget_data)->dv_in_progress)
68 #define spi_dv_mutex(x) (((struct spi_transport_attrs *)&(x)->starget_data)->dv_mutex)
75 #define to_spi_internal(tmpl) container_of(tmpl, struct spi_internal, t)
77 static const int ppr_to_ps[] = {
96 #define SPI_STATIC_PPR 0x0c
98 static int sprint_frac(
char *
dest,
int value,
int denom)
100 int frac = value % denom;
105 dest[result++] =
'.';
109 sprintf(dest + result,
"%d", frac / denom);
114 dest[result++] =
'\0';
162 for (i = 0; i <
ARRAY_SIZE(signal_types); i++) {
163 if (type == signal_types[i].
value)
164 return signal_types[
i].name;
172 for (i = 0; i <
ARRAY_SIZE(signal_types); i++) {
173 len =
strlen(signal_types[i].name);
174 if (
strncmp(name, signal_types[i].name, len) == 0 &&
175 (name[len] ==
'\n' || name[len] ==
'\0'))
176 return signal_types[
i].value;
209 shost = dev_to_shost(dev);
211 != &spi_host_class.class)
240 dev_info(dev,
"Information Units disabled by blacklist\n");
278 #define spi_transport_show_simple(field, format_string) \
281 show_spi_transport_##field(struct device *dev, \
282 struct device_attribute *attr, char *buf) \
284 struct scsi_target *starget = transport_class_to_starget(dev); \
285 struct spi_transport_attrs *tp; \
287 tp = (struct spi_transport_attrs *)&starget->starget_data; \
288 return snprintf(buf, 20, format_string, tp->field); \
291 #define spi_transport_store_simple(field, format_string) \
294 store_spi_transport_##field(struct device *dev, \
295 struct device_attribute *attr, \
296 const char *buf, size_t count) \
299 struct scsi_target *starget = transport_class_to_starget(dev); \
300 struct spi_transport_attrs *tp; \
302 tp = (struct spi_transport_attrs *)&starget->starget_data; \
303 val = simple_strtoul(buf, NULL, 0); \
308 #define spi_transport_show_function(field, format_string) \
311 show_spi_transport_##field(struct device *dev, \
312 struct device_attribute *attr, char *buf) \
314 struct scsi_target *starget = transport_class_to_starget(dev); \
315 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
316 struct spi_transport_attrs *tp; \
317 struct spi_internal *i = to_spi_internal(shost->transportt); \
318 tp = (struct spi_transport_attrs *)&starget->starget_data; \
319 if (i->f->get_##field) \
320 i->f->get_##field(starget); \
321 return snprintf(buf, 20, format_string, tp->field); \
324 #define spi_transport_store_function(field, format_string) \
326 store_spi_transport_##field(struct device *dev, \
327 struct device_attribute *attr, \
328 const char *buf, size_t count) \
331 struct scsi_target *starget = transport_class_to_starget(dev); \
332 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
333 struct spi_internal *i = to_spi_internal(shost->transportt); \
335 if (!i->f->set_##field) \
337 val = simple_strtoul(buf, NULL, 0); \
338 i->f->set_##field(starget, val); \
342 #define spi_transport_store_max(field, format_string) \
344 store_spi_transport_##field(struct device *dev, \
345 struct device_attribute *attr, \
346 const char *buf, size_t count) \
349 struct scsi_target *starget = transport_class_to_starget(dev); \
350 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
351 struct spi_internal *i = to_spi_internal(shost->transportt); \
352 struct spi_transport_attrs *tp \
353 = (struct spi_transport_attrs *)&starget->starget_data; \
355 if (i->f->set_##field) \
357 val = simple_strtoul(buf, NULL, 0); \
358 if (val > tp->max_##field) \
359 val = tp->max_##field; \
360 i->f->set_##field(starget, val); \
364 #define spi_transport_rd_attr(field, format_string) \
365 spi_transport_show_function(field, format_string) \
366 spi_transport_store_function(field, format_string) \
367 static DEVICE_ATTR(field, S_IRUGO, \
368 show_spi_transport_##field, \
369 store_spi_transport_##field);
371 #define spi_transport_simple_attr(field, format_string) \
372 spi_transport_show_simple(field, format_string) \
373 spi_transport_store_simple(field, format_string) \
374 static DEVICE_ATTR(field, S_IRUGO, \
375 show_spi_transport_##field, \
376 store_spi_transport_##field);
378 #define spi_transport_max_attr(field, format_string) \
379 spi_transport_show_function(field, format_string) \
380 spi_transport_store_max(field, format_string) \
381 spi_transport_simple_attr(max_##field, format_string) \
382 static DEVICE_ATTR(field, S_IRUGO, \
383 show_spi_transport_##field, \
384 store_spi_transport_##field);
400 static int child_iter(
struct device *dev,
void *
data)
422 static int period_to_str(
char *
buf,
int period)
426 if (period < 0 || period > 0xff) {
429 picosec = ppr_to_ps[
period];
431 picosec = period * 4000;
435 len =
sprintf(buf,
"reserved");
437 len = sprint_frac(buf, picosec, 1000);
444 show_spi_transport_period_helper(
char *buf,
int period)
446 int len = period_to_str(buf, period);
453 store_spi_transport_period_helper(
struct device *dev,
const char *buf,
454 size_t count,
int *periodp)
456 int j, picosec, period = -1;
466 picosec += (*endp -
'0') * mult;
472 if (ppr_to_ps[j] < picosec)
479 period = picosec / 4000;
490 show_spi_transport_period(
struct device *dev,
494 struct Scsi_Host *shost = dev_to_shost(starget->
dev.parent);
499 if (i->
f->get_period)
500 i->
f->get_period(starget);
502 return show_spi_transport_period_helper(buf, tp->
period);
507 const char *buf,
size_t count)
510 struct Scsi_Host *shost = dev_to_shost(starget->
dev.parent);
516 if (!i->
f->set_period)
519 retval = store_spi_transport_period_helper(cdev, buf, count, &period);
524 i->
f->set_period(starget, period);
530 show_spi_transport_period,
531 store_spi_transport_period);
534 show_spi_transport_min_period(
struct device *cdev,
538 struct Scsi_Host *shost = dev_to_shost(starget->
dev.parent);
543 if (!i->
f->set_period)
546 return show_spi_transport_period_helper(buf, tp->
min_period);
550 store_spi_transport_min_period(
struct device *cdev,
552 const char *buf,
size_t count)
558 return store_spi_transport_period_helper(cdev, buf, count,
564 show_spi_transport_min_period,
565 store_spi_transport_min_period);
575 if (i->
f->get_signalling)
576 i->
f->get_signalling(shost);
582 const char *buf,
size_t count)
588 if (!i->
f->set_signalling)
592 i->
f->set_signalling(shost, type);
597 show_spi_host_signalling,
598 store_spi_host_signalling);
606 return sprintf(buf,
"%s\n", shost->
max_id == 16 ?
"wide" :
"narrow");
609 show_spi_host_width,
NULL);
622 #define DV_SET(x, y) \
624 i->f->set_##x(sdev->sdev_target, y)
636 spi_dv_device_echo_buffer(
struct scsi_device *sdev,
u8 *buffer,
641 unsigned int pattern = 0x0000ffff;
644 const char spi_write_buffer[] = {
645 WRITE_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0
647 const char spi_read_buffer[] = {
648 READ_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0
653 for (j = 0; j < len; ) {
656 for ( ; j <
min(len, 32); j++)
661 for ( ; j <
min(len, k + 32); j += 2) {
664 *word = (j & 0x02) ? 0x0000 : 0xffff;
669 for ( ; j <
min(len, k + 32); j += 2) {
672 *word = (j & 0x02) ? 0x5555 : 0xaaaa;
676 for ( ; j <
min(len, k + 32); j += 4) {
677 u32 *
word = (
unsigned int *)&buffer[j];
678 u32 roll = (pattern & 0x80000000) ? 1 : 0;
681 pattern = (pattern << 1) | roll;
686 for (r = 0; r <
retries; r++) {
688 buffer, len, &sshdr);
689 if(result || !scsi_device_online(sdev)) {
692 if (scsi_sense_valid(&sshdr)
695 && sshdr.
asc == 0x24 && sshdr.
ascq == 0x00)
713 if (
memcmp(buffer, ptr, len) != 0)
722 spi_dv_device_compare_inquiry(
struct scsi_device *sdev,
u8 *buffer,
723 u8 *ptr,
const int retries)
727 const char spi_inquiry[] = {
731 for (r = 0; r <
retries; r++) {
737 if(result || !scsi_device_online(sdev)) {
750 if (
memcmp(buffer, ptr, len) != 0)
764 int period = 0, prevperiod = 0;
770 retval = compare_fn(sdev, buffer, ptr,
DV_LOOPS);
778 i->
f->get_iu(starget);
780 i->
f->get_qas(starget);
781 if (i->
f->get_period)
787 if (i->
f->set_iu &&
spi_iu(starget)) {
790 }
else if (i->
f->set_qas &&
spi_qas(starget)) {
795 period = newperiod > period ? newperiod :
period;
799 period += period >> 1;
801 if (
unlikely(period > 0xff || period == prevperiod)) {
816 spi_dv_device_get_echo_buffer(
struct scsi_device *sdev,
u8 *buffer)
825 const char spi_test_unit_ready[] = {
829 const char spi_read_buffer_descriptor[] = {
840 result = spi_execute(sdev, spi_test_unit_ready,
DMA_NONE,
852 result = spi_execute(sdev, spi_read_buffer_descriptor,
859 return buffer[3] + ((buffer[2] & 0x1f) << 8);
875 if (spi_dv_device_compare_inquiry(sdev, buffer, buffer,
DV_LOOPS)
888 if (i->
f->set_width && max_width) {
889 i->
f->set_width(starget, 1);
891 if (spi_dv_device_compare_inquiry(sdev, buffer,
896 i->
f->set_width(starget, 0);
905 if (!i->
f->set_period)
921 DV_SET(period, min_period);
948 if (i->
f->get_signalling)
949 i->
f->get_signalling(shost);
962 spi_dv_retrain(sdev, buffer, buffer + sdev->
inquiry_len,
963 spi_dv_device_compare_inquiry);
966 i->
f->get_dt(starget);
973 if (len == -1 &&
spi_dt(starget))
974 len = spi_dv_device_get_echo_buffer(sdev, buffer);
986 if (spi_dv_retrain(sdev, buffer, buffer + len,
987 spi_dv_device_echo_buffer)
1037 spi_dv_device_internal(sdev, buffer);
1126 unsigned int picosec, kb100;
1127 char *
scsi =
"FAST-?";
1131 picosec = ppr_to_ps[tp->
period];
1133 case 7: scsi =
"FAST-320";
break;
1134 case 8: scsi =
"FAST-160";
break;
1135 case 9: scsi =
"FAST-80";
break;
1137 case 11: scsi =
"FAST-40";
break;
1138 case 12: scsi =
"FAST-20";
break;
1141 picosec = tp->
period * 4000;
1144 else if (tp->
period < 50)
1150 kb100 = (10000000 + picosec / 2) / picosec;
1153 sprint_frac(tmp, picosec, 1000);
1156 "%s %sSCSI %d.%d MB/s %s%s%s%s%s%s%s%s (%s ns, offset %d)\n",
1157 scsi, tp->
width ?
"WIDE " :
"", kb100/10, kb100 % 10,
1158 tp->
dt ?
"DT" :
"ST",
1159 tp->
iu ?
" IU" :
"",
1160 tp->
qas ?
" QAS" :
"",
1162 tp->
rti ?
" RTI" :
"",
1169 tp->
width ?
"wide " :
"");
1210 #ifdef CONFIG_SCSI_CONSTANTS
1211 static const char *
const one_byte_msgs[] = {
1212 "Task Complete",
NULL ,
"Save Pointers",
1213 "Restore Pointers",
"Disconnect",
"Initiator Error",
1214 "Abort Task Set",
"Message Reject",
"Nop",
"Message Parity Error",
1215 "Linked Command Complete",
"Linked Command Complete w/flag",
1216 "Target Reset",
"Abort Task",
"Clear Task Set",
1217 "Initiate Recovery",
"Release Recovery",
1218 "Terminate Process",
"Continue Task",
"Target Transfer Disable",
1219 NULL,
NULL,
"Clear ACA",
"LUN Reset"
1222 static const char *
const two_byte_msgs[] = {
1223 "Simple Queue Tag",
"Head of Queue Tag",
"Ordered Queue Tag",
1224 "Ignore Wide Residue",
"ACA"
1227 static const char *
const extended_msgs[] = {
1228 "Modify Data Pointer",
"Synchronous Data Transfer Request",
1229 "SCSI-I Extended Identify",
"Wide Data Transfer Request",
1230 "Parallel Protocol Request",
"Modify Bidirectional Data Pointer"
1233 static void print_nego(
const unsigned char *
msg,
int per,
int off,
int width)
1237 period_to_str(buf, msg[per]);
1238 printk(
"period = %s ns ", buf);
1242 printk(
"offset = %d ", msg[off]);
1244 printk(
"width = %d ", 8 << msg[width]);
1247 static void print_ptr(
const unsigned char *msg,
int msb,
const char *
desc)
1249 int ptr = (msg[
msb] << 24) | (msg[msb+1] << 16) | (msg[msb+2] << 8) |
1251 printk(
"%s = %d ", desc, ptr);
1262 printk (
"%s ", extended_msgs[msg[2]]);
1264 printk (
"Extended Message, reserved code (0x%02x) ",
1268 print_ptr(msg, 3,
"pointer");
1271 print_nego(msg, 3, 4, 0);
1274 print_nego(msg, 0, 0, 3);
1277 print_nego(msg, 3, 5, 6);
1280 print_ptr(msg, 3,
"out");
1281 print_ptr(msg, 7,
"in");
1284 for (i = 2; i < len; ++
i)
1288 }
else if (msg[0] & 0x80) {
1289 printk(
"Identify disconnect %sallowed %s %d ",
1290 (msg[0] & 0x40) ?
"" :
"not ",
1291 (msg[0] & 0x20) ?
"target routine" :
"lun",
1294 }
else if (msg[0] < 0x1f) {
1295 if (msg[0] <
ARRAY_SIZE(one_byte_msgs) && one_byte_msgs[msg[0]])
1296 printk(
"%s ", one_byte_msgs[msg[0]]);
1298 printk(
"reserved (%02x) ", msg[0]);
1299 }
else if (msg[0] == 0x55) {
1302 }
else if (msg[0] <= 0x2f) {
1303 if ((msg[0] - 0x20) <
ARRAY_SIZE(two_byte_msgs))
1304 printk(
"%s %02x ", two_byte_msgs[msg[0] - 0x20],
1307 printk(
"reserved two byte (%02x %02x) ",
1326 for (i = 0; i < len; ++
i)
1329 }
else if (msg[0] & 0x80) {
1332 }
else if ((msg[0] < 0x1f) || (msg[0] == 0x55)) {
1335 }
else if (msg[0] <= 0x2f) {
1336 printk(
"%02x %02x", msg[0], msg[1]);
1358 != &spi_host_class.class)
1364 if (i->
f->deny_binding && i->
f->deny_binding(sdev->
sdev_target))
1379 shost = dev_to_shost(dev->
parent);
1381 != &spi_host_class.class)
1387 if (i->
f->deny_binding && i->
f->deny_binding(starget))
1390 return &i->
t.target_attrs.ac ==
cont;
1395 spi_setup_transport_attrs,
1397 spi_target_configure);
1401 spi_device_configure);
1403 static struct attribute *host_attributes[] = {
1404 &dev_attr_signalling.attr,
1405 &dev_attr_host_width.attr,
1406 &dev_attr_hba_id.attr,
1411 .attrs = host_attributes,
1424 if (si->
f->set_signalling)
1433 #define TARGET_ATTRIBUTE_HELPER(name) \
1434 (si->f->show_##name ? S_IRUGO : 0) | \
1435 (si->f->set_##name ? S_IWUSR : 0)
1445 if (attr == &dev_attr_period.attr &&
1448 else if (attr == &dev_attr_min_period.attr &&
1451 else if (attr == &dev_attr_offset.attr &&
1454 else if (attr == &dev_attr_max_offset.attr &&
1457 else if (attr == &dev_attr_width.attr &&
1460 else if (attr == &dev_attr_max_width.attr &&
1463 else if (attr == &dev_attr_iu.attr &&
1466 else if (attr == &dev_attr_max_iu.attr &&
1469 else if (attr == &dev_attr_dt.attr &&
1472 else if (attr == &dev_attr_qas.attr &&
1475 else if (attr == &dev_attr_max_qas.attr &&
1478 else if (attr == &dev_attr_wr_flow.attr &&
1481 else if (attr == &dev_attr_rd_strm.attr &&
1484 else if (attr == &dev_attr_rti.attr &&
1487 else if (attr == &dev_attr_pcomp_en.attr &&
1490 else if (attr == &dev_attr_hold_mcs.attr &&
1493 else if (attr == &dev_attr_revalidate.attr)
1499 static struct attribute *target_attributes[] = {
1500 &dev_attr_period.attr,
1501 &dev_attr_min_period.attr,
1502 &dev_attr_offset.attr,
1503 &dev_attr_max_offset.attr,
1504 &dev_attr_width.attr,
1505 &dev_attr_max_width.attr,
1507 &dev_attr_max_iu.attr,
1510 &dev_attr_max_qas.attr,
1511 &dev_attr_wr_flow.attr,
1512 &dev_attr_rd_strm.attr,
1514 &dev_attr_pcomp_en.attr,
1515 &dev_attr_hold_mcs.attr,
1516 &dev_attr_revalidate.attr,
1521 .attrs = target_attributes,
1522 .is_visible = target_attribute_is_visible,
1546 i->
t.target_attrs.ac.class = &spi_transport_class.class;
1547 i->
t.target_attrs.ac.grp = &target_attribute_group;
1548 i->
t.target_attrs.ac.match = spi_target_match;
1549 transport_container_register(&i->
t.target_attrs);
1551 i->
t.host_attrs.ac.class = &spi_host_class.class;
1552 i->
t.host_attrs.ac.grp = &host_attribute_group;
1553 i->
t.host_attrs.ac.match = spi_host_match;
1554 transport_container_register(&i->
t.host_attrs);
1566 transport_container_unregister(&i->
t.target_attrs);
1567 transport_container_unregister(&i->
t.host_attrs);
1573 static __init int spi_transport_init(
void)
1576 "SCSI Parallel Transport Class");
1580 for (i = 0; spi_static_device_list[
i].vendor; i++)
1582 spi_static_device_list[i].
vendor,
1583 spi_static_device_list[i].model,
1585 spi_static_device_list[i].
flags,
1596 static void __exit spi_transport_exit(
void)