blob: 5b38f2f963e09575f45dfa05fc26774d7898c9ea [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
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 */
87void 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);
Dan Williams6f231dd2011-07-02 22:56:22 -070097 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);
Dan Williams6f231dd2011-07-02 22:56:22 -0700103 isci_port->sci_port_handle = scic_port;
Maciej Patelczyk115bd1f2011-04-28 22:06:16 +0000104 scic_port->iport = isci_port;
Dan Williams6f231dd2011-07-02 22:56:22 -0700105}
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 */
114enum isci_status isci_port_get_state(
115 struct isci_port *isci_port)
116{
117 return isci_port->status;
118}
119
120static 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
135void isci_port_bc_change_received(
136 struct isci_host *isci_host,
137 struct scic_sds_port *port,
138 struct scic_sds_phy *phy)
139{
Maciej Patelczyke1e72a02011-04-28 22:06:11 +0000140 struct isci_phy *isci_phy = phy->iphy;
Dan Williams6f231dd2011-07-02 22:56:22 -0700141
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 */
165void 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;
Maciej Patelczyke1e72a02011-04-28 22:06:11 +0000172 struct isci_phy *isci_phy = phy->iphy;
Maciej Patelczyk115bd1f2011-04-28 22:06:16 +0000173 struct isci_port *isci_port = port->iport;
Dan Williams6f231dd2011-07-02 22:56:22 -0700174 enum sci_status call_status;
175 unsigned long success = true;
176
177 BUG_ON(isci_phy->isci_port != NULL);
Bartosz Barcinski6cb4d6b2011-04-12 17:28:43 -0700178
Dan Williams6f231dd2011-07-02 22:56:22 -0700179 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) {
Dan Williams150fc6f2011-02-25 10:25:21 -0800192 u64 attached_sas_address;
Dan Williams6f231dd2011-07-02 22:56:22 -0700193
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
Dan Williams35173d52011-03-26 16:43:01 -0700202 /*
Dan Williams6f231dd2011-07-02 22:56:22 -0700203 * XXX I am concerned about this "assert". shouldn't we
204 * handle the return appropriately?
205 */
206 BUG_ON(call_status != SCI_SUCCESS);
207
Dave Jiangf2f30082011-05-04 15:02:02 -0700208 isci_phy->frame_rcvd.fis = sata_phy_properties.signature_fis;
209 isci_phy->sas_phy.frame_rcvd_size = sizeof(struct dev_to_host_fis);
Dan Williams6f231dd2011-07-02 22:56:22 -0700210
211 /*
212 * For direct-attached SATA devices, the SCI core will
213 * automagically assign a SAS address to the end device
214 * for the purpose of creating a port. This SAS address
215 * will not be the same as assigned to the PHY and needs
216 * to be obtained from struct scic_port_properties properties.
217 */
Dan Williams150fc6f2011-02-25 10:25:21 -0800218 attached_sas_address = properties.remote.sas_address.high;
219 attached_sas_address <<= 32;
220 attached_sas_address |= properties.remote.sas_address.low;
221 swab64s(&attached_sas_address);
Dan Williams6f231dd2011-07-02 22:56:22 -0700222
Dan Williams150fc6f2011-02-25 10:25:21 -0800223 memcpy(&isci_phy->sas_phy.attached_sas_addr,
224 &attached_sas_address, sizeof(attached_sas_address));
Dan Williams6f231dd2011-07-02 22:56:22 -0700225
226 } else if (properties.remote.protocols.u.bits.ssp_target ||
227 properties.remote.protocols.u.bits.smp_target) {
228
229 struct scic_sas_phy_properties sas_phy_properties;
230
231 isci_phy->sas_phy.oob_mode = SAS_OOB_MODE;
232
233 /* Get a copy of the identify address frame for libsas */
234 call_status = scic_sas_phy_get_properties(phy,
235 &sas_phy_properties);
236
237 BUG_ON(call_status != SCI_SUCCESS);
238
239 memcpy(isci_phy->frame_rcvd.aif,
240 &(sas_phy_properties.received_iaf),
241 sizeof(struct sci_sas_identify_address_frame));
242
243 isci_phy->sas_phy.frame_rcvd_size
244 = sizeof(struct sci_sas_identify_address_frame);
245
246 /* Copy the attached SAS address from the IAF */
247 memcpy(isci_phy->sas_phy.attached_sas_addr,
248 ((struct sas_identify_frame *)
249 (&isci_phy->frame_rcvd.aif))->sas_addr,
250 SAS_ADDR_SIZE);
251
252 } else {
253 dev_err(&isci_host->pdev->dev, "%s: unkown target\n", __func__);
254 success = false;
255 }
256
Dan Williams83e51432011-02-18 09:25:13 -0800257 isci_phy->sas_phy.phy->negotiated_linkrate = sci_phy_linkrate(phy);
258
Dan Williams6f231dd2011-07-02 22:56:22 -0700259 spin_unlock_irqrestore(&isci_phy->sas_phy.frame_rcvd_lock, flags);
260
261 /* Notify libsas that we have an address frame, if indeed
262 * we've found an SSP, SMP, or STP target */
263 if (success)
264 isci_host->sas_ha.notify_port_event(&isci_phy->sas_phy,
265 PORTE_BYTES_DMAED);
266}
267
268
269/**
270 * isci_port_link_down() - This function is called by the sci core when a link
271 * becomes inactive.
272 * @isci_host: This parameter specifies the isci host object.
273 * @phy: This parameter specifies the isci phy with the active link.
274 * @port: This parameter specifies the isci port with the active link.
275 *
276 */
Dave Jiang09d7da12011-03-26 16:11:51 -0700277void isci_port_link_down(struct isci_host *isci_host, struct isci_phy *isci_phy,
278 struct isci_port *isci_port)
Dan Williams6f231dd2011-07-02 22:56:22 -0700279{
280 struct isci_remote_device *isci_device;
281
282 dev_dbg(&isci_host->pdev->dev,
283 "%s: isci_port = %p\n", __func__, isci_port);
284
285 if (isci_port) {
286
287 /* check to see if this is the last phy on this port. */
288 if (isci_phy->sas_phy.port
289 && isci_phy->sas_phy.port->num_phys == 1) {
290
291 /* change the state for all devices on this port.
292 * The next task sent to this device will be returned
293 * as SAS_TASK_UNDELIVERED, and the scsi mid layer
294 * will remove the target
295 */
296 list_for_each_entry(isci_device,
297 &isci_port->remote_dev_list,
298 node) {
299 dev_dbg(&isci_host->pdev->dev,
300 "%s: isci_device = %p\n",
301 __func__, isci_device);
302 isci_remote_device_change_state(isci_device,
303 isci_stopping);
304 }
305 }
306 isci_port_change_state(isci_port, isci_stopping);
307 }
308
309 /* Notify libsas of the borken link, this will trigger calls to our
310 * isci_port_deformed and isci_dev_gone functions.
311 */
312 sas_phy_disconnected(&isci_phy->sas_phy);
313 isci_host->sas_ha.notify_phy_event(&isci_phy->sas_phy,
314 PHYE_LOSS_OF_SIGNAL);
315
316 isci_phy->isci_port = NULL;
317
318 dev_dbg(&isci_host->pdev->dev,
319 "%s: isci_port = %p - Done\n", __func__, isci_port);
320}
321
322
323/**
324 * isci_port_deformed() - This function is called by libsas when a port becomes
325 * inactive.
326 * @phy: This parameter specifies the libsas phy with the inactive port.
327 *
328 */
329void isci_port_deformed(
330 struct asd_sas_phy *phy)
331{
332 pr_debug("%s: sas_phy = %p\n", __func__, phy);
333}
334
335/**
336 * isci_port_formed() - This function is called by libsas when a port becomes
337 * active.
338 * @phy: This parameter specifies the libsas phy with the active port.
339 *
340 */
341void isci_port_formed(
342 struct asd_sas_phy *phy)
343{
344 pr_debug("%s: sas_phy = %p, sas_port = %p\n", __func__, phy, phy->port);
345}
346
347/**
348 * isci_port_ready() - This function is called by the sci core when a link
349 * becomes ready.
350 * @isci_host: This parameter specifies the isci host object.
351 * @port: This parameter specifies the sci port with the active link.
352 *
353 */
Dave Jiang09d7da12011-03-26 16:11:51 -0700354void isci_port_ready(struct isci_host *isci_host, struct isci_port *isci_port)
Dan Williams6f231dd2011-07-02 22:56:22 -0700355{
356 dev_dbg(&isci_host->pdev->dev,
357 "%s: isci_port = %p\n", __func__, isci_port);
358
359 complete_all(&isci_port->start_complete);
360 isci_port_change_state(isci_port, isci_ready);
361 return;
362}
363
364/**
365 * isci_port_not_ready() - This function is called by the sci core when a link
366 * is not ready. All remote devices on this link will be removed if they are
367 * in the stopping state.
368 * @isci_host: This parameter specifies the isci host object.
369 * @port: This parameter specifies the sci port with the active link.
370 *
371 */
Dave Jiang09d7da12011-03-26 16:11:51 -0700372void isci_port_not_ready(struct isci_host *isci_host, struct isci_port *isci_port)
Dan Williams6f231dd2011-07-02 22:56:22 -0700373{
374 dev_dbg(&isci_host->pdev->dev,
375 "%s: isci_port = %p\n", __func__, isci_port);
376}
377
378/**
379 * isci_port_hard_reset_complete() - This function is called by the sci core
380 * when the hard reset complete notification has been received.
381 * @port: This parameter specifies the sci port with the active link.
382 * @completion_status: This parameter specifies the core status for the reset
383 * process.
384 *
385 */
Dave Jiang09d7da12011-03-26 16:11:51 -0700386void isci_port_hard_reset_complete(struct isci_port *isci_port,
387 enum sci_status completion_status)
Dan Williams6f231dd2011-07-02 22:56:22 -0700388{
389 dev_dbg(&isci_port->isci_host->pdev->dev,
390 "%s: isci_port = %p, completion_status=%x\n",
391 __func__, isci_port, completion_status);
392
393 /* Save the status of the hard reset from the port. */
394 isci_port->hard_reset_status = completion_status;
395
396 complete_all(&isci_port->hard_reset_complete);
397}
Dan Williams4393aa42011-03-31 13:10:44 -0700398
399int isci_port_perform_hard_reset(struct isci_host *ihost, struct isci_port *iport,
400 struct isci_phy *iphy)
Dan Williams6f231dd2011-07-02 22:56:22 -0700401{
Dan Williams4393aa42011-03-31 13:10:44 -0700402 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -0700403 enum sci_status status;
404 int ret = TMF_RESP_FUNC_COMPLETE;
Dan Williams6f231dd2011-07-02 22:56:22 -0700405
Dan Williams4393aa42011-03-31 13:10:44 -0700406 dev_dbg(&ihost->pdev->dev, "%s: iport = %p\n",
407 __func__, iport);
Dan Williams6f231dd2011-07-02 22:56:22 -0700408
Dan Williams4393aa42011-03-31 13:10:44 -0700409 init_completion(&iport->hard_reset_complete);
Dan Williams6f231dd2011-07-02 22:56:22 -0700410
Dan Williams4393aa42011-03-31 13:10:44 -0700411 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700412
413 #define ISCI_PORT_RESET_TIMEOUT SCIC_SDS_SIGNATURE_FIS_TIMEOUT
Dan Williams4393aa42011-03-31 13:10:44 -0700414 status = scic_port_hard_reset(iport->sci_port_handle,
Dan Williams6f231dd2011-07-02 22:56:22 -0700415 ISCI_PORT_RESET_TIMEOUT);
416
Dan Williams4393aa42011-03-31 13:10:44 -0700417 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700418
419 if (status == SCI_SUCCESS) {
Dan Williams4393aa42011-03-31 13:10:44 -0700420 wait_for_completion(&iport->hard_reset_complete);
Dan Williams6f231dd2011-07-02 22:56:22 -0700421
Dan Williams4393aa42011-03-31 13:10:44 -0700422 dev_dbg(&ihost->pdev->dev,
423 "%s: iport = %p; hard reset completion\n",
424 __func__, iport);
Dan Williams6f231dd2011-07-02 22:56:22 -0700425
Dan Williams4393aa42011-03-31 13:10:44 -0700426 if (iport->hard_reset_status != SCI_SUCCESS)
Dan Williams6f231dd2011-07-02 22:56:22 -0700427 ret = TMF_RESP_FUNC_FAILED;
428 } else {
429 ret = TMF_RESP_FUNC_FAILED;
430
Dan Williams4393aa42011-03-31 13:10:44 -0700431 dev_err(&ihost->pdev->dev,
432 "%s: iport = %p; scic_port_hard_reset call"
Dan Williams6f231dd2011-07-02 22:56:22 -0700433 " failed 0x%x\n",
Dan Williams4393aa42011-03-31 13:10:44 -0700434 __func__, iport, status);
Dan Williams6f231dd2011-07-02 22:56:22 -0700435
436 }
437
438 /* If the hard reset for the port has failed, consider this
439 * the same as link failures on all phys in the port.
440 */
441 if (ret != TMF_RESP_FUNC_COMPLETE) {
Dan Williams4393aa42011-03-31 13:10:44 -0700442 dev_err(&ihost->pdev->dev,
443 "%s: iport = %p; hard reset failed "
Dan Williams6f231dd2011-07-02 22:56:22 -0700444 "(0x%x) - sending link down to libsas for phy %p\n",
Dan Williams4393aa42011-03-31 13:10:44 -0700445 __func__, iport, iport->hard_reset_status, iphy);
Dan Williams6f231dd2011-07-02 22:56:22 -0700446
Dan Williams4393aa42011-03-31 13:10:44 -0700447 isci_port_link_down(ihost, iphy, iport);
Dan Williams6f231dd2011-07-02 22:56:22 -0700448 }
449
450 return ret;
451}
Dave Jiang09d7da12011-03-26 16:11:51 -0700452
453/**
454 * isci_port_invalid_link_up() - This function informs the SCI Core user that
455 * a phy/link became ready, but the phy is not allowed in the port. In some
456 * situations the underlying hardware only allows for certain phy to port
457 * mappings. If these mappings are violated, then this API is invoked.
458 * @controller: This parameter represents the controller which contains the
459 * port.
460 * @port: This parameter specifies the SCI port object for which the callback
461 * is being invoked.
462 * @phy: This parameter specifies the phy that came ready, but the phy can't be
463 * a valid member of the port.
464 *
465 */
466void isci_port_invalid_link_up(struct scic_sds_controller *scic,
467 struct scic_sds_port *sci_port,
468 struct scic_sds_phy *phy)
469{
Maciej Patelczykd3757c32011-04-28 22:06:06 +0000470 struct isci_host *ihost = scic->ihost;
Dave Jiang09d7da12011-03-26 16:11:51 -0700471
472 dev_warn(&ihost->pdev->dev, "Invalid link up!\n");
473}
474
475void isci_port_stop_complete(struct scic_sds_controller *scic,
476 struct scic_sds_port *sci_port,
477 enum sci_status completion_status)
478{
Maciej Patelczykd3757c32011-04-28 22:06:06 +0000479 struct isci_host *ihost = scic->ihost;
Dave Jiang09d7da12011-03-26 16:11:51 -0700480
481 dev_dbg(&ihost->pdev->dev, "Port stop complete\n");
482}