scsi_transport_sas.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486
  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. #include "scsi_sas_internal.h"
  36. struct sas_host_attrs {
  37. struct list_head rphy_list;
  38. struct mutex lock;
  39. u32 next_target_id;
  40. u32 next_expander_id;
  41. };
  42. #define to_sas_host_attrs(host) ((struct sas_host_attrs *)(host)->shost_data)
  43. /*
  44. * Hack to allow attributes of the same name in different objects.
  45. */
  46. #define SAS_CLASS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
  47. struct class_device_attribute class_device_attr_##_prefix##_##_name = \
  48. __ATTR(_name,_mode,_show,_store)
  49. /*
  50. * Pretty printing helpers
  51. */
  52. #define sas_bitfield_name_match(title, table) \
  53. static ssize_t \
  54. get_sas_##title##_names(u32 table_key, char *buf) \
  55. { \
  56. char *prefix = ""; \
  57. ssize_t len = 0; \
  58. int i; \
  59. \
  60. for (i = 0; i < ARRAY_SIZE(table); i++) { \
  61. if (table[i].value & table_key) { \
  62. len += sprintf(buf + len, "%s%s", \
  63. prefix, table[i].name); \
  64. prefix = ", "; \
  65. } \
  66. } \
  67. len += sprintf(buf + len, "\n"); \
  68. return len; \
  69. }
  70. #define sas_bitfield_name_search(title, table) \
  71. static ssize_t \
  72. get_sas_##title##_names(u32 table_key, char *buf) \
  73. { \
  74. ssize_t len = 0; \
  75. int i; \
  76. \
  77. for (i = 0; i < ARRAY_SIZE(table); i++) { \
  78. if (table[i].value == table_key) { \
  79. len += sprintf(buf + len, "%s", \
  80. table[i].name); \
  81. break; \
  82. } \
  83. } \
  84. len += sprintf(buf + len, "\n"); \
  85. return len; \
  86. }
  87. static struct {
  88. u32 value;
  89. char *name;
  90. } sas_device_type_names[] = {
  91. { SAS_PHY_UNUSED, "unused" },
  92. { SAS_END_DEVICE, "end device" },
  93. { SAS_EDGE_EXPANDER_DEVICE, "edge expander" },
  94. { SAS_FANOUT_EXPANDER_DEVICE, "fanout expander" },
  95. };
  96. sas_bitfield_name_search(device_type, sas_device_type_names)
  97. static struct {
  98. u32 value;
  99. char *name;
  100. } sas_protocol_names[] = {
  101. { SAS_PROTOCOL_SATA, "sata" },
  102. { SAS_PROTOCOL_SMP, "smp" },
  103. { SAS_PROTOCOL_STP, "stp" },
  104. { SAS_PROTOCOL_SSP, "ssp" },
  105. };
  106. sas_bitfield_name_match(protocol, sas_protocol_names)
  107. static struct {
  108. u32 value;
  109. char *name;
  110. } sas_linkspeed_names[] = {
  111. { SAS_LINK_RATE_UNKNOWN, "Unknown" },
  112. { SAS_PHY_DISABLED, "Phy disabled" },
  113. { SAS_LINK_RATE_FAILED, "Link Rate failed" },
  114. { SAS_SATA_SPINUP_HOLD, "Spin-up hold" },
  115. { SAS_LINK_RATE_1_5_GBPS, "1.5 Gbit" },
  116. { SAS_LINK_RATE_3_0_GBPS, "3.0 Gbit" },
  117. { SAS_LINK_RATE_6_0_GBPS, "6.0 Gbit" },
  118. };
  119. sas_bitfield_name_search(linkspeed, sas_linkspeed_names)
  120. /*
  121. * SAS host attributes
  122. */
  123. static int sas_host_setup(struct transport_container *tc, struct device *dev,
  124. struct class_device *cdev)
  125. {
  126. struct Scsi_Host *shost = dev_to_shost(dev);
  127. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  128. INIT_LIST_HEAD(&sas_host->rphy_list);
  129. mutex_init(&sas_host->lock);
  130. sas_host->next_target_id = 0;
  131. sas_host->next_expander_id = 0;
  132. return 0;
  133. }
  134. static DECLARE_TRANSPORT_CLASS(sas_host_class,
  135. "sas_host", sas_host_setup, NULL, NULL);
  136. static int sas_host_match(struct attribute_container *cont,
  137. struct device *dev)
  138. {
  139. struct Scsi_Host *shost;
  140. struct sas_internal *i;
  141. if (!scsi_is_host_device(dev))
  142. return 0;
  143. shost = dev_to_shost(dev);
  144. if (!shost->transportt)
  145. return 0;
  146. if (shost->transportt->host_attrs.ac.class !=
  147. &sas_host_class.class)
  148. return 0;
  149. i = to_sas_internal(shost->transportt);
  150. return &i->t.host_attrs.ac == cont;
  151. }
  152. static int do_sas_phy_delete(struct device *dev, void *data)
  153. {
  154. int pass = (int)(unsigned long)data;
  155. if (pass == 0 && scsi_is_sas_port(dev))
  156. sas_port_delete(dev_to_sas_port(dev));
  157. else if (pass == 1 && scsi_is_sas_phy(dev))
  158. sas_phy_delete(dev_to_phy(dev));
  159. return 0;
  160. }
  161. /**
  162. * sas_remove_children -- tear down a devices SAS data structures
  163. * @dev: device belonging to the sas object
  164. *
  165. * Removes all SAS PHYs and remote PHYs for a given object
  166. */
  167. void sas_remove_children(struct device *dev)
  168. {
  169. device_for_each_child(dev, (void *)0, do_sas_phy_delete);
  170. device_for_each_child(dev, (void *)1, do_sas_phy_delete);
  171. }
  172. EXPORT_SYMBOL(sas_remove_children);
  173. /**
  174. * sas_remove_host -- tear down a Scsi_Host's SAS data structures
  175. * @shost: Scsi Host that is torn down
  176. *
  177. * Removes all SAS PHYs and remote PHYs for a given Scsi_Host.
  178. * Must be called just before scsi_remove_host for SAS HBAs.
  179. */
  180. void sas_remove_host(struct Scsi_Host *shost)
  181. {
  182. sas_remove_children(&shost->shost_gendev);
  183. }
  184. EXPORT_SYMBOL(sas_remove_host);
  185. /*
  186. * SAS Phy attributes
  187. */
  188. #define sas_phy_show_simple(field, name, format_string, cast) \
  189. static ssize_t \
  190. show_sas_phy_##name(struct class_device *cdev, char *buf) \
  191. { \
  192. struct sas_phy *phy = transport_class_to_phy(cdev); \
  193. \
  194. return snprintf(buf, 20, format_string, cast phy->field); \
  195. }
  196. #define sas_phy_simple_attr(field, name, format_string, type) \
  197. sas_phy_show_simple(field, name, format_string, (type)) \
  198. static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
  199. #define sas_phy_show_protocol(field, name) \
  200. static ssize_t \
  201. show_sas_phy_##name(struct class_device *cdev, char *buf) \
  202. { \
  203. struct sas_phy *phy = transport_class_to_phy(cdev); \
  204. \
  205. if (!phy->field) \
  206. return snprintf(buf, 20, "none\n"); \
  207. return get_sas_protocol_names(phy->field, buf); \
  208. }
  209. #define sas_phy_protocol_attr(field, name) \
  210. sas_phy_show_protocol(field, name) \
  211. static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
  212. #define sas_phy_show_linkspeed(field) \
  213. static ssize_t \
  214. show_sas_phy_##field(struct class_device *cdev, char *buf) \
  215. { \
  216. struct sas_phy *phy = transport_class_to_phy(cdev); \
  217. \
  218. return get_sas_linkspeed_names(phy->field, buf); \
  219. }
  220. #define sas_phy_linkspeed_attr(field) \
  221. sas_phy_show_linkspeed(field) \
  222. static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
  223. #define sas_phy_show_linkerror(field) \
  224. static ssize_t \
  225. show_sas_phy_##field(struct class_device *cdev, char *buf) \
  226. { \
  227. struct sas_phy *phy = transport_class_to_phy(cdev); \
  228. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); \
  229. struct sas_internal *i = to_sas_internal(shost->transportt); \
  230. int error; \
  231. \
  232. if (!phy->local_attached) \
  233. return -EINVAL; \
  234. \
  235. error = i->f->get_linkerrors ? i->f->get_linkerrors(phy) : 0; \
  236. if (error) \
  237. return error; \
  238. return snprintf(buf, 20, "%u\n", phy->field); \
  239. }
  240. #define sas_phy_linkerror_attr(field) \
  241. sas_phy_show_linkerror(field) \
  242. static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
  243. static ssize_t
  244. show_sas_device_type(struct class_device *cdev, char *buf)
  245. {
  246. struct sas_phy *phy = transport_class_to_phy(cdev);
  247. if (!phy->identify.device_type)
  248. return snprintf(buf, 20, "none\n");
  249. return get_sas_device_type_names(phy->identify.device_type, buf);
  250. }
  251. static CLASS_DEVICE_ATTR(device_type, S_IRUGO, show_sas_device_type, NULL);
  252. static ssize_t do_sas_phy_reset(struct class_device *cdev,
  253. size_t count, int hard_reset)
  254. {
  255. struct sas_phy *phy = transport_class_to_phy(cdev);
  256. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
  257. struct sas_internal *i = to_sas_internal(shost->transportt);
  258. int error;
  259. if (!phy->local_attached)
  260. return -EINVAL;
  261. error = i->f->phy_reset(phy, hard_reset);
  262. if (error)
  263. return error;
  264. return count;
  265. };
  266. static ssize_t store_sas_link_reset(struct class_device *cdev,
  267. const char *buf, size_t count)
  268. {
  269. return do_sas_phy_reset(cdev, count, 0);
  270. }
  271. static CLASS_DEVICE_ATTR(link_reset, S_IWUSR, NULL, store_sas_link_reset);
  272. static ssize_t store_sas_hard_reset(struct class_device *cdev,
  273. const char *buf, size_t count)
  274. {
  275. return do_sas_phy_reset(cdev, count, 1);
  276. }
  277. static CLASS_DEVICE_ATTR(hard_reset, S_IWUSR, NULL, store_sas_hard_reset);
  278. sas_phy_protocol_attr(identify.initiator_port_protocols,
  279. initiator_port_protocols);
  280. sas_phy_protocol_attr(identify.target_port_protocols,
  281. target_port_protocols);
  282. sas_phy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
  283. unsigned long long);
  284. sas_phy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
  285. //sas_phy_simple_attr(port_identifier, port_identifier, "%d\n", u8);
  286. sas_phy_linkspeed_attr(negotiated_linkrate);
  287. sas_phy_linkspeed_attr(minimum_linkrate_hw);
  288. sas_phy_linkspeed_attr(minimum_linkrate);
  289. sas_phy_linkspeed_attr(maximum_linkrate_hw);
  290. sas_phy_linkspeed_attr(maximum_linkrate);
  291. sas_phy_linkerror_attr(invalid_dword_count);
  292. sas_phy_linkerror_attr(running_disparity_error_count);
  293. sas_phy_linkerror_attr(loss_of_dword_sync_count);
  294. sas_phy_linkerror_attr(phy_reset_problem_count);
  295. static DECLARE_TRANSPORT_CLASS(sas_phy_class,
  296. "sas_phy", NULL, NULL, NULL);
  297. static int sas_phy_match(struct attribute_container *cont, struct device *dev)
  298. {
  299. struct Scsi_Host *shost;
  300. struct sas_internal *i;
  301. if (!scsi_is_sas_phy(dev))
  302. return 0;
  303. shost = dev_to_shost(dev->parent);
  304. if (!shost->transportt)
  305. return 0;
  306. if (shost->transportt->host_attrs.ac.class !=
  307. &sas_host_class.class)
  308. return 0;
  309. i = to_sas_internal(shost->transportt);
  310. return &i->phy_attr_cont.ac == cont;
  311. }
  312. static void sas_phy_release(struct device *dev)
  313. {
  314. struct sas_phy *phy = dev_to_phy(dev);
  315. put_device(dev->parent);
  316. kfree(phy);
  317. }
  318. /**
  319. * sas_phy_alloc -- allocates and initialize a SAS PHY structure
  320. * @parent: Parent device
  321. * @number: Phy index
  322. *
  323. * Allocates an SAS PHY structure. It will be added in the device tree
  324. * below the device specified by @parent, which has to be either a Scsi_Host
  325. * or sas_rphy.
  326. *
  327. * Returns:
  328. * SAS PHY allocated or %NULL if the allocation failed.
  329. */
  330. struct sas_phy *sas_phy_alloc(struct device *parent, int number)
  331. {
  332. struct Scsi_Host *shost = dev_to_shost(parent);
  333. struct sas_phy *phy;
  334. phy = kzalloc(sizeof(*phy), GFP_KERNEL);
  335. if (!phy)
  336. return NULL;
  337. phy->number = number;
  338. device_initialize(&phy->dev);
  339. phy->dev.parent = get_device(parent);
  340. phy->dev.release = sas_phy_release;
  341. INIT_LIST_HEAD(&phy->port_siblings);
  342. if (scsi_is_sas_expander_device(parent)) {
  343. struct sas_rphy *rphy = dev_to_rphy(parent);
  344. sprintf(phy->dev.bus_id, "phy-%d:%d:%d", shost->host_no,
  345. rphy->scsi_target_id, number);
  346. } else
  347. sprintf(phy->dev.bus_id, "phy-%d:%d", shost->host_no, number);
  348. transport_setup_device(&phy->dev);
  349. return phy;
  350. }
  351. EXPORT_SYMBOL(sas_phy_alloc);
  352. /**
  353. * sas_phy_add -- add a SAS PHY to the device hierachy
  354. * @phy: The PHY to be added
  355. *
  356. * Publishes a SAS PHY to the rest of the system.
  357. */
  358. int sas_phy_add(struct sas_phy *phy)
  359. {
  360. int error;
  361. error = device_add(&phy->dev);
  362. if (!error) {
  363. transport_add_device(&phy->dev);
  364. transport_configure_device(&phy->dev);
  365. }
  366. return error;
  367. }
  368. EXPORT_SYMBOL(sas_phy_add);
  369. /**
  370. * sas_phy_free -- free a SAS PHY
  371. * @phy: SAS PHY to free
  372. *
  373. * Frees the specified SAS PHY.
  374. *
  375. * Note:
  376. * This function must only be called on a PHY that has not
  377. * sucessfully been added using sas_phy_add().
  378. */
  379. void sas_phy_free(struct sas_phy *phy)
  380. {
  381. transport_destroy_device(&phy->dev);
  382. put_device(&phy->dev);
  383. }
  384. EXPORT_SYMBOL(sas_phy_free);
  385. /**
  386. * sas_phy_delete -- remove SAS PHY
  387. * @phy: SAS PHY to remove
  388. *
  389. * Removes the specified SAS PHY. If the SAS PHY has an
  390. * associated remote PHY it is removed before.
  391. */
  392. void
  393. sas_phy_delete(struct sas_phy *phy)
  394. {
  395. struct device *dev = &phy->dev;
  396. /* this happens if the phy is still part of a port when deleted */
  397. BUG_ON(!list_empty(&phy->port_siblings));
  398. transport_remove_device(dev);
  399. device_del(dev);
  400. transport_destroy_device(dev);
  401. put_device(dev);
  402. }
  403. EXPORT_SYMBOL(sas_phy_delete);
  404. /**
  405. * scsi_is_sas_phy -- check if a struct device represents a SAS PHY
  406. * @dev: device to check
  407. *
  408. * Returns:
  409. * %1 if the device represents a SAS PHY, %0 else
  410. */
  411. int scsi_is_sas_phy(const struct device *dev)
  412. {
  413. return dev->release == sas_phy_release;
  414. }
  415. EXPORT_SYMBOL(scsi_is_sas_phy);
  416. /*
  417. * SAS Port attributes
  418. */
  419. #define sas_port_show_simple(field, name, format_string, cast) \
  420. static ssize_t \
  421. show_sas_port_##name(struct class_device *cdev, char *buf) \
  422. { \
  423. struct sas_port *port = transport_class_to_sas_port(cdev); \
  424. \
  425. return snprintf(buf, 20, format_string, cast port->field); \
  426. }
  427. #define sas_port_simple_attr(field, name, format_string, type) \
  428. sas_port_show_simple(field, name, format_string, (type)) \
  429. static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_port_##name, NULL)
  430. sas_port_simple_attr(num_phys, num_phys, "%d\n", int);
  431. static DECLARE_TRANSPORT_CLASS(sas_port_class,
  432. "sas_port", NULL, NULL, NULL);
  433. static int sas_port_match(struct attribute_container *cont, struct device *dev)
  434. {
  435. struct Scsi_Host *shost;
  436. struct sas_internal *i;
  437. if (!scsi_is_sas_port(dev))
  438. return 0;
  439. shost = dev_to_shost(dev->parent);
  440. if (!shost->transportt)
  441. return 0;
  442. if (shost->transportt->host_attrs.ac.class !=
  443. &sas_host_class.class)
  444. return 0;
  445. i = to_sas_internal(shost->transportt);
  446. return &i->port_attr_cont.ac == cont;
  447. }
  448. static void sas_port_release(struct device *dev)
  449. {
  450. struct sas_port *port = dev_to_sas_port(dev);
  451. BUG_ON(!list_empty(&port->phy_list));
  452. put_device(dev->parent);
  453. kfree(port);
  454. }
  455. static void sas_port_create_link(struct sas_port *port,
  456. struct sas_phy *phy)
  457. {
  458. sysfs_create_link(&port->dev.kobj, &phy->dev.kobj, phy->dev.bus_id);
  459. sysfs_create_link(&phy->dev.kobj, &port->dev.kobj, "port");
  460. }
  461. static void sas_port_delete_link(struct sas_port *port,
  462. struct sas_phy *phy)
  463. {
  464. sysfs_remove_link(&port->dev.kobj, phy->dev.bus_id);
  465. sysfs_remove_link(&phy->dev.kobj, "port");
  466. }
  467. /** sas_port_alloc - allocate and initialize a SAS port structure
  468. *
  469. * @parent: parent device
  470. * @port_id: port number
  471. *
  472. * Allocates a SAS port structure. It will be added to the device tree
  473. * below the device specified by @parent which must be either a Scsi_Host
  474. * or a sas_expander_device.
  475. *
  476. * Returns %NULL on error
  477. */
  478. struct sas_port *sas_port_alloc(struct device *parent, int port_id)
  479. {
  480. struct Scsi_Host *shost = dev_to_shost(parent);
  481. struct sas_port *port;
  482. port = kzalloc(sizeof(*port), GFP_KERNEL);
  483. if (!port)
  484. return NULL;
  485. port->port_identifier = port_id;
  486. device_initialize(&port->dev);
  487. port->dev.parent = get_device(parent);
  488. port->dev.release = sas_port_release;
  489. mutex_init(&port->phy_list_mutex);
  490. INIT_LIST_HEAD(&port->phy_list);
  491. if (scsi_is_sas_expander_device(parent)) {
  492. struct sas_rphy *rphy = dev_to_rphy(parent);
  493. sprintf(port->dev.bus_id, "port-%d:%d:%d", shost->host_no,
  494. rphy->scsi_target_id, port->port_identifier);
  495. } else
  496. sprintf(port->dev.bus_id, "port-%d:%d", shost->host_no,
  497. port->port_identifier);
  498. transport_setup_device(&port->dev);
  499. return port;
  500. }
  501. EXPORT_SYMBOL(sas_port_alloc);
  502. /**
  503. * sas_port_add - add a SAS port to the device hierarchy
  504. *
  505. * @port: port to be added
  506. *
  507. * publishes a port to the rest of the system
  508. */
  509. int sas_port_add(struct sas_port *port)
  510. {
  511. int error;
  512. /* No phys should be added until this is made visible */
  513. BUG_ON(!list_empty(&port->phy_list));
  514. error = device_add(&port->dev);
  515. if (error)
  516. return error;
  517. transport_add_device(&port->dev);
  518. transport_configure_device(&port->dev);
  519. return 0;
  520. }
  521. EXPORT_SYMBOL(sas_port_add);
  522. /**
  523. * sas_port_free -- free a SAS PORT
  524. * @port: SAS PORT to free
  525. *
  526. * Frees the specified SAS PORT.
  527. *
  528. * Note:
  529. * This function must only be called on a PORT that has not
  530. * sucessfully been added using sas_port_add().
  531. */
  532. void sas_port_free(struct sas_port *port)
  533. {
  534. transport_destroy_device(&port->dev);
  535. put_device(&port->dev);
  536. }
  537. EXPORT_SYMBOL(sas_port_free);
  538. /**
  539. * sas_port_delete -- remove SAS PORT
  540. * @port: SAS PORT to remove
  541. *
  542. * Removes the specified SAS PORT. If the SAS PORT has an
  543. * associated phys, unlink them from the port as well.
  544. */
  545. void sas_port_delete(struct sas_port *port)
  546. {
  547. struct device *dev = &port->dev;
  548. struct sas_phy *phy, *tmp_phy;
  549. if (port->rphy) {
  550. sas_rphy_delete(port->rphy);
  551. port->rphy = NULL;
  552. }
  553. mutex_lock(&port->phy_list_mutex);
  554. list_for_each_entry_safe(phy, tmp_phy, &port->phy_list,
  555. port_siblings) {
  556. sas_port_delete_link(port, phy);
  557. list_del_init(&phy->port_siblings);
  558. }
  559. mutex_unlock(&port->phy_list_mutex);
  560. transport_remove_device(dev);
  561. device_del(dev);
  562. transport_destroy_device(dev);
  563. put_device(dev);
  564. }
  565. EXPORT_SYMBOL(sas_port_delete);
  566. /**
  567. * scsi_is_sas_port -- check if a struct device represents a SAS port
  568. * @dev: device to check
  569. *
  570. * Returns:
  571. * %1 if the device represents a SAS Port, %0 else
  572. */
  573. int scsi_is_sas_port(const struct device *dev)
  574. {
  575. return dev->release == sas_port_release;
  576. }
  577. EXPORT_SYMBOL(scsi_is_sas_port);
  578. /**
  579. * sas_port_add_phy - add another phy to a port to form a wide port
  580. * @port: port to add the phy to
  581. * @phy: phy to add
  582. *
  583. * When a port is initially created, it is empty (has no phys). All
  584. * ports must have at least one phy to operated, and all wide ports
  585. * must have at least two. The current code makes no difference
  586. * between ports and wide ports, but the only object that can be
  587. * connected to a remote device is a port, so ports must be formed on
  588. * all devices with phys if they're connected to anything.
  589. */
  590. void sas_port_add_phy(struct sas_port *port, struct sas_phy *phy)
  591. {
  592. mutex_lock(&port->phy_list_mutex);
  593. if (unlikely(!list_empty(&phy->port_siblings))) {
  594. /* make sure we're already on this port */
  595. struct sas_phy *tmp;
  596. list_for_each_entry(tmp, &port->phy_list, port_siblings)
  597. if (tmp == phy)
  598. break;
  599. /* If this trips, you added a phy that was already
  600. * part of a different port */
  601. if (unlikely(tmp != phy)) {
  602. dev_printk(KERN_ERR, &port->dev, "trying to add phy %s fails: it's already part of another port\n", phy->dev.bus_id);
  603. BUG();
  604. }
  605. } else {
  606. sas_port_create_link(port, phy);
  607. list_add_tail(&phy->port_siblings, &port->phy_list);
  608. port->num_phys++;
  609. }
  610. mutex_unlock(&port->phy_list_mutex);
  611. }
  612. EXPORT_SYMBOL(sas_port_add_phy);
  613. /**
  614. * sas_port_delete_phy - remove a phy from a port or wide port
  615. * @port: port to remove the phy from
  616. * @phy: phy to remove
  617. *
  618. * This operation is used for tearing down ports again. It must be
  619. * done to every port or wide port before calling sas_port_delete.
  620. */
  621. void sas_port_delete_phy(struct sas_port *port, struct sas_phy *phy)
  622. {
  623. mutex_lock(&port->phy_list_mutex);
  624. sas_port_delete_link(port, phy);
  625. list_del_init(&phy->port_siblings);
  626. port->num_phys--;
  627. mutex_unlock(&port->phy_list_mutex);
  628. }
  629. EXPORT_SYMBOL(sas_port_delete_phy);
  630. /*
  631. * SAS remote PHY attributes.
  632. */
  633. #define sas_rphy_show_simple(field, name, format_string, cast) \
  634. static ssize_t \
  635. show_sas_rphy_##name(struct class_device *cdev, char *buf) \
  636. { \
  637. struct sas_rphy *rphy = transport_class_to_rphy(cdev); \
  638. \
  639. return snprintf(buf, 20, format_string, cast rphy->field); \
  640. }
  641. #define sas_rphy_simple_attr(field, name, format_string, type) \
  642. sas_rphy_show_simple(field, name, format_string, (type)) \
  643. static SAS_CLASS_DEVICE_ATTR(rphy, name, S_IRUGO, \
  644. show_sas_rphy_##name, NULL)
  645. #define sas_rphy_show_protocol(field, name) \
  646. static ssize_t \
  647. show_sas_rphy_##name(struct class_device *cdev, char *buf) \
  648. { \
  649. struct sas_rphy *rphy = transport_class_to_rphy(cdev); \
  650. \
  651. if (!rphy->field) \
  652. return snprintf(buf, 20, "none\n"); \
  653. return get_sas_protocol_names(rphy->field, buf); \
  654. }
  655. #define sas_rphy_protocol_attr(field, name) \
  656. sas_rphy_show_protocol(field, name) \
  657. static SAS_CLASS_DEVICE_ATTR(rphy, name, S_IRUGO, \
  658. show_sas_rphy_##name, NULL)
  659. static ssize_t
  660. show_sas_rphy_device_type(struct class_device *cdev, char *buf)
  661. {
  662. struct sas_rphy *rphy = transport_class_to_rphy(cdev);
  663. if (!rphy->identify.device_type)
  664. return snprintf(buf, 20, "none\n");
  665. return get_sas_device_type_names(
  666. rphy->identify.device_type, buf);
  667. }
  668. static SAS_CLASS_DEVICE_ATTR(rphy, device_type, S_IRUGO,
  669. show_sas_rphy_device_type, NULL);
  670. static ssize_t
  671. show_sas_rphy_enclosure_identifier(struct class_device *cdev, char *buf)
  672. {
  673. struct sas_rphy *rphy = transport_class_to_rphy(cdev);
  674. struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
  675. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
  676. struct sas_internal *i = to_sas_internal(shost->transportt);
  677. u64 identifier;
  678. int error;
  679. /*
  680. * Only devices behind an expander are supported, because the
  681. * enclosure identifier is a SMP feature.
  682. */
  683. if (phy->local_attached)
  684. return -EINVAL;
  685. error = i->f->get_enclosure_identifier(rphy, &identifier);
  686. if (error)
  687. return error;
  688. return sprintf(buf, "0x%llx\n", (unsigned long long)identifier);
  689. }
  690. static SAS_CLASS_DEVICE_ATTR(rphy, enclosure_identifier, S_IRUGO,
  691. show_sas_rphy_enclosure_identifier, NULL);
  692. static ssize_t
  693. show_sas_rphy_bay_identifier(struct class_device *cdev, char *buf)
  694. {
  695. struct sas_rphy *rphy = transport_class_to_rphy(cdev);
  696. struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
  697. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
  698. struct sas_internal *i = to_sas_internal(shost->transportt);
  699. int val;
  700. if (phy->local_attached)
  701. return -EINVAL;
  702. val = i->f->get_bay_identifier(rphy);
  703. if (val < 0)
  704. return val;
  705. return sprintf(buf, "%d\n", val);
  706. }
  707. static SAS_CLASS_DEVICE_ATTR(rphy, bay_identifier, S_IRUGO,
  708. show_sas_rphy_bay_identifier, NULL);
  709. sas_rphy_protocol_attr(identify.initiator_port_protocols,
  710. initiator_port_protocols);
  711. sas_rphy_protocol_attr(identify.target_port_protocols, target_port_protocols);
  712. sas_rphy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
  713. unsigned long long);
  714. sas_rphy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
  715. /* only need 8 bytes of data plus header (4 or 8) */
  716. #define BUF_SIZE 64
  717. int sas_read_port_mode_page(struct scsi_device *sdev)
  718. {
  719. char *buffer = kzalloc(BUF_SIZE, GFP_KERNEL), *msdata;
  720. struct sas_rphy *rphy = target_to_rphy(sdev->sdev_target);
  721. struct sas_end_device *rdev;
  722. struct scsi_mode_data mode_data;
  723. int res, error;
  724. BUG_ON(rphy->identify.device_type != SAS_END_DEVICE);
  725. rdev = rphy_to_end_device(rphy);
  726. if (!buffer)
  727. return -ENOMEM;
  728. res = scsi_mode_sense(sdev, 1, 0x19, buffer, BUF_SIZE, 30*HZ, 3,
  729. &mode_data, NULL);
  730. error = -EINVAL;
  731. if (!scsi_status_is_good(res))
  732. goto out;
  733. msdata = buffer + mode_data.header_length +
  734. mode_data.block_descriptor_length;
  735. if (msdata - buffer > BUF_SIZE - 8)
  736. goto out;
  737. error = 0;
  738. rdev->ready_led_meaning = msdata[2] & 0x10 ? 1 : 0;
  739. rdev->I_T_nexus_loss_timeout = (msdata[4] << 8) + msdata[5];
  740. rdev->initiator_response_timeout = (msdata[6] << 8) + msdata[7];
  741. out:
  742. kfree(buffer);
  743. return error;
  744. }
  745. EXPORT_SYMBOL(sas_read_port_mode_page);
  746. static DECLARE_TRANSPORT_CLASS(sas_end_dev_class,
  747. "sas_end_device", NULL, NULL, NULL);
  748. #define sas_end_dev_show_simple(field, name, format_string, cast) \
  749. static ssize_t \
  750. show_sas_end_dev_##name(struct class_device *cdev, char *buf) \
  751. { \
  752. struct sas_rphy *rphy = transport_class_to_rphy(cdev); \
  753. struct sas_end_device *rdev = rphy_to_end_device(rphy); \
  754. \
  755. return snprintf(buf, 20, format_string, cast rdev->field); \
  756. }
  757. #define sas_end_dev_simple_attr(field, name, format_string, type) \
  758. sas_end_dev_show_simple(field, name, format_string, (type)) \
  759. static SAS_CLASS_DEVICE_ATTR(end_dev, name, S_IRUGO, \
  760. show_sas_end_dev_##name, NULL)
  761. sas_end_dev_simple_attr(ready_led_meaning, ready_led_meaning, "%d\n", int);
  762. sas_end_dev_simple_attr(I_T_nexus_loss_timeout, I_T_nexus_loss_timeout,
  763. "%d\n", int);
  764. sas_end_dev_simple_attr(initiator_response_timeout, initiator_response_timeout,
  765. "%d\n", int);
  766. static DECLARE_TRANSPORT_CLASS(sas_expander_class,
  767. "sas_expander", NULL, NULL, NULL);
  768. #define sas_expander_show_simple(field, name, format_string, cast) \
  769. static ssize_t \
  770. show_sas_expander_##name(struct class_device *cdev, char *buf) \
  771. { \
  772. struct sas_rphy *rphy = transport_class_to_rphy(cdev); \
  773. struct sas_expander_device *edev = rphy_to_expander_device(rphy); \
  774. \
  775. return snprintf(buf, 20, format_string, cast edev->field); \
  776. }
  777. #define sas_expander_simple_attr(field, name, format_string, type) \
  778. sas_expander_show_simple(field, name, format_string, (type)) \
  779. static SAS_CLASS_DEVICE_ATTR(expander, name, S_IRUGO, \
  780. show_sas_expander_##name, NULL)
  781. sas_expander_simple_attr(vendor_id, vendor_id, "%s\n", char *);
  782. sas_expander_simple_attr(product_id, product_id, "%s\n", char *);
  783. sas_expander_simple_attr(product_rev, product_rev, "%s\n", char *);
  784. sas_expander_simple_attr(component_vendor_id, component_vendor_id,
  785. "%s\n", char *);
  786. sas_expander_simple_attr(component_id, component_id, "%u\n", unsigned int);
  787. sas_expander_simple_attr(component_revision_id, component_revision_id, "%u\n",
  788. unsigned int);
  789. sas_expander_simple_attr(level, level, "%d\n", int);
  790. static DECLARE_TRANSPORT_CLASS(sas_rphy_class,
  791. "sas_device", NULL, NULL, NULL);
  792. static int sas_rphy_match(struct attribute_container *cont, struct device *dev)
  793. {
  794. struct Scsi_Host *shost;
  795. struct sas_internal *i;
  796. if (!scsi_is_sas_rphy(dev))
  797. return 0;
  798. shost = dev_to_shost(dev->parent->parent);
  799. if (!shost->transportt)
  800. return 0;
  801. if (shost->transportt->host_attrs.ac.class !=
  802. &sas_host_class.class)
  803. return 0;
  804. i = to_sas_internal(shost->transportt);
  805. return &i->rphy_attr_cont.ac == cont;
  806. }
  807. static int sas_end_dev_match(struct attribute_container *cont,
  808. struct device *dev)
  809. {
  810. struct Scsi_Host *shost;
  811. struct sas_internal *i;
  812. struct sas_rphy *rphy;
  813. if (!scsi_is_sas_rphy(dev))
  814. return 0;
  815. shost = dev_to_shost(dev->parent->parent);
  816. rphy = dev_to_rphy(dev);
  817. if (!shost->transportt)
  818. return 0;
  819. if (shost->transportt->host_attrs.ac.class !=
  820. &sas_host_class.class)
  821. return 0;
  822. i = to_sas_internal(shost->transportt);
  823. return &i->end_dev_attr_cont.ac == cont &&
  824. rphy->identify.device_type == SAS_END_DEVICE;
  825. }
  826. static int sas_expander_match(struct attribute_container *cont,
  827. struct device *dev)
  828. {
  829. struct Scsi_Host *shost;
  830. struct sas_internal *i;
  831. struct sas_rphy *rphy;
  832. if (!scsi_is_sas_rphy(dev))
  833. return 0;
  834. shost = dev_to_shost(dev->parent->parent);
  835. rphy = dev_to_rphy(dev);
  836. if (!shost->transportt)
  837. return 0;
  838. if (shost->transportt->host_attrs.ac.class !=
  839. &sas_host_class.class)
  840. return 0;
  841. i = to_sas_internal(shost->transportt);
  842. return &i->expander_attr_cont.ac == cont &&
  843. (rphy->identify.device_type == SAS_EDGE_EXPANDER_DEVICE ||
  844. rphy->identify.device_type == SAS_FANOUT_EXPANDER_DEVICE);
  845. }
  846. static void sas_expander_release(struct device *dev)
  847. {
  848. struct sas_rphy *rphy = dev_to_rphy(dev);
  849. struct sas_expander_device *edev = rphy_to_expander_device(rphy);
  850. put_device(dev->parent);
  851. kfree(edev);
  852. }
  853. static void sas_end_device_release(struct device *dev)
  854. {
  855. struct sas_rphy *rphy = dev_to_rphy(dev);
  856. struct sas_end_device *edev = rphy_to_end_device(rphy);
  857. put_device(dev->parent);
  858. kfree(edev);
  859. }
  860. /**
  861. * sas_rphy_initialize - common rphy intialization
  862. * @rphy: rphy to initialise
  863. *
  864. * Used by both sas_end_device_alloc() and sas_expander_alloc() to
  865. * initialise the common rphy component of each.
  866. */
  867. static void sas_rphy_initialize(struct sas_rphy *rphy)
  868. {
  869. INIT_LIST_HEAD(&rphy->list);
  870. }
  871. /**
  872. * sas_end_device_alloc - allocate an rphy for an end device
  873. *
  874. * Allocates an SAS remote PHY structure, connected to @parent.
  875. *
  876. * Returns:
  877. * SAS PHY allocated or %NULL if the allocation failed.
  878. */
  879. struct sas_rphy *sas_end_device_alloc(struct sas_port *parent)
  880. {
  881. struct Scsi_Host *shost = dev_to_shost(&parent->dev);
  882. struct sas_end_device *rdev;
  883. rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
  884. if (!rdev) {
  885. return NULL;
  886. }
  887. device_initialize(&rdev->rphy.dev);
  888. rdev->rphy.dev.parent = get_device(&parent->dev);
  889. rdev->rphy.dev.release = sas_end_device_release;
  890. if (scsi_is_sas_expander_device(parent->dev.parent)) {
  891. struct sas_rphy *rphy = dev_to_rphy(parent->dev.parent);
  892. sprintf(rdev->rphy.dev.bus_id, "end_device-%d:%d:%d",
  893. shost->host_no, rphy->scsi_target_id, parent->port_identifier);
  894. } else
  895. sprintf(rdev->rphy.dev.bus_id, "end_device-%d:%d",
  896. shost->host_no, parent->port_identifier);
  897. rdev->rphy.identify.device_type = SAS_END_DEVICE;
  898. sas_rphy_initialize(&rdev->rphy);
  899. transport_setup_device(&rdev->rphy.dev);
  900. return &rdev->rphy;
  901. }
  902. EXPORT_SYMBOL(sas_end_device_alloc);
  903. /**
  904. * sas_expander_alloc - allocate an rphy for an end device
  905. *
  906. * Allocates an SAS remote PHY structure, connected to @parent.
  907. *
  908. * Returns:
  909. * SAS PHY allocated or %NULL if the allocation failed.
  910. */
  911. struct sas_rphy *sas_expander_alloc(struct sas_port *parent,
  912. enum sas_device_type type)
  913. {
  914. struct Scsi_Host *shost = dev_to_shost(&parent->dev);
  915. struct sas_expander_device *rdev;
  916. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  917. BUG_ON(type != SAS_EDGE_EXPANDER_DEVICE &&
  918. type != SAS_FANOUT_EXPANDER_DEVICE);
  919. rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
  920. if (!rdev) {
  921. return NULL;
  922. }
  923. device_initialize(&rdev->rphy.dev);
  924. rdev->rphy.dev.parent = get_device(&parent->dev);
  925. rdev->rphy.dev.release = sas_expander_release;
  926. mutex_lock(&sas_host->lock);
  927. rdev->rphy.scsi_target_id = sas_host->next_expander_id++;
  928. mutex_unlock(&sas_host->lock);
  929. sprintf(rdev->rphy.dev.bus_id, "expander-%d:%d",
  930. shost->host_no, rdev->rphy.scsi_target_id);
  931. rdev->rphy.identify.device_type = type;
  932. sas_rphy_initialize(&rdev->rphy);
  933. transport_setup_device(&rdev->rphy.dev);
  934. return &rdev->rphy;
  935. }
  936. EXPORT_SYMBOL(sas_expander_alloc);
  937. /**
  938. * sas_rphy_add -- add a SAS remote PHY to the device hierachy
  939. * @rphy: The remote PHY to be added
  940. *
  941. * Publishes a SAS remote PHY to the rest of the system.
  942. */
  943. int sas_rphy_add(struct sas_rphy *rphy)
  944. {
  945. struct sas_port *parent = dev_to_sas_port(rphy->dev.parent);
  946. struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
  947. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  948. struct sas_identify *identify = &rphy->identify;
  949. int error;
  950. if (parent->rphy)
  951. return -ENXIO;
  952. parent->rphy = rphy;
  953. error = device_add(&rphy->dev);
  954. if (error)
  955. return error;
  956. transport_add_device(&rphy->dev);
  957. transport_configure_device(&rphy->dev);
  958. mutex_lock(&sas_host->lock);
  959. list_add_tail(&rphy->list, &sas_host->rphy_list);
  960. if (identify->device_type == SAS_END_DEVICE &&
  961. (identify->target_port_protocols &
  962. (SAS_PROTOCOL_SSP|SAS_PROTOCOL_STP|SAS_PROTOCOL_SATA)))
  963. rphy->scsi_target_id = sas_host->next_target_id++;
  964. else if (identify->device_type == SAS_END_DEVICE)
  965. rphy->scsi_target_id = -1;
  966. mutex_unlock(&sas_host->lock);
  967. if (identify->device_type == SAS_END_DEVICE &&
  968. rphy->scsi_target_id != -1) {
  969. scsi_scan_target(&rphy->dev, parent->port_identifier,
  970. rphy->scsi_target_id, ~0, 0);
  971. }
  972. return 0;
  973. }
  974. EXPORT_SYMBOL(sas_rphy_add);
  975. /**
  976. * sas_rphy_free -- free a SAS remote PHY
  977. * @rphy SAS remote PHY to free
  978. *
  979. * Frees the specified SAS remote PHY.
  980. *
  981. * Note:
  982. * This function must only be called on a remote
  983. * PHY that has not sucessfully been added using
  984. * sas_rphy_add().
  985. */
  986. void sas_rphy_free(struct sas_rphy *rphy)
  987. {
  988. struct device *dev = &rphy->dev;
  989. struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
  990. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  991. mutex_lock(&sas_host->lock);
  992. list_del(&rphy->list);
  993. mutex_unlock(&sas_host->lock);
  994. transport_destroy_device(dev);
  995. put_device(dev);
  996. }
  997. EXPORT_SYMBOL(sas_rphy_free);
  998. /**
  999. * sas_rphy_delete -- remove SAS remote PHY
  1000. * @rphy: SAS remote PHY to remove
  1001. *
  1002. * Removes the specified SAS remote PHY.
  1003. */
  1004. void
  1005. sas_rphy_delete(struct sas_rphy *rphy)
  1006. {
  1007. struct device *dev = &rphy->dev;
  1008. struct sas_port *parent = dev_to_sas_port(dev->parent);
  1009. struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
  1010. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  1011. switch (rphy->identify.device_type) {
  1012. case SAS_END_DEVICE:
  1013. scsi_remove_target(dev);
  1014. break;
  1015. case SAS_EDGE_EXPANDER_DEVICE:
  1016. case SAS_FANOUT_EXPANDER_DEVICE:
  1017. sas_remove_children(dev);
  1018. break;
  1019. default:
  1020. break;
  1021. }
  1022. transport_remove_device(dev);
  1023. device_del(dev);
  1024. transport_destroy_device(dev);
  1025. mutex_lock(&sas_host->lock);
  1026. list_del(&rphy->list);
  1027. mutex_unlock(&sas_host->lock);
  1028. parent->rphy = NULL;
  1029. put_device(dev);
  1030. }
  1031. EXPORT_SYMBOL(sas_rphy_delete);
  1032. /**
  1033. * scsi_is_sas_rphy -- check if a struct device represents a SAS remote PHY
  1034. * @dev: device to check
  1035. *
  1036. * Returns:
  1037. * %1 if the device represents a SAS remote PHY, %0 else
  1038. */
  1039. int scsi_is_sas_rphy(const struct device *dev)
  1040. {
  1041. return dev->release == sas_end_device_release ||
  1042. dev->release == sas_expander_release;
  1043. }
  1044. EXPORT_SYMBOL(scsi_is_sas_rphy);
  1045. /*
  1046. * SCSI scan helper
  1047. */
  1048. static int sas_user_scan(struct Scsi_Host *shost, uint channel,
  1049. uint id, uint lun)
  1050. {
  1051. struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
  1052. struct sas_rphy *rphy;
  1053. mutex_lock(&sas_host->lock);
  1054. list_for_each_entry(rphy, &sas_host->rphy_list, list) {
  1055. struct sas_port *parent = dev_to_sas_port(rphy->dev.parent);
  1056. if (rphy->identify.device_type != SAS_END_DEVICE ||
  1057. rphy->scsi_target_id == -1)
  1058. continue;
  1059. if ((channel == SCAN_WILD_CARD || channel == parent->port_identifier) &&
  1060. (id == SCAN_WILD_CARD || id == rphy->scsi_target_id)) {
  1061. scsi_scan_target(&rphy->dev, parent->port_identifier,
  1062. rphy->scsi_target_id, lun, 1);
  1063. }
  1064. }
  1065. mutex_unlock(&sas_host->lock);
  1066. return 0;
  1067. }
  1068. /*
  1069. * Setup / Teardown code
  1070. */
  1071. #define SETUP_TEMPLATE(attrb, field, perm, test) \
  1072. i->private_##attrb[count] = class_device_attr_##field; \
  1073. i->private_##attrb[count].attr.mode = perm; \
  1074. i->attrb[count] = &i->private_##attrb[count]; \
  1075. if (test) \
  1076. count++
  1077. #define SETUP_RPORT_ATTRIBUTE(field) \
  1078. SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, 1)
  1079. #define SETUP_OPTIONAL_RPORT_ATTRIBUTE(field, func) \
  1080. SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, i->f->func)
  1081. #define SETUP_PHY_ATTRIBUTE(field) \
  1082. SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, 1)
  1083. #define SETUP_PORT_ATTRIBUTE(field) \
  1084. SETUP_TEMPLATE(port_attrs, field, S_IRUGO, 1)
  1085. #define SETUP_OPTIONAL_PHY_ATTRIBUTE(field, func) \
  1086. SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, i->f->func)
  1087. #define SETUP_PHY_ATTRIBUTE_WRONLY(field) \
  1088. SETUP_TEMPLATE(phy_attrs, field, S_IWUGO, 1)
  1089. #define SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(field, func) \
  1090. SETUP_TEMPLATE(phy_attrs, field, S_IWUGO, i->f->func)
  1091. #define SETUP_END_DEV_ATTRIBUTE(field) \
  1092. SETUP_TEMPLATE(end_dev_attrs, field, S_IRUGO, 1)
  1093. #define SETUP_EXPANDER_ATTRIBUTE(field) \
  1094. SETUP_TEMPLATE(expander_attrs, expander_##field, S_IRUGO, 1)
  1095. /**
  1096. * sas_attach_transport -- instantiate SAS transport template
  1097. * @ft: SAS transport class function template
  1098. */
  1099. struct scsi_transport_template *
  1100. sas_attach_transport(struct sas_function_template *ft)
  1101. {
  1102. struct sas_internal *i;
  1103. int count;
  1104. i = kzalloc(sizeof(struct sas_internal), GFP_KERNEL);
  1105. if (!i)
  1106. return NULL;
  1107. i->t.user_scan = sas_user_scan;
  1108. i->t.host_attrs.ac.attrs = &i->host_attrs[0];
  1109. i->t.host_attrs.ac.class = &sas_host_class.class;
  1110. i->t.host_attrs.ac.match = sas_host_match;
  1111. transport_container_register(&i->t.host_attrs);
  1112. i->t.host_size = sizeof(struct sas_host_attrs);
  1113. i->phy_attr_cont.ac.class = &sas_phy_class.class;
  1114. i->phy_attr_cont.ac.attrs = &i->phy_attrs[0];
  1115. i->phy_attr_cont.ac.match = sas_phy_match;
  1116. transport_container_register(&i->phy_attr_cont);
  1117. i->port_attr_cont.ac.class = &sas_port_class.class;
  1118. i->port_attr_cont.ac.attrs = &i->port_attrs[0];
  1119. i->port_attr_cont.ac.match = sas_port_match;
  1120. transport_container_register(&i->port_attr_cont);
  1121. i->rphy_attr_cont.ac.class = &sas_rphy_class.class;
  1122. i->rphy_attr_cont.ac.attrs = &i->rphy_attrs[0];
  1123. i->rphy_attr_cont.ac.match = sas_rphy_match;
  1124. transport_container_register(&i->rphy_attr_cont);
  1125. i->end_dev_attr_cont.ac.class = &sas_end_dev_class.class;
  1126. i->end_dev_attr_cont.ac.attrs = &i->end_dev_attrs[0];
  1127. i->end_dev_attr_cont.ac.match = sas_end_dev_match;
  1128. transport_container_register(&i->end_dev_attr_cont);
  1129. i->expander_attr_cont.ac.class = &sas_expander_class.class;
  1130. i->expander_attr_cont.ac.attrs = &i->expander_attrs[0];
  1131. i->expander_attr_cont.ac.match = sas_expander_match;
  1132. transport_container_register(&i->expander_attr_cont);
  1133. i->f = ft;
  1134. count = 0;
  1135. SETUP_PORT_ATTRIBUTE(num_phys);
  1136. i->host_attrs[count] = NULL;
  1137. count = 0;
  1138. SETUP_PHY_ATTRIBUTE(initiator_port_protocols);
  1139. SETUP_PHY_ATTRIBUTE(target_port_protocols);
  1140. SETUP_PHY_ATTRIBUTE(device_type);
  1141. SETUP_PHY_ATTRIBUTE(sas_address);
  1142. SETUP_PHY_ATTRIBUTE(phy_identifier);
  1143. //SETUP_PHY_ATTRIBUTE(port_identifier);
  1144. SETUP_PHY_ATTRIBUTE(negotiated_linkrate);
  1145. SETUP_PHY_ATTRIBUTE(minimum_linkrate_hw);
  1146. SETUP_PHY_ATTRIBUTE(minimum_linkrate);
  1147. SETUP_PHY_ATTRIBUTE(maximum_linkrate_hw);
  1148. SETUP_PHY_ATTRIBUTE(maximum_linkrate);
  1149. SETUP_PHY_ATTRIBUTE(invalid_dword_count);
  1150. SETUP_PHY_ATTRIBUTE(running_disparity_error_count);
  1151. SETUP_PHY_ATTRIBUTE(loss_of_dword_sync_count);
  1152. SETUP_PHY_ATTRIBUTE(phy_reset_problem_count);
  1153. SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(link_reset, phy_reset);
  1154. SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(hard_reset, phy_reset);
  1155. i->phy_attrs[count] = NULL;
  1156. count = 0;
  1157. SETUP_PORT_ATTRIBUTE(num_phys);
  1158. i->port_attrs[count] = NULL;
  1159. count = 0;
  1160. SETUP_RPORT_ATTRIBUTE(rphy_initiator_port_protocols);
  1161. SETUP_RPORT_ATTRIBUTE(rphy_target_port_protocols);
  1162. SETUP_RPORT_ATTRIBUTE(rphy_device_type);
  1163. SETUP_RPORT_ATTRIBUTE(rphy_sas_address);
  1164. SETUP_RPORT_ATTRIBUTE(rphy_phy_identifier);
  1165. SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_enclosure_identifier,
  1166. get_enclosure_identifier);
  1167. SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_bay_identifier,
  1168. get_bay_identifier);
  1169. i->rphy_attrs[count] = NULL;
  1170. count = 0;
  1171. SETUP_END_DEV_ATTRIBUTE(end_dev_ready_led_meaning);
  1172. SETUP_END_DEV_ATTRIBUTE(end_dev_I_T_nexus_loss_timeout);
  1173. SETUP_END_DEV_ATTRIBUTE(end_dev_initiator_response_timeout);
  1174. i->end_dev_attrs[count] = NULL;
  1175. count = 0;
  1176. SETUP_EXPANDER_ATTRIBUTE(vendor_id);
  1177. SETUP_EXPANDER_ATTRIBUTE(product_id);
  1178. SETUP_EXPANDER_ATTRIBUTE(product_rev);
  1179. SETUP_EXPANDER_ATTRIBUTE(component_vendor_id);
  1180. SETUP_EXPANDER_ATTRIBUTE(component_id);
  1181. SETUP_EXPANDER_ATTRIBUTE(component_revision_id);
  1182. SETUP_EXPANDER_ATTRIBUTE(level);
  1183. i->expander_attrs[count] = NULL;
  1184. return &i->t;
  1185. }
  1186. EXPORT_SYMBOL(sas_attach_transport);
  1187. /**
  1188. * sas_release_transport -- release SAS transport template instance
  1189. * @t: transport template instance
  1190. */
  1191. void sas_release_transport(struct scsi_transport_template *t)
  1192. {
  1193. struct sas_internal *i = to_sas_internal(t);
  1194. transport_container_unregister(&i->t.host_attrs);
  1195. transport_container_unregister(&i->phy_attr_cont);
  1196. transport_container_unregister(&i->port_attr_cont);
  1197. transport_container_unregister(&i->rphy_attr_cont);
  1198. transport_container_unregister(&i->end_dev_attr_cont);
  1199. transport_container_unregister(&i->expander_attr_cont);
  1200. kfree(i);
  1201. }
  1202. EXPORT_SYMBOL(sas_release_transport);
  1203. static __init int sas_transport_init(void)
  1204. {
  1205. int error;
  1206. error = transport_class_register(&sas_host_class);
  1207. if (error)
  1208. goto out;
  1209. error = transport_class_register(&sas_phy_class);
  1210. if (error)
  1211. goto out_unregister_transport;
  1212. error = transport_class_register(&sas_port_class);
  1213. if (error)
  1214. goto out_unregister_phy;
  1215. error = transport_class_register(&sas_rphy_class);
  1216. if (error)
  1217. goto out_unregister_port;
  1218. error = transport_class_register(&sas_end_dev_class);
  1219. if (error)
  1220. goto out_unregister_rphy;
  1221. error = transport_class_register(&sas_expander_class);
  1222. if (error)
  1223. goto out_unregister_end_dev;
  1224. return 0;
  1225. out_unregister_end_dev:
  1226. transport_class_unregister(&sas_end_dev_class);
  1227. out_unregister_rphy:
  1228. transport_class_unregister(&sas_rphy_class);
  1229. out_unregister_port:
  1230. transport_class_unregister(&sas_port_class);
  1231. out_unregister_phy:
  1232. transport_class_unregister(&sas_phy_class);
  1233. out_unregister_transport:
  1234. transport_class_unregister(&sas_host_class);
  1235. out:
  1236. return error;
  1237. }
  1238. static void __exit sas_transport_exit(void)
  1239. {
  1240. transport_class_unregister(&sas_host_class);
  1241. transport_class_unregister(&sas_phy_class);
  1242. transport_class_unregister(&sas_port_class);
  1243. transport_class_unregister(&sas_rphy_class);
  1244. transport_class_unregister(&sas_end_dev_class);
  1245. transport_class_unregister(&sas_expander_class);
  1246. }
  1247. MODULE_AUTHOR("Christoph Hellwig");
  1248. MODULE_DESCRIPTION("SAS Transphy Attributes");
  1249. MODULE_LICENSE("GPL");
  1250. module_init(sas_transport_init);
  1251. module_exit(sas_transport_exit);