2 * Copyright (c) 2004-07 Applied Micro Circuits Corporation.
3 * Copyright (c) 2004-05 Vinod Kashyap
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * AMCC'S 3ware driver for 9000 series storage controllers.
33 * Author: Vinod Kashyap
34 * Modifications by: Adam Radford
35 * Modifications by: Manjunath Ranganathaiah
40 * Common Layer I/O functions.
44 #include "tw_osl_share.h"
45 #include "tw_cl_share.h"
46 #include "tw_cl_fwif.h"
47 #include "tw_cl_ioctl.h"
49 #include "tw_cl_externs.h"
50 #include "tw_osl_ioctl.h"
55 * Function name: tw_cl_start_io
56 * Description: Interface to OS Layer for accepting SCSI requests.
58 * Input: ctlr_handle -- controller handle
59 * req_pkt -- OSL built request packet
60 * req_handle -- request handle
62 * Return value: 0 -- success
66 tw_cl_start_io(struct tw_cl_ctlr_handle *ctlr_handle,
67 struct tw_cl_req_packet *req_pkt, struct tw_cl_req_handle *req_handle)
69 struct tw_cli_ctlr_context *ctlr;
70 struct tw_cli_req_context *req;
71 struct tw_cl_command_9k *cmd;
72 struct tw_cl_scsi_req_packet *scsi_req;
75 tw_cli_dbg_printf(10, ctlr_handle, tw_osl_cur_func(), "entered");
77 ctlr = (struct tw_cli_ctlr_context *)(ctlr_handle->cl_ctlr_ctxt);
79 if (ctlr->state & TW_CLI_CTLR_STATE_RESET_IN_PROGRESS) {
80 tw_cli_dbg_printf(2, ctlr_handle, tw_osl_cur_func(),
81 "I/O during reset: returning busy. Ctlr state = 0x%x",
83 tw_osl_ctlr_busy(ctlr_handle, req_handle);
88 * If working with a firmware version that does not support multiple
89 * luns, and this request is directed at a non-zero lun, error it
92 if ((req_pkt->gen_req_pkt.scsi_req.lun) &&
93 (ctlr->working_srl < TWA_MULTI_LUN_FW_SRL)) {
94 req_pkt->status |= (TW_CL_ERR_REQ_INVALID_LUN |
95 TW_CL_ERR_REQ_SCSI_ERROR);
96 req_pkt->tw_osl_callback(req_handle);
97 return(TW_CL_ERR_REQ_SUCCESS);
100 if ((req = tw_cli_get_request(ctlr
102 tw_cli_dbg_printf(2, ctlr_handle, tw_osl_cur_func(),
103 "Out of request context packets: returning busy");
104 tw_osl_ctlr_busy(ctlr_handle, req_handle);
105 return(TW_OSL_EBUSY);
108 req_handle->cl_req_ctxt = req;
109 req->req_handle = req_handle;
110 req->orig_req = req_pkt;
111 req->tw_cli_callback = tw_cli_complete_io;
113 req->flags |= TW_CLI_REQ_FLAGS_EXTERNAL;
114 req->flags |= TW_CLI_REQ_FLAGS_9K;
116 scsi_req = &(req_pkt->gen_req_pkt.scsi_req);
118 /* Build the cmd pkt. */
119 cmd = &(req->cmd_pkt->command.cmd_pkt_9k);
121 req->cmd_pkt->cmd_hdr.header_desc.size_header = 128;
123 cmd->res__opcode = BUILD_RES__OPCODE(0, TWA_FW_CMD_EXECUTE_SCSI);
124 cmd->unit = (TW_UINT8)(scsi_req->unit);
125 cmd->lun_l4__req_id = TW_CL_SWAP16(
126 BUILD_LUN_L4__REQ_ID(scsi_req->lun, req->request_id));
128 cmd->sgl_offset = 16; /* offset from end of hdr = max cdb len */
129 tw_osl_memcpy(cmd->cdb, scsi_req->cdb, scsi_req->cdb_len);
131 if (req_pkt->flags & TW_CL_REQ_CALLBACK_FOR_SGLIST) {
132 TW_UINT32 num_sgl_entries;
134 req_pkt->tw_osl_sgl_callback(req_handle, cmd->sg_list,
136 cmd->lun_h4__sgl_entries =
137 TW_CL_SWAP16(BUILD_LUN_H4__SGL_ENTRIES(scsi_req->lun,
140 cmd->lun_h4__sgl_entries =
141 TW_CL_SWAP16(BUILD_LUN_H4__SGL_ENTRIES(scsi_req->lun,
142 scsi_req->sgl_entries));
143 tw_cli_fill_sg_list(ctlr, scsi_req->sg_list,
144 cmd->sg_list, scsi_req->sgl_entries);
147 if ((error = tw_cli_submit_cmd(req))) {
148 tw_cli_dbg_printf(2, ctlr_handle, tw_osl_cur_func(),
149 "Could not start request. request = %p, error = %d",
151 tw_cli_req_q_insert_tail(req, TW_CLI_FREE_Q);
159 * Function name: tw_cli_submit_cmd
160 * Description: Submits a cmd to firmware.
162 * Input: req -- ptr to CL internal request context
164 * Return value: 0 -- success
168 tw_cli_submit_cmd(struct tw_cli_req_context *req)
170 struct tw_cli_ctlr_context *ctlr = req->ctlr;
171 struct tw_cl_ctlr_handle *ctlr_handle = ctlr->ctlr_handle;
172 TW_UINT32 status_reg;
174 TW_UINT8 notify_osl_of_ctlr_busy = TW_CL_FALSE;
176 tw_cli_dbg_printf(10, ctlr_handle, tw_osl_cur_func(), "entered");
178 /* Serialize access to the controller cmd queue. */
179 tw_osl_get_lock(ctlr_handle, ctlr->io_lock);
181 /* For 9650SE first write low 4 bytes */
182 if ((ctlr->device_id == TW_CL_DEVICE_ID_9K_E) ||
183 (ctlr->device_id == TW_CL_DEVICE_ID_9K_SA))
184 tw_osl_write_reg(ctlr_handle,
185 TWA_COMMAND_QUEUE_OFFSET_LOW,
186 (TW_UINT32)(req->cmd_pkt_phys + sizeof(struct tw_cl_command_header)), 4);
188 /* Check to see if we can post a command. */
189 status_reg = TW_CLI_READ_STATUS_REGISTER(ctlr_handle);
190 if ((error = tw_cli_check_ctlr_state(ctlr, status_reg)))
193 if (status_reg & TWA_STATUS_COMMAND_QUEUE_FULL) {
194 struct tw_cl_req_packet *req_pkt =
195 (struct tw_cl_req_packet *)(req->orig_req);
197 tw_cli_dbg_printf(7, ctlr_handle, tw_osl_cur_func(),
200 if ((req->flags & TW_CLI_REQ_FLAGS_INTERNAL)
202 (req_pkt->flags & TW_CL_REQ_RETRY_ON_BUSY))
204 if (req->state != TW_CLI_REQ_STATE_PENDING) {
205 tw_cli_dbg_printf(2, ctlr_handle,
207 "pending internal/ioctl request");
208 req->state = TW_CLI_REQ_STATE_PENDING;
209 tw_cli_req_q_insert_tail(req, TW_CLI_PENDING_Q);
212 error = TW_OSL_EBUSY;
214 notify_osl_of_ctlr_busy = TW_CL_TRUE;
215 error = TW_OSL_EBUSY;
218 tw_cli_dbg_printf(10, ctlr_handle, tw_osl_cur_func(),
219 "Submitting command");
221 /* Insert command into busy queue */
222 req->state = TW_CLI_REQ_STATE_BUSY;
223 tw_cli_req_q_insert_tail(req, TW_CLI_BUSY_Q);
225 if ((ctlr->device_id == TW_CL_DEVICE_ID_9K_E) ||
226 (ctlr->device_id == TW_CL_DEVICE_ID_9K_SA)) {
227 /* Now write the high 4 bytes */
228 tw_osl_write_reg(ctlr_handle,
229 TWA_COMMAND_QUEUE_OFFSET_HIGH,
230 (TW_UINT32)(((TW_UINT64)(req->cmd_pkt_phys + sizeof(struct tw_cl_command_header)))>>32), 4);
232 if (ctlr->flags & TW_CL_64BIT_ADDRESSES) {
233 /* First write the low 4 bytes, then the high 4. */
234 tw_osl_write_reg(ctlr_handle,
235 TWA_COMMAND_QUEUE_OFFSET_LOW,
236 (TW_UINT32)(req->cmd_pkt_phys + sizeof(struct tw_cl_command_header)), 4);
237 tw_osl_write_reg(ctlr_handle,
238 TWA_COMMAND_QUEUE_OFFSET_HIGH,
239 (TW_UINT32)(((TW_UINT64)(req->cmd_pkt_phys + sizeof(struct tw_cl_command_header)))>>32), 4);
241 tw_osl_write_reg(ctlr_handle,
242 TWA_COMMAND_QUEUE_OFFSET,
243 (TW_UINT32)(req->cmd_pkt_phys + sizeof(struct tw_cl_command_header)), 4);
247 tw_osl_free_lock(ctlr_handle, ctlr->io_lock);
249 if (status_reg & TWA_STATUS_COMMAND_QUEUE_FULL) {
250 if (notify_osl_of_ctlr_busy)
251 tw_osl_ctlr_busy(ctlr_handle, req->req_handle);
254 * Synchronize access between writes to command and control
255 * registers in 64-bit environments, on G66.
257 if (ctlr->state & TW_CLI_CTLR_STATE_G66_WORKAROUND_NEEDED)
258 tw_osl_get_lock(ctlr_handle, ctlr->io_lock);
260 /* Unmask command interrupt. */
261 TW_CLI_WRITE_CONTROL_REGISTER(ctlr_handle,
262 TWA_CONTROL_UNMASK_COMMAND_INTERRUPT);
264 if (ctlr->state & TW_CLI_CTLR_STATE_G66_WORKAROUND_NEEDED)
265 tw_osl_free_lock(ctlr_handle, ctlr->io_lock);
274 * Function name: tw_cl_fw_passthru
275 * Description: Interface to OS Layer for accepting firmware
277 * Input: ctlr_handle -- controller handle
278 * req_pkt -- OSL built request packet
279 * req_handle -- request handle
281 * Return value: 0 -- success
285 tw_cl_fw_passthru(struct tw_cl_ctlr_handle *ctlr_handle,
286 struct tw_cl_req_packet *req_pkt, struct tw_cl_req_handle *req_handle)
288 struct tw_cli_ctlr_context *ctlr;
289 struct tw_cli_req_context *req;
290 union tw_cl_command_7k *cmd_7k;
291 struct tw_cl_command_9k *cmd_9k;
292 struct tw_cl_passthru_req_packet *pt_req;
295 TW_VOID *sgl = TW_CL_NULL;
298 tw_cli_dbg_printf(5, ctlr_handle, tw_osl_cur_func(), "entered");
300 ctlr = (struct tw_cli_ctlr_context *)(ctlr_handle->cl_ctlr_ctxt);
302 if (ctlr->state & TW_CLI_CTLR_STATE_RESET_IN_PROGRESS) {
303 tw_cli_dbg_printf(2, ctlr_handle, tw_osl_cur_func(),
304 "Passthru request during reset: returning busy. "
307 tw_osl_ctlr_busy(ctlr_handle, req_handle);
308 return(TW_OSL_EBUSY);
311 if ((req = tw_cli_get_request(ctlr
313 tw_cli_dbg_printf(2, ctlr_handle, tw_osl_cur_func(),
314 "Out of request context packets: returning busy");
315 tw_osl_ctlr_busy(ctlr_handle, req_handle);
316 return(TW_OSL_EBUSY);
319 req_handle->cl_req_ctxt = req;
320 req->req_handle = req_handle;
321 req->orig_req = req_pkt;
322 req->tw_cli_callback = tw_cli_complete_io;
324 req->flags |= (TW_CLI_REQ_FLAGS_EXTERNAL | TW_CLI_REQ_FLAGS_PASSTHRU);
326 pt_req = &(req_pkt->gen_req_pkt.pt_req);
328 tw_osl_memcpy(req->cmd_pkt, pt_req->cmd_pkt,
329 pt_req->cmd_pkt_length);
330 /* Build the cmd pkt. */
331 if ((opcode = GET_OPCODE(((TW_UINT8 *)
332 (pt_req->cmd_pkt))[sizeof(struct tw_cl_command_header)]))
333 == TWA_FW_CMD_EXECUTE_SCSI) {
334 TW_UINT16 lun_l4, lun_h4;
336 tw_cli_dbg_printf(5, ctlr_handle, tw_osl_cur_func(),
337 "passthru: 9k cmd pkt");
338 req->flags |= TW_CLI_REQ_FLAGS_9K;
339 cmd_9k = &(req->cmd_pkt->command.cmd_pkt_9k);
340 lun_l4 = GET_LUN_L4(cmd_9k->lun_l4__req_id);
341 lun_h4 = GET_LUN_H4(cmd_9k->lun_h4__sgl_entries);
342 cmd_9k->lun_l4__req_id = TW_CL_SWAP16(
343 BUILD_LUN_L4__REQ_ID(lun_l4, req->request_id));
344 if (pt_req->sgl_entries) {
345 cmd_9k->lun_h4__sgl_entries =
346 TW_CL_SWAP16(BUILD_LUN_H4__SGL_ENTRIES(lun_h4,
347 pt_req->sgl_entries));
348 sgl = (TW_VOID *)(cmd_9k->sg_list);
351 tw_cli_dbg_printf(5, ctlr_handle, tw_osl_cur_func(),
352 "passthru: 7k cmd pkt");
353 cmd_7k = &(req->cmd_pkt->command.cmd_pkt_7k);
354 cmd_7k->generic.request_id =
355 (TW_UINT8)(TW_CL_SWAP16(req->request_id));
357 GET_SGL_OFF(cmd_7k->generic.sgl_off__opcode))) {
358 if (ctlr->device_id == TW_CL_DEVICE_ID_9K_SA)
359 sgl = (((TW_UINT32 *)cmd_7k) + cmd_7k->generic.size);
361 sgl = (((TW_UINT32 *)cmd_7k) + sgl_offset);
362 cmd_7k->generic.size += pt_req->sgl_entries *
363 ((ctlr->flags & TW_CL_64BIT_ADDRESSES) ? 3 : 2);
368 tw_cli_fill_sg_list(ctlr, pt_req->sg_list,
369 sgl, pt_req->sgl_entries);
371 if ((error = tw_cli_submit_cmd(req))) {
372 tw_cl_create_event(ctlr_handle, TW_CL_FALSE,
373 TW_CL_MESSAGE_SOURCE_COMMON_LAYER_ERROR,
374 0x1100, 0x1, TW_CL_SEVERITY_ERROR_STRING,
375 "Failed to start passthru command",
376 "error = %d", error);
377 tw_cli_req_q_insert_tail(req, TW_CLI_FREE_Q);
385 * Function name: tw_cl_ioctl
386 * Description: Handler of CL supported ioctl cmds.
388 * Input: ctlr -- ptr to per ctlr structure
390 * buf -- ptr to buffer in kernel memory, which is
391 * a copy of the input buffer in user-space
392 * Output: buf -- ptr to buffer in kernel memory, which will
393 * need to be copied to the output buffer in
395 * Return value: 0 -- success
399 tw_cl_ioctl(struct tw_cl_ctlr_handle *ctlr_handle, TW_INT32 cmd, TW_VOID *buf)
401 struct tw_cli_ctlr_context *ctlr =
402 (struct tw_cli_ctlr_context *)(ctlr_handle->cl_ctlr_ctxt);
403 struct tw_cl_ioctl_packet *user_buf =
404 (struct tw_cl_ioctl_packet *)buf;
405 struct tw_cl_event_packet event_buf;
406 TW_INT32 event_index;
407 TW_INT32 start_index;
408 TW_INT32 error = TW_OSL_ESUCCESS;
410 tw_cli_dbg_printf(5, ctlr_handle, tw_osl_cur_func(), "entered");
412 /* Serialize access to the AEN queue and the ioctl lock. */
413 tw_osl_get_lock(ctlr_handle, ctlr->gen_lock);
416 case TW_CL_IOCTL_GET_FIRST_EVENT:
417 tw_cli_dbg_printf(3, ctlr_handle, tw_osl_cur_func(),
420 if (ctlr->aen_q_wrapped) {
421 if (ctlr->aen_q_overflow) {
423 * The aen queue has wrapped, even before some
424 * events have been retrieved. Let the caller
425 * know that he missed out on some AEN's.
427 user_buf->driver_pkt.status =
428 TW_CL_ERROR_AEN_OVERFLOW;
429 ctlr->aen_q_overflow = TW_CL_FALSE;
431 user_buf->driver_pkt.status = 0;
432 event_index = ctlr->aen_head;
434 if (ctlr->aen_head == ctlr->aen_tail) {
435 user_buf->driver_pkt.status =
436 TW_CL_ERROR_AEN_NO_EVENTS;
439 user_buf->driver_pkt.status = 0;
440 event_index = ctlr->aen_tail; /* = 0 */
442 tw_osl_memcpy(user_buf->data_buf,
443 &(ctlr->aen_queue[event_index]),
444 sizeof(struct tw_cl_event_packet));
446 ctlr->aen_queue[event_index].retrieved = TW_CL_AEN_RETRIEVED;
451 case TW_CL_IOCTL_GET_LAST_EVENT:
452 tw_cli_dbg_printf(3, ctlr_handle, tw_osl_cur_func(),
455 if (ctlr->aen_q_wrapped) {
456 if (ctlr->aen_q_overflow) {
458 * The aen queue has wrapped, even before some
459 * events have been retrieved. Let the caller
460 * know that he missed out on some AEN's.
462 user_buf->driver_pkt.status =
463 TW_CL_ERROR_AEN_OVERFLOW;
464 ctlr->aen_q_overflow = TW_CL_FALSE;
466 user_buf->driver_pkt.status = 0;
468 if (ctlr->aen_head == ctlr->aen_tail) {
469 user_buf->driver_pkt.status =
470 TW_CL_ERROR_AEN_NO_EVENTS;
473 user_buf->driver_pkt.status = 0;
475 event_index = (ctlr->aen_head - 1 + ctlr->max_aens_supported) %
476 ctlr->max_aens_supported;
478 tw_osl_memcpy(user_buf->data_buf,
479 &(ctlr->aen_queue[event_index]),
480 sizeof(struct tw_cl_event_packet));
482 ctlr->aen_queue[event_index].retrieved = TW_CL_AEN_RETRIEVED;
487 case TW_CL_IOCTL_GET_NEXT_EVENT:
488 tw_cli_dbg_printf(3, ctlr_handle, tw_osl_cur_func(),
491 user_buf->driver_pkt.status = 0;
492 if (ctlr->aen_q_wrapped) {
493 tw_cli_dbg_printf(3, ctlr_handle, tw_osl_cur_func(),
494 "Get Next Event: wrapped");
495 if (ctlr->aen_q_overflow) {
497 * The aen queue has wrapped, even before some
498 * events have been retrieved. Let the caller
499 * know that he missed out on some AEN's.
501 tw_cli_dbg_printf(2, ctlr_handle,
503 "Get Next Event: overflow");
504 user_buf->driver_pkt.status =
505 TW_CL_ERROR_AEN_OVERFLOW;
506 ctlr->aen_q_overflow = TW_CL_FALSE;
508 start_index = ctlr->aen_head;
510 if (ctlr->aen_head == ctlr->aen_tail) {
511 tw_cli_dbg_printf(3, ctlr_handle,
513 "Get Next Event: empty queue");
514 user_buf->driver_pkt.status =
515 TW_CL_ERROR_AEN_NO_EVENTS;
518 start_index = ctlr->aen_tail; /* = 0 */
520 tw_osl_memcpy(&event_buf, user_buf->data_buf,
521 sizeof(struct tw_cl_event_packet));
523 event_index = (start_index + event_buf.sequence_id -
524 ctlr->aen_queue[start_index].sequence_id + 1) %
525 ctlr->max_aens_supported;
527 tw_cli_dbg_printf(3, ctlr_handle, tw_osl_cur_func(),
528 "Get Next Event: si = %x, ei = %x, ebsi = %x, "
529 "sisi = %x, eisi = %x",
530 start_index, event_index, event_buf.sequence_id,
531 ctlr->aen_queue[start_index].sequence_id,
532 ctlr->aen_queue[event_index].sequence_id);
534 if (! (ctlr->aen_queue[event_index].sequence_id >
535 event_buf.sequence_id)) {
537 * We don't have any event matching the criterion. So,
538 * we have to report TW_CL_ERROR_NO_EVENTS. If we also
539 * encountered an overflow condition above, we cannot
540 * report both conditions during this call. We choose
541 * to report NO_EVENTS this time, and an overflow the
542 * next time we are called.
544 if (user_buf->driver_pkt.status ==
545 TW_CL_ERROR_AEN_OVERFLOW) {
547 * Make a note so we report the overflow
550 ctlr->aen_q_overflow = TW_CL_TRUE;
552 user_buf->driver_pkt.status = TW_CL_ERROR_AEN_NO_EVENTS;
555 /* Copy the event -- even if there has been an overflow. */
556 tw_osl_memcpy(user_buf->data_buf,
557 &(ctlr->aen_queue[event_index]),
558 sizeof(struct tw_cl_event_packet));
560 ctlr->aen_queue[event_index].retrieved = TW_CL_AEN_RETRIEVED;
565 case TW_CL_IOCTL_GET_PREVIOUS_EVENT:
566 tw_cli_dbg_printf(3, ctlr_handle, tw_osl_cur_func(),
567 "Get Previous Event");
569 user_buf->driver_pkt.status = 0;
570 if (ctlr->aen_q_wrapped) {
571 if (ctlr->aen_q_overflow) {
573 * The aen queue has wrapped, even before some
574 * events have been retrieved. Let the caller
575 * know that he missed out on some AEN's.
577 user_buf->driver_pkt.status =
578 TW_CL_ERROR_AEN_OVERFLOW;
579 ctlr->aen_q_overflow = TW_CL_FALSE;
581 start_index = ctlr->aen_head;
583 if (ctlr->aen_head == ctlr->aen_tail) {
584 user_buf->driver_pkt.status =
585 TW_CL_ERROR_AEN_NO_EVENTS;
588 start_index = ctlr->aen_tail; /* = 0 */
590 tw_osl_memcpy(&event_buf, user_buf->data_buf,
591 sizeof(struct tw_cl_event_packet));
593 event_index = (start_index + event_buf.sequence_id -
594 ctlr->aen_queue[start_index].sequence_id - 1) %
595 ctlr->max_aens_supported;
597 if (! (ctlr->aen_queue[event_index].sequence_id <
598 event_buf.sequence_id)) {
600 * We don't have any event matching the criterion. So,
601 * we have to report TW_CL_ERROR_NO_EVENTS. If we also
602 * encountered an overflow condition above, we cannot
603 * report both conditions during this call. We choose
604 * to report NO_EVENTS this time, and an overflow the
605 * next time we are called.
607 if (user_buf->driver_pkt.status ==
608 TW_CL_ERROR_AEN_OVERFLOW) {
610 * Make a note so we report the overflow
613 ctlr->aen_q_overflow = TW_CL_TRUE;
615 user_buf->driver_pkt.status = TW_CL_ERROR_AEN_NO_EVENTS;
618 /* Copy the event -- even if there has been an overflow. */
619 tw_osl_memcpy(user_buf->data_buf,
620 &(ctlr->aen_queue[event_index]),
621 sizeof(struct tw_cl_event_packet));
623 ctlr->aen_queue[event_index].retrieved = TW_CL_AEN_RETRIEVED;
628 case TW_CL_IOCTL_GET_LOCK:
630 struct tw_cl_lock_packet lock_pkt;
633 tw_cli_dbg_printf(3, ctlr_handle, tw_osl_cur_func(),
636 cur_time = tw_osl_get_local_time();
637 tw_osl_memcpy(&lock_pkt, user_buf->data_buf,
638 sizeof(struct tw_cl_lock_packet));
640 if ((ctlr->ioctl_lock.lock == TW_CLI_LOCK_FREE) ||
641 (lock_pkt.force_flag) ||
642 (cur_time >= ctlr->ioctl_lock.timeout)) {
643 tw_cli_dbg_printf(3, ctlr_handle, tw_osl_cur_func(),
644 "GET_LOCK: Getting lock!");
645 ctlr->ioctl_lock.lock = TW_CLI_LOCK_HELD;
646 ctlr->ioctl_lock.timeout =
647 cur_time + (lock_pkt.timeout_msec / 1000);
648 lock_pkt.time_remaining_msec = lock_pkt.timeout_msec;
649 user_buf->driver_pkt.status = 0;
651 tw_cli_dbg_printf(2, ctlr_handle, tw_osl_cur_func(),
652 "GET_LOCK: Lock already held!");
653 lock_pkt.time_remaining_msec = (TW_UINT32)(
654 (ctlr->ioctl_lock.timeout - cur_time) * 1000);
655 user_buf->driver_pkt.status =
656 TW_CL_ERROR_IOCTL_LOCK_ALREADY_HELD;
658 tw_osl_memcpy(user_buf->data_buf, &lock_pkt,
659 sizeof(struct tw_cl_lock_packet));
664 case TW_CL_IOCTL_RELEASE_LOCK:
665 tw_cli_dbg_printf(3, ctlr_handle, tw_osl_cur_func(),
666 "Release ioctl lock");
668 if (ctlr->ioctl_lock.lock == TW_CLI_LOCK_FREE) {
669 tw_cli_dbg_printf(2, ctlr_handle, tw_osl_cur_func(),
670 "twa_ioctl: RELEASE_LOCK: Lock not held!");
671 user_buf->driver_pkt.status =
672 TW_CL_ERROR_IOCTL_LOCK_NOT_HELD;
674 tw_cli_dbg_printf(3, ctlr_handle, tw_osl_cur_func(),
675 "RELEASE_LOCK: Releasing lock!");
676 ctlr->ioctl_lock.lock = TW_CLI_LOCK_FREE;
677 user_buf->driver_pkt.status = 0;
682 case TW_CL_IOCTL_GET_COMPATIBILITY_INFO:
684 struct tw_cl_compatibility_packet comp_pkt;
686 tw_cli_dbg_printf(3, ctlr_handle, tw_osl_cur_func(),
687 "Get compatibility info");
689 tw_osl_memcpy(comp_pkt.driver_version,
690 TW_OSL_DRIVER_VERSION_STRING,
691 sizeof(TW_OSL_DRIVER_VERSION_STRING));
692 comp_pkt.working_srl = ctlr->working_srl;
693 comp_pkt.working_branch = ctlr->working_branch;
694 comp_pkt.working_build = ctlr->working_build;
695 comp_pkt.driver_srl_high = TWA_CURRENT_FW_SRL;
696 comp_pkt.driver_branch_high =
697 TWA_CURRENT_FW_BRANCH(ctlr->arch_id);
698 comp_pkt.driver_build_high =
699 TWA_CURRENT_FW_BUILD(ctlr->arch_id);
700 comp_pkt.driver_srl_low = TWA_BASE_FW_SRL;
701 comp_pkt.driver_branch_low = TWA_BASE_FW_BRANCH;
702 comp_pkt.driver_build_low = TWA_BASE_FW_BUILD;
703 comp_pkt.fw_on_ctlr_srl = ctlr->fw_on_ctlr_srl;
704 comp_pkt.fw_on_ctlr_branch = ctlr->fw_on_ctlr_branch;
705 comp_pkt.fw_on_ctlr_build = ctlr->fw_on_ctlr_build;
706 user_buf->driver_pkt.status = 0;
708 /* Copy compatibility information to user space. */
709 tw_osl_memcpy(user_buf->data_buf, &comp_pkt,
710 (sizeof(struct tw_cl_compatibility_packet) <
711 user_buf->driver_pkt.buffer_length) ?
712 sizeof(struct tw_cl_compatibility_packet) :
713 user_buf->driver_pkt.buffer_length);
718 /* Unknown opcode. */
719 tw_cli_dbg_printf(3, ctlr_handle, tw_osl_cur_func(),
720 "Unknown ioctl cmd 0x%x", cmd);
721 error = TW_OSL_ENOTTY;
724 tw_osl_free_lock(ctlr_handle, ctlr->gen_lock);
731 * Function name: tw_cli_get_param
732 * Description: Get a firmware parameter.
734 * Input: ctlr -- ptr to per ctlr structure
735 * table_id -- parameter table #
736 * param_id -- index of the parameter in the table
737 * param_size -- size of the parameter in bytes
738 * callback -- ptr to function, if any, to be called
739 * back on completion; TW_CL_NULL if no callback.
740 * Output: param_data -- param value
741 * Return value: 0 -- success
745 tw_cli_get_param(struct tw_cli_ctlr_context *ctlr, TW_INT32 table_id,
746 TW_INT32 param_id, TW_VOID *param_data, TW_INT32 param_size,
747 TW_VOID (* callback)(struct tw_cli_req_context *req))
749 struct tw_cli_req_context *req;
750 union tw_cl_command_7k *cmd;
751 struct tw_cl_param_9k *param = TW_CL_NULL;
752 TW_INT32 error = TW_OSL_EBUSY;
754 tw_cli_dbg_printf(4, ctlr->ctlr_handle, tw_osl_cur_func(), "entered");
756 /* Get a request packet. */
757 if ((req = tw_cli_get_request(ctlr
761 /* Make sure this is the only CL internal request at this time. */
762 if (ctlr->state & TW_CLI_CTLR_STATE_INTERNAL_REQ_BUSY) {
763 error = TW_OSL_EBUSY;
766 ctlr->state |= TW_CLI_CTLR_STATE_INTERNAL_REQ_BUSY;
767 req->data = ctlr->internal_req_data;
768 req->data_phys = ctlr->internal_req_data_phys;
769 req->length = TW_CLI_SECTOR_SIZE;
770 req->flags |= TW_CLI_REQ_FLAGS_INTERNAL;
772 /* Initialize memory to read data into. */
773 param = (struct tw_cl_param_9k *)(req->data);
774 tw_osl_memzero(param, sizeof(struct tw_cl_param_9k) - 1 + param_size);
776 /* Build the cmd pkt. */
777 cmd = &(req->cmd_pkt->command.cmd_pkt_7k);
779 req->cmd_pkt->cmd_hdr.header_desc.size_header = 128;
781 cmd->param.sgl_off__opcode =
782 BUILD_SGL_OFF__OPCODE(2, TWA_FW_CMD_GET_PARAM);
783 cmd->param.request_id =
784 (TW_UINT8)(TW_CL_SWAP16(req->request_id));
785 cmd->param.host_id__unit = BUILD_HOST_ID__UNIT(0, 0);
786 cmd->param.param_count = TW_CL_SWAP16(1);
788 if (ctlr->flags & TW_CL_64BIT_ADDRESSES) {
789 ((struct tw_cl_sg_desc64 *)(cmd->param.sgl))[0].address =
790 TW_CL_SWAP64(req->data_phys);
791 ((struct tw_cl_sg_desc64 *)(cmd->param.sgl))[0].length =
792 TW_CL_SWAP32(req->length);
793 cmd->param.size = 2 + 3;
795 ((struct tw_cl_sg_desc32 *)(cmd->param.sgl))[0].address =
796 TW_CL_SWAP32(req->data_phys);
797 ((struct tw_cl_sg_desc32 *)(cmd->param.sgl))[0].length =
798 TW_CL_SWAP32(req->length);
799 cmd->param.size = 2 + 2;
802 /* Specify which parameter we need. */
803 param->table_id = TW_CL_SWAP16(table_id | TWA_9K_PARAM_DESCRIPTOR);
804 param->parameter_id = (TW_UINT8)(param_id);
805 param->parameter_size_bytes = TW_CL_SWAP16(param_size);
807 /* Submit the command. */
808 if (callback == TW_CL_NULL) {
809 /* There's no call back; wait till the command completes. */
810 error = tw_cli_submit_and_poll_request(req,
811 TW_CLI_REQUEST_TIMEOUT_PERIOD);
812 if (error == TW_OSL_ETIMEDOUT)
813 /* Clean-up done by tw_cli_submit_and_poll_request. */
817 if ((error = cmd->param.status)) {
818 tw_cli_create_ctlr_event(ctlr,
819 TW_CL_MESSAGE_SOURCE_CONTROLLER_ERROR,
820 &(req->cmd_pkt->cmd_hdr));
823 tw_osl_memcpy(param_data, param->data, param_size);
824 ctlr->state &= ~TW_CLI_CTLR_STATE_INTERNAL_REQ_BUSY;
825 tw_cli_req_q_insert_tail(req, TW_CLI_FREE_Q);
827 /* There's a call back. Simply submit the command. */
828 req->tw_cli_callback = callback;
829 if ((error = tw_cli_submit_cmd(req)))
835 tw_cl_create_event(ctlr->ctlr_handle, TW_CL_FALSE,
836 TW_CL_MESSAGE_SOURCE_COMMON_LAYER_ERROR,
837 0x1101, 0x1, TW_CL_SEVERITY_ERROR_STRING,
839 "error = %d", error);
841 ctlr->state &= ~TW_CLI_CTLR_STATE_INTERNAL_REQ_BUSY;
843 tw_cli_req_q_insert_tail(req, TW_CLI_FREE_Q);
850 * Function name: tw_cli_set_param
851 * Description: Set a firmware parameter.
853 * Input: ctlr -- ptr to per ctlr structure
854 * table_id -- parameter table #
855 * param_id -- index of the parameter in the table
856 * param_size -- size of the parameter in bytes
857 * callback -- ptr to function, if any, to be called
858 * back on completion; TW_CL_NULL if no callback.
860 * Return value: 0 -- success
864 tw_cli_set_param(struct tw_cli_ctlr_context *ctlr, TW_INT32 table_id,
865 TW_INT32 param_id, TW_INT32 param_size, TW_VOID *data,
866 TW_VOID (* callback)(struct tw_cli_req_context *req))
868 struct tw_cli_req_context *req;
869 union tw_cl_command_7k *cmd;
870 struct tw_cl_param_9k *param = TW_CL_NULL;
871 TW_INT32 error = TW_OSL_EBUSY;
873 tw_cli_dbg_printf(4, ctlr->ctlr_handle, tw_osl_cur_func(), "entered");
875 /* Get a request packet. */
876 if ((req = tw_cli_get_request(ctlr
880 /* Make sure this is the only CL internal request at this time. */
881 if (ctlr->state & TW_CLI_CTLR_STATE_INTERNAL_REQ_BUSY) {
882 error = TW_OSL_EBUSY;
885 ctlr->state |= TW_CLI_CTLR_STATE_INTERNAL_REQ_BUSY;
886 req->data = ctlr->internal_req_data;
887 req->data_phys = ctlr->internal_req_data_phys;
888 req->length = TW_CLI_SECTOR_SIZE;
889 req->flags |= TW_CLI_REQ_FLAGS_INTERNAL;
891 /* Initialize memory to send data using. */
892 param = (struct tw_cl_param_9k *)(req->data);
893 tw_osl_memzero(param, sizeof(struct tw_cl_param_9k) - 1 + param_size);
895 /* Build the cmd pkt. */
896 cmd = &(req->cmd_pkt->command.cmd_pkt_7k);
898 req->cmd_pkt->cmd_hdr.header_desc.size_header = 128;
900 cmd->param.sgl_off__opcode =
901 BUILD_SGL_OFF__OPCODE(2, TWA_FW_CMD_SET_PARAM);
902 cmd->param.request_id = (TW_UINT8)(TW_CL_SWAP16(req->request_id));
903 cmd->param.host_id__unit = BUILD_HOST_ID__UNIT(0, 0);
904 cmd->param.param_count = TW_CL_SWAP16(1);
906 if (ctlr->flags & TW_CL_64BIT_ADDRESSES) {
907 ((struct tw_cl_sg_desc64 *)(cmd->param.sgl))[0].address =
908 TW_CL_SWAP64(req->data_phys);
909 ((struct tw_cl_sg_desc64 *)(cmd->param.sgl))[0].length =
910 TW_CL_SWAP32(req->length);
911 cmd->param.size = 2 + 3;
913 ((struct tw_cl_sg_desc32 *)(cmd->param.sgl))[0].address =
914 TW_CL_SWAP32(req->data_phys);
915 ((struct tw_cl_sg_desc32 *)(cmd->param.sgl))[0].length =
916 TW_CL_SWAP32(req->length);
917 cmd->param.size = 2 + 2;
920 /* Specify which parameter we want to set. */
921 param->table_id = TW_CL_SWAP16(table_id | TWA_9K_PARAM_DESCRIPTOR);
922 param->parameter_id = (TW_UINT8)(param_id);
923 param->parameter_size_bytes = TW_CL_SWAP16(param_size);
924 tw_osl_memcpy(param->data, data, param_size);
926 /* Submit the command. */
927 if (callback == TW_CL_NULL) {
928 /* There's no call back; wait till the command completes. */
929 error = tw_cli_submit_and_poll_request(req,
930 TW_CLI_REQUEST_TIMEOUT_PERIOD);
931 if (error == TW_OSL_ETIMEDOUT)
932 /* Clean-up done by tw_cli_submit_and_poll_request. */
936 if ((error = cmd->param.status)) {
937 tw_cli_create_ctlr_event(ctlr,
938 TW_CL_MESSAGE_SOURCE_CONTROLLER_ERROR,
939 &(req->cmd_pkt->cmd_hdr));
942 ctlr->state &= ~TW_CLI_CTLR_STATE_INTERNAL_REQ_BUSY;
943 tw_cli_req_q_insert_tail(req, TW_CLI_FREE_Q);
945 /* There's a call back. Simply submit the command. */
946 req->tw_cli_callback = callback;
947 if ((error = tw_cli_submit_cmd(req)))
953 tw_cl_create_event(ctlr->ctlr_handle, TW_CL_FALSE,
954 TW_CL_MESSAGE_SOURCE_COMMON_LAYER_ERROR,
955 0x1102, 0x1, TW_CL_SEVERITY_ERROR_STRING,
957 "error = %d", error);
959 ctlr->state &= ~TW_CLI_CTLR_STATE_INTERNAL_REQ_BUSY;
961 tw_cli_req_q_insert_tail(req, TW_CLI_FREE_Q);
968 * Function name: tw_cli_submit_and_poll_request
969 * Description: Sends down a firmware cmd, and waits for the completion
972 * Input: req -- ptr to request pkt
973 * timeout -- max # of seconds to wait before giving up
975 * Return value: 0 -- success
979 tw_cli_submit_and_poll_request(struct tw_cli_req_context *req,
982 struct tw_cli_ctlr_context *ctlr = req->ctlr;
986 tw_cli_dbg_printf(4, ctlr->ctlr_handle, tw_osl_cur_func(), "entered");
989 * If the cmd queue is full, tw_cli_submit_cmd will queue this
990 * request in the pending queue, since this is an internal request.
992 if ((error = tw_cli_submit_cmd(req))) {
993 tw_cl_create_event(ctlr->ctlr_handle, TW_CL_FALSE,
994 TW_CL_MESSAGE_SOURCE_COMMON_LAYER_ERROR,
995 0x1103, 0x1, TW_CL_SEVERITY_ERROR_STRING,
996 "Failed to start internal request",
997 "error = %d", error);
1002 * Poll for the response until the command gets completed, or there's
1005 end_time = tw_osl_get_local_time() + timeout;
1007 if ((error = req->error_code))
1009 * This will take care of completion due to a reset,
1010 * or a failure in tw_cli_submit_pending_queue.
1011 * The caller should do the clean-up.
1015 /* See if the command completed. */
1016 tw_cli_process_resp_intr(ctlr);
1018 if ((req->state != TW_CLI_REQ_STATE_BUSY) &&
1019 (req->state != TW_CLI_REQ_STATE_PENDING))
1020 return(req->state != TW_CLI_REQ_STATE_COMPLETE);
1021 } while (tw_osl_get_local_time() <= end_time);
1024 tw_cl_create_event(ctlr->ctlr_handle, TW_CL_FALSE,
1025 TW_CL_MESSAGE_SOURCE_COMMON_LAYER_ERROR,
1026 0x1104, 0x1, TW_CL_SEVERITY_ERROR_STRING,
1027 "Internal request timed out",
1028 "request = %p", req);
1031 * We will reset the controller only if the request has already been
1032 * submitted, so as to not lose the request packet. If a busy request
1033 * timed out, the reset will take care of freeing resources. If a
1034 * pending request timed out, we will free resources for that request,
1035 * right here, thereby avoiding a reset. So, the caller is expected
1036 * to NOT cleanup when TW_OSL_ETIMEDOUT is returned.
1040 * We have to make sure that this timed out request, if it were in the
1041 * pending queue, doesn't get submitted while we are here, from
1042 * tw_cli_submit_pending_queue. There could be a race in that case.
1045 if (req->state != TW_CLI_REQ_STATE_PENDING)
1046 tw_cl_reset_ctlr(ctlr->ctlr_handle);
1048 tw_cli_dbg_printf(3, ctlr->ctlr_handle, tw_osl_cur_func(),
1049 "Removing request from pending queue");
1051 * Request was never submitted. Clean up. Note that we did
1052 * not do a reset. So, we have to remove the request ourselves
1053 * from the pending queue (as against tw_cli_drain_pendinq_queue
1054 * taking care of it).
1056 tw_cli_req_q_remove_item(req, TW_CLI_PENDING_Q);
1058 ctlr->state &= ~TW_CLI_CTLR_STATE_INTERNAL_REQ_BUSY;
1059 tw_cli_req_q_insert_tail(req, TW_CLI_FREE_Q);
1062 return(TW_OSL_ETIMEDOUT);
1068 * Function name: tw_cl_reset_ctlr
1069 * Description: Soft resets and then initializes the controller;
1070 * drains any incomplete requests.
1072 * Input: ctlr -- ptr to per ctlr structure
1074 * Return value: 0 -- success
1075 * non-zero-- failure
1078 tw_cl_reset_ctlr(struct tw_cl_ctlr_handle *ctlr_handle)
1080 struct tw_cli_ctlr_context *ctlr =
1081 (struct tw_cli_ctlr_context *)(ctlr_handle->cl_ctlr_ctxt);
1082 TW_INT32 reset_attempt = 1;
1085 tw_cli_dbg_printf(2, ctlr_handle, tw_osl_cur_func(), "entered");
1087 ctlr->state |= TW_CLI_CTLR_STATE_RESET_IN_PROGRESS;
1090 * Error back all requests in the complete, busy, and pending queues.
1091 * If any request is already on its way to getting submitted, it's in
1092 * none of these queues and so, will not be completed. That request
1093 * will continue its course and get submitted to the controller after
1094 * the reset is done (and io_lock is released).
1096 tw_cli_dbg_printf(2, ctlr_handle, tw_osl_cur_func(),
1097 "Draining all queues following reset");
1098 tw_cli_drain_complete_queue(ctlr);
1099 tw_cli_drain_busy_queue(ctlr);
1100 tw_cli_drain_pending_queue(ctlr);
1102 tw_cli_disable_interrupts(ctlr);
1104 /* Soft reset the controller. */
1106 if ((error = tw_cli_soft_reset(ctlr))) {
1107 tw_cl_create_event(ctlr_handle, TW_CL_TRUE,
1108 TW_CL_MESSAGE_SOURCE_COMMON_LAYER_EVENT,
1109 0x1105, 0x1, TW_CL_SEVERITY_ERROR_STRING,
1110 "Controller reset failed",
1111 "error = %d; attempt %d", error, reset_attempt++);
1112 if (reset_attempt <= TW_CLI_MAX_RESET_ATTEMPTS)
1118 /* Re-establish logical connection with the controller. */
1119 if ((error = tw_cli_init_connection(ctlr,
1120 (TW_UINT16)(ctlr->max_simult_reqs),
1121 0, 0, 0, 0, 0, TW_CL_NULL, TW_CL_NULL, TW_CL_NULL,
1122 TW_CL_NULL, TW_CL_NULL))) {
1123 tw_cl_create_event(ctlr_handle, TW_CL_TRUE,
1124 TW_CL_MESSAGE_SOURCE_COMMON_LAYER_EVENT,
1125 0x1106, 0x1, TW_CL_SEVERITY_ERROR_STRING,
1126 "Can't initialize connection after reset",
1127 "error = %d", error);
1131 tw_cl_create_event(ctlr_handle, TW_CL_TRUE,
1132 TW_CL_MESSAGE_SOURCE_COMMON_LAYER_EVENT,
1133 0x1107, 0x3, TW_CL_SEVERITY_INFO_STRING,
1134 "Controller reset done!",
1138 ctlr->state &= ~TW_CLI_CTLR_STATE_RESET_IN_PROGRESS;
1140 * Enable interrupts, and also clear attention and response interrupts.
1142 tw_cli_enable_interrupts(ctlr);
1144 /* Request for a bus re-scan. */
1146 tw_osl_scan_bus(ctlr_handle);
1153 * Function name: tw_cli_soft_reset
1154 * Description: Does the actual soft reset.
1156 * Input: ctlr -- ptr to per ctlr structure
1158 * Return value: 0 -- success
1159 * non-zero-- failure
1162 tw_cli_soft_reset(struct tw_cli_ctlr_context *ctlr)
1164 struct tw_cl_ctlr_handle *ctlr_handle = ctlr->ctlr_handle;
1165 TW_UINT32 status_reg;
1168 tw_cli_dbg_printf(1, ctlr_handle, tw_osl_cur_func(), "entered");
1170 tw_cl_create_event(ctlr_handle, TW_CL_TRUE,
1171 TW_CL_MESSAGE_SOURCE_COMMON_LAYER_EVENT,
1172 0x1108, 0x3, TW_CL_SEVERITY_INFO_STRING,
1173 "Resetting controller...",
1176 /* Don't let any new commands get submitted to the controller. */
1177 tw_osl_get_lock(ctlr_handle, ctlr->io_lock);
1179 TW_CLI_SOFT_RESET(ctlr_handle);
1181 if ((ctlr->device_id == TW_CL_DEVICE_ID_9K_X) ||
1182 (ctlr->device_id == TW_CL_DEVICE_ID_9K_E) ||
1183 (ctlr->device_id == TW_CL_DEVICE_ID_9K_SA)) {
1185 * There's a hardware bug in the G133 ASIC, which can lead to
1186 * PCI parity errors and hangs, if the host accesses any
1187 * registers when the firmware is resetting the hardware, as
1188 * part of a hard/soft reset. The window of time when the
1189 * problem can occur is about 10 ms. Here, we will handshake
1190 * with the firmware to find out when the firmware is pulling
1191 * down the hardware reset pin, and wait for about 500 ms to
1192 * make sure we don't access any hardware registers (for
1193 * polling) during that window.
1195 ctlr->state |= TW_CLI_CTLR_STATE_RESET_PHASE1_IN_PROGRESS;
1196 while (tw_cli_find_response(ctlr,
1197 TWA_RESET_PHASE1_NOTIFICATION_RESPONSE) != TW_OSL_ESUCCESS)
1199 tw_osl_delay(TWA_RESET_PHASE1_WAIT_TIME_MS * 1000);
1200 ctlr->state &= ~TW_CLI_CTLR_STATE_RESET_PHASE1_IN_PROGRESS;
1203 if ((error = tw_cli_poll_status(ctlr,
1204 TWA_STATUS_MICROCONTROLLER_READY |
1205 TWA_STATUS_ATTENTION_INTERRUPT,
1206 TW_CLI_RESET_TIMEOUT_PERIOD))) {
1207 tw_cl_create_event(ctlr_handle, TW_CL_TRUE,
1208 TW_CL_MESSAGE_SOURCE_COMMON_LAYER_EVENT,
1209 0x1109, 0x1, TW_CL_SEVERITY_ERROR_STRING,
1210 "Micro-ctlr not ready/No attn intr after reset",
1211 "error = %d", error);
1212 tw_osl_free_lock(ctlr_handle, ctlr->io_lock);
1216 TW_CLI_WRITE_CONTROL_REGISTER(ctlr_handle,
1217 TWA_CONTROL_CLEAR_ATTENTION_INTERRUPT);
1219 if ((error = tw_cli_drain_response_queue(ctlr))) {
1220 tw_cl_create_event(ctlr_handle, TW_CL_FALSE,
1221 TW_CL_MESSAGE_SOURCE_COMMON_LAYER_ERROR,
1222 0x110A, 0x1, TW_CL_SEVERITY_ERROR_STRING,
1223 "Can't drain response queue after reset",
1224 "error = %d", error);
1225 tw_osl_free_lock(ctlr_handle, ctlr->io_lock);
1229 tw_osl_free_lock(ctlr_handle, ctlr->io_lock);
1231 if ((error = tw_cli_drain_aen_queue(ctlr))) {
1232 tw_cl_create_event(ctlr_handle, TW_CL_FALSE,
1233 TW_CL_MESSAGE_SOURCE_COMMON_LAYER_ERROR,
1234 0x110B, 0x1, TW_CL_SEVERITY_ERROR_STRING,
1235 "Can't drain AEN queue after reset",
1236 "error = %d", error);
1240 if ((error = tw_cli_find_aen(ctlr, TWA_AEN_SOFT_RESET))) {
1241 tw_cl_create_event(ctlr_handle, TW_CL_TRUE,
1242 TW_CL_MESSAGE_SOURCE_COMMON_LAYER_EVENT,
1243 0x110C, 0x1, TW_CL_SEVERITY_ERROR_STRING,
1244 "Reset not reported by controller",
1245 "error = %d", error);
1249 status_reg = TW_CLI_READ_STATUS_REGISTER(ctlr_handle);
1251 if ((error = TW_CLI_STATUS_ERRORS(status_reg)) ||
1252 (error = tw_cli_check_ctlr_state(ctlr, status_reg))) {
1253 tw_cl_create_event(ctlr_handle, TW_CL_TRUE,
1254 TW_CL_MESSAGE_SOURCE_COMMON_LAYER_EVENT,
1255 0x110D, 0x1, TW_CL_SEVERITY_ERROR_STRING,
1256 "Controller errors detected after reset",
1257 "error = %d", error);
1261 return(TW_OSL_ESUCCESS);
1267 * Function name: tw_cli_send_scsi_cmd
1268 * Description: Sends down a scsi cmd to fw.
1270 * Input: req -- ptr to request pkt
1271 * cmd -- opcode of scsi cmd to send
1273 * Return value: 0 -- success
1274 * non-zero-- failure
1277 tw_cli_send_scsi_cmd(struct tw_cli_req_context *req, TW_INT32 cmd)
1279 struct tw_cl_command_packet *cmdpkt;
1280 struct tw_cl_command_9k *cmd9k;
1281 struct tw_cli_ctlr_context *ctlr;
1285 tw_cli_dbg_printf(4, ctlr->ctlr_handle, tw_osl_cur_func(), "entered");
1287 /* Make sure this is the only CL internal request at this time. */
1288 if (ctlr->state & TW_CLI_CTLR_STATE_INTERNAL_REQ_BUSY)
1289 return(TW_OSL_EBUSY);
1290 ctlr->state |= TW_CLI_CTLR_STATE_INTERNAL_REQ_BUSY;
1291 req->data = ctlr->internal_req_data;
1292 req->data_phys = ctlr->internal_req_data_phys;
1293 tw_osl_memzero(req->data, TW_CLI_SECTOR_SIZE);
1294 req->length = TW_CLI_SECTOR_SIZE;
1296 /* Build the cmd pkt. */
1297 cmdpkt = req->cmd_pkt;
1299 cmdpkt->cmd_hdr.header_desc.size_header = 128;
1301 cmd9k = &(cmdpkt->command.cmd_pkt_9k);
1303 cmd9k->res__opcode =
1304 BUILD_RES__OPCODE(0, TWA_FW_CMD_EXECUTE_SCSI);
1306 cmd9k->lun_l4__req_id = TW_CL_SWAP16(req->request_id);
1308 cmd9k->sgl_offset = 16; /* offset from end of hdr = max cdb len */
1309 cmd9k->lun_h4__sgl_entries = TW_CL_SWAP16(1);
1311 if (req->ctlr->flags & TW_CL_64BIT_ADDRESSES) {
1312 ((struct tw_cl_sg_desc64 *)(cmd9k->sg_list))[0].address =
1313 TW_CL_SWAP64(req->data_phys);
1314 ((struct tw_cl_sg_desc64 *)(cmd9k->sg_list))[0].length =
1315 TW_CL_SWAP32(req->length);
1317 ((struct tw_cl_sg_desc32 *)(cmd9k->sg_list))[0].address =
1318 TW_CL_SWAP32(req->data_phys);
1319 ((struct tw_cl_sg_desc32 *)(cmd9k->sg_list))[0].length =
1320 TW_CL_SWAP32(req->length);
1323 cmd9k->cdb[0] = (TW_UINT8)cmd;
1324 cmd9k->cdb[4] = 128;
1326 if ((error = tw_cli_submit_cmd(req)))
1327 if (error != TW_OSL_EBUSY) {
1328 tw_cli_dbg_printf(1, ctlr->ctlr_handle,
1330 "Failed to start SCSI command",
1331 "request = %p, error = %d", req, error);
1334 return(TW_OSL_ESUCCESS);
1340 * Function name: tw_cli_get_aen
1341 * Description: Sends down a Request Sense cmd to fw to fetch an AEN.
1343 * Input: ctlr -- ptr to per ctlr structure
1345 * Return value: 0 -- success
1346 * non-zero-- failure
1349 tw_cli_get_aen(struct tw_cli_ctlr_context *ctlr)
1351 struct tw_cli_req_context *req;
1354 tw_cli_dbg_printf(4, ctlr->ctlr_handle, tw_osl_cur_func(), "entered");
1356 if ((req = tw_cli_get_request(ctlr
1358 return(TW_OSL_EBUSY);
1360 req->flags |= TW_CLI_REQ_FLAGS_INTERNAL;
1361 req->flags |= TW_CLI_REQ_FLAGS_9K;
1362 req->tw_cli_callback = tw_cli_aen_callback;
1363 if ((error = tw_cli_send_scsi_cmd(req, 0x03 /* REQUEST_SENSE */))) {
1364 tw_cli_dbg_printf(1, ctlr->ctlr_handle, tw_osl_cur_func(),
1365 "Could not send SCSI command",
1366 "request = %p, error = %d", req, error);
1368 ctlr->state &= ~TW_CLI_CTLR_STATE_INTERNAL_REQ_BUSY;
1369 tw_cli_req_q_insert_tail(req, TW_CLI_FREE_Q);
1377 * Function name: tw_cli_fill_sg_list
1378 * Description: Fills in the scatter/gather list.
1380 * Input: ctlr -- ptr to per ctlr structure
1381 * sgl_src -- ptr to fill the sg list from
1382 * sgl_dest-- ptr to sg list
1383 * nsegments--# of segments
1385 * Return value: None
1388 tw_cli_fill_sg_list(struct tw_cli_ctlr_context *ctlr, TW_VOID *sgl_src,
1389 TW_VOID *sgl_dest, TW_INT32 num_sgl_entries)
1393 tw_cli_dbg_printf(10, ctlr->ctlr_handle, tw_osl_cur_func(), "entered");
1395 if (ctlr->flags & TW_CL_64BIT_ADDRESSES) {
1396 struct tw_cl_sg_desc64 *sgl_s =
1397 (struct tw_cl_sg_desc64 *)sgl_src;
1398 struct tw_cl_sg_desc64 *sgl_d =
1399 (struct tw_cl_sg_desc64 *)sgl_dest;
1401 tw_cli_dbg_printf(10, ctlr->ctlr_handle, tw_osl_cur_func(),
1402 "64 bit addresses");
1403 for (i = 0; i < num_sgl_entries; i++) {
1404 sgl_d[i].address = TW_CL_SWAP64(sgl_s->address);
1405 sgl_d[i].length = TW_CL_SWAP32(sgl_s->length);
1407 if (ctlr->flags & TW_CL_64BIT_SG_LENGTH)
1408 sgl_s = (struct tw_cl_sg_desc64 *)
1409 (((TW_INT8 *)(sgl_s)) + 4);
1412 struct tw_cl_sg_desc32 *sgl_s =
1413 (struct tw_cl_sg_desc32 *)sgl_src;
1414 struct tw_cl_sg_desc32 *sgl_d =
1415 (struct tw_cl_sg_desc32 *)sgl_dest;
1417 tw_cli_dbg_printf(10, ctlr->ctlr_handle, tw_osl_cur_func(),
1418 "32 bit addresses");
1419 for (i = 0; i < num_sgl_entries; i++) {
1420 sgl_d[i].address = TW_CL_SWAP32(sgl_s[i].address);
1421 sgl_d[i].length = TW_CL_SWAP32(sgl_s[i].length);