Skip to content

Commit 7146f0d

Browse files
oakadPierre Ossman
authored andcommitted
tifm_7xx1: switch from workqueue to kthread
As there's only one work item (media_switcher) to handle and it's effectively serialized with itself, I found it more convenient to use kthread instead of workqueue. This also allows for a working implementation of suspend/resume, which were totally broken in the past version. Signed-off-by: Alex Dubov <oakad@yahoo.com> Signed-off-by: Pierre Ossman <drzeus@drzeus.cx>
1 parent 6412d92 commit 7146f0d

3 files changed

Lines changed: 98 additions & 74 deletions

File tree

‎drivers/misc/tifm_7xx1.c‎

Lines changed: 89 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include <linux/tifm.h>
1313
#include <linux/dma-mapping.h>
14+
#include <linux/freezer.h>
1415

1516
#define DRIVER_NAME "tifm_7xx1"
1617
#define DRIVER_VERSION "0.7"
@@ -20,10 +21,8 @@ static void tifm_7xx1_eject(struct tifm_adapter *fm, struct tifm_dev *sock)
2021
unsigned long flags;
2122

2223
spin_lock_irqsave(&fm->lock, flags);
23-
if (!fm->inhibit_new_cards) {
24-
fm->socket_change_set |= 1 << sock->socket_id;
25-
queue_work(fm->wq, &fm->media_switcher);
26-
}
24+
fm->socket_change_set |= 1 << sock->socket_id;
25+
wake_up_all(&fm->change_set_notify);
2726
spin_unlock_irqrestore(&fm->lock, flags);
2827
}
2928

@@ -59,14 +58,10 @@ static irqreturn_t tifm_7xx1_isr(int irq, void *dev_id)
5958
}
6059
writel(irq_status, fm->addr + FM_INTERRUPT_STATUS);
6160

62-
if (!fm->inhibit_new_cards) {
63-
if (!fm->socket_change_set) {
64-
writel(TIFM_IRQ_ENABLE,
65-
fm->addr + FM_SET_INTERRUPT_ENABLE);
66-
} else {
67-
queue_work(fm->wq, &fm->media_switcher);
68-
}
69-
}
61+
if (!fm->socket_change_set)
62+
writel(TIFM_IRQ_ENABLE, fm->addr + FM_SET_INTERRUPT_ENABLE);
63+
else
64+
wake_up_all(&fm->change_set_notify);
7065

7166
spin_unlock(&fm->lock);
7267
return IRQ_HANDLED;
@@ -123,34 +118,35 @@ tifm_7xx1_sock_addr(char __iomem *base_addr, unsigned int sock_num)
123118
return base_addr + ((sock_num + 1) << 10);
124119
}
125120

126-
static void tifm_7xx1_switch_media(struct work_struct *work)
121+
static int tifm_7xx1_switch_media(void *data)
127122
{
128-
struct tifm_adapter *fm =
129-
container_of(work, struct tifm_adapter, media_switcher);
123+
struct tifm_adapter *fm = data;
130124
unsigned long flags;
131125
tifm_media_id media_id;
132126
char *card_name = "xx";
133-
int cnt;
127+
int cnt, rc;
134128
struct tifm_dev *sock;
135129
unsigned int socket_change_set;
136130

137-
if (!class_device_get(&fm->cdev))
138-
return;
139-
140131
while (1) {
132+
rc = wait_event_interruptible(fm->change_set_notify,
133+
fm->socket_change_set);
134+
if (rc == -ERESTARTSYS)
135+
try_to_freeze();
136+
141137
spin_lock_irqsave(&fm->lock, flags);
142138
socket_change_set = fm->socket_change_set;
143139
fm->socket_change_set = 0;
144140

145141
dev_dbg(fm->dev, "checking media set %x\n",
146142
socket_change_set);
147143

148-
if (fm->inhibit_new_cards)
144+
if (kthread_should_stop())
149145
socket_change_set = (1 << fm->num_sockets) - 1;
150146
spin_unlock_irqrestore(&fm->lock, flags);
151147

152148
if (!socket_change_set)
153-
break;
149+
continue;
154150

155151
spin_lock_irqsave(&fm->lock, flags);
156152
for (cnt = 0; cnt < fm->num_sockets; cnt++) {
@@ -169,7 +165,7 @@ static void tifm_7xx1_switch_media(struct work_struct *work)
169165
tifm_7xx1_sock_addr(fm->addr, cnt)
170166
+ SOCK_CONTROL);
171167
}
172-
if (fm->inhibit_new_cards)
168+
if (kthread_should_stop())
173169
continue;
174170

175171
spin_unlock_irqrestore(&fm->lock, flags);
@@ -218,7 +214,7 @@ static void tifm_7xx1_switch_media(struct work_struct *work)
218214
}
219215
}
220216

221-
if (!fm->inhibit_new_cards) {
217+
if (!kthread_should_stop()) {
222218
writel(TIFM_IRQ_FIFOMASK(socket_change_set)
223219
| TIFM_IRQ_CARDMASK(socket_change_set),
224220
fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
@@ -228,64 +224,100 @@ static void tifm_7xx1_switch_media(struct work_struct *work)
228224
writel(TIFM_IRQ_ENABLE,
229225
fm->addr + FM_SET_INTERRUPT_ENABLE);
230226
spin_unlock_irqrestore(&fm->lock, flags);
231-
break;
232227
} else {
233228
for (cnt = 0; cnt < fm->num_sockets; cnt++) {
234229
if (fm->sockets[cnt])
235230
fm->socket_change_set |= 1 << cnt;
236231
}
237232
if (!fm->socket_change_set) {
238233
spin_unlock_irqrestore(&fm->lock, flags);
239-
break;
234+
return 0;
240235
} else {
241236
spin_unlock_irqrestore(&fm->lock, flags);
242237
}
243238
}
244239
}
245-
class_device_put(&fm->cdev);
240+
return 0;
246241
}
247242

243+
#ifdef CONFIG_PM
244+
248245
static int tifm_7xx1_suspend(struct pci_dev *dev, pm_message_t state)
249246
{
250-
struct tifm_adapter *fm = pci_get_drvdata(dev);
251-
unsigned long flags;
247+
dev_dbg(&dev->dev, "suspending host\n");
252248

253-
spin_lock_irqsave(&fm->lock, flags);
254-
fm->inhibit_new_cards = 1;
255-
fm->socket_change_set = 0xf;
256-
writel(TIFM_IRQ_ENABLE, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
257-
spin_unlock_irqrestore(&fm->lock, flags);
258-
flush_workqueue(fm->wq);
259-
260-
tifm_7xx1_switch_media(&fm->media_switcher);
261-
262-
pci_set_power_state(dev, PCI_D3hot);
263-
pci_disable_device(dev);
264-
pci_save_state(dev);
249+
pci_save_state(dev);
250+
pci_enable_wake(dev, pci_choose_state(dev, state), 0);
251+
pci_disable_device(dev);
252+
pci_set_power_state(dev, pci_choose_state(dev, state));
265253
return 0;
266254
}
267255

268256
static int tifm_7xx1_resume(struct pci_dev *dev)
269257
{
270258
struct tifm_adapter *fm = pci_get_drvdata(dev);
259+
int cnt, rc;
271260
unsigned long flags;
261+
tifm_media_id new_ids[fm->num_sockets];
272262

263+
pci_set_power_state(dev, PCI_D0);
273264
pci_restore_state(dev);
274-
pci_enable_device(dev);
275-
pci_set_power_state(dev, PCI_D0);
276-
pci_set_master(dev);
265+
rc = pci_enable_device(dev);
266+
if (rc)
267+
return rc;
268+
pci_set_master(dev);
269+
270+
dev_dbg(&dev->dev, "resuming host\n");
277271

272+
for (cnt = 0; cnt < fm->num_sockets; cnt++)
273+
new_ids[cnt] = tifm_7xx1_toggle_sock_power(
274+
tifm_7xx1_sock_addr(fm->addr, cnt),
275+
fm->num_sockets == 2);
278276
spin_lock_irqsave(&fm->lock, flags);
279-
fm->inhibit_new_cards = 0;
280-
writel(TIFM_IRQ_SETALL, fm->addr + FM_INTERRUPT_STATUS);
281-
writel(TIFM_IRQ_SETALL, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
277+
fm->socket_change_set = 0;
278+
for (cnt = 0; cnt < fm->num_sockets; cnt++) {
279+
if (fm->sockets[cnt]) {
280+
if (fm->sockets[cnt]->media_id == new_ids[cnt])
281+
fm->socket_change_set |= 1 << cnt;
282+
283+
fm->sockets[cnt]->media_id = new_ids[cnt];
284+
}
285+
}
286+
282287
writel(TIFM_IRQ_ENABLE | TIFM_IRQ_SOCKMASK((1 << fm->num_sockets) - 1),
283-
fm->addr + FM_SET_INTERRUPT_ENABLE);
284-
fm->socket_change_set = 0xf;
288+
fm->addr + FM_SET_INTERRUPT_ENABLE);
289+
if (!fm->socket_change_set) {
290+
spin_unlock_irqrestore(&fm->lock, flags);
291+
return 0;
292+
} else {
293+
fm->socket_change_set = 0;
294+
spin_unlock_irqrestore(&fm->lock, flags);
295+
}
296+
297+
wait_event_timeout(fm->change_set_notify, fm->socket_change_set, HZ);
298+
299+
spin_lock_irqsave(&fm->lock, flags);
300+
writel(TIFM_IRQ_FIFOMASK(fm->socket_change_set)
301+
| TIFM_IRQ_CARDMASK(fm->socket_change_set),
302+
fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
303+
writel(TIFM_IRQ_FIFOMASK(fm->socket_change_set)
304+
| TIFM_IRQ_CARDMASK(fm->socket_change_set),
305+
fm->addr + FM_SET_INTERRUPT_ENABLE);
306+
writel(TIFM_IRQ_ENABLE,
307+
fm->addr + FM_SET_INTERRUPT_ENABLE);
308+
fm->socket_change_set = 0;
309+
285310
spin_unlock_irqrestore(&fm->lock, flags);
286311
return 0;
287312
}
288313

314+
#else
315+
316+
#define tifm_7xx1_suspend NULL
317+
#define tifm_7xx1_resume NULL
318+
319+
#endif /* CONFIG_PM */
320+
289321
static int tifm_7xx1_probe(struct pci_dev *dev,
290322
const struct pci_device_id *dev_id)
291323
{
@@ -324,7 +356,6 @@ static int tifm_7xx1_probe(struct pci_dev *dev,
324356
if (!fm->sockets)
325357
goto err_out_free;
326358

327-
INIT_WORK(&fm->media_switcher, tifm_7xx1_switch_media);
328359
fm->eject = tifm_7xx1_eject;
329360
pci_set_drvdata(dev, fm);
330361

@@ -337,16 +368,15 @@ static int tifm_7xx1_probe(struct pci_dev *dev,
337368
if (rc)
338369
goto err_out_unmap;
339370

340-
rc = tifm_add_adapter(fm);
371+
init_waitqueue_head(&fm->change_set_notify);
372+
rc = tifm_add_adapter(fm, tifm_7xx1_switch_media);
341373
if (rc)
342374
goto err_out_irq;
343375

344376
writel(TIFM_IRQ_SETALL, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
345377
writel(TIFM_IRQ_ENABLE | TIFM_IRQ_SOCKMASK((1 << fm->num_sockets) - 1),
346378
fm->addr + FM_SET_INTERRUPT_ENABLE);
347-
348-
fm->socket_change_set = 0xf;
349-
379+
wake_up_process(fm->media_switcher);
350380
return 0;
351381

352382
err_out_irq:
@@ -370,18 +400,15 @@ static void tifm_7xx1_remove(struct pci_dev *dev)
370400
struct tifm_adapter *fm = pci_get_drvdata(dev);
371401
unsigned long flags;
372402

403+
writel(TIFM_IRQ_SETALL, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
404+
mmiowb();
405+
free_irq(dev->irq, fm);
406+
373407
spin_lock_irqsave(&fm->lock, flags);
374-
fm->inhibit_new_cards = 1;
375-
fm->socket_change_set = 0xf;
376-
writel(TIFM_IRQ_ENABLE, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
408+
fm->socket_change_set = (1 << fm->num_sockets) - 1;
377409
spin_unlock_irqrestore(&fm->lock, flags);
378410

379-
flush_workqueue(fm->wq);
380-
381-
tifm_7xx1_switch_media(&fm->media_switcher);
382-
383-
writel(TIFM_IRQ_SETALL, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
384-
free_irq(dev->irq, fm);
411+
kthread_stop(fm->media_switcher);
385412

386413
tifm_remove_adapter(fm);
387414

‎drivers/misc/tifm_core.c‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,6 @@ static void tifm_free(struct class_device *cdev)
7171
struct tifm_adapter *fm = container_of(cdev, struct tifm_adapter, cdev);
7272

7373
kfree(fm->sockets);
74-
if (fm->wq)
75-
destroy_workqueue(fm->wq);
7674
kfree(fm);
7775
}
7876

@@ -101,7 +99,8 @@ void tifm_free_adapter(struct tifm_adapter *fm)
10199
}
102100
EXPORT_SYMBOL(tifm_free_adapter);
103101

104-
int tifm_add_adapter(struct tifm_adapter *fm)
102+
int tifm_add_adapter(struct tifm_adapter *fm,
103+
int (*mediathreadfn)(void *data))
105104
{
106105
int rc;
107106

@@ -113,10 +112,10 @@ int tifm_add_adapter(struct tifm_adapter *fm)
113112
spin_unlock(&tifm_adapter_lock);
114113
if (!rc) {
115114
snprintf(fm->cdev.class_id, BUS_ID_SIZE, "tifm%u", fm->id);
116-
strncpy(fm->wq_name, fm->cdev.class_id, KOBJ_NAME_LEN);
115+
fm->media_switcher = kthread_create(mediathreadfn,
116+
fm, "tifm/%u", fm->id);
117117

118-
fm->wq = create_singlethread_workqueue(fm->wq_name);
119-
if (fm->wq)
118+
if (!IS_ERR(fm->media_switcher))
120119
return class_device_add(&fm->cdev);
121120

122121
spin_lock(&tifm_adapter_lock);

‎include/linux/tifm.h‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include <linux/wait.h>
1818
#include <linux/delay.h>
1919
#include <linux/pci.h>
20-
#include <linux/scatterlist.h>
20+
#include <linux/kthread.h>
2121

2222
/* Host registers (relative to pci base address): */
2323
enum {
@@ -110,13 +110,11 @@ struct tifm_adapter {
110110
spinlock_t lock;
111111
unsigned int irq_status;
112112
unsigned int socket_change_set;
113+
wait_queue_head_t change_set_notify;
113114
unsigned int id;
114115
unsigned int num_sockets;
115116
struct tifm_dev **sockets;
116-
char wq_name[KOBJ_NAME_LEN];
117-
unsigned int inhibit_new_cards;
118-
struct workqueue_struct *wq;
119-
struct work_struct media_switcher;
117+
struct task_struct *media_switcher;
120118
struct class_device cdev;
121119
struct device *dev;
122120

@@ -126,7 +124,7 @@ struct tifm_adapter {
126124
struct tifm_adapter *tifm_alloc_adapter(void);
127125
void tifm_free_device(struct device *dev);
128126
void tifm_free_adapter(struct tifm_adapter *fm);
129-
int tifm_add_adapter(struct tifm_adapter *fm);
127+
int tifm_add_adapter(struct tifm_adapter *fm, int (*mediathreadfn)(void *data));
130128
void tifm_remove_adapter(struct tifm_adapter *fm);
131129
struct tifm_dev *tifm_alloc_device(struct tifm_adapter *fm);
132130
int tifm_register_driver(struct tifm_driver *drv);

0 commit comments

Comments
 (0)