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