libiscsi.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973
  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 = ctask->itt | (conn->id << ISCSI_CID_SHIFT) |
  106. (session->age << ISCSI_AGE_SHIFT);
  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. int 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 = (data[0] << 8) | data[1];
  243. if (datalen < senselen)
  244. goto invalid_datalen;
  245. memcpy(sc->sense_buffer, data + 2,
  246. min(senselen, SCSI_SENSE_BUFFERSIZE));
  247. debug_scsi("copied %d bytes of sense\n",
  248. min(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 = rejected_pdu.itt & ISCSI_ITT_MASK;
  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 != cpu_to_be32(ISCSI_RESERVED_TAG))
  324. itt = hdr->itt & ISCSI_ITT_MASK;
  325. else
  326. itt = hdr->itt;
  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 != 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 == ISCSI_RESERVED_TAG) {
  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 == 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. /* we need sth like iscsi_async_event_rsp() */
  427. rc = ISCSI_ERR_BAD_OPCODE;
  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 != cpu_to_be32(ISCSI_RESERVED_TAG)) {
  457. if ((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", hdr->itt,
  461. session->age & ISCSI_AGE_MASK);
  462. return ISCSI_ERR_BAD_ITT;
  463. }
  464. if ((hdr->itt & ISCSI_CID_MASK) !=
  465. (conn->id << ISCSI_CID_SHIFT)) {
  466. printk(KERN_ERR "iscsi: received itt %x, expected "
  467. "CID (%x)\n", hdr->itt, conn->id);
  468. return ISCSI_ERR_BAD_ITT;
  469. }
  470. itt = hdr->itt & ISCSI_ITT_MASK;
  471. } else
  472. itt = hdr->itt;
  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. /**
  510. * iscsi_data_xmit - xmit any command into the scheduled connection
  511. * @conn: iscsi connection
  512. *
  513. * Notes:
  514. * The function can return -EAGAIN in which case the caller must
  515. * re-schedule it again later or recover. '0' return code means
  516. * successful xmit.
  517. **/
  518. static int iscsi_data_xmit(struct iscsi_conn *conn)
  519. {
  520. struct iscsi_transport *tt;
  521. int rc = 0;
  522. if (unlikely(conn->suspend_tx)) {
  523. debug_scsi("conn %d Tx suspended!\n", conn->id);
  524. return -ENODATA;
  525. }
  526. tt = conn->session->tt;
  527. /*
  528. * Transmit in the following order:
  529. *
  530. * 1) un-finished xmit (ctask or mtask)
  531. * 2) immediate control PDUs
  532. * 3) write data
  533. * 4) SCSI commands
  534. * 5) non-immediate control PDUs
  535. *
  536. * No need to lock around __kfifo_get as long as
  537. * there's one producer and one consumer.
  538. */
  539. BUG_ON(conn->ctask && conn->mtask);
  540. if (conn->ctask) {
  541. iscsi_get_ctask(conn->ctask);
  542. rc = tt->xmit_cmd_task(conn, conn->ctask);
  543. iscsi_put_ctask(conn->ctask);
  544. if (rc)
  545. goto again;
  546. /* done with this in-progress ctask */
  547. conn->ctask = NULL;
  548. }
  549. if (conn->mtask) {
  550. rc = tt->xmit_mgmt_task(conn, conn->mtask);
  551. if (rc)
  552. goto again;
  553. /* done with this in-progress mtask */
  554. conn->mtask = NULL;
  555. }
  556. /* process immediate first */
  557. if (unlikely(__kfifo_len(conn->immqueue))) {
  558. while (__kfifo_get(conn->immqueue, (void*)&conn->mtask,
  559. sizeof(void*))) {
  560. spin_lock_bh(&conn->session->lock);
  561. list_add_tail(&conn->mtask->running,
  562. &conn->mgmt_run_list);
  563. spin_unlock_bh(&conn->session->lock);
  564. rc = tt->xmit_mgmt_task(conn, conn->mtask);
  565. if (rc)
  566. goto again;
  567. }
  568. /* done with this mtask */
  569. conn->mtask = NULL;
  570. }
  571. /* process command queue */
  572. spin_lock_bh(&conn->session->lock);
  573. while (!list_empty(&conn->xmitqueue)) {
  574. /*
  575. * iscsi tcp may readd the task to the xmitqueue to send
  576. * write data
  577. */
  578. conn->ctask = list_entry(conn->xmitqueue.next,
  579. struct iscsi_cmd_task, running);
  580. conn->ctask->state = ISCSI_TASK_RUNNING;
  581. list_move_tail(conn->xmitqueue.next, &conn->run_list);
  582. __iscsi_get_ctask(conn->ctask);
  583. spin_unlock_bh(&conn->session->lock);
  584. rc = tt->xmit_cmd_task(conn, conn->ctask);
  585. spin_lock_bh(&conn->session->lock);
  586. __iscsi_put_ctask(conn->ctask);
  587. if (rc) {
  588. spin_unlock_bh(&conn->session->lock);
  589. goto again;
  590. }
  591. }
  592. spin_unlock_bh(&conn->session->lock);
  593. /* done with this ctask */
  594. conn->ctask = NULL;
  595. /* process the rest control plane PDUs, if any */
  596. if (unlikely(__kfifo_len(conn->mgmtqueue))) {
  597. while (__kfifo_get(conn->mgmtqueue, (void*)&conn->mtask,
  598. sizeof(void*))) {
  599. spin_lock_bh(&conn->session->lock);
  600. list_add_tail(&conn->mtask->running,
  601. &conn->mgmt_run_list);
  602. spin_unlock_bh(&conn->session->lock);
  603. rc = tt->xmit_mgmt_task(conn, conn->mtask);
  604. if (rc)
  605. goto again;
  606. }
  607. /* done with this mtask */
  608. conn->mtask = NULL;
  609. }
  610. return -ENODATA;
  611. again:
  612. if (unlikely(conn->suspend_tx))
  613. return -ENODATA;
  614. return rc;
  615. }
  616. static void iscsi_xmitworker(void *data)
  617. {
  618. struct iscsi_conn *conn = data;
  619. int rc;
  620. /*
  621. * serialize Xmit worker on a per-connection basis.
  622. */
  623. mutex_lock(&conn->xmitmutex);
  624. do {
  625. rc = iscsi_data_xmit(conn);
  626. } while (rc >= 0 || rc == -EAGAIN);
  627. mutex_unlock(&conn->xmitmutex);
  628. }
  629. enum {
  630. FAILURE_BAD_HOST = 1,
  631. FAILURE_SESSION_FAILED,
  632. FAILURE_SESSION_FREED,
  633. FAILURE_WINDOW_CLOSED,
  634. FAILURE_OOM,
  635. FAILURE_SESSION_TERMINATE,
  636. FAILURE_SESSION_IN_RECOVERY,
  637. FAILURE_SESSION_RECOVERY_TIMEOUT,
  638. };
  639. int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
  640. {
  641. struct Scsi_Host *host;
  642. int reason = 0;
  643. struct iscsi_session *session;
  644. struct iscsi_conn *conn;
  645. struct iscsi_cmd_task *ctask = NULL;
  646. sc->scsi_done = done;
  647. sc->result = 0;
  648. sc->SCp.ptr = NULL;
  649. host = sc->device->host;
  650. session = iscsi_hostdata(host->hostdata);
  651. spin_lock(&session->lock);
  652. /*
  653. * ISCSI_STATE_FAILED is a temp. state. The recovery
  654. * code will decide what is best to do with command queued
  655. * during this time
  656. */
  657. if (session->state != ISCSI_STATE_LOGGED_IN &&
  658. session->state != ISCSI_STATE_FAILED) {
  659. /*
  660. * to handle the race between when we set the recovery state
  661. * and block the session we requeue here (commands could
  662. * be entering our queuecommand while a block is starting
  663. * up because the block code is not locked)
  664. */
  665. if (session->state == ISCSI_STATE_IN_RECOVERY) {
  666. reason = FAILURE_SESSION_IN_RECOVERY;
  667. goto reject;
  668. }
  669. if (session->state == ISCSI_STATE_RECOVERY_FAILED)
  670. reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
  671. else if (session->state == ISCSI_STATE_TERMINATE)
  672. reason = FAILURE_SESSION_TERMINATE;
  673. else
  674. reason = FAILURE_SESSION_FREED;
  675. goto fault;
  676. }
  677. /*
  678. * Check for iSCSI window and take care of CmdSN wrap-around
  679. */
  680. if ((int)(session->max_cmdsn - session->cmdsn) < 0) {
  681. reason = FAILURE_WINDOW_CLOSED;
  682. goto reject;
  683. }
  684. conn = session->leadconn;
  685. if (!conn) {
  686. reason = FAILURE_SESSION_FREED;
  687. goto fault;
  688. }
  689. if (!__kfifo_get(session->cmdpool.queue, (void*)&ctask,
  690. sizeof(void*))) {
  691. reason = FAILURE_OOM;
  692. goto reject;
  693. }
  694. sc->SCp.phase = session->age;
  695. sc->SCp.ptr = (char *)ctask;
  696. atomic_set(&ctask->refcount, 1);
  697. ctask->state = ISCSI_TASK_PENDING;
  698. ctask->mtask = NULL;
  699. ctask->conn = conn;
  700. ctask->sc = sc;
  701. INIT_LIST_HEAD(&ctask->running);
  702. ctask->total_length = sc->request_bufflen;
  703. iscsi_prep_scsi_cmd_pdu(ctask);
  704. session->tt->init_cmd_task(ctask);
  705. list_add_tail(&ctask->running, &conn->xmitqueue);
  706. debug_scsi(
  707. "ctask enq [%s cid %d sc %p cdb 0x%x itt 0x%x len %d cmdsn %d "
  708. "win %d]\n",
  709. sc->sc_data_direction == DMA_TO_DEVICE ? "write" : "read",
  710. conn->id, sc, sc->cmnd[0], ctask->itt, sc->request_bufflen,
  711. session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
  712. spin_unlock(&session->lock);
  713. scsi_queue_work(host, &conn->xmitwork);
  714. return 0;
  715. reject:
  716. spin_unlock(&session->lock);
  717. debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
  718. return SCSI_MLQUEUE_HOST_BUSY;
  719. fault:
  720. spin_unlock(&session->lock);
  721. printk(KERN_ERR "iscsi: cmd 0x%x is not queued (%d)\n",
  722. sc->cmnd[0], reason);
  723. sc->result = (DID_NO_CONNECT << 16);
  724. sc->resid = sc->request_bufflen;
  725. sc->scsi_done(sc);
  726. return 0;
  727. }
  728. EXPORT_SYMBOL_GPL(iscsi_queuecommand);
  729. int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
  730. {
  731. if (depth > ISCSI_MAX_CMD_PER_LUN)
  732. depth = ISCSI_MAX_CMD_PER_LUN;
  733. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
  734. return sdev->queue_depth;
  735. }
  736. EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
  737. static int
  738. iscsi_conn_send_generic(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  739. char *data, uint32_t data_size)
  740. {
  741. struct iscsi_session *session = conn->session;
  742. struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
  743. struct iscsi_mgmt_task *mtask;
  744. spin_lock_bh(&session->lock);
  745. if (session->state == ISCSI_STATE_TERMINATE) {
  746. spin_unlock_bh(&session->lock);
  747. return -EPERM;
  748. }
  749. if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
  750. hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
  751. /*
  752. * Login and Text are sent serially, in
  753. * request-followed-by-response sequence.
  754. * Same mtask can be used. Same ITT must be used.
  755. * Note that login_mtask is preallocated at conn_create().
  756. */
  757. mtask = conn->login_mtask;
  758. else {
  759. BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
  760. BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
  761. nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
  762. if (!__kfifo_get(session->mgmtpool.queue,
  763. (void*)&mtask, sizeof(void*))) {
  764. spin_unlock_bh(&session->lock);
  765. return -ENOSPC;
  766. }
  767. }
  768. /*
  769. * pre-format CmdSN for outgoing PDU.
  770. */
  771. if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) {
  772. hdr->itt = mtask->itt | (conn->id << ISCSI_CID_SHIFT) |
  773. (session->age << ISCSI_AGE_SHIFT);
  774. nop->cmdsn = cpu_to_be32(session->cmdsn);
  775. if (conn->c_stage == ISCSI_CONN_STARTED &&
  776. !(hdr->opcode & ISCSI_OP_IMMEDIATE))
  777. session->cmdsn++;
  778. } else
  779. /* do not advance CmdSN */
  780. nop->cmdsn = cpu_to_be32(session->cmdsn);
  781. if (data_size) {
  782. memcpy(mtask->data, data, data_size);
  783. mtask->data_count = data_size;
  784. } else
  785. mtask->data_count = 0;
  786. INIT_LIST_HEAD(&mtask->running);
  787. memcpy(mtask->hdr, hdr, sizeof(struct iscsi_hdr));
  788. if (session->tt->init_mgmt_task)
  789. session->tt->init_mgmt_task(conn, mtask, data, data_size);
  790. spin_unlock_bh(&session->lock);
  791. debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
  792. hdr->opcode, hdr->itt, data_size);
  793. /*
  794. * since send_pdu() could be called at least from two contexts,
  795. * we need to serialize __kfifo_put, so we don't have to take
  796. * additional lock on fast data-path
  797. */
  798. if (hdr->opcode & ISCSI_OP_IMMEDIATE)
  799. __kfifo_put(conn->immqueue, (void*)&mtask, sizeof(void*));
  800. else
  801. __kfifo_put(conn->mgmtqueue, (void*)&mtask, sizeof(void*));
  802. scsi_queue_work(session->host, &conn->xmitwork);
  803. return 0;
  804. }
  805. int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
  806. char *data, uint32_t data_size)
  807. {
  808. struct iscsi_conn *conn = cls_conn->dd_data;
  809. int rc;
  810. mutex_lock(&conn->xmitmutex);
  811. rc = iscsi_conn_send_generic(conn, hdr, data, data_size);
  812. mutex_unlock(&conn->xmitmutex);
  813. return rc;
  814. }
  815. EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
  816. void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
  817. {
  818. struct iscsi_session *session = class_to_transport_session(cls_session);
  819. struct iscsi_conn *conn = session->leadconn;
  820. spin_lock_bh(&session->lock);
  821. if (session->state != ISCSI_STATE_LOGGED_IN) {
  822. session->state = ISCSI_STATE_RECOVERY_FAILED;
  823. if (conn)
  824. wake_up(&conn->ehwait);
  825. }
  826. spin_unlock_bh(&session->lock);
  827. }
  828. EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
  829. int iscsi_eh_host_reset(struct scsi_cmnd *sc)
  830. {
  831. struct Scsi_Host *host = sc->device->host;
  832. struct iscsi_session *session = iscsi_hostdata(host->hostdata);
  833. struct iscsi_conn *conn = session->leadconn;
  834. int fail_session = 0;
  835. spin_lock_bh(&session->lock);
  836. if (session->state == ISCSI_STATE_TERMINATE) {
  837. failed:
  838. debug_scsi("failing host reset: session terminated "
  839. "[CID %d age %d]", conn->id, session->age);
  840. spin_unlock_bh(&session->lock);
  841. return FAILED;
  842. }
  843. if (sc->SCp.phase == session->age) {
  844. debug_scsi("failing connection CID %d due to SCSI host reset",
  845. conn->id);
  846. fail_session = 1;
  847. }
  848. spin_unlock_bh(&session->lock);
  849. /*
  850. * we drop the lock here but the leadconn cannot be destoyed while
  851. * we are in the scsi eh
  852. */
  853. if (fail_session)
  854. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  855. debug_scsi("iscsi_eh_host_reset wait for relogin\n");
  856. wait_event_interruptible(conn->ehwait,
  857. session->state == ISCSI_STATE_TERMINATE ||
  858. session->state == ISCSI_STATE_LOGGED_IN ||
  859. session->state == ISCSI_STATE_RECOVERY_FAILED);
  860. if (signal_pending(current))
  861. flush_signals(current);
  862. spin_lock_bh(&session->lock);
  863. if (session->state == ISCSI_STATE_LOGGED_IN)
  864. printk(KERN_INFO "iscsi: host reset succeeded\n");
  865. else
  866. goto failed;
  867. spin_unlock_bh(&session->lock);
  868. return SUCCESS;
  869. }
  870. EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
  871. static void iscsi_tmabort_timedout(unsigned long data)
  872. {
  873. struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)data;
  874. struct iscsi_conn *conn = ctask->conn;
  875. struct iscsi_session *session = conn->session;
  876. spin_lock(&session->lock);
  877. if (conn->tmabort_state == TMABORT_INITIAL) {
  878. conn->tmabort_state = TMABORT_TIMEDOUT;
  879. debug_scsi("tmabort timedout [sc %p itt 0x%x]\n",
  880. ctask->sc, ctask->itt);
  881. /* unblock eh_abort() */
  882. wake_up(&conn->ehwait);
  883. }
  884. spin_unlock(&session->lock);
  885. }
  886. /* must be called with the mutex lock */
  887. static int iscsi_exec_abort_task(struct scsi_cmnd *sc,
  888. struct iscsi_cmd_task *ctask)
  889. {
  890. struct iscsi_conn *conn = ctask->conn;
  891. struct iscsi_session *session = conn->session;
  892. struct iscsi_tm *hdr = &conn->tmhdr;
  893. int rc;
  894. /*
  895. * ctask timed out but session is OK requests must be serialized.
  896. */
  897. memset(hdr, 0, sizeof(struct iscsi_tm));
  898. hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
  899. hdr->flags = ISCSI_TM_FUNC_ABORT_TASK;
  900. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  901. memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
  902. hdr->rtt = ctask->hdr->itt;
  903. hdr->refcmdsn = ctask->hdr->cmdsn;
  904. rc = iscsi_conn_send_generic(conn, (struct iscsi_hdr *)hdr,
  905. NULL, 0);
  906. if (rc) {
  907. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  908. debug_scsi("abort sent failure [itt 0x%x] %d", ctask->itt, rc);
  909. return rc;
  910. }
  911. debug_scsi("abort sent [itt 0x%x]\n", ctask->itt);
  912. spin_lock_bh(&session->lock);
  913. ctask->mtask = (struct iscsi_mgmt_task *)
  914. session->mgmt_cmds[(hdr->itt & ISCSI_ITT_MASK) -
  915. ISCSI_MGMT_ITT_OFFSET];
  916. if (conn->tmabort_state == TMABORT_INITIAL) {
  917. conn->tmfcmd_pdus_cnt++;
  918. conn->tmabort_timer.expires = 10*HZ + jiffies;
  919. conn->tmabort_timer.function = iscsi_tmabort_timedout;
  920. conn->tmabort_timer.data = (unsigned long)ctask;
  921. add_timer(&conn->tmabort_timer);
  922. debug_scsi("abort set timeout [itt 0x%x]", ctask->itt);
  923. }
  924. spin_unlock_bh(&session->lock);
  925. mutex_unlock(&conn->xmitmutex);
  926. /*
  927. * block eh thread until:
  928. *
  929. * 1) abort response
  930. * 2) abort timeout
  931. * 3) session is terminated or restarted or userspace has
  932. * given up on recovery
  933. */
  934. wait_event_interruptible(conn->ehwait,
  935. sc->SCp.phase != session->age ||
  936. session->state != ISCSI_STATE_LOGGED_IN ||
  937. conn->tmabort_state != TMABORT_INITIAL);
  938. if (signal_pending(current))
  939. flush_signals(current);
  940. del_timer_sync(&conn->tmabort_timer);
  941. mutex_lock(&conn->xmitmutex);
  942. return 0;
  943. }
  944. /*
  945. * xmit mutex and session lock must be held
  946. */
  947. static struct iscsi_mgmt_task *
  948. iscsi_remove_mgmt_task(struct kfifo *fifo, uint32_t itt)
  949. {
  950. int i, nr_tasks = __kfifo_len(fifo) / sizeof(void*);
  951. struct iscsi_mgmt_task *task;
  952. debug_scsi("searching %d tasks\n", nr_tasks);
  953. for (i = 0; i < nr_tasks; i++) {
  954. __kfifo_get(fifo, (void*)&task, sizeof(void*));
  955. debug_scsi("check task %u\n", task->itt);
  956. if (task->itt == itt) {
  957. debug_scsi("matched task\n");
  958. return task;
  959. }
  960. __kfifo_put(fifo, (void*)&task, sizeof(void*));
  961. }
  962. return NULL;
  963. }
  964. static int iscsi_ctask_mtask_cleanup(struct iscsi_cmd_task *ctask)
  965. {
  966. struct iscsi_conn *conn = ctask->conn;
  967. struct iscsi_session *session = conn->session;
  968. if (!ctask->mtask)
  969. return -EINVAL;
  970. if (!iscsi_remove_mgmt_task(conn->immqueue, ctask->mtask->itt))
  971. list_del(&ctask->mtask->running);
  972. __kfifo_put(session->mgmtpool.queue, (void*)&ctask->mtask,
  973. sizeof(void*));
  974. ctask->mtask = NULL;
  975. return 0;
  976. }
  977. /*
  978. * session lock and xmitmutex must be held
  979. */
  980. static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
  981. int err)
  982. {
  983. struct scsi_cmnd *sc;
  984. sc = ctask->sc;
  985. if (!sc)
  986. return;
  987. conn->session->tt->cleanup_cmd_task(conn, ctask);
  988. iscsi_ctask_mtask_cleanup(ctask);
  989. sc->result = err;
  990. sc->resid = sc->request_bufflen;
  991. /* release ref from queuecommand */
  992. __iscsi_put_ctask(ctask);
  993. }
  994. int iscsi_eh_abort(struct scsi_cmnd *sc)
  995. {
  996. struct iscsi_cmd_task *ctask;
  997. struct iscsi_conn *conn;
  998. struct iscsi_session *session;
  999. int rc;
  1000. /*
  1001. * if session was ISCSI_STATE_IN_RECOVERY then we may not have
  1002. * got the command.
  1003. */
  1004. if (!sc->SCp.ptr) {
  1005. debug_scsi("sc never reached iscsi layer or it completed.\n");
  1006. return SUCCESS;
  1007. }
  1008. ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
  1009. conn = ctask->conn;
  1010. session = conn->session;
  1011. conn->eh_abort_cnt++;
  1012. debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
  1013. mutex_lock(&conn->xmitmutex);
  1014. spin_lock_bh(&session->lock);
  1015. /*
  1016. * If we are not logged in or we have started a new session
  1017. * then let the host reset code handle this
  1018. */
  1019. if (session->state != ISCSI_STATE_LOGGED_IN ||
  1020. sc->SCp.phase != session->age)
  1021. goto failed;
  1022. /* ctask completed before time out */
  1023. if (!ctask->sc) {
  1024. spin_unlock_bh(&session->lock);
  1025. debug_scsi("sc completed while abort in progress\n");
  1026. goto success_rel_mutex;
  1027. }
  1028. /* what should we do here ? */
  1029. if (conn->ctask == ctask) {
  1030. printk(KERN_INFO "iscsi: sc %p itt 0x%x partially sent. "
  1031. "Failing abort\n", sc, ctask->itt);
  1032. goto failed;
  1033. }
  1034. if (ctask->state == ISCSI_TASK_PENDING)
  1035. goto success_cleanup;
  1036. conn->tmabort_state = TMABORT_INITIAL;
  1037. spin_unlock_bh(&session->lock);
  1038. rc = iscsi_exec_abort_task(sc, ctask);
  1039. spin_lock_bh(&session->lock);
  1040. if (rc || sc->SCp.phase != session->age ||
  1041. session->state != ISCSI_STATE_LOGGED_IN)
  1042. goto failed;
  1043. iscsi_ctask_mtask_cleanup(ctask);
  1044. switch (conn->tmabort_state) {
  1045. case TMABORT_SUCCESS:
  1046. goto success_cleanup;
  1047. case TMABORT_NOT_FOUND:
  1048. if (!ctask->sc) {
  1049. /* ctask completed before tmf abort response */
  1050. spin_unlock_bh(&session->lock);
  1051. debug_scsi("sc completed while abort in progress\n");
  1052. goto success_rel_mutex;
  1053. }
  1054. /* fall through */
  1055. default:
  1056. /* timedout or failed */
  1057. spin_unlock_bh(&session->lock);
  1058. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1059. spin_lock_bh(&session->lock);
  1060. goto failed;
  1061. }
  1062. success_cleanup:
  1063. debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
  1064. spin_unlock_bh(&session->lock);
  1065. /*
  1066. * clean up task if aborted. we have the xmitmutex so grab
  1067. * the recv lock as a writer
  1068. */
  1069. write_lock_bh(conn->recv_lock);
  1070. spin_lock(&session->lock);
  1071. fail_command(conn, ctask, DID_ABORT << 16);
  1072. spin_unlock(&session->lock);
  1073. write_unlock_bh(conn->recv_lock);
  1074. success_rel_mutex:
  1075. mutex_unlock(&conn->xmitmutex);
  1076. return SUCCESS;
  1077. failed:
  1078. spin_unlock_bh(&session->lock);
  1079. mutex_unlock(&conn->xmitmutex);
  1080. debug_scsi("abort failed [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
  1081. return FAILED;
  1082. }
  1083. EXPORT_SYMBOL_GPL(iscsi_eh_abort);
  1084. int
  1085. iscsi_pool_init(struct iscsi_queue *q, int max, void ***items, int item_size)
  1086. {
  1087. int i;
  1088. *items = kmalloc(max * sizeof(void*), GFP_KERNEL);
  1089. if (*items == NULL)
  1090. return -ENOMEM;
  1091. q->max = max;
  1092. q->pool = kmalloc(max * sizeof(void*), GFP_KERNEL);
  1093. if (q->pool == NULL) {
  1094. kfree(*items);
  1095. return -ENOMEM;
  1096. }
  1097. q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
  1098. GFP_KERNEL, NULL);
  1099. if (q->queue == ERR_PTR(-ENOMEM)) {
  1100. kfree(q->pool);
  1101. kfree(*items);
  1102. return -ENOMEM;
  1103. }
  1104. for (i = 0; i < max; i++) {
  1105. q->pool[i] = kmalloc(item_size, GFP_KERNEL);
  1106. if (q->pool[i] == NULL) {
  1107. int j;
  1108. for (j = 0; j < i; j++)
  1109. kfree(q->pool[j]);
  1110. kfifo_free(q->queue);
  1111. kfree(q->pool);
  1112. kfree(*items);
  1113. return -ENOMEM;
  1114. }
  1115. memset(q->pool[i], 0, item_size);
  1116. (*items)[i] = q->pool[i];
  1117. __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
  1118. }
  1119. return 0;
  1120. }
  1121. EXPORT_SYMBOL_GPL(iscsi_pool_init);
  1122. void iscsi_pool_free(struct iscsi_queue *q, void **items)
  1123. {
  1124. int i;
  1125. for (i = 0; i < q->max; i++)
  1126. kfree(items[i]);
  1127. kfree(q->pool);
  1128. kfree(items);
  1129. }
  1130. EXPORT_SYMBOL_GPL(iscsi_pool_free);
  1131. /*
  1132. * iSCSI Session's hostdata organization:
  1133. *
  1134. * *------------------* <== hostdata_session(host->hostdata)
  1135. * | ptr to class sess|
  1136. * |------------------| <== iscsi_hostdata(host->hostdata)
  1137. * | iscsi_session |
  1138. * *------------------*
  1139. */
  1140. #define hostdata_privsize(_sz) (sizeof(unsigned long) + _sz + \
  1141. _sz % sizeof(unsigned long))
  1142. #define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
  1143. /**
  1144. * iscsi_session_setup - create iscsi cls session and host and session
  1145. * @scsit: scsi transport template
  1146. * @iscsit: iscsi transport template
  1147. * @initial_cmdsn: initial CmdSN
  1148. * @hostno: host no allocated
  1149. *
  1150. * This can be used by software iscsi_transports that allocate
  1151. * a session per scsi host.
  1152. **/
  1153. struct iscsi_cls_session *
  1154. iscsi_session_setup(struct iscsi_transport *iscsit,
  1155. struct scsi_transport_template *scsit,
  1156. int cmd_task_size, int mgmt_task_size,
  1157. uint32_t initial_cmdsn, uint32_t *hostno)
  1158. {
  1159. struct Scsi_Host *shost;
  1160. struct iscsi_session *session;
  1161. struct iscsi_cls_session *cls_session;
  1162. int cmd_i;
  1163. shost = scsi_host_alloc(iscsit->host_template,
  1164. hostdata_privsize(sizeof(*session)));
  1165. if (!shost)
  1166. return NULL;
  1167. shost->max_id = 1;
  1168. shost->max_channel = 0;
  1169. shost->max_lun = iscsit->max_lun;
  1170. shost->max_cmd_len = iscsit->max_cmd_len;
  1171. shost->transportt = scsit;
  1172. shost->transportt->create_work_queue = 1;
  1173. *hostno = shost->host_no;
  1174. session = iscsi_hostdata(shost->hostdata);
  1175. memset(session, 0, sizeof(struct iscsi_session));
  1176. session->host = shost;
  1177. session->state = ISCSI_STATE_FREE;
  1178. session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
  1179. session->cmds_max = ISCSI_XMIT_CMDS_MAX;
  1180. session->cmdsn = initial_cmdsn;
  1181. session->exp_cmdsn = initial_cmdsn + 1;
  1182. session->max_cmdsn = initial_cmdsn + 1;
  1183. session->max_r2t = 1;
  1184. session->tt = iscsit;
  1185. /* initialize SCSI PDU commands pool */
  1186. if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
  1187. (void***)&session->cmds,
  1188. cmd_task_size + sizeof(struct iscsi_cmd_task)))
  1189. goto cmdpool_alloc_fail;
  1190. /* pre-format cmds pool with ITT */
  1191. for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
  1192. struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
  1193. if (cmd_task_size)
  1194. ctask->dd_data = &ctask[1];
  1195. ctask->itt = cmd_i;
  1196. INIT_LIST_HEAD(&ctask->running);
  1197. }
  1198. spin_lock_init(&session->lock);
  1199. /* initialize immediate command pool */
  1200. if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
  1201. (void***)&session->mgmt_cmds,
  1202. mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
  1203. goto mgmtpool_alloc_fail;
  1204. /* pre-format immediate cmds pool with ITT */
  1205. for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
  1206. struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
  1207. if (mgmt_task_size)
  1208. mtask->dd_data = &mtask[1];
  1209. mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
  1210. INIT_LIST_HEAD(&mtask->running);
  1211. }
  1212. if (scsi_add_host(shost, NULL))
  1213. goto add_host_fail;
  1214. if (!try_module_get(iscsit->owner))
  1215. goto cls_session_fail;
  1216. cls_session = iscsi_create_session(shost, iscsit, 0);
  1217. if (!cls_session)
  1218. goto module_put;
  1219. *(unsigned long*)shost->hostdata = (unsigned long)cls_session;
  1220. return cls_session;
  1221. module_put:
  1222. module_put(iscsit->owner);
  1223. cls_session_fail:
  1224. scsi_remove_host(shost);
  1225. add_host_fail:
  1226. iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
  1227. mgmtpool_alloc_fail:
  1228. iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
  1229. cmdpool_alloc_fail:
  1230. scsi_host_put(shost);
  1231. return NULL;
  1232. }
  1233. EXPORT_SYMBOL_GPL(iscsi_session_setup);
  1234. /**
  1235. * iscsi_session_teardown - destroy session, host, and cls_session
  1236. * shost: scsi host
  1237. *
  1238. * This can be used by software iscsi_transports that allocate
  1239. * a session per scsi host.
  1240. **/
  1241. void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
  1242. {
  1243. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  1244. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  1245. struct module *owner = cls_session->transport->owner;
  1246. scsi_remove_host(shost);
  1247. iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
  1248. iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
  1249. kfree(session->targetname);
  1250. iscsi_destroy_session(cls_session);
  1251. scsi_host_put(shost);
  1252. module_put(owner);
  1253. }
  1254. EXPORT_SYMBOL_GPL(iscsi_session_teardown);
  1255. /**
  1256. * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
  1257. * @cls_session: iscsi_cls_session
  1258. * @conn_idx: cid
  1259. **/
  1260. struct iscsi_cls_conn *
  1261. iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
  1262. {
  1263. struct iscsi_session *session = class_to_transport_session(cls_session);
  1264. struct iscsi_conn *conn;
  1265. struct iscsi_cls_conn *cls_conn;
  1266. char *data;
  1267. cls_conn = iscsi_create_conn(cls_session, conn_idx);
  1268. if (!cls_conn)
  1269. return NULL;
  1270. conn = cls_conn->dd_data;
  1271. memset(conn, 0, sizeof(*conn));
  1272. conn->session = session;
  1273. conn->cls_conn = cls_conn;
  1274. conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
  1275. conn->id = conn_idx;
  1276. conn->exp_statsn = 0;
  1277. conn->tmabort_state = TMABORT_INITIAL;
  1278. INIT_LIST_HEAD(&conn->run_list);
  1279. INIT_LIST_HEAD(&conn->mgmt_run_list);
  1280. INIT_LIST_HEAD(&conn->xmitqueue);
  1281. /* initialize general immediate & non-immediate PDU commands queue */
  1282. conn->immqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
  1283. GFP_KERNEL, NULL);
  1284. if (conn->immqueue == ERR_PTR(-ENOMEM))
  1285. goto immqueue_alloc_fail;
  1286. conn->mgmtqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
  1287. GFP_KERNEL, NULL);
  1288. if (conn->mgmtqueue == ERR_PTR(-ENOMEM))
  1289. goto mgmtqueue_alloc_fail;
  1290. INIT_WORK(&conn->xmitwork, iscsi_xmitworker, conn);
  1291. /* allocate login_mtask used for the login/text sequences */
  1292. spin_lock_bh(&session->lock);
  1293. if (!__kfifo_get(session->mgmtpool.queue,
  1294. (void*)&conn->login_mtask,
  1295. sizeof(void*))) {
  1296. spin_unlock_bh(&session->lock);
  1297. goto login_mtask_alloc_fail;
  1298. }
  1299. spin_unlock_bh(&session->lock);
  1300. data = kmalloc(DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH, GFP_KERNEL);
  1301. if (!data)
  1302. goto login_mtask_data_alloc_fail;
  1303. conn->login_mtask->data = conn->data = data;
  1304. init_timer(&conn->tmabort_timer);
  1305. mutex_init(&conn->xmitmutex);
  1306. init_waitqueue_head(&conn->ehwait);
  1307. return cls_conn;
  1308. login_mtask_data_alloc_fail:
  1309. __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
  1310. sizeof(void*));
  1311. login_mtask_alloc_fail:
  1312. kfifo_free(conn->mgmtqueue);
  1313. mgmtqueue_alloc_fail:
  1314. kfifo_free(conn->immqueue);
  1315. immqueue_alloc_fail:
  1316. iscsi_destroy_conn(cls_conn);
  1317. return NULL;
  1318. }
  1319. EXPORT_SYMBOL_GPL(iscsi_conn_setup);
  1320. /**
  1321. * iscsi_conn_teardown - teardown iscsi connection
  1322. * cls_conn: iscsi class connection
  1323. *
  1324. * TODO: we may need to make this into a two step process
  1325. * like scsi-mls remove + put host
  1326. */
  1327. void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
  1328. {
  1329. struct iscsi_conn *conn = cls_conn->dd_data;
  1330. struct iscsi_session *session = conn->session;
  1331. unsigned long flags;
  1332. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1333. mutex_lock(&conn->xmitmutex);
  1334. spin_lock_bh(&session->lock);
  1335. conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
  1336. if (session->leadconn == conn) {
  1337. /*
  1338. * leading connection? then give up on recovery.
  1339. */
  1340. session->state = ISCSI_STATE_TERMINATE;
  1341. wake_up(&conn->ehwait);
  1342. }
  1343. spin_unlock_bh(&session->lock);
  1344. mutex_unlock(&conn->xmitmutex);
  1345. /*
  1346. * Block until all in-progress commands for this connection
  1347. * time out or fail.
  1348. */
  1349. for (;;) {
  1350. spin_lock_irqsave(session->host->host_lock, flags);
  1351. if (!session->host->host_busy) { /* OK for ERL == 0 */
  1352. spin_unlock_irqrestore(session->host->host_lock, flags);
  1353. break;
  1354. }
  1355. spin_unlock_irqrestore(session->host->host_lock, flags);
  1356. msleep_interruptible(500);
  1357. printk(KERN_INFO "iscsi: scsi conn_destroy(): host_busy %d "
  1358. "host_failed %d\n", session->host->host_busy,
  1359. session->host->host_failed);
  1360. /*
  1361. * force eh_abort() to unblock
  1362. */
  1363. wake_up(&conn->ehwait);
  1364. }
  1365. spin_lock_bh(&session->lock);
  1366. kfree(conn->data);
  1367. kfree(conn->persistent_address);
  1368. __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
  1369. sizeof(void*));
  1370. if (session->leadconn == conn) {
  1371. session->leadconn = NULL;
  1372. /* no connections exits.. reset sequencing */
  1373. session->cmdsn = session->max_cmdsn = session->exp_cmdsn = 1;
  1374. }
  1375. spin_unlock_bh(&session->lock);
  1376. kfifo_free(conn->immqueue);
  1377. kfifo_free(conn->mgmtqueue);
  1378. iscsi_destroy_conn(cls_conn);
  1379. }
  1380. EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
  1381. int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
  1382. {
  1383. struct iscsi_conn *conn = cls_conn->dd_data;
  1384. struct iscsi_session *session = conn->session;
  1385. if (!session) {
  1386. printk(KERN_ERR "iscsi: can't start unbound connection\n");
  1387. return -EPERM;
  1388. }
  1389. if ((session->imm_data_en || !session->initial_r2t_en) &&
  1390. session->first_burst > session->max_burst) {
  1391. printk("iscsi: invalid burst lengths: "
  1392. "first_burst %d max_burst %d\n",
  1393. session->first_burst, session->max_burst);
  1394. return -EINVAL;
  1395. }
  1396. spin_lock_bh(&session->lock);
  1397. conn->c_stage = ISCSI_CONN_STARTED;
  1398. session->state = ISCSI_STATE_LOGGED_IN;
  1399. switch(conn->stop_stage) {
  1400. case STOP_CONN_RECOVER:
  1401. /*
  1402. * unblock eh_abort() if it is blocked. re-try all
  1403. * commands after successful recovery
  1404. */
  1405. conn->stop_stage = 0;
  1406. conn->tmabort_state = TMABORT_INITIAL;
  1407. session->age++;
  1408. spin_unlock_bh(&session->lock);
  1409. iscsi_unblock_session(session_to_cls(session));
  1410. wake_up(&conn->ehwait);
  1411. return 0;
  1412. case STOP_CONN_TERM:
  1413. conn->stop_stage = 0;
  1414. break;
  1415. default:
  1416. break;
  1417. }
  1418. spin_unlock_bh(&session->lock);
  1419. return 0;
  1420. }
  1421. EXPORT_SYMBOL_GPL(iscsi_conn_start);
  1422. static void
  1423. flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
  1424. {
  1425. struct iscsi_mgmt_task *mtask, *tmp;
  1426. /* handle pending */
  1427. while (__kfifo_get(conn->immqueue, (void*)&mtask, sizeof(void*)) ||
  1428. __kfifo_get(conn->mgmtqueue, (void*)&mtask, sizeof(void*))) {
  1429. if (mtask == conn->login_mtask)
  1430. continue;
  1431. debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt);
  1432. __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
  1433. sizeof(void*));
  1434. }
  1435. /* handle running */
  1436. list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) {
  1437. debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt);
  1438. list_del(&mtask->running);
  1439. if (mtask == conn->login_mtask)
  1440. continue;
  1441. __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
  1442. sizeof(void*));
  1443. }
  1444. conn->mtask = NULL;
  1445. }
  1446. /* Fail commands. Mutex and session lock held and recv side suspended */
  1447. static void fail_all_commands(struct iscsi_conn *conn)
  1448. {
  1449. struct iscsi_cmd_task *ctask, *tmp;
  1450. /* flush pending */
  1451. list_for_each_entry_safe(ctask, tmp, &conn->xmitqueue, running) {
  1452. debug_scsi("failing pending sc %p itt 0x%x\n", ctask->sc,
  1453. ctask->itt);
  1454. fail_command(conn, ctask, DID_BUS_BUSY << 16);
  1455. }
  1456. /* fail all other running */
  1457. list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) {
  1458. debug_scsi("failing in progress sc %p itt 0x%x\n",
  1459. ctask->sc, ctask->itt);
  1460. fail_command(conn, ctask, DID_BUS_BUSY << 16);
  1461. }
  1462. conn->ctask = NULL;
  1463. }
  1464. static void iscsi_start_session_recovery(struct iscsi_session *session,
  1465. struct iscsi_conn *conn, int flag)
  1466. {
  1467. int old_stop_stage;
  1468. spin_lock_bh(&session->lock);
  1469. if (conn->stop_stage == STOP_CONN_TERM) {
  1470. spin_unlock_bh(&session->lock);
  1471. return;
  1472. }
  1473. /*
  1474. * When this is called for the in_login state, we only want to clean
  1475. * up the login task and connection. We do not need to block and set
  1476. * the recovery state again
  1477. */
  1478. if (flag == STOP_CONN_TERM)
  1479. session->state = ISCSI_STATE_TERMINATE;
  1480. else if (conn->stop_stage != STOP_CONN_RECOVER)
  1481. session->state = ISCSI_STATE_IN_RECOVERY;
  1482. old_stop_stage = conn->stop_stage;
  1483. conn->stop_stage = flag;
  1484. conn->c_stage = ISCSI_CONN_STOPPED;
  1485. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1486. spin_unlock_bh(&session->lock);
  1487. write_lock_bh(conn->recv_lock);
  1488. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  1489. write_unlock_bh(conn->recv_lock);
  1490. mutex_lock(&conn->xmitmutex);
  1491. /*
  1492. * for connection level recovery we should not calculate
  1493. * header digest. conn->hdr_size used for optimization
  1494. * in hdr_extract() and will be re-negotiated at
  1495. * set_param() time.
  1496. */
  1497. if (flag == STOP_CONN_RECOVER) {
  1498. conn->hdrdgst_en = 0;
  1499. conn->datadgst_en = 0;
  1500. if (session->state == ISCSI_STATE_IN_RECOVERY &&
  1501. old_stop_stage != STOP_CONN_RECOVER) {
  1502. debug_scsi("blocking session\n");
  1503. iscsi_block_session(session_to_cls(session));
  1504. }
  1505. }
  1506. /*
  1507. * flush queues.
  1508. */
  1509. spin_lock_bh(&session->lock);
  1510. fail_all_commands(conn);
  1511. flush_control_queues(session, conn);
  1512. spin_unlock_bh(&session->lock);
  1513. mutex_unlock(&conn->xmitmutex);
  1514. }
  1515. void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
  1516. {
  1517. struct iscsi_conn *conn = cls_conn->dd_data;
  1518. struct iscsi_session *session = conn->session;
  1519. switch (flag) {
  1520. case STOP_CONN_RECOVER:
  1521. case STOP_CONN_TERM:
  1522. iscsi_start_session_recovery(session, conn, flag);
  1523. break;
  1524. default:
  1525. printk(KERN_ERR "iscsi: invalid stop flag %d\n", flag);
  1526. }
  1527. }
  1528. EXPORT_SYMBOL_GPL(iscsi_conn_stop);
  1529. int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
  1530. struct iscsi_cls_conn *cls_conn, int is_leading)
  1531. {
  1532. struct iscsi_session *session = class_to_transport_session(cls_session);
  1533. struct iscsi_conn *conn = cls_conn->dd_data;
  1534. spin_lock_bh(&session->lock);
  1535. if (is_leading)
  1536. session->leadconn = conn;
  1537. spin_unlock_bh(&session->lock);
  1538. /*
  1539. * Unblock xmitworker(), Login Phase will pass through.
  1540. */
  1541. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  1542. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1543. return 0;
  1544. }
  1545. EXPORT_SYMBOL_GPL(iscsi_conn_bind);
  1546. int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
  1547. enum iscsi_param param, char *buf, int buflen)
  1548. {
  1549. struct iscsi_conn *conn = cls_conn->dd_data;
  1550. struct iscsi_session *session = conn->session;
  1551. uint32_t value;
  1552. switch(param) {
  1553. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  1554. sscanf(buf, "%d", &conn->max_recv_dlength);
  1555. break;
  1556. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  1557. sscanf(buf, "%d", &conn->max_xmit_dlength);
  1558. break;
  1559. case ISCSI_PARAM_HDRDGST_EN:
  1560. sscanf(buf, "%d", &conn->hdrdgst_en);
  1561. break;
  1562. case ISCSI_PARAM_DATADGST_EN:
  1563. sscanf(buf, "%d", &conn->datadgst_en);
  1564. break;
  1565. case ISCSI_PARAM_INITIAL_R2T_EN:
  1566. sscanf(buf, "%d", &session->initial_r2t_en);
  1567. break;
  1568. case ISCSI_PARAM_MAX_R2T:
  1569. sscanf(buf, "%d", &session->max_r2t);
  1570. break;
  1571. case ISCSI_PARAM_IMM_DATA_EN:
  1572. sscanf(buf, "%d", &session->imm_data_en);
  1573. break;
  1574. case ISCSI_PARAM_FIRST_BURST:
  1575. sscanf(buf, "%d", &session->first_burst);
  1576. break;
  1577. case ISCSI_PARAM_MAX_BURST:
  1578. sscanf(buf, "%d", &session->max_burst);
  1579. break;
  1580. case ISCSI_PARAM_PDU_INORDER_EN:
  1581. sscanf(buf, "%d", &session->pdu_inorder_en);
  1582. break;
  1583. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  1584. sscanf(buf, "%d", &session->dataseq_inorder_en);
  1585. break;
  1586. case ISCSI_PARAM_ERL:
  1587. sscanf(buf, "%d", &session->erl);
  1588. break;
  1589. case ISCSI_PARAM_IFMARKER_EN:
  1590. sscanf(buf, "%d", &value);
  1591. BUG_ON(value);
  1592. break;
  1593. case ISCSI_PARAM_OFMARKER_EN:
  1594. sscanf(buf, "%d", &value);
  1595. BUG_ON(value);
  1596. break;
  1597. case ISCSI_PARAM_EXP_STATSN:
  1598. sscanf(buf, "%u", &conn->exp_statsn);
  1599. break;
  1600. case ISCSI_PARAM_TARGET_NAME:
  1601. /* this should not change between logins */
  1602. if (session->targetname)
  1603. break;
  1604. session->targetname = kstrdup(buf, GFP_KERNEL);
  1605. if (!session->targetname)
  1606. return -ENOMEM;
  1607. break;
  1608. case ISCSI_PARAM_TPGT:
  1609. sscanf(buf, "%d", &session->tpgt);
  1610. break;
  1611. case ISCSI_PARAM_PERSISTENT_PORT:
  1612. sscanf(buf, "%d", &conn->persistent_port);
  1613. break;
  1614. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  1615. /*
  1616. * this is the address returned in discovery so it should
  1617. * not change between logins.
  1618. */
  1619. if (conn->persistent_address)
  1620. break;
  1621. conn->persistent_address = kstrdup(buf, GFP_KERNEL);
  1622. if (!conn->persistent_address)
  1623. return -ENOMEM;
  1624. break;
  1625. default:
  1626. return -ENOSYS;
  1627. }
  1628. return 0;
  1629. }
  1630. EXPORT_SYMBOL_GPL(iscsi_set_param);
  1631. int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
  1632. enum iscsi_param param, char *buf)
  1633. {
  1634. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  1635. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  1636. int len;
  1637. switch(param) {
  1638. case ISCSI_PARAM_INITIAL_R2T_EN:
  1639. len = sprintf(buf, "%d\n", session->initial_r2t_en);
  1640. break;
  1641. case ISCSI_PARAM_MAX_R2T:
  1642. len = sprintf(buf, "%hu\n", session->max_r2t);
  1643. break;
  1644. case ISCSI_PARAM_IMM_DATA_EN:
  1645. len = sprintf(buf, "%d\n", session->imm_data_en);
  1646. break;
  1647. case ISCSI_PARAM_FIRST_BURST:
  1648. len = sprintf(buf, "%u\n", session->first_burst);
  1649. break;
  1650. case ISCSI_PARAM_MAX_BURST:
  1651. len = sprintf(buf, "%u\n", session->max_burst);
  1652. break;
  1653. case ISCSI_PARAM_PDU_INORDER_EN:
  1654. len = sprintf(buf, "%d\n", session->pdu_inorder_en);
  1655. break;
  1656. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  1657. len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
  1658. break;
  1659. case ISCSI_PARAM_ERL:
  1660. len = sprintf(buf, "%d\n", session->erl);
  1661. break;
  1662. case ISCSI_PARAM_TARGET_NAME:
  1663. len = sprintf(buf, "%s\n", session->targetname);
  1664. break;
  1665. case ISCSI_PARAM_TPGT:
  1666. len = sprintf(buf, "%d\n", session->tpgt);
  1667. break;
  1668. default:
  1669. return -ENOSYS;
  1670. }
  1671. return len;
  1672. }
  1673. EXPORT_SYMBOL_GPL(iscsi_session_get_param);
  1674. int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
  1675. enum iscsi_param param, char *buf)
  1676. {
  1677. struct iscsi_conn *conn = cls_conn->dd_data;
  1678. int len;
  1679. switch(param) {
  1680. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  1681. len = sprintf(buf, "%u\n", conn->max_recv_dlength);
  1682. break;
  1683. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  1684. len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
  1685. break;
  1686. case ISCSI_PARAM_HDRDGST_EN:
  1687. len = sprintf(buf, "%d\n", conn->hdrdgst_en);
  1688. break;
  1689. case ISCSI_PARAM_DATADGST_EN:
  1690. len = sprintf(buf, "%d\n", conn->datadgst_en);
  1691. break;
  1692. case ISCSI_PARAM_IFMARKER_EN:
  1693. len = sprintf(buf, "%d\n", conn->ifmarker_en);
  1694. break;
  1695. case ISCSI_PARAM_OFMARKER_EN:
  1696. len = sprintf(buf, "%d\n", conn->ofmarker_en);
  1697. break;
  1698. case ISCSI_PARAM_EXP_STATSN:
  1699. len = sprintf(buf, "%u\n", conn->exp_statsn);
  1700. break;
  1701. case ISCSI_PARAM_PERSISTENT_PORT:
  1702. len = sprintf(buf, "%d\n", conn->persistent_port);
  1703. break;
  1704. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  1705. len = sprintf(buf, "%s\n", conn->persistent_address);
  1706. break;
  1707. default:
  1708. return -ENOSYS;
  1709. }
  1710. return len;
  1711. }
  1712. EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
  1713. MODULE_AUTHOR("Mike Christie");
  1714. MODULE_DESCRIPTION("iSCSI library functions");
  1715. MODULE_LICENSE("GPL");