1 /*
2 * SAS Transport Layer for MPT (Message Passing Technology) based controllers
3 *
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)
8 *
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.
13 *
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.
18 *
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.
29
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
38
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.
43 */
44
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>
53
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>
60
61 #include "mpt3sas_base.h"
62
63 /**
64 * _transport_sas_node_find_by_sas_address - sas node search
65 * @ioc: per adapter object
66 * @sas_address: sas address of expander or sas host
67 * Context: Calling function should acquire ioc->sas_node_lock.
68 *
69 * Search for either hba phys or expander device based on handle, then returns
70 * the sas_node object.
71 */
72 static struct _sas_node *
_transport_sas_node_find_by_sas_address(struct MPT3SAS_ADAPTER * ioc,u64 sas_address)73 _transport_sas_node_find_by_sas_address(struct MPT3SAS_ADAPTER *ioc,
74 u64 sas_address)
75 {
76 if (ioc->sas_hba.sas_address == sas_address)
77 return &ioc->sas_hba;
78 else
79 return mpt3sas_scsih_expander_find_by_sas_address(ioc,
80 sas_address);
81 }
82
83 /**
84 * _transport_convert_phy_link_rate -
85 * @link_rate: link rate returned from mpt firmware
86 *
87 * Convert link_rate from mpi fusion into sas_transport form.
88 */
89 static enum sas_linkrate
_transport_convert_phy_link_rate(u8 link_rate)90 _transport_convert_phy_link_rate(u8 link_rate)
91 {
92 enum sas_linkrate rc;
93
94 switch (link_rate) {
95 case MPI2_SAS_NEG_LINK_RATE_1_5:
96 rc = SAS_LINK_RATE_1_5_GBPS;
97 break;
98 case MPI2_SAS_NEG_LINK_RATE_3_0:
99 rc = SAS_LINK_RATE_3_0_GBPS;
100 break;
101 case MPI2_SAS_NEG_LINK_RATE_6_0:
102 rc = SAS_LINK_RATE_6_0_GBPS;
103 break;
104 case MPI25_SAS_NEG_LINK_RATE_12_0:
105 rc = SAS_LINK_RATE_12_0_GBPS;
106 break;
107 case MPI2_SAS_NEG_LINK_RATE_PHY_DISABLED:
108 rc = SAS_PHY_DISABLED;
109 break;
110 case MPI2_SAS_NEG_LINK_RATE_NEGOTIATION_FAILED:
111 rc = SAS_LINK_RATE_FAILED;
112 break;
113 case MPI2_SAS_NEG_LINK_RATE_PORT_SELECTOR:
114 rc = SAS_SATA_PORT_SELECTOR;
115 break;
116 case MPI2_SAS_NEG_LINK_RATE_SMP_RESET_IN_PROGRESS:
117 rc = SAS_PHY_RESET_IN_PROGRESS;
118 break;
119
120 default:
121 case MPI2_SAS_NEG_LINK_RATE_SATA_OOB_COMPLETE:
122 case MPI2_SAS_NEG_LINK_RATE_UNKNOWN_LINK_RATE:
123 rc = SAS_LINK_RATE_UNKNOWN;
124 break;
125 }
126 return rc;
127 }
128
129 /**
130 * _transport_set_identify - set identify for phys and end devices
131 * @ioc: per adapter object
132 * @handle: device handle
133 * @identify: sas identify info
134 *
135 * Populates sas identify info.
136 *
137 * Return: 0 for success, non-zero for failure.
138 */
139 static int
_transport_set_identify(struct MPT3SAS_ADAPTER * ioc,u16 handle,struct sas_identify * identify)140 _transport_set_identify(struct MPT3SAS_ADAPTER *ioc, u16 handle,
141 struct sas_identify *identify)
142 {
143 Mpi2SasDevicePage0_t sas_device_pg0;
144 Mpi2ConfigReply_t mpi_reply;
145 u32 device_info;
146 u32 ioc_status;
147
148 if (ioc->shost_recovery || ioc->pci_error_recovery) {
149 ioc_info(ioc, "%s: host reset in progress!\n", __func__);
150 return -EFAULT;
151 }
152
153 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
154 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
155 ioc_err(ioc, "failure at %s:%d/%s()!\n",
156 __FILE__, __LINE__, __func__);
157 return -ENXIO;
158 }
159
160 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
161 MPI2_IOCSTATUS_MASK;
162 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
163 ioc_err(ioc, "handle(0x%04x), ioc_status(0x%04x) failure at %s:%d/%s()!\n",
164 handle, ioc_status, __FILE__, __LINE__, __func__);
165 return -EIO;
166 }
167
168 memset(identify, 0, sizeof(struct sas_identify));
169 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
170
171 /* sas_address */
172 identify->sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
173
174 /* phy number of the parent device this device is linked to */
175 identify->phy_identifier = sas_device_pg0.PhyNum;
176
177 /* device_type */
178 switch (device_info & MPI2_SAS_DEVICE_INFO_MASK_DEVICE_TYPE) {
179 case MPI2_SAS_DEVICE_INFO_NO_DEVICE:
180 identify->device_type = SAS_PHY_UNUSED;
181 break;
182 case MPI2_SAS_DEVICE_INFO_END_DEVICE:
183 identify->device_type = SAS_END_DEVICE;
184 break;
185 case MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER:
186 identify->device_type = SAS_EDGE_EXPANDER_DEVICE;
187 break;
188 case MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER:
189 identify->device_type = SAS_FANOUT_EXPANDER_DEVICE;
190 break;
191 }
192
193 /* initiator_port_protocols */
194 if (device_info & MPI2_SAS_DEVICE_INFO_SSP_INITIATOR)
195 identify->initiator_port_protocols |= SAS_PROTOCOL_SSP;
196 if (device_info & MPI2_SAS_DEVICE_INFO_STP_INITIATOR)
197 identify->initiator_port_protocols |= SAS_PROTOCOL_STP;
198 if (device_info & MPI2_SAS_DEVICE_INFO_SMP_INITIATOR)
199 identify->initiator_port_protocols |= SAS_PROTOCOL_SMP;
200 if (device_info & MPI2_SAS_DEVICE_INFO_SATA_HOST)
201 identify->initiator_port_protocols |= SAS_PROTOCOL_SATA;
202
203 /* target_port_protocols */
204 if (device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET)
205 identify->target_port_protocols |= SAS_PROTOCOL_SSP;
206 if (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET)
207 identify->target_port_protocols |= SAS_PROTOCOL_STP;
208 if (device_info & MPI2_SAS_DEVICE_INFO_SMP_TARGET)
209 identify->target_port_protocols |= SAS_PROTOCOL_SMP;
210 if (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
211 identify->target_port_protocols |= SAS_PROTOCOL_SATA;
212
213 return 0;
214 }
215
216 /**
217 * mpt3sas_transport_done - internal transport layer callback handler.
218 * @ioc: per adapter object
219 * @smid: system request message index
220 * @msix_index: MSIX table index supplied by the OS
221 * @reply: reply message frame(lower 32bit addr)
222 *
223 * Callback handler when sending internal generated transport cmds.
224 * The callback index passed is `ioc->transport_cb_idx`
225 *
226 * Return: 1 meaning mf should be freed from _base_interrupt
227 * 0 means the mf is freed from this function.
228 */
229 u8
mpt3sas_transport_done(struct MPT3SAS_ADAPTER * ioc,u16 smid,u8 msix_index,u32 reply)230 mpt3sas_transport_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
231 u32 reply)
232 {
233 MPI2DefaultReply_t *mpi_reply;
234
235 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
236 if (ioc->transport_cmds.status == MPT3_CMD_NOT_USED)
237 return 1;
238 if (ioc->transport_cmds.smid != smid)
239 return 1;
240 ioc->transport_cmds.status |= MPT3_CMD_COMPLETE;
241 if (mpi_reply) {
242 memcpy(ioc->transport_cmds.reply, mpi_reply,
243 mpi_reply->MsgLength*4);
244 ioc->transport_cmds.status |= MPT3_CMD_REPLY_VALID;
245 }
246 ioc->transport_cmds.status &= ~MPT3_CMD_PENDING;
247 complete(&ioc->transport_cmds.done);
248 return 1;
249 }
250
251 /* report manufacture request structure */
252 struct rep_manu_request {
253 u8 smp_frame_type;
254 u8 function;
255 u8 reserved;
256 u8 request_length;
257 };
258
259 /* report manufacture reply structure */
260 struct rep_manu_reply {
261 u8 smp_frame_type; /* 0x41 */
262 u8 function; /* 0x01 */
263 u8 function_result;
264 u8 response_length;
265 u16 expander_change_count;
266 u8 reserved0[2];
267 u8 sas_format;
268 u8 reserved2[3];
269 u8 vendor_id[SAS_EXPANDER_VENDOR_ID_LEN];
270 u8 product_id[SAS_EXPANDER_PRODUCT_ID_LEN];
271 u8 product_rev[SAS_EXPANDER_PRODUCT_REV_LEN];
272 u8 component_vendor_id[SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN];
273 u16 component_id;
274 u8 component_revision_id;
275 u8 reserved3;
276 u8 vendor_specific[8];
277 };
278
279 /**
280 * transport_expander_report_manufacture - obtain SMP report_manufacture
281 * @ioc: per adapter object
282 * @sas_address: expander sas address
283 * @edev: the sas_expander_device object
284 *
285 * Fills in the sas_expander_device object when SMP port is created.
286 *
287 * Return: 0 for success, non-zero for failure.
288 */
289 static int
_transport_expander_report_manufacture(struct MPT3SAS_ADAPTER * ioc,u64 sas_address,struct sas_expander_device * edev)290 _transport_expander_report_manufacture(struct MPT3SAS_ADAPTER *ioc,
291 u64 sas_address, struct sas_expander_device *edev)
292 {
293 Mpi2SmpPassthroughRequest_t *mpi_request;
294 Mpi2SmpPassthroughReply_t *mpi_reply;
295 struct rep_manu_reply *manufacture_reply;
296 struct rep_manu_request *manufacture_request;
297 int rc;
298 u16 smid;
299 void *psge;
300 u8 issue_reset = 0;
301 void *data_out = NULL;
302 dma_addr_t data_out_dma;
303 dma_addr_t data_in_dma;
304 size_t data_in_sz;
305 size_t data_out_sz;
306
307 if (ioc->shost_recovery || ioc->pci_error_recovery) {
308 ioc_info(ioc, "%s: host reset in progress!\n", __func__);
309 return -EFAULT;
310 }
311
312 mutex_lock(&ioc->transport_cmds.mutex);
313
314 if (ioc->transport_cmds.status != MPT3_CMD_NOT_USED) {
315 ioc_err(ioc, "%s: transport_cmds in use\n", __func__);
316 rc = -EAGAIN;
317 goto out;
318 }
319 ioc->transport_cmds.status = MPT3_CMD_PENDING;
320
321 rc = mpt3sas_wait_for_ioc(ioc, IOC_OPERATIONAL_WAIT_COUNT);
322 if (rc)
323 goto out;
324
325 smid = mpt3sas_base_get_smid(ioc, ioc->transport_cb_idx);
326 if (!smid) {
327 ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
328 rc = -EAGAIN;
329 goto out;
330 }
331
332 rc = 0;
333 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
334 ioc->transport_cmds.smid = smid;
335
336 data_out_sz = sizeof(struct rep_manu_request);
337 data_in_sz = sizeof(struct rep_manu_reply);
338 data_out = dma_alloc_coherent(&ioc->pdev->dev, data_out_sz + data_in_sz,
339 &data_out_dma, GFP_KERNEL);
340 if (!data_out) {
341 pr_err("failure at %s:%d/%s()!\n", __FILE__,
342 __LINE__, __func__);
343 rc = -ENOMEM;
344 mpt3sas_base_free_smid(ioc, smid);
345 goto out;
346 }
347
348 data_in_dma = data_out_dma + sizeof(struct rep_manu_request);
349
350 manufacture_request = data_out;
351 manufacture_request->smp_frame_type = 0x40;
352 manufacture_request->function = 1;
353 manufacture_request->reserved = 0;
354 manufacture_request->request_length = 0;
355
356 memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
357 mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
358 mpi_request->PhysicalPort = 0xFF;
359 mpi_request->SASAddress = cpu_to_le64(sas_address);
360 mpi_request->RequestDataLength = cpu_to_le16(data_out_sz);
361 psge = &mpi_request->SGL;
362
363 ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
364 data_in_sz);
365
366 dtransportprintk(ioc,
367 ioc_info(ioc, "report_manufacture - send to sas_addr(0x%016llx)\n",
368 (u64)sas_address));
369 init_completion(&ioc->transport_cmds.done);
370 ioc->put_smid_default(ioc, smid);
371 wait_for_completion_timeout(&ioc->transport_cmds.done, 10*HZ);
372
373 if (!(ioc->transport_cmds.status & MPT3_CMD_COMPLETE)) {
374 ioc_err(ioc, "%s: timeout\n", __func__);
375 _debug_dump_mf(mpi_request,
376 sizeof(Mpi2SmpPassthroughRequest_t)/4);
377 if (!(ioc->transport_cmds.status & MPT3_CMD_RESET))
378 issue_reset = 1;
379 goto issue_host_reset;
380 }
381
382 dtransportprintk(ioc, ioc_info(ioc, "report_manufacture - complete\n"));
383
384 if (ioc->transport_cmds.status & MPT3_CMD_REPLY_VALID) {
385 u8 *tmp;
386
387 mpi_reply = ioc->transport_cmds.reply;
388
389 dtransportprintk(ioc,
390 ioc_info(ioc, "report_manufacture - reply data transfer size(%d)\n",
391 le16_to_cpu(mpi_reply->ResponseDataLength)));
392
393 if (le16_to_cpu(mpi_reply->ResponseDataLength) !=
394 sizeof(struct rep_manu_reply))
395 goto out;
396
397 manufacture_reply = data_out + sizeof(struct rep_manu_request);
398 strncpy(edev->vendor_id, manufacture_reply->vendor_id,
399 SAS_EXPANDER_VENDOR_ID_LEN);
400 strncpy(edev->product_id, manufacture_reply->product_id,
401 SAS_EXPANDER_PRODUCT_ID_LEN);
402 strncpy(edev->product_rev, manufacture_reply->product_rev,
403 SAS_EXPANDER_PRODUCT_REV_LEN);
404 edev->level = manufacture_reply->sas_format & 1;
405 if (edev->level) {
406 strncpy(edev->component_vendor_id,
407 manufacture_reply->component_vendor_id,
408 SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN);
409 tmp = (u8 *)&manufacture_reply->component_id;
410 edev->component_id = tmp[0] << 8 | tmp[1];
411 edev->component_revision_id =
412 manufacture_reply->component_revision_id;
413 }
414 } else
415 dtransportprintk(ioc,
416 ioc_info(ioc, "report_manufacture - no reply\n"));
417
418 issue_host_reset:
419 if (issue_reset)
420 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
421 out:
422 ioc->transport_cmds.status = MPT3_CMD_NOT_USED;
423 if (data_out)
424 dma_free_coherent(&ioc->pdev->dev, data_out_sz + data_in_sz,
425 data_out, data_out_dma);
426
427 mutex_unlock(&ioc->transport_cmds.mutex);
428 return rc;
429 }
430
431
432 /**
433 * _transport_delete_port - helper function to removing a port
434 * @ioc: per adapter object
435 * @mpt3sas_port: mpt3sas per port object
436 */
437 static void
_transport_delete_port(struct MPT3SAS_ADAPTER * ioc,struct _sas_port * mpt3sas_port)438 _transport_delete_port(struct MPT3SAS_ADAPTER *ioc,
439 struct _sas_port *mpt3sas_port)
440 {
441 u64 sas_address = mpt3sas_port->remote_identify.sas_address;
442 enum sas_device_type device_type =
443 mpt3sas_port->remote_identify.device_type;
444
445 dev_printk(KERN_INFO, &mpt3sas_port->port->dev,
446 "remove: sas_addr(0x%016llx)\n",
447 (unsigned long long) sas_address);
448
449 ioc->logging_level |= MPT_DEBUG_TRANSPORT;
450 if (device_type == SAS_END_DEVICE)
451 mpt3sas_device_remove_by_sas_address(ioc, sas_address);
452 else if (device_type == SAS_EDGE_EXPANDER_DEVICE ||
453 device_type == SAS_FANOUT_EXPANDER_DEVICE)
454 mpt3sas_expander_remove(ioc, sas_address);
455 ioc->logging_level &= ~MPT_DEBUG_TRANSPORT;
456 }
457
458 /**
459 * _transport_delete_phy - helper function to removing single phy from port
460 * @ioc: per adapter object
461 * @mpt3sas_port: mpt3sas per port object
462 * @mpt3sas_phy: mpt3sas per phy object
463 */
464 static void
_transport_delete_phy(struct MPT3SAS_ADAPTER * ioc,struct _sas_port * mpt3sas_port,struct _sas_phy * mpt3sas_phy)465 _transport_delete_phy(struct MPT3SAS_ADAPTER *ioc,
466 struct _sas_port *mpt3sas_port, struct _sas_phy *mpt3sas_phy)
467 {
468 u64 sas_address = mpt3sas_port->remote_identify.sas_address;
469
470 dev_printk(KERN_INFO, &mpt3sas_phy->phy->dev,
471 "remove: sas_addr(0x%016llx), phy(%d)\n",
472 (unsigned long long) sas_address, mpt3sas_phy->phy_id);
473
474 list_del(&mpt3sas_phy->port_siblings);
475 mpt3sas_port->num_phys--;
476 sas_port_delete_phy(mpt3sas_port->port, mpt3sas_phy->phy);
477 mpt3sas_phy->phy_belongs_to_port = 0;
478 }
479
480 /**
481 * _transport_add_phy - helper function to adding single phy to port
482 * @ioc: per adapter object
483 * @mpt3sas_port: mpt3sas per port object
484 * @mpt3sas_phy: mpt3sas per phy object
485 */
486 static void
_transport_add_phy(struct MPT3SAS_ADAPTER * ioc,struct _sas_port * mpt3sas_port,struct _sas_phy * mpt3sas_phy)487 _transport_add_phy(struct MPT3SAS_ADAPTER *ioc, struct _sas_port *mpt3sas_port,
488 struct _sas_phy *mpt3sas_phy)
489 {
490 u64 sas_address = mpt3sas_port->remote_identify.sas_address;
491
492 dev_printk(KERN_INFO, &mpt3sas_phy->phy->dev,
493 "add: sas_addr(0x%016llx), phy(%d)\n", (unsigned long long)
494 sas_address, mpt3sas_phy->phy_id);
495
496 list_add_tail(&mpt3sas_phy->port_siblings, &mpt3sas_port->phy_list);
497 mpt3sas_port->num_phys++;
498 sas_port_add_phy(mpt3sas_port->port, mpt3sas_phy->phy);
499 mpt3sas_phy->phy_belongs_to_port = 1;
500 }
501
502 /**
503 * _transport_add_phy_to_an_existing_port - adding new phy to existing port
504 * @ioc: per adapter object
505 * @sas_node: sas node object (either expander or sas host)
506 * @mpt3sas_phy: mpt3sas per phy object
507 * @sas_address: sas address of device/expander were phy needs to be added to
508 */
509 static void
_transport_add_phy_to_an_existing_port(struct MPT3SAS_ADAPTER * ioc,struct _sas_node * sas_node,struct _sas_phy * mpt3sas_phy,u64 sas_address)510 _transport_add_phy_to_an_existing_port(struct MPT3SAS_ADAPTER *ioc,
511 struct _sas_node *sas_node, struct _sas_phy *mpt3sas_phy,
512 u64 sas_address)
513 {
514 struct _sas_port *mpt3sas_port;
515 struct _sas_phy *phy_srch;
516
517 if (mpt3sas_phy->phy_belongs_to_port == 1)
518 return;
519
520 list_for_each_entry(mpt3sas_port, &sas_node->sas_port_list,
521 port_list) {
522 if (mpt3sas_port->remote_identify.sas_address !=
523 sas_address)
524 continue;
525 list_for_each_entry(phy_srch, &mpt3sas_port->phy_list,
526 port_siblings) {
527 if (phy_srch == mpt3sas_phy)
528 return;
529 }
530 _transport_add_phy(ioc, mpt3sas_port, mpt3sas_phy);
531 return;
532 }
533
534 }
535
536 /**
537 * _transport_del_phy_from_an_existing_port - delete phy from existing port
538 * @ioc: per adapter object
539 * @sas_node: sas node object (either expander or sas host)
540 * @mpt3sas_phy: mpt3sas per phy object
541 */
542 static void
_transport_del_phy_from_an_existing_port(struct MPT3SAS_ADAPTER * ioc,struct _sas_node * sas_node,struct _sas_phy * mpt3sas_phy)543 _transport_del_phy_from_an_existing_port(struct MPT3SAS_ADAPTER *ioc,
544 struct _sas_node *sas_node, struct _sas_phy *mpt3sas_phy)
545 {
546 struct _sas_port *mpt3sas_port, *next;
547 struct _sas_phy *phy_srch;
548
549 if (mpt3sas_phy->phy_belongs_to_port == 0)
550 return;
551
552 list_for_each_entry_safe(mpt3sas_port, next, &sas_node->sas_port_list,
553 port_list) {
554 list_for_each_entry(phy_srch, &mpt3sas_port->phy_list,
555 port_siblings) {
556 if (phy_srch != mpt3sas_phy)
557 continue;
558
559 if (mpt3sas_port->num_phys == 1)
560 _transport_delete_port(ioc, mpt3sas_port);
561 else
562 _transport_delete_phy(ioc, mpt3sas_port,
563 mpt3sas_phy);
564 return;
565 }
566 }
567 }
568
569 /**
570 * _transport_sanity_check - sanity check when adding a new port
571 * @ioc: per adapter object
572 * @sas_node: sas node object (either expander or sas host)
573 * @sas_address: sas address of device being added
574 *
575 * See the explanation above from _transport_delete_duplicate_port
576 */
577 static void
_transport_sanity_check(struct MPT3SAS_ADAPTER * ioc,struct _sas_node * sas_node,u64 sas_address)578 _transport_sanity_check(struct MPT3SAS_ADAPTER *ioc, struct _sas_node *sas_node,
579 u64 sas_address)
580 {
581 int i;
582
583 for (i = 0; i < sas_node->num_phys; i++) {
584 if (sas_node->phy[i].remote_identify.sas_address != sas_address)
585 continue;
586 if (sas_node->phy[i].phy_belongs_to_port == 1)
587 _transport_del_phy_from_an_existing_port(ioc, sas_node,
588 &sas_node->phy[i]);
589 }
590 }
591
592 /**
593 * mpt3sas_transport_port_add - insert port to the list
594 * @ioc: per adapter object
595 * @handle: handle of attached device
596 * @sas_address: sas address of parent expander or sas host
597 * Context: This function will acquire ioc->sas_node_lock.
598 *
599 * Adding new port object to the sas_node->sas_port_list.
600 *
601 * Return: mpt3sas_port.
602 */
603 struct _sas_port *
mpt3sas_transport_port_add(struct MPT3SAS_ADAPTER * ioc,u16 handle,u64 sas_address)604 mpt3sas_transport_port_add(struct MPT3SAS_ADAPTER *ioc, u16 handle,
605 u64 sas_address)
606 {
607 struct _sas_phy *mpt3sas_phy, *next;
608 struct _sas_port *mpt3sas_port;
609 unsigned long flags;
610 struct _sas_node *sas_node;
611 struct sas_rphy *rphy;
612 struct _sas_device *sas_device = NULL;
613 int i;
614 struct sas_port *port;
615
616 mpt3sas_port = kzalloc(sizeof(struct _sas_port),
617 GFP_KERNEL);
618 if (!mpt3sas_port) {
619 ioc_err(ioc, "failure at %s:%d/%s()!\n",
620 __FILE__, __LINE__, __func__);
621 return NULL;
622 }
623
624 INIT_LIST_HEAD(&mpt3sas_port->port_list);
625 INIT_LIST_HEAD(&mpt3sas_port->phy_list);
626 spin_lock_irqsave(&ioc->sas_node_lock, flags);
627 sas_node = _transport_sas_node_find_by_sas_address(ioc, sas_address);
628 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
629
630 if (!sas_node) {
631 ioc_err(ioc, "%s: Could not find parent sas_address(0x%016llx)!\n",
632 __func__, (u64)sas_address);
633 goto out_fail;
634 }
635
636 if ((_transport_set_identify(ioc, handle,
637 &mpt3sas_port->remote_identify))) {
638 ioc_err(ioc, "failure at %s:%d/%s()!\n",
639 __FILE__, __LINE__, __func__);
640 goto out_fail;
641 }
642
643 if (mpt3sas_port->remote_identify.device_type == SAS_PHY_UNUSED) {
644 ioc_err(ioc, "failure at %s:%d/%s()!\n",
645 __FILE__, __LINE__, __func__);
646 goto out_fail;
647 }
648
649 _transport_sanity_check(ioc, sas_node,
650 mpt3sas_port->remote_identify.sas_address);
651
652 for (i = 0; i < sas_node->num_phys; i++) {
653 if (sas_node->phy[i].remote_identify.sas_address !=
654 mpt3sas_port->remote_identify.sas_address)
655 continue;
656 list_add_tail(&sas_node->phy[i].port_siblings,
657 &mpt3sas_port->phy_list);
658 mpt3sas_port->num_phys++;
659 }
660
661 if (!mpt3sas_port->num_phys) {
662 ioc_err(ioc, "failure at %s:%d/%s()!\n",
663 __FILE__, __LINE__, __func__);
664 goto out_fail;
665 }
666
667 if (!sas_node->parent_dev) {
668 ioc_err(ioc, "failure at %s:%d/%s()!\n",
669 __FILE__, __LINE__, __func__);
670 goto out_fail;
671 }
672 port = sas_port_alloc_num(sas_node->parent_dev);
673 if ((sas_port_add(port))) {
674 ioc_err(ioc, "failure at %s:%d/%s()!\n",
675 __FILE__, __LINE__, __func__);
676 goto out_fail;
677 }
678
679 list_for_each_entry(mpt3sas_phy, &mpt3sas_port->phy_list,
680 port_siblings) {
681 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
682 dev_printk(KERN_INFO, &port->dev,
683 "add: handle(0x%04x), sas_addr(0x%016llx), phy(%d)\n",
684 handle, (unsigned long long)
685 mpt3sas_port->remote_identify.sas_address,
686 mpt3sas_phy->phy_id);
687 sas_port_add_phy(port, mpt3sas_phy->phy);
688 mpt3sas_phy->phy_belongs_to_port = 1;
689 }
690
691 mpt3sas_port->port = port;
692 if (mpt3sas_port->remote_identify.device_type == SAS_END_DEVICE)
693 rphy = sas_end_device_alloc(port);
694 else
695 rphy = sas_expander_alloc(port,
696 mpt3sas_port->remote_identify.device_type);
697
698 rphy->identify = mpt3sas_port->remote_identify;
699
700 if (mpt3sas_port->remote_identify.device_type == SAS_END_DEVICE) {
701 sas_device = mpt3sas_get_sdev_by_addr(ioc,
702 mpt3sas_port->remote_identify.sas_address);
703 if (!sas_device) {
704 dfailprintk(ioc,
705 ioc_info(ioc, "failure at %s:%d/%s()!\n",
706 __FILE__, __LINE__, __func__));
707 goto out_fail;
708 }
709 sas_device->pend_sas_rphy_add = 1;
710 }
711
712 if ((sas_rphy_add(rphy))) {
713 ioc_err(ioc, "failure at %s:%d/%s()!\n",
714 __FILE__, __LINE__, __func__);
715 sas_rphy_free(rphy);
716 rphy = NULL;
717 }
718
719 if (mpt3sas_port->remote_identify.device_type == SAS_END_DEVICE) {
720 sas_device->pend_sas_rphy_add = 0;
721 sas_device_put(sas_device);
722 }
723
724 dev_info(&rphy->dev,
725 "add: handle(0x%04x), sas_addr(0x%016llx)\n", handle,
726 (unsigned long long)mpt3sas_port->remote_identify.sas_address);
727
728 mpt3sas_port->rphy = rphy;
729 spin_lock_irqsave(&ioc->sas_node_lock, flags);
730 list_add_tail(&mpt3sas_port->port_list, &sas_node->sas_port_list);
731 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
732
733 /* fill in report manufacture */
734 if (mpt3sas_port->remote_identify.device_type ==
735 MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
736 mpt3sas_port->remote_identify.device_type ==
737 MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER)
738 _transport_expander_report_manufacture(ioc,
739 mpt3sas_port->remote_identify.sas_address,
740 rphy_to_expander_device(rphy));
741 return mpt3sas_port;
742
743 out_fail:
744 list_for_each_entry_safe(mpt3sas_phy, next, &mpt3sas_port->phy_list,
745 port_siblings)
746 list_del(&mpt3sas_phy->port_siblings);
747 kfree(mpt3sas_port);
748 return NULL;
749 }
750
751 /**
752 * mpt3sas_transport_port_remove - remove port from the list
753 * @ioc: per adapter object
754 * @sas_address: sas address of attached device
755 * @sas_address_parent: sas address of parent expander or sas host
756 * Context: This function will acquire ioc->sas_node_lock.
757 *
758 * Removing object and freeing associated memory from the
759 * ioc->sas_port_list.
760 */
761 void
mpt3sas_transport_port_remove(struct MPT3SAS_ADAPTER * ioc,u64 sas_address,u64 sas_address_parent)762 mpt3sas_transport_port_remove(struct MPT3SAS_ADAPTER *ioc, u64 sas_address,
763 u64 sas_address_parent)
764 {
765 int i;
766 unsigned long flags;
767 struct _sas_port *mpt3sas_port, *next;
768 struct _sas_node *sas_node;
769 u8 found = 0;
770 struct _sas_phy *mpt3sas_phy, *next_phy;
771
772 spin_lock_irqsave(&ioc->sas_node_lock, flags);
773 sas_node = _transport_sas_node_find_by_sas_address(ioc,
774 sas_address_parent);
775 if (!sas_node) {
776 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
777 return;
778 }
779 list_for_each_entry_safe(mpt3sas_port, next, &sas_node->sas_port_list,
780 port_list) {
781 if (mpt3sas_port->remote_identify.sas_address != sas_address)
782 continue;
783 found = 1;
784 list_del(&mpt3sas_port->port_list);
785 goto out;
786 }
787 out:
788 if (!found) {
789 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
790 return;
791 }
792
793 for (i = 0; i < sas_node->num_phys; i++) {
794 if (sas_node->phy[i].remote_identify.sas_address == sas_address)
795 memset(&sas_node->phy[i].remote_identify, 0 ,
796 sizeof(struct sas_identify));
797 }
798
799 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
800
801 list_for_each_entry_safe(mpt3sas_phy, next_phy,
802 &mpt3sas_port->phy_list, port_siblings) {
803 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
804 dev_printk(KERN_INFO, &mpt3sas_port->port->dev,
805 "remove: sas_addr(0x%016llx), phy(%d)\n",
806 (unsigned long long)
807 mpt3sas_port->remote_identify.sas_address,
808 mpt3sas_phy->phy_id);
809 mpt3sas_phy->phy_belongs_to_port = 0;
810 if (!ioc->remove_host)
811 sas_port_delete_phy(mpt3sas_port->port,
812 mpt3sas_phy->phy);
813 list_del(&mpt3sas_phy->port_siblings);
814 }
815 if (!ioc->remove_host)
816 sas_port_delete(mpt3sas_port->port);
817 ioc_info(ioc, "%s: removed: sas_addr(0x%016llx)\n",
818 __func__, (unsigned long long)sas_address);
819 kfree(mpt3sas_port);
820 }
821
822 /**
823 * mpt3sas_transport_add_host_phy - report sas_host phy to transport
824 * @ioc: per adapter object
825 * @mpt3sas_phy: mpt3sas per phy object
826 * @phy_pg0: sas phy page 0
827 * @parent_dev: parent device class object
828 *
829 * Return: 0 for success, non-zero for failure.
830 */
831 int
mpt3sas_transport_add_host_phy(struct MPT3SAS_ADAPTER * ioc,struct _sas_phy * mpt3sas_phy,Mpi2SasPhyPage0_t phy_pg0,struct device * parent_dev)832 mpt3sas_transport_add_host_phy(struct MPT3SAS_ADAPTER *ioc, struct _sas_phy
833 *mpt3sas_phy, Mpi2SasPhyPage0_t phy_pg0, struct device *parent_dev)
834 {
835 struct sas_phy *phy;
836 int phy_index = mpt3sas_phy->phy_id;
837
838
839 INIT_LIST_HEAD(&mpt3sas_phy->port_siblings);
840 phy = sas_phy_alloc(parent_dev, phy_index);
841 if (!phy) {
842 ioc_err(ioc, "failure at %s:%d/%s()!\n",
843 __FILE__, __LINE__, __func__);
844 return -1;
845 }
846 if ((_transport_set_identify(ioc, mpt3sas_phy->handle,
847 &mpt3sas_phy->identify))) {
848 ioc_err(ioc, "failure at %s:%d/%s()!\n",
849 __FILE__, __LINE__, __func__);
850 sas_phy_free(phy);
851 return -1;
852 }
853 phy->identify = mpt3sas_phy->identify;
854 mpt3sas_phy->attached_handle = le16_to_cpu(phy_pg0.AttachedDevHandle);
855 if (mpt3sas_phy->attached_handle)
856 _transport_set_identify(ioc, mpt3sas_phy->attached_handle,
857 &mpt3sas_phy->remote_identify);
858 phy->identify.phy_identifier = mpt3sas_phy->phy_id;
859 phy->negotiated_linkrate = _transport_convert_phy_link_rate(
860 phy_pg0.NegotiatedLinkRate & MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
861 phy->minimum_linkrate_hw = _transport_convert_phy_link_rate(
862 phy_pg0.HwLinkRate & MPI2_SAS_HWRATE_MIN_RATE_MASK);
863 phy->maximum_linkrate_hw = _transport_convert_phy_link_rate(
864 phy_pg0.HwLinkRate >> 4);
865 phy->minimum_linkrate = _transport_convert_phy_link_rate(
866 phy_pg0.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
867 phy->maximum_linkrate = _transport_convert_phy_link_rate(
868 phy_pg0.ProgrammedLinkRate >> 4);
869
870 if ((sas_phy_add(phy))) {
871 ioc_err(ioc, "failure at %s:%d/%s()!\n",
872 __FILE__, __LINE__, __func__);
873 sas_phy_free(phy);
874 return -1;
875 }
876 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
877 dev_printk(KERN_INFO, &phy->dev,
878 "add: handle(0x%04x), sas_addr(0x%016llx)\n"
879 "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
880 mpt3sas_phy->handle, (unsigned long long)
881 mpt3sas_phy->identify.sas_address,
882 mpt3sas_phy->attached_handle,
883 (unsigned long long)
884 mpt3sas_phy->remote_identify.sas_address);
885 mpt3sas_phy->phy = phy;
886 return 0;
887 }
888
889
890 /**
891 * mpt3sas_transport_add_expander_phy - report expander phy to transport
892 * @ioc: per adapter object
893 * @mpt3sas_phy: mpt3sas per phy object
894 * @expander_pg1: expander page 1
895 * @parent_dev: parent device class object
896 *
897 * Return: 0 for success, non-zero for failure.
898 */
899 int
mpt3sas_transport_add_expander_phy(struct MPT3SAS_ADAPTER * ioc,struct _sas_phy * mpt3sas_phy,Mpi2ExpanderPage1_t expander_pg1,struct device * parent_dev)900 mpt3sas_transport_add_expander_phy(struct MPT3SAS_ADAPTER *ioc, struct _sas_phy
901 *mpt3sas_phy, Mpi2ExpanderPage1_t expander_pg1,
902 struct device *parent_dev)
903 {
904 struct sas_phy *phy;
905 int phy_index = mpt3sas_phy->phy_id;
906
907 INIT_LIST_HEAD(&mpt3sas_phy->port_siblings);
908 phy = sas_phy_alloc(parent_dev, phy_index);
909 if (!phy) {
910 ioc_err(ioc, "failure at %s:%d/%s()!\n",
911 __FILE__, __LINE__, __func__);
912 return -1;
913 }
914 if ((_transport_set_identify(ioc, mpt3sas_phy->handle,
915 &mpt3sas_phy->identify))) {
916 ioc_err(ioc, "failure at %s:%d/%s()!\n",
917 __FILE__, __LINE__, __func__);
918 sas_phy_free(phy);
919 return -1;
920 }
921 phy->identify = mpt3sas_phy->identify;
922 mpt3sas_phy->attached_handle =
923 le16_to_cpu(expander_pg1.AttachedDevHandle);
924 if (mpt3sas_phy->attached_handle)
925 _transport_set_identify(ioc, mpt3sas_phy->attached_handle,
926 &mpt3sas_phy->remote_identify);
927 phy->identify.phy_identifier = mpt3sas_phy->phy_id;
928 phy->negotiated_linkrate = _transport_convert_phy_link_rate(
929 expander_pg1.NegotiatedLinkRate &
930 MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
931 phy->minimum_linkrate_hw = _transport_convert_phy_link_rate(
932 expander_pg1.HwLinkRate & MPI2_SAS_HWRATE_MIN_RATE_MASK);
933 phy->maximum_linkrate_hw = _transport_convert_phy_link_rate(
934 expander_pg1.HwLinkRate >> 4);
935 phy->minimum_linkrate = _transport_convert_phy_link_rate(
936 expander_pg1.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
937 phy->maximum_linkrate = _transport_convert_phy_link_rate(
938 expander_pg1.ProgrammedLinkRate >> 4);
939
940 if ((sas_phy_add(phy))) {
941 ioc_err(ioc, "failure at %s:%d/%s()!\n",
942 __FILE__, __LINE__, __func__);
943 sas_phy_free(phy);
944 return -1;
945 }
946 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
947 dev_printk(KERN_INFO, &phy->dev,
948 "add: handle(0x%04x), sas_addr(0x%016llx)\n"
949 "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
950 mpt3sas_phy->handle, (unsigned long long)
951 mpt3sas_phy->identify.sas_address,
952 mpt3sas_phy->attached_handle,
953 (unsigned long long)
954 mpt3sas_phy->remote_identify.sas_address);
955 mpt3sas_phy->phy = phy;
956 return 0;
957 }
958
959 /**
960 * mpt3sas_transport_update_links - refreshing phy link changes
961 * @ioc: per adapter object
962 * @sas_address: sas address of parent expander or sas host
963 * @handle: attached device handle
964 * @phy_number: phy number
965 * @link_rate: new link rate
966 */
967 void
mpt3sas_transport_update_links(struct MPT3SAS_ADAPTER * ioc,u64 sas_address,u16 handle,u8 phy_number,u8 link_rate)968 mpt3sas_transport_update_links(struct MPT3SAS_ADAPTER *ioc,
969 u64 sas_address, u16 handle, u8 phy_number, u8 link_rate)
970 {
971 unsigned long flags;
972 struct _sas_node *sas_node;
973 struct _sas_phy *mpt3sas_phy;
974
975 if (ioc->shost_recovery || ioc->pci_error_recovery)
976 return;
977
978 spin_lock_irqsave(&ioc->sas_node_lock, flags);
979 sas_node = _transport_sas_node_find_by_sas_address(ioc, sas_address);
980 if (!sas_node) {
981 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
982 return;
983 }
984
985 mpt3sas_phy = &sas_node->phy[phy_number];
986 mpt3sas_phy->attached_handle = handle;
987 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
988 if (handle && (link_rate >= MPI2_SAS_NEG_LINK_RATE_1_5)) {
989 _transport_set_identify(ioc, handle,
990 &mpt3sas_phy->remote_identify);
991 _transport_add_phy_to_an_existing_port(ioc, sas_node,
992 mpt3sas_phy, mpt3sas_phy->remote_identify.sas_address);
993 } else
994 memset(&mpt3sas_phy->remote_identify, 0 , sizeof(struct
995 sas_identify));
996
997 if (mpt3sas_phy->phy)
998 mpt3sas_phy->phy->negotiated_linkrate =
999 _transport_convert_phy_link_rate(link_rate);
1000
1001 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
1002 dev_printk(KERN_INFO, &mpt3sas_phy->phy->dev,
1003 "refresh: parent sas_addr(0x%016llx),\n"
1004 "\tlink_rate(0x%02x), phy(%d)\n"
1005 "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
1006 (unsigned long long)sas_address,
1007 link_rate, phy_number, handle, (unsigned long long)
1008 mpt3sas_phy->remote_identify.sas_address);
1009 }
1010
1011 static inline void *
phy_to_ioc(struct sas_phy * phy)1012 phy_to_ioc(struct sas_phy *phy)
1013 {
1014 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
1015 return shost_priv(shost);
1016 }
1017
1018 static inline void *
rphy_to_ioc(struct sas_rphy * rphy)1019 rphy_to_ioc(struct sas_rphy *rphy)
1020 {
1021 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
1022 return shost_priv(shost);
1023 }
1024
1025 /* report phy error log structure */
1026 struct phy_error_log_request {
1027 u8 smp_frame_type; /* 0x40 */
1028 u8 function; /* 0x11 */
1029 u8 allocated_response_length;
1030 u8 request_length; /* 02 */
1031 u8 reserved_1[5];
1032 u8 phy_identifier;
1033 u8 reserved_2[2];
1034 };
1035
1036 /* report phy error log reply structure */
1037 struct phy_error_log_reply {
1038 u8 smp_frame_type; /* 0x41 */
1039 u8 function; /* 0x11 */
1040 u8 function_result;
1041 u8 response_length;
1042 __be16 expander_change_count;
1043 u8 reserved_1[3];
1044 u8 phy_identifier;
1045 u8 reserved_2[2];
1046 __be32 invalid_dword;
1047 __be32 running_disparity_error;
1048 __be32 loss_of_dword_sync;
1049 __be32 phy_reset_problem;
1050 };
1051
1052 /**
1053 * _transport_get_expander_phy_error_log - return expander counters
1054 * @ioc: per adapter object
1055 * @phy: The sas phy object
1056 *
1057 * Return: 0 for success, non-zero for failure.
1058 *
1059 */
1060 static int
_transport_get_expander_phy_error_log(struct MPT3SAS_ADAPTER * ioc,struct sas_phy * phy)1061 _transport_get_expander_phy_error_log(struct MPT3SAS_ADAPTER *ioc,
1062 struct sas_phy *phy)
1063 {
1064 Mpi2SmpPassthroughRequest_t *mpi_request;
1065 Mpi2SmpPassthroughReply_t *mpi_reply;
1066 struct phy_error_log_request *phy_error_log_request;
1067 struct phy_error_log_reply *phy_error_log_reply;
1068 int rc;
1069 u16 smid;
1070 void *psge;
1071 u8 issue_reset = 0;
1072 void *data_out = NULL;
1073 dma_addr_t data_out_dma;
1074 u32 sz;
1075
1076 if (ioc->shost_recovery || ioc->pci_error_recovery) {
1077 ioc_info(ioc, "%s: host reset in progress!\n", __func__);
1078 return -EFAULT;
1079 }
1080
1081 mutex_lock(&ioc->transport_cmds.mutex);
1082
1083 if (ioc->transport_cmds.status != MPT3_CMD_NOT_USED) {
1084 ioc_err(ioc, "%s: transport_cmds in use\n", __func__);
1085 rc = -EAGAIN;
1086 goto out;
1087 }
1088 ioc->transport_cmds.status = MPT3_CMD_PENDING;
1089
1090 rc = mpt3sas_wait_for_ioc(ioc, IOC_OPERATIONAL_WAIT_COUNT);
1091 if (rc)
1092 goto out;
1093
1094 smid = mpt3sas_base_get_smid(ioc, ioc->transport_cb_idx);
1095 if (!smid) {
1096 ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
1097 rc = -EAGAIN;
1098 goto out;
1099 }
1100
1101 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
1102 ioc->transport_cmds.smid = smid;
1103
1104 sz = sizeof(struct phy_error_log_request) +
1105 sizeof(struct phy_error_log_reply);
1106 data_out = dma_alloc_coherent(&ioc->pdev->dev, sz, &data_out_dma,
1107 GFP_KERNEL);
1108 if (!data_out) {
1109 pr_err("failure at %s:%d/%s()!\n", __FILE__,
1110 __LINE__, __func__);
1111 rc = -ENOMEM;
1112 mpt3sas_base_free_smid(ioc, smid);
1113 goto out;
1114 }
1115
1116 rc = -EINVAL;
1117 memset(data_out, 0, sz);
1118 phy_error_log_request = data_out;
1119 phy_error_log_request->smp_frame_type = 0x40;
1120 phy_error_log_request->function = 0x11;
1121 phy_error_log_request->request_length = 2;
1122 phy_error_log_request->allocated_response_length = 0;
1123 phy_error_log_request->phy_identifier = phy->number;
1124
1125 memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
1126 mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
1127 mpi_request->PhysicalPort = 0xFF;
1128 mpi_request->VF_ID = 0; /* TODO */
1129 mpi_request->VP_ID = 0;
1130 mpi_request->SASAddress = cpu_to_le64(phy->identify.sas_address);
1131 mpi_request->RequestDataLength =
1132 cpu_to_le16(sizeof(struct phy_error_log_request));
1133 psge = &mpi_request->SGL;
1134
1135 ioc->build_sg(ioc, psge, data_out_dma,
1136 sizeof(struct phy_error_log_request),
1137 data_out_dma + sizeof(struct phy_error_log_request),
1138 sizeof(struct phy_error_log_reply));
1139
1140 dtransportprintk(ioc,
1141 ioc_info(ioc, "phy_error_log - send to sas_addr(0x%016llx), phy(%d)\n",
1142 (u64)phy->identify.sas_address,
1143 phy->number));
1144 init_completion(&ioc->transport_cmds.done);
1145 ioc->put_smid_default(ioc, smid);
1146 wait_for_completion_timeout(&ioc->transport_cmds.done, 10*HZ);
1147
1148 if (!(ioc->transport_cmds.status & MPT3_CMD_COMPLETE)) {
1149 ioc_err(ioc, "%s: timeout\n", __func__);
1150 _debug_dump_mf(mpi_request,
1151 sizeof(Mpi2SmpPassthroughRequest_t)/4);
1152 if (!(ioc->transport_cmds.status & MPT3_CMD_RESET))
1153 issue_reset = 1;
1154 goto issue_host_reset;
1155 }
1156
1157 dtransportprintk(ioc, ioc_info(ioc, "phy_error_log - complete\n"));
1158
1159 if (ioc->transport_cmds.status & MPT3_CMD_REPLY_VALID) {
1160
1161 mpi_reply = ioc->transport_cmds.reply;
1162
1163 dtransportprintk(ioc,
1164 ioc_info(ioc, "phy_error_log - reply data transfer size(%d)\n",
1165 le16_to_cpu(mpi_reply->ResponseDataLength)));
1166
1167 if (le16_to_cpu(mpi_reply->ResponseDataLength) !=
1168 sizeof(struct phy_error_log_reply))
1169 goto out;
1170
1171 phy_error_log_reply = data_out +
1172 sizeof(struct phy_error_log_request);
1173
1174 dtransportprintk(ioc,
1175 ioc_info(ioc, "phy_error_log - function_result(%d)\n",
1176 phy_error_log_reply->function_result));
1177
1178 phy->invalid_dword_count =
1179 be32_to_cpu(phy_error_log_reply->invalid_dword);
1180 phy->running_disparity_error_count =
1181 be32_to_cpu(phy_error_log_reply->running_disparity_error);
1182 phy->loss_of_dword_sync_count =
1183 be32_to_cpu(phy_error_log_reply->loss_of_dword_sync);
1184 phy->phy_reset_problem_count =
1185 be32_to_cpu(phy_error_log_reply->phy_reset_problem);
1186 rc = 0;
1187 } else
1188 dtransportprintk(ioc,
1189 ioc_info(ioc, "phy_error_log - no reply\n"));
1190
1191 issue_host_reset:
1192 if (issue_reset)
1193 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
1194 out:
1195 ioc->transport_cmds.status = MPT3_CMD_NOT_USED;
1196 if (data_out)
1197 dma_free_coherent(&ioc->pdev->dev, sz, data_out, data_out_dma);
1198
1199 mutex_unlock(&ioc->transport_cmds.mutex);
1200 return rc;
1201 }
1202
1203 /**
1204 * _transport_get_linkerrors - return phy counters for both hba and expanders
1205 * @phy: The sas phy object
1206 *
1207 * Return: 0 for success, non-zero for failure.
1208 *
1209 */
1210 static int
_transport_get_linkerrors(struct sas_phy * phy)1211 _transport_get_linkerrors(struct sas_phy *phy)
1212 {
1213 struct MPT3SAS_ADAPTER *ioc = phy_to_ioc(phy);
1214 unsigned long flags;
1215 Mpi2ConfigReply_t mpi_reply;
1216 Mpi2SasPhyPage1_t phy_pg1;
1217
1218 spin_lock_irqsave(&ioc->sas_node_lock, flags);
1219 if (_transport_sas_node_find_by_sas_address(ioc,
1220 phy->identify.sas_address) == NULL) {
1221 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1222 return -EINVAL;
1223 }
1224 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1225
1226 if (phy->identify.sas_address != ioc->sas_hba.sas_address)
1227 return _transport_get_expander_phy_error_log(ioc, phy);
1228
1229 /* get hba phy error logs */
1230 if ((mpt3sas_config_get_phy_pg1(ioc, &mpi_reply, &phy_pg1,
1231 phy->number))) {
1232 ioc_err(ioc, "failure at %s:%d/%s()!\n",
1233 __FILE__, __LINE__, __func__);
1234 return -ENXIO;
1235 }
1236
1237 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo)
1238 ioc_info(ioc, "phy(%d), ioc_status (0x%04x), loginfo(0x%08x)\n",
1239 phy->number,
1240 le16_to_cpu(mpi_reply.IOCStatus),
1241 le32_to_cpu(mpi_reply.IOCLogInfo));
1242
1243 phy->invalid_dword_count = le32_to_cpu(phy_pg1.InvalidDwordCount);
1244 phy->running_disparity_error_count =
1245 le32_to_cpu(phy_pg1.RunningDisparityErrorCount);
1246 phy->loss_of_dword_sync_count =
1247 le32_to_cpu(phy_pg1.LossDwordSynchCount);
1248 phy->phy_reset_problem_count =
1249 le32_to_cpu(phy_pg1.PhyResetProblemCount);
1250 return 0;
1251 }
1252
1253 /**
1254 * _transport_get_enclosure_identifier -
1255 * @rphy: The sas phy object
1256 * @identifier: ?
1257 *
1258 * Obtain the enclosure logical id for an expander.
1259 * Return: 0 for success, non-zero for failure.
1260 */
1261 static int
_transport_get_enclosure_identifier(struct sas_rphy * rphy,u64 * identifier)1262 _transport_get_enclosure_identifier(struct sas_rphy *rphy, u64 *identifier)
1263 {
1264 struct MPT3SAS_ADAPTER *ioc = rphy_to_ioc(rphy);
1265 struct _sas_device *sas_device;
1266 unsigned long flags;
1267 int rc;
1268
1269 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1270 sas_device = __mpt3sas_get_sdev_by_addr(ioc,
1271 rphy->identify.sas_address);
1272 if (sas_device) {
1273 *identifier = sas_device->enclosure_logical_id;
1274 rc = 0;
1275 sas_device_put(sas_device);
1276 } else {
1277 *identifier = 0;
1278 rc = -ENXIO;
1279 }
1280
1281 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1282 return rc;
1283 }
1284
1285 /**
1286 * _transport_get_bay_identifier -
1287 * @rphy: The sas phy object
1288 *
1289 * Return: the slot id for a device that resides inside an enclosure.
1290 */
1291 static int
_transport_get_bay_identifier(struct sas_rphy * rphy)1292 _transport_get_bay_identifier(struct sas_rphy *rphy)
1293 {
1294 struct MPT3SAS_ADAPTER *ioc = rphy_to_ioc(rphy);
1295 struct _sas_device *sas_device;
1296 unsigned long flags;
1297 int rc;
1298
1299 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1300 sas_device = __mpt3sas_get_sdev_by_addr(ioc,
1301 rphy->identify.sas_address);
1302 if (sas_device) {
1303 rc = sas_device->slot;
1304 sas_device_put(sas_device);
1305 } else {
1306 rc = -ENXIO;
1307 }
1308 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1309 return rc;
1310 }
1311
1312 /* phy control request structure */
1313 struct phy_control_request {
1314 u8 smp_frame_type; /* 0x40 */
1315 u8 function; /* 0x91 */
1316 u8 allocated_response_length;
1317 u8 request_length; /* 0x09 */
1318 u16 expander_change_count;
1319 u8 reserved_1[3];
1320 u8 phy_identifier;
1321 u8 phy_operation;
1322 u8 reserved_2[13];
1323 u64 attached_device_name;
1324 u8 programmed_min_physical_link_rate;
1325 u8 programmed_max_physical_link_rate;
1326 u8 reserved_3[6];
1327 };
1328
1329 /* phy control reply structure */
1330 struct phy_control_reply {
1331 u8 smp_frame_type; /* 0x41 */
1332 u8 function; /* 0x11 */
1333 u8 function_result;
1334 u8 response_length;
1335 };
1336
1337 #define SMP_PHY_CONTROL_LINK_RESET (0x01)
1338 #define SMP_PHY_CONTROL_HARD_RESET (0x02)
1339 #define SMP_PHY_CONTROL_DISABLE (0x03)
1340
1341 /**
1342 * _transport_expander_phy_control - expander phy control
1343 * @ioc: per adapter object
1344 * @phy: The sas phy object
1345 * @phy_operation: ?
1346 *
1347 * Return: 0 for success, non-zero for failure.
1348 *
1349 */
1350 static int
_transport_expander_phy_control(struct MPT3SAS_ADAPTER * ioc,struct sas_phy * phy,u8 phy_operation)1351 _transport_expander_phy_control(struct MPT3SAS_ADAPTER *ioc,
1352 struct sas_phy *phy, u8 phy_operation)
1353 {
1354 Mpi2SmpPassthroughRequest_t *mpi_request;
1355 Mpi2SmpPassthroughReply_t *mpi_reply;
1356 struct phy_control_request *phy_control_request;
1357 struct phy_control_reply *phy_control_reply;
1358 int rc;
1359 u16 smid;
1360 void *psge;
1361 u8 issue_reset = 0;
1362 void *data_out = NULL;
1363 dma_addr_t data_out_dma;
1364 u32 sz;
1365
1366 if (ioc->shost_recovery || ioc->pci_error_recovery) {
1367 ioc_info(ioc, "%s: host reset in progress!\n", __func__);
1368 return -EFAULT;
1369 }
1370
1371 mutex_lock(&ioc->transport_cmds.mutex);
1372
1373 if (ioc->transport_cmds.status != MPT3_CMD_NOT_USED) {
1374 ioc_err(ioc, "%s: transport_cmds in use\n", __func__);
1375 rc = -EAGAIN;
1376 goto out;
1377 }
1378 ioc->transport_cmds.status = MPT3_CMD_PENDING;
1379
1380 rc = mpt3sas_wait_for_ioc(ioc, IOC_OPERATIONAL_WAIT_COUNT);
1381 if (rc)
1382 goto out;
1383
1384 smid = mpt3sas_base_get_smid(ioc, ioc->transport_cb_idx);
1385 if (!smid) {
1386 ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
1387 rc = -EAGAIN;
1388 goto out;
1389 }
1390
1391 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
1392 ioc->transport_cmds.smid = smid;
1393
1394 sz = sizeof(struct phy_control_request) +
1395 sizeof(struct phy_control_reply);
1396 data_out = dma_alloc_coherent(&ioc->pdev->dev, sz, &data_out_dma,
1397 GFP_KERNEL);
1398 if (!data_out) {
1399 pr_err("failure at %s:%d/%s()!\n", __FILE__,
1400 __LINE__, __func__);
1401 rc = -ENOMEM;
1402 mpt3sas_base_free_smid(ioc, smid);
1403 goto out;
1404 }
1405
1406 rc = -EINVAL;
1407 memset(data_out, 0, sz);
1408 phy_control_request = data_out;
1409 phy_control_request->smp_frame_type = 0x40;
1410 phy_control_request->function = 0x91;
1411 phy_control_request->request_length = 9;
1412 phy_control_request->allocated_response_length = 0;
1413 phy_control_request->phy_identifier = phy->number;
1414 phy_control_request->phy_operation = phy_operation;
1415 phy_control_request->programmed_min_physical_link_rate =
1416 phy->minimum_linkrate << 4;
1417 phy_control_request->programmed_max_physical_link_rate =
1418 phy->maximum_linkrate << 4;
1419
1420 memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
1421 mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
1422 mpi_request->PhysicalPort = 0xFF;
1423 mpi_request->VF_ID = 0; /* TODO */
1424 mpi_request->VP_ID = 0;
1425 mpi_request->SASAddress = cpu_to_le64(phy->identify.sas_address);
1426 mpi_request->RequestDataLength =
1427 cpu_to_le16(sizeof(struct phy_error_log_request));
1428 psge = &mpi_request->SGL;
1429
1430 ioc->build_sg(ioc, psge, data_out_dma,
1431 sizeof(struct phy_control_request),
1432 data_out_dma + sizeof(struct phy_control_request),
1433 sizeof(struct phy_control_reply));
1434
1435 dtransportprintk(ioc,
1436 ioc_info(ioc, "phy_control - send to sas_addr(0x%016llx), phy(%d), opcode(%d)\n",
1437 (u64)phy->identify.sas_address,
1438 phy->number, phy_operation));
1439 init_completion(&ioc->transport_cmds.done);
1440 ioc->put_smid_default(ioc, smid);
1441 wait_for_completion_timeout(&ioc->transport_cmds.done, 10*HZ);
1442
1443 if (!(ioc->transport_cmds.status & MPT3_CMD_COMPLETE)) {
1444 ioc_err(ioc, "%s: timeout\n", __func__);
1445 _debug_dump_mf(mpi_request,
1446 sizeof(Mpi2SmpPassthroughRequest_t)/4);
1447 if (!(ioc->transport_cmds.status & MPT3_CMD_RESET))
1448 issue_reset = 1;
1449 goto issue_host_reset;
1450 }
1451
1452 dtransportprintk(ioc, ioc_info(ioc, "phy_control - complete\n"));
1453
1454 if (ioc->transport_cmds.status & MPT3_CMD_REPLY_VALID) {
1455
1456 mpi_reply = ioc->transport_cmds.reply;
1457
1458 dtransportprintk(ioc,
1459 ioc_info(ioc, "phy_control - reply data transfer size(%d)\n",
1460 le16_to_cpu(mpi_reply->ResponseDataLength)));
1461
1462 if (le16_to_cpu(mpi_reply->ResponseDataLength) !=
1463 sizeof(struct phy_control_reply))
1464 goto out;
1465
1466 phy_control_reply = data_out +
1467 sizeof(struct phy_control_request);
1468
1469 dtransportprintk(ioc,
1470 ioc_info(ioc, "phy_control - function_result(%d)\n",
1471 phy_control_reply->function_result));
1472
1473 rc = 0;
1474 } else
1475 dtransportprintk(ioc,
1476 ioc_info(ioc, "phy_control - no reply\n"));
1477
1478 issue_host_reset:
1479 if (issue_reset)
1480 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
1481 out:
1482 ioc->transport_cmds.status = MPT3_CMD_NOT_USED;
1483 if (data_out)
1484 dma_free_coherent(&ioc->pdev->dev, sz, data_out,
1485 data_out_dma);
1486
1487 mutex_unlock(&ioc->transport_cmds.mutex);
1488 return rc;
1489 }
1490
1491 /**
1492 * _transport_phy_reset -
1493 * @phy: The sas phy object
1494 * @hard_reset:
1495 *
1496 * Return: 0 for success, non-zero for failure.
1497 */
1498 static int
_transport_phy_reset(struct sas_phy * phy,int hard_reset)1499 _transport_phy_reset(struct sas_phy *phy, int hard_reset)
1500 {
1501 struct MPT3SAS_ADAPTER *ioc = phy_to_ioc(phy);
1502 Mpi2SasIoUnitControlReply_t mpi_reply;
1503 Mpi2SasIoUnitControlRequest_t mpi_request;
1504 unsigned long flags;
1505
1506 spin_lock_irqsave(&ioc->sas_node_lock, flags);
1507 if (_transport_sas_node_find_by_sas_address(ioc,
1508 phy->identify.sas_address) == NULL) {
1509 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1510 return -EINVAL;
1511 }
1512 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1513
1514 /* handle expander phys */
1515 if (phy->identify.sas_address != ioc->sas_hba.sas_address)
1516 return _transport_expander_phy_control(ioc, phy,
1517 (hard_reset == 1) ? SMP_PHY_CONTROL_HARD_RESET :
1518 SMP_PHY_CONTROL_LINK_RESET);
1519
1520 /* handle hba phys */
1521 memset(&mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
1522 mpi_request.Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
1523 mpi_request.Operation = hard_reset ?
1524 MPI2_SAS_OP_PHY_HARD_RESET : MPI2_SAS_OP_PHY_LINK_RESET;
1525 mpi_request.PhyNum = phy->number;
1526
1527 if ((mpt3sas_base_sas_iounit_control(ioc, &mpi_reply, &mpi_request))) {
1528 ioc_err(ioc, "failure at %s:%d/%s()!\n",
1529 __FILE__, __LINE__, __func__);
1530 return -ENXIO;
1531 }
1532
1533 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo)
1534 ioc_info(ioc, "phy(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
1535 phy->number, le16_to_cpu(mpi_reply.IOCStatus),
1536 le32_to_cpu(mpi_reply.IOCLogInfo));
1537
1538 return 0;
1539 }
1540
1541 /**
1542 * _transport_phy_enable - enable/disable phys
1543 * @phy: The sas phy object
1544 * @enable: enable phy when true
1545 *
1546 * Only support sas_host direct attached phys.
1547 * Return: 0 for success, non-zero for failure.
1548 */
1549 static int
_transport_phy_enable(struct sas_phy * phy,int enable)1550 _transport_phy_enable(struct sas_phy *phy, int enable)
1551 {
1552 struct MPT3SAS_ADAPTER *ioc = phy_to_ioc(phy);
1553 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
1554 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
1555 Mpi2ConfigReply_t mpi_reply;
1556 u16 ioc_status;
1557 u16 sz;
1558 int rc = 0;
1559 unsigned long flags;
1560 int i, discovery_active;
1561
1562 spin_lock_irqsave(&ioc->sas_node_lock, flags);
1563 if (_transport_sas_node_find_by_sas_address(ioc,
1564 phy->identify.sas_address) == NULL) {
1565 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1566 return -EINVAL;
1567 }
1568 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1569
1570 /* handle expander phys */
1571 if (phy->identify.sas_address != ioc->sas_hba.sas_address)
1572 return _transport_expander_phy_control(ioc, phy,
1573 (enable == 1) ? SMP_PHY_CONTROL_LINK_RESET :
1574 SMP_PHY_CONTROL_DISABLE);
1575
1576 /* handle hba phys */
1577
1578 /* read sas_iounit page 0 */
1579 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
1580 sizeof(Mpi2SasIOUnit0PhyData_t));
1581 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
1582 if (!sas_iounit_pg0) {
1583 ioc_err(ioc, "failure at %s:%d/%s()!\n",
1584 __FILE__, __LINE__, __func__);
1585 rc = -ENOMEM;
1586 goto out;
1587 }
1588 if ((mpt3sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
1589 sas_iounit_pg0, sz))) {
1590 ioc_err(ioc, "failure at %s:%d/%s()!\n",
1591 __FILE__, __LINE__, __func__);
1592 rc = -ENXIO;
1593 goto out;
1594 }
1595 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1596 MPI2_IOCSTATUS_MASK;
1597 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1598 ioc_err(ioc, "failure at %s:%d/%s()!\n",
1599 __FILE__, __LINE__, __func__);
1600 rc = -EIO;
1601 goto out;
1602 }
1603
1604 /* unable to enable/disable phys when when discovery is active */
1605 for (i = 0, discovery_active = 0; i < ioc->sas_hba.num_phys ; i++) {
1606 if (sas_iounit_pg0->PhyData[i].PortFlags &
1607 MPI2_SASIOUNIT0_PORTFLAGS_DISCOVERY_IN_PROGRESS) {
1608 ioc_err(ioc, "discovery is active on port = %d, phy = %d: unable to enable/disable phys, try again later!\n",
1609 sas_iounit_pg0->PhyData[i].Port, i);
1610 discovery_active = 1;
1611 }
1612 }
1613
1614 if (discovery_active) {
1615 rc = -EAGAIN;
1616 goto out;
1617 }
1618
1619 /* read sas_iounit page 1 */
1620 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
1621 sizeof(Mpi2SasIOUnit1PhyData_t));
1622 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
1623 if (!sas_iounit_pg1) {
1624 ioc_err(ioc, "failure at %s:%d/%s()!\n",
1625 __FILE__, __LINE__, __func__);
1626 rc = -ENOMEM;
1627 goto out;
1628 }
1629 if ((mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
1630 sas_iounit_pg1, sz))) {
1631 ioc_err(ioc, "failure at %s:%d/%s()!\n",
1632 __FILE__, __LINE__, __func__);
1633 rc = -ENXIO;
1634 goto out;
1635 }
1636 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1637 MPI2_IOCSTATUS_MASK;
1638 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1639 ioc_err(ioc, "failure at %s:%d/%s()!\n",
1640 __FILE__, __LINE__, __func__);
1641 rc = -EIO;
1642 goto out;
1643 }
1644
1645 /* copy Port/PortFlags/PhyFlags from page 0 */
1646 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
1647 sas_iounit_pg1->PhyData[i].Port =
1648 sas_iounit_pg0->PhyData[i].Port;
1649 sas_iounit_pg1->PhyData[i].PortFlags =
1650 (sas_iounit_pg0->PhyData[i].PortFlags &
1651 MPI2_SASIOUNIT0_PORTFLAGS_AUTO_PORT_CONFIG);
1652 sas_iounit_pg1->PhyData[i].PhyFlags =
1653 (sas_iounit_pg0->PhyData[i].PhyFlags &
1654 (MPI2_SASIOUNIT0_PHYFLAGS_ZONING_ENABLED +
1655 MPI2_SASIOUNIT0_PHYFLAGS_PHY_DISABLED));
1656 }
1657
1658 if (enable)
1659 sas_iounit_pg1->PhyData[phy->number].PhyFlags
1660 &= ~MPI2_SASIOUNIT1_PHYFLAGS_PHY_DISABLE;
1661 else
1662 sas_iounit_pg1->PhyData[phy->number].PhyFlags
1663 |= MPI2_SASIOUNIT1_PHYFLAGS_PHY_DISABLE;
1664
1665 mpt3sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1, sz);
1666
1667 /* link reset */
1668 if (enable)
1669 _transport_phy_reset(phy, 0);
1670
1671 out:
1672 kfree(sas_iounit_pg1);
1673 kfree(sas_iounit_pg0);
1674 return rc;
1675 }
1676
1677 /**
1678 * _transport_phy_speed - set phy min/max link rates
1679 * @phy: The sas phy object
1680 * @rates: rates defined in sas_phy_linkrates
1681 *
1682 * Only support sas_host direct attached phys.
1683 *
1684 * Return: 0 for success, non-zero for failure.
1685 */
1686 static int
_transport_phy_speed(struct sas_phy * phy,struct sas_phy_linkrates * rates)1687 _transport_phy_speed(struct sas_phy *phy, struct sas_phy_linkrates *rates)
1688 {
1689 struct MPT3SAS_ADAPTER *ioc = phy_to_ioc(phy);
1690 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
1691 Mpi2SasPhyPage0_t phy_pg0;
1692 Mpi2ConfigReply_t mpi_reply;
1693 u16 ioc_status;
1694 u16 sz;
1695 int i;
1696 int rc = 0;
1697 unsigned long flags;
1698
1699 spin_lock_irqsave(&ioc->sas_node_lock, flags);
1700 if (_transport_sas_node_find_by_sas_address(ioc,
1701 phy->identify.sas_address) == NULL) {
1702 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1703 return -EINVAL;
1704 }
1705 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1706
1707 if (!rates->minimum_linkrate)
1708 rates->minimum_linkrate = phy->minimum_linkrate;
1709 else if (rates->minimum_linkrate < phy->minimum_linkrate_hw)
1710 rates->minimum_linkrate = phy->minimum_linkrate_hw;
1711
1712 if (!rates->maximum_linkrate)
1713 rates->maximum_linkrate = phy->maximum_linkrate;
1714 else if (rates->maximum_linkrate > phy->maximum_linkrate_hw)
1715 rates->maximum_linkrate = phy->maximum_linkrate_hw;
1716
1717 /* handle expander phys */
1718 if (phy->identify.sas_address != ioc->sas_hba.sas_address) {
1719 phy->minimum_linkrate = rates->minimum_linkrate;
1720 phy->maximum_linkrate = rates->maximum_linkrate;
1721 return _transport_expander_phy_control(ioc, phy,
1722 SMP_PHY_CONTROL_LINK_RESET);
1723 }
1724
1725 /* handle hba phys */
1726
1727 /* sas_iounit page 1 */
1728 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
1729 sizeof(Mpi2SasIOUnit1PhyData_t));
1730 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
1731 if (!sas_iounit_pg1) {
1732 ioc_err(ioc, "failure at %s:%d/%s()!\n",
1733 __FILE__, __LINE__, __func__);
1734 rc = -ENOMEM;
1735 goto out;
1736 }
1737 if ((mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
1738 sas_iounit_pg1, sz))) {
1739 ioc_err(ioc, "failure at %s:%d/%s()!\n",
1740 __FILE__, __LINE__, __func__);
1741 rc = -ENXIO;
1742 goto out;
1743 }
1744 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1745 MPI2_IOCSTATUS_MASK;
1746 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1747 ioc_err(ioc, "failure at %s:%d/%s()!\n",
1748 __FILE__, __LINE__, __func__);
1749 rc = -EIO;
1750 goto out;
1751 }
1752
1753 for (i = 0; i < ioc->sas_hba.num_phys; i++) {
1754 if (phy->number != i) {
1755 sas_iounit_pg1->PhyData[i].MaxMinLinkRate =
1756 (ioc->sas_hba.phy[i].phy->minimum_linkrate +
1757 (ioc->sas_hba.phy[i].phy->maximum_linkrate << 4));
1758 } else {
1759 sas_iounit_pg1->PhyData[i].MaxMinLinkRate =
1760 (rates->minimum_linkrate +
1761 (rates->maximum_linkrate << 4));
1762 }
1763 }
1764
1765 if (mpt3sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1,
1766 sz)) {
1767 ioc_err(ioc, "failure at %s:%d/%s()!\n",
1768 __FILE__, __LINE__, __func__);
1769 rc = -ENXIO;
1770 goto out;
1771 }
1772
1773 /* link reset */
1774 _transport_phy_reset(phy, 0);
1775
1776 /* read phy page 0, then update the rates in the sas transport phy */
1777 if (!mpt3sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
1778 phy->number)) {
1779 phy->minimum_linkrate = _transport_convert_phy_link_rate(
1780 phy_pg0.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
1781 phy->maximum_linkrate = _transport_convert_phy_link_rate(
1782 phy_pg0.ProgrammedLinkRate >> 4);
1783 phy->negotiated_linkrate = _transport_convert_phy_link_rate(
1784 phy_pg0.NegotiatedLinkRate &
1785 MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
1786 }
1787
1788 out:
1789 kfree(sas_iounit_pg1);
1790 return rc;
1791 }
1792
1793 static int
_transport_map_smp_buffer(struct device * dev,struct bsg_buffer * buf,dma_addr_t * dma_addr,size_t * dma_len,void ** p)1794 _transport_map_smp_buffer(struct device *dev, struct bsg_buffer *buf,
1795 dma_addr_t *dma_addr, size_t *dma_len, void **p)
1796 {
1797 /* Check if the request is split across multiple segments */
1798 if (buf->sg_cnt > 1) {
1799 *p = dma_alloc_coherent(dev, buf->payload_len, dma_addr,
1800 GFP_KERNEL);
1801 if (!*p)
1802 return -ENOMEM;
1803 *dma_len = buf->payload_len;
1804 } else {
1805 if (!dma_map_sg(dev, buf->sg_list, 1, DMA_BIDIRECTIONAL))
1806 return -ENOMEM;
1807 *dma_addr = sg_dma_address(buf->sg_list);
1808 *dma_len = sg_dma_len(buf->sg_list);
1809 *p = NULL;
1810 }
1811
1812 return 0;
1813 }
1814
1815 static void
_transport_unmap_smp_buffer(struct device * dev,struct bsg_buffer * buf,dma_addr_t dma_addr,void * p)1816 _transport_unmap_smp_buffer(struct device *dev, struct bsg_buffer *buf,
1817 dma_addr_t dma_addr, void *p)
1818 {
1819 if (p)
1820 dma_free_coherent(dev, buf->payload_len, p, dma_addr);
1821 else
1822 dma_unmap_sg(dev, buf->sg_list, 1, DMA_BIDIRECTIONAL);
1823 }
1824
1825 /**
1826 * _transport_smp_handler - transport portal for smp passthru
1827 * @job: ?
1828 * @shost: shost object
1829 * @rphy: sas transport rphy object
1830 *
1831 * This used primarily for smp_utils.
1832 * Example:
1833 * smp_rep_general /sys/class/bsg/expander-5:0
1834 */
1835 static void
_transport_smp_handler(struct bsg_job * job,struct Scsi_Host * shost,struct sas_rphy * rphy)1836 _transport_smp_handler(struct bsg_job *job, struct Scsi_Host *shost,
1837 struct sas_rphy *rphy)
1838 {
1839 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1840 Mpi2SmpPassthroughRequest_t *mpi_request;
1841 Mpi2SmpPassthroughReply_t *mpi_reply;
1842 int rc;
1843 u16 smid;
1844 void *psge;
1845 dma_addr_t dma_addr_in;
1846 dma_addr_t dma_addr_out;
1847 void *addr_in = NULL;
1848 void *addr_out = NULL;
1849 size_t dma_len_in;
1850 size_t dma_len_out;
1851 unsigned int reslen = 0;
1852
1853 if (ioc->shost_recovery || ioc->pci_error_recovery) {
1854 ioc_info(ioc, "%s: host reset in progress!\n", __func__);
1855 rc = -EFAULT;
1856 goto job_done;
1857 }
1858
1859 rc = mutex_lock_interruptible(&ioc->transport_cmds.mutex);
1860 if (rc)
1861 goto job_done;
1862
1863 if (ioc->transport_cmds.status != MPT3_CMD_NOT_USED) {
1864 ioc_err(ioc, "%s: transport_cmds in use\n",
1865 __func__);
1866 rc = -EAGAIN;
1867 goto out;
1868 }
1869 ioc->transport_cmds.status = MPT3_CMD_PENDING;
1870
1871 rc = _transport_map_smp_buffer(&ioc->pdev->dev, &job->request_payload,
1872 &dma_addr_out, &dma_len_out, &addr_out);
1873 if (rc)
1874 goto out;
1875 if (addr_out) {
1876 sg_copy_to_buffer(job->request_payload.sg_list,
1877 job->request_payload.sg_cnt, addr_out,
1878 job->request_payload.payload_len);
1879 }
1880
1881 rc = _transport_map_smp_buffer(&ioc->pdev->dev, &job->reply_payload,
1882 &dma_addr_in, &dma_len_in, &addr_in);
1883 if (rc)
1884 goto unmap_out;
1885
1886 rc = mpt3sas_wait_for_ioc(ioc, IOC_OPERATIONAL_WAIT_COUNT);
1887 if (rc)
1888 goto unmap_in;
1889
1890 smid = mpt3sas_base_get_smid(ioc, ioc->transport_cb_idx);
1891 if (!smid) {
1892 ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
1893 rc = -EAGAIN;
1894 goto unmap_in;
1895 }
1896
1897 rc = 0;
1898 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
1899 ioc->transport_cmds.smid = smid;
1900
1901 memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
1902 mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
1903 mpi_request->PhysicalPort = 0xFF;
1904 mpi_request->SASAddress = (rphy) ?
1905 cpu_to_le64(rphy->identify.sas_address) :
1906 cpu_to_le64(ioc->sas_hba.sas_address);
1907 mpi_request->RequestDataLength = cpu_to_le16(dma_len_out - 4);
1908 psge = &mpi_request->SGL;
1909
1910 ioc->build_sg(ioc, psge, dma_addr_out, dma_len_out - 4, dma_addr_in,
1911 dma_len_in - 4);
1912
1913 dtransportprintk(ioc,
1914 ioc_info(ioc, "%s: sending smp request\n", __func__));
1915
1916 init_completion(&ioc->transport_cmds.done);
1917 ioc->put_smid_default(ioc, smid);
1918 wait_for_completion_timeout(&ioc->transport_cmds.done, 10*HZ);
1919
1920 if (!(ioc->transport_cmds.status & MPT3_CMD_COMPLETE)) {
1921 ioc_err(ioc, "%s: timeout\n", __func__);
1922 _debug_dump_mf(mpi_request,
1923 sizeof(Mpi2SmpPassthroughRequest_t)/4);
1924 if (!(ioc->transport_cmds.status & MPT3_CMD_RESET)) {
1925 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
1926 rc = -ETIMEDOUT;
1927 goto unmap_in;
1928 }
1929 }
1930
1931 dtransportprintk(ioc, ioc_info(ioc, "%s - complete\n", __func__));
1932
1933 if (!(ioc->transport_cmds.status & MPT3_CMD_REPLY_VALID)) {
1934 dtransportprintk(ioc,
1935 ioc_info(ioc, "%s: no reply\n", __func__));
1936 rc = -ENXIO;
1937 goto unmap_in;
1938 }
1939
1940 mpi_reply = ioc->transport_cmds.reply;
1941
1942 dtransportprintk(ioc,
1943 ioc_info(ioc, "%s: reply data transfer size(%d)\n",
1944 __func__,
1945 le16_to_cpu(mpi_reply->ResponseDataLength)));
1946
1947 memcpy(job->reply, mpi_reply, sizeof(*mpi_reply));
1948 job->reply_len = sizeof(*mpi_reply);
1949 reslen = le16_to_cpu(mpi_reply->ResponseDataLength);
1950
1951 if (addr_in) {
1952 sg_copy_to_buffer(job->reply_payload.sg_list,
1953 job->reply_payload.sg_cnt, addr_in,
1954 job->reply_payload.payload_len);
1955 }
1956
1957 rc = 0;
1958 unmap_in:
1959 _transport_unmap_smp_buffer(&ioc->pdev->dev, &job->reply_payload,
1960 dma_addr_in, addr_in);
1961 unmap_out:
1962 _transport_unmap_smp_buffer(&ioc->pdev->dev, &job->request_payload,
1963 dma_addr_out, addr_out);
1964 out:
1965 ioc->transport_cmds.status = MPT3_CMD_NOT_USED;
1966 mutex_unlock(&ioc->transport_cmds.mutex);
1967 job_done:
1968 bsg_job_done(job, rc, reslen);
1969 }
1970
1971 struct sas_function_template mpt3sas_transport_functions = {
1972 .get_linkerrors = _transport_get_linkerrors,
1973 .get_enclosure_identifier = _transport_get_enclosure_identifier,
1974 .get_bay_identifier = _transport_get_bay_identifier,
1975 .phy_reset = _transport_phy_reset,
1976 .phy_enable = _transport_phy_enable,
1977 .set_phy_speed = _transport_phy_speed,
1978 .smp_handler = _transport_smp_handler,
1979 };
1980
1981 struct scsi_transport_template *mpt3sas_transport_template;
1982