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