1 // SPDX-License-Identifier: GPL-2.0
3 * driver for Microchip PQI-based storage controllers
4 * Copyright (c) 2019-2021 Microchip Technology Inc. and its subsidiaries
5 * Copyright (c) 2016-2018 Microsemi Corporation
6 * Copyright (c) 2016 PMC-Sierra, Inc.
8 * Questions/Comments/Bugfixes to storagedev@microchip.com
12 #include <linux/kernel.h>
13 #include <linux/bsg-lib.h>
14 #include <scsi/scsi_host.h>
15 #include <scsi/scsi_cmnd.h>
16 #include <scsi/scsi_transport_sas.h>
17 #include <asm/unaligned.h>
20 static struct pqi_sas_phy *pqi_alloc_sas_phy(struct pqi_sas_port *pqi_sas_port)
22 struct pqi_sas_phy *pqi_sas_phy;
25 pqi_sas_phy = kzalloc(sizeof(*pqi_sas_phy), GFP_KERNEL);
29 phy = sas_phy_alloc(pqi_sas_port->parent_node->parent_dev,
30 pqi_sas_port->next_phy_index);
36 pqi_sas_port->next_phy_index++;
37 pqi_sas_phy->phy = phy;
38 pqi_sas_phy->parent_port = pqi_sas_port;
43 static void pqi_free_sas_phy(struct pqi_sas_phy *pqi_sas_phy)
45 struct sas_phy *phy = pqi_sas_phy->phy;
47 sas_port_delete_phy(pqi_sas_phy->parent_port->port, phy);
48 if (pqi_sas_phy->added_to_port)
49 list_del(&pqi_sas_phy->phy_list_entry);
54 static int pqi_sas_port_add_phy(struct pqi_sas_phy *pqi_sas_phy)
57 struct pqi_sas_port *pqi_sas_port;
59 struct sas_identify *identify;
61 pqi_sas_port = pqi_sas_phy->parent_port;
62 phy = pqi_sas_phy->phy;
64 identify = &phy->identify;
65 memset(identify, 0, sizeof(*identify));
66 identify->sas_address = pqi_sas_port->sas_address;
67 identify->device_type = SAS_END_DEVICE;
68 identify->initiator_port_protocols = SAS_PROTOCOL_ALL;
69 identify->target_port_protocols = SAS_PROTOCOL_ALL;
70 phy->minimum_linkrate_hw = SAS_LINK_RATE_UNKNOWN;
71 phy->maximum_linkrate_hw = SAS_LINK_RATE_UNKNOWN;
72 phy->minimum_linkrate = SAS_LINK_RATE_UNKNOWN;
73 phy->maximum_linkrate = SAS_LINK_RATE_UNKNOWN;
74 phy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
76 rc = sas_phy_add(pqi_sas_phy->phy);
80 sas_port_add_phy(pqi_sas_port->port, pqi_sas_phy->phy);
81 list_add_tail(&pqi_sas_phy->phy_list_entry,
82 &pqi_sas_port->phy_list_head);
83 pqi_sas_phy->added_to_port = true;
88 static int pqi_sas_port_add_rphy(struct pqi_sas_port *pqi_sas_port,
89 struct sas_rphy *rphy)
91 struct sas_identify *identify;
93 identify = &rphy->identify;
94 identify->sas_address = pqi_sas_port->sas_address;
96 identify->initiator_port_protocols = SAS_PROTOCOL_ALL;
97 identify->target_port_protocols = SAS_PROTOCOL_STP;
99 if (pqi_sas_port->device) {
100 identify->phy_identifier = pqi_sas_port->device->phy_id;
101 switch (pqi_sas_port->device->device_type) {
102 case SA_DEVICE_TYPE_SAS:
103 case SA_DEVICE_TYPE_SES:
104 case SA_DEVICE_TYPE_NVME:
105 identify->target_port_protocols = SAS_PROTOCOL_SSP;
107 case SA_DEVICE_TYPE_EXPANDER_SMP:
108 identify->target_port_protocols = SAS_PROTOCOL_SMP;
110 case SA_DEVICE_TYPE_SATA:
116 return sas_rphy_add(rphy);
119 static struct sas_rphy *pqi_sas_rphy_alloc(struct pqi_sas_port *pqi_sas_port)
121 if (pqi_sas_port->device && pqi_sas_port->device->is_expander_smp_device)
122 return sas_expander_alloc(pqi_sas_port->port,
123 SAS_FANOUT_EXPANDER_DEVICE);
125 return sas_end_device_alloc(pqi_sas_port->port);
128 static struct pqi_sas_port *pqi_alloc_sas_port(
129 struct pqi_sas_node *pqi_sas_node, u64 sas_address,
130 struct pqi_scsi_dev *device)
133 struct pqi_sas_port *pqi_sas_port;
134 struct sas_port *port;
136 pqi_sas_port = kzalloc(sizeof(*pqi_sas_port), GFP_KERNEL);
140 INIT_LIST_HEAD(&pqi_sas_port->phy_list_head);
141 pqi_sas_port->parent_node = pqi_sas_node;
143 port = sas_port_alloc_num(pqi_sas_node->parent_dev);
147 rc = sas_port_add(port);
151 pqi_sas_port->port = port;
152 pqi_sas_port->sas_address = sas_address;
153 pqi_sas_port->device = device;
154 list_add_tail(&pqi_sas_port->port_list_entry,
155 &pqi_sas_node->port_list_head);
167 static void pqi_free_sas_port(struct pqi_sas_port *pqi_sas_port)
169 struct pqi_sas_phy *pqi_sas_phy;
170 struct pqi_sas_phy *next;
172 list_for_each_entry_safe(pqi_sas_phy, next,
173 &pqi_sas_port->phy_list_head, phy_list_entry)
174 pqi_free_sas_phy(pqi_sas_phy);
176 sas_port_delete(pqi_sas_port->port);
177 list_del(&pqi_sas_port->port_list_entry);
181 static struct pqi_sas_node *pqi_alloc_sas_node(struct device *parent_dev)
183 struct pqi_sas_node *pqi_sas_node;
185 pqi_sas_node = kzalloc(sizeof(*pqi_sas_node), GFP_KERNEL);
187 pqi_sas_node->parent_dev = parent_dev;
188 INIT_LIST_HEAD(&pqi_sas_node->port_list_head);
194 static void pqi_free_sas_node(struct pqi_sas_node *pqi_sas_node)
196 struct pqi_sas_port *pqi_sas_port;
197 struct pqi_sas_port *next;
202 list_for_each_entry_safe(pqi_sas_port, next,
203 &pqi_sas_node->port_list_head, port_list_entry)
204 pqi_free_sas_port(pqi_sas_port);
209 struct pqi_scsi_dev *pqi_find_device_by_sas_rphy(
210 struct pqi_ctrl_info *ctrl_info, struct sas_rphy *rphy)
212 struct pqi_scsi_dev *device;
214 list_for_each_entry(device, &ctrl_info->scsi_device_list,
215 scsi_device_list_entry) {
216 if (!device->sas_port)
218 if (device->sas_port->rphy == rphy)
225 int pqi_add_sas_host(struct Scsi_Host *shost, struct pqi_ctrl_info *ctrl_info)
228 struct device *parent_dev;
229 struct pqi_sas_node *pqi_sas_node;
230 struct pqi_sas_port *pqi_sas_port;
231 struct pqi_sas_phy *pqi_sas_phy;
233 parent_dev = &shost->shost_dev;
235 pqi_sas_node = pqi_alloc_sas_node(parent_dev);
239 pqi_sas_port = pqi_alloc_sas_port(pqi_sas_node,
240 ctrl_info->sas_address, NULL);
246 pqi_sas_phy = pqi_alloc_sas_phy(pqi_sas_port);
252 rc = pqi_sas_port_add_phy(pqi_sas_phy);
256 ctrl_info->sas_host = pqi_sas_node;
261 pqi_free_sas_phy(pqi_sas_phy);
263 pqi_free_sas_port(pqi_sas_port);
265 pqi_free_sas_node(pqi_sas_node);
270 void pqi_delete_sas_host(struct pqi_ctrl_info *ctrl_info)
272 pqi_free_sas_node(ctrl_info->sas_host);
275 int pqi_add_sas_device(struct pqi_sas_node *pqi_sas_node,
276 struct pqi_scsi_dev *device)
279 struct pqi_sas_port *pqi_sas_port;
280 struct sas_rphy *rphy;
282 pqi_sas_port = pqi_alloc_sas_port(pqi_sas_node,
283 device->sas_address, device);
287 rphy = pqi_sas_rphy_alloc(pqi_sas_port);
293 pqi_sas_port->rphy = rphy;
294 device->sas_port = pqi_sas_port;
296 rc = pqi_sas_port_add_rphy(pqi_sas_port, rphy);
303 pqi_free_sas_port(pqi_sas_port);
304 device->sas_port = NULL;
309 void pqi_remove_sas_device(struct pqi_scsi_dev *device)
311 if (device->sas_port) {
312 pqi_free_sas_port(device->sas_port);
313 device->sas_port = NULL;
317 static int pqi_sas_get_linkerrors(struct sas_phy *phy)
322 static int pqi_sas_get_enclosure_identifier(struct sas_rphy *rphy,
327 struct Scsi_Host *shost;
328 struct pqi_ctrl_info *ctrl_info;
329 struct pqi_scsi_dev *found_device;
330 struct pqi_scsi_dev *device;
335 shost = rphy_to_shost(rphy);
336 ctrl_info = shost_to_hba(shost);
337 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
338 found_device = pqi_find_device_by_sas_rphy(ctrl_info, rphy);
345 if (found_device->devtype == TYPE_ENCLOSURE) {
346 *identifier = get_unaligned_be64(&found_device->wwid);
351 if (found_device->box_index == 0xff ||
352 found_device->phys_box_on_bus == 0 ||
353 found_device->bay == 0xff) {
358 list_for_each_entry(device, &ctrl_info->scsi_device_list,
359 scsi_device_list_entry) {
360 if (device->devtype == TYPE_ENCLOSURE &&
361 device->box_index == found_device->box_index &&
362 device->phys_box_on_bus ==
363 found_device->phys_box_on_bus &&
364 memcmp(device->phys_connector,
365 found_device->phys_connector, 2) == 0) {
367 get_unaligned_be64(&device->wwid);
373 if (found_device->phy_connected_dev_type != SA_DEVICE_TYPE_CONTROLLER) {
378 list_for_each_entry(device, &ctrl_info->scsi_device_list,
379 scsi_device_list_entry) {
380 if (device->devtype == TYPE_ENCLOSURE &&
381 CISS_GET_DRIVE_NUMBER(device->scsi3addr) ==
383 *identifier = get_unaligned_be64(&device->wwid);
391 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
396 static int pqi_sas_get_bay_identifier(struct sas_rphy *rphy)
400 struct pqi_ctrl_info *ctrl_info;
401 struct pqi_scsi_dev *device;
402 struct Scsi_Host *shost;
407 shost = rphy_to_shost(rphy);
408 ctrl_info = shost_to_hba(shost);
409 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
410 device = pqi_find_device_by_sas_rphy(ctrl_info, rphy);
417 if (device->bay == 0xff)
423 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
428 static int pqi_sas_phy_reset(struct sas_phy *phy, int hard_reset)
433 static int pqi_sas_phy_enable(struct sas_phy *phy, int enable)
438 static int pqi_sas_phy_setup(struct sas_phy *phy)
443 static void pqi_sas_phy_release(struct sas_phy *phy)
447 static int pqi_sas_phy_speed(struct sas_phy *phy,
448 struct sas_phy_linkrates *rates)
453 #define CSMI_IOCTL_TIMEOUT 60
454 #define SMP_CRC_FIELD_LENGTH 4
456 static struct bmic_csmi_smp_passthru_buffer *
457 pqi_build_csmi_smp_passthru_buffer(struct sas_rphy *rphy,
460 struct bmic_csmi_smp_passthru_buffer *smp_buf;
461 struct bmic_csmi_ioctl_header *ioctl_header;
462 struct bmic_csmi_smp_passthru *parameters;
466 smp_buf = kzalloc(sizeof(*smp_buf), GFP_KERNEL);
470 req_size = job->request_payload.payload_len;
471 resp_size = job->reply_payload.payload_len;
473 ioctl_header = &smp_buf->ioctl_header;
474 put_unaligned_le32(sizeof(smp_buf->ioctl_header),
475 &ioctl_header->header_length);
476 put_unaligned_le32(CSMI_IOCTL_TIMEOUT, &ioctl_header->timeout);
477 put_unaligned_le32(CSMI_CC_SAS_SMP_PASSTHRU,
478 &ioctl_header->control_code);
479 put_unaligned_le32(sizeof(smp_buf->parameters), &ioctl_header->length);
481 parameters = &smp_buf->parameters;
482 parameters->phy_identifier = rphy->identify.phy_identifier;
483 parameters->port_identifier = 0;
484 parameters->connection_rate = 0;
485 put_unaligned_be64(rphy->identify.sas_address,
486 ¶meters->destination_sas_address);
488 if (req_size > SMP_CRC_FIELD_LENGTH)
489 req_size -= SMP_CRC_FIELD_LENGTH;
491 put_unaligned_le32(req_size, ¶meters->request_length);
492 put_unaligned_le32(resp_size, ¶meters->response_length);
494 sg_copy_to_buffer(job->request_payload.sg_list,
495 job->reply_payload.sg_cnt, ¶meters->request,
501 static unsigned int pqi_build_sas_smp_handler_reply(
502 struct bmic_csmi_smp_passthru_buffer *smp_buf, struct bsg_job *job,
503 struct pqi_raid_error_info *error_info)
505 sg_copy_from_buffer(job->reply_payload.sg_list,
506 job->reply_payload.sg_cnt, &smp_buf->parameters.response,
507 le32_to_cpu(smp_buf->parameters.response_length));
509 job->reply_len = le16_to_cpu(error_info->sense_data_length);
510 memcpy(job->reply, error_info->data,
511 le16_to_cpu(error_info->sense_data_length));
513 return job->reply_payload.payload_len -
514 get_unaligned_le32(&error_info->data_in_transferred);
517 void pqi_sas_smp_handler(struct bsg_job *job, struct Scsi_Host *shost,
518 struct sas_rphy *rphy)
521 struct pqi_ctrl_info *ctrl_info;
522 struct bmic_csmi_smp_passthru_buffer *smp_buf;
523 struct pqi_raid_error_info error_info;
524 unsigned int reslen = 0;
526 ctrl_info = shost_to_hba(shost);
528 if (job->reply_payload.payload_len == 0) {
538 if (rphy->identify.device_type != SAS_FANOUT_EXPANDER_DEVICE) {
543 if (job->request_payload.sg_cnt > 1 || job->reply_payload.sg_cnt > 1) {
548 smp_buf = pqi_build_csmi_smp_passthru_buffer(rphy, job);
554 rc = pqi_csmi_smp_passthru(ctrl_info, smp_buf, sizeof(*smp_buf),
559 reslen = pqi_build_sas_smp_handler_reply(smp_buf, job, &error_info);
562 bsg_job_done(job, rc, reslen);
564 struct sas_function_template pqi_sas_transport_functions = {
565 .get_linkerrors = pqi_sas_get_linkerrors,
566 .get_enclosure_identifier = pqi_sas_get_enclosure_identifier,
567 .get_bay_identifier = pqi_sas_get_bay_identifier,
568 .phy_reset = pqi_sas_phy_reset,
569 .phy_enable = pqi_sas_phy_enable,
570 .phy_setup = pqi_sas_phy_setup,
571 .phy_release = pqi_sas_phy_release,
572 .set_phy_speed = pqi_sas_phy_speed,
573 .smp_handler = pqi_sas_smp_handler,