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