0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026 #include <linux/init.h>
0027 #include <linux/module.h>
0028 #include <linux/jiffies.h>
0029 #include <linux/err.h>
0030 #include <linux/slab.h>
0031 #include <linux/string.h>
0032 #include <linux/blkdev.h>
0033 #include <linux/bsg.h>
0034
0035 #include <scsi/scsi.h>
0036 #include <scsi/scsi_cmnd.h>
0037 #include <scsi/scsi_device.h>
0038 #include <scsi/scsi_host.h>
0039 #include <scsi/scsi_transport.h>
0040 #include <scsi/scsi_transport_sas.h>
0041
0042 #include "scsi_sas_internal.h"
0043 struct sas_host_attrs {
0044 struct list_head rphy_list;
0045 struct mutex lock;
0046 struct request_queue *q;
0047 u32 next_target_id;
0048 u32 next_expander_id;
0049 int next_port_id;
0050 };
0051 #define to_sas_host_attrs(host) ((struct sas_host_attrs *)(host)->shost_data)
0052
0053
0054
0055
0056
0057 #define SAS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
0058 struct device_attribute dev_attr_##_prefix##_##_name = \
0059 __ATTR(_name,_mode,_show,_store)
0060
0061
0062
0063
0064
0065
0066 #define sas_bitfield_name_match(title, table) \
0067 static ssize_t \
0068 get_sas_##title##_names(u32 table_key, char *buf) \
0069 { \
0070 char *prefix = ""; \
0071 ssize_t len = 0; \
0072 int i; \
0073 \
0074 for (i = 0; i < ARRAY_SIZE(table); i++) { \
0075 if (table[i].value & table_key) { \
0076 len += sprintf(buf + len, "%s%s", \
0077 prefix, table[i].name); \
0078 prefix = ", "; \
0079 } \
0080 } \
0081 len += sprintf(buf + len, "\n"); \
0082 return len; \
0083 }
0084
0085 #define sas_bitfield_name_set(title, table) \
0086 static ssize_t \
0087 set_sas_##title##_names(u32 *table_key, const char *buf) \
0088 { \
0089 ssize_t len = 0; \
0090 int i; \
0091 \
0092 for (i = 0; i < ARRAY_SIZE(table); i++) { \
0093 len = strlen(table[i].name); \
0094 if (strncmp(buf, table[i].name, len) == 0 && \
0095 (buf[len] == '\n' || buf[len] == '\0')) { \
0096 *table_key = table[i].value; \
0097 return 0; \
0098 } \
0099 } \
0100 return -EINVAL; \
0101 }
0102
0103 #define sas_bitfield_name_search(title, table) \
0104 static ssize_t \
0105 get_sas_##title##_names(u32 table_key, char *buf) \
0106 { \
0107 ssize_t len = 0; \
0108 int i; \
0109 \
0110 for (i = 0; i < ARRAY_SIZE(table); i++) { \
0111 if (table[i].value == table_key) { \
0112 len += sprintf(buf + len, "%s", \
0113 table[i].name); \
0114 break; \
0115 } \
0116 } \
0117 len += sprintf(buf + len, "\n"); \
0118 return len; \
0119 }
0120
0121 static struct {
0122 u32 value;
0123 char *name;
0124 } sas_device_type_names[] = {
0125 { SAS_PHY_UNUSED, "unused" },
0126 { SAS_END_DEVICE, "end device" },
0127 { SAS_EDGE_EXPANDER_DEVICE, "edge expander" },
0128 { SAS_FANOUT_EXPANDER_DEVICE, "fanout expander" },
0129 };
0130 sas_bitfield_name_search(device_type, sas_device_type_names)
0131
0132
0133 static struct {
0134 u32 value;
0135 char *name;
0136 } sas_protocol_names[] = {
0137 { SAS_PROTOCOL_SATA, "sata" },
0138 { SAS_PROTOCOL_SMP, "smp" },
0139 { SAS_PROTOCOL_STP, "stp" },
0140 { SAS_PROTOCOL_SSP, "ssp" },
0141 };
0142 sas_bitfield_name_match(protocol, sas_protocol_names)
0143
0144 static struct {
0145 u32 value;
0146 char *name;
0147 } sas_linkspeed_names[] = {
0148 { SAS_LINK_RATE_UNKNOWN, "Unknown" },
0149 { SAS_PHY_DISABLED, "Phy disabled" },
0150 { SAS_LINK_RATE_FAILED, "Link Rate failed" },
0151 { SAS_SATA_SPINUP_HOLD, "Spin-up hold" },
0152 { SAS_LINK_RATE_1_5_GBPS, "1.5 Gbit" },
0153 { SAS_LINK_RATE_3_0_GBPS, "3.0 Gbit" },
0154 { SAS_LINK_RATE_6_0_GBPS, "6.0 Gbit" },
0155 { SAS_LINK_RATE_12_0_GBPS, "12.0 Gbit" },
0156 { SAS_LINK_RATE_22_5_GBPS, "22.5 Gbit" },
0157 };
0158 sas_bitfield_name_search(linkspeed, sas_linkspeed_names)
0159 sas_bitfield_name_set(linkspeed, sas_linkspeed_names)
0160
0161 static struct sas_end_device *sas_sdev_to_rdev(struct scsi_device *sdev)
0162 {
0163 struct sas_rphy *rphy = target_to_rphy(sdev->sdev_target);
0164 struct sas_end_device *rdev;
0165
0166 BUG_ON(rphy->identify.device_type != SAS_END_DEVICE);
0167
0168 rdev = rphy_to_end_device(rphy);
0169 return rdev;
0170 }
0171
0172 static int sas_smp_dispatch(struct bsg_job *job)
0173 {
0174 struct Scsi_Host *shost = dev_to_shost(job->dev);
0175 struct sas_rphy *rphy = NULL;
0176
0177 if (!scsi_is_host_device(job->dev))
0178 rphy = dev_to_rphy(job->dev);
0179
0180 if (!job->reply_payload.payload_len) {
0181 dev_warn(job->dev, "space for a smp response is missing\n");
0182 bsg_job_done(job, -EINVAL, 0);
0183 return 0;
0184 }
0185
0186 to_sas_internal(shost->transportt)->f->smp_handler(job, shost, rphy);
0187 return 0;
0188 }
0189
0190 static int sas_bsg_initialize(struct Scsi_Host *shost, struct sas_rphy *rphy)
0191 {
0192 struct request_queue *q;
0193
0194 if (!to_sas_internal(shost->transportt)->f->smp_handler) {
0195 printk("%s can't handle SMP requests\n", shost->hostt->name);
0196 return 0;
0197 }
0198
0199 if (rphy) {
0200 q = bsg_setup_queue(&rphy->dev, dev_name(&rphy->dev),
0201 sas_smp_dispatch, NULL, 0);
0202 if (IS_ERR(q))
0203 return PTR_ERR(q);
0204 rphy->q = q;
0205 } else {
0206 char name[20];
0207
0208 snprintf(name, sizeof(name), "sas_host%d", shost->host_no);
0209 q = bsg_setup_queue(&shost->shost_gendev, name,
0210 sas_smp_dispatch, NULL, 0);
0211 if (IS_ERR(q))
0212 return PTR_ERR(q);
0213 to_sas_host_attrs(shost)->q = q;
0214 }
0215
0216 return 0;
0217 }
0218
0219
0220
0221
0222
0223 static int sas_host_setup(struct transport_container *tc, struct device *dev,
0224 struct device *cdev)
0225 {
0226 struct Scsi_Host *shost = dev_to_shost(dev);
0227 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
0228 struct device *dma_dev = shost->dma_dev;
0229
0230 INIT_LIST_HEAD(&sas_host->rphy_list);
0231 mutex_init(&sas_host->lock);
0232 sas_host->next_target_id = 0;
0233 sas_host->next_expander_id = 0;
0234 sas_host->next_port_id = 0;
0235
0236 if (sas_bsg_initialize(shost, NULL))
0237 dev_printk(KERN_ERR, dev, "fail to a bsg device %d\n",
0238 shost->host_no);
0239
0240 if (dma_dev->dma_mask) {
0241 shost->opt_sectors = min_t(unsigned int, shost->max_sectors,
0242 dma_opt_mapping_size(dma_dev) >> SECTOR_SHIFT);
0243 }
0244
0245 return 0;
0246 }
0247
0248 static int sas_host_remove(struct transport_container *tc, struct device *dev,
0249 struct device *cdev)
0250 {
0251 struct Scsi_Host *shost = dev_to_shost(dev);
0252 struct request_queue *q = to_sas_host_attrs(shost)->q;
0253
0254 bsg_remove_queue(q);
0255 return 0;
0256 }
0257
0258 static DECLARE_TRANSPORT_CLASS(sas_host_class,
0259 "sas_host", sas_host_setup, sas_host_remove, NULL);
0260
0261 static int sas_host_match(struct attribute_container *cont,
0262 struct device *dev)
0263 {
0264 struct Scsi_Host *shost;
0265 struct sas_internal *i;
0266
0267 if (!scsi_is_host_device(dev))
0268 return 0;
0269 shost = dev_to_shost(dev);
0270
0271 if (!shost->transportt)
0272 return 0;
0273 if (shost->transportt->host_attrs.ac.class !=
0274 &sas_host_class.class)
0275 return 0;
0276
0277 i = to_sas_internal(shost->transportt);
0278 return &i->t.host_attrs.ac == cont;
0279 }
0280
0281 static int do_sas_phy_delete(struct device *dev, void *data)
0282 {
0283 int pass = (int)(unsigned long)data;
0284
0285 if (pass == 0 && scsi_is_sas_port(dev))
0286 sas_port_delete(dev_to_sas_port(dev));
0287 else if (pass == 1 && scsi_is_sas_phy(dev))
0288 sas_phy_delete(dev_to_phy(dev));
0289 return 0;
0290 }
0291
0292
0293
0294
0295
0296
0297
0298 void sas_remove_children(struct device *dev)
0299 {
0300 device_for_each_child(dev, (void *)0, do_sas_phy_delete);
0301 device_for_each_child(dev, (void *)1, do_sas_phy_delete);
0302 }
0303 EXPORT_SYMBOL(sas_remove_children);
0304
0305
0306
0307
0308
0309
0310
0311
0312
0313
0314
0315 void sas_remove_host(struct Scsi_Host *shost)
0316 {
0317 sas_remove_children(&shost->shost_gendev);
0318 scsi_remove_host(shost);
0319 }
0320 EXPORT_SYMBOL(sas_remove_host);
0321
0322
0323
0324
0325
0326
0327
0328 u64 sas_get_address(struct scsi_device *sdev)
0329 {
0330 struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
0331
0332 return rdev->rphy.identify.sas_address;
0333 }
0334 EXPORT_SYMBOL(sas_get_address);
0335
0336
0337
0338
0339
0340
0341
0342
0343
0344 unsigned int
0345 sas_tlr_supported(struct scsi_device *sdev)
0346 {
0347 const int vpd_len = 32;
0348 struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
0349 char *buffer = kzalloc(vpd_len, GFP_KERNEL);
0350 int ret = 0;
0351
0352 if (!buffer)
0353 goto out;
0354
0355 if (scsi_get_vpd_page(sdev, 0x90, buffer, vpd_len))
0356 goto out;
0357
0358
0359
0360
0361
0362
0363
0364 ret = buffer[12] & 0x01;
0365
0366 out:
0367 kfree(buffer);
0368 rdev->tlr_supported = ret;
0369 return ret;
0370
0371 }
0372 EXPORT_SYMBOL_GPL(sas_tlr_supported);
0373
0374
0375
0376
0377
0378
0379
0380
0381 void
0382 sas_disable_tlr(struct scsi_device *sdev)
0383 {
0384 struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
0385
0386 rdev->tlr_enabled = 0;
0387 }
0388 EXPORT_SYMBOL_GPL(sas_disable_tlr);
0389
0390
0391
0392
0393
0394
0395
0396
0397 void sas_enable_tlr(struct scsi_device *sdev)
0398 {
0399 unsigned int tlr_supported = 0;
0400 tlr_supported = sas_tlr_supported(sdev);
0401
0402 if (tlr_supported) {
0403 struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
0404
0405 rdev->tlr_enabled = 1;
0406 }
0407
0408 return;
0409 }
0410 EXPORT_SYMBOL_GPL(sas_enable_tlr);
0411
0412 unsigned int sas_is_tlr_enabled(struct scsi_device *sdev)
0413 {
0414 struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
0415 return rdev->tlr_enabled;
0416 }
0417 EXPORT_SYMBOL_GPL(sas_is_tlr_enabled);
0418
0419
0420
0421
0422
0423 #define sas_phy_show_simple(field, name, format_string, cast) \
0424 static ssize_t \
0425 show_sas_phy_##name(struct device *dev, \
0426 struct device_attribute *attr, char *buf) \
0427 { \
0428 struct sas_phy *phy = transport_class_to_phy(dev); \
0429 \
0430 return snprintf(buf, 20, format_string, cast phy->field); \
0431 }
0432
0433 #define sas_phy_simple_attr(field, name, format_string, type) \
0434 sas_phy_show_simple(field, name, format_string, (type)) \
0435 static DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
0436
0437 #define sas_phy_show_protocol(field, name) \
0438 static ssize_t \
0439 show_sas_phy_##name(struct device *dev, \
0440 struct device_attribute *attr, char *buf) \
0441 { \
0442 struct sas_phy *phy = transport_class_to_phy(dev); \
0443 \
0444 if (!phy->field) \
0445 return snprintf(buf, 20, "none\n"); \
0446 return get_sas_protocol_names(phy->field, buf); \
0447 }
0448
0449 #define sas_phy_protocol_attr(field, name) \
0450 sas_phy_show_protocol(field, name) \
0451 static DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
0452
0453 #define sas_phy_show_linkspeed(field) \
0454 static ssize_t \
0455 show_sas_phy_##field(struct device *dev, \
0456 struct device_attribute *attr, char *buf) \
0457 { \
0458 struct sas_phy *phy = transport_class_to_phy(dev); \
0459 \
0460 return get_sas_linkspeed_names(phy->field, buf); \
0461 }
0462
0463
0464 #define sas_phy_store_linkspeed(field) \
0465 static ssize_t \
0466 store_sas_phy_##field(struct device *dev, \
0467 struct device_attribute *attr, \
0468 const char *buf, size_t count) \
0469 { \
0470 struct sas_phy *phy = transport_class_to_phy(dev); \
0471 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); \
0472 struct sas_internal *i = to_sas_internal(shost->transportt); \
0473 u32 value; \
0474 struct sas_phy_linkrates rates = {0}; \
0475 int error; \
0476 \
0477 error = set_sas_linkspeed_names(&value, buf); \
0478 if (error) \
0479 return error; \
0480 rates.field = value; \
0481 error = i->f->set_phy_speed(phy, &rates); \
0482 \
0483 return error ? error : count; \
0484 }
0485
0486 #define sas_phy_linkspeed_rw_attr(field) \
0487 sas_phy_show_linkspeed(field) \
0488 sas_phy_store_linkspeed(field) \
0489 static DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, \
0490 store_sas_phy_##field)
0491
0492 #define sas_phy_linkspeed_attr(field) \
0493 sas_phy_show_linkspeed(field) \
0494 static DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
0495
0496
0497 #define sas_phy_show_linkerror(field) \
0498 static ssize_t \
0499 show_sas_phy_##field(struct device *dev, \
0500 struct device_attribute *attr, char *buf) \
0501 { \
0502 struct sas_phy *phy = transport_class_to_phy(dev); \
0503 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); \
0504 struct sas_internal *i = to_sas_internal(shost->transportt); \
0505 int error; \
0506 \
0507 error = i->f->get_linkerrors ? i->f->get_linkerrors(phy) : 0; \
0508 if (error) \
0509 return error; \
0510 return snprintf(buf, 20, "%u\n", phy->field); \
0511 }
0512
0513 #define sas_phy_linkerror_attr(field) \
0514 sas_phy_show_linkerror(field) \
0515 static DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
0516
0517
0518 static ssize_t
0519 show_sas_device_type(struct device *dev,
0520 struct device_attribute *attr, char *buf)
0521 {
0522 struct sas_phy *phy = transport_class_to_phy(dev);
0523
0524 if (!phy->identify.device_type)
0525 return snprintf(buf, 20, "none\n");
0526 return get_sas_device_type_names(phy->identify.device_type, buf);
0527 }
0528 static DEVICE_ATTR(device_type, S_IRUGO, show_sas_device_type, NULL);
0529
0530 static ssize_t do_sas_phy_enable(struct device *dev,
0531 size_t count, int enable)
0532 {
0533 struct sas_phy *phy = transport_class_to_phy(dev);
0534 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
0535 struct sas_internal *i = to_sas_internal(shost->transportt);
0536 int error;
0537
0538 error = i->f->phy_enable(phy, enable);
0539 if (error)
0540 return error;
0541 phy->enabled = enable;
0542 return count;
0543 };
0544
0545 static ssize_t
0546 store_sas_phy_enable(struct device *dev, struct device_attribute *attr,
0547 const char *buf, size_t count)
0548 {
0549 if (count < 1)
0550 return -EINVAL;
0551
0552 switch (buf[0]) {
0553 case '0':
0554 do_sas_phy_enable(dev, count, 0);
0555 break;
0556 case '1':
0557 do_sas_phy_enable(dev, count, 1);
0558 break;
0559 default:
0560 return -EINVAL;
0561 }
0562
0563 return count;
0564 }
0565
0566 static ssize_t
0567 show_sas_phy_enable(struct device *dev, struct device_attribute *attr,
0568 char *buf)
0569 {
0570 struct sas_phy *phy = transport_class_to_phy(dev);
0571
0572 return snprintf(buf, 20, "%d\n", phy->enabled);
0573 }
0574
0575 static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, show_sas_phy_enable,
0576 store_sas_phy_enable);
0577
0578 static ssize_t
0579 do_sas_phy_reset(struct device *dev, size_t count, int hard_reset)
0580 {
0581 struct sas_phy *phy = transport_class_to_phy(dev);
0582 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
0583 struct sas_internal *i = to_sas_internal(shost->transportt);
0584 int error;
0585
0586 error = i->f->phy_reset(phy, hard_reset);
0587 if (error)
0588 return error;
0589 phy->enabled = 1;
0590 return count;
0591 };
0592
0593 static ssize_t
0594 store_sas_link_reset(struct device *dev, struct device_attribute *attr,
0595 const char *buf, size_t count)
0596 {
0597 return do_sas_phy_reset(dev, count, 0);
0598 }
0599 static DEVICE_ATTR(link_reset, S_IWUSR, NULL, store_sas_link_reset);
0600
0601 static ssize_t
0602 store_sas_hard_reset(struct device *dev, struct device_attribute *attr,
0603 const char *buf, size_t count)
0604 {
0605 return do_sas_phy_reset(dev, count, 1);
0606 }
0607 static DEVICE_ATTR(hard_reset, S_IWUSR, NULL, store_sas_hard_reset);
0608
0609 sas_phy_protocol_attr(identify.initiator_port_protocols,
0610 initiator_port_protocols);
0611 sas_phy_protocol_attr(identify.target_port_protocols,
0612 target_port_protocols);
0613 sas_phy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
0614 unsigned long long);
0615 sas_phy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
0616 sas_phy_linkspeed_attr(negotiated_linkrate);
0617 sas_phy_linkspeed_attr(minimum_linkrate_hw);
0618 sas_phy_linkspeed_rw_attr(minimum_linkrate);
0619 sas_phy_linkspeed_attr(maximum_linkrate_hw);
0620 sas_phy_linkspeed_rw_attr(maximum_linkrate);
0621 sas_phy_linkerror_attr(invalid_dword_count);
0622 sas_phy_linkerror_attr(running_disparity_error_count);
0623 sas_phy_linkerror_attr(loss_of_dword_sync_count);
0624 sas_phy_linkerror_attr(phy_reset_problem_count);
0625
0626 static int sas_phy_setup(struct transport_container *tc, struct device *dev,
0627 struct device *cdev)
0628 {
0629 struct sas_phy *phy = dev_to_phy(dev);
0630 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
0631 struct sas_internal *i = to_sas_internal(shost->transportt);
0632
0633 if (i->f->phy_setup)
0634 i->f->phy_setup(phy);
0635
0636 return 0;
0637 }
0638
0639 static DECLARE_TRANSPORT_CLASS(sas_phy_class,
0640 "sas_phy", sas_phy_setup, NULL, NULL);
0641
0642 static int sas_phy_match(struct attribute_container *cont, struct device *dev)
0643 {
0644 struct Scsi_Host *shost;
0645 struct sas_internal *i;
0646
0647 if (!scsi_is_sas_phy(dev))
0648 return 0;
0649 shost = dev_to_shost(dev->parent);
0650
0651 if (!shost->transportt)
0652 return 0;
0653 if (shost->transportt->host_attrs.ac.class !=
0654 &sas_host_class.class)
0655 return 0;
0656
0657 i = to_sas_internal(shost->transportt);
0658 return &i->phy_attr_cont.ac == cont;
0659 }
0660
0661 static void sas_phy_release(struct device *dev)
0662 {
0663 struct sas_phy *phy = dev_to_phy(dev);
0664 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
0665 struct sas_internal *i = to_sas_internal(shost->transportt);
0666
0667 if (i->f->phy_release)
0668 i->f->phy_release(phy);
0669 put_device(dev->parent);
0670 kfree(phy);
0671 }
0672
0673
0674
0675
0676
0677
0678
0679
0680
0681
0682
0683
0684
0685 struct sas_phy *sas_phy_alloc(struct device *parent, int number)
0686 {
0687 struct Scsi_Host *shost = dev_to_shost(parent);
0688 struct sas_phy *phy;
0689
0690 phy = kzalloc(sizeof(*phy), GFP_KERNEL);
0691 if (!phy)
0692 return NULL;
0693
0694 phy->number = number;
0695 phy->enabled = 1;
0696
0697 device_initialize(&phy->dev);
0698 phy->dev.parent = get_device(parent);
0699 phy->dev.release = sas_phy_release;
0700 INIT_LIST_HEAD(&phy->port_siblings);
0701 if (scsi_is_sas_expander_device(parent)) {
0702 struct sas_rphy *rphy = dev_to_rphy(parent);
0703 dev_set_name(&phy->dev, "phy-%d:%d:%d", shost->host_no,
0704 rphy->scsi_target_id, number);
0705 } else
0706 dev_set_name(&phy->dev, "phy-%d:%d", shost->host_no, number);
0707
0708 transport_setup_device(&phy->dev);
0709
0710 return phy;
0711 }
0712 EXPORT_SYMBOL(sas_phy_alloc);
0713
0714
0715
0716
0717
0718
0719
0720 int sas_phy_add(struct sas_phy *phy)
0721 {
0722 int error;
0723
0724 error = device_add(&phy->dev);
0725 if (!error) {
0726 transport_add_device(&phy->dev);
0727 transport_configure_device(&phy->dev);
0728 }
0729
0730 return error;
0731 }
0732 EXPORT_SYMBOL(sas_phy_add);
0733
0734
0735
0736
0737
0738
0739
0740
0741
0742
0743
0744 void sas_phy_free(struct sas_phy *phy)
0745 {
0746 transport_destroy_device(&phy->dev);
0747 put_device(&phy->dev);
0748 }
0749 EXPORT_SYMBOL(sas_phy_free);
0750
0751
0752
0753
0754
0755
0756
0757
0758 void
0759 sas_phy_delete(struct sas_phy *phy)
0760 {
0761 struct device *dev = &phy->dev;
0762
0763
0764 BUG_ON(!list_empty(&phy->port_siblings));
0765
0766 transport_remove_device(dev);
0767 device_del(dev);
0768 transport_destroy_device(dev);
0769 put_device(dev);
0770 }
0771 EXPORT_SYMBOL(sas_phy_delete);
0772
0773
0774
0775
0776
0777
0778
0779
0780 int scsi_is_sas_phy(const struct device *dev)
0781 {
0782 return dev->release == sas_phy_release;
0783 }
0784 EXPORT_SYMBOL(scsi_is_sas_phy);
0785
0786
0787
0788
0789 #define sas_port_show_simple(field, name, format_string, cast) \
0790 static ssize_t \
0791 show_sas_port_##name(struct device *dev, \
0792 struct device_attribute *attr, char *buf) \
0793 { \
0794 struct sas_port *port = transport_class_to_sas_port(dev); \
0795 \
0796 return snprintf(buf, 20, format_string, cast port->field); \
0797 }
0798
0799 #define sas_port_simple_attr(field, name, format_string, type) \
0800 sas_port_show_simple(field, name, format_string, (type)) \
0801 static DEVICE_ATTR(name, S_IRUGO, show_sas_port_##name, NULL)
0802
0803 sas_port_simple_attr(num_phys, num_phys, "%d\n", int);
0804
0805 static DECLARE_TRANSPORT_CLASS(sas_port_class,
0806 "sas_port", NULL, NULL, NULL);
0807
0808 static int sas_port_match(struct attribute_container *cont, struct device *dev)
0809 {
0810 struct Scsi_Host *shost;
0811 struct sas_internal *i;
0812
0813 if (!scsi_is_sas_port(dev))
0814 return 0;
0815 shost = dev_to_shost(dev->parent);
0816
0817 if (!shost->transportt)
0818 return 0;
0819 if (shost->transportt->host_attrs.ac.class !=
0820 &sas_host_class.class)
0821 return 0;
0822
0823 i = to_sas_internal(shost->transportt);
0824 return &i->port_attr_cont.ac == cont;
0825 }
0826
0827
0828 static void sas_port_release(struct device *dev)
0829 {
0830 struct sas_port *port = dev_to_sas_port(dev);
0831
0832 BUG_ON(!list_empty(&port->phy_list));
0833
0834 put_device(dev->parent);
0835 kfree(port);
0836 }
0837
0838 static void sas_port_create_link(struct sas_port *port,
0839 struct sas_phy *phy)
0840 {
0841 int res;
0842
0843 res = sysfs_create_link(&port->dev.kobj, &phy->dev.kobj,
0844 dev_name(&phy->dev));
0845 if (res)
0846 goto err;
0847 res = sysfs_create_link(&phy->dev.kobj, &port->dev.kobj, "port");
0848 if (res)
0849 goto err;
0850 return;
0851 err:
0852 printk(KERN_ERR "%s: Cannot create port links, err=%d\n",
0853 __func__, res);
0854 }
0855
0856 static void sas_port_delete_link(struct sas_port *port,
0857 struct sas_phy *phy)
0858 {
0859 sysfs_remove_link(&port->dev.kobj, dev_name(&phy->dev));
0860 sysfs_remove_link(&phy->dev.kobj, "port");
0861 }
0862
0863
0864
0865
0866
0867
0868
0869
0870
0871
0872
0873
0874 struct sas_port *sas_port_alloc(struct device *parent, int port_id)
0875 {
0876 struct Scsi_Host *shost = dev_to_shost(parent);
0877 struct sas_port *port;
0878
0879 port = kzalloc(sizeof(*port), GFP_KERNEL);
0880 if (!port)
0881 return NULL;
0882
0883 port->port_identifier = port_id;
0884
0885 device_initialize(&port->dev);
0886
0887 port->dev.parent = get_device(parent);
0888 port->dev.release = sas_port_release;
0889
0890 mutex_init(&port->phy_list_mutex);
0891 INIT_LIST_HEAD(&port->phy_list);
0892
0893 if (scsi_is_sas_expander_device(parent)) {
0894 struct sas_rphy *rphy = dev_to_rphy(parent);
0895 dev_set_name(&port->dev, "port-%d:%d:%d", shost->host_no,
0896 rphy->scsi_target_id, port->port_identifier);
0897 } else
0898 dev_set_name(&port->dev, "port-%d:%d", shost->host_no,
0899 port->port_identifier);
0900
0901 transport_setup_device(&port->dev);
0902
0903 return port;
0904 }
0905 EXPORT_SYMBOL(sas_port_alloc);
0906
0907
0908
0909
0910
0911
0912
0913
0914
0915
0916
0917
0918
0919 struct sas_port *sas_port_alloc_num(struct device *parent)
0920 {
0921 int index;
0922 struct Scsi_Host *shost = dev_to_shost(parent);
0923 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
0924
0925
0926 mutex_lock(&sas_host->lock);
0927 if (scsi_is_sas_expander_device(parent)) {
0928 struct sas_rphy *rphy = dev_to_rphy(parent);
0929 struct sas_expander_device *exp = rphy_to_expander_device(rphy);
0930
0931 index = exp->next_port_id++;
0932 } else
0933 index = sas_host->next_port_id++;
0934 mutex_unlock(&sas_host->lock);
0935 return sas_port_alloc(parent, index);
0936 }
0937 EXPORT_SYMBOL(sas_port_alloc_num);
0938
0939
0940
0941
0942
0943
0944
0945 int sas_port_add(struct sas_port *port)
0946 {
0947 int error;
0948
0949
0950 BUG_ON(!list_empty(&port->phy_list));
0951
0952 error = device_add(&port->dev);
0953
0954 if (error)
0955 return error;
0956
0957 transport_add_device(&port->dev);
0958 transport_configure_device(&port->dev);
0959
0960 return 0;
0961 }
0962 EXPORT_SYMBOL(sas_port_add);
0963
0964
0965
0966
0967
0968
0969
0970
0971
0972
0973
0974 void sas_port_free(struct sas_port *port)
0975 {
0976 transport_destroy_device(&port->dev);
0977 put_device(&port->dev);
0978 }
0979 EXPORT_SYMBOL(sas_port_free);
0980
0981
0982
0983
0984
0985
0986
0987
0988 void sas_port_delete(struct sas_port *port)
0989 {
0990 struct device *dev = &port->dev;
0991 struct sas_phy *phy, *tmp_phy;
0992
0993 if (port->rphy) {
0994 sas_rphy_delete(port->rphy);
0995 port->rphy = NULL;
0996 }
0997
0998 mutex_lock(&port->phy_list_mutex);
0999 list_for_each_entry_safe(phy, tmp_phy, &port->phy_list,
1000 port_siblings) {
1001 sas_port_delete_link(port, phy);
1002 list_del_init(&phy->port_siblings);
1003 }
1004 mutex_unlock(&port->phy_list_mutex);
1005
1006 if (port->is_backlink) {
1007 struct device *parent = port->dev.parent;
1008
1009 sysfs_remove_link(&port->dev.kobj, dev_name(parent));
1010 port->is_backlink = 0;
1011 }
1012
1013 transport_remove_device(dev);
1014 device_del(dev);
1015 transport_destroy_device(dev);
1016 put_device(dev);
1017 }
1018 EXPORT_SYMBOL(sas_port_delete);
1019
1020
1021
1022
1023
1024
1025
1026
1027 int scsi_is_sas_port(const struct device *dev)
1028 {
1029 return dev->release == sas_port_release;
1030 }
1031 EXPORT_SYMBOL(scsi_is_sas_port);
1032
1033
1034
1035
1036
1037 struct sas_phy *sas_port_get_phy(struct sas_port *port)
1038 {
1039 struct sas_phy *phy;
1040
1041 mutex_lock(&port->phy_list_mutex);
1042 if (list_empty(&port->phy_list))
1043 phy = NULL;
1044 else {
1045 struct list_head *ent = port->phy_list.next;
1046
1047 phy = list_entry(ent, typeof(*phy), port_siblings);
1048 get_device(&phy->dev);
1049 }
1050 mutex_unlock(&port->phy_list_mutex);
1051
1052 return phy;
1053 }
1054 EXPORT_SYMBOL(sas_port_get_phy);
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068 void sas_port_add_phy(struct sas_port *port, struct sas_phy *phy)
1069 {
1070 mutex_lock(&port->phy_list_mutex);
1071 if (unlikely(!list_empty(&phy->port_siblings))) {
1072
1073 struct sas_phy *tmp;
1074
1075 list_for_each_entry(tmp, &port->phy_list, port_siblings)
1076 if (tmp == phy)
1077 break;
1078
1079
1080 if (unlikely(tmp != phy)) {
1081 dev_printk(KERN_ERR, &port->dev, "trying to add phy %s fails: it's already part of another port\n",
1082 dev_name(&phy->dev));
1083 BUG();
1084 }
1085 } else {
1086 sas_port_create_link(port, phy);
1087 list_add_tail(&phy->port_siblings, &port->phy_list);
1088 port->num_phys++;
1089 }
1090 mutex_unlock(&port->phy_list_mutex);
1091 }
1092 EXPORT_SYMBOL(sas_port_add_phy);
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102 void sas_port_delete_phy(struct sas_port *port, struct sas_phy *phy)
1103 {
1104 mutex_lock(&port->phy_list_mutex);
1105 sas_port_delete_link(port, phy);
1106 list_del_init(&phy->port_siblings);
1107 port->num_phys--;
1108 mutex_unlock(&port->phy_list_mutex);
1109 }
1110 EXPORT_SYMBOL(sas_port_delete_phy);
1111
1112 void sas_port_mark_backlink(struct sas_port *port)
1113 {
1114 int res;
1115 struct device *parent = port->dev.parent->parent->parent;
1116
1117 if (port->is_backlink)
1118 return;
1119 port->is_backlink = 1;
1120 res = sysfs_create_link(&port->dev.kobj, &parent->kobj,
1121 dev_name(parent));
1122 if (res)
1123 goto err;
1124 return;
1125 err:
1126 printk(KERN_ERR "%s: Cannot create port backlink, err=%d\n",
1127 __func__, res);
1128
1129 }
1130 EXPORT_SYMBOL(sas_port_mark_backlink);
1131
1132
1133
1134
1135
1136 #define sas_rphy_show_simple(field, name, format_string, cast) \
1137 static ssize_t \
1138 show_sas_rphy_##name(struct device *dev, \
1139 struct device_attribute *attr, char *buf) \
1140 { \
1141 struct sas_rphy *rphy = transport_class_to_rphy(dev); \
1142 \
1143 return snprintf(buf, 20, format_string, cast rphy->field); \
1144 }
1145
1146 #define sas_rphy_simple_attr(field, name, format_string, type) \
1147 sas_rphy_show_simple(field, name, format_string, (type)) \
1148 static SAS_DEVICE_ATTR(rphy, name, S_IRUGO, \
1149 show_sas_rphy_##name, NULL)
1150
1151 #define sas_rphy_show_protocol(field, name) \
1152 static ssize_t \
1153 show_sas_rphy_##name(struct device *dev, \
1154 struct device_attribute *attr, char *buf) \
1155 { \
1156 struct sas_rphy *rphy = transport_class_to_rphy(dev); \
1157 \
1158 if (!rphy->field) \
1159 return snprintf(buf, 20, "none\n"); \
1160 return get_sas_protocol_names(rphy->field, buf); \
1161 }
1162
1163 #define sas_rphy_protocol_attr(field, name) \
1164 sas_rphy_show_protocol(field, name) \
1165 static SAS_DEVICE_ATTR(rphy, name, S_IRUGO, \
1166 show_sas_rphy_##name, NULL)
1167
1168 static ssize_t
1169 show_sas_rphy_device_type(struct device *dev,
1170 struct device_attribute *attr, char *buf)
1171 {
1172 struct sas_rphy *rphy = transport_class_to_rphy(dev);
1173
1174 if (!rphy->identify.device_type)
1175 return snprintf(buf, 20, "none\n");
1176 return get_sas_device_type_names(
1177 rphy->identify.device_type, buf);
1178 }
1179
1180 static SAS_DEVICE_ATTR(rphy, device_type, S_IRUGO,
1181 show_sas_rphy_device_type, NULL);
1182
1183 static ssize_t
1184 show_sas_rphy_enclosure_identifier(struct device *dev,
1185 struct device_attribute *attr, char *buf)
1186 {
1187 struct sas_rphy *rphy = transport_class_to_rphy(dev);
1188 struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
1189 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
1190 struct sas_internal *i = to_sas_internal(shost->transportt);
1191 u64 identifier;
1192 int error;
1193
1194 error = i->f->get_enclosure_identifier(rphy, &identifier);
1195 if (error)
1196 return error;
1197 return sprintf(buf, "0x%llx\n", (unsigned long long)identifier);
1198 }
1199
1200 static SAS_DEVICE_ATTR(rphy, enclosure_identifier, S_IRUGO,
1201 show_sas_rphy_enclosure_identifier, NULL);
1202
1203 static ssize_t
1204 show_sas_rphy_bay_identifier(struct device *dev,
1205 struct device_attribute *attr, char *buf)
1206 {
1207 struct sas_rphy *rphy = transport_class_to_rphy(dev);
1208 struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
1209 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
1210 struct sas_internal *i = to_sas_internal(shost->transportt);
1211 int val;
1212
1213 val = i->f->get_bay_identifier(rphy);
1214 if (val < 0)
1215 return val;
1216 return sprintf(buf, "%d\n", val);
1217 }
1218
1219 static SAS_DEVICE_ATTR(rphy, bay_identifier, S_IRUGO,
1220 show_sas_rphy_bay_identifier, NULL);
1221
1222 sas_rphy_protocol_attr(identify.initiator_port_protocols,
1223 initiator_port_protocols);
1224 sas_rphy_protocol_attr(identify.target_port_protocols, target_port_protocols);
1225 sas_rphy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
1226 unsigned long long);
1227 sas_rphy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
1228 sas_rphy_simple_attr(scsi_target_id, scsi_target_id, "%d\n", u32);
1229
1230
1231 #define BUF_SIZE 64
1232
1233 int sas_read_port_mode_page(struct scsi_device *sdev)
1234 {
1235 char *buffer = kzalloc(BUF_SIZE, GFP_KERNEL), *msdata;
1236 struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
1237 struct scsi_mode_data mode_data;
1238 int error;
1239
1240 if (!buffer)
1241 return -ENOMEM;
1242
1243 error = scsi_mode_sense(sdev, 1, 0x19, buffer, BUF_SIZE, 30*HZ, 3,
1244 &mode_data, NULL);
1245
1246 if (error)
1247 goto out;
1248
1249 msdata = buffer + mode_data.header_length +
1250 mode_data.block_descriptor_length;
1251
1252 if (msdata - buffer > BUF_SIZE - 8)
1253 goto out;
1254
1255 error = 0;
1256
1257 rdev->ready_led_meaning = msdata[2] & 0x10 ? 1 : 0;
1258 rdev->I_T_nexus_loss_timeout = (msdata[4] << 8) + msdata[5];
1259 rdev->initiator_response_timeout = (msdata[6] << 8) + msdata[7];
1260
1261 out:
1262 kfree(buffer);
1263 return error;
1264 }
1265 EXPORT_SYMBOL(sas_read_port_mode_page);
1266
1267 static DECLARE_TRANSPORT_CLASS(sas_end_dev_class,
1268 "sas_end_device", NULL, NULL, NULL);
1269
1270 #define sas_end_dev_show_simple(field, name, format_string, cast) \
1271 static ssize_t \
1272 show_sas_end_dev_##name(struct device *dev, \
1273 struct device_attribute *attr, char *buf) \
1274 { \
1275 struct sas_rphy *rphy = transport_class_to_rphy(dev); \
1276 struct sas_end_device *rdev = rphy_to_end_device(rphy); \
1277 \
1278 return snprintf(buf, 20, format_string, cast rdev->field); \
1279 }
1280
1281 #define sas_end_dev_simple_attr(field, name, format_string, type) \
1282 sas_end_dev_show_simple(field, name, format_string, (type)) \
1283 static SAS_DEVICE_ATTR(end_dev, name, S_IRUGO, \
1284 show_sas_end_dev_##name, NULL)
1285
1286 sas_end_dev_simple_attr(ready_led_meaning, ready_led_meaning, "%d\n", int);
1287 sas_end_dev_simple_attr(I_T_nexus_loss_timeout, I_T_nexus_loss_timeout,
1288 "%d\n", int);
1289 sas_end_dev_simple_attr(initiator_response_timeout, initiator_response_timeout,
1290 "%d\n", int);
1291 sas_end_dev_simple_attr(tlr_supported, tlr_supported,
1292 "%d\n", int);
1293 sas_end_dev_simple_attr(tlr_enabled, tlr_enabled,
1294 "%d\n", int);
1295
1296 static DECLARE_TRANSPORT_CLASS(sas_expander_class,
1297 "sas_expander", NULL, NULL, NULL);
1298
1299 #define sas_expander_show_simple(field, name, format_string, cast) \
1300 static ssize_t \
1301 show_sas_expander_##name(struct device *dev, \
1302 struct device_attribute *attr, char *buf) \
1303 { \
1304 struct sas_rphy *rphy = transport_class_to_rphy(dev); \
1305 struct sas_expander_device *edev = rphy_to_expander_device(rphy); \
1306 \
1307 return snprintf(buf, 20, format_string, cast edev->field); \
1308 }
1309
1310 #define sas_expander_simple_attr(field, name, format_string, type) \
1311 sas_expander_show_simple(field, name, format_string, (type)) \
1312 static SAS_DEVICE_ATTR(expander, name, S_IRUGO, \
1313 show_sas_expander_##name, NULL)
1314
1315 sas_expander_simple_attr(vendor_id, vendor_id, "%s\n", char *);
1316 sas_expander_simple_attr(product_id, product_id, "%s\n", char *);
1317 sas_expander_simple_attr(product_rev, product_rev, "%s\n", char *);
1318 sas_expander_simple_attr(component_vendor_id, component_vendor_id,
1319 "%s\n", char *);
1320 sas_expander_simple_attr(component_id, component_id, "%u\n", unsigned int);
1321 sas_expander_simple_attr(component_revision_id, component_revision_id, "%u\n",
1322 unsigned int);
1323 sas_expander_simple_attr(level, level, "%d\n", int);
1324
1325 static DECLARE_TRANSPORT_CLASS(sas_rphy_class,
1326 "sas_device", NULL, NULL, NULL);
1327
1328 static int sas_rphy_match(struct attribute_container *cont, struct device *dev)
1329 {
1330 struct Scsi_Host *shost;
1331 struct sas_internal *i;
1332
1333 if (!scsi_is_sas_rphy(dev))
1334 return 0;
1335 shost = dev_to_shost(dev->parent->parent);
1336
1337 if (!shost->transportt)
1338 return 0;
1339 if (shost->transportt->host_attrs.ac.class !=
1340 &sas_host_class.class)
1341 return 0;
1342
1343 i = to_sas_internal(shost->transportt);
1344 return &i->rphy_attr_cont.ac == cont;
1345 }
1346
1347 static int sas_end_dev_match(struct attribute_container *cont,
1348 struct device *dev)
1349 {
1350 struct Scsi_Host *shost;
1351 struct sas_internal *i;
1352 struct sas_rphy *rphy;
1353
1354 if (!scsi_is_sas_rphy(dev))
1355 return 0;
1356 shost = dev_to_shost(dev->parent->parent);
1357 rphy = dev_to_rphy(dev);
1358
1359 if (!shost->transportt)
1360 return 0;
1361 if (shost->transportt->host_attrs.ac.class !=
1362 &sas_host_class.class)
1363 return 0;
1364
1365 i = to_sas_internal(shost->transportt);
1366 return &i->end_dev_attr_cont.ac == cont &&
1367 rphy->identify.device_type == SAS_END_DEVICE;
1368 }
1369
1370 static int sas_expander_match(struct attribute_container *cont,
1371 struct device *dev)
1372 {
1373 struct Scsi_Host *shost;
1374 struct sas_internal *i;
1375 struct sas_rphy *rphy;
1376
1377 if (!scsi_is_sas_rphy(dev))
1378 return 0;
1379 shost = dev_to_shost(dev->parent->parent);
1380 rphy = dev_to_rphy(dev);
1381
1382 if (!shost->transportt)
1383 return 0;
1384 if (shost->transportt->host_attrs.ac.class !=
1385 &sas_host_class.class)
1386 return 0;
1387
1388 i = to_sas_internal(shost->transportt);
1389 return &i->expander_attr_cont.ac == cont &&
1390 (rphy->identify.device_type == SAS_EDGE_EXPANDER_DEVICE ||
1391 rphy->identify.device_type == SAS_FANOUT_EXPANDER_DEVICE);
1392 }
1393
1394 static void sas_expander_release(struct device *dev)
1395 {
1396 struct sas_rphy *rphy = dev_to_rphy(dev);
1397 struct sas_expander_device *edev = rphy_to_expander_device(rphy);
1398
1399 put_device(dev->parent);
1400 kfree(edev);
1401 }
1402
1403 static void sas_end_device_release(struct device *dev)
1404 {
1405 struct sas_rphy *rphy = dev_to_rphy(dev);
1406 struct sas_end_device *edev = rphy_to_end_device(rphy);
1407
1408 put_device(dev->parent);
1409 kfree(edev);
1410 }
1411
1412
1413
1414
1415
1416
1417
1418
1419 static void sas_rphy_initialize(struct sas_rphy *rphy)
1420 {
1421 INIT_LIST_HEAD(&rphy->list);
1422 }
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433 struct sas_rphy *sas_end_device_alloc(struct sas_port *parent)
1434 {
1435 struct Scsi_Host *shost = dev_to_shost(&parent->dev);
1436 struct sas_end_device *rdev;
1437
1438 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
1439 if (!rdev) {
1440 return NULL;
1441 }
1442
1443 device_initialize(&rdev->rphy.dev);
1444 rdev->rphy.dev.parent = get_device(&parent->dev);
1445 rdev->rphy.dev.release = sas_end_device_release;
1446 if (scsi_is_sas_expander_device(parent->dev.parent)) {
1447 struct sas_rphy *rphy = dev_to_rphy(parent->dev.parent);
1448 dev_set_name(&rdev->rphy.dev, "end_device-%d:%d:%d",
1449 shost->host_no, rphy->scsi_target_id,
1450 parent->port_identifier);
1451 } else
1452 dev_set_name(&rdev->rphy.dev, "end_device-%d:%d",
1453 shost->host_no, parent->port_identifier);
1454 rdev->rphy.identify.device_type = SAS_END_DEVICE;
1455 sas_rphy_initialize(&rdev->rphy);
1456 transport_setup_device(&rdev->rphy.dev);
1457
1458 return &rdev->rphy;
1459 }
1460 EXPORT_SYMBOL(sas_end_device_alloc);
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472 struct sas_rphy *sas_expander_alloc(struct sas_port *parent,
1473 enum sas_device_type type)
1474 {
1475 struct Scsi_Host *shost = dev_to_shost(&parent->dev);
1476 struct sas_expander_device *rdev;
1477 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1478
1479 BUG_ON(type != SAS_EDGE_EXPANDER_DEVICE &&
1480 type != SAS_FANOUT_EXPANDER_DEVICE);
1481
1482 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
1483 if (!rdev) {
1484 return NULL;
1485 }
1486
1487 device_initialize(&rdev->rphy.dev);
1488 rdev->rphy.dev.parent = get_device(&parent->dev);
1489 rdev->rphy.dev.release = sas_expander_release;
1490 mutex_lock(&sas_host->lock);
1491 rdev->rphy.scsi_target_id = sas_host->next_expander_id++;
1492 mutex_unlock(&sas_host->lock);
1493 dev_set_name(&rdev->rphy.dev, "expander-%d:%d",
1494 shost->host_no, rdev->rphy.scsi_target_id);
1495 rdev->rphy.identify.device_type = type;
1496 sas_rphy_initialize(&rdev->rphy);
1497 transport_setup_device(&rdev->rphy.dev);
1498
1499 return &rdev->rphy;
1500 }
1501 EXPORT_SYMBOL(sas_expander_alloc);
1502
1503
1504
1505
1506
1507
1508
1509 int sas_rphy_add(struct sas_rphy *rphy)
1510 {
1511 struct sas_port *parent = dev_to_sas_port(rphy->dev.parent);
1512 struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
1513 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1514 struct sas_identify *identify = &rphy->identify;
1515 int error;
1516
1517 if (parent->rphy)
1518 return -ENXIO;
1519 parent->rphy = rphy;
1520
1521 error = device_add(&rphy->dev);
1522 if (error)
1523 return error;
1524 transport_add_device(&rphy->dev);
1525 transport_configure_device(&rphy->dev);
1526 if (sas_bsg_initialize(shost, rphy))
1527 printk("fail to a bsg device %s\n", dev_name(&rphy->dev));
1528
1529
1530 mutex_lock(&sas_host->lock);
1531 list_add_tail(&rphy->list, &sas_host->rphy_list);
1532 if (identify->device_type == SAS_END_DEVICE &&
1533 (identify->target_port_protocols &
1534 (SAS_PROTOCOL_SSP | SAS_PROTOCOL_STP | SAS_PROTOCOL_SATA)))
1535 rphy->scsi_target_id = sas_host->next_target_id++;
1536 else if (identify->device_type == SAS_END_DEVICE)
1537 rphy->scsi_target_id = -1;
1538 mutex_unlock(&sas_host->lock);
1539
1540 if (identify->device_type == SAS_END_DEVICE &&
1541 rphy->scsi_target_id != -1) {
1542 int lun;
1543
1544 if (identify->target_port_protocols & SAS_PROTOCOL_SSP)
1545 lun = SCAN_WILD_CARD;
1546 else
1547 lun = 0;
1548
1549 scsi_scan_target(&rphy->dev, 0, rphy->scsi_target_id, lun,
1550 SCSI_SCAN_INITIAL);
1551 }
1552
1553 return 0;
1554 }
1555 EXPORT_SYMBOL(sas_rphy_add);
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568 void sas_rphy_free(struct sas_rphy *rphy)
1569 {
1570 struct device *dev = &rphy->dev;
1571 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
1572 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1573
1574 mutex_lock(&sas_host->lock);
1575 list_del(&rphy->list);
1576 mutex_unlock(&sas_host->lock);
1577
1578 transport_destroy_device(dev);
1579
1580 put_device(dev);
1581 }
1582 EXPORT_SYMBOL(sas_rphy_free);
1583
1584
1585
1586
1587
1588
1589
1590 void
1591 sas_rphy_delete(struct sas_rphy *rphy)
1592 {
1593 sas_rphy_remove(rphy);
1594 sas_rphy_free(rphy);
1595 }
1596 EXPORT_SYMBOL(sas_rphy_delete);
1597
1598
1599
1600
1601
1602
1603
1604 void sas_rphy_unlink(struct sas_rphy *rphy)
1605 {
1606 struct sas_port *parent = dev_to_sas_port(rphy->dev.parent);
1607
1608 parent->rphy = NULL;
1609 }
1610 EXPORT_SYMBOL(sas_rphy_unlink);
1611
1612
1613
1614
1615
1616
1617
1618 void
1619 sas_rphy_remove(struct sas_rphy *rphy)
1620 {
1621 struct device *dev = &rphy->dev;
1622
1623 switch (rphy->identify.device_type) {
1624 case SAS_END_DEVICE:
1625 scsi_remove_target(dev);
1626 break;
1627 case SAS_EDGE_EXPANDER_DEVICE:
1628 case SAS_FANOUT_EXPANDER_DEVICE:
1629 sas_remove_children(dev);
1630 break;
1631 default:
1632 break;
1633 }
1634
1635 sas_rphy_unlink(rphy);
1636 bsg_remove_queue(rphy->q);
1637 transport_remove_device(dev);
1638 device_del(dev);
1639 }
1640 EXPORT_SYMBOL(sas_rphy_remove);
1641
1642
1643
1644
1645
1646
1647
1648
1649 int scsi_is_sas_rphy(const struct device *dev)
1650 {
1651 return dev->release == sas_end_device_release ||
1652 dev->release == sas_expander_release;
1653 }
1654 EXPORT_SYMBOL(scsi_is_sas_rphy);
1655
1656
1657
1658
1659
1660
1661 static int sas_user_scan(struct Scsi_Host *shost, uint channel,
1662 uint id, u64 lun)
1663 {
1664 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1665 struct sas_rphy *rphy;
1666
1667 mutex_lock(&sas_host->lock);
1668 list_for_each_entry(rphy, &sas_host->rphy_list, list) {
1669 if (rphy->identify.device_type != SAS_END_DEVICE ||
1670 rphy->scsi_target_id == -1)
1671 continue;
1672
1673 if ((channel == SCAN_WILD_CARD || channel == 0) &&
1674 (id == SCAN_WILD_CARD || id == rphy->scsi_target_id)) {
1675 scsi_scan_target(&rphy->dev, 0, rphy->scsi_target_id,
1676 lun, SCSI_SCAN_MANUAL);
1677 }
1678 }
1679 mutex_unlock(&sas_host->lock);
1680
1681 return 0;
1682 }
1683
1684
1685
1686
1687
1688
1689 #define SETUP_TEMPLATE(attrb, field, perm, test) \
1690 i->private_##attrb[count] = dev_attr_##field; \
1691 i->private_##attrb[count].attr.mode = perm; \
1692 i->attrb[count] = &i->private_##attrb[count]; \
1693 if (test) \
1694 count++
1695
1696 #define SETUP_TEMPLATE_RW(attrb, field, perm, test, ro_test, ro_perm) \
1697 i->private_##attrb[count] = dev_attr_##field; \
1698 i->private_##attrb[count].attr.mode = perm; \
1699 if (ro_test) { \
1700 i->private_##attrb[count].attr.mode = ro_perm; \
1701 i->private_##attrb[count].store = NULL; \
1702 } \
1703 i->attrb[count] = &i->private_##attrb[count]; \
1704 if (test) \
1705 count++
1706
1707 #define SETUP_RPORT_ATTRIBUTE(field) \
1708 SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, 1)
1709
1710 #define SETUP_OPTIONAL_RPORT_ATTRIBUTE(field, func) \
1711 SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, i->f->func)
1712
1713 #define SETUP_PHY_ATTRIBUTE(field) \
1714 SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, 1)
1715
1716 #define SETUP_PHY_ATTRIBUTE_RW(field) \
1717 SETUP_TEMPLATE_RW(phy_attrs, field, S_IRUGO | S_IWUSR, 1, \
1718 !i->f->set_phy_speed, S_IRUGO)
1719
1720 #define SETUP_OPTIONAL_PHY_ATTRIBUTE_RW(field, func) \
1721 SETUP_TEMPLATE_RW(phy_attrs, field, S_IRUGO | S_IWUSR, 1, \
1722 !i->f->func, S_IRUGO)
1723
1724 #define SETUP_PORT_ATTRIBUTE(field) \
1725 SETUP_TEMPLATE(port_attrs, field, S_IRUGO, 1)
1726
1727 #define SETUP_OPTIONAL_PHY_ATTRIBUTE(field, func) \
1728 SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, i->f->func)
1729
1730 #define SETUP_PHY_ATTRIBUTE_WRONLY(field) \
1731 SETUP_TEMPLATE(phy_attrs, field, S_IWUSR, 1)
1732
1733 #define SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(field, func) \
1734 SETUP_TEMPLATE(phy_attrs, field, S_IWUSR, i->f->func)
1735
1736 #define SETUP_END_DEV_ATTRIBUTE(field) \
1737 SETUP_TEMPLATE(end_dev_attrs, field, S_IRUGO, 1)
1738
1739 #define SETUP_EXPANDER_ATTRIBUTE(field) \
1740 SETUP_TEMPLATE(expander_attrs, expander_##field, S_IRUGO, 1)
1741
1742
1743
1744
1745
1746 struct scsi_transport_template *
1747 sas_attach_transport(struct sas_function_template *ft)
1748 {
1749 struct sas_internal *i;
1750 int count;
1751
1752 i = kzalloc(sizeof(struct sas_internal), GFP_KERNEL);
1753 if (!i)
1754 return NULL;
1755
1756 i->t.user_scan = sas_user_scan;
1757
1758 i->t.host_attrs.ac.attrs = &i->host_attrs[0];
1759 i->t.host_attrs.ac.class = &sas_host_class.class;
1760 i->t.host_attrs.ac.match = sas_host_match;
1761 transport_container_register(&i->t.host_attrs);
1762 i->t.host_size = sizeof(struct sas_host_attrs);
1763
1764 i->phy_attr_cont.ac.class = &sas_phy_class.class;
1765 i->phy_attr_cont.ac.attrs = &i->phy_attrs[0];
1766 i->phy_attr_cont.ac.match = sas_phy_match;
1767 transport_container_register(&i->phy_attr_cont);
1768
1769 i->port_attr_cont.ac.class = &sas_port_class.class;
1770 i->port_attr_cont.ac.attrs = &i->port_attrs[0];
1771 i->port_attr_cont.ac.match = sas_port_match;
1772 transport_container_register(&i->port_attr_cont);
1773
1774 i->rphy_attr_cont.ac.class = &sas_rphy_class.class;
1775 i->rphy_attr_cont.ac.attrs = &i->rphy_attrs[0];
1776 i->rphy_attr_cont.ac.match = sas_rphy_match;
1777 transport_container_register(&i->rphy_attr_cont);
1778
1779 i->end_dev_attr_cont.ac.class = &sas_end_dev_class.class;
1780 i->end_dev_attr_cont.ac.attrs = &i->end_dev_attrs[0];
1781 i->end_dev_attr_cont.ac.match = sas_end_dev_match;
1782 transport_container_register(&i->end_dev_attr_cont);
1783
1784 i->expander_attr_cont.ac.class = &sas_expander_class.class;
1785 i->expander_attr_cont.ac.attrs = &i->expander_attrs[0];
1786 i->expander_attr_cont.ac.match = sas_expander_match;
1787 transport_container_register(&i->expander_attr_cont);
1788
1789 i->f = ft;
1790
1791 count = 0;
1792 SETUP_PHY_ATTRIBUTE(initiator_port_protocols);
1793 SETUP_PHY_ATTRIBUTE(target_port_protocols);
1794 SETUP_PHY_ATTRIBUTE(device_type);
1795 SETUP_PHY_ATTRIBUTE(sas_address);
1796 SETUP_PHY_ATTRIBUTE(phy_identifier);
1797 SETUP_PHY_ATTRIBUTE(negotiated_linkrate);
1798 SETUP_PHY_ATTRIBUTE(minimum_linkrate_hw);
1799 SETUP_PHY_ATTRIBUTE_RW(minimum_linkrate);
1800 SETUP_PHY_ATTRIBUTE(maximum_linkrate_hw);
1801 SETUP_PHY_ATTRIBUTE_RW(maximum_linkrate);
1802
1803 SETUP_PHY_ATTRIBUTE(invalid_dword_count);
1804 SETUP_PHY_ATTRIBUTE(running_disparity_error_count);
1805 SETUP_PHY_ATTRIBUTE(loss_of_dword_sync_count);
1806 SETUP_PHY_ATTRIBUTE(phy_reset_problem_count);
1807 SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(link_reset, phy_reset);
1808 SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(hard_reset, phy_reset);
1809 SETUP_OPTIONAL_PHY_ATTRIBUTE_RW(enable, phy_enable);
1810 i->phy_attrs[count] = NULL;
1811
1812 count = 0;
1813 SETUP_PORT_ATTRIBUTE(num_phys);
1814 i->port_attrs[count] = NULL;
1815
1816 count = 0;
1817 SETUP_RPORT_ATTRIBUTE(rphy_initiator_port_protocols);
1818 SETUP_RPORT_ATTRIBUTE(rphy_target_port_protocols);
1819 SETUP_RPORT_ATTRIBUTE(rphy_device_type);
1820 SETUP_RPORT_ATTRIBUTE(rphy_sas_address);
1821 SETUP_RPORT_ATTRIBUTE(rphy_phy_identifier);
1822 SETUP_RPORT_ATTRIBUTE(rphy_scsi_target_id);
1823 SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_enclosure_identifier,
1824 get_enclosure_identifier);
1825 SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_bay_identifier,
1826 get_bay_identifier);
1827 i->rphy_attrs[count] = NULL;
1828
1829 count = 0;
1830 SETUP_END_DEV_ATTRIBUTE(end_dev_ready_led_meaning);
1831 SETUP_END_DEV_ATTRIBUTE(end_dev_I_T_nexus_loss_timeout);
1832 SETUP_END_DEV_ATTRIBUTE(end_dev_initiator_response_timeout);
1833 SETUP_END_DEV_ATTRIBUTE(end_dev_tlr_supported);
1834 SETUP_END_DEV_ATTRIBUTE(end_dev_tlr_enabled);
1835 i->end_dev_attrs[count] = NULL;
1836
1837 count = 0;
1838 SETUP_EXPANDER_ATTRIBUTE(vendor_id);
1839 SETUP_EXPANDER_ATTRIBUTE(product_id);
1840 SETUP_EXPANDER_ATTRIBUTE(product_rev);
1841 SETUP_EXPANDER_ATTRIBUTE(component_vendor_id);
1842 SETUP_EXPANDER_ATTRIBUTE(component_id);
1843 SETUP_EXPANDER_ATTRIBUTE(component_revision_id);
1844 SETUP_EXPANDER_ATTRIBUTE(level);
1845 i->expander_attrs[count] = NULL;
1846
1847 return &i->t;
1848 }
1849 EXPORT_SYMBOL(sas_attach_transport);
1850
1851
1852
1853
1854
1855 void sas_release_transport(struct scsi_transport_template *t)
1856 {
1857 struct sas_internal *i = to_sas_internal(t);
1858
1859 transport_container_unregister(&i->t.host_attrs);
1860 transport_container_unregister(&i->phy_attr_cont);
1861 transport_container_unregister(&i->port_attr_cont);
1862 transport_container_unregister(&i->rphy_attr_cont);
1863 transport_container_unregister(&i->end_dev_attr_cont);
1864 transport_container_unregister(&i->expander_attr_cont);
1865
1866 kfree(i);
1867 }
1868 EXPORT_SYMBOL(sas_release_transport);
1869
1870 static __init int sas_transport_init(void)
1871 {
1872 int error;
1873
1874 error = transport_class_register(&sas_host_class);
1875 if (error)
1876 goto out;
1877 error = transport_class_register(&sas_phy_class);
1878 if (error)
1879 goto out_unregister_transport;
1880 error = transport_class_register(&sas_port_class);
1881 if (error)
1882 goto out_unregister_phy;
1883 error = transport_class_register(&sas_rphy_class);
1884 if (error)
1885 goto out_unregister_port;
1886 error = transport_class_register(&sas_end_dev_class);
1887 if (error)
1888 goto out_unregister_rphy;
1889 error = transport_class_register(&sas_expander_class);
1890 if (error)
1891 goto out_unregister_end_dev;
1892
1893 return 0;
1894
1895 out_unregister_end_dev:
1896 transport_class_unregister(&sas_end_dev_class);
1897 out_unregister_rphy:
1898 transport_class_unregister(&sas_rphy_class);
1899 out_unregister_port:
1900 transport_class_unregister(&sas_port_class);
1901 out_unregister_phy:
1902 transport_class_unregister(&sas_phy_class);
1903 out_unregister_transport:
1904 transport_class_unregister(&sas_host_class);
1905 out:
1906 return error;
1907
1908 }
1909
1910 static void __exit sas_transport_exit(void)
1911 {
1912 transport_class_unregister(&sas_host_class);
1913 transport_class_unregister(&sas_phy_class);
1914 transport_class_unregister(&sas_port_class);
1915 transport_class_unregister(&sas_rphy_class);
1916 transport_class_unregister(&sas_end_dev_class);
1917 transport_class_unregister(&sas_expander_class);
1918 }
1919
1920 MODULE_AUTHOR("Christoph Hellwig");
1921 MODULE_DESCRIPTION("SAS Transport Attributes");
1922 MODULE_LICENSE("GPL");
1923
1924 module_init(sas_transport_init);
1925 module_exit(sas_transport_exit);