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