]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/scsi/hisi_sas/hisi_sas_main.c
scsi: hisi_sas: allocate slot buffer earlier
[mirror_ubuntu-bionic-kernel.git] / drivers / scsi / hisi_sas / hisi_sas_main.c
1 /*
2 * Copyright (c) 2015 Linaro Ltd.
3 * Copyright (c) 2015 Hisilicon Limited.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 */
11
12 #include "hisi_sas.h"
13 #define DRV_NAME "hisi_sas"
14
15 #define DEV_IS_GONE(dev) \
16 ((!dev) || (dev->dev_type == SAS_PHY_UNUSED))
17
18 static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device,
19 u8 *lun, struct hisi_sas_tmf_task *tmf);
20 static int
21 hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
22 struct domain_device *device,
23 int abort_flag, int tag);
24 static int hisi_sas_softreset_ata_disk(struct domain_device *device);
25 static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
26 void *funcdata);
27
28 u8 hisi_sas_get_ata_protocol(struct host_to_dev_fis *fis, int direction)
29 {
30 switch (fis->command) {
31 case ATA_CMD_FPDMA_WRITE:
32 case ATA_CMD_FPDMA_READ:
33 case ATA_CMD_FPDMA_RECV:
34 case ATA_CMD_FPDMA_SEND:
35 case ATA_CMD_NCQ_NON_DATA:
36 return HISI_SAS_SATA_PROTOCOL_FPDMA;
37
38 case ATA_CMD_DOWNLOAD_MICRO:
39 case ATA_CMD_ID_ATA:
40 case ATA_CMD_PMP_READ:
41 case ATA_CMD_READ_LOG_EXT:
42 case ATA_CMD_PIO_READ:
43 case ATA_CMD_PIO_READ_EXT:
44 case ATA_CMD_PMP_WRITE:
45 case ATA_CMD_WRITE_LOG_EXT:
46 case ATA_CMD_PIO_WRITE:
47 case ATA_CMD_PIO_WRITE_EXT:
48 return HISI_SAS_SATA_PROTOCOL_PIO;
49
50 case ATA_CMD_DSM:
51 case ATA_CMD_DOWNLOAD_MICRO_DMA:
52 case ATA_CMD_PMP_READ_DMA:
53 case ATA_CMD_PMP_WRITE_DMA:
54 case ATA_CMD_READ:
55 case ATA_CMD_READ_EXT:
56 case ATA_CMD_READ_LOG_DMA_EXT:
57 case ATA_CMD_READ_STREAM_DMA_EXT:
58 case ATA_CMD_TRUSTED_RCV_DMA:
59 case ATA_CMD_TRUSTED_SND_DMA:
60 case ATA_CMD_WRITE:
61 case ATA_CMD_WRITE_EXT:
62 case ATA_CMD_WRITE_FUA_EXT:
63 case ATA_CMD_WRITE_QUEUED:
64 case ATA_CMD_WRITE_LOG_DMA_EXT:
65 case ATA_CMD_WRITE_STREAM_DMA_EXT:
66 case ATA_CMD_ZAC_MGMT_IN:
67 return HISI_SAS_SATA_PROTOCOL_DMA;
68
69 case ATA_CMD_CHK_POWER:
70 case ATA_CMD_DEV_RESET:
71 case ATA_CMD_EDD:
72 case ATA_CMD_FLUSH:
73 case ATA_CMD_FLUSH_EXT:
74 case ATA_CMD_VERIFY:
75 case ATA_CMD_VERIFY_EXT:
76 case ATA_CMD_SET_FEATURES:
77 case ATA_CMD_STANDBY:
78 case ATA_CMD_STANDBYNOW1:
79 case ATA_CMD_ZAC_MGMT_OUT:
80 return HISI_SAS_SATA_PROTOCOL_NONDATA;
81 default:
82 {
83 if (fis->command == ATA_CMD_SET_MAX) {
84 switch (fis->features) {
85 case ATA_SET_MAX_PASSWD:
86 case ATA_SET_MAX_LOCK:
87 return HISI_SAS_SATA_PROTOCOL_PIO;
88
89 case ATA_SET_MAX_PASSWD_DMA:
90 case ATA_SET_MAX_UNLOCK_DMA:
91 return HISI_SAS_SATA_PROTOCOL_DMA;
92
93 default:
94 return HISI_SAS_SATA_PROTOCOL_NONDATA;
95 }
96 }
97 if (direction == DMA_NONE)
98 return HISI_SAS_SATA_PROTOCOL_NONDATA;
99 return HISI_SAS_SATA_PROTOCOL_PIO;
100 }
101 }
102 }
103 EXPORT_SYMBOL_GPL(hisi_sas_get_ata_protocol);
104
105 void hisi_sas_sata_done(struct sas_task *task,
106 struct hisi_sas_slot *slot)
107 {
108 struct task_status_struct *ts = &task->task_status;
109 struct ata_task_resp *resp = (struct ata_task_resp *)ts->buf;
110 struct hisi_sas_status_buffer *status_buf =
111 hisi_sas_status_buf_addr_mem(slot);
112 u8 *iu = &status_buf->iu[0];
113 struct dev_to_host_fis *d2h = (struct dev_to_host_fis *)iu;
114
115 resp->frame_len = sizeof(struct dev_to_host_fis);
116 memcpy(&resp->ending_fis[0], d2h, sizeof(struct dev_to_host_fis));
117
118 ts->buf_valid_size = sizeof(*resp);
119 }
120 EXPORT_SYMBOL_GPL(hisi_sas_sata_done);
121
122 int hisi_sas_get_ncq_tag(struct sas_task *task, u32 *tag)
123 {
124 struct ata_queued_cmd *qc = task->uldd_task;
125
126 if (qc) {
127 if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
128 qc->tf.command == ATA_CMD_FPDMA_READ) {
129 *tag = qc->tag;
130 return 1;
131 }
132 }
133 return 0;
134 }
135 EXPORT_SYMBOL_GPL(hisi_sas_get_ncq_tag);
136
137 /*
138 * This function assumes linkrate mask fits in 8 bits, which it
139 * does for all HW versions supported.
140 */
141 u8 hisi_sas_get_prog_phy_linkrate_mask(enum sas_linkrate max)
142 {
143 u16 rate = 0;
144 int i;
145
146 max -= SAS_LINK_RATE_1_5_GBPS;
147 for (i = 0; i <= max; i++)
148 rate |= 1 << (i * 2);
149 return rate;
150 }
151 EXPORT_SYMBOL_GPL(hisi_sas_get_prog_phy_linkrate_mask);
152
153 static struct hisi_hba *dev_to_hisi_hba(struct domain_device *device)
154 {
155 return device->port->ha->lldd_ha;
156 }
157
158 struct hisi_sas_port *to_hisi_sas_port(struct asd_sas_port *sas_port)
159 {
160 return container_of(sas_port, struct hisi_sas_port, sas_port);
161 }
162 EXPORT_SYMBOL_GPL(to_hisi_sas_port);
163
164 void hisi_sas_stop_phys(struct hisi_hba *hisi_hba)
165 {
166 int phy_no;
167
168 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++)
169 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
170 }
171 EXPORT_SYMBOL_GPL(hisi_sas_stop_phys);
172
173 static void hisi_sas_slot_index_clear(struct hisi_hba *hisi_hba, int slot_idx)
174 {
175 void *bitmap = hisi_hba->slot_index_tags;
176
177 clear_bit(slot_idx, bitmap);
178 }
179
180 static void hisi_sas_slot_index_free(struct hisi_hba *hisi_hba, int slot_idx)
181 {
182 hisi_sas_slot_index_clear(hisi_hba, slot_idx);
183 }
184
185 static void hisi_sas_slot_index_set(struct hisi_hba *hisi_hba, int slot_idx)
186 {
187 void *bitmap = hisi_hba->slot_index_tags;
188
189 set_bit(slot_idx, bitmap);
190 }
191
192 static int hisi_sas_slot_index_alloc(struct hisi_hba *hisi_hba, int *slot_idx)
193 {
194 unsigned int index;
195 void *bitmap = hisi_hba->slot_index_tags;
196
197 index = find_first_zero_bit(bitmap, hisi_hba->slot_index_count);
198 if (index >= hisi_hba->slot_index_count)
199 return -SAS_QUEUE_FULL;
200 hisi_sas_slot_index_set(hisi_hba, index);
201 *slot_idx = index;
202 return 0;
203 }
204
205 static void hisi_sas_slot_index_init(struct hisi_hba *hisi_hba)
206 {
207 int i;
208
209 for (i = 0; i < hisi_hba->slot_index_count; ++i)
210 hisi_sas_slot_index_clear(hisi_hba, i);
211 }
212
213 void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba, struct sas_task *task,
214 struct hisi_sas_slot *slot)
215 {
216
217 if (task) {
218 struct device *dev = hisi_hba->dev;
219
220 if (!task->lldd_task)
221 return;
222
223 task->lldd_task = NULL;
224
225 if (!sas_protocol_ata(task->task_proto))
226 if (slot->n_elem)
227 dma_unmap_sg(dev, task->scatter,
228 task->num_scatter,
229 task->data_dir);
230 }
231
232 if (slot->buf)
233 dma_pool_free(hisi_hba->buffer_pool, slot->buf, slot->buf_dma);
234
235 list_del_init(&slot->entry);
236 slot->buf = NULL;
237 slot->task = NULL;
238 slot->port = NULL;
239 hisi_sas_slot_index_free(hisi_hba, slot->idx);
240
241 /* slot memory is fully zeroed when it is reused */
242 }
243 EXPORT_SYMBOL_GPL(hisi_sas_slot_task_free);
244
245 static void hisi_sas_task_prep_smp(struct hisi_hba *hisi_hba,
246 struct hisi_sas_slot *slot)
247 {
248 hisi_hba->hw->prep_smp(hisi_hba, slot);
249 }
250
251 static void hisi_sas_task_prep_ssp(struct hisi_hba *hisi_hba,
252 struct hisi_sas_slot *slot, int is_tmf,
253 struct hisi_sas_tmf_task *tmf)
254 {
255 hisi_hba->hw->prep_ssp(hisi_hba, slot, is_tmf, tmf);
256 }
257
258 static void hisi_sas_task_prep_ata(struct hisi_hba *hisi_hba,
259 struct hisi_sas_slot *slot)
260 {
261 hisi_hba->hw->prep_stp(hisi_hba, slot);
262 }
263
264 static void hisi_sas_task_prep_abort(struct hisi_hba *hisi_hba,
265 struct hisi_sas_slot *slot,
266 int device_id, int abort_flag, int tag_to_abort)
267 {
268 hisi_hba->hw->prep_abort(hisi_hba, slot,
269 device_id, abort_flag, tag_to_abort);
270 }
271
272 /*
273 * This function will issue an abort TMF regardless of whether the
274 * task is in the sdev or not. Then it will do the task complete
275 * cleanup and callbacks.
276 */
277 static void hisi_sas_slot_abort(struct work_struct *work)
278 {
279 struct hisi_sas_slot *abort_slot =
280 container_of(work, struct hisi_sas_slot, abort_slot);
281 struct sas_task *task = abort_slot->task;
282 struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev);
283 struct scsi_cmnd *cmnd = task->uldd_task;
284 struct hisi_sas_tmf_task tmf_task;
285 struct scsi_lun lun;
286 struct device *dev = hisi_hba->dev;
287 int tag = abort_slot->idx;
288 unsigned long flags;
289
290 if (!(task->task_proto & SAS_PROTOCOL_SSP)) {
291 dev_err(dev, "cannot abort slot for non-ssp task\n");
292 goto out;
293 }
294
295 int_to_scsilun(cmnd->device->lun, &lun);
296 tmf_task.tmf = TMF_ABORT_TASK;
297 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
298
299 hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun, &tmf_task);
300 out:
301 /* Do cleanup for this task */
302 spin_lock_irqsave(&hisi_hba->lock, flags);
303 hisi_sas_slot_task_free(hisi_hba, task, abort_slot);
304 spin_unlock_irqrestore(&hisi_hba->lock, flags);
305 if (task->task_done)
306 task->task_done(task);
307 }
308
309 static int hisi_sas_task_prep(struct sas_task *task, struct hisi_sas_dq
310 *dq, int is_tmf, struct hisi_sas_tmf_task *tmf,
311 int *pass)
312 {
313 struct hisi_hba *hisi_hba = dq->hisi_hba;
314 struct domain_device *device = task->dev;
315 struct hisi_sas_device *sas_dev = device->lldd_dev;
316 struct hisi_sas_port *port;
317 struct hisi_sas_slot *slot;
318 struct hisi_sas_cmd_hdr *cmd_hdr_base;
319 struct asd_sas_port *sas_port = device->port;
320 struct device *dev = hisi_hba->dev;
321 int dlvry_queue_slot, dlvry_queue, rc, slot_idx;
322 int n_elem = 0, n_elem_req = 0, n_elem_resp = 0;
323 unsigned long flags;
324
325 if (!sas_port) {
326 struct task_status_struct *ts = &task->task_status;
327
328 ts->resp = SAS_TASK_UNDELIVERED;
329 ts->stat = SAS_PHY_DOWN;
330 /*
331 * libsas will use dev->port, should
332 * not call task_done for sata
333 */
334 if (device->dev_type != SAS_SATA_DEV)
335 task->task_done(task);
336 return -ECOMM;
337 }
338
339 if (DEV_IS_GONE(sas_dev)) {
340 if (sas_dev)
341 dev_info(dev, "task prep: device %d not ready\n",
342 sas_dev->device_id);
343 else
344 dev_info(dev, "task prep: device %016llx not ready\n",
345 SAS_ADDR(device->sas_addr));
346
347 return -ECOMM;
348 }
349
350 port = to_hisi_sas_port(sas_port);
351 if (port && !port->port_attached) {
352 dev_info(dev, "task prep: %s port%d not attach device\n",
353 (dev_is_sata(device)) ?
354 "SATA/STP" : "SAS",
355 device->port->id);
356
357 return -ECOMM;
358 }
359
360 if (!sas_protocol_ata(task->task_proto)) {
361 unsigned int req_len, resp_len;
362
363 if (task->num_scatter) {
364 n_elem = dma_map_sg(dev, task->scatter,
365 task->num_scatter, task->data_dir);
366 if (!n_elem) {
367 rc = -ENOMEM;
368 goto prep_out;
369 }
370 } else if (task->task_proto & SAS_PROTOCOL_SMP) {
371 n_elem_req = dma_map_sg(dev, &task->smp_task.smp_req,
372 1, DMA_TO_DEVICE);
373 if (!n_elem_req) {
374 rc = -ENOMEM;
375 goto prep_out;
376 }
377 req_len = sg_dma_len(&task->smp_task.smp_req);
378 if (req_len & 0x3) {
379 rc = -EINVAL;
380 goto err_out_dma_unmap;
381 }
382 n_elem_resp = dma_map_sg(dev, &task->smp_task.smp_resp,
383 1, DMA_FROM_DEVICE);
384 if (!n_elem_req) {
385 rc = -ENOMEM;
386 goto err_out_dma_unmap;
387 }
388 resp_len = sg_dma_len(&task->smp_task.smp_resp);
389 if (resp_len & 0x3) {
390 rc = -EINVAL;
391 goto err_out_dma_unmap;
392 }
393 }
394 } else
395 n_elem = task->num_scatter;
396
397 if (n_elem > HISI_SAS_SGE_PAGE_CNT) {
398 dev_err(dev, "task prep: n_elem(%d) > HISI_SAS_SGE_PAGE_CNT",
399 n_elem);
400 rc = -EINVAL;
401 goto err_out_dma_unmap;
402 }
403
404 spin_lock_irqsave(&hisi_hba->lock, flags);
405 if (hisi_hba->hw->slot_index_alloc)
406 rc = hisi_hba->hw->slot_index_alloc(hisi_hba, &slot_idx,
407 device);
408 else
409 rc = hisi_sas_slot_index_alloc(hisi_hba, &slot_idx);
410 spin_unlock_irqrestore(&hisi_hba->lock, flags);
411 if (rc)
412 goto err_out_dma_unmap;
413
414 slot = &hisi_hba->slot_info[slot_idx];
415 memset(slot, 0, sizeof(struct hisi_sas_slot));
416
417 slot->buf = dma_pool_alloc(hisi_hba->buffer_pool,
418 GFP_ATOMIC, &slot->buf_dma);
419 if (!slot->buf) {
420 rc = -ENOMEM;
421 goto err_out_tag;
422 }
423
424 rc = hisi_hba->hw->get_free_slot(hisi_hba, dq);
425 if (rc)
426 goto err_out_buf;
427
428 dlvry_queue = dq->id;
429 dlvry_queue_slot = dq->wr_point;
430
431 slot->idx = slot_idx;
432 slot->n_elem = n_elem;
433 slot->dlvry_queue = dlvry_queue;
434 slot->dlvry_queue_slot = dlvry_queue_slot;
435 cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
436 slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
437 slot->task = task;
438 slot->port = port;
439 if (is_tmf)
440 slot->is_internal = true;
441 task->lldd_task = slot;
442 INIT_WORK(&slot->abort_slot, hisi_sas_slot_abort);
443
444 memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
445 memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ);
446 memset(hisi_sas_status_buf_addr_mem(slot), 0, HISI_SAS_STATUS_BUF_SZ);
447
448 switch (task->task_proto) {
449 case SAS_PROTOCOL_SMP:
450 hisi_sas_task_prep_smp(hisi_hba, slot);
451 break;
452 case SAS_PROTOCOL_SSP:
453 hisi_sas_task_prep_ssp(hisi_hba, slot, is_tmf, tmf);
454 break;
455 case SAS_PROTOCOL_SATA:
456 case SAS_PROTOCOL_STP:
457 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
458 hisi_sas_task_prep_ata(hisi_hba, slot);
459 break;
460 default:
461 dev_err(dev, "task prep: unknown/unsupported proto (0x%x)\n",
462 task->task_proto);
463 break;
464 }
465
466 spin_lock_irqsave(&hisi_hba->lock, flags);
467 list_add_tail(&slot->entry, &sas_dev->list);
468 spin_unlock_irqrestore(&hisi_hba->lock, flags);
469 spin_lock_irqsave(&task->task_state_lock, flags);
470 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
471 spin_unlock_irqrestore(&task->task_state_lock, flags);
472
473 dq->slot_prep = slot;
474 ++(*pass);
475
476 return 0;
477
478 err_out_buf:
479 dma_pool_free(hisi_hba->buffer_pool, slot->buf,
480 slot->buf_dma);
481 err_out_tag:
482 spin_lock_irqsave(&hisi_hba->lock, flags);
483 hisi_sas_slot_index_free(hisi_hba, slot_idx);
484 spin_unlock_irqrestore(&hisi_hba->lock, flags);
485 err_out_dma_unmap:
486 if (!sas_protocol_ata(task->task_proto)) {
487 if (task->num_scatter) {
488 dma_unmap_sg(dev, task->scatter, task->num_scatter,
489 task->data_dir);
490 } else if (task->task_proto & SAS_PROTOCOL_SMP) {
491 if (n_elem_req)
492 dma_unmap_sg(dev, &task->smp_task.smp_req,
493 1, DMA_TO_DEVICE);
494 if (n_elem_resp)
495 dma_unmap_sg(dev, &task->smp_task.smp_resp,
496 1, DMA_FROM_DEVICE);
497 }
498 }
499 prep_out:
500 dev_err(dev, "task prep: failed[%d]!\n", rc);
501 return rc;
502 }
503
504 static int hisi_sas_task_exec(struct sas_task *task, gfp_t gfp_flags,
505 int is_tmf, struct hisi_sas_tmf_task *tmf)
506 {
507 u32 rc;
508 u32 pass = 0;
509 unsigned long flags;
510 struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev);
511 struct device *dev = hisi_hba->dev;
512 struct domain_device *device = task->dev;
513 struct hisi_sas_device *sas_dev = device->lldd_dev;
514 struct hisi_sas_dq *dq = sas_dev->dq;
515
516 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags)))
517 return -EINVAL;
518
519 /* protect task_prep and start_delivery sequence */
520 spin_lock_irqsave(&dq->lock, flags);
521 rc = hisi_sas_task_prep(task, dq, is_tmf, tmf, &pass);
522 if (rc)
523 dev_err(dev, "task exec: failed[%d]!\n", rc);
524
525 if (likely(pass))
526 hisi_hba->hw->start_delivery(dq);
527 spin_unlock_irqrestore(&dq->lock, flags);
528
529 return rc;
530 }
531
532 static void hisi_sas_bytes_dmaed(struct hisi_hba *hisi_hba, int phy_no)
533 {
534 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
535 struct asd_sas_phy *sas_phy = &phy->sas_phy;
536 struct sas_ha_struct *sas_ha;
537
538 if (!phy->phy_attached)
539 return;
540
541 sas_ha = &hisi_hba->sha;
542 sas_ha->notify_phy_event(sas_phy, PHYE_OOB_DONE);
543
544 if (sas_phy->phy) {
545 struct sas_phy *sphy = sas_phy->phy;
546
547 sphy->negotiated_linkrate = sas_phy->linkrate;
548 sphy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
549 sphy->maximum_linkrate_hw =
550 hisi_hba->hw->phy_get_max_linkrate();
551 if (sphy->minimum_linkrate == SAS_LINK_RATE_UNKNOWN)
552 sphy->minimum_linkrate = phy->minimum_linkrate;
553
554 if (sphy->maximum_linkrate == SAS_LINK_RATE_UNKNOWN)
555 sphy->maximum_linkrate = phy->maximum_linkrate;
556 }
557
558 if (phy->phy_type & PORT_TYPE_SAS) {
559 struct sas_identify_frame *id;
560
561 id = (struct sas_identify_frame *)phy->frame_rcvd;
562 id->dev_type = phy->identify.device_type;
563 id->initiator_bits = SAS_PROTOCOL_ALL;
564 id->target_bits = phy->identify.target_port_protocols;
565 } else if (phy->phy_type & PORT_TYPE_SATA) {
566 /*Nothing*/
567 }
568
569 sas_phy->frame_rcvd_size = phy->frame_rcvd_size;
570 sas_ha->notify_port_event(sas_phy, PORTE_BYTES_DMAED);
571 }
572
573 static struct hisi_sas_device *hisi_sas_alloc_dev(struct domain_device *device)
574 {
575 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
576 struct hisi_sas_device *sas_dev = NULL;
577 unsigned long flags;
578 int i;
579
580 spin_lock_irqsave(&hisi_hba->lock, flags);
581 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
582 if (hisi_hba->devices[i].dev_type == SAS_PHY_UNUSED) {
583 int queue = i % hisi_hba->queue_count;
584 struct hisi_sas_dq *dq = &hisi_hba->dq[queue];
585
586 hisi_hba->devices[i].device_id = i;
587 sas_dev = &hisi_hba->devices[i];
588 sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
589 sas_dev->dev_type = device->dev_type;
590 sas_dev->hisi_hba = hisi_hba;
591 sas_dev->sas_device = device;
592 sas_dev->dq = dq;
593 INIT_LIST_HEAD(&hisi_hba->devices[i].list);
594 break;
595 }
596 }
597 spin_unlock_irqrestore(&hisi_hba->lock, flags);
598
599 return sas_dev;
600 }
601
602 static int hisi_sas_dev_found(struct domain_device *device)
603 {
604 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
605 struct domain_device *parent_dev = device->parent;
606 struct hisi_sas_device *sas_dev;
607 struct device *dev = hisi_hba->dev;
608
609 if (hisi_hba->hw->alloc_dev)
610 sas_dev = hisi_hba->hw->alloc_dev(device);
611 else
612 sas_dev = hisi_sas_alloc_dev(device);
613 if (!sas_dev) {
614 dev_err(dev, "fail alloc dev: max support %d devices\n",
615 HISI_SAS_MAX_DEVICES);
616 return -EINVAL;
617 }
618
619 device->lldd_dev = sas_dev;
620 hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
621
622 if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type)) {
623 int phy_no;
624 u8 phy_num = parent_dev->ex_dev.num_phys;
625 struct ex_phy *phy;
626
627 for (phy_no = 0; phy_no < phy_num; phy_no++) {
628 phy = &parent_dev->ex_dev.ex_phy[phy_no];
629 if (SAS_ADDR(phy->attached_sas_addr) ==
630 SAS_ADDR(device->sas_addr)) {
631 sas_dev->attached_phy = phy_no;
632 break;
633 }
634 }
635
636 if (phy_no == phy_num) {
637 dev_info(dev, "dev found: no attached "
638 "dev:%016llx at ex:%016llx\n",
639 SAS_ADDR(device->sas_addr),
640 SAS_ADDR(parent_dev->sas_addr));
641 return -EINVAL;
642 }
643 }
644
645 dev_info(dev, "dev[%d:%x] found\n",
646 sas_dev->device_id, sas_dev->dev_type);
647
648 return 0;
649 }
650
651 static int hisi_sas_slave_configure(struct scsi_device *sdev)
652 {
653 struct domain_device *dev = sdev_to_domain_dev(sdev);
654 int ret = sas_slave_configure(sdev);
655
656 if (ret)
657 return ret;
658 if (!dev_is_sata(dev))
659 sas_change_queue_depth(sdev, 64);
660
661 return 0;
662 }
663
664 static void hisi_sas_scan_start(struct Scsi_Host *shost)
665 {
666 struct hisi_hba *hisi_hba = shost_priv(shost);
667
668 hisi_hba->hw->phys_init(hisi_hba);
669 }
670
671 static int hisi_sas_scan_finished(struct Scsi_Host *shost, unsigned long time)
672 {
673 struct hisi_hba *hisi_hba = shost_priv(shost);
674 struct sas_ha_struct *sha = &hisi_hba->sha;
675
676 /* Wait for PHY up interrupt to occur */
677 if (time < HZ)
678 return 0;
679
680 sas_drain_work(sha);
681 return 1;
682 }
683
684 static void hisi_sas_phyup_work(struct work_struct *work)
685 {
686 struct hisi_sas_phy *phy =
687 container_of(work, typeof(*phy), works[HISI_PHYE_PHY_UP]);
688 struct hisi_hba *hisi_hba = phy->hisi_hba;
689 struct asd_sas_phy *sas_phy = &phy->sas_phy;
690 int phy_no = sas_phy->id;
691
692 hisi_hba->hw->sl_notify(hisi_hba, phy_no); /* This requires a sleep */
693 hisi_sas_bytes_dmaed(hisi_hba, phy_no);
694 }
695
696 static void hisi_sas_linkreset_work(struct work_struct *work)
697 {
698 struct hisi_sas_phy *phy =
699 container_of(work, typeof(*phy), works[HISI_PHYE_LINK_RESET]);
700 struct asd_sas_phy *sas_phy = &phy->sas_phy;
701
702 hisi_sas_control_phy(sas_phy, PHY_FUNC_LINK_RESET, NULL);
703 }
704
705 static const work_func_t hisi_sas_phye_fns[HISI_PHYES_NUM] = {
706 [HISI_PHYE_PHY_UP] = hisi_sas_phyup_work,
707 [HISI_PHYE_LINK_RESET] = hisi_sas_linkreset_work,
708 };
709
710 bool hisi_sas_notify_phy_event(struct hisi_sas_phy *phy,
711 enum hisi_sas_phy_event event)
712 {
713 struct hisi_hba *hisi_hba = phy->hisi_hba;
714
715 if (WARN_ON(event >= HISI_PHYES_NUM))
716 return false;
717
718 return queue_work(hisi_hba->wq, &phy->works[event]);
719 }
720 EXPORT_SYMBOL_GPL(hisi_sas_notify_phy_event);
721
722 static void hisi_sas_phy_init(struct hisi_hba *hisi_hba, int phy_no)
723 {
724 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
725 struct asd_sas_phy *sas_phy = &phy->sas_phy;
726 int i;
727
728 phy->hisi_hba = hisi_hba;
729 phy->port = NULL;
730 phy->minimum_linkrate = SAS_LINK_RATE_1_5_GBPS;
731 phy->maximum_linkrate = hisi_hba->hw->phy_get_max_linkrate();
732 sas_phy->enabled = (phy_no < hisi_hba->n_phy) ? 1 : 0;
733 sas_phy->class = SAS;
734 sas_phy->iproto = SAS_PROTOCOL_ALL;
735 sas_phy->tproto = 0;
736 sas_phy->type = PHY_TYPE_PHYSICAL;
737 sas_phy->role = PHY_ROLE_INITIATOR;
738 sas_phy->oob_mode = OOB_NOT_CONNECTED;
739 sas_phy->linkrate = SAS_LINK_RATE_UNKNOWN;
740 sas_phy->id = phy_no;
741 sas_phy->sas_addr = &hisi_hba->sas_addr[0];
742 sas_phy->frame_rcvd = &phy->frame_rcvd[0];
743 sas_phy->ha = (struct sas_ha_struct *)hisi_hba->shost->hostdata;
744 sas_phy->lldd_phy = phy;
745
746 for (i = 0; i < HISI_PHYES_NUM; i++)
747 INIT_WORK(&phy->works[i], hisi_sas_phye_fns[i]);
748 }
749
750 static void hisi_sas_port_notify_formed(struct asd_sas_phy *sas_phy)
751 {
752 struct sas_ha_struct *sas_ha = sas_phy->ha;
753 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
754 struct hisi_sas_phy *phy = sas_phy->lldd_phy;
755 struct asd_sas_port *sas_port = sas_phy->port;
756 struct hisi_sas_port *port = to_hisi_sas_port(sas_port);
757 unsigned long flags;
758
759 if (!sas_port)
760 return;
761
762 spin_lock_irqsave(&hisi_hba->lock, flags);
763 port->port_attached = 1;
764 port->id = phy->port_id;
765 phy->port = port;
766 sas_port->lldd_port = port;
767 spin_unlock_irqrestore(&hisi_hba->lock, flags);
768 }
769
770 static void hisi_sas_do_release_task(struct hisi_hba *hisi_hba, struct sas_task *task,
771 struct hisi_sas_slot *slot)
772 {
773 if (task) {
774 unsigned long flags;
775 struct task_status_struct *ts;
776
777 ts = &task->task_status;
778
779 ts->resp = SAS_TASK_COMPLETE;
780 ts->stat = SAS_ABORTED_TASK;
781 spin_lock_irqsave(&task->task_state_lock, flags);
782 task->task_state_flags &=
783 ~(SAS_TASK_STATE_PENDING | SAS_TASK_AT_INITIATOR);
784 task->task_state_flags |= SAS_TASK_STATE_DONE;
785 spin_unlock_irqrestore(&task->task_state_lock, flags);
786 }
787
788 hisi_sas_slot_task_free(hisi_hba, task, slot);
789 }
790
791 /* hisi_hba.lock should be locked */
792 static void hisi_sas_release_task(struct hisi_hba *hisi_hba,
793 struct domain_device *device)
794 {
795 struct hisi_sas_slot *slot, *slot2;
796 struct hisi_sas_device *sas_dev = device->lldd_dev;
797
798 list_for_each_entry_safe(slot, slot2, &sas_dev->list, entry)
799 hisi_sas_do_release_task(hisi_hba, slot->task, slot);
800 }
801
802 void hisi_sas_release_tasks(struct hisi_hba *hisi_hba)
803 {
804 struct hisi_sas_device *sas_dev;
805 struct domain_device *device;
806 int i;
807
808 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
809 sas_dev = &hisi_hba->devices[i];
810 device = sas_dev->sas_device;
811
812 if ((sas_dev->dev_type == SAS_PHY_UNUSED) ||
813 !device)
814 continue;
815
816 hisi_sas_release_task(hisi_hba, device);
817 }
818 }
819 EXPORT_SYMBOL_GPL(hisi_sas_release_tasks);
820
821 static void hisi_sas_dereg_device(struct hisi_hba *hisi_hba,
822 struct domain_device *device)
823 {
824 if (hisi_hba->hw->dereg_device)
825 hisi_hba->hw->dereg_device(hisi_hba, device);
826 }
827
828 static void hisi_sas_dev_gone(struct domain_device *device)
829 {
830 struct hisi_sas_device *sas_dev = device->lldd_dev;
831 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
832 struct device *dev = hisi_hba->dev;
833
834 dev_info(dev, "dev[%d:%x] is gone\n",
835 sas_dev->device_id, sas_dev->dev_type);
836
837 if (!test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags)) {
838 hisi_sas_internal_task_abort(hisi_hba, device,
839 HISI_SAS_INT_ABT_DEV, 0);
840
841 hisi_sas_dereg_device(hisi_hba, device);
842
843 hisi_hba->hw->clear_itct(hisi_hba, sas_dev);
844 device->lldd_dev = NULL;
845 }
846
847 if (hisi_hba->hw->free_device)
848 hisi_hba->hw->free_device(sas_dev);
849 sas_dev->dev_type = SAS_PHY_UNUSED;
850 }
851
852 static int hisi_sas_queue_command(struct sas_task *task, gfp_t gfp_flags)
853 {
854 return hisi_sas_task_exec(task, gfp_flags, 0, NULL);
855 }
856
857 static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
858 void *funcdata)
859 {
860 struct sas_ha_struct *sas_ha = sas_phy->ha;
861 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
862 int phy_no = sas_phy->id;
863
864 switch (func) {
865 case PHY_FUNC_HARD_RESET:
866 hisi_hba->hw->phy_hard_reset(hisi_hba, phy_no);
867 break;
868
869 case PHY_FUNC_LINK_RESET:
870 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
871 msleep(100);
872 hisi_hba->hw->phy_start(hisi_hba, phy_no);
873 break;
874
875 case PHY_FUNC_DISABLE:
876 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
877 break;
878
879 case PHY_FUNC_SET_LINK_RATE:
880 hisi_hba->hw->phy_set_linkrate(hisi_hba, phy_no, funcdata);
881 break;
882 case PHY_FUNC_GET_EVENTS:
883 if (hisi_hba->hw->get_events) {
884 hisi_hba->hw->get_events(hisi_hba, phy_no);
885 break;
886 }
887 /* fallthru */
888 case PHY_FUNC_RELEASE_SPINUP_HOLD:
889 default:
890 return -EOPNOTSUPP;
891 }
892 return 0;
893 }
894
895 static void hisi_sas_task_done(struct sas_task *task)
896 {
897 if (!del_timer(&task->slow_task->timer))
898 return;
899 complete(&task->slow_task->completion);
900 }
901
902 static void hisi_sas_tmf_timedout(struct timer_list *t)
903 {
904 struct sas_task_slow *slow = from_timer(slow, t, timer);
905 struct sas_task *task = slow->task;
906 unsigned long flags;
907
908 spin_lock_irqsave(&task->task_state_lock, flags);
909 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
910 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
911 spin_unlock_irqrestore(&task->task_state_lock, flags);
912
913 complete(&task->slow_task->completion);
914 }
915
916 #define TASK_TIMEOUT 20
917 #define TASK_RETRY 3
918 #define INTERNAL_ABORT_TIMEOUT 6
919 static int hisi_sas_exec_internal_tmf_task(struct domain_device *device,
920 void *parameter, u32 para_len,
921 struct hisi_sas_tmf_task *tmf)
922 {
923 struct hisi_sas_device *sas_dev = device->lldd_dev;
924 struct hisi_hba *hisi_hba = sas_dev->hisi_hba;
925 struct device *dev = hisi_hba->dev;
926 struct sas_task *task;
927 int res, retry;
928
929 for (retry = 0; retry < TASK_RETRY; retry++) {
930 task = sas_alloc_slow_task(GFP_KERNEL);
931 if (!task)
932 return -ENOMEM;
933
934 task->dev = device;
935 task->task_proto = device->tproto;
936
937 if (dev_is_sata(device)) {
938 task->ata_task.device_control_reg_update = 1;
939 memcpy(&task->ata_task.fis, parameter, para_len);
940 } else {
941 memcpy(&task->ssp_task, parameter, para_len);
942 }
943 task->task_done = hisi_sas_task_done;
944
945 task->slow_task->timer.function = hisi_sas_tmf_timedout;
946 task->slow_task->timer.expires = jiffies + TASK_TIMEOUT*HZ;
947 add_timer(&task->slow_task->timer);
948
949 res = hisi_sas_task_exec(task, GFP_KERNEL, 1, tmf);
950
951 if (res) {
952 del_timer(&task->slow_task->timer);
953 dev_err(dev, "abort tmf: executing internal task failed: %d\n",
954 res);
955 goto ex_err;
956 }
957
958 wait_for_completion(&task->slow_task->completion);
959 res = TMF_RESP_FUNC_FAILED;
960 /* Even TMF timed out, return direct. */
961 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
962 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
963 struct hisi_sas_slot *slot = task->lldd_task;
964
965 dev_err(dev, "abort tmf: TMF task timeout and not done\n");
966 if (slot)
967 slot->task = NULL;
968
969 goto ex_err;
970 } else
971 dev_err(dev, "abort tmf: TMF task timeout\n");
972 }
973
974 if (task->task_status.resp == SAS_TASK_COMPLETE &&
975 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
976 res = TMF_RESP_FUNC_COMPLETE;
977 break;
978 }
979
980 if (task->task_status.resp == SAS_TASK_COMPLETE &&
981 task->task_status.stat == TMF_RESP_FUNC_SUCC) {
982 res = TMF_RESP_FUNC_SUCC;
983 break;
984 }
985
986 if (task->task_status.resp == SAS_TASK_COMPLETE &&
987 task->task_status.stat == SAS_DATA_UNDERRUN) {
988 /* no error, but return the number of bytes of
989 * underrun
990 */
991 dev_warn(dev, "abort tmf: task to dev %016llx "
992 "resp: 0x%x sts 0x%x underrun\n",
993 SAS_ADDR(device->sas_addr),
994 task->task_status.resp,
995 task->task_status.stat);
996 res = task->task_status.residual;
997 break;
998 }
999
1000 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1001 task->task_status.stat == SAS_DATA_OVERRUN) {
1002 dev_warn(dev, "abort tmf: blocked task error\n");
1003 res = -EMSGSIZE;
1004 break;
1005 }
1006
1007 dev_warn(dev, "abort tmf: task to dev "
1008 "%016llx resp: 0x%x status 0x%x\n",
1009 SAS_ADDR(device->sas_addr), task->task_status.resp,
1010 task->task_status.stat);
1011 sas_free_task(task);
1012 task = NULL;
1013 }
1014 ex_err:
1015 if (retry == TASK_RETRY)
1016 dev_warn(dev, "abort tmf: executing internal task failed!\n");
1017 sas_free_task(task);
1018 return res;
1019 }
1020
1021 static void hisi_sas_fill_ata_reset_cmd(struct ata_device *dev,
1022 bool reset, int pmp, u8 *fis)
1023 {
1024 struct ata_taskfile tf;
1025
1026 ata_tf_init(dev, &tf);
1027 if (reset)
1028 tf.ctl |= ATA_SRST;
1029 else
1030 tf.ctl &= ~ATA_SRST;
1031 tf.command = ATA_CMD_DEV_RESET;
1032 ata_tf_to_fis(&tf, pmp, 0, fis);
1033 }
1034
1035 static int hisi_sas_softreset_ata_disk(struct domain_device *device)
1036 {
1037 u8 fis[20] = {0};
1038 struct ata_port *ap = device->sata_dev.ap;
1039 struct ata_link *link;
1040 int rc = TMF_RESP_FUNC_FAILED;
1041 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1042 struct device *dev = hisi_hba->dev;
1043 int s = sizeof(struct host_to_dev_fis);
1044 unsigned long flags;
1045
1046 ata_for_each_link(link, ap, EDGE) {
1047 int pmp = sata_srst_pmp(link);
1048
1049 hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis);
1050 rc = hisi_sas_exec_internal_tmf_task(device, fis, s, NULL);
1051 if (rc != TMF_RESP_FUNC_COMPLETE)
1052 break;
1053 }
1054
1055 if (rc == TMF_RESP_FUNC_COMPLETE) {
1056 ata_for_each_link(link, ap, EDGE) {
1057 int pmp = sata_srst_pmp(link);
1058
1059 hisi_sas_fill_ata_reset_cmd(link->device, 0, pmp, fis);
1060 rc = hisi_sas_exec_internal_tmf_task(device, fis,
1061 s, NULL);
1062 if (rc != TMF_RESP_FUNC_COMPLETE)
1063 dev_err(dev, "ata disk de-reset failed\n");
1064 }
1065 } else {
1066 dev_err(dev, "ata disk reset failed\n");
1067 }
1068
1069 if (rc == TMF_RESP_FUNC_COMPLETE) {
1070 spin_lock_irqsave(&hisi_hba->lock, flags);
1071 hisi_sas_release_task(hisi_hba, device);
1072 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1073 }
1074
1075 return rc;
1076 }
1077
1078 static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device,
1079 u8 *lun, struct hisi_sas_tmf_task *tmf)
1080 {
1081 struct sas_ssp_task ssp_task;
1082
1083 if (!(device->tproto & SAS_PROTOCOL_SSP))
1084 return TMF_RESP_FUNC_ESUPP;
1085
1086 memcpy(ssp_task.LUN, lun, 8);
1087
1088 return hisi_sas_exec_internal_tmf_task(device, &ssp_task,
1089 sizeof(ssp_task), tmf);
1090 }
1091
1092 static void hisi_sas_refresh_port_id(struct hisi_hba *hisi_hba)
1093 {
1094 u32 state = hisi_hba->hw->get_phys_state(hisi_hba);
1095 int i;
1096
1097 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1098 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1099 struct domain_device *device = sas_dev->sas_device;
1100 struct asd_sas_port *sas_port;
1101 struct hisi_sas_port *port;
1102 struct hisi_sas_phy *phy = NULL;
1103 struct asd_sas_phy *sas_phy;
1104
1105 if ((sas_dev->dev_type == SAS_PHY_UNUSED)
1106 || !device || !device->port)
1107 continue;
1108
1109 sas_port = device->port;
1110 port = to_hisi_sas_port(sas_port);
1111
1112 list_for_each_entry(sas_phy, &sas_port->phy_list, port_phy_el)
1113 if (state & BIT(sas_phy->id)) {
1114 phy = sas_phy->lldd_phy;
1115 break;
1116 }
1117
1118 if (phy) {
1119 port->id = phy->port_id;
1120
1121 /* Update linkrate of directly attached device. */
1122 if (!device->parent)
1123 device->linkrate = phy->sas_phy.linkrate;
1124
1125 hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
1126 } else
1127 port->id = 0xff;
1128 }
1129 }
1130
1131 static void hisi_sas_rescan_topology(struct hisi_hba *hisi_hba, u32 old_state,
1132 u32 state)
1133 {
1134 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1135 struct asd_sas_port *_sas_port = NULL;
1136 int phy_no;
1137
1138 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) {
1139 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1140 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1141 struct asd_sas_port *sas_port = sas_phy->port;
1142 bool do_port_check = !!(_sas_port != sas_port);
1143
1144 if (!sas_phy->phy->enabled)
1145 continue;
1146
1147 /* Report PHY state change to libsas */
1148 if (state & BIT(phy_no)) {
1149 if (do_port_check && sas_port && sas_port->port_dev) {
1150 struct domain_device *dev = sas_port->port_dev;
1151
1152 _sas_port = sas_port;
1153
1154 if (DEV_IS_EXPANDER(dev->dev_type))
1155 sas_ha->notify_port_event(sas_phy,
1156 PORTE_BROADCAST_RCVD);
1157 }
1158 } else if (old_state & (1 << phy_no))
1159 /* PHY down but was up before */
1160 hisi_sas_phy_down(hisi_hba, phy_no, 0);
1161
1162 }
1163 }
1164
1165 static int hisi_sas_controller_reset(struct hisi_hba *hisi_hba)
1166 {
1167 struct device *dev = hisi_hba->dev;
1168 struct Scsi_Host *shost = hisi_hba->shost;
1169 u32 old_state, state;
1170 unsigned long flags;
1171 int rc;
1172
1173 if (!hisi_hba->hw->soft_reset)
1174 return -1;
1175
1176 if (test_and_set_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags))
1177 return -1;
1178
1179 dev_info(dev, "controller resetting...\n");
1180 old_state = hisi_hba->hw->get_phys_state(hisi_hba);
1181
1182 scsi_block_requests(shost);
1183 if (timer_pending(&hisi_hba->timer))
1184 del_timer_sync(&hisi_hba->timer);
1185
1186 set_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1187 rc = hisi_hba->hw->soft_reset(hisi_hba);
1188 if (rc) {
1189 dev_warn(dev, "controller reset failed (%d)\n", rc);
1190 clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1191 scsi_unblock_requests(shost);
1192 goto out;
1193 }
1194 spin_lock_irqsave(&hisi_hba->lock, flags);
1195 hisi_sas_release_tasks(hisi_hba);
1196 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1197
1198 clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1199
1200 /* Init and wait for PHYs to come up and all libsas event finished. */
1201 hisi_hba->hw->phys_init(hisi_hba);
1202 msleep(1000);
1203 hisi_sas_refresh_port_id(hisi_hba);
1204 scsi_unblock_requests(shost);
1205
1206 state = hisi_hba->hw->get_phys_state(hisi_hba);
1207 hisi_sas_rescan_topology(hisi_hba, old_state, state);
1208 dev_info(dev, "controller reset complete\n");
1209
1210 out:
1211 clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
1212
1213 return rc;
1214 }
1215
1216 static int hisi_sas_abort_task(struct sas_task *task)
1217 {
1218 struct scsi_lun lun;
1219 struct hisi_sas_tmf_task tmf_task;
1220 struct domain_device *device = task->dev;
1221 struct hisi_sas_device *sas_dev = device->lldd_dev;
1222 struct hisi_hba *hisi_hba;
1223 struct device *dev;
1224 int rc = TMF_RESP_FUNC_FAILED;
1225 unsigned long flags;
1226
1227 if (!sas_dev)
1228 return TMF_RESP_FUNC_FAILED;
1229
1230 hisi_hba = dev_to_hisi_hba(task->dev);
1231 dev = hisi_hba->dev;
1232
1233 spin_lock_irqsave(&task->task_state_lock, flags);
1234 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
1235 spin_unlock_irqrestore(&task->task_state_lock, flags);
1236 rc = TMF_RESP_FUNC_COMPLETE;
1237 goto out;
1238 }
1239 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1240 spin_unlock_irqrestore(&task->task_state_lock, flags);
1241
1242 sas_dev->dev_status = HISI_SAS_DEV_EH;
1243 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1244 struct scsi_cmnd *cmnd = task->uldd_task;
1245 struct hisi_sas_slot *slot = task->lldd_task;
1246 u32 tag = slot->idx;
1247 int rc2;
1248
1249 int_to_scsilun(cmnd->device->lun, &lun);
1250 tmf_task.tmf = TMF_ABORT_TASK;
1251 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
1252
1253 rc = hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun,
1254 &tmf_task);
1255
1256 rc2 = hisi_sas_internal_task_abort(hisi_hba, device,
1257 HISI_SAS_INT_ABT_CMD, tag);
1258 if (rc2 < 0) {
1259 dev_err(dev, "abort task: internal abort (%d)\n", rc2);
1260 return TMF_RESP_FUNC_FAILED;
1261 }
1262
1263 /*
1264 * If the TMF finds that the IO is not in the device and also
1265 * the internal abort does not succeed, then it is safe to
1266 * free the slot.
1267 * Note: if the internal abort succeeds then the slot
1268 * will have already been completed
1269 */
1270 if (rc == TMF_RESP_FUNC_COMPLETE && rc2 != TMF_RESP_FUNC_SUCC) {
1271 if (task->lldd_task) {
1272 spin_lock_irqsave(&hisi_hba->lock, flags);
1273 hisi_sas_do_release_task(hisi_hba, task, slot);
1274 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1275 }
1276 }
1277 } else if (task->task_proto & SAS_PROTOCOL_SATA ||
1278 task->task_proto & SAS_PROTOCOL_STP) {
1279 if (task->dev->dev_type == SAS_SATA_DEV) {
1280 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1281 HISI_SAS_INT_ABT_DEV, 0);
1282 if (rc < 0) {
1283 dev_err(dev, "abort task: internal abort failed\n");
1284 goto out;
1285 }
1286 hisi_sas_dereg_device(hisi_hba, device);
1287 rc = hisi_sas_softreset_ata_disk(device);
1288 }
1289 } else if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SMP) {
1290 /* SMP */
1291 struct hisi_sas_slot *slot = task->lldd_task;
1292 u32 tag = slot->idx;
1293
1294 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1295 HISI_SAS_INT_ABT_CMD, tag);
1296 if (((rc < 0) || (rc == TMF_RESP_FUNC_FAILED)) &&
1297 task->lldd_task) {
1298 spin_lock_irqsave(&hisi_hba->lock, flags);
1299 hisi_sas_do_release_task(hisi_hba, task, slot);
1300 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1301 }
1302 }
1303
1304 out:
1305 if (rc != TMF_RESP_FUNC_COMPLETE)
1306 dev_notice(dev, "abort task: rc=%d\n", rc);
1307 return rc;
1308 }
1309
1310 static int hisi_sas_abort_task_set(struct domain_device *device, u8 *lun)
1311 {
1312 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1313 struct device *dev = hisi_hba->dev;
1314 struct hisi_sas_tmf_task tmf_task;
1315 int rc = TMF_RESP_FUNC_FAILED;
1316 unsigned long flags;
1317
1318 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1319 HISI_SAS_INT_ABT_DEV, 0);
1320 if (rc < 0) {
1321 dev_err(dev, "abort task set: internal abort rc=%d\n", rc);
1322 return TMF_RESP_FUNC_FAILED;
1323 }
1324 hisi_sas_dereg_device(hisi_hba, device);
1325
1326 tmf_task.tmf = TMF_ABORT_TASK_SET;
1327 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1328
1329 if (rc == TMF_RESP_FUNC_COMPLETE) {
1330 spin_lock_irqsave(&hisi_hba->lock, flags);
1331 hisi_sas_release_task(hisi_hba, device);
1332 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1333 }
1334
1335 return rc;
1336 }
1337
1338 static int hisi_sas_clear_aca(struct domain_device *device, u8 *lun)
1339 {
1340 int rc = TMF_RESP_FUNC_FAILED;
1341 struct hisi_sas_tmf_task tmf_task;
1342
1343 tmf_task.tmf = TMF_CLEAR_ACA;
1344 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1345
1346 return rc;
1347 }
1348
1349 static int hisi_sas_debug_I_T_nexus_reset(struct domain_device *device)
1350 {
1351 struct sas_phy *phy = sas_get_local_phy(device);
1352 int rc, reset_type = (device->dev_type == SAS_SATA_DEV ||
1353 (device->tproto & SAS_PROTOCOL_STP)) ? 0 : 1;
1354 rc = sas_phy_reset(phy, reset_type);
1355 sas_put_local_phy(phy);
1356 msleep(2000);
1357 return rc;
1358 }
1359
1360 static int hisi_sas_I_T_nexus_reset(struct domain_device *device)
1361 {
1362 struct hisi_sas_device *sas_dev = device->lldd_dev;
1363 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1364 struct device *dev = hisi_hba->dev;
1365 int rc = TMF_RESP_FUNC_FAILED;
1366 unsigned long flags;
1367
1368 if (sas_dev->dev_status != HISI_SAS_DEV_EH)
1369 return TMF_RESP_FUNC_FAILED;
1370 sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
1371
1372 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1373 HISI_SAS_INT_ABT_DEV, 0);
1374 if (rc < 0) {
1375 dev_err(dev, "I_T nexus reset: internal abort (%d)\n", rc);
1376 return TMF_RESP_FUNC_FAILED;
1377 }
1378 hisi_sas_dereg_device(hisi_hba, device);
1379
1380 rc = hisi_sas_debug_I_T_nexus_reset(device);
1381
1382 if ((rc == TMF_RESP_FUNC_COMPLETE) || (rc == -ENODEV)) {
1383 spin_lock_irqsave(&hisi_hba->lock, flags);
1384 hisi_sas_release_task(hisi_hba, device);
1385 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1386 }
1387 return rc;
1388 }
1389
1390 static int hisi_sas_lu_reset(struct domain_device *device, u8 *lun)
1391 {
1392 struct hisi_sas_device *sas_dev = device->lldd_dev;
1393 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1394 struct device *dev = hisi_hba->dev;
1395 unsigned long flags;
1396 int rc = TMF_RESP_FUNC_FAILED;
1397
1398 sas_dev->dev_status = HISI_SAS_DEV_EH;
1399 if (dev_is_sata(device)) {
1400 struct sas_phy *phy;
1401
1402 /* Clear internal IO and then hardreset */
1403 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1404 HISI_SAS_INT_ABT_DEV, 0);
1405 if (rc < 0) {
1406 dev_err(dev, "lu_reset: internal abort failed\n");
1407 goto out;
1408 }
1409 hisi_sas_dereg_device(hisi_hba, device);
1410
1411 phy = sas_get_local_phy(device);
1412
1413 rc = sas_phy_reset(phy, 1);
1414
1415 if (rc == 0) {
1416 spin_lock_irqsave(&hisi_hba->lock, flags);
1417 hisi_sas_release_task(hisi_hba, device);
1418 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1419 }
1420 sas_put_local_phy(phy);
1421 } else {
1422 struct hisi_sas_tmf_task tmf_task = { .tmf = TMF_LU_RESET };
1423
1424 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1425 HISI_SAS_INT_ABT_DEV, 0);
1426 if (rc < 0) {
1427 dev_err(dev, "lu_reset: internal abort failed\n");
1428 goto out;
1429 }
1430 hisi_sas_dereg_device(hisi_hba, device);
1431
1432 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1433 if (rc == TMF_RESP_FUNC_COMPLETE) {
1434 spin_lock_irqsave(&hisi_hba->lock, flags);
1435 hisi_sas_release_task(hisi_hba, device);
1436 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1437 }
1438 }
1439 out:
1440 if (rc != TMF_RESP_FUNC_COMPLETE)
1441 dev_err(dev, "lu_reset: for device[%d]:rc= %d\n",
1442 sas_dev->device_id, rc);
1443 return rc;
1444 }
1445
1446 static int hisi_sas_clear_nexus_ha(struct sas_ha_struct *sas_ha)
1447 {
1448 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
1449 HISI_SAS_DECLARE_RST_WORK_ON_STACK(r);
1450
1451 queue_work(hisi_hba->wq, &r.work);
1452 wait_for_completion(r.completion);
1453 if (r.done)
1454 return TMF_RESP_FUNC_COMPLETE;
1455
1456 return TMF_RESP_FUNC_FAILED;
1457 }
1458
1459 static int hisi_sas_query_task(struct sas_task *task)
1460 {
1461 struct scsi_lun lun;
1462 struct hisi_sas_tmf_task tmf_task;
1463 int rc = TMF_RESP_FUNC_FAILED;
1464
1465 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1466 struct scsi_cmnd *cmnd = task->uldd_task;
1467 struct domain_device *device = task->dev;
1468 struct hisi_sas_slot *slot = task->lldd_task;
1469 u32 tag = slot->idx;
1470
1471 int_to_scsilun(cmnd->device->lun, &lun);
1472 tmf_task.tmf = TMF_QUERY_TASK;
1473 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
1474
1475 rc = hisi_sas_debug_issue_ssp_tmf(device,
1476 lun.scsi_lun,
1477 &tmf_task);
1478 switch (rc) {
1479 /* The task is still in Lun, release it then */
1480 case TMF_RESP_FUNC_SUCC:
1481 /* The task is not in Lun or failed, reset the phy */
1482 case TMF_RESP_FUNC_FAILED:
1483 case TMF_RESP_FUNC_COMPLETE:
1484 break;
1485 default:
1486 rc = TMF_RESP_FUNC_FAILED;
1487 break;
1488 }
1489 }
1490 return rc;
1491 }
1492
1493 static int
1494 hisi_sas_internal_abort_task_exec(struct hisi_hba *hisi_hba, int device_id,
1495 struct sas_task *task, int abort_flag,
1496 int task_tag)
1497 {
1498 struct domain_device *device = task->dev;
1499 struct hisi_sas_device *sas_dev = device->lldd_dev;
1500 struct device *dev = hisi_hba->dev;
1501 struct hisi_sas_port *port;
1502 struct hisi_sas_slot *slot;
1503 struct asd_sas_port *sas_port = device->port;
1504 struct hisi_sas_cmd_hdr *cmd_hdr_base;
1505 struct hisi_sas_dq *dq = sas_dev->dq;
1506 int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx;
1507 unsigned long flags, flags_dq;
1508
1509 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags)))
1510 return -EINVAL;
1511
1512 if (!device->port)
1513 return -1;
1514
1515 port = to_hisi_sas_port(sas_port);
1516
1517 /* simply get a slot and send abort command */
1518 spin_lock_irqsave(&hisi_hba->lock, flags);
1519 rc = hisi_sas_slot_index_alloc(hisi_hba, &slot_idx);
1520 if (rc) {
1521 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1522 goto err_out;
1523 }
1524 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1525
1526 slot = &hisi_hba->slot_info[slot_idx];
1527 memset(slot, 0, sizeof(struct hisi_sas_slot));
1528
1529 slot->buf = dma_pool_alloc(hisi_hba->buffer_pool,
1530 GFP_ATOMIC, &slot->buf_dma);
1531 if (!slot->buf) {
1532 rc = -ENOMEM;
1533 goto err_out_tag;
1534 }
1535 spin_lock_irqsave(&dq->lock, flags_dq);
1536 rc = hisi_hba->hw->get_free_slot(hisi_hba, dq);
1537 if (rc) {
1538 rc = -ENOMEM;
1539 spin_unlock_irqrestore(&dq->lock, flags_dq);
1540 goto err_out_buf;
1541 }
1542
1543 dlvry_queue = dq->id;
1544 dlvry_queue_slot = dq->wr_point;
1545
1546 slot->idx = slot_idx;
1547 slot->n_elem = n_elem;
1548 slot->dlvry_queue = dlvry_queue;
1549 slot->dlvry_queue_slot = dlvry_queue_slot;
1550 cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
1551 slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
1552 slot->task = task;
1553 slot->port = port;
1554 slot->is_internal = true;
1555 task->lldd_task = slot;
1556
1557 memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
1558 memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ);
1559 memset(hisi_sas_status_buf_addr_mem(slot), 0, HISI_SAS_STATUS_BUF_SZ);
1560
1561 hisi_sas_task_prep_abort(hisi_hba, slot, device_id,
1562 abort_flag, task_tag);
1563
1564 spin_lock_irqsave(&hisi_hba->lock, flags);
1565 list_add_tail(&slot->entry, &sas_dev->list);
1566 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1567 spin_lock_irqsave(&task->task_state_lock, flags);
1568 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
1569 spin_unlock_irqrestore(&task->task_state_lock, flags);
1570
1571 dq->slot_prep = slot;
1572
1573 /* send abort command to the chip */
1574 hisi_hba->hw->start_delivery(dq);
1575 spin_unlock_irqrestore(&dq->lock, flags_dq);
1576
1577 return 0;
1578
1579 err_out_buf:
1580 dma_pool_free(hisi_hba->buffer_pool, slot->buf,
1581 slot->buf_dma);
1582 err_out_tag:
1583 spin_lock_irqsave(&hisi_hba->lock, flags);
1584 hisi_sas_slot_index_free(hisi_hba, slot_idx);
1585 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1586 err_out:
1587 dev_err(dev, "internal abort task prep: failed[%d]!\n", rc);
1588
1589 return rc;
1590 }
1591
1592 /**
1593 * hisi_sas_internal_task_abort -- execute an internal
1594 * abort command for single IO command or a device
1595 * @hisi_hba: host controller struct
1596 * @device: domain device
1597 * @abort_flag: mode of operation, device or single IO
1598 * @tag: tag of IO to be aborted (only relevant to single
1599 * IO mode)
1600 */
1601 static int
1602 hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
1603 struct domain_device *device,
1604 int abort_flag, int tag)
1605 {
1606 struct sas_task *task;
1607 struct hisi_sas_device *sas_dev = device->lldd_dev;
1608 struct device *dev = hisi_hba->dev;
1609 int res;
1610
1611 /*
1612 * The interface is not realized means this HW don't support internal
1613 * abort, or don't need to do internal abort. Then here, we return
1614 * TMF_RESP_FUNC_FAILED and let other steps go on, which depends that
1615 * the internal abort has been executed and returned CQ.
1616 */
1617 if (!hisi_hba->hw->prep_abort)
1618 return TMF_RESP_FUNC_FAILED;
1619
1620 task = sas_alloc_slow_task(GFP_KERNEL);
1621 if (!task)
1622 return -ENOMEM;
1623
1624 task->dev = device;
1625 task->task_proto = device->tproto;
1626 task->task_done = hisi_sas_task_done;
1627 task->slow_task->timer.function = hisi_sas_tmf_timedout;
1628 task->slow_task->timer.expires = jiffies + INTERNAL_ABORT_TIMEOUT*HZ;
1629 add_timer(&task->slow_task->timer);
1630
1631 res = hisi_sas_internal_abort_task_exec(hisi_hba, sas_dev->device_id,
1632 task, abort_flag, tag);
1633 if (res) {
1634 del_timer(&task->slow_task->timer);
1635 dev_err(dev, "internal task abort: executing internal task failed: %d\n",
1636 res);
1637 goto exit;
1638 }
1639 wait_for_completion(&task->slow_task->completion);
1640 res = TMF_RESP_FUNC_FAILED;
1641
1642 /* Internal abort timed out */
1643 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1644 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1645 struct hisi_sas_slot *slot = task->lldd_task;
1646
1647 if (slot)
1648 slot->task = NULL;
1649 dev_err(dev, "internal task abort: timeout and not done.\n");
1650 res = -EIO;
1651 goto exit;
1652 } else
1653 dev_err(dev, "internal task abort: timeout.\n");
1654 }
1655
1656 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1657 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
1658 res = TMF_RESP_FUNC_COMPLETE;
1659 goto exit;
1660 }
1661
1662 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1663 task->task_status.stat == TMF_RESP_FUNC_SUCC) {
1664 res = TMF_RESP_FUNC_SUCC;
1665 goto exit;
1666 }
1667
1668 exit:
1669 dev_dbg(dev, "internal task abort: task to dev %016llx task=%p "
1670 "resp: 0x%x sts 0x%x\n",
1671 SAS_ADDR(device->sas_addr),
1672 task,
1673 task->task_status.resp, /* 0 is complete, -1 is undelivered */
1674 task->task_status.stat);
1675 sas_free_task(task);
1676
1677 return res;
1678 }
1679
1680 static void hisi_sas_port_formed(struct asd_sas_phy *sas_phy)
1681 {
1682 hisi_sas_port_notify_formed(sas_phy);
1683 }
1684
1685 static void hisi_sas_port_deformed(struct asd_sas_phy *sas_phy)
1686 {
1687 }
1688
1689 static int hisi_sas_write_gpio(struct sas_ha_struct *sha, u8 reg_type,
1690 u8 reg_index, u8 reg_count, u8 *write_data)
1691 {
1692 struct hisi_hba *hisi_hba = sha->lldd_ha;
1693
1694 if (!hisi_hba->hw->write_gpio)
1695 return -EOPNOTSUPP;
1696
1697 return hisi_hba->hw->write_gpio(hisi_hba, reg_type,
1698 reg_index, reg_count, write_data);
1699 }
1700
1701 static void hisi_sas_phy_disconnected(struct hisi_sas_phy *phy)
1702 {
1703 phy->phy_attached = 0;
1704 phy->phy_type = 0;
1705 phy->port = NULL;
1706 }
1707
1708 void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy)
1709 {
1710 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1711 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1712 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1713
1714 if (rdy) {
1715 /* Phy down but ready */
1716 hisi_sas_bytes_dmaed(hisi_hba, phy_no);
1717 hisi_sas_port_notify_formed(sas_phy);
1718 } else {
1719 struct hisi_sas_port *port = phy->port;
1720
1721 /* Phy down and not ready */
1722 sas_ha->notify_phy_event(sas_phy, PHYE_LOSS_OF_SIGNAL);
1723 sas_phy_disconnected(sas_phy);
1724
1725 if (port) {
1726 if (phy->phy_type & PORT_TYPE_SAS) {
1727 int port_id = port->id;
1728
1729 if (!hisi_hba->hw->get_wideport_bitmap(hisi_hba,
1730 port_id))
1731 port->port_attached = 0;
1732 } else if (phy->phy_type & PORT_TYPE_SATA)
1733 port->port_attached = 0;
1734 }
1735 hisi_sas_phy_disconnected(phy);
1736 }
1737 }
1738 EXPORT_SYMBOL_GPL(hisi_sas_phy_down);
1739
1740 void hisi_sas_kill_tasklets(struct hisi_hba *hisi_hba)
1741 {
1742 int i;
1743
1744 for (i = 0; i < hisi_hba->queue_count; i++) {
1745 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
1746
1747 tasklet_kill(&cq->tasklet);
1748 }
1749 }
1750 EXPORT_SYMBOL_GPL(hisi_sas_kill_tasklets);
1751
1752 struct scsi_transport_template *hisi_sas_stt;
1753 EXPORT_SYMBOL_GPL(hisi_sas_stt);
1754
1755 static struct device_attribute *host_attrs[] = {
1756 &dev_attr_phy_event_threshold,
1757 NULL,
1758 };
1759
1760 static struct scsi_host_template _hisi_sas_sht = {
1761 .module = THIS_MODULE,
1762 .name = DRV_NAME,
1763 .queuecommand = sas_queuecommand,
1764 .target_alloc = sas_target_alloc,
1765 .slave_configure = hisi_sas_slave_configure,
1766 .scan_finished = hisi_sas_scan_finished,
1767 .scan_start = hisi_sas_scan_start,
1768 .change_queue_depth = sas_change_queue_depth,
1769 .bios_param = sas_bios_param,
1770 .can_queue = 1,
1771 .this_id = -1,
1772 .sg_tablesize = SG_ALL,
1773 .max_sectors = SCSI_DEFAULT_MAX_SECTORS,
1774 .use_clustering = ENABLE_CLUSTERING,
1775 .eh_device_reset_handler = sas_eh_device_reset_handler,
1776 .eh_target_reset_handler = sas_eh_target_reset_handler,
1777 .target_destroy = sas_target_destroy,
1778 .ioctl = sas_ioctl,
1779 .shost_attrs = host_attrs,
1780 };
1781 struct scsi_host_template *hisi_sas_sht = &_hisi_sas_sht;
1782 EXPORT_SYMBOL_GPL(hisi_sas_sht);
1783
1784 static struct sas_domain_function_template hisi_sas_transport_ops = {
1785 .lldd_dev_found = hisi_sas_dev_found,
1786 .lldd_dev_gone = hisi_sas_dev_gone,
1787 .lldd_execute_task = hisi_sas_queue_command,
1788 .lldd_control_phy = hisi_sas_control_phy,
1789 .lldd_abort_task = hisi_sas_abort_task,
1790 .lldd_abort_task_set = hisi_sas_abort_task_set,
1791 .lldd_clear_aca = hisi_sas_clear_aca,
1792 .lldd_I_T_nexus_reset = hisi_sas_I_T_nexus_reset,
1793 .lldd_lu_reset = hisi_sas_lu_reset,
1794 .lldd_query_task = hisi_sas_query_task,
1795 .lldd_clear_nexus_ha = hisi_sas_clear_nexus_ha,
1796 .lldd_port_formed = hisi_sas_port_formed,
1797 .lldd_port_deformed = hisi_sas_port_deformed,
1798 .lldd_write_gpio = hisi_sas_write_gpio,
1799 };
1800
1801 void hisi_sas_init_mem(struct hisi_hba *hisi_hba)
1802 {
1803 int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
1804
1805 for (i = 0; i < hisi_hba->queue_count; i++) {
1806 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
1807 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
1808
1809 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
1810 memset(hisi_hba->cmd_hdr[i], 0, s);
1811 dq->wr_point = 0;
1812
1813 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
1814 memset(hisi_hba->complete_hdr[i], 0, s);
1815 cq->rd_point = 0;
1816 }
1817
1818 s = sizeof(struct hisi_sas_initial_fis) * hisi_hba->n_phy;
1819 memset(hisi_hba->initial_fis, 0, s);
1820
1821 s = max_command_entries * sizeof(struct hisi_sas_iost);
1822 memset(hisi_hba->iost, 0, s);
1823
1824 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
1825 memset(hisi_hba->breakpoint, 0, s);
1826
1827 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
1828 memset(hisi_hba->sata_breakpoint, 0, s);
1829 }
1830 EXPORT_SYMBOL_GPL(hisi_sas_init_mem);
1831
1832 int hisi_sas_alloc(struct hisi_hba *hisi_hba, struct Scsi_Host *shost)
1833 {
1834 struct device *dev = hisi_hba->dev;
1835 int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
1836
1837 spin_lock_init(&hisi_hba->lock);
1838 for (i = 0; i < hisi_hba->n_phy; i++) {
1839 hisi_sas_phy_init(hisi_hba, i);
1840 hisi_hba->port[i].port_attached = 0;
1841 hisi_hba->port[i].id = -1;
1842 }
1843
1844 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1845 hisi_hba->devices[i].dev_type = SAS_PHY_UNUSED;
1846 hisi_hba->devices[i].device_id = i;
1847 hisi_hba->devices[i].dev_status = HISI_SAS_DEV_NORMAL;
1848 }
1849
1850 for (i = 0; i < hisi_hba->queue_count; i++) {
1851 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
1852 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
1853
1854 /* Completion queue structure */
1855 cq->id = i;
1856 cq->hisi_hba = hisi_hba;
1857
1858 /* Delivery queue structure */
1859 spin_lock_init(&dq->lock);
1860 dq->id = i;
1861 dq->hisi_hba = hisi_hba;
1862
1863 /* Delivery queue */
1864 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
1865 hisi_hba->cmd_hdr[i] = dma_alloc_coherent(dev, s,
1866 &hisi_hba->cmd_hdr_dma[i], GFP_KERNEL);
1867 if (!hisi_hba->cmd_hdr[i])
1868 goto err_out;
1869
1870 /* Completion queue */
1871 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
1872 hisi_hba->complete_hdr[i] = dma_alloc_coherent(dev, s,
1873 &hisi_hba->complete_hdr_dma[i], GFP_KERNEL);
1874 if (!hisi_hba->complete_hdr[i])
1875 goto err_out;
1876 }
1877
1878 s = sizeof(struct hisi_sas_slot_buf_table);
1879 hisi_hba->buffer_pool = dma_pool_create("dma_buffer", dev, s, 16, 0);
1880 if (!hisi_hba->buffer_pool)
1881 goto err_out;
1882
1883 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
1884 hisi_hba->itct = dma_alloc_coherent(dev, s, &hisi_hba->itct_dma,
1885 GFP_KERNEL);
1886 if (!hisi_hba->itct)
1887 goto err_out;
1888
1889 memset(hisi_hba->itct, 0, s);
1890
1891 hisi_hba->slot_info = devm_kcalloc(dev, max_command_entries,
1892 sizeof(struct hisi_sas_slot),
1893 GFP_KERNEL);
1894 if (!hisi_hba->slot_info)
1895 goto err_out;
1896
1897 s = max_command_entries * sizeof(struct hisi_sas_iost);
1898 hisi_hba->iost = dma_alloc_coherent(dev, s, &hisi_hba->iost_dma,
1899 GFP_KERNEL);
1900 if (!hisi_hba->iost)
1901 goto err_out;
1902
1903 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
1904 hisi_hba->breakpoint = dma_alloc_coherent(dev, s,
1905 &hisi_hba->breakpoint_dma, GFP_KERNEL);
1906 if (!hisi_hba->breakpoint)
1907 goto err_out;
1908
1909 hisi_hba->slot_index_count = max_command_entries;
1910 s = hisi_hba->slot_index_count / BITS_PER_BYTE;
1911 hisi_hba->slot_index_tags = devm_kzalloc(dev, s, GFP_KERNEL);
1912 if (!hisi_hba->slot_index_tags)
1913 goto err_out;
1914
1915 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
1916 hisi_hba->initial_fis = dma_alloc_coherent(dev, s,
1917 &hisi_hba->initial_fis_dma, GFP_KERNEL);
1918 if (!hisi_hba->initial_fis)
1919 goto err_out;
1920
1921 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
1922 hisi_hba->sata_breakpoint = dma_alloc_coherent(dev, s,
1923 &hisi_hba->sata_breakpoint_dma, GFP_KERNEL);
1924 if (!hisi_hba->sata_breakpoint)
1925 goto err_out;
1926 hisi_sas_init_mem(hisi_hba);
1927
1928 hisi_sas_slot_index_init(hisi_hba);
1929
1930 hisi_hba->wq = create_singlethread_workqueue(dev_name(dev));
1931 if (!hisi_hba->wq) {
1932 dev_err(dev, "sas_alloc: failed to create workqueue\n");
1933 goto err_out;
1934 }
1935
1936 return 0;
1937 err_out:
1938 return -ENOMEM;
1939 }
1940 EXPORT_SYMBOL_GPL(hisi_sas_alloc);
1941
1942 void hisi_sas_free(struct hisi_hba *hisi_hba)
1943 {
1944 struct device *dev = hisi_hba->dev;
1945 int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
1946
1947 for (i = 0; i < hisi_hba->queue_count; i++) {
1948 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
1949 if (hisi_hba->cmd_hdr[i])
1950 dma_free_coherent(dev, s,
1951 hisi_hba->cmd_hdr[i],
1952 hisi_hba->cmd_hdr_dma[i]);
1953
1954 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
1955 if (hisi_hba->complete_hdr[i])
1956 dma_free_coherent(dev, s,
1957 hisi_hba->complete_hdr[i],
1958 hisi_hba->complete_hdr_dma[i]);
1959 }
1960
1961 dma_pool_destroy(hisi_hba->buffer_pool);
1962
1963 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
1964 if (hisi_hba->itct)
1965 dma_free_coherent(dev, s,
1966 hisi_hba->itct, hisi_hba->itct_dma);
1967
1968 s = max_command_entries * sizeof(struct hisi_sas_iost);
1969 if (hisi_hba->iost)
1970 dma_free_coherent(dev, s,
1971 hisi_hba->iost, hisi_hba->iost_dma);
1972
1973 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
1974 if (hisi_hba->breakpoint)
1975 dma_free_coherent(dev, s,
1976 hisi_hba->breakpoint,
1977 hisi_hba->breakpoint_dma);
1978
1979
1980 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
1981 if (hisi_hba->initial_fis)
1982 dma_free_coherent(dev, s,
1983 hisi_hba->initial_fis,
1984 hisi_hba->initial_fis_dma);
1985
1986 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
1987 if (hisi_hba->sata_breakpoint)
1988 dma_free_coherent(dev, s,
1989 hisi_hba->sata_breakpoint,
1990 hisi_hba->sata_breakpoint_dma);
1991
1992 if (hisi_hba->wq)
1993 destroy_workqueue(hisi_hba->wq);
1994 }
1995 EXPORT_SYMBOL_GPL(hisi_sas_free);
1996
1997 void hisi_sas_rst_work_handler(struct work_struct *work)
1998 {
1999 struct hisi_hba *hisi_hba =
2000 container_of(work, struct hisi_hba, rst_work);
2001
2002 hisi_sas_controller_reset(hisi_hba);
2003 }
2004 EXPORT_SYMBOL_GPL(hisi_sas_rst_work_handler);
2005
2006 void hisi_sas_sync_rst_work_handler(struct work_struct *work)
2007 {
2008 struct hisi_sas_rst *rst =
2009 container_of(work, struct hisi_sas_rst, work);
2010
2011 if (!hisi_sas_controller_reset(rst->hisi_hba))
2012 rst->done = true;
2013 complete(rst->completion);
2014 }
2015 EXPORT_SYMBOL_GPL(hisi_sas_sync_rst_work_handler);
2016
2017 int hisi_sas_get_fw_info(struct hisi_hba *hisi_hba)
2018 {
2019 struct device *dev = hisi_hba->dev;
2020 struct platform_device *pdev = hisi_hba->platform_dev;
2021 struct device_node *np = pdev ? pdev->dev.of_node : NULL;
2022 struct clk *refclk;
2023
2024 if (device_property_read_u8_array(dev, "sas-addr", hisi_hba->sas_addr,
2025 SAS_ADDR_SIZE)) {
2026 dev_err(dev, "could not get property sas-addr\n");
2027 return -ENOENT;
2028 }
2029
2030 if (np) {
2031 /*
2032 * These properties are only required for platform device-based
2033 * controller with DT firmware.
2034 */
2035 hisi_hba->ctrl = syscon_regmap_lookup_by_phandle(np,
2036 "hisilicon,sas-syscon");
2037 if (IS_ERR(hisi_hba->ctrl)) {
2038 dev_err(dev, "could not get syscon\n");
2039 return -ENOENT;
2040 }
2041
2042 if (device_property_read_u32(dev, "ctrl-reset-reg",
2043 &hisi_hba->ctrl_reset_reg)) {
2044 dev_err(dev,
2045 "could not get property ctrl-reset-reg\n");
2046 return -ENOENT;
2047 }
2048
2049 if (device_property_read_u32(dev, "ctrl-reset-sts-reg",
2050 &hisi_hba->ctrl_reset_sts_reg)) {
2051 dev_err(dev,
2052 "could not get property ctrl-reset-sts-reg\n");
2053 return -ENOENT;
2054 }
2055
2056 if (device_property_read_u32(dev, "ctrl-clock-ena-reg",
2057 &hisi_hba->ctrl_clock_ena_reg)) {
2058 dev_err(dev,
2059 "could not get property ctrl-clock-ena-reg\n");
2060 return -ENOENT;
2061 }
2062 }
2063
2064 refclk = devm_clk_get(dev, NULL);
2065 if (IS_ERR(refclk))
2066 dev_dbg(dev, "no ref clk property\n");
2067 else
2068 hisi_hba->refclk_frequency_mhz = clk_get_rate(refclk) / 1000000;
2069
2070 if (device_property_read_u32(dev, "phy-count", &hisi_hba->n_phy)) {
2071 dev_err(dev, "could not get property phy-count\n");
2072 return -ENOENT;
2073 }
2074
2075 if (device_property_read_u32(dev, "queue-count",
2076 &hisi_hba->queue_count)) {
2077 dev_err(dev, "could not get property queue-count\n");
2078 return -ENOENT;
2079 }
2080
2081 return 0;
2082 }
2083 EXPORT_SYMBOL_GPL(hisi_sas_get_fw_info);
2084
2085 static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev,
2086 const struct hisi_sas_hw *hw)
2087 {
2088 struct resource *res;
2089 struct Scsi_Host *shost;
2090 struct hisi_hba *hisi_hba;
2091 struct device *dev = &pdev->dev;
2092
2093 shost = scsi_host_alloc(hisi_sas_sht, sizeof(*hisi_hba));
2094 if (!shost) {
2095 dev_err(dev, "scsi host alloc failed\n");
2096 return NULL;
2097 }
2098 hisi_hba = shost_priv(shost);
2099
2100 INIT_WORK(&hisi_hba->rst_work, hisi_sas_rst_work_handler);
2101 hisi_hba->hw = hw;
2102 hisi_hba->dev = dev;
2103 hisi_hba->platform_dev = pdev;
2104 hisi_hba->shost = shost;
2105 SHOST_TO_SAS_HA(shost) = &hisi_hba->sha;
2106
2107 timer_setup(&hisi_hba->timer, NULL, 0);
2108
2109 if (hisi_sas_get_fw_info(hisi_hba) < 0)
2110 goto err_out;
2111
2112 if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)) &&
2113 dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32))) {
2114 dev_err(dev, "No usable DMA addressing method\n");
2115 goto err_out;
2116 }
2117
2118 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2119 hisi_hba->regs = devm_ioremap_resource(dev, res);
2120 if (IS_ERR(hisi_hba->regs))
2121 goto err_out;
2122
2123 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2124 if (res) {
2125 hisi_hba->sgpio_regs = devm_ioremap_resource(dev, res);
2126 if (IS_ERR(hisi_hba->sgpio_regs))
2127 goto err_out;
2128 }
2129
2130 if (hisi_sas_alloc(hisi_hba, shost)) {
2131 hisi_sas_free(hisi_hba);
2132 goto err_out;
2133 }
2134
2135 return shost;
2136 err_out:
2137 scsi_host_put(shost);
2138 dev_err(dev, "shost alloc failed\n");
2139 return NULL;
2140 }
2141
2142 void hisi_sas_init_add(struct hisi_hba *hisi_hba)
2143 {
2144 int i;
2145
2146 for (i = 0; i < hisi_hba->n_phy; i++)
2147 memcpy(&hisi_hba->phy[i].dev_sas_addr,
2148 hisi_hba->sas_addr,
2149 SAS_ADDR_SIZE);
2150 }
2151 EXPORT_SYMBOL_GPL(hisi_sas_init_add);
2152
2153 int hisi_sas_probe(struct platform_device *pdev,
2154 const struct hisi_sas_hw *hw)
2155 {
2156 struct Scsi_Host *shost;
2157 struct hisi_hba *hisi_hba;
2158 struct device *dev = &pdev->dev;
2159 struct asd_sas_phy **arr_phy;
2160 struct asd_sas_port **arr_port;
2161 struct sas_ha_struct *sha;
2162 int rc, phy_nr, port_nr, i;
2163
2164 shost = hisi_sas_shost_alloc(pdev, hw);
2165 if (!shost)
2166 return -ENOMEM;
2167
2168 sha = SHOST_TO_SAS_HA(shost);
2169 hisi_hba = shost_priv(shost);
2170 platform_set_drvdata(pdev, sha);
2171
2172 phy_nr = port_nr = hisi_hba->n_phy;
2173
2174 arr_phy = devm_kcalloc(dev, phy_nr, sizeof(void *), GFP_KERNEL);
2175 arr_port = devm_kcalloc(dev, port_nr, sizeof(void *), GFP_KERNEL);
2176 if (!arr_phy || !arr_port) {
2177 rc = -ENOMEM;
2178 goto err_out_ha;
2179 }
2180
2181 sha->sas_phy = arr_phy;
2182 sha->sas_port = arr_port;
2183 sha->lldd_ha = hisi_hba;
2184
2185 shost->transportt = hisi_sas_stt;
2186 shost->max_id = HISI_SAS_MAX_DEVICES;
2187 shost->max_lun = ~0;
2188 shost->max_channel = 1;
2189 shost->max_cmd_len = 16;
2190 shost->sg_tablesize = min_t(u16, SG_ALL, HISI_SAS_SGE_PAGE_CNT);
2191 shost->can_queue = hisi_hba->hw->max_command_entries;
2192 shost->cmd_per_lun = hisi_hba->hw->max_command_entries;
2193
2194 sha->sas_ha_name = DRV_NAME;
2195 sha->dev = hisi_hba->dev;
2196 sha->lldd_module = THIS_MODULE;
2197 sha->sas_addr = &hisi_hba->sas_addr[0];
2198 sha->num_phys = hisi_hba->n_phy;
2199 sha->core.shost = hisi_hba->shost;
2200
2201 for (i = 0; i < hisi_hba->n_phy; i++) {
2202 sha->sas_phy[i] = &hisi_hba->phy[i].sas_phy;
2203 sha->sas_port[i] = &hisi_hba->port[i].sas_port;
2204 }
2205
2206 hisi_sas_init_add(hisi_hba);
2207
2208 rc = scsi_add_host(shost, &pdev->dev);
2209 if (rc)
2210 goto err_out_ha;
2211
2212 rc = sas_register_ha(sha);
2213 if (rc)
2214 goto err_out_register_ha;
2215
2216 rc = hisi_hba->hw->hw_init(hisi_hba);
2217 if (rc)
2218 goto err_out_register_ha;
2219
2220 scsi_scan_host(shost);
2221
2222 return 0;
2223
2224 err_out_register_ha:
2225 scsi_remove_host(shost);
2226 err_out_ha:
2227 hisi_sas_free(hisi_hba);
2228 scsi_host_put(shost);
2229 return rc;
2230 }
2231 EXPORT_SYMBOL_GPL(hisi_sas_probe);
2232
2233 int hisi_sas_remove(struct platform_device *pdev)
2234 {
2235 struct sas_ha_struct *sha = platform_get_drvdata(pdev);
2236 struct hisi_hba *hisi_hba = sha->lldd_ha;
2237 struct Scsi_Host *shost = sha->core.shost;
2238
2239 if (timer_pending(&hisi_hba->timer))
2240 del_timer(&hisi_hba->timer);
2241
2242 sas_unregister_ha(sha);
2243 sas_remove_host(sha->core.shost);
2244
2245 hisi_sas_free(hisi_hba);
2246 scsi_host_put(shost);
2247 return 0;
2248 }
2249 EXPORT_SYMBOL_GPL(hisi_sas_remove);
2250
2251 static __init int hisi_sas_init(void)
2252 {
2253 hisi_sas_stt = sas_domain_attach_transport(&hisi_sas_transport_ops);
2254 if (!hisi_sas_stt)
2255 return -ENOMEM;
2256
2257 return 0;
2258 }
2259
2260 static __exit void hisi_sas_exit(void)
2261 {
2262 sas_release_transport(hisi_sas_stt);
2263 }
2264
2265 module_init(hisi_sas_init);
2266 module_exit(hisi_sas_exit);
2267
2268 MODULE_LICENSE("GPL");
2269 MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
2270 MODULE_DESCRIPTION("HISILICON SAS controller driver");
2271 MODULE_ALIAS("platform:" DRV_NAME);