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