libiscsi.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047
  1. /*
  2. * iSCSI lib functions
  3. *
  4. * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
  5. * Copyright (C) 2004 - 2006 Mike Christie
  6. * Copyright (C) 2004 - 2005 Dmitry Yusupov
  7. * Copyright (C) 2004 - 2005 Alex Aizman
  8. * maintained by open-iscsi@googlegroups.com
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23. */
  24. #include <linux/types.h>
  25. #include <linux/mutex.h>
  26. #include <linux/kfifo.h>
  27. #include <linux/delay.h>
  28. #include <asm/unaligned.h>
  29. #include <net/tcp.h>
  30. #include <scsi/scsi_cmnd.h>
  31. #include <scsi/scsi_device.h>
  32. #include <scsi/scsi_eh.h>
  33. #include <scsi/scsi_tcq.h>
  34. #include <scsi/scsi_host.h>
  35. #include <scsi/scsi.h>
  36. #include <scsi/iscsi_proto.h>
  37. #include <scsi/scsi_transport.h>
  38. #include <scsi/scsi_transport_iscsi.h>
  39. #include <scsi/libiscsi.h>
  40. struct iscsi_session *
  41. class_to_transport_session(struct iscsi_cls_session *cls_session)
  42. {
  43. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  44. return iscsi_hostdata(shost->hostdata);
  45. }
  46. EXPORT_SYMBOL_GPL(class_to_transport_session);
  47. #define INVALID_SN_DELTA 0xffff
  48. int
  49. iscsi_check_assign_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
  50. {
  51. uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
  52. uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
  53. if (max_cmdsn < exp_cmdsn -1 &&
  54. max_cmdsn > exp_cmdsn - INVALID_SN_DELTA)
  55. return ISCSI_ERR_MAX_CMDSN;
  56. if (max_cmdsn > session->max_cmdsn ||
  57. max_cmdsn < session->max_cmdsn - INVALID_SN_DELTA)
  58. session->max_cmdsn = max_cmdsn;
  59. if (exp_cmdsn > session->exp_cmdsn ||
  60. exp_cmdsn < session->exp_cmdsn - INVALID_SN_DELTA)
  61. session->exp_cmdsn = exp_cmdsn;
  62. return 0;
  63. }
  64. EXPORT_SYMBOL_GPL(iscsi_check_assign_cmdsn);
  65. void iscsi_prep_unsolicit_data_pdu(struct iscsi_cmd_task *ctask,
  66. struct iscsi_data *hdr)
  67. {
  68. struct iscsi_conn *conn = ctask->conn;
  69. memset(hdr, 0, sizeof(struct iscsi_data));
  70. hdr->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
  71. hdr->datasn = cpu_to_be32(ctask->unsol_datasn);
  72. ctask->unsol_datasn++;
  73. hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
  74. memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
  75. hdr->itt = ctask->hdr->itt;
  76. hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
  77. hdr->offset = cpu_to_be32(ctask->unsol_offset);
  78. if (ctask->unsol_count > conn->max_xmit_dlength) {
  79. hton24(hdr->dlength, conn->max_xmit_dlength);
  80. ctask->data_count = conn->max_xmit_dlength;
  81. ctask->unsol_offset += ctask->data_count;
  82. hdr->flags = 0;
  83. } else {
  84. hton24(hdr->dlength, ctask->unsol_count);
  85. ctask->data_count = ctask->unsol_count;
  86. hdr->flags = ISCSI_FLAG_CMD_FINAL;
  87. }
  88. }
  89. EXPORT_SYMBOL_GPL(iscsi_prep_unsolicit_data_pdu);
  90. /**
  91. * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
  92. * @ctask: iscsi cmd task
  93. *
  94. * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
  95. * fields like dlength or final based on how much data it sends
  96. */
  97. static void iscsi_prep_scsi_cmd_pdu(struct iscsi_cmd_task *ctask)
  98. {
  99. struct iscsi_conn *conn = ctask->conn;
  100. struct iscsi_session *session = conn->session;
  101. struct iscsi_cmd *hdr = ctask->hdr;
  102. struct scsi_cmnd *sc = ctask->sc;
  103. hdr->opcode = ISCSI_OP_SCSI_CMD;
  104. hdr->flags = ISCSI_ATTR_SIMPLE;
  105. int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
  106. hdr->itt = build_itt(ctask->itt, conn->id, session->age);
  107. hdr->data_length = cpu_to_be32(sc->request_bufflen);
  108. hdr->cmdsn = cpu_to_be32(session->cmdsn);
  109. session->cmdsn++;
  110. hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
  111. memcpy(hdr->cdb, sc->cmnd, sc->cmd_len);
  112. memset(&hdr->cdb[sc->cmd_len], 0, MAX_COMMAND_SIZE - sc->cmd_len);
  113. ctask->data_count = 0;
  114. if (sc->sc_data_direction == DMA_TO_DEVICE) {
  115. hdr->flags |= ISCSI_FLAG_CMD_WRITE;
  116. /*
  117. * Write counters:
  118. *
  119. * imm_count bytes to be sent right after
  120. * SCSI PDU Header
  121. *
  122. * unsol_count bytes(as Data-Out) to be sent
  123. * without R2T ack right after
  124. * immediate data
  125. *
  126. * r2t_data_count bytes to be sent via R2T ack's
  127. *
  128. * pad_count bytes to be sent as zero-padding
  129. */
  130. ctask->imm_count = 0;
  131. ctask->unsol_count = 0;
  132. ctask->unsol_offset = 0;
  133. ctask->unsol_datasn = 0;
  134. if (session->imm_data_en) {
  135. if (ctask->total_length >= session->first_burst)
  136. ctask->imm_count = min(session->first_burst,
  137. conn->max_xmit_dlength);
  138. else
  139. ctask->imm_count = min(ctask->total_length,
  140. conn->max_xmit_dlength);
  141. hton24(ctask->hdr->dlength, ctask->imm_count);
  142. } else
  143. zero_data(ctask->hdr->dlength);
  144. if (!session->initial_r2t_en) {
  145. ctask->unsol_count = min(session->first_burst,
  146. ctask->total_length) - ctask->imm_count;
  147. ctask->unsol_offset = ctask->imm_count;
  148. }
  149. if (!ctask->unsol_count)
  150. /* No unsolicit Data-Out's */
  151. ctask->hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  152. } else {
  153. ctask->datasn = 0;
  154. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  155. zero_data(hdr->dlength);
  156. if (sc->sc_data_direction == DMA_FROM_DEVICE)
  157. hdr->flags |= ISCSI_FLAG_CMD_READ;
  158. }
  159. conn->scsicmd_pdus_cnt++;
  160. }
  161. EXPORT_SYMBOL_GPL(iscsi_prep_scsi_cmd_pdu);
  162. /**
  163. * iscsi_complete_command - return command back to scsi-ml
  164. * @ctask: iscsi cmd task
  165. *
  166. * Must be called with session lock.
  167. * This function returns the scsi command to scsi-ml and returns
  168. * the cmd task to the pool of available cmd tasks.
  169. */
  170. static void iscsi_complete_command(struct iscsi_cmd_task *ctask)
  171. {
  172. struct iscsi_session *session = ctask->conn->session;
  173. struct scsi_cmnd *sc = ctask->sc;
  174. ctask->state = ISCSI_TASK_COMPLETED;
  175. ctask->sc = NULL;
  176. /* SCSI eh reuses commands to verify us */
  177. sc->SCp.ptr = NULL;
  178. list_del_init(&ctask->running);
  179. __kfifo_put(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
  180. sc->scsi_done(sc);
  181. }
  182. static void __iscsi_get_ctask(struct iscsi_cmd_task *ctask)
  183. {
  184. atomic_inc(&ctask->refcount);
  185. }
  186. static void iscsi_get_ctask(struct iscsi_cmd_task *ctask)
  187. {
  188. spin_lock_bh(&ctask->conn->session->lock);
  189. __iscsi_get_ctask(ctask);
  190. spin_unlock_bh(&ctask->conn->session->lock);
  191. }
  192. static void __iscsi_put_ctask(struct iscsi_cmd_task *ctask)
  193. {
  194. if (atomic_dec_and_test(&ctask->refcount))
  195. iscsi_complete_command(ctask);
  196. }
  197. static void iscsi_put_ctask(struct iscsi_cmd_task *ctask)
  198. {
  199. spin_lock_bh(&ctask->conn->session->lock);
  200. __iscsi_put_ctask(ctask);
  201. spin_unlock_bh(&ctask->conn->session->lock);
  202. }
  203. /**
  204. * iscsi_cmd_rsp - SCSI Command Response processing
  205. * @conn: iscsi connection
  206. * @hdr: iscsi header
  207. * @ctask: scsi command task
  208. * @data: cmd data buffer
  209. * @datalen: len of buffer
  210. *
  211. * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
  212. * then completes the command and task.
  213. **/
  214. static int iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  215. struct iscsi_cmd_task *ctask, char *data,
  216. int datalen)
  217. {
  218. int rc;
  219. struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
  220. struct iscsi_session *session = conn->session;
  221. struct scsi_cmnd *sc = ctask->sc;
  222. rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr);
  223. if (rc) {
  224. sc->result = DID_ERROR << 16;
  225. goto out;
  226. }
  227. conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
  228. sc->result = (DID_OK << 16) | rhdr->cmd_status;
  229. if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
  230. sc->result = DID_ERROR << 16;
  231. goto out;
  232. }
  233. if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
  234. uint16_t senselen;
  235. if (datalen < 2) {
  236. invalid_datalen:
  237. printk(KERN_ERR "iscsi: Got CHECK_CONDITION but "
  238. "invalid data buffer size of %d\n", datalen);
  239. sc->result = DID_BAD_TARGET << 16;
  240. goto out;
  241. }
  242. senselen = be16_to_cpu(get_unaligned((__be16 *) data));
  243. if (datalen < senselen)
  244. goto invalid_datalen;
  245. memcpy(sc->sense_buffer, data + 2,
  246. min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
  247. debug_scsi("copied %d bytes of sense\n",
  248. min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
  249. }
  250. if (sc->sc_data_direction == DMA_TO_DEVICE)
  251. goto out;
  252. if (rhdr->flags & ISCSI_FLAG_CMD_UNDERFLOW) {
  253. int res_count = be32_to_cpu(rhdr->residual_count);
  254. if (res_count > 0 && res_count <= sc->request_bufflen)
  255. sc->resid = res_count;
  256. else
  257. sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
  258. } else if (rhdr->flags & ISCSI_FLAG_CMD_BIDI_UNDERFLOW)
  259. sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
  260. else if (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW)
  261. sc->resid = be32_to_cpu(rhdr->residual_count);
  262. out:
  263. debug_scsi("done [sc %lx res %d itt 0x%x]\n",
  264. (long)sc, sc->result, ctask->itt);
  265. conn->scsirsp_pdus_cnt++;
  266. __iscsi_put_ctask(ctask);
  267. return rc;
  268. }
  269. static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
  270. {
  271. struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
  272. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  273. conn->tmfrsp_pdus_cnt++;
  274. if (conn->tmabort_state != TMABORT_INITIAL)
  275. return;
  276. if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
  277. conn->tmabort_state = TMABORT_SUCCESS;
  278. else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
  279. conn->tmabort_state = TMABORT_NOT_FOUND;
  280. else
  281. conn->tmabort_state = TMABORT_FAILED;
  282. wake_up(&conn->ehwait);
  283. }
  284. static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  285. char *data, int datalen)
  286. {
  287. struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
  288. struct iscsi_hdr rejected_pdu;
  289. uint32_t itt;
  290. conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
  291. if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
  292. if (ntoh24(reject->dlength) > datalen)
  293. return ISCSI_ERR_PROTO;
  294. if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
  295. memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
  296. itt = get_itt(rejected_pdu.itt);
  297. printk(KERN_ERR "itt 0x%x had pdu (op 0x%x) rejected "
  298. "due to DataDigest error.\n", itt,
  299. rejected_pdu.opcode);
  300. }
  301. }
  302. return 0;
  303. }
  304. /**
  305. * __iscsi_complete_pdu - complete pdu
  306. * @conn: iscsi conn
  307. * @hdr: iscsi header
  308. * @data: data buffer
  309. * @datalen: len of data buffer
  310. *
  311. * Completes pdu processing by freeing any resources allocated at
  312. * queuecommand or send generic. session lock must be held and verify
  313. * itt must have been called.
  314. */
  315. int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  316. char *data, int datalen)
  317. {
  318. struct iscsi_session *session = conn->session;
  319. int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
  320. struct iscsi_cmd_task *ctask;
  321. struct iscsi_mgmt_task *mtask;
  322. uint32_t itt;
  323. if (hdr->itt != RESERVED_ITT)
  324. itt = get_itt(hdr->itt);
  325. else
  326. itt = ~0U;
  327. if (itt < session->cmds_max) {
  328. ctask = session->cmds[itt];
  329. debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n",
  330. opcode, conn->id, ctask->itt, datalen);
  331. switch(opcode) {
  332. case ISCSI_OP_SCSI_CMD_RSP:
  333. BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
  334. rc = iscsi_scsi_cmd_rsp(conn, hdr, ctask, data,
  335. datalen);
  336. break;
  337. case ISCSI_OP_SCSI_DATA_IN:
  338. BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
  339. if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
  340. conn->scsirsp_pdus_cnt++;
  341. __iscsi_put_ctask(ctask);
  342. }
  343. break;
  344. case ISCSI_OP_R2T:
  345. /* LLD handles this for now */
  346. break;
  347. default:
  348. rc = ISCSI_ERR_BAD_OPCODE;
  349. break;
  350. }
  351. } else if (itt >= ISCSI_MGMT_ITT_OFFSET &&
  352. itt < ISCSI_MGMT_ITT_OFFSET + session->mgmtpool_max) {
  353. mtask = session->mgmt_cmds[itt - ISCSI_MGMT_ITT_OFFSET];
  354. debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
  355. opcode, conn->id, mtask->itt, datalen);
  356. rc = iscsi_check_assign_cmdsn(session,
  357. (struct iscsi_nopin*)hdr);
  358. if (rc)
  359. goto done;
  360. switch(opcode) {
  361. case ISCSI_OP_LOGOUT_RSP:
  362. if (datalen) {
  363. rc = ISCSI_ERR_PROTO;
  364. break;
  365. }
  366. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  367. /* fall through */
  368. case ISCSI_OP_LOGIN_RSP:
  369. case ISCSI_OP_TEXT_RSP:
  370. /*
  371. * login related PDU's exp_statsn is handled in
  372. * userspace
  373. */
  374. if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
  375. rc = ISCSI_ERR_CONN_FAILED;
  376. list_del(&mtask->running);
  377. if (conn->login_mtask != mtask)
  378. __kfifo_put(session->mgmtpool.queue,
  379. (void*)&mtask, sizeof(void*));
  380. break;
  381. case ISCSI_OP_SCSI_TMFUNC_RSP:
  382. if (datalen) {
  383. rc = ISCSI_ERR_PROTO;
  384. break;
  385. }
  386. iscsi_tmf_rsp(conn, hdr);
  387. break;
  388. case ISCSI_OP_NOOP_IN:
  389. if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
  390. rc = ISCSI_ERR_PROTO;
  391. break;
  392. }
  393. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  394. if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
  395. rc = ISCSI_ERR_CONN_FAILED;
  396. list_del(&mtask->running);
  397. if (conn->login_mtask != mtask)
  398. __kfifo_put(session->mgmtpool.queue,
  399. (void*)&mtask, sizeof(void*));
  400. break;
  401. default:
  402. rc = ISCSI_ERR_BAD_OPCODE;
  403. break;
  404. }
  405. } else if (itt == ~0U) {
  406. rc = iscsi_check_assign_cmdsn(session,
  407. (struct iscsi_nopin*)hdr);
  408. if (rc)
  409. goto done;
  410. switch(opcode) {
  411. case ISCSI_OP_NOOP_IN:
  412. if (datalen) {
  413. rc = ISCSI_ERR_PROTO;
  414. break;
  415. }
  416. if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
  417. break;
  418. if (iscsi_recv_pdu(conn->cls_conn, hdr, NULL, 0))
  419. rc = ISCSI_ERR_CONN_FAILED;
  420. break;
  421. case ISCSI_OP_REJECT:
  422. rc = iscsi_handle_reject(conn, hdr, data, datalen);
  423. break;
  424. case ISCSI_OP_ASYNC_EVENT:
  425. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  426. if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
  427. rc = ISCSI_ERR_CONN_FAILED;
  428. break;
  429. default:
  430. rc = ISCSI_ERR_BAD_OPCODE;
  431. break;
  432. }
  433. } else
  434. rc = ISCSI_ERR_BAD_ITT;
  435. done:
  436. return rc;
  437. }
  438. EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
  439. int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  440. char *data, int datalen)
  441. {
  442. int rc;
  443. spin_lock(&conn->session->lock);
  444. rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
  445. spin_unlock(&conn->session->lock);
  446. return rc;
  447. }
  448. EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
  449. /* verify itt (itt encoding: age+cid+itt) */
  450. int iscsi_verify_itt(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  451. uint32_t *ret_itt)
  452. {
  453. struct iscsi_session *session = conn->session;
  454. struct iscsi_cmd_task *ctask;
  455. uint32_t itt;
  456. if (hdr->itt != RESERVED_ITT) {
  457. if (((__force u32)hdr->itt & ISCSI_AGE_MASK) !=
  458. (session->age << ISCSI_AGE_SHIFT)) {
  459. printk(KERN_ERR "iscsi: received itt %x expected "
  460. "session age (%x)\n", (__force u32)hdr->itt,
  461. session->age & ISCSI_AGE_MASK);
  462. return ISCSI_ERR_BAD_ITT;
  463. }
  464. if (((__force u32)hdr->itt & ISCSI_CID_MASK) !=
  465. (conn->id << ISCSI_CID_SHIFT)) {
  466. printk(KERN_ERR "iscsi: received itt %x, expected "
  467. "CID (%x)\n", (__force u32)hdr->itt, conn->id);
  468. return ISCSI_ERR_BAD_ITT;
  469. }
  470. itt = get_itt(hdr->itt);
  471. } else
  472. itt = ~0U;
  473. if (itt < session->cmds_max) {
  474. ctask = session->cmds[itt];
  475. if (!ctask->sc) {
  476. printk(KERN_INFO "iscsi: dropping ctask with "
  477. "itt 0x%x\n", ctask->itt);
  478. /* force drop */
  479. return ISCSI_ERR_NO_SCSI_CMD;
  480. }
  481. if (ctask->sc->SCp.phase != session->age) {
  482. printk(KERN_ERR "iscsi: ctask's session age %d, "
  483. "expected %d\n", ctask->sc->SCp.phase,
  484. session->age);
  485. return ISCSI_ERR_SESSION_FAILED;
  486. }
  487. }
  488. *ret_itt = itt;
  489. return 0;
  490. }
  491. EXPORT_SYMBOL_GPL(iscsi_verify_itt);
  492. void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
  493. {
  494. struct iscsi_session *session = conn->session;
  495. unsigned long flags;
  496. spin_lock_irqsave(&session->lock, flags);
  497. if (session->state == ISCSI_STATE_FAILED) {
  498. spin_unlock_irqrestore(&session->lock, flags);
  499. return;
  500. }
  501. if (conn->stop_stage == 0)
  502. session->state = ISCSI_STATE_FAILED;
  503. spin_unlock_irqrestore(&session->lock, flags);
  504. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  505. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  506. iscsi_conn_error(conn->cls_conn, err);
  507. }
  508. EXPORT_SYMBOL_GPL(iscsi_conn_failure);
  509. static int iscsi_xmit_mtask(struct iscsi_conn *conn)
  510. {
  511. struct iscsi_hdr *hdr = conn->mtask->hdr;
  512. int rc, was_logout = 0;
  513. if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT) {
  514. conn->session->state = ISCSI_STATE_IN_RECOVERY;
  515. iscsi_block_session(session_to_cls(conn->session));
  516. was_logout = 1;
  517. }
  518. rc = conn->session->tt->xmit_mgmt_task(conn, conn->mtask);
  519. if (rc)
  520. return rc;
  521. /* done with this in-progress mtask */
  522. conn->mtask = NULL;
  523. if (was_logout) {
  524. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  525. return -ENODATA;
  526. }
  527. return 0;
  528. }
  529. /**
  530. * iscsi_data_xmit - xmit any command into the scheduled connection
  531. * @conn: iscsi connection
  532. *
  533. * Notes:
  534. * The function can return -EAGAIN in which case the caller must
  535. * re-schedule it again later or recover. '0' return code means
  536. * successful xmit.
  537. **/
  538. static int iscsi_data_xmit(struct iscsi_conn *conn)
  539. {
  540. struct iscsi_transport *tt;
  541. int rc = 0;
  542. if (unlikely(conn->suspend_tx)) {
  543. debug_scsi("conn %d Tx suspended!\n", conn->id);
  544. return -ENODATA;
  545. }
  546. tt = conn->session->tt;
  547. /*
  548. * Transmit in the following order:
  549. *
  550. * 1) un-finished xmit (ctask or mtask)
  551. * 2) immediate control PDUs
  552. * 3) write data
  553. * 4) SCSI commands
  554. * 5) non-immediate control PDUs
  555. *
  556. * No need to lock around __kfifo_get as long as
  557. * there's one producer and one consumer.
  558. */
  559. BUG_ON(conn->ctask && conn->mtask);
  560. if (conn->ctask) {
  561. iscsi_get_ctask(conn->ctask);
  562. rc = tt->xmit_cmd_task(conn, conn->ctask);
  563. iscsi_put_ctask(conn->ctask);
  564. if (rc)
  565. goto again;
  566. /* done with this in-progress ctask */
  567. conn->ctask = NULL;
  568. }
  569. if (conn->mtask) {
  570. rc = iscsi_xmit_mtask(conn);
  571. if (rc)
  572. goto again;
  573. }
  574. /* process immediate first */
  575. if (unlikely(__kfifo_len(conn->immqueue))) {
  576. while (__kfifo_get(conn->immqueue, (void*)&conn->mtask,
  577. sizeof(void*))) {
  578. spin_lock_bh(&conn->session->lock);
  579. list_add_tail(&conn->mtask->running,
  580. &conn->mgmt_run_list);
  581. spin_unlock_bh(&conn->session->lock);
  582. rc = iscsi_xmit_mtask(conn);
  583. if (rc)
  584. goto again;
  585. }
  586. }
  587. /* process command queue */
  588. spin_lock_bh(&conn->session->lock);
  589. while (!list_empty(&conn->xmitqueue)) {
  590. /*
  591. * iscsi tcp may readd the task to the xmitqueue to send
  592. * write data
  593. */
  594. conn->ctask = list_entry(conn->xmitqueue.next,
  595. struct iscsi_cmd_task, running);
  596. conn->ctask->state = ISCSI_TASK_RUNNING;
  597. list_move_tail(conn->xmitqueue.next, &conn->run_list);
  598. __iscsi_get_ctask(conn->ctask);
  599. spin_unlock_bh(&conn->session->lock);
  600. rc = tt->xmit_cmd_task(conn, conn->ctask);
  601. spin_lock_bh(&conn->session->lock);
  602. __iscsi_put_ctask(conn->ctask);
  603. if (rc) {
  604. spin_unlock_bh(&conn->session->lock);
  605. goto again;
  606. }
  607. }
  608. spin_unlock_bh(&conn->session->lock);
  609. /* done with this ctask */
  610. conn->ctask = NULL;
  611. /* process the rest control plane PDUs, if any */
  612. if (unlikely(__kfifo_len(conn->mgmtqueue))) {
  613. while (__kfifo_get(conn->mgmtqueue, (void*)&conn->mtask,
  614. sizeof(void*))) {
  615. spin_lock_bh(&conn->session->lock);
  616. list_add_tail(&conn->mtask->running,
  617. &conn->mgmt_run_list);
  618. spin_unlock_bh(&conn->session->lock);
  619. rc = iscsi_xmit_mtask(conn);
  620. if (rc)
  621. goto again;
  622. }
  623. }
  624. return -ENODATA;
  625. again:
  626. if (unlikely(conn->suspend_tx))
  627. return -ENODATA;
  628. return rc;
  629. }
  630. static void iscsi_xmitworker(struct work_struct *work)
  631. {
  632. struct iscsi_conn *conn =
  633. container_of(work, struct iscsi_conn, xmitwork);
  634. int rc;
  635. /*
  636. * serialize Xmit worker on a per-connection basis.
  637. */
  638. mutex_lock(&conn->xmitmutex);
  639. do {
  640. rc = iscsi_data_xmit(conn);
  641. } while (rc >= 0 || rc == -EAGAIN);
  642. mutex_unlock(&conn->xmitmutex);
  643. }
  644. enum {
  645. FAILURE_BAD_HOST = 1,
  646. FAILURE_SESSION_FAILED,
  647. FAILURE_SESSION_FREED,
  648. FAILURE_WINDOW_CLOSED,
  649. FAILURE_OOM,
  650. FAILURE_SESSION_TERMINATE,
  651. FAILURE_SESSION_IN_RECOVERY,
  652. FAILURE_SESSION_RECOVERY_TIMEOUT,
  653. };
  654. int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
  655. {
  656. struct Scsi_Host *host;
  657. int reason = 0;
  658. struct iscsi_session *session;
  659. struct iscsi_conn *conn;
  660. struct iscsi_cmd_task *ctask = NULL;
  661. sc->scsi_done = done;
  662. sc->result = 0;
  663. sc->SCp.ptr = NULL;
  664. host = sc->device->host;
  665. session = iscsi_hostdata(host->hostdata);
  666. spin_lock(&session->lock);
  667. /*
  668. * ISCSI_STATE_FAILED is a temp. state. The recovery
  669. * code will decide what is best to do with command queued
  670. * during this time
  671. */
  672. if (session->state != ISCSI_STATE_LOGGED_IN &&
  673. session->state != ISCSI_STATE_FAILED) {
  674. /*
  675. * to handle the race between when we set the recovery state
  676. * and block the session we requeue here (commands could
  677. * be entering our queuecommand while a block is starting
  678. * up because the block code is not locked)
  679. */
  680. if (session->state == ISCSI_STATE_IN_RECOVERY) {
  681. reason = FAILURE_SESSION_IN_RECOVERY;
  682. goto reject;
  683. }
  684. if (session->state == ISCSI_STATE_RECOVERY_FAILED)
  685. reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
  686. else if (session->state == ISCSI_STATE_TERMINATE)
  687. reason = FAILURE_SESSION_TERMINATE;
  688. else
  689. reason = FAILURE_SESSION_FREED;
  690. goto fault;
  691. }
  692. /*
  693. * Check for iSCSI window and take care of CmdSN wrap-around
  694. */
  695. if ((int)(session->max_cmdsn - session->cmdsn) < 0) {
  696. reason = FAILURE_WINDOW_CLOSED;
  697. goto reject;
  698. }
  699. conn = session->leadconn;
  700. if (!conn) {
  701. reason = FAILURE_SESSION_FREED;
  702. goto fault;
  703. }
  704. if (!__kfifo_get(session->cmdpool.queue, (void*)&ctask,
  705. sizeof(void*))) {
  706. reason = FAILURE_OOM;
  707. goto reject;
  708. }
  709. sc->SCp.phase = session->age;
  710. sc->SCp.ptr = (char *)ctask;
  711. atomic_set(&ctask->refcount, 1);
  712. ctask->state = ISCSI_TASK_PENDING;
  713. ctask->mtask = NULL;
  714. ctask->conn = conn;
  715. ctask->sc = sc;
  716. INIT_LIST_HEAD(&ctask->running);
  717. ctask->total_length = sc->request_bufflen;
  718. iscsi_prep_scsi_cmd_pdu(ctask);
  719. session->tt->init_cmd_task(ctask);
  720. list_add_tail(&ctask->running, &conn->xmitqueue);
  721. debug_scsi(
  722. "ctask enq [%s cid %d sc %p cdb 0x%x itt 0x%x len %d cmdsn %d "
  723. "win %d]\n",
  724. sc->sc_data_direction == DMA_TO_DEVICE ? "write" : "read",
  725. conn->id, sc, sc->cmnd[0], ctask->itt, sc->request_bufflen,
  726. session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
  727. spin_unlock(&session->lock);
  728. scsi_queue_work(host, &conn->xmitwork);
  729. return 0;
  730. reject:
  731. spin_unlock(&session->lock);
  732. debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
  733. return SCSI_MLQUEUE_HOST_BUSY;
  734. fault:
  735. spin_unlock(&session->lock);
  736. printk(KERN_ERR "iscsi: cmd 0x%x is not queued (%d)\n",
  737. sc->cmnd[0], reason);
  738. sc->result = (DID_NO_CONNECT << 16);
  739. sc->resid = sc->request_bufflen;
  740. sc->scsi_done(sc);
  741. return 0;
  742. }
  743. EXPORT_SYMBOL_GPL(iscsi_queuecommand);
  744. int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
  745. {
  746. if (depth > ISCSI_MAX_CMD_PER_LUN)
  747. depth = ISCSI_MAX_CMD_PER_LUN;
  748. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
  749. return sdev->queue_depth;
  750. }
  751. EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
  752. static int
  753. iscsi_conn_send_generic(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  754. char *data, uint32_t data_size)
  755. {
  756. struct iscsi_session *session = conn->session;
  757. struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
  758. struct iscsi_mgmt_task *mtask;
  759. spin_lock_bh(&session->lock);
  760. if (session->state == ISCSI_STATE_TERMINATE) {
  761. spin_unlock_bh(&session->lock);
  762. return -EPERM;
  763. }
  764. if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
  765. hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
  766. /*
  767. * Login and Text are sent serially, in
  768. * request-followed-by-response sequence.
  769. * Same mtask can be used. Same ITT must be used.
  770. * Note that login_mtask is preallocated at conn_create().
  771. */
  772. mtask = conn->login_mtask;
  773. else {
  774. BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
  775. BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
  776. nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
  777. if (!__kfifo_get(session->mgmtpool.queue,
  778. (void*)&mtask, sizeof(void*))) {
  779. spin_unlock_bh(&session->lock);
  780. return -ENOSPC;
  781. }
  782. }
  783. /*
  784. * pre-format CmdSN for outgoing PDU.
  785. */
  786. if (hdr->itt != RESERVED_ITT) {
  787. hdr->itt = build_itt(mtask->itt, conn->id, session->age);
  788. nop->cmdsn = cpu_to_be32(session->cmdsn);
  789. if (conn->c_stage == ISCSI_CONN_STARTED &&
  790. !(hdr->opcode & ISCSI_OP_IMMEDIATE))
  791. session->cmdsn++;
  792. } else
  793. /* do not advance CmdSN */
  794. nop->cmdsn = cpu_to_be32(session->cmdsn);
  795. if (data_size) {
  796. memcpy(mtask->data, data, data_size);
  797. mtask->data_count = data_size;
  798. } else
  799. mtask->data_count = 0;
  800. INIT_LIST_HEAD(&mtask->running);
  801. memcpy(mtask->hdr, hdr, sizeof(struct iscsi_hdr));
  802. if (session->tt->init_mgmt_task)
  803. session->tt->init_mgmt_task(conn, mtask, data, data_size);
  804. spin_unlock_bh(&session->lock);
  805. debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
  806. hdr->opcode, hdr->itt, data_size);
  807. /*
  808. * since send_pdu() could be called at least from two contexts,
  809. * we need to serialize __kfifo_put, so we don't have to take
  810. * additional lock on fast data-path
  811. */
  812. if (hdr->opcode & ISCSI_OP_IMMEDIATE)
  813. __kfifo_put(conn->immqueue, (void*)&mtask, sizeof(void*));
  814. else
  815. __kfifo_put(conn->mgmtqueue, (void*)&mtask, sizeof(void*));
  816. scsi_queue_work(session->host, &conn->xmitwork);
  817. return 0;
  818. }
  819. int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
  820. char *data, uint32_t data_size)
  821. {
  822. struct iscsi_conn *conn = cls_conn->dd_data;
  823. int rc;
  824. mutex_lock(&conn->xmitmutex);
  825. rc = iscsi_conn_send_generic(conn, hdr, data, data_size);
  826. mutex_unlock(&conn->xmitmutex);
  827. return rc;
  828. }
  829. EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
  830. void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
  831. {
  832. struct iscsi_session *session = class_to_transport_session(cls_session);
  833. struct iscsi_conn *conn = session->leadconn;
  834. spin_lock_bh(&session->lock);
  835. if (session->state != ISCSI_STATE_LOGGED_IN) {
  836. session->state = ISCSI_STATE_RECOVERY_FAILED;
  837. if (conn)
  838. wake_up(&conn->ehwait);
  839. }
  840. spin_unlock_bh(&session->lock);
  841. }
  842. EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
  843. int iscsi_eh_host_reset(struct scsi_cmnd *sc)
  844. {
  845. struct Scsi_Host *host = sc->device->host;
  846. struct iscsi_session *session = iscsi_hostdata(host->hostdata);
  847. struct iscsi_conn *conn = session->leadconn;
  848. int fail_session = 0;
  849. spin_lock_bh(&session->lock);
  850. if (session->state == ISCSI_STATE_TERMINATE) {
  851. failed:
  852. debug_scsi("failing host reset: session terminated "
  853. "[CID %d age %d]\n", conn->id, session->age);
  854. spin_unlock_bh(&session->lock);
  855. return FAILED;
  856. }
  857. if (sc->SCp.phase == session->age) {
  858. debug_scsi("failing connection CID %d due to SCSI host reset\n",
  859. conn->id);
  860. fail_session = 1;
  861. }
  862. spin_unlock_bh(&session->lock);
  863. /*
  864. * we drop the lock here but the leadconn cannot be destoyed while
  865. * we are in the scsi eh
  866. */
  867. if (fail_session)
  868. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  869. debug_scsi("iscsi_eh_host_reset wait for relogin\n");
  870. wait_event_interruptible(conn->ehwait,
  871. session->state == ISCSI_STATE_TERMINATE ||
  872. session->state == ISCSI_STATE_LOGGED_IN ||
  873. session->state == ISCSI_STATE_RECOVERY_FAILED);
  874. if (signal_pending(current))
  875. flush_signals(current);
  876. spin_lock_bh(&session->lock);
  877. if (session->state == ISCSI_STATE_LOGGED_IN)
  878. printk(KERN_INFO "iscsi: host reset succeeded\n");
  879. else
  880. goto failed;
  881. spin_unlock_bh(&session->lock);
  882. return SUCCESS;
  883. }
  884. EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
  885. static void iscsi_tmabort_timedout(unsigned long data)
  886. {
  887. struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)data;
  888. struct iscsi_conn *conn = ctask->conn;
  889. struct iscsi_session *session = conn->session;
  890. spin_lock(&session->lock);
  891. if (conn->tmabort_state == TMABORT_INITIAL) {
  892. conn->tmabort_state = TMABORT_TIMEDOUT;
  893. debug_scsi("tmabort timedout [sc %p itt 0x%x]\n",
  894. ctask->sc, ctask->itt);
  895. /* unblock eh_abort() */
  896. wake_up(&conn->ehwait);
  897. }
  898. spin_unlock(&session->lock);
  899. }
  900. /* must be called with the mutex lock */
  901. static int iscsi_exec_abort_task(struct scsi_cmnd *sc,
  902. struct iscsi_cmd_task *ctask)
  903. {
  904. struct iscsi_conn *conn = ctask->conn;
  905. struct iscsi_session *session = conn->session;
  906. struct iscsi_tm *hdr = &conn->tmhdr;
  907. int rc;
  908. /*
  909. * ctask timed out but session is OK requests must be serialized.
  910. */
  911. memset(hdr, 0, sizeof(struct iscsi_tm));
  912. hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
  913. hdr->flags = ISCSI_TM_FUNC_ABORT_TASK;
  914. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  915. memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
  916. hdr->rtt = ctask->hdr->itt;
  917. hdr->refcmdsn = ctask->hdr->cmdsn;
  918. rc = iscsi_conn_send_generic(conn, (struct iscsi_hdr *)hdr,
  919. NULL, 0);
  920. if (rc) {
  921. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  922. debug_scsi("abort sent failure [itt 0x%x] %d\n", ctask->itt,
  923. rc);
  924. return rc;
  925. }
  926. debug_scsi("abort sent [itt 0x%x]\n", ctask->itt);
  927. spin_lock_bh(&session->lock);
  928. ctask->mtask = (struct iscsi_mgmt_task *)
  929. session->mgmt_cmds[get_itt(hdr->itt) -
  930. ISCSI_MGMT_ITT_OFFSET];
  931. if (conn->tmabort_state == TMABORT_INITIAL) {
  932. conn->tmfcmd_pdus_cnt++;
  933. conn->tmabort_timer.expires = 10*HZ + jiffies;
  934. conn->tmabort_timer.function = iscsi_tmabort_timedout;
  935. conn->tmabort_timer.data = (unsigned long)ctask;
  936. add_timer(&conn->tmabort_timer);
  937. debug_scsi("abort set timeout [itt 0x%x]\n", ctask->itt);
  938. }
  939. spin_unlock_bh(&session->lock);
  940. mutex_unlock(&conn->xmitmutex);
  941. /*
  942. * block eh thread until:
  943. *
  944. * 1) abort response
  945. * 2) abort timeout
  946. * 3) session is terminated or restarted or userspace has
  947. * given up on recovery
  948. */
  949. wait_event_interruptible(conn->ehwait,
  950. sc->SCp.phase != session->age ||
  951. session->state != ISCSI_STATE_LOGGED_IN ||
  952. conn->tmabort_state != TMABORT_INITIAL);
  953. if (signal_pending(current))
  954. flush_signals(current);
  955. del_timer_sync(&conn->tmabort_timer);
  956. mutex_lock(&conn->xmitmutex);
  957. return 0;
  958. }
  959. /*
  960. * xmit mutex and session lock must be held
  961. */
  962. static struct iscsi_mgmt_task *
  963. iscsi_remove_mgmt_task(struct kfifo *fifo, uint32_t itt)
  964. {
  965. int i, nr_tasks = __kfifo_len(fifo) / sizeof(void*);
  966. struct iscsi_mgmt_task *task;
  967. debug_scsi("searching %d tasks\n", nr_tasks);
  968. for (i = 0; i < nr_tasks; i++) {
  969. __kfifo_get(fifo, (void*)&task, sizeof(void*));
  970. debug_scsi("check task %u\n", task->itt);
  971. if (task->itt == itt) {
  972. debug_scsi("matched task\n");
  973. return task;
  974. }
  975. __kfifo_put(fifo, (void*)&task, sizeof(void*));
  976. }
  977. return NULL;
  978. }
  979. static int iscsi_ctask_mtask_cleanup(struct iscsi_cmd_task *ctask)
  980. {
  981. struct iscsi_conn *conn = ctask->conn;
  982. struct iscsi_session *session = conn->session;
  983. if (!ctask->mtask)
  984. return -EINVAL;
  985. if (!iscsi_remove_mgmt_task(conn->immqueue, ctask->mtask->itt))
  986. list_del(&ctask->mtask->running);
  987. __kfifo_put(session->mgmtpool.queue, (void*)&ctask->mtask,
  988. sizeof(void*));
  989. ctask->mtask = NULL;
  990. return 0;
  991. }
  992. /*
  993. * session lock and xmitmutex must be held
  994. */
  995. static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
  996. int err)
  997. {
  998. struct scsi_cmnd *sc;
  999. sc = ctask->sc;
  1000. if (!sc)
  1001. return;
  1002. conn->session->tt->cleanup_cmd_task(conn, ctask);
  1003. iscsi_ctask_mtask_cleanup(ctask);
  1004. sc->result = err;
  1005. sc->resid = sc->request_bufflen;
  1006. /* release ref from queuecommand */
  1007. __iscsi_put_ctask(ctask);
  1008. }
  1009. int iscsi_eh_abort(struct scsi_cmnd *sc)
  1010. {
  1011. struct iscsi_cmd_task *ctask;
  1012. struct iscsi_conn *conn;
  1013. struct iscsi_session *session;
  1014. int rc;
  1015. /*
  1016. * if session was ISCSI_STATE_IN_RECOVERY then we may not have
  1017. * got the command.
  1018. */
  1019. if (!sc->SCp.ptr) {
  1020. debug_scsi("sc never reached iscsi layer or it completed.\n");
  1021. return SUCCESS;
  1022. }
  1023. ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
  1024. conn = ctask->conn;
  1025. session = conn->session;
  1026. conn->eh_abort_cnt++;
  1027. debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
  1028. mutex_lock(&conn->xmitmutex);
  1029. spin_lock_bh(&session->lock);
  1030. /*
  1031. * If we are not logged in or we have started a new session
  1032. * then let the host reset code handle this
  1033. */
  1034. if (session->state != ISCSI_STATE_LOGGED_IN ||
  1035. sc->SCp.phase != session->age)
  1036. goto failed;
  1037. /* ctask completed before time out */
  1038. if (!ctask->sc) {
  1039. spin_unlock_bh(&session->lock);
  1040. debug_scsi("sc completed while abort in progress\n");
  1041. goto success_rel_mutex;
  1042. }
  1043. /* what should we do here ? */
  1044. if (conn->ctask == ctask) {
  1045. printk(KERN_INFO "iscsi: sc %p itt 0x%x partially sent. "
  1046. "Failing abort\n", sc, ctask->itt);
  1047. goto failed;
  1048. }
  1049. if (ctask->state == ISCSI_TASK_PENDING)
  1050. goto success_cleanup;
  1051. conn->tmabort_state = TMABORT_INITIAL;
  1052. spin_unlock_bh(&session->lock);
  1053. rc = iscsi_exec_abort_task(sc, ctask);
  1054. spin_lock_bh(&session->lock);
  1055. if (rc || sc->SCp.phase != session->age ||
  1056. session->state != ISCSI_STATE_LOGGED_IN)
  1057. goto failed;
  1058. iscsi_ctask_mtask_cleanup(ctask);
  1059. switch (conn->tmabort_state) {
  1060. case TMABORT_SUCCESS:
  1061. goto success_cleanup;
  1062. case TMABORT_NOT_FOUND:
  1063. if (!ctask->sc) {
  1064. /* ctask completed before tmf abort response */
  1065. spin_unlock_bh(&session->lock);
  1066. debug_scsi("sc completed while abort in progress\n");
  1067. goto success_rel_mutex;
  1068. }
  1069. /* fall through */
  1070. default:
  1071. /* timedout or failed */
  1072. spin_unlock_bh(&session->lock);
  1073. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1074. spin_lock_bh(&session->lock);
  1075. goto failed;
  1076. }
  1077. success_cleanup:
  1078. debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
  1079. spin_unlock_bh(&session->lock);
  1080. /*
  1081. * clean up task if aborted. we have the xmitmutex so grab
  1082. * the recv lock as a writer
  1083. */
  1084. write_lock_bh(conn->recv_lock);
  1085. spin_lock(&session->lock);
  1086. fail_command(conn, ctask, DID_ABORT << 16);
  1087. spin_unlock(&session->lock);
  1088. write_unlock_bh(conn->recv_lock);
  1089. success_rel_mutex:
  1090. mutex_unlock(&conn->xmitmutex);
  1091. return SUCCESS;
  1092. failed:
  1093. spin_unlock_bh(&session->lock);
  1094. mutex_unlock(&conn->xmitmutex);
  1095. debug_scsi("abort failed [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
  1096. return FAILED;
  1097. }
  1098. EXPORT_SYMBOL_GPL(iscsi_eh_abort);
  1099. int
  1100. iscsi_pool_init(struct iscsi_queue *q, int max, void ***items, int item_size)
  1101. {
  1102. int i;
  1103. *items = kmalloc(max * sizeof(void*), GFP_KERNEL);
  1104. if (*items == NULL)
  1105. return -ENOMEM;
  1106. q->max = max;
  1107. q->pool = kmalloc(max * sizeof(void*), GFP_KERNEL);
  1108. if (q->pool == NULL) {
  1109. kfree(*items);
  1110. return -ENOMEM;
  1111. }
  1112. q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
  1113. GFP_KERNEL, NULL);
  1114. if (q->queue == ERR_PTR(-ENOMEM)) {
  1115. kfree(q->pool);
  1116. kfree(*items);
  1117. return -ENOMEM;
  1118. }
  1119. for (i = 0; i < max; i++) {
  1120. q->pool[i] = kmalloc(item_size, GFP_KERNEL);
  1121. if (q->pool[i] == NULL) {
  1122. int j;
  1123. for (j = 0; j < i; j++)
  1124. kfree(q->pool[j]);
  1125. kfifo_free(q->queue);
  1126. kfree(q->pool);
  1127. kfree(*items);
  1128. return -ENOMEM;
  1129. }
  1130. memset(q->pool[i], 0, item_size);
  1131. (*items)[i] = q->pool[i];
  1132. __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
  1133. }
  1134. return 0;
  1135. }
  1136. EXPORT_SYMBOL_GPL(iscsi_pool_init);
  1137. void iscsi_pool_free(struct iscsi_queue *q, void **items)
  1138. {
  1139. int i;
  1140. for (i = 0; i < q->max; i++)
  1141. kfree(items[i]);
  1142. kfree(q->pool);
  1143. kfree(items);
  1144. }
  1145. EXPORT_SYMBOL_GPL(iscsi_pool_free);
  1146. /*
  1147. * iSCSI Session's hostdata organization:
  1148. *
  1149. * *------------------* <== hostdata_session(host->hostdata)
  1150. * | ptr to class sess|
  1151. * |------------------| <== iscsi_hostdata(host->hostdata)
  1152. * | iscsi_session |
  1153. * *------------------*
  1154. */
  1155. #define hostdata_privsize(_sz) (sizeof(unsigned long) + _sz + \
  1156. _sz % sizeof(unsigned long))
  1157. #define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
  1158. /**
  1159. * iscsi_session_setup - create iscsi cls session and host and session
  1160. * @scsit: scsi transport template
  1161. * @iscsit: iscsi transport template
  1162. * @initial_cmdsn: initial CmdSN
  1163. * @hostno: host no allocated
  1164. *
  1165. * This can be used by software iscsi_transports that allocate
  1166. * a session per scsi host.
  1167. **/
  1168. struct iscsi_cls_session *
  1169. iscsi_session_setup(struct iscsi_transport *iscsit,
  1170. struct scsi_transport_template *scsit,
  1171. int cmd_task_size, int mgmt_task_size,
  1172. uint32_t initial_cmdsn, uint32_t *hostno)
  1173. {
  1174. struct Scsi_Host *shost;
  1175. struct iscsi_session *session;
  1176. struct iscsi_cls_session *cls_session;
  1177. int cmd_i;
  1178. shost = scsi_host_alloc(iscsit->host_template,
  1179. hostdata_privsize(sizeof(*session)));
  1180. if (!shost)
  1181. return NULL;
  1182. shost->max_id = 1;
  1183. shost->max_channel = 0;
  1184. shost->max_lun = iscsit->max_lun;
  1185. shost->max_cmd_len = iscsit->max_cmd_len;
  1186. shost->transportt = scsit;
  1187. shost->transportt->create_work_queue = 1;
  1188. *hostno = shost->host_no;
  1189. session = iscsi_hostdata(shost->hostdata);
  1190. memset(session, 0, sizeof(struct iscsi_session));
  1191. session->host = shost;
  1192. session->state = ISCSI_STATE_FREE;
  1193. session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
  1194. session->cmds_max = ISCSI_XMIT_CMDS_MAX;
  1195. session->cmdsn = initial_cmdsn;
  1196. session->exp_cmdsn = initial_cmdsn + 1;
  1197. session->max_cmdsn = initial_cmdsn + 1;
  1198. session->max_r2t = 1;
  1199. session->tt = iscsit;
  1200. /* initialize SCSI PDU commands pool */
  1201. if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
  1202. (void***)&session->cmds,
  1203. cmd_task_size + sizeof(struct iscsi_cmd_task)))
  1204. goto cmdpool_alloc_fail;
  1205. /* pre-format cmds pool with ITT */
  1206. for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
  1207. struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
  1208. if (cmd_task_size)
  1209. ctask->dd_data = &ctask[1];
  1210. ctask->itt = cmd_i;
  1211. INIT_LIST_HEAD(&ctask->running);
  1212. }
  1213. spin_lock_init(&session->lock);
  1214. /* initialize immediate command pool */
  1215. if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
  1216. (void***)&session->mgmt_cmds,
  1217. mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
  1218. goto mgmtpool_alloc_fail;
  1219. /* pre-format immediate cmds pool with ITT */
  1220. for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
  1221. struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
  1222. if (mgmt_task_size)
  1223. mtask->dd_data = &mtask[1];
  1224. mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
  1225. INIT_LIST_HEAD(&mtask->running);
  1226. }
  1227. if (scsi_add_host(shost, NULL))
  1228. goto add_host_fail;
  1229. if (!try_module_get(iscsit->owner))
  1230. goto cls_session_fail;
  1231. cls_session = iscsi_create_session(shost, iscsit, 0);
  1232. if (!cls_session)
  1233. goto module_put;
  1234. *(unsigned long*)shost->hostdata = (unsigned long)cls_session;
  1235. return cls_session;
  1236. module_put:
  1237. module_put(iscsit->owner);
  1238. cls_session_fail:
  1239. scsi_remove_host(shost);
  1240. add_host_fail:
  1241. iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
  1242. mgmtpool_alloc_fail:
  1243. iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
  1244. cmdpool_alloc_fail:
  1245. scsi_host_put(shost);
  1246. return NULL;
  1247. }
  1248. EXPORT_SYMBOL_GPL(iscsi_session_setup);
  1249. /**
  1250. * iscsi_session_teardown - destroy session, host, and cls_session
  1251. * shost: scsi host
  1252. *
  1253. * This can be used by software iscsi_transports that allocate
  1254. * a session per scsi host.
  1255. **/
  1256. void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
  1257. {
  1258. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  1259. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  1260. struct module *owner = cls_session->transport->owner;
  1261. scsi_remove_host(shost);
  1262. iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
  1263. iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
  1264. kfree(session->targetname);
  1265. kfree(session->hwaddress);
  1266. kfree(session->initiatorname);
  1267. iscsi_destroy_session(cls_session);
  1268. scsi_host_put(shost);
  1269. module_put(owner);
  1270. }
  1271. EXPORT_SYMBOL_GPL(iscsi_session_teardown);
  1272. /**
  1273. * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
  1274. * @cls_session: iscsi_cls_session
  1275. * @conn_idx: cid
  1276. **/
  1277. struct iscsi_cls_conn *
  1278. iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
  1279. {
  1280. struct iscsi_session *session = class_to_transport_session(cls_session);
  1281. struct iscsi_conn *conn;
  1282. struct iscsi_cls_conn *cls_conn;
  1283. char *data;
  1284. cls_conn = iscsi_create_conn(cls_session, conn_idx);
  1285. if (!cls_conn)
  1286. return NULL;
  1287. conn = cls_conn->dd_data;
  1288. memset(conn, 0, sizeof(*conn));
  1289. conn->session = session;
  1290. conn->cls_conn = cls_conn;
  1291. conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
  1292. conn->id = conn_idx;
  1293. conn->exp_statsn = 0;
  1294. conn->tmabort_state = TMABORT_INITIAL;
  1295. INIT_LIST_HEAD(&conn->run_list);
  1296. INIT_LIST_HEAD(&conn->mgmt_run_list);
  1297. INIT_LIST_HEAD(&conn->xmitqueue);
  1298. /* initialize general immediate & non-immediate PDU commands queue */
  1299. conn->immqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
  1300. GFP_KERNEL, NULL);
  1301. if (conn->immqueue == ERR_PTR(-ENOMEM))
  1302. goto immqueue_alloc_fail;
  1303. conn->mgmtqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
  1304. GFP_KERNEL, NULL);
  1305. if (conn->mgmtqueue == ERR_PTR(-ENOMEM))
  1306. goto mgmtqueue_alloc_fail;
  1307. INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
  1308. /* allocate login_mtask used for the login/text sequences */
  1309. spin_lock_bh(&session->lock);
  1310. if (!__kfifo_get(session->mgmtpool.queue,
  1311. (void*)&conn->login_mtask,
  1312. sizeof(void*))) {
  1313. spin_unlock_bh(&session->lock);
  1314. goto login_mtask_alloc_fail;
  1315. }
  1316. spin_unlock_bh(&session->lock);
  1317. data = kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN, GFP_KERNEL);
  1318. if (!data)
  1319. goto login_mtask_data_alloc_fail;
  1320. conn->login_mtask->data = conn->data = data;
  1321. init_timer(&conn->tmabort_timer);
  1322. mutex_init(&conn->xmitmutex);
  1323. init_waitqueue_head(&conn->ehwait);
  1324. return cls_conn;
  1325. login_mtask_data_alloc_fail:
  1326. __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
  1327. sizeof(void*));
  1328. login_mtask_alloc_fail:
  1329. kfifo_free(conn->mgmtqueue);
  1330. mgmtqueue_alloc_fail:
  1331. kfifo_free(conn->immqueue);
  1332. immqueue_alloc_fail:
  1333. iscsi_destroy_conn(cls_conn);
  1334. return NULL;
  1335. }
  1336. EXPORT_SYMBOL_GPL(iscsi_conn_setup);
  1337. /**
  1338. * iscsi_conn_teardown - teardown iscsi connection
  1339. * cls_conn: iscsi class connection
  1340. *
  1341. * TODO: we may need to make this into a two step process
  1342. * like scsi-mls remove + put host
  1343. */
  1344. void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
  1345. {
  1346. struct iscsi_conn *conn = cls_conn->dd_data;
  1347. struct iscsi_session *session = conn->session;
  1348. unsigned long flags;
  1349. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1350. mutex_lock(&conn->xmitmutex);
  1351. spin_lock_bh(&session->lock);
  1352. conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
  1353. if (session->leadconn == conn) {
  1354. /*
  1355. * leading connection? then give up on recovery.
  1356. */
  1357. session->state = ISCSI_STATE_TERMINATE;
  1358. wake_up(&conn->ehwait);
  1359. }
  1360. spin_unlock_bh(&session->lock);
  1361. mutex_unlock(&conn->xmitmutex);
  1362. /*
  1363. * Block until all in-progress commands for this connection
  1364. * time out or fail.
  1365. */
  1366. for (;;) {
  1367. spin_lock_irqsave(session->host->host_lock, flags);
  1368. if (!session->host->host_busy) { /* OK for ERL == 0 */
  1369. spin_unlock_irqrestore(session->host->host_lock, flags);
  1370. break;
  1371. }
  1372. spin_unlock_irqrestore(session->host->host_lock, flags);
  1373. msleep_interruptible(500);
  1374. printk(KERN_INFO "iscsi: scsi conn_destroy(): host_busy %d "
  1375. "host_failed %d\n", session->host->host_busy,
  1376. session->host->host_failed);
  1377. /*
  1378. * force eh_abort() to unblock
  1379. */
  1380. wake_up(&conn->ehwait);
  1381. }
  1382. /* flush queued up work because we free the connection below */
  1383. scsi_flush_work(session->host);
  1384. spin_lock_bh(&session->lock);
  1385. kfree(conn->data);
  1386. kfree(conn->persistent_address);
  1387. __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
  1388. sizeof(void*));
  1389. if (session->leadconn == conn) {
  1390. session->leadconn = NULL;
  1391. /* no connections exits.. reset sequencing */
  1392. session->cmdsn = session->max_cmdsn = session->exp_cmdsn = 1;
  1393. }
  1394. spin_unlock_bh(&session->lock);
  1395. kfifo_free(conn->immqueue);
  1396. kfifo_free(conn->mgmtqueue);
  1397. iscsi_destroy_conn(cls_conn);
  1398. }
  1399. EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
  1400. int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
  1401. {
  1402. struct iscsi_conn *conn = cls_conn->dd_data;
  1403. struct iscsi_session *session = conn->session;
  1404. if (!session) {
  1405. printk(KERN_ERR "iscsi: can't start unbound connection\n");
  1406. return -EPERM;
  1407. }
  1408. if ((session->imm_data_en || !session->initial_r2t_en) &&
  1409. session->first_burst > session->max_burst) {
  1410. printk("iscsi: invalid burst lengths: "
  1411. "first_burst %d max_burst %d\n",
  1412. session->first_burst, session->max_burst);
  1413. return -EINVAL;
  1414. }
  1415. spin_lock_bh(&session->lock);
  1416. conn->c_stage = ISCSI_CONN_STARTED;
  1417. session->state = ISCSI_STATE_LOGGED_IN;
  1418. switch(conn->stop_stage) {
  1419. case STOP_CONN_RECOVER:
  1420. /*
  1421. * unblock eh_abort() if it is blocked. re-try all
  1422. * commands after successful recovery
  1423. */
  1424. conn->stop_stage = 0;
  1425. conn->tmabort_state = TMABORT_INITIAL;
  1426. session->age++;
  1427. spin_unlock_bh(&session->lock);
  1428. iscsi_unblock_session(session_to_cls(session));
  1429. wake_up(&conn->ehwait);
  1430. return 0;
  1431. case STOP_CONN_TERM:
  1432. conn->stop_stage = 0;
  1433. break;
  1434. default:
  1435. break;
  1436. }
  1437. spin_unlock_bh(&session->lock);
  1438. return 0;
  1439. }
  1440. EXPORT_SYMBOL_GPL(iscsi_conn_start);
  1441. static void
  1442. flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
  1443. {
  1444. struct iscsi_mgmt_task *mtask, *tmp;
  1445. /* handle pending */
  1446. while (__kfifo_get(conn->immqueue, (void*)&mtask, sizeof(void*)) ||
  1447. __kfifo_get(conn->mgmtqueue, (void*)&mtask, sizeof(void*))) {
  1448. if (mtask == conn->login_mtask)
  1449. continue;
  1450. debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt);
  1451. __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
  1452. sizeof(void*));
  1453. }
  1454. /* handle running */
  1455. list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) {
  1456. debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt);
  1457. list_del(&mtask->running);
  1458. if (mtask == conn->login_mtask)
  1459. continue;
  1460. __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
  1461. sizeof(void*));
  1462. }
  1463. conn->mtask = NULL;
  1464. }
  1465. /* Fail commands. Mutex and session lock held and recv side suspended */
  1466. static void fail_all_commands(struct iscsi_conn *conn)
  1467. {
  1468. struct iscsi_cmd_task *ctask, *tmp;
  1469. /* flush pending */
  1470. list_for_each_entry_safe(ctask, tmp, &conn->xmitqueue, running) {
  1471. debug_scsi("failing pending sc %p itt 0x%x\n", ctask->sc,
  1472. ctask->itt);
  1473. fail_command(conn, ctask, DID_BUS_BUSY << 16);
  1474. }
  1475. /* fail all other running */
  1476. list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) {
  1477. debug_scsi("failing in progress sc %p itt 0x%x\n",
  1478. ctask->sc, ctask->itt);
  1479. fail_command(conn, ctask, DID_BUS_BUSY << 16);
  1480. }
  1481. conn->ctask = NULL;
  1482. }
  1483. static void iscsi_start_session_recovery(struct iscsi_session *session,
  1484. struct iscsi_conn *conn, int flag)
  1485. {
  1486. int old_stop_stage;
  1487. spin_lock_bh(&session->lock);
  1488. if (conn->stop_stage == STOP_CONN_TERM) {
  1489. spin_unlock_bh(&session->lock);
  1490. return;
  1491. }
  1492. /*
  1493. * When this is called for the in_login state, we only want to clean
  1494. * up the login task and connection. We do not need to block and set
  1495. * the recovery state again
  1496. */
  1497. if (flag == STOP_CONN_TERM)
  1498. session->state = ISCSI_STATE_TERMINATE;
  1499. else if (conn->stop_stage != STOP_CONN_RECOVER)
  1500. session->state = ISCSI_STATE_IN_RECOVERY;
  1501. old_stop_stage = conn->stop_stage;
  1502. conn->stop_stage = flag;
  1503. conn->c_stage = ISCSI_CONN_STOPPED;
  1504. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1505. spin_unlock_bh(&session->lock);
  1506. write_lock_bh(conn->recv_lock);
  1507. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  1508. write_unlock_bh(conn->recv_lock);
  1509. mutex_lock(&conn->xmitmutex);
  1510. /*
  1511. * for connection level recovery we should not calculate
  1512. * header digest. conn->hdr_size used for optimization
  1513. * in hdr_extract() and will be re-negotiated at
  1514. * set_param() time.
  1515. */
  1516. if (flag == STOP_CONN_RECOVER) {
  1517. conn->hdrdgst_en = 0;
  1518. conn->datadgst_en = 0;
  1519. if (session->state == ISCSI_STATE_IN_RECOVERY &&
  1520. old_stop_stage != STOP_CONN_RECOVER) {
  1521. debug_scsi("blocking session\n");
  1522. iscsi_block_session(session_to_cls(session));
  1523. }
  1524. }
  1525. /*
  1526. * flush queues.
  1527. */
  1528. spin_lock_bh(&session->lock);
  1529. fail_all_commands(conn);
  1530. flush_control_queues(session, conn);
  1531. spin_unlock_bh(&session->lock);
  1532. mutex_unlock(&conn->xmitmutex);
  1533. }
  1534. void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
  1535. {
  1536. struct iscsi_conn *conn = cls_conn->dd_data;
  1537. struct iscsi_session *session = conn->session;
  1538. switch (flag) {
  1539. case STOP_CONN_RECOVER:
  1540. case STOP_CONN_TERM:
  1541. iscsi_start_session_recovery(session, conn, flag);
  1542. break;
  1543. default:
  1544. printk(KERN_ERR "iscsi: invalid stop flag %d\n", flag);
  1545. }
  1546. }
  1547. EXPORT_SYMBOL_GPL(iscsi_conn_stop);
  1548. int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
  1549. struct iscsi_cls_conn *cls_conn, int is_leading)
  1550. {
  1551. struct iscsi_session *session = class_to_transport_session(cls_session);
  1552. struct iscsi_conn *conn = cls_conn->dd_data;
  1553. spin_lock_bh(&session->lock);
  1554. if (is_leading)
  1555. session->leadconn = conn;
  1556. spin_unlock_bh(&session->lock);
  1557. /*
  1558. * Unblock xmitworker(), Login Phase will pass through.
  1559. */
  1560. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  1561. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1562. return 0;
  1563. }
  1564. EXPORT_SYMBOL_GPL(iscsi_conn_bind);
  1565. int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
  1566. enum iscsi_param param, char *buf, int buflen)
  1567. {
  1568. struct iscsi_conn *conn = cls_conn->dd_data;
  1569. struct iscsi_session *session = conn->session;
  1570. uint32_t value;
  1571. switch(param) {
  1572. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  1573. sscanf(buf, "%d", &conn->max_recv_dlength);
  1574. break;
  1575. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  1576. sscanf(buf, "%d", &conn->max_xmit_dlength);
  1577. break;
  1578. case ISCSI_PARAM_HDRDGST_EN:
  1579. sscanf(buf, "%d", &conn->hdrdgst_en);
  1580. break;
  1581. case ISCSI_PARAM_DATADGST_EN:
  1582. sscanf(buf, "%d", &conn->datadgst_en);
  1583. break;
  1584. case ISCSI_PARAM_INITIAL_R2T_EN:
  1585. sscanf(buf, "%d", &session->initial_r2t_en);
  1586. break;
  1587. case ISCSI_PARAM_MAX_R2T:
  1588. sscanf(buf, "%d", &session->max_r2t);
  1589. break;
  1590. case ISCSI_PARAM_IMM_DATA_EN:
  1591. sscanf(buf, "%d", &session->imm_data_en);
  1592. break;
  1593. case ISCSI_PARAM_FIRST_BURST:
  1594. sscanf(buf, "%d", &session->first_burst);
  1595. break;
  1596. case ISCSI_PARAM_MAX_BURST:
  1597. sscanf(buf, "%d", &session->max_burst);
  1598. break;
  1599. case ISCSI_PARAM_PDU_INORDER_EN:
  1600. sscanf(buf, "%d", &session->pdu_inorder_en);
  1601. break;
  1602. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  1603. sscanf(buf, "%d", &session->dataseq_inorder_en);
  1604. break;
  1605. case ISCSI_PARAM_ERL:
  1606. sscanf(buf, "%d", &session->erl);
  1607. break;
  1608. case ISCSI_PARAM_IFMARKER_EN:
  1609. sscanf(buf, "%d", &value);
  1610. BUG_ON(value);
  1611. break;
  1612. case ISCSI_PARAM_OFMARKER_EN:
  1613. sscanf(buf, "%d", &value);
  1614. BUG_ON(value);
  1615. break;
  1616. case ISCSI_PARAM_EXP_STATSN:
  1617. sscanf(buf, "%u", &conn->exp_statsn);
  1618. break;
  1619. case ISCSI_PARAM_TARGET_NAME:
  1620. /* this should not change between logins */
  1621. if (session->targetname)
  1622. break;
  1623. session->targetname = kstrdup(buf, GFP_KERNEL);
  1624. if (!session->targetname)
  1625. return -ENOMEM;
  1626. break;
  1627. case ISCSI_PARAM_TPGT:
  1628. sscanf(buf, "%d", &session->tpgt);
  1629. break;
  1630. case ISCSI_PARAM_PERSISTENT_PORT:
  1631. sscanf(buf, "%d", &conn->persistent_port);
  1632. break;
  1633. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  1634. /*
  1635. * this is the address returned in discovery so it should
  1636. * not change between logins.
  1637. */
  1638. if (conn->persistent_address)
  1639. break;
  1640. conn->persistent_address = kstrdup(buf, GFP_KERNEL);
  1641. if (!conn->persistent_address)
  1642. return -ENOMEM;
  1643. break;
  1644. default:
  1645. return -ENOSYS;
  1646. }
  1647. return 0;
  1648. }
  1649. EXPORT_SYMBOL_GPL(iscsi_set_param);
  1650. int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
  1651. enum iscsi_param param, char *buf)
  1652. {
  1653. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  1654. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  1655. int len;
  1656. switch(param) {
  1657. case ISCSI_PARAM_INITIAL_R2T_EN:
  1658. len = sprintf(buf, "%d\n", session->initial_r2t_en);
  1659. break;
  1660. case ISCSI_PARAM_MAX_R2T:
  1661. len = sprintf(buf, "%hu\n", session->max_r2t);
  1662. break;
  1663. case ISCSI_PARAM_IMM_DATA_EN:
  1664. len = sprintf(buf, "%d\n", session->imm_data_en);
  1665. break;
  1666. case ISCSI_PARAM_FIRST_BURST:
  1667. len = sprintf(buf, "%u\n", session->first_burst);
  1668. break;
  1669. case ISCSI_PARAM_MAX_BURST:
  1670. len = sprintf(buf, "%u\n", session->max_burst);
  1671. break;
  1672. case ISCSI_PARAM_PDU_INORDER_EN:
  1673. len = sprintf(buf, "%d\n", session->pdu_inorder_en);
  1674. break;
  1675. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  1676. len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
  1677. break;
  1678. case ISCSI_PARAM_ERL:
  1679. len = sprintf(buf, "%d\n", session->erl);
  1680. break;
  1681. case ISCSI_PARAM_TARGET_NAME:
  1682. len = sprintf(buf, "%s\n", session->targetname);
  1683. break;
  1684. case ISCSI_PARAM_TPGT:
  1685. len = sprintf(buf, "%d\n", session->tpgt);
  1686. break;
  1687. default:
  1688. return -ENOSYS;
  1689. }
  1690. return len;
  1691. }
  1692. EXPORT_SYMBOL_GPL(iscsi_session_get_param);
  1693. int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
  1694. enum iscsi_param param, char *buf)
  1695. {
  1696. struct iscsi_conn *conn = cls_conn->dd_data;
  1697. int len;
  1698. switch(param) {
  1699. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  1700. len = sprintf(buf, "%u\n", conn->max_recv_dlength);
  1701. break;
  1702. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  1703. len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
  1704. break;
  1705. case ISCSI_PARAM_HDRDGST_EN:
  1706. len = sprintf(buf, "%d\n", conn->hdrdgst_en);
  1707. break;
  1708. case ISCSI_PARAM_DATADGST_EN:
  1709. len = sprintf(buf, "%d\n", conn->datadgst_en);
  1710. break;
  1711. case ISCSI_PARAM_IFMARKER_EN:
  1712. len = sprintf(buf, "%d\n", conn->ifmarker_en);
  1713. break;
  1714. case ISCSI_PARAM_OFMARKER_EN:
  1715. len = sprintf(buf, "%d\n", conn->ofmarker_en);
  1716. break;
  1717. case ISCSI_PARAM_EXP_STATSN:
  1718. len = sprintf(buf, "%u\n", conn->exp_statsn);
  1719. break;
  1720. case ISCSI_PARAM_PERSISTENT_PORT:
  1721. len = sprintf(buf, "%d\n", conn->persistent_port);
  1722. break;
  1723. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  1724. len = sprintf(buf, "%s\n", conn->persistent_address);
  1725. break;
  1726. default:
  1727. return -ENOSYS;
  1728. }
  1729. return len;
  1730. }
  1731. EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
  1732. int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
  1733. char *buf)
  1734. {
  1735. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  1736. int len;
  1737. switch (param) {
  1738. case ISCSI_HOST_PARAM_HWADDRESS:
  1739. if (!session->hwaddress)
  1740. len = sprintf(buf, "%s\n", "default");
  1741. else
  1742. len = sprintf(buf, "%s\n", session->hwaddress);
  1743. break;
  1744. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  1745. if (!session->initiatorname)
  1746. len = sprintf(buf, "%s\n", "unknown");
  1747. else
  1748. len = sprintf(buf, "%s\n", session->initiatorname);
  1749. break;
  1750. default:
  1751. return -ENOSYS;
  1752. }
  1753. return len;
  1754. }
  1755. EXPORT_SYMBOL_GPL(iscsi_host_get_param);
  1756. int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
  1757. char *buf, int buflen)
  1758. {
  1759. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  1760. switch (param) {
  1761. case ISCSI_HOST_PARAM_HWADDRESS:
  1762. if (!session->hwaddress)
  1763. session->hwaddress = kstrdup(buf, GFP_KERNEL);
  1764. break;
  1765. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  1766. if (!session->initiatorname)
  1767. session->initiatorname = kstrdup(buf, GFP_KERNEL);
  1768. break;
  1769. default:
  1770. return -ENOSYS;
  1771. }
  1772. return 0;
  1773. }
  1774. EXPORT_SYMBOL_GPL(iscsi_host_set_param);
  1775. MODULE_AUTHOR("Mike Christie");
  1776. MODULE_DESCRIPTION("iSCSI library functions");
  1777. MODULE_LICENSE("GPL");