mpt2sas_transport.c 44 KB

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