isci: unify isci_host data structures
[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 /**
57  * This file contains the isci port implementation.
58  *
59  *
60  */
61
62
63 #include <linux/workqueue.h>
64 #include "isci.h"
65 #include "scic_io_request.h"
66 #include "scic_phy.h"
67 #include "scic_sds_phy.h"
68 #include "scic_port.h"
69 #include "port.h"
70 #include "request.h"
71 #include "core/scic_sds_controller.h"
72
73 static void isci_port_change_state(
74         struct isci_port *isci_port,
75         enum isci_status status);
76
77
78
79 /**
80  * isci_port_init() - This function initializes the given isci_port object.
81  * @isci_port: This parameter specifies the port object to be initialized.
82  * @isci_host: This parameter specifies parent controller object for the port.
83  * @index: This parameter specifies which SCU port the isci_port associates
84  *    with. Generally, SCU port 0 relates to isci_port 0, etc.
85  *
86  */
87 void isci_port_init(
88         struct isci_port *isci_port,
89         struct isci_host *isci_host,
90         int index)
91 {
92         struct scic_sds_port *scic_port;
93
94         INIT_LIST_HEAD(&isci_port->remote_dev_list);
95         INIT_LIST_HEAD(&isci_port->domain_dev_list);
96         spin_lock_init(&isci_port->state_lock);
97         init_completion(&isci_port->start_complete);
98         isci_port->isci_host = isci_host;
99         isci_port_change_state(isci_port, isci_freed);
100
101         (void)scic_controller_get_port_handle(&isci_host->sci, index, &scic_port);
102         isci_port->sci_port_handle = scic_port;
103         scic_port->iport = isci_port;
104 }
105
106
107 /**
108  * isci_port_get_state() - This function gets the status of the port object.
109  * @isci_port: This parameter points to the isci_port object
110  *
111  * status of the object as a isci_status enum.
112  */
113 enum isci_status isci_port_get_state(
114         struct isci_port *isci_port)
115 {
116         return isci_port->status;
117 }
118
119 static void isci_port_change_state(
120         struct isci_port *isci_port,
121         enum isci_status status)
122 {
123         unsigned long flags;
124
125         dev_dbg(&isci_port->isci_host->pdev->dev,
126                 "%s: isci_port = %p, state = 0x%x\n",
127                 __func__, isci_port, status);
128
129         spin_lock_irqsave(&isci_port->state_lock, flags);
130         isci_port->status = status;
131         spin_unlock_irqrestore(&isci_port->state_lock, flags);
132 }
133
134 void isci_port_bc_change_received(
135         struct isci_host *isci_host,
136         struct scic_sds_port *port,
137         struct scic_sds_phy *phy)
138 {
139         struct isci_phy *isci_phy = phy->iphy;
140
141         dev_dbg(&isci_host->pdev->dev,
142                 "%s: isci_phy = %p, sas_phy = %p\n",
143                 __func__,
144                 isci_phy,
145                 &isci_phy->sas_phy);
146
147         isci_host->sas_ha.notify_port_event(
148                 &isci_phy->sas_phy,
149                 PORTE_BROADCAST_RCVD
150                 );
151
152         scic_port_enable_broadcast_change_notification(port);
153 }
154
155 /**
156  * isci_port_link_up() - This function is called by the sci core when a link
157  *    becomes active. the identify address frame is retrieved from the core and
158  *    a notify port event is sent to libsas.
159  * @isci_host: This parameter specifies the isci host object.
160  * @port: This parameter specifies the sci port with the active link.
161  * @phy: This parameter specifies the sci phy with the active link.
162  *
163  */
164 void isci_port_link_up(
165         struct isci_host *isci_host,
166         struct scic_sds_port *port,
167         struct scic_sds_phy *phy)
168 {
169         unsigned long flags;
170         struct scic_port_properties properties;
171         struct isci_phy *isci_phy = phy->iphy;
172         struct isci_port *isci_port = port->iport;
173         unsigned long success = true;
174
175         BUG_ON(isci_phy->isci_port != NULL);
176
177         isci_phy->isci_port = isci_port;
178
179         dev_dbg(&isci_host->pdev->dev,
180                 "%s: isci_port = %p\n",
181                 __func__, isci_port);
182
183         spin_lock_irqsave(&isci_phy->sas_phy.frame_rcvd_lock, flags);
184
185         isci_port_change_state(isci_phy->isci_port, isci_starting);
186
187         scic_port_get_properties(port, &properties);
188
189         if (phy->protocol == SCIC_SDS_PHY_PROTOCOL_SATA) {
190                 u64 attached_sas_address;
191
192                 isci_phy->sas_phy.oob_mode = SATA_OOB_MODE;
193                 isci_phy->sas_phy.frame_rcvd_size = sizeof(struct dev_to_host_fis);
194
195                 /*
196                  * For direct-attached SATA devices, the SCI core will
197                  * automagically assign a SAS address to the end device
198                  * for the purpose of creating a port. This SAS address
199                  * will not be the same as assigned to the PHY and needs
200                  * to be obtained from struct scic_port_properties properties.
201                  */
202                 attached_sas_address = properties.remote.sas_address.high;
203                 attached_sas_address <<= 32;
204                 attached_sas_address |= properties.remote.sas_address.low;
205                 swab64s(&attached_sas_address);
206
207                 memcpy(&isci_phy->sas_phy.attached_sas_addr,
208                        &attached_sas_address, sizeof(attached_sas_address));
209         } else if (phy->protocol == SCIC_SDS_PHY_PROTOCOL_SAS) {
210                 isci_phy->sas_phy.oob_mode = SAS_OOB_MODE;
211                 isci_phy->sas_phy.frame_rcvd_size = sizeof(struct sas_identify_frame);
212
213                 /* Copy the attached SAS address from the IAF */
214                 memcpy(isci_phy->sas_phy.attached_sas_addr,
215                        isci_phy->frame_rcvd.iaf.sas_addr, SAS_ADDR_SIZE);
216         } else {
217                 dev_err(&isci_host->pdev->dev, "%s: unkown target\n", __func__);
218                 success = false;
219         }
220
221         isci_phy->sas_phy.phy->negotiated_linkrate = sci_phy_linkrate(phy);
222
223         spin_unlock_irqrestore(&isci_phy->sas_phy.frame_rcvd_lock, flags);
224
225         /* Notify libsas that we have an address frame, if indeed
226          * we've found an SSP, SMP, or STP target */
227         if (success)
228                 isci_host->sas_ha.notify_port_event(&isci_phy->sas_phy,
229                                                     PORTE_BYTES_DMAED);
230 }
231
232
233 /**
234  * isci_port_link_down() - This function is called by the sci core when a link
235  *    becomes inactive.
236  * @isci_host: This parameter specifies the isci host object.
237  * @phy: This parameter specifies the isci phy with the active link.
238  * @port: This parameter specifies the isci port with the active link.
239  *
240  */
241 void isci_port_link_down(struct isci_host *isci_host, struct isci_phy *isci_phy,
242                          struct isci_port *isci_port)
243 {
244         struct isci_remote_device *isci_device;
245
246         dev_dbg(&isci_host->pdev->dev,
247                 "%s: isci_port = %p\n", __func__, isci_port);
248
249         if (isci_port) {
250
251                 /* check to see if this is the last phy on this port. */
252                 if (isci_phy->sas_phy.port
253                     && isci_phy->sas_phy.port->num_phys == 1) {
254
255                         /* change the state for all devices on this port.
256                          * The next task sent to this device will be returned
257                          * as SAS_TASK_UNDELIVERED, and the scsi mid layer
258                          * will remove the target
259                          */
260                         list_for_each_entry(isci_device,
261                                             &isci_port->remote_dev_list,
262                                             node) {
263                                 dev_dbg(&isci_host->pdev->dev,
264                                         "%s: isci_device = %p\n",
265                                         __func__, isci_device);
266                                 isci_remote_device_change_state(isci_device,
267                                                                 isci_stopping);
268                         }
269                 }
270                 isci_port_change_state(isci_port, isci_stopping);
271         }
272
273         /* Notify libsas of the borken link, this will trigger calls to our
274          * isci_port_deformed and isci_dev_gone functions.
275          */
276         sas_phy_disconnected(&isci_phy->sas_phy);
277         isci_host->sas_ha.notify_phy_event(&isci_phy->sas_phy,
278                                            PHYE_LOSS_OF_SIGNAL);
279
280         isci_phy->isci_port = NULL;
281
282         dev_dbg(&isci_host->pdev->dev,
283                 "%s: isci_port = %p - Done\n", __func__, isci_port);
284 }
285
286
287 /**
288  * isci_port_deformed() - This function is called by libsas when a port becomes
289  *    inactive.
290  * @phy: This parameter specifies the libsas phy with the inactive port.
291  *
292  */
293 void isci_port_deformed(
294         struct asd_sas_phy *phy)
295 {
296         pr_debug("%s: sas_phy = %p\n", __func__, phy);
297 }
298
299 /**
300  * isci_port_formed() - This function is called by libsas when a port becomes
301  *    active.
302  * @phy: This parameter specifies the libsas phy with the active port.
303  *
304  */
305 void isci_port_formed(
306         struct asd_sas_phy *phy)
307 {
308         pr_debug("%s: sas_phy = %p, sas_port = %p\n", __func__, phy, phy->port);
309 }
310
311 /**
312  * isci_port_ready() - This function is called by the sci core when a link
313  *    becomes ready.
314  * @isci_host: This parameter specifies the isci host object.
315  * @port: This parameter specifies the sci port with the active link.
316  *
317  */
318 void isci_port_ready(struct isci_host *isci_host, struct isci_port *isci_port)
319 {
320         dev_dbg(&isci_host->pdev->dev,
321                 "%s: isci_port = %p\n", __func__, isci_port);
322
323         complete_all(&isci_port->start_complete);
324         isci_port_change_state(isci_port, isci_ready);
325         return;
326 }
327
328 /**
329  * isci_port_not_ready() - This function is called by the sci core when a link
330  *    is not ready. All remote devices on this link will be removed if they are
331  *    in the stopping state.
332  * @isci_host: This parameter specifies the isci host object.
333  * @port: This parameter specifies the sci port with the active link.
334  *
335  */
336 void isci_port_not_ready(struct isci_host *isci_host, struct isci_port *isci_port)
337 {
338         dev_dbg(&isci_host->pdev->dev,
339                 "%s: isci_port = %p\n", __func__, isci_port);
340 }
341
342 /**
343  * isci_port_hard_reset_complete() - This function is called by the sci core
344  *    when the hard reset complete notification has been received.
345  * @port: This parameter specifies the sci port with the active link.
346  * @completion_status: This parameter specifies the core status for the reset
347  *    process.
348  *
349  */
350 void isci_port_hard_reset_complete(struct isci_port *isci_port,
351                                    enum sci_status completion_status)
352 {
353         dev_dbg(&isci_port->isci_host->pdev->dev,
354                 "%s: isci_port = %p, completion_status=%x\n",
355                      __func__, isci_port, completion_status);
356
357         /* Save the status of the hard reset from the port. */
358         isci_port->hard_reset_status = completion_status;
359
360         complete_all(&isci_port->hard_reset_complete);
361 }
362
363 int isci_port_perform_hard_reset(struct isci_host *ihost, struct isci_port *iport,
364                                  struct isci_phy *iphy)
365 {
366         unsigned long flags;
367         enum sci_status status;
368         int ret = TMF_RESP_FUNC_COMPLETE;
369
370         dev_dbg(&ihost->pdev->dev, "%s: iport = %p\n",
371                 __func__, iport);
372
373         init_completion(&iport->hard_reset_complete);
374
375         spin_lock_irqsave(&ihost->scic_lock, flags);
376
377         #define ISCI_PORT_RESET_TIMEOUT SCIC_SDS_SIGNATURE_FIS_TIMEOUT
378         status = scic_port_hard_reset(iport->sci_port_handle,
379                                       ISCI_PORT_RESET_TIMEOUT);
380
381         spin_unlock_irqrestore(&ihost->scic_lock, flags);
382
383         if (status == SCI_SUCCESS) {
384                 wait_for_completion(&iport->hard_reset_complete);
385
386                 dev_dbg(&ihost->pdev->dev,
387                         "%s: iport = %p; hard reset completion\n",
388                         __func__, iport);
389
390                 if (iport->hard_reset_status != SCI_SUCCESS)
391                         ret = TMF_RESP_FUNC_FAILED;
392         } else {
393                 ret = TMF_RESP_FUNC_FAILED;
394
395                 dev_err(&ihost->pdev->dev,
396                         "%s: iport = %p; scic_port_hard_reset call"
397                         " failed 0x%x\n",
398                         __func__, iport, status);
399
400         }
401
402         /* If the hard reset for the port has failed, consider this
403          * the same as link failures on all phys in the port.
404          */
405         if (ret != TMF_RESP_FUNC_COMPLETE) {
406                 dev_err(&ihost->pdev->dev,
407                         "%s: iport = %p; hard reset failed "
408                         "(0x%x) - sending link down to libsas for phy %p\n",
409                         __func__, iport, iport->hard_reset_status, iphy);
410
411                 isci_port_link_down(ihost, iphy, iport);
412         }
413
414         return ret;
415 }
416
417 void isci_port_stop_complete(struct scic_sds_controller *scic,
418                                           struct scic_sds_port *sci_port,
419                                           enum sci_status completion_status)
420 {
421         dev_dbg(&scic_to_ihost(scic)->pdev->dev, "Port stop complete\n");
422 }