Lines Matching +full:channel +full:- +full:phase

1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2015-2018, Intel Corporation.
6 #define pr_fmt(fmt) "kcs-bmc: " fmt
60 /* IPMI 2.0 - Table 9-4, KCS Interface Status Codes */
76 enum kcs_ipmi_phases phase; member
94 #define DEVICE_NAME "ipmi-kcs"
100 /* IPMI 2.0 - Table 9-1, KCS Interface Status Register Bits */
108 /* IPMI 2.0 - Table 9-2, KCS Interface State Bits */
116 /* IPMI 2.0 - Table 9-3, KCS Interface Control Codes */
124 kcs_bmc_update_status(priv->client.dev, KCS_STATUS_STATE_MASK, KCS_STATUS_STATE(state)); in set_state()
130 kcs_bmc_read_data(priv->client.dev); in kcs_bmc_ipmi_force_abort()
131 kcs_bmc_write_data(priv->client.dev, KCS_ZERO_DATA); in kcs_bmc_ipmi_force_abort()
133 priv->phase = KCS_PHASE_ERROR; in kcs_bmc_ipmi_force_abort()
134 priv->data_in_avail = false; in kcs_bmc_ipmi_force_abort()
135 priv->data_in_idx = 0; in kcs_bmc_ipmi_force_abort()
143 dev = priv->client.dev; in kcs_bmc_ipmi_handle_data()
145 switch (priv->phase) { in kcs_bmc_ipmi_handle_data()
147 priv->phase = KCS_PHASE_WRITE_DATA; in kcs_bmc_ipmi_handle_data()
151 if (priv->data_in_idx < KCS_MSG_BUFSIZ) { in kcs_bmc_ipmi_handle_data()
154 priv->data_in[priv->data_in_idx++] = kcs_bmc_read_data(dev); in kcs_bmc_ipmi_handle_data()
157 priv->error = KCS_LENGTH_ERROR; in kcs_bmc_ipmi_handle_data()
162 if (priv->data_in_idx < KCS_MSG_BUFSIZ) { in kcs_bmc_ipmi_handle_data()
164 priv->data_in[priv->data_in_idx++] = kcs_bmc_read_data(dev); in kcs_bmc_ipmi_handle_data()
165 priv->phase = KCS_PHASE_WRITE_DONE; in kcs_bmc_ipmi_handle_data()
166 priv->data_in_avail = true; in kcs_bmc_ipmi_handle_data()
167 wake_up_interruptible(&priv->queue); in kcs_bmc_ipmi_handle_data()
170 priv->error = KCS_LENGTH_ERROR; in kcs_bmc_ipmi_handle_data()
175 if (priv->data_out_idx == priv->data_out_len) in kcs_bmc_ipmi_handle_data()
185 if (priv->data_out_idx == priv->data_out_len) { in kcs_bmc_ipmi_handle_data()
187 priv->phase = KCS_PHASE_IDLE; in kcs_bmc_ipmi_handle_data()
191 kcs_bmc_write_data(dev, priv->data_out[priv->data_out_idx++]); in kcs_bmc_ipmi_handle_data()
197 kcs_bmc_write_data(dev, priv->error); in kcs_bmc_ipmi_handle_data()
198 priv->phase = KCS_PHASE_ABORT_ERROR2; in kcs_bmc_ipmi_handle_data()
205 priv->phase = KCS_PHASE_IDLE; in kcs_bmc_ipmi_handle_data()
219 kcs_bmc_write_data(priv->client.dev, KCS_ZERO_DATA); in kcs_bmc_ipmi_handle_cmd()
221 cmd = kcs_bmc_read_data(priv->client.dev); in kcs_bmc_ipmi_handle_cmd()
224 priv->phase = KCS_PHASE_WRITE_START; in kcs_bmc_ipmi_handle_cmd()
225 priv->error = KCS_NO_ERROR; in kcs_bmc_ipmi_handle_cmd()
226 priv->data_in_avail = false; in kcs_bmc_ipmi_handle_cmd()
227 priv->data_in_idx = 0; in kcs_bmc_ipmi_handle_cmd()
231 if (priv->phase != KCS_PHASE_WRITE_DATA) { in kcs_bmc_ipmi_handle_cmd()
236 priv->phase = KCS_PHASE_WRITE_END_CMD; in kcs_bmc_ipmi_handle_cmd()
240 if (priv->error == KCS_NO_ERROR) in kcs_bmc_ipmi_handle_cmd()
241 priv->error = KCS_ABORTED_BY_COMMAND; in kcs_bmc_ipmi_handle_cmd()
243 priv->phase = KCS_PHASE_ABORT_ERROR1; in kcs_bmc_ipmi_handle_cmd()
244 priv->data_in_avail = false; in kcs_bmc_ipmi_handle_cmd()
245 priv->data_in_idx = 0; in kcs_bmc_ipmi_handle_cmd()
250 priv->error = KCS_ILLEGAL_CONTROL_CODE; in kcs_bmc_ipmi_handle_cmd()
270 spin_lock(&priv->lock); in kcs_bmc_ipmi_event()
272 status = kcs_bmc_read_status(client->dev); in kcs_bmc_ipmi_event()
284 spin_unlock(&priv->lock); in kcs_bmc_ipmi_event()
295 return container_of(filp->private_data, struct kcs_bmc_ipmi, miscdev); in to_kcs_bmc()
302 return kcs_bmc_enable_device(priv->client.dev, &priv->client); in kcs_bmc_ipmi_open()
310 poll_wait(filp, &priv->queue, wait); in kcs_bmc_ipmi_poll()
312 spin_lock_irq(&priv->lock); in kcs_bmc_ipmi_poll()
313 if (priv->data_in_avail) in kcs_bmc_ipmi_poll()
315 spin_unlock_irq(&priv->lock); in kcs_bmc_ipmi_poll()
328 if (!(filp->f_flags & O_NONBLOCK)) in kcs_bmc_ipmi_read()
329 wait_event_interruptible(priv->queue, in kcs_bmc_ipmi_read()
330 priv->data_in_avail); in kcs_bmc_ipmi_read()
332 mutex_lock(&priv->mutex); in kcs_bmc_ipmi_read()
334 spin_lock_irq(&priv->lock); in kcs_bmc_ipmi_read()
335 data_avail = priv->data_in_avail; in kcs_bmc_ipmi_read()
337 data_len = priv->data_in_idx; in kcs_bmc_ipmi_read()
338 memcpy(priv->kbuffer, priv->data_in, data_len); in kcs_bmc_ipmi_read()
340 spin_unlock_irq(&priv->lock); in kcs_bmc_ipmi_read()
343 ret = -EAGAIN; in kcs_bmc_ipmi_read()
348 pr_err("channel=%u with too large data : %zu\n", in kcs_bmc_ipmi_read()
349 priv->client.dev->channel, data_len); in kcs_bmc_ipmi_read()
351 spin_lock_irq(&priv->lock); in kcs_bmc_ipmi_read()
353 spin_unlock_irq(&priv->lock); in kcs_bmc_ipmi_read()
355 ret = -EOVERFLOW; in kcs_bmc_ipmi_read()
359 if (copy_to_user(buf, priv->kbuffer, data_len)) { in kcs_bmc_ipmi_read()
360 ret = -EFAULT; in kcs_bmc_ipmi_read()
366 spin_lock_irq(&priv->lock); in kcs_bmc_ipmi_read()
367 if (priv->phase == KCS_PHASE_WRITE_DONE) { in kcs_bmc_ipmi_read()
368 priv->phase = KCS_PHASE_WAIT_READ; in kcs_bmc_ipmi_read()
369 priv->data_in_avail = false; in kcs_bmc_ipmi_read()
370 priv->data_in_idx = 0; in kcs_bmc_ipmi_read()
372 ret = -EAGAIN; in kcs_bmc_ipmi_read()
374 spin_unlock_irq(&priv->lock); in kcs_bmc_ipmi_read()
377 mutex_unlock(&priv->mutex); in kcs_bmc_ipmi_read()
390 return -EINVAL; in kcs_bmc_ipmi_write()
392 mutex_lock(&priv->mutex); in kcs_bmc_ipmi_write()
394 if (copy_from_user(priv->kbuffer, buf, count)) { in kcs_bmc_ipmi_write()
395 ret = -EFAULT; in kcs_bmc_ipmi_write()
399 spin_lock_irq(&priv->lock); in kcs_bmc_ipmi_write()
400 if (priv->phase == KCS_PHASE_WAIT_READ) { in kcs_bmc_ipmi_write()
401 priv->phase = KCS_PHASE_READ; in kcs_bmc_ipmi_write()
402 priv->data_out_idx = 1; in kcs_bmc_ipmi_write()
403 priv->data_out_len = count; in kcs_bmc_ipmi_write()
404 memcpy(priv->data_out, priv->kbuffer, count); in kcs_bmc_ipmi_write()
405 kcs_bmc_write_data(priv->client.dev, priv->data_out[0]); in kcs_bmc_ipmi_write()
408 ret = -EINVAL; in kcs_bmc_ipmi_write()
410 spin_unlock_irq(&priv->lock); in kcs_bmc_ipmi_write()
413 mutex_unlock(&priv->mutex); in kcs_bmc_ipmi_write()
424 spin_lock_irq(&priv->lock); in kcs_bmc_ipmi_ioctl()
428 kcs_bmc_update_status(priv->client.dev, KCS_STATUS_SMS_ATN, KCS_STATUS_SMS_ATN); in kcs_bmc_ipmi_ioctl()
432 kcs_bmc_update_status(priv->client.dev, KCS_STATUS_SMS_ATN, 0); in kcs_bmc_ipmi_ioctl()
440 ret = -EINVAL; in kcs_bmc_ipmi_ioctl()
444 spin_unlock_irq(&priv->lock); in kcs_bmc_ipmi_ioctl()
454 kcs_bmc_disable_device(priv->client.dev, &priv->client); in kcs_bmc_ipmi_release()
477 priv = devm_kzalloc(kcs_bmc->dev, sizeof(*priv), GFP_KERNEL); in kcs_bmc_ipmi_add_device()
479 return -ENOMEM; in kcs_bmc_ipmi_add_device()
481 spin_lock_init(&priv->lock); in kcs_bmc_ipmi_add_device()
482 mutex_init(&priv->mutex); in kcs_bmc_ipmi_add_device()
484 init_waitqueue_head(&priv->queue); in kcs_bmc_ipmi_add_device()
486 priv->client.dev = kcs_bmc; in kcs_bmc_ipmi_add_device()
487 priv->client.ops = &kcs_bmc_ipmi_client_ops; in kcs_bmc_ipmi_add_device()
488 priv->data_in = devm_kmalloc(kcs_bmc->dev, KCS_MSG_BUFSIZ, GFP_KERNEL); in kcs_bmc_ipmi_add_device()
489 priv->data_out = devm_kmalloc(kcs_bmc->dev, KCS_MSG_BUFSIZ, GFP_KERNEL); in kcs_bmc_ipmi_add_device()
490 priv->kbuffer = devm_kmalloc(kcs_bmc->dev, KCS_MSG_BUFSIZ, GFP_KERNEL); in kcs_bmc_ipmi_add_device()
492 priv->miscdev.minor = MISC_DYNAMIC_MINOR; in kcs_bmc_ipmi_add_device()
493 priv->miscdev.name = devm_kasprintf(kcs_bmc->dev, GFP_KERNEL, "%s%u", DEVICE_NAME, in kcs_bmc_ipmi_add_device()
494 kcs_bmc->channel); in kcs_bmc_ipmi_add_device()
495 if (!priv->data_in || !priv->data_out || !priv->kbuffer || !priv->miscdev.name) in kcs_bmc_ipmi_add_device()
496 return -EINVAL; in kcs_bmc_ipmi_add_device()
498 priv->miscdev.fops = &kcs_bmc_ipmi_fops; in kcs_bmc_ipmi_add_device()
500 rc = misc_register(&priv->miscdev); in kcs_bmc_ipmi_add_device()
502 dev_err(kcs_bmc->dev, "Unable to register device: %d\n", rc); in kcs_bmc_ipmi_add_device()
507 list_add(&priv->entry, &kcs_bmc_ipmi_instances); in kcs_bmc_ipmi_add_device()
510 dev_info(kcs_bmc->dev, "Initialised IPMI client for channel %d", kcs_bmc->channel); in kcs_bmc_ipmi_add_device()
521 if (pos->client.dev == kcs_bmc) { in kcs_bmc_ipmi_remove_device()
523 list_del(&pos->entry); in kcs_bmc_ipmi_remove_device()
530 return -ENODEV; in kcs_bmc_ipmi_remove_device()
532 misc_deregister(&priv->miscdev); in kcs_bmc_ipmi_remove_device()
533 kcs_bmc_disable_device(priv->client.dev, &priv->client); in kcs_bmc_ipmi_remove_device()
534 devm_kfree(kcs_bmc->dev, priv->kbuffer); in kcs_bmc_ipmi_remove_device()
535 devm_kfree(kcs_bmc->dev, priv->data_out); in kcs_bmc_ipmi_remove_device()
536 devm_kfree(kcs_bmc->dev, priv->data_in); in kcs_bmc_ipmi_remove_device()
537 devm_kfree(kcs_bmc->dev, priv); in kcs_bmc_ipmi_remove_device()