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