mpt3sas_transport.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129
  1. /*
  2. * SAS Transport Layer for MPT (Message Passing Technology) based controllers
  3. *
  4. * This code is based on drivers/scsi/mpt3sas/mpt3sas_transport.c
  5. * Copyright (C) 2012-2014 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 "mpt3sas_base.h"
  56. /**
  57. * _transport_sas_node_find_by_sas_address - sas node search
  58. * @ioc: per adapter object
  59. * @sas_address: sas address of expander or sas host
  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_sas_address(struct MPT3SAS_ADAPTER *ioc,
  67. u64 sas_address)
  68. {
  69. if (ioc->sas_hba.sas_address == sas_address)
  70. return &ioc->sas_hba;
  71. else
  72. return mpt3sas_scsih_expander_find_by_sas_address(ioc,
  73. sas_address);
  74. }
  75. /**
  76. * _transport_convert_phy_link_rate -
  77. * @link_rate: link rate returned from mpt firmware
  78. *
  79. * Convert link_rate from mpi fusion into sas_transport form.
  80. */
  81. static enum sas_linkrate
  82. _transport_convert_phy_link_rate(u8 link_rate)
  83. {
  84. enum sas_linkrate rc;
  85. switch (link_rate) {
  86. case MPI2_SAS_NEG_LINK_RATE_1_5:
  87. rc = SAS_LINK_RATE_1_5_GBPS;
  88. break;
  89. case MPI2_SAS_NEG_LINK_RATE_3_0:
  90. rc = SAS_LINK_RATE_3_0_GBPS;
  91. break;
  92. case MPI2_SAS_NEG_LINK_RATE_6_0:
  93. rc = SAS_LINK_RATE_6_0_GBPS;
  94. break;
  95. case MPI25_SAS_NEG_LINK_RATE_12_0:
  96. rc = SAS_LINK_RATE_12_0_GBPS;
  97. break;
  98. case MPI2_SAS_NEG_LINK_RATE_PHY_DISABLED:
  99. rc = SAS_PHY_DISABLED;
  100. break;
  101. case MPI2_SAS_NEG_LINK_RATE_NEGOTIATION_FAILED:
  102. rc = SAS_LINK_RATE_FAILED;
  103. break;
  104. case MPI2_SAS_NEG_LINK_RATE_PORT_SELECTOR:
  105. rc = SAS_SATA_PORT_SELECTOR;
  106. break;
  107. case MPI2_SAS_NEG_LINK_RATE_SMP_RESET_IN_PROGRESS:
  108. rc = SAS_PHY_RESET_IN_PROGRESS;
  109. break;
  110. default:
  111. case MPI2_SAS_NEG_LINK_RATE_SATA_OOB_COMPLETE:
  112. case MPI2_SAS_NEG_LINK_RATE_UNKNOWN_LINK_RATE:
  113. rc = SAS_LINK_RATE_UNKNOWN;
  114. break;
  115. }
  116. return rc;
  117. }
  118. /**
  119. * _transport_set_identify - set identify for phys and end devices
  120. * @ioc: per adapter object
  121. * @handle: device handle
  122. * @identify: sas identify info
  123. *
  124. * Populates sas identify info.
  125. *
  126. * Returns 0 for success, non-zero for failure.
  127. */
  128. static int
  129. _transport_set_identify(struct MPT3SAS_ADAPTER *ioc, u16 handle,
  130. struct sas_identify *identify)
  131. {
  132. Mpi2SasDevicePage0_t sas_device_pg0;
  133. Mpi2ConfigReply_t mpi_reply;
  134. u32 device_info;
  135. u32 ioc_status;
  136. if (ioc->shost_recovery || ioc->pci_error_recovery) {
  137. pr_info(MPT3SAS_FMT "%s: host reset in progress!\n",
  138. __func__, ioc->name);
  139. return -EFAULT;
  140. }
  141. if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
  142. MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
  143. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  144. ioc->name, __FILE__, __LINE__, __func__);
  145. return -ENXIO;
  146. }
  147. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  148. MPI2_IOCSTATUS_MASK;
  149. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  150. pr_err(MPT3SAS_FMT
  151. "handle(0x%04x), ioc_status(0x%04x)\nfailure at %s:%d/%s()!\n",
  152. ioc->name, handle, ioc_status,
  153. __FILE__, __LINE__, __func__);
  154. return -EIO;
  155. }
  156. memset(identify, 0, sizeof(struct sas_identify));
  157. device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
  158. /* sas_address */
  159. identify->sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
  160. /* phy number of the parent device this device is linked to */
  161. identify->phy_identifier = sas_device_pg0.PhyNum;
  162. /* device_type */
  163. switch (device_info & MPI2_SAS_DEVICE_INFO_MASK_DEVICE_TYPE) {
  164. case MPI2_SAS_DEVICE_INFO_NO_DEVICE:
  165. identify->device_type = SAS_PHY_UNUSED;
  166. break;
  167. case MPI2_SAS_DEVICE_INFO_END_DEVICE:
  168. identify->device_type = SAS_END_DEVICE;
  169. break;
  170. case MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER:
  171. identify->device_type = SAS_EDGE_EXPANDER_DEVICE;
  172. break;
  173. case MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER:
  174. identify->device_type = SAS_FANOUT_EXPANDER_DEVICE;
  175. break;
  176. }
  177. /* initiator_port_protocols */
  178. if (device_info & MPI2_SAS_DEVICE_INFO_SSP_INITIATOR)
  179. identify->initiator_port_protocols |= SAS_PROTOCOL_SSP;
  180. if (device_info & MPI2_SAS_DEVICE_INFO_STP_INITIATOR)
  181. identify->initiator_port_protocols |= SAS_PROTOCOL_STP;
  182. if (device_info & MPI2_SAS_DEVICE_INFO_SMP_INITIATOR)
  183. identify->initiator_port_protocols |= SAS_PROTOCOL_SMP;
  184. if (device_info & MPI2_SAS_DEVICE_INFO_SATA_HOST)
  185. identify->initiator_port_protocols |= SAS_PROTOCOL_SATA;
  186. /* target_port_protocols */
  187. if (device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET)
  188. identify->target_port_protocols |= SAS_PROTOCOL_SSP;
  189. if (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET)
  190. identify->target_port_protocols |= SAS_PROTOCOL_STP;
  191. if (device_info & MPI2_SAS_DEVICE_INFO_SMP_TARGET)
  192. identify->target_port_protocols |= SAS_PROTOCOL_SMP;
  193. if (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
  194. identify->target_port_protocols |= SAS_PROTOCOL_SATA;
  195. return 0;
  196. }
  197. /**
  198. * mpt3sas_transport_done - internal transport layer callback handler.
  199. * @ioc: per adapter object
  200. * @smid: system request message index
  201. * @msix_index: MSIX table index supplied by the OS
  202. * @reply: reply message frame(lower 32bit addr)
  203. *
  204. * Callback handler when sending internal generated transport cmds.
  205. * The callback index passed is `ioc->transport_cb_idx`
  206. *
  207. * Return 1 meaning mf should be freed from _base_interrupt
  208. * 0 means the mf is freed from this function.
  209. */
  210. u8
  211. mpt3sas_transport_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
  212. u32 reply)
  213. {
  214. MPI2DefaultReply_t *mpi_reply;
  215. mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
  216. if (ioc->transport_cmds.status == MPT3_CMD_NOT_USED)
  217. return 1;
  218. if (ioc->transport_cmds.smid != smid)
  219. return 1;
  220. ioc->transport_cmds.status |= MPT3_CMD_COMPLETE;
  221. if (mpi_reply) {
  222. memcpy(ioc->transport_cmds.reply, mpi_reply,
  223. mpi_reply->MsgLength*4);
  224. ioc->transport_cmds.status |= MPT3_CMD_REPLY_VALID;
  225. }
  226. ioc->transport_cmds.status &= ~MPT3_CMD_PENDING;
  227. complete(&ioc->transport_cmds.done);
  228. return 1;
  229. }
  230. /* report manufacture request structure */
  231. struct rep_manu_request {
  232. u8 smp_frame_type;
  233. u8 function;
  234. u8 reserved;
  235. u8 request_length;
  236. };
  237. /* report manufacture reply structure */
  238. struct rep_manu_reply {
  239. u8 smp_frame_type; /* 0x41 */
  240. u8 function; /* 0x01 */
  241. u8 function_result;
  242. u8 response_length;
  243. u16 expander_change_count;
  244. u8 reserved0[2];
  245. u8 sas_format;
  246. u8 reserved2[3];
  247. u8 vendor_id[SAS_EXPANDER_VENDOR_ID_LEN];
  248. u8 product_id[SAS_EXPANDER_PRODUCT_ID_LEN];
  249. u8 product_rev[SAS_EXPANDER_PRODUCT_REV_LEN];
  250. u8 component_vendor_id[SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN];
  251. u16 component_id;
  252. u8 component_revision_id;
  253. u8 reserved3;
  254. u8 vendor_specific[8];
  255. };
  256. /**
  257. * transport_expander_report_manufacture - obtain SMP report_manufacture
  258. * @ioc: per adapter object
  259. * @sas_address: expander sas address
  260. * @edev: the sas_expander_device object
  261. *
  262. * Fills in the sas_expander_device object when SMP port is created.
  263. *
  264. * Returns 0 for success, non-zero for failure.
  265. */
  266. static int
  267. _transport_expander_report_manufacture(struct MPT3SAS_ADAPTER *ioc,
  268. u64 sas_address, struct sas_expander_device *edev)
  269. {
  270. Mpi2SmpPassthroughRequest_t *mpi_request;
  271. Mpi2SmpPassthroughReply_t *mpi_reply;
  272. struct rep_manu_reply *manufacture_reply;
  273. struct rep_manu_request *manufacture_request;
  274. int rc;
  275. u16 smid;
  276. u32 ioc_state;
  277. unsigned long timeleft;
  278. void *psge;
  279. u8 issue_reset = 0;
  280. void *data_out = NULL;
  281. dma_addr_t data_out_dma;
  282. dma_addr_t data_in_dma;
  283. size_t data_in_sz;
  284. size_t data_out_sz;
  285. u16 wait_state_count;
  286. if (ioc->shost_recovery || ioc->pci_error_recovery) {
  287. pr_info(MPT3SAS_FMT "%s: host reset in progress!\n",
  288. __func__, ioc->name);
  289. return -EFAULT;
  290. }
  291. mutex_lock(&ioc->transport_cmds.mutex);
  292. if (ioc->transport_cmds.status != MPT3_CMD_NOT_USED) {
  293. pr_err(MPT3SAS_FMT "%s: transport_cmds in use\n",
  294. ioc->name, __func__);
  295. rc = -EAGAIN;
  296. goto out;
  297. }
  298. ioc->transport_cmds.status = MPT3_CMD_PENDING;
  299. wait_state_count = 0;
  300. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  301. while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  302. if (wait_state_count++ == 10) {
  303. pr_err(MPT3SAS_FMT
  304. "%s: failed due to ioc not operational\n",
  305. ioc->name, __func__);
  306. rc = -EFAULT;
  307. goto out;
  308. }
  309. ssleep(1);
  310. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  311. pr_info(MPT3SAS_FMT
  312. "%s: waiting for operational state(count=%d)\n",
  313. ioc->name, __func__, wait_state_count);
  314. }
  315. if (wait_state_count)
  316. pr_info(MPT3SAS_FMT "%s: ioc is operational\n",
  317. ioc->name, __func__);
  318. smid = mpt3sas_base_get_smid(ioc, ioc->transport_cb_idx);
  319. if (!smid) {
  320. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  321. ioc->name, __func__);
  322. rc = -EAGAIN;
  323. goto out;
  324. }
  325. rc = 0;
  326. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  327. ioc->transport_cmds.smid = smid;
  328. data_out_sz = sizeof(struct rep_manu_request);
  329. data_in_sz = sizeof(struct rep_manu_reply);
  330. data_out = pci_alloc_consistent(ioc->pdev, data_out_sz + data_in_sz,
  331. &data_out_dma);
  332. if (!data_out) {
  333. pr_err("failure at %s:%d/%s()!\n", __FILE__,
  334. __LINE__, __func__);
  335. rc = -ENOMEM;
  336. mpt3sas_base_free_smid(ioc, smid);
  337. goto out;
  338. }
  339. data_in_dma = data_out_dma + sizeof(struct rep_manu_request);
  340. manufacture_request = data_out;
  341. manufacture_request->smp_frame_type = 0x40;
  342. manufacture_request->function = 1;
  343. manufacture_request->reserved = 0;
  344. manufacture_request->request_length = 0;
  345. memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
  346. mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
  347. mpi_request->PhysicalPort = 0xFF;
  348. mpi_request->SASAddress = cpu_to_le64(sas_address);
  349. mpi_request->RequestDataLength = cpu_to_le16(data_out_sz);
  350. psge = &mpi_request->SGL;
  351. ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
  352. data_in_sz);
  353. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  354. "report_manufacture - send to sas_addr(0x%016llx)\n",
  355. ioc->name, (unsigned long long)sas_address));
  356. init_completion(&ioc->transport_cmds.done);
  357. mpt3sas_base_put_smid_default(ioc, smid);
  358. timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done,
  359. 10*HZ);
  360. if (!(ioc->transport_cmds.status & MPT3_CMD_COMPLETE)) {
  361. pr_err(MPT3SAS_FMT "%s: timeout\n",
  362. ioc->name, __func__);
  363. _debug_dump_mf(mpi_request,
  364. sizeof(Mpi2SmpPassthroughRequest_t)/4);
  365. if (!(ioc->transport_cmds.status & MPT3_CMD_RESET))
  366. issue_reset = 1;
  367. goto issue_host_reset;
  368. }
  369. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  370. "report_manufacture - complete\n", ioc->name));
  371. if (ioc->transport_cmds.status & MPT3_CMD_REPLY_VALID) {
  372. u8 *tmp;
  373. mpi_reply = ioc->transport_cmds.reply;
  374. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  375. "report_manufacture - reply data transfer size(%d)\n",
  376. ioc->name, le16_to_cpu(mpi_reply->ResponseDataLength)));
  377. if (le16_to_cpu(mpi_reply->ResponseDataLength) !=
  378. sizeof(struct rep_manu_reply))
  379. goto out;
  380. manufacture_reply = data_out + sizeof(struct rep_manu_request);
  381. strncpy(edev->vendor_id, manufacture_reply->vendor_id,
  382. SAS_EXPANDER_VENDOR_ID_LEN);
  383. strncpy(edev->product_id, manufacture_reply->product_id,
  384. SAS_EXPANDER_PRODUCT_ID_LEN);
  385. strncpy(edev->product_rev, manufacture_reply->product_rev,
  386. SAS_EXPANDER_PRODUCT_REV_LEN);
  387. edev->level = manufacture_reply->sas_format & 1;
  388. if (edev->level) {
  389. strncpy(edev->component_vendor_id,
  390. manufacture_reply->component_vendor_id,
  391. SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN);
  392. tmp = (u8 *)&manufacture_reply->component_id;
  393. edev->component_id = tmp[0] << 8 | tmp[1];
  394. edev->component_revision_id =
  395. manufacture_reply->component_revision_id;
  396. }
  397. } else
  398. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  399. "report_manufacture - no reply\n", ioc->name));
  400. issue_host_reset:
  401. if (issue_reset)
  402. mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  403. FORCE_BIG_HAMMER);
  404. out:
  405. ioc->transport_cmds.status = MPT3_CMD_NOT_USED;
  406. if (data_out)
  407. pci_free_consistent(ioc->pdev, data_out_sz + data_in_sz,
  408. data_out, data_out_dma);
  409. mutex_unlock(&ioc->transport_cmds.mutex);
  410. return rc;
  411. }
  412. /**
  413. * _transport_delete_port - helper function to removing a port
  414. * @ioc: per adapter object
  415. * @mpt3sas_port: mpt3sas per port object
  416. *
  417. * Returns nothing.
  418. */
  419. static void
  420. _transport_delete_port(struct MPT3SAS_ADAPTER *ioc,
  421. struct _sas_port *mpt3sas_port)
  422. {
  423. u64 sas_address = mpt3sas_port->remote_identify.sas_address;
  424. enum sas_device_type device_type =
  425. mpt3sas_port->remote_identify.device_type;
  426. dev_printk(KERN_INFO, &mpt3sas_port->port->dev,
  427. "remove: sas_addr(0x%016llx)\n",
  428. (unsigned long long) sas_address);
  429. ioc->logging_level |= MPT_DEBUG_TRANSPORT;
  430. if (device_type == SAS_END_DEVICE)
  431. mpt3sas_device_remove_by_sas_address(ioc, sas_address);
  432. else if (device_type == SAS_EDGE_EXPANDER_DEVICE ||
  433. device_type == SAS_FANOUT_EXPANDER_DEVICE)
  434. mpt3sas_expander_remove(ioc, sas_address);
  435. ioc->logging_level &= ~MPT_DEBUG_TRANSPORT;
  436. }
  437. /**
  438. * _transport_delete_phy - helper function to removing single phy from port
  439. * @ioc: per adapter object
  440. * @mpt3sas_port: mpt3sas per port object
  441. * @mpt3sas_phy: mpt3sas per phy object
  442. *
  443. * Returns nothing.
  444. */
  445. static void
  446. _transport_delete_phy(struct MPT3SAS_ADAPTER *ioc,
  447. struct _sas_port *mpt3sas_port, struct _sas_phy *mpt3sas_phy)
  448. {
  449. u64 sas_address = mpt3sas_port->remote_identify.sas_address;
  450. dev_printk(KERN_INFO, &mpt3sas_phy->phy->dev,
  451. "remove: sas_addr(0x%016llx), phy(%d)\n",
  452. (unsigned long long) sas_address, mpt3sas_phy->phy_id);
  453. list_del(&mpt3sas_phy->port_siblings);
  454. mpt3sas_port->num_phys--;
  455. sas_port_delete_phy(mpt3sas_port->port, mpt3sas_phy->phy);
  456. mpt3sas_phy->phy_belongs_to_port = 0;
  457. }
  458. /**
  459. * _transport_add_phy - helper function to adding single phy to port
  460. * @ioc: per adapter object
  461. * @mpt3sas_port: mpt3sas per port object
  462. * @mpt3sas_phy: mpt3sas per phy object
  463. *
  464. * Returns nothing.
  465. */
  466. static void
  467. _transport_add_phy(struct MPT3SAS_ADAPTER *ioc, struct _sas_port *mpt3sas_port,
  468. struct _sas_phy *mpt3sas_phy)
  469. {
  470. u64 sas_address = mpt3sas_port->remote_identify.sas_address;
  471. dev_printk(KERN_INFO, &mpt3sas_phy->phy->dev,
  472. "add: sas_addr(0x%016llx), phy(%d)\n", (unsigned long long)
  473. sas_address, mpt3sas_phy->phy_id);
  474. list_add_tail(&mpt3sas_phy->port_siblings, &mpt3sas_port->phy_list);
  475. mpt3sas_port->num_phys++;
  476. sas_port_add_phy(mpt3sas_port->port, mpt3sas_phy->phy);
  477. mpt3sas_phy->phy_belongs_to_port = 1;
  478. }
  479. /**
  480. * _transport_add_phy_to_an_existing_port - adding new phy to existing port
  481. * @ioc: per adapter object
  482. * @sas_node: sas node object (either expander or sas host)
  483. * @mpt3sas_phy: mpt3sas per phy object
  484. * @sas_address: sas address of device/expander were phy needs to be added to
  485. *
  486. * Returns nothing.
  487. */
  488. static void
  489. _transport_add_phy_to_an_existing_port(struct MPT3SAS_ADAPTER *ioc,
  490. struct _sas_node *sas_node, struct _sas_phy *mpt3sas_phy,
  491. u64 sas_address)
  492. {
  493. struct _sas_port *mpt3sas_port;
  494. struct _sas_phy *phy_srch;
  495. if (mpt3sas_phy->phy_belongs_to_port == 1)
  496. return;
  497. list_for_each_entry(mpt3sas_port, &sas_node->sas_port_list,
  498. port_list) {
  499. if (mpt3sas_port->remote_identify.sas_address !=
  500. sas_address)
  501. continue;
  502. list_for_each_entry(phy_srch, &mpt3sas_port->phy_list,
  503. port_siblings) {
  504. if (phy_srch == mpt3sas_phy)
  505. return;
  506. }
  507. _transport_add_phy(ioc, mpt3sas_port, mpt3sas_phy);
  508. return;
  509. }
  510. }
  511. /**
  512. * _transport_del_phy_from_an_existing_port - delete phy from existing port
  513. * @ioc: per adapter object
  514. * @sas_node: sas node object (either expander or sas host)
  515. * @mpt3sas_phy: mpt3sas per phy object
  516. *
  517. * Returns nothing.
  518. */
  519. static void
  520. _transport_del_phy_from_an_existing_port(struct MPT3SAS_ADAPTER *ioc,
  521. struct _sas_node *sas_node, struct _sas_phy *mpt3sas_phy)
  522. {
  523. struct _sas_port *mpt3sas_port, *next;
  524. struct _sas_phy *phy_srch;
  525. if (mpt3sas_phy->phy_belongs_to_port == 0)
  526. return;
  527. list_for_each_entry_safe(mpt3sas_port, next, &sas_node->sas_port_list,
  528. port_list) {
  529. list_for_each_entry(phy_srch, &mpt3sas_port->phy_list,
  530. port_siblings) {
  531. if (phy_srch != mpt3sas_phy)
  532. continue;
  533. if (mpt3sas_port->num_phys == 1)
  534. _transport_delete_port(ioc, mpt3sas_port);
  535. else
  536. _transport_delete_phy(ioc, mpt3sas_port,
  537. mpt3sas_phy);
  538. return;
  539. }
  540. }
  541. }
  542. /**
  543. * _transport_sanity_check - sanity check when adding a new port
  544. * @ioc: per adapter object
  545. * @sas_node: sas node object (either expander or sas host)
  546. * @sas_address: sas address of device being added
  547. *
  548. * See the explanation above from _transport_delete_duplicate_port
  549. */
  550. static void
  551. _transport_sanity_check(struct MPT3SAS_ADAPTER *ioc, struct _sas_node *sas_node,
  552. u64 sas_address)
  553. {
  554. int i;
  555. for (i = 0; i < sas_node->num_phys; i++) {
  556. if (sas_node->phy[i].remote_identify.sas_address != sas_address)
  557. continue;
  558. if (sas_node->phy[i].phy_belongs_to_port == 1)
  559. _transport_del_phy_from_an_existing_port(ioc, sas_node,
  560. &sas_node->phy[i]);
  561. }
  562. }
  563. /**
  564. * mpt3sas_transport_port_add - insert port to the list
  565. * @ioc: per adapter object
  566. * @handle: handle of attached device
  567. * @sas_address: sas address of parent expander or sas host
  568. * Context: This function will acquire ioc->sas_node_lock.
  569. *
  570. * Adding new port object to the sas_node->sas_port_list.
  571. *
  572. * Returns mpt3sas_port.
  573. */
  574. struct _sas_port *
  575. mpt3sas_transport_port_add(struct MPT3SAS_ADAPTER *ioc, u16 handle,
  576. u64 sas_address)
  577. {
  578. struct _sas_phy *mpt3sas_phy, *next;
  579. struct _sas_port *mpt3sas_port;
  580. unsigned long flags;
  581. struct _sas_node *sas_node;
  582. struct sas_rphy *rphy;
  583. int i;
  584. struct sas_port *port;
  585. mpt3sas_port = kzalloc(sizeof(struct _sas_port),
  586. GFP_KERNEL);
  587. if (!mpt3sas_port) {
  588. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  589. ioc->name, __FILE__, __LINE__, __func__);
  590. return NULL;
  591. }
  592. INIT_LIST_HEAD(&mpt3sas_port->port_list);
  593. INIT_LIST_HEAD(&mpt3sas_port->phy_list);
  594. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  595. sas_node = _transport_sas_node_find_by_sas_address(ioc, sas_address);
  596. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  597. if (!sas_node) {
  598. pr_err(MPT3SAS_FMT
  599. "%s: Could not find parent sas_address(0x%016llx)!\n",
  600. ioc->name, __func__, (unsigned long long)sas_address);
  601. goto out_fail;
  602. }
  603. if ((_transport_set_identify(ioc, handle,
  604. &mpt3sas_port->remote_identify))) {
  605. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  606. ioc->name, __FILE__, __LINE__, __func__);
  607. goto out_fail;
  608. }
  609. if (mpt3sas_port->remote_identify.device_type == SAS_PHY_UNUSED) {
  610. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  611. ioc->name, __FILE__, __LINE__, __func__);
  612. goto out_fail;
  613. }
  614. _transport_sanity_check(ioc, sas_node,
  615. mpt3sas_port->remote_identify.sas_address);
  616. for (i = 0; i < sas_node->num_phys; i++) {
  617. if (sas_node->phy[i].remote_identify.sas_address !=
  618. mpt3sas_port->remote_identify.sas_address)
  619. continue;
  620. list_add_tail(&sas_node->phy[i].port_siblings,
  621. &mpt3sas_port->phy_list);
  622. mpt3sas_port->num_phys++;
  623. }
  624. if (!mpt3sas_port->num_phys) {
  625. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  626. ioc->name, __FILE__, __LINE__, __func__);
  627. goto out_fail;
  628. }
  629. port = sas_port_alloc_num(sas_node->parent_dev);
  630. if ((sas_port_add(port))) {
  631. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  632. ioc->name, __FILE__, __LINE__, __func__);
  633. goto out_fail;
  634. }
  635. list_for_each_entry(mpt3sas_phy, &mpt3sas_port->phy_list,
  636. port_siblings) {
  637. if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
  638. dev_printk(KERN_INFO, &port->dev,
  639. "add: handle(0x%04x), sas_addr(0x%016llx), phy(%d)\n",
  640. handle, (unsigned long long)
  641. mpt3sas_port->remote_identify.sas_address,
  642. mpt3sas_phy->phy_id);
  643. sas_port_add_phy(port, mpt3sas_phy->phy);
  644. mpt3sas_phy->phy_belongs_to_port = 1;
  645. }
  646. mpt3sas_port->port = port;
  647. if (mpt3sas_port->remote_identify.device_type == SAS_END_DEVICE)
  648. rphy = sas_end_device_alloc(port);
  649. else
  650. rphy = sas_expander_alloc(port,
  651. mpt3sas_port->remote_identify.device_type);
  652. rphy->identify = mpt3sas_port->remote_identify;
  653. if ((sas_rphy_add(rphy))) {
  654. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  655. ioc->name, __FILE__, __LINE__, __func__);
  656. }
  657. if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
  658. dev_printk(KERN_INFO, &rphy->dev,
  659. "add: handle(0x%04x), sas_addr(0x%016llx)\n",
  660. handle, (unsigned long long)
  661. mpt3sas_port->remote_identify.sas_address);
  662. mpt3sas_port->rphy = rphy;
  663. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  664. list_add_tail(&mpt3sas_port->port_list, &sas_node->sas_port_list);
  665. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  666. /* fill in report manufacture */
  667. if (mpt3sas_port->remote_identify.device_type ==
  668. MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
  669. mpt3sas_port->remote_identify.device_type ==
  670. MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER)
  671. _transport_expander_report_manufacture(ioc,
  672. mpt3sas_port->remote_identify.sas_address,
  673. rphy_to_expander_device(rphy));
  674. return mpt3sas_port;
  675. out_fail:
  676. list_for_each_entry_safe(mpt3sas_phy, next, &mpt3sas_port->phy_list,
  677. port_siblings)
  678. list_del(&mpt3sas_phy->port_siblings);
  679. kfree(mpt3sas_port);
  680. return NULL;
  681. }
  682. /**
  683. * mpt3sas_transport_port_remove - remove port from the list
  684. * @ioc: per adapter object
  685. * @sas_address: sas address of attached device
  686. * @sas_address_parent: sas address of parent expander or sas host
  687. * Context: This function will acquire ioc->sas_node_lock.
  688. *
  689. * Removing object and freeing associated memory from the
  690. * ioc->sas_port_list.
  691. *
  692. * Return nothing.
  693. */
  694. void
  695. mpt3sas_transport_port_remove(struct MPT3SAS_ADAPTER *ioc, u64 sas_address,
  696. u64 sas_address_parent)
  697. {
  698. int i;
  699. unsigned long flags;
  700. struct _sas_port *mpt3sas_port, *next;
  701. struct _sas_node *sas_node;
  702. u8 found = 0;
  703. struct _sas_phy *mpt3sas_phy, *next_phy;
  704. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  705. sas_node = _transport_sas_node_find_by_sas_address(ioc,
  706. sas_address_parent);
  707. if (!sas_node) {
  708. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  709. return;
  710. }
  711. list_for_each_entry_safe(mpt3sas_port, next, &sas_node->sas_port_list,
  712. port_list) {
  713. if (mpt3sas_port->remote_identify.sas_address != sas_address)
  714. continue;
  715. found = 1;
  716. list_del(&mpt3sas_port->port_list);
  717. goto out;
  718. }
  719. out:
  720. if (!found) {
  721. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  722. return;
  723. }
  724. for (i = 0; i < sas_node->num_phys; i++) {
  725. if (sas_node->phy[i].remote_identify.sas_address == sas_address)
  726. memset(&sas_node->phy[i].remote_identify, 0 ,
  727. sizeof(struct sas_identify));
  728. }
  729. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  730. list_for_each_entry_safe(mpt3sas_phy, next_phy,
  731. &mpt3sas_port->phy_list, port_siblings) {
  732. if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
  733. dev_printk(KERN_INFO, &mpt3sas_port->port->dev,
  734. "remove: sas_addr(0x%016llx), phy(%d)\n",
  735. (unsigned long long)
  736. mpt3sas_port->remote_identify.sas_address,
  737. mpt3sas_phy->phy_id);
  738. mpt3sas_phy->phy_belongs_to_port = 0;
  739. sas_port_delete_phy(mpt3sas_port->port, mpt3sas_phy->phy);
  740. list_del(&mpt3sas_phy->port_siblings);
  741. }
  742. sas_port_delete(mpt3sas_port->port);
  743. kfree(mpt3sas_port);
  744. }
  745. /**
  746. * mpt3sas_transport_add_host_phy - report sas_host phy to transport
  747. * @ioc: per adapter object
  748. * @mpt3sas_phy: mpt3sas per phy object
  749. * @phy_pg0: sas phy page 0
  750. * @parent_dev: parent device class object
  751. *
  752. * Returns 0 for success, non-zero for failure.
  753. */
  754. int
  755. mpt3sas_transport_add_host_phy(struct MPT3SAS_ADAPTER *ioc, struct _sas_phy
  756. *mpt3sas_phy, Mpi2SasPhyPage0_t phy_pg0, struct device *parent_dev)
  757. {
  758. struct sas_phy *phy;
  759. int phy_index = mpt3sas_phy->phy_id;
  760. INIT_LIST_HEAD(&mpt3sas_phy->port_siblings);
  761. phy = sas_phy_alloc(parent_dev, phy_index);
  762. if (!phy) {
  763. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  764. ioc->name, __FILE__, __LINE__, __func__);
  765. return -1;
  766. }
  767. if ((_transport_set_identify(ioc, mpt3sas_phy->handle,
  768. &mpt3sas_phy->identify))) {
  769. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  770. ioc->name, __FILE__, __LINE__, __func__);
  771. sas_phy_free(phy);
  772. return -1;
  773. }
  774. phy->identify = mpt3sas_phy->identify;
  775. mpt3sas_phy->attached_handle = le16_to_cpu(phy_pg0.AttachedDevHandle);
  776. if (mpt3sas_phy->attached_handle)
  777. _transport_set_identify(ioc, mpt3sas_phy->attached_handle,
  778. &mpt3sas_phy->remote_identify);
  779. phy->identify.phy_identifier = mpt3sas_phy->phy_id;
  780. phy->negotiated_linkrate = _transport_convert_phy_link_rate(
  781. phy_pg0.NegotiatedLinkRate & MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
  782. phy->minimum_linkrate_hw = _transport_convert_phy_link_rate(
  783. phy_pg0.HwLinkRate & MPI2_SAS_HWRATE_MIN_RATE_MASK);
  784. phy->maximum_linkrate_hw = _transport_convert_phy_link_rate(
  785. phy_pg0.HwLinkRate >> 4);
  786. phy->minimum_linkrate = _transport_convert_phy_link_rate(
  787. phy_pg0.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
  788. phy->maximum_linkrate = _transport_convert_phy_link_rate(
  789. phy_pg0.ProgrammedLinkRate >> 4);
  790. if ((sas_phy_add(phy))) {
  791. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  792. ioc->name, __FILE__, __LINE__, __func__);
  793. sas_phy_free(phy);
  794. return -1;
  795. }
  796. if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
  797. dev_printk(KERN_INFO, &phy->dev,
  798. "add: handle(0x%04x), sas_addr(0x%016llx)\n"
  799. "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
  800. mpt3sas_phy->handle, (unsigned long long)
  801. mpt3sas_phy->identify.sas_address,
  802. mpt3sas_phy->attached_handle,
  803. (unsigned long long)
  804. mpt3sas_phy->remote_identify.sas_address);
  805. mpt3sas_phy->phy = phy;
  806. return 0;
  807. }
  808. /**
  809. * mpt3sas_transport_add_expander_phy - report expander phy to transport
  810. * @ioc: per adapter object
  811. * @mpt3sas_phy: mpt3sas per phy object
  812. * @expander_pg1: expander page 1
  813. * @parent_dev: parent device class object
  814. *
  815. * Returns 0 for success, non-zero for failure.
  816. */
  817. int
  818. mpt3sas_transport_add_expander_phy(struct MPT3SAS_ADAPTER *ioc, struct _sas_phy
  819. *mpt3sas_phy, Mpi2ExpanderPage1_t expander_pg1,
  820. struct device *parent_dev)
  821. {
  822. struct sas_phy *phy;
  823. int phy_index = mpt3sas_phy->phy_id;
  824. INIT_LIST_HEAD(&mpt3sas_phy->port_siblings);
  825. phy = sas_phy_alloc(parent_dev, phy_index);
  826. if (!phy) {
  827. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  828. ioc->name, __FILE__, __LINE__, __func__);
  829. return -1;
  830. }
  831. if ((_transport_set_identify(ioc, mpt3sas_phy->handle,
  832. &mpt3sas_phy->identify))) {
  833. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  834. ioc->name, __FILE__, __LINE__, __func__);
  835. sas_phy_free(phy);
  836. return -1;
  837. }
  838. phy->identify = mpt3sas_phy->identify;
  839. mpt3sas_phy->attached_handle =
  840. le16_to_cpu(expander_pg1.AttachedDevHandle);
  841. if (mpt3sas_phy->attached_handle)
  842. _transport_set_identify(ioc, mpt3sas_phy->attached_handle,
  843. &mpt3sas_phy->remote_identify);
  844. phy->identify.phy_identifier = mpt3sas_phy->phy_id;
  845. phy->negotiated_linkrate = _transport_convert_phy_link_rate(
  846. expander_pg1.NegotiatedLinkRate &
  847. MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
  848. phy->minimum_linkrate_hw = _transport_convert_phy_link_rate(
  849. expander_pg1.HwLinkRate & MPI2_SAS_HWRATE_MIN_RATE_MASK);
  850. phy->maximum_linkrate_hw = _transport_convert_phy_link_rate(
  851. expander_pg1.HwLinkRate >> 4);
  852. phy->minimum_linkrate = _transport_convert_phy_link_rate(
  853. expander_pg1.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
  854. phy->maximum_linkrate = _transport_convert_phy_link_rate(
  855. expander_pg1.ProgrammedLinkRate >> 4);
  856. if ((sas_phy_add(phy))) {
  857. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  858. ioc->name, __FILE__, __LINE__, __func__);
  859. sas_phy_free(phy);
  860. return -1;
  861. }
  862. if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
  863. dev_printk(KERN_INFO, &phy->dev,
  864. "add: handle(0x%04x), sas_addr(0x%016llx)\n"
  865. "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
  866. mpt3sas_phy->handle, (unsigned long long)
  867. mpt3sas_phy->identify.sas_address,
  868. mpt3sas_phy->attached_handle,
  869. (unsigned long long)
  870. mpt3sas_phy->remote_identify.sas_address);
  871. mpt3sas_phy->phy = phy;
  872. return 0;
  873. }
  874. /**
  875. * mpt3sas_transport_update_links - refreshing phy link changes
  876. * @ioc: per adapter object
  877. * @sas_address: sas address of parent expander or sas host
  878. * @handle: attached device handle
  879. * @phy_numberv: phy number
  880. * @link_rate: new link rate
  881. *
  882. * Returns nothing.
  883. */
  884. void
  885. mpt3sas_transport_update_links(struct MPT3SAS_ADAPTER *ioc,
  886. u64 sas_address, u16 handle, u8 phy_number, u8 link_rate)
  887. {
  888. unsigned long flags;
  889. struct _sas_node *sas_node;
  890. struct _sas_phy *mpt3sas_phy;
  891. if (ioc->shost_recovery || ioc->pci_error_recovery)
  892. return;
  893. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  894. sas_node = _transport_sas_node_find_by_sas_address(ioc, sas_address);
  895. if (!sas_node) {
  896. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  897. return;
  898. }
  899. mpt3sas_phy = &sas_node->phy[phy_number];
  900. mpt3sas_phy->attached_handle = handle;
  901. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  902. if (handle && (link_rate >= MPI2_SAS_NEG_LINK_RATE_1_5)) {
  903. _transport_set_identify(ioc, handle,
  904. &mpt3sas_phy->remote_identify);
  905. _transport_add_phy_to_an_existing_port(ioc, sas_node,
  906. mpt3sas_phy, mpt3sas_phy->remote_identify.sas_address);
  907. } else
  908. memset(&mpt3sas_phy->remote_identify, 0 , sizeof(struct
  909. sas_identify));
  910. if (mpt3sas_phy->phy)
  911. mpt3sas_phy->phy->negotiated_linkrate =
  912. _transport_convert_phy_link_rate(link_rate);
  913. if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
  914. dev_printk(KERN_INFO, &mpt3sas_phy->phy->dev,
  915. "refresh: parent sas_addr(0x%016llx),\n"
  916. "\tlink_rate(0x%02x), phy(%d)\n"
  917. "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
  918. (unsigned long long)sas_address,
  919. link_rate, phy_number, handle, (unsigned long long)
  920. mpt3sas_phy->remote_identify.sas_address);
  921. }
  922. static inline void *
  923. phy_to_ioc(struct sas_phy *phy)
  924. {
  925. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
  926. return shost_priv(shost);
  927. }
  928. static inline void *
  929. rphy_to_ioc(struct sas_rphy *rphy)
  930. {
  931. struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
  932. return shost_priv(shost);
  933. }
  934. /* report phy error log structure */
  935. struct phy_error_log_request {
  936. u8 smp_frame_type; /* 0x40 */
  937. u8 function; /* 0x11 */
  938. u8 allocated_response_length;
  939. u8 request_length; /* 02 */
  940. u8 reserved_1[5];
  941. u8 phy_identifier;
  942. u8 reserved_2[2];
  943. };
  944. /* report phy error log reply structure */
  945. struct phy_error_log_reply {
  946. u8 smp_frame_type; /* 0x41 */
  947. u8 function; /* 0x11 */
  948. u8 function_result;
  949. u8 response_length;
  950. __be16 expander_change_count;
  951. u8 reserved_1[3];
  952. u8 phy_identifier;
  953. u8 reserved_2[2];
  954. __be32 invalid_dword;
  955. __be32 running_disparity_error;
  956. __be32 loss_of_dword_sync;
  957. __be32 phy_reset_problem;
  958. };
  959. /**
  960. * _transport_get_expander_phy_error_log - return expander counters
  961. * @ioc: per adapter object
  962. * @phy: The sas phy object
  963. *
  964. * Returns 0 for success, non-zero for failure.
  965. *
  966. */
  967. static int
  968. _transport_get_expander_phy_error_log(struct MPT3SAS_ADAPTER *ioc,
  969. struct sas_phy *phy)
  970. {
  971. Mpi2SmpPassthroughRequest_t *mpi_request;
  972. Mpi2SmpPassthroughReply_t *mpi_reply;
  973. struct phy_error_log_request *phy_error_log_request;
  974. struct phy_error_log_reply *phy_error_log_reply;
  975. int rc;
  976. u16 smid;
  977. u32 ioc_state;
  978. unsigned long timeleft;
  979. void *psge;
  980. u8 issue_reset = 0;
  981. void *data_out = NULL;
  982. dma_addr_t data_out_dma;
  983. u32 sz;
  984. u16 wait_state_count;
  985. if (ioc->shost_recovery || ioc->pci_error_recovery) {
  986. pr_info(MPT3SAS_FMT "%s: host reset in progress!\n",
  987. __func__, ioc->name);
  988. return -EFAULT;
  989. }
  990. mutex_lock(&ioc->transport_cmds.mutex);
  991. if (ioc->transport_cmds.status != MPT3_CMD_NOT_USED) {
  992. pr_err(MPT3SAS_FMT "%s: transport_cmds in use\n",
  993. ioc->name, __func__);
  994. rc = -EAGAIN;
  995. goto out;
  996. }
  997. ioc->transport_cmds.status = MPT3_CMD_PENDING;
  998. wait_state_count = 0;
  999. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  1000. while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  1001. if (wait_state_count++ == 10) {
  1002. pr_err(MPT3SAS_FMT
  1003. "%s: failed due to ioc not operational\n",
  1004. ioc->name, __func__);
  1005. rc = -EFAULT;
  1006. goto out;
  1007. }
  1008. ssleep(1);
  1009. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  1010. pr_info(MPT3SAS_FMT
  1011. "%s: waiting for operational state(count=%d)\n",
  1012. ioc->name, __func__, wait_state_count);
  1013. }
  1014. if (wait_state_count)
  1015. pr_info(MPT3SAS_FMT "%s: ioc is operational\n",
  1016. ioc->name, __func__);
  1017. smid = mpt3sas_base_get_smid(ioc, ioc->transport_cb_idx);
  1018. if (!smid) {
  1019. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  1020. ioc->name, __func__);
  1021. rc = -EAGAIN;
  1022. goto out;
  1023. }
  1024. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  1025. ioc->transport_cmds.smid = smid;
  1026. sz = sizeof(struct phy_error_log_request) +
  1027. sizeof(struct phy_error_log_reply);
  1028. data_out = pci_alloc_consistent(ioc->pdev, sz, &data_out_dma);
  1029. if (!data_out) {
  1030. pr_err("failure at %s:%d/%s()!\n", __FILE__,
  1031. __LINE__, __func__);
  1032. rc = -ENOMEM;
  1033. mpt3sas_base_free_smid(ioc, smid);
  1034. goto out;
  1035. }
  1036. rc = -EINVAL;
  1037. memset(data_out, 0, sz);
  1038. phy_error_log_request = data_out;
  1039. phy_error_log_request->smp_frame_type = 0x40;
  1040. phy_error_log_request->function = 0x11;
  1041. phy_error_log_request->request_length = 2;
  1042. phy_error_log_request->allocated_response_length = 0;
  1043. phy_error_log_request->phy_identifier = phy->number;
  1044. memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
  1045. mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
  1046. mpi_request->PhysicalPort = 0xFF;
  1047. mpi_request->VF_ID = 0; /* TODO */
  1048. mpi_request->VP_ID = 0;
  1049. mpi_request->SASAddress = cpu_to_le64(phy->identify.sas_address);
  1050. mpi_request->RequestDataLength =
  1051. cpu_to_le16(sizeof(struct phy_error_log_request));
  1052. psge = &mpi_request->SGL;
  1053. ioc->build_sg(ioc, psge, data_out_dma,
  1054. sizeof(struct phy_error_log_request),
  1055. data_out_dma + sizeof(struct phy_error_log_request),
  1056. sizeof(struct phy_error_log_reply));
  1057. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1058. "phy_error_log - send to sas_addr(0x%016llx), phy(%d)\n",
  1059. ioc->name, (unsigned long long)phy->identify.sas_address,
  1060. phy->number));
  1061. init_completion(&ioc->transport_cmds.done);
  1062. mpt3sas_base_put_smid_default(ioc, smid);
  1063. timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done,
  1064. 10*HZ);
  1065. if (!(ioc->transport_cmds.status & MPT3_CMD_COMPLETE)) {
  1066. pr_err(MPT3SAS_FMT "%s: timeout\n",
  1067. ioc->name, __func__);
  1068. _debug_dump_mf(mpi_request,
  1069. sizeof(Mpi2SmpPassthroughRequest_t)/4);
  1070. if (!(ioc->transport_cmds.status & MPT3_CMD_RESET))
  1071. issue_reset = 1;
  1072. goto issue_host_reset;
  1073. }
  1074. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1075. "phy_error_log - complete\n", ioc->name));
  1076. if (ioc->transport_cmds.status & MPT3_CMD_REPLY_VALID) {
  1077. mpi_reply = ioc->transport_cmds.reply;
  1078. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1079. "phy_error_log - reply data transfer size(%d)\n",
  1080. ioc->name, le16_to_cpu(mpi_reply->ResponseDataLength)));
  1081. if (le16_to_cpu(mpi_reply->ResponseDataLength) !=
  1082. sizeof(struct phy_error_log_reply))
  1083. goto out;
  1084. phy_error_log_reply = data_out +
  1085. sizeof(struct phy_error_log_request);
  1086. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1087. "phy_error_log - function_result(%d)\n",
  1088. ioc->name, phy_error_log_reply->function_result));
  1089. phy->invalid_dword_count =
  1090. be32_to_cpu(phy_error_log_reply->invalid_dword);
  1091. phy->running_disparity_error_count =
  1092. be32_to_cpu(phy_error_log_reply->running_disparity_error);
  1093. phy->loss_of_dword_sync_count =
  1094. be32_to_cpu(phy_error_log_reply->loss_of_dword_sync);
  1095. phy->phy_reset_problem_count =
  1096. be32_to_cpu(phy_error_log_reply->phy_reset_problem);
  1097. rc = 0;
  1098. } else
  1099. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1100. "phy_error_log - no reply\n", ioc->name));
  1101. issue_host_reset:
  1102. if (issue_reset)
  1103. mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  1104. FORCE_BIG_HAMMER);
  1105. out:
  1106. ioc->transport_cmds.status = MPT3_CMD_NOT_USED;
  1107. if (data_out)
  1108. pci_free_consistent(ioc->pdev, sz, data_out, data_out_dma);
  1109. mutex_unlock(&ioc->transport_cmds.mutex);
  1110. return rc;
  1111. }
  1112. /**
  1113. * _transport_get_linkerrors - return phy counters for both hba and expanders
  1114. * @phy: The sas phy object
  1115. *
  1116. * Returns 0 for success, non-zero for failure.
  1117. *
  1118. */
  1119. static int
  1120. _transport_get_linkerrors(struct sas_phy *phy)
  1121. {
  1122. struct MPT3SAS_ADAPTER *ioc = phy_to_ioc(phy);
  1123. unsigned long flags;
  1124. Mpi2ConfigReply_t mpi_reply;
  1125. Mpi2SasPhyPage1_t phy_pg1;
  1126. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  1127. if (_transport_sas_node_find_by_sas_address(ioc,
  1128. phy->identify.sas_address) == NULL) {
  1129. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  1130. return -EINVAL;
  1131. }
  1132. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  1133. if (phy->identify.sas_address != ioc->sas_hba.sas_address)
  1134. return _transport_get_expander_phy_error_log(ioc, phy);
  1135. /* get hba phy error logs */
  1136. if ((mpt3sas_config_get_phy_pg1(ioc, &mpi_reply, &phy_pg1,
  1137. phy->number))) {
  1138. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1139. ioc->name, __FILE__, __LINE__, __func__);
  1140. return -ENXIO;
  1141. }
  1142. if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo)
  1143. pr_info(MPT3SAS_FMT
  1144. "phy(%d), ioc_status (0x%04x), loginfo(0x%08x)\n",
  1145. ioc->name, phy->number,
  1146. le16_to_cpu(mpi_reply.IOCStatus),
  1147. le32_to_cpu(mpi_reply.IOCLogInfo));
  1148. phy->invalid_dword_count = le32_to_cpu(phy_pg1.InvalidDwordCount);
  1149. phy->running_disparity_error_count =
  1150. le32_to_cpu(phy_pg1.RunningDisparityErrorCount);
  1151. phy->loss_of_dword_sync_count =
  1152. le32_to_cpu(phy_pg1.LossDwordSynchCount);
  1153. phy->phy_reset_problem_count =
  1154. le32_to_cpu(phy_pg1.PhyResetProblemCount);
  1155. return 0;
  1156. }
  1157. /**
  1158. * _transport_get_enclosure_identifier -
  1159. * @phy: The sas phy object
  1160. *
  1161. * Obtain the enclosure logical id for an expander.
  1162. * Returns 0 for success, non-zero for failure.
  1163. */
  1164. static int
  1165. _transport_get_enclosure_identifier(struct sas_rphy *rphy, u64 *identifier)
  1166. {
  1167. struct MPT3SAS_ADAPTER *ioc = rphy_to_ioc(rphy);
  1168. struct _sas_device *sas_device;
  1169. unsigned long flags;
  1170. int rc;
  1171. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  1172. sas_device = mpt3sas_scsih_sas_device_find_by_sas_address(ioc,
  1173. rphy->identify.sas_address);
  1174. if (sas_device) {
  1175. *identifier = sas_device->enclosure_logical_id;
  1176. rc = 0;
  1177. } else {
  1178. *identifier = 0;
  1179. rc = -ENXIO;
  1180. }
  1181. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1182. return rc;
  1183. }
  1184. /**
  1185. * _transport_get_bay_identifier -
  1186. * @phy: The sas phy object
  1187. *
  1188. * Returns the slot id for a device that resides inside an enclosure.
  1189. */
  1190. static int
  1191. _transport_get_bay_identifier(struct sas_rphy *rphy)
  1192. {
  1193. struct MPT3SAS_ADAPTER *ioc = rphy_to_ioc(rphy);
  1194. struct _sas_device *sas_device;
  1195. unsigned long flags;
  1196. int rc;
  1197. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  1198. sas_device = mpt3sas_scsih_sas_device_find_by_sas_address(ioc,
  1199. rphy->identify.sas_address);
  1200. if (sas_device)
  1201. rc = sas_device->slot;
  1202. else
  1203. rc = -ENXIO;
  1204. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1205. return rc;
  1206. }
  1207. /* phy control request structure */
  1208. struct phy_control_request {
  1209. u8 smp_frame_type; /* 0x40 */
  1210. u8 function; /* 0x91 */
  1211. u8 allocated_response_length;
  1212. u8 request_length; /* 0x09 */
  1213. u16 expander_change_count;
  1214. u8 reserved_1[3];
  1215. u8 phy_identifier;
  1216. u8 phy_operation;
  1217. u8 reserved_2[13];
  1218. u64 attached_device_name;
  1219. u8 programmed_min_physical_link_rate;
  1220. u8 programmed_max_physical_link_rate;
  1221. u8 reserved_3[6];
  1222. };
  1223. /* phy control reply structure */
  1224. struct phy_control_reply {
  1225. u8 smp_frame_type; /* 0x41 */
  1226. u8 function; /* 0x11 */
  1227. u8 function_result;
  1228. u8 response_length;
  1229. };
  1230. #define SMP_PHY_CONTROL_LINK_RESET (0x01)
  1231. #define SMP_PHY_CONTROL_HARD_RESET (0x02)
  1232. #define SMP_PHY_CONTROL_DISABLE (0x03)
  1233. /**
  1234. * _transport_expander_phy_control - expander phy control
  1235. * @ioc: per adapter object
  1236. * @phy: The sas phy object
  1237. *
  1238. * Returns 0 for success, non-zero for failure.
  1239. *
  1240. */
  1241. static int
  1242. _transport_expander_phy_control(struct MPT3SAS_ADAPTER *ioc,
  1243. struct sas_phy *phy, u8 phy_operation)
  1244. {
  1245. Mpi2SmpPassthroughRequest_t *mpi_request;
  1246. Mpi2SmpPassthroughReply_t *mpi_reply;
  1247. struct phy_control_request *phy_control_request;
  1248. struct phy_control_reply *phy_control_reply;
  1249. int rc;
  1250. u16 smid;
  1251. u32 ioc_state;
  1252. unsigned long timeleft;
  1253. void *psge;
  1254. u32 sgl_flags;
  1255. u8 issue_reset = 0;
  1256. void *data_out = NULL;
  1257. dma_addr_t data_out_dma;
  1258. u32 sz;
  1259. u16 wait_state_count;
  1260. if (ioc->shost_recovery || ioc->pci_error_recovery) {
  1261. pr_info(MPT3SAS_FMT "%s: host reset in progress!\n",
  1262. __func__, ioc->name);
  1263. return -EFAULT;
  1264. }
  1265. mutex_lock(&ioc->transport_cmds.mutex);
  1266. if (ioc->transport_cmds.status != MPT3_CMD_NOT_USED) {
  1267. pr_err(MPT3SAS_FMT "%s: transport_cmds in use\n",
  1268. ioc->name, __func__);
  1269. rc = -EAGAIN;
  1270. goto out;
  1271. }
  1272. ioc->transport_cmds.status = MPT3_CMD_PENDING;
  1273. wait_state_count = 0;
  1274. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  1275. while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  1276. if (wait_state_count++ == 10) {
  1277. pr_err(MPT3SAS_FMT
  1278. "%s: failed due to ioc not operational\n",
  1279. ioc->name, __func__);
  1280. rc = -EFAULT;
  1281. goto out;
  1282. }
  1283. ssleep(1);
  1284. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  1285. pr_info(MPT3SAS_FMT
  1286. "%s: waiting for operational state(count=%d)\n",
  1287. ioc->name, __func__, wait_state_count);
  1288. }
  1289. if (wait_state_count)
  1290. pr_info(MPT3SAS_FMT "%s: ioc is operational\n",
  1291. ioc->name, __func__);
  1292. smid = mpt3sas_base_get_smid(ioc, ioc->transport_cb_idx);
  1293. if (!smid) {
  1294. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  1295. ioc->name, __func__);
  1296. rc = -EAGAIN;
  1297. goto out;
  1298. }
  1299. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  1300. ioc->transport_cmds.smid = smid;
  1301. sz = sizeof(struct phy_control_request) +
  1302. sizeof(struct phy_control_reply);
  1303. data_out = pci_alloc_consistent(ioc->pdev, sz, &data_out_dma);
  1304. if (!data_out) {
  1305. pr_err("failure at %s:%d/%s()!\n", __FILE__,
  1306. __LINE__, __func__);
  1307. rc = -ENOMEM;
  1308. mpt3sas_base_free_smid(ioc, smid);
  1309. goto out;
  1310. }
  1311. rc = -EINVAL;
  1312. memset(data_out, 0, sz);
  1313. phy_control_request = data_out;
  1314. phy_control_request->smp_frame_type = 0x40;
  1315. phy_control_request->function = 0x91;
  1316. phy_control_request->request_length = 9;
  1317. phy_control_request->allocated_response_length = 0;
  1318. phy_control_request->phy_identifier = phy->number;
  1319. phy_control_request->phy_operation = phy_operation;
  1320. phy_control_request->programmed_min_physical_link_rate =
  1321. phy->minimum_linkrate << 4;
  1322. phy_control_request->programmed_max_physical_link_rate =
  1323. phy->maximum_linkrate << 4;
  1324. memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
  1325. mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
  1326. mpi_request->PhysicalPort = 0xFF;
  1327. mpi_request->VF_ID = 0; /* TODO */
  1328. mpi_request->VP_ID = 0;
  1329. mpi_request->SASAddress = cpu_to_le64(phy->identify.sas_address);
  1330. mpi_request->RequestDataLength =
  1331. cpu_to_le16(sizeof(struct phy_error_log_request));
  1332. psge = &mpi_request->SGL;
  1333. /* WRITE sgel first */
  1334. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  1335. MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
  1336. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  1337. ioc->base_add_sg_single(psge, sgl_flags |
  1338. sizeof(struct phy_control_request), data_out_dma);
  1339. /* incr sgel */
  1340. psge += ioc->sge_size;
  1341. /* READ sgel last */
  1342. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  1343. MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
  1344. MPI2_SGE_FLAGS_END_OF_LIST);
  1345. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  1346. ioc->base_add_sg_single(psge, sgl_flags |
  1347. sizeof(struct phy_control_reply), data_out_dma +
  1348. sizeof(struct phy_control_request));
  1349. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1350. "phy_control - send to sas_addr(0x%016llx), phy(%d), opcode(%d)\n",
  1351. ioc->name, (unsigned long long)phy->identify.sas_address,
  1352. phy->number, phy_operation));
  1353. init_completion(&ioc->transport_cmds.done);
  1354. mpt3sas_base_put_smid_default(ioc, smid);
  1355. timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done,
  1356. 10*HZ);
  1357. if (!(ioc->transport_cmds.status & MPT3_CMD_COMPLETE)) {
  1358. pr_err(MPT3SAS_FMT "%s: timeout\n",
  1359. ioc->name, __func__);
  1360. _debug_dump_mf(mpi_request,
  1361. sizeof(Mpi2SmpPassthroughRequest_t)/4);
  1362. if (!(ioc->transport_cmds.status & MPT3_CMD_RESET))
  1363. issue_reset = 1;
  1364. goto issue_host_reset;
  1365. }
  1366. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1367. "phy_control - complete\n", ioc->name));
  1368. if (ioc->transport_cmds.status & MPT3_CMD_REPLY_VALID) {
  1369. mpi_reply = ioc->transport_cmds.reply;
  1370. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1371. "phy_control - reply data transfer size(%d)\n",
  1372. ioc->name, le16_to_cpu(mpi_reply->ResponseDataLength)));
  1373. if (le16_to_cpu(mpi_reply->ResponseDataLength) !=
  1374. sizeof(struct phy_control_reply))
  1375. goto out;
  1376. phy_control_reply = data_out +
  1377. sizeof(struct phy_control_request);
  1378. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1379. "phy_control - function_result(%d)\n",
  1380. ioc->name, phy_control_reply->function_result));
  1381. rc = 0;
  1382. } else
  1383. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1384. "phy_control - no reply\n", ioc->name));
  1385. issue_host_reset:
  1386. if (issue_reset)
  1387. mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  1388. FORCE_BIG_HAMMER);
  1389. out:
  1390. ioc->transport_cmds.status = MPT3_CMD_NOT_USED;
  1391. if (data_out)
  1392. pci_free_consistent(ioc->pdev, sz, data_out, data_out_dma);
  1393. mutex_unlock(&ioc->transport_cmds.mutex);
  1394. return rc;
  1395. }
  1396. /**
  1397. * _transport_phy_reset -
  1398. * @phy: The sas phy object
  1399. * @hard_reset:
  1400. *
  1401. * Returns 0 for success, non-zero for failure.
  1402. */
  1403. static int
  1404. _transport_phy_reset(struct sas_phy *phy, int hard_reset)
  1405. {
  1406. struct MPT3SAS_ADAPTER *ioc = phy_to_ioc(phy);
  1407. Mpi2SasIoUnitControlReply_t mpi_reply;
  1408. Mpi2SasIoUnitControlRequest_t mpi_request;
  1409. unsigned long flags;
  1410. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  1411. if (_transport_sas_node_find_by_sas_address(ioc,
  1412. phy->identify.sas_address) == NULL) {
  1413. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  1414. return -EINVAL;
  1415. }
  1416. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  1417. /* handle expander phys */
  1418. if (phy->identify.sas_address != ioc->sas_hba.sas_address)
  1419. return _transport_expander_phy_control(ioc, phy,
  1420. (hard_reset == 1) ? SMP_PHY_CONTROL_HARD_RESET :
  1421. SMP_PHY_CONTROL_LINK_RESET);
  1422. /* handle hba phys */
  1423. memset(&mpi_request, 0, sizeof(Mpi2SasIoUnitControlReply_t));
  1424. mpi_request.Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
  1425. mpi_request.Operation = hard_reset ?
  1426. MPI2_SAS_OP_PHY_HARD_RESET : MPI2_SAS_OP_PHY_LINK_RESET;
  1427. mpi_request.PhyNum = phy->number;
  1428. if ((mpt3sas_base_sas_iounit_control(ioc, &mpi_reply, &mpi_request))) {
  1429. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1430. ioc->name, __FILE__, __LINE__, __func__);
  1431. return -ENXIO;
  1432. }
  1433. if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo)
  1434. pr_info(MPT3SAS_FMT
  1435. "phy(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
  1436. ioc->name, phy->number, le16_to_cpu(mpi_reply.IOCStatus),
  1437. le32_to_cpu(mpi_reply.IOCLogInfo));
  1438. return 0;
  1439. }
  1440. /**
  1441. * _transport_phy_enable - enable/disable phys
  1442. * @phy: The sas phy object
  1443. * @enable: enable phy when true
  1444. *
  1445. * Only support sas_host direct attached phys.
  1446. * Returns 0 for success, non-zero for failure.
  1447. */
  1448. static int
  1449. _transport_phy_enable(struct sas_phy *phy, int enable)
  1450. {
  1451. struct MPT3SAS_ADAPTER *ioc = phy_to_ioc(phy);
  1452. Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
  1453. Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
  1454. Mpi2ConfigReply_t mpi_reply;
  1455. u16 ioc_status;
  1456. u16 sz;
  1457. int rc = 0;
  1458. unsigned long flags;
  1459. int i, discovery_active;
  1460. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  1461. if (_transport_sas_node_find_by_sas_address(ioc,
  1462. phy->identify.sas_address) == NULL) {
  1463. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  1464. return -EINVAL;
  1465. }
  1466. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  1467. /* handle expander phys */
  1468. if (phy->identify.sas_address != ioc->sas_hba.sas_address)
  1469. return _transport_expander_phy_control(ioc, phy,
  1470. (enable == 1) ? SMP_PHY_CONTROL_LINK_RESET :
  1471. SMP_PHY_CONTROL_DISABLE);
  1472. /* handle hba phys */
  1473. /* read sas_iounit page 0 */
  1474. sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
  1475. sizeof(Mpi2SasIOUnit0PhyData_t));
  1476. sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
  1477. if (!sas_iounit_pg0) {
  1478. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1479. ioc->name, __FILE__, __LINE__, __func__);
  1480. rc = -ENOMEM;
  1481. goto out;
  1482. }
  1483. if ((mpt3sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
  1484. sas_iounit_pg0, sz))) {
  1485. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1486. ioc->name, __FILE__, __LINE__, __func__);
  1487. rc = -ENXIO;
  1488. goto out;
  1489. }
  1490. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  1491. MPI2_IOCSTATUS_MASK;
  1492. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  1493. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1494. ioc->name, __FILE__, __LINE__, __func__);
  1495. rc = -EIO;
  1496. goto out;
  1497. }
  1498. /* unable to enable/disable phys when when discovery is active */
  1499. for (i = 0, discovery_active = 0; i < ioc->sas_hba.num_phys ; i++) {
  1500. if (sas_iounit_pg0->PhyData[i].PortFlags &
  1501. MPI2_SASIOUNIT0_PORTFLAGS_DISCOVERY_IN_PROGRESS) {
  1502. pr_err(MPT3SAS_FMT "discovery is active on " \
  1503. "port = %d, phy = %d: unable to enable/disable "
  1504. "phys, try again later!\n", ioc->name,
  1505. sas_iounit_pg0->PhyData[i].Port, i);
  1506. discovery_active = 1;
  1507. }
  1508. }
  1509. if (discovery_active) {
  1510. rc = -EAGAIN;
  1511. goto out;
  1512. }
  1513. /* read sas_iounit page 1 */
  1514. sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
  1515. sizeof(Mpi2SasIOUnit1PhyData_t));
  1516. sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
  1517. if (!sas_iounit_pg1) {
  1518. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1519. ioc->name, __FILE__, __LINE__, __func__);
  1520. rc = -ENOMEM;
  1521. goto out;
  1522. }
  1523. if ((mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
  1524. sas_iounit_pg1, sz))) {
  1525. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1526. ioc->name, __FILE__, __LINE__, __func__);
  1527. rc = -ENXIO;
  1528. goto out;
  1529. }
  1530. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  1531. MPI2_IOCSTATUS_MASK;
  1532. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  1533. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1534. ioc->name, __FILE__, __LINE__, __func__);
  1535. rc = -EIO;
  1536. goto out;
  1537. }
  1538. /* copy Port/PortFlags/PhyFlags from page 0 */
  1539. for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
  1540. sas_iounit_pg1->PhyData[i].Port =
  1541. sas_iounit_pg0->PhyData[i].Port;
  1542. sas_iounit_pg1->PhyData[i].PortFlags =
  1543. (sas_iounit_pg0->PhyData[i].PortFlags &
  1544. MPI2_SASIOUNIT0_PORTFLAGS_AUTO_PORT_CONFIG);
  1545. sas_iounit_pg1->PhyData[i].PhyFlags =
  1546. (sas_iounit_pg0->PhyData[i].PhyFlags &
  1547. (MPI2_SASIOUNIT0_PHYFLAGS_ZONING_ENABLED +
  1548. MPI2_SASIOUNIT0_PHYFLAGS_PHY_DISABLED));
  1549. }
  1550. if (enable)
  1551. sas_iounit_pg1->PhyData[phy->number].PhyFlags
  1552. &= ~MPI2_SASIOUNIT1_PHYFLAGS_PHY_DISABLE;
  1553. else
  1554. sas_iounit_pg1->PhyData[phy->number].PhyFlags
  1555. |= MPI2_SASIOUNIT1_PHYFLAGS_PHY_DISABLE;
  1556. mpt3sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1, sz);
  1557. /* link reset */
  1558. if (enable)
  1559. _transport_phy_reset(phy, 0);
  1560. out:
  1561. kfree(sas_iounit_pg1);
  1562. kfree(sas_iounit_pg0);
  1563. return rc;
  1564. }
  1565. /**
  1566. * _transport_phy_speed - set phy min/max link rates
  1567. * @phy: The sas phy object
  1568. * @rates: rates defined in sas_phy_linkrates
  1569. *
  1570. * Only support sas_host direct attached phys.
  1571. * Returns 0 for success, non-zero for failure.
  1572. */
  1573. static int
  1574. _transport_phy_speed(struct sas_phy *phy, struct sas_phy_linkrates *rates)
  1575. {
  1576. struct MPT3SAS_ADAPTER *ioc = phy_to_ioc(phy);
  1577. Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
  1578. Mpi2SasPhyPage0_t phy_pg0;
  1579. Mpi2ConfigReply_t mpi_reply;
  1580. u16 ioc_status;
  1581. u16 sz;
  1582. int i;
  1583. int rc = 0;
  1584. unsigned long flags;
  1585. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  1586. if (_transport_sas_node_find_by_sas_address(ioc,
  1587. phy->identify.sas_address) == NULL) {
  1588. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  1589. return -EINVAL;
  1590. }
  1591. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  1592. if (!rates->minimum_linkrate)
  1593. rates->minimum_linkrate = phy->minimum_linkrate;
  1594. else if (rates->minimum_linkrate < phy->minimum_linkrate_hw)
  1595. rates->minimum_linkrate = phy->minimum_linkrate_hw;
  1596. if (!rates->maximum_linkrate)
  1597. rates->maximum_linkrate = phy->maximum_linkrate;
  1598. else if (rates->maximum_linkrate > phy->maximum_linkrate_hw)
  1599. rates->maximum_linkrate = phy->maximum_linkrate_hw;
  1600. /* handle expander phys */
  1601. if (phy->identify.sas_address != ioc->sas_hba.sas_address) {
  1602. phy->minimum_linkrate = rates->minimum_linkrate;
  1603. phy->maximum_linkrate = rates->maximum_linkrate;
  1604. return _transport_expander_phy_control(ioc, phy,
  1605. SMP_PHY_CONTROL_LINK_RESET);
  1606. }
  1607. /* handle hba phys */
  1608. /* sas_iounit page 1 */
  1609. sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
  1610. sizeof(Mpi2SasIOUnit1PhyData_t));
  1611. sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
  1612. if (!sas_iounit_pg1) {
  1613. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1614. ioc->name, __FILE__, __LINE__, __func__);
  1615. rc = -ENOMEM;
  1616. goto out;
  1617. }
  1618. if ((mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
  1619. sas_iounit_pg1, sz))) {
  1620. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1621. ioc->name, __FILE__, __LINE__, __func__);
  1622. rc = -ENXIO;
  1623. goto out;
  1624. }
  1625. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  1626. MPI2_IOCSTATUS_MASK;
  1627. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  1628. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1629. ioc->name, __FILE__, __LINE__, __func__);
  1630. rc = -EIO;
  1631. goto out;
  1632. }
  1633. for (i = 0; i < ioc->sas_hba.num_phys; i++) {
  1634. if (phy->number != i) {
  1635. sas_iounit_pg1->PhyData[i].MaxMinLinkRate =
  1636. (ioc->sas_hba.phy[i].phy->minimum_linkrate +
  1637. (ioc->sas_hba.phy[i].phy->maximum_linkrate << 4));
  1638. } else {
  1639. sas_iounit_pg1->PhyData[i].MaxMinLinkRate =
  1640. (rates->minimum_linkrate +
  1641. (rates->maximum_linkrate << 4));
  1642. }
  1643. }
  1644. if (mpt3sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1,
  1645. sz)) {
  1646. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1647. ioc->name, __FILE__, __LINE__, __func__);
  1648. rc = -ENXIO;
  1649. goto out;
  1650. }
  1651. /* link reset */
  1652. _transport_phy_reset(phy, 0);
  1653. /* read phy page 0, then update the rates in the sas transport phy */
  1654. if (!mpt3sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
  1655. phy->number)) {
  1656. phy->minimum_linkrate = _transport_convert_phy_link_rate(
  1657. phy_pg0.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
  1658. phy->maximum_linkrate = _transport_convert_phy_link_rate(
  1659. phy_pg0.ProgrammedLinkRate >> 4);
  1660. phy->negotiated_linkrate = _transport_convert_phy_link_rate(
  1661. phy_pg0.NegotiatedLinkRate &
  1662. MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
  1663. }
  1664. out:
  1665. kfree(sas_iounit_pg1);
  1666. return rc;
  1667. }
  1668. /**
  1669. * _transport_smp_handler - transport portal for smp passthru
  1670. * @shost: shost object
  1671. * @rphy: sas transport rphy object
  1672. * @req:
  1673. *
  1674. * This used primarily for smp_utils.
  1675. * Example:
  1676. * smp_rep_general /sys/class/bsg/expander-5:0
  1677. */
  1678. static int
  1679. _transport_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
  1680. struct request *req)
  1681. {
  1682. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  1683. Mpi2SmpPassthroughRequest_t *mpi_request;
  1684. Mpi2SmpPassthroughReply_t *mpi_reply;
  1685. int rc;
  1686. u16 smid;
  1687. u32 ioc_state;
  1688. unsigned long timeleft;
  1689. void *psge;
  1690. u8 issue_reset = 0;
  1691. dma_addr_t dma_addr_in = 0;
  1692. dma_addr_t dma_addr_out = 0;
  1693. dma_addr_t pci_dma_in = 0;
  1694. dma_addr_t pci_dma_out = 0;
  1695. void *pci_addr_in = NULL;
  1696. void *pci_addr_out = NULL;
  1697. u16 wait_state_count;
  1698. struct request *rsp = req->next_rq;
  1699. struct bio_vec bvec;
  1700. struct bvec_iter iter;
  1701. if (!rsp) {
  1702. pr_err(MPT3SAS_FMT "%s: the smp response space is missing\n",
  1703. ioc->name, __func__);
  1704. return -EINVAL;
  1705. }
  1706. if (ioc->shost_recovery || ioc->pci_error_recovery) {
  1707. pr_info(MPT3SAS_FMT "%s: host reset in progress!\n",
  1708. __func__, ioc->name);
  1709. return -EFAULT;
  1710. }
  1711. rc = mutex_lock_interruptible(&ioc->transport_cmds.mutex);
  1712. if (rc)
  1713. return rc;
  1714. if (ioc->transport_cmds.status != MPT3_CMD_NOT_USED) {
  1715. pr_err(MPT3SAS_FMT "%s: transport_cmds in use\n", ioc->name,
  1716. __func__);
  1717. rc = -EAGAIN;
  1718. goto out;
  1719. }
  1720. ioc->transport_cmds.status = MPT3_CMD_PENDING;
  1721. /* Check if the request is split across multiple segments */
  1722. if (bio_multiple_segments(req->bio)) {
  1723. u32 offset = 0;
  1724. /* Allocate memory and copy the request */
  1725. pci_addr_out = pci_alloc_consistent(ioc->pdev,
  1726. blk_rq_bytes(req), &pci_dma_out);
  1727. if (!pci_addr_out) {
  1728. pr_info(MPT3SAS_FMT "%s(): PCI Addr out = NULL\n",
  1729. ioc->name, __func__);
  1730. rc = -ENOMEM;
  1731. goto out;
  1732. }
  1733. bio_for_each_segment(bvec, req->bio, iter) {
  1734. memcpy(pci_addr_out + offset,
  1735. page_address(bvec.bv_page) + bvec.bv_offset,
  1736. bvec.bv_len);
  1737. offset += bvec.bv_len;
  1738. }
  1739. } else {
  1740. dma_addr_out = pci_map_single(ioc->pdev, bio_data(req->bio),
  1741. blk_rq_bytes(req), PCI_DMA_BIDIRECTIONAL);
  1742. if (!dma_addr_out) {
  1743. pr_info(MPT3SAS_FMT "%s(): DMA Addr out = NULL\n",
  1744. ioc->name, __func__);
  1745. rc = -ENOMEM;
  1746. goto free_pci;
  1747. }
  1748. }
  1749. /* Check if the response needs to be populated across
  1750. * multiple segments */
  1751. if (bio_multiple_segments(rsp->bio)) {
  1752. pci_addr_in = pci_alloc_consistent(ioc->pdev, blk_rq_bytes(rsp),
  1753. &pci_dma_in);
  1754. if (!pci_addr_in) {
  1755. pr_info(MPT3SAS_FMT "%s(): PCI Addr in = NULL\n",
  1756. ioc->name, __func__);
  1757. rc = -ENOMEM;
  1758. goto unmap;
  1759. }
  1760. } else {
  1761. dma_addr_in = pci_map_single(ioc->pdev, bio_data(rsp->bio),
  1762. blk_rq_bytes(rsp), PCI_DMA_BIDIRECTIONAL);
  1763. if (!dma_addr_in) {
  1764. pr_info(MPT3SAS_FMT "%s(): DMA Addr in = NULL\n",
  1765. ioc->name, __func__);
  1766. rc = -ENOMEM;
  1767. goto unmap;
  1768. }
  1769. }
  1770. wait_state_count = 0;
  1771. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  1772. while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  1773. if (wait_state_count++ == 10) {
  1774. pr_err(MPT3SAS_FMT
  1775. "%s: failed due to ioc not operational\n",
  1776. ioc->name, __func__);
  1777. rc = -EFAULT;
  1778. goto unmap;
  1779. }
  1780. ssleep(1);
  1781. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  1782. pr_info(MPT3SAS_FMT
  1783. "%s: waiting for operational state(count=%d)\n",
  1784. ioc->name, __func__, wait_state_count);
  1785. }
  1786. if (wait_state_count)
  1787. pr_info(MPT3SAS_FMT "%s: ioc is operational\n",
  1788. ioc->name, __func__);
  1789. smid = mpt3sas_base_get_smid(ioc, ioc->transport_cb_idx);
  1790. if (!smid) {
  1791. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  1792. ioc->name, __func__);
  1793. rc = -EAGAIN;
  1794. goto unmap;
  1795. }
  1796. rc = 0;
  1797. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  1798. ioc->transport_cmds.smid = smid;
  1799. memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
  1800. mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
  1801. mpi_request->PhysicalPort = 0xFF;
  1802. mpi_request->SASAddress = (rphy) ?
  1803. cpu_to_le64(rphy->identify.sas_address) :
  1804. cpu_to_le64(ioc->sas_hba.sas_address);
  1805. mpi_request->RequestDataLength = cpu_to_le16(blk_rq_bytes(req) - 4);
  1806. psge = &mpi_request->SGL;
  1807. if (bio_multiple_segments(req->bio))
  1808. ioc->build_sg(ioc, psge, pci_dma_out, (blk_rq_bytes(req) - 4),
  1809. pci_dma_in, (blk_rq_bytes(rsp) + 4));
  1810. else
  1811. ioc->build_sg(ioc, psge, dma_addr_out, (blk_rq_bytes(req) - 4),
  1812. dma_addr_in, (blk_rq_bytes(rsp) + 4));
  1813. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1814. "%s - sending smp request\n", ioc->name, __func__));
  1815. init_completion(&ioc->transport_cmds.done);
  1816. mpt3sas_base_put_smid_default(ioc, smid);
  1817. timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done,
  1818. 10*HZ);
  1819. if (!(ioc->transport_cmds.status & MPT3_CMD_COMPLETE)) {
  1820. pr_err(MPT3SAS_FMT "%s : timeout\n",
  1821. __func__, ioc->name);
  1822. _debug_dump_mf(mpi_request,
  1823. sizeof(Mpi2SmpPassthroughRequest_t)/4);
  1824. if (!(ioc->transport_cmds.status & MPT3_CMD_RESET))
  1825. issue_reset = 1;
  1826. goto issue_host_reset;
  1827. }
  1828. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1829. "%s - complete\n", ioc->name, __func__));
  1830. if (ioc->transport_cmds.status & MPT3_CMD_REPLY_VALID) {
  1831. mpi_reply = ioc->transport_cmds.reply;
  1832. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1833. "%s - reply data transfer size(%d)\n",
  1834. ioc->name, __func__,
  1835. le16_to_cpu(mpi_reply->ResponseDataLength)));
  1836. memcpy(req->sense, mpi_reply, sizeof(*mpi_reply));
  1837. req->sense_len = sizeof(*mpi_reply);
  1838. req->resid_len = 0;
  1839. rsp->resid_len -=
  1840. le16_to_cpu(mpi_reply->ResponseDataLength);
  1841. /* check if the resp needs to be copied from the allocated
  1842. * pci mem */
  1843. if (bio_multiple_segments(rsp->bio)) {
  1844. u32 offset = 0;
  1845. u32 bytes_to_copy =
  1846. le16_to_cpu(mpi_reply->ResponseDataLength);
  1847. bio_for_each_segment(bvec, rsp->bio, iter) {
  1848. if (bytes_to_copy <= bvec.bv_len) {
  1849. memcpy(page_address(bvec.bv_page) +
  1850. bvec.bv_offset, pci_addr_in +
  1851. offset, bytes_to_copy);
  1852. break;
  1853. } else {
  1854. memcpy(page_address(bvec.bv_page) +
  1855. bvec.bv_offset, pci_addr_in +
  1856. offset, bvec.bv_len);
  1857. bytes_to_copy -= bvec.bv_len;
  1858. }
  1859. offset += bvec.bv_len;
  1860. }
  1861. }
  1862. } else {
  1863. dtransportprintk(ioc, pr_info(MPT3SAS_FMT
  1864. "%s - no reply\n", ioc->name, __func__));
  1865. rc = -ENXIO;
  1866. }
  1867. issue_host_reset:
  1868. if (issue_reset) {
  1869. mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  1870. FORCE_BIG_HAMMER);
  1871. rc = -ETIMEDOUT;
  1872. }
  1873. unmap:
  1874. if (dma_addr_out)
  1875. pci_unmap_single(ioc->pdev, dma_addr_out, blk_rq_bytes(req),
  1876. PCI_DMA_BIDIRECTIONAL);
  1877. if (dma_addr_in)
  1878. pci_unmap_single(ioc->pdev, dma_addr_in, blk_rq_bytes(rsp),
  1879. PCI_DMA_BIDIRECTIONAL);
  1880. free_pci:
  1881. if (pci_addr_out)
  1882. pci_free_consistent(ioc->pdev, blk_rq_bytes(req), pci_addr_out,
  1883. pci_dma_out);
  1884. if (pci_addr_in)
  1885. pci_free_consistent(ioc->pdev, blk_rq_bytes(rsp), pci_addr_in,
  1886. pci_dma_in);
  1887. out:
  1888. ioc->transport_cmds.status = MPT3_CMD_NOT_USED;
  1889. mutex_unlock(&ioc->transport_cmds.mutex);
  1890. return rc;
  1891. }
  1892. struct sas_function_template mpt3sas_transport_functions = {
  1893. .get_linkerrors = _transport_get_linkerrors,
  1894. .get_enclosure_identifier = _transport_get_enclosure_identifier,
  1895. .get_bay_identifier = _transport_get_bay_identifier,
  1896. .phy_reset = _transport_phy_reset,
  1897. .phy_enable = _transport_phy_enable,
  1898. .set_phy_speed = _transport_phy_speed,
  1899. .smp_handler = _transport_smp_handler,
  1900. };
  1901. struct scsi_transport_template *mpt3sas_transport_template;