blob: d53c0b1748e712aeeb329921745eb75314f67892 [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
Dan Williams6f231dd2011-07-02 22:56:22 -070056#include "isci.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070057#include "port.h"
58#include "request.h"
Dan Williamse2f8db502011-05-10 02:28:46 -070059
60#define SCIC_SDS_PORT_HARD_RESET_TIMEOUT (1000)
61#define SCU_DUMMY_INDEX (0xFFFF)
Dan Williams6f231dd2011-07-02 22:56:22 -070062
Dan Williamse5313812011-05-07 10:11:43 -070063static void isci_port_change_state(struct isci_port *iport, enum isci_status status)
64{
65 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -070066
Dan Williamse5313812011-05-07 10:11:43 -070067 dev_dbg(&iport->isci_host->pdev->dev,
68 "%s: iport = %p, state = 0x%x\n",
69 __func__, iport, status);
Dan Williams6f231dd2011-07-02 22:56:22 -070070
Dan Williamse5313812011-05-07 10:11:43 -070071 /* XXX pointless lock */
72 spin_lock_irqsave(&iport->state_lock, flags);
73 iport->status = status;
74 spin_unlock_irqrestore(&iport->state_lock, flags);
75}
Dan Williams6f231dd2011-07-02 22:56:22 -070076
Dan Williamse2f8db502011-05-10 02:28:46 -070077/*
78 * This function will indicate which protocols are supported by this port.
79 * @sci_port: a handle corresponding to the SAS port for which to return the
80 * supported protocols.
81 * @protocols: This parameter specifies a pointer to a data structure
82 * which the core will copy the protocol values for the port from the
83 * transmit_identification register.
84 */
85static void
86scic_sds_port_get_protocols(struct scic_sds_port *sci_port,
87 struct scic_phy_proto *protocols)
Dan Williams6f231dd2011-07-02 22:56:22 -070088{
Dan Williamse2f8db502011-05-10 02:28:46 -070089 u8 index;
90
91 protocols->all = 0;
92
93 for (index = 0; index < SCI_MAX_PHYS; index++) {
94 if (sci_port->phy_table[index] != NULL) {
95 scic_sds_phy_get_protocols(sci_port->phy_table[index],
96 protocols);
97 }
98 }
Dan Williams6f231dd2011-07-02 22:56:22 -070099}
100
Dan Williams6f231dd2011-07-02 22:56:22 -0700101/**
Dan Williamse2f8db502011-05-10 02:28:46 -0700102 * This method requests a list (mask) of the phys contained in the supplied SAS
103 * port.
104 * @sci_port: a handle corresponding to the SAS port for which to return the
105 * phy mask.
Dan Williams6f231dd2011-07-02 22:56:22 -0700106 *
Dan Williamse2f8db502011-05-10 02:28:46 -0700107 * Return a bit mask indicating which phys are a part of this port. Each bit
108 * corresponds to a phy identifier (e.g. bit 0 = phy id 0).
Dan Williams6f231dd2011-07-02 22:56:22 -0700109 */
Dan Williamse2f8db502011-05-10 02:28:46 -0700110static u32 scic_sds_port_get_phys(struct scic_sds_port *sci_port)
Dan Williams6f231dd2011-07-02 22:56:22 -0700111{
Dan Williamse2f8db502011-05-10 02:28:46 -0700112 u32 index;
113 u32 mask;
114
115 mask = 0;
116
117 for (index = 0; index < SCI_MAX_PHYS; index++) {
118 if (sci_port->phy_table[index] != NULL) {
119 mask |= (1 << index);
120 }
121 }
122
123 return mask;
Dan Williams6f231dd2011-07-02 22:56:22 -0700124}
125
Dan Williamse2f8db502011-05-10 02:28:46 -0700126/**
127 * scic_port_get_properties() - This method simply returns the properties
128 * regarding the port, such as: physical index, protocols, sas address, etc.
129 * @port: this parameter specifies the port for which to retrieve the physical
130 * index.
131 * @properties: This parameter specifies the properties structure into which to
132 * copy the requested information.
133 *
134 * Indicate if the user specified a valid port. SCI_SUCCESS This value is
135 * returned if the specified port was valid. SCI_FAILURE_INVALID_PORT This
136 * value is returned if the specified port is not valid. When this value is
137 * returned, no data is copied to the properties output parameter.
138 */
139static enum sci_status scic_port_get_properties(struct scic_sds_port *port,
140 struct scic_port_properties *prop)
Dan Williams6f231dd2011-07-02 22:56:22 -0700141{
Dan Williamse2f8db502011-05-10 02:28:46 -0700142 if ((port == NULL) ||
143 (port->logical_port_index == SCIC_SDS_DUMMY_PORT))
144 return SCI_FAILURE_INVALID_PORT;
Dan Williams6f231dd2011-07-02 22:56:22 -0700145
Dan Williamse2f8db502011-05-10 02:28:46 -0700146 prop->index = port->logical_port_index;
147 prop->phy_mask = scic_sds_port_get_phys(port);
148 scic_sds_port_get_sas_address(port, &prop->local.sas_address);
149 scic_sds_port_get_protocols(port, &prop->local.protocols);
150 scic_sds_port_get_attached_sas_address(port, &prop->remote.sas_address);
Dan Williams6f231dd2011-07-02 22:56:22 -0700151
Dan Williamse2f8db502011-05-10 02:28:46 -0700152 return SCI_SUCCESS;
Dan Williams6f231dd2011-07-02 22:56:22 -0700153}
154
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700155static void scic_port_bcn_enable(struct scic_sds_port *sci_port)
156{
Dan Williams85280952011-06-28 15:05:53 -0700157 struct isci_phy *iphy;
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700158 u32 val;
159 int i;
160
161 for (i = 0; i < ARRAY_SIZE(sci_port->phy_table); i++) {
Dan Williams85280952011-06-28 15:05:53 -0700162 iphy = sci_port->phy_table[i];
163 if (!iphy)
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700164 continue;
Dan Williams85280952011-06-28 15:05:53 -0700165 val = readl(&iphy->link_layer_registers->link_layer_control);
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700166 /* clear the bit by writing 1. */
Dan Williams85280952011-06-28 15:05:53 -0700167 writel(val, &iphy->link_layer_registers->link_layer_control);
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700168 }
169}
170
171/* called under scic_lock to stabilize phy:port associations */
172void isci_port_bcn_enable(struct isci_host *ihost, struct isci_port *iport)
173{
174 int i;
175
176 clear_bit(IPORT_BCN_BLOCKED, &iport->flags);
177 wake_up(&ihost->eventq);
178
179 if (!test_and_clear_bit(IPORT_BCN_PENDING, &iport->flags))
180 return;
181
182 for (i = 0; i < ARRAY_SIZE(iport->sci.phy_table); i++) {
Dan Williams85280952011-06-28 15:05:53 -0700183 struct isci_phy *iphy = iport->sci.phy_table[i];
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700184
Dan Williams85280952011-06-28 15:05:53 -0700185 if (!iphy)
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700186 continue;
187
188 ihost->sas_ha.notify_port_event(&iphy->sas_phy,
189 PORTE_BROADCAST_RCVD);
190 break;
191 }
192}
193
194void isci_port_bc_change_received(struct isci_host *ihost,
195 struct scic_sds_port *sci_port,
Dan Williams85280952011-06-28 15:05:53 -0700196 struct isci_phy *iphy)
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700197{
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700198 struct isci_port *iport = iphy->isci_port;
199
200 if (iport && test_bit(IPORT_BCN_BLOCKED, &iport->flags)) {
201 dev_dbg(&ihost->pdev->dev,
202 "%s: disabled BCN; isci_phy = %p, sas_phy = %p\n",
203 __func__, iphy, &iphy->sas_phy);
204 set_bit(IPORT_BCN_PENDING, &iport->flags);
205 atomic_inc(&iport->event);
206 wake_up(&ihost->eventq);
207 } else {
208 dev_dbg(&ihost->pdev->dev,
209 "%s: isci_phy = %p, sas_phy = %p\n",
210 __func__, iphy, &iphy->sas_phy);
211
212 ihost->sas_ha.notify_port_event(&iphy->sas_phy,
213 PORTE_BROADCAST_RCVD);
214 }
215 scic_port_bcn_enable(sci_port);
216}
217
Dan Williamse2f8db502011-05-10 02:28:46 -0700218static void isci_port_link_up(struct isci_host *isci_host,
219 struct scic_sds_port *port,
Dan Williams85280952011-06-28 15:05:53 -0700220 struct isci_phy *iphy)
Dan Williams6f231dd2011-07-02 22:56:22 -0700221{
222 unsigned long flags;
223 struct scic_port_properties properties;
Dan Williamse5313812011-05-07 10:11:43 -0700224 struct isci_port *isci_port = sci_port_to_iport(port);
Dan Williams6f231dd2011-07-02 22:56:22 -0700225 unsigned long success = true;
226
Dan Williams85280952011-06-28 15:05:53 -0700227 BUG_ON(iphy->isci_port != NULL);
Bartosz Barcinski6cb4d6b2011-04-12 17:28:43 -0700228
Dan Williams85280952011-06-28 15:05:53 -0700229 iphy->isci_port = isci_port;
Dan Williams6f231dd2011-07-02 22:56:22 -0700230
231 dev_dbg(&isci_host->pdev->dev,
232 "%s: isci_port = %p\n",
233 __func__, isci_port);
234
Dan Williams85280952011-06-28 15:05:53 -0700235 spin_lock_irqsave(&iphy->sas_phy.frame_rcvd_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700236
Dan Williams85280952011-06-28 15:05:53 -0700237 isci_port_change_state(iphy->isci_port, isci_starting);
Dan Williams6f231dd2011-07-02 22:56:22 -0700238
239 scic_port_get_properties(port, &properties);
240
Dan Williams85280952011-06-28 15:05:53 -0700241 if (iphy->protocol == SCIC_SDS_PHY_PROTOCOL_SATA) {
Dan Williams150fc6f2011-02-25 10:25:21 -0800242 u64 attached_sas_address;
Dan Williams6f231dd2011-07-02 22:56:22 -0700243
Dan Williams85280952011-06-28 15:05:53 -0700244 iphy->sas_phy.oob_mode = SATA_OOB_MODE;
245 iphy->sas_phy.frame_rcvd_size = sizeof(struct dev_to_host_fis);
Dan Williams6f231dd2011-07-02 22:56:22 -0700246
247 /*
248 * For direct-attached SATA devices, the SCI core will
249 * automagically assign a SAS address to the end device
250 * for the purpose of creating a port. This SAS address
251 * will not be the same as assigned to the PHY and needs
252 * to be obtained from struct scic_port_properties properties.
253 */
Dan Williams150fc6f2011-02-25 10:25:21 -0800254 attached_sas_address = properties.remote.sas_address.high;
255 attached_sas_address <<= 32;
256 attached_sas_address |= properties.remote.sas_address.low;
257 swab64s(&attached_sas_address);
Dan Williams6f231dd2011-07-02 22:56:22 -0700258
Dan Williams85280952011-06-28 15:05:53 -0700259 memcpy(&iphy->sas_phy.attached_sas_addr,
Dan Williams150fc6f2011-02-25 10:25:21 -0800260 &attached_sas_address, sizeof(attached_sas_address));
Dan Williams85280952011-06-28 15:05:53 -0700261 } else if (iphy->protocol == SCIC_SDS_PHY_PROTOCOL_SAS) {
262 iphy->sas_phy.oob_mode = SAS_OOB_MODE;
263 iphy->sas_phy.frame_rcvd_size = sizeof(struct sas_identify_frame);
Dan Williams6f231dd2011-07-02 22:56:22 -0700264
265 /* Copy the attached SAS address from the IAF */
Dan Williams85280952011-06-28 15:05:53 -0700266 memcpy(iphy->sas_phy.attached_sas_addr,
267 iphy->frame_rcvd.iaf.sas_addr, SAS_ADDR_SIZE);
Dan Williams6f231dd2011-07-02 22:56:22 -0700268 } else {
269 dev_err(&isci_host->pdev->dev, "%s: unkown target\n", __func__);
270 success = false;
271 }
272
Dan Williams85280952011-06-28 15:05:53 -0700273 iphy->sas_phy.phy->negotiated_linkrate = sci_phy_linkrate(iphy);
Dan Williams83e51432011-02-18 09:25:13 -0800274
Dan Williams85280952011-06-28 15:05:53 -0700275 spin_unlock_irqrestore(&iphy->sas_phy.frame_rcvd_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700276
277 /* Notify libsas that we have an address frame, if indeed
278 * we've found an SSP, SMP, or STP target */
279 if (success)
Dan Williams85280952011-06-28 15:05:53 -0700280 isci_host->sas_ha.notify_port_event(&iphy->sas_phy,
Dan Williams6f231dd2011-07-02 22:56:22 -0700281 PORTE_BYTES_DMAED);
282}
283
284
285/**
286 * isci_port_link_down() - This function is called by the sci core when a link
287 * becomes inactive.
288 * @isci_host: This parameter specifies the isci host object.
289 * @phy: This parameter specifies the isci phy with the active link.
290 * @port: This parameter specifies the isci port with the active link.
291 *
292 */
Dan Williamse2f8db502011-05-10 02:28:46 -0700293static void isci_port_link_down(struct isci_host *isci_host,
294 struct isci_phy *isci_phy,
295 struct isci_port *isci_port)
Dan Williams6f231dd2011-07-02 22:56:22 -0700296{
297 struct isci_remote_device *isci_device;
298
299 dev_dbg(&isci_host->pdev->dev,
300 "%s: isci_port = %p\n", __func__, isci_port);
301
302 if (isci_port) {
303
304 /* check to see if this is the last phy on this port. */
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700305 if (isci_phy->sas_phy.port &&
306 isci_phy->sas_phy.port->num_phys == 1) {
307 atomic_inc(&isci_port->event);
308 isci_port_bcn_enable(isci_host, isci_port);
Dan Williams6f231dd2011-07-02 22:56:22 -0700309
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700310 /* change the state for all devices on this port. The
311 * next task sent to this device will be returned as
312 * SAS_TASK_UNDELIVERED, and the scsi mid layer will
313 * remove the target
Dan Williams6f231dd2011-07-02 22:56:22 -0700314 */
315 list_for_each_entry(isci_device,
316 &isci_port->remote_dev_list,
317 node) {
318 dev_dbg(&isci_host->pdev->dev,
319 "%s: isci_device = %p\n",
320 __func__, isci_device);
Dan Williams209fae12011-06-13 17:39:44 -0700321 set_bit(IDEV_GONE, &isci_device->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700322 }
323 }
324 isci_port_change_state(isci_port, isci_stopping);
325 }
326
327 /* Notify libsas of the borken link, this will trigger calls to our
328 * isci_port_deformed and isci_dev_gone functions.
329 */
330 sas_phy_disconnected(&isci_phy->sas_phy);
331 isci_host->sas_ha.notify_phy_event(&isci_phy->sas_phy,
332 PHYE_LOSS_OF_SIGNAL);
333
334 isci_phy->isci_port = NULL;
335
336 dev_dbg(&isci_host->pdev->dev,
337 "%s: isci_port = %p - Done\n", __func__, isci_port);
338}
339
340
341/**
Dan Williams6f231dd2011-07-02 22:56:22 -0700342 * isci_port_ready() - This function is called by the sci core when a link
343 * becomes ready.
344 * @isci_host: This parameter specifies the isci host object.
345 * @port: This parameter specifies the sci port with the active link.
346 *
347 */
Dan Williamse2f8db502011-05-10 02:28:46 -0700348static void isci_port_ready(struct isci_host *isci_host, struct isci_port *isci_port)
Dan Williams6f231dd2011-07-02 22:56:22 -0700349{
350 dev_dbg(&isci_host->pdev->dev,
351 "%s: isci_port = %p\n", __func__, isci_port);
352
353 complete_all(&isci_port->start_complete);
354 isci_port_change_state(isci_port, isci_ready);
355 return;
356}
357
358/**
359 * isci_port_not_ready() - This function is called by the sci core when a link
360 * is not ready. All remote devices on this link will be removed if they are
361 * in the stopping state.
362 * @isci_host: This parameter specifies the isci host object.
363 * @port: This parameter specifies the sci port with the active link.
364 *
365 */
Dan Williamse2f8db502011-05-10 02:28:46 -0700366static void isci_port_not_ready(struct isci_host *isci_host, struct isci_port *isci_port)
Dan Williams6f231dd2011-07-02 22:56:22 -0700367{
368 dev_dbg(&isci_host->pdev->dev,
369 "%s: isci_port = %p\n", __func__, isci_port);
370}
371
Dan Williamse2f8db502011-05-10 02:28:46 -0700372static void isci_port_stop_complete(struct scic_sds_controller *scic,
373 struct scic_sds_port *sci_port,
374 enum sci_status completion_status)
375{
376 dev_dbg(&scic_to_ihost(scic)->pdev->dev, "Port stop complete\n");
377}
378
Dan Williams6f231dd2011-07-02 22:56:22 -0700379/**
380 * isci_port_hard_reset_complete() - This function is called by the sci core
381 * when the hard reset complete notification has been received.
382 * @port: This parameter specifies the sci port with the active link.
383 * @completion_status: This parameter specifies the core status for the reset
384 * process.
385 *
386 */
Dan Williamse2f8db502011-05-10 02:28:46 -0700387static void isci_port_hard_reset_complete(struct isci_port *isci_port,
388 enum sci_status completion_status)
Dan Williams6f231dd2011-07-02 22:56:22 -0700389{
390 dev_dbg(&isci_port->isci_host->pdev->dev,
391 "%s: isci_port = %p, completion_status=%x\n",
392 __func__, isci_port, completion_status);
393
394 /* Save the status of the hard reset from the port. */
395 isci_port->hard_reset_status = completion_status;
396
397 complete_all(&isci_port->hard_reset_complete);
398}
Dan Williams4393aa42011-03-31 13:10:44 -0700399
Dan Williamse2f8db502011-05-10 02:28:46 -0700400/* This method will return a true value if the specified phy can be assigned to
401 * this port The following is a list of phys for each port that are allowed: -
402 * Port 0 - 3 2 1 0 - Port 1 - 1 - Port 2 - 3 2 - Port 3 - 3 This method
403 * doesn't preclude all configurations. It merely ensures that a phy is part
404 * of the allowable set of phy identifiers for that port. For example, one
405 * could assign phy 3 to port 0 and no other phys. Please refer to
406 * scic_sds_port_is_phy_mask_valid() for information regarding whether the
407 * phy_mask for a port can be supported. bool true if this is a valid phy
408 * assignment for the port false if this is not a valid phy assignment for the
409 * port
410 */
411bool scic_sds_port_is_valid_phy_assignment(struct scic_sds_port *sci_port,
412 u32 phy_index)
413{
414 /* Initialize to invalid value. */
415 u32 existing_phy_index = SCI_MAX_PHYS;
416 u32 index;
417
418 if ((sci_port->physical_port_index == 1) && (phy_index != 1)) {
419 return false;
420 }
421
422 if (sci_port->physical_port_index == 3 && phy_index != 3) {
423 return false;
424 }
425
426 if (
427 (sci_port->physical_port_index == 2)
428 && ((phy_index == 0) || (phy_index == 1))
429 ) {
430 return false;
431 }
432
433 for (index = 0; index < SCI_MAX_PHYS; index++) {
434 if ((sci_port->phy_table[index] != NULL)
435 && (index != phy_index)) {
436 existing_phy_index = index;
437 }
438 }
439
440 /*
441 * Ensure that all of the phys in the port are capable of
442 * operating at the same maximum link rate. */
443 if (
444 (existing_phy_index < SCI_MAX_PHYS)
445 && (sci_port->owning_controller->user_parameters.sds1.phys[
446 phy_index].max_speed_generation !=
447 sci_port->owning_controller->user_parameters.sds1.phys[
448 existing_phy_index].max_speed_generation)
449 )
450 return false;
451
452 return true;
453}
454
455/**
456 *
457 * @sci_port: This is the port object for which to determine if the phy mask
458 * can be supported.
459 *
460 * This method will return a true value if the port's phy mask can be supported
461 * by the SCU. The following is a list of valid PHY mask configurations for
462 * each port: - Port 0 - [[3 2] 1] 0 - Port 1 - [1] - Port 2 - [[3] 2]
463 * - Port 3 - [3] This method returns a boolean indication specifying if the
464 * phy mask can be supported. true if this is a valid phy assignment for the
465 * port false if this is not a valid phy assignment for the port
466 */
467static bool scic_sds_port_is_phy_mask_valid(
468 struct scic_sds_port *sci_port,
469 u32 phy_mask)
470{
471 if (sci_port->physical_port_index == 0) {
472 if (((phy_mask & 0x0F) == 0x0F)
473 || ((phy_mask & 0x03) == 0x03)
474 || ((phy_mask & 0x01) == 0x01)
475 || (phy_mask == 0))
476 return true;
477 } else if (sci_port->physical_port_index == 1) {
478 if (((phy_mask & 0x02) == 0x02)
479 || (phy_mask == 0))
480 return true;
481 } else if (sci_port->physical_port_index == 2) {
482 if (((phy_mask & 0x0C) == 0x0C)
483 || ((phy_mask & 0x04) == 0x04)
484 || (phy_mask == 0))
485 return true;
486 } else if (sci_port->physical_port_index == 3) {
487 if (((phy_mask & 0x08) == 0x08)
488 || (phy_mask == 0))
489 return true;
490 }
491
492 return false;
493}
494
Dan Williams85280952011-06-28 15:05:53 -0700495/*
Dan Williamse2f8db502011-05-10 02:28:46 -0700496 * This method retrieves a currently active (i.e. connected) phy contained in
497 * the port. Currently, the lowest order phy that is connected is returned.
498 * This method returns a pointer to a SCIS_SDS_PHY object. NULL This value is
499 * returned if there are no currently active (i.e. connected to a remote end
500 * point) phys contained in the port. All other values specify a struct scic_sds_phy
501 * object that is active in the port.
502 */
Dan Williams85280952011-06-28 15:05:53 -0700503static struct isci_phy *scic_sds_port_get_a_connected_phy(struct scic_sds_port *sci_port)
504{
Dan Williamse2f8db502011-05-10 02:28:46 -0700505 u32 index;
Dan Williams85280952011-06-28 15:05:53 -0700506 struct isci_phy *iphy;
Dan Williamse2f8db502011-05-10 02:28:46 -0700507
508 for (index = 0; index < SCI_MAX_PHYS; index++) {
Dan Williams85280952011-06-28 15:05:53 -0700509 /* Ensure that the phy is both part of the port and currently
510 * connected to the remote end-point.
511 */
512 iphy = sci_port->phy_table[index];
513 if (iphy && scic_sds_port_active_phy(sci_port, iphy))
514 return iphy;
Dan Williamse2f8db502011-05-10 02:28:46 -0700515 }
516
517 return NULL;
518}
519
Dan Williams85280952011-06-28 15:05:53 -0700520static enum sci_status scic_sds_port_set_phy(struct scic_sds_port *port, struct isci_phy *iphy)
Dan Williamse2f8db502011-05-10 02:28:46 -0700521{
Dan Williams85280952011-06-28 15:05:53 -0700522 /* Check to see if we can add this phy to a port
Dan Williamse2f8db502011-05-10 02:28:46 -0700523 * that means that the phy is not part of a port and that the port does
Dan Williams85280952011-06-28 15:05:53 -0700524 * not already have a phy assinged to the phy index.
525 */
526 if (!port->phy_table[iphy->phy_index] &&
527 !phy_get_non_dummy_port(iphy) &&
528 scic_sds_port_is_valid_phy_assignment(port, iphy->phy_index)) {
529 /* Phy is being added in the stopped state so we are in MPC mode
530 * make logical port index = physical port index
531 */
Dan Williamse2f8db502011-05-10 02:28:46 -0700532 port->logical_port_index = port->physical_port_index;
Dan Williams85280952011-06-28 15:05:53 -0700533 port->phy_table[iphy->phy_index] = iphy;
534 scic_sds_phy_set_port(iphy, port);
Dan Williamse2f8db502011-05-10 02:28:46 -0700535
536 return SCI_SUCCESS;
537 }
538
539 return SCI_FAILURE;
540}
541
Dan Williams85280952011-06-28 15:05:53 -0700542static enum sci_status scic_sds_port_clear_phy(struct scic_sds_port *port,
543 struct isci_phy *iphy)
Dan Williamse2f8db502011-05-10 02:28:46 -0700544{
545 /* Make sure that this phy is part of this port */
Dan Williams85280952011-06-28 15:05:53 -0700546 if (port->phy_table[iphy->phy_index] == iphy &&
547 phy_get_non_dummy_port(iphy) == port) {
Dan Williamse2f8db502011-05-10 02:28:46 -0700548 struct scic_sds_controller *scic = port->owning_controller;
549 struct isci_host *ihost = scic_to_ihost(scic);
550
551 /* Yep it is assigned to this port so remove it */
Dan Williams85280952011-06-28 15:05:53 -0700552 scic_sds_phy_set_port(iphy, &ihost->ports[SCI_MAX_PORTS].sci);
553 port->phy_table[iphy->phy_index] = NULL;
Dan Williamse2f8db502011-05-10 02:28:46 -0700554 return SCI_SUCCESS;
555 }
556
557 return SCI_FAILURE;
558}
559
Dan Williamse2f8db502011-05-10 02:28:46 -0700560
561/**
562 * This method requests the SAS address for the supplied SAS port from the SCI
563 * implementation.
564 * @sci_port: a handle corresponding to the SAS port for which to return the
565 * SAS address.
566 * @sas_address: This parameter specifies a pointer to a SAS address structure
567 * into which the core will copy the SAS address for the port.
568 *
569 */
570void scic_sds_port_get_sas_address(
571 struct scic_sds_port *sci_port,
572 struct sci_sas_address *sas_address)
573{
574 u32 index;
575
576 sas_address->high = 0;
577 sas_address->low = 0;
578
579 for (index = 0; index < SCI_MAX_PHYS; index++) {
580 if (sci_port->phy_table[index] != NULL) {
581 scic_sds_phy_get_sas_address(sci_port->phy_table[index], sas_address);
582 }
583 }
584}
585
586/*
587 * This function requests the SAS address for the device directly attached to
588 * this SAS port.
589 * @sci_port: a handle corresponding to the SAS port for which to return the
590 * SAS address.
591 * @sas_address: This parameter specifies a pointer to a SAS address structure
592 * into which the core will copy the SAS address for the device directly
593 * attached to the port.
594 *
595 */
596void scic_sds_port_get_attached_sas_address(
597 struct scic_sds_port *sci_port,
598 struct sci_sas_address *sas_address)
599{
Dan Williams85280952011-06-28 15:05:53 -0700600 struct isci_phy *iphy;
Dan Williamse2f8db502011-05-10 02:28:46 -0700601
602 /*
603 * Ensure that the phy is both part of the port and currently
604 * connected to the remote end-point.
605 */
Dan Williams85280952011-06-28 15:05:53 -0700606 iphy = scic_sds_port_get_a_connected_phy(sci_port);
607 if (iphy) {
608 if (iphy->protocol != SCIC_SDS_PHY_PROTOCOL_SATA) {
609 scic_sds_phy_get_attached_sas_address(iphy,
Dan Williamse2f8db502011-05-10 02:28:46 -0700610 sas_address);
611 } else {
Dan Williams85280952011-06-28 15:05:53 -0700612 scic_sds_phy_get_sas_address(iphy, sas_address);
613 sas_address->low += iphy->phy_index;
Dan Williamse2f8db502011-05-10 02:28:46 -0700614 }
615 } else {
616 sas_address->high = 0;
617 sas_address->low = 0;
618 }
619}
620
621/**
622 * scic_sds_port_construct_dummy_rnc() - create dummy rnc for si workaround
623 *
624 * @sci_port: logical port on which we need to create the remote node context
625 * @rni: remote node index for this remote node context.
626 *
627 * This routine will construct a dummy remote node context data structure
628 * This structure will be posted to the hardware to work around a scheduler
629 * error in the hardware.
630 */
631static void scic_sds_port_construct_dummy_rnc(struct scic_sds_port *sci_port, u16 rni)
632{
633 union scu_remote_node_context *rnc;
634
635 rnc = &sci_port->owning_controller->remote_node_context_table[rni];
636
637 memset(rnc, 0, sizeof(union scu_remote_node_context));
638
639 rnc->ssp.remote_sas_address_hi = 0;
640 rnc->ssp.remote_sas_address_lo = 0;
641
642 rnc->ssp.remote_node_index = rni;
643 rnc->ssp.remote_node_port_width = 1;
644 rnc->ssp.logical_port_index = sci_port->physical_port_index;
645
646 rnc->ssp.nexus_loss_timer_enable = false;
647 rnc->ssp.check_bit = false;
648 rnc->ssp.is_valid = true;
649 rnc->ssp.is_remote_node_context = true;
650 rnc->ssp.function_number = 0;
651 rnc->ssp.arbitration_wait_time = 0;
652}
653
Dan Williamsdd047c82011-06-09 11:06:58 -0700654/*
655 * construct a dummy task context data structure. This
Dan Williamse2f8db502011-05-10 02:28:46 -0700656 * structure will be posted to the hardwre to work around a scheduler error
657 * in the hardware.
Dan Williamse2f8db502011-05-10 02:28:46 -0700658 */
Dan Williamsdd047c82011-06-09 11:06:58 -0700659static void scic_sds_port_construct_dummy_task(struct scic_sds_port *sci_port, u16 tag)
Dan Williamse2f8db502011-05-10 02:28:46 -0700660{
Dan Williams312e0c22011-06-28 13:47:09 -0700661 struct scic_sds_controller *scic = sci_port->owning_controller;
Dan Williamse2f8db502011-05-10 02:28:46 -0700662 struct scu_task_context *task_context;
663
Dan Williams312e0c22011-06-28 13:47:09 -0700664 task_context = &scic->task_context_table[ISCI_TAG_TCI(tag)];
Dan Williamse2f8db502011-05-10 02:28:46 -0700665 memset(task_context, 0, sizeof(struct scu_task_context));
666
Dan Williamse2f8db502011-05-10 02:28:46 -0700667 task_context->initiator_request = 1;
668 task_context->connection_rate = 1;
Dan Williamse2f8db502011-05-10 02:28:46 -0700669 task_context->logical_port_index = sci_port->physical_port_index;
670 task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SSP;
Dan Williamsdd047c82011-06-09 11:06:58 -0700671 task_context->task_index = ISCI_TAG_TCI(tag);
Dan Williamse2f8db502011-05-10 02:28:46 -0700672 task_context->valid = SCU_TASK_CONTEXT_VALID;
673 task_context->context_type = SCU_TASK_CONTEXT_TYPE;
Dan Williamse2f8db502011-05-10 02:28:46 -0700674 task_context->remote_node_index = sci_port->reserved_rni;
Dan Williamse2f8db502011-05-10 02:28:46 -0700675 task_context->do_not_dma_ssp_good_response = 1;
Dan Williamse2f8db502011-05-10 02:28:46 -0700676 task_context->task_phase = 0x01;
677}
678
679static void scic_sds_port_destroy_dummy_resources(struct scic_sds_port *sci_port)
680{
681 struct scic_sds_controller *scic = sci_port->owning_controller;
682
Dan Williams312e0c22011-06-28 13:47:09 -0700683 if (sci_port->reserved_tag != SCI_CONTROLLER_INVALID_IO_TAG)
684 isci_free_tag(scic_to_ihost(scic), sci_port->reserved_tag);
Dan Williamse2f8db502011-05-10 02:28:46 -0700685
686 if (sci_port->reserved_rni != SCU_DUMMY_INDEX)
687 scic_sds_remote_node_table_release_remote_node_index(&scic->available_remote_nodes,
688 1, sci_port->reserved_rni);
689
690 sci_port->reserved_rni = SCU_DUMMY_INDEX;
Dan Williams312e0c22011-06-28 13:47:09 -0700691 sci_port->reserved_tag = SCI_CONTROLLER_INVALID_IO_TAG;
Dan Williamse2f8db502011-05-10 02:28:46 -0700692}
693
694/**
695 * This method performs initialization of the supplied port. Initialization
696 * includes: - state machine initialization - member variable initialization
697 * - configuring the phy_mask
698 * @sci_port:
699 * @transport_layer_registers:
700 * @port_task_scheduler_registers:
701 * @port_configuration_regsiter:
702 *
703 * enum sci_status SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION This value is returned
704 * if the phy being added to the port
705 */
706enum sci_status scic_sds_port_initialize(
707 struct scic_sds_port *sci_port,
708 void __iomem *port_task_scheduler_registers,
709 void __iomem *port_configuration_regsiter,
710 void __iomem *viit_registers)
711{
712 sci_port->port_task_scheduler_registers = port_task_scheduler_registers;
713 sci_port->port_pe_configuration_register = port_configuration_regsiter;
714 sci_port->viit_registers = viit_registers;
715
716 return SCI_SUCCESS;
717}
718
Dan Williamse2f8db502011-05-10 02:28:46 -0700719
720/**
721 * This method assigns the direct attached device ID for this port.
722 *
723 * @param[in] sci_port The port for which the direct attached device id is to
724 * be assigned.
725 * @param[in] device_id The direct attached device ID to assign to the port.
726 * This will be the RNi for the device
727 */
728void scic_sds_port_setup_transports(
729 struct scic_sds_port *sci_port,
730 u32 device_id)
731{
732 u8 index;
733
734 for (index = 0; index < SCI_MAX_PHYS; index++) {
735 if (sci_port->active_phy_mask & (1 << index))
736 scic_sds_phy_setup_transport(sci_port->phy_table[index], device_id);
737 }
738}
739
740/**
741 *
742 * @sci_port: This is the port on which the phy should be enabled.
743 * @sci_phy: This is the specific phy which to enable.
744 * @do_notify_user: This parameter specifies whether to inform the user (via
745 * scic_cb_port_link_up()) as to the fact that a new phy as become ready.
746 *
747 * This function will activate the phy in the port.
748 * Activation includes: - adding
749 * the phy to the port - enabling the Protocol Engine in the silicon. -
750 * notifying the user that the link is up. none
751 */
752static void scic_sds_port_activate_phy(struct scic_sds_port *sci_port,
Dan Williams85280952011-06-28 15:05:53 -0700753 struct isci_phy *iphy,
Dan Williamse2f8db502011-05-10 02:28:46 -0700754 bool do_notify_user)
755{
756 struct scic_sds_controller *scic = sci_port->owning_controller;
757 struct isci_host *ihost = scic_to_ihost(scic);
758
Dan Williams85280952011-06-28 15:05:53 -0700759 if (iphy->protocol != SCIC_SDS_PHY_PROTOCOL_SATA)
760 scic_sds_phy_resume(iphy);
Dan Williamse2f8db502011-05-10 02:28:46 -0700761
Dan Williams85280952011-06-28 15:05:53 -0700762 sci_port->active_phy_mask |= 1 << iphy->phy_index;
Dan Williamse2f8db502011-05-10 02:28:46 -0700763
Dan Williams85280952011-06-28 15:05:53 -0700764 scic_sds_controller_clear_invalid_phy(scic, iphy);
Dan Williamse2f8db502011-05-10 02:28:46 -0700765
766 if (do_notify_user == true)
Dan Williams85280952011-06-28 15:05:53 -0700767 isci_port_link_up(ihost, sci_port, iphy);
Dan Williamse2f8db502011-05-10 02:28:46 -0700768}
769
770void scic_sds_port_deactivate_phy(struct scic_sds_port *sci_port,
Dan Williams85280952011-06-28 15:05:53 -0700771 struct isci_phy *iphy,
Dan Williamse2f8db502011-05-10 02:28:46 -0700772 bool do_notify_user)
773{
774 struct scic_sds_controller *scic = scic_sds_port_get_controller(sci_port);
775 struct isci_port *iport = sci_port_to_iport(sci_port);
776 struct isci_host *ihost = scic_to_ihost(scic);
Dan Williamse2f8db502011-05-10 02:28:46 -0700777
Dan Williams85280952011-06-28 15:05:53 -0700778 sci_port->active_phy_mask &= ~(1 << iphy->phy_index);
Dan Williamse2f8db502011-05-10 02:28:46 -0700779
Dan Williams85280952011-06-28 15:05:53 -0700780 iphy->max_negotiated_speed = SAS_LINK_RATE_UNKNOWN;
Dan Williamse2f8db502011-05-10 02:28:46 -0700781
782 /* Re-assign the phy back to the LP as if it were a narrow port */
Dan Williams85280952011-06-28 15:05:53 -0700783 writel(iphy->phy_index,
784 &sci_port->port_pe_configuration_register[iphy->phy_index]);
Dan Williamse2f8db502011-05-10 02:28:46 -0700785
786 if (do_notify_user == true)
787 isci_port_link_down(ihost, iphy, iport);
788}
789
790/**
791 *
792 * @sci_port: This is the port on which the phy should be disabled.
793 * @sci_phy: This is the specific phy which to disabled.
794 *
795 * This function will disable the phy and report that the phy is not valid for
796 * this port object. None
797 */
798static void scic_sds_port_invalid_link_up(struct scic_sds_port *sci_port,
Dan Williams85280952011-06-28 15:05:53 -0700799 struct isci_phy *iphy)
Dan Williamse2f8db502011-05-10 02:28:46 -0700800{
801 struct scic_sds_controller *scic = sci_port->owning_controller;
802
803 /*
804 * Check to see if we have alreay reported this link as bad and if
805 * not go ahead and tell the SCI_USER that we have discovered an
806 * invalid link.
807 */
Dan Williams85280952011-06-28 15:05:53 -0700808 if ((scic->invalid_phy_mask & (1 << iphy->phy_index)) == 0) {
809 scic_sds_controller_set_invalid_phy(scic, iphy);
Dan Williamse2f8db502011-05-10 02:28:46 -0700810 dev_warn(&scic_to_ihost(scic)->pdev->dev, "Invalid link up!\n");
811 }
812}
813
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000814static bool is_port_ready_state(enum scic_sds_port_states state)
815{
816 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000817 case SCI_PORT_READY:
818 case SCI_PORT_SUB_WAITING:
819 case SCI_PORT_SUB_OPERATIONAL:
820 case SCI_PORT_SUB_CONFIGURING:
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000821 return true;
822 default:
823 return false;
824 }
825}
826
827/* flag dummy rnc hanling when exiting a ready state */
828static void port_state_machine_change(struct scic_sds_port *sci_port,
829 enum scic_sds_port_states state)
830{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000831 struct sci_base_state_machine *sm = &sci_port->sm;
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000832 enum scic_sds_port_states old_state = sm->current_state_id;
833
834 if (is_port_ready_state(old_state) && !is_port_ready_state(state))
835 sci_port->ready_exit = true;
836
Edmund Nadolskie3013702011-06-02 00:10:43 +0000837 sci_change_state(sm, state);
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000838 sci_port->ready_exit = false;
839}
840
Dan Williamse2f8db502011-05-10 02:28:46 -0700841/**
842 * scic_sds_port_general_link_up_handler - phy can be assigned to port?
843 * @sci_port: scic_sds_port object for which has a phy that has gone link up.
Dan Williams85280952011-06-28 15:05:53 -0700844 * @sci_phy: This is the struct isci_phy object that has gone link up.
Dan Williamse2f8db502011-05-10 02:28:46 -0700845 * @do_notify_user: This parameter specifies whether to inform the user (via
846 * scic_cb_port_link_up()) as to the fact that a new phy as become ready.
847 *
848 * Determine if this phy can be assigned to this
849 * port . If the phy is not a valid PHY for
850 * this port then the function will notify the user. A PHY can only be
851 * part of a port if it's attached SAS ADDRESS is the same as all other PHYs in
852 * the same port. none
853 */
854static void scic_sds_port_general_link_up_handler(struct scic_sds_port *sci_port,
Dan Williams85280952011-06-28 15:05:53 -0700855 struct isci_phy *iphy,
Dan Williamse2f8db502011-05-10 02:28:46 -0700856 bool do_notify_user)
857{
858 struct sci_sas_address port_sas_address;
859 struct sci_sas_address phy_sas_address;
860
861 scic_sds_port_get_attached_sas_address(sci_port, &port_sas_address);
Dan Williams85280952011-06-28 15:05:53 -0700862 scic_sds_phy_get_attached_sas_address(iphy, &phy_sas_address);
Dan Williamse2f8db502011-05-10 02:28:46 -0700863
864 /* If the SAS address of the new phy matches the SAS address of
865 * other phys in the port OR this is the first phy in the port,
866 * then activate the phy and allow it to be used for operations
867 * in this port.
868 */
869 if ((phy_sas_address.high == port_sas_address.high &&
870 phy_sas_address.low == port_sas_address.low) ||
871 sci_port->active_phy_mask == 0) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000872 struct sci_base_state_machine *sm = &sci_port->sm;
Dan Williamse2f8db502011-05-10 02:28:46 -0700873
Dan Williams85280952011-06-28 15:05:53 -0700874 scic_sds_port_activate_phy(sci_port, iphy, do_notify_user);
Edmund Nadolskie3013702011-06-02 00:10:43 +0000875 if (sm->current_state_id == SCI_PORT_RESETTING)
876 port_state_machine_change(sci_port, SCI_PORT_READY);
Dan Williamse2f8db502011-05-10 02:28:46 -0700877 } else
Dan Williams85280952011-06-28 15:05:53 -0700878 scic_sds_port_invalid_link_up(sci_port, iphy);
Dan Williamse2f8db502011-05-10 02:28:46 -0700879}
880
881
882
883/**
884 * This method returns false if the port only has a single phy object assigned.
885 * If there are no phys or more than one phy then the method will return
886 * true.
887 * @sci_port: The port for which the wide port condition is to be checked.
888 *
889 * bool true Is returned if this is a wide ported port. false Is returned if
890 * this is a narrow port.
891 */
892static bool scic_sds_port_is_wide(struct scic_sds_port *sci_port)
893{
894 u32 index;
895 u32 phy_count = 0;
896
897 for (index = 0; index < SCI_MAX_PHYS; index++) {
898 if (sci_port->phy_table[index] != NULL) {
899 phy_count++;
900 }
901 }
902
903 return phy_count != 1;
904}
905
906/**
907 * This method is called by the PHY object when the link is detected. if the
908 * port wants the PHY to continue on to the link up state then the port
909 * layer must return true. If the port object returns false the phy object
910 * must halt its attempt to go link up.
911 * @sci_port: The port associated with the phy object.
912 * @sci_phy: The phy object that is trying to go link up.
913 *
914 * true if the phy object can continue to the link up condition. true Is
915 * returned if this phy can continue to the ready state. false Is returned if
916 * can not continue on to the ready state. This notification is in place for
917 * wide ports and direct attached phys. Since there are no wide ported SATA
918 * devices this could become an invalid port configuration.
919 */
920bool scic_sds_port_link_detected(
921 struct scic_sds_port *sci_port,
Dan Williams85280952011-06-28 15:05:53 -0700922 struct isci_phy *iphy)
Dan Williamse2f8db502011-05-10 02:28:46 -0700923{
924 if ((sci_port->logical_port_index != SCIC_SDS_DUMMY_PORT) &&
Dan Williams85280952011-06-28 15:05:53 -0700925 (iphy->protocol == SCIC_SDS_PHY_PROTOCOL_SATA) &&
Dan Williamse2f8db502011-05-10 02:28:46 -0700926 scic_sds_port_is_wide(sci_port)) {
Dan Williams85280952011-06-28 15:05:53 -0700927 scic_sds_port_invalid_link_up(sci_port, iphy);
Dan Williamse2f8db502011-05-10 02:28:46 -0700928
929 return false;
930 }
931
932 return true;
933}
934
Edmund Nadolski5553ba22011-05-19 11:59:10 +0000935static void port_timeout(unsigned long data)
Dan Williamse2f8db502011-05-10 02:28:46 -0700936{
Edmund Nadolski5553ba22011-05-19 11:59:10 +0000937 struct sci_timer *tmr = (struct sci_timer *)data;
938 struct scic_sds_port *sci_port = container_of(tmr, typeof(*sci_port), timer);
939 struct isci_host *ihost = scic_to_ihost(sci_port->owning_controller);
940 unsigned long flags;
Dan Williamse2f8db502011-05-10 02:28:46 -0700941 u32 current_state;
942
Edmund Nadolski5553ba22011-05-19 11:59:10 +0000943 spin_lock_irqsave(&ihost->scic_lock, flags);
944
945 if (tmr->cancel)
946 goto done;
947
Edmund Nadolskie3013702011-06-02 00:10:43 +0000948 current_state = sci_port->sm.current_state_id;
Dan Williamse2f8db502011-05-10 02:28:46 -0700949
Edmund Nadolskie3013702011-06-02 00:10:43 +0000950 if (current_state == SCI_PORT_RESETTING) {
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000951 /* if the port is still in the resetting state then the timeout
952 * fired before the reset completed.
Dan Williamse2f8db502011-05-10 02:28:46 -0700953 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000954 port_state_machine_change(sci_port, SCI_PORT_FAILED);
955 } else if (current_state == SCI_PORT_STOPPED) {
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000956 /* if the port is stopped then the start request failed In this
957 * case stay in the stopped state.
Dan Williamse2f8db502011-05-10 02:28:46 -0700958 */
959 dev_err(sciport_to_dev(sci_port),
960 "%s: SCIC Port 0x%p failed to stop before tiemout.\n",
961 __func__,
962 sci_port);
Edmund Nadolskie3013702011-06-02 00:10:43 +0000963 } else if (current_state == SCI_PORT_STOPPING) {
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000964 /* if the port is still stopping then the stop has not completed */
965 isci_port_stop_complete(sci_port->owning_controller,
966 sci_port,
967 SCI_FAILURE_TIMEOUT);
Dan Williamse2f8db502011-05-10 02:28:46 -0700968 } else {
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000969 /* The port is in the ready state and we have a timer
Dan Williamse2f8db502011-05-10 02:28:46 -0700970 * reporting a timeout this should not happen.
971 */
972 dev_err(sciport_to_dev(sci_port),
973 "%s: SCIC Port 0x%p is processing a timeout operation "
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000974 "in state %d.\n", __func__, sci_port, current_state);
Dan Williamse2f8db502011-05-10 02:28:46 -0700975 }
Edmund Nadolski5553ba22011-05-19 11:59:10 +0000976
977done:
978 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williamse2f8db502011-05-10 02:28:46 -0700979}
980
981/* --------------------------------------------------------------------------- */
982
983/**
984 * This function updates the hardwares VIIT entry for this port.
985 *
986 *
987 */
988static void scic_sds_port_update_viit_entry(struct scic_sds_port *sci_port)
989{
990 struct sci_sas_address sas_address;
991
992 scic_sds_port_get_sas_address(sci_port, &sas_address);
993
994 writel(sas_address.high,
995 &sci_port->viit_registers->initiator_sas_address_hi);
996 writel(sas_address.low,
997 &sci_port->viit_registers->initiator_sas_address_lo);
998
999 /* This value get cleared just in case its not already cleared */
1000 writel(0, &sci_port->viit_registers->reserved);
1001
1002 /* We are required to update the status register last */
1003 writel(SCU_VIIT_ENTRY_ID_VIIT |
1004 SCU_VIIT_IPPT_INITIATOR |
1005 ((1 << sci_port->physical_port_index) << SCU_VIIT_ENTRY_LPVIE_SHIFT) |
1006 SCU_VIIT_STATUS_ALL_VALID,
1007 &sci_port->viit_registers->status);
1008}
1009
Dan Williams85280952011-06-28 15:05:53 -07001010enum sas_linkrate scic_sds_port_get_max_allowed_speed(struct scic_sds_port *sci_port)
Dan Williamse2f8db502011-05-10 02:28:46 -07001011{
1012 u16 index;
Dan Williams85280952011-06-28 15:05:53 -07001013 struct isci_phy *iphy;
Dan Williamse2f8db502011-05-10 02:28:46 -07001014 enum sas_linkrate max_allowed_speed = SAS_LINK_RATE_6_0_GBPS;
Dan Williamse2f8db502011-05-10 02:28:46 -07001015
1016 /*
1017 * Loop through all of the phys in this port and find the phy with the
1018 * lowest maximum link rate. */
1019 for (index = 0; index < SCI_MAX_PHYS; index++) {
Dan Williams85280952011-06-28 15:05:53 -07001020 iphy = sci_port->phy_table[index];
1021 if (iphy && scic_sds_port_active_phy(sci_port, iphy) &&
1022 iphy->max_negotiated_speed < max_allowed_speed)
1023 max_allowed_speed = iphy->max_negotiated_speed;
Dan Williamse2f8db502011-05-10 02:28:46 -07001024 }
1025
1026 return max_allowed_speed;
1027}
1028
Dan Williams85280952011-06-28 15:05:53 -07001029static void scic_sds_port_suspend_port_task_scheduler(struct scic_sds_port *port)
Dan Williamse2f8db502011-05-10 02:28:46 -07001030{
1031 u32 pts_control_value;
1032
1033 pts_control_value = readl(&port->port_task_scheduler_registers->control);
1034 pts_control_value |= SCU_PTSxCR_GEN_BIT(SUSPEND);
1035 writel(pts_control_value, &port->port_task_scheduler_registers->control);
1036}
1037
1038/**
1039 * scic_sds_port_post_dummy_request() - post dummy/workaround request
1040 * @sci_port: port to post task
1041 *
1042 * Prevent the hardware scheduler from posting new requests to the front
1043 * of the scheduler queue causing a starvation problem for currently
1044 * ongoing requests.
1045 *
1046 */
1047static void scic_sds_port_post_dummy_request(struct scic_sds_port *sci_port)
1048{
Dan Williamse2f8db502011-05-10 02:28:46 -07001049 struct scic_sds_controller *scic = sci_port->owning_controller;
Dan Williams312e0c22011-06-28 13:47:09 -07001050 u16 tag = sci_port->reserved_tag;
1051 struct scu_task_context *tc;
1052 u32 command;
Dan Williamse2f8db502011-05-10 02:28:46 -07001053
Dan Williams312e0c22011-06-28 13:47:09 -07001054 tc = &scic->task_context_table[ISCI_TAG_TCI(tag)];
1055 tc->abort = 0;
Dan Williamse2f8db502011-05-10 02:28:46 -07001056
1057 command = SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
1058 sci_port->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT |
Dan Williams312e0c22011-06-28 13:47:09 -07001059 ISCI_TAG_TCI(tag);
Dan Williamse2f8db502011-05-10 02:28:46 -07001060
1061 scic_sds_controller_post_request(scic, command);
1062}
1063
1064/**
1065 * This routine will abort the dummy request. This will alow the hardware to
1066 * power down parts of the silicon to save power.
1067 *
1068 * @sci_port: The port on which the task must be aborted.
1069 *
1070 */
1071static void scic_sds_port_abort_dummy_request(struct scic_sds_port *sci_port)
1072{
1073 struct scic_sds_controller *scic = sci_port->owning_controller;
Dan Williams312e0c22011-06-28 13:47:09 -07001074 u16 tag = sci_port->reserved_tag;
Dan Williamse2f8db502011-05-10 02:28:46 -07001075 struct scu_task_context *tc;
1076 u32 command;
1077
Dan Williams312e0c22011-06-28 13:47:09 -07001078 tc = &scic->task_context_table[ISCI_TAG_TCI(tag)];
Dan Williamse2f8db502011-05-10 02:28:46 -07001079 tc->abort = 1;
1080
1081 command = SCU_CONTEXT_COMMAND_REQUEST_POST_TC_ABORT |
1082 sci_port->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT |
Dan Williams312e0c22011-06-28 13:47:09 -07001083 ISCI_TAG_TCI(tag);
Dan Williamse2f8db502011-05-10 02:28:46 -07001084
1085 scic_sds_controller_post_request(scic, command);
1086}
1087
1088/**
1089 *
1090 * @sci_port: This is the struct scic_sds_port object to resume.
1091 *
1092 * This method will resume the port task scheduler for this port object. none
1093 */
1094static void
1095scic_sds_port_resume_port_task_scheduler(struct scic_sds_port *port)
1096{
1097 u32 pts_control_value;
1098
1099 pts_control_value = readl(&port->port_task_scheduler_registers->control);
1100 pts_control_value &= ~SCU_PTSxCR_GEN_BIT(SUSPEND);
1101 writel(pts_control_value, &port->port_task_scheduler_registers->control);
1102}
1103
Dan Williams9269e0e2011-05-12 07:42:17 -07001104static void scic_sds_port_ready_substate_waiting_enter(struct sci_base_state_machine *sm)
Dan Williamse2f8db502011-05-10 02:28:46 -07001105{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001106 struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), sm);
Dan Williamse2f8db502011-05-10 02:28:46 -07001107
Dan Williamse2f8db502011-05-10 02:28:46 -07001108 scic_sds_port_suspend_port_task_scheduler(sci_port);
1109
1110 sci_port->not_ready_reason = SCIC_PORT_NOT_READY_NO_ACTIVE_PHYS;
1111
1112 if (sci_port->active_phy_mask != 0) {
1113 /* At least one of the phys on the port is ready */
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001114 port_state_machine_change(sci_port,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001115 SCI_PORT_SUB_OPERATIONAL);
Dan Williamse2f8db502011-05-10 02:28:46 -07001116 }
1117}
1118
Dan Williams9269e0e2011-05-12 07:42:17 -07001119static void scic_sds_port_ready_substate_operational_enter(struct sci_base_state_machine *sm)
Dan Williamse2f8db502011-05-10 02:28:46 -07001120{
1121 u32 index;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001122 struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), sm);
Dan Williamse2f8db502011-05-10 02:28:46 -07001123 struct scic_sds_controller *scic = sci_port->owning_controller;
1124 struct isci_host *ihost = scic_to_ihost(scic);
1125 struct isci_port *iport = sci_port_to_iport(sci_port);
1126
Dan Williamse2f8db502011-05-10 02:28:46 -07001127 isci_port_ready(ihost, iport);
1128
1129 for (index = 0; index < SCI_MAX_PHYS; index++) {
1130 if (sci_port->phy_table[index]) {
1131 writel(sci_port->physical_port_index,
1132 &sci_port->port_pe_configuration_register[
1133 sci_port->phy_table[index]->phy_index]);
1134 }
1135 }
1136
1137 scic_sds_port_update_viit_entry(sci_port);
1138
1139 scic_sds_port_resume_port_task_scheduler(sci_port);
1140
1141 /*
1142 * Post the dummy task for the port so the hardware can schedule
1143 * io correctly
1144 */
1145 scic_sds_port_post_dummy_request(sci_port);
1146}
1147
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001148static void scic_sds_port_invalidate_dummy_remote_node(struct scic_sds_port *sci_port)
1149{
1150 struct scic_sds_controller *scic = sci_port->owning_controller;
1151 u8 phys_index = sci_port->physical_port_index;
1152 union scu_remote_node_context *rnc;
1153 u16 rni = sci_port->reserved_rni;
1154 u32 command;
1155
1156 rnc = &scic->remote_node_context_table[rni];
1157
1158 rnc->ssp.is_valid = false;
1159
1160 /* ensure the preceding tc abort request has reached the
1161 * controller and give it ample time to act before posting the rnc
1162 * invalidate
1163 */
1164 readl(&scic->smu_registers->interrupt_status); /* flush */
1165 udelay(10);
1166
1167 command = SCU_CONTEXT_COMMAND_POST_RNC_INVALIDATE |
1168 phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
1169
1170 scic_sds_controller_post_request(scic, command);
1171}
1172
Dan Williamse2f8db502011-05-10 02:28:46 -07001173/**
1174 *
1175 * @object: This is the object which is cast to a struct scic_sds_port object.
1176 *
1177 * This method will perform the actions required by the struct scic_sds_port on
Edmund Nadolskie3013702011-06-02 00:10:43 +00001178 * exiting the SCI_PORT_SUB_OPERATIONAL. This function reports
Dan Williamse2f8db502011-05-10 02:28:46 -07001179 * the port not ready and suspends the port task scheduler. none
1180 */
Dan Williams9269e0e2011-05-12 07:42:17 -07001181static void scic_sds_port_ready_substate_operational_exit(struct sci_base_state_machine *sm)
Dan Williamse2f8db502011-05-10 02:28:46 -07001182{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001183 struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), sm);
Dan Williamse2f8db502011-05-10 02:28:46 -07001184 struct scic_sds_controller *scic = sci_port->owning_controller;
1185 struct isci_host *ihost = scic_to_ihost(scic);
1186 struct isci_port *iport = sci_port_to_iport(sci_port);
1187
1188 /*
1189 * Kill the dummy task for this port if it has not yet posted
1190 * the hardware will treat this as a NOP and just return abort
1191 * complete.
1192 */
1193 scic_sds_port_abort_dummy_request(sci_port);
1194
1195 isci_port_not_ready(ihost, iport);
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001196
1197 if (sci_port->ready_exit)
1198 scic_sds_port_invalidate_dummy_remote_node(sci_port);
Dan Williamse2f8db502011-05-10 02:28:46 -07001199}
1200
Dan Williams9269e0e2011-05-12 07:42:17 -07001201static void scic_sds_port_ready_substate_configuring_enter(struct sci_base_state_machine *sm)
Dan Williamse2f8db502011-05-10 02:28:46 -07001202{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001203 struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), sm);
Dan Williamse2f8db502011-05-10 02:28:46 -07001204 struct scic_sds_controller *scic = sci_port->owning_controller;
1205 struct isci_host *ihost = scic_to_ihost(scic);
1206 struct isci_port *iport = sci_port_to_iport(sci_port);
1207
Dan Williamse2f8db502011-05-10 02:28:46 -07001208 if (sci_port->active_phy_mask == 0) {
1209 isci_port_not_ready(ihost, iport);
1210
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001211 port_state_machine_change(sci_port,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001212 SCI_PORT_SUB_WAITING);
Dan Williamse2f8db502011-05-10 02:28:46 -07001213 } else if (sci_port->started_request_count == 0)
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001214 port_state_machine_change(sci_port,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001215 SCI_PORT_SUB_OPERATIONAL);
Dan Williamse2f8db502011-05-10 02:28:46 -07001216}
1217
Dan Williams9269e0e2011-05-12 07:42:17 -07001218static void scic_sds_port_ready_substate_configuring_exit(struct sci_base_state_machine *sm)
Dan Williamse2f8db502011-05-10 02:28:46 -07001219{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001220 struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), sm);
Dan Williamse2f8db502011-05-10 02:28:46 -07001221
1222 scic_sds_port_suspend_port_task_scheduler(sci_port);
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001223 if (sci_port->ready_exit)
1224 scic_sds_port_invalidate_dummy_remote_node(sci_port);
Dan Williamse2f8db502011-05-10 02:28:46 -07001225}
1226
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001227enum sci_status scic_sds_port_start(struct scic_sds_port *sci_port)
1228{
1229 struct scic_sds_controller *scic = sci_port->owning_controller;
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001230 enum sci_status status = SCI_SUCCESS;
1231 enum scic_sds_port_states state;
1232 u32 phy_mask;
1233
Edmund Nadolskie3013702011-06-02 00:10:43 +00001234 state = sci_port->sm.current_state_id;
1235 if (state != SCI_PORT_STOPPED) {
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001236 dev_warn(sciport_to_dev(sci_port),
1237 "%s: in wrong state: %d\n", __func__, state);
1238 return SCI_FAILURE_INVALID_STATE;
1239 }
1240
1241 if (sci_port->assigned_device_count > 0) {
1242 /* TODO This is a start failure operation because
1243 * there are still devices assigned to this port.
1244 * There must be no devices assigned to a port on a
1245 * start operation.
1246 */
1247 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
1248 }
1249
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001250 if (sci_port->reserved_rni == SCU_DUMMY_INDEX) {
1251 u16 rni = scic_sds_remote_node_table_allocate_remote_node(
1252 &scic->available_remote_nodes, 1);
1253
1254 if (rni != SCU_DUMMY_INDEX)
1255 scic_sds_port_construct_dummy_rnc(sci_port, rni);
1256 else
1257 status = SCI_FAILURE_INSUFFICIENT_RESOURCES;
1258 sci_port->reserved_rni = rni;
1259 }
1260
Dan Williams312e0c22011-06-28 13:47:09 -07001261 if (sci_port->reserved_tag == SCI_CONTROLLER_INVALID_IO_TAG) {
1262 struct isci_host *ihost = scic_to_ihost(scic);
1263 u16 tag;
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001264
Dan Williams312e0c22011-06-28 13:47:09 -07001265 tag = isci_alloc_tag(ihost);
1266 if (tag == SCI_CONTROLLER_INVALID_IO_TAG)
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001267 status = SCI_FAILURE_INSUFFICIENT_RESOURCES;
Dan Williams312e0c22011-06-28 13:47:09 -07001268 else
1269 scic_sds_port_construct_dummy_task(sci_port, tag);
1270 sci_port->reserved_tag = tag;
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001271 }
1272
1273 if (status == SCI_SUCCESS) {
1274 phy_mask = scic_sds_port_get_phys(sci_port);
1275
1276 /*
1277 * There are one or more phys assigned to this port. Make sure
1278 * the port's phy mask is in fact legal and supported by the
1279 * silicon.
1280 */
1281 if (scic_sds_port_is_phy_mask_valid(sci_port, phy_mask) == true) {
1282 port_state_machine_change(sci_port,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001283 SCI_PORT_READY);
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001284
1285 return SCI_SUCCESS;
1286 }
1287 status = SCI_FAILURE;
1288 }
1289
1290 if (status != SCI_SUCCESS)
1291 scic_sds_port_destroy_dummy_resources(sci_port);
1292
1293 return status;
1294}
1295
Piotr Sawicki8bc80d32011-05-11 23:52:31 +00001296enum sci_status scic_sds_port_stop(struct scic_sds_port *sci_port)
1297{
1298 enum scic_sds_port_states state;
1299
Edmund Nadolskie3013702011-06-02 00:10:43 +00001300 state = sci_port->sm.current_state_id;
Piotr Sawicki8bc80d32011-05-11 23:52:31 +00001301 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001302 case SCI_PORT_STOPPED:
Piotr Sawicki8bc80d32011-05-11 23:52:31 +00001303 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001304 case SCI_PORT_SUB_WAITING:
1305 case SCI_PORT_SUB_OPERATIONAL:
1306 case SCI_PORT_SUB_CONFIGURING:
1307 case SCI_PORT_RESETTING:
Piotr Sawicki8bc80d32011-05-11 23:52:31 +00001308 port_state_machine_change(sci_port,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001309 SCI_PORT_STOPPING);
Piotr Sawicki8bc80d32011-05-11 23:52:31 +00001310 return SCI_SUCCESS;
1311 default:
1312 dev_warn(sciport_to_dev(sci_port),
1313 "%s: in wrong state: %d\n", __func__, state);
1314 return SCI_FAILURE_INVALID_STATE;
1315 }
1316}
1317
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001318static enum sci_status scic_port_hard_reset(struct scic_sds_port *sci_port, u32 timeout)
1319{
1320 enum sci_status status = SCI_FAILURE_INVALID_PHY;
Dan Williams85280952011-06-28 15:05:53 -07001321 struct isci_phy *iphy = NULL;
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001322 enum scic_sds_port_states state;
1323 u32 phy_index;
1324
Edmund Nadolskie3013702011-06-02 00:10:43 +00001325 state = sci_port->sm.current_state_id;
1326 if (state != SCI_PORT_SUB_OPERATIONAL) {
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001327 dev_warn(sciport_to_dev(sci_port),
1328 "%s: in wrong state: %d\n", __func__, state);
1329 return SCI_FAILURE_INVALID_STATE;
1330 }
1331
1332 /* Select a phy on which we can send the hard reset request. */
Dan Williams85280952011-06-28 15:05:53 -07001333 for (phy_index = 0; phy_index < SCI_MAX_PHYS && !iphy; phy_index++) {
1334 iphy = sci_port->phy_table[phy_index];
1335 if (iphy && !scic_sds_port_active_phy(sci_port, iphy)) {
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001336 /*
1337 * We found a phy but it is not ready select
1338 * different phy
1339 */
Dan Williams85280952011-06-28 15:05:53 -07001340 iphy = NULL;
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001341 }
1342 }
1343
1344 /* If we have a phy then go ahead and start the reset procedure */
Dan Williams85280952011-06-28 15:05:53 -07001345 if (!iphy)
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001346 return status;
Dan Williams85280952011-06-28 15:05:53 -07001347 status = scic_sds_phy_reset(iphy);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001348
1349 if (status != SCI_SUCCESS)
1350 return status;
1351
Edmund Nadolski5553ba22011-05-19 11:59:10 +00001352 sci_mod_timer(&sci_port->timer, timeout);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001353 sci_port->not_ready_reason = SCIC_PORT_NOT_READY_HARD_RESET_REQUESTED;
1354
Dan Williams85280952011-06-28 15:05:53 -07001355 port_state_machine_change(sci_port, SCI_PORT_RESETTING);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001356 return SCI_SUCCESS;
1357}
1358
1359/**
1360 * scic_sds_port_add_phy() -
1361 * @sci_port: This parameter specifies the port in which the phy will be added.
1362 * @sci_phy: This parameter is the phy which is to be added to the port.
1363 *
1364 * This method will add a PHY to the selected port. This method returns an
1365 * enum sci_status. SCI_SUCCESS the phy has been added to the port. Any other
1366 * status is a failure to add the phy to the port.
1367 */
1368enum sci_status scic_sds_port_add_phy(struct scic_sds_port *sci_port,
Dan Williams85280952011-06-28 15:05:53 -07001369 struct isci_phy *iphy)
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001370{
1371 enum sci_status status;
1372 enum scic_sds_port_states state;
1373
Edmund Nadolskie3013702011-06-02 00:10:43 +00001374 state = sci_port->sm.current_state_id;
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001375 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001376 case SCI_PORT_STOPPED: {
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001377 struct sci_sas_address port_sas_address;
1378
1379 /* Read the port assigned SAS Address if there is one */
1380 scic_sds_port_get_sas_address(sci_port, &port_sas_address);
1381
1382 if (port_sas_address.high != 0 && port_sas_address.low != 0) {
1383 struct sci_sas_address phy_sas_address;
1384
1385 /* Make sure that the PHY SAS Address matches the SAS Address
1386 * for this port
1387 */
Dan Williams85280952011-06-28 15:05:53 -07001388 scic_sds_phy_get_sas_address(iphy, &phy_sas_address);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001389
1390 if (port_sas_address.high != phy_sas_address.high ||
1391 port_sas_address.low != phy_sas_address.low)
1392 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
1393 }
Dan Williams85280952011-06-28 15:05:53 -07001394 return scic_sds_port_set_phy(sci_port, iphy);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001395 }
Edmund Nadolskie3013702011-06-02 00:10:43 +00001396 case SCI_PORT_SUB_WAITING:
1397 case SCI_PORT_SUB_OPERATIONAL:
Dan Williams85280952011-06-28 15:05:53 -07001398 status = scic_sds_port_set_phy(sci_port, iphy);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001399
1400 if (status != SCI_SUCCESS)
1401 return status;
1402
Dan Williams85280952011-06-28 15:05:53 -07001403 scic_sds_port_general_link_up_handler(sci_port, iphy, true);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001404 sci_port->not_ready_reason = SCIC_PORT_NOT_READY_RECONFIGURING;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001405 port_state_machine_change(sci_port, SCI_PORT_SUB_CONFIGURING);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001406
1407 return status;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001408 case SCI_PORT_SUB_CONFIGURING:
Dan Williams85280952011-06-28 15:05:53 -07001409 status = scic_sds_port_set_phy(sci_port, iphy);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001410
1411 if (status != SCI_SUCCESS)
1412 return status;
Dan Williams85280952011-06-28 15:05:53 -07001413 scic_sds_port_general_link_up_handler(sci_port, iphy, true);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001414
1415 /* Re-enter the configuring state since this may be the last phy in
1416 * the port.
1417 */
1418 port_state_machine_change(sci_port,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001419 SCI_PORT_SUB_CONFIGURING);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001420 return SCI_SUCCESS;
1421 default:
1422 dev_warn(sciport_to_dev(sci_port),
1423 "%s: in wrong state: %d\n", __func__, state);
1424 return SCI_FAILURE_INVALID_STATE;
1425 }
1426}
1427
1428/**
1429 * scic_sds_port_remove_phy() -
1430 * @sci_port: This parameter specifies the port in which the phy will be added.
1431 * @sci_phy: This parameter is the phy which is to be added to the port.
1432 *
1433 * This method will remove the PHY from the selected PORT. This method returns
1434 * an enum sci_status. SCI_SUCCESS the phy has been removed from the port. Any
1435 * other status is a failure to add the phy to the port.
1436 */
1437enum sci_status scic_sds_port_remove_phy(struct scic_sds_port *sci_port,
Dan Williams85280952011-06-28 15:05:53 -07001438 struct isci_phy *iphy)
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001439{
1440 enum sci_status status;
1441 enum scic_sds_port_states state;
1442
Edmund Nadolskie3013702011-06-02 00:10:43 +00001443 state = sci_port->sm.current_state_id;
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001444
1445 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001446 case SCI_PORT_STOPPED:
Dan Williams85280952011-06-28 15:05:53 -07001447 return scic_sds_port_clear_phy(sci_port, iphy);
Edmund Nadolskie3013702011-06-02 00:10:43 +00001448 case SCI_PORT_SUB_OPERATIONAL:
Dan Williams85280952011-06-28 15:05:53 -07001449 status = scic_sds_port_clear_phy(sci_port, iphy);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001450 if (status != SCI_SUCCESS)
1451 return status;
1452
Dan Williams85280952011-06-28 15:05:53 -07001453 scic_sds_port_deactivate_phy(sci_port, iphy, true);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001454 sci_port->not_ready_reason = SCIC_PORT_NOT_READY_RECONFIGURING;
1455 port_state_machine_change(sci_port,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001456 SCI_PORT_SUB_CONFIGURING);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001457 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001458 case SCI_PORT_SUB_CONFIGURING:
Dan Williams85280952011-06-28 15:05:53 -07001459 status = scic_sds_port_clear_phy(sci_port, iphy);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001460
1461 if (status != SCI_SUCCESS)
1462 return status;
Dan Williams85280952011-06-28 15:05:53 -07001463 scic_sds_port_deactivate_phy(sci_port, iphy, true);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001464
1465 /* Re-enter the configuring state since this may be the last phy in
1466 * the port
1467 */
1468 port_state_machine_change(sci_port,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001469 SCI_PORT_SUB_CONFIGURING);
Piotr Sawicki051266c2011-05-12 19:10:14 +00001470 return SCI_SUCCESS;
1471 default:
1472 dev_warn(sciport_to_dev(sci_port),
1473 "%s: in wrong state: %d\n", __func__, state);
1474 return SCI_FAILURE_INVALID_STATE;
1475 }
1476}
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001477
Piotr Sawicki051266c2011-05-12 19:10:14 +00001478enum sci_status scic_sds_port_link_up(struct scic_sds_port *sci_port,
Dan Williams85280952011-06-28 15:05:53 -07001479 struct isci_phy *iphy)
Piotr Sawicki051266c2011-05-12 19:10:14 +00001480{
1481 enum scic_sds_port_states state;
1482
Edmund Nadolskie3013702011-06-02 00:10:43 +00001483 state = sci_port->sm.current_state_id;
Piotr Sawicki051266c2011-05-12 19:10:14 +00001484 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001485 case SCI_PORT_SUB_WAITING:
Piotr Sawicki051266c2011-05-12 19:10:14 +00001486 /* Since this is the first phy going link up for the port we
1487 * can just enable it and continue
1488 */
Dan Williams85280952011-06-28 15:05:53 -07001489 scic_sds_port_activate_phy(sci_port, iphy, true);
Piotr Sawicki051266c2011-05-12 19:10:14 +00001490
1491 port_state_machine_change(sci_port,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001492 SCI_PORT_SUB_OPERATIONAL);
Piotr Sawicki051266c2011-05-12 19:10:14 +00001493 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001494 case SCI_PORT_SUB_OPERATIONAL:
Dan Williams85280952011-06-28 15:05:53 -07001495 scic_sds_port_general_link_up_handler(sci_port, iphy, true);
Piotr Sawicki051266c2011-05-12 19:10:14 +00001496 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001497 case SCI_PORT_RESETTING:
Piotr Sawicki051266c2011-05-12 19:10:14 +00001498 /* TODO We should make sure that the phy that has gone
1499 * link up is the same one on which we sent the reset. It is
1500 * possible that the phy on which we sent the reset is not the
1501 * one that has gone link up and we want to make sure that
1502 * phy being reset comes back. Consider the case where a
1503 * reset is sent but before the hardware processes the reset it
1504 * get a link up on the port because of a hot plug event.
1505 * because of the reset request this phy will go link down
1506 * almost immediately.
1507 */
1508
1509 /* In the resetting state we don't notify the user regarding
1510 * link up and link down notifications.
1511 */
Dan Williams85280952011-06-28 15:05:53 -07001512 scic_sds_port_general_link_up_handler(sci_port, iphy, false);
Piotr Sawicki051266c2011-05-12 19:10:14 +00001513 return SCI_SUCCESS;
1514 default:
1515 dev_warn(sciport_to_dev(sci_port),
1516 "%s: in wrong state: %d\n", __func__, state);
1517 return SCI_FAILURE_INVALID_STATE;
1518 }
1519}
1520
1521enum sci_status scic_sds_port_link_down(struct scic_sds_port *sci_port,
Dan Williams85280952011-06-28 15:05:53 -07001522 struct isci_phy *iphy)
Piotr Sawicki051266c2011-05-12 19:10:14 +00001523{
1524 enum scic_sds_port_states state;
1525
Edmund Nadolskie3013702011-06-02 00:10:43 +00001526 state = sci_port->sm.current_state_id;
Piotr Sawicki051266c2011-05-12 19:10:14 +00001527 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001528 case SCI_PORT_SUB_OPERATIONAL:
Dan Williams85280952011-06-28 15:05:53 -07001529 scic_sds_port_deactivate_phy(sci_port, iphy, true);
Piotr Sawicki051266c2011-05-12 19:10:14 +00001530
1531 /* If there are no active phys left in the port, then
1532 * transition the port to the WAITING state until such time
1533 * as a phy goes link up
1534 */
1535 if (sci_port->active_phy_mask == 0)
1536 port_state_machine_change(sci_port,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001537 SCI_PORT_SUB_WAITING);
Piotr Sawicki051266c2011-05-12 19:10:14 +00001538 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001539 case SCI_PORT_RESETTING:
Piotr Sawicki051266c2011-05-12 19:10:14 +00001540 /* In the resetting state we don't notify the user regarding
1541 * link up and link down notifications. */
Dan Williams85280952011-06-28 15:05:53 -07001542 scic_sds_port_deactivate_phy(sci_port, iphy, false);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001543 return SCI_SUCCESS;
1544 default:
1545 dev_warn(sciport_to_dev(sci_port),
1546 "%s: in wrong state: %d\n", __func__, state);
1547 return SCI_FAILURE_INVALID_STATE;
1548 }
1549}
1550
Dan Williams68138202011-05-12 07:16:06 -07001551enum sci_status scic_sds_port_start_io(struct scic_sds_port *sci_port,
1552 struct scic_sds_remote_device *sci_dev,
Dan Williams5076a1a2011-06-27 14:57:03 -07001553 struct isci_request *ireq)
Dan Williams68138202011-05-12 07:16:06 -07001554{
1555 enum scic_sds_port_states state;
Dan Williamse2f8db502011-05-10 02:28:46 -07001556
Edmund Nadolskie3013702011-06-02 00:10:43 +00001557 state = sci_port->sm.current_state_id;
Dan Williams68138202011-05-12 07:16:06 -07001558 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001559 case SCI_PORT_SUB_WAITING:
Dan Williams68138202011-05-12 07:16:06 -07001560 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001561 case SCI_PORT_SUB_OPERATIONAL:
Dan Williams68138202011-05-12 07:16:06 -07001562 sci_port->started_request_count++;
1563 return SCI_SUCCESS;
1564 default:
1565 dev_warn(sciport_to_dev(sci_port),
1566 "%s: in wrong state: %d\n", __func__, state);
1567 return SCI_FAILURE_INVALID_STATE;
1568 }
1569}
1570
1571enum sci_status scic_sds_port_complete_io(struct scic_sds_port *sci_port,
1572 struct scic_sds_remote_device *sci_dev,
Dan Williams5076a1a2011-06-27 14:57:03 -07001573 struct isci_request *ireq)
Dan Williams68138202011-05-12 07:16:06 -07001574{
1575 enum scic_sds_port_states state;
1576
Edmund Nadolskie3013702011-06-02 00:10:43 +00001577 state = sci_port->sm.current_state_id;
Dan Williams68138202011-05-12 07:16:06 -07001578 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001579 case SCI_PORT_STOPPED:
Dan Williams68138202011-05-12 07:16:06 -07001580 dev_warn(sciport_to_dev(sci_port),
1581 "%s: in wrong state: %d\n", __func__, state);
1582 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001583 case SCI_PORT_STOPPING:
Dan Williams68138202011-05-12 07:16:06 -07001584 scic_sds_port_decrement_request_count(sci_port);
1585
1586 if (sci_port->started_request_count == 0)
1587 port_state_machine_change(sci_port,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001588 SCI_PORT_STOPPED);
Dan Williams68138202011-05-12 07:16:06 -07001589 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001590 case SCI_PORT_READY:
1591 case SCI_PORT_RESETTING:
1592 case SCI_PORT_FAILED:
1593 case SCI_PORT_SUB_WAITING:
1594 case SCI_PORT_SUB_OPERATIONAL:
Dan Williams68138202011-05-12 07:16:06 -07001595 scic_sds_port_decrement_request_count(sci_port);
1596 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001597 case SCI_PORT_SUB_CONFIGURING:
Dan Williams68138202011-05-12 07:16:06 -07001598 scic_sds_port_decrement_request_count(sci_port);
1599 if (sci_port->started_request_count == 0) {
1600 port_state_machine_change(sci_port,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001601 SCI_PORT_SUB_OPERATIONAL);
Dan Williams68138202011-05-12 07:16:06 -07001602 }
1603 break;
1604 }
1605 return SCI_SUCCESS;
1606}
Dan Williamse2f8db502011-05-10 02:28:46 -07001607
1608/**
1609 *
1610 * @sci_port: This is the port object which to suspend.
1611 *
1612 * This method will enable the SCU Port Task Scheduler for this port object but
1613 * will leave the port task scheduler in a suspended state. none
1614 */
1615static void
1616scic_sds_port_enable_port_task_scheduler(struct scic_sds_port *port)
1617{
1618 u32 pts_control_value;
1619
1620 pts_control_value = readl(&port->port_task_scheduler_registers->control);
1621 pts_control_value |= SCU_PTSxCR_GEN_BIT(ENABLE) | SCU_PTSxCR_GEN_BIT(SUSPEND);
1622 writel(pts_control_value, &port->port_task_scheduler_registers->control);
1623}
1624
1625/**
1626 *
1627 * @sci_port: This is the port object which to resume.
1628 *
1629 * This method will disable the SCU port task scheduler for this port object.
1630 * none
1631 */
1632static void
1633scic_sds_port_disable_port_task_scheduler(struct scic_sds_port *port)
1634{
1635 u32 pts_control_value;
1636
1637 pts_control_value = readl(&port->port_task_scheduler_registers->control);
1638 pts_control_value &=
1639 ~(SCU_PTSxCR_GEN_BIT(ENABLE) | SCU_PTSxCR_GEN_BIT(SUSPEND));
1640 writel(pts_control_value, &port->port_task_scheduler_registers->control);
1641}
1642
1643static void scic_sds_port_post_dummy_remote_node(struct scic_sds_port *sci_port)
1644{
1645 struct scic_sds_controller *scic = sci_port->owning_controller;
1646 u8 phys_index = sci_port->physical_port_index;
1647 union scu_remote_node_context *rnc;
1648 u16 rni = sci_port->reserved_rni;
1649 u32 command;
1650
1651 rnc = &scic->remote_node_context_table[rni];
1652 rnc->ssp.is_valid = true;
1653
1654 command = SCU_CONTEXT_COMMAND_POST_RNC_32 |
1655 phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
1656
1657 scic_sds_controller_post_request(scic, command);
1658
1659 /* ensure hardware has seen the post rnc command and give it
1660 * ample time to act before sending the suspend
1661 */
1662 readl(&scic->smu_registers->interrupt_status); /* flush */
1663 udelay(10);
1664
1665 command = SCU_CONTEXT_COMMAND_POST_RNC_SUSPEND_TX_RX |
1666 phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
1667
1668 scic_sds_controller_post_request(scic, command);
1669}
1670
Dan Williams9269e0e2011-05-12 07:42:17 -07001671static void scic_sds_port_stopped_state_enter(struct sci_base_state_machine *sm)
Dan Williamse2f8db502011-05-10 02:28:46 -07001672{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001673 struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), sm);
Dan Williamse2f8db502011-05-10 02:28:46 -07001674
Edmund Nadolskie3013702011-06-02 00:10:43 +00001675 if (sci_port->sm.previous_state_id == SCI_PORT_STOPPING) {
Dan Williamse2f8db502011-05-10 02:28:46 -07001676 /*
1677 * If we enter this state becasuse of a request to stop
1678 * the port then we want to disable the hardwares port
1679 * task scheduler. */
1680 scic_sds_port_disable_port_task_scheduler(sci_port);
1681 }
1682}
1683
Dan Williams9269e0e2011-05-12 07:42:17 -07001684static void scic_sds_port_stopped_state_exit(struct sci_base_state_machine *sm)
Dan Williamse2f8db502011-05-10 02:28:46 -07001685{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001686 struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), sm);
Dan Williamse2f8db502011-05-10 02:28:46 -07001687
1688 /* Enable and suspend the port task scheduler */
1689 scic_sds_port_enable_port_task_scheduler(sci_port);
1690}
1691
Dan Williams9269e0e2011-05-12 07:42:17 -07001692static void scic_sds_port_ready_state_enter(struct sci_base_state_machine *sm)
Dan Williamse2f8db502011-05-10 02:28:46 -07001693{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001694 struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), sm);
Dan Williamse2f8db502011-05-10 02:28:46 -07001695 struct scic_sds_controller *scic = sci_port->owning_controller;
1696 struct isci_host *ihost = scic_to_ihost(scic);
1697 struct isci_port *iport = sci_port_to_iport(sci_port);
1698 u32 prev_state;
1699
Edmund Nadolskie3013702011-06-02 00:10:43 +00001700 prev_state = sci_port->sm.previous_state_id;
1701 if (prev_state == SCI_PORT_RESETTING)
Dan Williamse2f8db502011-05-10 02:28:46 -07001702 isci_port_hard_reset_complete(iport, SCI_SUCCESS);
1703 else
1704 isci_port_not_ready(ihost, iport);
1705
1706 /* Post and suspend the dummy remote node context for this port. */
1707 scic_sds_port_post_dummy_remote_node(sci_port);
1708
1709 /* Start the ready substate machine */
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001710 port_state_machine_change(sci_port,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001711 SCI_PORT_SUB_WAITING);
Dan Williamse2f8db502011-05-10 02:28:46 -07001712}
1713
Dan Williams9269e0e2011-05-12 07:42:17 -07001714static void scic_sds_port_resetting_state_exit(struct sci_base_state_machine *sm)
Dan Williamse2f8db502011-05-10 02:28:46 -07001715{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001716 struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), sm);
Dan Williamse2f8db502011-05-10 02:28:46 -07001717
Edmund Nadolski5553ba22011-05-19 11:59:10 +00001718 sci_del_timer(&sci_port->timer);
Dan Williamse2f8db502011-05-10 02:28:46 -07001719}
1720
Dan Williams9269e0e2011-05-12 07:42:17 -07001721static void scic_sds_port_stopping_state_exit(struct sci_base_state_machine *sm)
Dan Williamse2f8db502011-05-10 02:28:46 -07001722{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001723 struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), sm);
Dan Williamse2f8db502011-05-10 02:28:46 -07001724
Edmund Nadolski5553ba22011-05-19 11:59:10 +00001725 sci_del_timer(&sci_port->timer);
Dan Williamse2f8db502011-05-10 02:28:46 -07001726
1727 scic_sds_port_destroy_dummy_resources(sci_port);
1728}
1729
Dan Williams9269e0e2011-05-12 07:42:17 -07001730static void scic_sds_port_failed_state_enter(struct sci_base_state_machine *sm)
Dan Williamse2f8db502011-05-10 02:28:46 -07001731{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001732 struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), sm);
Dan Williamse2f8db502011-05-10 02:28:46 -07001733 struct isci_port *iport = sci_port_to_iport(sci_port);
1734
Dan Williamse2f8db502011-05-10 02:28:46 -07001735 isci_port_hard_reset_complete(iport, SCI_FAILURE_TIMEOUT);
1736}
1737
1738/* --------------------------------------------------------------------------- */
1739
1740static const struct sci_base_state scic_sds_port_state_table[] = {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001741 [SCI_PORT_STOPPED] = {
Dan Williamse2f8db502011-05-10 02:28:46 -07001742 .enter_state = scic_sds_port_stopped_state_enter,
1743 .exit_state = scic_sds_port_stopped_state_exit
1744 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001745 [SCI_PORT_STOPPING] = {
Dan Williamse2f8db502011-05-10 02:28:46 -07001746 .exit_state = scic_sds_port_stopping_state_exit
1747 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001748 [SCI_PORT_READY] = {
Dan Williamse2f8db502011-05-10 02:28:46 -07001749 .enter_state = scic_sds_port_ready_state_enter,
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001750 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001751 [SCI_PORT_SUB_WAITING] = {
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001752 .enter_state = scic_sds_port_ready_substate_waiting_enter,
1753 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001754 [SCI_PORT_SUB_OPERATIONAL] = {
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001755 .enter_state = scic_sds_port_ready_substate_operational_enter,
1756 .exit_state = scic_sds_port_ready_substate_operational_exit
1757 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001758 [SCI_PORT_SUB_CONFIGURING] = {
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001759 .enter_state = scic_sds_port_ready_substate_configuring_enter,
1760 .exit_state = scic_sds_port_ready_substate_configuring_exit
Dan Williamse2f8db502011-05-10 02:28:46 -07001761 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001762 [SCI_PORT_RESETTING] = {
Dan Williamse2f8db502011-05-10 02:28:46 -07001763 .exit_state = scic_sds_port_resetting_state_exit
1764 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001765 [SCI_PORT_FAILED] = {
Dan Williamse2f8db502011-05-10 02:28:46 -07001766 .enter_state = scic_sds_port_failed_state_enter,
1767 }
1768};
1769
1770void scic_sds_port_construct(struct scic_sds_port *sci_port, u8 index,
1771 struct scic_sds_controller *scic)
1772{
Edmund Nadolski12ef6542011-06-02 00:10:50 +00001773 sci_init_sm(&sci_port->sm, scic_sds_port_state_table, SCI_PORT_STOPPED);
Dan Williamse2f8db502011-05-10 02:28:46 -07001774
Dan Williamse2f8db502011-05-10 02:28:46 -07001775 sci_port->logical_port_index = SCIC_SDS_DUMMY_PORT;
1776 sci_port->physical_port_index = index;
1777 sci_port->active_phy_mask = 0;
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001778 sci_port->ready_exit = false;
Dan Williamse2f8db502011-05-10 02:28:46 -07001779
1780 sci_port->owning_controller = scic;
1781
1782 sci_port->started_request_count = 0;
1783 sci_port->assigned_device_count = 0;
1784
1785 sci_port->reserved_rni = SCU_DUMMY_INDEX;
Dan Williams312e0c22011-06-28 13:47:09 -07001786 sci_port->reserved_tag = SCI_CONTROLLER_INVALID_IO_TAG;
Dan Williamse2f8db502011-05-10 02:28:46 -07001787
Edmund Nadolski5553ba22011-05-19 11:59:10 +00001788 sci_init_timer(&sci_port->timer, port_timeout);
1789
Dan Williamse2f8db502011-05-10 02:28:46 -07001790 sci_port->port_task_scheduler_registers = NULL;
1791
1792 for (index = 0; index < SCI_MAX_PHYS; index++)
1793 sci_port->phy_table[index] = NULL;
1794}
1795
1796void isci_port_init(struct isci_port *iport, struct isci_host *ihost, int index)
1797{
1798 INIT_LIST_HEAD(&iport->remote_dev_list);
1799 INIT_LIST_HEAD(&iport->domain_dev_list);
1800 spin_lock_init(&iport->state_lock);
1801 init_completion(&iport->start_complete);
1802 iport->isci_host = ihost;
1803 isci_port_change_state(iport, isci_freed);
Jeff Skirvin61aaff42011-06-21 12:16:33 -07001804 atomic_set(&iport->event, 0);
Dan Williamse2f8db502011-05-10 02:28:46 -07001805}
1806
1807/**
1808 * isci_port_get_state() - This function gets the status of the port object.
1809 * @isci_port: This parameter points to the isci_port object
1810 *
1811 * status of the object as a isci_status enum.
1812 */
1813enum isci_status isci_port_get_state(
1814 struct isci_port *isci_port)
1815{
1816 return isci_port->status;
1817}
1818
Dan Williamse2f8db502011-05-10 02:28:46 -07001819void scic_sds_port_broadcast_change_received(
1820 struct scic_sds_port *sci_port,
Dan Williams85280952011-06-28 15:05:53 -07001821 struct isci_phy *iphy)
Dan Williamse2f8db502011-05-10 02:28:46 -07001822{
1823 struct scic_sds_controller *scic = sci_port->owning_controller;
1824 struct isci_host *ihost = scic_to_ihost(scic);
1825
1826 /* notify the user. */
Dan Williams85280952011-06-28 15:05:53 -07001827 isci_port_bc_change_received(ihost, sci_port, iphy);
Dan Williamse2f8db502011-05-10 02:28:46 -07001828}
1829
Dan Williams4393aa42011-03-31 13:10:44 -07001830int isci_port_perform_hard_reset(struct isci_host *ihost, struct isci_port *iport,
1831 struct isci_phy *iphy)
Dan Williams6f231dd2011-07-02 22:56:22 -07001832{
Dan Williams4393aa42011-03-31 13:10:44 -07001833 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -07001834 enum sci_status status;
Jeff Skirvinfd0527a2011-06-20 14:09:26 -07001835 int idx, ret = TMF_RESP_FUNC_COMPLETE;
Dan Williams6f231dd2011-07-02 22:56:22 -07001836
Dan Williams4393aa42011-03-31 13:10:44 -07001837 dev_dbg(&ihost->pdev->dev, "%s: iport = %p\n",
1838 __func__, iport);
Dan Williams6f231dd2011-07-02 22:56:22 -07001839
Dan Williams4393aa42011-03-31 13:10:44 -07001840 init_completion(&iport->hard_reset_complete);
Dan Williams6f231dd2011-07-02 22:56:22 -07001841
Dan Williams4393aa42011-03-31 13:10:44 -07001842 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001843
1844 #define ISCI_PORT_RESET_TIMEOUT SCIC_SDS_SIGNATURE_FIS_TIMEOUT
Dan Williamse5313812011-05-07 10:11:43 -07001845 status = scic_port_hard_reset(&iport->sci, ISCI_PORT_RESET_TIMEOUT);
Dan Williams6f231dd2011-07-02 22:56:22 -07001846
Dan Williams4393aa42011-03-31 13:10:44 -07001847 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001848
1849 if (status == SCI_SUCCESS) {
Dan Williams4393aa42011-03-31 13:10:44 -07001850 wait_for_completion(&iport->hard_reset_complete);
Dan Williams6f231dd2011-07-02 22:56:22 -07001851
Dan Williams4393aa42011-03-31 13:10:44 -07001852 dev_dbg(&ihost->pdev->dev,
1853 "%s: iport = %p; hard reset completion\n",
1854 __func__, iport);
Dan Williams6f231dd2011-07-02 22:56:22 -07001855
Dan Williams4393aa42011-03-31 13:10:44 -07001856 if (iport->hard_reset_status != SCI_SUCCESS)
Dan Williams6f231dd2011-07-02 22:56:22 -07001857 ret = TMF_RESP_FUNC_FAILED;
1858 } else {
1859 ret = TMF_RESP_FUNC_FAILED;
1860
Dan Williams4393aa42011-03-31 13:10:44 -07001861 dev_err(&ihost->pdev->dev,
1862 "%s: iport = %p; scic_port_hard_reset call"
Dan Williams6f231dd2011-07-02 22:56:22 -07001863 " failed 0x%x\n",
Dan Williams4393aa42011-03-31 13:10:44 -07001864 __func__, iport, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001865
1866 }
1867
1868 /* If the hard reset for the port has failed, consider this
1869 * the same as link failures on all phys in the port.
1870 */
1871 if (ret != TMF_RESP_FUNC_COMPLETE) {
Jeff Skirvinfd0527a2011-06-20 14:09:26 -07001872
Dan Williams4393aa42011-03-31 13:10:44 -07001873 dev_err(&ihost->pdev->dev,
1874 "%s: iport = %p; hard reset failed "
Jeff Skirvinfd0527a2011-06-20 14:09:26 -07001875 "(0x%x) - driving explicit link fail for all phys\n",
1876 __func__, iport, iport->hard_reset_status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001877
Jeff Skirvinfd0527a2011-06-20 14:09:26 -07001878 /* Down all phys in the port. */
1879 spin_lock_irqsave(&ihost->scic_lock, flags);
1880 for (idx = 0; idx < SCI_MAX_PHYS; ++idx) {
1881
1882 if (iport->sci.phy_table[idx] != NULL) {
1883
1884 scic_sds_phy_stop(
1885 iport->sci.phy_table[idx]);
1886 scic_sds_phy_start(
1887 iport->sci.phy_table[idx]);
1888 }
1889 }
1890 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001891 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001892 return ret;
1893}
Dave Jiang09d7da12011-03-26 16:11:51 -07001894
Dan Williamse2f8db502011-05-10 02:28:46 -07001895/**
1896 * isci_port_deformed() - This function is called by libsas when a port becomes
1897 * inactive.
1898 * @phy: This parameter specifies the libsas phy with the inactive port.
1899 *
1900 */
1901void isci_port_deformed(struct asd_sas_phy *phy)
Dave Jiang09d7da12011-03-26 16:11:51 -07001902{
Dan Williamse2f8db502011-05-10 02:28:46 -07001903 pr_debug("%s: sas_phy = %p\n", __func__, phy);
1904}
1905
1906/**
1907 * isci_port_formed() - This function is called by libsas when a port becomes
1908 * active.
1909 * @phy: This parameter specifies the libsas phy with the active port.
1910 *
1911 */
1912void isci_port_formed(struct asd_sas_phy *phy)
1913{
1914 pr_debug("%s: sas_phy = %p, sas_port = %p\n", __func__, phy, phy->port);
Dave Jiang09d7da12011-03-26 16:11:51 -07001915}