mpt2sas_transport.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219
  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 <scsi/scsi.h>
  50. #include <scsi/scsi_cmnd.h>
  51. #include <scsi/scsi_device.h>
  52. #include <scsi/scsi_host.h>
  53. #include <scsi/scsi_transport_sas.h>
  54. #include <scsi/scsi_dbg.h>
  55. #include "mpt2sas_base.h"
  56. /**
  57. * _transport_sas_node_find_by_handle - sas node search
  58. * @ioc: per adapter object
  59. * @handle: expander or hba handle (assigned by firmware)
  60. * Context: Calling function should acquire ioc->sas_node_lock.
  61. *
  62. * Search for either hba phys or expander device based on handle, then returns
  63. * the sas_node object.
  64. */
  65. static struct _sas_node *
  66. _transport_sas_node_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  67. {
  68. int i;
  69. for (i = 0; i < ioc->sas_hba.num_phys; i++)
  70. if (ioc->sas_hba.phy[i].handle == handle)
  71. return &ioc->sas_hba;
  72. return mpt2sas_scsih_expander_find_by_handle(ioc, handle);
  73. }
  74. /**
  75. * _transport_convert_phy_link_rate -
  76. * @link_rate: link rate returned from mpt firmware
  77. *
  78. * Convert link_rate from mpi fusion into sas_transport form.
  79. */
  80. static enum sas_linkrate
  81. _transport_convert_phy_link_rate(u8 link_rate)
  82. {
  83. enum sas_linkrate rc;
  84. switch (link_rate) {
  85. case MPI2_SAS_NEG_LINK_RATE_1_5:
  86. rc = SAS_LINK_RATE_1_5_GBPS;
  87. break;
  88. case MPI2_SAS_NEG_LINK_RATE_3_0:
  89. rc = SAS_LINK_RATE_3_0_GBPS;
  90. break;
  91. case MPI2_SAS_NEG_LINK_RATE_6_0:
  92. rc = SAS_LINK_RATE_6_0_GBPS;
  93. break;
  94. case MPI2_SAS_NEG_LINK_RATE_PHY_DISABLED:
  95. rc = SAS_PHY_DISABLED;
  96. break;
  97. case MPI2_SAS_NEG_LINK_RATE_NEGOTIATION_FAILED:
  98. rc = SAS_LINK_RATE_FAILED;
  99. break;
  100. case MPI2_SAS_NEG_LINK_RATE_PORT_SELECTOR:
  101. rc = SAS_SATA_PORT_SELECTOR;
  102. break;
  103. case MPI2_SAS_NEG_LINK_RATE_SMP_RESET_IN_PROGRESS:
  104. rc = SAS_PHY_RESET_IN_PROGRESS;
  105. break;
  106. default:
  107. case MPI2_SAS_NEG_LINK_RATE_SATA_OOB_COMPLETE:
  108. case MPI2_SAS_NEG_LINK_RATE_UNKNOWN_LINK_RATE:
  109. rc = SAS_LINK_RATE_UNKNOWN;
  110. break;
  111. }
  112. return rc;
  113. }
  114. /**
  115. * _transport_set_identify - set identify for phys and end devices
  116. * @ioc: per adapter object
  117. * @handle: device handle
  118. * @identify: sas identify info
  119. *
  120. * Populates sas identify info.
  121. *
  122. * Returns 0 for success, non-zero for failure.
  123. */
  124. static int
  125. _transport_set_identify(struct MPT2SAS_ADAPTER *ioc, u16 handle,
  126. struct sas_identify *identify)
  127. {
  128. Mpi2SasDevicePage0_t sas_device_pg0;
  129. Mpi2ConfigReply_t mpi_reply;
  130. u32 device_info;
  131. u32 ioc_status;
  132. if (ioc->shost_recovery) {
  133. printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
  134. __func__, ioc->name);
  135. return -EFAULT;
  136. }
  137. if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
  138. MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
  139. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  140. ioc->name, __FILE__, __LINE__, __func__);
  141. return -ENXIO;
  142. }
  143. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  144. MPI2_IOCSTATUS_MASK;
  145. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  146. printk(MPT2SAS_ERR_FMT "handle(0x%04x), ioc_status(0x%04x)"
  147. "\nfailure at %s:%d/%s()!\n", ioc->name, handle, ioc_status,
  148. __FILE__, __LINE__, __func__);
  149. return -EIO;
  150. }
  151. memset(identify, 0, sizeof(identify));
  152. device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
  153. /* sas_address */
  154. identify->sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
  155. /* device_type */
  156. switch (device_info & MPI2_SAS_DEVICE_INFO_MASK_DEVICE_TYPE) {
  157. case MPI2_SAS_DEVICE_INFO_NO_DEVICE:
  158. identify->device_type = SAS_PHY_UNUSED;
  159. break;
  160. case MPI2_SAS_DEVICE_INFO_END_DEVICE:
  161. identify->device_type = SAS_END_DEVICE;
  162. break;
  163. case MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER:
  164. identify->device_type = SAS_EDGE_EXPANDER_DEVICE;
  165. break;
  166. case MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER:
  167. identify->device_type = SAS_FANOUT_EXPANDER_DEVICE;
  168. break;
  169. }
  170. /* initiator_port_protocols */
  171. if (device_info & MPI2_SAS_DEVICE_INFO_SSP_INITIATOR)
  172. identify->initiator_port_protocols |= SAS_PROTOCOL_SSP;
  173. if (device_info & MPI2_SAS_DEVICE_INFO_STP_INITIATOR)
  174. identify->initiator_port_protocols |= SAS_PROTOCOL_STP;
  175. if (device_info & MPI2_SAS_DEVICE_INFO_SMP_INITIATOR)
  176. identify->initiator_port_protocols |= SAS_PROTOCOL_SMP;
  177. if (device_info & MPI2_SAS_DEVICE_INFO_SATA_HOST)
  178. identify->initiator_port_protocols |= SAS_PROTOCOL_SATA;
  179. /* target_port_protocols */
  180. if (device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET)
  181. identify->target_port_protocols |= SAS_PROTOCOL_SSP;
  182. if (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET)
  183. identify->target_port_protocols |= SAS_PROTOCOL_STP;
  184. if (device_info & MPI2_SAS_DEVICE_INFO_SMP_TARGET)
  185. identify->target_port_protocols |= SAS_PROTOCOL_SMP;
  186. if (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
  187. identify->target_port_protocols |= SAS_PROTOCOL_SATA;
  188. return 0;
  189. }
  190. /**
  191. * mpt2sas_transport_done - internal transport layer callback handler.
  192. * @ioc: per adapter object
  193. * @smid: system request message index
  194. * @msix_index: MSIX table index supplied by the OS
  195. * @reply: reply message frame(lower 32bit addr)
  196. *
  197. * Callback handler when sending internal generated transport cmds.
  198. * The callback index passed is `ioc->transport_cb_idx`
  199. *
  200. * Return nothing.
  201. */
  202. void
  203. mpt2sas_transport_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
  204. u32 reply)
  205. {
  206. MPI2DefaultReply_t *mpi_reply;
  207. mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
  208. if (ioc->transport_cmds.status == MPT2_CMD_NOT_USED)
  209. return;
  210. if (ioc->transport_cmds.smid != smid)
  211. return;
  212. ioc->transport_cmds.status |= MPT2_CMD_COMPLETE;
  213. if (mpi_reply) {
  214. memcpy(ioc->transport_cmds.reply, mpi_reply,
  215. mpi_reply->MsgLength*4);
  216. ioc->transport_cmds.status |= MPT2_CMD_REPLY_VALID;
  217. }
  218. ioc->transport_cmds.status &= ~MPT2_CMD_PENDING;
  219. complete(&ioc->transport_cmds.done);
  220. }
  221. /* report manufacture request structure */
  222. struct rep_manu_request{
  223. u8 smp_frame_type;
  224. u8 function;
  225. u8 reserved;
  226. u8 request_length;
  227. };
  228. /* report manufacture reply structure */
  229. struct rep_manu_reply{
  230. u8 smp_frame_type; /* 0x41 */
  231. u8 function; /* 0x01 */
  232. u8 function_result;
  233. u8 response_length;
  234. u16 expander_change_count;
  235. u8 reserved0[2];
  236. u8 sas_format:1;
  237. u8 reserved1:7;
  238. u8 reserved2[3];
  239. u8 vendor_id[SAS_EXPANDER_VENDOR_ID_LEN];
  240. u8 product_id[SAS_EXPANDER_PRODUCT_ID_LEN];
  241. u8 product_rev[SAS_EXPANDER_PRODUCT_REV_LEN];
  242. u8 component_vendor_id[SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN];
  243. u16 component_id;
  244. u8 component_revision_id;
  245. u8 reserved3;
  246. u8 vendor_specific[8];
  247. };
  248. /**
  249. * _transport_expander_report_manufacture - obtain SMP report_manufacture
  250. * @ioc: per adapter object
  251. * @sas_address: expander sas address
  252. * @edev: the sas_expander_device object
  253. *
  254. * Fills in the sas_expander_device object when SMP port is created.
  255. *
  256. * Returns 0 for success, non-zero for failure.
  257. */
  258. static int
  259. _transport_expander_report_manufacture(struct MPT2SAS_ADAPTER *ioc,
  260. u64 sas_address, struct sas_expander_device *edev)
  261. {
  262. Mpi2SmpPassthroughRequest_t *mpi_request;
  263. Mpi2SmpPassthroughReply_t *mpi_reply;
  264. struct rep_manu_reply *manufacture_reply;
  265. struct rep_manu_request *manufacture_request;
  266. int rc;
  267. u16 smid;
  268. u32 ioc_state;
  269. unsigned long timeleft;
  270. void *psge;
  271. u32 sgl_flags;
  272. u8 issue_reset = 0;
  273. void *data_out = NULL;
  274. dma_addr_t data_out_dma;
  275. u32 sz;
  276. u64 *sas_address_le;
  277. u16 wait_state_count;
  278. if (ioc->shost_recovery) {
  279. printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
  280. __func__, ioc->name);
  281. return -EFAULT;
  282. }
  283. mutex_lock(&ioc->transport_cmds.mutex);
  284. if (ioc->transport_cmds.status != MPT2_CMD_NOT_USED) {
  285. printk(MPT2SAS_ERR_FMT "%s: transport_cmds in use\n",
  286. ioc->name, __func__);
  287. rc = -EAGAIN;
  288. goto out;
  289. }
  290. ioc->transport_cmds.status = MPT2_CMD_PENDING;
  291. wait_state_count = 0;
  292. ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
  293. while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  294. if (wait_state_count++ == 10) {
  295. printk(MPT2SAS_ERR_FMT
  296. "%s: failed due to ioc not operational\n",
  297. ioc->name, __func__);
  298. rc = -EFAULT;
  299. goto out;
  300. }
  301. ssleep(1);
  302. ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
  303. printk(MPT2SAS_INFO_FMT "%s: waiting for "
  304. "operational state(count=%d)\n", ioc->name,
  305. __func__, wait_state_count);
  306. }
  307. if (wait_state_count)
  308. printk(MPT2SAS_INFO_FMT "%s: ioc is operational\n",
  309. ioc->name, __func__);
  310. smid = mpt2sas_base_get_smid(ioc, ioc->transport_cb_idx);
  311. if (!smid) {
  312. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  313. ioc->name, __func__);
  314. rc = -EAGAIN;
  315. goto out;
  316. }
  317. rc = 0;
  318. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  319. ioc->transport_cmds.smid = smid;
  320. sz = sizeof(struct rep_manu_request) + sizeof(struct rep_manu_reply);
  321. data_out = pci_alloc_consistent(ioc->pdev, sz, &data_out_dma);
  322. if (!data_out) {
  323. printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
  324. __LINE__, __func__);
  325. rc = -ENOMEM;
  326. mpt2sas_base_free_smid(ioc, smid);
  327. goto out;
  328. }
  329. manufacture_request = data_out;
  330. manufacture_request->smp_frame_type = 0x40;
  331. manufacture_request->function = 1;
  332. manufacture_request->reserved = 0;
  333. manufacture_request->request_length = 0;
  334. memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
  335. mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
  336. mpi_request->PhysicalPort = 0xFF;
  337. mpi_request->VF_ID = 0; /* TODO */
  338. mpi_request->VP_ID = 0;
  339. sas_address_le = (u64 *)&mpi_request->SASAddress;
  340. *sas_address_le = cpu_to_le64(sas_address);
  341. mpi_request->RequestDataLength = sizeof(struct rep_manu_request);
  342. psge = &mpi_request->SGL;
  343. /* WRITE sgel first */
  344. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  345. MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
  346. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  347. ioc->base_add_sg_single(psge, sgl_flags |
  348. sizeof(struct rep_manu_request), data_out_dma);
  349. /* incr sgel */
  350. psge += ioc->sge_size;
  351. /* READ sgel last */
  352. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  353. MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
  354. MPI2_SGE_FLAGS_END_OF_LIST);
  355. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  356. ioc->base_add_sg_single(psge, sgl_flags |
  357. sizeof(struct rep_manu_reply), data_out_dma +
  358. sizeof(struct rep_manu_request));
  359. dtransportprintk(ioc, printk(MPT2SAS_DEBUG_FMT "report_manufacture - "
  360. "send to sas_addr(0x%016llx)\n", ioc->name,
  361. (unsigned long long)sas_address));
  362. mpt2sas_base_put_smid_default(ioc, smid);
  363. timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done,
  364. 10*HZ);
  365. if (!(ioc->transport_cmds.status & MPT2_CMD_COMPLETE)) {
  366. printk(MPT2SAS_ERR_FMT "%s: timeout\n",
  367. ioc->name, __func__);
  368. _debug_dump_mf(mpi_request,
  369. sizeof(Mpi2SmpPassthroughRequest_t)/4);
  370. if (!(ioc->transport_cmds.status & MPT2_CMD_RESET))
  371. issue_reset = 1;
  372. goto issue_host_reset;
  373. }
  374. dtransportprintk(ioc, printk(MPT2SAS_DEBUG_FMT "report_manufacture - "
  375. "complete\n", ioc->name));
  376. if (ioc->transport_cmds.status & MPT2_CMD_REPLY_VALID) {
  377. u8 *tmp;
  378. mpi_reply = ioc->transport_cmds.reply;
  379. dtransportprintk(ioc, printk(MPT2SAS_DEBUG_FMT
  380. "report_manufacture - reply data transfer size(%d)\n",
  381. ioc->name, le16_to_cpu(mpi_reply->ResponseDataLength)));
  382. if (le16_to_cpu(mpi_reply->ResponseDataLength) !=
  383. sizeof(struct rep_manu_reply))
  384. goto out;
  385. manufacture_reply = data_out + sizeof(struct rep_manu_request);
  386. strncpy(edev->vendor_id, manufacture_reply->vendor_id,
  387. SAS_EXPANDER_VENDOR_ID_LEN);
  388. strncpy(edev->product_id, manufacture_reply->product_id,
  389. SAS_EXPANDER_PRODUCT_ID_LEN);
  390. strncpy(edev->product_rev, manufacture_reply->product_rev,
  391. SAS_EXPANDER_PRODUCT_REV_LEN);
  392. edev->level = manufacture_reply->sas_format;
  393. if (manufacture_reply->sas_format) {
  394. strncpy(edev->component_vendor_id,
  395. manufacture_reply->component_vendor_id,
  396. SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN);
  397. tmp = (u8 *)&manufacture_reply->component_id;
  398. edev->component_id = tmp[0] << 8 | tmp[1];
  399. edev->component_revision_id =
  400. manufacture_reply->component_revision_id;
  401. }
  402. } else
  403. dtransportprintk(ioc, printk(MPT2SAS_DEBUG_FMT
  404. "report_manufacture - no reply\n", ioc->name));
  405. issue_host_reset:
  406. if (issue_reset)
  407. mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  408. FORCE_BIG_HAMMER);
  409. out:
  410. ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
  411. if (data_out)
  412. pci_free_consistent(ioc->pdev, sz, data_out, data_out_dma);
  413. mutex_unlock(&ioc->transport_cmds.mutex);
  414. return rc;
  415. }
  416. /**
  417. * mpt2sas_transport_port_add - insert port to the list
  418. * @ioc: per adapter object
  419. * @handle: handle of attached device
  420. * @parent_handle: parent handle(either hba or expander)
  421. * Context: This function will acquire ioc->sas_node_lock.
  422. *
  423. * Adding new port object to the sas_node->sas_port_list.
  424. *
  425. * Returns mpt2sas_port.
  426. */
  427. struct _sas_port *
  428. mpt2sas_transport_port_add(struct MPT2SAS_ADAPTER *ioc, u16 handle,
  429. u16 parent_handle)
  430. {
  431. struct _sas_phy *mpt2sas_phy, *next;
  432. struct _sas_port *mpt2sas_port;
  433. unsigned long flags;
  434. struct _sas_node *sas_node;
  435. struct sas_rphy *rphy;
  436. int i;
  437. struct sas_port *port;
  438. if (!parent_handle)
  439. return NULL;
  440. mpt2sas_port = kzalloc(sizeof(struct _sas_port),
  441. GFP_KERNEL);
  442. if (!mpt2sas_port) {
  443. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  444. ioc->name, __FILE__, __LINE__, __func__);
  445. return NULL;
  446. }
  447. INIT_LIST_HEAD(&mpt2sas_port->port_list);
  448. INIT_LIST_HEAD(&mpt2sas_port->phy_list);
  449. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  450. sas_node = _transport_sas_node_find_by_handle(ioc, parent_handle);
  451. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  452. if (!sas_node) {
  453. printk(MPT2SAS_ERR_FMT "%s: Could not find parent(0x%04x)!\n",
  454. ioc->name, __func__, parent_handle);
  455. goto out_fail;
  456. }
  457. mpt2sas_port->handle = parent_handle;
  458. mpt2sas_port->sas_address = sas_node->sas_address;
  459. if ((_transport_set_identify(ioc, handle,
  460. &mpt2sas_port->remote_identify))) {
  461. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  462. ioc->name, __FILE__, __LINE__, __func__);
  463. goto out_fail;
  464. }
  465. if (mpt2sas_port->remote_identify.device_type == SAS_PHY_UNUSED) {
  466. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  467. ioc->name, __FILE__, __LINE__, __func__);
  468. goto out_fail;
  469. }
  470. for (i = 0; i < sas_node->num_phys; i++) {
  471. if (sas_node->phy[i].remote_identify.sas_address !=
  472. mpt2sas_port->remote_identify.sas_address)
  473. continue;
  474. list_add_tail(&sas_node->phy[i].port_siblings,
  475. &mpt2sas_port->phy_list);
  476. mpt2sas_port->num_phys++;
  477. }
  478. if (!mpt2sas_port->num_phys) {
  479. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  480. ioc->name, __FILE__, __LINE__, __func__);
  481. goto out_fail;
  482. }
  483. port = sas_port_alloc_num(sas_node->parent_dev);
  484. if ((sas_port_add(port))) {
  485. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  486. ioc->name, __FILE__, __LINE__, __func__);
  487. goto out_fail;
  488. }
  489. list_for_each_entry(mpt2sas_phy, &mpt2sas_port->phy_list,
  490. port_siblings) {
  491. if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
  492. dev_printk(KERN_INFO, &port->dev, "add: handle(0x%04x)"
  493. ", sas_addr(0x%016llx), phy(%d)\n", handle,
  494. (unsigned long long)
  495. mpt2sas_port->remote_identify.sas_address,
  496. mpt2sas_phy->phy_id);
  497. sas_port_add_phy(port, mpt2sas_phy->phy);
  498. }
  499. mpt2sas_port->port = port;
  500. if (mpt2sas_port->remote_identify.device_type == SAS_END_DEVICE)
  501. rphy = sas_end_device_alloc(port);
  502. else
  503. rphy = sas_expander_alloc(port,
  504. mpt2sas_port->remote_identify.device_type);
  505. rphy->identify = mpt2sas_port->remote_identify;
  506. if ((sas_rphy_add(rphy))) {
  507. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  508. ioc->name, __FILE__, __LINE__, __func__);
  509. }
  510. if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
  511. dev_printk(KERN_INFO, &rphy->dev, "add: handle(0x%04x), "
  512. "sas_addr(0x%016llx)\n", handle,
  513. (unsigned long long)
  514. mpt2sas_port->remote_identify.sas_address);
  515. mpt2sas_port->rphy = rphy;
  516. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  517. list_add_tail(&mpt2sas_port->port_list, &sas_node->sas_port_list);
  518. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  519. /* fill in report manufacture */
  520. if (mpt2sas_port->remote_identify.device_type ==
  521. MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
  522. mpt2sas_port->remote_identify.device_type ==
  523. MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER)
  524. _transport_expander_report_manufacture(ioc,
  525. mpt2sas_port->remote_identify.sas_address,
  526. rphy_to_expander_device(rphy));
  527. return mpt2sas_port;
  528. out_fail:
  529. list_for_each_entry_safe(mpt2sas_phy, next, &mpt2sas_port->phy_list,
  530. port_siblings)
  531. list_del(&mpt2sas_phy->port_siblings);
  532. kfree(mpt2sas_port);
  533. return NULL;
  534. }
  535. /**
  536. * mpt2sas_transport_port_remove - remove port from the list
  537. * @ioc: per adapter object
  538. * @sas_address: sas address of attached device
  539. * @parent_handle: handle to the upstream parent(either hba or expander)
  540. * Context: This function will acquire ioc->sas_node_lock.
  541. *
  542. * Removing object and freeing associated memory from the
  543. * ioc->sas_port_list.
  544. *
  545. * Return nothing.
  546. */
  547. void
  548. mpt2sas_transport_port_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
  549. u16 parent_handle)
  550. {
  551. int i;
  552. unsigned long flags;
  553. struct _sas_port *mpt2sas_port, *next;
  554. struct _sas_node *sas_node;
  555. u8 found = 0;
  556. struct _sas_phy *mpt2sas_phy, *next_phy;
  557. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  558. sas_node = _transport_sas_node_find_by_handle(ioc, parent_handle);
  559. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  560. if (!sas_node)
  561. return;
  562. list_for_each_entry_safe(mpt2sas_port, next, &sas_node->sas_port_list,
  563. port_list) {
  564. if (mpt2sas_port->remote_identify.sas_address != sas_address)
  565. continue;
  566. found = 1;
  567. list_del(&mpt2sas_port->port_list);
  568. goto out;
  569. }
  570. out:
  571. if (!found)
  572. return;
  573. for (i = 0; i < sas_node->num_phys; i++) {
  574. if (sas_node->phy[i].remote_identify.sas_address == sas_address)
  575. memset(&sas_node->phy[i].remote_identify, 0 ,
  576. sizeof(struct sas_identify));
  577. }
  578. list_for_each_entry_safe(mpt2sas_phy, next_phy,
  579. &mpt2sas_port->phy_list, port_siblings) {
  580. if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
  581. dev_printk(KERN_INFO, &mpt2sas_port->port->dev,
  582. "remove: parent_handle(0x%04x), "
  583. "sas_addr(0x%016llx), phy(%d)\n", parent_handle,
  584. (unsigned long long)
  585. mpt2sas_port->remote_identify.sas_address,
  586. mpt2sas_phy->phy_id);
  587. sas_port_delete_phy(mpt2sas_port->port, mpt2sas_phy->phy);
  588. list_del(&mpt2sas_phy->port_siblings);
  589. }
  590. sas_port_delete(mpt2sas_port->port);
  591. kfree(mpt2sas_port);
  592. }
  593. /**
  594. * mpt2sas_transport_add_host_phy - report sas_host phy to transport
  595. * @ioc: per adapter object
  596. * @mpt2sas_phy: mpt2sas per phy object
  597. * @phy_pg0: sas phy page 0
  598. * @parent_dev: parent device class object
  599. *
  600. * Returns 0 for success, non-zero for failure.
  601. */
  602. int
  603. mpt2sas_transport_add_host_phy(struct MPT2SAS_ADAPTER *ioc, struct _sas_phy
  604. *mpt2sas_phy, Mpi2SasPhyPage0_t phy_pg0, struct device *parent_dev)
  605. {
  606. struct sas_phy *phy;
  607. int phy_index = mpt2sas_phy->phy_id;
  608. INIT_LIST_HEAD(&mpt2sas_phy->port_siblings);
  609. phy = sas_phy_alloc(parent_dev, phy_index);
  610. if (!phy) {
  611. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  612. ioc->name, __FILE__, __LINE__, __func__);
  613. return -1;
  614. }
  615. if ((_transport_set_identify(ioc, mpt2sas_phy->handle,
  616. &mpt2sas_phy->identify))) {
  617. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  618. ioc->name, __FILE__, __LINE__, __func__);
  619. return -1;
  620. }
  621. phy->identify = mpt2sas_phy->identify;
  622. mpt2sas_phy->attached_handle = le16_to_cpu(phy_pg0.AttachedDevHandle);
  623. if (mpt2sas_phy->attached_handle)
  624. _transport_set_identify(ioc, mpt2sas_phy->attached_handle,
  625. &mpt2sas_phy->remote_identify);
  626. phy->identify.phy_identifier = mpt2sas_phy->phy_id;
  627. phy->negotiated_linkrate = _transport_convert_phy_link_rate(
  628. phy_pg0.NegotiatedLinkRate & MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
  629. phy->minimum_linkrate_hw = _transport_convert_phy_link_rate(
  630. phy_pg0.HwLinkRate & MPI2_SAS_HWRATE_MIN_RATE_MASK);
  631. phy->maximum_linkrate_hw = _transport_convert_phy_link_rate(
  632. phy_pg0.HwLinkRate >> 4);
  633. phy->minimum_linkrate = _transport_convert_phy_link_rate(
  634. phy_pg0.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
  635. phy->maximum_linkrate = _transport_convert_phy_link_rate(
  636. phy_pg0.ProgrammedLinkRate >> 4);
  637. if ((sas_phy_add(phy))) {
  638. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  639. ioc->name, __FILE__, __LINE__, __func__);
  640. sas_phy_free(phy);
  641. return -1;
  642. }
  643. if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
  644. dev_printk(KERN_INFO, &phy->dev,
  645. "add: handle(0x%04x), sas_addr(0x%016llx)\n"
  646. "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
  647. mpt2sas_phy->handle, (unsigned long long)
  648. mpt2sas_phy->identify.sas_address,
  649. mpt2sas_phy->attached_handle,
  650. (unsigned long long)
  651. mpt2sas_phy->remote_identify.sas_address);
  652. mpt2sas_phy->phy = phy;
  653. return 0;
  654. }
  655. /**
  656. * mpt2sas_transport_add_expander_phy - report expander phy to transport
  657. * @ioc: per adapter object
  658. * @mpt2sas_phy: mpt2sas per phy object
  659. * @expander_pg1: expander page 1
  660. * @parent_dev: parent device class object
  661. *
  662. * Returns 0 for success, non-zero for failure.
  663. */
  664. int
  665. mpt2sas_transport_add_expander_phy(struct MPT2SAS_ADAPTER *ioc, struct _sas_phy
  666. *mpt2sas_phy, Mpi2ExpanderPage1_t expander_pg1, struct device *parent_dev)
  667. {
  668. struct sas_phy *phy;
  669. int phy_index = mpt2sas_phy->phy_id;
  670. INIT_LIST_HEAD(&mpt2sas_phy->port_siblings);
  671. phy = sas_phy_alloc(parent_dev, phy_index);
  672. if (!phy) {
  673. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  674. ioc->name, __FILE__, __LINE__, __func__);
  675. return -1;
  676. }
  677. if ((_transport_set_identify(ioc, mpt2sas_phy->handle,
  678. &mpt2sas_phy->identify))) {
  679. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  680. ioc->name, __FILE__, __LINE__, __func__);
  681. return -1;
  682. }
  683. phy->identify = mpt2sas_phy->identify;
  684. mpt2sas_phy->attached_handle =
  685. le16_to_cpu(expander_pg1.AttachedDevHandle);
  686. if (mpt2sas_phy->attached_handle)
  687. _transport_set_identify(ioc, mpt2sas_phy->attached_handle,
  688. &mpt2sas_phy->remote_identify);
  689. phy->identify.phy_identifier = mpt2sas_phy->phy_id;
  690. phy->negotiated_linkrate = _transport_convert_phy_link_rate(
  691. expander_pg1.NegotiatedLinkRate &
  692. MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
  693. phy->minimum_linkrate_hw = _transport_convert_phy_link_rate(
  694. expander_pg1.HwLinkRate & MPI2_SAS_HWRATE_MIN_RATE_MASK);
  695. phy->maximum_linkrate_hw = _transport_convert_phy_link_rate(
  696. expander_pg1.HwLinkRate >> 4);
  697. phy->minimum_linkrate = _transport_convert_phy_link_rate(
  698. expander_pg1.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
  699. phy->maximum_linkrate = _transport_convert_phy_link_rate(
  700. expander_pg1.ProgrammedLinkRate >> 4);
  701. if ((sas_phy_add(phy))) {
  702. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  703. ioc->name, __FILE__, __LINE__, __func__);
  704. sas_phy_free(phy);
  705. return -1;
  706. }
  707. if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
  708. dev_printk(KERN_INFO, &phy->dev,
  709. "add: handle(0x%04x), sas_addr(0x%016llx)\n"
  710. "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
  711. mpt2sas_phy->handle, (unsigned long long)
  712. mpt2sas_phy->identify.sas_address,
  713. mpt2sas_phy->attached_handle,
  714. (unsigned long long)
  715. mpt2sas_phy->remote_identify.sas_address);
  716. mpt2sas_phy->phy = phy;
  717. return 0;
  718. }
  719. /**
  720. * mpt2sas_transport_update_links - refreshing phy link changes
  721. * @ioc: per adapter object
  722. * @handle: handle to sas_host or expander
  723. * @attached_handle: attached device handle
  724. * @phy_numberv: phy number
  725. * @link_rate: new link rate
  726. *
  727. * Returns nothing.
  728. */
  729. void
  730. mpt2sas_transport_update_links(struct MPT2SAS_ADAPTER *ioc,
  731. u16 handle, u16 attached_handle, u8 phy_number, u8 link_rate)
  732. {
  733. unsigned long flags;
  734. struct _sas_node *sas_node;
  735. struct _sas_phy *mpt2sas_phy;
  736. if (ioc->shost_recovery) {
  737. printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
  738. __func__, ioc->name);
  739. return;
  740. }
  741. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  742. sas_node = _transport_sas_node_find_by_handle(ioc, handle);
  743. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  744. if (!sas_node)
  745. return;
  746. mpt2sas_phy = &sas_node->phy[phy_number];
  747. mpt2sas_phy->attached_handle = attached_handle;
  748. if (attached_handle && (link_rate >= MPI2_SAS_NEG_LINK_RATE_1_5))
  749. _transport_set_identify(ioc, mpt2sas_phy->attached_handle,
  750. &mpt2sas_phy->remote_identify);
  751. else
  752. memset(&mpt2sas_phy->remote_identify, 0 , sizeof(struct
  753. sas_identify));
  754. if (mpt2sas_phy->phy)
  755. mpt2sas_phy->phy->negotiated_linkrate =
  756. _transport_convert_phy_link_rate(link_rate);
  757. if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
  758. dev_printk(KERN_INFO, &mpt2sas_phy->phy->dev,
  759. "refresh: handle(0x%04x), sas_addr(0x%016llx),\n"
  760. "\tlink_rate(0x%02x), phy(%d)\n"
  761. "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
  762. handle, (unsigned long long)
  763. mpt2sas_phy->identify.sas_address, link_rate,
  764. phy_number, attached_handle,
  765. (unsigned long long)
  766. mpt2sas_phy->remote_identify.sas_address);
  767. }
  768. static inline void *
  769. phy_to_ioc(struct sas_phy *phy)
  770. {
  771. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
  772. return shost_priv(shost);
  773. }
  774. static inline void *
  775. rphy_to_ioc(struct sas_rphy *rphy)
  776. {
  777. struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
  778. return shost_priv(shost);
  779. }
  780. /**
  781. * _transport_get_linkerrors -
  782. * @phy: The sas phy object
  783. *
  784. * Only support sas_host direct attached phys.
  785. * Returns 0 for success, non-zero for failure.
  786. *
  787. */
  788. static int
  789. _transport_get_linkerrors(struct sas_phy *phy)
  790. {
  791. struct MPT2SAS_ADAPTER *ioc = phy_to_ioc(phy);
  792. struct _sas_phy *mpt2sas_phy;
  793. Mpi2ConfigReply_t mpi_reply;
  794. Mpi2SasPhyPage1_t phy_pg1;
  795. int i;
  796. for (i = 0, mpt2sas_phy = NULL; i < ioc->sas_hba.num_phys &&
  797. !mpt2sas_phy; i++) {
  798. if (ioc->sas_hba.phy[i].phy != phy)
  799. continue;
  800. mpt2sas_phy = &ioc->sas_hba.phy[i];
  801. }
  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. int i;
  883. for (i = 0, mpt2sas_phy = NULL; i < ioc->sas_hba.num_phys &&
  884. !mpt2sas_phy; i++) {
  885. if (ioc->sas_hba.phy[i].phy != phy)
  886. continue;
  887. mpt2sas_phy = &ioc->sas_hba.phy[i];
  888. }
  889. if (!mpt2sas_phy) /* this phy not on sas_host */
  890. return -EINVAL;
  891. memset(&mpi_request, 0, sizeof(Mpi2SasIoUnitControlReply_t));
  892. mpi_request.Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
  893. mpi_request.Operation = hard_reset ?
  894. MPI2_SAS_OP_PHY_HARD_RESET : MPI2_SAS_OP_PHY_LINK_RESET;
  895. mpi_request.PhyNum = mpt2sas_phy->phy_id;
  896. if ((mpt2sas_base_sas_iounit_control(ioc, &mpi_reply, &mpi_request))) {
  897. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  898. ioc->name, __FILE__, __LINE__, __func__);
  899. return -ENXIO;
  900. }
  901. if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo)
  902. printk(MPT2SAS_INFO_FMT "phy(%d), ioc_status"
  903. "(0x%04x), loginfo(0x%08x)\n", ioc->name,
  904. mpt2sas_phy->phy_id,
  905. le16_to_cpu(mpi_reply.IOCStatus),
  906. le32_to_cpu(mpi_reply.IOCLogInfo));
  907. return 0;
  908. }
  909. /**
  910. * _transport_smp_handler - transport portal for smp passthru
  911. * @shost: shost object
  912. * @rphy: sas transport rphy object
  913. * @req:
  914. *
  915. * This used primarily for smp_utils.
  916. * Example:
  917. * smp_rep_general /sys/class/bsg/expander-5:0
  918. */
  919. static int
  920. _transport_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
  921. struct request *req)
  922. {
  923. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  924. Mpi2SmpPassthroughRequest_t *mpi_request;
  925. Mpi2SmpPassthroughReply_t *mpi_reply;
  926. int rc;
  927. u16 smid;
  928. u32 ioc_state;
  929. unsigned long timeleft;
  930. void *psge;
  931. u32 sgl_flags;
  932. u8 issue_reset = 0;
  933. dma_addr_t dma_addr_in = 0;
  934. dma_addr_t dma_addr_out = 0;
  935. u16 wait_state_count;
  936. struct request *rsp = req->next_rq;
  937. if (!rsp) {
  938. printk(MPT2SAS_ERR_FMT "%s: the smp response space is "
  939. "missing\n", ioc->name, __func__);
  940. return -EINVAL;
  941. }
  942. /* do we need to support multiple segments? */
  943. if (req->bio->bi_vcnt > 1 || rsp->bio->bi_vcnt > 1) {
  944. printk(MPT2SAS_ERR_FMT "%s: multiple segments req %u %u, "
  945. "rsp %u %u\n", ioc->name, __func__, req->bio->bi_vcnt,
  946. blk_rq_bytes(req), rsp->bio->bi_vcnt, blk_rq_bytes(rsp));
  947. return -EINVAL;
  948. }
  949. if (ioc->shost_recovery) {
  950. printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
  951. __func__, ioc->name);
  952. return -EFAULT;
  953. }
  954. rc = mutex_lock_interruptible(&ioc->transport_cmds.mutex);
  955. if (rc)
  956. return rc;
  957. if (ioc->transport_cmds.status != MPT2_CMD_NOT_USED) {
  958. printk(MPT2SAS_ERR_FMT "%s: transport_cmds in use\n", ioc->name,
  959. __func__);
  960. rc = -EAGAIN;
  961. goto out;
  962. }
  963. ioc->transport_cmds.status = MPT2_CMD_PENDING;
  964. wait_state_count = 0;
  965. ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
  966. while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  967. if (wait_state_count++ == 10) {
  968. printk(MPT2SAS_ERR_FMT
  969. "%s: failed due to ioc not operational\n",
  970. ioc->name, __func__);
  971. rc = -EFAULT;
  972. goto out;
  973. }
  974. ssleep(1);
  975. ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
  976. printk(MPT2SAS_INFO_FMT "%s: waiting for "
  977. "operational state(count=%d)\n", ioc->name,
  978. __func__, wait_state_count);
  979. }
  980. if (wait_state_count)
  981. printk(MPT2SAS_INFO_FMT "%s: ioc is operational\n",
  982. ioc->name, __func__);
  983. smid = mpt2sas_base_get_smid(ioc, ioc->transport_cb_idx);
  984. if (!smid) {
  985. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  986. ioc->name, __func__);
  987. rc = -EAGAIN;
  988. goto out;
  989. }
  990. rc = 0;
  991. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  992. ioc->transport_cmds.smid = smid;
  993. memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
  994. mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
  995. mpi_request->PhysicalPort = 0xFF;
  996. mpi_request->VF_ID = 0; /* TODO */
  997. mpi_request->VP_ID = 0;
  998. *((u64 *)&mpi_request->SASAddress) = (rphy) ?
  999. cpu_to_le64(rphy->identify.sas_address) :
  1000. cpu_to_le64(ioc->sas_hba.sas_address);
  1001. mpi_request->RequestDataLength = cpu_to_le16(blk_rq_bytes(req) - 4);
  1002. psge = &mpi_request->SGL;
  1003. /* WRITE sgel first */
  1004. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  1005. MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
  1006. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  1007. dma_addr_out = pci_map_single(ioc->pdev, bio_data(req->bio),
  1008. blk_rq_bytes(req), PCI_DMA_BIDIRECTIONAL);
  1009. if (!dma_addr_out) {
  1010. mpt2sas_base_free_smid(ioc, le16_to_cpu(smid));
  1011. goto unmap;
  1012. }
  1013. ioc->base_add_sg_single(psge, sgl_flags | (blk_rq_bytes(req) - 4),
  1014. dma_addr_out);
  1015. /* incr sgel */
  1016. psge += ioc->sge_size;
  1017. /* READ sgel last */
  1018. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  1019. MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
  1020. MPI2_SGE_FLAGS_END_OF_LIST);
  1021. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  1022. dma_addr_in = pci_map_single(ioc->pdev, bio_data(rsp->bio),
  1023. blk_rq_bytes(rsp), PCI_DMA_BIDIRECTIONAL);
  1024. if (!dma_addr_in) {
  1025. mpt2sas_base_free_smid(ioc, le16_to_cpu(smid));
  1026. goto unmap;
  1027. }
  1028. ioc->base_add_sg_single(psge, sgl_flags | (blk_rq_bytes(rsp) + 4),
  1029. dma_addr_in);
  1030. dtransportprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s - "
  1031. "sending smp request\n", ioc->name, __func__));
  1032. mpt2sas_base_put_smid_default(ioc, smid);
  1033. timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done,
  1034. 10*HZ);
  1035. if (!(ioc->transport_cmds.status & MPT2_CMD_COMPLETE)) {
  1036. printk(MPT2SAS_ERR_FMT "%s : timeout\n",
  1037. __func__, ioc->name);
  1038. _debug_dump_mf(mpi_request,
  1039. sizeof(Mpi2SmpPassthroughRequest_t)/4);
  1040. if (!(ioc->transport_cmds.status & MPT2_CMD_RESET))
  1041. issue_reset = 1;
  1042. goto issue_host_reset;
  1043. }
  1044. dtransportprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s - "
  1045. "complete\n", ioc->name, __func__));
  1046. if (ioc->transport_cmds.status & MPT2_CMD_REPLY_VALID) {
  1047. mpi_reply = ioc->transport_cmds.reply;
  1048. dtransportprintk(ioc, printk(MPT2SAS_DEBUG_FMT
  1049. "%s - reply data transfer size(%d)\n",
  1050. ioc->name, __func__,
  1051. le16_to_cpu(mpi_reply->ResponseDataLength)));
  1052. memcpy(req->sense, mpi_reply, sizeof(*mpi_reply));
  1053. req->sense_len = sizeof(*mpi_reply);
  1054. req->resid_len = 0;
  1055. rsp->resid_len -= mpi_reply->ResponseDataLength;
  1056. } else {
  1057. dtransportprintk(ioc, printk(MPT2SAS_DEBUG_FMT
  1058. "%s - no reply\n", ioc->name, __func__));
  1059. rc = -ENXIO;
  1060. }
  1061. issue_host_reset:
  1062. if (issue_reset) {
  1063. mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  1064. FORCE_BIG_HAMMER);
  1065. rc = -ETIMEDOUT;
  1066. }
  1067. unmap:
  1068. if (dma_addr_out)
  1069. pci_unmap_single(ioc->pdev, dma_addr_out, blk_rq_bytes(req),
  1070. PCI_DMA_BIDIRECTIONAL);
  1071. if (dma_addr_in)
  1072. pci_unmap_single(ioc->pdev, dma_addr_in, blk_rq_bytes(rsp),
  1073. PCI_DMA_BIDIRECTIONAL);
  1074. out:
  1075. ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
  1076. mutex_unlock(&ioc->transport_cmds.mutex);
  1077. return rc;
  1078. }
  1079. struct sas_function_template mpt2sas_transport_functions = {
  1080. .get_linkerrors = _transport_get_linkerrors,
  1081. .get_enclosure_identifier = _transport_get_enclosure_identifier,
  1082. .get_bay_identifier = _transport_get_bay_identifier,
  1083. .phy_reset = _transport_phy_reset,
  1084. .smp_handler = _transport_smp_handler,
  1085. };
  1086. struct scsi_transport_template *mpt2sas_transport_template;