scsi: smartpqi: Align code with oob driver
[linux-2.6-block.git] / drivers / scsi / smartpqi / smartpqi_sas_transport.c
CommitLineData
2cc37b15 1// SPDX-License-Identifier: GPL-2.0
6c223761
KB
2/*
3 * driver for Microsemi PQI-based storage controllers
2a712681 4 * Copyright (c) 2019-2020 Microchip Technology Inc. and its subsidiaries
2f4c4b92 5 * Copyright (c) 2016-2018 Microsemi Corporation
6c223761
KB
6 * Copyright (c) 2016 PMC-Sierra, Inc.
7 *
2f4c4b92 8 * Questions/Comments/Bugfixes to storagedev@microchip.com
6c223761
KB
9 *
10 */
11
12#include <linux/kernel.h>
3d46a59a 13#include <linux/bsg-lib.h>
6c223761
KB
14#include <scsi/scsi_host.h>
15#include <scsi/scsi_cmnd.h>
16#include <scsi/scsi_transport_sas.h>
3d46a59a 17#include <asm/unaligned.h>
6c223761
KB
18#include "smartpqi.h"
19
20static struct pqi_sas_phy *pqi_alloc_sas_phy(struct pqi_sas_port *pqi_sas_port)
21{
22 struct pqi_sas_phy *pqi_sas_phy;
23 struct sas_phy *phy;
24
25 pqi_sas_phy = kzalloc(sizeof(*pqi_sas_phy), GFP_KERNEL);
26 if (!pqi_sas_phy)
27 return NULL;
28
29 phy = sas_phy_alloc(pqi_sas_port->parent_node->parent_dev,
30 pqi_sas_port->next_phy_index);
31 if (!phy) {
32 kfree(pqi_sas_phy);
33 return NULL;
34 }
35
36 pqi_sas_port->next_phy_index++;
37 pqi_sas_phy->phy = phy;
38 pqi_sas_phy->parent_port = pqi_sas_port;
39
40 return pqi_sas_phy;
41}
42
43static void pqi_free_sas_phy(struct pqi_sas_phy *pqi_sas_phy)
44{
45 struct sas_phy *phy = pqi_sas_phy->phy;
46
47 sas_port_delete_phy(pqi_sas_phy->parent_port->port, phy);
6c223761
KB
48 if (pqi_sas_phy->added_to_port)
49 list_del(&pqi_sas_phy->phy_list_entry);
b9692611 50 sas_phy_delete(phy);
6c223761
KB
51 kfree(pqi_sas_phy);
52}
53
54static int pqi_sas_port_add_phy(struct pqi_sas_phy *pqi_sas_phy)
55{
56 int rc;
57 struct pqi_sas_port *pqi_sas_port;
58 struct sas_phy *phy;
59 struct sas_identify *identify;
60
61 pqi_sas_port = pqi_sas_phy->parent_port;
62 phy = pqi_sas_phy->phy;
63
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_STP;
69 identify->target_port_protocols = SAS_PROTOCOL_STP;
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;
75
76 rc = sas_phy_add(pqi_sas_phy->phy);
77 if (rc)
78 return rc;
79
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;
84
85 return 0;
86}
87
88static int pqi_sas_port_add_rphy(struct pqi_sas_port *pqi_sas_port,
89 struct sas_rphy *rphy)
90{
91 struct sas_identify *identify;
92
93 identify = &rphy->identify;
94 identify->sas_address = pqi_sas_port->sas_address;
3d46a59a
DB
95
96 if (pqi_sas_port->device &&
97 pqi_sas_port->device->is_expander_smp_device) {
98 identify->initiator_port_protocols = SAS_PROTOCOL_SMP;
99 identify->target_port_protocols = SAS_PROTOCOL_SMP;
100 } else {
101 identify->initiator_port_protocols = SAS_PROTOCOL_STP;
102 identify->target_port_protocols = SAS_PROTOCOL_STP;
103 }
6c223761
KB
104
105 return sas_rphy_add(rphy);
106}
107
3d46a59a
DB
108static struct sas_rphy *pqi_sas_rphy_alloc(struct pqi_sas_port *pqi_sas_port)
109{
583891c9 110 if (pqi_sas_port->device && pqi_sas_port->device->is_expander_smp_device)
3d46a59a
DB
111 return sas_expander_alloc(pqi_sas_port->port,
112 SAS_FANOUT_EXPANDER_DEVICE);
113
114 return sas_end_device_alloc(pqi_sas_port->port);
115}
116
6c223761 117static struct pqi_sas_port *pqi_alloc_sas_port(
3d46a59a
DB
118 struct pqi_sas_node *pqi_sas_node, u64 sas_address,
119 struct pqi_scsi_dev *device)
6c223761
KB
120{
121 int rc;
122 struct pqi_sas_port *pqi_sas_port;
123 struct sas_port *port;
124
125 pqi_sas_port = kzalloc(sizeof(*pqi_sas_port), GFP_KERNEL);
126 if (!pqi_sas_port)
127 return NULL;
128
129 INIT_LIST_HEAD(&pqi_sas_port->phy_list_head);
130 pqi_sas_port->parent_node = pqi_sas_node;
131
132 port = sas_port_alloc_num(pqi_sas_node->parent_dev);
133 if (!port)
134 goto free_pqi_port;
135
136 rc = sas_port_add(port);
137 if (rc)
138 goto free_sas_port;
139
140 pqi_sas_port->port = port;
141 pqi_sas_port->sas_address = sas_address;
3d46a59a 142 pqi_sas_port->device = device;
6c223761
KB
143 list_add_tail(&pqi_sas_port->port_list_entry,
144 &pqi_sas_node->port_list_head);
145
146 return pqi_sas_port;
147
148free_sas_port:
149 sas_port_free(port);
150free_pqi_port:
151 kfree(pqi_sas_port);
152
153 return NULL;
154}
155
156static void pqi_free_sas_port(struct pqi_sas_port *pqi_sas_port)
157{
158 struct pqi_sas_phy *pqi_sas_phy;
159 struct pqi_sas_phy *next;
160
161 list_for_each_entry_safe(pqi_sas_phy, next,
3d46a59a 162 &pqi_sas_port->phy_list_head, phy_list_entry)
583891c9 163 pqi_free_sas_phy(pqi_sas_phy);
6c223761
KB
164
165 sas_port_delete(pqi_sas_port->port);
166 list_del(&pqi_sas_port->port_list_entry);
167 kfree(pqi_sas_port);
168}
169
170static struct pqi_sas_node *pqi_alloc_sas_node(struct device *parent_dev)
171{
172 struct pqi_sas_node *pqi_sas_node;
173
174 pqi_sas_node = kzalloc(sizeof(*pqi_sas_node), GFP_KERNEL);
175 if (pqi_sas_node) {
176 pqi_sas_node->parent_dev = parent_dev;
177 INIT_LIST_HEAD(&pqi_sas_node->port_list_head);
178 }
179
180 return pqi_sas_node;
181}
182
183static void pqi_free_sas_node(struct pqi_sas_node *pqi_sas_node)
184{
185 struct pqi_sas_port *pqi_sas_port;
186 struct pqi_sas_port *next;
187
188 if (!pqi_sas_node)
189 return;
190
191 list_for_each_entry_safe(pqi_sas_port, next,
3d46a59a 192 &pqi_sas_node->port_list_head, port_list_entry)
583891c9 193 pqi_free_sas_port(pqi_sas_port);
6c223761
KB
194
195 kfree(pqi_sas_node);
196}
197
198struct pqi_scsi_dev *pqi_find_device_by_sas_rphy(
199 struct pqi_ctrl_info *ctrl_info, struct sas_rphy *rphy)
200{
201 struct pqi_scsi_dev *device;
202
203 list_for_each_entry(device, &ctrl_info->scsi_device_list,
204 scsi_device_list_entry) {
205 if (!device->sas_port)
206 continue;
207 if (device->sas_port->rphy == rphy)
208 return device;
209 }
210
211 return NULL;
212}
213
214int pqi_add_sas_host(struct Scsi_Host *shost, struct pqi_ctrl_info *ctrl_info)
215{
216 int rc;
217 struct device *parent_dev;
218 struct pqi_sas_node *pqi_sas_node;
219 struct pqi_sas_port *pqi_sas_port;
220 struct pqi_sas_phy *pqi_sas_phy;
221
3d46a59a 222 parent_dev = &shost->shost_dev;
6c223761
KB
223
224 pqi_sas_node = pqi_alloc_sas_node(parent_dev);
225 if (!pqi_sas_node)
226 return -ENOMEM;
227
3d46a59a
DB
228 pqi_sas_port = pqi_alloc_sas_port(pqi_sas_node,
229 ctrl_info->sas_address, NULL);
6c223761
KB
230 if (!pqi_sas_port) {
231 rc = -ENODEV;
232 goto free_sas_node;
233 }
234
235 pqi_sas_phy = pqi_alloc_sas_phy(pqi_sas_port);
236 if (!pqi_sas_phy) {
237 rc = -ENODEV;
238 goto free_sas_port;
239 }
240
241 rc = pqi_sas_port_add_phy(pqi_sas_phy);
242 if (rc)
243 goto free_sas_phy;
244
245 ctrl_info->sas_host = pqi_sas_node;
246
247 return 0;
248
249free_sas_phy:
250 pqi_free_sas_phy(pqi_sas_phy);
251free_sas_port:
252 pqi_free_sas_port(pqi_sas_port);
253free_sas_node:
254 pqi_free_sas_node(pqi_sas_node);
255
256 return rc;
257}
258
259void pqi_delete_sas_host(struct pqi_ctrl_info *ctrl_info)
260{
261 pqi_free_sas_node(ctrl_info->sas_host);
262}
263
264int pqi_add_sas_device(struct pqi_sas_node *pqi_sas_node,
265 struct pqi_scsi_dev *device)
266{
267 int rc;
268 struct pqi_sas_port *pqi_sas_port;
269 struct sas_rphy *rphy;
270
3d46a59a
DB
271 pqi_sas_port = pqi_alloc_sas_port(pqi_sas_node,
272 device->sas_address, device);
6c223761
KB
273 if (!pqi_sas_port)
274 return -ENOMEM;
275
3d46a59a 276 rphy = pqi_sas_rphy_alloc(pqi_sas_port);
6c223761
KB
277 if (!rphy) {
278 rc = -ENODEV;
279 goto free_sas_port;
280 }
281
282 pqi_sas_port->rphy = rphy;
283 device->sas_port = pqi_sas_port;
284
285 rc = pqi_sas_port_add_rphy(pqi_sas_port, rphy);
286 if (rc)
287 goto free_sas_port;
288
289 return 0;
290
291free_sas_port:
292 pqi_free_sas_port(pqi_sas_port);
293 device->sas_port = NULL;
294
295 return rc;
296}
297
298void pqi_remove_sas_device(struct pqi_scsi_dev *device)
299{
300 if (device->sas_port) {
301 pqi_free_sas_port(device->sas_port);
302 device->sas_port = NULL;
303 }
304}
305
306static int pqi_sas_get_linkerrors(struct sas_phy *phy)
307{
308 return 0;
309}
310
311static int pqi_sas_get_enclosure_identifier(struct sas_rphy *rphy,
312 u64 *identifier)
313{
2d2ad4bc
GW
314 int rc;
315 unsigned long flags;
316 struct Scsi_Host *shost;
317 struct pqi_ctrl_info *ctrl_info;
318 struct pqi_scsi_dev *found_device;
319 struct pqi_scsi_dev *device;
320
321 if (!rphy)
322 return -ENODEV;
323
324 shost = rphy_to_shost(rphy);
325 ctrl_info = shost_to_hba(shost);
326 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
327 found_device = pqi_find_device_by_sas_rphy(ctrl_info, rphy);
328
329 if (!found_device) {
330 rc = -ENODEV;
331 goto out;
332 }
333
334 if (found_device->devtype == TYPE_ENCLOSURE) {
335 *identifier = get_unaligned_be64(&found_device->wwid);
336 rc = 0;
337 goto out;
338 }
339
340 if (found_device->box_index == 0xff ||
341 found_device->phys_box_on_bus == 0 ||
342 found_device->bay == 0xff) {
343 rc = -EINVAL;
344 goto out;
345 }
346
347 list_for_each_entry(device, &ctrl_info->scsi_device_list,
348 scsi_device_list_entry) {
349 if (device->devtype == TYPE_ENCLOSURE &&
350 device->box_index == found_device->box_index &&
351 device->phys_box_on_bus ==
352 found_device->phys_box_on_bus &&
353 memcmp(device->phys_connector,
354 found_device->phys_connector, 2) == 0) {
355 *identifier =
356 get_unaligned_be64(&device->wwid);
357 rc = 0;
358 goto out;
359 }
360 }
361
694c5d5b 362 if (found_device->phy_connected_dev_type != SA_DEVICE_TYPE_CONTROLLER) {
2d2ad4bc
GW
363 rc = -EINVAL;
364 goto out;
365 }
366
367 list_for_each_entry(device, &ctrl_info->scsi_device_list,
368 scsi_device_list_entry) {
369 if (device->devtype == TYPE_ENCLOSURE &&
370 CISS_GET_DRIVE_NUMBER(device->scsi3addr) ==
371 PQI_VSEP_CISS_BTL) {
372 *identifier = get_unaligned_be64(&device->wwid);
373 rc = 0;
374 goto out;
375 }
376 }
377
378 rc = -EINVAL;
379out:
380 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
381
382 return rc;
6c223761
KB
383}
384
385static int pqi_sas_get_bay_identifier(struct sas_rphy *rphy)
386{
2d2ad4bc
GW
387 int rc;
388 unsigned long flags;
389 struct pqi_ctrl_info *ctrl_info;
390 struct pqi_scsi_dev *device;
391 struct Scsi_Host *shost;
392
393 if (!rphy)
394 return -ENODEV;
395
396 shost = rphy_to_shost(rphy);
397 ctrl_info = shost_to_hba(shost);
398 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
399 device = pqi_find_device_by_sas_rphy(ctrl_info, rphy);
400
401 if (!device) {
402 rc = -ENODEV;
403 goto out;
404 }
405
406 if (device->bay == 0xff)
407 rc = -EINVAL;
408 else
409 rc = device->bay;
410
411out:
412 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
413
414 return rc;
6c223761
KB
415}
416
417static int pqi_sas_phy_reset(struct sas_phy *phy, int hard_reset)
418{
419 return 0;
420}
421
422static int pqi_sas_phy_enable(struct sas_phy *phy, int enable)
423{
424 return 0;
425}
426
427static int pqi_sas_phy_setup(struct sas_phy *phy)
428{
429 return 0;
430}
431
432static void pqi_sas_phy_release(struct sas_phy *phy)
433{
434}
435
436static int pqi_sas_phy_speed(struct sas_phy *phy,
437 struct sas_phy_linkrates *rates)
438{
439 return -EINVAL;
440}
441
3d46a59a
DB
442#define CSMI_IOCTL_TIMEOUT 60
443#define SMP_CRC_FIELD_LENGTH 4
444
445static struct bmic_csmi_smp_passthru_buffer *
446pqi_build_csmi_smp_passthru_buffer(struct sas_rphy *rphy,
447 struct bsg_job *job)
448{
449 struct bmic_csmi_smp_passthru_buffer *smp_buf;
450 struct bmic_csmi_ioctl_header *ioctl_header;
451 struct bmic_csmi_smp_passthru *parameters;
452 u32 req_size;
453 u32 resp_size;
454
455 smp_buf = kzalloc(sizeof(*smp_buf), GFP_KERNEL);
456 if (!smp_buf)
457 return NULL;
458
459 req_size = job->request_payload.payload_len;
460 resp_size = job->reply_payload.payload_len;
461
462 ioctl_header = &smp_buf->ioctl_header;
463 put_unaligned_le32(sizeof(smp_buf->ioctl_header),
464 &ioctl_header->header_length);
465 put_unaligned_le32(CSMI_IOCTL_TIMEOUT, &ioctl_header->timeout);
466 put_unaligned_le32(CSMI_CC_SAS_SMP_PASSTHRU,
467 &ioctl_header->control_code);
468 put_unaligned_le32(sizeof(smp_buf->parameters), &ioctl_header->length);
469
470 parameters = &smp_buf->parameters;
471 parameters->phy_identifier = rphy->identify.phy_identifier;
472 parameters->port_identifier = 0;
473 parameters->connection_rate = 0;
474 put_unaligned_be64(rphy->identify.sas_address,
475 &parameters->destination_sas_address);
476
477 if (req_size > SMP_CRC_FIELD_LENGTH)
478 req_size -= SMP_CRC_FIELD_LENGTH;
479
480 put_unaligned_le32(req_size, &parameters->request_length);
3d46a59a
DB
481 put_unaligned_le32(resp_size, &parameters->response_length);
482
483 sg_copy_to_buffer(job->request_payload.sg_list,
484 job->reply_payload.sg_cnt, &parameters->request,
485 req_size);
486
487 return smp_buf;
488}
489
490static unsigned int pqi_build_sas_smp_handler_reply(
491 struct bmic_csmi_smp_passthru_buffer *smp_buf, struct bsg_job *job,
492 struct pqi_raid_error_info *error_info)
493{
494 sg_copy_from_buffer(job->reply_payload.sg_list,
495 job->reply_payload.sg_cnt, &smp_buf->parameters.response,
496 le32_to_cpu(smp_buf->parameters.response_length));
497
498 job->reply_len = le16_to_cpu(error_info->sense_data_length);
499 memcpy(job->reply, error_info->data,
583891c9 500 le16_to_cpu(error_info->sense_data_length));
3d46a59a
DB
501
502 return job->reply_payload.payload_len -
503 get_unaligned_le32(&error_info->data_in_transferred);
504}
505
506void pqi_sas_smp_handler(struct bsg_job *job, struct Scsi_Host *shost,
507 struct sas_rphy *rphy)
508{
509 int rc;
694c5d5b 510 struct pqi_ctrl_info *ctrl_info;
3d46a59a
DB
511 struct bmic_csmi_smp_passthru_buffer *smp_buf;
512 struct pqi_raid_error_info error_info;
513 unsigned int reslen = 0;
514
694c5d5b 515 ctrl_info = shost_to_hba(shost);
3d46a59a
DB
516
517 if (job->reply_payload.payload_len == 0) {
518 rc = -ENOMEM;
519 goto out;
520 }
521
522 if (!rphy) {
523 rc = -EINVAL;
524 goto out;
525 }
526
527 if (rphy->identify.device_type != SAS_FANOUT_EXPANDER_DEVICE) {
528 rc = -EINVAL;
529 goto out;
530 }
531
532 if (job->request_payload.sg_cnt > 1 || job->reply_payload.sg_cnt > 1) {
533 rc = -EINVAL;
534 goto out;
535 }
536
3d46a59a
DB
537 smp_buf = pqi_build_csmi_smp_passthru_buffer(rphy, job);
538 if (!smp_buf) {
539 rc = -ENOMEM;
540 goto out;
541 }
542
543 rc = pqi_csmi_smp_passthru(ctrl_info, smp_buf, sizeof(*smp_buf),
544 &error_info);
545 if (rc)
546 goto out;
547
548 reslen = pqi_build_sas_smp_handler_reply(smp_buf, job, &error_info);
583891c9 549
3d46a59a
DB
550out:
551 bsg_job_done(job, rc, reslen);
3d46a59a 552}
6c223761
KB
553struct sas_function_template pqi_sas_transport_functions = {
554 .get_linkerrors = pqi_sas_get_linkerrors,
555 .get_enclosure_identifier = pqi_sas_get_enclosure_identifier,
556 .get_bay_identifier = pqi_sas_get_bay_identifier,
557 .phy_reset = pqi_sas_phy_reset,
558 .phy_enable = pqi_sas_phy_enable,
559 .phy_setup = pqi_sas_phy_setup,
560 .phy_release = pqi_sas_phy_release,
561 .set_phy_speed = pqi_sas_phy_speed,
3d46a59a 562 .smp_handler = pqi_sas_smp_handler,
6c223761 563};