libiscsi.c 52 KB

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