isci: uplevel phy infrastructure
[firefly-linux-kernel-4.4.55.git] / drivers / scsi / isci / port.c
1 /*
2  * This file is provided under a dual BSD/GPLv2 license.  When using or
3  * redistributing this file, you may do so under either license.
4  *
5  * GPL LICENSE SUMMARY
6  *
7  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of version 2 of the GNU General Public License as
11  * published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21  * The full GNU General Public License is included in this distribution
22  * in the file called LICENSE.GPL.
23  *
24  * BSD LICENSE
25  *
26  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
27  * All rights reserved.
28  *
29  * Redistribution and use in source and binary forms, with or without
30  * modification, are permitted provided that the following conditions
31  * are met:
32  *
33  *   * Redistributions of source code must retain the above copyright
34  *     notice, this list of conditions and the following disclaimer.
35  *   * Redistributions in binary form must reproduce the above copyright
36  *     notice, this list of conditions and the following disclaimer in
37  *     the documentation and/or other materials provided with the
38  *     distribution.
39  *   * Neither the name of Intel Corporation nor the names of its
40  *     contributors may be used to endorse or promote products derived
41  *     from this software without specific prior written permission.
42  *
43  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
44  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
45  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
46  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
47  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
53  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54  */
55
56 #include <linux/workqueue.h>
57 #include "isci.h"
58 #include "scic_port.h"
59 #include "port.h"
60 #include "request.h"
61
62 static void isci_port_change_state(struct isci_port *iport, enum isci_status status)
63 {
64         unsigned long flags;
65
66         dev_dbg(&iport->isci_host->pdev->dev,
67                 "%s: iport = %p, state = 0x%x\n",
68                 __func__, iport, status);
69
70         /* XXX pointless lock */
71         spin_lock_irqsave(&iport->state_lock, flags);
72         iport->status = status;
73         spin_unlock_irqrestore(&iport->state_lock, flags);
74 }
75
76 void isci_port_init(struct isci_port *iport, struct isci_host *ihost, int index)
77 {
78         INIT_LIST_HEAD(&iport->remote_dev_list);
79         INIT_LIST_HEAD(&iport->domain_dev_list);
80         spin_lock_init(&iport->state_lock);
81         init_completion(&iport->start_complete);
82         iport->isci_host = ihost;
83         isci_port_change_state(iport, isci_freed);
84 }
85
86 /**
87  * isci_port_get_state() - This function gets the status of the port object.
88  * @isci_port: This parameter points to the isci_port object
89  *
90  * status of the object as a isci_status enum.
91  */
92 enum isci_status isci_port_get_state(
93         struct isci_port *isci_port)
94 {
95         return isci_port->status;
96 }
97
98 void isci_port_bc_change_received(struct isci_host *ihost,
99                                   struct scic_sds_port *sci_port,
100                                   struct scic_sds_phy *sci_phy)
101 {
102         struct isci_phy *iphy = sci_phy_to_iphy(sci_phy);
103
104         dev_dbg(&ihost->pdev->dev, "%s: iphy = %p, sas_phy = %p\n",
105                 __func__, iphy, &iphy->sas_phy);
106
107         ihost->sas_ha.notify_port_event(&iphy->sas_phy, PORTE_BROADCAST_RCVD);
108         scic_port_enable_broadcast_change_notification(sci_port);
109 }
110
111 void isci_port_link_up(struct isci_host *isci_host,
112                        struct scic_sds_port *port,
113                        struct scic_sds_phy *phy)
114 {
115         unsigned long flags;
116         struct scic_port_properties properties;
117         struct isci_phy *isci_phy = sci_phy_to_iphy(phy);
118         struct isci_port *isci_port = sci_port_to_iport(port);
119         unsigned long success = true;
120
121         BUG_ON(isci_phy->isci_port != NULL);
122
123         isci_phy->isci_port = isci_port;
124
125         dev_dbg(&isci_host->pdev->dev,
126                 "%s: isci_port = %p\n",
127                 __func__, isci_port);
128
129         spin_lock_irqsave(&isci_phy->sas_phy.frame_rcvd_lock, flags);
130
131         isci_port_change_state(isci_phy->isci_port, isci_starting);
132
133         scic_port_get_properties(port, &properties);
134
135         if (phy->protocol == SCIC_SDS_PHY_PROTOCOL_SATA) {
136                 u64 attached_sas_address;
137
138                 isci_phy->sas_phy.oob_mode = SATA_OOB_MODE;
139                 isci_phy->sas_phy.frame_rcvd_size = sizeof(struct dev_to_host_fis);
140
141                 /*
142                  * For direct-attached SATA devices, the SCI core will
143                  * automagically assign a SAS address to the end device
144                  * for the purpose of creating a port. This SAS address
145                  * will not be the same as assigned to the PHY and needs
146                  * to be obtained from struct scic_port_properties properties.
147                  */
148                 attached_sas_address = properties.remote.sas_address.high;
149                 attached_sas_address <<= 32;
150                 attached_sas_address |= properties.remote.sas_address.low;
151                 swab64s(&attached_sas_address);
152
153                 memcpy(&isci_phy->sas_phy.attached_sas_addr,
154                        &attached_sas_address, sizeof(attached_sas_address));
155         } else if (phy->protocol == SCIC_SDS_PHY_PROTOCOL_SAS) {
156                 isci_phy->sas_phy.oob_mode = SAS_OOB_MODE;
157                 isci_phy->sas_phy.frame_rcvd_size = sizeof(struct sas_identify_frame);
158
159                 /* Copy the attached SAS address from the IAF */
160                 memcpy(isci_phy->sas_phy.attached_sas_addr,
161                        isci_phy->frame_rcvd.iaf.sas_addr, SAS_ADDR_SIZE);
162         } else {
163                 dev_err(&isci_host->pdev->dev, "%s: unkown target\n", __func__);
164                 success = false;
165         }
166
167         isci_phy->sas_phy.phy->negotiated_linkrate = sci_phy_linkrate(phy);
168
169         spin_unlock_irqrestore(&isci_phy->sas_phy.frame_rcvd_lock, flags);
170
171         /* Notify libsas that we have an address frame, if indeed
172          * we've found an SSP, SMP, or STP target */
173         if (success)
174                 isci_host->sas_ha.notify_port_event(&isci_phy->sas_phy,
175                                                     PORTE_BYTES_DMAED);
176 }
177
178
179 /**
180  * isci_port_link_down() - This function is called by the sci core when a link
181  *    becomes inactive.
182  * @isci_host: This parameter specifies the isci host object.
183  * @phy: This parameter specifies the isci phy with the active link.
184  * @port: This parameter specifies the isci port with the active link.
185  *
186  */
187 void isci_port_link_down(struct isci_host *isci_host, struct isci_phy *isci_phy,
188                          struct isci_port *isci_port)
189 {
190         struct isci_remote_device *isci_device;
191
192         dev_dbg(&isci_host->pdev->dev,
193                 "%s: isci_port = %p\n", __func__, isci_port);
194
195         if (isci_port) {
196
197                 /* check to see if this is the last phy on this port. */
198                 if (isci_phy->sas_phy.port
199                     && isci_phy->sas_phy.port->num_phys == 1) {
200
201                         /* change the state for all devices on this port.
202                          * The next task sent to this device will be returned
203                          * as SAS_TASK_UNDELIVERED, and the scsi mid layer
204                          * will remove the target
205                          */
206                         list_for_each_entry(isci_device,
207                                             &isci_port->remote_dev_list,
208                                             node) {
209                                 dev_dbg(&isci_host->pdev->dev,
210                                         "%s: isci_device = %p\n",
211                                         __func__, isci_device);
212                                 isci_remote_device_change_state(isci_device,
213                                                                 isci_stopping);
214                         }
215                 }
216                 isci_port_change_state(isci_port, isci_stopping);
217         }
218
219         /* Notify libsas of the borken link, this will trigger calls to our
220          * isci_port_deformed and isci_dev_gone functions.
221          */
222         sas_phy_disconnected(&isci_phy->sas_phy);
223         isci_host->sas_ha.notify_phy_event(&isci_phy->sas_phy,
224                                            PHYE_LOSS_OF_SIGNAL);
225
226         isci_phy->isci_port = NULL;
227
228         dev_dbg(&isci_host->pdev->dev,
229                 "%s: isci_port = %p - Done\n", __func__, isci_port);
230 }
231
232
233 /**
234  * isci_port_deformed() - This function is called by libsas when a port becomes
235  *    inactive.
236  * @phy: This parameter specifies the libsas phy with the inactive port.
237  *
238  */
239 void isci_port_deformed(
240         struct asd_sas_phy *phy)
241 {
242         pr_debug("%s: sas_phy = %p\n", __func__, phy);
243 }
244
245 /**
246  * isci_port_formed() - This function is called by libsas when a port becomes
247  *    active.
248  * @phy: This parameter specifies the libsas phy with the active port.
249  *
250  */
251 void isci_port_formed(
252         struct asd_sas_phy *phy)
253 {
254         pr_debug("%s: sas_phy = %p, sas_port = %p\n", __func__, phy, phy->port);
255 }
256
257 /**
258  * isci_port_ready() - This function is called by the sci core when a link
259  *    becomes ready.
260  * @isci_host: This parameter specifies the isci host object.
261  * @port: This parameter specifies the sci port with the active link.
262  *
263  */
264 void isci_port_ready(struct isci_host *isci_host, struct isci_port *isci_port)
265 {
266         dev_dbg(&isci_host->pdev->dev,
267                 "%s: isci_port = %p\n", __func__, isci_port);
268
269         complete_all(&isci_port->start_complete);
270         isci_port_change_state(isci_port, isci_ready);
271         return;
272 }
273
274 /**
275  * isci_port_not_ready() - This function is called by the sci core when a link
276  *    is not ready. All remote devices on this link will be removed if they are
277  *    in the stopping state.
278  * @isci_host: This parameter specifies the isci host object.
279  * @port: This parameter specifies the sci port with the active link.
280  *
281  */
282 void isci_port_not_ready(struct isci_host *isci_host, struct isci_port *isci_port)
283 {
284         dev_dbg(&isci_host->pdev->dev,
285                 "%s: isci_port = %p\n", __func__, isci_port);
286 }
287
288 /**
289  * isci_port_hard_reset_complete() - This function is called by the sci core
290  *    when the hard reset complete notification has been received.
291  * @port: This parameter specifies the sci port with the active link.
292  * @completion_status: This parameter specifies the core status for the reset
293  *    process.
294  *
295  */
296 void isci_port_hard_reset_complete(struct isci_port *isci_port,
297                                    enum sci_status completion_status)
298 {
299         dev_dbg(&isci_port->isci_host->pdev->dev,
300                 "%s: isci_port = %p, completion_status=%x\n",
301                      __func__, isci_port, completion_status);
302
303         /* Save the status of the hard reset from the port. */
304         isci_port->hard_reset_status = completion_status;
305
306         complete_all(&isci_port->hard_reset_complete);
307 }
308
309 int isci_port_perform_hard_reset(struct isci_host *ihost, struct isci_port *iport,
310                                  struct isci_phy *iphy)
311 {
312         unsigned long flags;
313         enum sci_status status;
314         int ret = TMF_RESP_FUNC_COMPLETE;
315
316         dev_dbg(&ihost->pdev->dev, "%s: iport = %p\n",
317                 __func__, iport);
318
319         init_completion(&iport->hard_reset_complete);
320
321         spin_lock_irqsave(&ihost->scic_lock, flags);
322
323         #define ISCI_PORT_RESET_TIMEOUT SCIC_SDS_SIGNATURE_FIS_TIMEOUT
324         status = scic_port_hard_reset(&iport->sci, ISCI_PORT_RESET_TIMEOUT);
325
326         spin_unlock_irqrestore(&ihost->scic_lock, flags);
327
328         if (status == SCI_SUCCESS) {
329                 wait_for_completion(&iport->hard_reset_complete);
330
331                 dev_dbg(&ihost->pdev->dev,
332                         "%s: iport = %p; hard reset completion\n",
333                         __func__, iport);
334
335                 if (iport->hard_reset_status != SCI_SUCCESS)
336                         ret = TMF_RESP_FUNC_FAILED;
337         } else {
338                 ret = TMF_RESP_FUNC_FAILED;
339
340                 dev_err(&ihost->pdev->dev,
341                         "%s: iport = %p; scic_port_hard_reset call"
342                         " failed 0x%x\n",
343                         __func__, iport, status);
344
345         }
346
347         /* If the hard reset for the port has failed, consider this
348          * the same as link failures on all phys in the port.
349          */
350         if (ret != TMF_RESP_FUNC_COMPLETE) {
351                 dev_err(&ihost->pdev->dev,
352                         "%s: iport = %p; hard reset failed "
353                         "(0x%x) - sending link down to libsas for phy %p\n",
354                         __func__, iport, iport->hard_reset_status, iphy);
355
356                 isci_port_link_down(ihost, iphy, iport);
357         }
358
359         return ret;
360 }
361
362 void isci_port_stop_complete(struct scic_sds_controller *scic,
363                                           struct scic_sds_port *sci_port,
364                                           enum sci_status completion_status)
365 {
366         dev_dbg(&scic_to_ihost(scic)->pdev->dev, "Port stop complete\n");
367 }