blob: 7143e86af326ec61c7a46d21cd9082a88848e9a5 [file] [log] [blame]
Eric Moore635374e2009-03-09 01:21:12 -06001/*
2 * SAS Transport Layer for MPT (Message Passing Technology) based controllers
3 *
4 * This code is based on drivers/scsi/mpt2sas/mpt2_transport.c
Sreekanth Reddyd648d5a2013-07-25 11:28:01 +05305 * Copyright (C) 2007-2013 LSI Corporation
Eric Moore635374e2009-03-09 01:21:12 -06006 * (mailto:DL-MPTFusionLinux@lsi.com)
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * NO WARRANTY
19 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
20 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
21 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
22 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
23 * solely responsible for determining the appropriateness of using and
24 * distributing the Program and assumes all risks associated with its
25 * exercise of rights under this Agreement, including but not limited to
26 * the risks and costs of program errors, damage to or loss of data,
27 * programs or equipment, and unavailability or interruption of operations.
28
29 * DISCLAIMER OF LIABILITY
30 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
31 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
33 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
35 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
36 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
37
38 * You should have received a copy of the GNU General Public License
39 * along with this program; if not, write to the Free Software
40 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
41 * USA.
42 */
43
44#include <linux/module.h>
45#include <linux/kernel.h>
46#include <linux/init.h>
47#include <linux/errno.h>
48#include <linux/sched.h>
49#include <linux/workqueue.h>
50#include <linux/delay.h>
51#include <linux/pci.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090052#include <linux/slab.h>
Eric Moore635374e2009-03-09 01:21:12 -060053
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 "mpt2sas_base.h"
62/**
Kashyap, Desaic5e039b2009-09-23 17:21:29 +053063 * _transport_sas_node_find_by_sas_address - sas node search
Eric Moore635374e2009-03-09 01:21:12 -060064 * @ioc: per adapter object
Kashyap, Desaic5e039b2009-09-23 17:21:29 +053065 * @sas_address: sas address of expander or sas host
Eric Moore635374e2009-03-09 01:21:12 -060066 * Context: Calling function should acquire ioc->sas_node_lock.
67 *
68 * Search for either hba phys or expander device based on handle, then returns
69 * the sas_node object.
70 */
71static struct _sas_node *
Kashyap, Desaic5e039b2009-09-23 17:21:29 +053072_transport_sas_node_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
73 u64 sas_address)
Eric Moore635374e2009-03-09 01:21:12 -060074{
Kashyap, Desaic5e039b2009-09-23 17:21:29 +053075 if (ioc->sas_hba.sas_address == sas_address)
76 return &ioc->sas_hba;
77 else
78 return mpt2sas_scsih_expander_find_by_sas_address(ioc,
79 sas_address);
Eric Moore635374e2009-03-09 01:21:12 -060080}
81
82/**
83 * _transport_convert_phy_link_rate -
84 * @link_rate: link rate returned from mpt firmware
85 *
86 * Convert link_rate from mpi fusion into sas_transport form.
87 */
88static enum sas_linkrate
89_transport_convert_phy_link_rate(u8 link_rate)
90{
91 enum sas_linkrate rc;
92
93 switch (link_rate) {
94 case MPI2_SAS_NEG_LINK_RATE_1_5:
95 rc = SAS_LINK_RATE_1_5_GBPS;
96 break;
97 case MPI2_SAS_NEG_LINK_RATE_3_0:
98 rc = SAS_LINK_RATE_3_0_GBPS;
99 break;
100 case MPI2_SAS_NEG_LINK_RATE_6_0:
101 rc = SAS_LINK_RATE_6_0_GBPS;
102 break;
103 case MPI2_SAS_NEG_LINK_RATE_PHY_DISABLED:
104 rc = SAS_PHY_DISABLED;
105 break;
106 case MPI2_SAS_NEG_LINK_RATE_NEGOTIATION_FAILED:
107 rc = SAS_LINK_RATE_FAILED;
108 break;
109 case MPI2_SAS_NEG_LINK_RATE_PORT_SELECTOR:
110 rc = SAS_SATA_PORT_SELECTOR;
111 break;
112 case MPI2_SAS_NEG_LINK_RATE_SMP_RESET_IN_PROGRESS:
113 rc = SAS_PHY_RESET_IN_PROGRESS;
114 break;
115 default:
116 case MPI2_SAS_NEG_LINK_RATE_SATA_OOB_COMPLETE:
117 case MPI2_SAS_NEG_LINK_RATE_UNKNOWN_LINK_RATE:
118 rc = SAS_LINK_RATE_UNKNOWN;
119 break;
120 }
121 return rc;
122}
123
124/**
125 * _transport_set_identify - set identify for phys and end devices
126 * @ioc: per adapter object
127 * @handle: device handle
128 * @identify: sas identify info
129 *
130 * Populates sas identify info.
131 *
132 * Returns 0 for success, non-zero for failure.
133 */
134static int
135_transport_set_identify(struct MPT2SAS_ADAPTER *ioc, u16 handle,
136 struct sas_identify *identify)
137{
138 Mpi2SasDevicePage0_t sas_device_pg0;
139 Mpi2ConfigReply_t mpi_reply;
140 u32 device_info;
141 u32 ioc_status;
142
Eric Moore3cb54692010-07-08 14:44:34 -0600143 if (ioc->shost_recovery || ioc->pci_error_recovery) {
Kashyap, Desai155dd4c2009-08-20 13:22:00 +0530144 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
145 __func__, ioc->name);
146 return -EFAULT;
147 }
148
Eric Moore635374e2009-03-09 01:21:12 -0600149 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
150 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
151 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
Kashyap, Desai155dd4c2009-08-20 13:22:00 +0530152
Eric Moore635374e2009-03-09 01:21:12 -0600153 ioc->name, __FILE__, __LINE__, __func__);
Kashyap, Desai155dd4c2009-08-20 13:22:00 +0530154 return -ENXIO;
Eric Moore635374e2009-03-09 01:21:12 -0600155 }
156
157 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
158 MPI2_IOCSTATUS_MASK;
159 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
160 printk(MPT2SAS_ERR_FMT "handle(0x%04x), ioc_status(0x%04x)"
161 "\nfailure at %s:%d/%s()!\n", ioc->name, handle, ioc_status,
162 __FILE__, __LINE__, __func__);
Kashyap, Desai155dd4c2009-08-20 13:22:00 +0530163 return -EIO;
Eric Moore635374e2009-03-09 01:21:12 -0600164 }
165
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +0530166 memset(identify, 0, sizeof(struct sas_identify));
Eric Moore635374e2009-03-09 01:21:12 -0600167 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
168
169 /* sas_address */
170 identify->sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
171
nagalakshmi.nandigama@lsi.com64bb8132012-03-20 12:08:40 +0530172 /* phy number of the parent device this device is linked to */
173 identify->phy_identifier = sas_device_pg0.PhyNum;
174
Eric Moore635374e2009-03-09 01:21:12 -0600175 /* device_type */
176 switch (device_info & MPI2_SAS_DEVICE_INFO_MASK_DEVICE_TYPE) {
177 case MPI2_SAS_DEVICE_INFO_NO_DEVICE:
178 identify->device_type = SAS_PHY_UNUSED;
179 break;
180 case MPI2_SAS_DEVICE_INFO_END_DEVICE:
181 identify->device_type = SAS_END_DEVICE;
182 break;
183 case MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER:
184 identify->device_type = SAS_EDGE_EXPANDER_DEVICE;
185 break;
186 case MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER:
187 identify->device_type = SAS_FANOUT_EXPANDER_DEVICE;
188 break;
189 }
190
191 /* initiator_port_protocols */
192 if (device_info & MPI2_SAS_DEVICE_INFO_SSP_INITIATOR)
193 identify->initiator_port_protocols |= SAS_PROTOCOL_SSP;
194 if (device_info & MPI2_SAS_DEVICE_INFO_STP_INITIATOR)
195 identify->initiator_port_protocols |= SAS_PROTOCOL_STP;
196 if (device_info & MPI2_SAS_DEVICE_INFO_SMP_INITIATOR)
197 identify->initiator_port_protocols |= SAS_PROTOCOL_SMP;
198 if (device_info & MPI2_SAS_DEVICE_INFO_SATA_HOST)
199 identify->initiator_port_protocols |= SAS_PROTOCOL_SATA;
200
201 /* target_port_protocols */
202 if (device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET)
203 identify->target_port_protocols |= SAS_PROTOCOL_SSP;
204 if (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET)
205 identify->target_port_protocols |= SAS_PROTOCOL_STP;
206 if (device_info & MPI2_SAS_DEVICE_INFO_SMP_TARGET)
207 identify->target_port_protocols |= SAS_PROTOCOL_SMP;
208 if (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
209 identify->target_port_protocols |= SAS_PROTOCOL_SATA;
210
211 return 0;
212}
213
214/**
215 * mpt2sas_transport_done - internal transport layer callback handler.
216 * @ioc: per adapter object
217 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530218 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -0600219 * @reply: reply message frame(lower 32bit addr)
220 *
221 * Callback handler when sending internal generated transport cmds.
222 * The callback index passed is `ioc->transport_cb_idx`
223 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530224 * Return 1 meaning mf should be freed from _base_interrupt
225 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -0600226 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530227u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530228mpt2sas_transport_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
Eric Moore635374e2009-03-09 01:21:12 -0600229 u32 reply)
230{
231 MPI2DefaultReply_t *mpi_reply;
232
233 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
234 if (ioc->transport_cmds.status == MPT2_CMD_NOT_USED)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530235 return 1;
Eric Moore635374e2009-03-09 01:21:12 -0600236 if (ioc->transport_cmds.smid != smid)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530237 return 1;
Eric Moore635374e2009-03-09 01:21:12 -0600238 ioc->transport_cmds.status |= MPT2_CMD_COMPLETE;
239 if (mpi_reply) {
240 memcpy(ioc->transport_cmds.reply, mpi_reply,
241 mpi_reply->MsgLength*4);
242 ioc->transport_cmds.status |= MPT2_CMD_REPLY_VALID;
243 }
244 ioc->transport_cmds.status &= ~MPT2_CMD_PENDING;
245 complete(&ioc->transport_cmds.done);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530246 return 1;
Eric Moore635374e2009-03-09 01:21:12 -0600247}
248
249/* report manufacture request structure */
250struct rep_manu_request{
251 u8 smp_frame_type;
252 u8 function;
253 u8 reserved;
254 u8 request_length;
255};
256
257/* report manufacture reply structure */
258struct rep_manu_reply{
259 u8 smp_frame_type; /* 0x41 */
260 u8 function; /* 0x01 */
261 u8 function_result;
262 u8 response_length;
263 u16 expander_change_count;
264 u8 reserved0[2];
Kashyap, Desai463217b2009-10-05 15:53:06 +0530265 u8 sas_format;
Eric Moore635374e2009-03-09 01:21:12 -0600266 u8 reserved2[3];
267 u8 vendor_id[SAS_EXPANDER_VENDOR_ID_LEN];
268 u8 product_id[SAS_EXPANDER_PRODUCT_ID_LEN];
269 u8 product_rev[SAS_EXPANDER_PRODUCT_REV_LEN];
270 u8 component_vendor_id[SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN];
271 u16 component_id;
272 u8 component_revision_id;
273 u8 reserved3;
274 u8 vendor_specific[8];
275};
276
277/**
Eric Moored5d135b2009-05-18 13:02:08 -0600278 * _transport_expander_report_manufacture - obtain SMP report_manufacture
Eric Moore635374e2009-03-09 01:21:12 -0600279 * @ioc: per adapter object
280 * @sas_address: expander sas address
281 * @edev: the sas_expander_device object
282 *
283 * Fills in the sas_expander_device object when SMP port is created.
284 *
285 * Returns 0 for success, non-zero for failure.
286 */
287static int
Eric Moored5d135b2009-05-18 13:02:08 -0600288_transport_expander_report_manufacture(struct MPT2SAS_ADAPTER *ioc,
Eric Moore635374e2009-03-09 01:21:12 -0600289 u64 sas_address, struct sas_expander_device *edev)
290{
291 Mpi2SmpPassthroughRequest_t *mpi_request;
292 Mpi2SmpPassthroughReply_t *mpi_reply;
293 struct rep_manu_reply *manufacture_reply;
294 struct rep_manu_request *manufacture_request;
295 int rc;
296 u16 smid;
297 u32 ioc_state;
298 unsigned long timeleft;
299 void *psge;
300 u32 sgl_flags;
301 u8 issue_reset = 0;
Eric Moore635374e2009-03-09 01:21:12 -0600302 void *data_out = NULL;
303 dma_addr_t data_out_dma;
304 u32 sz;
Eric Moore635374e2009-03-09 01:21:12 -0600305 u16 wait_state_count;
306
Eric Moore3cb54692010-07-08 14:44:34 -0600307 if (ioc->shost_recovery || ioc->pci_error_recovery) {
Eric Moore635374e2009-03-09 01:21:12 -0600308 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
309 __func__, ioc->name);
310 return -EFAULT;
311 }
Eric Moore635374e2009-03-09 01:21:12 -0600312
313 mutex_lock(&ioc->transport_cmds.mutex);
314
315 if (ioc->transport_cmds.status != MPT2_CMD_NOT_USED) {
316 printk(MPT2SAS_ERR_FMT "%s: transport_cmds in use\n",
317 ioc->name, __func__);
318 rc = -EAGAIN;
319 goto out;
320 }
321 ioc->transport_cmds.status = MPT2_CMD_PENDING;
322
323 wait_state_count = 0;
324 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
325 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
326 if (wait_state_count++ == 10) {
327 printk(MPT2SAS_ERR_FMT
328 "%s: failed due to ioc not operational\n",
329 ioc->name, __func__);
330 rc = -EFAULT;
331 goto out;
332 }
333 ssleep(1);
334 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
335 printk(MPT2SAS_INFO_FMT "%s: waiting for "
336 "operational state(count=%d)\n", ioc->name,
337 __func__, wait_state_count);
338 }
339 if (wait_state_count)
340 printk(MPT2SAS_INFO_FMT "%s: ioc is operational\n",
341 ioc->name, __func__);
342
343 smid = mpt2sas_base_get_smid(ioc, ioc->transport_cb_idx);
344 if (!smid) {
345 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
346 ioc->name, __func__);
347 rc = -EAGAIN;
348 goto out;
349 }
350
351 rc = 0;
352 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
353 ioc->transport_cmds.smid = smid;
354
355 sz = sizeof(struct rep_manu_request) + sizeof(struct rep_manu_reply);
356 data_out = pci_alloc_consistent(ioc->pdev, sz, &data_out_dma);
357
358 if (!data_out) {
359 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
360 __LINE__, __func__);
361 rc = -ENOMEM;
362 mpt2sas_base_free_smid(ioc, smid);
363 goto out;
364 }
365
366 manufacture_request = data_out;
367 manufacture_request->smp_frame_type = 0x40;
368 manufacture_request->function = 1;
369 manufacture_request->reserved = 0;
370 manufacture_request->request_length = 0;
371
372 memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
373 mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
374 mpi_request->PhysicalPort = 0xFF;
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530375 mpi_request->VF_ID = 0; /* TODO */
376 mpi_request->VP_ID = 0;
Kashyap, Desaic97951e2011-06-14 10:54:56 +0530377 mpi_request->SASAddress = cpu_to_le64(sas_address);
Kashyap, Desai463217b2009-10-05 15:53:06 +0530378 mpi_request->RequestDataLength =
379 cpu_to_le16(sizeof(struct rep_manu_request));
Eric Moore635374e2009-03-09 01:21:12 -0600380 psge = &mpi_request->SGL;
381
382 /* WRITE sgel first */
383 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
384 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
385 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
386 ioc->base_add_sg_single(psge, sgl_flags |
387 sizeof(struct rep_manu_request), data_out_dma);
388
389 /* incr sgel */
390 psge += ioc->sge_size;
391
392 /* READ sgel last */
393 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
394 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
395 MPI2_SGE_FLAGS_END_OF_LIST);
396 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
397 ioc->base_add_sg_single(psge, sgl_flags |
398 sizeof(struct rep_manu_reply), data_out_dma +
399 sizeof(struct rep_manu_request));
400
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530401 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "report_manufacture - "
Eric Moore635374e2009-03-09 01:21:12 -0600402 "send to sas_addr(0x%016llx)\n", ioc->name,
403 (unsigned long long)sas_address));
Kashyap, Desaibcfb6e62009-09-14 11:05:24 +0530404 init_completion(&ioc->transport_cmds.done);
nagalakshmi.nandigama@lsi.comf01690d2011-12-01 07:43:58 +0530405 mpt2sas_base_put_smid_default(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -0600406 timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done,
407 10*HZ);
408
409 if (!(ioc->transport_cmds.status & MPT2_CMD_COMPLETE)) {
410 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
411 ioc->name, __func__);
412 _debug_dump_mf(mpi_request,
413 sizeof(Mpi2SmpPassthroughRequest_t)/4);
414 if (!(ioc->transport_cmds.status & MPT2_CMD_RESET))
415 issue_reset = 1;
416 goto issue_host_reset;
417 }
418
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530419 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "report_manufacture - "
Eric Moore635374e2009-03-09 01:21:12 -0600420 "complete\n", ioc->name));
421
422 if (ioc->transport_cmds.status & MPT2_CMD_REPLY_VALID) {
423 u8 *tmp;
424
425 mpi_reply = ioc->transport_cmds.reply;
426
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530427 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -0600428 "report_manufacture - reply data transfer size(%d)\n",
429 ioc->name, le16_to_cpu(mpi_reply->ResponseDataLength)));
430
431 if (le16_to_cpu(mpi_reply->ResponseDataLength) !=
432 sizeof(struct rep_manu_reply))
433 goto out;
434
435 manufacture_reply = data_out + sizeof(struct rep_manu_request);
436 strncpy(edev->vendor_id, manufacture_reply->vendor_id,
437 SAS_EXPANDER_VENDOR_ID_LEN);
438 strncpy(edev->product_id, manufacture_reply->product_id,
439 SAS_EXPANDER_PRODUCT_ID_LEN);
440 strncpy(edev->product_rev, manufacture_reply->product_rev,
441 SAS_EXPANDER_PRODUCT_REV_LEN);
Kashyap, Desai463217b2009-10-05 15:53:06 +0530442 edev->level = manufacture_reply->sas_format & 1;
443 if (edev->level) {
Eric Moore635374e2009-03-09 01:21:12 -0600444 strncpy(edev->component_vendor_id,
445 manufacture_reply->component_vendor_id,
446 SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN);
447 tmp = (u8 *)&manufacture_reply->component_id;
448 edev->component_id = tmp[0] << 8 | tmp[1];
449 edev->component_revision_id =
450 manufacture_reply->component_revision_id;
451 }
452 } else
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530453 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -0600454 "report_manufacture - no reply\n", ioc->name));
455
456 issue_host_reset:
457 if (issue_reset)
458 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
459 FORCE_BIG_HAMMER);
460 out:
461 ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
462 if (data_out)
463 pci_free_consistent(ioc->pdev, sz, data_out, data_out_dma);
464
465 mutex_unlock(&ioc->transport_cmds.mutex);
466 return rc;
467}
468
Kashyap, Desai38c29112010-04-05 14:18:34 +0530469/**
Kashyap, Desai7f6f7942010-11-13 04:35:30 +0530470 * _transport_delete_port - helper function to removing a port
Kashyap, Desai38c29112010-04-05 14:18:34 +0530471 * @ioc: per adapter object
Kashyap, Desai7f6f7942010-11-13 04:35:30 +0530472 * @mpt2sas_port: mpt2sas per port object
Kashyap, Desai38c29112010-04-05 14:18:34 +0530473 *
Kashyap, Desai7f6f7942010-11-13 04:35:30 +0530474 * Returns nothing.
Kashyap, Desai38c29112010-04-05 14:18:34 +0530475 */
476static void
Kashyap, Desai7f6f7942010-11-13 04:35:30 +0530477_transport_delete_port(struct MPT2SAS_ADAPTER *ioc,
478 struct _sas_port *mpt2sas_port)
Kashyap, Desai38c29112010-04-05 14:18:34 +0530479{
Kashyap, Desai7f6f7942010-11-13 04:35:30 +0530480 u64 sas_address = mpt2sas_port->remote_identify.sas_address;
481 enum sas_device_type device_type =
482 mpt2sas_port->remote_identify.device_type;
Kashyap, Desai38c29112010-04-05 14:18:34 +0530483
Kashyap, Desai7f6f7942010-11-13 04:35:30 +0530484 dev_printk(KERN_INFO, &mpt2sas_port->port->dev,
485 "remove: sas_addr(0x%016llx)\n",
486 (unsigned long long) sas_address);
Kashyap, Desai38c29112010-04-05 14:18:34 +0530487
Kashyap, Desai7f6f7942010-11-13 04:35:30 +0530488 ioc->logging_level |= MPT_DEBUG_TRANSPORT;
489 if (device_type == SAS_END_DEVICE)
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +0530490 mpt2sas_device_remove_by_sas_address(ioc, sas_address);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +0530491 else if (device_type == SAS_EDGE_EXPANDER_DEVICE ||
492 device_type == SAS_FANOUT_EXPANDER_DEVICE)
493 mpt2sas_expander_remove(ioc, sas_address);
494 ioc->logging_level &= ~MPT_DEBUG_TRANSPORT;
495}
Kashyap, Desai38c29112010-04-05 14:18:34 +0530496
Kashyap, Desai7f6f7942010-11-13 04:35:30 +0530497/**
498 * _transport_delete_phy - helper function to removing single phy from port
499 * @ioc: per adapter object
500 * @mpt2sas_port: mpt2sas per port object
501 * @mpt2sas_phy: mpt2sas per phy object
502 *
503 * Returns nothing.
504 */
505static void
506_transport_delete_phy(struct MPT2SAS_ADAPTER *ioc,
507 struct _sas_port *mpt2sas_port, struct _sas_phy *mpt2sas_phy)
508{
509 u64 sas_address = mpt2sas_port->remote_identify.sas_address;
510
511 dev_printk(KERN_INFO, &mpt2sas_phy->phy->dev,
512 "remove: sas_addr(0x%016llx), phy(%d)\n",
513 (unsigned long long) sas_address, mpt2sas_phy->phy_id);
514
515 list_del(&mpt2sas_phy->port_siblings);
516 mpt2sas_port->num_phys--;
517 sas_port_delete_phy(mpt2sas_port->port, mpt2sas_phy->phy);
518 mpt2sas_phy->phy_belongs_to_port = 0;
519}
520
521/**
522 * _transport_add_phy - helper function to adding single phy to port
523 * @ioc: per adapter object
524 * @mpt2sas_port: mpt2sas per port object
525 * @mpt2sas_phy: mpt2sas per phy object
526 *
527 * Returns nothing.
528 */
529static void
530_transport_add_phy(struct MPT2SAS_ADAPTER *ioc, struct _sas_port *mpt2sas_port,
531 struct _sas_phy *mpt2sas_phy)
532{
533 u64 sas_address = mpt2sas_port->remote_identify.sas_address;
534
535 dev_printk(KERN_INFO, &mpt2sas_phy->phy->dev,
536 "add: sas_addr(0x%016llx), phy(%d)\n", (unsigned long long)
537 sas_address, mpt2sas_phy->phy_id);
538
539 list_add_tail(&mpt2sas_phy->port_siblings, &mpt2sas_port->phy_list);
540 mpt2sas_port->num_phys++;
541 sas_port_add_phy(mpt2sas_port->port, mpt2sas_phy->phy);
542 mpt2sas_phy->phy_belongs_to_port = 1;
543}
544
545/**
546 * _transport_add_phy_to_an_existing_port - adding new phy to existing port
547 * @ioc: per adapter object
548 * @sas_node: sas node object (either expander or sas host)
549 * @mpt2sas_phy: mpt2sas per phy object
550 * @sas_address: sas address of device/expander were phy needs to be added to
551 *
552 * Returns nothing.
553 */
554static void
555_transport_add_phy_to_an_existing_port(struct MPT2SAS_ADAPTER *ioc,
556struct _sas_node *sas_node, struct _sas_phy *mpt2sas_phy, u64 sas_address)
557{
558 struct _sas_port *mpt2sas_port;
559 struct _sas_phy *phy_srch;
560
561 if (mpt2sas_phy->phy_belongs_to_port == 1)
Kashyap, Desai38c29112010-04-05 14:18:34 +0530562 return;
563
Kashyap, Desai7f6f7942010-11-13 04:35:30 +0530564 list_for_each_entry(mpt2sas_port, &sas_node->sas_port_list,
565 port_list) {
566 if (mpt2sas_port->remote_identify.sas_address !=
567 sas_address)
568 continue;
569 list_for_each_entry(phy_srch, &mpt2sas_port->phy_list,
570 port_siblings) {
571 if (phy_srch == mpt2sas_phy)
572 return;
573 }
574 _transport_add_phy(ioc, mpt2sas_port, mpt2sas_phy);
575 return;
576 }
577
578}
579
580/**
581 * _transport_del_phy_from_an_existing_port - delete phy from existing port
582 * @ioc: per adapter object
583 * @sas_node: sas node object (either expander or sas host)
584 * @mpt2sas_phy: mpt2sas per phy object
585 *
586 * Returns nothing.
587 */
588static void
589_transport_del_phy_from_an_existing_port(struct MPT2SAS_ADAPTER *ioc,
590 struct _sas_node *sas_node, struct _sas_phy *mpt2sas_phy)
591{
592 struct _sas_port *mpt2sas_port, *next;
593 struct _sas_phy *phy_srch;
594
595 if (mpt2sas_phy->phy_belongs_to_port == 0)
596 return;
597
598 list_for_each_entry_safe(mpt2sas_port, next, &sas_node->sas_port_list,
599 port_list) {
600 list_for_each_entry(phy_srch, &mpt2sas_port->phy_list,
601 port_siblings) {
602 if (phy_srch != mpt2sas_phy)
603 continue;
604 if (mpt2sas_port->num_phys == 1)
605 _transport_delete_port(ioc, mpt2sas_port);
606 else
607 _transport_delete_phy(ioc, mpt2sas_port,
608 mpt2sas_phy);
609 return;
610 }
611 }
Kashyap, Desai38c29112010-04-05 14:18:34 +0530612}
613
614/**
615 * _transport_sanity_check - sanity check when adding a new port
616 * @ioc: per adapter object
617 * @sas_node: sas node object (either expander or sas host)
618 * @sas_address: sas address of device being added
619 *
620 * See the explanation above from _transport_delete_duplicate_port
621 */
622static void
623_transport_sanity_check(struct MPT2SAS_ADAPTER *ioc, struct _sas_node *sas_node,
624 u64 sas_address)
625{
626 int i;
627
Kashyap, Desai7f6f7942010-11-13 04:35:30 +0530628 for (i = 0; i < sas_node->num_phys; i++) {
629 if (sas_node->phy[i].remote_identify.sas_address != sas_address)
630 continue;
631 if (sas_node->phy[i].phy_belongs_to_port == 1)
632 _transport_del_phy_from_an_existing_port(ioc, sas_node,
633 &sas_node->phy[i]);
634 }
Kashyap, Desai38c29112010-04-05 14:18:34 +0530635}
636
Eric Moore635374e2009-03-09 01:21:12 -0600637/**
638 * mpt2sas_transport_port_add - insert port to the list
639 * @ioc: per adapter object
640 * @handle: handle of attached device
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530641 * @sas_address: sas address of parent expander or sas host
Eric Moore635374e2009-03-09 01:21:12 -0600642 * Context: This function will acquire ioc->sas_node_lock.
643 *
644 * Adding new port object to the sas_node->sas_port_list.
645 *
646 * Returns mpt2sas_port.
647 */
648struct _sas_port *
649mpt2sas_transport_port_add(struct MPT2SAS_ADAPTER *ioc, u16 handle,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530650 u64 sas_address)
Eric Moore635374e2009-03-09 01:21:12 -0600651{
652 struct _sas_phy *mpt2sas_phy, *next;
653 struct _sas_port *mpt2sas_port;
654 unsigned long flags;
655 struct _sas_node *sas_node;
656 struct sas_rphy *rphy;
657 int i;
658 struct sas_port *port;
659
Eric Moore635374e2009-03-09 01:21:12 -0600660 mpt2sas_port = kzalloc(sizeof(struct _sas_port),
661 GFP_KERNEL);
662 if (!mpt2sas_port) {
663 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
664 ioc->name, __FILE__, __LINE__, __func__);
665 return NULL;
666 }
667
668 INIT_LIST_HEAD(&mpt2sas_port->port_list);
669 INIT_LIST_HEAD(&mpt2sas_port->phy_list);
670 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530671 sas_node = _transport_sas_node_find_by_sas_address(ioc, sas_address);
Eric Moore635374e2009-03-09 01:21:12 -0600672 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
673
674 if (!sas_node) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530675 printk(MPT2SAS_ERR_FMT "%s: Could not find "
676 "parent sas_address(0x%016llx)!\n", ioc->name,
677 __func__, (unsigned long long)sas_address);
Eric Moore635374e2009-03-09 01:21:12 -0600678 goto out_fail;
679 }
680
Eric Moore635374e2009-03-09 01:21:12 -0600681 if ((_transport_set_identify(ioc, handle,
682 &mpt2sas_port->remote_identify))) {
683 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
684 ioc->name, __FILE__, __LINE__, __func__);
685 goto out_fail;
686 }
687
688 if (mpt2sas_port->remote_identify.device_type == SAS_PHY_UNUSED) {
689 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
690 ioc->name, __FILE__, __LINE__, __func__);
691 goto out_fail;
692 }
693
Kashyap, Desai38c29112010-04-05 14:18:34 +0530694 _transport_sanity_check(ioc, sas_node,
695 mpt2sas_port->remote_identify.sas_address);
696
Eric Moore635374e2009-03-09 01:21:12 -0600697 for (i = 0; i < sas_node->num_phys; i++) {
698 if (sas_node->phy[i].remote_identify.sas_address !=
699 mpt2sas_port->remote_identify.sas_address)
700 continue;
701 list_add_tail(&sas_node->phy[i].port_siblings,
702 &mpt2sas_port->phy_list);
703 mpt2sas_port->num_phys++;
704 }
705
706 if (!mpt2sas_port->num_phys) {
707 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
708 ioc->name, __FILE__, __LINE__, __func__);
709 goto out_fail;
710 }
711
712 port = sas_port_alloc_num(sas_node->parent_dev);
713 if ((sas_port_add(port))) {
714 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
715 ioc->name, __FILE__, __LINE__, __func__);
716 goto out_fail;
717 }
718
719 list_for_each_entry(mpt2sas_phy, &mpt2sas_port->phy_list,
720 port_siblings) {
721 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
722 dev_printk(KERN_INFO, &port->dev, "add: handle(0x%04x)"
723 ", sas_addr(0x%016llx), phy(%d)\n", handle,
724 (unsigned long long)
725 mpt2sas_port->remote_identify.sas_address,
726 mpt2sas_phy->phy_id);
727 sas_port_add_phy(port, mpt2sas_phy->phy);
Kashyap, Desai38c29112010-04-05 14:18:34 +0530728 mpt2sas_phy->phy_belongs_to_port = 1;
Eric Moore635374e2009-03-09 01:21:12 -0600729 }
730
731 mpt2sas_port->port = port;
732 if (mpt2sas_port->remote_identify.device_type == SAS_END_DEVICE)
733 rphy = sas_end_device_alloc(port);
734 else
735 rphy = sas_expander_alloc(port,
736 mpt2sas_port->remote_identify.device_type);
737
738 rphy->identify = mpt2sas_port->remote_identify;
739 if ((sas_rphy_add(rphy))) {
740 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
741 ioc->name, __FILE__, __LINE__, __func__);
742 }
743 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
744 dev_printk(KERN_INFO, &rphy->dev, "add: handle(0x%04x), "
745 "sas_addr(0x%016llx)\n", handle,
746 (unsigned long long)
747 mpt2sas_port->remote_identify.sas_address);
748 mpt2sas_port->rphy = rphy;
749 spin_lock_irqsave(&ioc->sas_node_lock, flags);
750 list_add_tail(&mpt2sas_port->port_list, &sas_node->sas_port_list);
751 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
752
753 /* fill in report manufacture */
754 if (mpt2sas_port->remote_identify.device_type ==
755 MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
756 mpt2sas_port->remote_identify.device_type ==
757 MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER)
Eric Moored5d135b2009-05-18 13:02:08 -0600758 _transport_expander_report_manufacture(ioc,
Eric Moore635374e2009-03-09 01:21:12 -0600759 mpt2sas_port->remote_identify.sas_address,
760 rphy_to_expander_device(rphy));
761
762 return mpt2sas_port;
763
764 out_fail:
765 list_for_each_entry_safe(mpt2sas_phy, next, &mpt2sas_port->phy_list,
766 port_siblings)
767 list_del(&mpt2sas_phy->port_siblings);
768 kfree(mpt2sas_port);
769 return NULL;
770}
771
772/**
773 * mpt2sas_transport_port_remove - remove port from the list
774 * @ioc: per adapter object
775 * @sas_address: sas address of attached device
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530776 * @sas_address_parent: sas address of parent expander or sas host
Eric Moore635374e2009-03-09 01:21:12 -0600777 * Context: This function will acquire ioc->sas_node_lock.
778 *
779 * Removing object and freeing associated memory from the
780 * ioc->sas_port_list.
781 *
782 * Return nothing.
783 */
784void
785mpt2sas_transport_port_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530786 u64 sas_address_parent)
Eric Moore635374e2009-03-09 01:21:12 -0600787{
788 int i;
789 unsigned long flags;
790 struct _sas_port *mpt2sas_port, *next;
791 struct _sas_node *sas_node;
792 u8 found = 0;
793 struct _sas_phy *mpt2sas_phy, *next_phy;
794
795 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530796 sas_node = _transport_sas_node_find_by_sas_address(ioc,
797 sas_address_parent);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +0530798 if (!sas_node) {
799 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -0600800 return;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +0530801 }
Eric Moore635374e2009-03-09 01:21:12 -0600802 list_for_each_entry_safe(mpt2sas_port, next, &sas_node->sas_port_list,
803 port_list) {
804 if (mpt2sas_port->remote_identify.sas_address != sas_address)
805 continue;
806 found = 1;
807 list_del(&mpt2sas_port->port_list);
808 goto out;
809 }
810 out:
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +0530811 if (!found) {
812 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -0600813 return;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +0530814 }
Eric Moore635374e2009-03-09 01:21:12 -0600815
816 for (i = 0; i < sas_node->num_phys; i++) {
817 if (sas_node->phy[i].remote_identify.sas_address == sas_address)
818 memset(&sas_node->phy[i].remote_identify, 0 ,
819 sizeof(struct sas_identify));
820 }
821
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +0530822 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -0600823 list_for_each_entry_safe(mpt2sas_phy, next_phy,
824 &mpt2sas_port->phy_list, port_siblings) {
825 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
826 dev_printk(KERN_INFO, &mpt2sas_port->port->dev,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530827 "remove: sas_addr(0x%016llx), phy(%d)\n",
Eric Moore635374e2009-03-09 01:21:12 -0600828 (unsigned long long)
829 mpt2sas_port->remote_identify.sas_address,
830 mpt2sas_phy->phy_id);
Kashyap, Desai38c29112010-04-05 14:18:34 +0530831 mpt2sas_phy->phy_belongs_to_port = 0;
Eric Moore635374e2009-03-09 01:21:12 -0600832 sas_port_delete_phy(mpt2sas_port->port, mpt2sas_phy->phy);
833 list_del(&mpt2sas_phy->port_siblings);
834 }
835 sas_port_delete(mpt2sas_port->port);
836 kfree(mpt2sas_port);
837}
838
839/**
840 * mpt2sas_transport_add_host_phy - report sas_host phy to transport
841 * @ioc: per adapter object
842 * @mpt2sas_phy: mpt2sas per phy object
843 * @phy_pg0: sas phy page 0
844 * @parent_dev: parent device class object
845 *
846 * Returns 0 for success, non-zero for failure.
847 */
848int
849mpt2sas_transport_add_host_phy(struct MPT2SAS_ADAPTER *ioc, struct _sas_phy
850 *mpt2sas_phy, Mpi2SasPhyPage0_t phy_pg0, struct device *parent_dev)
851{
852 struct sas_phy *phy;
853 int phy_index = mpt2sas_phy->phy_id;
854
855
856 INIT_LIST_HEAD(&mpt2sas_phy->port_siblings);
857 phy = sas_phy_alloc(parent_dev, phy_index);
858 if (!phy) {
859 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
860 ioc->name, __FILE__, __LINE__, __func__);
861 return -1;
862 }
863 if ((_transport_set_identify(ioc, mpt2sas_phy->handle,
864 &mpt2sas_phy->identify))) {
865 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
866 ioc->name, __FILE__, __LINE__, __func__);
867 return -1;
868 }
869 phy->identify = mpt2sas_phy->identify;
870 mpt2sas_phy->attached_handle = le16_to_cpu(phy_pg0.AttachedDevHandle);
871 if (mpt2sas_phy->attached_handle)
872 _transport_set_identify(ioc, mpt2sas_phy->attached_handle,
873 &mpt2sas_phy->remote_identify);
874 phy->identify.phy_identifier = mpt2sas_phy->phy_id;
875 phy->negotiated_linkrate = _transport_convert_phy_link_rate(
876 phy_pg0.NegotiatedLinkRate & MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
877 phy->minimum_linkrate_hw = _transport_convert_phy_link_rate(
878 phy_pg0.HwLinkRate & MPI2_SAS_HWRATE_MIN_RATE_MASK);
879 phy->maximum_linkrate_hw = _transport_convert_phy_link_rate(
880 phy_pg0.HwLinkRate >> 4);
881 phy->minimum_linkrate = _transport_convert_phy_link_rate(
882 phy_pg0.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
883 phy->maximum_linkrate = _transport_convert_phy_link_rate(
884 phy_pg0.ProgrammedLinkRate >> 4);
885
886 if ((sas_phy_add(phy))) {
887 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
888 ioc->name, __FILE__, __LINE__, __func__);
889 sas_phy_free(phy);
890 return -1;
891 }
892 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
893 dev_printk(KERN_INFO, &phy->dev,
894 "add: handle(0x%04x), sas_addr(0x%016llx)\n"
895 "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
896 mpt2sas_phy->handle, (unsigned long long)
897 mpt2sas_phy->identify.sas_address,
898 mpt2sas_phy->attached_handle,
899 (unsigned long long)
900 mpt2sas_phy->remote_identify.sas_address);
901 mpt2sas_phy->phy = phy;
902 return 0;
903}
904
905
906/**
907 * mpt2sas_transport_add_expander_phy - report expander phy to transport
908 * @ioc: per adapter object
909 * @mpt2sas_phy: mpt2sas per phy object
910 * @expander_pg1: expander page 1
911 * @parent_dev: parent device class object
912 *
913 * Returns 0 for success, non-zero for failure.
914 */
915int
916mpt2sas_transport_add_expander_phy(struct MPT2SAS_ADAPTER *ioc, struct _sas_phy
917 *mpt2sas_phy, Mpi2ExpanderPage1_t expander_pg1, struct device *parent_dev)
918{
919 struct sas_phy *phy;
920 int phy_index = mpt2sas_phy->phy_id;
921
922 INIT_LIST_HEAD(&mpt2sas_phy->port_siblings);
923 phy = sas_phy_alloc(parent_dev, phy_index);
924 if (!phy) {
925 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
926 ioc->name, __FILE__, __LINE__, __func__);
927 return -1;
928 }
929 if ((_transport_set_identify(ioc, mpt2sas_phy->handle,
930 &mpt2sas_phy->identify))) {
931 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
932 ioc->name, __FILE__, __LINE__, __func__);
933 return -1;
934 }
935 phy->identify = mpt2sas_phy->identify;
936 mpt2sas_phy->attached_handle =
937 le16_to_cpu(expander_pg1.AttachedDevHandle);
938 if (mpt2sas_phy->attached_handle)
939 _transport_set_identify(ioc, mpt2sas_phy->attached_handle,
940 &mpt2sas_phy->remote_identify);
941 phy->identify.phy_identifier = mpt2sas_phy->phy_id;
942 phy->negotiated_linkrate = _transport_convert_phy_link_rate(
943 expander_pg1.NegotiatedLinkRate &
944 MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
945 phy->minimum_linkrate_hw = _transport_convert_phy_link_rate(
946 expander_pg1.HwLinkRate & MPI2_SAS_HWRATE_MIN_RATE_MASK);
947 phy->maximum_linkrate_hw = _transport_convert_phy_link_rate(
948 expander_pg1.HwLinkRate >> 4);
949 phy->minimum_linkrate = _transport_convert_phy_link_rate(
950 expander_pg1.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
951 phy->maximum_linkrate = _transport_convert_phy_link_rate(
952 expander_pg1.ProgrammedLinkRate >> 4);
953
954 if ((sas_phy_add(phy))) {
955 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
956 ioc->name, __FILE__, __LINE__, __func__);
957 sas_phy_free(phy);
958 return -1;
959 }
960 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
961 dev_printk(KERN_INFO, &phy->dev,
962 "add: handle(0x%04x), sas_addr(0x%016llx)\n"
963 "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
964 mpt2sas_phy->handle, (unsigned long long)
965 mpt2sas_phy->identify.sas_address,
966 mpt2sas_phy->attached_handle,
967 (unsigned long long)
968 mpt2sas_phy->remote_identify.sas_address);
969 mpt2sas_phy->phy = phy;
970 return 0;
971}
972
973/**
Kashyap, Desaicc0f5202009-08-20 13:22:39 +0530974 * mpt2sas_transport_update_links - refreshing phy link changes
Eric Moore635374e2009-03-09 01:21:12 -0600975 * @ioc: per adapter object
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530976 * @sas_address: sas address of parent expander or sas host
977 * @handle: attached device handle
Eric Moore635374e2009-03-09 01:21:12 -0600978 * @phy_numberv: phy number
979 * @link_rate: new link rate
980 *
981 * Returns nothing.
982 */
983void
Kashyap, Desaicc0f5202009-08-20 13:22:39 +0530984mpt2sas_transport_update_links(struct MPT2SAS_ADAPTER *ioc,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530985 u64 sas_address, u16 handle, u8 phy_number, u8 link_rate)
Eric Moore635374e2009-03-09 01:21:12 -0600986{
987 unsigned long flags;
988 struct _sas_node *sas_node;
989 struct _sas_phy *mpt2sas_phy;
990
Eric Moore3cb54692010-07-08 14:44:34 -0600991 if (ioc->shost_recovery || ioc->pci_error_recovery)
Kashyap, Desai155dd4c2009-08-20 13:22:00 +0530992 return;
Kashyap, Desai155dd4c2009-08-20 13:22:00 +0530993
Eric Moore635374e2009-03-09 01:21:12 -0600994 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530995 sas_node = _transport_sas_node_find_by_sas_address(ioc, sas_address);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +0530996 if (!sas_node) {
997 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -0600998 return;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +0530999 }
Eric Moore635374e2009-03-09 01:21:12 -06001000
1001 mpt2sas_phy = &sas_node->phy[phy_number];
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05301002 mpt2sas_phy->attached_handle = handle;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301003 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05301004 if (handle && (link_rate >= MPI2_SAS_NEG_LINK_RATE_1_5)) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05301005 _transport_set_identify(ioc, handle,
Eric Moore635374e2009-03-09 01:21:12 -06001006 &mpt2sas_phy->remote_identify);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05301007 _transport_add_phy_to_an_existing_port(ioc, sas_node,
1008 mpt2sas_phy, mpt2sas_phy->remote_identify.sas_address);
Jan Vesely3520f9c2013-08-01 12:01:30 +02001009 } else {
Eric Moore635374e2009-03-09 01:21:12 -06001010 memset(&mpt2sas_phy->remote_identify, 0 , sizeof(struct
1011 sas_identify));
Jan Vesely3520f9c2013-08-01 12:01:30 +02001012 _transport_del_phy_from_an_existing_port(ioc, sas_node,
1013 mpt2sas_phy);
1014 }
Eric Moore635374e2009-03-09 01:21:12 -06001015
1016 if (mpt2sas_phy->phy)
1017 mpt2sas_phy->phy->negotiated_linkrate =
1018 _transport_convert_phy_link_rate(link_rate);
1019
1020 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
1021 dev_printk(KERN_INFO, &mpt2sas_phy->phy->dev,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05301022 "refresh: parent sas_addr(0x%016llx),\n"
Eric Moore635374e2009-03-09 01:21:12 -06001023 "\tlink_rate(0x%02x), phy(%d)\n"
1024 "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05301025 (unsigned long long)sas_address,
1026 link_rate, phy_number, handle, (unsigned long long)
Eric Moore635374e2009-03-09 01:21:12 -06001027 mpt2sas_phy->remote_identify.sas_address);
1028}
1029
1030static inline void *
1031phy_to_ioc(struct sas_phy *phy)
1032{
1033 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
1034 return shost_priv(shost);
1035}
1036
1037static inline void *
1038rphy_to_ioc(struct sas_rphy *rphy)
1039{
1040 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
1041 return shost_priv(shost);
1042}
1043
Kashyap, Desai50d5c602009-12-16 18:55:26 +05301044
Kashyap, Desaid5f491e2010-06-17 13:32:54 +05301045/* report phy error log structure */
1046struct phy_error_log_request{
1047 u8 smp_frame_type; /* 0x40 */
1048 u8 function; /* 0x11 */
1049 u8 allocated_response_length;
1050 u8 request_length; /* 02 */
1051 u8 reserved_1[5];
1052 u8 phy_identifier;
1053 u8 reserved_2[2];
1054};
1055
1056/* report phy error log reply structure */
1057struct phy_error_log_reply{
1058 u8 smp_frame_type; /* 0x41 */
1059 u8 function; /* 0x11 */
1060 u8 function_result;
1061 u8 response_length;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05301062 __be16 expander_change_count;
Kashyap, Desaid5f491e2010-06-17 13:32:54 +05301063 u8 reserved_1[3];
1064 u8 phy_identifier;
1065 u8 reserved_2[2];
Kashyap, Desaic97951e2011-06-14 10:54:56 +05301066 __be32 invalid_dword;
1067 __be32 running_disparity_error;
1068 __be32 loss_of_dword_sync;
1069 __be32 phy_reset_problem;
Kashyap, Desaid5f491e2010-06-17 13:32:54 +05301070};
1071
Eric Moore635374e2009-03-09 01:21:12 -06001072/**
Kashyap, Desaid5f491e2010-06-17 13:32:54 +05301073 * _transport_get_expander_phy_error_log - return expander counters
1074 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06001075 * @phy: The sas phy object
1076 *
Kashyap, Desaid5f491e2010-06-17 13:32:54 +05301077 * Returns 0 for success, non-zero for failure.
1078 *
1079 */
1080static int
1081_transport_get_expander_phy_error_log(struct MPT2SAS_ADAPTER *ioc,
1082 struct sas_phy *phy)
1083{
1084 Mpi2SmpPassthroughRequest_t *mpi_request;
1085 Mpi2SmpPassthroughReply_t *mpi_reply;
1086 struct phy_error_log_request *phy_error_log_request;
1087 struct phy_error_log_reply *phy_error_log_reply;
1088 int rc;
1089 u16 smid;
1090 u32 ioc_state;
1091 unsigned long timeleft;
1092 void *psge;
1093 u32 sgl_flags;
1094 u8 issue_reset = 0;
1095 void *data_out = NULL;
1096 dma_addr_t data_out_dma;
1097 u32 sz;
Kashyap, Desaid5f491e2010-06-17 13:32:54 +05301098 u16 wait_state_count;
1099
Eric Moore3cb54692010-07-08 14:44:34 -06001100 if (ioc->shost_recovery || ioc->pci_error_recovery) {
Kashyap, Desaid5f491e2010-06-17 13:32:54 +05301101 printk(MPT2SAS_INFO_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 != MPT2_CMD_NOT_USED) {
1109 printk(MPT2SAS_ERR_FMT "%s: transport_cmds in use\n",
1110 ioc->name, __func__);
1111 rc = -EAGAIN;
1112 goto out;
1113 }
1114 ioc->transport_cmds.status = MPT2_CMD_PENDING;
1115
1116 wait_state_count = 0;
1117 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
1118 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1119 if (wait_state_count++ == 10) {
1120 printk(MPT2SAS_ERR_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 = mpt2sas_base_get_iocstate(ioc, 1);
1128 printk(MPT2SAS_INFO_FMT "%s: waiting for "
1129 "operational state(count=%d)\n", ioc->name,
1130 __func__, wait_state_count);
1131 }
1132 if (wait_state_count)
1133 printk(MPT2SAS_INFO_FMT "%s: ioc is operational\n",
1134 ioc->name, __func__);
1135
1136 smid = mpt2sas_base_get_smid(ioc, ioc->transport_cb_idx);
1137 if (!smid) {
1138 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1139 ioc->name, __func__);
1140 rc = -EAGAIN;
1141 goto out;
1142 }
1143
1144 mpi_request = mpt2sas_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 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
1152 __LINE__, __func__);
1153 rc = -ENOMEM;
1154 mpt2sas_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;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05301172 mpi_request->SASAddress = cpu_to_le64(phy->identify.sas_address);
Kashyap, Desaid5f491e2010-06-17 13:32:54 +05301173 mpi_request->RequestDataLength =
1174 cpu_to_le16(sizeof(struct phy_error_log_request));
1175 psge = &mpi_request->SGL;
1176
1177 /* WRITE sgel first */
1178 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1179 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
1180 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1181 ioc->base_add_sg_single(psge, sgl_flags |
1182 sizeof(struct phy_error_log_request), data_out_dma);
1183
1184 /* incr sgel */
1185 psge += ioc->sge_size;
1186
1187 /* READ sgel last */
1188 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1189 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
1190 MPI2_SGE_FLAGS_END_OF_LIST);
1191 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1192 ioc->base_add_sg_single(psge, sgl_flags |
1193 sizeof(struct phy_error_log_reply), data_out_dma +
1194 sizeof(struct phy_error_log_request));
1195
1196 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "phy_error_log - "
1197 "send to sas_addr(0x%016llx), phy(%d)\n", ioc->name,
1198 (unsigned long long)phy->identify.sas_address, phy->number));
Kashyap, Desaid5f491e2010-06-17 13:32:54 +05301199 init_completion(&ioc->transport_cmds.done);
nagalakshmi.nandigama@lsi.comf01690d2011-12-01 07:43:58 +05301200 mpt2sas_base_put_smid_default(ioc, smid);
Kashyap, Desaid5f491e2010-06-17 13:32:54 +05301201 timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done,
1202 10*HZ);
1203
1204 if (!(ioc->transport_cmds.status & MPT2_CMD_COMPLETE)) {
1205 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
1206 ioc->name, __func__);
1207 _debug_dump_mf(mpi_request,
1208 sizeof(Mpi2SmpPassthroughRequest_t)/4);
1209 if (!(ioc->transport_cmds.status & MPT2_CMD_RESET))
1210 issue_reset = 1;
1211 goto issue_host_reset;
1212 }
1213
1214 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "phy_error_log - "
1215 "complete\n", ioc->name));
1216
1217 if (ioc->transport_cmds.status & MPT2_CMD_REPLY_VALID) {
1218
1219 mpi_reply = ioc->transport_cmds.reply;
1220
1221 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
1222 "phy_error_log - reply data transfer size(%d)\n",
1223 ioc->name, le16_to_cpu(mpi_reply->ResponseDataLength)));
1224
1225 if (le16_to_cpu(mpi_reply->ResponseDataLength) !=
1226 sizeof(struct phy_error_log_reply))
1227 goto out;
1228
1229 phy_error_log_reply = data_out +
1230 sizeof(struct phy_error_log_request);
1231
1232 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
1233 "phy_error_log - function_result(%d)\n",
1234 ioc->name, phy_error_log_reply->function_result));
1235
1236 phy->invalid_dword_count =
1237 be32_to_cpu(phy_error_log_reply->invalid_dword);
1238 phy->running_disparity_error_count =
1239 be32_to_cpu(phy_error_log_reply->running_disparity_error);
1240 phy->loss_of_dword_sync_count =
1241 be32_to_cpu(phy_error_log_reply->loss_of_dword_sync);
1242 phy->phy_reset_problem_count =
1243 be32_to_cpu(phy_error_log_reply->phy_reset_problem);
1244 rc = 0;
1245 } else
1246 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
1247 "phy_error_log - no reply\n", ioc->name));
1248
1249 issue_host_reset:
1250 if (issue_reset)
1251 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1252 FORCE_BIG_HAMMER);
1253 out:
1254 ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
1255 if (data_out)
1256 pci_free_consistent(ioc->pdev, sz, data_out, data_out_dma);
1257
1258 mutex_unlock(&ioc->transport_cmds.mutex);
1259 return rc;
1260}
1261
1262/**
1263 * _transport_get_linkerrors - return phy counters for both hba and expanders
1264 * @phy: The sas phy object
1265 *
Eric Moore635374e2009-03-09 01:21:12 -06001266 * Returns 0 for success, non-zero for failure.
1267 *
1268 */
1269static int
Eric Moored5d135b2009-05-18 13:02:08 -06001270_transport_get_linkerrors(struct sas_phy *phy)
Eric Moore635374e2009-03-09 01:21:12 -06001271{
1272 struct MPT2SAS_ADAPTER *ioc = phy_to_ioc(phy);
Kashyap, Desaid5f491e2010-06-17 13:32:54 +05301273 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06001274 Mpi2ConfigReply_t mpi_reply;
1275 Mpi2SasPhyPage1_t phy_pg1;
Eric Moore635374e2009-03-09 01:21:12 -06001276
Kashyap, Desaid5f491e2010-06-17 13:32:54 +05301277 spin_lock_irqsave(&ioc->sas_node_lock, flags);
1278 if (_transport_sas_node_find_by_sas_address(ioc,
1279 phy->identify.sas_address) == NULL) {
1280 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06001281 return -EINVAL;
Kashyap, Desaid5f491e2010-06-17 13:32:54 +05301282 }
1283 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06001284
Kashyap, Desaid5f491e2010-06-17 13:32:54 +05301285 if (phy->identify.sas_address != ioc->sas_hba.sas_address)
1286 return _transport_get_expander_phy_error_log(ioc, phy);
1287
1288 /* get hba phy error logs */
Eric Moore635374e2009-03-09 01:21:12 -06001289 if ((mpt2sas_config_get_phy_pg1(ioc, &mpi_reply, &phy_pg1,
Kashyap, Desaid5f491e2010-06-17 13:32:54 +05301290 phy->number))) {
Eric Moore635374e2009-03-09 01:21:12 -06001291 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1292 ioc->name, __FILE__, __LINE__, __func__);
1293 return -ENXIO;
1294 }
1295
1296 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo)
1297 printk(MPT2SAS_INFO_FMT "phy(%d), ioc_status"
1298 "(0x%04x), loginfo(0x%08x)\n", ioc->name,
Kashyap, Desaid5f491e2010-06-17 13:32:54 +05301299 phy->number, le16_to_cpu(mpi_reply.IOCStatus),
Eric Moore635374e2009-03-09 01:21:12 -06001300 le32_to_cpu(mpi_reply.IOCLogInfo));
1301
1302 phy->invalid_dword_count = le32_to_cpu(phy_pg1.InvalidDwordCount);
1303 phy->running_disparity_error_count =
1304 le32_to_cpu(phy_pg1.RunningDisparityErrorCount);
1305 phy->loss_of_dword_sync_count =
1306 le32_to_cpu(phy_pg1.LossDwordSynchCount);
1307 phy->phy_reset_problem_count =
1308 le32_to_cpu(phy_pg1.PhyResetProblemCount);
1309 return 0;
1310}
1311
1312/**
Eric Moored5d135b2009-05-18 13:02:08 -06001313 * _transport_get_enclosure_identifier -
Eric Moore635374e2009-03-09 01:21:12 -06001314 * @phy: The sas phy object
1315 *
1316 * Obtain the enclosure logical id for an expander.
1317 * Returns 0 for success, non-zero for failure.
1318 */
1319static int
Eric Moored5d135b2009-05-18 13:02:08 -06001320_transport_get_enclosure_identifier(struct sas_rphy *rphy, u64 *identifier)
Eric Moore635374e2009-03-09 01:21:12 -06001321{
1322 struct MPT2SAS_ADAPTER *ioc = rphy_to_ioc(rphy);
Kashyap, Desaiab6ce922010-06-17 13:25:13 +05301323 struct _sas_device *sas_device;
Eric Moore635374e2009-03-09 01:21:12 -06001324 unsigned long flags;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301325 int rc;
Eric Moore635374e2009-03-09 01:21:12 -06001326
Kashyap, Desaiab6ce922010-06-17 13:25:13 +05301327 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1328 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
Eric Moore635374e2009-03-09 01:21:12 -06001329 rphy->identify.sas_address);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301330 if (sas_device) {
1331 *identifier = sas_device->enclosure_logical_id;
1332 rc = 0;
1333 } else {
1334 *identifier = 0;
1335 rc = -ENXIO;
1336 }
Kashyap, Desaiab6ce922010-06-17 13:25:13 +05301337 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301338 return rc;
Eric Moore635374e2009-03-09 01:21:12 -06001339}
1340
1341/**
Eric Moored5d135b2009-05-18 13:02:08 -06001342 * _transport_get_bay_identifier -
Eric Moore635374e2009-03-09 01:21:12 -06001343 * @phy: The sas phy object
1344 *
1345 * Returns the slot id for a device that resides inside an enclosure.
1346 */
1347static int
Eric Moored5d135b2009-05-18 13:02:08 -06001348_transport_get_bay_identifier(struct sas_rphy *rphy)
Eric Moore635374e2009-03-09 01:21:12 -06001349{
1350 struct MPT2SAS_ADAPTER *ioc = rphy_to_ioc(rphy);
1351 struct _sas_device *sas_device;
1352 unsigned long flags;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301353 int rc;
Eric Moore635374e2009-03-09 01:21:12 -06001354
1355 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1356 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1357 rphy->identify.sas_address);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301358 if (sas_device)
1359 rc = sas_device->slot;
1360 else
1361 rc = -ENXIO;
Eric Moore635374e2009-03-09 01:21:12 -06001362 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301363 return rc;
Eric Moore635374e2009-03-09 01:21:12 -06001364}
1365
Kashyap, Desaib8d7d7b2010-06-17 13:34:31 +05301366/* phy control request structure */
1367struct phy_control_request{
1368 u8 smp_frame_type; /* 0x40 */
1369 u8 function; /* 0x91 */
1370 u8 allocated_response_length;
1371 u8 request_length; /* 0x09 */
1372 u16 expander_change_count;
1373 u8 reserved_1[3];
1374 u8 phy_identifier;
1375 u8 phy_operation;
1376 u8 reserved_2[13];
1377 u64 attached_device_name;
1378 u8 programmed_min_physical_link_rate;
1379 u8 programmed_max_physical_link_rate;
1380 u8 reserved_3[6];
1381};
1382
1383/* phy control reply structure */
1384struct phy_control_reply{
1385 u8 smp_frame_type; /* 0x41 */
1386 u8 function; /* 0x11 */
1387 u8 function_result;
1388 u8 response_length;
1389};
1390
1391#define SMP_PHY_CONTROL_LINK_RESET (0x01)
1392#define SMP_PHY_CONTROL_HARD_RESET (0x02)
1393#define SMP_PHY_CONTROL_DISABLE (0x03)
1394
1395/**
1396 * _transport_expander_phy_control - expander phy control
1397 * @ioc: per adapter object
1398 * @phy: The sas phy object
1399 *
1400 * Returns 0 for success, non-zero for failure.
1401 *
1402 */
1403static int
1404_transport_expander_phy_control(struct MPT2SAS_ADAPTER *ioc,
1405 struct sas_phy *phy, u8 phy_operation)
1406{
1407 Mpi2SmpPassthroughRequest_t *mpi_request;
1408 Mpi2SmpPassthroughReply_t *mpi_reply;
1409 struct phy_control_request *phy_control_request;
1410 struct phy_control_reply *phy_control_reply;
1411 int rc;
1412 u16 smid;
1413 u32 ioc_state;
1414 unsigned long timeleft;
1415 void *psge;
1416 u32 sgl_flags;
1417 u8 issue_reset = 0;
1418 void *data_out = NULL;
1419 dma_addr_t data_out_dma;
1420 u32 sz;
Kashyap, Desaib8d7d7b2010-06-17 13:34:31 +05301421 u16 wait_state_count;
1422
1423 if (ioc->shost_recovery) {
1424 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
1425 __func__, ioc->name);
1426 return -EFAULT;
1427 }
1428
1429 mutex_lock(&ioc->transport_cmds.mutex);
1430
1431 if (ioc->transport_cmds.status != MPT2_CMD_NOT_USED) {
1432 printk(MPT2SAS_ERR_FMT "%s: transport_cmds in use\n",
1433 ioc->name, __func__);
1434 rc = -EAGAIN;
1435 goto out;
1436 }
1437 ioc->transport_cmds.status = MPT2_CMD_PENDING;
1438
1439 wait_state_count = 0;
1440 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
1441 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1442 if (wait_state_count++ == 10) {
1443 printk(MPT2SAS_ERR_FMT
1444 "%s: failed due to ioc not operational\n",
1445 ioc->name, __func__);
1446 rc = -EFAULT;
1447 goto out;
1448 }
1449 ssleep(1);
1450 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
1451 printk(MPT2SAS_INFO_FMT "%s: waiting for "
1452 "operational state(count=%d)\n", ioc->name,
1453 __func__, wait_state_count);
1454 }
1455 if (wait_state_count)
1456 printk(MPT2SAS_INFO_FMT "%s: ioc is operational\n",
1457 ioc->name, __func__);
1458
1459 smid = mpt2sas_base_get_smid(ioc, ioc->transport_cb_idx);
1460 if (!smid) {
1461 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1462 ioc->name, __func__);
1463 rc = -EAGAIN;
1464 goto out;
1465 }
1466
1467 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1468 ioc->transport_cmds.smid = smid;
1469
1470 sz = sizeof(struct phy_control_request) +
1471 sizeof(struct phy_control_reply);
1472 data_out = pci_alloc_consistent(ioc->pdev, sz, &data_out_dma);
1473 if (!data_out) {
1474 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
1475 __LINE__, __func__);
1476 rc = -ENOMEM;
1477 mpt2sas_base_free_smid(ioc, smid);
1478 goto out;
1479 }
1480
1481 rc = -EINVAL;
1482 memset(data_out, 0, sz);
1483 phy_control_request = data_out;
1484 phy_control_request->smp_frame_type = 0x40;
1485 phy_control_request->function = 0x91;
1486 phy_control_request->request_length = 9;
1487 phy_control_request->allocated_response_length = 0;
1488 phy_control_request->phy_identifier = phy->number;
1489 phy_control_request->phy_operation = phy_operation;
1490 phy_control_request->programmed_min_physical_link_rate =
1491 phy->minimum_linkrate << 4;
1492 phy_control_request->programmed_max_physical_link_rate =
1493 phy->maximum_linkrate << 4;
1494
1495 memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
1496 mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
1497 mpi_request->PhysicalPort = 0xFF;
1498 mpi_request->VF_ID = 0; /* TODO */
1499 mpi_request->VP_ID = 0;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05301500 mpi_request->SASAddress = cpu_to_le64(phy->identify.sas_address);
Kashyap, Desaib8d7d7b2010-06-17 13:34:31 +05301501 mpi_request->RequestDataLength =
1502 cpu_to_le16(sizeof(struct phy_error_log_request));
1503 psge = &mpi_request->SGL;
1504
1505 /* WRITE sgel first */
1506 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1507 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
1508 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1509 ioc->base_add_sg_single(psge, sgl_flags |
1510 sizeof(struct phy_control_request), data_out_dma);
1511
1512 /* incr sgel */
1513 psge += ioc->sge_size;
1514
1515 /* READ sgel last */
1516 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1517 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
1518 MPI2_SGE_FLAGS_END_OF_LIST);
1519 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1520 ioc->base_add_sg_single(psge, sgl_flags |
1521 sizeof(struct phy_control_reply), data_out_dma +
1522 sizeof(struct phy_control_request));
1523
1524 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "phy_control - "
1525 "send to sas_addr(0x%016llx), phy(%d), opcode(%d)\n", ioc->name,
1526 (unsigned long long)phy->identify.sas_address, phy->number,
1527 phy_operation));
nagalakshmi.nandigama@lsi.comf01690d2011-12-01 07:43:58 +05301528
Kashyap, Desaib8d7d7b2010-06-17 13:34:31 +05301529 init_completion(&ioc->transport_cmds.done);
nagalakshmi.nandigama@lsi.comf01690d2011-12-01 07:43:58 +05301530 mpt2sas_base_put_smid_default(ioc, smid);
Kashyap, Desaib8d7d7b2010-06-17 13:34:31 +05301531 timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done,
1532 10*HZ);
1533
1534 if (!(ioc->transport_cmds.status & MPT2_CMD_COMPLETE)) {
1535 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
1536 ioc->name, __func__);
1537 _debug_dump_mf(mpi_request,
1538 sizeof(Mpi2SmpPassthroughRequest_t)/4);
1539 if (!(ioc->transport_cmds.status & MPT2_CMD_RESET))
1540 issue_reset = 1;
1541 goto issue_host_reset;
1542 }
1543
1544 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "phy_control - "
1545 "complete\n", ioc->name));
1546
1547 if (ioc->transport_cmds.status & MPT2_CMD_REPLY_VALID) {
1548
1549 mpi_reply = ioc->transport_cmds.reply;
1550
1551 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
1552 "phy_control - reply data transfer size(%d)\n",
1553 ioc->name, le16_to_cpu(mpi_reply->ResponseDataLength)));
1554
1555 if (le16_to_cpu(mpi_reply->ResponseDataLength) !=
1556 sizeof(struct phy_control_reply))
1557 goto out;
1558
1559 phy_control_reply = data_out +
1560 sizeof(struct phy_control_request);
1561
1562 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
1563 "phy_control - function_result(%d)\n",
1564 ioc->name, phy_control_reply->function_result));
1565
1566 rc = 0;
1567 } else
1568 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
1569 "phy_control - no reply\n", ioc->name));
1570
1571 issue_host_reset:
1572 if (issue_reset)
1573 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1574 FORCE_BIG_HAMMER);
1575 out:
1576 ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
1577 if (data_out)
1578 pci_free_consistent(ioc->pdev, sz, data_out, data_out_dma);
1579
1580 mutex_unlock(&ioc->transport_cmds.mutex);
1581 return rc;
1582}
1583
Eric Moore635374e2009-03-09 01:21:12 -06001584/**
Eric Moored5d135b2009-05-18 13:02:08 -06001585 * _transport_phy_reset -
Eric Moore635374e2009-03-09 01:21:12 -06001586 * @phy: The sas phy object
1587 * @hard_reset:
1588 *
Eric Moore635374e2009-03-09 01:21:12 -06001589 * Returns 0 for success, non-zero for failure.
1590 */
1591static int
Eric Moored5d135b2009-05-18 13:02:08 -06001592_transport_phy_reset(struct sas_phy *phy, int hard_reset)
Eric Moore635374e2009-03-09 01:21:12 -06001593{
1594 struct MPT2SAS_ADAPTER *ioc = phy_to_ioc(phy);
Eric Moore635374e2009-03-09 01:21:12 -06001595 Mpi2SasIoUnitControlReply_t mpi_reply;
1596 Mpi2SasIoUnitControlRequest_t mpi_request;
Kashyap, Desaib8d7d7b2010-06-17 13:34:31 +05301597 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06001598
Kashyap, Desaib8d7d7b2010-06-17 13:34:31 +05301599 spin_lock_irqsave(&ioc->sas_node_lock, flags);
1600 if (_transport_sas_node_find_by_sas_address(ioc,
1601 phy->identify.sas_address) == NULL) {
1602 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06001603 return -EINVAL;
Kashyap, Desaib8d7d7b2010-06-17 13:34:31 +05301604 }
1605 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06001606
Kashyap, Desaib8d7d7b2010-06-17 13:34:31 +05301607 /* handle expander phys */
1608 if (phy->identify.sas_address != ioc->sas_hba.sas_address)
1609 return _transport_expander_phy_control(ioc, phy,
1610 (hard_reset == 1) ? SMP_PHY_CONTROL_HARD_RESET :
1611 SMP_PHY_CONTROL_LINK_RESET);
1612
1613 /* handle hba phys */
Eric Moore635374e2009-03-09 01:21:12 -06001614 memset(&mpi_request, 0, sizeof(Mpi2SasIoUnitControlReply_t));
1615 mpi_request.Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
1616 mpi_request.Operation = hard_reset ?
1617 MPI2_SAS_OP_PHY_HARD_RESET : MPI2_SAS_OP_PHY_LINK_RESET;
Kashyap, Desaib8d7d7b2010-06-17 13:34:31 +05301618 mpi_request.PhyNum = phy->number;
Eric Moore635374e2009-03-09 01:21:12 -06001619
1620 if ((mpt2sas_base_sas_iounit_control(ioc, &mpi_reply, &mpi_request))) {
1621 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1622 ioc->name, __FILE__, __LINE__, __func__);
1623 return -ENXIO;
1624 }
1625
1626 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo)
1627 printk(MPT2SAS_INFO_FMT "phy(%d), ioc_status"
1628 "(0x%04x), loginfo(0x%08x)\n", ioc->name,
Kashyap, Desaib8d7d7b2010-06-17 13:34:31 +05301629 phy->number, le16_to_cpu(mpi_reply.IOCStatus),
Eric Moore635374e2009-03-09 01:21:12 -06001630 le32_to_cpu(mpi_reply.IOCLogInfo));
1631
1632 return 0;
1633}
1634
1635/**
Kashyap, Desai50d5c602009-12-16 18:55:26 +05301636 * _transport_phy_enable - enable/disable phys
1637 * @phy: The sas phy object
1638 * @enable: enable phy when true
1639 *
1640 * Only support sas_host direct attached phys.
1641 * Returns 0 for success, non-zero for failure.
1642 */
1643static int
1644_transport_phy_enable(struct sas_phy *phy, int enable)
1645{
1646 struct MPT2SAS_ADAPTER *ioc = phy_to_ioc(phy);
Kashyap, Desai50d5c602009-12-16 18:55:26 +05301647 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
nagalakshmi.nandigama@lsi.com43d6ddf2012-03-20 12:08:11 +05301648 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
Kashyap, Desai50d5c602009-12-16 18:55:26 +05301649 Mpi2ConfigReply_t mpi_reply;
1650 u16 ioc_status;
1651 u16 sz;
1652 int rc = 0;
Kashyap, Desaib8d7d7b2010-06-17 13:34:31 +05301653 unsigned long flags;
nagalakshmi.nandigama@lsi.com43d6ddf2012-03-20 12:08:11 +05301654 int i, discovery_active;
Kashyap, Desai50d5c602009-12-16 18:55:26 +05301655
Kashyap, Desaib8d7d7b2010-06-17 13:34:31 +05301656 spin_lock_irqsave(&ioc->sas_node_lock, flags);
1657 if (_transport_sas_node_find_by_sas_address(ioc,
1658 phy->identify.sas_address) == NULL) {
1659 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
Kashyap, Desai50d5c602009-12-16 18:55:26 +05301660 return -EINVAL;
Kashyap, Desaib8d7d7b2010-06-17 13:34:31 +05301661 }
1662 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1663
1664 /* handle expander phys */
1665 if (phy->identify.sas_address != ioc->sas_hba.sas_address)
1666 return _transport_expander_phy_control(ioc, phy,
1667 (enable == 1) ? SMP_PHY_CONTROL_LINK_RESET :
1668 SMP_PHY_CONTROL_DISABLE);
1669
1670 /* handle hba phys */
Kashyap, Desai50d5c602009-12-16 18:55:26 +05301671
nagalakshmi.nandigama@lsi.com43d6ddf2012-03-20 12:08:11 +05301672 /* read sas_iounit page 0 */
1673 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
1674 sizeof(Mpi2SasIOUnit0PhyData_t));
1675 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
1676 if (!sas_iounit_pg0) {
1677 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1678 ioc->name, __FILE__, __LINE__, __func__);
1679 rc = -ENOMEM;
1680 goto out;
1681 }
1682 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
1683 sas_iounit_pg0, sz))) {
1684 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1685 ioc->name, __FILE__, __LINE__, __func__);
1686 rc = -ENXIO;
1687 goto out;
1688 }
1689 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1690 MPI2_IOCSTATUS_MASK;
1691 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1692 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1693 ioc->name, __FILE__, __LINE__, __func__);
1694 rc = -EIO;
1695 goto out;
1696 }
1697
1698 /* unable to enable/disable phys when when discovery is active */
1699 for (i = 0, discovery_active = 0; i < ioc->sas_hba.num_phys ; i++) {
1700 if (sas_iounit_pg0->PhyData[i].PortFlags &
1701 MPI2_SASIOUNIT0_PORTFLAGS_DISCOVERY_IN_PROGRESS) {
1702 printk(MPT2SAS_ERR_FMT "discovery is active on "
1703 "port = %d, phy = %d: unable to enable/disable "
1704 "phys, try again later!\n", ioc->name,
1705 sas_iounit_pg0->PhyData[i].Port, i);
1706 discovery_active = 1;
1707 }
1708 }
1709
1710 if (discovery_active) {
1711 rc = -EAGAIN;
1712 goto out;
1713 }
1714
1715 /* read sas_iounit page 1 */
Kashyap, Desai50d5c602009-12-16 18:55:26 +05301716 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
1717 sizeof(Mpi2SasIOUnit1PhyData_t));
1718 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
1719 if (!sas_iounit_pg1) {
1720 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1721 ioc->name, __FILE__, __LINE__, __func__);
1722 rc = -ENOMEM;
1723 goto out;
1724 }
1725 if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
1726 sas_iounit_pg1, sz))) {
1727 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1728 ioc->name, __FILE__, __LINE__, __func__);
1729 rc = -ENXIO;
1730 goto out;
1731 }
1732 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1733 MPI2_IOCSTATUS_MASK;
1734 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1735 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1736 ioc->name, __FILE__, __LINE__, __func__);
1737 rc = -EIO;
1738 goto out;
1739 }
nagalakshmi.nandigama@lsi.com43d6ddf2012-03-20 12:08:11 +05301740 /* copy Port/PortFlags/PhyFlags from page 0 */
1741 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
1742 sas_iounit_pg1->PhyData[i].Port =
1743 sas_iounit_pg0->PhyData[i].Port;
1744 sas_iounit_pg1->PhyData[i].PortFlags =
1745 (sas_iounit_pg0->PhyData[i].PortFlags &
1746 MPI2_SASIOUNIT0_PORTFLAGS_AUTO_PORT_CONFIG);
1747 sas_iounit_pg1->PhyData[i].PhyFlags =
1748 (sas_iounit_pg0->PhyData[i].PhyFlags &
1749 (MPI2_SASIOUNIT0_PHYFLAGS_ZONING_ENABLED +
1750 MPI2_SASIOUNIT0_PHYFLAGS_PHY_DISABLED));
1751 }
Kashyap, Desai50d5c602009-12-16 18:55:26 +05301752 if (enable)
Kashyap, Desaib8d7d7b2010-06-17 13:34:31 +05301753 sas_iounit_pg1->PhyData[phy->number].PhyFlags
Kashyap, Desai50d5c602009-12-16 18:55:26 +05301754 &= ~MPI2_SASIOUNIT1_PHYFLAGS_PHY_DISABLE;
1755 else
Kashyap, Desaib8d7d7b2010-06-17 13:34:31 +05301756 sas_iounit_pg1->PhyData[phy->number].PhyFlags
Kashyap, Desai50d5c602009-12-16 18:55:26 +05301757 |= MPI2_SASIOUNIT1_PHYFLAGS_PHY_DISABLE;
1758
1759 mpt2sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1, sz);
1760
Kashyap, Desaib8d7d7b2010-06-17 13:34:31 +05301761 /* link reset */
1762 if (enable)
1763 _transport_phy_reset(phy, 0);
1764
Kashyap, Desai50d5c602009-12-16 18:55:26 +05301765 out:
1766 kfree(sas_iounit_pg1);
nagalakshmi.nandigama@lsi.com43d6ddf2012-03-20 12:08:11 +05301767 kfree(sas_iounit_pg0);
Kashyap, Desai50d5c602009-12-16 18:55:26 +05301768 return rc;
1769}
1770
1771/**
1772 * _transport_phy_speed - set phy min/max link rates
1773 * @phy: The sas phy object
1774 * @rates: rates defined in sas_phy_linkrates
1775 *
1776 * Only support sas_host direct attached phys.
1777 * Returns 0 for success, non-zero for failure.
1778 */
1779static int
1780_transport_phy_speed(struct sas_phy *phy, struct sas_phy_linkrates *rates)
1781{
1782 struct MPT2SAS_ADAPTER *ioc = phy_to_ioc(phy);
Kashyap, Desai50d5c602009-12-16 18:55:26 +05301783 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
1784 Mpi2SasPhyPage0_t phy_pg0;
1785 Mpi2ConfigReply_t mpi_reply;
1786 u16 ioc_status;
1787 u16 sz;
1788 int i;
1789 int rc = 0;
Kashyap, Desaib8d7d7b2010-06-17 13:34:31 +05301790 unsigned long flags;
Kashyap, Desai50d5c602009-12-16 18:55:26 +05301791
Kashyap, Desaib8d7d7b2010-06-17 13:34:31 +05301792 spin_lock_irqsave(&ioc->sas_node_lock, flags);
1793 if (_transport_sas_node_find_by_sas_address(ioc,
1794 phy->identify.sas_address) == NULL) {
1795 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
Kashyap, Desai50d5c602009-12-16 18:55:26 +05301796 return -EINVAL;
Kashyap, Desaib8d7d7b2010-06-17 13:34:31 +05301797 }
1798 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
Kashyap, Desai50d5c602009-12-16 18:55:26 +05301799
1800 if (!rates->minimum_linkrate)
1801 rates->minimum_linkrate = phy->minimum_linkrate;
1802 else if (rates->minimum_linkrate < phy->minimum_linkrate_hw)
1803 rates->minimum_linkrate = phy->minimum_linkrate_hw;
1804
1805 if (!rates->maximum_linkrate)
1806 rates->maximum_linkrate = phy->maximum_linkrate;
1807 else if (rates->maximum_linkrate > phy->maximum_linkrate_hw)
1808 rates->maximum_linkrate = phy->maximum_linkrate_hw;
1809
Kashyap, Desaib8d7d7b2010-06-17 13:34:31 +05301810 /* handle expander phys */
1811 if (phy->identify.sas_address != ioc->sas_hba.sas_address) {
1812 phy->minimum_linkrate = rates->minimum_linkrate;
1813 phy->maximum_linkrate = rates->maximum_linkrate;
1814 return _transport_expander_phy_control(ioc, phy,
1815 SMP_PHY_CONTROL_LINK_RESET);
1816 }
1817
1818 /* handle hba phys */
1819
Kashyap, Desai50d5c602009-12-16 18:55:26 +05301820 /* sas_iounit page 1 */
1821 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
1822 sizeof(Mpi2SasIOUnit1PhyData_t));
1823 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
1824 if (!sas_iounit_pg1) {
1825 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1826 ioc->name, __FILE__, __LINE__, __func__);
1827 rc = -ENOMEM;
1828 goto out;
1829 }
1830 if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
1831 sas_iounit_pg1, sz))) {
1832 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1833 ioc->name, __FILE__, __LINE__, __func__);
1834 rc = -ENXIO;
1835 goto out;
1836 }
1837 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1838 MPI2_IOCSTATUS_MASK;
1839 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1840 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1841 ioc->name, __FILE__, __LINE__, __func__);
1842 rc = -EIO;
1843 goto out;
1844 }
1845
1846 for (i = 0; i < ioc->sas_hba.num_phys; i++) {
Kashyap, Desaib8d7d7b2010-06-17 13:34:31 +05301847 if (phy->number != i) {
Kashyap, Desai50d5c602009-12-16 18:55:26 +05301848 sas_iounit_pg1->PhyData[i].MaxMinLinkRate =
1849 (ioc->sas_hba.phy[i].phy->minimum_linkrate +
1850 (ioc->sas_hba.phy[i].phy->maximum_linkrate << 4));
1851 } else {
1852 sas_iounit_pg1->PhyData[i].MaxMinLinkRate =
1853 (rates->minimum_linkrate +
1854 (rates->maximum_linkrate << 4));
1855 }
1856 }
1857
1858 if (mpt2sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1,
1859 sz)) {
1860 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1861 ioc->name, __FILE__, __LINE__, __func__);
1862 rc = -ENXIO;
1863 goto out;
1864 }
1865
1866 /* link reset */
1867 _transport_phy_reset(phy, 0);
1868
1869 /* read phy page 0, then update the rates in the sas transport phy */
1870 if (!mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
Kashyap, Desaib8d7d7b2010-06-17 13:34:31 +05301871 phy->number)) {
Kashyap, Desai50d5c602009-12-16 18:55:26 +05301872 phy->minimum_linkrate = _transport_convert_phy_link_rate(
1873 phy_pg0.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
1874 phy->maximum_linkrate = _transport_convert_phy_link_rate(
1875 phy_pg0.ProgrammedLinkRate >> 4);
1876 phy->negotiated_linkrate = _transport_convert_phy_link_rate(
1877 phy_pg0.NegotiatedLinkRate &
1878 MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
1879 }
1880
1881 out:
1882 kfree(sas_iounit_pg1);
1883 return rc;
1884}
1885
1886
1887/**
Eric Moored5d135b2009-05-18 13:02:08 -06001888 * _transport_smp_handler - transport portal for smp passthru
Eric Moore635374e2009-03-09 01:21:12 -06001889 * @shost: shost object
1890 * @rphy: sas transport rphy object
1891 * @req:
1892 *
1893 * This used primarily for smp_utils.
1894 * Example:
1895 * smp_rep_general /sys/class/bsg/expander-5:0
1896 */
1897static int
Eric Moored5d135b2009-05-18 13:02:08 -06001898_transport_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
Eric Moore635374e2009-03-09 01:21:12 -06001899 struct request *req)
1900{
1901 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1902 Mpi2SmpPassthroughRequest_t *mpi_request;
1903 Mpi2SmpPassthroughReply_t *mpi_reply;
Kent Overstreet79886132013-11-23 17:19:00 -08001904 int rc;
Eric Moore635374e2009-03-09 01:21:12 -06001905 u16 smid;
1906 u32 ioc_state;
1907 unsigned long timeleft;
1908 void *psge;
1909 u32 sgl_flags;
1910 u8 issue_reset = 0;
Eric Moore635374e2009-03-09 01:21:12 -06001911 dma_addr_t dma_addr_in = 0;
1912 dma_addr_t dma_addr_out = 0;
nagalakshmi.nandigama@lsi.com77a6edf2012-03-20 12:04:43 +05301913 dma_addr_t pci_dma_in = 0;
1914 dma_addr_t pci_dma_out = 0;
1915 void *pci_addr_in = NULL;
1916 void *pci_addr_out = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06001917 u16 wait_state_count;
1918 struct request *rsp = req->next_rq;
Kent Overstreet79886132013-11-23 17:19:00 -08001919 struct bio_vec bvec;
1920 struct bvec_iter iter;
Eric Moore635374e2009-03-09 01:21:12 -06001921
1922 if (!rsp) {
1923 printk(MPT2SAS_ERR_FMT "%s: the smp response space is "
1924 "missing\n", ioc->name, __func__);
1925 return -EINVAL;
1926 }
nagalakshmi.nandigama@lsi.com77a6edf2012-03-20 12:04:43 +05301927 if (ioc->shost_recovery || ioc->pci_error_recovery) {
Eric Moore635374e2009-03-09 01:21:12 -06001928 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
1929 __func__, ioc->name);
1930 return -EFAULT;
1931 }
Eric Moore635374e2009-03-09 01:21:12 -06001932
1933 rc = mutex_lock_interruptible(&ioc->transport_cmds.mutex);
1934 if (rc)
1935 return rc;
1936
1937 if (ioc->transport_cmds.status != MPT2_CMD_NOT_USED) {
1938 printk(MPT2SAS_ERR_FMT "%s: transport_cmds in use\n", ioc->name,
1939 __func__);
1940 rc = -EAGAIN;
1941 goto out;
1942 }
1943 ioc->transport_cmds.status = MPT2_CMD_PENDING;
1944
nagalakshmi.nandigama@lsi.com77a6edf2012-03-20 12:04:43 +05301945 /* Check if the request is split across multiple segments */
Kent Overstreet2f477872012-09-05 17:31:42 -07001946 if (bio_segments(req->bio) > 1) {
nagalakshmi.nandigama@lsi.com77a6edf2012-03-20 12:04:43 +05301947 u32 offset = 0;
1948
1949 /* Allocate memory and copy the request */
1950 pci_addr_out = pci_alloc_consistent(ioc->pdev,
1951 blk_rq_bytes(req), &pci_dma_out);
1952 if (!pci_addr_out) {
1953 printk(MPT2SAS_INFO_FMT "%s(): PCI Addr out = NULL\n",
1954 ioc->name, __func__);
1955 rc = -ENOMEM;
1956 goto out;
1957 }
1958
Kent Overstreet79886132013-11-23 17:19:00 -08001959 bio_for_each_segment(bvec, req->bio, iter) {
nagalakshmi.nandigama@lsi.com77a6edf2012-03-20 12:04:43 +05301960 memcpy(pci_addr_out + offset,
Kent Overstreet79886132013-11-23 17:19:00 -08001961 page_address(bvec.bv_page) + bvec.bv_offset,
1962 bvec.bv_len);
1963 offset += bvec.bv_len;
nagalakshmi.nandigama@lsi.com77a6edf2012-03-20 12:04:43 +05301964 }
1965 } else {
1966 dma_addr_out = pci_map_single(ioc->pdev, bio_data(req->bio),
1967 blk_rq_bytes(req), PCI_DMA_BIDIRECTIONAL);
1968 if (!dma_addr_out) {
1969 printk(MPT2SAS_INFO_FMT "%s(): DMA Addr out = NULL\n",
1970 ioc->name, __func__);
1971 rc = -ENOMEM;
1972 goto free_pci;
1973 }
1974 }
1975
1976 /* Check if the response needs to be populated across
1977 * multiple segments */
Kent Overstreet2f477872012-09-05 17:31:42 -07001978 if (bio_segments(rsp->bio) > 1) {
nagalakshmi.nandigama@lsi.com77a6edf2012-03-20 12:04:43 +05301979 pci_addr_in = pci_alloc_consistent(ioc->pdev, blk_rq_bytes(rsp),
1980 &pci_dma_in);
1981 if (!pci_addr_in) {
1982 printk(MPT2SAS_INFO_FMT "%s(): PCI Addr in = NULL\n",
1983 ioc->name, __func__);
1984 rc = -ENOMEM;
1985 goto unmap;
1986 }
1987 } else {
1988 dma_addr_in = pci_map_single(ioc->pdev, bio_data(rsp->bio),
1989 blk_rq_bytes(rsp), PCI_DMA_BIDIRECTIONAL);
1990 if (!dma_addr_in) {
1991 printk(MPT2SAS_INFO_FMT "%s(): DMA Addr in = NULL\n",
1992 ioc->name, __func__);
1993 rc = -ENOMEM;
1994 goto unmap;
1995 }
1996 }
1997
Eric Moore635374e2009-03-09 01:21:12 -06001998 wait_state_count = 0;
1999 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
2000 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
2001 if (wait_state_count++ == 10) {
2002 printk(MPT2SAS_ERR_FMT
2003 "%s: failed due to ioc not operational\n",
2004 ioc->name, __func__);
2005 rc = -EFAULT;
nagalakshmi.nandigama@lsi.com77a6edf2012-03-20 12:04:43 +05302006 goto unmap;
Eric Moore635374e2009-03-09 01:21:12 -06002007 }
2008 ssleep(1);
2009 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
2010 printk(MPT2SAS_INFO_FMT "%s: waiting for "
2011 "operational state(count=%d)\n", ioc->name,
2012 __func__, wait_state_count);
2013 }
2014 if (wait_state_count)
2015 printk(MPT2SAS_INFO_FMT "%s: ioc is operational\n",
2016 ioc->name, __func__);
2017
2018 smid = mpt2sas_base_get_smid(ioc, ioc->transport_cb_idx);
2019 if (!smid) {
2020 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2021 ioc->name, __func__);
2022 rc = -EAGAIN;
nagalakshmi.nandigama@lsi.com77a6edf2012-03-20 12:04:43 +05302023 goto unmap;
Eric Moore635374e2009-03-09 01:21:12 -06002024 }
2025
2026 rc = 0;
2027 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2028 ioc->transport_cmds.smid = smid;
2029
2030 memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
2031 mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
2032 mpi_request->PhysicalPort = 0xFF;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302033 mpi_request->VF_ID = 0; /* TODO */
2034 mpi_request->VP_ID = 0;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05302035 mpi_request->SASAddress = (rphy) ?
Eric Moore635374e2009-03-09 01:21:12 -06002036 cpu_to_le64(rphy->identify.sas_address) :
2037 cpu_to_le64(ioc->sas_hba.sas_address);
Tejun Heob0790412009-05-07 22:24:42 +09002038 mpi_request->RequestDataLength = cpu_to_le16(blk_rq_bytes(req) - 4);
Eric Moore635374e2009-03-09 01:21:12 -06002039 psge = &mpi_request->SGL;
2040
2041 /* WRITE sgel first */
2042 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
2043 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
2044 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
Kent Overstreet2f477872012-09-05 17:31:42 -07002045 if (bio_segments(req->bio) > 1) {
nagalakshmi.nandigama@lsi.com77a6edf2012-03-20 12:04:43 +05302046 ioc->base_add_sg_single(psge, sgl_flags |
2047 (blk_rq_bytes(req) - 4), pci_dma_out);
2048 } else {
2049 ioc->base_add_sg_single(psge, sgl_flags |
2050 (blk_rq_bytes(req) - 4), dma_addr_out);
Eric Moore635374e2009-03-09 01:21:12 -06002051 }
2052
Eric Moore635374e2009-03-09 01:21:12 -06002053 /* incr sgel */
2054 psge += ioc->sge_size;
2055
2056 /* READ sgel last */
2057 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
2058 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
2059 MPI2_SGE_FLAGS_END_OF_LIST);
2060 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
Kent Overstreet2f477872012-09-05 17:31:42 -07002061 if (bio_segments(rsp->bio) > 1) {
nagalakshmi.nandigama@lsi.com77a6edf2012-03-20 12:04:43 +05302062 ioc->base_add_sg_single(psge, sgl_flags |
2063 (blk_rq_bytes(rsp) + 4), pci_dma_in);
2064 } else {
2065 ioc->base_add_sg_single(psge, sgl_flags |
2066 (blk_rq_bytes(rsp) + 4), dma_addr_in);
Eric Moore635374e2009-03-09 01:21:12 -06002067 }
2068
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302069 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "%s - "
Eric Moore635374e2009-03-09 01:21:12 -06002070 "sending smp request\n", ioc->name, __func__));
2071
Kashyap, Desaibcfb6e62009-09-14 11:05:24 +05302072 init_completion(&ioc->transport_cmds.done);
nagalakshmi.nandigama@lsi.comf01690d2011-12-01 07:43:58 +05302073 mpt2sas_base_put_smid_default(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06002074 timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done,
2075 10*HZ);
2076
2077 if (!(ioc->transport_cmds.status & MPT2_CMD_COMPLETE)) {
2078 printk(MPT2SAS_ERR_FMT "%s : timeout\n",
2079 __func__, ioc->name);
2080 _debug_dump_mf(mpi_request,
2081 sizeof(Mpi2SmpPassthroughRequest_t)/4);
2082 if (!(ioc->transport_cmds.status & MPT2_CMD_RESET))
2083 issue_reset = 1;
2084 goto issue_host_reset;
2085 }
2086
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302087 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "%s - "
Eric Moore635374e2009-03-09 01:21:12 -06002088 "complete\n", ioc->name, __func__));
2089
2090 if (ioc->transport_cmds.status & MPT2_CMD_REPLY_VALID) {
2091
2092 mpi_reply = ioc->transport_cmds.reply;
2093
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302094 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -06002095 "%s - reply data transfer size(%d)\n",
2096 ioc->name, __func__,
2097 le16_to_cpu(mpi_reply->ResponseDataLength)));
2098
2099 memcpy(req->sense, mpi_reply, sizeof(*mpi_reply));
2100 req->sense_len = sizeof(*mpi_reply);
Tejun Heo5f49f632009-05-19 18:33:05 +09002101 req->resid_len = 0;
Kashyap, Desaie94f6742010-03-17 16:24:52 +05302102 rsp->resid_len -=
2103 le16_to_cpu(mpi_reply->ResponseDataLength);
nagalakshmi.nandigama@lsi.com77a6edf2012-03-20 12:04:43 +05302104 /* check if the resp needs to be copied from the allocated
2105 * pci mem */
Kent Overstreet2f477872012-09-05 17:31:42 -07002106 if (bio_segments(rsp->bio) > 1) {
nagalakshmi.nandigama@lsi.com77a6edf2012-03-20 12:04:43 +05302107 u32 offset = 0;
2108 u32 bytes_to_copy =
2109 le16_to_cpu(mpi_reply->ResponseDataLength);
Kent Overstreet79886132013-11-23 17:19:00 -08002110 bio_for_each_segment(bvec, rsp->bio, iter) {
2111 if (bytes_to_copy <= bvec.bv_len) {
2112 memcpy(page_address(bvec.bv_page) +
2113 bvec.bv_offset, pci_addr_in +
nagalakshmi.nandigama@lsi.com77a6edf2012-03-20 12:04:43 +05302114 offset, bytes_to_copy);
2115 break;
2116 } else {
Kent Overstreet79886132013-11-23 17:19:00 -08002117 memcpy(page_address(bvec.bv_page) +
2118 bvec.bv_offset, pci_addr_in +
2119 offset, bvec.bv_len);
2120 bytes_to_copy -= bvec.bv_len;
nagalakshmi.nandigama@lsi.com77a6edf2012-03-20 12:04:43 +05302121 }
Kent Overstreet79886132013-11-23 17:19:00 -08002122 offset += bvec.bv_len;
nagalakshmi.nandigama@lsi.com77a6edf2012-03-20 12:04:43 +05302123 }
2124 }
Eric Moore635374e2009-03-09 01:21:12 -06002125 } else {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302126 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -06002127 "%s - no reply\n", ioc->name, __func__));
2128 rc = -ENXIO;
2129 }
2130
2131 issue_host_reset:
2132 if (issue_reset) {
2133 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2134 FORCE_BIG_HAMMER);
2135 rc = -ETIMEDOUT;
2136 }
2137
2138 unmap:
2139 if (dma_addr_out)
Tejun Heob0790412009-05-07 22:24:42 +09002140 pci_unmap_single(ioc->pdev, dma_addr_out, blk_rq_bytes(req),
Eric Moore635374e2009-03-09 01:21:12 -06002141 PCI_DMA_BIDIRECTIONAL);
2142 if (dma_addr_in)
Tejun Heob0790412009-05-07 22:24:42 +09002143 pci_unmap_single(ioc->pdev, dma_addr_in, blk_rq_bytes(rsp),
Eric Moore635374e2009-03-09 01:21:12 -06002144 PCI_DMA_BIDIRECTIONAL);
2145
nagalakshmi.nandigama@lsi.com77a6edf2012-03-20 12:04:43 +05302146 free_pci:
2147 if (pci_addr_out)
2148 pci_free_consistent(ioc->pdev, blk_rq_bytes(req), pci_addr_out,
2149 pci_dma_out);
2150
2151 if (pci_addr_in)
2152 pci_free_consistent(ioc->pdev, blk_rq_bytes(rsp), pci_addr_in,
2153 pci_dma_in);
2154
Eric Moore635374e2009-03-09 01:21:12 -06002155 out:
2156 ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
2157 mutex_unlock(&ioc->transport_cmds.mutex);
2158 return rc;
2159}
2160
2161struct sas_function_template mpt2sas_transport_functions = {
Eric Moored5d135b2009-05-18 13:02:08 -06002162 .get_linkerrors = _transport_get_linkerrors,
2163 .get_enclosure_identifier = _transport_get_enclosure_identifier,
2164 .get_bay_identifier = _transport_get_bay_identifier,
2165 .phy_reset = _transport_phy_reset,
Kashyap, Desai50d5c602009-12-16 18:55:26 +05302166 .phy_enable = _transport_phy_enable,
2167 .set_phy_speed = _transport_phy_speed,
Eric Moored5d135b2009-05-18 13:02:08 -06002168 .smp_handler = _transport_smp_handler,
Eric Moore635374e2009-03-09 01:21:12 -06002169};
2170
2171struct scsi_transport_template *mpt2sas_transport_template;