blob: a28c9e6e280689fd2f6b1f4d72f0d4e6d1935886 [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 Williamse5313812011-05-07 10:11:43 -070078static void isci_port_change_state(struct isci_port *iport, enum isci_status status)
79{
80 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -070081
Dan Williamse5313812011-05-07 10:11:43 -070082 dev_dbg(&iport->isci_host->pdev->dev,
83 "%s: iport = %p, state = 0x%x\n",
84 __func__, iport, status);
Dan Williams6f231dd2011-07-02 22:56:22 -070085
Dan Williamse5313812011-05-07 10:11:43 -070086 /* XXX pointless lock */
87 spin_lock_irqsave(&iport->state_lock, flags);
88 iport->status = status;
89 spin_unlock_irqrestore(&iport->state_lock, flags);
90}
Dan Williams6f231dd2011-07-02 22:56:22 -070091
Dan Williams89a73012011-06-30 19:14:33 -070092static void sci_port_get_protocols(struct isci_port *iport, struct sci_phy_proto *proto)
Dan Williams6f231dd2011-07-02 22:56:22 -070093{
Dan Williamse2f8db52011-05-10 02:28:46 -070094 u8 index;
95
Dan Williams89a73012011-06-30 19:14:33 -070096 proto->all = 0;
Dan Williamse2f8db52011-05-10 02:28:46 -070097 for (index = 0; index < SCI_MAX_PHYS; index++) {
Dan Williams89a73012011-06-30 19:14:33 -070098 struct isci_phy *iphy = iport->phy_table[index];
99
100 if (!iphy)
101 continue;
102 sci_phy_get_protocols(iphy, proto);
Dan Williamse2f8db52011-05-10 02:28:46 -0700103 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700104}
105
Dan Williams89a73012011-06-30 19:14:33 -0700106static u32 sci_port_get_phys(struct isci_port *iport)
Dan Williams6f231dd2011-07-02 22:56:22 -0700107{
Dan Williamse2f8db52011-05-10 02:28:46 -0700108 u32 index;
109 u32 mask;
110
111 mask = 0;
Dan Williams89a73012011-06-30 19:14:33 -0700112 for (index = 0; index < SCI_MAX_PHYS; index++)
113 if (iport->phy_table[index])
Dan Williamse2f8db52011-05-10 02:28:46 -0700114 mask |= (1 << index);
Dan Williamse2f8db52011-05-10 02:28:46 -0700115
116 return mask;
Dan Williams6f231dd2011-07-02 22:56:22 -0700117}
118
Dan Williamse2f8db52011-05-10 02:28:46 -0700119/**
Dan Williams89a73012011-06-30 19:14:33 -0700120 * sci_port_get_properties() - This method simply returns the properties
Dan Williamse2f8db52011-05-10 02:28:46 -0700121 * regarding the port, such as: physical index, protocols, sas address, etc.
122 * @port: this parameter specifies the port for which to retrieve the physical
123 * index.
124 * @properties: This parameter specifies the properties structure into which to
125 * copy the requested information.
126 *
127 * Indicate if the user specified a valid port. SCI_SUCCESS This value is
128 * returned if the specified port was valid. SCI_FAILURE_INVALID_PORT This
129 * value is returned if the specified port is not valid. When this value is
130 * returned, no data is copied to the properties output parameter.
131 */
Bartek Nowakowski7e629842012-01-04 01:33:20 -0800132enum sci_status sci_port_get_properties(struct isci_port *iport,
Dan Williams89a73012011-06-30 19:14:33 -0700133 struct sci_port_properties *prop)
Dan Williams6f231dd2011-07-02 22:56:22 -0700134{
Dan Williamsffe191c2011-06-29 13:09:25 -0700135 if (!iport || iport->logical_port_index == SCIC_SDS_DUMMY_PORT)
Dan Williamse2f8db52011-05-10 02:28:46 -0700136 return SCI_FAILURE_INVALID_PORT;
Dan Williams6f231dd2011-07-02 22:56:22 -0700137
Dan Williams89a73012011-06-30 19:14:33 -0700138 prop->index = iport->logical_port_index;
139 prop->phy_mask = sci_port_get_phys(iport);
140 sci_port_get_sas_address(iport, &prop->local.sas_address);
141 sci_port_get_protocols(iport, &prop->local.protocols);
142 sci_port_get_attached_sas_address(iport, &prop->remote.sas_address);
Dan Williams6f231dd2011-07-02 22:56:22 -0700143
Dan Williamse2f8db52011-05-10 02:28:46 -0700144 return SCI_SUCCESS;
Dan Williams6f231dd2011-07-02 22:56:22 -0700145}
146
Dan Williams89a73012011-06-30 19:14:33 -0700147static void sci_port_bcn_enable(struct isci_port *iport)
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700148{
Dan Williams85280952011-06-28 15:05:53 -0700149 struct isci_phy *iphy;
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700150 u32 val;
151 int i;
152
Dan Williamsffe191c2011-06-29 13:09:25 -0700153 for (i = 0; i < ARRAY_SIZE(iport->phy_table); i++) {
154 iphy = iport->phy_table[i];
Dan Williams85280952011-06-28 15:05:53 -0700155 if (!iphy)
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700156 continue;
Dan Williams85280952011-06-28 15:05:53 -0700157 val = readl(&iphy->link_layer_registers->link_layer_control);
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700158 /* clear the bit by writing 1. */
Dan Williams85280952011-06-28 15:05:53 -0700159 writel(val, &iphy->link_layer_registers->link_layer_control);
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700160 }
161}
162
Dan Williamsffe191c2011-06-29 13:09:25 -0700163static void isci_port_bc_change_received(struct isci_host *ihost,
164 struct isci_port *iport,
165 struct isci_phy *iphy)
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700166{
Dan Williams52d74632011-10-27 15:05:37 -0700167 dev_dbg(&ihost->pdev->dev,
168 "%s: isci_phy = %p, sas_phy = %p\n",
169 __func__, iphy, &iphy->sas_phy);
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700170
Dan Williams52d74632011-10-27 15:05:37 -0700171 ihost->sas_ha.notify_port_event(&iphy->sas_phy, PORTE_BROADCAST_RCVD);
Dan Williams89a73012011-06-30 19:14:33 -0700172 sci_port_bcn_enable(iport);
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700173}
174
Dan Williamse2f8db52011-05-10 02:28:46 -0700175static void isci_port_link_up(struct isci_host *isci_host,
Dan Williamsffe191c2011-06-29 13:09:25 -0700176 struct isci_port *iport,
Dan Williams85280952011-06-28 15:05:53 -0700177 struct isci_phy *iphy)
Dan Williams6f231dd2011-07-02 22:56:22 -0700178{
179 unsigned long flags;
Dan Williams89a73012011-06-30 19:14:33 -0700180 struct sci_port_properties properties;
Dan Williams6f231dd2011-07-02 22:56:22 -0700181 unsigned long success = true;
182
Dan Williams6f231dd2011-07-02 22:56:22 -0700183 dev_dbg(&isci_host->pdev->dev,
184 "%s: isci_port = %p\n",
Dan Williamsffe191c2011-06-29 13:09:25 -0700185 __func__, iport);
Dan Williams6f231dd2011-07-02 22:56:22 -0700186
Dan Williams85280952011-06-28 15:05:53 -0700187 spin_lock_irqsave(&iphy->sas_phy.frame_rcvd_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700188
Dan Williamsc132f692012-01-03 23:26:08 -0800189 isci_port_change_state(iport, isci_starting);
Dan Williams6f231dd2011-07-02 22:56:22 -0700190
Dan Williams89a73012011-06-30 19:14:33 -0700191 sci_port_get_properties(iport, &properties);
Dan Williams6f231dd2011-07-02 22:56:22 -0700192
Dan Williams85280952011-06-28 15:05:53 -0700193 if (iphy->protocol == SCIC_SDS_PHY_PROTOCOL_SATA) {
Dan Williams150fc6f2011-02-25 10:25:21 -0800194 u64 attached_sas_address;
Dan Williams6f231dd2011-07-02 22:56:22 -0700195
Dan Williams85280952011-06-28 15:05:53 -0700196 iphy->sas_phy.oob_mode = SATA_OOB_MODE;
197 iphy->sas_phy.frame_rcvd_size = sizeof(struct dev_to_host_fis);
Dan Williams6f231dd2011-07-02 22:56:22 -0700198
199 /*
200 * For direct-attached SATA devices, the SCI core will
201 * automagically assign a SAS address to the end device
202 * for the purpose of creating a port. This SAS address
203 * will not be the same as assigned to the PHY and needs
Dan Williams89a73012011-06-30 19:14:33 -0700204 * to be obtained from struct sci_port_properties properties.
Dan Williams6f231dd2011-07-02 22:56:22 -0700205 */
Dan Williams150fc6f2011-02-25 10:25:21 -0800206 attached_sas_address = properties.remote.sas_address.high;
207 attached_sas_address <<= 32;
208 attached_sas_address |= properties.remote.sas_address.low;
209 swab64s(&attached_sas_address);
Dan Williams6f231dd2011-07-02 22:56:22 -0700210
Dan Williams85280952011-06-28 15:05:53 -0700211 memcpy(&iphy->sas_phy.attached_sas_addr,
Dan Williams150fc6f2011-02-25 10:25:21 -0800212 &attached_sas_address, sizeof(attached_sas_address));
Dan Williams85280952011-06-28 15:05:53 -0700213 } else if (iphy->protocol == SCIC_SDS_PHY_PROTOCOL_SAS) {
214 iphy->sas_phy.oob_mode = SAS_OOB_MODE;
215 iphy->sas_phy.frame_rcvd_size = sizeof(struct sas_identify_frame);
Dan Williams6f231dd2011-07-02 22:56:22 -0700216
217 /* Copy the attached SAS address from the IAF */
Dan Williams85280952011-06-28 15:05:53 -0700218 memcpy(iphy->sas_phy.attached_sas_addr,
219 iphy->frame_rcvd.iaf.sas_addr, SAS_ADDR_SIZE);
Dan Williams6f231dd2011-07-02 22:56:22 -0700220 } else {
221 dev_err(&isci_host->pdev->dev, "%s: unkown target\n", __func__);
222 success = false;
223 }
224
Dan Williams85280952011-06-28 15:05:53 -0700225 iphy->sas_phy.phy->negotiated_linkrate = sci_phy_linkrate(iphy);
Dan Williams83e51432011-02-18 09:25:13 -0800226
Dan Williams85280952011-06-28 15:05:53 -0700227 spin_unlock_irqrestore(&iphy->sas_phy.frame_rcvd_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700228
229 /* Notify libsas that we have an address frame, if indeed
230 * we've found an SSP, SMP, or STP target */
231 if (success)
Dan Williams85280952011-06-28 15:05:53 -0700232 isci_host->sas_ha.notify_port_event(&iphy->sas_phy,
Dan Williams6f231dd2011-07-02 22:56:22 -0700233 PORTE_BYTES_DMAED);
234}
235
236
237/**
238 * isci_port_link_down() - This function is called by the sci core when a link
239 * becomes inactive.
240 * @isci_host: This parameter specifies the isci host object.
241 * @phy: This parameter specifies the isci phy with the active link.
242 * @port: This parameter specifies the isci port with the active link.
243 *
244 */
Dan Williamse2f8db52011-05-10 02:28:46 -0700245static void isci_port_link_down(struct isci_host *isci_host,
246 struct isci_phy *isci_phy,
247 struct isci_port *isci_port)
Dan Williams6f231dd2011-07-02 22:56:22 -0700248{
249 struct isci_remote_device *isci_device;
250
251 dev_dbg(&isci_host->pdev->dev,
252 "%s: isci_port = %p\n", __func__, isci_port);
253
254 if (isci_port) {
255
256 /* check to see if this is the last phy on this port. */
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700257 if (isci_phy->sas_phy.port &&
258 isci_phy->sas_phy.port->num_phys == 1) {
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700259 /* change the state for all devices on this port. The
260 * next task sent to this device will be returned as
261 * SAS_TASK_UNDELIVERED, and the scsi mid layer will
262 * remove the target
Dan Williams6f231dd2011-07-02 22:56:22 -0700263 */
264 list_for_each_entry(isci_device,
265 &isci_port->remote_dev_list,
266 node) {
267 dev_dbg(&isci_host->pdev->dev,
268 "%s: isci_device = %p\n",
269 __func__, isci_device);
Dan Williams209fae12011-06-13 17:39:44 -0700270 set_bit(IDEV_GONE, &isci_device->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700271 }
Jeff Skirvincdd05f02011-09-28 18:47:56 -0700272 isci_port_change_state(isci_port, isci_stopping);
Dan Williams6f231dd2011-07-02 22:56:22 -0700273 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700274 }
275
276 /* Notify libsas of the borken link, this will trigger calls to our
277 * isci_port_deformed and isci_dev_gone functions.
278 */
279 sas_phy_disconnected(&isci_phy->sas_phy);
280 isci_host->sas_ha.notify_phy_event(&isci_phy->sas_phy,
281 PHYE_LOSS_OF_SIGNAL);
282
Dan Williams6f231dd2011-07-02 22:56:22 -0700283 dev_dbg(&isci_host->pdev->dev,
284 "%s: isci_port = %p - Done\n", __func__, isci_port);
285}
286
287
288/**
Dan Williams6f231dd2011-07-02 22:56:22 -0700289 * isci_port_ready() - This function is called by the sci core when a link
290 * becomes ready.
291 * @isci_host: This parameter specifies the isci host object.
292 * @port: This parameter specifies the sci port with the active link.
293 *
294 */
Dan Williamse2f8db52011-05-10 02:28:46 -0700295static void isci_port_ready(struct isci_host *isci_host, struct isci_port *isci_port)
Dan Williams6f231dd2011-07-02 22:56:22 -0700296{
297 dev_dbg(&isci_host->pdev->dev,
298 "%s: isci_port = %p\n", __func__, isci_port);
299
Dan Williams6f231dd2011-07-02 22:56:22 -0700300 isci_port_change_state(isci_port, isci_ready);
301 return;
302}
303
304/**
305 * isci_port_not_ready() - This function is called by the sci core when a link
306 * is not ready. All remote devices on this link will be removed if they are
307 * in the stopping state.
308 * @isci_host: This parameter specifies the isci host object.
309 * @port: This parameter specifies the sci port with the active link.
310 *
311 */
Dan Williamse2f8db52011-05-10 02:28:46 -0700312static void isci_port_not_ready(struct isci_host *isci_host, struct isci_port *isci_port)
Dan Williams6f231dd2011-07-02 22:56:22 -0700313{
314 dev_dbg(&isci_host->pdev->dev,
315 "%s: isci_port = %p\n", __func__, isci_port);
316}
317
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700318static void isci_port_stop_complete(struct isci_host *ihost,
Dan Williamsffe191c2011-06-29 13:09:25 -0700319 struct isci_port *iport,
Dan Williamse2f8db52011-05-10 02:28:46 -0700320 enum sci_status completion_status)
321{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700322 dev_dbg(&ihost->pdev->dev, "Port stop complete\n");
Dan Williamse2f8db52011-05-10 02:28:46 -0700323}
324
Jeff Skirvin8e35a132011-10-27 15:05:32 -0700325
326static bool is_port_ready_state(enum sci_port_states state)
327{
328 switch (state) {
329 case SCI_PORT_READY:
330 case SCI_PORT_SUB_WAITING:
331 case SCI_PORT_SUB_OPERATIONAL:
332 case SCI_PORT_SUB_CONFIGURING:
333 return true;
334 default:
335 return false;
336 }
337}
338
339/* flag dummy rnc hanling when exiting a ready state */
340static void port_state_machine_change(struct isci_port *iport,
341 enum sci_port_states state)
342{
343 struct sci_base_state_machine *sm = &iport->sm;
344 enum sci_port_states old_state = sm->current_state_id;
345
346 if (is_port_ready_state(old_state) && !is_port_ready_state(state))
347 iport->ready_exit = true;
348
349 sci_change_state(sm, state);
350 iport->ready_exit = false;
351}
352
Dan Williams6f231dd2011-07-02 22:56:22 -0700353/**
354 * isci_port_hard_reset_complete() - This function is called by the sci core
355 * when the hard reset complete notification has been received.
356 * @port: This parameter specifies the sci port with the active link.
357 * @completion_status: This parameter specifies the core status for the reset
358 * process.
359 *
360 */
Dan Williamse2f8db52011-05-10 02:28:46 -0700361static void isci_port_hard_reset_complete(struct isci_port *isci_port,
362 enum sci_status completion_status)
Dan Williams6f231dd2011-07-02 22:56:22 -0700363{
364 dev_dbg(&isci_port->isci_host->pdev->dev,
365 "%s: isci_port = %p, completion_status=%x\n",
366 __func__, isci_port, completion_status);
367
368 /* Save the status of the hard reset from the port. */
369 isci_port->hard_reset_status = completion_status;
370
Jeff Skirvin8e35a132011-10-27 15:05:32 -0700371 if (completion_status != SCI_SUCCESS) {
372
373 /* The reset failed. The port state is now SCI_PORT_FAILED. */
374 if (isci_port->active_phy_mask == 0) {
375
376 /* Generate the link down now to the host, since it
377 * was intercepted by the hard reset state machine when
378 * it really happened.
379 */
380 isci_port_link_down(isci_port->isci_host,
381 &isci_port->isci_host->phys[
382 isci_port->last_active_phy],
383 isci_port);
384 }
385 /* Advance the port state so that link state changes will be
386 * noticed.
387 */
388 port_state_machine_change(isci_port, SCI_PORT_SUB_WAITING);
389
390 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700391 complete_all(&isci_port->hard_reset_complete);
392}
Dan Williams4393aa42011-03-31 13:10:44 -0700393
Dan Williamse2f8db52011-05-10 02:28:46 -0700394/* This method will return a true value if the specified phy can be assigned to
395 * this port The following is a list of phys for each port that are allowed: -
396 * Port 0 - 3 2 1 0 - Port 1 - 1 - Port 2 - 3 2 - Port 3 - 3 This method
397 * doesn't preclude all configurations. It merely ensures that a phy is part
398 * of the allowable set of phy identifiers for that port. For example, one
399 * could assign phy 3 to port 0 and no other phys. Please refer to
Dan Williams89a73012011-06-30 19:14:33 -0700400 * sci_port_is_phy_mask_valid() for information regarding whether the
Dan Williamse2f8db52011-05-10 02:28:46 -0700401 * phy_mask for a port can be supported. bool true if this is a valid phy
402 * assignment for the port false if this is not a valid phy assignment for the
403 * port
404 */
Dan Williams89a73012011-06-30 19:14:33 -0700405bool sci_port_is_valid_phy_assignment(struct isci_port *iport, u32 phy_index)
Dan Williamse2f8db52011-05-10 02:28:46 -0700406{
Dan Williams89a73012011-06-30 19:14:33 -0700407 struct isci_host *ihost = iport->owning_controller;
408 struct sci_user_parameters *user = &ihost->user_parameters;
409
Dan Williamse2f8db52011-05-10 02:28:46 -0700410 /* Initialize to invalid value. */
411 u32 existing_phy_index = SCI_MAX_PHYS;
412 u32 index;
413
Dan Williams89a73012011-06-30 19:14:33 -0700414 if ((iport->physical_port_index == 1) && (phy_index != 1))
Dan Williamse2f8db52011-05-10 02:28:46 -0700415 return false;
Dan Williamse2f8db52011-05-10 02:28:46 -0700416
Dan Williams89a73012011-06-30 19:14:33 -0700417 if (iport->physical_port_index == 3 && phy_index != 3)
Dan Williamse2f8db52011-05-10 02:28:46 -0700418 return false;
Dan Williamse2f8db52011-05-10 02:28:46 -0700419
Dan Williams89a73012011-06-30 19:14:33 -0700420 if (iport->physical_port_index == 2 &&
421 (phy_index == 0 || phy_index == 1))
Dan Williamse2f8db52011-05-10 02:28:46 -0700422 return false;
Dan Williamse2f8db52011-05-10 02:28:46 -0700423
Dan Williams89a73012011-06-30 19:14:33 -0700424 for (index = 0; index < SCI_MAX_PHYS; index++)
425 if (iport->phy_table[index] && index != phy_index)
Dan Williamse2f8db52011-05-10 02:28:46 -0700426 existing_phy_index = index;
Dan Williamse2f8db52011-05-10 02:28:46 -0700427
Dan Williams89a73012011-06-30 19:14:33 -0700428 /* Ensure that all of the phys in the port are capable of
429 * operating at the same maximum link rate.
430 */
431 if (existing_phy_index < SCI_MAX_PHYS &&
432 user->phys[phy_index].max_speed_generation !=
433 user->phys[existing_phy_index].max_speed_generation)
Dan Williamse2f8db52011-05-10 02:28:46 -0700434 return false;
435
436 return true;
437}
438
439/**
440 *
441 * @sci_port: This is the port object for which to determine if the phy mask
442 * can be supported.
443 *
444 * This method will return a true value if the port's phy mask can be supported
445 * by the SCU. The following is a list of valid PHY mask configurations for
446 * each port: - Port 0 - [[3 2] 1] 0 - Port 1 - [1] - Port 2 - [[3] 2]
447 * - Port 3 - [3] This method returns a boolean indication specifying if the
448 * phy mask can be supported. true if this is a valid phy assignment for the
449 * port false if this is not a valid phy assignment for the port
450 */
Dan Williams89a73012011-06-30 19:14:33 -0700451static bool sci_port_is_phy_mask_valid(
Dan Williamsffe191c2011-06-29 13:09:25 -0700452 struct isci_port *iport,
Dan Williamse2f8db52011-05-10 02:28:46 -0700453 u32 phy_mask)
454{
Dan Williamsffe191c2011-06-29 13:09:25 -0700455 if (iport->physical_port_index == 0) {
Dan Williamse2f8db52011-05-10 02:28:46 -0700456 if (((phy_mask & 0x0F) == 0x0F)
457 || ((phy_mask & 0x03) == 0x03)
458 || ((phy_mask & 0x01) == 0x01)
459 || (phy_mask == 0))
460 return true;
Dan Williamsffe191c2011-06-29 13:09:25 -0700461 } else if (iport->physical_port_index == 1) {
Dan Williamse2f8db52011-05-10 02:28:46 -0700462 if (((phy_mask & 0x02) == 0x02)
463 || (phy_mask == 0))
464 return true;
Dan Williamsffe191c2011-06-29 13:09:25 -0700465 } else if (iport->physical_port_index == 2) {
Dan Williamse2f8db52011-05-10 02:28:46 -0700466 if (((phy_mask & 0x0C) == 0x0C)
467 || ((phy_mask & 0x04) == 0x04)
468 || (phy_mask == 0))
469 return true;
Dan Williamsffe191c2011-06-29 13:09:25 -0700470 } else if (iport->physical_port_index == 3) {
Dan Williamse2f8db52011-05-10 02:28:46 -0700471 if (((phy_mask & 0x08) == 0x08)
472 || (phy_mask == 0))
473 return true;
474 }
475
476 return false;
477}
478
Dan Williams85280952011-06-28 15:05:53 -0700479/*
Dan Williamse2f8db52011-05-10 02:28:46 -0700480 * This method retrieves a currently active (i.e. connected) phy contained in
481 * the port. Currently, the lowest order phy that is connected is returned.
482 * This method returns a pointer to a SCIS_SDS_PHY object. NULL This value is
483 * returned if there are no currently active (i.e. connected to a remote end
Dan Williams89a73012011-06-30 19:14:33 -0700484 * point) phys contained in the port. All other values specify a struct sci_phy
Dan Williamse2f8db52011-05-10 02:28:46 -0700485 * object that is active in the port.
486 */
Dan Williams89a73012011-06-30 19:14:33 -0700487static struct isci_phy *sci_port_get_a_connected_phy(struct isci_port *iport)
Dan Williams85280952011-06-28 15:05:53 -0700488{
Dan Williamse2f8db52011-05-10 02:28:46 -0700489 u32 index;
Dan Williams85280952011-06-28 15:05:53 -0700490 struct isci_phy *iphy;
Dan Williamse2f8db52011-05-10 02:28:46 -0700491
492 for (index = 0; index < SCI_MAX_PHYS; index++) {
Dan Williams85280952011-06-28 15:05:53 -0700493 /* Ensure that the phy is both part of the port and currently
494 * connected to the remote end-point.
495 */
Dan Williamsffe191c2011-06-29 13:09:25 -0700496 iphy = iport->phy_table[index];
Dan Williams89a73012011-06-30 19:14:33 -0700497 if (iphy && sci_port_active_phy(iport, iphy))
Dan Williams85280952011-06-28 15:05:53 -0700498 return iphy;
Dan Williamse2f8db52011-05-10 02:28:46 -0700499 }
500
501 return NULL;
502}
503
Dan Williams89a73012011-06-30 19:14:33 -0700504static enum sci_status sci_port_set_phy(struct isci_port *iport, struct isci_phy *iphy)
Dan Williamse2f8db52011-05-10 02:28:46 -0700505{
Dan Williams85280952011-06-28 15:05:53 -0700506 /* Check to see if we can add this phy to a port
Dan Williamse2f8db52011-05-10 02:28:46 -0700507 * that means that the phy is not part of a port and that the port does
Dan Williams85280952011-06-28 15:05:53 -0700508 * not already have a phy assinged to the phy index.
509 */
Dan Williamsffe191c2011-06-29 13:09:25 -0700510 if (!iport->phy_table[iphy->phy_index] &&
Dan Williams85280952011-06-28 15:05:53 -0700511 !phy_get_non_dummy_port(iphy) &&
Dan Williams89a73012011-06-30 19:14:33 -0700512 sci_port_is_valid_phy_assignment(iport, iphy->phy_index)) {
Dan Williams85280952011-06-28 15:05:53 -0700513 /* Phy is being added in the stopped state so we are in MPC mode
514 * make logical port index = physical port index
515 */
Dan Williamsffe191c2011-06-29 13:09:25 -0700516 iport->logical_port_index = iport->physical_port_index;
517 iport->phy_table[iphy->phy_index] = iphy;
Dan Williams89a73012011-06-30 19:14:33 -0700518 sci_phy_set_port(iphy, iport);
Dan Williamse2f8db52011-05-10 02:28:46 -0700519
520 return SCI_SUCCESS;
521 }
522
523 return SCI_FAILURE;
524}
525
Dan Williams89a73012011-06-30 19:14:33 -0700526static enum sci_status sci_port_clear_phy(struct isci_port *iport, struct isci_phy *iphy)
Dan Williamse2f8db52011-05-10 02:28:46 -0700527{
528 /* Make sure that this phy is part of this port */
Dan Williamsffe191c2011-06-29 13:09:25 -0700529 if (iport->phy_table[iphy->phy_index] == iphy &&
530 phy_get_non_dummy_port(iphy) == iport) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700531 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -0700532
533 /* Yep it is assigned to this port so remove it */
Dan Williams89a73012011-06-30 19:14:33 -0700534 sci_phy_set_port(iphy, &ihost->ports[SCI_MAX_PORTS]);
Dan Williamsffe191c2011-06-29 13:09:25 -0700535 iport->phy_table[iphy->phy_index] = NULL;
Dan Williamse2f8db52011-05-10 02:28:46 -0700536 return SCI_SUCCESS;
537 }
538
539 return SCI_FAILURE;
540}
541
Dan Williams89a73012011-06-30 19:14:33 -0700542void sci_port_get_sas_address(struct isci_port *iport, struct sci_sas_address *sas)
Dan Williamse2f8db52011-05-10 02:28:46 -0700543{
544 u32 index;
545
Dan Williams89a73012011-06-30 19:14:33 -0700546 sas->high = 0;
547 sas->low = 0;
548 for (index = 0; index < SCI_MAX_PHYS; index++)
549 if (iport->phy_table[index])
550 sci_phy_get_sas_address(iport->phy_table[index], sas);
Dan Williamse2f8db52011-05-10 02:28:46 -0700551}
552
Dan Williams89a73012011-06-30 19:14:33 -0700553void sci_port_get_attached_sas_address(struct isci_port *iport, struct sci_sas_address *sas)
Dan Williamse2f8db52011-05-10 02:28:46 -0700554{
Dan Williams85280952011-06-28 15:05:53 -0700555 struct isci_phy *iphy;
Dan Williamse2f8db52011-05-10 02:28:46 -0700556
557 /*
558 * Ensure that the phy is both part of the port and currently
559 * connected to the remote end-point.
560 */
Dan Williams89a73012011-06-30 19:14:33 -0700561 iphy = sci_port_get_a_connected_phy(iport);
Dan Williams85280952011-06-28 15:05:53 -0700562 if (iphy) {
563 if (iphy->protocol != SCIC_SDS_PHY_PROTOCOL_SATA) {
Dan Williams89a73012011-06-30 19:14:33 -0700564 sci_phy_get_attached_sas_address(iphy, sas);
Dan Williamse2f8db52011-05-10 02:28:46 -0700565 } else {
Dan Williams89a73012011-06-30 19:14:33 -0700566 sci_phy_get_sas_address(iphy, sas);
567 sas->low += iphy->phy_index;
Dan Williamse2f8db52011-05-10 02:28:46 -0700568 }
569 } else {
Dan Williams89a73012011-06-30 19:14:33 -0700570 sas->high = 0;
571 sas->low = 0;
Dan Williamse2f8db52011-05-10 02:28:46 -0700572 }
573}
574
575/**
Dan Williams89a73012011-06-30 19:14:33 -0700576 * sci_port_construct_dummy_rnc() - create dummy rnc for si workaround
Dan Williamse2f8db52011-05-10 02:28:46 -0700577 *
578 * @sci_port: logical port on which we need to create the remote node context
579 * @rni: remote node index for this remote node context.
580 *
581 * This routine will construct a dummy remote node context data structure
582 * This structure will be posted to the hardware to work around a scheduler
583 * error in the hardware.
584 */
Dan Williams89a73012011-06-30 19:14:33 -0700585static void sci_port_construct_dummy_rnc(struct isci_port *iport, u16 rni)
Dan Williamse2f8db52011-05-10 02:28:46 -0700586{
587 union scu_remote_node_context *rnc;
588
Dan Williamsffe191c2011-06-29 13:09:25 -0700589 rnc = &iport->owning_controller->remote_node_context_table[rni];
Dan Williamse2f8db52011-05-10 02:28:46 -0700590
591 memset(rnc, 0, sizeof(union scu_remote_node_context));
592
593 rnc->ssp.remote_sas_address_hi = 0;
594 rnc->ssp.remote_sas_address_lo = 0;
595
596 rnc->ssp.remote_node_index = rni;
597 rnc->ssp.remote_node_port_width = 1;
Dan Williamsffe191c2011-06-29 13:09:25 -0700598 rnc->ssp.logical_port_index = iport->physical_port_index;
Dan Williamse2f8db52011-05-10 02:28:46 -0700599
600 rnc->ssp.nexus_loss_timer_enable = false;
601 rnc->ssp.check_bit = false;
602 rnc->ssp.is_valid = true;
603 rnc->ssp.is_remote_node_context = true;
604 rnc->ssp.function_number = 0;
605 rnc->ssp.arbitration_wait_time = 0;
606}
607
Dan Williamsdd047c82011-06-09 11:06:58 -0700608/*
609 * construct a dummy task context data structure. This
Dan Williamse2f8db52011-05-10 02:28:46 -0700610 * structure will be posted to the hardwre to work around a scheduler error
611 * in the hardware.
Dan Williamse2f8db52011-05-10 02:28:46 -0700612 */
Dan Williams89a73012011-06-30 19:14:33 -0700613static void sci_port_construct_dummy_task(struct isci_port *iport, u16 tag)
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 struct scu_task_context *task_context;
617
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700618 task_context = &ihost->task_context_table[ISCI_TAG_TCI(tag)];
Dan Williamse2f8db52011-05-10 02:28:46 -0700619 memset(task_context, 0, sizeof(struct scu_task_context));
620
Dan Williamse2f8db52011-05-10 02:28:46 -0700621 task_context->initiator_request = 1;
622 task_context->connection_rate = 1;
Dan Williamsffe191c2011-06-29 13:09:25 -0700623 task_context->logical_port_index = iport->physical_port_index;
Dan Williamse2f8db52011-05-10 02:28:46 -0700624 task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SSP;
Dan Williamsdd047c82011-06-09 11:06:58 -0700625 task_context->task_index = ISCI_TAG_TCI(tag);
Dan Williamse2f8db52011-05-10 02:28:46 -0700626 task_context->valid = SCU_TASK_CONTEXT_VALID;
627 task_context->context_type = SCU_TASK_CONTEXT_TYPE;
Dan Williamsffe191c2011-06-29 13:09:25 -0700628 task_context->remote_node_index = iport->reserved_rni;
Dan Williamse2f8db52011-05-10 02:28:46 -0700629 task_context->do_not_dma_ssp_good_response = 1;
Dan Williamse2f8db52011-05-10 02:28:46 -0700630 task_context->task_phase = 0x01;
631}
632
Dan Williams89a73012011-06-30 19:14:33 -0700633static void sci_port_destroy_dummy_resources(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -0700634{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700635 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -0700636
Dan Williamsffe191c2011-06-29 13:09:25 -0700637 if (iport->reserved_tag != SCI_CONTROLLER_INVALID_IO_TAG)
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700638 isci_free_tag(ihost, iport->reserved_tag);
Dan Williamse2f8db52011-05-10 02:28:46 -0700639
Dan Williamsffe191c2011-06-29 13:09:25 -0700640 if (iport->reserved_rni != SCU_DUMMY_INDEX)
Dan Williams89a73012011-06-30 19:14:33 -0700641 sci_remote_node_table_release_remote_node_index(&ihost->available_remote_nodes,
Dan Williamsffe191c2011-06-29 13:09:25 -0700642 1, iport->reserved_rni);
Dan Williamse2f8db52011-05-10 02:28:46 -0700643
Dan Williamsffe191c2011-06-29 13:09:25 -0700644 iport->reserved_rni = SCU_DUMMY_INDEX;
645 iport->reserved_tag = SCI_CONTROLLER_INVALID_IO_TAG;
Dan Williamse2f8db52011-05-10 02:28:46 -0700646}
647
Dan Williams89a73012011-06-30 19:14:33 -0700648void sci_port_setup_transports(struct isci_port *iport, u32 device_id)
Dan Williamse2f8db52011-05-10 02:28:46 -0700649{
650 u8 index;
651
652 for (index = 0; index < SCI_MAX_PHYS; index++) {
Dan Williamsffe191c2011-06-29 13:09:25 -0700653 if (iport->active_phy_mask & (1 << index))
Dan Williams89a73012011-06-30 19:14:33 -0700654 sci_phy_setup_transport(iport->phy_table[index], device_id);
Dan Williamse2f8db52011-05-10 02:28:46 -0700655 }
656}
657
Marcin Tomczak05b080f2012-01-04 01:33:41 -0800658static void sci_port_resume_phy(struct isci_port *iport, struct isci_phy *iphy)
659{
660 sci_phy_resume(iphy);
661 iport->enabled_phy_mask |= 1 << iphy->phy_index;
662}
663
664static void sci_port_activate_phy(struct isci_port *iport,
665 struct isci_phy *iphy,
666 u8 flags)
Dan Williamse2f8db52011-05-10 02:28:46 -0700667{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700668 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -0700669
Marcin Tomczak05b080f2012-01-04 01:33:41 -0800670 if (iphy->protocol != SCIC_SDS_PHY_PROTOCOL_SATA && (flags & PF_RESUME))
Dan Williams89a73012011-06-30 19:14:33 -0700671 sci_phy_resume(iphy);
Dan Williamse2f8db52011-05-10 02:28:46 -0700672
Dan Williamsffe191c2011-06-29 13:09:25 -0700673 iport->active_phy_mask |= 1 << iphy->phy_index;
Dan Williamse2f8db52011-05-10 02:28:46 -0700674
Dan Williams89a73012011-06-30 19:14:33 -0700675 sci_controller_clear_invalid_phy(ihost, iphy);
Dan Williamse2f8db52011-05-10 02:28:46 -0700676
Marcin Tomczak05b080f2012-01-04 01:33:41 -0800677 if (flags & PF_NOTIFY)
Dan Williamsffe191c2011-06-29 13:09:25 -0700678 isci_port_link_up(ihost, iport, iphy);
Dan Williamse2f8db52011-05-10 02:28:46 -0700679}
680
Dan Williams89a73012011-06-30 19:14:33 -0700681void sci_port_deactivate_phy(struct isci_port *iport, struct isci_phy *iphy,
682 bool do_notify_user)
Dan Williamse2f8db52011-05-10 02:28:46 -0700683{
Dan Williams34a99152011-07-01 02:25:15 -0700684 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -0700685
Dan Williamsffe191c2011-06-29 13:09:25 -0700686 iport->active_phy_mask &= ~(1 << iphy->phy_index);
Marcin Tomczak05b080f2012-01-04 01:33:41 -0800687 iport->enabled_phy_mask &= ~(1 << iphy->phy_index);
Jeff Skirvin8e35a132011-10-27 15:05:32 -0700688 if (!iport->active_phy_mask)
689 iport->last_active_phy = iphy->phy_index;
Dan Williamse2f8db52011-05-10 02:28:46 -0700690
Dan Williams85280952011-06-28 15:05:53 -0700691 iphy->max_negotiated_speed = SAS_LINK_RATE_UNKNOWN;
Dan Williamse2f8db52011-05-10 02:28:46 -0700692
Marcin Tomczakd4ec1cf2012-01-04 01:33:15 -0800693 /* Re-assign the phy back to the LP as if it were a narrow port for APC
694 * mode. For MPC mode, the phy will remain in the port.
695 */
696 if (iport->owning_controller->oem_parameters.controller.mode_type ==
697 SCIC_PORT_AUTOMATIC_CONFIGURATION_MODE)
698 writel(iphy->phy_index,
699 &iport->port_pe_configuration_register[iphy->phy_index]);
Dan Williamse2f8db52011-05-10 02:28:46 -0700700
701 if (do_notify_user == true)
702 isci_port_link_down(ihost, iphy, iport);
703}
704
Dan Williams89a73012011-06-30 19:14:33 -0700705static void sci_port_invalid_link_up(struct isci_port *iport, struct isci_phy *iphy)
Dan Williamse2f8db52011-05-10 02:28:46 -0700706{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700707 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -0700708
709 /*
710 * Check to see if we have alreay reported this link as bad and if
711 * not go ahead and tell the SCI_USER that we have discovered an
712 * invalid link.
713 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700714 if ((ihost->invalid_phy_mask & (1 << iphy->phy_index)) == 0) {
Dan Williams34a99152011-07-01 02:25:15 -0700715 ihost->invalid_phy_mask |= 1 << iphy->phy_index;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700716 dev_warn(&ihost->pdev->dev, "Invalid link up!\n");
Dan Williamse2f8db52011-05-10 02:28:46 -0700717 }
718}
719
720/**
Dan Williams89a73012011-06-30 19:14:33 -0700721 * sci_port_general_link_up_handler - phy can be assigned to port?
722 * @sci_port: sci_port object for which has a phy that has gone link up.
Dan Williams85280952011-06-28 15:05:53 -0700723 * @sci_phy: This is the struct isci_phy object that has gone link up.
Marcin Tomczak05b080f2012-01-04 01:33:41 -0800724 * @flags: PF_RESUME, PF_NOTIFY to sci_port_activate_phy
Dan Williamse2f8db52011-05-10 02:28:46 -0700725 *
Marcin Tomczak05b080f2012-01-04 01:33:41 -0800726 * Determine if this phy can be assigned to this port . If the phy is
727 * not a valid PHY for this port then the function will notify the user.
728 * A PHY can only be part of a port if it's attached SAS ADDRESS is the
729 * same as all other PHYs in the same port.
Dan Williamse2f8db52011-05-10 02:28:46 -0700730 */
Dan Williams89a73012011-06-30 19:14:33 -0700731static void sci_port_general_link_up_handler(struct isci_port *iport,
Marcin Tomczak05b080f2012-01-04 01:33:41 -0800732 struct isci_phy *iphy,
733 u8 flags)
Dan Williamse2f8db52011-05-10 02:28:46 -0700734{
735 struct sci_sas_address port_sas_address;
736 struct sci_sas_address phy_sas_address;
737
Dan Williams89a73012011-06-30 19:14:33 -0700738 sci_port_get_attached_sas_address(iport, &port_sas_address);
739 sci_phy_get_attached_sas_address(iphy, &phy_sas_address);
Dan Williamse2f8db52011-05-10 02:28:46 -0700740
741 /* If the SAS address of the new phy matches the SAS address of
742 * other phys in the port OR this is the first phy in the port,
743 * then activate the phy and allow it to be used for operations
744 * in this port.
745 */
746 if ((phy_sas_address.high == port_sas_address.high &&
747 phy_sas_address.low == port_sas_address.low) ||
Dan Williamsffe191c2011-06-29 13:09:25 -0700748 iport->active_phy_mask == 0) {
749 struct sci_base_state_machine *sm = &iport->sm;
Dan Williamse2f8db52011-05-10 02:28:46 -0700750
Marcin Tomczak05b080f2012-01-04 01:33:41 -0800751 sci_port_activate_phy(iport, iphy, flags);
Edmund Nadolskie3013702011-06-02 00:10:43 +0000752 if (sm->current_state_id == SCI_PORT_RESETTING)
Dan Williamsffe191c2011-06-29 13:09:25 -0700753 port_state_machine_change(iport, SCI_PORT_READY);
Dan Williamse2f8db52011-05-10 02:28:46 -0700754 } else
Dan Williams89a73012011-06-30 19:14:33 -0700755 sci_port_invalid_link_up(iport, iphy);
Dan Williamse2f8db52011-05-10 02:28:46 -0700756}
757
758
759
760/**
761 * This method returns false if the port only has a single phy object assigned.
762 * If there are no phys or more than one phy then the method will return
763 * true.
764 * @sci_port: The port for which the wide port condition is to be checked.
765 *
766 * bool true Is returned if this is a wide ported port. false Is returned if
767 * this is a narrow port.
768 */
Dan Williams89a73012011-06-30 19:14:33 -0700769static bool sci_port_is_wide(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -0700770{
771 u32 index;
772 u32 phy_count = 0;
773
774 for (index = 0; index < SCI_MAX_PHYS; index++) {
Dan Williamsffe191c2011-06-29 13:09:25 -0700775 if (iport->phy_table[index] != NULL) {
Dan Williamse2f8db52011-05-10 02:28:46 -0700776 phy_count++;
777 }
778 }
779
780 return phy_count != 1;
781}
782
783/**
784 * This method is called by the PHY object when the link is detected. if the
785 * port wants the PHY to continue on to the link up state then the port
786 * layer must return true. If the port object returns false the phy object
787 * must halt its attempt to go link up.
788 * @sci_port: The port associated with the phy object.
789 * @sci_phy: The phy object that is trying to go link up.
790 *
791 * true if the phy object can continue to the link up condition. true Is
792 * returned if this phy can continue to the ready state. false Is returned if
793 * can not continue on to the ready state. This notification is in place for
794 * wide ports and direct attached phys. Since there are no wide ported SATA
795 * devices this could become an invalid port configuration.
796 */
Dan Williams89a73012011-06-30 19:14:33 -0700797bool sci_port_link_detected(
Dan Williamsffe191c2011-06-29 13:09:25 -0700798 struct isci_port *iport,
Dan Williams85280952011-06-28 15:05:53 -0700799 struct isci_phy *iphy)
Dan Williamse2f8db52011-05-10 02:28:46 -0700800{
Dan Williamsffe191c2011-06-29 13:09:25 -0700801 if ((iport->logical_port_index != SCIC_SDS_DUMMY_PORT) &&
Marcin Tomczak05b080f2012-01-04 01:33:41 -0800802 (iphy->protocol == SCIC_SDS_PHY_PROTOCOL_SATA)) {
803 if (sci_port_is_wide(iport)) {
804 sci_port_invalid_link_up(iport, iphy);
805 return false;
806 } else {
807 struct isci_host *ihost = iport->owning_controller;
808 struct isci_port *dst_port = &(ihost->ports[iphy->phy_index]);
809 writel(iphy->phy_index,
810 &dst_port->port_pe_configuration_register[iphy->phy_index]);
811 }
Dan Williamse2f8db52011-05-10 02:28:46 -0700812 }
813
814 return true;
815}
816
Edmund Nadolski5553ba22011-05-19 11:59:10 +0000817static void port_timeout(unsigned long data)
Dan Williamse2f8db52011-05-10 02:28:46 -0700818{
Edmund Nadolski5553ba22011-05-19 11:59:10 +0000819 struct sci_timer *tmr = (struct sci_timer *)data;
Dan Williamsffe191c2011-06-29 13:09:25 -0700820 struct isci_port *iport = container_of(tmr, typeof(*iport), timer);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700821 struct isci_host *ihost = iport->owning_controller;
Edmund Nadolski5553ba22011-05-19 11:59:10 +0000822 unsigned long flags;
Dan Williamse2f8db52011-05-10 02:28:46 -0700823 u32 current_state;
824
Edmund Nadolski5553ba22011-05-19 11:59:10 +0000825 spin_lock_irqsave(&ihost->scic_lock, flags);
826
827 if (tmr->cancel)
828 goto done;
829
Dan Williamsffe191c2011-06-29 13:09:25 -0700830 current_state = iport->sm.current_state_id;
Dan Williamse2f8db52011-05-10 02:28:46 -0700831
Edmund Nadolskie3013702011-06-02 00:10:43 +0000832 if (current_state == SCI_PORT_RESETTING) {
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000833 /* if the port is still in the resetting state then the timeout
834 * fired before the reset completed.
Dan Williamse2f8db52011-05-10 02:28:46 -0700835 */
Dan Williamsffe191c2011-06-29 13:09:25 -0700836 port_state_machine_change(iport, SCI_PORT_FAILED);
Edmund Nadolskie3013702011-06-02 00:10:43 +0000837 } else if (current_state == SCI_PORT_STOPPED) {
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000838 /* if the port is stopped then the start request failed In this
839 * case stay in the stopped state.
Dan Williamse2f8db52011-05-10 02:28:46 -0700840 */
Dan Williamsffe191c2011-06-29 13:09:25 -0700841 dev_err(sciport_to_dev(iport),
Dan Williamse2f8db52011-05-10 02:28:46 -0700842 "%s: SCIC Port 0x%p failed to stop before tiemout.\n",
843 __func__,
Dan Williamsffe191c2011-06-29 13:09:25 -0700844 iport);
Edmund Nadolskie3013702011-06-02 00:10:43 +0000845 } else if (current_state == SCI_PORT_STOPPING) {
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000846 /* if the port is still stopping then the stop has not completed */
Dan Williamsffe191c2011-06-29 13:09:25 -0700847 isci_port_stop_complete(iport->owning_controller,
848 iport,
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000849 SCI_FAILURE_TIMEOUT);
Dan Williamse2f8db52011-05-10 02:28:46 -0700850 } else {
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000851 /* The port is in the ready state and we have a timer
Dan Williamse2f8db52011-05-10 02:28:46 -0700852 * reporting a timeout this should not happen.
853 */
Dan Williamsffe191c2011-06-29 13:09:25 -0700854 dev_err(sciport_to_dev(iport),
Dan Williamse2f8db52011-05-10 02:28:46 -0700855 "%s: SCIC Port 0x%p is processing a timeout operation "
Dan Williamsffe191c2011-06-29 13:09:25 -0700856 "in state %d.\n", __func__, iport, current_state);
Dan Williamse2f8db52011-05-10 02:28:46 -0700857 }
Edmund Nadolski5553ba22011-05-19 11:59:10 +0000858
859done:
860 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williamse2f8db52011-05-10 02:28:46 -0700861}
862
863/* --------------------------------------------------------------------------- */
864
865/**
866 * This function updates the hardwares VIIT entry for this port.
867 *
868 *
869 */
Dan Williams89a73012011-06-30 19:14:33 -0700870static void sci_port_update_viit_entry(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -0700871{
872 struct sci_sas_address sas_address;
873
Dan Williams89a73012011-06-30 19:14:33 -0700874 sci_port_get_sas_address(iport, &sas_address);
Dan Williamse2f8db52011-05-10 02:28:46 -0700875
876 writel(sas_address.high,
Dan Williamsffe191c2011-06-29 13:09:25 -0700877 &iport->viit_registers->initiator_sas_address_hi);
Dan Williamse2f8db52011-05-10 02:28:46 -0700878 writel(sas_address.low,
Dan Williamsffe191c2011-06-29 13:09:25 -0700879 &iport->viit_registers->initiator_sas_address_lo);
Dan Williamse2f8db52011-05-10 02:28:46 -0700880
881 /* This value get cleared just in case its not already cleared */
Dan Williamsffe191c2011-06-29 13:09:25 -0700882 writel(0, &iport->viit_registers->reserved);
Dan Williamse2f8db52011-05-10 02:28:46 -0700883
884 /* We are required to update the status register last */
885 writel(SCU_VIIT_ENTRY_ID_VIIT |
886 SCU_VIIT_IPPT_INITIATOR |
Dan Williamsffe191c2011-06-29 13:09:25 -0700887 ((1 << iport->physical_port_index) << SCU_VIIT_ENTRY_LPVIE_SHIFT) |
Dan Williamse2f8db52011-05-10 02:28:46 -0700888 SCU_VIIT_STATUS_ALL_VALID,
Dan Williamsffe191c2011-06-29 13:09:25 -0700889 &iport->viit_registers->status);
Dan Williamse2f8db52011-05-10 02:28:46 -0700890}
891
Dan Williams89a73012011-06-30 19:14:33 -0700892enum sas_linkrate sci_port_get_max_allowed_speed(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -0700893{
894 u16 index;
Dan Williams85280952011-06-28 15:05:53 -0700895 struct isci_phy *iphy;
Dan Williamse2f8db52011-05-10 02:28:46 -0700896 enum sas_linkrate max_allowed_speed = SAS_LINK_RATE_6_0_GBPS;
Dan Williamse2f8db52011-05-10 02:28:46 -0700897
898 /*
899 * Loop through all of the phys in this port and find the phy with the
900 * lowest maximum link rate. */
901 for (index = 0; index < SCI_MAX_PHYS; index++) {
Dan Williamsffe191c2011-06-29 13:09:25 -0700902 iphy = iport->phy_table[index];
Dan Williams89a73012011-06-30 19:14:33 -0700903 if (iphy && sci_port_active_phy(iport, iphy) &&
Dan Williams85280952011-06-28 15:05:53 -0700904 iphy->max_negotiated_speed < max_allowed_speed)
905 max_allowed_speed = iphy->max_negotiated_speed;
Dan Williamse2f8db52011-05-10 02:28:46 -0700906 }
907
908 return max_allowed_speed;
909}
910
Dan Williams89a73012011-06-30 19:14:33 -0700911static void sci_port_suspend_port_task_scheduler(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -0700912{
913 u32 pts_control_value;
914
Dan Williamsffe191c2011-06-29 13:09:25 -0700915 pts_control_value = readl(&iport->port_task_scheduler_registers->control);
Dan Williamse2f8db52011-05-10 02:28:46 -0700916 pts_control_value |= SCU_PTSxCR_GEN_BIT(SUSPEND);
Dan Williamsffe191c2011-06-29 13:09:25 -0700917 writel(pts_control_value, &iport->port_task_scheduler_registers->control);
Dan Williamse2f8db52011-05-10 02:28:46 -0700918}
919
920/**
Dan Williams89a73012011-06-30 19:14:33 -0700921 * sci_port_post_dummy_request() - post dummy/workaround request
Dan Williamse2f8db52011-05-10 02:28:46 -0700922 * @sci_port: port to post task
923 *
924 * Prevent the hardware scheduler from posting new requests to the front
925 * of the scheduler queue causing a starvation problem for currently
926 * ongoing requests.
927 *
928 */
Dan Williams89a73012011-06-30 19:14:33 -0700929static void sci_port_post_dummy_request(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -0700930{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700931 struct isci_host *ihost = iport->owning_controller;
Dan Williamsffe191c2011-06-29 13:09:25 -0700932 u16 tag = iport->reserved_tag;
Dan Williams312e0c22011-06-28 13:47:09 -0700933 struct scu_task_context *tc;
934 u32 command;
Dan Williamse2f8db52011-05-10 02:28:46 -0700935
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700936 tc = &ihost->task_context_table[ISCI_TAG_TCI(tag)];
Dan Williams312e0c22011-06-28 13:47:09 -0700937 tc->abort = 0;
Dan Williamse2f8db52011-05-10 02:28:46 -0700938
939 command = SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
Dan Williamsffe191c2011-06-29 13:09:25 -0700940 iport->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT |
Dan Williams312e0c22011-06-28 13:47:09 -0700941 ISCI_TAG_TCI(tag);
Dan Williamse2f8db52011-05-10 02:28:46 -0700942
Dan Williams89a73012011-06-30 19:14:33 -0700943 sci_controller_post_request(ihost, command);
Dan Williamse2f8db52011-05-10 02:28:46 -0700944}
945
946/**
947 * This routine will abort the dummy request. This will alow the hardware to
948 * power down parts of the silicon to save power.
949 *
950 * @sci_port: The port on which the task must be aborted.
951 *
952 */
Dan Williams89a73012011-06-30 19:14:33 -0700953static void sci_port_abort_dummy_request(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -0700954{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700955 struct isci_host *ihost = iport->owning_controller;
Dan Williamsffe191c2011-06-29 13:09:25 -0700956 u16 tag = iport->reserved_tag;
Dan Williamse2f8db52011-05-10 02:28:46 -0700957 struct scu_task_context *tc;
958 u32 command;
959
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700960 tc = &ihost->task_context_table[ISCI_TAG_TCI(tag)];
Dan Williamse2f8db52011-05-10 02:28:46 -0700961 tc->abort = 1;
962
963 command = SCU_CONTEXT_COMMAND_REQUEST_POST_TC_ABORT |
Dan Williamsffe191c2011-06-29 13:09:25 -0700964 iport->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT |
Dan Williams312e0c22011-06-28 13:47:09 -0700965 ISCI_TAG_TCI(tag);
Dan Williamse2f8db52011-05-10 02:28:46 -0700966
Dan Williams89a73012011-06-30 19:14:33 -0700967 sci_controller_post_request(ihost, command);
Dan Williamse2f8db52011-05-10 02:28:46 -0700968}
969
970/**
971 *
Dan Williamsffe191c2011-06-29 13:09:25 -0700972 * @sci_port: This is the struct isci_port object to resume.
Dan Williamse2f8db52011-05-10 02:28:46 -0700973 *
974 * This method will resume the port task scheduler for this port object. none
975 */
976static void
Dan Williams89a73012011-06-30 19:14:33 -0700977sci_port_resume_port_task_scheduler(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -0700978{
979 u32 pts_control_value;
980
Dan Williamsffe191c2011-06-29 13:09:25 -0700981 pts_control_value = readl(&iport->port_task_scheduler_registers->control);
Dan Williamse2f8db52011-05-10 02:28:46 -0700982 pts_control_value &= ~SCU_PTSxCR_GEN_BIT(SUSPEND);
Dan Williamsffe191c2011-06-29 13:09:25 -0700983 writel(pts_control_value, &iport->port_task_scheduler_registers->control);
Dan Williamse2f8db52011-05-10 02:28:46 -0700984}
985
Dan Williams89a73012011-06-30 19:14:33 -0700986static void sci_port_ready_substate_waiting_enter(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -0700987{
Dan Williamsffe191c2011-06-29 13:09:25 -0700988 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamse2f8db52011-05-10 02:28:46 -0700989
Dan Williams89a73012011-06-30 19:14:33 -0700990 sci_port_suspend_port_task_scheduler(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -0700991
Dan Williamsffe191c2011-06-29 13:09:25 -0700992 iport->not_ready_reason = SCIC_PORT_NOT_READY_NO_ACTIVE_PHYS;
Dan Williamse2f8db52011-05-10 02:28:46 -0700993
Dan Williamsffe191c2011-06-29 13:09:25 -0700994 if (iport->active_phy_mask != 0) {
Dan Williamse2f8db52011-05-10 02:28:46 -0700995 /* At least one of the phys on the port is ready */
Dan Williamsffe191c2011-06-29 13:09:25 -0700996 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +0000997 SCI_PORT_SUB_OPERATIONAL);
Dan Williamse2f8db52011-05-10 02:28:46 -0700998 }
999}
1000
Marcin Tomczak05b080f2012-01-04 01:33:41 -08001001static void scic_sds_port_ready_substate_waiting_exit(
1002 struct sci_base_state_machine *sm)
1003{
1004 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
1005 sci_port_resume_port_task_scheduler(iport);
1006}
1007
Dan Williams89a73012011-06-30 19:14:33 -07001008static void sci_port_ready_substate_operational_enter(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001009{
1010 u32 index;
Dan Williamsffe191c2011-06-29 13:09:25 -07001011 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001012 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -07001013
Dan Williamse2f8db52011-05-10 02:28:46 -07001014 isci_port_ready(ihost, iport);
1015
1016 for (index = 0; index < SCI_MAX_PHYS; index++) {
Dan Williamsffe191c2011-06-29 13:09:25 -07001017 if (iport->phy_table[index]) {
1018 writel(iport->physical_port_index,
1019 &iport->port_pe_configuration_register[
1020 iport->phy_table[index]->phy_index]);
Marcin Tomczak05b080f2012-01-04 01:33:41 -08001021 if (((iport->active_phy_mask^iport->enabled_phy_mask) & (1 << index)) != 0)
1022 sci_port_resume_phy(iport, iport->phy_table[index]);
Dan Williamse2f8db52011-05-10 02:28:46 -07001023 }
1024 }
1025
Dan Williams89a73012011-06-30 19:14:33 -07001026 sci_port_update_viit_entry(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -07001027
Dan Williamse2f8db52011-05-10 02:28:46 -07001028 /*
1029 * Post the dummy task for the port so the hardware can schedule
1030 * io correctly
1031 */
Dan Williams89a73012011-06-30 19:14:33 -07001032 sci_port_post_dummy_request(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -07001033}
1034
Dan Williams89a73012011-06-30 19:14:33 -07001035static void sci_port_invalidate_dummy_remote_node(struct isci_port *iport)
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001036{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001037 struct isci_host *ihost = iport->owning_controller;
Dan Williamsffe191c2011-06-29 13:09:25 -07001038 u8 phys_index = iport->physical_port_index;
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001039 union scu_remote_node_context *rnc;
Dan Williamsffe191c2011-06-29 13:09:25 -07001040 u16 rni = iport->reserved_rni;
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001041 u32 command;
1042
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001043 rnc = &ihost->remote_node_context_table[rni];
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001044
1045 rnc->ssp.is_valid = false;
1046
1047 /* ensure the preceding tc abort request has reached the
1048 * controller and give it ample time to act before posting the rnc
1049 * invalidate
1050 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001051 readl(&ihost->smu_registers->interrupt_status); /* flush */
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001052 udelay(10);
1053
1054 command = SCU_CONTEXT_COMMAND_POST_RNC_INVALIDATE |
1055 phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
1056
Dan Williams89a73012011-06-30 19:14:33 -07001057 sci_controller_post_request(ihost, command);
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001058}
1059
Dan Williamse2f8db52011-05-10 02:28:46 -07001060/**
1061 *
Dan Williamsffe191c2011-06-29 13:09:25 -07001062 * @object: This is the object which is cast to a struct isci_port object.
Dan Williamse2f8db52011-05-10 02:28:46 -07001063 *
Dan Williamsffe191c2011-06-29 13:09:25 -07001064 * This method will perform the actions required by the struct isci_port on
Edmund Nadolskie3013702011-06-02 00:10:43 +00001065 * exiting the SCI_PORT_SUB_OPERATIONAL. This function reports
Dan Williamse2f8db52011-05-10 02:28:46 -07001066 * the port not ready and suspends the port task scheduler. none
1067 */
Dan Williams89a73012011-06-30 19:14:33 -07001068static void sci_port_ready_substate_operational_exit(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001069{
Dan Williamsffe191c2011-06-29 13:09:25 -07001070 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001071 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -07001072
1073 /*
1074 * Kill the dummy task for this port if it has not yet posted
1075 * the hardware will treat this as a NOP and just return abort
1076 * complete.
1077 */
Dan Williams89a73012011-06-30 19:14:33 -07001078 sci_port_abort_dummy_request(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -07001079
1080 isci_port_not_ready(ihost, iport);
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001081
Dan Williamsffe191c2011-06-29 13:09:25 -07001082 if (iport->ready_exit)
Dan Williams89a73012011-06-30 19:14:33 -07001083 sci_port_invalidate_dummy_remote_node(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -07001084}
1085
Dan Williams89a73012011-06-30 19:14:33 -07001086static void sci_port_ready_substate_configuring_enter(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001087{
Dan Williamsffe191c2011-06-29 13:09:25 -07001088 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001089 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -07001090
Dan Williamsffe191c2011-06-29 13:09:25 -07001091 if (iport->active_phy_mask == 0) {
Dan Williamse2f8db52011-05-10 02:28:46 -07001092 isci_port_not_ready(ihost, iport);
1093
Marcin Tomczak05b080f2012-01-04 01:33:41 -08001094 port_state_machine_change(iport, SCI_PORT_SUB_WAITING);
1095 } else
1096 port_state_machine_change(iport, SCI_PORT_SUB_OPERATIONAL);
Dan Williamse2f8db52011-05-10 02:28:46 -07001097}
1098
Dan Williams89a73012011-06-30 19:14:33 -07001099enum sci_status sci_port_start(struct isci_port *iport)
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001100{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001101 struct isci_host *ihost = iport->owning_controller;
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001102 enum sci_status status = SCI_SUCCESS;
Dan Williams89a73012011-06-30 19:14:33 -07001103 enum sci_port_states state;
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001104 u32 phy_mask;
1105
Dan Williamsffe191c2011-06-29 13:09:25 -07001106 state = iport->sm.current_state_id;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001107 if (state != SCI_PORT_STOPPED) {
Dan Williamsffe191c2011-06-29 13:09:25 -07001108 dev_warn(sciport_to_dev(iport),
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001109 "%s: in wrong state: %d\n", __func__, state);
1110 return SCI_FAILURE_INVALID_STATE;
1111 }
1112
Dan Williamsffe191c2011-06-29 13:09:25 -07001113 if (iport->assigned_device_count > 0) {
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001114 /* TODO This is a start failure operation because
1115 * there are still devices assigned to this port.
1116 * There must be no devices assigned to a port on a
1117 * start operation.
1118 */
1119 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
1120 }
1121
Dan Williamsffe191c2011-06-29 13:09:25 -07001122 if (iport->reserved_rni == SCU_DUMMY_INDEX) {
Dan Williams89a73012011-06-30 19:14:33 -07001123 u16 rni = sci_remote_node_table_allocate_remote_node(
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001124 &ihost->available_remote_nodes, 1);
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001125
1126 if (rni != SCU_DUMMY_INDEX)
Dan Williams89a73012011-06-30 19:14:33 -07001127 sci_port_construct_dummy_rnc(iport, rni);
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001128 else
1129 status = SCI_FAILURE_INSUFFICIENT_RESOURCES;
Dan Williamsffe191c2011-06-29 13:09:25 -07001130 iport->reserved_rni = rni;
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001131 }
1132
Dan Williamsffe191c2011-06-29 13:09:25 -07001133 if (iport->reserved_tag == SCI_CONTROLLER_INVALID_IO_TAG) {
Dan Williams312e0c22011-06-28 13:47:09 -07001134 u16 tag;
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001135
Dan Williams312e0c22011-06-28 13:47:09 -07001136 tag = isci_alloc_tag(ihost);
1137 if (tag == SCI_CONTROLLER_INVALID_IO_TAG)
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001138 status = SCI_FAILURE_INSUFFICIENT_RESOURCES;
Dan Williams312e0c22011-06-28 13:47:09 -07001139 else
Dan Williams89a73012011-06-30 19:14:33 -07001140 sci_port_construct_dummy_task(iport, tag);
Dan Williamsffe191c2011-06-29 13:09:25 -07001141 iport->reserved_tag = tag;
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001142 }
1143
1144 if (status == SCI_SUCCESS) {
Dan Williams89a73012011-06-30 19:14:33 -07001145 phy_mask = sci_port_get_phys(iport);
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001146
1147 /*
1148 * There are one or more phys assigned to this port. Make sure
1149 * the port's phy mask is in fact legal and supported by the
1150 * silicon.
1151 */
Dan Williams89a73012011-06-30 19:14:33 -07001152 if (sci_port_is_phy_mask_valid(iport, phy_mask) == true) {
Dan Williamsffe191c2011-06-29 13:09:25 -07001153 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001154 SCI_PORT_READY);
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001155
1156 return SCI_SUCCESS;
1157 }
1158 status = SCI_FAILURE;
1159 }
1160
1161 if (status != SCI_SUCCESS)
Dan Williams89a73012011-06-30 19:14:33 -07001162 sci_port_destroy_dummy_resources(iport);
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001163
1164 return status;
1165}
1166
Dan Williams89a73012011-06-30 19:14:33 -07001167enum sci_status sci_port_stop(struct isci_port *iport)
Piotr Sawicki8bc80d32011-05-11 23:52:31 +00001168{
Dan Williams89a73012011-06-30 19:14:33 -07001169 enum sci_port_states state;
Piotr Sawicki8bc80d32011-05-11 23:52:31 +00001170
Dan Williamsffe191c2011-06-29 13:09:25 -07001171 state = iport->sm.current_state_id;
Piotr Sawicki8bc80d32011-05-11 23:52:31 +00001172 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001173 case SCI_PORT_STOPPED:
Piotr Sawicki8bc80d32011-05-11 23:52:31 +00001174 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001175 case SCI_PORT_SUB_WAITING:
1176 case SCI_PORT_SUB_OPERATIONAL:
1177 case SCI_PORT_SUB_CONFIGURING:
1178 case SCI_PORT_RESETTING:
Dan Williamsffe191c2011-06-29 13:09:25 -07001179 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001180 SCI_PORT_STOPPING);
Piotr Sawicki8bc80d32011-05-11 23:52:31 +00001181 return SCI_SUCCESS;
1182 default:
Dan Williamsffe191c2011-06-29 13:09:25 -07001183 dev_warn(sciport_to_dev(iport),
Piotr Sawicki8bc80d32011-05-11 23:52:31 +00001184 "%s: in wrong state: %d\n", __func__, state);
1185 return SCI_FAILURE_INVALID_STATE;
1186 }
1187}
1188
Dan Williams89a73012011-06-30 19:14:33 -07001189static enum sci_status sci_port_hard_reset(struct isci_port *iport, u32 timeout)
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001190{
1191 enum sci_status status = SCI_FAILURE_INVALID_PHY;
Dan Williams85280952011-06-28 15:05:53 -07001192 struct isci_phy *iphy = NULL;
Dan Williams89a73012011-06-30 19:14:33 -07001193 enum sci_port_states state;
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001194 u32 phy_index;
1195
Dan Williamsffe191c2011-06-29 13:09:25 -07001196 state = iport->sm.current_state_id;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001197 if (state != SCI_PORT_SUB_OPERATIONAL) {
Dan Williamsffe191c2011-06-29 13:09:25 -07001198 dev_warn(sciport_to_dev(iport),
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001199 "%s: in wrong state: %d\n", __func__, state);
1200 return SCI_FAILURE_INVALID_STATE;
1201 }
1202
1203 /* Select a phy on which we can send the hard reset request. */
Dan Williams85280952011-06-28 15:05:53 -07001204 for (phy_index = 0; phy_index < SCI_MAX_PHYS && !iphy; phy_index++) {
Dan Williamsffe191c2011-06-29 13:09:25 -07001205 iphy = iport->phy_table[phy_index];
Dan Williams89a73012011-06-30 19:14:33 -07001206 if (iphy && !sci_port_active_phy(iport, iphy)) {
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001207 /*
1208 * We found a phy but it is not ready select
1209 * different phy
1210 */
Dan Williams85280952011-06-28 15:05:53 -07001211 iphy = NULL;
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001212 }
1213 }
1214
1215 /* If we have a phy then go ahead and start the reset procedure */
Dan Williams85280952011-06-28 15:05:53 -07001216 if (!iphy)
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001217 return status;
Dan Williams89a73012011-06-30 19:14:33 -07001218 status = sci_phy_reset(iphy);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001219
1220 if (status != SCI_SUCCESS)
1221 return status;
1222
Dan Williamsffe191c2011-06-29 13:09:25 -07001223 sci_mod_timer(&iport->timer, timeout);
1224 iport->not_ready_reason = SCIC_PORT_NOT_READY_HARD_RESET_REQUESTED;
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001225
Dan Williamsffe191c2011-06-29 13:09:25 -07001226 port_state_machine_change(iport, SCI_PORT_RESETTING);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001227 return SCI_SUCCESS;
1228}
1229
1230/**
Dan Williams89a73012011-06-30 19:14:33 -07001231 * sci_port_add_phy() -
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001232 * @sci_port: This parameter specifies the port in which the phy will be added.
1233 * @sci_phy: This parameter is the phy which is to be added to the port.
1234 *
1235 * This method will add a PHY to the selected port. This method returns an
1236 * enum sci_status. SCI_SUCCESS the phy has been added to the port. Any other
1237 * status is a failure to add the phy to the port.
1238 */
Dan Williams89a73012011-06-30 19:14:33 -07001239enum sci_status sci_port_add_phy(struct isci_port *iport,
Dan Williams85280952011-06-28 15:05:53 -07001240 struct isci_phy *iphy)
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001241{
1242 enum sci_status status;
Dan Williams89a73012011-06-30 19:14:33 -07001243 enum sci_port_states state;
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001244
Dan Williamsffe191c2011-06-29 13:09:25 -07001245 state = iport->sm.current_state_id;
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001246 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001247 case SCI_PORT_STOPPED: {
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001248 struct sci_sas_address port_sas_address;
1249
1250 /* Read the port assigned SAS Address if there is one */
Dan Williams89a73012011-06-30 19:14:33 -07001251 sci_port_get_sas_address(iport, &port_sas_address);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001252
1253 if (port_sas_address.high != 0 && port_sas_address.low != 0) {
1254 struct sci_sas_address phy_sas_address;
1255
1256 /* Make sure that the PHY SAS Address matches the SAS Address
1257 * for this port
1258 */
Dan Williams89a73012011-06-30 19:14:33 -07001259 sci_phy_get_sas_address(iphy, &phy_sas_address);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001260
1261 if (port_sas_address.high != phy_sas_address.high ||
1262 port_sas_address.low != phy_sas_address.low)
1263 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
1264 }
Dan Williams89a73012011-06-30 19:14:33 -07001265 return sci_port_set_phy(iport, iphy);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001266 }
Edmund Nadolskie3013702011-06-02 00:10:43 +00001267 case SCI_PORT_SUB_WAITING:
1268 case SCI_PORT_SUB_OPERATIONAL:
Dan Williams89a73012011-06-30 19:14:33 -07001269 status = sci_port_set_phy(iport, iphy);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001270
1271 if (status != SCI_SUCCESS)
1272 return status;
1273
Marcin Tomczak05b080f2012-01-04 01:33:41 -08001274 sci_port_general_link_up_handler(iport, iphy, PF_NOTIFY|PF_RESUME);
Dan Williamsffe191c2011-06-29 13:09:25 -07001275 iport->not_ready_reason = SCIC_PORT_NOT_READY_RECONFIGURING;
1276 port_state_machine_change(iport, SCI_PORT_SUB_CONFIGURING);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001277
1278 return status;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001279 case SCI_PORT_SUB_CONFIGURING:
Dan Williams89a73012011-06-30 19:14:33 -07001280 status = sci_port_set_phy(iport, iphy);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001281
1282 if (status != SCI_SUCCESS)
1283 return status;
Marcin Tomczak05b080f2012-01-04 01:33:41 -08001284 sci_port_general_link_up_handler(iport, iphy, PF_NOTIFY);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001285
1286 /* Re-enter the configuring state since this may be the last phy in
1287 * the port.
1288 */
Dan Williamsffe191c2011-06-29 13:09:25 -07001289 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001290 SCI_PORT_SUB_CONFIGURING);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001291 return SCI_SUCCESS;
1292 default:
Dan Williamsffe191c2011-06-29 13:09:25 -07001293 dev_warn(sciport_to_dev(iport),
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001294 "%s: in wrong state: %d\n", __func__, state);
1295 return SCI_FAILURE_INVALID_STATE;
1296 }
1297}
1298
1299/**
Dan Williams89a73012011-06-30 19:14:33 -07001300 * sci_port_remove_phy() -
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001301 * @sci_port: This parameter specifies the port in which the phy will be added.
1302 * @sci_phy: This parameter is the phy which is to be added to the port.
1303 *
1304 * This method will remove the PHY from the selected PORT. This method returns
1305 * an enum sci_status. SCI_SUCCESS the phy has been removed from the port. Any
1306 * other status is a failure to add the phy to the port.
1307 */
Dan Williams89a73012011-06-30 19:14:33 -07001308enum sci_status sci_port_remove_phy(struct isci_port *iport,
Dan Williams85280952011-06-28 15:05:53 -07001309 struct isci_phy *iphy)
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001310{
1311 enum sci_status status;
Dan Williams89a73012011-06-30 19:14:33 -07001312 enum sci_port_states state;
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001313
Dan Williamsffe191c2011-06-29 13:09:25 -07001314 state = iport->sm.current_state_id;
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001315
1316 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001317 case SCI_PORT_STOPPED:
Dan Williams89a73012011-06-30 19:14:33 -07001318 return sci_port_clear_phy(iport, iphy);
Edmund Nadolskie3013702011-06-02 00:10:43 +00001319 case SCI_PORT_SUB_OPERATIONAL:
Dan Williams89a73012011-06-30 19:14:33 -07001320 status = sci_port_clear_phy(iport, iphy);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001321 if (status != SCI_SUCCESS)
1322 return status;
1323
Dan Williams89a73012011-06-30 19:14:33 -07001324 sci_port_deactivate_phy(iport, iphy, true);
Dan Williamsffe191c2011-06-29 13:09:25 -07001325 iport->not_ready_reason = SCIC_PORT_NOT_READY_RECONFIGURING;
1326 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001327 SCI_PORT_SUB_CONFIGURING);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001328 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001329 case SCI_PORT_SUB_CONFIGURING:
Dan Williams89a73012011-06-30 19:14:33 -07001330 status = sci_port_clear_phy(iport, iphy);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001331
1332 if (status != SCI_SUCCESS)
1333 return status;
Dan Williams89a73012011-06-30 19:14:33 -07001334 sci_port_deactivate_phy(iport, iphy, true);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001335
1336 /* Re-enter the configuring state since this may be the last phy in
1337 * the port
1338 */
Dan Williamsffe191c2011-06-29 13:09:25 -07001339 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001340 SCI_PORT_SUB_CONFIGURING);
Piotr Sawicki051266c2011-05-12 19:10:14 +00001341 return SCI_SUCCESS;
1342 default:
Dan Williamsffe191c2011-06-29 13:09:25 -07001343 dev_warn(sciport_to_dev(iport),
Piotr Sawicki051266c2011-05-12 19:10:14 +00001344 "%s: in wrong state: %d\n", __func__, state);
1345 return SCI_FAILURE_INVALID_STATE;
1346 }
1347}
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001348
Dan Williams89a73012011-06-30 19:14:33 -07001349enum sci_status sci_port_link_up(struct isci_port *iport,
Dan Williams85280952011-06-28 15:05:53 -07001350 struct isci_phy *iphy)
Piotr Sawicki051266c2011-05-12 19:10:14 +00001351{
Dan Williams89a73012011-06-30 19:14:33 -07001352 enum sci_port_states state;
Piotr Sawicki051266c2011-05-12 19:10:14 +00001353
Dan Williamsffe191c2011-06-29 13:09:25 -07001354 state = iport->sm.current_state_id;
Piotr Sawicki051266c2011-05-12 19:10:14 +00001355 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001356 case SCI_PORT_SUB_WAITING:
Piotr Sawicki051266c2011-05-12 19:10:14 +00001357 /* Since this is the first phy going link up for the port we
1358 * can just enable it and continue
1359 */
Marcin Tomczak05b080f2012-01-04 01:33:41 -08001360 sci_port_activate_phy(iport, iphy, PF_NOTIFY|PF_RESUME);
Piotr Sawicki051266c2011-05-12 19:10:14 +00001361
Dan Williamsffe191c2011-06-29 13:09:25 -07001362 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001363 SCI_PORT_SUB_OPERATIONAL);
Piotr Sawicki051266c2011-05-12 19:10:14 +00001364 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001365 case SCI_PORT_SUB_OPERATIONAL:
Marcin Tomczak05b080f2012-01-04 01:33:41 -08001366 sci_port_general_link_up_handler(iport, iphy, PF_NOTIFY|PF_RESUME);
Piotr Sawicki051266c2011-05-12 19:10:14 +00001367 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001368 case SCI_PORT_RESETTING:
Piotr Sawicki051266c2011-05-12 19:10:14 +00001369 /* TODO We should make sure that the phy that has gone
1370 * link up is the same one on which we sent the reset. It is
1371 * possible that the phy on which we sent the reset is not the
1372 * one that has gone link up and we want to make sure that
1373 * phy being reset comes back. Consider the case where a
1374 * reset is sent but before the hardware processes the reset it
1375 * get a link up on the port because of a hot plug event.
1376 * because of the reset request this phy will go link down
1377 * almost immediately.
1378 */
1379
1380 /* In the resetting state we don't notify the user regarding
1381 * link up and link down notifications.
1382 */
Marcin Tomczak05b080f2012-01-04 01:33:41 -08001383 sci_port_general_link_up_handler(iport, iphy, PF_RESUME);
Piotr Sawicki051266c2011-05-12 19:10:14 +00001384 return SCI_SUCCESS;
1385 default:
Dan Williamsffe191c2011-06-29 13:09:25 -07001386 dev_warn(sciport_to_dev(iport),
Piotr Sawicki051266c2011-05-12 19:10:14 +00001387 "%s: in wrong state: %d\n", __func__, state);
1388 return SCI_FAILURE_INVALID_STATE;
1389 }
1390}
1391
Dan Williams89a73012011-06-30 19:14:33 -07001392enum sci_status sci_port_link_down(struct isci_port *iport,
Dan Williams85280952011-06-28 15:05:53 -07001393 struct isci_phy *iphy)
Piotr Sawicki051266c2011-05-12 19:10:14 +00001394{
Dan Williams89a73012011-06-30 19:14:33 -07001395 enum sci_port_states state;
Piotr Sawicki051266c2011-05-12 19:10:14 +00001396
Dan Williamsffe191c2011-06-29 13:09:25 -07001397 state = iport->sm.current_state_id;
Piotr Sawicki051266c2011-05-12 19:10:14 +00001398 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001399 case SCI_PORT_SUB_OPERATIONAL:
Dan Williams89a73012011-06-30 19:14:33 -07001400 sci_port_deactivate_phy(iport, iphy, true);
Piotr Sawicki051266c2011-05-12 19:10:14 +00001401
1402 /* If there are no active phys left in the port, then
1403 * transition the port to the WAITING state until such time
1404 * as a phy goes link up
1405 */
Dan Williamsffe191c2011-06-29 13:09:25 -07001406 if (iport->active_phy_mask == 0)
1407 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001408 SCI_PORT_SUB_WAITING);
Piotr Sawicki051266c2011-05-12 19:10:14 +00001409 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001410 case SCI_PORT_RESETTING:
Piotr Sawicki051266c2011-05-12 19:10:14 +00001411 /* In the resetting state we don't notify the user regarding
1412 * link up and link down notifications. */
Dan Williams89a73012011-06-30 19:14:33 -07001413 sci_port_deactivate_phy(iport, iphy, false);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001414 return SCI_SUCCESS;
1415 default:
Dan Williamsffe191c2011-06-29 13:09:25 -07001416 dev_warn(sciport_to_dev(iport),
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001417 "%s: in wrong state: %d\n", __func__, state);
1418 return SCI_FAILURE_INVALID_STATE;
1419 }
1420}
1421
Dan Williams89a73012011-06-30 19:14:33 -07001422enum sci_status sci_port_start_io(struct isci_port *iport,
1423 struct isci_remote_device *idev,
1424 struct isci_request *ireq)
Dan Williams68138202011-05-12 07:16:06 -07001425{
Dan Williams89a73012011-06-30 19:14:33 -07001426 enum sci_port_states state;
Dan Williamse2f8db52011-05-10 02:28:46 -07001427
Dan Williamsffe191c2011-06-29 13:09:25 -07001428 state = iport->sm.current_state_id;
Dan Williams68138202011-05-12 07:16:06 -07001429 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001430 case SCI_PORT_SUB_WAITING:
Dan Williams68138202011-05-12 07:16:06 -07001431 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001432 case SCI_PORT_SUB_OPERATIONAL:
Dan Williamsffe191c2011-06-29 13:09:25 -07001433 iport->started_request_count++;
Dan Williams68138202011-05-12 07:16:06 -07001434 return SCI_SUCCESS;
1435 default:
Dan Williamsffe191c2011-06-29 13:09:25 -07001436 dev_warn(sciport_to_dev(iport),
Dan Williams68138202011-05-12 07:16:06 -07001437 "%s: in wrong state: %d\n", __func__, state);
1438 return SCI_FAILURE_INVALID_STATE;
1439 }
1440}
1441
Dan Williams89a73012011-06-30 19:14:33 -07001442enum sci_status sci_port_complete_io(struct isci_port *iport,
1443 struct isci_remote_device *idev,
1444 struct isci_request *ireq)
Dan Williams68138202011-05-12 07:16:06 -07001445{
Dan Williams89a73012011-06-30 19:14:33 -07001446 enum sci_port_states state;
Dan Williams68138202011-05-12 07:16:06 -07001447
Dan Williamsffe191c2011-06-29 13:09:25 -07001448 state = iport->sm.current_state_id;
Dan Williams68138202011-05-12 07:16:06 -07001449 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001450 case SCI_PORT_STOPPED:
Dan Williamsffe191c2011-06-29 13:09:25 -07001451 dev_warn(sciport_to_dev(iport),
Dan Williams68138202011-05-12 07:16:06 -07001452 "%s: in wrong state: %d\n", __func__, state);
1453 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001454 case SCI_PORT_STOPPING:
Dan Williams89a73012011-06-30 19:14:33 -07001455 sci_port_decrement_request_count(iport);
Dan Williams68138202011-05-12 07:16:06 -07001456
Dan Williamsffe191c2011-06-29 13:09:25 -07001457 if (iport->started_request_count == 0)
1458 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001459 SCI_PORT_STOPPED);
Dan Williams68138202011-05-12 07:16:06 -07001460 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001461 case SCI_PORT_READY:
1462 case SCI_PORT_RESETTING:
1463 case SCI_PORT_FAILED:
1464 case SCI_PORT_SUB_WAITING:
1465 case SCI_PORT_SUB_OPERATIONAL:
Dan Williams89a73012011-06-30 19:14:33 -07001466 sci_port_decrement_request_count(iport);
Dan Williams68138202011-05-12 07:16:06 -07001467 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001468 case SCI_PORT_SUB_CONFIGURING:
Dan Williams89a73012011-06-30 19:14:33 -07001469 sci_port_decrement_request_count(iport);
Dan Williamsffe191c2011-06-29 13:09:25 -07001470 if (iport->started_request_count == 0) {
1471 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001472 SCI_PORT_SUB_OPERATIONAL);
Dan Williams68138202011-05-12 07:16:06 -07001473 }
1474 break;
1475 }
1476 return SCI_SUCCESS;
1477}
Dan Williamse2f8db52011-05-10 02:28:46 -07001478
Dan Williams89a73012011-06-30 19:14:33 -07001479static void sci_port_enable_port_task_scheduler(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -07001480{
1481 u32 pts_control_value;
1482
Dan Williams89a73012011-06-30 19:14:33 -07001483 /* enable the port task scheduler in a suspended state */
Dan Williamsffe191c2011-06-29 13:09:25 -07001484 pts_control_value = readl(&iport->port_task_scheduler_registers->control);
Dan Williamse2f8db52011-05-10 02:28:46 -07001485 pts_control_value |= SCU_PTSxCR_GEN_BIT(ENABLE) | SCU_PTSxCR_GEN_BIT(SUSPEND);
Dan Williamsffe191c2011-06-29 13:09:25 -07001486 writel(pts_control_value, &iport->port_task_scheduler_registers->control);
Dan Williamse2f8db52011-05-10 02:28:46 -07001487}
1488
Dan Williams89a73012011-06-30 19:14:33 -07001489static void sci_port_disable_port_task_scheduler(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -07001490{
1491 u32 pts_control_value;
1492
Dan Williamsffe191c2011-06-29 13:09:25 -07001493 pts_control_value = readl(&iport->port_task_scheduler_registers->control);
Dan Williamse2f8db52011-05-10 02:28:46 -07001494 pts_control_value &=
1495 ~(SCU_PTSxCR_GEN_BIT(ENABLE) | SCU_PTSxCR_GEN_BIT(SUSPEND));
Dan Williamsffe191c2011-06-29 13:09:25 -07001496 writel(pts_control_value, &iport->port_task_scheduler_registers->control);
Dan Williamse2f8db52011-05-10 02:28:46 -07001497}
1498
Dan Williams89a73012011-06-30 19:14:33 -07001499static void sci_port_post_dummy_remote_node(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -07001500{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001501 struct isci_host *ihost = iport->owning_controller;
Dan Williamsffe191c2011-06-29 13:09:25 -07001502 u8 phys_index = iport->physical_port_index;
Dan Williamse2f8db52011-05-10 02:28:46 -07001503 union scu_remote_node_context *rnc;
Dan Williamsffe191c2011-06-29 13:09:25 -07001504 u16 rni = iport->reserved_rni;
Dan Williamse2f8db52011-05-10 02:28:46 -07001505 u32 command;
1506
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001507 rnc = &ihost->remote_node_context_table[rni];
Dan Williamse2f8db52011-05-10 02:28:46 -07001508 rnc->ssp.is_valid = true;
1509
1510 command = SCU_CONTEXT_COMMAND_POST_RNC_32 |
1511 phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
1512
Dan Williams89a73012011-06-30 19:14:33 -07001513 sci_controller_post_request(ihost, command);
Dan Williamse2f8db52011-05-10 02:28:46 -07001514
1515 /* ensure hardware has seen the post rnc command and give it
1516 * ample time to act before sending the suspend
1517 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001518 readl(&ihost->smu_registers->interrupt_status); /* flush */
Dan Williamse2f8db52011-05-10 02:28:46 -07001519 udelay(10);
1520
1521 command = SCU_CONTEXT_COMMAND_POST_RNC_SUSPEND_TX_RX |
1522 phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
1523
Dan Williams89a73012011-06-30 19:14:33 -07001524 sci_controller_post_request(ihost, command);
Dan Williamse2f8db52011-05-10 02:28:46 -07001525}
1526
Dan Williams89a73012011-06-30 19:14:33 -07001527static void sci_port_stopped_state_enter(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001528{
Dan Williamsffe191c2011-06-29 13:09:25 -07001529 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamse2f8db52011-05-10 02:28:46 -07001530
Dan Williamsffe191c2011-06-29 13:09:25 -07001531 if (iport->sm.previous_state_id == SCI_PORT_STOPPING) {
Dan Williamse2f8db52011-05-10 02:28:46 -07001532 /*
1533 * If we enter this state becasuse of a request to stop
1534 * the port then we want to disable the hardwares port
1535 * task scheduler. */
Dan Williams89a73012011-06-30 19:14:33 -07001536 sci_port_disable_port_task_scheduler(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -07001537 }
1538}
1539
Dan Williams89a73012011-06-30 19:14:33 -07001540static void sci_port_stopped_state_exit(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001541{
Dan Williamsffe191c2011-06-29 13:09:25 -07001542 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamse2f8db52011-05-10 02:28:46 -07001543
1544 /* Enable and suspend the port task scheduler */
Dan Williams89a73012011-06-30 19:14:33 -07001545 sci_port_enable_port_task_scheduler(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -07001546}
1547
Dan Williams89a73012011-06-30 19:14:33 -07001548static void sci_port_ready_state_enter(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001549{
Dan Williamsffe191c2011-06-29 13:09:25 -07001550 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001551 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -07001552 u32 prev_state;
1553
Dan Williamsffe191c2011-06-29 13:09:25 -07001554 prev_state = iport->sm.previous_state_id;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001555 if (prev_state == SCI_PORT_RESETTING)
Dan Williamse2f8db52011-05-10 02:28:46 -07001556 isci_port_hard_reset_complete(iport, SCI_SUCCESS);
1557 else
1558 isci_port_not_ready(ihost, iport);
1559
1560 /* Post and suspend the dummy remote node context for this port. */
Dan Williams89a73012011-06-30 19:14:33 -07001561 sci_port_post_dummy_remote_node(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -07001562
1563 /* Start the ready substate machine */
Dan Williamsffe191c2011-06-29 13:09:25 -07001564 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001565 SCI_PORT_SUB_WAITING);
Dan Williamse2f8db52011-05-10 02:28:46 -07001566}
1567
Dan Williams89a73012011-06-30 19:14:33 -07001568static void sci_port_resetting_state_exit(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001569{
Dan Williamsffe191c2011-06-29 13:09:25 -07001570 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamse2f8db52011-05-10 02:28:46 -07001571
Dan Williamsffe191c2011-06-29 13:09:25 -07001572 sci_del_timer(&iport->timer);
Dan Williamse2f8db52011-05-10 02:28:46 -07001573}
1574
Dan Williams89a73012011-06-30 19:14:33 -07001575static void sci_port_stopping_state_exit(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001576{
Dan Williamsffe191c2011-06-29 13:09:25 -07001577 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamse2f8db52011-05-10 02:28:46 -07001578
Dan Williamsffe191c2011-06-29 13:09:25 -07001579 sci_del_timer(&iport->timer);
Dan Williamse2f8db52011-05-10 02:28:46 -07001580
Dan Williams89a73012011-06-30 19:14:33 -07001581 sci_port_destroy_dummy_resources(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -07001582}
1583
Dan Williams89a73012011-06-30 19:14:33 -07001584static void sci_port_failed_state_enter(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001585{
Dan Williamsffe191c2011-06-29 13:09:25 -07001586 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamse2f8db52011-05-10 02:28:46 -07001587
Dan Williamse2f8db52011-05-10 02:28:46 -07001588 isci_port_hard_reset_complete(iport, SCI_FAILURE_TIMEOUT);
1589}
1590
1591/* --------------------------------------------------------------------------- */
1592
Dan Williams89a73012011-06-30 19:14:33 -07001593static const struct sci_base_state sci_port_state_table[] = {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001594 [SCI_PORT_STOPPED] = {
Dan Williams89a73012011-06-30 19:14:33 -07001595 .enter_state = sci_port_stopped_state_enter,
1596 .exit_state = sci_port_stopped_state_exit
Dan Williamse2f8db52011-05-10 02:28:46 -07001597 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001598 [SCI_PORT_STOPPING] = {
Dan Williams89a73012011-06-30 19:14:33 -07001599 .exit_state = sci_port_stopping_state_exit
Dan Williamse2f8db52011-05-10 02:28:46 -07001600 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001601 [SCI_PORT_READY] = {
Dan Williams89a73012011-06-30 19:14:33 -07001602 .enter_state = sci_port_ready_state_enter,
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001603 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001604 [SCI_PORT_SUB_WAITING] = {
Dan Williams89a73012011-06-30 19:14:33 -07001605 .enter_state = sci_port_ready_substate_waiting_enter,
Marcin Tomczak05b080f2012-01-04 01:33:41 -08001606 .exit_state = scic_sds_port_ready_substate_waiting_exit,
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001607 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001608 [SCI_PORT_SUB_OPERATIONAL] = {
Dan Williams89a73012011-06-30 19:14:33 -07001609 .enter_state = sci_port_ready_substate_operational_enter,
1610 .exit_state = sci_port_ready_substate_operational_exit
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001611 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001612 [SCI_PORT_SUB_CONFIGURING] = {
Marcin Tomczak05b080f2012-01-04 01:33:41 -08001613 .enter_state = sci_port_ready_substate_configuring_enter
Dan Williamse2f8db52011-05-10 02:28:46 -07001614 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001615 [SCI_PORT_RESETTING] = {
Dan Williams89a73012011-06-30 19:14:33 -07001616 .exit_state = sci_port_resetting_state_exit
Dan Williamse2f8db52011-05-10 02:28:46 -07001617 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001618 [SCI_PORT_FAILED] = {
Dan Williams89a73012011-06-30 19:14:33 -07001619 .enter_state = sci_port_failed_state_enter,
Dan Williamse2f8db52011-05-10 02:28:46 -07001620 }
1621};
1622
Dan Williams89a73012011-06-30 19:14:33 -07001623void sci_port_construct(struct isci_port *iport, u8 index,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001624 struct isci_host *ihost)
Dan Williamse2f8db52011-05-10 02:28:46 -07001625{
Dan Williams89a73012011-06-30 19:14:33 -07001626 sci_init_sm(&iport->sm, sci_port_state_table, SCI_PORT_STOPPED);
Dan Williamse2f8db52011-05-10 02:28:46 -07001627
Dan Williamsffe191c2011-06-29 13:09:25 -07001628 iport->logical_port_index = SCIC_SDS_DUMMY_PORT;
1629 iport->physical_port_index = index;
1630 iport->active_phy_mask = 0;
Marcin Tomczak05b080f2012-01-04 01:33:41 -08001631 iport->enabled_phy_mask = 0;
Jeff Skirvin8e35a132011-10-27 15:05:32 -07001632 iport->last_active_phy = 0;
1633 iport->ready_exit = false;
Dan Williamse2f8db52011-05-10 02:28:46 -07001634
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001635 iport->owning_controller = ihost;
Dan Williamse2f8db52011-05-10 02:28:46 -07001636
Dan Williamsffe191c2011-06-29 13:09:25 -07001637 iport->started_request_count = 0;
1638 iport->assigned_device_count = 0;
Dan Williamse2f8db52011-05-10 02:28:46 -07001639
Dan Williamsffe191c2011-06-29 13:09:25 -07001640 iport->reserved_rni = SCU_DUMMY_INDEX;
1641 iport->reserved_tag = SCI_CONTROLLER_INVALID_IO_TAG;
Dan Williamse2f8db52011-05-10 02:28:46 -07001642
Dan Williamsffe191c2011-06-29 13:09:25 -07001643 sci_init_timer(&iport->timer, port_timeout);
Edmund Nadolski5553ba22011-05-19 11:59:10 +00001644
Dan Williamsffe191c2011-06-29 13:09:25 -07001645 iport->port_task_scheduler_registers = NULL;
Dan Williamse2f8db52011-05-10 02:28:46 -07001646
1647 for (index = 0; index < SCI_MAX_PHYS; index++)
Dan Williamsffe191c2011-06-29 13:09:25 -07001648 iport->phy_table[index] = NULL;
Dan Williamse2f8db52011-05-10 02:28:46 -07001649}
1650
1651void isci_port_init(struct isci_port *iport, struct isci_host *ihost, int index)
1652{
1653 INIT_LIST_HEAD(&iport->remote_dev_list);
1654 INIT_LIST_HEAD(&iport->domain_dev_list);
1655 spin_lock_init(&iport->state_lock);
Dan Williamse2f8db52011-05-10 02:28:46 -07001656 iport->isci_host = ihost;
1657 isci_port_change_state(iport, isci_freed);
1658}
1659
1660/**
1661 * isci_port_get_state() - This function gets the status of the port object.
1662 * @isci_port: This parameter points to the isci_port object
1663 *
1664 * status of the object as a isci_status enum.
1665 */
1666enum isci_status isci_port_get_state(
1667 struct isci_port *isci_port)
1668{
1669 return isci_port->status;
1670}
1671
Dan Williams89a73012011-06-30 19:14:33 -07001672void sci_port_broadcast_change_received(struct isci_port *iport, struct isci_phy *iphy)
Dan Williamse2f8db52011-05-10 02:28:46 -07001673{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001674 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -07001675
1676 /* notify the user. */
Dan Williamsffe191c2011-06-29 13:09:25 -07001677 isci_port_bc_change_received(ihost, iport, iphy);
Dan Williamse2f8db52011-05-10 02:28:46 -07001678}
1679
Dan Williams4393aa42011-03-31 13:10:44 -07001680int isci_port_perform_hard_reset(struct isci_host *ihost, struct isci_port *iport,
1681 struct isci_phy *iphy)
Dan Williams6f231dd2011-07-02 22:56:22 -07001682{
Dan Williams4393aa42011-03-31 13:10:44 -07001683 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -07001684 enum sci_status status;
Jeff Skirvin8e35a132011-10-27 15:05:32 -07001685 int ret = TMF_RESP_FUNC_COMPLETE;
Dan Williams6f231dd2011-07-02 22:56:22 -07001686
Dan Williams4393aa42011-03-31 13:10:44 -07001687 dev_dbg(&ihost->pdev->dev, "%s: iport = %p\n",
1688 __func__, iport);
Dan Williams6f231dd2011-07-02 22:56:22 -07001689
Dan Williams4393aa42011-03-31 13:10:44 -07001690 init_completion(&iport->hard_reset_complete);
Dan Williams6f231dd2011-07-02 22:56:22 -07001691
Dan Williams4393aa42011-03-31 13:10:44 -07001692 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001693
1694 #define ISCI_PORT_RESET_TIMEOUT SCIC_SDS_SIGNATURE_FIS_TIMEOUT
Dan Williams89a73012011-06-30 19:14:33 -07001695 status = sci_port_hard_reset(iport, ISCI_PORT_RESET_TIMEOUT);
Dan Williams6f231dd2011-07-02 22:56:22 -07001696
Dan Williams4393aa42011-03-31 13:10:44 -07001697 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001698
1699 if (status == SCI_SUCCESS) {
Dan Williams4393aa42011-03-31 13:10:44 -07001700 wait_for_completion(&iport->hard_reset_complete);
Dan Williams6f231dd2011-07-02 22:56:22 -07001701
Dan Williams4393aa42011-03-31 13:10:44 -07001702 dev_dbg(&ihost->pdev->dev,
1703 "%s: iport = %p; hard reset completion\n",
1704 __func__, iport);
Dan Williams6f231dd2011-07-02 22:56:22 -07001705
Jeff Skirvin8e35a132011-10-27 15:05:32 -07001706 if (iport->hard_reset_status != SCI_SUCCESS) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001707 ret = TMF_RESP_FUNC_FAILED;
Jeff Skirvin8e35a132011-10-27 15:05:32 -07001708
1709 dev_err(&ihost->pdev->dev,
1710 "%s: iport = %p; hard reset failed (0x%x)\n",
1711 __func__, iport, iport->hard_reset_status);
1712 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001713 } else {
1714 ret = TMF_RESP_FUNC_FAILED;
1715
Dan Williams4393aa42011-03-31 13:10:44 -07001716 dev_err(&ihost->pdev->dev,
Dan Williams89a73012011-06-30 19:14:33 -07001717 "%s: iport = %p; sci_port_hard_reset call"
Dan Williams6f231dd2011-07-02 22:56:22 -07001718 " failed 0x%x\n",
Dan Williams4393aa42011-03-31 13:10:44 -07001719 __func__, iport, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001720
1721 }
1722
1723 /* If the hard reset for the port has failed, consider this
1724 * the same as link failures on all phys in the port.
1725 */
1726 if (ret != TMF_RESP_FUNC_COMPLETE) {
Jeff Skirvinfd0527a2011-06-20 14:09:26 -07001727
Dan Williams4393aa42011-03-31 13:10:44 -07001728 dev_err(&ihost->pdev->dev,
1729 "%s: iport = %p; hard reset failed "
Jeff Skirvinfd0527a2011-06-20 14:09:26 -07001730 "(0x%x) - driving explicit link fail for all phys\n",
1731 __func__, iport, iport->hard_reset_status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001732 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001733 return ret;
1734}
Dave Jiang09d7da12011-03-26 16:11:51 -07001735
Dan Williamse2f8db52011-05-10 02:28:46 -07001736void isci_port_deformed(struct asd_sas_phy *phy)
Dave Jiang09d7da12011-03-26 16:11:51 -07001737{
Dan Williamsc132f692012-01-03 23:26:08 -08001738 struct isci_host *ihost = phy->ha->lldd_ha;
1739 struct isci_port *iport = phy->port->lldd_port;
1740 unsigned long flags;
1741 int i;
1742
1743 /* we got a port notification on a port that was subsequently
1744 * torn down and libsas is just now catching up
1745 */
1746 if (!iport)
1747 return;
1748
1749 spin_lock_irqsave(&ihost->scic_lock, flags);
1750 for (i = 0; i < SCI_MAX_PHYS; i++) {
1751 if (iport->active_phy_mask & 1 << i)
1752 break;
1753 }
1754 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1755
1756 if (i >= SCI_MAX_PHYS)
1757 dev_dbg(&ihost->pdev->dev, "%s: port: %ld\n",
1758 __func__, (long) (iport - &ihost->ports[0]));
Dan Williamse2f8db52011-05-10 02:28:46 -07001759}
1760
Dan Williamse2f8db52011-05-10 02:28:46 -07001761void isci_port_formed(struct asd_sas_phy *phy)
1762{
Dan Williamsc132f692012-01-03 23:26:08 -08001763 struct isci_host *ihost = phy->ha->lldd_ha;
1764 struct isci_phy *iphy = to_iphy(phy);
1765 struct asd_sas_port *port = phy->port;
1766 struct isci_port *iport;
1767 unsigned long flags;
1768 int i;
1769
1770 /* initial ports are formed as the driver is still initializing,
1771 * wait for that process to complete
1772 */
1773 wait_for_start(ihost);
1774
1775 spin_lock_irqsave(&ihost->scic_lock, flags);
1776 for (i = 0; i < SCI_MAX_PORTS; i++) {
1777 iport = &ihost->ports[i];
1778 if (iport->active_phy_mask & 1 << iphy->phy_index)
1779 break;
1780 }
1781 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1782
1783 if (i >= SCI_MAX_PORTS)
1784 iport = NULL;
1785
1786 port->lldd_port = iport;
Dave Jiang09d7da12011-03-26 16:11:51 -07001787}