scsi_transport_sas.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  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 managment
  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/err.h>
  28. #include <linux/slab.h>
  29. #include <linux/string.h>
  30. #include <scsi/scsi.h>
  31. #include <scsi/scsi_device.h>
  32. #include <scsi/scsi_host.h>
  33. #include <scsi/scsi_transport.h>
  34. #include <scsi/scsi_transport_sas.h>
  35. #define SAS_HOST_ATTRS 0
  36. #define SAS_PORT_ATTRS 17
  37. #define SAS_RPORT_ATTRS 7
  38. #define SAS_END_DEV_ATTRS 3
  39. struct sas_internal {
  40. struct scsi_transport_template t;
  41. struct sas_function_template *f;
  42. struct class_device_attribute private_host_attrs[SAS_HOST_ATTRS];
  43. struct class_device_attribute private_phy_attrs[SAS_PORT_ATTRS];
  44. struct class_device_attribute private_rphy_attrs[SAS_RPORT_ATTRS];
  45. struct class_device_attribute private_end_dev_attrs[SAS_END_DEV_ATTRS];
  46. struct transport_container phy_attr_cont;
  47. struct transport_container rphy_attr_cont;
  48. struct transport_container end_dev_attr_cont;
  49. /*
  50. * The array of null terminated pointers to attributes
  51. * needed by scsi_sysfs.c
  52. */
  53. struct class_device_attribute *host_attrs[SAS_HOST_ATTRS + 1];
  54. struct class_device_attribute *phy_attrs[SAS_PORT_ATTRS + 1];
  55. struct class_device_attribute *rphy_attrs[SAS_RPORT_ATTRS + 1];
  56. struct class_device_attribute *end_dev_attrs[SAS_END_DEV_ATTRS + 1];
  57. };
  58. #define to_sas_internal(tmpl) container_of(tmpl, struct sas_internal, t)
  59. struct sas_host_attrs {
  60. struct list_head rphy_list;
  61. struct mutex lock;
  62. u32 next_target_id;
  63. };
  64. #define to_sas_host_attrs(host) ((struct sas_host_attrs *)(host)->shost_data)
  65. /*
  66. * Hack to allow attributes of the same name in different objects.
  67. */
  68. #define SAS_CLASS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
  69. struct class_device_attribute class_device_attr_##_prefix##_##_name = \
  70. __ATTR(_name,_mode,_show,_store)
  71. /*
  72. * Pretty printing helpers
  73. */
  74. #define sas_bitfield_name_match(title, table) \
  75. static ssize_t \
  76. get_sas_##title##_names(u32 table_key, char *buf) \
  77. { \
  78. char *prefix = ""; \
  79. ssize_t len = 0; \
  80. int i; \
  81. \
  82. for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) { \
  83. if (table[i].value & table_key) { \
  84. len += sprintf(buf + len, "%s%s", \
  85. prefix, table[i].name); \
  86. prefix = ", "; \
  87. } \
  88. } \
  89. len += sprintf(buf + len, "\n"); \
  90. return len; \
  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 < sizeof(table)/sizeof(table[0]); 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. /*
  143. * SAS host attributes
  144. */
  145. static int sas_host_setup(struct transport_container *tc, struct device *dev,
  146. struct class_device *cdev)
  147. {
  148. struct Scsi_Host *shost = dev_to_shost(dev);
  149. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  150. INIT_LIST_HEAD(&sas_host->rphy_list);
  151. mutex_init(&sas_host->lock);
  152. sas_host->next_target_id = 0;
  153. return 0;
  154. }
  155. static DECLARE_TRANSPORT_CLASS(sas_host_class,
  156. "sas_host", sas_host_setup, NULL, NULL);
  157. static int sas_host_match(struct attribute_container *cont,
  158. struct device *dev)
  159. {
  160. struct Scsi_Host *shost;
  161. struct sas_internal *i;
  162. if (!scsi_is_host_device(dev))
  163. return 0;
  164. shost = dev_to_shost(dev);
  165. if (!shost->transportt)
  166. return 0;
  167. if (shost->transportt->host_attrs.ac.class !=
  168. &sas_host_class.class)
  169. return 0;
  170. i = to_sas_internal(shost->transportt);
  171. return &i->t.host_attrs.ac == cont;
  172. }
  173. static int do_sas_phy_delete(struct device *dev, void *data)
  174. {
  175. if (scsi_is_sas_phy(dev))
  176. sas_phy_delete(dev_to_phy(dev));
  177. return 0;
  178. }
  179. /**
  180. * sas_remove_host -- tear down a Scsi_Host's SAS data structures
  181. * @shost: Scsi Host that is torn down
  182. *
  183. * Removes all SAS PHYs and remote PHYs for a given Scsi_Host.
  184. * Must be called just before scsi_remove_host for SAS HBAs.
  185. */
  186. void sas_remove_host(struct Scsi_Host *shost)
  187. {
  188. device_for_each_child(&shost->shost_gendev, NULL, do_sas_phy_delete);
  189. }
  190. EXPORT_SYMBOL(sas_remove_host);
  191. /*
  192. * SAS Port attributes
  193. */
  194. #define sas_phy_show_simple(field, name, format_string, cast) \
  195. static ssize_t \
  196. show_sas_phy_##name(struct class_device *cdev, char *buf) \
  197. { \
  198. struct sas_phy *phy = transport_class_to_phy(cdev); \
  199. \
  200. return snprintf(buf, 20, format_string, cast phy->field); \
  201. }
  202. #define sas_phy_simple_attr(field, name, format_string, type) \
  203. sas_phy_show_simple(field, name, format_string, (type)) \
  204. static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
  205. #define sas_phy_show_protocol(field, name) \
  206. static ssize_t \
  207. show_sas_phy_##name(struct class_device *cdev, char *buf) \
  208. { \
  209. struct sas_phy *phy = transport_class_to_phy(cdev); \
  210. \
  211. if (!phy->field) \
  212. return snprintf(buf, 20, "none\n"); \
  213. return get_sas_protocol_names(phy->field, buf); \
  214. }
  215. #define sas_phy_protocol_attr(field, name) \
  216. sas_phy_show_protocol(field, name) \
  217. static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
  218. #define sas_phy_show_linkspeed(field) \
  219. static ssize_t \
  220. show_sas_phy_##field(struct class_device *cdev, char *buf) \
  221. { \
  222. struct sas_phy *phy = transport_class_to_phy(cdev); \
  223. \
  224. return get_sas_linkspeed_names(phy->field, buf); \
  225. }
  226. #define sas_phy_linkspeed_attr(field) \
  227. sas_phy_show_linkspeed(field) \
  228. static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
  229. #define sas_phy_show_linkerror(field) \
  230. static ssize_t \
  231. show_sas_phy_##field(struct class_device *cdev, char *buf) \
  232. { \
  233. struct sas_phy *phy = transport_class_to_phy(cdev); \
  234. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); \
  235. struct sas_internal *i = to_sas_internal(shost->transportt); \
  236. int error; \
  237. \
  238. if (!phy->local_attached) \
  239. return -EINVAL; \
  240. \
  241. error = i->f->get_linkerrors ? i->f->get_linkerrors(phy) : 0; \
  242. if (error) \
  243. return error; \
  244. return snprintf(buf, 20, "%u\n", phy->field); \
  245. }
  246. #define sas_phy_linkerror_attr(field) \
  247. sas_phy_show_linkerror(field) \
  248. static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
  249. static ssize_t
  250. show_sas_device_type(struct class_device *cdev, char *buf)
  251. {
  252. struct sas_phy *phy = transport_class_to_phy(cdev);
  253. if (!phy->identify.device_type)
  254. return snprintf(buf, 20, "none\n");
  255. return get_sas_device_type_names(phy->identify.device_type, buf);
  256. }
  257. static CLASS_DEVICE_ATTR(device_type, S_IRUGO, show_sas_device_type, NULL);
  258. static ssize_t do_sas_phy_reset(struct class_device *cdev,
  259. size_t count, int hard_reset)
  260. {
  261. struct sas_phy *phy = transport_class_to_phy(cdev);
  262. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
  263. struct sas_internal *i = to_sas_internal(shost->transportt);
  264. int error;
  265. if (!phy->local_attached)
  266. return -EINVAL;
  267. error = i->f->phy_reset(phy, hard_reset);
  268. if (error)
  269. return error;
  270. return count;
  271. };
  272. static ssize_t store_sas_link_reset(struct class_device *cdev,
  273. const char *buf, size_t count)
  274. {
  275. return do_sas_phy_reset(cdev, count, 0);
  276. }
  277. static CLASS_DEVICE_ATTR(link_reset, S_IWUSR, NULL, store_sas_link_reset);
  278. static ssize_t store_sas_hard_reset(struct class_device *cdev,
  279. const char *buf, size_t count)
  280. {
  281. return do_sas_phy_reset(cdev, count, 1);
  282. }
  283. static CLASS_DEVICE_ATTR(hard_reset, S_IWUSR, NULL, store_sas_hard_reset);
  284. sas_phy_protocol_attr(identify.initiator_port_protocols,
  285. initiator_port_protocols);
  286. sas_phy_protocol_attr(identify.target_port_protocols,
  287. target_port_protocols);
  288. sas_phy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
  289. unsigned long long);
  290. sas_phy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
  291. sas_phy_simple_attr(port_identifier, port_identifier, "%d\n", u8);
  292. sas_phy_linkspeed_attr(negotiated_linkrate);
  293. sas_phy_linkspeed_attr(minimum_linkrate_hw);
  294. sas_phy_linkspeed_attr(minimum_linkrate);
  295. sas_phy_linkspeed_attr(maximum_linkrate_hw);
  296. sas_phy_linkspeed_attr(maximum_linkrate);
  297. sas_phy_linkerror_attr(invalid_dword_count);
  298. sas_phy_linkerror_attr(running_disparity_error_count);
  299. sas_phy_linkerror_attr(loss_of_dword_sync_count);
  300. sas_phy_linkerror_attr(phy_reset_problem_count);
  301. static DECLARE_TRANSPORT_CLASS(sas_phy_class,
  302. "sas_phy", NULL, NULL, NULL);
  303. static int sas_phy_match(struct attribute_container *cont, struct device *dev)
  304. {
  305. struct Scsi_Host *shost;
  306. struct sas_internal *i;
  307. if (!scsi_is_sas_phy(dev))
  308. return 0;
  309. shost = dev_to_shost(dev->parent);
  310. if (!shost->transportt)
  311. return 0;
  312. if (shost->transportt->host_attrs.ac.class !=
  313. &sas_host_class.class)
  314. return 0;
  315. i = to_sas_internal(shost->transportt);
  316. return &i->phy_attr_cont.ac == cont;
  317. }
  318. static void sas_phy_release(struct device *dev)
  319. {
  320. struct sas_phy *phy = dev_to_phy(dev);
  321. put_device(dev->parent);
  322. kfree(phy);
  323. }
  324. /**
  325. * sas_phy_alloc -- allocates and initialize a SAS PHY structure
  326. * @parent: Parent device
  327. * @number: Phy index
  328. *
  329. * Allocates an SAS PHY structure. It will be added in the device tree
  330. * below the device specified by @parent, which has to be either a Scsi_Host
  331. * or sas_rphy.
  332. *
  333. * Returns:
  334. * SAS PHY allocated or %NULL if the allocation failed.
  335. */
  336. struct sas_phy *sas_phy_alloc(struct device *parent, int number)
  337. {
  338. struct Scsi_Host *shost = dev_to_shost(parent);
  339. struct sas_phy *phy;
  340. phy = kzalloc(sizeof(*phy), GFP_KERNEL);
  341. if (!phy)
  342. return NULL;
  343. get_device(parent);
  344. phy->number = number;
  345. device_initialize(&phy->dev);
  346. phy->dev.parent = get_device(parent);
  347. phy->dev.release = sas_phy_release;
  348. sprintf(phy->dev.bus_id, "phy-%d:%d", shost->host_no, number);
  349. transport_setup_device(&phy->dev);
  350. return phy;
  351. }
  352. EXPORT_SYMBOL(sas_phy_alloc);
  353. /**
  354. * sas_phy_add -- add a SAS PHY to the device hierachy
  355. * @phy: The PHY to be added
  356. *
  357. * Publishes a SAS PHY to the rest of the system.
  358. */
  359. int sas_phy_add(struct sas_phy *phy)
  360. {
  361. int error;
  362. error = device_add(&phy->dev);
  363. if (!error) {
  364. transport_add_device(&phy->dev);
  365. transport_configure_device(&phy->dev);
  366. }
  367. return error;
  368. }
  369. EXPORT_SYMBOL(sas_phy_add);
  370. /**
  371. * sas_phy_free -- free a SAS PHY
  372. * @phy: SAS PHY to free
  373. *
  374. * Frees the specified SAS PHY.
  375. *
  376. * Note:
  377. * This function must only be called on a PHY that has not
  378. * sucessfully been added using sas_phy_add().
  379. */
  380. void sas_phy_free(struct sas_phy *phy)
  381. {
  382. transport_destroy_device(&phy->dev);
  383. put_device(phy->dev.parent);
  384. put_device(phy->dev.parent);
  385. put_device(phy->dev.parent);
  386. kfree(phy);
  387. }
  388. EXPORT_SYMBOL(sas_phy_free);
  389. /**
  390. * sas_phy_delete -- remove SAS PHY
  391. * @phy: SAS PHY to remove
  392. *
  393. * Removes the specified SAS PHY. If the SAS PHY has an
  394. * associated remote PHY it is removed before.
  395. */
  396. void
  397. sas_phy_delete(struct sas_phy *phy)
  398. {
  399. struct device *dev = &phy->dev;
  400. if (phy->rphy)
  401. sas_rphy_delete(phy->rphy);
  402. transport_remove_device(dev);
  403. device_del(dev);
  404. transport_destroy_device(dev);
  405. put_device(dev->parent);
  406. }
  407. EXPORT_SYMBOL(sas_phy_delete);
  408. /**
  409. * scsi_is_sas_phy -- check if a struct device represents a SAS PHY
  410. * @dev: device to check
  411. *
  412. * Returns:
  413. * %1 if the device represents a SAS PHY, %0 else
  414. */
  415. int scsi_is_sas_phy(const struct device *dev)
  416. {
  417. return dev->release == sas_phy_release;
  418. }
  419. EXPORT_SYMBOL(scsi_is_sas_phy);
  420. /*
  421. * SAS remote PHY attributes.
  422. */
  423. #define sas_rphy_show_simple(field, name, format_string, cast) \
  424. static ssize_t \
  425. show_sas_rphy_##name(struct class_device *cdev, char *buf) \
  426. { \
  427. struct sas_rphy *rphy = transport_class_to_rphy(cdev); \
  428. \
  429. return snprintf(buf, 20, format_string, cast rphy->field); \
  430. }
  431. #define sas_rphy_simple_attr(field, name, format_string, type) \
  432. sas_rphy_show_simple(field, name, format_string, (type)) \
  433. static SAS_CLASS_DEVICE_ATTR(rphy, name, S_IRUGO, \
  434. show_sas_rphy_##name, NULL)
  435. #define sas_rphy_show_protocol(field, name) \
  436. static ssize_t \
  437. show_sas_rphy_##name(struct class_device *cdev, char *buf) \
  438. { \
  439. struct sas_rphy *rphy = transport_class_to_rphy(cdev); \
  440. \
  441. if (!rphy->field) \
  442. return snprintf(buf, 20, "none\n"); \
  443. return get_sas_protocol_names(rphy->field, buf); \
  444. }
  445. #define sas_rphy_protocol_attr(field, name) \
  446. sas_rphy_show_protocol(field, name) \
  447. static SAS_CLASS_DEVICE_ATTR(rphy, name, S_IRUGO, \
  448. show_sas_rphy_##name, NULL)
  449. static ssize_t
  450. show_sas_rphy_device_type(struct class_device *cdev, char *buf)
  451. {
  452. struct sas_rphy *rphy = transport_class_to_rphy(cdev);
  453. if (!rphy->identify.device_type)
  454. return snprintf(buf, 20, "none\n");
  455. return get_sas_device_type_names(
  456. rphy->identify.device_type, buf);
  457. }
  458. static SAS_CLASS_DEVICE_ATTR(rphy, device_type, S_IRUGO,
  459. show_sas_rphy_device_type, NULL);
  460. static ssize_t
  461. show_sas_rphy_enclosure_identifier(struct class_device *cdev, char *buf)
  462. {
  463. struct sas_rphy *rphy = transport_class_to_rphy(cdev);
  464. struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
  465. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
  466. struct sas_internal *i = to_sas_internal(shost->transportt);
  467. u64 identifier;
  468. int error;
  469. /*
  470. * Only devices behind an expander are supported, because the
  471. * enclosure identifier is a SMP feature.
  472. */
  473. if (phy->local_attached)
  474. return -EINVAL;
  475. error = i->f->get_enclosure_identifier(rphy, &identifier);
  476. if (error)
  477. return error;
  478. return sprintf(buf, "0x%llx\n", (unsigned long long)identifier);
  479. }
  480. static SAS_CLASS_DEVICE_ATTR(rphy, enclosure_identifier, S_IRUGO,
  481. show_sas_rphy_enclosure_identifier, NULL);
  482. static ssize_t
  483. show_sas_rphy_bay_identifier(struct class_device *cdev, char *buf)
  484. {
  485. struct sas_rphy *rphy = transport_class_to_rphy(cdev);
  486. struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
  487. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
  488. struct sas_internal *i = to_sas_internal(shost->transportt);
  489. int val;
  490. if (phy->local_attached)
  491. return -EINVAL;
  492. val = i->f->get_bay_identifier(rphy);
  493. if (val < 0)
  494. return val;
  495. return sprintf(buf, "%d\n", val);
  496. }
  497. static SAS_CLASS_DEVICE_ATTR(rphy, bay_identifier, S_IRUGO,
  498. show_sas_rphy_bay_identifier, NULL);
  499. sas_rphy_protocol_attr(identify.initiator_port_protocols,
  500. initiator_port_protocols);
  501. sas_rphy_protocol_attr(identify.target_port_protocols, target_port_protocols);
  502. sas_rphy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
  503. unsigned long long);
  504. sas_rphy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
  505. /* only need 8 bytes of data plus header (4 or 8) */
  506. #define BUF_SIZE 64
  507. int sas_read_port_mode_page(struct scsi_device *sdev)
  508. {
  509. char *buffer = kzalloc(BUF_SIZE, GFP_KERNEL), *msdata;
  510. struct sas_rphy *rphy = target_to_rphy(sdev->sdev_target);
  511. struct sas_end_device *rdev;
  512. struct scsi_mode_data mode_data;
  513. int res, error;
  514. BUG_ON(rphy->identify.device_type != SAS_END_DEVICE);
  515. rdev = rphy_to_end_device(rphy);
  516. if (!buffer)
  517. return -ENOMEM;
  518. res = scsi_mode_sense(sdev, 1, 0x19, buffer, BUF_SIZE, 30*HZ, 3,
  519. &mode_data, NULL);
  520. error = -EINVAL;
  521. if (!scsi_status_is_good(res))
  522. goto out;
  523. msdata = buffer + mode_data.header_length +
  524. mode_data.block_descriptor_length;
  525. if (msdata - buffer > BUF_SIZE - 8)
  526. goto out;
  527. error = 0;
  528. rdev->ready_led_meaning = msdata[2] & 0x10 ? 1 : 0;
  529. rdev->I_T_nexus_loss_timeout = (msdata[4] << 8) + msdata[5];
  530. rdev->initiator_response_timeout = (msdata[6] << 8) + msdata[7];
  531. out:
  532. kfree(buffer);
  533. return error;
  534. }
  535. EXPORT_SYMBOL(sas_read_port_mode_page);
  536. #define sas_end_dev_show_simple(field, name, format_string, cast) \
  537. static ssize_t \
  538. show_sas_end_dev_##name(struct class_device *cdev, char *buf) \
  539. { \
  540. struct sas_rphy *rphy = transport_class_to_rphy(cdev); \
  541. struct sas_end_device *rdev = rphy_to_end_device(rphy); \
  542. \
  543. return snprintf(buf, 20, format_string, cast rdev->field); \
  544. }
  545. #define sas_end_dev_simple_attr(field, name, format_string, type) \
  546. sas_end_dev_show_simple(field, name, format_string, (type)) \
  547. static SAS_CLASS_DEVICE_ATTR(end_dev, name, S_IRUGO, \
  548. show_sas_end_dev_##name, NULL)
  549. sas_end_dev_simple_attr(ready_led_meaning, ready_led_meaning, "%d\n", int);
  550. sas_end_dev_simple_attr(I_T_nexus_loss_timeout, I_T_nexus_loss_timeout,
  551. "%d\n", int);
  552. sas_end_dev_simple_attr(initiator_response_timeout, initiator_response_timeout,
  553. "%d\n", int);
  554. static DECLARE_TRANSPORT_CLASS(sas_end_dev_class,
  555. "sas_end_device", NULL, NULL, NULL);
  556. static DECLARE_TRANSPORT_CLASS(sas_rphy_class,
  557. "sas_rphy", NULL, NULL, NULL);
  558. static int sas_rphy_match(struct attribute_container *cont, struct device *dev)
  559. {
  560. struct Scsi_Host *shost;
  561. struct sas_internal *i;
  562. if (!scsi_is_sas_rphy(dev))
  563. return 0;
  564. shost = dev_to_shost(dev->parent->parent);
  565. if (!shost->transportt)
  566. return 0;
  567. if (shost->transportt->host_attrs.ac.class !=
  568. &sas_host_class.class)
  569. return 0;
  570. i = to_sas_internal(shost->transportt);
  571. return &i->rphy_attr_cont.ac == cont;
  572. }
  573. static int sas_end_dev_match(struct attribute_container *cont,
  574. struct device *dev)
  575. {
  576. struct Scsi_Host *shost;
  577. struct sas_internal *i;
  578. struct sas_rphy *rphy;
  579. if (!scsi_is_sas_rphy(dev))
  580. return 0;
  581. shost = dev_to_shost(dev->parent->parent);
  582. rphy = dev_to_rphy(dev);
  583. if (!shost->transportt)
  584. return 0;
  585. if (shost->transportt->host_attrs.ac.class !=
  586. &sas_host_class.class)
  587. return 0;
  588. i = to_sas_internal(shost->transportt);
  589. return &i->end_dev_attr_cont.ac == cont &&
  590. rphy->identify.device_type == SAS_END_DEVICE &&
  591. /* FIXME: remove contained eventually */
  592. rphy->contained;
  593. }
  594. static void sas_rphy_release(struct device *dev)
  595. {
  596. struct sas_rphy *rphy = dev_to_rphy(dev);
  597. put_device(dev->parent);
  598. kfree(rphy);
  599. }
  600. /**
  601. * sas_rphy_alloc -- allocates and initialize a SAS remote PHY structure
  602. * @parent: SAS PHY this remote PHY is conneted to
  603. *
  604. * Allocates an SAS remote PHY structure, connected to @parent.
  605. *
  606. * Returns:
  607. * SAS PHY allocated or %NULL if the allocation failed.
  608. */
  609. struct sas_rphy *sas_rphy_alloc(struct sas_phy *parent)
  610. {
  611. struct Scsi_Host *shost = dev_to_shost(&parent->dev);
  612. struct sas_rphy *rphy;
  613. rphy = kzalloc(sizeof(*rphy), GFP_KERNEL);
  614. if (!rphy) {
  615. put_device(&parent->dev);
  616. return NULL;
  617. }
  618. device_initialize(&rphy->dev);
  619. rphy->dev.parent = get_device(&parent->dev);
  620. rphy->dev.release = sas_rphy_release;
  621. sprintf(rphy->dev.bus_id, "rphy-%d:%d-%d",
  622. shost->host_no, parent->port_identifier, parent->number);
  623. transport_setup_device(&rphy->dev);
  624. return rphy;
  625. }
  626. EXPORT_SYMBOL(sas_rphy_alloc);
  627. /**
  628. * sas_end_device_alloc - allocate an rphy for an end device
  629. *
  630. * Allocates an SAS remote PHY structure, connected to @parent.
  631. *
  632. * Returns:
  633. * SAS PHY allocated or %NULL if the allocation failed.
  634. */
  635. struct sas_rphy *sas_end_device_alloc(struct sas_phy *parent)
  636. {
  637. struct Scsi_Host *shost = dev_to_shost(&parent->dev);
  638. struct sas_end_device *rdev;
  639. rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
  640. if (!rdev) {
  641. put_device(&parent->dev);
  642. return NULL;
  643. }
  644. device_initialize(&rdev->rphy.dev);
  645. rdev->rphy.dev.parent = get_device(&parent->dev);
  646. rdev->rphy.dev.release = sas_rphy_release;
  647. sprintf(rdev->rphy.dev.bus_id, "rphy-%d:%d-%d",
  648. shost->host_no, parent->port_identifier, parent->number);
  649. rdev->rphy.identify.device_type = SAS_END_DEVICE;
  650. /* FIXME: mark the rphy as being contained in a larger structure */
  651. rdev->rphy.contained = 1;
  652. transport_setup_device(&rdev->rphy.dev);
  653. return &rdev->rphy;
  654. }
  655. EXPORT_SYMBOL(sas_end_device_alloc);
  656. /**
  657. * sas_rphy_add -- add a SAS remote PHY to the device hierachy
  658. * @rphy: The remote PHY to be added
  659. *
  660. * Publishes a SAS remote PHY to the rest of the system.
  661. */
  662. int sas_rphy_add(struct sas_rphy *rphy)
  663. {
  664. struct sas_phy *parent = dev_to_phy(rphy->dev.parent);
  665. struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
  666. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  667. struct sas_identify *identify = &rphy->identify;
  668. int error;
  669. if (parent->rphy)
  670. return -ENXIO;
  671. parent->rphy = rphy;
  672. error = device_add(&rphy->dev);
  673. if (error)
  674. return error;
  675. transport_add_device(&rphy->dev);
  676. transport_configure_device(&rphy->dev);
  677. mutex_lock(&sas_host->lock);
  678. list_add_tail(&rphy->list, &sas_host->rphy_list);
  679. if (identify->device_type == SAS_END_DEVICE &&
  680. (identify->target_port_protocols &
  681. (SAS_PROTOCOL_SSP|SAS_PROTOCOL_STP|SAS_PROTOCOL_SATA)))
  682. rphy->scsi_target_id = sas_host->next_target_id++;
  683. else
  684. rphy->scsi_target_id = -1;
  685. mutex_unlock(&sas_host->lock);
  686. if (rphy->scsi_target_id != -1) {
  687. scsi_scan_target(&rphy->dev, parent->port_identifier,
  688. rphy->scsi_target_id, ~0, 0);
  689. }
  690. return 0;
  691. }
  692. EXPORT_SYMBOL(sas_rphy_add);
  693. /**
  694. * sas_rphy_free -- free a SAS remote PHY
  695. * @rphy SAS remote PHY to free
  696. *
  697. * Frees the specified SAS remote PHY.
  698. *
  699. * Note:
  700. * This function must only be called on a remote
  701. * PHY that has not sucessfully been added using
  702. * sas_rphy_add().
  703. */
  704. void sas_rphy_free(struct sas_rphy *rphy)
  705. {
  706. struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
  707. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  708. mutex_lock(&sas_host->lock);
  709. list_del(&rphy->list);
  710. mutex_unlock(&sas_host->lock);
  711. transport_destroy_device(&rphy->dev);
  712. put_device(rphy->dev.parent);
  713. put_device(rphy->dev.parent);
  714. put_device(rphy->dev.parent);
  715. kfree(rphy);
  716. }
  717. EXPORT_SYMBOL(sas_rphy_free);
  718. /**
  719. * sas_rphy_delete -- remove SAS remote PHY
  720. * @rphy: SAS remote PHY to remove
  721. *
  722. * Removes the specified SAS remote PHY.
  723. */
  724. void
  725. sas_rphy_delete(struct sas_rphy *rphy)
  726. {
  727. struct device *dev = &rphy->dev;
  728. struct sas_phy *parent = dev_to_phy(dev->parent);
  729. struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
  730. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  731. switch (rphy->identify.device_type) {
  732. case SAS_END_DEVICE:
  733. scsi_remove_target(dev);
  734. break;
  735. case SAS_EDGE_EXPANDER_DEVICE:
  736. case SAS_FANOUT_EXPANDER_DEVICE:
  737. device_for_each_child(dev, NULL, do_sas_phy_delete);
  738. break;
  739. default:
  740. break;
  741. }
  742. transport_remove_device(dev);
  743. device_del(dev);
  744. transport_destroy_device(dev);
  745. mutex_lock(&sas_host->lock);
  746. list_del(&rphy->list);
  747. mutex_unlock(&sas_host->lock);
  748. parent->rphy = NULL;
  749. put_device(&parent->dev);
  750. }
  751. EXPORT_SYMBOL(sas_rphy_delete);
  752. /**
  753. * scsi_is_sas_rphy -- check if a struct device represents a SAS remote PHY
  754. * @dev: device to check
  755. *
  756. * Returns:
  757. * %1 if the device represents a SAS remote PHY, %0 else
  758. */
  759. int scsi_is_sas_rphy(const struct device *dev)
  760. {
  761. return dev->release == sas_rphy_release;
  762. }
  763. EXPORT_SYMBOL(scsi_is_sas_rphy);
  764. /*
  765. * SCSI scan helper
  766. */
  767. static int sas_user_scan(struct Scsi_Host *shost, uint channel,
  768. uint id, uint lun)
  769. {
  770. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  771. struct sas_rphy *rphy;
  772. mutex_lock(&sas_host->lock);
  773. list_for_each_entry(rphy, &sas_host->rphy_list, list) {
  774. struct sas_phy *parent = dev_to_phy(rphy->dev.parent);
  775. if (rphy->scsi_target_id == -1)
  776. continue;
  777. if ((channel == SCAN_WILD_CARD || channel == parent->port_identifier) &&
  778. (id == SCAN_WILD_CARD || id == rphy->scsi_target_id)) {
  779. scsi_scan_target(&rphy->dev, parent->port_identifier,
  780. rphy->scsi_target_id, lun, 1);
  781. }
  782. }
  783. mutex_unlock(&sas_host->lock);
  784. return 0;
  785. }
  786. /*
  787. * Setup / Teardown code
  788. */
  789. #define SETUP_TEMPLATE(attrb, field, perm, test) \
  790. i->private_##attrb[count] = class_device_attr_##field; \
  791. i->private_##attrb[count].attr.mode = perm; \
  792. i->private_##attrb[count].store = NULL; \
  793. i->attrb[count] = &i->private_##attrb[count]; \
  794. if (test) \
  795. count++
  796. #define SETUP_RPORT_ATTRIBUTE(field) \
  797. SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, 1)
  798. #define SETUP_OPTIONAL_RPORT_ATTRIBUTE(field, func) \
  799. SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, i->f->func)
  800. #define SETUP_PORT_ATTRIBUTE(field) \
  801. SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, 1)
  802. #define SETUP_OPTIONAL_PORT_ATTRIBUTE(field, func) \
  803. SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, i->f->func)
  804. #define SETUP_PORT_ATTRIBUTE_WRONLY(field) \
  805. SETUP_TEMPLATE(phy_attrs, field, S_IWUGO, 1)
  806. #define SETUP_OPTIONAL_PORT_ATTRIBUTE_WRONLY(field, func) \
  807. SETUP_TEMPLATE(phy_attrs, field, S_IWUGO, i->f->func)
  808. #define SETUP_END_DEV_ATTRIBUTE(field) \
  809. SETUP_TEMPLATE(end_dev_attrs, field, S_IRUGO, 1)
  810. /**
  811. * sas_attach_transport -- instantiate SAS transport template
  812. * @ft: SAS transport class function template
  813. */
  814. struct scsi_transport_template *
  815. sas_attach_transport(struct sas_function_template *ft)
  816. {
  817. struct sas_internal *i;
  818. int count;
  819. i = kzalloc(sizeof(struct sas_internal), GFP_KERNEL);
  820. if (!i)
  821. return NULL;
  822. i->t.user_scan = sas_user_scan;
  823. i->t.host_attrs.ac.attrs = &i->host_attrs[0];
  824. i->t.host_attrs.ac.class = &sas_host_class.class;
  825. i->t.host_attrs.ac.match = sas_host_match;
  826. transport_container_register(&i->t.host_attrs);
  827. i->t.host_size = sizeof(struct sas_host_attrs);
  828. i->phy_attr_cont.ac.class = &sas_phy_class.class;
  829. i->phy_attr_cont.ac.attrs = &i->phy_attrs[0];
  830. i->phy_attr_cont.ac.match = sas_phy_match;
  831. transport_container_register(&i->phy_attr_cont);
  832. i->rphy_attr_cont.ac.class = &sas_rphy_class.class;
  833. i->rphy_attr_cont.ac.attrs = &i->rphy_attrs[0];
  834. i->rphy_attr_cont.ac.match = sas_rphy_match;
  835. transport_container_register(&i->rphy_attr_cont);
  836. i->end_dev_attr_cont.ac.class = &sas_end_dev_class.class;
  837. i->end_dev_attr_cont.ac.attrs = &i->end_dev_attrs[0];
  838. i->end_dev_attr_cont.ac.match = sas_end_dev_match;
  839. transport_container_register(&i->end_dev_attr_cont);
  840. i->f = ft;
  841. count = 0;
  842. i->host_attrs[count] = NULL;
  843. count = 0;
  844. SETUP_PORT_ATTRIBUTE(initiator_port_protocols);
  845. SETUP_PORT_ATTRIBUTE(target_port_protocols);
  846. SETUP_PORT_ATTRIBUTE(device_type);
  847. SETUP_PORT_ATTRIBUTE(sas_address);
  848. SETUP_PORT_ATTRIBUTE(phy_identifier);
  849. SETUP_PORT_ATTRIBUTE(port_identifier);
  850. SETUP_PORT_ATTRIBUTE(negotiated_linkrate);
  851. SETUP_PORT_ATTRIBUTE(minimum_linkrate_hw);
  852. SETUP_PORT_ATTRIBUTE(minimum_linkrate);
  853. SETUP_PORT_ATTRIBUTE(maximum_linkrate_hw);
  854. SETUP_PORT_ATTRIBUTE(maximum_linkrate);
  855. SETUP_PORT_ATTRIBUTE(invalid_dword_count);
  856. SETUP_PORT_ATTRIBUTE(running_disparity_error_count);
  857. SETUP_PORT_ATTRIBUTE(loss_of_dword_sync_count);
  858. SETUP_PORT_ATTRIBUTE(phy_reset_problem_count);
  859. SETUP_OPTIONAL_PORT_ATTRIBUTE_WRONLY(link_reset, phy_reset);
  860. SETUP_OPTIONAL_PORT_ATTRIBUTE_WRONLY(hard_reset, phy_reset);
  861. i->phy_attrs[count] = NULL;
  862. count = 0;
  863. SETUP_RPORT_ATTRIBUTE(rphy_initiator_port_protocols);
  864. SETUP_RPORT_ATTRIBUTE(rphy_target_port_protocols);
  865. SETUP_RPORT_ATTRIBUTE(rphy_device_type);
  866. SETUP_RPORT_ATTRIBUTE(rphy_sas_address);
  867. SETUP_RPORT_ATTRIBUTE(rphy_phy_identifier);
  868. SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_enclosure_identifier,
  869. get_enclosure_identifier);
  870. SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_bay_identifier,
  871. get_bay_identifier);
  872. i->rphy_attrs[count] = NULL;
  873. count = 0;
  874. SETUP_END_DEV_ATTRIBUTE(end_dev_ready_led_meaning);
  875. SETUP_END_DEV_ATTRIBUTE(end_dev_I_T_nexus_loss_timeout);
  876. SETUP_END_DEV_ATTRIBUTE(end_dev_initiator_response_timeout);
  877. i->end_dev_attrs[count] = NULL;
  878. return &i->t;
  879. }
  880. EXPORT_SYMBOL(sas_attach_transport);
  881. /**
  882. * sas_release_transport -- release SAS transport template instance
  883. * @t: transport template instance
  884. */
  885. void sas_release_transport(struct scsi_transport_template *t)
  886. {
  887. struct sas_internal *i = to_sas_internal(t);
  888. transport_container_unregister(&i->t.host_attrs);
  889. transport_container_unregister(&i->phy_attr_cont);
  890. transport_container_unregister(&i->rphy_attr_cont);
  891. transport_container_unregister(&i->end_dev_attr_cont);
  892. kfree(i);
  893. }
  894. EXPORT_SYMBOL(sas_release_transport);
  895. static __init int sas_transport_init(void)
  896. {
  897. int error;
  898. error = transport_class_register(&sas_host_class);
  899. if (error)
  900. goto out;
  901. error = transport_class_register(&sas_phy_class);
  902. if (error)
  903. goto out_unregister_transport;
  904. error = transport_class_register(&sas_rphy_class);
  905. if (error)
  906. goto out_unregister_phy;
  907. error = transport_class_register(&sas_end_dev_class);
  908. if (error)
  909. goto out_unregister_rphy;
  910. return 0;
  911. out_unregister_rphy:
  912. transport_class_unregister(&sas_rphy_class);
  913. out_unregister_phy:
  914. transport_class_unregister(&sas_phy_class);
  915. out_unregister_transport:
  916. transport_class_unregister(&sas_host_class);
  917. out:
  918. return error;
  919. }
  920. static void __exit sas_transport_exit(void)
  921. {
  922. transport_class_unregister(&sas_host_class);
  923. transport_class_unregister(&sas_phy_class);
  924. transport_class_unregister(&sas_rphy_class);
  925. transport_class_unregister(&sas_end_dev_class);
  926. }
  927. MODULE_AUTHOR("Christoph Hellwig");
  928. MODULE_DESCRIPTION("SAS Transphy Attributes");
  929. MODULE_LICENSE("GPL");
  930. module_init(sas_transport_init);
  931. module_exit(sas_transport_exit);