mpt2sas_transport.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382
  1. /*
  2. * SAS Transport Layer for MPT (Message Passing Technology) based controllers
  3. *
  4. * This code is based on drivers/scsi/mpt2sas/mpt2_transport.c
  5. * Copyright (C) 2007-2009 LSI Corporation
  6. * (mailto:DL-MPTFusionLinux@lsi.com)
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * NO WARRANTY
  19. * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
  20. * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
  21. * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
  22. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
  23. * solely responsible for determining the appropriateness of using and
  24. * distributing the Program and assumes all risks associated with its
  25. * exercise of rights under this Agreement, including but not limited to
  26. * the risks and costs of program errors, damage to or loss of data,
  27. * programs or equipment, and unavailability or interruption of operations.
  28. * DISCLAIMER OF LIABILITY
  29. * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
  30. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
  32. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  33. * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  34. * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
  35. * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
  36. * You should have received a copy of the GNU General Public License
  37. * along with this program; if not, write to the Free Software
  38. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  39. * USA.
  40. */
  41. #include <linux/module.h>
  42. #include <linux/kernel.h>
  43. #include <linux/init.h>
  44. #include <linux/errno.h>
  45. #include <linux/sched.h>
  46. #include <linux/workqueue.h>
  47. #include <linux/delay.h>
  48. #include <linux/pci.h>
  49. #include <linux/slab.h>
  50. #include <scsi/scsi.h>
  51. #include <scsi/scsi_cmnd.h>
  52. #include <scsi/scsi_device.h>
  53. #include <scsi/scsi_host.h>
  54. #include <scsi/scsi_transport_sas.h>
  55. #include <scsi/scsi_dbg.h>
  56. #include "mpt2sas_base.h"
  57. /**
  58. * _transport_sas_node_find_by_sas_address - sas node search
  59. * @ioc: per adapter object
  60. * @sas_address: sas address of expander or sas host
  61. * Context: Calling function should acquire ioc->sas_node_lock.
  62. *
  63. * Search for either hba phys or expander device based on handle, then returns
  64. * the sas_node object.
  65. */
  66. static struct _sas_node *
  67. _transport_sas_node_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
  68. u64 sas_address)
  69. {
  70. if (ioc->sas_hba.sas_address == sas_address)
  71. return &ioc->sas_hba;
  72. else
  73. return mpt2sas_scsih_expander_find_by_sas_address(ioc,
  74. sas_address);
  75. }
  76. /**
  77. * _transport_convert_phy_link_rate -
  78. * @link_rate: link rate returned from mpt firmware
  79. *
  80. * Convert link_rate from mpi fusion into sas_transport form.
  81. */
  82. static enum sas_linkrate
  83. _transport_convert_phy_link_rate(u8 link_rate)
  84. {
  85. enum sas_linkrate rc;
  86. switch (link_rate) {
  87. case MPI2_SAS_NEG_LINK_RATE_1_5:
  88. rc = SAS_LINK_RATE_1_5_GBPS;
  89. break;
  90. case MPI2_SAS_NEG_LINK_RATE_3_0:
  91. rc = SAS_LINK_RATE_3_0_GBPS;
  92. break;
  93. case MPI2_SAS_NEG_LINK_RATE_6_0:
  94. rc = SAS_LINK_RATE_6_0_GBPS;
  95. break;
  96. case MPI2_SAS_NEG_LINK_RATE_PHY_DISABLED:
  97. rc = SAS_PHY_DISABLED;
  98. break;
  99. case MPI2_SAS_NEG_LINK_RATE_NEGOTIATION_FAILED:
  100. rc = SAS_LINK_RATE_FAILED;
  101. break;
  102. case MPI2_SAS_NEG_LINK_RATE_PORT_SELECTOR:
  103. rc = SAS_SATA_PORT_SELECTOR;
  104. break;
  105. case MPI2_SAS_NEG_LINK_RATE_SMP_RESET_IN_PROGRESS:
  106. rc = SAS_PHY_RESET_IN_PROGRESS;
  107. break;
  108. default:
  109. case MPI2_SAS_NEG_LINK_RATE_SATA_OOB_COMPLETE:
  110. case MPI2_SAS_NEG_LINK_RATE_UNKNOWN_LINK_RATE:
  111. rc = SAS_LINK_RATE_UNKNOWN;
  112. break;
  113. }
  114. return rc;
  115. }
  116. /**
  117. * _transport_set_identify - set identify for phys and end devices
  118. * @ioc: per adapter object
  119. * @handle: device handle
  120. * @identify: sas identify info
  121. *
  122. * Populates sas identify info.
  123. *
  124. * Returns 0 for success, non-zero for failure.
  125. */
  126. static int
  127. _transport_set_identify(struct MPT2SAS_ADAPTER *ioc, u16 handle,
  128. struct sas_identify *identify)
  129. {
  130. Mpi2SasDevicePage0_t sas_device_pg0;
  131. Mpi2ConfigReply_t mpi_reply;
  132. u32 device_info;
  133. u32 ioc_status;
  134. if (ioc->shost_recovery) {
  135. printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
  136. __func__, ioc->name);
  137. return -EFAULT;
  138. }
  139. if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
  140. MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
  141. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  142. ioc->name, __FILE__, __LINE__, __func__);
  143. return -ENXIO;
  144. }
  145. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  146. MPI2_IOCSTATUS_MASK;
  147. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  148. printk(MPT2SAS_ERR_FMT "handle(0x%04x), ioc_status(0x%04x)"
  149. "\nfailure at %s:%d/%s()!\n", ioc->name, handle, ioc_status,
  150. __FILE__, __LINE__, __func__);
  151. return -EIO;
  152. }
  153. memset(identify, 0, sizeof(identify));
  154. device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
  155. /* sas_address */
  156. identify->sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
  157. /* device_type */
  158. switch (device_info & MPI2_SAS_DEVICE_INFO_MASK_DEVICE_TYPE) {
  159. case MPI2_SAS_DEVICE_INFO_NO_DEVICE:
  160. identify->device_type = SAS_PHY_UNUSED;
  161. break;
  162. case MPI2_SAS_DEVICE_INFO_END_DEVICE:
  163. identify->device_type = SAS_END_DEVICE;
  164. break;
  165. case MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER:
  166. identify->device_type = SAS_EDGE_EXPANDER_DEVICE;
  167. break;
  168. case MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER:
  169. identify->device_type = SAS_FANOUT_EXPANDER_DEVICE;
  170. break;
  171. }
  172. /* initiator_port_protocols */
  173. if (device_info & MPI2_SAS_DEVICE_INFO_SSP_INITIATOR)
  174. identify->initiator_port_protocols |= SAS_PROTOCOL_SSP;
  175. if (device_info & MPI2_SAS_DEVICE_INFO_STP_INITIATOR)
  176. identify->initiator_port_protocols |= SAS_PROTOCOL_STP;
  177. if (device_info & MPI2_SAS_DEVICE_INFO_SMP_INITIATOR)
  178. identify->initiator_port_protocols |= SAS_PROTOCOL_SMP;
  179. if (device_info & MPI2_SAS_DEVICE_INFO_SATA_HOST)
  180. identify->initiator_port_protocols |= SAS_PROTOCOL_SATA;
  181. /* target_port_protocols */
  182. if (device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET)
  183. identify->target_port_protocols |= SAS_PROTOCOL_SSP;
  184. if (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET)
  185. identify->target_port_protocols |= SAS_PROTOCOL_STP;
  186. if (device_info & MPI2_SAS_DEVICE_INFO_SMP_TARGET)
  187. identify->target_port_protocols |= SAS_PROTOCOL_SMP;
  188. if (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
  189. identify->target_port_protocols |= SAS_PROTOCOL_SATA;
  190. return 0;
  191. }
  192. /**
  193. * mpt2sas_transport_done - internal transport layer callback handler.
  194. * @ioc: per adapter object
  195. * @smid: system request message index
  196. * @msix_index: MSIX table index supplied by the OS
  197. * @reply: reply message frame(lower 32bit addr)
  198. *
  199. * Callback handler when sending internal generated transport cmds.
  200. * The callback index passed is `ioc->transport_cb_idx`
  201. *
  202. * Return 1 meaning mf should be freed from _base_interrupt
  203. * 0 means the mf is freed from this function.
  204. */
  205. u8
  206. mpt2sas_transport_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
  207. u32 reply)
  208. {
  209. MPI2DefaultReply_t *mpi_reply;
  210. mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
  211. if (ioc->transport_cmds.status == MPT2_CMD_NOT_USED)
  212. return 1;
  213. if (ioc->transport_cmds.smid != smid)
  214. return 1;
  215. ioc->transport_cmds.status |= MPT2_CMD_COMPLETE;
  216. if (mpi_reply) {
  217. memcpy(ioc->transport_cmds.reply, mpi_reply,
  218. mpi_reply->MsgLength*4);
  219. ioc->transport_cmds.status |= MPT2_CMD_REPLY_VALID;
  220. }
  221. ioc->transport_cmds.status &= ~MPT2_CMD_PENDING;
  222. complete(&ioc->transport_cmds.done);
  223. return 1;
  224. }
  225. /* report manufacture request structure */
  226. struct rep_manu_request{
  227. u8 smp_frame_type;
  228. u8 function;
  229. u8 reserved;
  230. u8 request_length;
  231. };
  232. /* report manufacture reply structure */
  233. struct rep_manu_reply{
  234. u8 smp_frame_type; /* 0x41 */
  235. u8 function; /* 0x01 */
  236. u8 function_result;
  237. u8 response_length;
  238. u16 expander_change_count;
  239. u8 reserved0[2];
  240. u8 sas_format;
  241. u8 reserved2[3];
  242. u8 vendor_id[SAS_EXPANDER_VENDOR_ID_LEN];
  243. u8 product_id[SAS_EXPANDER_PRODUCT_ID_LEN];
  244. u8 product_rev[SAS_EXPANDER_PRODUCT_REV_LEN];
  245. u8 component_vendor_id[SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN];
  246. u16 component_id;
  247. u8 component_revision_id;
  248. u8 reserved3;
  249. u8 vendor_specific[8];
  250. };
  251. /**
  252. * _transport_expander_report_manufacture - obtain SMP report_manufacture
  253. * @ioc: per adapter object
  254. * @sas_address: expander sas address
  255. * @edev: the sas_expander_device object
  256. *
  257. * Fills in the sas_expander_device object when SMP port is created.
  258. *
  259. * Returns 0 for success, non-zero for failure.
  260. */
  261. static int
  262. _transport_expander_report_manufacture(struct MPT2SAS_ADAPTER *ioc,
  263. u64 sas_address, struct sas_expander_device *edev)
  264. {
  265. Mpi2SmpPassthroughRequest_t *mpi_request;
  266. Mpi2SmpPassthroughReply_t *mpi_reply;
  267. struct rep_manu_reply *manufacture_reply;
  268. struct rep_manu_request *manufacture_request;
  269. int rc;
  270. u16 smid;
  271. u32 ioc_state;
  272. unsigned long timeleft;
  273. void *psge;
  274. u32 sgl_flags;
  275. u8 issue_reset = 0;
  276. void *data_out = NULL;
  277. dma_addr_t data_out_dma;
  278. u32 sz;
  279. u64 *sas_address_le;
  280. u16 wait_state_count;
  281. if (ioc->shost_recovery) {
  282. printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
  283. __func__, ioc->name);
  284. return -EFAULT;
  285. }
  286. mutex_lock(&ioc->transport_cmds.mutex);
  287. if (ioc->transport_cmds.status != MPT2_CMD_NOT_USED) {
  288. printk(MPT2SAS_ERR_FMT "%s: transport_cmds in use\n",
  289. ioc->name, __func__);
  290. rc = -EAGAIN;
  291. goto out;
  292. }
  293. ioc->transport_cmds.status = MPT2_CMD_PENDING;
  294. wait_state_count = 0;
  295. ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
  296. while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  297. if (wait_state_count++ == 10) {
  298. printk(MPT2SAS_ERR_FMT
  299. "%s: failed due to ioc not operational\n",
  300. ioc->name, __func__);
  301. rc = -EFAULT;
  302. goto out;
  303. }
  304. ssleep(1);
  305. ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
  306. printk(MPT2SAS_INFO_FMT "%s: waiting for "
  307. "operational state(count=%d)\n", ioc->name,
  308. __func__, wait_state_count);
  309. }
  310. if (wait_state_count)
  311. printk(MPT2SAS_INFO_FMT "%s: ioc is operational\n",
  312. ioc->name, __func__);
  313. smid = mpt2sas_base_get_smid(ioc, ioc->transport_cb_idx);
  314. if (!smid) {
  315. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  316. ioc->name, __func__);
  317. rc = -EAGAIN;
  318. goto out;
  319. }
  320. rc = 0;
  321. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  322. ioc->transport_cmds.smid = smid;
  323. sz = sizeof(struct rep_manu_request) + sizeof(struct rep_manu_reply);
  324. data_out = pci_alloc_consistent(ioc->pdev, sz, &data_out_dma);
  325. if (!data_out) {
  326. printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
  327. __LINE__, __func__);
  328. rc = -ENOMEM;
  329. mpt2sas_base_free_smid(ioc, smid);
  330. goto out;
  331. }
  332. manufacture_request = data_out;
  333. manufacture_request->smp_frame_type = 0x40;
  334. manufacture_request->function = 1;
  335. manufacture_request->reserved = 0;
  336. manufacture_request->request_length = 0;
  337. memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
  338. mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
  339. mpi_request->PhysicalPort = 0xFF;
  340. mpi_request->VF_ID = 0; /* TODO */
  341. mpi_request->VP_ID = 0;
  342. sas_address_le = (u64 *)&mpi_request->SASAddress;
  343. *sas_address_le = cpu_to_le64(sas_address);
  344. mpi_request->RequestDataLength =
  345. cpu_to_le16(sizeof(struct rep_manu_request));
  346. psge = &mpi_request->SGL;
  347. /* WRITE sgel first */
  348. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  349. MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
  350. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  351. ioc->base_add_sg_single(psge, sgl_flags |
  352. sizeof(struct rep_manu_request), data_out_dma);
  353. /* incr sgel */
  354. psge += ioc->sge_size;
  355. /* READ sgel last */
  356. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  357. MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
  358. MPI2_SGE_FLAGS_END_OF_LIST);
  359. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  360. ioc->base_add_sg_single(psge, sgl_flags |
  361. sizeof(struct rep_manu_reply), data_out_dma +
  362. sizeof(struct rep_manu_request));
  363. dtransportprintk(ioc, printk(MPT2SAS_DEBUG_FMT "report_manufacture - "
  364. "send to sas_addr(0x%016llx)\n", ioc->name,
  365. (unsigned long long)sas_address));
  366. mpt2sas_base_put_smid_default(ioc, smid);
  367. init_completion(&ioc->transport_cmds.done);
  368. timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done,
  369. 10*HZ);
  370. if (!(ioc->transport_cmds.status & MPT2_CMD_COMPLETE)) {
  371. printk(MPT2SAS_ERR_FMT "%s: timeout\n",
  372. ioc->name, __func__);
  373. _debug_dump_mf(mpi_request,
  374. sizeof(Mpi2SmpPassthroughRequest_t)/4);
  375. if (!(ioc->transport_cmds.status & MPT2_CMD_RESET))
  376. issue_reset = 1;
  377. goto issue_host_reset;
  378. }
  379. dtransportprintk(ioc, printk(MPT2SAS_DEBUG_FMT "report_manufacture - "
  380. "complete\n", ioc->name));
  381. if (ioc->transport_cmds.status & MPT2_CMD_REPLY_VALID) {
  382. u8 *tmp;
  383. mpi_reply = ioc->transport_cmds.reply;
  384. dtransportprintk(ioc, printk(MPT2SAS_DEBUG_FMT
  385. "report_manufacture - reply data transfer size(%d)\n",
  386. ioc->name, le16_to_cpu(mpi_reply->ResponseDataLength)));
  387. if (le16_to_cpu(mpi_reply->ResponseDataLength) !=
  388. sizeof(struct rep_manu_reply))
  389. goto out;
  390. manufacture_reply = data_out + sizeof(struct rep_manu_request);
  391. strncpy(edev->vendor_id, manufacture_reply->vendor_id,
  392. SAS_EXPANDER_VENDOR_ID_LEN);
  393. strncpy(edev->product_id, manufacture_reply->product_id,
  394. SAS_EXPANDER_PRODUCT_ID_LEN);
  395. strncpy(edev->product_rev, manufacture_reply->product_rev,
  396. SAS_EXPANDER_PRODUCT_REV_LEN);
  397. edev->level = manufacture_reply->sas_format & 1;
  398. if (edev->level) {
  399. strncpy(edev->component_vendor_id,
  400. manufacture_reply->component_vendor_id,
  401. SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN);
  402. tmp = (u8 *)&manufacture_reply->component_id;
  403. edev->component_id = tmp[0] << 8 | tmp[1];
  404. edev->component_revision_id =
  405. manufacture_reply->component_revision_id;
  406. }
  407. } else
  408. dtransportprintk(ioc, printk(MPT2SAS_DEBUG_FMT
  409. "report_manufacture - no reply\n", ioc->name));
  410. issue_host_reset:
  411. if (issue_reset)
  412. mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  413. FORCE_BIG_HAMMER);
  414. out:
  415. ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
  416. if (data_out)
  417. pci_free_consistent(ioc->pdev, sz, data_out, data_out_dma);
  418. mutex_unlock(&ioc->transport_cmds.mutex);
  419. return rc;
  420. }
  421. /**
  422. * mpt2sas_transport_port_add - insert port to the list
  423. * @ioc: per adapter object
  424. * @handle: handle of attached device
  425. * @sas_address: sas address of parent expander or sas host
  426. * Context: This function will acquire ioc->sas_node_lock.
  427. *
  428. * Adding new port object to the sas_node->sas_port_list.
  429. *
  430. * Returns mpt2sas_port.
  431. */
  432. struct _sas_port *
  433. mpt2sas_transport_port_add(struct MPT2SAS_ADAPTER *ioc, u16 handle,
  434. u64 sas_address)
  435. {
  436. struct _sas_phy *mpt2sas_phy, *next;
  437. struct _sas_port *mpt2sas_port;
  438. unsigned long flags;
  439. struct _sas_node *sas_node;
  440. struct sas_rphy *rphy;
  441. int i;
  442. struct sas_port *port;
  443. mpt2sas_port = kzalloc(sizeof(struct _sas_port),
  444. GFP_KERNEL);
  445. if (!mpt2sas_port) {
  446. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  447. ioc->name, __FILE__, __LINE__, __func__);
  448. return NULL;
  449. }
  450. INIT_LIST_HEAD(&mpt2sas_port->port_list);
  451. INIT_LIST_HEAD(&mpt2sas_port->phy_list);
  452. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  453. sas_node = _transport_sas_node_find_by_sas_address(ioc, sas_address);
  454. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  455. if (!sas_node) {
  456. printk(MPT2SAS_ERR_FMT "%s: Could not find "
  457. "parent sas_address(0x%016llx)!\n", ioc->name,
  458. __func__, (unsigned long long)sas_address);
  459. goto out_fail;
  460. }
  461. if ((_transport_set_identify(ioc, handle,
  462. &mpt2sas_port->remote_identify))) {
  463. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  464. ioc->name, __FILE__, __LINE__, __func__);
  465. goto out_fail;
  466. }
  467. if (mpt2sas_port->remote_identify.device_type == SAS_PHY_UNUSED) {
  468. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  469. ioc->name, __FILE__, __LINE__, __func__);
  470. goto out_fail;
  471. }
  472. for (i = 0; i < sas_node->num_phys; i++) {
  473. if (sas_node->phy[i].remote_identify.sas_address !=
  474. mpt2sas_port->remote_identify.sas_address)
  475. continue;
  476. list_add_tail(&sas_node->phy[i].port_siblings,
  477. &mpt2sas_port->phy_list);
  478. mpt2sas_port->num_phys++;
  479. }
  480. if (!mpt2sas_port->num_phys) {
  481. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  482. ioc->name, __FILE__, __LINE__, __func__);
  483. goto out_fail;
  484. }
  485. port = sas_port_alloc_num(sas_node->parent_dev);
  486. if ((sas_port_add(port))) {
  487. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  488. ioc->name, __FILE__, __LINE__, __func__);
  489. goto out_fail;
  490. }
  491. list_for_each_entry(mpt2sas_phy, &mpt2sas_port->phy_list,
  492. port_siblings) {
  493. if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
  494. dev_printk(KERN_INFO, &port->dev, "add: handle(0x%04x)"
  495. ", sas_addr(0x%016llx), phy(%d)\n", handle,
  496. (unsigned long long)
  497. mpt2sas_port->remote_identify.sas_address,
  498. mpt2sas_phy->phy_id);
  499. sas_port_add_phy(port, mpt2sas_phy->phy);
  500. }
  501. mpt2sas_port->port = port;
  502. if (mpt2sas_port->remote_identify.device_type == SAS_END_DEVICE)
  503. rphy = sas_end_device_alloc(port);
  504. else
  505. rphy = sas_expander_alloc(port,
  506. mpt2sas_port->remote_identify.device_type);
  507. rphy->identify = mpt2sas_port->remote_identify;
  508. if ((sas_rphy_add(rphy))) {
  509. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  510. ioc->name, __FILE__, __LINE__, __func__);
  511. }
  512. if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
  513. dev_printk(KERN_INFO, &rphy->dev, "add: handle(0x%04x), "
  514. "sas_addr(0x%016llx)\n", handle,
  515. (unsigned long long)
  516. mpt2sas_port->remote_identify.sas_address);
  517. mpt2sas_port->rphy = rphy;
  518. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  519. list_add_tail(&mpt2sas_port->port_list, &sas_node->sas_port_list);
  520. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  521. /* fill in report manufacture */
  522. if (mpt2sas_port->remote_identify.device_type ==
  523. MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
  524. mpt2sas_port->remote_identify.device_type ==
  525. MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER)
  526. _transport_expander_report_manufacture(ioc,
  527. mpt2sas_port->remote_identify.sas_address,
  528. rphy_to_expander_device(rphy));
  529. return mpt2sas_port;
  530. out_fail:
  531. list_for_each_entry_safe(mpt2sas_phy, next, &mpt2sas_port->phy_list,
  532. port_siblings)
  533. list_del(&mpt2sas_phy->port_siblings);
  534. kfree(mpt2sas_port);
  535. return NULL;
  536. }
  537. /**
  538. * mpt2sas_transport_port_remove - remove port from the list
  539. * @ioc: per adapter object
  540. * @sas_address: sas address of attached device
  541. * @sas_address_parent: sas address of parent expander or sas host
  542. * Context: This function will acquire ioc->sas_node_lock.
  543. *
  544. * Removing object and freeing associated memory from the
  545. * ioc->sas_port_list.
  546. *
  547. * Return nothing.
  548. */
  549. void
  550. mpt2sas_transport_port_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
  551. u64 sas_address_parent)
  552. {
  553. int i;
  554. unsigned long flags;
  555. struct _sas_port *mpt2sas_port, *next;
  556. struct _sas_node *sas_node;
  557. u8 found = 0;
  558. struct _sas_phy *mpt2sas_phy, *next_phy;
  559. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  560. sas_node = _transport_sas_node_find_by_sas_address(ioc,
  561. sas_address_parent);
  562. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  563. if (!sas_node)
  564. return;
  565. list_for_each_entry_safe(mpt2sas_port, next, &sas_node->sas_port_list,
  566. port_list) {
  567. if (mpt2sas_port->remote_identify.sas_address != sas_address)
  568. continue;
  569. found = 1;
  570. list_del(&mpt2sas_port->port_list);
  571. goto out;
  572. }
  573. out:
  574. if (!found)
  575. return;
  576. for (i = 0; i < sas_node->num_phys; i++) {
  577. if (sas_node->phy[i].remote_identify.sas_address == sas_address)
  578. memset(&sas_node->phy[i].remote_identify, 0 ,
  579. sizeof(struct sas_identify));
  580. }
  581. list_for_each_entry_safe(mpt2sas_phy, next_phy,
  582. &mpt2sas_port->phy_list, port_siblings) {
  583. if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
  584. dev_printk(KERN_INFO, &mpt2sas_port->port->dev,
  585. "remove: sas_addr(0x%016llx), phy(%d)\n",
  586. (unsigned long long)
  587. mpt2sas_port->remote_identify.sas_address,
  588. mpt2sas_phy->phy_id);
  589. sas_port_delete_phy(mpt2sas_port->port, mpt2sas_phy->phy);
  590. list_del(&mpt2sas_phy->port_siblings);
  591. }
  592. sas_port_delete(mpt2sas_port->port);
  593. kfree(mpt2sas_port);
  594. }
  595. /**
  596. * mpt2sas_transport_add_host_phy - report sas_host phy to transport
  597. * @ioc: per adapter object
  598. * @mpt2sas_phy: mpt2sas per phy object
  599. * @phy_pg0: sas phy page 0
  600. * @parent_dev: parent device class object
  601. *
  602. * Returns 0 for success, non-zero for failure.
  603. */
  604. int
  605. mpt2sas_transport_add_host_phy(struct MPT2SAS_ADAPTER *ioc, struct _sas_phy
  606. *mpt2sas_phy, Mpi2SasPhyPage0_t phy_pg0, struct device *parent_dev)
  607. {
  608. struct sas_phy *phy;
  609. int phy_index = mpt2sas_phy->phy_id;
  610. INIT_LIST_HEAD(&mpt2sas_phy->port_siblings);
  611. phy = sas_phy_alloc(parent_dev, phy_index);
  612. if (!phy) {
  613. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  614. ioc->name, __FILE__, __LINE__, __func__);
  615. return -1;
  616. }
  617. if ((_transport_set_identify(ioc, mpt2sas_phy->handle,
  618. &mpt2sas_phy->identify))) {
  619. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  620. ioc->name, __FILE__, __LINE__, __func__);
  621. return -1;
  622. }
  623. phy->identify = mpt2sas_phy->identify;
  624. mpt2sas_phy->attached_handle = le16_to_cpu(phy_pg0.AttachedDevHandle);
  625. if (mpt2sas_phy->attached_handle)
  626. _transport_set_identify(ioc, mpt2sas_phy->attached_handle,
  627. &mpt2sas_phy->remote_identify);
  628. phy->identify.phy_identifier = mpt2sas_phy->phy_id;
  629. phy->negotiated_linkrate = _transport_convert_phy_link_rate(
  630. phy_pg0.NegotiatedLinkRate & MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
  631. phy->minimum_linkrate_hw = _transport_convert_phy_link_rate(
  632. phy_pg0.HwLinkRate & MPI2_SAS_HWRATE_MIN_RATE_MASK);
  633. phy->maximum_linkrate_hw = _transport_convert_phy_link_rate(
  634. phy_pg0.HwLinkRate >> 4);
  635. phy->minimum_linkrate = _transport_convert_phy_link_rate(
  636. phy_pg0.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
  637. phy->maximum_linkrate = _transport_convert_phy_link_rate(
  638. phy_pg0.ProgrammedLinkRate >> 4);
  639. if ((sas_phy_add(phy))) {
  640. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  641. ioc->name, __FILE__, __LINE__, __func__);
  642. sas_phy_free(phy);
  643. return -1;
  644. }
  645. if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
  646. dev_printk(KERN_INFO, &phy->dev,
  647. "add: handle(0x%04x), sas_addr(0x%016llx)\n"
  648. "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
  649. mpt2sas_phy->handle, (unsigned long long)
  650. mpt2sas_phy->identify.sas_address,
  651. mpt2sas_phy->attached_handle,
  652. (unsigned long long)
  653. mpt2sas_phy->remote_identify.sas_address);
  654. mpt2sas_phy->phy = phy;
  655. return 0;
  656. }
  657. /**
  658. * mpt2sas_transport_add_expander_phy - report expander phy to transport
  659. * @ioc: per adapter object
  660. * @mpt2sas_phy: mpt2sas per phy object
  661. * @expander_pg1: expander page 1
  662. * @parent_dev: parent device class object
  663. *
  664. * Returns 0 for success, non-zero for failure.
  665. */
  666. int
  667. mpt2sas_transport_add_expander_phy(struct MPT2SAS_ADAPTER *ioc, struct _sas_phy
  668. *mpt2sas_phy, Mpi2ExpanderPage1_t expander_pg1, struct device *parent_dev)
  669. {
  670. struct sas_phy *phy;
  671. int phy_index = mpt2sas_phy->phy_id;
  672. INIT_LIST_HEAD(&mpt2sas_phy->port_siblings);
  673. phy = sas_phy_alloc(parent_dev, phy_index);
  674. if (!phy) {
  675. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  676. ioc->name, __FILE__, __LINE__, __func__);
  677. return -1;
  678. }
  679. if ((_transport_set_identify(ioc, mpt2sas_phy->handle,
  680. &mpt2sas_phy->identify))) {
  681. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  682. ioc->name, __FILE__, __LINE__, __func__);
  683. return -1;
  684. }
  685. phy->identify = mpt2sas_phy->identify;
  686. mpt2sas_phy->attached_handle =
  687. le16_to_cpu(expander_pg1.AttachedDevHandle);
  688. if (mpt2sas_phy->attached_handle)
  689. _transport_set_identify(ioc, mpt2sas_phy->attached_handle,
  690. &mpt2sas_phy->remote_identify);
  691. phy->identify.phy_identifier = mpt2sas_phy->phy_id;
  692. phy->negotiated_linkrate = _transport_convert_phy_link_rate(
  693. expander_pg1.NegotiatedLinkRate &
  694. MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
  695. phy->minimum_linkrate_hw = _transport_convert_phy_link_rate(
  696. expander_pg1.HwLinkRate & MPI2_SAS_HWRATE_MIN_RATE_MASK);
  697. phy->maximum_linkrate_hw = _transport_convert_phy_link_rate(
  698. expander_pg1.HwLinkRate >> 4);
  699. phy->minimum_linkrate = _transport_convert_phy_link_rate(
  700. expander_pg1.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
  701. phy->maximum_linkrate = _transport_convert_phy_link_rate(
  702. expander_pg1.ProgrammedLinkRate >> 4);
  703. if ((sas_phy_add(phy))) {
  704. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  705. ioc->name, __FILE__, __LINE__, __func__);
  706. sas_phy_free(phy);
  707. return -1;
  708. }
  709. if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
  710. dev_printk(KERN_INFO, &phy->dev,
  711. "add: handle(0x%04x), sas_addr(0x%016llx)\n"
  712. "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
  713. mpt2sas_phy->handle, (unsigned long long)
  714. mpt2sas_phy->identify.sas_address,
  715. mpt2sas_phy->attached_handle,
  716. (unsigned long long)
  717. mpt2sas_phy->remote_identify.sas_address);
  718. mpt2sas_phy->phy = phy;
  719. return 0;
  720. }
  721. /**
  722. * mpt2sas_transport_update_links - refreshing phy link changes
  723. * @ioc: per adapter object
  724. * @sas_address: sas address of parent expander or sas host
  725. * @handle: attached device handle
  726. * @phy_numberv: phy number
  727. * @link_rate: new link rate
  728. *
  729. * Returns nothing.
  730. */
  731. void
  732. mpt2sas_transport_update_links(struct MPT2SAS_ADAPTER *ioc,
  733. u64 sas_address, u16 handle, u8 phy_number, u8 link_rate)
  734. {
  735. unsigned long flags;
  736. struct _sas_node *sas_node;
  737. struct _sas_phy *mpt2sas_phy;
  738. if (ioc->shost_recovery)
  739. return;
  740. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  741. sas_node = _transport_sas_node_find_by_sas_address(ioc, sas_address);
  742. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  743. if (!sas_node)
  744. return;
  745. mpt2sas_phy = &sas_node->phy[phy_number];
  746. mpt2sas_phy->attached_handle = handle;
  747. if (handle && (link_rate >= MPI2_SAS_NEG_LINK_RATE_1_5))
  748. _transport_set_identify(ioc, handle,
  749. &mpt2sas_phy->remote_identify);
  750. else
  751. memset(&mpt2sas_phy->remote_identify, 0 , sizeof(struct
  752. sas_identify));
  753. if (mpt2sas_phy->phy)
  754. mpt2sas_phy->phy->negotiated_linkrate =
  755. _transport_convert_phy_link_rate(link_rate);
  756. if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
  757. dev_printk(KERN_INFO, &mpt2sas_phy->phy->dev,
  758. "refresh: parent sas_addr(0x%016llx),\n"
  759. "\tlink_rate(0x%02x), phy(%d)\n"
  760. "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
  761. (unsigned long long)sas_address,
  762. link_rate, phy_number, handle, (unsigned long long)
  763. mpt2sas_phy->remote_identify.sas_address);
  764. }
  765. static inline void *
  766. phy_to_ioc(struct sas_phy *phy)
  767. {
  768. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
  769. return shost_priv(shost);
  770. }
  771. static inline void *
  772. rphy_to_ioc(struct sas_rphy *rphy)
  773. {
  774. struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
  775. return shost_priv(shost);
  776. }
  777. static struct _sas_phy *
  778. _transport_find_local_phy(struct MPT2SAS_ADAPTER *ioc, struct sas_phy *phy)
  779. {
  780. int i;
  781. for (i = 0; i < ioc->sas_hba.num_phys; i++)
  782. if (ioc->sas_hba.phy[i].phy == phy)
  783. return(&ioc->sas_hba.phy[i]);
  784. return NULL;
  785. }
  786. /**
  787. * _transport_get_linkerrors -
  788. * @phy: The sas phy object
  789. *
  790. * Only support sas_host direct attached phys.
  791. * Returns 0 for success, non-zero for failure.
  792. *
  793. */
  794. static int
  795. _transport_get_linkerrors(struct sas_phy *phy)
  796. {
  797. struct MPT2SAS_ADAPTER *ioc = phy_to_ioc(phy);
  798. struct _sas_phy *mpt2sas_phy;
  799. Mpi2ConfigReply_t mpi_reply;
  800. Mpi2SasPhyPage1_t phy_pg1;
  801. mpt2sas_phy = _transport_find_local_phy(ioc, phy);
  802. if (!mpt2sas_phy) /* this phy not on sas_host */
  803. return -EINVAL;
  804. if ((mpt2sas_config_get_phy_pg1(ioc, &mpi_reply, &phy_pg1,
  805. mpt2sas_phy->phy_id))) {
  806. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  807. ioc->name, __FILE__, __LINE__, __func__);
  808. return -ENXIO;
  809. }
  810. if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo)
  811. printk(MPT2SAS_INFO_FMT "phy(%d), ioc_status"
  812. "(0x%04x), loginfo(0x%08x)\n", ioc->name,
  813. mpt2sas_phy->phy_id,
  814. le16_to_cpu(mpi_reply.IOCStatus),
  815. le32_to_cpu(mpi_reply.IOCLogInfo));
  816. phy->invalid_dword_count = le32_to_cpu(phy_pg1.InvalidDwordCount);
  817. phy->running_disparity_error_count =
  818. le32_to_cpu(phy_pg1.RunningDisparityErrorCount);
  819. phy->loss_of_dword_sync_count =
  820. le32_to_cpu(phy_pg1.LossDwordSynchCount);
  821. phy->phy_reset_problem_count =
  822. le32_to_cpu(phy_pg1.PhyResetProblemCount);
  823. return 0;
  824. }
  825. /**
  826. * _transport_get_enclosure_identifier -
  827. * @phy: The sas phy object
  828. *
  829. * Obtain the enclosure logical id for an expander.
  830. * Returns 0 for success, non-zero for failure.
  831. */
  832. static int
  833. _transport_get_enclosure_identifier(struct sas_rphy *rphy, u64 *identifier)
  834. {
  835. struct MPT2SAS_ADAPTER *ioc = rphy_to_ioc(rphy);
  836. struct _sas_node *sas_expander;
  837. unsigned long flags;
  838. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  839. sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
  840. rphy->identify.sas_address);
  841. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  842. if (!sas_expander)
  843. return -ENXIO;
  844. *identifier = sas_expander->enclosure_logical_id;
  845. return 0;
  846. }
  847. /**
  848. * _transport_get_bay_identifier -
  849. * @phy: The sas phy object
  850. *
  851. * Returns the slot id for a device that resides inside an enclosure.
  852. */
  853. static int
  854. _transport_get_bay_identifier(struct sas_rphy *rphy)
  855. {
  856. struct MPT2SAS_ADAPTER *ioc = rphy_to_ioc(rphy);
  857. struct _sas_device *sas_device;
  858. unsigned long flags;
  859. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  860. sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  861. rphy->identify.sas_address);
  862. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  863. if (!sas_device)
  864. return -ENXIO;
  865. return sas_device->slot;
  866. }
  867. /**
  868. * _transport_phy_reset -
  869. * @phy: The sas phy object
  870. * @hard_reset:
  871. *
  872. * Only support sas_host direct attached phys.
  873. * Returns 0 for success, non-zero for failure.
  874. */
  875. static int
  876. _transport_phy_reset(struct sas_phy *phy, int hard_reset)
  877. {
  878. struct MPT2SAS_ADAPTER *ioc = phy_to_ioc(phy);
  879. struct _sas_phy *mpt2sas_phy;
  880. Mpi2SasIoUnitControlReply_t mpi_reply;
  881. Mpi2SasIoUnitControlRequest_t mpi_request;
  882. mpt2sas_phy = _transport_find_local_phy(ioc, phy);
  883. if (!mpt2sas_phy) /* this phy not on sas_host */
  884. return -EINVAL;
  885. memset(&mpi_request, 0, sizeof(Mpi2SasIoUnitControlReply_t));
  886. mpi_request.Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
  887. mpi_request.Operation = hard_reset ?
  888. MPI2_SAS_OP_PHY_HARD_RESET : MPI2_SAS_OP_PHY_LINK_RESET;
  889. mpi_request.PhyNum = mpt2sas_phy->phy_id;
  890. if ((mpt2sas_base_sas_iounit_control(ioc, &mpi_reply, &mpi_request))) {
  891. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  892. ioc->name, __FILE__, __LINE__, __func__);
  893. return -ENXIO;
  894. }
  895. if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo)
  896. printk(MPT2SAS_INFO_FMT "phy(%d), ioc_status"
  897. "(0x%04x), loginfo(0x%08x)\n", ioc->name,
  898. mpt2sas_phy->phy_id,
  899. le16_to_cpu(mpi_reply.IOCStatus),
  900. le32_to_cpu(mpi_reply.IOCLogInfo));
  901. return 0;
  902. }
  903. /**
  904. * _transport_phy_enable - enable/disable phys
  905. * @phy: The sas phy object
  906. * @enable: enable phy when true
  907. *
  908. * Only support sas_host direct attached phys.
  909. * Returns 0 for success, non-zero for failure.
  910. */
  911. static int
  912. _transport_phy_enable(struct sas_phy *phy, int enable)
  913. {
  914. struct MPT2SAS_ADAPTER *ioc = phy_to_ioc(phy);
  915. struct _sas_phy *mpt2sas_phy;
  916. Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
  917. Mpi2ConfigReply_t mpi_reply;
  918. u16 ioc_status;
  919. u16 sz;
  920. int rc = 0;
  921. mpt2sas_phy = _transport_find_local_phy(ioc, phy);
  922. if (!mpt2sas_phy) /* this phy not on sas_host */
  923. return -EINVAL;
  924. /* sas_iounit page 1 */
  925. sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
  926. sizeof(Mpi2SasIOUnit1PhyData_t));
  927. sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
  928. if (!sas_iounit_pg1) {
  929. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  930. ioc->name, __FILE__, __LINE__, __func__);
  931. rc = -ENOMEM;
  932. goto out;
  933. }
  934. if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
  935. sas_iounit_pg1, sz))) {
  936. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  937. ioc->name, __FILE__, __LINE__, __func__);
  938. rc = -ENXIO;
  939. goto out;
  940. }
  941. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  942. MPI2_IOCSTATUS_MASK;
  943. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  944. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  945. ioc->name, __FILE__, __LINE__, __func__);
  946. rc = -EIO;
  947. goto out;
  948. }
  949. if (enable)
  950. sas_iounit_pg1->PhyData[mpt2sas_phy->phy_id].PhyFlags
  951. &= ~MPI2_SASIOUNIT1_PHYFLAGS_PHY_DISABLE;
  952. else
  953. sas_iounit_pg1->PhyData[mpt2sas_phy->phy_id].PhyFlags
  954. |= MPI2_SASIOUNIT1_PHYFLAGS_PHY_DISABLE;
  955. mpt2sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1, sz);
  956. out:
  957. kfree(sas_iounit_pg1);
  958. return rc;
  959. }
  960. /**
  961. * _transport_phy_speed - set phy min/max link rates
  962. * @phy: The sas phy object
  963. * @rates: rates defined in sas_phy_linkrates
  964. *
  965. * Only support sas_host direct attached phys.
  966. * Returns 0 for success, non-zero for failure.
  967. */
  968. static int
  969. _transport_phy_speed(struct sas_phy *phy, struct sas_phy_linkrates *rates)
  970. {
  971. struct MPT2SAS_ADAPTER *ioc = phy_to_ioc(phy);
  972. struct _sas_phy *mpt2sas_phy;
  973. Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
  974. Mpi2SasPhyPage0_t phy_pg0;
  975. Mpi2ConfigReply_t mpi_reply;
  976. u16 ioc_status;
  977. u16 sz;
  978. int i;
  979. int rc = 0;
  980. mpt2sas_phy = _transport_find_local_phy(ioc, phy);
  981. if (!mpt2sas_phy) /* this phy not on sas_host */
  982. return -EINVAL;
  983. if (!rates->minimum_linkrate)
  984. rates->minimum_linkrate = phy->minimum_linkrate;
  985. else if (rates->minimum_linkrate < phy->minimum_linkrate_hw)
  986. rates->minimum_linkrate = phy->minimum_linkrate_hw;
  987. if (!rates->maximum_linkrate)
  988. rates->maximum_linkrate = phy->maximum_linkrate;
  989. else if (rates->maximum_linkrate > phy->maximum_linkrate_hw)
  990. rates->maximum_linkrate = phy->maximum_linkrate_hw;
  991. /* sas_iounit page 1 */
  992. sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
  993. sizeof(Mpi2SasIOUnit1PhyData_t));
  994. sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
  995. if (!sas_iounit_pg1) {
  996. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  997. ioc->name, __FILE__, __LINE__, __func__);
  998. rc = -ENOMEM;
  999. goto out;
  1000. }
  1001. if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
  1002. sas_iounit_pg1, sz))) {
  1003. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1004. ioc->name, __FILE__, __LINE__, __func__);
  1005. rc = -ENXIO;
  1006. goto out;
  1007. }
  1008. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  1009. MPI2_IOCSTATUS_MASK;
  1010. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  1011. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1012. ioc->name, __FILE__, __LINE__, __func__);
  1013. rc = -EIO;
  1014. goto out;
  1015. }
  1016. for (i = 0; i < ioc->sas_hba.num_phys; i++) {
  1017. if (mpt2sas_phy->phy_id != i) {
  1018. sas_iounit_pg1->PhyData[i].MaxMinLinkRate =
  1019. (ioc->sas_hba.phy[i].phy->minimum_linkrate +
  1020. (ioc->sas_hba.phy[i].phy->maximum_linkrate << 4));
  1021. } else {
  1022. sas_iounit_pg1->PhyData[i].MaxMinLinkRate =
  1023. (rates->minimum_linkrate +
  1024. (rates->maximum_linkrate << 4));
  1025. }
  1026. }
  1027. if (mpt2sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1,
  1028. sz)) {
  1029. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1030. ioc->name, __FILE__, __LINE__, __func__);
  1031. rc = -ENXIO;
  1032. goto out;
  1033. }
  1034. /* link reset */
  1035. _transport_phy_reset(phy, 0);
  1036. /* read phy page 0, then update the rates in the sas transport phy */
  1037. if (!mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
  1038. mpt2sas_phy->phy_id)) {
  1039. phy->minimum_linkrate = _transport_convert_phy_link_rate(
  1040. phy_pg0.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
  1041. phy->maximum_linkrate = _transport_convert_phy_link_rate(
  1042. phy_pg0.ProgrammedLinkRate >> 4);
  1043. phy->negotiated_linkrate = _transport_convert_phy_link_rate(
  1044. phy_pg0.NegotiatedLinkRate &
  1045. MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
  1046. }
  1047. out:
  1048. kfree(sas_iounit_pg1);
  1049. return rc;
  1050. }
  1051. /**
  1052. * _transport_smp_handler - transport portal for smp passthru
  1053. * @shost: shost object
  1054. * @rphy: sas transport rphy object
  1055. * @req:
  1056. *
  1057. * This used primarily for smp_utils.
  1058. * Example:
  1059. * smp_rep_general /sys/class/bsg/expander-5:0
  1060. */
  1061. static int
  1062. _transport_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
  1063. struct request *req)
  1064. {
  1065. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  1066. Mpi2SmpPassthroughRequest_t *mpi_request;
  1067. Mpi2SmpPassthroughReply_t *mpi_reply;
  1068. int rc;
  1069. u16 smid;
  1070. u32 ioc_state;
  1071. unsigned long timeleft;
  1072. void *psge;
  1073. u32 sgl_flags;
  1074. u8 issue_reset = 0;
  1075. dma_addr_t dma_addr_in = 0;
  1076. dma_addr_t dma_addr_out = 0;
  1077. u16 wait_state_count;
  1078. struct request *rsp = req->next_rq;
  1079. if (!rsp) {
  1080. printk(MPT2SAS_ERR_FMT "%s: the smp response space is "
  1081. "missing\n", ioc->name, __func__);
  1082. return -EINVAL;
  1083. }
  1084. /* do we need to support multiple segments? */
  1085. if (req->bio->bi_vcnt > 1 || rsp->bio->bi_vcnt > 1) {
  1086. printk(MPT2SAS_ERR_FMT "%s: multiple segments req %u %u, "
  1087. "rsp %u %u\n", ioc->name, __func__, req->bio->bi_vcnt,
  1088. blk_rq_bytes(req), rsp->bio->bi_vcnt, blk_rq_bytes(rsp));
  1089. return -EINVAL;
  1090. }
  1091. if (ioc->shost_recovery) {
  1092. printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
  1093. __func__, ioc->name);
  1094. return -EFAULT;
  1095. }
  1096. rc = mutex_lock_interruptible(&ioc->transport_cmds.mutex);
  1097. if (rc)
  1098. return rc;
  1099. if (ioc->transport_cmds.status != MPT2_CMD_NOT_USED) {
  1100. printk(MPT2SAS_ERR_FMT "%s: transport_cmds in use\n", ioc->name,
  1101. __func__);
  1102. rc = -EAGAIN;
  1103. goto out;
  1104. }
  1105. ioc->transport_cmds.status = MPT2_CMD_PENDING;
  1106. wait_state_count = 0;
  1107. ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
  1108. while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  1109. if (wait_state_count++ == 10) {
  1110. printk(MPT2SAS_ERR_FMT
  1111. "%s: failed due to ioc not operational\n",
  1112. ioc->name, __func__);
  1113. rc = -EFAULT;
  1114. goto out;
  1115. }
  1116. ssleep(1);
  1117. ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
  1118. printk(MPT2SAS_INFO_FMT "%s: waiting for "
  1119. "operational state(count=%d)\n", ioc->name,
  1120. __func__, wait_state_count);
  1121. }
  1122. if (wait_state_count)
  1123. printk(MPT2SAS_INFO_FMT "%s: ioc is operational\n",
  1124. ioc->name, __func__);
  1125. smid = mpt2sas_base_get_smid(ioc, ioc->transport_cb_idx);
  1126. if (!smid) {
  1127. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  1128. ioc->name, __func__);
  1129. rc = -EAGAIN;
  1130. goto out;
  1131. }
  1132. rc = 0;
  1133. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  1134. ioc->transport_cmds.smid = smid;
  1135. memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
  1136. mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
  1137. mpi_request->PhysicalPort = 0xFF;
  1138. mpi_request->VF_ID = 0; /* TODO */
  1139. mpi_request->VP_ID = 0;
  1140. *((u64 *)&mpi_request->SASAddress) = (rphy) ?
  1141. cpu_to_le64(rphy->identify.sas_address) :
  1142. cpu_to_le64(ioc->sas_hba.sas_address);
  1143. mpi_request->RequestDataLength = cpu_to_le16(blk_rq_bytes(req) - 4);
  1144. psge = &mpi_request->SGL;
  1145. /* WRITE sgel first */
  1146. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  1147. MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
  1148. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  1149. dma_addr_out = pci_map_single(ioc->pdev, bio_data(req->bio),
  1150. blk_rq_bytes(req), PCI_DMA_BIDIRECTIONAL);
  1151. if (!dma_addr_out) {
  1152. mpt2sas_base_free_smid(ioc, smid);
  1153. goto unmap;
  1154. }
  1155. ioc->base_add_sg_single(psge, sgl_flags | (blk_rq_bytes(req) - 4),
  1156. dma_addr_out);
  1157. /* incr sgel */
  1158. psge += ioc->sge_size;
  1159. /* READ sgel last */
  1160. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  1161. MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
  1162. MPI2_SGE_FLAGS_END_OF_LIST);
  1163. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  1164. dma_addr_in = pci_map_single(ioc->pdev, bio_data(rsp->bio),
  1165. blk_rq_bytes(rsp), PCI_DMA_BIDIRECTIONAL);
  1166. if (!dma_addr_in) {
  1167. mpt2sas_base_free_smid(ioc, smid);
  1168. goto unmap;
  1169. }
  1170. ioc->base_add_sg_single(psge, sgl_flags | (blk_rq_bytes(rsp) + 4),
  1171. dma_addr_in);
  1172. dtransportprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s - "
  1173. "sending smp request\n", ioc->name, __func__));
  1174. mpt2sas_base_put_smid_default(ioc, smid);
  1175. init_completion(&ioc->transport_cmds.done);
  1176. timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done,
  1177. 10*HZ);
  1178. if (!(ioc->transport_cmds.status & MPT2_CMD_COMPLETE)) {
  1179. printk(MPT2SAS_ERR_FMT "%s : timeout\n",
  1180. __func__, ioc->name);
  1181. _debug_dump_mf(mpi_request,
  1182. sizeof(Mpi2SmpPassthroughRequest_t)/4);
  1183. if (!(ioc->transport_cmds.status & MPT2_CMD_RESET))
  1184. issue_reset = 1;
  1185. goto issue_host_reset;
  1186. }
  1187. dtransportprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s - "
  1188. "complete\n", ioc->name, __func__));
  1189. if (ioc->transport_cmds.status & MPT2_CMD_REPLY_VALID) {
  1190. mpi_reply = ioc->transport_cmds.reply;
  1191. dtransportprintk(ioc, printk(MPT2SAS_DEBUG_FMT
  1192. "%s - reply data transfer size(%d)\n",
  1193. ioc->name, __func__,
  1194. le16_to_cpu(mpi_reply->ResponseDataLength)));
  1195. memcpy(req->sense, mpi_reply, sizeof(*mpi_reply));
  1196. req->sense_len = sizeof(*mpi_reply);
  1197. req->resid_len = 0;
  1198. rsp->resid_len -= mpi_reply->ResponseDataLength;
  1199. } else {
  1200. dtransportprintk(ioc, printk(MPT2SAS_DEBUG_FMT
  1201. "%s - no reply\n", ioc->name, __func__));
  1202. rc = -ENXIO;
  1203. }
  1204. issue_host_reset:
  1205. if (issue_reset) {
  1206. mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  1207. FORCE_BIG_HAMMER);
  1208. rc = -ETIMEDOUT;
  1209. }
  1210. unmap:
  1211. if (dma_addr_out)
  1212. pci_unmap_single(ioc->pdev, dma_addr_out, blk_rq_bytes(req),
  1213. PCI_DMA_BIDIRECTIONAL);
  1214. if (dma_addr_in)
  1215. pci_unmap_single(ioc->pdev, dma_addr_in, blk_rq_bytes(rsp),
  1216. PCI_DMA_BIDIRECTIONAL);
  1217. out:
  1218. ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
  1219. mutex_unlock(&ioc->transport_cmds.mutex);
  1220. return rc;
  1221. }
  1222. struct sas_function_template mpt2sas_transport_functions = {
  1223. .get_linkerrors = _transport_get_linkerrors,
  1224. .get_enclosure_identifier = _transport_get_enclosure_identifier,
  1225. .get_bay_identifier = _transport_get_bay_identifier,
  1226. .phy_reset = _transport_phy_reset,
  1227. .phy_enable = _transport_phy_enable,
  1228. .set_phy_speed = _transport_phy_speed,
  1229. .smp_handler = _transport_smp_handler,
  1230. };
  1231. struct scsi_transport_template *mpt2sas_transport_template;