1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include <linux/delay.h>
22#include <linux/firmware.h>
23#include <linux/pci.h>
24#include <linux/kmod.h>
25
26#include <linux/mic_common.h>
27#include <linux/mic_bus.h>
28#include "../common/mic_dev.h"
29#include "mic_device.h"
30#include "mic_smpt.h"
31#include "mic_virtio.h"
32
33static inline struct mic_device *scdev_to_mdev(struct scif_hw_dev *scdev)
34{
35 return dev_get_drvdata(scdev->dev.parent);
36}
37
38static void *__mic_dma_alloc(struct device *dev, size_t size,
39 dma_addr_t *dma_handle, gfp_t gfp,
40 struct dma_attrs *attrs)
41{
42 struct scif_hw_dev *scdev = dev_get_drvdata(dev);
43 struct mic_device *mdev = scdev_to_mdev(scdev);
44 dma_addr_t tmp;
45 void *va = kmalloc(size, gfp);
46
47 if (va) {
48 tmp = mic_map_single(mdev, va, size);
49 if (dma_mapping_error(dev, tmp)) {
50 kfree(va);
51 va = NULL;
52 } else {
53 *dma_handle = tmp;
54 }
55 }
56 return va;
57}
58
59static void __mic_dma_free(struct device *dev, size_t size, void *vaddr,
60 dma_addr_t dma_handle, struct dma_attrs *attrs)
61{
62 struct scif_hw_dev *scdev = dev_get_drvdata(dev);
63 struct mic_device *mdev = scdev_to_mdev(scdev);
64
65 mic_unmap_single(mdev, dma_handle, size);
66 kfree(vaddr);
67}
68
69static dma_addr_t
70__mic_dma_map_page(struct device *dev, struct page *page, unsigned long offset,
71 size_t size, enum dma_data_direction dir,
72 struct dma_attrs *attrs)
73{
74 void *va = phys_to_virt(page_to_phys(page)) + offset;
75 struct scif_hw_dev *scdev = dev_get_drvdata(dev);
76 struct mic_device *mdev = scdev_to_mdev(scdev);
77
78 return mic_map_single(mdev, va, size);
79}
80
81static void
82__mic_dma_unmap_page(struct device *dev, dma_addr_t dma_addr,
83 size_t size, enum dma_data_direction dir,
84 struct dma_attrs *attrs)
85{
86 struct scif_hw_dev *scdev = dev_get_drvdata(dev);
87 struct mic_device *mdev = scdev_to_mdev(scdev);
88
89 mic_unmap_single(mdev, dma_addr, size);
90}
91
92static int __mic_dma_map_sg(struct device *dev, struct scatterlist *sg,
93 int nents, enum dma_data_direction dir,
94 struct dma_attrs *attrs)
95{
96 struct scif_hw_dev *scdev = dev_get_drvdata(dev);
97 struct mic_device *mdev = scdev_to_mdev(scdev);
98 struct scatterlist *s;
99 int i, j, ret;
100 dma_addr_t da;
101
102 ret = dma_map_sg(mdev->sdev->parent, sg, nents, dir);
103 if (ret <= 0)
104 return 0;
105
106 for_each_sg(sg, s, nents, i) {
107 da = mic_map(mdev, sg_dma_address(s) + s->offset, s->length);
108 if (!da)
109 goto err;
110 sg_dma_address(s) = da;
111 }
112 return nents;
113err:
114 for_each_sg(sg, s, i, j) {
115 mic_unmap(mdev, sg_dma_address(s), s->length);
116 sg_dma_address(s) = mic_to_dma_addr(mdev, sg_dma_address(s));
117 }
118 dma_unmap_sg(mdev->sdev->parent, sg, nents, dir);
119 return 0;
120}
121
122static void __mic_dma_unmap_sg(struct device *dev,
123 struct scatterlist *sg, int nents,
124 enum dma_data_direction dir,
125 struct dma_attrs *attrs)
126{
127 struct scif_hw_dev *scdev = dev_get_drvdata(dev);
128 struct mic_device *mdev = scdev_to_mdev(scdev);
129 struct scatterlist *s;
130 dma_addr_t da;
131 int i;
132
133 for_each_sg(sg, s, nents, i) {
134 da = mic_to_dma_addr(mdev, sg_dma_address(s));
135 mic_unmap(mdev, sg_dma_address(s), s->length);
136 sg_dma_address(s) = da;
137 }
138 dma_unmap_sg(mdev->sdev->parent, sg, nents, dir);
139}
140
141static struct dma_map_ops __mic_dma_ops = {
142 .alloc = __mic_dma_alloc,
143 .free = __mic_dma_free,
144 .map_page = __mic_dma_map_page,
145 .unmap_page = __mic_dma_unmap_page,
146 .map_sg = __mic_dma_map_sg,
147 .unmap_sg = __mic_dma_unmap_sg,
148};
149
150static struct mic_irq *
151___mic_request_irq(struct scif_hw_dev *scdev,
152 irqreturn_t (*func)(int irq, void *data),
153 const char *name,
154 void *data, int db)
155{
156 struct mic_device *mdev = scdev_to_mdev(scdev);
157
158 return mic_request_threaded_irq(mdev, func, NULL, name, data,
159 db, MIC_INTR_DB);
160}
161
162static void
163___mic_free_irq(struct scif_hw_dev *scdev,
164 struct mic_irq *cookie, void *data)
165{
166 struct mic_device *mdev = scdev_to_mdev(scdev);
167
168 return mic_free_irq(mdev, cookie, data);
169}
170
171static void ___mic_ack_interrupt(struct scif_hw_dev *scdev, int num)
172{
173 struct mic_device *mdev = scdev_to_mdev(scdev);
174
175 mdev->ops->intr_workarounds(mdev);
176}
177
178static int ___mic_next_db(struct scif_hw_dev *scdev)
179{
180 struct mic_device *mdev = scdev_to_mdev(scdev);
181
182 return mic_next_db(mdev);
183}
184
185static void ___mic_send_intr(struct scif_hw_dev *scdev, int db)
186{
187 struct mic_device *mdev = scdev_to_mdev(scdev);
188
189 mdev->ops->send_intr(mdev, db);
190}
191
192static void __iomem *___mic_ioremap(struct scif_hw_dev *scdev,
193 phys_addr_t pa, size_t len)
194{
195 struct mic_device *mdev = scdev_to_mdev(scdev);
196
197 return mdev->aper.va + pa;
198}
199
200static void ___mic_iounmap(struct scif_hw_dev *scdev, void __iomem *va)
201{
202
203}
204
205static struct scif_hw_ops scif_hw_ops = {
206 .request_irq = ___mic_request_irq,
207 .free_irq = ___mic_free_irq,
208 .ack_interrupt = ___mic_ack_interrupt,
209 .next_db = ___mic_next_db,
210 .send_intr = ___mic_send_intr,
211 .ioremap = ___mic_ioremap,
212 .iounmap = ___mic_iounmap,
213};
214
215static inline struct mic_device *mbdev_to_mdev(struct mbus_device *mbdev)
216{
217 return dev_get_drvdata(mbdev->dev.parent);
218}
219
220static dma_addr_t
221mic_dma_map_page(struct device *dev, struct page *page,
222 unsigned long offset, size_t size, enum dma_data_direction dir,
223 struct dma_attrs *attrs)
224{
225 void *va = phys_to_virt(page_to_phys(page)) + offset;
226 struct mic_device *mdev = dev_get_drvdata(dev->parent);
227
228 return mic_map_single(mdev, va, size);
229}
230
231static void
232mic_dma_unmap_page(struct device *dev, dma_addr_t dma_addr,
233 size_t size, enum dma_data_direction dir,
234 struct dma_attrs *attrs)
235{
236 struct mic_device *mdev = dev_get_drvdata(dev->parent);
237 mic_unmap_single(mdev, dma_addr, size);
238}
239
240static struct dma_map_ops mic_dma_ops = {
241 .map_page = mic_dma_map_page,
242 .unmap_page = mic_dma_unmap_page,
243};
244
245static struct mic_irq *
246_mic_request_threaded_irq(struct mbus_device *mbdev,
247 irq_handler_t handler, irq_handler_t thread_fn,
248 const char *name, void *data, int intr_src)
249{
250 return mic_request_threaded_irq(mbdev_to_mdev(mbdev), handler,
251 thread_fn, name, data,
252 intr_src, MIC_INTR_DMA);
253}
254
255static void _mic_free_irq(struct mbus_device *mbdev,
256 struct mic_irq *cookie, void *data)
257{
258 return mic_free_irq(mbdev_to_mdev(mbdev), cookie, data);
259}
260
261static void _mic_ack_interrupt(struct mbus_device *mbdev, int num)
262{
263 struct mic_device *mdev = mbdev_to_mdev(mbdev);
264 mdev->ops->intr_workarounds(mdev);
265}
266
267static struct mbus_hw_ops mbus_hw_ops = {
268 .request_threaded_irq = _mic_request_threaded_irq,
269 .free_irq = _mic_free_irq,
270 .ack_interrupt = _mic_ack_interrupt,
271};
272
273
274
275
276
277static void mic_reset(struct mic_device *mdev)
278{
279 int i;
280
281#define MIC_RESET_TO (45)
282
283 reinit_completion(&mdev->reset_wait);
284 mdev->ops->reset_fw_ready(mdev);
285 mdev->ops->reset(mdev);
286
287 for (i = 0; i < MIC_RESET_TO; i++) {
288 if (mdev->ops->is_fw_ready(mdev))
289 goto done;
290
291
292
293
294
295 msleep(1000);
296 }
297 mic_set_state(mdev, MIC_RESET_FAILED);
298done:
299 complete_all(&mdev->reset_wait);
300}
301
302
303void mic_bootparam_init(struct mic_device *mdev)
304{
305 struct mic_bootparam *bootparam = mdev->dp;
306
307 bootparam->magic = cpu_to_le32(MIC_MAGIC);
308 bootparam->c2h_shutdown_db = mdev->shutdown_db;
309 bootparam->h2c_shutdown_db = -1;
310 bootparam->h2c_config_db = -1;
311 bootparam->shutdown_status = 0;
312 bootparam->shutdown_card = 0;
313
314 bootparam->tot_nodes = atomic_read(&g_num_mics) + 1;
315 bootparam->node_id = mdev->id + 1;
316 bootparam->scif_host_dma_addr = 0x0;
317 bootparam->scif_card_dma_addr = 0x0;
318 bootparam->c2h_scif_db = -1;
319 bootparam->h2c_scif_db = -1;
320}
321
322
323
324
325
326
327
328static int mic_request_dma_chans(struct mic_device *mdev)
329{
330 dma_cap_mask_t mask;
331 struct dma_chan *chan;
332
333 request_module("mic_x100_dma");
334 dma_cap_zero(mask);
335 dma_cap_set(DMA_MEMCPY, mask);
336
337 do {
338 chan = dma_request_channel(mask, mdev->ops->dma_filter,
339 mdev->sdev->parent);
340 if (chan) {
341 mdev->dma_ch[mdev->num_dma_ch++] = chan;
342 if (mdev->num_dma_ch >= MIC_MAX_DMA_CHAN)
343 break;
344 }
345 } while (chan);
346 dev_info(mdev->sdev->parent, "DMA channels # %d\n", mdev->num_dma_ch);
347 return mdev->num_dma_ch;
348}
349
350
351
352
353
354
355
356static void mic_free_dma_chans(struct mic_device *mdev)
357{
358 int i = 0;
359
360 for (i = 0; i < mdev->num_dma_ch; i++) {
361 dma_release_channel(mdev->dma_ch[i]);
362 mdev->dma_ch[i] = NULL;
363 }
364 mdev->num_dma_ch = 0;
365}
366
367
368
369
370
371
372
373
374
375int mic_start(struct mic_device *mdev, const char *buf)
376{
377 int rc;
378 mutex_lock(&mdev->mic_mutex);
379 mic_bootparam_init(mdev);
380retry:
381 if (MIC_OFFLINE != mdev->state) {
382 rc = -EINVAL;
383 goto unlock_ret;
384 }
385 if (!mdev->ops->is_fw_ready(mdev)) {
386 mic_reset(mdev);
387
388
389
390
391 goto retry;
392 }
393 mdev->dma_mbdev = mbus_register_device(mdev->sdev->parent,
394 MBUS_DEV_DMA_HOST, &mic_dma_ops,
395 &mbus_hw_ops, mdev->mmio.va);
396 if (IS_ERR(mdev->dma_mbdev)) {
397 rc = PTR_ERR(mdev->dma_mbdev);
398 goto unlock_ret;
399 }
400 if (!mic_request_dma_chans(mdev)) {
401 rc = -ENODEV;
402 goto dma_remove;
403 }
404 mdev->scdev = scif_register_device(mdev->sdev->parent, MIC_SCIF_DEV,
405 &__mic_dma_ops, &scif_hw_ops,
406 mdev->id + 1, 0, &mdev->mmio,
407 &mdev->aper, mdev->dp, NULL,
408 mdev->dma_ch, mdev->num_dma_ch);
409 if (IS_ERR(mdev->scdev)) {
410 rc = PTR_ERR(mdev->scdev);
411 goto dma_free;
412 }
413 rc = mdev->ops->load_mic_fw(mdev, buf);
414 if (rc)
415 goto scif_remove;
416 mic_smpt_restore(mdev);
417 mic_intr_restore(mdev);
418 mdev->intr_ops->enable_interrupts(mdev);
419 mdev->ops->write_spad(mdev, MIC_DPLO_SPAD, mdev->dp_dma_addr);
420 mdev->ops->write_spad(mdev, MIC_DPHI_SPAD, mdev->dp_dma_addr >> 32);
421 mdev->ops->send_firmware_intr(mdev);
422 mic_set_state(mdev, MIC_ONLINE);
423 goto unlock_ret;
424scif_remove:
425 scif_unregister_device(mdev->scdev);
426dma_free:
427 mic_free_dma_chans(mdev);
428dma_remove:
429 mbus_unregister_device(mdev->dma_mbdev);
430unlock_ret:
431 mutex_unlock(&mdev->mic_mutex);
432 return rc;
433}
434
435
436
437
438
439
440
441
442void mic_stop(struct mic_device *mdev, bool force)
443{
444 mutex_lock(&mdev->mic_mutex);
445 if (MIC_OFFLINE != mdev->state || force) {
446 scif_unregister_device(mdev->scdev);
447 mic_virtio_reset_devices(mdev);
448 mic_free_dma_chans(mdev);
449 mbus_unregister_device(mdev->dma_mbdev);
450 mic_bootparam_init(mdev);
451 mic_reset(mdev);
452 if (MIC_RESET_FAILED == mdev->state)
453 goto unlock;
454 mic_set_shutdown_status(mdev, MIC_NOP);
455 if (MIC_SUSPENDED != mdev->state)
456 mic_set_state(mdev, MIC_OFFLINE);
457 }
458unlock:
459 mutex_unlock(&mdev->mic_mutex);
460}
461
462
463
464
465
466
467
468void mic_shutdown(struct mic_device *mdev)
469{
470 struct mic_bootparam *bootparam = mdev->dp;
471 s8 db = bootparam->h2c_shutdown_db;
472
473 mutex_lock(&mdev->mic_mutex);
474 if (MIC_ONLINE == mdev->state && db != -1) {
475 bootparam->shutdown_card = 1;
476 mdev->ops->send_intr(mdev, db);
477 mic_set_state(mdev, MIC_SHUTTING_DOWN);
478 }
479 mutex_unlock(&mdev->mic_mutex);
480}
481
482
483
484
485
486
487
488
489void mic_shutdown_work(struct work_struct *work)
490{
491 struct mic_device *mdev = container_of(work, struct mic_device,
492 shutdown_work);
493 struct mic_bootparam *bootparam = mdev->dp;
494
495 mutex_lock(&mdev->mic_mutex);
496 mic_set_shutdown_status(mdev, bootparam->shutdown_status);
497 bootparam->shutdown_status = 0;
498
499
500
501
502
503
504 if (MIC_SHUTTING_DOWN != mdev->state &&
505 MIC_SUSPENDED != mdev->state)
506 mic_set_state(mdev, MIC_SHUTTING_DOWN);
507 mutex_unlock(&mdev->mic_mutex);
508}
509
510
511
512
513
514
515
516void mic_reset_trigger_work(struct work_struct *work)
517{
518 struct mic_device *mdev = container_of(work, struct mic_device,
519 reset_trigger_work);
520
521 mic_stop(mdev, false);
522}
523
524
525
526
527
528
529
530
531void mic_complete_resume(struct mic_device *mdev)
532{
533 if (mdev->state != MIC_SUSPENDED) {
534 dev_warn(mdev->sdev->parent, "state %d should be %d\n",
535 mdev->state, MIC_SUSPENDED);
536 return;
537 }
538
539
540 if (!mdev->ops->is_fw_ready(mdev))
541 mic_stop(mdev, true);
542
543 mutex_lock(&mdev->mic_mutex);
544 mic_set_state(mdev, MIC_OFFLINE);
545 mutex_unlock(&mdev->mic_mutex);
546}
547
548
549
550
551
552
553
554void mic_prepare_suspend(struct mic_device *mdev)
555{
556 unsigned long timeout;
557
558#define MIC_SUSPEND_TIMEOUT (60 * HZ)
559
560 mutex_lock(&mdev->mic_mutex);
561 switch (mdev->state) {
562 case MIC_OFFLINE:
563
564
565
566
567 mic_set_state(mdev, MIC_SUSPENDED);
568 mutex_unlock(&mdev->mic_mutex);
569 break;
570 case MIC_ONLINE:
571
572
573
574
575
576 mic_set_state(mdev, MIC_SUSPENDING);
577 mutex_unlock(&mdev->mic_mutex);
578 timeout = wait_for_completion_timeout(&mdev->reset_wait,
579 MIC_SUSPEND_TIMEOUT);
580
581 if (!timeout) {
582 mutex_lock(&mdev->mic_mutex);
583 mic_set_state(mdev, MIC_SUSPENDED);
584 mutex_unlock(&mdev->mic_mutex);
585 mic_stop(mdev, true);
586 }
587 break;
588 case MIC_SHUTTING_DOWN:
589
590
591
592
593 mic_set_state(mdev, MIC_SUSPENDED);
594 mutex_unlock(&mdev->mic_mutex);
595 timeout = wait_for_completion_timeout(&mdev->reset_wait,
596 MIC_SUSPEND_TIMEOUT);
597
598 if (!timeout)
599 mic_stop(mdev, true);
600 break;
601 default:
602 mutex_unlock(&mdev->mic_mutex);
603 break;
604 }
605}
606
607
608
609
610
611
612
613void mic_suspend(struct mic_device *mdev)
614{
615 struct mic_bootparam *bootparam = mdev->dp;
616 s8 db = bootparam->h2c_shutdown_db;
617
618 mutex_lock(&mdev->mic_mutex);
619 if (MIC_SUSPENDING == mdev->state && db != -1) {
620 bootparam->shutdown_card = 1;
621 mdev->ops->send_intr(mdev, db);
622 mic_set_state(mdev, MIC_SUSPENDED);
623 }
624 mutex_unlock(&mdev->mic_mutex);
625}
626