blob: cf78cf0a6749ea7d5d6a150754de8b979d11760f [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"
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
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);
103 sci_object_set_association(scic_port, isci_port);
104 isci_port->sci_port_handle = scic_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 */
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{
140 struct isci_phy *isci_phy =
141 (struct isci_phy *)sci_object_get_association(phy);
142
143 dev_dbg(&isci_host->pdev->dev,
144 "%s: isci_phy = %p, sas_phy = %p\n",
145 __func__,
146 isci_phy,
147 &isci_phy->sas_phy);
148
149 isci_host->sas_ha.notify_port_event(
150 &isci_phy->sas_phy,
151 PORTE_BROADCAST_RCVD
152 );
153
154 scic_port_enable_broadcast_change_notification(port);
155}
156
157/**
158 * isci_port_link_up() - This function is called by the sci core when a link
159 * becomes active. the identify address frame is retrieved from the core and
160 * a notify port event is sent to libsas.
161 * @isci_host: This parameter specifies the isci host object.
162 * @port: This parameter specifies the sci port with the active link.
163 * @phy: This parameter specifies the sci phy with the active link.
164 *
165 */
166void isci_port_link_up(
167 struct isci_host *isci_host,
168 struct scic_sds_port *port,
169 struct scic_sds_phy *phy)
170{
171 unsigned long flags;
172 struct scic_port_properties properties;
173 struct isci_phy *isci_phy
174 = (struct isci_phy *)sci_object_get_association(phy);
175 struct isci_port *isci_port
176 = (struct isci_port *)sci_object_get_association(port);
177 enum sci_status call_status;
178 unsigned long success = true;
179
180 BUG_ON(isci_phy->isci_port != NULL);
Bartosz Barcinski6cb4d6b2011-04-12 17:28:43 -0700181
Dan Williams6f231dd2011-07-02 22:56:22 -0700182 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) {
Dan Williams150fc6f2011-02-25 10:25:21 -0800195 u64 attached_sas_address;
Dan Williams6f231dd2011-07-02 22:56:22 -0700196
197 struct scic_sata_phy_properties sata_phy_properties;
198
199 isci_phy->sas_phy.oob_mode = SATA_OOB_MODE;
200
201 /* Get a copy of the signature fis for libsas */
202 call_status = scic_sata_phy_get_properties(phy,
203 &sata_phy_properties);
204
Dan Williams35173d52011-03-26 16:43:01 -0700205 /*
Dan Williams6f231dd2011-07-02 22:56:22 -0700206 * XXX I am concerned about this "assert". shouldn't we
207 * handle the return appropriately?
208 */
209 BUG_ON(call_status != SCI_SUCCESS);
210
211 memcpy(isci_phy->frame_rcvd.fis,
212 &sata_phy_properties.signature_fis,
213 sizeof(struct sata_fis_reg_d2h));
214
215 isci_phy->sas_phy.frame_rcvd_size = sizeof(struct sata_fis_reg_d2h);
216
217 /*
218 * For direct-attached SATA devices, the SCI core will
219 * automagically assign a SAS address to the end device
220 * for the purpose of creating a port. This SAS address
221 * will not be the same as assigned to the PHY and needs
222 * to be obtained from struct scic_port_properties properties.
223 */
Dan Williams150fc6f2011-02-25 10:25:21 -0800224 attached_sas_address = properties.remote.sas_address.high;
225 attached_sas_address <<= 32;
226 attached_sas_address |= properties.remote.sas_address.low;
227 swab64s(&attached_sas_address);
Dan Williams6f231dd2011-07-02 22:56:22 -0700228
Dan Williams150fc6f2011-02-25 10:25:21 -0800229 memcpy(&isci_phy->sas_phy.attached_sas_addr,
230 &attached_sas_address, sizeof(attached_sas_address));
Dan Williams6f231dd2011-07-02 22:56:22 -0700231
232 } else if (properties.remote.protocols.u.bits.ssp_target ||
233 properties.remote.protocols.u.bits.smp_target) {
234
235 struct scic_sas_phy_properties sas_phy_properties;
236
237 isci_phy->sas_phy.oob_mode = SAS_OOB_MODE;
238
239 /* Get a copy of the identify address frame for libsas */
240 call_status = scic_sas_phy_get_properties(phy,
241 &sas_phy_properties);
242
243 BUG_ON(call_status != SCI_SUCCESS);
244
245 memcpy(isci_phy->frame_rcvd.aif,
246 &(sas_phy_properties.received_iaf),
247 sizeof(struct sci_sas_identify_address_frame));
248
249 isci_phy->sas_phy.frame_rcvd_size
250 = sizeof(struct sci_sas_identify_address_frame);
251
252 /* Copy the attached SAS address from the IAF */
253 memcpy(isci_phy->sas_phy.attached_sas_addr,
254 ((struct sas_identify_frame *)
255 (&isci_phy->frame_rcvd.aif))->sas_addr,
256 SAS_ADDR_SIZE);
257
258 } else {
259 dev_err(&isci_host->pdev->dev, "%s: unkown target\n", __func__);
260 success = false;
261 }
262
Dan Williams83e51432011-02-18 09:25:13 -0800263 isci_phy->sas_phy.phy->negotiated_linkrate = sci_phy_linkrate(phy);
264
Dan Williams6f231dd2011-07-02 22:56:22 -0700265 spin_unlock_irqrestore(&isci_phy->sas_phy.frame_rcvd_lock, flags);
266
267 /* Notify libsas that we have an address frame, if indeed
268 * we've found an SSP, SMP, or STP target */
269 if (success)
270 isci_host->sas_ha.notify_port_event(&isci_phy->sas_phy,
271 PORTE_BYTES_DMAED);
272}
273
274
275/**
276 * isci_port_link_down() - This function is called by the sci core when a link
277 * becomes inactive.
278 * @isci_host: This parameter specifies the isci host object.
279 * @phy: This parameter specifies the isci phy with the active link.
280 * @port: This parameter specifies the isci port with the active link.
281 *
282 */
Dave Jiang09d7da12011-03-26 16:11:51 -0700283void isci_port_link_down(struct isci_host *isci_host, struct isci_phy *isci_phy,
284 struct isci_port *isci_port)
Dan Williams6f231dd2011-07-02 22:56:22 -0700285{
286 struct isci_remote_device *isci_device;
287
288 dev_dbg(&isci_host->pdev->dev,
289 "%s: isci_port = %p\n", __func__, isci_port);
290
291 if (isci_port) {
292
293 /* check to see if this is the last phy on this port. */
294 if (isci_phy->sas_phy.port
295 && isci_phy->sas_phy.port->num_phys == 1) {
296
297 /* change the state for all devices on this port.
298 * The next task sent to this device will be returned
299 * as SAS_TASK_UNDELIVERED, and the scsi mid layer
300 * will remove the target
301 */
302 list_for_each_entry(isci_device,
303 &isci_port->remote_dev_list,
304 node) {
305 dev_dbg(&isci_host->pdev->dev,
306 "%s: isci_device = %p\n",
307 __func__, isci_device);
308 isci_remote_device_change_state(isci_device,
309 isci_stopping);
310 }
311 }
312 isci_port_change_state(isci_port, isci_stopping);
313 }
314
315 /* Notify libsas of the borken link, this will trigger calls to our
316 * isci_port_deformed and isci_dev_gone functions.
317 */
318 sas_phy_disconnected(&isci_phy->sas_phy);
319 isci_host->sas_ha.notify_phy_event(&isci_phy->sas_phy,
320 PHYE_LOSS_OF_SIGNAL);
321
322 isci_phy->isci_port = NULL;
323
324 dev_dbg(&isci_host->pdev->dev,
325 "%s: isci_port = %p - Done\n", __func__, isci_port);
326}
327
328
329/**
330 * isci_port_deformed() - This function is called by libsas when a port becomes
331 * inactive.
332 * @phy: This parameter specifies the libsas phy with the inactive port.
333 *
334 */
335void isci_port_deformed(
336 struct asd_sas_phy *phy)
337{
338 pr_debug("%s: sas_phy = %p\n", __func__, phy);
339}
340
341/**
342 * isci_port_formed() - This function is called by libsas when a port becomes
343 * active.
344 * @phy: This parameter specifies the libsas phy with the active port.
345 *
346 */
347void isci_port_formed(
348 struct asd_sas_phy *phy)
349{
350 pr_debug("%s: sas_phy = %p, sas_port = %p\n", __func__, phy, phy->port);
351}
352
353/**
354 * isci_port_ready() - This function is called by the sci core when a link
355 * becomes ready.
356 * @isci_host: This parameter specifies the isci host object.
357 * @port: This parameter specifies the sci port with the active link.
358 *
359 */
Dave Jiang09d7da12011-03-26 16:11:51 -0700360void isci_port_ready(struct isci_host *isci_host, struct isci_port *isci_port)
Dan Williams6f231dd2011-07-02 22:56:22 -0700361{
362 dev_dbg(&isci_host->pdev->dev,
363 "%s: isci_port = %p\n", __func__, isci_port);
364
365 complete_all(&isci_port->start_complete);
366 isci_port_change_state(isci_port, isci_ready);
367 return;
368}
369
370/**
371 * isci_port_not_ready() - This function is called by the sci core when a link
372 * is not ready. All remote devices on this link will be removed if they are
373 * in the stopping state.
374 * @isci_host: This parameter specifies the isci host object.
375 * @port: This parameter specifies the sci port with the active link.
376 *
377 */
Dave Jiang09d7da12011-03-26 16:11:51 -0700378void isci_port_not_ready(struct isci_host *isci_host, struct isci_port *isci_port)
Dan Williams6f231dd2011-07-02 22:56:22 -0700379{
380 dev_dbg(&isci_host->pdev->dev,
381 "%s: isci_port = %p\n", __func__, isci_port);
382}
383
384/**
385 * isci_port_hard_reset_complete() - This function is called by the sci core
386 * when the hard reset complete notification has been received.
387 * @port: This parameter specifies the sci port with the active link.
388 * @completion_status: This parameter specifies the core status for the reset
389 * process.
390 *
391 */
Dave Jiang09d7da12011-03-26 16:11:51 -0700392void isci_port_hard_reset_complete(struct isci_port *isci_port,
393 enum sci_status completion_status)
Dan Williams6f231dd2011-07-02 22:56:22 -0700394{
395 dev_dbg(&isci_port->isci_host->pdev->dev,
396 "%s: isci_port = %p, completion_status=%x\n",
397 __func__, isci_port, completion_status);
398
399 /* Save the status of the hard reset from the port. */
400 isci_port->hard_reset_status = completion_status;
401
402 complete_all(&isci_port->hard_reset_complete);
403}
Dan Williams4393aa42011-03-31 13:10:44 -0700404
405int isci_port_perform_hard_reset(struct isci_host *ihost, struct isci_port *iport,
406 struct isci_phy *iphy)
Dan Williams6f231dd2011-07-02 22:56:22 -0700407{
Dan Williams4393aa42011-03-31 13:10:44 -0700408 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -0700409 enum sci_status status;
410 int ret = TMF_RESP_FUNC_COMPLETE;
Dan Williams6f231dd2011-07-02 22:56:22 -0700411
Dan Williams4393aa42011-03-31 13:10:44 -0700412 dev_dbg(&ihost->pdev->dev, "%s: iport = %p\n",
413 __func__, iport);
Dan Williams6f231dd2011-07-02 22:56:22 -0700414
Dan Williams4393aa42011-03-31 13:10:44 -0700415 init_completion(&iport->hard_reset_complete);
Dan Williams6f231dd2011-07-02 22:56:22 -0700416
Dan Williams4393aa42011-03-31 13:10:44 -0700417 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700418
419 #define ISCI_PORT_RESET_TIMEOUT SCIC_SDS_SIGNATURE_FIS_TIMEOUT
Dan Williams4393aa42011-03-31 13:10:44 -0700420 status = scic_port_hard_reset(iport->sci_port_handle,
Dan Williams6f231dd2011-07-02 22:56:22 -0700421 ISCI_PORT_RESET_TIMEOUT);
422
Dan Williams4393aa42011-03-31 13:10:44 -0700423 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700424
425 if (status == SCI_SUCCESS) {
Dan Williams4393aa42011-03-31 13:10:44 -0700426 wait_for_completion(&iport->hard_reset_complete);
Dan Williams6f231dd2011-07-02 22:56:22 -0700427
Dan Williams4393aa42011-03-31 13:10:44 -0700428 dev_dbg(&ihost->pdev->dev,
429 "%s: iport = %p; hard reset completion\n",
430 __func__, iport);
Dan Williams6f231dd2011-07-02 22:56:22 -0700431
Dan Williams4393aa42011-03-31 13:10:44 -0700432 if (iport->hard_reset_status != SCI_SUCCESS)
Dan Williams6f231dd2011-07-02 22:56:22 -0700433 ret = TMF_RESP_FUNC_FAILED;
434 } else {
435 ret = TMF_RESP_FUNC_FAILED;
436
Dan Williams4393aa42011-03-31 13:10:44 -0700437 dev_err(&ihost->pdev->dev,
438 "%s: iport = %p; scic_port_hard_reset call"
Dan Williams6f231dd2011-07-02 22:56:22 -0700439 " failed 0x%x\n",
Dan Williams4393aa42011-03-31 13:10:44 -0700440 __func__, iport, status);
Dan Williams6f231dd2011-07-02 22:56:22 -0700441
442 }
443
444 /* If the hard reset for the port has failed, consider this
445 * the same as link failures on all phys in the port.
446 */
447 if (ret != TMF_RESP_FUNC_COMPLETE) {
Dan Williams4393aa42011-03-31 13:10:44 -0700448 dev_err(&ihost->pdev->dev,
449 "%s: iport = %p; hard reset failed "
Dan Williams6f231dd2011-07-02 22:56:22 -0700450 "(0x%x) - sending link down to libsas for phy %p\n",
Dan Williams4393aa42011-03-31 13:10:44 -0700451 __func__, iport, iport->hard_reset_status, iphy);
Dan Williams6f231dd2011-07-02 22:56:22 -0700452
Dan Williams4393aa42011-03-31 13:10:44 -0700453 isci_port_link_down(ihost, iphy, iport);
Dan Williams6f231dd2011-07-02 22:56:22 -0700454 }
455
456 return ret;
457}
Dave Jiang09d7da12011-03-26 16:11:51 -0700458
459/**
460 * isci_port_invalid_link_up() - This function informs the SCI Core user that
461 * a phy/link became ready, but the phy is not allowed in the port. In some
462 * situations the underlying hardware only allows for certain phy to port
463 * mappings. If these mappings are violated, then this API is invoked.
464 * @controller: This parameter represents the controller which contains the
465 * port.
466 * @port: This parameter specifies the SCI port object for which the callback
467 * is being invoked.
468 * @phy: This parameter specifies the phy that came ready, but the phy can't be
469 * a valid member of the port.
470 *
471 */
472void isci_port_invalid_link_up(struct scic_sds_controller *scic,
473 struct scic_sds_port *sci_port,
474 struct scic_sds_phy *phy)
475{
Dan Williams4393aa42011-03-31 13:10:44 -0700476 struct isci_host *ihost = sci_object_get_association(scic);
Dave Jiang09d7da12011-03-26 16:11:51 -0700477
478 dev_warn(&ihost->pdev->dev, "Invalid link up!\n");
479}
480
481void isci_port_stop_complete(struct scic_sds_controller *scic,
482 struct scic_sds_port *sci_port,
483 enum sci_status completion_status)
484{
485 struct isci_host *ihost = sci_object_get_association(scic);
486
487 dev_dbg(&ihost->pdev->dev, "Port stop complete\n");
488}