PageRenderTime 61ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/scsi/scsi_transport_sas.c

https://bitbucket.org/cyanogenmod/android_kernel_asus_tf300t
C | 1913 lines | 1331 code | 294 blank | 288 comment | 140 complexity | 29fabc3d7e0376f7f13d90769b6c77d1 MD5 | raw file
Possible License(s): LGPL-2.0, AGPL-1.0, GPL-2.0
  1. /*
  2. * Copyright (C) 2005-2006 Dell Inc.
  3. * Released under GPL v2.
  4. *
  5. * Serial Attached SCSI (SAS) transport class.
  6. *
  7. * The SAS transport class contains common code to deal with SAS HBAs,
  8. * an aproximated representation of SAS topologies in the driver model,
  9. * and various sysfs attributes to expose these topologies and management
  10. * interfaces to userspace.
  11. *
  12. * In addition to the basic SCSI core objects this transport class
  13. * introduces two additional intermediate objects: The SAS PHY
  14. * as represented by struct sas_phy defines an "outgoing" PHY on
  15. * a SAS HBA or Expander, and the SAS remote PHY represented by
  16. * struct sas_rphy defines an "incoming" PHY on a SAS Expander or
  17. * end device. Note that this is purely a software concept, the
  18. * underlying hardware for a PHY and a remote PHY is the exactly
  19. * the same.
  20. *
  21. * There is no concept of a SAS port in this code, users can see
  22. * what PHYs form a wide port based on the port_identifier attribute,
  23. * which is the same for all PHYs in a port.
  24. */
  25. #include <linux/init.h>
  26. #include <linux/module.h>
  27. #include <linux/jiffies.h>
  28. #include <linux/err.h>
  29. #include <linux/slab.h>
  30. #include <linux/string.h>
  31. #include <linux/blkdev.h>
  32. #include <linux/bsg.h>
  33. #include <scsi/scsi.h>
  34. #include <scsi/scsi_device.h>
  35. #include <scsi/scsi_host.h>
  36. #include <scsi/scsi_transport.h>
  37. #include <scsi/scsi_transport_sas.h>
  38. #include "scsi_sas_internal.h"
  39. struct sas_host_attrs {
  40. struct list_head rphy_list;
  41. struct mutex lock;
  42. struct request_queue *q;
  43. u32 next_target_id;
  44. u32 next_expander_id;
  45. int next_port_id;
  46. };
  47. #define to_sas_host_attrs(host) ((struct sas_host_attrs *)(host)->shost_data)
  48. /*
  49. * Hack to allow attributes of the same name in different objects.
  50. */
  51. #define SAS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
  52. struct device_attribute dev_attr_##_prefix##_##_name = \
  53. __ATTR(_name,_mode,_show,_store)
  54. /*
  55. * Pretty printing helpers
  56. */
  57. #define sas_bitfield_name_match(title, table) \
  58. static ssize_t \
  59. get_sas_##title##_names(u32 table_key, char *buf) \
  60. { \
  61. char *prefix = ""; \
  62. ssize_t len = 0; \
  63. int i; \
  64. \
  65. for (i = 0; i < ARRAY_SIZE(table); i++) { \
  66. if (table[i].value & table_key) { \
  67. len += sprintf(buf + len, "%s%s", \
  68. prefix, table[i].name); \
  69. prefix = ", "; \
  70. } \
  71. } \
  72. len += sprintf(buf + len, "\n"); \
  73. return len; \
  74. }
  75. #define sas_bitfield_name_set(title, table) \
  76. static ssize_t \
  77. set_sas_##title##_names(u32 *table_key, const char *buf) \
  78. { \
  79. ssize_t len = 0; \
  80. int i; \
  81. \
  82. for (i = 0; i < ARRAY_SIZE(table); i++) { \
  83. len = strlen(table[i].name); \
  84. if (strncmp(buf, table[i].name, len) == 0 && \
  85. (buf[len] == '\n' || buf[len] == '\0')) { \
  86. *table_key = table[i].value; \
  87. return 0; \
  88. } \
  89. } \
  90. return -EINVAL; \
  91. }
  92. #define sas_bitfield_name_search(title, table) \
  93. static ssize_t \
  94. get_sas_##title##_names(u32 table_key, char *buf) \
  95. { \
  96. ssize_t len = 0; \
  97. int i; \
  98. \
  99. for (i = 0; i < ARRAY_SIZE(table); i++) { \
  100. if (table[i].value == table_key) { \
  101. len += sprintf(buf + len, "%s", \
  102. table[i].name); \
  103. break; \
  104. } \
  105. } \
  106. len += sprintf(buf + len, "\n"); \
  107. return len; \
  108. }
  109. static struct {
  110. u32 value;
  111. char *name;
  112. } sas_device_type_names[] = {
  113. { SAS_PHY_UNUSED, "unused" },
  114. { SAS_END_DEVICE, "end device" },
  115. { SAS_EDGE_EXPANDER_DEVICE, "edge expander" },
  116. { SAS_FANOUT_EXPANDER_DEVICE, "fanout expander" },
  117. };
  118. sas_bitfield_name_search(device_type, sas_device_type_names)
  119. static struct {
  120. u32 value;
  121. char *name;
  122. } sas_protocol_names[] = {
  123. { SAS_PROTOCOL_SATA, "sata" },
  124. { SAS_PROTOCOL_SMP, "smp" },
  125. { SAS_PROTOCOL_STP, "stp" },
  126. { SAS_PROTOCOL_SSP, "ssp" },
  127. };
  128. sas_bitfield_name_match(protocol, sas_protocol_names)
  129. static struct {
  130. u32 value;
  131. char *name;
  132. } sas_linkspeed_names[] = {
  133. { SAS_LINK_RATE_UNKNOWN, "Unknown" },
  134. { SAS_PHY_DISABLED, "Phy disabled" },
  135. { SAS_LINK_RATE_FAILED, "Link Rate failed" },
  136. { SAS_SATA_SPINUP_HOLD, "Spin-up hold" },
  137. { SAS_LINK_RATE_1_5_GBPS, "1.5 Gbit" },
  138. { SAS_LINK_RATE_3_0_GBPS, "3.0 Gbit" },
  139. { SAS_LINK_RATE_6_0_GBPS, "6.0 Gbit" },
  140. };
  141. sas_bitfield_name_search(linkspeed, sas_linkspeed_names)
  142. sas_bitfield_name_set(linkspeed, sas_linkspeed_names)
  143. static struct sas_end_device *sas_sdev_to_rdev(struct scsi_device *sdev)
  144. {
  145. struct sas_rphy *rphy = target_to_rphy(sdev->sdev_target);
  146. struct sas_end_device *rdev;
  147. BUG_ON(rphy->identify.device_type != SAS_END_DEVICE);
  148. rdev = rphy_to_end_device(rphy);
  149. return rdev;
  150. }
  151. static void sas_smp_request(struct request_queue *q, struct Scsi_Host *shost,
  152. struct sas_rphy *rphy)
  153. {
  154. struct request *req;
  155. int ret;
  156. int (*handler)(struct Scsi_Host *, struct sas_rphy *, struct request *);
  157. while ((req = blk_fetch_request(q)) != NULL) {
  158. spin_unlock_irq(q->queue_lock);
  159. handler = to_sas_internal(shost->transportt)->f->smp_handler;
  160. ret = handler(shost, rphy, req);
  161. req->errors = ret;
  162. blk_end_request_all(req, ret);
  163. spin_lock_irq(q->queue_lock);
  164. }
  165. }
  166. static void sas_host_smp_request(struct request_queue *q)
  167. {
  168. sas_smp_request(q, (struct Scsi_Host *)q->queuedata, NULL);
  169. }
  170. static void sas_non_host_smp_request(struct request_queue *q)
  171. {
  172. struct sas_rphy *rphy = q->queuedata;
  173. sas_smp_request(q, rphy_to_shost(rphy), rphy);
  174. }
  175. static void sas_host_release(struct device *dev)
  176. {
  177. struct Scsi_Host *shost = dev_to_shost(dev);
  178. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  179. struct request_queue *q = sas_host->q;
  180. if (q)
  181. blk_cleanup_queue(q);
  182. }
  183. static int sas_bsg_initialize(struct Scsi_Host *shost, struct sas_rphy *rphy)
  184. {
  185. struct request_queue *q;
  186. int error;
  187. struct device *dev;
  188. char namebuf[20];
  189. const char *name;
  190. void (*release)(struct device *);
  191. if (!to_sas_internal(shost->transportt)->f->smp_handler) {
  192. printk("%s can't handle SMP requests\n", shost->hostt->name);
  193. return 0;
  194. }
  195. if (rphy) {
  196. q = blk_init_queue(sas_non_host_smp_request, NULL);
  197. dev = &rphy->dev;
  198. name = dev_name(dev);
  199. release = NULL;
  200. } else {
  201. q = blk_init_queue(sas_host_smp_request, NULL);
  202. dev = &shost->shost_gendev;
  203. snprintf(namebuf, sizeof(namebuf),
  204. "sas_host%d", shost->host_no);
  205. name = namebuf;
  206. release = sas_host_release;
  207. }
  208. if (!q)
  209. return -ENOMEM;
  210. error = bsg_register_queue(q, dev, name, release);
  211. if (error) {
  212. blk_cleanup_queue(q);
  213. return -ENOMEM;
  214. }
  215. if (rphy)
  216. rphy->q = q;
  217. else
  218. to_sas_host_attrs(shost)->q = q;
  219. if (rphy)
  220. q->queuedata = rphy;
  221. else
  222. q->queuedata = shost;
  223. queue_flag_set_unlocked(QUEUE_FLAG_BIDI, q);
  224. return 0;
  225. }
  226. static void sas_bsg_remove(struct Scsi_Host *shost, struct sas_rphy *rphy)
  227. {
  228. struct request_queue *q;
  229. if (rphy)
  230. q = rphy->q;
  231. else
  232. q = to_sas_host_attrs(shost)->q;
  233. if (!q)
  234. return;
  235. bsg_unregister_queue(q);
  236. }
  237. /*
  238. * SAS host attributes
  239. */
  240. static int sas_host_setup(struct transport_container *tc, struct device *dev,
  241. struct device *cdev)
  242. {
  243. struct Scsi_Host *shost = dev_to_shost(dev);
  244. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  245. INIT_LIST_HEAD(&sas_host->rphy_list);
  246. mutex_init(&sas_host->lock);
  247. sas_host->next_target_id = 0;
  248. sas_host->next_expander_id = 0;
  249. sas_host->next_port_id = 0;
  250. if (sas_bsg_initialize(shost, NULL))
  251. dev_printk(KERN_ERR, dev, "fail to a bsg device %d\n",
  252. shost->host_no);
  253. return 0;
  254. }
  255. static int sas_host_remove(struct transport_container *tc, struct device *dev,
  256. struct device *cdev)
  257. {
  258. struct Scsi_Host *shost = dev_to_shost(dev);
  259. sas_bsg_remove(shost, NULL);
  260. return 0;
  261. }
  262. static DECLARE_TRANSPORT_CLASS(sas_host_class,
  263. "sas_host", sas_host_setup, sas_host_remove, NULL);
  264. static int sas_host_match(struct attribute_container *cont,
  265. struct device *dev)
  266. {
  267. struct Scsi_Host *shost;
  268. struct sas_internal *i;
  269. if (!scsi_is_host_device(dev))
  270. return 0;
  271. shost = dev_to_shost(dev);
  272. if (!shost->transportt)
  273. return 0;
  274. if (shost->transportt->host_attrs.ac.class !=
  275. &sas_host_class.class)
  276. return 0;
  277. i = to_sas_internal(shost->transportt);
  278. return &i->t.host_attrs.ac == cont;
  279. }
  280. static int do_sas_phy_delete(struct device *dev, void *data)
  281. {
  282. int pass = (int)(unsigned long)data;
  283. if (pass == 0 && scsi_is_sas_port(dev))
  284. sas_port_delete(dev_to_sas_port(dev));
  285. else if (pass == 1 && scsi_is_sas_phy(dev))
  286. sas_phy_delete(dev_to_phy(dev));
  287. return 0;
  288. }
  289. /**
  290. * sas_remove_children - tear down a devices SAS data structures
  291. * @dev: device belonging to the sas object
  292. *
  293. * Removes all SAS PHYs and remote PHYs for a given object
  294. */
  295. void sas_remove_children(struct device *dev)
  296. {
  297. device_for_each_child(dev, (void *)0, do_sas_phy_delete);
  298. device_for_each_child(dev, (void *)1, do_sas_phy_delete);
  299. }
  300. EXPORT_SYMBOL(sas_remove_children);
  301. /**
  302. * sas_remove_host - tear down a Scsi_Host's SAS data structures
  303. * @shost: Scsi Host that is torn down
  304. *
  305. * Removes all SAS PHYs and remote PHYs for a given Scsi_Host.
  306. * Must be called just before scsi_remove_host for SAS HBAs.
  307. */
  308. void sas_remove_host(struct Scsi_Host *shost)
  309. {
  310. sas_remove_children(&shost->shost_gendev);
  311. }
  312. EXPORT_SYMBOL(sas_remove_host);
  313. /**
  314. * sas_tlr_supported - checking TLR bit in vpd 0x90
  315. * @sdev: scsi device struct
  316. *
  317. * Check Transport Layer Retries are supported or not.
  318. * If vpd page 0x90 is present, TRL is supported.
  319. *
  320. */
  321. unsigned int
  322. sas_tlr_supported(struct scsi_device *sdev)
  323. {
  324. const int vpd_len = 32;
  325. struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
  326. char *buffer = kzalloc(vpd_len, GFP_KERNEL);
  327. int ret = 0;
  328. if (scsi_get_vpd_page(sdev, 0x90, buffer, vpd_len))
  329. goto out;
  330. /*
  331. * Magic numbers: the VPD Protocol page (0x90)
  332. * has a 4 byte header and then one entry per device port
  333. * the TLR bit is at offset 8 on each port entry
  334. * if we take the first port, that's at total offset 12
  335. */
  336. ret = buffer[12] & 0x01;
  337. out:
  338. kfree(buffer);
  339. rdev->tlr_supported = ret;
  340. return ret;
  341. }
  342. EXPORT_SYMBOL_GPL(sas_tlr_supported);
  343. /**
  344. * sas_disable_tlr - setting TLR flags
  345. * @sdev: scsi device struct
  346. *
  347. * Seting tlr_enabled flag to 0.
  348. *
  349. */
  350. void
  351. sas_disable_tlr(struct scsi_device *sdev)
  352. {
  353. struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
  354. rdev->tlr_enabled = 0;
  355. }
  356. EXPORT_SYMBOL_GPL(sas_disable_tlr);
  357. /**
  358. * sas_enable_tlr - setting TLR flags
  359. * @sdev: scsi device struct
  360. *
  361. * Seting tlr_enabled flag 1.
  362. *
  363. */
  364. void sas_enable_tlr(struct scsi_device *sdev)
  365. {
  366. unsigned int tlr_supported = 0;
  367. tlr_supported = sas_tlr_supported(sdev);
  368. if (tlr_supported) {
  369. struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
  370. rdev->tlr_enabled = 1;
  371. }
  372. return;
  373. }
  374. EXPORT_SYMBOL_GPL(sas_enable_tlr);
  375. unsigned int sas_is_tlr_enabled(struct scsi_device *sdev)
  376. {
  377. struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
  378. return rdev->tlr_enabled;
  379. }
  380. EXPORT_SYMBOL_GPL(sas_is_tlr_enabled);
  381. /*
  382. * SAS Phy attributes
  383. */
  384. #define sas_phy_show_simple(field, name, format_string, cast) \
  385. static ssize_t \
  386. show_sas_phy_##name(struct device *dev, \
  387. struct device_attribute *attr, char *buf) \
  388. { \
  389. struct sas_phy *phy = transport_class_to_phy(dev); \
  390. \
  391. return snprintf(buf, 20, format_string, cast phy->field); \
  392. }
  393. #define sas_phy_simple_attr(field, name, format_string, type) \
  394. sas_phy_show_simple(field, name, format_string, (type)) \
  395. static DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
  396. #define sas_phy_show_protocol(field, name) \
  397. static ssize_t \
  398. show_sas_phy_##name(struct device *dev, \
  399. struct device_attribute *attr, char *buf) \
  400. { \
  401. struct sas_phy *phy = transport_class_to_phy(dev); \
  402. \
  403. if (!phy->field) \
  404. return snprintf(buf, 20, "none\n"); \
  405. return get_sas_protocol_names(phy->field, buf); \
  406. }
  407. #define sas_phy_protocol_attr(field, name) \
  408. sas_phy_show_protocol(field, name) \
  409. static DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
  410. #define sas_phy_show_linkspeed(field) \
  411. static ssize_t \
  412. show_sas_phy_##field(struct device *dev, \
  413. struct device_attribute *attr, char *buf) \
  414. { \
  415. struct sas_phy *phy = transport_class_to_phy(dev); \
  416. \
  417. return get_sas_linkspeed_names(phy->field, buf); \
  418. }
  419. /* Fudge to tell if we're minimum or maximum */
  420. #define sas_phy_store_linkspeed(field) \
  421. static ssize_t \
  422. store_sas_phy_##field(struct device *dev, \
  423. struct device_attribute *attr, \
  424. const char *buf, size_t count) \
  425. { \
  426. struct sas_phy *phy = transport_class_to_phy(dev); \
  427. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); \
  428. struct sas_internal *i = to_sas_internal(shost->transportt); \
  429. u32 value; \
  430. struct sas_phy_linkrates rates = {0}; \
  431. int error; \
  432. \
  433. error = set_sas_linkspeed_names(&value, buf); \
  434. if (error) \
  435. return error; \
  436. rates.field = value; \
  437. error = i->f->set_phy_speed(phy, &rates); \
  438. \
  439. return error ? error : count; \
  440. }
  441. #define sas_phy_linkspeed_rw_attr(field) \
  442. sas_phy_show_linkspeed(field) \
  443. sas_phy_store_linkspeed(field) \
  444. static DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, \
  445. store_sas_phy_##field)
  446. #define sas_phy_linkspeed_attr(field) \
  447. sas_phy_show_linkspeed(field) \
  448. static DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
  449. #define sas_phy_show_linkerror(field) \
  450. static ssize_t \
  451. show_sas_phy_##field(struct device *dev, \
  452. struct device_attribute *attr, char *buf) \
  453. { \
  454. struct sas_phy *phy = transport_class_to_phy(dev); \
  455. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); \
  456. struct sas_internal *i = to_sas_internal(shost->transportt); \
  457. int error; \
  458. \
  459. error = i->f->get_linkerrors ? i->f->get_linkerrors(phy) : 0; \
  460. if (error) \
  461. return error; \
  462. return snprintf(buf, 20, "%u\n", phy->field); \
  463. }
  464. #define sas_phy_linkerror_attr(field) \
  465. sas_phy_show_linkerror(field) \
  466. static DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
  467. static ssize_t
  468. show_sas_device_type(struct device *dev,
  469. struct device_attribute *attr, char *buf)
  470. {
  471. struct sas_phy *phy = transport_class_to_phy(dev);
  472. if (!phy->identify.device_type)
  473. return snprintf(buf, 20, "none\n");
  474. return get_sas_device_type_names(phy->identify.device_type, buf);
  475. }
  476. static DEVICE_ATTR(device_type, S_IRUGO, show_sas_device_type, NULL);
  477. static ssize_t do_sas_phy_enable(struct device *dev,
  478. size_t count, int enable)
  479. {
  480. struct sas_phy *phy = transport_class_to_phy(dev);
  481. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
  482. struct sas_internal *i = to_sas_internal(shost->transportt);
  483. int error;
  484. error = i->f->phy_enable(phy, enable);
  485. if (error)
  486. return error;
  487. phy->enabled = enable;
  488. return count;
  489. };
  490. static ssize_t
  491. store_sas_phy_enable(struct device *dev, struct device_attribute *attr,
  492. const char *buf, size_t count)
  493. {
  494. if (count < 1)
  495. return -EINVAL;
  496. switch (buf[0]) {
  497. case '0':
  498. do_sas_phy_enable(dev, count, 0);
  499. break;
  500. case '1':
  501. do_sas_phy_enable(dev, count, 1);
  502. break;
  503. default:
  504. return -EINVAL;
  505. }
  506. return count;
  507. }
  508. static ssize_t
  509. show_sas_phy_enable(struct device *dev, struct device_attribute *attr,
  510. char *buf)
  511. {
  512. struct sas_phy *phy = transport_class_to_phy(dev);
  513. return snprintf(buf, 20, "%d", phy->enabled);
  514. }
  515. static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, show_sas_phy_enable,
  516. store_sas_phy_enable);
  517. static ssize_t
  518. do_sas_phy_reset(struct device *dev, size_t count, int hard_reset)
  519. {
  520. struct sas_phy *phy = transport_class_to_phy(dev);
  521. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
  522. struct sas_internal *i = to_sas_internal(shost->transportt);
  523. int error;
  524. error = i->f->phy_reset(phy, hard_reset);
  525. if (error)
  526. return error;
  527. return count;
  528. };
  529. static ssize_t
  530. store_sas_link_reset(struct device *dev, struct device_attribute *attr,
  531. const char *buf, size_t count)
  532. {
  533. return do_sas_phy_reset(dev, count, 0);
  534. }
  535. static DEVICE_ATTR(link_reset, S_IWUSR, NULL, store_sas_link_reset);
  536. static ssize_t
  537. store_sas_hard_reset(struct device *dev, struct device_attribute *attr,
  538. const char *buf, size_t count)
  539. {
  540. return do_sas_phy_reset(dev, count, 1);
  541. }
  542. static DEVICE_ATTR(hard_reset, S_IWUSR, NULL, store_sas_hard_reset);
  543. sas_phy_protocol_attr(identify.initiator_port_protocols,
  544. initiator_port_protocols);
  545. sas_phy_protocol_attr(identify.target_port_protocols,
  546. target_port_protocols);
  547. sas_phy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
  548. unsigned long long);
  549. sas_phy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
  550. //sas_phy_simple_attr(port_identifier, port_identifier, "%d\n", int);
  551. sas_phy_linkspeed_attr(negotiated_linkrate);
  552. sas_phy_linkspeed_attr(minimum_linkrate_hw);
  553. sas_phy_linkspeed_rw_attr(minimum_linkrate);
  554. sas_phy_linkspeed_attr(maximum_linkrate_hw);
  555. sas_phy_linkspeed_rw_attr(maximum_linkrate);
  556. sas_phy_linkerror_attr(invalid_dword_count);
  557. sas_phy_linkerror_attr(running_disparity_error_count);
  558. sas_phy_linkerror_attr(loss_of_dword_sync_count);
  559. sas_phy_linkerror_attr(phy_reset_problem_count);
  560. static DECLARE_TRANSPORT_CLASS(sas_phy_class,
  561. "sas_phy", NULL, NULL, NULL);
  562. static int sas_phy_match(struct attribute_container *cont, struct device *dev)
  563. {
  564. struct Scsi_Host *shost;
  565. struct sas_internal *i;
  566. if (!scsi_is_sas_phy(dev))
  567. return 0;
  568. shost = dev_to_shost(dev->parent);
  569. if (!shost->transportt)
  570. return 0;
  571. if (shost->transportt->host_attrs.ac.class !=
  572. &sas_host_class.class)
  573. return 0;
  574. i = to_sas_internal(shost->transportt);
  575. return &i->phy_attr_cont.ac == cont;
  576. }
  577. static void sas_phy_release(struct device *dev)
  578. {
  579. struct sas_phy *phy = dev_to_phy(dev);
  580. put_device(dev->parent);
  581. kfree(phy);
  582. }
  583. /**
  584. * sas_phy_alloc - allocates and initialize a SAS PHY structure
  585. * @parent: Parent device
  586. * @number: Phy index
  587. *
  588. * Allocates an SAS PHY structure. It will be added in the device tree
  589. * below the device specified by @parent, which has to be either a Scsi_Host
  590. * or sas_rphy.
  591. *
  592. * Returns:
  593. * SAS PHY allocated or %NULL if the allocation failed.
  594. */
  595. struct sas_phy *sas_phy_alloc(struct device *parent, int number)
  596. {
  597. struct Scsi_Host *shost = dev_to_shost(parent);
  598. struct sas_phy *phy;
  599. phy = kzalloc(sizeof(*phy), GFP_KERNEL);
  600. if (!phy)
  601. return NULL;
  602. phy->number = number;
  603. phy->enabled = 1;
  604. device_initialize(&phy->dev);
  605. phy->dev.parent = get_device(parent);
  606. phy->dev.release = sas_phy_release;
  607. INIT_LIST_HEAD(&phy->port_siblings);
  608. if (scsi_is_sas_expander_device(parent)) {
  609. struct sas_rphy *rphy = dev_to_rphy(parent);
  610. dev_set_name(&phy->dev, "phy-%d:%d:%d", shost->host_no,
  611. rphy->scsi_target_id, number);
  612. } else
  613. dev_set_name(&phy->dev, "phy-%d:%d", shost->host_no, number);
  614. transport_setup_device(&phy->dev);
  615. return phy;
  616. }
  617. EXPORT_SYMBOL(sas_phy_alloc);
  618. /**
  619. * sas_phy_add - add a SAS PHY to the device hierarchy
  620. * @phy: The PHY to be added
  621. *
  622. * Publishes a SAS PHY to the rest of the system.
  623. */
  624. int sas_phy_add(struct sas_phy *phy)
  625. {
  626. int error;
  627. error = device_add(&phy->dev);
  628. if (!error) {
  629. transport_add_device(&phy->dev);
  630. transport_configure_device(&phy->dev);
  631. }
  632. return error;
  633. }
  634. EXPORT_SYMBOL(sas_phy_add);
  635. /**
  636. * sas_phy_free - free a SAS PHY
  637. * @phy: SAS PHY to free
  638. *
  639. * Frees the specified SAS PHY.
  640. *
  641. * Note:
  642. * This function must only be called on a PHY that has not
  643. * successfully been added using sas_phy_add().
  644. */
  645. void sas_phy_free(struct sas_phy *phy)
  646. {
  647. transport_destroy_device(&phy->dev);
  648. put_device(&phy->dev);
  649. }
  650. EXPORT_SYMBOL(sas_phy_free);
  651. /**
  652. * sas_phy_delete - remove SAS PHY
  653. * @phy: SAS PHY to remove
  654. *
  655. * Removes the specified SAS PHY. If the SAS PHY has an
  656. * associated remote PHY it is removed before.
  657. */
  658. void
  659. sas_phy_delete(struct sas_phy *phy)
  660. {
  661. struct device *dev = &phy->dev;
  662. /* this happens if the phy is still part of a port when deleted */
  663. BUG_ON(!list_empty(&phy->port_siblings));
  664. transport_remove_device(dev);
  665. device_del(dev);
  666. transport_destroy_device(dev);
  667. put_device(dev);
  668. }
  669. EXPORT_SYMBOL(sas_phy_delete);
  670. /**
  671. * scsi_is_sas_phy - check if a struct device represents a SAS PHY
  672. * @dev: device to check
  673. *
  674. * Returns:
  675. * %1 if the device represents a SAS PHY, %0 else
  676. */
  677. int scsi_is_sas_phy(const struct device *dev)
  678. {
  679. return dev->release == sas_phy_release;
  680. }
  681. EXPORT_SYMBOL(scsi_is_sas_phy);
  682. /*
  683. * SAS Port attributes
  684. */
  685. #define sas_port_show_simple(field, name, format_string, cast) \
  686. static ssize_t \
  687. show_sas_port_##name(struct device *dev, \
  688. struct device_attribute *attr, char *buf) \
  689. { \
  690. struct sas_port *port = transport_class_to_sas_port(dev); \
  691. \
  692. return snprintf(buf, 20, format_string, cast port->field); \
  693. }
  694. #define sas_port_simple_attr(field, name, format_string, type) \
  695. sas_port_show_simple(field, name, format_string, (type)) \
  696. static DEVICE_ATTR(name, S_IRUGO, show_sas_port_##name, NULL)
  697. sas_port_simple_attr(num_phys, num_phys, "%d\n", int);
  698. static DECLARE_TRANSPORT_CLASS(sas_port_class,
  699. "sas_port", NULL, NULL, NULL);
  700. static int sas_port_match(struct attribute_container *cont, struct device *dev)
  701. {
  702. struct Scsi_Host *shost;
  703. struct sas_internal *i;
  704. if (!scsi_is_sas_port(dev))
  705. return 0;
  706. shost = dev_to_shost(dev->parent);
  707. if (!shost->transportt)
  708. return 0;
  709. if (shost->transportt->host_attrs.ac.class !=
  710. &sas_host_class.class)
  711. return 0;
  712. i = to_sas_internal(shost->transportt);
  713. return &i->port_attr_cont.ac == cont;
  714. }
  715. static void sas_port_release(struct device *dev)
  716. {
  717. struct sas_port *port = dev_to_sas_port(dev);
  718. BUG_ON(!list_empty(&port->phy_list));
  719. put_device(dev->parent);
  720. kfree(port);
  721. }
  722. static void sas_port_create_link(struct sas_port *port,
  723. struct sas_phy *phy)
  724. {
  725. int res;
  726. res = sysfs_create_link(&port->dev.kobj, &phy->dev.kobj,
  727. dev_name(&phy->dev));
  728. if (res)
  729. goto err;
  730. res = sysfs_create_link(&phy->dev.kobj, &port->dev.kobj, "port");
  731. if (res)
  732. goto err;
  733. return;
  734. err:
  735. printk(KERN_ERR "%s: Cannot create port links, err=%d\n",
  736. __func__, res);
  737. }
  738. static void sas_port_delete_link(struct sas_port *port,
  739. struct sas_phy *phy)
  740. {
  741. sysfs_remove_link(&port->dev.kobj, dev_name(&phy->dev));
  742. sysfs_remove_link(&phy->dev.kobj, "port");
  743. }
  744. /** sas_port_alloc - allocate and initialize a SAS port structure
  745. *
  746. * @parent: parent device
  747. * @port_id: port number
  748. *
  749. * Allocates a SAS port structure. It will be added to the device tree
  750. * below the device specified by @parent which must be either a Scsi_Host
  751. * or a sas_expander_device.
  752. *
  753. * Returns %NULL on error
  754. */
  755. struct sas_port *sas_port_alloc(struct device *parent, int port_id)
  756. {
  757. struct Scsi_Host *shost = dev_to_shost(parent);
  758. struct sas_port *port;
  759. port = kzalloc(sizeof(*port), GFP_KERNEL);
  760. if (!port)
  761. return NULL;
  762. port->port_identifier = port_id;
  763. device_initialize(&port->dev);
  764. port->dev.parent = get_device(parent);
  765. port->dev.release = sas_port_release;
  766. mutex_init(&port->phy_list_mutex);
  767. INIT_LIST_HEAD(&port->phy_list);
  768. if (scsi_is_sas_expander_device(parent)) {
  769. struct sas_rphy *rphy = dev_to_rphy(parent);
  770. dev_set_name(&port->dev, "port-%d:%d:%d", shost->host_no,
  771. rphy->scsi_target_id, port->port_identifier);
  772. } else
  773. dev_set_name(&port->dev, "port-%d:%d", shost->host_no,
  774. port->port_identifier);
  775. transport_setup_device(&port->dev);
  776. return port;
  777. }
  778. EXPORT_SYMBOL(sas_port_alloc);
  779. /** sas_port_alloc_num - allocate and initialize a SAS port structure
  780. *
  781. * @parent: parent device
  782. *
  783. * Allocates a SAS port structure and a number to go with it. This
  784. * interface is really for adapters where the port number has no
  785. * meansing, so the sas class should manage them. It will be added to
  786. * the device tree below the device specified by @parent which must be
  787. * either a Scsi_Host or a sas_expander_device.
  788. *
  789. * Returns %NULL on error
  790. */
  791. struct sas_port *sas_port_alloc_num(struct device *parent)
  792. {
  793. int index;
  794. struct Scsi_Host *shost = dev_to_shost(parent);
  795. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  796. /* FIXME: use idr for this eventually */
  797. mutex_lock(&sas_host->lock);
  798. if (scsi_is_sas_expander_device(parent)) {
  799. struct sas_rphy *rphy = dev_to_rphy(parent);
  800. struct sas_expander_device *exp = rphy_to_expander_device(rphy);
  801. index = exp->next_port_id++;
  802. } else
  803. index = sas_host->next_port_id++;
  804. mutex_unlock(&sas_host->lock);
  805. return sas_port_alloc(parent, index);
  806. }
  807. EXPORT_SYMBOL(sas_port_alloc_num);
  808. /**
  809. * sas_port_add - add a SAS port to the device hierarchy
  810. * @port: port to be added
  811. *
  812. * publishes a port to the rest of the system
  813. */
  814. int sas_port_add(struct sas_port *port)
  815. {
  816. int error;
  817. /* No phys should be added until this is made visible */
  818. BUG_ON(!list_empty(&port->phy_list));
  819. error = device_add(&port->dev);
  820. if (error)
  821. return error;
  822. transport_add_device(&port->dev);
  823. transport_configure_device(&port->dev);
  824. return 0;
  825. }
  826. EXPORT_SYMBOL(sas_port_add);
  827. /**
  828. * sas_port_free - free a SAS PORT
  829. * @port: SAS PORT to free
  830. *
  831. * Frees the specified SAS PORT.
  832. *
  833. * Note:
  834. * This function must only be called on a PORT that has not
  835. * successfully been added using sas_port_add().
  836. */
  837. void sas_port_free(struct sas_port *port)
  838. {
  839. transport_destroy_device(&port->dev);
  840. put_device(&port->dev);
  841. }
  842. EXPORT_SYMBOL(sas_port_free);
  843. /**
  844. * sas_port_delete - remove SAS PORT
  845. * @port: SAS PORT to remove
  846. *
  847. * Removes the specified SAS PORT. If the SAS PORT has an
  848. * associated phys, unlink them from the port as well.
  849. */
  850. void sas_port_delete(struct sas_port *port)
  851. {
  852. struct device *dev = &port->dev;
  853. struct sas_phy *phy, *tmp_phy;
  854. if (port->rphy) {
  855. sas_rphy_delete(port->rphy);
  856. port->rphy = NULL;
  857. }
  858. mutex_lock(&port->phy_list_mutex);
  859. list_for_each_entry_safe(phy, tmp_phy, &port->phy_list,
  860. port_siblings) {
  861. sas_port_delete_link(port, phy);
  862. list_del_init(&phy->port_siblings);
  863. }
  864. mutex_unlock(&port->phy_list_mutex);
  865. if (port->is_backlink) {
  866. struct device *parent = port->dev.parent;
  867. sysfs_remove_link(&port->dev.kobj, dev_name(parent));
  868. port->is_backlink = 0;
  869. }
  870. transport_remove_device(dev);
  871. device_del(dev);
  872. transport_destroy_device(dev);
  873. put_device(dev);
  874. }
  875. EXPORT_SYMBOL(sas_port_delete);
  876. /**
  877. * scsi_is_sas_port - check if a struct device represents a SAS port
  878. * @dev: device to check
  879. *
  880. * Returns:
  881. * %1 if the device represents a SAS Port, %0 else
  882. */
  883. int scsi_is_sas_port(const struct device *dev)
  884. {
  885. return dev->release == sas_port_release;
  886. }
  887. EXPORT_SYMBOL(scsi_is_sas_port);
  888. /**
  889. * sas_port_add_phy - add another phy to a port to form a wide port
  890. * @port: port to add the phy to
  891. * @phy: phy to add
  892. *
  893. * When a port is initially created, it is empty (has no phys). All
  894. * ports must have at least one phy to operated, and all wide ports
  895. * must have at least two. The current code makes no difference
  896. * between ports and wide ports, but the only object that can be
  897. * connected to a remote device is a port, so ports must be formed on
  898. * all devices with phys if they're connected to anything.
  899. */
  900. void sas_port_add_phy(struct sas_port *port, struct sas_phy *phy)
  901. {
  902. mutex_lock(&port->phy_list_mutex);
  903. if (unlikely(!list_empty(&phy->port_siblings))) {
  904. /* make sure we're already on this port */
  905. struct sas_phy *tmp;
  906. list_for_each_entry(tmp, &port->phy_list, port_siblings)
  907. if (tmp == phy)
  908. break;
  909. /* If this trips, you added a phy that was already
  910. * part of a different port */
  911. if (unlikely(tmp != phy)) {
  912. dev_printk(KERN_ERR, &port->dev, "trying to add phy %s fails: it's already part of another port\n",
  913. dev_name(&phy->dev));
  914. BUG();
  915. }
  916. } else {
  917. sas_port_create_link(port, phy);
  918. list_add_tail(&phy->port_siblings, &port->phy_list);
  919. port->num_phys++;
  920. }
  921. mutex_unlock(&port->phy_list_mutex);
  922. }
  923. EXPORT_SYMBOL(sas_port_add_phy);
  924. /**
  925. * sas_port_delete_phy - remove a phy from a port or wide port
  926. * @port: port to remove the phy from
  927. * @phy: phy to remove
  928. *
  929. * This operation is used for tearing down ports again. It must be
  930. * done to every port or wide port before calling sas_port_delete.
  931. */
  932. void sas_port_delete_phy(struct sas_port *port, struct sas_phy *phy)
  933. {
  934. mutex_lock(&port->phy_list_mutex);
  935. sas_port_delete_link(port, phy);
  936. list_del_init(&phy->port_siblings);
  937. port->num_phys--;
  938. mutex_unlock(&port->phy_list_mutex);
  939. }
  940. EXPORT_SYMBOL(sas_port_delete_phy);
  941. void sas_port_mark_backlink(struct sas_port *port)
  942. {
  943. int res;
  944. struct device *parent = port->dev.parent->parent->parent;
  945. if (port->is_backlink)
  946. return;
  947. port->is_backlink = 1;
  948. res = sysfs_create_link(&port->dev.kobj, &parent->kobj,
  949. dev_name(parent));
  950. if (res)
  951. goto err;
  952. return;
  953. err:
  954. printk(KERN_ERR "%s: Cannot create port backlink, err=%d\n",
  955. __func__, res);
  956. }
  957. EXPORT_SYMBOL(sas_port_mark_backlink);
  958. /*
  959. * SAS remote PHY attributes.
  960. */
  961. #define sas_rphy_show_simple(field, name, format_string, cast) \
  962. static ssize_t \
  963. show_sas_rphy_##name(struct device *dev, \
  964. struct device_attribute *attr, char *buf) \
  965. { \
  966. struct sas_rphy *rphy = transport_class_to_rphy(dev); \
  967. \
  968. return snprintf(buf, 20, format_string, cast rphy->field); \
  969. }
  970. #define sas_rphy_simple_attr(field, name, format_string, type) \
  971. sas_rphy_show_simple(field, name, format_string, (type)) \
  972. static SAS_DEVICE_ATTR(rphy, name, S_IRUGO, \
  973. show_sas_rphy_##name, NULL)
  974. #define sas_rphy_show_protocol(field, name) \
  975. static ssize_t \
  976. show_sas_rphy_##name(struct device *dev, \
  977. struct device_attribute *attr, char *buf) \
  978. { \
  979. struct sas_rphy *rphy = transport_class_to_rphy(dev); \
  980. \
  981. if (!rphy->field) \
  982. return snprintf(buf, 20, "none\n"); \
  983. return get_sas_protocol_names(rphy->field, buf); \
  984. }
  985. #define sas_rphy_protocol_attr(field, name) \
  986. sas_rphy_show_protocol(field, name) \
  987. static SAS_DEVICE_ATTR(rphy, name, S_IRUGO, \
  988. show_sas_rphy_##name, NULL)
  989. static ssize_t
  990. show_sas_rphy_device_type(struct device *dev,
  991. struct device_attribute *attr, char *buf)
  992. {
  993. struct sas_rphy *rphy = transport_class_to_rphy(dev);
  994. if (!rphy->identify.device_type)
  995. return snprintf(buf, 20, "none\n");
  996. return get_sas_device_type_names(
  997. rphy->identify.device_type, buf);
  998. }
  999. static SAS_DEVICE_ATTR(rphy, device_type, S_IRUGO,
  1000. show_sas_rphy_device_type, NULL);
  1001. static ssize_t
  1002. show_sas_rphy_enclosure_identifier(struct device *dev,
  1003. struct device_attribute *attr, char *buf)
  1004. {
  1005. struct sas_rphy *rphy = transport_class_to_rphy(dev);
  1006. struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
  1007. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
  1008. struct sas_internal *i = to_sas_internal(shost->transportt);
  1009. u64 identifier;
  1010. int error;
  1011. /*
  1012. * Only devices behind an expander are supported, because the
  1013. * enclosure identifier is a SMP feature.
  1014. */
  1015. if (scsi_is_sas_phy_local(phy))
  1016. return -EINVAL;
  1017. error = i->f->get_enclosure_identifier(rphy, &identifier);
  1018. if (error)
  1019. return error;
  1020. return sprintf(buf, "0x%llx\n", (unsigned long long)identifier);
  1021. }
  1022. static SAS_DEVICE_ATTR(rphy, enclosure_identifier, S_IRUGO,
  1023. show_sas_rphy_enclosure_identifier, NULL);
  1024. static ssize_t
  1025. show_sas_rphy_bay_identifier(struct device *dev,
  1026. struct device_attribute *attr, char *buf)
  1027. {
  1028. struct sas_rphy *rphy = transport_class_to_rphy(dev);
  1029. struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
  1030. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
  1031. struct sas_internal *i = to_sas_internal(shost->transportt);
  1032. int val;
  1033. if (scsi_is_sas_phy_local(phy))
  1034. return -EINVAL;
  1035. val = i->f->get_bay_identifier(rphy);
  1036. if (val < 0)
  1037. return val;
  1038. return sprintf(buf, "%d\n", val);
  1039. }
  1040. static SAS_DEVICE_ATTR(rphy, bay_identifier, S_IRUGO,
  1041. show_sas_rphy_bay_identifier, NULL);
  1042. sas_rphy_protocol_attr(identify.initiator_port_protocols,
  1043. initiator_port_protocols);
  1044. sas_rphy_protocol_attr(identify.target_port_protocols, target_port_protocols);
  1045. sas_rphy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
  1046. unsigned long long);
  1047. sas_rphy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
  1048. /* only need 8 bytes of data plus header (4 or 8) */
  1049. #define BUF_SIZE 64
  1050. int sas_read_port_mode_page(struct scsi_device *sdev)
  1051. {
  1052. char *buffer = kzalloc(BUF_SIZE, GFP_KERNEL), *msdata;
  1053. struct sas_end_device *rdev = sas_sdev_to_rdev(sdev);
  1054. struct scsi_mode_data mode_data;
  1055. int res, error;
  1056. if (!buffer)
  1057. return -ENOMEM;
  1058. res = scsi_mode_sense(sdev, 1, 0x19, buffer, BUF_SIZE, 30*HZ, 3,
  1059. &mode_data, NULL);
  1060. error = -EINVAL;
  1061. if (!scsi_status_is_good(res))
  1062. goto out;
  1063. msdata = buffer + mode_data.header_length +
  1064. mode_data.block_descriptor_length;
  1065. if (msdata - buffer > BUF_SIZE - 8)
  1066. goto out;
  1067. error = 0;
  1068. rdev->ready_led_meaning = msdata[2] & 0x10 ? 1 : 0;
  1069. rdev->I_T_nexus_loss_timeout = (msdata[4] << 8) + msdata[5];
  1070. rdev->initiator_response_timeout = (msdata[6] << 8) + msdata[7];
  1071. out:
  1072. kfree(buffer);
  1073. return error;
  1074. }
  1075. EXPORT_SYMBOL(sas_read_port_mode_page);
  1076. static DECLARE_TRANSPORT_CLASS(sas_end_dev_class,
  1077. "sas_end_device", NULL, NULL, NULL);
  1078. #define sas_end_dev_show_simple(field, name, format_string, cast) \
  1079. static ssize_t \
  1080. show_sas_end_dev_##name(struct device *dev, \
  1081. struct device_attribute *attr, char *buf) \
  1082. { \
  1083. struct sas_rphy *rphy = transport_class_to_rphy(dev); \
  1084. struct sas_end_device *rdev = rphy_to_end_device(rphy); \
  1085. \
  1086. return snprintf(buf, 20, format_string, cast rdev->field); \
  1087. }
  1088. #define sas_end_dev_simple_attr(field, name, format_string, type) \
  1089. sas_end_dev_show_simple(field, name, format_string, (type)) \
  1090. static SAS_DEVICE_ATTR(end_dev, name, S_IRUGO, \
  1091. show_sas_end_dev_##name, NULL)
  1092. sas_end_dev_simple_attr(ready_led_meaning, ready_led_meaning, "%d\n", int);
  1093. sas_end_dev_simple_attr(I_T_nexus_loss_timeout, I_T_nexus_loss_timeout,
  1094. "%d\n", int);
  1095. sas_end_dev_simple_attr(initiator_response_timeout, initiator_response_timeout,
  1096. "%d\n", int);
  1097. sas_end_dev_simple_attr(tlr_supported, tlr_supported,
  1098. "%d\n", int);
  1099. sas_end_dev_simple_attr(tlr_enabled, tlr_enabled,
  1100. "%d\n", int);
  1101. static DECLARE_TRANSPORT_CLASS(sas_expander_class,
  1102. "sas_expander", NULL, NULL, NULL);
  1103. #define sas_expander_show_simple(field, name, format_string, cast) \
  1104. static ssize_t \
  1105. show_sas_expander_##name(struct device *dev, \
  1106. struct device_attribute *attr, char *buf) \
  1107. { \
  1108. struct sas_rphy *rphy = transport_class_to_rphy(dev); \
  1109. struct sas_expander_device *edev = rphy_to_expander_device(rphy); \
  1110. \
  1111. return snprintf(buf, 20, format_string, cast edev->field); \
  1112. }
  1113. #define sas_expander_simple_attr(field, name, format_string, type) \
  1114. sas_expander_show_simple(field, name, format_string, (type)) \
  1115. static SAS_DEVICE_ATTR(expander, name, S_IRUGO, \
  1116. show_sas_expander_##name, NULL)
  1117. sas_expander_simple_attr(vendor_id, vendor_id, "%s\n", char *);
  1118. sas_expander_simple_attr(product_id, product_id, "%s\n", char *);
  1119. sas_expander_simple_attr(product_rev, product_rev, "%s\n", char *);
  1120. sas_expander_simple_attr(component_vendor_id, component_vendor_id,
  1121. "%s\n", char *);
  1122. sas_expander_simple_attr(component_id, component_id, "%u\n", unsigned int);
  1123. sas_expander_simple_attr(component_revision_id, component_revision_id, "%u\n",
  1124. unsigned int);
  1125. sas_expander_simple_attr(level, level, "%d\n", int);
  1126. static DECLARE_TRANSPORT_CLASS(sas_rphy_class,
  1127. "sas_device", NULL, NULL, NULL);
  1128. static int sas_rphy_match(struct attribute_container *cont, struct device *dev)
  1129. {
  1130. struct Scsi_Host *shost;
  1131. struct sas_internal *i;
  1132. if (!scsi_is_sas_rphy(dev))
  1133. return 0;
  1134. shost = dev_to_shost(dev->parent->parent);
  1135. if (!shost->transportt)
  1136. return 0;
  1137. if (shost->transportt->host_attrs.ac.class !=
  1138. &sas_host_class.class)
  1139. return 0;
  1140. i = to_sas_internal(shost->transportt);
  1141. return &i->rphy_attr_cont.ac == cont;
  1142. }
  1143. static int sas_end_dev_match(struct attribute_container *cont,
  1144. struct device *dev)
  1145. {
  1146. struct Scsi_Host *shost;
  1147. struct sas_internal *i;
  1148. struct sas_rphy *rphy;
  1149. if (!scsi_is_sas_rphy(dev))
  1150. return 0;
  1151. shost = dev_to_shost(dev->parent->parent);
  1152. rphy = dev_to_rphy(dev);
  1153. if (!shost->transportt)
  1154. return 0;
  1155. if (shost->transportt->host_attrs.ac.class !=
  1156. &sas_host_class.class)
  1157. return 0;
  1158. i = to_sas_internal(shost->transportt);
  1159. return &i->end_dev_attr_cont.ac == cont &&
  1160. rphy->identify.device_type == SAS_END_DEVICE;
  1161. }
  1162. static int sas_expander_match(struct attribute_container *cont,
  1163. struct device *dev)
  1164. {
  1165. struct Scsi_Host *shost;
  1166. struct sas_internal *i;
  1167. struct sas_rphy *rphy;
  1168. if (!scsi_is_sas_rphy(dev))
  1169. return 0;
  1170. shost = dev_to_shost(dev->parent->parent);
  1171. rphy = dev_to_rphy(dev);
  1172. if (!shost->transportt)
  1173. return 0;
  1174. if (shost->transportt->host_attrs.ac.class !=
  1175. &sas_host_class.class)
  1176. return 0;
  1177. i = to_sas_internal(shost->transportt);
  1178. return &i->expander_attr_cont.ac == cont &&
  1179. (rphy->identify.device_type == SAS_EDGE_EXPANDER_DEVICE ||
  1180. rphy->identify.device_type == SAS_FANOUT_EXPANDER_DEVICE);
  1181. }
  1182. static void sas_expander_release(struct device *dev)
  1183. {
  1184. struct sas_rphy *rphy = dev_to_rphy(dev);
  1185. struct sas_expander_device *edev = rphy_to_expander_device(rphy);
  1186. if (rphy->q)
  1187. blk_cleanup_queue(rphy->q);
  1188. put_device(dev->parent);
  1189. kfree(edev);
  1190. }
  1191. static void sas_end_device_release(struct device *dev)
  1192. {
  1193. struct sas_rphy *rphy = dev_to_rphy(dev);
  1194. struct sas_end_device *edev = rphy_to_end_device(rphy);
  1195. if (rphy->q)
  1196. blk_cleanup_queue(rphy->q);
  1197. put_device(dev->parent);
  1198. kfree(edev);
  1199. }
  1200. /**
  1201. * sas_rphy_initialize - common rphy intialization
  1202. * @rphy: rphy to initialise
  1203. *
  1204. * Used by both sas_end_device_alloc() and sas_expander_alloc() to
  1205. * initialise the common rphy component of each.
  1206. */
  1207. static void sas_rphy_initialize(struct sas_rphy *rphy)
  1208. {
  1209. INIT_LIST_HEAD(&rphy->list);
  1210. }
  1211. /**
  1212. * sas_end_device_alloc - allocate an rphy for an end device
  1213. * @parent: which port
  1214. *
  1215. * Allocates an SAS remote PHY structure, connected to @parent.
  1216. *
  1217. * Returns:
  1218. * SAS PHY allocated or %NULL if the allocation failed.
  1219. */
  1220. struct sas_rphy *sas_end_device_alloc(struct sas_port *parent)
  1221. {
  1222. struct Scsi_Host *shost = dev_to_shost(&parent->dev);
  1223. struct sas_end_device *rdev;
  1224. rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
  1225. if (!rdev) {
  1226. return NULL;
  1227. }
  1228. device_initialize(&rdev->rphy.dev);
  1229. rdev->rphy.dev.parent = get_device(&parent->dev);
  1230. rdev->rphy.dev.release = sas_end_device_release;
  1231. if (scsi_is_sas_expander_device(parent->dev.parent)) {
  1232. struct sas_rphy *rphy = dev_to_rphy(parent->dev.parent);
  1233. dev_set_name(&rdev->rphy.dev, "end_device-%d:%d:%d",
  1234. shost->host_no, rphy->scsi_target_id,
  1235. parent->port_identifier);
  1236. } else
  1237. dev_set_name(&rdev->rphy.dev, "end_device-%d:%d",
  1238. shost->host_no, parent->port_identifier);
  1239. rdev->rphy.identify.device_type = SAS_END_DEVICE;
  1240. sas_rphy_initialize(&rdev->rphy);
  1241. transport_setup_device(&rdev->rphy.dev);
  1242. return &rdev->rphy;
  1243. }
  1244. EXPORT_SYMBOL(sas_end_device_alloc);
  1245. /**
  1246. * sas_expander_alloc - allocate an rphy for an end device
  1247. * @parent: which port
  1248. * @type: SAS_EDGE_EXPANDER_DEVICE or SAS_FANOUT_EXPANDER_DEVICE
  1249. *
  1250. * Allocates an SAS remote PHY structure, connected to @parent.
  1251. *
  1252. * Returns:
  1253. * SAS PHY allocated or %NULL if the allocation failed.
  1254. */
  1255. struct sas_rphy *sas_expander_alloc(struct sas_port *parent,
  1256. enum sas_device_type type)
  1257. {
  1258. struct Scsi_Host *shost = dev_to_shost(&parent->dev);
  1259. struct sas_expander_device *rdev;
  1260. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  1261. BUG_ON(type != SAS_EDGE_EXPANDER_DEVICE &&
  1262. type != SAS_FANOUT_EXPANDER_DEVICE);
  1263. rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
  1264. if (!rdev) {
  1265. return NULL;
  1266. }
  1267. device_initialize(&rdev->rphy.dev);
  1268. rdev->rphy.dev.parent = get_device(&parent->dev);
  1269. rdev->rphy.dev.release = sas_expander_release;
  1270. mutex_lock(&sas_host->lock);
  1271. rdev->rphy.scsi_target_id = sas_host->next_expander_id++;
  1272. mutex_unlock(&sas_host->lock);
  1273. dev_set_name(&rdev->rphy.dev, "expander-%d:%d",
  1274. shost->host_no, rdev->rphy.scsi_target_id);
  1275. rdev->rphy.identify.device_type = type;
  1276. sas_rphy_initialize(&rdev->rphy);
  1277. transport_setup_device(&rdev->rphy.dev);
  1278. return &rdev->rphy;
  1279. }
  1280. EXPORT_SYMBOL(sas_expander_alloc);
  1281. /**
  1282. * sas_rphy_add - add a SAS remote PHY to the device hierarchy
  1283. * @rphy: The remote PHY to be added
  1284. *
  1285. * Publishes a SAS remote PHY to the rest of the system.
  1286. */
  1287. int sas_rphy_add(struct sas_rphy *rphy)
  1288. {
  1289. struct sas_port *parent = dev_to_sas_port(rphy->dev.parent);
  1290. struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
  1291. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  1292. struct sas_identify *identify = &rphy->identify;
  1293. int error;
  1294. if (parent->rphy)
  1295. return -ENXIO;
  1296. parent->rphy = rphy;
  1297. error = device_add(&rphy->dev);
  1298. if (error)
  1299. return error;
  1300. transport_add_device(&rphy->dev);
  1301. transport_configure_device(&rphy->dev);
  1302. if (sas_bsg_initialize(shost, rphy))
  1303. printk("fail to a bsg device %s\n", dev_name(&rphy->dev));
  1304. mutex_lock(&sas_host->lock);
  1305. list_add_tail(&rphy->list, &sas_host->rphy_list);
  1306. if (identify->device_type == SAS_END_DEVICE &&
  1307. (identify->target_port_protocols &
  1308. (SAS_PROTOCOL_SSP|SAS_PROTOCOL_STP|SAS_PROTOCOL_SATA)))
  1309. rphy->scsi_target_id = sas_host->next_target_id++;
  1310. else if (identify->device_type == SAS_END_DEVICE)
  1311. rphy->scsi_target_id = -1;
  1312. mutex_unlock(&sas_host->lock);
  1313. if (identify->device_type == SAS_END_DEVICE &&
  1314. rphy->scsi_target_id != -1) {
  1315. scsi_scan_target(&rphy->dev, 0,
  1316. rphy->scsi_target_id, SCAN_WILD_CARD, 0);
  1317. }
  1318. return 0;
  1319. }
  1320. EXPORT_SYMBOL(sas_rphy_add);
  1321. /**
  1322. * sas_rphy_free - free a SAS remote PHY
  1323. * @rphy: SAS remote PHY to free
  1324. *
  1325. * Frees the specified SAS remote PHY.
  1326. *
  1327. * Note:
  1328. * This function must only be called on a remote
  1329. * PHY that has not successfully been added using
  1330. * sas_rphy_add() (or has been sas_rphy_remove()'d)
  1331. */
  1332. void sas_rphy_free(struct sas_rphy *rphy)
  1333. {
  1334. struct device *dev = &rphy->dev;
  1335. struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
  1336. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  1337. mutex_lock(&sas_host->lock);
  1338. list_del(&rphy->list);
  1339. mutex_unlock(&sas_host->lock);
  1340. sas_bsg_remove(shost, rphy);
  1341. transport_destroy_device(dev);
  1342. put_device(dev);
  1343. }
  1344. EXPORT_SYMBOL(sas_rphy_free);
  1345. /**
  1346. * sas_rphy_delete - remove and free SAS remote PHY
  1347. * @rphy: SAS remote PHY to remove and free
  1348. *
  1349. * Removes the specified SAS remote PHY and frees it.
  1350. */
  1351. void
  1352. sas_rphy_delete(struct sas_rphy *rphy)
  1353. {
  1354. sas_rphy_remove(rphy);
  1355. sas_rphy_free(rphy);
  1356. }
  1357. EXPORT_SYMBOL(sas_rphy_delete);
  1358. /**
  1359. * sas_rphy_remove - remove SAS remote PHY
  1360. * @rphy: SAS remote phy to remove
  1361. *
  1362. * Removes the specified SAS remote PHY.
  1363. */
  1364. void
  1365. sas_rphy_remove(struct sas_rphy *rphy)
  1366. {
  1367. struct device *dev = &rphy->dev;
  1368. struct sas_port *parent = dev_to_sas_port(dev->parent);
  1369. switch (rphy->identify.device_type) {
  1370. case SAS_END_DEVICE:
  1371. scsi_remove_target(dev);
  1372. break;
  1373. case SAS_EDGE_EXPANDER_DEVICE:
  1374. case SAS_FANOUT_EXPANDER_DEVICE:
  1375. sas_remove_children(dev);
  1376. break;
  1377. default:
  1378. break;
  1379. }
  1380. transport_remove_device(dev);
  1381. device_del(dev);
  1382. parent->rphy = NULL;
  1383. }
  1384. EXPORT_SYMBOL(sas_rphy_remove);
  1385. /**
  1386. * scsi_is_sas_rphy - check if a struct device represents a SAS remote PHY
  1387. * @dev: device to check
  1388. *
  1389. * Returns:
  1390. * %1 if the device represents a SAS remote PHY, %0 else
  1391. */
  1392. int scsi_is_sas_rphy(const struct device *dev)
  1393. {
  1394. return dev->release == sas_end_device_release ||
  1395. dev->release == sas_expander_release;
  1396. }
  1397. EXPORT_SYMBOL(scsi_is_sas_rphy);
  1398. /*
  1399. * SCSI scan helper
  1400. */
  1401. static int sas_user_scan(struct Scsi_Host *shost, uint channel,
  1402. uint id, uint lun)
  1403. {
  1404. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  1405. struct sas_rphy *rphy;
  1406. mutex_lock(&sas_host->lock);
  1407. list_for_each_entry(rphy, &sas_host->rphy_list, list) {
  1408. if (rphy->identify.device_type != SAS_END_DEVICE ||
  1409. rphy->scsi_target_id == -1)
  1410. continue;
  1411. if ((channel == SCAN_WILD_CARD || channel == 0) &&
  1412. (id == SCAN_WILD_CARD || id == rphy->scsi_target_id)) {
  1413. scsi_scan_target(&rphy->dev, 0,
  1414. rphy->scsi_target_id, lun, 1);
  1415. }
  1416. }
  1417. mutex_unlock(&sas_host->lock);
  1418. return 0;
  1419. }
  1420. /*
  1421. * Setup / Teardown code
  1422. */
  1423. #define SETUP_TEMPLATE(attrb, field, perm, test) \
  1424. i->private_##attrb[count] = dev_attr_##field; \
  1425. i->private_##attrb[count].attr.mode = perm; \
  1426. i->attrb[count] = &i->private_##attrb[count]; \
  1427. if (test) \
  1428. count++
  1429. #define SETUP_TEMPLATE_RW(attrb, field, perm, test, ro_test, ro_perm) \
  1430. i->private_##attrb[count] = dev_attr_##field; \
  1431. i->private_##attrb[count].attr.mode = perm; \
  1432. if (ro_test) { \
  1433. i->private_##attrb[count].attr.mode = ro_perm; \
  1434. i->private_##attrb[count].store = NULL; \
  1435. } \
  1436. i->attrb[count] = &i->private_##attrb[count]; \
  1437. if (test) \
  1438. count++
  1439. #define SETUP_RPORT_ATTRIBUTE(field) \
  1440. SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, 1)
  1441. #define SETUP_OPTIONAL_RPORT_ATTRIBUTE(field, func) \
  1442. SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, i->f->func)
  1443. #define SETUP_PHY_ATTRIBUTE(field) \
  1444. SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, 1)
  1445. #define SETUP_PHY_ATTRIBUTE_RW(field) \
  1446. SETUP_TEMPLATE_RW(phy_attrs, field, S_IRUGO | S_IWUSR, 1, \
  1447. !i->f->set_phy_speed, S_IRUGO)
  1448. #define SETUP_OPTIONAL_PHY_ATTRIBUTE_RW(field, func) \
  1449. SETUP_TEMPLATE_RW(phy_attrs, field, S_IRUGO | S_IWUSR, 1, \
  1450. !i->f->func, S_IRUGO)
  1451. #define SETUP_PORT_ATTRIBUTE(field) \
  1452. SETUP_TEMPLATE(port_attrs, field, S_IRUGO, 1)
  1453. #define SETUP_OPTIONAL_PHY_ATTRIBUTE(field, func) \
  1454. SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, i->f->func)
  1455. #define SETUP_PHY_ATTRIBUTE_WRONLY(field) \
  1456. SETUP_TEMPLATE(phy_attrs, field, S_IWUSR, 1)
  1457. #define SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(field, func) \
  1458. SETUP_TEMPLATE(phy_attrs, field, S_IWUSR, i->f->func)
  1459. #define SETUP_END_DEV_ATTRIBUTE(field) \
  1460. SETUP_TEMPLATE(end_dev_attrs, field, S_IRUGO, 1)
  1461. #define SETUP_EXPANDER_ATTRIBUTE(field) \
  1462. SETUP_TEMPLATE(expander_attrs, expander_##field, S_IRUGO, 1)
  1463. /**
  1464. * sas_attach_transport - instantiate SAS transport template
  1465. * @ft: SAS transport class function template
  1466. */
  1467. struct scsi_transport_template *
  1468. sas_attach_transport(struct sas_function_template *ft)
  1469. {
  1470. struct sas_internal *i;
  1471. int count;
  1472. i = kzalloc(sizeof(struct sas_internal), GFP_KERNEL);
  1473. if (!i)
  1474. return NULL;
  1475. i->t.user_scan = sas_user_scan;
  1476. i->t.host_attrs.ac.attrs = &i->host_attrs[0];
  1477. i->t.host_attrs.ac.class = &sas_host_class.class;
  1478. i->t.host_attrs.ac.match = sas_host_match;
  1479. transport_container_register(&i->t.host_attrs);
  1480. i->t.host_size = sizeof(struct sas_host_attrs);
  1481. i->phy_attr_cont.ac.class = &sas_phy_class.class;
  1482. i->phy_attr_cont.ac.attrs = &i->phy_attrs[0];
  1483. i->phy_attr_cont.ac.match = sas_phy_match;
  1484. transport_container_register(&i->phy_attr_cont);
  1485. i->port_attr_cont.ac.class = &sas_port_class.class;
  1486. i->port_attr_cont.ac.attrs = &i->port_attrs[0];
  1487. i->port_attr_cont.ac.match = sas_port_match;
  1488. transport_container_register(&i->port_attr_cont);
  1489. i->rphy_attr_cont.ac.class = &sas_rphy_class.class;
  1490. i->rphy_attr_cont.ac.attrs = &i->rphy_attrs[0];
  1491. i->rphy_attr_cont.ac.match = sas_rphy_match;
  1492. transport_container_register(&i->rphy_attr_cont);
  1493. i->end_dev_attr_cont.ac.class = &sas_end_dev_class.class;
  1494. i->end_dev_attr_cont.ac.attrs = &i->end_dev_attrs[0];
  1495. i->end_dev_attr_cont.ac.match = sas_end_dev_match;
  1496. transport_container_register(&i->end_dev_attr_cont);
  1497. i->expander_attr_cont.ac.class = &sas_expander_class.class;
  1498. i->expander_attr_cont.ac.attrs = &i->expander_attrs[0];
  1499. i->expander_attr_cont.ac.match = sas_expander_match;
  1500. transport_container_register(&i->expander_attr_cont);
  1501. i->f = ft;
  1502. count = 0;
  1503. SETUP_PHY_ATTRIBUTE(initiator_port_protocols);
  1504. SETUP_PHY_ATTRIBUTE(target_port_protocols);
  1505. SETUP_PHY_ATTRIBUTE(device_type);
  1506. SETUP_PHY_ATTRIBUTE(sas_address);
  1507. SETUP_PHY_ATTRIBUTE(phy_identifier);
  1508. //SETUP_PHY_ATTRIBUTE(port_identifier);
  1509. SETUP_PHY_ATTRIBUTE(negotiated_linkrate);
  1510. SETUP_PHY_ATTRIBUTE(minimum_linkrate_hw);
  1511. SETUP_PHY_ATTRIBUTE_RW(minimum_linkrate);
  1512. SETUP_PHY_ATTRIBUTE(maximum_linkrate_hw);
  1513. SETUP_PHY_ATTRIBUTE_RW(maximum_linkrate);
  1514. SETUP_PHY_ATTRIBUTE(invalid_dword_count);
  1515. SETUP_PHY_ATTRIBUTE(running_disparity_error_count);
  1516. SETUP_PHY_ATTRIBUTE(loss_of_dword_sync_count);
  1517. SETUP_PHY_ATTRIBUTE(phy_reset_problem_count);
  1518. SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(link_reset, phy_reset);
  1519. SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(hard_reset, phy_reset);
  1520. SETUP_OPTIONAL_PHY_ATTRIBUTE_RW(enable, phy_enable);
  1521. i->phy_attrs[count] = NULL;
  1522. count = 0;
  1523. SETUP_PORT_ATTRIBUTE(num_phys);
  1524. i->port_attrs[count] = NULL;
  1525. count = 0;
  1526. SETUP_RPORT_ATTRIBUTE(rphy_initiator_port_protocols);
  1527. SETUP_RPORT_ATTRIBUTE(rphy_target_port_protocols);
  1528. SETUP_RPORT_ATTRIBUTE(rphy_device_type);
  1529. SETUP_RPORT_ATTRIBUTE(rphy_sas_address);
  1530. SETUP_RPORT_ATTRIBUTE(rphy_phy_identifier);
  1531. SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_enclosure_identifier,
  1532. get_enclosure_identifier);
  1533. SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_bay_identifier,
  1534. get_bay_identifier);
  1535. i->rphy_attrs[count] = NULL;
  1536. count = 0;
  1537. SETUP_END_DEV_ATTRIBUTE(end_dev_ready_led_meaning);
  1538. SETUP_END_DEV_ATTRIBUTE(end_dev_I_T_nexus_loss_timeout);
  1539. SETUP_END_DEV_ATTRIBUTE(end_dev_initiator_response_timeout);
  1540. SETUP_END_DEV_ATTRIBUTE(end_dev_tlr_supported);
  1541. SETUP_END_DEV_ATTRIBUTE(end_dev_tlr_enabled);
  1542. i->end_dev_attrs[count] = NULL;
  1543. count = 0;
  1544. SETUP_EXPANDER_ATTRIBUTE(vendor_id);
  1545. SETUP_EXPANDER_ATTRIBUTE(product_id);
  1546. SETUP_EXPANDER_ATTRIBUTE(product_rev);
  1547. SETUP_EXPANDER_ATTRIBUTE(component_vendor_id);
  1548. SETUP_EXPANDER_ATTRIBUTE(component_id);
  1549. SETUP_EXPANDER_ATTRIBUTE(component_revision_id);
  1550. SETUP_EXPANDER_ATTRIBUTE(level);
  1551. i->expander_attrs[count] = NULL;
  1552. return &i->t;
  1553. }
  1554. EXPORT_SYMBOL(sas_attach_transport);
  1555. /**
  1556. * sas_release_transport - release SAS transport template instance
  1557. * @t: transport template instance
  1558. */
  1559. void sas_release_transport(struct scsi_transport_template *t)
  1560. {
  1561. struct sas_internal *i = to_sas_internal(t);
  1562. transport_container_unregister(&i->t.host_attrs);
  1563. transport_container_unregister(&i->phy_attr_cont);
  1564. transport_container_unregister(&i->port_attr_cont);
  1565. transport_container_unregister(&i->rphy_attr_cont);
  1566. transport_container_unregister(&i->end_dev_attr_cont);
  1567. transport_container_unregister(&i->expander_attr_cont);
  1568. kfree(i);
  1569. }
  1570. EXPORT_SYMBOL(sas_release_transport);
  1571. static __init int sas_transport_init(void)
  1572. {
  1573. int error;
  1574. error = transport_class_register(&sas_host_class);
  1575. if (error)
  1576. goto out;
  1577. error = transport_class_register(&sas_phy_class);
  1578. if (error)
  1579. goto out_unregister_transport;
  1580. error = transport_class_register(&sas_port_class);
  1581. if (error)
  1582. goto out_unregister_phy;
  1583. error = transport_class_register(&sas_rphy_class);
  1584. if (error)
  1585. goto out_unregister_port;
  1586. error = transport_class_register(&sas_end_dev_class);
  1587. if (error)
  1588. goto out_unregister_rphy;
  1589. error = transport_class_register(&sas_expander_class);
  1590. if (error)
  1591. goto out_unregister_end_dev;
  1592. return 0;
  1593. out_unregister_end_dev:
  1594. transport_class_unregister(&sas_end_dev_class);
  1595. out_unregister_rphy:
  1596. transport_class_unregister(&sas_rphy_class);
  1597. out_unregister_port:
  1598. transport_class_unregister(&sas_port_class);
  1599. out_unregister_phy:
  1600. transport_class_unregister(&sas_phy_class);
  1601. out_unregister_transport:
  1602. transport_class_unregister(&sas_host_class);
  1603. out:
  1604. return error;
  1605. }
  1606. static void __exit sas_transport_exit(void)
  1607. {
  1608. transport_class_unregister(&sas_host_class);
  1609. transport_class_unregister(&sas_phy_class);
  1610. transport_class_unregister(&sas_port_class);
  1611. transport_class_unregister(&sas_rphy_class);
  1612. transport_class_unregister(&sas_end_dev_class);
  1613. transport_class_unregister(&sas_expander_class);
  1614. }
  1615. MODULE_AUTHOR("Christoph Hellwig");
  1616. MODULE_DESCRIPTION("SAS Transport Attributes");
  1617. MODULE_LICENSE("GPL");
  1618. module_init(sas_transport_init);
  1619. module_exit(sas_transport_exit);