blob: 6110306e8e230ab1269351a61443bc9adb97c45d [file] [log] [blame]
Dan Williams6f231dd2011-07-02 22:56:22 -07001/*
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"
Dan Williams6f231dd2011-07-02 22:56:22 -070066#include "scic_phy.h"
67#include "scic_sds_phy.h"
68#include "scic_port.h"
69#include "port.h"
70#include "request.h"
Maciej Patelczykd3757c32011-04-28 22:06:06 +000071#include "core/scic_sds_controller.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070072
73static void isci_port_change_state(
74 struct isci_port *isci_port,
75 enum isci_status status);
76
77
78
Edmund Nadolskied30c272011-05-05 01:11:43 +000079void isci_port_init(struct isci_port *iport, struct isci_host *ihost, int index)
Dan Williams6f231dd2011-07-02 22:56:22 -070080{
Edmund Nadolskied30c272011-05-05 01:11:43 +000081 struct scic_sds_port *sci_port;
Dan Williams6f231dd2011-07-02 22:56:22 -070082
Edmund Nadolskied30c272011-05-05 01:11:43 +000083 INIT_LIST_HEAD(&iport->remote_dev_list);
84 INIT_LIST_HEAD(&iport->domain_dev_list);
85 spin_lock_init(&iport->state_lock);
86 init_completion(&iport->start_complete);
87 iport->isci_host = ihost;
88 isci_port_change_state(iport, isci_freed);
Dan Williams6f231dd2011-07-02 22:56:22 -070089
Edmund Nadolskied30c272011-05-05 01:11:43 +000090 sci_port = &ihost->sci.port_table[index];
91 iport->sci_port_handle = sci_port;
92 sci_port->iport = iport;
Dan Williams6f231dd2011-07-02 22:56:22 -070093}
94
95
96/**
97 * isci_port_get_state() - This function gets the status of the port object.
98 * @isci_port: This parameter points to the isci_port object
99 *
100 * status of the object as a isci_status enum.
101 */
102enum isci_status isci_port_get_state(
103 struct isci_port *isci_port)
104{
105 return isci_port->status;
106}
107
108static void isci_port_change_state(
109 struct isci_port *isci_port,
110 enum isci_status status)
111{
112 unsigned long flags;
113
114 dev_dbg(&isci_port->isci_host->pdev->dev,
115 "%s: isci_port = %p, state = 0x%x\n",
116 __func__, isci_port, status);
117
118 spin_lock_irqsave(&isci_port->state_lock, flags);
119 isci_port->status = status;
120 spin_unlock_irqrestore(&isci_port->state_lock, flags);
121}
122
123void isci_port_bc_change_received(
124 struct isci_host *isci_host,
125 struct scic_sds_port *port,
126 struct scic_sds_phy *phy)
127{
Maciej Patelczyke1e72a02011-04-28 22:06:11 +0000128 struct isci_phy *isci_phy = phy->iphy;
Dan Williams6f231dd2011-07-02 22:56:22 -0700129
130 dev_dbg(&isci_host->pdev->dev,
131 "%s: isci_phy = %p, sas_phy = %p\n",
132 __func__,
133 isci_phy,
134 &isci_phy->sas_phy);
135
136 isci_host->sas_ha.notify_port_event(
137 &isci_phy->sas_phy,
138 PORTE_BROADCAST_RCVD
139 );
140
141 scic_port_enable_broadcast_change_notification(port);
142}
143
144/**
145 * isci_port_link_up() - This function is called by the sci core when a link
146 * becomes active. the identify address frame is retrieved from the core and
147 * a notify port event is sent to libsas.
148 * @isci_host: This parameter specifies the isci host object.
149 * @port: This parameter specifies the sci port with the active link.
150 * @phy: This parameter specifies the sci phy with the active link.
151 *
152 */
153void isci_port_link_up(
154 struct isci_host *isci_host,
155 struct scic_sds_port *port,
156 struct scic_sds_phy *phy)
157{
158 unsigned long flags;
159 struct scic_port_properties properties;
Maciej Patelczyke1e72a02011-04-28 22:06:11 +0000160 struct isci_phy *isci_phy = phy->iphy;
Maciej Patelczyk115bd1f2011-04-28 22:06:16 +0000161 struct isci_port *isci_port = port->iport;
Dan Williams6f231dd2011-07-02 22:56:22 -0700162 unsigned long success = true;
163
164 BUG_ON(isci_phy->isci_port != NULL);
Bartosz Barcinski6cb4d6b2011-04-12 17:28:43 -0700165
Dan Williams6f231dd2011-07-02 22:56:22 -0700166 isci_phy->isci_port = isci_port;
167
168 dev_dbg(&isci_host->pdev->dev,
169 "%s: isci_port = %p\n",
170 __func__, isci_port);
171
172 spin_lock_irqsave(&isci_phy->sas_phy.frame_rcvd_lock, flags);
173
174 isci_port_change_state(isci_phy->isci_port, isci_starting);
175
176 scic_port_get_properties(port, &properties);
177
Dave Jiangd7b90fc2011-05-04 18:22:33 -0700178 if (phy->protocol == SCIC_SDS_PHY_PROTOCOL_SATA) {
Dan Williams150fc6f2011-02-25 10:25:21 -0800179 u64 attached_sas_address;
Dan Williams6f231dd2011-07-02 22:56:22 -0700180
Dan Williams6f231dd2011-07-02 22:56:22 -0700181 isci_phy->sas_phy.oob_mode = SATA_OOB_MODE;
Dave Jiangf2f30082011-05-04 15:02:02 -0700182 isci_phy->sas_phy.frame_rcvd_size = sizeof(struct dev_to_host_fis);
Dan Williams6f231dd2011-07-02 22:56:22 -0700183
184 /*
185 * For direct-attached SATA devices, the SCI core will
186 * automagically assign a SAS address to the end device
187 * for the purpose of creating a port. This SAS address
188 * will not be the same as assigned to the PHY and needs
189 * to be obtained from struct scic_port_properties properties.
190 */
Dan Williams150fc6f2011-02-25 10:25:21 -0800191 attached_sas_address = properties.remote.sas_address.high;
192 attached_sas_address <<= 32;
193 attached_sas_address |= properties.remote.sas_address.low;
194 swab64s(&attached_sas_address);
Dan Williams6f231dd2011-07-02 22:56:22 -0700195
Dan Williams150fc6f2011-02-25 10:25:21 -0800196 memcpy(&isci_phy->sas_phy.attached_sas_addr,
197 &attached_sas_address, sizeof(attached_sas_address));
Dave Jiangd7b90fc2011-05-04 18:22:33 -0700198 } else if (phy->protocol == SCIC_SDS_PHY_PROTOCOL_SAS) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700199 isci_phy->sas_phy.oob_mode = SAS_OOB_MODE;
Dave Jiang4b7ebd02011-05-04 15:37:52 -0700200 isci_phy->sas_phy.frame_rcvd_size = sizeof(struct sas_identify_frame);
Dan Williams6f231dd2011-07-02 22:56:22 -0700201
202 /* Copy the attached SAS address from the IAF */
203 memcpy(isci_phy->sas_phy.attached_sas_addr,
Dave Jiang4b7ebd02011-05-04 15:37:52 -0700204 isci_phy->frame_rcvd.iaf.sas_addr, SAS_ADDR_SIZE);
Dan Williams6f231dd2011-07-02 22:56:22 -0700205 } else {
206 dev_err(&isci_host->pdev->dev, "%s: unkown target\n", __func__);
207 success = false;
208 }
209
Dan Williams83e51432011-02-18 09:25:13 -0800210 isci_phy->sas_phy.phy->negotiated_linkrate = sci_phy_linkrate(phy);
211
Dan Williams6f231dd2011-07-02 22:56:22 -0700212 spin_unlock_irqrestore(&isci_phy->sas_phy.frame_rcvd_lock, flags);
213
214 /* Notify libsas that we have an address frame, if indeed
215 * we've found an SSP, SMP, or STP target */
216 if (success)
217 isci_host->sas_ha.notify_port_event(&isci_phy->sas_phy,
218 PORTE_BYTES_DMAED);
219}
220
221
222/**
223 * isci_port_link_down() - This function is called by the sci core when a link
224 * becomes inactive.
225 * @isci_host: This parameter specifies the isci host object.
226 * @phy: This parameter specifies the isci phy with the active link.
227 * @port: This parameter specifies the isci port with the active link.
228 *
229 */
Dave Jiang09d7da12011-03-26 16:11:51 -0700230void isci_port_link_down(struct isci_host *isci_host, struct isci_phy *isci_phy,
231 struct isci_port *isci_port)
Dan Williams6f231dd2011-07-02 22:56:22 -0700232{
233 struct isci_remote_device *isci_device;
234
235 dev_dbg(&isci_host->pdev->dev,
236 "%s: isci_port = %p\n", __func__, isci_port);
237
238 if (isci_port) {
239
240 /* check to see if this is the last phy on this port. */
241 if (isci_phy->sas_phy.port
242 && isci_phy->sas_phy.port->num_phys == 1) {
243
244 /* change the state for all devices on this port.
245 * The next task sent to this device will be returned
246 * as SAS_TASK_UNDELIVERED, and the scsi mid layer
247 * will remove the target
248 */
249 list_for_each_entry(isci_device,
250 &isci_port->remote_dev_list,
251 node) {
252 dev_dbg(&isci_host->pdev->dev,
253 "%s: isci_device = %p\n",
254 __func__, isci_device);
255 isci_remote_device_change_state(isci_device,
256 isci_stopping);
257 }
258 }
259 isci_port_change_state(isci_port, isci_stopping);
260 }
261
262 /* Notify libsas of the borken link, this will trigger calls to our
263 * isci_port_deformed and isci_dev_gone functions.
264 */
265 sas_phy_disconnected(&isci_phy->sas_phy);
266 isci_host->sas_ha.notify_phy_event(&isci_phy->sas_phy,
267 PHYE_LOSS_OF_SIGNAL);
268
269 isci_phy->isci_port = NULL;
270
271 dev_dbg(&isci_host->pdev->dev,
272 "%s: isci_port = %p - Done\n", __func__, isci_port);
273}
274
275
276/**
277 * isci_port_deformed() - This function is called by libsas when a port becomes
278 * inactive.
279 * @phy: This parameter specifies the libsas phy with the inactive port.
280 *
281 */
282void isci_port_deformed(
283 struct asd_sas_phy *phy)
284{
285 pr_debug("%s: sas_phy = %p\n", __func__, phy);
286}
287
288/**
289 * isci_port_formed() - This function is called by libsas when a port becomes
290 * active.
291 * @phy: This parameter specifies the libsas phy with the active port.
292 *
293 */
294void isci_port_formed(
295 struct asd_sas_phy *phy)
296{
297 pr_debug("%s: sas_phy = %p, sas_port = %p\n", __func__, phy, phy->port);
298}
299
300/**
301 * isci_port_ready() - This function is called by the sci core when a link
302 * becomes ready.
303 * @isci_host: This parameter specifies the isci host object.
304 * @port: This parameter specifies the sci port with the active link.
305 *
306 */
Dave Jiang09d7da12011-03-26 16:11:51 -0700307void isci_port_ready(struct isci_host *isci_host, struct isci_port *isci_port)
Dan Williams6f231dd2011-07-02 22:56:22 -0700308{
309 dev_dbg(&isci_host->pdev->dev,
310 "%s: isci_port = %p\n", __func__, isci_port);
311
312 complete_all(&isci_port->start_complete);
313 isci_port_change_state(isci_port, isci_ready);
314 return;
315}
316
317/**
318 * isci_port_not_ready() - This function is called by the sci core when a link
319 * is not ready. All remote devices on this link will be removed if they are
320 * in the stopping state.
321 * @isci_host: This parameter specifies the isci host object.
322 * @port: This parameter specifies the sci port with the active link.
323 *
324 */
Dave Jiang09d7da12011-03-26 16:11:51 -0700325void isci_port_not_ready(struct isci_host *isci_host, struct isci_port *isci_port)
Dan Williams6f231dd2011-07-02 22:56:22 -0700326{
327 dev_dbg(&isci_host->pdev->dev,
328 "%s: isci_port = %p\n", __func__, isci_port);
329}
330
331/**
332 * isci_port_hard_reset_complete() - This function is called by the sci core
333 * when the hard reset complete notification has been received.
334 * @port: This parameter specifies the sci port with the active link.
335 * @completion_status: This parameter specifies the core status for the reset
336 * process.
337 *
338 */
Dave Jiang09d7da12011-03-26 16:11:51 -0700339void isci_port_hard_reset_complete(struct isci_port *isci_port,
340 enum sci_status completion_status)
Dan Williams6f231dd2011-07-02 22:56:22 -0700341{
342 dev_dbg(&isci_port->isci_host->pdev->dev,
343 "%s: isci_port = %p, completion_status=%x\n",
344 __func__, isci_port, completion_status);
345
346 /* Save the status of the hard reset from the port. */
347 isci_port->hard_reset_status = completion_status;
348
349 complete_all(&isci_port->hard_reset_complete);
350}
Dan Williams4393aa42011-03-31 13:10:44 -0700351
352int isci_port_perform_hard_reset(struct isci_host *ihost, struct isci_port *iport,
353 struct isci_phy *iphy)
Dan Williams6f231dd2011-07-02 22:56:22 -0700354{
Dan Williams4393aa42011-03-31 13:10:44 -0700355 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -0700356 enum sci_status status;
357 int ret = TMF_RESP_FUNC_COMPLETE;
Dan Williams6f231dd2011-07-02 22:56:22 -0700358
Dan Williams4393aa42011-03-31 13:10:44 -0700359 dev_dbg(&ihost->pdev->dev, "%s: iport = %p\n",
360 __func__, iport);
Dan Williams6f231dd2011-07-02 22:56:22 -0700361
Dan Williams4393aa42011-03-31 13:10:44 -0700362 init_completion(&iport->hard_reset_complete);
Dan Williams6f231dd2011-07-02 22:56:22 -0700363
Dan Williams4393aa42011-03-31 13:10:44 -0700364 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700365
366 #define ISCI_PORT_RESET_TIMEOUT SCIC_SDS_SIGNATURE_FIS_TIMEOUT
Dan Williams4393aa42011-03-31 13:10:44 -0700367 status = scic_port_hard_reset(iport->sci_port_handle,
Dan Williams6f231dd2011-07-02 22:56:22 -0700368 ISCI_PORT_RESET_TIMEOUT);
369
Dan Williams4393aa42011-03-31 13:10:44 -0700370 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700371
372 if (status == SCI_SUCCESS) {
Dan Williams4393aa42011-03-31 13:10:44 -0700373 wait_for_completion(&iport->hard_reset_complete);
Dan Williams6f231dd2011-07-02 22:56:22 -0700374
Dan Williams4393aa42011-03-31 13:10:44 -0700375 dev_dbg(&ihost->pdev->dev,
376 "%s: iport = %p; hard reset completion\n",
377 __func__, iport);
Dan Williams6f231dd2011-07-02 22:56:22 -0700378
Dan Williams4393aa42011-03-31 13:10:44 -0700379 if (iport->hard_reset_status != SCI_SUCCESS)
Dan Williams6f231dd2011-07-02 22:56:22 -0700380 ret = TMF_RESP_FUNC_FAILED;
381 } else {
382 ret = TMF_RESP_FUNC_FAILED;
383
Dan Williams4393aa42011-03-31 13:10:44 -0700384 dev_err(&ihost->pdev->dev,
385 "%s: iport = %p; scic_port_hard_reset call"
Dan Williams6f231dd2011-07-02 22:56:22 -0700386 " failed 0x%x\n",
Dan Williams4393aa42011-03-31 13:10:44 -0700387 __func__, iport, status);
Dan Williams6f231dd2011-07-02 22:56:22 -0700388
389 }
390
391 /* If the hard reset for the port has failed, consider this
392 * the same as link failures on all phys in the port.
393 */
394 if (ret != TMF_RESP_FUNC_COMPLETE) {
Dan Williams4393aa42011-03-31 13:10:44 -0700395 dev_err(&ihost->pdev->dev,
396 "%s: iport = %p; hard reset failed "
Dan Williams6f231dd2011-07-02 22:56:22 -0700397 "(0x%x) - sending link down to libsas for phy %p\n",
Dan Williams4393aa42011-03-31 13:10:44 -0700398 __func__, iport, iport->hard_reset_status, iphy);
Dan Williams6f231dd2011-07-02 22:56:22 -0700399
Dan Williams4393aa42011-03-31 13:10:44 -0700400 isci_port_link_down(ihost, iphy, iport);
Dan Williams6f231dd2011-07-02 22:56:22 -0700401 }
402
403 return ret;
404}
Dave Jiang09d7da12011-03-26 16:11:51 -0700405
Dave Jiang09d7da12011-03-26 16:11:51 -0700406void isci_port_stop_complete(struct scic_sds_controller *scic,
407 struct scic_sds_port *sci_port,
408 enum sci_status completion_status)
409{
Artur Wojcikcc3dbd02011-05-04 07:58:16 +0000410 dev_dbg(&scic_to_ihost(scic)->pdev->dev, "Port stop complete\n");
Dave Jiang09d7da12011-03-26 16:11:51 -0700411}