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