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