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