isci: Removed sci_base_object from scic_sds_port.
[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         struct scic_sds_controller *controller = isci_host->core_controller;
94
95         INIT_LIST_HEAD(&isci_port->remote_dev_list);
96         INIT_LIST_HEAD(&isci_port->domain_dev_list);
97         spin_lock_init(&isci_port->state_lock);
98         init_completion(&isci_port->start_complete);
99         isci_port->isci_host = isci_host;
100         isci_port_change_state(isci_port, isci_freed);
101
102         (void)scic_controller_get_port_handle(controller, index, &scic_port);
103         isci_port->sci_port_handle = scic_port;
104         scic_port->iport = isci_port;
105 }
106
107
108 /**
109  * isci_port_get_state() - This function gets the status of the port object.
110  * @isci_port: This parameter points to the isci_port object
111  *
112  * status of the object as a isci_status enum.
113  */
114 enum isci_status isci_port_get_state(
115         struct isci_port *isci_port)
116 {
117         return isci_port->status;
118 }
119
120 static void isci_port_change_state(
121         struct isci_port *isci_port,
122         enum isci_status status)
123 {
124         unsigned long flags;
125
126         dev_dbg(&isci_port->isci_host->pdev->dev,
127                 "%s: isci_port = %p, state = 0x%x\n",
128                 __func__, isci_port, status);
129
130         spin_lock_irqsave(&isci_port->state_lock, flags);
131         isci_port->status = status;
132         spin_unlock_irqrestore(&isci_port->state_lock, flags);
133 }
134
135 void isci_port_bc_change_received(
136         struct isci_host *isci_host,
137         struct scic_sds_port *port,
138         struct scic_sds_phy *phy)
139 {
140         struct isci_phy *isci_phy = phy->iphy;
141
142         dev_dbg(&isci_host->pdev->dev,
143                 "%s: isci_phy = %p, sas_phy = %p\n",
144                 __func__,
145                 isci_phy,
146                 &isci_phy->sas_phy);
147
148         isci_host->sas_ha.notify_port_event(
149                 &isci_phy->sas_phy,
150                 PORTE_BROADCAST_RCVD
151                 );
152
153         scic_port_enable_broadcast_change_notification(port);
154 }
155
156 /**
157  * isci_port_link_up() - This function is called by the sci core when a link
158  *    becomes active. the identify address frame is retrieved from the core and
159  *    a notify port event is sent to libsas.
160  * @isci_host: This parameter specifies the isci host object.
161  * @port: This parameter specifies the sci port with the active link.
162  * @phy: This parameter specifies the sci phy with the active link.
163  *
164  */
165 void isci_port_link_up(
166         struct isci_host *isci_host,
167         struct scic_sds_port *port,
168         struct scic_sds_phy *phy)
169 {
170         unsigned long flags;
171         struct scic_port_properties properties;
172         struct isci_phy *isci_phy = phy->iphy;
173         struct isci_port *isci_port = port->iport;
174         enum sci_status call_status;
175         unsigned long success = true;
176
177         BUG_ON(isci_phy->isci_port != NULL);
178
179         isci_phy->isci_port = isci_port;
180
181         dev_dbg(&isci_host->pdev->dev,
182                 "%s: isci_port = %p\n",
183                 __func__, isci_port);
184
185         spin_lock_irqsave(&isci_phy->sas_phy.frame_rcvd_lock, flags);
186
187         isci_port_change_state(isci_phy->isci_port, isci_starting);
188
189         scic_port_get_properties(port, &properties);
190
191         if (properties.remote.protocols.u.bits.stp_target) {
192                 u64 attached_sas_address;
193
194                 struct scic_sata_phy_properties sata_phy_properties;
195
196                 isci_phy->sas_phy.oob_mode = SATA_OOB_MODE;
197
198                 /* Get a copy of the signature fis for libsas */
199                 call_status = scic_sata_phy_get_properties(phy,
200                                                            &sata_phy_properties);
201
202                 /*
203                  * XXX I am concerned about this "assert". shouldn't we
204                  * handle the return appropriately?
205                  */
206                 BUG_ON(call_status != SCI_SUCCESS);
207
208                 memcpy(isci_phy->frame_rcvd.fis,
209                        &sata_phy_properties.signature_fis,
210                        sizeof(struct sata_fis_reg_d2h));
211
212                 isci_phy->sas_phy.frame_rcvd_size = sizeof(struct sata_fis_reg_d2h);
213
214                 /*
215                  * For direct-attached SATA devices, the SCI core will
216                  * automagically assign a SAS address to the end device
217                  * for the purpose of creating a port. This SAS address
218                  * will not be the same as assigned to the PHY and needs
219                  * to be obtained from struct scic_port_properties properties.
220                  */
221                 attached_sas_address = properties.remote.sas_address.high;
222                 attached_sas_address <<= 32;
223                 attached_sas_address |= properties.remote.sas_address.low;
224                 swab64s(&attached_sas_address);
225
226                 memcpy(&isci_phy->sas_phy.attached_sas_addr,
227                        &attached_sas_address, sizeof(attached_sas_address));
228
229         } else if (properties.remote.protocols.u.bits.ssp_target ||
230                    properties.remote.protocols.u.bits.smp_target) {
231
232                 struct scic_sas_phy_properties sas_phy_properties;
233
234                 isci_phy->sas_phy.oob_mode = SAS_OOB_MODE;
235
236                 /* Get a copy of the identify address frame for libsas */
237                 call_status = scic_sas_phy_get_properties(phy,
238                                                           &sas_phy_properties);
239
240                 BUG_ON(call_status != SCI_SUCCESS);
241
242                 memcpy(isci_phy->frame_rcvd.aif,
243                        &(sas_phy_properties.received_iaf),
244                        sizeof(struct sci_sas_identify_address_frame));
245
246                 isci_phy->sas_phy.frame_rcvd_size
247                         = sizeof(struct sci_sas_identify_address_frame);
248
249                 /* Copy the attached SAS address from the IAF */
250                 memcpy(isci_phy->sas_phy.attached_sas_addr,
251                        ((struct sas_identify_frame *)
252                         (&isci_phy->frame_rcvd.aif))->sas_addr,
253                        SAS_ADDR_SIZE);
254
255         } else {
256                 dev_err(&isci_host->pdev->dev, "%s: unkown target\n", __func__);
257                 success = false;
258         }
259
260         isci_phy->sas_phy.phy->negotiated_linkrate = sci_phy_linkrate(phy);
261
262         spin_unlock_irqrestore(&isci_phy->sas_phy.frame_rcvd_lock, flags);
263
264         /* Notify libsas that we have an address frame, if indeed
265          * we've found an SSP, SMP, or STP target */
266         if (success)
267                 isci_host->sas_ha.notify_port_event(&isci_phy->sas_phy,
268                                                     PORTE_BYTES_DMAED);
269 }
270
271
272 /**
273  * isci_port_link_down() - This function is called by the sci core when a link
274  *    becomes inactive.
275  * @isci_host: This parameter specifies the isci host object.
276  * @phy: This parameter specifies the isci phy with the active link.
277  * @port: This parameter specifies the isci port with the active link.
278  *
279  */
280 void isci_port_link_down(struct isci_host *isci_host, struct isci_phy *isci_phy,
281                          struct isci_port *isci_port)
282 {
283         struct isci_remote_device *isci_device;
284
285         dev_dbg(&isci_host->pdev->dev,
286                 "%s: isci_port = %p\n", __func__, isci_port);
287
288         if (isci_port) {
289
290                 /* check to see if this is the last phy on this port. */
291                 if (isci_phy->sas_phy.port
292                     && isci_phy->sas_phy.port->num_phys == 1) {
293
294                         /* change the state for all devices on this port.
295                          * The next task sent to this device will be returned
296                          * as SAS_TASK_UNDELIVERED, and the scsi mid layer
297                          * will remove the target
298                          */
299                         list_for_each_entry(isci_device,
300                                             &isci_port->remote_dev_list,
301                                             node) {
302                                 dev_dbg(&isci_host->pdev->dev,
303                                         "%s: isci_device = %p\n",
304                                         __func__, isci_device);
305                                 isci_remote_device_change_state(isci_device,
306                                                                 isci_stopping);
307                         }
308                 }
309                 isci_port_change_state(isci_port, isci_stopping);
310         }
311
312         /* Notify libsas of the borken link, this will trigger calls to our
313          * isci_port_deformed and isci_dev_gone functions.
314          */
315         sas_phy_disconnected(&isci_phy->sas_phy);
316         isci_host->sas_ha.notify_phy_event(&isci_phy->sas_phy,
317                                            PHYE_LOSS_OF_SIGNAL);
318
319         isci_phy->isci_port = NULL;
320
321         dev_dbg(&isci_host->pdev->dev,
322                 "%s: isci_port = %p - Done\n", __func__, isci_port);
323 }
324
325
326 /**
327  * isci_port_deformed() - This function is called by libsas when a port becomes
328  *    inactive.
329  * @phy: This parameter specifies the libsas phy with the inactive port.
330  *
331  */
332 void isci_port_deformed(
333         struct asd_sas_phy *phy)
334 {
335         pr_debug("%s: sas_phy = %p\n", __func__, phy);
336 }
337
338 /**
339  * isci_port_formed() - This function is called by libsas when a port becomes
340  *    active.
341  * @phy: This parameter specifies the libsas phy with the active port.
342  *
343  */
344 void isci_port_formed(
345         struct asd_sas_phy *phy)
346 {
347         pr_debug("%s: sas_phy = %p, sas_port = %p\n", __func__, phy, phy->port);
348 }
349
350 /**
351  * isci_port_ready() - This function is called by the sci core when a link
352  *    becomes ready.
353  * @isci_host: This parameter specifies the isci host object.
354  * @port: This parameter specifies the sci port with the active link.
355  *
356  */
357 void isci_port_ready(struct isci_host *isci_host, struct isci_port *isci_port)
358 {
359         dev_dbg(&isci_host->pdev->dev,
360                 "%s: isci_port = %p\n", __func__, isci_port);
361
362         complete_all(&isci_port->start_complete);
363         isci_port_change_state(isci_port, isci_ready);
364         return;
365 }
366
367 /**
368  * isci_port_not_ready() - This function is called by the sci core when a link
369  *    is not ready. All remote devices on this link will be removed if they are
370  *    in the stopping state.
371  * @isci_host: This parameter specifies the isci host object.
372  * @port: This parameter specifies the sci port with the active link.
373  *
374  */
375 void isci_port_not_ready(struct isci_host *isci_host, struct isci_port *isci_port)
376 {
377         dev_dbg(&isci_host->pdev->dev,
378                 "%s: isci_port = %p\n", __func__, isci_port);
379 }
380
381 /**
382  * isci_port_hard_reset_complete() - This function is called by the sci core
383  *    when the hard reset complete notification has been received.
384  * @port: This parameter specifies the sci port with the active link.
385  * @completion_status: This parameter specifies the core status for the reset
386  *    process.
387  *
388  */
389 void isci_port_hard_reset_complete(struct isci_port *isci_port,
390                                    enum sci_status completion_status)
391 {
392         dev_dbg(&isci_port->isci_host->pdev->dev,
393                 "%s: isci_port = %p, completion_status=%x\n",
394                      __func__, isci_port, completion_status);
395
396         /* Save the status of the hard reset from the port. */
397         isci_port->hard_reset_status = completion_status;
398
399         complete_all(&isci_port->hard_reset_complete);
400 }
401
402 int isci_port_perform_hard_reset(struct isci_host *ihost, struct isci_port *iport,
403                                  struct isci_phy *iphy)
404 {
405         unsigned long flags;
406         enum sci_status status;
407         int ret = TMF_RESP_FUNC_COMPLETE;
408
409         dev_dbg(&ihost->pdev->dev, "%s: iport = %p\n",
410                 __func__, iport);
411
412         init_completion(&iport->hard_reset_complete);
413
414         spin_lock_irqsave(&ihost->scic_lock, flags);
415
416         #define ISCI_PORT_RESET_TIMEOUT SCIC_SDS_SIGNATURE_FIS_TIMEOUT
417         status = scic_port_hard_reset(iport->sci_port_handle,
418                                       ISCI_PORT_RESET_TIMEOUT);
419
420         spin_unlock_irqrestore(&ihost->scic_lock, flags);
421
422         if (status == SCI_SUCCESS) {
423                 wait_for_completion(&iport->hard_reset_complete);
424
425                 dev_dbg(&ihost->pdev->dev,
426                         "%s: iport = %p; hard reset completion\n",
427                         __func__, iport);
428
429                 if (iport->hard_reset_status != SCI_SUCCESS)
430                         ret = TMF_RESP_FUNC_FAILED;
431         } else {
432                 ret = TMF_RESP_FUNC_FAILED;
433
434                 dev_err(&ihost->pdev->dev,
435                         "%s: iport = %p; scic_port_hard_reset call"
436                         " failed 0x%x\n",
437                         __func__, iport, status);
438
439         }
440
441         /* If the hard reset for the port has failed, consider this
442          * the same as link failures on all phys in the port.
443          */
444         if (ret != TMF_RESP_FUNC_COMPLETE) {
445                 dev_err(&ihost->pdev->dev,
446                         "%s: iport = %p; hard reset failed "
447                         "(0x%x) - sending link down to libsas for phy %p\n",
448                         __func__, iport, iport->hard_reset_status, iphy);
449
450                 isci_port_link_down(ihost, iphy, iport);
451         }
452
453         return ret;
454 }
455
456 /**
457  * isci_port_invalid_link_up() - This function informs the SCI Core user that
458  *    a phy/link became ready, but the phy is not allowed in the port.  In some
459  *    situations the underlying hardware only allows for certain phy to port
460  *    mappings.  If these mappings are violated, then this API is invoked.
461  * @controller: This parameter represents the controller which contains the
462  *    port.
463  * @port: This parameter specifies the SCI port object for which the callback
464  *    is being invoked.
465  * @phy: This parameter specifies the phy that came ready, but the phy can't be
466  *    a valid member of the port.
467  *
468  */
469 void isci_port_invalid_link_up(struct scic_sds_controller *scic,
470                                       struct scic_sds_port *sci_port,
471                                       struct scic_sds_phy *phy)
472 {
473         struct isci_host *ihost = scic->ihost;
474
475         dev_warn(&ihost->pdev->dev, "Invalid link up!\n");
476 }
477
478 void isci_port_stop_complete(struct scic_sds_controller *scic,
479                                           struct scic_sds_port *sci_port,
480                                           enum sci_status completion_status)
481 {
482         struct isci_host *ihost = scic->ihost;
483
484         dev_dbg(&ihost->pdev->dev, "Port stop complete\n");
485 }