blob: c5ae94dbef48dfba7253336a6c794410a5ee14ea [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 Williamse2f8db52011-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 Williamsc132f692012-01-03 23:26:08 -080063static struct device *sciport_to_dev(struct isci_port *iport)
64{
65 int i = iport->physical_port_index;
66 struct isci_port *table;
67 struct isci_host *ihost;
68
69 if (i == SCIC_SDS_DUMMY_PORT)
70 i = SCI_MAX_PORTS+1;
71
72 table = iport - i;
73 ihost = container_of(table, typeof(*ihost), ports[0]);
74
75 return &ihost->pdev->dev;
76}
77
Dan Williams89a73012011-06-30 19:14:33 -070078static void sci_port_get_protocols(struct isci_port *iport, struct sci_phy_proto *proto)
Dan Williams6f231dd2011-07-02 22:56:22 -070079{
Dan Williamse2f8db52011-05-10 02:28:46 -070080 u8 index;
81
Dan Williams89a73012011-06-30 19:14:33 -070082 proto->all = 0;
Dan Williamse2f8db52011-05-10 02:28:46 -070083 for (index = 0; index < SCI_MAX_PHYS; index++) {
Dan Williams89a73012011-06-30 19:14:33 -070084 struct isci_phy *iphy = iport->phy_table[index];
85
86 if (!iphy)
87 continue;
88 sci_phy_get_protocols(iphy, proto);
Dan Williamse2f8db52011-05-10 02:28:46 -070089 }
Dan Williams6f231dd2011-07-02 22:56:22 -070090}
91
Dan Williams89a73012011-06-30 19:14:33 -070092static u32 sci_port_get_phys(struct isci_port *iport)
Dan Williams6f231dd2011-07-02 22:56:22 -070093{
Dan Williamse2f8db52011-05-10 02:28:46 -070094 u32 index;
95 u32 mask;
96
97 mask = 0;
Dan Williams89a73012011-06-30 19:14:33 -070098 for (index = 0; index < SCI_MAX_PHYS; index++)
99 if (iport->phy_table[index])
Dan Williamse2f8db52011-05-10 02:28:46 -0700100 mask |= (1 << index);
Dan Williamse2f8db52011-05-10 02:28:46 -0700101
102 return mask;
Dan Williams6f231dd2011-07-02 22:56:22 -0700103}
104
Dan Williamse2f8db52011-05-10 02:28:46 -0700105/**
Dan Williams89a73012011-06-30 19:14:33 -0700106 * sci_port_get_properties() - This method simply returns the properties
Dan Williamse2f8db52011-05-10 02:28:46 -0700107 * regarding the port, such as: physical index, protocols, sas address, etc.
108 * @port: this parameter specifies the port for which to retrieve the physical
109 * index.
110 * @properties: This parameter specifies the properties structure into which to
111 * copy the requested information.
112 *
113 * Indicate if the user specified a valid port. SCI_SUCCESS This value is
114 * returned if the specified port was valid. SCI_FAILURE_INVALID_PORT This
115 * value is returned if the specified port is not valid. When this value is
116 * returned, no data is copied to the properties output parameter.
117 */
Bartek Nowakowski7e629842012-01-04 01:33:20 -0800118enum sci_status sci_port_get_properties(struct isci_port *iport,
Dan Williams89a73012011-06-30 19:14:33 -0700119 struct sci_port_properties *prop)
Dan Williams6f231dd2011-07-02 22:56:22 -0700120{
Dan Williamsffe191c2011-06-29 13:09:25 -0700121 if (!iport || iport->logical_port_index == SCIC_SDS_DUMMY_PORT)
Dan Williamse2f8db52011-05-10 02:28:46 -0700122 return SCI_FAILURE_INVALID_PORT;
Dan Williams6f231dd2011-07-02 22:56:22 -0700123
Dan Williams89a73012011-06-30 19:14:33 -0700124 prop->index = iport->logical_port_index;
125 prop->phy_mask = sci_port_get_phys(iport);
126 sci_port_get_sas_address(iport, &prop->local.sas_address);
127 sci_port_get_protocols(iport, &prop->local.protocols);
128 sci_port_get_attached_sas_address(iport, &prop->remote.sas_address);
Dan Williams6f231dd2011-07-02 22:56:22 -0700129
Dan Williamse2f8db52011-05-10 02:28:46 -0700130 return SCI_SUCCESS;
Dan Williams6f231dd2011-07-02 22:56:22 -0700131}
132
Dan Williams89a73012011-06-30 19:14:33 -0700133static void sci_port_bcn_enable(struct isci_port *iport)
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700134{
Dan Williams85280952011-06-28 15:05:53 -0700135 struct isci_phy *iphy;
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700136 u32 val;
137 int i;
138
Dan Williamsffe191c2011-06-29 13:09:25 -0700139 for (i = 0; i < ARRAY_SIZE(iport->phy_table); i++) {
140 iphy = iport->phy_table[i];
Dan Williams85280952011-06-28 15:05:53 -0700141 if (!iphy)
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700142 continue;
Dan Williams85280952011-06-28 15:05:53 -0700143 val = readl(&iphy->link_layer_registers->link_layer_control);
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700144 /* clear the bit by writing 1. */
Dan Williams85280952011-06-28 15:05:53 -0700145 writel(val, &iphy->link_layer_registers->link_layer_control);
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700146 }
147}
148
Dan Williamsffe191c2011-06-29 13:09:25 -0700149static void isci_port_bc_change_received(struct isci_host *ihost,
150 struct isci_port *iport,
151 struct isci_phy *iphy)
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700152{
Dan Williams52d74632011-10-27 15:05:37 -0700153 dev_dbg(&ihost->pdev->dev,
154 "%s: isci_phy = %p, sas_phy = %p\n",
155 __func__, iphy, &iphy->sas_phy);
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700156
Dan Williams52d74632011-10-27 15:05:37 -0700157 ihost->sas_ha.notify_port_event(&iphy->sas_phy, PORTE_BROADCAST_RCVD);
Dan Williams89a73012011-06-30 19:14:33 -0700158 sci_port_bcn_enable(iport);
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700159}
160
Dan Williamse2f8db52011-05-10 02:28:46 -0700161static void isci_port_link_up(struct isci_host *isci_host,
Dan Williamsffe191c2011-06-29 13:09:25 -0700162 struct isci_port *iport,
Dan Williams85280952011-06-28 15:05:53 -0700163 struct isci_phy *iphy)
Dan Williams6f231dd2011-07-02 22:56:22 -0700164{
165 unsigned long flags;
Dan Williams89a73012011-06-30 19:14:33 -0700166 struct sci_port_properties properties;
Dan Williams6f231dd2011-07-02 22:56:22 -0700167 unsigned long success = true;
168
Dan Williams6f231dd2011-07-02 22:56:22 -0700169 dev_dbg(&isci_host->pdev->dev,
170 "%s: isci_port = %p\n",
Dan Williamsffe191c2011-06-29 13:09:25 -0700171 __func__, iport);
Dan Williams6f231dd2011-07-02 22:56:22 -0700172
Dan Williams85280952011-06-28 15:05:53 -0700173 spin_lock_irqsave(&iphy->sas_phy.frame_rcvd_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700174
Dan Williams89a73012011-06-30 19:14:33 -0700175 sci_port_get_properties(iport, &properties);
Dan Williams6f231dd2011-07-02 22:56:22 -0700176
Dan Williams85280952011-06-28 15:05:53 -0700177 if (iphy->protocol == SCIC_SDS_PHY_PROTOCOL_SATA) {
Dan Williams150fc6f2011-02-25 10:25:21 -0800178 u64 attached_sas_address;
Dan Williams6f231dd2011-07-02 22:56:22 -0700179
Dan Williams85280952011-06-28 15:05:53 -0700180 iphy->sas_phy.oob_mode = SATA_OOB_MODE;
181 iphy->sas_phy.frame_rcvd_size = sizeof(struct dev_to_host_fis);
Dan Williams6f231dd2011-07-02 22:56:22 -0700182
183 /*
184 * For direct-attached SATA devices, the SCI core will
185 * automagically assign a SAS address to the end device
186 * for the purpose of creating a port. This SAS address
187 * will not be the same as assigned to the PHY and needs
Dan Williams89a73012011-06-30 19:14:33 -0700188 * to be obtained from struct sci_port_properties properties.
Dan Williams6f231dd2011-07-02 22:56:22 -0700189 */
Dan Williams150fc6f2011-02-25 10:25:21 -0800190 attached_sas_address = properties.remote.sas_address.high;
191 attached_sas_address <<= 32;
192 attached_sas_address |= properties.remote.sas_address.low;
193 swab64s(&attached_sas_address);
Dan Williams6f231dd2011-07-02 22:56:22 -0700194
Dan Williams85280952011-06-28 15:05:53 -0700195 memcpy(&iphy->sas_phy.attached_sas_addr,
Dan Williams150fc6f2011-02-25 10:25:21 -0800196 &attached_sas_address, sizeof(attached_sas_address));
Dan Williams85280952011-06-28 15:05:53 -0700197 } else if (iphy->protocol == SCIC_SDS_PHY_PROTOCOL_SAS) {
198 iphy->sas_phy.oob_mode = SAS_OOB_MODE;
199 iphy->sas_phy.frame_rcvd_size = sizeof(struct sas_identify_frame);
Dan Williams6f231dd2011-07-02 22:56:22 -0700200
201 /* Copy the attached SAS address from the IAF */
Dan Williams85280952011-06-28 15:05:53 -0700202 memcpy(iphy->sas_phy.attached_sas_addr,
203 iphy->frame_rcvd.iaf.sas_addr, SAS_ADDR_SIZE);
Dan Williams6f231dd2011-07-02 22:56:22 -0700204 } else {
205 dev_err(&isci_host->pdev->dev, "%s: unkown target\n", __func__);
206 success = false;
207 }
208
Dan Williams85280952011-06-28 15:05:53 -0700209 iphy->sas_phy.phy->negotiated_linkrate = sci_phy_linkrate(iphy);
Dan Williams83e51432011-02-18 09:25:13 -0800210
Dan Williams85280952011-06-28 15:05:53 -0700211 spin_unlock_irqrestore(&iphy->sas_phy.frame_rcvd_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700212
213 /* Notify libsas that we have an address frame, if indeed
214 * we've found an SSP, SMP, or STP target */
215 if (success)
Dan Williams85280952011-06-28 15:05:53 -0700216 isci_host->sas_ha.notify_port_event(&iphy->sas_phy,
Dan Williams6f231dd2011-07-02 22:56:22 -0700217 PORTE_BYTES_DMAED);
218}
219
220
221/**
222 * isci_port_link_down() - This function is called by the sci core when a link
223 * becomes inactive.
224 * @isci_host: This parameter specifies the isci host object.
225 * @phy: This parameter specifies the isci phy with the active link.
226 * @port: This parameter specifies the isci port with the active link.
227 *
228 */
Dan Williamse2f8db52011-05-10 02:28:46 -0700229static void isci_port_link_down(struct isci_host *isci_host,
230 struct isci_phy *isci_phy,
231 struct isci_port *isci_port)
Dan Williams6f231dd2011-07-02 22:56:22 -0700232{
233 struct isci_remote_device *isci_device;
234
235 dev_dbg(&isci_host->pdev->dev,
236 "%s: isci_port = %p\n", __func__, isci_port);
237
238 if (isci_port) {
239
240 /* check to see if this is the last phy on this port. */
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700241 if (isci_phy->sas_phy.port &&
242 isci_phy->sas_phy.port->num_phys == 1) {
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700243 /* change the state for all devices on this port. The
244 * next task sent to this device will be returned as
245 * SAS_TASK_UNDELIVERED, and the scsi mid layer will
246 * remove the target
Dan Williams6f231dd2011-07-02 22:56:22 -0700247 */
248 list_for_each_entry(isci_device,
249 &isci_port->remote_dev_list,
250 node) {
251 dev_dbg(&isci_host->pdev->dev,
252 "%s: isci_device = %p\n",
253 __func__, isci_device);
Dan Williams209fae12011-06-13 17:39:44 -0700254 set_bit(IDEV_GONE, &isci_device->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700255 }
256 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700257 }
258
259 /* Notify libsas of the borken link, this will trigger calls to our
260 * isci_port_deformed and isci_dev_gone functions.
261 */
262 sas_phy_disconnected(&isci_phy->sas_phy);
263 isci_host->sas_ha.notify_phy_event(&isci_phy->sas_phy,
264 PHYE_LOSS_OF_SIGNAL);
265
Dan Williams6f231dd2011-07-02 22:56:22 -0700266 dev_dbg(&isci_host->pdev->dev,
267 "%s: isci_port = %p - Done\n", __func__, isci_port);
268}
269
Jeff Skirvin8e35a132011-10-27 15:05:32 -0700270static bool is_port_ready_state(enum sci_port_states state)
271{
272 switch (state) {
273 case SCI_PORT_READY:
274 case SCI_PORT_SUB_WAITING:
275 case SCI_PORT_SUB_OPERATIONAL:
276 case SCI_PORT_SUB_CONFIGURING:
277 return true;
278 default:
279 return false;
280 }
281}
282
283/* flag dummy rnc hanling when exiting a ready state */
284static void port_state_machine_change(struct isci_port *iport,
285 enum sci_port_states state)
286{
287 struct sci_base_state_machine *sm = &iport->sm;
288 enum sci_port_states old_state = sm->current_state_id;
289
290 if (is_port_ready_state(old_state) && !is_port_ready_state(state))
291 iport->ready_exit = true;
292
293 sci_change_state(sm, state);
294 iport->ready_exit = false;
295}
296
Dan Williams6f231dd2011-07-02 22:56:22 -0700297/**
298 * isci_port_hard_reset_complete() - This function is called by the sci core
299 * when the hard reset complete notification has been received.
300 * @port: This parameter specifies the sci port with the active link.
301 * @completion_status: This parameter specifies the core status for the reset
302 * process.
303 *
304 */
Dan Williamse2f8db52011-05-10 02:28:46 -0700305static void isci_port_hard_reset_complete(struct isci_port *isci_port,
306 enum sci_status completion_status)
Dan Williams6f231dd2011-07-02 22:56:22 -0700307{
Dan Williams92776992011-11-30 11:57:34 -0800308 struct isci_host *ihost = isci_port->owning_controller;
309
310 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -0700311 "%s: isci_port = %p, completion_status=%x\n",
312 __func__, isci_port, completion_status);
313
314 /* Save the status of the hard reset from the port. */
315 isci_port->hard_reset_status = completion_status;
316
Jeff Skirvin8e35a132011-10-27 15:05:32 -0700317 if (completion_status != SCI_SUCCESS) {
318
319 /* The reset failed. The port state is now SCI_PORT_FAILED. */
320 if (isci_port->active_phy_mask == 0) {
Dan Williams92776992011-11-30 11:57:34 -0800321 int phy_idx = isci_port->last_active_phy;
322 struct isci_phy *iphy = &ihost->phys[phy_idx];
Jeff Skirvin8e35a132011-10-27 15:05:32 -0700323
324 /* Generate the link down now to the host, since it
325 * was intercepted by the hard reset state machine when
326 * it really happened.
327 */
Dan Williams92776992011-11-30 11:57:34 -0800328 isci_port_link_down(ihost, iphy, isci_port);
Jeff Skirvin8e35a132011-10-27 15:05:32 -0700329 }
330 /* Advance the port state so that link state changes will be
Dan Williams92776992011-11-30 11:57:34 -0800331 * noticed.
332 */
Jeff Skirvin8e35a132011-10-27 15:05:32 -0700333 port_state_machine_change(isci_port, SCI_PORT_SUB_WAITING);
334
335 }
Dan Williams92776992011-11-30 11:57:34 -0800336 clear_bit(IPORT_RESET_PENDING, &isci_port->state);
337 wake_up(&ihost->eventq);
338
Dan Williams6f231dd2011-07-02 22:56:22 -0700339}
Dan Williams4393aa42011-03-31 13:10:44 -0700340
Dan Williamse2f8db52011-05-10 02:28:46 -0700341/* This method will return a true value if the specified phy can be assigned to
342 * this port The following is a list of phys for each port that are allowed: -
343 * Port 0 - 3 2 1 0 - Port 1 - 1 - Port 2 - 3 2 - Port 3 - 3 This method
344 * doesn't preclude all configurations. It merely ensures that a phy is part
345 * of the allowable set of phy identifiers for that port. For example, one
346 * could assign phy 3 to port 0 and no other phys. Please refer to
Dan Williams89a73012011-06-30 19:14:33 -0700347 * sci_port_is_phy_mask_valid() for information regarding whether the
Dan Williamse2f8db52011-05-10 02:28:46 -0700348 * phy_mask for a port can be supported. bool true if this is a valid phy
349 * assignment for the port false if this is not a valid phy assignment for the
350 * port
351 */
Dan Williams89a73012011-06-30 19:14:33 -0700352bool sci_port_is_valid_phy_assignment(struct isci_port *iport, u32 phy_index)
Dan Williamse2f8db52011-05-10 02:28:46 -0700353{
Dan Williams89a73012011-06-30 19:14:33 -0700354 struct isci_host *ihost = iport->owning_controller;
355 struct sci_user_parameters *user = &ihost->user_parameters;
356
Dan Williamse2f8db52011-05-10 02:28:46 -0700357 /* Initialize to invalid value. */
358 u32 existing_phy_index = SCI_MAX_PHYS;
359 u32 index;
360
Dan Williams89a73012011-06-30 19:14:33 -0700361 if ((iport->physical_port_index == 1) && (phy_index != 1))
Dan Williamse2f8db52011-05-10 02:28:46 -0700362 return false;
Dan Williamse2f8db52011-05-10 02:28:46 -0700363
Dan Williams89a73012011-06-30 19:14:33 -0700364 if (iport->physical_port_index == 3 && phy_index != 3)
Dan Williamse2f8db52011-05-10 02:28:46 -0700365 return false;
Dan Williamse2f8db52011-05-10 02:28:46 -0700366
Dan Williams89a73012011-06-30 19:14:33 -0700367 if (iport->physical_port_index == 2 &&
368 (phy_index == 0 || phy_index == 1))
Dan Williamse2f8db52011-05-10 02:28:46 -0700369 return false;
Dan Williamse2f8db52011-05-10 02:28:46 -0700370
Dan Williams89a73012011-06-30 19:14:33 -0700371 for (index = 0; index < SCI_MAX_PHYS; index++)
372 if (iport->phy_table[index] && index != phy_index)
Dan Williamse2f8db52011-05-10 02:28:46 -0700373 existing_phy_index = index;
Dan Williamse2f8db52011-05-10 02:28:46 -0700374
Dan Williams89a73012011-06-30 19:14:33 -0700375 /* Ensure that all of the phys in the port are capable of
376 * operating at the same maximum link rate.
377 */
378 if (existing_phy_index < SCI_MAX_PHYS &&
379 user->phys[phy_index].max_speed_generation !=
380 user->phys[existing_phy_index].max_speed_generation)
Dan Williamse2f8db52011-05-10 02:28:46 -0700381 return false;
382
383 return true;
384}
385
386/**
387 *
388 * @sci_port: This is the port object for which to determine if the phy mask
389 * can be supported.
390 *
391 * This method will return a true value if the port's phy mask can be supported
392 * by the SCU. The following is a list of valid PHY mask configurations for
393 * each port: - Port 0 - [[3 2] 1] 0 - Port 1 - [1] - Port 2 - [[3] 2]
394 * - Port 3 - [3] This method returns a boolean indication specifying if the
395 * phy mask can be supported. true if this is a valid phy assignment for the
396 * port false if this is not a valid phy assignment for the port
397 */
Dan Williams89a73012011-06-30 19:14:33 -0700398static bool sci_port_is_phy_mask_valid(
Dan Williamsffe191c2011-06-29 13:09:25 -0700399 struct isci_port *iport,
Dan Williamse2f8db52011-05-10 02:28:46 -0700400 u32 phy_mask)
401{
Dan Williamsffe191c2011-06-29 13:09:25 -0700402 if (iport->physical_port_index == 0) {
Dan Williamse2f8db52011-05-10 02:28:46 -0700403 if (((phy_mask & 0x0F) == 0x0F)
404 || ((phy_mask & 0x03) == 0x03)
405 || ((phy_mask & 0x01) == 0x01)
406 || (phy_mask == 0))
407 return true;
Dan Williamsffe191c2011-06-29 13:09:25 -0700408 } else if (iport->physical_port_index == 1) {
Dan Williamse2f8db52011-05-10 02:28:46 -0700409 if (((phy_mask & 0x02) == 0x02)
410 || (phy_mask == 0))
411 return true;
Dan Williamsffe191c2011-06-29 13:09:25 -0700412 } else if (iport->physical_port_index == 2) {
Dan Williamse2f8db52011-05-10 02:28:46 -0700413 if (((phy_mask & 0x0C) == 0x0C)
414 || ((phy_mask & 0x04) == 0x04)
415 || (phy_mask == 0))
416 return true;
Dan Williamsffe191c2011-06-29 13:09:25 -0700417 } else if (iport->physical_port_index == 3) {
Dan Williamse2f8db52011-05-10 02:28:46 -0700418 if (((phy_mask & 0x08) == 0x08)
419 || (phy_mask == 0))
420 return true;
421 }
422
423 return false;
424}
425
Dan Williams85280952011-06-28 15:05:53 -0700426/*
Dan Williamse2f8db52011-05-10 02:28:46 -0700427 * This method retrieves a currently active (i.e. connected) phy contained in
428 * the port. Currently, the lowest order phy that is connected is returned.
429 * This method returns a pointer to a SCIS_SDS_PHY object. NULL This value is
430 * returned if there are no currently active (i.e. connected to a remote end
Dan Williams89a73012011-06-30 19:14:33 -0700431 * point) phys contained in the port. All other values specify a struct sci_phy
Dan Williamse2f8db52011-05-10 02:28:46 -0700432 * object that is active in the port.
433 */
Dan Williams89a73012011-06-30 19:14:33 -0700434static struct isci_phy *sci_port_get_a_connected_phy(struct isci_port *iport)
Dan Williams85280952011-06-28 15:05:53 -0700435{
Dan Williamse2f8db52011-05-10 02:28:46 -0700436 u32 index;
Dan Williams85280952011-06-28 15:05:53 -0700437 struct isci_phy *iphy;
Dan Williamse2f8db52011-05-10 02:28:46 -0700438
439 for (index = 0; index < SCI_MAX_PHYS; index++) {
Dan Williams85280952011-06-28 15:05:53 -0700440 /* Ensure that the phy is both part of the port and currently
441 * connected to the remote end-point.
442 */
Dan Williamsffe191c2011-06-29 13:09:25 -0700443 iphy = iport->phy_table[index];
Dan Williams89a73012011-06-30 19:14:33 -0700444 if (iphy && sci_port_active_phy(iport, iphy))
Dan Williams85280952011-06-28 15:05:53 -0700445 return iphy;
Dan Williamse2f8db52011-05-10 02:28:46 -0700446 }
447
448 return NULL;
449}
450
Dan Williams89a73012011-06-30 19:14:33 -0700451static enum sci_status sci_port_set_phy(struct isci_port *iport, struct isci_phy *iphy)
Dan Williamse2f8db52011-05-10 02:28:46 -0700452{
Dan Williams85280952011-06-28 15:05:53 -0700453 /* Check to see if we can add this phy to a port
Dan Williamse2f8db52011-05-10 02:28:46 -0700454 * that means that the phy is not part of a port and that the port does
Dan Williams85280952011-06-28 15:05:53 -0700455 * not already have a phy assinged to the phy index.
456 */
Dan Williamsffe191c2011-06-29 13:09:25 -0700457 if (!iport->phy_table[iphy->phy_index] &&
Dan Williams85280952011-06-28 15:05:53 -0700458 !phy_get_non_dummy_port(iphy) &&
Dan Williams89a73012011-06-30 19:14:33 -0700459 sci_port_is_valid_phy_assignment(iport, iphy->phy_index)) {
Dan Williams85280952011-06-28 15:05:53 -0700460 /* Phy is being added in the stopped state so we are in MPC mode
461 * make logical port index = physical port index
462 */
Dan Williamsffe191c2011-06-29 13:09:25 -0700463 iport->logical_port_index = iport->physical_port_index;
464 iport->phy_table[iphy->phy_index] = iphy;
Dan Williams89a73012011-06-30 19:14:33 -0700465 sci_phy_set_port(iphy, iport);
Dan Williamse2f8db52011-05-10 02:28:46 -0700466
467 return SCI_SUCCESS;
468 }
469
470 return SCI_FAILURE;
471}
472
Dan Williams89a73012011-06-30 19:14:33 -0700473static enum sci_status sci_port_clear_phy(struct isci_port *iport, struct isci_phy *iphy)
Dan Williamse2f8db52011-05-10 02:28:46 -0700474{
475 /* Make sure that this phy is part of this port */
Dan Williamsffe191c2011-06-29 13:09:25 -0700476 if (iport->phy_table[iphy->phy_index] == iphy &&
477 phy_get_non_dummy_port(iphy) == iport) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700478 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -0700479
480 /* Yep it is assigned to this port so remove it */
Dan Williams89a73012011-06-30 19:14:33 -0700481 sci_phy_set_port(iphy, &ihost->ports[SCI_MAX_PORTS]);
Dan Williamsffe191c2011-06-29 13:09:25 -0700482 iport->phy_table[iphy->phy_index] = NULL;
Dan Williamse2f8db52011-05-10 02:28:46 -0700483 return SCI_SUCCESS;
484 }
485
486 return SCI_FAILURE;
487}
488
Dan Williams89a73012011-06-30 19:14:33 -0700489void sci_port_get_sas_address(struct isci_port *iport, struct sci_sas_address *sas)
Dan Williamse2f8db52011-05-10 02:28:46 -0700490{
491 u32 index;
492
Dan Williams89a73012011-06-30 19:14:33 -0700493 sas->high = 0;
494 sas->low = 0;
495 for (index = 0; index < SCI_MAX_PHYS; index++)
496 if (iport->phy_table[index])
497 sci_phy_get_sas_address(iport->phy_table[index], sas);
Dan Williamse2f8db52011-05-10 02:28:46 -0700498}
499
Dan Williams89a73012011-06-30 19:14:33 -0700500void sci_port_get_attached_sas_address(struct isci_port *iport, struct sci_sas_address *sas)
Dan Williamse2f8db52011-05-10 02:28:46 -0700501{
Dan Williams85280952011-06-28 15:05:53 -0700502 struct isci_phy *iphy;
Dan Williamse2f8db52011-05-10 02:28:46 -0700503
504 /*
505 * Ensure that the phy is both part of the port and currently
506 * connected to the remote end-point.
507 */
Dan Williams89a73012011-06-30 19:14:33 -0700508 iphy = sci_port_get_a_connected_phy(iport);
Dan Williams85280952011-06-28 15:05:53 -0700509 if (iphy) {
510 if (iphy->protocol != SCIC_SDS_PHY_PROTOCOL_SATA) {
Dan Williams89a73012011-06-30 19:14:33 -0700511 sci_phy_get_attached_sas_address(iphy, sas);
Dan Williamse2f8db52011-05-10 02:28:46 -0700512 } else {
Dan Williams89a73012011-06-30 19:14:33 -0700513 sci_phy_get_sas_address(iphy, sas);
514 sas->low += iphy->phy_index;
Dan Williamse2f8db52011-05-10 02:28:46 -0700515 }
516 } else {
Dan Williams89a73012011-06-30 19:14:33 -0700517 sas->high = 0;
518 sas->low = 0;
Dan Williamse2f8db52011-05-10 02:28:46 -0700519 }
520}
521
522/**
Dan Williams89a73012011-06-30 19:14:33 -0700523 * sci_port_construct_dummy_rnc() - create dummy rnc for si workaround
Dan Williamse2f8db52011-05-10 02:28:46 -0700524 *
525 * @sci_port: logical port on which we need to create the remote node context
526 * @rni: remote node index for this remote node context.
527 *
528 * This routine will construct a dummy remote node context data structure
529 * This structure will be posted to the hardware to work around a scheduler
530 * error in the hardware.
531 */
Dan Williams89a73012011-06-30 19:14:33 -0700532static void sci_port_construct_dummy_rnc(struct isci_port *iport, u16 rni)
Dan Williamse2f8db52011-05-10 02:28:46 -0700533{
534 union scu_remote_node_context *rnc;
535
Dan Williamsffe191c2011-06-29 13:09:25 -0700536 rnc = &iport->owning_controller->remote_node_context_table[rni];
Dan Williamse2f8db52011-05-10 02:28:46 -0700537
538 memset(rnc, 0, sizeof(union scu_remote_node_context));
539
540 rnc->ssp.remote_sas_address_hi = 0;
541 rnc->ssp.remote_sas_address_lo = 0;
542
543 rnc->ssp.remote_node_index = rni;
544 rnc->ssp.remote_node_port_width = 1;
Dan Williamsffe191c2011-06-29 13:09:25 -0700545 rnc->ssp.logical_port_index = iport->physical_port_index;
Dan Williamse2f8db52011-05-10 02:28:46 -0700546
547 rnc->ssp.nexus_loss_timer_enable = false;
548 rnc->ssp.check_bit = false;
549 rnc->ssp.is_valid = true;
550 rnc->ssp.is_remote_node_context = true;
551 rnc->ssp.function_number = 0;
552 rnc->ssp.arbitration_wait_time = 0;
553}
554
Dan Williamsdd047c82011-06-09 11:06:58 -0700555/*
556 * construct a dummy task context data structure. This
Dan Williamse2f8db52011-05-10 02:28:46 -0700557 * structure will be posted to the hardwre to work around a scheduler error
558 * in the hardware.
Dan Williamse2f8db52011-05-10 02:28:46 -0700559 */
Dan Williams89a73012011-06-30 19:14:33 -0700560static void sci_port_construct_dummy_task(struct isci_port *iport, u16 tag)
Dan Williamse2f8db52011-05-10 02:28:46 -0700561{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700562 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -0700563 struct scu_task_context *task_context;
564
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700565 task_context = &ihost->task_context_table[ISCI_TAG_TCI(tag)];
Dan Williamse2f8db52011-05-10 02:28:46 -0700566 memset(task_context, 0, sizeof(struct scu_task_context));
567
Dan Williamse2f8db52011-05-10 02:28:46 -0700568 task_context->initiator_request = 1;
569 task_context->connection_rate = 1;
Dan Williamsffe191c2011-06-29 13:09:25 -0700570 task_context->logical_port_index = iport->physical_port_index;
Dan Williamse2f8db52011-05-10 02:28:46 -0700571 task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SSP;
Dan Williamsdd047c82011-06-09 11:06:58 -0700572 task_context->task_index = ISCI_TAG_TCI(tag);
Dan Williamse2f8db52011-05-10 02:28:46 -0700573 task_context->valid = SCU_TASK_CONTEXT_VALID;
574 task_context->context_type = SCU_TASK_CONTEXT_TYPE;
Dan Williamsffe191c2011-06-29 13:09:25 -0700575 task_context->remote_node_index = iport->reserved_rni;
Dan Williamse2f8db52011-05-10 02:28:46 -0700576 task_context->do_not_dma_ssp_good_response = 1;
Dan Williamse2f8db52011-05-10 02:28:46 -0700577 task_context->task_phase = 0x01;
578}
579
Dan Williams89a73012011-06-30 19:14:33 -0700580static void sci_port_destroy_dummy_resources(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -0700581{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700582 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -0700583
Dan Williamsffe191c2011-06-29 13:09:25 -0700584 if (iport->reserved_tag != SCI_CONTROLLER_INVALID_IO_TAG)
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700585 isci_free_tag(ihost, iport->reserved_tag);
Dan Williamse2f8db52011-05-10 02:28:46 -0700586
Dan Williamsffe191c2011-06-29 13:09:25 -0700587 if (iport->reserved_rni != SCU_DUMMY_INDEX)
Dan Williams89a73012011-06-30 19:14:33 -0700588 sci_remote_node_table_release_remote_node_index(&ihost->available_remote_nodes,
Dan Williamsffe191c2011-06-29 13:09:25 -0700589 1, iport->reserved_rni);
Dan Williamse2f8db52011-05-10 02:28:46 -0700590
Dan Williamsffe191c2011-06-29 13:09:25 -0700591 iport->reserved_rni = SCU_DUMMY_INDEX;
592 iport->reserved_tag = SCI_CONTROLLER_INVALID_IO_TAG;
Dan Williamse2f8db52011-05-10 02:28:46 -0700593}
594
Dan Williams89a73012011-06-30 19:14:33 -0700595void sci_port_setup_transports(struct isci_port *iport, u32 device_id)
Dan Williamse2f8db52011-05-10 02:28:46 -0700596{
597 u8 index;
598
599 for (index = 0; index < SCI_MAX_PHYS; index++) {
Dan Williamsffe191c2011-06-29 13:09:25 -0700600 if (iport->active_phy_mask & (1 << index))
Dan Williams89a73012011-06-30 19:14:33 -0700601 sci_phy_setup_transport(iport->phy_table[index], device_id);
Dan Williamse2f8db52011-05-10 02:28:46 -0700602 }
603}
604
Marcin Tomczak05b080f2012-01-04 01:33:41 -0800605static void sci_port_resume_phy(struct isci_port *iport, struct isci_phy *iphy)
606{
607 sci_phy_resume(iphy);
608 iport->enabled_phy_mask |= 1 << iphy->phy_index;
609}
610
611static void sci_port_activate_phy(struct isci_port *iport,
612 struct isci_phy *iphy,
613 u8 flags)
Dan Williamse2f8db52011-05-10 02:28:46 -0700614{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700615 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -0700616
Marcin Tomczak05b080f2012-01-04 01:33:41 -0800617 if (iphy->protocol != SCIC_SDS_PHY_PROTOCOL_SATA && (flags & PF_RESUME))
Dan Williams89a73012011-06-30 19:14:33 -0700618 sci_phy_resume(iphy);
Dan Williamse2f8db52011-05-10 02:28:46 -0700619
Dan Williamsffe191c2011-06-29 13:09:25 -0700620 iport->active_phy_mask |= 1 << iphy->phy_index;
Dan Williamse2f8db52011-05-10 02:28:46 -0700621
Dan Williams89a73012011-06-30 19:14:33 -0700622 sci_controller_clear_invalid_phy(ihost, iphy);
Dan Williamse2f8db52011-05-10 02:28:46 -0700623
Marcin Tomczak05b080f2012-01-04 01:33:41 -0800624 if (flags & PF_NOTIFY)
Dan Williamsffe191c2011-06-29 13:09:25 -0700625 isci_port_link_up(ihost, iport, iphy);
Dan Williamse2f8db52011-05-10 02:28:46 -0700626}
627
Dan Williams89a73012011-06-30 19:14:33 -0700628void sci_port_deactivate_phy(struct isci_port *iport, struct isci_phy *iphy,
629 bool do_notify_user)
Dan Williamse2f8db52011-05-10 02:28:46 -0700630{
Dan Williams34a99152011-07-01 02:25:15 -0700631 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -0700632
Dan Williamsffe191c2011-06-29 13:09:25 -0700633 iport->active_phy_mask &= ~(1 << iphy->phy_index);
Marcin Tomczak05b080f2012-01-04 01:33:41 -0800634 iport->enabled_phy_mask &= ~(1 << iphy->phy_index);
Jeff Skirvin8e35a132011-10-27 15:05:32 -0700635 if (!iport->active_phy_mask)
636 iport->last_active_phy = iphy->phy_index;
Dan Williamse2f8db52011-05-10 02:28:46 -0700637
Dan Williams85280952011-06-28 15:05:53 -0700638 iphy->max_negotiated_speed = SAS_LINK_RATE_UNKNOWN;
Dan Williamse2f8db52011-05-10 02:28:46 -0700639
Marcin Tomczakd4ec1cf2012-01-04 01:33:15 -0800640 /* Re-assign the phy back to the LP as if it were a narrow port for APC
641 * mode. For MPC mode, the phy will remain in the port.
642 */
643 if (iport->owning_controller->oem_parameters.controller.mode_type ==
644 SCIC_PORT_AUTOMATIC_CONFIGURATION_MODE)
645 writel(iphy->phy_index,
646 &iport->port_pe_configuration_register[iphy->phy_index]);
Dan Williamse2f8db52011-05-10 02:28:46 -0700647
648 if (do_notify_user == true)
649 isci_port_link_down(ihost, iphy, iport);
650}
651
Dan Williams89a73012011-06-30 19:14:33 -0700652static void sci_port_invalid_link_up(struct isci_port *iport, struct isci_phy *iphy)
Dan Williamse2f8db52011-05-10 02:28:46 -0700653{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700654 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -0700655
656 /*
657 * Check to see if we have alreay reported this link as bad and if
658 * not go ahead and tell the SCI_USER that we have discovered an
659 * invalid link.
660 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700661 if ((ihost->invalid_phy_mask & (1 << iphy->phy_index)) == 0) {
Dan Williams34a99152011-07-01 02:25:15 -0700662 ihost->invalid_phy_mask |= 1 << iphy->phy_index;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700663 dev_warn(&ihost->pdev->dev, "Invalid link up!\n");
Dan Williamse2f8db52011-05-10 02:28:46 -0700664 }
665}
666
667/**
Dan Williams89a73012011-06-30 19:14:33 -0700668 * sci_port_general_link_up_handler - phy can be assigned to port?
669 * @sci_port: sci_port object for which has a phy that has gone link up.
Dan Williams85280952011-06-28 15:05:53 -0700670 * @sci_phy: This is the struct isci_phy object that has gone link up.
Marcin Tomczak05b080f2012-01-04 01:33:41 -0800671 * @flags: PF_RESUME, PF_NOTIFY to sci_port_activate_phy
Dan Williamse2f8db52011-05-10 02:28:46 -0700672 *
Marcin Tomczak05b080f2012-01-04 01:33:41 -0800673 * Determine if this phy can be assigned to this port . If the phy is
674 * not a valid PHY for this port then the function will notify the user.
675 * A PHY can only be part of a port if it's attached SAS ADDRESS is the
676 * same as all other PHYs in the same port.
Dan Williamse2f8db52011-05-10 02:28:46 -0700677 */
Dan Williams89a73012011-06-30 19:14:33 -0700678static void sci_port_general_link_up_handler(struct isci_port *iport,
Marcin Tomczak05b080f2012-01-04 01:33:41 -0800679 struct isci_phy *iphy,
680 u8 flags)
Dan Williamse2f8db52011-05-10 02:28:46 -0700681{
682 struct sci_sas_address port_sas_address;
683 struct sci_sas_address phy_sas_address;
684
Dan Williams89a73012011-06-30 19:14:33 -0700685 sci_port_get_attached_sas_address(iport, &port_sas_address);
686 sci_phy_get_attached_sas_address(iphy, &phy_sas_address);
Dan Williamse2f8db52011-05-10 02:28:46 -0700687
688 /* If the SAS address of the new phy matches the SAS address of
689 * other phys in the port OR this is the first phy in the port,
690 * then activate the phy and allow it to be used for operations
691 * in this port.
692 */
693 if ((phy_sas_address.high == port_sas_address.high &&
694 phy_sas_address.low == port_sas_address.low) ||
Dan Williamsffe191c2011-06-29 13:09:25 -0700695 iport->active_phy_mask == 0) {
696 struct sci_base_state_machine *sm = &iport->sm;
Dan Williamse2f8db52011-05-10 02:28:46 -0700697
Marcin Tomczak05b080f2012-01-04 01:33:41 -0800698 sci_port_activate_phy(iport, iphy, flags);
Edmund Nadolskie3013702011-06-02 00:10:43 +0000699 if (sm->current_state_id == SCI_PORT_RESETTING)
Dan Williamsffe191c2011-06-29 13:09:25 -0700700 port_state_machine_change(iport, SCI_PORT_READY);
Dan Williamse2f8db52011-05-10 02:28:46 -0700701 } else
Dan Williams89a73012011-06-30 19:14:33 -0700702 sci_port_invalid_link_up(iport, iphy);
Dan Williamse2f8db52011-05-10 02:28:46 -0700703}
704
705
706
707/**
708 * This method returns false if the port only has a single phy object assigned.
709 * If there are no phys or more than one phy then the method will return
710 * true.
711 * @sci_port: The port for which the wide port condition is to be checked.
712 *
713 * bool true Is returned if this is a wide ported port. false Is returned if
714 * this is a narrow port.
715 */
Dan Williams89a73012011-06-30 19:14:33 -0700716static bool sci_port_is_wide(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -0700717{
718 u32 index;
719 u32 phy_count = 0;
720
721 for (index = 0; index < SCI_MAX_PHYS; index++) {
Dan Williamsffe191c2011-06-29 13:09:25 -0700722 if (iport->phy_table[index] != NULL) {
Dan Williamse2f8db52011-05-10 02:28:46 -0700723 phy_count++;
724 }
725 }
726
727 return phy_count != 1;
728}
729
730/**
731 * This method is called by the PHY object when the link is detected. if the
732 * port wants the PHY to continue on to the link up state then the port
733 * layer must return true. If the port object returns false the phy object
734 * must halt its attempt to go link up.
735 * @sci_port: The port associated with the phy object.
736 * @sci_phy: The phy object that is trying to go link up.
737 *
738 * true if the phy object can continue to the link up condition. true Is
739 * returned if this phy can continue to the ready state. false Is returned if
740 * can not continue on to the ready state. This notification is in place for
741 * wide ports and direct attached phys. Since there are no wide ported SATA
742 * devices this could become an invalid port configuration.
743 */
Dan Williams89a73012011-06-30 19:14:33 -0700744bool sci_port_link_detected(
Dan Williamsffe191c2011-06-29 13:09:25 -0700745 struct isci_port *iport,
Dan Williams85280952011-06-28 15:05:53 -0700746 struct isci_phy *iphy)
Dan Williamse2f8db52011-05-10 02:28:46 -0700747{
Dan Williamsffe191c2011-06-29 13:09:25 -0700748 if ((iport->logical_port_index != SCIC_SDS_DUMMY_PORT) &&
Marcin Tomczak05b080f2012-01-04 01:33:41 -0800749 (iphy->protocol == SCIC_SDS_PHY_PROTOCOL_SATA)) {
750 if (sci_port_is_wide(iport)) {
751 sci_port_invalid_link_up(iport, iphy);
752 return false;
753 } else {
754 struct isci_host *ihost = iport->owning_controller;
755 struct isci_port *dst_port = &(ihost->ports[iphy->phy_index]);
756 writel(iphy->phy_index,
757 &dst_port->port_pe_configuration_register[iphy->phy_index]);
758 }
Dan Williamse2f8db52011-05-10 02:28:46 -0700759 }
760
761 return true;
762}
763
Edmund Nadolski5553ba22011-05-19 11:59:10 +0000764static void port_timeout(unsigned long data)
Dan Williamse2f8db52011-05-10 02:28:46 -0700765{
Edmund Nadolski5553ba22011-05-19 11:59:10 +0000766 struct sci_timer *tmr = (struct sci_timer *)data;
Dan Williamsffe191c2011-06-29 13:09:25 -0700767 struct isci_port *iport = container_of(tmr, typeof(*iport), timer);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700768 struct isci_host *ihost = iport->owning_controller;
Edmund Nadolski5553ba22011-05-19 11:59:10 +0000769 unsigned long flags;
Dan Williamse2f8db52011-05-10 02:28:46 -0700770 u32 current_state;
771
Edmund Nadolski5553ba22011-05-19 11:59:10 +0000772 spin_lock_irqsave(&ihost->scic_lock, flags);
773
774 if (tmr->cancel)
775 goto done;
776
Dan Williamsffe191c2011-06-29 13:09:25 -0700777 current_state = iport->sm.current_state_id;
Dan Williamse2f8db52011-05-10 02:28:46 -0700778
Edmund Nadolskie3013702011-06-02 00:10:43 +0000779 if (current_state == SCI_PORT_RESETTING) {
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000780 /* if the port is still in the resetting state then the timeout
781 * fired before the reset completed.
Dan Williamse2f8db52011-05-10 02:28:46 -0700782 */
Dan Williamsffe191c2011-06-29 13:09:25 -0700783 port_state_machine_change(iport, SCI_PORT_FAILED);
Edmund Nadolskie3013702011-06-02 00:10:43 +0000784 } else if (current_state == SCI_PORT_STOPPED) {
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000785 /* if the port is stopped then the start request failed In this
786 * case stay in the stopped state.
Dan Williamse2f8db52011-05-10 02:28:46 -0700787 */
Dan Williamsffe191c2011-06-29 13:09:25 -0700788 dev_err(sciport_to_dev(iport),
Dan Williamse2f8db52011-05-10 02:28:46 -0700789 "%s: SCIC Port 0x%p failed to stop before tiemout.\n",
790 __func__,
Dan Williamsffe191c2011-06-29 13:09:25 -0700791 iport);
Edmund Nadolskie3013702011-06-02 00:10:43 +0000792 } else if (current_state == SCI_PORT_STOPPING) {
Dan Williamsfca4ecb2012-01-03 23:26:15 -0800793 dev_dbg(sciport_to_dev(iport),
794 "%s: port%d: stop complete timeout\n",
795 __func__, iport->physical_port_index);
Dan Williamse2f8db52011-05-10 02:28:46 -0700796 } else {
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000797 /* The port is in the ready state and we have a timer
Dan Williamse2f8db52011-05-10 02:28:46 -0700798 * reporting a timeout this should not happen.
799 */
Dan Williamsffe191c2011-06-29 13:09:25 -0700800 dev_err(sciport_to_dev(iport),
Dan Williamse2f8db52011-05-10 02:28:46 -0700801 "%s: SCIC Port 0x%p is processing a timeout operation "
Dan Williamsffe191c2011-06-29 13:09:25 -0700802 "in state %d.\n", __func__, iport, current_state);
Dan Williamse2f8db52011-05-10 02:28:46 -0700803 }
Edmund Nadolski5553ba22011-05-19 11:59:10 +0000804
805done:
806 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williamse2f8db52011-05-10 02:28:46 -0700807}
808
809/* --------------------------------------------------------------------------- */
810
811/**
812 * This function updates the hardwares VIIT entry for this port.
813 *
814 *
815 */
Dan Williams89a73012011-06-30 19:14:33 -0700816static void sci_port_update_viit_entry(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -0700817{
818 struct sci_sas_address sas_address;
819
Dan Williams89a73012011-06-30 19:14:33 -0700820 sci_port_get_sas_address(iport, &sas_address);
Dan Williamse2f8db52011-05-10 02:28:46 -0700821
822 writel(sas_address.high,
Dan Williamsffe191c2011-06-29 13:09:25 -0700823 &iport->viit_registers->initiator_sas_address_hi);
Dan Williamse2f8db52011-05-10 02:28:46 -0700824 writel(sas_address.low,
Dan Williamsffe191c2011-06-29 13:09:25 -0700825 &iport->viit_registers->initiator_sas_address_lo);
Dan Williamse2f8db52011-05-10 02:28:46 -0700826
827 /* This value get cleared just in case its not already cleared */
Dan Williamsffe191c2011-06-29 13:09:25 -0700828 writel(0, &iport->viit_registers->reserved);
Dan Williamse2f8db52011-05-10 02:28:46 -0700829
830 /* We are required to update the status register last */
831 writel(SCU_VIIT_ENTRY_ID_VIIT |
832 SCU_VIIT_IPPT_INITIATOR |
Dan Williamsffe191c2011-06-29 13:09:25 -0700833 ((1 << iport->physical_port_index) << SCU_VIIT_ENTRY_LPVIE_SHIFT) |
Dan Williamse2f8db52011-05-10 02:28:46 -0700834 SCU_VIIT_STATUS_ALL_VALID,
Dan Williamsffe191c2011-06-29 13:09:25 -0700835 &iport->viit_registers->status);
Dan Williamse2f8db52011-05-10 02:28:46 -0700836}
837
Dan Williams89a73012011-06-30 19:14:33 -0700838enum sas_linkrate sci_port_get_max_allowed_speed(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -0700839{
840 u16 index;
Dan Williams85280952011-06-28 15:05:53 -0700841 struct isci_phy *iphy;
Dan Williamse2f8db52011-05-10 02:28:46 -0700842 enum sas_linkrate max_allowed_speed = SAS_LINK_RATE_6_0_GBPS;
Dan Williamse2f8db52011-05-10 02:28:46 -0700843
844 /*
845 * Loop through all of the phys in this port and find the phy with the
846 * lowest maximum link rate. */
847 for (index = 0; index < SCI_MAX_PHYS; index++) {
Dan Williamsffe191c2011-06-29 13:09:25 -0700848 iphy = iport->phy_table[index];
Dan Williams89a73012011-06-30 19:14:33 -0700849 if (iphy && sci_port_active_phy(iport, iphy) &&
Dan Williams85280952011-06-28 15:05:53 -0700850 iphy->max_negotiated_speed < max_allowed_speed)
851 max_allowed_speed = iphy->max_negotiated_speed;
Dan Williamse2f8db52011-05-10 02:28:46 -0700852 }
853
854 return max_allowed_speed;
855}
856
Dan Williams89a73012011-06-30 19:14:33 -0700857static void sci_port_suspend_port_task_scheduler(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -0700858{
859 u32 pts_control_value;
860
Dan Williamsffe191c2011-06-29 13:09:25 -0700861 pts_control_value = readl(&iport->port_task_scheduler_registers->control);
Dan Williamse2f8db52011-05-10 02:28:46 -0700862 pts_control_value |= SCU_PTSxCR_GEN_BIT(SUSPEND);
Dan Williamsffe191c2011-06-29 13:09:25 -0700863 writel(pts_control_value, &iport->port_task_scheduler_registers->control);
Dan Williamse2f8db52011-05-10 02:28:46 -0700864}
865
866/**
Dan Williams89a73012011-06-30 19:14:33 -0700867 * sci_port_post_dummy_request() - post dummy/workaround request
Dan Williamse2f8db52011-05-10 02:28:46 -0700868 * @sci_port: port to post task
869 *
870 * Prevent the hardware scheduler from posting new requests to the front
871 * of the scheduler queue causing a starvation problem for currently
872 * ongoing requests.
873 *
874 */
Dan Williams89a73012011-06-30 19:14:33 -0700875static void sci_port_post_dummy_request(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -0700876{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700877 struct isci_host *ihost = iport->owning_controller;
Dan Williamsffe191c2011-06-29 13:09:25 -0700878 u16 tag = iport->reserved_tag;
Dan Williams312e0c22011-06-28 13:47:09 -0700879 struct scu_task_context *tc;
880 u32 command;
Dan Williamse2f8db52011-05-10 02:28:46 -0700881
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700882 tc = &ihost->task_context_table[ISCI_TAG_TCI(tag)];
Dan Williams312e0c22011-06-28 13:47:09 -0700883 tc->abort = 0;
Dan Williamse2f8db52011-05-10 02:28:46 -0700884
885 command = SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
Dan Williamsffe191c2011-06-29 13:09:25 -0700886 iport->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT |
Dan Williams312e0c22011-06-28 13:47:09 -0700887 ISCI_TAG_TCI(tag);
Dan Williamse2f8db52011-05-10 02:28:46 -0700888
Dan Williams89a73012011-06-30 19:14:33 -0700889 sci_controller_post_request(ihost, command);
Dan Williamse2f8db52011-05-10 02:28:46 -0700890}
891
892/**
893 * This routine will abort the dummy request. This will alow the hardware to
894 * power down parts of the silicon to save power.
895 *
896 * @sci_port: The port on which the task must be aborted.
897 *
898 */
Dan Williams89a73012011-06-30 19:14:33 -0700899static void sci_port_abort_dummy_request(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -0700900{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700901 struct isci_host *ihost = iport->owning_controller;
Dan Williamsffe191c2011-06-29 13:09:25 -0700902 u16 tag = iport->reserved_tag;
Dan Williamse2f8db52011-05-10 02:28:46 -0700903 struct scu_task_context *tc;
904 u32 command;
905
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700906 tc = &ihost->task_context_table[ISCI_TAG_TCI(tag)];
Dan Williamse2f8db52011-05-10 02:28:46 -0700907 tc->abort = 1;
908
909 command = SCU_CONTEXT_COMMAND_REQUEST_POST_TC_ABORT |
Dan Williamsffe191c2011-06-29 13:09:25 -0700910 iport->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT |
Dan Williams312e0c22011-06-28 13:47:09 -0700911 ISCI_TAG_TCI(tag);
Dan Williamse2f8db52011-05-10 02:28:46 -0700912
Dan Williams89a73012011-06-30 19:14:33 -0700913 sci_controller_post_request(ihost, command);
Dan Williamse2f8db52011-05-10 02:28:46 -0700914}
915
916/**
917 *
Dan Williamsffe191c2011-06-29 13:09:25 -0700918 * @sci_port: This is the struct isci_port object to resume.
Dan Williamse2f8db52011-05-10 02:28:46 -0700919 *
920 * This method will resume the port task scheduler for this port object. none
921 */
922static void
Dan Williams89a73012011-06-30 19:14:33 -0700923sci_port_resume_port_task_scheduler(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -0700924{
925 u32 pts_control_value;
926
Dan Williamsffe191c2011-06-29 13:09:25 -0700927 pts_control_value = readl(&iport->port_task_scheduler_registers->control);
Dan Williamse2f8db52011-05-10 02:28:46 -0700928 pts_control_value &= ~SCU_PTSxCR_GEN_BIT(SUSPEND);
Dan Williamsffe191c2011-06-29 13:09:25 -0700929 writel(pts_control_value, &iport->port_task_scheduler_registers->control);
Dan Williamse2f8db52011-05-10 02:28:46 -0700930}
931
Dan Williams89a73012011-06-30 19:14:33 -0700932static void sci_port_ready_substate_waiting_enter(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -0700933{
Dan Williamsffe191c2011-06-29 13:09:25 -0700934 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamse2f8db52011-05-10 02:28:46 -0700935
Dan Williams89a73012011-06-30 19:14:33 -0700936 sci_port_suspend_port_task_scheduler(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -0700937
Dan Williamsffe191c2011-06-29 13:09:25 -0700938 iport->not_ready_reason = SCIC_PORT_NOT_READY_NO_ACTIVE_PHYS;
Dan Williamse2f8db52011-05-10 02:28:46 -0700939
Dan Williamsffe191c2011-06-29 13:09:25 -0700940 if (iport->active_phy_mask != 0) {
Dan Williamse2f8db52011-05-10 02:28:46 -0700941 /* At least one of the phys on the port is ready */
Dan Williamsffe191c2011-06-29 13:09:25 -0700942 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +0000943 SCI_PORT_SUB_OPERATIONAL);
Dan Williamse2f8db52011-05-10 02:28:46 -0700944 }
945}
946
Marcin Tomczak05b080f2012-01-04 01:33:41 -0800947static void scic_sds_port_ready_substate_waiting_exit(
948 struct sci_base_state_machine *sm)
949{
950 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
951 sci_port_resume_port_task_scheduler(iport);
952}
953
Dan Williams89a73012011-06-30 19:14:33 -0700954static void sci_port_ready_substate_operational_enter(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -0700955{
956 u32 index;
Dan Williamsffe191c2011-06-29 13:09:25 -0700957 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700958 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -0700959
Dan Williamsfca4ecb2012-01-03 23:26:15 -0800960 dev_dbg(&ihost->pdev->dev, "%s: port%d ready\n",
961 __func__, iport->physical_port_index);
Dan Williamse2f8db52011-05-10 02:28:46 -0700962
963 for (index = 0; index < SCI_MAX_PHYS; index++) {
Dan Williamsffe191c2011-06-29 13:09:25 -0700964 if (iport->phy_table[index]) {
965 writel(iport->physical_port_index,
966 &iport->port_pe_configuration_register[
967 iport->phy_table[index]->phy_index]);
Marcin Tomczak05b080f2012-01-04 01:33:41 -0800968 if (((iport->active_phy_mask^iport->enabled_phy_mask) & (1 << index)) != 0)
969 sci_port_resume_phy(iport, iport->phy_table[index]);
Dan Williamse2f8db52011-05-10 02:28:46 -0700970 }
971 }
972
Dan Williams89a73012011-06-30 19:14:33 -0700973 sci_port_update_viit_entry(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -0700974
Dan Williamse2f8db52011-05-10 02:28:46 -0700975 /*
976 * Post the dummy task for the port so the hardware can schedule
977 * io correctly
978 */
Dan Williams89a73012011-06-30 19:14:33 -0700979 sci_port_post_dummy_request(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -0700980}
981
Dan Williams89a73012011-06-30 19:14:33 -0700982static void sci_port_invalidate_dummy_remote_node(struct isci_port *iport)
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000983{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700984 struct isci_host *ihost = iport->owning_controller;
Dan Williamsffe191c2011-06-29 13:09:25 -0700985 u8 phys_index = iport->physical_port_index;
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000986 union scu_remote_node_context *rnc;
Dan Williamsffe191c2011-06-29 13:09:25 -0700987 u16 rni = iport->reserved_rni;
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000988 u32 command;
989
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700990 rnc = &ihost->remote_node_context_table[rni];
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000991
992 rnc->ssp.is_valid = false;
993
994 /* ensure the preceding tc abort request has reached the
995 * controller and give it ample time to act before posting the rnc
996 * invalidate
997 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700998 readl(&ihost->smu_registers->interrupt_status); /* flush */
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000999 udelay(10);
1000
1001 command = SCU_CONTEXT_COMMAND_POST_RNC_INVALIDATE |
1002 phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
1003
Dan Williams89a73012011-06-30 19:14:33 -07001004 sci_controller_post_request(ihost, command);
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001005}
1006
Dan Williamse2f8db52011-05-10 02:28:46 -07001007/**
1008 *
Dan Williamsffe191c2011-06-29 13:09:25 -07001009 * @object: This is the object which is cast to a struct isci_port object.
Dan Williamse2f8db52011-05-10 02:28:46 -07001010 *
Dan Williamsffe191c2011-06-29 13:09:25 -07001011 * This method will perform the actions required by the struct isci_port on
Edmund Nadolskie3013702011-06-02 00:10:43 +00001012 * exiting the SCI_PORT_SUB_OPERATIONAL. This function reports
Dan Williamse2f8db52011-05-10 02:28:46 -07001013 * the port not ready and suspends the port task scheduler. none
1014 */
Dan Williams89a73012011-06-30 19:14:33 -07001015static void sci_port_ready_substate_operational_exit(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001016{
Dan Williamsffe191c2011-06-29 13:09:25 -07001017 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001018 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -07001019
1020 /*
1021 * Kill the dummy task for this port if it has not yet posted
1022 * the hardware will treat this as a NOP and just return abort
1023 * complete.
1024 */
Dan Williams89a73012011-06-30 19:14:33 -07001025 sci_port_abort_dummy_request(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -07001026
Dan Williamsfca4ecb2012-01-03 23:26:15 -08001027 dev_dbg(&ihost->pdev->dev, "%s: port%d !ready\n",
1028 __func__, iport->physical_port_index);
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001029
Dan Williamsffe191c2011-06-29 13:09:25 -07001030 if (iport->ready_exit)
Dan Williams89a73012011-06-30 19:14:33 -07001031 sci_port_invalidate_dummy_remote_node(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -07001032}
1033
Dan Williams89a73012011-06-30 19:14:33 -07001034static void sci_port_ready_substate_configuring_enter(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001035{
Dan Williamsffe191c2011-06-29 13:09:25 -07001036 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001037 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -07001038
Dan Williamsffe191c2011-06-29 13:09:25 -07001039 if (iport->active_phy_mask == 0) {
Dan Williamsfca4ecb2012-01-03 23:26:15 -08001040 dev_dbg(&ihost->pdev->dev, "%s: port%d !ready\n",
1041 __func__, iport->physical_port_index);
Dan Williamse2f8db52011-05-10 02:28:46 -07001042
Marcin Tomczak05b080f2012-01-04 01:33:41 -08001043 port_state_machine_change(iport, SCI_PORT_SUB_WAITING);
1044 } else
1045 port_state_machine_change(iport, SCI_PORT_SUB_OPERATIONAL);
Dan Williamse2f8db52011-05-10 02:28:46 -07001046}
1047
Dan Williams89a73012011-06-30 19:14:33 -07001048enum sci_status sci_port_start(struct isci_port *iport)
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001049{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001050 struct isci_host *ihost = iport->owning_controller;
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001051 enum sci_status status = SCI_SUCCESS;
Dan Williams89a73012011-06-30 19:14:33 -07001052 enum sci_port_states state;
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001053 u32 phy_mask;
1054
Dan Williamsffe191c2011-06-29 13:09:25 -07001055 state = iport->sm.current_state_id;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001056 if (state != SCI_PORT_STOPPED) {
Dan Williamsffe191c2011-06-29 13:09:25 -07001057 dev_warn(sciport_to_dev(iport),
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001058 "%s: in wrong state: %d\n", __func__, state);
1059 return SCI_FAILURE_INVALID_STATE;
1060 }
1061
Dan Williamsffe191c2011-06-29 13:09:25 -07001062 if (iport->assigned_device_count > 0) {
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001063 /* TODO This is a start failure operation because
1064 * there are still devices assigned to this port.
1065 * There must be no devices assigned to a port on a
1066 * start operation.
1067 */
1068 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
1069 }
1070
Dan Williamsffe191c2011-06-29 13:09:25 -07001071 if (iport->reserved_rni == SCU_DUMMY_INDEX) {
Dan Williams89a73012011-06-30 19:14:33 -07001072 u16 rni = sci_remote_node_table_allocate_remote_node(
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001073 &ihost->available_remote_nodes, 1);
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001074
1075 if (rni != SCU_DUMMY_INDEX)
Dan Williams89a73012011-06-30 19:14:33 -07001076 sci_port_construct_dummy_rnc(iport, rni);
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001077 else
1078 status = SCI_FAILURE_INSUFFICIENT_RESOURCES;
Dan Williamsffe191c2011-06-29 13:09:25 -07001079 iport->reserved_rni = rni;
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001080 }
1081
Dan Williamsffe191c2011-06-29 13:09:25 -07001082 if (iport->reserved_tag == SCI_CONTROLLER_INVALID_IO_TAG) {
Dan Williams312e0c22011-06-28 13:47:09 -07001083 u16 tag;
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001084
Dan Williams312e0c22011-06-28 13:47:09 -07001085 tag = isci_alloc_tag(ihost);
1086 if (tag == SCI_CONTROLLER_INVALID_IO_TAG)
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001087 status = SCI_FAILURE_INSUFFICIENT_RESOURCES;
Dan Williams312e0c22011-06-28 13:47:09 -07001088 else
Dan Williams89a73012011-06-30 19:14:33 -07001089 sci_port_construct_dummy_task(iport, tag);
Dan Williamsffe191c2011-06-29 13:09:25 -07001090 iport->reserved_tag = tag;
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001091 }
1092
1093 if (status == SCI_SUCCESS) {
Dan Williams89a73012011-06-30 19:14:33 -07001094 phy_mask = sci_port_get_phys(iport);
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001095
1096 /*
1097 * There are one or more phys assigned to this port. Make sure
1098 * the port's phy mask is in fact legal and supported by the
1099 * silicon.
1100 */
Dan Williams89a73012011-06-30 19:14:33 -07001101 if (sci_port_is_phy_mask_valid(iport, phy_mask) == true) {
Dan Williamsffe191c2011-06-29 13:09:25 -07001102 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001103 SCI_PORT_READY);
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001104
1105 return SCI_SUCCESS;
1106 }
1107 status = SCI_FAILURE;
1108 }
1109
1110 if (status != SCI_SUCCESS)
Dan Williams89a73012011-06-30 19:14:33 -07001111 sci_port_destroy_dummy_resources(iport);
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001112
1113 return status;
1114}
1115
Dan Williams89a73012011-06-30 19:14:33 -07001116enum sci_status sci_port_stop(struct isci_port *iport)
Piotr Sawicki8bc80d32011-05-11 23:52:31 +00001117{
Dan Williams89a73012011-06-30 19:14:33 -07001118 enum sci_port_states state;
Piotr Sawicki8bc80d32011-05-11 23:52:31 +00001119
Dan Williamsffe191c2011-06-29 13:09:25 -07001120 state = iport->sm.current_state_id;
Piotr Sawicki8bc80d32011-05-11 23:52:31 +00001121 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001122 case SCI_PORT_STOPPED:
Piotr Sawicki8bc80d32011-05-11 23:52:31 +00001123 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001124 case SCI_PORT_SUB_WAITING:
1125 case SCI_PORT_SUB_OPERATIONAL:
1126 case SCI_PORT_SUB_CONFIGURING:
1127 case SCI_PORT_RESETTING:
Dan Williamsffe191c2011-06-29 13:09:25 -07001128 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001129 SCI_PORT_STOPPING);
Piotr Sawicki8bc80d32011-05-11 23:52:31 +00001130 return SCI_SUCCESS;
1131 default:
Dan Williamsffe191c2011-06-29 13:09:25 -07001132 dev_warn(sciport_to_dev(iport),
Piotr Sawicki8bc80d32011-05-11 23:52:31 +00001133 "%s: in wrong state: %d\n", __func__, state);
1134 return SCI_FAILURE_INVALID_STATE;
1135 }
1136}
1137
Dan Williams89a73012011-06-30 19:14:33 -07001138static enum sci_status sci_port_hard_reset(struct isci_port *iport, u32 timeout)
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001139{
1140 enum sci_status status = SCI_FAILURE_INVALID_PHY;
Dan Williams85280952011-06-28 15:05:53 -07001141 struct isci_phy *iphy = NULL;
Dan Williams89a73012011-06-30 19:14:33 -07001142 enum sci_port_states state;
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001143 u32 phy_index;
1144
Dan Williamsffe191c2011-06-29 13:09:25 -07001145 state = iport->sm.current_state_id;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001146 if (state != SCI_PORT_SUB_OPERATIONAL) {
Dan Williamsffe191c2011-06-29 13:09:25 -07001147 dev_warn(sciport_to_dev(iport),
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001148 "%s: in wrong state: %d\n", __func__, state);
1149 return SCI_FAILURE_INVALID_STATE;
1150 }
1151
1152 /* Select a phy on which we can send the hard reset request. */
Dan Williams85280952011-06-28 15:05:53 -07001153 for (phy_index = 0; phy_index < SCI_MAX_PHYS && !iphy; phy_index++) {
Dan Williamsffe191c2011-06-29 13:09:25 -07001154 iphy = iport->phy_table[phy_index];
Dan Williams89a73012011-06-30 19:14:33 -07001155 if (iphy && !sci_port_active_phy(iport, iphy)) {
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001156 /*
1157 * We found a phy but it is not ready select
1158 * different phy
1159 */
Dan Williams85280952011-06-28 15:05:53 -07001160 iphy = NULL;
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001161 }
1162 }
1163
1164 /* If we have a phy then go ahead and start the reset procedure */
Dan Williams85280952011-06-28 15:05:53 -07001165 if (!iphy)
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001166 return status;
Dan Williams89a73012011-06-30 19:14:33 -07001167 status = sci_phy_reset(iphy);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001168
1169 if (status != SCI_SUCCESS)
1170 return status;
1171
Dan Williamsffe191c2011-06-29 13:09:25 -07001172 sci_mod_timer(&iport->timer, timeout);
1173 iport->not_ready_reason = SCIC_PORT_NOT_READY_HARD_RESET_REQUESTED;
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001174
Dan Williamsffe191c2011-06-29 13:09:25 -07001175 port_state_machine_change(iport, SCI_PORT_RESETTING);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001176 return SCI_SUCCESS;
1177}
1178
1179/**
Dan Williams89a73012011-06-30 19:14:33 -07001180 * sci_port_add_phy() -
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001181 * @sci_port: This parameter specifies the port in which the phy will be added.
1182 * @sci_phy: This parameter is the phy which is to be added to the port.
1183 *
1184 * This method will add a PHY to the selected port. This method returns an
1185 * enum sci_status. SCI_SUCCESS the phy has been added to the port. Any other
1186 * status is a failure to add the phy to the port.
1187 */
Dan Williams89a73012011-06-30 19:14:33 -07001188enum sci_status sci_port_add_phy(struct isci_port *iport,
Dan Williams85280952011-06-28 15:05:53 -07001189 struct isci_phy *iphy)
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001190{
1191 enum sci_status status;
Dan Williams89a73012011-06-30 19:14:33 -07001192 enum sci_port_states state;
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001193
Dan Williamsffe191c2011-06-29 13:09:25 -07001194 state = iport->sm.current_state_id;
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001195 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001196 case SCI_PORT_STOPPED: {
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001197 struct sci_sas_address port_sas_address;
1198
1199 /* Read the port assigned SAS Address if there is one */
Dan Williams89a73012011-06-30 19:14:33 -07001200 sci_port_get_sas_address(iport, &port_sas_address);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001201
1202 if (port_sas_address.high != 0 && port_sas_address.low != 0) {
1203 struct sci_sas_address phy_sas_address;
1204
1205 /* Make sure that the PHY SAS Address matches the SAS Address
1206 * for this port
1207 */
Dan Williams89a73012011-06-30 19:14:33 -07001208 sci_phy_get_sas_address(iphy, &phy_sas_address);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001209
1210 if (port_sas_address.high != phy_sas_address.high ||
1211 port_sas_address.low != phy_sas_address.low)
1212 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
1213 }
Dan Williams89a73012011-06-30 19:14:33 -07001214 return sci_port_set_phy(iport, iphy);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001215 }
Edmund Nadolskie3013702011-06-02 00:10:43 +00001216 case SCI_PORT_SUB_WAITING:
1217 case SCI_PORT_SUB_OPERATIONAL:
Dan Williams89a73012011-06-30 19:14:33 -07001218 status = sci_port_set_phy(iport, iphy);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001219
1220 if (status != SCI_SUCCESS)
1221 return status;
1222
Marcin Tomczak05b080f2012-01-04 01:33:41 -08001223 sci_port_general_link_up_handler(iport, iphy, PF_NOTIFY|PF_RESUME);
Dan Williamsffe191c2011-06-29 13:09:25 -07001224 iport->not_ready_reason = SCIC_PORT_NOT_READY_RECONFIGURING;
1225 port_state_machine_change(iport, SCI_PORT_SUB_CONFIGURING);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001226
1227 return status;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001228 case SCI_PORT_SUB_CONFIGURING:
Dan Williams89a73012011-06-30 19:14:33 -07001229 status = sci_port_set_phy(iport, iphy);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001230
1231 if (status != SCI_SUCCESS)
1232 return status;
Marcin Tomczak05b080f2012-01-04 01:33:41 -08001233 sci_port_general_link_up_handler(iport, iphy, PF_NOTIFY);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001234
1235 /* Re-enter the configuring state since this may be the last phy in
1236 * the port.
1237 */
Dan Williamsffe191c2011-06-29 13:09:25 -07001238 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001239 SCI_PORT_SUB_CONFIGURING);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001240 return SCI_SUCCESS;
1241 default:
Dan Williamsffe191c2011-06-29 13:09:25 -07001242 dev_warn(sciport_to_dev(iport),
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001243 "%s: in wrong state: %d\n", __func__, state);
1244 return SCI_FAILURE_INVALID_STATE;
1245 }
1246}
1247
1248/**
Dan Williams89a73012011-06-30 19:14:33 -07001249 * sci_port_remove_phy() -
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001250 * @sci_port: This parameter specifies the port in which the phy will be added.
1251 * @sci_phy: This parameter is the phy which is to be added to the port.
1252 *
1253 * This method will remove the PHY from the selected PORT. This method returns
1254 * an enum sci_status. SCI_SUCCESS the phy has been removed from the port. Any
1255 * other status is a failure to add the phy to the port.
1256 */
Dan Williams89a73012011-06-30 19:14:33 -07001257enum sci_status sci_port_remove_phy(struct isci_port *iport,
Dan Williams85280952011-06-28 15:05:53 -07001258 struct isci_phy *iphy)
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001259{
1260 enum sci_status status;
Dan Williams89a73012011-06-30 19:14:33 -07001261 enum sci_port_states state;
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001262
Dan Williamsffe191c2011-06-29 13:09:25 -07001263 state = iport->sm.current_state_id;
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001264
1265 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001266 case SCI_PORT_STOPPED:
Dan Williams89a73012011-06-30 19:14:33 -07001267 return sci_port_clear_phy(iport, iphy);
Edmund Nadolskie3013702011-06-02 00:10:43 +00001268 case SCI_PORT_SUB_OPERATIONAL:
Dan Williams89a73012011-06-30 19:14:33 -07001269 status = sci_port_clear_phy(iport, iphy);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001270 if (status != SCI_SUCCESS)
1271 return status;
1272
Dan Williams89a73012011-06-30 19:14:33 -07001273 sci_port_deactivate_phy(iport, iphy, true);
Dan Williamsffe191c2011-06-29 13:09:25 -07001274 iport->not_ready_reason = SCIC_PORT_NOT_READY_RECONFIGURING;
1275 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001276 SCI_PORT_SUB_CONFIGURING);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001277 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001278 case SCI_PORT_SUB_CONFIGURING:
Dan Williams89a73012011-06-30 19:14:33 -07001279 status = sci_port_clear_phy(iport, iphy);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001280
1281 if (status != SCI_SUCCESS)
1282 return status;
Dan Williams89a73012011-06-30 19:14:33 -07001283 sci_port_deactivate_phy(iport, iphy, true);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001284
1285 /* Re-enter the configuring state since this may be the last phy in
1286 * the port
1287 */
Dan Williamsffe191c2011-06-29 13:09:25 -07001288 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001289 SCI_PORT_SUB_CONFIGURING);
Piotr Sawicki051266c2011-05-12 19:10:14 +00001290 return SCI_SUCCESS;
1291 default:
Dan Williamsffe191c2011-06-29 13:09:25 -07001292 dev_warn(sciport_to_dev(iport),
Piotr Sawicki051266c2011-05-12 19:10:14 +00001293 "%s: in wrong state: %d\n", __func__, state);
1294 return SCI_FAILURE_INVALID_STATE;
1295 }
1296}
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001297
Dan Williams89a73012011-06-30 19:14:33 -07001298enum sci_status sci_port_link_up(struct isci_port *iport,
Dan Williams85280952011-06-28 15:05:53 -07001299 struct isci_phy *iphy)
Piotr Sawicki051266c2011-05-12 19:10:14 +00001300{
Dan Williams89a73012011-06-30 19:14:33 -07001301 enum sci_port_states state;
Piotr Sawicki051266c2011-05-12 19:10:14 +00001302
Dan Williamsffe191c2011-06-29 13:09:25 -07001303 state = iport->sm.current_state_id;
Piotr Sawicki051266c2011-05-12 19:10:14 +00001304 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001305 case SCI_PORT_SUB_WAITING:
Piotr Sawicki051266c2011-05-12 19:10:14 +00001306 /* Since this is the first phy going link up for the port we
1307 * can just enable it and continue
1308 */
Marcin Tomczak05b080f2012-01-04 01:33:41 -08001309 sci_port_activate_phy(iport, iphy, PF_NOTIFY|PF_RESUME);
Piotr Sawicki051266c2011-05-12 19:10:14 +00001310
Dan Williamsffe191c2011-06-29 13:09:25 -07001311 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001312 SCI_PORT_SUB_OPERATIONAL);
Piotr Sawicki051266c2011-05-12 19:10:14 +00001313 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001314 case SCI_PORT_SUB_OPERATIONAL:
Marcin Tomczak05b080f2012-01-04 01:33:41 -08001315 sci_port_general_link_up_handler(iport, iphy, PF_NOTIFY|PF_RESUME);
Piotr Sawicki051266c2011-05-12 19:10:14 +00001316 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001317 case SCI_PORT_RESETTING:
Piotr Sawicki051266c2011-05-12 19:10:14 +00001318 /* TODO We should make sure that the phy that has gone
1319 * link up is the same one on which we sent the reset. It is
1320 * possible that the phy on which we sent the reset is not the
1321 * one that has gone link up and we want to make sure that
1322 * phy being reset comes back. Consider the case where a
1323 * reset is sent but before the hardware processes the reset it
1324 * get a link up on the port because of a hot plug event.
1325 * because of the reset request this phy will go link down
1326 * almost immediately.
1327 */
1328
1329 /* In the resetting state we don't notify the user regarding
1330 * link up and link down notifications.
1331 */
Marcin Tomczak05b080f2012-01-04 01:33:41 -08001332 sci_port_general_link_up_handler(iport, iphy, PF_RESUME);
Piotr Sawicki051266c2011-05-12 19:10:14 +00001333 return SCI_SUCCESS;
1334 default:
Dan Williamsffe191c2011-06-29 13:09:25 -07001335 dev_warn(sciport_to_dev(iport),
Piotr Sawicki051266c2011-05-12 19:10:14 +00001336 "%s: in wrong state: %d\n", __func__, state);
1337 return SCI_FAILURE_INVALID_STATE;
1338 }
1339}
1340
Dan Williams89a73012011-06-30 19:14:33 -07001341enum sci_status sci_port_link_down(struct isci_port *iport,
Dan Williams85280952011-06-28 15:05:53 -07001342 struct isci_phy *iphy)
Piotr Sawicki051266c2011-05-12 19:10:14 +00001343{
Dan Williams89a73012011-06-30 19:14:33 -07001344 enum sci_port_states state;
Piotr Sawicki051266c2011-05-12 19:10:14 +00001345
Dan Williamsffe191c2011-06-29 13:09:25 -07001346 state = iport->sm.current_state_id;
Piotr Sawicki051266c2011-05-12 19:10:14 +00001347 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001348 case SCI_PORT_SUB_OPERATIONAL:
Dan Williams89a73012011-06-30 19:14:33 -07001349 sci_port_deactivate_phy(iport, iphy, true);
Piotr Sawicki051266c2011-05-12 19:10:14 +00001350
1351 /* If there are no active phys left in the port, then
1352 * transition the port to the WAITING state until such time
1353 * as a phy goes link up
1354 */
Dan Williamsffe191c2011-06-29 13:09:25 -07001355 if (iport->active_phy_mask == 0)
1356 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001357 SCI_PORT_SUB_WAITING);
Piotr Sawicki051266c2011-05-12 19:10:14 +00001358 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001359 case SCI_PORT_RESETTING:
Piotr Sawicki051266c2011-05-12 19:10:14 +00001360 /* In the resetting state we don't notify the user regarding
1361 * link up and link down notifications. */
Dan Williams89a73012011-06-30 19:14:33 -07001362 sci_port_deactivate_phy(iport, iphy, false);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001363 return SCI_SUCCESS;
1364 default:
Dan Williamsffe191c2011-06-29 13:09:25 -07001365 dev_warn(sciport_to_dev(iport),
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001366 "%s: in wrong state: %d\n", __func__, state);
1367 return SCI_FAILURE_INVALID_STATE;
1368 }
1369}
1370
Dan Williams89a73012011-06-30 19:14:33 -07001371enum sci_status sci_port_start_io(struct isci_port *iport,
1372 struct isci_remote_device *idev,
1373 struct isci_request *ireq)
Dan Williams68138202011-05-12 07:16:06 -07001374{
Dan Williams89a73012011-06-30 19:14:33 -07001375 enum sci_port_states state;
Dan Williamse2f8db52011-05-10 02:28:46 -07001376
Dan Williamsffe191c2011-06-29 13:09:25 -07001377 state = iport->sm.current_state_id;
Dan Williams68138202011-05-12 07:16:06 -07001378 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001379 case SCI_PORT_SUB_WAITING:
Dan Williams68138202011-05-12 07:16:06 -07001380 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001381 case SCI_PORT_SUB_OPERATIONAL:
Dan Williamsffe191c2011-06-29 13:09:25 -07001382 iport->started_request_count++;
Dan Williams68138202011-05-12 07:16:06 -07001383 return SCI_SUCCESS;
1384 default:
Dan Williamsffe191c2011-06-29 13:09:25 -07001385 dev_warn(sciport_to_dev(iport),
Dan Williams68138202011-05-12 07:16:06 -07001386 "%s: in wrong state: %d\n", __func__, state);
1387 return SCI_FAILURE_INVALID_STATE;
1388 }
1389}
1390
Dan Williams89a73012011-06-30 19:14:33 -07001391enum sci_status sci_port_complete_io(struct isci_port *iport,
1392 struct isci_remote_device *idev,
1393 struct isci_request *ireq)
Dan Williams68138202011-05-12 07:16:06 -07001394{
Dan Williams89a73012011-06-30 19:14:33 -07001395 enum sci_port_states state;
Dan Williams68138202011-05-12 07:16:06 -07001396
Dan Williamsffe191c2011-06-29 13:09:25 -07001397 state = iport->sm.current_state_id;
Dan Williams68138202011-05-12 07:16:06 -07001398 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001399 case SCI_PORT_STOPPED:
Dan Williamsffe191c2011-06-29 13:09:25 -07001400 dev_warn(sciport_to_dev(iport),
Dan Williams68138202011-05-12 07:16:06 -07001401 "%s: in wrong state: %d\n", __func__, state);
1402 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001403 case SCI_PORT_STOPPING:
Dan Williams89a73012011-06-30 19:14:33 -07001404 sci_port_decrement_request_count(iport);
Dan Williams68138202011-05-12 07:16:06 -07001405
Dan Williamsffe191c2011-06-29 13:09:25 -07001406 if (iport->started_request_count == 0)
1407 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001408 SCI_PORT_STOPPED);
Dan Williams68138202011-05-12 07:16:06 -07001409 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001410 case SCI_PORT_READY:
1411 case SCI_PORT_RESETTING:
1412 case SCI_PORT_FAILED:
1413 case SCI_PORT_SUB_WAITING:
1414 case SCI_PORT_SUB_OPERATIONAL:
Dan Williams89a73012011-06-30 19:14:33 -07001415 sci_port_decrement_request_count(iport);
Dan Williams68138202011-05-12 07:16:06 -07001416 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001417 case SCI_PORT_SUB_CONFIGURING:
Dan Williams89a73012011-06-30 19:14:33 -07001418 sci_port_decrement_request_count(iport);
Dan Williamsffe191c2011-06-29 13:09:25 -07001419 if (iport->started_request_count == 0) {
1420 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001421 SCI_PORT_SUB_OPERATIONAL);
Dan Williams68138202011-05-12 07:16:06 -07001422 }
1423 break;
1424 }
1425 return SCI_SUCCESS;
1426}
Dan Williamse2f8db52011-05-10 02:28:46 -07001427
Dan Williams89a73012011-06-30 19:14:33 -07001428static void sci_port_enable_port_task_scheduler(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -07001429{
1430 u32 pts_control_value;
1431
Dan Williams89a73012011-06-30 19:14:33 -07001432 /* enable the port task scheduler in a suspended state */
Dan Williamsffe191c2011-06-29 13:09:25 -07001433 pts_control_value = readl(&iport->port_task_scheduler_registers->control);
Dan Williamse2f8db52011-05-10 02:28:46 -07001434 pts_control_value |= SCU_PTSxCR_GEN_BIT(ENABLE) | SCU_PTSxCR_GEN_BIT(SUSPEND);
Dan Williamsffe191c2011-06-29 13:09:25 -07001435 writel(pts_control_value, &iport->port_task_scheduler_registers->control);
Dan Williamse2f8db52011-05-10 02:28:46 -07001436}
1437
Dan Williams89a73012011-06-30 19:14:33 -07001438static void sci_port_disable_port_task_scheduler(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -07001439{
1440 u32 pts_control_value;
1441
Dan Williamsffe191c2011-06-29 13:09:25 -07001442 pts_control_value = readl(&iport->port_task_scheduler_registers->control);
Dan Williamse2f8db52011-05-10 02:28:46 -07001443 pts_control_value &=
1444 ~(SCU_PTSxCR_GEN_BIT(ENABLE) | SCU_PTSxCR_GEN_BIT(SUSPEND));
Dan Williamsffe191c2011-06-29 13:09:25 -07001445 writel(pts_control_value, &iport->port_task_scheduler_registers->control);
Dan Williamse2f8db52011-05-10 02:28:46 -07001446}
1447
Dan Williams89a73012011-06-30 19:14:33 -07001448static void sci_port_post_dummy_remote_node(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -07001449{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001450 struct isci_host *ihost = iport->owning_controller;
Dan Williamsffe191c2011-06-29 13:09:25 -07001451 u8 phys_index = iport->physical_port_index;
Dan Williamse2f8db52011-05-10 02:28:46 -07001452 union scu_remote_node_context *rnc;
Dan Williamsffe191c2011-06-29 13:09:25 -07001453 u16 rni = iport->reserved_rni;
Dan Williamse2f8db52011-05-10 02:28:46 -07001454 u32 command;
1455
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001456 rnc = &ihost->remote_node_context_table[rni];
Dan Williamse2f8db52011-05-10 02:28:46 -07001457 rnc->ssp.is_valid = true;
1458
1459 command = SCU_CONTEXT_COMMAND_POST_RNC_32 |
1460 phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
1461
Dan Williams89a73012011-06-30 19:14:33 -07001462 sci_controller_post_request(ihost, command);
Dan Williamse2f8db52011-05-10 02:28:46 -07001463
1464 /* ensure hardware has seen the post rnc command and give it
1465 * ample time to act before sending the suspend
1466 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001467 readl(&ihost->smu_registers->interrupt_status); /* flush */
Dan Williamse2f8db52011-05-10 02:28:46 -07001468 udelay(10);
1469
1470 command = SCU_CONTEXT_COMMAND_POST_RNC_SUSPEND_TX_RX |
1471 phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
1472
Dan Williams89a73012011-06-30 19:14:33 -07001473 sci_controller_post_request(ihost, command);
Dan Williamse2f8db52011-05-10 02:28:46 -07001474}
1475
Dan Williams89a73012011-06-30 19:14:33 -07001476static void sci_port_stopped_state_enter(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001477{
Dan Williamsffe191c2011-06-29 13:09:25 -07001478 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamse2f8db52011-05-10 02:28:46 -07001479
Dan Williamsffe191c2011-06-29 13:09:25 -07001480 if (iport->sm.previous_state_id == SCI_PORT_STOPPING) {
Dan Williamse2f8db52011-05-10 02:28:46 -07001481 /*
1482 * If we enter this state becasuse of a request to stop
1483 * the port then we want to disable the hardwares port
1484 * task scheduler. */
Dan Williams89a73012011-06-30 19:14:33 -07001485 sci_port_disable_port_task_scheduler(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -07001486 }
1487}
1488
Dan Williams89a73012011-06-30 19:14:33 -07001489static void sci_port_stopped_state_exit(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001490{
Dan Williamsffe191c2011-06-29 13:09:25 -07001491 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamse2f8db52011-05-10 02:28:46 -07001492
1493 /* Enable and suspend the port task scheduler */
Dan Williams89a73012011-06-30 19:14:33 -07001494 sci_port_enable_port_task_scheduler(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -07001495}
1496
Dan Williams89a73012011-06-30 19:14:33 -07001497static void sci_port_ready_state_enter(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001498{
Dan Williamsffe191c2011-06-29 13:09:25 -07001499 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001500 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -07001501 u32 prev_state;
1502
Dan Williamsffe191c2011-06-29 13:09:25 -07001503 prev_state = iport->sm.previous_state_id;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001504 if (prev_state == SCI_PORT_RESETTING)
Dan Williamse2f8db52011-05-10 02:28:46 -07001505 isci_port_hard_reset_complete(iport, SCI_SUCCESS);
1506 else
Dan Williamsfca4ecb2012-01-03 23:26:15 -08001507 dev_dbg(&ihost->pdev->dev, "%s: port%d !ready\n",
1508 __func__, iport->physical_port_index);
Dan Williamse2f8db52011-05-10 02:28:46 -07001509
1510 /* Post and suspend the dummy remote node context for this port. */
Dan Williams89a73012011-06-30 19:14:33 -07001511 sci_port_post_dummy_remote_node(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -07001512
1513 /* Start the ready substate machine */
Dan Williamsffe191c2011-06-29 13:09:25 -07001514 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001515 SCI_PORT_SUB_WAITING);
Dan Williamse2f8db52011-05-10 02:28:46 -07001516}
1517
Dan Williams89a73012011-06-30 19:14:33 -07001518static void sci_port_resetting_state_exit(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001519{
Dan Williamsffe191c2011-06-29 13:09:25 -07001520 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamse2f8db52011-05-10 02:28:46 -07001521
Dan Williamsffe191c2011-06-29 13:09:25 -07001522 sci_del_timer(&iport->timer);
Dan Williamse2f8db52011-05-10 02:28:46 -07001523}
1524
Dan Williams89a73012011-06-30 19:14:33 -07001525static void sci_port_stopping_state_exit(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001526{
Dan Williamsffe191c2011-06-29 13:09:25 -07001527 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamse2f8db52011-05-10 02:28:46 -07001528
Dan Williamsffe191c2011-06-29 13:09:25 -07001529 sci_del_timer(&iport->timer);
Dan Williamse2f8db52011-05-10 02:28:46 -07001530
Dan Williams89a73012011-06-30 19:14:33 -07001531 sci_port_destroy_dummy_resources(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -07001532}
1533
Dan Williams89a73012011-06-30 19:14:33 -07001534static void sci_port_failed_state_enter(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001535{
Dan Williamsffe191c2011-06-29 13:09:25 -07001536 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamse2f8db52011-05-10 02:28:46 -07001537
Dan Williamse2f8db52011-05-10 02:28:46 -07001538 isci_port_hard_reset_complete(iport, SCI_FAILURE_TIMEOUT);
1539}
1540
1541/* --------------------------------------------------------------------------- */
1542
Dan Williams89a73012011-06-30 19:14:33 -07001543static const struct sci_base_state sci_port_state_table[] = {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001544 [SCI_PORT_STOPPED] = {
Dan Williams89a73012011-06-30 19:14:33 -07001545 .enter_state = sci_port_stopped_state_enter,
1546 .exit_state = sci_port_stopped_state_exit
Dan Williamse2f8db52011-05-10 02:28:46 -07001547 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001548 [SCI_PORT_STOPPING] = {
Dan Williams89a73012011-06-30 19:14:33 -07001549 .exit_state = sci_port_stopping_state_exit
Dan Williamse2f8db52011-05-10 02:28:46 -07001550 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001551 [SCI_PORT_READY] = {
Dan Williams89a73012011-06-30 19:14:33 -07001552 .enter_state = sci_port_ready_state_enter,
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001553 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001554 [SCI_PORT_SUB_WAITING] = {
Dan Williams89a73012011-06-30 19:14:33 -07001555 .enter_state = sci_port_ready_substate_waiting_enter,
Marcin Tomczak05b080f2012-01-04 01:33:41 -08001556 .exit_state = scic_sds_port_ready_substate_waiting_exit,
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001557 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001558 [SCI_PORT_SUB_OPERATIONAL] = {
Dan Williams89a73012011-06-30 19:14:33 -07001559 .enter_state = sci_port_ready_substate_operational_enter,
1560 .exit_state = sci_port_ready_substate_operational_exit
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001561 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001562 [SCI_PORT_SUB_CONFIGURING] = {
Marcin Tomczak05b080f2012-01-04 01:33:41 -08001563 .enter_state = sci_port_ready_substate_configuring_enter
Dan Williamse2f8db52011-05-10 02:28:46 -07001564 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001565 [SCI_PORT_RESETTING] = {
Dan Williams89a73012011-06-30 19:14:33 -07001566 .exit_state = sci_port_resetting_state_exit
Dan Williamse2f8db52011-05-10 02:28:46 -07001567 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001568 [SCI_PORT_FAILED] = {
Dan Williams89a73012011-06-30 19:14:33 -07001569 .enter_state = sci_port_failed_state_enter,
Dan Williamse2f8db52011-05-10 02:28:46 -07001570 }
1571};
1572
Dan Williams89a73012011-06-30 19:14:33 -07001573void sci_port_construct(struct isci_port *iport, u8 index,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001574 struct isci_host *ihost)
Dan Williamse2f8db52011-05-10 02:28:46 -07001575{
Dan Williams89a73012011-06-30 19:14:33 -07001576 sci_init_sm(&iport->sm, sci_port_state_table, SCI_PORT_STOPPED);
Dan Williamse2f8db52011-05-10 02:28:46 -07001577
Dan Williamsffe191c2011-06-29 13:09:25 -07001578 iport->logical_port_index = SCIC_SDS_DUMMY_PORT;
1579 iport->physical_port_index = index;
1580 iport->active_phy_mask = 0;
Marcin Tomczak05b080f2012-01-04 01:33:41 -08001581 iport->enabled_phy_mask = 0;
Jeff Skirvin8e35a132011-10-27 15:05:32 -07001582 iport->last_active_phy = 0;
1583 iport->ready_exit = false;
Dan Williamse2f8db52011-05-10 02:28:46 -07001584
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001585 iport->owning_controller = ihost;
Dan Williamse2f8db52011-05-10 02:28:46 -07001586
Dan Williamsffe191c2011-06-29 13:09:25 -07001587 iport->started_request_count = 0;
1588 iport->assigned_device_count = 0;
Dan Williamse2f8db52011-05-10 02:28:46 -07001589
Dan Williamsffe191c2011-06-29 13:09:25 -07001590 iport->reserved_rni = SCU_DUMMY_INDEX;
1591 iport->reserved_tag = SCI_CONTROLLER_INVALID_IO_TAG;
Dan Williamse2f8db52011-05-10 02:28:46 -07001592
Dan Williamsffe191c2011-06-29 13:09:25 -07001593 sci_init_timer(&iport->timer, port_timeout);
Edmund Nadolski5553ba22011-05-19 11:59:10 +00001594
Dan Williamsffe191c2011-06-29 13:09:25 -07001595 iport->port_task_scheduler_registers = NULL;
Dan Williamse2f8db52011-05-10 02:28:46 -07001596
1597 for (index = 0; index < SCI_MAX_PHYS; index++)
Dan Williamsffe191c2011-06-29 13:09:25 -07001598 iport->phy_table[index] = NULL;
Dan Williamse2f8db52011-05-10 02:28:46 -07001599}
1600
1601void isci_port_init(struct isci_port *iport, struct isci_host *ihost, int index)
1602{
1603 INIT_LIST_HEAD(&iport->remote_dev_list);
1604 INIT_LIST_HEAD(&iport->domain_dev_list);
Dan Williamse2f8db52011-05-10 02:28:46 -07001605 iport->isci_host = ihost;
Dan Williamse2f8db52011-05-10 02:28:46 -07001606}
1607
Dan Williams89a73012011-06-30 19:14:33 -07001608void sci_port_broadcast_change_received(struct isci_port *iport, struct isci_phy *iphy)
Dan Williamse2f8db52011-05-10 02:28:46 -07001609{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001610 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -07001611
1612 /* notify the user. */
Dan Williamsffe191c2011-06-29 13:09:25 -07001613 isci_port_bc_change_received(ihost, iport, iphy);
Dan Williamse2f8db52011-05-10 02:28:46 -07001614}
1615
Dan Williams92776992011-11-30 11:57:34 -08001616static void wait_port_reset(struct isci_host *ihost, struct isci_port *iport)
1617{
1618 wait_event(ihost->eventq, !test_bit(IPORT_RESET_PENDING, &iport->state));
1619}
1620
Dan Williams4393aa42011-03-31 13:10:44 -07001621int isci_port_perform_hard_reset(struct isci_host *ihost, struct isci_port *iport,
1622 struct isci_phy *iphy)
Dan Williams6f231dd2011-07-02 22:56:22 -07001623{
Dan Williams4393aa42011-03-31 13:10:44 -07001624 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -07001625 enum sci_status status;
Jeff Skirvin8e35a132011-10-27 15:05:32 -07001626 int ret = TMF_RESP_FUNC_COMPLETE;
Dan Williams6f231dd2011-07-02 22:56:22 -07001627
Dan Williams4393aa42011-03-31 13:10:44 -07001628 dev_dbg(&ihost->pdev->dev, "%s: iport = %p\n",
1629 __func__, iport);
Dan Williams6f231dd2011-07-02 22:56:22 -07001630
Dan Williams4393aa42011-03-31 13:10:44 -07001631 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams92776992011-11-30 11:57:34 -08001632 set_bit(IPORT_RESET_PENDING, &iport->state);
Dan Williams6f231dd2011-07-02 22:56:22 -07001633
1634 #define ISCI_PORT_RESET_TIMEOUT SCIC_SDS_SIGNATURE_FIS_TIMEOUT
Dan Williams89a73012011-06-30 19:14:33 -07001635 status = sci_port_hard_reset(iport, ISCI_PORT_RESET_TIMEOUT);
Dan Williams6f231dd2011-07-02 22:56:22 -07001636
Dan Williams4393aa42011-03-31 13:10:44 -07001637 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001638
1639 if (status == SCI_SUCCESS) {
Dan Williams92776992011-11-30 11:57:34 -08001640 wait_port_reset(ihost, iport);
Dan Williams6f231dd2011-07-02 22:56:22 -07001641
Dan Williams4393aa42011-03-31 13:10:44 -07001642 dev_dbg(&ihost->pdev->dev,
1643 "%s: iport = %p; hard reset completion\n",
1644 __func__, iport);
Dan Williams6f231dd2011-07-02 22:56:22 -07001645
Jeff Skirvin8e35a132011-10-27 15:05:32 -07001646 if (iport->hard_reset_status != SCI_SUCCESS) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001647 ret = TMF_RESP_FUNC_FAILED;
Jeff Skirvin8e35a132011-10-27 15:05:32 -07001648
1649 dev_err(&ihost->pdev->dev,
1650 "%s: iport = %p; hard reset failed (0x%x)\n",
1651 __func__, iport, iport->hard_reset_status);
1652 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001653 } else {
Dan Williams92776992011-11-30 11:57:34 -08001654 clear_bit(IPORT_RESET_PENDING, &iport->state);
1655 wake_up(&ihost->eventq);
Dan Williams6f231dd2011-07-02 22:56:22 -07001656 ret = TMF_RESP_FUNC_FAILED;
1657
Dan Williams4393aa42011-03-31 13:10:44 -07001658 dev_err(&ihost->pdev->dev,
Dan Williams89a73012011-06-30 19:14:33 -07001659 "%s: iport = %p; sci_port_hard_reset call"
Dan Williams6f231dd2011-07-02 22:56:22 -07001660 " failed 0x%x\n",
Dan Williams4393aa42011-03-31 13:10:44 -07001661 __func__, iport, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001662
1663 }
1664
1665 /* If the hard reset for the port has failed, consider this
1666 * the same as link failures on all phys in the port.
1667 */
1668 if (ret != TMF_RESP_FUNC_COMPLETE) {
Jeff Skirvinfd0527a2011-06-20 14:09:26 -07001669
Dan Williams4393aa42011-03-31 13:10:44 -07001670 dev_err(&ihost->pdev->dev,
1671 "%s: iport = %p; hard reset failed "
Jeff Skirvinfd0527a2011-06-20 14:09:26 -07001672 "(0x%x) - driving explicit link fail for all phys\n",
1673 __func__, iport, iport->hard_reset_status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001674 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001675 return ret;
1676}
Dave Jiang09d7da12011-03-26 16:11:51 -07001677
Dan Williams687833a2011-11-17 18:01:38 -08001678int isci_ata_check_ready(struct domain_device *dev)
1679{
1680 struct isci_port *iport = dev->port->lldd_port;
1681 struct isci_host *ihost = dev_to_ihost(dev);
1682 struct isci_remote_device *idev;
1683 unsigned long flags;
1684 int rc = 0;
1685
1686 spin_lock_irqsave(&ihost->scic_lock, flags);
1687 idev = isci_lookup_device(dev);
1688 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1689
1690 if (!idev)
1691 goto out;
1692
1693 if (test_bit(IPORT_RESET_PENDING, &iport->state))
1694 goto out;
1695
1696 rc = !!iport->active_phy_mask;
1697 out:
1698 isci_put_device(idev);
1699
1700 return rc;
1701}
1702
Dan Williamse2f8db52011-05-10 02:28:46 -07001703void isci_port_deformed(struct asd_sas_phy *phy)
Dave Jiang09d7da12011-03-26 16:11:51 -07001704{
Dan Williamsc132f692012-01-03 23:26:08 -08001705 struct isci_host *ihost = phy->ha->lldd_ha;
1706 struct isci_port *iport = phy->port->lldd_port;
1707 unsigned long flags;
1708 int i;
1709
1710 /* we got a port notification on a port that was subsequently
1711 * torn down and libsas is just now catching up
1712 */
1713 if (!iport)
1714 return;
1715
1716 spin_lock_irqsave(&ihost->scic_lock, flags);
1717 for (i = 0; i < SCI_MAX_PHYS; i++) {
1718 if (iport->active_phy_mask & 1 << i)
1719 break;
1720 }
1721 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1722
1723 if (i >= SCI_MAX_PHYS)
1724 dev_dbg(&ihost->pdev->dev, "%s: port: %ld\n",
1725 __func__, (long) (iport - &ihost->ports[0]));
Dan Williamse2f8db52011-05-10 02:28:46 -07001726}
1727
Dan Williamse2f8db52011-05-10 02:28:46 -07001728void isci_port_formed(struct asd_sas_phy *phy)
1729{
Dan Williamsc132f692012-01-03 23:26:08 -08001730 struct isci_host *ihost = phy->ha->lldd_ha;
1731 struct isci_phy *iphy = to_iphy(phy);
1732 struct asd_sas_port *port = phy->port;
1733 struct isci_port *iport;
1734 unsigned long flags;
1735 int i;
1736
1737 /* initial ports are formed as the driver is still initializing,
1738 * wait for that process to complete
1739 */
1740 wait_for_start(ihost);
1741
1742 spin_lock_irqsave(&ihost->scic_lock, flags);
1743 for (i = 0; i < SCI_MAX_PORTS; i++) {
1744 iport = &ihost->ports[i];
1745 if (iport->active_phy_mask & 1 << iphy->phy_index)
1746 break;
1747 }
1748 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1749
1750 if (i >= SCI_MAX_PORTS)
1751 iport = NULL;
1752
1753 port->lldd_port = iport;
Dave Jiang09d7da12011-03-26 16:11:51 -07001754}