mpt2sas_transport.c 37 KB

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