isci: c99 port state handlers
[firefly-linux-kernel-4.4.55.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  * scic_sds_port_add_phy() -
534  * @sci_port: This parameter specifies the port in which the phy will be added.
535  * @sci_phy: This parameter is the phy which is to be added to the port.
536  *
537  * This method will add a PHY to the selected port. This method returns an
538  * enum sci_status. SCI_SUCCESS the phy has been added to the port. Any other status
539  * is failre to add the phy to the port.
540  */
541 enum sci_status scic_sds_port_add_phy(
542         struct scic_sds_port *sci_port,
543         struct scic_sds_phy *sci_phy)
544 {
545         return sci_port->state_handlers->add_phy_handler(
546                        sci_port, sci_phy);
547 }
548
549
550 /**
551  * scic_sds_port_remove_phy() -
552  * @sci_port: This parameter specifies the port in which the phy will be added.
553  * @sci_phy: This parameter is the phy which is to be added to the port.
554  *
555  * This method will remove the PHY from the selected PORT. This method returns
556  * an enum sci_status. SCI_SUCCESS the phy has been removed from the port. Any other
557  * status is failre to add the phy to the port.
558  */
559 enum sci_status scic_sds_port_remove_phy(
560         struct scic_sds_port *sci_port,
561         struct scic_sds_phy *sci_phy)
562 {
563         return sci_port->state_handlers->remove_phy_handler(
564                        sci_port, sci_phy);
565 }
566
567 /**
568  * This method requests the SAS address for the supplied SAS port from the SCI
569  *    implementation.
570  * @sci_port: a handle corresponding to the SAS port for which to return the
571  *    SAS address.
572  * @sas_address: This parameter specifies a pointer to a SAS address structure
573  *    into which the core will copy the SAS address for the port.
574  *
575  */
576 void scic_sds_port_get_sas_address(
577         struct scic_sds_port *sci_port,
578         struct sci_sas_address *sas_address)
579 {
580         u32 index;
581
582         sas_address->high = 0;
583         sas_address->low  = 0;
584
585         for (index = 0; index < SCI_MAX_PHYS; index++) {
586                 if (sci_port->phy_table[index] != NULL) {
587                         scic_sds_phy_get_sas_address(sci_port->phy_table[index], sas_address);
588                 }
589         }
590 }
591
592 /*
593  * This function requests the SAS address for the device directly attached to
594  *    this SAS port.
595  * @sci_port: a handle corresponding to the SAS port for which to return the
596  *    SAS address.
597  * @sas_address: This parameter specifies a pointer to a SAS address structure
598  *    into which the core will copy the SAS address for the device directly
599  *    attached to the port.
600  *
601  */
602 void scic_sds_port_get_attached_sas_address(
603         struct scic_sds_port *sci_port,
604         struct sci_sas_address *sas_address)
605 {
606         struct scic_sds_phy *sci_phy;
607
608         /*
609          * Ensure that the phy is both part of the port and currently
610          * connected to the remote end-point.
611          */
612         sci_phy = scic_sds_port_get_a_connected_phy(sci_port);
613         if (sci_phy) {
614                 if (sci_phy->protocol != SCIC_SDS_PHY_PROTOCOL_SATA) {
615                         scic_sds_phy_get_attached_sas_address(sci_phy,
616                                                               sas_address);
617                 } else {
618                         scic_sds_phy_get_sas_address(sci_phy, sas_address);
619                         sas_address->low += sci_phy->phy_index;
620                 }
621         } else {
622                 sas_address->high = 0;
623                 sas_address->low  = 0;
624         }
625 }
626
627 /**
628  * scic_sds_port_construct_dummy_rnc() - create dummy rnc for si workaround
629  *
630  * @sci_port: logical port on which we need to create the remote node context
631  * @rni: remote node index for this remote node context.
632  *
633  * This routine will construct a dummy remote node context data structure
634  * This structure will be posted to the hardware to work around a scheduler
635  * error in the hardware.
636  */
637 static void scic_sds_port_construct_dummy_rnc(struct scic_sds_port *sci_port, u16 rni)
638 {
639         union scu_remote_node_context *rnc;
640
641         rnc = &sci_port->owning_controller->remote_node_context_table[rni];
642
643         memset(rnc, 0, sizeof(union scu_remote_node_context));
644
645         rnc->ssp.remote_sas_address_hi = 0;
646         rnc->ssp.remote_sas_address_lo = 0;
647
648         rnc->ssp.remote_node_index = rni;
649         rnc->ssp.remote_node_port_width = 1;
650         rnc->ssp.logical_port_index = sci_port->physical_port_index;
651
652         rnc->ssp.nexus_loss_timer_enable = false;
653         rnc->ssp.check_bit = false;
654         rnc->ssp.is_valid = true;
655         rnc->ssp.is_remote_node_context = true;
656         rnc->ssp.function_number = 0;
657         rnc->ssp.arbitration_wait_time = 0;
658 }
659
660 /**
661  * scic_sds_port_construct_dummy_task() - create dummy task for si workaround
662  * @sci_port The logical port on which we need to create the
663  *            remote node context.
664  *            context.
665  * @tci The remote node index for this remote node context.
666  *
667  * This routine will construct a dummy task context data structure.  This
668  * structure will be posted to the hardwre to work around a scheduler error
669  * in the hardware.
670  *
671  */
672 static void scic_sds_port_construct_dummy_task(struct scic_sds_port *sci_port, u16 tci)
673 {
674         struct scu_task_context *task_context;
675
676         task_context = scic_sds_controller_get_task_context_buffer(sci_port->owning_controller, tci);
677
678         memset(task_context, 0, sizeof(struct scu_task_context));
679
680         task_context->abort = 0;
681         task_context->priority = 0;
682         task_context->initiator_request = 1;
683         task_context->connection_rate = 1;
684         task_context->protocol_engine_index = 0;
685         task_context->logical_port_index = sci_port->physical_port_index;
686         task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SSP;
687         task_context->task_index = scic_sds_io_tag_get_index(tci);
688         task_context->valid = SCU_TASK_CONTEXT_VALID;
689         task_context->context_type = SCU_TASK_CONTEXT_TYPE;
690
691         task_context->remote_node_index = sci_port->reserved_rni;
692         task_context->command_code = 0;
693
694         task_context->link_layer_control = 0;
695         task_context->do_not_dma_ssp_good_response = 1;
696         task_context->strict_ordering = 0;
697         task_context->control_frame = 0;
698         task_context->timeout_enable = 0;
699         task_context->block_guard_enable = 0;
700
701         task_context->address_modifier = 0;
702
703         task_context->task_phase = 0x01;
704 }
705
706 static void scic_sds_port_destroy_dummy_resources(struct scic_sds_port *sci_port)
707 {
708         struct scic_sds_controller *scic = sci_port->owning_controller;
709
710         if (sci_port->reserved_tci != SCU_DUMMY_INDEX)
711                 scic_controller_free_io_tag(scic, sci_port->reserved_tci);
712
713         if (sci_port->reserved_rni != SCU_DUMMY_INDEX)
714                 scic_sds_remote_node_table_release_remote_node_index(&scic->available_remote_nodes,
715                                                                      1, sci_port->reserved_rni);
716
717         sci_port->reserved_rni = SCU_DUMMY_INDEX;
718         sci_port->reserved_tci = SCU_DUMMY_INDEX;
719 }
720
721 /**
722  * This method performs initialization of the supplied port. Initialization
723  *    includes: - state machine initialization - member variable initialization
724  *    - configuring the phy_mask
725  * @sci_port:
726  * @transport_layer_registers:
727  * @port_task_scheduler_registers:
728  * @port_configuration_regsiter:
729  *
730  * enum sci_status SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION This value is returned
731  * if the phy being added to the port
732  */
733 enum sci_status scic_sds_port_initialize(
734         struct scic_sds_port *sci_port,
735         void __iomem *port_task_scheduler_registers,
736         void __iomem *port_configuration_regsiter,
737         void __iomem *viit_registers)
738 {
739         sci_port->port_task_scheduler_registers  = port_task_scheduler_registers;
740         sci_port->port_pe_configuration_register = port_configuration_regsiter;
741         sci_port->viit_registers                 = viit_registers;
742
743         return SCI_SUCCESS;
744 }
745
746 /**
747  * scic_port_hard_reset() - perform port hard reset
748  * @port: a handle corresponding to the SAS port to be hard reset.
749  * @reset_timeout: This parameter specifies the number of milliseconds in which
750  *    the port reset operation should complete.
751  *
752  * The SCI User callback in scic_user_callbacks_t will only be called once for
753  * each phy in the SAS Port at completion of the hard reset sequence. Return a
754  * status indicating whether the hard reset started successfully. SCI_SUCCESS
755  * This value is returned if the hard reset operation started successfully.
756  */
757 static enum sci_status scic_port_hard_reset(struct scic_sds_port *port,
758                                             u32 reset_timeout)
759 {
760         return port->state_handlers->reset_handler(
761                        port, reset_timeout);
762 }
763
764 /**
765  * This method assigns the direct attached device ID for this port.
766  *
767  * @param[in] sci_port The port for which the direct attached device id is to
768  *       be assigned.
769  * @param[in] device_id The direct attached device ID to assign to the port.
770  *       This will be the RNi for the device
771  */
772 void scic_sds_port_setup_transports(
773         struct scic_sds_port *sci_port,
774         u32 device_id)
775 {
776         u8 index;
777
778         for (index = 0; index < SCI_MAX_PHYS; index++) {
779                 if (sci_port->active_phy_mask & (1 << index))
780                         scic_sds_phy_setup_transport(sci_port->phy_table[index], device_id);
781         }
782 }
783
784 /**
785  *
786  * @sci_port: This is the port on which the phy should be enabled.
787  * @sci_phy: This is the specific phy which to enable.
788  * @do_notify_user: This parameter specifies whether to inform the user (via
789  *    scic_cb_port_link_up()) as to the fact that a new phy as become ready.
790  *
791  * This function will activate the phy in the port.
792  * Activation includes: - adding
793  * the phy to the port - enabling the Protocol Engine in the silicon. -
794  * notifying the user that the link is up. none
795  */
796 static void scic_sds_port_activate_phy(struct scic_sds_port *sci_port,
797                                        struct scic_sds_phy *sci_phy,
798                                        bool do_notify_user)
799 {
800         struct scic_sds_controller *scic = sci_port->owning_controller;
801         struct isci_host *ihost = scic_to_ihost(scic);
802
803         if (sci_phy->protocol != SCIC_SDS_PHY_PROTOCOL_SATA)
804                 scic_sds_phy_resume(sci_phy);
805
806         sci_port->active_phy_mask |= 1 << sci_phy->phy_index;
807
808         scic_sds_controller_clear_invalid_phy(scic, sci_phy);
809
810         if (do_notify_user == true)
811                 isci_port_link_up(ihost, sci_port, sci_phy);
812 }
813
814 void scic_sds_port_deactivate_phy(struct scic_sds_port *sci_port,
815                                   struct scic_sds_phy *sci_phy,
816                                   bool do_notify_user)
817 {
818         struct scic_sds_controller *scic = scic_sds_port_get_controller(sci_port);
819         struct isci_port *iport = sci_port_to_iport(sci_port);
820         struct isci_host *ihost = scic_to_ihost(scic);
821         struct isci_phy *iphy = sci_phy_to_iphy(sci_phy);
822
823         sci_port->active_phy_mask &= ~(1 << sci_phy->phy_index);
824
825         sci_phy->max_negotiated_speed = SAS_LINK_RATE_UNKNOWN;
826
827         /* Re-assign the phy back to the LP as if it were a narrow port */
828         writel(sci_phy->phy_index,
829                 &sci_port->port_pe_configuration_register[sci_phy->phy_index]);
830
831         if (do_notify_user == true)
832                 isci_port_link_down(ihost, iphy, iport);
833 }
834
835 /**
836  *
837  * @sci_port: This is the port on which the phy should be disabled.
838  * @sci_phy: This is the specific phy which to disabled.
839  *
840  * This function will disable the phy and report that the phy is not valid for
841  * this port object. None
842  */
843 static void scic_sds_port_invalid_link_up(struct scic_sds_port *sci_port,
844                                           struct scic_sds_phy *sci_phy)
845 {
846         struct scic_sds_controller *scic = sci_port->owning_controller;
847
848         /*
849          * Check to see if we have alreay reported this link as bad and if
850          * not go ahead and tell the SCI_USER that we have discovered an
851          * invalid link.
852          */
853         if ((scic->invalid_phy_mask & (1 << sci_phy->phy_index)) == 0) {
854                 scic_sds_controller_set_invalid_phy(scic, sci_phy);
855                 dev_warn(&scic_to_ihost(scic)->pdev->dev, "Invalid link up!\n");
856         }
857 }
858
859 /**
860  * scic_sds_port_general_link_up_handler - phy can be assigned to port?
861  * @sci_port: scic_sds_port object for which has a phy that has gone link up.
862  * @sci_phy: This is the struct scic_sds_phy object that has gone link up.
863  * @do_notify_user: This parameter specifies whether to inform the user (via
864  *    scic_cb_port_link_up()) as to the fact that a new phy as become ready.
865  *
866  * Determine if this phy can be assigned to this
867  * port . If the phy is not a valid PHY for
868  * this port then the function will notify the user. A PHY can only be
869  * part of a port if it's attached SAS ADDRESS is the same as all other PHYs in
870  * the same port. none
871  */
872 static void scic_sds_port_general_link_up_handler(struct scic_sds_port *sci_port,
873                                                   struct scic_sds_phy *sci_phy,
874                                                   bool do_notify_user)
875 {
876         struct sci_sas_address port_sas_address;
877         struct sci_sas_address phy_sas_address;
878
879         scic_sds_port_get_attached_sas_address(sci_port, &port_sas_address);
880         scic_sds_phy_get_attached_sas_address(sci_phy, &phy_sas_address);
881
882         /* If the SAS address of the new phy matches the SAS address of
883          * other phys in the port OR this is the first phy in the port,
884          * then activate the phy and allow it to be used for operations
885          * in this port.
886          */
887         if ((phy_sas_address.high == port_sas_address.high &&
888              phy_sas_address.low  == port_sas_address.low) ||
889             sci_port->active_phy_mask == 0) {
890                 struct sci_base_state_machine *sm = &sci_port->state_machine;
891
892                 scic_sds_port_activate_phy(sci_port, sci_phy, do_notify_user);
893                 if (sm->current_state_id == SCI_BASE_PORT_STATE_RESETTING)
894                         sci_base_state_machine_change_state(sm, SCI_BASE_PORT_STATE_READY);
895         } else
896                 scic_sds_port_invalid_link_up(sci_port, sci_phy);
897 }
898
899
900
901 /**
902  * This method returns false if the port only has a single phy object assigned.
903  *     If there are no phys or more than one phy then the method will return
904  *    true.
905  * @sci_port: The port for which the wide port condition is to be checked.
906  *
907  * bool true Is returned if this is a wide ported port. false Is returned if
908  * this is a narrow port.
909  */
910 static bool scic_sds_port_is_wide(struct scic_sds_port *sci_port)
911 {
912         u32 index;
913         u32 phy_count = 0;
914
915         for (index = 0; index < SCI_MAX_PHYS; index++) {
916                 if (sci_port->phy_table[index] != NULL) {
917                         phy_count++;
918                 }
919         }
920
921         return phy_count != 1;
922 }
923
924 /**
925  * This method is called by the PHY object when the link is detected. if the
926  *    port wants the PHY to continue on to the link up state then the port
927  *    layer must return true.  If the port object returns false the phy object
928  *    must halt its attempt to go link up.
929  * @sci_port: The port associated with the phy object.
930  * @sci_phy: The phy object that is trying to go link up.
931  *
932  * true if the phy object can continue to the link up condition. true Is
933  * returned if this phy can continue to the ready state. false Is returned if
934  * can not continue on to the ready state. This notification is in place for
935  * wide ports and direct attached phys.  Since there are no wide ported SATA
936  * devices this could become an invalid port configuration.
937  */
938 bool scic_sds_port_link_detected(
939         struct scic_sds_port *sci_port,
940         struct scic_sds_phy *sci_phy)
941 {
942         if ((sci_port->logical_port_index != SCIC_SDS_DUMMY_PORT) &&
943             (sci_phy->protocol == SCIC_SDS_PHY_PROTOCOL_SATA) &&
944             scic_sds_port_is_wide(sci_port)) {
945                 scic_sds_port_invalid_link_up(sci_port, sci_phy);
946
947                 return false;
948         }
949
950         return true;
951 }
952
953 /**
954  * This method is the entry point for the phy to inform the port that it is now
955  *    in a ready state
956  * @sci_port:
957  *
958  *
959  */
960 void scic_sds_port_link_up(
961         struct scic_sds_port *sci_port,
962         struct scic_sds_phy *sci_phy)
963 {
964         sci_phy->is_in_link_training = false;
965
966         sci_port->state_handlers->link_up_handler(sci_port, sci_phy);
967 }
968
969 /**
970  * This method is the entry point for the phy to inform the port that it is no
971  *    longer in a ready state
972  * @sci_port:
973  *
974  *
975  */
976 void scic_sds_port_link_down(
977         struct scic_sds_port *sci_port,
978         struct scic_sds_phy *sci_phy)
979 {
980         sci_port->state_handlers->link_down_handler(sci_port, sci_phy);
981 }
982
983 /**
984  * This method is called to start an IO request on this port.
985  * @sci_port:
986  * @sci_dev:
987  * @sci_req:
988  *
989  * enum sci_status
990  */
991 enum sci_status scic_sds_port_start_io(
992         struct scic_sds_port *sci_port,
993         struct scic_sds_remote_device *sci_dev,
994         struct scic_sds_request *sci_req)
995 {
996         return sci_port->state_handlers->start_io_handler(
997                        sci_port, sci_dev, sci_req);
998 }
999
1000 /**
1001  * This method is called to complete an IO request to the port.
1002  * @sci_port:
1003  * @sci_dev:
1004  * @sci_req:
1005  *
1006  * enum sci_status
1007  */
1008 enum sci_status scic_sds_port_complete_io(
1009         struct scic_sds_port *sci_port,
1010         struct scic_sds_remote_device *sci_dev,
1011         struct scic_sds_request *sci_req)
1012 {
1013         return sci_port->state_handlers->complete_io_handler(
1014                        sci_port, sci_dev, sci_req);
1015 }
1016
1017 /**
1018  * This method is provided to timeout requests for port operations. Mostly its
1019  *    for the port reset operation.
1020  *
1021  *
1022  */
1023 static void scic_sds_port_timeout_handler(void *port)
1024 {
1025         struct scic_sds_port *sci_port = port;
1026         u32 current_state;
1027
1028         current_state = sci_base_state_machine_get_state(
1029                 &sci_port->state_machine);
1030
1031         if (current_state == SCI_BASE_PORT_STATE_RESETTING) {
1032                 /*
1033                  * if the port is still in the resetting state then the
1034                  * timeout fired before the reset completed.
1035                  */
1036                 sci_base_state_machine_change_state(
1037                         &sci_port->state_machine,
1038                         SCI_BASE_PORT_STATE_FAILED);
1039         } else if (current_state == SCI_BASE_PORT_STATE_STOPPED) {
1040                 /*
1041                  * if the port is stopped then the start request failed
1042                  * In this case stay in the stopped state.
1043                  */
1044                 dev_err(sciport_to_dev(sci_port),
1045                         "%s: SCIC Port 0x%p failed to stop before tiemout.\n",
1046                         __func__,
1047                         sci_port);
1048         } else if (current_state == SCI_BASE_PORT_STATE_STOPPING) {
1049                 /*
1050                  * if the port is still stopping then the stop has not
1051                  * completed
1052                  */
1053                 isci_port_stop_complete(
1054                                 scic_sds_port_get_controller(sci_port),
1055                                 sci_port,
1056                                 SCI_FAILURE_TIMEOUT);
1057         } else {
1058                 /*
1059                  * The port is in the ready state and we have a timer
1060                  * reporting a timeout this should not happen.
1061                  */
1062                 dev_err(sciport_to_dev(sci_port),
1063                         "%s: SCIC Port 0x%p is processing a timeout operation "
1064                         "in state %d.\n",
1065                         __func__,
1066                         sci_port,
1067                         current_state);
1068         }
1069 }
1070
1071 /* --------------------------------------------------------------------------- */
1072
1073 /**
1074  * This function updates the hardwares VIIT entry for this port.
1075  *
1076  *
1077  */
1078 static void scic_sds_port_update_viit_entry(struct scic_sds_port *sci_port)
1079 {
1080         struct sci_sas_address sas_address;
1081
1082         scic_sds_port_get_sas_address(sci_port, &sas_address);
1083
1084         writel(sas_address.high,
1085                 &sci_port->viit_registers->initiator_sas_address_hi);
1086         writel(sas_address.low,
1087                 &sci_port->viit_registers->initiator_sas_address_lo);
1088
1089         /* This value get cleared just in case its not already cleared */
1090         writel(0, &sci_port->viit_registers->reserved);
1091
1092         /* We are required to update the status register last */
1093         writel(SCU_VIIT_ENTRY_ID_VIIT |
1094                SCU_VIIT_IPPT_INITIATOR |
1095                ((1 << sci_port->physical_port_index) << SCU_VIIT_ENTRY_LPVIE_SHIFT) |
1096                SCU_VIIT_STATUS_ALL_VALID,
1097                &sci_port->viit_registers->status);
1098 }
1099
1100 /**
1101  * This method returns the maximum allowed speed for data transfers on this
1102  *    port.  This maximum allowed speed evaluates to the maximum speed of the
1103  *    slowest phy in the port.
1104  * @sci_port: This parameter specifies the port for which to retrieve the
1105  *    maximum allowed speed.
1106  *
1107  * This method returns the maximum negotiated speed of the slowest phy in the
1108  * port.
1109  */
1110 enum sas_linkrate scic_sds_port_get_max_allowed_speed(
1111         struct scic_sds_port *sci_port)
1112 {
1113         u16 index;
1114         enum sas_linkrate max_allowed_speed = SAS_LINK_RATE_6_0_GBPS;
1115         struct scic_sds_phy *phy = NULL;
1116
1117         /*
1118          * Loop through all of the phys in this port and find the phy with the
1119          * lowest maximum link rate. */
1120         for (index = 0; index < SCI_MAX_PHYS; index++) {
1121                 phy = sci_port->phy_table[index];
1122                 if (
1123                         (phy != NULL)
1124                         && (scic_sds_port_active_phy(sci_port, phy) == true)
1125                         && (phy->max_negotiated_speed < max_allowed_speed)
1126                         )
1127                         max_allowed_speed = phy->max_negotiated_speed;
1128         }
1129
1130         return max_allowed_speed;
1131 }
1132
1133 static void scic_port_enable_broadcast_change_notification(struct scic_sds_port *port)
1134 {
1135         struct scic_sds_phy *phy;
1136         u32 register_value;
1137         u8 index;
1138
1139         /* Loop through all of the phys to enable BCN. */
1140         for (index = 0; index < SCI_MAX_PHYS; index++) {
1141                 phy = port->phy_table[index];
1142                 if (phy != NULL) {
1143                         register_value =
1144                                 readl(&phy->link_layer_registers->link_layer_control);
1145
1146                         /* clear the bit by writing 1. */
1147                         writel(register_value,
1148                                 &phy->link_layer_registers->link_layer_control);
1149                 }
1150         }
1151 }
1152
1153 /*
1154  * ****************************************************************************
1155  * *  READY SUBSTATE HANDLERS
1156  * **************************************************************************** */
1157
1158 /*
1159  * This method is the general ready state stop handler for the struct scic_sds_port
1160  * object.  This function will transition the ready substate machine to its
1161  * final state. enum sci_status SCI_SUCCESS
1162  */
1163 static enum sci_status scic_sds_port_ready_substate_stop_handler(
1164         struct scic_sds_port *port)
1165 {
1166         sci_base_state_machine_change_state(
1167                 &port->state_machine,
1168                 SCI_BASE_PORT_STATE_STOPPING
1169                 );
1170
1171         return SCI_SUCCESS;
1172 }
1173
1174 /*
1175  * This method is the general ready substate complete io handler for the
1176  * struct scic_sds_port object.  This function decrments the outstanding request count
1177  * for this port object. enum sci_status SCI_SUCCESS
1178  */
1179 static enum sci_status scic_sds_port_ready_substate_complete_io_handler(
1180         struct scic_sds_port *port,
1181         struct scic_sds_remote_device *device,
1182         struct scic_sds_request *io_request)
1183 {
1184         scic_sds_port_decrement_request_count(port);
1185
1186         return SCI_SUCCESS;
1187 }
1188
1189 static enum sci_status scic_sds_port_ready_substate_add_phy_handler(
1190         struct scic_sds_port *port,
1191         struct scic_sds_phy *phy)
1192 {
1193         enum sci_status status;
1194
1195         status = scic_sds_port_set_phy(port, phy);
1196
1197         if (status == SCI_SUCCESS) {
1198                 scic_sds_port_general_link_up_handler(port, phy, true);
1199
1200                 port->not_ready_reason = SCIC_PORT_NOT_READY_RECONFIGURING;
1201
1202                 sci_base_state_machine_change_state(
1203                         &port->ready_substate_machine,
1204                         SCIC_SDS_PORT_READY_SUBSTATE_CONFIGURING
1205                         );
1206         }
1207
1208         return status;
1209 }
1210
1211
1212 static enum sci_status scic_sds_port_ready_substate_remove_phy_handler(
1213         struct scic_sds_port *port,
1214         struct scic_sds_phy *phy)
1215 {
1216         enum sci_status status;
1217
1218         status = scic_sds_port_clear_phy(port, phy);
1219
1220         if (status == SCI_SUCCESS) {
1221                 scic_sds_port_deactivate_phy(port, phy, true);
1222
1223                 port->not_ready_reason = SCIC_PORT_NOT_READY_RECONFIGURING;
1224
1225                 sci_base_state_machine_change_state(
1226                         &port->ready_substate_machine,
1227                         SCIC_SDS_PORT_READY_SUBSTATE_CONFIGURING
1228                         );
1229         }
1230
1231         return status;
1232 }
1233
1234 /*
1235  * ****************************************************************************
1236  * *  READY SUBSTATE WAITING HANDLERS
1237  * **************************************************************************** */
1238
1239 /**
1240  *
1241  * @sci_port: This is the struct scic_sds_port object that which has a phy that has
1242  *    gone link up.
1243  * @sci_phy: This is the struct scic_sds_phy object that has gone link up.
1244  *
1245  * This method is the ready waiting substate link up handler for the
1246  * struct scic_sds_port object.  This methos will report the link up condition for
1247  * this port and will transition to the ready operational substate. none
1248  */
1249 static void scic_sds_port_ready_waiting_substate_link_up_handler(
1250         struct scic_sds_port *sci_port,
1251         struct scic_sds_phy *sci_phy)
1252 {
1253         /*
1254          * Since this is the first phy going link up for the port we can just enable
1255          * it and continue. */
1256         scic_sds_port_activate_phy(sci_port, sci_phy, true);
1257
1258         sci_base_state_machine_change_state(
1259                 &sci_port->ready_substate_machine,
1260                 SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL
1261                 );
1262 }
1263
1264 /*
1265  * This method is the ready waiting substate start io handler for the
1266  * struct scic_sds_port object. The port object can not accept new requests so the
1267  * request is failed. enum sci_status SCI_FAILURE_INVALID_STATE
1268  */
1269 static enum sci_status scic_sds_port_ready_waiting_substate_start_io_handler(
1270         struct scic_sds_port *port,
1271         struct scic_sds_remote_device *device,
1272         struct scic_sds_request *io_request)
1273 {
1274         return SCI_FAILURE_INVALID_STATE;
1275 }
1276
1277 /*
1278  * ****************************************************************************
1279  * *  READY SUBSTATE OPERATIONAL HANDLERS
1280  * **************************************************************************** */
1281
1282 /*
1283  * This method will casue the port to reset. enum sci_status SCI_SUCCESS
1284  */
1285 static enum
1286 sci_status scic_sds_port_ready_operational_substate_reset_handler(
1287                 struct scic_sds_port *port,
1288                 u32 timeout)
1289 {
1290         enum sci_status status = SCI_FAILURE_INVALID_PHY;
1291         u32 phy_index;
1292         struct scic_sds_phy *selected_phy = NULL;
1293
1294
1295         /* Select a phy on which we can send the hard reset request. */
1296         for (phy_index = 0;
1297              (phy_index < SCI_MAX_PHYS) && (selected_phy == NULL);
1298              phy_index++) {
1299                 selected_phy = port->phy_table[phy_index];
1300
1301                 if ((selected_phy != NULL) &&
1302                     !scic_sds_port_active_phy(port, selected_phy)) {
1303                         /*
1304                          * We found a phy but it is not ready select
1305                          * different phy
1306                          */
1307                         selected_phy = NULL;
1308                 }
1309         }
1310
1311         /* If we have a phy then go ahead and start the reset procedure */
1312         if (selected_phy != NULL) {
1313                 status = scic_sds_phy_reset(selected_phy);
1314
1315                 if (status == SCI_SUCCESS) {
1316                         isci_timer_start(port->timer_handle, timeout);
1317                         port->not_ready_reason =
1318                                 SCIC_PORT_NOT_READY_HARD_RESET_REQUESTED;
1319
1320                         sci_base_state_machine_change_state(
1321                                         &port->state_machine,
1322                                         SCI_BASE_PORT_STATE_RESETTING);
1323                 }
1324         }
1325
1326         return status;
1327 }
1328
1329 /**
1330  * scic_sds_port_ready_operational_substate_link_up_handler() -
1331  * @sci_port: This is the struct scic_sds_port object that which has a phy that has
1332  *    gone link up.
1333  * @sci_phy: This is the struct scic_sds_phy object that has gone link up.
1334  *
1335  * This method is the ready operational substate link up handler for the
1336  * struct scic_sds_port object. This function notifies the SCI User that the phy has
1337  * gone link up. none
1338  */
1339 static void scic_sds_port_ready_operational_substate_link_up_handler(
1340         struct scic_sds_port *sci_port,
1341         struct scic_sds_phy *sci_phy)
1342 {
1343         scic_sds_port_general_link_up_handler(sci_port, sci_phy, true);
1344 }
1345
1346 /**
1347  * scic_sds_port_ready_operational_substate_link_down_handler() -
1348  * @sci_port: This is the struct scic_sds_port object that which has a phy that has
1349  *    gone link down.
1350  * @sci_phy: This is the struct scic_sds_phy object that has gone link down.
1351  *
1352  * This method is the ready operational substate link down handler for the
1353  * struct scic_sds_port object. This function notifies the SCI User that the phy has
1354  * gone link down and if this is the last phy in the port the port will change
1355  * state to the ready waiting substate. none
1356  */
1357 static void scic_sds_port_ready_operational_substate_link_down_handler(
1358         struct scic_sds_port *sci_port,
1359         struct scic_sds_phy *sci_phy)
1360 {
1361         scic_sds_port_deactivate_phy(sci_port, sci_phy, true);
1362
1363         /*
1364          * If there are no active phys left in the port, then transition
1365          * the port to the WAITING state until such time as a phy goes
1366          * link up. */
1367         if (sci_port->active_phy_mask == 0)
1368                 sci_base_state_machine_change_state(&sci_port->ready_substate_machine,
1369                                                     SCIC_SDS_PORT_READY_SUBSTATE_WAITING);
1370 }
1371
1372 /*
1373  * This method is the ready operational substate start io handler for the
1374  * struct scic_sds_port object.  This function incremetns the outstanding request
1375  * count for this port object. enum sci_status SCI_SUCCESS
1376  */
1377 static enum sci_status scic_sds_port_ready_operational_substate_start_io_handler(
1378         struct scic_sds_port *port,
1379         struct scic_sds_remote_device *device,
1380         struct scic_sds_request *io_request)
1381 {
1382         port->started_request_count++;
1383         return SCI_SUCCESS;
1384 }
1385
1386 /*
1387  * ****************************************************************************
1388  * *  READY SUBSTATE OPERATIONAL HANDLERS
1389  * **************************************************************************** */
1390
1391 /*
1392  * This is the default method for a port add phy request.  It will report a
1393  * warning and exit. enum sci_status SCI_FAILURE_INVALID_STATE
1394  */
1395 static enum sci_status scic_sds_port_ready_configuring_substate_add_phy_handler(
1396         struct scic_sds_port *port,
1397         struct scic_sds_phy *phy)
1398 {
1399         enum sci_status status;
1400
1401         status = scic_sds_port_set_phy(port, phy);
1402
1403         if (status == SCI_SUCCESS) {
1404                 scic_sds_port_general_link_up_handler(port, phy, true);
1405
1406                 /*
1407                  * Re-enter the configuring state since this may be the last phy in
1408                  * the port. */
1409                 sci_base_state_machine_change_state(
1410                         &port->ready_substate_machine,
1411                         SCIC_SDS_PORT_READY_SUBSTATE_CONFIGURING
1412                         );
1413         }
1414
1415         return status;
1416 }
1417
1418 /*
1419  * This is the default method for a port remove phy request.  It will report a
1420  * warning and exit. enum sci_status SCI_FAILURE_INVALID_STATE
1421  */
1422 static enum sci_status scic_sds_port_ready_configuring_substate_remove_phy_handler(
1423         struct scic_sds_port *port,
1424         struct scic_sds_phy *phy)
1425 {
1426         enum sci_status status;
1427
1428         status = scic_sds_port_clear_phy(port, phy);
1429
1430         if (status == SCI_SUCCESS) {
1431                 scic_sds_port_deactivate_phy(port, phy, true);
1432
1433                 /*
1434                  * Re-enter the configuring state since this may be the last phy in
1435                  * the port. */
1436                 sci_base_state_machine_change_state(
1437                         &port->ready_substate_machine,
1438                         SCIC_SDS_PORT_READY_SUBSTATE_CONFIGURING
1439                         );
1440         }
1441
1442         return status;
1443 }
1444
1445 /**
1446  * scic_sds_port_ready_configuring_substate_complete_io_handler() -
1447  * @port: This is the port that is being requested to complete the io request.
1448  * @device: This is the device on which the io is completing.
1449  *
1450  * This method will decrement the outstanding request count for this port. If
1451  * the request count goes to 0 then the port can be reprogrammed with its new
1452  * phy data.
1453  */
1454 static enum sci_status
1455 scic_sds_port_ready_configuring_substate_complete_io_handler(
1456         struct scic_sds_port *port,
1457         struct scic_sds_remote_device *device,
1458         struct scic_sds_request *io_request)
1459 {
1460         scic_sds_port_decrement_request_count(port);
1461
1462         if (port->started_request_count == 0) {
1463                 sci_base_state_machine_change_state(
1464                         &port->ready_substate_machine,
1465                         SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL
1466                         );
1467         }
1468
1469         return SCI_SUCCESS;
1470 }
1471
1472 static enum sci_status default_port_handler(struct scic_sds_port *sci_port,
1473                                             const char *func)
1474 {
1475         dev_warn(sciport_to_dev(sci_port),
1476                  "%s: in wrong state: %d\n", func,
1477                  sci_base_state_machine_get_state(&sci_port->state_machine));
1478         return SCI_FAILURE_INVALID_STATE;
1479 }
1480
1481 static enum sci_status
1482 scic_sds_port_default_start_handler(struct scic_sds_port *sci_port)
1483 {
1484         return default_port_handler(sci_port, __func__);
1485 }
1486
1487 static enum sci_status
1488 scic_sds_port_default_stop_handler(struct scic_sds_port *sci_port)
1489 {
1490         return default_port_handler(sci_port, __func__);
1491 }
1492
1493 static enum sci_status
1494 scic_sds_port_default_destruct_handler(struct scic_sds_port *sci_port)
1495 {
1496         return default_port_handler(sci_port, __func__);
1497 }
1498
1499 static enum sci_status
1500 scic_sds_port_default_reset_handler(struct scic_sds_port *sci_port,
1501                                     u32 timeout)
1502 {
1503         return default_port_handler(sci_port, __func__);
1504 }
1505
1506 static enum sci_status
1507 scic_sds_port_default_add_phy_handler(struct scic_sds_port *sci_port,
1508                                       struct scic_sds_phy *base_phy)
1509 {
1510         return default_port_handler(sci_port, __func__);
1511 }
1512
1513 static enum sci_status
1514 scic_sds_port_default_remove_phy_handler(struct scic_sds_port *sci_port,
1515                                          struct scic_sds_phy *base_phy)
1516 {
1517         return default_port_handler(sci_port, __func__);
1518 }
1519
1520 /*
1521  * This is the default method for a port unsolicited frame request.  It will
1522  * report a warning and exit. enum sci_status SCI_FAILURE_INVALID_STATE Is it even
1523  * possible to receive an unsolicited frame directed to a port object?  It
1524  * seems possible if we implementing virtual functions but until then?
1525  */
1526 static enum sci_status
1527 scic_sds_port_default_frame_handler(struct scic_sds_port *sci_port,
1528                                     u32 frame_index)
1529 {
1530         struct scic_sds_controller *scic = scic_sds_port_get_controller(sci_port);
1531
1532         default_port_handler(sci_port, __func__);
1533         scic_sds_controller_release_frame(scic, frame_index);
1534
1535         return SCI_FAILURE_INVALID_STATE;
1536 }
1537
1538 static enum sci_status scic_sds_port_default_event_handler(struct scic_sds_port *sci_port,
1539                                                     u32 event_code)
1540 {
1541         return default_port_handler(sci_port, __func__);
1542 }
1543
1544 static void scic_sds_port_default_link_up_handler(struct scic_sds_port *sci_port,
1545                                            struct scic_sds_phy *sci_phy)
1546 {
1547         default_port_handler(sci_port, __func__);
1548 }
1549
1550 static void scic_sds_port_default_link_down_handler(struct scic_sds_port *sci_port,
1551                                              struct scic_sds_phy *sci_phy)
1552 {
1553         default_port_handler(sci_port, __func__);
1554 }
1555
1556 static enum sci_status scic_sds_port_default_start_io_handler(struct scic_sds_port *sci_port,
1557                                                        struct scic_sds_remote_device *sci_dev,
1558                                                        struct scic_sds_request *sci_req)
1559 {
1560         return default_port_handler(sci_port, __func__);
1561 }
1562
1563 static enum sci_status scic_sds_port_default_complete_io_handler(struct scic_sds_port *sci_port,
1564                                                                  struct scic_sds_remote_device *sci_dev,
1565                                                                  struct scic_sds_request *sci_req)
1566 {
1567         return default_port_handler(sci_port, __func__);
1568 }
1569
1570 static struct scic_sds_port_state_handler scic_sds_port_ready_substate_handler_table[] = {
1571         [SCIC_SDS_PORT_READY_SUBSTATE_WAITING] = {
1572                 .start_handler          = scic_sds_port_default_start_handler,
1573                 .stop_handler           = scic_sds_port_ready_substate_stop_handler,
1574                 .destruct_handler       = scic_sds_port_default_destruct_handler,
1575                 .reset_handler          = scic_sds_port_default_reset_handler,
1576                 .add_phy_handler        = scic_sds_port_ready_substate_add_phy_handler,
1577                 .remove_phy_handler     = scic_sds_port_default_remove_phy_handler,
1578                 .frame_handler          = scic_sds_port_default_frame_handler,
1579                 .event_handler          = scic_sds_port_default_event_handler,
1580                 .link_up_handler        = scic_sds_port_ready_waiting_substate_link_up_handler,
1581                 .link_down_handler      = scic_sds_port_default_link_down_handler,
1582                 .start_io_handler       = scic_sds_port_ready_waiting_substate_start_io_handler,
1583                 .complete_io_handler    = scic_sds_port_ready_substate_complete_io_handler,
1584         },
1585         [SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL] = {
1586                 .start_handler          = scic_sds_port_default_start_handler,
1587                 .stop_handler           = scic_sds_port_ready_substate_stop_handler,
1588                 .destruct_handler       = scic_sds_port_default_destruct_handler,
1589                 .reset_handler          = scic_sds_port_ready_operational_substate_reset_handler,
1590                 .add_phy_handler        = scic_sds_port_ready_substate_add_phy_handler,
1591                 .remove_phy_handler     = scic_sds_port_ready_substate_remove_phy_handler,
1592                 .frame_handler          = scic_sds_port_default_frame_handler,
1593                 .event_handler          = scic_sds_port_default_event_handler,
1594                 .link_up_handler        = scic_sds_port_ready_operational_substate_link_up_handler,
1595                 .link_down_handler      = scic_sds_port_ready_operational_substate_link_down_handler,
1596                 .start_io_handler       = scic_sds_port_ready_operational_substate_start_io_handler,
1597                 .complete_io_handler    = scic_sds_port_ready_substate_complete_io_handler,
1598         },
1599         [SCIC_SDS_PORT_READY_SUBSTATE_CONFIGURING] = {
1600                 .start_handler          = scic_sds_port_default_start_handler,
1601                 .stop_handler           = scic_sds_port_ready_substate_stop_handler,
1602                 .destruct_handler       = scic_sds_port_default_destruct_handler,
1603                 .reset_handler          = scic_sds_port_default_reset_handler,
1604                 .add_phy_handler        = scic_sds_port_ready_configuring_substate_add_phy_handler,
1605                 .remove_phy_handler     = scic_sds_port_ready_configuring_substate_remove_phy_handler,
1606                 .frame_handler          = scic_sds_port_default_frame_handler,
1607                 .event_handler          = scic_sds_port_default_event_handler,
1608                 .link_up_handler        = scic_sds_port_default_link_up_handler,
1609                 .link_down_handler      = scic_sds_port_default_link_down_handler,
1610                 .start_io_handler       = scic_sds_port_default_start_io_handler,
1611                 .complete_io_handler    = scic_sds_port_ready_configuring_substate_complete_io_handler
1612         }
1613 };
1614
1615 /**
1616  * scic_sds_port_set_ready_state_handlers() -
1617  *
1618  * This macro sets the port ready substate handlers.
1619  */
1620 #define scic_sds_port_set_ready_state_handlers(port, state_id) \
1621         scic_sds_port_set_state_handlers(\
1622                 port, &scic_sds_port_ready_substate_handler_table[(state_id)] \
1623                 )
1624
1625 /*
1626  * ******************************************************************************
1627  * *  PORT STATE PRIVATE METHODS
1628  * ****************************************************************************** */
1629
1630 /**
1631  *
1632  * @sci_port: This is the struct scic_sds_port object to suspend.
1633  *
1634  * This method will susped the port task scheduler for this port object. none
1635  */
1636 static void
1637 scic_sds_port_suspend_port_task_scheduler(struct scic_sds_port *port)
1638 {
1639         u32 pts_control_value;
1640
1641         pts_control_value = readl(&port->port_task_scheduler_registers->control);
1642         pts_control_value |= SCU_PTSxCR_GEN_BIT(SUSPEND);
1643         writel(pts_control_value, &port->port_task_scheduler_registers->control);
1644 }
1645
1646 /**
1647  * scic_sds_port_post_dummy_request() - post dummy/workaround request
1648  * @sci_port: port to post task
1649  *
1650  * Prevent the hardware scheduler from posting new requests to the front
1651  * of the scheduler queue causing a starvation problem for currently
1652  * ongoing requests.
1653  *
1654  */
1655 static void scic_sds_port_post_dummy_request(struct scic_sds_port *sci_port)
1656 {
1657         u32 command;
1658         struct scu_task_context *task_context;
1659         struct scic_sds_controller *scic = sci_port->owning_controller;
1660         u16 tci = sci_port->reserved_tci;
1661
1662         task_context = scic_sds_controller_get_task_context_buffer(scic, tci);
1663
1664         task_context->abort = 0;
1665
1666         command = SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
1667                   sci_port->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT |
1668                   tci;
1669
1670         scic_sds_controller_post_request(scic, command);
1671 }
1672
1673 /**
1674  * This routine will abort the dummy request.  This will alow the hardware to
1675  * power down parts of the silicon to save power.
1676  *
1677  * @sci_port: The port on which the task must be aborted.
1678  *
1679  */
1680 static void scic_sds_port_abort_dummy_request(struct scic_sds_port *sci_port)
1681 {
1682         struct scic_sds_controller *scic = sci_port->owning_controller;
1683         u16 tci = sci_port->reserved_tci;
1684         struct scu_task_context *tc;
1685         u32 command;
1686
1687         tc = scic_sds_controller_get_task_context_buffer(scic, tci);
1688
1689         tc->abort = 1;
1690
1691         command = SCU_CONTEXT_COMMAND_REQUEST_POST_TC_ABORT |
1692                   sci_port->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT |
1693                   tci;
1694
1695         scic_sds_controller_post_request(scic, command);
1696 }
1697
1698 /**
1699  *
1700  * @sci_port: This is the struct scic_sds_port object to resume.
1701  *
1702  * This method will resume the port task scheduler for this port object. none
1703  */
1704 static void
1705 scic_sds_port_resume_port_task_scheduler(struct scic_sds_port *port)
1706 {
1707         u32 pts_control_value;
1708
1709         pts_control_value = readl(&port->port_task_scheduler_registers->control);
1710         pts_control_value &= ~SCU_PTSxCR_GEN_BIT(SUSPEND);
1711         writel(pts_control_value, &port->port_task_scheduler_registers->control);
1712 }
1713
1714 /*
1715  * ******************************************************************************
1716  * *  PORT READY SUBSTATE METHODS
1717  * ****************************************************************************** */
1718
1719 /**
1720  *
1721  * @object: This is the object which is cast to a struct scic_sds_port object.
1722  *
1723  * This method will perform the actions required by the struct scic_sds_port on
1724  * entering the SCIC_SDS_PORT_READY_SUBSTATE_WAITING. This function checks the
1725  * port for any ready phys.  If there is at least one phy in a ready state then
1726  * the port transitions to the ready operational substate. none
1727  */
1728 static void scic_sds_port_ready_substate_waiting_enter(void *object)
1729 {
1730         struct scic_sds_port *sci_port = object;
1731
1732         scic_sds_port_set_ready_state_handlers(
1733                 sci_port, SCIC_SDS_PORT_READY_SUBSTATE_WAITING
1734                 );
1735
1736         scic_sds_port_suspend_port_task_scheduler(sci_port);
1737
1738         sci_port->not_ready_reason = SCIC_PORT_NOT_READY_NO_ACTIVE_PHYS;
1739
1740         if (sci_port->active_phy_mask != 0) {
1741                 /* At least one of the phys on the port is ready */
1742                 sci_base_state_machine_change_state(
1743                         &sci_port->ready_substate_machine,
1744                         SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL
1745                         );
1746         }
1747 }
1748
1749 /**
1750  *
1751  * @object: This is the object which is cast to a struct scic_sds_port object.
1752  *
1753  * This function will perform the actions required by the struct scic_sds_port
1754  * on entering the SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL. This function sets
1755  * the state handlers for the port object, notifies the SCI User that the port
1756  * is ready, and resumes port operations. none
1757  */
1758 static void scic_sds_port_ready_substate_operational_enter(void *object)
1759 {
1760         u32 index;
1761         struct scic_sds_port *sci_port = object;
1762         struct scic_sds_controller *scic = sci_port->owning_controller;
1763         struct isci_host *ihost = scic_to_ihost(scic);
1764         struct isci_port *iport = sci_port_to_iport(sci_port);
1765
1766         scic_sds_port_set_ready_state_handlers(
1767                         sci_port,
1768                         SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL);
1769
1770         isci_port_ready(ihost, iport);
1771
1772         for (index = 0; index < SCI_MAX_PHYS; index++) {
1773                 if (sci_port->phy_table[index]) {
1774                         writel(sci_port->physical_port_index,
1775                                 &sci_port->port_pe_configuration_register[
1776                                         sci_port->phy_table[index]->phy_index]);
1777                 }
1778         }
1779
1780         scic_sds_port_update_viit_entry(sci_port);
1781
1782         scic_sds_port_resume_port_task_scheduler(sci_port);
1783
1784         /*
1785          * Post the dummy task for the port so the hardware can schedule
1786          * io correctly
1787          */
1788         scic_sds_port_post_dummy_request(sci_port);
1789 }
1790
1791 /**
1792  *
1793  * @object: This is the object which is cast to a struct scic_sds_port object.
1794  *
1795  * This method will perform the actions required by the struct scic_sds_port on
1796  * exiting the SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL. This function reports
1797  * the port not ready and suspends the port task scheduler. none
1798  */
1799 static void scic_sds_port_ready_substate_operational_exit(void *object)
1800 {
1801         struct scic_sds_port *sci_port = object;
1802         struct scic_sds_controller *scic = sci_port->owning_controller;
1803         struct isci_host *ihost = scic_to_ihost(scic);
1804         struct isci_port *iport = sci_port_to_iport(sci_port);
1805
1806         /*
1807          * Kill the dummy task for this port if it has not yet posted
1808          * the hardware will treat this as a NOP and just return abort
1809          * complete.
1810          */
1811         scic_sds_port_abort_dummy_request(sci_port);
1812
1813         isci_port_not_ready(ihost, iport);
1814 }
1815
1816 /*
1817  * ******************************************************************************
1818  * *  PORT READY CONFIGURING METHODS
1819  * ****************************************************************************** */
1820
1821 /**
1822  * scic_sds_port_ready_substate_configuring_enter() -
1823  * @object: This is the object which is cast to a struct scic_sds_port object.
1824  *
1825  * This method will perform the actions required by the struct scic_sds_port on
1826  * exiting the SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL. This function reports
1827  * the port not ready and suspends the port task scheduler. none
1828  */
1829 static void scic_sds_port_ready_substate_configuring_enter(void *object)
1830 {
1831         struct scic_sds_port *sci_port = object;
1832         struct scic_sds_controller *scic = sci_port->owning_controller;
1833         struct isci_host *ihost = scic_to_ihost(scic);
1834         struct isci_port *iport = sci_port_to_iport(sci_port);
1835
1836         scic_sds_port_set_ready_state_handlers(
1837                         sci_port,
1838                         SCIC_SDS_PORT_READY_SUBSTATE_CONFIGURING);
1839
1840         if (sci_port->active_phy_mask == 0) {
1841                 isci_port_not_ready(ihost, iport);
1842
1843                 sci_base_state_machine_change_state(
1844                                 &sci_port->ready_substate_machine,
1845                                 SCIC_SDS_PORT_READY_SUBSTATE_WAITING);
1846         } else if (sci_port->started_request_count == 0)
1847                 sci_base_state_machine_change_state(
1848                                 &sci_port->ready_substate_machine,
1849                                 SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL);
1850 }
1851
1852 static void scic_sds_port_ready_substate_configuring_exit(void *object)
1853 {
1854         struct scic_sds_port *sci_port = object;
1855
1856         scic_sds_port_suspend_port_task_scheduler(sci_port);
1857 }
1858
1859 /* --------------------------------------------------------------------------- */
1860
1861 static const struct sci_base_state scic_sds_port_ready_substate_table[] = {
1862         [SCIC_SDS_PORT_READY_SUBSTATE_WAITING] = {
1863                 .enter_state = scic_sds_port_ready_substate_waiting_enter,
1864         },
1865         [SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL] = {
1866                 .enter_state = scic_sds_port_ready_substate_operational_enter,
1867                 .exit_state  = scic_sds_port_ready_substate_operational_exit
1868         },
1869         [SCIC_SDS_PORT_READY_SUBSTATE_CONFIGURING] = {
1870                 .enter_state = scic_sds_port_ready_substate_configuring_enter,
1871                 .exit_state  = scic_sds_port_ready_substate_configuring_exit
1872         },
1873 };
1874
1875 /**
1876  *
1877  * @port: This is the struct scic_sds_port object on which the io request count will
1878  *    be decremented.
1879  * @device: This is the struct scic_sds_remote_device object to which the io request
1880  *    is being directed.  This parameter is not required to complete this
1881  *    operation.
1882  * @io_request: This is the request that is being completed on this port
1883  *    object.  This parameter is not required to complete this operation.
1884  *
1885  * This is a general complete io request handler for the struct scic_sds_port object.
1886  * enum sci_status SCI_SUCCESS
1887  */
1888 static enum sci_status scic_sds_port_general_complete_io_handler(
1889         struct scic_sds_port *port,
1890         struct scic_sds_remote_device *device,
1891         struct scic_sds_request *io_request)
1892 {
1893         scic_sds_port_decrement_request_count(port);
1894
1895         return SCI_SUCCESS;
1896 }
1897
1898 /**
1899  * scic_sds_port_stopped_state_start_handler() - stop a port from "started"
1900  *
1901  * @port: This is the struct scic_sds_port object which is cast into a
1902  * struct scic_sds_port object.
1903  *
1904  * This function takes the struct scic_sds_port from a stopped state and
1905  * attempts to start it.  To start a port it must have no assiged devices and
1906  * it must have at least one phy assigned to it.  If those conditions are
1907  * met then the port can transition to the ready state.
1908  * enum sci_status
1909  * SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION
1910  * This struct scic_sds_port object could not be started because the port
1911  * configuration is not valid.
1912  * SCI_SUCCESS
1913  * the start request is successful and the struct scic_sds_port object
1914  * has transitioned to the SCI_BASE_PORT_STATE_READY.
1915  */
1916 static enum sci_status
1917 scic_sds_port_stopped_state_start_handler(struct scic_sds_port *sci_port)
1918 {
1919         struct scic_sds_controller *scic = sci_port->owning_controller;
1920         struct isci_host *ihost = scic_to_ihost(scic);
1921         enum sci_status status = SCI_SUCCESS;
1922         u32 phy_mask;
1923
1924         if (sci_port->assigned_device_count > 0) {
1925                 /*
1926                  * @todo This is a start failure operation because
1927                  * there are still devices assigned to this port.
1928                  * There must be no devices assigned to a port on a
1929                  * start operation.
1930                  */
1931                 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
1932         }
1933
1934         sci_port->timer_handle =
1935                 isci_timer_create(ihost,
1936                                   sci_port,
1937                                   scic_sds_port_timeout_handler);
1938
1939         if (!sci_port->timer_handle)
1940                 return SCI_FAILURE_INSUFFICIENT_RESOURCES;
1941
1942         if (sci_port->reserved_rni == SCU_DUMMY_INDEX) {
1943                 u16 rni = scic_sds_remote_node_table_allocate_remote_node(
1944                                 &scic->available_remote_nodes, 1);
1945
1946                 if (rni != SCU_DUMMY_INDEX)
1947                         scic_sds_port_construct_dummy_rnc(sci_port, rni);
1948                 else
1949                         status = SCI_FAILURE_INSUFFICIENT_RESOURCES;
1950                 sci_port->reserved_rni = rni;
1951         }
1952
1953         if (sci_port->reserved_tci == SCU_DUMMY_INDEX) {
1954                 /* Allocate a TCI and remove the sequence nibble */
1955                 u16 tci = scic_controller_allocate_io_tag(scic);
1956
1957                 if (tci != SCU_DUMMY_INDEX)
1958                         scic_sds_port_construct_dummy_task(sci_port, tci);
1959                 else
1960                         status = SCI_FAILURE_INSUFFICIENT_RESOURCES;
1961                 sci_port->reserved_tci = tci;
1962         }
1963
1964         if (status == SCI_SUCCESS) {
1965                 phy_mask = scic_sds_port_get_phys(sci_port);
1966
1967                 /*
1968                  * There are one or more phys assigned to this port.  Make sure
1969                  * the port's phy mask is in fact legal and supported by the
1970                  * silicon.
1971                  */
1972                 if (scic_sds_port_is_phy_mask_valid(sci_port, phy_mask) == true) {
1973                         sci_base_state_machine_change_state(
1974                                 &sci_port->state_machine,
1975                                 SCI_BASE_PORT_STATE_READY);
1976
1977                         return SCI_SUCCESS;
1978                 } else
1979                         status = SCI_FAILURE;
1980         }
1981
1982         if (status != SCI_SUCCESS)
1983                 scic_sds_port_destroy_dummy_resources(sci_port);
1984
1985         return status;
1986 }
1987
1988 /*
1989  * This method takes the struct scic_sds_port that is in a stopped state and handles a
1990  * stop request.  This function takes no action. enum sci_status SCI_SUCCESS the
1991  * stop request is successful as the struct scic_sds_port object is already stopped.
1992  */
1993 static enum sci_status scic_sds_port_stopped_state_stop_handler(
1994         struct scic_sds_port *port)
1995 {
1996         /* We are already stopped so there is nothing to do here */
1997         return SCI_SUCCESS;
1998 }
1999
2000 /*
2001  * This method takes the struct scic_sds_port that is in a stopped state and handles
2002  * the destruct request.  The stopped state is the only state in which the
2003  * struct scic_sds_port can be destroyed.  This function causes the port object to
2004  * transition to the SCI_BASE_PORT_STATE_FINAL. enum sci_status SCI_SUCCESS
2005  */
2006 static enum sci_status scic_sds_port_stopped_state_destruct_handler(
2007         struct scic_sds_port *port)
2008 {
2009         sci_base_state_machine_stop(&port->state_machine);
2010
2011         return SCI_SUCCESS;
2012 }
2013
2014 /*
2015  * This method takes the struct scic_sds_port that is in a stopped state and handles
2016  * the add phy request.  In MPC mode the only time a phy can be added to a port
2017  * is in the SCI_BASE_PORT_STATE_STOPPED. enum sci_status
2018  * SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION is returned when the phy can not
2019  * be added to the port. SCI_SUCCESS if the phy is added to the port.
2020  */
2021 static enum sci_status scic_sds_port_stopped_state_add_phy_handler(
2022         struct scic_sds_port *port,
2023         struct scic_sds_phy *phy)
2024 {
2025         struct sci_sas_address port_sas_address;
2026
2027         /* Read the port assigned SAS Address if there is one */
2028         scic_sds_port_get_sas_address(port, &port_sas_address);
2029
2030         if (port_sas_address.high != 0 && port_sas_address.low != 0) {
2031                 struct sci_sas_address phy_sas_address;
2032
2033                 /*
2034                  * Make sure that the PHY SAS Address matches the SAS Address
2035                  * for this port. */
2036                 scic_sds_phy_get_sas_address(phy, &phy_sas_address);
2037
2038                 if (
2039                         (port_sas_address.high != phy_sas_address.high)
2040                         || (port_sas_address.low  != phy_sas_address.low)
2041                         ) {
2042                         return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
2043                 }
2044         }
2045
2046         return scic_sds_port_set_phy(port, phy);
2047 }
2048
2049 /*
2050  * This method takes the struct scic_sds_port that is in a stopped state and handles
2051  * the remove phy request.  In MPC mode the only time a phy can be removed from
2052  * a port is in the SCI_BASE_PORT_STATE_STOPPED. enum sci_status
2053  * SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION is returned when the phy can not
2054  * be added to the port. SCI_SUCCESS if the phy is added to the port.
2055  */
2056 static enum sci_status scic_sds_port_stopped_state_remove_phy_handler(
2057         struct scic_sds_port *port,
2058         struct scic_sds_phy *phy)
2059 {
2060         return scic_sds_port_clear_phy(port, phy);
2061 }
2062
2063 /*
2064  * ****************************************************************************
2065  * *  READY STATE HANDLERS
2066  * **************************************************************************** */
2067
2068 /*
2069  * ****************************************************************************
2070  * *  RESETTING STATE HANDLERS
2071  * **************************************************************************** */
2072
2073 /*
2074  * ****************************************************************************
2075  * *  STOPPING STATE HANDLERS
2076  * **************************************************************************** */
2077
2078 /*
2079  * This method takes the struct scic_sds_port that is in a stopping state and handles
2080  * the complete io request. Should the request count reach 0 then the port
2081  * object will transition to the stopped state. enum sci_status SCI_SUCCESS
2082  */
2083 static enum sci_status scic_sds_port_stopping_state_complete_io_handler(
2084         struct scic_sds_port *sci_port,
2085         struct scic_sds_remote_device *device,
2086         struct scic_sds_request *io_request)
2087 {
2088         scic_sds_port_decrement_request_count(sci_port);
2089
2090         if (sci_port->started_request_count == 0) {
2091                 sci_base_state_machine_change_state(&sci_port->state_machine,
2092                                                     SCI_BASE_PORT_STATE_STOPPED);
2093         }
2094
2095         return SCI_SUCCESS;
2096 }
2097
2098 /*
2099  * ****************************************************************************
2100  * *  RESETTING STATE HANDLERS
2101  * **************************************************************************** */
2102
2103 /**
2104  *
2105  * @port: This is the port object which is being requested to stop.
2106  *
2107  * This method will stop a failed port.  This causes a transition to the
2108  * stopping state. enum sci_status SCI_SUCCESS
2109  */
2110 static enum sci_status scic_sds_port_reset_state_stop_handler(
2111         struct scic_sds_port *port)
2112 {
2113         sci_base_state_machine_change_state(
2114                 &port->state_machine,
2115                 SCI_BASE_PORT_STATE_STOPPING
2116                 );
2117
2118         return SCI_SUCCESS;
2119 }
2120
2121 /*
2122  * This method will transition a failed port to its ready state.  The port
2123  * failed because a hard reset request timed out but at some time later one or
2124  * more phys in the port became ready. enum sci_status SCI_SUCCESS
2125  */
2126 static void scic_sds_port_reset_state_link_up_handler(
2127         struct scic_sds_port *port,
2128         struct scic_sds_phy *phy)
2129 {
2130         /*
2131          * / @todo We should make sure that the phy that has gone link up is the same
2132          * /       one on which we sent the reset.  It is possible that the phy on
2133          * /       which we sent the reset is not the one that has gone link up and we
2134          * /       want to make sure that phy being reset comes back.  Consider the
2135          * /       case where a reset is sent but before the hardware processes the
2136          * /       reset it get a link up on the port because of a hot plug event.
2137          * /       because of the reset request this phy will go link down almost
2138          * /       immediately. */
2139
2140         /*
2141          * In the resetting state we don't notify the user regarding
2142          * link up and link down notifications. */
2143         scic_sds_port_general_link_up_handler(port, phy, false);
2144 }
2145
2146 /*
2147  * This method process link down notifications that occur during a port reset
2148  * operation. Link downs can occur during the reset operation. enum sci_status
2149  * SCI_SUCCESS
2150  */
2151 static void scic_sds_port_reset_state_link_down_handler(
2152         struct scic_sds_port *port,
2153         struct scic_sds_phy *phy)
2154 {
2155         /*
2156          * In the resetting state we don't notify the user regarding
2157          * link up and link down notifications. */
2158         scic_sds_port_deactivate_phy(port, phy, false);
2159 }
2160
2161 static struct scic_sds_port_state_handler scic_sds_port_state_handler_table[] = {
2162         [SCI_BASE_PORT_STATE_STOPPED] = {
2163                 .start_handler          = scic_sds_port_stopped_state_start_handler,
2164                 .stop_handler           = scic_sds_port_stopped_state_stop_handler,
2165                 .destruct_handler       = scic_sds_port_stopped_state_destruct_handler,
2166                 .reset_handler          = scic_sds_port_default_reset_handler,
2167                 .add_phy_handler        = scic_sds_port_stopped_state_add_phy_handler,
2168                 .remove_phy_handler     = scic_sds_port_stopped_state_remove_phy_handler,
2169                 .frame_handler          = scic_sds_port_default_frame_handler,
2170                 .event_handler          = scic_sds_port_default_event_handler,
2171                 .link_up_handler        = scic_sds_port_default_link_up_handler,
2172                 .link_down_handler      = scic_sds_port_default_link_down_handler,
2173                 .start_io_handler       = scic_sds_port_default_start_io_handler,
2174                 .complete_io_handler    = scic_sds_port_default_complete_io_handler
2175         },
2176         [SCI_BASE_PORT_STATE_STOPPING] = {
2177                 .start_handler          = scic_sds_port_default_start_handler,
2178                 .stop_handler           = scic_sds_port_default_stop_handler,
2179                 .destruct_handler       = scic_sds_port_default_destruct_handler,
2180                 .reset_handler          = scic_sds_port_default_reset_handler,
2181                 .add_phy_handler        = scic_sds_port_default_add_phy_handler,
2182                 .remove_phy_handler     = scic_sds_port_default_remove_phy_handler,
2183                 .frame_handler          = scic_sds_port_default_frame_handler,
2184                 .event_handler          = scic_sds_port_default_event_handler,
2185                 .link_up_handler        = scic_sds_port_default_link_up_handler,
2186                 .link_down_handler      = scic_sds_port_default_link_down_handler,
2187                 .start_io_handler       = scic_sds_port_default_start_io_handler,
2188                 .complete_io_handler    = scic_sds_port_stopping_state_complete_io_handler
2189         },
2190         [SCI_BASE_PORT_STATE_READY] = {
2191                 .start_handler          = scic_sds_port_default_start_handler,
2192                 .stop_handler           = scic_sds_port_default_stop_handler,
2193                 .destruct_handler       = scic_sds_port_default_destruct_handler,
2194                 .reset_handler          = scic_sds_port_default_reset_handler,
2195                 .add_phy_handler        = scic_sds_port_default_add_phy_handler,
2196                 .remove_phy_handler     = scic_sds_port_default_remove_phy_handler,
2197                 .frame_handler          = scic_sds_port_default_frame_handler,
2198                 .event_handler          = scic_sds_port_default_event_handler,
2199                 .link_up_handler        = scic_sds_port_default_link_up_handler,
2200                 .link_down_handler      = scic_sds_port_default_link_down_handler,
2201                 .start_io_handler       = scic_sds_port_default_start_io_handler,
2202                 .complete_io_handler    = scic_sds_port_general_complete_io_handler
2203         },
2204         [SCI_BASE_PORT_STATE_RESETTING] = {
2205                 .start_handler          = scic_sds_port_default_start_handler,
2206                 .stop_handler           = scic_sds_port_reset_state_stop_handler,
2207                 .destruct_handler       = scic_sds_port_default_destruct_handler,
2208                 .reset_handler          = scic_sds_port_default_reset_handler,
2209                 .add_phy_handler        = scic_sds_port_default_add_phy_handler,
2210                 .remove_phy_handler     = scic_sds_port_default_remove_phy_handler,
2211                 .frame_handler          = scic_sds_port_default_frame_handler,
2212                 .event_handler          = scic_sds_port_default_event_handler,
2213                 .link_up_handler        = scic_sds_port_reset_state_link_up_handler,
2214                 .link_down_handler      = scic_sds_port_reset_state_link_down_handler,
2215                 .start_io_handler       = scic_sds_port_default_start_io_handler,
2216                 .complete_io_handler    = scic_sds_port_general_complete_io_handler
2217         },
2218         [SCI_BASE_PORT_STATE_FAILED] = {
2219                 .start_handler          = scic_sds_port_default_start_handler,
2220                 .stop_handler           = scic_sds_port_default_stop_handler,
2221                 .destruct_handler       = scic_sds_port_default_destruct_handler,
2222                 .reset_handler          = scic_sds_port_default_reset_handler,
2223                 .add_phy_handler        = scic_sds_port_default_add_phy_handler,
2224                 .remove_phy_handler     = scic_sds_port_default_remove_phy_handler,
2225                 .frame_handler          = scic_sds_port_default_frame_handler,
2226                 .event_handler          = scic_sds_port_default_event_handler,
2227                 .link_up_handler        = scic_sds_port_default_link_up_handler,
2228                 .link_down_handler      = scic_sds_port_default_link_down_handler,
2229                 .start_io_handler       = scic_sds_port_default_start_io_handler,
2230                 .complete_io_handler    = scic_sds_port_general_complete_io_handler
2231         }
2232 };
2233
2234 /*
2235  * ******************************************************************************
2236  * *  PORT STATE PRIVATE METHODS
2237  * ****************************************************************************** */
2238
2239 /**
2240  *
2241  * @sci_port: This is the port object which to suspend.
2242  *
2243  * This method will enable the SCU Port Task Scheduler for this port object but
2244  * will leave the port task scheduler in a suspended state. none
2245  */
2246 static void
2247 scic_sds_port_enable_port_task_scheduler(struct scic_sds_port *port)
2248 {
2249         u32 pts_control_value;
2250
2251         pts_control_value = readl(&port->port_task_scheduler_registers->control);
2252         pts_control_value |= SCU_PTSxCR_GEN_BIT(ENABLE) | SCU_PTSxCR_GEN_BIT(SUSPEND);
2253         writel(pts_control_value, &port->port_task_scheduler_registers->control);
2254 }
2255
2256 /**
2257  *
2258  * @sci_port: This is the port object which to resume.
2259  *
2260  * This method will disable the SCU port task scheduler for this port object.
2261  * none
2262  */
2263 static void
2264 scic_sds_port_disable_port_task_scheduler(struct scic_sds_port *port)
2265 {
2266         u32 pts_control_value;
2267
2268         pts_control_value = readl(&port->port_task_scheduler_registers->control);
2269         pts_control_value &=
2270                 ~(SCU_PTSxCR_GEN_BIT(ENABLE) | SCU_PTSxCR_GEN_BIT(SUSPEND));
2271         writel(pts_control_value, &port->port_task_scheduler_registers->control);
2272 }
2273
2274 static void scic_sds_port_post_dummy_remote_node(struct scic_sds_port *sci_port)
2275 {
2276         struct scic_sds_controller *scic = sci_port->owning_controller;
2277         u8 phys_index = sci_port->physical_port_index;
2278         union scu_remote_node_context *rnc;
2279         u16 rni = sci_port->reserved_rni;
2280         u32 command;
2281
2282         rnc = &scic->remote_node_context_table[rni];
2283         rnc->ssp.is_valid = true;
2284
2285         command = SCU_CONTEXT_COMMAND_POST_RNC_32 |
2286                   phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
2287
2288         scic_sds_controller_post_request(scic, command);
2289
2290         /* ensure hardware has seen the post rnc command and give it
2291          * ample time to act before sending the suspend
2292          */
2293         readl(&scic->smu_registers->interrupt_status); /* flush */
2294         udelay(10);
2295
2296         command = SCU_CONTEXT_COMMAND_POST_RNC_SUSPEND_TX_RX |
2297                   phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
2298
2299         scic_sds_controller_post_request(scic, command);
2300 }
2301
2302 static void scic_sds_port_invalidate_dummy_remote_node(struct scic_sds_port *sci_port)
2303 {
2304         struct scic_sds_controller *scic = sci_port->owning_controller;
2305         u8 phys_index = sci_port->physical_port_index;
2306         union scu_remote_node_context *rnc;
2307         u16 rni = sci_port->reserved_rni;
2308         u32 command;
2309
2310         rnc = &scic->remote_node_context_table[rni];
2311
2312         rnc->ssp.is_valid = false;
2313
2314         /* ensure the preceding tc abort request has reached the
2315          * controller and give it ample time to act before posting the rnc
2316          * invalidate
2317          */
2318         readl(&scic->smu_registers->interrupt_status); /* flush */
2319         udelay(10);
2320
2321         command = SCU_CONTEXT_COMMAND_POST_RNC_INVALIDATE |
2322                   phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
2323
2324         scic_sds_controller_post_request(scic, command);
2325 }
2326
2327 /*
2328  * ******************************************************************************
2329  * *  PORT STATE METHODS
2330  * ****************************************************************************** */
2331
2332 /**
2333  *
2334  * @object: This is the object which is cast to a struct scic_sds_port object.
2335  *
2336  * This method will perform the actions required by the struct scic_sds_port on
2337  * entering the SCI_BASE_PORT_STATE_STOPPED. This function sets the stopped
2338  * state handlers for the struct scic_sds_port object and disables the port task
2339  * scheduler in the hardware. none
2340  */
2341 static void scic_sds_port_stopped_state_enter(void *object)
2342 {
2343         struct scic_sds_port *sci_port = object;
2344
2345         scic_sds_port_set_base_state_handlers(
2346                 sci_port, SCI_BASE_PORT_STATE_STOPPED
2347                 );
2348
2349         if (
2350                 SCI_BASE_PORT_STATE_STOPPING
2351                 == sci_port->state_machine.previous_state_id
2352                 ) {
2353                 /*
2354                  * If we enter this state becasuse of a request to stop
2355                  * the port then we want to disable the hardwares port
2356                  * task scheduler. */
2357                 scic_sds_port_disable_port_task_scheduler(sci_port);
2358         }
2359 }
2360
2361 /**
2362  *
2363  * @object: This is the object which is cast to a struct scic_sds_port object.
2364  *
2365  * This method will perform the actions required by the struct scic_sds_port on
2366  * exiting the SCI_BASE_STATE_STOPPED. This function enables the SCU hardware
2367  * port task scheduler. none
2368  */
2369 static void scic_sds_port_stopped_state_exit(void *object)
2370 {
2371         struct scic_sds_port *sci_port = object;
2372
2373         /* Enable and suspend the port task scheduler */
2374         scic_sds_port_enable_port_task_scheduler(sci_port);
2375 }
2376
2377 /**
2378  * scic_sds_port_ready_state_enter -
2379  * @object: This is the object which is cast to a struct scic_sds_port object.
2380  *
2381  * This method will perform the actions required by the struct scic_sds_port on
2382  * entering the SCI_BASE_PORT_STATE_READY. This function sets the ready state
2383  * handlers for the struct scic_sds_port object, reports the port object as
2384  * not ready and starts the ready substate machine. none
2385  */
2386 static void scic_sds_port_ready_state_enter(void *object)
2387 {
2388         struct scic_sds_port *sci_port = object;
2389         struct scic_sds_controller *scic = sci_port->owning_controller;
2390         struct isci_host *ihost = scic_to_ihost(scic);
2391         struct isci_port *iport = sci_port_to_iport(sci_port);
2392         u32 prev_state;
2393
2394         /* Put the ready state handlers in place though they will not be there long */
2395         scic_sds_port_set_base_state_handlers(sci_port, SCI_BASE_PORT_STATE_READY);
2396
2397         prev_state = sci_port->state_machine.previous_state_id;
2398         if (prev_state  == SCI_BASE_PORT_STATE_RESETTING)
2399                 isci_port_hard_reset_complete(iport, SCI_SUCCESS);
2400         else
2401                 isci_port_not_ready(ihost, iport);
2402
2403         /* Post and suspend the dummy remote node context for this port. */
2404         scic_sds_port_post_dummy_remote_node(sci_port);
2405
2406         /* Start the ready substate machine */
2407         sci_base_state_machine_start(&sci_port->ready_substate_machine);
2408 }
2409
2410 static void scic_sds_port_ready_state_exit(void *object)
2411 {
2412         struct scic_sds_port *sci_port = object;
2413
2414         sci_base_state_machine_stop(&sci_port->ready_substate_machine);
2415         scic_sds_port_invalidate_dummy_remote_node(sci_port);
2416 }
2417
2418 /**
2419  *
2420  * @object: This is the object which is cast to a struct scic_sds_port object.
2421  *
2422  * This method will perform the actions required by the struct scic_sds_port on
2423  * entering the SCI_BASE_PORT_STATE_RESETTING. This function sets the resetting
2424  * state handlers for the struct scic_sds_port object. none
2425  */
2426 static void scic_sds_port_resetting_state_enter(void *object)
2427 {
2428         struct scic_sds_port *sci_port = object;
2429
2430         scic_sds_port_set_base_state_handlers(
2431                 sci_port, SCI_BASE_PORT_STATE_RESETTING
2432                 );
2433 }
2434
2435 /**
2436  *
2437  * @object: This is the object which is cast to a struct scic_sds_port object.
2438  *
2439  * This function will perform the actions required by the
2440  * struct scic_sds_port on
2441  * exiting the SCI_BASE_STATE_RESETTING. This function does nothing. none
2442  */
2443 static inline void scic_sds_port_resetting_state_exit(void *object)
2444 {
2445         struct scic_sds_port *sci_port = object;
2446
2447         isci_timer_stop(sci_port->timer_handle);
2448 }
2449
2450 /**
2451  *
2452  * @object: This is the void object which is cast to a
2453  * struct scic_sds_port object.
2454  *
2455  * This method will perform the actions required by the struct scic_sds_port on
2456  * entering the SCI_BASE_PORT_STATE_STOPPING. This function sets the stopping
2457  * state handlers for the struct scic_sds_port object. none
2458  */
2459 static void scic_sds_port_stopping_state_enter(void *object)
2460 {
2461         struct scic_sds_port *sci_port = object;
2462
2463         scic_sds_port_set_base_state_handlers(
2464                 sci_port, SCI_BASE_PORT_STATE_STOPPING
2465                 );
2466 }
2467
2468 /**
2469  *
2470  * @object: This is the object which is cast to a struct scic_sds_port object.
2471  *
2472  * This function will perform the actions required by the
2473  * struct scic_sds_port on
2474  * exiting the SCI_BASE_STATE_STOPPING. This function does nothing. none
2475  */
2476 static inline void
2477 scic_sds_port_stopping_state_exit(void *object)
2478 {
2479         struct scic_sds_port *sci_port = object;
2480
2481         isci_timer_stop(sci_port->timer_handle);
2482
2483         scic_sds_port_destroy_dummy_resources(sci_port);
2484 }
2485
2486 /**
2487  *
2488  * @object: This is the object which is cast to a struct scic_sds_port object.
2489  *
2490  * This function will perform the actions required by the
2491  * struct scic_sds_port on
2492  * entering the SCI_BASE_PORT_STATE_STOPPING. This function sets the stopping
2493  * state handlers for the struct scic_sds_port object. none
2494  */
2495 static void scic_sds_port_failed_state_enter(void *object)
2496 {
2497         struct scic_sds_port *sci_port = object;
2498         struct isci_port *iport = sci_port_to_iport(sci_port);
2499
2500         scic_sds_port_set_base_state_handlers(sci_port,
2501                                               SCI_BASE_PORT_STATE_FAILED);
2502
2503         isci_port_hard_reset_complete(iport, SCI_FAILURE_TIMEOUT);
2504 }
2505
2506 /* --------------------------------------------------------------------------- */
2507
2508 static const struct sci_base_state scic_sds_port_state_table[] = {
2509         [SCI_BASE_PORT_STATE_STOPPED] = {
2510                 .enter_state = scic_sds_port_stopped_state_enter,
2511                 .exit_state  = scic_sds_port_stopped_state_exit
2512         },
2513         [SCI_BASE_PORT_STATE_STOPPING] = {
2514                 .enter_state = scic_sds_port_stopping_state_enter,
2515                 .exit_state  = scic_sds_port_stopping_state_exit
2516         },
2517         [SCI_BASE_PORT_STATE_READY] = {
2518                 .enter_state = scic_sds_port_ready_state_enter,
2519                 .exit_state  = scic_sds_port_ready_state_exit
2520         },
2521         [SCI_BASE_PORT_STATE_RESETTING] = {
2522                 .enter_state = scic_sds_port_resetting_state_enter,
2523                 .exit_state  = scic_sds_port_resetting_state_exit
2524         },
2525         [SCI_BASE_PORT_STATE_FAILED] = {
2526                 .enter_state = scic_sds_port_failed_state_enter,
2527         }
2528 };
2529
2530 void scic_sds_port_construct(struct scic_sds_port *sci_port, u8 index,
2531                              struct scic_sds_controller *scic)
2532 {
2533         sci_base_state_machine_construct(&sci_port->state_machine,
2534                                          sci_port,
2535                                          scic_sds_port_state_table,
2536                                          SCI_BASE_PORT_STATE_STOPPED);
2537
2538         sci_base_state_machine_start(&sci_port->state_machine);
2539
2540         sci_base_state_machine_construct(&sci_port->ready_substate_machine,
2541                                          sci_port,
2542                                          scic_sds_port_ready_substate_table,
2543                                          SCIC_SDS_PORT_READY_SUBSTATE_WAITING);
2544
2545         sci_port->logical_port_index  = SCIC_SDS_DUMMY_PORT;
2546         sci_port->physical_port_index = index;
2547         sci_port->active_phy_mask     = 0;
2548
2549         sci_port->owning_controller = scic;
2550
2551         sci_port->started_request_count = 0;
2552         sci_port->assigned_device_count = 0;
2553
2554         sci_port->reserved_rni = SCU_DUMMY_INDEX;
2555         sci_port->reserved_tci = SCU_DUMMY_INDEX;
2556
2557         sci_port->timer_handle = NULL;
2558         sci_port->port_task_scheduler_registers = NULL;
2559
2560         for (index = 0; index < SCI_MAX_PHYS; index++)
2561                 sci_port->phy_table[index] = NULL;
2562 }
2563
2564 void isci_port_init(struct isci_port *iport, struct isci_host *ihost, int index)
2565 {
2566         INIT_LIST_HEAD(&iport->remote_dev_list);
2567         INIT_LIST_HEAD(&iport->domain_dev_list);
2568         spin_lock_init(&iport->state_lock);
2569         init_completion(&iport->start_complete);
2570         iport->isci_host = ihost;
2571         isci_port_change_state(iport, isci_freed);
2572 }
2573
2574 /**
2575  * isci_port_get_state() - This function gets the status of the port object.
2576  * @isci_port: This parameter points to the isci_port object
2577  *
2578  * status of the object as a isci_status enum.
2579  */
2580 enum isci_status isci_port_get_state(
2581         struct isci_port *isci_port)
2582 {
2583         return isci_port->status;
2584 }
2585
2586 static void isci_port_bc_change_received(struct isci_host *ihost,
2587                                          struct scic_sds_port *sci_port,
2588                                          struct scic_sds_phy *sci_phy)
2589 {
2590         struct isci_phy *iphy = sci_phy_to_iphy(sci_phy);
2591
2592         dev_dbg(&ihost->pdev->dev, "%s: iphy = %p, sas_phy = %p\n",
2593                 __func__, iphy, &iphy->sas_phy);
2594
2595         ihost->sas_ha.notify_port_event(&iphy->sas_phy, PORTE_BROADCAST_RCVD);
2596         scic_port_enable_broadcast_change_notification(sci_port);
2597 }
2598
2599 void scic_sds_port_broadcast_change_received(
2600         struct scic_sds_port *sci_port,
2601         struct scic_sds_phy *sci_phy)
2602 {
2603         struct scic_sds_controller *scic = sci_port->owning_controller;
2604         struct isci_host *ihost = scic_to_ihost(scic);
2605
2606         /* notify the user. */
2607         isci_port_bc_change_received(ihost, sci_port, sci_phy);
2608 }
2609
2610 int isci_port_perform_hard_reset(struct isci_host *ihost, struct isci_port *iport,
2611                                  struct isci_phy *iphy)
2612 {
2613         unsigned long flags;
2614         enum sci_status status;
2615         int ret = TMF_RESP_FUNC_COMPLETE;
2616
2617         dev_dbg(&ihost->pdev->dev, "%s: iport = %p\n",
2618                 __func__, iport);
2619
2620         init_completion(&iport->hard_reset_complete);
2621
2622         spin_lock_irqsave(&ihost->scic_lock, flags);
2623
2624         #define ISCI_PORT_RESET_TIMEOUT SCIC_SDS_SIGNATURE_FIS_TIMEOUT
2625         status = scic_port_hard_reset(&iport->sci, ISCI_PORT_RESET_TIMEOUT);
2626
2627         spin_unlock_irqrestore(&ihost->scic_lock, flags);
2628
2629         if (status == SCI_SUCCESS) {
2630                 wait_for_completion(&iport->hard_reset_complete);
2631
2632                 dev_dbg(&ihost->pdev->dev,
2633                         "%s: iport = %p; hard reset completion\n",
2634                         __func__, iport);
2635
2636                 if (iport->hard_reset_status != SCI_SUCCESS)
2637                         ret = TMF_RESP_FUNC_FAILED;
2638         } else {
2639                 ret = TMF_RESP_FUNC_FAILED;
2640
2641                 dev_err(&ihost->pdev->dev,
2642                         "%s: iport = %p; scic_port_hard_reset call"
2643                         " failed 0x%x\n",
2644                         __func__, iport, status);
2645
2646         }
2647
2648         /* If the hard reset for the port has failed, consider this
2649          * the same as link failures on all phys in the port.
2650          */
2651         if (ret != TMF_RESP_FUNC_COMPLETE) {
2652                 dev_err(&ihost->pdev->dev,
2653                         "%s: iport = %p; hard reset failed "
2654                         "(0x%x) - sending link down to libsas for phy %p\n",
2655                         __func__, iport, iport->hard_reset_status, iphy);
2656
2657                 isci_port_link_down(ihost, iphy, iport);
2658         }
2659
2660         return ret;
2661 }
2662
2663 /**
2664  * isci_port_deformed() - This function is called by libsas when a port becomes
2665  *    inactive.
2666  * @phy: This parameter specifies the libsas phy with the inactive port.
2667  *
2668  */
2669 void isci_port_deformed(struct asd_sas_phy *phy)
2670 {
2671         pr_debug("%s: sas_phy = %p\n", __func__, phy);
2672 }
2673
2674 /**
2675  * isci_port_formed() - This function is called by libsas when a port becomes
2676  *    active.
2677  * @phy: This parameter specifies the libsas phy with the active port.
2678  *
2679  */
2680 void isci_port_formed(struct asd_sas_phy *phy)
2681 {
2682         pr_debug("%s: sas_phy = %p, sas_port = %p\n", __func__, phy, phy->port);
2683 }