sas_internal.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Serial Attached SCSI (SAS) class internal header file
  4. *
  5. * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
  6. * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
  7. */
  8. #ifndef _SAS_INTERNAL_H_
  9. #define _SAS_INTERNAL_H_
  10. #include <scsi/scsi.h>
  11. #include <scsi/scsi_host.h>
  12. #include <scsi/scsi_transport_sas.h>
  13. #include <scsi/libsas.h>
  14. #include <scsi/sas_ata.h>
  15. #include <linux/pm_runtime.h>
  16. #ifdef pr_fmt
  17. #undef pr_fmt
  18. #endif
  19. #define SAS_FMT "sas: "
  20. #define pr_fmt(fmt) SAS_FMT fmt
  21. #define TO_SAS_TASK(_scsi_cmd) ((void *)(_scsi_cmd)->host_scribble)
  22. #define ASSIGN_SAS_TASK(_sc, _t) do { (_sc)->host_scribble = (void *) _t; } while (0)
  23. struct sas_phy_data {
  24. /* let reset be performed in sas_queue_work() context */
  25. struct sas_phy *phy;
  26. struct mutex event_lock;
  27. int hard_reset;
  28. int reset_result;
  29. struct sas_work reset_work;
  30. int enable;
  31. int enable_result;
  32. struct sas_work enable_work;
  33. };
  34. void sas_hash_addr(u8 *hashed, const u8 *sas_addr);
  35. int sas_discover_root_expander(struct domain_device *dev);
  36. int sas_ex_revalidate_domain(struct domain_device *dev);
  37. void sas_unregister_domain_devices(struct asd_sas_port *port, int gone);
  38. void sas_init_disc(struct sas_discovery *disc, struct asd_sas_port *port);
  39. void sas_discover_event(struct asd_sas_port *port, enum discover_event ev);
  40. void sas_init_dev(struct domain_device *dev);
  41. void sas_unregister_dev(struct asd_sas_port *port, struct domain_device *dev);
  42. void sas_scsi_recover_host(struct Scsi_Host *shost);
  43. int sas_register_phys(struct sas_ha_struct *sas_ha);
  44. struct asd_sas_event *sas_alloc_event(struct asd_sas_phy *phy, gfp_t gfp_flags);
  45. void sas_free_event(struct asd_sas_event *event);
  46. struct sas_task *sas_alloc_task(gfp_t flags);
  47. struct sas_task *sas_alloc_slow_task(gfp_t flags);
  48. void sas_free_task(struct sas_task *task);
  49. int sas_register_ports(struct sas_ha_struct *sas_ha);
  50. void sas_unregister_ports(struct sas_ha_struct *sas_ha);
  51. void sas_disable_revalidation(struct sas_ha_struct *ha);
  52. void sas_enable_revalidation(struct sas_ha_struct *ha);
  53. void sas_queue_deferred_work(struct sas_ha_struct *ha);
  54. void __sas_drain_work(struct sas_ha_struct *ha);
  55. void sas_deform_port(struct asd_sas_phy *phy, int gone);
  56. void sas_porte_bytes_dmaed(struct work_struct *work);
  57. void sas_porte_broadcast_rcvd(struct work_struct *work);
  58. void sas_porte_link_reset_err(struct work_struct *work);
  59. void sas_porte_timer_event(struct work_struct *work);
  60. void sas_porte_hard_reset(struct work_struct *work);
  61. bool sas_queue_work(struct sas_ha_struct *ha, struct sas_work *sw);
  62. int sas_notify_lldd_dev_found(struct domain_device *);
  63. void sas_notify_lldd_dev_gone(struct domain_device *);
  64. void sas_smp_handler(struct bsg_job *job, struct Scsi_Host *shost,
  65. struct sas_rphy *rphy);
  66. int sas_smp_phy_control(struct domain_device *dev, int phy_id,
  67. enum phy_func phy_func, struct sas_phy_linkrates *);
  68. int sas_smp_get_phy_events(struct sas_phy *phy);
  69. void sas_device_set_phy(struct domain_device *dev, struct sas_port *port);
  70. struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy);
  71. struct domain_device *sas_ex_to_ata(struct domain_device *ex_dev, int phy_id);
  72. int sas_ex_phy_discover(struct domain_device *dev, int single);
  73. int sas_get_report_phy_sata(struct domain_device *dev, int phy_id,
  74. struct smp_rps_resp *rps_resp);
  75. int sas_get_phy_attached_dev(struct domain_device *dev, int phy_id,
  76. u8 *sas_addr, enum sas_device_type *type);
  77. int sas_try_ata_reset(struct asd_sas_phy *phy);
  78. void sas_free_device(struct kref *kref);
  79. void sas_destruct_devices(struct asd_sas_port *port);
  80. extern const work_func_t sas_phy_event_fns[PHY_NUM_EVENTS];
  81. extern const work_func_t sas_port_event_fns[PORT_NUM_EVENTS];
  82. void sas_task_internal_done(struct sas_task *task);
  83. void sas_task_internal_timedout(struct timer_list *t);
  84. int sas_execute_tmf(struct domain_device *device, void *parameter,
  85. int para_len, int force_phy_id,
  86. struct sas_tmf_task *tmf);
  87. #ifdef CONFIG_SCSI_SAS_HOST_SMP
  88. extern void sas_smp_host_handler(struct bsg_job *job, struct Scsi_Host *shost);
  89. #else
  90. static inline void sas_smp_host_handler(struct bsg_job *job,
  91. struct Scsi_Host *shost)
  92. {
  93. shost_printk(KERN_ERR, shost,
  94. "Cannot send SMP to a sas host (not enabled in CONFIG)\n");
  95. bsg_job_done(job, -EINVAL, 0);
  96. }
  97. #endif
  98. static inline bool sas_phy_match_dev_addr(struct domain_device *dev,
  99. struct ex_phy *phy)
  100. {
  101. return SAS_ADDR(dev->sas_addr) == SAS_ADDR(phy->attached_sas_addr);
  102. }
  103. static inline bool sas_phy_match_port_addr(struct asd_sas_port *port,
  104. struct ex_phy *phy)
  105. {
  106. return SAS_ADDR(port->sas_addr) == SAS_ADDR(phy->attached_sas_addr);
  107. }
  108. static inline bool sas_phy_addr_match(struct ex_phy *p1, struct ex_phy *p2)
  109. {
  110. return SAS_ADDR(p1->attached_sas_addr) == SAS_ADDR(p2->attached_sas_addr);
  111. }
  112. static inline void sas_fail_probe(struct domain_device *dev, const char *func, int err)
  113. {
  114. pr_warn("%s: for %s device %016llx returned %d\n",
  115. func, dev->parent ? "exp-attached" :
  116. "direct-attached",
  117. SAS_ADDR(dev->sas_addr), err);
  118. /*
  119. * If the device probe failed, the expander phy attached address
  120. * needs to be reset so that the phy will not be treated as flutter
  121. * in the next revalidation
  122. */
  123. if (dev->parent && !dev_is_expander(dev->dev_type)) {
  124. struct sas_phy *phy = dev->phy;
  125. struct domain_device *parent = dev->parent;
  126. struct ex_phy *ex_phy = &parent->ex_dev.ex_phy[phy->number];
  127. memset(ex_phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
  128. }
  129. sas_unregister_dev(dev->port, dev);
  130. }
  131. static inline void sas_fill_in_rphy(struct domain_device *dev,
  132. struct sas_rphy *rphy)
  133. {
  134. rphy->identify.sas_address = SAS_ADDR(dev->sas_addr);
  135. rphy->identify.initiator_port_protocols = dev->iproto;
  136. rphy->identify.target_port_protocols = dev->tproto;
  137. switch (dev->dev_type) {
  138. case SAS_SATA_DEV:
  139. /* FIXME: need sata device type */
  140. case SAS_END_DEVICE:
  141. case SAS_SATA_PENDING:
  142. rphy->identify.device_type = SAS_END_DEVICE;
  143. break;
  144. case SAS_EDGE_EXPANDER_DEVICE:
  145. rphy->identify.device_type = SAS_EDGE_EXPANDER_DEVICE;
  146. break;
  147. case SAS_FANOUT_EXPANDER_DEVICE:
  148. rphy->identify.device_type = SAS_FANOUT_EXPANDER_DEVICE;
  149. break;
  150. default:
  151. rphy->identify.device_type = SAS_PHY_UNUSED;
  152. break;
  153. }
  154. }
  155. static inline void sas_phy_set_target(struct asd_sas_phy *p, struct domain_device *dev)
  156. {
  157. struct sas_phy *phy = p->phy;
  158. if (dev) {
  159. if (dev_is_sata(dev))
  160. phy->identify.device_type = SAS_END_DEVICE;
  161. else
  162. phy->identify.device_type = dev->dev_type;
  163. phy->identify.target_port_protocols = dev->tproto;
  164. } else {
  165. phy->identify.device_type = SAS_PHY_UNUSED;
  166. phy->identify.target_port_protocols = 0;
  167. }
  168. }
  169. static inline struct domain_device *sas_alloc_device(void)
  170. {
  171. struct domain_device *dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  172. if (dev) {
  173. INIT_LIST_HEAD(&dev->siblings);
  174. INIT_LIST_HEAD(&dev->dev_list_node);
  175. INIT_LIST_HEAD(&dev->disco_list_node);
  176. kref_init(&dev->kref);
  177. spin_lock_init(&dev->done_lock);
  178. }
  179. return dev;
  180. }
  181. static inline void sas_put_device(struct domain_device *dev)
  182. {
  183. kref_put(&dev->kref, sas_free_device);
  184. }
  185. #endif /* _SAS_INTERNAL_H_ */