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