PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / scsi / mpt2sas / mpt2sas_transport.c
blob410f4a3e88887a6f0087c06f9da42f22b61f556d
1 /*
2 * SAS Transport Layer for MPT (Message Passing Technology) based controllers
4 * This code is based on drivers/scsi/mpt2sas/mpt2_transport.c
5 * Copyright (C) 2007-2013 LSI Corporation
6 * (mailto:DL-MPTFusionLinux@lsi.com)
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * NO WARRANTY
19 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
20 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
21 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
22 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
23 * solely responsible for determining the appropriateness of using and
24 * distributing the Program and assumes all risks associated with its
25 * exercise of rights under this Agreement, including but not limited to
26 * the risks and costs of program errors, damage to or loss of data,
27 * programs or equipment, and unavailability or interruption of operations.
29 * DISCLAIMER OF LIABILITY
30 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
31 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
33 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
35 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
36 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
38 * You should have received a copy of the GNU General Public License
39 * along with this program; if not, write to the Free Software
40 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
41 * USA.
44 #include <linux/module.h>
45 #include <linux/kernel.h>
46 #include <linux/init.h>
47 #include <linux/errno.h>
48 #include <linux/sched.h>
49 #include <linux/workqueue.h>
50 #include <linux/delay.h>
51 #include <linux/pci.h>
52 #include <linux/slab.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 "mpt2sas_base.h"
62 /**
63 * _transport_sas_node_find_by_sas_address - sas node search
64 * @ioc: per adapter object
65 * @sas_address: sas address of expander or sas host
66 * Context: Calling function should acquire ioc->sas_node_lock.
68 * Search for either hba phys or expander device based on handle, then returns
69 * the sas_node object.
71 static struct _sas_node *
72 _transport_sas_node_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
73 u64 sas_address)
75 if (ioc->sas_hba.sas_address == sas_address)
76 return &ioc->sas_hba;
77 else
78 return mpt2sas_scsih_expander_find_by_sas_address(ioc,
79 sas_address);
82 /**
83 * _transport_convert_phy_link_rate -
84 * @link_rate: link rate returned from mpt firmware
86 * Convert link_rate from mpi fusion into sas_transport form.
88 static enum sas_linkrate
89 _transport_convert_phy_link_rate(u8 link_rate)
91 enum sas_linkrate rc;
93 switch (link_rate) {
94 case MPI2_SAS_NEG_LINK_RATE_1_5:
95 rc = SAS_LINK_RATE_1_5_GBPS;
96 break;
97 case MPI2_SAS_NEG_LINK_RATE_3_0:
98 rc = SAS_LINK_RATE_3_0_GBPS;
99 break;
100 case MPI2_SAS_NEG_LINK_RATE_6_0:
101 rc = SAS_LINK_RATE_6_0_GBPS;
102 break;
103 case MPI2_SAS_NEG_LINK_RATE_PHY_DISABLED:
104 rc = SAS_PHY_DISABLED;
105 break;
106 case MPI2_SAS_NEG_LINK_RATE_NEGOTIATION_FAILED:
107 rc = SAS_LINK_RATE_FAILED;
108 break;
109 case MPI2_SAS_NEG_LINK_RATE_PORT_SELECTOR:
110 rc = SAS_SATA_PORT_SELECTOR;
111 break;
112 case MPI2_SAS_NEG_LINK_RATE_SMP_RESET_IN_PROGRESS:
113 rc = SAS_PHY_RESET_IN_PROGRESS;
114 break;
115 default:
116 case MPI2_SAS_NEG_LINK_RATE_SATA_OOB_COMPLETE:
117 case MPI2_SAS_NEG_LINK_RATE_UNKNOWN_LINK_RATE:
118 rc = SAS_LINK_RATE_UNKNOWN;
119 break;
121 return rc;
125 * _transport_set_identify - set identify for phys and end devices
126 * @ioc: per adapter object
127 * @handle: device handle
128 * @identify: sas identify info
130 * Populates sas identify info.
132 * Returns 0 for success, non-zero for failure.
134 static int
135 _transport_set_identify(struct MPT2SAS_ADAPTER *ioc, u16 handle,
136 struct sas_identify *identify)
138 Mpi2SasDevicePage0_t sas_device_pg0;
139 Mpi2ConfigReply_t mpi_reply;
140 u32 device_info;
141 u32 ioc_status;
143 if (ioc->shost_recovery || ioc->pci_error_recovery) {
144 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
145 __func__, ioc->name);
146 return -EFAULT;
149 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
150 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
151 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
153 ioc->name, __FILE__, __LINE__, __func__);
154 return -ENXIO;
157 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
158 MPI2_IOCSTATUS_MASK;
159 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
160 printk(MPT2SAS_ERR_FMT "handle(0x%04x), ioc_status(0x%04x)"
161 "\nfailure at %s:%d/%s()!\n", ioc->name, handle, ioc_status,
162 __FILE__, __LINE__, __func__);
163 return -EIO;
166 memset(identify, 0, sizeof(struct sas_identify));
167 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
169 /* sas_address */
170 identify->sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
172 /* phy number of the parent device this device is linked to */
173 identify->phy_identifier = sas_device_pg0.PhyNum;
175 /* device_type */
176 switch (device_info & MPI2_SAS_DEVICE_INFO_MASK_DEVICE_TYPE) {
177 case MPI2_SAS_DEVICE_INFO_NO_DEVICE:
178 identify->device_type = SAS_PHY_UNUSED;
179 break;
180 case MPI2_SAS_DEVICE_INFO_END_DEVICE:
181 identify->device_type = SAS_END_DEVICE;
182 break;
183 case MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER:
184 identify->device_type = SAS_EDGE_EXPANDER_DEVICE;
185 break;
186 case MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER:
187 identify->device_type = SAS_FANOUT_EXPANDER_DEVICE;
188 break;
191 /* initiator_port_protocols */
192 if (device_info & MPI2_SAS_DEVICE_INFO_SSP_INITIATOR)
193 identify->initiator_port_protocols |= SAS_PROTOCOL_SSP;
194 if (device_info & MPI2_SAS_DEVICE_INFO_STP_INITIATOR)
195 identify->initiator_port_protocols |= SAS_PROTOCOL_STP;
196 if (device_info & MPI2_SAS_DEVICE_INFO_SMP_INITIATOR)
197 identify->initiator_port_protocols |= SAS_PROTOCOL_SMP;
198 if (device_info & MPI2_SAS_DEVICE_INFO_SATA_HOST)
199 identify->initiator_port_protocols |= SAS_PROTOCOL_SATA;
201 /* target_port_protocols */
202 if (device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET)
203 identify->target_port_protocols |= SAS_PROTOCOL_SSP;
204 if (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET)
205 identify->target_port_protocols |= SAS_PROTOCOL_STP;
206 if (device_info & MPI2_SAS_DEVICE_INFO_SMP_TARGET)
207 identify->target_port_protocols |= SAS_PROTOCOL_SMP;
208 if (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
209 identify->target_port_protocols |= SAS_PROTOCOL_SATA;
211 return 0;
215 * mpt2sas_transport_done - internal transport layer callback handler.
216 * @ioc: per adapter object
217 * @smid: system request message index
218 * @msix_index: MSIX table index supplied by the OS
219 * @reply: reply message frame(lower 32bit addr)
221 * Callback handler when sending internal generated transport cmds.
222 * The callback index passed is `ioc->transport_cb_idx`
224 * Return 1 meaning mf should be freed from _base_interrupt
225 * 0 means the mf is freed from this function.
228 mpt2sas_transport_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
229 u32 reply)
231 MPI2DefaultReply_t *mpi_reply;
233 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
234 if (ioc->transport_cmds.status == MPT2_CMD_NOT_USED)
235 return 1;
236 if (ioc->transport_cmds.smid != smid)
237 return 1;
238 ioc->transport_cmds.status |= MPT2_CMD_COMPLETE;
239 if (mpi_reply) {
240 memcpy(ioc->transport_cmds.reply, mpi_reply,
241 mpi_reply->MsgLength*4);
242 ioc->transport_cmds.status |= MPT2_CMD_REPLY_VALID;
244 ioc->transport_cmds.status &= ~MPT2_CMD_PENDING;
245 complete(&ioc->transport_cmds.done);
246 return 1;
249 /* report manufacture request structure */
250 struct rep_manu_request{
251 u8 smp_frame_type;
252 u8 function;
253 u8 reserved;
254 u8 request_length;
257 /* report manufacture reply structure */
258 struct rep_manu_reply{
259 u8 smp_frame_type; /* 0x41 */
260 u8 function; /* 0x01 */
261 u8 function_result;
262 u8 response_length;
263 u16 expander_change_count;
264 u8 reserved0[2];
265 u8 sas_format;
266 u8 reserved2[3];
267 u8 vendor_id[SAS_EXPANDER_VENDOR_ID_LEN];
268 u8 product_id[SAS_EXPANDER_PRODUCT_ID_LEN];
269 u8 product_rev[SAS_EXPANDER_PRODUCT_REV_LEN];
270 u8 component_vendor_id[SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN];
271 u16 component_id;
272 u8 component_revision_id;
273 u8 reserved3;
274 u8 vendor_specific[8];
278 * _transport_expander_report_manufacture - obtain SMP report_manufacture
279 * @ioc: per adapter object
280 * @sas_address: expander sas address
281 * @edev: the sas_expander_device object
283 * Fills in the sas_expander_device object when SMP port is created.
285 * Returns 0 for success, non-zero for failure.
287 static int
288 _transport_expander_report_manufacture(struct MPT2SAS_ADAPTER *ioc,
289 u64 sas_address, struct sas_expander_device *edev)
291 Mpi2SmpPassthroughRequest_t *mpi_request;
292 Mpi2SmpPassthroughReply_t *mpi_reply;
293 struct rep_manu_reply *manufacture_reply;
294 struct rep_manu_request *manufacture_request;
295 int rc;
296 u16 smid;
297 u32 ioc_state;
298 unsigned long timeleft;
299 void *psge;
300 u32 sgl_flags;
301 u8 issue_reset = 0;
302 void *data_out = NULL;
303 dma_addr_t data_out_dma;
304 u32 sz;
305 u16 wait_state_count;
307 if (ioc->shost_recovery || ioc->pci_error_recovery) {
308 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
309 __func__, ioc->name);
310 return -EFAULT;
313 mutex_lock(&ioc->transport_cmds.mutex);
315 if (ioc->transport_cmds.status != MPT2_CMD_NOT_USED) {
316 printk(MPT2SAS_ERR_FMT "%s: transport_cmds in use\n",
317 ioc->name, __func__);
318 rc = -EAGAIN;
319 goto out;
321 ioc->transport_cmds.status = MPT2_CMD_PENDING;
323 wait_state_count = 0;
324 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
325 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
326 if (wait_state_count++ == 10) {
327 printk(MPT2SAS_ERR_FMT
328 "%s: failed due to ioc not operational\n",
329 ioc->name, __func__);
330 rc = -EFAULT;
331 goto out;
333 ssleep(1);
334 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
335 printk(MPT2SAS_INFO_FMT "%s: waiting for "
336 "operational state(count=%d)\n", ioc->name,
337 __func__, wait_state_count);
339 if (wait_state_count)
340 printk(MPT2SAS_INFO_FMT "%s: ioc is operational\n",
341 ioc->name, __func__);
343 smid = mpt2sas_base_get_smid(ioc, ioc->transport_cb_idx);
344 if (!smid) {
345 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
346 ioc->name, __func__);
347 rc = -EAGAIN;
348 goto out;
351 rc = 0;
352 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
353 ioc->transport_cmds.smid = smid;
355 sz = sizeof(struct rep_manu_request) + sizeof(struct rep_manu_reply);
356 data_out = pci_alloc_consistent(ioc->pdev, sz, &data_out_dma);
358 if (!data_out) {
359 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
360 __LINE__, __func__);
361 rc = -ENOMEM;
362 mpt2sas_base_free_smid(ioc, smid);
363 goto out;
366 manufacture_request = data_out;
367 manufacture_request->smp_frame_type = 0x40;
368 manufacture_request->function = 1;
369 manufacture_request->reserved = 0;
370 manufacture_request->request_length = 0;
372 memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
373 mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
374 mpi_request->PhysicalPort = 0xFF;
375 mpi_request->VF_ID = 0; /* TODO */
376 mpi_request->VP_ID = 0;
377 mpi_request->SASAddress = cpu_to_le64(sas_address);
378 mpi_request->RequestDataLength =
379 cpu_to_le16(sizeof(struct rep_manu_request));
380 psge = &mpi_request->SGL;
382 /* WRITE sgel first */
383 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
384 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
385 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
386 ioc->base_add_sg_single(psge, sgl_flags |
387 sizeof(struct rep_manu_request), data_out_dma);
389 /* incr sgel */
390 psge += ioc->sge_size;
392 /* READ sgel last */
393 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
394 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
395 MPI2_SGE_FLAGS_END_OF_LIST);
396 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
397 ioc->base_add_sg_single(psge, sgl_flags |
398 sizeof(struct rep_manu_reply), data_out_dma +
399 sizeof(struct rep_manu_request));
401 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "report_manufacture - "
402 "send to sas_addr(0x%016llx)\n", ioc->name,
403 (unsigned long long)sas_address));
404 init_completion(&ioc->transport_cmds.done);
405 mpt2sas_base_put_smid_default(ioc, smid);
406 timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done,
407 10*HZ);
409 if (!(ioc->transport_cmds.status & MPT2_CMD_COMPLETE)) {
410 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
411 ioc->name, __func__);
412 _debug_dump_mf(mpi_request,
413 sizeof(Mpi2SmpPassthroughRequest_t)/4);
414 if (!(ioc->transport_cmds.status & MPT2_CMD_RESET))
415 issue_reset = 1;
416 goto issue_host_reset;
419 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "report_manufacture - "
420 "complete\n", ioc->name));
422 if (ioc->transport_cmds.status & MPT2_CMD_REPLY_VALID) {
423 u8 *tmp;
425 mpi_reply = ioc->transport_cmds.reply;
427 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
428 "report_manufacture - reply data transfer size(%d)\n",
429 ioc->name, le16_to_cpu(mpi_reply->ResponseDataLength)));
431 if (le16_to_cpu(mpi_reply->ResponseDataLength) !=
432 sizeof(struct rep_manu_reply))
433 goto out;
435 manufacture_reply = data_out + sizeof(struct rep_manu_request);
436 strncpy(edev->vendor_id, manufacture_reply->vendor_id,
437 SAS_EXPANDER_VENDOR_ID_LEN);
438 strncpy(edev->product_id, manufacture_reply->product_id,
439 SAS_EXPANDER_PRODUCT_ID_LEN);
440 strncpy(edev->product_rev, manufacture_reply->product_rev,
441 SAS_EXPANDER_PRODUCT_REV_LEN);
442 edev->level = manufacture_reply->sas_format & 1;
443 if (edev->level) {
444 strncpy(edev->component_vendor_id,
445 manufacture_reply->component_vendor_id,
446 SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN);
447 tmp = (u8 *)&manufacture_reply->component_id;
448 edev->component_id = tmp[0] << 8 | tmp[1];
449 edev->component_revision_id =
450 manufacture_reply->component_revision_id;
452 } else
453 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
454 "report_manufacture - no reply\n", ioc->name));
456 issue_host_reset:
457 if (issue_reset)
458 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
459 FORCE_BIG_HAMMER);
460 out:
461 ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
462 if (data_out)
463 pci_free_consistent(ioc->pdev, sz, data_out, data_out_dma);
465 mutex_unlock(&ioc->transport_cmds.mutex);
466 return rc;
470 * _transport_delete_port - helper function to removing a port
471 * @ioc: per adapter object
472 * @mpt2sas_port: mpt2sas per port object
474 * Returns nothing.
476 static void
477 _transport_delete_port(struct MPT2SAS_ADAPTER *ioc,
478 struct _sas_port *mpt2sas_port)
480 u64 sas_address = mpt2sas_port->remote_identify.sas_address;
481 enum sas_device_type device_type =
482 mpt2sas_port->remote_identify.device_type;
484 dev_printk(KERN_INFO, &mpt2sas_port->port->dev,
485 "remove: sas_addr(0x%016llx)\n",
486 (unsigned long long) sas_address);
488 ioc->logging_level |= MPT_DEBUG_TRANSPORT;
489 if (device_type == SAS_END_DEVICE)
490 mpt2sas_device_remove_by_sas_address(ioc, sas_address);
491 else if (device_type == SAS_EDGE_EXPANDER_DEVICE ||
492 device_type == SAS_FANOUT_EXPANDER_DEVICE)
493 mpt2sas_expander_remove(ioc, sas_address);
494 ioc->logging_level &= ~MPT_DEBUG_TRANSPORT;
498 * _transport_delete_phy - helper function to removing single phy from port
499 * @ioc: per adapter object
500 * @mpt2sas_port: mpt2sas per port object
501 * @mpt2sas_phy: mpt2sas per phy object
503 * Returns nothing.
505 static void
506 _transport_delete_phy(struct MPT2SAS_ADAPTER *ioc,
507 struct _sas_port *mpt2sas_port, struct _sas_phy *mpt2sas_phy)
509 u64 sas_address = mpt2sas_port->remote_identify.sas_address;
511 dev_printk(KERN_INFO, &mpt2sas_phy->phy->dev,
512 "remove: sas_addr(0x%016llx), phy(%d)\n",
513 (unsigned long long) sas_address, mpt2sas_phy->phy_id);
515 list_del(&mpt2sas_phy->port_siblings);
516 mpt2sas_port->num_phys--;
517 sas_port_delete_phy(mpt2sas_port->port, mpt2sas_phy->phy);
518 mpt2sas_phy->phy_belongs_to_port = 0;
522 * _transport_add_phy - helper function to adding single phy to port
523 * @ioc: per adapter object
524 * @mpt2sas_port: mpt2sas per port object
525 * @mpt2sas_phy: mpt2sas per phy object
527 * Returns nothing.
529 static void
530 _transport_add_phy(struct MPT2SAS_ADAPTER *ioc, struct _sas_port *mpt2sas_port,
531 struct _sas_phy *mpt2sas_phy)
533 u64 sas_address = mpt2sas_port->remote_identify.sas_address;
535 dev_printk(KERN_INFO, &mpt2sas_phy->phy->dev,
536 "add: sas_addr(0x%016llx), phy(%d)\n", (unsigned long long)
537 sas_address, mpt2sas_phy->phy_id);
539 list_add_tail(&mpt2sas_phy->port_siblings, &mpt2sas_port->phy_list);
540 mpt2sas_port->num_phys++;
541 sas_port_add_phy(mpt2sas_port->port, mpt2sas_phy->phy);
542 mpt2sas_phy->phy_belongs_to_port = 1;
546 * _transport_add_phy_to_an_existing_port - adding new phy to existing port
547 * @ioc: per adapter object
548 * @sas_node: sas node object (either expander or sas host)
549 * @mpt2sas_phy: mpt2sas per phy object
550 * @sas_address: sas address of device/expander were phy needs to be added to
552 * Returns nothing.
554 static void
555 _transport_add_phy_to_an_existing_port(struct MPT2SAS_ADAPTER *ioc,
556 struct _sas_node *sas_node, struct _sas_phy *mpt2sas_phy, u64 sas_address)
558 struct _sas_port *mpt2sas_port;
559 struct _sas_phy *phy_srch;
561 if (mpt2sas_phy->phy_belongs_to_port == 1)
562 return;
564 list_for_each_entry(mpt2sas_port, &sas_node->sas_port_list,
565 port_list) {
566 if (mpt2sas_port->remote_identify.sas_address !=
567 sas_address)
568 continue;
569 list_for_each_entry(phy_srch, &mpt2sas_port->phy_list,
570 port_siblings) {
571 if (phy_srch == mpt2sas_phy)
572 return;
574 _transport_add_phy(ioc, mpt2sas_port, mpt2sas_phy);
575 return;
581 * _transport_del_phy_from_an_existing_port - delete phy from existing port
582 * @ioc: per adapter object
583 * @sas_node: sas node object (either expander or sas host)
584 * @mpt2sas_phy: mpt2sas per phy object
586 * Returns nothing.
588 static void
589 _transport_del_phy_from_an_existing_port(struct MPT2SAS_ADAPTER *ioc,
590 struct _sas_node *sas_node, struct _sas_phy *mpt2sas_phy)
592 struct _sas_port *mpt2sas_port, *next;
593 struct _sas_phy *phy_srch;
595 if (mpt2sas_phy->phy_belongs_to_port == 0)
596 return;
598 list_for_each_entry_safe(mpt2sas_port, next, &sas_node->sas_port_list,
599 port_list) {
600 list_for_each_entry(phy_srch, &mpt2sas_port->phy_list,
601 port_siblings) {
602 if (phy_srch != mpt2sas_phy)
603 continue;
604 if (mpt2sas_port->num_phys == 1)
605 _transport_delete_port(ioc, mpt2sas_port);
606 else
607 _transport_delete_phy(ioc, mpt2sas_port,
608 mpt2sas_phy);
609 return;
615 * _transport_sanity_check - sanity check when adding a new port
616 * @ioc: per adapter object
617 * @sas_node: sas node object (either expander or sas host)
618 * @sas_address: sas address of device being added
620 * See the explanation above from _transport_delete_duplicate_port
622 static void
623 _transport_sanity_check(struct MPT2SAS_ADAPTER *ioc, struct _sas_node *sas_node,
624 u64 sas_address)
626 int i;
628 for (i = 0; i < sas_node->num_phys; i++) {
629 if (sas_node->phy[i].remote_identify.sas_address != sas_address)
630 continue;
631 if (sas_node->phy[i].phy_belongs_to_port == 1)
632 _transport_del_phy_from_an_existing_port(ioc, sas_node,
633 &sas_node->phy[i]);
638 * mpt2sas_transport_port_add - insert port to the list
639 * @ioc: per adapter object
640 * @handle: handle of attached device
641 * @sas_address: sas address of parent expander or sas host
642 * Context: This function will acquire ioc->sas_node_lock.
644 * Adding new port object to the sas_node->sas_port_list.
646 * Returns mpt2sas_port.
648 struct _sas_port *
649 mpt2sas_transport_port_add(struct MPT2SAS_ADAPTER *ioc, u16 handle,
650 u64 sas_address)
652 struct _sas_phy *mpt2sas_phy, *next;
653 struct _sas_port *mpt2sas_port;
654 unsigned long flags;
655 struct _sas_node *sas_node;
656 struct sas_rphy *rphy;
657 int i;
658 struct sas_port *port;
660 mpt2sas_port = kzalloc(sizeof(struct _sas_port),
661 GFP_KERNEL);
662 if (!mpt2sas_port) {
663 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
664 ioc->name, __FILE__, __LINE__, __func__);
665 return NULL;
668 INIT_LIST_HEAD(&mpt2sas_port->port_list);
669 INIT_LIST_HEAD(&mpt2sas_port->phy_list);
670 spin_lock_irqsave(&ioc->sas_node_lock, flags);
671 sas_node = _transport_sas_node_find_by_sas_address(ioc, sas_address);
672 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
674 if (!sas_node) {
675 printk(MPT2SAS_ERR_FMT "%s: Could not find "
676 "parent sas_address(0x%016llx)!\n", ioc->name,
677 __func__, (unsigned long long)sas_address);
678 goto out_fail;
681 if ((_transport_set_identify(ioc, handle,
682 &mpt2sas_port->remote_identify))) {
683 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
684 ioc->name, __FILE__, __LINE__, __func__);
685 goto out_fail;
688 if (mpt2sas_port->remote_identify.device_type == SAS_PHY_UNUSED) {
689 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
690 ioc->name, __FILE__, __LINE__, __func__);
691 goto out_fail;
694 _transport_sanity_check(ioc, sas_node,
695 mpt2sas_port->remote_identify.sas_address);
697 for (i = 0; i < sas_node->num_phys; i++) {
698 if (sas_node->phy[i].remote_identify.sas_address !=
699 mpt2sas_port->remote_identify.sas_address)
700 continue;
701 list_add_tail(&sas_node->phy[i].port_siblings,
702 &mpt2sas_port->phy_list);
703 mpt2sas_port->num_phys++;
706 if (!mpt2sas_port->num_phys) {
707 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
708 ioc->name, __FILE__, __LINE__, __func__);
709 goto out_fail;
712 port = sas_port_alloc_num(sas_node->parent_dev);
713 if ((sas_port_add(port))) {
714 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
715 ioc->name, __FILE__, __LINE__, __func__);
716 goto out_fail;
719 list_for_each_entry(mpt2sas_phy, &mpt2sas_port->phy_list,
720 port_siblings) {
721 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
722 dev_printk(KERN_INFO, &port->dev, "add: handle(0x%04x)"
723 ", sas_addr(0x%016llx), phy(%d)\n", handle,
724 (unsigned long long)
725 mpt2sas_port->remote_identify.sas_address,
726 mpt2sas_phy->phy_id);
727 sas_port_add_phy(port, mpt2sas_phy->phy);
728 mpt2sas_phy->phy_belongs_to_port = 1;
731 mpt2sas_port->port = port;
732 if (mpt2sas_port->remote_identify.device_type == SAS_END_DEVICE)
733 rphy = sas_end_device_alloc(port);
734 else
735 rphy = sas_expander_alloc(port,
736 mpt2sas_port->remote_identify.device_type);
738 rphy->identify = mpt2sas_port->remote_identify;
739 if ((sas_rphy_add(rphy))) {
740 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
741 ioc->name, __FILE__, __LINE__, __func__);
743 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
744 dev_printk(KERN_INFO, &rphy->dev, "add: handle(0x%04x), "
745 "sas_addr(0x%016llx)\n", handle,
746 (unsigned long long)
747 mpt2sas_port->remote_identify.sas_address);
748 mpt2sas_port->rphy = rphy;
749 spin_lock_irqsave(&ioc->sas_node_lock, flags);
750 list_add_tail(&mpt2sas_port->port_list, &sas_node->sas_port_list);
751 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
753 /* fill in report manufacture */
754 if (mpt2sas_port->remote_identify.device_type ==
755 MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
756 mpt2sas_port->remote_identify.device_type ==
757 MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER)
758 _transport_expander_report_manufacture(ioc,
759 mpt2sas_port->remote_identify.sas_address,
760 rphy_to_expander_device(rphy));
762 return mpt2sas_port;
764 out_fail:
765 list_for_each_entry_safe(mpt2sas_phy, next, &mpt2sas_port->phy_list,
766 port_siblings)
767 list_del(&mpt2sas_phy->port_siblings);
768 kfree(mpt2sas_port);
769 return NULL;
773 * mpt2sas_transport_port_remove - remove port from the list
774 * @ioc: per adapter object
775 * @sas_address: sas address of attached device
776 * @sas_address_parent: sas address of parent expander or sas host
777 * Context: This function will acquire ioc->sas_node_lock.
779 * Removing object and freeing associated memory from the
780 * ioc->sas_port_list.
782 * Return nothing.
784 void
785 mpt2sas_transport_port_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
786 u64 sas_address_parent)
788 int i;
789 unsigned long flags;
790 struct _sas_port *mpt2sas_port, *next;
791 struct _sas_node *sas_node;
792 u8 found = 0;
793 struct _sas_phy *mpt2sas_phy, *next_phy;
795 spin_lock_irqsave(&ioc->sas_node_lock, flags);
796 sas_node = _transport_sas_node_find_by_sas_address(ioc,
797 sas_address_parent);
798 if (!sas_node) {
799 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
800 return;
802 list_for_each_entry_safe(mpt2sas_port, next, &sas_node->sas_port_list,
803 port_list) {
804 if (mpt2sas_port->remote_identify.sas_address != sas_address)
805 continue;
806 found = 1;
807 list_del(&mpt2sas_port->port_list);
808 goto out;
810 out:
811 if (!found) {
812 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
813 return;
816 for (i = 0; i < sas_node->num_phys; i++) {
817 if (sas_node->phy[i].remote_identify.sas_address == sas_address)
818 memset(&sas_node->phy[i].remote_identify, 0 ,
819 sizeof(struct sas_identify));
822 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
823 list_for_each_entry_safe(mpt2sas_phy, next_phy,
824 &mpt2sas_port->phy_list, port_siblings) {
825 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
826 dev_printk(KERN_INFO, &mpt2sas_port->port->dev,
827 "remove: sas_addr(0x%016llx), phy(%d)\n",
828 (unsigned long long)
829 mpt2sas_port->remote_identify.sas_address,
830 mpt2sas_phy->phy_id);
831 mpt2sas_phy->phy_belongs_to_port = 0;
832 sas_port_delete_phy(mpt2sas_port->port, mpt2sas_phy->phy);
833 list_del(&mpt2sas_phy->port_siblings);
835 sas_port_delete(mpt2sas_port->port);
836 kfree(mpt2sas_port);
840 * mpt2sas_transport_add_host_phy - report sas_host phy to transport
841 * @ioc: per adapter object
842 * @mpt2sas_phy: mpt2sas per phy object
843 * @phy_pg0: sas phy page 0
844 * @parent_dev: parent device class object
846 * Returns 0 for success, non-zero for failure.
849 mpt2sas_transport_add_host_phy(struct MPT2SAS_ADAPTER *ioc, struct _sas_phy
850 *mpt2sas_phy, Mpi2SasPhyPage0_t phy_pg0, struct device *parent_dev)
852 struct sas_phy *phy;
853 int phy_index = mpt2sas_phy->phy_id;
856 INIT_LIST_HEAD(&mpt2sas_phy->port_siblings);
857 phy = sas_phy_alloc(parent_dev, phy_index);
858 if (!phy) {
859 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
860 ioc->name, __FILE__, __LINE__, __func__);
861 return -1;
863 if ((_transport_set_identify(ioc, mpt2sas_phy->handle,
864 &mpt2sas_phy->identify))) {
865 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
866 ioc->name, __FILE__, __LINE__, __func__);
867 return -1;
869 phy->identify = mpt2sas_phy->identify;
870 mpt2sas_phy->attached_handle = le16_to_cpu(phy_pg0.AttachedDevHandle);
871 if (mpt2sas_phy->attached_handle)
872 _transport_set_identify(ioc, mpt2sas_phy->attached_handle,
873 &mpt2sas_phy->remote_identify);
874 phy->identify.phy_identifier = mpt2sas_phy->phy_id;
875 phy->negotiated_linkrate = _transport_convert_phy_link_rate(
876 phy_pg0.NegotiatedLinkRate & MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
877 phy->minimum_linkrate_hw = _transport_convert_phy_link_rate(
878 phy_pg0.HwLinkRate & MPI2_SAS_HWRATE_MIN_RATE_MASK);
879 phy->maximum_linkrate_hw = _transport_convert_phy_link_rate(
880 phy_pg0.HwLinkRate >> 4);
881 phy->minimum_linkrate = _transport_convert_phy_link_rate(
882 phy_pg0.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
883 phy->maximum_linkrate = _transport_convert_phy_link_rate(
884 phy_pg0.ProgrammedLinkRate >> 4);
886 if ((sas_phy_add(phy))) {
887 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
888 ioc->name, __FILE__, __LINE__, __func__);
889 sas_phy_free(phy);
890 return -1;
892 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
893 dev_printk(KERN_INFO, &phy->dev,
894 "add: handle(0x%04x), sas_addr(0x%016llx)\n"
895 "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
896 mpt2sas_phy->handle, (unsigned long long)
897 mpt2sas_phy->identify.sas_address,
898 mpt2sas_phy->attached_handle,
899 (unsigned long long)
900 mpt2sas_phy->remote_identify.sas_address);
901 mpt2sas_phy->phy = phy;
902 return 0;
907 * mpt2sas_transport_add_expander_phy - report expander phy to transport
908 * @ioc: per adapter object
909 * @mpt2sas_phy: mpt2sas per phy object
910 * @expander_pg1: expander page 1
911 * @parent_dev: parent device class object
913 * Returns 0 for success, non-zero for failure.
916 mpt2sas_transport_add_expander_phy(struct MPT2SAS_ADAPTER *ioc, struct _sas_phy
917 *mpt2sas_phy, Mpi2ExpanderPage1_t expander_pg1, struct device *parent_dev)
919 struct sas_phy *phy;
920 int phy_index = mpt2sas_phy->phy_id;
922 INIT_LIST_HEAD(&mpt2sas_phy->port_siblings);
923 phy = sas_phy_alloc(parent_dev, phy_index);
924 if (!phy) {
925 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
926 ioc->name, __FILE__, __LINE__, __func__);
927 return -1;
929 if ((_transport_set_identify(ioc, mpt2sas_phy->handle,
930 &mpt2sas_phy->identify))) {
931 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
932 ioc->name, __FILE__, __LINE__, __func__);
933 return -1;
935 phy->identify = mpt2sas_phy->identify;
936 mpt2sas_phy->attached_handle =
937 le16_to_cpu(expander_pg1.AttachedDevHandle);
938 if (mpt2sas_phy->attached_handle)
939 _transport_set_identify(ioc, mpt2sas_phy->attached_handle,
940 &mpt2sas_phy->remote_identify);
941 phy->identify.phy_identifier = mpt2sas_phy->phy_id;
942 phy->negotiated_linkrate = _transport_convert_phy_link_rate(
943 expander_pg1.NegotiatedLinkRate &
944 MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
945 phy->minimum_linkrate_hw = _transport_convert_phy_link_rate(
946 expander_pg1.HwLinkRate & MPI2_SAS_HWRATE_MIN_RATE_MASK);
947 phy->maximum_linkrate_hw = _transport_convert_phy_link_rate(
948 expander_pg1.HwLinkRate >> 4);
949 phy->minimum_linkrate = _transport_convert_phy_link_rate(
950 expander_pg1.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
951 phy->maximum_linkrate = _transport_convert_phy_link_rate(
952 expander_pg1.ProgrammedLinkRate >> 4);
954 if ((sas_phy_add(phy))) {
955 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
956 ioc->name, __FILE__, __LINE__, __func__);
957 sas_phy_free(phy);
958 return -1;
960 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
961 dev_printk(KERN_INFO, &phy->dev,
962 "add: handle(0x%04x), sas_addr(0x%016llx)\n"
963 "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
964 mpt2sas_phy->handle, (unsigned long long)
965 mpt2sas_phy->identify.sas_address,
966 mpt2sas_phy->attached_handle,
967 (unsigned long long)
968 mpt2sas_phy->remote_identify.sas_address);
969 mpt2sas_phy->phy = phy;
970 return 0;
974 * mpt2sas_transport_update_links - refreshing phy link changes
975 * @ioc: per adapter object
976 * @sas_address: sas address of parent expander or sas host
977 * @handle: attached device handle
978 * @phy_numberv: phy number
979 * @link_rate: new link rate
981 * Returns nothing.
983 void
984 mpt2sas_transport_update_links(struct MPT2SAS_ADAPTER *ioc,
985 u64 sas_address, u16 handle, u8 phy_number, u8 link_rate)
987 unsigned long flags;
988 struct _sas_node *sas_node;
989 struct _sas_phy *mpt2sas_phy;
991 if (ioc->shost_recovery || ioc->pci_error_recovery)
992 return;
994 spin_lock_irqsave(&ioc->sas_node_lock, flags);
995 sas_node = _transport_sas_node_find_by_sas_address(ioc, sas_address);
996 if (!sas_node) {
997 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
998 return;
1001 mpt2sas_phy = &sas_node->phy[phy_number];
1002 mpt2sas_phy->attached_handle = handle;
1003 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1004 if (handle && (link_rate >= MPI2_SAS_NEG_LINK_RATE_1_5)) {
1005 _transport_set_identify(ioc, handle,
1006 &mpt2sas_phy->remote_identify);
1007 _transport_add_phy_to_an_existing_port(ioc, sas_node,
1008 mpt2sas_phy, mpt2sas_phy->remote_identify.sas_address);
1009 } else {
1010 memset(&mpt2sas_phy->remote_identify, 0 , sizeof(struct
1011 sas_identify));
1012 _transport_del_phy_from_an_existing_port(ioc, sas_node,
1013 mpt2sas_phy);
1016 if (mpt2sas_phy->phy)
1017 mpt2sas_phy->phy->negotiated_linkrate =
1018 _transport_convert_phy_link_rate(link_rate);
1020 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
1021 dev_printk(KERN_INFO, &mpt2sas_phy->phy->dev,
1022 "refresh: parent sas_addr(0x%016llx),\n"
1023 "\tlink_rate(0x%02x), phy(%d)\n"
1024 "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
1025 (unsigned long long)sas_address,
1026 link_rate, phy_number, handle, (unsigned long long)
1027 mpt2sas_phy->remote_identify.sas_address);
1030 static inline void *
1031 phy_to_ioc(struct sas_phy *phy)
1033 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
1034 return shost_priv(shost);
1037 static inline void *
1038 rphy_to_ioc(struct sas_rphy *rphy)
1040 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
1041 return shost_priv(shost);
1045 /* report phy error log structure */
1046 struct phy_error_log_request{
1047 u8 smp_frame_type; /* 0x40 */
1048 u8 function; /* 0x11 */
1049 u8 allocated_response_length;
1050 u8 request_length; /* 02 */
1051 u8 reserved_1[5];
1052 u8 phy_identifier;
1053 u8 reserved_2[2];
1056 /* report phy error log reply structure */
1057 struct phy_error_log_reply{
1058 u8 smp_frame_type; /* 0x41 */
1059 u8 function; /* 0x11 */
1060 u8 function_result;
1061 u8 response_length;
1062 __be16 expander_change_count;
1063 u8 reserved_1[3];
1064 u8 phy_identifier;
1065 u8 reserved_2[2];
1066 __be32 invalid_dword;
1067 __be32 running_disparity_error;
1068 __be32 loss_of_dword_sync;
1069 __be32 phy_reset_problem;
1073 * _transport_get_expander_phy_error_log - return expander counters
1074 * @ioc: per adapter object
1075 * @phy: The sas phy object
1077 * Returns 0 for success, non-zero for failure.
1080 static int
1081 _transport_get_expander_phy_error_log(struct MPT2SAS_ADAPTER *ioc,
1082 struct sas_phy *phy)
1084 Mpi2SmpPassthroughRequest_t *mpi_request;
1085 Mpi2SmpPassthroughReply_t *mpi_reply;
1086 struct phy_error_log_request *phy_error_log_request;
1087 struct phy_error_log_reply *phy_error_log_reply;
1088 int rc;
1089 u16 smid;
1090 u32 ioc_state;
1091 unsigned long timeleft;
1092 void *psge;
1093 u32 sgl_flags;
1094 u8 issue_reset = 0;
1095 void *data_out = NULL;
1096 dma_addr_t data_out_dma;
1097 u32 sz;
1098 u16 wait_state_count;
1100 if (ioc->shost_recovery || ioc->pci_error_recovery) {
1101 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
1102 __func__, ioc->name);
1103 return -EFAULT;
1106 mutex_lock(&ioc->transport_cmds.mutex);
1108 if (ioc->transport_cmds.status != MPT2_CMD_NOT_USED) {
1109 printk(MPT2SAS_ERR_FMT "%s: transport_cmds in use\n",
1110 ioc->name, __func__);
1111 rc = -EAGAIN;
1112 goto out;
1114 ioc->transport_cmds.status = MPT2_CMD_PENDING;
1116 wait_state_count = 0;
1117 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
1118 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1119 if (wait_state_count++ == 10) {
1120 printk(MPT2SAS_ERR_FMT
1121 "%s: failed due to ioc not operational\n",
1122 ioc->name, __func__);
1123 rc = -EFAULT;
1124 goto out;
1126 ssleep(1);
1127 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
1128 printk(MPT2SAS_INFO_FMT "%s: waiting for "
1129 "operational state(count=%d)\n", ioc->name,
1130 __func__, wait_state_count);
1132 if (wait_state_count)
1133 printk(MPT2SAS_INFO_FMT "%s: ioc is operational\n",
1134 ioc->name, __func__);
1136 smid = mpt2sas_base_get_smid(ioc, ioc->transport_cb_idx);
1137 if (!smid) {
1138 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1139 ioc->name, __func__);
1140 rc = -EAGAIN;
1141 goto out;
1144 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1145 ioc->transport_cmds.smid = smid;
1147 sz = sizeof(struct phy_error_log_request) +
1148 sizeof(struct phy_error_log_reply);
1149 data_out = pci_alloc_consistent(ioc->pdev, sz, &data_out_dma);
1150 if (!data_out) {
1151 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
1152 __LINE__, __func__);
1153 rc = -ENOMEM;
1154 mpt2sas_base_free_smid(ioc, smid);
1155 goto out;
1158 rc = -EINVAL;
1159 memset(data_out, 0, sz);
1160 phy_error_log_request = data_out;
1161 phy_error_log_request->smp_frame_type = 0x40;
1162 phy_error_log_request->function = 0x11;
1163 phy_error_log_request->request_length = 2;
1164 phy_error_log_request->allocated_response_length = 0;
1165 phy_error_log_request->phy_identifier = phy->number;
1167 memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
1168 mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
1169 mpi_request->PhysicalPort = 0xFF;
1170 mpi_request->VF_ID = 0; /* TODO */
1171 mpi_request->VP_ID = 0;
1172 mpi_request->SASAddress = cpu_to_le64(phy->identify.sas_address);
1173 mpi_request->RequestDataLength =
1174 cpu_to_le16(sizeof(struct phy_error_log_request));
1175 psge = &mpi_request->SGL;
1177 /* WRITE sgel first */
1178 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1179 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
1180 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1181 ioc->base_add_sg_single(psge, sgl_flags |
1182 sizeof(struct phy_error_log_request), data_out_dma);
1184 /* incr sgel */
1185 psge += ioc->sge_size;
1187 /* READ sgel last */
1188 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1189 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
1190 MPI2_SGE_FLAGS_END_OF_LIST);
1191 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1192 ioc->base_add_sg_single(psge, sgl_flags |
1193 sizeof(struct phy_error_log_reply), data_out_dma +
1194 sizeof(struct phy_error_log_request));
1196 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "phy_error_log - "
1197 "send to sas_addr(0x%016llx), phy(%d)\n", ioc->name,
1198 (unsigned long long)phy->identify.sas_address, phy->number));
1199 init_completion(&ioc->transport_cmds.done);
1200 mpt2sas_base_put_smid_default(ioc, smid);
1201 timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done,
1202 10*HZ);
1204 if (!(ioc->transport_cmds.status & MPT2_CMD_COMPLETE)) {
1205 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
1206 ioc->name, __func__);
1207 _debug_dump_mf(mpi_request,
1208 sizeof(Mpi2SmpPassthroughRequest_t)/4);
1209 if (!(ioc->transport_cmds.status & MPT2_CMD_RESET))
1210 issue_reset = 1;
1211 goto issue_host_reset;
1214 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "phy_error_log - "
1215 "complete\n", ioc->name));
1217 if (ioc->transport_cmds.status & MPT2_CMD_REPLY_VALID) {
1219 mpi_reply = ioc->transport_cmds.reply;
1221 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
1222 "phy_error_log - reply data transfer size(%d)\n",
1223 ioc->name, le16_to_cpu(mpi_reply->ResponseDataLength)));
1225 if (le16_to_cpu(mpi_reply->ResponseDataLength) !=
1226 sizeof(struct phy_error_log_reply))
1227 goto out;
1229 phy_error_log_reply = data_out +
1230 sizeof(struct phy_error_log_request);
1232 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
1233 "phy_error_log - function_result(%d)\n",
1234 ioc->name, phy_error_log_reply->function_result));
1236 phy->invalid_dword_count =
1237 be32_to_cpu(phy_error_log_reply->invalid_dword);
1238 phy->running_disparity_error_count =
1239 be32_to_cpu(phy_error_log_reply->running_disparity_error);
1240 phy->loss_of_dword_sync_count =
1241 be32_to_cpu(phy_error_log_reply->loss_of_dword_sync);
1242 phy->phy_reset_problem_count =
1243 be32_to_cpu(phy_error_log_reply->phy_reset_problem);
1244 rc = 0;
1245 } else
1246 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
1247 "phy_error_log - no reply\n", ioc->name));
1249 issue_host_reset:
1250 if (issue_reset)
1251 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1252 FORCE_BIG_HAMMER);
1253 out:
1254 ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
1255 if (data_out)
1256 pci_free_consistent(ioc->pdev, sz, data_out, data_out_dma);
1258 mutex_unlock(&ioc->transport_cmds.mutex);
1259 return rc;
1263 * _transport_get_linkerrors - return phy counters for both hba and expanders
1264 * @phy: The sas phy object
1266 * Returns 0 for success, non-zero for failure.
1269 static int
1270 _transport_get_linkerrors(struct sas_phy *phy)
1272 struct MPT2SAS_ADAPTER *ioc = phy_to_ioc(phy);
1273 unsigned long flags;
1274 Mpi2ConfigReply_t mpi_reply;
1275 Mpi2SasPhyPage1_t phy_pg1;
1277 spin_lock_irqsave(&ioc->sas_node_lock, flags);
1278 if (_transport_sas_node_find_by_sas_address(ioc,
1279 phy->identify.sas_address) == NULL) {
1280 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1281 return -EINVAL;
1283 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1285 if (phy->identify.sas_address != ioc->sas_hba.sas_address)
1286 return _transport_get_expander_phy_error_log(ioc, phy);
1288 /* get hba phy error logs */
1289 if ((mpt2sas_config_get_phy_pg1(ioc, &mpi_reply, &phy_pg1,
1290 phy->number))) {
1291 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1292 ioc->name, __FILE__, __LINE__, __func__);
1293 return -ENXIO;
1296 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo)
1297 printk(MPT2SAS_INFO_FMT "phy(%d), ioc_status"
1298 "(0x%04x), loginfo(0x%08x)\n", ioc->name,
1299 phy->number, le16_to_cpu(mpi_reply.IOCStatus),
1300 le32_to_cpu(mpi_reply.IOCLogInfo));
1302 phy->invalid_dword_count = le32_to_cpu(phy_pg1.InvalidDwordCount);
1303 phy->running_disparity_error_count =
1304 le32_to_cpu(phy_pg1.RunningDisparityErrorCount);
1305 phy->loss_of_dword_sync_count =
1306 le32_to_cpu(phy_pg1.LossDwordSynchCount);
1307 phy->phy_reset_problem_count =
1308 le32_to_cpu(phy_pg1.PhyResetProblemCount);
1309 return 0;
1313 * _transport_get_enclosure_identifier -
1314 * @phy: The sas phy object
1316 * Obtain the enclosure logical id for an expander.
1317 * Returns 0 for success, non-zero for failure.
1319 static int
1320 _transport_get_enclosure_identifier(struct sas_rphy *rphy, u64 *identifier)
1322 struct MPT2SAS_ADAPTER *ioc = rphy_to_ioc(rphy);
1323 struct _sas_device *sas_device;
1324 unsigned long flags;
1325 int rc;
1327 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1328 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1329 rphy->identify.sas_address);
1330 if (sas_device) {
1331 *identifier = sas_device->enclosure_logical_id;
1332 rc = 0;
1333 } else {
1334 *identifier = 0;
1335 rc = -ENXIO;
1337 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1338 return rc;
1342 * _transport_get_bay_identifier -
1343 * @phy: The sas phy object
1345 * Returns the slot id for a device that resides inside an enclosure.
1347 static int
1348 _transport_get_bay_identifier(struct sas_rphy *rphy)
1350 struct MPT2SAS_ADAPTER *ioc = rphy_to_ioc(rphy);
1351 struct _sas_device *sas_device;
1352 unsigned long flags;
1353 int rc;
1355 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1356 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1357 rphy->identify.sas_address);
1358 if (sas_device)
1359 rc = sas_device->slot;
1360 else
1361 rc = -ENXIO;
1362 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1363 return rc;
1366 /* phy control request structure */
1367 struct phy_control_request{
1368 u8 smp_frame_type; /* 0x40 */
1369 u8 function; /* 0x91 */
1370 u8 allocated_response_length;
1371 u8 request_length; /* 0x09 */
1372 u16 expander_change_count;
1373 u8 reserved_1[3];
1374 u8 phy_identifier;
1375 u8 phy_operation;
1376 u8 reserved_2[13];
1377 u64 attached_device_name;
1378 u8 programmed_min_physical_link_rate;
1379 u8 programmed_max_physical_link_rate;
1380 u8 reserved_3[6];
1383 /* phy control reply structure */
1384 struct phy_control_reply{
1385 u8 smp_frame_type; /* 0x41 */
1386 u8 function; /* 0x11 */
1387 u8 function_result;
1388 u8 response_length;
1391 #define SMP_PHY_CONTROL_LINK_RESET (0x01)
1392 #define SMP_PHY_CONTROL_HARD_RESET (0x02)
1393 #define SMP_PHY_CONTROL_DISABLE (0x03)
1396 * _transport_expander_phy_control - expander phy control
1397 * @ioc: per adapter object
1398 * @phy: The sas phy object
1400 * Returns 0 for success, non-zero for failure.
1403 static int
1404 _transport_expander_phy_control(struct MPT2SAS_ADAPTER *ioc,
1405 struct sas_phy *phy, u8 phy_operation)
1407 Mpi2SmpPassthroughRequest_t *mpi_request;
1408 Mpi2SmpPassthroughReply_t *mpi_reply;
1409 struct phy_control_request *phy_control_request;
1410 struct phy_control_reply *phy_control_reply;
1411 int rc;
1412 u16 smid;
1413 u32 ioc_state;
1414 unsigned long timeleft;
1415 void *psge;
1416 u32 sgl_flags;
1417 u8 issue_reset = 0;
1418 void *data_out = NULL;
1419 dma_addr_t data_out_dma;
1420 u32 sz;
1421 u16 wait_state_count;
1423 if (ioc->shost_recovery) {
1424 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
1425 __func__, ioc->name);
1426 return -EFAULT;
1429 mutex_lock(&ioc->transport_cmds.mutex);
1431 if (ioc->transport_cmds.status != MPT2_CMD_NOT_USED) {
1432 printk(MPT2SAS_ERR_FMT "%s: transport_cmds in use\n",
1433 ioc->name, __func__);
1434 rc = -EAGAIN;
1435 goto out;
1437 ioc->transport_cmds.status = MPT2_CMD_PENDING;
1439 wait_state_count = 0;
1440 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
1441 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1442 if (wait_state_count++ == 10) {
1443 printk(MPT2SAS_ERR_FMT
1444 "%s: failed due to ioc not operational\n",
1445 ioc->name, __func__);
1446 rc = -EFAULT;
1447 goto out;
1449 ssleep(1);
1450 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
1451 printk(MPT2SAS_INFO_FMT "%s: waiting for "
1452 "operational state(count=%d)\n", ioc->name,
1453 __func__, wait_state_count);
1455 if (wait_state_count)
1456 printk(MPT2SAS_INFO_FMT "%s: ioc is operational\n",
1457 ioc->name, __func__);
1459 smid = mpt2sas_base_get_smid(ioc, ioc->transport_cb_idx);
1460 if (!smid) {
1461 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1462 ioc->name, __func__);
1463 rc = -EAGAIN;
1464 goto out;
1467 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1468 ioc->transport_cmds.smid = smid;
1470 sz = sizeof(struct phy_control_request) +
1471 sizeof(struct phy_control_reply);
1472 data_out = pci_alloc_consistent(ioc->pdev, sz, &data_out_dma);
1473 if (!data_out) {
1474 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
1475 __LINE__, __func__);
1476 rc = -ENOMEM;
1477 mpt2sas_base_free_smid(ioc, smid);
1478 goto out;
1481 rc = -EINVAL;
1482 memset(data_out, 0, sz);
1483 phy_control_request = data_out;
1484 phy_control_request->smp_frame_type = 0x40;
1485 phy_control_request->function = 0x91;
1486 phy_control_request->request_length = 9;
1487 phy_control_request->allocated_response_length = 0;
1488 phy_control_request->phy_identifier = phy->number;
1489 phy_control_request->phy_operation = phy_operation;
1490 phy_control_request->programmed_min_physical_link_rate =
1491 phy->minimum_linkrate << 4;
1492 phy_control_request->programmed_max_physical_link_rate =
1493 phy->maximum_linkrate << 4;
1495 memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
1496 mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
1497 mpi_request->PhysicalPort = 0xFF;
1498 mpi_request->VF_ID = 0; /* TODO */
1499 mpi_request->VP_ID = 0;
1500 mpi_request->SASAddress = cpu_to_le64(phy->identify.sas_address);
1501 mpi_request->RequestDataLength =
1502 cpu_to_le16(sizeof(struct phy_error_log_request));
1503 psge = &mpi_request->SGL;
1505 /* WRITE sgel first */
1506 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1507 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
1508 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1509 ioc->base_add_sg_single(psge, sgl_flags |
1510 sizeof(struct phy_control_request), data_out_dma);
1512 /* incr sgel */
1513 psge += ioc->sge_size;
1515 /* READ sgel last */
1516 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1517 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
1518 MPI2_SGE_FLAGS_END_OF_LIST);
1519 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1520 ioc->base_add_sg_single(psge, sgl_flags |
1521 sizeof(struct phy_control_reply), data_out_dma +
1522 sizeof(struct phy_control_request));
1524 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "phy_control - "
1525 "send to sas_addr(0x%016llx), phy(%d), opcode(%d)\n", ioc->name,
1526 (unsigned long long)phy->identify.sas_address, phy->number,
1527 phy_operation));
1529 init_completion(&ioc->transport_cmds.done);
1530 mpt2sas_base_put_smid_default(ioc, smid);
1531 timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done,
1532 10*HZ);
1534 if (!(ioc->transport_cmds.status & MPT2_CMD_COMPLETE)) {
1535 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
1536 ioc->name, __func__);
1537 _debug_dump_mf(mpi_request,
1538 sizeof(Mpi2SmpPassthroughRequest_t)/4);
1539 if (!(ioc->transport_cmds.status & MPT2_CMD_RESET))
1540 issue_reset = 1;
1541 goto issue_host_reset;
1544 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "phy_control - "
1545 "complete\n", ioc->name));
1547 if (ioc->transport_cmds.status & MPT2_CMD_REPLY_VALID) {
1549 mpi_reply = ioc->transport_cmds.reply;
1551 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
1552 "phy_control - reply data transfer size(%d)\n",
1553 ioc->name, le16_to_cpu(mpi_reply->ResponseDataLength)));
1555 if (le16_to_cpu(mpi_reply->ResponseDataLength) !=
1556 sizeof(struct phy_control_reply))
1557 goto out;
1559 phy_control_reply = data_out +
1560 sizeof(struct phy_control_request);
1562 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
1563 "phy_control - function_result(%d)\n",
1564 ioc->name, phy_control_reply->function_result));
1566 rc = 0;
1567 } else
1568 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
1569 "phy_control - no reply\n", ioc->name));
1571 issue_host_reset:
1572 if (issue_reset)
1573 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1574 FORCE_BIG_HAMMER);
1575 out:
1576 ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
1577 if (data_out)
1578 pci_free_consistent(ioc->pdev, sz, data_out, data_out_dma);
1580 mutex_unlock(&ioc->transport_cmds.mutex);
1581 return rc;
1585 * _transport_phy_reset -
1586 * @phy: The sas phy object
1587 * @hard_reset:
1589 * Returns 0 for success, non-zero for failure.
1591 static int
1592 _transport_phy_reset(struct sas_phy *phy, int hard_reset)
1594 struct MPT2SAS_ADAPTER *ioc = phy_to_ioc(phy);
1595 Mpi2SasIoUnitControlReply_t mpi_reply;
1596 Mpi2SasIoUnitControlRequest_t mpi_request;
1597 unsigned long flags;
1599 spin_lock_irqsave(&ioc->sas_node_lock, flags);
1600 if (_transport_sas_node_find_by_sas_address(ioc,
1601 phy->identify.sas_address) == NULL) {
1602 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1603 return -EINVAL;
1605 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1607 /* handle expander phys */
1608 if (phy->identify.sas_address != ioc->sas_hba.sas_address)
1609 return _transport_expander_phy_control(ioc, phy,
1610 (hard_reset == 1) ? SMP_PHY_CONTROL_HARD_RESET :
1611 SMP_PHY_CONTROL_LINK_RESET);
1613 /* handle hba phys */
1614 memset(&mpi_request, 0, sizeof(Mpi2SasIoUnitControlReply_t));
1615 mpi_request.Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
1616 mpi_request.Operation = hard_reset ?
1617 MPI2_SAS_OP_PHY_HARD_RESET : MPI2_SAS_OP_PHY_LINK_RESET;
1618 mpi_request.PhyNum = phy->number;
1620 if ((mpt2sas_base_sas_iounit_control(ioc, &mpi_reply, &mpi_request))) {
1621 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1622 ioc->name, __FILE__, __LINE__, __func__);
1623 return -ENXIO;
1626 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo)
1627 printk(MPT2SAS_INFO_FMT "phy(%d), ioc_status"
1628 "(0x%04x), loginfo(0x%08x)\n", ioc->name,
1629 phy->number, le16_to_cpu(mpi_reply.IOCStatus),
1630 le32_to_cpu(mpi_reply.IOCLogInfo));
1632 return 0;
1636 * _transport_phy_enable - enable/disable phys
1637 * @phy: The sas phy object
1638 * @enable: enable phy when true
1640 * Only support sas_host direct attached phys.
1641 * Returns 0 for success, non-zero for failure.
1643 static int
1644 _transport_phy_enable(struct sas_phy *phy, int enable)
1646 struct MPT2SAS_ADAPTER *ioc = phy_to_ioc(phy);
1647 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
1648 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
1649 Mpi2ConfigReply_t mpi_reply;
1650 u16 ioc_status;
1651 u16 sz;
1652 int rc = 0;
1653 unsigned long flags;
1654 int i, discovery_active;
1656 spin_lock_irqsave(&ioc->sas_node_lock, flags);
1657 if (_transport_sas_node_find_by_sas_address(ioc,
1658 phy->identify.sas_address) == NULL) {
1659 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1660 return -EINVAL;
1662 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1664 /* handle expander phys */
1665 if (phy->identify.sas_address != ioc->sas_hba.sas_address)
1666 return _transport_expander_phy_control(ioc, phy,
1667 (enable == 1) ? SMP_PHY_CONTROL_LINK_RESET :
1668 SMP_PHY_CONTROL_DISABLE);
1670 /* handle hba phys */
1672 /* read sas_iounit page 0 */
1673 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
1674 sizeof(Mpi2SasIOUnit0PhyData_t));
1675 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
1676 if (!sas_iounit_pg0) {
1677 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1678 ioc->name, __FILE__, __LINE__, __func__);
1679 rc = -ENOMEM;
1680 goto out;
1682 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
1683 sas_iounit_pg0, sz))) {
1684 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1685 ioc->name, __FILE__, __LINE__, __func__);
1686 rc = -ENXIO;
1687 goto out;
1689 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1690 MPI2_IOCSTATUS_MASK;
1691 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1692 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1693 ioc->name, __FILE__, __LINE__, __func__);
1694 rc = -EIO;
1695 goto out;
1698 /* unable to enable/disable phys when when discovery is active */
1699 for (i = 0, discovery_active = 0; i < ioc->sas_hba.num_phys ; i++) {
1700 if (sas_iounit_pg0->PhyData[i].PortFlags &
1701 MPI2_SASIOUNIT0_PORTFLAGS_DISCOVERY_IN_PROGRESS) {
1702 printk(MPT2SAS_ERR_FMT "discovery is active on "
1703 "port = %d, phy = %d: unable to enable/disable "
1704 "phys, try again later!\n", ioc->name,
1705 sas_iounit_pg0->PhyData[i].Port, i);
1706 discovery_active = 1;
1710 if (discovery_active) {
1711 rc = -EAGAIN;
1712 goto out;
1715 /* read sas_iounit page 1 */
1716 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
1717 sizeof(Mpi2SasIOUnit1PhyData_t));
1718 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
1719 if (!sas_iounit_pg1) {
1720 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1721 ioc->name, __FILE__, __LINE__, __func__);
1722 rc = -ENOMEM;
1723 goto out;
1725 if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
1726 sas_iounit_pg1, sz))) {
1727 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1728 ioc->name, __FILE__, __LINE__, __func__);
1729 rc = -ENXIO;
1730 goto out;
1732 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1733 MPI2_IOCSTATUS_MASK;
1734 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1735 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1736 ioc->name, __FILE__, __LINE__, __func__);
1737 rc = -EIO;
1738 goto out;
1740 /* copy Port/PortFlags/PhyFlags from page 0 */
1741 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
1742 sas_iounit_pg1->PhyData[i].Port =
1743 sas_iounit_pg0->PhyData[i].Port;
1744 sas_iounit_pg1->PhyData[i].PortFlags =
1745 (sas_iounit_pg0->PhyData[i].PortFlags &
1746 MPI2_SASIOUNIT0_PORTFLAGS_AUTO_PORT_CONFIG);
1747 sas_iounit_pg1->PhyData[i].PhyFlags =
1748 (sas_iounit_pg0->PhyData[i].PhyFlags &
1749 (MPI2_SASIOUNIT0_PHYFLAGS_ZONING_ENABLED +
1750 MPI2_SASIOUNIT0_PHYFLAGS_PHY_DISABLED));
1752 if (enable)
1753 sas_iounit_pg1->PhyData[phy->number].PhyFlags
1754 &= ~MPI2_SASIOUNIT1_PHYFLAGS_PHY_DISABLE;
1755 else
1756 sas_iounit_pg1->PhyData[phy->number].PhyFlags
1757 |= MPI2_SASIOUNIT1_PHYFLAGS_PHY_DISABLE;
1759 mpt2sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1, sz);
1761 /* link reset */
1762 if (enable)
1763 _transport_phy_reset(phy, 0);
1765 out:
1766 kfree(sas_iounit_pg1);
1767 kfree(sas_iounit_pg0);
1768 return rc;
1772 * _transport_phy_speed - set phy min/max link rates
1773 * @phy: The sas phy object
1774 * @rates: rates defined in sas_phy_linkrates
1776 * Only support sas_host direct attached phys.
1777 * Returns 0 for success, non-zero for failure.
1779 static int
1780 _transport_phy_speed(struct sas_phy *phy, struct sas_phy_linkrates *rates)
1782 struct MPT2SAS_ADAPTER *ioc = phy_to_ioc(phy);
1783 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
1784 Mpi2SasPhyPage0_t phy_pg0;
1785 Mpi2ConfigReply_t mpi_reply;
1786 u16 ioc_status;
1787 u16 sz;
1788 int i;
1789 int rc = 0;
1790 unsigned long flags;
1792 spin_lock_irqsave(&ioc->sas_node_lock, flags);
1793 if (_transport_sas_node_find_by_sas_address(ioc,
1794 phy->identify.sas_address) == NULL) {
1795 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1796 return -EINVAL;
1798 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1800 if (!rates->minimum_linkrate)
1801 rates->minimum_linkrate = phy->minimum_linkrate;
1802 else if (rates->minimum_linkrate < phy->minimum_linkrate_hw)
1803 rates->minimum_linkrate = phy->minimum_linkrate_hw;
1805 if (!rates->maximum_linkrate)
1806 rates->maximum_linkrate = phy->maximum_linkrate;
1807 else if (rates->maximum_linkrate > phy->maximum_linkrate_hw)
1808 rates->maximum_linkrate = phy->maximum_linkrate_hw;
1810 /* handle expander phys */
1811 if (phy->identify.sas_address != ioc->sas_hba.sas_address) {
1812 phy->minimum_linkrate = rates->minimum_linkrate;
1813 phy->maximum_linkrate = rates->maximum_linkrate;
1814 return _transport_expander_phy_control(ioc, phy,
1815 SMP_PHY_CONTROL_LINK_RESET);
1818 /* handle hba phys */
1820 /* sas_iounit page 1 */
1821 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
1822 sizeof(Mpi2SasIOUnit1PhyData_t));
1823 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
1824 if (!sas_iounit_pg1) {
1825 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1826 ioc->name, __FILE__, __LINE__, __func__);
1827 rc = -ENOMEM;
1828 goto out;
1830 if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
1831 sas_iounit_pg1, sz))) {
1832 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1833 ioc->name, __FILE__, __LINE__, __func__);
1834 rc = -ENXIO;
1835 goto out;
1837 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1838 MPI2_IOCSTATUS_MASK;
1839 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1840 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1841 ioc->name, __FILE__, __LINE__, __func__);
1842 rc = -EIO;
1843 goto out;
1846 for (i = 0; i < ioc->sas_hba.num_phys; i++) {
1847 if (phy->number != i) {
1848 sas_iounit_pg1->PhyData[i].MaxMinLinkRate =
1849 (ioc->sas_hba.phy[i].phy->minimum_linkrate +
1850 (ioc->sas_hba.phy[i].phy->maximum_linkrate << 4));
1851 } else {
1852 sas_iounit_pg1->PhyData[i].MaxMinLinkRate =
1853 (rates->minimum_linkrate +
1854 (rates->maximum_linkrate << 4));
1858 if (mpt2sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1,
1859 sz)) {
1860 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1861 ioc->name, __FILE__, __LINE__, __func__);
1862 rc = -ENXIO;
1863 goto out;
1866 /* link reset */
1867 _transport_phy_reset(phy, 0);
1869 /* read phy page 0, then update the rates in the sas transport phy */
1870 if (!mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
1871 phy->number)) {
1872 phy->minimum_linkrate = _transport_convert_phy_link_rate(
1873 phy_pg0.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
1874 phy->maximum_linkrate = _transport_convert_phy_link_rate(
1875 phy_pg0.ProgrammedLinkRate >> 4);
1876 phy->negotiated_linkrate = _transport_convert_phy_link_rate(
1877 phy_pg0.NegotiatedLinkRate &
1878 MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
1881 out:
1882 kfree(sas_iounit_pg1);
1883 return rc;
1888 * _transport_smp_handler - transport portal for smp passthru
1889 * @shost: shost object
1890 * @rphy: sas transport rphy object
1891 * @req:
1893 * This used primarily for smp_utils.
1894 * Example:
1895 * smp_rep_general /sys/class/bsg/expander-5:0
1897 static int
1898 _transport_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
1899 struct request *req)
1901 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1902 Mpi2SmpPassthroughRequest_t *mpi_request;
1903 Mpi2SmpPassthroughReply_t *mpi_reply;
1904 int rc;
1905 u16 smid;
1906 u32 ioc_state;
1907 unsigned long timeleft;
1908 void *psge;
1909 u32 sgl_flags;
1910 u8 issue_reset = 0;
1911 dma_addr_t dma_addr_in = 0;
1912 dma_addr_t dma_addr_out = 0;
1913 dma_addr_t pci_dma_in = 0;
1914 dma_addr_t pci_dma_out = 0;
1915 void *pci_addr_in = NULL;
1916 void *pci_addr_out = NULL;
1917 u16 wait_state_count;
1918 struct request *rsp = req->next_rq;
1919 struct bio_vec bvec;
1920 struct bvec_iter iter;
1922 if (!rsp) {
1923 printk(MPT2SAS_ERR_FMT "%s: the smp response space is "
1924 "missing\n", ioc->name, __func__);
1925 return -EINVAL;
1927 if (ioc->shost_recovery || ioc->pci_error_recovery) {
1928 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
1929 __func__, ioc->name);
1930 return -EFAULT;
1933 rc = mutex_lock_interruptible(&ioc->transport_cmds.mutex);
1934 if (rc)
1935 return rc;
1937 if (ioc->transport_cmds.status != MPT2_CMD_NOT_USED) {
1938 printk(MPT2SAS_ERR_FMT "%s: transport_cmds in use\n", ioc->name,
1939 __func__);
1940 rc = -EAGAIN;
1941 goto out;
1943 ioc->transport_cmds.status = MPT2_CMD_PENDING;
1945 /* Check if the request is split across multiple segments */
1946 if (bio_multiple_segments(req->bio)) {
1947 u32 offset = 0;
1949 /* Allocate memory and copy the request */
1950 pci_addr_out = pci_alloc_consistent(ioc->pdev,
1951 blk_rq_bytes(req), &pci_dma_out);
1952 if (!pci_addr_out) {
1953 printk(MPT2SAS_INFO_FMT "%s(): PCI Addr out = NULL\n",
1954 ioc->name, __func__);
1955 rc = -ENOMEM;
1956 goto out;
1959 bio_for_each_segment(bvec, req->bio, iter) {
1960 memcpy(pci_addr_out + offset,
1961 page_address(bvec.bv_page) + bvec.bv_offset,
1962 bvec.bv_len);
1963 offset += bvec.bv_len;
1965 } else {
1966 dma_addr_out = pci_map_single(ioc->pdev, bio_data(req->bio),
1967 blk_rq_bytes(req), PCI_DMA_BIDIRECTIONAL);
1968 if (!dma_addr_out) {
1969 printk(MPT2SAS_INFO_FMT "%s(): DMA Addr out = NULL\n",
1970 ioc->name, __func__);
1971 rc = -ENOMEM;
1972 goto free_pci;
1976 /* Check if the response needs to be populated across
1977 * multiple segments */
1978 if (bio_multiple_segments(rsp->bio)) {
1979 pci_addr_in = pci_alloc_consistent(ioc->pdev, blk_rq_bytes(rsp),
1980 &pci_dma_in);
1981 if (!pci_addr_in) {
1982 printk(MPT2SAS_INFO_FMT "%s(): PCI Addr in = NULL\n",
1983 ioc->name, __func__);
1984 rc = -ENOMEM;
1985 goto unmap;
1987 } else {
1988 dma_addr_in = pci_map_single(ioc->pdev, bio_data(rsp->bio),
1989 blk_rq_bytes(rsp), PCI_DMA_BIDIRECTIONAL);
1990 if (!dma_addr_in) {
1991 printk(MPT2SAS_INFO_FMT "%s(): DMA Addr in = NULL\n",
1992 ioc->name, __func__);
1993 rc = -ENOMEM;
1994 goto unmap;
1998 wait_state_count = 0;
1999 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
2000 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
2001 if (wait_state_count++ == 10) {
2002 printk(MPT2SAS_ERR_FMT
2003 "%s: failed due to ioc not operational\n",
2004 ioc->name, __func__);
2005 rc = -EFAULT;
2006 goto unmap;
2008 ssleep(1);
2009 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
2010 printk(MPT2SAS_INFO_FMT "%s: waiting for "
2011 "operational state(count=%d)\n", ioc->name,
2012 __func__, wait_state_count);
2014 if (wait_state_count)
2015 printk(MPT2SAS_INFO_FMT "%s: ioc is operational\n",
2016 ioc->name, __func__);
2018 smid = mpt2sas_base_get_smid(ioc, ioc->transport_cb_idx);
2019 if (!smid) {
2020 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2021 ioc->name, __func__);
2022 rc = -EAGAIN;
2023 goto unmap;
2026 rc = 0;
2027 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2028 ioc->transport_cmds.smid = smid;
2030 memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
2031 mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
2032 mpi_request->PhysicalPort = 0xFF;
2033 mpi_request->VF_ID = 0; /* TODO */
2034 mpi_request->VP_ID = 0;
2035 mpi_request->SASAddress = (rphy) ?
2036 cpu_to_le64(rphy->identify.sas_address) :
2037 cpu_to_le64(ioc->sas_hba.sas_address);
2038 mpi_request->RequestDataLength = cpu_to_le16(blk_rq_bytes(req) - 4);
2039 psge = &mpi_request->SGL;
2041 /* WRITE sgel first */
2042 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
2043 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
2044 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
2045 if (bio_multiple_segments(req->bio)) {
2046 ioc->base_add_sg_single(psge, sgl_flags |
2047 (blk_rq_bytes(req) - 4), pci_dma_out);
2048 } else {
2049 ioc->base_add_sg_single(psge, sgl_flags |
2050 (blk_rq_bytes(req) - 4), dma_addr_out);
2053 /* incr sgel */
2054 psge += ioc->sge_size;
2056 /* READ sgel last */
2057 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
2058 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
2059 MPI2_SGE_FLAGS_END_OF_LIST);
2060 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
2061 if (bio_multiple_segments(rsp->bio)) {
2062 ioc->base_add_sg_single(psge, sgl_flags |
2063 (blk_rq_bytes(rsp) + 4), pci_dma_in);
2064 } else {
2065 ioc->base_add_sg_single(psge, sgl_flags |
2066 (blk_rq_bytes(rsp) + 4), dma_addr_in);
2069 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "%s - "
2070 "sending smp request\n", ioc->name, __func__));
2072 init_completion(&ioc->transport_cmds.done);
2073 mpt2sas_base_put_smid_default(ioc, smid);
2074 timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done,
2075 10*HZ);
2077 if (!(ioc->transport_cmds.status & MPT2_CMD_COMPLETE)) {
2078 printk(MPT2SAS_ERR_FMT "%s : timeout\n",
2079 __func__, ioc->name);
2080 _debug_dump_mf(mpi_request,
2081 sizeof(Mpi2SmpPassthroughRequest_t)/4);
2082 if (!(ioc->transport_cmds.status & MPT2_CMD_RESET))
2083 issue_reset = 1;
2084 goto issue_host_reset;
2087 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "%s - "
2088 "complete\n", ioc->name, __func__));
2090 if (ioc->transport_cmds.status & MPT2_CMD_REPLY_VALID) {
2092 mpi_reply = ioc->transport_cmds.reply;
2094 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
2095 "%s - reply data transfer size(%d)\n",
2096 ioc->name, __func__,
2097 le16_to_cpu(mpi_reply->ResponseDataLength)));
2099 memcpy(req->sense, mpi_reply, sizeof(*mpi_reply));
2100 req->sense_len = sizeof(*mpi_reply);
2101 req->resid_len = 0;
2102 rsp->resid_len -=
2103 le16_to_cpu(mpi_reply->ResponseDataLength);
2104 /* check if the resp needs to be copied from the allocated
2105 * pci mem */
2106 if (bio_multiple_segments(rsp->bio)) {
2107 u32 offset = 0;
2108 u32 bytes_to_copy =
2109 le16_to_cpu(mpi_reply->ResponseDataLength);
2110 bio_for_each_segment(bvec, rsp->bio, iter) {
2111 if (bytes_to_copy <= bvec.bv_len) {
2112 memcpy(page_address(bvec.bv_page) +
2113 bvec.bv_offset, pci_addr_in +
2114 offset, bytes_to_copy);
2115 break;
2116 } else {
2117 memcpy(page_address(bvec.bv_page) +
2118 bvec.bv_offset, pci_addr_in +
2119 offset, bvec.bv_len);
2120 bytes_to_copy -= bvec.bv_len;
2122 offset += bvec.bv_len;
2125 } else {
2126 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
2127 "%s - no reply\n", ioc->name, __func__));
2128 rc = -ENXIO;
2131 issue_host_reset:
2132 if (issue_reset) {
2133 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2134 FORCE_BIG_HAMMER);
2135 rc = -ETIMEDOUT;
2138 unmap:
2139 if (dma_addr_out)
2140 pci_unmap_single(ioc->pdev, dma_addr_out, blk_rq_bytes(req),
2141 PCI_DMA_BIDIRECTIONAL);
2142 if (dma_addr_in)
2143 pci_unmap_single(ioc->pdev, dma_addr_in, blk_rq_bytes(rsp),
2144 PCI_DMA_BIDIRECTIONAL);
2146 free_pci:
2147 if (pci_addr_out)
2148 pci_free_consistent(ioc->pdev, blk_rq_bytes(req), pci_addr_out,
2149 pci_dma_out);
2151 if (pci_addr_in)
2152 pci_free_consistent(ioc->pdev, blk_rq_bytes(rsp), pci_addr_in,
2153 pci_dma_in);
2155 out:
2156 ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
2157 mutex_unlock(&ioc->transport_cmds.mutex);
2158 return rc;
2161 struct sas_function_template mpt2sas_transport_functions = {
2162 .get_linkerrors = _transport_get_linkerrors,
2163 .get_enclosure_identifier = _transport_get_enclosure_identifier,
2164 .get_bay_identifier = _transport_get_bay_identifier,
2165 .phy_reset = _transport_phy_reset,
2166 .phy_enable = _transport_phy_enable,
2167 .set_phy_speed = _transport_phy_speed,
2168 .smp_handler = _transport_smp_handler,
2171 struct scsi_transport_template *mpt2sas_transport_template;