isci: unify port stop 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 struct scic_sds_port_state_handler scic_sds_port_state_handler_table[];
65
66 static void isci_port_change_state(struct isci_port *iport, enum isci_status status)
67 {
68         unsigned long flags;
69
70         dev_dbg(&iport->isci_host->pdev->dev,
71                 "%s: iport = %p, state = 0x%x\n",
72                 __func__, iport, status);
73
74         /* XXX pointless lock */
75         spin_lock_irqsave(&iport->state_lock, flags);
76         iport->status = status;
77         spin_unlock_irqrestore(&iport->state_lock, flags);
78 }
79
80 /*
81  * This function will indicate which protocols are supported by this port.
82  * @sci_port: a handle corresponding to the SAS port for which to return the
83  *    supported protocols.
84  * @protocols: This parameter specifies a pointer to a data structure
85  *    which the core will copy the protocol values for the port from the
86  *    transmit_identification register.
87  */
88 static void
89 scic_sds_port_get_protocols(struct scic_sds_port *sci_port,
90                             struct scic_phy_proto *protocols)
91 {
92         u8 index;
93
94         protocols->all = 0;
95
96         for (index = 0; index < SCI_MAX_PHYS; index++) {
97                 if (sci_port->phy_table[index] != NULL) {
98                         scic_sds_phy_get_protocols(sci_port->phy_table[index],
99                                                    protocols);
100                 }
101         }
102 }
103
104 /**
105  * This method requests a list (mask) of the phys contained in the supplied SAS
106  *    port.
107  * @sci_port: a handle corresponding to the SAS port for which to return the
108  *    phy mask.
109  *
110  * Return a bit mask indicating which phys are a part of this port. Each bit
111  * corresponds to a phy identifier (e.g. bit 0 = phy id 0).
112  */
113 static u32 scic_sds_port_get_phys(struct scic_sds_port *sci_port)
114 {
115         u32 index;
116         u32 mask;
117
118         mask = 0;
119
120         for (index = 0; index < SCI_MAX_PHYS; index++) {
121                 if (sci_port->phy_table[index] != NULL) {
122                         mask |= (1 << index);
123                 }
124         }
125
126         return mask;
127 }
128
129 /**
130  * scic_port_get_properties() - This method simply returns the properties
131  *    regarding the port, such as: physical index, protocols, sas address, etc.
132  * @port: this parameter specifies the port for which to retrieve the physical
133  *    index.
134  * @properties: This parameter specifies the properties structure into which to
135  *    copy the requested information.
136  *
137  * Indicate if the user specified a valid port. SCI_SUCCESS This value is
138  * returned if the specified port was valid. SCI_FAILURE_INVALID_PORT This
139  * value is returned if the specified port is not valid.  When this value is
140  * returned, no data is copied to the properties output parameter.
141  */
142 static enum sci_status scic_port_get_properties(struct scic_sds_port *port,
143                                                 struct scic_port_properties *prop)
144 {
145         if ((port == NULL) ||
146             (port->logical_port_index == SCIC_SDS_DUMMY_PORT))
147                 return SCI_FAILURE_INVALID_PORT;
148
149         prop->index    = port->logical_port_index;
150         prop->phy_mask = scic_sds_port_get_phys(port);
151         scic_sds_port_get_sas_address(port, &prop->local.sas_address);
152         scic_sds_port_get_protocols(port, &prop->local.protocols);
153         scic_sds_port_get_attached_sas_address(port, &prop->remote.sas_address);
154
155         return SCI_SUCCESS;
156 }
157
158 static void isci_port_link_up(struct isci_host *isci_host,
159                               struct scic_sds_port *port,
160                               struct scic_sds_phy *phy)
161 {
162         unsigned long flags;
163         struct scic_port_properties properties;
164         struct isci_phy *isci_phy = sci_phy_to_iphy(phy);
165         struct isci_port *isci_port = sci_port_to_iport(port);
166         unsigned long success = true;
167
168         BUG_ON(isci_phy->isci_port != NULL);
169
170         isci_phy->isci_port = isci_port;
171
172         dev_dbg(&isci_host->pdev->dev,
173                 "%s: isci_port = %p\n",
174                 __func__, isci_port);
175
176         spin_lock_irqsave(&isci_phy->sas_phy.frame_rcvd_lock, flags);
177
178         isci_port_change_state(isci_phy->isci_port, isci_starting);
179
180         scic_port_get_properties(port, &properties);
181
182         if (phy->protocol == SCIC_SDS_PHY_PROTOCOL_SATA) {
183                 u64 attached_sas_address;
184
185                 isci_phy->sas_phy.oob_mode = SATA_OOB_MODE;
186                 isci_phy->sas_phy.frame_rcvd_size = sizeof(struct dev_to_host_fis);
187
188                 /*
189                  * For direct-attached SATA devices, the SCI core will
190                  * automagically assign a SAS address to the end device
191                  * for the purpose of creating a port. This SAS address
192                  * will not be the same as assigned to the PHY and needs
193                  * to be obtained from struct scic_port_properties properties.
194                  */
195                 attached_sas_address = properties.remote.sas_address.high;
196                 attached_sas_address <<= 32;
197                 attached_sas_address |= properties.remote.sas_address.low;
198                 swab64s(&attached_sas_address);
199
200                 memcpy(&isci_phy->sas_phy.attached_sas_addr,
201                        &attached_sas_address, sizeof(attached_sas_address));
202         } else if (phy->protocol == SCIC_SDS_PHY_PROTOCOL_SAS) {
203                 isci_phy->sas_phy.oob_mode = SAS_OOB_MODE;
204                 isci_phy->sas_phy.frame_rcvd_size = sizeof(struct sas_identify_frame);
205
206                 /* Copy the attached SAS address from the IAF */
207                 memcpy(isci_phy->sas_phy.attached_sas_addr,
208                        isci_phy->frame_rcvd.iaf.sas_addr, SAS_ADDR_SIZE);
209         } else {
210                 dev_err(&isci_host->pdev->dev, "%s: unkown target\n", __func__);
211                 success = false;
212         }
213
214         isci_phy->sas_phy.phy->negotiated_linkrate = sci_phy_linkrate(phy);
215
216         spin_unlock_irqrestore(&isci_phy->sas_phy.frame_rcvd_lock, flags);
217
218         /* Notify libsas that we have an address frame, if indeed
219          * we've found an SSP, SMP, or STP target */
220         if (success)
221                 isci_host->sas_ha.notify_port_event(&isci_phy->sas_phy,
222                                                     PORTE_BYTES_DMAED);
223 }
224
225
226 /**
227  * isci_port_link_down() - This function is called by the sci core when a link
228  *    becomes inactive.
229  * @isci_host: This parameter specifies the isci host object.
230  * @phy: This parameter specifies the isci phy with the active link.
231  * @port: This parameter specifies the isci port with the active link.
232  *
233  */
234 static void isci_port_link_down(struct isci_host *isci_host,
235                                 struct isci_phy *isci_phy,
236                                 struct isci_port *isci_port)
237 {
238         struct isci_remote_device *isci_device;
239
240         dev_dbg(&isci_host->pdev->dev,
241                 "%s: isci_port = %p\n", __func__, isci_port);
242
243         if (isci_port) {
244
245                 /* check to see if this is the last phy on this port. */
246                 if (isci_phy->sas_phy.port
247                     && isci_phy->sas_phy.port->num_phys == 1) {
248
249                         /* change the state for all devices on this port.
250                          * The next task sent to this device will be returned
251                          * as SAS_TASK_UNDELIVERED, and the scsi mid layer
252                          * will remove the target
253                          */
254                         list_for_each_entry(isci_device,
255                                             &isci_port->remote_dev_list,
256                                             node) {
257                                 dev_dbg(&isci_host->pdev->dev,
258                                         "%s: isci_device = %p\n",
259                                         __func__, isci_device);
260                                 isci_remote_device_change_state(isci_device,
261                                                                 isci_stopping);
262                         }
263                 }
264                 isci_port_change_state(isci_port, isci_stopping);
265         }
266
267         /* Notify libsas of the borken link, this will trigger calls to our
268          * isci_port_deformed and isci_dev_gone functions.
269          */
270         sas_phy_disconnected(&isci_phy->sas_phy);
271         isci_host->sas_ha.notify_phy_event(&isci_phy->sas_phy,
272                                            PHYE_LOSS_OF_SIGNAL);
273
274         isci_phy->isci_port = NULL;
275
276         dev_dbg(&isci_host->pdev->dev,
277                 "%s: isci_port = %p - Done\n", __func__, isci_port);
278 }
279
280
281 /**
282  * isci_port_ready() - This function is called by the sci core when a link
283  *    becomes ready.
284  * @isci_host: This parameter specifies the isci host object.
285  * @port: This parameter specifies the sci port with the active link.
286  *
287  */
288 static void isci_port_ready(struct isci_host *isci_host, struct isci_port *isci_port)
289 {
290         dev_dbg(&isci_host->pdev->dev,
291                 "%s: isci_port = %p\n", __func__, isci_port);
292
293         complete_all(&isci_port->start_complete);
294         isci_port_change_state(isci_port, isci_ready);
295         return;
296 }
297
298 /**
299  * isci_port_not_ready() - This function is called by the sci core when a link
300  *    is not ready. All remote devices on this link will be removed if they are
301  *    in the stopping state.
302  * @isci_host: This parameter specifies the isci host object.
303  * @port: This parameter specifies the sci port with the active link.
304  *
305  */
306 static void isci_port_not_ready(struct isci_host *isci_host, struct isci_port *isci_port)
307 {
308         dev_dbg(&isci_host->pdev->dev,
309                 "%s: isci_port = %p\n", __func__, isci_port);
310 }
311
312 static void isci_port_stop_complete(struct scic_sds_controller *scic,
313                                     struct scic_sds_port *sci_port,
314                                     enum sci_status completion_status)
315 {
316         dev_dbg(&scic_to_ihost(scic)->pdev->dev, "Port stop complete\n");
317 }
318
319 /**
320  * isci_port_hard_reset_complete() - This function is called by the sci core
321  *    when the hard reset complete notification has been received.
322  * @port: This parameter specifies the sci port with the active link.
323  * @completion_status: This parameter specifies the core status for the reset
324  *    process.
325  *
326  */
327 static void isci_port_hard_reset_complete(struct isci_port *isci_port,
328                                           enum sci_status completion_status)
329 {
330         dev_dbg(&isci_port->isci_host->pdev->dev,
331                 "%s: isci_port = %p, completion_status=%x\n",
332                      __func__, isci_port, completion_status);
333
334         /* Save the status of the hard reset from the port. */
335         isci_port->hard_reset_status = completion_status;
336
337         complete_all(&isci_port->hard_reset_complete);
338 }
339
340 /* This method will return a true value if the specified phy can be assigned to
341  * this port The following is a list of phys for each port that are allowed: -
342  * Port 0 - 3 2 1 0 - Port 1 -     1 - Port 2 - 3 2 - Port 3 - 3 This method
343  * doesn't preclude all configurations.  It merely ensures that a phy is part
344  * of the allowable set of phy identifiers for that port.  For example, one
345  * could assign phy 3 to port 0 and no other phys.  Please refer to
346  * scic_sds_port_is_phy_mask_valid() for information regarding whether the
347  * phy_mask for a port can be supported. bool true if this is a valid phy
348  * assignment for the port false if this is not a valid phy assignment for the
349  * port
350  */
351 bool scic_sds_port_is_valid_phy_assignment(struct scic_sds_port *sci_port,
352                                            u32 phy_index)
353 {
354         /* Initialize to invalid value. */
355         u32 existing_phy_index = SCI_MAX_PHYS;
356         u32 index;
357
358         if ((sci_port->physical_port_index == 1) && (phy_index != 1)) {
359                 return false;
360         }
361
362         if (sci_port->physical_port_index == 3 && phy_index != 3) {
363                 return false;
364         }
365
366         if (
367                 (sci_port->physical_port_index == 2)
368                 && ((phy_index == 0) || (phy_index == 1))
369                 ) {
370                 return false;
371         }
372
373         for (index = 0; index < SCI_MAX_PHYS; index++) {
374                 if ((sci_port->phy_table[index] != NULL)
375                     && (index != phy_index)) {
376                         existing_phy_index = index;
377                 }
378         }
379
380         /*
381          * Ensure that all of the phys in the port are capable of
382          * operating at the same maximum link rate. */
383         if (
384                 (existing_phy_index < SCI_MAX_PHYS)
385                 && (sci_port->owning_controller->user_parameters.sds1.phys[
386                             phy_index].max_speed_generation !=
387                     sci_port->owning_controller->user_parameters.sds1.phys[
388                             existing_phy_index].max_speed_generation)
389                 )
390                 return false;
391
392         return true;
393 }
394
395 /**
396  *
397  * @sci_port: This is the port object for which to determine if the phy mask
398  *    can be supported.
399  *
400  * This method will return a true value if the port's phy mask can be supported
401  * by the SCU. The following is a list of valid PHY mask configurations for
402  * each port: - Port 0 - [[3  2] 1] 0 - Port 1 -        [1] - Port 2 - [[3] 2]
403  * - Port 3 -  [3] This method returns a boolean indication specifying if the
404  * phy mask can be supported. true if this is a valid phy assignment for the
405  * port false if this is not a valid phy assignment for the port
406  */
407 static bool scic_sds_port_is_phy_mask_valid(
408         struct scic_sds_port *sci_port,
409         u32 phy_mask)
410 {
411         if (sci_port->physical_port_index == 0) {
412                 if (((phy_mask & 0x0F) == 0x0F)
413                     || ((phy_mask & 0x03) == 0x03)
414                     || ((phy_mask & 0x01) == 0x01)
415                     || (phy_mask == 0))
416                         return true;
417         } else if (sci_port->physical_port_index == 1) {
418                 if (((phy_mask & 0x02) == 0x02)
419                     || (phy_mask == 0))
420                         return true;
421         } else if (sci_port->physical_port_index == 2) {
422                 if (((phy_mask & 0x0C) == 0x0C)
423                     || ((phy_mask & 0x04) == 0x04)
424                     || (phy_mask == 0))
425                         return true;
426         } else if (sci_port->physical_port_index == 3) {
427                 if (((phy_mask & 0x08) == 0x08)
428                     || (phy_mask == 0))
429                         return true;
430         }
431
432         return false;
433 }
434
435 /**
436  *
437  * @sci_port: This parameter specifies the port from which to return a
438  *    connected phy.
439  *
440  * This method retrieves a currently active (i.e. connected) phy contained in
441  * the port.  Currently, the lowest order phy that is connected is returned.
442  * This method returns a pointer to a SCIS_SDS_PHY object. NULL This value is
443  * returned if there are no currently active (i.e. connected to a remote end
444  * point) phys contained in the port. All other values specify a struct scic_sds_phy
445  * object that is active in the port.
446  */
447 static struct scic_sds_phy *scic_sds_port_get_a_connected_phy(
448         struct scic_sds_port *sci_port
449         ) {
450         u32 index;
451         struct scic_sds_phy *phy;
452
453         for (index = 0; index < SCI_MAX_PHYS; index++) {
454                 /*
455                  * Ensure that the phy is both part of the port and currently
456                  * connected to the remote end-point. */
457                 phy = sci_port->phy_table[index];
458                 if (
459                         (phy != NULL)
460                         && scic_sds_port_active_phy(sci_port, phy)
461                         ) {
462                         return phy;
463                 }
464         }
465
466         return NULL;
467 }
468
469 /**
470  * scic_sds_port_set_phy() -
471  * @out]: port The port object to which the phy assignement is being made.
472  * @out]: phy The phy which is being assigned to the port.
473  *
474  * This method attempts to make the assignment of the phy to the port. If
475  * successful the phy is assigned to the ports phy table. bool true if the phy
476  * assignment can be made. false if the phy assignement can not be made. This
477  * is a functional test that only fails if the phy is currently assigned to a
478  * different port.
479  */
480 static enum sci_status scic_sds_port_set_phy(
481         struct scic_sds_port *port,
482         struct scic_sds_phy *phy)
483 {
484         /*
485          * Check to see if we can add this phy to a port
486          * that means that the phy is not part of a port and that the port does
487          * not already have a phy assinged to the phy index. */
488         if (
489                 (port->phy_table[phy->phy_index] == NULL)
490                 && (phy_get_non_dummy_port(phy) == NULL)
491                 && scic_sds_port_is_valid_phy_assignment(port, phy->phy_index)
492                 ) {
493                 /*
494                  * Phy is being added in the stopped state so we are in MPC mode
495                  * make logical port index = physical port index */
496                 port->logical_port_index = port->physical_port_index;
497                 port->phy_table[phy->phy_index] = phy;
498                 scic_sds_phy_set_port(phy, port);
499
500                 return SCI_SUCCESS;
501         }
502
503         return SCI_FAILURE;
504 }
505
506 /**
507  * scic_sds_port_clear_phy() -
508  * @out]: port The port from which the phy is being cleared.
509  * @out]: phy The phy being cleared from the port.
510  *
511  * This method will clear the phy assigned to this port.  This method fails if
512  * this phy is not currently assinged to this port. bool true if the phy is
513  * removed from the port. false if this phy is not assined to this port.
514  */
515 static enum sci_status scic_sds_port_clear_phy(
516         struct scic_sds_port *port,
517         struct scic_sds_phy *phy)
518 {
519         /* Make sure that this phy is part of this port */
520         if (port->phy_table[phy->phy_index] == phy &&
521             phy_get_non_dummy_port(phy) == port) {
522                 struct scic_sds_controller *scic = port->owning_controller;
523                 struct isci_host *ihost = scic_to_ihost(scic);
524
525                 /* Yep it is assigned to this port so remove it */
526                 scic_sds_phy_set_port(phy, &ihost->ports[SCI_MAX_PORTS].sci);
527                 port->phy_table[phy->phy_index] = NULL;
528                 return SCI_SUCCESS;
529         }
530
531         return SCI_FAILURE;
532 }
533
534 /**
535  * scic_sds_port_add_phy() -
536  * @sci_port: This parameter specifies the port in which the phy will be added.
537  * @sci_phy: This parameter is the phy which is to be added to the port.
538  *
539  * This method will add a PHY to the selected port. This method returns an
540  * enum sci_status. SCI_SUCCESS the phy has been added to the port. Any other status
541  * is failre to add the phy to the port.
542  */
543 enum sci_status scic_sds_port_add_phy(
544         struct scic_sds_port *sci_port,
545         struct scic_sds_phy *sci_phy)
546 {
547         return sci_port->state_handlers->add_phy_handler(
548                        sci_port, sci_phy);
549 }
550
551
552 /**
553  * scic_sds_port_remove_phy() -
554  * @sci_port: This parameter specifies the port in which the phy will be added.
555  * @sci_phy: This parameter is the phy which is to be added to the port.
556  *
557  * This method will remove the PHY from the selected PORT. This method returns
558  * an enum sci_status. SCI_SUCCESS the phy has been removed from the port. Any other
559  * status is failre to add the phy to the port.
560  */
561 enum sci_status scic_sds_port_remove_phy(
562         struct scic_sds_port *sci_port,
563         struct scic_sds_phy *sci_phy)
564 {
565         return sci_port->state_handlers->remove_phy_handler(
566                        sci_port, sci_phy);
567 }
568
569 /**
570  * This method requests the SAS address for the supplied SAS port from the SCI
571  *    implementation.
572  * @sci_port: a handle corresponding to the SAS port for which to return the
573  *    SAS address.
574  * @sas_address: This parameter specifies a pointer to a SAS address structure
575  *    into which the core will copy the SAS address for the port.
576  *
577  */
578 void scic_sds_port_get_sas_address(
579         struct scic_sds_port *sci_port,
580         struct sci_sas_address *sas_address)
581 {
582         u32 index;
583
584         sas_address->high = 0;
585         sas_address->low  = 0;
586
587         for (index = 0; index < SCI_MAX_PHYS; index++) {
588                 if (sci_port->phy_table[index] != NULL) {
589                         scic_sds_phy_get_sas_address(sci_port->phy_table[index], sas_address);
590                 }
591         }
592 }
593
594 /*
595  * This function requests the SAS address for the device directly attached to
596  *    this SAS port.
597  * @sci_port: a handle corresponding to the SAS port for which to return the
598  *    SAS address.
599  * @sas_address: This parameter specifies a pointer to a SAS address structure
600  *    into which the core will copy the SAS address for the device directly
601  *    attached to the port.
602  *
603  */
604 void scic_sds_port_get_attached_sas_address(
605         struct scic_sds_port *sci_port,
606         struct sci_sas_address *sas_address)
607 {
608         struct scic_sds_phy *sci_phy;
609
610         /*
611          * Ensure that the phy is both part of the port and currently
612          * connected to the remote end-point.
613          */
614         sci_phy = scic_sds_port_get_a_connected_phy(sci_port);
615         if (sci_phy) {
616                 if (sci_phy->protocol != SCIC_SDS_PHY_PROTOCOL_SATA) {
617                         scic_sds_phy_get_attached_sas_address(sci_phy,
618                                                               sas_address);
619                 } else {
620                         scic_sds_phy_get_sas_address(sci_phy, sas_address);
621                         sas_address->low += sci_phy->phy_index;
622                 }
623         } else {
624                 sas_address->high = 0;
625                 sas_address->low  = 0;
626         }
627 }
628
629 /**
630  * scic_sds_port_construct_dummy_rnc() - create dummy rnc for si workaround
631  *
632  * @sci_port: logical port on which we need to create the remote node context
633  * @rni: remote node index for this remote node context.
634  *
635  * This routine will construct a dummy remote node context data structure
636  * This structure will be posted to the hardware to work around a scheduler
637  * error in the hardware.
638  */
639 static void scic_sds_port_construct_dummy_rnc(struct scic_sds_port *sci_port, u16 rni)
640 {
641         union scu_remote_node_context *rnc;
642
643         rnc = &sci_port->owning_controller->remote_node_context_table[rni];
644
645         memset(rnc, 0, sizeof(union scu_remote_node_context));
646
647         rnc->ssp.remote_sas_address_hi = 0;
648         rnc->ssp.remote_sas_address_lo = 0;
649
650         rnc->ssp.remote_node_index = rni;
651         rnc->ssp.remote_node_port_width = 1;
652         rnc->ssp.logical_port_index = sci_port->physical_port_index;
653
654         rnc->ssp.nexus_loss_timer_enable = false;
655         rnc->ssp.check_bit = false;
656         rnc->ssp.is_valid = true;
657         rnc->ssp.is_remote_node_context = true;
658         rnc->ssp.function_number = 0;
659         rnc->ssp.arbitration_wait_time = 0;
660 }
661
662 /**
663  * scic_sds_port_construct_dummy_task() - create dummy task for si workaround
664  * @sci_port The logical port on which we need to create the
665  *            remote node context.
666  *            context.
667  * @tci The remote node index for this remote node context.
668  *
669  * This routine will construct a dummy task context data structure.  This
670  * structure will be posted to the hardwre to work around a scheduler error
671  * in the hardware.
672  *
673  */
674 static void scic_sds_port_construct_dummy_task(struct scic_sds_port *sci_port, u16 tci)
675 {
676         struct scu_task_context *task_context;
677
678         task_context = scic_sds_controller_get_task_context_buffer(sci_port->owning_controller, tci);
679
680         memset(task_context, 0, sizeof(struct scu_task_context));
681
682         task_context->abort = 0;
683         task_context->priority = 0;
684         task_context->initiator_request = 1;
685         task_context->connection_rate = 1;
686         task_context->protocol_engine_index = 0;
687         task_context->logical_port_index = sci_port->physical_port_index;
688         task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SSP;
689         task_context->task_index = scic_sds_io_tag_get_index(tci);
690         task_context->valid = SCU_TASK_CONTEXT_VALID;
691         task_context->context_type = SCU_TASK_CONTEXT_TYPE;
692
693         task_context->remote_node_index = sci_port->reserved_rni;
694         task_context->command_code = 0;
695
696         task_context->link_layer_control = 0;
697         task_context->do_not_dma_ssp_good_response = 1;
698         task_context->strict_ordering = 0;
699         task_context->control_frame = 0;
700         task_context->timeout_enable = 0;
701         task_context->block_guard_enable = 0;
702
703         task_context->address_modifier = 0;
704
705         task_context->task_phase = 0x01;
706 }
707
708 static void scic_sds_port_destroy_dummy_resources(struct scic_sds_port *sci_port)
709 {
710         struct scic_sds_controller *scic = sci_port->owning_controller;
711
712         if (sci_port->reserved_tci != SCU_DUMMY_INDEX)
713                 scic_controller_free_io_tag(scic, sci_port->reserved_tci);
714
715         if (sci_port->reserved_rni != SCU_DUMMY_INDEX)
716                 scic_sds_remote_node_table_release_remote_node_index(&scic->available_remote_nodes,
717                                                                      1, sci_port->reserved_rni);
718
719         sci_port->reserved_rni = SCU_DUMMY_INDEX;
720         sci_port->reserved_tci = SCU_DUMMY_INDEX;
721 }
722
723 /**
724  * This method performs initialization of the supplied port. Initialization
725  *    includes: - state machine initialization - member variable initialization
726  *    - configuring the phy_mask
727  * @sci_port:
728  * @transport_layer_registers:
729  * @port_task_scheduler_registers:
730  * @port_configuration_regsiter:
731  *
732  * enum sci_status SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION This value is returned
733  * if the phy being added to the port
734  */
735 enum sci_status scic_sds_port_initialize(
736         struct scic_sds_port *sci_port,
737         void __iomem *port_task_scheduler_registers,
738         void __iomem *port_configuration_regsiter,
739         void __iomem *viit_registers)
740 {
741         sci_port->port_task_scheduler_registers  = port_task_scheduler_registers;
742         sci_port->port_pe_configuration_register = port_configuration_regsiter;
743         sci_port->viit_registers                 = viit_registers;
744
745         return SCI_SUCCESS;
746 }
747
748 /**
749  * scic_port_hard_reset() - perform port hard reset
750  * @port: a handle corresponding to the SAS port to be hard reset.
751  * @reset_timeout: This parameter specifies the number of milliseconds in which
752  *    the port reset operation should complete.
753  *
754  * The SCI User callback in scic_user_callbacks_t will only be called once for
755  * each phy in the SAS Port at completion of the hard reset sequence. Return a
756  * status indicating whether the hard reset started successfully. SCI_SUCCESS
757  * This value is returned if the hard reset operation started successfully.
758  */
759 static enum sci_status scic_port_hard_reset(struct scic_sds_port *port,
760                                             u32 reset_timeout)
761 {
762         return port->state_handlers->reset_handler(
763                        port, reset_timeout);
764 }
765
766 /**
767  * This method assigns the direct attached device ID for this port.
768  *
769  * @param[in] sci_port The port for which the direct attached device id is to
770  *       be assigned.
771  * @param[in] device_id The direct attached device ID to assign to the port.
772  *       This will be the RNi for the device
773  */
774 void scic_sds_port_setup_transports(
775         struct scic_sds_port *sci_port,
776         u32 device_id)
777 {
778         u8 index;
779
780         for (index = 0; index < SCI_MAX_PHYS; index++) {
781                 if (sci_port->active_phy_mask & (1 << index))
782                         scic_sds_phy_setup_transport(sci_port->phy_table[index], device_id);
783         }
784 }
785
786 /**
787  *
788  * @sci_port: This is the port on which the phy should be enabled.
789  * @sci_phy: This is the specific phy which to enable.
790  * @do_notify_user: This parameter specifies whether to inform the user (via
791  *    scic_cb_port_link_up()) as to the fact that a new phy as become ready.
792  *
793  * This function will activate the phy in the port.
794  * Activation includes: - adding
795  * the phy to the port - enabling the Protocol Engine in the silicon. -
796  * notifying the user that the link is up. none
797  */
798 static void scic_sds_port_activate_phy(struct scic_sds_port *sci_port,
799                                        struct scic_sds_phy *sci_phy,
800                                        bool do_notify_user)
801 {
802         struct scic_sds_controller *scic = sci_port->owning_controller;
803         struct isci_host *ihost = scic_to_ihost(scic);
804
805         if (sci_phy->protocol != SCIC_SDS_PHY_PROTOCOL_SATA)
806                 scic_sds_phy_resume(sci_phy);
807
808         sci_port->active_phy_mask |= 1 << sci_phy->phy_index;
809
810         scic_sds_controller_clear_invalid_phy(scic, sci_phy);
811
812         if (do_notify_user == true)
813                 isci_port_link_up(ihost, sci_port, sci_phy);
814 }
815
816 void scic_sds_port_deactivate_phy(struct scic_sds_port *sci_port,
817                                   struct scic_sds_phy *sci_phy,
818                                   bool do_notify_user)
819 {
820         struct scic_sds_controller *scic = scic_sds_port_get_controller(sci_port);
821         struct isci_port *iport = sci_port_to_iport(sci_port);
822         struct isci_host *ihost = scic_to_ihost(scic);
823         struct isci_phy *iphy = sci_phy_to_iphy(sci_phy);
824
825         sci_port->active_phy_mask &= ~(1 << sci_phy->phy_index);
826
827         sci_phy->max_negotiated_speed = SAS_LINK_RATE_UNKNOWN;
828
829         /* Re-assign the phy back to the LP as if it were a narrow port */
830         writel(sci_phy->phy_index,
831                 &sci_port->port_pe_configuration_register[sci_phy->phy_index]);
832
833         if (do_notify_user == true)
834                 isci_port_link_down(ihost, iphy, iport);
835 }
836
837 /**
838  *
839  * @sci_port: This is the port on which the phy should be disabled.
840  * @sci_phy: This is the specific phy which to disabled.
841  *
842  * This function will disable the phy and report that the phy is not valid for
843  * this port object. None
844  */
845 static void scic_sds_port_invalid_link_up(struct scic_sds_port *sci_port,
846                                           struct scic_sds_phy *sci_phy)
847 {
848         struct scic_sds_controller *scic = sci_port->owning_controller;
849
850         /*
851          * Check to see if we have alreay reported this link as bad and if
852          * not go ahead and tell the SCI_USER that we have discovered an
853          * invalid link.
854          */
855         if ((scic->invalid_phy_mask & (1 << sci_phy->phy_index)) == 0) {
856                 scic_sds_controller_set_invalid_phy(scic, sci_phy);
857                 dev_warn(&scic_to_ihost(scic)->pdev->dev, "Invalid link up!\n");
858         }
859 }
860
861 static bool is_port_ready_state(enum scic_sds_port_states state)
862 {
863         switch (state) {
864         case SCI_BASE_PORT_STATE_READY:
865         case SCIC_SDS_PORT_READY_SUBSTATE_WAITING:
866         case SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL:
867         case SCIC_SDS_PORT_READY_SUBSTATE_CONFIGURING:
868                 return true;
869         default:
870                 return false;
871         }
872 }
873
874 /* flag dummy rnc hanling when exiting a ready state */
875 static void port_state_machine_change(struct scic_sds_port *sci_port,
876                                       enum scic_sds_port_states state)
877 {
878         struct sci_base_state_machine *sm = &sci_port->state_machine;
879         enum scic_sds_port_states old_state = sm->current_state_id;
880
881         if (is_port_ready_state(old_state) && !is_port_ready_state(state))
882                 sci_port->ready_exit = true;
883
884         sci_base_state_machine_change_state(sm, state);
885         sci_port->ready_exit = false;
886 }
887
888 static void port_state_machine_stop(struct scic_sds_port *sci_port)
889 {
890         sci_port->ready_exit = true;
891         sci_base_state_machine_stop(&sci_port->state_machine);
892         sci_port->ready_exit = false;
893 }
894
895 /**
896  * scic_sds_port_general_link_up_handler - phy can be assigned to port?
897  * @sci_port: scic_sds_port object for which has a phy that has gone link up.
898  * @sci_phy: This is the struct scic_sds_phy object that has gone link up.
899  * @do_notify_user: This parameter specifies whether to inform the user (via
900  *    scic_cb_port_link_up()) as to the fact that a new phy as become ready.
901  *
902  * Determine if this phy can be assigned to this
903  * port . If the phy is not a valid PHY for
904  * this port then the function will notify the user. A PHY can only be
905  * part of a port if it's attached SAS ADDRESS is the same as all other PHYs in
906  * the same port. none
907  */
908 static void scic_sds_port_general_link_up_handler(struct scic_sds_port *sci_port,
909                                                   struct scic_sds_phy *sci_phy,
910                                                   bool do_notify_user)
911 {
912         struct sci_sas_address port_sas_address;
913         struct sci_sas_address phy_sas_address;
914
915         scic_sds_port_get_attached_sas_address(sci_port, &port_sas_address);
916         scic_sds_phy_get_attached_sas_address(sci_phy, &phy_sas_address);
917
918         /* If the SAS address of the new phy matches the SAS address of
919          * other phys in the port OR this is the first phy in the port,
920          * then activate the phy and allow it to be used for operations
921          * in this port.
922          */
923         if ((phy_sas_address.high == port_sas_address.high &&
924              phy_sas_address.low  == port_sas_address.low) ||
925             sci_port->active_phy_mask == 0) {
926                 struct sci_base_state_machine *sm = &sci_port->state_machine;
927
928                 scic_sds_port_activate_phy(sci_port, sci_phy, do_notify_user);
929                 if (sm->current_state_id == SCI_BASE_PORT_STATE_RESETTING)
930                         port_state_machine_change(sci_port, SCI_BASE_PORT_STATE_READY);
931         } else
932                 scic_sds_port_invalid_link_up(sci_port, sci_phy);
933 }
934
935
936
937 /**
938  * This method returns false if the port only has a single phy object assigned.
939  *     If there are no phys or more than one phy then the method will return
940  *    true.
941  * @sci_port: The port for which the wide port condition is to be checked.
942  *
943  * bool true Is returned if this is a wide ported port. false Is returned if
944  * this is a narrow port.
945  */
946 static bool scic_sds_port_is_wide(struct scic_sds_port *sci_port)
947 {
948         u32 index;
949         u32 phy_count = 0;
950
951         for (index = 0; index < SCI_MAX_PHYS; index++) {
952                 if (sci_port->phy_table[index] != NULL) {
953                         phy_count++;
954                 }
955         }
956
957         return phy_count != 1;
958 }
959
960 /**
961  * This method is called by the PHY object when the link is detected. if the
962  *    port wants the PHY to continue on to the link up state then the port
963  *    layer must return true.  If the port object returns false the phy object
964  *    must halt its attempt to go link up.
965  * @sci_port: The port associated with the phy object.
966  * @sci_phy: The phy object that is trying to go link up.
967  *
968  * true if the phy object can continue to the link up condition. true Is
969  * returned if this phy can continue to the ready state. false Is returned if
970  * can not continue on to the ready state. This notification is in place for
971  * wide ports and direct attached phys.  Since there are no wide ported SATA
972  * devices this could become an invalid port configuration.
973  */
974 bool scic_sds_port_link_detected(
975         struct scic_sds_port *sci_port,
976         struct scic_sds_phy *sci_phy)
977 {
978         if ((sci_port->logical_port_index != SCIC_SDS_DUMMY_PORT) &&
979             (sci_phy->protocol == SCIC_SDS_PHY_PROTOCOL_SATA) &&
980             scic_sds_port_is_wide(sci_port)) {
981                 scic_sds_port_invalid_link_up(sci_port, sci_phy);
982
983                 return false;
984         }
985
986         return true;
987 }
988
989 /**
990  * This method is the entry point for the phy to inform the port that it is now
991  *    in a ready state
992  * @sci_port:
993  *
994  *
995  */
996 void scic_sds_port_link_up(
997         struct scic_sds_port *sci_port,
998         struct scic_sds_phy *sci_phy)
999 {
1000         sci_phy->is_in_link_training = false;
1001
1002         sci_port->state_handlers->link_up_handler(sci_port, sci_phy);
1003 }
1004
1005 /**
1006  * This method is the entry point for the phy to inform the port that it is no
1007  *    longer in a ready state
1008  * @sci_port:
1009  *
1010  *
1011  */
1012 void scic_sds_port_link_down(
1013         struct scic_sds_port *sci_port,
1014         struct scic_sds_phy *sci_phy)
1015 {
1016         sci_port->state_handlers->link_down_handler(sci_port, sci_phy);
1017 }
1018
1019 /**
1020  * This method is called to start an IO request on this port.
1021  * @sci_port:
1022  * @sci_dev:
1023  * @sci_req:
1024  *
1025  * enum sci_status
1026  */
1027 enum sci_status scic_sds_port_start_io(
1028         struct scic_sds_port *sci_port,
1029         struct scic_sds_remote_device *sci_dev,
1030         struct scic_sds_request *sci_req)
1031 {
1032         return sci_port->state_handlers->start_io_handler(
1033                        sci_port, sci_dev, sci_req);
1034 }
1035
1036 /**
1037  * This method is called to complete an IO request to the port.
1038  * @sci_port:
1039  * @sci_dev:
1040  * @sci_req:
1041  *
1042  * enum sci_status
1043  */
1044 enum sci_status scic_sds_port_complete_io(
1045         struct scic_sds_port *sci_port,
1046         struct scic_sds_remote_device *sci_dev,
1047         struct scic_sds_request *sci_req)
1048 {
1049         return sci_port->state_handlers->complete_io_handler(
1050                        sci_port, sci_dev, sci_req);
1051 }
1052
1053 /**
1054  * This method is provided to timeout requests for port operations. Mostly its
1055  *    for the port reset operation.
1056  *
1057  *
1058  */
1059 static void scic_sds_port_timeout_handler(void *port)
1060 {
1061         struct scic_sds_port *sci_port = port;
1062         u32 current_state;
1063
1064         current_state = sci_base_state_machine_get_state(&sci_port->state_machine);
1065
1066         if (current_state == SCI_BASE_PORT_STATE_RESETTING) {
1067                 /* if the port is still in the resetting state then the timeout
1068                  * fired before the reset completed.
1069                  */
1070                 port_state_machine_change(sci_port, SCI_BASE_PORT_STATE_FAILED);
1071         } else if (current_state == SCI_BASE_PORT_STATE_STOPPED) {
1072                 /* if the port is stopped then the start request failed In this
1073                  * case stay in the stopped state.
1074                  */
1075                 dev_err(sciport_to_dev(sci_port),
1076                         "%s: SCIC Port 0x%p failed to stop before tiemout.\n",
1077                         __func__,
1078                         sci_port);
1079         } else if (current_state == SCI_BASE_PORT_STATE_STOPPING) {
1080                 /* if the port is still stopping then the stop has not completed */
1081                 isci_port_stop_complete(sci_port->owning_controller,
1082                                         sci_port,
1083                                         SCI_FAILURE_TIMEOUT);
1084         } else {
1085                 /* The port is in the ready state and we have a timer
1086                  * reporting a timeout this should not happen.
1087                  */
1088                 dev_err(sciport_to_dev(sci_port),
1089                         "%s: SCIC Port 0x%p is processing a timeout operation "
1090                         "in state %d.\n", __func__, sci_port, current_state);
1091         }
1092 }
1093
1094 /* --------------------------------------------------------------------------- */
1095
1096 /**
1097  * This function updates the hardwares VIIT entry for this port.
1098  *
1099  *
1100  */
1101 static void scic_sds_port_update_viit_entry(struct scic_sds_port *sci_port)
1102 {
1103         struct sci_sas_address sas_address;
1104
1105         scic_sds_port_get_sas_address(sci_port, &sas_address);
1106
1107         writel(sas_address.high,
1108                 &sci_port->viit_registers->initiator_sas_address_hi);
1109         writel(sas_address.low,
1110                 &sci_port->viit_registers->initiator_sas_address_lo);
1111
1112         /* This value get cleared just in case its not already cleared */
1113         writel(0, &sci_port->viit_registers->reserved);
1114
1115         /* We are required to update the status register last */
1116         writel(SCU_VIIT_ENTRY_ID_VIIT |
1117                SCU_VIIT_IPPT_INITIATOR |
1118                ((1 << sci_port->physical_port_index) << SCU_VIIT_ENTRY_LPVIE_SHIFT) |
1119                SCU_VIIT_STATUS_ALL_VALID,
1120                &sci_port->viit_registers->status);
1121 }
1122
1123 /**
1124  * This method returns the maximum allowed speed for data transfers on this
1125  *    port.  This maximum allowed speed evaluates to the maximum speed of the
1126  *    slowest phy in the port.
1127  * @sci_port: This parameter specifies the port for which to retrieve the
1128  *    maximum allowed speed.
1129  *
1130  * This method returns the maximum negotiated speed of the slowest phy in the
1131  * port.
1132  */
1133 enum sas_linkrate scic_sds_port_get_max_allowed_speed(
1134         struct scic_sds_port *sci_port)
1135 {
1136         u16 index;
1137         enum sas_linkrate max_allowed_speed = SAS_LINK_RATE_6_0_GBPS;
1138         struct scic_sds_phy *phy = NULL;
1139
1140         /*
1141          * Loop through all of the phys in this port and find the phy with the
1142          * lowest maximum link rate. */
1143         for (index = 0; index < SCI_MAX_PHYS; index++) {
1144                 phy = sci_port->phy_table[index];
1145                 if (
1146                         (phy != NULL)
1147                         && (scic_sds_port_active_phy(sci_port, phy) == true)
1148                         && (phy->max_negotiated_speed < max_allowed_speed)
1149                         )
1150                         max_allowed_speed = phy->max_negotiated_speed;
1151         }
1152
1153         return max_allowed_speed;
1154 }
1155
1156 static void scic_port_enable_broadcast_change_notification(struct scic_sds_port *port)
1157 {
1158         struct scic_sds_phy *phy;
1159         u32 register_value;
1160         u8 index;
1161
1162         /* Loop through all of the phys to enable BCN. */
1163         for (index = 0; index < SCI_MAX_PHYS; index++) {
1164                 phy = port->phy_table[index];
1165                 if (phy != NULL) {
1166                         register_value =
1167                                 readl(&phy->link_layer_registers->link_layer_control);
1168
1169                         /* clear the bit by writing 1. */
1170                         writel(register_value,
1171                                 &phy->link_layer_registers->link_layer_control);
1172                 }
1173         }
1174 }
1175
1176 /*
1177  * ****************************************************************************
1178  * *  READY SUBSTATE HANDLERS
1179  * **************************************************************************** */
1180
1181 /*
1182  * This method is the general ready substate complete io handler for the
1183  * struct scic_sds_port object.  This function decrments the outstanding request count
1184  * for this port object. enum sci_status SCI_SUCCESS
1185  */
1186 static enum sci_status scic_sds_port_ready_substate_complete_io_handler(
1187         struct scic_sds_port *port,
1188         struct scic_sds_remote_device *device,
1189         struct scic_sds_request *io_request)
1190 {
1191         scic_sds_port_decrement_request_count(port);
1192
1193         return SCI_SUCCESS;
1194 }
1195
1196 static enum sci_status scic_sds_port_ready_substate_add_phy_handler(struct scic_sds_port *sci_port,
1197                                                                     struct scic_sds_phy *sci_phy)
1198 {
1199         enum sci_status status;
1200
1201         status = scic_sds_port_set_phy(sci_port, sci_phy);
1202
1203         if (status != SCI_SUCCESS)
1204                 return status;
1205
1206         scic_sds_port_general_link_up_handler(sci_port, sci_phy, true);
1207         sci_port->not_ready_reason = SCIC_PORT_NOT_READY_RECONFIGURING;
1208         port_state_machine_change(sci_port, SCIC_SDS_PORT_READY_SUBSTATE_CONFIGURING);
1209
1210         return status;
1211 }
1212
1213
1214 static enum sci_status scic_sds_port_ready_substate_remove_phy_handler(struct scic_sds_port *port,
1215                                                                        struct scic_sds_phy *phy)
1216 {
1217         enum sci_status status;
1218
1219         status = scic_sds_port_clear_phy(port, phy);
1220
1221         if (status != SCI_SUCCESS)
1222                 return status;
1223
1224         scic_sds_port_deactivate_phy(port, phy, true);
1225
1226         port->not_ready_reason = SCIC_PORT_NOT_READY_RECONFIGURING;
1227
1228         port_state_machine_change(port,
1229                                   SCIC_SDS_PORT_READY_SUBSTATE_CONFIGURING);
1230         return status;
1231 }
1232
1233 /*
1234  * ****************************************************************************
1235  * *  READY SUBSTATE WAITING HANDLERS
1236  * **************************************************************************** */
1237
1238 /**
1239  *
1240  * @sci_port: This is the struct scic_sds_port object that which has a phy that has
1241  *    gone link up.
1242  * @sci_phy: This is the struct scic_sds_phy object that has gone link up.
1243  *
1244  * This method is the ready waiting substate link up handler for the
1245  * struct scic_sds_port object.  This methos will report the link up condition for
1246  * this port and will transition to the ready operational substate. none
1247  */
1248 static void scic_sds_port_ready_waiting_substate_link_up_handler(
1249         struct scic_sds_port *sci_port,
1250         struct scic_sds_phy *sci_phy)
1251 {
1252         /*
1253          * Since this is the first phy going link up for the port we can just enable
1254          * it and continue. */
1255         scic_sds_port_activate_phy(sci_port, sci_phy, true);
1256
1257         port_state_machine_change(sci_port,
1258                                   SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL);
1259 }
1260
1261 /*
1262  * This method is the ready waiting substate start io handler for the
1263  * struct scic_sds_port object. The port object can not accept new requests so the
1264  * request is failed. enum sci_status SCI_FAILURE_INVALID_STATE
1265  */
1266 static enum sci_status scic_sds_port_ready_waiting_substate_start_io_handler(
1267         struct scic_sds_port *port,
1268         struct scic_sds_remote_device *device,
1269         struct scic_sds_request *io_request)
1270 {
1271         return SCI_FAILURE_INVALID_STATE;
1272 }
1273
1274 /*
1275  * ****************************************************************************
1276  * *  READY SUBSTATE OPERATIONAL HANDLERS
1277  * **************************************************************************** */
1278
1279 /*
1280  * This method will casue the port to reset. enum sci_status SCI_SUCCESS
1281  */
1282 static enum
1283 sci_status scic_sds_port_ready_operational_substate_reset_handler(
1284                 struct scic_sds_port *port,
1285                 u32 timeout)
1286 {
1287         enum sci_status status = SCI_FAILURE_INVALID_PHY;
1288         u32 phy_index;
1289         struct scic_sds_phy *selected_phy = NULL;
1290
1291
1292         /* Select a phy on which we can send the hard reset request. */
1293         for (phy_index = 0;
1294              (phy_index < SCI_MAX_PHYS) && (selected_phy == NULL);
1295              phy_index++) {
1296                 selected_phy = port->phy_table[phy_index];
1297
1298                 if ((selected_phy != NULL) &&
1299                     !scic_sds_port_active_phy(port, selected_phy)) {
1300                         /*
1301                          * We found a phy but it is not ready select
1302                          * different phy
1303                          */
1304                         selected_phy = NULL;
1305                 }
1306         }
1307
1308         /* If we have a phy then go ahead and start the reset procedure */
1309         if (selected_phy != NULL) {
1310                 status = scic_sds_phy_reset(selected_phy);
1311
1312                 if (status == SCI_SUCCESS) {
1313                         isci_timer_start(port->timer_handle, timeout);
1314                         port->not_ready_reason =
1315                                 SCIC_PORT_NOT_READY_HARD_RESET_REQUESTED;
1316
1317                         port_state_machine_change(port,
1318                                                   SCI_BASE_PORT_STATE_RESETTING);
1319                 }
1320         }
1321
1322         return status;
1323 }
1324
1325 /**
1326  * scic_sds_port_ready_operational_substate_link_up_handler() -
1327  * @sci_port: This is the struct scic_sds_port object that which has a phy that has
1328  *    gone link up.
1329  * @sci_phy: This is the struct scic_sds_phy object that has gone link up.
1330  *
1331  * This method is the ready operational substate link up handler for the
1332  * struct scic_sds_port object. This function notifies the SCI User that the phy has
1333  * gone link up. none
1334  */
1335 static void scic_sds_port_ready_operational_substate_link_up_handler(
1336         struct scic_sds_port *sci_port,
1337         struct scic_sds_phy *sci_phy)
1338 {
1339         scic_sds_port_general_link_up_handler(sci_port, sci_phy, true);
1340 }
1341
1342 /**
1343  * scic_sds_port_ready_operational_substate_link_down_handler() -
1344  * @sci_port: This is the struct scic_sds_port object that which has a phy that has
1345  *    gone link down.
1346  * @sci_phy: This is the struct scic_sds_phy object that has gone link down.
1347  *
1348  * This method is the ready operational substate link down handler for the
1349  * struct scic_sds_port object. This function notifies the SCI User that the phy has
1350  * gone link down and if this is the last phy in the port the port will change
1351  * state to the ready waiting substate. none
1352  */
1353 static void scic_sds_port_ready_operational_substate_link_down_handler(
1354         struct scic_sds_port *sci_port,
1355         struct scic_sds_phy *sci_phy)
1356 {
1357         scic_sds_port_deactivate_phy(sci_port, sci_phy, true);
1358
1359         /*
1360          * If there are no active phys left in the port, then transition
1361          * the port to the WAITING state until such time as a phy goes
1362          * link up. */
1363         if (sci_port->active_phy_mask == 0)
1364                 port_state_machine_change(sci_port,
1365                                           SCIC_SDS_PORT_READY_SUBSTATE_WAITING);
1366 }
1367
1368 /*
1369  * This method is the ready operational substate start io handler for the
1370  * struct scic_sds_port object.  This function incremetns the outstanding request
1371  * count for this port object. enum sci_status SCI_SUCCESS
1372  */
1373 static enum sci_status scic_sds_port_ready_operational_substate_start_io_handler(
1374         struct scic_sds_port *port,
1375         struct scic_sds_remote_device *device,
1376         struct scic_sds_request *io_request)
1377 {
1378         port->started_request_count++;
1379         return SCI_SUCCESS;
1380 }
1381
1382 /*
1383  * ****************************************************************************
1384  * *  READY SUBSTATE OPERATIONAL HANDLERS
1385  * **************************************************************************** */
1386
1387 /*
1388  * This is the default method for a port add phy request.  It will report a
1389  * warning and exit. enum sci_status SCI_FAILURE_INVALID_STATE
1390  */
1391 static enum sci_status scic_sds_port_ready_configuring_substate_add_phy_handler(
1392         struct scic_sds_port *port,
1393         struct scic_sds_phy *phy)
1394 {
1395         enum sci_status status;
1396
1397         status = scic_sds_port_set_phy(port, phy);
1398
1399         if (status == SCI_SUCCESS) {
1400                 scic_sds_port_general_link_up_handler(port, phy, true);
1401
1402                 /*
1403                  * Re-enter the configuring state since this may be the last phy in
1404                  * the port. */
1405                 port_state_machine_change(port,
1406                                           SCIC_SDS_PORT_READY_SUBSTATE_CONFIGURING);
1407         }
1408
1409         return status;
1410 }
1411
1412 /*
1413  * This is the default method for a port remove phy request.  It will report a
1414  * warning and exit. enum sci_status SCI_FAILURE_INVALID_STATE
1415  */
1416 static enum sci_status scic_sds_port_ready_configuring_substate_remove_phy_handler(
1417         struct scic_sds_port *port,
1418         struct scic_sds_phy *phy)
1419 {
1420         enum sci_status status;
1421
1422         status = scic_sds_port_clear_phy(port, phy);
1423
1424         if (status != SCI_SUCCESS)
1425                 return status;
1426         scic_sds_port_deactivate_phy(port, phy, true);
1427
1428         /* Re-enter the configuring state since this may be the last phy in
1429          * the port
1430          */
1431         port_state_machine_change(port,
1432                                   SCIC_SDS_PORT_READY_SUBSTATE_CONFIGURING);
1433
1434         return status;
1435 }
1436
1437 /**
1438  * scic_sds_port_ready_configuring_substate_complete_io_handler() -
1439  * @port: This is the port that is being requested to complete the io request.
1440  * @device: This is the device on which the io is completing.
1441  *
1442  * This method will decrement the outstanding request count for this port. If
1443  * the request count goes to 0 then the port can be reprogrammed with its new
1444  * phy data.
1445  */
1446 static enum sci_status
1447 scic_sds_port_ready_configuring_substate_complete_io_handler(
1448         struct scic_sds_port *port,
1449         struct scic_sds_remote_device *device,
1450         struct scic_sds_request *io_request)
1451 {
1452         scic_sds_port_decrement_request_count(port);
1453
1454         if (port->started_request_count == 0) {
1455                 port_state_machine_change(port,
1456                                           SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL);
1457         }
1458
1459         return SCI_SUCCESS;
1460 }
1461
1462 static enum sci_status default_port_handler(struct scic_sds_port *sci_port,
1463                                             const char *func)
1464 {
1465         dev_warn(sciport_to_dev(sci_port),
1466                  "%s: in wrong state: %d\n", func,
1467                  sci_base_state_machine_get_state(&sci_port->state_machine));
1468         return SCI_FAILURE_INVALID_STATE;
1469 }
1470
1471 static enum sci_status
1472 scic_sds_port_default_destruct_handler(struct scic_sds_port *sci_port)
1473 {
1474         return default_port_handler(sci_port, __func__);
1475 }
1476
1477 static enum sci_status
1478 scic_sds_port_default_reset_handler(struct scic_sds_port *sci_port,
1479                                     u32 timeout)
1480 {
1481         return default_port_handler(sci_port, __func__);
1482 }
1483
1484 static enum sci_status
1485 scic_sds_port_default_add_phy_handler(struct scic_sds_port *sci_port,
1486                                       struct scic_sds_phy *base_phy)
1487 {
1488         return default_port_handler(sci_port, __func__);
1489 }
1490
1491 static enum sci_status
1492 scic_sds_port_default_remove_phy_handler(struct scic_sds_port *sci_port,
1493                                          struct scic_sds_phy *base_phy)
1494 {
1495         return default_port_handler(sci_port, __func__);
1496 }
1497
1498 /*
1499  * This is the default method for a port unsolicited frame request.  It will
1500  * report a warning and exit. enum sci_status SCI_FAILURE_INVALID_STATE Is it even
1501  * possible to receive an unsolicited frame directed to a port object?  It
1502  * seems possible if we implementing virtual functions but until then?
1503  */
1504 static enum sci_status
1505 scic_sds_port_default_frame_handler(struct scic_sds_port *sci_port,
1506                                     u32 frame_index)
1507 {
1508         struct scic_sds_controller *scic = scic_sds_port_get_controller(sci_port);
1509
1510         default_port_handler(sci_port, __func__);
1511         scic_sds_controller_release_frame(scic, frame_index);
1512
1513         return SCI_FAILURE_INVALID_STATE;
1514 }
1515
1516 static enum sci_status scic_sds_port_default_event_handler(struct scic_sds_port *sci_port,
1517                                                     u32 event_code)
1518 {
1519         return default_port_handler(sci_port, __func__);
1520 }
1521
1522 static void scic_sds_port_default_link_up_handler(struct scic_sds_port *sci_port,
1523                                            struct scic_sds_phy *sci_phy)
1524 {
1525         default_port_handler(sci_port, __func__);
1526 }
1527
1528 static void scic_sds_port_default_link_down_handler(struct scic_sds_port *sci_port,
1529                                              struct scic_sds_phy *sci_phy)
1530 {
1531         default_port_handler(sci_port, __func__);
1532 }
1533
1534 static enum sci_status scic_sds_port_default_start_io_handler(struct scic_sds_port *sci_port,
1535                                                        struct scic_sds_remote_device *sci_dev,
1536                                                        struct scic_sds_request *sci_req)
1537 {
1538         return default_port_handler(sci_port, __func__);
1539 }
1540
1541 static enum sci_status scic_sds_port_default_complete_io_handler(struct scic_sds_port *sci_port,
1542                                                                  struct scic_sds_remote_device *sci_dev,
1543                                                                  struct scic_sds_request *sci_req)
1544 {
1545         return default_port_handler(sci_port, __func__);
1546 }
1547
1548 /*
1549  * ******************************************************************************
1550  * *  PORT STATE PRIVATE METHODS
1551  * ****************************************************************************** */
1552
1553 /**
1554  *
1555  * @sci_port: This is the struct scic_sds_port object to suspend.
1556  *
1557  * This method will susped the port task scheduler for this port object. none
1558  */
1559 static void
1560 scic_sds_port_suspend_port_task_scheduler(struct scic_sds_port *port)
1561 {
1562         u32 pts_control_value;
1563
1564         pts_control_value = readl(&port->port_task_scheduler_registers->control);
1565         pts_control_value |= SCU_PTSxCR_GEN_BIT(SUSPEND);
1566         writel(pts_control_value, &port->port_task_scheduler_registers->control);
1567 }
1568
1569 /**
1570  * scic_sds_port_post_dummy_request() - post dummy/workaround request
1571  * @sci_port: port to post task
1572  *
1573  * Prevent the hardware scheduler from posting new requests to the front
1574  * of the scheduler queue causing a starvation problem for currently
1575  * ongoing requests.
1576  *
1577  */
1578 static void scic_sds_port_post_dummy_request(struct scic_sds_port *sci_port)
1579 {
1580         u32 command;
1581         struct scu_task_context *task_context;
1582         struct scic_sds_controller *scic = sci_port->owning_controller;
1583         u16 tci = sci_port->reserved_tci;
1584
1585         task_context = scic_sds_controller_get_task_context_buffer(scic, tci);
1586
1587         task_context->abort = 0;
1588
1589         command = SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
1590                   sci_port->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT |
1591                   tci;
1592
1593         scic_sds_controller_post_request(scic, command);
1594 }
1595
1596 /**
1597  * This routine will abort the dummy request.  This will alow the hardware to
1598  * power down parts of the silicon to save power.
1599  *
1600  * @sci_port: The port on which the task must be aborted.
1601  *
1602  */
1603 static void scic_sds_port_abort_dummy_request(struct scic_sds_port *sci_port)
1604 {
1605         struct scic_sds_controller *scic = sci_port->owning_controller;
1606         u16 tci = sci_port->reserved_tci;
1607         struct scu_task_context *tc;
1608         u32 command;
1609
1610         tc = scic_sds_controller_get_task_context_buffer(scic, tci);
1611
1612         tc->abort = 1;
1613
1614         command = SCU_CONTEXT_COMMAND_REQUEST_POST_TC_ABORT |
1615                   sci_port->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT |
1616                   tci;
1617
1618         scic_sds_controller_post_request(scic, command);
1619 }
1620
1621 /**
1622  *
1623  * @sci_port: This is the struct scic_sds_port object to resume.
1624  *
1625  * This method will resume the port task scheduler for this port object. none
1626  */
1627 static void
1628 scic_sds_port_resume_port_task_scheduler(struct scic_sds_port *port)
1629 {
1630         u32 pts_control_value;
1631
1632         pts_control_value = readl(&port->port_task_scheduler_registers->control);
1633         pts_control_value &= ~SCU_PTSxCR_GEN_BIT(SUSPEND);
1634         writel(pts_control_value, &port->port_task_scheduler_registers->control);
1635 }
1636
1637 /*
1638  * ******************************************************************************
1639  * *  PORT READY SUBSTATE METHODS
1640  * ****************************************************************************** */
1641
1642 /**
1643  *
1644  * @object: This is the object which is cast to a struct scic_sds_port object.
1645  *
1646  * This method will perform the actions required by the struct scic_sds_port on
1647  * entering the SCIC_SDS_PORT_READY_SUBSTATE_WAITING. This function checks the
1648  * port for any ready phys.  If there is at least one phy in a ready state then
1649  * the port transitions to the ready operational substate. none
1650  */
1651 static void scic_sds_port_ready_substate_waiting_enter(void *object)
1652 {
1653         struct scic_sds_port *sci_port = object;
1654
1655         scic_sds_port_set_base_state_handlers(
1656                 sci_port, SCIC_SDS_PORT_READY_SUBSTATE_WAITING
1657                 );
1658
1659         scic_sds_port_suspend_port_task_scheduler(sci_port);
1660
1661         sci_port->not_ready_reason = SCIC_PORT_NOT_READY_NO_ACTIVE_PHYS;
1662
1663         if (sci_port->active_phy_mask != 0) {
1664                 /* At least one of the phys on the port is ready */
1665                 port_state_machine_change(sci_port,
1666                                           SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL);
1667         }
1668 }
1669
1670 /**
1671  *
1672  * @object: This is the object which is cast to a struct scic_sds_port object.
1673  *
1674  * This function will perform the actions required by the struct scic_sds_port
1675  * on entering the SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL. This function sets
1676  * the state handlers for the port object, notifies the SCI User that the port
1677  * is ready, and resumes port operations. none
1678  */
1679 static void scic_sds_port_ready_substate_operational_enter(void *object)
1680 {
1681         u32 index;
1682         struct scic_sds_port *sci_port = object;
1683         struct scic_sds_controller *scic = sci_port->owning_controller;
1684         struct isci_host *ihost = scic_to_ihost(scic);
1685         struct isci_port *iport = sci_port_to_iport(sci_port);
1686
1687         scic_sds_port_set_base_state_handlers(
1688                         sci_port,
1689                         SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL);
1690
1691         isci_port_ready(ihost, iport);
1692
1693         for (index = 0; index < SCI_MAX_PHYS; index++) {
1694                 if (sci_port->phy_table[index]) {
1695                         writel(sci_port->physical_port_index,
1696                                 &sci_port->port_pe_configuration_register[
1697                                         sci_port->phy_table[index]->phy_index]);
1698                 }
1699         }
1700
1701         scic_sds_port_update_viit_entry(sci_port);
1702
1703         scic_sds_port_resume_port_task_scheduler(sci_port);
1704
1705         /*
1706          * Post the dummy task for the port so the hardware can schedule
1707          * io correctly
1708          */
1709         scic_sds_port_post_dummy_request(sci_port);
1710 }
1711
1712 static void scic_sds_port_invalidate_dummy_remote_node(struct scic_sds_port *sci_port)
1713 {
1714         struct scic_sds_controller *scic = sci_port->owning_controller;
1715         u8 phys_index = sci_port->physical_port_index;
1716         union scu_remote_node_context *rnc;
1717         u16 rni = sci_port->reserved_rni;
1718         u32 command;
1719
1720         rnc = &scic->remote_node_context_table[rni];
1721
1722         rnc->ssp.is_valid = false;
1723
1724         /* ensure the preceding tc abort request has reached the
1725          * controller and give it ample time to act before posting the rnc
1726          * invalidate
1727          */
1728         readl(&scic->smu_registers->interrupt_status); /* flush */
1729         udelay(10);
1730
1731         command = SCU_CONTEXT_COMMAND_POST_RNC_INVALIDATE |
1732                   phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
1733
1734         scic_sds_controller_post_request(scic, command);
1735 }
1736
1737 /**
1738  *
1739  * @object: This is the object which is cast to a struct scic_sds_port object.
1740  *
1741  * This method will perform the actions required by the struct scic_sds_port on
1742  * exiting the SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL. This function reports
1743  * the port not ready and suspends the port task scheduler. none
1744  */
1745 static void scic_sds_port_ready_substate_operational_exit(void *object)
1746 {
1747         struct scic_sds_port *sci_port = object;
1748         struct scic_sds_controller *scic = sci_port->owning_controller;
1749         struct isci_host *ihost = scic_to_ihost(scic);
1750         struct isci_port *iport = sci_port_to_iport(sci_port);
1751
1752         /*
1753          * Kill the dummy task for this port if it has not yet posted
1754          * the hardware will treat this as a NOP and just return abort
1755          * complete.
1756          */
1757         scic_sds_port_abort_dummy_request(sci_port);
1758
1759         isci_port_not_ready(ihost, iport);
1760
1761         if (sci_port->ready_exit)
1762                 scic_sds_port_invalidate_dummy_remote_node(sci_port);
1763 }
1764
1765 /*
1766  * ******************************************************************************
1767  * *  PORT READY CONFIGURING METHODS
1768  * ****************************************************************************** */
1769
1770 /**
1771  * scic_sds_port_ready_substate_configuring_enter() -
1772  * @object: This is the object which is cast to a struct scic_sds_port object.
1773  *
1774  * This method will perform the actions required by the struct scic_sds_port on
1775  * exiting the SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL. This function reports
1776  * the port not ready and suspends the port task scheduler. none
1777  */
1778 static void scic_sds_port_ready_substate_configuring_enter(void *object)
1779 {
1780         struct scic_sds_port *sci_port = object;
1781         struct scic_sds_controller *scic = sci_port->owning_controller;
1782         struct isci_host *ihost = scic_to_ihost(scic);
1783         struct isci_port *iport = sci_port_to_iport(sci_port);
1784
1785         scic_sds_port_set_base_state_handlers(
1786                         sci_port,
1787                         SCIC_SDS_PORT_READY_SUBSTATE_CONFIGURING);
1788
1789         if (sci_port->active_phy_mask == 0) {
1790                 isci_port_not_ready(ihost, iport);
1791
1792                 port_state_machine_change(sci_port,
1793                                           SCIC_SDS_PORT_READY_SUBSTATE_WAITING);
1794         } else if (sci_port->started_request_count == 0)
1795                 port_state_machine_change(sci_port,
1796                                           SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL);
1797 }
1798
1799 static void scic_sds_port_ready_substate_configuring_exit(void *object)
1800 {
1801         struct scic_sds_port *sci_port = object;
1802
1803         scic_sds_port_suspend_port_task_scheduler(sci_port);
1804         if (sci_port->ready_exit)
1805                 scic_sds_port_invalidate_dummy_remote_node(sci_port);
1806 }
1807
1808 /* --------------------------------------------------------------------------- */
1809
1810 /**
1811  *
1812  * @port: This is the struct scic_sds_port object on which the io request count will
1813  *    be decremented.
1814  * @device: This is the struct scic_sds_remote_device object to which the io request
1815  *    is being directed.  This parameter is not required to complete this
1816  *    operation.
1817  * @io_request: This is the request that is being completed on this port
1818  *    object.  This parameter is not required to complete this operation.
1819  *
1820  * This is a general complete io request handler for the struct scic_sds_port object.
1821  * enum sci_status SCI_SUCCESS
1822  */
1823 static enum sci_status scic_sds_port_general_complete_io_handler(
1824         struct scic_sds_port *port,
1825         struct scic_sds_remote_device *device,
1826         struct scic_sds_request *io_request)
1827 {
1828         scic_sds_port_decrement_request_count(port);
1829
1830         return SCI_SUCCESS;
1831 }
1832
1833 /*
1834  * This method takes the struct scic_sds_port that is in a stopped state and handles
1835  * the destruct request.  The stopped state is the only state in which the
1836  * struct scic_sds_port can be destroyed.  This function causes the port object to
1837  * transition to the SCI_BASE_PORT_STATE_FINAL. enum sci_status SCI_SUCCESS
1838  */
1839 static enum sci_status scic_sds_port_stopped_state_destruct_handler(struct scic_sds_port *port)
1840 {
1841         port_state_machine_stop(port);
1842
1843         return SCI_SUCCESS;
1844 }
1845
1846 /*
1847  * This method takes the struct scic_sds_port that is in a stopped state and handles
1848  * the add phy request.  In MPC mode the only time a phy can be added to a port
1849  * is in the SCI_BASE_PORT_STATE_STOPPED. enum sci_status
1850  * SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION is returned when the phy can not
1851  * be added to the port. SCI_SUCCESS if the phy is added to the port.
1852  */
1853 static enum sci_status scic_sds_port_stopped_state_add_phy_handler(
1854         struct scic_sds_port *port,
1855         struct scic_sds_phy *phy)
1856 {
1857         struct sci_sas_address port_sas_address;
1858
1859         /* Read the port assigned SAS Address if there is one */
1860         scic_sds_port_get_sas_address(port, &port_sas_address);
1861
1862         if (port_sas_address.high != 0 && port_sas_address.low != 0) {
1863                 struct sci_sas_address phy_sas_address;
1864
1865                 /*
1866                  * Make sure that the PHY SAS Address matches the SAS Address
1867                  * for this port. */
1868                 scic_sds_phy_get_sas_address(phy, &phy_sas_address);
1869
1870                 if (
1871                         (port_sas_address.high != phy_sas_address.high)
1872                         || (port_sas_address.low  != phy_sas_address.low)
1873                         ) {
1874                         return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
1875                 }
1876         }
1877
1878         return scic_sds_port_set_phy(port, phy);
1879 }
1880
1881 /*
1882  * This method takes the struct scic_sds_port that is in a stopped state and handles
1883  * the remove phy request.  In MPC mode the only time a phy can be removed from
1884  * a port is in the SCI_BASE_PORT_STATE_STOPPED. enum sci_status
1885  * SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION is returned when the phy can not
1886  * be added to the port. SCI_SUCCESS if the phy is added to the port.
1887  */
1888 static enum sci_status scic_sds_port_stopped_state_remove_phy_handler(
1889         struct scic_sds_port *port,
1890         struct scic_sds_phy *phy)
1891 {
1892         return scic_sds_port_clear_phy(port, phy);
1893 }
1894
1895 /*
1896  * ****************************************************************************
1897  * *  READY STATE HANDLERS
1898  * **************************************************************************** */
1899
1900 /*
1901  * ****************************************************************************
1902  * *  RESETTING STATE HANDLERS
1903  * **************************************************************************** */
1904
1905 /*
1906  * ****************************************************************************
1907  * *  STOPPING STATE HANDLERS
1908  * **************************************************************************** */
1909
1910 /*
1911  * This method takes the struct scic_sds_port that is in a stopping state and handles
1912  * the complete io request. Should the request count reach 0 then the port
1913  * object will transition to the stopped state. enum sci_status SCI_SUCCESS
1914  */
1915 static enum sci_status scic_sds_port_stopping_state_complete_io_handler(
1916         struct scic_sds_port *sci_port,
1917         struct scic_sds_remote_device *device,
1918         struct scic_sds_request *io_request)
1919 {
1920         scic_sds_port_decrement_request_count(sci_port);
1921
1922         if (sci_port->started_request_count == 0)
1923                 port_state_machine_change(sci_port,
1924                                           SCI_BASE_PORT_STATE_STOPPED);
1925
1926         return SCI_SUCCESS;
1927 }
1928
1929 /*
1930  * ****************************************************************************
1931  * *  RESETTING STATE HANDLERS
1932  * **************************************************************************** */
1933
1934 /*
1935  * This method will transition a failed port to its ready state.  The port
1936  * failed because a hard reset request timed out but at some time later one or
1937  * more phys in the port became ready. enum sci_status SCI_SUCCESS
1938  */
1939 static void scic_sds_port_reset_state_link_up_handler(
1940         struct scic_sds_port *port,
1941         struct scic_sds_phy *phy)
1942 {
1943         /*
1944          * / @todo We should make sure that the phy that has gone link up is the same
1945          * /       one on which we sent the reset.  It is possible that the phy on
1946          * /       which we sent the reset is not the one that has gone link up and we
1947          * /       want to make sure that phy being reset comes back.  Consider the
1948          * /       case where a reset is sent but before the hardware processes the
1949          * /       reset it get a link up on the port because of a hot plug event.
1950          * /       because of the reset request this phy will go link down almost
1951          * /       immediately. */
1952
1953         /*
1954          * In the resetting state we don't notify the user regarding
1955          * link up and link down notifications. */
1956         scic_sds_port_general_link_up_handler(port, phy, false);
1957 }
1958
1959 /*
1960  * This method process link down notifications that occur during a port reset
1961  * operation. Link downs can occur during the reset operation. enum sci_status
1962  * SCI_SUCCESS
1963  */
1964 static void scic_sds_port_reset_state_link_down_handler(
1965         struct scic_sds_port *port,
1966         struct scic_sds_phy *phy)
1967 {
1968         /*
1969          * In the resetting state we don't notify the user regarding
1970          * link up and link down notifications. */
1971         scic_sds_port_deactivate_phy(port, phy, false);
1972 }
1973
1974 enum sci_status scic_sds_port_start(struct scic_sds_port *sci_port)
1975 {
1976         struct scic_sds_controller *scic = sci_port->owning_controller;
1977         struct isci_host *ihost = scic_to_ihost(scic);
1978         enum sci_status status = SCI_SUCCESS;
1979         enum scic_sds_port_states state;
1980         u32 phy_mask;
1981
1982         state = sci_port->state_machine.current_state_id;
1983         if (state != SCI_BASE_PORT_STATE_STOPPED) {
1984                 dev_warn(sciport_to_dev(sci_port),
1985                          "%s: in wrong state: %d\n", __func__, state);
1986                 return SCI_FAILURE_INVALID_STATE;
1987         }
1988
1989         if (sci_port->assigned_device_count > 0) {
1990                 /* TODO This is a start failure operation because
1991                  * there are still devices assigned to this port.
1992                  * There must be no devices assigned to a port on a
1993                  * start operation.
1994                  */
1995                 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
1996         }
1997
1998         sci_port->timer_handle =
1999                 isci_timer_create(ihost,
2000                                   sci_port,
2001                                   scic_sds_port_timeout_handler);
2002
2003         if (!sci_port->timer_handle)
2004                 return SCI_FAILURE_INSUFFICIENT_RESOURCES;
2005
2006         if (sci_port->reserved_rni == SCU_DUMMY_INDEX) {
2007                 u16 rni = scic_sds_remote_node_table_allocate_remote_node(
2008                                 &scic->available_remote_nodes, 1);
2009
2010                 if (rni != SCU_DUMMY_INDEX)
2011                         scic_sds_port_construct_dummy_rnc(sci_port, rni);
2012                 else
2013                         status = SCI_FAILURE_INSUFFICIENT_RESOURCES;
2014                 sci_port->reserved_rni = rni;
2015         }
2016
2017         if (sci_port->reserved_tci == SCU_DUMMY_INDEX) {
2018                 /* Allocate a TCI and remove the sequence nibble */
2019                 u16 tci = scic_controller_allocate_io_tag(scic);
2020
2021                 if (tci != SCU_DUMMY_INDEX)
2022                         scic_sds_port_construct_dummy_task(sci_port, tci);
2023                 else
2024                         status = SCI_FAILURE_INSUFFICIENT_RESOURCES;
2025                 sci_port->reserved_tci = tci;
2026         }
2027
2028         if (status == SCI_SUCCESS) {
2029                 phy_mask = scic_sds_port_get_phys(sci_port);
2030
2031                 /*
2032                  * There are one or more phys assigned to this port.  Make sure
2033                  * the port's phy mask is in fact legal and supported by the
2034                  * silicon.
2035                  */
2036                 if (scic_sds_port_is_phy_mask_valid(sci_port, phy_mask) == true) {
2037                         port_state_machine_change(sci_port,
2038                                                   SCI_BASE_PORT_STATE_READY);
2039
2040                         return SCI_SUCCESS;
2041                 }
2042                 status = SCI_FAILURE;
2043         }
2044
2045         if (status != SCI_SUCCESS)
2046                 scic_sds_port_destroy_dummy_resources(sci_port);
2047
2048         return status;
2049 }
2050
2051 enum sci_status scic_sds_port_stop(struct scic_sds_port *sci_port)
2052 {
2053         enum scic_sds_port_states state;
2054
2055         state = sci_port->state_machine.current_state_id;
2056         switch (state) {
2057         case SCI_BASE_PORT_STATE_STOPPED:
2058                 return SCI_SUCCESS;
2059         case SCIC_SDS_PORT_READY_SUBSTATE_WAITING:
2060         case SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL:
2061         case SCIC_SDS_PORT_READY_SUBSTATE_CONFIGURING:
2062         case SCI_BASE_PORT_STATE_RESETTING:
2063                 port_state_machine_change(sci_port,
2064                                           SCI_BASE_PORT_STATE_STOPPING);
2065                 return SCI_SUCCESS;
2066         default:
2067                 dev_warn(sciport_to_dev(sci_port),
2068                          "%s: in wrong state: %d\n", __func__, state);
2069                 return SCI_FAILURE_INVALID_STATE;
2070         }
2071 }
2072
2073 static struct scic_sds_port_state_handler scic_sds_port_state_handler_table[] = {
2074         [SCI_BASE_PORT_STATE_STOPPED] = {
2075                 .destruct_handler       = scic_sds_port_stopped_state_destruct_handler,
2076                 .reset_handler          = scic_sds_port_default_reset_handler,
2077                 .add_phy_handler        = scic_sds_port_stopped_state_add_phy_handler,
2078                 .remove_phy_handler     = scic_sds_port_stopped_state_remove_phy_handler,
2079                 .frame_handler          = scic_sds_port_default_frame_handler,
2080                 .event_handler          = scic_sds_port_default_event_handler,
2081                 .link_up_handler        = scic_sds_port_default_link_up_handler,
2082                 .link_down_handler      = scic_sds_port_default_link_down_handler,
2083                 .start_io_handler       = scic_sds_port_default_start_io_handler,
2084                 .complete_io_handler    = scic_sds_port_default_complete_io_handler
2085         },
2086         [SCI_BASE_PORT_STATE_STOPPING] = {
2087                 .destruct_handler       = scic_sds_port_default_destruct_handler,
2088                 .reset_handler          = scic_sds_port_default_reset_handler,
2089                 .add_phy_handler        = scic_sds_port_default_add_phy_handler,
2090                 .remove_phy_handler     = scic_sds_port_default_remove_phy_handler,
2091                 .frame_handler          = scic_sds_port_default_frame_handler,
2092                 .event_handler          = scic_sds_port_default_event_handler,
2093                 .link_up_handler        = scic_sds_port_default_link_up_handler,
2094                 .link_down_handler      = scic_sds_port_default_link_down_handler,
2095                 .start_io_handler       = scic_sds_port_default_start_io_handler,
2096                 .complete_io_handler    = scic_sds_port_stopping_state_complete_io_handler
2097         },
2098         [SCI_BASE_PORT_STATE_READY] = {
2099                 .destruct_handler       = scic_sds_port_default_destruct_handler,
2100                 .reset_handler          = scic_sds_port_default_reset_handler,
2101                 .add_phy_handler        = scic_sds_port_default_add_phy_handler,
2102                 .remove_phy_handler     = scic_sds_port_default_remove_phy_handler,
2103                 .frame_handler          = scic_sds_port_default_frame_handler,
2104                 .event_handler          = scic_sds_port_default_event_handler,
2105                 .link_up_handler        = scic_sds_port_default_link_up_handler,
2106                 .link_down_handler      = scic_sds_port_default_link_down_handler,
2107                 .start_io_handler       = scic_sds_port_default_start_io_handler,
2108                 .complete_io_handler    = scic_sds_port_general_complete_io_handler
2109         },
2110         [SCIC_SDS_PORT_READY_SUBSTATE_WAITING] = {
2111                 .destruct_handler       = scic_sds_port_default_destruct_handler,
2112                 .reset_handler          = scic_sds_port_default_reset_handler,
2113                 .add_phy_handler        = scic_sds_port_ready_substate_add_phy_handler,
2114                 .remove_phy_handler     = scic_sds_port_default_remove_phy_handler,
2115                 .frame_handler          = scic_sds_port_default_frame_handler,
2116                 .event_handler          = scic_sds_port_default_event_handler,
2117                 .link_up_handler        = scic_sds_port_ready_waiting_substate_link_up_handler,
2118                 .link_down_handler      = scic_sds_port_default_link_down_handler,
2119                 .start_io_handler       = scic_sds_port_ready_waiting_substate_start_io_handler,
2120                 .complete_io_handler    = scic_sds_port_ready_substate_complete_io_handler,
2121         },
2122         [SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL] = {
2123                 .destruct_handler       = scic_sds_port_default_destruct_handler,
2124                 .reset_handler          = scic_sds_port_ready_operational_substate_reset_handler,
2125                 .add_phy_handler        = scic_sds_port_ready_substate_add_phy_handler,
2126                 .remove_phy_handler     = scic_sds_port_ready_substate_remove_phy_handler,
2127                 .frame_handler          = scic_sds_port_default_frame_handler,
2128                 .event_handler          = scic_sds_port_default_event_handler,
2129                 .link_up_handler        = scic_sds_port_ready_operational_substate_link_up_handler,
2130                 .link_down_handler      = scic_sds_port_ready_operational_substate_link_down_handler,
2131                 .start_io_handler       = scic_sds_port_ready_operational_substate_start_io_handler,
2132                 .complete_io_handler    = scic_sds_port_ready_substate_complete_io_handler,
2133         },
2134         [SCIC_SDS_PORT_READY_SUBSTATE_CONFIGURING] = {
2135                 .destruct_handler       = scic_sds_port_default_destruct_handler,
2136                 .reset_handler          = scic_sds_port_default_reset_handler,
2137                 .add_phy_handler        = scic_sds_port_ready_configuring_substate_add_phy_handler,
2138                 .remove_phy_handler     = scic_sds_port_ready_configuring_substate_remove_phy_handler,
2139                 .frame_handler          = scic_sds_port_default_frame_handler,
2140                 .event_handler          = scic_sds_port_default_event_handler,
2141                 .link_up_handler        = scic_sds_port_default_link_up_handler,
2142                 .link_down_handler      = scic_sds_port_default_link_down_handler,
2143                 .start_io_handler       = scic_sds_port_default_start_io_handler,
2144                 .complete_io_handler    = scic_sds_port_ready_configuring_substate_complete_io_handler
2145         },
2146         [SCI_BASE_PORT_STATE_RESETTING] = {
2147                 .destruct_handler       = scic_sds_port_default_destruct_handler,
2148                 .reset_handler          = scic_sds_port_default_reset_handler,
2149                 .add_phy_handler        = scic_sds_port_default_add_phy_handler,
2150                 .remove_phy_handler     = scic_sds_port_default_remove_phy_handler,
2151                 .frame_handler          = scic_sds_port_default_frame_handler,
2152                 .event_handler          = scic_sds_port_default_event_handler,
2153                 .link_up_handler        = scic_sds_port_reset_state_link_up_handler,
2154                 .link_down_handler      = scic_sds_port_reset_state_link_down_handler,
2155                 .start_io_handler       = scic_sds_port_default_start_io_handler,
2156                 .complete_io_handler    = scic_sds_port_general_complete_io_handler
2157         },
2158         [SCI_BASE_PORT_STATE_FAILED] = {
2159                 .destruct_handler       = scic_sds_port_default_destruct_handler,
2160                 .reset_handler          = scic_sds_port_default_reset_handler,
2161                 .add_phy_handler        = scic_sds_port_default_add_phy_handler,
2162                 .remove_phy_handler     = scic_sds_port_default_remove_phy_handler,
2163                 .frame_handler          = scic_sds_port_default_frame_handler,
2164                 .event_handler          = scic_sds_port_default_event_handler,
2165                 .link_up_handler        = scic_sds_port_default_link_up_handler,
2166                 .link_down_handler      = scic_sds_port_default_link_down_handler,
2167                 .start_io_handler       = scic_sds_port_default_start_io_handler,
2168                 .complete_io_handler    = scic_sds_port_general_complete_io_handler
2169         }
2170 };
2171
2172 /*
2173  * ******************************************************************************
2174  * *  PORT STATE PRIVATE METHODS
2175  * ****************************************************************************** */
2176
2177 /**
2178  *
2179  * @sci_port: This is the port object which to suspend.
2180  *
2181  * This method will enable the SCU Port Task Scheduler for this port object but
2182  * will leave the port task scheduler in a suspended state. none
2183  */
2184 static void
2185 scic_sds_port_enable_port_task_scheduler(struct scic_sds_port *port)
2186 {
2187         u32 pts_control_value;
2188
2189         pts_control_value = readl(&port->port_task_scheduler_registers->control);
2190         pts_control_value |= SCU_PTSxCR_GEN_BIT(ENABLE) | SCU_PTSxCR_GEN_BIT(SUSPEND);
2191         writel(pts_control_value, &port->port_task_scheduler_registers->control);
2192 }
2193
2194 /**
2195  *
2196  * @sci_port: This is the port object which to resume.
2197  *
2198  * This method will disable the SCU port task scheduler for this port object.
2199  * none
2200  */
2201 static void
2202 scic_sds_port_disable_port_task_scheduler(struct scic_sds_port *port)
2203 {
2204         u32 pts_control_value;
2205
2206         pts_control_value = readl(&port->port_task_scheduler_registers->control);
2207         pts_control_value &=
2208                 ~(SCU_PTSxCR_GEN_BIT(ENABLE) | SCU_PTSxCR_GEN_BIT(SUSPEND));
2209         writel(pts_control_value, &port->port_task_scheduler_registers->control);
2210 }
2211
2212 static void scic_sds_port_post_dummy_remote_node(struct scic_sds_port *sci_port)
2213 {
2214         struct scic_sds_controller *scic = sci_port->owning_controller;
2215         u8 phys_index = sci_port->physical_port_index;
2216         union scu_remote_node_context *rnc;
2217         u16 rni = sci_port->reserved_rni;
2218         u32 command;
2219
2220         rnc = &scic->remote_node_context_table[rni];
2221         rnc->ssp.is_valid = true;
2222
2223         command = SCU_CONTEXT_COMMAND_POST_RNC_32 |
2224                   phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
2225
2226         scic_sds_controller_post_request(scic, command);
2227
2228         /* ensure hardware has seen the post rnc command and give it
2229          * ample time to act before sending the suspend
2230          */
2231         readl(&scic->smu_registers->interrupt_status); /* flush */
2232         udelay(10);
2233
2234         command = SCU_CONTEXT_COMMAND_POST_RNC_SUSPEND_TX_RX |
2235                   phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
2236
2237         scic_sds_controller_post_request(scic, command);
2238 }
2239
2240 /*
2241  * ******************************************************************************
2242  * *  PORT STATE METHODS
2243  * ****************************************************************************** */
2244
2245 /**
2246  *
2247  * @object: This is the object which is cast to a struct scic_sds_port object.
2248  *
2249  * This method will perform the actions required by the struct scic_sds_port on
2250  * entering the SCI_BASE_PORT_STATE_STOPPED. This function sets the stopped
2251  * state handlers for the struct scic_sds_port object and disables the port task
2252  * scheduler in the hardware. none
2253  */
2254 static void scic_sds_port_stopped_state_enter(void *object)
2255 {
2256         struct scic_sds_port *sci_port = object;
2257
2258         scic_sds_port_set_base_state_handlers(
2259                 sci_port, SCI_BASE_PORT_STATE_STOPPED
2260                 );
2261
2262         if (
2263                 SCI_BASE_PORT_STATE_STOPPING
2264                 == sci_port->state_machine.previous_state_id
2265                 ) {
2266                 /*
2267                  * If we enter this state becasuse of a request to stop
2268                  * the port then we want to disable the hardwares port
2269                  * task scheduler. */
2270                 scic_sds_port_disable_port_task_scheduler(sci_port);
2271         }
2272 }
2273
2274 /**
2275  *
2276  * @object: This is the object which is cast to a struct scic_sds_port object.
2277  *
2278  * This method will perform the actions required by the struct scic_sds_port on
2279  * exiting the SCI_BASE_STATE_STOPPED. This function enables the SCU hardware
2280  * port task scheduler. none
2281  */
2282 static void scic_sds_port_stopped_state_exit(void *object)
2283 {
2284         struct scic_sds_port *sci_port = object;
2285
2286         /* Enable and suspend the port task scheduler */
2287         scic_sds_port_enable_port_task_scheduler(sci_port);
2288 }
2289
2290 /**
2291  * scic_sds_port_ready_state_enter -
2292  * @object: This is the object which is cast to a struct scic_sds_port object.
2293  *
2294  * This method will perform the actions required by the struct scic_sds_port on
2295  * entering the SCI_BASE_PORT_STATE_READY. This function sets the ready state
2296  * handlers for the struct scic_sds_port object, reports the port object as
2297  * not ready and starts the ready substate machine. none
2298  */
2299 static void scic_sds_port_ready_state_enter(void *object)
2300 {
2301         struct scic_sds_port *sci_port = object;
2302         struct scic_sds_controller *scic = sci_port->owning_controller;
2303         struct isci_host *ihost = scic_to_ihost(scic);
2304         struct isci_port *iport = sci_port_to_iport(sci_port);
2305         u32 prev_state;
2306
2307         /* Put the ready state handlers in place though they will not be there long */
2308         scic_sds_port_set_base_state_handlers(sci_port, SCI_BASE_PORT_STATE_READY);
2309
2310         prev_state = sci_port->state_machine.previous_state_id;
2311         if (prev_state  == SCI_BASE_PORT_STATE_RESETTING)
2312                 isci_port_hard_reset_complete(iport, SCI_SUCCESS);
2313         else
2314                 isci_port_not_ready(ihost, iport);
2315
2316         /* Post and suspend the dummy remote node context for this port. */
2317         scic_sds_port_post_dummy_remote_node(sci_port);
2318
2319         /* Start the ready substate machine */
2320         port_state_machine_change(sci_port,
2321                                   SCIC_SDS_PORT_READY_SUBSTATE_WAITING);
2322 }
2323
2324 /**
2325  *
2326  * @object: This is the object which is cast to a struct scic_sds_port object.
2327  *
2328  * This method will perform the actions required by the struct scic_sds_port on
2329  * entering the SCI_BASE_PORT_STATE_RESETTING. This function sets the resetting
2330  * state handlers for the struct scic_sds_port object. none
2331  */
2332 static void scic_sds_port_resetting_state_enter(void *object)
2333 {
2334         struct scic_sds_port *sci_port = object;
2335
2336         scic_sds_port_set_base_state_handlers(
2337                 sci_port, SCI_BASE_PORT_STATE_RESETTING
2338                 );
2339 }
2340
2341 /**
2342  *
2343  * @object: This is the object which is cast to a struct scic_sds_port object.
2344  *
2345  * This function will perform the actions required by the
2346  * struct scic_sds_port on
2347  * exiting the SCI_BASE_STATE_RESETTING. This function does nothing. none
2348  */
2349 static inline void scic_sds_port_resetting_state_exit(void *object)
2350 {
2351         struct scic_sds_port *sci_port = object;
2352
2353         isci_timer_stop(sci_port->timer_handle);
2354 }
2355
2356 /**
2357  *
2358  * @object: This is the void object which is cast to a
2359  * struct scic_sds_port object.
2360  *
2361  * This method will perform the actions required by the struct scic_sds_port on
2362  * entering the SCI_BASE_PORT_STATE_STOPPING. This function sets the stopping
2363  * state handlers for the struct scic_sds_port object. none
2364  */
2365 static void scic_sds_port_stopping_state_enter(void *object)
2366 {
2367         struct scic_sds_port *sci_port = object;
2368
2369         scic_sds_port_set_base_state_handlers(
2370                 sci_port, SCI_BASE_PORT_STATE_STOPPING
2371                 );
2372 }
2373
2374 /**
2375  *
2376  * @object: This is the object which is cast to a struct scic_sds_port object.
2377  *
2378  * This function will perform the actions required by the
2379  * struct scic_sds_port on
2380  * exiting the SCI_BASE_STATE_STOPPING. This function does nothing. none
2381  */
2382 static inline void
2383 scic_sds_port_stopping_state_exit(void *object)
2384 {
2385         struct scic_sds_port *sci_port = object;
2386
2387         isci_timer_stop(sci_port->timer_handle);
2388
2389         scic_sds_port_destroy_dummy_resources(sci_port);
2390 }
2391
2392 /**
2393  *
2394  * @object: This is the object which is cast to a struct scic_sds_port object.
2395  *
2396  * This function will perform the actions required by the
2397  * struct scic_sds_port on
2398  * entering the SCI_BASE_PORT_STATE_STOPPING. This function sets the stopping
2399  * state handlers for the struct scic_sds_port object. none
2400  */
2401 static void scic_sds_port_failed_state_enter(void *object)
2402 {
2403         struct scic_sds_port *sci_port = object;
2404         struct isci_port *iport = sci_port_to_iport(sci_port);
2405
2406         scic_sds_port_set_base_state_handlers(sci_port,
2407                                               SCI_BASE_PORT_STATE_FAILED);
2408
2409         isci_port_hard_reset_complete(iport, SCI_FAILURE_TIMEOUT);
2410 }
2411
2412 /* --------------------------------------------------------------------------- */
2413
2414 static const struct sci_base_state scic_sds_port_state_table[] = {
2415         [SCI_BASE_PORT_STATE_STOPPED] = {
2416                 .enter_state = scic_sds_port_stopped_state_enter,
2417                 .exit_state  = scic_sds_port_stopped_state_exit
2418         },
2419         [SCI_BASE_PORT_STATE_STOPPING] = {
2420                 .enter_state = scic_sds_port_stopping_state_enter,
2421                 .exit_state  = scic_sds_port_stopping_state_exit
2422         },
2423         [SCI_BASE_PORT_STATE_READY] = {
2424                 .enter_state = scic_sds_port_ready_state_enter,
2425         },
2426         [SCIC_SDS_PORT_READY_SUBSTATE_WAITING] = {
2427                 .enter_state = scic_sds_port_ready_substate_waiting_enter,
2428         },
2429         [SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL] = {
2430                 .enter_state = scic_sds_port_ready_substate_operational_enter,
2431                 .exit_state  = scic_sds_port_ready_substate_operational_exit
2432         },
2433         [SCIC_SDS_PORT_READY_SUBSTATE_CONFIGURING] = {
2434                 .enter_state = scic_sds_port_ready_substate_configuring_enter,
2435                 .exit_state  = scic_sds_port_ready_substate_configuring_exit
2436         },
2437         [SCI_BASE_PORT_STATE_RESETTING] = {
2438                 .enter_state = scic_sds_port_resetting_state_enter,
2439                 .exit_state  = scic_sds_port_resetting_state_exit
2440         },
2441         [SCI_BASE_PORT_STATE_FAILED] = {
2442                 .enter_state = scic_sds_port_failed_state_enter,
2443         }
2444 };
2445
2446 void scic_sds_port_construct(struct scic_sds_port *sci_port, u8 index,
2447                              struct scic_sds_controller *scic)
2448 {
2449         sci_base_state_machine_construct(&sci_port->state_machine,
2450                                          sci_port,
2451                                          scic_sds_port_state_table,
2452                                          SCI_BASE_PORT_STATE_STOPPED);
2453
2454         sci_base_state_machine_start(&sci_port->state_machine);
2455
2456         sci_port->logical_port_index  = SCIC_SDS_DUMMY_PORT;
2457         sci_port->physical_port_index = index;
2458         sci_port->active_phy_mask     = 0;
2459         sci_port->ready_exit          = false;
2460
2461         sci_port->owning_controller = scic;
2462
2463         sci_port->started_request_count = 0;
2464         sci_port->assigned_device_count = 0;
2465
2466         sci_port->reserved_rni = SCU_DUMMY_INDEX;
2467         sci_port->reserved_tci = SCU_DUMMY_INDEX;
2468
2469         sci_port->timer_handle = NULL;
2470         sci_port->port_task_scheduler_registers = NULL;
2471
2472         for (index = 0; index < SCI_MAX_PHYS; index++)
2473                 sci_port->phy_table[index] = NULL;
2474 }
2475
2476 void isci_port_init(struct isci_port *iport, struct isci_host *ihost, int index)
2477 {
2478         INIT_LIST_HEAD(&iport->remote_dev_list);
2479         INIT_LIST_HEAD(&iport->domain_dev_list);
2480         spin_lock_init(&iport->state_lock);
2481         init_completion(&iport->start_complete);
2482         iport->isci_host = ihost;
2483         isci_port_change_state(iport, isci_freed);
2484 }
2485
2486 /**
2487  * isci_port_get_state() - This function gets the status of the port object.
2488  * @isci_port: This parameter points to the isci_port object
2489  *
2490  * status of the object as a isci_status enum.
2491  */
2492 enum isci_status isci_port_get_state(
2493         struct isci_port *isci_port)
2494 {
2495         return isci_port->status;
2496 }
2497
2498 static void isci_port_bc_change_received(struct isci_host *ihost,
2499                                          struct scic_sds_port *sci_port,
2500                                          struct scic_sds_phy *sci_phy)
2501 {
2502         struct isci_phy *iphy = sci_phy_to_iphy(sci_phy);
2503
2504         dev_dbg(&ihost->pdev->dev, "%s: iphy = %p, sas_phy = %p\n",
2505                 __func__, iphy, &iphy->sas_phy);
2506
2507         ihost->sas_ha.notify_port_event(&iphy->sas_phy, PORTE_BROADCAST_RCVD);
2508         scic_port_enable_broadcast_change_notification(sci_port);
2509 }
2510
2511 void scic_sds_port_broadcast_change_received(
2512         struct scic_sds_port *sci_port,
2513         struct scic_sds_phy *sci_phy)
2514 {
2515         struct scic_sds_controller *scic = sci_port->owning_controller;
2516         struct isci_host *ihost = scic_to_ihost(scic);
2517
2518         /* notify the user. */
2519         isci_port_bc_change_received(ihost, sci_port, sci_phy);
2520 }
2521
2522 int isci_port_perform_hard_reset(struct isci_host *ihost, struct isci_port *iport,
2523                                  struct isci_phy *iphy)
2524 {
2525         unsigned long flags;
2526         enum sci_status status;
2527         int ret = TMF_RESP_FUNC_COMPLETE;
2528
2529         dev_dbg(&ihost->pdev->dev, "%s: iport = %p\n",
2530                 __func__, iport);
2531
2532         init_completion(&iport->hard_reset_complete);
2533
2534         spin_lock_irqsave(&ihost->scic_lock, flags);
2535
2536         #define ISCI_PORT_RESET_TIMEOUT SCIC_SDS_SIGNATURE_FIS_TIMEOUT
2537         status = scic_port_hard_reset(&iport->sci, ISCI_PORT_RESET_TIMEOUT);
2538
2539         spin_unlock_irqrestore(&ihost->scic_lock, flags);
2540
2541         if (status == SCI_SUCCESS) {
2542                 wait_for_completion(&iport->hard_reset_complete);
2543
2544                 dev_dbg(&ihost->pdev->dev,
2545                         "%s: iport = %p; hard reset completion\n",
2546                         __func__, iport);
2547
2548                 if (iport->hard_reset_status != SCI_SUCCESS)
2549                         ret = TMF_RESP_FUNC_FAILED;
2550         } else {
2551                 ret = TMF_RESP_FUNC_FAILED;
2552
2553                 dev_err(&ihost->pdev->dev,
2554                         "%s: iport = %p; scic_port_hard_reset call"
2555                         " failed 0x%x\n",
2556                         __func__, iport, status);
2557
2558         }
2559
2560         /* If the hard reset for the port has failed, consider this
2561          * the same as link failures on all phys in the port.
2562          */
2563         if (ret != TMF_RESP_FUNC_COMPLETE) {
2564                 dev_err(&ihost->pdev->dev,
2565                         "%s: iport = %p; hard reset failed "
2566                         "(0x%x) - sending link down to libsas for phy %p\n",
2567                         __func__, iport, iport->hard_reset_status, iphy);
2568
2569                 isci_port_link_down(ihost, iphy, iport);
2570         }
2571
2572         return ret;
2573 }
2574
2575 /**
2576  * isci_port_deformed() - This function is called by libsas when a port becomes
2577  *    inactive.
2578  * @phy: This parameter specifies the libsas phy with the inactive port.
2579  *
2580  */
2581 void isci_port_deformed(struct asd_sas_phy *phy)
2582 {
2583         pr_debug("%s: sas_phy = %p\n", __func__, phy);
2584 }
2585
2586 /**
2587  * isci_port_formed() - This function is called by libsas when a port becomes
2588  *    active.
2589  * @phy: This parameter specifies the libsas phy with the active port.
2590  *
2591  */
2592 void isci_port_formed(struct asd_sas_phy *phy)
2593 {
2594         pr_debug("%s: sas_phy = %p, sas_port = %p\n", __func__, phy, phy->port);
2595 }