blob: 6370b93bd6aee0dcc4858a52ac65e0d638a45e98 [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 Williamse2f8db52011-05-10 02:28:46 -070077/*
78 * This function will indicate which protocols are supported by this port.
79 * @sci_port: a handle corresponding to the SAS port for which to return the
80 * supported protocols.
81 * @protocols: This parameter specifies a pointer to a data structure
82 * which the core will copy the protocol values for the port from the
83 * transmit_identification register.
84 */
85static void
86scic_sds_port_get_protocols(struct scic_sds_port *sci_port,
87 struct scic_phy_proto *protocols)
Dan Williams6f231dd2011-07-02 22:56:22 -070088{
Dan Williamse2f8db52011-05-10 02:28:46 -070089 u8 index;
90
91 protocols->all = 0;
92
93 for (index = 0; index < SCI_MAX_PHYS; index++) {
94 if (sci_port->phy_table[index] != NULL) {
95 scic_sds_phy_get_protocols(sci_port->phy_table[index],
96 protocols);
97 }
98 }
Dan Williams6f231dd2011-07-02 22:56:22 -070099}
100
Dan Williams6f231dd2011-07-02 22:56:22 -0700101/**
Dan Williamse2f8db52011-05-10 02:28:46 -0700102 * This method requests a list (mask) of the phys contained in the supplied SAS
103 * port.
104 * @sci_port: a handle corresponding to the SAS port for which to return the
105 * phy mask.
Dan Williams6f231dd2011-07-02 22:56:22 -0700106 *
Dan Williamse2f8db52011-05-10 02:28:46 -0700107 * Return a bit mask indicating which phys are a part of this port. Each bit
108 * corresponds to a phy identifier (e.g. bit 0 = phy id 0).
Dan Williams6f231dd2011-07-02 22:56:22 -0700109 */
Dan Williamse2f8db52011-05-10 02:28:46 -0700110static u32 scic_sds_port_get_phys(struct scic_sds_port *sci_port)
Dan Williams6f231dd2011-07-02 22:56:22 -0700111{
Dan Williamse2f8db52011-05-10 02:28:46 -0700112 u32 index;
113 u32 mask;
114
115 mask = 0;
116
117 for (index = 0; index < SCI_MAX_PHYS; index++) {
118 if (sci_port->phy_table[index] != NULL) {
119 mask |= (1 << index);
120 }
121 }
122
123 return mask;
Dan Williams6f231dd2011-07-02 22:56:22 -0700124}
125
Dan Williamse2f8db52011-05-10 02:28:46 -0700126/**
127 * scic_port_get_properties() - This method simply returns the properties
128 * regarding the port, such as: physical index, protocols, sas address, etc.
129 * @port: this parameter specifies the port for which to retrieve the physical
130 * index.
131 * @properties: This parameter specifies the properties structure into which to
132 * copy the requested information.
133 *
134 * Indicate if the user specified a valid port. SCI_SUCCESS This value is
135 * returned if the specified port was valid. SCI_FAILURE_INVALID_PORT This
136 * value is returned if the specified port is not valid. When this value is
137 * returned, no data is copied to the properties output parameter.
138 */
139static enum sci_status scic_port_get_properties(struct scic_sds_port *port,
140 struct scic_port_properties *prop)
Dan Williams6f231dd2011-07-02 22:56:22 -0700141{
Dan Williamse2f8db52011-05-10 02:28:46 -0700142 if ((port == NULL) ||
143 (port->logical_port_index == SCIC_SDS_DUMMY_PORT))
144 return SCI_FAILURE_INVALID_PORT;
Dan Williams6f231dd2011-07-02 22:56:22 -0700145
Dan Williamse2f8db52011-05-10 02:28:46 -0700146 prop->index = port->logical_port_index;
147 prop->phy_mask = scic_sds_port_get_phys(port);
148 scic_sds_port_get_sas_address(port, &prop->local.sas_address);
149 scic_sds_port_get_protocols(port, &prop->local.protocols);
150 scic_sds_port_get_attached_sas_address(port, &prop->remote.sas_address);
Dan Williams6f231dd2011-07-02 22:56:22 -0700151
Dan Williamse2f8db52011-05-10 02:28:46 -0700152 return SCI_SUCCESS;
Dan Williams6f231dd2011-07-02 22:56:22 -0700153}
154
Dan Williamse2f8db52011-05-10 02:28:46 -0700155static void isci_port_link_up(struct isci_host *isci_host,
156 struct scic_sds_port *port,
157 struct scic_sds_phy *phy)
Dan Williams6f231dd2011-07-02 22:56:22 -0700158{
159 unsigned long flags;
160 struct scic_port_properties properties;
Dan Williams4b339812011-05-06 17:36:38 -0700161 struct isci_phy *isci_phy = sci_phy_to_iphy(phy);
Dan Williamse5313812011-05-07 10:11:43 -0700162 struct isci_port *isci_port = sci_port_to_iport(port);
Dan Williams6f231dd2011-07-02 22:56:22 -0700163 unsigned long success = true;
164
165 BUG_ON(isci_phy->isci_port != NULL);
Bartosz Barcinski6cb4d6b2011-04-12 17:28:43 -0700166
Dan Williams6f231dd2011-07-02 22:56:22 -0700167 isci_phy->isci_port = isci_port;
168
169 dev_dbg(&isci_host->pdev->dev,
170 "%s: isci_port = %p\n",
171 __func__, isci_port);
172
173 spin_lock_irqsave(&isci_phy->sas_phy.frame_rcvd_lock, flags);
174
175 isci_port_change_state(isci_phy->isci_port, isci_starting);
176
177 scic_port_get_properties(port, &properties);
178
Dave Jiangd7b90fc2011-05-04 18:22:33 -0700179 if (phy->protocol == SCIC_SDS_PHY_PROTOCOL_SATA) {
Dan Williams150fc6f2011-02-25 10:25:21 -0800180 u64 attached_sas_address;
Dan Williams6f231dd2011-07-02 22:56:22 -0700181
Dan Williams6f231dd2011-07-02 22:56:22 -0700182 isci_phy->sas_phy.oob_mode = SATA_OOB_MODE;
Dave Jiangf2f30082011-05-04 15:02:02 -0700183 isci_phy->sas_phy.frame_rcvd_size = sizeof(struct dev_to_host_fis);
Dan Williams6f231dd2011-07-02 22:56:22 -0700184
185 /*
186 * For direct-attached SATA devices, the SCI core will
187 * automagically assign a SAS address to the end device
188 * for the purpose of creating a port. This SAS address
189 * will not be the same as assigned to the PHY and needs
190 * to be obtained from struct scic_port_properties properties.
191 */
Dan Williams150fc6f2011-02-25 10:25:21 -0800192 attached_sas_address = properties.remote.sas_address.high;
193 attached_sas_address <<= 32;
194 attached_sas_address |= properties.remote.sas_address.low;
195 swab64s(&attached_sas_address);
Dan Williams6f231dd2011-07-02 22:56:22 -0700196
Dan Williams150fc6f2011-02-25 10:25:21 -0800197 memcpy(&isci_phy->sas_phy.attached_sas_addr,
198 &attached_sas_address, sizeof(attached_sas_address));
Dave Jiangd7b90fc2011-05-04 18:22:33 -0700199 } else if (phy->protocol == SCIC_SDS_PHY_PROTOCOL_SAS) {
Dan Williams6f231dd2011-07-02 22:56:22 -0700200 isci_phy->sas_phy.oob_mode = SAS_OOB_MODE;
Dave Jiang4b7ebd02011-05-04 15:37:52 -0700201 isci_phy->sas_phy.frame_rcvd_size = sizeof(struct sas_identify_frame);
Dan Williams6f231dd2011-07-02 22:56:22 -0700202
203 /* Copy the attached SAS address from the IAF */
204 memcpy(isci_phy->sas_phy.attached_sas_addr,
Dave Jiang4b7ebd02011-05-04 15:37:52 -0700205 isci_phy->frame_rcvd.iaf.sas_addr, SAS_ADDR_SIZE);
Dan Williams6f231dd2011-07-02 22:56:22 -0700206 } else {
207 dev_err(&isci_host->pdev->dev, "%s: unkown target\n", __func__);
208 success = false;
209 }
210
Dan Williams83e51432011-02-18 09:25:13 -0800211 isci_phy->sas_phy.phy->negotiated_linkrate = sci_phy_linkrate(phy);
212
Dan Williams6f231dd2011-07-02 22:56:22 -0700213 spin_unlock_irqrestore(&isci_phy->sas_phy.frame_rcvd_lock, flags);
214
215 /* Notify libsas that we have an address frame, if indeed
216 * we've found an SSP, SMP, or STP target */
217 if (success)
218 isci_host->sas_ha.notify_port_event(&isci_phy->sas_phy,
219 PORTE_BYTES_DMAED);
220}
221
222
223/**
224 * isci_port_link_down() - This function is called by the sci core when a link
225 * becomes inactive.
226 * @isci_host: This parameter specifies the isci host object.
227 * @phy: This parameter specifies the isci phy with the active link.
228 * @port: This parameter specifies the isci port with the active link.
229 *
230 */
Dan Williamse2f8db52011-05-10 02:28:46 -0700231static void isci_port_link_down(struct isci_host *isci_host,
232 struct isci_phy *isci_phy,
233 struct isci_port *isci_port)
Dan Williams6f231dd2011-07-02 22:56:22 -0700234{
235 struct isci_remote_device *isci_device;
236
237 dev_dbg(&isci_host->pdev->dev,
238 "%s: isci_port = %p\n", __func__, isci_port);
239
240 if (isci_port) {
241
242 /* check to see if this is the last phy on this port. */
243 if (isci_phy->sas_phy.port
244 && isci_phy->sas_phy.port->num_phys == 1) {
245
246 /* change the state for all devices on this port.
247 * The next task sent to this device will be returned
248 * as SAS_TASK_UNDELIVERED, and the scsi mid layer
249 * will remove the target
250 */
251 list_for_each_entry(isci_device,
252 &isci_port->remote_dev_list,
253 node) {
254 dev_dbg(&isci_host->pdev->dev,
255 "%s: isci_device = %p\n",
256 __func__, isci_device);
257 isci_remote_device_change_state(isci_device,
258 isci_stopping);
259 }
260 }
261 isci_port_change_state(isci_port, isci_stopping);
262 }
263
264 /* Notify libsas of the borken link, this will trigger calls to our
265 * isci_port_deformed and isci_dev_gone functions.
266 */
267 sas_phy_disconnected(&isci_phy->sas_phy);
268 isci_host->sas_ha.notify_phy_event(&isci_phy->sas_phy,
269 PHYE_LOSS_OF_SIGNAL);
270
271 isci_phy->isci_port = NULL;
272
273 dev_dbg(&isci_host->pdev->dev,
274 "%s: isci_port = %p - Done\n", __func__, isci_port);
275}
276
277
278/**
Dan Williams6f231dd2011-07-02 22:56:22 -0700279 * isci_port_ready() - This function is called by the sci core when a link
280 * becomes ready.
281 * @isci_host: This parameter specifies the isci host object.
282 * @port: This parameter specifies the sci port with the active link.
283 *
284 */
Dan Williamse2f8db52011-05-10 02:28:46 -0700285static void isci_port_ready(struct isci_host *isci_host, struct isci_port *isci_port)
Dan Williams6f231dd2011-07-02 22:56:22 -0700286{
287 dev_dbg(&isci_host->pdev->dev,
288 "%s: isci_port = %p\n", __func__, isci_port);
289
290 complete_all(&isci_port->start_complete);
291 isci_port_change_state(isci_port, isci_ready);
292 return;
293}
294
295/**
296 * isci_port_not_ready() - This function is called by the sci core when a link
297 * is not ready. All remote devices on this link will be removed if they are
298 * in the stopping state.
299 * @isci_host: This parameter specifies the isci host object.
300 * @port: This parameter specifies the sci port with the active link.
301 *
302 */
Dan Williamse2f8db52011-05-10 02:28:46 -0700303static void isci_port_not_ready(struct isci_host *isci_host, struct isci_port *isci_port)
Dan Williams6f231dd2011-07-02 22:56:22 -0700304{
305 dev_dbg(&isci_host->pdev->dev,
306 "%s: isci_port = %p\n", __func__, isci_port);
307}
308
Dan Williamse2f8db52011-05-10 02:28:46 -0700309static void isci_port_stop_complete(struct scic_sds_controller *scic,
310 struct scic_sds_port *sci_port,
311 enum sci_status completion_status)
312{
313 dev_dbg(&scic_to_ihost(scic)->pdev->dev, "Port stop complete\n");
314}
315
Dan Williams6f231dd2011-07-02 22:56:22 -0700316/**
317 * isci_port_hard_reset_complete() - This function is called by the sci core
318 * when the hard reset complete notification has been received.
319 * @port: This parameter specifies the sci port with the active link.
320 * @completion_status: This parameter specifies the core status for the reset
321 * process.
322 *
323 */
Dan Williamse2f8db52011-05-10 02:28:46 -0700324static void isci_port_hard_reset_complete(struct isci_port *isci_port,
325 enum sci_status completion_status)
Dan Williams6f231dd2011-07-02 22:56:22 -0700326{
327 dev_dbg(&isci_port->isci_host->pdev->dev,
328 "%s: isci_port = %p, completion_status=%x\n",
329 __func__, isci_port, completion_status);
330
331 /* Save the status of the hard reset from the port. */
332 isci_port->hard_reset_status = completion_status;
333
334 complete_all(&isci_port->hard_reset_complete);
335}
Dan Williams4393aa42011-03-31 13:10:44 -0700336
Dan Williamse2f8db52011-05-10 02:28:46 -0700337/* This method will return a true value if the specified phy can be assigned to
338 * this port The following is a list of phys for each port that are allowed: -
339 * Port 0 - 3 2 1 0 - Port 1 - 1 - Port 2 - 3 2 - Port 3 - 3 This method
340 * doesn't preclude all configurations. It merely ensures that a phy is part
341 * of the allowable set of phy identifiers for that port. For example, one
342 * could assign phy 3 to port 0 and no other phys. Please refer to
343 * scic_sds_port_is_phy_mask_valid() for information regarding whether the
344 * phy_mask for a port can be supported. bool true if this is a valid phy
345 * assignment for the port false if this is not a valid phy assignment for the
346 * port
347 */
348bool scic_sds_port_is_valid_phy_assignment(struct scic_sds_port *sci_port,
349 u32 phy_index)
350{
351 /* Initialize to invalid value. */
352 u32 existing_phy_index = SCI_MAX_PHYS;
353 u32 index;
354
355 if ((sci_port->physical_port_index == 1) && (phy_index != 1)) {
356 return false;
357 }
358
359 if (sci_port->physical_port_index == 3 && phy_index != 3) {
360 return false;
361 }
362
363 if (
364 (sci_port->physical_port_index == 2)
365 && ((phy_index == 0) || (phy_index == 1))
366 ) {
367 return false;
368 }
369
370 for (index = 0; index < SCI_MAX_PHYS; index++) {
371 if ((sci_port->phy_table[index] != NULL)
372 && (index != phy_index)) {
373 existing_phy_index = index;
374 }
375 }
376
377 /*
378 * Ensure that all of the phys in the port are capable of
379 * operating at the same maximum link rate. */
380 if (
381 (existing_phy_index < SCI_MAX_PHYS)
382 && (sci_port->owning_controller->user_parameters.sds1.phys[
383 phy_index].max_speed_generation !=
384 sci_port->owning_controller->user_parameters.sds1.phys[
385 existing_phy_index].max_speed_generation)
386 )
387 return false;
388
389 return true;
390}
391
392/**
393 *
394 * @sci_port: This is the port object for which to determine if the phy mask
395 * can be supported.
396 *
397 * This method will return a true value if the port's phy mask can be supported
398 * by the SCU. The following is a list of valid PHY mask configurations for
399 * each port: - Port 0 - [[3 2] 1] 0 - Port 1 - [1] - Port 2 - [[3] 2]
400 * - Port 3 - [3] This method returns a boolean indication specifying if the
401 * phy mask can be supported. true if this is a valid phy assignment for the
402 * port false if this is not a valid phy assignment for the port
403 */
404static bool scic_sds_port_is_phy_mask_valid(
405 struct scic_sds_port *sci_port,
406 u32 phy_mask)
407{
408 if (sci_port->physical_port_index == 0) {
409 if (((phy_mask & 0x0F) == 0x0F)
410 || ((phy_mask & 0x03) == 0x03)
411 || ((phy_mask & 0x01) == 0x01)
412 || (phy_mask == 0))
413 return true;
414 } else if (sci_port->physical_port_index == 1) {
415 if (((phy_mask & 0x02) == 0x02)
416 || (phy_mask == 0))
417 return true;
418 } else if (sci_port->physical_port_index == 2) {
419 if (((phy_mask & 0x0C) == 0x0C)
420 || ((phy_mask & 0x04) == 0x04)
421 || (phy_mask == 0))
422 return true;
423 } else if (sci_port->physical_port_index == 3) {
424 if (((phy_mask & 0x08) == 0x08)
425 || (phy_mask == 0))
426 return true;
427 }
428
429 return false;
430}
431
432/**
433 *
434 * @sci_port: This parameter specifies the port from which to return a
435 * connected phy.
436 *
437 * This method retrieves a currently active (i.e. connected) phy contained in
438 * the port. Currently, the lowest order phy that is connected is returned.
439 * This method returns a pointer to a SCIS_SDS_PHY object. NULL This value is
440 * returned if there are no currently active (i.e. connected to a remote end
441 * point) phys contained in the port. All other values specify a struct scic_sds_phy
442 * object that is active in the port.
443 */
444static struct scic_sds_phy *scic_sds_port_get_a_connected_phy(
445 struct scic_sds_port *sci_port
446 ) {
447 u32 index;
448 struct scic_sds_phy *phy;
449
450 for (index = 0; index < SCI_MAX_PHYS; index++) {
451 /*
452 * Ensure that the phy is both part of the port and currently
453 * connected to the remote end-point. */
454 phy = sci_port->phy_table[index];
455 if (
456 (phy != NULL)
457 && scic_sds_port_active_phy(sci_port, phy)
458 ) {
459 return phy;
460 }
461 }
462
463 return NULL;
464}
465
466/**
467 * scic_sds_port_set_phy() -
468 * @out]: port The port object to which the phy assignement is being made.
469 * @out]: phy The phy which is being assigned to the port.
470 *
471 * This method attempts to make the assignment of the phy to the port. If
472 * successful the phy is assigned to the ports phy table. bool true if the phy
473 * assignment can be made. false if the phy assignement can not be made. This
474 * is a functional test that only fails if the phy is currently assigned to a
475 * different port.
476 */
477static enum sci_status scic_sds_port_set_phy(
478 struct scic_sds_port *port,
479 struct scic_sds_phy *phy)
480{
481 /*
482 * Check to see if we can add this phy to a port
483 * that means that the phy is not part of a port and that the port does
484 * not already have a phy assinged to the phy index. */
485 if (
486 (port->phy_table[phy->phy_index] == NULL)
Dan Williams4f20ef42011-05-12 06:00:31 -0700487 && (phy_get_non_dummy_port(phy) == NULL)
Dan Williamse2f8db52011-05-10 02:28:46 -0700488 && scic_sds_port_is_valid_phy_assignment(port, phy->phy_index)
489 ) {
490 /*
491 * Phy is being added in the stopped state so we are in MPC mode
492 * make logical port index = physical port index */
493 port->logical_port_index = port->physical_port_index;
494 port->phy_table[phy->phy_index] = phy;
495 scic_sds_phy_set_port(phy, port);
496
497 return SCI_SUCCESS;
498 }
499
500 return SCI_FAILURE;
501}
502
503/**
504 * scic_sds_port_clear_phy() -
505 * @out]: port The port from which the phy is being cleared.
506 * @out]: phy The phy being cleared from the port.
507 *
508 * This method will clear the phy assigned to this port. This method fails if
509 * this phy is not currently assinged to this port. bool true if the phy is
510 * removed from the port. false if this phy is not assined to this port.
511 */
512static enum sci_status scic_sds_port_clear_phy(
513 struct scic_sds_port *port,
514 struct scic_sds_phy *phy)
515{
516 /* Make sure that this phy is part of this port */
517 if (port->phy_table[phy->phy_index] == phy &&
Dan Williams4f20ef42011-05-12 06:00:31 -0700518 phy_get_non_dummy_port(phy) == port) {
Dan Williamse2f8db52011-05-10 02:28:46 -0700519 struct scic_sds_controller *scic = port->owning_controller;
520 struct isci_host *ihost = scic_to_ihost(scic);
521
522 /* Yep it is assigned to this port so remove it */
523 scic_sds_phy_set_port(phy, &ihost->ports[SCI_MAX_PORTS].sci);
524 port->phy_table[phy->phy_index] = NULL;
525 return SCI_SUCCESS;
526 }
527
528 return SCI_FAILURE;
529}
530
Dan Williamse2f8db52011-05-10 02:28:46 -0700531
532/**
533 * This method requests the SAS address for the supplied SAS port from the SCI
534 * implementation.
535 * @sci_port: a handle corresponding to the SAS port for which to return the
536 * SAS address.
537 * @sas_address: This parameter specifies a pointer to a SAS address structure
538 * into which the core will copy the SAS address for the port.
539 *
540 */
541void scic_sds_port_get_sas_address(
542 struct scic_sds_port *sci_port,
543 struct sci_sas_address *sas_address)
544{
545 u32 index;
546
547 sas_address->high = 0;
548 sas_address->low = 0;
549
550 for (index = 0; index < SCI_MAX_PHYS; index++) {
551 if (sci_port->phy_table[index] != NULL) {
552 scic_sds_phy_get_sas_address(sci_port->phy_table[index], sas_address);
553 }
554 }
555}
556
557/*
558 * This function requests the SAS address for the device directly attached to
559 * this SAS port.
560 * @sci_port: a handle corresponding to the SAS port for which to return the
561 * SAS address.
562 * @sas_address: This parameter specifies a pointer to a SAS address structure
563 * into which the core will copy the SAS address for the device directly
564 * attached to the port.
565 *
566 */
567void scic_sds_port_get_attached_sas_address(
568 struct scic_sds_port *sci_port,
569 struct sci_sas_address *sas_address)
570{
571 struct scic_sds_phy *sci_phy;
572
573 /*
574 * Ensure that the phy is both part of the port and currently
575 * connected to the remote end-point.
576 */
577 sci_phy = scic_sds_port_get_a_connected_phy(sci_port);
578 if (sci_phy) {
579 if (sci_phy->protocol != SCIC_SDS_PHY_PROTOCOL_SATA) {
580 scic_sds_phy_get_attached_sas_address(sci_phy,
581 sas_address);
582 } else {
583 scic_sds_phy_get_sas_address(sci_phy, sas_address);
584 sas_address->low += sci_phy->phy_index;
585 }
586 } else {
587 sas_address->high = 0;
588 sas_address->low = 0;
589 }
590}
591
592/**
593 * scic_sds_port_construct_dummy_rnc() - create dummy rnc for si workaround
594 *
595 * @sci_port: logical port on which we need to create the remote node context
596 * @rni: remote node index for this remote node context.
597 *
598 * This routine will construct a dummy remote node context data structure
599 * This structure will be posted to the hardware to work around a scheduler
600 * error in the hardware.
601 */
602static void scic_sds_port_construct_dummy_rnc(struct scic_sds_port *sci_port, u16 rni)
603{
604 union scu_remote_node_context *rnc;
605
606 rnc = &sci_port->owning_controller->remote_node_context_table[rni];
607
608 memset(rnc, 0, sizeof(union scu_remote_node_context));
609
610 rnc->ssp.remote_sas_address_hi = 0;
611 rnc->ssp.remote_sas_address_lo = 0;
612
613 rnc->ssp.remote_node_index = rni;
614 rnc->ssp.remote_node_port_width = 1;
615 rnc->ssp.logical_port_index = sci_port->physical_port_index;
616
617 rnc->ssp.nexus_loss_timer_enable = false;
618 rnc->ssp.check_bit = false;
619 rnc->ssp.is_valid = true;
620 rnc->ssp.is_remote_node_context = true;
621 rnc->ssp.function_number = 0;
622 rnc->ssp.arbitration_wait_time = 0;
623}
624
625/**
626 * scic_sds_port_construct_dummy_task() - create dummy task for si workaround
627 * @sci_port The logical port on which we need to create the
628 * remote node context.
629 * context.
630 * @tci The remote node index for this remote node context.
631 *
632 * This routine will construct a dummy task context data structure. This
633 * structure will be posted to the hardwre to work around a scheduler error
634 * in the hardware.
635 *
636 */
637static void scic_sds_port_construct_dummy_task(struct scic_sds_port *sci_port, u16 tci)
638{
639 struct scu_task_context *task_context;
640
641 task_context = scic_sds_controller_get_task_context_buffer(sci_port->owning_controller, tci);
642
643 memset(task_context, 0, sizeof(struct scu_task_context));
644
645 task_context->abort = 0;
646 task_context->priority = 0;
647 task_context->initiator_request = 1;
648 task_context->connection_rate = 1;
649 task_context->protocol_engine_index = 0;
650 task_context->logical_port_index = sci_port->physical_port_index;
651 task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SSP;
652 task_context->task_index = scic_sds_io_tag_get_index(tci);
653 task_context->valid = SCU_TASK_CONTEXT_VALID;
654 task_context->context_type = SCU_TASK_CONTEXT_TYPE;
655
656 task_context->remote_node_index = sci_port->reserved_rni;
657 task_context->command_code = 0;
658
659 task_context->link_layer_control = 0;
660 task_context->do_not_dma_ssp_good_response = 1;
661 task_context->strict_ordering = 0;
662 task_context->control_frame = 0;
663 task_context->timeout_enable = 0;
664 task_context->block_guard_enable = 0;
665
666 task_context->address_modifier = 0;
667
668 task_context->task_phase = 0x01;
669}
670
671static void scic_sds_port_destroy_dummy_resources(struct scic_sds_port *sci_port)
672{
673 struct scic_sds_controller *scic = sci_port->owning_controller;
674
675 if (sci_port->reserved_tci != SCU_DUMMY_INDEX)
676 scic_controller_free_io_tag(scic, sci_port->reserved_tci);
677
678 if (sci_port->reserved_rni != SCU_DUMMY_INDEX)
679 scic_sds_remote_node_table_release_remote_node_index(&scic->available_remote_nodes,
680 1, sci_port->reserved_rni);
681
682 sci_port->reserved_rni = SCU_DUMMY_INDEX;
683 sci_port->reserved_tci = SCU_DUMMY_INDEX;
684}
685
686/**
687 * This method performs initialization of the supplied port. Initialization
688 * includes: - state machine initialization - member variable initialization
689 * - configuring the phy_mask
690 * @sci_port:
691 * @transport_layer_registers:
692 * @port_task_scheduler_registers:
693 * @port_configuration_regsiter:
694 *
695 * enum sci_status SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION This value is returned
696 * if the phy being added to the port
697 */
698enum sci_status scic_sds_port_initialize(
699 struct scic_sds_port *sci_port,
700 void __iomem *port_task_scheduler_registers,
701 void __iomem *port_configuration_regsiter,
702 void __iomem *viit_registers)
703{
704 sci_port->port_task_scheduler_registers = port_task_scheduler_registers;
705 sci_port->port_pe_configuration_register = port_configuration_regsiter;
706 sci_port->viit_registers = viit_registers;
707
708 return SCI_SUCCESS;
709}
710
Dan Williamse2f8db52011-05-10 02:28:46 -0700711
712/**
713 * This method assigns the direct attached device ID for this port.
714 *
715 * @param[in] sci_port The port for which the direct attached device id is to
716 * be assigned.
717 * @param[in] device_id The direct attached device ID to assign to the port.
718 * This will be the RNi for the device
719 */
720void scic_sds_port_setup_transports(
721 struct scic_sds_port *sci_port,
722 u32 device_id)
723{
724 u8 index;
725
726 for (index = 0; index < SCI_MAX_PHYS; index++) {
727 if (sci_port->active_phy_mask & (1 << index))
728 scic_sds_phy_setup_transport(sci_port->phy_table[index], device_id);
729 }
730}
731
732/**
733 *
734 * @sci_port: This is the port on which the phy should be enabled.
735 * @sci_phy: This is the specific phy which to enable.
736 * @do_notify_user: This parameter specifies whether to inform the user (via
737 * scic_cb_port_link_up()) as to the fact that a new phy as become ready.
738 *
739 * This function will activate the phy in the port.
740 * Activation includes: - adding
741 * the phy to the port - enabling the Protocol Engine in the silicon. -
742 * notifying the user that the link is up. none
743 */
744static void scic_sds_port_activate_phy(struct scic_sds_port *sci_port,
745 struct scic_sds_phy *sci_phy,
746 bool do_notify_user)
747{
748 struct scic_sds_controller *scic = sci_port->owning_controller;
749 struct isci_host *ihost = scic_to_ihost(scic);
750
751 if (sci_phy->protocol != SCIC_SDS_PHY_PROTOCOL_SATA)
752 scic_sds_phy_resume(sci_phy);
753
754 sci_port->active_phy_mask |= 1 << sci_phy->phy_index;
755
756 scic_sds_controller_clear_invalid_phy(scic, sci_phy);
757
758 if (do_notify_user == true)
759 isci_port_link_up(ihost, sci_port, sci_phy);
760}
761
762void scic_sds_port_deactivate_phy(struct scic_sds_port *sci_port,
763 struct scic_sds_phy *sci_phy,
764 bool do_notify_user)
765{
766 struct scic_sds_controller *scic = scic_sds_port_get_controller(sci_port);
767 struct isci_port *iport = sci_port_to_iport(sci_port);
768 struct isci_host *ihost = scic_to_ihost(scic);
769 struct isci_phy *iphy = sci_phy_to_iphy(sci_phy);
770
771 sci_port->active_phy_mask &= ~(1 << sci_phy->phy_index);
772
773 sci_phy->max_negotiated_speed = SAS_LINK_RATE_UNKNOWN;
774
775 /* Re-assign the phy back to the LP as if it were a narrow port */
776 writel(sci_phy->phy_index,
777 &sci_port->port_pe_configuration_register[sci_phy->phy_index]);
778
779 if (do_notify_user == true)
780 isci_port_link_down(ihost, iphy, iport);
781}
782
783/**
784 *
785 * @sci_port: This is the port on which the phy should be disabled.
786 * @sci_phy: This is the specific phy which to disabled.
787 *
788 * This function will disable the phy and report that the phy is not valid for
789 * this port object. None
790 */
791static void scic_sds_port_invalid_link_up(struct scic_sds_port *sci_port,
792 struct scic_sds_phy *sci_phy)
793{
794 struct scic_sds_controller *scic = sci_port->owning_controller;
795
796 /*
797 * Check to see if we have alreay reported this link as bad and if
798 * not go ahead and tell the SCI_USER that we have discovered an
799 * invalid link.
800 */
801 if ((scic->invalid_phy_mask & (1 << sci_phy->phy_index)) == 0) {
802 scic_sds_controller_set_invalid_phy(scic, sci_phy);
803 dev_warn(&scic_to_ihost(scic)->pdev->dev, "Invalid link up!\n");
804 }
805}
806
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000807static bool is_port_ready_state(enum scic_sds_port_states state)
808{
809 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000810 case SCI_PORT_READY:
811 case SCI_PORT_SUB_WAITING:
812 case SCI_PORT_SUB_OPERATIONAL:
813 case SCI_PORT_SUB_CONFIGURING:
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000814 return true;
815 default:
816 return false;
817 }
818}
819
820/* flag dummy rnc hanling when exiting a ready state */
821static void port_state_machine_change(struct scic_sds_port *sci_port,
822 enum scic_sds_port_states state)
823{
Edmund Nadolskie3013702011-06-02 00:10:43 +0000824 struct sci_base_state_machine *sm = &sci_port->sm;
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000825 enum scic_sds_port_states old_state = sm->current_state_id;
826
827 if (is_port_ready_state(old_state) && !is_port_ready_state(state))
828 sci_port->ready_exit = true;
829
Edmund Nadolskie3013702011-06-02 00:10:43 +0000830 sci_change_state(sm, state);
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000831 sci_port->ready_exit = false;
832}
833
Dan Williamse2f8db52011-05-10 02:28:46 -0700834/**
835 * scic_sds_port_general_link_up_handler - phy can be assigned to port?
836 * @sci_port: scic_sds_port object for which has a phy that has gone link up.
837 * @sci_phy: This is the struct scic_sds_phy object that has gone link up.
838 * @do_notify_user: This parameter specifies whether to inform the user (via
839 * scic_cb_port_link_up()) as to the fact that a new phy as become ready.
840 *
841 * Determine if this phy can be assigned to this
842 * port . If the phy is not a valid PHY for
843 * this port then the function will notify the user. A PHY can only be
844 * part of a port if it's attached SAS ADDRESS is the same as all other PHYs in
845 * the same port. none
846 */
847static void scic_sds_port_general_link_up_handler(struct scic_sds_port *sci_port,
848 struct scic_sds_phy *sci_phy,
849 bool do_notify_user)
850{
851 struct sci_sas_address port_sas_address;
852 struct sci_sas_address phy_sas_address;
853
854 scic_sds_port_get_attached_sas_address(sci_port, &port_sas_address);
855 scic_sds_phy_get_attached_sas_address(sci_phy, &phy_sas_address);
856
857 /* If the SAS address of the new phy matches the SAS address of
858 * other phys in the port OR this is the first phy in the port,
859 * then activate the phy and allow it to be used for operations
860 * in this port.
861 */
862 if ((phy_sas_address.high == port_sas_address.high &&
863 phy_sas_address.low == port_sas_address.low) ||
864 sci_port->active_phy_mask == 0) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000865 struct sci_base_state_machine *sm = &sci_port->sm;
Dan Williamse2f8db52011-05-10 02:28:46 -0700866
867 scic_sds_port_activate_phy(sci_port, sci_phy, do_notify_user);
Edmund Nadolskie3013702011-06-02 00:10:43 +0000868 if (sm->current_state_id == SCI_PORT_RESETTING)
869 port_state_machine_change(sci_port, SCI_PORT_READY);
Dan Williamse2f8db52011-05-10 02:28:46 -0700870 } else
871 scic_sds_port_invalid_link_up(sci_port, sci_phy);
872}
873
874
875
876/**
877 * This method returns false if the port only has a single phy object assigned.
878 * If there are no phys or more than one phy then the method will return
879 * true.
880 * @sci_port: The port for which the wide port condition is to be checked.
881 *
882 * bool true Is returned if this is a wide ported port. false Is returned if
883 * this is a narrow port.
884 */
885static bool scic_sds_port_is_wide(struct scic_sds_port *sci_port)
886{
887 u32 index;
888 u32 phy_count = 0;
889
890 for (index = 0; index < SCI_MAX_PHYS; index++) {
891 if (sci_port->phy_table[index] != NULL) {
892 phy_count++;
893 }
894 }
895
896 return phy_count != 1;
897}
898
899/**
900 * This method is called by the PHY object when the link is detected. if the
901 * port wants the PHY to continue on to the link up state then the port
902 * layer must return true. If the port object returns false the phy object
903 * must halt its attempt to go link up.
904 * @sci_port: The port associated with the phy object.
905 * @sci_phy: The phy object that is trying to go link up.
906 *
907 * true if the phy object can continue to the link up condition. true Is
908 * returned if this phy can continue to the ready state. false Is returned if
909 * can not continue on to the ready state. This notification is in place for
910 * wide ports and direct attached phys. Since there are no wide ported SATA
911 * devices this could become an invalid port configuration.
912 */
913bool scic_sds_port_link_detected(
914 struct scic_sds_port *sci_port,
915 struct scic_sds_phy *sci_phy)
916{
917 if ((sci_port->logical_port_index != SCIC_SDS_DUMMY_PORT) &&
918 (sci_phy->protocol == SCIC_SDS_PHY_PROTOCOL_SATA) &&
919 scic_sds_port_is_wide(sci_port)) {
920 scic_sds_port_invalid_link_up(sci_port, sci_phy);
921
922 return false;
923 }
924
925 return true;
926}
927
Edmund Nadolski5553ba22011-05-19 11:59:10 +0000928static void port_timeout(unsigned long data)
Dan Williamse2f8db52011-05-10 02:28:46 -0700929{
Edmund Nadolski5553ba22011-05-19 11:59:10 +0000930 struct sci_timer *tmr = (struct sci_timer *)data;
931 struct scic_sds_port *sci_port = container_of(tmr, typeof(*sci_port), timer);
932 struct isci_host *ihost = scic_to_ihost(sci_port->owning_controller);
933 unsigned long flags;
Dan Williamse2f8db52011-05-10 02:28:46 -0700934 u32 current_state;
935
Edmund Nadolski5553ba22011-05-19 11:59:10 +0000936 spin_lock_irqsave(&ihost->scic_lock, flags);
937
938 if (tmr->cancel)
939 goto done;
940
Edmund Nadolskie3013702011-06-02 00:10:43 +0000941 current_state = sci_port->sm.current_state_id;
Dan Williamse2f8db52011-05-10 02:28:46 -0700942
Edmund Nadolskie3013702011-06-02 00:10:43 +0000943 if (current_state == SCI_PORT_RESETTING) {
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000944 /* if the port is still in the resetting state then the timeout
945 * fired before the reset completed.
Dan Williamse2f8db52011-05-10 02:28:46 -0700946 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000947 port_state_machine_change(sci_port, SCI_PORT_FAILED);
948 } else if (current_state == SCI_PORT_STOPPED) {
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000949 /* if the port is stopped then the start request failed In this
950 * case stay in the stopped state.
Dan Williamse2f8db52011-05-10 02:28:46 -0700951 */
952 dev_err(sciport_to_dev(sci_port),
953 "%s: SCIC Port 0x%p failed to stop before tiemout.\n",
954 __func__,
955 sci_port);
Edmund Nadolskie3013702011-06-02 00:10:43 +0000956 } else if (current_state == SCI_PORT_STOPPING) {
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000957 /* if the port is still stopping then the stop has not completed */
958 isci_port_stop_complete(sci_port->owning_controller,
959 sci_port,
960 SCI_FAILURE_TIMEOUT);
Dan Williamse2f8db52011-05-10 02:28:46 -0700961 } else {
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000962 /* The port is in the ready state and we have a timer
Dan Williamse2f8db52011-05-10 02:28:46 -0700963 * reporting a timeout this should not happen.
964 */
965 dev_err(sciport_to_dev(sci_port),
966 "%s: SCIC Port 0x%p is processing a timeout operation "
Piotr Sawickie91f41e2011-05-11 23:52:21 +0000967 "in state %d.\n", __func__, sci_port, current_state);
Dan Williamse2f8db52011-05-10 02:28:46 -0700968 }
Edmund Nadolski5553ba22011-05-19 11:59:10 +0000969
970done:
971 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williamse2f8db52011-05-10 02:28:46 -0700972}
973
974/* --------------------------------------------------------------------------- */
975
976/**
977 * This function updates the hardwares VIIT entry for this port.
978 *
979 *
980 */
981static void scic_sds_port_update_viit_entry(struct scic_sds_port *sci_port)
982{
983 struct sci_sas_address sas_address;
984
985 scic_sds_port_get_sas_address(sci_port, &sas_address);
986
987 writel(sas_address.high,
988 &sci_port->viit_registers->initiator_sas_address_hi);
989 writel(sas_address.low,
990 &sci_port->viit_registers->initiator_sas_address_lo);
991
992 /* This value get cleared just in case its not already cleared */
993 writel(0, &sci_port->viit_registers->reserved);
994
995 /* We are required to update the status register last */
996 writel(SCU_VIIT_ENTRY_ID_VIIT |
997 SCU_VIIT_IPPT_INITIATOR |
998 ((1 << sci_port->physical_port_index) << SCU_VIIT_ENTRY_LPVIE_SHIFT) |
999 SCU_VIIT_STATUS_ALL_VALID,
1000 &sci_port->viit_registers->status);
1001}
1002
1003/**
1004 * This method returns the maximum allowed speed for data transfers on this
1005 * port. This maximum allowed speed evaluates to the maximum speed of the
1006 * slowest phy in the port.
1007 * @sci_port: This parameter specifies the port for which to retrieve the
1008 * maximum allowed speed.
1009 *
1010 * This method returns the maximum negotiated speed of the slowest phy in the
1011 * port.
1012 */
1013enum sas_linkrate scic_sds_port_get_max_allowed_speed(
1014 struct scic_sds_port *sci_port)
1015{
1016 u16 index;
1017 enum sas_linkrate max_allowed_speed = SAS_LINK_RATE_6_0_GBPS;
1018 struct scic_sds_phy *phy = NULL;
1019
1020 /*
1021 * Loop through all of the phys in this port and find the phy with the
1022 * lowest maximum link rate. */
1023 for (index = 0; index < SCI_MAX_PHYS; index++) {
1024 phy = sci_port->phy_table[index];
1025 if (
1026 (phy != NULL)
1027 && (scic_sds_port_active_phy(sci_port, phy) == true)
1028 && (phy->max_negotiated_speed < max_allowed_speed)
1029 )
1030 max_allowed_speed = phy->max_negotiated_speed;
1031 }
1032
1033 return max_allowed_speed;
1034}
1035
1036static void scic_port_enable_broadcast_change_notification(struct scic_sds_port *port)
1037{
1038 struct scic_sds_phy *phy;
1039 u32 register_value;
1040 u8 index;
1041
1042 /* Loop through all of the phys to enable BCN. */
1043 for (index = 0; index < SCI_MAX_PHYS; index++) {
1044 phy = port->phy_table[index];
1045 if (phy != NULL) {
1046 register_value =
1047 readl(&phy->link_layer_registers->link_layer_control);
1048
1049 /* clear the bit by writing 1. */
1050 writel(register_value,
1051 &phy->link_layer_registers->link_layer_control);
1052 }
1053 }
1054}
1055
Dan Williamse2f8db52011-05-10 02:28:46 -07001056/**
1057 *
1058 * @sci_port: This is the struct scic_sds_port object to suspend.
1059 *
1060 * This method will susped the port task scheduler for this port object. none
1061 */
1062static void
1063scic_sds_port_suspend_port_task_scheduler(struct scic_sds_port *port)
1064{
1065 u32 pts_control_value;
1066
1067 pts_control_value = readl(&port->port_task_scheduler_registers->control);
1068 pts_control_value |= SCU_PTSxCR_GEN_BIT(SUSPEND);
1069 writel(pts_control_value, &port->port_task_scheduler_registers->control);
1070}
1071
1072/**
1073 * scic_sds_port_post_dummy_request() - post dummy/workaround request
1074 * @sci_port: port to post task
1075 *
1076 * Prevent the hardware scheduler from posting new requests to the front
1077 * of the scheduler queue causing a starvation problem for currently
1078 * ongoing requests.
1079 *
1080 */
1081static void scic_sds_port_post_dummy_request(struct scic_sds_port *sci_port)
1082{
1083 u32 command;
1084 struct scu_task_context *task_context;
1085 struct scic_sds_controller *scic = sci_port->owning_controller;
1086 u16 tci = sci_port->reserved_tci;
1087
1088 task_context = scic_sds_controller_get_task_context_buffer(scic, tci);
1089
1090 task_context->abort = 0;
1091
1092 command = SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
1093 sci_port->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT |
1094 tci;
1095
1096 scic_sds_controller_post_request(scic, command);
1097}
1098
1099/**
1100 * This routine will abort the dummy request. This will alow the hardware to
1101 * power down parts of the silicon to save power.
1102 *
1103 * @sci_port: The port on which the task must be aborted.
1104 *
1105 */
1106static void scic_sds_port_abort_dummy_request(struct scic_sds_port *sci_port)
1107{
1108 struct scic_sds_controller *scic = sci_port->owning_controller;
1109 u16 tci = sci_port->reserved_tci;
1110 struct scu_task_context *tc;
1111 u32 command;
1112
1113 tc = scic_sds_controller_get_task_context_buffer(scic, tci);
1114
1115 tc->abort = 1;
1116
1117 command = SCU_CONTEXT_COMMAND_REQUEST_POST_TC_ABORT |
1118 sci_port->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT |
1119 tci;
1120
1121 scic_sds_controller_post_request(scic, command);
1122}
1123
1124/**
1125 *
1126 * @sci_port: This is the struct scic_sds_port object to resume.
1127 *
1128 * This method will resume the port task scheduler for this port object. none
1129 */
1130static void
1131scic_sds_port_resume_port_task_scheduler(struct scic_sds_port *port)
1132{
1133 u32 pts_control_value;
1134
1135 pts_control_value = readl(&port->port_task_scheduler_registers->control);
1136 pts_control_value &= ~SCU_PTSxCR_GEN_BIT(SUSPEND);
1137 writel(pts_control_value, &port->port_task_scheduler_registers->control);
1138}
1139
Dan Williams9269e0e2011-05-12 07:42:17 -07001140static void scic_sds_port_ready_substate_waiting_enter(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001141{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001142 struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), sm);
Dan Williamse2f8db52011-05-10 02:28:46 -07001143
Dan Williamse2f8db52011-05-10 02:28:46 -07001144 scic_sds_port_suspend_port_task_scheduler(sci_port);
1145
1146 sci_port->not_ready_reason = SCIC_PORT_NOT_READY_NO_ACTIVE_PHYS;
1147
1148 if (sci_port->active_phy_mask != 0) {
1149 /* At least one of the phys on the port is ready */
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001150 port_state_machine_change(sci_port,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001151 SCI_PORT_SUB_OPERATIONAL);
Dan Williamse2f8db52011-05-10 02:28:46 -07001152 }
1153}
1154
Dan Williams9269e0e2011-05-12 07:42:17 -07001155static void scic_sds_port_ready_substate_operational_enter(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001156{
1157 u32 index;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001158 struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), sm);
Dan Williamse2f8db52011-05-10 02:28:46 -07001159 struct scic_sds_controller *scic = sci_port->owning_controller;
1160 struct isci_host *ihost = scic_to_ihost(scic);
1161 struct isci_port *iport = sci_port_to_iport(sci_port);
1162
Dan Williamse2f8db52011-05-10 02:28:46 -07001163 isci_port_ready(ihost, iport);
1164
1165 for (index = 0; index < SCI_MAX_PHYS; index++) {
1166 if (sci_port->phy_table[index]) {
1167 writel(sci_port->physical_port_index,
1168 &sci_port->port_pe_configuration_register[
1169 sci_port->phy_table[index]->phy_index]);
1170 }
1171 }
1172
1173 scic_sds_port_update_viit_entry(sci_port);
1174
1175 scic_sds_port_resume_port_task_scheduler(sci_port);
1176
1177 /*
1178 * Post the dummy task for the port so the hardware can schedule
1179 * io correctly
1180 */
1181 scic_sds_port_post_dummy_request(sci_port);
1182}
1183
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001184static void scic_sds_port_invalidate_dummy_remote_node(struct scic_sds_port *sci_port)
1185{
1186 struct scic_sds_controller *scic = sci_port->owning_controller;
1187 u8 phys_index = sci_port->physical_port_index;
1188 union scu_remote_node_context *rnc;
1189 u16 rni = sci_port->reserved_rni;
1190 u32 command;
1191
1192 rnc = &scic->remote_node_context_table[rni];
1193
1194 rnc->ssp.is_valid = false;
1195
1196 /* ensure the preceding tc abort request has reached the
1197 * controller and give it ample time to act before posting the rnc
1198 * invalidate
1199 */
1200 readl(&scic->smu_registers->interrupt_status); /* flush */
1201 udelay(10);
1202
1203 command = SCU_CONTEXT_COMMAND_POST_RNC_INVALIDATE |
1204 phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
1205
1206 scic_sds_controller_post_request(scic, command);
1207}
1208
Dan Williamse2f8db52011-05-10 02:28:46 -07001209/**
1210 *
1211 * @object: This is the object which is cast to a struct scic_sds_port object.
1212 *
1213 * This method will perform the actions required by the struct scic_sds_port on
Edmund Nadolskie3013702011-06-02 00:10:43 +00001214 * exiting the SCI_PORT_SUB_OPERATIONAL. This function reports
Dan Williamse2f8db52011-05-10 02:28:46 -07001215 * the port not ready and suspends the port task scheduler. none
1216 */
Dan Williams9269e0e2011-05-12 07:42:17 -07001217static void scic_sds_port_ready_substate_operational_exit(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001218{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001219 struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), sm);
Dan Williamse2f8db52011-05-10 02:28:46 -07001220 struct scic_sds_controller *scic = sci_port->owning_controller;
1221 struct isci_host *ihost = scic_to_ihost(scic);
1222 struct isci_port *iport = sci_port_to_iport(sci_port);
1223
1224 /*
1225 * Kill the dummy task for this port if it has not yet posted
1226 * the hardware will treat this as a NOP and just return abort
1227 * complete.
1228 */
1229 scic_sds_port_abort_dummy_request(sci_port);
1230
1231 isci_port_not_ready(ihost, iport);
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001232
1233 if (sci_port->ready_exit)
1234 scic_sds_port_invalidate_dummy_remote_node(sci_port);
Dan Williamse2f8db52011-05-10 02:28:46 -07001235}
1236
Dan Williams9269e0e2011-05-12 07:42:17 -07001237static void scic_sds_port_ready_substate_configuring_enter(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001238{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001239 struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), sm);
Dan Williamse2f8db52011-05-10 02:28:46 -07001240 struct scic_sds_controller *scic = sci_port->owning_controller;
1241 struct isci_host *ihost = scic_to_ihost(scic);
1242 struct isci_port *iport = sci_port_to_iport(sci_port);
1243
Dan Williamse2f8db52011-05-10 02:28:46 -07001244 if (sci_port->active_phy_mask == 0) {
1245 isci_port_not_ready(ihost, iport);
1246
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001247 port_state_machine_change(sci_port,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001248 SCI_PORT_SUB_WAITING);
Dan Williamse2f8db52011-05-10 02:28:46 -07001249 } else if (sci_port->started_request_count == 0)
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001250 port_state_machine_change(sci_port,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001251 SCI_PORT_SUB_OPERATIONAL);
Dan Williamse2f8db52011-05-10 02:28:46 -07001252}
1253
Dan Williams9269e0e2011-05-12 07:42:17 -07001254static void scic_sds_port_ready_substate_configuring_exit(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001255{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001256 struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), sm);
Dan Williamse2f8db52011-05-10 02:28:46 -07001257
1258 scic_sds_port_suspend_port_task_scheduler(sci_port);
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001259 if (sci_port->ready_exit)
1260 scic_sds_port_invalidate_dummy_remote_node(sci_port);
Dan Williamse2f8db52011-05-10 02:28:46 -07001261}
1262
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001263enum sci_status scic_sds_port_start(struct scic_sds_port *sci_port)
1264{
1265 struct scic_sds_controller *scic = sci_port->owning_controller;
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001266 enum sci_status status = SCI_SUCCESS;
1267 enum scic_sds_port_states state;
1268 u32 phy_mask;
1269
Edmund Nadolskie3013702011-06-02 00:10:43 +00001270 state = sci_port->sm.current_state_id;
1271 if (state != SCI_PORT_STOPPED) {
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001272 dev_warn(sciport_to_dev(sci_port),
1273 "%s: in wrong state: %d\n", __func__, state);
1274 return SCI_FAILURE_INVALID_STATE;
1275 }
1276
1277 if (sci_port->assigned_device_count > 0) {
1278 /* TODO This is a start failure operation because
1279 * there are still devices assigned to this port.
1280 * There must be no devices assigned to a port on a
1281 * start operation.
1282 */
1283 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
1284 }
1285
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001286 if (sci_port->reserved_rni == SCU_DUMMY_INDEX) {
1287 u16 rni = scic_sds_remote_node_table_allocate_remote_node(
1288 &scic->available_remote_nodes, 1);
1289
1290 if (rni != SCU_DUMMY_INDEX)
1291 scic_sds_port_construct_dummy_rnc(sci_port, rni);
1292 else
1293 status = SCI_FAILURE_INSUFFICIENT_RESOURCES;
1294 sci_port->reserved_rni = rni;
1295 }
1296
1297 if (sci_port->reserved_tci == SCU_DUMMY_INDEX) {
1298 /* Allocate a TCI and remove the sequence nibble */
1299 u16 tci = scic_controller_allocate_io_tag(scic);
1300
1301 if (tci != SCU_DUMMY_INDEX)
1302 scic_sds_port_construct_dummy_task(sci_port, tci);
1303 else
1304 status = SCI_FAILURE_INSUFFICIENT_RESOURCES;
1305 sci_port->reserved_tci = tci;
1306 }
1307
1308 if (status == SCI_SUCCESS) {
1309 phy_mask = scic_sds_port_get_phys(sci_port);
1310
1311 /*
1312 * There are one or more phys assigned to this port. Make sure
1313 * the port's phy mask is in fact legal and supported by the
1314 * silicon.
1315 */
1316 if (scic_sds_port_is_phy_mask_valid(sci_port, phy_mask) == true) {
1317 port_state_machine_change(sci_port,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001318 SCI_PORT_READY);
Piotr Sawickid76f71d2011-05-11 23:52:26 +00001319
1320 return SCI_SUCCESS;
1321 }
1322 status = SCI_FAILURE;
1323 }
1324
1325 if (status != SCI_SUCCESS)
1326 scic_sds_port_destroy_dummy_resources(sci_port);
1327
1328 return status;
1329}
1330
Piotr Sawicki8bc80d32011-05-11 23:52:31 +00001331enum sci_status scic_sds_port_stop(struct scic_sds_port *sci_port)
1332{
1333 enum scic_sds_port_states state;
1334
Edmund Nadolskie3013702011-06-02 00:10:43 +00001335 state = sci_port->sm.current_state_id;
Piotr Sawicki8bc80d32011-05-11 23:52:31 +00001336 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001337 case SCI_PORT_STOPPED:
Piotr Sawicki8bc80d32011-05-11 23:52:31 +00001338 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001339 case SCI_PORT_SUB_WAITING:
1340 case SCI_PORT_SUB_OPERATIONAL:
1341 case SCI_PORT_SUB_CONFIGURING:
1342 case SCI_PORT_RESETTING:
Piotr Sawicki8bc80d32011-05-11 23:52:31 +00001343 port_state_machine_change(sci_port,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001344 SCI_PORT_STOPPING);
Piotr Sawicki8bc80d32011-05-11 23:52:31 +00001345 return SCI_SUCCESS;
1346 default:
1347 dev_warn(sciport_to_dev(sci_port),
1348 "%s: in wrong state: %d\n", __func__, state);
1349 return SCI_FAILURE_INVALID_STATE;
1350 }
1351}
1352
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001353static enum sci_status scic_port_hard_reset(struct scic_sds_port *sci_port, u32 timeout)
1354{
1355 enum sci_status status = SCI_FAILURE_INVALID_PHY;
1356 struct scic_sds_phy *selected_phy = NULL;
1357 enum scic_sds_port_states state;
1358 u32 phy_index;
1359
Edmund Nadolskie3013702011-06-02 00:10:43 +00001360 state = sci_port->sm.current_state_id;
1361 if (state != SCI_PORT_SUB_OPERATIONAL) {
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001362 dev_warn(sciport_to_dev(sci_port),
1363 "%s: in wrong state: %d\n", __func__, state);
1364 return SCI_FAILURE_INVALID_STATE;
1365 }
1366
1367 /* Select a phy on which we can send the hard reset request. */
1368 for (phy_index = 0; phy_index < SCI_MAX_PHYS && !selected_phy; phy_index++) {
1369 selected_phy = sci_port->phy_table[phy_index];
1370 if (selected_phy &&
1371 !scic_sds_port_active_phy(sci_port, selected_phy)) {
1372 /*
1373 * We found a phy but it is not ready select
1374 * different phy
1375 */
1376 selected_phy = NULL;
1377 }
1378 }
1379
1380 /* If we have a phy then go ahead and start the reset procedure */
1381 if (!selected_phy)
1382 return status;
1383 status = scic_sds_phy_reset(selected_phy);
1384
1385 if (status != SCI_SUCCESS)
1386 return status;
1387
Edmund Nadolski5553ba22011-05-19 11:59:10 +00001388 sci_mod_timer(&sci_port->timer, timeout);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001389 sci_port->not_ready_reason = SCIC_PORT_NOT_READY_HARD_RESET_REQUESTED;
1390
1391 port_state_machine_change(sci_port,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001392 SCI_PORT_RESETTING);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001393 return SCI_SUCCESS;
1394}
1395
1396/**
1397 * scic_sds_port_add_phy() -
1398 * @sci_port: This parameter specifies the port in which the phy will be added.
1399 * @sci_phy: This parameter is the phy which is to be added to the port.
1400 *
1401 * This method will add a PHY to the selected port. This method returns an
1402 * enum sci_status. SCI_SUCCESS the phy has been added to the port. Any other
1403 * status is a failure to add the phy to the port.
1404 */
1405enum sci_status scic_sds_port_add_phy(struct scic_sds_port *sci_port,
1406 struct scic_sds_phy *sci_phy)
1407{
1408 enum sci_status status;
1409 enum scic_sds_port_states state;
1410
Edmund Nadolskie3013702011-06-02 00:10:43 +00001411 state = sci_port->sm.current_state_id;
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001412 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001413 case SCI_PORT_STOPPED: {
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001414 struct sci_sas_address port_sas_address;
1415
1416 /* Read the port assigned SAS Address if there is one */
1417 scic_sds_port_get_sas_address(sci_port, &port_sas_address);
1418
1419 if (port_sas_address.high != 0 && port_sas_address.low != 0) {
1420 struct sci_sas_address phy_sas_address;
1421
1422 /* Make sure that the PHY SAS Address matches the SAS Address
1423 * for this port
1424 */
1425 scic_sds_phy_get_sas_address(sci_phy, &phy_sas_address);
1426
1427 if (port_sas_address.high != phy_sas_address.high ||
1428 port_sas_address.low != phy_sas_address.low)
1429 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
1430 }
1431 return scic_sds_port_set_phy(sci_port, sci_phy);
1432 }
Edmund Nadolskie3013702011-06-02 00:10:43 +00001433 case SCI_PORT_SUB_WAITING:
1434 case SCI_PORT_SUB_OPERATIONAL:
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001435 status = scic_sds_port_set_phy(sci_port, sci_phy);
1436
1437 if (status != SCI_SUCCESS)
1438 return status;
1439
1440 scic_sds_port_general_link_up_handler(sci_port, sci_phy, true);
1441 sci_port->not_ready_reason = SCIC_PORT_NOT_READY_RECONFIGURING;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001442 port_state_machine_change(sci_port, SCI_PORT_SUB_CONFIGURING);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001443
1444 return status;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001445 case SCI_PORT_SUB_CONFIGURING:
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001446 status = scic_sds_port_set_phy(sci_port, sci_phy);
1447
1448 if (status != SCI_SUCCESS)
1449 return status;
1450 scic_sds_port_general_link_up_handler(sci_port, sci_phy, true);
1451
1452 /* Re-enter the configuring state since this may be the last phy in
1453 * the port.
1454 */
1455 port_state_machine_change(sci_port,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001456 SCI_PORT_SUB_CONFIGURING);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001457 return SCI_SUCCESS;
1458 default:
1459 dev_warn(sciport_to_dev(sci_port),
1460 "%s: in wrong state: %d\n", __func__, state);
1461 return SCI_FAILURE_INVALID_STATE;
1462 }
1463}
1464
1465/**
1466 * scic_sds_port_remove_phy() -
1467 * @sci_port: This parameter specifies the port in which the phy will be added.
1468 * @sci_phy: This parameter is the phy which is to be added to the port.
1469 *
1470 * This method will remove the PHY from the selected PORT. This method returns
1471 * an enum sci_status. SCI_SUCCESS the phy has been removed from the port. Any
1472 * other status is a failure to add the phy to the port.
1473 */
1474enum sci_status scic_sds_port_remove_phy(struct scic_sds_port *sci_port,
1475 struct scic_sds_phy *sci_phy)
1476{
1477 enum sci_status status;
1478 enum scic_sds_port_states state;
1479
Edmund Nadolskie3013702011-06-02 00:10:43 +00001480 state = sci_port->sm.current_state_id;
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001481
1482 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001483 case SCI_PORT_STOPPED:
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001484 return scic_sds_port_clear_phy(sci_port, sci_phy);
Edmund Nadolskie3013702011-06-02 00:10:43 +00001485 case SCI_PORT_SUB_OPERATIONAL:
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001486 status = scic_sds_port_clear_phy(sci_port, sci_phy);
1487 if (status != SCI_SUCCESS)
1488 return status;
1489
1490 scic_sds_port_deactivate_phy(sci_port, sci_phy, true);
1491 sci_port->not_ready_reason = SCIC_PORT_NOT_READY_RECONFIGURING;
1492 port_state_machine_change(sci_port,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001493 SCI_PORT_SUB_CONFIGURING);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001494 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001495 case SCI_PORT_SUB_CONFIGURING:
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001496 status = scic_sds_port_clear_phy(sci_port, sci_phy);
1497
1498 if (status != SCI_SUCCESS)
1499 return status;
1500 scic_sds_port_deactivate_phy(sci_port, sci_phy, true);
1501
1502 /* Re-enter the configuring state since this may be the last phy in
1503 * the port
1504 */
1505 port_state_machine_change(sci_port,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001506 SCI_PORT_SUB_CONFIGURING);
Piotr Sawicki051266c2011-05-12 19:10:14 +00001507 return SCI_SUCCESS;
1508 default:
1509 dev_warn(sciport_to_dev(sci_port),
1510 "%s: in wrong state: %d\n", __func__, state);
1511 return SCI_FAILURE_INVALID_STATE;
1512 }
1513}
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001514
Piotr Sawicki051266c2011-05-12 19:10:14 +00001515enum sci_status scic_sds_port_link_up(struct scic_sds_port *sci_port,
1516 struct scic_sds_phy *sci_phy)
1517{
1518 enum scic_sds_port_states state;
1519
Edmund Nadolskie3013702011-06-02 00:10:43 +00001520 state = sci_port->sm.current_state_id;
Piotr Sawicki051266c2011-05-12 19:10:14 +00001521 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001522 case SCI_PORT_SUB_WAITING:
Piotr Sawicki051266c2011-05-12 19:10:14 +00001523 /* Since this is the first phy going link up for the port we
1524 * can just enable it and continue
1525 */
1526 scic_sds_port_activate_phy(sci_port, sci_phy, true);
1527
1528 port_state_machine_change(sci_port,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001529 SCI_PORT_SUB_OPERATIONAL);
Piotr Sawicki051266c2011-05-12 19:10:14 +00001530 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001531 case SCI_PORT_SUB_OPERATIONAL:
Piotr Sawicki051266c2011-05-12 19:10:14 +00001532 scic_sds_port_general_link_up_handler(sci_port, sci_phy, true);
1533 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001534 case SCI_PORT_RESETTING:
Piotr Sawicki051266c2011-05-12 19:10:14 +00001535 /* TODO We should make sure that the phy that has gone
1536 * link up is the same one on which we sent the reset. It is
1537 * possible that the phy on which we sent the reset is not the
1538 * one that has gone link up and we want to make sure that
1539 * phy being reset comes back. Consider the case where a
1540 * reset is sent but before the hardware processes the reset it
1541 * get a link up on the port because of a hot plug event.
1542 * because of the reset request this phy will go link down
1543 * almost immediately.
1544 */
1545
1546 /* In the resetting state we don't notify the user regarding
1547 * link up and link down notifications.
1548 */
1549 scic_sds_port_general_link_up_handler(sci_port, sci_phy, false);
1550 return SCI_SUCCESS;
1551 default:
1552 dev_warn(sciport_to_dev(sci_port),
1553 "%s: in wrong state: %d\n", __func__, state);
1554 return SCI_FAILURE_INVALID_STATE;
1555 }
1556}
1557
1558enum sci_status scic_sds_port_link_down(struct scic_sds_port *sci_port,
1559 struct scic_sds_phy *sci_phy)
1560{
1561 enum scic_sds_port_states state;
1562
Edmund Nadolskie3013702011-06-02 00:10:43 +00001563 state = sci_port->sm.current_state_id;
Piotr Sawicki051266c2011-05-12 19:10:14 +00001564 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001565 case SCI_PORT_SUB_OPERATIONAL:
Piotr Sawicki051266c2011-05-12 19:10:14 +00001566 scic_sds_port_deactivate_phy(sci_port, sci_phy, true);
1567
1568 /* If there are no active phys left in the port, then
1569 * transition the port to the WAITING state until such time
1570 * as a phy goes link up
1571 */
1572 if (sci_port->active_phy_mask == 0)
1573 port_state_machine_change(sci_port,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001574 SCI_PORT_SUB_WAITING);
Piotr Sawicki051266c2011-05-12 19:10:14 +00001575 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001576 case SCI_PORT_RESETTING:
Piotr Sawicki051266c2011-05-12 19:10:14 +00001577 /* In the resetting state we don't notify the user regarding
1578 * link up and link down notifications. */
1579 scic_sds_port_deactivate_phy(sci_port, sci_phy, false);
Piotr Sawickibd6713b2011-05-12 19:10:03 +00001580 return SCI_SUCCESS;
1581 default:
1582 dev_warn(sciport_to_dev(sci_port),
1583 "%s: in wrong state: %d\n", __func__, state);
1584 return SCI_FAILURE_INVALID_STATE;
1585 }
1586}
1587
Dan Williams68138202011-05-12 07:16:06 -07001588enum sci_status scic_sds_port_start_io(struct scic_sds_port *sci_port,
1589 struct scic_sds_remote_device *sci_dev,
1590 struct scic_sds_request *sci_req)
1591{
1592 enum scic_sds_port_states state;
Dan Williamse2f8db52011-05-10 02:28:46 -07001593
Edmund Nadolskie3013702011-06-02 00:10:43 +00001594 state = sci_port->sm.current_state_id;
Dan Williams68138202011-05-12 07:16:06 -07001595 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001596 case SCI_PORT_SUB_WAITING:
Dan Williams68138202011-05-12 07:16:06 -07001597 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001598 case SCI_PORT_SUB_OPERATIONAL:
Dan Williams68138202011-05-12 07:16:06 -07001599 sci_port->started_request_count++;
1600 return SCI_SUCCESS;
1601 default:
1602 dev_warn(sciport_to_dev(sci_port),
1603 "%s: in wrong state: %d\n", __func__, state);
1604 return SCI_FAILURE_INVALID_STATE;
1605 }
1606}
1607
1608enum sci_status scic_sds_port_complete_io(struct scic_sds_port *sci_port,
1609 struct scic_sds_remote_device *sci_dev,
1610 struct scic_sds_request *sci_req)
1611{
1612 enum scic_sds_port_states state;
1613
Edmund Nadolskie3013702011-06-02 00:10:43 +00001614 state = sci_port->sm.current_state_id;
Dan Williams68138202011-05-12 07:16:06 -07001615 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001616 case SCI_PORT_STOPPED:
Dan Williams68138202011-05-12 07:16:06 -07001617 dev_warn(sciport_to_dev(sci_port),
1618 "%s: in wrong state: %d\n", __func__, state);
1619 return SCI_FAILURE_INVALID_STATE;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001620 case SCI_PORT_STOPPING:
Dan Williams68138202011-05-12 07:16:06 -07001621 scic_sds_port_decrement_request_count(sci_port);
1622
1623 if (sci_port->started_request_count == 0)
1624 port_state_machine_change(sci_port,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001625 SCI_PORT_STOPPED);
Dan Williams68138202011-05-12 07:16:06 -07001626 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001627 case SCI_PORT_READY:
1628 case SCI_PORT_RESETTING:
1629 case SCI_PORT_FAILED:
1630 case SCI_PORT_SUB_WAITING:
1631 case SCI_PORT_SUB_OPERATIONAL:
Dan Williams68138202011-05-12 07:16:06 -07001632 scic_sds_port_decrement_request_count(sci_port);
1633 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001634 case SCI_PORT_SUB_CONFIGURING:
Dan Williams68138202011-05-12 07:16:06 -07001635 scic_sds_port_decrement_request_count(sci_port);
1636 if (sci_port->started_request_count == 0) {
1637 port_state_machine_change(sci_port,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001638 SCI_PORT_SUB_OPERATIONAL);
Dan Williams68138202011-05-12 07:16:06 -07001639 }
1640 break;
1641 }
1642 return SCI_SUCCESS;
1643}
Dan Williamse2f8db52011-05-10 02:28:46 -07001644
1645/**
1646 *
1647 * @sci_port: This is the port object which to suspend.
1648 *
1649 * This method will enable the SCU Port Task Scheduler for this port object but
1650 * will leave the port task scheduler in a suspended state. none
1651 */
1652static void
1653scic_sds_port_enable_port_task_scheduler(struct scic_sds_port *port)
1654{
1655 u32 pts_control_value;
1656
1657 pts_control_value = readl(&port->port_task_scheduler_registers->control);
1658 pts_control_value |= SCU_PTSxCR_GEN_BIT(ENABLE) | SCU_PTSxCR_GEN_BIT(SUSPEND);
1659 writel(pts_control_value, &port->port_task_scheduler_registers->control);
1660}
1661
1662/**
1663 *
1664 * @sci_port: This is the port object which to resume.
1665 *
1666 * This method will disable the SCU port task scheduler for this port object.
1667 * none
1668 */
1669static void
1670scic_sds_port_disable_port_task_scheduler(struct scic_sds_port *port)
1671{
1672 u32 pts_control_value;
1673
1674 pts_control_value = readl(&port->port_task_scheduler_registers->control);
1675 pts_control_value &=
1676 ~(SCU_PTSxCR_GEN_BIT(ENABLE) | SCU_PTSxCR_GEN_BIT(SUSPEND));
1677 writel(pts_control_value, &port->port_task_scheduler_registers->control);
1678}
1679
1680static void scic_sds_port_post_dummy_remote_node(struct scic_sds_port *sci_port)
1681{
1682 struct scic_sds_controller *scic = sci_port->owning_controller;
1683 u8 phys_index = sci_port->physical_port_index;
1684 union scu_remote_node_context *rnc;
1685 u16 rni = sci_port->reserved_rni;
1686 u32 command;
1687
1688 rnc = &scic->remote_node_context_table[rni];
1689 rnc->ssp.is_valid = true;
1690
1691 command = SCU_CONTEXT_COMMAND_POST_RNC_32 |
1692 phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
1693
1694 scic_sds_controller_post_request(scic, command);
1695
1696 /* ensure hardware has seen the post rnc command and give it
1697 * ample time to act before sending the suspend
1698 */
1699 readl(&scic->smu_registers->interrupt_status); /* flush */
1700 udelay(10);
1701
1702 command = SCU_CONTEXT_COMMAND_POST_RNC_SUSPEND_TX_RX |
1703 phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
1704
1705 scic_sds_controller_post_request(scic, command);
1706}
1707
Dan Williams9269e0e2011-05-12 07:42:17 -07001708static void scic_sds_port_stopped_state_enter(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001709{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001710 struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), sm);
Dan Williamse2f8db52011-05-10 02:28:46 -07001711
Edmund Nadolskie3013702011-06-02 00:10:43 +00001712 if (sci_port->sm.previous_state_id == SCI_PORT_STOPPING) {
Dan Williamse2f8db52011-05-10 02:28:46 -07001713 /*
1714 * If we enter this state becasuse of a request to stop
1715 * the port then we want to disable the hardwares port
1716 * task scheduler. */
1717 scic_sds_port_disable_port_task_scheduler(sci_port);
1718 }
1719}
1720
Dan Williams9269e0e2011-05-12 07:42:17 -07001721static void scic_sds_port_stopped_state_exit(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001722{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001723 struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), sm);
Dan Williamse2f8db52011-05-10 02:28:46 -07001724
1725 /* Enable and suspend the port task scheduler */
1726 scic_sds_port_enable_port_task_scheduler(sci_port);
1727}
1728
Dan Williams9269e0e2011-05-12 07:42:17 -07001729static void scic_sds_port_ready_state_enter(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001730{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001731 struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), sm);
Dan Williamse2f8db52011-05-10 02:28:46 -07001732 struct scic_sds_controller *scic = sci_port->owning_controller;
1733 struct isci_host *ihost = scic_to_ihost(scic);
1734 struct isci_port *iport = sci_port_to_iport(sci_port);
1735 u32 prev_state;
1736
Edmund Nadolskie3013702011-06-02 00:10:43 +00001737 prev_state = sci_port->sm.previous_state_id;
1738 if (prev_state == SCI_PORT_RESETTING)
Dan Williamse2f8db52011-05-10 02:28:46 -07001739 isci_port_hard_reset_complete(iport, SCI_SUCCESS);
1740 else
1741 isci_port_not_ready(ihost, iport);
1742
1743 /* Post and suspend the dummy remote node context for this port. */
1744 scic_sds_port_post_dummy_remote_node(sci_port);
1745
1746 /* Start the ready substate machine */
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001747 port_state_machine_change(sci_port,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001748 SCI_PORT_SUB_WAITING);
Dan Williamse2f8db52011-05-10 02:28:46 -07001749}
1750
Dan Williams9269e0e2011-05-12 07:42:17 -07001751static void scic_sds_port_resetting_state_exit(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001752{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001753 struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), sm);
Dan Williamse2f8db52011-05-10 02:28:46 -07001754
Edmund Nadolski5553ba22011-05-19 11:59:10 +00001755 sci_del_timer(&sci_port->timer);
Dan Williamse2f8db52011-05-10 02:28:46 -07001756}
1757
Dan Williams9269e0e2011-05-12 07:42:17 -07001758static void scic_sds_port_stopping_state_exit(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001759{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001760 struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), sm);
Dan Williamse2f8db52011-05-10 02:28:46 -07001761
Edmund Nadolski5553ba22011-05-19 11:59:10 +00001762 sci_del_timer(&sci_port->timer);
Dan Williamse2f8db52011-05-10 02:28:46 -07001763
1764 scic_sds_port_destroy_dummy_resources(sci_port);
1765}
1766
Dan Williams9269e0e2011-05-12 07:42:17 -07001767static void scic_sds_port_failed_state_enter(struct sci_base_state_machine *sm)
Dan Williamse2f8db52011-05-10 02:28:46 -07001768{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001769 struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), sm);
Dan Williamse2f8db52011-05-10 02:28:46 -07001770 struct isci_port *iport = sci_port_to_iport(sci_port);
1771
Dan Williamse2f8db52011-05-10 02:28:46 -07001772 isci_port_hard_reset_complete(iport, SCI_FAILURE_TIMEOUT);
1773}
1774
1775/* --------------------------------------------------------------------------- */
1776
1777static const struct sci_base_state scic_sds_port_state_table[] = {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001778 [SCI_PORT_STOPPED] = {
Dan Williamse2f8db52011-05-10 02:28:46 -07001779 .enter_state = scic_sds_port_stopped_state_enter,
1780 .exit_state = scic_sds_port_stopped_state_exit
1781 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001782 [SCI_PORT_STOPPING] = {
Dan Williamse2f8db52011-05-10 02:28:46 -07001783 .exit_state = scic_sds_port_stopping_state_exit
1784 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001785 [SCI_PORT_READY] = {
Dan Williamse2f8db52011-05-10 02:28:46 -07001786 .enter_state = scic_sds_port_ready_state_enter,
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001787 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001788 [SCI_PORT_SUB_WAITING] = {
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001789 .enter_state = scic_sds_port_ready_substate_waiting_enter,
1790 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001791 [SCI_PORT_SUB_OPERATIONAL] = {
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001792 .enter_state = scic_sds_port_ready_substate_operational_enter,
1793 .exit_state = scic_sds_port_ready_substate_operational_exit
1794 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001795 [SCI_PORT_SUB_CONFIGURING] = {
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001796 .enter_state = scic_sds_port_ready_substate_configuring_enter,
1797 .exit_state = scic_sds_port_ready_substate_configuring_exit
Dan Williamse2f8db52011-05-10 02:28:46 -07001798 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001799 [SCI_PORT_RESETTING] = {
Dan Williamse2f8db52011-05-10 02:28:46 -07001800 .exit_state = scic_sds_port_resetting_state_exit
1801 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00001802 [SCI_PORT_FAILED] = {
Dan Williamse2f8db52011-05-10 02:28:46 -07001803 .enter_state = scic_sds_port_failed_state_enter,
1804 }
1805};
1806
1807void scic_sds_port_construct(struct scic_sds_port *sci_port, u8 index,
1808 struct scic_sds_controller *scic)
1809{
Edmund Nadolskie3013702011-06-02 00:10:43 +00001810 sci_base_state_machine_construct(&sci_port->sm,
Dan Williamse2f8db52011-05-10 02:28:46 -07001811 scic_sds_port_state_table,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001812 SCI_PORT_STOPPED);
Dan Williamse2f8db52011-05-10 02:28:46 -07001813
Edmund Nadolskie3013702011-06-02 00:10:43 +00001814 sci_base_state_machine_start(&sci_port->sm);
Dan Williamse2f8db52011-05-10 02:28:46 -07001815
Dan Williamse2f8db52011-05-10 02:28:46 -07001816 sci_port->logical_port_index = SCIC_SDS_DUMMY_PORT;
1817 sci_port->physical_port_index = index;
1818 sci_port->active_phy_mask = 0;
Piotr Sawickie91f41e2011-05-11 23:52:21 +00001819 sci_port->ready_exit = false;
Dan Williamse2f8db52011-05-10 02:28:46 -07001820
1821 sci_port->owning_controller = scic;
1822
1823 sci_port->started_request_count = 0;
1824 sci_port->assigned_device_count = 0;
1825
1826 sci_port->reserved_rni = SCU_DUMMY_INDEX;
1827 sci_port->reserved_tci = SCU_DUMMY_INDEX;
1828
Edmund Nadolski5553ba22011-05-19 11:59:10 +00001829 sci_init_timer(&sci_port->timer, port_timeout);
1830
Dan Williamse2f8db52011-05-10 02:28:46 -07001831 sci_port->port_task_scheduler_registers = NULL;
1832
1833 for (index = 0; index < SCI_MAX_PHYS; index++)
1834 sci_port->phy_table[index] = NULL;
1835}
1836
1837void isci_port_init(struct isci_port *iport, struct isci_host *ihost, int index)
1838{
1839 INIT_LIST_HEAD(&iport->remote_dev_list);
1840 INIT_LIST_HEAD(&iport->domain_dev_list);
1841 spin_lock_init(&iport->state_lock);
1842 init_completion(&iport->start_complete);
1843 iport->isci_host = ihost;
1844 isci_port_change_state(iport, isci_freed);
1845}
1846
1847/**
1848 * isci_port_get_state() - This function gets the status of the port object.
1849 * @isci_port: This parameter points to the isci_port object
1850 *
1851 * status of the object as a isci_status enum.
1852 */
1853enum isci_status isci_port_get_state(
1854 struct isci_port *isci_port)
1855{
1856 return isci_port->status;
1857}
1858
1859static void isci_port_bc_change_received(struct isci_host *ihost,
1860 struct scic_sds_port *sci_port,
1861 struct scic_sds_phy *sci_phy)
1862{
1863 struct isci_phy *iphy = sci_phy_to_iphy(sci_phy);
1864
1865 dev_dbg(&ihost->pdev->dev, "%s: iphy = %p, sas_phy = %p\n",
1866 __func__, iphy, &iphy->sas_phy);
1867
1868 ihost->sas_ha.notify_port_event(&iphy->sas_phy, PORTE_BROADCAST_RCVD);
1869 scic_port_enable_broadcast_change_notification(sci_port);
1870}
1871
1872void scic_sds_port_broadcast_change_received(
1873 struct scic_sds_port *sci_port,
1874 struct scic_sds_phy *sci_phy)
1875{
1876 struct scic_sds_controller *scic = sci_port->owning_controller;
1877 struct isci_host *ihost = scic_to_ihost(scic);
1878
1879 /* notify the user. */
1880 isci_port_bc_change_received(ihost, sci_port, sci_phy);
1881}
1882
Dan Williams4393aa42011-03-31 13:10:44 -07001883int isci_port_perform_hard_reset(struct isci_host *ihost, struct isci_port *iport,
1884 struct isci_phy *iphy)
Dan Williams6f231dd2011-07-02 22:56:22 -07001885{
Dan Williams4393aa42011-03-31 13:10:44 -07001886 unsigned long flags;
Dan Williams6f231dd2011-07-02 22:56:22 -07001887 enum sci_status status;
1888 int ret = TMF_RESP_FUNC_COMPLETE;
Dan Williams6f231dd2011-07-02 22:56:22 -07001889
Dan Williams4393aa42011-03-31 13:10:44 -07001890 dev_dbg(&ihost->pdev->dev, "%s: iport = %p\n",
1891 __func__, iport);
Dan Williams6f231dd2011-07-02 22:56:22 -07001892
Dan Williams4393aa42011-03-31 13:10:44 -07001893 init_completion(&iport->hard_reset_complete);
Dan Williams6f231dd2011-07-02 22:56:22 -07001894
Dan Williams4393aa42011-03-31 13:10:44 -07001895 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001896
1897 #define ISCI_PORT_RESET_TIMEOUT SCIC_SDS_SIGNATURE_FIS_TIMEOUT
Dan Williamse5313812011-05-07 10:11:43 -07001898 status = scic_port_hard_reset(&iport->sci, ISCI_PORT_RESET_TIMEOUT);
Dan Williams6f231dd2011-07-02 22:56:22 -07001899
Dan Williams4393aa42011-03-31 13:10:44 -07001900 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07001901
1902 if (status == SCI_SUCCESS) {
Dan Williams4393aa42011-03-31 13:10:44 -07001903 wait_for_completion(&iport->hard_reset_complete);
Dan Williams6f231dd2011-07-02 22:56:22 -07001904
Dan Williams4393aa42011-03-31 13:10:44 -07001905 dev_dbg(&ihost->pdev->dev,
1906 "%s: iport = %p; hard reset completion\n",
1907 __func__, iport);
Dan Williams6f231dd2011-07-02 22:56:22 -07001908
Dan Williams4393aa42011-03-31 13:10:44 -07001909 if (iport->hard_reset_status != SCI_SUCCESS)
Dan Williams6f231dd2011-07-02 22:56:22 -07001910 ret = TMF_RESP_FUNC_FAILED;
1911 } else {
1912 ret = TMF_RESP_FUNC_FAILED;
1913
Dan Williams4393aa42011-03-31 13:10:44 -07001914 dev_err(&ihost->pdev->dev,
1915 "%s: iport = %p; scic_port_hard_reset call"
Dan Williams6f231dd2011-07-02 22:56:22 -07001916 " failed 0x%x\n",
Dan Williams4393aa42011-03-31 13:10:44 -07001917 __func__, iport, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07001918
1919 }
1920
1921 /* If the hard reset for the port has failed, consider this
1922 * the same as link failures on all phys in the port.
1923 */
1924 if (ret != TMF_RESP_FUNC_COMPLETE) {
Dan Williams4393aa42011-03-31 13:10:44 -07001925 dev_err(&ihost->pdev->dev,
1926 "%s: iport = %p; hard reset failed "
Dan Williams6f231dd2011-07-02 22:56:22 -07001927 "(0x%x) - sending link down to libsas for phy %p\n",
Dan Williams4393aa42011-03-31 13:10:44 -07001928 __func__, iport, iport->hard_reset_status, iphy);
Dan Williams6f231dd2011-07-02 22:56:22 -07001929
Dan Williams4393aa42011-03-31 13:10:44 -07001930 isci_port_link_down(ihost, iphy, iport);
Dan Williams6f231dd2011-07-02 22:56:22 -07001931 }
1932
1933 return ret;
1934}
Dave Jiang09d7da12011-03-26 16:11:51 -07001935
Dan Williamse2f8db52011-05-10 02:28:46 -07001936/**
1937 * isci_port_deformed() - This function is called by libsas when a port becomes
1938 * inactive.
1939 * @phy: This parameter specifies the libsas phy with the inactive port.
1940 *
1941 */
1942void isci_port_deformed(struct asd_sas_phy *phy)
Dave Jiang09d7da12011-03-26 16:11:51 -07001943{
Dan Williamse2f8db52011-05-10 02:28:46 -07001944 pr_debug("%s: sas_phy = %p\n", __func__, phy);
1945}
1946
1947/**
1948 * isci_port_formed() - This function is called by libsas when a port becomes
1949 * active.
1950 * @phy: This parameter specifies the libsas phy with the active port.
1951 *
1952 */
1953void isci_port_formed(struct asd_sas_phy *phy)
1954{
1955 pr_debug("%s: sas_phy = %p, sas_port = %p\n", __func__, phy, phy->port);
Dave Jiang09d7da12011-03-26 16:11:51 -07001956}