xref: /openbmc/linux/drivers/misc/mei/hdcp/mei_hdcp.c (revision 36fe4655)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright © 2019 Intel Corporation
4  *
5  * mei_hdcp.c: HDCP client driver for mei bus
6  *
7  * Author:
8  * Ramalingam C <ramalingam.c@intel.com>
9  */
10 
11 /**
12  * DOC: MEI_HDCP Client Driver
13  *
14  * The mei_hdcp driver acts as a translation layer between HDCP 2.2
15  * protocol  implementer (I915) and ME FW by translating HDCP2.2
16  * negotiation messages to ME FW command payloads and vice versa.
17  */
18 
19 #include <linux/module.h>
20 #include <linux/slab.h>
21 #include <linux/uuid.h>
22 #include <linux/mei_cl_bus.h>
23 #include <linux/component.h>
24 #include <drm/drm_connector.h>
25 #include <drm/i915_component.h>
26 #include <drm/i915_mei_hdcp_interface.h>
27 
28 #include "mei_hdcp.h"
29 
30 /**
31  * mei_hdcp_initiate_session() - Initiate a Wired HDCP2.2 Tx Session in ME FW
32  * @dev: device corresponding to the mei_cl_device
33  * @data: Intel HW specific hdcp data
34  * @ake_data: AKE_Init msg output.
35  *
36  * Return:  0 on Success, <0 on Failure.
37  */
38 static int
39 mei_hdcp_initiate_session(struct device *dev, struct hdcp_port_data *data,
40 			  struct hdcp2_ake_init *ake_data)
41 {
42 	struct wired_cmd_initiate_hdcp2_session_in session_init_in = { { 0 } };
43 	struct wired_cmd_initiate_hdcp2_session_out
44 						session_init_out = { { 0 } };
45 	struct mei_cl_device *cldev;
46 	ssize_t byte;
47 
48 	if (!dev || !data || !ake_data)
49 		return -EINVAL;
50 
51 	cldev = to_mei_cl_device(dev);
52 
53 	session_init_in.header.api_version = HDCP_API_VERSION;
54 	session_init_in.header.command_id = WIRED_INITIATE_HDCP2_SESSION;
55 	session_init_in.header.status = ME_HDCP_STATUS_SUCCESS;
56 	session_init_in.header.buffer_len =
57 				WIRED_CMD_BUF_LEN_INITIATE_HDCP2_SESSION_IN;
58 
59 	session_init_in.port.integrated_port_type = data->port_type;
60 	session_init_in.port.physical_port = (u8)data->fw_ddi;
61 	session_init_in.port.attached_transcoder = (u8)data->fw_tc;
62 	session_init_in.protocol = data->protocol;
63 
64 	byte = mei_cldev_send(cldev, (u8 *)&session_init_in,
65 			      sizeof(session_init_in));
66 	if (byte < 0) {
67 		dev_dbg(dev, "mei_cldev_send failed. %zd\n", byte);
68 		return byte;
69 	}
70 
71 	byte = mei_cldev_recv(cldev, (u8 *)&session_init_out,
72 			      sizeof(session_init_out));
73 	if (byte < 0) {
74 		dev_dbg(dev, "mei_cldev_recv failed. %zd\n", byte);
75 		return byte;
76 	}
77 
78 	if (session_init_out.header.status != ME_HDCP_STATUS_SUCCESS) {
79 		dev_dbg(dev, "ME cmd 0x%08X Failed. Status: 0x%X\n",
80 			WIRED_INITIATE_HDCP2_SESSION,
81 			session_init_out.header.status);
82 		return -EIO;
83 	}
84 
85 	ake_data->msg_id = HDCP_2_2_AKE_INIT;
86 	ake_data->tx_caps = session_init_out.tx_caps;
87 	memcpy(ake_data->r_tx, session_init_out.r_tx, HDCP_2_2_RTX_LEN);
88 
89 	return 0;
90 }
91 
92 /**
93  * mei_hdcp_verify_receiver_cert_prepare_km() - Verify the Receiver Certificate
94  * AKE_Send_Cert and prepare AKE_Stored_Km/AKE_No_Stored_Km
95  * @dev: device corresponding to the mei_cl_device
96  * @data: Intel HW specific hdcp data
97  * @rx_cert: AKE_Send_Cert for verification
98  * @km_stored: Pairing status flag output
99  * @ek_pub_km: AKE_Stored_Km/AKE_No_Stored_Km output msg
100  * @msg_sz : size of AKE_XXXXX_Km output msg
101  *
102  * Return: 0 on Success, <0 on Failure
103  */
104 static int
105 mei_hdcp_verify_receiver_cert_prepare_km(struct device *dev,
106 					 struct hdcp_port_data *data,
107 					 struct hdcp2_ake_send_cert *rx_cert,
108 					 bool *km_stored,
109 					 struct hdcp2_ake_no_stored_km
110 								*ek_pub_km,
111 					 size_t *msg_sz)
112 {
113 	struct wired_cmd_verify_receiver_cert_in verify_rxcert_in = { { 0 } };
114 	struct wired_cmd_verify_receiver_cert_out verify_rxcert_out = { { 0 } };
115 	struct mei_cl_device *cldev;
116 	ssize_t byte;
117 
118 	if (!dev || !data || !rx_cert || !km_stored || !ek_pub_km || !msg_sz)
119 		return -EINVAL;
120 
121 	cldev = to_mei_cl_device(dev);
122 
123 	verify_rxcert_in.header.api_version = HDCP_API_VERSION;
124 	verify_rxcert_in.header.command_id = WIRED_VERIFY_RECEIVER_CERT;
125 	verify_rxcert_in.header.status = ME_HDCP_STATUS_SUCCESS;
126 	verify_rxcert_in.header.buffer_len =
127 				WIRED_CMD_BUF_LEN_VERIFY_RECEIVER_CERT_IN;
128 
129 	verify_rxcert_in.port.integrated_port_type = data->port_type;
130 	verify_rxcert_in.port.physical_port = (u8)data->fw_ddi;
131 	verify_rxcert_in.port.attached_transcoder = (u8)data->fw_tc;
132 
133 	verify_rxcert_in.cert_rx = rx_cert->cert_rx;
134 	memcpy(verify_rxcert_in.r_rx, &rx_cert->r_rx, HDCP_2_2_RRX_LEN);
135 	memcpy(verify_rxcert_in.rx_caps, rx_cert->rx_caps, HDCP_2_2_RXCAPS_LEN);
136 
137 	byte = mei_cldev_send(cldev, (u8 *)&verify_rxcert_in,
138 			      sizeof(verify_rxcert_in));
139 	if (byte < 0) {
140 		dev_dbg(dev, "mei_cldev_send failed: %zd\n", byte);
141 		return byte;
142 	}
143 
144 	byte = mei_cldev_recv(cldev, (u8 *)&verify_rxcert_out,
145 			      sizeof(verify_rxcert_out));
146 	if (byte < 0) {
147 		dev_dbg(dev, "mei_cldev_recv failed: %zd\n", byte);
148 		return byte;
149 	}
150 
151 	if (verify_rxcert_out.header.status != ME_HDCP_STATUS_SUCCESS) {
152 		dev_dbg(dev, "ME cmd 0x%08X Failed. Status: 0x%X\n",
153 			WIRED_VERIFY_RECEIVER_CERT,
154 			verify_rxcert_out.header.status);
155 		return -EIO;
156 	}
157 
158 	*km_stored = !!verify_rxcert_out.km_stored;
159 	if (verify_rxcert_out.km_stored) {
160 		ek_pub_km->msg_id = HDCP_2_2_AKE_STORED_KM;
161 		*msg_sz = sizeof(struct hdcp2_ake_stored_km);
162 	} else {
163 		ek_pub_km->msg_id = HDCP_2_2_AKE_NO_STORED_KM;
164 		*msg_sz = sizeof(struct hdcp2_ake_no_stored_km);
165 	}
166 
167 	memcpy(ek_pub_km->e_kpub_km, &verify_rxcert_out.ekm_buff,
168 	       sizeof(verify_rxcert_out.ekm_buff));
169 
170 	return 0;
171 }
172 
173 /**
174  * mei_hdcp_verify_hprime() - Verify AKE_Send_H_prime at ME FW.
175  * @dev: device corresponding to the mei_cl_device
176  * @data: Intel HW specific hdcp data
177  * @rx_hprime: AKE_Send_H_prime msg for ME FW verification
178  *
179  * Return: 0 on Success, <0 on Failure
180  */
181 static int
182 mei_hdcp_verify_hprime(struct device *dev, struct hdcp_port_data *data,
183 		       struct hdcp2_ake_send_hprime *rx_hprime)
184 {
185 	struct wired_cmd_ake_send_hprime_in send_hprime_in = { { 0 } };
186 	struct wired_cmd_ake_send_hprime_out send_hprime_out = { { 0 } };
187 	struct mei_cl_device *cldev;
188 	ssize_t byte;
189 
190 	if (!dev || !data || !rx_hprime)
191 		return -EINVAL;
192 
193 	cldev = to_mei_cl_device(dev);
194 
195 	send_hprime_in.header.api_version = HDCP_API_VERSION;
196 	send_hprime_in.header.command_id = WIRED_AKE_SEND_HPRIME;
197 	send_hprime_in.header.status = ME_HDCP_STATUS_SUCCESS;
198 	send_hprime_in.header.buffer_len = WIRED_CMD_BUF_LEN_AKE_SEND_HPRIME_IN;
199 
200 	send_hprime_in.port.integrated_port_type = data->port_type;
201 	send_hprime_in.port.physical_port = (u8)data->fw_ddi;
202 	send_hprime_in.port.attached_transcoder = (u8)data->fw_tc;
203 
204 	memcpy(send_hprime_in.h_prime, rx_hprime->h_prime,
205 	       HDCP_2_2_H_PRIME_LEN);
206 
207 	byte = mei_cldev_send(cldev, (u8 *)&send_hprime_in,
208 			      sizeof(send_hprime_in));
209 	if (byte < 0) {
210 		dev_dbg(dev, "mei_cldev_send failed. %zd\n", byte);
211 		return byte;
212 	}
213 
214 	byte = mei_cldev_recv(cldev, (u8 *)&send_hprime_out,
215 			      sizeof(send_hprime_out));
216 	if (byte < 0) {
217 		dev_dbg(dev, "mei_cldev_recv failed. %zd\n", byte);
218 		return byte;
219 	}
220 
221 	if (send_hprime_out.header.status != ME_HDCP_STATUS_SUCCESS) {
222 		dev_dbg(dev, "ME cmd 0x%08X Failed. Status: 0x%X\n",
223 			WIRED_AKE_SEND_HPRIME, send_hprime_out.header.status);
224 		return -EIO;
225 	}
226 
227 	return 0;
228 }
229 
230 /**
231  * mei_hdcp_store_pairing_info() - Store pairing info received at ME FW
232  * @dev: device corresponding to the mei_cl_device
233  * @data: Intel HW specific hdcp data
234  * @pairing_info: AKE_Send_Pairing_Info msg input to ME FW
235  *
236  * Return: 0 on Success, <0 on Failure
237  */
238 static int
239 mei_hdcp_store_pairing_info(struct device *dev, struct hdcp_port_data *data,
240 			    struct hdcp2_ake_send_pairing_info *pairing_info)
241 {
242 	struct wired_cmd_ake_send_pairing_info_in pairing_info_in = { { 0 } };
243 	struct wired_cmd_ake_send_pairing_info_out pairing_info_out = { { 0 } };
244 	struct mei_cl_device *cldev;
245 	ssize_t byte;
246 
247 	if (!dev || !data || !pairing_info)
248 		return -EINVAL;
249 
250 	cldev = to_mei_cl_device(dev);
251 
252 	pairing_info_in.header.api_version = HDCP_API_VERSION;
253 	pairing_info_in.header.command_id = WIRED_AKE_SEND_PAIRING_INFO;
254 	pairing_info_in.header.status = ME_HDCP_STATUS_SUCCESS;
255 	pairing_info_in.header.buffer_len =
256 					WIRED_CMD_BUF_LEN_SEND_PAIRING_INFO_IN;
257 
258 	pairing_info_in.port.integrated_port_type = data->port_type;
259 	pairing_info_in.port.physical_port = (u8)data->fw_ddi;
260 	pairing_info_in.port.attached_transcoder = (u8)data->fw_tc;
261 
262 	memcpy(pairing_info_in.e_kh_km, pairing_info->e_kh_km,
263 	       HDCP_2_2_E_KH_KM_LEN);
264 
265 	byte = mei_cldev_send(cldev, (u8 *)&pairing_info_in,
266 			      sizeof(pairing_info_in));
267 	if (byte < 0) {
268 		dev_dbg(dev, "mei_cldev_send failed. %zd\n", byte);
269 		return byte;
270 	}
271 
272 	byte = mei_cldev_recv(cldev, (u8 *)&pairing_info_out,
273 			      sizeof(pairing_info_out));
274 	if (byte < 0) {
275 		dev_dbg(dev, "mei_cldev_recv failed. %zd\n", byte);
276 		return byte;
277 	}
278 
279 	if (pairing_info_out.header.status != ME_HDCP_STATUS_SUCCESS) {
280 		dev_dbg(dev, "ME cmd 0x%08X failed. Status: 0x%X\n",
281 			WIRED_AKE_SEND_PAIRING_INFO,
282 			pairing_info_out.header.status);
283 		return -EIO;
284 	}
285 
286 	return 0;
287 }
288 
289 /**
290  * mei_hdcp_initiate_locality_check() - Prepare LC_Init
291  * @dev: device corresponding to the mei_cl_device
292  * @data: Intel HW specific hdcp data
293  * @lc_init_data: LC_Init msg output
294  *
295  * Return: 0 on Success, <0 on Failure
296  */
297 static int
298 mei_hdcp_initiate_locality_check(struct device *dev,
299 				 struct hdcp_port_data *data,
300 				 struct hdcp2_lc_init *lc_init_data)
301 {
302 	struct wired_cmd_init_locality_check_in lc_init_in = { { 0 } };
303 	struct wired_cmd_init_locality_check_out lc_init_out = { { 0 } };
304 	struct mei_cl_device *cldev;
305 	ssize_t byte;
306 
307 	if (!dev || !data || !lc_init_data)
308 		return -EINVAL;
309 
310 	cldev = to_mei_cl_device(dev);
311 
312 	lc_init_in.header.api_version = HDCP_API_VERSION;
313 	lc_init_in.header.command_id = WIRED_INIT_LOCALITY_CHECK;
314 	lc_init_in.header.status = ME_HDCP_STATUS_SUCCESS;
315 	lc_init_in.header.buffer_len = WIRED_CMD_BUF_LEN_INIT_LOCALITY_CHECK_IN;
316 
317 	lc_init_in.port.integrated_port_type = data->port_type;
318 	lc_init_in.port.physical_port = (u8)data->fw_ddi;
319 	lc_init_in.port.attached_transcoder = (u8)data->fw_tc;
320 
321 	byte = mei_cldev_send(cldev, (u8 *)&lc_init_in, sizeof(lc_init_in));
322 	if (byte < 0) {
323 		dev_dbg(dev, "mei_cldev_send failed. %zd\n", byte);
324 		return byte;
325 	}
326 
327 	byte = mei_cldev_recv(cldev, (u8 *)&lc_init_out, sizeof(lc_init_out));
328 	if (byte < 0) {
329 		dev_dbg(dev, "mei_cldev_recv failed. %zd\n", byte);
330 		return byte;
331 	}
332 
333 	if (lc_init_out.header.status != ME_HDCP_STATUS_SUCCESS) {
334 		dev_dbg(dev, "ME cmd 0x%08X Failed. status: 0x%X\n",
335 			WIRED_INIT_LOCALITY_CHECK, lc_init_out.header.status);
336 		return -EIO;
337 	}
338 
339 	lc_init_data->msg_id = HDCP_2_2_LC_INIT;
340 	memcpy(lc_init_data->r_n, lc_init_out.r_n, HDCP_2_2_RN_LEN);
341 
342 	return 0;
343 }
344 
345 /**
346  * mei_hdcp_verify_lprime() - Verify lprime.
347  * @dev: device corresponding to the mei_cl_device
348  * @data: Intel HW specific hdcp data
349  * @rx_lprime: LC_Send_L_prime msg for ME FW verification
350  *
351  * Return: 0 on Success, <0 on Failure
352  */
353 static int
354 mei_hdcp_verify_lprime(struct device *dev, struct hdcp_port_data *data,
355 		       struct hdcp2_lc_send_lprime *rx_lprime)
356 {
357 	struct wired_cmd_validate_locality_in verify_lprime_in = { { 0 } };
358 	struct wired_cmd_validate_locality_out verify_lprime_out = { { 0 } };
359 	struct mei_cl_device *cldev;
360 	ssize_t byte;
361 
362 	if (!dev || !data || !rx_lprime)
363 		return -EINVAL;
364 
365 	cldev = to_mei_cl_device(dev);
366 
367 	verify_lprime_in.header.api_version = HDCP_API_VERSION;
368 	verify_lprime_in.header.command_id = WIRED_VALIDATE_LOCALITY;
369 	verify_lprime_in.header.status = ME_HDCP_STATUS_SUCCESS;
370 	verify_lprime_in.header.buffer_len =
371 					WIRED_CMD_BUF_LEN_VALIDATE_LOCALITY_IN;
372 
373 	verify_lprime_in.port.integrated_port_type = data->port_type;
374 	verify_lprime_in.port.physical_port = (u8)data->fw_ddi;
375 	verify_lprime_in.port.attached_transcoder = (u8)data->fw_tc;
376 
377 	memcpy(verify_lprime_in.l_prime, rx_lprime->l_prime,
378 	       HDCP_2_2_L_PRIME_LEN);
379 
380 	byte = mei_cldev_send(cldev, (u8 *)&verify_lprime_in,
381 			      sizeof(verify_lprime_in));
382 	if (byte < 0) {
383 		dev_dbg(dev, "mei_cldev_send failed. %zd\n", byte);
384 		return byte;
385 	}
386 
387 	byte = mei_cldev_recv(cldev, (u8 *)&verify_lprime_out,
388 			      sizeof(verify_lprime_out));
389 	if (byte < 0) {
390 		dev_dbg(dev, "mei_cldev_recv failed. %zd\n", byte);
391 		return byte;
392 	}
393 
394 	if (verify_lprime_out.header.status != ME_HDCP_STATUS_SUCCESS) {
395 		dev_dbg(dev, "ME cmd 0x%08X failed. status: 0x%X\n",
396 			WIRED_VALIDATE_LOCALITY,
397 			verify_lprime_out.header.status);
398 		return -EIO;
399 	}
400 
401 	return 0;
402 }
403 
404 /**
405  * mei_hdcp_get_session_key() - Prepare SKE_Send_Eks.
406  * @dev: device corresponding to the mei_cl_device
407  * @data: Intel HW specific hdcp data
408  * @ske_data: SKE_Send_Eks msg output from ME FW.
409  *
410  * Return: 0 on Success, <0 on Failure
411  */
412 static int mei_hdcp_get_session_key(struct device *dev,
413 				    struct hdcp_port_data *data,
414 				    struct hdcp2_ske_send_eks *ske_data)
415 {
416 	struct wired_cmd_get_session_key_in get_skey_in = { { 0 } };
417 	struct wired_cmd_get_session_key_out get_skey_out = { { 0 } };
418 	struct mei_cl_device *cldev;
419 	ssize_t byte;
420 
421 	if (!dev || !data || !ske_data)
422 		return -EINVAL;
423 
424 	cldev = to_mei_cl_device(dev);
425 
426 	get_skey_in.header.api_version = HDCP_API_VERSION;
427 	get_skey_in.header.command_id = WIRED_GET_SESSION_KEY;
428 	get_skey_in.header.status = ME_HDCP_STATUS_SUCCESS;
429 	get_skey_in.header.buffer_len = WIRED_CMD_BUF_LEN_GET_SESSION_KEY_IN;
430 
431 	get_skey_in.port.integrated_port_type = data->port_type;
432 	get_skey_in.port.physical_port = (u8)data->fw_ddi;
433 	get_skey_in.port.attached_transcoder = (u8)data->fw_tc;
434 
435 	byte = mei_cldev_send(cldev, (u8 *)&get_skey_in, sizeof(get_skey_in));
436 	if (byte < 0) {
437 		dev_dbg(dev, "mei_cldev_send failed. %zd\n", byte);
438 		return byte;
439 	}
440 
441 	byte = mei_cldev_recv(cldev, (u8 *)&get_skey_out, sizeof(get_skey_out));
442 
443 	if (byte < 0) {
444 		dev_dbg(dev, "mei_cldev_recv failed. %zd\n", byte);
445 		return byte;
446 	}
447 
448 	if (get_skey_out.header.status != ME_HDCP_STATUS_SUCCESS) {
449 		dev_dbg(dev, "ME cmd 0x%08X failed. status: 0x%X\n",
450 			WIRED_GET_SESSION_KEY, get_skey_out.header.status);
451 		return -EIO;
452 	}
453 
454 	ske_data->msg_id = HDCP_2_2_SKE_SEND_EKS;
455 	memcpy(ske_data->e_dkey_ks, get_skey_out.e_dkey_ks,
456 	       HDCP_2_2_E_DKEY_KS_LEN);
457 	memcpy(ske_data->riv, get_skey_out.r_iv, HDCP_2_2_RIV_LEN);
458 
459 	return 0;
460 }
461 
462 /**
463  * mei_hdcp_repeater_check_flow_prepare_ack() - Validate the Downstream topology
464  * and prepare rep_ack.
465  * @dev: device corresponding to the mei_cl_device
466  * @data: Intel HW specific hdcp data
467  * @rep_topology: Receiver ID List to be validated
468  * @rep_send_ack : repeater ack from ME FW.
469  *
470  * Return: 0 on Success, <0 on Failure
471  */
472 static int
473 mei_hdcp_repeater_check_flow_prepare_ack(struct device *dev,
474 					 struct hdcp_port_data *data,
475 					 struct hdcp2_rep_send_receiverid_list
476 							*rep_topology,
477 					 struct hdcp2_rep_send_ack
478 							*rep_send_ack)
479 {
480 	struct wired_cmd_verify_repeater_in verify_repeater_in = { { 0 } };
481 	struct wired_cmd_verify_repeater_out verify_repeater_out = { { 0 } };
482 	struct mei_cl_device *cldev;
483 	ssize_t byte;
484 
485 	if (!dev || !rep_topology || !rep_send_ack || !data)
486 		return -EINVAL;
487 
488 	cldev = to_mei_cl_device(dev);
489 
490 	verify_repeater_in.header.api_version = HDCP_API_VERSION;
491 	verify_repeater_in.header.command_id = WIRED_VERIFY_REPEATER;
492 	verify_repeater_in.header.status = ME_HDCP_STATUS_SUCCESS;
493 	verify_repeater_in.header.buffer_len =
494 					WIRED_CMD_BUF_LEN_VERIFY_REPEATER_IN;
495 
496 	verify_repeater_in.port.integrated_port_type = data->port_type;
497 	verify_repeater_in.port.physical_port = (u8)data->fw_ddi;
498 	verify_repeater_in.port.attached_transcoder = (u8)data->fw_tc;
499 
500 	memcpy(verify_repeater_in.rx_info, rep_topology->rx_info,
501 	       HDCP_2_2_RXINFO_LEN);
502 	memcpy(verify_repeater_in.seq_num_v, rep_topology->seq_num_v,
503 	       HDCP_2_2_SEQ_NUM_LEN);
504 	memcpy(verify_repeater_in.v_prime, rep_topology->v_prime,
505 	       HDCP_2_2_V_PRIME_HALF_LEN);
506 	memcpy(verify_repeater_in.receiver_ids, rep_topology->receiver_ids,
507 	       HDCP_2_2_RECEIVER_IDS_MAX_LEN);
508 
509 	byte = mei_cldev_send(cldev, (u8 *)&verify_repeater_in,
510 			      sizeof(verify_repeater_in));
511 	if (byte < 0) {
512 		dev_dbg(dev, "mei_cldev_send failed. %zd\n", byte);
513 		return byte;
514 	}
515 
516 	byte = mei_cldev_recv(cldev, (u8 *)&verify_repeater_out,
517 			      sizeof(verify_repeater_out));
518 	if (byte < 0) {
519 		dev_dbg(dev, "mei_cldev_recv failed. %zd\n", byte);
520 		return byte;
521 	}
522 
523 	if (verify_repeater_out.header.status != ME_HDCP_STATUS_SUCCESS) {
524 		dev_dbg(dev, "ME cmd 0x%08X failed. status: 0x%X\n",
525 			WIRED_VERIFY_REPEATER,
526 			verify_repeater_out.header.status);
527 		return -EIO;
528 	}
529 
530 	memcpy(rep_send_ack->v, verify_repeater_out.v,
531 	       HDCP_2_2_V_PRIME_HALF_LEN);
532 	rep_send_ack->msg_id = HDCP_2_2_REP_SEND_ACK;
533 
534 	return 0;
535 }
536 
537 /**
538  * mei_hdcp_verify_mprime() - Verify mprime.
539  * @dev: device corresponding to the mei_cl_device
540  * @data: Intel HW specific hdcp data
541  * @stream_ready: RepeaterAuth_Stream_Ready msg for ME FW verification.
542  *
543  * Return: 0 on Success, <0 on Failure
544  */
545 static int mei_hdcp_verify_mprime(struct device *dev,
546 				  struct hdcp_port_data *data,
547 				  struct hdcp2_rep_stream_ready *stream_ready)
548 {
549 	struct wired_cmd_repeater_auth_stream_req_in *verify_mprime_in;
550 	struct wired_cmd_repeater_auth_stream_req_out
551 					verify_mprime_out = { { 0 } };
552 	struct mei_cl_device *cldev;
553 	ssize_t byte;
554 	size_t cmd_size;
555 
556 	if (!dev || !stream_ready || !data)
557 		return -EINVAL;
558 
559 	cldev = to_mei_cl_device(dev);
560 
561 	cmd_size = struct_size(verify_mprime_in, streams, data->k);
562 	if (cmd_size == SIZE_MAX)
563 		return -EINVAL;
564 
565 	verify_mprime_in = kzalloc(cmd_size, GFP_KERNEL);
566 	if (!verify_mprime_in)
567 		return -ENOMEM;
568 
569 	verify_mprime_in->header.api_version = HDCP_API_VERSION;
570 	verify_mprime_in->header.command_id = WIRED_REPEATER_AUTH_STREAM_REQ;
571 	verify_mprime_in->header.status = ME_HDCP_STATUS_SUCCESS;
572 	verify_mprime_in->header.buffer_len =
573 			WIRED_CMD_BUF_LEN_REPEATER_AUTH_STREAM_REQ_MIN_IN;
574 
575 	verify_mprime_in->port.integrated_port_type = data->port_type;
576 	verify_mprime_in->port.physical_port = (u8)data->fw_ddi;
577 	verify_mprime_in->port.attached_transcoder = (u8)data->fw_tc;
578 
579 	memcpy(verify_mprime_in->m_prime, stream_ready->m_prime, HDCP_2_2_MPRIME_LEN);
580 	drm_hdcp_cpu_to_be24(verify_mprime_in->seq_num_m, data->seq_num_m);
581 
582 	memcpy(verify_mprime_in->streams, data->streams,
583 	       array_size(data->k, sizeof(*data->streams)));
584 
585 	verify_mprime_in->k = cpu_to_be16(data->k);
586 
587 	byte = mei_cldev_send(cldev, (u8 *)verify_mprime_in, cmd_size);
588 	kfree(verify_mprime_in);
589 	if (byte < 0) {
590 		dev_dbg(dev, "mei_cldev_send failed. %zd\n", byte);
591 		return byte;
592 	}
593 
594 	byte = mei_cldev_recv(cldev, (u8 *)&verify_mprime_out,
595 			      sizeof(verify_mprime_out));
596 	if (byte < 0) {
597 		dev_dbg(dev, "mei_cldev_recv failed. %zd\n", byte);
598 		return byte;
599 	}
600 
601 	if (verify_mprime_out.header.status != ME_HDCP_STATUS_SUCCESS) {
602 		dev_dbg(dev, "ME cmd 0x%08X failed. status: 0x%X\n",
603 			WIRED_REPEATER_AUTH_STREAM_REQ,
604 			verify_mprime_out.header.status);
605 		return -EIO;
606 	}
607 
608 	return 0;
609 }
610 
611 /**
612  * mei_hdcp_enable_authentication() - Mark a port as authenticated
613  * through ME FW
614  * @dev: device corresponding to the mei_cl_device
615  * @data: Intel HW specific hdcp data
616  *
617  * Return: 0 on Success, <0 on Failure
618  */
619 static int mei_hdcp_enable_authentication(struct device *dev,
620 					  struct hdcp_port_data *data)
621 {
622 	struct wired_cmd_enable_auth_in enable_auth_in = { { 0 } };
623 	struct wired_cmd_enable_auth_out enable_auth_out = { { 0 } };
624 	struct mei_cl_device *cldev;
625 	ssize_t byte;
626 
627 	if (!dev || !data)
628 		return -EINVAL;
629 
630 	cldev = to_mei_cl_device(dev);
631 
632 	enable_auth_in.header.api_version = HDCP_API_VERSION;
633 	enable_auth_in.header.command_id = WIRED_ENABLE_AUTH;
634 	enable_auth_in.header.status = ME_HDCP_STATUS_SUCCESS;
635 	enable_auth_in.header.buffer_len = WIRED_CMD_BUF_LEN_ENABLE_AUTH_IN;
636 
637 	enable_auth_in.port.integrated_port_type = data->port_type;
638 	enable_auth_in.port.physical_port = (u8)data->fw_ddi;
639 	enable_auth_in.port.attached_transcoder = (u8)data->fw_tc;
640 	enable_auth_in.stream_type = data->streams[0].stream_type;
641 
642 	byte = mei_cldev_send(cldev, (u8 *)&enable_auth_in,
643 			      sizeof(enable_auth_in));
644 	if (byte < 0) {
645 		dev_dbg(dev, "mei_cldev_send failed. %zd\n", byte);
646 		return byte;
647 	}
648 
649 	byte = mei_cldev_recv(cldev, (u8 *)&enable_auth_out,
650 			      sizeof(enable_auth_out));
651 	if (byte < 0) {
652 		dev_dbg(dev, "mei_cldev_recv failed. %zd\n", byte);
653 		return byte;
654 	}
655 
656 	if (enable_auth_out.header.status != ME_HDCP_STATUS_SUCCESS) {
657 		dev_dbg(dev, "ME cmd 0x%08X failed. status: 0x%X\n",
658 			WIRED_ENABLE_AUTH, enable_auth_out.header.status);
659 		return -EIO;
660 	}
661 
662 	return 0;
663 }
664 
665 /**
666  * mei_hdcp_close_session() - Close the Wired HDCP Tx session of ME FW per port.
667  * This also disables the authenticated state of the port.
668  * @dev: device corresponding to the mei_cl_device
669  * @data: Intel HW specific hdcp data
670  *
671  * Return: 0 on Success, <0 on Failure
672  */
673 static int
674 mei_hdcp_close_session(struct device *dev, struct hdcp_port_data *data)
675 {
676 	struct wired_cmd_close_session_in session_close_in = { { 0 } };
677 	struct wired_cmd_close_session_out session_close_out = { { 0 } };
678 	struct mei_cl_device *cldev;
679 	ssize_t byte;
680 
681 	if (!dev || !data)
682 		return -EINVAL;
683 
684 	cldev = to_mei_cl_device(dev);
685 
686 	session_close_in.header.api_version = HDCP_API_VERSION;
687 	session_close_in.header.command_id = WIRED_CLOSE_SESSION;
688 	session_close_in.header.status = ME_HDCP_STATUS_SUCCESS;
689 	session_close_in.header.buffer_len =
690 				WIRED_CMD_BUF_LEN_CLOSE_SESSION_IN;
691 
692 	session_close_in.port.integrated_port_type = data->port_type;
693 	session_close_in.port.physical_port = (u8)data->fw_ddi;
694 	session_close_in.port.attached_transcoder = (u8)data->fw_tc;
695 
696 	byte = mei_cldev_send(cldev, (u8 *)&session_close_in,
697 			      sizeof(session_close_in));
698 	if (byte < 0) {
699 		dev_dbg(dev, "mei_cldev_send failed. %zd\n", byte);
700 		return byte;
701 	}
702 
703 	byte = mei_cldev_recv(cldev, (u8 *)&session_close_out,
704 			      sizeof(session_close_out));
705 	if (byte < 0) {
706 		dev_dbg(dev, "mei_cldev_recv failed. %zd\n", byte);
707 		return byte;
708 	}
709 
710 	if (session_close_out.header.status != ME_HDCP_STATUS_SUCCESS) {
711 		dev_dbg(dev, "Session Close Failed. status: 0x%X\n",
712 			session_close_out.header.status);
713 		return -EIO;
714 	}
715 
716 	return 0;
717 }
718 
719 static const struct i915_hdcp_component_ops mei_hdcp_ops = {
720 	.owner = THIS_MODULE,
721 	.initiate_hdcp2_session = mei_hdcp_initiate_session,
722 	.verify_receiver_cert_prepare_km =
723 				mei_hdcp_verify_receiver_cert_prepare_km,
724 	.verify_hprime = mei_hdcp_verify_hprime,
725 	.store_pairing_info = mei_hdcp_store_pairing_info,
726 	.initiate_locality_check = mei_hdcp_initiate_locality_check,
727 	.verify_lprime = mei_hdcp_verify_lprime,
728 	.get_session_key = mei_hdcp_get_session_key,
729 	.repeater_check_flow_prepare_ack =
730 				mei_hdcp_repeater_check_flow_prepare_ack,
731 	.verify_mprime = mei_hdcp_verify_mprime,
732 	.enable_hdcp_authentication = mei_hdcp_enable_authentication,
733 	.close_hdcp_session = mei_hdcp_close_session,
734 };
735 
736 static int mei_component_master_bind(struct device *dev)
737 {
738 	struct mei_cl_device *cldev = to_mei_cl_device(dev);
739 	struct i915_hdcp_comp_master *comp_master =
740 						mei_cldev_get_drvdata(cldev);
741 	int ret;
742 
743 	dev_dbg(dev, "%s\n", __func__);
744 	comp_master->ops = &mei_hdcp_ops;
745 	comp_master->mei_dev = dev;
746 	ret = component_bind_all(dev, comp_master);
747 	if (ret < 0)
748 		return ret;
749 
750 	return 0;
751 }
752 
753 static void mei_component_master_unbind(struct device *dev)
754 {
755 	struct mei_cl_device *cldev = to_mei_cl_device(dev);
756 	struct i915_hdcp_comp_master *comp_master =
757 						mei_cldev_get_drvdata(cldev);
758 
759 	dev_dbg(dev, "%s\n", __func__);
760 	component_unbind_all(dev, comp_master);
761 }
762 
763 static const struct component_master_ops mei_component_master_ops = {
764 	.bind = mei_component_master_bind,
765 	.unbind = mei_component_master_unbind,
766 };
767 
768 /**
769  * mei_hdcp_component_match - compare function for matching mei hdcp.
770  *
771  *    The function checks if the driver is i915, the subcomponent is HDCP
772  *    and the grand parent of hdcp and the parent of i915 are the same
773  *    PCH device.
774  *
775  * @dev: master device
776  * @subcomponent: subcomponent to match (I915_COMPONENT_HDCP)
777  * @data: compare data (mei hdcp device)
778  *
779  * Return:
780  * * 1 - if components match
781  * * 0 - otherwise
782  */
783 static int mei_hdcp_component_match(struct device *dev, int subcomponent,
784 				    void *data)
785 {
786 	struct device *base = data;
787 
788 	if (strcmp(dev->driver->name, "i915") ||
789 	    subcomponent != I915_COMPONENT_HDCP)
790 		return 0;
791 
792 	base = base->parent;
793 	if (!base)
794 		return 0;
795 
796 	base = base->parent;
797 	dev = dev->parent;
798 
799 	return (base && dev && dev == base);
800 }
801 
802 static int mei_hdcp_probe(struct mei_cl_device *cldev,
803 			  const struct mei_cl_device_id *id)
804 {
805 	struct i915_hdcp_comp_master *comp_master;
806 	struct component_match *master_match;
807 	int ret;
808 
809 	ret = mei_cldev_enable(cldev);
810 	if (ret < 0) {
811 		dev_err(&cldev->dev, "mei_cldev_enable Failed. %d\n", ret);
812 		goto enable_err_exit;
813 	}
814 
815 	comp_master = kzalloc(sizeof(*comp_master), GFP_KERNEL);
816 	if (!comp_master) {
817 		ret = -ENOMEM;
818 		goto err_exit;
819 	}
820 
821 	master_match = NULL;
822 	component_match_add_typed(&cldev->dev, &master_match,
823 				  mei_hdcp_component_match, &cldev->dev);
824 	if (IS_ERR_OR_NULL(master_match)) {
825 		ret = -ENOMEM;
826 		goto err_exit;
827 	}
828 
829 	mei_cldev_set_drvdata(cldev, comp_master);
830 	ret = component_master_add_with_match(&cldev->dev,
831 					      &mei_component_master_ops,
832 					      master_match);
833 	if (ret < 0) {
834 		dev_err(&cldev->dev, "Master comp add failed %d\n", ret);
835 		goto err_exit;
836 	}
837 
838 	return 0;
839 
840 err_exit:
841 	mei_cldev_set_drvdata(cldev, NULL);
842 	kfree(comp_master);
843 	mei_cldev_disable(cldev);
844 enable_err_exit:
845 	return ret;
846 }
847 
848 static int mei_hdcp_remove(struct mei_cl_device *cldev)
849 {
850 	struct i915_hdcp_comp_master *comp_master =
851 						mei_cldev_get_drvdata(cldev);
852 
853 	component_master_del(&cldev->dev, &mei_component_master_ops);
854 	kfree(comp_master);
855 	mei_cldev_set_drvdata(cldev, NULL);
856 
857 	return mei_cldev_disable(cldev);
858 }
859 
860 #define MEI_UUID_HDCP GUID_INIT(0xB638AB7E, 0x94E2, 0x4EA2, 0xA5, \
861 				0x52, 0xD1, 0xC5, 0x4B, 0x62, 0x7F, 0x04)
862 
863 static const struct mei_cl_device_id mei_hdcp_tbl[] = {
864 	{ .uuid = MEI_UUID_HDCP, .version = MEI_CL_VERSION_ANY },
865 	{ }
866 };
867 MODULE_DEVICE_TABLE(mei, mei_hdcp_tbl);
868 
869 static struct mei_cl_driver mei_hdcp_driver = {
870 	.id_table = mei_hdcp_tbl,
871 	.name = KBUILD_MODNAME,
872 	.probe = mei_hdcp_probe,
873 	.remove	= mei_hdcp_remove,
874 };
875 
876 module_mei_cl_driver(mei_hdcp_driver);
877 
878 MODULE_AUTHOR("Intel Corporation");
879 MODULE_LICENSE("GPL");
880 MODULE_DESCRIPTION("MEI HDCP");
881