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