mpt2sas_transport.c 64 KB

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