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