1 /*
2  * Serial Attached SCSI (SAS) class internal header file
3  *
4  * Copyright (C) 2005 Adaptec, Inc.  All rights reserved.
5  * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
6  *
7  * This file is licensed under GPLv2.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 2 of the
12  * License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22  * USA
23  *
24  */
25 
26 #ifndef _SAS_INTERNAL_H_
27 #define _SAS_INTERNAL_H_
28 
29 #include <scsi/scsi.h>
30 #include <scsi/scsi_host.h>
31 #include <scsi/scsi_transport_sas.h>
32 #include <scsi/libsas.h>
33 #include <scsi/sas_ata.h>
34 
35 #ifdef pr_fmt
36 #undef pr_fmt
37 #endif
38 
39 #define SAS_FMT "sas: "
40 
41 #define pr_fmt(fmt) SAS_FMT fmt
42 
43 #define TO_SAS_TASK(_scsi_cmd)  ((void *)(_scsi_cmd)->host_scribble)
44 #define ASSIGN_SAS_TASK(_sc, _t) do { (_sc)->host_scribble = (void *) _t; } while (0)
45 
46 struct sas_phy_data {
47 	/* let reset be performed in sas_queue_work() context */
48 	struct sas_phy *phy;
49 	struct mutex event_lock;
50 	int hard_reset;
51 	int reset_result;
52 	struct sas_work reset_work;
53 	int enable;
54 	int enable_result;
55 	struct sas_work enable_work;
56 };
57 
58 void sas_scsi_recover_host(struct Scsi_Host *shost);
59 
60 int sas_show_class(enum sas_class class, char *buf);
61 int sas_show_proto(enum sas_protocol proto, char *buf);
62 int sas_show_linkrate(enum sas_linkrate linkrate, char *buf);
63 int sas_show_oob_mode(enum sas_oob_mode oob_mode, char *buf);
64 
65 int  sas_register_phys(struct sas_ha_struct *sas_ha);
66 void sas_unregister_phys(struct sas_ha_struct *sas_ha);
67 
68 struct asd_sas_event *sas_alloc_event(struct asd_sas_phy *phy);
69 void sas_free_event(struct asd_sas_event *event);
70 
71 int  sas_register_ports(struct sas_ha_struct *sas_ha);
72 void sas_unregister_ports(struct sas_ha_struct *sas_ha);
73 
74 int  sas_init_events(struct sas_ha_struct *sas_ha);
75 void sas_disable_revalidation(struct sas_ha_struct *ha);
76 void sas_enable_revalidation(struct sas_ha_struct *ha);
77 void __sas_drain_work(struct sas_ha_struct *ha);
78 
79 void sas_deform_port(struct asd_sas_phy *phy, int gone);
80 
81 void sas_porte_bytes_dmaed(struct work_struct *work);
82 void sas_porte_broadcast_rcvd(struct work_struct *work);
83 void sas_porte_link_reset_err(struct work_struct *work);
84 void sas_porte_timer_event(struct work_struct *work);
85 void sas_porte_hard_reset(struct work_struct *work);
86 int sas_queue_work(struct sas_ha_struct *ha, struct sas_work *sw);
87 
88 int sas_notify_lldd_dev_found(struct domain_device *);
89 void sas_notify_lldd_dev_gone(struct domain_device *);
90 
91 void sas_smp_handler(struct bsg_job *job, struct Scsi_Host *shost,
92 		struct sas_rphy *rphy);
93 int sas_smp_phy_control(struct domain_device *dev, int phy_id,
94 			enum phy_func phy_func, struct sas_phy_linkrates *);
95 int sas_smp_get_phy_events(struct sas_phy *phy);
96 
97 int sas_notify_phy_event(struct asd_sas_phy *phy, enum phy_event event);
98 void sas_device_set_phy(struct domain_device *dev, struct sas_port *port);
99 struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy);
100 struct domain_device *sas_ex_to_ata(struct domain_device *ex_dev, int phy_id);
101 int sas_ex_phy_discover(struct domain_device *dev, int single);
102 int sas_get_report_phy_sata(struct domain_device *dev, int phy_id,
103 			    struct smp_resp *rps_resp);
104 int sas_try_ata_reset(struct asd_sas_phy *phy);
105 void sas_hae_reset(struct work_struct *work);
106 
107 void sas_free_device(struct kref *kref);
108 void sas_destruct_devices(struct asd_sas_port *port);
109 
110 extern const work_func_t sas_phy_event_fns[PHY_NUM_EVENTS];
111 extern const work_func_t sas_port_event_fns[PORT_NUM_EVENTS];
112 
113 #ifdef CONFIG_SCSI_SAS_HOST_SMP
114 extern void sas_smp_host_handler(struct bsg_job *job, struct Scsi_Host *shost);
115 #else
116 static inline void sas_smp_host_handler(struct bsg_job *job,
117 		struct Scsi_Host *shost)
118 {
119 	shost_printk(KERN_ERR, shost,
120 		"Cannot send SMP to a sas host (not enabled in CONFIG)\n");
121 	bsg_job_done(job, -EINVAL, 0);
122 }
123 #endif
124 
125 static inline void sas_fail_probe(struct domain_device *dev, const char *func, int err)
126 {
127 	pr_warn("%s: for %s device %16llx returned %d\n",
128 		func, dev->parent ? "exp-attached" :
129 		"direct-attached",
130 		SAS_ADDR(dev->sas_addr), err);
131 	sas_unregister_dev(dev->port, dev);
132 }
133 
134 static inline void sas_fill_in_rphy(struct domain_device *dev,
135 				    struct sas_rphy *rphy)
136 {
137 	rphy->identify.sas_address = SAS_ADDR(dev->sas_addr);
138 	rphy->identify.initiator_port_protocols = dev->iproto;
139 	rphy->identify.target_port_protocols = dev->tproto;
140 	switch (dev->dev_type) {
141 	case SAS_SATA_DEV:
142 		/* FIXME: need sata device type */
143 	case SAS_END_DEVICE:
144 	case SAS_SATA_PENDING:
145 		rphy->identify.device_type = SAS_END_DEVICE;
146 		break;
147 	case SAS_EDGE_EXPANDER_DEVICE:
148 		rphy->identify.device_type = SAS_EDGE_EXPANDER_DEVICE;
149 		break;
150 	case SAS_FANOUT_EXPANDER_DEVICE:
151 		rphy->identify.device_type = SAS_FANOUT_EXPANDER_DEVICE;
152 		break;
153 	default:
154 		rphy->identify.device_type = SAS_PHY_UNUSED;
155 		break;
156 	}
157 }
158 
159 static inline void sas_phy_set_target(struct asd_sas_phy *p, struct domain_device *dev)
160 {
161 	struct sas_phy *phy = p->phy;
162 
163 	if (dev) {
164 		if (dev_is_sata(dev))
165 			phy->identify.device_type = SAS_END_DEVICE;
166 		else
167 			phy->identify.device_type = dev->dev_type;
168 		phy->identify.target_port_protocols = dev->tproto;
169 	} else {
170 		phy->identify.device_type = SAS_PHY_UNUSED;
171 		phy->identify.target_port_protocols = 0;
172 	}
173 }
174 
175 static inline void sas_add_parent_port(struct domain_device *dev, int phy_id)
176 {
177 	struct expander_device *ex = &dev->ex_dev;
178 	struct ex_phy *ex_phy = &ex->ex_phy[phy_id];
179 
180 	if (!ex->parent_port) {
181 		ex->parent_port = sas_port_alloc(&dev->rphy->dev, phy_id);
182 		/* FIXME: error handling */
183 		BUG_ON(!ex->parent_port);
184 		BUG_ON(sas_port_add(ex->parent_port));
185 		sas_port_mark_backlink(ex->parent_port);
186 	}
187 	sas_port_add_phy(ex->parent_port, ex_phy->phy);
188 }
189 
190 static inline struct domain_device *sas_alloc_device(void)
191 {
192 	struct domain_device *dev = kzalloc(sizeof(*dev), GFP_KERNEL);
193 
194 	if (dev) {
195 		INIT_LIST_HEAD(&dev->siblings);
196 		INIT_LIST_HEAD(&dev->dev_list_node);
197 		INIT_LIST_HEAD(&dev->disco_list_node);
198 		kref_init(&dev->kref);
199 		spin_lock_init(&dev->done_lock);
200 	}
201 	return dev;
202 }
203 
204 static inline void sas_put_device(struct domain_device *dev)
205 {
206 	kref_put(&dev->kref, sas_free_device);
207 }
208 
209 #endif /* _SAS_INTERNAL_H_ */
210