blob: a96501fdba0c806f370af4d32937b381d292a941 [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
Kashyap, Desai155dd4c2009-08-20 13:22:00 +0530143 if (ioc->shost_recovery) {
144 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
166 memset(identify, 0, sizeof(identify));
167 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;
302 u64 *sas_address_le;
303 u16 wait_state_count;
304
Kashyap, Desai155dd4c2009-08-20 13:22:00 +0530305 if (ioc->shost_recovery) {
Eric Moore635374e2009-03-09 01:21:12 -0600306 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
307 __func__, ioc->name);
308 return -EFAULT;
309 }
Eric Moore635374e2009-03-09 01:21:12 -0600310
311 mutex_lock(&ioc->transport_cmds.mutex);
312
313 if (ioc->transport_cmds.status != MPT2_CMD_NOT_USED) {
314 printk(MPT2SAS_ERR_FMT "%s: transport_cmds in use\n",
315 ioc->name, __func__);
316 rc = -EAGAIN;
317 goto out;
318 }
319 ioc->transport_cmds.status = MPT2_CMD_PENDING;
320
321 wait_state_count = 0;
322 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
323 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
324 if (wait_state_count++ == 10) {
325 printk(MPT2SAS_ERR_FMT
326 "%s: failed due to ioc not operational\n",
327 ioc->name, __func__);
328 rc = -EFAULT;
329 goto out;
330 }
331 ssleep(1);
332 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
333 printk(MPT2SAS_INFO_FMT "%s: waiting for "
334 "operational state(count=%d)\n", ioc->name,
335 __func__, wait_state_count);
336 }
337 if (wait_state_count)
338 printk(MPT2SAS_INFO_FMT "%s: ioc is operational\n",
339 ioc->name, __func__);
340
341 smid = mpt2sas_base_get_smid(ioc, ioc->transport_cb_idx);
342 if (!smid) {
343 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
344 ioc->name, __func__);
345 rc = -EAGAIN;
346 goto out;
347 }
348
349 rc = 0;
350 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
351 ioc->transport_cmds.smid = smid;
352
353 sz = sizeof(struct rep_manu_request) + sizeof(struct rep_manu_reply);
354 data_out = pci_alloc_consistent(ioc->pdev, sz, &data_out_dma);
355
356 if (!data_out) {
357 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
358 __LINE__, __func__);
359 rc = -ENOMEM;
360 mpt2sas_base_free_smid(ioc, smid);
361 goto out;
362 }
363
364 manufacture_request = data_out;
365 manufacture_request->smp_frame_type = 0x40;
366 manufacture_request->function = 1;
367 manufacture_request->reserved = 0;
368 manufacture_request->request_length = 0;
369
370 memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
371 mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
372 mpi_request->PhysicalPort = 0xFF;
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530373 mpi_request->VF_ID = 0; /* TODO */
374 mpi_request->VP_ID = 0;
Eric Moore635374e2009-03-09 01:21:12 -0600375 sas_address_le = (u64 *)&mpi_request->SASAddress;
376 *sas_address_le = cpu_to_le64(sas_address);
Kashyap, Desai463217b2009-10-05 15:53:06 +0530377 mpi_request->RequestDataLength =
378 cpu_to_le16(sizeof(struct rep_manu_request));
Eric Moore635374e2009-03-09 01:21:12 -0600379 psge = &mpi_request->SGL;
380
381 /* WRITE sgel first */
382 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
383 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
384 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
385 ioc->base_add_sg_single(psge, sgl_flags |
386 sizeof(struct rep_manu_request), data_out_dma);
387
388 /* incr sgel */
389 psge += ioc->sge_size;
390
391 /* READ sgel last */
392 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
393 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
394 MPI2_SGE_FLAGS_END_OF_LIST);
395 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
396 ioc->base_add_sg_single(psge, sgl_flags |
397 sizeof(struct rep_manu_reply), data_out_dma +
398 sizeof(struct rep_manu_request));
399
400 dtransportprintk(ioc, printk(MPT2SAS_DEBUG_FMT "report_manufacture - "
401 "send to sas_addr(0x%016llx)\n", ioc->name,
402 (unsigned long long)sas_address));
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530403 mpt2sas_base_put_smid_default(ioc, smid);
Kashyap, Desaibcfb6e62009-09-14 11:05:24 +0530404 init_completion(&ioc->transport_cmds.done);
Eric Moore635374e2009-03-09 01:21:12 -0600405 timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done,
406 10*HZ);
407
408 if (!(ioc->transport_cmds.status & MPT2_CMD_COMPLETE)) {
409 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
410 ioc->name, __func__);
411 _debug_dump_mf(mpi_request,
412 sizeof(Mpi2SmpPassthroughRequest_t)/4);
413 if (!(ioc->transport_cmds.status & MPT2_CMD_RESET))
414 issue_reset = 1;
415 goto issue_host_reset;
416 }
417
418 dtransportprintk(ioc, printk(MPT2SAS_DEBUG_FMT "report_manufacture - "
419 "complete\n", ioc->name));
420
421 if (ioc->transport_cmds.status & MPT2_CMD_REPLY_VALID) {
422 u8 *tmp;
423
424 mpi_reply = ioc->transport_cmds.reply;
425
426 dtransportprintk(ioc, printk(MPT2SAS_DEBUG_FMT
427 "report_manufacture - reply data transfer size(%d)\n",
428 ioc->name, le16_to_cpu(mpi_reply->ResponseDataLength)));
429
430 if (le16_to_cpu(mpi_reply->ResponseDataLength) !=
431 sizeof(struct rep_manu_reply))
432 goto out;
433
434 manufacture_reply = data_out + sizeof(struct rep_manu_request);
435 strncpy(edev->vendor_id, manufacture_reply->vendor_id,
436 SAS_EXPANDER_VENDOR_ID_LEN);
437 strncpy(edev->product_id, manufacture_reply->product_id,
438 SAS_EXPANDER_PRODUCT_ID_LEN);
439 strncpy(edev->product_rev, manufacture_reply->product_rev,
440 SAS_EXPANDER_PRODUCT_REV_LEN);
Kashyap, Desai463217b2009-10-05 15:53:06 +0530441 edev->level = manufacture_reply->sas_format & 1;
442 if (edev->level) {
Eric Moore635374e2009-03-09 01:21:12 -0600443 strncpy(edev->component_vendor_id,
444 manufacture_reply->component_vendor_id,
445 SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN);
446 tmp = (u8 *)&manufacture_reply->component_id;
447 edev->component_id = tmp[0] << 8 | tmp[1];
448 edev->component_revision_id =
449 manufacture_reply->component_revision_id;
450 }
451 } else
452 dtransportprintk(ioc, printk(MPT2SAS_DEBUG_FMT
453 "report_manufacture - no reply\n", ioc->name));
454
455 issue_host_reset:
456 if (issue_reset)
457 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
458 FORCE_BIG_HAMMER);
459 out:
460 ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
461 if (data_out)
462 pci_free_consistent(ioc->pdev, sz, data_out, data_out_dma);
463
464 mutex_unlock(&ioc->transport_cmds.mutex);
465 return rc;
466}
467
Kashyap, Desai38c29112010-04-05 14:18:34 +0530468
469/**
470 * _transport_delete_duplicate_port - (see below description)
471 * @ioc: per adapter object
472 * @sas_node: sas node object (either expander or sas host)
473 * @sas_address: sas address of device being added
474 * @phy_num: phy number
475 *
476 * This function is called when attempting to add a new port that is claiming
477 * the same phy resources already in use by another port. If we don't release
478 * the claimed phy resources, the sas transport layer will hang from the BUG
479 * in sas_port_add_phy.
480 *
481 * The reason we would hit this issue is becuase someone is changing the
482 * sas address of a device on the fly, meanwhile controller firmware sends
483 * EVENTs out of order when removing the previous instance of the device.
484 */
485static void
486_transport_delete_duplicate_port(struct MPT2SAS_ADAPTER *ioc,
487 struct _sas_node *sas_node, u64 sas_address, int phy_num)
488{
489 struct _sas_port *mpt2sas_port, *mpt2sas_port_duplicate;
490 struct _sas_phy *mpt2sas_phy;
491
492 printk(MPT2SAS_ERR_FMT "new device located at sas_addr(0x%016llx), "
493 "phy_id(%d)\n", ioc->name, (unsigned long long)sas_address,
494 phy_num);
495
496 mpt2sas_port_duplicate = NULL;
497 list_for_each_entry(mpt2sas_port, &sas_node->sas_port_list, port_list) {
498 dev_printk(KERN_ERR, &mpt2sas_port->port->dev,
499 "existing device at sas_addr(0x%016llx), num_phys(%d)\n",
500 (unsigned long long)
501 mpt2sas_port->remote_identify.sas_address,
502 mpt2sas_port->num_phys);
503 list_for_each_entry(mpt2sas_phy, &mpt2sas_port->phy_list,
504 port_siblings) {
505 dev_printk(KERN_ERR, &mpt2sas_phy->phy->dev,
506 "phy_number(%d)\n", mpt2sas_phy->phy_id);
507 if (mpt2sas_phy->phy_id == phy_num)
508 mpt2sas_port_duplicate = mpt2sas_port;
509 }
510 }
511
512 if (!mpt2sas_port_duplicate)
513 return;
514
515 dev_printk(KERN_ERR, &mpt2sas_port_duplicate->port->dev,
516 "deleting duplicate device at sas_addr(0x%016llx), phy(%d)!!!!\n",
517 (unsigned long long)
518 mpt2sas_port_duplicate->remote_identify.sas_address, phy_num);
519 ioc->logging_level |= MPT_DEBUG_TRANSPORT;
520 mpt2sas_transport_port_remove(ioc,
521 mpt2sas_port_duplicate->remote_identify.sas_address,
522 sas_node->sas_address);
523 ioc->logging_level &= ~MPT_DEBUG_TRANSPORT;
524}
525
526/**
527 * _transport_sanity_check - sanity check when adding a new port
528 * @ioc: per adapter object
529 * @sas_node: sas node object (either expander or sas host)
530 * @sas_address: sas address of device being added
531 *
532 * See the explanation above from _transport_delete_duplicate_port
533 */
534static void
535_transport_sanity_check(struct MPT2SAS_ADAPTER *ioc, struct _sas_node *sas_node,
536 u64 sas_address)
537{
538 int i;
539
540 for (i = 0; i < sas_node->num_phys; i++)
541 if (sas_node->phy[i].remote_identify.sas_address == sas_address)
542 if (sas_node->phy[i].phy_belongs_to_port)
543 _transport_delete_duplicate_port(ioc, sas_node,
544 sas_address, i);
545}
546
Eric Moore635374e2009-03-09 01:21:12 -0600547/**
548 * mpt2sas_transport_port_add - insert port to the list
549 * @ioc: per adapter object
550 * @handle: handle of attached device
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530551 * @sas_address: sas address of parent expander or sas host
Eric Moore635374e2009-03-09 01:21:12 -0600552 * Context: This function will acquire ioc->sas_node_lock.
553 *
554 * Adding new port object to the sas_node->sas_port_list.
555 *
556 * Returns mpt2sas_port.
557 */
558struct _sas_port *
559mpt2sas_transport_port_add(struct MPT2SAS_ADAPTER *ioc, u16 handle,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530560 u64 sas_address)
Eric Moore635374e2009-03-09 01:21:12 -0600561{
562 struct _sas_phy *mpt2sas_phy, *next;
563 struct _sas_port *mpt2sas_port;
564 unsigned long flags;
565 struct _sas_node *sas_node;
566 struct sas_rphy *rphy;
567 int i;
568 struct sas_port *port;
569
Eric Moore635374e2009-03-09 01:21:12 -0600570 mpt2sas_port = kzalloc(sizeof(struct _sas_port),
571 GFP_KERNEL);
572 if (!mpt2sas_port) {
573 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
574 ioc->name, __FILE__, __LINE__, __func__);
575 return NULL;
576 }
577
578 INIT_LIST_HEAD(&mpt2sas_port->port_list);
579 INIT_LIST_HEAD(&mpt2sas_port->phy_list);
580 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530581 sas_node = _transport_sas_node_find_by_sas_address(ioc, sas_address);
Eric Moore635374e2009-03-09 01:21:12 -0600582 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
583
584 if (!sas_node) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530585 printk(MPT2SAS_ERR_FMT "%s: Could not find "
586 "parent sas_address(0x%016llx)!\n", ioc->name,
587 __func__, (unsigned long long)sas_address);
Eric Moore635374e2009-03-09 01:21:12 -0600588 goto out_fail;
589 }
590
Eric Moore635374e2009-03-09 01:21:12 -0600591 if ((_transport_set_identify(ioc, handle,
592 &mpt2sas_port->remote_identify))) {
593 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
594 ioc->name, __FILE__, __LINE__, __func__);
595 goto out_fail;
596 }
597
598 if (mpt2sas_port->remote_identify.device_type == SAS_PHY_UNUSED) {
599 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
600 ioc->name, __FILE__, __LINE__, __func__);
601 goto out_fail;
602 }
603
Kashyap, Desai38c29112010-04-05 14:18:34 +0530604 _transport_sanity_check(ioc, sas_node,
605 mpt2sas_port->remote_identify.sas_address);
606
Eric Moore635374e2009-03-09 01:21:12 -0600607 for (i = 0; i < sas_node->num_phys; i++) {
608 if (sas_node->phy[i].remote_identify.sas_address !=
609 mpt2sas_port->remote_identify.sas_address)
610 continue;
611 list_add_tail(&sas_node->phy[i].port_siblings,
612 &mpt2sas_port->phy_list);
613 mpt2sas_port->num_phys++;
614 }
615
616 if (!mpt2sas_port->num_phys) {
617 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
618 ioc->name, __FILE__, __LINE__, __func__);
619 goto out_fail;
620 }
621
622 port = sas_port_alloc_num(sas_node->parent_dev);
623 if ((sas_port_add(port))) {
624 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
625 ioc->name, __FILE__, __LINE__, __func__);
626 goto out_fail;
627 }
628
629 list_for_each_entry(mpt2sas_phy, &mpt2sas_port->phy_list,
630 port_siblings) {
631 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
632 dev_printk(KERN_INFO, &port->dev, "add: handle(0x%04x)"
633 ", sas_addr(0x%016llx), phy(%d)\n", handle,
634 (unsigned long long)
635 mpt2sas_port->remote_identify.sas_address,
636 mpt2sas_phy->phy_id);
637 sas_port_add_phy(port, mpt2sas_phy->phy);
Kashyap, Desai38c29112010-04-05 14:18:34 +0530638 mpt2sas_phy->phy_belongs_to_port = 1;
Eric Moore635374e2009-03-09 01:21:12 -0600639 }
640
641 mpt2sas_port->port = port;
642 if (mpt2sas_port->remote_identify.device_type == SAS_END_DEVICE)
643 rphy = sas_end_device_alloc(port);
644 else
645 rphy = sas_expander_alloc(port,
646 mpt2sas_port->remote_identify.device_type);
647
648 rphy->identify = mpt2sas_port->remote_identify;
649 if ((sas_rphy_add(rphy))) {
650 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
651 ioc->name, __FILE__, __LINE__, __func__);
652 }
653 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
654 dev_printk(KERN_INFO, &rphy->dev, "add: handle(0x%04x), "
655 "sas_addr(0x%016llx)\n", handle,
656 (unsigned long long)
657 mpt2sas_port->remote_identify.sas_address);
658 mpt2sas_port->rphy = rphy;
659 spin_lock_irqsave(&ioc->sas_node_lock, flags);
660 list_add_tail(&mpt2sas_port->port_list, &sas_node->sas_port_list);
661 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
662
663 /* fill in report manufacture */
664 if (mpt2sas_port->remote_identify.device_type ==
665 MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
666 mpt2sas_port->remote_identify.device_type ==
667 MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER)
Eric Moored5d135b2009-05-18 13:02:08 -0600668 _transport_expander_report_manufacture(ioc,
Eric Moore635374e2009-03-09 01:21:12 -0600669 mpt2sas_port->remote_identify.sas_address,
670 rphy_to_expander_device(rphy));
671
672 return mpt2sas_port;
673
674 out_fail:
675 list_for_each_entry_safe(mpt2sas_phy, next, &mpt2sas_port->phy_list,
676 port_siblings)
677 list_del(&mpt2sas_phy->port_siblings);
678 kfree(mpt2sas_port);
679 return NULL;
680}
681
682/**
683 * mpt2sas_transport_port_remove - remove port from the list
684 * @ioc: per adapter object
685 * @sas_address: sas address of attached device
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530686 * @sas_address_parent: sas address of parent expander or sas host
Eric Moore635374e2009-03-09 01:21:12 -0600687 * Context: This function will acquire ioc->sas_node_lock.
688 *
689 * Removing object and freeing associated memory from the
690 * ioc->sas_port_list.
691 *
692 * Return nothing.
693 */
694void
695mpt2sas_transport_port_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530696 u64 sas_address_parent)
Eric Moore635374e2009-03-09 01:21:12 -0600697{
698 int i;
699 unsigned long flags;
700 struct _sas_port *mpt2sas_port, *next;
701 struct _sas_node *sas_node;
702 u8 found = 0;
703 struct _sas_phy *mpt2sas_phy, *next_phy;
704
705 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530706 sas_node = _transport_sas_node_find_by_sas_address(ioc,
707 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -0600708 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
709 if (!sas_node)
710 return;
711 list_for_each_entry_safe(mpt2sas_port, next, &sas_node->sas_port_list,
712 port_list) {
713 if (mpt2sas_port->remote_identify.sas_address != sas_address)
714 continue;
715 found = 1;
716 list_del(&mpt2sas_port->port_list);
717 goto out;
718 }
719 out:
720 if (!found)
721 return;
722
723 for (i = 0; i < sas_node->num_phys; i++) {
724 if (sas_node->phy[i].remote_identify.sas_address == sas_address)
725 memset(&sas_node->phy[i].remote_identify, 0 ,
726 sizeof(struct sas_identify));
727 }
728
729 list_for_each_entry_safe(mpt2sas_phy, next_phy,
730 &mpt2sas_port->phy_list, port_siblings) {
731 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
732 dev_printk(KERN_INFO, &mpt2sas_port->port->dev,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530733 "remove: sas_addr(0x%016llx), phy(%d)\n",
Eric Moore635374e2009-03-09 01:21:12 -0600734 (unsigned long long)
735 mpt2sas_port->remote_identify.sas_address,
736 mpt2sas_phy->phy_id);
Kashyap, Desai38c29112010-04-05 14:18:34 +0530737 mpt2sas_phy->phy_belongs_to_port = 0;
Eric Moore635374e2009-03-09 01:21:12 -0600738 sas_port_delete_phy(mpt2sas_port->port, mpt2sas_phy->phy);
739 list_del(&mpt2sas_phy->port_siblings);
740 }
741 sas_port_delete(mpt2sas_port->port);
742 kfree(mpt2sas_port);
743}
744
745/**
746 * mpt2sas_transport_add_host_phy - report sas_host phy to transport
747 * @ioc: per adapter object
748 * @mpt2sas_phy: mpt2sas per phy object
749 * @phy_pg0: sas phy page 0
750 * @parent_dev: parent device class object
751 *
752 * Returns 0 for success, non-zero for failure.
753 */
754int
755mpt2sas_transport_add_host_phy(struct MPT2SAS_ADAPTER *ioc, struct _sas_phy
756 *mpt2sas_phy, Mpi2SasPhyPage0_t phy_pg0, struct device *parent_dev)
757{
758 struct sas_phy *phy;
759 int phy_index = mpt2sas_phy->phy_id;
760
761
762 INIT_LIST_HEAD(&mpt2sas_phy->port_siblings);
763 phy = sas_phy_alloc(parent_dev, phy_index);
764 if (!phy) {
765 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
766 ioc->name, __FILE__, __LINE__, __func__);
767 return -1;
768 }
769 if ((_transport_set_identify(ioc, mpt2sas_phy->handle,
770 &mpt2sas_phy->identify))) {
771 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
772 ioc->name, __FILE__, __LINE__, __func__);
773 return -1;
774 }
775 phy->identify = mpt2sas_phy->identify;
776 mpt2sas_phy->attached_handle = le16_to_cpu(phy_pg0.AttachedDevHandle);
777 if (mpt2sas_phy->attached_handle)
778 _transport_set_identify(ioc, mpt2sas_phy->attached_handle,
779 &mpt2sas_phy->remote_identify);
780 phy->identify.phy_identifier = mpt2sas_phy->phy_id;
781 phy->negotiated_linkrate = _transport_convert_phy_link_rate(
782 phy_pg0.NegotiatedLinkRate & MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
783 phy->minimum_linkrate_hw = _transport_convert_phy_link_rate(
784 phy_pg0.HwLinkRate & MPI2_SAS_HWRATE_MIN_RATE_MASK);
785 phy->maximum_linkrate_hw = _transport_convert_phy_link_rate(
786 phy_pg0.HwLinkRate >> 4);
787 phy->minimum_linkrate = _transport_convert_phy_link_rate(
788 phy_pg0.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
789 phy->maximum_linkrate = _transport_convert_phy_link_rate(
790 phy_pg0.ProgrammedLinkRate >> 4);
791
792 if ((sas_phy_add(phy))) {
793 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
794 ioc->name, __FILE__, __LINE__, __func__);
795 sas_phy_free(phy);
796 return -1;
797 }
798 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
799 dev_printk(KERN_INFO, &phy->dev,
800 "add: handle(0x%04x), sas_addr(0x%016llx)\n"
801 "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
802 mpt2sas_phy->handle, (unsigned long long)
803 mpt2sas_phy->identify.sas_address,
804 mpt2sas_phy->attached_handle,
805 (unsigned long long)
806 mpt2sas_phy->remote_identify.sas_address);
807 mpt2sas_phy->phy = phy;
808 return 0;
809}
810
811
812/**
813 * mpt2sas_transport_add_expander_phy - report expander phy to transport
814 * @ioc: per adapter object
815 * @mpt2sas_phy: mpt2sas per phy object
816 * @expander_pg1: expander page 1
817 * @parent_dev: parent device class object
818 *
819 * Returns 0 for success, non-zero for failure.
820 */
821int
822mpt2sas_transport_add_expander_phy(struct MPT2SAS_ADAPTER *ioc, struct _sas_phy
823 *mpt2sas_phy, Mpi2ExpanderPage1_t expander_pg1, struct device *parent_dev)
824{
825 struct sas_phy *phy;
826 int phy_index = mpt2sas_phy->phy_id;
827
828 INIT_LIST_HEAD(&mpt2sas_phy->port_siblings);
829 phy = sas_phy_alloc(parent_dev, phy_index);
830 if (!phy) {
831 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
832 ioc->name, __FILE__, __LINE__, __func__);
833 return -1;
834 }
835 if ((_transport_set_identify(ioc, mpt2sas_phy->handle,
836 &mpt2sas_phy->identify))) {
837 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
838 ioc->name, __FILE__, __LINE__, __func__);
839 return -1;
840 }
841 phy->identify = mpt2sas_phy->identify;
842 mpt2sas_phy->attached_handle =
843 le16_to_cpu(expander_pg1.AttachedDevHandle);
844 if (mpt2sas_phy->attached_handle)
845 _transport_set_identify(ioc, mpt2sas_phy->attached_handle,
846 &mpt2sas_phy->remote_identify);
847 phy->identify.phy_identifier = mpt2sas_phy->phy_id;
848 phy->negotiated_linkrate = _transport_convert_phy_link_rate(
849 expander_pg1.NegotiatedLinkRate &
850 MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
851 phy->minimum_linkrate_hw = _transport_convert_phy_link_rate(
852 expander_pg1.HwLinkRate & MPI2_SAS_HWRATE_MIN_RATE_MASK);
853 phy->maximum_linkrate_hw = _transport_convert_phy_link_rate(
854 expander_pg1.HwLinkRate >> 4);
855 phy->minimum_linkrate = _transport_convert_phy_link_rate(
856 expander_pg1.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
857 phy->maximum_linkrate = _transport_convert_phy_link_rate(
858 expander_pg1.ProgrammedLinkRate >> 4);
859
860 if ((sas_phy_add(phy))) {
861 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
862 ioc->name, __FILE__, __LINE__, __func__);
863 sas_phy_free(phy);
864 return -1;
865 }
866 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
867 dev_printk(KERN_INFO, &phy->dev,
868 "add: handle(0x%04x), sas_addr(0x%016llx)\n"
869 "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
870 mpt2sas_phy->handle, (unsigned long long)
871 mpt2sas_phy->identify.sas_address,
872 mpt2sas_phy->attached_handle,
873 (unsigned long long)
874 mpt2sas_phy->remote_identify.sas_address);
875 mpt2sas_phy->phy = phy;
876 return 0;
877}
878
879/**
Kashyap, Desaicc0f5202009-08-20 13:22:39 +0530880 * mpt2sas_transport_update_links - refreshing phy link changes
Eric Moore635374e2009-03-09 01:21:12 -0600881 * @ioc: per adapter object
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530882 * @sas_address: sas address of parent expander or sas host
883 * @handle: attached device handle
Eric Moore635374e2009-03-09 01:21:12 -0600884 * @phy_numberv: phy number
885 * @link_rate: new link rate
886 *
887 * Returns nothing.
888 */
889void
Kashyap, Desaicc0f5202009-08-20 13:22:39 +0530890mpt2sas_transport_update_links(struct MPT2SAS_ADAPTER *ioc,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530891 u64 sas_address, u16 handle, u8 phy_number, u8 link_rate)
Eric Moore635374e2009-03-09 01:21:12 -0600892{
893 unsigned long flags;
894 struct _sas_node *sas_node;
895 struct _sas_phy *mpt2sas_phy;
896
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530897 if (ioc->shost_recovery)
Kashyap, Desai155dd4c2009-08-20 13:22:00 +0530898 return;
Kashyap, Desai155dd4c2009-08-20 13:22:00 +0530899
Eric Moore635374e2009-03-09 01:21:12 -0600900 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530901 sas_node = _transport_sas_node_find_by_sas_address(ioc, sas_address);
Eric Moore635374e2009-03-09 01:21:12 -0600902 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
903 if (!sas_node)
904 return;
905
906 mpt2sas_phy = &sas_node->phy[phy_number];
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530907 mpt2sas_phy->attached_handle = handle;
908 if (handle && (link_rate >= MPI2_SAS_NEG_LINK_RATE_1_5))
909 _transport_set_identify(ioc, handle,
Eric Moore635374e2009-03-09 01:21:12 -0600910 &mpt2sas_phy->remote_identify);
911 else
912 memset(&mpt2sas_phy->remote_identify, 0 , sizeof(struct
913 sas_identify));
914
915 if (mpt2sas_phy->phy)
916 mpt2sas_phy->phy->negotiated_linkrate =
917 _transport_convert_phy_link_rate(link_rate);
918
919 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
920 dev_printk(KERN_INFO, &mpt2sas_phy->phy->dev,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530921 "refresh: parent sas_addr(0x%016llx),\n"
Eric Moore635374e2009-03-09 01:21:12 -0600922 "\tlink_rate(0x%02x), phy(%d)\n"
923 "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530924 (unsigned long long)sas_address,
925 link_rate, phy_number, handle, (unsigned long long)
Eric Moore635374e2009-03-09 01:21:12 -0600926 mpt2sas_phy->remote_identify.sas_address);
927}
928
929static inline void *
930phy_to_ioc(struct sas_phy *phy)
931{
932 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
933 return shost_priv(shost);
934}
935
936static inline void *
937rphy_to_ioc(struct sas_rphy *rphy)
938{
939 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
940 return shost_priv(shost);
941}
942
Kashyap, Desai50d5c602009-12-16 18:55:26 +0530943static struct _sas_phy *
944_transport_find_local_phy(struct MPT2SAS_ADAPTER *ioc, struct sas_phy *phy)
945{
946 int i;
947
948 for (i = 0; i < ioc->sas_hba.num_phys; i++)
949 if (ioc->sas_hba.phy[i].phy == phy)
950 return(&ioc->sas_hba.phy[i]);
951 return NULL;
952}
953
Kashyap, Desaid5f491e2010-06-17 13:32:54 +0530954/* report phy error log structure */
955struct phy_error_log_request{
956 u8 smp_frame_type; /* 0x40 */
957 u8 function; /* 0x11 */
958 u8 allocated_response_length;
959 u8 request_length; /* 02 */
960 u8 reserved_1[5];
961 u8 phy_identifier;
962 u8 reserved_2[2];
963};
964
965/* report phy error log reply structure */
966struct phy_error_log_reply{
967 u8 smp_frame_type; /* 0x41 */
968 u8 function; /* 0x11 */
969 u8 function_result;
970 u8 response_length;
971 u16 expander_change_count;
972 u8 reserved_1[3];
973 u8 phy_identifier;
974 u8 reserved_2[2];
975 u32 invalid_dword;
976 u32 running_disparity_error;
977 u32 loss_of_dword_sync;
978 u32 phy_reset_problem;
979};
980
Eric Moore635374e2009-03-09 01:21:12 -0600981/**
Kashyap, Desaid5f491e2010-06-17 13:32:54 +0530982 * _transport_get_expander_phy_error_log - return expander counters
983 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -0600984 * @phy: The sas phy object
985 *
Kashyap, Desaid5f491e2010-06-17 13:32:54 +0530986 * Returns 0 for success, non-zero for failure.
987 *
988 */
989static int
990_transport_get_expander_phy_error_log(struct MPT2SAS_ADAPTER *ioc,
991 struct sas_phy *phy)
992{
993 Mpi2SmpPassthroughRequest_t *mpi_request;
994 Mpi2SmpPassthroughReply_t *mpi_reply;
995 struct phy_error_log_request *phy_error_log_request;
996 struct phy_error_log_reply *phy_error_log_reply;
997 int rc;
998 u16 smid;
999 u32 ioc_state;
1000 unsigned long timeleft;
1001 void *psge;
1002 u32 sgl_flags;
1003 u8 issue_reset = 0;
1004 void *data_out = NULL;
1005 dma_addr_t data_out_dma;
1006 u32 sz;
1007 u64 *sas_address_le;
1008 u16 wait_state_count;
1009
1010 if (ioc->shost_recovery) {
1011 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
1012 __func__, ioc->name);
1013 return -EFAULT;
1014 }
1015
1016 mutex_lock(&ioc->transport_cmds.mutex);
1017
1018 if (ioc->transport_cmds.status != MPT2_CMD_NOT_USED) {
1019 printk(MPT2SAS_ERR_FMT "%s: transport_cmds in use\n",
1020 ioc->name, __func__);
1021 rc = -EAGAIN;
1022 goto out;
1023 }
1024 ioc->transport_cmds.status = MPT2_CMD_PENDING;
1025
1026 wait_state_count = 0;
1027 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
1028 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1029 if (wait_state_count++ == 10) {
1030 printk(MPT2SAS_ERR_FMT
1031 "%s: failed due to ioc not operational\n",
1032 ioc->name, __func__);
1033 rc = -EFAULT;
1034 goto out;
1035 }
1036 ssleep(1);
1037 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
1038 printk(MPT2SAS_INFO_FMT "%s: waiting for "
1039 "operational state(count=%d)\n", ioc->name,
1040 __func__, wait_state_count);
1041 }
1042 if (wait_state_count)
1043 printk(MPT2SAS_INFO_FMT "%s: ioc is operational\n",
1044 ioc->name, __func__);
1045
1046 smid = mpt2sas_base_get_smid(ioc, ioc->transport_cb_idx);
1047 if (!smid) {
1048 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1049 ioc->name, __func__);
1050 rc = -EAGAIN;
1051 goto out;
1052 }
1053
1054 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1055 ioc->transport_cmds.smid = smid;
1056
1057 sz = sizeof(struct phy_error_log_request) +
1058 sizeof(struct phy_error_log_reply);
1059 data_out = pci_alloc_consistent(ioc->pdev, sz, &data_out_dma);
1060 if (!data_out) {
1061 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
1062 __LINE__, __func__);
1063 rc = -ENOMEM;
1064 mpt2sas_base_free_smid(ioc, smid);
1065 goto out;
1066 }
1067
1068 rc = -EINVAL;
1069 memset(data_out, 0, sz);
1070 phy_error_log_request = data_out;
1071 phy_error_log_request->smp_frame_type = 0x40;
1072 phy_error_log_request->function = 0x11;
1073 phy_error_log_request->request_length = 2;
1074 phy_error_log_request->allocated_response_length = 0;
1075 phy_error_log_request->phy_identifier = phy->number;
1076
1077 memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
1078 mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
1079 mpi_request->PhysicalPort = 0xFF;
1080 mpi_request->VF_ID = 0; /* TODO */
1081 mpi_request->VP_ID = 0;
1082 sas_address_le = (u64 *)&mpi_request->SASAddress;
1083 *sas_address_le = cpu_to_le64(phy->identify.sas_address);
1084 mpi_request->RequestDataLength =
1085 cpu_to_le16(sizeof(struct phy_error_log_request));
1086 psge = &mpi_request->SGL;
1087
1088 /* WRITE sgel first */
1089 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1090 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
1091 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1092 ioc->base_add_sg_single(psge, sgl_flags |
1093 sizeof(struct phy_error_log_request), data_out_dma);
1094
1095 /* incr sgel */
1096 psge += ioc->sge_size;
1097
1098 /* READ sgel last */
1099 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1100 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
1101 MPI2_SGE_FLAGS_END_OF_LIST);
1102 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1103 ioc->base_add_sg_single(psge, sgl_flags |
1104 sizeof(struct phy_error_log_reply), data_out_dma +
1105 sizeof(struct phy_error_log_request));
1106
1107 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "phy_error_log - "
1108 "send to sas_addr(0x%016llx), phy(%d)\n", ioc->name,
1109 (unsigned long long)phy->identify.sas_address, phy->number));
1110 mpt2sas_base_put_smid_default(ioc, smid);
1111 init_completion(&ioc->transport_cmds.done);
1112 timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done,
1113 10*HZ);
1114
1115 if (!(ioc->transport_cmds.status & MPT2_CMD_COMPLETE)) {
1116 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
1117 ioc->name, __func__);
1118 _debug_dump_mf(mpi_request,
1119 sizeof(Mpi2SmpPassthroughRequest_t)/4);
1120 if (!(ioc->transport_cmds.status & MPT2_CMD_RESET))
1121 issue_reset = 1;
1122 goto issue_host_reset;
1123 }
1124
1125 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "phy_error_log - "
1126 "complete\n", ioc->name));
1127
1128 if (ioc->transport_cmds.status & MPT2_CMD_REPLY_VALID) {
1129
1130 mpi_reply = ioc->transport_cmds.reply;
1131
1132 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
1133 "phy_error_log - reply data transfer size(%d)\n",
1134 ioc->name, le16_to_cpu(mpi_reply->ResponseDataLength)));
1135
1136 if (le16_to_cpu(mpi_reply->ResponseDataLength) !=
1137 sizeof(struct phy_error_log_reply))
1138 goto out;
1139
1140 phy_error_log_reply = data_out +
1141 sizeof(struct phy_error_log_request);
1142
1143 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
1144 "phy_error_log - function_result(%d)\n",
1145 ioc->name, phy_error_log_reply->function_result));
1146
1147 phy->invalid_dword_count =
1148 be32_to_cpu(phy_error_log_reply->invalid_dword);
1149 phy->running_disparity_error_count =
1150 be32_to_cpu(phy_error_log_reply->running_disparity_error);
1151 phy->loss_of_dword_sync_count =
1152 be32_to_cpu(phy_error_log_reply->loss_of_dword_sync);
1153 phy->phy_reset_problem_count =
1154 be32_to_cpu(phy_error_log_reply->phy_reset_problem);
1155 rc = 0;
1156 } else
1157 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
1158 "phy_error_log - no reply\n", ioc->name));
1159
1160 issue_host_reset:
1161 if (issue_reset)
1162 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1163 FORCE_BIG_HAMMER);
1164 out:
1165 ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
1166 if (data_out)
1167 pci_free_consistent(ioc->pdev, sz, data_out, data_out_dma);
1168
1169 mutex_unlock(&ioc->transport_cmds.mutex);
1170 return rc;
1171}
1172
1173/**
1174 * _transport_get_linkerrors - return phy counters for both hba and expanders
1175 * @phy: The sas phy object
1176 *
Eric Moore635374e2009-03-09 01:21:12 -06001177 * Returns 0 for success, non-zero for failure.
1178 *
1179 */
1180static int
Eric Moored5d135b2009-05-18 13:02:08 -06001181_transport_get_linkerrors(struct sas_phy *phy)
Eric Moore635374e2009-03-09 01:21:12 -06001182{
1183 struct MPT2SAS_ADAPTER *ioc = phy_to_ioc(phy);
Kashyap, Desaid5f491e2010-06-17 13:32:54 +05301184 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06001185 Mpi2ConfigReply_t mpi_reply;
1186 Mpi2SasPhyPage1_t phy_pg1;
Eric Moore635374e2009-03-09 01:21:12 -06001187
Kashyap, Desaid5f491e2010-06-17 13:32:54 +05301188 spin_lock_irqsave(&ioc->sas_node_lock, flags);
1189 if (_transport_sas_node_find_by_sas_address(ioc,
1190 phy->identify.sas_address) == NULL) {
1191 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06001192 return -EINVAL;
Kashyap, Desaid5f491e2010-06-17 13:32:54 +05301193 }
1194 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06001195
Kashyap, Desaid5f491e2010-06-17 13:32:54 +05301196 if (phy->identify.sas_address != ioc->sas_hba.sas_address)
1197 return _transport_get_expander_phy_error_log(ioc, phy);
1198
1199 /* get hba phy error logs */
Eric Moore635374e2009-03-09 01:21:12 -06001200 if ((mpt2sas_config_get_phy_pg1(ioc, &mpi_reply, &phy_pg1,
Kashyap, Desaid5f491e2010-06-17 13:32:54 +05301201 phy->number))) {
Eric Moore635374e2009-03-09 01:21:12 -06001202 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1203 ioc->name, __FILE__, __LINE__, __func__);
1204 return -ENXIO;
1205 }
1206
1207 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo)
1208 printk(MPT2SAS_INFO_FMT "phy(%d), ioc_status"
1209 "(0x%04x), loginfo(0x%08x)\n", ioc->name,
Kashyap, Desaid5f491e2010-06-17 13:32:54 +05301210 phy->number, le16_to_cpu(mpi_reply.IOCStatus),
Eric Moore635374e2009-03-09 01:21:12 -06001211 le32_to_cpu(mpi_reply.IOCLogInfo));
1212
1213 phy->invalid_dword_count = le32_to_cpu(phy_pg1.InvalidDwordCount);
1214 phy->running_disparity_error_count =
1215 le32_to_cpu(phy_pg1.RunningDisparityErrorCount);
1216 phy->loss_of_dword_sync_count =
1217 le32_to_cpu(phy_pg1.LossDwordSynchCount);
1218 phy->phy_reset_problem_count =
1219 le32_to_cpu(phy_pg1.PhyResetProblemCount);
1220 return 0;
1221}
1222
1223/**
Eric Moored5d135b2009-05-18 13:02:08 -06001224 * _transport_get_enclosure_identifier -
Eric Moore635374e2009-03-09 01:21:12 -06001225 * @phy: The sas phy object
1226 *
1227 * Obtain the enclosure logical id for an expander.
1228 * Returns 0 for success, non-zero for failure.
1229 */
1230static int
Eric Moored5d135b2009-05-18 13:02:08 -06001231_transport_get_enclosure_identifier(struct sas_rphy *rphy, u64 *identifier)
Eric Moore635374e2009-03-09 01:21:12 -06001232{
1233 struct MPT2SAS_ADAPTER *ioc = rphy_to_ioc(rphy);
Kashyap, Desaiab6ce922010-06-17 13:25:13 +05301234 struct _sas_device *sas_device;
Eric Moore635374e2009-03-09 01:21:12 -06001235 unsigned long flags;
1236
Kashyap, Desaiab6ce922010-06-17 13:25:13 +05301237 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1238 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
Eric Moore635374e2009-03-09 01:21:12 -06001239 rphy->identify.sas_address);
Kashyap, Desaiab6ce922010-06-17 13:25:13 +05301240 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06001241
Kashyap, Desaiab6ce922010-06-17 13:25:13 +05301242 if (!sas_device)
Eric Moore635374e2009-03-09 01:21:12 -06001243 return -ENXIO;
1244
Kashyap, Desaiab6ce922010-06-17 13:25:13 +05301245 *identifier = sas_device->enclosure_logical_id;
Eric Moore635374e2009-03-09 01:21:12 -06001246 return 0;
1247}
1248
1249/**
Eric Moored5d135b2009-05-18 13:02:08 -06001250 * _transport_get_bay_identifier -
Eric Moore635374e2009-03-09 01:21:12 -06001251 * @phy: The sas phy object
1252 *
1253 * Returns the slot id for a device that resides inside an enclosure.
1254 */
1255static int
Eric Moored5d135b2009-05-18 13:02:08 -06001256_transport_get_bay_identifier(struct sas_rphy *rphy)
Eric Moore635374e2009-03-09 01:21:12 -06001257{
1258 struct MPT2SAS_ADAPTER *ioc = rphy_to_ioc(rphy);
1259 struct _sas_device *sas_device;
1260 unsigned long flags;
1261
1262 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1263 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1264 rphy->identify.sas_address);
1265 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1266
1267 if (!sas_device)
1268 return -ENXIO;
1269
1270 return sas_device->slot;
1271}
1272
1273/**
Eric Moored5d135b2009-05-18 13:02:08 -06001274 * _transport_phy_reset -
Eric Moore635374e2009-03-09 01:21:12 -06001275 * @phy: The sas phy object
1276 * @hard_reset:
1277 *
1278 * Only support sas_host direct attached phys.
1279 * Returns 0 for success, non-zero for failure.
1280 */
1281static int
Eric Moored5d135b2009-05-18 13:02:08 -06001282_transport_phy_reset(struct sas_phy *phy, int hard_reset)
Eric Moore635374e2009-03-09 01:21:12 -06001283{
1284 struct MPT2SAS_ADAPTER *ioc = phy_to_ioc(phy);
1285 struct _sas_phy *mpt2sas_phy;
1286 Mpi2SasIoUnitControlReply_t mpi_reply;
1287 Mpi2SasIoUnitControlRequest_t mpi_request;
Eric Moore635374e2009-03-09 01:21:12 -06001288
Kashyap, Desai50d5c602009-12-16 18:55:26 +05301289 mpt2sas_phy = _transport_find_local_phy(ioc, phy);
Eric Moore635374e2009-03-09 01:21:12 -06001290
1291 if (!mpt2sas_phy) /* this phy not on sas_host */
1292 return -EINVAL;
1293
1294 memset(&mpi_request, 0, sizeof(Mpi2SasIoUnitControlReply_t));
1295 mpi_request.Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
1296 mpi_request.Operation = hard_reset ?
1297 MPI2_SAS_OP_PHY_HARD_RESET : MPI2_SAS_OP_PHY_LINK_RESET;
1298 mpi_request.PhyNum = mpt2sas_phy->phy_id;
1299
1300 if ((mpt2sas_base_sas_iounit_control(ioc, &mpi_reply, &mpi_request))) {
1301 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1302 ioc->name, __FILE__, __LINE__, __func__);
1303 return -ENXIO;
1304 }
1305
1306 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo)
1307 printk(MPT2SAS_INFO_FMT "phy(%d), ioc_status"
1308 "(0x%04x), loginfo(0x%08x)\n", ioc->name,
1309 mpt2sas_phy->phy_id,
1310 le16_to_cpu(mpi_reply.IOCStatus),
1311 le32_to_cpu(mpi_reply.IOCLogInfo));
1312
1313 return 0;
1314}
1315
1316/**
Kashyap, Desai50d5c602009-12-16 18:55:26 +05301317 * _transport_phy_enable - enable/disable phys
1318 * @phy: The sas phy object
1319 * @enable: enable phy when true
1320 *
1321 * Only support sas_host direct attached phys.
1322 * Returns 0 for success, non-zero for failure.
1323 */
1324static int
1325_transport_phy_enable(struct sas_phy *phy, int enable)
1326{
1327 struct MPT2SAS_ADAPTER *ioc = phy_to_ioc(phy);
1328 struct _sas_phy *mpt2sas_phy;
1329 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
1330 Mpi2ConfigReply_t mpi_reply;
1331 u16 ioc_status;
1332 u16 sz;
1333 int rc = 0;
1334
1335 mpt2sas_phy = _transport_find_local_phy(ioc, phy);
1336
1337 if (!mpt2sas_phy) /* this phy not on sas_host */
1338 return -EINVAL;
1339
1340 /* sas_iounit page 1 */
1341 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
1342 sizeof(Mpi2SasIOUnit1PhyData_t));
1343 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
1344 if (!sas_iounit_pg1) {
1345 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1346 ioc->name, __FILE__, __LINE__, __func__);
1347 rc = -ENOMEM;
1348 goto out;
1349 }
1350 if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
1351 sas_iounit_pg1, sz))) {
1352 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1353 ioc->name, __FILE__, __LINE__, __func__);
1354 rc = -ENXIO;
1355 goto out;
1356 }
1357 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1358 MPI2_IOCSTATUS_MASK;
1359 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1360 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1361 ioc->name, __FILE__, __LINE__, __func__);
1362 rc = -EIO;
1363 goto out;
1364 }
1365
1366 if (enable)
1367 sas_iounit_pg1->PhyData[mpt2sas_phy->phy_id].PhyFlags
1368 &= ~MPI2_SASIOUNIT1_PHYFLAGS_PHY_DISABLE;
1369 else
1370 sas_iounit_pg1->PhyData[mpt2sas_phy->phy_id].PhyFlags
1371 |= MPI2_SASIOUNIT1_PHYFLAGS_PHY_DISABLE;
1372
1373 mpt2sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1, sz);
1374
1375 out:
1376 kfree(sas_iounit_pg1);
1377 return rc;
1378}
1379
1380/**
1381 * _transport_phy_speed - set phy min/max link rates
1382 * @phy: The sas phy object
1383 * @rates: rates defined in sas_phy_linkrates
1384 *
1385 * Only support sas_host direct attached phys.
1386 * Returns 0 for success, non-zero for failure.
1387 */
1388static int
1389_transport_phy_speed(struct sas_phy *phy, struct sas_phy_linkrates *rates)
1390{
1391 struct MPT2SAS_ADAPTER *ioc = phy_to_ioc(phy);
1392 struct _sas_phy *mpt2sas_phy;
1393 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
1394 Mpi2SasPhyPage0_t phy_pg0;
1395 Mpi2ConfigReply_t mpi_reply;
1396 u16 ioc_status;
1397 u16 sz;
1398 int i;
1399 int rc = 0;
1400
1401 mpt2sas_phy = _transport_find_local_phy(ioc, phy);
1402
1403 if (!mpt2sas_phy) /* this phy not on sas_host */
1404 return -EINVAL;
1405
1406 if (!rates->minimum_linkrate)
1407 rates->minimum_linkrate = phy->minimum_linkrate;
1408 else if (rates->minimum_linkrate < phy->minimum_linkrate_hw)
1409 rates->minimum_linkrate = phy->minimum_linkrate_hw;
1410
1411 if (!rates->maximum_linkrate)
1412 rates->maximum_linkrate = phy->maximum_linkrate;
1413 else if (rates->maximum_linkrate > phy->maximum_linkrate_hw)
1414 rates->maximum_linkrate = phy->maximum_linkrate_hw;
1415
1416 /* sas_iounit page 1 */
1417 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
1418 sizeof(Mpi2SasIOUnit1PhyData_t));
1419 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
1420 if (!sas_iounit_pg1) {
1421 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1422 ioc->name, __FILE__, __LINE__, __func__);
1423 rc = -ENOMEM;
1424 goto out;
1425 }
1426 if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
1427 sas_iounit_pg1, sz))) {
1428 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1429 ioc->name, __FILE__, __LINE__, __func__);
1430 rc = -ENXIO;
1431 goto out;
1432 }
1433 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1434 MPI2_IOCSTATUS_MASK;
1435 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1436 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1437 ioc->name, __FILE__, __LINE__, __func__);
1438 rc = -EIO;
1439 goto out;
1440 }
1441
1442 for (i = 0; i < ioc->sas_hba.num_phys; i++) {
1443 if (mpt2sas_phy->phy_id != i) {
1444 sas_iounit_pg1->PhyData[i].MaxMinLinkRate =
1445 (ioc->sas_hba.phy[i].phy->minimum_linkrate +
1446 (ioc->sas_hba.phy[i].phy->maximum_linkrate << 4));
1447 } else {
1448 sas_iounit_pg1->PhyData[i].MaxMinLinkRate =
1449 (rates->minimum_linkrate +
1450 (rates->maximum_linkrate << 4));
1451 }
1452 }
1453
1454 if (mpt2sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1,
1455 sz)) {
1456 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1457 ioc->name, __FILE__, __LINE__, __func__);
1458 rc = -ENXIO;
1459 goto out;
1460 }
1461
1462 /* link reset */
1463 _transport_phy_reset(phy, 0);
1464
1465 /* read phy page 0, then update the rates in the sas transport phy */
1466 if (!mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
1467 mpt2sas_phy->phy_id)) {
1468 phy->minimum_linkrate = _transport_convert_phy_link_rate(
1469 phy_pg0.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
1470 phy->maximum_linkrate = _transport_convert_phy_link_rate(
1471 phy_pg0.ProgrammedLinkRate >> 4);
1472 phy->negotiated_linkrate = _transport_convert_phy_link_rate(
1473 phy_pg0.NegotiatedLinkRate &
1474 MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
1475 }
1476
1477 out:
1478 kfree(sas_iounit_pg1);
1479 return rc;
1480}
1481
1482
1483/**
Eric Moored5d135b2009-05-18 13:02:08 -06001484 * _transport_smp_handler - transport portal for smp passthru
Eric Moore635374e2009-03-09 01:21:12 -06001485 * @shost: shost object
1486 * @rphy: sas transport rphy object
1487 * @req:
1488 *
1489 * This used primarily for smp_utils.
1490 * Example:
1491 * smp_rep_general /sys/class/bsg/expander-5:0
1492 */
1493static int
Eric Moored5d135b2009-05-18 13:02:08 -06001494_transport_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
Eric Moore635374e2009-03-09 01:21:12 -06001495 struct request *req)
1496{
1497 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1498 Mpi2SmpPassthroughRequest_t *mpi_request;
1499 Mpi2SmpPassthroughReply_t *mpi_reply;
1500 int rc;
1501 u16 smid;
1502 u32 ioc_state;
1503 unsigned long timeleft;
1504 void *psge;
1505 u32 sgl_flags;
1506 u8 issue_reset = 0;
Eric Moore635374e2009-03-09 01:21:12 -06001507 dma_addr_t dma_addr_in = 0;
1508 dma_addr_t dma_addr_out = 0;
1509 u16 wait_state_count;
1510 struct request *rsp = req->next_rq;
1511
1512 if (!rsp) {
1513 printk(MPT2SAS_ERR_FMT "%s: the smp response space is "
1514 "missing\n", ioc->name, __func__);
1515 return -EINVAL;
1516 }
1517
1518 /* do we need to support multiple segments? */
1519 if (req->bio->bi_vcnt > 1 || rsp->bio->bi_vcnt > 1) {
1520 printk(MPT2SAS_ERR_FMT "%s: multiple segments req %u %u, "
1521 "rsp %u %u\n", ioc->name, __func__, req->bio->bi_vcnt,
Tejun Heob0790412009-05-07 22:24:42 +09001522 blk_rq_bytes(req), rsp->bio->bi_vcnt, blk_rq_bytes(rsp));
Eric Moore635374e2009-03-09 01:21:12 -06001523 return -EINVAL;
1524 }
1525
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05301526 if (ioc->shost_recovery) {
Eric Moore635374e2009-03-09 01:21:12 -06001527 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
1528 __func__, ioc->name);
1529 return -EFAULT;
1530 }
Eric Moore635374e2009-03-09 01:21:12 -06001531
1532 rc = mutex_lock_interruptible(&ioc->transport_cmds.mutex);
1533 if (rc)
1534 return rc;
1535
1536 if (ioc->transport_cmds.status != MPT2_CMD_NOT_USED) {
1537 printk(MPT2SAS_ERR_FMT "%s: transport_cmds in use\n", ioc->name,
1538 __func__);
1539 rc = -EAGAIN;
1540 goto out;
1541 }
1542 ioc->transport_cmds.status = MPT2_CMD_PENDING;
1543
1544 wait_state_count = 0;
1545 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
1546 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1547 if (wait_state_count++ == 10) {
1548 printk(MPT2SAS_ERR_FMT
1549 "%s: failed due to ioc not operational\n",
1550 ioc->name, __func__);
1551 rc = -EFAULT;
1552 goto out;
1553 }
1554 ssleep(1);
1555 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
1556 printk(MPT2SAS_INFO_FMT "%s: waiting for "
1557 "operational state(count=%d)\n", ioc->name,
1558 __func__, wait_state_count);
1559 }
1560 if (wait_state_count)
1561 printk(MPT2SAS_INFO_FMT "%s: ioc is operational\n",
1562 ioc->name, __func__);
1563
1564 smid = mpt2sas_base_get_smid(ioc, ioc->transport_cb_idx);
1565 if (!smid) {
1566 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1567 ioc->name, __func__);
1568 rc = -EAGAIN;
1569 goto out;
1570 }
1571
1572 rc = 0;
1573 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1574 ioc->transport_cmds.smid = smid;
1575
1576 memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
1577 mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
1578 mpi_request->PhysicalPort = 0xFF;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301579 mpi_request->VF_ID = 0; /* TODO */
1580 mpi_request->VP_ID = 0;
Eric Moore635374e2009-03-09 01:21:12 -06001581 *((u64 *)&mpi_request->SASAddress) = (rphy) ?
1582 cpu_to_le64(rphy->identify.sas_address) :
1583 cpu_to_le64(ioc->sas_hba.sas_address);
Tejun Heob0790412009-05-07 22:24:42 +09001584 mpi_request->RequestDataLength = cpu_to_le16(blk_rq_bytes(req) - 4);
Eric Moore635374e2009-03-09 01:21:12 -06001585 psge = &mpi_request->SGL;
1586
1587 /* WRITE sgel first */
1588 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1589 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
1590 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1591 dma_addr_out = pci_map_single(ioc->pdev, bio_data(req->bio),
Tejun Heob0790412009-05-07 22:24:42 +09001592 blk_rq_bytes(req), PCI_DMA_BIDIRECTIONAL);
Eric Moore635374e2009-03-09 01:21:12 -06001593 if (!dma_addr_out) {
Kashyap, Desai463217b2009-10-05 15:53:06 +05301594 mpt2sas_base_free_smid(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06001595 goto unmap;
1596 }
1597
Tejun Heob0790412009-05-07 22:24:42 +09001598 ioc->base_add_sg_single(psge, sgl_flags | (blk_rq_bytes(req) - 4),
Eric Moore635374e2009-03-09 01:21:12 -06001599 dma_addr_out);
1600
1601 /* incr sgel */
1602 psge += ioc->sge_size;
1603
1604 /* READ sgel last */
1605 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1606 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
1607 MPI2_SGE_FLAGS_END_OF_LIST);
1608 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
Tejun Heob0790412009-05-07 22:24:42 +09001609 dma_addr_in = pci_map_single(ioc->pdev, bio_data(rsp->bio),
1610 blk_rq_bytes(rsp), PCI_DMA_BIDIRECTIONAL);
Eric Moore635374e2009-03-09 01:21:12 -06001611 if (!dma_addr_in) {
Kashyap, Desai463217b2009-10-05 15:53:06 +05301612 mpt2sas_base_free_smid(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06001613 goto unmap;
1614 }
1615
Tejun Heob0790412009-05-07 22:24:42 +09001616 ioc->base_add_sg_single(psge, sgl_flags | (blk_rq_bytes(rsp) + 4),
Eric Moore635374e2009-03-09 01:21:12 -06001617 dma_addr_in);
1618
1619 dtransportprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s - "
1620 "sending smp request\n", ioc->name, __func__));
1621
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301622 mpt2sas_base_put_smid_default(ioc, smid);
Kashyap, Desaibcfb6e62009-09-14 11:05:24 +05301623 init_completion(&ioc->transport_cmds.done);
Eric Moore635374e2009-03-09 01:21:12 -06001624 timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done,
1625 10*HZ);
1626
1627 if (!(ioc->transport_cmds.status & MPT2_CMD_COMPLETE)) {
1628 printk(MPT2SAS_ERR_FMT "%s : timeout\n",
1629 __func__, ioc->name);
1630 _debug_dump_mf(mpi_request,
1631 sizeof(Mpi2SmpPassthroughRequest_t)/4);
1632 if (!(ioc->transport_cmds.status & MPT2_CMD_RESET))
1633 issue_reset = 1;
1634 goto issue_host_reset;
1635 }
1636
1637 dtransportprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s - "
1638 "complete\n", ioc->name, __func__));
1639
1640 if (ioc->transport_cmds.status & MPT2_CMD_REPLY_VALID) {
1641
1642 mpi_reply = ioc->transport_cmds.reply;
1643
1644 dtransportprintk(ioc, printk(MPT2SAS_DEBUG_FMT
1645 "%s - reply data transfer size(%d)\n",
1646 ioc->name, __func__,
1647 le16_to_cpu(mpi_reply->ResponseDataLength)));
1648
1649 memcpy(req->sense, mpi_reply, sizeof(*mpi_reply));
1650 req->sense_len = sizeof(*mpi_reply);
Tejun Heo5f49f632009-05-19 18:33:05 +09001651 req->resid_len = 0;
Kashyap, Desaie94f6742010-03-17 16:24:52 +05301652 rsp->resid_len -=
1653 le16_to_cpu(mpi_reply->ResponseDataLength);
Eric Moore635374e2009-03-09 01:21:12 -06001654 } else {
1655 dtransportprintk(ioc, printk(MPT2SAS_DEBUG_FMT
1656 "%s - no reply\n", ioc->name, __func__));
1657 rc = -ENXIO;
1658 }
1659
1660 issue_host_reset:
1661 if (issue_reset) {
1662 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1663 FORCE_BIG_HAMMER);
1664 rc = -ETIMEDOUT;
1665 }
1666
1667 unmap:
1668 if (dma_addr_out)
Tejun Heob0790412009-05-07 22:24:42 +09001669 pci_unmap_single(ioc->pdev, dma_addr_out, blk_rq_bytes(req),
Eric Moore635374e2009-03-09 01:21:12 -06001670 PCI_DMA_BIDIRECTIONAL);
1671 if (dma_addr_in)
Tejun Heob0790412009-05-07 22:24:42 +09001672 pci_unmap_single(ioc->pdev, dma_addr_in, blk_rq_bytes(rsp),
Eric Moore635374e2009-03-09 01:21:12 -06001673 PCI_DMA_BIDIRECTIONAL);
1674
1675 out:
1676 ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
1677 mutex_unlock(&ioc->transport_cmds.mutex);
1678 return rc;
1679}
1680
1681struct sas_function_template mpt2sas_transport_functions = {
Eric Moored5d135b2009-05-18 13:02:08 -06001682 .get_linkerrors = _transport_get_linkerrors,
1683 .get_enclosure_identifier = _transport_get_enclosure_identifier,
1684 .get_bay_identifier = _transport_get_bay_identifier,
1685 .phy_reset = _transport_phy_reset,
Kashyap, Desai50d5c602009-12-16 18:55:26 +05301686 .phy_enable = _transport_phy_enable,
1687 .set_phy_speed = _transport_phy_speed,
Eric Moored5d135b2009-05-18 13:02:08 -06001688 .smp_handler = _transport_smp_handler,
Eric Moore635374e2009-03-09 01:21:12 -06001689};
1690
1691struct scsi_transport_template *mpt2sas_transport_template;