Tesla reports quarterly profit, opening door to S&P 500 as shares rise