WIP FPC-III support
[linux/fpc-iii.git] / drivers / scsi / mpt3sas / mpt3sas_transport.c
blob6f470822475516afd9a36edba2abd6d517f6bde8
1 /*
2 * SAS Transport Layer for MPT (Message Passing Technology) based controllers
4 * This code is based on drivers/scsi/mpt3sas/mpt3sas_transport.c
5 * Copyright (C) 2012-2014 LSI Corporation
6 * Copyright (C) 2013-2014 Avago Technologies
7 * (mailto: MPT-FusionLinux.pdl@avagotech.com)
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * NO WARRANTY
20 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
21 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
22 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
23 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
24 * solely responsible for determining the appropriateness of using and
25 * distributing the Program and assumes all risks associated with its
26 * exercise of rights under this Agreement, including but not limited to
27 * the risks and costs of program errors, damage to or loss of data,
28 * programs or equipment, and unavailability or interruption of operations.
30 * DISCLAIMER OF LIABILITY
31 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
32 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
34 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
35 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
36 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
37 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
39 * You should have received a copy of the GNU General Public License
40 * along with this program; if not, write to the Free Software
41 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
42 * USA.
45 #include <linux/module.h>
46 #include <linux/kernel.h>
47 #include <linux/init.h>
48 #include <linux/errno.h>
49 #include <linux/sched.h>
50 #include <linux/workqueue.h>
51 #include <linux/delay.h>
52 #include <linux/pci.h>
54 #include <scsi/scsi.h>
55 #include <scsi/scsi_cmnd.h>
56 #include <scsi/scsi_device.h>
57 #include <scsi/scsi_host.h>
58 #include <scsi/scsi_transport_sas.h>
59 #include <scsi/scsi_dbg.h>
61 #include "mpt3sas_base.h"
63 /**
64 * _transport_get_port_id_by_sas_phy - get zone's port id that Phy belong to
65 * @phy - sas_phy object
67 * Return Port number
69 static inline u8
70 _transport_get_port_id_by_sas_phy(struct sas_phy *phy)
72 u8 port_id = 0xFF;
73 struct hba_port *port = phy->hostdata;
75 if (port)
76 port_id = port->port_id;
78 return port_id;
81 /**
82 * _transport_sas_node_find_by_sas_address - sas node search
83 * @ioc: per adapter object
84 * @sas_address: sas address of expander or sas host
85 * @port: hba port entry
86 * Context: Calling function should acquire ioc->sas_node_lock.
88 * Search for either hba phys or expander device based on handle, then returns
89 * the sas_node object.
91 static struct _sas_node *
92 _transport_sas_node_find_by_sas_address(struct MPT3SAS_ADAPTER *ioc,
93 u64 sas_address, struct hba_port *port)
95 if (ioc->sas_hba.sas_address == sas_address)
96 return &ioc->sas_hba;
97 else
98 return mpt3sas_scsih_expander_find_by_sas_address(ioc,
99 sas_address, port);
103 * _transport_get_port_id_by_rphy - Get Port number from rphy object
104 * @ioc: per adapter object
105 * @rphy: sas_rphy object
107 * Returns Port number.
109 static u8
110 _transport_get_port_id_by_rphy(struct MPT3SAS_ADAPTER *ioc,
111 struct sas_rphy *rphy)
113 struct _sas_node *sas_expander;
114 struct _sas_device *sas_device;
115 unsigned long flags;
116 u8 port_id = 0xFF;
118 if (!rphy)
119 return port_id;
121 if (rphy->identify.device_type == SAS_EDGE_EXPANDER_DEVICE ||
122 rphy->identify.device_type == SAS_FANOUT_EXPANDER_DEVICE) {
123 spin_lock_irqsave(&ioc->sas_node_lock, flags);
124 list_for_each_entry(sas_expander,
125 &ioc->sas_expander_list, list) {
126 if (sas_expander->rphy == rphy) {
127 port_id = sas_expander->port->port_id;
128 break;
131 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
132 } else if (rphy->identify.device_type == SAS_END_DEVICE) {
133 spin_lock_irqsave(&ioc->sas_device_lock, flags);
134 sas_device = __mpt3sas_get_sdev_by_rphy(ioc, rphy);
135 if (sas_device) {
136 port_id = sas_device->port->port_id;
137 sas_device_put(sas_device);
139 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
142 return port_id;
146 * _transport_convert_phy_link_rate -
147 * @link_rate: link rate returned from mpt firmware
149 * Convert link_rate from mpi fusion into sas_transport form.
151 static enum sas_linkrate
152 _transport_convert_phy_link_rate(u8 link_rate)
154 enum sas_linkrate rc;
156 switch (link_rate) {
157 case MPI2_SAS_NEG_LINK_RATE_1_5:
158 rc = SAS_LINK_RATE_1_5_GBPS;
159 break;
160 case MPI2_SAS_NEG_LINK_RATE_3_0:
161 rc = SAS_LINK_RATE_3_0_GBPS;
162 break;
163 case MPI2_SAS_NEG_LINK_RATE_6_0:
164 rc = SAS_LINK_RATE_6_0_GBPS;
165 break;
166 case MPI25_SAS_NEG_LINK_RATE_12_0:
167 rc = SAS_LINK_RATE_12_0_GBPS;
168 break;
169 case MPI2_SAS_NEG_LINK_RATE_PHY_DISABLED:
170 rc = SAS_PHY_DISABLED;
171 break;
172 case MPI2_SAS_NEG_LINK_RATE_NEGOTIATION_FAILED:
173 rc = SAS_LINK_RATE_FAILED;
174 break;
175 case MPI2_SAS_NEG_LINK_RATE_PORT_SELECTOR:
176 rc = SAS_SATA_PORT_SELECTOR;
177 break;
178 case MPI2_SAS_NEG_LINK_RATE_SMP_RESET_IN_PROGRESS:
179 rc = SAS_PHY_RESET_IN_PROGRESS;
180 break;
182 default:
183 case MPI2_SAS_NEG_LINK_RATE_SATA_OOB_COMPLETE:
184 case MPI2_SAS_NEG_LINK_RATE_UNKNOWN_LINK_RATE:
185 rc = SAS_LINK_RATE_UNKNOWN;
186 break;
188 return rc;
192 * _transport_set_identify - set identify for phys and end devices
193 * @ioc: per adapter object
194 * @handle: device handle
195 * @identify: sas identify info
197 * Populates sas identify info.
199 * Return: 0 for success, non-zero for failure.
201 static int
202 _transport_set_identify(struct MPT3SAS_ADAPTER *ioc, u16 handle,
203 struct sas_identify *identify)
205 Mpi2SasDevicePage0_t sas_device_pg0;
206 Mpi2ConfigReply_t mpi_reply;
207 u32 device_info;
208 u32 ioc_status;
210 if (ioc->shost_recovery || ioc->pci_error_recovery) {
211 ioc_info(ioc, "%s: host reset in progress!\n", __func__);
212 return -EFAULT;
215 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
216 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
217 ioc_err(ioc, "failure at %s:%d/%s()!\n",
218 __FILE__, __LINE__, __func__);
219 return -ENXIO;
222 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
223 MPI2_IOCSTATUS_MASK;
224 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
225 ioc_err(ioc, "handle(0x%04x), ioc_status(0x%04x) failure at %s:%d/%s()!\n",
226 handle, ioc_status, __FILE__, __LINE__, __func__);
227 return -EIO;
230 memset(identify, 0, sizeof(struct sas_identify));
231 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
233 /* sas_address */
234 identify->sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
236 /* phy number of the parent device this device is linked to */
237 identify->phy_identifier = sas_device_pg0.PhyNum;
239 /* device_type */
240 switch (device_info & MPI2_SAS_DEVICE_INFO_MASK_DEVICE_TYPE) {
241 case MPI2_SAS_DEVICE_INFO_NO_DEVICE:
242 identify->device_type = SAS_PHY_UNUSED;
243 break;
244 case MPI2_SAS_DEVICE_INFO_END_DEVICE:
245 identify->device_type = SAS_END_DEVICE;
246 break;
247 case MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER:
248 identify->device_type = SAS_EDGE_EXPANDER_DEVICE;
249 break;
250 case MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER:
251 identify->device_type = SAS_FANOUT_EXPANDER_DEVICE;
252 break;
255 /* initiator_port_protocols */
256 if (device_info & MPI2_SAS_DEVICE_INFO_SSP_INITIATOR)
257 identify->initiator_port_protocols |= SAS_PROTOCOL_SSP;
258 if (device_info & MPI2_SAS_DEVICE_INFO_STP_INITIATOR)
259 identify->initiator_port_protocols |= SAS_PROTOCOL_STP;
260 if (device_info & MPI2_SAS_DEVICE_INFO_SMP_INITIATOR)
261 identify->initiator_port_protocols |= SAS_PROTOCOL_SMP;
262 if (device_info & MPI2_SAS_DEVICE_INFO_SATA_HOST)
263 identify->initiator_port_protocols |= SAS_PROTOCOL_SATA;
265 /* target_port_protocols */
266 if (device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET)
267 identify->target_port_protocols |= SAS_PROTOCOL_SSP;
268 if (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET)
269 identify->target_port_protocols |= SAS_PROTOCOL_STP;
270 if (device_info & MPI2_SAS_DEVICE_INFO_SMP_TARGET)
271 identify->target_port_protocols |= SAS_PROTOCOL_SMP;
272 if (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
273 identify->target_port_protocols |= SAS_PROTOCOL_SATA;
275 return 0;
279 * mpt3sas_transport_done - internal transport layer callback handler.
280 * @ioc: per adapter object
281 * @smid: system request message index
282 * @msix_index: MSIX table index supplied by the OS
283 * @reply: reply message frame(lower 32bit addr)
285 * Callback handler when sending internal generated transport cmds.
286 * The callback index passed is `ioc->transport_cb_idx`
288 * Return: 1 meaning mf should be freed from _base_interrupt
289 * 0 means the mf is freed from this function.
292 mpt3sas_transport_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
293 u32 reply)
295 MPI2DefaultReply_t *mpi_reply;
297 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
298 if (ioc->transport_cmds.status == MPT3_CMD_NOT_USED)
299 return 1;
300 if (ioc->transport_cmds.smid != smid)
301 return 1;
302 ioc->transport_cmds.status |= MPT3_CMD_COMPLETE;
303 if (mpi_reply) {
304 memcpy(ioc->transport_cmds.reply, mpi_reply,
305 mpi_reply->MsgLength*4);
306 ioc->transport_cmds.status |= MPT3_CMD_REPLY_VALID;
308 ioc->transport_cmds.status &= ~MPT3_CMD_PENDING;
309 complete(&ioc->transport_cmds.done);
310 return 1;
313 /* report manufacture request structure */
314 struct rep_manu_request {
315 u8 smp_frame_type;
316 u8 function;
317 u8 reserved;
318 u8 request_length;
321 /* report manufacture reply structure */
322 struct rep_manu_reply {
323 u8 smp_frame_type; /* 0x41 */
324 u8 function; /* 0x01 */
325 u8 function_result;
326 u8 response_length;
327 u16 expander_change_count;
328 u8 reserved0[2];
329 u8 sas_format;
330 u8 reserved2[3];
331 u8 vendor_id[SAS_EXPANDER_VENDOR_ID_LEN];
332 u8 product_id[SAS_EXPANDER_PRODUCT_ID_LEN];
333 u8 product_rev[SAS_EXPANDER_PRODUCT_REV_LEN];
334 u8 component_vendor_id[SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN];
335 u16 component_id;
336 u8 component_revision_id;
337 u8 reserved3;
338 u8 vendor_specific[8];
342 * transport_expander_report_manufacture - obtain SMP report_manufacture
343 * @ioc: per adapter object
344 * @sas_address: expander sas address
345 * @edev: the sas_expander_device object
347 * Fills in the sas_expander_device object when SMP port is created.
349 * Return: 0 for success, non-zero for failure.
351 static int
352 _transport_expander_report_manufacture(struct MPT3SAS_ADAPTER *ioc,
353 u64 sas_address, struct sas_expander_device *edev, u8 port_id)
355 Mpi2SmpPassthroughRequest_t *mpi_request;
356 Mpi2SmpPassthroughReply_t *mpi_reply;
357 struct rep_manu_reply *manufacture_reply;
358 struct rep_manu_request *manufacture_request;
359 int rc;
360 u16 smid;
361 void *psge;
362 u8 issue_reset = 0;
363 void *data_out = NULL;
364 dma_addr_t data_out_dma;
365 dma_addr_t data_in_dma;
366 size_t data_in_sz;
367 size_t data_out_sz;
369 if (ioc->shost_recovery || ioc->pci_error_recovery) {
370 ioc_info(ioc, "%s: host reset in progress!\n", __func__);
371 return -EFAULT;
374 mutex_lock(&ioc->transport_cmds.mutex);
376 if (ioc->transport_cmds.status != MPT3_CMD_NOT_USED) {
377 ioc_err(ioc, "%s: transport_cmds in use\n", __func__);
378 rc = -EAGAIN;
379 goto out;
381 ioc->transport_cmds.status = MPT3_CMD_PENDING;
383 rc = mpt3sas_wait_for_ioc(ioc, IOC_OPERATIONAL_WAIT_COUNT);
384 if (rc)
385 goto out;
387 smid = mpt3sas_base_get_smid(ioc, ioc->transport_cb_idx);
388 if (!smid) {
389 ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
390 rc = -EAGAIN;
391 goto out;
394 rc = 0;
395 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
396 ioc->transport_cmds.smid = smid;
398 data_out_sz = sizeof(struct rep_manu_request);
399 data_in_sz = sizeof(struct rep_manu_reply);
400 data_out = dma_alloc_coherent(&ioc->pdev->dev, data_out_sz + data_in_sz,
401 &data_out_dma, GFP_KERNEL);
402 if (!data_out) {
403 pr_err("failure at %s:%d/%s()!\n", __FILE__,
404 __LINE__, __func__);
405 rc = -ENOMEM;
406 mpt3sas_base_free_smid(ioc, smid);
407 goto out;
410 data_in_dma = data_out_dma + sizeof(struct rep_manu_request);
412 manufacture_request = data_out;
413 manufacture_request->smp_frame_type = 0x40;
414 manufacture_request->function = 1;
415 manufacture_request->reserved = 0;
416 manufacture_request->request_length = 0;
418 memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
419 mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
420 mpi_request->PhysicalPort = port_id;
421 mpi_request->SASAddress = cpu_to_le64(sas_address);
422 mpi_request->RequestDataLength = cpu_to_le16(data_out_sz);
423 psge = &mpi_request->SGL;
425 ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
426 data_in_sz);
428 dtransportprintk(ioc,
429 ioc_info(ioc, "report_manufacture - send to sas_addr(0x%016llx)\n",
430 (u64)sas_address));
431 init_completion(&ioc->transport_cmds.done);
432 ioc->put_smid_default(ioc, smid);
433 wait_for_completion_timeout(&ioc->transport_cmds.done, 10*HZ);
435 if (!(ioc->transport_cmds.status & MPT3_CMD_COMPLETE)) {
436 ioc_err(ioc, "%s: timeout\n", __func__);
437 _debug_dump_mf(mpi_request,
438 sizeof(Mpi2SmpPassthroughRequest_t)/4);
439 if (!(ioc->transport_cmds.status & MPT3_CMD_RESET))
440 issue_reset = 1;
441 goto issue_host_reset;
444 dtransportprintk(ioc, ioc_info(ioc, "report_manufacture - complete\n"));
446 if (ioc->transport_cmds.status & MPT3_CMD_REPLY_VALID) {
447 u8 *tmp;
449 mpi_reply = ioc->transport_cmds.reply;
451 dtransportprintk(ioc,
452 ioc_info(ioc, "report_manufacture - reply data transfer size(%d)\n",
453 le16_to_cpu(mpi_reply->ResponseDataLength)));
455 if (le16_to_cpu(mpi_reply->ResponseDataLength) !=
456 sizeof(struct rep_manu_reply))
457 goto out;
459 manufacture_reply = data_out + sizeof(struct rep_manu_request);
460 strncpy(edev->vendor_id, manufacture_reply->vendor_id,
461 SAS_EXPANDER_VENDOR_ID_LEN);
462 strncpy(edev->product_id, manufacture_reply->product_id,
463 SAS_EXPANDER_PRODUCT_ID_LEN);
464 strncpy(edev->product_rev, manufacture_reply->product_rev,
465 SAS_EXPANDER_PRODUCT_REV_LEN);
466 edev->level = manufacture_reply->sas_format & 1;
467 if (edev->level) {
468 strncpy(edev->component_vendor_id,
469 manufacture_reply->component_vendor_id,
470 SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN);
471 tmp = (u8 *)&manufacture_reply->component_id;
472 edev->component_id = tmp[0] << 8 | tmp[1];
473 edev->component_revision_id =
474 manufacture_reply->component_revision_id;
476 } else
477 dtransportprintk(ioc,
478 ioc_info(ioc, "report_manufacture - no reply\n"));
480 issue_host_reset:
481 if (issue_reset)
482 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
483 out:
484 ioc->transport_cmds.status = MPT3_CMD_NOT_USED;
485 if (data_out)
486 dma_free_coherent(&ioc->pdev->dev, data_out_sz + data_in_sz,
487 data_out, data_out_dma);
489 mutex_unlock(&ioc->transport_cmds.mutex);
490 return rc;
495 * _transport_delete_port - helper function to removing a port
496 * @ioc: per adapter object
497 * @mpt3sas_port: mpt3sas per port object
499 static void
500 _transport_delete_port(struct MPT3SAS_ADAPTER *ioc,
501 struct _sas_port *mpt3sas_port)
503 u64 sas_address = mpt3sas_port->remote_identify.sas_address;
504 struct hba_port *port = mpt3sas_port->hba_port;
505 enum sas_device_type device_type =
506 mpt3sas_port->remote_identify.device_type;
508 dev_printk(KERN_INFO, &mpt3sas_port->port->dev,
509 "remove: sas_addr(0x%016llx)\n",
510 (unsigned long long) sas_address);
512 ioc->logging_level |= MPT_DEBUG_TRANSPORT;
513 if (device_type == SAS_END_DEVICE)
514 mpt3sas_device_remove_by_sas_address(ioc,
515 sas_address, port);
516 else if (device_type == SAS_EDGE_EXPANDER_DEVICE ||
517 device_type == SAS_FANOUT_EXPANDER_DEVICE)
518 mpt3sas_expander_remove(ioc, sas_address, port);
519 ioc->logging_level &= ~MPT_DEBUG_TRANSPORT;
523 * _transport_delete_phy - helper function to removing single phy from port
524 * @ioc: per adapter object
525 * @mpt3sas_port: mpt3sas per port object
526 * @mpt3sas_phy: mpt3sas per phy object
528 static void
529 _transport_delete_phy(struct MPT3SAS_ADAPTER *ioc,
530 struct _sas_port *mpt3sas_port, struct _sas_phy *mpt3sas_phy)
532 u64 sas_address = mpt3sas_port->remote_identify.sas_address;
534 dev_printk(KERN_INFO, &mpt3sas_phy->phy->dev,
535 "remove: sas_addr(0x%016llx), phy(%d)\n",
536 (unsigned long long) sas_address, mpt3sas_phy->phy_id);
538 list_del(&mpt3sas_phy->port_siblings);
539 mpt3sas_port->num_phys--;
540 sas_port_delete_phy(mpt3sas_port->port, mpt3sas_phy->phy);
541 mpt3sas_phy->phy_belongs_to_port = 0;
545 * _transport_add_phy - helper function to adding single phy to port
546 * @ioc: per adapter object
547 * @mpt3sas_port: mpt3sas per port object
548 * @mpt3sas_phy: mpt3sas per phy object
550 static void
551 _transport_add_phy(struct MPT3SAS_ADAPTER *ioc, struct _sas_port *mpt3sas_port,
552 struct _sas_phy *mpt3sas_phy)
554 u64 sas_address = mpt3sas_port->remote_identify.sas_address;
556 dev_printk(KERN_INFO, &mpt3sas_phy->phy->dev,
557 "add: sas_addr(0x%016llx), phy(%d)\n", (unsigned long long)
558 sas_address, mpt3sas_phy->phy_id);
560 list_add_tail(&mpt3sas_phy->port_siblings, &mpt3sas_port->phy_list);
561 mpt3sas_port->num_phys++;
562 sas_port_add_phy(mpt3sas_port->port, mpt3sas_phy->phy);
563 mpt3sas_phy->phy_belongs_to_port = 1;
567 * mpt3sas_transport_add_phy_to_an_existing_port - adding new phy to existing port
568 * @ioc: per adapter object
569 * @sas_node: sas node object (either expander or sas host)
570 * @mpt3sas_phy: mpt3sas per phy object
571 * @sas_address: sas address of device/expander were phy needs to be added to
572 * @port: hba port entry
574 void
575 mpt3sas_transport_add_phy_to_an_existing_port(struct MPT3SAS_ADAPTER *ioc,
576 struct _sas_node *sas_node, struct _sas_phy *mpt3sas_phy,
577 u64 sas_address, struct hba_port *port)
579 struct _sas_port *mpt3sas_port;
580 struct _sas_phy *phy_srch;
582 if (mpt3sas_phy->phy_belongs_to_port == 1)
583 return;
585 if (!port)
586 return;
588 list_for_each_entry(mpt3sas_port, &sas_node->sas_port_list,
589 port_list) {
590 if (mpt3sas_port->remote_identify.sas_address !=
591 sas_address)
592 continue;
593 if (mpt3sas_port->hba_port != port)
594 continue;
595 list_for_each_entry(phy_srch, &mpt3sas_port->phy_list,
596 port_siblings) {
597 if (phy_srch == mpt3sas_phy)
598 return;
600 _transport_add_phy(ioc, mpt3sas_port, mpt3sas_phy);
601 return;
607 * mpt3sas_transport_del_phy_from_an_existing_port - delete phy from existing port
608 * @ioc: per adapter object
609 * @sas_node: sas node object (either expander or sas host)
610 * @mpt3sas_phy: mpt3sas per phy object
612 void
613 mpt3sas_transport_del_phy_from_an_existing_port(struct MPT3SAS_ADAPTER *ioc,
614 struct _sas_node *sas_node, struct _sas_phy *mpt3sas_phy)
616 struct _sas_port *mpt3sas_port, *next;
617 struct _sas_phy *phy_srch;
619 if (mpt3sas_phy->phy_belongs_to_port == 0)
620 return;
622 list_for_each_entry_safe(mpt3sas_port, next, &sas_node->sas_port_list,
623 port_list) {
624 list_for_each_entry(phy_srch, &mpt3sas_port->phy_list,
625 port_siblings) {
626 if (phy_srch != mpt3sas_phy)
627 continue;
630 * Don't delete port during host reset,
631 * just delete phy.
633 if (mpt3sas_port->num_phys == 1 && !ioc->shost_recovery)
634 _transport_delete_port(ioc, mpt3sas_port);
635 else
636 _transport_delete_phy(ioc, mpt3sas_port,
637 mpt3sas_phy);
638 return;
644 * _transport_sanity_check - sanity check when adding a new port
645 * @ioc: per adapter object
646 * @sas_node: sas node object (either expander or sas host)
647 * @sas_address: sas address of device being added
648 * @port: hba port entry
650 * See the explanation above from _transport_delete_duplicate_port
652 static void
653 _transport_sanity_check(struct MPT3SAS_ADAPTER *ioc, struct _sas_node *sas_node,
654 u64 sas_address, struct hba_port *port)
656 int i;
658 for (i = 0; i < sas_node->num_phys; i++) {
659 if (sas_node->phy[i].remote_identify.sas_address != sas_address)
660 continue;
661 if (sas_node->phy[i].port != port)
662 continue;
663 if (sas_node->phy[i].phy_belongs_to_port == 1)
664 mpt3sas_transport_del_phy_from_an_existing_port(ioc,
665 sas_node, &sas_node->phy[i]);
670 * mpt3sas_transport_port_add - insert port to the list
671 * @ioc: per adapter object
672 * @handle: handle of attached device
673 * @sas_address: sas address of parent expander or sas host
674 * @port: hba port entry
675 * Context: This function will acquire ioc->sas_node_lock.
677 * Adding new port object to the sas_node->sas_port_list.
679 * Return: mpt3sas_port.
681 struct _sas_port *
682 mpt3sas_transport_port_add(struct MPT3SAS_ADAPTER *ioc, u16 handle,
683 u64 sas_address, struct hba_port *hba_port)
685 struct _sas_phy *mpt3sas_phy, *next;
686 struct _sas_port *mpt3sas_port;
687 unsigned long flags;
688 struct _sas_node *sas_node;
689 struct sas_rphy *rphy;
690 struct _sas_device *sas_device = NULL;
691 int i;
692 struct sas_port *port;
693 struct virtual_phy *vphy = NULL;
695 if (!hba_port) {
696 ioc_err(ioc, "failure at %s:%d/%s()!\n",
697 __FILE__, __LINE__, __func__);
698 return NULL;
701 mpt3sas_port = kzalloc(sizeof(struct _sas_port),
702 GFP_KERNEL);
703 if (!mpt3sas_port) {
704 ioc_err(ioc, "failure at %s:%d/%s()!\n",
705 __FILE__, __LINE__, __func__);
706 return NULL;
709 INIT_LIST_HEAD(&mpt3sas_port->port_list);
710 INIT_LIST_HEAD(&mpt3sas_port->phy_list);
711 spin_lock_irqsave(&ioc->sas_node_lock, flags);
712 sas_node = _transport_sas_node_find_by_sas_address(ioc,
713 sas_address, hba_port);
714 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
716 if (!sas_node) {
717 ioc_err(ioc, "%s: Could not find parent sas_address(0x%016llx)!\n",
718 __func__, (u64)sas_address);
719 goto out_fail;
722 if ((_transport_set_identify(ioc, handle,
723 &mpt3sas_port->remote_identify))) {
724 ioc_err(ioc, "failure at %s:%d/%s()!\n",
725 __FILE__, __LINE__, __func__);
726 goto out_fail;
729 if (mpt3sas_port->remote_identify.device_type == SAS_PHY_UNUSED) {
730 ioc_err(ioc, "failure at %s:%d/%s()!\n",
731 __FILE__, __LINE__, __func__);
732 goto out_fail;
735 mpt3sas_port->hba_port = hba_port;
736 _transport_sanity_check(ioc, sas_node,
737 mpt3sas_port->remote_identify.sas_address, hba_port);
739 for (i = 0; i < sas_node->num_phys; i++) {
740 if (sas_node->phy[i].remote_identify.sas_address !=
741 mpt3sas_port->remote_identify.sas_address)
742 continue;
743 if (sas_node->phy[i].port != hba_port)
744 continue;
745 list_add_tail(&sas_node->phy[i].port_siblings,
746 &mpt3sas_port->phy_list);
747 mpt3sas_port->num_phys++;
748 if (sas_node->handle <= ioc->sas_hba.num_phys) {
749 if (!sas_node->phy[i].hba_vphy) {
750 hba_port->phy_mask |= (1 << i);
751 continue;
754 vphy = mpt3sas_get_vphy_by_phy(ioc, hba_port, i);
755 if (!vphy) {
756 ioc_err(ioc, "failure at %s:%d/%s()!\n",
757 __FILE__, __LINE__, __func__);
758 goto out_fail;
763 if (!mpt3sas_port->num_phys) {
764 ioc_err(ioc, "failure at %s:%d/%s()!\n",
765 __FILE__, __LINE__, __func__);
766 goto out_fail;
769 if (mpt3sas_port->remote_identify.device_type == SAS_END_DEVICE) {
770 sas_device = mpt3sas_get_sdev_by_addr(ioc,
771 mpt3sas_port->remote_identify.sas_address,
772 mpt3sas_port->hba_port);
773 if (!sas_device) {
774 ioc_err(ioc, "failure at %s:%d/%s()!\n",
775 __FILE__, __LINE__, __func__);
776 goto out_fail;
778 sas_device->pend_sas_rphy_add = 1;
781 if (!sas_node->parent_dev) {
782 ioc_err(ioc, "failure at %s:%d/%s()!\n",
783 __FILE__, __LINE__, __func__);
784 goto out_fail;
786 port = sas_port_alloc_num(sas_node->parent_dev);
787 if ((sas_port_add(port))) {
788 ioc_err(ioc, "failure at %s:%d/%s()!\n",
789 __FILE__, __LINE__, __func__);
790 goto out_fail;
793 list_for_each_entry(mpt3sas_phy, &mpt3sas_port->phy_list,
794 port_siblings) {
795 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
796 dev_printk(KERN_INFO, &port->dev,
797 "add: handle(0x%04x), sas_addr(0x%016llx), phy(%d)\n",
798 handle, (unsigned long long)
799 mpt3sas_port->remote_identify.sas_address,
800 mpt3sas_phy->phy_id);
801 sas_port_add_phy(port, mpt3sas_phy->phy);
802 mpt3sas_phy->phy_belongs_to_port = 1;
803 mpt3sas_phy->port = hba_port;
806 mpt3sas_port->port = port;
807 if (mpt3sas_port->remote_identify.device_type == SAS_END_DEVICE) {
808 rphy = sas_end_device_alloc(port);
809 sas_device->rphy = rphy;
810 if (sas_node->handle <= ioc->sas_hba.num_phys) {
811 if (!vphy)
812 hba_port->sas_address =
813 sas_device->sas_address;
814 else
815 vphy->sas_address =
816 sas_device->sas_address;
818 } else {
819 rphy = sas_expander_alloc(port,
820 mpt3sas_port->remote_identify.device_type);
821 if (sas_node->handle <= ioc->sas_hba.num_phys)
822 hba_port->sas_address =
823 mpt3sas_port->remote_identify.sas_address;
826 rphy->identify = mpt3sas_port->remote_identify;
828 if ((sas_rphy_add(rphy))) {
829 ioc_err(ioc, "failure at %s:%d/%s()!\n",
830 __FILE__, __LINE__, __func__);
833 if (mpt3sas_port->remote_identify.device_type == SAS_END_DEVICE) {
834 sas_device->pend_sas_rphy_add = 0;
835 sas_device_put(sas_device);
838 dev_info(&rphy->dev,
839 "add: handle(0x%04x), sas_addr(0x%016llx)\n", handle,
840 (unsigned long long)mpt3sas_port->remote_identify.sas_address);
842 mpt3sas_port->rphy = rphy;
843 spin_lock_irqsave(&ioc->sas_node_lock, flags);
844 list_add_tail(&mpt3sas_port->port_list, &sas_node->sas_port_list);
845 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
847 /* fill in report manufacture */
848 if (mpt3sas_port->remote_identify.device_type ==
849 MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
850 mpt3sas_port->remote_identify.device_type ==
851 MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER)
852 _transport_expander_report_manufacture(ioc,
853 mpt3sas_port->remote_identify.sas_address,
854 rphy_to_expander_device(rphy), hba_port->port_id);
855 return mpt3sas_port;
857 out_fail:
858 list_for_each_entry_safe(mpt3sas_phy, next, &mpt3sas_port->phy_list,
859 port_siblings)
860 list_del(&mpt3sas_phy->port_siblings);
861 kfree(mpt3sas_port);
862 return NULL;
866 * mpt3sas_transport_port_remove - remove port from the list
867 * @ioc: per adapter object
868 * @sas_address: sas address of attached device
869 * @sas_address_parent: sas address of parent expander or sas host
870 * @port: hba port entry
871 * Context: This function will acquire ioc->sas_node_lock.
873 * Removing object and freeing associated memory from the
874 * ioc->sas_port_list.
876 void
877 mpt3sas_transport_port_remove(struct MPT3SAS_ADAPTER *ioc, u64 sas_address,
878 u64 sas_address_parent, struct hba_port *port)
880 int i;
881 unsigned long flags;
882 struct _sas_port *mpt3sas_port, *next;
883 struct _sas_node *sas_node;
884 u8 found = 0;
885 struct _sas_phy *mpt3sas_phy, *next_phy;
886 struct hba_port *hba_port_next, *hba_port = NULL;
887 struct virtual_phy *vphy, *vphy_next = NULL;
889 if (!port)
890 return;
892 spin_lock_irqsave(&ioc->sas_node_lock, flags);
893 sas_node = _transport_sas_node_find_by_sas_address(ioc,
894 sas_address_parent, port);
895 if (!sas_node) {
896 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
897 return;
899 list_for_each_entry_safe(mpt3sas_port, next, &sas_node->sas_port_list,
900 port_list) {
901 if (mpt3sas_port->remote_identify.sas_address != sas_address)
902 continue;
903 if (mpt3sas_port->hba_port != port)
904 continue;
905 found = 1;
906 list_del(&mpt3sas_port->port_list);
907 goto out;
909 out:
910 if (!found) {
911 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
912 return;
915 if (sas_node->handle <= ioc->sas_hba.num_phys &&
916 (ioc->multipath_on_hba)) {
917 if (port->vphys_mask) {
918 list_for_each_entry_safe(vphy, vphy_next,
919 &port->vphys_list, list) {
920 if (vphy->sas_address != sas_address)
921 continue;
922 ioc_info(ioc,
923 "remove vphy entry: %p of port:%p,from %d port's vphys list\n",
924 vphy, port, port->port_id);
925 port->vphys_mask &= ~vphy->phy_mask;
926 list_del(&vphy->list);
927 kfree(vphy);
931 list_for_each_entry_safe(hba_port, hba_port_next,
932 &ioc->port_table_list, list) {
933 if (hba_port != port)
934 continue;
936 * Delete hba_port object if
937 * - hba_port object's sas address matches with current
938 * removed device's sas address and no vphy's
939 * associated with it.
940 * - Current removed device is a vSES device and
941 * none of the other direct attached device have
942 * this vSES device's port number (hence hba_port
943 * object sas_address field will be zero).
945 if ((hba_port->sas_address == sas_address ||
946 !hba_port->sas_address) && !hba_port->vphys_mask) {
947 ioc_info(ioc,
948 "remove hba_port entry: %p port: %d from hba_port list\n",
949 hba_port, hba_port->port_id);
950 list_del(&hba_port->list);
951 kfree(hba_port);
952 } else if (hba_port->sas_address == sas_address &&
953 hba_port->vphys_mask) {
955 * Current removed device is a non vSES device
956 * and a vSES device has the same port number
957 * as of current device's port number. Hence
958 * only clear the sas_address filed, don't
959 * delete the hba_port object.
961 ioc_info(ioc,
962 "clearing sas_address from hba_port entry: %p port: %d from hba_port list\n",
963 hba_port, hba_port->port_id);
964 port->sas_address = 0;
966 break;
970 for (i = 0; i < sas_node->num_phys; i++) {
971 if (sas_node->phy[i].remote_identify.sas_address == sas_address)
972 memset(&sas_node->phy[i].remote_identify, 0 ,
973 sizeof(struct sas_identify));
976 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
978 list_for_each_entry_safe(mpt3sas_phy, next_phy,
979 &mpt3sas_port->phy_list, port_siblings) {
980 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
981 dev_printk(KERN_INFO, &mpt3sas_port->port->dev,
982 "remove: sas_addr(0x%016llx), phy(%d)\n",
983 (unsigned long long)
984 mpt3sas_port->remote_identify.sas_address,
985 mpt3sas_phy->phy_id);
986 mpt3sas_phy->phy_belongs_to_port = 0;
987 if (!ioc->remove_host)
988 sas_port_delete_phy(mpt3sas_port->port,
989 mpt3sas_phy->phy);
990 list_del(&mpt3sas_phy->port_siblings);
992 if (!ioc->remove_host)
993 sas_port_delete(mpt3sas_port->port);
994 ioc_info(ioc, "%s: removed: sas_addr(0x%016llx)\n",
995 __func__, (unsigned long long)sas_address);
996 kfree(mpt3sas_port);
1000 * mpt3sas_transport_add_host_phy - report sas_host phy to transport
1001 * @ioc: per adapter object
1002 * @mpt3sas_phy: mpt3sas per phy object
1003 * @phy_pg0: sas phy page 0
1004 * @parent_dev: parent device class object
1006 * Return: 0 for success, non-zero for failure.
1009 mpt3sas_transport_add_host_phy(struct MPT3SAS_ADAPTER *ioc, struct _sas_phy
1010 *mpt3sas_phy, Mpi2SasPhyPage0_t phy_pg0, struct device *parent_dev)
1012 struct sas_phy *phy;
1013 int phy_index = mpt3sas_phy->phy_id;
1016 INIT_LIST_HEAD(&mpt3sas_phy->port_siblings);
1017 phy = sas_phy_alloc(parent_dev, phy_index);
1018 if (!phy) {
1019 ioc_err(ioc, "failure at %s:%d/%s()!\n",
1020 __FILE__, __LINE__, __func__);
1021 return -1;
1023 if ((_transport_set_identify(ioc, mpt3sas_phy->handle,
1024 &mpt3sas_phy->identify))) {
1025 ioc_err(ioc, "failure at %s:%d/%s()!\n",
1026 __FILE__, __LINE__, __func__);
1027 sas_phy_free(phy);
1028 return -1;
1030 phy->identify = mpt3sas_phy->identify;
1031 mpt3sas_phy->attached_handle = le16_to_cpu(phy_pg0.AttachedDevHandle);
1032 if (mpt3sas_phy->attached_handle)
1033 _transport_set_identify(ioc, mpt3sas_phy->attached_handle,
1034 &mpt3sas_phy->remote_identify);
1035 phy->identify.phy_identifier = mpt3sas_phy->phy_id;
1036 phy->negotiated_linkrate = _transport_convert_phy_link_rate(
1037 phy_pg0.NegotiatedLinkRate & MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
1038 phy->minimum_linkrate_hw = _transport_convert_phy_link_rate(
1039 phy_pg0.HwLinkRate & MPI2_SAS_HWRATE_MIN_RATE_MASK);
1040 phy->maximum_linkrate_hw = _transport_convert_phy_link_rate(
1041 phy_pg0.HwLinkRate >> 4);
1042 phy->minimum_linkrate = _transport_convert_phy_link_rate(
1043 phy_pg0.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
1044 phy->maximum_linkrate = _transport_convert_phy_link_rate(
1045 phy_pg0.ProgrammedLinkRate >> 4);
1046 phy->hostdata = mpt3sas_phy->port;
1048 if ((sas_phy_add(phy))) {
1049 ioc_err(ioc, "failure at %s:%d/%s()!\n",
1050 __FILE__, __LINE__, __func__);
1051 sas_phy_free(phy);
1052 return -1;
1054 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
1055 dev_printk(KERN_INFO, &phy->dev,
1056 "add: handle(0x%04x), sas_addr(0x%016llx)\n"
1057 "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
1058 mpt3sas_phy->handle, (unsigned long long)
1059 mpt3sas_phy->identify.sas_address,
1060 mpt3sas_phy->attached_handle,
1061 (unsigned long long)
1062 mpt3sas_phy->remote_identify.sas_address);
1063 mpt3sas_phy->phy = phy;
1064 return 0;
1069 * mpt3sas_transport_add_expander_phy - report expander phy to transport
1070 * @ioc: per adapter object
1071 * @mpt3sas_phy: mpt3sas per phy object
1072 * @expander_pg1: expander page 1
1073 * @parent_dev: parent device class object
1075 * Return: 0 for success, non-zero for failure.
1078 mpt3sas_transport_add_expander_phy(struct MPT3SAS_ADAPTER *ioc, struct _sas_phy
1079 *mpt3sas_phy, Mpi2ExpanderPage1_t expander_pg1,
1080 struct device *parent_dev)
1082 struct sas_phy *phy;
1083 int phy_index = mpt3sas_phy->phy_id;
1085 INIT_LIST_HEAD(&mpt3sas_phy->port_siblings);
1086 phy = sas_phy_alloc(parent_dev, phy_index);
1087 if (!phy) {
1088 ioc_err(ioc, "failure at %s:%d/%s()!\n",
1089 __FILE__, __LINE__, __func__);
1090 return -1;
1092 if ((_transport_set_identify(ioc, mpt3sas_phy->handle,
1093 &mpt3sas_phy->identify))) {
1094 ioc_err(ioc, "failure at %s:%d/%s()!\n",
1095 __FILE__, __LINE__, __func__);
1096 sas_phy_free(phy);
1097 return -1;
1099 phy->identify = mpt3sas_phy->identify;
1100 mpt3sas_phy->attached_handle =
1101 le16_to_cpu(expander_pg1.AttachedDevHandle);
1102 if (mpt3sas_phy->attached_handle)
1103 _transport_set_identify(ioc, mpt3sas_phy->attached_handle,
1104 &mpt3sas_phy->remote_identify);
1105 phy->identify.phy_identifier = mpt3sas_phy->phy_id;
1106 phy->negotiated_linkrate = _transport_convert_phy_link_rate(
1107 expander_pg1.NegotiatedLinkRate &
1108 MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
1109 phy->minimum_linkrate_hw = _transport_convert_phy_link_rate(
1110 expander_pg1.HwLinkRate & MPI2_SAS_HWRATE_MIN_RATE_MASK);
1111 phy->maximum_linkrate_hw = _transport_convert_phy_link_rate(
1112 expander_pg1.HwLinkRate >> 4);
1113 phy->minimum_linkrate = _transport_convert_phy_link_rate(
1114 expander_pg1.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
1115 phy->maximum_linkrate = _transport_convert_phy_link_rate(
1116 expander_pg1.ProgrammedLinkRate >> 4);
1117 phy->hostdata = mpt3sas_phy->port;
1119 if ((sas_phy_add(phy))) {
1120 ioc_err(ioc, "failure at %s:%d/%s()!\n",
1121 __FILE__, __LINE__, __func__);
1122 sas_phy_free(phy);
1123 return -1;
1125 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
1126 dev_printk(KERN_INFO, &phy->dev,
1127 "add: handle(0x%04x), sas_addr(0x%016llx)\n"
1128 "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
1129 mpt3sas_phy->handle, (unsigned long long)
1130 mpt3sas_phy->identify.sas_address,
1131 mpt3sas_phy->attached_handle,
1132 (unsigned long long)
1133 mpt3sas_phy->remote_identify.sas_address);
1134 mpt3sas_phy->phy = phy;
1135 return 0;
1139 * mpt3sas_transport_update_links - refreshing phy link changes
1140 * @ioc: per adapter object
1141 * @sas_address: sas address of parent expander or sas host
1142 * @handle: attached device handle
1143 * @phy_number: phy number
1144 * @link_rate: new link rate
1145 * @port: hba port entry
1147 * Return nothing.
1149 void
1150 mpt3sas_transport_update_links(struct MPT3SAS_ADAPTER *ioc,
1151 u64 sas_address, u16 handle, u8 phy_number, u8 link_rate,
1152 struct hba_port *port)
1154 unsigned long flags;
1155 struct _sas_node *sas_node;
1156 struct _sas_phy *mpt3sas_phy;
1157 struct hba_port *hba_port = NULL;
1159 if (ioc->shost_recovery || ioc->pci_error_recovery)
1160 return;
1162 spin_lock_irqsave(&ioc->sas_node_lock, flags);
1163 sas_node = _transport_sas_node_find_by_sas_address(ioc,
1164 sas_address, port);
1165 if (!sas_node) {
1166 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1167 return;
1170 mpt3sas_phy = &sas_node->phy[phy_number];
1171 mpt3sas_phy->attached_handle = handle;
1172 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1173 if (handle && (link_rate >= MPI2_SAS_NEG_LINK_RATE_1_5)) {
1174 _transport_set_identify(ioc, handle,
1175 &mpt3sas_phy->remote_identify);
1176 if ((sas_node->handle <= ioc->sas_hba.num_phys) &&
1177 (ioc->multipath_on_hba)) {
1178 list_for_each_entry(hba_port,
1179 &ioc->port_table_list, list) {
1180 if (hba_port->sas_address == sas_address &&
1181 hba_port == port)
1182 hba_port->phy_mask |=
1183 (1 << mpt3sas_phy->phy_id);
1186 mpt3sas_transport_add_phy_to_an_existing_port(ioc, sas_node,
1187 mpt3sas_phy, mpt3sas_phy->remote_identify.sas_address,
1188 port);
1189 } else
1190 memset(&mpt3sas_phy->remote_identify, 0 , sizeof(struct
1191 sas_identify));
1193 if (mpt3sas_phy->phy)
1194 mpt3sas_phy->phy->negotiated_linkrate =
1195 _transport_convert_phy_link_rate(link_rate);
1197 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
1198 dev_printk(KERN_INFO, &mpt3sas_phy->phy->dev,
1199 "refresh: parent sas_addr(0x%016llx),\n"
1200 "\tlink_rate(0x%02x), phy(%d)\n"
1201 "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
1202 (unsigned long long)sas_address,
1203 link_rate, phy_number, handle, (unsigned long long)
1204 mpt3sas_phy->remote_identify.sas_address);
1207 static inline void *
1208 phy_to_ioc(struct sas_phy *phy)
1210 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
1211 return shost_priv(shost);
1214 static inline void *
1215 rphy_to_ioc(struct sas_rphy *rphy)
1217 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
1218 return shost_priv(shost);
1221 /* report phy error log structure */
1222 struct phy_error_log_request {
1223 u8 smp_frame_type; /* 0x40 */
1224 u8 function; /* 0x11 */
1225 u8 allocated_response_length;
1226 u8 request_length; /* 02 */
1227 u8 reserved_1[5];
1228 u8 phy_identifier;
1229 u8 reserved_2[2];
1232 /* report phy error log reply structure */
1233 struct phy_error_log_reply {
1234 u8 smp_frame_type; /* 0x41 */
1235 u8 function; /* 0x11 */
1236 u8 function_result;
1237 u8 response_length;
1238 __be16 expander_change_count;
1239 u8 reserved_1[3];
1240 u8 phy_identifier;
1241 u8 reserved_2[2];
1242 __be32 invalid_dword;
1243 __be32 running_disparity_error;
1244 __be32 loss_of_dword_sync;
1245 __be32 phy_reset_problem;
1249 * _transport_get_expander_phy_error_log - return expander counters
1250 * @ioc: per adapter object
1251 * @phy: The sas phy object
1253 * Return: 0 for success, non-zero for failure.
1256 static int
1257 _transport_get_expander_phy_error_log(struct MPT3SAS_ADAPTER *ioc,
1258 struct sas_phy *phy)
1260 Mpi2SmpPassthroughRequest_t *mpi_request;
1261 Mpi2SmpPassthroughReply_t *mpi_reply;
1262 struct phy_error_log_request *phy_error_log_request;
1263 struct phy_error_log_reply *phy_error_log_reply;
1264 int rc;
1265 u16 smid;
1266 void *psge;
1267 u8 issue_reset = 0;
1268 void *data_out = NULL;
1269 dma_addr_t data_out_dma;
1270 u32 sz;
1272 if (ioc->shost_recovery || ioc->pci_error_recovery) {
1273 ioc_info(ioc, "%s: host reset in progress!\n", __func__);
1274 return -EFAULT;
1277 mutex_lock(&ioc->transport_cmds.mutex);
1279 if (ioc->transport_cmds.status != MPT3_CMD_NOT_USED) {
1280 ioc_err(ioc, "%s: transport_cmds in use\n", __func__);
1281 rc = -EAGAIN;
1282 goto out;
1284 ioc->transport_cmds.status = MPT3_CMD_PENDING;
1286 rc = mpt3sas_wait_for_ioc(ioc, IOC_OPERATIONAL_WAIT_COUNT);
1287 if (rc)
1288 goto out;
1290 smid = mpt3sas_base_get_smid(ioc, ioc->transport_cb_idx);
1291 if (!smid) {
1292 ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
1293 rc = -EAGAIN;
1294 goto out;
1297 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
1298 ioc->transport_cmds.smid = smid;
1300 sz = sizeof(struct phy_error_log_request) +
1301 sizeof(struct phy_error_log_reply);
1302 data_out = dma_alloc_coherent(&ioc->pdev->dev, sz, &data_out_dma,
1303 GFP_KERNEL);
1304 if (!data_out) {
1305 pr_err("failure at %s:%d/%s()!\n", __FILE__,
1306 __LINE__, __func__);
1307 rc = -ENOMEM;
1308 mpt3sas_base_free_smid(ioc, smid);
1309 goto out;
1312 rc = -EINVAL;
1313 memset(data_out, 0, sz);
1314 phy_error_log_request = data_out;
1315 phy_error_log_request->smp_frame_type = 0x40;
1316 phy_error_log_request->function = 0x11;
1317 phy_error_log_request->request_length = 2;
1318 phy_error_log_request->allocated_response_length = 0;
1319 phy_error_log_request->phy_identifier = phy->number;
1321 memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
1322 mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
1323 mpi_request->PhysicalPort = _transport_get_port_id_by_sas_phy(phy);
1324 mpi_request->VF_ID = 0; /* TODO */
1325 mpi_request->VP_ID = 0;
1326 mpi_request->SASAddress = cpu_to_le64(phy->identify.sas_address);
1327 mpi_request->RequestDataLength =
1328 cpu_to_le16(sizeof(struct phy_error_log_request));
1329 psge = &mpi_request->SGL;
1331 ioc->build_sg(ioc, psge, data_out_dma,
1332 sizeof(struct phy_error_log_request),
1333 data_out_dma + sizeof(struct phy_error_log_request),
1334 sizeof(struct phy_error_log_reply));
1336 dtransportprintk(ioc,
1337 ioc_info(ioc, "phy_error_log - send to sas_addr(0x%016llx), phy(%d)\n",
1338 (u64)phy->identify.sas_address,
1339 phy->number));
1340 init_completion(&ioc->transport_cmds.done);
1341 ioc->put_smid_default(ioc, smid);
1342 wait_for_completion_timeout(&ioc->transport_cmds.done, 10*HZ);
1344 if (!(ioc->transport_cmds.status & MPT3_CMD_COMPLETE)) {
1345 ioc_err(ioc, "%s: timeout\n", __func__);
1346 _debug_dump_mf(mpi_request,
1347 sizeof(Mpi2SmpPassthroughRequest_t)/4);
1348 if (!(ioc->transport_cmds.status & MPT3_CMD_RESET))
1349 issue_reset = 1;
1350 goto issue_host_reset;
1353 dtransportprintk(ioc, ioc_info(ioc, "phy_error_log - complete\n"));
1355 if (ioc->transport_cmds.status & MPT3_CMD_REPLY_VALID) {
1357 mpi_reply = ioc->transport_cmds.reply;
1359 dtransportprintk(ioc,
1360 ioc_info(ioc, "phy_error_log - reply data transfer size(%d)\n",
1361 le16_to_cpu(mpi_reply->ResponseDataLength)));
1363 if (le16_to_cpu(mpi_reply->ResponseDataLength) !=
1364 sizeof(struct phy_error_log_reply))
1365 goto out;
1367 phy_error_log_reply = data_out +
1368 sizeof(struct phy_error_log_request);
1370 dtransportprintk(ioc,
1371 ioc_info(ioc, "phy_error_log - function_result(%d)\n",
1372 phy_error_log_reply->function_result));
1374 phy->invalid_dword_count =
1375 be32_to_cpu(phy_error_log_reply->invalid_dword);
1376 phy->running_disparity_error_count =
1377 be32_to_cpu(phy_error_log_reply->running_disparity_error);
1378 phy->loss_of_dword_sync_count =
1379 be32_to_cpu(phy_error_log_reply->loss_of_dword_sync);
1380 phy->phy_reset_problem_count =
1381 be32_to_cpu(phy_error_log_reply->phy_reset_problem);
1382 rc = 0;
1383 } else
1384 dtransportprintk(ioc,
1385 ioc_info(ioc, "phy_error_log - no reply\n"));
1387 issue_host_reset:
1388 if (issue_reset)
1389 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
1390 out:
1391 ioc->transport_cmds.status = MPT3_CMD_NOT_USED;
1392 if (data_out)
1393 dma_free_coherent(&ioc->pdev->dev, sz, data_out, data_out_dma);
1395 mutex_unlock(&ioc->transport_cmds.mutex);
1396 return rc;
1400 * _transport_get_linkerrors - return phy counters for both hba and expanders
1401 * @phy: The sas phy object
1403 * Return: 0 for success, non-zero for failure.
1406 static int
1407 _transport_get_linkerrors(struct sas_phy *phy)
1409 struct MPT3SAS_ADAPTER *ioc = phy_to_ioc(phy);
1410 unsigned long flags;
1411 Mpi2ConfigReply_t mpi_reply;
1412 Mpi2SasPhyPage1_t phy_pg1;
1413 struct hba_port *port = phy->hostdata;
1414 int port_id = port->port_id;
1416 spin_lock_irqsave(&ioc->sas_node_lock, flags);
1417 if (_transport_sas_node_find_by_sas_address(ioc,
1418 phy->identify.sas_address,
1419 mpt3sas_get_port_by_id(ioc, port_id, 0)) == NULL) {
1420 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1421 return -EINVAL;
1423 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1425 if (phy->identify.sas_address != ioc->sas_hba.sas_address)
1426 return _transport_get_expander_phy_error_log(ioc, phy);
1428 /* get hba phy error logs */
1429 if ((mpt3sas_config_get_phy_pg1(ioc, &mpi_reply, &phy_pg1,
1430 phy->number))) {
1431 ioc_err(ioc, "failure at %s:%d/%s()!\n",
1432 __FILE__, __LINE__, __func__);
1433 return -ENXIO;
1436 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo)
1437 ioc_info(ioc, "phy(%d), ioc_status (0x%04x), loginfo(0x%08x)\n",
1438 phy->number,
1439 le16_to_cpu(mpi_reply.IOCStatus),
1440 le32_to_cpu(mpi_reply.IOCLogInfo));
1442 phy->invalid_dword_count = le32_to_cpu(phy_pg1.InvalidDwordCount);
1443 phy->running_disparity_error_count =
1444 le32_to_cpu(phy_pg1.RunningDisparityErrorCount);
1445 phy->loss_of_dword_sync_count =
1446 le32_to_cpu(phy_pg1.LossDwordSynchCount);
1447 phy->phy_reset_problem_count =
1448 le32_to_cpu(phy_pg1.PhyResetProblemCount);
1449 return 0;
1453 * _transport_get_enclosure_identifier -
1454 * @rphy: The sas phy object
1455 * @identifier: ?
1457 * Obtain the enclosure logical id for an expander.
1458 * Return: 0 for success, non-zero for failure.
1460 static int
1461 _transport_get_enclosure_identifier(struct sas_rphy *rphy, u64 *identifier)
1463 struct MPT3SAS_ADAPTER *ioc = rphy_to_ioc(rphy);
1464 struct _sas_device *sas_device;
1465 unsigned long flags;
1466 int rc;
1468 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1469 sas_device = __mpt3sas_get_sdev_by_rphy(ioc, rphy);
1470 if (sas_device) {
1471 *identifier = sas_device->enclosure_logical_id;
1472 rc = 0;
1473 sas_device_put(sas_device);
1474 } else {
1475 *identifier = 0;
1476 rc = -ENXIO;
1479 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1480 return rc;
1484 * _transport_get_bay_identifier -
1485 * @rphy: The sas phy object
1487 * Return: the slot id for a device that resides inside an enclosure.
1489 static int
1490 _transport_get_bay_identifier(struct sas_rphy *rphy)
1492 struct MPT3SAS_ADAPTER *ioc = rphy_to_ioc(rphy);
1493 struct _sas_device *sas_device;
1494 unsigned long flags;
1495 int rc;
1497 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1498 sas_device = __mpt3sas_get_sdev_by_rphy(ioc, rphy);
1499 if (sas_device) {
1500 rc = sas_device->slot;
1501 sas_device_put(sas_device);
1502 } else {
1503 rc = -ENXIO;
1505 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1506 return rc;
1509 /* phy control request structure */
1510 struct phy_control_request {
1511 u8 smp_frame_type; /* 0x40 */
1512 u8 function; /* 0x91 */
1513 u8 allocated_response_length;
1514 u8 request_length; /* 0x09 */
1515 u16 expander_change_count;
1516 u8 reserved_1[3];
1517 u8 phy_identifier;
1518 u8 phy_operation;
1519 u8 reserved_2[13];
1520 u64 attached_device_name;
1521 u8 programmed_min_physical_link_rate;
1522 u8 programmed_max_physical_link_rate;
1523 u8 reserved_3[6];
1526 /* phy control reply structure */
1527 struct phy_control_reply {
1528 u8 smp_frame_type; /* 0x41 */
1529 u8 function; /* 0x11 */
1530 u8 function_result;
1531 u8 response_length;
1534 #define SMP_PHY_CONTROL_LINK_RESET (0x01)
1535 #define SMP_PHY_CONTROL_HARD_RESET (0x02)
1536 #define SMP_PHY_CONTROL_DISABLE (0x03)
1539 * _transport_expander_phy_control - expander phy control
1540 * @ioc: per adapter object
1541 * @phy: The sas phy object
1542 * @phy_operation: ?
1544 * Return: 0 for success, non-zero for failure.
1547 static int
1548 _transport_expander_phy_control(struct MPT3SAS_ADAPTER *ioc,
1549 struct sas_phy *phy, u8 phy_operation)
1551 Mpi2SmpPassthroughRequest_t *mpi_request;
1552 Mpi2SmpPassthroughReply_t *mpi_reply;
1553 struct phy_control_request *phy_control_request;
1554 struct phy_control_reply *phy_control_reply;
1555 int rc;
1556 u16 smid;
1557 void *psge;
1558 u8 issue_reset = 0;
1559 void *data_out = NULL;
1560 dma_addr_t data_out_dma;
1561 u32 sz;
1563 if (ioc->shost_recovery || ioc->pci_error_recovery) {
1564 ioc_info(ioc, "%s: host reset in progress!\n", __func__);
1565 return -EFAULT;
1568 mutex_lock(&ioc->transport_cmds.mutex);
1570 if (ioc->transport_cmds.status != MPT3_CMD_NOT_USED) {
1571 ioc_err(ioc, "%s: transport_cmds in use\n", __func__);
1572 rc = -EAGAIN;
1573 goto out;
1575 ioc->transport_cmds.status = MPT3_CMD_PENDING;
1577 rc = mpt3sas_wait_for_ioc(ioc, IOC_OPERATIONAL_WAIT_COUNT);
1578 if (rc)
1579 goto out;
1581 smid = mpt3sas_base_get_smid(ioc, ioc->transport_cb_idx);
1582 if (!smid) {
1583 ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
1584 rc = -EAGAIN;
1585 goto out;
1588 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
1589 ioc->transport_cmds.smid = smid;
1591 sz = sizeof(struct phy_control_request) +
1592 sizeof(struct phy_control_reply);
1593 data_out = dma_alloc_coherent(&ioc->pdev->dev, sz, &data_out_dma,
1594 GFP_KERNEL);
1595 if (!data_out) {
1596 pr_err("failure at %s:%d/%s()!\n", __FILE__,
1597 __LINE__, __func__);
1598 rc = -ENOMEM;
1599 mpt3sas_base_free_smid(ioc, smid);
1600 goto out;
1603 rc = -EINVAL;
1604 memset(data_out, 0, sz);
1605 phy_control_request = data_out;
1606 phy_control_request->smp_frame_type = 0x40;
1607 phy_control_request->function = 0x91;
1608 phy_control_request->request_length = 9;
1609 phy_control_request->allocated_response_length = 0;
1610 phy_control_request->phy_identifier = phy->number;
1611 phy_control_request->phy_operation = phy_operation;
1612 phy_control_request->programmed_min_physical_link_rate =
1613 phy->minimum_linkrate << 4;
1614 phy_control_request->programmed_max_physical_link_rate =
1615 phy->maximum_linkrate << 4;
1617 memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
1618 mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
1619 mpi_request->PhysicalPort = _transport_get_port_id_by_sas_phy(phy);
1620 mpi_request->VF_ID = 0; /* TODO */
1621 mpi_request->VP_ID = 0;
1622 mpi_request->SASAddress = cpu_to_le64(phy->identify.sas_address);
1623 mpi_request->RequestDataLength =
1624 cpu_to_le16(sizeof(struct phy_error_log_request));
1625 psge = &mpi_request->SGL;
1627 ioc->build_sg(ioc, psge, data_out_dma,
1628 sizeof(struct phy_control_request),
1629 data_out_dma + sizeof(struct phy_control_request),
1630 sizeof(struct phy_control_reply));
1632 dtransportprintk(ioc,
1633 ioc_info(ioc, "phy_control - send to sas_addr(0x%016llx), phy(%d), opcode(%d)\n",
1634 (u64)phy->identify.sas_address,
1635 phy->number, phy_operation));
1636 init_completion(&ioc->transport_cmds.done);
1637 ioc->put_smid_default(ioc, smid);
1638 wait_for_completion_timeout(&ioc->transport_cmds.done, 10*HZ);
1640 if (!(ioc->transport_cmds.status & MPT3_CMD_COMPLETE)) {
1641 ioc_err(ioc, "%s: timeout\n", __func__);
1642 _debug_dump_mf(mpi_request,
1643 sizeof(Mpi2SmpPassthroughRequest_t)/4);
1644 if (!(ioc->transport_cmds.status & MPT3_CMD_RESET))
1645 issue_reset = 1;
1646 goto issue_host_reset;
1649 dtransportprintk(ioc, ioc_info(ioc, "phy_control - complete\n"));
1651 if (ioc->transport_cmds.status & MPT3_CMD_REPLY_VALID) {
1653 mpi_reply = ioc->transport_cmds.reply;
1655 dtransportprintk(ioc,
1656 ioc_info(ioc, "phy_control - reply data transfer size(%d)\n",
1657 le16_to_cpu(mpi_reply->ResponseDataLength)));
1659 if (le16_to_cpu(mpi_reply->ResponseDataLength) !=
1660 sizeof(struct phy_control_reply))
1661 goto out;
1663 phy_control_reply = data_out +
1664 sizeof(struct phy_control_request);
1666 dtransportprintk(ioc,
1667 ioc_info(ioc, "phy_control - function_result(%d)\n",
1668 phy_control_reply->function_result));
1670 rc = 0;
1671 } else
1672 dtransportprintk(ioc,
1673 ioc_info(ioc, "phy_control - no reply\n"));
1675 issue_host_reset:
1676 if (issue_reset)
1677 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
1678 out:
1679 ioc->transport_cmds.status = MPT3_CMD_NOT_USED;
1680 if (data_out)
1681 dma_free_coherent(&ioc->pdev->dev, sz, data_out,
1682 data_out_dma);
1684 mutex_unlock(&ioc->transport_cmds.mutex);
1685 return rc;
1689 * _transport_phy_reset -
1690 * @phy: The sas phy object
1691 * @hard_reset:
1693 * Return: 0 for success, non-zero for failure.
1695 static int
1696 _transport_phy_reset(struct sas_phy *phy, int hard_reset)
1698 struct MPT3SAS_ADAPTER *ioc = phy_to_ioc(phy);
1699 Mpi2SasIoUnitControlReply_t mpi_reply;
1700 Mpi2SasIoUnitControlRequest_t mpi_request;
1701 struct hba_port *port = phy->hostdata;
1702 int port_id = port->port_id;
1703 unsigned long flags;
1705 spin_lock_irqsave(&ioc->sas_node_lock, flags);
1706 if (_transport_sas_node_find_by_sas_address(ioc,
1707 phy->identify.sas_address,
1708 mpt3sas_get_port_by_id(ioc, port_id, 0)) == NULL) {
1709 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1710 return -EINVAL;
1712 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1714 /* handle expander phys */
1715 if (phy->identify.sas_address != ioc->sas_hba.sas_address)
1716 return _transport_expander_phy_control(ioc, phy,
1717 (hard_reset == 1) ? SMP_PHY_CONTROL_HARD_RESET :
1718 SMP_PHY_CONTROL_LINK_RESET);
1720 /* handle hba phys */
1721 memset(&mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
1722 mpi_request.Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
1723 mpi_request.Operation = hard_reset ?
1724 MPI2_SAS_OP_PHY_HARD_RESET : MPI2_SAS_OP_PHY_LINK_RESET;
1725 mpi_request.PhyNum = phy->number;
1727 if ((mpt3sas_base_sas_iounit_control(ioc, &mpi_reply, &mpi_request))) {
1728 ioc_err(ioc, "failure at %s:%d/%s()!\n",
1729 __FILE__, __LINE__, __func__);
1730 return -ENXIO;
1733 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo)
1734 ioc_info(ioc, "phy(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
1735 phy->number, le16_to_cpu(mpi_reply.IOCStatus),
1736 le32_to_cpu(mpi_reply.IOCLogInfo));
1738 return 0;
1742 * _transport_phy_enable - enable/disable phys
1743 * @phy: The sas phy object
1744 * @enable: enable phy when true
1746 * Only support sas_host direct attached phys.
1747 * Return: 0 for success, non-zero for failure.
1749 static int
1750 _transport_phy_enable(struct sas_phy *phy, int enable)
1752 struct MPT3SAS_ADAPTER *ioc = phy_to_ioc(phy);
1753 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
1754 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
1755 Mpi2ConfigReply_t mpi_reply;
1756 u16 ioc_status;
1757 u16 sz;
1758 int rc = 0;
1759 unsigned long flags;
1760 int i, discovery_active;
1761 struct hba_port *port = phy->hostdata;
1762 int port_id = port->port_id;
1764 spin_lock_irqsave(&ioc->sas_node_lock, flags);
1765 if (_transport_sas_node_find_by_sas_address(ioc,
1766 phy->identify.sas_address,
1767 mpt3sas_get_port_by_id(ioc, port_id, 0)) == NULL) {
1768 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1769 return -EINVAL;
1771 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1773 /* handle expander phys */
1774 if (phy->identify.sas_address != ioc->sas_hba.sas_address)
1775 return _transport_expander_phy_control(ioc, phy,
1776 (enable == 1) ? SMP_PHY_CONTROL_LINK_RESET :
1777 SMP_PHY_CONTROL_DISABLE);
1779 /* handle hba phys */
1781 /* read sas_iounit page 0 */
1782 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
1783 sizeof(Mpi2SasIOUnit0PhyData_t));
1784 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
1785 if (!sas_iounit_pg0) {
1786 ioc_err(ioc, "failure at %s:%d/%s()!\n",
1787 __FILE__, __LINE__, __func__);
1788 rc = -ENOMEM;
1789 goto out;
1791 if ((mpt3sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
1792 sas_iounit_pg0, sz))) {
1793 ioc_err(ioc, "failure at %s:%d/%s()!\n",
1794 __FILE__, __LINE__, __func__);
1795 rc = -ENXIO;
1796 goto out;
1798 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1799 MPI2_IOCSTATUS_MASK;
1800 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1801 ioc_err(ioc, "failure at %s:%d/%s()!\n",
1802 __FILE__, __LINE__, __func__);
1803 rc = -EIO;
1804 goto out;
1807 /* unable to enable/disable phys when when discovery is active */
1808 for (i = 0, discovery_active = 0; i < ioc->sas_hba.num_phys ; i++) {
1809 if (sas_iounit_pg0->PhyData[i].PortFlags &
1810 MPI2_SASIOUNIT0_PORTFLAGS_DISCOVERY_IN_PROGRESS) {
1811 ioc_err(ioc, "discovery is active on port = %d, phy = %d: unable to enable/disable phys, try again later!\n",
1812 sas_iounit_pg0->PhyData[i].Port, i);
1813 discovery_active = 1;
1817 if (discovery_active) {
1818 rc = -EAGAIN;
1819 goto out;
1822 /* read sas_iounit page 1 */
1823 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
1824 sizeof(Mpi2SasIOUnit1PhyData_t));
1825 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
1826 if (!sas_iounit_pg1) {
1827 ioc_err(ioc, "failure at %s:%d/%s()!\n",
1828 __FILE__, __LINE__, __func__);
1829 rc = -ENOMEM;
1830 goto out;
1832 if ((mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
1833 sas_iounit_pg1, sz))) {
1834 ioc_err(ioc, "failure at %s:%d/%s()!\n",
1835 __FILE__, __LINE__, __func__);
1836 rc = -ENXIO;
1837 goto out;
1839 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1840 MPI2_IOCSTATUS_MASK;
1841 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1842 ioc_err(ioc, "failure at %s:%d/%s()!\n",
1843 __FILE__, __LINE__, __func__);
1844 rc = -EIO;
1845 goto out;
1848 /* copy Port/PortFlags/PhyFlags from page 0 */
1849 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
1850 sas_iounit_pg1->PhyData[i].Port =
1851 sas_iounit_pg0->PhyData[i].Port;
1852 sas_iounit_pg1->PhyData[i].PortFlags =
1853 (sas_iounit_pg0->PhyData[i].PortFlags &
1854 MPI2_SASIOUNIT0_PORTFLAGS_AUTO_PORT_CONFIG);
1855 sas_iounit_pg1->PhyData[i].PhyFlags =
1856 (sas_iounit_pg0->PhyData[i].PhyFlags &
1857 (MPI2_SASIOUNIT0_PHYFLAGS_ZONING_ENABLED +
1858 MPI2_SASIOUNIT0_PHYFLAGS_PHY_DISABLED));
1861 if (enable)
1862 sas_iounit_pg1->PhyData[phy->number].PhyFlags
1863 &= ~MPI2_SASIOUNIT1_PHYFLAGS_PHY_DISABLE;
1864 else
1865 sas_iounit_pg1->PhyData[phy->number].PhyFlags
1866 |= MPI2_SASIOUNIT1_PHYFLAGS_PHY_DISABLE;
1868 mpt3sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1, sz);
1870 /* link reset */
1871 if (enable)
1872 _transport_phy_reset(phy, 0);
1874 out:
1875 kfree(sas_iounit_pg1);
1876 kfree(sas_iounit_pg0);
1877 return rc;
1881 * _transport_phy_speed - set phy min/max link rates
1882 * @phy: The sas phy object
1883 * @rates: rates defined in sas_phy_linkrates
1885 * Only support sas_host direct attached phys.
1887 * Return: 0 for success, non-zero for failure.
1889 static int
1890 _transport_phy_speed(struct sas_phy *phy, struct sas_phy_linkrates *rates)
1892 struct MPT3SAS_ADAPTER *ioc = phy_to_ioc(phy);
1893 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
1894 Mpi2SasPhyPage0_t phy_pg0;
1895 Mpi2ConfigReply_t mpi_reply;
1896 u16 ioc_status;
1897 u16 sz;
1898 int i;
1899 int rc = 0;
1900 unsigned long flags;
1901 struct hba_port *port = phy->hostdata;
1902 int port_id = port->port_id;
1904 spin_lock_irqsave(&ioc->sas_node_lock, flags);
1905 if (_transport_sas_node_find_by_sas_address(ioc,
1906 phy->identify.sas_address,
1907 mpt3sas_get_port_by_id(ioc, port_id, 0)) == NULL) {
1908 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1909 return -EINVAL;
1911 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1913 if (!rates->minimum_linkrate)
1914 rates->minimum_linkrate = phy->minimum_linkrate;
1915 else if (rates->minimum_linkrate < phy->minimum_linkrate_hw)
1916 rates->minimum_linkrate = phy->minimum_linkrate_hw;
1918 if (!rates->maximum_linkrate)
1919 rates->maximum_linkrate = phy->maximum_linkrate;
1920 else if (rates->maximum_linkrate > phy->maximum_linkrate_hw)
1921 rates->maximum_linkrate = phy->maximum_linkrate_hw;
1923 /* handle expander phys */
1924 if (phy->identify.sas_address != ioc->sas_hba.sas_address) {
1925 phy->minimum_linkrate = rates->minimum_linkrate;
1926 phy->maximum_linkrate = rates->maximum_linkrate;
1927 return _transport_expander_phy_control(ioc, phy,
1928 SMP_PHY_CONTROL_LINK_RESET);
1931 /* handle hba phys */
1933 /* sas_iounit page 1 */
1934 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
1935 sizeof(Mpi2SasIOUnit1PhyData_t));
1936 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
1937 if (!sas_iounit_pg1) {
1938 ioc_err(ioc, "failure at %s:%d/%s()!\n",
1939 __FILE__, __LINE__, __func__);
1940 rc = -ENOMEM;
1941 goto out;
1943 if ((mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
1944 sas_iounit_pg1, sz))) {
1945 ioc_err(ioc, "failure at %s:%d/%s()!\n",
1946 __FILE__, __LINE__, __func__);
1947 rc = -ENXIO;
1948 goto out;
1950 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1951 MPI2_IOCSTATUS_MASK;
1952 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1953 ioc_err(ioc, "failure at %s:%d/%s()!\n",
1954 __FILE__, __LINE__, __func__);
1955 rc = -EIO;
1956 goto out;
1959 for (i = 0; i < ioc->sas_hba.num_phys; i++) {
1960 if (phy->number != i) {
1961 sas_iounit_pg1->PhyData[i].MaxMinLinkRate =
1962 (ioc->sas_hba.phy[i].phy->minimum_linkrate +
1963 (ioc->sas_hba.phy[i].phy->maximum_linkrate << 4));
1964 } else {
1965 sas_iounit_pg1->PhyData[i].MaxMinLinkRate =
1966 (rates->minimum_linkrate +
1967 (rates->maximum_linkrate << 4));
1971 if (mpt3sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1,
1972 sz)) {
1973 ioc_err(ioc, "failure at %s:%d/%s()!\n",
1974 __FILE__, __LINE__, __func__);
1975 rc = -ENXIO;
1976 goto out;
1979 /* link reset */
1980 _transport_phy_reset(phy, 0);
1982 /* read phy page 0, then update the rates in the sas transport phy */
1983 if (!mpt3sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
1984 phy->number)) {
1985 phy->minimum_linkrate = _transport_convert_phy_link_rate(
1986 phy_pg0.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
1987 phy->maximum_linkrate = _transport_convert_phy_link_rate(
1988 phy_pg0.ProgrammedLinkRate >> 4);
1989 phy->negotiated_linkrate = _transport_convert_phy_link_rate(
1990 phy_pg0.NegotiatedLinkRate &
1991 MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
1994 out:
1995 kfree(sas_iounit_pg1);
1996 return rc;
1999 static int
2000 _transport_map_smp_buffer(struct device *dev, struct bsg_buffer *buf,
2001 dma_addr_t *dma_addr, size_t *dma_len, void **p)
2003 /* Check if the request is split across multiple segments */
2004 if (buf->sg_cnt > 1) {
2005 *p = dma_alloc_coherent(dev, buf->payload_len, dma_addr,
2006 GFP_KERNEL);
2007 if (!*p)
2008 return -ENOMEM;
2009 *dma_len = buf->payload_len;
2010 } else {
2011 if (!dma_map_sg(dev, buf->sg_list, 1, DMA_BIDIRECTIONAL))
2012 return -ENOMEM;
2013 *dma_addr = sg_dma_address(buf->sg_list);
2014 *dma_len = sg_dma_len(buf->sg_list);
2015 *p = NULL;
2018 return 0;
2021 static void
2022 _transport_unmap_smp_buffer(struct device *dev, struct bsg_buffer *buf,
2023 dma_addr_t dma_addr, void *p)
2025 if (p)
2026 dma_free_coherent(dev, buf->payload_len, p, dma_addr);
2027 else
2028 dma_unmap_sg(dev, buf->sg_list, 1, DMA_BIDIRECTIONAL);
2032 * _transport_smp_handler - transport portal for smp passthru
2033 * @job: ?
2034 * @shost: shost object
2035 * @rphy: sas transport rphy object
2037 * This used primarily for smp_utils.
2038 * Example:
2039 * smp_rep_general /sys/class/bsg/expander-5:0
2041 static void
2042 _transport_smp_handler(struct bsg_job *job, struct Scsi_Host *shost,
2043 struct sas_rphy *rphy)
2045 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2046 Mpi2SmpPassthroughRequest_t *mpi_request;
2047 Mpi2SmpPassthroughReply_t *mpi_reply;
2048 int rc;
2049 u16 smid;
2050 void *psge;
2051 dma_addr_t dma_addr_in;
2052 dma_addr_t dma_addr_out;
2053 void *addr_in = NULL;
2054 void *addr_out = NULL;
2055 size_t dma_len_in;
2056 size_t dma_len_out;
2057 unsigned int reslen = 0;
2059 if (ioc->shost_recovery || ioc->pci_error_recovery) {
2060 ioc_info(ioc, "%s: host reset in progress!\n", __func__);
2061 rc = -EFAULT;
2062 goto job_done;
2065 rc = mutex_lock_interruptible(&ioc->transport_cmds.mutex);
2066 if (rc)
2067 goto job_done;
2069 if (ioc->transport_cmds.status != MPT3_CMD_NOT_USED) {
2070 ioc_err(ioc, "%s: transport_cmds in use\n",
2071 __func__);
2072 rc = -EAGAIN;
2073 goto out;
2075 ioc->transport_cmds.status = MPT3_CMD_PENDING;
2077 rc = _transport_map_smp_buffer(&ioc->pdev->dev, &job->request_payload,
2078 &dma_addr_out, &dma_len_out, &addr_out);
2079 if (rc)
2080 goto out;
2081 if (addr_out) {
2082 sg_copy_to_buffer(job->request_payload.sg_list,
2083 job->request_payload.sg_cnt, addr_out,
2084 job->request_payload.payload_len);
2087 rc = _transport_map_smp_buffer(&ioc->pdev->dev, &job->reply_payload,
2088 &dma_addr_in, &dma_len_in, &addr_in);
2089 if (rc)
2090 goto unmap_out;
2092 rc = mpt3sas_wait_for_ioc(ioc, IOC_OPERATIONAL_WAIT_COUNT);
2093 if (rc)
2094 goto unmap_in;
2096 smid = mpt3sas_base_get_smid(ioc, ioc->transport_cb_idx);
2097 if (!smid) {
2098 ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
2099 rc = -EAGAIN;
2100 goto unmap_in;
2103 rc = 0;
2104 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
2105 ioc->transport_cmds.smid = smid;
2107 memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
2108 mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
2109 mpi_request->PhysicalPort = _transport_get_port_id_by_rphy(ioc, rphy);
2110 mpi_request->SASAddress = (rphy) ?
2111 cpu_to_le64(rphy->identify.sas_address) :
2112 cpu_to_le64(ioc->sas_hba.sas_address);
2113 mpi_request->RequestDataLength = cpu_to_le16(dma_len_out - 4);
2114 psge = &mpi_request->SGL;
2116 ioc->build_sg(ioc, psge, dma_addr_out, dma_len_out - 4, dma_addr_in,
2117 dma_len_in - 4);
2119 dtransportprintk(ioc,
2120 ioc_info(ioc, "%s: sending smp request\n", __func__));
2122 init_completion(&ioc->transport_cmds.done);
2123 ioc->put_smid_default(ioc, smid);
2124 wait_for_completion_timeout(&ioc->transport_cmds.done, 10*HZ);
2126 if (!(ioc->transport_cmds.status & MPT3_CMD_COMPLETE)) {
2127 ioc_err(ioc, "%s: timeout\n", __func__);
2128 _debug_dump_mf(mpi_request,
2129 sizeof(Mpi2SmpPassthroughRequest_t)/4);
2130 if (!(ioc->transport_cmds.status & MPT3_CMD_RESET)) {
2131 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
2132 rc = -ETIMEDOUT;
2133 goto unmap_in;
2137 dtransportprintk(ioc, ioc_info(ioc, "%s - complete\n", __func__));
2139 if (!(ioc->transport_cmds.status & MPT3_CMD_REPLY_VALID)) {
2140 dtransportprintk(ioc,
2141 ioc_info(ioc, "%s: no reply\n", __func__));
2142 rc = -ENXIO;
2143 goto unmap_in;
2146 mpi_reply = ioc->transport_cmds.reply;
2148 dtransportprintk(ioc,
2149 ioc_info(ioc, "%s: reply data transfer size(%d)\n",
2150 __func__,
2151 le16_to_cpu(mpi_reply->ResponseDataLength)));
2153 memcpy(job->reply, mpi_reply, sizeof(*mpi_reply));
2154 job->reply_len = sizeof(*mpi_reply);
2155 reslen = le16_to_cpu(mpi_reply->ResponseDataLength);
2157 if (addr_in) {
2158 sg_copy_to_buffer(job->reply_payload.sg_list,
2159 job->reply_payload.sg_cnt, addr_in,
2160 job->reply_payload.payload_len);
2163 rc = 0;
2164 unmap_in:
2165 _transport_unmap_smp_buffer(&ioc->pdev->dev, &job->reply_payload,
2166 dma_addr_in, addr_in);
2167 unmap_out:
2168 _transport_unmap_smp_buffer(&ioc->pdev->dev, &job->request_payload,
2169 dma_addr_out, addr_out);
2170 out:
2171 ioc->transport_cmds.status = MPT3_CMD_NOT_USED;
2172 mutex_unlock(&ioc->transport_cmds.mutex);
2173 job_done:
2174 bsg_job_done(job, rc, reslen);
2177 struct sas_function_template mpt3sas_transport_functions = {
2178 .get_linkerrors = _transport_get_linkerrors,
2179 .get_enclosure_identifier = _transport_get_enclosure_identifier,
2180 .get_bay_identifier = _transport_get_bay_identifier,
2181 .phy_reset = _transport_phy_reset,
2182 .phy_enable = _transport_phy_enable,
2183 .set_phy_speed = _transport_phy_speed,
2184 .smp_handler = _transport_smp_handler,
2187 struct scsi_transport_template *mpt3sas_transport_template;