blob: ac7f27749f975761a1beefc7f1b1741a5c02e0bc [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 Williamse5313812011-05-07 10:11:43 -070063static void isci_port_change_state(struct isci_port *iport, enum isci_status status)
64{
65 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -070066
Dan Williamse5313812011-05-07 10:11:43 -070067 dev_dbg(&iport->isci_host->pdev->dev,
68 "%s: iport = %p, state = 0x%x\n",
69 __func__, iport, status);
Dan Williams6f231dd2011-07-02 22:56:22 -070070
Dan Williamse5313812011-05-07 10:11:43 -070071 /* XXX pointless lock */
72 spin_lock_irqsave(&iport->state_lock, flags);
73 iport->status = status;
74 spin_unlock_irqrestore(&iport->state_lock, flags);
75}
Dan Williams6f231dd2011-07-02 22:56:22 -070076
Dan Williams89a73012011-06-30 19:14:33 -070077static void sci_port_get_protocols(struct isci_port *iport, struct sci_phy_proto *proto)
Dan Williams6f231dd2011-07-02 22:56:22 -070078{
Dan Williamse2f8db52011-05-10 02:28:46 -070079 u8 index;
80
Dan Williams89a73012011-06-30 19:14:33 -070081 proto->all = 0;
Dan Williamse2f8db52011-05-10 02:28:46 -070082 for (index = 0; index < SCI_MAX_PHYS; index++) {
Dan Williams89a73012011-06-30 19:14:33 -070083 struct isci_phy *iphy = iport->phy_table[index];
84
85 if (!iphy)
86 continue;
87 sci_phy_get_protocols(iphy, proto);
Dan Williamse2f8db52011-05-10 02:28:46 -070088 }
Dan Williams6f231dd2011-07-02 22:56:22 -070089}
90
Dan Williams89a73012011-06-30 19:14:33 -070091static u32 sci_port_get_phys(struct isci_port *iport)
Dan Williams6f231dd2011-07-02 22:56:22 -070092{
Dan Williamse2f8db52011-05-10 02:28:46 -070093 u32 index;
94 u32 mask;
95
96 mask = 0;
Dan Williams89a73012011-06-30 19:14:33 -070097 for (index = 0; index < SCI_MAX_PHYS; index++)
98 if (iport->phy_table[index])
Dan Williamse2f8db52011-05-10 02:28:46 -070099 mask |= (1 << index);
Dan Williamse2f8db52011-05-10 02:28:46 -0700100
101 return mask;
Dan Williams6f231dd2011-07-02 22:56:22 -0700102}
103
Dan Williamse2f8db52011-05-10 02:28:46 -0700104/**
Dan Williams89a73012011-06-30 19:14:33 -0700105 * sci_port_get_properties() - This method simply returns the properties
Dan Williamse2f8db52011-05-10 02:28:46 -0700106 * regarding the port, such as: physical index, protocols, sas address, etc.
107 * @port: this parameter specifies the port for which to retrieve the physical
108 * index.
109 * @properties: This parameter specifies the properties structure into which to
110 * copy the requested information.
111 *
112 * Indicate if the user specified a valid port. SCI_SUCCESS This value is
113 * returned if the specified port was valid. SCI_FAILURE_INVALID_PORT This
114 * value is returned if the specified port is not valid. When this value is
115 * returned, no data is copied to the properties output parameter.
116 */
Dan Williams89a73012011-06-30 19:14:33 -0700117static enum sci_status sci_port_get_properties(struct isci_port *iport,
118 struct sci_port_properties *prop)
Dan Williams6f231dd2011-07-02 22:56:22 -0700119{
Dan Williamsffe191c2011-06-29 13:09:25 -0700120 if (!iport || iport->logical_port_index == SCIC_SDS_DUMMY_PORT)
Dan Williamse2f8db52011-05-10 02:28:46 -0700121 return SCI_FAILURE_INVALID_PORT;
Dan Williams6f231dd2011-07-02 22:56:22 -0700122
Dan Williams89a73012011-06-30 19:14:33 -0700123 prop->index = iport->logical_port_index;
124 prop->phy_mask = sci_port_get_phys(iport);
125 sci_port_get_sas_address(iport, &prop->local.sas_address);
126 sci_port_get_protocols(iport, &prop->local.protocols);
127 sci_port_get_attached_sas_address(iport, &prop->remote.sas_address);
Dan Williams6f231dd2011-07-02 22:56:22 -0700128
Dan Williamse2f8db52011-05-10 02:28:46 -0700129 return SCI_SUCCESS;
Dan Williams6f231dd2011-07-02 22:56:22 -0700130}
131
Dan Williams89a73012011-06-30 19:14:33 -0700132static void sci_port_bcn_enable(struct isci_port *iport)
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700133{
Dan Williams85280952011-06-28 15:05:53 -0700134 struct isci_phy *iphy;
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700135 u32 val;
136 int i;
137
Dan Williamsffe191c2011-06-29 13:09:25 -0700138 for (i = 0; i < ARRAY_SIZE(iport->phy_table); i++) {
139 iphy = iport->phy_table[i];
Dan Williams85280952011-06-28 15:05:53 -0700140 if (!iphy)
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700141 continue;
Dan Williams85280952011-06-28 15:05:53 -0700142 val = readl(&iphy->link_layer_registers->link_layer_control);
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700143 /* clear the bit by writing 1. */
Dan Williams85280952011-06-28 15:05:53 -0700144 writel(val, &iphy->link_layer_registers->link_layer_control);
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700145 }
146}
147
Dan Williamsffe191c2011-06-29 13:09:25 -0700148static void isci_port_bc_change_received(struct isci_host *ihost,
149 struct isci_port *iport,
150 struct isci_phy *iphy)
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700151{
Dan Williams52d74632011-10-27 15:05:37 -0700152 dev_dbg(&ihost->pdev->dev,
153 "%s: isci_phy = %p, sas_phy = %p\n",
154 __func__, iphy, &iphy->sas_phy);
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700155
Dan Williams52d74632011-10-27 15:05:37 -0700156 ihost->sas_ha.notify_port_event(&iphy->sas_phy, PORTE_BROADCAST_RCVD);
Dan Williams89a73012011-06-30 19:14:33 -0700157 sci_port_bcn_enable(iport);
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700158}
159
Dan Williamse2f8db52011-05-10 02:28:46 -0700160static void isci_port_link_up(struct isci_host *isci_host,
Dan Williamsffe191c2011-06-29 13:09:25 -0700161 struct isci_port *iport,
Dan Williams85280952011-06-28 15:05:53 -0700162 struct isci_phy *iphy)
Dan Williams6f231dd2011-07-02 22:56:22 -0700163{
164 unsigned long flags;
Dan Williams89a73012011-06-30 19:14:33 -0700165 struct sci_port_properties properties;
Dan Williams6f231dd2011-07-02 22:56:22 -0700166 unsigned long success = true;
167
Dan Williams85280952011-06-28 15:05:53 -0700168 BUG_ON(iphy->isci_port != NULL);
Bartosz Barcinski6cb4d6b2011-04-12 17:28:43 -0700169
Dan Williamsffe191c2011-06-29 13:09:25 -0700170 iphy->isci_port = iport;
Dan Williams6f231dd2011-07-02 22:56:22 -0700171
172 dev_dbg(&isci_host->pdev->dev,
173 "%s: isci_port = %p\n",
Dan Williamsffe191c2011-06-29 13:09:25 -0700174 __func__, iport);
Dan Williams6f231dd2011-07-02 22:56:22 -0700175
Dan Williams85280952011-06-28 15:05:53 -0700176 spin_lock_irqsave(&iphy->sas_phy.frame_rcvd_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700177
Dan Williams85280952011-06-28 15:05:53 -0700178 isci_port_change_state(iphy->isci_port, isci_starting);
Dan Williams6f231dd2011-07-02 22:56:22 -0700179
Dan Williams89a73012011-06-30 19:14:33 -0700180 sci_port_get_properties(iport, &properties);
Dan Williams6f231dd2011-07-02 22:56:22 -0700181
Dan Williams85280952011-06-28 15:05:53 -0700182 if (iphy->protocol == SCIC_SDS_PHY_PROTOCOL_SATA) {
Dan Williams150fc6f2011-02-25 10:25:21 -0800183 u64 attached_sas_address;
Dan Williams6f231dd2011-07-02 22:56:22 -0700184
Dan Williams85280952011-06-28 15:05:53 -0700185 iphy->sas_phy.oob_mode = SATA_OOB_MODE;
186 iphy->sas_phy.frame_rcvd_size = sizeof(struct dev_to_host_fis);
Dan Williams6f231dd2011-07-02 22:56:22 -0700187
188 /*
189 * For direct-attached SATA devices, the SCI core will
190 * automagically assign a SAS address to the end device
191 * for the purpose of creating a port. This SAS address
192 * will not be the same as assigned to the PHY and needs
Dan Williams89a73012011-06-30 19:14:33 -0700193 * to be obtained from struct sci_port_properties properties.
Dan Williams6f231dd2011-07-02 22:56:22 -0700194 */
Dan Williams150fc6f2011-02-25 10:25:21 -0800195 attached_sas_address = properties.remote.sas_address.high;
196 attached_sas_address <<= 32;
197 attached_sas_address |= properties.remote.sas_address.low;
198 swab64s(&attached_sas_address);
Dan Williams6f231dd2011-07-02 22:56:22 -0700199
Dan Williams85280952011-06-28 15:05:53 -0700200 memcpy(&iphy->sas_phy.attached_sas_addr,
Dan Williams150fc6f2011-02-25 10:25:21 -0800201 &attached_sas_address, sizeof(attached_sas_address));
Dan Williams85280952011-06-28 15:05:53 -0700202 } else if (iphy->protocol == SCIC_SDS_PHY_PROTOCOL_SAS) {
203 iphy->sas_phy.oob_mode = SAS_OOB_MODE;
204 iphy->sas_phy.frame_rcvd_size = sizeof(struct sas_identify_frame);
Dan Williams6f231dd2011-07-02 22:56:22 -0700205
206 /* Copy the attached SAS address from the IAF */
Dan Williams85280952011-06-28 15:05:53 -0700207 memcpy(iphy->sas_phy.attached_sas_addr,
208 iphy->frame_rcvd.iaf.sas_addr, SAS_ADDR_SIZE);
Dan Williams6f231dd2011-07-02 22:56:22 -0700209 } else {
210 dev_err(&isci_host->pdev->dev, "%s: unkown target\n", __func__);
211 success = false;
212 }
213
Dan Williams85280952011-06-28 15:05:53 -0700214 iphy->sas_phy.phy->negotiated_linkrate = sci_phy_linkrate(iphy);
Dan Williams83e51432011-02-18 09:25:13 -0800215
Dan Williams85280952011-06-28 15:05:53 -0700216 spin_unlock_irqrestore(&iphy->sas_phy.frame_rcvd_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700217
218 /* Notify libsas that we have an address frame, if indeed
219 * we've found an SSP, SMP, or STP target */
220 if (success)
Dan Williams85280952011-06-28 15:05:53 -0700221 isci_host->sas_ha.notify_port_event(&iphy->sas_phy,
Dan Williams6f231dd2011-07-02 22:56:22 -0700222 PORTE_BYTES_DMAED);
223}
224
225
226/**
227 * isci_port_link_down() - This function is called by the sci core when a link
228 * becomes inactive.
229 * @isci_host: This parameter specifies the isci host object.
230 * @phy: This parameter specifies the isci phy with the active link.
231 * @port: This parameter specifies the isci port with the active link.
232 *
233 */
Dan Williamse2f8db52011-05-10 02:28:46 -0700234static void isci_port_link_down(struct isci_host *isci_host,
235 struct isci_phy *isci_phy,
236 struct isci_port *isci_port)
Dan Williams6f231dd2011-07-02 22:56:22 -0700237{
238 struct isci_remote_device *isci_device;
239
240 dev_dbg(&isci_host->pdev->dev,
241 "%s: isci_port = %p\n", __func__, isci_port);
242
243 if (isci_port) {
244
245 /* check to see if this is the last phy on this port. */
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700246 if (isci_phy->sas_phy.port &&
247 isci_phy->sas_phy.port->num_phys == 1) {
Jeff Skirvin61aaff42011-06-21 12:16:33 -0700248 /* change the state for all devices on this port. The
249 * next task sent to this device will be returned as
250 * SAS_TASK_UNDELIVERED, and the scsi mid layer will
251 * remove the target
Dan Williams6f231dd2011-07-02 22:56:22 -0700252 */
253 list_for_each_entry(isci_device,
254 &isci_port->remote_dev_list,
255 node) {
256 dev_dbg(&isci_host->pdev->dev,
257 "%s: isci_device = %p\n",
258 __func__, isci_device);
Dan Williams209fae12011-06-13 17:39:44 -0700259 set_bit(IDEV_GONE, &isci_device->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -0700260 }
Jeff Skirvincdd05f02011-09-28 18:47:56 -0700261 isci_port_change_state(isci_port, isci_stopping);
Dan Williams6f231dd2011-07-02 22:56:22 -0700262 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700263 }
264
265 /* Notify libsas of the borken link, this will trigger calls to our
266 * isci_port_deformed and isci_dev_gone functions.
267 */
268 sas_phy_disconnected(&isci_phy->sas_phy);
269 isci_host->sas_ha.notify_phy_event(&isci_phy->sas_phy,
270 PHYE_LOSS_OF_SIGNAL);
271
272 isci_phy->isci_port = NULL;
273
274 dev_dbg(&isci_host->pdev->dev,
275 "%s: isci_port = %p - Done\n", __func__, isci_port);
276}
277
278
279/**
Dan Williams6f231dd2011-07-02 22:56:22 -0700280 * isci_port_ready() - This function is called by the sci core when a link
281 * becomes ready.
282 * @isci_host: This parameter specifies the isci host object.
283 * @port: This parameter specifies the sci port with the active link.
284 *
285 */
Dan Williamse2f8db52011-05-10 02:28:46 -0700286static void isci_port_ready(struct isci_host *isci_host, struct isci_port *isci_port)
Dan Williams6f231dd2011-07-02 22:56:22 -0700287{
288 dev_dbg(&isci_host->pdev->dev,
289 "%s: isci_port = %p\n", __func__, isci_port);
290
291 complete_all(&isci_port->start_complete);
292 isci_port_change_state(isci_port, isci_ready);
293 return;
294}
295
296/**
297 * isci_port_not_ready() - This function is called by the sci core when a link
298 * is not ready. All remote devices on this link will be removed if they are
299 * in the stopping state.
300 * @isci_host: This parameter specifies the isci host object.
301 * @port: This parameter specifies the sci port with the active link.
302 *
303 */
Dan Williamse2f8db52011-05-10 02:28:46 -0700304static void isci_port_not_ready(struct isci_host *isci_host, struct isci_port *isci_port)
Dan Williams6f231dd2011-07-02 22:56:22 -0700305{
306 dev_dbg(&isci_host->pdev->dev,
307 "%s: isci_port = %p\n", __func__, isci_port);
308}
309
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700310static void isci_port_stop_complete(struct isci_host *ihost,
Dan Williamsffe191c2011-06-29 13:09:25 -0700311 struct isci_port *iport,
Dan Williamse2f8db52011-05-10 02:28:46 -0700312 enum sci_status completion_status)
313{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700314 dev_dbg(&ihost->pdev->dev, "Port stop complete\n");
Dan Williamse2f8db52011-05-10 02:28:46 -0700315}
316
Jeff Skirvin8e35a132011-10-27 15:05:32 -0700317
318static bool is_port_ready_state(enum sci_port_states state)
319{
320 switch (state) {
321 case SCI_PORT_READY:
322 case SCI_PORT_SUB_WAITING:
323 case SCI_PORT_SUB_OPERATIONAL:
324 case SCI_PORT_SUB_CONFIGURING:
325 return true;
326 default:
327 return false;
328 }
329}
330
331/* flag dummy rnc hanling when exiting a ready state */
332static void port_state_machine_change(struct isci_port *iport,
333 enum sci_port_states state)
334{
335 struct sci_base_state_machine *sm = &iport->sm;
336 enum sci_port_states old_state = sm->current_state_id;
337
338 if (is_port_ready_state(old_state) && !is_port_ready_state(state))
339 iport->ready_exit = true;
340
341 sci_change_state(sm, state);
342 iport->ready_exit = false;
343}
344
Dan Williams6f231dd2011-07-02 22:56:22 -0700345/**
346 * isci_port_hard_reset_complete() - This function is called by the sci core
347 * when the hard reset complete notification has been received.
348 * @port: This parameter specifies the sci port with the active link.
349 * @completion_status: This parameter specifies the core status for the reset
350 * process.
351 *
352 */
Dan Williamse2f8db52011-05-10 02:28:46 -0700353static void isci_port_hard_reset_complete(struct isci_port *isci_port,
354 enum sci_status completion_status)
Dan Williams6f231dd2011-07-02 22:56:22 -0700355{
356 dev_dbg(&isci_port->isci_host->pdev->dev,
357 "%s: isci_port = %p, completion_status=%x\n",
358 __func__, isci_port, completion_status);
359
360 /* Save the status of the hard reset from the port. */
361 isci_port->hard_reset_status = completion_status;
362
Jeff Skirvin8e35a132011-10-27 15:05:32 -0700363 if (completion_status != SCI_SUCCESS) {
364
365 /* The reset failed. The port state is now SCI_PORT_FAILED. */
366 if (isci_port->active_phy_mask == 0) {
367
368 /* Generate the link down now to the host, since it
369 * was intercepted by the hard reset state machine when
370 * it really happened.
371 */
372 isci_port_link_down(isci_port->isci_host,
373 &isci_port->isci_host->phys[
374 isci_port->last_active_phy],
375 isci_port);
376 }
377 /* Advance the port state so that link state changes will be
378 * noticed.
379 */
380 port_state_machine_change(isci_port, SCI_PORT_SUB_WAITING);
381
382 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700383 complete_all(&isci_port->hard_reset_complete);
384}
Dan Williams4393aa42011-03-31 13:10:44 -0700385
Dan Williamse2f8db52011-05-10 02:28:46 -0700386/* This method will return a true value if the specified phy can be assigned to
387 * this port The following is a list of phys for each port that are allowed: -
388 * Port 0 - 3 2 1 0 - Port 1 - 1 - Port 2 - 3 2 - Port 3 - 3 This method
389 * doesn't preclude all configurations. It merely ensures that a phy is part
390 * of the allowable set of phy identifiers for that port. For example, one
391 * could assign phy 3 to port 0 and no other phys. Please refer to
Dan Williams89a73012011-06-30 19:14:33 -0700392 * sci_port_is_phy_mask_valid() for information regarding whether the
Dan Williamse2f8db52011-05-10 02:28:46 -0700393 * phy_mask for a port can be supported. bool true if this is a valid phy
394 * assignment for the port false if this is not a valid phy assignment for the
395 * port
396 */
Dan Williams89a73012011-06-30 19:14:33 -0700397bool sci_port_is_valid_phy_assignment(struct isci_port *iport, u32 phy_index)
Dan Williamse2f8db52011-05-10 02:28:46 -0700398{
Dan Williams89a73012011-06-30 19:14:33 -0700399 struct isci_host *ihost = iport->owning_controller;
400 struct sci_user_parameters *user = &ihost->user_parameters;
401
Dan Williamse2f8db52011-05-10 02:28:46 -0700402 /* Initialize to invalid value. */
403 u32 existing_phy_index = SCI_MAX_PHYS;
404 u32 index;
405
Dan Williams89a73012011-06-30 19:14:33 -0700406 if ((iport->physical_port_index == 1) && (phy_index != 1))
Dan Williamse2f8db52011-05-10 02:28:46 -0700407 return false;
Dan Williamse2f8db52011-05-10 02:28:46 -0700408
Dan Williams89a73012011-06-30 19:14:33 -0700409 if (iport->physical_port_index == 3 && phy_index != 3)
Dan Williamse2f8db52011-05-10 02:28:46 -0700410 return false;
Dan Williamse2f8db52011-05-10 02:28:46 -0700411
Dan Williams89a73012011-06-30 19:14:33 -0700412 if (iport->physical_port_index == 2 &&
413 (phy_index == 0 || phy_index == 1))
Dan Williamse2f8db52011-05-10 02:28:46 -0700414 return false;
Dan Williamse2f8db52011-05-10 02:28:46 -0700415
Dan Williams89a73012011-06-30 19:14:33 -0700416 for (index = 0; index < SCI_MAX_PHYS; index++)
417 if (iport->phy_table[index] && index != phy_index)
Dan Williamse2f8db52011-05-10 02:28:46 -0700418 existing_phy_index = index;
Dan Williamse2f8db52011-05-10 02:28:46 -0700419
Dan Williams89a73012011-06-30 19:14:33 -0700420 /* Ensure that all of the phys in the port are capable of
421 * operating at the same maximum link rate.
422 */
423 if (existing_phy_index < SCI_MAX_PHYS &&
424 user->phys[phy_index].max_speed_generation !=
425 user->phys[existing_phy_index].max_speed_generation)
Dan Williamse2f8db52011-05-10 02:28:46 -0700426 return false;
427
428 return true;
429}
430
431/**
432 *
433 * @sci_port: This is the port object for which to determine if the phy mask
434 * can be supported.
435 *
436 * This method will return a true value if the port's phy mask can be supported
437 * by the SCU. The following is a list of valid PHY mask configurations for
438 * each port: - Port 0 - [[3 2] 1] 0 - Port 1 - [1] - Port 2 - [[3] 2]
439 * - Port 3 - [3] This method returns a boolean indication specifying if the
440 * phy mask can be supported. true if this is a valid phy assignment for the
441 * port false if this is not a valid phy assignment for the port
442 */
Dan Williams89a73012011-06-30 19:14:33 -0700443static bool sci_port_is_phy_mask_valid(
Dan Williamsffe191c2011-06-29 13:09:25 -0700444 struct isci_port *iport,
Dan Williamse2f8db52011-05-10 02:28:46 -0700445 u32 phy_mask)
446{
Dan Williamsffe191c2011-06-29 13:09:25 -0700447 if (iport->physical_port_index == 0) {
Dan Williamse2f8db52011-05-10 02:28:46 -0700448 if (((phy_mask & 0x0F) == 0x0F)
449 || ((phy_mask & 0x03) == 0x03)
450 || ((phy_mask & 0x01) == 0x01)
451 || (phy_mask == 0))
452 return true;
Dan Williamsffe191c2011-06-29 13:09:25 -0700453 } else if (iport->physical_port_index == 1) {
Dan Williamse2f8db52011-05-10 02:28:46 -0700454 if (((phy_mask & 0x02) == 0x02)
455 || (phy_mask == 0))
456 return true;
Dan Williamsffe191c2011-06-29 13:09:25 -0700457 } else if (iport->physical_port_index == 2) {
Dan Williamse2f8db52011-05-10 02:28:46 -0700458 if (((phy_mask & 0x0C) == 0x0C)
459 || ((phy_mask & 0x04) == 0x04)
460 || (phy_mask == 0))
461 return true;
Dan Williamsffe191c2011-06-29 13:09:25 -0700462 } else if (iport->physical_port_index == 3) {
Dan Williamse2f8db52011-05-10 02:28:46 -0700463 if (((phy_mask & 0x08) == 0x08)
464 || (phy_mask == 0))
465 return true;
466 }
467
468 return false;
469}
470
Dan Williams85280952011-06-28 15:05:53 -0700471/*
Dan Williamse2f8db52011-05-10 02:28:46 -0700472 * This method retrieves a currently active (i.e. connected) phy contained in
473 * the port. Currently, the lowest order phy that is connected is returned.
474 * This method returns a pointer to a SCIS_SDS_PHY object. NULL This value is
475 * returned if there are no currently active (i.e. connected to a remote end
Dan Williams89a73012011-06-30 19:14:33 -0700476 * point) phys contained in the port. All other values specify a struct sci_phy
Dan Williamse2f8db52011-05-10 02:28:46 -0700477 * object that is active in the port.
478 */
Dan Williams89a73012011-06-30 19:14:33 -0700479static struct isci_phy *sci_port_get_a_connected_phy(struct isci_port *iport)
Dan Williams85280952011-06-28 15:05:53 -0700480{
Dan Williamse2f8db52011-05-10 02:28:46 -0700481 u32 index;
Dan Williams85280952011-06-28 15:05:53 -0700482 struct isci_phy *iphy;
Dan Williamse2f8db52011-05-10 02:28:46 -0700483
484 for (index = 0; index < SCI_MAX_PHYS; index++) {
Dan Williams85280952011-06-28 15:05:53 -0700485 /* Ensure that the phy is both part of the port and currently
486 * connected to the remote end-point.
487 */
Dan Williamsffe191c2011-06-29 13:09:25 -0700488 iphy = iport->phy_table[index];
Dan Williams89a73012011-06-30 19:14:33 -0700489 if (iphy && sci_port_active_phy(iport, iphy))
Dan Williams85280952011-06-28 15:05:53 -0700490 return iphy;
Dan Williamse2f8db52011-05-10 02:28:46 -0700491 }
492
493 return NULL;
494}
495
Dan Williams89a73012011-06-30 19:14:33 -0700496static enum sci_status sci_port_set_phy(struct isci_port *iport, struct isci_phy *iphy)
Dan Williamse2f8db52011-05-10 02:28:46 -0700497{
Dan Williams85280952011-06-28 15:05:53 -0700498 /* Check to see if we can add this phy to a port
Dan Williamse2f8db52011-05-10 02:28:46 -0700499 * that means that the phy is not part of a port and that the port does
Dan Williams85280952011-06-28 15:05:53 -0700500 * not already have a phy assinged to the phy index.
501 */
Dan Williamsffe191c2011-06-29 13:09:25 -0700502 if (!iport->phy_table[iphy->phy_index] &&
Dan Williams85280952011-06-28 15:05:53 -0700503 !phy_get_non_dummy_port(iphy) &&
Dan Williams89a73012011-06-30 19:14:33 -0700504 sci_port_is_valid_phy_assignment(iport, iphy->phy_index)) {
Dan Williams85280952011-06-28 15:05:53 -0700505 /* Phy is being added in the stopped state so we are in MPC mode
506 * make logical port index = physical port index
507 */
Dan Williamsffe191c2011-06-29 13:09:25 -0700508 iport->logical_port_index = iport->physical_port_index;
509 iport->phy_table[iphy->phy_index] = iphy;
Dan Williams89a73012011-06-30 19:14:33 -0700510 sci_phy_set_port(iphy, iport);
Dan Williamse2f8db52011-05-10 02:28:46 -0700511
512 return SCI_SUCCESS;
513 }
514
515 return SCI_FAILURE;
516}
517
Dan Williams89a73012011-06-30 19:14:33 -0700518static enum sci_status sci_port_clear_phy(struct isci_port *iport, struct isci_phy *iphy)
Dan Williamse2f8db52011-05-10 02:28:46 -0700519{
520 /* Make sure that this phy is part of this port */
Dan Williamsffe191c2011-06-29 13:09:25 -0700521 if (iport->phy_table[iphy->phy_index] == iphy &&
522 phy_get_non_dummy_port(iphy) == iport) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700523 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -0700524
525 /* Yep it is assigned to this port so remove it */
Dan Williams89a73012011-06-30 19:14:33 -0700526 sci_phy_set_port(iphy, &ihost->ports[SCI_MAX_PORTS]);
Dan Williamsffe191c2011-06-29 13:09:25 -0700527 iport->phy_table[iphy->phy_index] = NULL;
Dan Williamse2f8db52011-05-10 02:28:46 -0700528 return SCI_SUCCESS;
529 }
530
531 return SCI_FAILURE;
532}
533
Dan Williams89a73012011-06-30 19:14:33 -0700534void sci_port_get_sas_address(struct isci_port *iport, struct sci_sas_address *sas)
Dan Williamse2f8db52011-05-10 02:28:46 -0700535{
536 u32 index;
537
Dan Williams89a73012011-06-30 19:14:33 -0700538 sas->high = 0;
539 sas->low = 0;
540 for (index = 0; index < SCI_MAX_PHYS; index++)
541 if (iport->phy_table[index])
542 sci_phy_get_sas_address(iport->phy_table[index], sas);
Dan Williamse2f8db52011-05-10 02:28:46 -0700543}
544
Dan Williams89a73012011-06-30 19:14:33 -0700545void sci_port_get_attached_sas_address(struct isci_port *iport, struct sci_sas_address *sas)
Dan Williamse2f8db52011-05-10 02:28:46 -0700546{
Dan Williams85280952011-06-28 15:05:53 -0700547 struct isci_phy *iphy;
Dan Williamse2f8db52011-05-10 02:28:46 -0700548
549 /*
550 * Ensure that the phy is both part of the port and currently
551 * connected to the remote end-point.
552 */
Dan Williams89a73012011-06-30 19:14:33 -0700553 iphy = sci_port_get_a_connected_phy(iport);
Dan Williams85280952011-06-28 15:05:53 -0700554 if (iphy) {
555 if (iphy->protocol != SCIC_SDS_PHY_PROTOCOL_SATA) {
Dan Williams89a73012011-06-30 19:14:33 -0700556 sci_phy_get_attached_sas_address(iphy, sas);
Dan Williamse2f8db52011-05-10 02:28:46 -0700557 } else {
Dan Williams89a73012011-06-30 19:14:33 -0700558 sci_phy_get_sas_address(iphy, sas);
559 sas->low += iphy->phy_index;
Dan Williamse2f8db52011-05-10 02:28:46 -0700560 }
561 } else {
Dan Williams89a73012011-06-30 19:14:33 -0700562 sas->high = 0;
563 sas->low = 0;
Dan Williamse2f8db52011-05-10 02:28:46 -0700564 }
565}
566
567/**
Dan Williams89a73012011-06-30 19:14:33 -0700568 * sci_port_construct_dummy_rnc() - create dummy rnc for si workaround
Dan Williamse2f8db52011-05-10 02:28:46 -0700569 *
570 * @sci_port: logical port on which we need to create the remote node context
571 * @rni: remote node index for this remote node context.
572 *
573 * This routine will construct a dummy remote node context data structure
574 * This structure will be posted to the hardware to work around a scheduler
575 * error in the hardware.
576 */
Dan Williams89a73012011-06-30 19:14:33 -0700577static void sci_port_construct_dummy_rnc(struct isci_port *iport, u16 rni)
Dan Williamse2f8db52011-05-10 02:28:46 -0700578{
579 union scu_remote_node_context *rnc;
580
Dan Williamsffe191c2011-06-29 13:09:25 -0700581 rnc = &iport->owning_controller->remote_node_context_table[rni];
Dan Williamse2f8db52011-05-10 02:28:46 -0700582
583 memset(rnc, 0, sizeof(union scu_remote_node_context));
584
585 rnc->ssp.remote_sas_address_hi = 0;
586 rnc->ssp.remote_sas_address_lo = 0;
587
588 rnc->ssp.remote_node_index = rni;
589 rnc->ssp.remote_node_port_width = 1;
Dan Williamsffe191c2011-06-29 13:09:25 -0700590 rnc->ssp.logical_port_index = iport->physical_port_index;
Dan Williamse2f8db52011-05-10 02:28:46 -0700591
592 rnc->ssp.nexus_loss_timer_enable = false;
593 rnc->ssp.check_bit = false;
594 rnc->ssp.is_valid = true;
595 rnc->ssp.is_remote_node_context = true;
596 rnc->ssp.function_number = 0;
597 rnc->ssp.arbitration_wait_time = 0;
598}
599
Dan Williamsdd047c82011-06-09 11:06:58 -0700600/*
601 * construct a dummy task context data structure. This
Dan Williamse2f8db52011-05-10 02:28:46 -0700602 * structure will be posted to the hardwre to work around a scheduler error
603 * in the hardware.
Dan Williamse2f8db52011-05-10 02:28:46 -0700604 */
Dan Williams89a73012011-06-30 19:14:33 -0700605static void sci_port_construct_dummy_task(struct isci_port *iport, u16 tag)
Dan Williamse2f8db52011-05-10 02:28:46 -0700606{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700607 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -0700608 struct scu_task_context *task_context;
609
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700610 task_context = &ihost->task_context_table[ISCI_TAG_TCI(tag)];
Dan Williamse2f8db52011-05-10 02:28:46 -0700611 memset(task_context, 0, sizeof(struct scu_task_context));
612
Dan Williamse2f8db52011-05-10 02:28:46 -0700613 task_context->initiator_request = 1;
614 task_context->connection_rate = 1;
Dan Williamsffe191c2011-06-29 13:09:25 -0700615 task_context->logical_port_index = iport->physical_port_index;
Dan Williamse2f8db52011-05-10 02:28:46 -0700616 task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SSP;
Dan Williamsdd047c82011-06-09 11:06:58 -0700617 task_context->task_index = ISCI_TAG_TCI(tag);
Dan Williamse2f8db52011-05-10 02:28:46 -0700618 task_context->valid = SCU_TASK_CONTEXT_VALID;
619 task_context->context_type = SCU_TASK_CONTEXT_TYPE;
Dan Williamsffe191c2011-06-29 13:09:25 -0700620 task_context->remote_node_index = iport->reserved_rni;
Dan Williamse2f8db52011-05-10 02:28:46 -0700621 task_context->do_not_dma_ssp_good_response = 1;
Dan Williamse2f8db52011-05-10 02:28:46 -0700622 task_context->task_phase = 0x01;
623}
624
Dan Williams89a73012011-06-30 19:14:33 -0700625static void sci_port_destroy_dummy_resources(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -0700626{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700627 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -0700628
Dan Williamsffe191c2011-06-29 13:09:25 -0700629 if (iport->reserved_tag != SCI_CONTROLLER_INVALID_IO_TAG)
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700630 isci_free_tag(ihost, iport->reserved_tag);
Dan Williamse2f8db52011-05-10 02:28:46 -0700631
Dan Williamsffe191c2011-06-29 13:09:25 -0700632 if (iport->reserved_rni != SCU_DUMMY_INDEX)
Dan Williams89a73012011-06-30 19:14:33 -0700633 sci_remote_node_table_release_remote_node_index(&ihost->available_remote_nodes,
Dan Williamsffe191c2011-06-29 13:09:25 -0700634 1, iport->reserved_rni);
Dan Williamse2f8db52011-05-10 02:28:46 -0700635
Dan Williamsffe191c2011-06-29 13:09:25 -0700636 iport->reserved_rni = SCU_DUMMY_INDEX;
637 iport->reserved_tag = SCI_CONTROLLER_INVALID_IO_TAG;
Dan Williamse2f8db52011-05-10 02:28:46 -0700638}
639
Dan Williams89a73012011-06-30 19:14:33 -0700640void sci_port_setup_transports(struct isci_port *iport, u32 device_id)
Dan Williamse2f8db52011-05-10 02:28:46 -0700641{
642 u8 index;
643
644 for (index = 0; index < SCI_MAX_PHYS; index++) {
Dan Williamsffe191c2011-06-29 13:09:25 -0700645 if (iport->active_phy_mask & (1 << index))
Dan Williams89a73012011-06-30 19:14:33 -0700646 sci_phy_setup_transport(iport->phy_table[index], device_id);
Dan Williamse2f8db52011-05-10 02:28:46 -0700647 }
648}
649
Dan Williams89a73012011-06-30 19:14:33 -0700650static void sci_port_activate_phy(struct isci_port *iport, struct isci_phy *iphy,
651 bool do_notify_user)
Dan Williamse2f8db52011-05-10 02:28:46 -0700652{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700653 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -0700654
Dan Williams85280952011-06-28 15:05:53 -0700655 if (iphy->protocol != SCIC_SDS_PHY_PROTOCOL_SATA)
Dan Williams89a73012011-06-30 19:14:33 -0700656 sci_phy_resume(iphy);
Dan Williamse2f8db52011-05-10 02:28:46 -0700657
Dan Williamsffe191c2011-06-29 13:09:25 -0700658 iport->active_phy_mask |= 1 << iphy->phy_index;
Dan Williamse2f8db52011-05-10 02:28:46 -0700659
Dan Williams89a73012011-06-30 19:14:33 -0700660 sci_controller_clear_invalid_phy(ihost, iphy);
Dan Williamse2f8db52011-05-10 02:28:46 -0700661
662 if (do_notify_user == true)
Dan Williamsffe191c2011-06-29 13:09:25 -0700663 isci_port_link_up(ihost, iport, iphy);
Dan Williamse2f8db52011-05-10 02:28:46 -0700664}
665
Dan Williams89a73012011-06-30 19:14:33 -0700666void sci_port_deactivate_phy(struct isci_port *iport, struct isci_phy *iphy,
667 bool do_notify_user)
Dan Williamse2f8db52011-05-10 02:28:46 -0700668{
Dan Williams34a99152011-07-01 02:25:15 -0700669 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -0700670
Dan Williamsffe191c2011-06-29 13:09:25 -0700671 iport->active_phy_mask &= ~(1 << iphy->phy_index);
Jeff Skirvin8e35a132011-10-27 15:05:32 -0700672 if (!iport->active_phy_mask)
673 iport->last_active_phy = iphy->phy_index;
Dan Williamse2f8db52011-05-10 02:28:46 -0700674
Dan Williams85280952011-06-28 15:05:53 -0700675 iphy->max_negotiated_speed = SAS_LINK_RATE_UNKNOWN;
Dan Williamse2f8db52011-05-10 02:28:46 -0700676
677 /* Re-assign the phy back to the LP as if it were a narrow port */
Dan Williams85280952011-06-28 15:05:53 -0700678 writel(iphy->phy_index,
Dan Williamsffe191c2011-06-29 13:09:25 -0700679 &iport->port_pe_configuration_register[iphy->phy_index]);
Dan Williamse2f8db52011-05-10 02:28:46 -0700680
681 if (do_notify_user == true)
682 isci_port_link_down(ihost, iphy, iport);
683}
684
Dan Williams89a73012011-06-30 19:14:33 -0700685static void sci_port_invalid_link_up(struct isci_port *iport, struct isci_phy *iphy)
Dan Williamse2f8db52011-05-10 02:28:46 -0700686{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700687 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -0700688
689 /*
690 * Check to see if we have alreay reported this link as bad and if
691 * not go ahead and tell the SCI_USER that we have discovered an
692 * invalid link.
693 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700694 if ((ihost->invalid_phy_mask & (1 << iphy->phy_index)) == 0) {
Dan Williams34a99152011-07-01 02:25:15 -0700695 ihost->invalid_phy_mask |= 1 << iphy->phy_index;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700696 dev_warn(&ihost->pdev->dev, "Invalid link up!\n");
Dan Williamse2f8db52011-05-10 02:28:46 -0700697 }
698}
699
700/**
Dan Williams89a73012011-06-30 19:14:33 -0700701 * sci_port_general_link_up_handler - phy can be assigned to port?
702 * @sci_port: sci_port object for which has a phy that has gone link up.
Dan Williams85280952011-06-28 15:05:53 -0700703 * @sci_phy: This is the struct isci_phy object that has gone link up.
Dan Williamse2f8db52011-05-10 02:28:46 -0700704 * @do_notify_user: This parameter specifies whether to inform the user (via
Dan Williams89a73012011-06-30 19:14:33 -0700705 * sci_port_link_up()) as to the fact that a new phy as become ready.
Dan Williamse2f8db52011-05-10 02:28:46 -0700706 *
707 * Determine if this phy can be assigned to this
708 * port . If the phy is not a valid PHY for
709 * this port then the function will notify the user. A PHY can only be
710 * part of a port if it's attached SAS ADDRESS is the same as all other PHYs in
711 * the same port. none
712 */
Dan Williams89a73012011-06-30 19:14:33 -0700713static void sci_port_general_link_up_handler(struct isci_port *iport,
Dan Williams85280952011-06-28 15:05:53 -0700714 struct isci_phy *iphy,
Dan Williamse2f8db52011-05-10 02:28:46 -0700715 bool do_notify_user)
716{
717 struct sci_sas_address port_sas_address;
718 struct sci_sas_address phy_sas_address;
719
Dan Williams89a73012011-06-30 19:14:33 -0700720 sci_port_get_attached_sas_address(iport, &port_sas_address);
721 sci_phy_get_attached_sas_address(iphy, &phy_sas_address);
Dan Williamse2f8db52011-05-10 02:28:46 -0700722
723 /* If the SAS address of the new phy matches the SAS address of
724 * other phys in the port OR this is the first phy in the port,
725 * then activate the phy and allow it to be used for operations
726 * in this port.
727 */
728 if ((phy_sas_address.high == port_sas_address.high &&
729 phy_sas_address.low == port_sas_address.low) ||
Dan Williamsffe191c2011-06-29 13:09:25 -0700730 iport->active_phy_mask == 0) {
731 struct sci_base_state_machine *sm = &iport->sm;
Dan Williamse2f8db52011-05-10 02:28:46 -0700732
Dan Williams89a73012011-06-30 19:14:33 -0700733 sci_port_activate_phy(iport, iphy, do_notify_user);
Edmund Nadolskie3013702011-06-02 00:10:43 +0000734 if (sm->current_state_id == SCI_PORT_RESETTING)
Dan Williamsffe191c2011-06-29 13:09:25 -0700735 port_state_machine_change(iport, SCI_PORT_READY);
Dan Williamse2f8db52011-05-10 02:28:46 -0700736 } else
Dan Williams89a73012011-06-30 19:14:33 -0700737 sci_port_invalid_link_up(iport, iphy);
Dan Williamse2f8db52011-05-10 02:28:46 -0700738}
739
740
741
742/**
743 * This method returns false if the port only has a single phy object assigned.
744 * If there are no phys or more than one phy then the method will return
745 * true.
746 * @sci_port: The port for which the wide port condition is to be checked.
747 *
748 * bool true Is returned if this is a wide ported port. false Is returned if
749 * this is a narrow port.
750 */
Dan Williams89a73012011-06-30 19:14:33 -0700751static bool sci_port_is_wide(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -0700752{
753 u32 index;
754 u32 phy_count = 0;
755
756 for (index = 0; index < SCI_MAX_PHYS; index++) {
Dan Williamsffe191c2011-06-29 13:09:25 -0700757 if (iport->phy_table[index] != NULL) {
Dan Williamse2f8db52011-05-10 02:28:46 -0700758 phy_count++;
759 }
760 }
761
762 return phy_count != 1;
763}
764
765/**
766 * This method is called by the PHY object when the link is detected. if the
767 * port wants the PHY to continue on to the link up state then the port
768 * layer must return true. If the port object returns false the phy object
769 * must halt its attempt to go link up.
770 * @sci_port: The port associated with the phy object.
771 * @sci_phy: The phy object that is trying to go link up.
772 *
773 * true if the phy object can continue to the link up condition. true Is
774 * returned if this phy can continue to the ready state. false Is returned if
775 * can not continue on to the ready state. This notification is in place for
776 * wide ports and direct attached phys. Since there are no wide ported SATA
777 * devices this could become an invalid port configuration.
778 */
Dan Williams89a73012011-06-30 19:14:33 -0700779bool sci_port_link_detected(
Dan Williamsffe191c2011-06-29 13:09:25 -0700780 struct isci_port *iport,
Dan Williams85280952011-06-28 15:05:53 -0700781 struct isci_phy *iphy)
Dan Williamse2f8db52011-05-10 02:28:46 -0700782{
Dan Williamsffe191c2011-06-29 13:09:25 -0700783 if ((iport->logical_port_index != SCIC_SDS_DUMMY_PORT) &&
Dan Williams85280952011-06-28 15:05:53 -0700784 (iphy->protocol == SCIC_SDS_PHY_PROTOCOL_SATA) &&
Dan Williams89a73012011-06-30 19:14:33 -0700785 sci_port_is_wide(iport)) {
786 sci_port_invalid_link_up(iport, iphy);
Dan Williamse2f8db52011-05-10 02:28:46 -0700787
788 return false;
789 }
790
791 return true;
792}
793
Edmund Nadolski5553ba22011-05-19 11:59:10 +0000794static void port_timeout(unsigned long data)
Dan Williamse2f8db52011-05-10 02:28:46 -0700795{
Edmund Nadolski5553ba22011-05-19 11:59:10 +0000796 struct sci_timer *tmr = (struct sci_timer *)data;
Dan Williamsffe191c2011-06-29 13:09:25 -0700797 struct isci_port *iport = container_of(tmr, typeof(*iport), timer);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700798 struct isci_host *ihost = iport->owning_controller;
Edmund Nadolski5553ba22011-05-19 11:59:10 +0000799 unsigned long flags;
Dan Williamse2f8db52011-05-10 02:28:46 -0700800 u32 current_state;
801
Edmund Nadolski5553ba22011-05-19 11:59:10 +0000802 spin_lock_irqsave(&ihost->scic_lock, flags);
803
804 if (tmr->cancel)
805 goto done;
806
Dan Williamsffe191c2011-06-29 13:09:25 -0700807 current_state = iport->sm.current_state_id;
Dan Williamse2f8db52011-05-10 02:28:46 -0700808
Edmund Nadolskie3013702011-06-02 00:10:43 +0000809 if (current_state == SCI_PORT_RESETTING) {
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000810 /* if the port is still in the resetting state then the timeout
811 * fired before the reset completed.
Dan Williamse2f8db52011-05-10 02:28:46 -0700812 */
Dan Williamsffe191c2011-06-29 13:09:25 -0700813 port_state_machine_change(iport, SCI_PORT_FAILED);
Edmund Nadolskie3013702011-06-02 00:10:43 +0000814 } else if (current_state == SCI_PORT_STOPPED) {
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000815 /* if the port is stopped then the start request failed In this
816 * case stay in the stopped state.
Dan Williamse2f8db52011-05-10 02:28:46 -0700817 */
Dan Williamsffe191c2011-06-29 13:09:25 -0700818 dev_err(sciport_to_dev(iport),
Dan Williamse2f8db52011-05-10 02:28:46 -0700819 "%s: SCIC Port 0x%p failed to stop before tiemout.\n",
820 __func__,
Dan Williamsffe191c2011-06-29 13:09:25 -0700821 iport);
Edmund Nadolskie3013702011-06-02 00:10:43 +0000822 } else if (current_state == SCI_PORT_STOPPING) {
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000823 /* if the port is still stopping then the stop has not completed */
Dan Williamsffe191c2011-06-29 13:09:25 -0700824 isci_port_stop_complete(iport->owning_controller,
825 iport,
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000826 SCI_FAILURE_TIMEOUT);
Dan Williamse2f8db52011-05-10 02:28:46 -0700827 } else {
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000828 /* The port is in the ready state and we have a timer
Dan Williamse2f8db52011-05-10 02:28:46 -0700829 * reporting a timeout this should not happen.
830 */
Dan Williamsffe191c2011-06-29 13:09:25 -0700831 dev_err(sciport_to_dev(iport),
Dan Williamse2f8db52011-05-10 02:28:46 -0700832 "%s: SCIC Port 0x%p is processing a timeout operation "
Dan Williamsffe191c2011-06-29 13:09:25 -0700833 "in state %d.\n", __func__, iport, current_state);
Dan Williamse2f8db52011-05-10 02:28:46 -0700834 }
Edmund Nadolski5553ba22011-05-19 11:59:10 +0000835
836done:
837 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williamse2f8db52011-05-10 02:28:46 -0700838}
839
840/* --------------------------------------------------------------------------- */
841
842/**
843 * This function updates the hardwares VIIT entry for this port.
844 *
845 *
846 */
Dan Williams89a73012011-06-30 19:14:33 -0700847static void sci_port_update_viit_entry(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -0700848{
849 struct sci_sas_address sas_address;
850
Dan Williams89a73012011-06-30 19:14:33 -0700851 sci_port_get_sas_address(iport, &sas_address);
Dan Williamse2f8db52011-05-10 02:28:46 -0700852
853 writel(sas_address.high,
Dan Williamsffe191c2011-06-29 13:09:25 -0700854 &iport->viit_registers->initiator_sas_address_hi);
Dan Williamse2f8db52011-05-10 02:28:46 -0700855 writel(sas_address.low,
Dan Williamsffe191c2011-06-29 13:09:25 -0700856 &iport->viit_registers->initiator_sas_address_lo);
Dan Williamse2f8db52011-05-10 02:28:46 -0700857
858 /* This value get cleared just in case its not already cleared */
Dan Williamsffe191c2011-06-29 13:09:25 -0700859 writel(0, &iport->viit_registers->reserved);
Dan Williamse2f8db52011-05-10 02:28:46 -0700860
861 /* We are required to update the status register last */
862 writel(SCU_VIIT_ENTRY_ID_VIIT |
863 SCU_VIIT_IPPT_INITIATOR |
Dan Williamsffe191c2011-06-29 13:09:25 -0700864 ((1 << iport->physical_port_index) << SCU_VIIT_ENTRY_LPVIE_SHIFT) |
Dan Williamse2f8db52011-05-10 02:28:46 -0700865 SCU_VIIT_STATUS_ALL_VALID,
Dan Williamsffe191c2011-06-29 13:09:25 -0700866 &iport->viit_registers->status);
Dan Williamse2f8db52011-05-10 02:28:46 -0700867}
868
Dan Williams89a73012011-06-30 19:14:33 -0700869enum sas_linkrate sci_port_get_max_allowed_speed(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -0700870{
871 u16 index;
Dan Williams85280952011-06-28 15:05:53 -0700872 struct isci_phy *iphy;
Dan Williamse2f8db52011-05-10 02:28:46 -0700873 enum sas_linkrate max_allowed_speed = SAS_LINK_RATE_6_0_GBPS;
Dan Williamse2f8db52011-05-10 02:28:46 -0700874
875 /*
876 * Loop through all of the phys in this port and find the phy with the
877 * lowest maximum link rate. */
878 for (index = 0; index < SCI_MAX_PHYS; index++) {
Dan Williamsffe191c2011-06-29 13:09:25 -0700879 iphy = iport->phy_table[index];
Dan Williams89a73012011-06-30 19:14:33 -0700880 if (iphy && sci_port_active_phy(iport, iphy) &&
Dan Williams85280952011-06-28 15:05:53 -0700881 iphy->max_negotiated_speed < max_allowed_speed)
882 max_allowed_speed = iphy->max_negotiated_speed;
Dan Williamse2f8db52011-05-10 02:28:46 -0700883 }
884
885 return max_allowed_speed;
886}
887
Dan Williams89a73012011-06-30 19:14:33 -0700888static void sci_port_suspend_port_task_scheduler(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -0700889{
890 u32 pts_control_value;
891
Dan Williamsffe191c2011-06-29 13:09:25 -0700892 pts_control_value = readl(&iport->port_task_scheduler_registers->control);
Dan Williamse2f8db52011-05-10 02:28:46 -0700893 pts_control_value |= SCU_PTSxCR_GEN_BIT(SUSPEND);
Dan Williamsffe191c2011-06-29 13:09:25 -0700894 writel(pts_control_value, &iport->port_task_scheduler_registers->control);
Dan Williamse2f8db52011-05-10 02:28:46 -0700895}
896
897/**
Dan Williams89a73012011-06-30 19:14:33 -0700898 * sci_port_post_dummy_request() - post dummy/workaround request
Dan Williamse2f8db52011-05-10 02:28:46 -0700899 * @sci_port: port to post task
900 *
901 * Prevent the hardware scheduler from posting new requests to the front
902 * of the scheduler queue causing a starvation problem for currently
903 * ongoing requests.
904 *
905 */
Dan Williams89a73012011-06-30 19:14:33 -0700906static void sci_port_post_dummy_request(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -0700907{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700908 struct isci_host *ihost = iport->owning_controller;
Dan Williamsffe191c2011-06-29 13:09:25 -0700909 u16 tag = iport->reserved_tag;
Dan Williams312e0c22011-06-28 13:47:09 -0700910 struct scu_task_context *tc;
911 u32 command;
Dan Williamse2f8db52011-05-10 02:28:46 -0700912
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700913 tc = &ihost->task_context_table[ISCI_TAG_TCI(tag)];
Dan Williams312e0c22011-06-28 13:47:09 -0700914 tc->abort = 0;
Dan Williamse2f8db52011-05-10 02:28:46 -0700915
916 command = SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
Dan Williamsffe191c2011-06-29 13:09:25 -0700917 iport->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT |
Dan Williams312e0c22011-06-28 13:47:09 -0700918 ISCI_TAG_TCI(tag);
Dan Williamse2f8db52011-05-10 02:28:46 -0700919
Dan Williams89a73012011-06-30 19:14:33 -0700920 sci_controller_post_request(ihost, command);
Dan Williamse2f8db52011-05-10 02:28:46 -0700921}
922
923/**
924 * This routine will abort the dummy request. This will alow the hardware to
925 * power down parts of the silicon to save power.
926 *
927 * @sci_port: The port on which the task must be aborted.
928 *
929 */
Dan Williams89a73012011-06-30 19:14:33 -0700930static void sci_port_abort_dummy_request(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -0700931{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700932 struct isci_host *ihost = iport->owning_controller;
Dan Williamsffe191c2011-06-29 13:09:25 -0700933 u16 tag = iport->reserved_tag;
Dan Williamse2f8db52011-05-10 02:28:46 -0700934 struct scu_task_context *tc;
935 u32 command;
936
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700937 tc = &ihost->task_context_table[ISCI_TAG_TCI(tag)];
Dan Williamse2f8db52011-05-10 02:28:46 -0700938 tc->abort = 1;
939
940 command = SCU_CONTEXT_COMMAND_REQUEST_POST_TC_ABORT |
Dan Williamsffe191c2011-06-29 13:09:25 -0700941 iport->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT |
Dan Williams312e0c22011-06-28 13:47:09 -0700942 ISCI_TAG_TCI(tag);
Dan Williamse2f8db52011-05-10 02:28:46 -0700943
Dan Williams89a73012011-06-30 19:14:33 -0700944 sci_controller_post_request(ihost, command);
Dan Williamse2f8db52011-05-10 02:28:46 -0700945}
946
947/**
948 *
Dan Williamsffe191c2011-06-29 13:09:25 -0700949 * @sci_port: This is the struct isci_port object to resume.
Dan Williamse2f8db52011-05-10 02:28:46 -0700950 *
951 * This method will resume the port task scheduler for this port object. none
952 */
953static void
Dan Williams89a73012011-06-30 19:14:33 -0700954sci_port_resume_port_task_scheduler(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -0700955{
956 u32 pts_control_value;
957
Dan Williamsffe191c2011-06-29 13:09:25 -0700958 pts_control_value = readl(&iport->port_task_scheduler_registers->control);
Dan Williamse2f8db52011-05-10 02:28:46 -0700959 pts_control_value &= ~SCU_PTSxCR_GEN_BIT(SUSPEND);
Dan Williamsffe191c2011-06-29 13:09:25 -0700960 writel(pts_control_value, &iport->port_task_scheduler_registers->control);
Dan Williamse2f8db52011-05-10 02:28:46 -0700961}
962
Dan Williams89a73012011-06-30 19:14:33 -0700963static void sci_port_ready_substate_waiting_enter(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -0700964{
Dan Williamsffe191c2011-06-29 13:09:25 -0700965 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamse2f8db52011-05-10 02:28:46 -0700966
Dan Williams89a73012011-06-30 19:14:33 -0700967 sci_port_suspend_port_task_scheduler(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -0700968
Dan Williamsffe191c2011-06-29 13:09:25 -0700969 iport->not_ready_reason = SCIC_PORT_NOT_READY_NO_ACTIVE_PHYS;
Dan Williamse2f8db52011-05-10 02:28:46 -0700970
Dan Williamsffe191c2011-06-29 13:09:25 -0700971 if (iport->active_phy_mask != 0) {
Dan Williamse2f8db52011-05-10 02:28:46 -0700972 /* At least one of the phys on the port is ready */
Dan Williamsffe191c2011-06-29 13:09:25 -0700973 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +0000974 SCI_PORT_SUB_OPERATIONAL);
Dan Williamse2f8db52011-05-10 02:28:46 -0700975 }
976}
977
Dan Williams89a73012011-06-30 19:14:33 -0700978static void sci_port_ready_substate_operational_enter(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -0700979{
980 u32 index;
Dan Williamsffe191c2011-06-29 13:09:25 -0700981 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700982 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -0700983
Dan Williamse2f8db52011-05-10 02:28:46 -0700984 isci_port_ready(ihost, iport);
985
986 for (index = 0; index < SCI_MAX_PHYS; index++) {
Dan Williamsffe191c2011-06-29 13:09:25 -0700987 if (iport->phy_table[index]) {
988 writel(iport->physical_port_index,
989 &iport->port_pe_configuration_register[
990 iport->phy_table[index]->phy_index]);
Dan Williamse2f8db52011-05-10 02:28:46 -0700991 }
992 }
993
Dan Williams89a73012011-06-30 19:14:33 -0700994 sci_port_update_viit_entry(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -0700995
Dan Williams89a73012011-06-30 19:14:33 -0700996 sci_port_resume_port_task_scheduler(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -0700997
998 /*
999 * Post the dummy task for the port so the hardware can schedule
1000 * io correctly
1001 */
Dan Williams89a73012011-06-30 19:14:33 -07001002 sci_port_post_dummy_request(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -07001003}
1004
Dan Williams89a73012011-06-30 19:14:33 -07001005static void sci_port_invalidate_dummy_remote_node(struct isci_port *iport)
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001006{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001007 struct isci_host *ihost = iport->owning_controller;
Dan Williamsffe191c2011-06-29 13:09:25 -07001008 u8 phys_index = iport->physical_port_index;
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001009 union scu_remote_node_context *rnc;
Dan Williamsffe191c2011-06-29 13:09:25 -07001010 u16 rni = iport->reserved_rni;
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001011 u32 command;
1012
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001013 rnc = &ihost->remote_node_context_table[rni];
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001014
1015 rnc->ssp.is_valid = false;
1016
1017 /* ensure the preceding tc abort request has reached the
1018 * controller and give it ample time to act before posting the rnc
1019 * invalidate
1020 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001021 readl(&ihost->smu_registers->interrupt_status); /* flush */
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001022 udelay(10);
1023
1024 command = SCU_CONTEXT_COMMAND_POST_RNC_INVALIDATE |
1025 phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
1026
Dan Williams89a73012011-06-30 19:14:33 -07001027 sci_controller_post_request(ihost, command);
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001028}
1029
Dan Williamse2f8db52011-05-10 02:28:46 -07001030/**
1031 *
Dan Williamsffe191c2011-06-29 13:09:25 -07001032 * @object: This is the object which is cast to a struct isci_port object.
Dan Williamse2f8db52011-05-10 02:28:46 -07001033 *
Dan Williamsffe191c2011-06-29 13:09:25 -07001034 * This method will perform the actions required by the struct isci_port on
Edmund Nadolskie3013702011-06-02 00:10:43 +00001035 * exiting the SCI_PORT_SUB_OPERATIONAL. This function reports
Dan Williamse2f8db52011-05-10 02:28:46 -07001036 * the port not ready and suspends the port task scheduler. none
1037 */
Dan Williams89a73012011-06-30 19:14:33 -07001038static void sci_port_ready_substate_operational_exit(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001039{
Dan Williamsffe191c2011-06-29 13:09:25 -07001040 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001041 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -07001042
1043 /*
1044 * Kill the dummy task for this port if it has not yet posted
1045 * the hardware will treat this as a NOP and just return abort
1046 * complete.
1047 */
Dan Williams89a73012011-06-30 19:14:33 -07001048 sci_port_abort_dummy_request(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -07001049
1050 isci_port_not_ready(ihost, iport);
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001051
Dan Williamsffe191c2011-06-29 13:09:25 -07001052 if (iport->ready_exit)
Dan Williams89a73012011-06-30 19:14:33 -07001053 sci_port_invalidate_dummy_remote_node(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -07001054}
1055
Dan Williams89a73012011-06-30 19:14:33 -07001056static void sci_port_ready_substate_configuring_enter(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001057{
Dan Williamsffe191c2011-06-29 13:09:25 -07001058 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001059 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -07001060
Dan Williamsffe191c2011-06-29 13:09:25 -07001061 if (iport->active_phy_mask == 0) {
Dan Williamse2f8db52011-05-10 02:28:46 -07001062 isci_port_not_ready(ihost, iport);
1063
Dan Williamsffe191c2011-06-29 13:09:25 -07001064 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001065 SCI_PORT_SUB_WAITING);
Dan Williamsffe191c2011-06-29 13:09:25 -07001066 } else if (iport->started_request_count == 0)
1067 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001068 SCI_PORT_SUB_OPERATIONAL);
Dan Williamse2f8db52011-05-10 02:28:46 -07001069}
1070
Dan Williams89a73012011-06-30 19:14:33 -07001071static void sci_port_ready_substate_configuring_exit(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001072{
Dan Williamsffe191c2011-06-29 13:09:25 -07001073 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamse2f8db52011-05-10 02:28:46 -07001074
Dan Williams89a73012011-06-30 19:14:33 -07001075 sci_port_suspend_port_task_scheduler(iport);
Dan Williamsffe191c2011-06-29 13:09:25 -07001076 if (iport->ready_exit)
Dan Williams89a73012011-06-30 19:14:33 -07001077 sci_port_invalidate_dummy_remote_node(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -07001078}
1079
Dan Williams89a73012011-06-30 19:14:33 -07001080enum sci_status sci_port_start(struct isci_port *iport)
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001081{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001082 struct isci_host *ihost = iport->owning_controller;
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001083 enum sci_status status = SCI_SUCCESS;
Dan Williams89a73012011-06-30 19:14:33 -07001084 enum sci_port_states state;
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001085 u32 phy_mask;
1086
Dan Williamsffe191c2011-06-29 13:09:25 -07001087 state = iport->sm.current_state_id;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001088 if (state != SCI_PORT_STOPPED) {
Dan Williamsffe191c2011-06-29 13:09:25 -07001089 dev_warn(sciport_to_dev(iport),
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001090 "%s: in wrong state: %d\n", __func__, state);
1091 return SCI_FAILURE_INVALID_STATE;
1092 }
1093
Dan Williamsffe191c2011-06-29 13:09:25 -07001094 if (iport->assigned_device_count > 0) {
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001095 /* TODO This is a start failure operation because
1096 * there are still devices assigned to this port.
1097 * There must be no devices assigned to a port on a
1098 * start operation.
1099 */
1100 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
1101 }
1102
Dan Williamsffe191c2011-06-29 13:09:25 -07001103 if (iport->reserved_rni == SCU_DUMMY_INDEX) {
Dan Williams89a73012011-06-30 19:14:33 -07001104 u16 rni = sci_remote_node_table_allocate_remote_node(
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001105 &ihost->available_remote_nodes, 1);
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001106
1107 if (rni != SCU_DUMMY_INDEX)
Dan Williams89a73012011-06-30 19:14:33 -07001108 sci_port_construct_dummy_rnc(iport, rni);
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001109 else
1110 status = SCI_FAILURE_INSUFFICIENT_RESOURCES;
Dan Williamsffe191c2011-06-29 13:09:25 -07001111 iport->reserved_rni = rni;
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001112 }
1113
Dan Williamsffe191c2011-06-29 13:09:25 -07001114 if (iport->reserved_tag == SCI_CONTROLLER_INVALID_IO_TAG) {
Dan Williams312e0c22011-06-28 13:47:09 -07001115 u16 tag;
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001116
Dan Williams312e0c22011-06-28 13:47:09 -07001117 tag = isci_alloc_tag(ihost);
1118 if (tag == SCI_CONTROLLER_INVALID_IO_TAG)
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001119 status = SCI_FAILURE_INSUFFICIENT_RESOURCES;
Dan Williams312e0c22011-06-28 13:47:09 -07001120 else
Dan Williams89a73012011-06-30 19:14:33 -07001121 sci_port_construct_dummy_task(iport, tag);
Dan Williamsffe191c2011-06-29 13:09:25 -07001122 iport->reserved_tag = tag;
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001123 }
1124
1125 if (status == SCI_SUCCESS) {
Dan Williams89a73012011-06-30 19:14:33 -07001126 phy_mask = sci_port_get_phys(iport);
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001127
1128 /*
1129 * There are one or more phys assigned to this port. Make sure
1130 * the port's phy mask is in fact legal and supported by the
1131 * silicon.
1132 */
Dan Williams89a73012011-06-30 19:14:33 -07001133 if (sci_port_is_phy_mask_valid(iport, phy_mask) == true) {
Dan Williamsffe191c2011-06-29 13:09:25 -07001134 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001135 SCI_PORT_READY);
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001136
1137 return SCI_SUCCESS;
1138 }
1139 status = SCI_FAILURE;
1140 }
1141
1142 if (status != SCI_SUCCESS)
Dan Williams89a73012011-06-30 19:14:33 -07001143 sci_port_destroy_dummy_resources(iport);
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001144
1145 return status;
1146}
1147
Dan Williams89a73012011-06-30 19:14:33 -07001148enum sci_status sci_port_stop(struct isci_port *iport)
Piotr Sawicki8bc80d32011-05-11 23:52:31 +00001149{
Dan Williams89a73012011-06-30 19:14:33 -07001150 enum sci_port_states state;
Piotr Sawicki8bc80d32011-05-11 23:52:31 +00001151
Dan Williamsffe191c2011-06-29 13:09:25 -07001152 state = iport->sm.current_state_id;
Piotr Sawicki8bc80d32011-05-11 23:52:31 +00001153 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001154 case SCI_PORT_STOPPED:
Piotr Sawicki8bc80d32011-05-11 23:52:31 +00001155 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001156 case SCI_PORT_SUB_WAITING:
1157 case SCI_PORT_SUB_OPERATIONAL:
1158 case SCI_PORT_SUB_CONFIGURING:
1159 case SCI_PORT_RESETTING:
Dan Williamsffe191c2011-06-29 13:09:25 -07001160 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001161 SCI_PORT_STOPPING);
Piotr Sawicki8bc80d32011-05-11 23:52:31 +00001162 return SCI_SUCCESS;
1163 default:
Dan Williamsffe191c2011-06-29 13:09:25 -07001164 dev_warn(sciport_to_dev(iport),
Piotr Sawicki8bc80d32011-05-11 23:52:31 +00001165 "%s: in wrong state: %d\n", __func__, state);
1166 return SCI_FAILURE_INVALID_STATE;
1167 }
1168}
1169
Dan Williams89a73012011-06-30 19:14:33 -07001170static enum sci_status sci_port_hard_reset(struct isci_port *iport, u32 timeout)
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001171{
1172 enum sci_status status = SCI_FAILURE_INVALID_PHY;
Dan Williams85280952011-06-28 15:05:53 -07001173 struct isci_phy *iphy = NULL;
Dan Williams89a73012011-06-30 19:14:33 -07001174 enum sci_port_states state;
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001175 u32 phy_index;
1176
Dan Williamsffe191c2011-06-29 13:09:25 -07001177 state = iport->sm.current_state_id;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001178 if (state != SCI_PORT_SUB_OPERATIONAL) {
Dan Williamsffe191c2011-06-29 13:09:25 -07001179 dev_warn(sciport_to_dev(iport),
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001180 "%s: in wrong state: %d\n", __func__, state);
1181 return SCI_FAILURE_INVALID_STATE;
1182 }
1183
1184 /* Select a phy on which we can send the hard reset request. */
Dan Williams85280952011-06-28 15:05:53 -07001185 for (phy_index = 0; phy_index < SCI_MAX_PHYS && !iphy; phy_index++) {
Dan Williamsffe191c2011-06-29 13:09:25 -07001186 iphy = iport->phy_table[phy_index];
Dan Williams89a73012011-06-30 19:14:33 -07001187 if (iphy && !sci_port_active_phy(iport, iphy)) {
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001188 /*
1189 * We found a phy but it is not ready select
1190 * different phy
1191 */
Dan Williams85280952011-06-28 15:05:53 -07001192 iphy = NULL;
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001193 }
1194 }
1195
1196 /* If we have a phy then go ahead and start the reset procedure */
Dan Williams85280952011-06-28 15:05:53 -07001197 if (!iphy)
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001198 return status;
Dan Williams89a73012011-06-30 19:14:33 -07001199 status = sci_phy_reset(iphy);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001200
1201 if (status != SCI_SUCCESS)
1202 return status;
1203
Dan Williamsffe191c2011-06-29 13:09:25 -07001204 sci_mod_timer(&iport->timer, timeout);
1205 iport->not_ready_reason = SCIC_PORT_NOT_READY_HARD_RESET_REQUESTED;
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001206
Dan Williamsffe191c2011-06-29 13:09:25 -07001207 port_state_machine_change(iport, SCI_PORT_RESETTING);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001208 return SCI_SUCCESS;
1209}
1210
1211/**
Dan Williams89a73012011-06-30 19:14:33 -07001212 * sci_port_add_phy() -
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001213 * @sci_port: This parameter specifies the port in which the phy will be added.
1214 * @sci_phy: This parameter is the phy which is to be added to the port.
1215 *
1216 * This method will add a PHY to the selected port. This method returns an
1217 * enum sci_status. SCI_SUCCESS the phy has been added to the port. Any other
1218 * status is a failure to add the phy to the port.
1219 */
Dan Williams89a73012011-06-30 19:14:33 -07001220enum sci_status sci_port_add_phy(struct isci_port *iport,
Dan Williams85280952011-06-28 15:05:53 -07001221 struct isci_phy *iphy)
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001222{
1223 enum sci_status status;
Dan Williams89a73012011-06-30 19:14:33 -07001224 enum sci_port_states state;
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001225
Dan Williamsffe191c2011-06-29 13:09:25 -07001226 state = iport->sm.current_state_id;
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001227 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001228 case SCI_PORT_STOPPED: {
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001229 struct sci_sas_address port_sas_address;
1230
1231 /* Read the port assigned SAS Address if there is one */
Dan Williams89a73012011-06-30 19:14:33 -07001232 sci_port_get_sas_address(iport, &port_sas_address);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001233
1234 if (port_sas_address.high != 0 && port_sas_address.low != 0) {
1235 struct sci_sas_address phy_sas_address;
1236
1237 /* Make sure that the PHY SAS Address matches the SAS Address
1238 * for this port
1239 */
Dan Williams89a73012011-06-30 19:14:33 -07001240 sci_phy_get_sas_address(iphy, &phy_sas_address);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001241
1242 if (port_sas_address.high != phy_sas_address.high ||
1243 port_sas_address.low != phy_sas_address.low)
1244 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
1245 }
Dan Williams89a73012011-06-30 19:14:33 -07001246 return sci_port_set_phy(iport, iphy);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001247 }
Edmund Nadolskie3013702011-06-02 00:10:43 +00001248 case SCI_PORT_SUB_WAITING:
1249 case SCI_PORT_SUB_OPERATIONAL:
Dan Williams89a73012011-06-30 19:14:33 -07001250 status = sci_port_set_phy(iport, iphy);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001251
1252 if (status != SCI_SUCCESS)
1253 return status;
1254
Dan Williams89a73012011-06-30 19:14:33 -07001255 sci_port_general_link_up_handler(iport, iphy, true);
Dan Williamsffe191c2011-06-29 13:09:25 -07001256 iport->not_ready_reason = SCIC_PORT_NOT_READY_RECONFIGURING;
1257 port_state_machine_change(iport, SCI_PORT_SUB_CONFIGURING);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001258
1259 return status;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001260 case SCI_PORT_SUB_CONFIGURING:
Dan Williams89a73012011-06-30 19:14:33 -07001261 status = sci_port_set_phy(iport, iphy);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001262
1263 if (status != SCI_SUCCESS)
1264 return status;
Dan Williams89a73012011-06-30 19:14:33 -07001265 sci_port_general_link_up_handler(iport, iphy, true);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001266
1267 /* Re-enter the configuring state since this may be the last phy in
1268 * the port.
1269 */
Dan Williamsffe191c2011-06-29 13:09:25 -07001270 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001271 SCI_PORT_SUB_CONFIGURING);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001272 return SCI_SUCCESS;
1273 default:
Dan Williamsffe191c2011-06-29 13:09:25 -07001274 dev_warn(sciport_to_dev(iport),
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001275 "%s: in wrong state: %d\n", __func__, state);
1276 return SCI_FAILURE_INVALID_STATE;
1277 }
1278}
1279
1280/**
Dan Williams89a73012011-06-30 19:14:33 -07001281 * sci_port_remove_phy() -
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001282 * @sci_port: This parameter specifies the port in which the phy will be added.
1283 * @sci_phy: This parameter is the phy which is to be added to the port.
1284 *
1285 * This method will remove the PHY from the selected PORT. This method returns
1286 * an enum sci_status. SCI_SUCCESS the phy has been removed from the port. Any
1287 * other status is a failure to add the phy to the port.
1288 */
Dan Williams89a73012011-06-30 19:14:33 -07001289enum sci_status sci_port_remove_phy(struct isci_port *iport,
Dan Williams85280952011-06-28 15:05:53 -07001290 struct isci_phy *iphy)
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001291{
1292 enum sci_status status;
Dan Williams89a73012011-06-30 19:14:33 -07001293 enum sci_port_states state;
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001294
Dan Williamsffe191c2011-06-29 13:09:25 -07001295 state = iport->sm.current_state_id;
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001296
1297 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001298 case SCI_PORT_STOPPED:
Dan Williams89a73012011-06-30 19:14:33 -07001299 return sci_port_clear_phy(iport, iphy);
Edmund Nadolskie3013702011-06-02 00:10:43 +00001300 case SCI_PORT_SUB_OPERATIONAL:
Dan Williams89a73012011-06-30 19:14:33 -07001301 status = sci_port_clear_phy(iport, iphy);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001302 if (status != SCI_SUCCESS)
1303 return status;
1304
Dan Williams89a73012011-06-30 19:14:33 -07001305 sci_port_deactivate_phy(iport, iphy, true);
Dan Williamsffe191c2011-06-29 13:09:25 -07001306 iport->not_ready_reason = SCIC_PORT_NOT_READY_RECONFIGURING;
1307 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001308 SCI_PORT_SUB_CONFIGURING);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001309 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001310 case SCI_PORT_SUB_CONFIGURING:
Dan Williams89a73012011-06-30 19:14:33 -07001311 status = sci_port_clear_phy(iport, iphy);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001312
1313 if (status != SCI_SUCCESS)
1314 return status;
Dan Williams89a73012011-06-30 19:14:33 -07001315 sci_port_deactivate_phy(iport, iphy, true);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001316
1317 /* Re-enter the configuring state since this may be the last phy in
1318 * the port
1319 */
Dan Williamsffe191c2011-06-29 13:09:25 -07001320 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001321 SCI_PORT_SUB_CONFIGURING);
Piotr Sawicki051266c2011-05-12 19:10:14 +00001322 return SCI_SUCCESS;
1323 default:
Dan Williamsffe191c2011-06-29 13:09:25 -07001324 dev_warn(sciport_to_dev(iport),
Piotr Sawicki051266c2011-05-12 19:10:14 +00001325 "%s: in wrong state: %d\n", __func__, state);
1326 return SCI_FAILURE_INVALID_STATE;
1327 }
1328}
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001329
Dan Williams89a73012011-06-30 19:14:33 -07001330enum sci_status sci_port_link_up(struct isci_port *iport,
Dan Williams85280952011-06-28 15:05:53 -07001331 struct isci_phy *iphy)
Piotr Sawicki051266c2011-05-12 19:10:14 +00001332{
Dan Williams89a73012011-06-30 19:14:33 -07001333 enum sci_port_states state;
Piotr Sawicki051266c2011-05-12 19:10:14 +00001334
Dan Williamsffe191c2011-06-29 13:09:25 -07001335 state = iport->sm.current_state_id;
Piotr Sawicki051266c2011-05-12 19:10:14 +00001336 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001337 case SCI_PORT_SUB_WAITING:
Piotr Sawicki051266c2011-05-12 19:10:14 +00001338 /* Since this is the first phy going link up for the port we
1339 * can just enable it and continue
1340 */
Dan Williams89a73012011-06-30 19:14:33 -07001341 sci_port_activate_phy(iport, iphy, true);
Piotr Sawicki051266c2011-05-12 19:10:14 +00001342
Dan Williamsffe191c2011-06-29 13:09:25 -07001343 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001344 SCI_PORT_SUB_OPERATIONAL);
Piotr Sawicki051266c2011-05-12 19:10:14 +00001345 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001346 case SCI_PORT_SUB_OPERATIONAL:
Dan Williams89a73012011-06-30 19:14:33 -07001347 sci_port_general_link_up_handler(iport, iphy, true);
Piotr Sawicki051266c2011-05-12 19:10:14 +00001348 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001349 case SCI_PORT_RESETTING:
Piotr Sawicki051266c2011-05-12 19:10:14 +00001350 /* TODO We should make sure that the phy that has gone
1351 * link up is the same one on which we sent the reset. It is
1352 * possible that the phy on which we sent the reset is not the
1353 * one that has gone link up and we want to make sure that
1354 * phy being reset comes back. Consider the case where a
1355 * reset is sent but before the hardware processes the reset it
1356 * get a link up on the port because of a hot plug event.
1357 * because of the reset request this phy will go link down
1358 * almost immediately.
1359 */
1360
1361 /* In the resetting state we don't notify the user regarding
1362 * link up and link down notifications.
1363 */
Dan Williams89a73012011-06-30 19:14:33 -07001364 sci_port_general_link_up_handler(iport, iphy, false);
Piotr Sawicki051266c2011-05-12 19:10:14 +00001365 return SCI_SUCCESS;
1366 default:
Dan Williamsffe191c2011-06-29 13:09:25 -07001367 dev_warn(sciport_to_dev(iport),
Piotr Sawicki051266c2011-05-12 19:10:14 +00001368 "%s: in wrong state: %d\n", __func__, state);
1369 return SCI_FAILURE_INVALID_STATE;
1370 }
1371}
1372
Dan Williams89a73012011-06-30 19:14:33 -07001373enum sci_status sci_port_link_down(struct isci_port *iport,
Dan Williams85280952011-06-28 15:05:53 -07001374 struct isci_phy *iphy)
Piotr Sawicki051266c2011-05-12 19:10:14 +00001375{
Dan Williams89a73012011-06-30 19:14:33 -07001376 enum sci_port_states state;
Piotr Sawicki051266c2011-05-12 19:10:14 +00001377
Dan Williamsffe191c2011-06-29 13:09:25 -07001378 state = iport->sm.current_state_id;
Piotr Sawicki051266c2011-05-12 19:10:14 +00001379 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001380 case SCI_PORT_SUB_OPERATIONAL:
Dan Williams89a73012011-06-30 19:14:33 -07001381 sci_port_deactivate_phy(iport, iphy, true);
Piotr Sawicki051266c2011-05-12 19:10:14 +00001382
1383 /* If there are no active phys left in the port, then
1384 * transition the port to the WAITING state until such time
1385 * as a phy goes link up
1386 */
Dan Williamsffe191c2011-06-29 13:09:25 -07001387 if (iport->active_phy_mask == 0)
1388 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001389 SCI_PORT_SUB_WAITING);
Piotr Sawicki051266c2011-05-12 19:10:14 +00001390 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001391 case SCI_PORT_RESETTING:
Piotr Sawicki051266c2011-05-12 19:10:14 +00001392 /* In the resetting state we don't notify the user regarding
1393 * link up and link down notifications. */
Dan Williams89a73012011-06-30 19:14:33 -07001394 sci_port_deactivate_phy(iport, iphy, false);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001395 return SCI_SUCCESS;
1396 default:
Dan Williamsffe191c2011-06-29 13:09:25 -07001397 dev_warn(sciport_to_dev(iport),
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001398 "%s: in wrong state: %d\n", __func__, state);
1399 return SCI_FAILURE_INVALID_STATE;
1400 }
1401}
1402
Dan Williams89a73012011-06-30 19:14:33 -07001403enum sci_status sci_port_start_io(struct isci_port *iport,
1404 struct isci_remote_device *idev,
1405 struct isci_request *ireq)
Dan Williams68138202011-05-12 07:16:06 -07001406{
Dan Williams89a73012011-06-30 19:14:33 -07001407 enum sci_port_states state;
Dan Williamse2f8db52011-05-10 02:28:46 -07001408
Dan Williamsffe191c2011-06-29 13:09:25 -07001409 state = iport->sm.current_state_id;
Dan Williams68138202011-05-12 07:16:06 -07001410 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001411 case SCI_PORT_SUB_WAITING:
Dan Williams68138202011-05-12 07:16:06 -07001412 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001413 case SCI_PORT_SUB_OPERATIONAL:
Dan Williamsffe191c2011-06-29 13:09:25 -07001414 iport->started_request_count++;
Dan Williams68138202011-05-12 07:16:06 -07001415 return SCI_SUCCESS;
1416 default:
Dan Williamsffe191c2011-06-29 13:09:25 -07001417 dev_warn(sciport_to_dev(iport),
Dan Williams68138202011-05-12 07:16:06 -07001418 "%s: in wrong state: %d\n", __func__, state);
1419 return SCI_FAILURE_INVALID_STATE;
1420 }
1421}
1422
Dan Williams89a73012011-06-30 19:14:33 -07001423enum sci_status sci_port_complete_io(struct isci_port *iport,
1424 struct isci_remote_device *idev,
1425 struct isci_request *ireq)
Dan Williams68138202011-05-12 07:16:06 -07001426{
Dan Williams89a73012011-06-30 19:14:33 -07001427 enum sci_port_states state;
Dan Williams68138202011-05-12 07:16:06 -07001428
Dan Williamsffe191c2011-06-29 13:09:25 -07001429 state = iport->sm.current_state_id;
Dan Williams68138202011-05-12 07:16:06 -07001430 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001431 case SCI_PORT_STOPPED:
Dan Williamsffe191c2011-06-29 13:09:25 -07001432 dev_warn(sciport_to_dev(iport),
Dan Williams68138202011-05-12 07:16:06 -07001433 "%s: in wrong state: %d\n", __func__, state);
1434 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001435 case SCI_PORT_STOPPING:
Dan Williams89a73012011-06-30 19:14:33 -07001436 sci_port_decrement_request_count(iport);
Dan Williams68138202011-05-12 07:16:06 -07001437
Dan Williamsffe191c2011-06-29 13:09:25 -07001438 if (iport->started_request_count == 0)
1439 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001440 SCI_PORT_STOPPED);
Dan Williams68138202011-05-12 07:16:06 -07001441 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001442 case SCI_PORT_READY:
1443 case SCI_PORT_RESETTING:
1444 case SCI_PORT_FAILED:
1445 case SCI_PORT_SUB_WAITING:
1446 case SCI_PORT_SUB_OPERATIONAL:
Dan Williams89a73012011-06-30 19:14:33 -07001447 sci_port_decrement_request_count(iport);
Dan Williams68138202011-05-12 07:16:06 -07001448 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001449 case SCI_PORT_SUB_CONFIGURING:
Dan Williams89a73012011-06-30 19:14:33 -07001450 sci_port_decrement_request_count(iport);
Dan Williamsffe191c2011-06-29 13:09:25 -07001451 if (iport->started_request_count == 0) {
1452 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001453 SCI_PORT_SUB_OPERATIONAL);
Dan Williams68138202011-05-12 07:16:06 -07001454 }
1455 break;
1456 }
1457 return SCI_SUCCESS;
1458}
Dan Williamse2f8db52011-05-10 02:28:46 -07001459
Dan Williams89a73012011-06-30 19:14:33 -07001460static void sci_port_enable_port_task_scheduler(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -07001461{
1462 u32 pts_control_value;
1463
Dan Williams89a73012011-06-30 19:14:33 -07001464 /* enable the port task scheduler in a suspended state */
Dan Williamsffe191c2011-06-29 13:09:25 -07001465 pts_control_value = readl(&iport->port_task_scheduler_registers->control);
Dan Williamse2f8db52011-05-10 02:28:46 -07001466 pts_control_value |= SCU_PTSxCR_GEN_BIT(ENABLE) | SCU_PTSxCR_GEN_BIT(SUSPEND);
Dan Williamsffe191c2011-06-29 13:09:25 -07001467 writel(pts_control_value, &iport->port_task_scheduler_registers->control);
Dan Williamse2f8db52011-05-10 02:28:46 -07001468}
1469
Dan Williams89a73012011-06-30 19:14:33 -07001470static void sci_port_disable_port_task_scheduler(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -07001471{
1472 u32 pts_control_value;
1473
Dan Williamsffe191c2011-06-29 13:09:25 -07001474 pts_control_value = readl(&iport->port_task_scheduler_registers->control);
Dan Williamse2f8db52011-05-10 02:28:46 -07001475 pts_control_value &=
1476 ~(SCU_PTSxCR_GEN_BIT(ENABLE) | SCU_PTSxCR_GEN_BIT(SUSPEND));
Dan Williamsffe191c2011-06-29 13:09:25 -07001477 writel(pts_control_value, &iport->port_task_scheduler_registers->control);
Dan Williamse2f8db52011-05-10 02:28:46 -07001478}
1479
Dan Williams89a73012011-06-30 19:14:33 -07001480static void sci_port_post_dummy_remote_node(struct isci_port *iport)
Dan Williamse2f8db52011-05-10 02:28:46 -07001481{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001482 struct isci_host *ihost = iport->owning_controller;
Dan Williamsffe191c2011-06-29 13:09:25 -07001483 u8 phys_index = iport->physical_port_index;
Dan Williamse2f8db52011-05-10 02:28:46 -07001484 union scu_remote_node_context *rnc;
Dan Williamsffe191c2011-06-29 13:09:25 -07001485 u16 rni = iport->reserved_rni;
Dan Williamse2f8db52011-05-10 02:28:46 -07001486 u32 command;
1487
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001488 rnc = &ihost->remote_node_context_table[rni];
Dan Williamse2f8db52011-05-10 02:28:46 -07001489 rnc->ssp.is_valid = true;
1490
1491 command = SCU_CONTEXT_COMMAND_POST_RNC_32 |
1492 phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
1493
Dan Williams89a73012011-06-30 19:14:33 -07001494 sci_controller_post_request(ihost, command);
Dan Williamse2f8db52011-05-10 02:28:46 -07001495
1496 /* ensure hardware has seen the post rnc command and give it
1497 * ample time to act before sending the suspend
1498 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001499 readl(&ihost->smu_registers->interrupt_status); /* flush */
Dan Williamse2f8db52011-05-10 02:28:46 -07001500 udelay(10);
1501
1502 command = SCU_CONTEXT_COMMAND_POST_RNC_SUSPEND_TX_RX |
1503 phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
1504
Dan Williams89a73012011-06-30 19:14:33 -07001505 sci_controller_post_request(ihost, command);
Dan Williamse2f8db52011-05-10 02:28:46 -07001506}
1507
Dan Williams89a73012011-06-30 19:14:33 -07001508static void sci_port_stopped_state_enter(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001509{
Dan Williamsffe191c2011-06-29 13:09:25 -07001510 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamse2f8db52011-05-10 02:28:46 -07001511
Dan Williamsffe191c2011-06-29 13:09:25 -07001512 if (iport->sm.previous_state_id == SCI_PORT_STOPPING) {
Dan Williamse2f8db52011-05-10 02:28:46 -07001513 /*
1514 * If we enter this state becasuse of a request to stop
1515 * the port then we want to disable the hardwares port
1516 * task scheduler. */
Dan Williams89a73012011-06-30 19:14:33 -07001517 sci_port_disable_port_task_scheduler(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -07001518 }
1519}
1520
Dan Williams89a73012011-06-30 19:14:33 -07001521static void sci_port_stopped_state_exit(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001522{
Dan Williamsffe191c2011-06-29 13:09:25 -07001523 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamse2f8db52011-05-10 02:28:46 -07001524
1525 /* Enable and suspend the port task scheduler */
Dan Williams89a73012011-06-30 19:14:33 -07001526 sci_port_enable_port_task_scheduler(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -07001527}
1528
Dan Williams89a73012011-06-30 19:14:33 -07001529static void sci_port_ready_state_enter(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001530{
Dan Williamsffe191c2011-06-29 13:09:25 -07001531 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001532 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -07001533 u32 prev_state;
1534
Dan Williamsffe191c2011-06-29 13:09:25 -07001535 prev_state = iport->sm.previous_state_id;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001536 if (prev_state == SCI_PORT_RESETTING)
Dan Williamse2f8db52011-05-10 02:28:46 -07001537 isci_port_hard_reset_complete(iport, SCI_SUCCESS);
1538 else
1539 isci_port_not_ready(ihost, iport);
1540
1541 /* Post and suspend the dummy remote node context for this port. */
Dan Williams89a73012011-06-30 19:14:33 -07001542 sci_port_post_dummy_remote_node(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -07001543
1544 /* Start the ready substate machine */
Dan Williamsffe191c2011-06-29 13:09:25 -07001545 port_state_machine_change(iport,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001546 SCI_PORT_SUB_WAITING);
Dan Williamse2f8db52011-05-10 02:28:46 -07001547}
1548
Dan Williams89a73012011-06-30 19:14:33 -07001549static void sci_port_resetting_state_exit(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001550{
Dan Williamsffe191c2011-06-29 13:09:25 -07001551 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamse2f8db52011-05-10 02:28:46 -07001552
Dan Williamsffe191c2011-06-29 13:09:25 -07001553 sci_del_timer(&iport->timer);
Dan Williamse2f8db52011-05-10 02:28:46 -07001554}
1555
Dan Williams89a73012011-06-30 19:14:33 -07001556static void sci_port_stopping_state_exit(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001557{
Dan Williamsffe191c2011-06-29 13:09:25 -07001558 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamse2f8db52011-05-10 02:28:46 -07001559
Dan Williamsffe191c2011-06-29 13:09:25 -07001560 sci_del_timer(&iport->timer);
Dan Williamse2f8db52011-05-10 02:28:46 -07001561
Dan Williams89a73012011-06-30 19:14:33 -07001562 sci_port_destroy_dummy_resources(iport);
Dan Williamse2f8db52011-05-10 02:28:46 -07001563}
1564
Dan Williams89a73012011-06-30 19:14:33 -07001565static void sci_port_failed_state_enter(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001566{
Dan Williamsffe191c2011-06-29 13:09:25 -07001567 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
Dan Williamse2f8db52011-05-10 02:28:46 -07001568
Dan Williamse2f8db52011-05-10 02:28:46 -07001569 isci_port_hard_reset_complete(iport, SCI_FAILURE_TIMEOUT);
1570}
1571
1572/* --------------------------------------------------------------------------- */
1573
Dan Williams89a73012011-06-30 19:14:33 -07001574static const struct sci_base_state sci_port_state_table[] = {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001575 [SCI_PORT_STOPPED] = {
Dan Williams89a73012011-06-30 19:14:33 -07001576 .enter_state = sci_port_stopped_state_enter,
1577 .exit_state = sci_port_stopped_state_exit
Dan Williamse2f8db52011-05-10 02:28:46 -07001578 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001579 [SCI_PORT_STOPPING] = {
Dan Williams89a73012011-06-30 19:14:33 -07001580 .exit_state = sci_port_stopping_state_exit
Dan Williamse2f8db52011-05-10 02:28:46 -07001581 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001582 [SCI_PORT_READY] = {
Dan Williams89a73012011-06-30 19:14:33 -07001583 .enter_state = sci_port_ready_state_enter,
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001584 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001585 [SCI_PORT_SUB_WAITING] = {
Dan Williams89a73012011-06-30 19:14:33 -07001586 .enter_state = sci_port_ready_substate_waiting_enter,
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001587 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001588 [SCI_PORT_SUB_OPERATIONAL] = {
Dan Williams89a73012011-06-30 19:14:33 -07001589 .enter_state = sci_port_ready_substate_operational_enter,
1590 .exit_state = sci_port_ready_substate_operational_exit
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001591 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001592 [SCI_PORT_SUB_CONFIGURING] = {
Dan Williams89a73012011-06-30 19:14:33 -07001593 .enter_state = sci_port_ready_substate_configuring_enter,
1594 .exit_state = sci_port_ready_substate_configuring_exit
Dan Williamse2f8db52011-05-10 02:28:46 -07001595 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001596 [SCI_PORT_RESETTING] = {
Dan Williams89a73012011-06-30 19:14:33 -07001597 .exit_state = sci_port_resetting_state_exit
Dan Williamse2f8db52011-05-10 02:28:46 -07001598 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001599 [SCI_PORT_FAILED] = {
Dan Williams89a73012011-06-30 19:14:33 -07001600 .enter_state = sci_port_failed_state_enter,
Dan Williamse2f8db52011-05-10 02:28:46 -07001601 }
1602};
1603
Dan Williams89a73012011-06-30 19:14:33 -07001604void sci_port_construct(struct isci_port *iport, u8 index,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001605 struct isci_host *ihost)
Dan Williamse2f8db52011-05-10 02:28:46 -07001606{
Dan Williams89a73012011-06-30 19:14:33 -07001607 sci_init_sm(&iport->sm, sci_port_state_table, SCI_PORT_STOPPED);
Dan Williamse2f8db52011-05-10 02:28:46 -07001608
Dan Williamsffe191c2011-06-29 13:09:25 -07001609 iport->logical_port_index = SCIC_SDS_DUMMY_PORT;
1610 iport->physical_port_index = index;
1611 iport->active_phy_mask = 0;
Jeff Skirvin8e35a132011-10-27 15:05:32 -07001612 iport->last_active_phy = 0;
1613 iport->ready_exit = false;
Dan Williamse2f8db52011-05-10 02:28:46 -07001614
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001615 iport->owning_controller = ihost;
Dan Williamse2f8db52011-05-10 02:28:46 -07001616
Dan Williamsffe191c2011-06-29 13:09:25 -07001617 iport->started_request_count = 0;
1618 iport->assigned_device_count = 0;
Dan Williamse2f8db52011-05-10 02:28:46 -07001619
Dan Williamsffe191c2011-06-29 13:09:25 -07001620 iport->reserved_rni = SCU_DUMMY_INDEX;
1621 iport->reserved_tag = SCI_CONTROLLER_INVALID_IO_TAG;
Dan Williamse2f8db52011-05-10 02:28:46 -07001622
Dan Williamsffe191c2011-06-29 13:09:25 -07001623 sci_init_timer(&iport->timer, port_timeout);
Edmund Nadolski5553ba22011-05-19 11:59:10 +00001624
Dan Williamsffe191c2011-06-29 13:09:25 -07001625 iport->port_task_scheduler_registers = NULL;
Dan Williamse2f8db52011-05-10 02:28:46 -07001626
1627 for (index = 0; index < SCI_MAX_PHYS; index++)
Dan Williamsffe191c2011-06-29 13:09:25 -07001628 iport->phy_table[index] = NULL;
Dan Williamse2f8db52011-05-10 02:28:46 -07001629}
1630
1631void isci_port_init(struct isci_port *iport, struct isci_host *ihost, int index)
1632{
1633 INIT_LIST_HEAD(&iport->remote_dev_list);
1634 INIT_LIST_HEAD(&iport->domain_dev_list);
1635 spin_lock_init(&iport->state_lock);
1636 init_completion(&iport->start_complete);
1637 iport->isci_host = ihost;
1638 isci_port_change_state(iport, isci_freed);
1639}
1640
1641/**
1642 * isci_port_get_state() - This function gets the status of the port object.
1643 * @isci_port: This parameter points to the isci_port object
1644 *
1645 * status of the object as a isci_status enum.
1646 */
1647enum isci_status isci_port_get_state(
1648 struct isci_port *isci_port)
1649{
1650 return isci_port->status;
1651}
1652
Dan Williams89a73012011-06-30 19:14:33 -07001653void sci_port_broadcast_change_received(struct isci_port *iport, struct isci_phy *iphy)
Dan Williamse2f8db52011-05-10 02:28:46 -07001654{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001655 struct isci_host *ihost = iport->owning_controller;
Dan Williamse2f8db52011-05-10 02:28:46 -07001656
1657 /* notify the user. */
Dan Williamsffe191c2011-06-29 13:09:25 -07001658 isci_port_bc_change_received(ihost, iport, iphy);
Dan Williamse2f8db52011-05-10 02:28:46 -07001659}
1660
Dan Williams4393aa42011-03-31 13:10:44 -07001661int isci_port_perform_hard_reset(struct isci_host *ihost, struct isci_port *iport,
1662 struct isci_phy *iphy)
Dan Williams6f231dd2011-07-02 22:56:22 -07001663{
Dan Williams4393aa42011-03-31 13:10:44 -07001664 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -07001665 enum sci_status status;
Jeff Skirvin8e35a132011-10-27 15:05:32 -07001666 int ret = TMF_RESP_FUNC_COMPLETE;
Dan Williams6f231dd2011-07-02 22:56:22 -07001667
Dan Williams4393aa42011-03-31 13:10:44 -07001668 dev_dbg(&ihost->pdev->dev, "%s: iport = %p\n",
1669 __func__, iport);
Dan Williams6f231dd2011-07-02 22:56:22 -07001670
Dan Williams4393aa42011-03-31 13:10:44 -07001671 init_completion(&iport->hard_reset_complete);
Dan Williams6f231dd2011-07-02 22:56:22 -07001672
Dan Williams4393aa42011-03-31 13:10:44 -07001673 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001674
1675 #define ISCI_PORT_RESET_TIMEOUT SCIC_SDS_SIGNATURE_FIS_TIMEOUT
Dan Williams89a73012011-06-30 19:14:33 -07001676 status = sci_port_hard_reset(iport, ISCI_PORT_RESET_TIMEOUT);
Dan Williams6f231dd2011-07-02 22:56:22 -07001677
Dan Williams4393aa42011-03-31 13:10:44 -07001678 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001679
1680 if (status == SCI_SUCCESS) {
Dan Williams4393aa42011-03-31 13:10:44 -07001681 wait_for_completion(&iport->hard_reset_complete);
Dan Williams6f231dd2011-07-02 22:56:22 -07001682
Dan Williams4393aa42011-03-31 13:10:44 -07001683 dev_dbg(&ihost->pdev->dev,
1684 "%s: iport = %p; hard reset completion\n",
1685 __func__, iport);
Dan Williams6f231dd2011-07-02 22:56:22 -07001686
Jeff Skirvin8e35a132011-10-27 15:05:32 -07001687 if (iport->hard_reset_status != SCI_SUCCESS) {
Dan Williams6f231dd2011-07-02 22:56:22 -07001688 ret = TMF_RESP_FUNC_FAILED;
Jeff Skirvin8e35a132011-10-27 15:05:32 -07001689
1690 dev_err(&ihost->pdev->dev,
1691 "%s: iport = %p; hard reset failed (0x%x)\n",
1692 __func__, iport, iport->hard_reset_status);
1693 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001694 } else {
1695 ret = TMF_RESP_FUNC_FAILED;
1696
Dan Williams4393aa42011-03-31 13:10:44 -07001697 dev_err(&ihost->pdev->dev,
Dan Williams89a73012011-06-30 19:14:33 -07001698 "%s: iport = %p; sci_port_hard_reset call"
Dan Williams6f231dd2011-07-02 22:56:22 -07001699 " failed 0x%x\n",
Dan Williams4393aa42011-03-31 13:10:44 -07001700 __func__, iport, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001701
1702 }
1703
1704 /* If the hard reset for the port has failed, consider this
1705 * the same as link failures on all phys in the port.
1706 */
1707 if (ret != TMF_RESP_FUNC_COMPLETE) {
Jeff Skirvinfd0527a2011-06-20 14:09:26 -07001708
Dan Williams4393aa42011-03-31 13:10:44 -07001709 dev_err(&ihost->pdev->dev,
1710 "%s: iport = %p; hard reset failed "
Jeff Skirvinfd0527a2011-06-20 14:09:26 -07001711 "(0x%x) - driving explicit link fail for all phys\n",
1712 __func__, iport, iport->hard_reset_status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001713 }
Dan Williams6f231dd2011-07-02 22:56:22 -07001714 return ret;
1715}
Dave Jiang09d7da12011-03-26 16:11:51 -07001716
Dan Williamse2f8db52011-05-10 02:28:46 -07001717/**
1718 * isci_port_deformed() - This function is called by libsas when a port becomes
1719 * inactive.
1720 * @phy: This parameter specifies the libsas phy with the inactive port.
1721 *
1722 */
1723void isci_port_deformed(struct asd_sas_phy *phy)
Dave Jiang09d7da12011-03-26 16:11:51 -07001724{
Dan Williamse2f8db52011-05-10 02:28:46 -07001725 pr_debug("%s: sas_phy = %p\n", __func__, phy);
1726}
1727
1728/**
1729 * isci_port_formed() - This function is called by libsas when a port becomes
1730 * active.
1731 * @phy: This parameter specifies the libsas phy with the active port.
1732 *
1733 */
1734void isci_port_formed(struct asd_sas_phy *phy)
1735{
1736 pr_debug("%s: sas_phy = %p, sas_port = %p\n", __func__, phy, phy->port);
Dave Jiang09d7da12011-03-26 16:11:51 -07001737}