blob: 865deb9a5e5a92fd3bef2459839b7d74f23f4485 [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
Kashyap, Desai31b7f2e2010-03-17 16:28:04 +05305 * Copyright (C) 2007-2010 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
172 /* device_type */
173 switch (device_info & MPI2_SAS_DEVICE_INFO_MASK_DEVICE_TYPE) {
174 case MPI2_SAS_DEVICE_INFO_NO_DEVICE:
175 identify->device_type = SAS_PHY_UNUSED;
176 break;
177 case MPI2_SAS_DEVICE_INFO_END_DEVICE:
178 identify->device_type = SAS_END_DEVICE;
179 break;
180 case MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER:
181 identify->device_type = SAS_EDGE_EXPANDER_DEVICE;
182 break;
183 case MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER:
184 identify->device_type = SAS_FANOUT_EXPANDER_DEVICE;
185 break;
186 }
187
188 /* initiator_port_protocols */
189 if (device_info & MPI2_SAS_DEVICE_INFO_SSP_INITIATOR)
190 identify->initiator_port_protocols |= SAS_PROTOCOL_SSP;
191 if (device_info & MPI2_SAS_DEVICE_INFO_STP_INITIATOR)
192 identify->initiator_port_protocols |= SAS_PROTOCOL_STP;
193 if (device_info & MPI2_SAS_DEVICE_INFO_SMP_INITIATOR)
194 identify->initiator_port_protocols |= SAS_PROTOCOL_SMP;
195 if (device_info & MPI2_SAS_DEVICE_INFO_SATA_HOST)
196 identify->initiator_port_protocols |= SAS_PROTOCOL_SATA;
197
198 /* target_port_protocols */
199 if (device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET)
200 identify->target_port_protocols |= SAS_PROTOCOL_SSP;
201 if (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET)
202 identify->target_port_protocols |= SAS_PROTOCOL_STP;
203 if (device_info & MPI2_SAS_DEVICE_INFO_SMP_TARGET)
204 identify->target_port_protocols |= SAS_PROTOCOL_SMP;
205 if (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
206 identify->target_port_protocols |= SAS_PROTOCOL_SATA;
207
208 return 0;
209}
210
211/**
212 * mpt2sas_transport_done - internal transport layer callback handler.
213 * @ioc: per adapter object
214 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530215 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -0600216 * @reply: reply message frame(lower 32bit addr)
217 *
218 * Callback handler when sending internal generated transport cmds.
219 * The callback index passed is `ioc->transport_cb_idx`
220 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530221 * Return 1 meaning mf should be freed from _base_interrupt
222 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -0600223 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530224u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530225mpt2sas_transport_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
Eric Moore635374e2009-03-09 01:21:12 -0600226 u32 reply)
227{
228 MPI2DefaultReply_t *mpi_reply;
229
230 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
231 if (ioc->transport_cmds.status == MPT2_CMD_NOT_USED)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530232 return 1;
Eric Moore635374e2009-03-09 01:21:12 -0600233 if (ioc->transport_cmds.smid != smid)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530234 return 1;
Eric Moore635374e2009-03-09 01:21:12 -0600235 ioc->transport_cmds.status |= MPT2_CMD_COMPLETE;
236 if (mpi_reply) {
237 memcpy(ioc->transport_cmds.reply, mpi_reply,
238 mpi_reply->MsgLength*4);
239 ioc->transport_cmds.status |= MPT2_CMD_REPLY_VALID;
240 }
241 ioc->transport_cmds.status &= ~MPT2_CMD_PENDING;
242 complete(&ioc->transport_cmds.done);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530243 return 1;
Eric Moore635374e2009-03-09 01:21:12 -0600244}
245
246/* report manufacture request structure */
247struct rep_manu_request{
248 u8 smp_frame_type;
249 u8 function;
250 u8 reserved;
251 u8 request_length;
252};
253
254/* report manufacture reply structure */
255struct rep_manu_reply{
256 u8 smp_frame_type; /* 0x41 */
257 u8 function; /* 0x01 */
258 u8 function_result;
259 u8 response_length;
260 u16 expander_change_count;
261 u8 reserved0[2];
Kashyap, Desai463217b2009-10-05 15:53:06 +0530262 u8 sas_format;
Eric Moore635374e2009-03-09 01:21:12 -0600263 u8 reserved2[3];
264 u8 vendor_id[SAS_EXPANDER_VENDOR_ID_LEN];
265 u8 product_id[SAS_EXPANDER_PRODUCT_ID_LEN];
266 u8 product_rev[SAS_EXPANDER_PRODUCT_REV_LEN];
267 u8 component_vendor_id[SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN];
268 u16 component_id;
269 u8 component_revision_id;
270 u8 reserved3;
271 u8 vendor_specific[8];
272};
273
274/**
Eric Moored5d135b2009-05-18 13:02:08 -0600275 * _transport_expander_report_manufacture - obtain SMP report_manufacture
Eric Moore635374e2009-03-09 01:21:12 -0600276 * @ioc: per adapter object
277 * @sas_address: expander sas address
278 * @edev: the sas_expander_device object
279 *
280 * Fills in the sas_expander_device object when SMP port is created.
281 *
282 * Returns 0 for success, non-zero for failure.
283 */
284static int
Eric Moored5d135b2009-05-18 13:02:08 -0600285_transport_expander_report_manufacture(struct MPT2SAS_ADAPTER *ioc,
Eric Moore635374e2009-03-09 01:21:12 -0600286 u64 sas_address, struct sas_expander_device *edev)
287{
288 Mpi2SmpPassthroughRequest_t *mpi_request;
289 Mpi2SmpPassthroughReply_t *mpi_reply;
290 struct rep_manu_reply *manufacture_reply;
291 struct rep_manu_request *manufacture_request;
292 int rc;
293 u16 smid;
294 u32 ioc_state;
295 unsigned long timeleft;
296 void *psge;
297 u32 sgl_flags;
298 u8 issue_reset = 0;
Eric Moore635374e2009-03-09 01:21:12 -0600299 void *data_out = NULL;
300 dma_addr_t data_out_dma;
301 u32 sz;
Eric Moore635374e2009-03-09 01:21:12 -0600302 u16 wait_state_count;
303
Eric Moore3cb54692010-07-08 14:44:34 -0600304 if (ioc->shost_recovery || ioc->pci_error_recovery) {
Eric Moore635374e2009-03-09 01:21:12 -0600305 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
306 __func__, ioc->name);
307 return -EFAULT;
308 }
Eric Moore635374e2009-03-09 01:21:12 -0600309
310 mutex_lock(&ioc->transport_cmds.mutex);
311
312 if (ioc->transport_cmds.status != MPT2_CMD_NOT_USED) {
313 printk(MPT2SAS_ERR_FMT "%s: transport_cmds in use\n",
314 ioc->name, __func__);
315 rc = -EAGAIN;
316 goto out;
317 }
318 ioc->transport_cmds.status = MPT2_CMD_PENDING;
319
320 wait_state_count = 0;
321 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
322 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
323 if (wait_state_count++ == 10) {
324 printk(MPT2SAS_ERR_FMT
325 "%s: failed due to ioc not operational\n",
326 ioc->name, __func__);
327 rc = -EFAULT;
328 goto out;
329 }
330 ssleep(1);
331 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
332 printk(MPT2SAS_INFO_FMT "%s: waiting for "
333 "operational state(count=%d)\n", ioc->name,
334 __func__, wait_state_count);
335 }
336 if (wait_state_count)
337 printk(MPT2SAS_INFO_FMT "%s: ioc is operational\n",
338 ioc->name, __func__);
339
340 smid = mpt2sas_base_get_smid(ioc, ioc->transport_cb_idx);
341 if (!smid) {
342 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
343 ioc->name, __func__);
344 rc = -EAGAIN;
345 goto out;
346 }
347
348 rc = 0;
349 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
350 ioc->transport_cmds.smid = smid;
351
352 sz = sizeof(struct rep_manu_request) + sizeof(struct rep_manu_reply);
353 data_out = pci_alloc_consistent(ioc->pdev, sz, &data_out_dma);
354
355 if (!data_out) {
356 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
357 __LINE__, __func__);
358 rc = -ENOMEM;
359 mpt2sas_base_free_smid(ioc, smid);
360 goto out;
361 }
362
363 manufacture_request = data_out;
364 manufacture_request->smp_frame_type = 0x40;
365 manufacture_request->function = 1;
366 manufacture_request->reserved = 0;
367 manufacture_request->request_length = 0;
368
369 memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
370 mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
371 mpi_request->PhysicalPort = 0xFF;
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530372 mpi_request->VF_ID = 0; /* TODO */
373 mpi_request->VP_ID = 0;
Kashyap, Desaic97951e2011-06-14 10:54:56 +0530374 mpi_request->SASAddress = cpu_to_le64(sas_address);
Kashyap, Desai463217b2009-10-05 15:53:06 +0530375 mpi_request->RequestDataLength =
376 cpu_to_le16(sizeof(struct rep_manu_request));
Eric Moore635374e2009-03-09 01:21:12 -0600377 psge = &mpi_request->SGL;
378
379 /* WRITE sgel first */
380 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
381 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
382 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
383 ioc->base_add_sg_single(psge, sgl_flags |
384 sizeof(struct rep_manu_request), data_out_dma);
385
386 /* incr sgel */
387 psge += ioc->sge_size;
388
389 /* READ sgel last */
390 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
391 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
392 MPI2_SGE_FLAGS_END_OF_LIST);
393 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
394 ioc->base_add_sg_single(psge, sgl_flags |
395 sizeof(struct rep_manu_reply), data_out_dma +
396 sizeof(struct rep_manu_request));
397
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530398 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "report_manufacture - "
Eric Moore635374e2009-03-09 01:21:12 -0600399 "send to sas_addr(0x%016llx)\n", ioc->name,
400 (unsigned long long)sas_address));
Kashyap, Desaibcfb6e62009-09-14 11:05:24 +0530401 init_completion(&ioc->transport_cmds.done);
nagalakshmi.nandigama@lsi.comf01690d2011-12-01 07:43:58 +0530402 mpt2sas_base_put_smid_default(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -0600403 timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done,
404 10*HZ);
405
406 if (!(ioc->transport_cmds.status & MPT2_CMD_COMPLETE)) {
407 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
408 ioc->name, __func__);
409 _debug_dump_mf(mpi_request,
410 sizeof(Mpi2SmpPassthroughRequest_t)/4);
411 if (!(ioc->transport_cmds.status & MPT2_CMD_RESET))
412 issue_reset = 1;
413 goto issue_host_reset;
414 }
415
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530416 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "report_manufacture - "
Eric Moore635374e2009-03-09 01:21:12 -0600417 "complete\n", ioc->name));
418
419 if (ioc->transport_cmds.status & MPT2_CMD_REPLY_VALID) {
420 u8 *tmp;
421
422 mpi_reply = ioc->transport_cmds.reply;
423
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530424 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -0600425 "report_manufacture - reply data transfer size(%d)\n",
426 ioc->name, le16_to_cpu(mpi_reply->ResponseDataLength)));
427
428 if (le16_to_cpu(mpi_reply->ResponseDataLength) !=
429 sizeof(struct rep_manu_reply))
430 goto out;
431
432 manufacture_reply = data_out + sizeof(struct rep_manu_request);
433 strncpy(edev->vendor_id, manufacture_reply->vendor_id,
434 SAS_EXPANDER_VENDOR_ID_LEN);
435 strncpy(edev->product_id, manufacture_reply->product_id,
436 SAS_EXPANDER_PRODUCT_ID_LEN);
437 strncpy(edev->product_rev, manufacture_reply->product_rev,
438 SAS_EXPANDER_PRODUCT_REV_LEN);
Kashyap, Desai463217b2009-10-05 15:53:06 +0530439 edev->level = manufacture_reply->sas_format & 1;
440 if (edev->level) {
Eric Moore635374e2009-03-09 01:21:12 -0600441 strncpy(edev->component_vendor_id,
442 manufacture_reply->component_vendor_id,
443 SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN);
444 tmp = (u8 *)&manufacture_reply->component_id;
445 edev->component_id = tmp[0] << 8 | tmp[1];
446 edev->component_revision_id =
447 manufacture_reply->component_revision_id;
448 }
449 } else
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530450 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -0600451 "report_manufacture - no reply\n", ioc->name));
452
453 issue_host_reset:
454 if (issue_reset)
455 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
456 FORCE_BIG_HAMMER);
457 out:
458 ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
459 if (data_out)
460 pci_free_consistent(ioc->pdev, sz, data_out, data_out_dma);
461
462 mutex_unlock(&ioc->transport_cmds.mutex);
463 return rc;
464}
465
Kashyap, Desai38c29112010-04-05 14:18:34 +0530466/**
Kashyap, Desai7f6f7942010-11-13 04:35:30 +0530467 * _transport_delete_port - helper function to removing a port
Kashyap, Desai38c29112010-04-05 14:18:34 +0530468 * @ioc: per adapter object
Kashyap, Desai7f6f7942010-11-13 04:35:30 +0530469 * @mpt2sas_port: mpt2sas per port object
Kashyap, Desai38c29112010-04-05 14:18:34 +0530470 *
Kashyap, Desai7f6f7942010-11-13 04:35:30 +0530471 * Returns nothing.
Kashyap, Desai38c29112010-04-05 14:18:34 +0530472 */
473static void
Kashyap, Desai7f6f7942010-11-13 04:35:30 +0530474_transport_delete_port(struct MPT2SAS_ADAPTER *ioc,
475 struct _sas_port *mpt2sas_port)
Kashyap, Desai38c29112010-04-05 14:18:34 +0530476{
Kashyap, Desai7f6f7942010-11-13 04:35:30 +0530477 u64 sas_address = mpt2sas_port->remote_identify.sas_address;
478 enum sas_device_type device_type =
479 mpt2sas_port->remote_identify.device_type;
Kashyap, Desai38c29112010-04-05 14:18:34 +0530480
Kashyap, Desai7f6f7942010-11-13 04:35:30 +0530481 dev_printk(KERN_INFO, &mpt2sas_port->port->dev,
482 "remove: sas_addr(0x%016llx)\n",
483 (unsigned long long) sas_address);
Kashyap, Desai38c29112010-04-05 14:18:34 +0530484
Kashyap, Desai7f6f7942010-11-13 04:35:30 +0530485 ioc->logging_level |= MPT_DEBUG_TRANSPORT;
486 if (device_type == SAS_END_DEVICE)
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +0530487 mpt2sas_device_remove_by_sas_address(ioc, sas_address);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +0530488 else if (device_type == SAS_EDGE_EXPANDER_DEVICE ||
489 device_type == SAS_FANOUT_EXPANDER_DEVICE)
490 mpt2sas_expander_remove(ioc, sas_address);
491 ioc->logging_level &= ~MPT_DEBUG_TRANSPORT;
492}
Kashyap, Desai38c29112010-04-05 14:18:34 +0530493
Kashyap, Desai7f6f7942010-11-13 04:35:30 +0530494/**
495 * _transport_delete_phy - helper function to removing single phy from port
496 * @ioc: per adapter object
497 * @mpt2sas_port: mpt2sas per port object
498 * @mpt2sas_phy: mpt2sas per phy object
499 *
500 * Returns nothing.
501 */
502static void
503_transport_delete_phy(struct MPT2SAS_ADAPTER *ioc,
504 struct _sas_port *mpt2sas_port, struct _sas_phy *mpt2sas_phy)
505{
506 u64 sas_address = mpt2sas_port->remote_identify.sas_address;
507
508 dev_printk(KERN_INFO, &mpt2sas_phy->phy->dev,
509 "remove: sas_addr(0x%016llx), phy(%d)\n",
510 (unsigned long long) sas_address, mpt2sas_phy->phy_id);
511
512 list_del(&mpt2sas_phy->port_siblings);
513 mpt2sas_port->num_phys--;
514 sas_port_delete_phy(mpt2sas_port->port, mpt2sas_phy->phy);
515 mpt2sas_phy->phy_belongs_to_port = 0;
516}
517
518/**
519 * _transport_add_phy - helper function to adding single phy to port
520 * @ioc: per adapter object
521 * @mpt2sas_port: mpt2sas per port object
522 * @mpt2sas_phy: mpt2sas per phy object
523 *
524 * Returns nothing.
525 */
526static void
527_transport_add_phy(struct MPT2SAS_ADAPTER *ioc, struct _sas_port *mpt2sas_port,
528 struct _sas_phy *mpt2sas_phy)
529{
530 u64 sas_address = mpt2sas_port->remote_identify.sas_address;
531
532 dev_printk(KERN_INFO, &mpt2sas_phy->phy->dev,
533 "add: sas_addr(0x%016llx), phy(%d)\n", (unsigned long long)
534 sas_address, mpt2sas_phy->phy_id);
535
536 list_add_tail(&mpt2sas_phy->port_siblings, &mpt2sas_port->phy_list);
537 mpt2sas_port->num_phys++;
538 sas_port_add_phy(mpt2sas_port->port, mpt2sas_phy->phy);
539 mpt2sas_phy->phy_belongs_to_port = 1;
540}
541
542/**
543 * _transport_add_phy_to_an_existing_port - adding new phy to existing port
544 * @ioc: per adapter object
545 * @sas_node: sas node object (either expander or sas host)
546 * @mpt2sas_phy: mpt2sas per phy object
547 * @sas_address: sas address of device/expander were phy needs to be added to
548 *
549 * Returns nothing.
550 */
551static void
552_transport_add_phy_to_an_existing_port(struct MPT2SAS_ADAPTER *ioc,
553struct _sas_node *sas_node, struct _sas_phy *mpt2sas_phy, u64 sas_address)
554{
555 struct _sas_port *mpt2sas_port;
556 struct _sas_phy *phy_srch;
557
558 if (mpt2sas_phy->phy_belongs_to_port == 1)
Kashyap, Desai38c29112010-04-05 14:18:34 +0530559 return;
560
Kashyap, Desai7f6f7942010-11-13 04:35:30 +0530561 list_for_each_entry(mpt2sas_port, &sas_node->sas_port_list,
562 port_list) {
563 if (mpt2sas_port->remote_identify.sas_address !=
564 sas_address)
565 continue;
566 list_for_each_entry(phy_srch, &mpt2sas_port->phy_list,
567 port_siblings) {
568 if (phy_srch == mpt2sas_phy)
569 return;
570 }
571 _transport_add_phy(ioc, mpt2sas_port, mpt2sas_phy);
572 return;
573 }
574
575}
576
577/**
578 * _transport_del_phy_from_an_existing_port - delete phy from existing port
579 * @ioc: per adapter object
580 * @sas_node: sas node object (either expander or sas host)
581 * @mpt2sas_phy: mpt2sas per phy object
582 *
583 * Returns nothing.
584 */
585static void
586_transport_del_phy_from_an_existing_port(struct MPT2SAS_ADAPTER *ioc,
587 struct _sas_node *sas_node, struct _sas_phy *mpt2sas_phy)
588{
589 struct _sas_port *mpt2sas_port, *next;
590 struct _sas_phy *phy_srch;
591
592 if (mpt2sas_phy->phy_belongs_to_port == 0)
593 return;
594
595 list_for_each_entry_safe(mpt2sas_port, next, &sas_node->sas_port_list,
596 port_list) {
597 list_for_each_entry(phy_srch, &mpt2sas_port->phy_list,
598 port_siblings) {
599 if (phy_srch != mpt2sas_phy)
600 continue;
601 if (mpt2sas_port->num_phys == 1)
602 _transport_delete_port(ioc, mpt2sas_port);
603 else
604 _transport_delete_phy(ioc, mpt2sas_port,
605 mpt2sas_phy);
606 return;
607 }
608 }
Kashyap, Desai38c29112010-04-05 14:18:34 +0530609}
610
611/**
612 * _transport_sanity_check - sanity check when adding a new port
613 * @ioc: per adapter object
614 * @sas_node: sas node object (either expander or sas host)
615 * @sas_address: sas address of device being added
616 *
617 * See the explanation above from _transport_delete_duplicate_port
618 */
619static void
620_transport_sanity_check(struct MPT2SAS_ADAPTER *ioc, struct _sas_node *sas_node,
621 u64 sas_address)
622{
623 int i;
624
Kashyap, Desai7f6f7942010-11-13 04:35:30 +0530625 for (i = 0; i < sas_node->num_phys; i++) {
626 if (sas_node->phy[i].remote_identify.sas_address != sas_address)
627 continue;
628 if (sas_node->phy[i].phy_belongs_to_port == 1)
629 _transport_del_phy_from_an_existing_port(ioc, sas_node,
630 &sas_node->phy[i]);
631 }
Kashyap, Desai38c29112010-04-05 14:18:34 +0530632}
633
Eric Moore635374e2009-03-09 01:21:12 -0600634/**
635 * mpt2sas_transport_port_add - insert port to the list
636 * @ioc: per adapter object
637 * @handle: handle of attached device
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530638 * @sas_address: sas address of parent expander or sas host
Eric Moore635374e2009-03-09 01:21:12 -0600639 * Context: This function will acquire ioc->sas_node_lock.
640 *
641 * Adding new port object to the sas_node->sas_port_list.
642 *
643 * Returns mpt2sas_port.
644 */
645struct _sas_port *
646mpt2sas_transport_port_add(struct MPT2SAS_ADAPTER *ioc, u16 handle,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530647 u64 sas_address)
Eric Moore635374e2009-03-09 01:21:12 -0600648{
649 struct _sas_phy *mpt2sas_phy, *next;
650 struct _sas_port *mpt2sas_port;
651 unsigned long flags;
652 struct _sas_node *sas_node;
653 struct sas_rphy *rphy;
654 int i;
655 struct sas_port *port;
656
Eric Moore635374e2009-03-09 01:21:12 -0600657 mpt2sas_port = kzalloc(sizeof(struct _sas_port),
658 GFP_KERNEL);
659 if (!mpt2sas_port) {
660 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
661 ioc->name, __FILE__, __LINE__, __func__);
662 return NULL;
663 }
664
665 INIT_LIST_HEAD(&mpt2sas_port->port_list);
666 INIT_LIST_HEAD(&mpt2sas_port->phy_list);
667 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530668 sas_node = _transport_sas_node_find_by_sas_address(ioc, sas_address);
Eric Moore635374e2009-03-09 01:21:12 -0600669 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
670
671 if (!sas_node) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530672 printk(MPT2SAS_ERR_FMT "%s: Could not find "
673 "parent sas_address(0x%016llx)!\n", ioc->name,
674 __func__, (unsigned long long)sas_address);
Eric Moore635374e2009-03-09 01:21:12 -0600675 goto out_fail;
676 }
677
Eric Moore635374e2009-03-09 01:21:12 -0600678 if ((_transport_set_identify(ioc, handle,
679 &mpt2sas_port->remote_identify))) {
680 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
681 ioc->name, __FILE__, __LINE__, __func__);
682 goto out_fail;
683 }
684
685 if (mpt2sas_port->remote_identify.device_type == SAS_PHY_UNUSED) {
686 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
687 ioc->name, __FILE__, __LINE__, __func__);
688 goto out_fail;
689 }
690
Kashyap, Desai38c29112010-04-05 14:18:34 +0530691 _transport_sanity_check(ioc, sas_node,
692 mpt2sas_port->remote_identify.sas_address);
693
Eric Moore635374e2009-03-09 01:21:12 -0600694 for (i = 0; i < sas_node->num_phys; i++) {
695 if (sas_node->phy[i].remote_identify.sas_address !=
696 mpt2sas_port->remote_identify.sas_address)
697 continue;
698 list_add_tail(&sas_node->phy[i].port_siblings,
699 &mpt2sas_port->phy_list);
700 mpt2sas_port->num_phys++;
701 }
702
703 if (!mpt2sas_port->num_phys) {
704 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
705 ioc->name, __FILE__, __LINE__, __func__);
706 goto out_fail;
707 }
708
709 port = sas_port_alloc_num(sas_node->parent_dev);
710 if ((sas_port_add(port))) {
711 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
712 ioc->name, __FILE__, __LINE__, __func__);
713 goto out_fail;
714 }
715
716 list_for_each_entry(mpt2sas_phy, &mpt2sas_port->phy_list,
717 port_siblings) {
718 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
719 dev_printk(KERN_INFO, &port->dev, "add: handle(0x%04x)"
720 ", sas_addr(0x%016llx), phy(%d)\n", handle,
721 (unsigned long long)
722 mpt2sas_port->remote_identify.sas_address,
723 mpt2sas_phy->phy_id);
724 sas_port_add_phy(port, mpt2sas_phy->phy);
Kashyap, Desai38c29112010-04-05 14:18:34 +0530725 mpt2sas_phy->phy_belongs_to_port = 1;
Eric Moore635374e2009-03-09 01:21:12 -0600726 }
727
728 mpt2sas_port->port = port;
729 if (mpt2sas_port->remote_identify.device_type == SAS_END_DEVICE)
730 rphy = sas_end_device_alloc(port);
731 else
732 rphy = sas_expander_alloc(port,
733 mpt2sas_port->remote_identify.device_type);
734
735 rphy->identify = mpt2sas_port->remote_identify;
736 if ((sas_rphy_add(rphy))) {
737 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
738 ioc->name, __FILE__, __LINE__, __func__);
739 }
740 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
741 dev_printk(KERN_INFO, &rphy->dev, "add: handle(0x%04x), "
742 "sas_addr(0x%016llx)\n", handle,
743 (unsigned long long)
744 mpt2sas_port->remote_identify.sas_address);
745 mpt2sas_port->rphy = rphy;
746 spin_lock_irqsave(&ioc->sas_node_lock, flags);
747 list_add_tail(&mpt2sas_port->port_list, &sas_node->sas_port_list);
748 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
749
750 /* fill in report manufacture */
751 if (mpt2sas_port->remote_identify.device_type ==
752 MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
753 mpt2sas_port->remote_identify.device_type ==
754 MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER)
Eric Moored5d135b2009-05-18 13:02:08 -0600755 _transport_expander_report_manufacture(ioc,
Eric Moore635374e2009-03-09 01:21:12 -0600756 mpt2sas_port->remote_identify.sas_address,
757 rphy_to_expander_device(rphy));
758
759 return mpt2sas_port;
760
761 out_fail:
762 list_for_each_entry_safe(mpt2sas_phy, next, &mpt2sas_port->phy_list,
763 port_siblings)
764 list_del(&mpt2sas_phy->port_siblings);
765 kfree(mpt2sas_port);
766 return NULL;
767}
768
769/**
770 * mpt2sas_transport_port_remove - remove port from the list
771 * @ioc: per adapter object
772 * @sas_address: sas address of attached device
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530773 * @sas_address_parent: sas address of parent expander or sas host
Eric Moore635374e2009-03-09 01:21:12 -0600774 * Context: This function will acquire ioc->sas_node_lock.
775 *
776 * Removing object and freeing associated memory from the
777 * ioc->sas_port_list.
778 *
779 * Return nothing.
780 */
781void
782mpt2sas_transport_port_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530783 u64 sas_address_parent)
Eric Moore635374e2009-03-09 01:21:12 -0600784{
785 int i;
786 unsigned long flags;
787 struct _sas_port *mpt2sas_port, *next;
788 struct _sas_node *sas_node;
789 u8 found = 0;
790 struct _sas_phy *mpt2sas_phy, *next_phy;
791
792 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530793 sas_node = _transport_sas_node_find_by_sas_address(ioc,
794 sas_address_parent);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +0530795 if (!sas_node) {
796 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -0600797 return;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +0530798 }
Eric Moore635374e2009-03-09 01:21:12 -0600799 list_for_each_entry_safe(mpt2sas_port, next, &sas_node->sas_port_list,
800 port_list) {
801 if (mpt2sas_port->remote_identify.sas_address != sas_address)
802 continue;
803 found = 1;
804 list_del(&mpt2sas_port->port_list);
805 goto out;
806 }
807 out:
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +0530808 if (!found) {
809 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -0600810 return;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +0530811 }
Eric Moore635374e2009-03-09 01:21:12 -0600812
813 for (i = 0; i < sas_node->num_phys; i++) {
814 if (sas_node->phy[i].remote_identify.sas_address == sas_address)
815 memset(&sas_node->phy[i].remote_identify, 0 ,
816 sizeof(struct sas_identify));
817 }
818
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +0530819 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -0600820 list_for_each_entry_safe(mpt2sas_phy, next_phy,
821 &mpt2sas_port->phy_list, port_siblings) {
822 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
823 dev_printk(KERN_INFO, &mpt2sas_port->port->dev,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530824 "remove: sas_addr(0x%016llx), phy(%d)\n",
Eric Moore635374e2009-03-09 01:21:12 -0600825 (unsigned long long)
826 mpt2sas_port->remote_identify.sas_address,
827 mpt2sas_phy->phy_id);
Kashyap, Desai38c29112010-04-05 14:18:34 +0530828 mpt2sas_phy->phy_belongs_to_port = 0;
Eric Moore635374e2009-03-09 01:21:12 -0600829 sas_port_delete_phy(mpt2sas_port->port, mpt2sas_phy->phy);
830 list_del(&mpt2sas_phy->port_siblings);
831 }
832 sas_port_delete(mpt2sas_port->port);
833 kfree(mpt2sas_port);
834}
835
836/**
837 * mpt2sas_transport_add_host_phy - report sas_host phy to transport
838 * @ioc: per adapter object
839 * @mpt2sas_phy: mpt2sas per phy object
840 * @phy_pg0: sas phy page 0
841 * @parent_dev: parent device class object
842 *
843 * Returns 0 for success, non-zero for failure.
844 */
845int
846mpt2sas_transport_add_host_phy(struct MPT2SAS_ADAPTER *ioc, struct _sas_phy
847 *mpt2sas_phy, Mpi2SasPhyPage0_t phy_pg0, struct device *parent_dev)
848{
849 struct sas_phy *phy;
850 int phy_index = mpt2sas_phy->phy_id;
851
852
853 INIT_LIST_HEAD(&mpt2sas_phy->port_siblings);
854 phy = sas_phy_alloc(parent_dev, phy_index);
855 if (!phy) {
856 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
857 ioc->name, __FILE__, __LINE__, __func__);
858 return -1;
859 }
860 if ((_transport_set_identify(ioc, mpt2sas_phy->handle,
861 &mpt2sas_phy->identify))) {
862 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
863 ioc->name, __FILE__, __LINE__, __func__);
864 return -1;
865 }
866 phy->identify = mpt2sas_phy->identify;
867 mpt2sas_phy->attached_handle = le16_to_cpu(phy_pg0.AttachedDevHandle);
868 if (mpt2sas_phy->attached_handle)
869 _transport_set_identify(ioc, mpt2sas_phy->attached_handle,
870 &mpt2sas_phy->remote_identify);
871 phy->identify.phy_identifier = mpt2sas_phy->phy_id;
872 phy->negotiated_linkrate = _transport_convert_phy_link_rate(
873 phy_pg0.NegotiatedLinkRate & MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
874 phy->minimum_linkrate_hw = _transport_convert_phy_link_rate(
875 phy_pg0.HwLinkRate & MPI2_SAS_HWRATE_MIN_RATE_MASK);
876 phy->maximum_linkrate_hw = _transport_convert_phy_link_rate(
877 phy_pg0.HwLinkRate >> 4);
878 phy->minimum_linkrate = _transport_convert_phy_link_rate(
879 phy_pg0.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
880 phy->maximum_linkrate = _transport_convert_phy_link_rate(
881 phy_pg0.ProgrammedLinkRate >> 4);
882
883 if ((sas_phy_add(phy))) {
884 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
885 ioc->name, __FILE__, __LINE__, __func__);
886 sas_phy_free(phy);
887 return -1;
888 }
889 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
890 dev_printk(KERN_INFO, &phy->dev,
891 "add: handle(0x%04x), sas_addr(0x%016llx)\n"
892 "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
893 mpt2sas_phy->handle, (unsigned long long)
894 mpt2sas_phy->identify.sas_address,
895 mpt2sas_phy->attached_handle,
896 (unsigned long long)
897 mpt2sas_phy->remote_identify.sas_address);
898 mpt2sas_phy->phy = phy;
899 return 0;
900}
901
902
903/**
904 * mpt2sas_transport_add_expander_phy - report expander phy to transport
905 * @ioc: per adapter object
906 * @mpt2sas_phy: mpt2sas per phy object
907 * @expander_pg1: expander page 1
908 * @parent_dev: parent device class object
909 *
910 * Returns 0 for success, non-zero for failure.
911 */
912int
913mpt2sas_transport_add_expander_phy(struct MPT2SAS_ADAPTER *ioc, struct _sas_phy
914 *mpt2sas_phy, Mpi2ExpanderPage1_t expander_pg1, struct device *parent_dev)
915{
916 struct sas_phy *phy;
917 int phy_index = mpt2sas_phy->phy_id;
918
919 INIT_LIST_HEAD(&mpt2sas_phy->port_siblings);
920 phy = sas_phy_alloc(parent_dev, phy_index);
921 if (!phy) {
922 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
923 ioc->name, __FILE__, __LINE__, __func__);
924 return -1;
925 }
926 if ((_transport_set_identify(ioc, mpt2sas_phy->handle,
927 &mpt2sas_phy->identify))) {
928 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
929 ioc->name, __FILE__, __LINE__, __func__);
930 return -1;
931 }
932 phy->identify = mpt2sas_phy->identify;
933 mpt2sas_phy->attached_handle =
934 le16_to_cpu(expander_pg1.AttachedDevHandle);
935 if (mpt2sas_phy->attached_handle)
936 _transport_set_identify(ioc, mpt2sas_phy->attached_handle,
937 &mpt2sas_phy->remote_identify);
938 phy->identify.phy_identifier = mpt2sas_phy->phy_id;
939 phy->negotiated_linkrate = _transport_convert_phy_link_rate(
940 expander_pg1.NegotiatedLinkRate &
941 MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
942 phy->minimum_linkrate_hw = _transport_convert_phy_link_rate(
943 expander_pg1.HwLinkRate & MPI2_SAS_HWRATE_MIN_RATE_MASK);
944 phy->maximum_linkrate_hw = _transport_convert_phy_link_rate(
945 expander_pg1.HwLinkRate >> 4);
946 phy->minimum_linkrate = _transport_convert_phy_link_rate(
947 expander_pg1.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
948 phy->maximum_linkrate = _transport_convert_phy_link_rate(
949 expander_pg1.ProgrammedLinkRate >> 4);
950
951 if ((sas_phy_add(phy))) {
952 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
953 ioc->name, __FILE__, __LINE__, __func__);
954 sas_phy_free(phy);
955 return -1;
956 }
957 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
958 dev_printk(KERN_INFO, &phy->dev,
959 "add: handle(0x%04x), sas_addr(0x%016llx)\n"
960 "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
961 mpt2sas_phy->handle, (unsigned long long)
962 mpt2sas_phy->identify.sas_address,
963 mpt2sas_phy->attached_handle,
964 (unsigned long long)
965 mpt2sas_phy->remote_identify.sas_address);
966 mpt2sas_phy->phy = phy;
967 return 0;
968}
969
970/**
Kashyap, Desaicc0f5202009-08-20 13:22:39 +0530971 * mpt2sas_transport_update_links - refreshing phy link changes
Eric Moore635374e2009-03-09 01:21:12 -0600972 * @ioc: per adapter object
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530973 * @sas_address: sas address of parent expander or sas host
974 * @handle: attached device handle
Eric Moore635374e2009-03-09 01:21:12 -0600975 * @phy_numberv: phy number
976 * @link_rate: new link rate
977 *
978 * Returns nothing.
979 */
980void
Kashyap, Desaicc0f5202009-08-20 13:22:39 +0530981mpt2sas_transport_update_links(struct MPT2SAS_ADAPTER *ioc,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530982 u64 sas_address, u16 handle, u8 phy_number, u8 link_rate)
Eric Moore635374e2009-03-09 01:21:12 -0600983{
984 unsigned long flags;
985 struct _sas_node *sas_node;
986 struct _sas_phy *mpt2sas_phy;
987
Eric Moore3cb54692010-07-08 14:44:34 -0600988 if (ioc->shost_recovery || ioc->pci_error_recovery)
Kashyap, Desai155dd4c2009-08-20 13:22:00 +0530989 return;
Kashyap, Desai155dd4c2009-08-20 13:22:00 +0530990
Eric Moore635374e2009-03-09 01:21:12 -0600991 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530992 sas_node = _transport_sas_node_find_by_sas_address(ioc, sas_address);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +0530993 if (!sas_node) {
994 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -0600995 return;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +0530996 }
Eric Moore635374e2009-03-09 01:21:12 -0600997
998 mpt2sas_phy = &sas_node->phy[phy_number];
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530999 mpt2sas_phy->attached_handle = handle;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301000 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05301001 if (handle && (link_rate >= MPI2_SAS_NEG_LINK_RATE_1_5)) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05301002 _transport_set_identify(ioc, handle,
Eric Moore635374e2009-03-09 01:21:12 -06001003 &mpt2sas_phy->remote_identify);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05301004 _transport_add_phy_to_an_existing_port(ioc, sas_node,
1005 mpt2sas_phy, mpt2sas_phy->remote_identify.sas_address);
1006 } else
Eric Moore635374e2009-03-09 01:21:12 -06001007 memset(&mpt2sas_phy->remote_identify, 0 , sizeof(struct
1008 sas_identify));
1009
1010 if (mpt2sas_phy->phy)
1011 mpt2sas_phy->phy->negotiated_linkrate =
1012 _transport_convert_phy_link_rate(link_rate);
1013
1014 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
1015 dev_printk(KERN_INFO, &mpt2sas_phy->phy->dev,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05301016 "refresh: parent sas_addr(0x%016llx),\n"
Eric Moore635374e2009-03-09 01:21:12 -06001017 "\tlink_rate(0x%02x), phy(%d)\n"
1018 "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05301019 (unsigned long long)sas_address,
1020 link_rate, phy_number, handle, (unsigned long long)
Eric Moore635374e2009-03-09 01:21:12 -06001021 mpt2sas_phy->remote_identify.sas_address);
1022}
1023
1024static inline void *
1025phy_to_ioc(struct sas_phy *phy)
1026{
1027 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
1028 return shost_priv(shost);
1029}
1030
1031static inline void *
1032rphy_to_ioc(struct sas_rphy *rphy)
1033{
1034 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
1035 return shost_priv(shost);
1036}
1037
Kashyap, Desai50d5c602009-12-16 18:55:26 +05301038
Kashyap, Desaid5f491e2010-06-17 13:32:54 +05301039/* report phy error log structure */
1040struct phy_error_log_request{
1041 u8 smp_frame_type; /* 0x40 */
1042 u8 function; /* 0x11 */
1043 u8 allocated_response_length;
1044 u8 request_length; /* 02 */
1045 u8 reserved_1[5];
1046 u8 phy_identifier;
1047 u8 reserved_2[2];
1048};
1049
1050/* report phy error log reply structure */
1051struct phy_error_log_reply{
1052 u8 smp_frame_type; /* 0x41 */
1053 u8 function; /* 0x11 */
1054 u8 function_result;
1055 u8 response_length;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05301056 __be16 expander_change_count;
Kashyap, Desaid5f491e2010-06-17 13:32:54 +05301057 u8 reserved_1[3];
1058 u8 phy_identifier;
1059 u8 reserved_2[2];
Kashyap, Desaic97951e2011-06-14 10:54:56 +05301060 __be32 invalid_dword;
1061 __be32 running_disparity_error;
1062 __be32 loss_of_dword_sync;
1063 __be32 phy_reset_problem;
Kashyap, Desaid5f491e2010-06-17 13:32:54 +05301064};
1065
Eric Moore635374e2009-03-09 01:21:12 -06001066/**
Kashyap, Desaid5f491e2010-06-17 13:32:54 +05301067 * _transport_get_expander_phy_error_log - return expander counters
1068 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06001069 * @phy: The sas phy object
1070 *
Kashyap, Desaid5f491e2010-06-17 13:32:54 +05301071 * Returns 0 for success, non-zero for failure.
1072 *
1073 */
1074static int
1075_transport_get_expander_phy_error_log(struct MPT2SAS_ADAPTER *ioc,
1076 struct sas_phy *phy)
1077{
1078 Mpi2SmpPassthroughRequest_t *mpi_request;
1079 Mpi2SmpPassthroughReply_t *mpi_reply;
1080 struct phy_error_log_request *phy_error_log_request;
1081 struct phy_error_log_reply *phy_error_log_reply;
1082 int rc;
1083 u16 smid;
1084 u32 ioc_state;
1085 unsigned long timeleft;
1086 void *psge;
1087 u32 sgl_flags;
1088 u8 issue_reset = 0;
1089 void *data_out = NULL;
1090 dma_addr_t data_out_dma;
1091 u32 sz;
Kashyap, Desaid5f491e2010-06-17 13:32:54 +05301092 u16 wait_state_count;
1093
Eric Moore3cb54692010-07-08 14:44:34 -06001094 if (ioc->shost_recovery || ioc->pci_error_recovery) {
Kashyap, Desaid5f491e2010-06-17 13:32:54 +05301095 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
1096 __func__, ioc->name);
1097 return -EFAULT;
1098 }
1099
1100 mutex_lock(&ioc->transport_cmds.mutex);
1101
1102 if (ioc->transport_cmds.status != MPT2_CMD_NOT_USED) {
1103 printk(MPT2SAS_ERR_FMT "%s: transport_cmds in use\n",
1104 ioc->name, __func__);
1105 rc = -EAGAIN;
1106 goto out;
1107 }
1108 ioc->transport_cmds.status = MPT2_CMD_PENDING;
1109
1110 wait_state_count = 0;
1111 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
1112 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1113 if (wait_state_count++ == 10) {
1114 printk(MPT2SAS_ERR_FMT
1115 "%s: failed due to ioc not operational\n",
1116 ioc->name, __func__);
1117 rc = -EFAULT;
1118 goto out;
1119 }
1120 ssleep(1);
1121 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
1122 printk(MPT2SAS_INFO_FMT "%s: waiting for "
1123 "operational state(count=%d)\n", ioc->name,
1124 __func__, wait_state_count);
1125 }
1126 if (wait_state_count)
1127 printk(MPT2SAS_INFO_FMT "%s: ioc is operational\n",
1128 ioc->name, __func__);
1129
1130 smid = mpt2sas_base_get_smid(ioc, ioc->transport_cb_idx);
1131 if (!smid) {
1132 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1133 ioc->name, __func__);
1134 rc = -EAGAIN;
1135 goto out;
1136 }
1137
1138 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1139 ioc->transport_cmds.smid = smid;
1140
1141 sz = sizeof(struct phy_error_log_request) +
1142 sizeof(struct phy_error_log_reply);
1143 data_out = pci_alloc_consistent(ioc->pdev, sz, &data_out_dma);
1144 if (!data_out) {
1145 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
1146 __LINE__, __func__);
1147 rc = -ENOMEM;
1148 mpt2sas_base_free_smid(ioc, smid);
1149 goto out;
1150 }
1151
1152 rc = -EINVAL;
1153 memset(data_out, 0, sz);
1154 phy_error_log_request = data_out;
1155 phy_error_log_request->smp_frame_type = 0x40;
1156 phy_error_log_request->function = 0x11;
1157 phy_error_log_request->request_length = 2;
1158 phy_error_log_request->allocated_response_length = 0;
1159 phy_error_log_request->phy_identifier = phy->number;
1160
1161 memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
1162 mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
1163 mpi_request->PhysicalPort = 0xFF;
1164 mpi_request->VF_ID = 0; /* TODO */
1165 mpi_request->VP_ID = 0;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05301166 mpi_request->SASAddress = cpu_to_le64(phy->identify.sas_address);
Kashyap, Desaid5f491e2010-06-17 13:32:54 +05301167 mpi_request->RequestDataLength =
1168 cpu_to_le16(sizeof(struct phy_error_log_request));
1169 psge = &mpi_request->SGL;
1170
1171 /* WRITE sgel first */
1172 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1173 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
1174 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1175 ioc->base_add_sg_single(psge, sgl_flags |
1176 sizeof(struct phy_error_log_request), data_out_dma);
1177
1178 /* incr sgel */
1179 psge += ioc->sge_size;
1180
1181 /* READ sgel last */
1182 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1183 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
1184 MPI2_SGE_FLAGS_END_OF_LIST);
1185 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1186 ioc->base_add_sg_single(psge, sgl_flags |
1187 sizeof(struct phy_error_log_reply), data_out_dma +
1188 sizeof(struct phy_error_log_request));
1189
1190 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "phy_error_log - "
1191 "send to sas_addr(0x%016llx), phy(%d)\n", ioc->name,
1192 (unsigned long long)phy->identify.sas_address, phy->number));
Kashyap, Desaid5f491e2010-06-17 13:32:54 +05301193 init_completion(&ioc->transport_cmds.done);
nagalakshmi.nandigama@lsi.comf01690d2011-12-01 07:43:58 +05301194 mpt2sas_base_put_smid_default(ioc, smid);
Kashyap, Desaid5f491e2010-06-17 13:32:54 +05301195 timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done,
1196 10*HZ);
1197
1198 if (!(ioc->transport_cmds.status & MPT2_CMD_COMPLETE)) {
1199 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
1200 ioc->name, __func__);
1201 _debug_dump_mf(mpi_request,
1202 sizeof(Mpi2SmpPassthroughRequest_t)/4);
1203 if (!(ioc->transport_cmds.status & MPT2_CMD_RESET))
1204 issue_reset = 1;
1205 goto issue_host_reset;
1206 }
1207
1208 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "phy_error_log - "
1209 "complete\n", ioc->name));
1210
1211 if (ioc->transport_cmds.status & MPT2_CMD_REPLY_VALID) {
1212
1213 mpi_reply = ioc->transport_cmds.reply;
1214
1215 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
1216 "phy_error_log - reply data transfer size(%d)\n",
1217 ioc->name, le16_to_cpu(mpi_reply->ResponseDataLength)));
1218
1219 if (le16_to_cpu(mpi_reply->ResponseDataLength) !=
1220 sizeof(struct phy_error_log_reply))
1221 goto out;
1222
1223 phy_error_log_reply = data_out +
1224 sizeof(struct phy_error_log_request);
1225
1226 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
1227 "phy_error_log - function_result(%d)\n",
1228 ioc->name, phy_error_log_reply->function_result));
1229
1230 phy->invalid_dword_count =
1231 be32_to_cpu(phy_error_log_reply->invalid_dword);
1232 phy->running_disparity_error_count =
1233 be32_to_cpu(phy_error_log_reply->running_disparity_error);
1234 phy->loss_of_dword_sync_count =
1235 be32_to_cpu(phy_error_log_reply->loss_of_dword_sync);
1236 phy->phy_reset_problem_count =
1237 be32_to_cpu(phy_error_log_reply->phy_reset_problem);
1238 rc = 0;
1239 } else
1240 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
1241 "phy_error_log - no reply\n", ioc->name));
1242
1243 issue_host_reset:
1244 if (issue_reset)
1245 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1246 FORCE_BIG_HAMMER);
1247 out:
1248 ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
1249 if (data_out)
1250 pci_free_consistent(ioc->pdev, sz, data_out, data_out_dma);
1251
1252 mutex_unlock(&ioc->transport_cmds.mutex);
1253 return rc;
1254}
1255
1256/**
1257 * _transport_get_linkerrors - return phy counters for both hba and expanders
1258 * @phy: The sas phy object
1259 *
Eric Moore635374e2009-03-09 01:21:12 -06001260 * Returns 0 for success, non-zero for failure.
1261 *
1262 */
1263static int
Eric Moored5d135b2009-05-18 13:02:08 -06001264_transport_get_linkerrors(struct sas_phy *phy)
Eric Moore635374e2009-03-09 01:21:12 -06001265{
1266 struct MPT2SAS_ADAPTER *ioc = phy_to_ioc(phy);
Kashyap, Desaid5f491e2010-06-17 13:32:54 +05301267 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06001268 Mpi2ConfigReply_t mpi_reply;
1269 Mpi2SasPhyPage1_t phy_pg1;
Eric Moore635374e2009-03-09 01:21:12 -06001270
Kashyap, Desaid5f491e2010-06-17 13:32:54 +05301271 spin_lock_irqsave(&ioc->sas_node_lock, flags);
1272 if (_transport_sas_node_find_by_sas_address(ioc,
1273 phy->identify.sas_address) == NULL) {
1274 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06001275 return -EINVAL;
Kashyap, Desaid5f491e2010-06-17 13:32:54 +05301276 }
1277 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06001278
Kashyap, Desaid5f491e2010-06-17 13:32:54 +05301279 if (phy->identify.sas_address != ioc->sas_hba.sas_address)
1280 return _transport_get_expander_phy_error_log(ioc, phy);
1281
1282 /* get hba phy error logs */
Eric Moore635374e2009-03-09 01:21:12 -06001283 if ((mpt2sas_config_get_phy_pg1(ioc, &mpi_reply, &phy_pg1,
Kashyap, Desaid5f491e2010-06-17 13:32:54 +05301284 phy->number))) {
Eric Moore635374e2009-03-09 01:21:12 -06001285 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1286 ioc->name, __FILE__, __LINE__, __func__);
1287 return -ENXIO;
1288 }
1289
1290 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo)
1291 printk(MPT2SAS_INFO_FMT "phy(%d), ioc_status"
1292 "(0x%04x), loginfo(0x%08x)\n", ioc->name,
Kashyap, Desaid5f491e2010-06-17 13:32:54 +05301293 phy->number, le16_to_cpu(mpi_reply.IOCStatus),
Eric Moore635374e2009-03-09 01:21:12 -06001294 le32_to_cpu(mpi_reply.IOCLogInfo));
1295
1296 phy->invalid_dword_count = le32_to_cpu(phy_pg1.InvalidDwordCount);
1297 phy->running_disparity_error_count =
1298 le32_to_cpu(phy_pg1.RunningDisparityErrorCount);
1299 phy->loss_of_dword_sync_count =
1300 le32_to_cpu(phy_pg1.LossDwordSynchCount);
1301 phy->phy_reset_problem_count =
1302 le32_to_cpu(phy_pg1.PhyResetProblemCount);
1303 return 0;
1304}
1305
1306/**
Eric Moored5d135b2009-05-18 13:02:08 -06001307 * _transport_get_enclosure_identifier -
Eric Moore635374e2009-03-09 01:21:12 -06001308 * @phy: The sas phy object
1309 *
1310 * Obtain the enclosure logical id for an expander.
1311 * Returns 0 for success, non-zero for failure.
1312 */
1313static int
Eric Moored5d135b2009-05-18 13:02:08 -06001314_transport_get_enclosure_identifier(struct sas_rphy *rphy, u64 *identifier)
Eric Moore635374e2009-03-09 01:21:12 -06001315{
1316 struct MPT2SAS_ADAPTER *ioc = rphy_to_ioc(rphy);
Kashyap, Desaiab6ce922010-06-17 13:25:13 +05301317 struct _sas_device *sas_device;
Eric Moore635374e2009-03-09 01:21:12 -06001318 unsigned long flags;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301319 int rc;
Eric Moore635374e2009-03-09 01:21:12 -06001320
Kashyap, Desaiab6ce922010-06-17 13:25:13 +05301321 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1322 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
Eric Moore635374e2009-03-09 01:21:12 -06001323 rphy->identify.sas_address);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301324 if (sas_device) {
1325 *identifier = sas_device->enclosure_logical_id;
1326 rc = 0;
1327 } else {
1328 *identifier = 0;
1329 rc = -ENXIO;
1330 }
Kashyap, Desaiab6ce922010-06-17 13:25:13 +05301331 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301332 return rc;
Eric Moore635374e2009-03-09 01:21:12 -06001333}
1334
1335/**
Eric Moored5d135b2009-05-18 13:02:08 -06001336 * _transport_get_bay_identifier -
Eric Moore635374e2009-03-09 01:21:12 -06001337 * @phy: The sas phy object
1338 *
1339 * Returns the slot id for a device that resides inside an enclosure.
1340 */
1341static int
Eric Moored5d135b2009-05-18 13:02:08 -06001342_transport_get_bay_identifier(struct sas_rphy *rphy)
Eric Moore635374e2009-03-09 01:21:12 -06001343{
1344 struct MPT2SAS_ADAPTER *ioc = rphy_to_ioc(rphy);
1345 struct _sas_device *sas_device;
1346 unsigned long flags;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301347 int rc;
Eric Moore635374e2009-03-09 01:21:12 -06001348
1349 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1350 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1351 rphy->identify.sas_address);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301352 if (sas_device)
1353 rc = sas_device->slot;
1354 else
1355 rc = -ENXIO;
Eric Moore635374e2009-03-09 01:21:12 -06001356 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301357 return rc;
Eric Moore635374e2009-03-09 01:21:12 -06001358}
1359
Kashyap, Desaib8d7d7b2010-06-17 13:34:31 +05301360/* phy control request structure */
1361struct phy_control_request{
1362 u8 smp_frame_type; /* 0x40 */
1363 u8 function; /* 0x91 */
1364 u8 allocated_response_length;
1365 u8 request_length; /* 0x09 */
1366 u16 expander_change_count;
1367 u8 reserved_1[3];
1368 u8 phy_identifier;
1369 u8 phy_operation;
1370 u8 reserved_2[13];
1371 u64 attached_device_name;
1372 u8 programmed_min_physical_link_rate;
1373 u8 programmed_max_physical_link_rate;
1374 u8 reserved_3[6];
1375};
1376
1377/* phy control reply structure */
1378struct phy_control_reply{
1379 u8 smp_frame_type; /* 0x41 */
1380 u8 function; /* 0x11 */
1381 u8 function_result;
1382 u8 response_length;
1383};
1384
1385#define SMP_PHY_CONTROL_LINK_RESET (0x01)
1386#define SMP_PHY_CONTROL_HARD_RESET (0x02)
1387#define SMP_PHY_CONTROL_DISABLE (0x03)
1388
1389/**
1390 * _transport_expander_phy_control - expander phy control
1391 * @ioc: per adapter object
1392 * @phy: The sas phy object
1393 *
1394 * Returns 0 for success, non-zero for failure.
1395 *
1396 */
1397static int
1398_transport_expander_phy_control(struct MPT2SAS_ADAPTER *ioc,
1399 struct sas_phy *phy, u8 phy_operation)
1400{
1401 Mpi2SmpPassthroughRequest_t *mpi_request;
1402 Mpi2SmpPassthroughReply_t *mpi_reply;
1403 struct phy_control_request *phy_control_request;
1404 struct phy_control_reply *phy_control_reply;
1405 int rc;
1406 u16 smid;
1407 u32 ioc_state;
1408 unsigned long timeleft;
1409 void *psge;
1410 u32 sgl_flags;
1411 u8 issue_reset = 0;
1412 void *data_out = NULL;
1413 dma_addr_t data_out_dma;
1414 u32 sz;
Kashyap, Desaib8d7d7b2010-06-17 13:34:31 +05301415 u16 wait_state_count;
1416
1417 if (ioc->shost_recovery) {
1418 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
1419 __func__, ioc->name);
1420 return -EFAULT;
1421 }
1422
1423 mutex_lock(&ioc->transport_cmds.mutex);
1424
1425 if (ioc->transport_cmds.status != MPT2_CMD_NOT_USED) {
1426 printk(MPT2SAS_ERR_FMT "%s: transport_cmds in use\n",
1427 ioc->name, __func__);
1428 rc = -EAGAIN;
1429 goto out;
1430 }
1431 ioc->transport_cmds.status = MPT2_CMD_PENDING;
1432
1433 wait_state_count = 0;
1434 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
1435 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1436 if (wait_state_count++ == 10) {
1437 printk(MPT2SAS_ERR_FMT
1438 "%s: failed due to ioc not operational\n",
1439 ioc->name, __func__);
1440 rc = -EFAULT;
1441 goto out;
1442 }
1443 ssleep(1);
1444 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
1445 printk(MPT2SAS_INFO_FMT "%s: waiting for "
1446 "operational state(count=%d)\n", ioc->name,
1447 __func__, wait_state_count);
1448 }
1449 if (wait_state_count)
1450 printk(MPT2SAS_INFO_FMT "%s: ioc is operational\n",
1451 ioc->name, __func__);
1452
1453 smid = mpt2sas_base_get_smid(ioc, ioc->transport_cb_idx);
1454 if (!smid) {
1455 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1456 ioc->name, __func__);
1457 rc = -EAGAIN;
1458 goto out;
1459 }
1460
1461 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1462 ioc->transport_cmds.smid = smid;
1463
1464 sz = sizeof(struct phy_control_request) +
1465 sizeof(struct phy_control_reply);
1466 data_out = pci_alloc_consistent(ioc->pdev, sz, &data_out_dma);
1467 if (!data_out) {
1468 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
1469 __LINE__, __func__);
1470 rc = -ENOMEM;
1471 mpt2sas_base_free_smid(ioc, smid);
1472 goto out;
1473 }
1474
1475 rc = -EINVAL;
1476 memset(data_out, 0, sz);
1477 phy_control_request = data_out;
1478 phy_control_request->smp_frame_type = 0x40;
1479 phy_control_request->function = 0x91;
1480 phy_control_request->request_length = 9;
1481 phy_control_request->allocated_response_length = 0;
1482 phy_control_request->phy_identifier = phy->number;
1483 phy_control_request->phy_operation = phy_operation;
1484 phy_control_request->programmed_min_physical_link_rate =
1485 phy->minimum_linkrate << 4;
1486 phy_control_request->programmed_max_physical_link_rate =
1487 phy->maximum_linkrate << 4;
1488
1489 memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
1490 mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
1491 mpi_request->PhysicalPort = 0xFF;
1492 mpi_request->VF_ID = 0; /* TODO */
1493 mpi_request->VP_ID = 0;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05301494 mpi_request->SASAddress = cpu_to_le64(phy->identify.sas_address);
Kashyap, Desaib8d7d7b2010-06-17 13:34:31 +05301495 mpi_request->RequestDataLength =
1496 cpu_to_le16(sizeof(struct phy_error_log_request));
1497 psge = &mpi_request->SGL;
1498
1499 /* WRITE sgel first */
1500 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1501 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
1502 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1503 ioc->base_add_sg_single(psge, sgl_flags |
1504 sizeof(struct phy_control_request), data_out_dma);
1505
1506 /* incr sgel */
1507 psge += ioc->sge_size;
1508
1509 /* READ sgel last */
1510 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1511 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
1512 MPI2_SGE_FLAGS_END_OF_LIST);
1513 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1514 ioc->base_add_sg_single(psge, sgl_flags |
1515 sizeof(struct phy_control_reply), data_out_dma +
1516 sizeof(struct phy_control_request));
1517
1518 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "phy_control - "
1519 "send to sas_addr(0x%016llx), phy(%d), opcode(%d)\n", ioc->name,
1520 (unsigned long long)phy->identify.sas_address, phy->number,
1521 phy_operation));
nagalakshmi.nandigama@lsi.comf01690d2011-12-01 07:43:58 +05301522
Kashyap, Desaib8d7d7b2010-06-17 13:34:31 +05301523 init_completion(&ioc->transport_cmds.done);
nagalakshmi.nandigama@lsi.comf01690d2011-12-01 07:43:58 +05301524 mpt2sas_base_put_smid_default(ioc, smid);
Kashyap, Desaib8d7d7b2010-06-17 13:34:31 +05301525 timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done,
1526 10*HZ);
1527
1528 if (!(ioc->transport_cmds.status & MPT2_CMD_COMPLETE)) {
1529 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
1530 ioc->name, __func__);
1531 _debug_dump_mf(mpi_request,
1532 sizeof(Mpi2SmpPassthroughRequest_t)/4);
1533 if (!(ioc->transport_cmds.status & MPT2_CMD_RESET))
1534 issue_reset = 1;
1535 goto issue_host_reset;
1536 }
1537
1538 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "phy_control - "
1539 "complete\n", ioc->name));
1540
1541 if (ioc->transport_cmds.status & MPT2_CMD_REPLY_VALID) {
1542
1543 mpi_reply = ioc->transport_cmds.reply;
1544
1545 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
1546 "phy_control - reply data transfer size(%d)\n",
1547 ioc->name, le16_to_cpu(mpi_reply->ResponseDataLength)));
1548
1549 if (le16_to_cpu(mpi_reply->ResponseDataLength) !=
1550 sizeof(struct phy_control_reply))
1551 goto out;
1552
1553 phy_control_reply = data_out +
1554 sizeof(struct phy_control_request);
1555
1556 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
1557 "phy_control - function_result(%d)\n",
1558 ioc->name, phy_control_reply->function_result));
1559
1560 rc = 0;
1561 } else
1562 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
1563 "phy_control - no reply\n", ioc->name));
1564
1565 issue_host_reset:
1566 if (issue_reset)
1567 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1568 FORCE_BIG_HAMMER);
1569 out:
1570 ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
1571 if (data_out)
1572 pci_free_consistent(ioc->pdev, sz, data_out, data_out_dma);
1573
1574 mutex_unlock(&ioc->transport_cmds.mutex);
1575 return rc;
1576}
1577
Eric Moore635374e2009-03-09 01:21:12 -06001578/**
Eric Moored5d135b2009-05-18 13:02:08 -06001579 * _transport_phy_reset -
Eric Moore635374e2009-03-09 01:21:12 -06001580 * @phy: The sas phy object
1581 * @hard_reset:
1582 *
Eric Moore635374e2009-03-09 01:21:12 -06001583 * Returns 0 for success, non-zero for failure.
1584 */
1585static int
Eric Moored5d135b2009-05-18 13:02:08 -06001586_transport_phy_reset(struct sas_phy *phy, int hard_reset)
Eric Moore635374e2009-03-09 01:21:12 -06001587{
1588 struct MPT2SAS_ADAPTER *ioc = phy_to_ioc(phy);
Eric Moore635374e2009-03-09 01:21:12 -06001589 Mpi2SasIoUnitControlReply_t mpi_reply;
1590 Mpi2SasIoUnitControlRequest_t mpi_request;
Kashyap, Desaib8d7d7b2010-06-17 13:34:31 +05301591 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06001592
Kashyap, Desaib8d7d7b2010-06-17 13:34:31 +05301593 spin_lock_irqsave(&ioc->sas_node_lock, flags);
1594 if (_transport_sas_node_find_by_sas_address(ioc,
1595 phy->identify.sas_address) == NULL) {
1596 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06001597 return -EINVAL;
Kashyap, Desaib8d7d7b2010-06-17 13:34:31 +05301598 }
1599 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06001600
Kashyap, Desaib8d7d7b2010-06-17 13:34:31 +05301601 /* handle expander phys */
1602 if (phy->identify.sas_address != ioc->sas_hba.sas_address)
1603 return _transport_expander_phy_control(ioc, phy,
1604 (hard_reset == 1) ? SMP_PHY_CONTROL_HARD_RESET :
1605 SMP_PHY_CONTROL_LINK_RESET);
1606
1607 /* handle hba phys */
Eric Moore635374e2009-03-09 01:21:12 -06001608 memset(&mpi_request, 0, sizeof(Mpi2SasIoUnitControlReply_t));
1609 mpi_request.Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
1610 mpi_request.Operation = hard_reset ?
1611 MPI2_SAS_OP_PHY_HARD_RESET : MPI2_SAS_OP_PHY_LINK_RESET;
Kashyap, Desaib8d7d7b2010-06-17 13:34:31 +05301612 mpi_request.PhyNum = phy->number;
Eric Moore635374e2009-03-09 01:21:12 -06001613
1614 if ((mpt2sas_base_sas_iounit_control(ioc, &mpi_reply, &mpi_request))) {
1615 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1616 ioc->name, __FILE__, __LINE__, __func__);
1617 return -ENXIO;
1618 }
1619
1620 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo)
1621 printk(MPT2SAS_INFO_FMT "phy(%d), ioc_status"
1622 "(0x%04x), loginfo(0x%08x)\n", ioc->name,
Kashyap, Desaib8d7d7b2010-06-17 13:34:31 +05301623 phy->number, le16_to_cpu(mpi_reply.IOCStatus),
Eric Moore635374e2009-03-09 01:21:12 -06001624 le32_to_cpu(mpi_reply.IOCLogInfo));
1625
1626 return 0;
1627}
1628
1629/**
Kashyap, Desai50d5c602009-12-16 18:55:26 +05301630 * _transport_phy_enable - enable/disable phys
1631 * @phy: The sas phy object
1632 * @enable: enable phy when true
1633 *
1634 * Only support sas_host direct attached phys.
1635 * Returns 0 for success, non-zero for failure.
1636 */
1637static int
1638_transport_phy_enable(struct sas_phy *phy, int enable)
1639{
1640 struct MPT2SAS_ADAPTER *ioc = phy_to_ioc(phy);
Kashyap, Desai50d5c602009-12-16 18:55:26 +05301641 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
nagalakshmi.nandigama@lsi.com43d6ddf2012-03-20 12:08:11 +05301642 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
Kashyap, Desai50d5c602009-12-16 18:55:26 +05301643 Mpi2ConfigReply_t mpi_reply;
1644 u16 ioc_status;
1645 u16 sz;
1646 int rc = 0;
Kashyap, Desaib8d7d7b2010-06-17 13:34:31 +05301647 unsigned long flags;
nagalakshmi.nandigama@lsi.com43d6ddf2012-03-20 12:08:11 +05301648 int i, discovery_active;
Kashyap, Desai50d5c602009-12-16 18:55:26 +05301649
Kashyap, Desaib8d7d7b2010-06-17 13:34:31 +05301650 spin_lock_irqsave(&ioc->sas_node_lock, flags);
1651 if (_transport_sas_node_find_by_sas_address(ioc,
1652 phy->identify.sas_address) == NULL) {
1653 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
Kashyap, Desai50d5c602009-12-16 18:55:26 +05301654 return -EINVAL;
Kashyap, Desaib8d7d7b2010-06-17 13:34:31 +05301655 }
1656 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1657
1658 /* handle expander phys */
1659 if (phy->identify.sas_address != ioc->sas_hba.sas_address)
1660 return _transport_expander_phy_control(ioc, phy,
1661 (enable == 1) ? SMP_PHY_CONTROL_LINK_RESET :
1662 SMP_PHY_CONTROL_DISABLE);
1663
1664 /* handle hba phys */
Kashyap, Desai50d5c602009-12-16 18:55:26 +05301665
nagalakshmi.nandigama@lsi.com43d6ddf2012-03-20 12:08:11 +05301666 /* read sas_iounit page 0 */
1667 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
1668 sizeof(Mpi2SasIOUnit0PhyData_t));
1669 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
1670 if (!sas_iounit_pg0) {
1671 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1672 ioc->name, __FILE__, __LINE__, __func__);
1673 rc = -ENOMEM;
1674 goto out;
1675 }
1676 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
1677 sas_iounit_pg0, sz))) {
1678 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1679 ioc->name, __FILE__, __LINE__, __func__);
1680 rc = -ENXIO;
1681 goto out;
1682 }
1683 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1684 MPI2_IOCSTATUS_MASK;
1685 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1686 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1687 ioc->name, __FILE__, __LINE__, __func__);
1688 rc = -EIO;
1689 goto out;
1690 }
1691
1692 /* unable to enable/disable phys when when discovery is active */
1693 for (i = 0, discovery_active = 0; i < ioc->sas_hba.num_phys ; i++) {
1694 if (sas_iounit_pg0->PhyData[i].PortFlags &
1695 MPI2_SASIOUNIT0_PORTFLAGS_DISCOVERY_IN_PROGRESS) {
1696 printk(MPT2SAS_ERR_FMT "discovery is active on "
1697 "port = %d, phy = %d: unable to enable/disable "
1698 "phys, try again later!\n", ioc->name,
1699 sas_iounit_pg0->PhyData[i].Port, i);
1700 discovery_active = 1;
1701 }
1702 }
1703
1704 if (discovery_active) {
1705 rc = -EAGAIN;
1706 goto out;
1707 }
1708
1709 /* read sas_iounit page 1 */
Kashyap, Desai50d5c602009-12-16 18:55:26 +05301710 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
1711 sizeof(Mpi2SasIOUnit1PhyData_t));
1712 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
1713 if (!sas_iounit_pg1) {
1714 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1715 ioc->name, __FILE__, __LINE__, __func__);
1716 rc = -ENOMEM;
1717 goto out;
1718 }
1719 if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
1720 sas_iounit_pg1, sz))) {
1721 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1722 ioc->name, __FILE__, __LINE__, __func__);
1723 rc = -ENXIO;
1724 goto out;
1725 }
1726 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1727 MPI2_IOCSTATUS_MASK;
1728 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1729 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1730 ioc->name, __FILE__, __LINE__, __func__);
1731 rc = -EIO;
1732 goto out;
1733 }
nagalakshmi.nandigama@lsi.com43d6ddf2012-03-20 12:08:11 +05301734 /* copy Port/PortFlags/PhyFlags from page 0 */
1735 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
1736 sas_iounit_pg1->PhyData[i].Port =
1737 sas_iounit_pg0->PhyData[i].Port;
1738 sas_iounit_pg1->PhyData[i].PortFlags =
1739 (sas_iounit_pg0->PhyData[i].PortFlags &
1740 MPI2_SASIOUNIT0_PORTFLAGS_AUTO_PORT_CONFIG);
1741 sas_iounit_pg1->PhyData[i].PhyFlags =
1742 (sas_iounit_pg0->PhyData[i].PhyFlags &
1743 (MPI2_SASIOUNIT0_PHYFLAGS_ZONING_ENABLED +
1744 MPI2_SASIOUNIT0_PHYFLAGS_PHY_DISABLED));
1745 }
Kashyap, Desai50d5c602009-12-16 18:55:26 +05301746 if (enable)
Kashyap, Desaib8d7d7b2010-06-17 13:34:31 +05301747 sas_iounit_pg1->PhyData[phy->number].PhyFlags
Kashyap, Desai50d5c602009-12-16 18:55:26 +05301748 &= ~MPI2_SASIOUNIT1_PHYFLAGS_PHY_DISABLE;
1749 else
Kashyap, Desaib8d7d7b2010-06-17 13:34:31 +05301750 sas_iounit_pg1->PhyData[phy->number].PhyFlags
Kashyap, Desai50d5c602009-12-16 18:55:26 +05301751 |= MPI2_SASIOUNIT1_PHYFLAGS_PHY_DISABLE;
1752
1753 mpt2sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1, sz);
1754
Kashyap, Desaib8d7d7b2010-06-17 13:34:31 +05301755 /* link reset */
1756 if (enable)
1757 _transport_phy_reset(phy, 0);
1758
Kashyap, Desai50d5c602009-12-16 18:55:26 +05301759 out:
1760 kfree(sas_iounit_pg1);
nagalakshmi.nandigama@lsi.com43d6ddf2012-03-20 12:08:11 +05301761 kfree(sas_iounit_pg0);
Kashyap, Desai50d5c602009-12-16 18:55:26 +05301762 return rc;
1763}
1764
1765/**
1766 * _transport_phy_speed - set phy min/max link rates
1767 * @phy: The sas phy object
1768 * @rates: rates defined in sas_phy_linkrates
1769 *
1770 * Only support sas_host direct attached phys.
1771 * Returns 0 for success, non-zero for failure.
1772 */
1773static int
1774_transport_phy_speed(struct sas_phy *phy, struct sas_phy_linkrates *rates)
1775{
1776 struct MPT2SAS_ADAPTER *ioc = phy_to_ioc(phy);
Kashyap, Desai50d5c602009-12-16 18:55:26 +05301777 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
1778 Mpi2SasPhyPage0_t phy_pg0;
1779 Mpi2ConfigReply_t mpi_reply;
1780 u16 ioc_status;
1781 u16 sz;
1782 int i;
1783 int rc = 0;
Kashyap, Desaib8d7d7b2010-06-17 13:34:31 +05301784 unsigned long flags;
Kashyap, Desai50d5c602009-12-16 18:55:26 +05301785
Kashyap, Desaib8d7d7b2010-06-17 13:34:31 +05301786 spin_lock_irqsave(&ioc->sas_node_lock, flags);
1787 if (_transport_sas_node_find_by_sas_address(ioc,
1788 phy->identify.sas_address) == NULL) {
1789 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
Kashyap, Desai50d5c602009-12-16 18:55:26 +05301790 return -EINVAL;
Kashyap, Desaib8d7d7b2010-06-17 13:34:31 +05301791 }
1792 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
Kashyap, Desai50d5c602009-12-16 18:55:26 +05301793
1794 if (!rates->minimum_linkrate)
1795 rates->minimum_linkrate = phy->minimum_linkrate;
1796 else if (rates->minimum_linkrate < phy->minimum_linkrate_hw)
1797 rates->minimum_linkrate = phy->minimum_linkrate_hw;
1798
1799 if (!rates->maximum_linkrate)
1800 rates->maximum_linkrate = phy->maximum_linkrate;
1801 else if (rates->maximum_linkrate > phy->maximum_linkrate_hw)
1802 rates->maximum_linkrate = phy->maximum_linkrate_hw;
1803
Kashyap, Desaib8d7d7b2010-06-17 13:34:31 +05301804 /* handle expander phys */
1805 if (phy->identify.sas_address != ioc->sas_hba.sas_address) {
1806 phy->minimum_linkrate = rates->minimum_linkrate;
1807 phy->maximum_linkrate = rates->maximum_linkrate;
1808 return _transport_expander_phy_control(ioc, phy,
1809 SMP_PHY_CONTROL_LINK_RESET);
1810 }
1811
1812 /* handle hba phys */
1813
Kashyap, Desai50d5c602009-12-16 18:55:26 +05301814 /* sas_iounit page 1 */
1815 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
1816 sizeof(Mpi2SasIOUnit1PhyData_t));
1817 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
1818 if (!sas_iounit_pg1) {
1819 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1820 ioc->name, __FILE__, __LINE__, __func__);
1821 rc = -ENOMEM;
1822 goto out;
1823 }
1824 if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
1825 sas_iounit_pg1, sz))) {
1826 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1827 ioc->name, __FILE__, __LINE__, __func__);
1828 rc = -ENXIO;
1829 goto out;
1830 }
1831 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1832 MPI2_IOCSTATUS_MASK;
1833 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1834 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1835 ioc->name, __FILE__, __LINE__, __func__);
1836 rc = -EIO;
1837 goto out;
1838 }
1839
1840 for (i = 0; i < ioc->sas_hba.num_phys; i++) {
Kashyap, Desaib8d7d7b2010-06-17 13:34:31 +05301841 if (phy->number != i) {
Kashyap, Desai50d5c602009-12-16 18:55:26 +05301842 sas_iounit_pg1->PhyData[i].MaxMinLinkRate =
1843 (ioc->sas_hba.phy[i].phy->minimum_linkrate +
1844 (ioc->sas_hba.phy[i].phy->maximum_linkrate << 4));
1845 } else {
1846 sas_iounit_pg1->PhyData[i].MaxMinLinkRate =
1847 (rates->minimum_linkrate +
1848 (rates->maximum_linkrate << 4));
1849 }
1850 }
1851
1852 if (mpt2sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1,
1853 sz)) {
1854 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1855 ioc->name, __FILE__, __LINE__, __func__);
1856 rc = -ENXIO;
1857 goto out;
1858 }
1859
1860 /* link reset */
1861 _transport_phy_reset(phy, 0);
1862
1863 /* read phy page 0, then update the rates in the sas transport phy */
1864 if (!mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
Kashyap, Desaib8d7d7b2010-06-17 13:34:31 +05301865 phy->number)) {
Kashyap, Desai50d5c602009-12-16 18:55:26 +05301866 phy->minimum_linkrate = _transport_convert_phy_link_rate(
1867 phy_pg0.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
1868 phy->maximum_linkrate = _transport_convert_phy_link_rate(
1869 phy_pg0.ProgrammedLinkRate >> 4);
1870 phy->negotiated_linkrate = _transport_convert_phy_link_rate(
1871 phy_pg0.NegotiatedLinkRate &
1872 MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
1873 }
1874
1875 out:
1876 kfree(sas_iounit_pg1);
1877 return rc;
1878}
1879
1880
1881/**
Eric Moored5d135b2009-05-18 13:02:08 -06001882 * _transport_smp_handler - transport portal for smp passthru
Eric Moore635374e2009-03-09 01:21:12 -06001883 * @shost: shost object
1884 * @rphy: sas transport rphy object
1885 * @req:
1886 *
1887 * This used primarily for smp_utils.
1888 * Example:
1889 * smp_rep_general /sys/class/bsg/expander-5:0
1890 */
1891static int
Eric Moored5d135b2009-05-18 13:02:08 -06001892_transport_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
Eric Moore635374e2009-03-09 01:21:12 -06001893 struct request *req)
1894{
1895 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1896 Mpi2SmpPassthroughRequest_t *mpi_request;
1897 Mpi2SmpPassthroughReply_t *mpi_reply;
nagalakshmi.nandigama@lsi.com77a6edf2012-03-20 12:04:43 +05301898 int rc, i;
Eric Moore635374e2009-03-09 01:21:12 -06001899 u16 smid;
1900 u32 ioc_state;
1901 unsigned long timeleft;
1902 void *psge;
1903 u32 sgl_flags;
1904 u8 issue_reset = 0;
Eric Moore635374e2009-03-09 01:21:12 -06001905 dma_addr_t dma_addr_in = 0;
1906 dma_addr_t dma_addr_out = 0;
nagalakshmi.nandigama@lsi.com77a6edf2012-03-20 12:04:43 +05301907 dma_addr_t pci_dma_in = 0;
1908 dma_addr_t pci_dma_out = 0;
1909 void *pci_addr_in = NULL;
1910 void *pci_addr_out = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06001911 u16 wait_state_count;
1912 struct request *rsp = req->next_rq;
nagalakshmi.nandigama@lsi.com77a6edf2012-03-20 12:04:43 +05301913 struct bio_vec *bvec = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06001914
1915 if (!rsp) {
1916 printk(MPT2SAS_ERR_FMT "%s: the smp response space is "
1917 "missing\n", ioc->name, __func__);
1918 return -EINVAL;
1919 }
nagalakshmi.nandigama@lsi.com77a6edf2012-03-20 12:04:43 +05301920 if (ioc->shost_recovery || ioc->pci_error_recovery) {
Eric Moore635374e2009-03-09 01:21:12 -06001921 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
1922 __func__, ioc->name);
1923 return -EFAULT;
1924 }
Eric Moore635374e2009-03-09 01:21:12 -06001925
1926 rc = mutex_lock_interruptible(&ioc->transport_cmds.mutex);
1927 if (rc)
1928 return rc;
1929
1930 if (ioc->transport_cmds.status != MPT2_CMD_NOT_USED) {
1931 printk(MPT2SAS_ERR_FMT "%s: transport_cmds in use\n", ioc->name,
1932 __func__);
1933 rc = -EAGAIN;
1934 goto out;
1935 }
1936 ioc->transport_cmds.status = MPT2_CMD_PENDING;
1937
nagalakshmi.nandigama@lsi.com77a6edf2012-03-20 12:04:43 +05301938 /* Check if the request is split across multiple segments */
1939 if (req->bio->bi_vcnt > 1) {
1940 u32 offset = 0;
1941
1942 /* Allocate memory and copy the request */
1943 pci_addr_out = pci_alloc_consistent(ioc->pdev,
1944 blk_rq_bytes(req), &pci_dma_out);
1945 if (!pci_addr_out) {
1946 printk(MPT2SAS_INFO_FMT "%s(): PCI Addr out = NULL\n",
1947 ioc->name, __func__);
1948 rc = -ENOMEM;
1949 goto out;
1950 }
1951
1952 bio_for_each_segment(bvec, req->bio, i) {
1953 memcpy(pci_addr_out + offset,
1954 page_address(bvec->bv_page) + bvec->bv_offset,
1955 bvec->bv_len);
1956 offset += bvec->bv_len;
1957 }
1958 } else {
1959 dma_addr_out = pci_map_single(ioc->pdev, bio_data(req->bio),
1960 blk_rq_bytes(req), PCI_DMA_BIDIRECTIONAL);
1961 if (!dma_addr_out) {
1962 printk(MPT2SAS_INFO_FMT "%s(): DMA Addr out = NULL\n",
1963 ioc->name, __func__);
1964 rc = -ENOMEM;
1965 goto free_pci;
1966 }
1967 }
1968
1969 /* Check if the response needs to be populated across
1970 * multiple segments */
1971 if (rsp->bio->bi_vcnt > 1) {
1972 pci_addr_in = pci_alloc_consistent(ioc->pdev, blk_rq_bytes(rsp),
1973 &pci_dma_in);
1974 if (!pci_addr_in) {
1975 printk(MPT2SAS_INFO_FMT "%s(): PCI Addr in = NULL\n",
1976 ioc->name, __func__);
1977 rc = -ENOMEM;
1978 goto unmap;
1979 }
1980 } else {
1981 dma_addr_in = pci_map_single(ioc->pdev, bio_data(rsp->bio),
1982 blk_rq_bytes(rsp), PCI_DMA_BIDIRECTIONAL);
1983 if (!dma_addr_in) {
1984 printk(MPT2SAS_INFO_FMT "%s(): DMA Addr in = NULL\n",
1985 ioc->name, __func__);
1986 rc = -ENOMEM;
1987 goto unmap;
1988 }
1989 }
1990
Eric Moore635374e2009-03-09 01:21:12 -06001991 wait_state_count = 0;
1992 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
1993 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1994 if (wait_state_count++ == 10) {
1995 printk(MPT2SAS_ERR_FMT
1996 "%s: failed due to ioc not operational\n",
1997 ioc->name, __func__);
1998 rc = -EFAULT;
nagalakshmi.nandigama@lsi.com77a6edf2012-03-20 12:04:43 +05301999 goto unmap;
Eric Moore635374e2009-03-09 01:21:12 -06002000 }
2001 ssleep(1);
2002 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
2003 printk(MPT2SAS_INFO_FMT "%s: waiting for "
2004 "operational state(count=%d)\n", ioc->name,
2005 __func__, wait_state_count);
2006 }
2007 if (wait_state_count)
2008 printk(MPT2SAS_INFO_FMT "%s: ioc is operational\n",
2009 ioc->name, __func__);
2010
2011 smid = mpt2sas_base_get_smid(ioc, ioc->transport_cb_idx);
2012 if (!smid) {
2013 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2014 ioc->name, __func__);
2015 rc = -EAGAIN;
nagalakshmi.nandigama@lsi.com77a6edf2012-03-20 12:04:43 +05302016 goto unmap;
Eric Moore635374e2009-03-09 01:21:12 -06002017 }
2018
2019 rc = 0;
2020 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2021 ioc->transport_cmds.smid = smid;
2022
2023 memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
2024 mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
2025 mpi_request->PhysicalPort = 0xFF;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302026 mpi_request->VF_ID = 0; /* TODO */
2027 mpi_request->VP_ID = 0;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05302028 mpi_request->SASAddress = (rphy) ?
Eric Moore635374e2009-03-09 01:21:12 -06002029 cpu_to_le64(rphy->identify.sas_address) :
2030 cpu_to_le64(ioc->sas_hba.sas_address);
Tejun Heob0790412009-05-07 22:24:42 +09002031 mpi_request->RequestDataLength = cpu_to_le16(blk_rq_bytes(req) - 4);
Eric Moore635374e2009-03-09 01:21:12 -06002032 psge = &mpi_request->SGL;
2033
2034 /* WRITE sgel first */
2035 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
2036 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
2037 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
nagalakshmi.nandigama@lsi.com77a6edf2012-03-20 12:04:43 +05302038 if (req->bio->bi_vcnt > 1) {
2039 ioc->base_add_sg_single(psge, sgl_flags |
2040 (blk_rq_bytes(req) - 4), pci_dma_out);
2041 } else {
2042 ioc->base_add_sg_single(psge, sgl_flags |
2043 (blk_rq_bytes(req) - 4), dma_addr_out);
Eric Moore635374e2009-03-09 01:21:12 -06002044 }
2045
Eric Moore635374e2009-03-09 01:21:12 -06002046 /* incr sgel */
2047 psge += ioc->sge_size;
2048
2049 /* READ sgel last */
2050 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
2051 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
2052 MPI2_SGE_FLAGS_END_OF_LIST);
2053 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
nagalakshmi.nandigama@lsi.com77a6edf2012-03-20 12:04:43 +05302054 if (rsp->bio->bi_vcnt > 1) {
2055 ioc->base_add_sg_single(psge, sgl_flags |
2056 (blk_rq_bytes(rsp) + 4), pci_dma_in);
2057 } else {
2058 ioc->base_add_sg_single(psge, sgl_flags |
2059 (blk_rq_bytes(rsp) + 4), dma_addr_in);
Eric Moore635374e2009-03-09 01:21:12 -06002060 }
2061
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302062 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "%s - "
Eric Moore635374e2009-03-09 01:21:12 -06002063 "sending smp request\n", ioc->name, __func__));
2064
Kashyap, Desaibcfb6e62009-09-14 11:05:24 +05302065 init_completion(&ioc->transport_cmds.done);
nagalakshmi.nandigama@lsi.comf01690d2011-12-01 07:43:58 +05302066 mpt2sas_base_put_smid_default(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06002067 timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done,
2068 10*HZ);
2069
2070 if (!(ioc->transport_cmds.status & MPT2_CMD_COMPLETE)) {
2071 printk(MPT2SAS_ERR_FMT "%s : timeout\n",
2072 __func__, ioc->name);
2073 _debug_dump_mf(mpi_request,
2074 sizeof(Mpi2SmpPassthroughRequest_t)/4);
2075 if (!(ioc->transport_cmds.status & MPT2_CMD_RESET))
2076 issue_reset = 1;
2077 goto issue_host_reset;
2078 }
2079
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302080 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "%s - "
Eric Moore635374e2009-03-09 01:21:12 -06002081 "complete\n", ioc->name, __func__));
2082
2083 if (ioc->transport_cmds.status & MPT2_CMD_REPLY_VALID) {
2084
2085 mpi_reply = ioc->transport_cmds.reply;
2086
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302087 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -06002088 "%s - reply data transfer size(%d)\n",
2089 ioc->name, __func__,
2090 le16_to_cpu(mpi_reply->ResponseDataLength)));
2091
2092 memcpy(req->sense, mpi_reply, sizeof(*mpi_reply));
2093 req->sense_len = sizeof(*mpi_reply);
Tejun Heo5f49f632009-05-19 18:33:05 +09002094 req->resid_len = 0;
Kashyap, Desaie94f6742010-03-17 16:24:52 +05302095 rsp->resid_len -=
2096 le16_to_cpu(mpi_reply->ResponseDataLength);
nagalakshmi.nandigama@lsi.com77a6edf2012-03-20 12:04:43 +05302097 /* check if the resp needs to be copied from the allocated
2098 * pci mem */
2099 if (rsp->bio->bi_vcnt > 1) {
2100 u32 offset = 0;
2101 u32 bytes_to_copy =
2102 le16_to_cpu(mpi_reply->ResponseDataLength);
2103 bio_for_each_segment(bvec, rsp->bio, i) {
2104 if (bytes_to_copy <= bvec->bv_len) {
2105 memcpy(page_address(bvec->bv_page) +
2106 bvec->bv_offset, pci_addr_in +
2107 offset, bytes_to_copy);
2108 break;
2109 } else {
2110 memcpy(page_address(bvec->bv_page) +
2111 bvec->bv_offset, pci_addr_in +
2112 offset, bvec->bv_len);
2113 bytes_to_copy -= bvec->bv_len;
2114 }
2115 offset += bvec->bv_len;
2116 }
2117 }
Eric Moore635374e2009-03-09 01:21:12 -06002118 } else {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302119 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -06002120 "%s - no reply\n", ioc->name, __func__));
2121 rc = -ENXIO;
2122 }
2123
2124 issue_host_reset:
2125 if (issue_reset) {
2126 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2127 FORCE_BIG_HAMMER);
2128 rc = -ETIMEDOUT;
2129 }
2130
2131 unmap:
2132 if (dma_addr_out)
Tejun Heob0790412009-05-07 22:24:42 +09002133 pci_unmap_single(ioc->pdev, dma_addr_out, blk_rq_bytes(req),
Eric Moore635374e2009-03-09 01:21:12 -06002134 PCI_DMA_BIDIRECTIONAL);
2135 if (dma_addr_in)
Tejun Heob0790412009-05-07 22:24:42 +09002136 pci_unmap_single(ioc->pdev, dma_addr_in, blk_rq_bytes(rsp),
Eric Moore635374e2009-03-09 01:21:12 -06002137 PCI_DMA_BIDIRECTIONAL);
2138
nagalakshmi.nandigama@lsi.com77a6edf2012-03-20 12:04:43 +05302139 free_pci:
2140 if (pci_addr_out)
2141 pci_free_consistent(ioc->pdev, blk_rq_bytes(req), pci_addr_out,
2142 pci_dma_out);
2143
2144 if (pci_addr_in)
2145 pci_free_consistent(ioc->pdev, blk_rq_bytes(rsp), pci_addr_in,
2146 pci_dma_in);
2147
Eric Moore635374e2009-03-09 01:21:12 -06002148 out:
2149 ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
2150 mutex_unlock(&ioc->transport_cmds.mutex);
2151 return rc;
2152}
2153
2154struct sas_function_template mpt2sas_transport_functions = {
Eric Moored5d135b2009-05-18 13:02:08 -06002155 .get_linkerrors = _transport_get_linkerrors,
2156 .get_enclosure_identifier = _transport_get_enclosure_identifier,
2157 .get_bay_identifier = _transport_get_bay_identifier,
2158 .phy_reset = _transport_phy_reset,
Kashyap, Desai50d5c602009-12-16 18:55:26 +05302159 .phy_enable = _transport_phy_enable,
2160 .set_phy_speed = _transport_phy_speed,
Eric Moored5d135b2009-05-18 13:02:08 -06002161 .smp_handler = _transport_smp_handler,
Eric Moore635374e2009-03-09 01:21:12 -06002162};
2163
2164struct scsi_transport_template *mpt2sas_transport_template;