1 /*-
2 * Copyright (c) 2008 Yahoo!, Inc.
3 * All rights reserved.
4 * Written by: John Baldwin <jhb@FreeBSD.org>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the author nor the names of any co-contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 * Broadcom Inc. (LSI) MPT-Fusion Host Adapter FreeBSD userland interface
31 */
32 /*-
33 * Copyright (c) 2011-2015 LSI Corp.
34 * Copyright (c) 2013-2016 Avago Technologies
35 * Copyright 2000-2020 Broadcom Inc.
36 * All rights reserved.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * SUCH DAMAGE.
58 *
59 * Broadcom Inc. (LSI) MPT-Fusion Host Adapter FreeBSD
60 */
61
62 /* TODO Move headers to mprvar */
63 #include <sys/types.h>
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/kernel.h>
67 #include <sys/selinfo.h>
68 #include <sys/module.h>
69 #include <sys/bus.h>
70 #include <sys/conf.h>
71 #include <sys/bio.h>
72 #include <sys/abi_compat.h>
73 #include <sys/malloc.h>
74 #include <sys/uio.h>
75 #include <sys/sysctl.h>
76 #include <sys/ioccom.h>
77 #include <sys/endian.h>
78 #include <sys/queue.h>
79 #include <sys/kthread.h>
80 #include <sys/taskqueue.h>
81 #include <sys/proc.h>
82 #include <sys/sysent.h>
83
84 #include <machine/bus.h>
85 #include <machine/resource.h>
86 #include <sys/rman.h>
87
88 #include <cam/cam.h>
89 #include <cam/cam_ccb.h>
90
91 #include <dev/mpr/mpi/mpi2_type.h>
92 #include <dev/mpr/mpi/mpi2.h>
93 #include <dev/mpr/mpi/mpi2_ioc.h>
94 #include <dev/mpr/mpi/mpi2_cnfg.h>
95 #include <dev/mpr/mpi/mpi2_init.h>
96 #include <dev/mpr/mpi/mpi2_tool.h>
97 #include <dev/mpr/mpi/mpi2_pci.h>
98 #include <dev/mpr/mpr_ioctl.h>
99 #include <dev/mpr/mprvar.h>
100 #include <dev/mpr/mpr_table.h>
101 #include <dev/mpr/mpr_sas.h>
102 #include <dev/pci/pcivar.h>
103 #include <dev/pci/pcireg.h>
104
105 static d_open_t mpr_open;
106 static d_close_t mpr_close;
107 static d_ioctl_t mpr_ioctl_devsw;
108
109 static struct cdevsw mpr_cdevsw = {
110 .d_version = D_VERSION,
111 .d_flags = 0,
112 .d_open = mpr_open,
113 .d_close = mpr_close,
114 .d_ioctl = mpr_ioctl_devsw,
115 .d_name = "mpr",
116 };
117
118 typedef int (mpr_user_f)(struct mpr_command *, struct mpr_usr_command *);
119 static mpr_user_f mpi_pre_ioc_facts;
120 static mpr_user_f mpi_pre_port_facts;
121 static mpr_user_f mpi_pre_fw_download;
122 static mpr_user_f mpi_pre_fw_upload;
123 static mpr_user_f mpi_pre_sata_passthrough;
124 static mpr_user_f mpi_pre_smp_passthrough;
125 static mpr_user_f mpi_pre_config;
126 static mpr_user_f mpi_pre_sas_io_unit_control;
127
128 static int mpr_user_read_cfg_header(struct mpr_softc *,
129 struct mpr_cfg_page_req *);
130 static int mpr_user_read_cfg_page(struct mpr_softc *,
131 struct mpr_cfg_page_req *, void *);
132 static int mpr_user_read_extcfg_header(struct mpr_softc *,
133 struct mpr_ext_cfg_page_req *);
134 static int mpr_user_read_extcfg_page(struct mpr_softc *,
135 struct mpr_ext_cfg_page_req *, void *);
136 static int mpr_user_write_cfg_page(struct mpr_softc *,
137 struct mpr_cfg_page_req *, void *);
138 static int mpr_user_setup_request(struct mpr_command *,
139 struct mpr_usr_command *);
140 static int mpr_user_command(struct mpr_softc *, struct mpr_usr_command *);
141
142 static int mpr_user_pass_thru(struct mpr_softc *sc, mpr_pass_thru_t *data);
143 static void mpr_user_get_adapter_data(struct mpr_softc *sc,
144 mpr_adapter_data_t *data);
145 static void mpr_user_read_pci_info(struct mpr_softc *sc, mpr_pci_info_t *data);
146 static uint8_t mpr_get_fw_diag_buffer_number(struct mpr_softc *sc,
147 uint32_t unique_id);
148 static int mpr_post_fw_diag_buffer(struct mpr_softc *sc,
149 mpr_fw_diagnostic_buffer_t *pBuffer, uint32_t *return_code);
150 static int mpr_release_fw_diag_buffer(struct mpr_softc *sc,
151 mpr_fw_diagnostic_buffer_t *pBuffer, uint32_t *return_code,
152 uint32_t diag_type);
153 static int mpr_diag_register(struct mpr_softc *sc,
154 mpr_fw_diag_register_t *diag_register, uint32_t *return_code);
155 static int mpr_diag_unregister(struct mpr_softc *sc,
156 mpr_fw_diag_unregister_t *diag_unregister, uint32_t *return_code);
157 static int mpr_diag_query(struct mpr_softc *sc, mpr_fw_diag_query_t *diag_query,
158 uint32_t *return_code);
159 static int mpr_diag_read_buffer(struct mpr_softc *sc,
160 mpr_diag_read_buffer_t *diag_read_buffer, uint8_t *ioctl_buf,
161 uint32_t *return_code);
162 static int mpr_diag_release(struct mpr_softc *sc,
163 mpr_fw_diag_release_t *diag_release, uint32_t *return_code);
164 static int mpr_do_diag_action(struct mpr_softc *sc, uint32_t action,
165 uint8_t *diag_action, uint32_t length, uint32_t *return_code);
166 static int mpr_user_diag_action(struct mpr_softc *sc, mpr_diag_action_t *data);
167 static void mpr_user_event_query(struct mpr_softc *sc, mpr_event_query_t *data);
168 static void mpr_user_event_enable(struct mpr_softc *sc,
169 mpr_event_enable_t *data);
170 static int mpr_user_event_report(struct mpr_softc *sc,
171 mpr_event_report_t *data);
172 static int mpr_user_reg_access(struct mpr_softc *sc, mpr_reg_access_t *data);
173 static int mpr_user_btdh(struct mpr_softc *sc, mpr_btdh_mapping_t *data);
174
175 static MALLOC_DEFINE(M_MPRUSER, "mpr_user", "Buffers for mpr(4) ioctls");
176
177 /*
178 * MPI functions that support IEEE SGLs for SAS3.
179 */
180 static uint8_t ieee_sgl_func_list[] = {
181 MPI2_FUNCTION_SCSI_IO_REQUEST,
182 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH,
183 MPI2_FUNCTION_SMP_PASSTHROUGH,
184 MPI2_FUNCTION_SATA_PASSTHROUGH,
185 MPI2_FUNCTION_FW_UPLOAD,
186 MPI2_FUNCTION_FW_DOWNLOAD,
187 MPI2_FUNCTION_TARGET_ASSIST,
188 MPI2_FUNCTION_TARGET_STATUS_SEND,
189 MPI2_FUNCTION_TOOLBOX
190 };
191
192 int
mpr_attach_user(struct mpr_softc * sc)193 mpr_attach_user(struct mpr_softc *sc)
194 {
195 int unit;
196
197 unit = device_get_unit(sc->mpr_dev);
198 sc->mpr_cdev = make_dev(&mpr_cdevsw, unit, UID_ROOT, GID_OPERATOR, 0640,
199 "mpr%d", unit);
200
201 if (sc->mpr_cdev == NULL)
202 return (ENOMEM);
203
204 sc->mpr_cdev->si_drv1 = sc;
205 return (0);
206 }
207
208 void
mpr_detach_user(struct mpr_softc * sc)209 mpr_detach_user(struct mpr_softc *sc)
210 {
211
212 /* XXX: do a purge of pending requests? */
213 if (sc->mpr_cdev != NULL)
214 destroy_dev(sc->mpr_cdev);
215 }
216
217 static int
mpr_open(struct cdev * dev,int flags,int fmt,struct thread * td)218 mpr_open(struct cdev *dev, int flags, int fmt, struct thread *td)
219 {
220
221 return (0);
222 }
223
224 static int
mpr_close(struct cdev * dev,int flags,int fmt,struct thread * td)225 mpr_close(struct cdev *dev, int flags, int fmt, struct thread *td)
226 {
227
228 return (0);
229 }
230
231 static int
mpr_user_read_cfg_header(struct mpr_softc * sc,struct mpr_cfg_page_req * page_req)232 mpr_user_read_cfg_header(struct mpr_softc *sc,
233 struct mpr_cfg_page_req *page_req)
234 {
235 MPI2_CONFIG_PAGE_HEADER *hdr;
236 struct mpr_config_params params;
237 int error;
238
239 hdr = ¶ms.hdr.Struct;
240 params.action = MPI2_CONFIG_ACTION_PAGE_HEADER;
241 params.page_address = le32toh(page_req->page_address);
242 hdr->PageVersion = 0;
243 hdr->PageLength = 0;
244 hdr->PageNumber = page_req->header.PageNumber;
245 hdr->PageType = page_req->header.PageType;
246 params.buffer = NULL;
247 params.length = 0;
248 params.callback = NULL;
249
250 if ((error = mpr_read_config_page(sc, ¶ms)) != 0) {
251 /*
252 * Leave the request. Without resetting the chip, it's
253 * still owned by it and we'll just get into trouble
254 * freeing it now. Mark it as abandoned so that if it
255 * shows up later it can be freed.
256 */
257 mpr_printf(sc, "read_cfg_header timed out\n");
258 return (ETIMEDOUT);
259 }
260
261 page_req->ioc_status = htole16(params.status);
262 if ((page_req->ioc_status & MPI2_IOCSTATUS_MASK) ==
263 MPI2_IOCSTATUS_SUCCESS) {
264 bcopy(hdr, &page_req->header, sizeof(page_req->header));
265 }
266
267 return (0);
268 }
269
270 static int
mpr_user_read_cfg_page(struct mpr_softc * sc,struct mpr_cfg_page_req * page_req,void * buf)271 mpr_user_read_cfg_page(struct mpr_softc *sc, struct mpr_cfg_page_req *page_req,
272 void *buf)
273 {
274 MPI2_CONFIG_PAGE_HEADER *reqhdr, *hdr;
275 struct mpr_config_params params;
276 int error;
277
278 reqhdr = buf;
279 hdr = ¶ms.hdr.Struct;
280 hdr->PageVersion = reqhdr->PageVersion;
281 hdr->PageLength = reqhdr->PageLength;
282 hdr->PageNumber = reqhdr->PageNumber;
283 hdr->PageType = reqhdr->PageType & MPI2_CONFIG_PAGETYPE_MASK;
284 params.action = MPI2_CONFIG_ACTION_PAGE_READ_CURRENT;
285 params.page_address = le32toh(page_req->page_address);
286 params.buffer = buf;
287 params.length = le32toh(page_req->len);
288 params.callback = NULL;
289
290 if ((error = mpr_read_config_page(sc, ¶ms)) != 0) {
291 mpr_printf(sc, "mpr_user_read_cfg_page timed out\n");
292 return (ETIMEDOUT);
293 }
294
295 page_req->ioc_status = htole16(params.status);
296 return (0);
297 }
298
299 static int
mpr_user_read_extcfg_header(struct mpr_softc * sc,struct mpr_ext_cfg_page_req * ext_page_req)300 mpr_user_read_extcfg_header(struct mpr_softc *sc,
301 struct mpr_ext_cfg_page_req *ext_page_req)
302 {
303 MPI2_CONFIG_EXTENDED_PAGE_HEADER *hdr;
304 struct mpr_config_params params;
305 int error;
306
307 hdr = ¶ms.hdr.Ext;
308 params.action = MPI2_CONFIG_ACTION_PAGE_HEADER;
309 hdr->PageVersion = ext_page_req->header.PageVersion;
310 hdr->PageType = MPI2_CONFIG_PAGETYPE_EXTENDED;
311 hdr->ExtPageLength = 0;
312 hdr->PageNumber = ext_page_req->header.PageNumber;
313 hdr->ExtPageType = ext_page_req->header.ExtPageType;
314 params.page_address = le32toh(ext_page_req->page_address);
315 params.buffer = NULL;
316 params.length = 0;
317 params.callback = NULL;
318
319 if ((error = mpr_read_config_page(sc, ¶ms)) != 0) {
320 /*
321 * Leave the request. Without resetting the chip, it's
322 * still owned by it and we'll just get into trouble
323 * freeing it now. Mark it as abandoned so that if it
324 * shows up later it can be freed.
325 */
326 mpr_printf(sc, "mpr_user_read_extcfg_header timed out\n");
327 return (ETIMEDOUT);
328 }
329
330 ext_page_req->ioc_status = htole16(params.status);
331 if ((ext_page_req->ioc_status & MPI2_IOCSTATUS_MASK) ==
332 MPI2_IOCSTATUS_SUCCESS) {
333 ext_page_req->header.PageVersion = hdr->PageVersion;
334 ext_page_req->header.PageNumber = hdr->PageNumber;
335 ext_page_req->header.PageType = hdr->PageType;
336 ext_page_req->header.ExtPageLength = hdr->ExtPageLength;
337 ext_page_req->header.ExtPageType = hdr->ExtPageType;
338 }
339
340 return (0);
341 }
342
343 static int
mpr_user_read_extcfg_page(struct mpr_softc * sc,struct mpr_ext_cfg_page_req * ext_page_req,void * buf)344 mpr_user_read_extcfg_page(struct mpr_softc *sc,
345 struct mpr_ext_cfg_page_req *ext_page_req, void *buf)
346 {
347 MPI2_CONFIG_EXTENDED_PAGE_HEADER *reqhdr, *hdr;
348 struct mpr_config_params params;
349 int error;
350
351 reqhdr = buf;
352 hdr = ¶ms.hdr.Ext;
353 params.action = MPI2_CONFIG_ACTION_PAGE_READ_CURRENT;
354 params.page_address = le32toh(ext_page_req->page_address);
355 hdr->PageVersion = reqhdr->PageVersion;
356 hdr->PageType = MPI2_CONFIG_PAGETYPE_EXTENDED;
357 hdr->PageNumber = reqhdr->PageNumber;
358 hdr->ExtPageType = reqhdr->ExtPageType;
359 hdr->ExtPageLength = reqhdr->ExtPageLength;
360 params.buffer = buf;
361 params.length = le32toh(ext_page_req->len);
362 params.callback = NULL;
363
364 if ((error = mpr_read_config_page(sc, ¶ms)) != 0) {
365 mpr_printf(sc, "mpr_user_read_extcfg_page timed out\n");
366 return (ETIMEDOUT);
367 }
368
369 ext_page_req->ioc_status = htole16(params.status);
370 return (0);
371 }
372
373 static int
mpr_user_write_cfg_page(struct mpr_softc * sc,struct mpr_cfg_page_req * page_req,void * buf)374 mpr_user_write_cfg_page(struct mpr_softc *sc,
375 struct mpr_cfg_page_req *page_req, void *buf)
376 {
377 MPI2_CONFIG_PAGE_HEADER *reqhdr, *hdr;
378 struct mpr_config_params params;
379 u_int hdr_attr;
380 int error;
381
382 reqhdr = buf;
383 hdr = ¶ms.hdr.Struct;
384 hdr_attr = reqhdr->PageType & MPI2_CONFIG_PAGEATTR_MASK;
385 if (hdr_attr != MPI2_CONFIG_PAGEATTR_CHANGEABLE &&
386 hdr_attr != MPI2_CONFIG_PAGEATTR_PERSISTENT) {
387 mpr_printf(sc, "page type 0x%x not changeable\n",
388 reqhdr->PageType & MPI2_CONFIG_PAGETYPE_MASK);
389 return (EINVAL);
390 }
391
392 /*
393 * There isn't any point in restoring stripped out attributes
394 * if you then mask them going down to issue the request.
395 */
396
397 hdr->PageVersion = reqhdr->PageVersion;
398 hdr->PageLength = reqhdr->PageLength;
399 hdr->PageNumber = reqhdr->PageNumber;
400 hdr->PageType = reqhdr->PageType;
401 params.action = MPI2_CONFIG_ACTION_PAGE_WRITE_CURRENT;
402 params.page_address = le32toh(page_req->page_address);
403 params.buffer = buf;
404 params.length = le32toh(page_req->len);
405 params.callback = NULL;
406
407 if ((error = mpr_write_config_page(sc, ¶ms)) != 0) {
408 mpr_printf(sc, "mpr_write_cfg_page timed out\n");
409 return (ETIMEDOUT);
410 }
411
412 page_req->ioc_status = htole16(params.status);
413 return (0);
414 }
415
416 void
mpr_init_sge(struct mpr_command * cm,void * req,void * sge)417 mpr_init_sge(struct mpr_command *cm, void *req, void *sge)
418 {
419 int off, space;
420
421 space = (int)cm->cm_sc->reqframesz;
422 off = (uintptr_t)sge - (uintptr_t)req;
423
424 KASSERT(off < space, ("bad pointers %p %p, off %d, space %d",
425 req, sge, off, space));
426
427 cm->cm_sge = sge;
428 cm->cm_sglsize = space - off;
429 }
430
431 /*
432 * Prepare the mpr_command for an IOC_FACTS request.
433 */
434 static int
mpi_pre_ioc_facts(struct mpr_command * cm,struct mpr_usr_command * cmd)435 mpi_pre_ioc_facts(struct mpr_command *cm, struct mpr_usr_command *cmd)
436 {
437 MPI2_IOC_FACTS_REQUEST *req = (void *)cm->cm_req;
438 MPI2_IOC_FACTS_REPLY *rpl;
439
440 if (cmd->req_len != sizeof *req)
441 return (EINVAL);
442 if (cmd->rpl_len != sizeof *rpl)
443 return (EINVAL);
444
445 cm->cm_sge = NULL;
446 cm->cm_sglsize = 0;
447 return (0);
448 }
449
450 /*
451 * Prepare the mpr_command for a PORT_FACTS request.
452 */
453 static int
mpi_pre_port_facts(struct mpr_command * cm,struct mpr_usr_command * cmd)454 mpi_pre_port_facts(struct mpr_command *cm, struct mpr_usr_command *cmd)
455 {
456 MPI2_PORT_FACTS_REQUEST *req = (void *)cm->cm_req;
457 MPI2_PORT_FACTS_REPLY *rpl;
458
459 if (cmd->req_len != sizeof *req)
460 return (EINVAL);
461 if (cmd->rpl_len != sizeof *rpl)
462 return (EINVAL);
463
464 cm->cm_sge = NULL;
465 cm->cm_sglsize = 0;
466 return (0);
467 }
468
469 /*
470 * Prepare the mpr_command for a FW_DOWNLOAD request.
471 */
472 static int
mpi_pre_fw_download(struct mpr_command * cm,struct mpr_usr_command * cmd)473 mpi_pre_fw_download(struct mpr_command *cm, struct mpr_usr_command *cmd)
474 {
475 MPI25_FW_DOWNLOAD_REQUEST *req = (void *)cm->cm_req;
476 MPI2_FW_DOWNLOAD_REPLY *rpl;
477 int error;
478
479 if (cmd->req_len != sizeof *req)
480 return (EINVAL);
481 if (cmd->rpl_len != sizeof *rpl)
482 return (EINVAL);
483
484 if (cmd->len == 0)
485 return (EINVAL);
486
487 error = copyin(cmd->buf, cm->cm_data, cmd->len);
488 if (error != 0)
489 return (error);
490
491 mpr_init_sge(cm, req, &req->SGL);
492
493 /*
494 * For now, the F/W image must be provided in a single request.
495 */
496 if ((req->MsgFlags & MPI2_FW_DOWNLOAD_MSGFLGS_LAST_SEGMENT) == 0)
497 return (EINVAL);
498 if (req->TotalImageSize != cmd->len)
499 return (EINVAL);
500
501 req->ImageOffset = 0;
502 req->ImageSize = cmd->len;
503
504 cm->cm_flags |= MPR_CM_FLAGS_DATAOUT;
505
506 return (mpr_push_ieee_sge(cm, &req->SGL, 0));
507 }
508
509 /*
510 * Prepare the mpr_command for a FW_UPLOAD request.
511 */
512 static int
mpi_pre_fw_upload(struct mpr_command * cm,struct mpr_usr_command * cmd)513 mpi_pre_fw_upload(struct mpr_command *cm, struct mpr_usr_command *cmd)
514 {
515 MPI25_FW_UPLOAD_REQUEST *req = (void *)cm->cm_req;
516 MPI2_FW_UPLOAD_REPLY *rpl;
517
518 if (cmd->req_len != sizeof *req)
519 return (EINVAL);
520 if (cmd->rpl_len != sizeof *rpl)
521 return (EINVAL);
522
523 mpr_init_sge(cm, req, &req->SGL);
524 if (cmd->len == 0) {
525 /* Perhaps just asking what the size of the fw is? */
526 return (0);
527 }
528
529 req->ImageOffset = 0;
530 req->ImageSize = cmd->len;
531
532 cm->cm_flags |= MPR_CM_FLAGS_DATAIN;
533
534 return (mpr_push_ieee_sge(cm, &req->SGL, 0));
535 }
536
537 /*
538 * Prepare the mpr_command for a SATA_PASSTHROUGH request.
539 */
540 static int
mpi_pre_sata_passthrough(struct mpr_command * cm,struct mpr_usr_command * cmd)541 mpi_pre_sata_passthrough(struct mpr_command *cm, struct mpr_usr_command *cmd)
542 {
543 MPI2_SATA_PASSTHROUGH_REQUEST *req = (void *)cm->cm_req;
544 MPI2_SATA_PASSTHROUGH_REPLY *rpl;
545
546 if (cmd->req_len != sizeof *req)
547 return (EINVAL);
548 if (cmd->rpl_len != sizeof *rpl)
549 return (EINVAL);
550
551 mpr_init_sge(cm, req, &req->SGL);
552 return (0);
553 }
554
555 /*
556 * Prepare the mpr_command for a SMP_PASSTHROUGH request.
557 */
558 static int
mpi_pre_smp_passthrough(struct mpr_command * cm,struct mpr_usr_command * cmd)559 mpi_pre_smp_passthrough(struct mpr_command *cm, struct mpr_usr_command *cmd)
560 {
561 MPI2_SMP_PASSTHROUGH_REQUEST *req = (void *)cm->cm_req;
562 MPI2_SMP_PASSTHROUGH_REPLY *rpl;
563
564 if (cmd->req_len != sizeof *req)
565 return (EINVAL);
566 if (cmd->rpl_len != sizeof *rpl)
567 return (EINVAL);
568
569 mpr_init_sge(cm, req, &req->SGL);
570 return (0);
571 }
572
573 /*
574 * Prepare the mpr_command for a CONFIG request.
575 */
576 static int
mpi_pre_config(struct mpr_command * cm,struct mpr_usr_command * cmd)577 mpi_pre_config(struct mpr_command *cm, struct mpr_usr_command *cmd)
578 {
579 MPI2_CONFIG_REQUEST *req = (void *)cm->cm_req;
580 MPI2_CONFIG_REPLY *rpl;
581
582 if (cmd->req_len != sizeof *req)
583 return (EINVAL);
584 if (cmd->rpl_len != sizeof *rpl)
585 return (EINVAL);
586
587 mpr_init_sge(cm, req, &req->PageBufferSGE);
588 return (0);
589 }
590
591 /*
592 * Prepare the mpr_command for a SAS_IO_UNIT_CONTROL request.
593 */
594 static int
mpi_pre_sas_io_unit_control(struct mpr_command * cm,struct mpr_usr_command * cmd)595 mpi_pre_sas_io_unit_control(struct mpr_command *cm,
596 struct mpr_usr_command *cmd)
597 {
598
599 cm->cm_sge = NULL;
600 cm->cm_sglsize = 0;
601 return (0);
602 }
603
604 /*
605 * A set of functions to prepare an mpr_command for the various
606 * supported requests.
607 */
608 struct mpr_user_func {
609 U8 Function;
610 mpr_user_f *f_pre;
611 } mpr_user_func_list[] = {
612 { MPI2_FUNCTION_IOC_FACTS, mpi_pre_ioc_facts },
613 { MPI2_FUNCTION_PORT_FACTS, mpi_pre_port_facts },
614 { MPI2_FUNCTION_FW_DOWNLOAD, mpi_pre_fw_download },
615 { MPI2_FUNCTION_FW_UPLOAD, mpi_pre_fw_upload },
616 { MPI2_FUNCTION_SATA_PASSTHROUGH, mpi_pre_sata_passthrough },
617 { MPI2_FUNCTION_SMP_PASSTHROUGH, mpi_pre_smp_passthrough},
618 { MPI2_FUNCTION_CONFIG, mpi_pre_config},
619 { MPI2_FUNCTION_SAS_IO_UNIT_CONTROL, mpi_pre_sas_io_unit_control },
620 { 0xFF, NULL } /* list end */
621 };
622
623 static int
mpr_user_setup_request(struct mpr_command * cm,struct mpr_usr_command * cmd)624 mpr_user_setup_request(struct mpr_command *cm, struct mpr_usr_command *cmd)
625 {
626 MPI2_REQUEST_HEADER *hdr = (MPI2_REQUEST_HEADER *)cm->cm_req;
627 struct mpr_user_func *f;
628
629 for (f = mpr_user_func_list; f->f_pre != NULL; f++) {
630 if (hdr->Function == f->Function)
631 return (f->f_pre(cm, cmd));
632 }
633 return (EINVAL);
634 }
635
636 static int
mpr_user_command(struct mpr_softc * sc,struct mpr_usr_command * cmd)637 mpr_user_command(struct mpr_softc *sc, struct mpr_usr_command *cmd)
638 {
639 MPI2_REQUEST_HEADER *hdr;
640 MPI2_DEFAULT_REPLY *rpl = NULL;
641 void *buf = NULL;
642 struct mpr_command *cm = NULL;
643 int err = 0;
644 int sz;
645
646 mpr_lock(sc);
647 cm = mpr_alloc_command(sc);
648
649 if (cm == NULL) {
650 mpr_printf(sc, "%s: no mpr requests\n", __func__);
651 err = ENOMEM;
652 goto RetFree;
653 }
654 mpr_unlock(sc);
655
656 hdr = (MPI2_REQUEST_HEADER *)cm->cm_req;
657
658 mpr_dprint(sc, MPR_USER, "%s: req %p %d rpl %p %d\n", __func__,
659 cmd->req, cmd->req_len, cmd->rpl, cmd->rpl_len);
660
661 if (cmd->req_len > (int)sc->reqframesz) {
662 err = EINVAL;
663 goto RetFreeUnlocked;
664 }
665 err = copyin(cmd->req, hdr, cmd->req_len);
666 if (err != 0)
667 goto RetFreeUnlocked;
668
669 mpr_dprint(sc, MPR_USER, "%s: Function %02X MsgFlags %02X\n", __func__,
670 hdr->Function, hdr->MsgFlags);
671
672 if (cmd->len > 0) {
673 buf = malloc(cmd->len, M_MPRUSER, M_WAITOK|M_ZERO);
674 cm->cm_data = buf;
675 cm->cm_length = cmd->len;
676 } else {
677 cm->cm_data = NULL;
678 cm->cm_length = 0;
679 }
680
681 cm->cm_flags = MPR_CM_FLAGS_SGE_SIMPLE;
682 cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
683
684 err = mpr_user_setup_request(cm, cmd);
685 if (err == EINVAL) {
686 mpr_printf(sc, "%s: unsupported parameter or unsupported "
687 "function in request (function = 0x%X)\n", __func__,
688 hdr->Function);
689 }
690 if (err != 0)
691 goto RetFreeUnlocked;
692
693 mpr_lock(sc);
694 err = mpr_wait_command(sc, &cm, 30, CAN_SLEEP);
695
696 if (err || (cm == NULL)) {
697 mpr_printf(sc, "%s: invalid request: error %d\n",
698 __func__, err);
699 goto RetFree;
700 }
701
702 if (cm != NULL)
703 rpl = (MPI2_DEFAULT_REPLY *)cm->cm_reply;
704 if (rpl != NULL)
705 sz = rpl->MsgLength * 4;
706 else
707 sz = 0;
708
709 if (sz > cmd->rpl_len) {
710 mpr_printf(sc, "%s: user reply buffer (%d) smaller than "
711 "returned buffer (%d)\n", __func__, cmd->rpl_len, sz);
712 sz = cmd->rpl_len;
713 }
714
715 mpr_unlock(sc);
716 err = copyout(rpl, cmd->rpl, sz);
717 if (buf != NULL && err == 0)
718 err = copyout(buf, cmd->buf, cmd->len);
719 mpr_dprint(sc, MPR_USER, "%s: reply size %d\n", __func__, sz);
720
721 RetFreeUnlocked:
722 mpr_lock(sc);
723 RetFree:
724 if (cm != NULL)
725 mpr_free_command(sc, cm);
726 mpr_unlock(sc);
727 if (buf != NULL)
728 free(buf, M_MPRUSER);
729 return (err);
730 }
731
732 static int
mpr_user_pass_thru(struct mpr_softc * sc,mpr_pass_thru_t * data)733 mpr_user_pass_thru(struct mpr_softc *sc, mpr_pass_thru_t *data)
734 {
735 MPI2_REQUEST_HEADER *hdr, *tmphdr;
736 MPI2_DEFAULT_REPLY *rpl;
737 Mpi26NVMeEncapsulatedErrorReply_t *nvme_error_reply = NULL;
738 Mpi26NVMeEncapsulatedRequest_t *nvme_encap_request = NULL;
739 struct mpr_command *cm = NULL;
740 void *req = NULL;
741 int i, err = 0, dir = 0, sz;
742 uint8_t tool, function = 0;
743 u_int sense_len;
744 struct mprsas_target *targ = NULL;
745
746 /*
747 * Only allow one passthru command at a time. Use the MPR_FLAGS_BUSY
748 * bit to denote that a passthru is being processed.
749 */
750 mpr_lock(sc);
751 if (sc->mpr_flags & MPR_FLAGS_BUSY) {
752 mpr_dprint(sc, MPR_USER, "%s: Only one passthru command "
753 "allowed at a single time.", __func__);
754 mpr_unlock(sc);
755 return (EBUSY);
756 }
757 sc->mpr_flags |= MPR_FLAGS_BUSY;
758 mpr_unlock(sc);
759
760 /*
761 * Do some validation on data direction. Valid cases are:
762 * 1) DataSize is 0 and direction is NONE
763 * 2) DataSize is non-zero and one of:
764 * a) direction is READ or
765 * b) direction is WRITE or
766 * c) direction is BOTH and DataOutSize is non-zero
767 * If valid and the direction is BOTH, change the direction to READ.
768 * if valid and the direction is not BOTH, make sure DataOutSize is 0.
769 */
770 if (((data->DataSize == 0) &&
771 (data->DataDirection == MPR_PASS_THRU_DIRECTION_NONE)) ||
772 ((data->DataSize != 0) &&
773 ((data->DataDirection == MPR_PASS_THRU_DIRECTION_READ) ||
774 (data->DataDirection == MPR_PASS_THRU_DIRECTION_WRITE) ||
775 ((data->DataDirection == MPR_PASS_THRU_DIRECTION_BOTH) &&
776 (data->DataOutSize != 0))))) {
777 if (data->DataDirection == MPR_PASS_THRU_DIRECTION_BOTH)
778 data->DataDirection = MPR_PASS_THRU_DIRECTION_READ;
779 else
780 data->DataOutSize = 0;
781 } else {
782 err = EINVAL;
783 goto RetFreeUnlocked;
784 }
785
786 mpr_dprint(sc, MPR_USER, "%s: req 0x%jx %d rpl 0x%jx %d "
787 "data in 0x%jx %d data out 0x%jx %d data dir %d\n", __func__,
788 data->PtrRequest, data->RequestSize, data->PtrReply,
789 data->ReplySize, data->PtrData, data->DataSize,
790 data->PtrDataOut, data->DataOutSize, data->DataDirection);
791
792 if (data->RequestSize > sc->reqframesz) {
793 err = EINVAL;
794 goto RetFreeUnlocked;
795 }
796
797 req = malloc(data->RequestSize, M_MPRUSER, M_WAITOK | M_ZERO);
798 tmphdr = (MPI2_REQUEST_HEADER *)req;
799
800 err = copyin(PTRIN(data->PtrRequest), req, data->RequestSize);
801 if (err != 0)
802 goto RetFreeUnlocked;
803
804 function = tmphdr->Function;
805 mpr_dprint(sc, MPR_USER, "%s: Function %02X MsgFlags %02X\n", __func__,
806 function, tmphdr->MsgFlags);
807
808 /*
809 * Handle a passthru TM request.
810 */
811 if (function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
812 MPI2_SCSI_TASK_MANAGE_REQUEST *task;
813
814 mpr_lock(sc);
815 cm = mprsas_alloc_tm(sc);
816 if (cm == NULL) {
817 err = EINVAL;
818 goto Ret;
819 }
820
821 /* Copy the header in. Only a small fixup is needed. */
822 task = (MPI2_SCSI_TASK_MANAGE_REQUEST *)cm->cm_req;
823 memcpy(task, req, data->RequestSize);
824 task->TaskMID = cm->cm_desc.Default.SMID;
825
826 cm->cm_data = NULL;
827 cm->cm_complete = NULL;
828 cm->cm_complete_data = NULL;
829
830 targ = mprsas_find_target_by_handle(sc->sassc, 0,
831 task->DevHandle);
832 if (targ == NULL) {
833 mpr_dprint(sc, MPR_INFO,
834 "%s %d : invalid handle for requested TM 0x%x \n",
835 __func__, __LINE__, task->DevHandle);
836 err = 1;
837 } else {
838 mprsas_prepare_for_tm(sc, cm, targ, CAM_LUN_WILDCARD);
839 err = mpr_wait_command(sc, &cm, 30, CAN_SLEEP);
840 }
841
842 if (err != 0) {
843 err = EIO;
844 mpr_dprint(sc, MPR_FAULT, "%s: task management failed",
845 __func__);
846 }
847 /*
848 * Copy the reply data and sense data to user space.
849 */
850 if (err == 0 && cm != NULL && cm->cm_reply != NULL) {
851 rpl = (MPI2_DEFAULT_REPLY *)cm->cm_reply;
852 sz = rpl->MsgLength * 4;
853
854 if (bootverbose && sz > data->ReplySize) {
855 mpr_printf(sc, "%s: user reply buffer (%d) "
856 "smaller than returned buffer (%d)\n",
857 __func__, data->ReplySize, sz);
858 }
859 mpr_unlock(sc);
860 err = copyout(cm->cm_reply, PTRIN(data->PtrReply),
861 MIN(sz, data->ReplySize));
862 mpr_lock(sc);
863 }
864 mprsas_free_tm(sc, cm);
865 goto Ret;
866 }
867
868 mpr_lock(sc);
869 cm = mpr_alloc_command(sc);
870 if (cm == NULL) {
871 mpr_printf(sc, "%s: no mpr requests\n", __func__);
872 err = ENOMEM;
873 goto Ret;
874 }
875 mpr_unlock(sc);
876
877 hdr = (MPI2_REQUEST_HEADER *)cm->cm_req;
878 memcpy(hdr, req, data->RequestSize);
879
880 /*
881 * Do some checking to make sure the IOCTL request contains a valid
882 * request. Then set the SGL info.
883 */
884 mpr_init_sge(cm, hdr, (void *)((uint8_t *)hdr + data->RequestSize));
885
886 /*
887 * Set up for read, write or both. From check above, DataOutSize will
888 * be 0 if direction is READ or WRITE, but it will have some non-zero
889 * value if the direction is BOTH. So, just use the biggest size to get
890 * the cm_data buffer size. If direction is BOTH, 2 SGLs need to be set
891 * up; the first is for the request and the second will contain the
892 * response data. cm_out_len needs to be set here and this will be used
893 * when the SGLs are set up.
894 */
895 cm->cm_data = NULL;
896 cm->cm_length = MAX(data->DataSize, data->DataOutSize);
897 cm->cm_out_len = data->DataOutSize;
898 cm->cm_flags = 0;
899 if (cm->cm_length != 0) {
900 cm->cm_data = malloc(cm->cm_length, M_MPRUSER, M_WAITOK |
901 M_ZERO);
902 cm->cm_flags = MPR_CM_FLAGS_DATAIN;
903 if (data->DataOutSize) {
904 cm->cm_flags |= MPR_CM_FLAGS_DATAOUT;
905 err = copyin(PTRIN(data->PtrDataOut),
906 cm->cm_data, data->DataOutSize);
907 } else if (data->DataDirection ==
908 MPR_PASS_THRU_DIRECTION_WRITE) {
909 cm->cm_flags = MPR_CM_FLAGS_DATAOUT;
910 err = copyin(PTRIN(data->PtrData),
911 cm->cm_data, data->DataSize);
912 }
913 if (err != 0)
914 mpr_dprint(sc, MPR_FAULT, "%s: failed to copy IOCTL "
915 "data from user space\n", __func__);
916 }
917 /*
918 * Set this flag only if processing a command that does not need an
919 * IEEE SGL. The CLI Tool within the Toolbox uses IEEE SGLs, so clear
920 * the flag only for that tool if processing a Toolbox function.
921 */
922 cm->cm_flags |= MPR_CM_FLAGS_SGE_SIMPLE;
923 for (i = 0; i < sizeof (ieee_sgl_func_list); i++) {
924 if (function == ieee_sgl_func_list[i]) {
925 if (function == MPI2_FUNCTION_TOOLBOX)
926 {
927 tool = (uint8_t)hdr->FunctionDependent1;
928 if (tool != MPI2_TOOLBOX_DIAGNOSTIC_CLI_TOOL)
929 break;
930 }
931 cm->cm_flags &= ~MPR_CM_FLAGS_SGE_SIMPLE;
932 break;
933 }
934 }
935 cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
936
937 if (function == MPI2_FUNCTION_NVME_ENCAPSULATED) {
938 nvme_encap_request =
939 (Mpi26NVMeEncapsulatedRequest_t *)cm->cm_req;
940 cm->cm_desc.Default.RequestFlags =
941 MPI26_REQ_DESCRIPT_FLAGS_PCIE_ENCAPSULATED;
942
943 /*
944 * Get the Physical Address of the sense buffer.
945 * Save the user's Error Response buffer address and use that
946 * field to hold the sense buffer address.
947 * Clear the internal sense buffer, which will potentially hold
948 * the Completion Queue Entry on return, or 0 if no Entry.
949 * Build the PRPs and set direction bits.
950 * Send the request.
951 */
952 cm->nvme_error_response =
953 (uint64_t *)(uintptr_t)(((uint64_t)nvme_encap_request->
954 ErrorResponseBaseAddress.High << 32) |
955 (uint64_t)nvme_encap_request->
956 ErrorResponseBaseAddress.Low);
957 nvme_encap_request->ErrorResponseBaseAddress.High =
958 htole32((uint32_t)((uint64_t)cm->cm_sense_busaddr >> 32));
959 nvme_encap_request->ErrorResponseBaseAddress.Low =
960 htole32(cm->cm_sense_busaddr);
961 memset(cm->cm_sense, 0, NVME_ERROR_RESPONSE_SIZE);
962 mpr_build_nvme_prp(sc, cm, nvme_encap_request, cm->cm_data,
963 data->DataSize, data->DataOutSize);
964 }
965
966 /*
967 * Set up Sense buffer and SGL offset for IO passthru. SCSI IO request
968 * uses SCSI IO or Fast Path SCSI IO descriptor.
969 */
970 if ((function == MPI2_FUNCTION_SCSI_IO_REQUEST) ||
971 (function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)) {
972 MPI2_SCSI_IO_REQUEST *scsi_io_req;
973
974 scsi_io_req = (MPI2_SCSI_IO_REQUEST *)hdr;
975 /*
976 * Put SGE for data and data_out buffer at the end of
977 * scsi_io_request message header (64 bytes in total).
978 * Following above SGEs, the residual space will be used by
979 * sense data.
980 */
981 scsi_io_req->SenseBufferLength = (uint8_t)(data->RequestSize -
982 64);
983 scsi_io_req->SenseBufferLowAddress =
984 htole32(cm->cm_sense_busaddr);
985
986 /*
987 * Set SGLOffset0 value. This is the number of dwords that SGL
988 * is offset from the beginning of MPI2_SCSI_IO_REQUEST struct.
989 */
990 scsi_io_req->SGLOffset0 = 24;
991
992 /*
993 * Setup descriptor info. RAID passthrough must use the
994 * default request descriptor which is already set, so if this
995 * is a SCSI IO request, change the descriptor to SCSI IO or
996 * Fast Path SCSI IO. Also, if this is a SCSI IO request,
997 * handle the reply in the mprsas_scsio_complete function.
998 */
999 if (function == MPI2_FUNCTION_SCSI_IO_REQUEST) {
1000 targ = mprsas_find_target_by_handle(sc->sassc, 0,
1001 scsi_io_req->DevHandle);
1002
1003 if (!targ) {
1004 printf("No Target found for handle %d\n",
1005 scsi_io_req->DevHandle);
1006 err = EINVAL;
1007 goto RetFreeUnlocked;
1008 }
1009
1010 if (targ->scsi_req_desc_type ==
1011 MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO) {
1012 cm->cm_desc.FastPathSCSIIO.RequestFlags =
1013 MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO;
1014 if (!sc->atomic_desc_capable) {
1015 cm->cm_desc.FastPathSCSIIO.DevHandle =
1016 scsi_io_req->DevHandle;
1017 }
1018 scsi_io_req->IoFlags |=
1019 MPI25_SCSIIO_IOFLAGS_FAST_PATH;
1020 } else {
1021 cm->cm_desc.SCSIIO.RequestFlags =
1022 MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
1023 if (!sc->atomic_desc_capable) {
1024 cm->cm_desc.SCSIIO.DevHandle =
1025 scsi_io_req->DevHandle;
1026 }
1027 }
1028
1029 /*
1030 * Make sure the DevHandle is not 0 because this is a
1031 * likely error.
1032 */
1033 if (scsi_io_req->DevHandle == 0) {
1034 err = EINVAL;
1035 goto RetFreeUnlocked;
1036 }
1037 }
1038 }
1039
1040 mpr_lock(sc);
1041
1042 err = mpr_wait_command(sc, &cm, 30, CAN_SLEEP);
1043
1044 if (err || (cm == NULL)) {
1045 mpr_printf(sc, "%s: invalid request: error %d\n", __func__,
1046 err);
1047 goto RetFree;
1048 }
1049
1050 /*
1051 * Sync the DMA data, if any. Then copy the data to user space.
1052 */
1053 if (cm->cm_data != NULL) {
1054 if (cm->cm_flags & MPR_CM_FLAGS_DATAIN)
1055 dir = BUS_DMASYNC_POSTREAD;
1056 else if (cm->cm_flags & MPR_CM_FLAGS_DATAOUT)
1057 dir = BUS_DMASYNC_POSTWRITE;
1058 bus_dmamap_sync(sc->buffer_dmat, cm->cm_dmamap, dir);
1059 bus_dmamap_unload(sc->buffer_dmat, cm->cm_dmamap);
1060
1061 if (cm->cm_flags & MPR_CM_FLAGS_DATAIN) {
1062 mpr_unlock(sc);
1063 err = copyout(cm->cm_data,
1064 PTRIN(data->PtrData), data->DataSize);
1065 mpr_lock(sc);
1066 if (err != 0)
1067 mpr_dprint(sc, MPR_FAULT, "%s: failed to copy "
1068 "IOCTL data to user space\n", __func__);
1069 }
1070 }
1071
1072 /*
1073 * Copy the reply data and sense data to user space.
1074 */
1075 if (err == 0 && cm->cm_reply != NULL) {
1076 rpl = (MPI2_DEFAULT_REPLY *)cm->cm_reply;
1077 sz = rpl->MsgLength * 4;
1078
1079 if (bootverbose && sz > data->ReplySize) {
1080 mpr_printf(sc, "%s: user reply buffer (%d) smaller "
1081 "than returned buffer (%d)\n", __func__,
1082 data->ReplySize, sz);
1083 }
1084 mpr_unlock(sc);
1085 err = copyout(cm->cm_reply, PTRIN(data->PtrReply),
1086 MIN(sz, data->ReplySize));
1087 if (err != 0)
1088 mpr_dprint(sc, MPR_FAULT, "%s: failed to copy "
1089 "IOCTL data to user space\n", __func__);
1090 mpr_lock(sc);
1091
1092 if (err == 0 &&
1093 (function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
1094 function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)) {
1095 if (((MPI2_SCSI_IO_REPLY *)rpl)->SCSIState &
1096 MPI2_SCSI_STATE_AUTOSENSE_VALID) {
1097 sense_len =
1098 MIN((le32toh(((MPI2_SCSI_IO_REPLY *)rpl)->
1099 SenseCount)), sizeof(struct
1100 scsi_sense_data));
1101 mpr_unlock(sc);
1102 err = copyout(cm->cm_sense,
1103 PTRIN(data->PtrReply +
1104 sizeof(MPI2_SCSI_IO_REPLY)), sense_len);
1105 if (err != 0)
1106 mpr_dprint(sc, MPR_FAULT,
1107 "%s: failed to copy IOCTL data to "
1108 "user space\n", __func__);
1109 mpr_lock(sc);
1110 }
1111 }
1112
1113 /*
1114 * Copy out the NVMe Error Reponse to user. The Error Response
1115 * buffer is given by the user, but a sense buffer is used to
1116 * get that data from the IOC. The user's
1117 * ErrorResponseBaseAddress is saved in the
1118 * 'nvme_error_response' field before the command because that
1119 * field is set to a sense buffer. When the command is
1120 * complete, the Error Response data from the IOC is copied to
1121 * that user address after it is checked for validity.
1122 * Also note that 'sense' buffers are not defined for
1123 * NVMe commands. Sense terminalogy is only used here so that
1124 * the same IOCTL structure and sense buffers can be used for
1125 * NVMe.
1126 */
1127 if (err == 0 && function == MPI2_FUNCTION_NVME_ENCAPSULATED) {
1128 if (cm->nvme_error_response == NULL) {
1129 mpr_dprint(sc, MPR_INFO, "NVMe Error Response "
1130 "buffer is NULL. Response data will not be "
1131 "returned.\n");
1132 mpr_unlock(sc);
1133 goto RetFreeUnlocked;
1134 }
1135
1136 nvme_error_reply =
1137 (Mpi26NVMeEncapsulatedErrorReply_t *)cm->cm_reply;
1138 sz = MIN(le32toh(nvme_error_reply->ErrorResponseCount),
1139 NVME_ERROR_RESPONSE_SIZE);
1140 mpr_unlock(sc);
1141 err = copyout(cm->cm_sense,
1142 (PTRIN(data->PtrReply +
1143 sizeof(MPI2_SCSI_IO_REPLY))), sz);
1144 if (err != 0)
1145 mpr_dprint(sc, MPR_FAULT,
1146 "%s: failed to copy IOCTL data to "
1147 "user space\n", __func__);
1148 mpr_lock(sc);
1149 }
1150 }
1151 mpr_unlock(sc);
1152
1153 RetFreeUnlocked:
1154 mpr_lock(sc);
1155
1156 RetFree:
1157 if (cm != NULL) {
1158 if (cm->cm_data)
1159 free(cm->cm_data, M_MPRUSER);
1160 mpr_free_command(sc, cm);
1161 }
1162 Ret:
1163 sc->mpr_flags &= ~MPR_FLAGS_BUSY;
1164 mpr_unlock(sc);
1165 free(req, M_MPRUSER);
1166
1167 return (err);
1168 }
1169
1170 static void
mpr_user_get_adapter_data(struct mpr_softc * sc,mpr_adapter_data_t * data)1171 mpr_user_get_adapter_data(struct mpr_softc *sc, mpr_adapter_data_t *data)
1172 {
1173 Mpi2ConfigReply_t mpi_reply;
1174 Mpi2BiosPage3_t config_page;
1175
1176 /*
1177 * Use the PCI interface functions to get the Bus, Device, and Function
1178 * information.
1179 */
1180 data->PciInformation.u.bits.BusNumber = pci_get_bus(sc->mpr_dev);
1181 data->PciInformation.u.bits.DeviceNumber = pci_get_slot(sc->mpr_dev);
1182 data->PciInformation.u.bits.FunctionNumber =
1183 pci_get_function(sc->mpr_dev);
1184
1185 /*
1186 * Get the FW version that should already be saved in IOC Facts.
1187 */
1188 data->MpiFirmwareVersion = sc->facts->FWVersion.Word;
1189
1190 /*
1191 * General device info.
1192 */
1193 if (sc->mpr_flags & MPR_FLAGS_GEN35_IOC)
1194 data->AdapterType = MPRIOCTL_ADAPTER_TYPE_SAS35;
1195 else
1196 data->AdapterType = MPRIOCTL_ADAPTER_TYPE_SAS3;
1197 data->PCIDeviceHwId = pci_get_device(sc->mpr_dev);
1198 data->PCIDeviceHwRev = pci_read_config(sc->mpr_dev, PCIR_REVID, 1);
1199 data->SubSystemId = pci_get_subdevice(sc->mpr_dev);
1200 data->SubsystemVendorId = pci_get_subvendor(sc->mpr_dev);
1201
1202 /*
1203 * Get the driver version.
1204 */
1205 strcpy((char *)&data->DriverVersion[0], MPR_DRIVER_VERSION);
1206
1207 /*
1208 * Need to get BIOS Config Page 3 for the BIOS Version.
1209 */
1210 data->BiosVersion = 0;
1211 mpr_lock(sc);
1212 if (mpr_config_get_bios_pg3(sc, &mpi_reply, &config_page))
1213 printf("%s: Error while retrieving BIOS Version\n", __func__);
1214 else
1215 data->BiosVersion = config_page.BiosVersion;
1216 mpr_unlock(sc);
1217 }
1218
1219 static void
mpr_user_read_pci_info(struct mpr_softc * sc,mpr_pci_info_t * data)1220 mpr_user_read_pci_info(struct mpr_softc *sc, mpr_pci_info_t *data)
1221 {
1222 int i;
1223
1224 /*
1225 * Use the PCI interface functions to get the Bus, Device, and Function
1226 * information.
1227 */
1228 data->BusNumber = pci_get_bus(sc->mpr_dev);
1229 data->DeviceNumber = pci_get_slot(sc->mpr_dev);
1230 data->FunctionNumber = pci_get_function(sc->mpr_dev);
1231
1232 /*
1233 * Now get the interrupt vector and the pci header. The vector can
1234 * only be 0 right now. The header is the first 256 bytes of config
1235 * space.
1236 */
1237 data->InterruptVector = 0;
1238 for (i = 0; i < sizeof (data->PciHeader); i++) {
1239 data->PciHeader[i] = pci_read_config(sc->mpr_dev, i, 1);
1240 }
1241 }
1242
1243 static uint8_t
mpr_get_fw_diag_buffer_number(struct mpr_softc * sc,uint32_t unique_id)1244 mpr_get_fw_diag_buffer_number(struct mpr_softc *sc, uint32_t unique_id)
1245 {
1246 uint8_t index;
1247
1248 for (index = 0; index < MPI2_DIAG_BUF_TYPE_COUNT; index++) {
1249 if (sc->fw_diag_buffer_list[index].unique_id == unique_id) {
1250 return (index);
1251 }
1252 }
1253
1254 return (MPR_FW_DIAGNOSTIC_UID_NOT_FOUND);
1255 }
1256
1257 static int
mpr_post_fw_diag_buffer(struct mpr_softc * sc,mpr_fw_diagnostic_buffer_t * pBuffer,uint32_t * return_code)1258 mpr_post_fw_diag_buffer(struct mpr_softc *sc,
1259 mpr_fw_diagnostic_buffer_t *pBuffer, uint32_t *return_code)
1260 {
1261 MPI2_DIAG_BUFFER_POST_REQUEST *req;
1262 MPI2_DIAG_BUFFER_POST_REPLY *reply;
1263 struct mpr_command *cm = NULL;
1264 int i, status;
1265
1266 /*
1267 * If buffer is not enabled, just leave.
1268 */
1269 *return_code = MPR_FW_DIAG_ERROR_POST_FAILED;
1270 if (!pBuffer->enabled) {
1271 return (MPR_DIAG_FAILURE);
1272 }
1273
1274 /*
1275 * Clear some flags initially.
1276 */
1277 pBuffer->force_release = FALSE;
1278 pBuffer->valid_data = FALSE;
1279 pBuffer->owned_by_firmware = FALSE;
1280
1281 /*
1282 * Get a command.
1283 */
1284 cm = mpr_alloc_command(sc);
1285 if (cm == NULL) {
1286 mpr_printf(sc, "%s: no mpr requests\n", __func__);
1287 return (MPR_DIAG_FAILURE);
1288 }
1289
1290 /*
1291 * Build the request for releasing the FW Diag Buffer and send it.
1292 */
1293 req = (MPI2_DIAG_BUFFER_POST_REQUEST *)cm->cm_req;
1294 req->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
1295 req->BufferType = pBuffer->buffer_type;
1296 req->ExtendedType = pBuffer->extended_type;
1297 req->BufferLength = pBuffer->size;
1298 for (i = 0; i < (sizeof(req->ProductSpecific) / 4); i++)
1299 req->ProductSpecific[i] = pBuffer->product_specific[i];
1300 mpr_from_u64(sc->fw_diag_busaddr, &req->BufferAddress);
1301 cm->cm_data = NULL;
1302 cm->cm_length = 0;
1303 cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
1304 cm->cm_complete_data = NULL;
1305
1306 /*
1307 * Send command synchronously.
1308 */
1309 status = mpr_wait_command(sc, &cm, 30, CAN_SLEEP);
1310 if (status || (cm == NULL)) {
1311 mpr_printf(sc, "%s: invalid request: error %d\n", __func__,
1312 status);
1313 status = MPR_DIAG_FAILURE;
1314 goto done;
1315 }
1316
1317 /*
1318 * Process POST reply.
1319 */
1320 reply = (MPI2_DIAG_BUFFER_POST_REPLY *)cm->cm_reply;
1321 if (reply == NULL) {
1322 mpr_printf(sc, "%s: reply is NULL, probably due to "
1323 "reinitialization\n", __func__);
1324 status = MPR_DIAG_FAILURE;
1325 goto done;
1326 }
1327
1328 if ((le16toh(reply->IOCStatus) & MPI2_IOCSTATUS_MASK) !=
1329 MPI2_IOCSTATUS_SUCCESS) {
1330 status = MPR_DIAG_FAILURE;
1331 mpr_dprint(sc, MPR_FAULT, "%s: post of FW Diag Buffer failed "
1332 "with IOCStatus = 0x%x, IOCLogInfo = 0x%x and "
1333 "TransferLength = 0x%x\n", __func__,
1334 le16toh(reply->IOCStatus), le32toh(reply->IOCLogInfo),
1335 le32toh(reply->TransferLength));
1336 goto done;
1337 }
1338
1339 /*
1340 * Post was successful.
1341 */
1342 pBuffer->valid_data = TRUE;
1343 pBuffer->owned_by_firmware = TRUE;
1344 *return_code = MPR_FW_DIAG_ERROR_SUCCESS;
1345 status = MPR_DIAG_SUCCESS;
1346
1347 done:
1348 if (cm != NULL)
1349 mpr_free_command(sc, cm);
1350 return (status);
1351 }
1352
1353 static int
mpr_release_fw_diag_buffer(struct mpr_softc * sc,mpr_fw_diagnostic_buffer_t * pBuffer,uint32_t * return_code,uint32_t diag_type)1354 mpr_release_fw_diag_buffer(struct mpr_softc *sc,
1355 mpr_fw_diagnostic_buffer_t *pBuffer, uint32_t *return_code,
1356 uint32_t diag_type)
1357 {
1358 MPI2_DIAG_RELEASE_REQUEST *req;
1359 MPI2_DIAG_RELEASE_REPLY *reply;
1360 struct mpr_command *cm = NULL;
1361 int status;
1362
1363 /*
1364 * If buffer is not enabled, just leave.
1365 */
1366 *return_code = MPR_FW_DIAG_ERROR_RELEASE_FAILED;
1367 if (!pBuffer->enabled) {
1368 mpr_dprint(sc, MPR_USER, "%s: This buffer type is not "
1369 "supported by the IOC", __func__);
1370 return (MPR_DIAG_FAILURE);
1371 }
1372
1373 /*
1374 * Clear some flags initially.
1375 */
1376 pBuffer->force_release = FALSE;
1377 pBuffer->valid_data = FALSE;
1378 pBuffer->owned_by_firmware = FALSE;
1379
1380 /*
1381 * Get a command.
1382 */
1383 cm = mpr_alloc_command(sc);
1384 if (cm == NULL) {
1385 mpr_printf(sc, "%s: no mpr requests\n", __func__);
1386 return (MPR_DIAG_FAILURE);
1387 }
1388
1389 /*
1390 * Build the request for releasing the FW Diag Buffer and send it.
1391 */
1392 req = (MPI2_DIAG_RELEASE_REQUEST *)cm->cm_req;
1393 req->Function = MPI2_FUNCTION_DIAG_RELEASE;
1394 req->BufferType = pBuffer->buffer_type;
1395 cm->cm_data = NULL;
1396 cm->cm_length = 0;
1397 cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
1398 cm->cm_complete_data = NULL;
1399
1400 /*
1401 * Send command synchronously.
1402 */
1403 status = mpr_wait_command(sc, &cm, 30, CAN_SLEEP);
1404 if (status || (cm == NULL)) {
1405 mpr_printf(sc, "%s: invalid request: error %d\n", __func__,
1406 status);
1407 status = MPR_DIAG_FAILURE;
1408 goto done;
1409 }
1410
1411 /*
1412 * Process RELEASE reply.
1413 */
1414 reply = (MPI2_DIAG_RELEASE_REPLY *)cm->cm_reply;
1415 if (reply == NULL) {
1416 mpr_printf(sc, "%s: reply is NULL, probably due to "
1417 "reinitialization\n", __func__);
1418 status = MPR_DIAG_FAILURE;
1419 goto done;
1420 }
1421 if (((le16toh(reply->IOCStatus) & MPI2_IOCSTATUS_MASK) !=
1422 MPI2_IOCSTATUS_SUCCESS) || pBuffer->owned_by_firmware) {
1423 status = MPR_DIAG_FAILURE;
1424 mpr_dprint(sc, MPR_FAULT, "%s: release of FW Diag Buffer "
1425 "failed with IOCStatus = 0x%x and IOCLogInfo = 0x%x\n",
1426 __func__, le16toh(reply->IOCStatus),
1427 le32toh(reply->IOCLogInfo));
1428 goto done;
1429 }
1430
1431 /*
1432 * Release was successful.
1433 */
1434 *return_code = MPR_FW_DIAG_ERROR_SUCCESS;
1435 status = MPR_DIAG_SUCCESS;
1436
1437 /*
1438 * If this was for an UNREGISTER diag type command, clear the unique ID.
1439 */
1440 if (diag_type == MPR_FW_DIAG_TYPE_UNREGISTER) {
1441 pBuffer->unique_id = MPR_FW_DIAG_INVALID_UID;
1442 }
1443
1444 done:
1445 if (cm != NULL)
1446 mpr_free_command(sc, cm);
1447
1448 return (status);
1449 }
1450
1451 static int
mpr_diag_register(struct mpr_softc * sc,mpr_fw_diag_register_t * diag_register,uint32_t * return_code)1452 mpr_diag_register(struct mpr_softc *sc, mpr_fw_diag_register_t *diag_register,
1453 uint32_t *return_code)
1454 {
1455 bus_dma_template_t t;
1456 mpr_fw_diagnostic_buffer_t *pBuffer;
1457 struct mpr_busdma_context *ctx;
1458 uint8_t extended_type, buffer_type, i;
1459 uint32_t buffer_size;
1460 uint32_t unique_id;
1461 int status;
1462 int error;
1463
1464 extended_type = diag_register->ExtendedType;
1465 buffer_type = diag_register->BufferType;
1466 buffer_size = diag_register->RequestedBufferSize;
1467 unique_id = diag_register->UniqueId;
1468 ctx = NULL;
1469 error = 0;
1470
1471 /*
1472 * Check for valid buffer type
1473 */
1474 if (buffer_type >= MPI2_DIAG_BUF_TYPE_COUNT) {
1475 *return_code = MPR_FW_DIAG_ERROR_INVALID_PARAMETER;
1476 return (MPR_DIAG_FAILURE);
1477 }
1478
1479 /*
1480 * Get the current buffer and look up the unique ID. The unique ID
1481 * should not be found. If it is, the ID is already in use.
1482 */
1483 i = mpr_get_fw_diag_buffer_number(sc, unique_id);
1484 pBuffer = &sc->fw_diag_buffer_list[buffer_type];
1485 if (i != MPR_FW_DIAGNOSTIC_UID_NOT_FOUND) {
1486 *return_code = MPR_FW_DIAG_ERROR_INVALID_UID;
1487 return (MPR_DIAG_FAILURE);
1488 }
1489
1490 /*
1491 * The buffer's unique ID should not be registered yet, and the given
1492 * unique ID cannot be 0.
1493 */
1494 if ((pBuffer->unique_id != MPR_FW_DIAG_INVALID_UID) ||
1495 (unique_id == MPR_FW_DIAG_INVALID_UID)) {
1496 *return_code = MPR_FW_DIAG_ERROR_INVALID_UID;
1497 return (MPR_DIAG_FAILURE);
1498 }
1499
1500 /*
1501 * If this buffer is already posted as immediate, just change owner.
1502 */
1503 if (pBuffer->immediate && pBuffer->owned_by_firmware &&
1504 (pBuffer->unique_id == MPR_FW_DIAG_INVALID_UID)) {
1505 pBuffer->immediate = FALSE;
1506 pBuffer->unique_id = unique_id;
1507 return (MPR_DIAG_SUCCESS);
1508 }
1509
1510 /*
1511 * Post a new buffer after checking if it's enabled. The DMA buffer
1512 * that is allocated will be contiguous (nsegments = 1).
1513 */
1514 if (!pBuffer->enabled) {
1515 *return_code = MPR_FW_DIAG_ERROR_NO_BUFFER;
1516 return (MPR_DIAG_FAILURE);
1517 }
1518 bus_dma_template_init(&t, sc->mpr_parent_dmat);
1519 BUS_DMA_TEMPLATE_FILL(&t, BD_LOWADDR(BUS_SPACE_MAXADDR_32BIT),
1520 BD_MAXSIZE(buffer_size), BD_MAXSEGSIZE(buffer_size),
1521 BD_NSEGMENTS(1));
1522 if (bus_dma_template_tag(&t, &sc->fw_diag_dmat)) {
1523 mpr_dprint(sc, MPR_ERROR,
1524 "Cannot allocate FW diag buffer DMA tag\n");
1525 *return_code = MPR_FW_DIAG_ERROR_NO_BUFFER;
1526 status = MPR_DIAG_FAILURE;
1527 goto bailout;
1528 }
1529 if (bus_dmamem_alloc(sc->fw_diag_dmat, (void **)&sc->fw_diag_buffer,
1530 BUS_DMA_NOWAIT, &sc->fw_diag_map)) {
1531 mpr_dprint(sc, MPR_ERROR,
1532 "Cannot allocate FW diag buffer memory\n");
1533 *return_code = MPR_FW_DIAG_ERROR_NO_BUFFER;
1534 status = MPR_DIAG_FAILURE;
1535 goto bailout;
1536 }
1537 bzero(sc->fw_diag_buffer, buffer_size);
1538
1539 ctx = malloc(sizeof(*ctx), M_MPR, M_WAITOK | M_ZERO);
1540 ctx->addr = &sc->fw_diag_busaddr;
1541 ctx->buffer_dmat = sc->fw_diag_dmat;
1542 ctx->buffer_dmamap = sc->fw_diag_map;
1543 ctx->softc = sc;
1544 error = bus_dmamap_load(sc->fw_diag_dmat, sc->fw_diag_map,
1545 sc->fw_diag_buffer, buffer_size, mpr_memaddr_wait_cb,
1546 ctx, 0);
1547 if (error == EINPROGRESS) {
1548 /* XXX KDM */
1549 device_printf(sc->mpr_dev, "%s: Deferred bus_dmamap_load\n",
1550 __func__);
1551 /*
1552 * Wait for the load to complete. If we're interrupted,
1553 * bail out.
1554 */
1555 mpr_lock(sc);
1556 if (ctx->completed == 0) {
1557 error = msleep(ctx, &sc->mpr_mtx, PCATCH, "mprwait", 0);
1558 if (error != 0) {
1559 /*
1560 * We got an error from msleep(9). This is
1561 * most likely due to a signal. Tell
1562 * mpr_memaddr_wait_cb() that we've abandoned
1563 * the context, so it needs to clean up when
1564 * it is called.
1565 */
1566 ctx->abandoned = 1;
1567
1568 /* The callback will free this memory */
1569 ctx = NULL;
1570 mpr_unlock(sc);
1571
1572 device_printf(sc->mpr_dev, "Cannot "
1573 "bus_dmamap_load FW diag buffer, error = "
1574 "%d returned from msleep\n", error);
1575 *return_code = MPR_FW_DIAG_ERROR_NO_BUFFER;
1576 status = MPR_DIAG_FAILURE;
1577 goto bailout;
1578 }
1579 }
1580 mpr_unlock(sc);
1581 }
1582
1583 if ((error != 0) || (ctx->error != 0)) {
1584 device_printf(sc->mpr_dev, "Cannot bus_dmamap_load FW diag "
1585 "buffer, %serror = %d\n", error ? "" : "callback ",
1586 error ? error : ctx->error);
1587 *return_code = MPR_FW_DIAG_ERROR_NO_BUFFER;
1588 status = MPR_DIAG_FAILURE;
1589 goto bailout;
1590 }
1591
1592 bus_dmamap_sync(sc->fw_diag_dmat, sc->fw_diag_map, BUS_DMASYNC_PREREAD);
1593
1594 pBuffer->size = buffer_size;
1595
1596 /*
1597 * Copy the given info to the diag buffer and post the buffer.
1598 */
1599 pBuffer->buffer_type = buffer_type;
1600 pBuffer->immediate = FALSE;
1601 if (buffer_type == MPI2_DIAG_BUF_TYPE_TRACE) {
1602 for (i = 0; i < (sizeof (pBuffer->product_specific) / 4);
1603 i++) {
1604 pBuffer->product_specific[i] =
1605 diag_register->ProductSpecific[i];
1606 }
1607 }
1608 pBuffer->extended_type = extended_type;
1609 pBuffer->unique_id = unique_id;
1610 status = mpr_post_fw_diag_buffer(sc, pBuffer, return_code);
1611
1612 bailout:
1613
1614 /*
1615 * In case there was a failure, free the DMA buffer.
1616 */
1617 if (status == MPR_DIAG_FAILURE) {
1618 if (sc->fw_diag_busaddr != 0) {
1619 bus_dmamap_unload(sc->fw_diag_dmat, sc->fw_diag_map);
1620 sc->fw_diag_busaddr = 0;
1621 }
1622 if (sc->fw_diag_buffer != NULL) {
1623 bus_dmamem_free(sc->fw_diag_dmat, sc->fw_diag_buffer,
1624 sc->fw_diag_map);
1625 sc->fw_diag_buffer = NULL;
1626 }
1627 if (sc->fw_diag_dmat != NULL) {
1628 bus_dma_tag_destroy(sc->fw_diag_dmat);
1629 sc->fw_diag_dmat = NULL;
1630 }
1631 }
1632
1633 if (ctx != NULL)
1634 free(ctx, M_MPR);
1635
1636 return (status);
1637 }
1638
1639 static int
mpr_diag_unregister(struct mpr_softc * sc,mpr_fw_diag_unregister_t * diag_unregister,uint32_t * return_code)1640 mpr_diag_unregister(struct mpr_softc *sc,
1641 mpr_fw_diag_unregister_t *diag_unregister, uint32_t *return_code)
1642 {
1643 mpr_fw_diagnostic_buffer_t *pBuffer;
1644 uint8_t i;
1645 uint32_t unique_id;
1646 int status;
1647
1648 unique_id = diag_unregister->UniqueId;
1649
1650 /*
1651 * Get the current buffer and look up the unique ID. The unique ID
1652 * should be there.
1653 */
1654 i = mpr_get_fw_diag_buffer_number(sc, unique_id);
1655 if (i == MPR_FW_DIAGNOSTIC_UID_NOT_FOUND) {
1656 *return_code = MPR_FW_DIAG_ERROR_INVALID_UID;
1657 return (MPR_DIAG_FAILURE);
1658 }
1659
1660 pBuffer = &sc->fw_diag_buffer_list[i];
1661
1662 /*
1663 * Try to release the buffer from FW before freeing it. If release
1664 * fails, don't free the DMA buffer in case FW tries to access it
1665 * later. If buffer is not owned by firmware, can't release it.
1666 */
1667 if (!pBuffer->owned_by_firmware) {
1668 status = MPR_DIAG_SUCCESS;
1669 } else {
1670 status = mpr_release_fw_diag_buffer(sc, pBuffer, return_code,
1671 MPR_FW_DIAG_TYPE_UNREGISTER);
1672 }
1673
1674 /*
1675 * At this point, return the current status no matter what happens with
1676 * the DMA buffer.
1677 */
1678 pBuffer->unique_id = MPR_FW_DIAG_INVALID_UID;
1679 if (status == MPR_DIAG_SUCCESS) {
1680 if (sc->fw_diag_busaddr != 0) {
1681 bus_dmamap_unload(sc->fw_diag_dmat, sc->fw_diag_map);
1682 sc->fw_diag_busaddr = 0;
1683 }
1684 if (sc->fw_diag_buffer != NULL) {
1685 bus_dmamem_free(sc->fw_diag_dmat, sc->fw_diag_buffer,
1686 sc->fw_diag_map);
1687 sc->fw_diag_buffer = NULL;
1688 }
1689 if (sc->fw_diag_dmat != NULL) {
1690 bus_dma_tag_destroy(sc->fw_diag_dmat);
1691 sc->fw_diag_dmat = NULL;
1692 }
1693 }
1694
1695 return (status);
1696 }
1697
1698 static int
mpr_diag_query(struct mpr_softc * sc,mpr_fw_diag_query_t * diag_query,uint32_t * return_code)1699 mpr_diag_query(struct mpr_softc *sc, mpr_fw_diag_query_t *diag_query,
1700 uint32_t *return_code)
1701 {
1702 mpr_fw_diagnostic_buffer_t *pBuffer;
1703 uint8_t i;
1704 uint32_t unique_id;
1705
1706 unique_id = diag_query->UniqueId;
1707
1708 /*
1709 * If ID is valid, query on ID.
1710 * If ID is invalid, query on buffer type.
1711 */
1712 if (unique_id == MPR_FW_DIAG_INVALID_UID) {
1713 i = diag_query->BufferType;
1714 if (i >= MPI2_DIAG_BUF_TYPE_COUNT) {
1715 *return_code = MPR_FW_DIAG_ERROR_INVALID_UID;
1716 return (MPR_DIAG_FAILURE);
1717 }
1718 } else {
1719 i = mpr_get_fw_diag_buffer_number(sc, unique_id);
1720 if (i == MPR_FW_DIAGNOSTIC_UID_NOT_FOUND) {
1721 *return_code = MPR_FW_DIAG_ERROR_INVALID_UID;
1722 return (MPR_DIAG_FAILURE);
1723 }
1724 }
1725
1726 /*
1727 * Fill query structure with the diag buffer info.
1728 */
1729 pBuffer = &sc->fw_diag_buffer_list[i];
1730 diag_query->BufferType = pBuffer->buffer_type;
1731 diag_query->ExtendedType = pBuffer->extended_type;
1732 if (diag_query->BufferType == MPI2_DIAG_BUF_TYPE_TRACE) {
1733 for (i = 0; i < (sizeof(diag_query->ProductSpecific) / 4);
1734 i++) {
1735 diag_query->ProductSpecific[i] =
1736 pBuffer->product_specific[i];
1737 }
1738 }
1739 diag_query->TotalBufferSize = pBuffer->size;
1740 diag_query->DriverAddedBufferSize = 0;
1741 diag_query->UniqueId = pBuffer->unique_id;
1742 diag_query->ApplicationFlags = 0;
1743 diag_query->DiagnosticFlags = 0;
1744
1745 /*
1746 * Set/Clear application flags
1747 */
1748 if (pBuffer->immediate) {
1749 diag_query->ApplicationFlags &= ~MPR_FW_DIAG_FLAG_APP_OWNED;
1750 } else {
1751 diag_query->ApplicationFlags |= MPR_FW_DIAG_FLAG_APP_OWNED;
1752 }
1753 if (pBuffer->valid_data || pBuffer->owned_by_firmware) {
1754 diag_query->ApplicationFlags |= MPR_FW_DIAG_FLAG_BUFFER_VALID;
1755 } else {
1756 diag_query->ApplicationFlags &= ~MPR_FW_DIAG_FLAG_BUFFER_VALID;
1757 }
1758 if (pBuffer->owned_by_firmware) {
1759 diag_query->ApplicationFlags |=
1760 MPR_FW_DIAG_FLAG_FW_BUFFER_ACCESS;
1761 } else {
1762 diag_query->ApplicationFlags &=
1763 ~MPR_FW_DIAG_FLAG_FW_BUFFER_ACCESS;
1764 }
1765
1766 return (MPR_DIAG_SUCCESS);
1767 }
1768
1769 static int
mpr_diag_read_buffer(struct mpr_softc * sc,mpr_diag_read_buffer_t * diag_read_buffer,uint8_t * ioctl_buf,uint32_t * return_code)1770 mpr_diag_read_buffer(struct mpr_softc *sc,
1771 mpr_diag_read_buffer_t *diag_read_buffer, uint8_t *ioctl_buf,
1772 uint32_t *return_code)
1773 {
1774 mpr_fw_diagnostic_buffer_t *pBuffer;
1775 uint8_t i, *pData;
1776 uint32_t unique_id;
1777 int status;
1778
1779 unique_id = diag_read_buffer->UniqueId;
1780
1781 /*
1782 * Get the current buffer and look up the unique ID. The unique ID
1783 * should be there.
1784 */
1785 i = mpr_get_fw_diag_buffer_number(sc, unique_id);
1786 if (i == MPR_FW_DIAGNOSTIC_UID_NOT_FOUND) {
1787 *return_code = MPR_FW_DIAG_ERROR_INVALID_UID;
1788 return (MPR_DIAG_FAILURE);
1789 }
1790
1791 pBuffer = &sc->fw_diag_buffer_list[i];
1792
1793 /*
1794 * Make sure requested read is within limits
1795 */
1796 if (diag_read_buffer->StartingOffset + diag_read_buffer->BytesToRead >
1797 pBuffer->size) {
1798 *return_code = MPR_FW_DIAG_ERROR_INVALID_PARAMETER;
1799 return (MPR_DIAG_FAILURE);
1800 }
1801
1802 /* Sync the DMA map before we copy to userland. */
1803 bus_dmamap_sync(sc->fw_diag_dmat, sc->fw_diag_map,
1804 BUS_DMASYNC_POSTREAD);
1805
1806 /*
1807 * Copy the requested data from DMA to the diag_read_buffer. The DMA
1808 * buffer that was allocated is one contiguous buffer.
1809 */
1810 pData = (uint8_t *)(sc->fw_diag_buffer +
1811 diag_read_buffer->StartingOffset);
1812 if (copyout(pData, ioctl_buf, diag_read_buffer->BytesToRead) != 0)
1813 return (MPR_DIAG_FAILURE);
1814 diag_read_buffer->Status = 0;
1815
1816 /*
1817 * Set or clear the Force Release flag.
1818 */
1819 if (pBuffer->force_release) {
1820 diag_read_buffer->Flags |= MPR_FW_DIAG_FLAG_FORCE_RELEASE;
1821 } else {
1822 diag_read_buffer->Flags &= ~MPR_FW_DIAG_FLAG_FORCE_RELEASE;
1823 }
1824
1825 /*
1826 * If buffer is to be reregistered, make sure it's not already owned by
1827 * firmware first.
1828 */
1829 status = MPR_DIAG_SUCCESS;
1830 if (!pBuffer->owned_by_firmware) {
1831 if (diag_read_buffer->Flags & MPR_FW_DIAG_FLAG_REREGISTER) {
1832 status = mpr_post_fw_diag_buffer(sc, pBuffer,
1833 return_code);
1834 }
1835 }
1836
1837 return (status);
1838 }
1839
1840 static int
mpr_diag_release(struct mpr_softc * sc,mpr_fw_diag_release_t * diag_release,uint32_t * return_code)1841 mpr_diag_release(struct mpr_softc *sc, mpr_fw_diag_release_t *diag_release,
1842 uint32_t *return_code)
1843 {
1844 mpr_fw_diagnostic_buffer_t *pBuffer;
1845 uint8_t i;
1846 uint32_t unique_id;
1847 int status;
1848
1849 unique_id = diag_release->UniqueId;
1850
1851 /*
1852 * Get the current buffer and look up the unique ID. The unique ID
1853 * should be there.
1854 */
1855 i = mpr_get_fw_diag_buffer_number(sc, unique_id);
1856 if (i == MPR_FW_DIAGNOSTIC_UID_NOT_FOUND) {
1857 *return_code = MPR_FW_DIAG_ERROR_INVALID_UID;
1858 return (MPR_DIAG_FAILURE);
1859 }
1860
1861 pBuffer = &sc->fw_diag_buffer_list[i];
1862
1863 /*
1864 * If buffer is not owned by firmware, it's already been released.
1865 */
1866 if (!pBuffer->owned_by_firmware) {
1867 *return_code = MPR_FW_DIAG_ERROR_ALREADY_RELEASED;
1868 return (MPR_DIAG_FAILURE);
1869 }
1870
1871 /*
1872 * Release the buffer.
1873 */
1874 status = mpr_release_fw_diag_buffer(sc, pBuffer, return_code,
1875 MPR_FW_DIAG_TYPE_RELEASE);
1876 return (status);
1877 }
1878
1879 static int
mpr_do_diag_action(struct mpr_softc * sc,uint32_t action,uint8_t * diag_action,uint32_t length,uint32_t * return_code)1880 mpr_do_diag_action(struct mpr_softc *sc, uint32_t action, uint8_t *diag_action,
1881 uint32_t length, uint32_t *return_code)
1882 {
1883 mpr_fw_diag_register_t diag_register;
1884 mpr_fw_diag_unregister_t diag_unregister;
1885 mpr_fw_diag_query_t diag_query;
1886 mpr_diag_read_buffer_t diag_read_buffer;
1887 mpr_fw_diag_release_t diag_release;
1888 int status = MPR_DIAG_SUCCESS;
1889 uint32_t original_return_code;
1890
1891 original_return_code = *return_code;
1892 *return_code = MPR_FW_DIAG_ERROR_SUCCESS;
1893
1894 switch (action) {
1895 case MPR_FW_DIAG_TYPE_REGISTER:
1896 if (!length) {
1897 *return_code =
1898 MPR_FW_DIAG_ERROR_INVALID_PARAMETER;
1899 status = MPR_DIAG_FAILURE;
1900 break;
1901 }
1902 if (copyin(diag_action, &diag_register,
1903 sizeof(diag_register)) != 0)
1904 return (MPR_DIAG_FAILURE);
1905 status = mpr_diag_register(sc, &diag_register,
1906 return_code);
1907 break;
1908
1909 case MPR_FW_DIAG_TYPE_UNREGISTER:
1910 if (length < sizeof(diag_unregister)) {
1911 *return_code =
1912 MPR_FW_DIAG_ERROR_INVALID_PARAMETER;
1913 status = MPR_DIAG_FAILURE;
1914 break;
1915 }
1916 if (copyin(diag_action, &diag_unregister,
1917 sizeof(diag_unregister)) != 0)
1918 return (MPR_DIAG_FAILURE);
1919 status = mpr_diag_unregister(sc, &diag_unregister,
1920 return_code);
1921 break;
1922
1923 case MPR_FW_DIAG_TYPE_QUERY:
1924 if (length < sizeof (diag_query)) {
1925 *return_code =
1926 MPR_FW_DIAG_ERROR_INVALID_PARAMETER;
1927 status = MPR_DIAG_FAILURE;
1928 break;
1929 }
1930 if (copyin(diag_action, &diag_query, sizeof(diag_query))
1931 != 0)
1932 return (MPR_DIAG_FAILURE);
1933 status = mpr_diag_query(sc, &diag_query, return_code);
1934 if (status == MPR_DIAG_SUCCESS)
1935 if (copyout(&diag_query, diag_action,
1936 sizeof (diag_query)) != 0)
1937 return (MPR_DIAG_FAILURE);
1938 break;
1939
1940 case MPR_FW_DIAG_TYPE_READ_BUFFER:
1941 if (copyin(diag_action, &diag_read_buffer,
1942 sizeof(diag_read_buffer)) != 0)
1943 return (MPR_DIAG_FAILURE);
1944 if (length < diag_read_buffer.BytesToRead) {
1945 *return_code =
1946 MPR_FW_DIAG_ERROR_INVALID_PARAMETER;
1947 status = MPR_DIAG_FAILURE;
1948 break;
1949 }
1950 status = mpr_diag_read_buffer(sc, &diag_read_buffer,
1951 PTRIN(diag_read_buffer.PtrDataBuffer),
1952 return_code);
1953 if (status == MPR_DIAG_SUCCESS) {
1954 if (copyout(&diag_read_buffer, diag_action,
1955 sizeof(diag_read_buffer) -
1956 sizeof(diag_read_buffer.PtrDataBuffer)) !=
1957 0)
1958 return (MPR_DIAG_FAILURE);
1959 }
1960 break;
1961
1962 case MPR_FW_DIAG_TYPE_RELEASE:
1963 if (length < sizeof(diag_release)) {
1964 *return_code =
1965 MPR_FW_DIAG_ERROR_INVALID_PARAMETER;
1966 status = MPR_DIAG_FAILURE;
1967 break;
1968 }
1969 if (copyin(diag_action, &diag_release,
1970 sizeof(diag_release)) != 0)
1971 return (MPR_DIAG_FAILURE);
1972 status = mpr_diag_release(sc, &diag_release,
1973 return_code);
1974 break;
1975
1976 default:
1977 *return_code = MPR_FW_DIAG_ERROR_INVALID_PARAMETER;
1978 status = MPR_DIAG_FAILURE;
1979 break;
1980 }
1981
1982 if ((status == MPR_DIAG_FAILURE) &&
1983 (original_return_code == MPR_FW_DIAG_NEW) &&
1984 (*return_code != MPR_FW_DIAG_ERROR_SUCCESS))
1985 status = MPR_DIAG_SUCCESS;
1986
1987 return (status);
1988 }
1989
1990 static int
mpr_user_diag_action(struct mpr_softc * sc,mpr_diag_action_t * data)1991 mpr_user_diag_action(struct mpr_softc *sc, mpr_diag_action_t *data)
1992 {
1993 int status;
1994
1995 /*
1996 * Only allow one diag action at one time.
1997 */
1998 if (sc->mpr_flags & MPR_FLAGS_BUSY) {
1999 mpr_dprint(sc, MPR_USER, "%s: Only one FW diag command "
2000 "allowed at a single time.", __func__);
2001 return (EBUSY);
2002 }
2003 sc->mpr_flags |= MPR_FLAGS_BUSY;
2004
2005 /*
2006 * Send diag action request
2007 */
2008 if (data->Action == MPR_FW_DIAG_TYPE_REGISTER ||
2009 data->Action == MPR_FW_DIAG_TYPE_UNREGISTER ||
2010 data->Action == MPR_FW_DIAG_TYPE_QUERY ||
2011 data->Action == MPR_FW_DIAG_TYPE_READ_BUFFER ||
2012 data->Action == MPR_FW_DIAG_TYPE_RELEASE) {
2013 status = mpr_do_diag_action(sc, data->Action,
2014 PTRIN(data->PtrDiagAction), data->Length,
2015 &data->ReturnCode);
2016 } else
2017 status = EINVAL;
2018
2019 sc->mpr_flags &= ~MPR_FLAGS_BUSY;
2020 return (status);
2021 }
2022
2023 /*
2024 * Copy the event recording mask and the event queue size out. For
2025 * clarification, the event recording mask (events_to_record) is not the same
2026 * thing as the event mask (event_mask). events_to_record has a bit set for
2027 * every event type that is to be recorded by the driver, and event_mask has a
2028 * bit cleared for every event that is allowed into the driver from the IOC.
2029 * They really have nothing to do with each other.
2030 */
2031 static void
mpr_user_event_query(struct mpr_softc * sc,mpr_event_query_t * data)2032 mpr_user_event_query(struct mpr_softc *sc, mpr_event_query_t *data)
2033 {
2034 uint8_t i;
2035
2036 mpr_lock(sc);
2037 data->Entries = MPR_EVENT_QUEUE_SIZE;
2038
2039 for (i = 0; i < 4; i++) {
2040 data->Types[i] = sc->events_to_record[i];
2041 }
2042 mpr_unlock(sc);
2043 }
2044
2045 /*
2046 * Set the driver's event mask according to what's been given. See
2047 * mpr_user_event_query for explanation of the event recording mask and the IOC
2048 * event mask. It's the app's responsibility to enable event logging by setting
2049 * the bits in events_to_record. Initially, no events will be logged.
2050 */
2051 static void
mpr_user_event_enable(struct mpr_softc * sc,mpr_event_enable_t * data)2052 mpr_user_event_enable(struct mpr_softc *sc, mpr_event_enable_t *data)
2053 {
2054 uint8_t i;
2055
2056 mpr_lock(sc);
2057 for (i = 0; i < 4; i++) {
2058 sc->events_to_record[i] = data->Types[i];
2059 }
2060 mpr_unlock(sc);
2061 }
2062
2063 /*
2064 * Copy out the events that have been recorded, up to the max events allowed.
2065 */
2066 static int
mpr_user_event_report(struct mpr_softc * sc,mpr_event_report_t * data)2067 mpr_user_event_report(struct mpr_softc *sc, mpr_event_report_t *data)
2068 {
2069 int status = 0;
2070 uint32_t size;
2071
2072 mpr_lock(sc);
2073 size = data->Size;
2074 if ((size >= sizeof(sc->recorded_events)) && (status == 0)) {
2075 mpr_unlock(sc);
2076 if (copyout((void *)sc->recorded_events,
2077 PTRIN(data->PtrEvents), sizeof(sc->recorded_events)) != 0)
2078 status = EFAULT;
2079 mpr_lock(sc);
2080 } else {
2081 /*
2082 * data->Size value is not large enough to copy event data.
2083 */
2084 status = EFAULT;
2085 }
2086
2087 /*
2088 * Change size value to match the number of bytes that were copied.
2089 */
2090 if (status == 0)
2091 data->Size = sizeof(sc->recorded_events);
2092 mpr_unlock(sc);
2093
2094 return (status);
2095 }
2096
2097 /*
2098 * Record events into the driver from the IOC if they are not masked.
2099 */
2100 void
mprsas_record_event(struct mpr_softc * sc,MPI2_EVENT_NOTIFICATION_REPLY * event_reply)2101 mprsas_record_event(struct mpr_softc *sc,
2102 MPI2_EVENT_NOTIFICATION_REPLY *event_reply)
2103 {
2104 uint32_t event;
2105 int i, j;
2106 uint16_t event_data_len;
2107 boolean_t sendAEN = FALSE;
2108
2109 event = event_reply->Event;
2110
2111 /*
2112 * Generate a system event to let anyone who cares know that a
2113 * LOG_ENTRY_ADDED event has occurred. This is sent no matter what the
2114 * event mask is set to.
2115 */
2116 if (event == MPI2_EVENT_LOG_ENTRY_ADDED) {
2117 sendAEN = TRUE;
2118 }
2119
2120 /*
2121 * Record the event only if its corresponding bit is set in
2122 * events_to_record. event_index is the index into recorded_events and
2123 * event_number is the overall number of an event being recorded since
2124 * start-of-day. event_index will roll over; event_number will never
2125 * roll over.
2126 */
2127 i = (uint8_t)(event / 32);
2128 j = (uint8_t)(event % 32);
2129 if ((i < 4) && ((1 << j) & sc->events_to_record[i])) {
2130 i = sc->event_index;
2131 sc->recorded_events[i].Type = event;
2132 sc->recorded_events[i].Number = ++sc->event_number;
2133 bzero(sc->recorded_events[i].Data, MPR_MAX_EVENT_DATA_LENGTH *
2134 4);
2135 event_data_len = event_reply->EventDataLength;
2136
2137 if (event_data_len > 0) {
2138 /*
2139 * Limit data to size in m_event entry
2140 */
2141 if (event_data_len > MPR_MAX_EVENT_DATA_LENGTH) {
2142 event_data_len = MPR_MAX_EVENT_DATA_LENGTH;
2143 }
2144 for (j = 0; j < event_data_len; j++) {
2145 sc->recorded_events[i].Data[j] =
2146 event_reply->EventData[j];
2147 }
2148
2149 /*
2150 * check for index wrap-around
2151 */
2152 if (++i == MPR_EVENT_QUEUE_SIZE) {
2153 i = 0;
2154 }
2155 sc->event_index = (uint8_t)i;
2156
2157 /*
2158 * Set flag to send the event.
2159 */
2160 sendAEN = TRUE;
2161 }
2162 }
2163
2164 /*
2165 * Generate a system event if flag is set to let anyone who cares know
2166 * that an event has occurred.
2167 */
2168 if (sendAEN) {
2169 //SLM-how to send a system event (see kqueue, kevent)
2170 // (void) ddi_log_sysevent(mpt->m_dip, DDI_VENDOR_LSI, "MPT_SAS",
2171 // "SAS", NULL, NULL, DDI_NOSLEEP);
2172 }
2173 }
2174
2175 static int
mpr_user_reg_access(struct mpr_softc * sc,mpr_reg_access_t * data)2176 mpr_user_reg_access(struct mpr_softc *sc, mpr_reg_access_t *data)
2177 {
2178 int status = 0;
2179
2180 switch (data->Command) {
2181 /*
2182 * IO access is not supported.
2183 */
2184 case REG_IO_READ:
2185 case REG_IO_WRITE:
2186 mpr_dprint(sc, MPR_USER, "IO access is not supported. "
2187 "Use memory access.");
2188 status = EINVAL;
2189 break;
2190
2191 case REG_MEM_READ:
2192 data->RegData = mpr_regread(sc, data->RegOffset);
2193 break;
2194
2195 case REG_MEM_WRITE:
2196 mpr_regwrite(sc, data->RegOffset, data->RegData);
2197 break;
2198
2199 default:
2200 status = EINVAL;
2201 break;
2202 }
2203
2204 return (status);
2205 }
2206
2207 static int
mpr_user_btdh(struct mpr_softc * sc,mpr_btdh_mapping_t * data)2208 mpr_user_btdh(struct mpr_softc *sc, mpr_btdh_mapping_t *data)
2209 {
2210 uint8_t bt2dh = FALSE;
2211 uint8_t dh2bt = FALSE;
2212 uint16_t dev_handle, bus, target;
2213
2214 bus = data->Bus;
2215 target = data->TargetID;
2216 dev_handle = data->DevHandle;
2217
2218 /*
2219 * When DevHandle is 0xFFFF and Bus/Target are not 0xFFFF, use Bus/
2220 * Target to get DevHandle. When Bus/Target are 0xFFFF and DevHandle is
2221 * not 0xFFFF, use DevHandle to get Bus/Target. Anything else is
2222 * invalid.
2223 */
2224 if ((bus == 0xFFFF) && (target == 0xFFFF) && (dev_handle != 0xFFFF))
2225 dh2bt = TRUE;
2226 if ((dev_handle == 0xFFFF) && (bus != 0xFFFF) && (target != 0xFFFF))
2227 bt2dh = TRUE;
2228 if (!dh2bt && !bt2dh)
2229 return (EINVAL);
2230
2231 /*
2232 * Only handle bus of 0. Make sure target is within range.
2233 */
2234 if (bt2dh) {
2235 if (bus != 0)
2236 return (EINVAL);
2237
2238 if (target >= sc->max_devices) {
2239 mpr_dprint(sc, MPR_XINFO, "Target ID is out of range "
2240 "for Bus/Target to DevHandle mapping.");
2241 return (EINVAL);
2242 }
2243 dev_handle = sc->mapping_table[target].dev_handle;
2244 if (dev_handle)
2245 data->DevHandle = dev_handle;
2246 } else {
2247 bus = 0;
2248 target = mpr_mapping_get_tid_from_handle(sc, dev_handle);
2249 data->Bus = bus;
2250 data->TargetID = target;
2251 }
2252
2253 return (0);
2254 }
2255
2256 static int
mpr_ioctl(struct cdev * dev,u_long cmd,void * arg,int flag,struct thread * td)2257 mpr_ioctl(struct cdev *dev, u_long cmd, void *arg, int flag,
2258 struct thread *td)
2259 {
2260 struct mpr_softc *sc;
2261 struct mpr_cfg_page_req *page_req;
2262 struct mpr_ext_cfg_page_req *ext_page_req;
2263 void *mpr_page;
2264 int error, msleep_ret;
2265
2266 mpr_page = NULL;
2267 sc = dev->si_drv1;
2268 page_req = (void *)arg;
2269 ext_page_req = (void *)arg;
2270
2271 switch (cmd) {
2272 case MPRIO_READ_CFG_HEADER:
2273 mpr_lock(sc);
2274 error = mpr_user_read_cfg_header(sc, page_req);
2275 mpr_unlock(sc);
2276 break;
2277 case MPRIO_READ_CFG_PAGE:
2278 if (page_req->len < (int)sizeof(MPI2_CONFIG_PAGE_HEADER)) {
2279 error = EINVAL;
2280 break;
2281 }
2282 mpr_page = malloc(page_req->len, M_MPRUSER, M_WAITOK | M_ZERO);
2283 error = copyin(page_req->buf, mpr_page,
2284 sizeof(MPI2_CONFIG_PAGE_HEADER));
2285 if (error)
2286 break;
2287 mpr_lock(sc);
2288 error = mpr_user_read_cfg_page(sc, page_req, mpr_page);
2289 mpr_unlock(sc);
2290 if (error)
2291 break;
2292 error = copyout(mpr_page, page_req->buf, page_req->len);
2293 break;
2294 case MPRIO_READ_EXT_CFG_HEADER:
2295 mpr_lock(sc);
2296 error = mpr_user_read_extcfg_header(sc, ext_page_req);
2297 mpr_unlock(sc);
2298 break;
2299 case MPRIO_READ_EXT_CFG_PAGE:
2300 if (ext_page_req->len <
2301 (int)sizeof(MPI2_CONFIG_EXTENDED_PAGE_HEADER)) {
2302 error = EINVAL;
2303 break;
2304 }
2305 mpr_page = malloc(ext_page_req->len, M_MPRUSER,
2306 M_WAITOK | M_ZERO);
2307 error = copyin(ext_page_req->buf, mpr_page,
2308 sizeof(MPI2_CONFIG_EXTENDED_PAGE_HEADER));
2309 if (error)
2310 break;
2311 mpr_lock(sc);
2312 error = mpr_user_read_extcfg_page(sc, ext_page_req, mpr_page);
2313 mpr_unlock(sc);
2314 if (error)
2315 break;
2316 error = copyout(mpr_page, ext_page_req->buf, ext_page_req->len);
2317 break;
2318 case MPRIO_WRITE_CFG_PAGE:
2319 if (page_req->len < (int)sizeof(MPI2_CONFIG_PAGE_HEADER)) {
2320 error = EINVAL;
2321 break;
2322 }
2323 mpr_page = malloc(page_req->len, M_MPRUSER, M_WAITOK|M_ZERO);
2324 error = copyin(page_req->buf, mpr_page, page_req->len);
2325 if (error)
2326 break;
2327 mpr_lock(sc);
2328 error = mpr_user_write_cfg_page(sc, page_req, mpr_page);
2329 mpr_unlock(sc);
2330 break;
2331 case MPRIO_MPR_COMMAND:
2332 error = mpr_user_command(sc, (struct mpr_usr_command *)arg);
2333 break;
2334 case MPTIOCTL_PASS_THRU:
2335 /*
2336 * The user has requested to pass through a command to be
2337 * executed by the MPT firmware. Call our routine which does
2338 * this. Only allow one passthru IOCTL at one time.
2339 */
2340 error = mpr_user_pass_thru(sc, (mpr_pass_thru_t *)arg);
2341 break;
2342 case MPTIOCTL_GET_ADAPTER_DATA:
2343 /*
2344 * The user has requested to read adapter data. Call our
2345 * routine which does this.
2346 */
2347 error = 0;
2348 mpr_user_get_adapter_data(sc, (mpr_adapter_data_t *)arg);
2349 break;
2350 case MPTIOCTL_GET_PCI_INFO:
2351 /*
2352 * The user has requested to read pci info. Call
2353 * our routine which does this.
2354 */
2355 mpr_lock(sc);
2356 error = 0;
2357 mpr_user_read_pci_info(sc, (mpr_pci_info_t *)arg);
2358 mpr_unlock(sc);
2359 break;
2360 case MPTIOCTL_RESET_ADAPTER:
2361 mpr_lock(sc);
2362 sc->port_enable_complete = 0;
2363 uint32_t reinit_start = time_uptime;
2364 error = mpr_reinit(sc);
2365 /* Sleep for 300 second. */
2366 msleep_ret = msleep(&sc->port_enable_complete, &sc->mpr_mtx,
2367 PRIBIO, "mpr_porten", 300 * hz);
2368 mpr_unlock(sc);
2369 if (msleep_ret)
2370 printf("Port Enable did not complete after Diag "
2371 "Reset msleep error %d.\n", msleep_ret);
2372 else
2373 mpr_dprint(sc, MPR_USER, "Hard Reset with Port Enable "
2374 "completed in %d seconds.\n",
2375 (uint32_t)(time_uptime - reinit_start));
2376 break;
2377 case MPTIOCTL_DIAG_ACTION:
2378 /*
2379 * The user has done a diag buffer action. Call our routine
2380 * which does this. Only allow one diag action at one time.
2381 */
2382 mpr_lock(sc);
2383 error = mpr_user_diag_action(sc, (mpr_diag_action_t *)arg);
2384 mpr_unlock(sc);
2385 break;
2386 case MPTIOCTL_EVENT_QUERY:
2387 /*
2388 * The user has done an event query. Call our routine which does
2389 * this.
2390 */
2391 error = 0;
2392 mpr_user_event_query(sc, (mpr_event_query_t *)arg);
2393 break;
2394 case MPTIOCTL_EVENT_ENABLE:
2395 /*
2396 * The user has done an event enable. Call our routine which
2397 * does this.
2398 */
2399 error = 0;
2400 mpr_user_event_enable(sc, (mpr_event_enable_t *)arg);
2401 break;
2402 case MPTIOCTL_EVENT_REPORT:
2403 /*
2404 * The user has done an event report. Call our routine which
2405 * does this.
2406 */
2407 error = mpr_user_event_report(sc, (mpr_event_report_t *)arg);
2408 break;
2409 case MPTIOCTL_REG_ACCESS:
2410 /*
2411 * The user has requested register access. Call our routine
2412 * which does this.
2413 */
2414 mpr_lock(sc);
2415 error = mpr_user_reg_access(sc, (mpr_reg_access_t *)arg);
2416 mpr_unlock(sc);
2417 break;
2418 case MPTIOCTL_BTDH_MAPPING:
2419 /*
2420 * The user has requested to translate a bus/target to a
2421 * DevHandle or a DevHandle to a bus/target. Call our routine
2422 * which does this.
2423 */
2424 error = mpr_user_btdh(sc, (mpr_btdh_mapping_t *)arg);
2425 break;
2426 default:
2427 error = ENOIOCTL;
2428 break;
2429 }
2430
2431 if (mpr_page != NULL)
2432 free(mpr_page, M_MPRUSER);
2433
2434 return (error);
2435 }
2436
2437 #ifdef COMPAT_FREEBSD32
2438
2439 struct mpr_cfg_page_req32 {
2440 MPI2_CONFIG_PAGE_HEADER header;
2441 uint32_t page_address;
2442 uint32_t buf;
2443 int len;
2444 uint16_t ioc_status;
2445 };
2446
2447 struct mpr_ext_cfg_page_req32 {
2448 MPI2_CONFIG_EXTENDED_PAGE_HEADER header;
2449 uint32_t page_address;
2450 uint32_t buf;
2451 int len;
2452 uint16_t ioc_status;
2453 };
2454
2455 struct mpr_raid_action32 {
2456 uint8_t action;
2457 uint8_t volume_bus;
2458 uint8_t volume_id;
2459 uint8_t phys_disk_num;
2460 uint32_t action_data_word;
2461 uint32_t buf;
2462 int len;
2463 uint32_t volume_status;
2464 uint32_t action_data[4];
2465 uint16_t action_status;
2466 uint16_t ioc_status;
2467 uint8_t write;
2468 };
2469
2470 struct mpr_usr_command32 {
2471 uint32_t req;
2472 uint32_t req_len;
2473 uint32_t rpl;
2474 uint32_t rpl_len;
2475 uint32_t buf;
2476 int len;
2477 uint32_t flags;
2478 };
2479
2480 #define MPRIO_READ_CFG_HEADER32 _IOWR('M', 200, struct mpr_cfg_page_req32)
2481 #define MPRIO_READ_CFG_PAGE32 _IOWR('M', 201, struct mpr_cfg_page_req32)
2482 #define MPRIO_READ_EXT_CFG_HEADER32 _IOWR('M', 202, struct mpr_ext_cfg_page_req32)
2483 #define MPRIO_READ_EXT_CFG_PAGE32 _IOWR('M', 203, struct mpr_ext_cfg_page_req32)
2484 #define MPRIO_WRITE_CFG_PAGE32 _IOWR('M', 204, struct mpr_cfg_page_req32)
2485 #define MPRIO_RAID_ACTION32 _IOWR('M', 205, struct mpr_raid_action32)
2486 #define MPRIO_MPR_COMMAND32 _IOWR('M', 210, struct mpr_usr_command32)
2487
2488 static int
mpr_ioctl32(struct cdev * dev,u_long cmd32,void * _arg,int flag,struct thread * td)2489 mpr_ioctl32(struct cdev *dev, u_long cmd32, void *_arg, int flag,
2490 struct thread *td)
2491 {
2492 struct mpr_cfg_page_req32 *page32 = _arg;
2493 struct mpr_ext_cfg_page_req32 *ext32 = _arg;
2494 struct mpr_raid_action32 *raid32 = _arg;
2495 struct mpr_usr_command32 *user32 = _arg;
2496 union {
2497 struct mpr_cfg_page_req page;
2498 struct mpr_ext_cfg_page_req ext;
2499 struct mpr_raid_action raid;
2500 struct mpr_usr_command user;
2501 } arg;
2502 u_long cmd;
2503 int error;
2504
2505 switch (cmd32) {
2506 case MPRIO_READ_CFG_HEADER32:
2507 case MPRIO_READ_CFG_PAGE32:
2508 case MPRIO_WRITE_CFG_PAGE32:
2509 if (cmd32 == MPRIO_READ_CFG_HEADER32)
2510 cmd = MPRIO_READ_CFG_HEADER;
2511 else if (cmd32 == MPRIO_READ_CFG_PAGE32)
2512 cmd = MPRIO_READ_CFG_PAGE;
2513 else
2514 cmd = MPRIO_WRITE_CFG_PAGE;
2515 CP(*page32, arg.page, header);
2516 CP(*page32, arg.page, page_address);
2517 PTRIN_CP(*page32, arg.page, buf);
2518 CP(*page32, arg.page, len);
2519 CP(*page32, arg.page, ioc_status);
2520 break;
2521
2522 case MPRIO_READ_EXT_CFG_HEADER32:
2523 case MPRIO_READ_EXT_CFG_PAGE32:
2524 if (cmd32 == MPRIO_READ_EXT_CFG_HEADER32)
2525 cmd = MPRIO_READ_EXT_CFG_HEADER;
2526 else
2527 cmd = MPRIO_READ_EXT_CFG_PAGE;
2528 CP(*ext32, arg.ext, header);
2529 CP(*ext32, arg.ext, page_address);
2530 PTRIN_CP(*ext32, arg.ext, buf);
2531 CP(*ext32, arg.ext, len);
2532 CP(*ext32, arg.ext, ioc_status);
2533 break;
2534
2535 case MPRIO_RAID_ACTION32:
2536 cmd = MPRIO_RAID_ACTION;
2537 CP(*raid32, arg.raid, action);
2538 CP(*raid32, arg.raid, volume_bus);
2539 CP(*raid32, arg.raid, volume_id);
2540 CP(*raid32, arg.raid, phys_disk_num);
2541 CP(*raid32, arg.raid, action_data_word);
2542 PTRIN_CP(*raid32, arg.raid, buf);
2543 CP(*raid32, arg.raid, len);
2544 CP(*raid32, arg.raid, volume_status);
2545 bcopy(raid32->action_data, arg.raid.action_data,
2546 sizeof arg.raid.action_data);
2547 CP(*raid32, arg.raid, ioc_status);
2548 CP(*raid32, arg.raid, write);
2549 break;
2550
2551 case MPRIO_MPR_COMMAND32:
2552 cmd = MPRIO_MPR_COMMAND;
2553 PTRIN_CP(*user32, arg.user, req);
2554 CP(*user32, arg.user, req_len);
2555 PTRIN_CP(*user32, arg.user, rpl);
2556 CP(*user32, arg.user, rpl_len);
2557 PTRIN_CP(*user32, arg.user, buf);
2558 CP(*user32, arg.user, len);
2559 CP(*user32, arg.user, flags);
2560 break;
2561 default:
2562 return (ENOIOCTL);
2563 }
2564
2565 error = mpr_ioctl(dev, cmd, &arg, flag, td);
2566 if (error == 0 && (cmd32 & IOC_OUT) != 0) {
2567 switch (cmd32) {
2568 case MPRIO_READ_CFG_HEADER32:
2569 case MPRIO_READ_CFG_PAGE32:
2570 case MPRIO_WRITE_CFG_PAGE32:
2571 CP(arg.page, *page32, header);
2572 CP(arg.page, *page32, page_address);
2573 PTROUT_CP(arg.page, *page32, buf);
2574 CP(arg.page, *page32, len);
2575 CP(arg.page, *page32, ioc_status);
2576 break;
2577
2578 case MPRIO_READ_EXT_CFG_HEADER32:
2579 case MPRIO_READ_EXT_CFG_PAGE32:
2580 CP(arg.ext, *ext32, header);
2581 CP(arg.ext, *ext32, page_address);
2582 PTROUT_CP(arg.ext, *ext32, buf);
2583 CP(arg.ext, *ext32, len);
2584 CP(arg.ext, *ext32, ioc_status);
2585 break;
2586
2587 case MPRIO_RAID_ACTION32:
2588 CP(arg.raid, *raid32, action);
2589 CP(arg.raid, *raid32, volume_bus);
2590 CP(arg.raid, *raid32, volume_id);
2591 CP(arg.raid, *raid32, phys_disk_num);
2592 CP(arg.raid, *raid32, action_data_word);
2593 PTROUT_CP(arg.raid, *raid32, buf);
2594 CP(arg.raid, *raid32, len);
2595 CP(arg.raid, *raid32, volume_status);
2596 bcopy(arg.raid.action_data, raid32->action_data,
2597 sizeof arg.raid.action_data);
2598 CP(arg.raid, *raid32, ioc_status);
2599 CP(arg.raid, *raid32, write);
2600 break;
2601
2602 case MPRIO_MPR_COMMAND32:
2603 PTROUT_CP(arg.user, *user32, req);
2604 CP(arg.user, *user32, req_len);
2605 PTROUT_CP(arg.user, *user32, rpl);
2606 CP(arg.user, *user32, rpl_len);
2607 PTROUT_CP(arg.user, *user32, buf);
2608 CP(arg.user, *user32, len);
2609 CP(arg.user, *user32, flags);
2610 break;
2611 }
2612 }
2613
2614 return (error);
2615 }
2616 #endif /* COMPAT_FREEBSD32 */
2617
2618 static int
mpr_ioctl_devsw(struct cdev * dev,u_long com,caddr_t arg,int flag,struct thread * td)2619 mpr_ioctl_devsw(struct cdev *dev, u_long com, caddr_t arg, int flag,
2620 struct thread *td)
2621 {
2622 #ifdef COMPAT_FREEBSD32
2623 if (SV_CURPROC_FLAG(SV_ILP32))
2624 return (mpr_ioctl32(dev, com, arg, flag, td));
2625 #endif
2626 return (mpr_ioctl(dev, com, arg, flag, td));
2627 }
2628