]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/scsi/isci/port.c
3da9048b45b7e6a04b104b146de46482f618c92f
[karo-tx-linux.git] / drivers / scsi / isci / port.c
1 /*
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
56 #include "isci.h"
57 #include "port.h"
58 #include "request.h"
59 #include "timers.h"
60
61 #define SCIC_SDS_PORT_HARD_RESET_TIMEOUT  (1000)
62 #define SCU_DUMMY_INDEX    (0xFFFF)
63
64 static void isci_port_change_state(struct isci_port *iport, enum isci_status status)
65 {
66         unsigned long flags;
67
68         dev_dbg(&iport->isci_host->pdev->dev,
69                 "%s: iport = %p, state = 0x%x\n",
70                 __func__, iport, status);
71
72         /* XXX pointless lock */
73         spin_lock_irqsave(&iport->state_lock, flags);
74         iport->status = status;
75         spin_unlock_irqrestore(&iport->state_lock, flags);
76 }
77
78 /*
79  * This function will indicate which protocols are supported by this port.
80  * @sci_port: a handle corresponding to the SAS port for which to return the
81  *    supported protocols.
82  * @protocols: This parameter specifies a pointer to a data structure
83  *    which the core will copy the protocol values for the port from the
84  *    transmit_identification register.
85  */
86 static void
87 scic_sds_port_get_protocols(struct scic_sds_port *sci_port,
88                             struct scic_phy_proto *protocols)
89 {
90         u8 index;
91
92         protocols->all = 0;
93
94         for (index = 0; index < SCI_MAX_PHYS; index++) {
95                 if (sci_port->phy_table[index] != NULL) {
96                         scic_sds_phy_get_protocols(sci_port->phy_table[index],
97                                                    protocols);
98                 }
99         }
100 }
101
102 /**
103  * This method requests a list (mask) of the phys contained in the supplied SAS
104  *    port.
105  * @sci_port: a handle corresponding to the SAS port for which to return the
106  *    phy mask.
107  *
108  * Return a bit mask indicating which phys are a part of this port. Each bit
109  * corresponds to a phy identifier (e.g. bit 0 = phy id 0).
110  */
111 static u32 scic_sds_port_get_phys(struct scic_sds_port *sci_port)
112 {
113         u32 index;
114         u32 mask;
115
116         mask = 0;
117
118         for (index = 0; index < SCI_MAX_PHYS; index++) {
119                 if (sci_port->phy_table[index] != NULL) {
120                         mask |= (1 << index);
121                 }
122         }
123
124         return mask;
125 }
126
127 /**
128  * scic_port_get_properties() - This method simply returns the properties
129  *    regarding the port, such as: physical index, protocols, sas address, etc.
130  * @port: this parameter specifies the port for which to retrieve the physical
131  *    index.
132  * @properties: This parameter specifies the properties structure into which to
133  *    copy the requested information.
134  *
135  * Indicate if the user specified a valid port. SCI_SUCCESS This value is
136  * returned if the specified port was valid. SCI_FAILURE_INVALID_PORT This
137  * value is returned if the specified port is not valid.  When this value is
138  * returned, no data is copied to the properties output parameter.
139  */
140 static enum sci_status scic_port_get_properties(struct scic_sds_port *port,
141                                                 struct scic_port_properties *prop)
142 {
143         if ((port == NULL) ||
144             (port->logical_port_index == SCIC_SDS_DUMMY_PORT))
145                 return SCI_FAILURE_INVALID_PORT;
146
147         prop->index    = port->logical_port_index;
148         prop->phy_mask = scic_sds_port_get_phys(port);
149         scic_sds_port_get_sas_address(port, &prop->local.sas_address);
150         scic_sds_port_get_protocols(port, &prop->local.protocols);
151         scic_sds_port_get_attached_sas_address(port, &prop->remote.sas_address);
152
153         return SCI_SUCCESS;
154 }
155
156 static void isci_port_link_up(struct isci_host *isci_host,
157                               struct scic_sds_port *port,
158                               struct scic_sds_phy *phy)
159 {
160         unsigned long flags;
161         struct scic_port_properties properties;
162         struct isci_phy *isci_phy = sci_phy_to_iphy(phy);
163         struct isci_port *isci_port = sci_port_to_iport(port);
164         unsigned long success = true;
165
166         BUG_ON(isci_phy->isci_port != NULL);
167
168         isci_phy->isci_port = isci_port;
169
170         dev_dbg(&isci_host->pdev->dev,
171                 "%s: isci_port = %p\n",
172                 __func__, isci_port);
173
174         spin_lock_irqsave(&isci_phy->sas_phy.frame_rcvd_lock, flags);
175
176         isci_port_change_state(isci_phy->isci_port, isci_starting);
177
178         scic_port_get_properties(port, &properties);
179
180         if (phy->protocol == SCIC_SDS_PHY_PROTOCOL_SATA) {
181                 u64 attached_sas_address;
182
183                 isci_phy->sas_phy.oob_mode = SATA_OOB_MODE;
184                 isci_phy->sas_phy.frame_rcvd_size = sizeof(struct dev_to_host_fis);
185
186                 /*
187                  * For direct-attached SATA devices, the SCI core will
188                  * automagically assign a SAS address to the end device
189                  * for the purpose of creating a port. This SAS address
190                  * will not be the same as assigned to the PHY and needs
191                  * to be obtained from struct scic_port_properties properties.
192                  */
193                 attached_sas_address = properties.remote.sas_address.high;
194                 attached_sas_address <<= 32;
195                 attached_sas_address |= properties.remote.sas_address.low;
196                 swab64s(&attached_sas_address);
197
198                 memcpy(&isci_phy->sas_phy.attached_sas_addr,
199                        &attached_sas_address, sizeof(attached_sas_address));
200         } else if (phy->protocol == SCIC_SDS_PHY_PROTOCOL_SAS) {
201                 isci_phy->sas_phy.oob_mode = SAS_OOB_MODE;
202                 isci_phy->sas_phy.frame_rcvd_size = sizeof(struct sas_identify_frame);
203
204                 /* Copy the attached SAS address from the IAF */
205                 memcpy(isci_phy->sas_phy.attached_sas_addr,
206                        isci_phy->frame_rcvd.iaf.sas_addr, SAS_ADDR_SIZE);
207         } else {
208                 dev_err(&isci_host->pdev->dev, "%s: unkown target\n", __func__);
209                 success = false;
210         }
211
212         isci_phy->sas_phy.phy->negotiated_linkrate = sci_phy_linkrate(phy);
213
214         spin_unlock_irqrestore(&isci_phy->sas_phy.frame_rcvd_lock, flags);
215
216         /* Notify libsas that we have an address frame, if indeed
217          * we've found an SSP, SMP, or STP target */
218         if (success)
219                 isci_host->sas_ha.notify_port_event(&isci_phy->sas_phy,
220                                                     PORTE_BYTES_DMAED);
221 }
222
223
224 /**
225  * isci_port_link_down() - This function is called by the sci core when a link
226  *    becomes inactive.
227  * @isci_host: This parameter specifies the isci host object.
228  * @phy: This parameter specifies the isci phy with the active link.
229  * @port: This parameter specifies the isci port with the active link.
230  *
231  */
232 static void isci_port_link_down(struct isci_host *isci_host,
233                                 struct isci_phy *isci_phy,
234                                 struct isci_port *isci_port)
235 {
236         struct isci_remote_device *isci_device;
237
238         dev_dbg(&isci_host->pdev->dev,
239                 "%s: isci_port = %p\n", __func__, isci_port);
240
241         if (isci_port) {
242
243                 /* check to see if this is the last phy on this port. */
244                 if (isci_phy->sas_phy.port
245                     && isci_phy->sas_phy.port->num_phys == 1) {
246
247                         /* change the state for all devices on this port.
248                          * The next task sent to this device will be returned
249                          * as SAS_TASK_UNDELIVERED, and the scsi mid layer
250                          * will remove the target
251                          */
252                         list_for_each_entry(isci_device,
253                                             &isci_port->remote_dev_list,
254                                             node) {
255                                 dev_dbg(&isci_host->pdev->dev,
256                                         "%s: isci_device = %p\n",
257                                         __func__, isci_device);
258                                 isci_remote_device_change_state(isci_device,
259                                                                 isci_stopping);
260                         }
261                 }
262                 isci_port_change_state(isci_port, isci_stopping);
263         }
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 /**
280  * 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  */
286 static void isci_port_ready(struct isci_host *isci_host, struct isci_port *isci_port)
287 {
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  */
304 static void isci_port_not_ready(struct isci_host *isci_host, struct isci_port *isci_port)
305 {
306         dev_dbg(&isci_host->pdev->dev,
307                 "%s: isci_port = %p\n", __func__, isci_port);
308 }
309
310 static void isci_port_stop_complete(struct scic_sds_controller *scic,
311                                     struct scic_sds_port *sci_port,
312                                     enum sci_status completion_status)
313 {
314         dev_dbg(&scic_to_ihost(scic)->pdev->dev, "Port stop complete\n");
315 }
316
317 /**
318  * isci_port_hard_reset_complete() - This function is called by the sci core
319  *    when the hard reset complete notification has been received.
320  * @port: This parameter specifies the sci port with the active link.
321  * @completion_status: This parameter specifies the core status for the reset
322  *    process.
323  *
324  */
325 static void isci_port_hard_reset_complete(struct isci_port *isci_port,
326                                           enum sci_status completion_status)
327 {
328         dev_dbg(&isci_port->isci_host->pdev->dev,
329                 "%s: isci_port = %p, completion_status=%x\n",
330                      __func__, isci_port, completion_status);
331
332         /* Save the status of the hard reset from the port. */
333         isci_port->hard_reset_status = completion_status;
334
335         complete_all(&isci_port->hard_reset_complete);
336 }
337
338 /* This method will return a true value if the specified phy can be assigned to
339  * this port The following is a list of phys for each port that are allowed: -
340  * Port 0 - 3 2 1 0 - Port 1 -     1 - Port 2 - 3 2 - Port 3 - 3 This method
341  * doesn't preclude all configurations.  It merely ensures that a phy is part
342  * of the allowable set of phy identifiers for that port.  For example, one
343  * could assign phy 3 to port 0 and no other phys.  Please refer to
344  * scic_sds_port_is_phy_mask_valid() for information regarding whether the
345  * phy_mask for a port can be supported. bool true if this is a valid phy
346  * assignment for the port false if this is not a valid phy assignment for the
347  * port
348  */
349 bool scic_sds_port_is_valid_phy_assignment(struct scic_sds_port *sci_port,
350                                            u32 phy_index)
351 {
352         /* Initialize to invalid value. */
353         u32 existing_phy_index = SCI_MAX_PHYS;
354         u32 index;
355
356         if ((sci_port->physical_port_index == 1) && (phy_index != 1)) {
357                 return false;
358         }
359
360         if (sci_port->physical_port_index == 3 && phy_index != 3) {
361                 return false;
362         }
363
364         if (
365                 (sci_port->physical_port_index == 2)
366                 && ((phy_index == 0) || (phy_index == 1))
367                 ) {
368                 return false;
369         }
370
371         for (index = 0; index < SCI_MAX_PHYS; index++) {
372                 if ((sci_port->phy_table[index] != NULL)
373                     && (index != phy_index)) {
374                         existing_phy_index = index;
375                 }
376         }
377
378         /*
379          * Ensure that all of the phys in the port are capable of
380          * operating at the same maximum link rate. */
381         if (
382                 (existing_phy_index < SCI_MAX_PHYS)
383                 && (sci_port->owning_controller->user_parameters.sds1.phys[
384                             phy_index].max_speed_generation !=
385                     sci_port->owning_controller->user_parameters.sds1.phys[
386                             existing_phy_index].max_speed_generation)
387                 )
388                 return false;
389
390         return true;
391 }
392
393 /**
394  *
395  * @sci_port: This is the port object for which to determine if the phy mask
396  *    can be supported.
397  *
398  * This method will return a true value if the port's phy mask can be supported
399  * by the SCU. The following is a list of valid PHY mask configurations for
400  * each port: - Port 0 - [[3  2] 1] 0 - Port 1 -        [1] - Port 2 - [[3] 2]
401  * - Port 3 -  [3] This method returns a boolean indication specifying if the
402  * phy mask can be supported. true if this is a valid phy assignment for the
403  * port false if this is not a valid phy assignment for the port
404  */
405 static bool scic_sds_port_is_phy_mask_valid(
406         struct scic_sds_port *sci_port,
407         u32 phy_mask)
408 {
409         if (sci_port->physical_port_index == 0) {
410                 if (((phy_mask & 0x0F) == 0x0F)
411                     || ((phy_mask & 0x03) == 0x03)
412                     || ((phy_mask & 0x01) == 0x01)
413                     || (phy_mask == 0))
414                         return true;
415         } else if (sci_port->physical_port_index == 1) {
416                 if (((phy_mask & 0x02) == 0x02)
417                     || (phy_mask == 0))
418                         return true;
419         } else if (sci_port->physical_port_index == 2) {
420                 if (((phy_mask & 0x0C) == 0x0C)
421                     || ((phy_mask & 0x04) == 0x04)
422                     || (phy_mask == 0))
423                         return true;
424         } else if (sci_port->physical_port_index == 3) {
425                 if (((phy_mask & 0x08) == 0x08)
426                     || (phy_mask == 0))
427                         return true;
428         }
429
430         return false;
431 }
432
433 /**
434  *
435  * @sci_port: This parameter specifies the port from which to return a
436  *    connected phy.
437  *
438  * This method retrieves a currently active (i.e. connected) phy contained in
439  * the port.  Currently, the lowest order phy that is connected is returned.
440  * This method returns a pointer to a SCIS_SDS_PHY object. NULL This value is
441  * returned if there are no currently active (i.e. connected to a remote end
442  * point) phys contained in the port. All other values specify a struct scic_sds_phy
443  * object that is active in the port.
444  */
445 static struct scic_sds_phy *scic_sds_port_get_a_connected_phy(
446         struct scic_sds_port *sci_port
447         ) {
448         u32 index;
449         struct scic_sds_phy *phy;
450
451         for (index = 0; index < SCI_MAX_PHYS; index++) {
452                 /*
453                  * Ensure that the phy is both part of the port and currently
454                  * connected to the remote end-point. */
455                 phy = sci_port->phy_table[index];
456                 if (
457                         (phy != NULL)
458                         && scic_sds_port_active_phy(sci_port, phy)
459                         ) {
460                         return phy;
461                 }
462         }
463
464         return NULL;
465 }
466
467 /**
468  * scic_sds_port_set_phy() -
469  * @out]: port The port object to which the phy assignement is being made.
470  * @out]: phy The phy which is being assigned to the port.
471  *
472  * This method attempts to make the assignment of the phy to the port. If
473  * successful the phy is assigned to the ports phy table. bool true if the phy
474  * assignment can be made. false if the phy assignement can not be made. This
475  * is a functional test that only fails if the phy is currently assigned to a
476  * different port.
477  */
478 static enum sci_status scic_sds_port_set_phy(
479         struct scic_sds_port *port,
480         struct scic_sds_phy *phy)
481 {
482         /*
483          * Check to see if we can add this phy to a port
484          * that means that the phy is not part of a port and that the port does
485          * not already have a phy assinged to the phy index. */
486         if (
487                 (port->phy_table[phy->phy_index] == NULL)
488                 && (phy_get_non_dummy_port(phy) == NULL)
489                 && scic_sds_port_is_valid_phy_assignment(port, phy->phy_index)
490                 ) {
491                 /*
492                  * Phy is being added in the stopped state so we are in MPC mode
493                  * make logical port index = physical port index */
494                 port->logical_port_index = port->physical_port_index;
495                 port->phy_table[phy->phy_index] = phy;
496                 scic_sds_phy_set_port(phy, port);
497
498                 return SCI_SUCCESS;
499         }
500
501         return SCI_FAILURE;
502 }
503
504 /**
505  * scic_sds_port_clear_phy() -
506  * @out]: port The port from which the phy is being cleared.
507  * @out]: phy The phy being cleared from the port.
508  *
509  * This method will clear the phy assigned to this port.  This method fails if
510  * this phy is not currently assinged to this port. bool true if the phy is
511  * removed from the port. false if this phy is not assined to this port.
512  */
513 static enum sci_status scic_sds_port_clear_phy(
514         struct scic_sds_port *port,
515         struct scic_sds_phy *phy)
516 {
517         /* Make sure that this phy is part of this port */
518         if (port->phy_table[phy->phy_index] == phy &&
519             phy_get_non_dummy_port(phy) == port) {
520                 struct scic_sds_controller *scic = port->owning_controller;
521                 struct isci_host *ihost = scic_to_ihost(scic);
522
523                 /* Yep it is assigned to this port so remove it */
524                 scic_sds_phy_set_port(phy, &ihost->ports[SCI_MAX_PORTS].sci);
525                 port->phy_table[phy->phy_index] = NULL;
526                 return SCI_SUCCESS;
527         }
528
529         return SCI_FAILURE;
530 }
531
532
533 /**
534  * This method requests the SAS address for the supplied SAS port from the SCI
535  *    implementation.
536  * @sci_port: a handle corresponding to the SAS port for which to return the
537  *    SAS address.
538  * @sas_address: This parameter specifies a pointer to a SAS address structure
539  *    into which the core will copy the SAS address for the port.
540  *
541  */
542 void scic_sds_port_get_sas_address(
543         struct scic_sds_port *sci_port,
544         struct sci_sas_address *sas_address)
545 {
546         u32 index;
547
548         sas_address->high = 0;
549         sas_address->low  = 0;
550
551         for (index = 0; index < SCI_MAX_PHYS; index++) {
552                 if (sci_port->phy_table[index] != NULL) {
553                         scic_sds_phy_get_sas_address(sci_port->phy_table[index], sas_address);
554                 }
555         }
556 }
557
558 /*
559  * This function requests the SAS address for the device directly attached to
560  *    this SAS port.
561  * @sci_port: a handle corresponding to the SAS port for which to return the
562  *    SAS address.
563  * @sas_address: This parameter specifies a pointer to a SAS address structure
564  *    into which the core will copy the SAS address for the device directly
565  *    attached to the port.
566  *
567  */
568 void scic_sds_port_get_attached_sas_address(
569         struct scic_sds_port *sci_port,
570         struct sci_sas_address *sas_address)
571 {
572         struct scic_sds_phy *sci_phy;
573
574         /*
575          * Ensure that the phy is both part of the port and currently
576          * connected to the remote end-point.
577          */
578         sci_phy = scic_sds_port_get_a_connected_phy(sci_port);
579         if (sci_phy) {
580                 if (sci_phy->protocol != SCIC_SDS_PHY_PROTOCOL_SATA) {
581                         scic_sds_phy_get_attached_sas_address(sci_phy,
582                                                               sas_address);
583                 } else {
584                         scic_sds_phy_get_sas_address(sci_phy, sas_address);
585                         sas_address->low += sci_phy->phy_index;
586                 }
587         } else {
588                 sas_address->high = 0;
589                 sas_address->low  = 0;
590         }
591 }
592
593 /**
594  * scic_sds_port_construct_dummy_rnc() - create dummy rnc for si workaround
595  *
596  * @sci_port: logical port on which we need to create the remote node context
597  * @rni: remote node index for this remote node context.
598  *
599  * This routine will construct a dummy remote node context data structure
600  * This structure will be posted to the hardware to work around a scheduler
601  * error in the hardware.
602  */
603 static void scic_sds_port_construct_dummy_rnc(struct scic_sds_port *sci_port, u16 rni)
604 {
605         union scu_remote_node_context *rnc;
606
607         rnc = &sci_port->owning_controller->remote_node_context_table[rni];
608
609         memset(rnc, 0, sizeof(union scu_remote_node_context));
610
611         rnc->ssp.remote_sas_address_hi = 0;
612         rnc->ssp.remote_sas_address_lo = 0;
613
614         rnc->ssp.remote_node_index = rni;
615         rnc->ssp.remote_node_port_width = 1;
616         rnc->ssp.logical_port_index = sci_port->physical_port_index;
617
618         rnc->ssp.nexus_loss_timer_enable = false;
619         rnc->ssp.check_bit = false;
620         rnc->ssp.is_valid = true;
621         rnc->ssp.is_remote_node_context = true;
622         rnc->ssp.function_number = 0;
623         rnc->ssp.arbitration_wait_time = 0;
624 }
625
626 /**
627  * scic_sds_port_construct_dummy_task() - create dummy task for si workaround
628  * @sci_port The logical port on which we need to create the
629  *            remote node context.
630  *            context.
631  * @tci The remote node index for this remote node context.
632  *
633  * This routine will construct a dummy task context data structure.  This
634  * structure will be posted to the hardwre to work around a scheduler error
635  * in the hardware.
636  *
637  */
638 static void scic_sds_port_construct_dummy_task(struct scic_sds_port *sci_port, u16 tci)
639 {
640         struct scu_task_context *task_context;
641
642         task_context = scic_sds_controller_get_task_context_buffer(sci_port->owning_controller, tci);
643
644         memset(task_context, 0, sizeof(struct scu_task_context));
645
646         task_context->abort = 0;
647         task_context->priority = 0;
648         task_context->initiator_request = 1;
649         task_context->connection_rate = 1;
650         task_context->protocol_engine_index = 0;
651         task_context->logical_port_index = sci_port->physical_port_index;
652         task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SSP;
653         task_context->task_index = scic_sds_io_tag_get_index(tci);
654         task_context->valid = SCU_TASK_CONTEXT_VALID;
655         task_context->context_type = SCU_TASK_CONTEXT_TYPE;
656
657         task_context->remote_node_index = sci_port->reserved_rni;
658         task_context->command_code = 0;
659
660         task_context->link_layer_control = 0;
661         task_context->do_not_dma_ssp_good_response = 1;
662         task_context->strict_ordering = 0;
663         task_context->control_frame = 0;
664         task_context->timeout_enable = 0;
665         task_context->block_guard_enable = 0;
666
667         task_context->address_modifier = 0;
668
669         task_context->task_phase = 0x01;
670 }
671
672 static void scic_sds_port_destroy_dummy_resources(struct scic_sds_port *sci_port)
673 {
674         struct scic_sds_controller *scic = sci_port->owning_controller;
675
676         if (sci_port->reserved_tci != SCU_DUMMY_INDEX)
677                 scic_controller_free_io_tag(scic, sci_port->reserved_tci);
678
679         if (sci_port->reserved_rni != SCU_DUMMY_INDEX)
680                 scic_sds_remote_node_table_release_remote_node_index(&scic->available_remote_nodes,
681                                                                      1, sci_port->reserved_rni);
682
683         sci_port->reserved_rni = SCU_DUMMY_INDEX;
684         sci_port->reserved_tci = SCU_DUMMY_INDEX;
685 }
686
687 /**
688  * This method performs initialization of the supplied port. Initialization
689  *    includes: - state machine initialization - member variable initialization
690  *    - configuring the phy_mask
691  * @sci_port:
692  * @transport_layer_registers:
693  * @port_task_scheduler_registers:
694  * @port_configuration_regsiter:
695  *
696  * enum sci_status SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION This value is returned
697  * if the phy being added to the port
698  */
699 enum sci_status scic_sds_port_initialize(
700         struct scic_sds_port *sci_port,
701         void __iomem *port_task_scheduler_registers,
702         void __iomem *port_configuration_regsiter,
703         void __iomem *viit_registers)
704 {
705         sci_port->port_task_scheduler_registers  = port_task_scheduler_registers;
706         sci_port->port_pe_configuration_register = port_configuration_regsiter;
707         sci_port->viit_registers                 = viit_registers;
708
709         return SCI_SUCCESS;
710 }
711
712
713 /**
714  * This method assigns the direct attached device ID for this port.
715  *
716  * @param[in] sci_port The port for which the direct attached device id is to
717  *       be assigned.
718  * @param[in] device_id The direct attached device ID to assign to the port.
719  *       This will be the RNi for the device
720  */
721 void scic_sds_port_setup_transports(
722         struct scic_sds_port *sci_port,
723         u32 device_id)
724 {
725         u8 index;
726
727         for (index = 0; index < SCI_MAX_PHYS; index++) {
728                 if (sci_port->active_phy_mask & (1 << index))
729                         scic_sds_phy_setup_transport(sci_port->phy_table[index], device_id);
730         }
731 }
732
733 /**
734  *
735  * @sci_port: This is the port on which the phy should be enabled.
736  * @sci_phy: This is the specific phy which to enable.
737  * @do_notify_user: This parameter specifies whether to inform the user (via
738  *    scic_cb_port_link_up()) as to the fact that a new phy as become ready.
739  *
740  * This function will activate the phy in the port.
741  * Activation includes: - adding
742  * the phy to the port - enabling the Protocol Engine in the silicon. -
743  * notifying the user that the link is up. none
744  */
745 static void scic_sds_port_activate_phy(struct scic_sds_port *sci_port,
746                                        struct scic_sds_phy *sci_phy,
747                                        bool do_notify_user)
748 {
749         struct scic_sds_controller *scic = sci_port->owning_controller;
750         struct isci_host *ihost = scic_to_ihost(scic);
751
752         if (sci_phy->protocol != SCIC_SDS_PHY_PROTOCOL_SATA)
753                 scic_sds_phy_resume(sci_phy);
754
755         sci_port->active_phy_mask |= 1 << sci_phy->phy_index;
756
757         scic_sds_controller_clear_invalid_phy(scic, sci_phy);
758
759         if (do_notify_user == true)
760                 isci_port_link_up(ihost, sci_port, sci_phy);
761 }
762
763 void scic_sds_port_deactivate_phy(struct scic_sds_port *sci_port,
764                                   struct scic_sds_phy *sci_phy,
765                                   bool do_notify_user)
766 {
767         struct scic_sds_controller *scic = scic_sds_port_get_controller(sci_port);
768         struct isci_port *iport = sci_port_to_iport(sci_port);
769         struct isci_host *ihost = scic_to_ihost(scic);
770         struct isci_phy *iphy = sci_phy_to_iphy(sci_phy);
771
772         sci_port->active_phy_mask &= ~(1 << sci_phy->phy_index);
773
774         sci_phy->max_negotiated_speed = SAS_LINK_RATE_UNKNOWN;
775
776         /* Re-assign the phy back to the LP as if it were a narrow port */
777         writel(sci_phy->phy_index,
778                 &sci_port->port_pe_configuration_register[sci_phy->phy_index]);
779
780         if (do_notify_user == true)
781                 isci_port_link_down(ihost, iphy, iport);
782 }
783
784 /**
785  *
786  * @sci_port: This is the port on which the phy should be disabled.
787  * @sci_phy: This is the specific phy which to disabled.
788  *
789  * This function will disable the phy and report that the phy is not valid for
790  * this port object. None
791  */
792 static void scic_sds_port_invalid_link_up(struct scic_sds_port *sci_port,
793                                           struct scic_sds_phy *sci_phy)
794 {
795         struct scic_sds_controller *scic = sci_port->owning_controller;
796
797         /*
798          * Check to see if we have alreay reported this link as bad and if
799          * not go ahead and tell the SCI_USER that we have discovered an
800          * invalid link.
801          */
802         if ((scic->invalid_phy_mask & (1 << sci_phy->phy_index)) == 0) {
803                 scic_sds_controller_set_invalid_phy(scic, sci_phy);
804                 dev_warn(&scic_to_ihost(scic)->pdev->dev, "Invalid link up!\n");
805         }
806 }
807
808 static bool is_port_ready_state(enum scic_sds_port_states state)
809 {
810         switch (state) {
811         case SCI_BASE_PORT_STATE_READY:
812         case SCIC_SDS_PORT_READY_SUBSTATE_WAITING:
813         case SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL:
814         case SCIC_SDS_PORT_READY_SUBSTATE_CONFIGURING:
815                 return true;
816         default:
817                 return false;
818         }
819 }
820
821 /* flag dummy rnc hanling when exiting a ready state */
822 static void port_state_machine_change(struct scic_sds_port *sci_port,
823                                       enum scic_sds_port_states state)
824 {
825         struct sci_base_state_machine *sm = &sci_port->state_machine;
826         enum scic_sds_port_states old_state = sm->current_state_id;
827
828         if (is_port_ready_state(old_state) && !is_port_ready_state(state))
829                 sci_port->ready_exit = true;
830
831         sci_base_state_machine_change_state(sm, state);
832         sci_port->ready_exit = false;
833 }
834
835 /**
836  * scic_sds_port_general_link_up_handler - phy can be assigned to port?
837  * @sci_port: scic_sds_port object for which has a phy that has gone link up.
838  * @sci_phy: This is the struct scic_sds_phy object that has gone link up.
839  * @do_notify_user: This parameter specifies whether to inform the user (via
840  *    scic_cb_port_link_up()) as to the fact that a new phy as become ready.
841  *
842  * Determine if this phy can be assigned to this
843  * port . If the phy is not a valid PHY for
844  * this port then the function will notify the user. A PHY can only be
845  * part of a port if it's attached SAS ADDRESS is the same as all other PHYs in
846  * the same port. none
847  */
848 static void scic_sds_port_general_link_up_handler(struct scic_sds_port *sci_port,
849                                                   struct scic_sds_phy *sci_phy,
850                                                   bool do_notify_user)
851 {
852         struct sci_sas_address port_sas_address;
853         struct sci_sas_address phy_sas_address;
854
855         scic_sds_port_get_attached_sas_address(sci_port, &port_sas_address);
856         scic_sds_phy_get_attached_sas_address(sci_phy, &phy_sas_address);
857
858         /* If the SAS address of the new phy matches the SAS address of
859          * other phys in the port OR this is the first phy in the port,
860          * then activate the phy and allow it to be used for operations
861          * in this port.
862          */
863         if ((phy_sas_address.high == port_sas_address.high &&
864              phy_sas_address.low  == port_sas_address.low) ||
865             sci_port->active_phy_mask == 0) {
866                 struct sci_base_state_machine *sm = &sci_port->state_machine;
867
868                 scic_sds_port_activate_phy(sci_port, sci_phy, do_notify_user);
869                 if (sm->current_state_id == SCI_BASE_PORT_STATE_RESETTING)
870                         port_state_machine_change(sci_port, SCI_BASE_PORT_STATE_READY);
871         } else
872                 scic_sds_port_invalid_link_up(sci_port, sci_phy);
873 }
874
875
876
877 /**
878  * This method returns false if the port only has a single phy object assigned.
879  *     If there are no phys or more than one phy then the method will return
880  *    true.
881  * @sci_port: The port for which the wide port condition is to be checked.
882  *
883  * bool true Is returned if this is a wide ported port. false Is returned if
884  * this is a narrow port.
885  */
886 static bool scic_sds_port_is_wide(struct scic_sds_port *sci_port)
887 {
888         u32 index;
889         u32 phy_count = 0;
890
891         for (index = 0; index < SCI_MAX_PHYS; index++) {
892                 if (sci_port->phy_table[index] != NULL) {
893                         phy_count++;
894                 }
895         }
896
897         return phy_count != 1;
898 }
899
900 /**
901  * This method is called by the PHY object when the link is detected. if the
902  *    port wants the PHY to continue on to the link up state then the port
903  *    layer must return true.  If the port object returns false the phy object
904  *    must halt its attempt to go link up.
905  * @sci_port: The port associated with the phy object.
906  * @sci_phy: The phy object that is trying to go link up.
907  *
908  * true if the phy object can continue to the link up condition. true Is
909  * returned if this phy can continue to the ready state. false Is returned if
910  * can not continue on to the ready state. This notification is in place for
911  * wide ports and direct attached phys.  Since there are no wide ported SATA
912  * devices this could become an invalid port configuration.
913  */
914 bool scic_sds_port_link_detected(
915         struct scic_sds_port *sci_port,
916         struct scic_sds_phy *sci_phy)
917 {
918         if ((sci_port->logical_port_index != SCIC_SDS_DUMMY_PORT) &&
919             (sci_phy->protocol == SCIC_SDS_PHY_PROTOCOL_SATA) &&
920             scic_sds_port_is_wide(sci_port)) {
921                 scic_sds_port_invalid_link_up(sci_port, sci_phy);
922
923                 return false;
924         }
925
926         return true;
927 }
928
929 static void port_timeout(unsigned long data)
930 {
931         struct sci_timer *tmr = (struct sci_timer *)data;
932         struct scic_sds_port *sci_port = container_of(tmr, typeof(*sci_port), timer);
933         struct isci_host *ihost = scic_to_ihost(sci_port->owning_controller);
934         unsigned long flags;
935         u32 current_state;
936
937         spin_lock_irqsave(&ihost->scic_lock, flags);
938
939         if (tmr->cancel)
940                 goto done;
941
942         current_state = sci_base_state_machine_get_state(&sci_port->state_machine);
943
944         if (current_state == SCI_BASE_PORT_STATE_RESETTING) {
945                 /* if the port is still in the resetting state then the timeout
946                  * fired before the reset completed.
947                  */
948                 port_state_machine_change(sci_port, SCI_BASE_PORT_STATE_FAILED);
949         } else if (current_state == SCI_BASE_PORT_STATE_STOPPED) {
950                 /* if the port is stopped then the start request failed In this
951                  * case stay in the stopped state.
952                  */
953                 dev_err(sciport_to_dev(sci_port),
954                         "%s: SCIC Port 0x%p failed to stop before tiemout.\n",
955                         __func__,
956                         sci_port);
957         } else if (current_state == SCI_BASE_PORT_STATE_STOPPING) {
958                 /* if the port is still stopping then the stop has not completed */
959                 isci_port_stop_complete(sci_port->owning_controller,
960                                         sci_port,
961                                         SCI_FAILURE_TIMEOUT);
962         } else {
963                 /* The port is in the ready state and we have a timer
964                  * reporting a timeout this should not happen.
965                  */
966                 dev_err(sciport_to_dev(sci_port),
967                         "%s: SCIC Port 0x%p is processing a timeout operation "
968                         "in state %d.\n", __func__, sci_port, current_state);
969         }
970
971 done:
972         spin_unlock_irqrestore(&ihost->scic_lock, flags);
973 }
974
975 /* --------------------------------------------------------------------------- */
976
977 /**
978  * This function updates the hardwares VIIT entry for this port.
979  *
980  *
981  */
982 static void scic_sds_port_update_viit_entry(struct scic_sds_port *sci_port)
983 {
984         struct sci_sas_address sas_address;
985
986         scic_sds_port_get_sas_address(sci_port, &sas_address);
987
988         writel(sas_address.high,
989                 &sci_port->viit_registers->initiator_sas_address_hi);
990         writel(sas_address.low,
991                 &sci_port->viit_registers->initiator_sas_address_lo);
992
993         /* This value get cleared just in case its not already cleared */
994         writel(0, &sci_port->viit_registers->reserved);
995
996         /* We are required to update the status register last */
997         writel(SCU_VIIT_ENTRY_ID_VIIT |
998                SCU_VIIT_IPPT_INITIATOR |
999                ((1 << sci_port->physical_port_index) << SCU_VIIT_ENTRY_LPVIE_SHIFT) |
1000                SCU_VIIT_STATUS_ALL_VALID,
1001                &sci_port->viit_registers->status);
1002 }
1003
1004 /**
1005  * This method returns the maximum allowed speed for data transfers on this
1006  *    port.  This maximum allowed speed evaluates to the maximum speed of the
1007  *    slowest phy in the port.
1008  * @sci_port: This parameter specifies the port for which to retrieve the
1009  *    maximum allowed speed.
1010  *
1011  * This method returns the maximum negotiated speed of the slowest phy in the
1012  * port.
1013  */
1014 enum sas_linkrate scic_sds_port_get_max_allowed_speed(
1015         struct scic_sds_port *sci_port)
1016 {
1017         u16 index;
1018         enum sas_linkrate max_allowed_speed = SAS_LINK_RATE_6_0_GBPS;
1019         struct scic_sds_phy *phy = NULL;
1020
1021         /*
1022          * Loop through all of the phys in this port and find the phy with the
1023          * lowest maximum link rate. */
1024         for (index = 0; index < SCI_MAX_PHYS; index++) {
1025                 phy = sci_port->phy_table[index];
1026                 if (
1027                         (phy != NULL)
1028                         && (scic_sds_port_active_phy(sci_port, phy) == true)
1029                         && (phy->max_negotiated_speed < max_allowed_speed)
1030                         )
1031                         max_allowed_speed = phy->max_negotiated_speed;
1032         }
1033
1034         return max_allowed_speed;
1035 }
1036
1037 static void scic_port_enable_broadcast_change_notification(struct scic_sds_port *port)
1038 {
1039         struct scic_sds_phy *phy;
1040         u32 register_value;
1041         u8 index;
1042
1043         /* Loop through all of the phys to enable BCN. */
1044         for (index = 0; index < SCI_MAX_PHYS; index++) {
1045                 phy = port->phy_table[index];
1046                 if (phy != NULL) {
1047                         register_value =
1048                                 readl(&phy->link_layer_registers->link_layer_control);
1049
1050                         /* clear the bit by writing 1. */
1051                         writel(register_value,
1052                                 &phy->link_layer_registers->link_layer_control);
1053                 }
1054         }
1055 }
1056
1057 /**
1058  *
1059  * @sci_port: This is the struct scic_sds_port object to suspend.
1060  *
1061  * This method will susped the port task scheduler for this port object. none
1062  */
1063 static void
1064 scic_sds_port_suspend_port_task_scheduler(struct scic_sds_port *port)
1065 {
1066         u32 pts_control_value;
1067
1068         pts_control_value = readl(&port->port_task_scheduler_registers->control);
1069         pts_control_value |= SCU_PTSxCR_GEN_BIT(SUSPEND);
1070         writel(pts_control_value, &port->port_task_scheduler_registers->control);
1071 }
1072
1073 /**
1074  * scic_sds_port_post_dummy_request() - post dummy/workaround request
1075  * @sci_port: port to post task
1076  *
1077  * Prevent the hardware scheduler from posting new requests to the front
1078  * of the scheduler queue causing a starvation problem for currently
1079  * ongoing requests.
1080  *
1081  */
1082 static void scic_sds_port_post_dummy_request(struct scic_sds_port *sci_port)
1083 {
1084         u32 command;
1085         struct scu_task_context *task_context;
1086         struct scic_sds_controller *scic = sci_port->owning_controller;
1087         u16 tci = sci_port->reserved_tci;
1088
1089         task_context = scic_sds_controller_get_task_context_buffer(scic, tci);
1090
1091         task_context->abort = 0;
1092
1093         command = SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
1094                   sci_port->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT |
1095                   tci;
1096
1097         scic_sds_controller_post_request(scic, command);
1098 }
1099
1100 /**
1101  * This routine will abort the dummy request.  This will alow the hardware to
1102  * power down parts of the silicon to save power.
1103  *
1104  * @sci_port: The port on which the task must be aborted.
1105  *
1106  */
1107 static void scic_sds_port_abort_dummy_request(struct scic_sds_port *sci_port)
1108 {
1109         struct scic_sds_controller *scic = sci_port->owning_controller;
1110         u16 tci = sci_port->reserved_tci;
1111         struct scu_task_context *tc;
1112         u32 command;
1113
1114         tc = scic_sds_controller_get_task_context_buffer(scic, tci);
1115
1116         tc->abort = 1;
1117
1118         command = SCU_CONTEXT_COMMAND_REQUEST_POST_TC_ABORT |
1119                   sci_port->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT |
1120                   tci;
1121
1122         scic_sds_controller_post_request(scic, command);
1123 }
1124
1125 /**
1126  *
1127  * @sci_port: This is the struct scic_sds_port object to resume.
1128  *
1129  * This method will resume the port task scheduler for this port object. none
1130  */
1131 static void
1132 scic_sds_port_resume_port_task_scheduler(struct scic_sds_port *port)
1133 {
1134         u32 pts_control_value;
1135
1136         pts_control_value = readl(&port->port_task_scheduler_registers->control);
1137         pts_control_value &= ~SCU_PTSxCR_GEN_BIT(SUSPEND);
1138         writel(pts_control_value, &port->port_task_scheduler_registers->control);
1139 }
1140
1141 static void scic_sds_port_ready_substate_waiting_enter(struct sci_base_state_machine *sm)
1142 {
1143         struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), state_machine);
1144
1145         scic_sds_port_suspend_port_task_scheduler(sci_port);
1146
1147         sci_port->not_ready_reason = SCIC_PORT_NOT_READY_NO_ACTIVE_PHYS;
1148
1149         if (sci_port->active_phy_mask != 0) {
1150                 /* At least one of the phys on the port is ready */
1151                 port_state_machine_change(sci_port,
1152                                           SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL);
1153         }
1154 }
1155
1156 static void scic_sds_port_ready_substate_operational_enter(struct sci_base_state_machine *sm)
1157 {
1158         u32 index;
1159         struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), state_machine);
1160         struct scic_sds_controller *scic = sci_port->owning_controller;
1161         struct isci_host *ihost = scic_to_ihost(scic);
1162         struct isci_port *iport = sci_port_to_iport(sci_port);
1163
1164         isci_port_ready(ihost, iport);
1165
1166         for (index = 0; index < SCI_MAX_PHYS; index++) {
1167                 if (sci_port->phy_table[index]) {
1168                         writel(sci_port->physical_port_index,
1169                                 &sci_port->port_pe_configuration_register[
1170                                         sci_port->phy_table[index]->phy_index]);
1171                 }
1172         }
1173
1174         scic_sds_port_update_viit_entry(sci_port);
1175
1176         scic_sds_port_resume_port_task_scheduler(sci_port);
1177
1178         /*
1179          * Post the dummy task for the port so the hardware can schedule
1180          * io correctly
1181          */
1182         scic_sds_port_post_dummy_request(sci_port);
1183 }
1184
1185 static void scic_sds_port_invalidate_dummy_remote_node(struct scic_sds_port *sci_port)
1186 {
1187         struct scic_sds_controller *scic = sci_port->owning_controller;
1188         u8 phys_index = sci_port->physical_port_index;
1189         union scu_remote_node_context *rnc;
1190         u16 rni = sci_port->reserved_rni;
1191         u32 command;
1192
1193         rnc = &scic->remote_node_context_table[rni];
1194
1195         rnc->ssp.is_valid = false;
1196
1197         /* ensure the preceding tc abort request has reached the
1198          * controller and give it ample time to act before posting the rnc
1199          * invalidate
1200          */
1201         readl(&scic->smu_registers->interrupt_status); /* flush */
1202         udelay(10);
1203
1204         command = SCU_CONTEXT_COMMAND_POST_RNC_INVALIDATE |
1205                   phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
1206
1207         scic_sds_controller_post_request(scic, command);
1208 }
1209
1210 /**
1211  *
1212  * @object: This is the object which is cast to a struct scic_sds_port object.
1213  *
1214  * This method will perform the actions required by the struct scic_sds_port on
1215  * exiting the SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL. This function reports
1216  * the port not ready and suspends the port task scheduler. none
1217  */
1218 static void scic_sds_port_ready_substate_operational_exit(struct sci_base_state_machine *sm)
1219 {
1220         struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), state_machine);
1221         struct scic_sds_controller *scic = sci_port->owning_controller;
1222         struct isci_host *ihost = scic_to_ihost(scic);
1223         struct isci_port *iport = sci_port_to_iport(sci_port);
1224
1225         /*
1226          * Kill the dummy task for this port if it has not yet posted
1227          * the hardware will treat this as a NOP and just return abort
1228          * complete.
1229          */
1230         scic_sds_port_abort_dummy_request(sci_port);
1231
1232         isci_port_not_ready(ihost, iport);
1233
1234         if (sci_port->ready_exit)
1235                 scic_sds_port_invalidate_dummy_remote_node(sci_port);
1236 }
1237
1238 static void scic_sds_port_ready_substate_configuring_enter(struct sci_base_state_machine *sm)
1239 {
1240         struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), state_machine);
1241         struct scic_sds_controller *scic = sci_port->owning_controller;
1242         struct isci_host *ihost = scic_to_ihost(scic);
1243         struct isci_port *iport = sci_port_to_iport(sci_port);
1244
1245         if (sci_port->active_phy_mask == 0) {
1246                 isci_port_not_ready(ihost, iport);
1247
1248                 port_state_machine_change(sci_port,
1249                                           SCIC_SDS_PORT_READY_SUBSTATE_WAITING);
1250         } else if (sci_port->started_request_count == 0)
1251                 port_state_machine_change(sci_port,
1252                                           SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL);
1253 }
1254
1255 static void scic_sds_port_ready_substate_configuring_exit(struct sci_base_state_machine *sm)
1256 {
1257         struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), state_machine);
1258
1259         scic_sds_port_suspend_port_task_scheduler(sci_port);
1260         if (sci_port->ready_exit)
1261                 scic_sds_port_invalidate_dummy_remote_node(sci_port);
1262 }
1263
1264 enum sci_status scic_sds_port_start(struct scic_sds_port *sci_port)
1265 {
1266         struct scic_sds_controller *scic = sci_port->owning_controller;
1267         enum sci_status status = SCI_SUCCESS;
1268         enum scic_sds_port_states state;
1269         u32 phy_mask;
1270
1271         state = sci_port->state_machine.current_state_id;
1272         if (state != SCI_BASE_PORT_STATE_STOPPED) {
1273                 dev_warn(sciport_to_dev(sci_port),
1274                          "%s: in wrong state: %d\n", __func__, state);
1275                 return SCI_FAILURE_INVALID_STATE;
1276         }
1277
1278         if (sci_port->assigned_device_count > 0) {
1279                 /* TODO This is a start failure operation because
1280                  * there are still devices assigned to this port.
1281                  * There must be no devices assigned to a port on a
1282                  * start operation.
1283                  */
1284                 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
1285         }
1286
1287         if (sci_port->reserved_rni == SCU_DUMMY_INDEX) {
1288                 u16 rni = scic_sds_remote_node_table_allocate_remote_node(
1289                                 &scic->available_remote_nodes, 1);
1290
1291                 if (rni != SCU_DUMMY_INDEX)
1292                         scic_sds_port_construct_dummy_rnc(sci_port, rni);
1293                 else
1294                         status = SCI_FAILURE_INSUFFICIENT_RESOURCES;
1295                 sci_port->reserved_rni = rni;
1296         }
1297
1298         if (sci_port->reserved_tci == SCU_DUMMY_INDEX) {
1299                 /* Allocate a TCI and remove the sequence nibble */
1300                 u16 tci = scic_controller_allocate_io_tag(scic);
1301
1302                 if (tci != SCU_DUMMY_INDEX)
1303                         scic_sds_port_construct_dummy_task(sci_port, tci);
1304                 else
1305                         status = SCI_FAILURE_INSUFFICIENT_RESOURCES;
1306                 sci_port->reserved_tci = tci;
1307         }
1308
1309         if (status == SCI_SUCCESS) {
1310                 phy_mask = scic_sds_port_get_phys(sci_port);
1311
1312                 /*
1313                  * There are one or more phys assigned to this port.  Make sure
1314                  * the port's phy mask is in fact legal and supported by the
1315                  * silicon.
1316                  */
1317                 if (scic_sds_port_is_phy_mask_valid(sci_port, phy_mask) == true) {
1318                         port_state_machine_change(sci_port,
1319                                                   SCI_BASE_PORT_STATE_READY);
1320
1321                         return SCI_SUCCESS;
1322                 }
1323                 status = SCI_FAILURE;
1324         }
1325
1326         if (status != SCI_SUCCESS)
1327                 scic_sds_port_destroy_dummy_resources(sci_port);
1328
1329         return status;
1330 }
1331
1332 enum sci_status scic_sds_port_stop(struct scic_sds_port *sci_port)
1333 {
1334         enum scic_sds_port_states state;
1335
1336         state = sci_port->state_machine.current_state_id;
1337         switch (state) {
1338         case SCI_BASE_PORT_STATE_STOPPED:
1339                 return SCI_SUCCESS;
1340         case SCIC_SDS_PORT_READY_SUBSTATE_WAITING:
1341         case SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL:
1342         case SCIC_SDS_PORT_READY_SUBSTATE_CONFIGURING:
1343         case SCI_BASE_PORT_STATE_RESETTING:
1344                 port_state_machine_change(sci_port,
1345                                           SCI_BASE_PORT_STATE_STOPPING);
1346                 return SCI_SUCCESS;
1347         default:
1348                 dev_warn(sciport_to_dev(sci_port),
1349                          "%s: in wrong state: %d\n", __func__, state);
1350                 return SCI_FAILURE_INVALID_STATE;
1351         }
1352 }
1353
1354 static enum sci_status scic_port_hard_reset(struct scic_sds_port *sci_port, u32 timeout)
1355 {
1356         enum sci_status status = SCI_FAILURE_INVALID_PHY;
1357         struct scic_sds_phy *selected_phy = NULL;
1358         enum scic_sds_port_states state;
1359         u32 phy_index;
1360
1361         state = sci_port->state_machine.current_state_id;
1362         if (state != SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL) {
1363                 dev_warn(sciport_to_dev(sci_port),
1364                          "%s: in wrong state: %d\n", __func__, state);
1365                 return SCI_FAILURE_INVALID_STATE;
1366         }
1367
1368         /* Select a phy on which we can send the hard reset request. */
1369         for (phy_index = 0; phy_index < SCI_MAX_PHYS && !selected_phy; phy_index++) {
1370                 selected_phy = sci_port->phy_table[phy_index];
1371                 if (selected_phy &&
1372                     !scic_sds_port_active_phy(sci_port, selected_phy)) {
1373                         /*
1374                          * We found a phy but it is not ready select
1375                          * different phy
1376                          */
1377                         selected_phy = NULL;
1378                 }
1379         }
1380
1381         /* If we have a phy then go ahead and start the reset procedure */
1382         if (!selected_phy)
1383                 return status;
1384         status = scic_sds_phy_reset(selected_phy);
1385
1386         if (status != SCI_SUCCESS)
1387                 return status;
1388
1389         sci_mod_timer(&sci_port->timer, timeout);
1390         sci_port->not_ready_reason = SCIC_PORT_NOT_READY_HARD_RESET_REQUESTED;
1391
1392         port_state_machine_change(sci_port,
1393                                   SCI_BASE_PORT_STATE_RESETTING);
1394         return SCI_SUCCESS;
1395 }
1396
1397 /**
1398  * scic_sds_port_add_phy() -
1399  * @sci_port: This parameter specifies the port in which the phy will be added.
1400  * @sci_phy: This parameter is the phy which is to be added to the port.
1401  *
1402  * This method will add a PHY to the selected port. This method returns an
1403  * enum sci_status. SCI_SUCCESS the phy has been added to the port. Any other
1404  * status is a failure to add the phy to the port.
1405  */
1406 enum sci_status scic_sds_port_add_phy(struct scic_sds_port *sci_port,
1407                                       struct scic_sds_phy *sci_phy)
1408 {
1409         enum sci_status status;
1410         enum scic_sds_port_states state;
1411
1412         state = sci_port->state_machine.current_state_id;
1413         switch (state) {
1414         case SCI_BASE_PORT_STATE_STOPPED: {
1415                 struct sci_sas_address port_sas_address;
1416
1417                 /* Read the port assigned SAS Address if there is one */
1418                 scic_sds_port_get_sas_address(sci_port, &port_sas_address);
1419
1420                 if (port_sas_address.high != 0 && port_sas_address.low != 0) {
1421                         struct sci_sas_address phy_sas_address;
1422
1423                         /* Make sure that the PHY SAS Address matches the SAS Address
1424                          * for this port
1425                          */
1426                         scic_sds_phy_get_sas_address(sci_phy, &phy_sas_address);
1427
1428                         if (port_sas_address.high != phy_sas_address.high ||
1429                             port_sas_address.low  != phy_sas_address.low)
1430                                 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
1431                 }
1432                 return scic_sds_port_set_phy(sci_port, sci_phy);
1433         }
1434         case SCIC_SDS_PORT_READY_SUBSTATE_WAITING:
1435         case SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL:
1436                 status = scic_sds_port_set_phy(sci_port, sci_phy);
1437
1438                 if (status != SCI_SUCCESS)
1439                         return status;
1440
1441                 scic_sds_port_general_link_up_handler(sci_port, sci_phy, true);
1442                 sci_port->not_ready_reason = SCIC_PORT_NOT_READY_RECONFIGURING;
1443                 port_state_machine_change(sci_port, SCIC_SDS_PORT_READY_SUBSTATE_CONFIGURING);
1444
1445                 return status;
1446         case SCIC_SDS_PORT_READY_SUBSTATE_CONFIGURING:
1447                 status = scic_sds_port_set_phy(sci_port, sci_phy);
1448
1449                 if (status != SCI_SUCCESS)
1450                         return status;
1451                 scic_sds_port_general_link_up_handler(sci_port, sci_phy, true);
1452
1453                 /* Re-enter the configuring state since this may be the last phy in
1454                  * the port.
1455                  */
1456                 port_state_machine_change(sci_port,
1457                                           SCIC_SDS_PORT_READY_SUBSTATE_CONFIGURING);
1458                 return SCI_SUCCESS;
1459         default:
1460                 dev_warn(sciport_to_dev(sci_port),
1461                          "%s: in wrong state: %d\n", __func__, state);
1462                 return SCI_FAILURE_INVALID_STATE;
1463         }
1464 }
1465
1466 /**
1467  * scic_sds_port_remove_phy() -
1468  * @sci_port: This parameter specifies the port in which the phy will be added.
1469  * @sci_phy: This parameter is the phy which is to be added to the port.
1470  *
1471  * This method will remove the PHY from the selected PORT. This method returns
1472  * an enum sci_status. SCI_SUCCESS the phy has been removed from the port. Any
1473  * other status is a failure to add the phy to the port.
1474  */
1475 enum sci_status scic_sds_port_remove_phy(struct scic_sds_port *sci_port,
1476                                          struct scic_sds_phy *sci_phy)
1477 {
1478         enum sci_status status;
1479         enum scic_sds_port_states state;
1480
1481         state = sci_port->state_machine.current_state_id;
1482
1483         switch (state) {
1484         case SCI_BASE_PORT_STATE_STOPPED:
1485                 return scic_sds_port_clear_phy(sci_port, sci_phy);
1486         case SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL:
1487                 status = scic_sds_port_clear_phy(sci_port, sci_phy);
1488                 if (status != SCI_SUCCESS)
1489                         return status;
1490
1491                 scic_sds_port_deactivate_phy(sci_port, sci_phy, true);
1492                 sci_port->not_ready_reason = SCIC_PORT_NOT_READY_RECONFIGURING;
1493                 port_state_machine_change(sci_port,
1494                                           SCIC_SDS_PORT_READY_SUBSTATE_CONFIGURING);
1495                 return SCI_SUCCESS;
1496         case SCIC_SDS_PORT_READY_SUBSTATE_CONFIGURING:
1497                 status = scic_sds_port_clear_phy(sci_port, sci_phy);
1498
1499                 if (status != SCI_SUCCESS)
1500                         return status;
1501                 scic_sds_port_deactivate_phy(sci_port, sci_phy, true);
1502
1503                 /* Re-enter the configuring state since this may be the last phy in
1504                  * the port
1505                  */
1506                 port_state_machine_change(sci_port,
1507                                           SCIC_SDS_PORT_READY_SUBSTATE_CONFIGURING);
1508                 return SCI_SUCCESS;
1509         default:
1510                 dev_warn(sciport_to_dev(sci_port),
1511                          "%s: in wrong state: %d\n", __func__, state);
1512                 return SCI_FAILURE_INVALID_STATE;
1513         }
1514 }
1515
1516 enum sci_status scic_sds_port_link_up(struct scic_sds_port *sci_port,
1517                                       struct scic_sds_phy *sci_phy)
1518 {
1519         enum scic_sds_port_states state;
1520
1521         state = sci_port->state_machine.current_state_id;
1522         switch (state) {
1523         case SCIC_SDS_PORT_READY_SUBSTATE_WAITING:
1524                 /* Since this is the first phy going link up for the port we
1525                  * can just enable it and continue
1526                  */
1527                 scic_sds_port_activate_phy(sci_port, sci_phy, true);
1528
1529                 port_state_machine_change(sci_port,
1530                                           SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL);
1531                 return SCI_SUCCESS;
1532         case SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL:
1533                 scic_sds_port_general_link_up_handler(sci_port, sci_phy, true);
1534                 return SCI_SUCCESS;
1535         case SCI_BASE_PORT_STATE_RESETTING:
1536                 /* TODO We should  make  sure  that  the phy  that  has gone
1537                  * link up is the same one on which we sent the reset.  It is
1538                  * possible that the phy on which we sent  the reset is not the
1539                  * one that has  gone  link up  and we  want to make sure that
1540                  * phy being reset  comes  back.  Consider the case where a
1541                  * reset is sent but before the hardware processes the reset it
1542                  * get a link up on  the  port because of a hot plug event.
1543                  * because  of  the reset request this phy will go link down
1544                  * almost immediately.
1545                  */
1546
1547                 /* In the resetting state we don't notify the user regarding
1548                  * link up and link down notifications.
1549                  */
1550                 scic_sds_port_general_link_up_handler(sci_port, sci_phy, false);
1551                 return SCI_SUCCESS;
1552         default:
1553                 dev_warn(sciport_to_dev(sci_port),
1554                          "%s: in wrong state: %d\n", __func__, state);
1555                 return SCI_FAILURE_INVALID_STATE;
1556         }
1557 }
1558
1559 enum sci_status scic_sds_port_link_down(struct scic_sds_port *sci_port,
1560                                         struct scic_sds_phy *sci_phy)
1561 {
1562         enum scic_sds_port_states state;
1563
1564         state = sci_port->state_machine.current_state_id;
1565         switch (state) {
1566         case SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL:
1567                 scic_sds_port_deactivate_phy(sci_port, sci_phy, true);
1568
1569                 /* If there are no active phys left in the port, then
1570                  * transition the port to the WAITING state until such time
1571                  * as a phy goes link up
1572                  */
1573                 if (sci_port->active_phy_mask == 0)
1574                         port_state_machine_change(sci_port,
1575                                                   SCIC_SDS_PORT_READY_SUBSTATE_WAITING);
1576                 return SCI_SUCCESS;
1577         case SCI_BASE_PORT_STATE_RESETTING:
1578                 /* In the resetting state we don't notify the user regarding
1579                  * link up and link down notifications. */
1580                 scic_sds_port_deactivate_phy(sci_port, sci_phy, false);
1581                 return SCI_SUCCESS;
1582         default:
1583                 dev_warn(sciport_to_dev(sci_port),
1584                          "%s: in wrong state: %d\n", __func__, state);
1585                 return SCI_FAILURE_INVALID_STATE;
1586         }
1587 }
1588
1589 enum sci_status scic_sds_port_start_io(struct scic_sds_port *sci_port,
1590                                        struct scic_sds_remote_device *sci_dev,
1591                                        struct scic_sds_request *sci_req)
1592 {
1593         enum scic_sds_port_states state;
1594
1595         state = sci_port->state_machine.current_state_id;
1596         switch (state) {
1597         case SCIC_SDS_PORT_READY_SUBSTATE_WAITING:
1598                 return SCI_FAILURE_INVALID_STATE;
1599         case SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL:
1600                 sci_port->started_request_count++;
1601                 return SCI_SUCCESS;
1602         default:
1603                 dev_warn(sciport_to_dev(sci_port),
1604                          "%s: in wrong state: %d\n", __func__, state);
1605                 return SCI_FAILURE_INVALID_STATE;
1606         }
1607 }
1608
1609 enum sci_status scic_sds_port_complete_io(struct scic_sds_port *sci_port,
1610                                           struct scic_sds_remote_device *sci_dev,
1611                                           struct scic_sds_request *sci_req)
1612 {
1613         enum scic_sds_port_states state;
1614
1615         state = sci_port->state_machine.current_state_id;
1616         switch (state) {
1617         case SCI_BASE_PORT_STATE_STOPPED:
1618                 dev_warn(sciport_to_dev(sci_port),
1619                          "%s: in wrong state: %d\n", __func__, state);
1620                 return SCI_FAILURE_INVALID_STATE;
1621         case SCI_BASE_PORT_STATE_STOPPING:
1622                 scic_sds_port_decrement_request_count(sci_port);
1623
1624                 if (sci_port->started_request_count == 0)
1625                         port_state_machine_change(sci_port,
1626                                                   SCI_BASE_PORT_STATE_STOPPED);
1627                 break;
1628         case SCI_BASE_PORT_STATE_READY:
1629         case SCI_BASE_PORT_STATE_RESETTING:
1630         case SCI_BASE_PORT_STATE_FAILED:
1631         case SCIC_SDS_PORT_READY_SUBSTATE_WAITING:
1632         case SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL:
1633                 scic_sds_port_decrement_request_count(sci_port);
1634                 break;
1635         case SCIC_SDS_PORT_READY_SUBSTATE_CONFIGURING:
1636                 scic_sds_port_decrement_request_count(sci_port);
1637                 if (sci_port->started_request_count == 0) {
1638                         port_state_machine_change(sci_port,
1639                                                   SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL);
1640                 }
1641                 break;
1642         }
1643         return SCI_SUCCESS;
1644 }
1645
1646 /**
1647  *
1648  * @sci_port: This is the port object which to suspend.
1649  *
1650  * This method will enable the SCU Port Task Scheduler for this port object but
1651  * will leave the port task scheduler in a suspended state. none
1652  */
1653 static void
1654 scic_sds_port_enable_port_task_scheduler(struct scic_sds_port *port)
1655 {
1656         u32 pts_control_value;
1657
1658         pts_control_value = readl(&port->port_task_scheduler_registers->control);
1659         pts_control_value |= SCU_PTSxCR_GEN_BIT(ENABLE) | SCU_PTSxCR_GEN_BIT(SUSPEND);
1660         writel(pts_control_value, &port->port_task_scheduler_registers->control);
1661 }
1662
1663 /**
1664  *
1665  * @sci_port: This is the port object which to resume.
1666  *
1667  * This method will disable the SCU port task scheduler for this port object.
1668  * none
1669  */
1670 static void
1671 scic_sds_port_disable_port_task_scheduler(struct scic_sds_port *port)
1672 {
1673         u32 pts_control_value;
1674
1675         pts_control_value = readl(&port->port_task_scheduler_registers->control);
1676         pts_control_value &=
1677                 ~(SCU_PTSxCR_GEN_BIT(ENABLE) | SCU_PTSxCR_GEN_BIT(SUSPEND));
1678         writel(pts_control_value, &port->port_task_scheduler_registers->control);
1679 }
1680
1681 static void scic_sds_port_post_dummy_remote_node(struct scic_sds_port *sci_port)
1682 {
1683         struct scic_sds_controller *scic = sci_port->owning_controller;
1684         u8 phys_index = sci_port->physical_port_index;
1685         union scu_remote_node_context *rnc;
1686         u16 rni = sci_port->reserved_rni;
1687         u32 command;
1688
1689         rnc = &scic->remote_node_context_table[rni];
1690         rnc->ssp.is_valid = true;
1691
1692         command = SCU_CONTEXT_COMMAND_POST_RNC_32 |
1693                   phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
1694
1695         scic_sds_controller_post_request(scic, command);
1696
1697         /* ensure hardware has seen the post rnc command and give it
1698          * ample time to act before sending the suspend
1699          */
1700         readl(&scic->smu_registers->interrupt_status); /* flush */
1701         udelay(10);
1702
1703         command = SCU_CONTEXT_COMMAND_POST_RNC_SUSPEND_TX_RX |
1704                   phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
1705
1706         scic_sds_controller_post_request(scic, command);
1707 }
1708
1709 static void scic_sds_port_stopped_state_enter(struct sci_base_state_machine *sm)
1710 {
1711         struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), state_machine);
1712
1713         if (sci_port->state_machine.previous_state_id == SCI_BASE_PORT_STATE_STOPPING) {
1714                 /*
1715                  * If we enter this state becasuse of a request to stop
1716                  * the port then we want to disable the hardwares port
1717                  * task scheduler. */
1718                 scic_sds_port_disable_port_task_scheduler(sci_port);
1719         }
1720 }
1721
1722 static void scic_sds_port_stopped_state_exit(struct sci_base_state_machine *sm)
1723 {
1724         struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), state_machine);
1725
1726         /* Enable and suspend the port task scheduler */
1727         scic_sds_port_enable_port_task_scheduler(sci_port);
1728 }
1729
1730 static void scic_sds_port_ready_state_enter(struct sci_base_state_machine *sm)
1731 {
1732         struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), state_machine);
1733         struct scic_sds_controller *scic = sci_port->owning_controller;
1734         struct isci_host *ihost = scic_to_ihost(scic);
1735         struct isci_port *iport = sci_port_to_iport(sci_port);
1736         u32 prev_state;
1737
1738         prev_state = sci_port->state_machine.previous_state_id;
1739         if (prev_state  == SCI_BASE_PORT_STATE_RESETTING)
1740                 isci_port_hard_reset_complete(iport, SCI_SUCCESS);
1741         else
1742                 isci_port_not_ready(ihost, iport);
1743
1744         /* Post and suspend the dummy remote node context for this port. */
1745         scic_sds_port_post_dummy_remote_node(sci_port);
1746
1747         /* Start the ready substate machine */
1748         port_state_machine_change(sci_port,
1749                                   SCIC_SDS_PORT_READY_SUBSTATE_WAITING);
1750 }
1751
1752 static void scic_sds_port_resetting_state_exit(struct sci_base_state_machine *sm)
1753 {
1754         struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), state_machine);
1755
1756         sci_del_timer(&sci_port->timer);
1757 }
1758
1759 static void scic_sds_port_stopping_state_exit(struct sci_base_state_machine *sm)
1760 {
1761         struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), state_machine);
1762
1763         sci_del_timer(&sci_port->timer);
1764
1765         scic_sds_port_destroy_dummy_resources(sci_port);
1766 }
1767
1768 static void scic_sds_port_failed_state_enter(struct sci_base_state_machine *sm)
1769 {
1770         struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), state_machine);
1771         struct isci_port *iport = sci_port_to_iport(sci_port);
1772
1773         isci_port_hard_reset_complete(iport, SCI_FAILURE_TIMEOUT);
1774 }
1775
1776 /* --------------------------------------------------------------------------- */
1777
1778 static const struct sci_base_state scic_sds_port_state_table[] = {
1779         [SCI_BASE_PORT_STATE_STOPPED] = {
1780                 .enter_state = scic_sds_port_stopped_state_enter,
1781                 .exit_state  = scic_sds_port_stopped_state_exit
1782         },
1783         [SCI_BASE_PORT_STATE_STOPPING] = {
1784                 .exit_state  = scic_sds_port_stopping_state_exit
1785         },
1786         [SCI_BASE_PORT_STATE_READY] = {
1787                 .enter_state = scic_sds_port_ready_state_enter,
1788         },
1789         [SCIC_SDS_PORT_READY_SUBSTATE_WAITING] = {
1790                 .enter_state = scic_sds_port_ready_substate_waiting_enter,
1791         },
1792         [SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL] = {
1793                 .enter_state = scic_sds_port_ready_substate_operational_enter,
1794                 .exit_state  = scic_sds_port_ready_substate_operational_exit
1795         },
1796         [SCIC_SDS_PORT_READY_SUBSTATE_CONFIGURING] = {
1797                 .enter_state = scic_sds_port_ready_substate_configuring_enter,
1798                 .exit_state  = scic_sds_port_ready_substate_configuring_exit
1799         },
1800         [SCI_BASE_PORT_STATE_RESETTING] = {
1801                 .exit_state  = scic_sds_port_resetting_state_exit
1802         },
1803         [SCI_BASE_PORT_STATE_FAILED] = {
1804                 .enter_state = scic_sds_port_failed_state_enter,
1805         }
1806 };
1807
1808 void scic_sds_port_construct(struct scic_sds_port *sci_port, u8 index,
1809                              struct scic_sds_controller *scic)
1810 {
1811         sci_base_state_machine_construct(&sci_port->state_machine,
1812                                          scic_sds_port_state_table,
1813                                          SCI_BASE_PORT_STATE_STOPPED);
1814
1815         sci_base_state_machine_start(&sci_port->state_machine);
1816
1817         sci_port->logical_port_index  = SCIC_SDS_DUMMY_PORT;
1818         sci_port->physical_port_index = index;
1819         sci_port->active_phy_mask     = 0;
1820         sci_port->ready_exit          = false;
1821
1822         sci_port->owning_controller = scic;
1823
1824         sci_port->started_request_count = 0;
1825         sci_port->assigned_device_count = 0;
1826
1827         sci_port->reserved_rni = SCU_DUMMY_INDEX;
1828         sci_port->reserved_tci = SCU_DUMMY_INDEX;
1829
1830         sci_init_timer(&sci_port->timer, port_timeout);
1831
1832         sci_port->port_task_scheduler_registers = NULL;
1833
1834         for (index = 0; index < SCI_MAX_PHYS; index++)
1835                 sci_port->phy_table[index] = NULL;
1836 }
1837
1838 void isci_port_init(struct isci_port *iport, struct isci_host *ihost, int index)
1839 {
1840         INIT_LIST_HEAD(&iport->remote_dev_list);
1841         INIT_LIST_HEAD(&iport->domain_dev_list);
1842         spin_lock_init(&iport->state_lock);
1843         init_completion(&iport->start_complete);
1844         iport->isci_host = ihost;
1845         isci_port_change_state(iport, isci_freed);
1846 }
1847
1848 /**
1849  * isci_port_get_state() - This function gets the status of the port object.
1850  * @isci_port: This parameter points to the isci_port object
1851  *
1852  * status of the object as a isci_status enum.
1853  */
1854 enum isci_status isci_port_get_state(
1855         struct isci_port *isci_port)
1856 {
1857         return isci_port->status;
1858 }
1859
1860 static void isci_port_bc_change_received(struct isci_host *ihost,
1861                                          struct scic_sds_port *sci_port,
1862                                          struct scic_sds_phy *sci_phy)
1863 {
1864         struct isci_phy *iphy = sci_phy_to_iphy(sci_phy);
1865
1866         dev_dbg(&ihost->pdev->dev, "%s: iphy = %p, sas_phy = %p\n",
1867                 __func__, iphy, &iphy->sas_phy);
1868
1869         ihost->sas_ha.notify_port_event(&iphy->sas_phy, PORTE_BROADCAST_RCVD);
1870         scic_port_enable_broadcast_change_notification(sci_port);
1871 }
1872
1873 void scic_sds_port_broadcast_change_received(
1874         struct scic_sds_port *sci_port,
1875         struct scic_sds_phy *sci_phy)
1876 {
1877         struct scic_sds_controller *scic = sci_port->owning_controller;
1878         struct isci_host *ihost = scic_to_ihost(scic);
1879
1880         /* notify the user. */
1881         isci_port_bc_change_received(ihost, sci_port, sci_phy);
1882 }
1883
1884 int isci_port_perform_hard_reset(struct isci_host *ihost, struct isci_port *iport,
1885                                  struct isci_phy *iphy)
1886 {
1887         unsigned long flags;
1888         enum sci_status status;
1889         int ret = TMF_RESP_FUNC_COMPLETE;
1890
1891         dev_dbg(&ihost->pdev->dev, "%s: iport = %p\n",
1892                 __func__, iport);
1893
1894         init_completion(&iport->hard_reset_complete);
1895
1896         spin_lock_irqsave(&ihost->scic_lock, flags);
1897
1898         #define ISCI_PORT_RESET_TIMEOUT SCIC_SDS_SIGNATURE_FIS_TIMEOUT
1899         status = scic_port_hard_reset(&iport->sci, ISCI_PORT_RESET_TIMEOUT);
1900
1901         spin_unlock_irqrestore(&ihost->scic_lock, flags);
1902
1903         if (status == SCI_SUCCESS) {
1904                 wait_for_completion(&iport->hard_reset_complete);
1905
1906                 dev_dbg(&ihost->pdev->dev,
1907                         "%s: iport = %p; hard reset completion\n",
1908                         __func__, iport);
1909
1910                 if (iport->hard_reset_status != SCI_SUCCESS)
1911                         ret = TMF_RESP_FUNC_FAILED;
1912         } else {
1913                 ret = TMF_RESP_FUNC_FAILED;
1914
1915                 dev_err(&ihost->pdev->dev,
1916                         "%s: iport = %p; scic_port_hard_reset call"
1917                         " failed 0x%x\n",
1918                         __func__, iport, status);
1919
1920         }
1921
1922         /* If the hard reset for the port has failed, consider this
1923          * the same as link failures on all phys in the port.
1924          */
1925         if (ret != TMF_RESP_FUNC_COMPLETE) {
1926                 dev_err(&ihost->pdev->dev,
1927                         "%s: iport = %p; hard reset failed "
1928                         "(0x%x) - sending link down to libsas for phy %p\n",
1929                         __func__, iport, iport->hard_reset_status, iphy);
1930
1931                 isci_port_link_down(ihost, iphy, iport);
1932         }
1933
1934         return ret;
1935 }
1936
1937 /**
1938  * isci_port_deformed() - This function is called by libsas when a port becomes
1939  *    inactive.
1940  * @phy: This parameter specifies the libsas phy with the inactive port.
1941  *
1942  */
1943 void isci_port_deformed(struct asd_sas_phy *phy)
1944 {
1945         pr_debug("%s: sas_phy = %p\n", __func__, phy);
1946 }
1947
1948 /**
1949  * isci_port_formed() - This function is called by libsas when a port becomes
1950  *    active.
1951  * @phy: This parameter specifies the libsas phy with the active port.
1952  *
1953  */
1954 void isci_port_formed(struct asd_sas_phy *phy)
1955 {
1956         pr_debug("%s: sas_phy = %p, sas_port = %p\n", __func__, phy, phy->port);
1957 }