Print this page
XXXX introduce drv_sectohz
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/sun4v/ontario/io/tsalarm.c
+++ new/usr/src/uts/sun4v/ontario/io/tsalarm.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 23 * Use is subject to license terms.
24 24 */
25 25
26 26
27 27 #include <sys/types.h>
28 28 #include <sys/file.h>
29 29 #include <sys/errno.h>
30 30 #include <sys/open.h>
31 31 #include <sys/cred.h>
32 32 #include <sys/cmn_err.h>
33 33 #include <sys/modctl.h>
34 34 #include <sys/conf.h>
35 35 #include <sys/stat.h>
36 36
37 37 #include <sys/callb.h>
38 38 #include <sys/strlog.h>
39 39 #include <sys/lom_io.h>
40 40 #include <sys/time.h>
41 41 #include <sys/glvc.h>
42 42 #include <sys/kmem.h>
43 43 #include <netinet/in.h>
44 44 #include <sys/inttypes.h>
45 45
46 46 #include <sys/ddi.h>
47 47 #include <sys/sunddi.h>
48 48 #include <sys/sunldi.h>
49 49
50 50 /* common defines */
51 51 #ifndef MIN
52 52 #define MIN(x, y) ((x) < (y) ? (x) : (y))
53 53 #endif
54 54 #ifndef MAX
55 55 #define MAX(x, y) ((x) > (y) ? (x) : (y))
56 56 #endif
57 57 #ifndef ABS
58 58 #define ABS(x) ((x) < (0) ? (-(x)) : (x))
59 59 #endif
60 60
61 61 #define LOMIOCALCTL_OLD _IOW('a', 4, ts_aldata_t)
62 62 #define LOMIOCALSTATE_OLD _IOWR('a', 5, ts_aldata_t)
63 63
64 64 #define PCP_CKSUM_ENABLE
65 65 #define PCP_DEF_MTU_SZ 100
66 66
67 67 #define PCP_MAX_TRY_CNT 5
68 68 #define PCP_GLVC_SLEEP 5
69 69 #define PCP_COMM_TIMEOUT 0x10
70 70
71 71 #define PCP_IO_OP_READ (1)
72 72 #define PCP_IO_OP_WRITE (2)
73 73 #define PCP_IO_OP_PEEK (3)
74 74
75 75
76 76 /* Error codes for 'status' field in response message header */
77 77 #define TSAL_PCP_ERROR (-1)
78 78 #define TSAL_PCP_OK (0) /* message received okay */
79 79
80 80 /*
81 81 * magic number for Platform Channel Protocol (PCP)
82 82 * ~(rot13("PCP_") = 0xAFBCAFA0
83 83 * rot13 is a simple Caesar-cypher encryption that replaces each English letter
84 84 * with the one 13 places forward or back along the alphabet.
85 85 */
86 86 #define PCP_MAGIC_NUM (0xAFBCAFA0)
87 87
88 88 /* Platform channel protocol versions. */
89 89 #define PCP_PROT_VER_1 1
90 90
91 91 /* defines for 'timeout' */
92 92 #define PCP_TO_NO_RESPONSE (0xFFFFFFFF) /* no response required */
93 93 #define PCP_TO_WAIT_FOREVER (0) /* wait forever..(in reality, */
94 94 /* it waits until glvc driver */
95 95 /* call returns; curently glvc */
96 96 /* calls are blocking calls. */
97 97
98 98 /* Message Types */
99 99 #define PCP_ALARM_CONTROL 15
100 100 #define PCP_ALARM_CONTROL_R 16
101 101
102 102 /* alarm_action */
103 103 #define PCP_ALARM_ENABLE 1
104 104 #define PCP_ALARM_DISABLE 2
105 105 #define PCP_ALARM_STATUS 3
106 106
107 107 /* alarm_id */
108 108 #define PCP_ALARM_CRITICAL 0
109 109 #define PCP_ALARM_MAJOR 1
110 110 #define PCP_ALARM_MINOR 2
111 111 #define PCP_ALARM_USER 3
112 112
113 113 /* alarm_state */
114 114 #define ALARM_STATE_ON 1
115 115 #define ALARM_STATE_OFF 2
116 116 #define ALARM_STATE_UNKNOWN 3
117 117
118 118 /* Status Types */
119 119 #define PCP_ALARM_OK (1)
120 120 #define PCP_ALARM_ERROR (2)
121 121
122 122 /* tsalarm service channel */
123 123 #define ALARM_CHANNEL "/devices/virtual-devices@100/telco-alarm@f:glvc"
124 124
125 125 /* Driver state flags */
126 126 #define TSAL_OPENED 0x1
127 127 #define TSAL_IDENTED 0x2
128 128
129 129 /*
130 130 * Platform Channel Request Message Header.
131 131 */
132 132 typedef struct tsal_pcp_req_msg_hdr {
133 133 uint32_t magic_num; /* magic number */
134 134 uint8_t proto_ver; /* version info for */
135 135 /* backward compatibility */
136 136 uint8_t msg_type; /* provided by user apps */
137 137 uint8_t sub_type; /* provided by user apps */
138 138 uint8_t rsvd_pad; /* padding bits */
139 139 uint32_t xid; /* transaction id */
140 140 uint32_t timeout; /* timeout in seconds */
141 141 uint32_t msg_len; /* length of request or response data */
142 142 uint16_t msg_cksum; /* 16-bit checksum of req msg data */
143 143 uint16_t hdr_cksum; /* 16-bit checksum of req hdr */
144 144 } tsal_pcp_req_msg_hdr_t;
145 145
146 146 /*
147 147 * Platform Channel Response Message Header.
148 148 */
149 149 typedef struct tsal_pcp_resp_msg_hdr {
150 150 uint32_t magic_num; /* magic number */
151 151 uint8_t proto_ver; /* version info for */
152 152 /* backward compatibility */
153 153 uint8_t msg_type; /* passed to user apps */
154 154 uint8_t sub_type; /* passed to user apps */
155 155 uint8_t rsvd_pad; /* for padding */
156 156 uint32_t xid; /* transaction id */
157 157 uint32_t timeout; /* timeout in seconds */
158 158 uint32_t msg_len; /* length of request or response data */
159 159 uint32_t status; /* response status */
160 160 uint16_t msg_cksum; /* 16-bit checksum of resp msg data */
161 161 uint16_t hdr_cksum; /* 16-bit checksum of resp hdr */
162 162 } tsal_pcp_resp_msg_hdr_t;
163 163
164 164 /*
165 165 * PCP user apps message format
166 166 */
167 167 typedef struct tsal_pcp_msg {
168 168 uint8_t msg_type;
169 169 uint8_t sub_type;
170 170 uint16_t rsvd_pad;
171 171 uint32_t msg_len;
172 172 void *msg_data;
173 173 } tsal_pcp_msg_t;
174 174
175 175 /*
176 176 * alarm set/get request message
177 177 */
178 178 typedef struct tsal_pcp_alarm_req {
179 179 uint32_t alarm_id;
180 180 uint32_t alarm_action;
181 181 } tsal_pcp_alarm_req_t;
182 182
183 183 /*
184 184 * alarm set/get response message
185 185 */
186 186 typedef struct tsal_pcp_alarm_resp {
187 187 uint32_t status;
188 188 uint32_t alarm_id;
189 189 uint32_t alarm_state;
190 190 } tsal_pcp_alarm_resp_t;
191 191
192 192 /*
193 193 * tsalarm driver soft structure
194 194 */
195 195 typedef struct tsalarm_softc {
196 196 ldi_handle_t lh;
197 197 ldi_ident_t li;
198 198 dev_info_t *dip;
199 199 minor_t minor;
200 200 int flags;
201 201 kmutex_t mutex;
202 202 uint32_t msg_xid;
203 203 uint32_t mtu_size;
204 204 uint8_t *read_head;
205 205 uint8_t *read_tail;
206 206 uint8_t *read_area;
207 207 uint8_t *peek_area;
208 208 uint8_t *peek_read_area;
209 209 tsal_pcp_alarm_req_t *req_ptr;
210 210 tsal_pcp_alarm_resp_t *resp_ptr;
211 211 tsal_pcp_req_msg_hdr_t *req_msg_hdr;
212 212 tsal_pcp_resp_msg_hdr_t *resp_msg_hdr;
213 213 }tsalarm_softc_t;
214 214
215 215 /*
216 216 * Forward declarations.
217 217 */
218 218 static int tsal_pcp_send_req_msg_hdr(tsalarm_softc_t *sc,
219 219 tsal_pcp_req_msg_hdr_t *req_hdr);
220 220 static int tsal_pcp_recv_resp_msg_hdr(tsalarm_softc_t *sc,
221 221 tsal_pcp_resp_msg_hdr_t *resp_hdr);
222 222 static int tsal_pcp_io_op(tsalarm_softc_t *sc, void *buf,
223 223 int byte_cnt, int io_op);
224 224 static int tsal_pcp_read(tsalarm_softc_t *sc, uint8_t *buf, int buf_len);
225 225 static int tsal_pcp_write(tsalarm_softc_t *sc, uint8_t *buf, int buf_len);
226 226 static int tsal_pcp_peek(tsalarm_softc_t *sc, uint8_t *buf, int buf_len);
227 227 static int tsal_pcp_peek_read(tsalarm_softc_t *sc, uint8_t *buf, int buf_len);
228 228 static int tsal_pcp_frame_error_handle(tsalarm_softc_t *sc);
229 229 static int check_magic_byte_presence(tsalarm_softc_t *sc, int byte_cnt,
230 230 uint8_t *byte_val, int *ispresent);
231 231 static int tsal_pcp_send_recv(tsalarm_softc_t *sc, tsal_pcp_msg_t *req_msg,
232 232 tsal_pcp_msg_t *resp_msg, uint32_t timeout);
233 233 static uint32_t tsal_pcp_get_xid(tsalarm_softc_t *sc);
234 234 static uint16_t checksum(uint16_t *addr, int32_t count);
235 235 static int glvc_alarm_get(int alarm_type, int *alarm_state,
236 236 tsalarm_softc_t *sc);
237 237 static int glvc_alarm_set(int alarm_type, int new_state,
238 238 tsalarm_softc_t *sc);
239 239
240 240 #define getsoftc(minor) \
241 241 ((struct tsalarm_softc *)ddi_get_soft_state(statep, (minor)))
242 242
243 243 /*
244 244 * Driver entry points
245 245 */
246 246
247 247 /* dev_ops and cb_ops entry point function declarations */
248 248
249 249 static int tsalarm_attach(dev_info_t *, ddi_attach_cmd_t);
250 250 static int tsalarm_detach(dev_info_t *, ddi_detach_cmd_t);
251 251 static int tsalarm_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
252 252
253 253 static int tsalarm_open(dev_t *, int, int, cred_t *);
254 254 static int tsalarm_close(dev_t, int, int, cred_t *);
255 255 static int tsalarm_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
256 256
257 257 static struct cb_ops tsalarm_cb_ops = {
258 258 tsalarm_open, /* open */
259 259 tsalarm_close, /* close */
260 260 nodev, /* strategy() */
261 261 nodev, /* print() */
262 262 nodev, /* dump() */
263 263 nodev, /* read() */
264 264 nodev, /* write() */
265 265 tsalarm_ioctl, /* ioctl() */
266 266 nodev, /* devmap() */
267 267 nodev, /* mmap() */
268 268 ddi_segmap, /* segmap() */
269 269 nochpoll, /* poll() */
270 270 ddi_prop_op, /* prop_op() */
271 271 NULL, /* cb_str */
272 272 D_NEW | D_MP /* cb_flag */
273 273 };
274 274
275 275
276 276 static struct dev_ops tsalarm_ops = {
277 277 DEVO_REV,
278 278 0, /* ref count */
279 279 tsalarm_getinfo, /* getinfo() */
280 280 nulldev, /* identify() */
281 281 nulldev, /* probe() */
282 282 tsalarm_attach, /* attach() */
283 283 tsalarm_detach, /* detach */
284 284 nodev, /* reset */
285 285 &tsalarm_cb_ops, /* pointer to cb_ops structure */
286 286 (struct bus_ops *)NULL,
287 287 nulldev, /* power() */
288 288 ddi_quiesce_not_needed, /* quiesce() */
289 289 };
290 290
291 291 /*
292 292 * Loadable module support.
293 293 */
294 294 extern struct mod_ops mod_driverops;
295 295 static void *statep;
296 296
297 297 static struct modldrv modldrv = {
298 298 &mod_driverops, /* Type of module. This is a driver */
299 299 "tsalarm control driver", /* Name of the module */
300 300 &tsalarm_ops /* pointer to the dev_ops structure */
301 301 };
302 302
303 303 static struct modlinkage modlinkage = {
304 304 MODREV_1,
305 305 &modldrv,
306 306 NULL
307 307 };
308 308
309 309 int
310 310 _init(void)
311 311 {
312 312 int e;
313 313
314 314 if (e = ddi_soft_state_init(&statep,
315 315 sizeof (struct tsalarm_softc), 1)) {
316 316 return (e);
317 317 }
318 318
319 319 if ((e = mod_install(&modlinkage)) != 0) {
320 320 ddi_soft_state_fini(&statep);
321 321 }
322 322
323 323 return (e);
324 324 }
325 325
326 326 int
327 327 _fini(void)
328 328 {
329 329 int e;
330 330
331 331 if ((e = mod_remove(&modlinkage)) != 0) {
332 332 return (e);
333 333 }
334 334
335 335 ddi_soft_state_fini(&statep);
336 336
337 337 return (DDI_SUCCESS);
338 338 }
339 339
340 340 int
341 341 _info(struct modinfo *modinfop)
342 342 {
343 343 return (mod_info(&modlinkage, modinfop));
344 344 }
345 345
346 346
347 347 /* ARGSUSED */
348 348 static int
349 349 tsalarm_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
350 350 {
351 351 int inst = getminor((dev_t)arg);
352 352 int retval = DDI_SUCCESS;
353 353 struct tsalarm_softc *softc;
354 354
355 355 switch (cmd) {
356 356
357 357 case DDI_INFO_DEVT2DEVINFO:
358 358 if ((softc = getsoftc(inst)) == NULL) {
359 359 *result = (void *)NULL;
360 360 retval = DDI_FAILURE;
361 361 } else {
362 362 *result = (void *)softc->dip;
363 363 }
364 364 break;
365 365
366 366 case DDI_INFO_DEVT2INSTANCE:
367 367 *result = (void *)(uintptr_t)inst;
368 368 break;
369 369
370 370 default:
371 371 retval = DDI_FAILURE;
372 372 }
373 373
374 374 return (retval);
375 375 }
376 376
377 377 static int
378 378 tsalarm_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
379 379 {
380 380
381 381 int inst;
382 382 struct tsalarm_softc *softc = NULL;
383 383
384 384 switch (cmd) {
385 385
386 386 case DDI_ATTACH:
387 387 inst = ddi_get_instance(dip);
388 388 /*
389 389 * Allocate a soft state structure for this instance.
390 390 */
391 391 if (ddi_soft_state_zalloc(statep, inst) != DDI_SUCCESS) {
392 392 cmn_err(CE_WARN, "Failed to allocate memory");
393 393 goto attach_failed;
394 394 }
395 395
396 396 softc = getsoftc(inst);
397 397 softc->dip = dip;
398 398 softc->mtu_size = PCP_DEF_MTU_SZ;
399 399 softc->msg_xid = 0;
400 400 softc->read_area = NULL;
401 401 softc->read_head = NULL;
402 402 softc->read_tail = NULL;
403 403 softc->req_ptr = NULL;
404 404 softc->resp_ptr = NULL;
405 405
406 406 mutex_init(&softc->mutex, NULL, MUTEX_DRIVER, NULL);
407 407 /*
408 408 * Create minor node. The minor device number, inst, has no
409 409 * meaning. The model number above, which will be added to
410 410 * the device's softc, is used to direct peculiar behavior.
411 411 */
412 412 if (ddi_create_minor_node(dip, "lom", S_IFCHR, 0,
413 413 DDI_PSEUDO, NULL) == DDI_FAILURE) {
414 414 goto attach_failed;
415 415 }
416 416
417 417 ddi_report_dev(dip);
418 418 return (DDI_SUCCESS);
419 419
420 420 case DDI_RESUME:
421 421 return (DDI_SUCCESS);
422 422
423 423 default:
424 424 return (DDI_FAILURE);
425 425 }
426 426
427 427 attach_failed:
428 428 /* Free soft state, if allocated. remove minor node if added earlier */
429 429 if (softc) {
430 430 mutex_destroy(&softc->mutex);
431 431 ddi_soft_state_free(statep, inst);
432 432 }
433 433
434 434 ddi_remove_minor_node(dip, NULL);
435 435
436 436 return (DDI_FAILURE);
437 437 }
438 438
439 439 static int
440 440 tsalarm_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
441 441 {
442 442 int inst;
443 443 struct tsalarm_softc *softc;
444 444
445 445 switch (cmd) {
446 446
447 447 case DDI_DETACH:
448 448 inst = ddi_get_instance(dip);
449 449 if ((softc = getsoftc(inst)) == NULL)
450 450 return (DDI_FAILURE);
451 451 /*
452 452 * Free the soft state and remove minor node added earlier.
453 453 */
454 454 ddi_remove_minor_node(dip, NULL);
455 455 mutex_destroy(&softc->mutex);
456 456 ddi_soft_state_free(statep, inst);
457 457 return (DDI_SUCCESS);
458 458
459 459 case DDI_SUSPEND:
460 460 return (DDI_SUCCESS);
461 461
462 462 default:
463 463 return (DDI_FAILURE);
464 464
465 465 }
466 466 }
467 467
468 468 /* ARGSUSED */
469 469 static int
470 470 tsalarm_open(dev_t *devp, int flag, int otyp, cred_t *credp)
471 471 {
472 472 int rv, inst = getminor(*devp);
473 473 struct tsalarm_softc *softc;
474 474 glvc_xport_opt_op_t channel_op;
475 475 int rval;
476 476
477 477 softc = (struct tsalarm_softc *)getsoftc(inst);
478 478 if (softc == NULL) {
479 479 cmn_err(CE_WARN, "getsoftc failed\n");
480 480 return (EIO);
481 481 }
482 482
483 483 mutex_enter(&softc->mutex);
484 484
485 485 rv = ldi_ident_from_dev(*devp, &softc->li);
486 486 if (rv != 0) {
487 487 cmn_err(CE_WARN, "ldi_ident_from_dev failed\n");
488 488 goto FAIL;
489 489 }
490 490 softc->flags |= TSAL_IDENTED;
491 491
492 492 rv = ldi_open_by_name(ALARM_CHANNEL, FREAD | FWRITE, kcred, &softc->lh,
493 493 softc->li);
494 494 if (rv != 0) {
495 495 cmn_err(CE_WARN, "ldi_open_by_name failed\n");
496 496 goto FAIL;
497 497 }
498 498 softc->flags |= TSAL_OPENED;
499 499
500 500 /* Get the MTU of the target channel */
501 501 channel_op.op_sel = GLVC_XPORT_OPT_GET;
502 502 channel_op.opt_sel = GLVC_XPORT_OPT_MTU_SZ;
503 503 channel_op.opt_val = 0;
504 504
505 505 if ((rv = ldi_ioctl(softc->lh, GLVC_XPORT_IOCTL_OPT_OP,
506 506 (intptr_t)&channel_op, FKIOCTL, kcred, &rval)) < 0) {
507 507 cmn_err(CE_WARN, "ldi_ioctl failed\n");
508 508 goto FAIL;
509 509 }
510 510 softc->mtu_size = channel_op.opt_val;
511 511
512 512 if ((softc->req_ptr = (tsal_pcp_alarm_req_t *)kmem_zalloc(
513 513 sizeof (tsal_pcp_alarm_req_t),
514 514 KM_NOSLEEP)) == NULL) {
515 515 goto FAIL;
516 516 }
517 517 if ((softc->resp_ptr = (tsal_pcp_alarm_resp_t *)kmem_zalloc(
518 518 sizeof (tsal_pcp_alarm_resp_t),
519 519 KM_NOSLEEP)) == NULL) {
520 520 goto FAIL;
521 521 }
522 522 if ((softc->req_msg_hdr = (tsal_pcp_req_msg_hdr_t *)kmem_zalloc(
523 523 sizeof (tsal_pcp_req_msg_hdr_t),
524 524 KM_NOSLEEP)) == NULL) {
525 525 goto FAIL;
526 526 }
527 527 if ((softc->resp_msg_hdr = (tsal_pcp_resp_msg_hdr_t *)kmem_zalloc(
528 528 sizeof (tsal_pcp_resp_msg_hdr_t),
529 529 KM_NOSLEEP)) == NULL) {
530 530 goto FAIL;
531 531 }
532 532 if ((softc->peek_area = (uint8_t *)kmem_zalloc(softc->mtu_size,
533 533 KM_NOSLEEP)) == NULL) {
534 534 goto FAIL;
535 535 }
536 536 if ((softc->peek_read_area = (uint8_t *)kmem_zalloc(2*softc->mtu_size,
537 537 KM_NOSLEEP)) == NULL) {
538 538 goto FAIL;
539 539 }
540 540
541 541 rv = 0;
542 542
543 543 FAIL:
544 544 if (rv != 0) {
545 545 if (softc->flags & TSAL_OPENED)
546 546 (void) ldi_close(softc->lh, FREAD|FWRITE, credp);
547 547 if (softc->flags * TSAL_IDENTED)
548 548 (void) ldi_ident_release(softc->li);
549 549 softc->flags &= ~(TSAL_OPENED | TSAL_IDENTED);
550 550 if (softc->req_ptr != NULL)
551 551 kmem_free(softc->req_ptr,
552 552 sizeof (tsal_pcp_alarm_req_t));
553 553 if (softc->resp_ptr != NULL)
554 554 kmem_free(softc->resp_ptr,
555 555 sizeof (tsal_pcp_alarm_resp_t));
556 556 if (softc->req_msg_hdr != NULL)
557 557 kmem_free(softc->req_msg_hdr,
558 558 sizeof (tsal_pcp_req_msg_hdr_t));
559 559 if (softc->resp_msg_hdr != NULL)
560 560 kmem_free(softc->resp_msg_hdr,
561 561 sizeof (tsal_pcp_resp_msg_hdr_t));
562 562 if (softc->peek_area != NULL)
563 563 kmem_free(softc->peek_area, softc->mtu_size);
564 564 if (softc->peek_read_area != NULL)
565 565 kmem_free(softc->peek_read_area, 2*softc->mtu_size);
566 566 }
567 567 mutex_exit(&softc->mutex);
568 568
569 569 return (rv);
570 570 }
571 571
572 572
573 573 /* ARGSUSED */
574 574 static int
575 575 tsalarm_close(dev_t dev, int flag, int otyp, cred_t *credp)
576 576 {
577 577 int rv, inst = getminor(dev);
578 578 struct tsalarm_softc *softc;
579 579
580 580 softc = (struct tsalarm_softc *)getsoftc(inst);
581 581
582 582 if (softc == NULL) {
583 583 return (EIO);
584 584 }
585 585
586 586 mutex_enter(&softc->mutex);
587 587
588 588 rv = ldi_close(softc->lh, FREAD | FWRITE, kcred);
589 589 if (rv != 0) {
590 590 cmn_err(CE_WARN, "ldi_close failed \n");
591 591 }
592 592
593 593 ldi_ident_release(softc->li);
594 594 softc->flags &= ~(TSAL_OPENED | TSAL_IDENTED);
595 595
596 596 mutex_exit(&softc->mutex);
597 597
598 598 /*
599 599 * free global buffers
600 600 */
601 601 if (softc->read_area != NULL) {
602 602 kmem_free(softc->read_area, 2*softc->mtu_size);
603 603 softc->read_area = NULL;
604 604 }
605 605 if (softc->req_ptr != NULL) {
606 606 kmem_free(softc->req_ptr,
607 607 sizeof (tsal_pcp_alarm_req_t));
608 608 softc->req_ptr = NULL;
609 609 }
610 610 if (softc->resp_ptr != NULL) {
611 611 kmem_free(softc->resp_ptr,
612 612 sizeof (tsal_pcp_alarm_resp_t));
613 613 softc->resp_ptr = NULL;
614 614 }
615 615 if (softc->req_msg_hdr != NULL) {
616 616 kmem_free(softc->req_msg_hdr,
617 617 sizeof (tsal_pcp_req_msg_hdr_t));
618 618 softc->req_msg_hdr = NULL;
619 619 }
620 620 if (softc->resp_msg_hdr != NULL) {
621 621 kmem_free(softc->resp_msg_hdr,
622 622 sizeof (tsal_pcp_resp_msg_hdr_t));
623 623 softc->resp_msg_hdr = NULL;
624 624 }
625 625 if (softc->peek_area != NULL) {
626 626 kmem_free(softc->peek_area, softc->mtu_size);
627 627 softc->peek_area = NULL;
628 628 }
629 629 if (softc->peek_read_area != NULL) {
630 630 kmem_free(softc->peek_read_area, 2*softc->mtu_size);
631 631 softc->peek_read_area = NULL;
632 632 }
633 633
634 634 return (rv);
635 635 }
636 636
637 637
638 638 /* ARGSUSED */
639 639 static int
640 640 tsalarm_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
641 641 cred_t *credp, int *rvalp)
642 642 {
643 643 int inst = getminor(dev);
644 644 struct tsalarm_softc *softc;
645 645 int retval = 0;
646 646 ts_aldata_t ts_alinfo;
647 647 int alarm_type, alarm_state = 0;
648 648
649 649 if ((softc = getsoftc(inst)) == NULL)
650 650 return (ENXIO);
651 651
652 652 mutex_enter(&softc->mutex);
653 653
654 654 switch (cmd) {
655 655
656 656 case LOMIOCALSTATE:
657 657 case LOMIOCALSTATE_OLD:
658 658 {
659 659 if (ddi_copyin((caddr_t)arg, (caddr_t)&ts_alinfo,
660 660 sizeof (ts_aldata_t), mode) != 0) {
661 661 retval = EFAULT;
662 662 goto end;
663 663 }
664 664
665 665 alarm_type = ts_alinfo.alarm_no;
666 666 if ((alarm_type < ALARM_CRITICAL) ||
667 667 (alarm_type > ALARM_USER)) {
668 668 retval = EINVAL;
669 669 goto end;
670 670 }
671 671
672 672 retval = glvc_alarm_get(alarm_type, &alarm_state,
673 673 softc);
674 674
675 675 if (retval != 0)
676 676 goto end;
677 677
678 678 if ((alarm_state != 0) && (alarm_state != 1)) {
679 679 retval = EIO;
680 680 goto end;
681 681 }
682 682
683 683 ts_alinfo.alarm_state = alarm_state;
684 684 if (ddi_copyout((caddr_t)&ts_alinfo, (caddr_t)arg,
685 685 sizeof (ts_aldata_t), mode) != 0) {
686 686 retval = EFAULT;
687 687 goto end;
688 688 }
689 689 }
690 690 break;
691 691
692 692 case LOMIOCALCTL:
693 693 case LOMIOCALCTL_OLD:
694 694 {
695 695 if (ddi_copyin((caddr_t)arg, (caddr_t)&ts_alinfo,
696 696 sizeof (ts_aldata_t), mode) != 0) {
697 697 retval = EFAULT;
698 698 goto end;
699 699 }
700 700
701 701 alarm_type = ts_alinfo.alarm_no;
702 702 alarm_state = ts_alinfo.alarm_state;
703 703
704 704 if ((alarm_type < ALARM_CRITICAL) ||
705 705 (alarm_type > ALARM_USER)) {
706 706 retval = EINVAL;
707 707 goto end;
708 708 }
709 709 if ((alarm_state < ALARM_OFF) ||
710 710 (alarm_state > ALARM_ON)) {
711 711 retval = EINVAL;
712 712 goto end;
713 713 }
714 714
715 715 retval = glvc_alarm_set(alarm_type, alarm_state, softc);
716 716 }
717 717 break;
718 718
719 719 default:
720 720 retval = EINVAL;
721 721 break;
722 722 }
723 723
724 724 end:
725 725 mutex_exit(&softc->mutex);
726 726
727 727 return (retval);
728 728 }
729 729
730 730 static int
731 731 glvc_alarm_get(int alarm_type, int *alarm_state, tsalarm_softc_t *sc)
732 732 {
733 733 tsal_pcp_alarm_req_t *req_ptr = NULL;
734 734 tsal_pcp_alarm_resp_t *resp_ptr = NULL;
735 735 tsal_pcp_msg_t send_msg;
736 736 tsal_pcp_msg_t recv_msg;
737 737 int status = -1;
738 738
739 739 /*
740 740 * setup the request data to attach to the libpcp msg
741 741 */
742 742 if (sc->req_ptr == NULL) {
743 743 goto alarm_return;
744 744 }
745 745
746 746 req_ptr = sc->req_ptr;
747 747
748 748 req_ptr->alarm_action = PCP_ALARM_STATUS;
749 749 req_ptr->alarm_id = alarm_type;
750 750
751 751 send_msg.msg_type = PCP_ALARM_CONTROL;
↓ open down ↓ |
751 lines elided |
↑ open up ↑ |
752 752 send_msg.sub_type = NULL;
753 753 send_msg.msg_len = sizeof (tsal_pcp_alarm_req_t);
754 754 send_msg.msg_data = (uint8_t *)req_ptr;
755 755
756 756 /*
757 757 * send the request, receive the response
758 758 */
759 759 if (tsal_pcp_send_recv(sc, &send_msg, &recv_msg,
760 760 PCP_COMM_TIMEOUT) < 0) {
761 761 /* we either timed out or erred; either way try again */
762 - (void) delay(PCP_COMM_TIMEOUT * drv_usectohz(1000000));
762 + (void) delay(drv_sectohz(PCP_COMM_TIMEOUT));
763 763
764 764 if (tsal_pcp_send_recv(sc, &send_msg, &recv_msg,
765 765 PCP_COMM_TIMEOUT) < 0) {
766 766 cmn_err(CE_WARN, "tsalarm: communication failure");
767 767 goto alarm_return;
768 768 }
769 769 }
770 770
771 771 /*
772 772 * validate that this data was meant for us
773 773 */
774 774 if (recv_msg.msg_type != PCP_ALARM_CONTROL_R) {
775 775 cmn_err(CE_WARN, "tsalarm: unbound packet received");
776 776 goto alarm_return;
777 777 }
778 778
779 779 /*
780 780 * verify that the Alarm action has taken place
781 781 */
782 782 resp_ptr = (tsal_pcp_alarm_resp_t *)recv_msg.msg_data;
783 783 if (resp_ptr->status == PCP_ALARM_ERROR) {
784 784 cmn_err(CE_WARN, "tsalarm: failed to get alarm status");
785 785 goto alarm_return;
786 786 }
787 787
788 788 if (resp_ptr->alarm_state == ALARM_STATE_UNKNOWN)
789 789 cmn_err(CE_WARN, "tsalarm: ALARM set to unknown state");
790 790
791 791 *alarm_state = resp_ptr->alarm_state;
792 792 status = TSAL_PCP_OK;
793 793
794 794 alarm_return:
795 795 return (status);
796 796 }
797 797
798 798 static int
799 799 glvc_alarm_set(int alarm_type, int new_state, tsalarm_softc_t *sc)
800 800 {
801 801 tsal_pcp_alarm_req_t *req_ptr = NULL;
802 802 tsal_pcp_alarm_resp_t *resp_ptr = NULL;
803 803 tsal_pcp_msg_t send_msg;
804 804 tsal_pcp_msg_t recv_msg;
805 805 int status = -1;
806 806
807 807 /*
808 808 * setup the request data to attach to the libpcp msg
809 809 */
810 810 if (sc->req_ptr == NULL) {
811 811 if ((sc->req_ptr = (tsal_pcp_alarm_req_t *)kmem_zalloc(
812 812 sizeof (tsal_pcp_alarm_req_t),
813 813 KM_NOSLEEP)) == NULL)
814 814 goto alarm_return;
815 815 }
816 816
817 817 req_ptr = sc->req_ptr;
818 818
819 819 if (new_state == ALARM_ON)
820 820 req_ptr->alarm_action = PCP_ALARM_ENABLE;
821 821 else if (new_state == ALARM_OFF)
822 822 req_ptr->alarm_action = PCP_ALARM_DISABLE;
823 823
824 824 req_ptr->alarm_id = alarm_type;
825 825
826 826 send_msg.msg_type = PCP_ALARM_CONTROL;
↓ open down ↓ |
54 lines elided |
↑ open up ↑ |
827 827 send_msg.sub_type = NULL;
828 828 send_msg.msg_len = sizeof (tsal_pcp_alarm_req_t);
829 829 send_msg.msg_data = (uint8_t *)req_ptr;
830 830
831 831 /*
832 832 * send the request, receive the response
833 833 */
834 834 if (tsal_pcp_send_recv(sc, &send_msg, &recv_msg,
835 835 PCP_COMM_TIMEOUT) < 0) {
836 836 /* we either timed out or erred; either way try again */
837 - (void) delay(PCP_COMM_TIMEOUT * drv_usectohz(1000000));
837 + (void) delay(drv_sectohz(PCP_COMM_TIMEOUT));
838 838
839 839 if (tsal_pcp_send_recv(sc, &send_msg, &recv_msg,
840 840 PCP_COMM_TIMEOUT) < 0) {
841 841 goto alarm_return;
842 842 }
843 843 }
844 844
845 845 /*
846 846 * validate that this data was meant for us
847 847 */
848 848 if (recv_msg.msg_type != PCP_ALARM_CONTROL_R) {
849 849 cmn_err(CE_WARN, "tsalarm: unbound packet received");
850 850 goto alarm_return;
851 851 }
852 852
853 853 /*
854 854 * verify that the Alarm action has taken place
855 855 */
856 856 resp_ptr = (tsal_pcp_alarm_resp_t *)recv_msg.msg_data;
857 857 if (resp_ptr->status == PCP_ALARM_ERROR) {
858 858 cmn_err(CE_WARN, "tsalarm: failed to set alarm status");
859 859 goto alarm_return;
860 860 }
861 861
862 862 /*
863 863 * ensure the Alarm action taken is the one requested
864 864 */
865 865 if ((req_ptr->alarm_action == PCP_ALARM_DISABLE) &&
866 866 (resp_ptr->alarm_state != ALARM_STATE_OFF)) {
867 867 cmn_err(CE_WARN, "tsalarm: failed to set alarm");
868 868 goto alarm_return;
869 869 } else if ((req_ptr->alarm_action == PCP_ALARM_ENABLE) &&
870 870 (resp_ptr->alarm_state != ALARM_STATE_ON)) {
871 871 cmn_err(CE_WARN, "tsalarm: failed to set alarm");
872 872 goto alarm_return;
873 873 } else if (resp_ptr->alarm_state == ALARM_STATE_UNKNOWN) {
874 874 cmn_err(CE_WARN, "tsalarm: Alarm set to unknown state");
875 875 goto alarm_return;
876 876 }
877 877
878 878 status = TSAL_PCP_OK;
879 879
880 880 alarm_return:
881 881 return (status);
882 882 }
883 883 /*
884 884 * Function: Send and Receive messages on platform channel.
885 885 * Arguments:
886 886 * int channel_fd - channel file descriptor.
887 887 * tsal_pcp_msg_t *req_msg - Request Message to send to other end of channel.
888 888 * tsal_pcp_msg_t *resp_msg - Response Message to be received.
889 889 * uint32_t timeout - timeout field when waiting for data from channel.
890 890 * Returns:
891 891 * 0 - success (TSAL_PCP_OK).
892 892 * (-1) - failure (TSAL_PCP_ERROR).
893 893 */
894 894 static int
895 895 tsal_pcp_send_recv(tsalarm_softc_t *sc, tsal_pcp_msg_t *req_msg,
896 896 tsal_pcp_msg_t *resp_msg, uint32_t timeout)
897 897 {
898 898 void *datap;
899 899 void *resp_msg_data = NULL;
900 900 uint32_t status;
901 901 uint16_t cksum = 0;
902 902 int ret;
903 903 int resp_hdr_ok;
904 904 tsal_pcp_req_msg_hdr_t *req_msg_hdr = NULL;
905 905 tsal_pcp_resp_msg_hdr_t *resp_msg_hdr = NULL;
906 906 #ifdef PCP_CKSUM_ENABLE
907 907 uint16_t bkup_resp_hdr_cksum;
908 908 #endif
909 909
910 910
911 911 if (req_msg == NULL) {
912 912 return (TSAL_PCP_ERROR);
913 913 }
914 914
915 915 if ((req_msg->msg_len != 0) && ((datap = req_msg->msg_data) == NULL))
916 916 return (TSAL_PCP_ERROR);
917 917
918 918 req_msg_hdr = sc->req_msg_hdr;
919 919
920 920 if (req_msg_hdr == NULL)
921 921 return (TSAL_PCP_ERROR);
922 922
923 923 if (req_msg->msg_len != 0) {
924 924 /* calculate request msg_cksum */
925 925 cksum = checksum((uint16_t *)datap, req_msg->msg_len);
926 926 }
927 927
928 928 /*
929 929 * Fill in the message header for the request packet
930 930 */
931 931 req_msg_hdr->magic_num = PCP_MAGIC_NUM;
932 932 req_msg_hdr->proto_ver = PCP_PROT_VER_1;
933 933 req_msg_hdr->msg_type = req_msg->msg_type;
934 934 req_msg_hdr->sub_type = req_msg->sub_type;
935 935 req_msg_hdr->rsvd_pad = 0;
936 936 req_msg_hdr->xid = tsal_pcp_get_xid(sc);
937 937 req_msg_hdr->msg_len = req_msg->msg_len;
938 938 req_msg_hdr->timeout = timeout;
939 939 req_msg_hdr->msg_cksum = cksum;
940 940 req_msg_hdr->hdr_cksum = 0;
941 941
942 942 /* fill request header checksum */
943 943 req_msg_hdr->hdr_cksum = checksum((uint16_t *)req_msg_hdr,
944 944 sizeof (tsal_pcp_req_msg_hdr_t));
945 945
946 946 /*
947 947 * send request message header
948 948 */
949 949 if ((ret = tsal_pcp_send_req_msg_hdr(sc, req_msg_hdr))) {
950 950 return (ret);
951 951 }
952 952
953 953 /*
954 954 * send request message
955 955 */
956 956 if (req_msg->msg_len != 0) {
957 957 if ((ret = tsal_pcp_io_op(sc, datap, req_msg->msg_len,
958 958 PCP_IO_OP_WRITE))) {
959 959 return (ret);
960 960 }
961 961 }
962 962
963 963 if (timeout == (uint32_t)PCP_TO_NO_RESPONSE)
964 964 return (TSAL_PCP_OK);
965 965
966 966 resp_msg_hdr = sc->resp_msg_hdr;
967 967
968 968 if (resp_msg_hdr == NULL) {
969 969 return (TSAL_PCP_ERROR);
970 970 }
971 971
972 972 resp_hdr_ok = 0;
973 973 while (!resp_hdr_ok) {
974 974 /*
975 975 * Receive response message header
976 976 * Note: frame error handling is done in
977 977 * 'tsal_pcp_recv_resp_msg_hdr()'.
978 978 */
979 979 if ((ret = tsal_pcp_recv_resp_msg_hdr(sc, resp_msg_hdr))) {
980 980 return (ret);
981 981 }
982 982
983 983 /*
984 984 * Check header checksum if it matches with the received hdr
985 985 * checksum.
986 986 */
987 987 #ifdef PCP_CKSUM_ENABLE
988 988 bkup_resp_hdr_cksum = resp_msg_hdr->hdr_cksum;
989 989 resp_msg_hdr->hdr_cksum = 0;
990 990 cksum = checksum((uint16_t *)resp_msg_hdr,
991 991 sizeof (tsal_pcp_resp_msg_hdr_t));
992 992
993 993 if (cksum != bkup_resp_hdr_cksum) {
994 994 return (TSAL_PCP_ERROR);
995 995 }
996 996 #endif
997 997 /*
998 998 * Check for matching request and response messages
999 999 */
1000 1000 if (resp_msg_hdr->xid != req_msg_hdr->xid) {
1001 1001 continue; /* continue reading response header */
1002 1002 }
1003 1003 resp_hdr_ok = 1;
1004 1004 }
1005 1005
1006 1006 /*
1007 1007 * check status field for any channel protocol errrors
1008 1008 * This field signifies something happend during request
1009 1009 * message trasmission. This field is set by the receiver.
1010 1010 */
1011 1011 status = resp_msg_hdr->status;
1012 1012 if (status != TSAL_PCP_OK) {
1013 1013 return (TSAL_PCP_ERROR);
1014 1014 }
1015 1015
1016 1016 if (resp_msg_hdr->msg_len != 0) {
1017 1017 if (sc->resp_ptr == NULL)
1018 1018 return (TSAL_PCP_ERROR);
1019 1019
1020 1020 resp_msg_data = (uint8_t *)sc->resp_ptr;
1021 1021 /*
1022 1022 * Receive response message.
1023 1023 */
1024 1024 if ((ret = tsal_pcp_io_op(sc, resp_msg_data,
1025 1025 resp_msg_hdr->msg_len,
1026 1026 PCP_IO_OP_READ))) {
1027 1027 return (ret);
1028 1028 }
1029 1029
1030 1030 #ifdef PCP_CKSUM_ENABLE
1031 1031 /* verify response message data checksum */
1032 1032 cksum = checksum((uint16_t *)resp_msg_data,
1033 1033 resp_msg_hdr->msg_len);
1034 1034 if (cksum != resp_msg_hdr->msg_cksum) {
1035 1035 return (TSAL_PCP_ERROR);
1036 1036 }
1037 1037 #endif
1038 1038 }
1039 1039 /* Everything is okay put the received data into user */
1040 1040 /* resp_msg struct */
1041 1041 resp_msg->msg_len = resp_msg_hdr->msg_len;
1042 1042 resp_msg->msg_type = resp_msg_hdr->msg_type;
1043 1043 resp_msg->sub_type = resp_msg_hdr->sub_type;
1044 1044 resp_msg->msg_data = (uint8_t *)resp_msg_data;
1045 1045
1046 1046 return (TSAL_PCP_OK);
1047 1047 }
1048 1048
1049 1049 /*
1050 1050 * Function: wrapper for handling glvc calls (read/write/peek).
1051 1051 */
1052 1052 static int
1053 1053 tsal_pcp_io_op(tsalarm_softc_t *sc, void *buf, int byte_cnt, int io_op)
1054 1054 {
1055 1055 int rv;
1056 1056 int n;
1057 1057 uint8_t *datap;
1058 1058 int (*func_ptr)(tsalarm_softc_t *, uint8_t *, int);
1059 1059 int io_sz;
1060 1060 int try_cnt;
1061 1061
1062 1062 if ((buf == NULL) || (byte_cnt < 0)) {
1063 1063 return (TSAL_PCP_ERROR);
1064 1064 }
1065 1065
1066 1066 switch (io_op) {
1067 1067 case PCP_IO_OP_READ:
1068 1068 func_ptr = tsal_pcp_read;
1069 1069 break;
1070 1070 case PCP_IO_OP_WRITE:
1071 1071 func_ptr = tsal_pcp_write;
1072 1072 break;
1073 1073 case PCP_IO_OP_PEEK:
1074 1074 func_ptr = tsal_pcp_peek;
1075 1075 break;
1076 1076 default:
1077 1077 return (TSAL_PCP_ERROR);
1078 1078 }
1079 1079
1080 1080 /*
1081 1081 * loop until all I/O done, try limit exceded, or real failure
1082 1082 */
1083 1083
1084 1084 rv = 0;
1085 1085 datap = buf;
↓ open down ↓ |
238 lines elided |
↑ open up ↑ |
1086 1086 while (rv < byte_cnt) {
1087 1087 io_sz = MIN((byte_cnt - rv), sc->mtu_size);
1088 1088 try_cnt = 0;
1089 1089 while ((n = (*func_ptr)(sc, datap, io_sz)) < 0) {
1090 1090 try_cnt++;
1091 1091 if (try_cnt > PCP_MAX_TRY_CNT) {
1092 1092 rv = n;
1093 1093 goto done;
1094 1094 }
1095 1095 /* waiting 5 secs. Do we need 5 Secs? */
1096 - (void) delay(PCP_GLVC_SLEEP * drv_usectohz(1000000));
1096 + (void) delay(drv_sectohz(PCP_GLVC_SLEEP));
1097 1097 } /* while trying the io operation */
1098 1098
1099 1099 if (n < 0) {
1100 1100 rv = n;
1101 1101 goto done;
1102 1102 }
1103 1103 rv += n;
1104 1104 datap += n;
1105 1105 } /* while still have more data */
1106 1106
1107 1107 done:
1108 1108 if (rv == byte_cnt)
1109 1109 return (0);
1110 1110 else
1111 1111 return (TSAL_PCP_ERROR);
1112 1112 }
1113 1113
1114 1114 /*
1115 1115 * For peeking 'bytes_cnt' bytes in channel (glvc) buffers.
1116 1116 * If data is available, the data is copied into 'buf'.
1117 1117 */
1118 1118 static int
1119 1119 tsal_pcp_peek(tsalarm_softc_t *sc, uint8_t *buf, int bytes_cnt)
1120 1120 {
1121 1121 int ret, rval;
1122 1122 glvc_xport_msg_peek_t peek_ctrl;
1123 1123 int n, m;
1124 1124
1125 1125 if (bytes_cnt < 0 || bytes_cnt > sc->mtu_size) {
1126 1126 return (TSAL_PCP_ERROR);
1127 1127 }
1128 1128
1129 1129 /*
1130 1130 * initialization of buffers used for peeking data in channel buffers.
1131 1131 */
1132 1132 if (sc->peek_area == NULL) {
1133 1133 return (TSAL_PCP_ERROR);
1134 1134 }
1135 1135
1136 1136 /*
1137 1137 * peek max MTU size bytes
1138 1138 */
1139 1139 peek_ctrl.buf = (caddr_t)sc->peek_area;
1140 1140 peek_ctrl.buflen = sc->mtu_size;
1141 1141 peek_ctrl.flags = 0;
1142 1142
1143 1143 if ((ret = ldi_ioctl(sc->lh, GLVC_XPORT_IOCTL_DATA_PEEK,
1144 1144 (intptr_t)&peek_ctrl, FKIOCTL, kcred, &rval)) < 0) {
1145 1145 return (ret);
1146 1146 }
1147 1147
1148 1148 n = peek_ctrl.buflen;
1149 1149
1150 1150 if (n < 0)
1151 1151 return (TSAL_PCP_ERROR);
1152 1152
1153 1153 /*
1154 1154 * satisfy request as best as we can
1155 1155 */
1156 1156 m = MIN(bytes_cnt, n);
1157 1157 (void) memcpy(buf, sc->peek_area, m);
1158 1158
1159 1159 return (m);
1160 1160 }
1161 1161
1162 1162 /*
1163 1163 * Function: write 'byte_cnt' bytes from 'buf' to channel.
1164 1164 */
1165 1165 static int
1166 1166 tsal_pcp_write(tsalarm_softc_t *sc, uint8_t *buf, int byte_cnt)
1167 1167 {
1168 1168 int ret;
1169 1169 struct uio uio;
1170 1170 struct iovec iov;
1171 1171
1172 1172 /* check for valid arguments */
1173 1173 if (buf == NULL || byte_cnt < 0 || byte_cnt > sc->mtu_size) {
1174 1174 return (TSAL_PCP_ERROR);
1175 1175 }
1176 1176 bzero(&uio, sizeof (uio));
1177 1177 bzero(&iov, sizeof (iov));
1178 1178 iov.iov_base = (int8_t *)buf;
1179 1179 iov.iov_len = byte_cnt;
1180 1180 uio.uio_iov = &iov;
1181 1181 uio.uio_iovcnt = 1;
1182 1182 uio.uio_loffset = 0;
1183 1183 uio.uio_segflg = UIO_SYSSPACE;
1184 1184 uio.uio_resid = byte_cnt;
1185 1185
1186 1186 if ((ret = ldi_write(sc->lh, &uio, kcred)) < 0) {
1187 1187 return (ret);
1188 1188 }
1189 1189 return (byte_cnt - iov.iov_len);
1190 1190 }
1191 1191
1192 1192 /*
1193 1193 * In current implementaion of glvc driver, streams reads are not supported.
1194 1194 * tsal_pcp_read mimics stream reads by first reading all the bytes present in
1195 1195 * channel buffer into a local buffer and from then on read requests
1196 1196 * are serviced from local buffer. When read requests are not serviceble
1197 1197 * from local buffer, it repeates by first reading data from channel buffers.
1198 1198 */
1199 1199
1200 1200 static int
1201 1201 tsal_pcp_read(tsalarm_softc_t *sc, uint8_t *buf, int byte_cnt)
1202 1202 {
1203 1203 int ret;
1204 1204 int n, m, i;
1205 1205 struct uio uio;
1206 1206 struct iovec iov;
1207 1207 int read_area_size = 0;
1208 1208
1209 1209 if (byte_cnt < 0 || byte_cnt > sc->mtu_size) {
1210 1210 return (TSAL_PCP_ERROR);
1211 1211 }
1212 1212
1213 1213 read_area_size = 2*sc->mtu_size;
1214 1214 /*
1215 1215 * initialization of local read buffer
1216 1216 * from which the stream read requests are serviced.
1217 1217 */
1218 1218 if (sc->read_area == NULL) {
1219 1219 sc->read_area = (uint8_t *)kmem_zalloc(read_area_size,
1220 1220 KM_NOSLEEP);
1221 1221 if (sc->read_area == NULL) {
1222 1222 return (TSAL_PCP_ERROR);
1223 1223 }
1224 1224 sc->read_head = sc->read_area;
1225 1225 sc->read_tail = sc->read_area;
1226 1226 }
1227 1227
1228 1228 /*
1229 1229 * if we already read this data then copy from local buffer it self
1230 1230 * without calling new read.
1231 1231 */
1232 1232 if (byte_cnt <= (sc->read_tail - sc->read_head)) {
1233 1233 (void) memcpy(buf, sc->read_head, byte_cnt);
1234 1234 sc->read_head += byte_cnt;
1235 1235 return (byte_cnt);
1236 1236 }
1237 1237
1238 1238 /*
1239 1239 * if the request is not satisfied from the buffered data, then move
1240 1240 * remaining data to front of the buffer and read new data.
1241 1241 */
1242 1242 for (i = 0; i < (sc->read_tail - sc->read_head); ++i) {
1243 1243 sc->read_area[i] = sc->read_head[i];
1244 1244 }
1245 1245 sc->read_head = sc->read_area;
1246 1246 sc->read_tail = sc->read_head + i;
1247 1247
1248 1248 /*
1249 1249 * do a peek to see how much data is available and read complete data.
1250 1250 */
1251 1251
1252 1252 if ((m = tsal_pcp_peek(sc, sc->read_tail, sc->mtu_size)) < 0) {
1253 1253 return (m);
1254 1254 }
1255 1255
1256 1256 bzero(&uio, sizeof (uio));
1257 1257 bzero(&iov, sizeof (iov));
1258 1258 iov.iov_base = (int8_t *)sc->read_tail;
1259 1259 iov.iov_len = m;
1260 1260 uio.uio_iov = &iov;
1261 1261 uio.uio_iovcnt = 1;
1262 1262 uio.uio_loffset = 0;
1263 1263 uio.uio_segflg = UIO_SYSSPACE;
1264 1264 uio.uio_resid = m;
1265 1265
1266 1266 if ((ret = ldi_read(sc->lh, &uio, kcred)) != 0) {
1267 1267 return (ret);
1268 1268 }
1269 1269
1270 1270 sc->read_tail += (m - iov.iov_len);
1271 1271
1272 1272 /*
1273 1273 * copy the requested bytes.
1274 1274 */
1275 1275 n = MIN(byte_cnt, (sc->read_tail - sc->read_head));
1276 1276 (void) memcpy(buf, sc->read_head, n);
1277 1277
1278 1278 sc->read_head += n;
1279 1279
1280 1280 return (n);
1281 1281 }
1282 1282 /*
1283 1283 * This function is slight different from tsal_pcp_peek. The peek requests are
1284 1284 * serviced from local read buffer, if data is available. If the peek request
1285 1285 * is not serviceble from local read buffer, then the data is peeked from
1286 1286 * channel buffer. This function is mainly used for proper protocol framing
1287 1287 * error handling.
1288 1288 */
1289 1289 static int
1290 1290 tsal_pcp_peek_read(tsalarm_softc_t *sc, uint8_t *buf, int byte_cnt)
1291 1291 {
1292 1292 int n, m, i;
1293 1293 uint8_t *peek_read_head = NULL;
1294 1294 uint8_t *peek_read_tail = NULL;
1295 1295
1296 1296 if (byte_cnt < 0 || byte_cnt > sc->mtu_size) {
1297 1297 return (TSAL_PCP_ERROR);
1298 1298 }
1299 1299
1300 1300 /*
1301 1301 * if we already have the data in local read buffer then copy
1302 1302 * from local buffer it self w/out calling new peek
1303 1303 */
1304 1304 if (byte_cnt <= (sc->read_tail - sc->read_head)) {
1305 1305 (void) memcpy(buf, sc->read_head, byte_cnt);
1306 1306 return (byte_cnt);
1307 1307 }
1308 1308
1309 1309
1310 1310 if (sc->peek_read_area == NULL) {
1311 1311 return (TSAL_PCP_ERROR);
1312 1312 }
1313 1313 peek_read_head = sc->peek_read_area;
1314 1314 peek_read_tail = sc->peek_read_area;
1315 1315
1316 1316 /*
1317 1317 * if the request is not satisfied from local read buffer, then first
1318 1318 * copy the remaining data in local read buffer to peek_read_area and
1319 1319 * then issue new peek.
1320 1320 */
1321 1321 for (i = 0; i < (sc->read_tail - sc->read_head); ++i) {
1322 1322 sc->peek_read_area[i] = sc->read_head[i];
1323 1323 }
1324 1324 peek_read_head = sc->peek_read_area;
1325 1325 peek_read_tail = peek_read_head + i;
1326 1326
1327 1327 /*
1328 1328 * do a peek to see how much data is available and read complete data.
1329 1329 */
1330 1330
1331 1331 if ((m = tsal_pcp_peek(sc, peek_read_tail, sc->mtu_size)) < 0) {
1332 1332 return (m);
1333 1333 }
1334 1334
1335 1335 peek_read_tail += m;
1336 1336
1337 1337 /*
1338 1338 * copy the requested bytes
1339 1339 */
1340 1340 n = MIN(byte_cnt, (peek_read_tail - peek_read_head));
1341 1341 (void) memcpy(buf, peek_read_head, n);
1342 1342
1343 1343 return (n);
1344 1344 }
1345 1345 /*
1346 1346 * Send Request Message Header.
1347 1347 */
1348 1348 static int
1349 1349 tsal_pcp_send_req_msg_hdr(tsalarm_softc_t *sc, tsal_pcp_req_msg_hdr_t *req_hdr)
1350 1350 {
1351 1351 tsal_pcp_req_msg_hdr_t *hdrp;
1352 1352 int hdr_sz;
1353 1353 int ret;
1354 1354
1355 1355 hdr_sz = sizeof (tsal_pcp_req_msg_hdr_t);
1356 1356 if ((hdrp = (tsal_pcp_req_msg_hdr_t *)kmem_zalloc(hdr_sz,
1357 1357 KM_NOSLEEP)) == NULL) {
1358 1358 return (TSAL_PCP_ERROR);
1359 1359 }
1360 1360
1361 1361 hdrp->magic_num = htonl(req_hdr->magic_num);
1362 1362 hdrp->proto_ver = req_hdr->proto_ver;
1363 1363 hdrp->msg_type = req_hdr->msg_type;
1364 1364 hdrp->sub_type = req_hdr->sub_type;
1365 1365 hdrp->rsvd_pad = htons(req_hdr->rsvd_pad);
1366 1366 hdrp->xid = htonl(req_hdr->xid);
1367 1367 hdrp->timeout = htonl(req_hdr->timeout);
1368 1368 hdrp->msg_len = htonl(req_hdr->msg_len);
1369 1369 hdrp->msg_cksum = htons(req_hdr->msg_cksum);
1370 1370 hdrp->hdr_cksum = htons(req_hdr->hdr_cksum);
1371 1371
1372 1372 if ((ret = tsal_pcp_io_op(sc, (char *)hdrp, hdr_sz,
1373 1373 PCP_IO_OP_WRITE)) != 0) {
1374 1374 kmem_free(hdrp, hdr_sz);
1375 1375 return (ret);
1376 1376 }
1377 1377 kmem_free(hdrp, hdr_sz);
1378 1378 return (TSAL_PCP_OK);
1379 1379 }
1380 1380 /*
1381 1381 * Receive Response message header.
1382 1382 */
1383 1383 static int
1384 1384 tsal_pcp_recv_resp_msg_hdr(tsalarm_softc_t *sc,
1385 1385 tsal_pcp_resp_msg_hdr_t *resp_hdr)
1386 1386 {
1387 1387 uint32_t magic_num;
1388 1388 uint8_t proto_ver;
1389 1389 uint8_t msg_type;
1390 1390 uint8_t sub_type;
1391 1391 uint8_t rsvd_pad;
1392 1392 uint32_t xid;
1393 1393 uint32_t timeout;
1394 1394 uint32_t msg_len;
1395 1395 uint32_t status;
1396 1396 uint16_t msg_cksum;
1397 1397 uint16_t hdr_cksum;
1398 1398 int ret;
1399 1399
1400 1400 if (resp_hdr == NULL) {
1401 1401 return (TSAL_PCP_ERROR);
1402 1402 }
1403 1403
1404 1404 /*
1405 1405 * handle protocol framing errors.
1406 1406 * tsal_pcp_frame_error_handle() returns when proper frame arrived
1407 1407 * (magic seq) or if an error happens while reading data from
1408 1408 * channel.
1409 1409 */
1410 1410 if ((ret = tsal_pcp_frame_error_handle(sc)) != 0) {
1411 1411 return (TSAL_PCP_ERROR);
1412 1412 }
1413 1413
1414 1414 /* read magic number first */
1415 1415 if ((ret = tsal_pcp_io_op(sc, &magic_num, sizeof (magic_num),
1416 1416 PCP_IO_OP_READ)) != 0) {
1417 1417 return (ret);
1418 1418 }
1419 1419
1420 1420 magic_num = ntohl(magic_num);
1421 1421
1422 1422 if (magic_num != PCP_MAGIC_NUM) {
1423 1423 return (TSAL_PCP_ERROR);
1424 1424 }
1425 1425
1426 1426 /* read version field */
1427 1427 if ((ret = tsal_pcp_io_op(sc, &proto_ver, sizeof (proto_ver),
1428 1428 PCP_IO_OP_READ)) != 0) {
1429 1429 return (ret);
1430 1430 }
1431 1431
1432 1432 /* check protocol version */
1433 1433 if (proto_ver != PCP_PROT_VER_1) {
1434 1434 return (TSAL_PCP_ERROR);
1435 1435 }
1436 1436
1437 1437 /* Read message type */
1438 1438 if ((ret = tsal_pcp_io_op(sc, &msg_type, sizeof (msg_type),
1439 1439 PCP_IO_OP_READ)) != 0) {
1440 1440 return (ret);
1441 1441 }
1442 1442
1443 1443 /* Read message sub type */
1444 1444 if ((ret = tsal_pcp_io_op(sc, &sub_type, sizeof (sub_type),
1445 1445 PCP_IO_OP_READ)) != 0) {
1446 1446 return (ret);
1447 1447 }
1448 1448
1449 1449 /* Read rcvd_pad bits */
1450 1450 if ((ret = tsal_pcp_io_op(sc, &rsvd_pad, sizeof (rsvd_pad),
1451 1451 PCP_IO_OP_READ)) != 0) {
1452 1452 return (ret);
1453 1453 }
1454 1454
1455 1455 /* receive transaction id */
1456 1456 if ((ret = tsal_pcp_io_op(sc, &xid, sizeof (xid),
1457 1457 PCP_IO_OP_READ)) != 0) {
1458 1458 return (ret);
1459 1459 }
1460 1460
1461 1461 xid = ntohl(xid);
1462 1462
1463 1463 /* receive timeout value */
1464 1464 if ((ret = tsal_pcp_io_op(sc, &timeout, sizeof (timeout),
1465 1465 PCP_IO_OP_READ)) != 0) {
1466 1466 return (ret);
1467 1467 }
1468 1468
1469 1469 timeout = ntohl(timeout);
1470 1470
1471 1471 /* receive message length */
1472 1472 if ((ret = tsal_pcp_io_op(sc, &msg_len, sizeof (msg_len),
1473 1473 PCP_IO_OP_READ)) != 0) {
1474 1474 return (ret);
1475 1475 }
1476 1476
1477 1477 msg_len = ntohl(msg_len);
1478 1478
1479 1479 /* receive status field */
1480 1480 if ((ret = tsal_pcp_io_op(sc, &status, sizeof (status),
1481 1481 PCP_IO_OP_READ)) != 0) {
1482 1482 return (ret);
1483 1483 }
1484 1484
1485 1485 status = ntohl(status);
1486 1486
1487 1487 /* receive message checksum */
1488 1488 if ((ret = tsal_pcp_io_op(sc, &msg_cksum, sizeof (msg_cksum),
1489 1489 PCP_IO_OP_READ)) != 0) {
1490 1490 return (ret);
1491 1491 }
1492 1492
1493 1493 msg_cksum = ntohs(msg_cksum);
1494 1494
1495 1495 /* receive header checksum */
1496 1496 if ((ret = tsal_pcp_io_op(sc, &hdr_cksum, sizeof (hdr_cksum),
1497 1497 PCP_IO_OP_READ)) != 0) {
1498 1498 return (ret);
1499 1499 }
1500 1500
1501 1501 hdr_cksum = ntohs(hdr_cksum);
1502 1502
1503 1503 /* copy to resp_hdr */
1504 1504
1505 1505 resp_hdr->magic_num = magic_num;
1506 1506 resp_hdr->proto_ver = proto_ver;
1507 1507 resp_hdr->msg_type = msg_type;
1508 1508 resp_hdr->sub_type = sub_type;
1509 1509 resp_hdr->rsvd_pad = rsvd_pad;
1510 1510 resp_hdr->xid = xid;
1511 1511 resp_hdr->timeout = timeout;
1512 1512 resp_hdr->msg_len = msg_len;
1513 1513 resp_hdr->status = status;
1514 1514 resp_hdr->msg_cksum = msg_cksum;
1515 1515 resp_hdr->hdr_cksum = hdr_cksum;
1516 1516
1517 1517 return (TSAL_PCP_OK);
1518 1518 }
1519 1519
1520 1520 /*
1521 1521 * Get next xid for including in request message.
1522 1522 * Every request and response message are matched
1523 1523 * for same xid.
1524 1524 */
1525 1525
1526 1526 static uint32_t
1527 1527 tsal_pcp_get_xid(tsalarm_softc_t *sc)
1528 1528 {
1529 1529 uint32_t ret;
1530 1530 static boolean_t xid_initialized = B_FALSE;
1531 1531
1532 1532 if (xid_initialized == B_FALSE) {
1533 1533 xid_initialized = B_TRUE;
1534 1534 /*
1535 1535 * starting xid is initialized to a different value everytime
1536 1536 * user application is restarted so that user apps will not
1537 1537 * receive previous session's packets.
1538 1538 *
1539 1539 * Note: The algorithm for generating initial xid is partially
1540 1540 * taken from Solaris rpc code.
1541 1541 */
1542 1542 sc->msg_xid = (uint32_t)gethrtime();
1543 1543 }
1544 1544
1545 1545 ret = sc->msg_xid++;
1546 1546
1547 1547 /* zero xid is not allowed */
1548 1548 if (ret == 0)
1549 1549 ret = sc->msg_xid++;
1550 1550
1551 1551 return (ret);
1552 1552 }
1553 1553
1554 1554 /*
1555 1555 * This function handles channel framing errors. It waits until proper
1556 1556 * frame with starting sequence as magic numder (0xAFBCAFA0)
1557 1557 * is arrived. It removes unexpected data (before the magic number sequence)
1558 1558 * on the channel. It returns when proper magic number sequence is seen
1559 1559 * or when any failure happens while reading/peeking the channel.
1560 1560 */
1561 1561 static int
1562 1562 tsal_pcp_frame_error_handle(tsalarm_softc_t *sc)
1563 1563 {
1564 1564 uint8_t magic_num_buf[4];
1565 1565 int ispresent = 0;
1566 1566 uint32_t net_magic_num; /* magic byte in network byte order */
1567 1567 uint32_t host_magic_num = PCP_MAGIC_NUM;
1568 1568 uint8_t buf[2];
1569 1569
1570 1570 net_magic_num = htonl(host_magic_num);
1571 1571 (void) memcpy(magic_num_buf, (uint8_t *)&net_magic_num, 4);
1572 1572
1573 1573 while (!ispresent) {
1574 1574 /*
1575 1575 * Check if next four bytes matches pcp magic number.
1576 1576 * if mathing not found, discard 1 byte and continue checking.
1577 1577 */
1578 1578 if (!check_magic_byte_presence(sc, 4, &magic_num_buf[0],
1579 1579 &ispresent)) {
1580 1580 if (!ispresent) {
1581 1581 /* remove 1 byte */
1582 1582 (void) tsal_pcp_io_op(sc, buf, 1,
1583 1583 PCP_IO_OP_READ);
1584 1584 }
1585 1585 } else {
1586 1586 return (-1);
1587 1587 }
1588 1588 }
1589 1589
1590 1590 return (0);
1591 1591 }
1592 1592
1593 1593 /*
1594 1594 * checks whether certain byte sequence is present in the data stream.
1595 1595 */
1596 1596 static int
1597 1597 check_magic_byte_presence(tsalarm_softc_t *sc,
1598 1598 int byte_cnt, uint8_t *byte_seq, int *ispresent)
1599 1599 {
1600 1600 int ret, i;
1601 1601 uint8_t buf[4];
1602 1602
1603 1603 if ((ret = tsal_pcp_peek_read(sc, buf, byte_cnt)) < 0) {
1604 1604 return (ret);
1605 1605 }
1606 1606
1607 1607 /* 'byte_cnt' bytes not present */
1608 1608 if (ret != byte_cnt) {
1609 1609 *ispresent = 0;
1610 1610 return (0);
1611 1611 }
1612 1612
1613 1613 for (i = 0; i < byte_cnt; ++i) {
1614 1614 if (buf[i] != byte_seq[i]) {
1615 1615 *ispresent = 0;
1616 1616 return (0);
1617 1617 }
1618 1618 }
1619 1619 *ispresent = 1;
1620 1620
1621 1621 return (0);
1622 1622 }
1623 1623
1624 1624 /*
1625 1625 * 16-bit simple internet checksum
1626 1626 */
1627 1627 static uint16_t
1628 1628 checksum(uint16_t *addr, int32_t count)
1629 1629 {
1630 1630 /*
1631 1631 * Compute Internet Checksum for "count" bytes
1632 1632 * beginning at location "addr".
1633 1633 */
1634 1634
1635 1635 register uint32_t sum = 0;
1636 1636
1637 1637 while (count > 1) {
1638 1638 /* This is the inner loop */
1639 1639 sum += *(unsigned short *)addr++;
1640 1640 count -= 2;
1641 1641 }
1642 1642
1643 1643 /* Add left-over byte, if any */
1644 1644 if (count > 0)
1645 1645 sum += * (unsigned char *)addr;
1646 1646
1647 1647 /* Fold 32-bit sum to 16 bits */
1648 1648 while (sum >> 16)
1649 1649 sum = (sum & 0xffff) + (sum >> 16);
1650 1650
1651 1651 sum = (~sum) & 0xffff;
1652 1652 if (sum == 0)
1653 1653 sum = 0xffff;
1654 1654
1655 1655 return (sum);
1656 1656 }
↓ open down ↓ |
550 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX