Ron Mercer | c4e84bd | 2008-09-18 11:56:28 -0400 | [diff] [blame] | 1 | #include "qlge.h" |
| 2 | |
Ron Mercer | ca0413b | 2009-03-02 08:07:30 +0000 | [diff] [blame^] | 3 | static void ql_display_mb_sts(struct ql_adapter *qdev, |
| 4 | struct mbox_params *mbcp) |
| 5 | { |
| 6 | int i; |
| 7 | static char *err_sts[] = { |
| 8 | "Command Complete", |
| 9 | "Command Not Supported", |
| 10 | "Host Interface Error", |
| 11 | "Checksum Error", |
| 12 | "Unused Completion Status", |
| 13 | "Test Failed", |
| 14 | "Command Parameter Error"}; |
| 15 | |
| 16 | QPRINTK(qdev, DRV, DEBUG, "%s.\n", |
| 17 | err_sts[mbcp->mbox_out[0] & 0x0000000f]); |
| 18 | for (i = 0; i < mbcp->out_count; i++) |
| 19 | QPRINTK(qdev, DRV, DEBUG, "mbox_out[%d] = 0x%.08x.\n", |
| 20 | i, mbcp->mbox_out[i]); |
| 21 | } |
| 22 | |
Ron Mercer | a2e809b | 2009-02-26 10:08:33 +0000 | [diff] [blame] | 23 | int ql_read_mpi_reg(struct ql_adapter *qdev, u32 reg, u32 *data) |
Ron Mercer | c4e84bd | 2008-09-18 11:56:28 -0400 | [diff] [blame] | 24 | { |
| 25 | int status; |
| 26 | /* wait for reg to come ready */ |
| 27 | status = ql_wait_reg_rdy(qdev, PROC_ADDR, PROC_ADDR_RDY, PROC_ADDR_ERR); |
| 28 | if (status) |
| 29 | goto exit; |
| 30 | /* set up for reg read */ |
| 31 | ql_write32(qdev, PROC_ADDR, reg | PROC_ADDR_R); |
| 32 | /* wait for reg to come ready */ |
| 33 | status = ql_wait_reg_rdy(qdev, PROC_ADDR, PROC_ADDR_RDY, PROC_ADDR_ERR); |
| 34 | if (status) |
| 35 | goto exit; |
| 36 | /* get the data */ |
| 37 | *data = ql_read32(qdev, PROC_DATA); |
| 38 | exit: |
| 39 | return status; |
| 40 | } |
| 41 | |
Ron Mercer | a2e809b | 2009-02-26 10:08:33 +0000 | [diff] [blame] | 42 | int ql_write_mpi_reg(struct ql_adapter *qdev, u32 reg, u32 data) |
| 43 | { |
| 44 | int status = 0; |
| 45 | /* wait for reg to come ready */ |
| 46 | status = ql_wait_reg_rdy(qdev, PROC_ADDR, PROC_ADDR_RDY, PROC_ADDR_ERR); |
| 47 | if (status) |
| 48 | goto exit; |
| 49 | /* write the data to the data reg */ |
| 50 | ql_write32(qdev, PROC_DATA, data); |
| 51 | /* trigger the write */ |
| 52 | ql_write32(qdev, PROC_ADDR, reg); |
| 53 | /* wait for reg to come ready */ |
| 54 | status = ql_wait_reg_rdy(qdev, PROC_ADDR, PROC_ADDR_RDY, PROC_ADDR_ERR); |
| 55 | if (status) |
| 56 | goto exit; |
| 57 | exit: |
| 58 | return status; |
| 59 | } |
| 60 | |
| 61 | int ql_soft_reset_mpi_risc(struct ql_adapter *qdev) |
| 62 | { |
| 63 | int status; |
| 64 | status = ql_write_mpi_reg(qdev, 0x00001010, 1); |
| 65 | return status; |
| 66 | } |
| 67 | |
Hannes Eder | 2f22d22 | 2008-12-26 00:04:53 -0800 | [diff] [blame] | 68 | static int ql_get_mb_sts(struct ql_adapter *qdev, struct mbox_params *mbcp) |
Ron Mercer | c4e84bd | 2008-09-18 11:56:28 -0400 | [diff] [blame] | 69 | { |
| 70 | int i, status; |
| 71 | |
| 72 | status = ql_sem_spinlock(qdev, SEM_PROC_REG_MASK); |
| 73 | if (status) |
| 74 | return -EBUSY; |
| 75 | for (i = 0; i < mbcp->out_count; i++) { |
| 76 | status = |
Ron Mercer | a2e809b | 2009-02-26 10:08:33 +0000 | [diff] [blame] | 77 | ql_read_mpi_reg(qdev, qdev->mailbox_out + i, |
Ron Mercer | c4e84bd | 2008-09-18 11:56:28 -0400 | [diff] [blame] | 78 | &mbcp->mbox_out[i]); |
| 79 | if (status) { |
| 80 | QPRINTK(qdev, DRV, ERR, "Failed mailbox read.\n"); |
| 81 | break; |
| 82 | } |
| 83 | } |
| 84 | ql_sem_unlock(qdev, SEM_PROC_REG_MASK); /* does flush too */ |
| 85 | return status; |
| 86 | } |
| 87 | |
Ron Mercer | ca0413b | 2009-03-02 08:07:30 +0000 | [diff] [blame^] | 88 | /* Wait for a single mailbox command to complete. |
| 89 | * Returns zero on success. |
| 90 | */ |
| 91 | static int ql_wait_mbx_cmd_cmplt(struct ql_adapter *qdev) |
| 92 | { |
| 93 | int count = 50; /* TODO: arbitrary for now. */ |
| 94 | u32 value; |
| 95 | |
| 96 | do { |
| 97 | value = ql_read32(qdev, STS); |
| 98 | if (value & STS_PI) |
| 99 | return 0; |
| 100 | udelay(UDELAY_DELAY); /* 10us */ |
| 101 | } while (--count); |
| 102 | return -ETIMEDOUT; |
| 103 | } |
| 104 | |
| 105 | /* Execute a single mailbox command. |
| 106 | * Caller must hold PROC_ADDR semaphore. |
| 107 | */ |
| 108 | static int ql_exec_mb_cmd(struct ql_adapter *qdev, struct mbox_params *mbcp) |
| 109 | { |
| 110 | int i, status; |
| 111 | |
| 112 | /* |
| 113 | * Make sure there's nothing pending. |
| 114 | * This shouldn't happen. |
| 115 | */ |
| 116 | if (ql_read32(qdev, CSR) & CSR_HRI) |
| 117 | return -EIO; |
| 118 | |
| 119 | status = ql_sem_spinlock(qdev, SEM_PROC_REG_MASK); |
| 120 | if (status) |
| 121 | return status; |
| 122 | |
| 123 | /* |
| 124 | * Fill the outbound mailboxes. |
| 125 | */ |
| 126 | for (i = 0; i < mbcp->in_count; i++) { |
| 127 | status = ql_write_mpi_reg(qdev, qdev->mailbox_in + i, |
| 128 | mbcp->mbox_in[i]); |
| 129 | if (status) |
| 130 | goto end; |
| 131 | } |
| 132 | /* |
| 133 | * Wake up the MPI firmware. |
| 134 | */ |
| 135 | ql_write32(qdev, CSR, CSR_CMD_SET_H2R_INT); |
| 136 | end: |
| 137 | ql_sem_unlock(qdev, SEM_PROC_REG_MASK); |
| 138 | return status; |
| 139 | } |
| 140 | |
Ron Mercer | c4e84bd | 2008-09-18 11:56:28 -0400 | [diff] [blame] | 141 | static void ql_link_up(struct ql_adapter *qdev, struct mbox_params *mbcp) |
| 142 | { |
| 143 | mbcp->out_count = 2; |
| 144 | |
| 145 | if (ql_get_mb_sts(qdev, mbcp)) |
| 146 | goto exit; |
| 147 | |
| 148 | qdev->link_status = mbcp->mbox_out[1]; |
| 149 | QPRINTK(qdev, DRV, ERR, "Link Up.\n"); |
| 150 | QPRINTK(qdev, DRV, INFO, "Link Status = 0x%.08x.\n", mbcp->mbox_out[1]); |
| 151 | if (!netif_carrier_ok(qdev->ndev)) { |
| 152 | QPRINTK(qdev, LINK, INFO, "Link is Up.\n"); |
| 153 | netif_carrier_on(qdev->ndev); |
| 154 | netif_wake_queue(qdev->ndev); |
| 155 | } |
| 156 | exit: |
| 157 | /* Clear the MPI firmware status. */ |
| 158 | ql_write32(qdev, CSR, CSR_CMD_CLR_R2PCI_INT); |
| 159 | } |
| 160 | |
| 161 | static void ql_link_down(struct ql_adapter *qdev, struct mbox_params *mbcp) |
| 162 | { |
| 163 | mbcp->out_count = 3; |
| 164 | |
| 165 | if (ql_get_mb_sts(qdev, mbcp)) { |
| 166 | QPRINTK(qdev, DRV, ERR, "Firmware did not initialize!\n"); |
| 167 | goto exit; |
| 168 | } |
| 169 | |
| 170 | if (netif_carrier_ok(qdev->ndev)) { |
| 171 | QPRINTK(qdev, LINK, INFO, "Link is Down.\n"); |
| 172 | netif_carrier_off(qdev->ndev); |
| 173 | netif_stop_queue(qdev->ndev); |
| 174 | } |
| 175 | QPRINTK(qdev, DRV, ERR, "Link Down.\n"); |
| 176 | QPRINTK(qdev, DRV, ERR, "Link Status = 0x%.08x.\n", mbcp->mbox_out[1]); |
| 177 | exit: |
| 178 | /* Clear the MPI firmware status. */ |
| 179 | ql_write32(qdev, CSR, CSR_CMD_CLR_R2PCI_INT); |
| 180 | } |
| 181 | |
| 182 | static void ql_init_fw_done(struct ql_adapter *qdev, struct mbox_params *mbcp) |
| 183 | { |
| 184 | mbcp->out_count = 2; |
| 185 | |
| 186 | if (ql_get_mb_sts(qdev, mbcp)) { |
| 187 | QPRINTK(qdev, DRV, ERR, "Firmware did not initialize!\n"); |
| 188 | goto exit; |
| 189 | } |
| 190 | QPRINTK(qdev, DRV, ERR, "Firmware initialized!\n"); |
| 191 | QPRINTK(qdev, DRV, ERR, "Firmware status = 0x%.08x.\n", |
| 192 | mbcp->mbox_out[0]); |
| 193 | QPRINTK(qdev, DRV, ERR, "Firmware Revision = 0x%.08x.\n", |
| 194 | mbcp->mbox_out[1]); |
| 195 | exit: |
| 196 | /* Clear the MPI firmware status. */ |
| 197 | ql_write32(qdev, CSR, CSR_CMD_CLR_R2PCI_INT); |
| 198 | } |
| 199 | |
Ron Mercer | 125844e | 2009-02-26 10:08:34 +0000 | [diff] [blame] | 200 | /* Process an async event and clear it unless it's an |
| 201 | * error condition. |
| 202 | * This can get called iteratively from the mpi_work thread |
| 203 | * when events arrive via an interrupt. |
| 204 | * It also gets called when a mailbox command is polling for |
| 205 | * it's completion. */ |
| 206 | static int ql_mpi_handler(struct ql_adapter *qdev, struct mbox_params *mbcp) |
| 207 | { |
| 208 | int status; |
Ron Mercer | ca0413b | 2009-03-02 08:07:30 +0000 | [diff] [blame^] | 209 | int orig_count = mbcp->out_count; |
Ron Mercer | 125844e | 2009-02-26 10:08:34 +0000 | [diff] [blame] | 210 | |
| 211 | /* Just get mailbox zero for now. */ |
| 212 | mbcp->out_count = 1; |
| 213 | status = ql_get_mb_sts(qdev, mbcp); |
| 214 | if (status) { |
| 215 | QPRINTK(qdev, DRV, ERR, |
| 216 | "Could not read MPI, resetting ASIC!\n"); |
| 217 | ql_queue_asic_error(qdev); |
| 218 | goto end; |
| 219 | } |
| 220 | |
| 221 | switch (mbcp->mbox_out[0]) { |
| 222 | |
Ron Mercer | ca0413b | 2009-03-02 08:07:30 +0000 | [diff] [blame^] | 223 | /* This case is only active when we arrive here |
| 224 | * as a result of issuing a mailbox command to |
| 225 | * the firmware. |
| 226 | */ |
| 227 | case MB_CMD_STS_INTRMDT: |
| 228 | case MB_CMD_STS_GOOD: |
| 229 | case MB_CMD_STS_INVLD_CMD: |
| 230 | case MB_CMD_STS_XFC_ERR: |
| 231 | case MB_CMD_STS_CSUM_ERR: |
| 232 | case MB_CMD_STS_ERR: |
| 233 | case MB_CMD_STS_PARAM_ERR: |
| 234 | /* We can only get mailbox status if we're polling from an |
| 235 | * unfinished command. Get the rest of the status data and |
| 236 | * return back to the caller. |
| 237 | * We only end up here when we're polling for a mailbox |
| 238 | * command completion. |
| 239 | */ |
| 240 | mbcp->out_count = orig_count; |
| 241 | status = ql_get_mb_sts(qdev, mbcp); |
| 242 | return status; |
| 243 | |
Ron Mercer | 125844e | 2009-02-26 10:08:34 +0000 | [diff] [blame] | 244 | case AEN_LINK_UP: |
| 245 | ql_link_up(qdev, mbcp); |
| 246 | break; |
| 247 | |
| 248 | case AEN_LINK_DOWN: |
| 249 | ql_link_down(qdev, mbcp); |
| 250 | break; |
| 251 | |
| 252 | case AEN_FW_INIT_DONE: |
| 253 | ql_init_fw_done(qdev, mbcp); |
| 254 | break; |
| 255 | |
Ron Mercer | 125844e | 2009-02-26 10:08:34 +0000 | [diff] [blame] | 256 | case AEN_FW_INIT_FAIL: |
| 257 | case AEN_SYS_ERR: |
Ron Mercer | 125844e | 2009-02-26 10:08:34 +0000 | [diff] [blame] | 258 | ql_queue_fw_error(qdev); |
| 259 | break; |
| 260 | |
| 261 | default: |
| 262 | QPRINTK(qdev, DRV, ERR, |
| 263 | "Unsupported AE %.08x.\n", mbcp->mbox_out[0]); |
| 264 | /* Clear the MPI firmware status. */ |
| 265 | } |
| 266 | end: |
| 267 | ql_write32(qdev, CSR, CSR_CMD_CLR_R2PCI_INT); |
| 268 | return status; |
| 269 | } |
| 270 | |
Ron Mercer | ca0413b | 2009-03-02 08:07:30 +0000 | [diff] [blame^] | 271 | /* Execute a single mailbox command. |
| 272 | * mbcp is a pointer to an array of u32. Each |
| 273 | * element in the array contains the value for it's |
| 274 | * respective mailbox register. |
| 275 | */ |
| 276 | static int ql_mailbox_command(struct ql_adapter *qdev, struct mbox_params *mbcp) |
| 277 | { |
| 278 | int status, count; |
| 279 | |
| 280 | mutex_lock(&qdev->mpi_mutex); |
| 281 | |
| 282 | /* Begin polled mode for MPI */ |
| 283 | ql_write32(qdev, INTR_MASK, (INTR_MASK_PI << 16)); |
| 284 | |
| 285 | /* Load the mailbox registers and wake up MPI RISC. */ |
| 286 | status = ql_exec_mb_cmd(qdev, mbcp); |
| 287 | if (status) |
| 288 | goto end; |
| 289 | |
| 290 | |
| 291 | /* If we're generating a system error, then there's nothing |
| 292 | * to wait for. |
| 293 | */ |
| 294 | if (mbcp->mbox_in[0] == MB_CMD_MAKE_SYS_ERR) |
| 295 | goto end; |
| 296 | |
| 297 | /* Wait for the command to complete. We loop |
| 298 | * here because some AEN might arrive while |
| 299 | * we're waiting for the mailbox command to |
| 300 | * complete. If more than 5 arrive then we can |
| 301 | * assume something is wrong. */ |
| 302 | count = 5; |
| 303 | do { |
| 304 | /* Wait for the interrupt to come in. */ |
| 305 | status = ql_wait_mbx_cmd_cmplt(qdev); |
| 306 | if (status) |
| 307 | goto end; |
| 308 | |
| 309 | /* Process the event. If it's an AEN, it |
| 310 | * will be handled in-line or a worker |
| 311 | * will be spawned. If it's our completion |
| 312 | * we will catch it below. |
| 313 | */ |
| 314 | status = ql_mpi_handler(qdev, mbcp); |
| 315 | if (status) |
| 316 | goto end; |
| 317 | |
| 318 | /* It's either the completion for our mailbox |
| 319 | * command complete or an AEN. If it's our |
| 320 | * completion then get out. |
| 321 | */ |
| 322 | if (((mbcp->mbox_out[0] & 0x0000f000) == |
| 323 | MB_CMD_STS_GOOD) || |
| 324 | ((mbcp->mbox_out[0] & 0x0000f000) == |
| 325 | MB_CMD_STS_INTRMDT)) |
| 326 | break; |
| 327 | } while (--count); |
| 328 | |
| 329 | if (!count) { |
| 330 | QPRINTK(qdev, DRV, ERR, |
| 331 | "Timed out waiting for mailbox complete.\n"); |
| 332 | status = -ETIMEDOUT; |
| 333 | goto end; |
| 334 | } |
| 335 | |
| 336 | /* Now we can clear the interrupt condition |
| 337 | * and look at our status. |
| 338 | */ |
| 339 | ql_write32(qdev, CSR, CSR_CMD_CLR_R2PCI_INT); |
| 340 | |
| 341 | if (((mbcp->mbox_out[0] & 0x0000f000) != |
| 342 | MB_CMD_STS_GOOD) && |
| 343 | ((mbcp->mbox_out[0] & 0x0000f000) != |
| 344 | MB_CMD_STS_INTRMDT)) { |
| 345 | ql_display_mb_sts(qdev, mbcp); |
| 346 | status = -EIO; |
| 347 | } |
| 348 | end: |
| 349 | mutex_unlock(&qdev->mpi_mutex); |
| 350 | /* End polled mode for MPI */ |
| 351 | ql_write32(qdev, INTR_MASK, (INTR_MASK_PI << 16) | INTR_MASK_PI); |
| 352 | return status; |
| 353 | } |
| 354 | |
| 355 | /* Get functional state for MPI firmware. |
| 356 | * Returns zero on success. |
| 357 | */ |
| 358 | int ql_mb_get_fw_state(struct ql_adapter *qdev) |
| 359 | { |
| 360 | struct mbox_params mbc; |
| 361 | struct mbox_params *mbcp = &mbc; |
| 362 | int status = 0; |
| 363 | |
| 364 | memset(mbcp, 0, sizeof(struct mbox_params)); |
| 365 | |
| 366 | mbcp->in_count = 1; |
| 367 | mbcp->out_count = 2; |
| 368 | |
| 369 | mbcp->mbox_in[0] = MB_CMD_GET_FW_STATE; |
| 370 | |
| 371 | status = ql_mailbox_command(qdev, mbcp); |
| 372 | if (status) |
| 373 | return status; |
| 374 | |
| 375 | if (mbcp->mbox_out[0] != MB_CMD_STS_GOOD) { |
| 376 | QPRINTK(qdev, DRV, ERR, |
| 377 | "Failed Get Firmware State.\n"); |
| 378 | status = -EIO; |
| 379 | } |
| 380 | |
| 381 | /* If bit zero is set in mbx 1 then the firmware is |
| 382 | * running, but not initialized. This should never |
| 383 | * happen. |
| 384 | */ |
| 385 | if (mbcp->mbox_out[1] & 1) { |
| 386 | QPRINTK(qdev, DRV, ERR, |
| 387 | "Firmware waiting for initialization.\n"); |
| 388 | status = -EIO; |
| 389 | } |
| 390 | |
| 391 | return status; |
| 392 | } |
| 393 | |
Ron Mercer | c4e84bd | 2008-09-18 11:56:28 -0400 | [diff] [blame] | 394 | void ql_mpi_work(struct work_struct *work) |
| 395 | { |
| 396 | struct ql_adapter *qdev = |
| 397 | container_of(work, struct ql_adapter, mpi_work.work); |
| 398 | struct mbox_params mbc; |
| 399 | struct mbox_params *mbcp = &mbc; |
Ron Mercer | 125844e | 2009-02-26 10:08:34 +0000 | [diff] [blame] | 400 | |
| 401 | mutex_lock(&qdev->mpi_mutex); |
Ron Mercer | c4e84bd | 2008-09-18 11:56:28 -0400 | [diff] [blame] | 402 | |
| 403 | while (ql_read32(qdev, STS) & STS_PI) { |
Ron Mercer | 125844e | 2009-02-26 10:08:34 +0000 | [diff] [blame] | 404 | memset(mbcp, 0, sizeof(struct mbox_params)); |
| 405 | mbcp->out_count = 1; |
| 406 | ql_mpi_handler(qdev, mbcp); |
Ron Mercer | c4e84bd | 2008-09-18 11:56:28 -0400 | [diff] [blame] | 407 | } |
Ron Mercer | 125844e | 2009-02-26 10:08:34 +0000 | [diff] [blame] | 408 | |
| 409 | mutex_unlock(&qdev->mpi_mutex); |
Ron Mercer | c4e84bd | 2008-09-18 11:56:28 -0400 | [diff] [blame] | 410 | ql_enable_completion_interrupt(qdev, 0); |
| 411 | } |
| 412 | |
| 413 | void ql_mpi_reset_work(struct work_struct *work) |
| 414 | { |
| 415 | struct ql_adapter *qdev = |
| 416 | container_of(work, struct ql_adapter, mpi_reset_work.work); |
Ron Mercer | a2e809b | 2009-02-26 10:08:33 +0000 | [diff] [blame] | 417 | ql_soft_reset_mpi_risc(qdev); |
Ron Mercer | c4e84bd | 2008-09-18 11:56:28 -0400 | [diff] [blame] | 418 | } |